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
3537 PR 25699: A corrupt input file could contain bogus group info. */
3538 if (elf_section_syms (abfd
) == NULL
)
3543 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3545 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3547 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3549 /* The ELF backend linker sets sh_info to -2 when the group
3550 signature symbol is global, and thus the index can't be
3551 set until all local symbols are output. */
3553 struct bfd_elf_section_data
*sec_data
;
3554 unsigned long symndx
;
3555 unsigned long extsymoff
;
3556 struct elf_link_hash_entry
*h
;
3558 /* The point of this little dance to the first SHF_GROUP section
3559 then back to the SHT_GROUP section is that this gets us to
3560 the SHT_GROUP in the input object. */
3561 igroup
= elf_sec_group (elf_next_in_group (sec
));
3562 sec_data
= elf_section_data (igroup
);
3563 symndx
= sec_data
->this_hdr
.sh_info
;
3565 if (!elf_bad_symtab (igroup
->owner
))
3567 Elf_Internal_Shdr
*symtab_hdr
;
3569 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3570 extsymoff
= symtab_hdr
->sh_info
;
3572 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3573 while (h
->root
.type
== bfd_link_hash_indirect
3574 || h
->root
.type
== bfd_link_hash_warning
)
3575 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3577 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3580 /* The contents won't be allocated for "ld -r" or objcopy. */
3582 if (sec
->contents
== NULL
)
3585 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3587 /* Arrange for the section to be written out. */
3588 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3589 if (sec
->contents
== NULL
)
3596 loc
= sec
->contents
+ sec
->size
;
3598 /* Get the pointer to the first section in the group that gas
3599 squirreled away here. objcopy arranges for this to be set to the
3600 start of the input section group. */
3601 first
= elt
= elf_next_in_group (sec
);
3603 /* First element is a flag word. Rest of section is elf section
3604 indices for all the sections of the group. Write them backwards
3605 just to keep the group in the same order as given in .section
3606 directives, not that it matters. */
3613 s
= s
->output_section
;
3615 && !bfd_is_abs_section (s
))
3617 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3618 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3620 if (elf_sec
->rel
.hdr
!= NULL
3622 || (input_elf_sec
->rel
.hdr
!= NULL
3623 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3625 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3627 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3629 if (elf_sec
->rela
.hdr
!= NULL
3631 || (input_elf_sec
->rela
.hdr
!= NULL
3632 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3634 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3636 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3639 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3641 elt
= elf_next_in_group (elt
);
3647 BFD_ASSERT (loc
== sec
->contents
);
3649 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3652 /* Given NAME, the name of a relocation section stripped of its
3653 .rel/.rela prefix, return the section in ABFD to which the
3654 relocations apply. */
3657 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3659 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3660 section likely apply to .got.plt or .got section. */
3661 if (get_elf_backend_data (abfd
)->want_got_plt
3662 && strcmp (name
, ".plt") == 0)
3667 sec
= bfd_get_section_by_name (abfd
, name
);
3673 return bfd_get_section_by_name (abfd
, name
);
3676 /* Return the section to which RELOC_SEC applies. */
3679 elf_get_reloc_section (asection
*reloc_sec
)
3684 const struct elf_backend_data
*bed
;
3686 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3687 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3690 /* We look up the section the relocs apply to by name. */
3691 name
= reloc_sec
->name
;
3692 if (strncmp (name
, ".rel", 4) != 0)
3695 if (type
== SHT_RELA
&& *name
++ != 'a')
3698 abfd
= reloc_sec
->owner
;
3699 bed
= get_elf_backend_data (abfd
);
3700 return bed
->get_reloc_section (abfd
, name
);
3703 /* Assign all ELF section numbers. The dummy first section is handled here
3704 too. The link/info pointers for the standard section types are filled
3705 in here too, while we're at it. */
3708 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3710 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3712 unsigned int section_number
;
3713 Elf_Internal_Shdr
**i_shdrp
;
3714 struct bfd_elf_section_data
*d
;
3715 bfd_boolean need_symtab
;
3720 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3722 /* SHT_GROUP sections are in relocatable files only. */
3723 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3725 size_t reloc_count
= 0;
3727 /* Put SHT_GROUP sections first. */
3728 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3730 d
= elf_section_data (sec
);
3732 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3734 if (sec
->flags
& SEC_LINKER_CREATED
)
3736 /* Remove the linker created SHT_GROUP sections. */
3737 bfd_section_list_remove (abfd
, sec
);
3738 abfd
->section_count
--;
3741 d
->this_idx
= section_number
++;
3744 /* Count relocations. */
3745 reloc_count
+= sec
->reloc_count
;
3748 /* Clear HAS_RELOC if there are no relocations. */
3749 if (reloc_count
== 0)
3750 abfd
->flags
&= ~HAS_RELOC
;
3753 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3755 d
= elf_section_data (sec
);
3757 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3758 d
->this_idx
= section_number
++;
3759 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3760 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3763 d
->rel
.idx
= section_number
++;
3764 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3765 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3772 d
->rela
.idx
= section_number
++;
3773 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3774 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3780 need_symtab
= (bfd_get_symcount (abfd
) > 0
3781 || (link_info
== NULL
3782 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3786 elf_onesymtab (abfd
) = section_number
++;
3787 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3788 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3790 elf_section_list
*entry
;
3792 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3794 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3795 entry
->ndx
= section_number
++;
3796 elf_symtab_shndx_list (abfd
) = entry
;
3798 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3799 ".symtab_shndx", FALSE
);
3800 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3803 elf_strtab_sec (abfd
) = section_number
++;
3804 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3807 elf_shstrtab_sec (abfd
) = section_number
++;
3808 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3809 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3811 if (section_number
>= SHN_LORESERVE
)
3813 /* xgettext:c-format */
3814 _bfd_error_handler (_("%pB: too many sections: %u"),
3815 abfd
, section_number
);
3819 elf_numsections (abfd
) = section_number
;
3820 elf_elfheader (abfd
)->e_shnum
= section_number
;
3822 /* Set up the list of section header pointers, in agreement with the
3824 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3825 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3826 if (i_shdrp
== NULL
)
3829 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3830 sizeof (Elf_Internal_Shdr
));
3831 if (i_shdrp
[0] == NULL
)
3833 bfd_release (abfd
, i_shdrp
);
3837 elf_elfsections (abfd
) = i_shdrp
;
3839 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3842 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3843 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3845 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3846 BFD_ASSERT (entry
!= NULL
);
3847 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3848 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3850 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3851 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3854 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3858 d
= elf_section_data (sec
);
3860 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3861 if (d
->rel
.idx
!= 0)
3862 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3863 if (d
->rela
.idx
!= 0)
3864 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3866 /* Fill in the sh_link and sh_info fields while we're at it. */
3868 /* sh_link of a reloc section is the section index of the symbol
3869 table. sh_info is the section index of the section to which
3870 the relocation entries apply. */
3871 if (d
->rel
.idx
!= 0)
3873 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3874 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3875 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3877 if (d
->rela
.idx
!= 0)
3879 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3880 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3881 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3884 /* We need to set up sh_link for SHF_LINK_ORDER. */
3885 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3887 s
= elf_linked_to_section (sec
);
3890 /* elf_linked_to_section points to the input section. */
3891 if (link_info
!= NULL
)
3893 /* Check discarded linkonce section. */
3894 if (discarded_section (s
))
3898 /* xgettext:c-format */
3899 (_("%pB: sh_link of section `%pA' points to"
3900 " discarded section `%pA' of `%pB'"),
3901 abfd
, d
->this_hdr
.bfd_section
,
3903 /* Point to the kept section if it has the same
3904 size as the discarded one. */
3905 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3908 bfd_set_error (bfd_error_bad_value
);
3914 s
= s
->output_section
;
3915 BFD_ASSERT (s
!= NULL
);
3919 /* Handle objcopy. */
3920 if (s
->output_section
== NULL
)
3923 /* xgettext:c-format */
3924 (_("%pB: sh_link of section `%pA' points to"
3925 " removed section `%pA' of `%pB'"),
3926 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3927 bfd_set_error (bfd_error_bad_value
);
3930 s
= s
->output_section
;
3932 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3937 The Intel C compiler generates SHT_IA_64_UNWIND with
3938 SHF_LINK_ORDER. But it doesn't set the sh_link or
3939 sh_info fields. Hence we could get the situation
3941 const struct elf_backend_data
*bed
3942 = get_elf_backend_data (abfd
);
3943 if (bed
->link_order_error_handler
)
3944 bed
->link_order_error_handler
3945 /* xgettext:c-format */
3946 (_("%pB: warning: sh_link not set for section `%pA'"),
3951 switch (d
->this_hdr
.sh_type
)
3955 /* A reloc section which we are treating as a normal BFD
3956 section. sh_link is the section index of the symbol
3957 table. sh_info is the section index of the section to
3958 which the relocation entries apply. We assume that an
3959 allocated reloc section uses the dynamic symbol table.
3960 FIXME: How can we be sure? */
3961 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3963 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3965 s
= elf_get_reloc_section (sec
);
3968 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3969 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3974 /* We assume that a section named .stab*str is a stabs
3975 string section. We look for a section with the same name
3976 but without the trailing ``str'', and set its sh_link
3977 field to point to this section. */
3978 if (CONST_STRNEQ (sec
->name
, ".stab")
3979 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3984 len
= strlen (sec
->name
);
3985 alc
= (char *) bfd_malloc (len
- 2);
3988 memcpy (alc
, sec
->name
, len
- 3);
3989 alc
[len
- 3] = '\0';
3990 s
= bfd_get_section_by_name (abfd
, alc
);
3994 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3996 /* This is a .stab section. */
3997 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3998 elf_section_data (s
)->this_hdr
.sh_entsize
3999 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
4006 case SHT_GNU_verneed
:
4007 case SHT_GNU_verdef
:
4008 /* sh_link is the section header index of the string table
4009 used for the dynamic entries, or the symbol table, or the
4011 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4013 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4016 case SHT_GNU_LIBLIST
:
4017 /* sh_link is the section header index of the prelink library
4018 list used for the dynamic entries, or the symbol table, or
4019 the version strings. */
4020 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4021 ? ".dynstr" : ".gnu.libstr");
4023 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4028 case SHT_GNU_versym
:
4029 /* sh_link is the section header index of the symbol table
4030 this hash table or version table is for. */
4031 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4033 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4037 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4041 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4042 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4043 debug section name from .debug_* to .zdebug_* if needed. */
4049 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4051 /* If the backend has a special mapping, use it. */
4052 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4053 if (bed
->elf_backend_sym_is_global
)
4054 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4056 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4057 || bfd_is_und_section (bfd_asymbol_section (sym
))
4058 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4061 /* Filter global symbols of ABFD to include in the import library. All
4062 SYMCOUNT symbols of ABFD can be examined from their pointers in
4063 SYMS. Pointers of symbols to keep should be stored contiguously at
4064 the beginning of that array.
4066 Returns the number of symbols to keep. */
4069 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4070 asymbol
**syms
, long symcount
)
4072 long src_count
, dst_count
= 0;
4074 for (src_count
= 0; src_count
< symcount
; src_count
++)
4076 asymbol
*sym
= syms
[src_count
];
4077 char *name
= (char *) bfd_asymbol_name (sym
);
4078 struct bfd_link_hash_entry
*h
;
4080 if (!sym_is_global (abfd
, sym
))
4083 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4086 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4088 if (h
->linker_def
|| h
->ldscript_def
)
4091 syms
[dst_count
++] = sym
;
4094 syms
[dst_count
] = NULL
;
4099 /* Don't output section symbols for sections that are not going to be
4100 output, that are duplicates or there is no BFD section. */
4103 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4105 elf_symbol_type
*type_ptr
;
4110 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4113 if (sym
->section
== NULL
)
4116 type_ptr
= elf_symbol_from (abfd
, sym
);
4117 return ((type_ptr
!= NULL
4118 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4119 && bfd_is_abs_section (sym
->section
))
4120 || !(sym
->section
->owner
== abfd
4121 || (sym
->section
->output_section
!= NULL
4122 && sym
->section
->output_section
->owner
== abfd
4123 && sym
->section
->output_offset
== 0)
4124 || bfd_is_abs_section (sym
->section
)));
4127 /* Map symbol from it's internal number to the external number, moving
4128 all local symbols to be at the head of the list. */
4131 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4133 unsigned int symcount
= bfd_get_symcount (abfd
);
4134 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4135 asymbol
**sect_syms
;
4136 unsigned int num_locals
= 0;
4137 unsigned int num_globals
= 0;
4138 unsigned int num_locals2
= 0;
4139 unsigned int num_globals2
= 0;
4140 unsigned int max_index
= 0;
4147 fprintf (stderr
, "elf_map_symbols\n");
4151 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4153 if (max_index
< asect
->index
)
4154 max_index
= asect
->index
;
4158 amt
= max_index
* sizeof (asymbol
*);
4159 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4160 if (sect_syms
== NULL
)
4162 elf_section_syms (abfd
) = sect_syms
;
4163 elf_num_section_syms (abfd
) = max_index
;
4165 /* Init sect_syms entries for any section symbols we have already
4166 decided to output. */
4167 for (idx
= 0; idx
< symcount
; idx
++)
4169 asymbol
*sym
= syms
[idx
];
4171 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4173 && !ignore_section_sym (abfd
, sym
)
4174 && !bfd_is_abs_section (sym
->section
))
4176 asection
*sec
= sym
->section
;
4178 if (sec
->owner
!= abfd
)
4179 sec
= sec
->output_section
;
4181 sect_syms
[sec
->index
] = syms
[idx
];
4185 /* Classify all of the symbols. */
4186 for (idx
= 0; idx
< symcount
; idx
++)
4188 if (sym_is_global (abfd
, syms
[idx
]))
4190 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4194 /* We will be adding a section symbol for each normal BFD section. Most
4195 sections will already have a section symbol in outsymbols, but
4196 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4197 at least in that case. */
4198 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4200 if (sect_syms
[asect
->index
] == NULL
)
4202 if (!sym_is_global (abfd
, asect
->symbol
))
4209 /* Now sort the symbols so the local symbols are first. */
4210 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4211 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4212 if (new_syms
== NULL
)
4215 for (idx
= 0; idx
< symcount
; idx
++)
4217 asymbol
*sym
= syms
[idx
];
4220 if (sym_is_global (abfd
, sym
))
4221 i
= num_locals
+ num_globals2
++;
4222 else if (!ignore_section_sym (abfd
, sym
))
4227 sym
->udata
.i
= i
+ 1;
4229 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4231 if (sect_syms
[asect
->index
] == NULL
)
4233 asymbol
*sym
= asect
->symbol
;
4236 sect_syms
[asect
->index
] = sym
;
4237 if (!sym_is_global (abfd
, sym
))
4240 i
= num_locals
+ num_globals2
++;
4242 sym
->udata
.i
= i
+ 1;
4246 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4248 *pnum_locals
= num_locals
;
4252 /* Align to the maximum file alignment that could be required for any
4253 ELF data structure. */
4255 static inline file_ptr
4256 align_file_position (file_ptr off
, int align
)
4258 return (off
+ align
- 1) & ~(align
- 1);
4261 /* Assign a file position to a section, optionally aligning to the
4262 required section alignment. */
4265 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4269 if (align
&& i_shdrp
->sh_addralign
> 1)
4270 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4271 i_shdrp
->sh_offset
= offset
;
4272 if (i_shdrp
->bfd_section
!= NULL
)
4273 i_shdrp
->bfd_section
->filepos
= offset
;
4274 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4275 offset
+= i_shdrp
->sh_size
;
4279 /* Compute the file positions we are going to put the sections at, and
4280 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4281 is not NULL, this is being called by the ELF backend linker. */
4284 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4285 struct bfd_link_info
*link_info
)
4287 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4288 struct fake_section_arg fsargs
;
4290 struct elf_strtab_hash
*strtab
= NULL
;
4291 Elf_Internal_Shdr
*shstrtab_hdr
;
4292 bfd_boolean need_symtab
;
4294 if (abfd
->output_has_begun
)
4297 /* Do any elf backend specific processing first. */
4298 if (bed
->elf_backend_begin_write_processing
)
4299 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4301 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4304 fsargs
.failed
= FALSE
;
4305 fsargs
.link_info
= link_info
;
4306 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4310 if (!assign_section_numbers (abfd
, link_info
))
4313 /* The backend linker builds symbol table information itself. */
4314 need_symtab
= (link_info
== NULL
4315 && (bfd_get_symcount (abfd
) > 0
4316 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4320 /* Non-zero if doing a relocatable link. */
4321 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4323 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4328 if (link_info
== NULL
)
4330 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4335 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4336 /* sh_name was set in init_file_header. */
4337 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4338 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4339 shstrtab_hdr
->sh_addr
= 0;
4340 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4341 shstrtab_hdr
->sh_entsize
= 0;
4342 shstrtab_hdr
->sh_link
= 0;
4343 shstrtab_hdr
->sh_info
= 0;
4344 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4345 shstrtab_hdr
->sh_addralign
= 1;
4347 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4353 Elf_Internal_Shdr
*hdr
;
4355 off
= elf_next_file_pos (abfd
);
4357 hdr
= & elf_symtab_hdr (abfd
);
4358 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4360 if (elf_symtab_shndx_list (abfd
) != NULL
)
4362 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4363 if (hdr
->sh_size
!= 0)
4364 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4365 /* FIXME: What about other symtab_shndx sections in the list ? */
4368 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4369 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4371 elf_next_file_pos (abfd
) = off
;
4373 /* Now that we know where the .strtab section goes, write it
4375 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4376 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4378 _bfd_elf_strtab_free (strtab
);
4381 abfd
->output_has_begun
= TRUE
;
4386 /* Make an initial estimate of the size of the program header. If we
4387 get the number wrong here, we'll redo section placement. */
4389 static bfd_size_type
4390 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4394 const struct elf_backend_data
*bed
;
4396 /* Assume we will need exactly two PT_LOAD segments: one for text
4397 and one for data. */
4400 s
= bfd_get_section_by_name (abfd
, ".interp");
4401 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4403 /* If we have a loadable interpreter section, we need a
4404 PT_INTERP segment. In this case, assume we also need a
4405 PT_PHDR segment, although that may not be true for all
4410 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4412 /* We need a PT_DYNAMIC segment. */
4416 if (info
!= NULL
&& info
->relro
)
4418 /* We need a PT_GNU_RELRO segment. */
4422 if (elf_eh_frame_hdr (abfd
))
4424 /* We need a PT_GNU_EH_FRAME segment. */
4428 if (elf_stack_flags (abfd
))
4430 /* We need a PT_GNU_STACK segment. */
4434 s
= bfd_get_section_by_name (abfd
,
4435 NOTE_GNU_PROPERTY_SECTION_NAME
);
4436 if (s
!= NULL
&& s
->size
!= 0)
4438 /* We need a PT_GNU_PROPERTY segment. */
4442 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4444 if ((s
->flags
& SEC_LOAD
) != 0
4445 && elf_section_type (s
) == SHT_NOTE
)
4447 unsigned int alignment_power
;
4448 /* We need a PT_NOTE segment. */
4450 /* Try to create just one PT_NOTE segment for all adjacent
4451 loadable SHT_NOTE sections. gABI requires that within a
4452 PT_NOTE segment (and also inside of each SHT_NOTE section)
4453 each note should have the same alignment. So we check
4454 whether the sections are correctly aligned. */
4455 alignment_power
= s
->alignment_power
;
4456 while (s
->next
!= NULL
4457 && s
->next
->alignment_power
== alignment_power
4458 && (s
->next
->flags
& SEC_LOAD
) != 0
4459 && elf_section_type (s
->next
) == SHT_NOTE
)
4464 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4466 if (s
->flags
& SEC_THREAD_LOCAL
)
4468 /* We need a PT_TLS segment. */
4474 bed
= get_elf_backend_data (abfd
);
4476 if ((abfd
->flags
& D_PAGED
) != 0
4477 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4479 /* Add a PT_GNU_MBIND segment for each mbind section. */
4480 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4481 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4482 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4484 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4487 /* xgettext:c-format */
4488 (_("%pB: GNU_MBIND section `%pA' has invalid "
4489 "sh_info field: %d"),
4490 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4493 /* Align mbind section to page size. */
4494 if (s
->alignment_power
< page_align_power
)
4495 s
->alignment_power
= page_align_power
;
4500 /* Let the backend count up any program headers it might need. */
4501 if (bed
->elf_backend_additional_program_headers
)
4505 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4511 return segs
* bed
->s
->sizeof_phdr
;
4514 /* Find the segment that contains the output_section of section. */
4517 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4519 struct elf_segment_map
*m
;
4520 Elf_Internal_Phdr
*p
;
4522 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4528 for (i
= m
->count
- 1; i
>= 0; i
--)
4529 if (m
->sections
[i
] == section
)
4536 /* Create a mapping from a set of sections to a program segment. */
4538 static struct elf_segment_map
*
4539 make_mapping (bfd
*abfd
,
4540 asection
**sections
,
4545 struct elf_segment_map
*m
;
4550 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4551 amt
+= (to
- from
) * sizeof (asection
*);
4552 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4556 m
->p_type
= PT_LOAD
;
4557 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4558 m
->sections
[i
- from
] = *hdrpp
;
4559 m
->count
= to
- from
;
4561 if (from
== 0 && phdr
)
4563 /* Include the headers in the first PT_LOAD segment. */
4564 m
->includes_filehdr
= 1;
4565 m
->includes_phdrs
= 1;
4571 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4574 struct elf_segment_map
*
4575 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4577 struct elf_segment_map
*m
;
4579 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4580 sizeof (struct elf_segment_map
));
4584 m
->p_type
= PT_DYNAMIC
;
4586 m
->sections
[0] = dynsec
;
4591 /* Possibly add or remove segments from the segment map. */
4594 elf_modify_segment_map (bfd
*abfd
,
4595 struct bfd_link_info
*info
,
4596 bfd_boolean remove_empty_load
)
4598 struct elf_segment_map
**m
;
4599 const struct elf_backend_data
*bed
;
4601 /* The placement algorithm assumes that non allocated sections are
4602 not in PT_LOAD segments. We ensure this here by removing such
4603 sections from the segment map. We also remove excluded
4604 sections. Finally, any PT_LOAD segment without sections is
4606 m
= &elf_seg_map (abfd
);
4609 unsigned int i
, new_count
;
4611 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4613 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4614 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4615 || (*m
)->p_type
!= PT_LOAD
))
4617 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4621 (*m
)->count
= new_count
;
4623 if (remove_empty_load
4624 && (*m
)->p_type
== PT_LOAD
4626 && !(*m
)->includes_phdrs
)
4632 bed
= get_elf_backend_data (abfd
);
4633 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4635 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4642 #define IS_TBSS(s) \
4643 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4645 /* Set up a mapping from BFD sections to program segments. */
4648 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4651 struct elf_segment_map
*m
;
4652 asection
**sections
= NULL
;
4653 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4654 bfd_boolean no_user_phdrs
;
4656 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4659 info
->user_phdrs
= !no_user_phdrs
;
4661 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4665 struct elf_segment_map
*mfirst
;
4666 struct elf_segment_map
**pm
;
4669 unsigned int hdr_index
;
4670 bfd_vma maxpagesize
;
4672 bfd_boolean phdr_in_segment
;
4673 bfd_boolean writable
;
4674 bfd_boolean executable
;
4675 unsigned int tls_count
= 0;
4676 asection
*first_tls
= NULL
;
4677 asection
*first_mbind
= NULL
;
4678 asection
*dynsec
, *eh_frame_hdr
;
4680 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4681 bfd_size_type phdr_size
; /* Octets/bytes. */
4682 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4684 /* Select the allocated sections, and sort them. */
4686 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4687 sections
= (asection
**) bfd_malloc (amt
);
4688 if (sections
== NULL
)
4691 /* Calculate top address, avoiding undefined behaviour of shift
4692 left operator when shift count is equal to size of type
4694 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4695 addr_mask
= (addr_mask
<< 1) + 1;
4698 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4700 if ((s
->flags
& SEC_ALLOC
) != 0)
4702 /* target_index is unused until bfd_elf_final_link
4703 starts output of section symbols. Use it to make
4705 s
->target_index
= i
;
4708 /* A wrapping section potentially clashes with header. */
4709 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4710 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4713 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4716 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4718 phdr_size
= elf_program_header_size (abfd
);
4719 if (phdr_size
== (bfd_size_type
) -1)
4720 phdr_size
= get_program_header_size (abfd
, info
);
4721 phdr_size
+= bed
->s
->sizeof_ehdr
;
4722 /* phdr_size is compared to LMA values which are in bytes. */
4724 maxpagesize
= bed
->maxpagesize
;
4725 if (maxpagesize
== 0)
4727 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4729 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4730 >= (phdr_size
& (maxpagesize
- 1))))
4731 /* For compatibility with old scripts that may not be using
4732 SIZEOF_HEADERS, add headers when it looks like space has
4733 been left for them. */
4734 phdr_in_segment
= TRUE
;
4736 /* Build the mapping. */
4740 /* If we have a .interp section, then create a PT_PHDR segment for
4741 the program headers and a PT_INTERP segment for the .interp
4743 s
= bfd_get_section_by_name (abfd
, ".interp");
4744 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4746 amt
= sizeof (struct elf_segment_map
);
4747 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4751 m
->p_type
= PT_PHDR
;
4753 m
->p_flags_valid
= 1;
4754 m
->includes_phdrs
= 1;
4755 phdr_in_segment
= TRUE
;
4759 amt
= sizeof (struct elf_segment_map
);
4760 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4764 m
->p_type
= PT_INTERP
;
4772 /* Look through the sections. We put sections in the same program
4773 segment when the start of the second section can be placed within
4774 a few bytes of the end of the first section. */
4780 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4782 && (dynsec
->flags
& SEC_LOAD
) == 0)
4785 if ((abfd
->flags
& D_PAGED
) == 0)
4786 phdr_in_segment
= FALSE
;
4788 /* Deal with -Ttext or something similar such that the first section
4789 is not adjacent to the program headers. This is an
4790 approximation, since at this point we don't know exactly how many
4791 program headers we will need. */
4792 if (phdr_in_segment
&& count
> 0)
4794 bfd_vma phdr_lma
; /* Bytes. */
4795 bfd_boolean separate_phdr
= FALSE
;
4797 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4799 && info
->separate_code
4800 && (sections
[0]->flags
& SEC_CODE
) != 0)
4802 /* If data sections should be separate from code and
4803 thus not executable, and the first section is
4804 executable then put the file and program headers in
4805 their own PT_LOAD. */
4806 separate_phdr
= TRUE
;
4807 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4808 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4810 /* The file and program headers are currently on the
4811 same page as the first section. Put them on the
4812 previous page if we can. */
4813 if (phdr_lma
>= maxpagesize
)
4814 phdr_lma
-= maxpagesize
;
4816 separate_phdr
= FALSE
;
4819 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4820 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4821 /* If file and program headers would be placed at the end
4822 of memory then it's probably better to omit them. */
4823 phdr_in_segment
= FALSE
;
4824 else if (phdr_lma
< wrap_to
)
4825 /* If a section wraps around to where we'll be placing
4826 file and program headers, then the headers will be
4828 phdr_in_segment
= FALSE
;
4829 else if (separate_phdr
)
4831 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4834 m
->p_paddr
= phdr_lma
* opb
;
4836 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4837 m
->p_paddr_valid
= 1;
4840 phdr_in_segment
= FALSE
;
4844 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4847 bfd_boolean new_segment
;
4851 /* See if this section and the last one will fit in the same
4854 if (last_hdr
== NULL
)
4856 /* If we don't have a segment yet, then we don't need a new
4857 one (we build the last one after this loop). */
4858 new_segment
= FALSE
;
4860 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4862 /* If this section has a different relation between the
4863 virtual address and the load address, then we need a new
4867 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4868 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4870 /* If this section has a load address that makes it overlap
4871 the previous section, then we need a new segment. */
4874 else if ((abfd
->flags
& D_PAGED
) != 0
4875 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4876 == (hdr
->lma
& -maxpagesize
)))
4878 /* If we are demand paged then we can't map two disk
4879 pages onto the same memory page. */
4880 new_segment
= FALSE
;
4882 /* In the next test we have to be careful when last_hdr->lma is close
4883 to the end of the address space. If the aligned address wraps
4884 around to the start of the address space, then there are no more
4885 pages left in memory and it is OK to assume that the current
4886 section can be included in the current segment. */
4887 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4888 + maxpagesize
> last_hdr
->lma
)
4889 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4890 + maxpagesize
<= hdr
->lma
))
4892 /* If putting this section in this segment would force us to
4893 skip a page in the segment, then we need a new segment. */
4896 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4897 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4899 /* We don't want to put a loaded section after a
4900 nonloaded (ie. bss style) section in the same segment
4901 as that will force the non-loaded section to be loaded.
4902 Consider .tbss sections as loaded for this purpose. */
4905 else if ((abfd
->flags
& D_PAGED
) == 0)
4907 /* If the file is not demand paged, which means that we
4908 don't require the sections to be correctly aligned in the
4909 file, then there is no other reason for a new segment. */
4910 new_segment
= FALSE
;
4912 else if (info
!= NULL
4913 && info
->separate_code
4914 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4919 && (hdr
->flags
& SEC_READONLY
) == 0)
4921 /* We don't want to put a writable section in a read only
4927 /* Otherwise, we can use the same segment. */
4928 new_segment
= FALSE
;
4931 /* Allow interested parties a chance to override our decision. */
4932 if (last_hdr
!= NULL
4934 && info
->callbacks
->override_segment_assignment
!= NULL
)
4936 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4942 if ((hdr
->flags
& SEC_READONLY
) == 0)
4944 if ((hdr
->flags
& SEC_CODE
) != 0)
4947 /* .tbss sections effectively have zero size. */
4948 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4952 /* We need a new program segment. We must create a new program
4953 header holding all the sections from hdr_index until hdr. */
4955 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4962 if ((hdr
->flags
& SEC_READONLY
) == 0)
4967 if ((hdr
->flags
& SEC_CODE
) == 0)
4973 /* .tbss sections effectively have zero size. */
4974 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4976 phdr_in_segment
= FALSE
;
4979 /* Create a final PT_LOAD program segment, but not if it's just
4981 if (last_hdr
!= NULL
4982 && (i
- hdr_index
!= 1
4983 || !IS_TBSS (last_hdr
)))
4985 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4993 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4996 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5003 /* For each batch of consecutive loadable SHT_NOTE sections,
5004 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5005 because if we link together nonloadable .note sections and
5006 loadable .note sections, we will generate two .note sections
5007 in the output file. */
5008 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5010 if ((s
->flags
& SEC_LOAD
) != 0
5011 && elf_section_type (s
) == SHT_NOTE
)
5014 unsigned int alignment_power
= s
->alignment_power
;
5017 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5019 if (s2
->next
->alignment_power
== alignment_power
5020 && (s2
->next
->flags
& SEC_LOAD
) != 0
5021 && elf_section_type (s2
->next
) == SHT_NOTE
5022 && align_power (s2
->lma
+ s2
->size
/ opb
,
5029 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5030 amt
+= count
* sizeof (asection
*);
5031 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5035 m
->p_type
= PT_NOTE
;
5039 m
->sections
[m
->count
- count
--] = s
;
5040 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5043 m
->sections
[m
->count
- 1] = s
;
5044 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5048 if (s
->flags
& SEC_THREAD_LOCAL
)
5054 if (first_mbind
== NULL
5055 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5059 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5062 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5063 amt
+= tls_count
* sizeof (asection
*);
5064 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5069 m
->count
= tls_count
;
5070 /* Mandated PF_R. */
5072 m
->p_flags_valid
= 1;
5074 for (i
= 0; i
< tls_count
; ++i
)
5076 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5079 (_("%pB: TLS sections are not adjacent:"), abfd
);
5082 while (i
< tls_count
)
5084 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5086 _bfd_error_handler (_(" TLS: %pA"), s
);
5090 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5093 bfd_set_error (bfd_error_bad_value
);
5105 && (abfd
->flags
& D_PAGED
) != 0
5106 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5107 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5108 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5109 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5111 /* Mandated PF_R. */
5112 unsigned long p_flags
= PF_R
;
5113 if ((s
->flags
& SEC_READONLY
) == 0)
5115 if ((s
->flags
& SEC_CODE
) != 0)
5118 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5119 m
= bfd_zalloc (abfd
, amt
);
5123 m
->p_type
= (PT_GNU_MBIND_LO
5124 + elf_section_data (s
)->this_hdr
.sh_info
);
5126 m
->p_flags_valid
= 1;
5128 m
->p_flags
= p_flags
;
5134 s
= bfd_get_section_by_name (abfd
,
5135 NOTE_GNU_PROPERTY_SECTION_NAME
);
5136 if (s
!= NULL
&& s
->size
!= 0)
5138 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5139 m
= bfd_zalloc (abfd
, amt
);
5143 m
->p_type
= PT_GNU_PROPERTY
;
5145 m
->p_flags_valid
= 1;
5152 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5154 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5155 if (eh_frame_hdr
!= NULL
5156 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5158 amt
= sizeof (struct elf_segment_map
);
5159 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5163 m
->p_type
= PT_GNU_EH_FRAME
;
5165 m
->sections
[0] = eh_frame_hdr
->output_section
;
5171 if (elf_stack_flags (abfd
))
5173 amt
= sizeof (struct elf_segment_map
);
5174 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5178 m
->p_type
= PT_GNU_STACK
;
5179 m
->p_flags
= elf_stack_flags (abfd
);
5180 m
->p_align
= bed
->stack_align
;
5181 m
->p_flags_valid
= 1;
5182 m
->p_align_valid
= m
->p_align
!= 0;
5183 if (info
->stacksize
> 0)
5185 m
->p_size
= info
->stacksize
;
5186 m
->p_size_valid
= 1;
5193 if (info
!= NULL
&& info
->relro
)
5195 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5197 if (m
->p_type
== PT_LOAD
5199 && m
->sections
[0]->vma
>= info
->relro_start
5200 && m
->sections
[0]->vma
< info
->relro_end
)
5203 while (--i
!= (unsigned) -1)
5204 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5205 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5208 if (i
!= (unsigned) -1)
5213 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5216 amt
= sizeof (struct elf_segment_map
);
5217 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5221 m
->p_type
= PT_GNU_RELRO
;
5228 elf_seg_map (abfd
) = mfirst
;
5231 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5234 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5236 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5241 if (sections
!= NULL
)
5246 /* Sort sections by address. */
5249 elf_sort_sections (const void *arg1
, const void *arg2
)
5251 const asection
*sec1
= *(const asection
**) arg1
;
5252 const asection
*sec2
= *(const asection
**) arg2
;
5253 bfd_size_type size1
, size2
;
5255 /* Sort by LMA first, since this is the address used to
5256 place the section into a segment. */
5257 if (sec1
->lma
< sec2
->lma
)
5259 else if (sec1
->lma
> sec2
->lma
)
5262 /* Then sort by VMA. Normally the LMA and the VMA will be
5263 the same, and this will do nothing. */
5264 if (sec1
->vma
< sec2
->vma
)
5266 else if (sec1
->vma
> sec2
->vma
)
5269 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5271 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5278 else if (TOEND (sec2
))
5283 /* Sort by size, to put zero sized sections
5284 before others at the same address. */
5286 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5287 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5294 return sec1
->target_index
- sec2
->target_index
;
5297 /* This qsort comparison functions sorts PT_LOAD segments first and
5298 by p_paddr, for assign_file_positions_for_load_sections. */
5301 elf_sort_segments (const void *arg1
, const void *arg2
)
5303 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5304 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5306 if (m1
->p_type
!= m2
->p_type
)
5308 if (m1
->p_type
== PT_NULL
)
5310 if (m2
->p_type
== PT_NULL
)
5312 return m1
->p_type
< m2
->p_type
? -1 : 1;
5314 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5315 return m1
->includes_filehdr
? -1 : 1;
5316 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5317 return m1
->no_sort_lma
? -1 : 1;
5318 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5320 bfd_vma lma1
, lma2
; /* Octets. */
5322 if (m1
->p_paddr_valid
)
5324 else if (m1
->count
!= 0)
5326 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5328 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5331 if (m2
->p_paddr_valid
)
5333 else if (m2
->count
!= 0)
5335 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5337 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5340 return lma1
< lma2
? -1 : 1;
5342 if (m1
->idx
!= m2
->idx
)
5343 return m1
->idx
< m2
->idx
? -1 : 1;
5347 /* Ian Lance Taylor writes:
5349 We shouldn't be using % with a negative signed number. That's just
5350 not good. We have to make sure either that the number is not
5351 negative, or that the number has an unsigned type. When the types
5352 are all the same size they wind up as unsigned. When file_ptr is a
5353 larger signed type, the arithmetic winds up as signed long long,
5356 What we're trying to say here is something like ``increase OFF by
5357 the least amount that will cause it to be equal to the VMA modulo
5359 /* In other words, something like:
5361 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5362 off_offset = off % bed->maxpagesize;
5363 if (vma_offset < off_offset)
5364 adjustment = vma_offset + bed->maxpagesize - off_offset;
5366 adjustment = vma_offset - off_offset;
5368 which can be collapsed into the expression below. */
5371 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5373 /* PR binutils/16199: Handle an alignment of zero. */
5374 if (maxpagesize
== 0)
5376 return ((vma
- off
) % maxpagesize
);
5380 print_segment_map (const struct elf_segment_map
*m
)
5383 const char *pt
= get_segment_type (m
->p_type
);
5388 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5389 sprintf (buf
, "LOPROC+%7.7x",
5390 (unsigned int) (m
->p_type
- PT_LOPROC
));
5391 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5392 sprintf (buf
, "LOOS+%7.7x",
5393 (unsigned int) (m
->p_type
- PT_LOOS
));
5395 snprintf (buf
, sizeof (buf
), "%8.8x",
5396 (unsigned int) m
->p_type
);
5400 fprintf (stderr
, "%s:", pt
);
5401 for (j
= 0; j
< m
->count
; j
++)
5402 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5408 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5413 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5415 buf
= bfd_zmalloc (len
);
5418 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5423 /* Assign file positions to the sections based on the mapping from
5424 sections to segments. This function also sets up some fields in
5428 assign_file_positions_for_load_sections (bfd
*abfd
,
5429 struct bfd_link_info
*link_info
)
5431 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5432 struct elf_segment_map
*m
;
5433 struct elf_segment_map
*phdr_load_seg
;
5434 Elf_Internal_Phdr
*phdrs
;
5435 Elf_Internal_Phdr
*p
;
5436 file_ptr off
; /* Octets. */
5437 bfd_size_type maxpagesize
;
5438 unsigned int alloc
, actual
;
5440 struct elf_segment_map
**sorted_seg_map
;
5441 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5443 if (link_info
== NULL
5444 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5448 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5453 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5454 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5458 /* PR binutils/12467. */
5459 elf_elfheader (abfd
)->e_phoff
= 0;
5460 elf_elfheader (abfd
)->e_phentsize
= 0;
5463 elf_elfheader (abfd
)->e_phnum
= alloc
;
5465 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5468 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5472 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5473 BFD_ASSERT (elf_program_header_size (abfd
)
5474 == actual
* bed
->s
->sizeof_phdr
);
5475 BFD_ASSERT (actual
>= alloc
);
5480 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5484 /* We're writing the size in elf_program_header_size (abfd),
5485 see assign_file_positions_except_relocs, so make sure we have
5486 that amount allocated, with trailing space cleared.
5487 The variable alloc contains the computed need, while
5488 elf_program_header_size (abfd) contains the size used for the
5490 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5491 where the layout is forced to according to a larger size in the
5492 last iterations for the testcase ld-elf/header. */
5493 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5494 + alloc
* sizeof (*sorted_seg_map
)));
5495 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5496 elf_tdata (abfd
)->phdr
= phdrs
;
5500 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5502 sorted_seg_map
[j
] = m
;
5503 /* If elf_segment_map is not from map_sections_to_segments, the
5504 sections may not be correctly ordered. NOTE: sorting should
5505 not be done to the PT_NOTE section of a corefile, which may
5506 contain several pseudo-sections artificially created by bfd.
5507 Sorting these pseudo-sections breaks things badly. */
5509 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5510 && m
->p_type
== PT_NOTE
))
5512 for (i
= 0; i
< m
->count
; i
++)
5513 m
->sections
[i
]->target_index
= i
;
5514 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5519 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5523 if ((abfd
->flags
& D_PAGED
) != 0)
5524 maxpagesize
= bed
->maxpagesize
;
5526 /* Sections must map to file offsets past the ELF file header. */
5527 off
= bed
->s
->sizeof_ehdr
;
5528 /* And if one of the PT_LOAD headers doesn't include the program
5529 headers then we'll be mapping program headers in the usual
5530 position after the ELF file header. */
5531 phdr_load_seg
= NULL
;
5532 for (j
= 0; j
< alloc
; j
++)
5534 m
= sorted_seg_map
[j
];
5535 if (m
->p_type
!= PT_LOAD
)
5537 if (m
->includes_phdrs
)
5543 if (phdr_load_seg
== NULL
)
5544 off
+= actual
* bed
->s
->sizeof_phdr
;
5546 for (j
= 0; j
< alloc
; j
++)
5549 bfd_vma off_adjust
; /* Octets. */
5550 bfd_boolean no_contents
;
5552 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5553 number of sections with contents contributing to both p_filesz
5554 and p_memsz, followed by a number of sections with no contents
5555 that just contribute to p_memsz. In this loop, OFF tracks next
5556 available file offset for PT_LOAD and PT_NOTE segments. */
5557 m
= sorted_seg_map
[j
];
5559 p
->p_type
= m
->p_type
;
5560 p
->p_flags
= m
->p_flags
;
5563 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5565 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5567 if (m
->p_paddr_valid
)
5568 p
->p_paddr
= m
->p_paddr
;
5569 else if (m
->count
== 0)
5572 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5574 if (p
->p_type
== PT_LOAD
5575 && (abfd
->flags
& D_PAGED
) != 0)
5577 /* p_align in demand paged PT_LOAD segments effectively stores
5578 the maximum page size. When copying an executable with
5579 objcopy, we set m->p_align from the input file. Use this
5580 value for maxpagesize rather than bed->maxpagesize, which
5581 may be different. Note that we use maxpagesize for PT_TLS
5582 segment alignment later in this function, so we are relying
5583 on at least one PT_LOAD segment appearing before a PT_TLS
5585 if (m
->p_align_valid
)
5586 maxpagesize
= m
->p_align
;
5588 p
->p_align
= maxpagesize
;
5590 else if (m
->p_align_valid
)
5591 p
->p_align
= m
->p_align
;
5592 else if (m
->count
== 0)
5593 p
->p_align
= 1 << bed
->s
->log_file_align
;
5595 if (m
== phdr_load_seg
)
5597 if (!m
->includes_filehdr
)
5599 off
+= actual
* bed
->s
->sizeof_phdr
;
5602 no_contents
= FALSE
;
5604 if (p
->p_type
== PT_LOAD
5607 bfd_size_type align
; /* Bytes. */
5608 unsigned int align_power
= 0;
5610 if (m
->p_align_valid
)
5614 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5616 unsigned int secalign
;
5618 secalign
= bfd_section_alignment (*secpp
);
5619 if (secalign
> align_power
)
5620 align_power
= secalign
;
5622 align
= (bfd_size_type
) 1 << align_power
;
5623 if (align
< maxpagesize
)
5624 align
= maxpagesize
;
5627 for (i
= 0; i
< m
->count
; i
++)
5628 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5629 /* If we aren't making room for this section, then
5630 it must be SHT_NOBITS regardless of what we've
5631 set via struct bfd_elf_special_section. */
5632 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5634 /* Find out whether this segment contains any loadable
5637 for (i
= 0; i
< m
->count
; i
++)
5638 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5640 no_contents
= FALSE
;
5644 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5646 /* Broken hardware and/or kernel require that files do not
5647 map the same page with different permissions on some hppa
5650 && (abfd
->flags
& D_PAGED
) != 0
5651 && bed
->no_page_alias
5652 && (off
& (maxpagesize
- 1)) != 0
5653 && ((off
& -maxpagesize
)
5654 == ((off
+ off_adjust
) & -maxpagesize
)))
5655 off_adjust
+= maxpagesize
;
5659 /* We shouldn't need to align the segment on disk since
5660 the segment doesn't need file space, but the gABI
5661 arguably requires the alignment and glibc ld.so
5662 checks it. So to comply with the alignment
5663 requirement but not waste file space, we adjust
5664 p_offset for just this segment. (OFF_ADJUST is
5665 subtracted from OFF later.) This may put p_offset
5666 past the end of file, but that shouldn't matter. */
5671 /* Make sure the .dynamic section is the first section in the
5672 PT_DYNAMIC segment. */
5673 else if (p
->p_type
== PT_DYNAMIC
5675 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5678 (_("%pB: The first section in the PT_DYNAMIC segment"
5679 " is not the .dynamic section"),
5681 bfd_set_error (bfd_error_bad_value
);
5684 /* Set the note section type to SHT_NOTE. */
5685 else if (p
->p_type
== PT_NOTE
)
5686 for (i
= 0; i
< m
->count
; i
++)
5687 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5689 if (m
->includes_filehdr
)
5691 if (!m
->p_flags_valid
)
5693 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5694 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5695 if (p
->p_type
== PT_LOAD
)
5699 if (p
->p_vaddr
< (bfd_vma
) off
5700 || (!m
->p_paddr_valid
5701 && p
->p_paddr
< (bfd_vma
) off
))
5704 (_("%pB: not enough room for program headers,"
5705 " try linking with -N"),
5707 bfd_set_error (bfd_error_bad_value
);
5711 if (!m
->p_paddr_valid
)
5715 else if (sorted_seg_map
[0]->includes_filehdr
)
5717 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5718 p
->p_vaddr
= filehdr
->p_vaddr
;
5719 if (!m
->p_paddr_valid
)
5720 p
->p_paddr
= filehdr
->p_paddr
;
5724 if (m
->includes_phdrs
)
5726 if (!m
->p_flags_valid
)
5728 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5729 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5730 if (!m
->includes_filehdr
)
5732 if (p
->p_type
== PT_LOAD
)
5734 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5737 p
->p_vaddr
-= off
- p
->p_offset
;
5738 if (!m
->p_paddr_valid
)
5739 p
->p_paddr
-= off
- p
->p_offset
;
5742 else if (phdr_load_seg
!= NULL
)
5744 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5745 bfd_vma phdr_off
= 0; /* Octets. */
5746 if (phdr_load_seg
->includes_filehdr
)
5747 phdr_off
= bed
->s
->sizeof_ehdr
;
5748 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5749 if (!m
->p_paddr_valid
)
5750 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5751 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5754 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5758 if (p
->p_type
== PT_LOAD
5759 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5761 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5766 /* Put meaningless p_offset for PT_LOAD segments
5767 without file contents somewhere within the first
5768 page, in an attempt to not point past EOF. */
5769 bfd_size_type align
= maxpagesize
;
5770 if (align
< p
->p_align
)
5774 p
->p_offset
= off
% align
;
5779 file_ptr adjust
; /* Octets. */
5781 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5783 p
->p_filesz
+= adjust
;
5784 p
->p_memsz
+= adjust
;
5788 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5789 maps. Set filepos for sections in PT_LOAD segments, and in
5790 core files, for sections in PT_NOTE segments.
5791 assign_file_positions_for_non_load_sections will set filepos
5792 for other sections and update p_filesz for other segments. */
5793 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5796 bfd_size_type align
;
5797 Elf_Internal_Shdr
*this_hdr
;
5800 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5801 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5803 if ((p
->p_type
== PT_LOAD
5804 || p
->p_type
== PT_TLS
)
5805 && (this_hdr
->sh_type
!= SHT_NOBITS
5806 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5807 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5808 || p
->p_type
== PT_TLS
))))
5810 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5811 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5812 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5813 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5817 || p_end
< p_start
))
5820 /* xgettext:c-format */
5821 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5822 abfd
, sec
, (uint64_t) s_start
/ opb
,
5823 (uint64_t) p_end
/ opb
);
5825 sec
->lma
= p_end
/ opb
;
5827 p
->p_memsz
+= adjust
;
5829 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5831 if (p
->p_type
== PT_LOAD
)
5833 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5835 /* We have a PROGBITS section following NOBITS ones.
5836 Allocate file space for the NOBITS section(s) and
5838 adjust
= p
->p_memsz
- p
->p_filesz
;
5839 if (!write_zeros (abfd
, off
, adjust
))
5844 p
->p_filesz
+= adjust
;
5848 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5850 /* The section at i == 0 is the one that actually contains
5854 this_hdr
->sh_offset
= sec
->filepos
= off
;
5855 off
+= this_hdr
->sh_size
;
5856 p
->p_filesz
= this_hdr
->sh_size
;
5862 /* The rest are fake sections that shouldn't be written. */
5871 if (p
->p_type
== PT_LOAD
)
5873 this_hdr
->sh_offset
= sec
->filepos
= off
;
5874 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5875 off
+= this_hdr
->sh_size
;
5877 else if (this_hdr
->sh_type
== SHT_NOBITS
5878 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5879 && this_hdr
->sh_offset
== 0)
5881 /* This is a .tbss section that didn't get a PT_LOAD.
5882 (See _bfd_elf_map_sections_to_segments "Create a
5883 final PT_LOAD".) Set sh_offset to the value it
5884 would have if we had created a zero p_filesz and
5885 p_memsz PT_LOAD header for the section. This
5886 also makes the PT_TLS header have the same
5888 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5890 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5893 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5895 p
->p_filesz
+= this_hdr
->sh_size
;
5896 /* A load section without SHF_ALLOC is something like
5897 a note section in a PT_NOTE segment. These take
5898 file space but are not loaded into memory. */
5899 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5900 p
->p_memsz
+= this_hdr
->sh_size
;
5902 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5904 if (p
->p_type
== PT_TLS
)
5905 p
->p_memsz
+= this_hdr
->sh_size
;
5907 /* .tbss is special. It doesn't contribute to p_memsz of
5909 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5910 p
->p_memsz
+= this_hdr
->sh_size
;
5913 if (align
> p
->p_align
5914 && !m
->p_align_valid
5915 && (p
->p_type
!= PT_LOAD
5916 || (abfd
->flags
& D_PAGED
) == 0))
5920 if (!m
->p_flags_valid
)
5923 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5925 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5932 /* PR ld/20815 - Check that the program header segment, if
5933 present, will be loaded into memory. */
5934 if (p
->p_type
== PT_PHDR
5935 && phdr_load_seg
== NULL
5936 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
5937 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
5939 /* The fix for this error is usually to edit the linker script being
5940 used and set up the program headers manually. Either that or
5941 leave room for the headers at the start of the SECTIONS. */
5942 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5943 " by LOAD segment"),
5945 if (link_info
== NULL
)
5947 /* Arrange for the linker to exit with an error, deleting
5948 the output file unless --noinhibit-exec is given. */
5949 link_info
->callbacks
->info ("%X");
5952 /* Check that all sections are in a PT_LOAD segment.
5953 Don't check funky gdb generated core files. */
5954 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5956 bfd_boolean check_vma
= TRUE
;
5958 for (i
= 1; i
< m
->count
; i
++)
5959 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5960 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5961 ->this_hdr
), p
) != 0
5962 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5963 ->this_hdr
), p
) != 0)
5965 /* Looks like we have overlays packed into the segment. */
5970 for (i
= 0; i
< m
->count
; i
++)
5972 Elf_Internal_Shdr
*this_hdr
;
5975 sec
= m
->sections
[i
];
5976 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5977 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5978 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5981 /* xgettext:c-format */
5982 (_("%pB: section `%pA' can't be allocated in segment %d"),
5984 print_segment_map (m
);
5990 elf_next_file_pos (abfd
) = off
;
5992 if (link_info
!= NULL
5993 && phdr_load_seg
!= NULL
5994 && phdr_load_seg
->includes_filehdr
)
5996 /* There is a segment that contains both the file headers and the
5997 program headers, so provide a symbol __ehdr_start pointing there.
5998 A program can use this to examine itself robustly. */
6000 struct elf_link_hash_entry
*hash
6001 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6002 FALSE
, FALSE
, TRUE
);
6003 /* If the symbol was referenced and not defined, define it. */
6005 && (hash
->root
.type
== bfd_link_hash_new
6006 || hash
->root
.type
== bfd_link_hash_undefined
6007 || hash
->root
.type
== bfd_link_hash_undefweak
6008 || hash
->root
.type
== bfd_link_hash_common
))
6011 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6013 if (phdr_load_seg
->count
!= 0)
6014 /* The segment contains sections, so use the first one. */
6015 s
= phdr_load_seg
->sections
[0];
6017 /* Use the first (i.e. lowest-addressed) section in any segment. */
6018 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6019 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6027 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6028 hash
->root
.u
.def
.section
= s
;
6032 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6033 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6036 hash
->root
.type
= bfd_link_hash_defined
;
6037 hash
->def_regular
= 1;
6045 /* Determine if a bfd is a debuginfo file. Unfortunately there
6046 is no defined method for detecting such files, so we have to
6047 use heuristics instead. */
6050 is_debuginfo_file (bfd
*abfd
)
6052 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6055 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6056 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6057 Elf_Internal_Shdr
**headerp
;
6059 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6061 Elf_Internal_Shdr
*header
= * headerp
;
6063 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6064 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6065 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6066 && header
->sh_type
!= SHT_NOBITS
6067 && header
->sh_type
!= SHT_NOTE
)
6074 /* Assign file positions for the other sections, except for compressed debugging
6075 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6078 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6079 struct bfd_link_info
*link_info
)
6081 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6082 Elf_Internal_Shdr
**i_shdrpp
;
6083 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6084 Elf_Internal_Phdr
*phdrs
;
6085 Elf_Internal_Phdr
*p
;
6086 struct elf_segment_map
*m
;
6088 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6090 i_shdrpp
= elf_elfsections (abfd
);
6091 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6092 off
= elf_next_file_pos (abfd
);
6093 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6095 Elf_Internal_Shdr
*hdr
;
6098 if (hdr
->bfd_section
!= NULL
6099 && (hdr
->bfd_section
->filepos
!= 0
6100 || (hdr
->sh_type
== SHT_NOBITS
6101 && hdr
->contents
== NULL
)))
6102 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6103 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6105 if (hdr
->sh_size
!= 0
6106 /* PR 24717 - debuginfo files are known to be not strictly
6107 compliant with the ELF standard. In particular they often
6108 have .note.gnu.property sections that are outside of any
6109 loadable segment. This is not a problem for such files,
6110 so do not warn about them. */
6111 && ! is_debuginfo_file (abfd
))
6113 /* xgettext:c-format */
6114 (_("%pB: warning: allocated section `%s' not in segment"),
6116 (hdr
->bfd_section
== NULL
6118 : hdr
->bfd_section
->name
));
6119 /* We don't need to page align empty sections. */
6120 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6121 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6124 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6126 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6129 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6130 && hdr
->bfd_section
== NULL
)
6131 /* We don't know the offset of these sections yet: their size has
6132 not been decided. */
6133 || (hdr
->bfd_section
!= NULL
6134 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6135 || (bfd_section_is_ctf (hdr
->bfd_section
)
6136 && abfd
->is_linker_output
)))
6137 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6138 || (elf_symtab_shndx_list (abfd
) != NULL
6139 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6140 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6141 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6142 hdr
->sh_offset
= -1;
6144 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6146 elf_next_file_pos (abfd
) = off
;
6148 /* Now that we have set the section file positions, we can set up
6149 the file positions for the non PT_LOAD segments. */
6150 phdrs
= elf_tdata (abfd
)->phdr
;
6151 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6153 if (p
->p_type
== PT_GNU_RELRO
)
6155 bfd_vma start
, end
; /* Bytes. */
6158 if (link_info
!= NULL
)
6160 /* During linking the range of the RELRO segment is passed
6161 in link_info. Note that there may be padding between
6162 relro_start and the first RELRO section. */
6163 start
= link_info
->relro_start
;
6164 end
= link_info
->relro_end
;
6166 else if (m
->count
!= 0)
6168 if (!m
->p_size_valid
)
6170 start
= m
->sections
[0]->vma
;
6171 end
= start
+ m
->p_size
/ opb
;
6182 struct elf_segment_map
*lm
;
6183 const Elf_Internal_Phdr
*lp
;
6186 /* Find a LOAD segment containing a section in the RELRO
6188 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6190 lm
= lm
->next
, lp
++)
6192 if (lp
->p_type
== PT_LOAD
6194 && (lm
->sections
[lm
->count
- 1]->vma
6195 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6196 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6198 && lm
->sections
[0]->vma
< end
)
6204 /* Find the section starting the RELRO segment. */
6205 for (i
= 0; i
< lm
->count
; i
++)
6207 asection
*s
= lm
->sections
[i
];
6216 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6217 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6218 p
->p_offset
= lm
->sections
[i
]->filepos
;
6219 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6220 p
->p_filesz
= p
->p_memsz
;
6222 /* The RELRO segment typically ends a few bytes
6223 into .got.plt but other layouts are possible.
6224 In cases where the end does not match any
6225 loaded section (for instance is in file
6226 padding), trim p_filesz back to correspond to
6227 the end of loaded section contents. */
6228 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6229 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6231 /* Preserve the alignment and flags if they are
6232 valid. The gold linker generates RW/4 for
6233 the PT_GNU_RELRO section. It is better for
6234 objcopy/strip to honor these attributes
6235 otherwise gdb will choke when using separate
6237 if (!m
->p_align_valid
)
6239 if (!m
->p_flags_valid
)
6245 if (link_info
!= NULL
)
6248 memset (p
, 0, sizeof *p
);
6250 else if (p
->p_type
== PT_GNU_STACK
)
6252 if (m
->p_size_valid
)
6253 p
->p_memsz
= m
->p_size
;
6255 else if (m
->count
!= 0)
6259 if (p
->p_type
!= PT_LOAD
6260 && (p
->p_type
!= PT_NOTE
6261 || bfd_get_format (abfd
) != bfd_core
))
6263 /* A user specified segment layout may include a PHDR
6264 segment that overlaps with a LOAD segment... */
6265 if (p
->p_type
== PT_PHDR
)
6271 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6273 /* PR 17512: file: 2195325e. */
6275 (_("%pB: error: non-load segment %d includes file header "
6276 "and/or program header"),
6277 abfd
, (int) (p
- phdrs
));
6282 p
->p_offset
= m
->sections
[0]->filepos
;
6283 for (i
= m
->count
; i
-- != 0;)
6285 asection
*sect
= m
->sections
[i
];
6286 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6287 if (hdr
->sh_type
!= SHT_NOBITS
)
6289 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6301 static elf_section_list
*
6302 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6304 for (;list
!= NULL
; list
= list
->next
)
6310 /* Work out the file positions of all the sections. This is called by
6311 _bfd_elf_compute_section_file_positions. All the section sizes and
6312 VMAs must be known before this is called.
6314 Reloc sections come in two flavours: Those processed specially as
6315 "side-channel" data attached to a section to which they apply, and those that
6316 bfd doesn't process as relocations. The latter sort are stored in a normal
6317 bfd section by bfd_section_from_shdr. We don't consider the former sort
6318 here, unless they form part of the loadable image. Reloc sections not
6319 assigned here (and compressed debugging sections and CTF sections which
6320 nothing else in the file can rely upon) will be handled later by
6321 assign_file_positions_for_relocs.
6323 We also don't set the positions of the .symtab and .strtab here. */
6326 assign_file_positions_except_relocs (bfd
*abfd
,
6327 struct bfd_link_info
*link_info
)
6329 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6330 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6331 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6334 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6335 && bfd_get_format (abfd
) != bfd_core
)
6337 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6338 unsigned int num_sec
= elf_numsections (abfd
);
6339 Elf_Internal_Shdr
**hdrpp
;
6343 /* Start after the ELF header. */
6344 off
= i_ehdrp
->e_ehsize
;
6346 /* We are not creating an executable, which means that we are
6347 not creating a program header, and that the actual order of
6348 the sections in the file is unimportant. */
6349 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6351 Elf_Internal_Shdr
*hdr
;
6354 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6355 && hdr
->bfd_section
== NULL
)
6356 /* Do not assign offsets for these sections yet: we don't know
6358 || (hdr
->bfd_section
!= NULL
6359 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6360 || (bfd_section_is_ctf (hdr
->bfd_section
)
6361 && abfd
->is_linker_output
)))
6362 || i
== elf_onesymtab (abfd
)
6363 || (elf_symtab_shndx_list (abfd
) != NULL
6364 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6365 || i
== elf_strtab_sec (abfd
)
6366 || i
== elf_shstrtab_sec (abfd
))
6368 hdr
->sh_offset
= -1;
6371 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6374 elf_next_file_pos (abfd
) = off
;
6375 elf_program_header_size (abfd
) = 0;
6379 /* Assign file positions for the loaded sections based on the
6380 assignment of sections to segments. */
6381 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6384 /* And for non-load sections. */
6385 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6389 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6392 /* Write out the program headers. */
6393 alloc
= i_ehdrp
->e_phnum
;
6396 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6397 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6405 _bfd_elf_init_file_header (bfd
*abfd
,
6406 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6408 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6409 struct elf_strtab_hash
*shstrtab
;
6410 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6412 i_ehdrp
= elf_elfheader (abfd
);
6414 shstrtab
= _bfd_elf_strtab_init ();
6415 if (shstrtab
== NULL
)
6418 elf_shstrtab (abfd
) = shstrtab
;
6420 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6421 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6422 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6423 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6425 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6426 i_ehdrp
->e_ident
[EI_DATA
] =
6427 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6428 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6430 if ((abfd
->flags
& DYNAMIC
) != 0)
6431 i_ehdrp
->e_type
= ET_DYN
;
6432 else if ((abfd
->flags
& EXEC_P
) != 0)
6433 i_ehdrp
->e_type
= ET_EXEC
;
6434 else if (bfd_get_format (abfd
) == bfd_core
)
6435 i_ehdrp
->e_type
= ET_CORE
;
6437 i_ehdrp
->e_type
= ET_REL
;
6439 switch (bfd_get_arch (abfd
))
6441 case bfd_arch_unknown
:
6442 i_ehdrp
->e_machine
= EM_NONE
;
6445 /* There used to be a long list of cases here, each one setting
6446 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6447 in the corresponding bfd definition. To avoid duplication,
6448 the switch was removed. Machines that need special handling
6449 can generally do it in elf_backend_final_write_processing(),
6450 unless they need the information earlier than the final write.
6451 Such need can generally be supplied by replacing the tests for
6452 e_machine with the conditions used to determine it. */
6454 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6457 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6458 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6460 /* No program header, for now. */
6461 i_ehdrp
->e_phoff
= 0;
6462 i_ehdrp
->e_phentsize
= 0;
6463 i_ehdrp
->e_phnum
= 0;
6465 /* Each bfd section is section header entry. */
6466 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6467 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6469 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6470 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6471 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6472 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6473 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6474 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6475 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6476 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6477 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6483 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6485 FIXME: We used to have code here to sort the PT_LOAD segments into
6486 ascending order, as per the ELF spec. But this breaks some programs,
6487 including the Linux kernel. But really either the spec should be
6488 changed or the programs updated. */
6491 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6493 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6495 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6496 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6497 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6498 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6499 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6501 /* Find the lowest p_vaddr in PT_LOAD segments. */
6502 bfd_vma p_vaddr
= (bfd_vma
) -1;
6503 for (; segment
< end_segment
; segment
++)
6504 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6505 p_vaddr
= segment
->p_vaddr
;
6507 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6508 segments is non-zero. */
6510 i_ehdrp
->e_type
= ET_EXEC
;
6515 /* Assign file positions for all the reloc sections which are not part
6516 of the loadable file image, and the file position of section headers. */
6519 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6522 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6523 Elf_Internal_Shdr
*shdrp
;
6524 Elf_Internal_Ehdr
*i_ehdrp
;
6525 const struct elf_backend_data
*bed
;
6527 off
= elf_next_file_pos (abfd
);
6529 shdrpp
= elf_elfsections (abfd
);
6530 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6531 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6534 if (shdrp
->sh_offset
== -1)
6536 asection
*sec
= shdrp
->bfd_section
;
6537 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6538 || shdrp
->sh_type
== SHT_RELA
);
6539 bfd_boolean is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6542 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6544 if (!is_rel
&& !is_ctf
)
6546 const char *name
= sec
->name
;
6547 struct bfd_elf_section_data
*d
;
6549 /* Compress DWARF debug sections. */
6550 if (!bfd_compress_section (abfd
, sec
,
6554 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6555 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6557 /* If section is compressed with zlib-gnu, convert
6558 section name from .debug_* to .zdebug_*. */
6560 = convert_debug_to_zdebug (abfd
, name
);
6561 if (new_name
== NULL
)
6565 /* Add section name to section name section. */
6566 if (shdrp
->sh_name
!= (unsigned int) -1)
6569 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6571 d
= elf_section_data (sec
);
6573 /* Add reloc section name to section name section. */
6575 && !_bfd_elf_set_reloc_sh_name (abfd
,
6580 && !_bfd_elf_set_reloc_sh_name (abfd
,
6585 /* Update section size and contents. */
6586 shdrp
->sh_size
= sec
->size
;
6587 shdrp
->contents
= sec
->contents
;
6588 shdrp
->bfd_section
->contents
= NULL
;
6592 /* Update section size and contents. */
6593 shdrp
->sh_size
= sec
->size
;
6594 shdrp
->contents
= sec
->contents
;
6597 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6604 /* Place section name section after DWARF debug sections have been
6606 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6607 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6608 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6609 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6611 /* Place the section headers. */
6612 i_ehdrp
= elf_elfheader (abfd
);
6613 bed
= get_elf_backend_data (abfd
);
6614 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6615 i_ehdrp
->e_shoff
= off
;
6616 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6617 elf_next_file_pos (abfd
) = off
;
6623 _bfd_elf_write_object_contents (bfd
*abfd
)
6625 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6626 Elf_Internal_Shdr
**i_shdrp
;
6628 unsigned int count
, num_sec
;
6629 struct elf_obj_tdata
*t
;
6631 if (! abfd
->output_has_begun
6632 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6634 /* Do not rewrite ELF data when the BFD has been opened for update.
6635 abfd->output_has_begun was set to TRUE on opening, so creation of new
6636 sections, and modification of existing section sizes was restricted.
6637 This means the ELF header, program headers and section headers can't have
6639 If the contents of any sections has been modified, then those changes have
6640 already been written to the BFD. */
6641 else if (abfd
->direction
== both_direction
)
6643 BFD_ASSERT (abfd
->output_has_begun
);
6647 i_shdrp
= elf_elfsections (abfd
);
6650 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6654 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6657 /* After writing the headers, we need to write the sections too... */
6658 num_sec
= elf_numsections (abfd
);
6659 for (count
= 1; count
< num_sec
; count
++)
6661 i_shdrp
[count
]->sh_name
6662 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6663 i_shdrp
[count
]->sh_name
);
6664 if (bed
->elf_backend_section_processing
)
6665 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6667 if (i_shdrp
[count
]->contents
)
6669 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6671 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6672 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6677 /* Write out the section header names. */
6678 t
= elf_tdata (abfd
);
6679 if (elf_shstrtab (abfd
) != NULL
6680 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6681 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6684 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6687 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6690 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6691 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6692 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6698 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6700 /* Hopefully this can be done just like an object file. */
6701 return _bfd_elf_write_object_contents (abfd
);
6704 /* Given a section, search the header to find them. */
6707 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6709 const struct elf_backend_data
*bed
;
6710 unsigned int sec_index
;
6712 if (elf_section_data (asect
) != NULL
6713 && elf_section_data (asect
)->this_idx
!= 0)
6714 return elf_section_data (asect
)->this_idx
;
6716 if (bfd_is_abs_section (asect
))
6717 sec_index
= SHN_ABS
;
6718 else if (bfd_is_com_section (asect
))
6719 sec_index
= SHN_COMMON
;
6720 else if (bfd_is_und_section (asect
))
6721 sec_index
= SHN_UNDEF
;
6723 sec_index
= SHN_BAD
;
6725 bed
= get_elf_backend_data (abfd
);
6726 if (bed
->elf_backend_section_from_bfd_section
)
6728 int retval
= sec_index
;
6730 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6734 if (sec_index
== SHN_BAD
)
6735 bfd_set_error (bfd_error_nonrepresentable_section
);
6740 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6744 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6746 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6748 flagword flags
= asym_ptr
->flags
;
6750 /* When gas creates relocations against local labels, it creates its
6751 own symbol for the section, but does put the symbol into the
6752 symbol chain, so udata is 0. When the linker is generating
6753 relocatable output, this section symbol may be for one of the
6754 input sections rather than the output section. */
6755 if (asym_ptr
->udata
.i
== 0
6756 && (flags
& BSF_SECTION_SYM
)
6757 && asym_ptr
->section
)
6762 sec
= asym_ptr
->section
;
6763 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6764 sec
= sec
->output_section
;
6765 if (sec
->owner
== abfd
6766 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6767 && elf_section_syms (abfd
)[indx
] != NULL
)
6768 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6771 idx
= asym_ptr
->udata
.i
;
6775 /* This case can occur when using --strip-symbol on a symbol
6776 which is used in a relocation entry. */
6778 /* xgettext:c-format */
6779 (_("%pB: symbol `%s' required but not present"),
6780 abfd
, bfd_asymbol_name (asym_ptr
));
6781 bfd_set_error (bfd_error_no_symbols
);
6788 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6789 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6797 /* Rewrite program header information. */
6800 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6802 Elf_Internal_Ehdr
*iehdr
;
6803 struct elf_segment_map
*map
;
6804 struct elf_segment_map
*map_first
;
6805 struct elf_segment_map
**pointer_to_map
;
6806 Elf_Internal_Phdr
*segment
;
6809 unsigned int num_segments
;
6810 bfd_boolean phdr_included
= FALSE
;
6811 bfd_boolean p_paddr_valid
;
6812 bfd_vma maxpagesize
;
6813 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6814 unsigned int phdr_adjust_num
= 0;
6815 const struct elf_backend_data
*bed
;
6816 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
6818 bed
= get_elf_backend_data (ibfd
);
6819 iehdr
= elf_elfheader (ibfd
);
6822 pointer_to_map
= &map_first
;
6824 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6825 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6827 /* Returns the end address of the segment + 1. */
6828 #define SEGMENT_END(segment, start) \
6829 (start + (segment->p_memsz > segment->p_filesz \
6830 ? segment->p_memsz : segment->p_filesz))
6832 #define SECTION_SIZE(section, segment) \
6833 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6834 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6835 ? section->size : 0)
6837 /* Returns TRUE if the given section is contained within
6838 the given segment. VMA addresses are compared. */
6839 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6840 (section->vma * (opb) >= segment->p_vaddr \
6841 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6842 <= (SEGMENT_END (segment, segment->p_vaddr))))
6844 /* Returns TRUE if the given section is contained within
6845 the given segment. LMA addresses are compared. */
6846 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6847 (section->lma * (opb) >= base \
6848 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6849 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6850 <= SEGMENT_END (segment, base)))
6852 /* Handle PT_NOTE segment. */
6853 #define IS_NOTE(p, s) \
6854 (p->p_type == PT_NOTE \
6855 && elf_section_type (s) == SHT_NOTE \
6856 && (bfd_vma) s->filepos >= p->p_offset \
6857 && ((bfd_vma) s->filepos + s->size \
6858 <= p->p_offset + p->p_filesz))
6860 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6862 #define IS_COREFILE_NOTE(p, s) \
6864 && bfd_get_format (ibfd) == bfd_core \
6868 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6869 linker, which generates a PT_INTERP section with p_vaddr and
6870 p_memsz set to 0. */
6871 #define IS_SOLARIS_PT_INTERP(p, s) \
6873 && p->p_paddr == 0 \
6874 && p->p_memsz == 0 \
6875 && p->p_filesz > 0 \
6876 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6878 && (bfd_vma) s->filepos >= p->p_offset \
6879 && ((bfd_vma) s->filepos + s->size \
6880 <= p->p_offset + p->p_filesz))
6882 /* Decide if the given section should be included in the given segment.
6883 A section will be included if:
6884 1. It is within the address space of the segment -- we use the LMA
6885 if that is set for the segment and the VMA otherwise,
6886 2. It is an allocated section or a NOTE section in a PT_NOTE
6888 3. There is an output section associated with it,
6889 4. The section has not already been allocated to a previous segment.
6890 5. PT_GNU_STACK segments do not include any sections.
6891 6. PT_TLS segment includes only SHF_TLS sections.
6892 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6893 8. PT_DYNAMIC should not contain empty sections at the beginning
6894 (with the possible exception of .dynamic). */
6895 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6896 ((((segment->p_paddr \
6897 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6898 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
6899 && (section->flags & SEC_ALLOC) != 0) \
6900 || IS_NOTE (segment, section)) \
6901 && segment->p_type != PT_GNU_STACK \
6902 && (segment->p_type != PT_TLS \
6903 || (section->flags & SEC_THREAD_LOCAL)) \
6904 && (segment->p_type == PT_LOAD \
6905 || segment->p_type == PT_TLS \
6906 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6907 && (segment->p_type != PT_DYNAMIC \
6908 || SECTION_SIZE (section, segment) > 0 \
6909 || (segment->p_paddr \
6910 ? segment->p_paddr != section->lma * (opb) \
6911 : segment->p_vaddr != section->vma * (opb)) \
6912 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6913 && (segment->p_type != PT_LOAD || !section->segment_mark))
6915 /* If the output section of a section in the input segment is NULL,
6916 it is removed from the corresponding output segment. */
6917 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6918 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
6919 && section->output_section != NULL)
6921 /* Returns TRUE iff seg1 starts after the end of seg2. */
6922 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6923 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6925 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6926 their VMA address ranges and their LMA address ranges overlap.
6927 It is possible to have overlapping VMA ranges without overlapping LMA
6928 ranges. RedBoot images for example can have both .data and .bss mapped
6929 to the same VMA range, but with the .data section mapped to a different
6931 #define SEGMENT_OVERLAPS(seg1, seg2) \
6932 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6933 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6934 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6935 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6937 /* Initialise the segment mark field. */
6938 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6939 section
->segment_mark
= FALSE
;
6941 /* The Solaris linker creates program headers in which all the
6942 p_paddr fields are zero. When we try to objcopy or strip such a
6943 file, we get confused. Check for this case, and if we find it
6944 don't set the p_paddr_valid fields. */
6945 p_paddr_valid
= FALSE
;
6946 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6949 if (segment
->p_paddr
!= 0)
6951 p_paddr_valid
= TRUE
;
6955 /* Scan through the segments specified in the program header
6956 of the input BFD. For this first scan we look for overlaps
6957 in the loadable segments. These can be created by weird
6958 parameters to objcopy. Also, fix some solaris weirdness. */
6959 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6964 Elf_Internal_Phdr
*segment2
;
6966 if (segment
->p_type
== PT_INTERP
)
6967 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6968 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6970 /* Mininal change so that the normal section to segment
6971 assignment code will work. */
6972 segment
->p_vaddr
= section
->vma
* opb
;
6976 if (segment
->p_type
!= PT_LOAD
)
6978 /* Remove PT_GNU_RELRO segment. */
6979 if (segment
->p_type
== PT_GNU_RELRO
)
6980 segment
->p_type
= PT_NULL
;
6984 /* Determine if this segment overlaps any previous segments. */
6985 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6987 bfd_signed_vma extra_length
;
6989 if (segment2
->p_type
!= PT_LOAD
6990 || !SEGMENT_OVERLAPS (segment
, segment2
))
6993 /* Merge the two segments together. */
6994 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6996 /* Extend SEGMENT2 to include SEGMENT and then delete
6998 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6999 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
7001 if (extra_length
> 0)
7003 segment2
->p_memsz
+= extra_length
;
7004 segment2
->p_filesz
+= extra_length
;
7007 segment
->p_type
= PT_NULL
;
7009 /* Since we have deleted P we must restart the outer loop. */
7011 segment
= elf_tdata (ibfd
)->phdr
;
7016 /* Extend SEGMENT to include SEGMENT2 and then delete
7018 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
7019 - SEGMENT_END (segment
, segment
->p_vaddr
));
7021 if (extra_length
> 0)
7023 segment
->p_memsz
+= extra_length
;
7024 segment
->p_filesz
+= extra_length
;
7027 segment2
->p_type
= PT_NULL
;
7032 /* The second scan attempts to assign sections to segments. */
7033 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7037 unsigned int section_count
;
7038 asection
**sections
;
7039 asection
*output_section
;
7041 asection
*matching_lma
;
7042 asection
*suggested_lma
;
7045 asection
*first_section
;
7047 if (segment
->p_type
== PT_NULL
)
7050 first_section
= NULL
;
7051 /* Compute how many sections might be placed into this segment. */
7052 for (section
= ibfd
->sections
, section_count
= 0;
7054 section
= section
->next
)
7056 /* Find the first section in the input segment, which may be
7057 removed from the corresponding output segment. */
7058 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7060 if (first_section
== NULL
)
7061 first_section
= section
;
7062 if (section
->output_section
!= NULL
)
7067 /* Allocate a segment map big enough to contain
7068 all of the sections we have selected. */
7069 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7070 amt
+= section_count
* sizeof (asection
*);
7071 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7075 /* Initialise the fields of the segment map. Default to
7076 using the physical address of the segment in the input BFD. */
7078 map
->p_type
= segment
->p_type
;
7079 map
->p_flags
= segment
->p_flags
;
7080 map
->p_flags_valid
= 1;
7082 /* If the first section in the input segment is removed, there is
7083 no need to preserve segment physical address in the corresponding
7085 if (!first_section
|| first_section
->output_section
!= NULL
)
7087 map
->p_paddr
= segment
->p_paddr
;
7088 map
->p_paddr_valid
= p_paddr_valid
;
7091 /* Determine if this segment contains the ELF file header
7092 and if it contains the program headers themselves. */
7093 map
->includes_filehdr
= (segment
->p_offset
== 0
7094 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7095 map
->includes_phdrs
= 0;
7097 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7099 map
->includes_phdrs
=
7100 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7101 && (segment
->p_offset
+ segment
->p_filesz
7102 >= ((bfd_vma
) iehdr
->e_phoff
7103 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7105 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7106 phdr_included
= TRUE
;
7109 if (section_count
== 0)
7111 /* Special segments, such as the PT_PHDR segment, may contain
7112 no sections, but ordinary, loadable segments should contain
7113 something. They are allowed by the ELF spec however, so only
7114 a warning is produced.
7115 There is however the valid use case of embedded systems which
7116 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7117 flash memory with zeros. No warning is shown for that case. */
7118 if (segment
->p_type
== PT_LOAD
7119 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7120 /* xgettext:c-format */
7122 (_("%pB: warning: empty loadable segment detected"
7123 " at vaddr=%#" PRIx64
", is this intentional?"),
7124 ibfd
, (uint64_t) segment
->p_vaddr
);
7126 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7128 *pointer_to_map
= map
;
7129 pointer_to_map
= &map
->next
;
7134 /* Now scan the sections in the input BFD again and attempt
7135 to add their corresponding output sections to the segment map.
7136 The problem here is how to handle an output section which has
7137 been moved (ie had its LMA changed). There are four possibilities:
7139 1. None of the sections have been moved.
7140 In this case we can continue to use the segment LMA from the
7143 2. All of the sections have been moved by the same amount.
7144 In this case we can change the segment's LMA to match the LMA
7145 of the first section.
7147 3. Some of the sections have been moved, others have not.
7148 In this case those sections which have not been moved can be
7149 placed in the current segment which will have to have its size,
7150 and possibly its LMA changed, and a new segment or segments will
7151 have to be created to contain the other sections.
7153 4. The sections have been moved, but not by the same amount.
7154 In this case we can change the segment's LMA to match the LMA
7155 of the first section and we will have to create a new segment
7156 or segments to contain the other sections.
7158 In order to save time, we allocate an array to hold the section
7159 pointers that we are interested in. As these sections get assigned
7160 to a segment, they are removed from this array. */
7162 amt
= section_count
* sizeof (asection
*);
7163 sections
= (asection
**) bfd_malloc (amt
);
7164 if (sections
== NULL
)
7167 /* Step One: Scan for segment vs section LMA conflicts.
7168 Also add the sections to the section array allocated above.
7169 Also add the sections to the current segment. In the common
7170 case, where the sections have not been moved, this means that
7171 we have completely filled the segment, and there is nothing
7174 matching_lma
= NULL
;
7175 suggested_lma
= NULL
;
7177 for (section
= first_section
, j
= 0;
7179 section
= section
->next
)
7181 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7183 output_section
= section
->output_section
;
7185 sections
[j
++] = section
;
7187 /* The Solaris native linker always sets p_paddr to 0.
7188 We try to catch that case here, and set it to the
7189 correct value. Note - some backends require that
7190 p_paddr be left as zero. */
7192 && segment
->p_vaddr
!= 0
7193 && !bed
->want_p_paddr_set_to_zero
7195 && output_section
->lma
!= 0
7196 && (align_power (segment
->p_vaddr
7197 + (map
->includes_filehdr
7198 ? iehdr
->e_ehsize
: 0)
7199 + (map
->includes_phdrs
7200 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7202 output_section
->alignment_power
* opb
)
7203 == (output_section
->vma
* opb
)))
7204 map
->p_paddr
= segment
->p_vaddr
;
7206 /* Match up the physical address of the segment with the
7207 LMA address of the output section. */
7208 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7210 || IS_COREFILE_NOTE (segment
, section
)
7211 || (bed
->want_p_paddr_set_to_zero
7212 && IS_CONTAINED_BY_VMA (output_section
, segment
, opb
)))
7214 if (matching_lma
== NULL
7215 || output_section
->lma
< matching_lma
->lma
)
7216 matching_lma
= output_section
;
7218 /* We assume that if the section fits within the segment
7219 then it does not overlap any other section within that
7221 map
->sections
[isec
++] = output_section
;
7223 else if (suggested_lma
== NULL
)
7224 suggested_lma
= output_section
;
7226 if (j
== section_count
)
7231 BFD_ASSERT (j
== section_count
);
7233 /* Step Two: Adjust the physical address of the current segment,
7235 if (isec
== section_count
)
7237 /* All of the sections fitted within the segment as currently
7238 specified. This is the default case. Add the segment to
7239 the list of built segments and carry on to process the next
7240 program header in the input BFD. */
7241 map
->count
= section_count
;
7242 *pointer_to_map
= map
;
7243 pointer_to_map
= &map
->next
;
7246 && !bed
->want_p_paddr_set_to_zero
)
7248 bfd_vma hdr_size
= 0;
7249 if (map
->includes_filehdr
)
7250 hdr_size
= iehdr
->e_ehsize
;
7251 if (map
->includes_phdrs
)
7252 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7254 /* Account for padding before the first section in the
7256 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7257 - matching_lma
->lma
);
7265 /* Change the current segment's physical address to match
7266 the LMA of the first section that fitted, or if no
7267 section fitted, the first section. */
7268 if (matching_lma
== NULL
)
7269 matching_lma
= suggested_lma
;
7271 map
->p_paddr
= matching_lma
->lma
* opb
;
7273 /* Offset the segment physical address from the lma
7274 to allow for space taken up by elf headers. */
7275 if (map
->includes_phdrs
)
7277 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7279 /* iehdr->e_phnum is just an estimate of the number
7280 of program headers that we will need. Make a note
7281 here of the number we used and the segment we chose
7282 to hold these headers, so that we can adjust the
7283 offset when we know the correct value. */
7284 phdr_adjust_num
= iehdr
->e_phnum
;
7285 phdr_adjust_seg
= map
;
7288 if (map
->includes_filehdr
)
7290 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7291 map
->p_paddr
-= iehdr
->e_ehsize
;
7292 /* We've subtracted off the size of headers from the
7293 first section lma, but there may have been some
7294 alignment padding before that section too. Try to
7295 account for that by adjusting the segment lma down to
7296 the same alignment. */
7297 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7298 align
= segment
->p_align
;
7299 map
->p_paddr
&= -(align
* opb
);
7303 /* Step Three: Loop over the sections again, this time assigning
7304 those that fit to the current segment and removing them from the
7305 sections array; but making sure not to leave large gaps. Once all
7306 possible sections have been assigned to the current segment it is
7307 added to the list of built segments and if sections still remain
7308 to be assigned, a new segment is constructed before repeating
7314 suggested_lma
= NULL
;
7316 /* Fill the current segment with sections that fit. */
7317 for (j
= 0; j
< section_count
; j
++)
7319 section
= sections
[j
];
7321 if (section
== NULL
)
7324 output_section
= section
->output_section
;
7326 BFD_ASSERT (output_section
!= NULL
);
7328 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7330 || IS_COREFILE_NOTE (segment
, section
))
7332 if (map
->count
== 0)
7334 /* If the first section in a segment does not start at
7335 the beginning of the segment, then something is
7337 if (align_power (map
->p_paddr
7338 + (map
->includes_filehdr
7339 ? iehdr
->e_ehsize
: 0)
7340 + (map
->includes_phdrs
7341 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7343 output_section
->alignment_power
* opb
)
7344 != output_section
->lma
* opb
)
7351 prev_sec
= map
->sections
[map
->count
- 1];
7353 /* If the gap between the end of the previous section
7354 and the start of this section is more than
7355 maxpagesize then we need to start a new segment. */
7356 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7358 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7359 || (prev_sec
->lma
+ prev_sec
->size
7360 > output_section
->lma
))
7362 if (suggested_lma
== NULL
)
7363 suggested_lma
= output_section
;
7369 map
->sections
[map
->count
++] = output_section
;
7372 if (segment
->p_type
== PT_LOAD
)
7373 section
->segment_mark
= TRUE
;
7375 else if (suggested_lma
== NULL
)
7376 suggested_lma
= output_section
;
7379 /* PR 23932. A corrupt input file may contain sections that cannot
7380 be assigned to any segment - because for example they have a
7381 negative size - or segments that do not contain any sections.
7382 But there are also valid reasons why a segment can be empty.
7383 So allow a count of zero. */
7385 /* Add the current segment to the list of built segments. */
7386 *pointer_to_map
= map
;
7387 pointer_to_map
= &map
->next
;
7389 if (isec
< section_count
)
7391 /* We still have not allocated all of the sections to
7392 segments. Create a new segment here, initialise it
7393 and carry on looping. */
7394 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7395 amt
+= section_count
* sizeof (asection
*);
7396 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7403 /* Initialise the fields of the segment map. Set the physical
7404 physical address to the LMA of the first section that has
7405 not yet been assigned. */
7407 map
->p_type
= segment
->p_type
;
7408 map
->p_flags
= segment
->p_flags
;
7409 map
->p_flags_valid
= 1;
7410 map
->p_paddr
= suggested_lma
->lma
* opb
;
7411 map
->p_paddr_valid
= p_paddr_valid
;
7412 map
->includes_filehdr
= 0;
7413 map
->includes_phdrs
= 0;
7418 bfd_set_error (bfd_error_sorry
);
7422 while (isec
< section_count
);
7427 elf_seg_map (obfd
) = map_first
;
7429 /* If we had to estimate the number of program headers that were
7430 going to be needed, then check our estimate now and adjust
7431 the offset if necessary. */
7432 if (phdr_adjust_seg
!= NULL
)
7436 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7439 if (count
> phdr_adjust_num
)
7440 phdr_adjust_seg
->p_paddr
7441 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7443 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7444 if (map
->p_type
== PT_PHDR
)
7447 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7448 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7455 #undef IS_CONTAINED_BY_VMA
7456 #undef IS_CONTAINED_BY_LMA
7458 #undef IS_COREFILE_NOTE
7459 #undef IS_SOLARIS_PT_INTERP
7460 #undef IS_SECTION_IN_INPUT_SEGMENT
7461 #undef INCLUDE_SECTION_IN_SEGMENT
7462 #undef SEGMENT_AFTER_SEGMENT
7463 #undef SEGMENT_OVERLAPS
7467 /* Copy ELF program header information. */
7470 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7472 Elf_Internal_Ehdr
*iehdr
;
7473 struct elf_segment_map
*map
;
7474 struct elf_segment_map
*map_first
;
7475 struct elf_segment_map
**pointer_to_map
;
7476 Elf_Internal_Phdr
*segment
;
7478 unsigned int num_segments
;
7479 bfd_boolean phdr_included
= FALSE
;
7480 bfd_boolean p_paddr_valid
;
7481 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7483 iehdr
= elf_elfheader (ibfd
);
7486 pointer_to_map
= &map_first
;
7488 /* If all the segment p_paddr fields are zero, don't set
7489 map->p_paddr_valid. */
7490 p_paddr_valid
= FALSE
;
7491 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7492 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7495 if (segment
->p_paddr
!= 0)
7497 p_paddr_valid
= TRUE
;
7501 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7506 unsigned int section_count
;
7508 Elf_Internal_Shdr
*this_hdr
;
7509 asection
*first_section
= NULL
;
7510 asection
*lowest_section
;
7512 /* Compute how many sections are in this segment. */
7513 for (section
= ibfd
->sections
, section_count
= 0;
7515 section
= section
->next
)
7517 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7518 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7520 if (first_section
== NULL
)
7521 first_section
= section
;
7526 /* Allocate a segment map big enough to contain
7527 all of the sections we have selected. */
7528 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7529 amt
+= section_count
* sizeof (asection
*);
7530 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7534 /* Initialize the fields of the output segment map with the
7537 map
->p_type
= segment
->p_type
;
7538 map
->p_flags
= segment
->p_flags
;
7539 map
->p_flags_valid
= 1;
7540 map
->p_paddr
= segment
->p_paddr
;
7541 map
->p_paddr_valid
= p_paddr_valid
;
7542 map
->p_align
= segment
->p_align
;
7543 map
->p_align_valid
= 1;
7544 map
->p_vaddr_offset
= 0;
7546 if (map
->p_type
== PT_GNU_RELRO
7547 || map
->p_type
== PT_GNU_STACK
)
7549 /* The PT_GNU_RELRO segment may contain the first a few
7550 bytes in the .got.plt section even if the whole .got.plt
7551 section isn't in the PT_GNU_RELRO segment. We won't
7552 change the size of the PT_GNU_RELRO segment.
7553 Similarly, PT_GNU_STACK size is significant on uclinux
7555 map
->p_size
= segment
->p_memsz
;
7556 map
->p_size_valid
= 1;
7559 /* Determine if this segment contains the ELF file header
7560 and if it contains the program headers themselves. */
7561 map
->includes_filehdr
= (segment
->p_offset
== 0
7562 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7564 map
->includes_phdrs
= 0;
7565 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7567 map
->includes_phdrs
=
7568 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7569 && (segment
->p_offset
+ segment
->p_filesz
7570 >= ((bfd_vma
) iehdr
->e_phoff
7571 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7573 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7574 phdr_included
= TRUE
;
7577 lowest_section
= NULL
;
7578 if (section_count
!= 0)
7580 unsigned int isec
= 0;
7582 for (section
= first_section
;
7584 section
= section
->next
)
7586 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7587 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7589 map
->sections
[isec
++] = section
->output_section
;
7590 if ((section
->flags
& SEC_ALLOC
) != 0)
7594 if (lowest_section
== NULL
7595 || section
->lma
< lowest_section
->lma
)
7596 lowest_section
= section
;
7598 /* Section lmas are set up from PT_LOAD header
7599 p_paddr in _bfd_elf_make_section_from_shdr.
7600 If this header has a p_paddr that disagrees
7601 with the section lma, flag the p_paddr as
7603 if ((section
->flags
& SEC_LOAD
) != 0)
7604 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7606 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7607 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7608 map
->p_paddr_valid
= FALSE
;
7610 if (isec
== section_count
)
7616 if (section_count
== 0)
7617 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7618 else if (map
->p_paddr_valid
)
7620 /* Account for padding before the first section in the segment. */
7621 bfd_vma hdr_size
= 0;
7622 if (map
->includes_filehdr
)
7623 hdr_size
= iehdr
->e_ehsize
;
7624 if (map
->includes_phdrs
)
7625 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7627 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7628 - (lowest_section
? lowest_section
->lma
: 0));
7631 map
->count
= section_count
;
7632 *pointer_to_map
= map
;
7633 pointer_to_map
= &map
->next
;
7636 elf_seg_map (obfd
) = map_first
;
7640 /* Copy private BFD data. This copies or rewrites ELF program header
7644 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7646 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7647 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7650 if (elf_tdata (ibfd
)->phdr
== NULL
)
7653 if (ibfd
->xvec
== obfd
->xvec
)
7655 /* Check to see if any sections in the input BFD
7656 covered by ELF program header have changed. */
7657 Elf_Internal_Phdr
*segment
;
7658 asection
*section
, *osec
;
7659 unsigned int i
, num_segments
;
7660 Elf_Internal_Shdr
*this_hdr
;
7661 const struct elf_backend_data
*bed
;
7663 bed
= get_elf_backend_data (ibfd
);
7665 /* Regenerate the segment map if p_paddr is set to 0. */
7666 if (bed
->want_p_paddr_set_to_zero
)
7669 /* Initialize the segment mark field. */
7670 for (section
= obfd
->sections
; section
!= NULL
;
7671 section
= section
->next
)
7672 section
->segment_mark
= FALSE
;
7674 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7675 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7679 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7680 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7681 which severly confuses things, so always regenerate the segment
7682 map in this case. */
7683 if (segment
->p_paddr
== 0
7684 && segment
->p_memsz
== 0
7685 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7688 for (section
= ibfd
->sections
;
7689 section
!= NULL
; section
= section
->next
)
7691 /* We mark the output section so that we know it comes
7692 from the input BFD. */
7693 osec
= section
->output_section
;
7695 osec
->segment_mark
= TRUE
;
7697 /* Check if this section is covered by the segment. */
7698 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7699 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7701 /* FIXME: Check if its output section is changed or
7702 removed. What else do we need to check? */
7704 || section
->flags
!= osec
->flags
7705 || section
->lma
!= osec
->lma
7706 || section
->vma
!= osec
->vma
7707 || section
->size
!= osec
->size
7708 || section
->rawsize
!= osec
->rawsize
7709 || section
->alignment_power
!= osec
->alignment_power
)
7715 /* Check to see if any output section do not come from the
7717 for (section
= obfd
->sections
; section
!= NULL
;
7718 section
= section
->next
)
7720 if (!section
->segment_mark
)
7723 section
->segment_mark
= FALSE
;
7726 return copy_elf_program_header (ibfd
, obfd
);
7730 if (ibfd
->xvec
== obfd
->xvec
)
7732 /* When rewriting program header, set the output maxpagesize to
7733 the maximum alignment of input PT_LOAD segments. */
7734 Elf_Internal_Phdr
*segment
;
7736 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7737 bfd_vma maxpagesize
= 0;
7739 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7742 if (segment
->p_type
== PT_LOAD
7743 && maxpagesize
< segment
->p_align
)
7745 /* PR 17512: file: f17299af. */
7746 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7747 /* xgettext:c-format */
7748 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7749 PRIx64
" is too large"),
7750 ibfd
, (uint64_t) segment
->p_align
);
7752 maxpagesize
= segment
->p_align
;
7755 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7756 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7759 return rewrite_elf_program_header (ibfd
, obfd
);
7762 /* Initialize private output section information from input section. */
7765 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7769 struct bfd_link_info
*link_info
)
7772 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7773 bfd_boolean final_link
= (link_info
!= NULL
7774 && !bfd_link_relocatable (link_info
));
7776 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7777 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7780 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7782 /* If this is a known ABI section, ELF section type and flags may
7783 have been set up when OSEC was created. For normal sections we
7784 allow the user to override the type and flags other than
7785 SHF_MASKOS and SHF_MASKPROC. */
7786 if (elf_section_type (osec
) == SHT_PROGBITS
7787 || elf_section_type (osec
) == SHT_NOTE
7788 || elf_section_type (osec
) == SHT_NOBITS
)
7789 elf_section_type (osec
) = SHT_NULL
;
7790 /* For objcopy and relocatable link, copy the ELF section type from
7791 the input file if the BFD section flags are the same. (If they
7792 are different the user may be doing something like
7793 "objcopy --set-section-flags .text=alloc,data".) For a final
7794 link allow some flags that the linker clears to differ. */
7795 if (elf_section_type (osec
) == SHT_NULL
7796 && (osec
->flags
== isec
->flags
7798 && ((osec
->flags
^ isec
->flags
)
7799 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7800 elf_section_type (osec
) = elf_section_type (isec
);
7802 /* FIXME: Is this correct for all OS/PROC specific flags? */
7803 elf_section_flags (osec
) = (elf_section_flags (isec
)
7804 & (SHF_MASKOS
| SHF_MASKPROC
));
7806 /* Copy sh_info from input for mbind section. */
7807 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7808 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7809 elf_section_data (osec
)->this_hdr
.sh_info
7810 = elf_section_data (isec
)->this_hdr
.sh_info
;
7812 /* Set things up for objcopy and relocatable link. The output
7813 SHT_GROUP section will have its elf_next_in_group pointing back
7814 to the input group members. Ignore linker created group section.
7815 See elfNN_ia64_object_p in elfxx-ia64.c. */
7816 if ((link_info
== NULL
7817 || !link_info
->resolve_section_groups
)
7818 && (elf_sec_group (isec
) == NULL
7819 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7821 if (elf_section_flags (isec
) & SHF_GROUP
)
7822 elf_section_flags (osec
) |= SHF_GROUP
;
7823 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7824 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7827 /* If not decompress, preserve SHF_COMPRESSED. */
7828 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7829 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7832 ihdr
= &elf_section_data (isec
)->this_hdr
;
7834 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7835 don't use the output section of the linked-to section since it
7836 may be NULL at this point. */
7837 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7839 ohdr
= &elf_section_data (osec
)->this_hdr
;
7840 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7841 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7844 osec
->use_rela_p
= isec
->use_rela_p
;
7849 /* Copy private section information. This copies over the entsize
7850 field, and sometimes the info field. */
7853 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7858 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7860 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7861 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7864 ihdr
= &elf_section_data (isec
)->this_hdr
;
7865 ohdr
= &elf_section_data (osec
)->this_hdr
;
7867 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7869 if (ihdr
->sh_type
== SHT_SYMTAB
7870 || ihdr
->sh_type
== SHT_DYNSYM
7871 || ihdr
->sh_type
== SHT_GNU_verneed
7872 || ihdr
->sh_type
== SHT_GNU_verdef
)
7873 ohdr
->sh_info
= ihdr
->sh_info
;
7875 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7879 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7880 necessary if we are removing either the SHT_GROUP section or any of
7881 the group member sections. DISCARDED is the value that a section's
7882 output_section has if the section will be discarded, NULL when this
7883 function is called from objcopy, bfd_abs_section_ptr when called
7887 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7891 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7892 if (elf_section_type (isec
) == SHT_GROUP
)
7894 asection
*first
= elf_next_in_group (isec
);
7895 asection
*s
= first
;
7896 bfd_size_type removed
= 0;
7900 /* If this member section is being output but the
7901 SHT_GROUP section is not, then clear the group info
7902 set up by _bfd_elf_copy_private_section_data. */
7903 if (s
->output_section
!= discarded
7904 && isec
->output_section
== discarded
)
7906 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7907 elf_group_name (s
->output_section
) = NULL
;
7909 /* Conversely, if the member section is not being output
7910 but the SHT_GROUP section is, then adjust its size. */
7911 else if (s
->output_section
== discarded
7912 && isec
->output_section
!= discarded
)
7914 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7916 if (elf_sec
->rel
.hdr
!= NULL
7917 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7919 if (elf_sec
->rela
.hdr
!= NULL
7920 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7923 s
= elf_next_in_group (s
);
7929 if (discarded
!= NULL
)
7931 /* If we've been called for ld -r, then we need to
7932 adjust the input section size. */
7933 if (isec
->rawsize
== 0)
7934 isec
->rawsize
= isec
->size
;
7935 isec
->size
= isec
->rawsize
- removed
;
7936 if (isec
->size
<= 4)
7939 isec
->flags
|= SEC_EXCLUDE
;
7944 /* Adjust the output section size when called from
7946 isec
->output_section
->size
-= removed
;
7947 if (isec
->output_section
->size
<= 4)
7949 isec
->output_section
->size
= 0;
7950 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7959 /* Copy private header information. */
7962 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7964 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7965 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7968 /* Copy over private BFD data if it has not already been copied.
7969 This must be done here, rather than in the copy_private_bfd_data
7970 entry point, because the latter is called after the section
7971 contents have been set, which means that the program headers have
7972 already been worked out. */
7973 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7975 if (! copy_private_bfd_data (ibfd
, obfd
))
7979 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7982 /* Copy private symbol information. If this symbol is in a section
7983 which we did not map into a BFD section, try to map the section
7984 index correctly. We use special macro definitions for the mapped
7985 section indices; these definitions are interpreted by the
7986 swap_out_syms function. */
7988 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7989 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7990 #define MAP_STRTAB (SHN_HIOS + 3)
7991 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7992 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7995 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8000 elf_symbol_type
*isym
, *osym
;
8002 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8003 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8006 isym
= elf_symbol_from (ibfd
, isymarg
);
8007 osym
= elf_symbol_from (obfd
, osymarg
);
8010 && isym
->internal_elf_sym
.st_shndx
!= 0
8012 && bfd_is_abs_section (isym
->symbol
.section
))
8016 shndx
= isym
->internal_elf_sym
.st_shndx
;
8017 if (shndx
== elf_onesymtab (ibfd
))
8018 shndx
= MAP_ONESYMTAB
;
8019 else if (shndx
== elf_dynsymtab (ibfd
))
8020 shndx
= MAP_DYNSYMTAB
;
8021 else if (shndx
== elf_strtab_sec (ibfd
))
8023 else if (shndx
== elf_shstrtab_sec (ibfd
))
8024 shndx
= MAP_SHSTRTAB
;
8025 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8026 shndx
= MAP_SYM_SHNDX
;
8027 osym
->internal_elf_sym
.st_shndx
= shndx
;
8033 /* Swap out the symbols. */
8036 swap_out_syms (bfd
*abfd
,
8037 struct elf_strtab_hash
**sttp
,
8040 const struct elf_backend_data
*bed
;
8041 unsigned int symcount
;
8043 struct elf_strtab_hash
*stt
;
8044 Elf_Internal_Shdr
*symtab_hdr
;
8045 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8046 Elf_Internal_Shdr
*symstrtab_hdr
;
8047 struct elf_sym_strtab
*symstrtab
;
8048 bfd_byte
*outbound_syms
;
8049 bfd_byte
*outbound_shndx
;
8050 unsigned long outbound_syms_index
;
8051 unsigned long outbound_shndx_index
;
8053 unsigned int num_locals
;
8055 bfd_boolean name_local_sections
;
8057 if (!elf_map_symbols (abfd
, &num_locals
))
8060 /* Dump out the symtabs. */
8061 stt
= _bfd_elf_strtab_init ();
8065 bed
= get_elf_backend_data (abfd
);
8066 symcount
= bfd_get_symcount (abfd
);
8067 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8068 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8069 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8070 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8071 symtab_hdr
->sh_info
= num_locals
+ 1;
8072 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8074 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8075 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8077 /* Allocate buffer to swap out the .strtab section. */
8078 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8079 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8081 bfd_set_error (bfd_error_no_memory
);
8082 _bfd_elf_strtab_free (stt
);
8086 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8087 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8090 bfd_set_error (bfd_error_no_memory
);
8093 _bfd_elf_strtab_free (stt
);
8096 symtab_hdr
->contents
= outbound_syms
;
8097 outbound_syms_index
= 0;
8099 outbound_shndx
= NULL
;
8100 outbound_shndx_index
= 0;
8102 if (elf_symtab_shndx_list (abfd
))
8104 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8105 if (symtab_shndx_hdr
->sh_name
!= 0)
8107 if (_bfd_mul_overflow (symcount
+ 1,
8108 sizeof (Elf_External_Sym_Shndx
), &amt
))
8110 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8111 if (outbound_shndx
== NULL
)
8114 symtab_shndx_hdr
->contents
= outbound_shndx
;
8115 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8116 symtab_shndx_hdr
->sh_size
= amt
;
8117 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8118 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8120 /* FIXME: What about any other headers in the list ? */
8123 /* Now generate the data (for "contents"). */
8125 /* Fill in zeroth symbol and swap it out. */
8126 Elf_Internal_Sym sym
;
8132 sym
.st_shndx
= SHN_UNDEF
;
8133 sym
.st_target_internal
= 0;
8134 symstrtab
[0].sym
= sym
;
8135 symstrtab
[0].dest_index
= outbound_syms_index
;
8136 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8137 outbound_syms_index
++;
8138 if (outbound_shndx
!= NULL
)
8139 outbound_shndx_index
++;
8143 = (bed
->elf_backend_name_local_section_symbols
8144 && bed
->elf_backend_name_local_section_symbols (abfd
));
8146 syms
= bfd_get_outsymbols (abfd
);
8147 for (idx
= 0; idx
< symcount
;)
8149 Elf_Internal_Sym sym
;
8150 bfd_vma value
= syms
[idx
]->value
;
8151 elf_symbol_type
*type_ptr
;
8152 flagword flags
= syms
[idx
]->flags
;
8155 if (!name_local_sections
8156 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8158 /* Local section symbols have no name. */
8159 sym
.st_name
= (unsigned long) -1;
8163 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8164 to get the final offset for st_name. */
8166 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8168 if (sym
.st_name
== (unsigned long) -1)
8172 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
8174 if ((flags
& BSF_SECTION_SYM
) == 0
8175 && bfd_is_com_section (syms
[idx
]->section
))
8177 /* ELF common symbols put the alignment into the `value' field,
8178 and the size into the `size' field. This is backwards from
8179 how BFD handles it, so reverse it here. */
8180 sym
.st_size
= value
;
8181 if (type_ptr
== NULL
8182 || type_ptr
->internal_elf_sym
.st_value
== 0)
8183 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8185 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8186 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8187 (abfd
, syms
[idx
]->section
);
8191 asection
*sec
= syms
[idx
]->section
;
8194 if (sec
->output_section
)
8196 value
+= sec
->output_offset
;
8197 sec
= sec
->output_section
;
8200 /* Don't add in the section vma for relocatable output. */
8201 if (! relocatable_p
)
8203 sym
.st_value
= value
;
8204 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8206 if (bfd_is_abs_section (sec
)
8208 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8210 /* This symbol is in a real ELF section which we did
8211 not create as a BFD section. Undo the mapping done
8212 by copy_private_symbol_data. */
8213 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8217 shndx
= elf_onesymtab (abfd
);
8220 shndx
= elf_dynsymtab (abfd
);
8223 shndx
= elf_strtab_sec (abfd
);
8226 shndx
= elf_shstrtab_sec (abfd
);
8229 if (elf_symtab_shndx_list (abfd
))
8230 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8237 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8239 if (bed
->symbol_section_index
)
8240 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8241 /* Otherwise just leave the index alone. */
8245 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8246 _bfd_error_handler (_("%pB: \
8247 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8256 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8258 if (shndx
== SHN_BAD
)
8262 /* Writing this would be a hell of a lot easier if
8263 we had some decent documentation on bfd, and
8264 knew what to expect of the library, and what to
8265 demand of applications. For example, it
8266 appears that `objcopy' might not set the
8267 section of a symbol to be a section that is
8268 actually in the output file. */
8269 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8271 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8272 if (shndx
== SHN_BAD
)
8274 /* xgettext:c-format */
8276 (_("unable to find equivalent output section"
8277 " for symbol '%s' from section '%s'"),
8278 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8280 bfd_set_error (bfd_error_invalid_operation
);
8286 sym
.st_shndx
= shndx
;
8289 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8291 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8292 type
= STT_GNU_IFUNC
;
8293 else if ((flags
& BSF_FUNCTION
) != 0)
8295 else if ((flags
& BSF_OBJECT
) != 0)
8297 else if ((flags
& BSF_RELC
) != 0)
8299 else if ((flags
& BSF_SRELC
) != 0)
8304 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8307 /* Processor-specific types. */
8308 if (type_ptr
!= NULL
8309 && bed
->elf_backend_get_symbol_type
)
8310 type
= ((*bed
->elf_backend_get_symbol_type
)
8311 (&type_ptr
->internal_elf_sym
, type
));
8313 if (flags
& BSF_SECTION_SYM
)
8315 if (flags
& BSF_GLOBAL
)
8316 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8318 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8320 else if (bfd_is_com_section (syms
[idx
]->section
))
8322 if (type
!= STT_TLS
)
8324 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8325 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8326 ? STT_COMMON
: STT_OBJECT
);
8328 type
= ((flags
& BSF_ELF_COMMON
) != 0
8329 ? STT_COMMON
: STT_OBJECT
);
8331 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8333 else if (bfd_is_und_section (syms
[idx
]->section
))
8334 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8338 else if (flags
& BSF_FILE
)
8339 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8342 int bind
= STB_LOCAL
;
8344 if (flags
& BSF_LOCAL
)
8346 else if (flags
& BSF_GNU_UNIQUE
)
8347 bind
= STB_GNU_UNIQUE
;
8348 else if (flags
& BSF_WEAK
)
8350 else if (flags
& BSF_GLOBAL
)
8353 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8356 if (type_ptr
!= NULL
)
8358 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8359 sym
.st_target_internal
8360 = type_ptr
->internal_elf_sym
.st_target_internal
;
8365 sym
.st_target_internal
= 0;
8369 symstrtab
[idx
].sym
= sym
;
8370 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8371 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8373 outbound_syms_index
++;
8374 if (outbound_shndx
!= NULL
)
8375 outbound_shndx_index
++;
8378 /* Finalize the .strtab section. */
8379 _bfd_elf_strtab_finalize (stt
);
8381 /* Swap out the .strtab section. */
8382 for (idx
= 0; idx
<= symcount
; idx
++)
8384 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8385 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8386 elfsym
->sym
.st_name
= 0;
8388 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8389 elfsym
->sym
.st_name
);
8390 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8392 + (elfsym
->dest_index
8393 * bed
->s
->sizeof_sym
)),
8395 + (elfsym
->destshndx_index
8396 * sizeof (Elf_External_Sym_Shndx
))));
8401 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8402 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8403 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8404 symstrtab_hdr
->sh_addr
= 0;
8405 symstrtab_hdr
->sh_entsize
= 0;
8406 symstrtab_hdr
->sh_link
= 0;
8407 symstrtab_hdr
->sh_info
= 0;
8408 symstrtab_hdr
->sh_addralign
= 1;
8413 /* Return the number of bytes required to hold the symtab vector.
8415 Note that we base it on the count plus 1, since we will null terminate
8416 the vector allocated based on this size. However, the ELF symbol table
8417 always has a dummy entry as symbol #0, so it ends up even. */
8420 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8422 bfd_size_type symcount
;
8424 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8426 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8427 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8429 bfd_set_error (bfd_error_file_too_big
);
8432 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8434 symtab_size
-= sizeof (asymbol
*);
8440 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8442 bfd_size_type symcount
;
8444 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8446 if (elf_dynsymtab (abfd
) == 0)
8448 bfd_set_error (bfd_error_invalid_operation
);
8452 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8453 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8455 bfd_set_error (bfd_error_file_too_big
);
8458 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8460 symtab_size
-= sizeof (asymbol
*);
8466 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8469 #if SIZEOF_LONG == SIZEOF_INT
8470 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8472 bfd_set_error (bfd_error_file_too_big
);
8476 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8479 /* Canonicalize the relocs. */
8482 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8489 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8491 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8494 tblptr
= section
->relocation
;
8495 for (i
= 0; i
< section
->reloc_count
; i
++)
8496 *relptr
++ = tblptr
++;
8500 return section
->reloc_count
;
8504 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8506 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8507 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8510 abfd
->symcount
= symcount
;
8515 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8516 asymbol
**allocation
)
8518 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8519 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8522 abfd
->dynsymcount
= symcount
;
8526 /* Return the size required for the dynamic reloc entries. Any loadable
8527 section that was actually installed in the BFD, and has type SHT_REL
8528 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8529 dynamic reloc section. */
8532 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8534 bfd_size_type count
;
8537 if (elf_dynsymtab (abfd
) == 0)
8539 bfd_set_error (bfd_error_invalid_operation
);
8544 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8545 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8546 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8547 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8549 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8550 if (count
> LONG_MAX
/ sizeof (arelent
*))
8552 bfd_set_error (bfd_error_file_too_big
);
8556 return count
* sizeof (arelent
*);
8559 /* Canonicalize the dynamic relocation entries. Note that we return the
8560 dynamic relocations as a single block, although they are actually
8561 associated with particular sections; the interface, which was
8562 designed for SunOS style shared libraries, expects that there is only
8563 one set of dynamic relocs. Any loadable section that was actually
8564 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8565 dynamic symbol table, is considered to be a dynamic reloc section. */
8568 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8572 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8576 if (elf_dynsymtab (abfd
) == 0)
8578 bfd_set_error (bfd_error_invalid_operation
);
8582 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8584 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8586 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8587 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8588 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8593 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8595 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8597 for (i
= 0; i
< count
; i
++)
8608 /* Read in the version information. */
8611 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8613 bfd_byte
*contents
= NULL
;
8614 unsigned int freeidx
= 0;
8617 if (elf_dynverref (abfd
) != 0)
8619 Elf_Internal_Shdr
*hdr
;
8620 Elf_External_Verneed
*everneed
;
8621 Elf_Internal_Verneed
*iverneed
;
8623 bfd_byte
*contents_end
;
8625 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8627 if (hdr
->sh_info
== 0
8628 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8630 error_return_bad_verref
:
8632 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8633 bfd_set_error (bfd_error_bad_value
);
8634 error_return_verref
:
8635 elf_tdata (abfd
)->verref
= NULL
;
8636 elf_tdata (abfd
)->cverrefs
= 0;
8640 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8641 goto error_return_verref
;
8642 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8643 if (contents
== NULL
)
8644 goto error_return_verref
;
8646 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8648 bfd_set_error (bfd_error_file_too_big
);
8649 goto error_return_verref
;
8651 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8652 if (elf_tdata (abfd
)->verref
== NULL
)
8653 goto error_return_verref
;
8655 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8656 == sizeof (Elf_External_Vernaux
));
8657 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8658 everneed
= (Elf_External_Verneed
*) contents
;
8659 iverneed
= elf_tdata (abfd
)->verref
;
8660 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8662 Elf_External_Vernaux
*evernaux
;
8663 Elf_Internal_Vernaux
*ivernaux
;
8666 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8668 iverneed
->vn_bfd
= abfd
;
8670 iverneed
->vn_filename
=
8671 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8673 if (iverneed
->vn_filename
== NULL
)
8674 goto error_return_bad_verref
;
8676 if (iverneed
->vn_cnt
== 0)
8677 iverneed
->vn_auxptr
= NULL
;
8680 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8681 sizeof (Elf_Internal_Vernaux
), &amt
))
8683 bfd_set_error (bfd_error_file_too_big
);
8684 goto error_return_verref
;
8686 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8687 bfd_alloc (abfd
, amt
);
8688 if (iverneed
->vn_auxptr
== NULL
)
8689 goto error_return_verref
;
8692 if (iverneed
->vn_aux
8693 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8694 goto error_return_bad_verref
;
8696 evernaux
= ((Elf_External_Vernaux
*)
8697 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8698 ivernaux
= iverneed
->vn_auxptr
;
8699 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8701 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8703 ivernaux
->vna_nodename
=
8704 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8705 ivernaux
->vna_name
);
8706 if (ivernaux
->vna_nodename
== NULL
)
8707 goto error_return_bad_verref
;
8709 if (ivernaux
->vna_other
> freeidx
)
8710 freeidx
= ivernaux
->vna_other
;
8712 ivernaux
->vna_nextptr
= NULL
;
8713 if (ivernaux
->vna_next
== 0)
8715 iverneed
->vn_cnt
= j
+ 1;
8718 if (j
+ 1 < iverneed
->vn_cnt
)
8719 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8721 if (ivernaux
->vna_next
8722 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8723 goto error_return_bad_verref
;
8725 evernaux
= ((Elf_External_Vernaux
*)
8726 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8729 iverneed
->vn_nextref
= NULL
;
8730 if (iverneed
->vn_next
== 0)
8732 if (i
+ 1 < hdr
->sh_info
)
8733 iverneed
->vn_nextref
= iverneed
+ 1;
8735 if (iverneed
->vn_next
8736 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8737 goto error_return_bad_verref
;
8739 everneed
= ((Elf_External_Verneed
*)
8740 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8742 elf_tdata (abfd
)->cverrefs
= i
;
8748 if (elf_dynverdef (abfd
) != 0)
8750 Elf_Internal_Shdr
*hdr
;
8751 Elf_External_Verdef
*everdef
;
8752 Elf_Internal_Verdef
*iverdef
;
8753 Elf_Internal_Verdef
*iverdefarr
;
8754 Elf_Internal_Verdef iverdefmem
;
8756 unsigned int maxidx
;
8757 bfd_byte
*contents_end_def
, *contents_end_aux
;
8759 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8761 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8763 error_return_bad_verdef
:
8765 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8766 bfd_set_error (bfd_error_bad_value
);
8767 error_return_verdef
:
8768 elf_tdata (abfd
)->verdef
= NULL
;
8769 elf_tdata (abfd
)->cverdefs
= 0;
8773 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8774 goto error_return_verdef
;
8775 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8776 if (contents
== NULL
)
8777 goto error_return_verdef
;
8779 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8780 >= sizeof (Elf_External_Verdaux
));
8781 contents_end_def
= contents
+ hdr
->sh_size
8782 - sizeof (Elf_External_Verdef
);
8783 contents_end_aux
= contents
+ hdr
->sh_size
8784 - sizeof (Elf_External_Verdaux
);
8786 /* We know the number of entries in the section but not the maximum
8787 index. Therefore we have to run through all entries and find
8789 everdef
= (Elf_External_Verdef
*) contents
;
8791 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8793 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8795 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8796 goto error_return_bad_verdef
;
8797 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8798 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8800 if (iverdefmem
.vd_next
== 0)
8803 if (iverdefmem
.vd_next
8804 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8805 goto error_return_bad_verdef
;
8807 everdef
= ((Elf_External_Verdef
*)
8808 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8811 if (default_imported_symver
)
8813 if (freeidx
> maxidx
)
8818 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8820 bfd_set_error (bfd_error_file_too_big
);
8821 goto error_return_verdef
;
8823 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8824 if (elf_tdata (abfd
)->verdef
== NULL
)
8825 goto error_return_verdef
;
8827 elf_tdata (abfd
)->cverdefs
= maxidx
;
8829 everdef
= (Elf_External_Verdef
*) contents
;
8830 iverdefarr
= elf_tdata (abfd
)->verdef
;
8831 for (i
= 0; i
< hdr
->sh_info
; i
++)
8833 Elf_External_Verdaux
*everdaux
;
8834 Elf_Internal_Verdaux
*iverdaux
;
8837 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8839 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8840 goto error_return_bad_verdef
;
8842 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8843 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8845 iverdef
->vd_bfd
= abfd
;
8847 if (iverdef
->vd_cnt
== 0)
8848 iverdef
->vd_auxptr
= NULL
;
8851 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
8852 sizeof (Elf_Internal_Verdaux
), &amt
))
8854 bfd_set_error (bfd_error_file_too_big
);
8855 goto error_return_verdef
;
8857 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8858 bfd_alloc (abfd
, amt
);
8859 if (iverdef
->vd_auxptr
== NULL
)
8860 goto error_return_verdef
;
8864 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8865 goto error_return_bad_verdef
;
8867 everdaux
= ((Elf_External_Verdaux
*)
8868 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8869 iverdaux
= iverdef
->vd_auxptr
;
8870 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8872 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8874 iverdaux
->vda_nodename
=
8875 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8876 iverdaux
->vda_name
);
8877 if (iverdaux
->vda_nodename
== NULL
)
8878 goto error_return_bad_verdef
;
8880 iverdaux
->vda_nextptr
= NULL
;
8881 if (iverdaux
->vda_next
== 0)
8883 iverdef
->vd_cnt
= j
+ 1;
8886 if (j
+ 1 < iverdef
->vd_cnt
)
8887 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8889 if (iverdaux
->vda_next
8890 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8891 goto error_return_bad_verdef
;
8893 everdaux
= ((Elf_External_Verdaux
*)
8894 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8897 iverdef
->vd_nodename
= NULL
;
8898 if (iverdef
->vd_cnt
)
8899 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8901 iverdef
->vd_nextdef
= NULL
;
8902 if (iverdef
->vd_next
== 0)
8904 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8905 iverdef
->vd_nextdef
= iverdef
+ 1;
8907 everdef
= ((Elf_External_Verdef
*)
8908 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8914 else if (default_imported_symver
)
8921 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8923 bfd_set_error (bfd_error_file_too_big
);
8926 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8927 if (elf_tdata (abfd
)->verdef
== NULL
)
8930 elf_tdata (abfd
)->cverdefs
= freeidx
;
8933 /* Create a default version based on the soname. */
8934 if (default_imported_symver
)
8936 Elf_Internal_Verdef
*iverdef
;
8937 Elf_Internal_Verdaux
*iverdaux
;
8939 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8941 iverdef
->vd_version
= VER_DEF_CURRENT
;
8942 iverdef
->vd_flags
= 0;
8943 iverdef
->vd_ndx
= freeidx
;
8944 iverdef
->vd_cnt
= 1;
8946 iverdef
->vd_bfd
= abfd
;
8948 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8949 if (iverdef
->vd_nodename
== NULL
)
8950 goto error_return_verdef
;
8951 iverdef
->vd_nextdef
= NULL
;
8952 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8953 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8954 if (iverdef
->vd_auxptr
== NULL
)
8955 goto error_return_verdef
;
8957 iverdaux
= iverdef
->vd_auxptr
;
8958 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8964 if (contents
!= NULL
)
8970 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8972 elf_symbol_type
*newsym
;
8974 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
8977 newsym
->symbol
.the_bfd
= abfd
;
8978 return &newsym
->symbol
;
8982 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8986 bfd_symbol_info (symbol
, ret
);
8989 /* Return whether a symbol name implies a local symbol. Most targets
8990 use this function for the is_local_label_name entry point, but some
8994 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8997 /* Normal local symbols start with ``.L''. */
8998 if (name
[0] == '.' && name
[1] == 'L')
9001 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9002 DWARF debugging symbols starting with ``..''. */
9003 if (name
[0] == '.' && name
[1] == '.')
9006 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9007 emitting DWARF debugging output. I suspect this is actually a
9008 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9009 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9010 underscore to be emitted on some ELF targets). For ease of use,
9011 we treat such symbols as local. */
9012 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9015 /* Treat assembler generated fake symbols, dollar local labels and
9016 forward-backward labels (aka local labels) as locals.
9017 These labels have the form:
9019 L0^A.* (fake symbols)
9021 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9023 Versions which start with .L will have already been matched above,
9024 so we only need to match the rest. */
9025 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9027 bfd_boolean ret
= FALSE
;
9031 for (p
= name
+ 2; (c
= *p
); p
++)
9033 if (c
== 1 || c
== 2)
9035 if (c
== 1 && p
== name
+ 2)
9036 /* A fake symbol. */
9039 /* FIXME: We are being paranoid here and treating symbols like
9040 L0^Bfoo as if there were non-local, on the grounds that the
9041 assembler will never generate them. But can any symbol
9042 containing an ASCII value in the range 1-31 ever be anything
9043 other than some kind of local ? */
9060 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9061 asymbol
*symbol ATTRIBUTE_UNUSED
)
9068 _bfd_elf_set_arch_mach (bfd
*abfd
,
9069 enum bfd_architecture arch
,
9070 unsigned long machine
)
9072 /* If this isn't the right architecture for this backend, and this
9073 isn't the generic backend, fail. */
9074 if (arch
!= get_elf_backend_data (abfd
)->arch
9075 && arch
!= bfd_arch_unknown
9076 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9079 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9082 /* Find the nearest line to a particular section and offset,
9083 for error reporting. */
9086 _bfd_elf_find_nearest_line (bfd
*abfd
,
9090 const char **filename_ptr
,
9091 const char **functionname_ptr
,
9092 unsigned int *line_ptr
,
9093 unsigned int *discriminator_ptr
)
9097 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
9098 filename_ptr
, functionname_ptr
,
9099 line_ptr
, discriminator_ptr
,
9100 dwarf_debug_sections
,
9101 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9104 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9105 filename_ptr
, functionname_ptr
, line_ptr
))
9107 if (!*functionname_ptr
)
9108 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9109 *filename_ptr
? NULL
: filename_ptr
,
9114 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9115 &found
, filename_ptr
,
9116 functionname_ptr
, line_ptr
,
9117 &elf_tdata (abfd
)->line_info
))
9119 if (found
&& (*functionname_ptr
|| *line_ptr
))
9122 if (symbols
== NULL
)
9125 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9126 filename_ptr
, functionname_ptr
))
9133 /* Find the line for a symbol. */
9136 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9137 const char **filename_ptr
, unsigned int *line_ptr
)
9139 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9140 filename_ptr
, NULL
, line_ptr
, NULL
,
9141 dwarf_debug_sections
,
9142 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9145 /* After a call to bfd_find_nearest_line, successive calls to
9146 bfd_find_inliner_info can be used to get source information about
9147 each level of function inlining that terminated at the address
9148 passed to bfd_find_nearest_line. Currently this is only supported
9149 for DWARF2 with appropriate DWARF3 extensions. */
9152 _bfd_elf_find_inliner_info (bfd
*abfd
,
9153 const char **filename_ptr
,
9154 const char **functionname_ptr
,
9155 unsigned int *line_ptr
)
9158 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9159 functionname_ptr
, line_ptr
,
9160 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9165 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9167 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9168 int ret
= bed
->s
->sizeof_ehdr
;
9170 if (!bfd_link_relocatable (info
))
9172 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9174 if (phdr_size
== (bfd_size_type
) -1)
9176 struct elf_segment_map
*m
;
9179 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9180 phdr_size
+= bed
->s
->sizeof_phdr
;
9183 phdr_size
= get_program_header_size (abfd
, info
);
9186 elf_program_header_size (abfd
) = phdr_size
;
9194 _bfd_elf_set_section_contents (bfd
*abfd
,
9196 const void *location
,
9198 bfd_size_type count
)
9200 Elf_Internal_Shdr
*hdr
;
9203 if (! abfd
->output_has_begun
9204 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9210 hdr
= &elf_section_data (section
)->this_hdr
;
9211 if (hdr
->sh_offset
== (file_ptr
) -1)
9213 unsigned char *contents
;
9215 if (bfd_section_is_ctf (section
))
9216 /* Nothing to do with this section: the contents are generated
9220 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9223 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9225 bfd_set_error (bfd_error_invalid_operation
);
9229 if ((offset
+ count
) > hdr
->sh_size
)
9232 (_("%pB:%pA: error: attempting to write over the end of the section"),
9235 bfd_set_error (bfd_error_invalid_operation
);
9239 contents
= hdr
->contents
;
9240 if (contents
== NULL
)
9243 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9246 bfd_set_error (bfd_error_invalid_operation
);
9250 memcpy (contents
+ offset
, location
, count
);
9254 pos
= hdr
->sh_offset
+ offset
;
9255 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9256 || bfd_bwrite (location
, count
, abfd
) != count
)
9263 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9264 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9265 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9271 /* Try to convert a non-ELF reloc into an ELF one. */
9274 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9276 /* Check whether we really have an ELF howto. */
9278 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9280 bfd_reloc_code_real_type code
;
9281 reloc_howto_type
*howto
;
9283 /* Alien reloc: Try to determine its type to replace it with an
9284 equivalent ELF reloc. */
9286 if (areloc
->howto
->pc_relative
)
9288 switch (areloc
->howto
->bitsize
)
9291 code
= BFD_RELOC_8_PCREL
;
9294 code
= BFD_RELOC_12_PCREL
;
9297 code
= BFD_RELOC_16_PCREL
;
9300 code
= BFD_RELOC_24_PCREL
;
9303 code
= BFD_RELOC_32_PCREL
;
9306 code
= BFD_RELOC_64_PCREL
;
9312 howto
= bfd_reloc_type_lookup (abfd
, code
);
9314 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9316 if (howto
->pcrel_offset
)
9317 areloc
->addend
+= areloc
->address
;
9319 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9324 switch (areloc
->howto
->bitsize
)
9330 code
= BFD_RELOC_14
;
9333 code
= BFD_RELOC_16
;
9336 code
= BFD_RELOC_26
;
9339 code
= BFD_RELOC_32
;
9342 code
= BFD_RELOC_64
;
9348 howto
= bfd_reloc_type_lookup (abfd
, code
);
9352 areloc
->howto
= howto
;
9360 /* xgettext:c-format */
9361 _bfd_error_handler (_("%pB: %s unsupported"),
9362 abfd
, areloc
->howto
->name
);
9363 bfd_set_error (bfd_error_sorry
);
9368 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9370 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9371 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9373 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9374 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9375 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9378 return _bfd_generic_close_and_cleanup (abfd
);
9381 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9382 in the relocation's offset. Thus we cannot allow any sort of sanity
9383 range-checking to interfere. There is nothing else to do in processing
9386 bfd_reloc_status_type
9387 _bfd_elf_rel_vtable_reloc_fn
9388 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9389 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9390 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9391 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9393 return bfd_reloc_ok
;
9396 /* Elf core file support. Much of this only works on native
9397 toolchains, since we rely on knowing the
9398 machine-dependent procfs structure in order to pick
9399 out details about the corefile. */
9401 #ifdef HAVE_SYS_PROCFS_H
9402 /* Needed for new procfs interface on sparc-solaris. */
9403 # define _STRUCTURED_PROC 1
9404 # include <sys/procfs.h>
9407 /* Return a PID that identifies a "thread" for threaded cores, or the
9408 PID of the main process for non-threaded cores. */
9411 elfcore_make_pid (bfd
*abfd
)
9415 pid
= elf_tdata (abfd
)->core
->lwpid
;
9417 pid
= elf_tdata (abfd
)->core
->pid
;
9422 /* If there isn't a section called NAME, make one, using
9423 data from SECT. Note, this function will generate a
9424 reference to NAME, so you shouldn't deallocate or
9428 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9432 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9435 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9439 sect2
->size
= sect
->size
;
9440 sect2
->filepos
= sect
->filepos
;
9441 sect2
->alignment_power
= sect
->alignment_power
;
9445 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9446 actually creates up to two pseudosections:
9447 - For the single-threaded case, a section named NAME, unless
9448 such a section already exists.
9449 - For the multi-threaded case, a section named "NAME/PID", where
9450 PID is elfcore_make_pid (abfd).
9451 Both pseudosections have identical contents. */
9453 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9459 char *threaded_name
;
9463 /* Build the section name. */
9465 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9466 len
= strlen (buf
) + 1;
9467 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9468 if (threaded_name
== NULL
)
9470 memcpy (threaded_name
, buf
, len
);
9472 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9477 sect
->filepos
= filepos
;
9478 sect
->alignment_power
= 2;
9480 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9484 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9487 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9493 sect
->size
= note
->descsz
- offs
;
9494 sect
->filepos
= note
->descpos
+ offs
;
9495 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9500 /* prstatus_t exists on:
9502 linux 2.[01] + glibc
9506 #if defined (HAVE_PRSTATUS_T)
9509 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9514 if (note
->descsz
== sizeof (prstatus_t
))
9518 size
= sizeof (prstat
.pr_reg
);
9519 offset
= offsetof (prstatus_t
, pr_reg
);
9520 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9522 /* Do not overwrite the core signal if it
9523 has already been set by another thread. */
9524 if (elf_tdata (abfd
)->core
->signal
== 0)
9525 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9526 if (elf_tdata (abfd
)->core
->pid
== 0)
9527 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9529 /* pr_who exists on:
9532 pr_who doesn't exist on:
9535 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9536 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9538 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9541 #if defined (HAVE_PRSTATUS32_T)
9542 else if (note
->descsz
== sizeof (prstatus32_t
))
9544 /* 64-bit host, 32-bit corefile */
9545 prstatus32_t prstat
;
9547 size
= sizeof (prstat
.pr_reg
);
9548 offset
= offsetof (prstatus32_t
, pr_reg
);
9549 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9551 /* Do not overwrite the core signal if it
9552 has already been set by another thread. */
9553 if (elf_tdata (abfd
)->core
->signal
== 0)
9554 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9555 if (elf_tdata (abfd
)->core
->pid
== 0)
9556 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9558 /* pr_who exists on:
9561 pr_who doesn't exist on:
9564 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9565 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9567 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9570 #endif /* HAVE_PRSTATUS32_T */
9573 /* Fail - we don't know how to handle any other
9574 note size (ie. data object type). */
9578 /* Make a ".reg/999" section and a ".reg" section. */
9579 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9580 size
, note
->descpos
+ offset
);
9582 #endif /* defined (HAVE_PRSTATUS_T) */
9584 /* Create a pseudosection containing the exact contents of NOTE. */
9586 elfcore_make_note_pseudosection (bfd
*abfd
,
9588 Elf_Internal_Note
*note
)
9590 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9591 note
->descsz
, note
->descpos
);
9594 /* There isn't a consistent prfpregset_t across platforms,
9595 but it doesn't matter, because we don't have to pick this
9596 data structure apart. */
9599 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9601 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9604 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9605 type of NT_PRXFPREG. Just include the whole note's contents
9609 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9611 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9614 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9615 with a note type of NT_X86_XSTATE. Just include the whole note's
9616 contents literally. */
9619 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9621 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9625 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9627 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9631 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9633 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9637 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9639 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9643 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9645 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9649 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9651 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9655 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9657 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9661 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9663 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9667 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9669 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9673 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9675 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9679 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9681 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9685 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9687 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9691 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9693 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9697 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9699 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9703 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9705 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9709 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9711 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9715 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9717 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9721 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9723 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9727 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9729 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9733 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9735 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9739 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9741 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9745 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9747 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9751 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9753 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9757 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9759 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9763 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9765 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9769 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9771 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9775 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9777 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9781 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9783 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9787 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9789 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9793 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9795 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9799 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9801 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9805 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9807 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9811 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9813 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9817 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9819 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9823 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9825 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9828 #if defined (HAVE_PRPSINFO_T)
9829 typedef prpsinfo_t elfcore_psinfo_t
;
9830 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9831 typedef prpsinfo32_t elfcore_psinfo32_t
;
9835 #if defined (HAVE_PSINFO_T)
9836 typedef psinfo_t elfcore_psinfo_t
;
9837 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9838 typedef psinfo32_t elfcore_psinfo32_t
;
9842 /* return a malloc'ed copy of a string at START which is at
9843 most MAX bytes long, possibly without a terminating '\0'.
9844 the copy will always have a terminating '\0'. */
9847 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9850 char *end
= (char *) memchr (start
, '\0', max
);
9858 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9862 memcpy (dups
, start
, len
);
9868 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9870 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9872 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9874 elfcore_psinfo_t psinfo
;
9876 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9878 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9879 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9881 elf_tdata (abfd
)->core
->program
9882 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9883 sizeof (psinfo
.pr_fname
));
9885 elf_tdata (abfd
)->core
->command
9886 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9887 sizeof (psinfo
.pr_psargs
));
9889 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9890 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9892 /* 64-bit host, 32-bit corefile */
9893 elfcore_psinfo32_t psinfo
;
9895 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9897 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9898 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9900 elf_tdata (abfd
)->core
->program
9901 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9902 sizeof (psinfo
.pr_fname
));
9904 elf_tdata (abfd
)->core
->command
9905 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9906 sizeof (psinfo
.pr_psargs
));
9912 /* Fail - we don't know how to handle any other
9913 note size (ie. data object type). */
9917 /* Note that for some reason, a spurious space is tacked
9918 onto the end of the args in some (at least one anyway)
9919 implementations, so strip it off if it exists. */
9922 char *command
= elf_tdata (abfd
)->core
->command
;
9923 int n
= strlen (command
);
9925 if (0 < n
&& command
[n
- 1] == ' ')
9926 command
[n
- 1] = '\0';
9931 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9933 #if defined (HAVE_PSTATUS_T)
9935 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9937 if (note
->descsz
== sizeof (pstatus_t
)
9938 #if defined (HAVE_PXSTATUS_T)
9939 || note
->descsz
== sizeof (pxstatus_t
)
9945 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9947 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9949 #if defined (HAVE_PSTATUS32_T)
9950 else if (note
->descsz
== sizeof (pstatus32_t
))
9952 /* 64-bit host, 32-bit corefile */
9955 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9957 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9960 /* Could grab some more details from the "representative"
9961 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9962 NT_LWPSTATUS note, presumably. */
9966 #endif /* defined (HAVE_PSTATUS_T) */
9968 #if defined (HAVE_LWPSTATUS_T)
9970 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9972 lwpstatus_t lwpstat
;
9978 if (note
->descsz
!= sizeof (lwpstat
)
9979 #if defined (HAVE_LWPXSTATUS_T)
9980 && note
->descsz
!= sizeof (lwpxstatus_t
)
9985 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9987 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9988 /* Do not overwrite the core signal if it has already been set by
9990 if (elf_tdata (abfd
)->core
->signal
== 0)
9991 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9993 /* Make a ".reg/999" section. */
9995 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9996 len
= strlen (buf
) + 1;
9997 name
= bfd_alloc (abfd
, len
);
10000 memcpy (name
, buf
, len
);
10002 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10006 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10007 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10008 sect
->filepos
= note
->descpos
10009 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10012 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10013 sect
->size
= sizeof (lwpstat
.pr_reg
);
10014 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10017 sect
->alignment_power
= 2;
10019 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10022 /* Make a ".reg2/999" section */
10024 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10025 len
= strlen (buf
) + 1;
10026 name
= bfd_alloc (abfd
, len
);
10029 memcpy (name
, buf
, len
);
10031 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10035 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10036 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10037 sect
->filepos
= note
->descpos
10038 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10041 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10042 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10043 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10046 sect
->alignment_power
= 2;
10048 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10050 #endif /* defined (HAVE_LWPSTATUS_T) */
10053 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10060 int is_active_thread
;
10063 if (note
->descsz
< 728)
10066 if (! CONST_STRNEQ (note
->namedata
, "win32"))
10069 type
= bfd_get_32 (abfd
, note
->descdata
);
10073 case 1 /* NOTE_INFO_PROCESS */:
10074 /* FIXME: need to add ->core->command. */
10075 /* process_info.pid */
10076 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10077 /* process_info.signal */
10078 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10081 case 2 /* NOTE_INFO_THREAD */:
10082 /* Make a ".reg/999" section. */
10083 /* thread_info.tid */
10084 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
10086 len
= strlen (buf
) + 1;
10087 name
= (char *) bfd_alloc (abfd
, len
);
10091 memcpy (name
, buf
, len
);
10093 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10097 /* sizeof (thread_info.thread_context) */
10099 /* offsetof (thread_info.thread_context) */
10100 sect
->filepos
= note
->descpos
+ 12;
10101 sect
->alignment_power
= 2;
10103 /* thread_info.is_active_thread */
10104 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10106 if (is_active_thread
)
10107 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10111 case 3 /* NOTE_INFO_MODULE */:
10112 /* Make a ".module/xxxxxxxx" section. */
10113 /* module_info.base_address */
10114 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10115 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10117 len
= strlen (buf
) + 1;
10118 name
= (char *) bfd_alloc (abfd
, len
);
10122 memcpy (name
, buf
, len
);
10124 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10129 sect
->size
= note
->descsz
;
10130 sect
->filepos
= note
->descpos
;
10131 sect
->alignment_power
= 2;
10142 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10144 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10146 switch (note
->type
)
10152 if (bed
->elf_backend_grok_prstatus
)
10153 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10155 #if defined (HAVE_PRSTATUS_T)
10156 return elfcore_grok_prstatus (abfd
, note
);
10161 #if defined (HAVE_PSTATUS_T)
10163 return elfcore_grok_pstatus (abfd
, note
);
10166 #if defined (HAVE_LWPSTATUS_T)
10168 return elfcore_grok_lwpstatus (abfd
, note
);
10171 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10172 return elfcore_grok_prfpreg (abfd
, note
);
10174 case NT_WIN32PSTATUS
:
10175 return elfcore_grok_win32pstatus (abfd
, note
);
10177 case NT_PRXFPREG
: /* Linux SSE extension */
10178 if (note
->namesz
== 6
10179 && strcmp (note
->namedata
, "LINUX") == 0)
10180 return elfcore_grok_prxfpreg (abfd
, note
);
10184 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10185 if (note
->namesz
== 6
10186 && strcmp (note
->namedata
, "LINUX") == 0)
10187 return elfcore_grok_xstatereg (abfd
, note
);
10192 if (note
->namesz
== 6
10193 && strcmp (note
->namedata
, "LINUX") == 0)
10194 return elfcore_grok_ppc_vmx (abfd
, note
);
10199 if (note
->namesz
== 6
10200 && strcmp (note
->namedata
, "LINUX") == 0)
10201 return elfcore_grok_ppc_vsx (abfd
, note
);
10206 if (note
->namesz
== 6
10207 && strcmp (note
->namedata
, "LINUX") == 0)
10208 return elfcore_grok_ppc_tar (abfd
, note
);
10213 if (note
->namesz
== 6
10214 && strcmp (note
->namedata
, "LINUX") == 0)
10215 return elfcore_grok_ppc_ppr (abfd
, note
);
10220 if (note
->namesz
== 6
10221 && strcmp (note
->namedata
, "LINUX") == 0)
10222 return elfcore_grok_ppc_dscr (abfd
, note
);
10227 if (note
->namesz
== 6
10228 && strcmp (note
->namedata
, "LINUX") == 0)
10229 return elfcore_grok_ppc_ebb (abfd
, note
);
10234 if (note
->namesz
== 6
10235 && strcmp (note
->namedata
, "LINUX") == 0)
10236 return elfcore_grok_ppc_pmu (abfd
, note
);
10240 case NT_PPC_TM_CGPR
:
10241 if (note
->namesz
== 6
10242 && strcmp (note
->namedata
, "LINUX") == 0)
10243 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10247 case NT_PPC_TM_CFPR
:
10248 if (note
->namesz
== 6
10249 && strcmp (note
->namedata
, "LINUX") == 0)
10250 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10254 case NT_PPC_TM_CVMX
:
10255 if (note
->namesz
== 6
10256 && strcmp (note
->namedata
, "LINUX") == 0)
10257 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10261 case NT_PPC_TM_CVSX
:
10262 if (note
->namesz
== 6
10263 && strcmp (note
->namedata
, "LINUX") == 0)
10264 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10268 case NT_PPC_TM_SPR
:
10269 if (note
->namesz
== 6
10270 && strcmp (note
->namedata
, "LINUX") == 0)
10271 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10275 case NT_PPC_TM_CTAR
:
10276 if (note
->namesz
== 6
10277 && strcmp (note
->namedata
, "LINUX") == 0)
10278 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10282 case NT_PPC_TM_CPPR
:
10283 if (note
->namesz
== 6
10284 && strcmp (note
->namedata
, "LINUX") == 0)
10285 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10289 case NT_PPC_TM_CDSCR
:
10290 if (note
->namesz
== 6
10291 && strcmp (note
->namedata
, "LINUX") == 0)
10292 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10296 case NT_S390_HIGH_GPRS
:
10297 if (note
->namesz
== 6
10298 && strcmp (note
->namedata
, "LINUX") == 0)
10299 return elfcore_grok_s390_high_gprs (abfd
, note
);
10303 case NT_S390_TIMER
:
10304 if (note
->namesz
== 6
10305 && strcmp (note
->namedata
, "LINUX") == 0)
10306 return elfcore_grok_s390_timer (abfd
, note
);
10310 case NT_S390_TODCMP
:
10311 if (note
->namesz
== 6
10312 && strcmp (note
->namedata
, "LINUX") == 0)
10313 return elfcore_grok_s390_todcmp (abfd
, note
);
10317 case NT_S390_TODPREG
:
10318 if (note
->namesz
== 6
10319 && strcmp (note
->namedata
, "LINUX") == 0)
10320 return elfcore_grok_s390_todpreg (abfd
, note
);
10325 if (note
->namesz
== 6
10326 && strcmp (note
->namedata
, "LINUX") == 0)
10327 return elfcore_grok_s390_ctrs (abfd
, note
);
10331 case NT_S390_PREFIX
:
10332 if (note
->namesz
== 6
10333 && strcmp (note
->namedata
, "LINUX") == 0)
10334 return elfcore_grok_s390_prefix (abfd
, note
);
10338 case NT_S390_LAST_BREAK
:
10339 if (note
->namesz
== 6
10340 && strcmp (note
->namedata
, "LINUX") == 0)
10341 return elfcore_grok_s390_last_break (abfd
, note
);
10345 case NT_S390_SYSTEM_CALL
:
10346 if (note
->namesz
== 6
10347 && strcmp (note
->namedata
, "LINUX") == 0)
10348 return elfcore_grok_s390_system_call (abfd
, note
);
10353 if (note
->namesz
== 6
10354 && strcmp (note
->namedata
, "LINUX") == 0)
10355 return elfcore_grok_s390_tdb (abfd
, note
);
10359 case NT_S390_VXRS_LOW
:
10360 if (note
->namesz
== 6
10361 && strcmp (note
->namedata
, "LINUX") == 0)
10362 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10366 case NT_S390_VXRS_HIGH
:
10367 if (note
->namesz
== 6
10368 && strcmp (note
->namedata
, "LINUX") == 0)
10369 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10373 case NT_S390_GS_CB
:
10374 if (note
->namesz
== 6
10375 && strcmp (note
->namedata
, "LINUX") == 0)
10376 return elfcore_grok_s390_gs_cb (abfd
, note
);
10380 case NT_S390_GS_BC
:
10381 if (note
->namesz
== 6
10382 && strcmp (note
->namedata
, "LINUX") == 0)
10383 return elfcore_grok_s390_gs_bc (abfd
, note
);
10388 if (note
->namesz
== 6
10389 && strcmp (note
->namedata
, "LINUX") == 0)
10390 return elfcore_grok_arm_vfp (abfd
, note
);
10395 if (note
->namesz
== 6
10396 && strcmp (note
->namedata
, "LINUX") == 0)
10397 return elfcore_grok_aarch_tls (abfd
, note
);
10401 case NT_ARM_HW_BREAK
:
10402 if (note
->namesz
== 6
10403 && strcmp (note
->namedata
, "LINUX") == 0)
10404 return elfcore_grok_aarch_hw_break (abfd
, note
);
10408 case NT_ARM_HW_WATCH
:
10409 if (note
->namesz
== 6
10410 && strcmp (note
->namedata
, "LINUX") == 0)
10411 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10416 if (note
->namesz
== 6
10417 && strcmp (note
->namedata
, "LINUX") == 0)
10418 return elfcore_grok_aarch_sve (abfd
, note
);
10422 case NT_ARM_PAC_MASK
:
10423 if (note
->namesz
== 6
10424 && strcmp (note
->namedata
, "LINUX") == 0)
10425 return elfcore_grok_aarch_pauth (abfd
, note
);
10431 if (bed
->elf_backend_grok_psinfo
)
10432 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10434 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10435 return elfcore_grok_psinfo (abfd
, note
);
10441 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10444 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10448 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10455 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10457 struct bfd_build_id
* build_id
;
10459 if (note
->descsz
== 0)
10462 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10463 if (build_id
== NULL
)
10466 build_id
->size
= note
->descsz
;
10467 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10468 abfd
->build_id
= build_id
;
10474 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10476 switch (note
->type
)
10481 case NT_GNU_PROPERTY_TYPE_0
:
10482 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10484 case NT_GNU_BUILD_ID
:
10485 return elfobj_grok_gnu_build_id (abfd
, note
);
10490 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10492 struct sdt_note
*cur
=
10493 (struct sdt_note
*) bfd_alloc (abfd
,
10494 sizeof (struct sdt_note
) + note
->descsz
);
10496 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10497 cur
->size
= (bfd_size_type
) note
->descsz
;
10498 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10500 elf_tdata (abfd
)->sdt_note_head
= cur
;
10506 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10508 switch (note
->type
)
10511 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10519 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10523 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10526 if (note
->descsz
< 108)
10531 if (note
->descsz
< 120)
10539 /* Check for version 1 in pr_version. */
10540 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10545 /* Skip over pr_psinfosz. */
10546 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10550 offset
+= 4; /* Padding before pr_psinfosz. */
10554 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10555 elf_tdata (abfd
)->core
->program
10556 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10559 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10560 elf_tdata (abfd
)->core
->command
10561 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10564 /* Padding before pr_pid. */
10567 /* The pr_pid field was added in version "1a". */
10568 if (note
->descsz
< offset
+ 4)
10571 elf_tdata (abfd
)->core
->pid
10572 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10578 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10584 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10585 Also compute minimum size of this note. */
10586 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10590 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10594 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10595 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10602 if (note
->descsz
< min_size
)
10605 /* Check for version 1 in pr_version. */
10606 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10609 /* Extract size of pr_reg from pr_gregsetsz. */
10610 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10611 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10613 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10618 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10622 /* Skip over pr_osreldate. */
10625 /* Read signal from pr_cursig. */
10626 if (elf_tdata (abfd
)->core
->signal
== 0)
10627 elf_tdata (abfd
)->core
->signal
10628 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10631 /* Read TID from pr_pid. */
10632 elf_tdata (abfd
)->core
->lwpid
10633 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10636 /* Padding before pr_reg. */
10637 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10640 /* Make sure that there is enough data remaining in the note. */
10641 if ((note
->descsz
- offset
) < size
)
10644 /* Make a ".reg/999" section and a ".reg" section. */
10645 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10646 size
, note
->descpos
+ offset
);
10650 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10652 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10654 switch (note
->type
)
10657 if (bed
->elf_backend_grok_freebsd_prstatus
)
10658 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10660 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10663 return elfcore_grok_prfpreg (abfd
, note
);
10666 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10668 case NT_FREEBSD_THRMISC
:
10669 if (note
->namesz
== 8)
10670 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10674 case NT_FREEBSD_PROCSTAT_PROC
:
10675 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10678 case NT_FREEBSD_PROCSTAT_FILES
:
10679 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10682 case NT_FREEBSD_PROCSTAT_VMMAP
:
10683 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10686 case NT_FREEBSD_PROCSTAT_AUXV
:
10687 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10689 case NT_X86_XSTATE
:
10690 if (note
->namesz
== 8)
10691 return elfcore_grok_xstatereg (abfd
, note
);
10695 case NT_FREEBSD_PTLWPINFO
:
10696 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10700 return elfcore_grok_arm_vfp (abfd
, note
);
10708 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10712 cp
= strchr (note
->namedata
, '@');
10715 *lwpidp
= atoi(cp
+ 1);
10722 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10724 if (note
->descsz
<= 0x7c + 31)
10727 /* Signal number at offset 0x08. */
10728 elf_tdata (abfd
)->core
->signal
10729 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10731 /* Process ID at offset 0x50. */
10732 elf_tdata (abfd
)->core
->pid
10733 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10735 /* Command name at 0x7c (max 32 bytes, including nul). */
10736 elf_tdata (abfd
)->core
->command
10737 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10739 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10744 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10748 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10749 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10751 switch (note
->type
)
10753 case NT_NETBSDCORE_PROCINFO
:
10754 /* NetBSD-specific core "procinfo". Note that we expect to
10755 find this note before any of the others, which is fine,
10756 since the kernel writes this note out first when it
10757 creates a core file. */
10758 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10759 #ifdef NT_NETBSDCORE_AUXV
10760 case NT_NETBSDCORE_AUXV
:
10761 /* NetBSD-specific Elf Auxiliary Vector data. */
10762 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10764 #ifdef NT_NETBSDCORE_LWPSTATUS
10765 case NT_NETBSDCORE_LWPSTATUS
:
10766 return elfcore_make_note_pseudosection (abfd
,
10767 ".note.netbsdcore.lwpstatus",
10774 /* As of March 2020 there are no other machine-independent notes
10775 defined for NetBSD core files. If the note type is less
10776 than the start of the machine-dependent note types, we don't
10779 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10783 switch (bfd_get_arch (abfd
))
10785 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10786 PT_GETFPREGS == mach+2. */
10788 case bfd_arch_aarch64
:
10789 case bfd_arch_alpha
:
10790 case bfd_arch_sparc
:
10791 switch (note
->type
)
10793 case NT_NETBSDCORE_FIRSTMACH
+0:
10794 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10796 case NT_NETBSDCORE_FIRSTMACH
+2:
10797 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10803 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10804 There's also old PT___GETREGS40 == mach + 1 for old reg
10805 structure which lacks GBR. */
10808 switch (note
->type
)
10810 case NT_NETBSDCORE_FIRSTMACH
+3:
10811 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10813 case NT_NETBSDCORE_FIRSTMACH
+5:
10814 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10820 /* On all other arch's, PT_GETREGS == mach+1 and
10821 PT_GETFPREGS == mach+3. */
10824 switch (note
->type
)
10826 case NT_NETBSDCORE_FIRSTMACH
+1:
10827 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10829 case NT_NETBSDCORE_FIRSTMACH
+3:
10830 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10840 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10842 if (note
->descsz
<= 0x48 + 31)
10845 /* Signal number at offset 0x08. */
10846 elf_tdata (abfd
)->core
->signal
10847 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10849 /* Process ID at offset 0x20. */
10850 elf_tdata (abfd
)->core
->pid
10851 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10853 /* Command name at 0x48 (max 32 bytes, including nul). */
10854 elf_tdata (abfd
)->core
->command
10855 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10861 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10863 if (note
->type
== NT_OPENBSD_PROCINFO
)
10864 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10866 if (note
->type
== NT_OPENBSD_REGS
)
10867 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10869 if (note
->type
== NT_OPENBSD_FPREGS
)
10870 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10872 if (note
->type
== NT_OPENBSD_XFPREGS
)
10873 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10875 if (note
->type
== NT_OPENBSD_AUXV
)
10876 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10878 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10880 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10885 sect
->size
= note
->descsz
;
10886 sect
->filepos
= note
->descpos
;
10887 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10896 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10898 void *ddata
= note
->descdata
;
10905 if (note
->descsz
< 16)
10908 /* nto_procfs_status 'pid' field is at offset 0. */
10909 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10911 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10912 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10914 /* nto_procfs_status 'flags' field is at offset 8. */
10915 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10917 /* nto_procfs_status 'what' field is at offset 14. */
10918 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10920 elf_tdata (abfd
)->core
->signal
= sig
;
10921 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10924 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10925 do not come from signals so we make sure we set the current
10926 thread just in case. */
10927 if (flags
& 0x00000080)
10928 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10930 /* Make a ".qnx_core_status/%d" section. */
10931 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10933 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10936 strcpy (name
, buf
);
10938 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10942 sect
->size
= note
->descsz
;
10943 sect
->filepos
= note
->descpos
;
10944 sect
->alignment_power
= 2;
10946 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10950 elfcore_grok_nto_regs (bfd
*abfd
,
10951 Elf_Internal_Note
*note
,
10959 /* Make a "(base)/%d" section. */
10960 sprintf (buf
, "%s/%ld", base
, tid
);
10962 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10965 strcpy (name
, buf
);
10967 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10971 sect
->size
= note
->descsz
;
10972 sect
->filepos
= note
->descpos
;
10973 sect
->alignment_power
= 2;
10975 /* This is the current thread. */
10976 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10977 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10982 #define BFD_QNT_CORE_INFO 7
10983 #define BFD_QNT_CORE_STATUS 8
10984 #define BFD_QNT_CORE_GREG 9
10985 #define BFD_QNT_CORE_FPREG 10
10988 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10990 /* Every GREG section has a STATUS section before it. Store the
10991 tid from the previous call to pass down to the next gregs
10993 static long tid
= 1;
10995 switch (note
->type
)
10997 case BFD_QNT_CORE_INFO
:
10998 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10999 case BFD_QNT_CORE_STATUS
:
11000 return elfcore_grok_nto_status (abfd
, note
, &tid
);
11001 case BFD_QNT_CORE_GREG
:
11002 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
11003 case BFD_QNT_CORE_FPREG
:
11004 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
11011 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11017 /* Use note name as section name. */
11018 len
= note
->namesz
;
11019 name
= (char *) bfd_alloc (abfd
, len
);
11022 memcpy (name
, note
->namedata
, len
);
11023 name
[len
- 1] = '\0';
11025 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11029 sect
->size
= note
->descsz
;
11030 sect
->filepos
= note
->descpos
;
11031 sect
->alignment_power
= 1;
11036 /* Function: elfcore_write_note
11039 buffer to hold note, and current size of buffer
11043 size of data for note
11045 Writes note to end of buffer. ELF64 notes are written exactly as
11046 for ELF32, despite the current (as of 2006) ELF gabi specifying
11047 that they ought to have 8-byte namesz and descsz field, and have
11048 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11051 Pointer to realloc'd buffer, *BUFSIZ updated. */
11054 elfcore_write_note (bfd
*abfd
,
11062 Elf_External_Note
*xnp
;
11069 namesz
= strlen (name
) + 1;
11071 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11073 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11076 dest
= buf
+ *bufsiz
;
11077 *bufsiz
+= newspace
;
11078 xnp
= (Elf_External_Note
*) dest
;
11079 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11080 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11081 H_PUT_32 (abfd
, type
, xnp
->type
);
11085 memcpy (dest
, name
, namesz
);
11093 memcpy (dest
, input
, size
);
11103 /* gcc-8 warns (*) on all the strncpy calls in this function about
11104 possible string truncation. The "truncation" is not a bug. We
11105 have an external representation of structs with fields that are not
11106 necessarily NULL terminated and corresponding internal
11107 representation fields that are one larger so that they can always
11108 be NULL terminated.
11109 gcc versions between 4.2 and 4.6 do not allow pragma control of
11110 diagnostics inside functions, giving a hard error if you try to use
11111 the finer control available with later versions.
11112 gcc prior to 4.2 warns about diagnostic push and pop.
11113 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11114 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11115 (*) Depending on your system header files! */
11116 #if GCC_VERSION >= 8000
11117 # pragma GCC diagnostic push
11118 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11121 elfcore_write_prpsinfo (bfd
*abfd
,
11125 const char *psargs
)
11127 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11129 if (bed
->elf_backend_write_core_note
!= NULL
)
11132 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11133 NT_PRPSINFO
, fname
, psargs
);
11138 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11139 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11140 if (bed
->s
->elfclass
== ELFCLASS32
)
11142 # if defined (HAVE_PSINFO32_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
));
11159 # if defined (HAVE_PSINFO_T)
11161 int note_type
= NT_PSINFO
;
11164 int note_type
= NT_PRPSINFO
;
11167 memset (&data
, 0, sizeof (data
));
11168 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11169 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11170 return elfcore_write_note (abfd
, buf
, bufsiz
,
11171 "CORE", note_type
, &data
, sizeof (data
));
11173 #endif /* PSINFO_T or PRPSINFO_T */
11178 #if GCC_VERSION >= 8000
11179 # pragma GCC diagnostic pop
11183 elfcore_write_linux_prpsinfo32
11184 (bfd
*abfd
, char *buf
, int *bufsiz
,
11185 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11187 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11189 struct elf_external_linux_prpsinfo32_ugid16 data
;
11191 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11192 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11193 &data
, sizeof (data
));
11197 struct elf_external_linux_prpsinfo32_ugid32 data
;
11199 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11200 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11201 &data
, sizeof (data
));
11206 elfcore_write_linux_prpsinfo64
11207 (bfd
*abfd
, char *buf
, int *bufsiz
,
11208 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11210 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11212 struct elf_external_linux_prpsinfo64_ugid16 data
;
11214 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11215 return elfcore_write_note (abfd
, buf
, bufsiz
,
11216 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11220 struct elf_external_linux_prpsinfo64_ugid32 data
;
11222 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11223 return elfcore_write_note (abfd
, buf
, bufsiz
,
11224 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11229 elfcore_write_prstatus (bfd
*abfd
,
11236 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11238 if (bed
->elf_backend_write_core_note
!= NULL
)
11241 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11243 pid
, cursig
, gregs
);
11248 #if defined (HAVE_PRSTATUS_T)
11249 #if defined (HAVE_PRSTATUS32_T)
11250 if (bed
->s
->elfclass
== ELFCLASS32
)
11252 prstatus32_t prstat
;
11254 memset (&prstat
, 0, sizeof (prstat
));
11255 prstat
.pr_pid
= pid
;
11256 prstat
.pr_cursig
= cursig
;
11257 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11258 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11259 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11266 memset (&prstat
, 0, sizeof (prstat
));
11267 prstat
.pr_pid
= pid
;
11268 prstat
.pr_cursig
= cursig
;
11269 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11270 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11271 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11273 #endif /* HAVE_PRSTATUS_T */
11279 #if defined (HAVE_LWPSTATUS_T)
11281 elfcore_write_lwpstatus (bfd
*abfd
,
11288 lwpstatus_t lwpstat
;
11289 const char *note_name
= "CORE";
11291 memset (&lwpstat
, 0, sizeof (lwpstat
));
11292 lwpstat
.pr_lwpid
= pid
>> 16;
11293 lwpstat
.pr_cursig
= cursig
;
11294 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11295 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11296 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11297 #if !defined(gregs)
11298 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11299 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11301 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11302 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11305 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11306 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11308 #endif /* HAVE_LWPSTATUS_T */
11310 #if defined (HAVE_PSTATUS_T)
11312 elfcore_write_pstatus (bfd
*abfd
,
11316 int cursig ATTRIBUTE_UNUSED
,
11317 const void *gregs ATTRIBUTE_UNUSED
)
11319 const char *note_name
= "CORE";
11320 #if defined (HAVE_PSTATUS32_T)
11321 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11323 if (bed
->s
->elfclass
== ELFCLASS32
)
11327 memset (&pstat
, 0, sizeof (pstat
));
11328 pstat
.pr_pid
= pid
& 0xffff;
11329 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11330 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11338 memset (&pstat
, 0, sizeof (pstat
));
11339 pstat
.pr_pid
= pid
& 0xffff;
11340 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11341 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11345 #endif /* HAVE_PSTATUS_T */
11348 elfcore_write_prfpreg (bfd
*abfd
,
11351 const void *fpregs
,
11354 const char *note_name
= "CORE";
11355 return elfcore_write_note (abfd
, buf
, bufsiz
,
11356 note_name
, NT_FPREGSET
, fpregs
, size
);
11360 elfcore_write_prxfpreg (bfd
*abfd
,
11363 const void *xfpregs
,
11366 char *note_name
= "LINUX";
11367 return elfcore_write_note (abfd
, buf
, bufsiz
,
11368 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11372 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11373 const void *xfpregs
, int size
)
11376 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11377 note_name
= "FreeBSD";
11379 note_name
= "LINUX";
11380 return elfcore_write_note (abfd
, buf
, bufsiz
,
11381 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11385 elfcore_write_ppc_vmx (bfd
*abfd
,
11388 const void *ppc_vmx
,
11391 char *note_name
= "LINUX";
11392 return elfcore_write_note (abfd
, buf
, bufsiz
,
11393 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11397 elfcore_write_ppc_vsx (bfd
*abfd
,
11400 const void *ppc_vsx
,
11403 char *note_name
= "LINUX";
11404 return elfcore_write_note (abfd
, buf
, bufsiz
,
11405 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11409 elfcore_write_ppc_tar (bfd
*abfd
,
11412 const void *ppc_tar
,
11415 char *note_name
= "LINUX";
11416 return elfcore_write_note (abfd
, buf
, bufsiz
,
11417 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11421 elfcore_write_ppc_ppr (bfd
*abfd
,
11424 const void *ppc_ppr
,
11427 char *note_name
= "LINUX";
11428 return elfcore_write_note (abfd
, buf
, bufsiz
,
11429 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11433 elfcore_write_ppc_dscr (bfd
*abfd
,
11436 const void *ppc_dscr
,
11439 char *note_name
= "LINUX";
11440 return elfcore_write_note (abfd
, buf
, bufsiz
,
11441 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11445 elfcore_write_ppc_ebb (bfd
*abfd
,
11448 const void *ppc_ebb
,
11451 char *note_name
= "LINUX";
11452 return elfcore_write_note (abfd
, buf
, bufsiz
,
11453 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11457 elfcore_write_ppc_pmu (bfd
*abfd
,
11460 const void *ppc_pmu
,
11463 char *note_name
= "LINUX";
11464 return elfcore_write_note (abfd
, buf
, bufsiz
,
11465 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11469 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11472 const void *ppc_tm_cgpr
,
11475 char *note_name
= "LINUX";
11476 return elfcore_write_note (abfd
, buf
, bufsiz
,
11477 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11481 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11484 const void *ppc_tm_cfpr
,
11487 char *note_name
= "LINUX";
11488 return elfcore_write_note (abfd
, buf
, bufsiz
,
11489 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11493 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11496 const void *ppc_tm_cvmx
,
11499 char *note_name
= "LINUX";
11500 return elfcore_write_note (abfd
, buf
, bufsiz
,
11501 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11505 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11508 const void *ppc_tm_cvsx
,
11511 char *note_name
= "LINUX";
11512 return elfcore_write_note (abfd
, buf
, bufsiz
,
11513 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11517 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11520 const void *ppc_tm_spr
,
11523 char *note_name
= "LINUX";
11524 return elfcore_write_note (abfd
, buf
, bufsiz
,
11525 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11529 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11532 const void *ppc_tm_ctar
,
11535 char *note_name
= "LINUX";
11536 return elfcore_write_note (abfd
, buf
, bufsiz
,
11537 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11541 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11544 const void *ppc_tm_cppr
,
11547 char *note_name
= "LINUX";
11548 return elfcore_write_note (abfd
, buf
, bufsiz
,
11549 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11553 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11556 const void *ppc_tm_cdscr
,
11559 char *note_name
= "LINUX";
11560 return elfcore_write_note (abfd
, buf
, bufsiz
,
11561 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11565 elfcore_write_s390_high_gprs (bfd
*abfd
,
11568 const void *s390_high_gprs
,
11571 char *note_name
= "LINUX";
11572 return elfcore_write_note (abfd
, buf
, bufsiz
,
11573 note_name
, NT_S390_HIGH_GPRS
,
11574 s390_high_gprs
, size
);
11578 elfcore_write_s390_timer (bfd
*abfd
,
11581 const void *s390_timer
,
11584 char *note_name
= "LINUX";
11585 return elfcore_write_note (abfd
, buf
, bufsiz
,
11586 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11590 elfcore_write_s390_todcmp (bfd
*abfd
,
11593 const void *s390_todcmp
,
11596 char *note_name
= "LINUX";
11597 return elfcore_write_note (abfd
, buf
, bufsiz
,
11598 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11602 elfcore_write_s390_todpreg (bfd
*abfd
,
11605 const void *s390_todpreg
,
11608 char *note_name
= "LINUX";
11609 return elfcore_write_note (abfd
, buf
, bufsiz
,
11610 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11614 elfcore_write_s390_ctrs (bfd
*abfd
,
11617 const void *s390_ctrs
,
11620 char *note_name
= "LINUX";
11621 return elfcore_write_note (abfd
, buf
, bufsiz
,
11622 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11626 elfcore_write_s390_prefix (bfd
*abfd
,
11629 const void *s390_prefix
,
11632 char *note_name
= "LINUX";
11633 return elfcore_write_note (abfd
, buf
, bufsiz
,
11634 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11638 elfcore_write_s390_last_break (bfd
*abfd
,
11641 const void *s390_last_break
,
11644 char *note_name
= "LINUX";
11645 return elfcore_write_note (abfd
, buf
, bufsiz
,
11646 note_name
, NT_S390_LAST_BREAK
,
11647 s390_last_break
, size
);
11651 elfcore_write_s390_system_call (bfd
*abfd
,
11654 const void *s390_system_call
,
11657 char *note_name
= "LINUX";
11658 return elfcore_write_note (abfd
, buf
, bufsiz
,
11659 note_name
, NT_S390_SYSTEM_CALL
,
11660 s390_system_call
, size
);
11664 elfcore_write_s390_tdb (bfd
*abfd
,
11667 const void *s390_tdb
,
11670 char *note_name
= "LINUX";
11671 return elfcore_write_note (abfd
, buf
, bufsiz
,
11672 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11676 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11679 const void *s390_vxrs_low
,
11682 char *note_name
= "LINUX";
11683 return elfcore_write_note (abfd
, buf
, bufsiz
,
11684 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11688 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11691 const void *s390_vxrs_high
,
11694 char *note_name
= "LINUX";
11695 return elfcore_write_note (abfd
, buf
, bufsiz
,
11696 note_name
, NT_S390_VXRS_HIGH
,
11697 s390_vxrs_high
, size
);
11701 elfcore_write_s390_gs_cb (bfd
*abfd
,
11704 const void *s390_gs_cb
,
11707 char *note_name
= "LINUX";
11708 return elfcore_write_note (abfd
, buf
, bufsiz
,
11709 note_name
, NT_S390_GS_CB
,
11714 elfcore_write_s390_gs_bc (bfd
*abfd
,
11717 const void *s390_gs_bc
,
11720 char *note_name
= "LINUX";
11721 return elfcore_write_note (abfd
, buf
, bufsiz
,
11722 note_name
, NT_S390_GS_BC
,
11727 elfcore_write_arm_vfp (bfd
*abfd
,
11730 const void *arm_vfp
,
11733 char *note_name
= "LINUX";
11734 return elfcore_write_note (abfd
, buf
, bufsiz
,
11735 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11739 elfcore_write_aarch_tls (bfd
*abfd
,
11742 const void *aarch_tls
,
11745 char *note_name
= "LINUX";
11746 return elfcore_write_note (abfd
, buf
, bufsiz
,
11747 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11751 elfcore_write_aarch_hw_break (bfd
*abfd
,
11754 const void *aarch_hw_break
,
11757 char *note_name
= "LINUX";
11758 return elfcore_write_note (abfd
, buf
, bufsiz
,
11759 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11763 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11766 const void *aarch_hw_watch
,
11769 char *note_name
= "LINUX";
11770 return elfcore_write_note (abfd
, buf
, bufsiz
,
11771 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11775 elfcore_write_aarch_sve (bfd
*abfd
,
11778 const void *aarch_sve
,
11781 char *note_name
= "LINUX";
11782 return elfcore_write_note (abfd
, buf
, bufsiz
,
11783 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11787 elfcore_write_aarch_pauth (bfd
*abfd
,
11790 const void *aarch_pauth
,
11793 char *note_name
= "LINUX";
11794 return elfcore_write_note (abfd
, buf
, bufsiz
,
11795 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11799 elfcore_write_register_note (bfd
*abfd
,
11802 const char *section
,
11806 if (strcmp (section
, ".reg2") == 0)
11807 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11808 if (strcmp (section
, ".reg-xfp") == 0)
11809 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11810 if (strcmp (section
, ".reg-xstate") == 0)
11811 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11812 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11813 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11814 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11815 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11816 if (strcmp (section
, ".reg-ppc-tar") == 0)
11817 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11818 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11819 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11820 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11821 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11822 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11823 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11824 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11825 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11826 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11827 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11828 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11829 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11830 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11831 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11832 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11833 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11834 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11835 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11836 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11837 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11838 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11839 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11840 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11841 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11842 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11843 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11844 if (strcmp (section
, ".reg-s390-timer") == 0)
11845 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11846 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11847 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11848 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11849 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11850 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11851 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11852 if (strcmp (section
, ".reg-s390-prefix") == 0)
11853 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11854 if (strcmp (section
, ".reg-s390-last-break") == 0)
11855 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11856 if (strcmp (section
, ".reg-s390-system-call") == 0)
11857 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11858 if (strcmp (section
, ".reg-s390-tdb") == 0)
11859 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11860 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11861 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11862 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11863 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11864 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11865 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11866 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11867 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11868 if (strcmp (section
, ".reg-arm-vfp") == 0)
11869 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11870 if (strcmp (section
, ".reg-aarch-tls") == 0)
11871 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11872 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11873 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11874 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11875 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11876 if (strcmp (section
, ".reg-aarch-sve") == 0)
11877 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11878 if (strcmp (section
, ".reg-aarch-pauth") == 0)
11879 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
11884 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11889 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11890 gABI specifies that PT_NOTE alignment should be aligned to 4
11891 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11892 align is less than 4, we use 4 byte alignment. */
11895 if (align
!= 4 && align
!= 8)
11899 while (p
< buf
+ size
)
11901 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11902 Elf_Internal_Note in
;
11904 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11907 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11909 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11910 in
.namedata
= xnp
->name
;
11911 if (in
.namesz
> buf
- in
.namedata
+ size
)
11914 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11915 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11916 in
.descpos
= offset
+ (in
.descdata
- buf
);
11918 && (in
.descdata
>= buf
+ size
11919 || in
.descsz
> buf
- in
.descdata
+ size
))
11922 switch (bfd_get_format (abfd
))
11929 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11932 const char * string
;
11934 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11938 GROKER_ELEMENT ("", elfcore_grok_note
),
11939 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11940 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11941 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11942 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11943 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
11944 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
)
11946 #undef GROKER_ELEMENT
11949 for (i
= ARRAY_SIZE (grokers
); i
--;)
11951 if (in
.namesz
>= grokers
[i
].len
11952 && strncmp (in
.namedata
, grokers
[i
].string
,
11953 grokers
[i
].len
) == 0)
11955 if (! grokers
[i
].func (abfd
, & in
))
11964 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
11966 if (! elfobj_grok_gnu_note (abfd
, &in
))
11969 else if (in
.namesz
== sizeof "stapsdt"
11970 && strcmp (in
.namedata
, "stapsdt") == 0)
11972 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
11978 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
11985 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
11990 if (size
== 0 || (size
+ 1) == 0)
11993 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
11996 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
12000 /* PR 17512: file: ec08f814
12001 0-termintate the buffer so that string searches will not overflow. */
12004 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
12014 /* Providing external access to the ELF program header table. */
12016 /* Return an upper bound on the number of bytes required to store a
12017 copy of ABFD's program header table entries. Return -1 if an error
12018 occurs; bfd_get_error will return an appropriate code. */
12021 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12023 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12025 bfd_set_error (bfd_error_wrong_format
);
12029 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12032 /* Copy ABFD's program header table entries to *PHDRS. The entries
12033 will be stored as an array of Elf_Internal_Phdr structures, as
12034 defined in include/elf/internal.h. To find out how large the
12035 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12037 Return the number of program header table entries read, or -1 if an
12038 error occurs; bfd_get_error will return an appropriate code. */
12041 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12045 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12047 bfd_set_error (bfd_error_wrong_format
);
12051 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12052 if (num_phdrs
!= 0)
12053 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12054 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12059 enum elf_reloc_type_class
12060 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12061 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12062 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12064 return reloc_class_normal
;
12067 /* For RELA architectures, return the relocation value for a
12068 relocation against a local symbol. */
12071 _bfd_elf_rela_local_sym (bfd
*abfd
,
12072 Elf_Internal_Sym
*sym
,
12074 Elf_Internal_Rela
*rel
)
12076 asection
*sec
= *psec
;
12077 bfd_vma relocation
;
12079 relocation
= (sec
->output_section
->vma
12080 + sec
->output_offset
12082 if ((sec
->flags
& SEC_MERGE
)
12083 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12084 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12087 _bfd_merged_section_offset (abfd
, psec
,
12088 elf_section_data (sec
)->sec_info
,
12089 sym
->st_value
+ rel
->r_addend
);
12092 /* If we have changed the section, and our original section is
12093 marked with SEC_EXCLUDE, it means that the original
12094 SEC_MERGE section has been completely subsumed in some
12095 other SEC_MERGE section. In this case, we need to leave
12096 some info around for --emit-relocs. */
12097 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12098 sec
->kept_section
= *psec
;
12101 rel
->r_addend
-= relocation
;
12102 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12108 _bfd_elf_rel_local_sym (bfd
*abfd
,
12109 Elf_Internal_Sym
*sym
,
12113 asection
*sec
= *psec
;
12115 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12116 return sym
->st_value
+ addend
;
12118 return _bfd_merged_section_offset (abfd
, psec
,
12119 elf_section_data (sec
)->sec_info
,
12120 sym
->st_value
+ addend
);
12123 /* Adjust an address within a section. Given OFFSET within SEC, return
12124 the new offset within the section, based upon changes made to the
12125 section. Returns -1 if the offset is now invalid.
12126 The offset (in abnd out) is in target sized bytes, however big a
12130 _bfd_elf_section_offset (bfd
*abfd
,
12131 struct bfd_link_info
*info
,
12135 switch (sec
->sec_info_type
)
12137 case SEC_INFO_TYPE_STABS
:
12138 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12140 case SEC_INFO_TYPE_EH_FRAME
:
12141 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12144 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12146 /* Reverse the offset. */
12147 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12148 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12150 /* address_size and sec->size are in octets. Convert
12151 to bytes before subtracting the original offset. */
12152 offset
= ((sec
->size
- address_size
)
12153 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12159 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12160 reconstruct an ELF file by reading the segments out of remote memory
12161 based on the ELF file header at EHDR_VMA and the ELF program headers it
12162 points to. If not null, *LOADBASEP is filled in with the difference
12163 between the VMAs from which the segments were read, and the VMAs the
12164 file headers (and hence BFD's idea of each section's VMA) put them at.
12166 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12167 remote memory at target address VMA into the local buffer at MYADDR; it
12168 should return zero on success or an `errno' code on failure. TEMPL must
12169 be a BFD for an ELF target with the word size and byte order found in
12170 the remote memory. */
12173 bfd_elf_bfd_from_remote_memory
12176 bfd_size_type size
,
12177 bfd_vma
*loadbasep
,
12178 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12180 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12181 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12185 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12186 long symcount ATTRIBUTE_UNUSED
,
12187 asymbol
**syms ATTRIBUTE_UNUSED
,
12192 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12195 const char *relplt_name
;
12196 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12200 Elf_Internal_Shdr
*hdr
;
12206 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12209 if (dynsymcount
<= 0)
12212 if (!bed
->plt_sym_val
)
12215 relplt_name
= bed
->relplt_name
;
12216 if (relplt_name
== NULL
)
12217 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12218 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12219 if (relplt
== NULL
)
12222 hdr
= &elf_section_data (relplt
)->this_hdr
;
12223 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12224 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12227 plt
= bfd_get_section_by_name (abfd
, ".plt");
12231 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12232 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12235 count
= relplt
->size
/ hdr
->sh_entsize
;
12236 size
= count
* sizeof (asymbol
);
12237 p
= relplt
->relocation
;
12238 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12240 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12241 if (p
->addend
!= 0)
12244 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12246 size
+= sizeof ("+0x") - 1 + 8;
12251 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12255 names
= (char *) (s
+ count
);
12256 p
= relplt
->relocation
;
12258 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12263 addr
= bed
->plt_sym_val (i
, plt
, p
);
12264 if (addr
== (bfd_vma
) -1)
12267 *s
= **p
->sym_ptr_ptr
;
12268 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12269 we are defining a symbol, ensure one of them is set. */
12270 if ((s
->flags
& BSF_LOCAL
) == 0)
12271 s
->flags
|= BSF_GLOBAL
;
12272 s
->flags
|= BSF_SYNTHETIC
;
12274 s
->value
= addr
- plt
->vma
;
12277 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12278 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12280 if (p
->addend
!= 0)
12284 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12285 names
+= sizeof ("+0x") - 1;
12286 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12287 for (a
= buf
; *a
== '0'; ++a
)
12290 memcpy (names
, a
, len
);
12293 memcpy (names
, "@plt", sizeof ("@plt"));
12294 names
+= sizeof ("@plt");
12301 /* It is only used by x86-64 so far.
12302 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12303 but current usage would allow all of _bfd_std_section to be zero. */
12304 static const asymbol lcomm_sym
12305 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12306 asection _bfd_elf_large_com_section
12307 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12308 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12311 _bfd_elf_final_write_processing (bfd
*abfd
)
12313 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12315 i_ehdrp
= elf_elfheader (abfd
);
12317 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12318 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12320 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12321 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12322 STB_GNU_UNIQUE binding. */
12323 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12325 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12326 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12327 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12328 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12330 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12331 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12332 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12333 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12334 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12335 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12336 bfd_set_error (bfd_error_sorry
);
12344 /* Return TRUE for ELF symbol types that represent functions.
12345 This is the default version of this function, which is sufficient for
12346 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12349 _bfd_elf_is_function_type (unsigned int type
)
12351 return (type
== STT_FUNC
12352 || type
== STT_GNU_IFUNC
);
12355 /* If the ELF symbol SYM might be a function in SEC, return the
12356 function size and set *CODE_OFF to the function's entry point,
12357 otherwise return zero. */
12360 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12363 bfd_size_type size
;
12365 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12366 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12367 || sym
->section
!= sec
)
12370 *code_off
= sym
->value
;
12372 if (!(sym
->flags
& BSF_SYNTHETIC
))
12373 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
12379 /* Set to non-zero to enable some debug messages. */
12380 #define DEBUG_SECONDARY_RELOCS 0
12382 /* An internal-to-the-bfd-library only section type
12383 used to indicate a cached secondary reloc section. */
12384 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12386 /* Create a BFD section to hold a secondary reloc section. */
12389 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
12390 Elf_Internal_Shdr
*hdr
,
12392 unsigned int shindex
)
12394 /* We only support RELA secondary relocs. */
12395 if (hdr
->sh_type
!= SHT_RELA
)
12398 #if DEBUG_SECONDARY_RELOCS
12399 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
12401 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
12402 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
12405 /* Read in any secondary relocs associated with SEC. */
12408 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
12410 asymbol
** symbols
)
12412 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12414 bfd_boolean result
= TRUE
;
12415 bfd_vma (*r_sym
) (bfd_vma
);
12417 #if BFD_DEFAULT_TARGET_SIZE > 32
12418 if (bfd_arch_bits_per_address (abfd
) != 32)
12419 r_sym
= elf64_r_sym
;
12422 r_sym
= elf32_r_sym
;
12424 /* Discover if there are any secondary reloc sections
12425 associated with SEC. */
12426 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12428 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
12430 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
12431 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12433 bfd_byte
* native_relocs
;
12434 bfd_byte
* native_reloc
;
12435 arelent
* internal_relocs
;
12436 arelent
* internal_reloc
;
12438 unsigned int entsize
;
12439 unsigned int symcount
;
12440 unsigned int reloc_count
;
12443 if (ebd
->elf_info_to_howto
== NULL
)
12446 #if DEBUG_SECONDARY_RELOCS
12447 fprintf (stderr
, "read secondary relocs for %s from %s\n",
12448 sec
->name
, relsec
->name
);
12450 entsize
= hdr
->sh_entsize
;
12452 native_relocs
= bfd_malloc (hdr
->sh_size
);
12453 if (native_relocs
== NULL
)
12459 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
12460 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
12462 free (native_relocs
);
12463 bfd_set_error (bfd_error_file_too_big
);
12468 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
12469 if (internal_relocs
== NULL
)
12471 free (native_relocs
);
12476 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
12477 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
12480 free (native_relocs
);
12481 /* The internal_relocs will be freed when
12482 the memory for the bfd is released. */
12487 symcount
= bfd_get_symcount (abfd
);
12489 for (i
= 0, internal_reloc
= internal_relocs
,
12490 native_reloc
= native_relocs
;
12492 i
++, internal_reloc
++, native_reloc
+= entsize
)
12495 Elf_Internal_Rela rela
;
12497 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
12499 /* The address of an ELF reloc is section relative for an object
12500 file, and absolute for an executable file or shared library.
12501 The address of a normal BFD reloc is always section relative,
12502 and the address of a dynamic reloc is absolute.. */
12503 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
12504 internal_reloc
->address
= rela
.r_offset
;
12506 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
12508 if (r_sym (rela
.r_info
) == STN_UNDEF
)
12510 /* FIXME: This and the error case below mean that we
12511 have a symbol on relocs that is not elf_symbol_type. */
12512 internal_reloc
->sym_ptr_ptr
=
12513 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12515 else if (r_sym (rela
.r_info
) > symcount
)
12518 /* xgettext:c-format */
12519 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12520 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
12521 bfd_set_error (bfd_error_bad_value
);
12522 internal_reloc
->sym_ptr_ptr
=
12523 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12530 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
12532 internal_reloc
->sym_ptr_ptr
= ps
;
12533 /* Make sure that this symbol is not removed by strip. */
12534 (*ps
)->flags
|= BSF_KEEP
;
12537 internal_reloc
->addend
= rela
.r_addend
;
12539 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
12540 if (! res
|| internal_reloc
->howto
== NULL
)
12542 #if DEBUG_SECONDARY_RELOCS
12543 fprintf (stderr
, "there is no howto associated with reloc %lx\n",
12550 free (native_relocs
);
12551 /* Store the internal relocs. */
12552 elf_section_data (relsec
)->sec_info
= internal_relocs
;
12559 /* Set the ELF section header fields of an output secondary reloc section. */
12562 _bfd_elf_copy_special_section_fields (const bfd
* ibfd ATTRIBUTE_UNUSED
,
12563 bfd
* obfd ATTRIBUTE_UNUSED
,
12564 const Elf_Internal_Shdr
* isection
,
12565 Elf_Internal_Shdr
* osection
)
12570 if (isection
== NULL
)
12573 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
12576 isec
= isection
->bfd_section
;
12580 osec
= osection
->bfd_section
;
12584 BFD_ASSERT (elf_section_data (osec
)->sec_info
== NULL
);
12585 elf_section_data (osec
)->sec_info
= elf_section_data (isec
)->sec_info
;
12586 osection
->sh_type
= SHT_RELA
;
12587 osection
->sh_link
= elf_onesymtab (obfd
);
12588 if (osection
->sh_link
== 0)
12590 /* There is no symbol table - we are hosed... */
12592 /* xgettext:c-format */
12593 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12595 bfd_set_error (bfd_error_bad_value
);
12599 /* Find the output section that corresponds to the isection's sh_info link. */
12600 if (isection
->sh_info
== 0
12601 || isection
->sh_info
>= elf_numsections (ibfd
))
12604 /* xgettext:c-format */
12605 (_("%pB(%pA): info section index is invalid"),
12607 bfd_set_error (bfd_error_bad_value
);
12611 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
12613 if (isection
== NULL
12614 || isection
->bfd_section
== NULL
12615 || isection
->bfd_section
->output_section
== NULL
)
12618 /* xgettext:c-format */
12619 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12621 bfd_set_error (bfd_error_bad_value
);
12625 osection
->sh_info
=
12626 elf_section_data (isection
->bfd_section
->output_section
)->this_idx
;
12628 #if DEBUG_SECONDARY_RELOCS
12629 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
12630 osec
->name
, osection
->sh_link
, osection
->sh_info
);
12636 /* Write out a secondary reloc section. */
12639 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
12641 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12642 bfd_vma addr_offset
;
12644 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
12645 bfd_boolean result
= TRUE
;
12650 #if BFD_DEFAULT_TARGET_SIZE > 32
12651 if (bfd_arch_bits_per_address (abfd
) != 32)
12652 r_info
= elf64_r_info
;
12655 r_info
= elf32_r_info
;
12657 /* The address of an ELF reloc is section relative for an object
12658 file, and absolute for an executable file or shared library.
12659 The address of a BFD reloc is always section relative. */
12661 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
12662 addr_offset
= sec
->vma
;
12664 /* Discover if there are any secondary reloc sections
12665 associated with SEC. */
12666 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12668 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
12669 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
12671 if (hdr
->sh_type
== SHT_RELA
12672 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12674 asymbol
* last_sym
;
12676 unsigned int reloc_count
;
12678 arelent
* src_irel
;
12679 bfd_byte
* dst_rela
;
12681 if (hdr
->contents
!= NULL
)
12684 /* xgettext:c-format */
12685 (_("%pB(%pA): error: secondary reloc section processed twice"),
12687 bfd_set_error (bfd_error_bad_value
);
12692 reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
12693 if (reloc_count
<= 0)
12696 /* xgettext:c-format */
12697 (_("%pB(%pA): error: secondary reloc section is empty!"),
12699 bfd_set_error (bfd_error_bad_value
);
12704 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
12705 if (hdr
->contents
== NULL
)
12708 #if DEBUG_SECONDARY_RELOCS
12709 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
12710 reloc_count
, sec
->name
, relsec
->name
);
12714 dst_rela
= hdr
->contents
;
12715 src_irel
= (arelent
*) esd
->sec_info
;
12716 if (src_irel
== NULL
)
12719 /* xgettext:c-format */
12720 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12722 bfd_set_error (bfd_error_bad_value
);
12727 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= hdr
->sh_entsize
)
12729 Elf_Internal_Rela src_rela
;
12734 ptr
= src_irel
+ idx
;
12738 /* xgettext:c-format */
12739 (_("%pB(%pA): error: reloc table entry %u is empty"),
12740 abfd
, relsec
, idx
);
12741 bfd_set_error (bfd_error_bad_value
);
12746 if (ptr
->sym_ptr_ptr
== NULL
)
12748 /* FIXME: Is this an error ? */
12753 sym
= *ptr
->sym_ptr_ptr
;
12755 if (sym
== last_sym
)
12759 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
12763 /* xgettext:c-format */
12764 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12765 abfd
, relsec
, idx
);
12766 bfd_set_error (bfd_error_bad_value
);
12775 if (sym
->the_bfd
!= NULL
12776 && sym
->the_bfd
->xvec
!= abfd
->xvec
12777 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
12780 /* xgettext:c-format */
12781 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12782 abfd
, relsec
, idx
);
12783 bfd_set_error (bfd_error_bad_value
);
12789 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
12790 if (ptr
->howto
== NULL
)
12793 /* xgettext:c-format */
12794 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12795 abfd
, relsec
, idx
);
12796 bfd_set_error (bfd_error_bad_value
);
12798 src_rela
.r_info
= r_info (0, 0);
12801 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
12802 src_rela
.r_addend
= ptr
->addend
;
12803 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);