1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2020 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
56 file_ptr offset
, size_t align
);
58 /* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
62 /* Swap in a Verdef structure. */
65 _bfd_elf_swap_verdef_in (bfd
*abfd
,
66 const Elf_External_Verdef
*src
,
67 Elf_Internal_Verdef
*dst
)
69 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
70 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
71 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
72 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
73 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
74 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
75 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
78 /* Swap out a Verdef structure. */
81 _bfd_elf_swap_verdef_out (bfd
*abfd
,
82 const Elf_Internal_Verdef
*src
,
83 Elf_External_Verdef
*dst
)
85 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
86 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
87 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
88 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
89 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
90 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
91 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
94 /* Swap in a Verdaux structure. */
97 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
98 const Elf_External_Verdaux
*src
,
99 Elf_Internal_Verdaux
*dst
)
101 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
102 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
105 /* Swap out a Verdaux structure. */
108 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
109 const Elf_Internal_Verdaux
*src
,
110 Elf_External_Verdaux
*dst
)
112 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
113 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
116 /* Swap in a Verneed structure. */
119 _bfd_elf_swap_verneed_in (bfd
*abfd
,
120 const Elf_External_Verneed
*src
,
121 Elf_Internal_Verneed
*dst
)
123 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
124 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
125 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
126 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
127 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
130 /* Swap out a Verneed structure. */
133 _bfd_elf_swap_verneed_out (bfd
*abfd
,
134 const Elf_Internal_Verneed
*src
,
135 Elf_External_Verneed
*dst
)
137 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
138 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
139 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
140 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
141 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
144 /* Swap in a Vernaux structure. */
147 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
148 const Elf_External_Vernaux
*src
,
149 Elf_Internal_Vernaux
*dst
)
151 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
152 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
153 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
154 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
155 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
158 /* Swap out a Vernaux structure. */
161 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
162 const Elf_Internal_Vernaux
*src
,
163 Elf_External_Vernaux
*dst
)
165 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
166 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
167 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
168 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
169 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
172 /* Swap in a Versym structure. */
175 _bfd_elf_swap_versym_in (bfd
*abfd
,
176 const Elf_External_Versym
*src
,
177 Elf_Internal_Versym
*dst
)
179 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
182 /* Swap out a Versym structure. */
185 _bfd_elf_swap_versym_out (bfd
*abfd
,
186 const Elf_Internal_Versym
*src
,
187 Elf_External_Versym
*dst
)
189 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
192 /* Standard ELF hash function. Do not change this function; you will
193 cause invalid hash tables to be generated. */
196 bfd_elf_hash (const char *namearg
)
198 const unsigned char *name
= (const unsigned char *) namearg
;
203 while ((ch
= *name
++) != '\0')
206 if ((g
= (h
& 0xf0000000)) != 0)
209 /* The ELF ABI says `h &= ~g', but this is equivalent in
210 this case and on some machines one insn instead of two. */
214 return h
& 0xffffffff;
217 /* DT_GNU_HASH hash function. Do not change this function; you will
218 cause invalid hash tables to be generated. */
221 bfd_elf_gnu_hash (const char *namearg
)
223 const unsigned char *name
= (const unsigned char *) namearg
;
224 unsigned long h
= 5381;
227 while ((ch
= *name
++) != '\0')
228 h
= (h
<< 5) + h
+ ch
;
229 return h
& 0xffffffff;
232 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
233 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 bfd_elf_allocate_object (bfd
*abfd
,
237 enum elf_target_id object_id
)
239 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
240 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
241 if (abfd
->tdata
.any
== NULL
)
244 elf_object_id (abfd
) = object_id
;
245 if (abfd
->direction
!= read_direction
)
247 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
250 elf_tdata (abfd
)->o
= o
;
251 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
258 bfd_elf_make_object (bfd
*abfd
)
260 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
261 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
266 bfd_elf_mkcorefile (bfd
*abfd
)
268 /* I think this can be done just like an object file. */
269 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
271 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
272 return elf_tdata (abfd
)->core
!= NULL
;
276 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
278 Elf_Internal_Shdr
**i_shdrp
;
279 bfd_byte
*shstrtab
= NULL
;
281 bfd_size_type shstrtabsize
;
283 i_shdrp
= elf_elfsections (abfd
);
285 || shindex
>= elf_numsections (abfd
)
286 || i_shdrp
[shindex
] == 0)
289 shstrtab
= i_shdrp
[shindex
]->contents
;
290 if (shstrtab
== NULL
)
292 /* No cached one, attempt to read, and cache what we read. */
293 offset
= i_shdrp
[shindex
]->sh_offset
;
294 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
296 /* Allocate and clear an extra byte at the end, to prevent crashes
297 in case the string table is not terminated. */
298 if (shstrtabsize
+ 1 <= 1
299 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
300 || (shstrtab
= _bfd_alloc_and_read (abfd
, shstrtabsize
+ 1,
301 shstrtabsize
)) == NULL
)
303 /* Once we've failed to read it, make sure we don't keep
304 trying. Otherwise, we'll keep allocating space for
305 the string table over and over. */
306 i_shdrp
[shindex
]->sh_size
= 0;
309 shstrtab
[shstrtabsize
] = '\0';
310 i_shdrp
[shindex
]->contents
= shstrtab
;
312 return (char *) shstrtab
;
316 bfd_elf_string_from_elf_section (bfd
*abfd
,
317 unsigned int shindex
,
318 unsigned int strindex
)
320 Elf_Internal_Shdr
*hdr
;
325 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
328 hdr
= elf_elfsections (abfd
)[shindex
];
330 if (hdr
->contents
== NULL
)
332 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
334 /* PR 17512: file: f057ec89. */
335 /* xgettext:c-format */
336 _bfd_error_handler (_("%pB: attempt to load strings from"
337 " a non-string section (number %d)"),
342 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
347 /* PR 24273: The string section's contents may have already
348 been loaded elsewhere, eg because a corrupt file has the
349 string section index in the ELF header pointing at a group
350 section. So be paranoid, and test that the last byte of
351 the section is zero. */
352 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
356 if (strindex
>= hdr
->sh_size
)
358 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
360 /* xgettext:c-format */
361 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
362 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
363 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
365 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
369 return ((char *) hdr
->contents
) + strindex
;
372 /* Read and convert symbols to internal format.
373 SYMCOUNT specifies the number of symbols to read, starting from
374 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
375 are non-NULL, they are used to store the internal symbols, external
376 symbols, and symbol section index extensions, respectively.
377 Returns a pointer to the internal symbol buffer (malloced if necessary)
378 or NULL if there were no symbols or some kind of problem. */
381 bfd_elf_get_elf_syms (bfd
*ibfd
,
382 Elf_Internal_Shdr
*symtab_hdr
,
385 Elf_Internal_Sym
*intsym_buf
,
387 Elf_External_Sym_Shndx
*extshndx_buf
)
389 Elf_Internal_Shdr
*shndx_hdr
;
391 const bfd_byte
*esym
;
392 Elf_External_Sym_Shndx
*alloc_extshndx
;
393 Elf_External_Sym_Shndx
*shndx
;
394 Elf_Internal_Sym
*alloc_intsym
;
395 Elf_Internal_Sym
*isym
;
396 Elf_Internal_Sym
*isymend
;
397 const struct elf_backend_data
*bed
;
402 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
408 /* Normal syms might have section extension entries. */
410 if (elf_symtab_shndx_list (ibfd
) != NULL
)
412 elf_section_list
* entry
;
413 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
415 /* Find an index section that is linked to this symtab section. */
416 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
419 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
422 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
424 shndx_hdr
= & entry
->hdr
;
429 if (shndx_hdr
== NULL
)
431 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
432 /* Not really accurate, but this was how the old code used to work. */
433 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
434 /* Otherwise we do nothing. The assumption is that
435 the index table will not be needed. */
439 /* Read the symbols. */
441 alloc_extshndx
= NULL
;
443 bed
= get_elf_backend_data (ibfd
);
444 extsym_size
= bed
->s
->sizeof_sym
;
445 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
447 bfd_set_error (bfd_error_file_too_big
);
451 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
452 if (extsym_buf
== NULL
)
454 alloc_ext
= bfd_malloc (amt
);
455 extsym_buf
= alloc_ext
;
457 if (extsym_buf
== NULL
458 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
459 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
465 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
469 if (_bfd_mul_overflow (symcount
, sizeof (Elf_External_Sym_Shndx
), &amt
))
471 bfd_set_error (bfd_error_file_too_big
);
475 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
476 if (extshndx_buf
== NULL
)
478 alloc_extshndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
479 extshndx_buf
= alloc_extshndx
;
481 if (extshndx_buf
== NULL
482 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
483 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
490 if (intsym_buf
== NULL
)
492 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
494 bfd_set_error (bfd_error_file_too_big
);
497 alloc_intsym
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
498 intsym_buf
= alloc_intsym
;
499 if (intsym_buf
== NULL
)
503 /* Convert the symbols to internal form. */
504 isymend
= intsym_buf
+ symcount
;
505 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
506 shndx
= extshndx_buf
;
508 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
509 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
511 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
512 /* xgettext:c-format */
513 _bfd_error_handler (_("%pB symbol number %lu references"
514 " nonexistent SHT_SYMTAB_SHNDX section"),
515 ibfd
, (unsigned long) symoffset
);
516 if (alloc_intsym
!= NULL
)
523 if (alloc_ext
!= NULL
)
525 if (alloc_extshndx
!= NULL
)
526 free (alloc_extshndx
);
531 /* Look up a symbol name. */
533 bfd_elf_sym_name (bfd
*abfd
,
534 Elf_Internal_Shdr
*symtab_hdr
,
535 Elf_Internal_Sym
*isym
,
539 unsigned int iname
= isym
->st_name
;
540 unsigned int shindex
= symtab_hdr
->sh_link
;
542 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
543 /* Check for a bogus st_shndx to avoid crashing. */
544 && isym
->st_shndx
< elf_numsections (abfd
))
546 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
547 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
550 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
553 else if (sym_sec
&& *name
== '\0')
554 name
= bfd_section_name (sym_sec
);
559 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
560 sections. The first element is the flags, the rest are section
563 typedef union elf_internal_group
{
564 Elf_Internal_Shdr
*shdr
;
566 } Elf_Internal_Group
;
568 /* Return the name of the group signature symbol. Why isn't the
569 signature just a string? */
572 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
574 Elf_Internal_Shdr
*hdr
;
575 unsigned char esym
[sizeof (Elf64_External_Sym
)];
576 Elf_External_Sym_Shndx eshndx
;
577 Elf_Internal_Sym isym
;
579 /* First we need to ensure the symbol table is available. Make sure
580 that it is a symbol table section. */
581 if (ghdr
->sh_link
>= elf_numsections (abfd
))
583 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
584 if (hdr
->sh_type
!= SHT_SYMTAB
585 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
588 /* Go read the symbol. */
589 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
590 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
591 &isym
, esym
, &eshndx
) == NULL
)
594 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
597 /* Set next_in_group list pointer, and group name for NEWSECT. */
600 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
602 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
604 /* If num_group is zero, read in all SHT_GROUP sections. The count
605 is set to -1 if there are no SHT_GROUP sections. */
608 unsigned int i
, shnum
;
610 /* First count the number of groups. If we have a SHT_GROUP
611 section with just a flag word (ie. sh_size is 4), ignore it. */
612 shnum
= elf_numsections (abfd
);
615 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
616 ( (shdr)->sh_type == SHT_GROUP \
617 && (shdr)->sh_size >= minsize \
618 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
619 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
621 for (i
= 0; i
< shnum
; i
++)
623 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
625 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
631 num_group
= (unsigned) -1;
632 elf_tdata (abfd
)->num_group
= num_group
;
633 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
637 /* We keep a list of elf section headers for group sections,
638 so we can find them quickly. */
641 elf_tdata (abfd
)->num_group
= num_group
;
642 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
643 elf_tdata (abfd
)->group_sect_ptr
644 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
645 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
649 for (i
= 0; i
< shnum
; i
++)
651 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
653 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
656 Elf_Internal_Group
*dest
;
658 /* Make sure the group section has a BFD section
660 if (!bfd_section_from_shdr (abfd
, i
))
663 /* Add to list of sections. */
664 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
667 /* Read the raw contents. */
668 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
669 shdr
->contents
= NULL
;
670 if (_bfd_mul_overflow (shdr
->sh_size
,
671 sizeof (*dest
) / 4, &amt
)
672 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
674 = _bfd_alloc_and_read (abfd
, amt
, shdr
->sh_size
)))
677 /* xgettext:c-format */
678 (_("%pB: invalid size field in group section"
679 " header: %#" PRIx64
""),
680 abfd
, (uint64_t) shdr
->sh_size
);
681 bfd_set_error (bfd_error_bad_value
);
686 /* Translate raw contents, a flag word followed by an
687 array of elf section indices all in target byte order,
688 to the flag word followed by an array of elf section
690 src
= shdr
->contents
+ shdr
->sh_size
;
691 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
699 idx
= H_GET_32 (abfd
, src
);
700 if (src
== shdr
->contents
)
704 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
705 shdr
->bfd_section
->flags
706 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
711 dest
->shdr
= elf_elfsections (abfd
)[idx
];
712 /* PR binutils/23199: All sections in a
713 section group should be marked with
714 SHF_GROUP. But some tools generate
715 broken objects without SHF_GROUP. Fix
717 dest
->shdr
->sh_flags
|= SHF_GROUP
;
720 || dest
->shdr
->sh_type
== SHT_GROUP
)
723 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
731 /* PR 17510: Corrupt binaries might contain invalid groups. */
732 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
734 elf_tdata (abfd
)->num_group
= num_group
;
736 /* If all groups are invalid then fail. */
739 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
740 elf_tdata (abfd
)->num_group
= num_group
= -1;
742 (_("%pB: no valid group sections found"), abfd
);
743 bfd_set_error (bfd_error_bad_value
);
749 if (num_group
!= (unsigned) -1)
751 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
754 for (j
= 0; j
< num_group
; j
++)
756 /* Begin search from previous found group. */
757 unsigned i
= (j
+ search_offset
) % num_group
;
759 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
760 Elf_Internal_Group
*idx
;
766 idx
= (Elf_Internal_Group
*) shdr
->contents
;
767 if (idx
== NULL
|| shdr
->sh_size
< 4)
769 /* See PR 21957 for a reproducer. */
770 /* xgettext:c-format */
771 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
772 abfd
, shdr
->bfd_section
);
773 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
774 bfd_set_error (bfd_error_bad_value
);
777 n_elt
= shdr
->sh_size
/ 4;
779 /* Look through this group's sections to see if current
780 section is a member. */
782 if ((++idx
)->shdr
== hdr
)
786 /* We are a member of this group. Go looking through
787 other members to see if any others are linked via
789 idx
= (Elf_Internal_Group
*) shdr
->contents
;
790 n_elt
= shdr
->sh_size
/ 4;
792 if ((++idx
)->shdr
!= NULL
793 && (s
= idx
->shdr
->bfd_section
) != NULL
794 && elf_next_in_group (s
) != NULL
)
798 /* Snarf the group name from other member, and
799 insert current section in circular list. */
800 elf_group_name (newsect
) = elf_group_name (s
);
801 elf_next_in_group (newsect
) = elf_next_in_group (s
);
802 elf_next_in_group (s
) = newsect
;
808 gname
= group_signature (abfd
, shdr
);
811 elf_group_name (newsect
) = gname
;
813 /* Start a circular list with one element. */
814 elf_next_in_group (newsect
) = newsect
;
817 /* If the group section has been created, point to the
819 if (shdr
->bfd_section
!= NULL
)
820 elf_next_in_group (shdr
->bfd_section
) = newsect
;
822 elf_tdata (abfd
)->group_search_offset
= i
;
829 if (elf_group_name (newsect
) == NULL
)
831 /* xgettext:c-format */
832 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
840 _bfd_elf_setup_sections (bfd
*abfd
)
843 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
844 bfd_boolean result
= TRUE
;
847 /* Process SHF_LINK_ORDER. */
848 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
850 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
851 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
853 unsigned int elfsec
= this_hdr
->sh_link
;
854 /* FIXME: The old Intel compiler and old strip/objcopy may
855 not set the sh_link or sh_info fields. Hence we could
856 get the situation where elfsec is 0. */
859 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
860 bed
->link_order_error_handler
861 /* xgettext:c-format */
862 (_("%pB: warning: sh_link not set for section `%pA'"),
867 asection
*linksec
= NULL
;
869 if (elfsec
< elf_numsections (abfd
))
871 this_hdr
= elf_elfsections (abfd
)[elfsec
];
872 linksec
= this_hdr
->bfd_section
;
876 Some strip/objcopy may leave an incorrect value in
877 sh_link. We don't want to proceed. */
881 /* xgettext:c-format */
882 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
883 s
->owner
, elfsec
, s
);
887 elf_linked_to_section (s
) = linksec
;
890 else if (this_hdr
->sh_type
== SHT_GROUP
891 && elf_next_in_group (s
) == NULL
)
894 /* xgettext:c-format */
895 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
896 abfd
, elf_section_data (s
)->this_idx
);
901 /* Process section groups. */
902 if (num_group
== (unsigned) -1)
905 for (i
= 0; i
< num_group
; i
++)
907 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
908 Elf_Internal_Group
*idx
;
911 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
912 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
915 /* xgettext:c-format */
916 (_("%pB: section group entry number %u is corrupt"),
922 idx
= (Elf_Internal_Group
*) shdr
->contents
;
923 n_elt
= shdr
->sh_size
/ 4;
929 if (idx
->shdr
== NULL
)
931 else if (idx
->shdr
->bfd_section
)
932 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
933 else if (idx
->shdr
->sh_type
!= SHT_RELA
934 && idx
->shdr
->sh_type
!= SHT_REL
)
936 /* There are some unknown sections in the group. */
938 /* xgettext:c-format */
939 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
942 bfd_elf_string_from_elf_section (abfd
,
943 (elf_elfheader (abfd
)
956 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
958 return elf_next_in_group (sec
) != NULL
;
962 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
964 if (elf_sec_group (sec
) != NULL
)
965 return elf_group_name (sec
);
970 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
972 unsigned int len
= strlen (name
);
973 char *new_name
= bfd_alloc (abfd
, len
+ 2);
974 if (new_name
== NULL
)
978 memcpy (new_name
+ 2, name
+ 1, len
);
983 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
985 unsigned int len
= strlen (name
);
986 char *new_name
= bfd_alloc (abfd
, len
);
987 if (new_name
== NULL
)
990 memcpy (new_name
+ 1, name
+ 2, len
- 1);
994 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
998 int16_t major_version
;
999 int16_t minor_version
;
1000 unsigned char slim_object
;
1002 /* Flags is a private field that is not defined publicly. */
1006 /* Make a BFD section from an ELF section. We store a pointer to the
1007 BFD section in the bfd_section field of the header. */
1010 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1011 Elf_Internal_Shdr
*hdr
,
1017 const struct elf_backend_data
*bed
;
1018 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
1020 if (hdr
->bfd_section
!= NULL
)
1023 newsect
= bfd_make_section_anyway (abfd
, name
);
1024 if (newsect
== NULL
)
1027 hdr
->bfd_section
= newsect
;
1028 elf_section_data (newsect
)->this_hdr
= *hdr
;
1029 elf_section_data (newsect
)->this_idx
= shindex
;
1031 /* Always use the real type/flags. */
1032 elf_section_type (newsect
) = hdr
->sh_type
;
1033 elf_section_flags (newsect
) = hdr
->sh_flags
;
1035 newsect
->filepos
= hdr
->sh_offset
;
1037 flags
= SEC_NO_FLAGS
;
1038 if (hdr
->sh_type
!= SHT_NOBITS
)
1039 flags
|= SEC_HAS_CONTENTS
;
1040 if (hdr
->sh_type
== SHT_GROUP
)
1042 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1045 if (hdr
->sh_type
!= SHT_NOBITS
)
1048 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1049 flags
|= SEC_READONLY
;
1050 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1052 else if ((flags
& SEC_LOAD
) != 0)
1054 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1057 newsect
->entsize
= hdr
->sh_entsize
;
1059 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1060 flags
|= SEC_STRINGS
;
1061 if (hdr
->sh_flags
& SHF_GROUP
)
1062 if (!setup_group (abfd
, hdr
, newsect
))
1064 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1065 flags
|= SEC_THREAD_LOCAL
;
1066 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1067 flags
|= SEC_EXCLUDE
;
1069 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1071 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1072 but binutils as of 2019-07-23 did not set the EI_OSABI header
1076 case ELFOSABI_FREEBSD
:
1077 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1078 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1082 if ((flags
& SEC_ALLOC
) == 0)
1084 /* The debugging sections appear to be recognized only by name,
1085 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1086 if (name
[0] == '.')
1088 if (strncmp (name
, ".debug", 6) == 0
1089 || strncmp (name
, ".gnu.linkonce.wi.", 17) == 0
1090 || strncmp (name
, ".zdebug", 7) == 0)
1091 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1092 else if (strncmp (name
, GNU_BUILD_ATTRS_SECTION_NAME
, 21) == 0
1093 || strncmp (name
, ".note.gnu", 9) == 0)
1095 flags
|= SEC_ELF_OCTETS
;
1098 else if (strncmp (name
, ".line", 5) == 0
1099 || strncmp (name
, ".stab", 5) == 0
1100 || strcmp (name
, ".gdb_index") == 0)
1101 flags
|= SEC_DEBUGGING
;
1105 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1106 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1107 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
)))
1110 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1111 only link a single copy of the section. This is used to support
1112 g++. g++ will emit each template expansion in its own section.
1113 The symbols will be defined as weak, so that multiple definitions
1114 are permitted. The GNU linker extension is to actually discard
1115 all but one of the sections. */
1116 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1117 && elf_next_in_group (newsect
) == NULL
)
1118 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1120 if (!bfd_set_section_flags (newsect
, flags
))
1123 bed
= get_elf_backend_data (abfd
);
1124 if (bed
->elf_backend_section_flags
)
1125 if (!bed
->elf_backend_section_flags (hdr
))
1128 /* We do not parse the PT_NOTE segments as we are interested even in the
1129 separate debug info files which may have the segments offsets corrupted.
1130 PT_NOTEs from the core files are currently not parsed using BFD. */
1131 if (hdr
->sh_type
== SHT_NOTE
)
1135 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1138 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1139 hdr
->sh_offset
, hdr
->sh_addralign
);
1143 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1145 Elf_Internal_Phdr
*phdr
;
1146 unsigned int i
, nload
;
1148 /* Some ELF linkers produce binaries with all the program header
1149 p_paddr fields zero. If we have such a binary with more than
1150 one PT_LOAD header, then leave the section lma equal to vma
1151 so that we don't create sections with overlapping lma. */
1152 phdr
= elf_tdata (abfd
)->phdr
;
1153 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1154 if (phdr
->p_paddr
!= 0)
1156 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1158 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1161 phdr
= elf_tdata (abfd
)->phdr
;
1162 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1164 if (((phdr
->p_type
== PT_LOAD
1165 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1166 || phdr
->p_type
== PT_TLS
)
1167 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1169 if ((newsect
->flags
& SEC_LOAD
) == 0)
1170 newsect
->lma
= (phdr
->p_paddr
1171 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1173 /* We used to use the same adjustment for SEC_LOAD
1174 sections, but that doesn't work if the segment
1175 is packed with code from multiple VMAs.
1176 Instead we calculate the section LMA based on
1177 the segment LMA. It is assumed that the
1178 segment will contain sections with contiguous
1179 LMAs, even if the VMAs are not. */
1180 newsect
->lma
= (phdr
->p_paddr
1181 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1183 /* With contiguous segments, we can't tell from file
1184 offsets whether a section with zero size should
1185 be placed at the end of one segment or the
1186 beginning of the next. Decide based on vaddr. */
1187 if (hdr
->sh_addr
>= phdr
->p_vaddr
1188 && (hdr
->sh_addr
+ hdr
->sh_size
1189 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1195 /* Compress/decompress DWARF debug sections with names: .debug_* and
1196 .zdebug_*, after the section flags is set. */
1197 if ((newsect
->flags
& SEC_DEBUGGING
)
1198 && ((name
[1] == 'd' && name
[6] == '_')
1199 || (name
[1] == 'z' && name
[7] == '_')))
1201 enum { nothing
, compress
, decompress
} action
= nothing
;
1202 int compression_header_size
;
1203 bfd_size_type uncompressed_size
;
1204 unsigned int uncompressed_align_power
;
1205 bfd_boolean compressed
1206 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1207 &compression_header_size
,
1209 &uncompressed_align_power
);
1212 /* Compressed section. Check if we should decompress. */
1213 if ((abfd
->flags
& BFD_DECOMPRESS
))
1214 action
= decompress
;
1217 /* Compress the uncompressed section or convert from/to .zdebug*
1218 section. Check if we should compress. */
1219 if (action
== nothing
)
1221 if (newsect
->size
!= 0
1222 && (abfd
->flags
& BFD_COMPRESS
)
1223 && compression_header_size
>= 0
1224 && uncompressed_size
> 0
1226 || ((compression_header_size
> 0)
1227 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1233 if (action
== compress
)
1235 if (!bfd_init_section_compress_status (abfd
, newsect
))
1238 /* xgettext:c-format */
1239 (_("%pB: unable to initialize compress status for section %s"),
1246 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1249 /* xgettext:c-format */
1250 (_("%pB: unable to initialize decompress status for section %s"),
1256 if (abfd
->is_linker_input
)
1259 && (action
== decompress
1260 || (action
== compress
1261 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1263 /* Convert section name from .zdebug_* to .debug_* so
1264 that linker will consider this section as a debug
1266 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1267 if (new_name
== NULL
)
1269 bfd_rename_section (newsect
, new_name
);
1273 /* For objdump, don't rename the section. For objcopy, delay
1274 section rename to elf_fake_sections. */
1275 newsect
->flags
|= SEC_ELF_RENAME
;
1278 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1280 const char *lto_section_name
= ".gnu.lto_.lto.";
1281 if (strncmp (name
, lto_section_name
, strlen (lto_section_name
)) == 0)
1283 struct lto_section lsection
;
1284 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1285 sizeof (struct lto_section
)))
1286 abfd
->lto_slim_object
= lsection
.slim_object
;
1292 const char *const bfd_elf_section_type_names
[] =
1294 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1295 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1296 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1299 /* ELF relocs are against symbols. If we are producing relocatable
1300 output, and the reloc is against an external symbol, and nothing
1301 has given us any additional addend, the resulting reloc will also
1302 be against the same symbol. In such a case, we don't want to
1303 change anything about the way the reloc is handled, since it will
1304 all be done at final link time. Rather than put special case code
1305 into bfd_perform_relocation, all the reloc types use this howto
1306 function. It just short circuits the reloc if producing
1307 relocatable output against an external symbol. */
1309 bfd_reloc_status_type
1310 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1311 arelent
*reloc_entry
,
1313 void *data ATTRIBUTE_UNUSED
,
1314 asection
*input_section
,
1316 char **error_message ATTRIBUTE_UNUSED
)
1318 if (output_bfd
!= NULL
1319 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1320 && (! reloc_entry
->howto
->partial_inplace
1321 || reloc_entry
->addend
== 0))
1323 reloc_entry
->address
+= input_section
->output_offset
;
1324 return bfd_reloc_ok
;
1327 return bfd_reloc_continue
;
1330 /* Returns TRUE if section A matches section B.
1331 Names, addresses and links may be different, but everything else
1332 should be the same. */
1335 section_match (const Elf_Internal_Shdr
* a
,
1336 const Elf_Internal_Shdr
* b
)
1338 if (a
->sh_type
!= b
->sh_type
1339 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1340 || a
->sh_addralign
!= b
->sh_addralign
1341 || a
->sh_entsize
!= b
->sh_entsize
)
1343 if (a
->sh_type
== SHT_SYMTAB
1344 || a
->sh_type
== SHT_STRTAB
)
1346 return a
->sh_size
== b
->sh_size
;
1349 /* Find a section in OBFD that has the same characteristics
1350 as IHEADER. Return the index of this section or SHN_UNDEF if
1351 none can be found. Check's section HINT first, as this is likely
1352 to be the correct section. */
1355 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1356 const unsigned int hint
)
1358 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1361 BFD_ASSERT (iheader
!= NULL
);
1363 /* See PR 20922 for a reproducer of the NULL test. */
1364 if (hint
< elf_numsections (obfd
)
1365 && oheaders
[hint
] != NULL
1366 && section_match (oheaders
[hint
], iheader
))
1369 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1371 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1373 if (oheader
== NULL
)
1375 if (section_match (oheader
, iheader
))
1376 /* FIXME: Do we care if there is a potential for
1377 multiple matches ? */
1384 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1385 Processor specific section, based upon a matching input section.
1386 Returns TRUE upon success, FALSE otherwise. */
1389 copy_special_section_fields (const bfd
*ibfd
,
1391 const Elf_Internal_Shdr
*iheader
,
1392 Elf_Internal_Shdr
*oheader
,
1393 const unsigned int secnum
)
1395 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1396 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1397 bfd_boolean changed
= FALSE
;
1398 unsigned int sh_link
;
1400 if (oheader
->sh_type
== SHT_NOBITS
)
1402 /* This is a feature for objcopy --only-keep-debug:
1403 When a section's type is changed to NOBITS, we preserve
1404 the sh_link and sh_info fields so that they can be
1405 matched up with the original.
1407 Note: Strictly speaking these assignments are wrong.
1408 The sh_link and sh_info fields should point to the
1409 relevent sections in the output BFD, which may not be in
1410 the same location as they were in the input BFD. But
1411 the whole point of this action is to preserve the
1412 original values of the sh_link and sh_info fields, so
1413 that they can be matched up with the section headers in
1414 the original file. So strictly speaking we may be
1415 creating an invalid ELF file, but it is only for a file
1416 that just contains debug info and only for sections
1417 without any contents. */
1418 if (oheader
->sh_link
== 0)
1419 oheader
->sh_link
= iheader
->sh_link
;
1420 if (oheader
->sh_info
== 0)
1421 oheader
->sh_info
= iheader
->sh_info
;
1425 /* Allow the target a chance to decide how these fields should be set. */
1426 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1430 /* We have an iheader which might match oheader, and which has non-zero
1431 sh_info and/or sh_link fields. Attempt to follow those links and find
1432 the section in the output bfd which corresponds to the linked section
1433 in the input bfd. */
1434 if (iheader
->sh_link
!= SHN_UNDEF
)
1436 /* See PR 20931 for a reproducer. */
1437 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1440 /* xgettext:c-format */
1441 (_("%pB: invalid sh_link field (%d) in section number %d"),
1442 ibfd
, iheader
->sh_link
, secnum
);
1446 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1447 if (sh_link
!= SHN_UNDEF
)
1449 oheader
->sh_link
= sh_link
;
1453 /* FIXME: Should we install iheader->sh_link
1454 if we could not find a match ? */
1456 /* xgettext:c-format */
1457 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1460 if (iheader
->sh_info
)
1462 /* The sh_info field can hold arbitrary information, but if the
1463 SHF_LINK_INFO flag is set then it should be interpreted as a
1465 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1467 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1469 if (sh_link
!= SHN_UNDEF
)
1470 oheader
->sh_flags
|= SHF_INFO_LINK
;
1473 /* No idea what it means - just copy it. */
1474 sh_link
= iheader
->sh_info
;
1476 if (sh_link
!= SHN_UNDEF
)
1478 oheader
->sh_info
= sh_link
;
1483 /* xgettext:c-format */
1484 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1490 /* Copy the program header and other data from one object module to
1494 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1496 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1497 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1498 const struct elf_backend_data
*bed
;
1501 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1502 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1505 if (!elf_flags_init (obfd
))
1507 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1508 elf_flags_init (obfd
) = TRUE
;
1511 elf_gp (obfd
) = elf_gp (ibfd
);
1513 /* Also copy the EI_OSABI field. */
1514 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1515 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1517 /* If set, copy the EI_ABIVERSION field. */
1518 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1519 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1520 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1522 /* Copy object attributes. */
1523 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1525 if (iheaders
== NULL
|| oheaders
== NULL
)
1528 bed
= get_elf_backend_data (obfd
);
1530 /* Possibly copy other fields in the section header. */
1531 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1534 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1536 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1537 because of a special case need for generating separate debug info
1538 files. See below for more details. */
1540 || (oheader
->sh_type
!= SHT_NOBITS
1541 && oheader
->sh_type
< SHT_LOOS
))
1544 /* Ignore empty sections, and sections whose
1545 fields have already been initialised. */
1546 if (oheader
->sh_size
== 0
1547 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1550 /* Scan for the matching section in the input bfd.
1551 First we try for a direct mapping between the input and output sections. */
1552 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1554 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1556 if (iheader
== NULL
)
1559 if (oheader
->bfd_section
!= NULL
1560 && iheader
->bfd_section
!= NULL
1561 && iheader
->bfd_section
->output_section
!= NULL
1562 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1564 /* We have found a connection from the input section to the
1565 output section. Attempt to copy the header fields. If
1566 this fails then do not try any further sections - there
1567 should only be a one-to-one mapping between input and output. */
1568 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1569 j
= elf_numsections (ibfd
);
1574 if (j
< elf_numsections (ibfd
))
1577 /* That failed. So try to deduce the corresponding input section.
1578 Unfortunately we cannot compare names as the output string table
1579 is empty, so instead we check size, address and type. */
1580 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1582 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1584 if (iheader
== NULL
)
1587 /* Try matching fields in the input section's header.
1588 Since --only-keep-debug turns all non-debug sections into
1589 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1591 if ((oheader
->sh_type
== SHT_NOBITS
1592 || iheader
->sh_type
== oheader
->sh_type
)
1593 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1594 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1595 && iheader
->sh_addralign
== oheader
->sh_addralign
1596 && iheader
->sh_entsize
== oheader
->sh_entsize
1597 && iheader
->sh_size
== oheader
->sh_size
1598 && iheader
->sh_addr
== oheader
->sh_addr
1599 && (iheader
->sh_info
!= oheader
->sh_info
1600 || iheader
->sh_link
!= oheader
->sh_link
))
1602 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1607 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1609 /* Final attempt. Call the backend copy function
1610 with a NULL input section. */
1611 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1620 get_segment_type (unsigned int p_type
)
1625 case PT_NULL
: pt
= "NULL"; break;
1626 case PT_LOAD
: pt
= "LOAD"; break;
1627 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1628 case PT_INTERP
: pt
= "INTERP"; break;
1629 case PT_NOTE
: pt
= "NOTE"; break;
1630 case PT_SHLIB
: pt
= "SHLIB"; break;
1631 case PT_PHDR
: pt
= "PHDR"; break;
1632 case PT_TLS
: pt
= "TLS"; break;
1633 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1634 case PT_GNU_STACK
: pt
= "STACK"; break;
1635 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1636 default: pt
= NULL
; break;
1641 /* Print out the program headers. */
1644 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1646 FILE *f
= (FILE *) farg
;
1647 Elf_Internal_Phdr
*p
;
1649 bfd_byte
*dynbuf
= NULL
;
1651 p
= elf_tdata (abfd
)->phdr
;
1656 fprintf (f
, _("\nProgram Header:\n"));
1657 c
= elf_elfheader (abfd
)->e_phnum
;
1658 for (i
= 0; i
< c
; i
++, p
++)
1660 const char *pt
= get_segment_type (p
->p_type
);
1665 sprintf (buf
, "0x%lx", p
->p_type
);
1668 fprintf (f
, "%8s off 0x", pt
);
1669 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1670 fprintf (f
, " vaddr 0x");
1671 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1672 fprintf (f
, " paddr 0x");
1673 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1674 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1675 fprintf (f
, " filesz 0x");
1676 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1677 fprintf (f
, " memsz 0x");
1678 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1679 fprintf (f
, " flags %c%c%c",
1680 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1681 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1682 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1683 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1684 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1689 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1692 unsigned int elfsec
;
1693 unsigned long shlink
;
1694 bfd_byte
*extdyn
, *extdynend
;
1696 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1698 fprintf (f
, _("\nDynamic Section:\n"));
1700 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1703 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1704 if (elfsec
== SHN_BAD
)
1706 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1708 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1709 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1712 /* PR 17512: file: 6f427532. */
1713 if (s
->size
< extdynsize
)
1715 extdynend
= extdyn
+ s
->size
;
1716 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1718 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1720 Elf_Internal_Dyn dyn
;
1721 const char *name
= "";
1723 bfd_boolean stringp
;
1724 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1726 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1728 if (dyn
.d_tag
== DT_NULL
)
1735 if (bed
->elf_backend_get_target_dtag
)
1736 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1738 if (!strcmp (name
, ""))
1740 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1745 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1746 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1747 case DT_PLTGOT
: name
= "PLTGOT"; break;
1748 case DT_HASH
: name
= "HASH"; break;
1749 case DT_STRTAB
: name
= "STRTAB"; break;
1750 case DT_SYMTAB
: name
= "SYMTAB"; break;
1751 case DT_RELA
: name
= "RELA"; break;
1752 case DT_RELASZ
: name
= "RELASZ"; break;
1753 case DT_RELAENT
: name
= "RELAENT"; break;
1754 case DT_STRSZ
: name
= "STRSZ"; break;
1755 case DT_SYMENT
: name
= "SYMENT"; break;
1756 case DT_INIT
: name
= "INIT"; break;
1757 case DT_FINI
: name
= "FINI"; break;
1758 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1759 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1760 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1761 case DT_REL
: name
= "REL"; break;
1762 case DT_RELSZ
: name
= "RELSZ"; break;
1763 case DT_RELENT
: name
= "RELENT"; break;
1764 case DT_PLTREL
: name
= "PLTREL"; break;
1765 case DT_DEBUG
: name
= "DEBUG"; break;
1766 case DT_TEXTREL
: name
= "TEXTREL"; break;
1767 case DT_JMPREL
: name
= "JMPREL"; break;
1768 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1769 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1770 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1771 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1772 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1773 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1774 case DT_FLAGS
: name
= "FLAGS"; break;
1775 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1776 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1777 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1778 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1779 case DT_MOVEENT
: name
= "MOVEENT"; break;
1780 case DT_MOVESZ
: name
= "MOVESZ"; break;
1781 case DT_FEATURE
: name
= "FEATURE"; break;
1782 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1783 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1784 case DT_SYMINENT
: name
= "SYMINENT"; break;
1785 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1786 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1787 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1788 case DT_PLTPAD
: name
= "PLTPAD"; break;
1789 case DT_MOVETAB
: name
= "MOVETAB"; break;
1790 case DT_SYMINFO
: name
= "SYMINFO"; break;
1791 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1792 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1793 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1794 case DT_VERSYM
: name
= "VERSYM"; break;
1795 case DT_VERDEF
: name
= "VERDEF"; break;
1796 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1797 case DT_VERNEED
: name
= "VERNEED"; break;
1798 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1799 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1800 case DT_USED
: name
= "USED"; break;
1801 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1802 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1805 fprintf (f
, " %-20s ", name
);
1809 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1814 unsigned int tagv
= dyn
.d_un
.d_val
;
1816 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1819 fprintf (f
, "%s", string
);
1828 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1829 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1831 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1835 if (elf_dynverdef (abfd
) != 0)
1837 Elf_Internal_Verdef
*t
;
1839 fprintf (f
, _("\nVersion definitions:\n"));
1840 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1842 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1843 t
->vd_flags
, t
->vd_hash
,
1844 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1845 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1847 Elf_Internal_Verdaux
*a
;
1850 for (a
= t
->vd_auxptr
->vda_nextptr
;
1854 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1860 if (elf_dynverref (abfd
) != 0)
1862 Elf_Internal_Verneed
*t
;
1864 fprintf (f
, _("\nVersion References:\n"));
1865 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1867 Elf_Internal_Vernaux
*a
;
1869 fprintf (f
, _(" required from %s:\n"),
1870 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1871 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1872 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1873 a
->vna_flags
, a
->vna_other
,
1874 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1886 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1887 and return symbol version for symbol version itself. */
1890 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1892 bfd_boolean
*hidden
)
1894 const char *version_string
= NULL
;
1895 if (elf_dynversym (abfd
) != 0
1896 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1898 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1900 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1901 vernum
&= VERSYM_VERSION
;
1904 version_string
= "";
1905 else if (vernum
== 1
1906 && (vernum
> elf_tdata (abfd
)->cverdefs
1907 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1909 version_string
= base_p
? "Base" : "";
1910 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1912 const char *nodename
1913 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1914 version_string
= ((base_p
|| strcmp (symbol
->name
, nodename
))
1919 Elf_Internal_Verneed
*t
;
1921 version_string
= _("<corrupt>");
1922 for (t
= elf_tdata (abfd
)->verref
;
1926 Elf_Internal_Vernaux
*a
;
1928 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1930 if (a
->vna_other
== vernum
)
1932 version_string
= a
->vna_nodename
;
1939 return version_string
;
1942 /* Display ELF-specific fields of a symbol. */
1945 bfd_elf_print_symbol (bfd
*abfd
,
1948 bfd_print_symbol_type how
)
1950 FILE *file
= (FILE *) filep
;
1953 case bfd_print_symbol_name
:
1954 fprintf (file
, "%s", symbol
->name
);
1956 case bfd_print_symbol_more
:
1957 fprintf (file
, "elf ");
1958 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1959 fprintf (file
, " %x", symbol
->flags
);
1961 case bfd_print_symbol_all
:
1963 const char *section_name
;
1964 const char *name
= NULL
;
1965 const struct elf_backend_data
*bed
;
1966 unsigned char st_other
;
1968 const char *version_string
;
1971 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1973 bed
= get_elf_backend_data (abfd
);
1974 if (bed
->elf_backend_print_symbol_all
)
1975 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1979 name
= symbol
->name
;
1980 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1983 fprintf (file
, " %s\t", section_name
);
1984 /* Print the "other" value for a symbol. For common symbols,
1985 we've already printed the size; now print the alignment.
1986 For other symbols, we have no specified alignment, and
1987 we've printed the address; now print the size. */
1988 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1989 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1991 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1992 bfd_fprintf_vma (abfd
, file
, val
);
1994 /* If we have version information, print it. */
1995 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2002 fprintf (file
, " %-11s", version_string
);
2007 fprintf (file
, " (%s)", version_string
);
2008 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2013 /* If the st_other field is not zero, print it. */
2014 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2019 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2020 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2021 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2023 /* Some other non-defined flags are also present, so print
2025 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2028 fprintf (file
, " %s", name
);
2034 /* ELF .o/exec file reading */
2036 /* Create a new bfd section from an ELF section header. */
2039 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2041 Elf_Internal_Shdr
*hdr
;
2042 Elf_Internal_Ehdr
*ehdr
;
2043 const struct elf_backend_data
*bed
;
2045 bfd_boolean ret
= TRUE
;
2046 static bfd_boolean
* sections_being_created
= NULL
;
2047 static bfd
* sections_being_created_abfd
= NULL
;
2048 static unsigned int nesting
= 0;
2050 if (shindex
>= elf_numsections (abfd
))
2055 /* PR17512: A corrupt ELF binary might contain a recursive group of
2056 sections, with each the string indices pointing to the next in the
2057 loop. Detect this here, by refusing to load a section that we are
2058 already in the process of loading. We only trigger this test if
2059 we have nested at least three sections deep as normal ELF binaries
2060 can expect to recurse at least once.
2062 FIXME: It would be better if this array was attached to the bfd,
2063 rather than being held in a static pointer. */
2065 if (sections_being_created_abfd
!= abfd
)
2066 sections_being_created
= NULL
;
2067 if (sections_being_created
== NULL
)
2069 size_t amt
= elf_numsections (abfd
) * sizeof (bfd_boolean
);
2070 sections_being_created
= (bfd_boolean
*) bfd_zalloc (abfd
, amt
);
2071 if (sections_being_created
== NULL
)
2073 sections_being_created_abfd
= abfd
;
2075 if (sections_being_created
[shindex
])
2078 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2081 sections_being_created
[shindex
] = TRUE
;
2084 hdr
= elf_elfsections (abfd
)[shindex
];
2085 ehdr
= elf_elfheader (abfd
);
2086 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2091 bed
= get_elf_backend_data (abfd
);
2092 switch (hdr
->sh_type
)
2095 /* Inactive section. Throw it away. */
2098 case SHT_PROGBITS
: /* Normal section with contents. */
2099 case SHT_NOBITS
: /* .bss section. */
2100 case SHT_HASH
: /* .hash section. */
2101 case SHT_NOTE
: /* .note section. */
2102 case SHT_INIT_ARRAY
: /* .init_array section. */
2103 case SHT_FINI_ARRAY
: /* .fini_array section. */
2104 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2105 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2106 case SHT_GNU_HASH
: /* .gnu.hash section. */
2107 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2110 case SHT_DYNAMIC
: /* Dynamic linking information. */
2111 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2114 if (hdr
->sh_link
> elf_numsections (abfd
))
2116 /* PR 10478: Accept Solaris binaries with a sh_link
2117 field set to SHN_BEFORE or SHN_AFTER. */
2118 switch (bfd_get_arch (abfd
))
2121 case bfd_arch_sparc
:
2122 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2123 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2125 /* Otherwise fall through. */
2130 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2132 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2134 Elf_Internal_Shdr
*dynsymhdr
;
2136 /* The shared libraries distributed with hpux11 have a bogus
2137 sh_link field for the ".dynamic" section. Find the
2138 string table for the ".dynsym" section instead. */
2139 if (elf_dynsymtab (abfd
) != 0)
2141 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2142 hdr
->sh_link
= dynsymhdr
->sh_link
;
2146 unsigned int i
, num_sec
;
2148 num_sec
= elf_numsections (abfd
);
2149 for (i
= 1; i
< num_sec
; i
++)
2151 dynsymhdr
= elf_elfsections (abfd
)[i
];
2152 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2154 hdr
->sh_link
= dynsymhdr
->sh_link
;
2162 case SHT_SYMTAB
: /* A symbol table. */
2163 if (elf_onesymtab (abfd
) == shindex
)
2166 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2169 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2171 if (hdr
->sh_size
!= 0)
2173 /* Some assemblers erroneously set sh_info to one with a
2174 zero sh_size. ld sees this as a global symbol count
2175 of (unsigned) -1. Fix it here. */
2180 /* PR 18854: A binary might contain more than one symbol table.
2181 Unusual, but possible. Warn, but continue. */
2182 if (elf_onesymtab (abfd
) != 0)
2185 /* xgettext:c-format */
2186 (_("%pB: warning: multiple symbol tables detected"
2187 " - ignoring the table in section %u"),
2191 elf_onesymtab (abfd
) = shindex
;
2192 elf_symtab_hdr (abfd
) = *hdr
;
2193 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2194 abfd
->flags
|= HAS_SYMS
;
2196 /* Sometimes a shared object will map in the symbol table. If
2197 SHF_ALLOC is set, and this is a shared object, then we also
2198 treat this section as a BFD section. We can not base the
2199 decision purely on SHF_ALLOC, because that flag is sometimes
2200 set in a relocatable object file, which would confuse the
2202 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2203 && (abfd
->flags
& DYNAMIC
) != 0
2204 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2208 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2209 can't read symbols without that section loaded as well. It
2210 is most likely specified by the next section header. */
2212 elf_section_list
* entry
;
2213 unsigned int i
, num_sec
;
2215 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2216 if (entry
->hdr
.sh_link
== shindex
)
2219 num_sec
= elf_numsections (abfd
);
2220 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2222 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2224 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2225 && hdr2
->sh_link
== shindex
)
2230 for (i
= 1; i
< shindex
; i
++)
2232 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2234 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2235 && hdr2
->sh_link
== shindex
)
2240 ret
= bfd_section_from_shdr (abfd
, i
);
2241 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2245 case SHT_DYNSYM
: /* A dynamic symbol table. */
2246 if (elf_dynsymtab (abfd
) == shindex
)
2249 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2252 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2254 if (hdr
->sh_size
!= 0)
2257 /* Some linkers erroneously set sh_info to one with a
2258 zero sh_size. ld sees this as a global symbol count
2259 of (unsigned) -1. Fix it here. */
2264 /* PR 18854: A binary might contain more than one dynamic symbol table.
2265 Unusual, but possible. Warn, but continue. */
2266 if (elf_dynsymtab (abfd
) != 0)
2269 /* xgettext:c-format */
2270 (_("%pB: warning: multiple dynamic symbol tables detected"
2271 " - ignoring the table in section %u"),
2275 elf_dynsymtab (abfd
) = shindex
;
2276 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2277 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2278 abfd
->flags
|= HAS_SYMS
;
2280 /* Besides being a symbol table, we also treat this as a regular
2281 section, so that objcopy can handle it. */
2282 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2285 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2287 elf_section_list
* entry
;
2289 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2290 if (entry
->ndx
== shindex
)
2293 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2296 entry
->ndx
= shindex
;
2298 entry
->next
= elf_symtab_shndx_list (abfd
);
2299 elf_symtab_shndx_list (abfd
) = entry
;
2300 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2304 case SHT_STRTAB
: /* A string table. */
2305 if (hdr
->bfd_section
!= NULL
)
2308 if (ehdr
->e_shstrndx
== shindex
)
2310 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2311 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2315 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2318 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2319 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2323 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2326 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2327 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2328 elf_elfsections (abfd
)[shindex
] = hdr
;
2329 /* We also treat this as a regular section, so that objcopy
2331 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2336 /* If the string table isn't one of the above, then treat it as a
2337 regular section. We need to scan all the headers to be sure,
2338 just in case this strtab section appeared before the above. */
2339 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2341 unsigned int i
, num_sec
;
2343 num_sec
= elf_numsections (abfd
);
2344 for (i
= 1; i
< num_sec
; i
++)
2346 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2347 if (hdr2
->sh_link
== shindex
)
2349 /* Prevent endless recursion on broken objects. */
2352 if (! bfd_section_from_shdr (abfd
, i
))
2354 if (elf_onesymtab (abfd
) == i
)
2356 if (elf_dynsymtab (abfd
) == i
)
2357 goto dynsymtab_strtab
;
2361 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2366 /* *These* do a lot of work -- but build no sections! */
2368 asection
*target_sect
;
2369 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2370 unsigned int num_sec
= elf_numsections (abfd
);
2371 struct bfd_elf_section_data
*esdt
;
2374 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2375 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2378 /* Check for a bogus link to avoid crashing. */
2379 if (hdr
->sh_link
>= num_sec
)
2382 /* xgettext:c-format */
2383 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2384 abfd
, hdr
->sh_link
, name
, shindex
);
2385 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2390 /* For some incomprehensible reason Oracle distributes
2391 libraries for Solaris in which some of the objects have
2392 bogus sh_link fields. It would be nice if we could just
2393 reject them, but, unfortunately, some people need to use
2394 them. We scan through the section headers; if we find only
2395 one suitable symbol table, we clobber the sh_link to point
2396 to it. I hope this doesn't break anything.
2398 Don't do it on executable nor shared library. */
2399 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2400 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2401 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2407 for (scan
= 1; scan
< num_sec
; scan
++)
2409 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2410 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2421 hdr
->sh_link
= found
;
2424 /* Get the symbol table. */
2425 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2426 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2427 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2430 /* If this is an alloc section in an executable or shared
2431 library, or the reloc section does not use the main symbol
2432 table we don't treat it as a reloc section. BFD can't
2433 adequately represent such a section, so at least for now,
2434 we don't try. We just present it as a normal section. We
2435 also can't use it as a reloc section if it points to the
2436 null section, an invalid section, another reloc section, or
2437 its sh_link points to the null section. */
2438 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2439 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2440 || hdr
->sh_link
== SHN_UNDEF
2441 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2442 || hdr
->sh_info
== SHN_UNDEF
2443 || hdr
->sh_info
>= num_sec
2444 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2445 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2447 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2452 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2455 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2456 if (target_sect
== NULL
)
2459 esdt
= elf_section_data (target_sect
);
2460 if (hdr
->sh_type
== SHT_RELA
)
2461 p_hdr
= &esdt
->rela
.hdr
;
2463 p_hdr
= &esdt
->rel
.hdr
;
2465 /* PR 17512: file: 0b4f81b7.
2466 Also see PR 24456, for a file which deliberately has two reloc
2470 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2473 /* xgettext:c-format */
2474 (_("%pB: warning: secondary relocation section '%s' "
2475 "for section %pA found - ignoring"),
2476 abfd
, name
, target_sect
);
2481 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2486 elf_elfsections (abfd
)[shindex
] = hdr2
;
2487 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2488 * bed
->s
->int_rels_per_ext_rel
);
2489 target_sect
->flags
|= SEC_RELOC
;
2490 target_sect
->relocation
= NULL
;
2491 target_sect
->rel_filepos
= hdr
->sh_offset
;
2492 /* In the section to which the relocations apply, mark whether
2493 its relocations are of the REL or RELA variety. */
2494 if (hdr
->sh_size
!= 0)
2496 if (hdr
->sh_type
== SHT_RELA
)
2497 target_sect
->use_rela_p
= 1;
2499 abfd
->flags
|= HAS_RELOC
;
2503 case SHT_GNU_verdef
:
2504 elf_dynverdef (abfd
) = shindex
;
2505 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2506 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2509 case SHT_GNU_versym
:
2510 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2513 elf_dynversym (abfd
) = shindex
;
2514 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2515 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2518 case SHT_GNU_verneed
:
2519 elf_dynverref (abfd
) = shindex
;
2520 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2521 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2528 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2531 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2537 /* Possibly an attributes section. */
2538 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2539 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2541 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2543 _bfd_elf_parse_attributes (abfd
, hdr
);
2547 /* Check for any processor-specific section types. */
2548 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2551 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2553 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2554 /* FIXME: How to properly handle allocated section reserved
2555 for applications? */
2557 /* xgettext:c-format */
2558 (_("%pB: unknown type [%#x] section `%s'"),
2559 abfd
, hdr
->sh_type
, name
);
2562 /* Allow sections reserved for applications. */
2563 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2568 else if (hdr
->sh_type
>= SHT_LOPROC
2569 && hdr
->sh_type
<= SHT_HIPROC
)
2570 /* FIXME: We should handle this section. */
2572 /* xgettext:c-format */
2573 (_("%pB: unknown type [%#x] section `%s'"),
2574 abfd
, hdr
->sh_type
, name
);
2575 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2577 /* Unrecognised OS-specific sections. */
2578 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2579 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2580 required to correctly process the section and the file should
2581 be rejected with an error message. */
2583 /* xgettext:c-format */
2584 (_("%pB: unknown type [%#x] section `%s'"),
2585 abfd
, hdr
->sh_type
, name
);
2588 /* Otherwise it should be processed. */
2589 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2594 /* FIXME: We should handle this section. */
2596 /* xgettext:c-format */
2597 (_("%pB: unknown type [%#x] section `%s'"),
2598 abfd
, hdr
->sh_type
, name
);
2606 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2607 sections_being_created
[shindex
] = FALSE
;
2608 if (-- nesting
== 0)
2610 sections_being_created
= NULL
;
2611 sections_being_created_abfd
= abfd
;
2616 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2619 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2621 unsigned long r_symndx
)
2623 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2625 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2627 Elf_Internal_Shdr
*symtab_hdr
;
2628 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2629 Elf_External_Sym_Shndx eshndx
;
2631 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2632 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2633 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2636 if (cache
->abfd
!= abfd
)
2638 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2641 cache
->indx
[ent
] = r_symndx
;
2644 return &cache
->sym
[ent
];
2647 /* Given an ELF section number, retrieve the corresponding BFD
2651 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2653 if (sec_index
>= elf_numsections (abfd
))
2655 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2658 static const struct bfd_elf_special_section special_sections_b
[] =
2660 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2661 { NULL
, 0, 0, 0, 0 }
2664 static const struct bfd_elf_special_section special_sections_c
[] =
2666 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2667 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2668 { NULL
, 0, 0, 0, 0 }
2671 static const struct bfd_elf_special_section special_sections_d
[] =
2673 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2674 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2675 /* There are more DWARF sections than these, but they needn't be added here
2676 unless you have to cope with broken compilers that don't emit section
2677 attributes or you want to help the user writing assembler. */
2678 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2679 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2680 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2681 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2682 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2683 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2684 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2685 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2686 { NULL
, 0, 0, 0, 0 }
2689 static const struct bfd_elf_special_section special_sections_f
[] =
2691 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2692 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2693 { NULL
, 0 , 0, 0, 0 }
2696 static const struct bfd_elf_special_section special_sections_g
[] =
2698 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2699 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2700 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2701 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2702 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2703 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2704 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2705 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2706 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2707 { NULL
, 0, 0, 0, 0 }
2710 static const struct bfd_elf_special_section special_sections_h
[] =
2712 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2713 { NULL
, 0, 0, 0, 0 }
2716 static const struct bfd_elf_special_section special_sections_i
[] =
2718 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2719 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2720 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2721 { NULL
, 0, 0, 0, 0 }
2724 static const struct bfd_elf_special_section special_sections_l
[] =
2726 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2727 { NULL
, 0, 0, 0, 0 }
2730 static const struct bfd_elf_special_section special_sections_n
[] =
2732 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2733 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2734 { NULL
, 0, 0, 0, 0 }
2737 static const struct bfd_elf_special_section special_sections_p
[] =
2739 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2740 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2741 { NULL
, 0, 0, 0, 0 }
2744 static const struct bfd_elf_special_section special_sections_r
[] =
2746 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2747 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2748 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2749 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2750 { NULL
, 0, 0, 0, 0 }
2753 static const struct bfd_elf_special_section special_sections_s
[] =
2755 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2756 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2757 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2758 /* See struct bfd_elf_special_section declaration for the semantics of
2759 this special case where .prefix_length != strlen (.prefix). */
2760 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2761 { NULL
, 0, 0, 0, 0 }
2764 static const struct bfd_elf_special_section special_sections_t
[] =
2766 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2767 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2768 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2769 { NULL
, 0, 0, 0, 0 }
2772 static const struct bfd_elf_special_section special_sections_z
[] =
2774 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2775 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2776 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2777 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2778 { NULL
, 0, 0, 0, 0 }
2781 static const struct bfd_elf_special_section
* const special_sections
[] =
2783 special_sections_b
, /* 'b' */
2784 special_sections_c
, /* 'c' */
2785 special_sections_d
, /* 'd' */
2787 special_sections_f
, /* 'f' */
2788 special_sections_g
, /* 'g' */
2789 special_sections_h
, /* 'h' */
2790 special_sections_i
, /* 'i' */
2793 special_sections_l
, /* 'l' */
2795 special_sections_n
, /* 'n' */
2797 special_sections_p
, /* 'p' */
2799 special_sections_r
, /* 'r' */
2800 special_sections_s
, /* 's' */
2801 special_sections_t
, /* 't' */
2807 special_sections_z
/* 'z' */
2810 const struct bfd_elf_special_section
*
2811 _bfd_elf_get_special_section (const char *name
,
2812 const struct bfd_elf_special_section
*spec
,
2818 len
= strlen (name
);
2820 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2823 int prefix_len
= spec
[i
].prefix_length
;
2825 if (len
< prefix_len
)
2827 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2830 suffix_len
= spec
[i
].suffix_length
;
2831 if (suffix_len
<= 0)
2833 if (name
[prefix_len
] != 0)
2835 if (suffix_len
== 0)
2837 if (name
[prefix_len
] != '.'
2838 && (suffix_len
== -2
2839 || (rela
&& spec
[i
].type
== SHT_REL
)))
2845 if (len
< prefix_len
+ suffix_len
)
2847 if (memcmp (name
+ len
- suffix_len
,
2848 spec
[i
].prefix
+ prefix_len
,
2858 const struct bfd_elf_special_section
*
2859 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2862 const struct bfd_elf_special_section
*spec
;
2863 const struct elf_backend_data
*bed
;
2865 /* See if this is one of the special sections. */
2866 if (sec
->name
== NULL
)
2869 bed
= get_elf_backend_data (abfd
);
2870 spec
= bed
->special_sections
;
2873 spec
= _bfd_elf_get_special_section (sec
->name
,
2874 bed
->special_sections
,
2880 if (sec
->name
[0] != '.')
2883 i
= sec
->name
[1] - 'b';
2884 if (i
< 0 || i
> 'z' - 'b')
2887 spec
= special_sections
[i
];
2892 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2896 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2898 struct bfd_elf_section_data
*sdata
;
2899 const struct elf_backend_data
*bed
;
2900 const struct bfd_elf_special_section
*ssect
;
2902 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2905 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2909 sec
->used_by_bfd
= sdata
;
2912 /* Indicate whether or not this section should use RELA relocations. */
2913 bed
= get_elf_backend_data (abfd
);
2914 sec
->use_rela_p
= bed
->default_use_rela_p
;
2916 /* Set up ELF section type and flags for newly created sections, if
2917 there is an ABI mandated section. */
2918 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2921 elf_section_type (sec
) = ssect
->type
;
2922 elf_section_flags (sec
) = ssect
->attr
;
2925 return _bfd_generic_new_section_hook (abfd
, sec
);
2928 /* Create a new bfd section from an ELF program header.
2930 Since program segments have no names, we generate a synthetic name
2931 of the form segment<NUM>, where NUM is generally the index in the
2932 program header table. For segments that are split (see below) we
2933 generate the names segment<NUM>a and segment<NUM>b.
2935 Note that some program segments may have a file size that is different than
2936 (less than) the memory size. All this means is that at execution the
2937 system must allocate the amount of memory specified by the memory size,
2938 but only initialize it with the first "file size" bytes read from the
2939 file. This would occur for example, with program segments consisting
2940 of combined data+bss.
2942 To handle the above situation, this routine generates TWO bfd sections
2943 for the single program segment. The first has the length specified by
2944 the file size of the segment, and the second has the length specified
2945 by the difference between the two sizes. In effect, the segment is split
2946 into its initialized and uninitialized parts.
2951 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2952 Elf_Internal_Phdr
*hdr
,
2954 const char *type_name
)
2961 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
2963 split
= ((hdr
->p_memsz
> 0)
2964 && (hdr
->p_filesz
> 0)
2965 && (hdr
->p_memsz
> hdr
->p_filesz
));
2967 if (hdr
->p_filesz
> 0)
2969 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2970 len
= strlen (namebuf
) + 1;
2971 name
= (char *) bfd_alloc (abfd
, len
);
2974 memcpy (name
, namebuf
, len
);
2975 newsect
= bfd_make_section (abfd
, name
);
2976 if (newsect
== NULL
)
2978 newsect
->vma
= hdr
->p_vaddr
/ opb
;
2979 newsect
->lma
= hdr
->p_paddr
/ opb
;
2980 newsect
->size
= hdr
->p_filesz
;
2981 newsect
->filepos
= hdr
->p_offset
;
2982 newsect
->flags
|= SEC_HAS_CONTENTS
;
2983 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2984 if (hdr
->p_type
== PT_LOAD
)
2986 newsect
->flags
|= SEC_ALLOC
;
2987 newsect
->flags
|= SEC_LOAD
;
2988 if (hdr
->p_flags
& PF_X
)
2990 /* FIXME: all we known is that it has execute PERMISSION,
2992 newsect
->flags
|= SEC_CODE
;
2995 if (!(hdr
->p_flags
& PF_W
))
2997 newsect
->flags
|= SEC_READONLY
;
3001 if (hdr
->p_memsz
> hdr
->p_filesz
)
3005 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3006 len
= strlen (namebuf
) + 1;
3007 name
= (char *) bfd_alloc (abfd
, len
);
3010 memcpy (name
, namebuf
, len
);
3011 newsect
= bfd_make_section (abfd
, name
);
3012 if (newsect
== NULL
)
3014 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3015 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3016 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3017 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3018 align
= newsect
->vma
& -newsect
->vma
;
3019 if (align
== 0 || align
> hdr
->p_align
)
3020 align
= hdr
->p_align
;
3021 newsect
->alignment_power
= bfd_log2 (align
);
3022 if (hdr
->p_type
== PT_LOAD
)
3024 /* Hack for gdb. Segments that have not been modified do
3025 not have their contents written to a core file, on the
3026 assumption that a debugger can find the contents in the
3027 executable. We flag this case by setting the fake
3028 section size to zero. Note that "real" bss sections will
3029 always have their contents dumped to the core file. */
3030 if (bfd_get_format (abfd
) == bfd_core
)
3032 newsect
->flags
|= SEC_ALLOC
;
3033 if (hdr
->p_flags
& PF_X
)
3034 newsect
->flags
|= SEC_CODE
;
3036 if (!(hdr
->p_flags
& PF_W
))
3037 newsect
->flags
|= SEC_READONLY
;
3044 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3046 /* The return value is ignored. Build-ids are considered optional. */
3047 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3048 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3054 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3056 const struct elf_backend_data
*bed
;
3058 switch (hdr
->p_type
)
3061 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3064 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3066 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3067 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3071 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3074 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3077 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3079 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3085 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3088 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3090 case PT_GNU_EH_FRAME
:
3091 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3095 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3098 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3101 /* Check for any processor-specific program segment types. */
3102 bed
= get_elf_backend_data (abfd
);
3103 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3107 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3111 _bfd_elf_single_rel_hdr (asection
*sec
)
3113 if (elf_section_data (sec
)->rel
.hdr
)
3115 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3116 return elf_section_data (sec
)->rel
.hdr
;
3119 return elf_section_data (sec
)->rela
.hdr
;
3123 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3124 Elf_Internal_Shdr
*rel_hdr
,
3125 const char *sec_name
,
3126 bfd_boolean use_rela_p
)
3128 char *name
= (char *) bfd_alloc (abfd
,
3129 sizeof ".rela" + strlen (sec_name
));
3133 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3135 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3137 if (rel_hdr
->sh_name
== (unsigned int) -1)
3143 /* Allocate and initialize a section-header for a new reloc section,
3144 containing relocations against ASECT. It is stored in RELDATA. If
3145 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3149 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3150 struct bfd_elf_section_reloc_data
*reldata
,
3151 const char *sec_name
,
3152 bfd_boolean use_rela_p
,
3153 bfd_boolean delay_st_name_p
)
3155 Elf_Internal_Shdr
*rel_hdr
;
3156 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3158 BFD_ASSERT (reldata
->hdr
== NULL
);
3159 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3160 reldata
->hdr
= rel_hdr
;
3162 if (delay_st_name_p
)
3163 rel_hdr
->sh_name
= (unsigned int) -1;
3164 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3167 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3168 rel_hdr
->sh_entsize
= (use_rela_p
3169 ? bed
->s
->sizeof_rela
3170 : bed
->s
->sizeof_rel
);
3171 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3172 rel_hdr
->sh_flags
= 0;
3173 rel_hdr
->sh_addr
= 0;
3174 rel_hdr
->sh_size
= 0;
3175 rel_hdr
->sh_offset
= 0;
3180 /* Return the default section type based on the passed in section flags. */
3183 bfd_elf_get_default_section_type (flagword flags
)
3185 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3186 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3188 return SHT_PROGBITS
;
3191 struct fake_section_arg
3193 struct bfd_link_info
*link_info
;
3197 /* Set up an ELF internal section header for a section. */
3200 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3202 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3203 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3204 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3205 Elf_Internal_Shdr
*this_hdr
;
3206 unsigned int sh_type
;
3207 const char *name
= asect
->name
;
3208 bfd_boolean delay_st_name_p
= FALSE
;
3213 /* We already failed; just get out of the bfd_map_over_sections
3218 this_hdr
= &esd
->this_hdr
;
3222 /* ld: compress DWARF debug sections with names: .debug_*. */
3223 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3224 && (asect
->flags
& SEC_DEBUGGING
)
3228 /* Set SEC_ELF_COMPRESS to indicate this section should be
3230 asect
->flags
|= SEC_ELF_COMPRESS
;
3231 /* If this section will be compressed, delay adding section
3232 name to section name section after it is compressed in
3233 _bfd_elf_assign_file_positions_for_non_load. */
3234 delay_st_name_p
= TRUE
;
3237 else if ((asect
->flags
& SEC_ELF_RENAME
))
3239 /* objcopy: rename output DWARF debug section. */
3240 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3242 /* When we decompress or compress with SHF_COMPRESSED,
3243 convert section name from .zdebug_* to .debug_* if
3247 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3248 if (new_name
== NULL
)
3256 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3258 /* PR binutils/18087: Compression does not always make a
3259 section smaller. So only rename the section when
3260 compression has actually taken place. If input section
3261 name is .zdebug_*, we should never compress it again. */
3262 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3263 if (new_name
== NULL
)
3268 BFD_ASSERT (name
[1] != 'z');
3273 if (delay_st_name_p
)
3274 this_hdr
->sh_name
= (unsigned int) -1;
3278 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3280 if (this_hdr
->sh_name
== (unsigned int) -1)
3287 /* Don't clear sh_flags. Assembler may set additional bits. */
3289 if ((asect
->flags
& SEC_ALLOC
) != 0
3290 || asect
->user_set_vma
)
3291 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3293 this_hdr
->sh_addr
= 0;
3295 this_hdr
->sh_offset
= 0;
3296 this_hdr
->sh_size
= asect
->size
;
3297 this_hdr
->sh_link
= 0;
3298 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3299 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3302 /* xgettext:c-format */
3303 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3304 abfd
, asect
->alignment_power
, asect
);
3308 /* Set sh_addralign to the highest power of two given by alignment
3309 consistent with the section VMA. Linker scripts can force VMA. */
3310 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3311 this_hdr
->sh_addralign
= mask
& -mask
;
3312 /* The sh_entsize and sh_info fields may have been set already by
3313 copy_private_section_data. */
3315 this_hdr
->bfd_section
= asect
;
3316 this_hdr
->contents
= NULL
;
3318 /* If the section type is unspecified, we set it based on
3320 if ((asect
->flags
& SEC_GROUP
) != 0)
3321 sh_type
= SHT_GROUP
;
3323 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3325 if (this_hdr
->sh_type
== SHT_NULL
)
3326 this_hdr
->sh_type
= sh_type
;
3327 else if (this_hdr
->sh_type
== SHT_NOBITS
3328 && sh_type
== SHT_PROGBITS
3329 && (asect
->flags
& SEC_ALLOC
) != 0)
3331 /* Warn if we are changing a NOBITS section to PROGBITS, but
3332 allow the link to proceed. This can happen when users link
3333 non-bss input sections to bss output sections, or emit data
3334 to a bss output section via a linker script. */
3336 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3337 this_hdr
->sh_type
= sh_type
;
3340 switch (this_hdr
->sh_type
)
3351 case SHT_INIT_ARRAY
:
3352 case SHT_FINI_ARRAY
:
3353 case SHT_PREINIT_ARRAY
:
3354 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3358 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3362 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3366 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3370 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3371 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3375 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3376 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3379 case SHT_GNU_versym
:
3380 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3383 case SHT_GNU_verdef
:
3384 this_hdr
->sh_entsize
= 0;
3385 /* objcopy or strip will copy over sh_info, but may not set
3386 cverdefs. The linker will set cverdefs, but sh_info will be
3388 if (this_hdr
->sh_info
== 0)
3389 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3391 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3392 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3395 case SHT_GNU_verneed
:
3396 this_hdr
->sh_entsize
= 0;
3397 /* objcopy or strip will copy over sh_info, but may not set
3398 cverrefs. The linker will set cverrefs, but sh_info will be
3400 if (this_hdr
->sh_info
== 0)
3401 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3403 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3404 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3408 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3412 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3416 if ((asect
->flags
& SEC_ALLOC
) != 0)
3417 this_hdr
->sh_flags
|= SHF_ALLOC
;
3418 if ((asect
->flags
& SEC_READONLY
) == 0)
3419 this_hdr
->sh_flags
|= SHF_WRITE
;
3420 if ((asect
->flags
& SEC_CODE
) != 0)
3421 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3422 if ((asect
->flags
& SEC_MERGE
) != 0)
3424 this_hdr
->sh_flags
|= SHF_MERGE
;
3425 this_hdr
->sh_entsize
= asect
->entsize
;
3427 if ((asect
->flags
& SEC_STRINGS
) != 0)
3428 this_hdr
->sh_flags
|= SHF_STRINGS
;
3429 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3430 this_hdr
->sh_flags
|= SHF_GROUP
;
3431 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3433 this_hdr
->sh_flags
|= SHF_TLS
;
3434 if (asect
->size
== 0
3435 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3437 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3439 this_hdr
->sh_size
= 0;
3442 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3443 if (this_hdr
->sh_size
!= 0)
3444 this_hdr
->sh_type
= SHT_NOBITS
;
3448 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3449 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3451 /* If the section has relocs, set up a section header for the
3452 SHT_REL[A] section. If two relocation sections are required for
3453 this section, it is up to the processor-specific back-end to
3454 create the other. */
3455 if ((asect
->flags
& SEC_RELOC
) != 0)
3457 /* When doing a relocatable link, create both REL and RELA sections if
3460 /* Do the normal setup if we wouldn't create any sections here. */
3461 && esd
->rel
.count
+ esd
->rela
.count
> 0
3462 && (bfd_link_relocatable (arg
->link_info
)
3463 || arg
->link_info
->emitrelocations
))
3465 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3466 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3467 FALSE
, delay_st_name_p
))
3472 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3473 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3474 TRUE
, delay_st_name_p
))
3480 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3482 ? &esd
->rela
: &esd
->rel
),
3492 /* Check for processor-specific section types. */
3493 sh_type
= this_hdr
->sh_type
;
3494 if (bed
->elf_backend_fake_sections
3495 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3501 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3503 /* Don't change the header type from NOBITS if we are being
3504 called for objcopy --only-keep-debug. */
3505 this_hdr
->sh_type
= sh_type
;
3509 /* Fill in the contents of a SHT_GROUP section. Called from
3510 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3511 when ELF targets use the generic linker, ld. Called for ld -r
3512 from bfd_elf_final_link. */
3515 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3517 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3518 asection
*elt
, *first
;
3522 /* Ignore linker created group section. See elfNN_ia64_object_p in
3524 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3529 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3531 unsigned long symindx
= 0;
3533 /* elf_group_id will have been set up by objcopy and the
3535 if (elf_group_id (sec
) != NULL
)
3536 symindx
= elf_group_id (sec
)->udata
.i
;
3540 /* If called from the assembler, swap_out_syms will have set up
3542 PR 25699: A corrupt input file could contain bogus group info. */
3543 if (elf_section_syms (abfd
) == NULL
)
3548 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3550 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3552 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3554 /* The ELF backend linker sets sh_info to -2 when the group
3555 signature symbol is global, and thus the index can't be
3556 set until all local symbols are output. */
3558 struct bfd_elf_section_data
*sec_data
;
3559 unsigned long symndx
;
3560 unsigned long extsymoff
;
3561 struct elf_link_hash_entry
*h
;
3563 /* The point of this little dance to the first SHF_GROUP section
3564 then back to the SHT_GROUP section is that this gets us to
3565 the SHT_GROUP in the input object. */
3566 igroup
= elf_sec_group (elf_next_in_group (sec
));
3567 sec_data
= elf_section_data (igroup
);
3568 symndx
= sec_data
->this_hdr
.sh_info
;
3570 if (!elf_bad_symtab (igroup
->owner
))
3572 Elf_Internal_Shdr
*symtab_hdr
;
3574 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3575 extsymoff
= symtab_hdr
->sh_info
;
3577 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3578 while (h
->root
.type
== bfd_link_hash_indirect
3579 || h
->root
.type
== bfd_link_hash_warning
)
3580 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3582 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3585 /* The contents won't be allocated for "ld -r" or objcopy. */
3587 if (sec
->contents
== NULL
)
3590 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3592 /* Arrange for the section to be written out. */
3593 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3594 if (sec
->contents
== NULL
)
3601 loc
= sec
->contents
+ sec
->size
;
3603 /* Get the pointer to the first section in the group that gas
3604 squirreled away here. objcopy arranges for this to be set to the
3605 start of the input section group. */
3606 first
= elt
= elf_next_in_group (sec
);
3608 /* First element is a flag word. Rest of section is elf section
3609 indices for all the sections of the group. Write them backwards
3610 just to keep the group in the same order as given in .section
3611 directives, not that it matters. */
3618 s
= s
->output_section
;
3620 && !bfd_is_abs_section (s
))
3622 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3623 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3625 if (elf_sec
->rel
.hdr
!= NULL
3627 || (input_elf_sec
->rel
.hdr
!= NULL
3628 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3630 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3632 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3634 if (elf_sec
->rela
.hdr
!= NULL
3636 || (input_elf_sec
->rela
.hdr
!= NULL
3637 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3639 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3641 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3644 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3646 elt
= elf_next_in_group (elt
);
3652 BFD_ASSERT (loc
== sec
->contents
);
3654 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3657 /* Given NAME, the name of a relocation section stripped of its
3658 .rel/.rela prefix, return the section in ABFD to which the
3659 relocations apply. */
3662 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3664 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3665 section likely apply to .got.plt or .got section. */
3666 if (get_elf_backend_data (abfd
)->want_got_plt
3667 && strcmp (name
, ".plt") == 0)
3672 sec
= bfd_get_section_by_name (abfd
, name
);
3678 return bfd_get_section_by_name (abfd
, name
);
3681 /* Return the section to which RELOC_SEC applies. */
3684 elf_get_reloc_section (asection
*reloc_sec
)
3689 const struct elf_backend_data
*bed
;
3691 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3692 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3695 /* We look up the section the relocs apply to by name. */
3696 name
= reloc_sec
->name
;
3697 if (strncmp (name
, ".rel", 4) != 0)
3700 if (type
== SHT_RELA
&& *name
++ != 'a')
3703 abfd
= reloc_sec
->owner
;
3704 bed
= get_elf_backend_data (abfd
);
3705 return bed
->get_reloc_section (abfd
, name
);
3708 /* Assign all ELF section numbers. The dummy first section is handled here
3709 too. The link/info pointers for the standard section types are filled
3710 in here too, while we're at it. */
3713 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3715 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3717 unsigned int section_number
;
3718 Elf_Internal_Shdr
**i_shdrp
;
3719 struct bfd_elf_section_data
*d
;
3720 bfd_boolean need_symtab
;
3725 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3727 /* SHT_GROUP sections are in relocatable files only. */
3728 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3730 size_t reloc_count
= 0;
3732 /* Put SHT_GROUP sections first. */
3733 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3735 d
= elf_section_data (sec
);
3737 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3739 if (sec
->flags
& SEC_LINKER_CREATED
)
3741 /* Remove the linker created SHT_GROUP sections. */
3742 bfd_section_list_remove (abfd
, sec
);
3743 abfd
->section_count
--;
3746 d
->this_idx
= section_number
++;
3749 /* Count relocations. */
3750 reloc_count
+= sec
->reloc_count
;
3753 /* Clear HAS_RELOC if there are no relocations. */
3754 if (reloc_count
== 0)
3755 abfd
->flags
&= ~HAS_RELOC
;
3758 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3760 d
= elf_section_data (sec
);
3762 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3763 d
->this_idx
= section_number
++;
3764 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3765 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3768 d
->rel
.idx
= section_number
++;
3769 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3770 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3777 d
->rela
.idx
= section_number
++;
3778 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3779 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3785 need_symtab
= (bfd_get_symcount (abfd
) > 0
3786 || (link_info
== NULL
3787 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3791 elf_onesymtab (abfd
) = section_number
++;
3792 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3793 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3795 elf_section_list
*entry
;
3797 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3799 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3800 entry
->ndx
= section_number
++;
3801 elf_symtab_shndx_list (abfd
) = entry
;
3803 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3804 ".symtab_shndx", FALSE
);
3805 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3808 elf_strtab_sec (abfd
) = section_number
++;
3809 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3812 elf_shstrtab_sec (abfd
) = section_number
++;
3813 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3814 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3816 if (section_number
>= SHN_LORESERVE
)
3818 /* xgettext:c-format */
3819 _bfd_error_handler (_("%pB: too many sections: %u"),
3820 abfd
, section_number
);
3824 elf_numsections (abfd
) = section_number
;
3825 elf_elfheader (abfd
)->e_shnum
= section_number
;
3827 /* Set up the list of section header pointers, in agreement with the
3829 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3830 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3831 if (i_shdrp
== NULL
)
3834 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3835 sizeof (Elf_Internal_Shdr
));
3836 if (i_shdrp
[0] == NULL
)
3838 bfd_release (abfd
, i_shdrp
);
3842 elf_elfsections (abfd
) = i_shdrp
;
3844 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3847 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3848 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3850 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3851 BFD_ASSERT (entry
!= NULL
);
3852 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3853 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3855 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3856 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3859 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3863 d
= elf_section_data (sec
);
3865 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3866 if (d
->rel
.idx
!= 0)
3867 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3868 if (d
->rela
.idx
!= 0)
3869 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3871 /* Fill in the sh_link and sh_info fields while we're at it. */
3873 /* sh_link of a reloc section is the section index of the symbol
3874 table. sh_info is the section index of the section to which
3875 the relocation entries apply. */
3876 if (d
->rel
.idx
!= 0)
3878 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3879 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3880 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3882 if (d
->rela
.idx
!= 0)
3884 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3885 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3886 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3889 /* We need to set up sh_link for SHF_LINK_ORDER. */
3890 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3892 s
= elf_linked_to_section (sec
);
3895 /* elf_linked_to_section points to the input section. */
3896 if (link_info
!= NULL
)
3898 /* Check discarded linkonce section. */
3899 if (discarded_section (s
))
3903 /* xgettext:c-format */
3904 (_("%pB: sh_link of section `%pA' points to"
3905 " discarded section `%pA' of `%pB'"),
3906 abfd
, d
->this_hdr
.bfd_section
,
3908 /* Point to the kept section if it has the same
3909 size as the discarded one. */
3910 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3913 bfd_set_error (bfd_error_bad_value
);
3919 s
= s
->output_section
;
3920 BFD_ASSERT (s
!= NULL
);
3924 /* Handle objcopy. */
3925 if (s
->output_section
== NULL
)
3928 /* xgettext:c-format */
3929 (_("%pB: sh_link of section `%pA' points to"
3930 " removed section `%pA' of `%pB'"),
3931 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3932 bfd_set_error (bfd_error_bad_value
);
3935 s
= s
->output_section
;
3937 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3942 The Intel C compiler generates SHT_IA_64_UNWIND with
3943 SHF_LINK_ORDER. But it doesn't set the sh_link or
3944 sh_info fields. Hence we could get the situation
3946 const struct elf_backend_data
*bed
3947 = get_elf_backend_data (abfd
);
3948 bed
->link_order_error_handler
3949 /* xgettext:c-format */
3950 (_("%pB: warning: sh_link not set for section `%pA'"),
3955 switch (d
->this_hdr
.sh_type
)
3959 /* A reloc section which we are treating as a normal BFD
3960 section. sh_link is the section index of the symbol
3961 table. sh_info is the section index of the section to
3962 which the relocation entries apply. We assume that an
3963 allocated reloc section uses the dynamic symbol table.
3964 FIXME: How can we be sure? */
3965 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3967 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3969 s
= elf_get_reloc_section (sec
);
3972 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3973 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3978 /* We assume that a section named .stab*str is a stabs
3979 string section. We look for a section with the same name
3980 but without the trailing ``str'', and set its sh_link
3981 field to point to this section. */
3982 if (CONST_STRNEQ (sec
->name
, ".stab")
3983 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3988 len
= strlen (sec
->name
);
3989 alc
= (char *) bfd_malloc (len
- 2);
3992 memcpy (alc
, sec
->name
, len
- 3);
3993 alc
[len
- 3] = '\0';
3994 s
= bfd_get_section_by_name (abfd
, alc
);
3998 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
4000 /* This is a .stab section. */
4001 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
4002 elf_section_data (s
)->this_hdr
.sh_entsize
4003 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
4010 case SHT_GNU_verneed
:
4011 case SHT_GNU_verdef
:
4012 /* sh_link is the section header index of the string table
4013 used for the dynamic entries, or the symbol table, or the
4015 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4017 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4020 case SHT_GNU_LIBLIST
:
4021 /* sh_link is the section header index of the prelink library
4022 list used for the dynamic entries, or the symbol table, or
4023 the version strings. */
4024 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4025 ? ".dynstr" : ".gnu.libstr");
4027 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4032 case SHT_GNU_versym
:
4033 /* sh_link is the section header index of the symbol table
4034 this hash table or version table is for. */
4035 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4037 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4041 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4045 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4046 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4047 debug section name from .debug_* to .zdebug_* if needed. */
4053 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4055 /* If the backend has a special mapping, use it. */
4056 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4057 if (bed
->elf_backend_sym_is_global
)
4058 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4060 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4061 || bfd_is_und_section (bfd_asymbol_section (sym
))
4062 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4065 /* Filter global symbols of ABFD to include in the import library. All
4066 SYMCOUNT symbols of ABFD can be examined from their pointers in
4067 SYMS. Pointers of symbols to keep should be stored contiguously at
4068 the beginning of that array.
4070 Returns the number of symbols to keep. */
4073 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4074 asymbol
**syms
, long symcount
)
4076 long src_count
, dst_count
= 0;
4078 for (src_count
= 0; src_count
< symcount
; src_count
++)
4080 asymbol
*sym
= syms
[src_count
];
4081 char *name
= (char *) bfd_asymbol_name (sym
);
4082 struct bfd_link_hash_entry
*h
;
4084 if (!sym_is_global (abfd
, sym
))
4087 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4090 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4092 if (h
->linker_def
|| h
->ldscript_def
)
4095 syms
[dst_count
++] = sym
;
4098 syms
[dst_count
] = NULL
;
4103 /* Don't output section symbols for sections that are not going to be
4104 output, that are duplicates or there is no BFD section. */
4107 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4109 elf_symbol_type
*type_ptr
;
4114 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4117 if (sym
->section
== NULL
)
4120 type_ptr
= elf_symbol_from (abfd
, sym
);
4121 return ((type_ptr
!= NULL
4122 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4123 && bfd_is_abs_section (sym
->section
))
4124 || !(sym
->section
->owner
== abfd
4125 || (sym
->section
->output_section
!= NULL
4126 && sym
->section
->output_section
->owner
== abfd
4127 && sym
->section
->output_offset
== 0)
4128 || bfd_is_abs_section (sym
->section
)));
4131 /* Map symbol from it's internal number to the external number, moving
4132 all local symbols to be at the head of the list. */
4135 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4137 unsigned int symcount
= bfd_get_symcount (abfd
);
4138 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4139 asymbol
**sect_syms
;
4140 unsigned int num_locals
= 0;
4141 unsigned int num_globals
= 0;
4142 unsigned int num_locals2
= 0;
4143 unsigned int num_globals2
= 0;
4144 unsigned int max_index
= 0;
4151 fprintf (stderr
, "elf_map_symbols\n");
4155 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4157 if (max_index
< asect
->index
)
4158 max_index
= asect
->index
;
4162 amt
= max_index
* sizeof (asymbol
*);
4163 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4164 if (sect_syms
== NULL
)
4166 elf_section_syms (abfd
) = sect_syms
;
4167 elf_num_section_syms (abfd
) = max_index
;
4169 /* Init sect_syms entries for any section symbols we have already
4170 decided to output. */
4171 for (idx
= 0; idx
< symcount
; idx
++)
4173 asymbol
*sym
= syms
[idx
];
4175 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4177 && !ignore_section_sym (abfd
, sym
)
4178 && !bfd_is_abs_section (sym
->section
))
4180 asection
*sec
= sym
->section
;
4182 if (sec
->owner
!= abfd
)
4183 sec
= sec
->output_section
;
4185 sect_syms
[sec
->index
] = syms
[idx
];
4189 /* Classify all of the symbols. */
4190 for (idx
= 0; idx
< symcount
; idx
++)
4192 if (sym_is_global (abfd
, syms
[idx
]))
4194 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4198 /* We will be adding a section symbol for each normal BFD section. Most
4199 sections will already have a section symbol in outsymbols, but
4200 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4201 at least in that case. */
4202 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4204 if (sect_syms
[asect
->index
] == NULL
)
4206 if (!sym_is_global (abfd
, asect
->symbol
))
4213 /* Now sort the symbols so the local symbols are first. */
4214 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4215 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4216 if (new_syms
== NULL
)
4219 for (idx
= 0; idx
< symcount
; idx
++)
4221 asymbol
*sym
= syms
[idx
];
4224 if (sym_is_global (abfd
, sym
))
4225 i
= num_locals
+ num_globals2
++;
4226 else if (!ignore_section_sym (abfd
, sym
))
4231 sym
->udata
.i
= i
+ 1;
4233 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4235 if (sect_syms
[asect
->index
] == NULL
)
4237 asymbol
*sym
= asect
->symbol
;
4240 sect_syms
[asect
->index
] = sym
;
4241 if (!sym_is_global (abfd
, sym
))
4244 i
= num_locals
+ num_globals2
++;
4246 sym
->udata
.i
= i
+ 1;
4250 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4252 *pnum_locals
= num_locals
;
4256 /* Align to the maximum file alignment that could be required for any
4257 ELF data structure. */
4259 static inline file_ptr
4260 align_file_position (file_ptr off
, int align
)
4262 return (off
+ align
- 1) & ~(align
- 1);
4265 /* Assign a file position to a section, optionally aligning to the
4266 required section alignment. */
4269 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4273 if (align
&& i_shdrp
->sh_addralign
> 1)
4274 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4275 i_shdrp
->sh_offset
= offset
;
4276 if (i_shdrp
->bfd_section
!= NULL
)
4277 i_shdrp
->bfd_section
->filepos
= offset
;
4278 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4279 offset
+= i_shdrp
->sh_size
;
4283 /* Compute the file positions we are going to put the sections at, and
4284 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4285 is not NULL, this is being called by the ELF backend linker. */
4288 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4289 struct bfd_link_info
*link_info
)
4291 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4292 struct fake_section_arg fsargs
;
4294 struct elf_strtab_hash
*strtab
= NULL
;
4295 Elf_Internal_Shdr
*shstrtab_hdr
;
4296 bfd_boolean need_symtab
;
4298 if (abfd
->output_has_begun
)
4301 /* Do any elf backend specific processing first. */
4302 if (bed
->elf_backend_begin_write_processing
)
4303 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4305 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4308 fsargs
.failed
= FALSE
;
4309 fsargs
.link_info
= link_info
;
4310 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4314 if (!assign_section_numbers (abfd
, link_info
))
4317 /* The backend linker builds symbol table information itself. */
4318 need_symtab
= (link_info
== NULL
4319 && (bfd_get_symcount (abfd
) > 0
4320 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4324 /* Non-zero if doing a relocatable link. */
4325 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4327 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4332 if (link_info
== NULL
)
4334 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4339 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4340 /* sh_name was set in init_file_header. */
4341 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4342 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4343 shstrtab_hdr
->sh_addr
= 0;
4344 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4345 shstrtab_hdr
->sh_entsize
= 0;
4346 shstrtab_hdr
->sh_link
= 0;
4347 shstrtab_hdr
->sh_info
= 0;
4348 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4349 shstrtab_hdr
->sh_addralign
= 1;
4351 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4357 Elf_Internal_Shdr
*hdr
;
4359 off
= elf_next_file_pos (abfd
);
4361 hdr
= & elf_symtab_hdr (abfd
);
4362 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4364 if (elf_symtab_shndx_list (abfd
) != NULL
)
4366 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4367 if (hdr
->sh_size
!= 0)
4368 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4369 /* FIXME: What about other symtab_shndx sections in the list ? */
4372 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4373 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4375 elf_next_file_pos (abfd
) = off
;
4377 /* Now that we know where the .strtab section goes, write it
4379 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4380 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4382 _bfd_elf_strtab_free (strtab
);
4385 abfd
->output_has_begun
= TRUE
;
4390 /* Make an initial estimate of the size of the program header. If we
4391 get the number wrong here, we'll redo section placement. */
4393 static bfd_size_type
4394 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4398 const struct elf_backend_data
*bed
;
4400 /* Assume we will need exactly two PT_LOAD segments: one for text
4401 and one for data. */
4404 s
= bfd_get_section_by_name (abfd
, ".interp");
4405 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4407 /* If we have a loadable interpreter section, we need a
4408 PT_INTERP segment. In this case, assume we also need a
4409 PT_PHDR segment, although that may not be true for all
4414 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4416 /* We need a PT_DYNAMIC segment. */
4420 if (info
!= NULL
&& info
->relro
)
4422 /* We need a PT_GNU_RELRO segment. */
4426 if (elf_eh_frame_hdr (abfd
))
4428 /* We need a PT_GNU_EH_FRAME segment. */
4432 if (elf_stack_flags (abfd
))
4434 /* We need a PT_GNU_STACK segment. */
4438 s
= bfd_get_section_by_name (abfd
,
4439 NOTE_GNU_PROPERTY_SECTION_NAME
);
4440 if (s
!= NULL
&& s
->size
!= 0)
4442 /* We need a PT_GNU_PROPERTY segment. */
4446 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4448 if ((s
->flags
& SEC_LOAD
) != 0
4449 && elf_section_type (s
) == SHT_NOTE
)
4451 unsigned int alignment_power
;
4452 /* We need a PT_NOTE segment. */
4454 /* Try to create just one PT_NOTE segment for all adjacent
4455 loadable SHT_NOTE sections. gABI requires that within a
4456 PT_NOTE segment (and also inside of each SHT_NOTE section)
4457 each note should have the same alignment. So we check
4458 whether the sections are correctly aligned. */
4459 alignment_power
= s
->alignment_power
;
4460 while (s
->next
!= NULL
4461 && s
->next
->alignment_power
== alignment_power
4462 && (s
->next
->flags
& SEC_LOAD
) != 0
4463 && elf_section_type (s
->next
) == SHT_NOTE
)
4468 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4470 if (s
->flags
& SEC_THREAD_LOCAL
)
4472 /* We need a PT_TLS segment. */
4478 bed
= get_elf_backend_data (abfd
);
4480 if ((abfd
->flags
& D_PAGED
) != 0
4481 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4483 /* Add a PT_GNU_MBIND segment for each mbind section. */
4484 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4485 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4486 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4488 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4491 /* xgettext:c-format */
4492 (_("%pB: GNU_MBIND section `%pA' has invalid "
4493 "sh_info field: %d"),
4494 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4497 /* Align mbind section to page size. */
4498 if (s
->alignment_power
< page_align_power
)
4499 s
->alignment_power
= page_align_power
;
4504 /* Let the backend count up any program headers it might need. */
4505 if (bed
->elf_backend_additional_program_headers
)
4509 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4515 return segs
* bed
->s
->sizeof_phdr
;
4518 /* Find the segment that contains the output_section of section. */
4521 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4523 struct elf_segment_map
*m
;
4524 Elf_Internal_Phdr
*p
;
4526 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4532 for (i
= m
->count
- 1; i
>= 0; i
--)
4533 if (m
->sections
[i
] == section
)
4540 /* Create a mapping from a set of sections to a program segment. */
4542 static struct elf_segment_map
*
4543 make_mapping (bfd
*abfd
,
4544 asection
**sections
,
4549 struct elf_segment_map
*m
;
4554 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4555 amt
+= (to
- from
) * sizeof (asection
*);
4556 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4560 m
->p_type
= PT_LOAD
;
4561 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4562 m
->sections
[i
- from
] = *hdrpp
;
4563 m
->count
= to
- from
;
4565 if (from
== 0 && phdr
)
4567 /* Include the headers in the first PT_LOAD segment. */
4568 m
->includes_filehdr
= 1;
4569 m
->includes_phdrs
= 1;
4575 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4578 struct elf_segment_map
*
4579 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4581 struct elf_segment_map
*m
;
4583 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4584 sizeof (struct elf_segment_map
));
4588 m
->p_type
= PT_DYNAMIC
;
4590 m
->sections
[0] = dynsec
;
4595 /* Possibly add or remove segments from the segment map. */
4598 elf_modify_segment_map (bfd
*abfd
,
4599 struct bfd_link_info
*info
,
4600 bfd_boolean remove_empty_load
)
4602 struct elf_segment_map
**m
;
4603 const struct elf_backend_data
*bed
;
4605 /* The placement algorithm assumes that non allocated sections are
4606 not in PT_LOAD segments. We ensure this here by removing such
4607 sections from the segment map. We also remove excluded
4608 sections. Finally, any PT_LOAD segment without sections is
4610 m
= &elf_seg_map (abfd
);
4613 unsigned int i
, new_count
;
4615 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4617 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4618 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4619 || (*m
)->p_type
!= PT_LOAD
))
4621 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4625 (*m
)->count
= new_count
;
4627 if (remove_empty_load
4628 && (*m
)->p_type
== PT_LOAD
4630 && !(*m
)->includes_phdrs
)
4636 bed
= get_elf_backend_data (abfd
);
4637 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4639 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4646 #define IS_TBSS(s) \
4647 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4649 /* Set up a mapping from BFD sections to program segments. */
4652 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4655 struct elf_segment_map
*m
;
4656 asection
**sections
= NULL
;
4657 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4658 bfd_boolean no_user_phdrs
;
4660 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4663 info
->user_phdrs
= !no_user_phdrs
;
4665 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4669 struct elf_segment_map
*mfirst
;
4670 struct elf_segment_map
**pm
;
4673 unsigned int hdr_index
;
4674 bfd_vma maxpagesize
;
4676 bfd_boolean phdr_in_segment
;
4677 bfd_boolean writable
;
4678 bfd_boolean executable
;
4679 unsigned int tls_count
= 0;
4680 asection
*first_tls
= NULL
;
4681 asection
*first_mbind
= NULL
;
4682 asection
*dynsec
, *eh_frame_hdr
;
4684 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4685 bfd_size_type phdr_size
; /* Octets/bytes. */
4686 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4688 /* Select the allocated sections, and sort them. */
4690 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4691 sections
= (asection
**) bfd_malloc (amt
);
4692 if (sections
== NULL
)
4695 /* Calculate top address, avoiding undefined behaviour of shift
4696 left operator when shift count is equal to size of type
4698 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4699 addr_mask
= (addr_mask
<< 1) + 1;
4702 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4704 if ((s
->flags
& SEC_ALLOC
) != 0)
4706 /* target_index is unused until bfd_elf_final_link
4707 starts output of section symbols. Use it to make
4709 s
->target_index
= i
;
4712 /* A wrapping section potentially clashes with header. */
4713 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4714 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4717 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4720 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4722 phdr_size
= elf_program_header_size (abfd
);
4723 if (phdr_size
== (bfd_size_type
) -1)
4724 phdr_size
= get_program_header_size (abfd
, info
);
4725 phdr_size
+= bed
->s
->sizeof_ehdr
;
4726 /* phdr_size is compared to LMA values which are in bytes. */
4728 maxpagesize
= bed
->maxpagesize
;
4729 if (maxpagesize
== 0)
4731 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4733 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4734 >= (phdr_size
& (maxpagesize
- 1))))
4735 /* For compatibility with old scripts that may not be using
4736 SIZEOF_HEADERS, add headers when it looks like space has
4737 been left for them. */
4738 phdr_in_segment
= TRUE
;
4740 /* Build the mapping. */
4744 /* If we have a .interp section, then create a PT_PHDR segment for
4745 the program headers and a PT_INTERP segment for the .interp
4747 s
= bfd_get_section_by_name (abfd
, ".interp");
4748 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4750 amt
= sizeof (struct elf_segment_map
);
4751 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4755 m
->p_type
= PT_PHDR
;
4757 m
->p_flags_valid
= 1;
4758 m
->includes_phdrs
= 1;
4759 phdr_in_segment
= TRUE
;
4763 amt
= sizeof (struct elf_segment_map
);
4764 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4768 m
->p_type
= PT_INTERP
;
4776 /* Look through the sections. We put sections in the same program
4777 segment when the start of the second section can be placed within
4778 a few bytes of the end of the first section. */
4784 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4786 && (dynsec
->flags
& SEC_LOAD
) == 0)
4789 if ((abfd
->flags
& D_PAGED
) == 0)
4790 phdr_in_segment
= FALSE
;
4792 /* Deal with -Ttext or something similar such that the first section
4793 is not adjacent to the program headers. This is an
4794 approximation, since at this point we don't know exactly how many
4795 program headers we will need. */
4796 if (phdr_in_segment
&& count
> 0)
4798 bfd_vma phdr_lma
; /* Bytes. */
4799 bfd_boolean separate_phdr
= FALSE
;
4801 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4803 && info
->separate_code
4804 && (sections
[0]->flags
& SEC_CODE
) != 0)
4806 /* If data sections should be separate from code and
4807 thus not executable, and the first section is
4808 executable then put the file and program headers in
4809 their own PT_LOAD. */
4810 separate_phdr
= TRUE
;
4811 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4812 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4814 /* The file and program headers are currently on the
4815 same page as the first section. Put them on the
4816 previous page if we can. */
4817 if (phdr_lma
>= maxpagesize
)
4818 phdr_lma
-= maxpagesize
;
4820 separate_phdr
= FALSE
;
4823 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4824 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4825 /* If file and program headers would be placed at the end
4826 of memory then it's probably better to omit them. */
4827 phdr_in_segment
= FALSE
;
4828 else if (phdr_lma
< wrap_to
)
4829 /* If a section wraps around to where we'll be placing
4830 file and program headers, then the headers will be
4832 phdr_in_segment
= FALSE
;
4833 else if (separate_phdr
)
4835 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4838 m
->p_paddr
= phdr_lma
* opb
;
4840 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4841 m
->p_paddr_valid
= 1;
4844 phdr_in_segment
= FALSE
;
4848 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4851 bfd_boolean new_segment
;
4855 /* See if this section and the last one will fit in the same
4858 if (last_hdr
== NULL
)
4860 /* If we don't have a segment yet, then we don't need a new
4861 one (we build the last one after this loop). */
4862 new_segment
= FALSE
;
4864 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4866 /* If this section has a different relation between the
4867 virtual address and the load address, then we need a new
4871 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4872 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4874 /* If this section has a load address that makes it overlap
4875 the previous section, then we need a new segment. */
4878 else if ((abfd
->flags
& D_PAGED
) != 0
4879 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4880 == (hdr
->lma
& -maxpagesize
)))
4882 /* If we are demand paged then we can't map two disk
4883 pages onto the same memory page. */
4884 new_segment
= FALSE
;
4886 /* In the next test we have to be careful when last_hdr->lma is close
4887 to the end of the address space. If the aligned address wraps
4888 around to the start of the address space, then there are no more
4889 pages left in memory and it is OK to assume that the current
4890 section can be included in the current segment. */
4891 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4892 + maxpagesize
> last_hdr
->lma
)
4893 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4894 + maxpagesize
<= hdr
->lma
))
4896 /* If putting this section in this segment would force us to
4897 skip a page in the segment, then we need a new segment. */
4900 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4901 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4903 /* We don't want to put a loaded section after a
4904 nonloaded (ie. bss style) section in the same segment
4905 as that will force the non-loaded section to be loaded.
4906 Consider .tbss sections as loaded for this purpose. */
4909 else if ((abfd
->flags
& D_PAGED
) == 0)
4911 /* If the file is not demand paged, which means that we
4912 don't require the sections to be correctly aligned in the
4913 file, then there is no other reason for a new segment. */
4914 new_segment
= FALSE
;
4916 else if (info
!= NULL
4917 && info
->separate_code
4918 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4923 && (hdr
->flags
& SEC_READONLY
) == 0)
4925 /* We don't want to put a writable section in a read only
4931 /* Otherwise, we can use the same segment. */
4932 new_segment
= FALSE
;
4935 /* Allow interested parties a chance to override our decision. */
4936 if (last_hdr
!= NULL
4938 && info
->callbacks
->override_segment_assignment
!= NULL
)
4940 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4946 if ((hdr
->flags
& SEC_READONLY
) == 0)
4948 if ((hdr
->flags
& SEC_CODE
) != 0)
4951 /* .tbss sections effectively have zero size. */
4952 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4956 /* We need a new program segment. We must create a new program
4957 header holding all the sections from hdr_index until hdr. */
4959 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4966 if ((hdr
->flags
& SEC_READONLY
) == 0)
4971 if ((hdr
->flags
& SEC_CODE
) == 0)
4977 /* .tbss sections effectively have zero size. */
4978 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4980 phdr_in_segment
= FALSE
;
4983 /* Create a final PT_LOAD program segment, but not if it's just
4985 if (last_hdr
!= NULL
4986 && (i
- hdr_index
!= 1
4987 || !IS_TBSS (last_hdr
)))
4989 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4997 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5000 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5007 /* For each batch of consecutive loadable SHT_NOTE sections,
5008 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5009 because if we link together nonloadable .note sections and
5010 loadable .note sections, we will generate two .note sections
5011 in the output file. */
5012 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5014 if ((s
->flags
& SEC_LOAD
) != 0
5015 && elf_section_type (s
) == SHT_NOTE
)
5018 unsigned int alignment_power
= s
->alignment_power
;
5021 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5023 if (s2
->next
->alignment_power
== alignment_power
5024 && (s2
->next
->flags
& SEC_LOAD
) != 0
5025 && elf_section_type (s2
->next
) == SHT_NOTE
5026 && align_power (s2
->lma
+ s2
->size
/ opb
,
5033 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5034 amt
+= count
* sizeof (asection
*);
5035 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5039 m
->p_type
= PT_NOTE
;
5043 m
->sections
[m
->count
- count
--] = s
;
5044 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5047 m
->sections
[m
->count
- 1] = s
;
5048 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5052 if (s
->flags
& SEC_THREAD_LOCAL
)
5058 if (first_mbind
== NULL
5059 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5063 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5066 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5067 amt
+= tls_count
* sizeof (asection
*);
5068 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5073 m
->count
= tls_count
;
5074 /* Mandated PF_R. */
5076 m
->p_flags_valid
= 1;
5078 for (i
= 0; i
< tls_count
; ++i
)
5080 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5083 (_("%pB: TLS sections are not adjacent:"), abfd
);
5086 while (i
< tls_count
)
5088 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5090 _bfd_error_handler (_(" TLS: %pA"), s
);
5094 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5097 bfd_set_error (bfd_error_bad_value
);
5109 && (abfd
->flags
& D_PAGED
) != 0
5110 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5111 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5112 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5113 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5115 /* Mandated PF_R. */
5116 unsigned long p_flags
= PF_R
;
5117 if ((s
->flags
& SEC_READONLY
) == 0)
5119 if ((s
->flags
& SEC_CODE
) != 0)
5122 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5123 m
= bfd_zalloc (abfd
, amt
);
5127 m
->p_type
= (PT_GNU_MBIND_LO
5128 + elf_section_data (s
)->this_hdr
.sh_info
);
5130 m
->p_flags_valid
= 1;
5132 m
->p_flags
= p_flags
;
5138 s
= bfd_get_section_by_name (abfd
,
5139 NOTE_GNU_PROPERTY_SECTION_NAME
);
5140 if (s
!= NULL
&& s
->size
!= 0)
5142 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5143 m
= bfd_zalloc (abfd
, amt
);
5147 m
->p_type
= PT_GNU_PROPERTY
;
5149 m
->p_flags_valid
= 1;
5156 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5158 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5159 if (eh_frame_hdr
!= NULL
5160 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5162 amt
= sizeof (struct elf_segment_map
);
5163 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5167 m
->p_type
= PT_GNU_EH_FRAME
;
5169 m
->sections
[0] = eh_frame_hdr
->output_section
;
5175 if (elf_stack_flags (abfd
))
5177 amt
= sizeof (struct elf_segment_map
);
5178 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5182 m
->p_type
= PT_GNU_STACK
;
5183 m
->p_flags
= elf_stack_flags (abfd
);
5184 m
->p_align
= bed
->stack_align
;
5185 m
->p_flags_valid
= 1;
5186 m
->p_align_valid
= m
->p_align
!= 0;
5187 if (info
->stacksize
> 0)
5189 m
->p_size
= info
->stacksize
;
5190 m
->p_size_valid
= 1;
5197 if (info
!= NULL
&& info
->relro
)
5199 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5201 if (m
->p_type
== PT_LOAD
5203 && m
->sections
[0]->vma
>= info
->relro_start
5204 && m
->sections
[0]->vma
< info
->relro_end
)
5207 while (--i
!= (unsigned) -1)
5209 if (m
->sections
[i
]->size
> 0
5210 && (m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5211 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5215 if (i
!= (unsigned) -1)
5220 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5223 amt
= sizeof (struct elf_segment_map
);
5224 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5228 m
->p_type
= PT_GNU_RELRO
;
5235 elf_seg_map (abfd
) = mfirst
;
5238 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5241 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5243 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5248 if (sections
!= NULL
)
5253 /* Sort sections by address. */
5256 elf_sort_sections (const void *arg1
, const void *arg2
)
5258 const asection
*sec1
= *(const asection
**) arg1
;
5259 const asection
*sec2
= *(const asection
**) arg2
;
5260 bfd_size_type size1
, size2
;
5262 /* Sort by LMA first, since this is the address used to
5263 place the section into a segment. */
5264 if (sec1
->lma
< sec2
->lma
)
5266 else if (sec1
->lma
> sec2
->lma
)
5269 /* Then sort by VMA. Normally the LMA and the VMA will be
5270 the same, and this will do nothing. */
5271 if (sec1
->vma
< sec2
->vma
)
5273 else if (sec1
->vma
> sec2
->vma
)
5276 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5278 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5285 else if (TOEND (sec2
))
5290 /* Sort by size, to put zero sized sections
5291 before others at the same address. */
5293 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5294 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5301 return sec1
->target_index
- sec2
->target_index
;
5304 /* This qsort comparison functions sorts PT_LOAD segments first and
5305 by p_paddr, for assign_file_positions_for_load_sections. */
5308 elf_sort_segments (const void *arg1
, const void *arg2
)
5310 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5311 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5313 if (m1
->p_type
!= m2
->p_type
)
5315 if (m1
->p_type
== PT_NULL
)
5317 if (m2
->p_type
== PT_NULL
)
5319 return m1
->p_type
< m2
->p_type
? -1 : 1;
5321 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5322 return m1
->includes_filehdr
? -1 : 1;
5323 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5324 return m1
->no_sort_lma
? -1 : 1;
5325 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5327 bfd_vma lma1
, lma2
; /* Octets. */
5329 if (m1
->p_paddr_valid
)
5331 else if (m1
->count
!= 0)
5333 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5335 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5338 if (m2
->p_paddr_valid
)
5340 else if (m2
->count
!= 0)
5342 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5344 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5347 return lma1
< lma2
? -1 : 1;
5349 if (m1
->idx
!= m2
->idx
)
5350 return m1
->idx
< m2
->idx
? -1 : 1;
5354 /* Ian Lance Taylor writes:
5356 We shouldn't be using % with a negative signed number. That's just
5357 not good. We have to make sure either that the number is not
5358 negative, or that the number has an unsigned type. When the types
5359 are all the same size they wind up as unsigned. When file_ptr is a
5360 larger signed type, the arithmetic winds up as signed long long,
5363 What we're trying to say here is something like ``increase OFF by
5364 the least amount that will cause it to be equal to the VMA modulo
5366 /* In other words, something like:
5368 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5369 off_offset = off % bed->maxpagesize;
5370 if (vma_offset < off_offset)
5371 adjustment = vma_offset + bed->maxpagesize - off_offset;
5373 adjustment = vma_offset - off_offset;
5375 which can be collapsed into the expression below. */
5378 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5380 /* PR binutils/16199: Handle an alignment of zero. */
5381 if (maxpagesize
== 0)
5383 return ((vma
- off
) % maxpagesize
);
5387 print_segment_map (const struct elf_segment_map
*m
)
5390 const char *pt
= get_segment_type (m
->p_type
);
5395 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5396 sprintf (buf
, "LOPROC+%7.7x",
5397 (unsigned int) (m
->p_type
- PT_LOPROC
));
5398 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5399 sprintf (buf
, "LOOS+%7.7x",
5400 (unsigned int) (m
->p_type
- PT_LOOS
));
5402 snprintf (buf
, sizeof (buf
), "%8.8x",
5403 (unsigned int) m
->p_type
);
5407 fprintf (stderr
, "%s:", pt
);
5408 for (j
= 0; j
< m
->count
; j
++)
5409 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5415 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5420 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5422 buf
= bfd_zmalloc (len
);
5425 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5430 /* Assign file positions to the sections based on the mapping from
5431 sections to segments. This function also sets up some fields in
5435 assign_file_positions_for_load_sections (bfd
*abfd
,
5436 struct bfd_link_info
*link_info
)
5438 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5439 struct elf_segment_map
*m
;
5440 struct elf_segment_map
*phdr_load_seg
;
5441 Elf_Internal_Phdr
*phdrs
;
5442 Elf_Internal_Phdr
*p
;
5443 file_ptr off
; /* Octets. */
5444 bfd_size_type maxpagesize
;
5445 unsigned int alloc
, actual
;
5447 struct elf_segment_map
**sorted_seg_map
;
5448 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5450 if (link_info
== NULL
5451 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5455 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5460 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5461 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5465 /* PR binutils/12467. */
5466 elf_elfheader (abfd
)->e_phoff
= 0;
5467 elf_elfheader (abfd
)->e_phentsize
= 0;
5470 elf_elfheader (abfd
)->e_phnum
= alloc
;
5472 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5475 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5479 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5480 BFD_ASSERT (elf_program_header_size (abfd
)
5481 == actual
* bed
->s
->sizeof_phdr
);
5482 BFD_ASSERT (actual
>= alloc
);
5487 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5491 /* We're writing the size in elf_program_header_size (abfd),
5492 see assign_file_positions_except_relocs, so make sure we have
5493 that amount allocated, with trailing space cleared.
5494 The variable alloc contains the computed need, while
5495 elf_program_header_size (abfd) contains the size used for the
5497 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5498 where the layout is forced to according to a larger size in the
5499 last iterations for the testcase ld-elf/header. */
5500 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5501 + alloc
* sizeof (*sorted_seg_map
)));
5502 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5503 elf_tdata (abfd
)->phdr
= phdrs
;
5507 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5509 sorted_seg_map
[j
] = m
;
5510 /* If elf_segment_map is not from map_sections_to_segments, the
5511 sections may not be correctly ordered. NOTE: sorting should
5512 not be done to the PT_NOTE section of a corefile, which may
5513 contain several pseudo-sections artificially created by bfd.
5514 Sorting these pseudo-sections breaks things badly. */
5516 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5517 && m
->p_type
== PT_NOTE
))
5519 for (i
= 0; i
< m
->count
; i
++)
5520 m
->sections
[i
]->target_index
= i
;
5521 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5526 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5530 if ((abfd
->flags
& D_PAGED
) != 0)
5531 maxpagesize
= bed
->maxpagesize
;
5533 /* Sections must map to file offsets past the ELF file header. */
5534 off
= bed
->s
->sizeof_ehdr
;
5535 /* And if one of the PT_LOAD headers doesn't include the program
5536 headers then we'll be mapping program headers in the usual
5537 position after the ELF file header. */
5538 phdr_load_seg
= NULL
;
5539 for (j
= 0; j
< alloc
; j
++)
5541 m
= sorted_seg_map
[j
];
5542 if (m
->p_type
!= PT_LOAD
)
5544 if (m
->includes_phdrs
)
5550 if (phdr_load_seg
== NULL
)
5551 off
+= actual
* bed
->s
->sizeof_phdr
;
5553 for (j
= 0; j
< alloc
; j
++)
5556 bfd_vma off_adjust
; /* Octets. */
5557 bfd_boolean no_contents
;
5559 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5560 number of sections with contents contributing to both p_filesz
5561 and p_memsz, followed by a number of sections with no contents
5562 that just contribute to p_memsz. In this loop, OFF tracks next
5563 available file offset for PT_LOAD and PT_NOTE segments. */
5564 m
= sorted_seg_map
[j
];
5566 p
->p_type
= m
->p_type
;
5567 p
->p_flags
= m
->p_flags
;
5570 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5572 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5574 if (m
->p_paddr_valid
)
5575 p
->p_paddr
= m
->p_paddr
;
5576 else if (m
->count
== 0)
5579 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5581 if (p
->p_type
== PT_LOAD
5582 && (abfd
->flags
& D_PAGED
) != 0)
5584 /* p_align in demand paged PT_LOAD segments effectively stores
5585 the maximum page size. When copying an executable with
5586 objcopy, we set m->p_align from the input file. Use this
5587 value for maxpagesize rather than bed->maxpagesize, which
5588 may be different. Note that we use maxpagesize for PT_TLS
5589 segment alignment later in this function, so we are relying
5590 on at least one PT_LOAD segment appearing before a PT_TLS
5592 if (m
->p_align_valid
)
5593 maxpagesize
= m
->p_align
;
5595 p
->p_align
= maxpagesize
;
5597 else if (m
->p_align_valid
)
5598 p
->p_align
= m
->p_align
;
5599 else if (m
->count
== 0)
5600 p
->p_align
= 1 << bed
->s
->log_file_align
;
5602 if (m
== phdr_load_seg
)
5604 if (!m
->includes_filehdr
)
5606 off
+= actual
* bed
->s
->sizeof_phdr
;
5609 no_contents
= FALSE
;
5611 if (p
->p_type
== PT_LOAD
5614 bfd_size_type align
; /* Bytes. */
5615 unsigned int align_power
= 0;
5617 if (m
->p_align_valid
)
5621 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5623 unsigned int secalign
;
5625 secalign
= bfd_section_alignment (*secpp
);
5626 if (secalign
> align_power
)
5627 align_power
= secalign
;
5629 align
= (bfd_size_type
) 1 << align_power
;
5630 if (align
< maxpagesize
)
5631 align
= maxpagesize
;
5634 for (i
= 0; i
< m
->count
; i
++)
5635 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5636 /* If we aren't making room for this section, then
5637 it must be SHT_NOBITS regardless of what we've
5638 set via struct bfd_elf_special_section. */
5639 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5641 /* Find out whether this segment contains any loadable
5644 for (i
= 0; i
< m
->count
; i
++)
5645 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5647 no_contents
= FALSE
;
5651 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5653 /* Broken hardware and/or kernel require that files do not
5654 map the same page with different permissions on some hppa
5657 && (abfd
->flags
& D_PAGED
) != 0
5658 && bed
->no_page_alias
5659 && (off
& (maxpagesize
- 1)) != 0
5660 && ((off
& -maxpagesize
)
5661 == ((off
+ off_adjust
) & -maxpagesize
)))
5662 off_adjust
+= maxpagesize
;
5666 /* We shouldn't need to align the segment on disk since
5667 the segment doesn't need file space, but the gABI
5668 arguably requires the alignment and glibc ld.so
5669 checks it. So to comply with the alignment
5670 requirement but not waste file space, we adjust
5671 p_offset for just this segment. (OFF_ADJUST is
5672 subtracted from OFF later.) This may put p_offset
5673 past the end of file, but that shouldn't matter. */
5678 /* Make sure the .dynamic section is the first section in the
5679 PT_DYNAMIC segment. */
5680 else if (p
->p_type
== PT_DYNAMIC
5682 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5685 (_("%pB: The first section in the PT_DYNAMIC segment"
5686 " is not the .dynamic section"),
5688 bfd_set_error (bfd_error_bad_value
);
5691 /* Set the note section type to SHT_NOTE. */
5692 else if (p
->p_type
== PT_NOTE
)
5693 for (i
= 0; i
< m
->count
; i
++)
5694 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5696 if (m
->includes_filehdr
)
5698 if (!m
->p_flags_valid
)
5700 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5701 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5702 if (p
->p_type
== PT_LOAD
)
5706 if (p
->p_vaddr
< (bfd_vma
) off
5707 || (!m
->p_paddr_valid
5708 && p
->p_paddr
< (bfd_vma
) off
))
5711 (_("%pB: not enough room for program headers,"
5712 " try linking with -N"),
5714 bfd_set_error (bfd_error_bad_value
);
5718 if (!m
->p_paddr_valid
)
5722 else if (sorted_seg_map
[0]->includes_filehdr
)
5724 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5725 p
->p_vaddr
= filehdr
->p_vaddr
;
5726 if (!m
->p_paddr_valid
)
5727 p
->p_paddr
= filehdr
->p_paddr
;
5731 if (m
->includes_phdrs
)
5733 if (!m
->p_flags_valid
)
5735 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5736 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5737 if (!m
->includes_filehdr
)
5739 if (p
->p_type
== PT_LOAD
)
5741 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5744 p
->p_vaddr
-= off
- p
->p_offset
;
5745 if (!m
->p_paddr_valid
)
5746 p
->p_paddr
-= off
- p
->p_offset
;
5749 else if (phdr_load_seg
!= NULL
)
5751 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5752 bfd_vma phdr_off
= 0; /* Octets. */
5753 if (phdr_load_seg
->includes_filehdr
)
5754 phdr_off
= bed
->s
->sizeof_ehdr
;
5755 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5756 if (!m
->p_paddr_valid
)
5757 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5758 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5761 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5765 if (p
->p_type
== PT_LOAD
5766 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5768 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5773 /* Put meaningless p_offset for PT_LOAD segments
5774 without file contents somewhere within the first
5775 page, in an attempt to not point past EOF. */
5776 bfd_size_type align
= maxpagesize
;
5777 if (align
< p
->p_align
)
5781 p
->p_offset
= off
% align
;
5786 file_ptr adjust
; /* Octets. */
5788 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5790 p
->p_filesz
+= adjust
;
5791 p
->p_memsz
+= adjust
;
5795 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5796 maps. Set filepos for sections in PT_LOAD segments, and in
5797 core files, for sections in PT_NOTE segments.
5798 assign_file_positions_for_non_load_sections will set filepos
5799 for other sections and update p_filesz for other segments. */
5800 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5803 bfd_size_type align
;
5804 Elf_Internal_Shdr
*this_hdr
;
5807 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5808 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5810 if ((p
->p_type
== PT_LOAD
5811 || p
->p_type
== PT_TLS
)
5812 && (this_hdr
->sh_type
!= SHT_NOBITS
5813 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5814 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5815 || p
->p_type
== PT_TLS
))))
5817 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5818 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5819 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5820 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5824 || p_end
< p_start
))
5827 /* xgettext:c-format */
5828 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5829 abfd
, sec
, (uint64_t) s_start
/ opb
,
5830 (uint64_t) p_end
/ opb
);
5832 sec
->lma
= p_end
/ opb
;
5834 p
->p_memsz
+= adjust
;
5836 if (p
->p_type
== PT_LOAD
)
5838 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5841 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5843 /* We have a PROGBITS section following NOBITS ones.
5844 Allocate file space for the NOBITS section(s) and
5846 adjust
= p
->p_memsz
- p
->p_filesz
;
5847 if (!write_zeros (abfd
, off
, adjust
))
5851 /* We only adjust sh_offset in SHT_NOBITS sections
5852 as would seem proper for their address when the
5853 section is first in the segment. sh_offset
5854 doesn't really have any significance for
5855 SHT_NOBITS anyway, apart from a notional position
5856 relative to other sections. Historically we
5857 didn't bother with adjusting sh_offset and some
5858 programs depend on it not being adjusted. See
5859 pr12921 and pr25662. */
5860 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
5863 if (this_hdr
->sh_type
== SHT_NOBITS
)
5864 off_adjust
+= adjust
;
5867 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5868 p
->p_filesz
+= adjust
;
5871 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5873 /* The section at i == 0 is the one that actually contains
5877 this_hdr
->sh_offset
= sec
->filepos
= off
;
5878 off
+= this_hdr
->sh_size
;
5879 p
->p_filesz
= this_hdr
->sh_size
;
5885 /* The rest are fake sections that shouldn't be written. */
5894 if (p
->p_type
== PT_LOAD
)
5896 this_hdr
->sh_offset
= sec
->filepos
= off
;
5897 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5898 off
+= this_hdr
->sh_size
;
5900 else if (this_hdr
->sh_type
== SHT_NOBITS
5901 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5902 && this_hdr
->sh_offset
== 0)
5904 /* This is a .tbss section that didn't get a PT_LOAD.
5905 (See _bfd_elf_map_sections_to_segments "Create a
5906 final PT_LOAD".) Set sh_offset to the value it
5907 would have if we had created a zero p_filesz and
5908 p_memsz PT_LOAD header for the section. This
5909 also makes the PT_TLS header have the same
5911 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5913 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5916 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5918 p
->p_filesz
+= this_hdr
->sh_size
;
5919 /* A load section without SHF_ALLOC is something like
5920 a note section in a PT_NOTE segment. These take
5921 file space but are not loaded into memory. */
5922 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5923 p
->p_memsz
+= this_hdr
->sh_size
;
5925 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5927 if (p
->p_type
== PT_TLS
)
5928 p
->p_memsz
+= this_hdr
->sh_size
;
5930 /* .tbss is special. It doesn't contribute to p_memsz of
5932 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5933 p
->p_memsz
+= this_hdr
->sh_size
;
5936 if (align
> p
->p_align
5937 && !m
->p_align_valid
5938 && (p
->p_type
!= PT_LOAD
5939 || (abfd
->flags
& D_PAGED
) == 0))
5943 if (!m
->p_flags_valid
)
5946 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5948 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5955 /* PR ld/20815 - Check that the program header segment, if
5956 present, will be loaded into memory. */
5957 if (p
->p_type
== PT_PHDR
5958 && phdr_load_seg
== NULL
5959 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
5960 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
5962 /* The fix for this error is usually to edit the linker script being
5963 used and set up the program headers manually. Either that or
5964 leave room for the headers at the start of the SECTIONS. */
5965 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5966 " by LOAD segment"),
5968 if (link_info
== NULL
)
5970 /* Arrange for the linker to exit with an error, deleting
5971 the output file unless --noinhibit-exec is given. */
5972 link_info
->callbacks
->info ("%X");
5975 /* Check that all sections are in a PT_LOAD segment.
5976 Don't check funky gdb generated core files. */
5977 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5979 bfd_boolean check_vma
= TRUE
;
5981 for (i
= 1; i
< m
->count
; i
++)
5982 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5983 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5984 ->this_hdr
), p
) != 0
5985 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5986 ->this_hdr
), p
) != 0)
5988 /* Looks like we have overlays packed into the segment. */
5993 for (i
= 0; i
< m
->count
; i
++)
5995 Elf_Internal_Shdr
*this_hdr
;
5998 sec
= m
->sections
[i
];
5999 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6000 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6001 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6004 /* xgettext:c-format */
6005 (_("%pB: section `%pA' can't be allocated in segment %d"),
6007 print_segment_map (m
);
6013 elf_next_file_pos (abfd
) = off
;
6015 if (link_info
!= NULL
6016 && phdr_load_seg
!= NULL
6017 && phdr_load_seg
->includes_filehdr
)
6019 /* There is a segment that contains both the file headers and the
6020 program headers, so provide a symbol __ehdr_start pointing there.
6021 A program can use this to examine itself robustly. */
6023 struct elf_link_hash_entry
*hash
6024 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6025 FALSE
, FALSE
, TRUE
);
6026 /* If the symbol was referenced and not defined, define it. */
6028 && (hash
->root
.type
== bfd_link_hash_new
6029 || hash
->root
.type
== bfd_link_hash_undefined
6030 || hash
->root
.type
== bfd_link_hash_undefweak
6031 || hash
->root
.type
== bfd_link_hash_common
))
6034 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6036 if (phdr_load_seg
->count
!= 0)
6037 /* The segment contains sections, so use the first one. */
6038 s
= phdr_load_seg
->sections
[0];
6040 /* Use the first (i.e. lowest-addressed) section in any segment. */
6041 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6042 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6050 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6051 hash
->root
.u
.def
.section
= s
;
6055 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6056 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6059 hash
->root
.type
= bfd_link_hash_defined
;
6060 hash
->def_regular
= 1;
6068 /* Determine if a bfd is a debuginfo file. Unfortunately there
6069 is no defined method for detecting such files, so we have to
6070 use heuristics instead. */
6073 is_debuginfo_file (bfd
*abfd
)
6075 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6078 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6079 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6080 Elf_Internal_Shdr
**headerp
;
6082 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6084 Elf_Internal_Shdr
*header
= * headerp
;
6086 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6087 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6088 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6089 && header
->sh_type
!= SHT_NOBITS
6090 && header
->sh_type
!= SHT_NOTE
)
6097 /* Assign file positions for the other sections, except for compressed debugging
6098 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6101 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6102 struct bfd_link_info
*link_info
)
6104 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6105 Elf_Internal_Shdr
**i_shdrpp
;
6106 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6107 Elf_Internal_Phdr
*phdrs
;
6108 Elf_Internal_Phdr
*p
;
6109 struct elf_segment_map
*m
;
6111 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6113 i_shdrpp
= elf_elfsections (abfd
);
6114 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6115 off
= elf_next_file_pos (abfd
);
6116 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6118 Elf_Internal_Shdr
*hdr
;
6121 if (hdr
->bfd_section
!= NULL
6122 && (hdr
->bfd_section
->filepos
!= 0
6123 || (hdr
->sh_type
== SHT_NOBITS
6124 && hdr
->contents
== NULL
)))
6125 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6126 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6128 if (hdr
->sh_size
!= 0
6129 /* PR 24717 - debuginfo files are known to be not strictly
6130 compliant with the ELF standard. In particular they often
6131 have .note.gnu.property sections that are outside of any
6132 loadable segment. This is not a problem for such files,
6133 so do not warn about them. */
6134 && ! is_debuginfo_file (abfd
))
6136 /* xgettext:c-format */
6137 (_("%pB: warning: allocated section `%s' not in segment"),
6139 (hdr
->bfd_section
== NULL
6141 : hdr
->bfd_section
->name
));
6142 /* We don't need to page align empty sections. */
6143 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6144 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6147 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6149 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6152 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6153 && hdr
->bfd_section
== NULL
)
6154 /* We don't know the offset of these sections yet: their size has
6155 not been decided. */
6156 || (hdr
->bfd_section
!= NULL
6157 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6158 || (bfd_section_is_ctf (hdr
->bfd_section
)
6159 && abfd
->is_linker_output
)))
6160 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6161 || (elf_symtab_shndx_list (abfd
) != NULL
6162 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6163 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6164 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6165 hdr
->sh_offset
= -1;
6167 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6169 elf_next_file_pos (abfd
) = off
;
6171 /* Now that we have set the section file positions, we can set up
6172 the file positions for the non PT_LOAD segments. */
6173 phdrs
= elf_tdata (abfd
)->phdr
;
6174 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6176 if (p
->p_type
== PT_GNU_RELRO
)
6178 bfd_vma start
, end
; /* Bytes. */
6181 if (link_info
!= NULL
)
6183 /* During linking the range of the RELRO segment is passed
6184 in link_info. Note that there may be padding between
6185 relro_start and the first RELRO section. */
6186 start
= link_info
->relro_start
;
6187 end
= link_info
->relro_end
;
6189 else if (m
->count
!= 0)
6191 if (!m
->p_size_valid
)
6193 start
= m
->sections
[0]->vma
;
6194 end
= start
+ m
->p_size
/ opb
;
6205 struct elf_segment_map
*lm
;
6206 const Elf_Internal_Phdr
*lp
;
6209 /* Find a LOAD segment containing a section in the RELRO
6211 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6213 lm
= lm
->next
, lp
++)
6215 if (lp
->p_type
== PT_LOAD
6217 && (lm
->sections
[lm
->count
- 1]->vma
6218 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6219 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6221 && lm
->sections
[0]->vma
< end
)
6227 /* Find the section starting the RELRO segment. */
6228 for (i
= 0; i
< lm
->count
; i
++)
6230 asection
*s
= lm
->sections
[i
];
6239 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6240 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6241 p
->p_offset
= lm
->sections
[i
]->filepos
;
6242 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6243 p
->p_filesz
= p
->p_memsz
;
6245 /* The RELRO segment typically ends a few bytes
6246 into .got.plt but other layouts are possible.
6247 In cases where the end does not match any
6248 loaded section (for instance is in file
6249 padding), trim p_filesz back to correspond to
6250 the end of loaded section contents. */
6251 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6252 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6254 /* Preserve the alignment and flags if they are
6255 valid. The gold linker generates RW/4 for
6256 the PT_GNU_RELRO section. It is better for
6257 objcopy/strip to honor these attributes
6258 otherwise gdb will choke when using separate
6260 if (!m
->p_align_valid
)
6262 if (!m
->p_flags_valid
)
6268 if (link_info
!= NULL
)
6271 memset (p
, 0, sizeof *p
);
6273 else if (p
->p_type
== PT_GNU_STACK
)
6275 if (m
->p_size_valid
)
6276 p
->p_memsz
= m
->p_size
;
6278 else if (m
->count
!= 0)
6282 if (p
->p_type
!= PT_LOAD
6283 && (p
->p_type
!= PT_NOTE
6284 || bfd_get_format (abfd
) != bfd_core
))
6286 /* A user specified segment layout may include a PHDR
6287 segment that overlaps with a LOAD segment... */
6288 if (p
->p_type
== PT_PHDR
)
6294 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6296 /* PR 17512: file: 2195325e. */
6298 (_("%pB: error: non-load segment %d includes file header "
6299 "and/or program header"),
6300 abfd
, (int) (p
- phdrs
));
6305 p
->p_offset
= m
->sections
[0]->filepos
;
6306 for (i
= m
->count
; i
-- != 0;)
6308 asection
*sect
= m
->sections
[i
];
6309 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6310 if (hdr
->sh_type
!= SHT_NOBITS
)
6312 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6324 static elf_section_list
*
6325 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6327 for (;list
!= NULL
; list
= list
->next
)
6333 /* Work out the file positions of all the sections. This is called by
6334 _bfd_elf_compute_section_file_positions. All the section sizes and
6335 VMAs must be known before this is called.
6337 Reloc sections come in two flavours: Those processed specially as
6338 "side-channel" data attached to a section to which they apply, and those that
6339 bfd doesn't process as relocations. The latter sort are stored in a normal
6340 bfd section by bfd_section_from_shdr. We don't consider the former sort
6341 here, unless they form part of the loadable image. Reloc sections not
6342 assigned here (and compressed debugging sections and CTF sections which
6343 nothing else in the file can rely upon) will be handled later by
6344 assign_file_positions_for_relocs.
6346 We also don't set the positions of the .symtab and .strtab here. */
6349 assign_file_positions_except_relocs (bfd
*abfd
,
6350 struct bfd_link_info
*link_info
)
6352 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6353 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6354 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6357 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6358 && bfd_get_format (abfd
) != bfd_core
)
6360 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6361 unsigned int num_sec
= elf_numsections (abfd
);
6362 Elf_Internal_Shdr
**hdrpp
;
6366 /* Start after the ELF header. */
6367 off
= i_ehdrp
->e_ehsize
;
6369 /* We are not creating an executable, which means that we are
6370 not creating a program header, and that the actual order of
6371 the sections in the file is unimportant. */
6372 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6374 Elf_Internal_Shdr
*hdr
;
6377 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6378 && hdr
->bfd_section
== NULL
)
6379 /* Do not assign offsets for these sections yet: we don't know
6381 || (hdr
->bfd_section
!= NULL
6382 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6383 || (bfd_section_is_ctf (hdr
->bfd_section
)
6384 && abfd
->is_linker_output
)))
6385 || i
== elf_onesymtab (abfd
)
6386 || (elf_symtab_shndx_list (abfd
) != NULL
6387 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6388 || i
== elf_strtab_sec (abfd
)
6389 || i
== elf_shstrtab_sec (abfd
))
6391 hdr
->sh_offset
= -1;
6394 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6397 elf_next_file_pos (abfd
) = off
;
6398 elf_program_header_size (abfd
) = 0;
6402 /* Assign file positions for the loaded sections based on the
6403 assignment of sections to segments. */
6404 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6407 /* And for non-load sections. */
6408 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6412 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6415 /* Write out the program headers. */
6416 alloc
= i_ehdrp
->e_phnum
;
6419 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6420 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6428 _bfd_elf_init_file_header (bfd
*abfd
,
6429 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6431 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6432 struct elf_strtab_hash
*shstrtab
;
6433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6435 i_ehdrp
= elf_elfheader (abfd
);
6437 shstrtab
= _bfd_elf_strtab_init ();
6438 if (shstrtab
== NULL
)
6441 elf_shstrtab (abfd
) = shstrtab
;
6443 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6444 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6445 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6446 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6448 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6449 i_ehdrp
->e_ident
[EI_DATA
] =
6450 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6451 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6453 if ((abfd
->flags
& DYNAMIC
) != 0)
6454 i_ehdrp
->e_type
= ET_DYN
;
6455 else if ((abfd
->flags
& EXEC_P
) != 0)
6456 i_ehdrp
->e_type
= ET_EXEC
;
6457 else if (bfd_get_format (abfd
) == bfd_core
)
6458 i_ehdrp
->e_type
= ET_CORE
;
6460 i_ehdrp
->e_type
= ET_REL
;
6462 switch (bfd_get_arch (abfd
))
6464 case bfd_arch_unknown
:
6465 i_ehdrp
->e_machine
= EM_NONE
;
6468 /* There used to be a long list of cases here, each one setting
6469 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6470 in the corresponding bfd definition. To avoid duplication,
6471 the switch was removed. Machines that need special handling
6472 can generally do it in elf_backend_final_write_processing(),
6473 unless they need the information earlier than the final write.
6474 Such need can generally be supplied by replacing the tests for
6475 e_machine with the conditions used to determine it. */
6477 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6480 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6481 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6483 /* No program header, for now. */
6484 i_ehdrp
->e_phoff
= 0;
6485 i_ehdrp
->e_phentsize
= 0;
6486 i_ehdrp
->e_phnum
= 0;
6488 /* Each bfd section is section header entry. */
6489 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6490 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6492 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6493 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6494 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6495 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6496 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6497 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6498 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6499 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6500 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6506 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6508 FIXME: We used to have code here to sort the PT_LOAD segments into
6509 ascending order, as per the ELF spec. But this breaks some programs,
6510 including the Linux kernel. But really either the spec should be
6511 changed or the programs updated. */
6514 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6516 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6518 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6519 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6520 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6521 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6522 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6524 /* Find the lowest p_vaddr in PT_LOAD segments. */
6525 bfd_vma p_vaddr
= (bfd_vma
) -1;
6526 for (; segment
< end_segment
; segment
++)
6527 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6528 p_vaddr
= segment
->p_vaddr
;
6530 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6531 segments is non-zero. */
6533 i_ehdrp
->e_type
= ET_EXEC
;
6538 /* Assign file positions for all the reloc sections which are not part
6539 of the loadable file image, and the file position of section headers. */
6542 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6545 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6546 Elf_Internal_Shdr
*shdrp
;
6547 Elf_Internal_Ehdr
*i_ehdrp
;
6548 const struct elf_backend_data
*bed
;
6550 off
= elf_next_file_pos (abfd
);
6552 shdrpp
= elf_elfsections (abfd
);
6553 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6554 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6557 if (shdrp
->sh_offset
== -1)
6559 asection
*sec
= shdrp
->bfd_section
;
6560 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6561 || shdrp
->sh_type
== SHT_RELA
);
6562 bfd_boolean is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6565 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6567 if (!is_rel
&& !is_ctf
)
6569 const char *name
= sec
->name
;
6570 struct bfd_elf_section_data
*d
;
6572 /* Compress DWARF debug sections. */
6573 if (!bfd_compress_section (abfd
, sec
,
6577 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6578 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6580 /* If section is compressed with zlib-gnu, convert
6581 section name from .debug_* to .zdebug_*. */
6583 = convert_debug_to_zdebug (abfd
, name
);
6584 if (new_name
== NULL
)
6588 /* Add section name to section name section. */
6589 if (shdrp
->sh_name
!= (unsigned int) -1)
6592 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6594 d
= elf_section_data (sec
);
6596 /* Add reloc section name to section name section. */
6598 && !_bfd_elf_set_reloc_sh_name (abfd
,
6603 && !_bfd_elf_set_reloc_sh_name (abfd
,
6608 /* Update section size and contents. */
6609 shdrp
->sh_size
= sec
->size
;
6610 shdrp
->contents
= sec
->contents
;
6611 shdrp
->bfd_section
->contents
= NULL
;
6615 /* Update section size and contents. */
6616 shdrp
->sh_size
= sec
->size
;
6617 shdrp
->contents
= sec
->contents
;
6620 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6627 /* Place section name section after DWARF debug sections have been
6629 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6630 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6631 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6632 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6634 /* Place the section headers. */
6635 i_ehdrp
= elf_elfheader (abfd
);
6636 bed
= get_elf_backend_data (abfd
);
6637 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6638 i_ehdrp
->e_shoff
= off
;
6639 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6640 elf_next_file_pos (abfd
) = off
;
6646 _bfd_elf_write_object_contents (bfd
*abfd
)
6648 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6649 Elf_Internal_Shdr
**i_shdrp
;
6651 unsigned int count
, num_sec
;
6652 struct elf_obj_tdata
*t
;
6654 if (! abfd
->output_has_begun
6655 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6657 /* Do not rewrite ELF data when the BFD has been opened for update.
6658 abfd->output_has_begun was set to TRUE on opening, so creation of new
6659 sections, and modification of existing section sizes was restricted.
6660 This means the ELF header, program headers and section headers can't have
6662 If the contents of any sections has been modified, then those changes have
6663 already been written to the BFD. */
6664 else if (abfd
->direction
== both_direction
)
6666 BFD_ASSERT (abfd
->output_has_begun
);
6670 i_shdrp
= elf_elfsections (abfd
);
6673 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6677 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6680 /* After writing the headers, we need to write the sections too... */
6681 num_sec
= elf_numsections (abfd
);
6682 for (count
= 1; count
< num_sec
; count
++)
6684 i_shdrp
[count
]->sh_name
6685 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6686 i_shdrp
[count
]->sh_name
);
6687 if (bed
->elf_backend_section_processing
)
6688 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6690 if (i_shdrp
[count
]->contents
)
6692 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6694 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6695 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6700 /* Write out the section header names. */
6701 t
= elf_tdata (abfd
);
6702 if (elf_shstrtab (abfd
) != NULL
6703 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6704 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6707 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6710 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6713 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6714 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6715 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6721 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6723 /* Hopefully this can be done just like an object file. */
6724 return _bfd_elf_write_object_contents (abfd
);
6727 /* Given a section, search the header to find them. */
6730 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6732 const struct elf_backend_data
*bed
;
6733 unsigned int sec_index
;
6735 if (elf_section_data (asect
) != NULL
6736 && elf_section_data (asect
)->this_idx
!= 0)
6737 return elf_section_data (asect
)->this_idx
;
6739 if (bfd_is_abs_section (asect
))
6740 sec_index
= SHN_ABS
;
6741 else if (bfd_is_com_section (asect
))
6742 sec_index
= SHN_COMMON
;
6743 else if (bfd_is_und_section (asect
))
6744 sec_index
= SHN_UNDEF
;
6746 sec_index
= SHN_BAD
;
6748 bed
= get_elf_backend_data (abfd
);
6749 if (bed
->elf_backend_section_from_bfd_section
)
6751 int retval
= sec_index
;
6753 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6757 if (sec_index
== SHN_BAD
)
6758 bfd_set_error (bfd_error_nonrepresentable_section
);
6763 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6767 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6769 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6771 flagword flags
= asym_ptr
->flags
;
6773 /* When gas creates relocations against local labels, it creates its
6774 own symbol for the section, but does put the symbol into the
6775 symbol chain, so udata is 0. When the linker is generating
6776 relocatable output, this section symbol may be for one of the
6777 input sections rather than the output section. */
6778 if (asym_ptr
->udata
.i
== 0
6779 && (flags
& BSF_SECTION_SYM
)
6780 && asym_ptr
->section
)
6785 sec
= asym_ptr
->section
;
6786 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6787 sec
= sec
->output_section
;
6788 if (sec
->owner
== abfd
6789 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6790 && elf_section_syms (abfd
)[indx
] != NULL
)
6791 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6794 idx
= asym_ptr
->udata
.i
;
6798 /* This case can occur when using --strip-symbol on a symbol
6799 which is used in a relocation entry. */
6801 /* xgettext:c-format */
6802 (_("%pB: symbol `%s' required but not present"),
6803 abfd
, bfd_asymbol_name (asym_ptr
));
6804 bfd_set_error (bfd_error_no_symbols
);
6811 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6812 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6820 /* Rewrite program header information. */
6823 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6825 Elf_Internal_Ehdr
*iehdr
;
6826 struct elf_segment_map
*map
;
6827 struct elf_segment_map
*map_first
;
6828 struct elf_segment_map
**pointer_to_map
;
6829 Elf_Internal_Phdr
*segment
;
6832 unsigned int num_segments
;
6833 bfd_boolean phdr_included
= FALSE
;
6834 bfd_boolean p_paddr_valid
;
6835 bfd_vma maxpagesize
;
6836 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6837 unsigned int phdr_adjust_num
= 0;
6838 const struct elf_backend_data
*bed
;
6839 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
6841 bed
= get_elf_backend_data (ibfd
);
6842 iehdr
= elf_elfheader (ibfd
);
6845 pointer_to_map
= &map_first
;
6847 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6848 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6850 /* Returns the end address of the segment + 1. */
6851 #define SEGMENT_END(segment, start) \
6852 (start + (segment->p_memsz > segment->p_filesz \
6853 ? segment->p_memsz : segment->p_filesz))
6855 #define SECTION_SIZE(section, segment) \
6856 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6857 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6858 ? section->size : 0)
6860 /* Returns TRUE if the given section is contained within
6861 the given segment. VMA addresses are compared. */
6862 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6863 (section->vma * (opb) >= segment->p_vaddr \
6864 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6865 <= (SEGMENT_END (segment, segment->p_vaddr))))
6867 /* Returns TRUE if the given section is contained within
6868 the given segment. LMA addresses are compared. */
6869 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6870 (section->lma * (opb) >= base \
6871 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6872 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6873 <= SEGMENT_END (segment, base)))
6875 /* Handle PT_NOTE segment. */
6876 #define IS_NOTE(p, s) \
6877 (p->p_type == PT_NOTE \
6878 && elf_section_type (s) == SHT_NOTE \
6879 && (bfd_vma) s->filepos >= p->p_offset \
6880 && ((bfd_vma) s->filepos + s->size \
6881 <= p->p_offset + p->p_filesz))
6883 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6885 #define IS_COREFILE_NOTE(p, s) \
6887 && bfd_get_format (ibfd) == bfd_core \
6891 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6892 linker, which generates a PT_INTERP section with p_vaddr and
6893 p_memsz set to 0. */
6894 #define IS_SOLARIS_PT_INTERP(p, s) \
6896 && p->p_paddr == 0 \
6897 && p->p_memsz == 0 \
6898 && p->p_filesz > 0 \
6899 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6901 && (bfd_vma) s->filepos >= p->p_offset \
6902 && ((bfd_vma) s->filepos + s->size \
6903 <= p->p_offset + p->p_filesz))
6905 /* Decide if the given section should be included in the given segment.
6906 A section will be included if:
6907 1. It is within the address space of the segment -- we use the LMA
6908 if that is set for the segment and the VMA otherwise,
6909 2. It is an allocated section or a NOTE section in a PT_NOTE
6911 3. There is an output section associated with it,
6912 4. The section has not already been allocated to a previous segment.
6913 5. PT_GNU_STACK segments do not include any sections.
6914 6. PT_TLS segment includes only SHF_TLS sections.
6915 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6916 8. PT_DYNAMIC should not contain empty sections at the beginning
6917 (with the possible exception of .dynamic). */
6918 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6919 ((((segment->p_paddr \
6920 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6921 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
6922 && (section->flags & SEC_ALLOC) != 0) \
6923 || IS_NOTE (segment, section)) \
6924 && segment->p_type != PT_GNU_STACK \
6925 && (segment->p_type != PT_TLS \
6926 || (section->flags & SEC_THREAD_LOCAL)) \
6927 && (segment->p_type == PT_LOAD \
6928 || segment->p_type == PT_TLS \
6929 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6930 && (segment->p_type != PT_DYNAMIC \
6931 || SECTION_SIZE (section, segment) > 0 \
6932 || (segment->p_paddr \
6933 ? segment->p_paddr != section->lma * (opb) \
6934 : segment->p_vaddr != section->vma * (opb)) \
6935 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6936 && (segment->p_type != PT_LOAD || !section->segment_mark))
6938 /* If the output section of a section in the input segment is NULL,
6939 it is removed from the corresponding output segment. */
6940 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6941 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
6942 && section->output_section != NULL)
6944 /* Returns TRUE iff seg1 starts after the end of seg2. */
6945 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6946 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6948 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6949 their VMA address ranges and their LMA address ranges overlap.
6950 It is possible to have overlapping VMA ranges without overlapping LMA
6951 ranges. RedBoot images for example can have both .data and .bss mapped
6952 to the same VMA range, but with the .data section mapped to a different
6954 #define SEGMENT_OVERLAPS(seg1, seg2) \
6955 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6956 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6957 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6958 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6960 /* Initialise the segment mark field. */
6961 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6962 section
->segment_mark
= FALSE
;
6964 /* The Solaris linker creates program headers in which all the
6965 p_paddr fields are zero. When we try to objcopy or strip such a
6966 file, we get confused. Check for this case, and if we find it
6967 don't set the p_paddr_valid fields. */
6968 p_paddr_valid
= FALSE
;
6969 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6972 if (segment
->p_paddr
!= 0)
6974 p_paddr_valid
= TRUE
;
6978 /* Scan through the segments specified in the program header
6979 of the input BFD. For this first scan we look for overlaps
6980 in the loadable segments. These can be created by weird
6981 parameters to objcopy. Also, fix some solaris weirdness. */
6982 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6987 Elf_Internal_Phdr
*segment2
;
6989 if (segment
->p_type
== PT_INTERP
)
6990 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6991 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6993 /* Mininal change so that the normal section to segment
6994 assignment code will work. */
6995 segment
->p_vaddr
= section
->vma
* opb
;
6999 if (segment
->p_type
!= PT_LOAD
)
7001 /* Remove PT_GNU_RELRO segment. */
7002 if (segment
->p_type
== PT_GNU_RELRO
)
7003 segment
->p_type
= PT_NULL
;
7007 /* Determine if this segment overlaps any previous segments. */
7008 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7010 bfd_signed_vma extra_length
;
7012 if (segment2
->p_type
!= PT_LOAD
7013 || !SEGMENT_OVERLAPS (segment
, segment2
))
7016 /* Merge the two segments together. */
7017 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7019 /* Extend SEGMENT2 to include SEGMENT and then delete
7021 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
7022 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
7024 if (extra_length
> 0)
7026 segment2
->p_memsz
+= extra_length
;
7027 segment2
->p_filesz
+= extra_length
;
7030 segment
->p_type
= PT_NULL
;
7032 /* Since we have deleted P we must restart the outer loop. */
7034 segment
= elf_tdata (ibfd
)->phdr
;
7039 /* Extend SEGMENT to include SEGMENT2 and then delete
7041 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
7042 - SEGMENT_END (segment
, segment
->p_vaddr
));
7044 if (extra_length
> 0)
7046 segment
->p_memsz
+= extra_length
;
7047 segment
->p_filesz
+= extra_length
;
7050 segment2
->p_type
= PT_NULL
;
7055 /* The second scan attempts to assign sections to segments. */
7056 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7060 unsigned int section_count
;
7061 asection
**sections
;
7062 asection
*output_section
;
7064 asection
*matching_lma
;
7065 asection
*suggested_lma
;
7068 asection
*first_section
;
7070 if (segment
->p_type
== PT_NULL
)
7073 first_section
= NULL
;
7074 /* Compute how many sections might be placed into this segment. */
7075 for (section
= ibfd
->sections
, section_count
= 0;
7077 section
= section
->next
)
7079 /* Find the first section in the input segment, which may be
7080 removed from the corresponding output segment. */
7081 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7083 if (first_section
== NULL
)
7084 first_section
= section
;
7085 if (section
->output_section
!= NULL
)
7090 /* Allocate a segment map big enough to contain
7091 all of the sections we have selected. */
7092 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7093 amt
+= section_count
* sizeof (asection
*);
7094 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7098 /* Initialise the fields of the segment map. Default to
7099 using the physical address of the segment in the input BFD. */
7101 map
->p_type
= segment
->p_type
;
7102 map
->p_flags
= segment
->p_flags
;
7103 map
->p_flags_valid
= 1;
7105 /* If the first section in the input segment is removed, there is
7106 no need to preserve segment physical address in the corresponding
7108 if (!first_section
|| first_section
->output_section
!= NULL
)
7110 map
->p_paddr
= segment
->p_paddr
;
7111 map
->p_paddr_valid
= p_paddr_valid
;
7114 /* Determine if this segment contains the ELF file header
7115 and if it contains the program headers themselves. */
7116 map
->includes_filehdr
= (segment
->p_offset
== 0
7117 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7118 map
->includes_phdrs
= 0;
7120 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7122 map
->includes_phdrs
=
7123 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7124 && (segment
->p_offset
+ segment
->p_filesz
7125 >= ((bfd_vma
) iehdr
->e_phoff
7126 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7128 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7129 phdr_included
= TRUE
;
7132 if (section_count
== 0)
7134 /* Special segments, such as the PT_PHDR segment, may contain
7135 no sections, but ordinary, loadable segments should contain
7136 something. They are allowed by the ELF spec however, so only
7137 a warning is produced.
7138 There is however the valid use case of embedded systems which
7139 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7140 flash memory with zeros. No warning is shown for that case. */
7141 if (segment
->p_type
== PT_LOAD
7142 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7143 /* xgettext:c-format */
7145 (_("%pB: warning: empty loadable segment detected"
7146 " at vaddr=%#" PRIx64
", is this intentional?"),
7147 ibfd
, (uint64_t) segment
->p_vaddr
);
7149 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7151 *pointer_to_map
= map
;
7152 pointer_to_map
= &map
->next
;
7157 /* Now scan the sections in the input BFD again and attempt
7158 to add their corresponding output sections to the segment map.
7159 The problem here is how to handle an output section which has
7160 been moved (ie had its LMA changed). There are four possibilities:
7162 1. None of the sections have been moved.
7163 In this case we can continue to use the segment LMA from the
7166 2. All of the sections have been moved by the same amount.
7167 In this case we can change the segment's LMA to match the LMA
7168 of the first section.
7170 3. Some of the sections have been moved, others have not.
7171 In this case those sections which have not been moved can be
7172 placed in the current segment which will have to have its size,
7173 and possibly its LMA changed, and a new segment or segments will
7174 have to be created to contain the other sections.
7176 4. The sections have been moved, but not by the same amount.
7177 In this case we can change the segment's LMA to match the LMA
7178 of the first section and we will have to create a new segment
7179 or segments to contain the other sections.
7181 In order to save time, we allocate an array to hold the section
7182 pointers that we are interested in. As these sections get assigned
7183 to a segment, they are removed from this array. */
7185 amt
= section_count
* sizeof (asection
*);
7186 sections
= (asection
**) bfd_malloc (amt
);
7187 if (sections
== NULL
)
7190 /* Step One: Scan for segment vs section LMA conflicts.
7191 Also add the sections to the section array allocated above.
7192 Also add the sections to the current segment. In the common
7193 case, where the sections have not been moved, this means that
7194 we have completely filled the segment, and there is nothing
7197 matching_lma
= NULL
;
7198 suggested_lma
= NULL
;
7200 for (section
= first_section
, j
= 0;
7202 section
= section
->next
)
7204 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7206 output_section
= section
->output_section
;
7208 sections
[j
++] = section
;
7210 /* The Solaris native linker always sets p_paddr to 0.
7211 We try to catch that case here, and set it to the
7212 correct value. Note - some backends require that
7213 p_paddr be left as zero. */
7215 && segment
->p_vaddr
!= 0
7216 && !bed
->want_p_paddr_set_to_zero
7218 && output_section
->lma
!= 0
7219 && (align_power (segment
->p_vaddr
7220 + (map
->includes_filehdr
7221 ? iehdr
->e_ehsize
: 0)
7222 + (map
->includes_phdrs
7223 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7225 output_section
->alignment_power
* opb
)
7226 == (output_section
->vma
* opb
)))
7227 map
->p_paddr
= segment
->p_vaddr
;
7229 /* Match up the physical address of the segment with the
7230 LMA address of the output section. */
7231 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7233 || IS_COREFILE_NOTE (segment
, section
)
7234 || (bed
->want_p_paddr_set_to_zero
7235 && IS_CONTAINED_BY_VMA (output_section
, segment
, opb
)))
7237 if (matching_lma
== NULL
7238 || output_section
->lma
< matching_lma
->lma
)
7239 matching_lma
= output_section
;
7241 /* We assume that if the section fits within the segment
7242 then it does not overlap any other section within that
7244 map
->sections
[isec
++] = output_section
;
7246 else if (suggested_lma
== NULL
)
7247 suggested_lma
= output_section
;
7249 if (j
== section_count
)
7254 BFD_ASSERT (j
== section_count
);
7256 /* Step Two: Adjust the physical address of the current segment,
7258 if (isec
== section_count
)
7260 /* All of the sections fitted within the segment as currently
7261 specified. This is the default case. Add the segment to
7262 the list of built segments and carry on to process the next
7263 program header in the input BFD. */
7264 map
->count
= section_count
;
7265 *pointer_to_map
= map
;
7266 pointer_to_map
= &map
->next
;
7269 && !bed
->want_p_paddr_set_to_zero
)
7271 bfd_vma hdr_size
= 0;
7272 if (map
->includes_filehdr
)
7273 hdr_size
= iehdr
->e_ehsize
;
7274 if (map
->includes_phdrs
)
7275 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7277 /* Account for padding before the first section in the
7279 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7280 - matching_lma
->lma
);
7288 /* Change the current segment's physical address to match
7289 the LMA of the first section that fitted, or if no
7290 section fitted, the first section. */
7291 if (matching_lma
== NULL
)
7292 matching_lma
= suggested_lma
;
7294 map
->p_paddr
= matching_lma
->lma
* opb
;
7296 /* Offset the segment physical address from the lma
7297 to allow for space taken up by elf headers. */
7298 if (map
->includes_phdrs
)
7300 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7302 /* iehdr->e_phnum is just an estimate of the number
7303 of program headers that we will need. Make a note
7304 here of the number we used and the segment we chose
7305 to hold these headers, so that we can adjust the
7306 offset when we know the correct value. */
7307 phdr_adjust_num
= iehdr
->e_phnum
;
7308 phdr_adjust_seg
= map
;
7311 if (map
->includes_filehdr
)
7313 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7314 map
->p_paddr
-= iehdr
->e_ehsize
;
7315 /* We've subtracted off the size of headers from the
7316 first section lma, but there may have been some
7317 alignment padding before that section too. Try to
7318 account for that by adjusting the segment lma down to
7319 the same alignment. */
7320 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7321 align
= segment
->p_align
;
7322 map
->p_paddr
&= -(align
* opb
);
7326 /* Step Three: Loop over the sections again, this time assigning
7327 those that fit to the current segment and removing them from the
7328 sections array; but making sure not to leave large gaps. Once all
7329 possible sections have been assigned to the current segment it is
7330 added to the list of built segments and if sections still remain
7331 to be assigned, a new segment is constructed before repeating
7337 suggested_lma
= NULL
;
7339 /* Fill the current segment with sections that fit. */
7340 for (j
= 0; j
< section_count
; j
++)
7342 section
= sections
[j
];
7344 if (section
== NULL
)
7347 output_section
= section
->output_section
;
7349 BFD_ASSERT (output_section
!= NULL
);
7351 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7353 || IS_COREFILE_NOTE (segment
, section
))
7355 if (map
->count
== 0)
7357 /* If the first section in a segment does not start at
7358 the beginning of the segment, then something is
7360 if (align_power (map
->p_paddr
7361 + (map
->includes_filehdr
7362 ? iehdr
->e_ehsize
: 0)
7363 + (map
->includes_phdrs
7364 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7366 output_section
->alignment_power
* opb
)
7367 != output_section
->lma
* opb
)
7374 prev_sec
= map
->sections
[map
->count
- 1];
7376 /* If the gap between the end of the previous section
7377 and the start of this section is more than
7378 maxpagesize then we need to start a new segment. */
7379 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7381 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7382 || (prev_sec
->lma
+ prev_sec
->size
7383 > output_section
->lma
))
7385 if (suggested_lma
== NULL
)
7386 suggested_lma
= output_section
;
7392 map
->sections
[map
->count
++] = output_section
;
7395 if (segment
->p_type
== PT_LOAD
)
7396 section
->segment_mark
= TRUE
;
7398 else if (suggested_lma
== NULL
)
7399 suggested_lma
= output_section
;
7402 /* PR 23932. A corrupt input file may contain sections that cannot
7403 be assigned to any segment - because for example they have a
7404 negative size - or segments that do not contain any sections.
7405 But there are also valid reasons why a segment can be empty.
7406 So allow a count of zero. */
7408 /* Add the current segment to the list of built segments. */
7409 *pointer_to_map
= map
;
7410 pointer_to_map
= &map
->next
;
7412 if (isec
< section_count
)
7414 /* We still have not allocated all of the sections to
7415 segments. Create a new segment here, initialise it
7416 and carry on looping. */
7417 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7418 amt
+= section_count
* sizeof (asection
*);
7419 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7426 /* Initialise the fields of the segment map. Set the physical
7427 physical address to the LMA of the first section that has
7428 not yet been assigned. */
7430 map
->p_type
= segment
->p_type
;
7431 map
->p_flags
= segment
->p_flags
;
7432 map
->p_flags_valid
= 1;
7433 map
->p_paddr
= suggested_lma
->lma
* opb
;
7434 map
->p_paddr_valid
= p_paddr_valid
;
7435 map
->includes_filehdr
= 0;
7436 map
->includes_phdrs
= 0;
7441 bfd_set_error (bfd_error_sorry
);
7445 while (isec
< section_count
);
7450 elf_seg_map (obfd
) = map_first
;
7452 /* If we had to estimate the number of program headers that were
7453 going to be needed, then check our estimate now and adjust
7454 the offset if necessary. */
7455 if (phdr_adjust_seg
!= NULL
)
7459 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7462 if (count
> phdr_adjust_num
)
7463 phdr_adjust_seg
->p_paddr
7464 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7466 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7467 if (map
->p_type
== PT_PHDR
)
7470 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7471 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7478 #undef IS_CONTAINED_BY_VMA
7479 #undef IS_CONTAINED_BY_LMA
7481 #undef IS_COREFILE_NOTE
7482 #undef IS_SOLARIS_PT_INTERP
7483 #undef IS_SECTION_IN_INPUT_SEGMENT
7484 #undef INCLUDE_SECTION_IN_SEGMENT
7485 #undef SEGMENT_AFTER_SEGMENT
7486 #undef SEGMENT_OVERLAPS
7490 /* Copy ELF program header information. */
7493 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7495 Elf_Internal_Ehdr
*iehdr
;
7496 struct elf_segment_map
*map
;
7497 struct elf_segment_map
*map_first
;
7498 struct elf_segment_map
**pointer_to_map
;
7499 Elf_Internal_Phdr
*segment
;
7501 unsigned int num_segments
;
7502 bfd_boolean phdr_included
= FALSE
;
7503 bfd_boolean p_paddr_valid
;
7504 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7506 iehdr
= elf_elfheader (ibfd
);
7509 pointer_to_map
= &map_first
;
7511 /* If all the segment p_paddr fields are zero, don't set
7512 map->p_paddr_valid. */
7513 p_paddr_valid
= FALSE
;
7514 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7515 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7518 if (segment
->p_paddr
!= 0)
7520 p_paddr_valid
= TRUE
;
7524 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7529 unsigned int section_count
;
7531 Elf_Internal_Shdr
*this_hdr
;
7532 asection
*first_section
= NULL
;
7533 asection
*lowest_section
;
7535 /* Compute how many sections are in this segment. */
7536 for (section
= ibfd
->sections
, section_count
= 0;
7538 section
= section
->next
)
7540 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7541 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7543 if (first_section
== NULL
)
7544 first_section
= section
;
7549 /* Allocate a segment map big enough to contain
7550 all of the sections we have selected. */
7551 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7552 amt
+= section_count
* sizeof (asection
*);
7553 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7557 /* Initialize the fields of the output segment map with the
7560 map
->p_type
= segment
->p_type
;
7561 map
->p_flags
= segment
->p_flags
;
7562 map
->p_flags_valid
= 1;
7563 map
->p_paddr
= segment
->p_paddr
;
7564 map
->p_paddr_valid
= p_paddr_valid
;
7565 map
->p_align
= segment
->p_align
;
7566 map
->p_align_valid
= 1;
7567 map
->p_vaddr_offset
= 0;
7569 if (map
->p_type
== PT_GNU_RELRO
7570 || map
->p_type
== PT_GNU_STACK
)
7572 /* The PT_GNU_RELRO segment may contain the first a few
7573 bytes in the .got.plt section even if the whole .got.plt
7574 section isn't in the PT_GNU_RELRO segment. We won't
7575 change the size of the PT_GNU_RELRO segment.
7576 Similarly, PT_GNU_STACK size is significant on uclinux
7578 map
->p_size
= segment
->p_memsz
;
7579 map
->p_size_valid
= 1;
7582 /* Determine if this segment contains the ELF file header
7583 and if it contains the program headers themselves. */
7584 map
->includes_filehdr
= (segment
->p_offset
== 0
7585 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7587 map
->includes_phdrs
= 0;
7588 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7590 map
->includes_phdrs
=
7591 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7592 && (segment
->p_offset
+ segment
->p_filesz
7593 >= ((bfd_vma
) iehdr
->e_phoff
7594 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7596 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7597 phdr_included
= TRUE
;
7600 lowest_section
= NULL
;
7601 if (section_count
!= 0)
7603 unsigned int isec
= 0;
7605 for (section
= first_section
;
7607 section
= section
->next
)
7609 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7610 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7612 map
->sections
[isec
++] = section
->output_section
;
7613 if ((section
->flags
& SEC_ALLOC
) != 0)
7617 if (lowest_section
== NULL
7618 || section
->lma
< lowest_section
->lma
)
7619 lowest_section
= section
;
7621 /* Section lmas are set up from PT_LOAD header
7622 p_paddr in _bfd_elf_make_section_from_shdr.
7623 If this header has a p_paddr that disagrees
7624 with the section lma, flag the p_paddr as
7626 if ((section
->flags
& SEC_LOAD
) != 0)
7627 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7629 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7630 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7631 map
->p_paddr_valid
= FALSE
;
7633 if (isec
== section_count
)
7639 if (section_count
== 0)
7640 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7641 else if (map
->p_paddr_valid
)
7643 /* Account for padding before the first section in the segment. */
7644 bfd_vma hdr_size
= 0;
7645 if (map
->includes_filehdr
)
7646 hdr_size
= iehdr
->e_ehsize
;
7647 if (map
->includes_phdrs
)
7648 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7650 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7651 - (lowest_section
? lowest_section
->lma
: 0));
7654 map
->count
= section_count
;
7655 *pointer_to_map
= map
;
7656 pointer_to_map
= &map
->next
;
7659 elf_seg_map (obfd
) = map_first
;
7663 /* Copy private BFD data. This copies or rewrites ELF program header
7667 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7669 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7670 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7673 if (elf_tdata (ibfd
)->phdr
== NULL
)
7676 if (ibfd
->xvec
== obfd
->xvec
)
7678 /* Check to see if any sections in the input BFD
7679 covered by ELF program header have changed. */
7680 Elf_Internal_Phdr
*segment
;
7681 asection
*section
, *osec
;
7682 unsigned int i
, num_segments
;
7683 Elf_Internal_Shdr
*this_hdr
;
7684 const struct elf_backend_data
*bed
;
7686 bed
= get_elf_backend_data (ibfd
);
7688 /* Regenerate the segment map if p_paddr is set to 0. */
7689 if (bed
->want_p_paddr_set_to_zero
)
7692 /* Initialize the segment mark field. */
7693 for (section
= obfd
->sections
; section
!= NULL
;
7694 section
= section
->next
)
7695 section
->segment_mark
= FALSE
;
7697 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7698 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7702 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7703 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7704 which severly confuses things, so always regenerate the segment
7705 map in this case. */
7706 if (segment
->p_paddr
== 0
7707 && segment
->p_memsz
== 0
7708 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7711 for (section
= ibfd
->sections
;
7712 section
!= NULL
; section
= section
->next
)
7714 /* We mark the output section so that we know it comes
7715 from the input BFD. */
7716 osec
= section
->output_section
;
7718 osec
->segment_mark
= TRUE
;
7720 /* Check if this section is covered by the segment. */
7721 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7722 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7724 /* FIXME: Check if its output section is changed or
7725 removed. What else do we need to check? */
7727 || section
->flags
!= osec
->flags
7728 || section
->lma
!= osec
->lma
7729 || section
->vma
!= osec
->vma
7730 || section
->size
!= osec
->size
7731 || section
->rawsize
!= osec
->rawsize
7732 || section
->alignment_power
!= osec
->alignment_power
)
7738 /* Check to see if any output section do not come from the
7740 for (section
= obfd
->sections
; section
!= NULL
;
7741 section
= section
->next
)
7743 if (!section
->segment_mark
)
7746 section
->segment_mark
= FALSE
;
7749 return copy_elf_program_header (ibfd
, obfd
);
7753 if (ibfd
->xvec
== obfd
->xvec
)
7755 /* When rewriting program header, set the output maxpagesize to
7756 the maximum alignment of input PT_LOAD segments. */
7757 Elf_Internal_Phdr
*segment
;
7759 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7760 bfd_vma maxpagesize
= 0;
7762 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7765 if (segment
->p_type
== PT_LOAD
7766 && maxpagesize
< segment
->p_align
)
7768 /* PR 17512: file: f17299af. */
7769 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7770 /* xgettext:c-format */
7771 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7772 PRIx64
" is too large"),
7773 ibfd
, (uint64_t) segment
->p_align
);
7775 maxpagesize
= segment
->p_align
;
7778 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7779 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7782 return rewrite_elf_program_header (ibfd
, obfd
);
7785 /* Initialize private output section information from input section. */
7788 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7792 struct bfd_link_info
*link_info
)
7795 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7796 bfd_boolean final_link
= (link_info
!= NULL
7797 && !bfd_link_relocatable (link_info
));
7799 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7800 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7803 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7805 /* If this is a known ABI section, ELF section type and flags may
7806 have been set up when OSEC was created. For normal sections we
7807 allow the user to override the type and flags other than
7808 SHF_MASKOS and SHF_MASKPROC. */
7809 if (elf_section_type (osec
) == SHT_PROGBITS
7810 || elf_section_type (osec
) == SHT_NOTE
7811 || elf_section_type (osec
) == SHT_NOBITS
)
7812 elf_section_type (osec
) = SHT_NULL
;
7813 /* For objcopy and relocatable link, copy the ELF section type from
7814 the input file if the BFD section flags are the same. (If they
7815 are different the user may be doing something like
7816 "objcopy --set-section-flags .text=alloc,data".) For a final
7817 link allow some flags that the linker clears to differ. */
7818 if (elf_section_type (osec
) == SHT_NULL
7819 && (osec
->flags
== isec
->flags
7821 && ((osec
->flags
^ isec
->flags
)
7822 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7823 elf_section_type (osec
) = elf_section_type (isec
);
7825 /* FIXME: Is this correct for all OS/PROC specific flags? */
7826 elf_section_flags (osec
) = (elf_section_flags (isec
)
7827 & (SHF_MASKOS
| SHF_MASKPROC
));
7829 /* Copy sh_info from input for mbind section. */
7830 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7831 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7832 elf_section_data (osec
)->this_hdr
.sh_info
7833 = elf_section_data (isec
)->this_hdr
.sh_info
;
7835 /* Set things up for objcopy and relocatable link. The output
7836 SHT_GROUP section will have its elf_next_in_group pointing back
7837 to the input group members. Ignore linker created group section.
7838 See elfNN_ia64_object_p in elfxx-ia64.c. */
7839 if ((link_info
== NULL
7840 || !link_info
->resolve_section_groups
)
7841 && (elf_sec_group (isec
) == NULL
7842 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7844 if (elf_section_flags (isec
) & SHF_GROUP
)
7845 elf_section_flags (osec
) |= SHF_GROUP
;
7846 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7847 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7850 /* If not decompress, preserve SHF_COMPRESSED. */
7851 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7852 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7855 ihdr
= &elf_section_data (isec
)->this_hdr
;
7857 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7858 don't use the output section of the linked-to section since it
7859 may be NULL at this point. */
7860 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7862 ohdr
= &elf_section_data (osec
)->this_hdr
;
7863 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7864 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7867 osec
->use_rela_p
= isec
->use_rela_p
;
7872 /* Copy private section information. This copies over the entsize
7873 field, and sometimes the info field. */
7876 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7881 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7883 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7884 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7887 ihdr
= &elf_section_data (isec
)->this_hdr
;
7888 ohdr
= &elf_section_data (osec
)->this_hdr
;
7890 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7892 if (ihdr
->sh_type
== SHT_SYMTAB
7893 || ihdr
->sh_type
== SHT_DYNSYM
7894 || ihdr
->sh_type
== SHT_GNU_verneed
7895 || ihdr
->sh_type
== SHT_GNU_verdef
)
7896 ohdr
->sh_info
= ihdr
->sh_info
;
7898 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7902 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7903 necessary if we are removing either the SHT_GROUP section or any of
7904 the group member sections. DISCARDED is the value that a section's
7905 output_section has if the section will be discarded, NULL when this
7906 function is called from objcopy, bfd_abs_section_ptr when called
7910 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7914 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7915 if (elf_section_type (isec
) == SHT_GROUP
)
7917 asection
*first
= elf_next_in_group (isec
);
7918 asection
*s
= first
;
7919 bfd_size_type removed
= 0;
7923 /* If this member section is being output but the
7924 SHT_GROUP section is not, then clear the group info
7925 set up by _bfd_elf_copy_private_section_data. */
7926 if (s
->output_section
!= discarded
7927 && isec
->output_section
== discarded
)
7929 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7930 elf_group_name (s
->output_section
) = NULL
;
7932 /* Conversely, if the member section is not being output
7933 but the SHT_GROUP section is, then adjust its size. */
7934 else if (s
->output_section
== discarded
7935 && isec
->output_section
!= discarded
)
7937 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7939 if (elf_sec
->rel
.hdr
!= NULL
7940 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7942 if (elf_sec
->rela
.hdr
!= NULL
7943 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7946 s
= elf_next_in_group (s
);
7952 if (discarded
!= NULL
)
7954 /* If we've been called for ld -r, then we need to
7955 adjust the input section size. */
7956 if (isec
->rawsize
== 0)
7957 isec
->rawsize
= isec
->size
;
7958 isec
->size
= isec
->rawsize
- removed
;
7959 if (isec
->size
<= 4)
7962 isec
->flags
|= SEC_EXCLUDE
;
7967 /* Adjust the output section size when called from
7969 isec
->output_section
->size
-= removed
;
7970 if (isec
->output_section
->size
<= 4)
7972 isec
->output_section
->size
= 0;
7973 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7982 /* Copy private header information. */
7985 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7987 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7988 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7991 /* Copy over private BFD data if it has not already been copied.
7992 This must be done here, rather than in the copy_private_bfd_data
7993 entry point, because the latter is called after the section
7994 contents have been set, which means that the program headers have
7995 already been worked out. */
7996 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7998 if (! copy_private_bfd_data (ibfd
, obfd
))
8002 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8005 /* Copy private symbol information. If this symbol is in a section
8006 which we did not map into a BFD section, try to map the section
8007 index correctly. We use special macro definitions for the mapped
8008 section indices; these definitions are interpreted by the
8009 swap_out_syms function. */
8011 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8012 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8013 #define MAP_STRTAB (SHN_HIOS + 3)
8014 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8015 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8018 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8023 elf_symbol_type
*isym
, *osym
;
8025 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8026 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8029 isym
= elf_symbol_from (ibfd
, isymarg
);
8030 osym
= elf_symbol_from (obfd
, osymarg
);
8033 && isym
->internal_elf_sym
.st_shndx
!= 0
8035 && bfd_is_abs_section (isym
->symbol
.section
))
8039 shndx
= isym
->internal_elf_sym
.st_shndx
;
8040 if (shndx
== elf_onesymtab (ibfd
))
8041 shndx
= MAP_ONESYMTAB
;
8042 else if (shndx
== elf_dynsymtab (ibfd
))
8043 shndx
= MAP_DYNSYMTAB
;
8044 else if (shndx
== elf_strtab_sec (ibfd
))
8046 else if (shndx
== elf_shstrtab_sec (ibfd
))
8047 shndx
= MAP_SHSTRTAB
;
8048 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8049 shndx
= MAP_SYM_SHNDX
;
8050 osym
->internal_elf_sym
.st_shndx
= shndx
;
8056 /* Swap out the symbols. */
8059 swap_out_syms (bfd
*abfd
,
8060 struct elf_strtab_hash
**sttp
,
8063 const struct elf_backend_data
*bed
;
8064 unsigned int symcount
;
8066 struct elf_strtab_hash
*stt
;
8067 Elf_Internal_Shdr
*symtab_hdr
;
8068 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8069 Elf_Internal_Shdr
*symstrtab_hdr
;
8070 struct elf_sym_strtab
*symstrtab
;
8071 bfd_byte
*outbound_syms
;
8072 bfd_byte
*outbound_shndx
;
8073 unsigned long outbound_syms_index
;
8074 unsigned long outbound_shndx_index
;
8076 unsigned int num_locals
;
8078 bfd_boolean name_local_sections
;
8080 if (!elf_map_symbols (abfd
, &num_locals
))
8083 /* Dump out the symtabs. */
8084 stt
= _bfd_elf_strtab_init ();
8088 bed
= get_elf_backend_data (abfd
);
8089 symcount
= bfd_get_symcount (abfd
);
8090 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8091 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8092 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8093 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8094 symtab_hdr
->sh_info
= num_locals
+ 1;
8095 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8097 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8098 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8100 /* Allocate buffer to swap out the .strtab section. */
8101 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8102 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8104 bfd_set_error (bfd_error_no_memory
);
8105 _bfd_elf_strtab_free (stt
);
8109 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8110 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8113 bfd_set_error (bfd_error_no_memory
);
8116 _bfd_elf_strtab_free (stt
);
8119 symtab_hdr
->contents
= outbound_syms
;
8120 outbound_syms_index
= 0;
8122 outbound_shndx
= NULL
;
8123 outbound_shndx_index
= 0;
8125 if (elf_symtab_shndx_list (abfd
))
8127 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8128 if (symtab_shndx_hdr
->sh_name
!= 0)
8130 if (_bfd_mul_overflow (symcount
+ 1,
8131 sizeof (Elf_External_Sym_Shndx
), &amt
))
8133 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8134 if (outbound_shndx
== NULL
)
8137 symtab_shndx_hdr
->contents
= outbound_shndx
;
8138 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8139 symtab_shndx_hdr
->sh_size
= amt
;
8140 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8141 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8143 /* FIXME: What about any other headers in the list ? */
8146 /* Now generate the data (for "contents"). */
8148 /* Fill in zeroth symbol and swap it out. */
8149 Elf_Internal_Sym sym
;
8155 sym
.st_shndx
= SHN_UNDEF
;
8156 sym
.st_target_internal
= 0;
8157 symstrtab
[0].sym
= sym
;
8158 symstrtab
[0].dest_index
= outbound_syms_index
;
8159 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8160 outbound_syms_index
++;
8161 if (outbound_shndx
!= NULL
)
8162 outbound_shndx_index
++;
8166 = (bed
->elf_backend_name_local_section_symbols
8167 && bed
->elf_backend_name_local_section_symbols (abfd
));
8169 syms
= bfd_get_outsymbols (abfd
);
8170 for (idx
= 0; idx
< symcount
;)
8172 Elf_Internal_Sym sym
;
8173 bfd_vma value
= syms
[idx
]->value
;
8174 elf_symbol_type
*type_ptr
;
8175 flagword flags
= syms
[idx
]->flags
;
8178 if (!name_local_sections
8179 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8181 /* Local section symbols have no name. */
8182 sym
.st_name
= (unsigned long) -1;
8186 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8187 to get the final offset for st_name. */
8189 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8191 if (sym
.st_name
== (unsigned long) -1)
8195 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
8197 if ((flags
& BSF_SECTION_SYM
) == 0
8198 && bfd_is_com_section (syms
[idx
]->section
))
8200 /* ELF common symbols put the alignment into the `value' field,
8201 and the size into the `size' field. This is backwards from
8202 how BFD handles it, so reverse it here. */
8203 sym
.st_size
= value
;
8204 if (type_ptr
== NULL
8205 || type_ptr
->internal_elf_sym
.st_value
== 0)
8206 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8208 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8209 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8210 (abfd
, syms
[idx
]->section
);
8214 asection
*sec
= syms
[idx
]->section
;
8217 if (sec
->output_section
)
8219 value
+= sec
->output_offset
;
8220 sec
= sec
->output_section
;
8223 /* Don't add in the section vma for relocatable output. */
8224 if (! relocatable_p
)
8226 sym
.st_value
= value
;
8227 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8229 if (bfd_is_abs_section (sec
)
8231 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8233 /* This symbol is in a real ELF section which we did
8234 not create as a BFD section. Undo the mapping done
8235 by copy_private_symbol_data. */
8236 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8240 shndx
= elf_onesymtab (abfd
);
8243 shndx
= elf_dynsymtab (abfd
);
8246 shndx
= elf_strtab_sec (abfd
);
8249 shndx
= elf_shstrtab_sec (abfd
);
8252 if (elf_symtab_shndx_list (abfd
))
8253 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8260 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8262 if (bed
->symbol_section_index
)
8263 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8264 /* Otherwise just leave the index alone. */
8268 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8269 _bfd_error_handler (_("%pB: \
8270 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8279 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8281 if (shndx
== SHN_BAD
)
8285 /* Writing this would be a hell of a lot easier if
8286 we had some decent documentation on bfd, and
8287 knew what to expect of the library, and what to
8288 demand of applications. For example, it
8289 appears that `objcopy' might not set the
8290 section of a symbol to be a section that is
8291 actually in the output file. */
8292 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8294 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8295 if (shndx
== SHN_BAD
)
8297 /* xgettext:c-format */
8299 (_("unable to find equivalent output section"
8300 " for symbol '%s' from section '%s'"),
8301 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8303 bfd_set_error (bfd_error_invalid_operation
);
8309 sym
.st_shndx
= shndx
;
8312 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8314 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8315 type
= STT_GNU_IFUNC
;
8316 else if ((flags
& BSF_FUNCTION
) != 0)
8318 else if ((flags
& BSF_OBJECT
) != 0)
8320 else if ((flags
& BSF_RELC
) != 0)
8322 else if ((flags
& BSF_SRELC
) != 0)
8327 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8330 /* Processor-specific types. */
8331 if (type_ptr
!= NULL
8332 && bed
->elf_backend_get_symbol_type
)
8333 type
= ((*bed
->elf_backend_get_symbol_type
)
8334 (&type_ptr
->internal_elf_sym
, type
));
8336 if (flags
& BSF_SECTION_SYM
)
8338 if (flags
& BSF_GLOBAL
)
8339 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8341 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8343 else if (bfd_is_com_section (syms
[idx
]->section
))
8345 if (type
!= STT_TLS
)
8347 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8348 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8349 ? STT_COMMON
: STT_OBJECT
);
8351 type
= ((flags
& BSF_ELF_COMMON
) != 0
8352 ? STT_COMMON
: STT_OBJECT
);
8354 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8356 else if (bfd_is_und_section (syms
[idx
]->section
))
8357 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8361 else if (flags
& BSF_FILE
)
8362 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8365 int bind
= STB_LOCAL
;
8367 if (flags
& BSF_LOCAL
)
8369 else if (flags
& BSF_GNU_UNIQUE
)
8370 bind
= STB_GNU_UNIQUE
;
8371 else if (flags
& BSF_WEAK
)
8373 else if (flags
& BSF_GLOBAL
)
8376 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8379 if (type_ptr
!= NULL
)
8381 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8382 sym
.st_target_internal
8383 = type_ptr
->internal_elf_sym
.st_target_internal
;
8388 sym
.st_target_internal
= 0;
8392 symstrtab
[idx
].sym
= sym
;
8393 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8394 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8396 outbound_syms_index
++;
8397 if (outbound_shndx
!= NULL
)
8398 outbound_shndx_index
++;
8401 /* Finalize the .strtab section. */
8402 _bfd_elf_strtab_finalize (stt
);
8404 /* Swap out the .strtab section. */
8405 for (idx
= 0; idx
<= symcount
; idx
++)
8407 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8408 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8409 elfsym
->sym
.st_name
= 0;
8411 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8412 elfsym
->sym
.st_name
);
8413 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8415 + (elfsym
->dest_index
8416 * bed
->s
->sizeof_sym
)),
8418 + (elfsym
->destshndx_index
8419 * sizeof (Elf_External_Sym_Shndx
))));
8424 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8425 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8426 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8427 symstrtab_hdr
->sh_addr
= 0;
8428 symstrtab_hdr
->sh_entsize
= 0;
8429 symstrtab_hdr
->sh_link
= 0;
8430 symstrtab_hdr
->sh_info
= 0;
8431 symstrtab_hdr
->sh_addralign
= 1;
8436 /* Return the number of bytes required to hold the symtab vector.
8438 Note that we base it on the count plus 1, since we will null terminate
8439 the vector allocated based on this size. However, the ELF symbol table
8440 always has a dummy entry as symbol #0, so it ends up even. */
8443 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8445 bfd_size_type symcount
;
8447 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8449 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8450 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8452 bfd_set_error (bfd_error_file_too_big
);
8455 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8457 symtab_size
-= sizeof (asymbol
*);
8463 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8465 bfd_size_type symcount
;
8467 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8469 if (elf_dynsymtab (abfd
) == 0)
8471 bfd_set_error (bfd_error_invalid_operation
);
8475 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8476 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8478 bfd_set_error (bfd_error_file_too_big
);
8481 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8483 symtab_size
-= sizeof (asymbol
*);
8489 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8492 #if SIZEOF_LONG == SIZEOF_INT
8493 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8495 bfd_set_error (bfd_error_file_too_big
);
8499 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8502 /* Canonicalize the relocs. */
8505 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8512 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8514 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8517 tblptr
= section
->relocation
;
8518 for (i
= 0; i
< section
->reloc_count
; i
++)
8519 *relptr
++ = tblptr
++;
8523 return section
->reloc_count
;
8527 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8529 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8530 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8533 abfd
->symcount
= symcount
;
8538 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8539 asymbol
**allocation
)
8541 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8542 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8545 abfd
->dynsymcount
= symcount
;
8549 /* Return the size required for the dynamic reloc entries. Any loadable
8550 section that was actually installed in the BFD, and has type SHT_REL
8551 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8552 dynamic reloc section. */
8555 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8557 bfd_size_type count
;
8560 if (elf_dynsymtab (abfd
) == 0)
8562 bfd_set_error (bfd_error_invalid_operation
);
8567 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8568 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8569 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8570 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8572 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8573 if (count
> LONG_MAX
/ sizeof (arelent
*))
8575 bfd_set_error (bfd_error_file_too_big
);
8579 return count
* sizeof (arelent
*);
8582 /* Canonicalize the dynamic relocation entries. Note that we return the
8583 dynamic relocations as a single block, although they are actually
8584 associated with particular sections; the interface, which was
8585 designed for SunOS style shared libraries, expects that there is only
8586 one set of dynamic relocs. Any loadable section that was actually
8587 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8588 dynamic symbol table, is considered to be a dynamic reloc section. */
8591 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8595 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8599 if (elf_dynsymtab (abfd
) == 0)
8601 bfd_set_error (bfd_error_invalid_operation
);
8605 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8607 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8609 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8610 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8611 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8616 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8618 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8620 for (i
= 0; i
< count
; i
++)
8631 /* Read in the version information. */
8634 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8636 bfd_byte
*contents
= NULL
;
8637 unsigned int freeidx
= 0;
8640 if (elf_dynverref (abfd
) != 0)
8642 Elf_Internal_Shdr
*hdr
;
8643 Elf_External_Verneed
*everneed
;
8644 Elf_Internal_Verneed
*iverneed
;
8646 bfd_byte
*contents_end
;
8648 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8650 if (hdr
->sh_info
== 0
8651 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8653 error_return_bad_verref
:
8655 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8656 bfd_set_error (bfd_error_bad_value
);
8657 error_return_verref
:
8658 elf_tdata (abfd
)->verref
= NULL
;
8659 elf_tdata (abfd
)->cverrefs
= 0;
8663 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8664 goto error_return_verref
;
8665 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8666 if (contents
== NULL
)
8667 goto error_return_verref
;
8669 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8671 bfd_set_error (bfd_error_file_too_big
);
8672 goto error_return_verref
;
8674 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8675 if (elf_tdata (abfd
)->verref
== NULL
)
8676 goto error_return_verref
;
8678 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8679 == sizeof (Elf_External_Vernaux
));
8680 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8681 everneed
= (Elf_External_Verneed
*) contents
;
8682 iverneed
= elf_tdata (abfd
)->verref
;
8683 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8685 Elf_External_Vernaux
*evernaux
;
8686 Elf_Internal_Vernaux
*ivernaux
;
8689 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8691 iverneed
->vn_bfd
= abfd
;
8693 iverneed
->vn_filename
=
8694 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8696 if (iverneed
->vn_filename
== NULL
)
8697 goto error_return_bad_verref
;
8699 if (iverneed
->vn_cnt
== 0)
8700 iverneed
->vn_auxptr
= NULL
;
8703 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8704 sizeof (Elf_Internal_Vernaux
), &amt
))
8706 bfd_set_error (bfd_error_file_too_big
);
8707 goto error_return_verref
;
8709 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8710 bfd_alloc (abfd
, amt
);
8711 if (iverneed
->vn_auxptr
== NULL
)
8712 goto error_return_verref
;
8715 if (iverneed
->vn_aux
8716 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8717 goto error_return_bad_verref
;
8719 evernaux
= ((Elf_External_Vernaux
*)
8720 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8721 ivernaux
= iverneed
->vn_auxptr
;
8722 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8724 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8726 ivernaux
->vna_nodename
=
8727 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8728 ivernaux
->vna_name
);
8729 if (ivernaux
->vna_nodename
== NULL
)
8730 goto error_return_bad_verref
;
8732 if (ivernaux
->vna_other
> freeidx
)
8733 freeidx
= ivernaux
->vna_other
;
8735 ivernaux
->vna_nextptr
= NULL
;
8736 if (ivernaux
->vna_next
== 0)
8738 iverneed
->vn_cnt
= j
+ 1;
8741 if (j
+ 1 < iverneed
->vn_cnt
)
8742 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8744 if (ivernaux
->vna_next
8745 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8746 goto error_return_bad_verref
;
8748 evernaux
= ((Elf_External_Vernaux
*)
8749 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8752 iverneed
->vn_nextref
= NULL
;
8753 if (iverneed
->vn_next
== 0)
8755 if (i
+ 1 < hdr
->sh_info
)
8756 iverneed
->vn_nextref
= iverneed
+ 1;
8758 if (iverneed
->vn_next
8759 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8760 goto error_return_bad_verref
;
8762 everneed
= ((Elf_External_Verneed
*)
8763 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8765 elf_tdata (abfd
)->cverrefs
= i
;
8771 if (elf_dynverdef (abfd
) != 0)
8773 Elf_Internal_Shdr
*hdr
;
8774 Elf_External_Verdef
*everdef
;
8775 Elf_Internal_Verdef
*iverdef
;
8776 Elf_Internal_Verdef
*iverdefarr
;
8777 Elf_Internal_Verdef iverdefmem
;
8779 unsigned int maxidx
;
8780 bfd_byte
*contents_end_def
, *contents_end_aux
;
8782 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8784 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8786 error_return_bad_verdef
:
8788 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8789 bfd_set_error (bfd_error_bad_value
);
8790 error_return_verdef
:
8791 elf_tdata (abfd
)->verdef
= NULL
;
8792 elf_tdata (abfd
)->cverdefs
= 0;
8796 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8797 goto error_return_verdef
;
8798 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8799 if (contents
== NULL
)
8800 goto error_return_verdef
;
8802 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8803 >= sizeof (Elf_External_Verdaux
));
8804 contents_end_def
= contents
+ hdr
->sh_size
8805 - sizeof (Elf_External_Verdef
);
8806 contents_end_aux
= contents
+ hdr
->sh_size
8807 - sizeof (Elf_External_Verdaux
);
8809 /* We know the number of entries in the section but not the maximum
8810 index. Therefore we have to run through all entries and find
8812 everdef
= (Elf_External_Verdef
*) contents
;
8814 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8816 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8818 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8819 goto error_return_bad_verdef
;
8820 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8821 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8823 if (iverdefmem
.vd_next
== 0)
8826 if (iverdefmem
.vd_next
8827 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8828 goto error_return_bad_verdef
;
8830 everdef
= ((Elf_External_Verdef
*)
8831 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8834 if (default_imported_symver
)
8836 if (freeidx
> maxidx
)
8841 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8843 bfd_set_error (bfd_error_file_too_big
);
8844 goto error_return_verdef
;
8846 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8847 if (elf_tdata (abfd
)->verdef
== NULL
)
8848 goto error_return_verdef
;
8850 elf_tdata (abfd
)->cverdefs
= maxidx
;
8852 everdef
= (Elf_External_Verdef
*) contents
;
8853 iverdefarr
= elf_tdata (abfd
)->verdef
;
8854 for (i
= 0; i
< hdr
->sh_info
; i
++)
8856 Elf_External_Verdaux
*everdaux
;
8857 Elf_Internal_Verdaux
*iverdaux
;
8860 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8862 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8863 goto error_return_bad_verdef
;
8865 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8866 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8868 iverdef
->vd_bfd
= abfd
;
8870 if (iverdef
->vd_cnt
== 0)
8871 iverdef
->vd_auxptr
= NULL
;
8874 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
8875 sizeof (Elf_Internal_Verdaux
), &amt
))
8877 bfd_set_error (bfd_error_file_too_big
);
8878 goto error_return_verdef
;
8880 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8881 bfd_alloc (abfd
, amt
);
8882 if (iverdef
->vd_auxptr
== NULL
)
8883 goto error_return_verdef
;
8887 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8888 goto error_return_bad_verdef
;
8890 everdaux
= ((Elf_External_Verdaux
*)
8891 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8892 iverdaux
= iverdef
->vd_auxptr
;
8893 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8895 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8897 iverdaux
->vda_nodename
=
8898 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8899 iverdaux
->vda_name
);
8900 if (iverdaux
->vda_nodename
== NULL
)
8901 goto error_return_bad_verdef
;
8903 iverdaux
->vda_nextptr
= NULL
;
8904 if (iverdaux
->vda_next
== 0)
8906 iverdef
->vd_cnt
= j
+ 1;
8909 if (j
+ 1 < iverdef
->vd_cnt
)
8910 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8912 if (iverdaux
->vda_next
8913 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8914 goto error_return_bad_verdef
;
8916 everdaux
= ((Elf_External_Verdaux
*)
8917 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8920 iverdef
->vd_nodename
= NULL
;
8921 if (iverdef
->vd_cnt
)
8922 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8924 iverdef
->vd_nextdef
= NULL
;
8925 if (iverdef
->vd_next
== 0)
8927 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8928 iverdef
->vd_nextdef
= iverdef
+ 1;
8930 everdef
= ((Elf_External_Verdef
*)
8931 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8937 else if (default_imported_symver
)
8944 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8946 bfd_set_error (bfd_error_file_too_big
);
8949 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8950 if (elf_tdata (abfd
)->verdef
== NULL
)
8953 elf_tdata (abfd
)->cverdefs
= freeidx
;
8956 /* Create a default version based on the soname. */
8957 if (default_imported_symver
)
8959 Elf_Internal_Verdef
*iverdef
;
8960 Elf_Internal_Verdaux
*iverdaux
;
8962 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8964 iverdef
->vd_version
= VER_DEF_CURRENT
;
8965 iverdef
->vd_flags
= 0;
8966 iverdef
->vd_ndx
= freeidx
;
8967 iverdef
->vd_cnt
= 1;
8969 iverdef
->vd_bfd
= abfd
;
8971 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8972 if (iverdef
->vd_nodename
== NULL
)
8973 goto error_return_verdef
;
8974 iverdef
->vd_nextdef
= NULL
;
8975 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8976 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8977 if (iverdef
->vd_auxptr
== NULL
)
8978 goto error_return_verdef
;
8980 iverdaux
= iverdef
->vd_auxptr
;
8981 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8987 if (contents
!= NULL
)
8993 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8995 elf_symbol_type
*newsym
;
8997 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9000 newsym
->symbol
.the_bfd
= abfd
;
9001 return &newsym
->symbol
;
9005 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9009 bfd_symbol_info (symbol
, ret
);
9012 /* Return whether a symbol name implies a local symbol. Most targets
9013 use this function for the is_local_label_name entry point, but some
9017 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9020 /* Normal local symbols start with ``.L''. */
9021 if (name
[0] == '.' && name
[1] == 'L')
9024 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9025 DWARF debugging symbols starting with ``..''. */
9026 if (name
[0] == '.' && name
[1] == '.')
9029 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9030 emitting DWARF debugging output. I suspect this is actually a
9031 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9032 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9033 underscore to be emitted on some ELF targets). For ease of use,
9034 we treat such symbols as local. */
9035 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9038 /* Treat assembler generated fake symbols, dollar local labels and
9039 forward-backward labels (aka local labels) as locals.
9040 These labels have the form:
9042 L0^A.* (fake symbols)
9044 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9046 Versions which start with .L will have already been matched above,
9047 so we only need to match the rest. */
9048 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9050 bfd_boolean ret
= FALSE
;
9054 for (p
= name
+ 2; (c
= *p
); p
++)
9056 if (c
== 1 || c
== 2)
9058 if (c
== 1 && p
== name
+ 2)
9059 /* A fake symbol. */
9062 /* FIXME: We are being paranoid here and treating symbols like
9063 L0^Bfoo as if there were non-local, on the grounds that the
9064 assembler will never generate them. But can any symbol
9065 containing an ASCII value in the range 1-31 ever be anything
9066 other than some kind of local ? */
9083 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9084 asymbol
*symbol ATTRIBUTE_UNUSED
)
9091 _bfd_elf_set_arch_mach (bfd
*abfd
,
9092 enum bfd_architecture arch
,
9093 unsigned long machine
)
9095 /* If this isn't the right architecture for this backend, and this
9096 isn't the generic backend, fail. */
9097 if (arch
!= get_elf_backend_data (abfd
)->arch
9098 && arch
!= bfd_arch_unknown
9099 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9102 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9105 /* Find the nearest line to a particular section and offset,
9106 for error reporting. */
9109 _bfd_elf_find_nearest_line (bfd
*abfd
,
9113 const char **filename_ptr
,
9114 const char **functionname_ptr
,
9115 unsigned int *line_ptr
,
9116 unsigned int *discriminator_ptr
)
9120 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
9121 filename_ptr
, functionname_ptr
,
9122 line_ptr
, discriminator_ptr
,
9123 dwarf_debug_sections
,
9124 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9127 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9128 filename_ptr
, functionname_ptr
, line_ptr
))
9130 if (!*functionname_ptr
)
9131 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9132 *filename_ptr
? NULL
: filename_ptr
,
9137 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9138 &found
, filename_ptr
,
9139 functionname_ptr
, line_ptr
,
9140 &elf_tdata (abfd
)->line_info
))
9142 if (found
&& (*functionname_ptr
|| *line_ptr
))
9145 if (symbols
== NULL
)
9148 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9149 filename_ptr
, functionname_ptr
))
9156 /* Find the line for a symbol. */
9159 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9160 const char **filename_ptr
, unsigned int *line_ptr
)
9162 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9163 filename_ptr
, NULL
, line_ptr
, NULL
,
9164 dwarf_debug_sections
,
9165 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9168 /* After a call to bfd_find_nearest_line, successive calls to
9169 bfd_find_inliner_info can be used to get source information about
9170 each level of function inlining that terminated at the address
9171 passed to bfd_find_nearest_line. Currently this is only supported
9172 for DWARF2 with appropriate DWARF3 extensions. */
9175 _bfd_elf_find_inliner_info (bfd
*abfd
,
9176 const char **filename_ptr
,
9177 const char **functionname_ptr
,
9178 unsigned int *line_ptr
)
9181 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9182 functionname_ptr
, line_ptr
,
9183 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9188 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9190 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9191 int ret
= bed
->s
->sizeof_ehdr
;
9193 if (!bfd_link_relocatable (info
))
9195 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9197 if (phdr_size
== (bfd_size_type
) -1)
9199 struct elf_segment_map
*m
;
9202 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9203 phdr_size
+= bed
->s
->sizeof_phdr
;
9206 phdr_size
= get_program_header_size (abfd
, info
);
9209 elf_program_header_size (abfd
) = phdr_size
;
9217 _bfd_elf_set_section_contents (bfd
*abfd
,
9219 const void *location
,
9221 bfd_size_type count
)
9223 Elf_Internal_Shdr
*hdr
;
9226 if (! abfd
->output_has_begun
9227 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9233 hdr
= &elf_section_data (section
)->this_hdr
;
9234 if (hdr
->sh_offset
== (file_ptr
) -1)
9236 unsigned char *contents
;
9238 if (bfd_section_is_ctf (section
))
9239 /* Nothing to do with this section: the contents are generated
9243 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9246 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9248 bfd_set_error (bfd_error_invalid_operation
);
9252 if ((offset
+ count
) > hdr
->sh_size
)
9255 (_("%pB:%pA: error: attempting to write over the end of the section"),
9258 bfd_set_error (bfd_error_invalid_operation
);
9262 contents
= hdr
->contents
;
9263 if (contents
== NULL
)
9266 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9269 bfd_set_error (bfd_error_invalid_operation
);
9273 memcpy (contents
+ offset
, location
, count
);
9277 pos
= hdr
->sh_offset
+ offset
;
9278 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9279 || bfd_bwrite (location
, count
, abfd
) != count
)
9286 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9287 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9288 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9294 /* Try to convert a non-ELF reloc into an ELF one. */
9297 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9299 /* Check whether we really have an ELF howto. */
9301 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9303 bfd_reloc_code_real_type code
;
9304 reloc_howto_type
*howto
;
9306 /* Alien reloc: Try to determine its type to replace it with an
9307 equivalent ELF reloc. */
9309 if (areloc
->howto
->pc_relative
)
9311 switch (areloc
->howto
->bitsize
)
9314 code
= BFD_RELOC_8_PCREL
;
9317 code
= BFD_RELOC_12_PCREL
;
9320 code
= BFD_RELOC_16_PCREL
;
9323 code
= BFD_RELOC_24_PCREL
;
9326 code
= BFD_RELOC_32_PCREL
;
9329 code
= BFD_RELOC_64_PCREL
;
9335 howto
= bfd_reloc_type_lookup (abfd
, code
);
9337 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9339 if (howto
->pcrel_offset
)
9340 areloc
->addend
+= areloc
->address
;
9342 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9347 switch (areloc
->howto
->bitsize
)
9353 code
= BFD_RELOC_14
;
9356 code
= BFD_RELOC_16
;
9359 code
= BFD_RELOC_26
;
9362 code
= BFD_RELOC_32
;
9365 code
= BFD_RELOC_64
;
9371 howto
= bfd_reloc_type_lookup (abfd
, code
);
9375 areloc
->howto
= howto
;
9383 /* xgettext:c-format */
9384 _bfd_error_handler (_("%pB: %s unsupported"),
9385 abfd
, areloc
->howto
->name
);
9386 bfd_set_error (bfd_error_sorry
);
9391 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9393 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9394 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9396 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9397 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9398 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9401 return _bfd_generic_close_and_cleanup (abfd
);
9404 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9405 in the relocation's offset. Thus we cannot allow any sort of sanity
9406 range-checking to interfere. There is nothing else to do in processing
9409 bfd_reloc_status_type
9410 _bfd_elf_rel_vtable_reloc_fn
9411 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9412 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9413 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9414 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9416 return bfd_reloc_ok
;
9419 /* Elf core file support. Much of this only works on native
9420 toolchains, since we rely on knowing the
9421 machine-dependent procfs structure in order to pick
9422 out details about the corefile. */
9424 #ifdef HAVE_SYS_PROCFS_H
9425 /* Needed for new procfs interface on sparc-solaris. */
9426 # define _STRUCTURED_PROC 1
9427 # include <sys/procfs.h>
9430 /* Return a PID that identifies a "thread" for threaded cores, or the
9431 PID of the main process for non-threaded cores. */
9434 elfcore_make_pid (bfd
*abfd
)
9438 pid
= elf_tdata (abfd
)->core
->lwpid
;
9440 pid
= elf_tdata (abfd
)->core
->pid
;
9445 /* If there isn't a section called NAME, make one, using
9446 data from SECT. Note, this function will generate a
9447 reference to NAME, so you shouldn't deallocate or
9451 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9455 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9458 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9462 sect2
->size
= sect
->size
;
9463 sect2
->filepos
= sect
->filepos
;
9464 sect2
->alignment_power
= sect
->alignment_power
;
9468 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9469 actually creates up to two pseudosections:
9470 - For the single-threaded case, a section named NAME, unless
9471 such a section already exists.
9472 - For the multi-threaded case, a section named "NAME/PID", where
9473 PID is elfcore_make_pid (abfd).
9474 Both pseudosections have identical contents. */
9476 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9482 char *threaded_name
;
9486 /* Build the section name. */
9488 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9489 len
= strlen (buf
) + 1;
9490 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9491 if (threaded_name
== NULL
)
9493 memcpy (threaded_name
, buf
, len
);
9495 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9500 sect
->filepos
= filepos
;
9501 sect
->alignment_power
= 2;
9503 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9507 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9510 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9516 sect
->size
= note
->descsz
- offs
;
9517 sect
->filepos
= note
->descpos
+ offs
;
9518 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9523 /* prstatus_t exists on:
9525 linux 2.[01] + glibc
9529 #if defined (HAVE_PRSTATUS_T)
9532 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9537 if (note
->descsz
== sizeof (prstatus_t
))
9541 size
= sizeof (prstat
.pr_reg
);
9542 offset
= offsetof (prstatus_t
, pr_reg
);
9543 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9545 /* Do not overwrite the core signal if it
9546 has already been set by another thread. */
9547 if (elf_tdata (abfd
)->core
->signal
== 0)
9548 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9549 if (elf_tdata (abfd
)->core
->pid
== 0)
9550 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9552 /* pr_who exists on:
9555 pr_who doesn't exist on:
9558 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9559 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9561 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9564 #if defined (HAVE_PRSTATUS32_T)
9565 else if (note
->descsz
== sizeof (prstatus32_t
))
9567 /* 64-bit host, 32-bit corefile */
9568 prstatus32_t prstat
;
9570 size
= sizeof (prstat
.pr_reg
);
9571 offset
= offsetof (prstatus32_t
, pr_reg
);
9572 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9574 /* Do not overwrite the core signal if it
9575 has already been set by another thread. */
9576 if (elf_tdata (abfd
)->core
->signal
== 0)
9577 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9578 if (elf_tdata (abfd
)->core
->pid
== 0)
9579 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9581 /* pr_who exists on:
9584 pr_who doesn't exist on:
9587 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9588 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9590 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9593 #endif /* HAVE_PRSTATUS32_T */
9596 /* Fail - we don't know how to handle any other
9597 note size (ie. data object type). */
9601 /* Make a ".reg/999" section and a ".reg" section. */
9602 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9603 size
, note
->descpos
+ offset
);
9605 #endif /* defined (HAVE_PRSTATUS_T) */
9607 /* Create a pseudosection containing the exact contents of NOTE. */
9609 elfcore_make_note_pseudosection (bfd
*abfd
,
9611 Elf_Internal_Note
*note
)
9613 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9614 note
->descsz
, note
->descpos
);
9617 /* There isn't a consistent prfpregset_t across platforms,
9618 but it doesn't matter, because we don't have to pick this
9619 data structure apart. */
9622 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9624 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9627 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9628 type of NT_PRXFPREG. Just include the whole note's contents
9632 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9634 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9637 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9638 with a note type of NT_X86_XSTATE. Just include the whole note's
9639 contents literally. */
9642 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9644 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9648 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9650 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9654 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9656 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9660 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9662 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9666 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9668 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9672 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9674 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9678 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9680 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9684 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9686 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9690 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9692 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9696 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9698 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9702 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9704 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9708 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9710 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9714 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9716 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9720 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9722 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9726 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9728 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9732 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9734 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9738 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9740 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9744 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9746 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9750 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9752 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9756 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9758 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9762 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9764 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9768 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9770 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9774 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9776 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9780 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9782 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9786 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9788 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9792 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9794 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9798 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9800 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9804 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9806 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9810 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9812 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9816 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9818 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9822 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9824 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9828 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9830 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9834 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9836 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9840 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9842 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9846 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9848 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9851 #if defined (HAVE_PRPSINFO_T)
9852 typedef prpsinfo_t elfcore_psinfo_t
;
9853 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9854 typedef prpsinfo32_t elfcore_psinfo32_t
;
9858 #if defined (HAVE_PSINFO_T)
9859 typedef psinfo_t elfcore_psinfo_t
;
9860 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9861 typedef psinfo32_t elfcore_psinfo32_t
;
9865 /* return a malloc'ed copy of a string at START which is at
9866 most MAX bytes long, possibly without a terminating '\0'.
9867 the copy will always have a terminating '\0'. */
9870 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9873 char *end
= (char *) memchr (start
, '\0', max
);
9881 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9885 memcpy (dups
, start
, len
);
9891 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9893 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9895 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9897 elfcore_psinfo_t psinfo
;
9899 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9901 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9902 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9904 elf_tdata (abfd
)->core
->program
9905 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9906 sizeof (psinfo
.pr_fname
));
9908 elf_tdata (abfd
)->core
->command
9909 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9910 sizeof (psinfo
.pr_psargs
));
9912 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9913 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9915 /* 64-bit host, 32-bit corefile */
9916 elfcore_psinfo32_t psinfo
;
9918 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9920 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9921 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9923 elf_tdata (abfd
)->core
->program
9924 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9925 sizeof (psinfo
.pr_fname
));
9927 elf_tdata (abfd
)->core
->command
9928 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9929 sizeof (psinfo
.pr_psargs
));
9935 /* Fail - we don't know how to handle any other
9936 note size (ie. data object type). */
9940 /* Note that for some reason, a spurious space is tacked
9941 onto the end of the args in some (at least one anyway)
9942 implementations, so strip it off if it exists. */
9945 char *command
= elf_tdata (abfd
)->core
->command
;
9946 int n
= strlen (command
);
9948 if (0 < n
&& command
[n
- 1] == ' ')
9949 command
[n
- 1] = '\0';
9954 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9956 #if defined (HAVE_PSTATUS_T)
9958 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9960 if (note
->descsz
== sizeof (pstatus_t
)
9961 #if defined (HAVE_PXSTATUS_T)
9962 || note
->descsz
== sizeof (pxstatus_t
)
9968 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9970 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9972 #if defined (HAVE_PSTATUS32_T)
9973 else if (note
->descsz
== sizeof (pstatus32_t
))
9975 /* 64-bit host, 32-bit corefile */
9978 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9980 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9983 /* Could grab some more details from the "representative"
9984 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9985 NT_LWPSTATUS note, presumably. */
9989 #endif /* defined (HAVE_PSTATUS_T) */
9991 #if defined (HAVE_LWPSTATUS_T)
9993 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9995 lwpstatus_t lwpstat
;
10001 if (note
->descsz
!= sizeof (lwpstat
)
10002 #if defined (HAVE_LWPXSTATUS_T)
10003 && note
->descsz
!= sizeof (lwpxstatus_t
)
10008 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10010 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10011 /* Do not overwrite the core signal if it has already been set by
10013 if (elf_tdata (abfd
)->core
->signal
== 0)
10014 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10016 /* Make a ".reg/999" section. */
10018 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10019 len
= strlen (buf
) + 1;
10020 name
= bfd_alloc (abfd
, len
);
10023 memcpy (name
, buf
, len
);
10025 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10029 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10030 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10031 sect
->filepos
= note
->descpos
10032 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10035 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10036 sect
->size
= sizeof (lwpstat
.pr_reg
);
10037 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10040 sect
->alignment_power
= 2;
10042 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10045 /* Make a ".reg2/999" section */
10047 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10048 len
= strlen (buf
) + 1;
10049 name
= bfd_alloc (abfd
, len
);
10052 memcpy (name
, buf
, len
);
10054 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10058 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10059 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10060 sect
->filepos
= note
->descpos
10061 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10064 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10065 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10066 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10069 sect
->alignment_power
= 2;
10071 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10073 #endif /* defined (HAVE_LWPSTATUS_T) */
10076 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10083 int is_active_thread
;
10086 if (note
->descsz
< 728)
10089 if (! CONST_STRNEQ (note
->namedata
, "win32"))
10092 type
= bfd_get_32 (abfd
, note
->descdata
);
10096 case 1 /* NOTE_INFO_PROCESS */:
10097 /* FIXME: need to add ->core->command. */
10098 /* process_info.pid */
10099 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10100 /* process_info.signal */
10101 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10104 case 2 /* NOTE_INFO_THREAD */:
10105 /* Make a ".reg/999" section. */
10106 /* thread_info.tid */
10107 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
10109 len
= strlen (buf
) + 1;
10110 name
= (char *) bfd_alloc (abfd
, len
);
10114 memcpy (name
, buf
, len
);
10116 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10120 /* sizeof (thread_info.thread_context) */
10122 /* offsetof (thread_info.thread_context) */
10123 sect
->filepos
= note
->descpos
+ 12;
10124 sect
->alignment_power
= 2;
10126 /* thread_info.is_active_thread */
10127 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10129 if (is_active_thread
)
10130 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10134 case 3 /* NOTE_INFO_MODULE */:
10135 /* Make a ".module/xxxxxxxx" section. */
10136 /* module_info.base_address */
10137 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10138 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10140 len
= strlen (buf
) + 1;
10141 name
= (char *) bfd_alloc (abfd
, len
);
10145 memcpy (name
, buf
, len
);
10147 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10152 sect
->size
= note
->descsz
;
10153 sect
->filepos
= note
->descpos
;
10154 sect
->alignment_power
= 2;
10165 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10167 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10169 switch (note
->type
)
10175 if (bed
->elf_backend_grok_prstatus
)
10176 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10178 #if defined (HAVE_PRSTATUS_T)
10179 return elfcore_grok_prstatus (abfd
, note
);
10184 #if defined (HAVE_PSTATUS_T)
10186 return elfcore_grok_pstatus (abfd
, note
);
10189 #if defined (HAVE_LWPSTATUS_T)
10191 return elfcore_grok_lwpstatus (abfd
, note
);
10194 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10195 return elfcore_grok_prfpreg (abfd
, note
);
10197 case NT_WIN32PSTATUS
:
10198 return elfcore_grok_win32pstatus (abfd
, note
);
10200 case NT_PRXFPREG
: /* Linux SSE extension */
10201 if (note
->namesz
== 6
10202 && strcmp (note
->namedata
, "LINUX") == 0)
10203 return elfcore_grok_prxfpreg (abfd
, note
);
10207 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10208 if (note
->namesz
== 6
10209 && strcmp (note
->namedata
, "LINUX") == 0)
10210 return elfcore_grok_xstatereg (abfd
, note
);
10215 if (note
->namesz
== 6
10216 && strcmp (note
->namedata
, "LINUX") == 0)
10217 return elfcore_grok_ppc_vmx (abfd
, note
);
10222 if (note
->namesz
== 6
10223 && strcmp (note
->namedata
, "LINUX") == 0)
10224 return elfcore_grok_ppc_vsx (abfd
, note
);
10229 if (note
->namesz
== 6
10230 && strcmp (note
->namedata
, "LINUX") == 0)
10231 return elfcore_grok_ppc_tar (abfd
, note
);
10236 if (note
->namesz
== 6
10237 && strcmp (note
->namedata
, "LINUX") == 0)
10238 return elfcore_grok_ppc_ppr (abfd
, note
);
10243 if (note
->namesz
== 6
10244 && strcmp (note
->namedata
, "LINUX") == 0)
10245 return elfcore_grok_ppc_dscr (abfd
, note
);
10250 if (note
->namesz
== 6
10251 && strcmp (note
->namedata
, "LINUX") == 0)
10252 return elfcore_grok_ppc_ebb (abfd
, note
);
10257 if (note
->namesz
== 6
10258 && strcmp (note
->namedata
, "LINUX") == 0)
10259 return elfcore_grok_ppc_pmu (abfd
, note
);
10263 case NT_PPC_TM_CGPR
:
10264 if (note
->namesz
== 6
10265 && strcmp (note
->namedata
, "LINUX") == 0)
10266 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10270 case NT_PPC_TM_CFPR
:
10271 if (note
->namesz
== 6
10272 && strcmp (note
->namedata
, "LINUX") == 0)
10273 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10277 case NT_PPC_TM_CVMX
:
10278 if (note
->namesz
== 6
10279 && strcmp (note
->namedata
, "LINUX") == 0)
10280 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10284 case NT_PPC_TM_CVSX
:
10285 if (note
->namesz
== 6
10286 && strcmp (note
->namedata
, "LINUX") == 0)
10287 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10291 case NT_PPC_TM_SPR
:
10292 if (note
->namesz
== 6
10293 && strcmp (note
->namedata
, "LINUX") == 0)
10294 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10298 case NT_PPC_TM_CTAR
:
10299 if (note
->namesz
== 6
10300 && strcmp (note
->namedata
, "LINUX") == 0)
10301 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10305 case NT_PPC_TM_CPPR
:
10306 if (note
->namesz
== 6
10307 && strcmp (note
->namedata
, "LINUX") == 0)
10308 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10312 case NT_PPC_TM_CDSCR
:
10313 if (note
->namesz
== 6
10314 && strcmp (note
->namedata
, "LINUX") == 0)
10315 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10319 case NT_S390_HIGH_GPRS
:
10320 if (note
->namesz
== 6
10321 && strcmp (note
->namedata
, "LINUX") == 0)
10322 return elfcore_grok_s390_high_gprs (abfd
, note
);
10326 case NT_S390_TIMER
:
10327 if (note
->namesz
== 6
10328 && strcmp (note
->namedata
, "LINUX") == 0)
10329 return elfcore_grok_s390_timer (abfd
, note
);
10333 case NT_S390_TODCMP
:
10334 if (note
->namesz
== 6
10335 && strcmp (note
->namedata
, "LINUX") == 0)
10336 return elfcore_grok_s390_todcmp (abfd
, note
);
10340 case NT_S390_TODPREG
:
10341 if (note
->namesz
== 6
10342 && strcmp (note
->namedata
, "LINUX") == 0)
10343 return elfcore_grok_s390_todpreg (abfd
, note
);
10348 if (note
->namesz
== 6
10349 && strcmp (note
->namedata
, "LINUX") == 0)
10350 return elfcore_grok_s390_ctrs (abfd
, note
);
10354 case NT_S390_PREFIX
:
10355 if (note
->namesz
== 6
10356 && strcmp (note
->namedata
, "LINUX") == 0)
10357 return elfcore_grok_s390_prefix (abfd
, note
);
10361 case NT_S390_LAST_BREAK
:
10362 if (note
->namesz
== 6
10363 && strcmp (note
->namedata
, "LINUX") == 0)
10364 return elfcore_grok_s390_last_break (abfd
, note
);
10368 case NT_S390_SYSTEM_CALL
:
10369 if (note
->namesz
== 6
10370 && strcmp (note
->namedata
, "LINUX") == 0)
10371 return elfcore_grok_s390_system_call (abfd
, note
);
10376 if (note
->namesz
== 6
10377 && strcmp (note
->namedata
, "LINUX") == 0)
10378 return elfcore_grok_s390_tdb (abfd
, note
);
10382 case NT_S390_VXRS_LOW
:
10383 if (note
->namesz
== 6
10384 && strcmp (note
->namedata
, "LINUX") == 0)
10385 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10389 case NT_S390_VXRS_HIGH
:
10390 if (note
->namesz
== 6
10391 && strcmp (note
->namedata
, "LINUX") == 0)
10392 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10396 case NT_S390_GS_CB
:
10397 if (note
->namesz
== 6
10398 && strcmp (note
->namedata
, "LINUX") == 0)
10399 return elfcore_grok_s390_gs_cb (abfd
, note
);
10403 case NT_S390_GS_BC
:
10404 if (note
->namesz
== 6
10405 && strcmp (note
->namedata
, "LINUX") == 0)
10406 return elfcore_grok_s390_gs_bc (abfd
, note
);
10411 if (note
->namesz
== 6
10412 && strcmp (note
->namedata
, "LINUX") == 0)
10413 return elfcore_grok_arm_vfp (abfd
, note
);
10418 if (note
->namesz
== 6
10419 && strcmp (note
->namedata
, "LINUX") == 0)
10420 return elfcore_grok_aarch_tls (abfd
, note
);
10424 case NT_ARM_HW_BREAK
:
10425 if (note
->namesz
== 6
10426 && strcmp (note
->namedata
, "LINUX") == 0)
10427 return elfcore_grok_aarch_hw_break (abfd
, note
);
10431 case NT_ARM_HW_WATCH
:
10432 if (note
->namesz
== 6
10433 && strcmp (note
->namedata
, "LINUX") == 0)
10434 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10439 if (note
->namesz
== 6
10440 && strcmp (note
->namedata
, "LINUX") == 0)
10441 return elfcore_grok_aarch_sve (abfd
, note
);
10445 case NT_ARM_PAC_MASK
:
10446 if (note
->namesz
== 6
10447 && strcmp (note
->namedata
, "LINUX") == 0)
10448 return elfcore_grok_aarch_pauth (abfd
, note
);
10454 if (bed
->elf_backend_grok_psinfo
)
10455 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10457 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10458 return elfcore_grok_psinfo (abfd
, note
);
10464 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10467 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10471 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10478 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10480 struct bfd_build_id
* build_id
;
10482 if (note
->descsz
== 0)
10485 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10486 if (build_id
== NULL
)
10489 build_id
->size
= note
->descsz
;
10490 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10491 abfd
->build_id
= build_id
;
10497 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10499 switch (note
->type
)
10504 case NT_GNU_PROPERTY_TYPE_0
:
10505 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10507 case NT_GNU_BUILD_ID
:
10508 return elfobj_grok_gnu_build_id (abfd
, note
);
10513 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10515 struct sdt_note
*cur
=
10516 (struct sdt_note
*) bfd_alloc (abfd
,
10517 sizeof (struct sdt_note
) + note
->descsz
);
10519 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10520 cur
->size
= (bfd_size_type
) note
->descsz
;
10521 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10523 elf_tdata (abfd
)->sdt_note_head
= cur
;
10529 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10531 switch (note
->type
)
10534 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10542 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10546 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10549 if (note
->descsz
< 108)
10554 if (note
->descsz
< 120)
10562 /* Check for version 1 in pr_version. */
10563 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10568 /* Skip over pr_psinfosz. */
10569 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10573 offset
+= 4; /* Padding before pr_psinfosz. */
10577 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10578 elf_tdata (abfd
)->core
->program
10579 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10582 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10583 elf_tdata (abfd
)->core
->command
10584 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10587 /* Padding before pr_pid. */
10590 /* The pr_pid field was added in version "1a". */
10591 if (note
->descsz
< offset
+ 4)
10594 elf_tdata (abfd
)->core
->pid
10595 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10601 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10607 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10608 Also compute minimum size of this note. */
10609 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10613 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10617 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10618 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10625 if (note
->descsz
< min_size
)
10628 /* Check for version 1 in pr_version. */
10629 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10632 /* Extract size of pr_reg from pr_gregsetsz. */
10633 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10634 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10636 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10641 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10645 /* Skip over pr_osreldate. */
10648 /* Read signal from pr_cursig. */
10649 if (elf_tdata (abfd
)->core
->signal
== 0)
10650 elf_tdata (abfd
)->core
->signal
10651 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10654 /* Read TID from pr_pid. */
10655 elf_tdata (abfd
)->core
->lwpid
10656 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10659 /* Padding before pr_reg. */
10660 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10663 /* Make sure that there is enough data remaining in the note. */
10664 if ((note
->descsz
- offset
) < size
)
10667 /* Make a ".reg/999" section and a ".reg" section. */
10668 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10669 size
, note
->descpos
+ offset
);
10673 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10675 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10677 switch (note
->type
)
10680 if (bed
->elf_backend_grok_freebsd_prstatus
)
10681 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10683 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10686 return elfcore_grok_prfpreg (abfd
, note
);
10689 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10691 case NT_FREEBSD_THRMISC
:
10692 if (note
->namesz
== 8)
10693 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10697 case NT_FREEBSD_PROCSTAT_PROC
:
10698 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10701 case NT_FREEBSD_PROCSTAT_FILES
:
10702 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10705 case NT_FREEBSD_PROCSTAT_VMMAP
:
10706 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10709 case NT_FREEBSD_PROCSTAT_AUXV
:
10710 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10712 case NT_X86_XSTATE
:
10713 if (note
->namesz
== 8)
10714 return elfcore_grok_xstatereg (abfd
, note
);
10718 case NT_FREEBSD_PTLWPINFO
:
10719 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10723 return elfcore_grok_arm_vfp (abfd
, note
);
10731 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10735 cp
= strchr (note
->namedata
, '@');
10738 *lwpidp
= atoi(cp
+ 1);
10745 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10747 if (note
->descsz
<= 0x7c + 31)
10750 /* Signal number at offset 0x08. */
10751 elf_tdata (abfd
)->core
->signal
10752 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10754 /* Process ID at offset 0x50. */
10755 elf_tdata (abfd
)->core
->pid
10756 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10758 /* Command name at 0x7c (max 32 bytes, including nul). */
10759 elf_tdata (abfd
)->core
->command
10760 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10762 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10767 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10771 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10772 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10774 switch (note
->type
)
10776 case NT_NETBSDCORE_PROCINFO
:
10777 /* NetBSD-specific core "procinfo". Note that we expect to
10778 find this note before any of the others, which is fine,
10779 since the kernel writes this note out first when it
10780 creates a core file. */
10781 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10782 #ifdef NT_NETBSDCORE_AUXV
10783 case NT_NETBSDCORE_AUXV
:
10784 /* NetBSD-specific Elf Auxiliary Vector data. */
10785 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10787 #ifdef NT_NETBSDCORE_LWPSTATUS
10788 case NT_NETBSDCORE_LWPSTATUS
:
10789 return elfcore_make_note_pseudosection (abfd
,
10790 ".note.netbsdcore.lwpstatus",
10797 /* As of March 2020 there are no other machine-independent notes
10798 defined for NetBSD core files. If the note type is less
10799 than the start of the machine-dependent note types, we don't
10802 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10806 switch (bfd_get_arch (abfd
))
10808 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10809 PT_GETFPREGS == mach+2. */
10811 case bfd_arch_aarch64
:
10812 case bfd_arch_alpha
:
10813 case bfd_arch_sparc
:
10814 switch (note
->type
)
10816 case NT_NETBSDCORE_FIRSTMACH
+0:
10817 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10819 case NT_NETBSDCORE_FIRSTMACH
+2:
10820 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10826 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10827 There's also old PT___GETREGS40 == mach + 1 for old reg
10828 structure which lacks GBR. */
10831 switch (note
->type
)
10833 case NT_NETBSDCORE_FIRSTMACH
+3:
10834 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10836 case NT_NETBSDCORE_FIRSTMACH
+5:
10837 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10843 /* On all other arch's, PT_GETREGS == mach+1 and
10844 PT_GETFPREGS == mach+3. */
10847 switch (note
->type
)
10849 case NT_NETBSDCORE_FIRSTMACH
+1:
10850 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10852 case NT_NETBSDCORE_FIRSTMACH
+3:
10853 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10863 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10865 if (note
->descsz
<= 0x48 + 31)
10868 /* Signal number at offset 0x08. */
10869 elf_tdata (abfd
)->core
->signal
10870 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10872 /* Process ID at offset 0x20. */
10873 elf_tdata (abfd
)->core
->pid
10874 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10876 /* Command name at 0x48 (max 32 bytes, including nul). */
10877 elf_tdata (abfd
)->core
->command
10878 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10884 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10886 if (note
->type
== NT_OPENBSD_PROCINFO
)
10887 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10889 if (note
->type
== NT_OPENBSD_REGS
)
10890 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10892 if (note
->type
== NT_OPENBSD_FPREGS
)
10893 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10895 if (note
->type
== NT_OPENBSD_XFPREGS
)
10896 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10898 if (note
->type
== NT_OPENBSD_AUXV
)
10899 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10901 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10903 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10908 sect
->size
= note
->descsz
;
10909 sect
->filepos
= note
->descpos
;
10910 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10919 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10921 void *ddata
= note
->descdata
;
10928 if (note
->descsz
< 16)
10931 /* nto_procfs_status 'pid' field is at offset 0. */
10932 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10934 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10935 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10937 /* nto_procfs_status 'flags' field is at offset 8. */
10938 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10940 /* nto_procfs_status 'what' field is at offset 14. */
10941 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10943 elf_tdata (abfd
)->core
->signal
= sig
;
10944 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10947 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10948 do not come from signals so we make sure we set the current
10949 thread just in case. */
10950 if (flags
& 0x00000080)
10951 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10953 /* Make a ".qnx_core_status/%d" section. */
10954 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10956 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10959 strcpy (name
, buf
);
10961 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10965 sect
->size
= note
->descsz
;
10966 sect
->filepos
= note
->descpos
;
10967 sect
->alignment_power
= 2;
10969 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10973 elfcore_grok_nto_regs (bfd
*abfd
,
10974 Elf_Internal_Note
*note
,
10982 /* Make a "(base)/%d" section. */
10983 sprintf (buf
, "%s/%ld", base
, tid
);
10985 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10988 strcpy (name
, buf
);
10990 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10994 sect
->size
= note
->descsz
;
10995 sect
->filepos
= note
->descpos
;
10996 sect
->alignment_power
= 2;
10998 /* This is the current thread. */
10999 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
11000 return elfcore_maybe_make_sect (abfd
, base
, sect
);
11005 #define BFD_QNT_CORE_INFO 7
11006 #define BFD_QNT_CORE_STATUS 8
11007 #define BFD_QNT_CORE_GREG 9
11008 #define BFD_QNT_CORE_FPREG 10
11011 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11013 /* Every GREG section has a STATUS section before it. Store the
11014 tid from the previous call to pass down to the next gregs
11016 static long tid
= 1;
11018 switch (note
->type
)
11020 case BFD_QNT_CORE_INFO
:
11021 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
11022 case BFD_QNT_CORE_STATUS
:
11023 return elfcore_grok_nto_status (abfd
, note
, &tid
);
11024 case BFD_QNT_CORE_GREG
:
11025 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
11026 case BFD_QNT_CORE_FPREG
:
11027 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
11034 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11040 /* Use note name as section name. */
11041 len
= note
->namesz
;
11042 name
= (char *) bfd_alloc (abfd
, len
);
11045 memcpy (name
, note
->namedata
, len
);
11046 name
[len
- 1] = '\0';
11048 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11052 sect
->size
= note
->descsz
;
11053 sect
->filepos
= note
->descpos
;
11054 sect
->alignment_power
= 1;
11059 /* Function: elfcore_write_note
11062 buffer to hold note, and current size of buffer
11066 size of data for note
11068 Writes note to end of buffer. ELF64 notes are written exactly as
11069 for ELF32, despite the current (as of 2006) ELF gabi specifying
11070 that they ought to have 8-byte namesz and descsz field, and have
11071 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11074 Pointer to realloc'd buffer, *BUFSIZ updated. */
11077 elfcore_write_note (bfd
*abfd
,
11085 Elf_External_Note
*xnp
;
11092 namesz
= strlen (name
) + 1;
11094 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11096 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11099 dest
= buf
+ *bufsiz
;
11100 *bufsiz
+= newspace
;
11101 xnp
= (Elf_External_Note
*) dest
;
11102 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11103 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11104 H_PUT_32 (abfd
, type
, xnp
->type
);
11108 memcpy (dest
, name
, namesz
);
11116 memcpy (dest
, input
, size
);
11126 /* gcc-8 warns (*) on all the strncpy calls in this function about
11127 possible string truncation. The "truncation" is not a bug. We
11128 have an external representation of structs with fields that are not
11129 necessarily NULL terminated and corresponding internal
11130 representation fields that are one larger so that they can always
11131 be NULL terminated.
11132 gcc versions between 4.2 and 4.6 do not allow pragma control of
11133 diagnostics inside functions, giving a hard error if you try to use
11134 the finer control available with later versions.
11135 gcc prior to 4.2 warns about diagnostic push and pop.
11136 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11137 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11138 (*) Depending on your system header files! */
11139 #if GCC_VERSION >= 8000
11140 # pragma GCC diagnostic push
11141 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11144 elfcore_write_prpsinfo (bfd
*abfd
,
11148 const char *psargs
)
11150 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11152 if (bed
->elf_backend_write_core_note
!= NULL
)
11155 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11156 NT_PRPSINFO
, fname
, psargs
);
11161 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11162 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11163 if (bed
->s
->elfclass
== ELFCLASS32
)
11165 # if defined (HAVE_PSINFO32_T)
11167 int note_type
= NT_PSINFO
;
11170 int note_type
= NT_PRPSINFO
;
11173 memset (&data
, 0, sizeof (data
));
11174 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11175 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11176 return elfcore_write_note (abfd
, buf
, bufsiz
,
11177 "CORE", note_type
, &data
, sizeof (data
));
11182 # if defined (HAVE_PSINFO_T)
11184 int note_type
= NT_PSINFO
;
11187 int note_type
= NT_PRPSINFO
;
11190 memset (&data
, 0, sizeof (data
));
11191 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11192 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11193 return elfcore_write_note (abfd
, buf
, bufsiz
,
11194 "CORE", note_type
, &data
, sizeof (data
));
11196 #endif /* PSINFO_T or PRPSINFO_T */
11201 #if GCC_VERSION >= 8000
11202 # pragma GCC diagnostic pop
11206 elfcore_write_linux_prpsinfo32
11207 (bfd
*abfd
, char *buf
, int *bufsiz
,
11208 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11210 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11212 struct elf_external_linux_prpsinfo32_ugid16 data
;
11214 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11215 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11216 &data
, sizeof (data
));
11220 struct elf_external_linux_prpsinfo32_ugid32 data
;
11222 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11223 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11224 &data
, sizeof (data
));
11229 elfcore_write_linux_prpsinfo64
11230 (bfd
*abfd
, char *buf
, int *bufsiz
,
11231 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11233 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11235 struct elf_external_linux_prpsinfo64_ugid16 data
;
11237 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11238 return elfcore_write_note (abfd
, buf
, bufsiz
,
11239 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11243 struct elf_external_linux_prpsinfo64_ugid32 data
;
11245 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11246 return elfcore_write_note (abfd
, buf
, bufsiz
,
11247 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11252 elfcore_write_prstatus (bfd
*abfd
,
11259 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11261 if (bed
->elf_backend_write_core_note
!= NULL
)
11264 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11266 pid
, cursig
, gregs
);
11271 #if defined (HAVE_PRSTATUS_T)
11272 #if defined (HAVE_PRSTATUS32_T)
11273 if (bed
->s
->elfclass
== ELFCLASS32
)
11275 prstatus32_t prstat
;
11277 memset (&prstat
, 0, sizeof (prstat
));
11278 prstat
.pr_pid
= pid
;
11279 prstat
.pr_cursig
= cursig
;
11280 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11281 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11282 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11289 memset (&prstat
, 0, sizeof (prstat
));
11290 prstat
.pr_pid
= pid
;
11291 prstat
.pr_cursig
= cursig
;
11292 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11293 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11294 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11296 #endif /* HAVE_PRSTATUS_T */
11302 #if defined (HAVE_LWPSTATUS_T)
11304 elfcore_write_lwpstatus (bfd
*abfd
,
11311 lwpstatus_t lwpstat
;
11312 const char *note_name
= "CORE";
11314 memset (&lwpstat
, 0, sizeof (lwpstat
));
11315 lwpstat
.pr_lwpid
= pid
>> 16;
11316 lwpstat
.pr_cursig
= cursig
;
11317 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11318 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11319 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11320 #if !defined(gregs)
11321 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11322 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11324 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11325 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11328 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11329 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11331 #endif /* HAVE_LWPSTATUS_T */
11333 #if defined (HAVE_PSTATUS_T)
11335 elfcore_write_pstatus (bfd
*abfd
,
11339 int cursig ATTRIBUTE_UNUSED
,
11340 const void *gregs ATTRIBUTE_UNUSED
)
11342 const char *note_name
= "CORE";
11343 #if defined (HAVE_PSTATUS32_T)
11344 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11346 if (bed
->s
->elfclass
== ELFCLASS32
)
11350 memset (&pstat
, 0, sizeof (pstat
));
11351 pstat
.pr_pid
= pid
& 0xffff;
11352 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11353 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11361 memset (&pstat
, 0, sizeof (pstat
));
11362 pstat
.pr_pid
= pid
& 0xffff;
11363 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11364 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11368 #endif /* HAVE_PSTATUS_T */
11371 elfcore_write_prfpreg (bfd
*abfd
,
11374 const void *fpregs
,
11377 const char *note_name
= "CORE";
11378 return elfcore_write_note (abfd
, buf
, bufsiz
,
11379 note_name
, NT_FPREGSET
, fpregs
, size
);
11383 elfcore_write_prxfpreg (bfd
*abfd
,
11386 const void *xfpregs
,
11389 char *note_name
= "LINUX";
11390 return elfcore_write_note (abfd
, buf
, bufsiz
,
11391 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11395 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11396 const void *xfpregs
, int size
)
11399 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11400 note_name
= "FreeBSD";
11402 note_name
= "LINUX";
11403 return elfcore_write_note (abfd
, buf
, bufsiz
,
11404 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11408 elfcore_write_ppc_vmx (bfd
*abfd
,
11411 const void *ppc_vmx
,
11414 char *note_name
= "LINUX";
11415 return elfcore_write_note (abfd
, buf
, bufsiz
,
11416 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11420 elfcore_write_ppc_vsx (bfd
*abfd
,
11423 const void *ppc_vsx
,
11426 char *note_name
= "LINUX";
11427 return elfcore_write_note (abfd
, buf
, bufsiz
,
11428 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11432 elfcore_write_ppc_tar (bfd
*abfd
,
11435 const void *ppc_tar
,
11438 char *note_name
= "LINUX";
11439 return elfcore_write_note (abfd
, buf
, bufsiz
,
11440 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11444 elfcore_write_ppc_ppr (bfd
*abfd
,
11447 const void *ppc_ppr
,
11450 char *note_name
= "LINUX";
11451 return elfcore_write_note (abfd
, buf
, bufsiz
,
11452 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11456 elfcore_write_ppc_dscr (bfd
*abfd
,
11459 const void *ppc_dscr
,
11462 char *note_name
= "LINUX";
11463 return elfcore_write_note (abfd
, buf
, bufsiz
,
11464 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11468 elfcore_write_ppc_ebb (bfd
*abfd
,
11471 const void *ppc_ebb
,
11474 char *note_name
= "LINUX";
11475 return elfcore_write_note (abfd
, buf
, bufsiz
,
11476 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11480 elfcore_write_ppc_pmu (bfd
*abfd
,
11483 const void *ppc_pmu
,
11486 char *note_name
= "LINUX";
11487 return elfcore_write_note (abfd
, buf
, bufsiz
,
11488 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11492 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11495 const void *ppc_tm_cgpr
,
11498 char *note_name
= "LINUX";
11499 return elfcore_write_note (abfd
, buf
, bufsiz
,
11500 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11504 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11507 const void *ppc_tm_cfpr
,
11510 char *note_name
= "LINUX";
11511 return elfcore_write_note (abfd
, buf
, bufsiz
,
11512 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11516 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11519 const void *ppc_tm_cvmx
,
11522 char *note_name
= "LINUX";
11523 return elfcore_write_note (abfd
, buf
, bufsiz
,
11524 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11528 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11531 const void *ppc_tm_cvsx
,
11534 char *note_name
= "LINUX";
11535 return elfcore_write_note (abfd
, buf
, bufsiz
,
11536 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11540 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11543 const void *ppc_tm_spr
,
11546 char *note_name
= "LINUX";
11547 return elfcore_write_note (abfd
, buf
, bufsiz
,
11548 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11552 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11555 const void *ppc_tm_ctar
,
11558 char *note_name
= "LINUX";
11559 return elfcore_write_note (abfd
, buf
, bufsiz
,
11560 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11564 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11567 const void *ppc_tm_cppr
,
11570 char *note_name
= "LINUX";
11571 return elfcore_write_note (abfd
, buf
, bufsiz
,
11572 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11576 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11579 const void *ppc_tm_cdscr
,
11582 char *note_name
= "LINUX";
11583 return elfcore_write_note (abfd
, buf
, bufsiz
,
11584 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11588 elfcore_write_s390_high_gprs (bfd
*abfd
,
11591 const void *s390_high_gprs
,
11594 char *note_name
= "LINUX";
11595 return elfcore_write_note (abfd
, buf
, bufsiz
,
11596 note_name
, NT_S390_HIGH_GPRS
,
11597 s390_high_gprs
, size
);
11601 elfcore_write_s390_timer (bfd
*abfd
,
11604 const void *s390_timer
,
11607 char *note_name
= "LINUX";
11608 return elfcore_write_note (abfd
, buf
, bufsiz
,
11609 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11613 elfcore_write_s390_todcmp (bfd
*abfd
,
11616 const void *s390_todcmp
,
11619 char *note_name
= "LINUX";
11620 return elfcore_write_note (abfd
, buf
, bufsiz
,
11621 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11625 elfcore_write_s390_todpreg (bfd
*abfd
,
11628 const void *s390_todpreg
,
11631 char *note_name
= "LINUX";
11632 return elfcore_write_note (abfd
, buf
, bufsiz
,
11633 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11637 elfcore_write_s390_ctrs (bfd
*abfd
,
11640 const void *s390_ctrs
,
11643 char *note_name
= "LINUX";
11644 return elfcore_write_note (abfd
, buf
, bufsiz
,
11645 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11649 elfcore_write_s390_prefix (bfd
*abfd
,
11652 const void *s390_prefix
,
11655 char *note_name
= "LINUX";
11656 return elfcore_write_note (abfd
, buf
, bufsiz
,
11657 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11661 elfcore_write_s390_last_break (bfd
*abfd
,
11664 const void *s390_last_break
,
11667 char *note_name
= "LINUX";
11668 return elfcore_write_note (abfd
, buf
, bufsiz
,
11669 note_name
, NT_S390_LAST_BREAK
,
11670 s390_last_break
, size
);
11674 elfcore_write_s390_system_call (bfd
*abfd
,
11677 const void *s390_system_call
,
11680 char *note_name
= "LINUX";
11681 return elfcore_write_note (abfd
, buf
, bufsiz
,
11682 note_name
, NT_S390_SYSTEM_CALL
,
11683 s390_system_call
, size
);
11687 elfcore_write_s390_tdb (bfd
*abfd
,
11690 const void *s390_tdb
,
11693 char *note_name
= "LINUX";
11694 return elfcore_write_note (abfd
, buf
, bufsiz
,
11695 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11699 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11702 const void *s390_vxrs_low
,
11705 char *note_name
= "LINUX";
11706 return elfcore_write_note (abfd
, buf
, bufsiz
,
11707 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11711 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11714 const void *s390_vxrs_high
,
11717 char *note_name
= "LINUX";
11718 return elfcore_write_note (abfd
, buf
, bufsiz
,
11719 note_name
, NT_S390_VXRS_HIGH
,
11720 s390_vxrs_high
, size
);
11724 elfcore_write_s390_gs_cb (bfd
*abfd
,
11727 const void *s390_gs_cb
,
11730 char *note_name
= "LINUX";
11731 return elfcore_write_note (abfd
, buf
, bufsiz
,
11732 note_name
, NT_S390_GS_CB
,
11737 elfcore_write_s390_gs_bc (bfd
*abfd
,
11740 const void *s390_gs_bc
,
11743 char *note_name
= "LINUX";
11744 return elfcore_write_note (abfd
, buf
, bufsiz
,
11745 note_name
, NT_S390_GS_BC
,
11750 elfcore_write_arm_vfp (bfd
*abfd
,
11753 const void *arm_vfp
,
11756 char *note_name
= "LINUX";
11757 return elfcore_write_note (abfd
, buf
, bufsiz
,
11758 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11762 elfcore_write_aarch_tls (bfd
*abfd
,
11765 const void *aarch_tls
,
11768 char *note_name
= "LINUX";
11769 return elfcore_write_note (abfd
, buf
, bufsiz
,
11770 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11774 elfcore_write_aarch_hw_break (bfd
*abfd
,
11777 const void *aarch_hw_break
,
11780 char *note_name
= "LINUX";
11781 return elfcore_write_note (abfd
, buf
, bufsiz
,
11782 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11786 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11789 const void *aarch_hw_watch
,
11792 char *note_name
= "LINUX";
11793 return elfcore_write_note (abfd
, buf
, bufsiz
,
11794 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11798 elfcore_write_aarch_sve (bfd
*abfd
,
11801 const void *aarch_sve
,
11804 char *note_name
= "LINUX";
11805 return elfcore_write_note (abfd
, buf
, bufsiz
,
11806 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11810 elfcore_write_aarch_pauth (bfd
*abfd
,
11813 const void *aarch_pauth
,
11816 char *note_name
= "LINUX";
11817 return elfcore_write_note (abfd
, buf
, bufsiz
,
11818 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11822 elfcore_write_register_note (bfd
*abfd
,
11825 const char *section
,
11829 if (strcmp (section
, ".reg2") == 0)
11830 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11831 if (strcmp (section
, ".reg-xfp") == 0)
11832 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11833 if (strcmp (section
, ".reg-xstate") == 0)
11834 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11835 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11836 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11837 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11838 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11839 if (strcmp (section
, ".reg-ppc-tar") == 0)
11840 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11841 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11842 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11843 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11844 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11845 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11846 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11847 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11848 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11849 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11850 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11851 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11852 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11853 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11854 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11855 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11856 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11857 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11858 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11859 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11860 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11861 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11862 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11863 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11864 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11865 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11866 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11867 if (strcmp (section
, ".reg-s390-timer") == 0)
11868 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11869 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11870 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11871 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11872 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11873 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11874 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11875 if (strcmp (section
, ".reg-s390-prefix") == 0)
11876 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11877 if (strcmp (section
, ".reg-s390-last-break") == 0)
11878 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11879 if (strcmp (section
, ".reg-s390-system-call") == 0)
11880 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11881 if (strcmp (section
, ".reg-s390-tdb") == 0)
11882 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11883 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11884 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11885 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11886 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11887 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11888 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11889 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11890 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11891 if (strcmp (section
, ".reg-arm-vfp") == 0)
11892 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11893 if (strcmp (section
, ".reg-aarch-tls") == 0)
11894 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11895 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11896 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11897 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11898 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11899 if (strcmp (section
, ".reg-aarch-sve") == 0)
11900 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11901 if (strcmp (section
, ".reg-aarch-pauth") == 0)
11902 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
11907 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11912 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11913 gABI specifies that PT_NOTE alignment should be aligned to 4
11914 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11915 align is less than 4, we use 4 byte alignment. */
11918 if (align
!= 4 && align
!= 8)
11922 while (p
< buf
+ size
)
11924 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11925 Elf_Internal_Note in
;
11927 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11930 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11932 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11933 in
.namedata
= xnp
->name
;
11934 if (in
.namesz
> buf
- in
.namedata
+ size
)
11937 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11938 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11939 in
.descpos
= offset
+ (in
.descdata
- buf
);
11941 && (in
.descdata
>= buf
+ size
11942 || in
.descsz
> buf
- in
.descdata
+ size
))
11945 switch (bfd_get_format (abfd
))
11952 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11955 const char * string
;
11957 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11961 GROKER_ELEMENT ("", elfcore_grok_note
),
11962 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11963 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11964 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11965 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11966 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
11967 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
)
11969 #undef GROKER_ELEMENT
11972 for (i
= ARRAY_SIZE (grokers
); i
--;)
11974 if (in
.namesz
>= grokers
[i
].len
11975 && strncmp (in
.namedata
, grokers
[i
].string
,
11976 grokers
[i
].len
) == 0)
11978 if (! grokers
[i
].func (abfd
, & in
))
11987 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
11989 if (! elfobj_grok_gnu_note (abfd
, &in
))
11992 else if (in
.namesz
== sizeof "stapsdt"
11993 && strcmp (in
.namedata
, "stapsdt") == 0)
11995 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
12001 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
12008 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
12013 if (size
== 0 || (size
+ 1) == 0)
12016 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
12019 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
12023 /* PR 17512: file: ec08f814
12024 0-termintate the buffer so that string searches will not overflow. */
12027 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
12037 /* Providing external access to the ELF program header table. */
12039 /* Return an upper bound on the number of bytes required to store a
12040 copy of ABFD's program header table entries. Return -1 if an error
12041 occurs; bfd_get_error will return an appropriate code. */
12044 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12046 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12048 bfd_set_error (bfd_error_wrong_format
);
12052 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12055 /* Copy ABFD's program header table entries to *PHDRS. The entries
12056 will be stored as an array of Elf_Internal_Phdr structures, as
12057 defined in include/elf/internal.h. To find out how large the
12058 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12060 Return the number of program header table entries read, or -1 if an
12061 error occurs; bfd_get_error will return an appropriate code. */
12064 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12068 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12070 bfd_set_error (bfd_error_wrong_format
);
12074 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12075 if (num_phdrs
!= 0)
12076 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12077 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12082 enum elf_reloc_type_class
12083 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12084 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12085 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12087 return reloc_class_normal
;
12090 /* For RELA architectures, return the relocation value for a
12091 relocation against a local symbol. */
12094 _bfd_elf_rela_local_sym (bfd
*abfd
,
12095 Elf_Internal_Sym
*sym
,
12097 Elf_Internal_Rela
*rel
)
12099 asection
*sec
= *psec
;
12100 bfd_vma relocation
;
12102 relocation
= (sec
->output_section
->vma
12103 + sec
->output_offset
12105 if ((sec
->flags
& SEC_MERGE
)
12106 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12107 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12110 _bfd_merged_section_offset (abfd
, psec
,
12111 elf_section_data (sec
)->sec_info
,
12112 sym
->st_value
+ rel
->r_addend
);
12115 /* If we have changed the section, and our original section is
12116 marked with SEC_EXCLUDE, it means that the original
12117 SEC_MERGE section has been completely subsumed in some
12118 other SEC_MERGE section. In this case, we need to leave
12119 some info around for --emit-relocs. */
12120 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12121 sec
->kept_section
= *psec
;
12124 rel
->r_addend
-= relocation
;
12125 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12131 _bfd_elf_rel_local_sym (bfd
*abfd
,
12132 Elf_Internal_Sym
*sym
,
12136 asection
*sec
= *psec
;
12138 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12139 return sym
->st_value
+ addend
;
12141 return _bfd_merged_section_offset (abfd
, psec
,
12142 elf_section_data (sec
)->sec_info
,
12143 sym
->st_value
+ addend
);
12146 /* Adjust an address within a section. Given OFFSET within SEC, return
12147 the new offset within the section, based upon changes made to the
12148 section. Returns -1 if the offset is now invalid.
12149 The offset (in abnd out) is in target sized bytes, however big a
12153 _bfd_elf_section_offset (bfd
*abfd
,
12154 struct bfd_link_info
*info
,
12158 switch (sec
->sec_info_type
)
12160 case SEC_INFO_TYPE_STABS
:
12161 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12163 case SEC_INFO_TYPE_EH_FRAME
:
12164 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12167 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12169 /* Reverse the offset. */
12170 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12171 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12173 /* address_size and sec->size are in octets. Convert
12174 to bytes before subtracting the original offset. */
12175 offset
= ((sec
->size
- address_size
)
12176 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12182 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12183 reconstruct an ELF file by reading the segments out of remote memory
12184 based on the ELF file header at EHDR_VMA and the ELF program headers it
12185 points to. If not null, *LOADBASEP is filled in with the difference
12186 between the VMAs from which the segments were read, and the VMAs the
12187 file headers (and hence BFD's idea of each section's VMA) put them at.
12189 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12190 remote memory at target address VMA into the local buffer at MYADDR; it
12191 should return zero on success or an `errno' code on failure. TEMPL must
12192 be a BFD for an ELF target with the word size and byte order found in
12193 the remote memory. */
12196 bfd_elf_bfd_from_remote_memory
12199 bfd_size_type size
,
12200 bfd_vma
*loadbasep
,
12201 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12203 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12204 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12208 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12209 long symcount ATTRIBUTE_UNUSED
,
12210 asymbol
**syms ATTRIBUTE_UNUSED
,
12215 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12218 const char *relplt_name
;
12219 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12223 Elf_Internal_Shdr
*hdr
;
12229 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12232 if (dynsymcount
<= 0)
12235 if (!bed
->plt_sym_val
)
12238 relplt_name
= bed
->relplt_name
;
12239 if (relplt_name
== NULL
)
12240 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12241 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12242 if (relplt
== NULL
)
12245 hdr
= &elf_section_data (relplt
)->this_hdr
;
12246 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12247 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12250 plt
= bfd_get_section_by_name (abfd
, ".plt");
12254 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12255 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12258 count
= relplt
->size
/ hdr
->sh_entsize
;
12259 size
= count
* sizeof (asymbol
);
12260 p
= relplt
->relocation
;
12261 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12263 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12264 if (p
->addend
!= 0)
12267 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12269 size
+= sizeof ("+0x") - 1 + 8;
12274 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12278 names
= (char *) (s
+ count
);
12279 p
= relplt
->relocation
;
12281 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12286 addr
= bed
->plt_sym_val (i
, plt
, p
);
12287 if (addr
== (bfd_vma
) -1)
12290 *s
= **p
->sym_ptr_ptr
;
12291 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12292 we are defining a symbol, ensure one of them is set. */
12293 if ((s
->flags
& BSF_LOCAL
) == 0)
12294 s
->flags
|= BSF_GLOBAL
;
12295 s
->flags
|= BSF_SYNTHETIC
;
12297 s
->value
= addr
- plt
->vma
;
12300 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12301 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12303 if (p
->addend
!= 0)
12307 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12308 names
+= sizeof ("+0x") - 1;
12309 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12310 for (a
= buf
; *a
== '0'; ++a
)
12313 memcpy (names
, a
, len
);
12316 memcpy (names
, "@plt", sizeof ("@plt"));
12317 names
+= sizeof ("@plt");
12324 /* It is only used by x86-64 so far.
12325 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12326 but current usage would allow all of _bfd_std_section to be zero. */
12327 static const asymbol lcomm_sym
12328 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12329 asection _bfd_elf_large_com_section
12330 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12331 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12334 _bfd_elf_final_write_processing (bfd
*abfd
)
12336 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12338 i_ehdrp
= elf_elfheader (abfd
);
12340 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12341 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12343 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12344 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12345 STB_GNU_UNIQUE binding. */
12346 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12348 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12349 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12350 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12351 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12353 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12354 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12355 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12356 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12357 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12358 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12359 bfd_set_error (bfd_error_sorry
);
12367 /* Return TRUE for ELF symbol types that represent functions.
12368 This is the default version of this function, which is sufficient for
12369 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12372 _bfd_elf_is_function_type (unsigned int type
)
12374 return (type
== STT_FUNC
12375 || type
== STT_GNU_IFUNC
);
12378 /* If the ELF symbol SYM might be a function in SEC, return the
12379 function size and set *CODE_OFF to the function's entry point,
12380 otherwise return zero. */
12383 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12386 bfd_size_type size
;
12388 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12389 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12390 || sym
->section
!= sec
)
12393 *code_off
= sym
->value
;
12395 if (!(sym
->flags
& BSF_SYNTHETIC
))
12396 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
12402 /* Set to non-zero to enable some debug messages. */
12403 #define DEBUG_SECONDARY_RELOCS 0
12405 /* An internal-to-the-bfd-library only section type
12406 used to indicate a cached secondary reloc section. */
12407 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12409 /* Create a BFD section to hold a secondary reloc section. */
12412 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
12413 Elf_Internal_Shdr
*hdr
,
12415 unsigned int shindex
)
12417 /* We only support RELA secondary relocs. */
12418 if (hdr
->sh_type
!= SHT_RELA
)
12421 #if DEBUG_SECONDARY_RELOCS
12422 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
12424 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
12425 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
12428 /* Read in any secondary relocs associated with SEC. */
12431 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
12433 asymbol
** symbols
)
12435 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12437 bfd_boolean result
= TRUE
;
12438 bfd_vma (*r_sym
) (bfd_vma
);
12440 #if BFD_DEFAULT_TARGET_SIZE > 32
12441 if (bfd_arch_bits_per_address (abfd
) != 32)
12442 r_sym
= elf64_r_sym
;
12445 r_sym
= elf32_r_sym
;
12447 /* Discover if there are any secondary reloc sections
12448 associated with SEC. */
12449 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12451 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
12453 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
12454 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12456 bfd_byte
* native_relocs
;
12457 bfd_byte
* native_reloc
;
12458 arelent
* internal_relocs
;
12459 arelent
* internal_reloc
;
12461 unsigned int entsize
;
12462 unsigned int symcount
;
12463 unsigned int reloc_count
;
12466 if (ebd
->elf_info_to_howto
== NULL
)
12469 #if DEBUG_SECONDARY_RELOCS
12470 fprintf (stderr
, "read secondary relocs for %s from %s\n",
12471 sec
->name
, relsec
->name
);
12473 entsize
= hdr
->sh_entsize
;
12475 native_relocs
= bfd_malloc (hdr
->sh_size
);
12476 if (native_relocs
== NULL
)
12482 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
12483 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
12485 free (native_relocs
);
12486 bfd_set_error (bfd_error_file_too_big
);
12491 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
12492 if (internal_relocs
== NULL
)
12494 free (native_relocs
);
12499 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
12500 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
12503 free (native_relocs
);
12504 /* The internal_relocs will be freed when
12505 the memory for the bfd is released. */
12510 symcount
= bfd_get_symcount (abfd
);
12512 for (i
= 0, internal_reloc
= internal_relocs
,
12513 native_reloc
= native_relocs
;
12515 i
++, internal_reloc
++, native_reloc
+= entsize
)
12518 Elf_Internal_Rela rela
;
12520 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
12522 /* The address of an ELF reloc is section relative for an object
12523 file, and absolute for an executable file or shared library.
12524 The address of a normal BFD reloc is always section relative,
12525 and the address of a dynamic reloc is absolute.. */
12526 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
12527 internal_reloc
->address
= rela
.r_offset
;
12529 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
12531 if (r_sym (rela
.r_info
) == STN_UNDEF
)
12533 /* FIXME: This and the error case below mean that we
12534 have a symbol on relocs that is not elf_symbol_type. */
12535 internal_reloc
->sym_ptr_ptr
=
12536 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12538 else if (r_sym (rela
.r_info
) > symcount
)
12541 /* xgettext:c-format */
12542 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12543 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
12544 bfd_set_error (bfd_error_bad_value
);
12545 internal_reloc
->sym_ptr_ptr
=
12546 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12553 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
12555 internal_reloc
->sym_ptr_ptr
= ps
;
12556 /* Make sure that this symbol is not removed by strip. */
12557 (*ps
)->flags
|= BSF_KEEP
;
12560 internal_reloc
->addend
= rela
.r_addend
;
12562 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
12563 if (! res
|| internal_reloc
->howto
== NULL
)
12565 #if DEBUG_SECONDARY_RELOCS
12566 fprintf (stderr
, "there is no howto associated with reloc %lx\n",
12573 free (native_relocs
);
12574 /* Store the internal relocs. */
12575 elf_section_data (relsec
)->sec_info
= internal_relocs
;
12582 /* Set the ELF section header fields of an output secondary reloc section. */
12585 _bfd_elf_copy_special_section_fields (const bfd
* ibfd ATTRIBUTE_UNUSED
,
12586 bfd
* obfd ATTRIBUTE_UNUSED
,
12587 const Elf_Internal_Shdr
* isection
,
12588 Elf_Internal_Shdr
* osection
)
12593 if (isection
== NULL
)
12596 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
12599 isec
= isection
->bfd_section
;
12603 osec
= osection
->bfd_section
;
12607 BFD_ASSERT (elf_section_data (osec
)->sec_info
== NULL
);
12608 elf_section_data (osec
)->sec_info
= elf_section_data (isec
)->sec_info
;
12609 osection
->sh_type
= SHT_RELA
;
12610 osection
->sh_link
= elf_onesymtab (obfd
);
12611 if (osection
->sh_link
== 0)
12613 /* There is no symbol table - we are hosed... */
12615 /* xgettext:c-format */
12616 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12618 bfd_set_error (bfd_error_bad_value
);
12622 /* Find the output section that corresponds to the isection's sh_info link. */
12623 if (isection
->sh_info
== 0
12624 || isection
->sh_info
>= elf_numsections (ibfd
))
12627 /* xgettext:c-format */
12628 (_("%pB(%pA): info section index is invalid"),
12630 bfd_set_error (bfd_error_bad_value
);
12634 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
12636 if (isection
== NULL
12637 || isection
->bfd_section
== NULL
12638 || isection
->bfd_section
->output_section
== NULL
)
12641 /* xgettext:c-format */
12642 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12644 bfd_set_error (bfd_error_bad_value
);
12648 osection
->sh_info
=
12649 elf_section_data (isection
->bfd_section
->output_section
)->this_idx
;
12651 #if DEBUG_SECONDARY_RELOCS
12652 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
12653 osec
->name
, osection
->sh_link
, osection
->sh_info
);
12659 /* Write out a secondary reloc section. */
12662 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
12664 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12665 bfd_vma addr_offset
;
12667 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
12668 bfd_boolean result
= TRUE
;
12673 #if BFD_DEFAULT_TARGET_SIZE > 32
12674 if (bfd_arch_bits_per_address (abfd
) != 32)
12675 r_info
= elf64_r_info
;
12678 r_info
= elf32_r_info
;
12680 /* The address of an ELF reloc is section relative for an object
12681 file, and absolute for an executable file or shared library.
12682 The address of a BFD reloc is always section relative. */
12684 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
12685 addr_offset
= sec
->vma
;
12687 /* Discover if there are any secondary reloc sections
12688 associated with SEC. */
12689 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12691 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
12692 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
12694 if (hdr
->sh_type
== SHT_RELA
12695 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12697 asymbol
* last_sym
;
12699 unsigned int reloc_count
;
12701 arelent
* src_irel
;
12702 bfd_byte
* dst_rela
;
12704 if (hdr
->contents
!= NULL
)
12707 /* xgettext:c-format */
12708 (_("%pB(%pA): error: secondary reloc section processed twice"),
12710 bfd_set_error (bfd_error_bad_value
);
12715 reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
12716 if (reloc_count
<= 0)
12719 /* xgettext:c-format */
12720 (_("%pB(%pA): error: secondary reloc section is empty!"),
12722 bfd_set_error (bfd_error_bad_value
);
12727 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
12728 if (hdr
->contents
== NULL
)
12731 #if DEBUG_SECONDARY_RELOCS
12732 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
12733 reloc_count
, sec
->name
, relsec
->name
);
12737 dst_rela
= hdr
->contents
;
12738 src_irel
= (arelent
*) esd
->sec_info
;
12739 if (src_irel
== NULL
)
12742 /* xgettext:c-format */
12743 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12745 bfd_set_error (bfd_error_bad_value
);
12750 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= hdr
->sh_entsize
)
12752 Elf_Internal_Rela src_rela
;
12757 ptr
= src_irel
+ idx
;
12761 /* xgettext:c-format */
12762 (_("%pB(%pA): error: reloc table entry %u is empty"),
12763 abfd
, relsec
, idx
);
12764 bfd_set_error (bfd_error_bad_value
);
12769 if (ptr
->sym_ptr_ptr
== NULL
)
12771 /* FIXME: Is this an error ? */
12776 sym
= *ptr
->sym_ptr_ptr
;
12778 if (sym
== last_sym
)
12782 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
12786 /* xgettext:c-format */
12787 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12788 abfd
, relsec
, idx
);
12789 bfd_set_error (bfd_error_bad_value
);
12798 if (sym
->the_bfd
!= NULL
12799 && sym
->the_bfd
->xvec
!= abfd
->xvec
12800 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
12803 /* xgettext:c-format */
12804 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12805 abfd
, relsec
, idx
);
12806 bfd_set_error (bfd_error_bad_value
);
12812 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
12813 if (ptr
->howto
== NULL
)
12816 /* xgettext:c-format */
12817 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12818 abfd
, relsec
, idx
);
12819 bfd_set_error (bfd_error_bad_value
);
12821 src_rela
.r_info
= r_info (0, 0);
12824 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
12825 src_rela
.r_addend
= ptr
->addend
;
12826 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);