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 if (bed
->link_order_error_handler
)
861 bed
->link_order_error_handler
862 /* xgettext:c-format */
863 (_("%pB: warning: sh_link not set for section `%pA'"),
868 asection
*linksec
= NULL
;
870 if (elfsec
< elf_numsections (abfd
))
872 this_hdr
= elf_elfsections (abfd
)[elfsec
];
873 linksec
= this_hdr
->bfd_section
;
877 Some strip/objcopy may leave an incorrect value in
878 sh_link. We don't want to proceed. */
882 /* xgettext:c-format */
883 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
884 s
->owner
, elfsec
, s
);
888 elf_linked_to_section (s
) = linksec
;
891 else if (this_hdr
->sh_type
== SHT_GROUP
892 && elf_next_in_group (s
) == NULL
)
895 /* xgettext:c-format */
896 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
897 abfd
, elf_section_data (s
)->this_idx
);
902 /* Process section groups. */
903 if (num_group
== (unsigned) -1)
906 for (i
= 0; i
< num_group
; i
++)
908 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
909 Elf_Internal_Group
*idx
;
912 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
913 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
916 /* xgettext:c-format */
917 (_("%pB: section group entry number %u is corrupt"),
923 idx
= (Elf_Internal_Group
*) shdr
->contents
;
924 n_elt
= shdr
->sh_size
/ 4;
930 if (idx
->shdr
== NULL
)
932 else if (idx
->shdr
->bfd_section
)
933 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
934 else if (idx
->shdr
->sh_type
!= SHT_RELA
935 && idx
->shdr
->sh_type
!= SHT_REL
)
937 /* There are some unknown sections in the group. */
939 /* xgettext:c-format */
940 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
943 bfd_elf_string_from_elf_section (abfd
,
944 (elf_elfheader (abfd
)
957 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
959 return elf_next_in_group (sec
) != NULL
;
963 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
965 if (elf_sec_group (sec
) != NULL
)
966 return elf_group_name (sec
);
971 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
973 unsigned int len
= strlen (name
);
974 char *new_name
= bfd_alloc (abfd
, len
+ 2);
975 if (new_name
== NULL
)
979 memcpy (new_name
+ 2, name
+ 1, len
);
984 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
986 unsigned int len
= strlen (name
);
987 char *new_name
= bfd_alloc (abfd
, len
);
988 if (new_name
== NULL
)
991 memcpy (new_name
+ 1, name
+ 2, len
- 1);
995 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
999 int16_t major_version
;
1000 int16_t minor_version
;
1001 unsigned char slim_object
;
1003 /* Flags is a private field that is not defined publicly. */
1007 /* Make a BFD section from an ELF section. We store a pointer to the
1008 BFD section in the bfd_section field of the header. */
1011 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1012 Elf_Internal_Shdr
*hdr
,
1018 const struct elf_backend_data
*bed
;
1019 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
1021 if (hdr
->bfd_section
!= NULL
)
1024 newsect
= bfd_make_section_anyway (abfd
, name
);
1025 if (newsect
== NULL
)
1028 hdr
->bfd_section
= newsect
;
1029 elf_section_data (newsect
)->this_hdr
= *hdr
;
1030 elf_section_data (newsect
)->this_idx
= shindex
;
1032 /* Always use the real type/flags. */
1033 elf_section_type (newsect
) = hdr
->sh_type
;
1034 elf_section_flags (newsect
) = hdr
->sh_flags
;
1036 newsect
->filepos
= hdr
->sh_offset
;
1038 flags
= SEC_NO_FLAGS
;
1039 if (hdr
->sh_type
!= SHT_NOBITS
)
1040 flags
|= SEC_HAS_CONTENTS
;
1041 if (hdr
->sh_type
== SHT_GROUP
)
1043 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1046 if (hdr
->sh_type
!= SHT_NOBITS
)
1049 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1050 flags
|= SEC_READONLY
;
1051 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1053 else if ((flags
& SEC_LOAD
) != 0)
1055 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1058 newsect
->entsize
= hdr
->sh_entsize
;
1060 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1061 flags
|= SEC_STRINGS
;
1062 if (hdr
->sh_flags
& SHF_GROUP
)
1063 if (!setup_group (abfd
, hdr
, newsect
))
1065 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1066 flags
|= SEC_THREAD_LOCAL
;
1067 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1068 flags
|= SEC_EXCLUDE
;
1070 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1072 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1073 but binutils as of 2019-07-23 did not set the EI_OSABI header
1077 case ELFOSABI_FREEBSD
:
1078 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1079 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1083 if ((flags
& SEC_ALLOC
) == 0)
1085 /* The debugging sections appear to be recognized only by name,
1086 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1087 if (name
[0] == '.')
1089 if (strncmp (name
, ".debug", 6) == 0
1090 || strncmp (name
, ".gnu.linkonce.wi.", 17) == 0
1091 || strncmp (name
, ".zdebug", 7) == 0)
1092 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1093 else if (strncmp (name
, GNU_BUILD_ATTRS_SECTION_NAME
, 21) == 0
1094 || strncmp (name
, ".note.gnu", 9) == 0)
1096 flags
|= SEC_ELF_OCTETS
;
1099 else if (strncmp (name
, ".line", 5) == 0
1100 || strncmp (name
, ".stab", 5) == 0
1101 || strcmp (name
, ".gdb_index") == 0)
1102 flags
|= SEC_DEBUGGING
;
1106 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1107 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1108 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
)))
1111 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1112 only link a single copy of the section. This is used to support
1113 g++. g++ will emit each template expansion in its own section.
1114 The symbols will be defined as weak, so that multiple definitions
1115 are permitted. The GNU linker extension is to actually discard
1116 all but one of the sections. */
1117 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1118 && elf_next_in_group (newsect
) == NULL
)
1119 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1121 if (!bfd_set_section_flags (newsect
, flags
))
1124 bed
= get_elf_backend_data (abfd
);
1125 if (bed
->elf_backend_section_flags
)
1126 if (!bed
->elf_backend_section_flags (hdr
))
1129 /* We do not parse the PT_NOTE segments as we are interested even in the
1130 separate debug info files which may have the segments offsets corrupted.
1131 PT_NOTEs from the core files are currently not parsed using BFD. */
1132 if (hdr
->sh_type
== SHT_NOTE
)
1136 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1139 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1140 hdr
->sh_offset
, hdr
->sh_addralign
);
1144 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1146 Elf_Internal_Phdr
*phdr
;
1147 unsigned int i
, nload
;
1149 /* Some ELF linkers produce binaries with all the program header
1150 p_paddr fields zero. If we have such a binary with more than
1151 one PT_LOAD header, then leave the section lma equal to vma
1152 so that we don't create sections with overlapping lma. */
1153 phdr
= elf_tdata (abfd
)->phdr
;
1154 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1155 if (phdr
->p_paddr
!= 0)
1157 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1159 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1162 phdr
= elf_tdata (abfd
)->phdr
;
1163 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1165 if (((phdr
->p_type
== PT_LOAD
1166 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1167 || phdr
->p_type
== PT_TLS
)
1168 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1170 if ((newsect
->flags
& SEC_LOAD
) == 0)
1171 newsect
->lma
= (phdr
->p_paddr
1172 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1174 /* We used to use the same adjustment for SEC_LOAD
1175 sections, but that doesn't work if the segment
1176 is packed with code from multiple VMAs.
1177 Instead we calculate the section LMA based on
1178 the segment LMA. It is assumed that the
1179 segment will contain sections with contiguous
1180 LMAs, even if the VMAs are not. */
1181 newsect
->lma
= (phdr
->p_paddr
1182 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1184 /* With contiguous segments, we can't tell from file
1185 offsets whether a section with zero size should
1186 be placed at the end of one segment or the
1187 beginning of the next. Decide based on vaddr. */
1188 if (hdr
->sh_addr
>= phdr
->p_vaddr
1189 && (hdr
->sh_addr
+ hdr
->sh_size
1190 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1196 /* Compress/decompress DWARF debug sections with names: .debug_* and
1197 .zdebug_*, after the section flags is set. */
1198 if ((newsect
->flags
& SEC_DEBUGGING
)
1199 && ((name
[1] == 'd' && name
[6] == '_')
1200 || (name
[1] == 'z' && name
[7] == '_')))
1202 enum { nothing
, compress
, decompress
} action
= nothing
;
1203 int compression_header_size
;
1204 bfd_size_type uncompressed_size
;
1205 unsigned int uncompressed_align_power
;
1206 bfd_boolean compressed
1207 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1208 &compression_header_size
,
1210 &uncompressed_align_power
);
1213 /* Compressed section. Check if we should decompress. */
1214 if ((abfd
->flags
& BFD_DECOMPRESS
))
1215 action
= decompress
;
1218 /* Compress the uncompressed section or convert from/to .zdebug*
1219 section. Check if we should compress. */
1220 if (action
== nothing
)
1222 if (newsect
->size
!= 0
1223 && (abfd
->flags
& BFD_COMPRESS
)
1224 && compression_header_size
>= 0
1225 && uncompressed_size
> 0
1227 || ((compression_header_size
> 0)
1228 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1234 if (action
== compress
)
1236 if (!bfd_init_section_compress_status (abfd
, newsect
))
1239 /* xgettext:c-format */
1240 (_("%pB: unable to initialize compress status for section %s"),
1247 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1250 /* xgettext:c-format */
1251 (_("%pB: unable to initialize decompress status for section %s"),
1257 if (abfd
->is_linker_input
)
1260 && (action
== decompress
1261 || (action
== compress
1262 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1264 /* Convert section name from .zdebug_* to .debug_* so
1265 that linker will consider this section as a debug
1267 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1268 if (new_name
== NULL
)
1270 bfd_rename_section (newsect
, new_name
);
1274 /* For objdump, don't rename the section. For objcopy, delay
1275 section rename to elf_fake_sections. */
1276 newsect
->flags
|= SEC_ELF_RENAME
;
1279 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1281 const char *lto_section_name
= ".gnu.lto_.lto.";
1282 if (strncmp (name
, lto_section_name
, strlen (lto_section_name
)) == 0)
1284 struct lto_section lsection
;
1285 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1286 sizeof (struct lto_section
)))
1287 abfd
->lto_slim_object
= lsection
.slim_object
;
1293 const char *const bfd_elf_section_type_names
[] =
1295 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1296 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1297 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1300 /* ELF relocs are against symbols. If we are producing relocatable
1301 output, and the reloc is against an external symbol, and nothing
1302 has given us any additional addend, the resulting reloc will also
1303 be against the same symbol. In such a case, we don't want to
1304 change anything about the way the reloc is handled, since it will
1305 all be done at final link time. Rather than put special case code
1306 into bfd_perform_relocation, all the reloc types use this howto
1307 function. It just short circuits the reloc if producing
1308 relocatable output against an external symbol. */
1310 bfd_reloc_status_type
1311 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1312 arelent
*reloc_entry
,
1314 void *data ATTRIBUTE_UNUSED
,
1315 asection
*input_section
,
1317 char **error_message ATTRIBUTE_UNUSED
)
1319 if (output_bfd
!= NULL
1320 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1321 && (! reloc_entry
->howto
->partial_inplace
1322 || reloc_entry
->addend
== 0))
1324 reloc_entry
->address
+= input_section
->output_offset
;
1325 return bfd_reloc_ok
;
1328 return bfd_reloc_continue
;
1331 /* Returns TRUE if section A matches section B.
1332 Names, addresses and links may be different, but everything else
1333 should be the same. */
1336 section_match (const Elf_Internal_Shdr
* a
,
1337 const Elf_Internal_Shdr
* b
)
1339 if (a
->sh_type
!= b
->sh_type
1340 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1341 || a
->sh_addralign
!= b
->sh_addralign
1342 || a
->sh_entsize
!= b
->sh_entsize
)
1344 if (a
->sh_type
== SHT_SYMTAB
1345 || a
->sh_type
== SHT_STRTAB
)
1347 return a
->sh_size
== b
->sh_size
;
1350 /* Find a section in OBFD that has the same characteristics
1351 as IHEADER. Return the index of this section or SHN_UNDEF if
1352 none can be found. Check's section HINT first, as this is likely
1353 to be the correct section. */
1356 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1357 const unsigned int hint
)
1359 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1362 BFD_ASSERT (iheader
!= NULL
);
1364 /* See PR 20922 for a reproducer of the NULL test. */
1365 if (hint
< elf_numsections (obfd
)
1366 && oheaders
[hint
] != NULL
1367 && section_match (oheaders
[hint
], iheader
))
1370 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1372 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1374 if (oheader
== NULL
)
1376 if (section_match (oheader
, iheader
))
1377 /* FIXME: Do we care if there is a potential for
1378 multiple matches ? */
1385 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1386 Processor specific section, based upon a matching input section.
1387 Returns TRUE upon success, FALSE otherwise. */
1390 copy_special_section_fields (const bfd
*ibfd
,
1392 const Elf_Internal_Shdr
*iheader
,
1393 Elf_Internal_Shdr
*oheader
,
1394 const unsigned int secnum
)
1396 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1397 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1398 bfd_boolean changed
= FALSE
;
1399 unsigned int sh_link
;
1401 if (oheader
->sh_type
== SHT_NOBITS
)
1403 /* This is a feature for objcopy --only-keep-debug:
1404 When a section's type is changed to NOBITS, we preserve
1405 the sh_link and sh_info fields so that they can be
1406 matched up with the original.
1408 Note: Strictly speaking these assignments are wrong.
1409 The sh_link and sh_info fields should point to the
1410 relevent sections in the output BFD, which may not be in
1411 the same location as they were in the input BFD. But
1412 the whole point of this action is to preserve the
1413 original values of the sh_link and sh_info fields, so
1414 that they can be matched up with the section headers in
1415 the original file. So strictly speaking we may be
1416 creating an invalid ELF file, but it is only for a file
1417 that just contains debug info and only for sections
1418 without any contents. */
1419 if (oheader
->sh_link
== 0)
1420 oheader
->sh_link
= iheader
->sh_link
;
1421 if (oheader
->sh_info
== 0)
1422 oheader
->sh_info
= iheader
->sh_info
;
1426 /* Allow the target a chance to decide how these fields should be set. */
1427 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1428 && bed
->elf_backend_copy_special_section_fields
1429 (ibfd
, obfd
, iheader
, oheader
))
1432 /* We have an iheader which might match oheader, and which has non-zero
1433 sh_info and/or sh_link fields. Attempt to follow those links and find
1434 the section in the output bfd which corresponds to the linked section
1435 in the input bfd. */
1436 if (iheader
->sh_link
!= SHN_UNDEF
)
1438 /* See PR 20931 for a reproducer. */
1439 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1442 /* xgettext:c-format */
1443 (_("%pB: invalid sh_link field (%d) in section number %d"),
1444 ibfd
, iheader
->sh_link
, secnum
);
1448 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1449 if (sh_link
!= SHN_UNDEF
)
1451 oheader
->sh_link
= sh_link
;
1455 /* FIXME: Should we install iheader->sh_link
1456 if we could not find a match ? */
1458 /* xgettext:c-format */
1459 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1462 if (iheader
->sh_info
)
1464 /* The sh_info field can hold arbitrary information, but if the
1465 SHF_LINK_INFO flag is set then it should be interpreted as a
1467 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1469 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1471 if (sh_link
!= SHN_UNDEF
)
1472 oheader
->sh_flags
|= SHF_INFO_LINK
;
1475 /* No idea what it means - just copy it. */
1476 sh_link
= iheader
->sh_info
;
1478 if (sh_link
!= SHN_UNDEF
)
1480 oheader
->sh_info
= sh_link
;
1485 /* xgettext:c-format */
1486 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1492 /* Copy the program header and other data from one object module to
1496 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1498 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1499 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1500 const struct elf_backend_data
*bed
;
1503 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1504 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1507 if (!elf_flags_init (obfd
))
1509 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1510 elf_flags_init (obfd
) = TRUE
;
1513 elf_gp (obfd
) = elf_gp (ibfd
);
1515 /* Also copy the EI_OSABI field. */
1516 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1517 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1519 /* If set, copy the EI_ABIVERSION field. */
1520 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1521 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1522 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1524 /* Copy object attributes. */
1525 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1527 if (iheaders
== NULL
|| oheaders
== NULL
)
1530 bed
= get_elf_backend_data (obfd
);
1532 /* Possibly copy other fields in the section header. */
1533 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1536 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1538 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1539 because of a special case need for generating separate debug info
1540 files. See below for more details. */
1542 || (oheader
->sh_type
!= SHT_NOBITS
1543 && oheader
->sh_type
< SHT_LOOS
))
1546 /* Ignore empty sections, and sections whose
1547 fields have already been initialised. */
1548 if (oheader
->sh_size
== 0
1549 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1552 /* Scan for the matching section in the input bfd.
1553 First we try for a direct mapping between the input and output sections. */
1554 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1556 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1558 if (iheader
== NULL
)
1561 if (oheader
->bfd_section
!= NULL
1562 && iheader
->bfd_section
!= NULL
1563 && iheader
->bfd_section
->output_section
!= NULL
1564 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1566 /* We have found a connection from the input section to the
1567 output section. Attempt to copy the header fields. If
1568 this fails then do not try any further sections - there
1569 should only be a one-to-one mapping between input and output. */
1570 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1571 j
= elf_numsections (ibfd
);
1576 if (j
< elf_numsections (ibfd
))
1579 /* That failed. So try to deduce the corresponding input section.
1580 Unfortunately we cannot compare names as the output string table
1581 is empty, so instead we check size, address and type. */
1582 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1584 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1586 if (iheader
== NULL
)
1589 /* Try matching fields in the input section's header.
1590 Since --only-keep-debug turns all non-debug sections into
1591 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1593 if ((oheader
->sh_type
== SHT_NOBITS
1594 || iheader
->sh_type
== oheader
->sh_type
)
1595 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1596 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1597 && iheader
->sh_addralign
== oheader
->sh_addralign
1598 && iheader
->sh_entsize
== oheader
->sh_entsize
1599 && iheader
->sh_size
== oheader
->sh_size
1600 && iheader
->sh_addr
== oheader
->sh_addr
1601 && (iheader
->sh_info
!= oheader
->sh_info
1602 || iheader
->sh_link
!= oheader
->sh_link
))
1604 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1609 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1611 /* Final attempt. Call the backend copy function
1612 with a NULL input section. */
1613 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1614 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1622 get_segment_type (unsigned int p_type
)
1627 case PT_NULL
: pt
= "NULL"; break;
1628 case PT_LOAD
: pt
= "LOAD"; break;
1629 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1630 case PT_INTERP
: pt
= "INTERP"; break;
1631 case PT_NOTE
: pt
= "NOTE"; break;
1632 case PT_SHLIB
: pt
= "SHLIB"; break;
1633 case PT_PHDR
: pt
= "PHDR"; break;
1634 case PT_TLS
: pt
= "TLS"; break;
1635 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1636 case PT_GNU_STACK
: pt
= "STACK"; break;
1637 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1638 default: pt
= NULL
; break;
1643 /* Print out the program headers. */
1646 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1648 FILE *f
= (FILE *) farg
;
1649 Elf_Internal_Phdr
*p
;
1651 bfd_byte
*dynbuf
= NULL
;
1653 p
= elf_tdata (abfd
)->phdr
;
1658 fprintf (f
, _("\nProgram Header:\n"));
1659 c
= elf_elfheader (abfd
)->e_phnum
;
1660 for (i
= 0; i
< c
; i
++, p
++)
1662 const char *pt
= get_segment_type (p
->p_type
);
1667 sprintf (buf
, "0x%lx", p
->p_type
);
1670 fprintf (f
, "%8s off 0x", pt
);
1671 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1672 fprintf (f
, " vaddr 0x");
1673 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1674 fprintf (f
, " paddr 0x");
1675 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1676 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1677 fprintf (f
, " filesz 0x");
1678 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1679 fprintf (f
, " memsz 0x");
1680 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1681 fprintf (f
, " flags %c%c%c",
1682 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1683 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1684 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1685 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1686 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1691 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1694 unsigned int elfsec
;
1695 unsigned long shlink
;
1696 bfd_byte
*extdyn
, *extdynend
;
1698 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1700 fprintf (f
, _("\nDynamic Section:\n"));
1702 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1705 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1706 if (elfsec
== SHN_BAD
)
1708 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1710 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1711 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1714 /* PR 17512: file: 6f427532. */
1715 if (s
->size
< extdynsize
)
1717 extdynend
= extdyn
+ s
->size
;
1718 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1720 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1722 Elf_Internal_Dyn dyn
;
1723 const char *name
= "";
1725 bfd_boolean stringp
;
1726 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1728 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1730 if (dyn
.d_tag
== DT_NULL
)
1737 if (bed
->elf_backend_get_target_dtag
)
1738 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1740 if (!strcmp (name
, ""))
1742 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1747 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1748 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1749 case DT_PLTGOT
: name
= "PLTGOT"; break;
1750 case DT_HASH
: name
= "HASH"; break;
1751 case DT_STRTAB
: name
= "STRTAB"; break;
1752 case DT_SYMTAB
: name
= "SYMTAB"; break;
1753 case DT_RELA
: name
= "RELA"; break;
1754 case DT_RELASZ
: name
= "RELASZ"; break;
1755 case DT_RELAENT
: name
= "RELAENT"; break;
1756 case DT_STRSZ
: name
= "STRSZ"; break;
1757 case DT_SYMENT
: name
= "SYMENT"; break;
1758 case DT_INIT
: name
= "INIT"; break;
1759 case DT_FINI
: name
= "FINI"; break;
1760 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1761 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1762 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1763 case DT_REL
: name
= "REL"; break;
1764 case DT_RELSZ
: name
= "RELSZ"; break;
1765 case DT_RELENT
: name
= "RELENT"; break;
1766 case DT_PLTREL
: name
= "PLTREL"; break;
1767 case DT_DEBUG
: name
= "DEBUG"; break;
1768 case DT_TEXTREL
: name
= "TEXTREL"; break;
1769 case DT_JMPREL
: name
= "JMPREL"; break;
1770 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1771 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1772 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1773 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1774 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1775 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1776 case DT_FLAGS
: name
= "FLAGS"; break;
1777 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1778 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1779 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1780 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1781 case DT_MOVEENT
: name
= "MOVEENT"; break;
1782 case DT_MOVESZ
: name
= "MOVESZ"; break;
1783 case DT_FEATURE
: name
= "FEATURE"; break;
1784 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1785 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1786 case DT_SYMINENT
: name
= "SYMINENT"; break;
1787 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1788 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1789 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1790 case DT_PLTPAD
: name
= "PLTPAD"; break;
1791 case DT_MOVETAB
: name
= "MOVETAB"; break;
1792 case DT_SYMINFO
: name
= "SYMINFO"; break;
1793 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1794 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1795 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1796 case DT_VERSYM
: name
= "VERSYM"; break;
1797 case DT_VERDEF
: name
= "VERDEF"; break;
1798 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1799 case DT_VERNEED
: name
= "VERNEED"; break;
1800 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1801 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1802 case DT_USED
: name
= "USED"; break;
1803 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1804 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1807 fprintf (f
, " %-20s ", name
);
1811 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1816 unsigned int tagv
= dyn
.d_un
.d_val
;
1818 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1821 fprintf (f
, "%s", string
);
1830 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1831 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1833 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1837 if (elf_dynverdef (abfd
) != 0)
1839 Elf_Internal_Verdef
*t
;
1841 fprintf (f
, _("\nVersion definitions:\n"));
1842 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1844 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1845 t
->vd_flags
, t
->vd_hash
,
1846 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1847 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1849 Elf_Internal_Verdaux
*a
;
1852 for (a
= t
->vd_auxptr
->vda_nextptr
;
1856 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1862 if (elf_dynverref (abfd
) != 0)
1864 Elf_Internal_Verneed
*t
;
1866 fprintf (f
, _("\nVersion References:\n"));
1867 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1869 Elf_Internal_Vernaux
*a
;
1871 fprintf (f
, _(" required from %s:\n"),
1872 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1873 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1874 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1875 a
->vna_flags
, a
->vna_other
,
1876 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1888 /* Get version string. */
1891 _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";
1910 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1912 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1915 Elf_Internal_Verneed
*t
;
1917 version_string
= _("<corrupt>");
1918 for (t
= elf_tdata (abfd
)->verref
;
1922 Elf_Internal_Vernaux
*a
;
1924 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1926 if (a
->vna_other
== vernum
)
1928 version_string
= a
->vna_nodename
;
1935 return version_string
;
1938 /* Display ELF-specific fields of a symbol. */
1941 bfd_elf_print_symbol (bfd
*abfd
,
1944 bfd_print_symbol_type how
)
1946 FILE *file
= (FILE *) filep
;
1949 case bfd_print_symbol_name
:
1950 fprintf (file
, "%s", symbol
->name
);
1952 case bfd_print_symbol_more
:
1953 fprintf (file
, "elf ");
1954 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1955 fprintf (file
, " %x", symbol
->flags
);
1957 case bfd_print_symbol_all
:
1959 const char *section_name
;
1960 const char *name
= NULL
;
1961 const struct elf_backend_data
*bed
;
1962 unsigned char st_other
;
1964 const char *version_string
;
1967 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1969 bed
= get_elf_backend_data (abfd
);
1970 if (bed
->elf_backend_print_symbol_all
)
1971 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1975 name
= symbol
->name
;
1976 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1979 fprintf (file
, " %s\t", section_name
);
1980 /* Print the "other" value for a symbol. For common symbols,
1981 we've already printed the size; now print the alignment.
1982 For other symbols, we have no specified alignment, and
1983 we've printed the address; now print the size. */
1984 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1985 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1987 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1988 bfd_fprintf_vma (abfd
, file
, val
);
1990 /* If we have version information, print it. */
1991 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1997 fprintf (file
, " %-11s", version_string
);
2002 fprintf (file
, " (%s)", version_string
);
2003 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2008 /* If the st_other field is not zero, print it. */
2009 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2014 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2015 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2016 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2018 /* Some other non-defined flags are also present, so print
2020 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2023 fprintf (file
, " %s", name
);
2029 /* ELF .o/exec file reading */
2031 /* Create a new bfd section from an ELF section header. */
2034 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2036 Elf_Internal_Shdr
*hdr
;
2037 Elf_Internal_Ehdr
*ehdr
;
2038 const struct elf_backend_data
*bed
;
2040 bfd_boolean ret
= TRUE
;
2041 static bfd_boolean
* sections_being_created
= NULL
;
2042 static bfd
* sections_being_created_abfd
= NULL
;
2043 static unsigned int nesting
= 0;
2045 if (shindex
>= elf_numsections (abfd
))
2050 /* PR17512: A corrupt ELF binary might contain a recursive group of
2051 sections, with each the string indices pointing to the next in the
2052 loop. Detect this here, by refusing to load a section that we are
2053 already in the process of loading. We only trigger this test if
2054 we have nested at least three sections deep as normal ELF binaries
2055 can expect to recurse at least once.
2057 FIXME: It would be better if this array was attached to the bfd,
2058 rather than being held in a static pointer. */
2060 if (sections_being_created_abfd
!= abfd
)
2061 sections_being_created
= NULL
;
2062 if (sections_being_created
== NULL
)
2064 size_t amt
= elf_numsections (abfd
) * sizeof (bfd_boolean
);
2065 sections_being_created
= (bfd_boolean
*) bfd_zalloc (abfd
, amt
);
2066 if (sections_being_created
== NULL
)
2068 sections_being_created_abfd
= abfd
;
2070 if (sections_being_created
[shindex
])
2073 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2076 sections_being_created
[shindex
] = TRUE
;
2079 hdr
= elf_elfsections (abfd
)[shindex
];
2080 ehdr
= elf_elfheader (abfd
);
2081 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2086 bed
= get_elf_backend_data (abfd
);
2087 switch (hdr
->sh_type
)
2090 /* Inactive section. Throw it away. */
2093 case SHT_PROGBITS
: /* Normal section with contents. */
2094 case SHT_NOBITS
: /* .bss section. */
2095 case SHT_HASH
: /* .hash section. */
2096 case SHT_NOTE
: /* .note section. */
2097 case SHT_INIT_ARRAY
: /* .init_array section. */
2098 case SHT_FINI_ARRAY
: /* .fini_array section. */
2099 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2100 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2101 case SHT_GNU_HASH
: /* .gnu.hash section. */
2102 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2105 case SHT_DYNAMIC
: /* Dynamic linking information. */
2106 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2109 if (hdr
->sh_link
> elf_numsections (abfd
))
2111 /* PR 10478: Accept Solaris binaries with a sh_link
2112 field set to SHN_BEFORE or SHN_AFTER. */
2113 switch (bfd_get_arch (abfd
))
2116 case bfd_arch_sparc
:
2117 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2118 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2120 /* Otherwise fall through. */
2125 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2127 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2129 Elf_Internal_Shdr
*dynsymhdr
;
2131 /* The shared libraries distributed with hpux11 have a bogus
2132 sh_link field for the ".dynamic" section. Find the
2133 string table for the ".dynsym" section instead. */
2134 if (elf_dynsymtab (abfd
) != 0)
2136 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2137 hdr
->sh_link
= dynsymhdr
->sh_link
;
2141 unsigned int i
, num_sec
;
2143 num_sec
= elf_numsections (abfd
);
2144 for (i
= 1; i
< num_sec
; i
++)
2146 dynsymhdr
= elf_elfsections (abfd
)[i
];
2147 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2149 hdr
->sh_link
= dynsymhdr
->sh_link
;
2157 case SHT_SYMTAB
: /* A symbol table. */
2158 if (elf_onesymtab (abfd
) == shindex
)
2161 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2164 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2166 if (hdr
->sh_size
!= 0)
2168 /* Some assemblers erroneously set sh_info to one with a
2169 zero sh_size. ld sees this as a global symbol count
2170 of (unsigned) -1. Fix it here. */
2175 /* PR 18854: A binary might contain more than one symbol table.
2176 Unusual, but possible. Warn, but continue. */
2177 if (elf_onesymtab (abfd
) != 0)
2180 /* xgettext:c-format */
2181 (_("%pB: warning: multiple symbol tables detected"
2182 " - ignoring the table in section %u"),
2186 elf_onesymtab (abfd
) = shindex
;
2187 elf_symtab_hdr (abfd
) = *hdr
;
2188 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2189 abfd
->flags
|= HAS_SYMS
;
2191 /* Sometimes a shared object will map in the symbol table. If
2192 SHF_ALLOC is set, and this is a shared object, then we also
2193 treat this section as a BFD section. We can not base the
2194 decision purely on SHF_ALLOC, because that flag is sometimes
2195 set in a relocatable object file, which would confuse the
2197 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2198 && (abfd
->flags
& DYNAMIC
) != 0
2199 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2203 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2204 can't read symbols without that section loaded as well. It
2205 is most likely specified by the next section header. */
2207 elf_section_list
* entry
;
2208 unsigned int i
, num_sec
;
2210 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2211 if (entry
->hdr
.sh_link
== shindex
)
2214 num_sec
= elf_numsections (abfd
);
2215 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2217 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2219 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2220 && hdr2
->sh_link
== shindex
)
2225 for (i
= 1; i
< shindex
; i
++)
2227 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2229 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2230 && hdr2
->sh_link
== shindex
)
2235 ret
= bfd_section_from_shdr (abfd
, i
);
2236 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2240 case SHT_DYNSYM
: /* A dynamic symbol table. */
2241 if (elf_dynsymtab (abfd
) == shindex
)
2244 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2247 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2249 if (hdr
->sh_size
!= 0)
2252 /* Some linkers erroneously set sh_info to one with a
2253 zero sh_size. ld sees this as a global symbol count
2254 of (unsigned) -1. Fix it here. */
2259 /* PR 18854: A binary might contain more than one dynamic symbol table.
2260 Unusual, but possible. Warn, but continue. */
2261 if (elf_dynsymtab (abfd
) != 0)
2264 /* xgettext:c-format */
2265 (_("%pB: warning: multiple dynamic symbol tables detected"
2266 " - ignoring the table in section %u"),
2270 elf_dynsymtab (abfd
) = shindex
;
2271 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2272 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2273 abfd
->flags
|= HAS_SYMS
;
2275 /* Besides being a symbol table, we also treat this as a regular
2276 section, so that objcopy can handle it. */
2277 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2280 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2282 elf_section_list
* entry
;
2284 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2285 if (entry
->ndx
== shindex
)
2288 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2291 entry
->ndx
= shindex
;
2293 entry
->next
= elf_symtab_shndx_list (abfd
);
2294 elf_symtab_shndx_list (abfd
) = entry
;
2295 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2299 case SHT_STRTAB
: /* A string table. */
2300 if (hdr
->bfd_section
!= NULL
)
2303 if (ehdr
->e_shstrndx
== shindex
)
2305 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2306 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2310 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2313 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2314 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2318 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2321 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2322 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2323 elf_elfsections (abfd
)[shindex
] = hdr
;
2324 /* We also treat this as a regular section, so that objcopy
2326 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2331 /* If the string table isn't one of the above, then treat it as a
2332 regular section. We need to scan all the headers to be sure,
2333 just in case this strtab section appeared before the above. */
2334 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2336 unsigned int i
, num_sec
;
2338 num_sec
= elf_numsections (abfd
);
2339 for (i
= 1; i
< num_sec
; i
++)
2341 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2342 if (hdr2
->sh_link
== shindex
)
2344 /* Prevent endless recursion on broken objects. */
2347 if (! bfd_section_from_shdr (abfd
, i
))
2349 if (elf_onesymtab (abfd
) == i
)
2351 if (elf_dynsymtab (abfd
) == i
)
2352 goto dynsymtab_strtab
;
2356 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2361 /* *These* do a lot of work -- but build no sections! */
2363 asection
*target_sect
;
2364 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2365 unsigned int num_sec
= elf_numsections (abfd
);
2366 struct bfd_elf_section_data
*esdt
;
2369 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2370 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2373 /* Check for a bogus link to avoid crashing. */
2374 if (hdr
->sh_link
>= num_sec
)
2377 /* xgettext:c-format */
2378 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2379 abfd
, hdr
->sh_link
, name
, shindex
);
2380 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2385 /* For some incomprehensible reason Oracle distributes
2386 libraries for Solaris in which some of the objects have
2387 bogus sh_link fields. It would be nice if we could just
2388 reject them, but, unfortunately, some people need to use
2389 them. We scan through the section headers; if we find only
2390 one suitable symbol table, we clobber the sh_link to point
2391 to it. I hope this doesn't break anything.
2393 Don't do it on executable nor shared library. */
2394 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2395 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2396 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2402 for (scan
= 1; scan
< num_sec
; scan
++)
2404 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2405 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2416 hdr
->sh_link
= found
;
2419 /* Get the symbol table. */
2420 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2421 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2422 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2425 /* If this is an alloc section in an executable or shared
2426 library, or the reloc section does not use the main symbol
2427 table we don't treat it as a reloc section. BFD can't
2428 adequately represent such a section, so at least for now,
2429 we don't try. We just present it as a normal section. We
2430 also can't use it as a reloc section if it points to the
2431 null section, an invalid section, another reloc section, or
2432 its sh_link points to the null section. */
2433 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2434 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2435 || hdr
->sh_link
== SHN_UNDEF
2436 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2437 || hdr
->sh_info
== SHN_UNDEF
2438 || hdr
->sh_info
>= num_sec
2439 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2440 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2442 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2447 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2450 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2451 if (target_sect
== NULL
)
2454 esdt
= elf_section_data (target_sect
);
2455 if (hdr
->sh_type
== SHT_RELA
)
2456 p_hdr
= &esdt
->rela
.hdr
;
2458 p_hdr
= &esdt
->rel
.hdr
;
2460 /* PR 17512: file: 0b4f81b7.
2461 Also see PR 24456, for a file which deliberately has two reloc
2465 if (bed
->init_secondary_reloc_section
== NULL
2466 || ! bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2469 /* xgettext:c-format */
2470 (_("%pB: warning: secondary relocation section '%s' for section %pA found - ignoring"),
2471 abfd
, name
, target_sect
);
2476 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2481 elf_elfsections (abfd
)[shindex
] = hdr2
;
2482 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2483 * bed
->s
->int_rels_per_ext_rel
);
2484 target_sect
->flags
|= SEC_RELOC
;
2485 target_sect
->relocation
= NULL
;
2486 target_sect
->rel_filepos
= hdr
->sh_offset
;
2487 /* In the section to which the relocations apply, mark whether
2488 its relocations are of the REL or RELA variety. */
2489 if (hdr
->sh_size
!= 0)
2491 if (hdr
->sh_type
== SHT_RELA
)
2492 target_sect
->use_rela_p
= 1;
2494 abfd
->flags
|= HAS_RELOC
;
2498 case SHT_GNU_verdef
:
2499 elf_dynverdef (abfd
) = shindex
;
2500 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2501 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2504 case SHT_GNU_versym
:
2505 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2508 elf_dynversym (abfd
) = shindex
;
2509 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2510 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2513 case SHT_GNU_verneed
:
2514 elf_dynverref (abfd
) = shindex
;
2515 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2516 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2523 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2526 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2532 /* Possibly an attributes section. */
2533 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2534 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2536 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2538 _bfd_elf_parse_attributes (abfd
, hdr
);
2542 /* Check for any processor-specific section types. */
2543 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2546 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2548 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2549 /* FIXME: How to properly handle allocated section reserved
2550 for applications? */
2552 /* xgettext:c-format */
2553 (_("%pB: unknown type [%#x] section `%s'"),
2554 abfd
, hdr
->sh_type
, name
);
2557 /* Allow sections reserved for applications. */
2558 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2563 else if (hdr
->sh_type
>= SHT_LOPROC
2564 && hdr
->sh_type
<= SHT_HIPROC
)
2565 /* FIXME: We should handle this section. */
2567 /* xgettext:c-format */
2568 (_("%pB: unknown type [%#x] section `%s'"),
2569 abfd
, hdr
->sh_type
, name
);
2570 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2572 /* Unrecognised OS-specific sections. */
2573 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2574 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2575 required to correctly process the section and the file should
2576 be rejected with an error message. */
2578 /* xgettext:c-format */
2579 (_("%pB: unknown type [%#x] section `%s'"),
2580 abfd
, hdr
->sh_type
, name
);
2583 /* Otherwise it should be processed. */
2584 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2589 /* FIXME: We should handle this section. */
2591 /* xgettext:c-format */
2592 (_("%pB: unknown type [%#x] section `%s'"),
2593 abfd
, hdr
->sh_type
, name
);
2601 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2602 sections_being_created
[shindex
] = FALSE
;
2603 if (-- nesting
== 0)
2605 sections_being_created
= NULL
;
2606 sections_being_created_abfd
= abfd
;
2611 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2614 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2616 unsigned long r_symndx
)
2618 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2620 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2622 Elf_Internal_Shdr
*symtab_hdr
;
2623 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2624 Elf_External_Sym_Shndx eshndx
;
2626 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2627 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2628 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2631 if (cache
->abfd
!= abfd
)
2633 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2636 cache
->indx
[ent
] = r_symndx
;
2639 return &cache
->sym
[ent
];
2642 /* Given an ELF section number, retrieve the corresponding BFD
2646 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2648 if (sec_index
>= elf_numsections (abfd
))
2650 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2653 static const struct bfd_elf_special_section special_sections_b
[] =
2655 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2656 { NULL
, 0, 0, 0, 0 }
2659 static const struct bfd_elf_special_section special_sections_c
[] =
2661 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2662 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2663 { NULL
, 0, 0, 0, 0 }
2666 static const struct bfd_elf_special_section special_sections_d
[] =
2668 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2669 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2670 /* There are more DWARF sections than these, but they needn't be added here
2671 unless you have to cope with broken compilers that don't emit section
2672 attributes or you want to help the user writing assembler. */
2673 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2674 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2675 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2676 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2677 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2678 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2679 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2680 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2681 { NULL
, 0, 0, 0, 0 }
2684 static const struct bfd_elf_special_section special_sections_f
[] =
2686 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2687 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2688 { NULL
, 0 , 0, 0, 0 }
2691 static const struct bfd_elf_special_section special_sections_g
[] =
2693 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2694 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2695 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2696 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2697 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2698 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2699 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2700 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2701 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2702 { NULL
, 0, 0, 0, 0 }
2705 static const struct bfd_elf_special_section special_sections_h
[] =
2707 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2708 { NULL
, 0, 0, 0, 0 }
2711 static const struct bfd_elf_special_section special_sections_i
[] =
2713 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2714 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2715 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2716 { NULL
, 0, 0, 0, 0 }
2719 static const struct bfd_elf_special_section special_sections_l
[] =
2721 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2722 { NULL
, 0, 0, 0, 0 }
2725 static const struct bfd_elf_special_section special_sections_n
[] =
2727 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2728 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2729 { NULL
, 0, 0, 0, 0 }
2732 static const struct bfd_elf_special_section special_sections_p
[] =
2734 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2735 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2736 { NULL
, 0, 0, 0, 0 }
2739 static const struct bfd_elf_special_section special_sections_r
[] =
2741 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2742 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2743 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2744 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2745 { NULL
, 0, 0, 0, 0 }
2748 static const struct bfd_elf_special_section special_sections_s
[] =
2750 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2751 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2752 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2753 /* See struct bfd_elf_special_section declaration for the semantics of
2754 this special case where .prefix_length != strlen (.prefix). */
2755 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2756 { NULL
, 0, 0, 0, 0 }
2759 static const struct bfd_elf_special_section special_sections_t
[] =
2761 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2762 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2763 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2764 { NULL
, 0, 0, 0, 0 }
2767 static const struct bfd_elf_special_section special_sections_z
[] =
2769 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2770 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2771 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2772 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2773 { NULL
, 0, 0, 0, 0 }
2776 static const struct bfd_elf_special_section
* const special_sections
[] =
2778 special_sections_b
, /* 'b' */
2779 special_sections_c
, /* 'c' */
2780 special_sections_d
, /* 'd' */
2782 special_sections_f
, /* 'f' */
2783 special_sections_g
, /* 'g' */
2784 special_sections_h
, /* 'h' */
2785 special_sections_i
, /* 'i' */
2788 special_sections_l
, /* 'l' */
2790 special_sections_n
, /* 'n' */
2792 special_sections_p
, /* 'p' */
2794 special_sections_r
, /* 'r' */
2795 special_sections_s
, /* 's' */
2796 special_sections_t
, /* 't' */
2802 special_sections_z
/* 'z' */
2805 const struct bfd_elf_special_section
*
2806 _bfd_elf_get_special_section (const char *name
,
2807 const struct bfd_elf_special_section
*spec
,
2813 len
= strlen (name
);
2815 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2818 int prefix_len
= spec
[i
].prefix_length
;
2820 if (len
< prefix_len
)
2822 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2825 suffix_len
= spec
[i
].suffix_length
;
2826 if (suffix_len
<= 0)
2828 if (name
[prefix_len
] != 0)
2830 if (suffix_len
== 0)
2832 if (name
[prefix_len
] != '.'
2833 && (suffix_len
== -2
2834 || (rela
&& spec
[i
].type
== SHT_REL
)))
2840 if (len
< prefix_len
+ suffix_len
)
2842 if (memcmp (name
+ len
- suffix_len
,
2843 spec
[i
].prefix
+ prefix_len
,
2853 const struct bfd_elf_special_section
*
2854 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2857 const struct bfd_elf_special_section
*spec
;
2858 const struct elf_backend_data
*bed
;
2860 /* See if this is one of the special sections. */
2861 if (sec
->name
== NULL
)
2864 bed
= get_elf_backend_data (abfd
);
2865 spec
= bed
->special_sections
;
2868 spec
= _bfd_elf_get_special_section (sec
->name
,
2869 bed
->special_sections
,
2875 if (sec
->name
[0] != '.')
2878 i
= sec
->name
[1] - 'b';
2879 if (i
< 0 || i
> 'z' - 'b')
2882 spec
= special_sections
[i
];
2887 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2891 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2893 struct bfd_elf_section_data
*sdata
;
2894 const struct elf_backend_data
*bed
;
2895 const struct bfd_elf_special_section
*ssect
;
2897 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2900 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2904 sec
->used_by_bfd
= sdata
;
2907 /* Indicate whether or not this section should use RELA relocations. */
2908 bed
= get_elf_backend_data (abfd
);
2909 sec
->use_rela_p
= bed
->default_use_rela_p
;
2911 /* Set up ELF section type and flags for newly created sections, if
2912 there is an ABI mandated section. */
2913 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2916 elf_section_type (sec
) = ssect
->type
;
2917 elf_section_flags (sec
) = ssect
->attr
;
2920 return _bfd_generic_new_section_hook (abfd
, sec
);
2923 /* Create a new bfd section from an ELF program header.
2925 Since program segments have no names, we generate a synthetic name
2926 of the form segment<NUM>, where NUM is generally the index in the
2927 program header table. For segments that are split (see below) we
2928 generate the names segment<NUM>a and segment<NUM>b.
2930 Note that some program segments may have a file size that is different than
2931 (less than) the memory size. All this means is that at execution the
2932 system must allocate the amount of memory specified by the memory size,
2933 but only initialize it with the first "file size" bytes read from the
2934 file. This would occur for example, with program segments consisting
2935 of combined data+bss.
2937 To handle the above situation, this routine generates TWO bfd sections
2938 for the single program segment. The first has the length specified by
2939 the file size of the segment, and the second has the length specified
2940 by the difference between the two sizes. In effect, the segment is split
2941 into its initialized and uninitialized parts.
2946 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2947 Elf_Internal_Phdr
*hdr
,
2949 const char *type_name
)
2956 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
2958 split
= ((hdr
->p_memsz
> 0)
2959 && (hdr
->p_filesz
> 0)
2960 && (hdr
->p_memsz
> hdr
->p_filesz
));
2962 if (hdr
->p_filesz
> 0)
2964 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2965 len
= strlen (namebuf
) + 1;
2966 name
= (char *) bfd_alloc (abfd
, len
);
2969 memcpy (name
, namebuf
, len
);
2970 newsect
= bfd_make_section (abfd
, name
);
2971 if (newsect
== NULL
)
2973 newsect
->vma
= hdr
->p_vaddr
/ opb
;
2974 newsect
->lma
= hdr
->p_paddr
/ opb
;
2975 newsect
->size
= hdr
->p_filesz
;
2976 newsect
->filepos
= hdr
->p_offset
;
2977 newsect
->flags
|= SEC_HAS_CONTENTS
;
2978 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2979 if (hdr
->p_type
== PT_LOAD
)
2981 newsect
->flags
|= SEC_ALLOC
;
2982 newsect
->flags
|= SEC_LOAD
;
2983 if (hdr
->p_flags
& PF_X
)
2985 /* FIXME: all we known is that it has execute PERMISSION,
2987 newsect
->flags
|= SEC_CODE
;
2990 if (!(hdr
->p_flags
& PF_W
))
2992 newsect
->flags
|= SEC_READONLY
;
2996 if (hdr
->p_memsz
> hdr
->p_filesz
)
3000 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3001 len
= strlen (namebuf
) + 1;
3002 name
= (char *) bfd_alloc (abfd
, len
);
3005 memcpy (name
, namebuf
, len
);
3006 newsect
= bfd_make_section (abfd
, name
);
3007 if (newsect
== NULL
)
3009 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3010 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3011 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3012 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3013 align
= newsect
->vma
& -newsect
->vma
;
3014 if (align
== 0 || align
> hdr
->p_align
)
3015 align
= hdr
->p_align
;
3016 newsect
->alignment_power
= bfd_log2 (align
);
3017 if (hdr
->p_type
== PT_LOAD
)
3019 /* Hack for gdb. Segments that have not been modified do
3020 not have their contents written to a core file, on the
3021 assumption that a debugger can find the contents in the
3022 executable. We flag this case by setting the fake
3023 section size to zero. Note that "real" bss sections will
3024 always have their contents dumped to the core file. */
3025 if (bfd_get_format (abfd
) == bfd_core
)
3027 newsect
->flags
|= SEC_ALLOC
;
3028 if (hdr
->p_flags
& PF_X
)
3029 newsect
->flags
|= SEC_CODE
;
3031 if (!(hdr
->p_flags
& PF_W
))
3032 newsect
->flags
|= SEC_READONLY
;
3039 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3041 /* The return value is ignored. Build-ids are considered optional. */
3042 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3043 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3049 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3051 const struct elf_backend_data
*bed
;
3053 switch (hdr
->p_type
)
3056 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3059 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3061 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3062 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3066 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3069 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3072 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3074 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3080 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3083 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3085 case PT_GNU_EH_FRAME
:
3086 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3090 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3093 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3096 /* Check for any processor-specific program segment types. */
3097 bed
= get_elf_backend_data (abfd
);
3098 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3102 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3106 _bfd_elf_single_rel_hdr (asection
*sec
)
3108 if (elf_section_data (sec
)->rel
.hdr
)
3110 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3111 return elf_section_data (sec
)->rel
.hdr
;
3114 return elf_section_data (sec
)->rela
.hdr
;
3118 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3119 Elf_Internal_Shdr
*rel_hdr
,
3120 const char *sec_name
,
3121 bfd_boolean use_rela_p
)
3123 char *name
= (char *) bfd_alloc (abfd
,
3124 sizeof ".rela" + strlen (sec_name
));
3128 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3130 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3132 if (rel_hdr
->sh_name
== (unsigned int) -1)
3138 /* Allocate and initialize a section-header for a new reloc section,
3139 containing relocations against ASECT. It is stored in RELDATA. If
3140 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3144 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3145 struct bfd_elf_section_reloc_data
*reldata
,
3146 const char *sec_name
,
3147 bfd_boolean use_rela_p
,
3148 bfd_boolean delay_st_name_p
)
3150 Elf_Internal_Shdr
*rel_hdr
;
3151 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3153 BFD_ASSERT (reldata
->hdr
== NULL
);
3154 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3155 reldata
->hdr
= rel_hdr
;
3157 if (delay_st_name_p
)
3158 rel_hdr
->sh_name
= (unsigned int) -1;
3159 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3162 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3163 rel_hdr
->sh_entsize
= (use_rela_p
3164 ? bed
->s
->sizeof_rela
3165 : bed
->s
->sizeof_rel
);
3166 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3167 rel_hdr
->sh_flags
= 0;
3168 rel_hdr
->sh_addr
= 0;
3169 rel_hdr
->sh_size
= 0;
3170 rel_hdr
->sh_offset
= 0;
3175 /* Return the default section type based on the passed in section flags. */
3178 bfd_elf_get_default_section_type (flagword flags
)
3180 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3181 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3183 return SHT_PROGBITS
;
3186 struct fake_section_arg
3188 struct bfd_link_info
*link_info
;
3192 /* Set up an ELF internal section header for a section. */
3195 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3197 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3198 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3199 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3200 Elf_Internal_Shdr
*this_hdr
;
3201 unsigned int sh_type
;
3202 const char *name
= asect
->name
;
3203 bfd_boolean delay_st_name_p
= FALSE
;
3208 /* We already failed; just get out of the bfd_map_over_sections
3213 this_hdr
= &esd
->this_hdr
;
3217 /* ld: compress DWARF debug sections with names: .debug_*. */
3218 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3219 && (asect
->flags
& SEC_DEBUGGING
)
3223 /* Set SEC_ELF_COMPRESS to indicate this section should be
3225 asect
->flags
|= SEC_ELF_COMPRESS
;
3226 /* If this section will be compressed, delay adding section
3227 name to section name section after it is compressed in
3228 _bfd_elf_assign_file_positions_for_non_load. */
3229 delay_st_name_p
= TRUE
;
3232 else if ((asect
->flags
& SEC_ELF_RENAME
))
3234 /* objcopy: rename output DWARF debug section. */
3235 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3237 /* When we decompress or compress with SHF_COMPRESSED,
3238 convert section name from .zdebug_* to .debug_* if
3242 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3243 if (new_name
== NULL
)
3251 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3253 /* PR binutils/18087: Compression does not always make a
3254 section smaller. So only rename the section when
3255 compression has actually taken place. If input section
3256 name is .zdebug_*, we should never compress it again. */
3257 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3258 if (new_name
== NULL
)
3263 BFD_ASSERT (name
[1] != 'z');
3268 if (delay_st_name_p
)
3269 this_hdr
->sh_name
= (unsigned int) -1;
3273 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3275 if (this_hdr
->sh_name
== (unsigned int) -1)
3282 /* Don't clear sh_flags. Assembler may set additional bits. */
3284 if ((asect
->flags
& SEC_ALLOC
) != 0
3285 || asect
->user_set_vma
)
3286 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3288 this_hdr
->sh_addr
= 0;
3290 this_hdr
->sh_offset
= 0;
3291 this_hdr
->sh_size
= asect
->size
;
3292 this_hdr
->sh_link
= 0;
3293 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3294 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3297 /* xgettext:c-format */
3298 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3299 abfd
, asect
->alignment_power
, asect
);
3303 /* Set sh_addralign to the highest power of two given by alignment
3304 consistent with the section VMA. Linker scripts can force VMA. */
3305 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3306 this_hdr
->sh_addralign
= mask
& -mask
;
3307 /* The sh_entsize and sh_info fields may have been set already by
3308 copy_private_section_data. */
3310 this_hdr
->bfd_section
= asect
;
3311 this_hdr
->contents
= NULL
;
3313 /* If the section type is unspecified, we set it based on
3315 if ((asect
->flags
& SEC_GROUP
) != 0)
3316 sh_type
= SHT_GROUP
;
3318 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3320 if (this_hdr
->sh_type
== SHT_NULL
)
3321 this_hdr
->sh_type
= sh_type
;
3322 else if (this_hdr
->sh_type
== SHT_NOBITS
3323 && sh_type
== SHT_PROGBITS
3324 && (asect
->flags
& SEC_ALLOC
) != 0)
3326 /* Warn if we are changing a NOBITS section to PROGBITS, but
3327 allow the link to proceed. This can happen when users link
3328 non-bss input sections to bss output sections, or emit data
3329 to a bss output section via a linker script. */
3331 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3332 this_hdr
->sh_type
= sh_type
;
3335 switch (this_hdr
->sh_type
)
3346 case SHT_INIT_ARRAY
:
3347 case SHT_FINI_ARRAY
:
3348 case SHT_PREINIT_ARRAY
:
3349 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3353 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3357 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3361 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3365 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3366 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3370 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3371 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3374 case SHT_GNU_versym
:
3375 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3378 case SHT_GNU_verdef
:
3379 this_hdr
->sh_entsize
= 0;
3380 /* objcopy or strip will copy over sh_info, but may not set
3381 cverdefs. The linker will set cverdefs, but sh_info will be
3383 if (this_hdr
->sh_info
== 0)
3384 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3386 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3387 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3390 case SHT_GNU_verneed
:
3391 this_hdr
->sh_entsize
= 0;
3392 /* objcopy or strip will copy over sh_info, but may not set
3393 cverrefs. The linker will set cverrefs, but sh_info will be
3395 if (this_hdr
->sh_info
== 0)
3396 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3398 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3399 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3403 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3407 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3411 if ((asect
->flags
& SEC_ALLOC
) != 0)
3412 this_hdr
->sh_flags
|= SHF_ALLOC
;
3413 if ((asect
->flags
& SEC_READONLY
) == 0)
3414 this_hdr
->sh_flags
|= SHF_WRITE
;
3415 if ((asect
->flags
& SEC_CODE
) != 0)
3416 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3417 if ((asect
->flags
& SEC_MERGE
) != 0)
3419 this_hdr
->sh_flags
|= SHF_MERGE
;
3420 this_hdr
->sh_entsize
= asect
->entsize
;
3422 if ((asect
->flags
& SEC_STRINGS
) != 0)
3423 this_hdr
->sh_flags
|= SHF_STRINGS
;
3424 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3425 this_hdr
->sh_flags
|= SHF_GROUP
;
3426 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3428 this_hdr
->sh_flags
|= SHF_TLS
;
3429 if (asect
->size
== 0
3430 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3432 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3434 this_hdr
->sh_size
= 0;
3437 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3438 if (this_hdr
->sh_size
!= 0)
3439 this_hdr
->sh_type
= SHT_NOBITS
;
3443 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3444 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3446 /* If the section has relocs, set up a section header for the
3447 SHT_REL[A] section. If two relocation sections are required for
3448 this section, it is up to the processor-specific back-end to
3449 create the other. */
3450 if ((asect
->flags
& SEC_RELOC
) != 0)
3452 /* When doing a relocatable link, create both REL and RELA sections if
3455 /* Do the normal setup if we wouldn't create any sections here. */
3456 && esd
->rel
.count
+ esd
->rela
.count
> 0
3457 && (bfd_link_relocatable (arg
->link_info
)
3458 || arg
->link_info
->emitrelocations
))
3460 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3461 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3462 FALSE
, delay_st_name_p
))
3467 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3468 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3469 TRUE
, delay_st_name_p
))
3475 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3477 ? &esd
->rela
: &esd
->rel
),
3487 /* Check for processor-specific section types. */
3488 sh_type
= this_hdr
->sh_type
;
3489 if (bed
->elf_backend_fake_sections
3490 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3496 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3498 /* Don't change the header type from NOBITS if we are being
3499 called for objcopy --only-keep-debug. */
3500 this_hdr
->sh_type
= sh_type
;
3504 /* Fill in the contents of a SHT_GROUP section. Called from
3505 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3506 when ELF targets use the generic linker, ld. Called for ld -r
3507 from bfd_elf_final_link. */
3510 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3512 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3513 asection
*elt
, *first
;
3517 /* Ignore linker created group section. See elfNN_ia64_object_p in
3519 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3524 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3526 unsigned long symindx
= 0;
3528 /* elf_group_id will have been set up by objcopy and the
3530 if (elf_group_id (sec
) != NULL
)
3531 symindx
= elf_group_id (sec
)->udata
.i
;
3535 /* If called from the assembler, swap_out_syms will have set up
3536 elf_section_syms. */
3537 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3538 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3540 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3542 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3544 /* The ELF backend linker sets sh_info to -2 when the group
3545 signature symbol is global, and thus the index can't be
3546 set until all local symbols are output. */
3548 struct bfd_elf_section_data
*sec_data
;
3549 unsigned long symndx
;
3550 unsigned long extsymoff
;
3551 struct elf_link_hash_entry
*h
;
3553 /* The point of this little dance to the first SHF_GROUP section
3554 then back to the SHT_GROUP section is that this gets us to
3555 the SHT_GROUP in the input object. */
3556 igroup
= elf_sec_group (elf_next_in_group (sec
));
3557 sec_data
= elf_section_data (igroup
);
3558 symndx
= sec_data
->this_hdr
.sh_info
;
3560 if (!elf_bad_symtab (igroup
->owner
))
3562 Elf_Internal_Shdr
*symtab_hdr
;
3564 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3565 extsymoff
= symtab_hdr
->sh_info
;
3567 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3568 while (h
->root
.type
== bfd_link_hash_indirect
3569 || h
->root
.type
== bfd_link_hash_warning
)
3570 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3572 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3575 /* The contents won't be allocated for "ld -r" or objcopy. */
3577 if (sec
->contents
== NULL
)
3580 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3582 /* Arrange for the section to be written out. */
3583 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3584 if (sec
->contents
== NULL
)
3591 loc
= sec
->contents
+ sec
->size
;
3593 /* Get the pointer to the first section in the group that gas
3594 squirreled away here. objcopy arranges for this to be set to the
3595 start of the input section group. */
3596 first
= elt
= elf_next_in_group (sec
);
3598 /* First element is a flag word. Rest of section is elf section
3599 indices for all the sections of the group. Write them backwards
3600 just to keep the group in the same order as given in .section
3601 directives, not that it matters. */
3608 s
= s
->output_section
;
3610 && !bfd_is_abs_section (s
))
3612 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3613 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3615 if (elf_sec
->rel
.hdr
!= NULL
3617 || (input_elf_sec
->rel
.hdr
!= NULL
3618 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3620 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3622 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3624 if (elf_sec
->rela
.hdr
!= NULL
3626 || (input_elf_sec
->rela
.hdr
!= NULL
3627 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3629 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3631 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3634 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3636 elt
= elf_next_in_group (elt
);
3642 BFD_ASSERT (loc
== sec
->contents
);
3644 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3647 /* Given NAME, the name of a relocation section stripped of its
3648 .rel/.rela prefix, return the section in ABFD to which the
3649 relocations apply. */
3652 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3654 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3655 section likely apply to .got.plt or .got section. */
3656 if (get_elf_backend_data (abfd
)->want_got_plt
3657 && strcmp (name
, ".plt") == 0)
3662 sec
= bfd_get_section_by_name (abfd
, name
);
3668 return bfd_get_section_by_name (abfd
, name
);
3671 /* Return the section to which RELOC_SEC applies. */
3674 elf_get_reloc_section (asection
*reloc_sec
)
3679 const struct elf_backend_data
*bed
;
3681 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3682 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3685 /* We look up the section the relocs apply to by name. */
3686 name
= reloc_sec
->name
;
3687 if (strncmp (name
, ".rel", 4) != 0)
3690 if (type
== SHT_RELA
&& *name
++ != 'a')
3693 abfd
= reloc_sec
->owner
;
3694 bed
= get_elf_backend_data (abfd
);
3695 return bed
->get_reloc_section (abfd
, name
);
3698 /* Assign all ELF section numbers. The dummy first section is handled here
3699 too. The link/info pointers for the standard section types are filled
3700 in here too, while we're at it. */
3703 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3705 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3707 unsigned int section_number
;
3708 Elf_Internal_Shdr
**i_shdrp
;
3709 struct bfd_elf_section_data
*d
;
3710 bfd_boolean need_symtab
;
3715 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3717 /* SHT_GROUP sections are in relocatable files only. */
3718 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3720 size_t reloc_count
= 0;
3722 /* Put SHT_GROUP sections first. */
3723 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3725 d
= elf_section_data (sec
);
3727 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3729 if (sec
->flags
& SEC_LINKER_CREATED
)
3731 /* Remove the linker created SHT_GROUP sections. */
3732 bfd_section_list_remove (abfd
, sec
);
3733 abfd
->section_count
--;
3736 d
->this_idx
= section_number
++;
3739 /* Count relocations. */
3740 reloc_count
+= sec
->reloc_count
;
3743 /* Clear HAS_RELOC if there are no relocations. */
3744 if (reloc_count
== 0)
3745 abfd
->flags
&= ~HAS_RELOC
;
3748 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3750 d
= elf_section_data (sec
);
3752 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3753 d
->this_idx
= section_number
++;
3754 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3755 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3758 d
->rel
.idx
= section_number
++;
3759 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3760 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3767 d
->rela
.idx
= section_number
++;
3768 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3769 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3775 need_symtab
= (bfd_get_symcount (abfd
) > 0
3776 || (link_info
== NULL
3777 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3781 elf_onesymtab (abfd
) = section_number
++;
3782 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3783 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3785 elf_section_list
*entry
;
3787 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3789 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3790 entry
->ndx
= section_number
++;
3791 elf_symtab_shndx_list (abfd
) = entry
;
3793 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3794 ".symtab_shndx", FALSE
);
3795 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3798 elf_strtab_sec (abfd
) = section_number
++;
3799 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3802 elf_shstrtab_sec (abfd
) = section_number
++;
3803 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3804 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3806 if (section_number
>= SHN_LORESERVE
)
3808 /* xgettext:c-format */
3809 _bfd_error_handler (_("%pB: too many sections: %u"),
3810 abfd
, section_number
);
3814 elf_numsections (abfd
) = section_number
;
3815 elf_elfheader (abfd
)->e_shnum
= section_number
;
3817 /* Set up the list of section header pointers, in agreement with the
3819 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3820 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3821 if (i_shdrp
== NULL
)
3824 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3825 sizeof (Elf_Internal_Shdr
));
3826 if (i_shdrp
[0] == NULL
)
3828 bfd_release (abfd
, i_shdrp
);
3832 elf_elfsections (abfd
) = i_shdrp
;
3834 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3837 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3838 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3840 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3841 BFD_ASSERT (entry
!= NULL
);
3842 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3843 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3845 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3846 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3849 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3853 d
= elf_section_data (sec
);
3855 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3856 if (d
->rel
.idx
!= 0)
3857 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3858 if (d
->rela
.idx
!= 0)
3859 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3861 /* Fill in the sh_link and sh_info fields while we're at it. */
3863 /* sh_link of a reloc section is the section index of the symbol
3864 table. sh_info is the section index of the section to which
3865 the relocation entries apply. */
3866 if (d
->rel
.idx
!= 0)
3868 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3869 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3870 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3872 if (d
->rela
.idx
!= 0)
3874 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3875 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3876 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3879 /* We need to set up sh_link for SHF_LINK_ORDER. */
3880 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3882 s
= elf_linked_to_section (sec
);
3885 /* elf_linked_to_section points to the input section. */
3886 if (link_info
!= NULL
)
3888 /* Check discarded linkonce section. */
3889 if (discarded_section (s
))
3893 /* xgettext:c-format */
3894 (_("%pB: sh_link of section `%pA' points to"
3895 " discarded section `%pA' of `%pB'"),
3896 abfd
, d
->this_hdr
.bfd_section
,
3898 /* Point to the kept section if it has the same
3899 size as the discarded one. */
3900 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3903 bfd_set_error (bfd_error_bad_value
);
3909 s
= s
->output_section
;
3910 BFD_ASSERT (s
!= NULL
);
3914 /* Handle objcopy. */
3915 if (s
->output_section
== NULL
)
3918 /* xgettext:c-format */
3919 (_("%pB: sh_link of section `%pA' points to"
3920 " removed section `%pA' of `%pB'"),
3921 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3922 bfd_set_error (bfd_error_bad_value
);
3925 s
= s
->output_section
;
3927 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3932 The Intel C compiler generates SHT_IA_64_UNWIND with
3933 SHF_LINK_ORDER. But it doesn't set the sh_link or
3934 sh_info fields. Hence we could get the situation
3936 const struct elf_backend_data
*bed
3937 = get_elf_backend_data (abfd
);
3938 if (bed
->link_order_error_handler
)
3939 bed
->link_order_error_handler
3940 /* xgettext:c-format */
3941 (_("%pB: warning: sh_link not set for section `%pA'"),
3946 switch (d
->this_hdr
.sh_type
)
3950 /* A reloc section which we are treating as a normal BFD
3951 section. sh_link is the section index of the symbol
3952 table. sh_info is the section index of the section to
3953 which the relocation entries apply. We assume that an
3954 allocated reloc section uses the dynamic symbol table.
3955 FIXME: How can we be sure? */
3956 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3958 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3960 s
= elf_get_reloc_section (sec
);
3963 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3964 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3969 /* We assume that a section named .stab*str is a stabs
3970 string section. We look for a section with the same name
3971 but without the trailing ``str'', and set its sh_link
3972 field to point to this section. */
3973 if (CONST_STRNEQ (sec
->name
, ".stab")
3974 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3979 len
= strlen (sec
->name
);
3980 alc
= (char *) bfd_malloc (len
- 2);
3983 memcpy (alc
, sec
->name
, len
- 3);
3984 alc
[len
- 3] = '\0';
3985 s
= bfd_get_section_by_name (abfd
, alc
);
3989 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3991 /* This is a .stab section. */
3992 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3993 elf_section_data (s
)->this_hdr
.sh_entsize
3994 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
4001 case SHT_GNU_verneed
:
4002 case SHT_GNU_verdef
:
4003 /* sh_link is the section header index of the string table
4004 used for the dynamic entries, or the symbol table, or the
4006 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4008 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4011 case SHT_GNU_LIBLIST
:
4012 /* sh_link is the section header index of the prelink library
4013 list used for the dynamic entries, or the symbol table, or
4014 the version strings. */
4015 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4016 ? ".dynstr" : ".gnu.libstr");
4018 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4023 case SHT_GNU_versym
:
4024 /* sh_link is the section header index of the symbol table
4025 this hash table or version table is for. */
4026 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4028 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4032 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4036 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4037 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4038 debug section name from .debug_* to .zdebug_* if needed. */
4044 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4046 /* If the backend has a special mapping, use it. */
4047 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4048 if (bed
->elf_backend_sym_is_global
)
4049 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4051 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4052 || bfd_is_und_section (bfd_asymbol_section (sym
))
4053 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4056 /* Filter global symbols of ABFD to include in the import library. All
4057 SYMCOUNT symbols of ABFD can be examined from their pointers in
4058 SYMS. Pointers of symbols to keep should be stored contiguously at
4059 the beginning of that array.
4061 Returns the number of symbols to keep. */
4064 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4065 asymbol
**syms
, long symcount
)
4067 long src_count
, dst_count
= 0;
4069 for (src_count
= 0; src_count
< symcount
; src_count
++)
4071 asymbol
*sym
= syms
[src_count
];
4072 char *name
= (char *) bfd_asymbol_name (sym
);
4073 struct bfd_link_hash_entry
*h
;
4075 if (!sym_is_global (abfd
, sym
))
4078 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4081 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4083 if (h
->linker_def
|| h
->ldscript_def
)
4086 syms
[dst_count
++] = sym
;
4089 syms
[dst_count
] = NULL
;
4094 /* Don't output section symbols for sections that are not going to be
4095 output, that are duplicates or there is no BFD section. */
4098 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4100 elf_symbol_type
*type_ptr
;
4105 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4108 if (sym
->section
== NULL
)
4111 type_ptr
= elf_symbol_from (abfd
, sym
);
4112 return ((type_ptr
!= NULL
4113 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4114 && bfd_is_abs_section (sym
->section
))
4115 || !(sym
->section
->owner
== abfd
4116 || (sym
->section
->output_section
!= NULL
4117 && sym
->section
->output_section
->owner
== abfd
4118 && sym
->section
->output_offset
== 0)
4119 || bfd_is_abs_section (sym
->section
)));
4122 /* Map symbol from it's internal number to the external number, moving
4123 all local symbols to be at the head of the list. */
4126 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4128 unsigned int symcount
= bfd_get_symcount (abfd
);
4129 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4130 asymbol
**sect_syms
;
4131 unsigned int num_locals
= 0;
4132 unsigned int num_globals
= 0;
4133 unsigned int num_locals2
= 0;
4134 unsigned int num_globals2
= 0;
4135 unsigned int max_index
= 0;
4142 fprintf (stderr
, "elf_map_symbols\n");
4146 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4148 if (max_index
< asect
->index
)
4149 max_index
= asect
->index
;
4153 amt
= max_index
* sizeof (asymbol
*);
4154 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4155 if (sect_syms
== NULL
)
4157 elf_section_syms (abfd
) = sect_syms
;
4158 elf_num_section_syms (abfd
) = max_index
;
4160 /* Init sect_syms entries for any section symbols we have already
4161 decided to output. */
4162 for (idx
= 0; idx
< symcount
; idx
++)
4164 asymbol
*sym
= syms
[idx
];
4166 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4168 && !ignore_section_sym (abfd
, sym
)
4169 && !bfd_is_abs_section (sym
->section
))
4171 asection
*sec
= sym
->section
;
4173 if (sec
->owner
!= abfd
)
4174 sec
= sec
->output_section
;
4176 sect_syms
[sec
->index
] = syms
[idx
];
4180 /* Classify all of the symbols. */
4181 for (idx
= 0; idx
< symcount
; idx
++)
4183 if (sym_is_global (abfd
, syms
[idx
]))
4185 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4189 /* We will be adding a section symbol for each normal BFD section. Most
4190 sections will already have a section symbol in outsymbols, but
4191 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4192 at least in that case. */
4193 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4195 if (sect_syms
[asect
->index
] == NULL
)
4197 if (!sym_is_global (abfd
, asect
->symbol
))
4204 /* Now sort the symbols so the local symbols are first. */
4205 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4206 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4207 if (new_syms
== NULL
)
4210 for (idx
= 0; idx
< symcount
; idx
++)
4212 asymbol
*sym
= syms
[idx
];
4215 if (sym_is_global (abfd
, sym
))
4216 i
= num_locals
+ num_globals2
++;
4217 else if (!ignore_section_sym (abfd
, sym
))
4222 sym
->udata
.i
= i
+ 1;
4224 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4226 if (sect_syms
[asect
->index
] == NULL
)
4228 asymbol
*sym
= asect
->symbol
;
4231 sect_syms
[asect
->index
] = sym
;
4232 if (!sym_is_global (abfd
, sym
))
4235 i
= num_locals
+ num_globals2
++;
4237 sym
->udata
.i
= i
+ 1;
4241 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4243 *pnum_locals
= num_locals
;
4247 /* Align to the maximum file alignment that could be required for any
4248 ELF data structure. */
4250 static inline file_ptr
4251 align_file_position (file_ptr off
, int align
)
4253 return (off
+ align
- 1) & ~(align
- 1);
4256 /* Assign a file position to a section, optionally aligning to the
4257 required section alignment. */
4260 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4264 if (align
&& i_shdrp
->sh_addralign
> 1)
4265 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4266 i_shdrp
->sh_offset
= offset
;
4267 if (i_shdrp
->bfd_section
!= NULL
)
4268 i_shdrp
->bfd_section
->filepos
= offset
;
4269 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4270 offset
+= i_shdrp
->sh_size
;
4274 /* Compute the file positions we are going to put the sections at, and
4275 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4276 is not NULL, this is being called by the ELF backend linker. */
4279 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4280 struct bfd_link_info
*link_info
)
4282 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4283 struct fake_section_arg fsargs
;
4285 struct elf_strtab_hash
*strtab
= NULL
;
4286 Elf_Internal_Shdr
*shstrtab_hdr
;
4287 bfd_boolean need_symtab
;
4289 if (abfd
->output_has_begun
)
4292 /* Do any elf backend specific processing first. */
4293 if (bed
->elf_backend_begin_write_processing
)
4294 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4296 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4299 fsargs
.failed
= FALSE
;
4300 fsargs
.link_info
= link_info
;
4301 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4305 if (!assign_section_numbers (abfd
, link_info
))
4308 /* The backend linker builds symbol table information itself. */
4309 need_symtab
= (link_info
== NULL
4310 && (bfd_get_symcount (abfd
) > 0
4311 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4315 /* Non-zero if doing a relocatable link. */
4316 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4318 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4323 if (link_info
== NULL
)
4325 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4330 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4331 /* sh_name was set in init_file_header. */
4332 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4333 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4334 shstrtab_hdr
->sh_addr
= 0;
4335 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4336 shstrtab_hdr
->sh_entsize
= 0;
4337 shstrtab_hdr
->sh_link
= 0;
4338 shstrtab_hdr
->sh_info
= 0;
4339 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4340 shstrtab_hdr
->sh_addralign
= 1;
4342 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4348 Elf_Internal_Shdr
*hdr
;
4350 off
= elf_next_file_pos (abfd
);
4352 hdr
= & elf_symtab_hdr (abfd
);
4353 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4355 if (elf_symtab_shndx_list (abfd
) != NULL
)
4357 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4358 if (hdr
->sh_size
!= 0)
4359 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4360 /* FIXME: What about other symtab_shndx sections in the list ? */
4363 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4364 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4366 elf_next_file_pos (abfd
) = off
;
4368 /* Now that we know where the .strtab section goes, write it
4370 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4371 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4373 _bfd_elf_strtab_free (strtab
);
4376 abfd
->output_has_begun
= TRUE
;
4381 /* Make an initial estimate of the size of the program header. If we
4382 get the number wrong here, we'll redo section placement. */
4384 static bfd_size_type
4385 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4389 const struct elf_backend_data
*bed
;
4391 /* Assume we will need exactly two PT_LOAD segments: one for text
4392 and one for data. */
4395 s
= bfd_get_section_by_name (abfd
, ".interp");
4396 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4398 /* If we have a loadable interpreter section, we need a
4399 PT_INTERP segment. In this case, assume we also need a
4400 PT_PHDR segment, although that may not be true for all
4405 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4407 /* We need a PT_DYNAMIC segment. */
4411 if (info
!= NULL
&& info
->relro
)
4413 /* We need a PT_GNU_RELRO segment. */
4417 if (elf_eh_frame_hdr (abfd
))
4419 /* We need a PT_GNU_EH_FRAME segment. */
4423 if (elf_stack_flags (abfd
))
4425 /* We need a PT_GNU_STACK segment. */
4429 s
= bfd_get_section_by_name (abfd
,
4430 NOTE_GNU_PROPERTY_SECTION_NAME
);
4431 if (s
!= NULL
&& s
->size
!= 0)
4433 /* We need a PT_GNU_PROPERTY segment. */
4437 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4439 if ((s
->flags
& SEC_LOAD
) != 0
4440 && elf_section_type (s
) == SHT_NOTE
)
4442 unsigned int alignment_power
;
4443 /* We need a PT_NOTE segment. */
4445 /* Try to create just one PT_NOTE segment for all adjacent
4446 loadable SHT_NOTE sections. gABI requires that within a
4447 PT_NOTE segment (and also inside of each SHT_NOTE section)
4448 each note should have the same alignment. So we check
4449 whether the sections are correctly aligned. */
4450 alignment_power
= s
->alignment_power
;
4451 while (s
->next
!= NULL
4452 && s
->next
->alignment_power
== alignment_power
4453 && (s
->next
->flags
& SEC_LOAD
) != 0
4454 && elf_section_type (s
->next
) == SHT_NOTE
)
4459 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4461 if (s
->flags
& SEC_THREAD_LOCAL
)
4463 /* We need a PT_TLS segment. */
4469 bed
= get_elf_backend_data (abfd
);
4471 if ((abfd
->flags
& D_PAGED
) != 0
4472 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4474 /* Add a PT_GNU_MBIND segment for each mbind section. */
4475 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4476 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4477 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4479 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4482 /* xgettext:c-format */
4483 (_("%pB: GNU_MBIND section `%pA' has invalid "
4484 "sh_info field: %d"),
4485 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4488 /* Align mbind section to page size. */
4489 if (s
->alignment_power
< page_align_power
)
4490 s
->alignment_power
= page_align_power
;
4495 /* Let the backend count up any program headers it might need. */
4496 if (bed
->elf_backend_additional_program_headers
)
4500 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4506 return segs
* bed
->s
->sizeof_phdr
;
4509 /* Find the segment that contains the output_section of section. */
4512 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4514 struct elf_segment_map
*m
;
4515 Elf_Internal_Phdr
*p
;
4517 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4523 for (i
= m
->count
- 1; i
>= 0; i
--)
4524 if (m
->sections
[i
] == section
)
4531 /* Create a mapping from a set of sections to a program segment. */
4533 static struct elf_segment_map
*
4534 make_mapping (bfd
*abfd
,
4535 asection
**sections
,
4540 struct elf_segment_map
*m
;
4545 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4546 amt
+= (to
- from
) * sizeof (asection
*);
4547 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4551 m
->p_type
= PT_LOAD
;
4552 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4553 m
->sections
[i
- from
] = *hdrpp
;
4554 m
->count
= to
- from
;
4556 if (from
== 0 && phdr
)
4558 /* Include the headers in the first PT_LOAD segment. */
4559 m
->includes_filehdr
= 1;
4560 m
->includes_phdrs
= 1;
4566 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4569 struct elf_segment_map
*
4570 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4572 struct elf_segment_map
*m
;
4574 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4575 sizeof (struct elf_segment_map
));
4579 m
->p_type
= PT_DYNAMIC
;
4581 m
->sections
[0] = dynsec
;
4586 /* Possibly add or remove segments from the segment map. */
4589 elf_modify_segment_map (bfd
*abfd
,
4590 struct bfd_link_info
*info
,
4591 bfd_boolean remove_empty_load
)
4593 struct elf_segment_map
**m
;
4594 const struct elf_backend_data
*bed
;
4596 /* The placement algorithm assumes that non allocated sections are
4597 not in PT_LOAD segments. We ensure this here by removing such
4598 sections from the segment map. We also remove excluded
4599 sections. Finally, any PT_LOAD segment without sections is
4601 m
= &elf_seg_map (abfd
);
4604 unsigned int i
, new_count
;
4606 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4608 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4609 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4610 || (*m
)->p_type
!= PT_LOAD
))
4612 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4616 (*m
)->count
= new_count
;
4618 if (remove_empty_load
4619 && (*m
)->p_type
== PT_LOAD
4621 && !(*m
)->includes_phdrs
)
4627 bed
= get_elf_backend_data (abfd
);
4628 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4630 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4637 #define IS_TBSS(s) \
4638 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4640 /* Set up a mapping from BFD sections to program segments. */
4643 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4646 struct elf_segment_map
*m
;
4647 asection
**sections
= NULL
;
4648 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4649 bfd_boolean no_user_phdrs
;
4651 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4654 info
->user_phdrs
= !no_user_phdrs
;
4656 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4660 struct elf_segment_map
*mfirst
;
4661 struct elf_segment_map
**pm
;
4664 unsigned int hdr_index
;
4665 bfd_vma maxpagesize
;
4667 bfd_boolean phdr_in_segment
;
4668 bfd_boolean writable
;
4669 bfd_boolean executable
;
4670 unsigned int tls_count
= 0;
4671 asection
*first_tls
= NULL
;
4672 asection
*first_mbind
= NULL
;
4673 asection
*dynsec
, *eh_frame_hdr
;
4675 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4676 bfd_size_type phdr_size
; /* Octets/bytes. */
4677 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4679 /* Select the allocated sections, and sort them. */
4681 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4682 sections
= (asection
**) bfd_malloc (amt
);
4683 if (sections
== NULL
)
4686 /* Calculate top address, avoiding undefined behaviour of shift
4687 left operator when shift count is equal to size of type
4689 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4690 addr_mask
= (addr_mask
<< 1) + 1;
4693 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4695 if ((s
->flags
& SEC_ALLOC
) != 0)
4697 /* target_index is unused until bfd_elf_final_link
4698 starts output of section symbols. Use it to make
4700 s
->target_index
= i
;
4703 /* A wrapping section potentially clashes with header. */
4704 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4705 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4708 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4711 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4713 phdr_size
= elf_program_header_size (abfd
);
4714 if (phdr_size
== (bfd_size_type
) -1)
4715 phdr_size
= get_program_header_size (abfd
, info
);
4716 phdr_size
+= bed
->s
->sizeof_ehdr
;
4717 /* phdr_size is compared to LMA values which are in bytes. */
4719 maxpagesize
= bed
->maxpagesize
;
4720 if (maxpagesize
== 0)
4722 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4724 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4725 >= (phdr_size
& (maxpagesize
- 1))))
4726 /* For compatibility with old scripts that may not be using
4727 SIZEOF_HEADERS, add headers when it looks like space has
4728 been left for them. */
4729 phdr_in_segment
= TRUE
;
4731 /* Build the mapping. */
4735 /* If we have a .interp section, then create a PT_PHDR segment for
4736 the program headers and a PT_INTERP segment for the .interp
4738 s
= bfd_get_section_by_name (abfd
, ".interp");
4739 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4741 amt
= sizeof (struct elf_segment_map
);
4742 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4746 m
->p_type
= PT_PHDR
;
4748 m
->p_flags_valid
= 1;
4749 m
->includes_phdrs
= 1;
4750 phdr_in_segment
= TRUE
;
4754 amt
= sizeof (struct elf_segment_map
);
4755 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4759 m
->p_type
= PT_INTERP
;
4767 /* Look through the sections. We put sections in the same program
4768 segment when the start of the second section can be placed within
4769 a few bytes of the end of the first section. */
4775 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4777 && (dynsec
->flags
& SEC_LOAD
) == 0)
4780 if ((abfd
->flags
& D_PAGED
) == 0)
4781 phdr_in_segment
= FALSE
;
4783 /* Deal with -Ttext or something similar such that the first section
4784 is not adjacent to the program headers. This is an
4785 approximation, since at this point we don't know exactly how many
4786 program headers we will need. */
4787 if (phdr_in_segment
&& count
> 0)
4789 bfd_vma phdr_lma
; /* Bytes. */
4790 bfd_boolean separate_phdr
= FALSE
;
4792 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4794 && info
->separate_code
4795 && (sections
[0]->flags
& SEC_CODE
) != 0)
4797 /* If data sections should be separate from code and
4798 thus not executable, and the first section is
4799 executable then put the file and program headers in
4800 their own PT_LOAD. */
4801 separate_phdr
= TRUE
;
4802 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4803 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4805 /* The file and program headers are currently on the
4806 same page as the first section. Put them on the
4807 previous page if we can. */
4808 if (phdr_lma
>= maxpagesize
)
4809 phdr_lma
-= maxpagesize
;
4811 separate_phdr
= FALSE
;
4814 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4815 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4816 /* If file and program headers would be placed at the end
4817 of memory then it's probably better to omit them. */
4818 phdr_in_segment
= FALSE
;
4819 else if (phdr_lma
< wrap_to
)
4820 /* If a section wraps around to where we'll be placing
4821 file and program headers, then the headers will be
4823 phdr_in_segment
= FALSE
;
4824 else if (separate_phdr
)
4826 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4829 m
->p_paddr
= phdr_lma
* opb
;
4831 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4832 m
->p_paddr_valid
= 1;
4835 phdr_in_segment
= FALSE
;
4839 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4842 bfd_boolean new_segment
;
4846 /* See if this section and the last one will fit in the same
4849 if (last_hdr
== NULL
)
4851 /* If we don't have a segment yet, then we don't need a new
4852 one (we build the last one after this loop). */
4853 new_segment
= FALSE
;
4855 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4857 /* If this section has a different relation between the
4858 virtual address and the load address, then we need a new
4862 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4863 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4865 /* If this section has a load address that makes it overlap
4866 the previous section, then we need a new segment. */
4869 else if ((abfd
->flags
& D_PAGED
) != 0
4870 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4871 == (hdr
->lma
& -maxpagesize
)))
4873 /* If we are demand paged then we can't map two disk
4874 pages onto the same memory page. */
4875 new_segment
= FALSE
;
4877 /* In the next test we have to be careful when last_hdr->lma is close
4878 to the end of the address space. If the aligned address wraps
4879 around to the start of the address space, then there are no more
4880 pages left in memory and it is OK to assume that the current
4881 section can be included in the current segment. */
4882 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4883 + maxpagesize
> last_hdr
->lma
)
4884 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4885 + maxpagesize
<= hdr
->lma
))
4887 /* If putting this section in this segment would force us to
4888 skip a page in the segment, then we need a new segment. */
4891 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4892 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4894 /* We don't want to put a loaded section after a
4895 nonloaded (ie. bss style) section in the same segment
4896 as that will force the non-loaded section to be loaded.
4897 Consider .tbss sections as loaded for this purpose. */
4900 else if ((abfd
->flags
& D_PAGED
) == 0)
4902 /* If the file is not demand paged, which means that we
4903 don't require the sections to be correctly aligned in the
4904 file, then there is no other reason for a new segment. */
4905 new_segment
= FALSE
;
4907 else if (info
!= NULL
4908 && info
->separate_code
4909 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4914 && (hdr
->flags
& SEC_READONLY
) == 0)
4916 /* We don't want to put a writable section in a read only
4922 /* Otherwise, we can use the same segment. */
4923 new_segment
= FALSE
;
4926 /* Allow interested parties a chance to override our decision. */
4927 if (last_hdr
!= NULL
4929 && info
->callbacks
->override_segment_assignment
!= NULL
)
4931 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4937 if ((hdr
->flags
& SEC_READONLY
) == 0)
4939 if ((hdr
->flags
& SEC_CODE
) != 0)
4942 /* .tbss sections effectively have zero size. */
4943 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4947 /* We need a new program segment. We must create a new program
4948 header holding all the sections from hdr_index until hdr. */
4950 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4957 if ((hdr
->flags
& SEC_READONLY
) == 0)
4962 if ((hdr
->flags
& SEC_CODE
) == 0)
4968 /* .tbss sections effectively have zero size. */
4969 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4971 phdr_in_segment
= FALSE
;
4974 /* Create a final PT_LOAD program segment, but not if it's just
4976 if (last_hdr
!= NULL
4977 && (i
- hdr_index
!= 1
4978 || !IS_TBSS (last_hdr
)))
4980 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4988 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4991 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4998 /* For each batch of consecutive loadable SHT_NOTE sections,
4999 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5000 because if we link together nonloadable .note sections and
5001 loadable .note sections, we will generate two .note sections
5002 in the output file. */
5003 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5005 if ((s
->flags
& SEC_LOAD
) != 0
5006 && elf_section_type (s
) == SHT_NOTE
)
5009 unsigned int alignment_power
= s
->alignment_power
;
5012 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5014 if (s2
->next
->alignment_power
== alignment_power
5015 && (s2
->next
->flags
& SEC_LOAD
) != 0
5016 && elf_section_type (s2
->next
) == SHT_NOTE
5017 && align_power (s2
->lma
+ s2
->size
/ opb
,
5024 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5025 amt
+= count
* sizeof (asection
*);
5026 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5030 m
->p_type
= PT_NOTE
;
5034 m
->sections
[m
->count
- count
--] = s
;
5035 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5038 m
->sections
[m
->count
- 1] = s
;
5039 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5043 if (s
->flags
& SEC_THREAD_LOCAL
)
5049 if (first_mbind
== NULL
5050 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5054 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5057 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5058 amt
+= tls_count
* sizeof (asection
*);
5059 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5064 m
->count
= tls_count
;
5065 /* Mandated PF_R. */
5067 m
->p_flags_valid
= 1;
5069 for (i
= 0; i
< tls_count
; ++i
)
5071 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5074 (_("%pB: TLS sections are not adjacent:"), abfd
);
5077 while (i
< tls_count
)
5079 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5081 _bfd_error_handler (_(" TLS: %pA"), s
);
5085 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5088 bfd_set_error (bfd_error_bad_value
);
5100 && (abfd
->flags
& D_PAGED
) != 0
5101 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5102 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5103 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5104 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5106 /* Mandated PF_R. */
5107 unsigned long p_flags
= PF_R
;
5108 if ((s
->flags
& SEC_READONLY
) == 0)
5110 if ((s
->flags
& SEC_CODE
) != 0)
5113 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5114 m
= bfd_zalloc (abfd
, amt
);
5118 m
->p_type
= (PT_GNU_MBIND_LO
5119 + elf_section_data (s
)->this_hdr
.sh_info
);
5121 m
->p_flags_valid
= 1;
5123 m
->p_flags
= p_flags
;
5129 s
= bfd_get_section_by_name (abfd
,
5130 NOTE_GNU_PROPERTY_SECTION_NAME
);
5131 if (s
!= NULL
&& s
->size
!= 0)
5133 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5134 m
= bfd_zalloc (abfd
, amt
);
5138 m
->p_type
= PT_GNU_PROPERTY
;
5140 m
->p_flags_valid
= 1;
5147 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5149 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5150 if (eh_frame_hdr
!= NULL
5151 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5153 amt
= sizeof (struct elf_segment_map
);
5154 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5158 m
->p_type
= PT_GNU_EH_FRAME
;
5160 m
->sections
[0] = eh_frame_hdr
->output_section
;
5166 if (elf_stack_flags (abfd
))
5168 amt
= sizeof (struct elf_segment_map
);
5169 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5173 m
->p_type
= PT_GNU_STACK
;
5174 m
->p_flags
= elf_stack_flags (abfd
);
5175 m
->p_align
= bed
->stack_align
;
5176 m
->p_flags_valid
= 1;
5177 m
->p_align_valid
= m
->p_align
!= 0;
5178 if (info
->stacksize
> 0)
5180 m
->p_size
= info
->stacksize
;
5181 m
->p_size_valid
= 1;
5188 if (info
!= NULL
&& info
->relro
)
5190 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5192 if (m
->p_type
== PT_LOAD
5194 && m
->sections
[0]->vma
>= info
->relro_start
5195 && m
->sections
[0]->vma
< info
->relro_end
)
5198 while (--i
!= (unsigned) -1)
5199 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5200 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5203 if (i
!= (unsigned) -1)
5208 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5211 amt
= sizeof (struct elf_segment_map
);
5212 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5216 m
->p_type
= PT_GNU_RELRO
;
5223 elf_seg_map (abfd
) = mfirst
;
5226 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5229 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5231 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5236 if (sections
!= NULL
)
5241 /* Sort sections by address. */
5244 elf_sort_sections (const void *arg1
, const void *arg2
)
5246 const asection
*sec1
= *(const asection
**) arg1
;
5247 const asection
*sec2
= *(const asection
**) arg2
;
5248 bfd_size_type size1
, size2
;
5250 /* Sort by LMA first, since this is the address used to
5251 place the section into a segment. */
5252 if (sec1
->lma
< sec2
->lma
)
5254 else if (sec1
->lma
> sec2
->lma
)
5257 /* Then sort by VMA. Normally the LMA and the VMA will be
5258 the same, and this will do nothing. */
5259 if (sec1
->vma
< sec2
->vma
)
5261 else if (sec1
->vma
> sec2
->vma
)
5264 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5266 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5273 else if (TOEND (sec2
))
5278 /* Sort by size, to put zero sized sections
5279 before others at the same address. */
5281 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5282 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5289 return sec1
->target_index
- sec2
->target_index
;
5292 /* This qsort comparison functions sorts PT_LOAD segments first and
5293 by p_paddr, for assign_file_positions_for_load_sections. */
5296 elf_sort_segments (const void *arg1
, const void *arg2
)
5298 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5299 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5301 if (m1
->p_type
!= m2
->p_type
)
5303 if (m1
->p_type
== PT_NULL
)
5305 if (m2
->p_type
== PT_NULL
)
5307 return m1
->p_type
< m2
->p_type
? -1 : 1;
5309 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5310 return m1
->includes_filehdr
? -1 : 1;
5311 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5312 return m1
->no_sort_lma
? -1 : 1;
5313 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5315 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5317 bfd_vma lma1
, lma2
; /* Bytes. */
5319 if (m1
->p_paddr_valid
)
5320 lma1
= m1
->p_paddr
/ opb
;
5321 else if (m1
->count
!= 0)
5322 lma1
= m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
;
5324 if (m2
->p_paddr_valid
)
5325 lma2
= m2
->p_paddr
/ opb
;
5326 else if (m2
->count
!= 0)
5327 lma2
= m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
;
5329 return lma1
< lma2
? -1 : 1;
5331 if (m1
->idx
!= m2
->idx
)
5332 return m1
->idx
< m2
->idx
? -1 : 1;
5336 /* Ian Lance Taylor writes:
5338 We shouldn't be using % with a negative signed number. That's just
5339 not good. We have to make sure either that the number is not
5340 negative, or that the number has an unsigned type. When the types
5341 are all the same size they wind up as unsigned. When file_ptr is a
5342 larger signed type, the arithmetic winds up as signed long long,
5345 What we're trying to say here is something like ``increase OFF by
5346 the least amount that will cause it to be equal to the VMA modulo
5348 /* In other words, something like:
5350 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5351 off_offset = off % bed->maxpagesize;
5352 if (vma_offset < off_offset)
5353 adjustment = vma_offset + bed->maxpagesize - off_offset;
5355 adjustment = vma_offset - off_offset;
5357 which can be collapsed into the expression below. */
5360 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5362 /* PR binutils/16199: Handle an alignment of zero. */
5363 if (maxpagesize
== 0)
5365 return ((vma
- off
) % maxpagesize
);
5369 print_segment_map (const struct elf_segment_map
*m
)
5372 const char *pt
= get_segment_type (m
->p_type
);
5377 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5378 sprintf (buf
, "LOPROC+%7.7x",
5379 (unsigned int) (m
->p_type
- PT_LOPROC
));
5380 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5381 sprintf (buf
, "LOOS+%7.7x",
5382 (unsigned int) (m
->p_type
- PT_LOOS
));
5384 snprintf (buf
, sizeof (buf
), "%8.8x",
5385 (unsigned int) m
->p_type
);
5389 fprintf (stderr
, "%s:", pt
);
5390 for (j
= 0; j
< m
->count
; j
++)
5391 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5397 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5402 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5404 buf
= bfd_zmalloc (len
);
5407 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5412 /* Assign file positions to the sections based on the mapping from
5413 sections to segments. This function also sets up some fields in
5417 assign_file_positions_for_load_sections (bfd
*abfd
,
5418 struct bfd_link_info
*link_info
)
5420 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5421 struct elf_segment_map
*m
;
5422 struct elf_segment_map
*phdr_load_seg
;
5423 Elf_Internal_Phdr
*phdrs
;
5424 Elf_Internal_Phdr
*p
;
5425 file_ptr off
; /* Octets. */
5426 bfd_size_type maxpagesize
;
5427 unsigned int alloc
, actual
;
5429 struct elf_segment_map
**sorted_seg_map
;
5430 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5432 if (link_info
== NULL
5433 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5437 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5442 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5443 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5447 /* PR binutils/12467. */
5448 elf_elfheader (abfd
)->e_phoff
= 0;
5449 elf_elfheader (abfd
)->e_phentsize
= 0;
5452 elf_elfheader (abfd
)->e_phnum
= alloc
;
5454 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5457 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5461 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5462 BFD_ASSERT (elf_program_header_size (abfd
)
5463 == actual
* bed
->s
->sizeof_phdr
);
5464 BFD_ASSERT (actual
>= alloc
);
5469 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5473 /* We're writing the size in elf_program_header_size (abfd),
5474 see assign_file_positions_except_relocs, so make sure we have
5475 that amount allocated, with trailing space cleared.
5476 The variable alloc contains the computed need, while
5477 elf_program_header_size (abfd) contains the size used for the
5479 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5480 where the layout is forced to according to a larger size in the
5481 last iterations for the testcase ld-elf/header. */
5482 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5483 + alloc
* sizeof (*sorted_seg_map
)));
5484 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5485 elf_tdata (abfd
)->phdr
= phdrs
;
5489 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5491 sorted_seg_map
[j
] = m
;
5492 /* If elf_segment_map is not from map_sections_to_segments, the
5493 sections may not be correctly ordered. NOTE: sorting should
5494 not be done to the PT_NOTE section of a corefile, which may
5495 contain several pseudo-sections artificially created by bfd.
5496 Sorting these pseudo-sections breaks things badly. */
5498 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5499 && m
->p_type
== PT_NOTE
))
5501 for (i
= 0; i
< m
->count
; i
++)
5502 m
->sections
[i
]->target_index
= i
;
5503 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5508 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5512 if ((abfd
->flags
& D_PAGED
) != 0)
5513 maxpagesize
= bed
->maxpagesize
;
5515 /* Sections must map to file offsets past the ELF file header. */
5516 off
= bed
->s
->sizeof_ehdr
;
5517 /* And if one of the PT_LOAD headers doesn't include the program
5518 headers then we'll be mapping program headers in the usual
5519 position after the ELF file header. */
5520 phdr_load_seg
= NULL
;
5521 for (j
= 0; j
< alloc
; j
++)
5523 m
= sorted_seg_map
[j
];
5524 if (m
->p_type
!= PT_LOAD
)
5526 if (m
->includes_phdrs
)
5532 if (phdr_load_seg
== NULL
)
5533 off
+= actual
* bed
->s
->sizeof_phdr
;
5535 for (j
= 0; j
< alloc
; j
++)
5538 bfd_vma off_adjust
; /* Octets. */
5539 bfd_boolean no_contents
;
5541 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5542 number of sections with contents contributing to both p_filesz
5543 and p_memsz, followed by a number of sections with no contents
5544 that just contribute to p_memsz. In this loop, OFF tracks next
5545 available file offset for PT_LOAD and PT_NOTE segments. */
5546 m
= sorted_seg_map
[j
];
5548 p
->p_type
= m
->p_type
;
5549 p
->p_flags
= m
->p_flags
;
5552 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5554 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5556 if (m
->p_paddr_valid
)
5557 p
->p_paddr
= m
->p_paddr
;
5558 else if (m
->count
== 0)
5561 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5563 if (p
->p_type
== PT_LOAD
5564 && (abfd
->flags
& D_PAGED
) != 0)
5566 /* p_align in demand paged PT_LOAD segments effectively stores
5567 the maximum page size. When copying an executable with
5568 objcopy, we set m->p_align from the input file. Use this
5569 value for maxpagesize rather than bed->maxpagesize, which
5570 may be different. Note that we use maxpagesize for PT_TLS
5571 segment alignment later in this function, so we are relying
5572 on at least one PT_LOAD segment appearing before a PT_TLS
5574 if (m
->p_align_valid
)
5575 maxpagesize
= m
->p_align
;
5577 p
->p_align
= maxpagesize
;
5579 else if (m
->p_align_valid
)
5580 p
->p_align
= m
->p_align
;
5581 else if (m
->count
== 0)
5582 p
->p_align
= 1 << bed
->s
->log_file_align
;
5584 if (m
== phdr_load_seg
)
5586 if (!m
->includes_filehdr
)
5588 off
+= actual
* bed
->s
->sizeof_phdr
;
5591 no_contents
= FALSE
;
5593 if (p
->p_type
== PT_LOAD
5596 bfd_size_type align
; /* Bytes. */
5597 unsigned int align_power
= 0;
5599 if (m
->p_align_valid
)
5603 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5605 unsigned int secalign
;
5607 secalign
= bfd_section_alignment (*secpp
);
5608 if (secalign
> align_power
)
5609 align_power
= secalign
;
5611 align
= (bfd_size_type
) 1 << align_power
;
5612 if (align
< maxpagesize
)
5613 align
= maxpagesize
;
5616 for (i
= 0; i
< m
->count
; i
++)
5617 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5618 /* If we aren't making room for this section, then
5619 it must be SHT_NOBITS regardless of what we've
5620 set via struct bfd_elf_special_section. */
5621 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5623 /* Find out whether this segment contains any loadable
5626 for (i
= 0; i
< m
->count
; i
++)
5627 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5629 no_contents
= FALSE
;
5633 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5635 /* Broken hardware and/or kernel require that files do not
5636 map the same page with different permissions on some hppa
5639 && (abfd
->flags
& D_PAGED
) != 0
5640 && bed
->no_page_alias
5641 && (off
& (maxpagesize
- 1)) != 0
5642 && ((off
& -maxpagesize
)
5643 == ((off
+ off_adjust
) & -maxpagesize
)))
5644 off_adjust
+= maxpagesize
;
5648 /* We shouldn't need to align the segment on disk since
5649 the segment doesn't need file space, but the gABI
5650 arguably requires the alignment and glibc ld.so
5651 checks it. So to comply with the alignment
5652 requirement but not waste file space, we adjust
5653 p_offset for just this segment. (OFF_ADJUST is
5654 subtracted from OFF later.) This may put p_offset
5655 past the end of file, but that shouldn't matter. */
5660 /* Make sure the .dynamic section is the first section in the
5661 PT_DYNAMIC segment. */
5662 else if (p
->p_type
== PT_DYNAMIC
5664 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5667 (_("%pB: The first section in the PT_DYNAMIC segment"
5668 " is not the .dynamic section"),
5670 bfd_set_error (bfd_error_bad_value
);
5673 /* Set the note section type to SHT_NOTE. */
5674 else if (p
->p_type
== PT_NOTE
)
5675 for (i
= 0; i
< m
->count
; i
++)
5676 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5678 if (m
->includes_filehdr
)
5680 if (!m
->p_flags_valid
)
5682 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5683 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5684 if (p
->p_type
== PT_LOAD
)
5688 if (p
->p_vaddr
< (bfd_vma
) off
5689 || (!m
->p_paddr_valid
5690 && p
->p_paddr
< (bfd_vma
) off
))
5693 (_("%pB: not enough room for program headers,"
5694 " try linking with -N"),
5696 bfd_set_error (bfd_error_bad_value
);
5700 if (!m
->p_paddr_valid
)
5704 else if (sorted_seg_map
[0]->includes_filehdr
)
5706 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5707 p
->p_vaddr
= filehdr
->p_vaddr
;
5708 if (!m
->p_paddr_valid
)
5709 p
->p_paddr
= filehdr
->p_paddr
;
5713 if (m
->includes_phdrs
)
5715 if (!m
->p_flags_valid
)
5717 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5718 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5719 if (!m
->includes_filehdr
)
5721 if (p
->p_type
== PT_LOAD
)
5723 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5726 p
->p_vaddr
-= off
- p
->p_offset
;
5727 if (!m
->p_paddr_valid
)
5728 p
->p_paddr
-= off
- p
->p_offset
;
5731 else if (phdr_load_seg
!= NULL
)
5733 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5734 bfd_vma phdr_off
= 0; /* Octets. */
5735 if (phdr_load_seg
->includes_filehdr
)
5736 phdr_off
= bed
->s
->sizeof_ehdr
;
5737 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5738 if (!m
->p_paddr_valid
)
5739 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5740 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5743 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5747 if (p
->p_type
== PT_LOAD
5748 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5750 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5755 /* Put meaningless p_offset for PT_LOAD segments
5756 without file contents somewhere within the first
5757 page, in an attempt to not point past EOF. */
5758 bfd_size_type align
= maxpagesize
;
5759 if (align
< p
->p_align
)
5763 p
->p_offset
= off
% align
;
5768 file_ptr adjust
; /* Octets. */
5770 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5772 p
->p_filesz
+= adjust
;
5773 p
->p_memsz
+= adjust
;
5777 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5778 maps. Set filepos for sections in PT_LOAD segments, and in
5779 core files, for sections in PT_NOTE segments.
5780 assign_file_positions_for_non_load_sections will set filepos
5781 for other sections and update p_filesz for other segments. */
5782 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5785 bfd_size_type align
;
5786 Elf_Internal_Shdr
*this_hdr
;
5789 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5790 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5792 if ((p
->p_type
== PT_LOAD
5793 || p
->p_type
== PT_TLS
)
5794 && (this_hdr
->sh_type
!= SHT_NOBITS
5795 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5796 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5797 || p
->p_type
== PT_TLS
))))
5799 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5800 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5801 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5802 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5806 || p_end
< p_start
))
5809 /* xgettext:c-format */
5810 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5811 abfd
, sec
, (uint64_t) s_start
/ opb
,
5812 (uint64_t) p_end
/ opb
);
5814 sec
->lma
= p_end
/ opb
;
5816 p
->p_memsz
+= adjust
;
5818 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5820 if (p
->p_type
== PT_LOAD
)
5822 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5824 /* We have a PROGBITS section following NOBITS ones.
5825 Allocate file space for the NOBITS section(s) and
5827 adjust
= p
->p_memsz
- p
->p_filesz
;
5828 if (!write_zeros (abfd
, off
, adjust
))
5833 p
->p_filesz
+= adjust
;
5837 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5839 /* The section at i == 0 is the one that actually contains
5843 this_hdr
->sh_offset
= sec
->filepos
= off
;
5844 off
+= this_hdr
->sh_size
;
5845 p
->p_filesz
= this_hdr
->sh_size
;
5851 /* The rest are fake sections that shouldn't be written. */
5860 if (p
->p_type
== PT_LOAD
)
5862 this_hdr
->sh_offset
= sec
->filepos
= off
;
5863 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5864 off
+= this_hdr
->sh_size
;
5866 else if (this_hdr
->sh_type
== SHT_NOBITS
5867 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5868 && this_hdr
->sh_offset
== 0)
5870 /* This is a .tbss section that didn't get a PT_LOAD.
5871 (See _bfd_elf_map_sections_to_segments "Create a
5872 final PT_LOAD".) Set sh_offset to the value it
5873 would have if we had created a zero p_filesz and
5874 p_memsz PT_LOAD header for the section. This
5875 also makes the PT_TLS header have the same
5877 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5879 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5882 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5884 p
->p_filesz
+= this_hdr
->sh_size
;
5885 /* A load section without SHF_ALLOC is something like
5886 a note section in a PT_NOTE segment. These take
5887 file space but are not loaded into memory. */
5888 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5889 p
->p_memsz
+= this_hdr
->sh_size
;
5891 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5893 if (p
->p_type
== PT_TLS
)
5894 p
->p_memsz
+= this_hdr
->sh_size
;
5896 /* .tbss is special. It doesn't contribute to p_memsz of
5898 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5899 p
->p_memsz
+= this_hdr
->sh_size
;
5902 if (align
> p
->p_align
5903 && !m
->p_align_valid
5904 && (p
->p_type
!= PT_LOAD
5905 || (abfd
->flags
& D_PAGED
) == 0))
5909 if (!m
->p_flags_valid
)
5912 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5914 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5921 /* PR ld/20815 - Check that the program header segment, if
5922 present, will be loaded into memory. */
5923 if (p
->p_type
== PT_PHDR
5924 && phdr_load_seg
== NULL
5925 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
5926 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
5928 /* The fix for this error is usually to edit the linker script being
5929 used and set up the program headers manually. Either that or
5930 leave room for the headers at the start of the SECTIONS. */
5931 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5932 " by LOAD segment"),
5934 if (link_info
== NULL
)
5936 /* Arrange for the linker to exit with an error, deleting
5937 the output file unless --noinhibit-exec is given. */
5938 link_info
->callbacks
->info ("%X");
5941 /* Check that all sections are in a PT_LOAD segment.
5942 Don't check funky gdb generated core files. */
5943 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5945 bfd_boolean check_vma
= TRUE
;
5947 for (i
= 1; i
< m
->count
; i
++)
5948 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5949 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5950 ->this_hdr
), p
) != 0
5951 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5952 ->this_hdr
), p
) != 0)
5954 /* Looks like we have overlays packed into the segment. */
5959 for (i
= 0; i
< m
->count
; i
++)
5961 Elf_Internal_Shdr
*this_hdr
;
5964 sec
= m
->sections
[i
];
5965 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5966 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5967 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5970 /* xgettext:c-format */
5971 (_("%pB: section `%pA' can't be allocated in segment %d"),
5973 print_segment_map (m
);
5979 elf_next_file_pos (abfd
) = off
;
5981 if (link_info
!= NULL
5982 && phdr_load_seg
!= NULL
5983 && phdr_load_seg
->includes_filehdr
)
5985 /* There is a segment that contains both the file headers and the
5986 program headers, so provide a symbol __ehdr_start pointing there.
5987 A program can use this to examine itself robustly. */
5989 struct elf_link_hash_entry
*hash
5990 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5991 FALSE
, FALSE
, TRUE
);
5992 /* If the symbol was referenced and not defined, define it. */
5994 && (hash
->root
.type
== bfd_link_hash_new
5995 || hash
->root
.type
== bfd_link_hash_undefined
5996 || hash
->root
.type
== bfd_link_hash_undefweak
5997 || hash
->root
.type
== bfd_link_hash_common
))
6000 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6002 if (phdr_load_seg
->count
!= 0)
6003 /* The segment contains sections, so use the first one. */
6004 s
= phdr_load_seg
->sections
[0];
6006 /* Use the first (i.e. lowest-addressed) section in any segment. */
6007 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6008 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6016 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6017 hash
->root
.u
.def
.section
= s
;
6021 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6022 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6025 hash
->root
.type
= bfd_link_hash_defined
;
6026 hash
->def_regular
= 1;
6034 /* Determine if a bfd is a debuginfo file. Unfortunately there
6035 is no defined method for detecting such files, so we have to
6036 use heuristics instead. */
6039 is_debuginfo_file (bfd
*abfd
)
6041 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6044 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6045 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6046 Elf_Internal_Shdr
**headerp
;
6048 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6050 Elf_Internal_Shdr
*header
= * headerp
;
6052 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6053 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6054 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6055 && header
->sh_type
!= SHT_NOBITS
6056 && header
->sh_type
!= SHT_NOTE
)
6063 /* Assign file positions for the other sections, except for compressed debugging
6064 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6067 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6068 struct bfd_link_info
*link_info
)
6070 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6071 Elf_Internal_Shdr
**i_shdrpp
;
6072 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6073 Elf_Internal_Phdr
*phdrs
;
6074 Elf_Internal_Phdr
*p
;
6075 struct elf_segment_map
*m
;
6077 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6079 i_shdrpp
= elf_elfsections (abfd
);
6080 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6081 off
= elf_next_file_pos (abfd
);
6082 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6084 Elf_Internal_Shdr
*hdr
;
6087 if (hdr
->bfd_section
!= NULL
6088 && (hdr
->bfd_section
->filepos
!= 0
6089 || (hdr
->sh_type
== SHT_NOBITS
6090 && hdr
->contents
== NULL
)))
6091 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6092 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6094 if (hdr
->sh_size
!= 0
6095 /* PR 24717 - debuginfo files are known to be not strictly
6096 compliant with the ELF standard. In particular they often
6097 have .note.gnu.property sections that are outside of any
6098 loadable segment. This is not a problem for such files,
6099 so do not warn about them. */
6100 && ! is_debuginfo_file (abfd
))
6102 /* xgettext:c-format */
6103 (_("%pB: warning: allocated section `%s' not in segment"),
6105 (hdr
->bfd_section
== NULL
6107 : hdr
->bfd_section
->name
));
6108 /* We don't need to page align empty sections. */
6109 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6110 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6113 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6115 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6118 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6119 && hdr
->bfd_section
== NULL
)
6120 /* We don't know the offset of these sections yet: their size has
6121 not been decided. */
6122 || (hdr
->bfd_section
!= NULL
6123 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6124 || (bfd_section_is_ctf (hdr
->bfd_section
)
6125 && abfd
->is_linker_output
)))
6126 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6127 || (elf_symtab_shndx_list (abfd
) != NULL
6128 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6129 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6130 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6131 hdr
->sh_offset
= -1;
6133 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6135 elf_next_file_pos (abfd
) = off
;
6137 /* Now that we have set the section file positions, we can set up
6138 the file positions for the non PT_LOAD segments. */
6139 phdrs
= elf_tdata (abfd
)->phdr
;
6140 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6142 if (p
->p_type
== PT_GNU_RELRO
)
6144 bfd_vma start
, end
; /* Bytes. */
6147 if (link_info
!= NULL
)
6149 /* During linking the range of the RELRO segment is passed
6150 in link_info. Note that there may be padding between
6151 relro_start and the first RELRO section. */
6152 start
= link_info
->relro_start
;
6153 end
= link_info
->relro_end
;
6155 else if (m
->count
!= 0)
6157 if (!m
->p_size_valid
)
6159 start
= m
->sections
[0]->vma
;
6160 end
= start
+ m
->p_size
/ opb
;
6171 struct elf_segment_map
*lm
;
6172 const Elf_Internal_Phdr
*lp
;
6175 /* Find a LOAD segment containing a section in the RELRO
6177 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6179 lm
= lm
->next
, lp
++)
6181 if (lp
->p_type
== PT_LOAD
6183 && (lm
->sections
[lm
->count
- 1]->vma
6184 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6185 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6187 && lm
->sections
[0]->vma
< end
)
6193 /* Find the section starting the RELRO segment. */
6194 for (i
= 0; i
< lm
->count
; i
++)
6196 asection
*s
= lm
->sections
[i
];
6205 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6206 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6207 p
->p_offset
= lm
->sections
[i
]->filepos
;
6208 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6209 p
->p_filesz
= p
->p_memsz
;
6211 /* The RELRO segment typically ends a few bytes
6212 into .got.plt but other layouts are possible.
6213 In cases where the end does not match any
6214 loaded section (for instance is in file
6215 padding), trim p_filesz back to correspond to
6216 the end of loaded section contents. */
6217 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6218 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6220 /* Preserve the alignment and flags if they are
6221 valid. The gold linker generates RW/4 for
6222 the PT_GNU_RELRO section. It is better for
6223 objcopy/strip to honor these attributes
6224 otherwise gdb will choke when using separate
6226 if (!m
->p_align_valid
)
6228 if (!m
->p_flags_valid
)
6234 if (link_info
!= NULL
)
6237 memset (p
, 0, sizeof *p
);
6239 else if (p
->p_type
== PT_GNU_STACK
)
6241 if (m
->p_size_valid
)
6242 p
->p_memsz
= m
->p_size
;
6244 else if (m
->count
!= 0)
6248 if (p
->p_type
!= PT_LOAD
6249 && (p
->p_type
!= PT_NOTE
6250 || bfd_get_format (abfd
) != bfd_core
))
6252 /* A user specified segment layout may include a PHDR
6253 segment that overlaps with a LOAD segment... */
6254 if (p
->p_type
== PT_PHDR
)
6260 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6262 /* PR 17512: file: 2195325e. */
6264 (_("%pB: error: non-load segment %d includes file header "
6265 "and/or program header"),
6266 abfd
, (int) (p
- phdrs
));
6271 p
->p_offset
= m
->sections
[0]->filepos
;
6272 for (i
= m
->count
; i
-- != 0;)
6274 asection
*sect
= m
->sections
[i
];
6275 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6276 if (hdr
->sh_type
!= SHT_NOBITS
)
6278 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6290 static elf_section_list
*
6291 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6293 for (;list
!= NULL
; list
= list
->next
)
6299 /* Work out the file positions of all the sections. This is called by
6300 _bfd_elf_compute_section_file_positions. All the section sizes and
6301 VMAs must be known before this is called.
6303 Reloc sections come in two flavours: Those processed specially as
6304 "side-channel" data attached to a section to which they apply, and those that
6305 bfd doesn't process as relocations. The latter sort are stored in a normal
6306 bfd section by bfd_section_from_shdr. We don't consider the former sort
6307 here, unless they form part of the loadable image. Reloc sections not
6308 assigned here (and compressed debugging sections and CTF sections which
6309 nothing else in the file can rely upon) will be handled later by
6310 assign_file_positions_for_relocs.
6312 We also don't set the positions of the .symtab and .strtab here. */
6315 assign_file_positions_except_relocs (bfd
*abfd
,
6316 struct bfd_link_info
*link_info
)
6318 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6319 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6320 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6323 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6324 && bfd_get_format (abfd
) != bfd_core
)
6326 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6327 unsigned int num_sec
= elf_numsections (abfd
);
6328 Elf_Internal_Shdr
**hdrpp
;
6332 /* Start after the ELF header. */
6333 off
= i_ehdrp
->e_ehsize
;
6335 /* We are not creating an executable, which means that we are
6336 not creating a program header, and that the actual order of
6337 the sections in the file is unimportant. */
6338 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6340 Elf_Internal_Shdr
*hdr
;
6343 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6344 && hdr
->bfd_section
== NULL
)
6345 /* Do not assign offsets for these sections yet: we don't know
6347 || (hdr
->bfd_section
!= NULL
6348 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6349 || (bfd_section_is_ctf (hdr
->bfd_section
)
6350 && abfd
->is_linker_output
)))
6351 || i
== elf_onesymtab (abfd
)
6352 || (elf_symtab_shndx_list (abfd
) != NULL
6353 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6354 || i
== elf_strtab_sec (abfd
)
6355 || i
== elf_shstrtab_sec (abfd
))
6357 hdr
->sh_offset
= -1;
6360 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6363 elf_next_file_pos (abfd
) = off
;
6364 elf_program_header_size (abfd
) = 0;
6368 /* Assign file positions for the loaded sections based on the
6369 assignment of sections to segments. */
6370 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6373 /* And for non-load sections. */
6374 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6378 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6381 /* Write out the program headers. */
6382 alloc
= i_ehdrp
->e_phnum
;
6385 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6386 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6394 _bfd_elf_init_file_header (bfd
*abfd
,
6395 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6397 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6398 struct elf_strtab_hash
*shstrtab
;
6399 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6401 i_ehdrp
= elf_elfheader (abfd
);
6403 shstrtab
= _bfd_elf_strtab_init ();
6404 if (shstrtab
== NULL
)
6407 elf_shstrtab (abfd
) = shstrtab
;
6409 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6410 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6411 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6412 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6414 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6415 i_ehdrp
->e_ident
[EI_DATA
] =
6416 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6417 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6419 if ((abfd
->flags
& DYNAMIC
) != 0)
6420 i_ehdrp
->e_type
= ET_DYN
;
6421 else if ((abfd
->flags
& EXEC_P
) != 0)
6422 i_ehdrp
->e_type
= ET_EXEC
;
6423 else if (bfd_get_format (abfd
) == bfd_core
)
6424 i_ehdrp
->e_type
= ET_CORE
;
6426 i_ehdrp
->e_type
= ET_REL
;
6428 switch (bfd_get_arch (abfd
))
6430 case bfd_arch_unknown
:
6431 i_ehdrp
->e_machine
= EM_NONE
;
6434 /* There used to be a long list of cases here, each one setting
6435 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6436 in the corresponding bfd definition. To avoid duplication,
6437 the switch was removed. Machines that need special handling
6438 can generally do it in elf_backend_final_write_processing(),
6439 unless they need the information earlier than the final write.
6440 Such need can generally be supplied by replacing the tests for
6441 e_machine with the conditions used to determine it. */
6443 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6446 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6447 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6449 /* No program header, for now. */
6450 i_ehdrp
->e_phoff
= 0;
6451 i_ehdrp
->e_phentsize
= 0;
6452 i_ehdrp
->e_phnum
= 0;
6454 /* Each bfd section is section header entry. */
6455 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6456 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6458 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6459 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6460 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6461 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6462 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6463 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6464 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6465 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6466 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6472 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6474 FIXME: We used to have code here to sort the PT_LOAD segments into
6475 ascending order, as per the ELF spec. But this breaks some programs,
6476 including the Linux kernel. But really either the spec should be
6477 changed or the programs updated. */
6480 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6482 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6484 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6485 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6486 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6487 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6488 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6490 /* Find the lowest p_vaddr in PT_LOAD segments. */
6491 bfd_vma p_vaddr
= (bfd_vma
) -1;
6492 for (; segment
< end_segment
; segment
++)
6493 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6494 p_vaddr
= segment
->p_vaddr
;
6496 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6497 segments is non-zero. */
6499 i_ehdrp
->e_type
= ET_EXEC
;
6504 /* Assign file positions for all the reloc sections which are not part
6505 of the loadable file image, and the file position of section headers. */
6508 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6511 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6512 Elf_Internal_Shdr
*shdrp
;
6513 Elf_Internal_Ehdr
*i_ehdrp
;
6514 const struct elf_backend_data
*bed
;
6516 off
= elf_next_file_pos (abfd
);
6518 shdrpp
= elf_elfsections (abfd
);
6519 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6520 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6523 if (shdrp
->sh_offset
== -1)
6525 asection
*sec
= shdrp
->bfd_section
;
6526 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6527 || shdrp
->sh_type
== SHT_RELA
);
6528 bfd_boolean is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6531 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6533 if (!is_rel
&& !is_ctf
)
6535 const char *name
= sec
->name
;
6536 struct bfd_elf_section_data
*d
;
6538 /* Compress DWARF debug sections. */
6539 if (!bfd_compress_section (abfd
, sec
,
6543 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6544 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6546 /* If section is compressed with zlib-gnu, convert
6547 section name from .debug_* to .zdebug_*. */
6549 = convert_debug_to_zdebug (abfd
, name
);
6550 if (new_name
== NULL
)
6554 /* Add section name to section name section. */
6555 if (shdrp
->sh_name
!= (unsigned int) -1)
6558 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6560 d
= elf_section_data (sec
);
6562 /* Add reloc section name to section name section. */
6564 && !_bfd_elf_set_reloc_sh_name (abfd
,
6569 && !_bfd_elf_set_reloc_sh_name (abfd
,
6574 /* Update section size and contents. */
6575 shdrp
->sh_size
= sec
->size
;
6576 shdrp
->contents
= sec
->contents
;
6577 shdrp
->bfd_section
->contents
= NULL
;
6581 /* Update section size and contents. */
6582 shdrp
->sh_size
= sec
->size
;
6583 shdrp
->contents
= sec
->contents
;
6586 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6593 /* Place section name section after DWARF debug sections have been
6595 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6596 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6597 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6598 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6600 /* Place the section headers. */
6601 i_ehdrp
= elf_elfheader (abfd
);
6602 bed
= get_elf_backend_data (abfd
);
6603 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6604 i_ehdrp
->e_shoff
= off
;
6605 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6606 elf_next_file_pos (abfd
) = off
;
6612 _bfd_elf_write_object_contents (bfd
*abfd
)
6614 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6615 Elf_Internal_Shdr
**i_shdrp
;
6617 unsigned int count
, num_sec
;
6618 struct elf_obj_tdata
*t
;
6620 if (! abfd
->output_has_begun
6621 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6623 /* Do not rewrite ELF data when the BFD has been opened for update.
6624 abfd->output_has_begun was set to TRUE on opening, so creation of new
6625 sections, and modification of existing section sizes was restricted.
6626 This means the ELF header, program headers and section headers can't have
6628 If the contents of any sections has been modified, then those changes have
6629 already been written to the BFD. */
6630 else if (abfd
->direction
== both_direction
)
6632 BFD_ASSERT (abfd
->output_has_begun
);
6636 i_shdrp
= elf_elfsections (abfd
);
6639 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6643 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6646 /* After writing the headers, we need to write the sections too... */
6647 num_sec
= elf_numsections (abfd
);
6648 for (count
= 1; count
< num_sec
; count
++)
6650 i_shdrp
[count
]->sh_name
6651 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6652 i_shdrp
[count
]->sh_name
);
6653 if (bed
->elf_backend_section_processing
)
6654 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6656 if (i_shdrp
[count
]->contents
)
6658 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6660 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6661 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6666 /* Write out the section header names. */
6667 t
= elf_tdata (abfd
);
6668 if (elf_shstrtab (abfd
) != NULL
6669 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6670 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6673 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6676 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6679 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6680 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6681 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6687 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6689 /* Hopefully this can be done just like an object file. */
6690 return _bfd_elf_write_object_contents (abfd
);
6693 /* Given a section, search the header to find them. */
6696 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6698 const struct elf_backend_data
*bed
;
6699 unsigned int sec_index
;
6701 if (elf_section_data (asect
) != NULL
6702 && elf_section_data (asect
)->this_idx
!= 0)
6703 return elf_section_data (asect
)->this_idx
;
6705 if (bfd_is_abs_section (asect
))
6706 sec_index
= SHN_ABS
;
6707 else if (bfd_is_com_section (asect
))
6708 sec_index
= SHN_COMMON
;
6709 else if (bfd_is_und_section (asect
))
6710 sec_index
= SHN_UNDEF
;
6712 sec_index
= SHN_BAD
;
6714 bed
= get_elf_backend_data (abfd
);
6715 if (bed
->elf_backend_section_from_bfd_section
)
6717 int retval
= sec_index
;
6719 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6723 if (sec_index
== SHN_BAD
)
6724 bfd_set_error (bfd_error_nonrepresentable_section
);
6729 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6733 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6735 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6737 flagword flags
= asym_ptr
->flags
;
6739 /* When gas creates relocations against local labels, it creates its
6740 own symbol for the section, but does put the symbol into the
6741 symbol chain, so udata is 0. When the linker is generating
6742 relocatable output, this section symbol may be for one of the
6743 input sections rather than the output section. */
6744 if (asym_ptr
->udata
.i
== 0
6745 && (flags
& BSF_SECTION_SYM
)
6746 && asym_ptr
->section
)
6751 sec
= asym_ptr
->section
;
6752 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6753 sec
= sec
->output_section
;
6754 if (sec
->owner
== abfd
6755 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6756 && elf_section_syms (abfd
)[indx
] != NULL
)
6757 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6760 idx
= asym_ptr
->udata
.i
;
6764 /* This case can occur when using --strip-symbol on a symbol
6765 which is used in a relocation entry. */
6767 /* xgettext:c-format */
6768 (_("%pB: symbol `%s' required but not present"),
6769 abfd
, bfd_asymbol_name (asym_ptr
));
6770 bfd_set_error (bfd_error_no_symbols
);
6777 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6778 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6786 /* Rewrite program header information. */
6789 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6791 Elf_Internal_Ehdr
*iehdr
;
6792 struct elf_segment_map
*map
;
6793 struct elf_segment_map
*map_first
;
6794 struct elf_segment_map
**pointer_to_map
;
6795 Elf_Internal_Phdr
*segment
;
6798 unsigned int num_segments
;
6799 bfd_boolean phdr_included
= FALSE
;
6800 bfd_boolean p_paddr_valid
;
6801 bfd_vma maxpagesize
;
6802 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6803 unsigned int phdr_adjust_num
= 0;
6804 const struct elf_backend_data
*bed
;
6805 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
6807 bed
= get_elf_backend_data (ibfd
);
6808 iehdr
= elf_elfheader (ibfd
);
6811 pointer_to_map
= &map_first
;
6813 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6814 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6816 /* Returns the end address of the segment + 1. */
6817 #define SEGMENT_END(segment, start) \
6818 (start + (segment->p_memsz > segment->p_filesz \
6819 ? segment->p_memsz : segment->p_filesz))
6821 #define SECTION_SIZE(section, segment) \
6822 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6823 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6824 ? section->size : 0)
6826 /* Returns TRUE if the given section is contained within
6827 the given segment. VMA addresses are compared. */
6828 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6829 (section->vma * (opb) >= segment->p_vaddr \
6830 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6831 <= (SEGMENT_END (segment, segment->p_vaddr))))
6833 /* Returns TRUE if the given section is contained within
6834 the given segment. LMA addresses are compared. */
6835 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6836 (section->lma * (opb) >= base \
6837 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6838 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6839 <= SEGMENT_END (segment, base)))
6841 /* Handle PT_NOTE segment. */
6842 #define IS_NOTE(p, s) \
6843 (p->p_type == PT_NOTE \
6844 && elf_section_type (s) == SHT_NOTE \
6845 && (bfd_vma) s->filepos >= p->p_offset \
6846 && ((bfd_vma) s->filepos + s->size \
6847 <= p->p_offset + p->p_filesz))
6849 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6851 #define IS_COREFILE_NOTE(p, s) \
6853 && bfd_get_format (ibfd) == bfd_core \
6857 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6858 linker, which generates a PT_INTERP section with p_vaddr and
6859 p_memsz set to 0. */
6860 #define IS_SOLARIS_PT_INTERP(p, s) \
6862 && p->p_paddr == 0 \
6863 && p->p_memsz == 0 \
6864 && p->p_filesz > 0 \
6865 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6867 && (bfd_vma) s->filepos >= p->p_offset \
6868 && ((bfd_vma) s->filepos + s->size \
6869 <= p->p_offset + p->p_filesz))
6871 /* Decide if the given section should be included in the given segment.
6872 A section will be included if:
6873 1. It is within the address space of the segment -- we use the LMA
6874 if that is set for the segment and the VMA otherwise,
6875 2. It is an allocated section or a NOTE section in a PT_NOTE
6877 3. There is an output section associated with it,
6878 4. The section has not already been allocated to a previous segment.
6879 5. PT_GNU_STACK segments do not include any sections.
6880 6. PT_TLS segment includes only SHF_TLS sections.
6881 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6882 8. PT_DYNAMIC should not contain empty sections at the beginning
6883 (with the possible exception of .dynamic). */
6884 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6885 ((((segment->p_paddr \
6886 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6887 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
6888 && (section->flags & SEC_ALLOC) != 0) \
6889 || IS_NOTE (segment, section)) \
6890 && segment->p_type != PT_GNU_STACK \
6891 && (segment->p_type != PT_TLS \
6892 || (section->flags & SEC_THREAD_LOCAL)) \
6893 && (segment->p_type == PT_LOAD \
6894 || segment->p_type == PT_TLS \
6895 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6896 && (segment->p_type != PT_DYNAMIC \
6897 || SECTION_SIZE (section, segment) > 0 \
6898 || (segment->p_paddr \
6899 ? segment->p_paddr != section->lma * (opb) \
6900 : segment->p_vaddr != section->vma * (opb)) \
6901 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6902 && (segment->p_type != PT_LOAD || !section->segment_mark))
6904 /* If the output section of a section in the input segment is NULL,
6905 it is removed from the corresponding output segment. */
6906 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6907 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
6908 && section->output_section != NULL)
6910 /* Returns TRUE iff seg1 starts after the end of seg2. */
6911 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6912 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6914 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6915 their VMA address ranges and their LMA address ranges overlap.
6916 It is possible to have overlapping VMA ranges without overlapping LMA
6917 ranges. RedBoot images for example can have both .data and .bss mapped
6918 to the same VMA range, but with the .data section mapped to a different
6920 #define SEGMENT_OVERLAPS(seg1, seg2) \
6921 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6922 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6923 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6924 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6926 /* Initialise the segment mark field. */
6927 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6928 section
->segment_mark
= FALSE
;
6930 /* The Solaris linker creates program headers in which all the
6931 p_paddr fields are zero. When we try to objcopy or strip such a
6932 file, we get confused. Check for this case, and if we find it
6933 don't set the p_paddr_valid fields. */
6934 p_paddr_valid
= FALSE
;
6935 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6938 if (segment
->p_paddr
!= 0)
6940 p_paddr_valid
= TRUE
;
6944 /* Scan through the segments specified in the program header
6945 of the input BFD. For this first scan we look for overlaps
6946 in the loadable segments. These can be created by weird
6947 parameters to objcopy. Also, fix some solaris weirdness. */
6948 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6953 Elf_Internal_Phdr
*segment2
;
6955 if (segment
->p_type
== PT_INTERP
)
6956 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6957 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6959 /* Mininal change so that the normal section to segment
6960 assignment code will work. */
6961 segment
->p_vaddr
= section
->vma
* opb
;
6965 if (segment
->p_type
!= PT_LOAD
)
6967 /* Remove PT_GNU_RELRO segment. */
6968 if (segment
->p_type
== PT_GNU_RELRO
)
6969 segment
->p_type
= PT_NULL
;
6973 /* Determine if this segment overlaps any previous segments. */
6974 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6976 bfd_signed_vma extra_length
;
6978 if (segment2
->p_type
!= PT_LOAD
6979 || !SEGMENT_OVERLAPS (segment
, segment2
))
6982 /* Merge the two segments together. */
6983 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6985 /* Extend SEGMENT2 to include SEGMENT and then delete
6987 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6988 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6990 if (extra_length
> 0)
6992 segment2
->p_memsz
+= extra_length
;
6993 segment2
->p_filesz
+= extra_length
;
6996 segment
->p_type
= PT_NULL
;
6998 /* Since we have deleted P we must restart the outer loop. */
7000 segment
= elf_tdata (ibfd
)->phdr
;
7005 /* Extend SEGMENT to include SEGMENT2 and then delete
7007 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
7008 - SEGMENT_END (segment
, segment
->p_vaddr
));
7010 if (extra_length
> 0)
7012 segment
->p_memsz
+= extra_length
;
7013 segment
->p_filesz
+= extra_length
;
7016 segment2
->p_type
= PT_NULL
;
7021 /* The second scan attempts to assign sections to segments. */
7022 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7026 unsigned int section_count
;
7027 asection
**sections
;
7028 asection
*output_section
;
7030 asection
*matching_lma
;
7031 asection
*suggested_lma
;
7034 asection
*first_section
;
7036 if (segment
->p_type
== PT_NULL
)
7039 first_section
= NULL
;
7040 /* Compute how many sections might be placed into this segment. */
7041 for (section
= ibfd
->sections
, section_count
= 0;
7043 section
= section
->next
)
7045 /* Find the first section in the input segment, which may be
7046 removed from the corresponding output segment. */
7047 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7049 if (first_section
== NULL
)
7050 first_section
= section
;
7051 if (section
->output_section
!= NULL
)
7056 /* Allocate a segment map big enough to contain
7057 all of the sections we have selected. */
7058 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7059 amt
+= section_count
* sizeof (asection
*);
7060 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7064 /* Initialise the fields of the segment map. Default to
7065 using the physical address of the segment in the input BFD. */
7067 map
->p_type
= segment
->p_type
;
7068 map
->p_flags
= segment
->p_flags
;
7069 map
->p_flags_valid
= 1;
7071 /* If the first section in the input segment is removed, there is
7072 no need to preserve segment physical address in the corresponding
7074 if (!first_section
|| first_section
->output_section
!= NULL
)
7076 map
->p_paddr
= segment
->p_paddr
;
7077 map
->p_paddr_valid
= p_paddr_valid
;
7080 /* Determine if this segment contains the ELF file header
7081 and if it contains the program headers themselves. */
7082 map
->includes_filehdr
= (segment
->p_offset
== 0
7083 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7084 map
->includes_phdrs
= 0;
7086 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7088 map
->includes_phdrs
=
7089 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7090 && (segment
->p_offset
+ segment
->p_filesz
7091 >= ((bfd_vma
) iehdr
->e_phoff
7092 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7094 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7095 phdr_included
= TRUE
;
7098 if (section_count
== 0)
7100 /* Special segments, such as the PT_PHDR segment, may contain
7101 no sections, but ordinary, loadable segments should contain
7102 something. They are allowed by the ELF spec however, so only
7103 a warning is produced.
7104 There is however the valid use case of embedded systems which
7105 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7106 flash memory with zeros. No warning is shown for that case. */
7107 if (segment
->p_type
== PT_LOAD
7108 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7109 /* xgettext:c-format */
7111 (_("%pB: warning: empty loadable segment detected"
7112 " at vaddr=%#" PRIx64
", is this intentional?"),
7113 ibfd
, (uint64_t) segment
->p_vaddr
);
7115 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7117 *pointer_to_map
= map
;
7118 pointer_to_map
= &map
->next
;
7123 /* Now scan the sections in the input BFD again and attempt
7124 to add their corresponding output sections to the segment map.
7125 The problem here is how to handle an output section which has
7126 been moved (ie had its LMA changed). There are four possibilities:
7128 1. None of the sections have been moved.
7129 In this case we can continue to use the segment LMA from the
7132 2. All of the sections have been moved by the same amount.
7133 In this case we can change the segment's LMA to match the LMA
7134 of the first section.
7136 3. Some of the sections have been moved, others have not.
7137 In this case those sections which have not been moved can be
7138 placed in the current segment which will have to have its size,
7139 and possibly its LMA changed, and a new segment or segments will
7140 have to be created to contain the other sections.
7142 4. The sections have been moved, but not by the same amount.
7143 In this case we can change the segment's LMA to match the LMA
7144 of the first section and we will have to create a new segment
7145 or segments to contain the other sections.
7147 In order to save time, we allocate an array to hold the section
7148 pointers that we are interested in. As these sections get assigned
7149 to a segment, they are removed from this array. */
7151 amt
= section_count
* sizeof (asection
*);
7152 sections
= (asection
**) bfd_malloc (amt
);
7153 if (sections
== NULL
)
7156 /* Step One: Scan for segment vs section LMA conflicts.
7157 Also add the sections to the section array allocated above.
7158 Also add the sections to the current segment. In the common
7159 case, where the sections have not been moved, this means that
7160 we have completely filled the segment, and there is nothing
7163 matching_lma
= NULL
;
7164 suggested_lma
= NULL
;
7166 for (section
= first_section
, j
= 0;
7168 section
= section
->next
)
7170 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7172 output_section
= section
->output_section
;
7174 sections
[j
++] = section
;
7176 /* The Solaris native linker always sets p_paddr to 0.
7177 We try to catch that case here, and set it to the
7178 correct value. Note - some backends require that
7179 p_paddr be left as zero. */
7181 && segment
->p_vaddr
!= 0
7182 && !bed
->want_p_paddr_set_to_zero
7184 && output_section
->lma
!= 0
7185 && (align_power (segment
->p_vaddr
7186 + (map
->includes_filehdr
7187 ? iehdr
->e_ehsize
: 0)
7188 + (map
->includes_phdrs
7189 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7191 output_section
->alignment_power
* opb
)
7192 == (output_section
->vma
* opb
)))
7193 map
->p_paddr
= segment
->p_vaddr
;
7195 /* Match up the physical address of the segment with the
7196 LMA address of the output section. */
7197 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7199 || IS_COREFILE_NOTE (segment
, section
)
7200 || (bed
->want_p_paddr_set_to_zero
7201 && IS_CONTAINED_BY_VMA (output_section
, segment
, opb
)))
7203 if (matching_lma
== NULL
7204 || output_section
->lma
< matching_lma
->lma
)
7205 matching_lma
= output_section
;
7207 /* We assume that if the section fits within the segment
7208 then it does not overlap any other section within that
7210 map
->sections
[isec
++] = output_section
;
7212 else if (suggested_lma
== NULL
)
7213 suggested_lma
= output_section
;
7215 if (j
== section_count
)
7220 BFD_ASSERT (j
== section_count
);
7222 /* Step Two: Adjust the physical address of the current segment,
7224 if (isec
== section_count
)
7226 /* All of the sections fitted within the segment as currently
7227 specified. This is the default case. Add the segment to
7228 the list of built segments and carry on to process the next
7229 program header in the input BFD. */
7230 map
->count
= section_count
;
7231 *pointer_to_map
= map
;
7232 pointer_to_map
= &map
->next
;
7235 && !bed
->want_p_paddr_set_to_zero
)
7237 bfd_vma hdr_size
= 0;
7238 if (map
->includes_filehdr
)
7239 hdr_size
= iehdr
->e_ehsize
;
7240 if (map
->includes_phdrs
)
7241 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7243 /* Account for padding before the first section in the
7245 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7246 - matching_lma
->lma
);
7254 /* Change the current segment's physical address to match
7255 the LMA of the first section that fitted, or if no
7256 section fitted, the first section. */
7257 if (matching_lma
== NULL
)
7258 matching_lma
= suggested_lma
;
7260 map
->p_paddr
= matching_lma
->lma
* opb
;
7262 /* Offset the segment physical address from the lma
7263 to allow for space taken up by elf headers. */
7264 if (map
->includes_phdrs
)
7266 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7268 /* iehdr->e_phnum is just an estimate of the number
7269 of program headers that we will need. Make a note
7270 here of the number we used and the segment we chose
7271 to hold these headers, so that we can adjust the
7272 offset when we know the correct value. */
7273 phdr_adjust_num
= iehdr
->e_phnum
;
7274 phdr_adjust_seg
= map
;
7277 if (map
->includes_filehdr
)
7279 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7280 map
->p_paddr
-= iehdr
->e_ehsize
;
7281 /* We've subtracted off the size of headers from the
7282 first section lma, but there may have been some
7283 alignment padding before that section too. Try to
7284 account for that by adjusting the segment lma down to
7285 the same alignment. */
7286 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7287 align
= segment
->p_align
;
7288 map
->p_paddr
&= -(align
* opb
);
7292 /* Step Three: Loop over the sections again, this time assigning
7293 those that fit to the current segment and removing them from the
7294 sections array; but making sure not to leave large gaps. Once all
7295 possible sections have been assigned to the current segment it is
7296 added to the list of built segments and if sections still remain
7297 to be assigned, a new segment is constructed before repeating
7303 suggested_lma
= NULL
;
7305 /* Fill the current segment with sections that fit. */
7306 for (j
= 0; j
< section_count
; j
++)
7308 section
= sections
[j
];
7310 if (section
== NULL
)
7313 output_section
= section
->output_section
;
7315 BFD_ASSERT (output_section
!= NULL
);
7317 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7319 || IS_COREFILE_NOTE (segment
, section
))
7321 if (map
->count
== 0)
7323 /* If the first section in a segment does not start at
7324 the beginning of the segment, then something is
7326 if (align_power (map
->p_paddr
7327 + (map
->includes_filehdr
7328 ? iehdr
->e_ehsize
: 0)
7329 + (map
->includes_phdrs
7330 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7332 output_section
->alignment_power
* opb
)
7333 != output_section
->lma
* opb
)
7340 prev_sec
= map
->sections
[map
->count
- 1];
7342 /* If the gap between the end of the previous section
7343 and the start of this section is more than
7344 maxpagesize then we need to start a new segment. */
7345 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7347 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7348 || (prev_sec
->lma
+ prev_sec
->size
7349 > output_section
->lma
))
7351 if (suggested_lma
== NULL
)
7352 suggested_lma
= output_section
;
7358 map
->sections
[map
->count
++] = output_section
;
7361 if (segment
->p_type
== PT_LOAD
)
7362 section
->segment_mark
= TRUE
;
7364 else if (suggested_lma
== NULL
)
7365 suggested_lma
= output_section
;
7368 /* PR 23932. A corrupt input file may contain sections that cannot
7369 be assigned to any segment - because for example they have a
7370 negative size - or segments that do not contain any sections.
7371 But there are also valid reasons why a segment can be empty.
7372 So allow a count of zero. */
7374 /* Add the current segment to the list of built segments. */
7375 *pointer_to_map
= map
;
7376 pointer_to_map
= &map
->next
;
7378 if (isec
< section_count
)
7380 /* We still have not allocated all of the sections to
7381 segments. Create a new segment here, initialise it
7382 and carry on looping. */
7383 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7384 amt
+= section_count
* sizeof (asection
*);
7385 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7392 /* Initialise the fields of the segment map. Set the physical
7393 physical address to the LMA of the first section that has
7394 not yet been assigned. */
7396 map
->p_type
= segment
->p_type
;
7397 map
->p_flags
= segment
->p_flags
;
7398 map
->p_flags_valid
= 1;
7399 map
->p_paddr
= suggested_lma
->lma
* opb
;
7400 map
->p_paddr_valid
= p_paddr_valid
;
7401 map
->includes_filehdr
= 0;
7402 map
->includes_phdrs
= 0;
7407 bfd_set_error (bfd_error_sorry
);
7411 while (isec
< section_count
);
7416 elf_seg_map (obfd
) = map_first
;
7418 /* If we had to estimate the number of program headers that were
7419 going to be needed, then check our estimate now and adjust
7420 the offset if necessary. */
7421 if (phdr_adjust_seg
!= NULL
)
7425 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7428 if (count
> phdr_adjust_num
)
7429 phdr_adjust_seg
->p_paddr
7430 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7432 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7433 if (map
->p_type
== PT_PHDR
)
7436 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7437 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7444 #undef IS_CONTAINED_BY_VMA
7445 #undef IS_CONTAINED_BY_LMA
7447 #undef IS_COREFILE_NOTE
7448 #undef IS_SOLARIS_PT_INTERP
7449 #undef IS_SECTION_IN_INPUT_SEGMENT
7450 #undef INCLUDE_SECTION_IN_SEGMENT
7451 #undef SEGMENT_AFTER_SEGMENT
7452 #undef SEGMENT_OVERLAPS
7456 /* Copy ELF program header information. */
7459 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7461 Elf_Internal_Ehdr
*iehdr
;
7462 struct elf_segment_map
*map
;
7463 struct elf_segment_map
*map_first
;
7464 struct elf_segment_map
**pointer_to_map
;
7465 Elf_Internal_Phdr
*segment
;
7467 unsigned int num_segments
;
7468 bfd_boolean phdr_included
= FALSE
;
7469 bfd_boolean p_paddr_valid
;
7470 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7472 iehdr
= elf_elfheader (ibfd
);
7475 pointer_to_map
= &map_first
;
7477 /* If all the segment p_paddr fields are zero, don't set
7478 map->p_paddr_valid. */
7479 p_paddr_valid
= FALSE
;
7480 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7481 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7484 if (segment
->p_paddr
!= 0)
7486 p_paddr_valid
= TRUE
;
7490 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7495 unsigned int section_count
;
7497 Elf_Internal_Shdr
*this_hdr
;
7498 asection
*first_section
= NULL
;
7499 asection
*lowest_section
;
7501 /* Compute how many sections are in this segment. */
7502 for (section
= ibfd
->sections
, section_count
= 0;
7504 section
= section
->next
)
7506 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7507 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7509 if (first_section
== NULL
)
7510 first_section
= section
;
7515 /* Allocate a segment map big enough to contain
7516 all of the sections we have selected. */
7517 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7518 amt
+= section_count
* sizeof (asection
*);
7519 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7523 /* Initialize the fields of the output segment map with the
7526 map
->p_type
= segment
->p_type
;
7527 map
->p_flags
= segment
->p_flags
;
7528 map
->p_flags_valid
= 1;
7529 map
->p_paddr
= segment
->p_paddr
;
7530 map
->p_paddr_valid
= p_paddr_valid
;
7531 map
->p_align
= segment
->p_align
;
7532 map
->p_align_valid
= 1;
7533 map
->p_vaddr_offset
= 0;
7535 if (map
->p_type
== PT_GNU_RELRO
7536 || map
->p_type
== PT_GNU_STACK
)
7538 /* The PT_GNU_RELRO segment may contain the first a few
7539 bytes in the .got.plt section even if the whole .got.plt
7540 section isn't in the PT_GNU_RELRO segment. We won't
7541 change the size of the PT_GNU_RELRO segment.
7542 Similarly, PT_GNU_STACK size is significant on uclinux
7544 map
->p_size
= segment
->p_memsz
;
7545 map
->p_size_valid
= 1;
7548 /* Determine if this segment contains the ELF file header
7549 and if it contains the program headers themselves. */
7550 map
->includes_filehdr
= (segment
->p_offset
== 0
7551 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7553 map
->includes_phdrs
= 0;
7554 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7556 map
->includes_phdrs
=
7557 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7558 && (segment
->p_offset
+ segment
->p_filesz
7559 >= ((bfd_vma
) iehdr
->e_phoff
7560 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7562 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7563 phdr_included
= TRUE
;
7566 lowest_section
= NULL
;
7567 if (section_count
!= 0)
7569 unsigned int isec
= 0;
7571 for (section
= first_section
;
7573 section
= section
->next
)
7575 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7576 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7578 map
->sections
[isec
++] = section
->output_section
;
7579 if ((section
->flags
& SEC_ALLOC
) != 0)
7583 if (lowest_section
== NULL
7584 || section
->lma
< lowest_section
->lma
)
7585 lowest_section
= section
;
7587 /* Section lmas are set up from PT_LOAD header
7588 p_paddr in _bfd_elf_make_section_from_shdr.
7589 If this header has a p_paddr that disagrees
7590 with the section lma, flag the p_paddr as
7592 if ((section
->flags
& SEC_LOAD
) != 0)
7593 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7595 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7596 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7597 map
->p_paddr_valid
= FALSE
;
7599 if (isec
== section_count
)
7605 if (section_count
== 0)
7606 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7607 else if (map
->p_paddr_valid
)
7609 /* Account for padding before the first section in the segment. */
7610 bfd_vma hdr_size
= 0;
7611 if (map
->includes_filehdr
)
7612 hdr_size
= iehdr
->e_ehsize
;
7613 if (map
->includes_phdrs
)
7614 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7616 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7617 - (lowest_section
? lowest_section
->lma
: 0));
7620 map
->count
= section_count
;
7621 *pointer_to_map
= map
;
7622 pointer_to_map
= &map
->next
;
7625 elf_seg_map (obfd
) = map_first
;
7629 /* Copy private BFD data. This copies or rewrites ELF program header
7633 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7635 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7636 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7639 if (elf_tdata (ibfd
)->phdr
== NULL
)
7642 if (ibfd
->xvec
== obfd
->xvec
)
7644 /* Check to see if any sections in the input BFD
7645 covered by ELF program header have changed. */
7646 Elf_Internal_Phdr
*segment
;
7647 asection
*section
, *osec
;
7648 unsigned int i
, num_segments
;
7649 Elf_Internal_Shdr
*this_hdr
;
7650 const struct elf_backend_data
*bed
;
7652 bed
= get_elf_backend_data (ibfd
);
7654 /* Regenerate the segment map if p_paddr is set to 0. */
7655 if (bed
->want_p_paddr_set_to_zero
)
7658 /* Initialize the segment mark field. */
7659 for (section
= obfd
->sections
; section
!= NULL
;
7660 section
= section
->next
)
7661 section
->segment_mark
= FALSE
;
7663 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7664 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7668 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7669 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7670 which severly confuses things, so always regenerate the segment
7671 map in this case. */
7672 if (segment
->p_paddr
== 0
7673 && segment
->p_memsz
== 0
7674 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7677 for (section
= ibfd
->sections
;
7678 section
!= NULL
; section
= section
->next
)
7680 /* We mark the output section so that we know it comes
7681 from the input BFD. */
7682 osec
= section
->output_section
;
7684 osec
->segment_mark
= TRUE
;
7686 /* Check if this section is covered by the segment. */
7687 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7688 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7690 /* FIXME: Check if its output section is changed or
7691 removed. What else do we need to check? */
7693 || section
->flags
!= osec
->flags
7694 || section
->lma
!= osec
->lma
7695 || section
->vma
!= osec
->vma
7696 || section
->size
!= osec
->size
7697 || section
->rawsize
!= osec
->rawsize
7698 || section
->alignment_power
!= osec
->alignment_power
)
7704 /* Check to see if any output section do not come from the
7706 for (section
= obfd
->sections
; section
!= NULL
;
7707 section
= section
->next
)
7709 if (!section
->segment_mark
)
7712 section
->segment_mark
= FALSE
;
7715 return copy_elf_program_header (ibfd
, obfd
);
7719 if (ibfd
->xvec
== obfd
->xvec
)
7721 /* When rewriting program header, set the output maxpagesize to
7722 the maximum alignment of input PT_LOAD segments. */
7723 Elf_Internal_Phdr
*segment
;
7725 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7726 bfd_vma maxpagesize
= 0;
7728 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7731 if (segment
->p_type
== PT_LOAD
7732 && maxpagesize
< segment
->p_align
)
7734 /* PR 17512: file: f17299af. */
7735 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7736 /* xgettext:c-format */
7737 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7738 PRIx64
" is too large"),
7739 ibfd
, (uint64_t) segment
->p_align
);
7741 maxpagesize
= segment
->p_align
;
7744 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7745 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7748 return rewrite_elf_program_header (ibfd
, obfd
);
7751 /* Initialize private output section information from input section. */
7754 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7758 struct bfd_link_info
*link_info
)
7761 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7762 bfd_boolean final_link
= (link_info
!= NULL
7763 && !bfd_link_relocatable (link_info
));
7765 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7766 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7769 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7771 /* If this is a known ABI section, ELF section type and flags may
7772 have been set up when OSEC was created. For normal sections we
7773 allow the user to override the type and flags other than
7774 SHF_MASKOS and SHF_MASKPROC. */
7775 if (elf_section_type (osec
) == SHT_PROGBITS
7776 || elf_section_type (osec
) == SHT_NOTE
7777 || elf_section_type (osec
) == SHT_NOBITS
)
7778 elf_section_type (osec
) = SHT_NULL
;
7779 /* For objcopy and relocatable link, copy the ELF section type from
7780 the input file if the BFD section flags are the same. (If they
7781 are different the user may be doing something like
7782 "objcopy --set-section-flags .text=alloc,data".) For a final
7783 link allow some flags that the linker clears to differ. */
7784 if (elf_section_type (osec
) == SHT_NULL
7785 && (osec
->flags
== isec
->flags
7787 && ((osec
->flags
^ isec
->flags
)
7788 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7789 elf_section_type (osec
) = elf_section_type (isec
);
7791 /* FIXME: Is this correct for all OS/PROC specific flags? */
7792 elf_section_flags (osec
) = (elf_section_flags (isec
)
7793 & (SHF_MASKOS
| SHF_MASKPROC
));
7795 /* Copy sh_info from input for mbind section. */
7796 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7797 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7798 elf_section_data (osec
)->this_hdr
.sh_info
7799 = elf_section_data (isec
)->this_hdr
.sh_info
;
7801 /* Set things up for objcopy and relocatable link. The output
7802 SHT_GROUP section will have its elf_next_in_group pointing back
7803 to the input group members. Ignore linker created group section.
7804 See elfNN_ia64_object_p in elfxx-ia64.c. */
7805 if ((link_info
== NULL
7806 || !link_info
->resolve_section_groups
)
7807 && (elf_sec_group (isec
) == NULL
7808 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7810 if (elf_section_flags (isec
) & SHF_GROUP
)
7811 elf_section_flags (osec
) |= SHF_GROUP
;
7812 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7813 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7816 /* If not decompress, preserve SHF_COMPRESSED. */
7817 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7818 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7821 ihdr
= &elf_section_data (isec
)->this_hdr
;
7823 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7824 don't use the output section of the linked-to section since it
7825 may be NULL at this point. */
7826 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7828 ohdr
= &elf_section_data (osec
)->this_hdr
;
7829 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7830 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7833 osec
->use_rela_p
= isec
->use_rela_p
;
7838 /* Copy private section information. This copies over the entsize
7839 field, and sometimes the info field. */
7842 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7847 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7849 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7850 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7853 ihdr
= &elf_section_data (isec
)->this_hdr
;
7854 ohdr
= &elf_section_data (osec
)->this_hdr
;
7856 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7858 if (ihdr
->sh_type
== SHT_SYMTAB
7859 || ihdr
->sh_type
== SHT_DYNSYM
7860 || ihdr
->sh_type
== SHT_GNU_verneed
7861 || ihdr
->sh_type
== SHT_GNU_verdef
)
7862 ohdr
->sh_info
= ihdr
->sh_info
;
7864 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7868 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7869 necessary if we are removing either the SHT_GROUP section or any of
7870 the group member sections. DISCARDED is the value that a section's
7871 output_section has if the section will be discarded, NULL when this
7872 function is called from objcopy, bfd_abs_section_ptr when called
7876 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7880 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7881 if (elf_section_type (isec
) == SHT_GROUP
)
7883 asection
*first
= elf_next_in_group (isec
);
7884 asection
*s
= first
;
7885 bfd_size_type removed
= 0;
7889 /* If this member section is being output but the
7890 SHT_GROUP section is not, then clear the group info
7891 set up by _bfd_elf_copy_private_section_data. */
7892 if (s
->output_section
!= discarded
7893 && isec
->output_section
== discarded
)
7895 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7896 elf_group_name (s
->output_section
) = NULL
;
7898 /* Conversely, if the member section is not being output
7899 but the SHT_GROUP section is, then adjust its size. */
7900 else if (s
->output_section
== discarded
7901 && isec
->output_section
!= discarded
)
7903 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7905 if (elf_sec
->rel
.hdr
!= NULL
7906 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7908 if (elf_sec
->rela
.hdr
!= NULL
7909 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7912 s
= elf_next_in_group (s
);
7918 if (discarded
!= NULL
)
7920 /* If we've been called for ld -r, then we need to
7921 adjust the input section size. */
7922 if (isec
->rawsize
== 0)
7923 isec
->rawsize
= isec
->size
;
7924 isec
->size
= isec
->rawsize
- removed
;
7925 if (isec
->size
<= 4)
7928 isec
->flags
|= SEC_EXCLUDE
;
7933 /* Adjust the output section size when called from
7935 isec
->output_section
->size
-= removed
;
7936 if (isec
->output_section
->size
<= 4)
7938 isec
->output_section
->size
= 0;
7939 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7948 /* Copy private header information. */
7951 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7953 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7954 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7957 /* Copy over private BFD data if it has not already been copied.
7958 This must be done here, rather than in the copy_private_bfd_data
7959 entry point, because the latter is called after the section
7960 contents have been set, which means that the program headers have
7961 already been worked out. */
7962 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7964 if (! copy_private_bfd_data (ibfd
, obfd
))
7968 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7971 /* Copy private symbol information. If this symbol is in a section
7972 which we did not map into a BFD section, try to map the section
7973 index correctly. We use special macro definitions for the mapped
7974 section indices; these definitions are interpreted by the
7975 swap_out_syms function. */
7977 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7978 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7979 #define MAP_STRTAB (SHN_HIOS + 3)
7980 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7981 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7984 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7989 elf_symbol_type
*isym
, *osym
;
7991 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7992 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7995 isym
= elf_symbol_from (ibfd
, isymarg
);
7996 osym
= elf_symbol_from (obfd
, osymarg
);
7999 && isym
->internal_elf_sym
.st_shndx
!= 0
8001 && bfd_is_abs_section (isym
->symbol
.section
))
8005 shndx
= isym
->internal_elf_sym
.st_shndx
;
8006 if (shndx
== elf_onesymtab (ibfd
))
8007 shndx
= MAP_ONESYMTAB
;
8008 else if (shndx
== elf_dynsymtab (ibfd
))
8009 shndx
= MAP_DYNSYMTAB
;
8010 else if (shndx
== elf_strtab_sec (ibfd
))
8012 else if (shndx
== elf_shstrtab_sec (ibfd
))
8013 shndx
= MAP_SHSTRTAB
;
8014 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8015 shndx
= MAP_SYM_SHNDX
;
8016 osym
->internal_elf_sym
.st_shndx
= shndx
;
8022 /* Swap out the symbols. */
8025 swap_out_syms (bfd
*abfd
,
8026 struct elf_strtab_hash
**sttp
,
8029 const struct elf_backend_data
*bed
;
8030 unsigned int symcount
;
8032 struct elf_strtab_hash
*stt
;
8033 Elf_Internal_Shdr
*symtab_hdr
;
8034 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8035 Elf_Internal_Shdr
*symstrtab_hdr
;
8036 struct elf_sym_strtab
*symstrtab
;
8037 bfd_byte
*outbound_syms
;
8038 bfd_byte
*outbound_shndx
;
8039 unsigned long outbound_syms_index
;
8040 unsigned long outbound_shndx_index
;
8042 unsigned int num_locals
;
8044 bfd_boolean name_local_sections
;
8046 if (!elf_map_symbols (abfd
, &num_locals
))
8049 /* Dump out the symtabs. */
8050 stt
= _bfd_elf_strtab_init ();
8054 bed
= get_elf_backend_data (abfd
);
8055 symcount
= bfd_get_symcount (abfd
);
8056 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8057 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8058 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8059 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8060 symtab_hdr
->sh_info
= num_locals
+ 1;
8061 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8063 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8064 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8066 /* Allocate buffer to swap out the .strtab section. */
8067 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8068 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8070 bfd_set_error (bfd_error_no_memory
);
8071 _bfd_elf_strtab_free (stt
);
8075 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8076 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8079 bfd_set_error (bfd_error_no_memory
);
8082 _bfd_elf_strtab_free (stt
);
8085 symtab_hdr
->contents
= outbound_syms
;
8086 outbound_syms_index
= 0;
8088 outbound_shndx
= NULL
;
8089 outbound_shndx_index
= 0;
8091 if (elf_symtab_shndx_list (abfd
))
8093 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8094 if (symtab_shndx_hdr
->sh_name
!= 0)
8096 if (_bfd_mul_overflow (symcount
+ 1,
8097 sizeof (Elf_External_Sym_Shndx
), &amt
))
8099 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8100 if (outbound_shndx
== NULL
)
8103 symtab_shndx_hdr
->contents
= outbound_shndx
;
8104 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8105 symtab_shndx_hdr
->sh_size
= amt
;
8106 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8107 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8109 /* FIXME: What about any other headers in the list ? */
8112 /* Now generate the data (for "contents"). */
8114 /* Fill in zeroth symbol and swap it out. */
8115 Elf_Internal_Sym sym
;
8121 sym
.st_shndx
= SHN_UNDEF
;
8122 sym
.st_target_internal
= 0;
8123 symstrtab
[0].sym
= sym
;
8124 symstrtab
[0].dest_index
= outbound_syms_index
;
8125 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8126 outbound_syms_index
++;
8127 if (outbound_shndx
!= NULL
)
8128 outbound_shndx_index
++;
8132 = (bed
->elf_backend_name_local_section_symbols
8133 && bed
->elf_backend_name_local_section_symbols (abfd
));
8135 syms
= bfd_get_outsymbols (abfd
);
8136 for (idx
= 0; idx
< symcount
;)
8138 Elf_Internal_Sym sym
;
8139 bfd_vma value
= syms
[idx
]->value
;
8140 elf_symbol_type
*type_ptr
;
8141 flagword flags
= syms
[idx
]->flags
;
8144 if (!name_local_sections
8145 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8147 /* Local section symbols have no name. */
8148 sym
.st_name
= (unsigned long) -1;
8152 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8153 to get the final offset for st_name. */
8155 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8157 if (sym
.st_name
== (unsigned long) -1)
8161 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
8163 if ((flags
& BSF_SECTION_SYM
) == 0
8164 && bfd_is_com_section (syms
[idx
]->section
))
8166 /* ELF common symbols put the alignment into the `value' field,
8167 and the size into the `size' field. This is backwards from
8168 how BFD handles it, so reverse it here. */
8169 sym
.st_size
= value
;
8170 if (type_ptr
== NULL
8171 || type_ptr
->internal_elf_sym
.st_value
== 0)
8172 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8174 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8175 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8176 (abfd
, syms
[idx
]->section
);
8180 asection
*sec
= syms
[idx
]->section
;
8183 if (sec
->output_section
)
8185 value
+= sec
->output_offset
;
8186 sec
= sec
->output_section
;
8189 /* Don't add in the section vma for relocatable output. */
8190 if (! relocatable_p
)
8192 sym
.st_value
= value
;
8193 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8195 if (bfd_is_abs_section (sec
)
8197 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8199 /* This symbol is in a real ELF section which we did
8200 not create as a BFD section. Undo the mapping done
8201 by copy_private_symbol_data. */
8202 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8206 shndx
= elf_onesymtab (abfd
);
8209 shndx
= elf_dynsymtab (abfd
);
8212 shndx
= elf_strtab_sec (abfd
);
8215 shndx
= elf_shstrtab_sec (abfd
);
8218 if (elf_symtab_shndx_list (abfd
))
8219 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8226 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8228 if (bed
->symbol_section_index
)
8229 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8230 /* Otherwise just leave the index alone. */
8234 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8235 _bfd_error_handler (_("%pB: \
8236 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8245 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8247 if (shndx
== SHN_BAD
)
8251 /* Writing this would be a hell of a lot easier if
8252 we had some decent documentation on bfd, and
8253 knew what to expect of the library, and what to
8254 demand of applications. For example, it
8255 appears that `objcopy' might not set the
8256 section of a symbol to be a section that is
8257 actually in the output file. */
8258 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8260 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8261 if (shndx
== SHN_BAD
)
8263 /* xgettext:c-format */
8265 (_("unable to find equivalent output section"
8266 " for symbol '%s' from section '%s'"),
8267 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8269 bfd_set_error (bfd_error_invalid_operation
);
8275 sym
.st_shndx
= shndx
;
8278 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8280 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8281 type
= STT_GNU_IFUNC
;
8282 else if ((flags
& BSF_FUNCTION
) != 0)
8284 else if ((flags
& BSF_OBJECT
) != 0)
8286 else if ((flags
& BSF_RELC
) != 0)
8288 else if ((flags
& BSF_SRELC
) != 0)
8293 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8296 /* Processor-specific types. */
8297 if (type_ptr
!= NULL
8298 && bed
->elf_backend_get_symbol_type
)
8299 type
= ((*bed
->elf_backend_get_symbol_type
)
8300 (&type_ptr
->internal_elf_sym
, type
));
8302 if (flags
& BSF_SECTION_SYM
)
8304 if (flags
& BSF_GLOBAL
)
8305 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8307 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8309 else if (bfd_is_com_section (syms
[idx
]->section
))
8311 if (type
!= STT_TLS
)
8313 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8314 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8315 ? STT_COMMON
: STT_OBJECT
);
8317 type
= ((flags
& BSF_ELF_COMMON
) != 0
8318 ? STT_COMMON
: STT_OBJECT
);
8320 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8322 else if (bfd_is_und_section (syms
[idx
]->section
))
8323 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8327 else if (flags
& BSF_FILE
)
8328 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8331 int bind
= STB_LOCAL
;
8333 if (flags
& BSF_LOCAL
)
8335 else if (flags
& BSF_GNU_UNIQUE
)
8336 bind
= STB_GNU_UNIQUE
;
8337 else if (flags
& BSF_WEAK
)
8339 else if (flags
& BSF_GLOBAL
)
8342 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8345 if (type_ptr
!= NULL
)
8347 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8348 sym
.st_target_internal
8349 = type_ptr
->internal_elf_sym
.st_target_internal
;
8354 sym
.st_target_internal
= 0;
8358 symstrtab
[idx
].sym
= sym
;
8359 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8360 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8362 outbound_syms_index
++;
8363 if (outbound_shndx
!= NULL
)
8364 outbound_shndx_index
++;
8367 /* Finalize the .strtab section. */
8368 _bfd_elf_strtab_finalize (stt
);
8370 /* Swap out the .strtab section. */
8371 for (idx
= 0; idx
<= symcount
; idx
++)
8373 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8374 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8375 elfsym
->sym
.st_name
= 0;
8377 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8378 elfsym
->sym
.st_name
);
8379 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8381 + (elfsym
->dest_index
8382 * bed
->s
->sizeof_sym
)),
8384 + (elfsym
->destshndx_index
8385 * sizeof (Elf_External_Sym_Shndx
))));
8390 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8391 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8392 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8393 symstrtab_hdr
->sh_addr
= 0;
8394 symstrtab_hdr
->sh_entsize
= 0;
8395 symstrtab_hdr
->sh_link
= 0;
8396 symstrtab_hdr
->sh_info
= 0;
8397 symstrtab_hdr
->sh_addralign
= 1;
8402 /* Return the number of bytes required to hold the symtab vector.
8404 Note that we base it on the count plus 1, since we will null terminate
8405 the vector allocated based on this size. However, the ELF symbol table
8406 always has a dummy entry as symbol #0, so it ends up even. */
8409 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8411 bfd_size_type symcount
;
8413 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8415 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8416 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8418 bfd_set_error (bfd_error_file_too_big
);
8421 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8423 symtab_size
-= sizeof (asymbol
*);
8429 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8431 bfd_size_type symcount
;
8433 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8435 if (elf_dynsymtab (abfd
) == 0)
8437 bfd_set_error (bfd_error_invalid_operation
);
8441 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8442 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8444 bfd_set_error (bfd_error_file_too_big
);
8447 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8449 symtab_size
-= sizeof (asymbol
*);
8455 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8458 #if SIZEOF_LONG == SIZEOF_INT
8459 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8461 bfd_set_error (bfd_error_file_too_big
);
8465 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8468 /* Canonicalize the relocs. */
8471 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8478 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8480 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8483 tblptr
= section
->relocation
;
8484 for (i
= 0; i
< section
->reloc_count
; i
++)
8485 *relptr
++ = tblptr
++;
8489 return section
->reloc_count
;
8493 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8495 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8496 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8499 abfd
->symcount
= symcount
;
8504 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8505 asymbol
**allocation
)
8507 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8508 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8511 abfd
->dynsymcount
= symcount
;
8515 /* Return the size required for the dynamic reloc entries. Any loadable
8516 section that was actually installed in the BFD, and has type SHT_REL
8517 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8518 dynamic reloc section. */
8521 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8523 bfd_size_type count
;
8526 if (elf_dynsymtab (abfd
) == 0)
8528 bfd_set_error (bfd_error_invalid_operation
);
8533 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8534 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8535 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8536 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8538 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8539 if (count
> LONG_MAX
/ sizeof (arelent
*))
8541 bfd_set_error (bfd_error_file_too_big
);
8545 return count
* sizeof (arelent
*);
8548 /* Canonicalize the dynamic relocation entries. Note that we return the
8549 dynamic relocations as a single block, although they are actually
8550 associated with particular sections; the interface, which was
8551 designed for SunOS style shared libraries, expects that there is only
8552 one set of dynamic relocs. Any loadable section that was actually
8553 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8554 dynamic symbol table, is considered to be a dynamic reloc section. */
8557 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8561 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8565 if (elf_dynsymtab (abfd
) == 0)
8567 bfd_set_error (bfd_error_invalid_operation
);
8571 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8573 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8575 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8576 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8577 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8582 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8584 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8586 for (i
= 0; i
< count
; i
++)
8597 /* Read in the version information. */
8600 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8602 bfd_byte
*contents
= NULL
;
8603 unsigned int freeidx
= 0;
8606 if (elf_dynverref (abfd
) != 0)
8608 Elf_Internal_Shdr
*hdr
;
8609 Elf_External_Verneed
*everneed
;
8610 Elf_Internal_Verneed
*iverneed
;
8612 bfd_byte
*contents_end
;
8614 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8616 if (hdr
->sh_info
== 0
8617 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8619 error_return_bad_verref
:
8621 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8622 bfd_set_error (bfd_error_bad_value
);
8623 error_return_verref
:
8624 elf_tdata (abfd
)->verref
= NULL
;
8625 elf_tdata (abfd
)->cverrefs
= 0;
8629 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8630 goto error_return_verref
;
8631 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8632 if (contents
== NULL
)
8633 goto error_return_verref
;
8635 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8637 bfd_set_error (bfd_error_file_too_big
);
8638 goto error_return_verref
;
8640 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8641 if (elf_tdata (abfd
)->verref
== NULL
)
8642 goto error_return_verref
;
8644 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8645 == sizeof (Elf_External_Vernaux
));
8646 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8647 everneed
= (Elf_External_Verneed
*) contents
;
8648 iverneed
= elf_tdata (abfd
)->verref
;
8649 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8651 Elf_External_Vernaux
*evernaux
;
8652 Elf_Internal_Vernaux
*ivernaux
;
8655 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8657 iverneed
->vn_bfd
= abfd
;
8659 iverneed
->vn_filename
=
8660 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8662 if (iverneed
->vn_filename
== NULL
)
8663 goto error_return_bad_verref
;
8665 if (iverneed
->vn_cnt
== 0)
8666 iverneed
->vn_auxptr
= NULL
;
8669 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8670 sizeof (Elf_Internal_Vernaux
), &amt
))
8672 bfd_set_error (bfd_error_file_too_big
);
8673 goto error_return_verref
;
8675 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8676 bfd_alloc (abfd
, amt
);
8677 if (iverneed
->vn_auxptr
== NULL
)
8678 goto error_return_verref
;
8681 if (iverneed
->vn_aux
8682 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8683 goto error_return_bad_verref
;
8685 evernaux
= ((Elf_External_Vernaux
*)
8686 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8687 ivernaux
= iverneed
->vn_auxptr
;
8688 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8690 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8692 ivernaux
->vna_nodename
=
8693 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8694 ivernaux
->vna_name
);
8695 if (ivernaux
->vna_nodename
== NULL
)
8696 goto error_return_bad_verref
;
8698 if (ivernaux
->vna_other
> freeidx
)
8699 freeidx
= ivernaux
->vna_other
;
8701 ivernaux
->vna_nextptr
= NULL
;
8702 if (ivernaux
->vna_next
== 0)
8704 iverneed
->vn_cnt
= j
+ 1;
8707 if (j
+ 1 < iverneed
->vn_cnt
)
8708 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8710 if (ivernaux
->vna_next
8711 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8712 goto error_return_bad_verref
;
8714 evernaux
= ((Elf_External_Vernaux
*)
8715 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8718 iverneed
->vn_nextref
= NULL
;
8719 if (iverneed
->vn_next
== 0)
8721 if (i
+ 1 < hdr
->sh_info
)
8722 iverneed
->vn_nextref
= iverneed
+ 1;
8724 if (iverneed
->vn_next
8725 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8726 goto error_return_bad_verref
;
8728 everneed
= ((Elf_External_Verneed
*)
8729 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8731 elf_tdata (abfd
)->cverrefs
= i
;
8737 if (elf_dynverdef (abfd
) != 0)
8739 Elf_Internal_Shdr
*hdr
;
8740 Elf_External_Verdef
*everdef
;
8741 Elf_Internal_Verdef
*iverdef
;
8742 Elf_Internal_Verdef
*iverdefarr
;
8743 Elf_Internal_Verdef iverdefmem
;
8745 unsigned int maxidx
;
8746 bfd_byte
*contents_end_def
, *contents_end_aux
;
8748 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8750 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8752 error_return_bad_verdef
:
8754 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8755 bfd_set_error (bfd_error_bad_value
);
8756 error_return_verdef
:
8757 elf_tdata (abfd
)->verdef
= NULL
;
8758 elf_tdata (abfd
)->cverdefs
= 0;
8762 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8763 goto error_return_verdef
;
8764 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8765 if (contents
== NULL
)
8766 goto error_return_verdef
;
8768 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8769 >= sizeof (Elf_External_Verdaux
));
8770 contents_end_def
= contents
+ hdr
->sh_size
8771 - sizeof (Elf_External_Verdef
);
8772 contents_end_aux
= contents
+ hdr
->sh_size
8773 - sizeof (Elf_External_Verdaux
);
8775 /* We know the number of entries in the section but not the maximum
8776 index. Therefore we have to run through all entries and find
8778 everdef
= (Elf_External_Verdef
*) contents
;
8780 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8782 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8784 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8785 goto error_return_bad_verdef
;
8786 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8787 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8789 if (iverdefmem
.vd_next
== 0)
8792 if (iverdefmem
.vd_next
8793 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8794 goto error_return_bad_verdef
;
8796 everdef
= ((Elf_External_Verdef
*)
8797 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8800 if (default_imported_symver
)
8802 if (freeidx
> maxidx
)
8807 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8809 bfd_set_error (bfd_error_file_too_big
);
8810 goto error_return_verdef
;
8812 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8813 if (elf_tdata (abfd
)->verdef
== NULL
)
8814 goto error_return_verdef
;
8816 elf_tdata (abfd
)->cverdefs
= maxidx
;
8818 everdef
= (Elf_External_Verdef
*) contents
;
8819 iverdefarr
= elf_tdata (abfd
)->verdef
;
8820 for (i
= 0; i
< hdr
->sh_info
; i
++)
8822 Elf_External_Verdaux
*everdaux
;
8823 Elf_Internal_Verdaux
*iverdaux
;
8826 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8828 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8829 goto error_return_bad_verdef
;
8831 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8832 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8834 iverdef
->vd_bfd
= abfd
;
8836 if (iverdef
->vd_cnt
== 0)
8837 iverdef
->vd_auxptr
= NULL
;
8840 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
8841 sizeof (Elf_Internal_Verdaux
), &amt
))
8843 bfd_set_error (bfd_error_file_too_big
);
8844 goto error_return_verdef
;
8846 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8847 bfd_alloc (abfd
, amt
);
8848 if (iverdef
->vd_auxptr
== NULL
)
8849 goto error_return_verdef
;
8853 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8854 goto error_return_bad_verdef
;
8856 everdaux
= ((Elf_External_Verdaux
*)
8857 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8858 iverdaux
= iverdef
->vd_auxptr
;
8859 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8861 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8863 iverdaux
->vda_nodename
=
8864 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8865 iverdaux
->vda_name
);
8866 if (iverdaux
->vda_nodename
== NULL
)
8867 goto error_return_bad_verdef
;
8869 iverdaux
->vda_nextptr
= NULL
;
8870 if (iverdaux
->vda_next
== 0)
8872 iverdef
->vd_cnt
= j
+ 1;
8875 if (j
+ 1 < iverdef
->vd_cnt
)
8876 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8878 if (iverdaux
->vda_next
8879 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8880 goto error_return_bad_verdef
;
8882 everdaux
= ((Elf_External_Verdaux
*)
8883 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8886 iverdef
->vd_nodename
= NULL
;
8887 if (iverdef
->vd_cnt
)
8888 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8890 iverdef
->vd_nextdef
= NULL
;
8891 if (iverdef
->vd_next
== 0)
8893 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8894 iverdef
->vd_nextdef
= iverdef
+ 1;
8896 everdef
= ((Elf_External_Verdef
*)
8897 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8903 else if (default_imported_symver
)
8910 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8912 bfd_set_error (bfd_error_file_too_big
);
8915 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8916 if (elf_tdata (abfd
)->verdef
== NULL
)
8919 elf_tdata (abfd
)->cverdefs
= freeidx
;
8922 /* Create a default version based on the soname. */
8923 if (default_imported_symver
)
8925 Elf_Internal_Verdef
*iverdef
;
8926 Elf_Internal_Verdaux
*iverdaux
;
8928 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8930 iverdef
->vd_version
= VER_DEF_CURRENT
;
8931 iverdef
->vd_flags
= 0;
8932 iverdef
->vd_ndx
= freeidx
;
8933 iverdef
->vd_cnt
= 1;
8935 iverdef
->vd_bfd
= abfd
;
8937 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8938 if (iverdef
->vd_nodename
== NULL
)
8939 goto error_return_verdef
;
8940 iverdef
->vd_nextdef
= NULL
;
8941 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8942 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8943 if (iverdef
->vd_auxptr
== NULL
)
8944 goto error_return_verdef
;
8946 iverdaux
= iverdef
->vd_auxptr
;
8947 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8953 if (contents
!= NULL
)
8959 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8961 elf_symbol_type
*newsym
;
8963 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
8966 newsym
->symbol
.the_bfd
= abfd
;
8967 return &newsym
->symbol
;
8971 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8975 bfd_symbol_info (symbol
, ret
);
8978 /* Return whether a symbol name implies a local symbol. Most targets
8979 use this function for the is_local_label_name entry point, but some
8983 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8986 /* Normal local symbols start with ``.L''. */
8987 if (name
[0] == '.' && name
[1] == 'L')
8990 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8991 DWARF debugging symbols starting with ``..''. */
8992 if (name
[0] == '.' && name
[1] == '.')
8995 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8996 emitting DWARF debugging output. I suspect this is actually a
8997 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8998 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8999 underscore to be emitted on some ELF targets). For ease of use,
9000 we treat such symbols as local. */
9001 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9004 /* Treat assembler generated fake symbols, dollar local labels and
9005 forward-backward labels (aka local labels) as locals.
9006 These labels have the form:
9008 L0^A.* (fake symbols)
9010 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9012 Versions which start with .L will have already been matched above,
9013 so we only need to match the rest. */
9014 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9016 bfd_boolean ret
= FALSE
;
9020 for (p
= name
+ 2; (c
= *p
); p
++)
9022 if (c
== 1 || c
== 2)
9024 if (c
== 1 && p
== name
+ 2)
9025 /* A fake symbol. */
9028 /* FIXME: We are being paranoid here and treating symbols like
9029 L0^Bfoo as if there were non-local, on the grounds that the
9030 assembler will never generate them. But can any symbol
9031 containing an ASCII value in the range 1-31 ever be anything
9032 other than some kind of local ? */
9049 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9050 asymbol
*symbol ATTRIBUTE_UNUSED
)
9057 _bfd_elf_set_arch_mach (bfd
*abfd
,
9058 enum bfd_architecture arch
,
9059 unsigned long machine
)
9061 /* If this isn't the right architecture for this backend, and this
9062 isn't the generic backend, fail. */
9063 if (arch
!= get_elf_backend_data (abfd
)->arch
9064 && arch
!= bfd_arch_unknown
9065 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9068 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9071 /* Find the nearest line to a particular section and offset,
9072 for error reporting. */
9075 _bfd_elf_find_nearest_line (bfd
*abfd
,
9079 const char **filename_ptr
,
9080 const char **functionname_ptr
,
9081 unsigned int *line_ptr
,
9082 unsigned int *discriminator_ptr
)
9086 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
9087 filename_ptr
, functionname_ptr
,
9088 line_ptr
, discriminator_ptr
,
9089 dwarf_debug_sections
,
9090 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9093 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9094 filename_ptr
, functionname_ptr
, line_ptr
))
9096 if (!*functionname_ptr
)
9097 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9098 *filename_ptr
? NULL
: filename_ptr
,
9103 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9104 &found
, filename_ptr
,
9105 functionname_ptr
, line_ptr
,
9106 &elf_tdata (abfd
)->line_info
))
9108 if (found
&& (*functionname_ptr
|| *line_ptr
))
9111 if (symbols
== NULL
)
9114 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9115 filename_ptr
, functionname_ptr
))
9122 /* Find the line for a symbol. */
9125 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9126 const char **filename_ptr
, unsigned int *line_ptr
)
9128 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9129 filename_ptr
, NULL
, line_ptr
, NULL
,
9130 dwarf_debug_sections
,
9131 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9134 /* After a call to bfd_find_nearest_line, successive calls to
9135 bfd_find_inliner_info can be used to get source information about
9136 each level of function inlining that terminated at the address
9137 passed to bfd_find_nearest_line. Currently this is only supported
9138 for DWARF2 with appropriate DWARF3 extensions. */
9141 _bfd_elf_find_inliner_info (bfd
*abfd
,
9142 const char **filename_ptr
,
9143 const char **functionname_ptr
,
9144 unsigned int *line_ptr
)
9147 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9148 functionname_ptr
, line_ptr
,
9149 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9154 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9156 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9157 int ret
= bed
->s
->sizeof_ehdr
;
9159 if (!bfd_link_relocatable (info
))
9161 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9163 if (phdr_size
== (bfd_size_type
) -1)
9165 struct elf_segment_map
*m
;
9168 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9169 phdr_size
+= bed
->s
->sizeof_phdr
;
9172 phdr_size
= get_program_header_size (abfd
, info
);
9175 elf_program_header_size (abfd
) = phdr_size
;
9183 _bfd_elf_set_section_contents (bfd
*abfd
,
9185 const void *location
,
9187 bfd_size_type count
)
9189 Elf_Internal_Shdr
*hdr
;
9192 if (! abfd
->output_has_begun
9193 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9199 hdr
= &elf_section_data (section
)->this_hdr
;
9200 if (hdr
->sh_offset
== (file_ptr
) -1)
9202 unsigned char *contents
;
9204 if (bfd_section_is_ctf (section
))
9205 /* Nothing to do with this section: the contents are generated
9209 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9212 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9214 bfd_set_error (bfd_error_invalid_operation
);
9218 if ((offset
+ count
) > hdr
->sh_size
)
9221 (_("%pB:%pA: error: attempting to write over the end of the section"),
9224 bfd_set_error (bfd_error_invalid_operation
);
9228 contents
= hdr
->contents
;
9229 if (contents
== NULL
)
9232 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9235 bfd_set_error (bfd_error_invalid_operation
);
9239 memcpy (contents
+ offset
, location
, count
);
9243 pos
= hdr
->sh_offset
+ offset
;
9244 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9245 || bfd_bwrite (location
, count
, abfd
) != count
)
9252 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9253 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9254 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9260 /* Try to convert a non-ELF reloc into an ELF one. */
9263 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9265 /* Check whether we really have an ELF howto. */
9267 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9269 bfd_reloc_code_real_type code
;
9270 reloc_howto_type
*howto
;
9272 /* Alien reloc: Try to determine its type to replace it with an
9273 equivalent ELF reloc. */
9275 if (areloc
->howto
->pc_relative
)
9277 switch (areloc
->howto
->bitsize
)
9280 code
= BFD_RELOC_8_PCREL
;
9283 code
= BFD_RELOC_12_PCREL
;
9286 code
= BFD_RELOC_16_PCREL
;
9289 code
= BFD_RELOC_24_PCREL
;
9292 code
= BFD_RELOC_32_PCREL
;
9295 code
= BFD_RELOC_64_PCREL
;
9301 howto
= bfd_reloc_type_lookup (abfd
, code
);
9303 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9305 if (howto
->pcrel_offset
)
9306 areloc
->addend
+= areloc
->address
;
9308 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9313 switch (areloc
->howto
->bitsize
)
9319 code
= BFD_RELOC_14
;
9322 code
= BFD_RELOC_16
;
9325 code
= BFD_RELOC_26
;
9328 code
= BFD_RELOC_32
;
9331 code
= BFD_RELOC_64
;
9337 howto
= bfd_reloc_type_lookup (abfd
, code
);
9341 areloc
->howto
= howto
;
9349 /* xgettext:c-format */
9350 _bfd_error_handler (_("%pB: %s unsupported"),
9351 abfd
, areloc
->howto
->name
);
9352 bfd_set_error (bfd_error_sorry
);
9357 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9359 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9360 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9362 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9363 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9364 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9367 return _bfd_generic_close_and_cleanup (abfd
);
9370 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9371 in the relocation's offset. Thus we cannot allow any sort of sanity
9372 range-checking to interfere. There is nothing else to do in processing
9375 bfd_reloc_status_type
9376 _bfd_elf_rel_vtable_reloc_fn
9377 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9378 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9379 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9380 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9382 return bfd_reloc_ok
;
9385 /* Elf core file support. Much of this only works on native
9386 toolchains, since we rely on knowing the
9387 machine-dependent procfs structure in order to pick
9388 out details about the corefile. */
9390 #ifdef HAVE_SYS_PROCFS_H
9391 /* Needed for new procfs interface on sparc-solaris. */
9392 # define _STRUCTURED_PROC 1
9393 # include <sys/procfs.h>
9396 /* Return a PID that identifies a "thread" for threaded cores, or the
9397 PID of the main process for non-threaded cores. */
9400 elfcore_make_pid (bfd
*abfd
)
9404 pid
= elf_tdata (abfd
)->core
->lwpid
;
9406 pid
= elf_tdata (abfd
)->core
->pid
;
9411 /* If there isn't a section called NAME, make one, using
9412 data from SECT. Note, this function will generate a
9413 reference to NAME, so you shouldn't deallocate or
9417 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9421 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9424 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9428 sect2
->size
= sect
->size
;
9429 sect2
->filepos
= sect
->filepos
;
9430 sect2
->alignment_power
= sect
->alignment_power
;
9434 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9435 actually creates up to two pseudosections:
9436 - For the single-threaded case, a section named NAME, unless
9437 such a section already exists.
9438 - For the multi-threaded case, a section named "NAME/PID", where
9439 PID is elfcore_make_pid (abfd).
9440 Both pseudosections have identical contents. */
9442 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9448 char *threaded_name
;
9452 /* Build the section name. */
9454 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9455 len
= strlen (buf
) + 1;
9456 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9457 if (threaded_name
== NULL
)
9459 memcpy (threaded_name
, buf
, len
);
9461 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9466 sect
->filepos
= filepos
;
9467 sect
->alignment_power
= 2;
9469 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9473 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9476 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9482 sect
->size
= note
->descsz
- offs
;
9483 sect
->filepos
= note
->descpos
+ offs
;
9484 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9489 /* prstatus_t exists on:
9491 linux 2.[01] + glibc
9495 #if defined (HAVE_PRSTATUS_T)
9498 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9503 if (note
->descsz
== sizeof (prstatus_t
))
9507 size
= sizeof (prstat
.pr_reg
);
9508 offset
= offsetof (prstatus_t
, pr_reg
);
9509 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9511 /* Do not overwrite the core signal if it
9512 has already been set by another thread. */
9513 if (elf_tdata (abfd
)->core
->signal
== 0)
9514 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9515 if (elf_tdata (abfd
)->core
->pid
== 0)
9516 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9518 /* pr_who exists on:
9521 pr_who doesn't exist on:
9524 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9525 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9527 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9530 #if defined (HAVE_PRSTATUS32_T)
9531 else if (note
->descsz
== sizeof (prstatus32_t
))
9533 /* 64-bit host, 32-bit corefile */
9534 prstatus32_t prstat
;
9536 size
= sizeof (prstat
.pr_reg
);
9537 offset
= offsetof (prstatus32_t
, pr_reg
);
9538 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9540 /* Do not overwrite the core signal if it
9541 has already been set by another thread. */
9542 if (elf_tdata (abfd
)->core
->signal
== 0)
9543 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9544 if (elf_tdata (abfd
)->core
->pid
== 0)
9545 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9547 /* pr_who exists on:
9550 pr_who doesn't exist on:
9553 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9554 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9556 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9559 #endif /* HAVE_PRSTATUS32_T */
9562 /* Fail - we don't know how to handle any other
9563 note size (ie. data object type). */
9567 /* Make a ".reg/999" section and a ".reg" section. */
9568 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9569 size
, note
->descpos
+ offset
);
9571 #endif /* defined (HAVE_PRSTATUS_T) */
9573 /* Create a pseudosection containing the exact contents of NOTE. */
9575 elfcore_make_note_pseudosection (bfd
*abfd
,
9577 Elf_Internal_Note
*note
)
9579 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9580 note
->descsz
, note
->descpos
);
9583 /* There isn't a consistent prfpregset_t across platforms,
9584 but it doesn't matter, because we don't have to pick this
9585 data structure apart. */
9588 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9590 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9593 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9594 type of NT_PRXFPREG. Just include the whole note's contents
9598 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9600 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9603 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9604 with a note type of NT_X86_XSTATE. Just include the whole note's
9605 contents literally. */
9608 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9610 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9614 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9616 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9620 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9622 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9626 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9628 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9632 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9634 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9638 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9640 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9644 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9646 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9650 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9652 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9656 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9658 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9662 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9664 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9668 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9670 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9674 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9676 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9680 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9682 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9686 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9688 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9692 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9694 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9698 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9700 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9704 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9706 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9710 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9712 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9716 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9718 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9722 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9724 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9728 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9730 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9734 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9736 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9740 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9742 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9746 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9748 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9752 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9754 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9758 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9760 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9764 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9766 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9770 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9772 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9776 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9778 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9782 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9784 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9788 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9790 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9794 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9796 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9800 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9802 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9806 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9808 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9812 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9814 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9817 #if defined (HAVE_PRPSINFO_T)
9818 typedef prpsinfo_t elfcore_psinfo_t
;
9819 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9820 typedef prpsinfo32_t elfcore_psinfo32_t
;
9824 #if defined (HAVE_PSINFO_T)
9825 typedef psinfo_t elfcore_psinfo_t
;
9826 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9827 typedef psinfo32_t elfcore_psinfo32_t
;
9831 /* return a malloc'ed copy of a string at START which is at
9832 most MAX bytes long, possibly without a terminating '\0'.
9833 the copy will always have a terminating '\0'. */
9836 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9839 char *end
= (char *) memchr (start
, '\0', max
);
9847 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9851 memcpy (dups
, start
, len
);
9857 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9859 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9861 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9863 elfcore_psinfo_t psinfo
;
9865 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9867 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9868 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9870 elf_tdata (abfd
)->core
->program
9871 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9872 sizeof (psinfo
.pr_fname
));
9874 elf_tdata (abfd
)->core
->command
9875 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9876 sizeof (psinfo
.pr_psargs
));
9878 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9879 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9881 /* 64-bit host, 32-bit corefile */
9882 elfcore_psinfo32_t psinfo
;
9884 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9886 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9887 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9889 elf_tdata (abfd
)->core
->program
9890 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9891 sizeof (psinfo
.pr_fname
));
9893 elf_tdata (abfd
)->core
->command
9894 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9895 sizeof (psinfo
.pr_psargs
));
9901 /* Fail - we don't know how to handle any other
9902 note size (ie. data object type). */
9906 /* Note that for some reason, a spurious space is tacked
9907 onto the end of the args in some (at least one anyway)
9908 implementations, so strip it off if it exists. */
9911 char *command
= elf_tdata (abfd
)->core
->command
;
9912 int n
= strlen (command
);
9914 if (0 < n
&& command
[n
- 1] == ' ')
9915 command
[n
- 1] = '\0';
9920 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9922 #if defined (HAVE_PSTATUS_T)
9924 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9926 if (note
->descsz
== sizeof (pstatus_t
)
9927 #if defined (HAVE_PXSTATUS_T)
9928 || note
->descsz
== sizeof (pxstatus_t
)
9934 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9936 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9938 #if defined (HAVE_PSTATUS32_T)
9939 else if (note
->descsz
== sizeof (pstatus32_t
))
9941 /* 64-bit host, 32-bit corefile */
9944 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9946 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9949 /* Could grab some more details from the "representative"
9950 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9951 NT_LWPSTATUS note, presumably. */
9955 #endif /* defined (HAVE_PSTATUS_T) */
9957 #if defined (HAVE_LWPSTATUS_T)
9959 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9961 lwpstatus_t lwpstat
;
9967 if (note
->descsz
!= sizeof (lwpstat
)
9968 #if defined (HAVE_LWPXSTATUS_T)
9969 && note
->descsz
!= sizeof (lwpxstatus_t
)
9974 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9976 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9977 /* Do not overwrite the core signal if it has already been set by
9979 if (elf_tdata (abfd
)->core
->signal
== 0)
9980 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9982 /* Make a ".reg/999" section. */
9984 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9985 len
= strlen (buf
) + 1;
9986 name
= bfd_alloc (abfd
, len
);
9989 memcpy (name
, buf
, len
);
9991 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9995 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9996 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9997 sect
->filepos
= note
->descpos
9998 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10001 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10002 sect
->size
= sizeof (lwpstat
.pr_reg
);
10003 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10006 sect
->alignment_power
= 2;
10008 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10011 /* Make a ".reg2/999" section */
10013 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10014 len
= strlen (buf
) + 1;
10015 name
= bfd_alloc (abfd
, len
);
10018 memcpy (name
, buf
, len
);
10020 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10024 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10025 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10026 sect
->filepos
= note
->descpos
10027 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10030 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10031 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10032 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10035 sect
->alignment_power
= 2;
10037 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10039 #endif /* defined (HAVE_LWPSTATUS_T) */
10042 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10049 int is_active_thread
;
10052 if (note
->descsz
< 728)
10055 if (! CONST_STRNEQ (note
->namedata
, "win32"))
10058 type
= bfd_get_32 (abfd
, note
->descdata
);
10062 case 1 /* NOTE_INFO_PROCESS */:
10063 /* FIXME: need to add ->core->command. */
10064 /* process_info.pid */
10065 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10066 /* process_info.signal */
10067 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10070 case 2 /* NOTE_INFO_THREAD */:
10071 /* Make a ".reg/999" section. */
10072 /* thread_info.tid */
10073 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
10075 len
= strlen (buf
) + 1;
10076 name
= (char *) bfd_alloc (abfd
, len
);
10080 memcpy (name
, buf
, len
);
10082 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10086 /* sizeof (thread_info.thread_context) */
10088 /* offsetof (thread_info.thread_context) */
10089 sect
->filepos
= note
->descpos
+ 12;
10090 sect
->alignment_power
= 2;
10092 /* thread_info.is_active_thread */
10093 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10095 if (is_active_thread
)
10096 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10100 case 3 /* NOTE_INFO_MODULE */:
10101 /* Make a ".module/xxxxxxxx" section. */
10102 /* module_info.base_address */
10103 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10104 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10106 len
= strlen (buf
) + 1;
10107 name
= (char *) bfd_alloc (abfd
, len
);
10111 memcpy (name
, buf
, len
);
10113 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10118 sect
->size
= note
->descsz
;
10119 sect
->filepos
= note
->descpos
;
10120 sect
->alignment_power
= 2;
10131 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10133 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10135 switch (note
->type
)
10141 if (bed
->elf_backend_grok_prstatus
)
10142 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10144 #if defined (HAVE_PRSTATUS_T)
10145 return elfcore_grok_prstatus (abfd
, note
);
10150 #if defined (HAVE_PSTATUS_T)
10152 return elfcore_grok_pstatus (abfd
, note
);
10155 #if defined (HAVE_LWPSTATUS_T)
10157 return elfcore_grok_lwpstatus (abfd
, note
);
10160 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10161 return elfcore_grok_prfpreg (abfd
, note
);
10163 case NT_WIN32PSTATUS
:
10164 return elfcore_grok_win32pstatus (abfd
, note
);
10166 case NT_PRXFPREG
: /* Linux SSE extension */
10167 if (note
->namesz
== 6
10168 && strcmp (note
->namedata
, "LINUX") == 0)
10169 return elfcore_grok_prxfpreg (abfd
, note
);
10173 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10174 if (note
->namesz
== 6
10175 && strcmp (note
->namedata
, "LINUX") == 0)
10176 return elfcore_grok_xstatereg (abfd
, note
);
10181 if (note
->namesz
== 6
10182 && strcmp (note
->namedata
, "LINUX") == 0)
10183 return elfcore_grok_ppc_vmx (abfd
, note
);
10188 if (note
->namesz
== 6
10189 && strcmp (note
->namedata
, "LINUX") == 0)
10190 return elfcore_grok_ppc_vsx (abfd
, note
);
10195 if (note
->namesz
== 6
10196 && strcmp (note
->namedata
, "LINUX") == 0)
10197 return elfcore_grok_ppc_tar (abfd
, note
);
10202 if (note
->namesz
== 6
10203 && strcmp (note
->namedata
, "LINUX") == 0)
10204 return elfcore_grok_ppc_ppr (abfd
, note
);
10209 if (note
->namesz
== 6
10210 && strcmp (note
->namedata
, "LINUX") == 0)
10211 return elfcore_grok_ppc_dscr (abfd
, note
);
10216 if (note
->namesz
== 6
10217 && strcmp (note
->namedata
, "LINUX") == 0)
10218 return elfcore_grok_ppc_ebb (abfd
, note
);
10223 if (note
->namesz
== 6
10224 && strcmp (note
->namedata
, "LINUX") == 0)
10225 return elfcore_grok_ppc_pmu (abfd
, note
);
10229 case NT_PPC_TM_CGPR
:
10230 if (note
->namesz
== 6
10231 && strcmp (note
->namedata
, "LINUX") == 0)
10232 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10236 case NT_PPC_TM_CFPR
:
10237 if (note
->namesz
== 6
10238 && strcmp (note
->namedata
, "LINUX") == 0)
10239 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10243 case NT_PPC_TM_CVMX
:
10244 if (note
->namesz
== 6
10245 && strcmp (note
->namedata
, "LINUX") == 0)
10246 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10250 case NT_PPC_TM_CVSX
:
10251 if (note
->namesz
== 6
10252 && strcmp (note
->namedata
, "LINUX") == 0)
10253 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10257 case NT_PPC_TM_SPR
:
10258 if (note
->namesz
== 6
10259 && strcmp (note
->namedata
, "LINUX") == 0)
10260 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10264 case NT_PPC_TM_CTAR
:
10265 if (note
->namesz
== 6
10266 && strcmp (note
->namedata
, "LINUX") == 0)
10267 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10271 case NT_PPC_TM_CPPR
:
10272 if (note
->namesz
== 6
10273 && strcmp (note
->namedata
, "LINUX") == 0)
10274 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10278 case NT_PPC_TM_CDSCR
:
10279 if (note
->namesz
== 6
10280 && strcmp (note
->namedata
, "LINUX") == 0)
10281 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10285 case NT_S390_HIGH_GPRS
:
10286 if (note
->namesz
== 6
10287 && strcmp (note
->namedata
, "LINUX") == 0)
10288 return elfcore_grok_s390_high_gprs (abfd
, note
);
10292 case NT_S390_TIMER
:
10293 if (note
->namesz
== 6
10294 && strcmp (note
->namedata
, "LINUX") == 0)
10295 return elfcore_grok_s390_timer (abfd
, note
);
10299 case NT_S390_TODCMP
:
10300 if (note
->namesz
== 6
10301 && strcmp (note
->namedata
, "LINUX") == 0)
10302 return elfcore_grok_s390_todcmp (abfd
, note
);
10306 case NT_S390_TODPREG
:
10307 if (note
->namesz
== 6
10308 && strcmp (note
->namedata
, "LINUX") == 0)
10309 return elfcore_grok_s390_todpreg (abfd
, note
);
10314 if (note
->namesz
== 6
10315 && strcmp (note
->namedata
, "LINUX") == 0)
10316 return elfcore_grok_s390_ctrs (abfd
, note
);
10320 case NT_S390_PREFIX
:
10321 if (note
->namesz
== 6
10322 && strcmp (note
->namedata
, "LINUX") == 0)
10323 return elfcore_grok_s390_prefix (abfd
, note
);
10327 case NT_S390_LAST_BREAK
:
10328 if (note
->namesz
== 6
10329 && strcmp (note
->namedata
, "LINUX") == 0)
10330 return elfcore_grok_s390_last_break (abfd
, note
);
10334 case NT_S390_SYSTEM_CALL
:
10335 if (note
->namesz
== 6
10336 && strcmp (note
->namedata
, "LINUX") == 0)
10337 return elfcore_grok_s390_system_call (abfd
, note
);
10342 if (note
->namesz
== 6
10343 && strcmp (note
->namedata
, "LINUX") == 0)
10344 return elfcore_grok_s390_tdb (abfd
, note
);
10348 case NT_S390_VXRS_LOW
:
10349 if (note
->namesz
== 6
10350 && strcmp (note
->namedata
, "LINUX") == 0)
10351 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10355 case NT_S390_VXRS_HIGH
:
10356 if (note
->namesz
== 6
10357 && strcmp (note
->namedata
, "LINUX") == 0)
10358 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10362 case NT_S390_GS_CB
:
10363 if (note
->namesz
== 6
10364 && strcmp (note
->namedata
, "LINUX") == 0)
10365 return elfcore_grok_s390_gs_cb (abfd
, note
);
10369 case NT_S390_GS_BC
:
10370 if (note
->namesz
== 6
10371 && strcmp (note
->namedata
, "LINUX") == 0)
10372 return elfcore_grok_s390_gs_bc (abfd
, note
);
10377 if (note
->namesz
== 6
10378 && strcmp (note
->namedata
, "LINUX") == 0)
10379 return elfcore_grok_arm_vfp (abfd
, note
);
10384 if (note
->namesz
== 6
10385 && strcmp (note
->namedata
, "LINUX") == 0)
10386 return elfcore_grok_aarch_tls (abfd
, note
);
10390 case NT_ARM_HW_BREAK
:
10391 if (note
->namesz
== 6
10392 && strcmp (note
->namedata
, "LINUX") == 0)
10393 return elfcore_grok_aarch_hw_break (abfd
, note
);
10397 case NT_ARM_HW_WATCH
:
10398 if (note
->namesz
== 6
10399 && strcmp (note
->namedata
, "LINUX") == 0)
10400 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10405 if (note
->namesz
== 6
10406 && strcmp (note
->namedata
, "LINUX") == 0)
10407 return elfcore_grok_aarch_sve (abfd
, note
);
10411 case NT_ARM_PAC_MASK
:
10412 if (note
->namesz
== 6
10413 && strcmp (note
->namedata
, "LINUX") == 0)
10414 return elfcore_grok_aarch_pauth (abfd
, note
);
10420 if (bed
->elf_backend_grok_psinfo
)
10421 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10423 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10424 return elfcore_grok_psinfo (abfd
, note
);
10430 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10433 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10437 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10444 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10446 struct bfd_build_id
* build_id
;
10448 if (note
->descsz
== 0)
10451 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10452 if (build_id
== NULL
)
10455 build_id
->size
= note
->descsz
;
10456 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10457 abfd
->build_id
= build_id
;
10463 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10465 switch (note
->type
)
10470 case NT_GNU_PROPERTY_TYPE_0
:
10471 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10473 case NT_GNU_BUILD_ID
:
10474 return elfobj_grok_gnu_build_id (abfd
, note
);
10479 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10481 struct sdt_note
*cur
=
10482 (struct sdt_note
*) bfd_alloc (abfd
,
10483 sizeof (struct sdt_note
) + note
->descsz
);
10485 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10486 cur
->size
= (bfd_size_type
) note
->descsz
;
10487 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10489 elf_tdata (abfd
)->sdt_note_head
= cur
;
10495 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10497 switch (note
->type
)
10500 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10508 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10512 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10515 if (note
->descsz
< 108)
10520 if (note
->descsz
< 120)
10528 /* Check for version 1 in pr_version. */
10529 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10534 /* Skip over pr_psinfosz. */
10535 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10539 offset
+= 4; /* Padding before pr_psinfosz. */
10543 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10544 elf_tdata (abfd
)->core
->program
10545 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10548 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10549 elf_tdata (abfd
)->core
->command
10550 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10553 /* Padding before pr_pid. */
10556 /* The pr_pid field was added in version "1a". */
10557 if (note
->descsz
< offset
+ 4)
10560 elf_tdata (abfd
)->core
->pid
10561 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10567 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10573 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10574 Also compute minimum size of this note. */
10575 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10579 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10583 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10584 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10591 if (note
->descsz
< min_size
)
10594 /* Check for version 1 in pr_version. */
10595 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10598 /* Extract size of pr_reg from pr_gregsetsz. */
10599 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10600 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10602 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10607 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10611 /* Skip over pr_osreldate. */
10614 /* Read signal from pr_cursig. */
10615 if (elf_tdata (abfd
)->core
->signal
== 0)
10616 elf_tdata (abfd
)->core
->signal
10617 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10620 /* Read TID from pr_pid. */
10621 elf_tdata (abfd
)->core
->lwpid
10622 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10625 /* Padding before pr_reg. */
10626 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10629 /* Make sure that there is enough data remaining in the note. */
10630 if ((note
->descsz
- offset
) < size
)
10633 /* Make a ".reg/999" section and a ".reg" section. */
10634 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10635 size
, note
->descpos
+ offset
);
10639 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10641 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10643 switch (note
->type
)
10646 if (bed
->elf_backend_grok_freebsd_prstatus
)
10647 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10649 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10652 return elfcore_grok_prfpreg (abfd
, note
);
10655 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10657 case NT_FREEBSD_THRMISC
:
10658 if (note
->namesz
== 8)
10659 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10663 case NT_FREEBSD_PROCSTAT_PROC
:
10664 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10667 case NT_FREEBSD_PROCSTAT_FILES
:
10668 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10671 case NT_FREEBSD_PROCSTAT_VMMAP
:
10672 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10675 case NT_FREEBSD_PROCSTAT_AUXV
:
10676 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10678 case NT_X86_XSTATE
:
10679 if (note
->namesz
== 8)
10680 return elfcore_grok_xstatereg (abfd
, note
);
10684 case NT_FREEBSD_PTLWPINFO
:
10685 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10689 return elfcore_grok_arm_vfp (abfd
, note
);
10697 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10701 cp
= strchr (note
->namedata
, '@');
10704 *lwpidp
= atoi(cp
+ 1);
10711 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10713 if (note
->descsz
<= 0x7c + 31)
10716 /* Signal number at offset 0x08. */
10717 elf_tdata (abfd
)->core
->signal
10718 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10720 /* Process ID at offset 0x50. */
10721 elf_tdata (abfd
)->core
->pid
10722 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10724 /* Command name at 0x7c (max 32 bytes, including nul). */
10725 elf_tdata (abfd
)->core
->command
10726 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10728 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10733 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10737 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10738 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10740 switch (note
->type
)
10742 case NT_NETBSDCORE_PROCINFO
:
10743 /* NetBSD-specific core "procinfo". Note that we expect to
10744 find this note before any of the others, which is fine,
10745 since the kernel writes this note out first when it
10746 creates a core file. */
10747 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10748 #ifdef NT_NETBSDCORE_AUXV
10749 case NT_NETBSDCORE_AUXV
:
10750 /* NetBSD-specific Elf Auxiliary Vector data. */
10751 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10757 /* As of March 2017 there are no other machine-independent notes
10758 defined for NetBSD core files. If the note type is less
10759 than the start of the machine-dependent note types, we don't
10762 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10766 switch (bfd_get_arch (abfd
))
10768 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10769 PT_GETFPREGS == mach+2. */
10771 case bfd_arch_aarch64
:
10772 case bfd_arch_alpha
:
10773 case bfd_arch_sparc
:
10774 switch (note
->type
)
10776 case NT_NETBSDCORE_FIRSTMACH
+0:
10777 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10779 case NT_NETBSDCORE_FIRSTMACH
+2:
10780 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10786 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10787 There's also old PT___GETREGS40 == mach + 1 for old reg
10788 structure which lacks GBR. */
10791 switch (note
->type
)
10793 case NT_NETBSDCORE_FIRSTMACH
+3:
10794 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10796 case NT_NETBSDCORE_FIRSTMACH
+5:
10797 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10803 /* On all other arch's, PT_GETREGS == mach+1 and
10804 PT_GETFPREGS == mach+3. */
10807 switch (note
->type
)
10809 case NT_NETBSDCORE_FIRSTMACH
+1:
10810 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10812 case NT_NETBSDCORE_FIRSTMACH
+3:
10813 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10823 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10825 if (note
->descsz
<= 0x48 + 31)
10828 /* Signal number at offset 0x08. */
10829 elf_tdata (abfd
)->core
->signal
10830 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10832 /* Process ID at offset 0x20. */
10833 elf_tdata (abfd
)->core
->pid
10834 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10836 /* Command name at 0x48 (max 32 bytes, including nul). */
10837 elf_tdata (abfd
)->core
->command
10838 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10844 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10846 if (note
->type
== NT_OPENBSD_PROCINFO
)
10847 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10849 if (note
->type
== NT_OPENBSD_REGS
)
10850 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10852 if (note
->type
== NT_OPENBSD_FPREGS
)
10853 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10855 if (note
->type
== NT_OPENBSD_XFPREGS
)
10856 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10858 if (note
->type
== NT_OPENBSD_AUXV
)
10859 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10861 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10863 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10868 sect
->size
= note
->descsz
;
10869 sect
->filepos
= note
->descpos
;
10870 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10879 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10881 void *ddata
= note
->descdata
;
10888 if (note
->descsz
< 16)
10891 /* nto_procfs_status 'pid' field is at offset 0. */
10892 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10894 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10895 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10897 /* nto_procfs_status 'flags' field is at offset 8. */
10898 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10900 /* nto_procfs_status 'what' field is at offset 14. */
10901 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10903 elf_tdata (abfd
)->core
->signal
= sig
;
10904 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10907 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10908 do not come from signals so we make sure we set the current
10909 thread just in case. */
10910 if (flags
& 0x00000080)
10911 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10913 /* Make a ".qnx_core_status/%d" section. */
10914 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10916 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10919 strcpy (name
, buf
);
10921 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10925 sect
->size
= note
->descsz
;
10926 sect
->filepos
= note
->descpos
;
10927 sect
->alignment_power
= 2;
10929 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10933 elfcore_grok_nto_regs (bfd
*abfd
,
10934 Elf_Internal_Note
*note
,
10942 /* Make a "(base)/%d" section. */
10943 sprintf (buf
, "%s/%ld", base
, tid
);
10945 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10948 strcpy (name
, buf
);
10950 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10954 sect
->size
= note
->descsz
;
10955 sect
->filepos
= note
->descpos
;
10956 sect
->alignment_power
= 2;
10958 /* This is the current thread. */
10959 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10960 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10965 #define BFD_QNT_CORE_INFO 7
10966 #define BFD_QNT_CORE_STATUS 8
10967 #define BFD_QNT_CORE_GREG 9
10968 #define BFD_QNT_CORE_FPREG 10
10971 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10973 /* Every GREG section has a STATUS section before it. Store the
10974 tid from the previous call to pass down to the next gregs
10976 static long tid
= 1;
10978 switch (note
->type
)
10980 case BFD_QNT_CORE_INFO
:
10981 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10982 case BFD_QNT_CORE_STATUS
:
10983 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10984 case BFD_QNT_CORE_GREG
:
10985 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10986 case BFD_QNT_CORE_FPREG
:
10987 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10994 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11000 /* Use note name as section name. */
11001 len
= note
->namesz
;
11002 name
= (char *) bfd_alloc (abfd
, len
);
11005 memcpy (name
, note
->namedata
, len
);
11006 name
[len
- 1] = '\0';
11008 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11012 sect
->size
= note
->descsz
;
11013 sect
->filepos
= note
->descpos
;
11014 sect
->alignment_power
= 1;
11019 /* Function: elfcore_write_note
11022 buffer to hold note, and current size of buffer
11026 size of data for note
11028 Writes note to end of buffer. ELF64 notes are written exactly as
11029 for ELF32, despite the current (as of 2006) ELF gabi specifying
11030 that they ought to have 8-byte namesz and descsz field, and have
11031 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11034 Pointer to realloc'd buffer, *BUFSIZ updated. */
11037 elfcore_write_note (bfd
*abfd
,
11045 Elf_External_Note
*xnp
;
11052 namesz
= strlen (name
) + 1;
11054 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11056 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11059 dest
= buf
+ *bufsiz
;
11060 *bufsiz
+= newspace
;
11061 xnp
= (Elf_External_Note
*) dest
;
11062 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11063 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11064 H_PUT_32 (abfd
, type
, xnp
->type
);
11068 memcpy (dest
, name
, namesz
);
11076 memcpy (dest
, input
, size
);
11086 /* gcc-8 warns (*) on all the strncpy calls in this function about
11087 possible string truncation. The "truncation" is not a bug. We
11088 have an external representation of structs with fields that are not
11089 necessarily NULL terminated and corresponding internal
11090 representation fields that are one larger so that they can always
11091 be NULL terminated.
11092 gcc versions between 4.2 and 4.6 do not allow pragma control of
11093 diagnostics inside functions, giving a hard error if you try to use
11094 the finer control available with later versions.
11095 gcc prior to 4.2 warns about diagnostic push and pop.
11096 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11097 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11098 (*) Depending on your system header files! */
11099 #if GCC_VERSION >= 8000
11100 # pragma GCC diagnostic push
11101 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11104 elfcore_write_prpsinfo (bfd
*abfd
,
11108 const char *psargs
)
11110 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11112 if (bed
->elf_backend_write_core_note
!= NULL
)
11115 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11116 NT_PRPSINFO
, fname
, psargs
);
11121 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11122 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11123 if (bed
->s
->elfclass
== ELFCLASS32
)
11125 # if defined (HAVE_PSINFO32_T)
11127 int note_type
= NT_PSINFO
;
11130 int note_type
= NT_PRPSINFO
;
11133 memset (&data
, 0, sizeof (data
));
11134 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11135 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11136 return elfcore_write_note (abfd
, buf
, bufsiz
,
11137 "CORE", note_type
, &data
, sizeof (data
));
11142 # if defined (HAVE_PSINFO_T)
11144 int note_type
= NT_PSINFO
;
11147 int note_type
= NT_PRPSINFO
;
11150 memset (&data
, 0, sizeof (data
));
11151 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11152 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11153 return elfcore_write_note (abfd
, buf
, bufsiz
,
11154 "CORE", note_type
, &data
, sizeof (data
));
11156 #endif /* PSINFO_T or PRPSINFO_T */
11161 #if GCC_VERSION >= 8000
11162 # pragma GCC diagnostic pop
11166 elfcore_write_linux_prpsinfo32
11167 (bfd
*abfd
, char *buf
, int *bufsiz
,
11168 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11170 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11172 struct elf_external_linux_prpsinfo32_ugid16 data
;
11174 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11175 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11176 &data
, sizeof (data
));
11180 struct elf_external_linux_prpsinfo32_ugid32 data
;
11182 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11183 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11184 &data
, sizeof (data
));
11189 elfcore_write_linux_prpsinfo64
11190 (bfd
*abfd
, char *buf
, int *bufsiz
,
11191 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11193 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11195 struct elf_external_linux_prpsinfo64_ugid16 data
;
11197 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11198 return elfcore_write_note (abfd
, buf
, bufsiz
,
11199 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11203 struct elf_external_linux_prpsinfo64_ugid32 data
;
11205 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11206 return elfcore_write_note (abfd
, buf
, bufsiz
,
11207 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11212 elfcore_write_prstatus (bfd
*abfd
,
11219 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11221 if (bed
->elf_backend_write_core_note
!= NULL
)
11224 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11226 pid
, cursig
, gregs
);
11231 #if defined (HAVE_PRSTATUS_T)
11232 #if defined (HAVE_PRSTATUS32_T)
11233 if (bed
->s
->elfclass
== ELFCLASS32
)
11235 prstatus32_t prstat
;
11237 memset (&prstat
, 0, sizeof (prstat
));
11238 prstat
.pr_pid
= pid
;
11239 prstat
.pr_cursig
= cursig
;
11240 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11241 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11242 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11249 memset (&prstat
, 0, sizeof (prstat
));
11250 prstat
.pr_pid
= pid
;
11251 prstat
.pr_cursig
= cursig
;
11252 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11253 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11254 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11256 #endif /* HAVE_PRSTATUS_T */
11262 #if defined (HAVE_LWPSTATUS_T)
11264 elfcore_write_lwpstatus (bfd
*abfd
,
11271 lwpstatus_t lwpstat
;
11272 const char *note_name
= "CORE";
11274 memset (&lwpstat
, 0, sizeof (lwpstat
));
11275 lwpstat
.pr_lwpid
= pid
>> 16;
11276 lwpstat
.pr_cursig
= cursig
;
11277 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11278 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11279 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11280 #if !defined(gregs)
11281 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11282 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11284 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11285 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11288 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11289 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11291 #endif /* HAVE_LWPSTATUS_T */
11293 #if defined (HAVE_PSTATUS_T)
11295 elfcore_write_pstatus (bfd
*abfd
,
11299 int cursig ATTRIBUTE_UNUSED
,
11300 const void *gregs ATTRIBUTE_UNUSED
)
11302 const char *note_name
= "CORE";
11303 #if defined (HAVE_PSTATUS32_T)
11304 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11306 if (bed
->s
->elfclass
== ELFCLASS32
)
11310 memset (&pstat
, 0, sizeof (pstat
));
11311 pstat
.pr_pid
= pid
& 0xffff;
11312 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11313 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11321 memset (&pstat
, 0, sizeof (pstat
));
11322 pstat
.pr_pid
= pid
& 0xffff;
11323 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11324 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11328 #endif /* HAVE_PSTATUS_T */
11331 elfcore_write_prfpreg (bfd
*abfd
,
11334 const void *fpregs
,
11337 const char *note_name
= "CORE";
11338 return elfcore_write_note (abfd
, buf
, bufsiz
,
11339 note_name
, NT_FPREGSET
, fpregs
, size
);
11343 elfcore_write_prxfpreg (bfd
*abfd
,
11346 const void *xfpregs
,
11349 char *note_name
= "LINUX";
11350 return elfcore_write_note (abfd
, buf
, bufsiz
,
11351 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11355 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11356 const void *xfpregs
, int size
)
11359 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11360 note_name
= "FreeBSD";
11362 note_name
= "LINUX";
11363 return elfcore_write_note (abfd
, buf
, bufsiz
,
11364 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11368 elfcore_write_ppc_vmx (bfd
*abfd
,
11371 const void *ppc_vmx
,
11374 char *note_name
= "LINUX";
11375 return elfcore_write_note (abfd
, buf
, bufsiz
,
11376 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11380 elfcore_write_ppc_vsx (bfd
*abfd
,
11383 const void *ppc_vsx
,
11386 char *note_name
= "LINUX";
11387 return elfcore_write_note (abfd
, buf
, bufsiz
,
11388 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11392 elfcore_write_ppc_tar (bfd
*abfd
,
11395 const void *ppc_tar
,
11398 char *note_name
= "LINUX";
11399 return elfcore_write_note (abfd
, buf
, bufsiz
,
11400 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11404 elfcore_write_ppc_ppr (bfd
*abfd
,
11407 const void *ppc_ppr
,
11410 char *note_name
= "LINUX";
11411 return elfcore_write_note (abfd
, buf
, bufsiz
,
11412 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11416 elfcore_write_ppc_dscr (bfd
*abfd
,
11419 const void *ppc_dscr
,
11422 char *note_name
= "LINUX";
11423 return elfcore_write_note (abfd
, buf
, bufsiz
,
11424 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11428 elfcore_write_ppc_ebb (bfd
*abfd
,
11431 const void *ppc_ebb
,
11434 char *note_name
= "LINUX";
11435 return elfcore_write_note (abfd
, buf
, bufsiz
,
11436 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11440 elfcore_write_ppc_pmu (bfd
*abfd
,
11443 const void *ppc_pmu
,
11446 char *note_name
= "LINUX";
11447 return elfcore_write_note (abfd
, buf
, bufsiz
,
11448 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11452 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11455 const void *ppc_tm_cgpr
,
11458 char *note_name
= "LINUX";
11459 return elfcore_write_note (abfd
, buf
, bufsiz
,
11460 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11464 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11467 const void *ppc_tm_cfpr
,
11470 char *note_name
= "LINUX";
11471 return elfcore_write_note (abfd
, buf
, bufsiz
,
11472 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11476 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11479 const void *ppc_tm_cvmx
,
11482 char *note_name
= "LINUX";
11483 return elfcore_write_note (abfd
, buf
, bufsiz
,
11484 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11488 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11491 const void *ppc_tm_cvsx
,
11494 char *note_name
= "LINUX";
11495 return elfcore_write_note (abfd
, buf
, bufsiz
,
11496 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11500 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11503 const void *ppc_tm_spr
,
11506 char *note_name
= "LINUX";
11507 return elfcore_write_note (abfd
, buf
, bufsiz
,
11508 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11512 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11515 const void *ppc_tm_ctar
,
11518 char *note_name
= "LINUX";
11519 return elfcore_write_note (abfd
, buf
, bufsiz
,
11520 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11524 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11527 const void *ppc_tm_cppr
,
11530 char *note_name
= "LINUX";
11531 return elfcore_write_note (abfd
, buf
, bufsiz
,
11532 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11536 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11539 const void *ppc_tm_cdscr
,
11542 char *note_name
= "LINUX";
11543 return elfcore_write_note (abfd
, buf
, bufsiz
,
11544 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11548 elfcore_write_s390_high_gprs (bfd
*abfd
,
11551 const void *s390_high_gprs
,
11554 char *note_name
= "LINUX";
11555 return elfcore_write_note (abfd
, buf
, bufsiz
,
11556 note_name
, NT_S390_HIGH_GPRS
,
11557 s390_high_gprs
, size
);
11561 elfcore_write_s390_timer (bfd
*abfd
,
11564 const void *s390_timer
,
11567 char *note_name
= "LINUX";
11568 return elfcore_write_note (abfd
, buf
, bufsiz
,
11569 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11573 elfcore_write_s390_todcmp (bfd
*abfd
,
11576 const void *s390_todcmp
,
11579 char *note_name
= "LINUX";
11580 return elfcore_write_note (abfd
, buf
, bufsiz
,
11581 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11585 elfcore_write_s390_todpreg (bfd
*abfd
,
11588 const void *s390_todpreg
,
11591 char *note_name
= "LINUX";
11592 return elfcore_write_note (abfd
, buf
, bufsiz
,
11593 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11597 elfcore_write_s390_ctrs (bfd
*abfd
,
11600 const void *s390_ctrs
,
11603 char *note_name
= "LINUX";
11604 return elfcore_write_note (abfd
, buf
, bufsiz
,
11605 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11609 elfcore_write_s390_prefix (bfd
*abfd
,
11612 const void *s390_prefix
,
11615 char *note_name
= "LINUX";
11616 return elfcore_write_note (abfd
, buf
, bufsiz
,
11617 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11621 elfcore_write_s390_last_break (bfd
*abfd
,
11624 const void *s390_last_break
,
11627 char *note_name
= "LINUX";
11628 return elfcore_write_note (abfd
, buf
, bufsiz
,
11629 note_name
, NT_S390_LAST_BREAK
,
11630 s390_last_break
, size
);
11634 elfcore_write_s390_system_call (bfd
*abfd
,
11637 const void *s390_system_call
,
11640 char *note_name
= "LINUX";
11641 return elfcore_write_note (abfd
, buf
, bufsiz
,
11642 note_name
, NT_S390_SYSTEM_CALL
,
11643 s390_system_call
, size
);
11647 elfcore_write_s390_tdb (bfd
*abfd
,
11650 const void *s390_tdb
,
11653 char *note_name
= "LINUX";
11654 return elfcore_write_note (abfd
, buf
, bufsiz
,
11655 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11659 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11662 const void *s390_vxrs_low
,
11665 char *note_name
= "LINUX";
11666 return elfcore_write_note (abfd
, buf
, bufsiz
,
11667 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11671 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11674 const void *s390_vxrs_high
,
11677 char *note_name
= "LINUX";
11678 return elfcore_write_note (abfd
, buf
, bufsiz
,
11679 note_name
, NT_S390_VXRS_HIGH
,
11680 s390_vxrs_high
, size
);
11684 elfcore_write_s390_gs_cb (bfd
*abfd
,
11687 const void *s390_gs_cb
,
11690 char *note_name
= "LINUX";
11691 return elfcore_write_note (abfd
, buf
, bufsiz
,
11692 note_name
, NT_S390_GS_CB
,
11697 elfcore_write_s390_gs_bc (bfd
*abfd
,
11700 const void *s390_gs_bc
,
11703 char *note_name
= "LINUX";
11704 return elfcore_write_note (abfd
, buf
, bufsiz
,
11705 note_name
, NT_S390_GS_BC
,
11710 elfcore_write_arm_vfp (bfd
*abfd
,
11713 const void *arm_vfp
,
11716 char *note_name
= "LINUX";
11717 return elfcore_write_note (abfd
, buf
, bufsiz
,
11718 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11722 elfcore_write_aarch_tls (bfd
*abfd
,
11725 const void *aarch_tls
,
11728 char *note_name
= "LINUX";
11729 return elfcore_write_note (abfd
, buf
, bufsiz
,
11730 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11734 elfcore_write_aarch_hw_break (bfd
*abfd
,
11737 const void *aarch_hw_break
,
11740 char *note_name
= "LINUX";
11741 return elfcore_write_note (abfd
, buf
, bufsiz
,
11742 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11746 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11749 const void *aarch_hw_watch
,
11752 char *note_name
= "LINUX";
11753 return elfcore_write_note (abfd
, buf
, bufsiz
,
11754 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11758 elfcore_write_aarch_sve (bfd
*abfd
,
11761 const void *aarch_sve
,
11764 char *note_name
= "LINUX";
11765 return elfcore_write_note (abfd
, buf
, bufsiz
,
11766 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11770 elfcore_write_aarch_pauth (bfd
*abfd
,
11773 const void *aarch_pauth
,
11776 char *note_name
= "LINUX";
11777 return elfcore_write_note (abfd
, buf
, bufsiz
,
11778 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11782 elfcore_write_register_note (bfd
*abfd
,
11785 const char *section
,
11789 if (strcmp (section
, ".reg2") == 0)
11790 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11791 if (strcmp (section
, ".reg-xfp") == 0)
11792 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11793 if (strcmp (section
, ".reg-xstate") == 0)
11794 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11795 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11796 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11797 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11798 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11799 if (strcmp (section
, ".reg-ppc-tar") == 0)
11800 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11801 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11802 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11803 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11804 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11805 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11806 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11807 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11808 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11809 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11810 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11811 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11812 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11813 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11814 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11815 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11816 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11817 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11818 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11819 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11820 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11821 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11822 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11823 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11824 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11825 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11826 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11827 if (strcmp (section
, ".reg-s390-timer") == 0)
11828 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11829 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11830 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11831 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11832 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11833 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11834 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11835 if (strcmp (section
, ".reg-s390-prefix") == 0)
11836 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11837 if (strcmp (section
, ".reg-s390-last-break") == 0)
11838 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11839 if (strcmp (section
, ".reg-s390-system-call") == 0)
11840 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11841 if (strcmp (section
, ".reg-s390-tdb") == 0)
11842 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11843 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11844 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11845 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11846 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11847 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11848 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11849 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11850 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11851 if (strcmp (section
, ".reg-arm-vfp") == 0)
11852 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11853 if (strcmp (section
, ".reg-aarch-tls") == 0)
11854 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11855 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11856 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11857 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11858 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11859 if (strcmp (section
, ".reg-aarch-sve") == 0)
11860 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11861 if (strcmp (section
, ".reg-aarch-pauth") == 0)
11862 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
11867 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11872 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11873 gABI specifies that PT_NOTE alignment should be aligned to 4
11874 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11875 align is less than 4, we use 4 byte alignment. */
11878 if (align
!= 4 && align
!= 8)
11882 while (p
< buf
+ size
)
11884 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11885 Elf_Internal_Note in
;
11887 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11890 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11892 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11893 in
.namedata
= xnp
->name
;
11894 if (in
.namesz
> buf
- in
.namedata
+ size
)
11897 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11898 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11899 in
.descpos
= offset
+ (in
.descdata
- buf
);
11901 && (in
.descdata
>= buf
+ size
11902 || in
.descsz
> buf
- in
.descdata
+ size
))
11905 switch (bfd_get_format (abfd
))
11912 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11915 const char * string
;
11917 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11921 GROKER_ELEMENT ("", elfcore_grok_note
),
11922 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11923 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11924 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11925 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11926 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
11927 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
)
11929 #undef GROKER_ELEMENT
11932 for (i
= ARRAY_SIZE (grokers
); i
--;)
11934 if (in
.namesz
>= grokers
[i
].len
11935 && strncmp (in
.namedata
, grokers
[i
].string
,
11936 grokers
[i
].len
) == 0)
11938 if (! grokers
[i
].func (abfd
, & in
))
11947 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
11949 if (! elfobj_grok_gnu_note (abfd
, &in
))
11952 else if (in
.namesz
== sizeof "stapsdt"
11953 && strcmp (in
.namedata
, "stapsdt") == 0)
11955 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
11961 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
11968 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
11973 if (size
== 0 || (size
+ 1) == 0)
11976 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
11979 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
11983 /* PR 17512: file: ec08f814
11984 0-termintate the buffer so that string searches will not overflow. */
11987 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
11997 /* Providing external access to the ELF program header table. */
11999 /* Return an upper bound on the number of bytes required to store a
12000 copy of ABFD's program header table entries. Return -1 if an error
12001 occurs; bfd_get_error will return an appropriate code. */
12004 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12006 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12008 bfd_set_error (bfd_error_wrong_format
);
12012 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12015 /* Copy ABFD's program header table entries to *PHDRS. The entries
12016 will be stored as an array of Elf_Internal_Phdr structures, as
12017 defined in include/elf/internal.h. To find out how large the
12018 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12020 Return the number of program header table entries read, or -1 if an
12021 error occurs; bfd_get_error will return an appropriate code. */
12024 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12028 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12030 bfd_set_error (bfd_error_wrong_format
);
12034 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12035 if (num_phdrs
!= 0)
12036 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12037 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12042 enum elf_reloc_type_class
12043 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12044 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12045 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12047 return reloc_class_normal
;
12050 /* For RELA architectures, return the relocation value for a
12051 relocation against a local symbol. */
12054 _bfd_elf_rela_local_sym (bfd
*abfd
,
12055 Elf_Internal_Sym
*sym
,
12057 Elf_Internal_Rela
*rel
)
12059 asection
*sec
= *psec
;
12060 bfd_vma relocation
;
12062 relocation
= (sec
->output_section
->vma
12063 + sec
->output_offset
12065 if ((sec
->flags
& SEC_MERGE
)
12066 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12067 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12070 _bfd_merged_section_offset (abfd
, psec
,
12071 elf_section_data (sec
)->sec_info
,
12072 sym
->st_value
+ rel
->r_addend
);
12075 /* If we have changed the section, and our original section is
12076 marked with SEC_EXCLUDE, it means that the original
12077 SEC_MERGE section has been completely subsumed in some
12078 other SEC_MERGE section. In this case, we need to leave
12079 some info around for --emit-relocs. */
12080 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12081 sec
->kept_section
= *psec
;
12084 rel
->r_addend
-= relocation
;
12085 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12091 _bfd_elf_rel_local_sym (bfd
*abfd
,
12092 Elf_Internal_Sym
*sym
,
12096 asection
*sec
= *psec
;
12098 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12099 return sym
->st_value
+ addend
;
12101 return _bfd_merged_section_offset (abfd
, psec
,
12102 elf_section_data (sec
)->sec_info
,
12103 sym
->st_value
+ addend
);
12106 /* Adjust an address within a section. Given OFFSET within SEC, return
12107 the new offset within the section, based upon changes made to the
12108 section. Returns -1 if the offset is now invalid.
12109 The offset (in abnd out) is in target sized bytes, however big a
12113 _bfd_elf_section_offset (bfd
*abfd
,
12114 struct bfd_link_info
*info
,
12118 switch (sec
->sec_info_type
)
12120 case SEC_INFO_TYPE_STABS
:
12121 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12123 case SEC_INFO_TYPE_EH_FRAME
:
12124 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12127 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12129 /* Reverse the offset. */
12130 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12131 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12133 /* address_size and sec->size are in octets. Convert
12134 to bytes before subtracting the original offset. */
12135 offset
= ((sec
->size
- address_size
)
12136 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12142 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12143 reconstruct an ELF file by reading the segments out of remote memory
12144 based on the ELF file header at EHDR_VMA and the ELF program headers it
12145 points to. If not null, *LOADBASEP is filled in with the difference
12146 between the VMAs from which the segments were read, and the VMAs the
12147 file headers (and hence BFD's idea of each section's VMA) put them at.
12149 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12150 remote memory at target address VMA into the local buffer at MYADDR; it
12151 should return zero on success or an `errno' code on failure. TEMPL must
12152 be a BFD for an ELF target with the word size and byte order found in
12153 the remote memory. */
12156 bfd_elf_bfd_from_remote_memory
12159 bfd_size_type size
,
12160 bfd_vma
*loadbasep
,
12161 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12163 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12164 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12168 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12169 long symcount ATTRIBUTE_UNUSED
,
12170 asymbol
**syms ATTRIBUTE_UNUSED
,
12175 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12178 const char *relplt_name
;
12179 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12183 Elf_Internal_Shdr
*hdr
;
12189 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12192 if (dynsymcount
<= 0)
12195 if (!bed
->plt_sym_val
)
12198 relplt_name
= bed
->relplt_name
;
12199 if (relplt_name
== NULL
)
12200 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12201 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12202 if (relplt
== NULL
)
12205 hdr
= &elf_section_data (relplt
)->this_hdr
;
12206 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12207 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12210 plt
= bfd_get_section_by_name (abfd
, ".plt");
12214 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12215 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12218 count
= relplt
->size
/ hdr
->sh_entsize
;
12219 size
= count
* sizeof (asymbol
);
12220 p
= relplt
->relocation
;
12221 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12223 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12224 if (p
->addend
!= 0)
12227 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12229 size
+= sizeof ("+0x") - 1 + 8;
12234 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12238 names
= (char *) (s
+ count
);
12239 p
= relplt
->relocation
;
12241 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12246 addr
= bed
->plt_sym_val (i
, plt
, p
);
12247 if (addr
== (bfd_vma
) -1)
12250 *s
= **p
->sym_ptr_ptr
;
12251 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12252 we are defining a symbol, ensure one of them is set. */
12253 if ((s
->flags
& BSF_LOCAL
) == 0)
12254 s
->flags
|= BSF_GLOBAL
;
12255 s
->flags
|= BSF_SYNTHETIC
;
12257 s
->value
= addr
- plt
->vma
;
12260 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12261 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12263 if (p
->addend
!= 0)
12267 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12268 names
+= sizeof ("+0x") - 1;
12269 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12270 for (a
= buf
; *a
== '0'; ++a
)
12273 memcpy (names
, a
, len
);
12276 memcpy (names
, "@plt", sizeof ("@plt"));
12277 names
+= sizeof ("@plt");
12284 /* It is only used by x86-64 so far.
12285 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12286 but current usage would allow all of _bfd_std_section to be zero. */
12287 static const asymbol lcomm_sym
12288 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12289 asection _bfd_elf_large_com_section
12290 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12291 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12294 _bfd_elf_final_write_processing (bfd
*abfd
)
12296 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12298 i_ehdrp
= elf_elfheader (abfd
);
12300 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12301 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12303 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12304 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12305 STB_GNU_UNIQUE binding. */
12306 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12308 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12309 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12310 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12311 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12313 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12314 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12315 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12316 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12317 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12318 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12319 bfd_set_error (bfd_error_sorry
);
12327 /* Return TRUE for ELF symbol types that represent functions.
12328 This is the default version of this function, which is sufficient for
12329 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12332 _bfd_elf_is_function_type (unsigned int type
)
12334 return (type
== STT_FUNC
12335 || type
== STT_GNU_IFUNC
);
12338 /* If the ELF symbol SYM might be a function in SEC, return the
12339 function size and set *CODE_OFF to the function's entry point,
12340 otherwise return zero. */
12343 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12346 bfd_size_type size
;
12348 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12349 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12350 || sym
->section
!= sec
)
12353 *code_off
= sym
->value
;
12355 if (!(sym
->flags
& BSF_SYNTHETIC
))
12356 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
12362 /* Set to non-zero to enable some debug messages. */
12363 #define DEBUG_SECONDARY_RELOCS 0
12365 /* An internal-to-the-bfd-library only section type
12366 used to indicate a cached secondary reloc section. */
12367 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12369 /* Create a BFD section to hold a secondary reloc section. */
12372 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
12373 Elf_Internal_Shdr
*hdr
,
12375 unsigned int shindex
)
12377 /* We only support RELA secondary relocs. */
12378 if (hdr
->sh_type
!= SHT_RELA
)
12381 #if DEBUG_SECONDARY_RELOCS
12382 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
12384 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
12385 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
12388 /* Read in any secondary relocs associated with SEC. */
12391 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
12393 asymbol
** symbols
)
12395 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12397 bfd_boolean result
= TRUE
;
12398 bfd_vma (*r_sym
) (bfd_vma
);
12400 #if BFD_DEFAULT_TARGET_SIZE > 32
12401 if (bfd_arch_bits_per_address (abfd
) != 32)
12402 r_sym
= elf64_r_sym
;
12405 r_sym
= elf32_r_sym
;
12407 /* Discover if there are any secondary reloc sections
12408 associated with SEC. */
12409 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12411 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
12413 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
12414 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12416 bfd_byte
* native_relocs
;
12417 bfd_byte
* native_reloc
;
12418 arelent
* internal_relocs
;
12419 arelent
* internal_reloc
;
12421 unsigned int entsize
;
12422 unsigned int symcount
;
12423 unsigned int reloc_count
;
12426 if (ebd
->elf_info_to_howto
== NULL
)
12429 #if DEBUG_SECONDARY_RELOCS
12430 fprintf (stderr
, "read secondary relocs for %s from %s\n",
12431 sec
->name
, relsec
->name
);
12433 entsize
= hdr
->sh_entsize
;
12435 native_relocs
= bfd_malloc (hdr
->sh_size
);
12436 if (native_relocs
== NULL
)
12442 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
12443 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
12445 bfd_set_error (bfd_error_file_too_big
);
12450 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
12451 if (internal_relocs
== NULL
)
12453 free (native_relocs
);
12458 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
12459 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
12462 free (native_relocs
);
12463 free (internal_relocs
);
12468 symcount
= bfd_get_symcount (abfd
);
12470 for (i
= 0, internal_reloc
= internal_relocs
,
12471 native_reloc
= native_relocs
;
12473 i
++, internal_reloc
++, native_reloc
+= entsize
)
12476 Elf_Internal_Rela rela
;
12478 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
12480 /* The address of an ELF reloc is section relative for an object
12481 file, and absolute for an executable file or shared library.
12482 The address of a normal BFD reloc is always section relative,
12483 and the address of a dynamic reloc is absolute.. */
12484 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
12485 internal_reloc
->address
= rela
.r_offset
;
12487 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
12489 if (r_sym (rela
.r_info
) == STN_UNDEF
)
12491 /* FIXME: This and the error case below mean that we
12492 have a symbol on relocs that is not elf_symbol_type. */
12493 internal_reloc
->sym_ptr_ptr
=
12494 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12496 else if (r_sym (rela
.r_info
) > symcount
)
12499 /* xgettext:c-format */
12500 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12501 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
12502 bfd_set_error (bfd_error_bad_value
);
12503 internal_reloc
->sym_ptr_ptr
=
12504 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12511 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
12513 internal_reloc
->sym_ptr_ptr
= ps
;
12514 /* Make sure that this symbol is not removed by strip. */
12515 (*ps
)->flags
|= BSF_KEEP
;
12518 internal_reloc
->addend
= rela
.r_addend
;
12520 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
12521 if (! res
|| internal_reloc
->howto
== NULL
)
12523 #if DEBUG_SECONDARY_RELOCS
12524 fprintf (stderr
, "there is no howto associated with reloc %lx\n",
12531 free (native_relocs
);
12532 /* Store the internal relocs. */
12533 elf_section_data (relsec
)->sec_info
= internal_relocs
;
12540 /* Set the ELF section header fields of an output secondary reloc section. */
12543 _bfd_elf_copy_special_section_fields (const bfd
* ibfd ATTRIBUTE_UNUSED
,
12544 bfd
* obfd ATTRIBUTE_UNUSED
,
12545 const Elf_Internal_Shdr
* isection
,
12546 Elf_Internal_Shdr
* osection
)
12551 if (isection
== NULL
)
12554 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
12557 isec
= isection
->bfd_section
;
12561 osec
= osection
->bfd_section
;
12565 BFD_ASSERT (elf_section_data (osec
)->sec_info
== NULL
);
12566 elf_section_data (osec
)->sec_info
= elf_section_data (isec
)->sec_info
;
12567 osection
->sh_type
= SHT_RELA
;
12568 osection
->sh_link
= elf_onesymtab (obfd
);
12569 if (osection
->sh_link
== 0)
12571 /* There is no symbol table - we are hosed... */
12573 /* xgettext:c-format */
12574 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12576 bfd_set_error (bfd_error_bad_value
);
12580 /* Find the output section that corresponds to the isection's sh_info link. */
12581 BFD_ASSERT (isection
->sh_info
> 0
12582 && isection
->sh_info
< elf_numsections (ibfd
));
12583 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
12585 BFD_ASSERT (isection
!= NULL
);
12586 BFD_ASSERT (isection
->bfd_section
!= NULL
);
12587 BFD_ASSERT (isection
->bfd_section
->output_section
!= NULL
);
12588 osection
->sh_info
=
12589 elf_section_data (isection
->bfd_section
->output_section
)->this_idx
;
12591 #if DEBUG_SECONDARY_RELOCS
12592 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
12593 osec
->name
, osection
->sh_link
, osection
->sh_info
);
12599 /* Write out a secondary reloc section. */
12602 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
12604 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12605 bfd_vma addr_offset
;
12607 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
12609 #if BFD_DEFAULT_TARGET_SIZE > 32
12610 if (bfd_arch_bits_per_address (abfd
) != 32)
12611 r_info
= elf64_r_info
;
12614 r_info
= elf32_r_info
;
12619 /* The address of an ELF reloc is section relative for an object
12620 file, and absolute for an executable file or shared library.
12621 The address of a BFD reloc is always section relative. */
12623 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
12624 addr_offset
= sec
->vma
;
12626 /* Discover if there are any secondary reloc sections
12627 associated with SEC. */
12628 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12630 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
12631 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
12633 if (hdr
->sh_type
== SHT_RELA
12634 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12636 asymbol
* last_sym
;
12638 unsigned int reloc_count
;
12640 arelent
* src_irel
;
12641 bfd_byte
* dst_rela
;
12643 BFD_ASSERT (hdr
->contents
== NULL
);
12645 reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
12646 BFD_ASSERT (reloc_count
> 0);
12648 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
12649 if (hdr
->contents
== NULL
)
12652 #if DEBUG_SECONDARY_RELOCS
12653 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
12654 reloc_count
, sec
->name
, relsec
->name
);
12658 dst_rela
= hdr
->contents
;
12659 src_irel
= (arelent
*) esd
->sec_info
;
12660 BFD_ASSERT (src_irel
!= NULL
);
12662 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= hdr
->sh_entsize
)
12664 Elf_Internal_Rela src_rela
;
12669 ptr
= src_irel
+ idx
;
12670 sym
= *ptr
->sym_ptr_ptr
;
12672 if (sym
== last_sym
)
12677 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
12680 #if DEBUG_SECONDARY_RELOCS
12681 fprintf (stderr
, "failed to find symbol %s whilst rewriting relocs\n",
12684 /* FIXME: Signal failure somehow. */
12690 if ((*ptr
->sym_ptr_ptr
)->the_bfd
!= NULL
12691 && (*ptr
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
12692 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
12694 #if DEBUG_SECONDARY_RELOCS
12695 fprintf (stderr
, "symbol %s is not in the output bfd\n",
12698 /* FIXME: Signal failure somehow. */
12702 if (ptr
->howto
== NULL
)
12704 #if DEBUG_SECONDARY_RELOCS
12705 fprintf (stderr
, "reloc for symbol %s does not have a howto associated with it\n",
12708 /* FIXME: Signal failure somehow. */
12712 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
12713 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
12714 src_rela
.r_addend
= ptr
->addend
;
12715 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);