1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2020 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
44 #include "libiberty.h"
45 #include "safe-ctype.h"
46 #include "elf-linux-core.h"
52 static int elf_sort_sections (const void *, const void *);
53 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
56 file_ptr offset
, size_t align
);
58 /* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
62 /* Swap in a Verdef structure. */
65 _bfd_elf_swap_verdef_in (bfd
*abfd
,
66 const Elf_External_Verdef
*src
,
67 Elf_Internal_Verdef
*dst
)
69 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
70 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
71 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
72 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
73 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
74 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
75 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
78 /* Swap out a Verdef structure. */
81 _bfd_elf_swap_verdef_out (bfd
*abfd
,
82 const Elf_Internal_Verdef
*src
,
83 Elf_External_Verdef
*dst
)
85 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
86 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
87 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
88 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
89 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
90 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
91 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
94 /* Swap in a Verdaux structure. */
97 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
98 const Elf_External_Verdaux
*src
,
99 Elf_Internal_Verdaux
*dst
)
101 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
102 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
105 /* Swap out a Verdaux structure. */
108 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
109 const Elf_Internal_Verdaux
*src
,
110 Elf_External_Verdaux
*dst
)
112 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
113 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
116 /* Swap in a Verneed structure. */
119 _bfd_elf_swap_verneed_in (bfd
*abfd
,
120 const Elf_External_Verneed
*src
,
121 Elf_Internal_Verneed
*dst
)
123 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
124 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
125 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
126 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
127 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
130 /* Swap out a Verneed structure. */
133 _bfd_elf_swap_verneed_out (bfd
*abfd
,
134 const Elf_Internal_Verneed
*src
,
135 Elf_External_Verneed
*dst
)
137 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
138 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
139 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
140 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
141 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
144 /* Swap in a Vernaux structure. */
147 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
148 const Elf_External_Vernaux
*src
,
149 Elf_Internal_Vernaux
*dst
)
151 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
152 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
153 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
154 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
155 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
158 /* Swap out a Vernaux structure. */
161 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
162 const Elf_Internal_Vernaux
*src
,
163 Elf_External_Vernaux
*dst
)
165 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
166 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
167 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
168 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
169 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
172 /* Swap in a Versym structure. */
175 _bfd_elf_swap_versym_in (bfd
*abfd
,
176 const Elf_External_Versym
*src
,
177 Elf_Internal_Versym
*dst
)
179 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
182 /* Swap out a Versym structure. */
185 _bfd_elf_swap_versym_out (bfd
*abfd
,
186 const Elf_Internal_Versym
*src
,
187 Elf_External_Versym
*dst
)
189 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
192 /* Standard ELF hash function. Do not change this function; you will
193 cause invalid hash tables to be generated. */
196 bfd_elf_hash (const char *namearg
)
198 const unsigned char *name
= (const unsigned char *) namearg
;
203 while ((ch
= *name
++) != '\0')
206 if ((g
= (h
& 0xf0000000)) != 0)
209 /* The ELF ABI says `h &= ~g', but this is equivalent in
210 this case and on some machines one insn instead of two. */
214 return h
& 0xffffffff;
217 /* DT_GNU_HASH hash function. Do not change this function; you will
218 cause invalid hash tables to be generated. */
221 bfd_elf_gnu_hash (const char *namearg
)
223 const unsigned char *name
= (const unsigned char *) namearg
;
224 unsigned long h
= 5381;
227 while ((ch
= *name
++) != '\0')
228 h
= (h
<< 5) + h
+ ch
;
229 return h
& 0xffffffff;
232 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
233 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
235 bfd_elf_allocate_object (bfd
*abfd
,
237 enum elf_target_id object_id
)
239 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
240 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
241 if (abfd
->tdata
.any
== NULL
)
244 elf_object_id (abfd
) = object_id
;
245 if (abfd
->direction
!= read_direction
)
247 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
250 elf_tdata (abfd
)->o
= o
;
251 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
258 bfd_elf_make_object (bfd
*abfd
)
260 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
261 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
266 bfd_elf_mkcorefile (bfd
*abfd
)
268 /* I think this can be done just like an object file. */
269 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
271 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
272 return elf_tdata (abfd
)->core
!= NULL
;
276 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
278 Elf_Internal_Shdr
**i_shdrp
;
279 bfd_byte
*shstrtab
= NULL
;
281 bfd_size_type shstrtabsize
;
283 i_shdrp
= elf_elfsections (abfd
);
285 || shindex
>= elf_numsections (abfd
)
286 || i_shdrp
[shindex
] == 0)
289 shstrtab
= i_shdrp
[shindex
]->contents
;
290 if (shstrtab
== NULL
)
292 /* No cached one, attempt to read, and cache what we read. */
293 offset
= i_shdrp
[shindex
]->sh_offset
;
294 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
296 /* Allocate and clear an extra byte at the end, to prevent crashes
297 in case the string table is not terminated. */
298 if (shstrtabsize
+ 1 <= 1
299 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
300 || (shstrtab
= _bfd_alloc_and_read (abfd
, shstrtabsize
+ 1,
301 shstrtabsize
)) == NULL
)
303 /* Once we've failed to read it, make sure we don't keep
304 trying. Otherwise, we'll keep allocating space for
305 the string table over and over. */
306 i_shdrp
[shindex
]->sh_size
= 0;
309 shstrtab
[shstrtabsize
] = '\0';
310 i_shdrp
[shindex
]->contents
= shstrtab
;
312 return (char *) shstrtab
;
316 bfd_elf_string_from_elf_section (bfd
*abfd
,
317 unsigned int shindex
,
318 unsigned int strindex
)
320 Elf_Internal_Shdr
*hdr
;
325 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
328 hdr
= elf_elfsections (abfd
)[shindex
];
330 if (hdr
->contents
== NULL
)
332 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
334 /* PR 17512: file: f057ec89. */
335 /* xgettext:c-format */
336 _bfd_error_handler (_("%pB: attempt to load strings from"
337 " a non-string section (number %d)"),
342 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
347 /* PR 24273: The string section's contents may have already
348 been loaded elsewhere, eg because a corrupt file has the
349 string section index in the ELF header pointing at a group
350 section. So be paranoid, and test that the last byte of
351 the section is zero. */
352 if (hdr
->sh_size
== 0 || hdr
->contents
[hdr
->sh_size
- 1] != 0)
356 if (strindex
>= hdr
->sh_size
)
358 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
360 /* xgettext:c-format */
361 (_("%pB: invalid string offset %u >= %" PRIu64
" for section `%s'"),
362 abfd
, strindex
, (uint64_t) hdr
->sh_size
,
363 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
365 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
369 return ((char *) hdr
->contents
) + strindex
;
372 /* Read and convert symbols to internal format.
373 SYMCOUNT specifies the number of symbols to read, starting from
374 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
375 are non-NULL, they are used to store the internal symbols, external
376 symbols, and symbol section index extensions, respectively.
377 Returns a pointer to the internal symbol buffer (malloced if necessary)
378 or NULL if there were no symbols or some kind of problem. */
381 bfd_elf_get_elf_syms (bfd
*ibfd
,
382 Elf_Internal_Shdr
*symtab_hdr
,
385 Elf_Internal_Sym
*intsym_buf
,
387 Elf_External_Sym_Shndx
*extshndx_buf
)
389 Elf_Internal_Shdr
*shndx_hdr
;
391 const bfd_byte
*esym
;
392 Elf_External_Sym_Shndx
*alloc_extshndx
;
393 Elf_External_Sym_Shndx
*shndx
;
394 Elf_Internal_Sym
*alloc_intsym
;
395 Elf_Internal_Sym
*isym
;
396 Elf_Internal_Sym
*isymend
;
397 const struct elf_backend_data
*bed
;
402 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
408 /* Normal syms might have section extension entries. */
410 if (elf_symtab_shndx_list (ibfd
) != NULL
)
412 elf_section_list
* entry
;
413 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
415 /* Find an index section that is linked to this symtab section. */
416 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
419 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
422 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
424 shndx_hdr
= & entry
->hdr
;
429 if (shndx_hdr
== NULL
)
431 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
432 /* Not really accurate, but this was how the old code used to work. */
433 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
434 /* Otherwise we do nothing. The assumption is that
435 the index table will not be needed. */
439 /* Read the symbols. */
441 alloc_extshndx
= NULL
;
443 bed
= get_elf_backend_data (ibfd
);
444 extsym_size
= bed
->s
->sizeof_sym
;
445 if (_bfd_mul_overflow (symcount
, extsym_size
, &amt
))
447 bfd_set_error (bfd_error_file_too_big
);
451 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
452 if (extsym_buf
== NULL
)
454 alloc_ext
= bfd_malloc (amt
);
455 extsym_buf
= alloc_ext
;
457 if (extsym_buf
== NULL
458 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
459 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
465 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
469 if (_bfd_mul_overflow (symcount
, sizeof (Elf_External_Sym_Shndx
), &amt
))
471 bfd_set_error (bfd_error_file_too_big
);
475 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
476 if (extshndx_buf
== NULL
)
478 alloc_extshndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
479 extshndx_buf
= alloc_extshndx
;
481 if (extshndx_buf
== NULL
482 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
483 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
490 if (intsym_buf
== NULL
)
492 if (_bfd_mul_overflow (symcount
, sizeof (Elf_Internal_Sym
), &amt
))
494 bfd_set_error (bfd_error_file_too_big
);
497 alloc_intsym
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
498 intsym_buf
= alloc_intsym
;
499 if (intsym_buf
== NULL
)
503 /* Convert the symbols to internal form. */
504 isymend
= intsym_buf
+ symcount
;
505 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
506 shndx
= extshndx_buf
;
508 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
509 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
511 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
512 /* xgettext:c-format */
513 _bfd_error_handler (_("%pB symbol number %lu references"
514 " nonexistent SHT_SYMTAB_SHNDX section"),
515 ibfd
, (unsigned long) symoffset
);
516 if (alloc_intsym
!= NULL
)
523 if (alloc_ext
!= NULL
)
525 if (alloc_extshndx
!= NULL
)
526 free (alloc_extshndx
);
531 /* Look up a symbol name. */
533 bfd_elf_sym_name (bfd
*abfd
,
534 Elf_Internal_Shdr
*symtab_hdr
,
535 Elf_Internal_Sym
*isym
,
539 unsigned int iname
= isym
->st_name
;
540 unsigned int shindex
= symtab_hdr
->sh_link
;
542 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
543 /* Check for a bogus st_shndx to avoid crashing. */
544 && isym
->st_shndx
< elf_numsections (abfd
))
546 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
547 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
550 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
553 else if (sym_sec
&& *name
== '\0')
554 name
= bfd_section_name (sym_sec
);
559 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
560 sections. The first element is the flags, the rest are section
563 typedef union elf_internal_group
{
564 Elf_Internal_Shdr
*shdr
;
566 } Elf_Internal_Group
;
568 /* Return the name of the group signature symbol. Why isn't the
569 signature just a string? */
572 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
574 Elf_Internal_Shdr
*hdr
;
575 unsigned char esym
[sizeof (Elf64_External_Sym
)];
576 Elf_External_Sym_Shndx eshndx
;
577 Elf_Internal_Sym isym
;
579 /* First we need to ensure the symbol table is available. Make sure
580 that it is a symbol table section. */
581 if (ghdr
->sh_link
>= elf_numsections (abfd
))
583 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
584 if (hdr
->sh_type
!= SHT_SYMTAB
585 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
588 /* Go read the symbol. */
589 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
590 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
591 &isym
, esym
, &eshndx
) == NULL
)
594 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
597 /* Set next_in_group list pointer, and group name for NEWSECT. */
600 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
602 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
604 /* If num_group is zero, read in all SHT_GROUP sections. The count
605 is set to -1 if there are no SHT_GROUP sections. */
608 unsigned int i
, shnum
;
610 /* First count the number of groups. If we have a SHT_GROUP
611 section with just a flag word (ie. sh_size is 4), ignore it. */
612 shnum
= elf_numsections (abfd
);
615 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
616 ( (shdr)->sh_type == SHT_GROUP \
617 && (shdr)->sh_size >= minsize \
618 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
619 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
621 for (i
= 0; i
< shnum
; i
++)
623 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
625 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
631 num_group
= (unsigned) -1;
632 elf_tdata (abfd
)->num_group
= num_group
;
633 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
637 /* We keep a list of elf section headers for group sections,
638 so we can find them quickly. */
641 elf_tdata (abfd
)->num_group
= num_group
;
642 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
643 elf_tdata (abfd
)->group_sect_ptr
644 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
645 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
649 for (i
= 0; i
< shnum
; i
++)
651 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
653 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
656 Elf_Internal_Group
*dest
;
658 /* Make sure the group section has a BFD section
660 if (!bfd_section_from_shdr (abfd
, i
))
663 /* Add to list of sections. */
664 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
667 /* Read the raw contents. */
668 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
669 shdr
->contents
= NULL
;
670 if (_bfd_mul_overflow (shdr
->sh_size
,
671 sizeof (*dest
) / 4, &amt
)
672 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
674 = _bfd_alloc_and_read (abfd
, amt
, shdr
->sh_size
)))
677 /* xgettext:c-format */
678 (_("%pB: invalid size field in group section"
679 " header: %#" PRIx64
""),
680 abfd
, (uint64_t) shdr
->sh_size
);
681 bfd_set_error (bfd_error_bad_value
);
686 /* Translate raw contents, a flag word followed by an
687 array of elf section indices all in target byte order,
688 to the flag word followed by an array of elf section
690 src
= shdr
->contents
+ shdr
->sh_size
;
691 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
699 idx
= H_GET_32 (abfd
, src
);
700 if (src
== shdr
->contents
)
704 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
705 shdr
->bfd_section
->flags
706 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
711 dest
->shdr
= elf_elfsections (abfd
)[idx
];
712 /* PR binutils/23199: All sections in a
713 section group should be marked with
714 SHF_GROUP. But some tools generate
715 broken objects without SHF_GROUP. Fix
717 dest
->shdr
->sh_flags
|= SHF_GROUP
;
720 || dest
->shdr
->sh_type
== SHT_GROUP
)
723 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
731 /* PR 17510: Corrupt binaries might contain invalid groups. */
732 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
734 elf_tdata (abfd
)->num_group
= num_group
;
736 /* If all groups are invalid then fail. */
739 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
740 elf_tdata (abfd
)->num_group
= num_group
= -1;
742 (_("%pB: no valid group sections found"), abfd
);
743 bfd_set_error (bfd_error_bad_value
);
749 if (num_group
!= (unsigned) -1)
751 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
754 for (j
= 0; j
< num_group
; j
++)
756 /* Begin search from previous found group. */
757 unsigned i
= (j
+ search_offset
) % num_group
;
759 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
760 Elf_Internal_Group
*idx
;
766 idx
= (Elf_Internal_Group
*) shdr
->contents
;
767 if (idx
== NULL
|| shdr
->sh_size
< 4)
769 /* See PR 21957 for a reproducer. */
770 /* xgettext:c-format */
771 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
772 abfd
, shdr
->bfd_section
);
773 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
774 bfd_set_error (bfd_error_bad_value
);
777 n_elt
= shdr
->sh_size
/ 4;
779 /* Look through this group's sections to see if current
780 section is a member. */
782 if ((++idx
)->shdr
== hdr
)
786 /* We are a member of this group. Go looking through
787 other members to see if any others are linked via
789 idx
= (Elf_Internal_Group
*) shdr
->contents
;
790 n_elt
= shdr
->sh_size
/ 4;
792 if ((++idx
)->shdr
!= NULL
793 && (s
= idx
->shdr
->bfd_section
) != NULL
794 && elf_next_in_group (s
) != NULL
)
798 /* Snarf the group name from other member, and
799 insert current section in circular list. */
800 elf_group_name (newsect
) = elf_group_name (s
);
801 elf_next_in_group (newsect
) = elf_next_in_group (s
);
802 elf_next_in_group (s
) = newsect
;
808 gname
= group_signature (abfd
, shdr
);
811 elf_group_name (newsect
) = gname
;
813 /* Start a circular list with one element. */
814 elf_next_in_group (newsect
) = newsect
;
817 /* If the group section has been created, point to the
819 if (shdr
->bfd_section
!= NULL
)
820 elf_next_in_group (shdr
->bfd_section
) = newsect
;
822 elf_tdata (abfd
)->group_search_offset
= i
;
829 if (elf_group_name (newsect
) == NULL
)
831 /* xgettext:c-format */
832 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
840 _bfd_elf_setup_sections (bfd
*abfd
)
843 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
844 bfd_boolean result
= TRUE
;
847 /* Process SHF_LINK_ORDER. */
848 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
850 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
851 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
853 unsigned int elfsec
= this_hdr
->sh_link
;
854 /* FIXME: The old Intel compiler and old strip/objcopy may
855 not set the sh_link or sh_info fields. Hence we could
856 get the situation where elfsec is 0. */
859 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
860 bed
->link_order_error_handler
861 /* xgettext:c-format */
862 (_("%pB: warning: sh_link not set for section `%pA'"),
867 asection
*linksec
= NULL
;
869 if (elfsec
< elf_numsections (abfd
))
871 this_hdr
= elf_elfsections (abfd
)[elfsec
];
872 linksec
= this_hdr
->bfd_section
;
876 Some strip/objcopy may leave an incorrect value in
877 sh_link. We don't want to proceed. */
881 /* xgettext:c-format */
882 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
883 s
->owner
, elfsec
, s
);
887 elf_linked_to_section (s
) = linksec
;
890 else if (this_hdr
->sh_type
== SHT_GROUP
891 && elf_next_in_group (s
) == NULL
)
894 /* xgettext:c-format */
895 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
896 abfd
, elf_section_data (s
)->this_idx
);
901 /* Process section groups. */
902 if (num_group
== (unsigned) -1)
905 for (i
= 0; i
< num_group
; i
++)
907 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
908 Elf_Internal_Group
*idx
;
911 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
912 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
915 /* xgettext:c-format */
916 (_("%pB: section group entry number %u is corrupt"),
922 idx
= (Elf_Internal_Group
*) shdr
->contents
;
923 n_elt
= shdr
->sh_size
/ 4;
929 if (idx
->shdr
== NULL
)
931 else if (idx
->shdr
->bfd_section
)
932 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
933 else if (idx
->shdr
->sh_type
!= SHT_RELA
934 && idx
->shdr
->sh_type
!= SHT_REL
)
936 /* There are some unknown sections in the group. */
938 /* xgettext:c-format */
939 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
942 bfd_elf_string_from_elf_section (abfd
,
943 (elf_elfheader (abfd
)
956 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
958 return elf_next_in_group (sec
) != NULL
;
962 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
964 if (elf_sec_group (sec
) != NULL
)
965 return elf_group_name (sec
);
970 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
972 unsigned int len
= strlen (name
);
973 char *new_name
= bfd_alloc (abfd
, len
+ 2);
974 if (new_name
== NULL
)
978 memcpy (new_name
+ 2, name
+ 1, len
);
983 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
985 unsigned int len
= strlen (name
);
986 char *new_name
= bfd_alloc (abfd
, len
);
987 if (new_name
== NULL
)
990 memcpy (new_name
+ 1, name
+ 2, len
- 1);
994 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
998 int16_t major_version
;
999 int16_t minor_version
;
1000 unsigned char slim_object
;
1002 /* Flags is a private field that is not defined publicly. */
1006 /* Make a BFD section from an ELF section. We store a pointer to the
1007 BFD section in the bfd_section field of the header. */
1010 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1011 Elf_Internal_Shdr
*hdr
,
1017 const struct elf_backend_data
*bed
;
1018 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
1020 if (hdr
->bfd_section
!= NULL
)
1023 newsect
= bfd_make_section_anyway (abfd
, name
);
1024 if (newsect
== NULL
)
1027 hdr
->bfd_section
= newsect
;
1028 elf_section_data (newsect
)->this_hdr
= *hdr
;
1029 elf_section_data (newsect
)->this_idx
= shindex
;
1031 /* Always use the real type/flags. */
1032 elf_section_type (newsect
) = hdr
->sh_type
;
1033 elf_section_flags (newsect
) = hdr
->sh_flags
;
1035 newsect
->filepos
= hdr
->sh_offset
;
1037 flags
= SEC_NO_FLAGS
;
1038 if (hdr
->sh_type
!= SHT_NOBITS
)
1039 flags
|= SEC_HAS_CONTENTS
;
1040 if (hdr
->sh_type
== SHT_GROUP
)
1042 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1045 if (hdr
->sh_type
!= SHT_NOBITS
)
1048 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1049 flags
|= SEC_READONLY
;
1050 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1052 else if ((flags
& SEC_LOAD
) != 0)
1054 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1057 newsect
->entsize
= hdr
->sh_entsize
;
1059 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1060 flags
|= SEC_STRINGS
;
1061 if (hdr
->sh_flags
& SHF_GROUP
)
1062 if (!setup_group (abfd
, hdr
, newsect
))
1064 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1065 flags
|= SEC_THREAD_LOCAL
;
1066 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1067 flags
|= SEC_EXCLUDE
;
1069 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1071 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1072 but binutils as of 2019-07-23 did not set the EI_OSABI header
1076 case ELFOSABI_FREEBSD
:
1077 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1078 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1082 if ((flags
& SEC_ALLOC
) == 0)
1084 /* The debugging sections appear to be recognized only by name,
1085 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1086 if (name
[0] == '.')
1088 if (strncmp (name
, ".debug", 6) == 0
1089 || strncmp (name
, ".gnu.linkonce.wi.", 17) == 0
1090 || strncmp (name
, ".zdebug", 7) == 0)
1091 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1092 else if (strncmp (name
, GNU_BUILD_ATTRS_SECTION_NAME
, 21) == 0
1093 || strncmp (name
, ".note.gnu", 9) == 0)
1095 flags
|= SEC_ELF_OCTETS
;
1098 else if (strncmp (name
, ".line", 5) == 0
1099 || strncmp (name
, ".stab", 5) == 0
1100 || strcmp (name
, ".gdb_index") == 0)
1101 flags
|= SEC_DEBUGGING
;
1105 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1106 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1107 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
)))
1110 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1111 only link a single copy of the section. This is used to support
1112 g++. g++ will emit each template expansion in its own section.
1113 The symbols will be defined as weak, so that multiple definitions
1114 are permitted. The GNU linker extension is to actually discard
1115 all but one of the sections. */
1116 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1117 && elf_next_in_group (newsect
) == NULL
)
1118 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1120 if (!bfd_set_section_flags (newsect
, flags
))
1123 bed
= get_elf_backend_data (abfd
);
1124 if (bed
->elf_backend_section_flags
)
1125 if (!bed
->elf_backend_section_flags (hdr
))
1128 /* We do not parse the PT_NOTE segments as we are interested even in the
1129 separate debug info files which may have the segments offsets corrupted.
1130 PT_NOTEs from the core files are currently not parsed using BFD. */
1131 if (hdr
->sh_type
== SHT_NOTE
)
1135 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1138 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1139 hdr
->sh_offset
, hdr
->sh_addralign
);
1143 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1145 Elf_Internal_Phdr
*phdr
;
1146 unsigned int i
, nload
;
1148 /* Some ELF linkers produce binaries with all the program header
1149 p_paddr fields zero. If we have such a binary with more than
1150 one PT_LOAD header, then leave the section lma equal to vma
1151 so that we don't create sections with overlapping lma. */
1152 phdr
= elf_tdata (abfd
)->phdr
;
1153 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1154 if (phdr
->p_paddr
!= 0)
1156 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1158 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1161 phdr
= elf_tdata (abfd
)->phdr
;
1162 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1164 if (((phdr
->p_type
== PT_LOAD
1165 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1166 || phdr
->p_type
== PT_TLS
)
1167 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1169 if ((newsect
->flags
& SEC_LOAD
) == 0)
1170 newsect
->lma
= (phdr
->p_paddr
1171 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1173 /* We used to use the same adjustment for SEC_LOAD
1174 sections, but that doesn't work if the segment
1175 is packed with code from multiple VMAs.
1176 Instead we calculate the section LMA based on
1177 the segment LMA. It is assumed that the
1178 segment will contain sections with contiguous
1179 LMAs, even if the VMAs are not. */
1180 newsect
->lma
= (phdr
->p_paddr
1181 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1183 /* With contiguous segments, we can't tell from file
1184 offsets whether a section with zero size should
1185 be placed at the end of one segment or the
1186 beginning of the next. Decide based on vaddr. */
1187 if (hdr
->sh_addr
>= phdr
->p_vaddr
1188 && (hdr
->sh_addr
+ hdr
->sh_size
1189 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1195 /* Compress/decompress DWARF debug sections with names: .debug_* and
1196 .zdebug_*, after the section flags is set. */
1197 if ((newsect
->flags
& SEC_DEBUGGING
)
1198 && ((name
[1] == 'd' && name
[6] == '_')
1199 || (name
[1] == 'z' && name
[7] == '_')))
1201 enum { nothing
, compress
, decompress
} action
= nothing
;
1202 int compression_header_size
;
1203 bfd_size_type uncompressed_size
;
1204 unsigned int uncompressed_align_power
;
1205 bfd_boolean compressed
1206 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1207 &compression_header_size
,
1209 &uncompressed_align_power
);
1212 /* Compressed section. Check if we should decompress. */
1213 if ((abfd
->flags
& BFD_DECOMPRESS
))
1214 action
= decompress
;
1217 /* Compress the uncompressed section or convert from/to .zdebug*
1218 section. Check if we should compress. */
1219 if (action
== nothing
)
1221 if (newsect
->size
!= 0
1222 && (abfd
->flags
& BFD_COMPRESS
)
1223 && compression_header_size
>= 0
1224 && uncompressed_size
> 0
1226 || ((compression_header_size
> 0)
1227 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1233 if (action
== compress
)
1235 if (!bfd_init_section_compress_status (abfd
, newsect
))
1238 /* xgettext:c-format */
1239 (_("%pB: unable to initialize compress status for section %s"),
1246 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1249 /* xgettext:c-format */
1250 (_("%pB: unable to initialize decompress status for section %s"),
1256 if (abfd
->is_linker_input
)
1259 && (action
== decompress
1260 || (action
== compress
1261 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1263 /* Convert section name from .zdebug_* to .debug_* so
1264 that linker will consider this section as a debug
1266 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1267 if (new_name
== NULL
)
1269 bfd_rename_section (newsect
, new_name
);
1273 /* For objdump, don't rename the section. For objcopy, delay
1274 section rename to elf_fake_sections. */
1275 newsect
->flags
|= SEC_ELF_RENAME
;
1278 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1280 const char *lto_section_name
= ".gnu.lto_.lto.";
1281 if (strncmp (name
, lto_section_name
, strlen (lto_section_name
)) == 0)
1283 struct lto_section lsection
;
1284 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1285 sizeof (struct lto_section
)))
1286 abfd
->lto_slim_object
= lsection
.slim_object
;
1292 const char *const bfd_elf_section_type_names
[] =
1294 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1295 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1296 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1299 /* ELF relocs are against symbols. If we are producing relocatable
1300 output, and the reloc is against an external symbol, and nothing
1301 has given us any additional addend, the resulting reloc will also
1302 be against the same symbol. In such a case, we don't want to
1303 change anything about the way the reloc is handled, since it will
1304 all be done at final link time. Rather than put special case code
1305 into bfd_perform_relocation, all the reloc types use this howto
1306 function. It just short circuits the reloc if producing
1307 relocatable output against an external symbol. */
1309 bfd_reloc_status_type
1310 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1311 arelent
*reloc_entry
,
1313 void *data ATTRIBUTE_UNUSED
,
1314 asection
*input_section
,
1316 char **error_message ATTRIBUTE_UNUSED
)
1318 if (output_bfd
!= NULL
1319 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1320 && (! reloc_entry
->howto
->partial_inplace
1321 || reloc_entry
->addend
== 0))
1323 reloc_entry
->address
+= input_section
->output_offset
;
1324 return bfd_reloc_ok
;
1327 return bfd_reloc_continue
;
1330 /* Returns TRUE if section A matches section B.
1331 Names, addresses and links may be different, but everything else
1332 should be the same. */
1335 section_match (const Elf_Internal_Shdr
* a
,
1336 const Elf_Internal_Shdr
* b
)
1338 if (a
->sh_type
!= b
->sh_type
1339 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1340 || a
->sh_addralign
!= b
->sh_addralign
1341 || a
->sh_entsize
!= b
->sh_entsize
)
1343 if (a
->sh_type
== SHT_SYMTAB
1344 || a
->sh_type
== SHT_STRTAB
)
1346 return a
->sh_size
== b
->sh_size
;
1349 /* Find a section in OBFD that has the same characteristics
1350 as IHEADER. Return the index of this section or SHN_UNDEF if
1351 none can be found. Check's section HINT first, as this is likely
1352 to be the correct section. */
1355 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1356 const unsigned int hint
)
1358 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1361 BFD_ASSERT (iheader
!= NULL
);
1363 /* See PR 20922 for a reproducer of the NULL test. */
1364 if (hint
< elf_numsections (obfd
)
1365 && oheaders
[hint
] != NULL
1366 && section_match (oheaders
[hint
], iheader
))
1369 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1371 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1373 if (oheader
== NULL
)
1375 if (section_match (oheader
, iheader
))
1376 /* FIXME: Do we care if there is a potential for
1377 multiple matches ? */
1384 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1385 Processor specific section, based upon a matching input section.
1386 Returns TRUE upon success, FALSE otherwise. */
1389 copy_special_section_fields (const bfd
*ibfd
,
1391 const Elf_Internal_Shdr
*iheader
,
1392 Elf_Internal_Shdr
*oheader
,
1393 const unsigned int secnum
)
1395 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1396 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1397 bfd_boolean changed
= FALSE
;
1398 unsigned int sh_link
;
1400 if (oheader
->sh_type
== SHT_NOBITS
)
1402 /* This is a feature for objcopy --only-keep-debug:
1403 When a section's type is changed to NOBITS, we preserve
1404 the sh_link and sh_info fields so that they can be
1405 matched up with the original.
1407 Note: Strictly speaking these assignments are wrong.
1408 The sh_link and sh_info fields should point to the
1409 relevent sections in the output BFD, which may not be in
1410 the same location as they were in the input BFD. But
1411 the whole point of this action is to preserve the
1412 original values of the sh_link and sh_info fields, so
1413 that they can be matched up with the section headers in
1414 the original file. So strictly speaking we may be
1415 creating an invalid ELF file, but it is only for a file
1416 that just contains debug info and only for sections
1417 without any contents. */
1418 if (oheader
->sh_link
== 0)
1419 oheader
->sh_link
= iheader
->sh_link
;
1420 if (oheader
->sh_info
== 0)
1421 oheader
->sh_info
= iheader
->sh_info
;
1425 /* Allow the target a chance to decide how these fields should be set. */
1426 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1430 /* We have an iheader which might match oheader, and which has non-zero
1431 sh_info and/or sh_link fields. Attempt to follow those links and find
1432 the section in the output bfd which corresponds to the linked section
1433 in the input bfd. */
1434 if (iheader
->sh_link
!= SHN_UNDEF
)
1436 /* See PR 20931 for a reproducer. */
1437 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1440 /* xgettext:c-format */
1441 (_("%pB: invalid sh_link field (%d) in section number %d"),
1442 ibfd
, iheader
->sh_link
, secnum
);
1446 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1447 if (sh_link
!= SHN_UNDEF
)
1449 oheader
->sh_link
= sh_link
;
1453 /* FIXME: Should we install iheader->sh_link
1454 if we could not find a match ? */
1456 /* xgettext:c-format */
1457 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1460 if (iheader
->sh_info
)
1462 /* The sh_info field can hold arbitrary information, but if the
1463 SHF_LINK_INFO flag is set then it should be interpreted as a
1465 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1467 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1469 if (sh_link
!= SHN_UNDEF
)
1470 oheader
->sh_flags
|= SHF_INFO_LINK
;
1473 /* No idea what it means - just copy it. */
1474 sh_link
= iheader
->sh_info
;
1476 if (sh_link
!= SHN_UNDEF
)
1478 oheader
->sh_info
= sh_link
;
1483 /* xgettext:c-format */
1484 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1490 /* Copy the program header and other data from one object module to
1494 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1496 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1497 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1498 const struct elf_backend_data
*bed
;
1501 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1502 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1505 if (!elf_flags_init (obfd
))
1507 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1508 elf_flags_init (obfd
) = TRUE
;
1511 elf_gp (obfd
) = elf_gp (ibfd
);
1513 /* Also copy the EI_OSABI field. */
1514 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1515 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1517 /* If set, copy the EI_ABIVERSION field. */
1518 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1519 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1520 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1522 /* Copy object attributes. */
1523 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1525 if (iheaders
== NULL
|| oheaders
== NULL
)
1528 bed
= get_elf_backend_data (obfd
);
1530 /* Possibly copy other fields in the section header. */
1531 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1534 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1536 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1537 because of a special case need for generating separate debug info
1538 files. See below for more details. */
1540 || (oheader
->sh_type
!= SHT_NOBITS
1541 && oheader
->sh_type
< SHT_LOOS
))
1544 /* Ignore empty sections, and sections whose
1545 fields have already been initialised. */
1546 if (oheader
->sh_size
== 0
1547 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1550 /* Scan for the matching section in the input bfd.
1551 First we try for a direct mapping between the input and output sections. */
1552 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1554 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1556 if (iheader
== NULL
)
1559 if (oheader
->bfd_section
!= NULL
1560 && iheader
->bfd_section
!= NULL
1561 && iheader
->bfd_section
->output_section
!= NULL
1562 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1564 /* We have found a connection from the input section to the
1565 output section. Attempt to copy the header fields. If
1566 this fails then do not try any further sections - there
1567 should only be a one-to-one mapping between input and output. */
1568 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1569 j
= elf_numsections (ibfd
);
1574 if (j
< elf_numsections (ibfd
))
1577 /* That failed. So try to deduce the corresponding input section.
1578 Unfortunately we cannot compare names as the output string table
1579 is empty, so instead we check size, address and type. */
1580 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1582 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1584 if (iheader
== NULL
)
1587 /* Try matching fields in the input section's header.
1588 Since --only-keep-debug turns all non-debug sections into
1589 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1591 if ((oheader
->sh_type
== SHT_NOBITS
1592 || iheader
->sh_type
== oheader
->sh_type
)
1593 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1594 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1595 && iheader
->sh_addralign
== oheader
->sh_addralign
1596 && iheader
->sh_entsize
== oheader
->sh_entsize
1597 && iheader
->sh_size
== oheader
->sh_size
1598 && iheader
->sh_addr
== oheader
->sh_addr
1599 && (iheader
->sh_info
!= oheader
->sh_info
1600 || iheader
->sh_link
!= oheader
->sh_link
))
1602 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1607 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1609 /* Final attempt. Call the backend copy function
1610 with a NULL input section. */
1611 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1620 get_segment_type (unsigned int p_type
)
1625 case PT_NULL
: pt
= "NULL"; break;
1626 case PT_LOAD
: pt
= "LOAD"; break;
1627 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1628 case PT_INTERP
: pt
= "INTERP"; break;
1629 case PT_NOTE
: pt
= "NOTE"; break;
1630 case PT_SHLIB
: pt
= "SHLIB"; break;
1631 case PT_PHDR
: pt
= "PHDR"; break;
1632 case PT_TLS
: pt
= "TLS"; break;
1633 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1634 case PT_GNU_STACK
: pt
= "STACK"; break;
1635 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1636 default: pt
= NULL
; break;
1641 /* Print out the program headers. */
1644 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1646 FILE *f
= (FILE *) farg
;
1647 Elf_Internal_Phdr
*p
;
1649 bfd_byte
*dynbuf
= NULL
;
1651 p
= elf_tdata (abfd
)->phdr
;
1656 fprintf (f
, _("\nProgram Header:\n"));
1657 c
= elf_elfheader (abfd
)->e_phnum
;
1658 for (i
= 0; i
< c
; i
++, p
++)
1660 const char *pt
= get_segment_type (p
->p_type
);
1665 sprintf (buf
, "0x%lx", p
->p_type
);
1668 fprintf (f
, "%8s off 0x", pt
);
1669 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1670 fprintf (f
, " vaddr 0x");
1671 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1672 fprintf (f
, " paddr 0x");
1673 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1674 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1675 fprintf (f
, " filesz 0x");
1676 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1677 fprintf (f
, " memsz 0x");
1678 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1679 fprintf (f
, " flags %c%c%c",
1680 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1681 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1682 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1683 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1684 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1689 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1692 unsigned int elfsec
;
1693 unsigned long shlink
;
1694 bfd_byte
*extdyn
, *extdynend
;
1696 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1698 fprintf (f
, _("\nDynamic Section:\n"));
1700 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1703 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1704 if (elfsec
== SHN_BAD
)
1706 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1708 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1709 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1712 /* PR 17512: file: 6f427532. */
1713 if (s
->size
< extdynsize
)
1715 extdynend
= extdyn
+ s
->size
;
1716 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1718 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1720 Elf_Internal_Dyn dyn
;
1721 const char *name
= "";
1723 bfd_boolean stringp
;
1724 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1726 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1728 if (dyn
.d_tag
== DT_NULL
)
1735 if (bed
->elf_backend_get_target_dtag
)
1736 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1738 if (!strcmp (name
, ""))
1740 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1745 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1746 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1747 case DT_PLTGOT
: name
= "PLTGOT"; break;
1748 case DT_HASH
: name
= "HASH"; break;
1749 case DT_STRTAB
: name
= "STRTAB"; break;
1750 case DT_SYMTAB
: name
= "SYMTAB"; break;
1751 case DT_RELA
: name
= "RELA"; break;
1752 case DT_RELASZ
: name
= "RELASZ"; break;
1753 case DT_RELAENT
: name
= "RELAENT"; break;
1754 case DT_STRSZ
: name
= "STRSZ"; break;
1755 case DT_SYMENT
: name
= "SYMENT"; break;
1756 case DT_INIT
: name
= "INIT"; break;
1757 case DT_FINI
: name
= "FINI"; break;
1758 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1759 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1760 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1761 case DT_REL
: name
= "REL"; break;
1762 case DT_RELSZ
: name
= "RELSZ"; break;
1763 case DT_RELENT
: name
= "RELENT"; break;
1764 case DT_PLTREL
: name
= "PLTREL"; break;
1765 case DT_DEBUG
: name
= "DEBUG"; break;
1766 case DT_TEXTREL
: name
= "TEXTREL"; break;
1767 case DT_JMPREL
: name
= "JMPREL"; break;
1768 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1769 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1770 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1771 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1772 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1773 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1774 case DT_FLAGS
: name
= "FLAGS"; break;
1775 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1776 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1777 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1778 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1779 case DT_MOVEENT
: name
= "MOVEENT"; break;
1780 case DT_MOVESZ
: name
= "MOVESZ"; break;
1781 case DT_FEATURE
: name
= "FEATURE"; break;
1782 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1783 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1784 case DT_SYMINENT
: name
= "SYMINENT"; break;
1785 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1786 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1787 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1788 case DT_PLTPAD
: name
= "PLTPAD"; break;
1789 case DT_MOVETAB
: name
= "MOVETAB"; break;
1790 case DT_SYMINFO
: name
= "SYMINFO"; break;
1791 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1792 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1793 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1794 case DT_VERSYM
: name
= "VERSYM"; break;
1795 case DT_VERDEF
: name
= "VERDEF"; break;
1796 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1797 case DT_VERNEED
: name
= "VERNEED"; break;
1798 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1799 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1800 case DT_USED
: name
= "USED"; break;
1801 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1802 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1805 fprintf (f
, " %-20s ", name
);
1809 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1814 unsigned int tagv
= dyn
.d_un
.d_val
;
1816 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1819 fprintf (f
, "%s", string
);
1828 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1829 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1831 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1835 if (elf_dynverdef (abfd
) != 0)
1837 Elf_Internal_Verdef
*t
;
1839 fprintf (f
, _("\nVersion definitions:\n"));
1840 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1842 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1843 t
->vd_flags
, t
->vd_hash
,
1844 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1845 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1847 Elf_Internal_Verdaux
*a
;
1850 for (a
= t
->vd_auxptr
->vda_nextptr
;
1854 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1860 if (elf_dynverref (abfd
) != 0)
1862 Elf_Internal_Verneed
*t
;
1864 fprintf (f
, _("\nVersion References:\n"));
1865 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1867 Elf_Internal_Vernaux
*a
;
1869 fprintf (f
, _(" required from %s:\n"),
1870 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1871 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1872 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1873 a
->vna_flags
, a
->vna_other
,
1874 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1886 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1887 and return symbol version for symbol version itself. */
1890 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1892 bfd_boolean
*hidden
)
1894 const char *version_string
= NULL
;
1895 if (elf_dynversym (abfd
) != 0
1896 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1898 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1900 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1901 vernum
&= VERSYM_VERSION
;
1904 version_string
= "";
1905 else if (vernum
== 1
1906 && (vernum
> elf_tdata (abfd
)->cverdefs
1907 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1909 version_string
= base_p
? "Base" : "";
1910 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1912 const char *nodename
1913 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1914 version_string
= "";
1917 || symbol
->name
== NULL
1918 || strcmp (symbol
->name
, nodename
) != 0)
1919 version_string
= nodename
;
1923 Elf_Internal_Verneed
*t
;
1925 version_string
= _("<corrupt>");
1926 for (t
= elf_tdata (abfd
)->verref
;
1930 Elf_Internal_Vernaux
*a
;
1932 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1934 if (a
->vna_other
== vernum
)
1936 version_string
= a
->vna_nodename
;
1943 return version_string
;
1946 /* Display ELF-specific fields of a symbol. */
1949 bfd_elf_print_symbol (bfd
*abfd
,
1952 bfd_print_symbol_type how
)
1954 FILE *file
= (FILE *) filep
;
1957 case bfd_print_symbol_name
:
1958 fprintf (file
, "%s", symbol
->name
);
1960 case bfd_print_symbol_more
:
1961 fprintf (file
, "elf ");
1962 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1963 fprintf (file
, " %x", symbol
->flags
);
1965 case bfd_print_symbol_all
:
1967 const char *section_name
;
1968 const char *name
= NULL
;
1969 const struct elf_backend_data
*bed
;
1970 unsigned char st_other
;
1972 const char *version_string
;
1975 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1977 bed
= get_elf_backend_data (abfd
);
1978 if (bed
->elf_backend_print_symbol_all
)
1979 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1983 name
= symbol
->name
;
1984 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1987 fprintf (file
, " %s\t", section_name
);
1988 /* Print the "other" value for a symbol. For common symbols,
1989 we've already printed the size; now print the alignment.
1990 For other symbols, we have no specified alignment, and
1991 we've printed the address; now print the size. */
1992 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1993 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1995 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1996 bfd_fprintf_vma (abfd
, file
, val
);
1998 /* If we have version information, print it. */
1999 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2006 fprintf (file
, " %-11s", version_string
);
2011 fprintf (file
, " (%s)", version_string
);
2012 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2017 /* If the st_other field is not zero, print it. */
2018 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2023 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2024 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2025 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2027 /* Some other non-defined flags are also present, so print
2029 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2032 fprintf (file
, " %s", name
);
2038 /* ELF .o/exec file reading */
2040 /* Create a new bfd section from an ELF section header. */
2043 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2045 Elf_Internal_Shdr
*hdr
;
2046 Elf_Internal_Ehdr
*ehdr
;
2047 const struct elf_backend_data
*bed
;
2049 bfd_boolean ret
= TRUE
;
2050 static bfd_boolean
* sections_being_created
= NULL
;
2051 static bfd
* sections_being_created_abfd
= NULL
;
2052 static unsigned int nesting
= 0;
2054 if (shindex
>= elf_numsections (abfd
))
2059 /* PR17512: A corrupt ELF binary might contain a recursive group of
2060 sections, with each the string indices pointing to the next in the
2061 loop. Detect this here, by refusing to load a section that we are
2062 already in the process of loading. We only trigger this test if
2063 we have nested at least three sections deep as normal ELF binaries
2064 can expect to recurse at least once.
2066 FIXME: It would be better if this array was attached to the bfd,
2067 rather than being held in a static pointer. */
2069 if (sections_being_created_abfd
!= abfd
)
2070 sections_being_created
= NULL
;
2071 if (sections_being_created
== NULL
)
2073 size_t amt
= elf_numsections (abfd
) * sizeof (bfd_boolean
);
2074 sections_being_created
= (bfd_boolean
*) bfd_zalloc (abfd
, amt
);
2075 if (sections_being_created
== NULL
)
2077 sections_being_created_abfd
= abfd
;
2079 if (sections_being_created
[shindex
])
2082 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2085 sections_being_created
[shindex
] = TRUE
;
2088 hdr
= elf_elfsections (abfd
)[shindex
];
2089 ehdr
= elf_elfheader (abfd
);
2090 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2095 bed
= get_elf_backend_data (abfd
);
2096 switch (hdr
->sh_type
)
2099 /* Inactive section. Throw it away. */
2102 case SHT_PROGBITS
: /* Normal section with contents. */
2103 case SHT_NOBITS
: /* .bss section. */
2104 case SHT_HASH
: /* .hash section. */
2105 case SHT_NOTE
: /* .note section. */
2106 case SHT_INIT_ARRAY
: /* .init_array section. */
2107 case SHT_FINI_ARRAY
: /* .fini_array section. */
2108 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2109 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2110 case SHT_GNU_HASH
: /* .gnu.hash section. */
2111 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2114 case SHT_DYNAMIC
: /* Dynamic linking information. */
2115 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2118 if (hdr
->sh_link
> elf_numsections (abfd
))
2120 /* PR 10478: Accept Solaris binaries with a sh_link
2121 field set to SHN_BEFORE or SHN_AFTER. */
2122 switch (bfd_get_arch (abfd
))
2125 case bfd_arch_sparc
:
2126 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2127 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2129 /* Otherwise fall through. */
2134 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2136 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2138 Elf_Internal_Shdr
*dynsymhdr
;
2140 /* The shared libraries distributed with hpux11 have a bogus
2141 sh_link field for the ".dynamic" section. Find the
2142 string table for the ".dynsym" section instead. */
2143 if (elf_dynsymtab (abfd
) != 0)
2145 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2146 hdr
->sh_link
= dynsymhdr
->sh_link
;
2150 unsigned int i
, num_sec
;
2152 num_sec
= elf_numsections (abfd
);
2153 for (i
= 1; i
< num_sec
; i
++)
2155 dynsymhdr
= elf_elfsections (abfd
)[i
];
2156 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2158 hdr
->sh_link
= dynsymhdr
->sh_link
;
2166 case SHT_SYMTAB
: /* A symbol table. */
2167 if (elf_onesymtab (abfd
) == shindex
)
2170 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2173 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2175 if (hdr
->sh_size
!= 0)
2177 /* Some assemblers erroneously set sh_info to one with a
2178 zero sh_size. ld sees this as a global symbol count
2179 of (unsigned) -1. Fix it here. */
2184 /* PR 18854: A binary might contain more than one symbol table.
2185 Unusual, but possible. Warn, but continue. */
2186 if (elf_onesymtab (abfd
) != 0)
2189 /* xgettext:c-format */
2190 (_("%pB: warning: multiple symbol tables detected"
2191 " - ignoring the table in section %u"),
2195 elf_onesymtab (abfd
) = shindex
;
2196 elf_symtab_hdr (abfd
) = *hdr
;
2197 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2198 abfd
->flags
|= HAS_SYMS
;
2200 /* Sometimes a shared object will map in the symbol table. If
2201 SHF_ALLOC is set, and this is a shared object, then we also
2202 treat this section as a BFD section. We can not base the
2203 decision purely on SHF_ALLOC, because that flag is sometimes
2204 set in a relocatable object file, which would confuse the
2206 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2207 && (abfd
->flags
& DYNAMIC
) != 0
2208 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2212 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2213 can't read symbols without that section loaded as well. It
2214 is most likely specified by the next section header. */
2216 elf_section_list
* entry
;
2217 unsigned int i
, num_sec
;
2219 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2220 if (entry
->hdr
.sh_link
== shindex
)
2223 num_sec
= elf_numsections (abfd
);
2224 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2226 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2228 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2229 && hdr2
->sh_link
== shindex
)
2234 for (i
= 1; i
< shindex
; i
++)
2236 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2238 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2239 && hdr2
->sh_link
== shindex
)
2244 ret
= bfd_section_from_shdr (abfd
, i
);
2245 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2249 case SHT_DYNSYM
: /* A dynamic symbol table. */
2250 if (elf_dynsymtab (abfd
) == shindex
)
2253 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2256 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2258 if (hdr
->sh_size
!= 0)
2261 /* Some linkers erroneously set sh_info to one with a
2262 zero sh_size. ld sees this as a global symbol count
2263 of (unsigned) -1. Fix it here. */
2268 /* PR 18854: A binary might contain more than one dynamic symbol table.
2269 Unusual, but possible. Warn, but continue. */
2270 if (elf_dynsymtab (abfd
) != 0)
2273 /* xgettext:c-format */
2274 (_("%pB: warning: multiple dynamic symbol tables detected"
2275 " - ignoring the table in section %u"),
2279 elf_dynsymtab (abfd
) = shindex
;
2280 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2281 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2282 abfd
->flags
|= HAS_SYMS
;
2284 /* Besides being a symbol table, we also treat this as a regular
2285 section, so that objcopy can handle it. */
2286 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2289 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2291 elf_section_list
* entry
;
2293 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2294 if (entry
->ndx
== shindex
)
2297 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2300 entry
->ndx
= shindex
;
2302 entry
->next
= elf_symtab_shndx_list (abfd
);
2303 elf_symtab_shndx_list (abfd
) = entry
;
2304 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2308 case SHT_STRTAB
: /* A string table. */
2309 if (hdr
->bfd_section
!= NULL
)
2312 if (ehdr
->e_shstrndx
== shindex
)
2314 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2315 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2319 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2322 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2323 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2327 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2330 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2331 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2332 elf_elfsections (abfd
)[shindex
] = hdr
;
2333 /* We also treat this as a regular section, so that objcopy
2335 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2340 /* If the string table isn't one of the above, then treat it as a
2341 regular section. We need to scan all the headers to be sure,
2342 just in case this strtab section appeared before the above. */
2343 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2345 unsigned int i
, num_sec
;
2347 num_sec
= elf_numsections (abfd
);
2348 for (i
= 1; i
< num_sec
; i
++)
2350 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2351 if (hdr2
->sh_link
== shindex
)
2353 /* Prevent endless recursion on broken objects. */
2356 if (! bfd_section_from_shdr (abfd
, i
))
2358 if (elf_onesymtab (abfd
) == i
)
2360 if (elf_dynsymtab (abfd
) == i
)
2361 goto dynsymtab_strtab
;
2365 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2370 /* *These* do a lot of work -- but build no sections! */
2372 asection
*target_sect
;
2373 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2374 unsigned int num_sec
= elf_numsections (abfd
);
2375 struct bfd_elf_section_data
*esdt
;
2378 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2379 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2382 /* Check for a bogus link to avoid crashing. */
2383 if (hdr
->sh_link
>= num_sec
)
2386 /* xgettext:c-format */
2387 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2388 abfd
, hdr
->sh_link
, name
, shindex
);
2389 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2394 /* For some incomprehensible reason Oracle distributes
2395 libraries for Solaris in which some of the objects have
2396 bogus sh_link fields. It would be nice if we could just
2397 reject them, but, unfortunately, some people need to use
2398 them. We scan through the section headers; if we find only
2399 one suitable symbol table, we clobber the sh_link to point
2400 to it. I hope this doesn't break anything.
2402 Don't do it on executable nor shared library. */
2403 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2404 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2405 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2411 for (scan
= 1; scan
< num_sec
; scan
++)
2413 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2414 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2425 hdr
->sh_link
= found
;
2428 /* Get the symbol table. */
2429 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2430 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2431 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2434 /* If this is an alloc section in an executable or shared
2435 library, or the reloc section does not use the main symbol
2436 table we don't treat it as a reloc section. BFD can't
2437 adequately represent such a section, so at least for now,
2438 we don't try. We just present it as a normal section. We
2439 also can't use it as a reloc section if it points to the
2440 null section, an invalid section, another reloc section, or
2441 its sh_link points to the null section. */
2442 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2443 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2444 || hdr
->sh_link
== SHN_UNDEF
2445 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2446 || hdr
->sh_info
== SHN_UNDEF
2447 || hdr
->sh_info
>= num_sec
2448 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2449 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2451 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2456 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2459 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2460 if (target_sect
== NULL
)
2463 esdt
= elf_section_data (target_sect
);
2464 if (hdr
->sh_type
== SHT_RELA
)
2465 p_hdr
= &esdt
->rela
.hdr
;
2467 p_hdr
= &esdt
->rel
.hdr
;
2469 /* PR 17512: file: 0b4f81b7.
2470 Also see PR 24456, for a file which deliberately has two reloc
2474 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2477 /* xgettext:c-format */
2478 (_("%pB: warning: secondary relocation section '%s' "
2479 "for section %pA found - ignoring"),
2480 abfd
, name
, target_sect
);
2485 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2490 elf_elfsections (abfd
)[shindex
] = hdr2
;
2491 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2492 * bed
->s
->int_rels_per_ext_rel
);
2493 target_sect
->flags
|= SEC_RELOC
;
2494 target_sect
->relocation
= NULL
;
2495 target_sect
->rel_filepos
= hdr
->sh_offset
;
2496 /* In the section to which the relocations apply, mark whether
2497 its relocations are of the REL or RELA variety. */
2498 if (hdr
->sh_size
!= 0)
2500 if (hdr
->sh_type
== SHT_RELA
)
2501 target_sect
->use_rela_p
= 1;
2503 abfd
->flags
|= HAS_RELOC
;
2507 case SHT_GNU_verdef
:
2508 elf_dynverdef (abfd
) = shindex
;
2509 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2510 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2513 case SHT_GNU_versym
:
2514 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2517 elf_dynversym (abfd
) = shindex
;
2518 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2519 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2522 case SHT_GNU_verneed
:
2523 elf_dynverref (abfd
) = shindex
;
2524 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2525 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2532 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2535 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2541 /* Possibly an attributes section. */
2542 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2543 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2545 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2547 _bfd_elf_parse_attributes (abfd
, hdr
);
2551 /* Check for any processor-specific section types. */
2552 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2555 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2557 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2558 /* FIXME: How to properly handle allocated section reserved
2559 for applications? */
2561 /* xgettext:c-format */
2562 (_("%pB: unknown type [%#x] section `%s'"),
2563 abfd
, hdr
->sh_type
, name
);
2566 /* Allow sections reserved for applications. */
2567 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2572 else if (hdr
->sh_type
>= SHT_LOPROC
2573 && hdr
->sh_type
<= SHT_HIPROC
)
2574 /* FIXME: We should handle this section. */
2576 /* xgettext:c-format */
2577 (_("%pB: unknown type [%#x] section `%s'"),
2578 abfd
, hdr
->sh_type
, name
);
2579 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2581 /* Unrecognised OS-specific sections. */
2582 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2583 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2584 required to correctly process the section and the file should
2585 be rejected with an error message. */
2587 /* xgettext:c-format */
2588 (_("%pB: unknown type [%#x] section `%s'"),
2589 abfd
, hdr
->sh_type
, name
);
2592 /* Otherwise it should be processed. */
2593 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2598 /* FIXME: We should handle this section. */
2600 /* xgettext:c-format */
2601 (_("%pB: unknown type [%#x] section `%s'"),
2602 abfd
, hdr
->sh_type
, name
);
2610 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2611 sections_being_created
[shindex
] = FALSE
;
2612 if (-- nesting
== 0)
2614 sections_being_created
= NULL
;
2615 sections_being_created_abfd
= abfd
;
2620 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2623 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2625 unsigned long r_symndx
)
2627 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2629 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2631 Elf_Internal_Shdr
*symtab_hdr
;
2632 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2633 Elf_External_Sym_Shndx eshndx
;
2635 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2636 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2637 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2640 if (cache
->abfd
!= abfd
)
2642 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2645 cache
->indx
[ent
] = r_symndx
;
2648 return &cache
->sym
[ent
];
2651 /* Given an ELF section number, retrieve the corresponding BFD
2655 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2657 if (sec_index
>= elf_numsections (abfd
))
2659 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2662 static const struct bfd_elf_special_section special_sections_b
[] =
2664 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2665 { NULL
, 0, 0, 0, 0 }
2668 static const struct bfd_elf_special_section special_sections_c
[] =
2670 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2671 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2672 { NULL
, 0, 0, 0, 0 }
2675 static const struct bfd_elf_special_section special_sections_d
[] =
2677 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2678 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2679 /* There are more DWARF sections than these, but they needn't be added here
2680 unless you have to cope with broken compilers that don't emit section
2681 attributes or you want to help the user writing assembler. */
2682 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2683 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2684 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2685 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2686 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2687 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2688 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2689 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2690 { NULL
, 0, 0, 0, 0 }
2693 static const struct bfd_elf_special_section special_sections_f
[] =
2695 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2696 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2697 { NULL
, 0 , 0, 0, 0 }
2700 static const struct bfd_elf_special_section special_sections_g
[] =
2702 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2703 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2704 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2705 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2706 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2707 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2708 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2709 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2710 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2711 { NULL
, 0, 0, 0, 0 }
2714 static const struct bfd_elf_special_section special_sections_h
[] =
2716 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2717 { NULL
, 0, 0, 0, 0 }
2720 static const struct bfd_elf_special_section special_sections_i
[] =
2722 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2723 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2724 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2725 { NULL
, 0, 0, 0, 0 }
2728 static const struct bfd_elf_special_section special_sections_l
[] =
2730 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2731 { NULL
, 0, 0, 0, 0 }
2734 static const struct bfd_elf_special_section special_sections_n
[] =
2736 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2737 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2738 { NULL
, 0, 0, 0, 0 }
2741 static const struct bfd_elf_special_section special_sections_p
[] =
2743 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2744 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2745 { NULL
, 0, 0, 0, 0 }
2748 static const struct bfd_elf_special_section special_sections_r
[] =
2750 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2751 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2752 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2753 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2754 { NULL
, 0, 0, 0, 0 }
2757 static const struct bfd_elf_special_section special_sections_s
[] =
2759 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2760 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2761 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2762 /* See struct bfd_elf_special_section declaration for the semantics of
2763 this special case where .prefix_length != strlen (.prefix). */
2764 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2765 { NULL
, 0, 0, 0, 0 }
2768 static const struct bfd_elf_special_section special_sections_t
[] =
2770 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2771 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2772 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2773 { NULL
, 0, 0, 0, 0 }
2776 static const struct bfd_elf_special_section special_sections_z
[] =
2778 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2779 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2780 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2781 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2782 { NULL
, 0, 0, 0, 0 }
2785 static const struct bfd_elf_special_section
* const special_sections
[] =
2787 special_sections_b
, /* 'b' */
2788 special_sections_c
, /* 'c' */
2789 special_sections_d
, /* 'd' */
2791 special_sections_f
, /* 'f' */
2792 special_sections_g
, /* 'g' */
2793 special_sections_h
, /* 'h' */
2794 special_sections_i
, /* 'i' */
2797 special_sections_l
, /* 'l' */
2799 special_sections_n
, /* 'n' */
2801 special_sections_p
, /* 'p' */
2803 special_sections_r
, /* 'r' */
2804 special_sections_s
, /* 's' */
2805 special_sections_t
, /* 't' */
2811 special_sections_z
/* 'z' */
2814 const struct bfd_elf_special_section
*
2815 _bfd_elf_get_special_section (const char *name
,
2816 const struct bfd_elf_special_section
*spec
,
2822 len
= strlen (name
);
2824 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2827 int prefix_len
= spec
[i
].prefix_length
;
2829 if (len
< prefix_len
)
2831 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2834 suffix_len
= spec
[i
].suffix_length
;
2835 if (suffix_len
<= 0)
2837 if (name
[prefix_len
] != 0)
2839 if (suffix_len
== 0)
2841 if (name
[prefix_len
] != '.'
2842 && (suffix_len
== -2
2843 || (rela
&& spec
[i
].type
== SHT_REL
)))
2849 if (len
< prefix_len
+ suffix_len
)
2851 if (memcmp (name
+ len
- suffix_len
,
2852 spec
[i
].prefix
+ prefix_len
,
2862 const struct bfd_elf_special_section
*
2863 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2866 const struct bfd_elf_special_section
*spec
;
2867 const struct elf_backend_data
*bed
;
2869 /* See if this is one of the special sections. */
2870 if (sec
->name
== NULL
)
2873 bed
= get_elf_backend_data (abfd
);
2874 spec
= bed
->special_sections
;
2877 spec
= _bfd_elf_get_special_section (sec
->name
,
2878 bed
->special_sections
,
2884 if (sec
->name
[0] != '.')
2887 i
= sec
->name
[1] - 'b';
2888 if (i
< 0 || i
> 'z' - 'b')
2891 spec
= special_sections
[i
];
2896 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2900 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2902 struct bfd_elf_section_data
*sdata
;
2903 const struct elf_backend_data
*bed
;
2904 const struct bfd_elf_special_section
*ssect
;
2906 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2909 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2913 sec
->used_by_bfd
= sdata
;
2916 /* Indicate whether or not this section should use RELA relocations. */
2917 bed
= get_elf_backend_data (abfd
);
2918 sec
->use_rela_p
= bed
->default_use_rela_p
;
2920 /* Set up ELF section type and flags for newly created sections, if
2921 there is an ABI mandated section. */
2922 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2925 elf_section_type (sec
) = ssect
->type
;
2926 elf_section_flags (sec
) = ssect
->attr
;
2929 return _bfd_generic_new_section_hook (abfd
, sec
);
2932 /* Create a new bfd section from an ELF program header.
2934 Since program segments have no names, we generate a synthetic name
2935 of the form segment<NUM>, where NUM is generally the index in the
2936 program header table. For segments that are split (see below) we
2937 generate the names segment<NUM>a and segment<NUM>b.
2939 Note that some program segments may have a file size that is different than
2940 (less than) the memory size. All this means is that at execution the
2941 system must allocate the amount of memory specified by the memory size,
2942 but only initialize it with the first "file size" bytes read from the
2943 file. This would occur for example, with program segments consisting
2944 of combined data+bss.
2946 To handle the above situation, this routine generates TWO bfd sections
2947 for the single program segment. The first has the length specified by
2948 the file size of the segment, and the second has the length specified
2949 by the difference between the two sizes. In effect, the segment is split
2950 into its initialized and uninitialized parts.
2955 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2956 Elf_Internal_Phdr
*hdr
,
2958 const char *type_name
)
2965 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
2967 split
= ((hdr
->p_memsz
> 0)
2968 && (hdr
->p_filesz
> 0)
2969 && (hdr
->p_memsz
> hdr
->p_filesz
));
2971 if (hdr
->p_filesz
> 0)
2973 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2974 len
= strlen (namebuf
) + 1;
2975 name
= (char *) bfd_alloc (abfd
, len
);
2978 memcpy (name
, namebuf
, len
);
2979 newsect
= bfd_make_section (abfd
, name
);
2980 if (newsect
== NULL
)
2982 newsect
->vma
= hdr
->p_vaddr
/ opb
;
2983 newsect
->lma
= hdr
->p_paddr
/ opb
;
2984 newsect
->size
= hdr
->p_filesz
;
2985 newsect
->filepos
= hdr
->p_offset
;
2986 newsect
->flags
|= SEC_HAS_CONTENTS
;
2987 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2988 if (hdr
->p_type
== PT_LOAD
)
2990 newsect
->flags
|= SEC_ALLOC
;
2991 newsect
->flags
|= SEC_LOAD
;
2992 if (hdr
->p_flags
& PF_X
)
2994 /* FIXME: all we known is that it has execute PERMISSION,
2996 newsect
->flags
|= SEC_CODE
;
2999 if (!(hdr
->p_flags
& PF_W
))
3001 newsect
->flags
|= SEC_READONLY
;
3005 if (hdr
->p_memsz
> hdr
->p_filesz
)
3009 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3010 len
= strlen (namebuf
) + 1;
3011 name
= (char *) bfd_alloc (abfd
, len
);
3014 memcpy (name
, namebuf
, len
);
3015 newsect
= bfd_make_section (abfd
, name
);
3016 if (newsect
== NULL
)
3018 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3019 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3020 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3021 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3022 align
= newsect
->vma
& -newsect
->vma
;
3023 if (align
== 0 || align
> hdr
->p_align
)
3024 align
= hdr
->p_align
;
3025 newsect
->alignment_power
= bfd_log2 (align
);
3026 if (hdr
->p_type
== PT_LOAD
)
3028 /* Hack for gdb. Segments that have not been modified do
3029 not have their contents written to a core file, on the
3030 assumption that a debugger can find the contents in the
3031 executable. We flag this case by setting the fake
3032 section size to zero. Note that "real" bss sections will
3033 always have their contents dumped to the core file. */
3034 if (bfd_get_format (abfd
) == bfd_core
)
3036 newsect
->flags
|= SEC_ALLOC
;
3037 if (hdr
->p_flags
& PF_X
)
3038 newsect
->flags
|= SEC_CODE
;
3040 if (!(hdr
->p_flags
& PF_W
))
3041 newsect
->flags
|= SEC_READONLY
;
3048 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3050 /* The return value is ignored. Build-ids are considered optional. */
3051 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3052 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3058 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3060 const struct elf_backend_data
*bed
;
3062 switch (hdr
->p_type
)
3065 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3068 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3070 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3071 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3075 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3078 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3081 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3083 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3089 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3092 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3094 case PT_GNU_EH_FRAME
:
3095 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3099 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3102 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3105 /* Check for any processor-specific program segment types. */
3106 bed
= get_elf_backend_data (abfd
);
3107 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3111 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3115 _bfd_elf_single_rel_hdr (asection
*sec
)
3117 if (elf_section_data (sec
)->rel
.hdr
)
3119 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3120 return elf_section_data (sec
)->rel
.hdr
;
3123 return elf_section_data (sec
)->rela
.hdr
;
3127 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3128 Elf_Internal_Shdr
*rel_hdr
,
3129 const char *sec_name
,
3130 bfd_boolean use_rela_p
)
3132 char *name
= (char *) bfd_alloc (abfd
,
3133 sizeof ".rela" + strlen (sec_name
));
3137 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3139 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3141 if (rel_hdr
->sh_name
== (unsigned int) -1)
3147 /* Allocate and initialize a section-header for a new reloc section,
3148 containing relocations against ASECT. It is stored in RELDATA. If
3149 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3153 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3154 struct bfd_elf_section_reloc_data
*reldata
,
3155 const char *sec_name
,
3156 bfd_boolean use_rela_p
,
3157 bfd_boolean delay_st_name_p
)
3159 Elf_Internal_Shdr
*rel_hdr
;
3160 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3162 BFD_ASSERT (reldata
->hdr
== NULL
);
3163 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3164 reldata
->hdr
= rel_hdr
;
3166 if (delay_st_name_p
)
3167 rel_hdr
->sh_name
= (unsigned int) -1;
3168 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3171 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3172 rel_hdr
->sh_entsize
= (use_rela_p
3173 ? bed
->s
->sizeof_rela
3174 : bed
->s
->sizeof_rel
);
3175 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3176 rel_hdr
->sh_flags
= 0;
3177 rel_hdr
->sh_addr
= 0;
3178 rel_hdr
->sh_size
= 0;
3179 rel_hdr
->sh_offset
= 0;
3184 /* Return the default section type based on the passed in section flags. */
3187 bfd_elf_get_default_section_type (flagword flags
)
3189 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3190 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3192 return SHT_PROGBITS
;
3195 struct fake_section_arg
3197 struct bfd_link_info
*link_info
;
3201 /* Set up an ELF internal section header for a section. */
3204 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3206 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3207 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3208 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3209 Elf_Internal_Shdr
*this_hdr
;
3210 unsigned int sh_type
;
3211 const char *name
= asect
->name
;
3212 bfd_boolean delay_st_name_p
= FALSE
;
3217 /* We already failed; just get out of the bfd_map_over_sections
3222 this_hdr
= &esd
->this_hdr
;
3226 /* ld: compress DWARF debug sections with names: .debug_*. */
3227 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3228 && (asect
->flags
& SEC_DEBUGGING
)
3232 /* Set SEC_ELF_COMPRESS to indicate this section should be
3234 asect
->flags
|= SEC_ELF_COMPRESS
;
3235 /* If this section will be compressed, delay adding section
3236 name to section name section after it is compressed in
3237 _bfd_elf_assign_file_positions_for_non_load. */
3238 delay_st_name_p
= TRUE
;
3241 else if ((asect
->flags
& SEC_ELF_RENAME
))
3243 /* objcopy: rename output DWARF debug section. */
3244 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3246 /* When we decompress or compress with SHF_COMPRESSED,
3247 convert section name from .zdebug_* to .debug_* if
3251 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3252 if (new_name
== NULL
)
3260 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3262 /* PR binutils/18087: Compression does not always make a
3263 section smaller. So only rename the section when
3264 compression has actually taken place. If input section
3265 name is .zdebug_*, we should never compress it again. */
3266 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3267 if (new_name
== NULL
)
3272 BFD_ASSERT (name
[1] != 'z');
3277 if (delay_st_name_p
)
3278 this_hdr
->sh_name
= (unsigned int) -1;
3282 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3284 if (this_hdr
->sh_name
== (unsigned int) -1)
3291 /* Don't clear sh_flags. Assembler may set additional bits. */
3293 if ((asect
->flags
& SEC_ALLOC
) != 0
3294 || asect
->user_set_vma
)
3295 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3297 this_hdr
->sh_addr
= 0;
3299 this_hdr
->sh_offset
= 0;
3300 this_hdr
->sh_size
= asect
->size
;
3301 this_hdr
->sh_link
= 0;
3302 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3303 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3306 /* xgettext:c-format */
3307 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3308 abfd
, asect
->alignment_power
, asect
);
3312 /* Set sh_addralign to the highest power of two given by alignment
3313 consistent with the section VMA. Linker scripts can force VMA. */
3314 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3315 this_hdr
->sh_addralign
= mask
& -mask
;
3316 /* The sh_entsize and sh_info fields may have been set already by
3317 copy_private_section_data. */
3319 this_hdr
->bfd_section
= asect
;
3320 this_hdr
->contents
= NULL
;
3322 /* If the section type is unspecified, we set it based on
3324 if ((asect
->flags
& SEC_GROUP
) != 0)
3325 sh_type
= SHT_GROUP
;
3327 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3329 if (this_hdr
->sh_type
== SHT_NULL
)
3330 this_hdr
->sh_type
= sh_type
;
3331 else if (this_hdr
->sh_type
== SHT_NOBITS
3332 && sh_type
== SHT_PROGBITS
3333 && (asect
->flags
& SEC_ALLOC
) != 0)
3335 /* Warn if we are changing a NOBITS section to PROGBITS, but
3336 allow the link to proceed. This can happen when users link
3337 non-bss input sections to bss output sections, or emit data
3338 to a bss output section via a linker script. */
3340 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3341 this_hdr
->sh_type
= sh_type
;
3344 switch (this_hdr
->sh_type
)
3355 case SHT_INIT_ARRAY
:
3356 case SHT_FINI_ARRAY
:
3357 case SHT_PREINIT_ARRAY
:
3358 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3362 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3366 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3370 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3374 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3375 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3379 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3380 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3383 case SHT_GNU_versym
:
3384 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3387 case SHT_GNU_verdef
:
3388 this_hdr
->sh_entsize
= 0;
3389 /* objcopy or strip will copy over sh_info, but may not set
3390 cverdefs. The linker will set cverdefs, but sh_info will be
3392 if (this_hdr
->sh_info
== 0)
3393 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3395 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3396 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3399 case SHT_GNU_verneed
:
3400 this_hdr
->sh_entsize
= 0;
3401 /* objcopy or strip will copy over sh_info, but may not set
3402 cverrefs. The linker will set cverrefs, but sh_info will be
3404 if (this_hdr
->sh_info
== 0)
3405 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3407 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3408 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3412 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3416 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3420 if ((asect
->flags
& SEC_ALLOC
) != 0)
3421 this_hdr
->sh_flags
|= SHF_ALLOC
;
3422 if ((asect
->flags
& SEC_READONLY
) == 0)
3423 this_hdr
->sh_flags
|= SHF_WRITE
;
3424 if ((asect
->flags
& SEC_CODE
) != 0)
3425 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3426 if ((asect
->flags
& SEC_MERGE
) != 0)
3428 this_hdr
->sh_flags
|= SHF_MERGE
;
3429 this_hdr
->sh_entsize
= asect
->entsize
;
3431 if ((asect
->flags
& SEC_STRINGS
) != 0)
3432 this_hdr
->sh_flags
|= SHF_STRINGS
;
3433 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3434 this_hdr
->sh_flags
|= SHF_GROUP
;
3435 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3437 this_hdr
->sh_flags
|= SHF_TLS
;
3438 if (asect
->size
== 0
3439 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3441 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3443 this_hdr
->sh_size
= 0;
3446 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3447 if (this_hdr
->sh_size
!= 0)
3448 this_hdr
->sh_type
= SHT_NOBITS
;
3452 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3453 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3455 /* If the section has relocs, set up a section header for the
3456 SHT_REL[A] section. If two relocation sections are required for
3457 this section, it is up to the processor-specific back-end to
3458 create the other. */
3459 if ((asect
->flags
& SEC_RELOC
) != 0)
3461 /* When doing a relocatable link, create both REL and RELA sections if
3464 /* Do the normal setup if we wouldn't create any sections here. */
3465 && esd
->rel
.count
+ esd
->rela
.count
> 0
3466 && (bfd_link_relocatable (arg
->link_info
)
3467 || arg
->link_info
->emitrelocations
))
3469 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3470 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3471 FALSE
, delay_st_name_p
))
3476 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3477 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3478 TRUE
, delay_st_name_p
))
3484 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3486 ? &esd
->rela
: &esd
->rel
),
3496 /* Check for processor-specific section types. */
3497 sh_type
= this_hdr
->sh_type
;
3498 if (bed
->elf_backend_fake_sections
3499 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3505 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3507 /* Don't change the header type from NOBITS if we are being
3508 called for objcopy --only-keep-debug. */
3509 this_hdr
->sh_type
= sh_type
;
3513 /* Fill in the contents of a SHT_GROUP section. Called from
3514 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3515 when ELF targets use the generic linker, ld. Called for ld -r
3516 from bfd_elf_final_link. */
3519 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3521 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3522 asection
*elt
, *first
;
3526 /* Ignore linker created group section. See elfNN_ia64_object_p in
3528 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3533 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3535 unsigned long symindx
= 0;
3537 /* elf_group_id will have been set up by objcopy and the
3539 if (elf_group_id (sec
) != NULL
)
3540 symindx
= elf_group_id (sec
)->udata
.i
;
3544 /* If called from the assembler, swap_out_syms will have set up
3546 PR 25699: A corrupt input file could contain bogus group info. */
3547 if (elf_section_syms (abfd
) == NULL
)
3552 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3554 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3556 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3558 /* The ELF backend linker sets sh_info to -2 when the group
3559 signature symbol is global, and thus the index can't be
3560 set until all local symbols are output. */
3562 struct bfd_elf_section_data
*sec_data
;
3563 unsigned long symndx
;
3564 unsigned long extsymoff
;
3565 struct elf_link_hash_entry
*h
;
3567 /* The point of this little dance to the first SHF_GROUP section
3568 then back to the SHT_GROUP section is that this gets us to
3569 the SHT_GROUP in the input object. */
3570 igroup
= elf_sec_group (elf_next_in_group (sec
));
3571 sec_data
= elf_section_data (igroup
);
3572 symndx
= sec_data
->this_hdr
.sh_info
;
3574 if (!elf_bad_symtab (igroup
->owner
))
3576 Elf_Internal_Shdr
*symtab_hdr
;
3578 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3579 extsymoff
= symtab_hdr
->sh_info
;
3581 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3582 while (h
->root
.type
== bfd_link_hash_indirect
3583 || h
->root
.type
== bfd_link_hash_warning
)
3584 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3586 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3589 /* The contents won't be allocated for "ld -r" or objcopy. */
3591 if (sec
->contents
== NULL
)
3594 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3596 /* Arrange for the section to be written out. */
3597 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3598 if (sec
->contents
== NULL
)
3605 loc
= sec
->contents
+ sec
->size
;
3607 /* Get the pointer to the first section in the group that gas
3608 squirreled away here. objcopy arranges for this to be set to the
3609 start of the input section group. */
3610 first
= elt
= elf_next_in_group (sec
);
3612 /* First element is a flag word. Rest of section is elf section
3613 indices for all the sections of the group. Write them backwards
3614 just to keep the group in the same order as given in .section
3615 directives, not that it matters. */
3622 s
= s
->output_section
;
3624 && !bfd_is_abs_section (s
))
3626 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3627 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3629 if (elf_sec
->rel
.hdr
!= NULL
3631 || (input_elf_sec
->rel
.hdr
!= NULL
3632 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3634 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3636 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3638 if (elf_sec
->rela
.hdr
!= NULL
3640 || (input_elf_sec
->rela
.hdr
!= NULL
3641 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3643 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3645 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3648 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3650 elt
= elf_next_in_group (elt
);
3656 BFD_ASSERT (loc
== sec
->contents
);
3658 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3661 /* Given NAME, the name of a relocation section stripped of its
3662 .rel/.rela prefix, return the section in ABFD to which the
3663 relocations apply. */
3666 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3668 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3669 section likely apply to .got.plt or .got section. */
3670 if (get_elf_backend_data (abfd
)->want_got_plt
3671 && strcmp (name
, ".plt") == 0)
3676 sec
= bfd_get_section_by_name (abfd
, name
);
3682 return bfd_get_section_by_name (abfd
, name
);
3685 /* Return the section to which RELOC_SEC applies. */
3688 elf_get_reloc_section (asection
*reloc_sec
)
3693 const struct elf_backend_data
*bed
;
3695 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3696 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3699 /* We look up the section the relocs apply to by name. */
3700 name
= reloc_sec
->name
;
3701 if (strncmp (name
, ".rel", 4) != 0)
3704 if (type
== SHT_RELA
&& *name
++ != 'a')
3707 abfd
= reloc_sec
->owner
;
3708 bed
= get_elf_backend_data (abfd
);
3709 return bed
->get_reloc_section (abfd
, name
);
3712 /* Assign all ELF section numbers. The dummy first section is handled here
3713 too. The link/info pointers for the standard section types are filled
3714 in here too, while we're at it. */
3717 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3719 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3721 unsigned int section_number
;
3722 Elf_Internal_Shdr
**i_shdrp
;
3723 struct bfd_elf_section_data
*d
;
3724 bfd_boolean need_symtab
;
3729 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3731 /* SHT_GROUP sections are in relocatable files only. */
3732 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3734 size_t reloc_count
= 0;
3736 /* Put SHT_GROUP sections first. */
3737 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3739 d
= elf_section_data (sec
);
3741 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3743 if (sec
->flags
& SEC_LINKER_CREATED
)
3745 /* Remove the linker created SHT_GROUP sections. */
3746 bfd_section_list_remove (abfd
, sec
);
3747 abfd
->section_count
--;
3750 d
->this_idx
= section_number
++;
3753 /* Count relocations. */
3754 reloc_count
+= sec
->reloc_count
;
3757 /* Clear HAS_RELOC if there are no relocations. */
3758 if (reloc_count
== 0)
3759 abfd
->flags
&= ~HAS_RELOC
;
3762 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3764 d
= elf_section_data (sec
);
3766 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3767 d
->this_idx
= section_number
++;
3768 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3769 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3772 d
->rel
.idx
= section_number
++;
3773 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3774 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3781 d
->rela
.idx
= section_number
++;
3782 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3783 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3789 need_symtab
= (bfd_get_symcount (abfd
) > 0
3790 || (link_info
== NULL
3791 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3795 elf_onesymtab (abfd
) = section_number
++;
3796 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3797 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3799 elf_section_list
*entry
;
3801 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3803 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3804 entry
->ndx
= section_number
++;
3805 elf_symtab_shndx_list (abfd
) = entry
;
3807 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3808 ".symtab_shndx", FALSE
);
3809 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3812 elf_strtab_sec (abfd
) = section_number
++;
3813 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3816 elf_shstrtab_sec (abfd
) = section_number
++;
3817 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3818 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3820 if (section_number
>= SHN_LORESERVE
)
3822 /* xgettext:c-format */
3823 _bfd_error_handler (_("%pB: too many sections: %u"),
3824 abfd
, section_number
);
3828 elf_numsections (abfd
) = section_number
;
3829 elf_elfheader (abfd
)->e_shnum
= section_number
;
3831 /* Set up the list of section header pointers, in agreement with the
3833 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3834 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3835 if (i_shdrp
== NULL
)
3838 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3839 sizeof (Elf_Internal_Shdr
));
3840 if (i_shdrp
[0] == NULL
)
3842 bfd_release (abfd
, i_shdrp
);
3846 elf_elfsections (abfd
) = i_shdrp
;
3848 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3851 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3852 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3854 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3855 BFD_ASSERT (entry
!= NULL
);
3856 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3857 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3859 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3860 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3863 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3867 d
= elf_section_data (sec
);
3869 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3870 if (d
->rel
.idx
!= 0)
3871 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3872 if (d
->rela
.idx
!= 0)
3873 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3875 /* Fill in the sh_link and sh_info fields while we're at it. */
3877 /* sh_link of a reloc section is the section index of the symbol
3878 table. sh_info is the section index of the section to which
3879 the relocation entries apply. */
3880 if (d
->rel
.idx
!= 0)
3882 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3883 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3884 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3886 if (d
->rela
.idx
!= 0)
3888 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3889 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3890 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3893 /* We need to set up sh_link for SHF_LINK_ORDER. */
3894 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3896 s
= elf_linked_to_section (sec
);
3899 /* elf_linked_to_section points to the input section. */
3900 if (link_info
!= NULL
)
3902 /* Check discarded linkonce section. */
3903 if (discarded_section (s
))
3907 /* xgettext:c-format */
3908 (_("%pB: sh_link of section `%pA' points to"
3909 " discarded section `%pA' of `%pB'"),
3910 abfd
, d
->this_hdr
.bfd_section
,
3912 /* Point to the kept section if it has the same
3913 size as the discarded one. */
3914 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3917 bfd_set_error (bfd_error_bad_value
);
3923 s
= s
->output_section
;
3924 BFD_ASSERT (s
!= NULL
);
3928 /* Handle objcopy. */
3929 if (s
->output_section
== NULL
)
3932 /* xgettext:c-format */
3933 (_("%pB: sh_link of section `%pA' points to"
3934 " removed section `%pA' of `%pB'"),
3935 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3936 bfd_set_error (bfd_error_bad_value
);
3939 s
= s
->output_section
;
3941 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3946 The Intel C compiler generates SHT_IA_64_UNWIND with
3947 SHF_LINK_ORDER. But it doesn't set the sh_link or
3948 sh_info fields. Hence we could get the situation
3950 const struct elf_backend_data
*bed
3951 = get_elf_backend_data (abfd
);
3952 bed
->link_order_error_handler
3953 /* xgettext:c-format */
3954 (_("%pB: warning: sh_link not set for section `%pA'"),
3959 switch (d
->this_hdr
.sh_type
)
3963 /* A reloc section which we are treating as a normal BFD
3964 section. sh_link is the section index of the symbol
3965 table. sh_info is the section index of the section to
3966 which the relocation entries apply. We assume that an
3967 allocated reloc section uses the dynamic symbol table.
3968 FIXME: How can we be sure? */
3969 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3971 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3973 s
= elf_get_reloc_section (sec
);
3976 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3977 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3982 /* We assume that a section named .stab*str is a stabs
3983 string section. We look for a section with the same name
3984 but without the trailing ``str'', and set its sh_link
3985 field to point to this section. */
3986 if (CONST_STRNEQ (sec
->name
, ".stab")
3987 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3992 len
= strlen (sec
->name
);
3993 alc
= (char *) bfd_malloc (len
- 2);
3996 memcpy (alc
, sec
->name
, len
- 3);
3997 alc
[len
- 3] = '\0';
3998 s
= bfd_get_section_by_name (abfd
, alc
);
4002 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
4004 /* This is a .stab section. */
4005 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
4012 case SHT_GNU_verneed
:
4013 case SHT_GNU_verdef
:
4014 /* sh_link is the section header index of the string table
4015 used for the dynamic entries, or the symbol table, or the
4017 s
= bfd_get_section_by_name (abfd
, ".dynstr");
4019 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4022 case SHT_GNU_LIBLIST
:
4023 /* sh_link is the section header index of the prelink library
4024 list used for the dynamic entries, or the symbol table, or
4025 the version strings. */
4026 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
4027 ? ".dynstr" : ".gnu.libstr");
4029 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4034 case SHT_GNU_versym
:
4035 /* sh_link is the section header index of the symbol table
4036 this hash table or version table is for. */
4037 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4039 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4043 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4047 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4048 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4049 debug section name from .debug_* to .zdebug_* if needed. */
4055 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4057 /* If the backend has a special mapping, use it. */
4058 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4059 if (bed
->elf_backend_sym_is_global
)
4060 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4062 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4063 || bfd_is_und_section (bfd_asymbol_section (sym
))
4064 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4067 /* Filter global symbols of ABFD to include in the import library. All
4068 SYMCOUNT symbols of ABFD can be examined from their pointers in
4069 SYMS. Pointers of symbols to keep should be stored contiguously at
4070 the beginning of that array.
4072 Returns the number of symbols to keep. */
4075 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4076 asymbol
**syms
, long symcount
)
4078 long src_count
, dst_count
= 0;
4080 for (src_count
= 0; src_count
< symcount
; src_count
++)
4082 asymbol
*sym
= syms
[src_count
];
4083 char *name
= (char *) bfd_asymbol_name (sym
);
4084 struct bfd_link_hash_entry
*h
;
4086 if (!sym_is_global (abfd
, sym
))
4089 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4092 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4094 if (h
->linker_def
|| h
->ldscript_def
)
4097 syms
[dst_count
++] = sym
;
4100 syms
[dst_count
] = NULL
;
4105 /* Don't output section symbols for sections that are not going to be
4106 output, that are duplicates or there is no BFD section. */
4109 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4111 elf_symbol_type
*type_ptr
;
4116 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4119 if (sym
->section
== NULL
)
4122 type_ptr
= elf_symbol_from (abfd
, sym
);
4123 return ((type_ptr
!= NULL
4124 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4125 && bfd_is_abs_section (sym
->section
))
4126 || !(sym
->section
->owner
== abfd
4127 || (sym
->section
->output_section
!= NULL
4128 && sym
->section
->output_section
->owner
== abfd
4129 && sym
->section
->output_offset
== 0)
4130 || bfd_is_abs_section (sym
->section
)));
4133 /* Map symbol from it's internal number to the external number, moving
4134 all local symbols to be at the head of the list. */
4137 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4139 unsigned int symcount
= bfd_get_symcount (abfd
);
4140 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4141 asymbol
**sect_syms
;
4142 unsigned int num_locals
= 0;
4143 unsigned int num_globals
= 0;
4144 unsigned int num_locals2
= 0;
4145 unsigned int num_globals2
= 0;
4146 unsigned int max_index
= 0;
4153 fprintf (stderr
, "elf_map_symbols\n");
4157 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4159 if (max_index
< asect
->index
)
4160 max_index
= asect
->index
;
4164 amt
= max_index
* sizeof (asymbol
*);
4165 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4166 if (sect_syms
== NULL
)
4168 elf_section_syms (abfd
) = sect_syms
;
4169 elf_num_section_syms (abfd
) = max_index
;
4171 /* Init sect_syms entries for any section symbols we have already
4172 decided to output. */
4173 for (idx
= 0; idx
< symcount
; idx
++)
4175 asymbol
*sym
= syms
[idx
];
4177 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4179 && !ignore_section_sym (abfd
, sym
)
4180 && !bfd_is_abs_section (sym
->section
))
4182 asection
*sec
= sym
->section
;
4184 if (sec
->owner
!= abfd
)
4185 sec
= sec
->output_section
;
4187 sect_syms
[sec
->index
] = syms
[idx
];
4191 /* Classify all of the symbols. */
4192 for (idx
= 0; idx
< symcount
; idx
++)
4194 if (sym_is_global (abfd
, syms
[idx
]))
4196 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4200 /* We will be adding a section symbol for each normal BFD section. Most
4201 sections will already have a section symbol in outsymbols, but
4202 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4203 at least in that case. */
4204 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4206 if (sect_syms
[asect
->index
] == NULL
)
4208 if (!sym_is_global (abfd
, asect
->symbol
))
4215 /* Now sort the symbols so the local symbols are first. */
4216 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4217 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4218 if (new_syms
== NULL
)
4221 for (idx
= 0; idx
< symcount
; idx
++)
4223 asymbol
*sym
= syms
[idx
];
4226 if (sym_is_global (abfd
, sym
))
4227 i
= num_locals
+ num_globals2
++;
4228 else if (!ignore_section_sym (abfd
, sym
))
4233 sym
->udata
.i
= i
+ 1;
4235 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4237 if (sect_syms
[asect
->index
] == NULL
)
4239 asymbol
*sym
= asect
->symbol
;
4242 sect_syms
[asect
->index
] = sym
;
4243 if (!sym_is_global (abfd
, sym
))
4246 i
= num_locals
+ num_globals2
++;
4248 sym
->udata
.i
= i
+ 1;
4252 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4254 *pnum_locals
= num_locals
;
4258 /* Align to the maximum file alignment that could be required for any
4259 ELF data structure. */
4261 static inline file_ptr
4262 align_file_position (file_ptr off
, int align
)
4264 return (off
+ align
- 1) & ~(align
- 1);
4267 /* Assign a file position to a section, optionally aligning to the
4268 required section alignment. */
4271 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4275 if (align
&& i_shdrp
->sh_addralign
> 1)
4276 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4277 i_shdrp
->sh_offset
= offset
;
4278 if (i_shdrp
->bfd_section
!= NULL
)
4279 i_shdrp
->bfd_section
->filepos
= offset
;
4280 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4281 offset
+= i_shdrp
->sh_size
;
4285 /* Compute the file positions we are going to put the sections at, and
4286 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4287 is not NULL, this is being called by the ELF backend linker. */
4290 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4291 struct bfd_link_info
*link_info
)
4293 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4294 struct fake_section_arg fsargs
;
4296 struct elf_strtab_hash
*strtab
= NULL
;
4297 Elf_Internal_Shdr
*shstrtab_hdr
;
4298 bfd_boolean need_symtab
;
4300 if (abfd
->output_has_begun
)
4303 /* Do any elf backend specific processing first. */
4304 if (bed
->elf_backend_begin_write_processing
)
4305 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4307 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4310 fsargs
.failed
= FALSE
;
4311 fsargs
.link_info
= link_info
;
4312 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4316 if (!assign_section_numbers (abfd
, link_info
))
4319 /* The backend linker builds symbol table information itself. */
4320 need_symtab
= (link_info
== NULL
4321 && (bfd_get_symcount (abfd
) > 0
4322 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4326 /* Non-zero if doing a relocatable link. */
4327 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4329 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4334 if (link_info
== NULL
)
4336 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4341 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4342 /* sh_name was set in init_file_header. */
4343 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4344 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4345 shstrtab_hdr
->sh_addr
= 0;
4346 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4347 shstrtab_hdr
->sh_entsize
= 0;
4348 shstrtab_hdr
->sh_link
= 0;
4349 shstrtab_hdr
->sh_info
= 0;
4350 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4351 shstrtab_hdr
->sh_addralign
= 1;
4353 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4359 Elf_Internal_Shdr
*hdr
;
4361 off
= elf_next_file_pos (abfd
);
4363 hdr
= & elf_symtab_hdr (abfd
);
4364 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4366 if (elf_symtab_shndx_list (abfd
) != NULL
)
4368 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4369 if (hdr
->sh_size
!= 0)
4370 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4371 /* FIXME: What about other symtab_shndx sections in the list ? */
4374 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4375 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4377 elf_next_file_pos (abfd
) = off
;
4379 /* Now that we know where the .strtab section goes, write it
4381 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4382 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4384 _bfd_elf_strtab_free (strtab
);
4387 abfd
->output_has_begun
= TRUE
;
4392 /* Make an initial estimate of the size of the program header. If we
4393 get the number wrong here, we'll redo section placement. */
4395 static bfd_size_type
4396 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4400 const struct elf_backend_data
*bed
;
4402 /* Assume we will need exactly two PT_LOAD segments: one for text
4403 and one for data. */
4406 s
= bfd_get_section_by_name (abfd
, ".interp");
4407 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4409 /* If we have a loadable interpreter section, we need a
4410 PT_INTERP segment. In this case, assume we also need a
4411 PT_PHDR segment, although that may not be true for all
4416 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4418 /* We need a PT_DYNAMIC segment. */
4422 if (info
!= NULL
&& info
->relro
)
4424 /* We need a PT_GNU_RELRO segment. */
4428 if (elf_eh_frame_hdr (abfd
))
4430 /* We need a PT_GNU_EH_FRAME segment. */
4434 if (elf_stack_flags (abfd
))
4436 /* We need a PT_GNU_STACK segment. */
4440 s
= bfd_get_section_by_name (abfd
,
4441 NOTE_GNU_PROPERTY_SECTION_NAME
);
4442 if (s
!= NULL
&& s
->size
!= 0)
4444 /* We need a PT_GNU_PROPERTY segment. */
4448 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4450 if ((s
->flags
& SEC_LOAD
) != 0
4451 && elf_section_type (s
) == SHT_NOTE
)
4453 unsigned int alignment_power
;
4454 /* We need a PT_NOTE segment. */
4456 /* Try to create just one PT_NOTE segment for all adjacent
4457 loadable SHT_NOTE sections. gABI requires that within a
4458 PT_NOTE segment (and also inside of each SHT_NOTE section)
4459 each note should have the same alignment. So we check
4460 whether the sections are correctly aligned. */
4461 alignment_power
= s
->alignment_power
;
4462 while (s
->next
!= NULL
4463 && s
->next
->alignment_power
== alignment_power
4464 && (s
->next
->flags
& SEC_LOAD
) != 0
4465 && elf_section_type (s
->next
) == SHT_NOTE
)
4470 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4472 if (s
->flags
& SEC_THREAD_LOCAL
)
4474 /* We need a PT_TLS segment. */
4480 bed
= get_elf_backend_data (abfd
);
4482 if ((abfd
->flags
& D_PAGED
) != 0
4483 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4485 /* Add a PT_GNU_MBIND segment for each mbind section. */
4486 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4487 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4488 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4490 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4493 /* xgettext:c-format */
4494 (_("%pB: GNU_MBIND section `%pA' has invalid "
4495 "sh_info field: %d"),
4496 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4499 /* Align mbind section to page size. */
4500 if (s
->alignment_power
< page_align_power
)
4501 s
->alignment_power
= page_align_power
;
4506 /* Let the backend count up any program headers it might need. */
4507 if (bed
->elf_backend_additional_program_headers
)
4511 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4517 return segs
* bed
->s
->sizeof_phdr
;
4520 /* Find the segment that contains the output_section of section. */
4523 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4525 struct elf_segment_map
*m
;
4526 Elf_Internal_Phdr
*p
;
4528 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4534 for (i
= m
->count
- 1; i
>= 0; i
--)
4535 if (m
->sections
[i
] == section
)
4542 /* Create a mapping from a set of sections to a program segment. */
4544 static struct elf_segment_map
*
4545 make_mapping (bfd
*abfd
,
4546 asection
**sections
,
4551 struct elf_segment_map
*m
;
4556 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4557 amt
+= (to
- from
) * sizeof (asection
*);
4558 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4562 m
->p_type
= PT_LOAD
;
4563 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4564 m
->sections
[i
- from
] = *hdrpp
;
4565 m
->count
= to
- from
;
4567 if (from
== 0 && phdr
)
4569 /* Include the headers in the first PT_LOAD segment. */
4570 m
->includes_filehdr
= 1;
4571 m
->includes_phdrs
= 1;
4577 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4580 struct elf_segment_map
*
4581 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4583 struct elf_segment_map
*m
;
4585 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4586 sizeof (struct elf_segment_map
));
4590 m
->p_type
= PT_DYNAMIC
;
4592 m
->sections
[0] = dynsec
;
4597 /* Possibly add or remove segments from the segment map. */
4600 elf_modify_segment_map (bfd
*abfd
,
4601 struct bfd_link_info
*info
,
4602 bfd_boolean remove_empty_load
)
4604 struct elf_segment_map
**m
;
4605 const struct elf_backend_data
*bed
;
4607 /* The placement algorithm assumes that non allocated sections are
4608 not in PT_LOAD segments. We ensure this here by removing such
4609 sections from the segment map. We also remove excluded
4610 sections. Finally, any PT_LOAD segment without sections is
4612 m
= &elf_seg_map (abfd
);
4615 unsigned int i
, new_count
;
4617 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4619 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4620 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4621 || (*m
)->p_type
!= PT_LOAD
))
4623 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4627 (*m
)->count
= new_count
;
4629 if (remove_empty_load
4630 && (*m
)->p_type
== PT_LOAD
4632 && !(*m
)->includes_phdrs
)
4638 bed
= get_elf_backend_data (abfd
);
4639 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4641 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4648 #define IS_TBSS(s) \
4649 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4651 /* Set up a mapping from BFD sections to program segments. */
4654 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4657 struct elf_segment_map
*m
;
4658 asection
**sections
= NULL
;
4659 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4660 bfd_boolean no_user_phdrs
;
4662 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4665 info
->user_phdrs
= !no_user_phdrs
;
4667 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4671 struct elf_segment_map
*mfirst
;
4672 struct elf_segment_map
**pm
;
4675 unsigned int hdr_index
;
4676 bfd_vma maxpagesize
;
4678 bfd_boolean phdr_in_segment
;
4679 bfd_boolean writable
;
4680 bfd_boolean executable
;
4681 unsigned int tls_count
= 0;
4682 asection
*first_tls
= NULL
;
4683 asection
*first_mbind
= NULL
;
4684 asection
*dynsec
, *eh_frame_hdr
;
4686 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4687 bfd_size_type phdr_size
; /* Octets/bytes. */
4688 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4690 /* Select the allocated sections, and sort them. */
4692 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4693 sections
= (asection
**) bfd_malloc (amt
);
4694 if (sections
== NULL
)
4697 /* Calculate top address, avoiding undefined behaviour of shift
4698 left operator when shift count is equal to size of type
4700 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4701 addr_mask
= (addr_mask
<< 1) + 1;
4704 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4706 if ((s
->flags
& SEC_ALLOC
) != 0)
4708 /* target_index is unused until bfd_elf_final_link
4709 starts output of section symbols. Use it to make
4711 s
->target_index
= i
;
4714 /* A wrapping section potentially clashes with header. */
4715 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4716 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4719 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4722 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4724 phdr_size
= elf_program_header_size (abfd
);
4725 if (phdr_size
== (bfd_size_type
) -1)
4726 phdr_size
= get_program_header_size (abfd
, info
);
4727 phdr_size
+= bed
->s
->sizeof_ehdr
;
4728 /* phdr_size is compared to LMA values which are in bytes. */
4730 maxpagesize
= bed
->maxpagesize
;
4731 if (maxpagesize
== 0)
4733 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4735 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4736 >= (phdr_size
& (maxpagesize
- 1))))
4737 /* For compatibility with old scripts that may not be using
4738 SIZEOF_HEADERS, add headers when it looks like space has
4739 been left for them. */
4740 phdr_in_segment
= TRUE
;
4742 /* Build the mapping. */
4746 /* If we have a .interp section, then create a PT_PHDR segment for
4747 the program headers and a PT_INTERP segment for the .interp
4749 s
= bfd_get_section_by_name (abfd
, ".interp");
4750 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4752 amt
= sizeof (struct elf_segment_map
);
4753 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4757 m
->p_type
= PT_PHDR
;
4759 m
->p_flags_valid
= 1;
4760 m
->includes_phdrs
= 1;
4761 phdr_in_segment
= TRUE
;
4765 amt
= sizeof (struct elf_segment_map
);
4766 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4770 m
->p_type
= PT_INTERP
;
4778 /* Look through the sections. We put sections in the same program
4779 segment when the start of the second section can be placed within
4780 a few bytes of the end of the first section. */
4786 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4788 && (dynsec
->flags
& SEC_LOAD
) == 0)
4791 if ((abfd
->flags
& D_PAGED
) == 0)
4792 phdr_in_segment
= FALSE
;
4794 /* Deal with -Ttext or something similar such that the first section
4795 is not adjacent to the program headers. This is an
4796 approximation, since at this point we don't know exactly how many
4797 program headers we will need. */
4798 if (phdr_in_segment
&& count
> 0)
4800 bfd_vma phdr_lma
; /* Bytes. */
4801 bfd_boolean separate_phdr
= FALSE
;
4803 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4805 && info
->separate_code
4806 && (sections
[0]->flags
& SEC_CODE
) != 0)
4808 /* If data sections should be separate from code and
4809 thus not executable, and the first section is
4810 executable then put the file and program headers in
4811 their own PT_LOAD. */
4812 separate_phdr
= TRUE
;
4813 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4814 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4816 /* The file and program headers are currently on the
4817 same page as the first section. Put them on the
4818 previous page if we can. */
4819 if (phdr_lma
>= maxpagesize
)
4820 phdr_lma
-= maxpagesize
;
4822 separate_phdr
= FALSE
;
4825 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4826 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4827 /* If file and program headers would be placed at the end
4828 of memory then it's probably better to omit them. */
4829 phdr_in_segment
= FALSE
;
4830 else if (phdr_lma
< wrap_to
)
4831 /* If a section wraps around to where we'll be placing
4832 file and program headers, then the headers will be
4834 phdr_in_segment
= FALSE
;
4835 else if (separate_phdr
)
4837 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4840 m
->p_paddr
= phdr_lma
* opb
;
4842 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4843 m
->p_paddr_valid
= 1;
4846 phdr_in_segment
= FALSE
;
4850 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4853 bfd_boolean new_segment
;
4857 /* See if this section and the last one will fit in the same
4860 if (last_hdr
== NULL
)
4862 /* If we don't have a segment yet, then we don't need a new
4863 one (we build the last one after this loop). */
4864 new_segment
= FALSE
;
4866 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4868 /* If this section has a different relation between the
4869 virtual address and the load address, then we need a new
4873 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4874 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4876 /* If this section has a load address that makes it overlap
4877 the previous section, then we need a new segment. */
4880 else if ((abfd
->flags
& D_PAGED
) != 0
4881 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4882 == (hdr
->lma
& -maxpagesize
)))
4884 /* If we are demand paged then we can't map two disk
4885 pages onto the same memory page. */
4886 new_segment
= FALSE
;
4888 /* In the next test we have to be careful when last_hdr->lma is close
4889 to the end of the address space. If the aligned address wraps
4890 around to the start of the address space, then there are no more
4891 pages left in memory and it is OK to assume that the current
4892 section can be included in the current segment. */
4893 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4894 + maxpagesize
> last_hdr
->lma
)
4895 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4896 + maxpagesize
<= hdr
->lma
))
4898 /* If putting this section in this segment would force us to
4899 skip a page in the segment, then we need a new segment. */
4902 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4903 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4905 /* We don't want to put a loaded section after a
4906 nonloaded (ie. bss style) section in the same segment
4907 as that will force the non-loaded section to be loaded.
4908 Consider .tbss sections as loaded for this purpose. */
4911 else if ((abfd
->flags
& D_PAGED
) == 0)
4913 /* If the file is not demand paged, which means that we
4914 don't require the sections to be correctly aligned in the
4915 file, then there is no other reason for a new segment. */
4916 new_segment
= FALSE
;
4918 else if (info
!= NULL
4919 && info
->separate_code
4920 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4925 && (hdr
->flags
& SEC_READONLY
) == 0)
4927 /* We don't want to put a writable section in a read only
4933 /* Otherwise, we can use the same segment. */
4934 new_segment
= FALSE
;
4937 /* Allow interested parties a chance to override our decision. */
4938 if (last_hdr
!= NULL
4940 && info
->callbacks
->override_segment_assignment
!= NULL
)
4942 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4948 if ((hdr
->flags
& SEC_READONLY
) == 0)
4950 if ((hdr
->flags
& SEC_CODE
) != 0)
4953 /* .tbss sections effectively have zero size. */
4954 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4958 /* We need a new program segment. We must create a new program
4959 header holding all the sections from hdr_index until hdr. */
4961 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4968 if ((hdr
->flags
& SEC_READONLY
) == 0)
4973 if ((hdr
->flags
& SEC_CODE
) == 0)
4979 /* .tbss sections effectively have zero size. */
4980 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4982 phdr_in_segment
= FALSE
;
4985 /* Create a final PT_LOAD program segment, but not if it's just
4987 if (last_hdr
!= NULL
4988 && (i
- hdr_index
!= 1
4989 || !IS_TBSS (last_hdr
)))
4991 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4999 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
5002 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
5009 /* For each batch of consecutive loadable SHT_NOTE sections,
5010 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
5011 because if we link together nonloadable .note sections and
5012 loadable .note sections, we will generate two .note sections
5013 in the output file. */
5014 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5016 if ((s
->flags
& SEC_LOAD
) != 0
5017 && elf_section_type (s
) == SHT_NOTE
)
5020 unsigned int alignment_power
= s
->alignment_power
;
5023 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
5025 if (s2
->next
->alignment_power
== alignment_power
5026 && (s2
->next
->flags
& SEC_LOAD
) != 0
5027 && elf_section_type (s2
->next
) == SHT_NOTE
5028 && align_power (s2
->lma
+ s2
->size
/ opb
,
5035 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5036 amt
+= count
* sizeof (asection
*);
5037 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5041 m
->p_type
= PT_NOTE
;
5045 m
->sections
[m
->count
- count
--] = s
;
5046 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5049 m
->sections
[m
->count
- 1] = s
;
5050 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5054 if (s
->flags
& SEC_THREAD_LOCAL
)
5060 if (first_mbind
== NULL
5061 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5065 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5068 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5069 amt
+= tls_count
* sizeof (asection
*);
5070 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5075 m
->count
= tls_count
;
5076 /* Mandated PF_R. */
5078 m
->p_flags_valid
= 1;
5080 for (i
= 0; i
< tls_count
; ++i
)
5082 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5085 (_("%pB: TLS sections are not adjacent:"), abfd
);
5088 while (i
< tls_count
)
5090 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5092 _bfd_error_handler (_(" TLS: %pA"), s
);
5096 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5099 bfd_set_error (bfd_error_bad_value
);
5111 && (abfd
->flags
& D_PAGED
) != 0
5112 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5113 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5114 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5115 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5117 /* Mandated PF_R. */
5118 unsigned long p_flags
= PF_R
;
5119 if ((s
->flags
& SEC_READONLY
) == 0)
5121 if ((s
->flags
& SEC_CODE
) != 0)
5124 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5125 m
= bfd_zalloc (abfd
, amt
);
5129 m
->p_type
= (PT_GNU_MBIND_LO
5130 + elf_section_data (s
)->this_hdr
.sh_info
);
5132 m
->p_flags_valid
= 1;
5134 m
->p_flags
= p_flags
;
5140 s
= bfd_get_section_by_name (abfd
,
5141 NOTE_GNU_PROPERTY_SECTION_NAME
);
5142 if (s
!= NULL
&& s
->size
!= 0)
5144 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5145 m
= bfd_zalloc (abfd
, amt
);
5149 m
->p_type
= PT_GNU_PROPERTY
;
5151 m
->p_flags_valid
= 1;
5158 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5160 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5161 if (eh_frame_hdr
!= NULL
5162 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5164 amt
= sizeof (struct elf_segment_map
);
5165 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5169 m
->p_type
= PT_GNU_EH_FRAME
;
5171 m
->sections
[0] = eh_frame_hdr
->output_section
;
5177 if (elf_stack_flags (abfd
))
5179 amt
= sizeof (struct elf_segment_map
);
5180 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5184 m
->p_type
= PT_GNU_STACK
;
5185 m
->p_flags
= elf_stack_flags (abfd
);
5186 m
->p_align
= bed
->stack_align
;
5187 m
->p_flags_valid
= 1;
5188 m
->p_align_valid
= m
->p_align
!= 0;
5189 if (info
->stacksize
> 0)
5191 m
->p_size
= info
->stacksize
;
5192 m
->p_size_valid
= 1;
5199 if (info
!= NULL
&& info
->relro
)
5201 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5203 if (m
->p_type
== PT_LOAD
5205 && m
->sections
[0]->vma
>= info
->relro_start
5206 && m
->sections
[0]->vma
< info
->relro_end
)
5209 while (--i
!= (unsigned) -1)
5211 if (m
->sections
[i
]->size
> 0
5212 && (m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5213 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5217 if (i
!= (unsigned) -1)
5222 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5225 amt
= sizeof (struct elf_segment_map
);
5226 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5230 m
->p_type
= PT_GNU_RELRO
;
5237 elf_seg_map (abfd
) = mfirst
;
5240 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5243 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5245 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5250 if (sections
!= NULL
)
5255 /* Sort sections by address. */
5258 elf_sort_sections (const void *arg1
, const void *arg2
)
5260 const asection
*sec1
= *(const asection
**) arg1
;
5261 const asection
*sec2
= *(const asection
**) arg2
;
5262 bfd_size_type size1
, size2
;
5264 /* Sort by LMA first, since this is the address used to
5265 place the section into a segment. */
5266 if (sec1
->lma
< sec2
->lma
)
5268 else if (sec1
->lma
> sec2
->lma
)
5271 /* Then sort by VMA. Normally the LMA and the VMA will be
5272 the same, and this will do nothing. */
5273 if (sec1
->vma
< sec2
->vma
)
5275 else if (sec1
->vma
> sec2
->vma
)
5278 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5280 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5287 else if (TOEND (sec2
))
5292 /* Sort by size, to put zero sized sections
5293 before others at the same address. */
5295 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5296 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5303 return sec1
->target_index
- sec2
->target_index
;
5306 /* This qsort comparison functions sorts PT_LOAD segments first and
5307 by p_paddr, for assign_file_positions_for_load_sections. */
5310 elf_sort_segments (const void *arg1
, const void *arg2
)
5312 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5313 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5315 if (m1
->p_type
!= m2
->p_type
)
5317 if (m1
->p_type
== PT_NULL
)
5319 if (m2
->p_type
== PT_NULL
)
5321 return m1
->p_type
< m2
->p_type
? -1 : 1;
5323 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5324 return m1
->includes_filehdr
? -1 : 1;
5325 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5326 return m1
->no_sort_lma
? -1 : 1;
5327 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5329 bfd_vma lma1
, lma2
; /* Octets. */
5331 if (m1
->p_paddr_valid
)
5333 else if (m1
->count
!= 0)
5335 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5337 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5340 if (m2
->p_paddr_valid
)
5342 else if (m2
->count
!= 0)
5344 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5346 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5349 return lma1
< lma2
? -1 : 1;
5351 if (m1
->idx
!= m2
->idx
)
5352 return m1
->idx
< m2
->idx
? -1 : 1;
5356 /* Ian Lance Taylor writes:
5358 We shouldn't be using % with a negative signed number. That's just
5359 not good. We have to make sure either that the number is not
5360 negative, or that the number has an unsigned type. When the types
5361 are all the same size they wind up as unsigned. When file_ptr is a
5362 larger signed type, the arithmetic winds up as signed long long,
5365 What we're trying to say here is something like ``increase OFF by
5366 the least amount that will cause it to be equal to the VMA modulo
5368 /* In other words, something like:
5370 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5371 off_offset = off % bed->maxpagesize;
5372 if (vma_offset < off_offset)
5373 adjustment = vma_offset + bed->maxpagesize - off_offset;
5375 adjustment = vma_offset - off_offset;
5377 which can be collapsed into the expression below. */
5380 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5382 /* PR binutils/16199: Handle an alignment of zero. */
5383 if (maxpagesize
== 0)
5385 return ((vma
- off
) % maxpagesize
);
5389 print_segment_map (const struct elf_segment_map
*m
)
5392 const char *pt
= get_segment_type (m
->p_type
);
5397 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5398 sprintf (buf
, "LOPROC+%7.7x",
5399 (unsigned int) (m
->p_type
- PT_LOPROC
));
5400 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5401 sprintf (buf
, "LOOS+%7.7x",
5402 (unsigned int) (m
->p_type
- PT_LOOS
));
5404 snprintf (buf
, sizeof (buf
), "%8.8x",
5405 (unsigned int) m
->p_type
);
5409 fprintf (stderr
, "%s:", pt
);
5410 for (j
= 0; j
< m
->count
; j
++)
5411 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5417 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5422 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5424 buf
= bfd_zmalloc (len
);
5427 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5432 /* Assign file positions to the sections based on the mapping from
5433 sections to segments. This function also sets up some fields in
5437 assign_file_positions_for_load_sections (bfd
*abfd
,
5438 struct bfd_link_info
*link_info
)
5440 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5441 struct elf_segment_map
*m
;
5442 struct elf_segment_map
*phdr_load_seg
;
5443 Elf_Internal_Phdr
*phdrs
;
5444 Elf_Internal_Phdr
*p
;
5445 file_ptr off
; /* Octets. */
5446 bfd_size_type maxpagesize
;
5447 unsigned int alloc
, actual
;
5449 struct elf_segment_map
**sorted_seg_map
;
5450 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5452 if (link_info
== NULL
5453 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5457 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5462 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5463 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5467 /* PR binutils/12467. */
5468 elf_elfheader (abfd
)->e_phoff
= 0;
5469 elf_elfheader (abfd
)->e_phentsize
= 0;
5472 elf_elfheader (abfd
)->e_phnum
= alloc
;
5474 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5477 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5481 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5482 BFD_ASSERT (elf_program_header_size (abfd
)
5483 == actual
* bed
->s
->sizeof_phdr
);
5484 BFD_ASSERT (actual
>= alloc
);
5489 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5493 /* We're writing the size in elf_program_header_size (abfd),
5494 see assign_file_positions_except_relocs, so make sure we have
5495 that amount allocated, with trailing space cleared.
5496 The variable alloc contains the computed need, while
5497 elf_program_header_size (abfd) contains the size used for the
5499 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5500 where the layout is forced to according to a larger size in the
5501 last iterations for the testcase ld-elf/header. */
5502 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5503 + alloc
* sizeof (*sorted_seg_map
)));
5504 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5505 elf_tdata (abfd
)->phdr
= phdrs
;
5509 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5511 sorted_seg_map
[j
] = m
;
5512 /* If elf_segment_map is not from map_sections_to_segments, the
5513 sections may not be correctly ordered. NOTE: sorting should
5514 not be done to the PT_NOTE section of a corefile, which may
5515 contain several pseudo-sections artificially created by bfd.
5516 Sorting these pseudo-sections breaks things badly. */
5518 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5519 && m
->p_type
== PT_NOTE
))
5521 for (i
= 0; i
< m
->count
; i
++)
5522 m
->sections
[i
]->target_index
= i
;
5523 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5528 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5532 if ((abfd
->flags
& D_PAGED
) != 0)
5533 maxpagesize
= bed
->maxpagesize
;
5535 /* Sections must map to file offsets past the ELF file header. */
5536 off
= bed
->s
->sizeof_ehdr
;
5537 /* And if one of the PT_LOAD headers doesn't include the program
5538 headers then we'll be mapping program headers in the usual
5539 position after the ELF file header. */
5540 phdr_load_seg
= NULL
;
5541 for (j
= 0; j
< alloc
; j
++)
5543 m
= sorted_seg_map
[j
];
5544 if (m
->p_type
!= PT_LOAD
)
5546 if (m
->includes_phdrs
)
5552 if (phdr_load_seg
== NULL
)
5553 off
+= actual
* bed
->s
->sizeof_phdr
;
5555 for (j
= 0; j
< alloc
; j
++)
5558 bfd_vma off_adjust
; /* Octets. */
5559 bfd_boolean no_contents
;
5561 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5562 number of sections with contents contributing to both p_filesz
5563 and p_memsz, followed by a number of sections with no contents
5564 that just contribute to p_memsz. In this loop, OFF tracks next
5565 available file offset for PT_LOAD and PT_NOTE segments. */
5566 m
= sorted_seg_map
[j
];
5568 p
->p_type
= m
->p_type
;
5569 p
->p_flags
= m
->p_flags
;
5572 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5574 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5576 if (m
->p_paddr_valid
)
5577 p
->p_paddr
= m
->p_paddr
;
5578 else if (m
->count
== 0)
5581 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5583 if (p
->p_type
== PT_LOAD
5584 && (abfd
->flags
& D_PAGED
) != 0)
5586 /* p_align in demand paged PT_LOAD segments effectively stores
5587 the maximum page size. When copying an executable with
5588 objcopy, we set m->p_align from the input file. Use this
5589 value for maxpagesize rather than bed->maxpagesize, which
5590 may be different. Note that we use maxpagesize for PT_TLS
5591 segment alignment later in this function, so we are relying
5592 on at least one PT_LOAD segment appearing before a PT_TLS
5594 if (m
->p_align_valid
)
5595 maxpagesize
= m
->p_align
;
5597 p
->p_align
= maxpagesize
;
5599 else if (m
->p_align_valid
)
5600 p
->p_align
= m
->p_align
;
5601 else if (m
->count
== 0)
5602 p
->p_align
= 1 << bed
->s
->log_file_align
;
5604 if (m
== phdr_load_seg
)
5606 if (!m
->includes_filehdr
)
5608 off
+= actual
* bed
->s
->sizeof_phdr
;
5611 no_contents
= FALSE
;
5613 if (p
->p_type
== PT_LOAD
5616 bfd_size_type align
; /* Bytes. */
5617 unsigned int align_power
= 0;
5619 if (m
->p_align_valid
)
5623 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5625 unsigned int secalign
;
5627 secalign
= bfd_section_alignment (*secpp
);
5628 if (secalign
> align_power
)
5629 align_power
= secalign
;
5631 align
= (bfd_size_type
) 1 << align_power
;
5632 if (align
< maxpagesize
)
5633 align
= maxpagesize
;
5636 for (i
= 0; i
< m
->count
; i
++)
5637 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5638 /* If we aren't making room for this section, then
5639 it must be SHT_NOBITS regardless of what we've
5640 set via struct bfd_elf_special_section. */
5641 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5643 /* Find out whether this segment contains any loadable
5646 for (i
= 0; i
< m
->count
; i
++)
5647 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5649 no_contents
= FALSE
;
5653 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5655 /* Broken hardware and/or kernel require that files do not
5656 map the same page with different permissions on some hppa
5659 && (abfd
->flags
& D_PAGED
) != 0
5660 && bed
->no_page_alias
5661 && (off
& (maxpagesize
- 1)) != 0
5662 && ((off
& -maxpagesize
)
5663 == ((off
+ off_adjust
) & -maxpagesize
)))
5664 off_adjust
+= maxpagesize
;
5668 /* We shouldn't need to align the segment on disk since
5669 the segment doesn't need file space, but the gABI
5670 arguably requires the alignment and glibc ld.so
5671 checks it. So to comply with the alignment
5672 requirement but not waste file space, we adjust
5673 p_offset for just this segment. (OFF_ADJUST is
5674 subtracted from OFF later.) This may put p_offset
5675 past the end of file, but that shouldn't matter. */
5680 /* Make sure the .dynamic section is the first section in the
5681 PT_DYNAMIC segment. */
5682 else if (p
->p_type
== PT_DYNAMIC
5684 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5687 (_("%pB: The first section in the PT_DYNAMIC segment"
5688 " is not the .dynamic section"),
5690 bfd_set_error (bfd_error_bad_value
);
5693 /* Set the note section type to SHT_NOTE. */
5694 else if (p
->p_type
== PT_NOTE
)
5695 for (i
= 0; i
< m
->count
; i
++)
5696 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5698 if (m
->includes_filehdr
)
5700 if (!m
->p_flags_valid
)
5702 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5703 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5704 if (p
->p_type
== PT_LOAD
)
5708 if (p
->p_vaddr
< (bfd_vma
) off
5709 || (!m
->p_paddr_valid
5710 && p
->p_paddr
< (bfd_vma
) off
))
5713 (_("%pB: not enough room for program headers,"
5714 " try linking with -N"),
5716 bfd_set_error (bfd_error_bad_value
);
5720 if (!m
->p_paddr_valid
)
5724 else if (sorted_seg_map
[0]->includes_filehdr
)
5726 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5727 p
->p_vaddr
= filehdr
->p_vaddr
;
5728 if (!m
->p_paddr_valid
)
5729 p
->p_paddr
= filehdr
->p_paddr
;
5733 if (m
->includes_phdrs
)
5735 if (!m
->p_flags_valid
)
5737 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5738 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5739 if (!m
->includes_filehdr
)
5741 if (p
->p_type
== PT_LOAD
)
5743 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5746 p
->p_vaddr
-= off
- p
->p_offset
;
5747 if (!m
->p_paddr_valid
)
5748 p
->p_paddr
-= off
- p
->p_offset
;
5751 else if (phdr_load_seg
!= NULL
)
5753 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5754 bfd_vma phdr_off
= 0; /* Octets. */
5755 if (phdr_load_seg
->includes_filehdr
)
5756 phdr_off
= bed
->s
->sizeof_ehdr
;
5757 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5758 if (!m
->p_paddr_valid
)
5759 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5760 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5763 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5767 if (p
->p_type
== PT_LOAD
5768 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5770 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5775 /* Put meaningless p_offset for PT_LOAD segments
5776 without file contents somewhere within the first
5777 page, in an attempt to not point past EOF. */
5778 bfd_size_type align
= maxpagesize
;
5779 if (align
< p
->p_align
)
5783 p
->p_offset
= off
% align
;
5788 file_ptr adjust
; /* Octets. */
5790 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5792 p
->p_filesz
+= adjust
;
5793 p
->p_memsz
+= adjust
;
5797 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5798 maps. Set filepos for sections in PT_LOAD segments, and in
5799 core files, for sections in PT_NOTE segments.
5800 assign_file_positions_for_non_load_sections will set filepos
5801 for other sections and update p_filesz for other segments. */
5802 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5805 bfd_size_type align
;
5806 Elf_Internal_Shdr
*this_hdr
;
5809 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5810 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5812 if ((p
->p_type
== PT_LOAD
5813 || p
->p_type
== PT_TLS
)
5814 && (this_hdr
->sh_type
!= SHT_NOBITS
5815 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5816 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5817 || p
->p_type
== PT_TLS
))))
5819 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5820 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5821 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5822 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5826 || p_end
< p_start
))
5829 /* xgettext:c-format */
5830 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5831 abfd
, sec
, (uint64_t) s_start
/ opb
,
5832 (uint64_t) p_end
/ opb
);
5834 sec
->lma
= p_end
/ opb
;
5836 p
->p_memsz
+= adjust
;
5838 if (p
->p_type
== PT_LOAD
)
5840 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5843 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5845 /* We have a PROGBITS section following NOBITS ones.
5846 Allocate file space for the NOBITS section(s) and
5848 adjust
= p
->p_memsz
- p
->p_filesz
;
5849 if (!write_zeros (abfd
, off
, adjust
))
5853 /* We only adjust sh_offset in SHT_NOBITS sections
5854 as would seem proper for their address when the
5855 section is first in the segment. sh_offset
5856 doesn't really have any significance for
5857 SHT_NOBITS anyway, apart from a notional position
5858 relative to other sections. Historically we
5859 didn't bother with adjusting sh_offset and some
5860 programs depend on it not being adjusted. See
5861 pr12921 and pr25662. */
5862 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
5865 if (this_hdr
->sh_type
== SHT_NOBITS
)
5866 off_adjust
+= adjust
;
5869 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5870 p
->p_filesz
+= adjust
;
5873 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5875 /* The section at i == 0 is the one that actually contains
5879 this_hdr
->sh_offset
= sec
->filepos
= off
;
5880 off
+= this_hdr
->sh_size
;
5881 p
->p_filesz
= this_hdr
->sh_size
;
5887 /* The rest are fake sections that shouldn't be written. */
5896 if (p
->p_type
== PT_LOAD
)
5898 this_hdr
->sh_offset
= sec
->filepos
= off
;
5899 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5900 off
+= this_hdr
->sh_size
;
5902 else if (this_hdr
->sh_type
== SHT_NOBITS
5903 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5904 && this_hdr
->sh_offset
== 0)
5906 /* This is a .tbss section that didn't get a PT_LOAD.
5907 (See _bfd_elf_map_sections_to_segments "Create a
5908 final PT_LOAD".) Set sh_offset to the value it
5909 would have if we had created a zero p_filesz and
5910 p_memsz PT_LOAD header for the section. This
5911 also makes the PT_TLS header have the same
5913 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5915 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5918 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5920 p
->p_filesz
+= this_hdr
->sh_size
;
5921 /* A load section without SHF_ALLOC is something like
5922 a note section in a PT_NOTE segment. These take
5923 file space but are not loaded into memory. */
5924 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5925 p
->p_memsz
+= this_hdr
->sh_size
;
5927 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5929 if (p
->p_type
== PT_TLS
)
5930 p
->p_memsz
+= this_hdr
->sh_size
;
5932 /* .tbss is special. It doesn't contribute to p_memsz of
5934 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5935 p
->p_memsz
+= this_hdr
->sh_size
;
5938 if (align
> p
->p_align
5939 && !m
->p_align_valid
5940 && (p
->p_type
!= PT_LOAD
5941 || (abfd
->flags
& D_PAGED
) == 0))
5945 if (!m
->p_flags_valid
)
5948 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5950 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5957 /* PR ld/20815 - Check that the program header segment, if
5958 present, will be loaded into memory. */
5959 if (p
->p_type
== PT_PHDR
5960 && phdr_load_seg
== NULL
5961 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
5962 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
5964 /* The fix for this error is usually to edit the linker script being
5965 used and set up the program headers manually. Either that or
5966 leave room for the headers at the start of the SECTIONS. */
5967 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5968 " by LOAD segment"),
5970 if (link_info
== NULL
)
5972 /* Arrange for the linker to exit with an error, deleting
5973 the output file unless --noinhibit-exec is given. */
5974 link_info
->callbacks
->info ("%X");
5977 /* Check that all sections are in a PT_LOAD segment.
5978 Don't check funky gdb generated core files. */
5979 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5981 bfd_boolean check_vma
= TRUE
;
5983 for (i
= 1; i
< m
->count
; i
++)
5984 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5985 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5986 ->this_hdr
), p
) != 0
5987 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5988 ->this_hdr
), p
) != 0)
5990 /* Looks like we have overlays packed into the segment. */
5995 for (i
= 0; i
< m
->count
; i
++)
5997 Elf_Internal_Shdr
*this_hdr
;
6000 sec
= m
->sections
[i
];
6001 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
6002 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
6003 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
6006 /* xgettext:c-format */
6007 (_("%pB: section `%pA' can't be allocated in segment %d"),
6009 print_segment_map (m
);
6015 elf_next_file_pos (abfd
) = off
;
6017 if (link_info
!= NULL
6018 && phdr_load_seg
!= NULL
6019 && phdr_load_seg
->includes_filehdr
)
6021 /* There is a segment that contains both the file headers and the
6022 program headers, so provide a symbol __ehdr_start pointing there.
6023 A program can use this to examine itself robustly. */
6025 struct elf_link_hash_entry
*hash
6026 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
6027 FALSE
, FALSE
, TRUE
);
6028 /* If the symbol was referenced and not defined, define it. */
6030 && (hash
->root
.type
== bfd_link_hash_new
6031 || hash
->root
.type
== bfd_link_hash_undefined
6032 || hash
->root
.type
== bfd_link_hash_undefweak
6033 || hash
->root
.type
== bfd_link_hash_common
))
6036 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6038 if (phdr_load_seg
->count
!= 0)
6039 /* The segment contains sections, so use the first one. */
6040 s
= phdr_load_seg
->sections
[0];
6042 /* Use the first (i.e. lowest-addressed) section in any segment. */
6043 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6044 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6052 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6053 hash
->root
.u
.def
.section
= s
;
6057 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6058 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6061 hash
->root
.type
= bfd_link_hash_defined
;
6062 hash
->def_regular
= 1;
6070 /* Determine if a bfd is a debuginfo file. Unfortunately there
6071 is no defined method for detecting such files, so we have to
6072 use heuristics instead. */
6075 is_debuginfo_file (bfd
*abfd
)
6077 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6080 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6081 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6082 Elf_Internal_Shdr
**headerp
;
6084 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6086 Elf_Internal_Shdr
*header
= * headerp
;
6088 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6089 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6090 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6091 && header
->sh_type
!= SHT_NOBITS
6092 && header
->sh_type
!= SHT_NOTE
)
6099 /* Assign file positions for the other sections, except for compressed debugging
6100 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6103 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6104 struct bfd_link_info
*link_info
)
6106 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6107 Elf_Internal_Shdr
**i_shdrpp
;
6108 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6109 Elf_Internal_Phdr
*phdrs
;
6110 Elf_Internal_Phdr
*p
;
6111 struct elf_segment_map
*m
;
6113 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6115 i_shdrpp
= elf_elfsections (abfd
);
6116 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6117 off
= elf_next_file_pos (abfd
);
6118 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6120 Elf_Internal_Shdr
*hdr
;
6123 if (hdr
->bfd_section
!= NULL
6124 && (hdr
->bfd_section
->filepos
!= 0
6125 || (hdr
->sh_type
== SHT_NOBITS
6126 && hdr
->contents
== NULL
)))
6127 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6128 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6130 if (hdr
->sh_size
!= 0
6131 /* PR 24717 - debuginfo files are known to be not strictly
6132 compliant with the ELF standard. In particular they often
6133 have .note.gnu.property sections that are outside of any
6134 loadable segment. This is not a problem for such files,
6135 so do not warn about them. */
6136 && ! is_debuginfo_file (abfd
))
6138 /* xgettext:c-format */
6139 (_("%pB: warning: allocated section `%s' not in segment"),
6141 (hdr
->bfd_section
== NULL
6143 : hdr
->bfd_section
->name
));
6144 /* We don't need to page align empty sections. */
6145 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6146 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6149 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6151 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6154 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6155 && hdr
->bfd_section
== NULL
)
6156 /* We don't know the offset of these sections yet: their size has
6157 not been decided. */
6158 || (hdr
->bfd_section
!= NULL
6159 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6160 || (bfd_section_is_ctf (hdr
->bfd_section
)
6161 && abfd
->is_linker_output
)))
6162 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6163 || (elf_symtab_shndx_list (abfd
) != NULL
6164 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6165 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6166 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6167 hdr
->sh_offset
= -1;
6169 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6171 elf_next_file_pos (abfd
) = off
;
6173 /* Now that we have set the section file positions, we can set up
6174 the file positions for the non PT_LOAD segments. */
6175 phdrs
= elf_tdata (abfd
)->phdr
;
6176 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6178 if (p
->p_type
== PT_GNU_RELRO
)
6180 bfd_vma start
, end
; /* Bytes. */
6183 if (link_info
!= NULL
)
6185 /* During linking the range of the RELRO segment is passed
6186 in link_info. Note that there may be padding between
6187 relro_start and the first RELRO section. */
6188 start
= link_info
->relro_start
;
6189 end
= link_info
->relro_end
;
6191 else if (m
->count
!= 0)
6193 if (!m
->p_size_valid
)
6195 start
= m
->sections
[0]->vma
;
6196 end
= start
+ m
->p_size
/ opb
;
6207 struct elf_segment_map
*lm
;
6208 const Elf_Internal_Phdr
*lp
;
6211 /* Find a LOAD segment containing a section in the RELRO
6213 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6215 lm
= lm
->next
, lp
++)
6217 if (lp
->p_type
== PT_LOAD
6219 && (lm
->sections
[lm
->count
- 1]->vma
6220 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6221 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6223 && lm
->sections
[0]->vma
< end
)
6229 /* Find the section starting the RELRO segment. */
6230 for (i
= 0; i
< lm
->count
; i
++)
6232 asection
*s
= lm
->sections
[i
];
6241 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6242 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6243 p
->p_offset
= lm
->sections
[i
]->filepos
;
6244 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6245 p
->p_filesz
= p
->p_memsz
;
6247 /* The RELRO segment typically ends a few bytes
6248 into .got.plt but other layouts are possible.
6249 In cases where the end does not match any
6250 loaded section (for instance is in file
6251 padding), trim p_filesz back to correspond to
6252 the end of loaded section contents. */
6253 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6254 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6256 /* Preserve the alignment and flags if they are
6257 valid. The gold linker generates RW/4 for
6258 the PT_GNU_RELRO section. It is better for
6259 objcopy/strip to honor these attributes
6260 otherwise gdb will choke when using separate
6262 if (!m
->p_align_valid
)
6264 if (!m
->p_flags_valid
)
6270 if (link_info
!= NULL
)
6273 memset (p
, 0, sizeof *p
);
6275 else if (p
->p_type
== PT_GNU_STACK
)
6277 if (m
->p_size_valid
)
6278 p
->p_memsz
= m
->p_size
;
6280 else if (m
->count
!= 0)
6284 if (p
->p_type
!= PT_LOAD
6285 && (p
->p_type
!= PT_NOTE
6286 || bfd_get_format (abfd
) != bfd_core
))
6288 /* A user specified segment layout may include a PHDR
6289 segment that overlaps with a LOAD segment... */
6290 if (p
->p_type
== PT_PHDR
)
6296 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6298 /* PR 17512: file: 2195325e. */
6300 (_("%pB: error: non-load segment %d includes file header "
6301 "and/or program header"),
6302 abfd
, (int) (p
- phdrs
));
6307 p
->p_offset
= m
->sections
[0]->filepos
;
6308 for (i
= m
->count
; i
-- != 0;)
6310 asection
*sect
= m
->sections
[i
];
6311 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6312 if (hdr
->sh_type
!= SHT_NOBITS
)
6314 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6326 static elf_section_list
*
6327 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6329 for (;list
!= NULL
; list
= list
->next
)
6335 /* Work out the file positions of all the sections. This is called by
6336 _bfd_elf_compute_section_file_positions. All the section sizes and
6337 VMAs must be known before this is called.
6339 Reloc sections come in two flavours: Those processed specially as
6340 "side-channel" data attached to a section to which they apply, and those that
6341 bfd doesn't process as relocations. The latter sort are stored in a normal
6342 bfd section by bfd_section_from_shdr. We don't consider the former sort
6343 here, unless they form part of the loadable image. Reloc sections not
6344 assigned here (and compressed debugging sections and CTF sections which
6345 nothing else in the file can rely upon) will be handled later by
6346 assign_file_positions_for_relocs.
6348 We also don't set the positions of the .symtab and .strtab here. */
6351 assign_file_positions_except_relocs (bfd
*abfd
,
6352 struct bfd_link_info
*link_info
)
6354 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6355 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6356 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6359 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6360 && bfd_get_format (abfd
) != bfd_core
)
6362 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6363 unsigned int num_sec
= elf_numsections (abfd
);
6364 Elf_Internal_Shdr
**hdrpp
;
6368 /* Start after the ELF header. */
6369 off
= i_ehdrp
->e_ehsize
;
6371 /* We are not creating an executable, which means that we are
6372 not creating a program header, and that the actual order of
6373 the sections in the file is unimportant. */
6374 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6376 Elf_Internal_Shdr
*hdr
;
6379 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6380 && hdr
->bfd_section
== NULL
)
6381 /* Do not assign offsets for these sections yet: we don't know
6383 || (hdr
->bfd_section
!= NULL
6384 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6385 || (bfd_section_is_ctf (hdr
->bfd_section
)
6386 && abfd
->is_linker_output
)))
6387 || i
== elf_onesymtab (abfd
)
6388 || (elf_symtab_shndx_list (abfd
) != NULL
6389 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6390 || i
== elf_strtab_sec (abfd
)
6391 || i
== elf_shstrtab_sec (abfd
))
6393 hdr
->sh_offset
= -1;
6396 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6399 elf_next_file_pos (abfd
) = off
;
6400 elf_program_header_size (abfd
) = 0;
6404 /* Assign file positions for the loaded sections based on the
6405 assignment of sections to segments. */
6406 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6409 /* And for non-load sections. */
6410 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6414 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6417 /* Write out the program headers. */
6418 alloc
= i_ehdrp
->e_phnum
;
6421 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6422 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6430 _bfd_elf_init_file_header (bfd
*abfd
,
6431 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6433 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6434 struct elf_strtab_hash
*shstrtab
;
6435 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6437 i_ehdrp
= elf_elfheader (abfd
);
6439 shstrtab
= _bfd_elf_strtab_init ();
6440 if (shstrtab
== NULL
)
6443 elf_shstrtab (abfd
) = shstrtab
;
6445 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6446 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6447 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6448 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6450 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6451 i_ehdrp
->e_ident
[EI_DATA
] =
6452 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6453 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6455 if ((abfd
->flags
& DYNAMIC
) != 0)
6456 i_ehdrp
->e_type
= ET_DYN
;
6457 else if ((abfd
->flags
& EXEC_P
) != 0)
6458 i_ehdrp
->e_type
= ET_EXEC
;
6459 else if (bfd_get_format (abfd
) == bfd_core
)
6460 i_ehdrp
->e_type
= ET_CORE
;
6462 i_ehdrp
->e_type
= ET_REL
;
6464 switch (bfd_get_arch (abfd
))
6466 case bfd_arch_unknown
:
6467 i_ehdrp
->e_machine
= EM_NONE
;
6470 /* There used to be a long list of cases here, each one setting
6471 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6472 in the corresponding bfd definition. To avoid duplication,
6473 the switch was removed. Machines that need special handling
6474 can generally do it in elf_backend_final_write_processing(),
6475 unless they need the information earlier than the final write.
6476 Such need can generally be supplied by replacing the tests for
6477 e_machine with the conditions used to determine it. */
6479 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6482 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6483 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6485 /* No program header, for now. */
6486 i_ehdrp
->e_phoff
= 0;
6487 i_ehdrp
->e_phentsize
= 0;
6488 i_ehdrp
->e_phnum
= 0;
6490 /* Each bfd section is section header entry. */
6491 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6492 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6494 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6495 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6496 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6497 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6498 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6499 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6500 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6501 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6502 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6508 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6510 FIXME: We used to have code here to sort the PT_LOAD segments into
6511 ascending order, as per the ELF spec. But this breaks some programs,
6512 including the Linux kernel. But really either the spec should be
6513 changed or the programs updated. */
6516 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6518 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6520 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6521 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6522 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6523 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6524 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6526 /* Find the lowest p_vaddr in PT_LOAD segments. */
6527 bfd_vma p_vaddr
= (bfd_vma
) -1;
6528 for (; segment
< end_segment
; segment
++)
6529 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6530 p_vaddr
= segment
->p_vaddr
;
6532 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6533 segments is non-zero. */
6535 i_ehdrp
->e_type
= ET_EXEC
;
6540 /* Assign file positions for all the reloc sections which are not part
6541 of the loadable file image, and the file position of section headers. */
6544 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6547 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6548 Elf_Internal_Shdr
*shdrp
;
6549 Elf_Internal_Ehdr
*i_ehdrp
;
6550 const struct elf_backend_data
*bed
;
6552 off
= elf_next_file_pos (abfd
);
6554 shdrpp
= elf_elfsections (abfd
);
6555 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6556 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6559 if (shdrp
->sh_offset
== -1)
6561 asection
*sec
= shdrp
->bfd_section
;
6562 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6563 || shdrp
->sh_type
== SHT_RELA
);
6564 bfd_boolean is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6567 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6569 if (!is_rel
&& !is_ctf
)
6571 const char *name
= sec
->name
;
6572 struct bfd_elf_section_data
*d
;
6574 /* Compress DWARF debug sections. */
6575 if (!bfd_compress_section (abfd
, sec
,
6579 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6580 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6582 /* If section is compressed with zlib-gnu, convert
6583 section name from .debug_* to .zdebug_*. */
6585 = convert_debug_to_zdebug (abfd
, name
);
6586 if (new_name
== NULL
)
6590 /* Add section name to section name section. */
6591 if (shdrp
->sh_name
!= (unsigned int) -1)
6594 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6596 d
= elf_section_data (sec
);
6598 /* Add reloc section name to section name section. */
6600 && !_bfd_elf_set_reloc_sh_name (abfd
,
6605 && !_bfd_elf_set_reloc_sh_name (abfd
,
6610 /* Update section size and contents. */
6611 shdrp
->sh_size
= sec
->size
;
6612 shdrp
->contents
= sec
->contents
;
6613 shdrp
->bfd_section
->contents
= NULL
;
6617 /* Update section size and contents. */
6618 shdrp
->sh_size
= sec
->size
;
6619 shdrp
->contents
= sec
->contents
;
6622 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6629 /* Place section name section after DWARF debug sections have been
6631 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6632 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6633 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6634 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6636 /* Place the section headers. */
6637 i_ehdrp
= elf_elfheader (abfd
);
6638 bed
= get_elf_backend_data (abfd
);
6639 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6640 i_ehdrp
->e_shoff
= off
;
6641 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6642 elf_next_file_pos (abfd
) = off
;
6648 _bfd_elf_write_object_contents (bfd
*abfd
)
6650 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6651 Elf_Internal_Shdr
**i_shdrp
;
6653 unsigned int count
, num_sec
;
6654 struct elf_obj_tdata
*t
;
6656 if (! abfd
->output_has_begun
6657 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6659 /* Do not rewrite ELF data when the BFD has been opened for update.
6660 abfd->output_has_begun was set to TRUE on opening, so creation of new
6661 sections, and modification of existing section sizes was restricted.
6662 This means the ELF header, program headers and section headers can't have
6664 If the contents of any sections has been modified, then those changes have
6665 already been written to the BFD. */
6666 else if (abfd
->direction
== both_direction
)
6668 BFD_ASSERT (abfd
->output_has_begun
);
6672 i_shdrp
= elf_elfsections (abfd
);
6675 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6679 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6682 /* After writing the headers, we need to write the sections too... */
6683 num_sec
= elf_numsections (abfd
);
6684 for (count
= 1; count
< num_sec
; count
++)
6686 i_shdrp
[count
]->sh_name
6687 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6688 i_shdrp
[count
]->sh_name
);
6689 if (bed
->elf_backend_section_processing
)
6690 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6692 if (i_shdrp
[count
]->contents
)
6694 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6696 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6697 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6702 /* Write out the section header names. */
6703 t
= elf_tdata (abfd
);
6704 if (elf_shstrtab (abfd
) != NULL
6705 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6706 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6709 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6712 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6715 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6716 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6717 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6723 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6725 /* Hopefully this can be done just like an object file. */
6726 return _bfd_elf_write_object_contents (abfd
);
6729 /* Given a section, search the header to find them. */
6732 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6734 const struct elf_backend_data
*bed
;
6735 unsigned int sec_index
;
6737 if (elf_section_data (asect
) != NULL
6738 && elf_section_data (asect
)->this_idx
!= 0)
6739 return elf_section_data (asect
)->this_idx
;
6741 if (bfd_is_abs_section (asect
))
6742 sec_index
= SHN_ABS
;
6743 else if (bfd_is_com_section (asect
))
6744 sec_index
= SHN_COMMON
;
6745 else if (bfd_is_und_section (asect
))
6746 sec_index
= SHN_UNDEF
;
6748 sec_index
= SHN_BAD
;
6750 bed
= get_elf_backend_data (abfd
);
6751 if (bed
->elf_backend_section_from_bfd_section
)
6753 int retval
= sec_index
;
6755 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6759 if (sec_index
== SHN_BAD
)
6760 bfd_set_error (bfd_error_nonrepresentable_section
);
6765 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6769 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6771 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6773 flagword flags
= asym_ptr
->flags
;
6775 /* When gas creates relocations against local labels, it creates its
6776 own symbol for the section, but does put the symbol into the
6777 symbol chain, so udata is 0. When the linker is generating
6778 relocatable output, this section symbol may be for one of the
6779 input sections rather than the output section. */
6780 if (asym_ptr
->udata
.i
== 0
6781 && (flags
& BSF_SECTION_SYM
)
6782 && asym_ptr
->section
)
6787 sec
= asym_ptr
->section
;
6788 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6789 sec
= sec
->output_section
;
6790 if (sec
->owner
== abfd
6791 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6792 && elf_section_syms (abfd
)[indx
] != NULL
)
6793 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6796 idx
= asym_ptr
->udata
.i
;
6800 /* This case can occur when using --strip-symbol on a symbol
6801 which is used in a relocation entry. */
6803 /* xgettext:c-format */
6804 (_("%pB: symbol `%s' required but not present"),
6805 abfd
, bfd_asymbol_name (asym_ptr
));
6806 bfd_set_error (bfd_error_no_symbols
);
6813 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6814 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6822 /* Rewrite program header information. */
6825 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6827 Elf_Internal_Ehdr
*iehdr
;
6828 struct elf_segment_map
*map
;
6829 struct elf_segment_map
*map_first
;
6830 struct elf_segment_map
**pointer_to_map
;
6831 Elf_Internal_Phdr
*segment
;
6834 unsigned int num_segments
;
6835 bfd_boolean phdr_included
= FALSE
;
6836 bfd_boolean p_paddr_valid
;
6837 bfd_vma maxpagesize
;
6838 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6839 unsigned int phdr_adjust_num
= 0;
6840 const struct elf_backend_data
*bed
;
6841 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
6843 bed
= get_elf_backend_data (ibfd
);
6844 iehdr
= elf_elfheader (ibfd
);
6847 pointer_to_map
= &map_first
;
6849 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6850 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6852 /* Returns the end address of the segment + 1. */
6853 #define SEGMENT_END(segment, start) \
6854 (start + (segment->p_memsz > segment->p_filesz \
6855 ? segment->p_memsz : segment->p_filesz))
6857 #define SECTION_SIZE(section, segment) \
6858 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6859 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6860 ? section->size : 0)
6862 /* Returns TRUE if the given section is contained within
6863 the given segment. VMA addresses are compared. */
6864 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6865 (section->vma * (opb) >= segment->p_vaddr \
6866 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6867 <= (SEGMENT_END (segment, segment->p_vaddr))))
6869 /* Returns TRUE if the given section is contained within
6870 the given segment. LMA addresses are compared. */
6871 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6872 (section->lma * (opb) >= base \
6873 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6874 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6875 <= SEGMENT_END (segment, base)))
6877 /* Handle PT_NOTE segment. */
6878 #define IS_NOTE(p, s) \
6879 (p->p_type == PT_NOTE \
6880 && elf_section_type (s) == SHT_NOTE \
6881 && (bfd_vma) s->filepos >= p->p_offset \
6882 && ((bfd_vma) s->filepos + s->size \
6883 <= p->p_offset + p->p_filesz))
6885 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6887 #define IS_COREFILE_NOTE(p, s) \
6889 && bfd_get_format (ibfd) == bfd_core \
6893 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6894 linker, which generates a PT_INTERP section with p_vaddr and
6895 p_memsz set to 0. */
6896 #define IS_SOLARIS_PT_INTERP(p, s) \
6898 && p->p_paddr == 0 \
6899 && p->p_memsz == 0 \
6900 && p->p_filesz > 0 \
6901 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6903 && (bfd_vma) s->filepos >= p->p_offset \
6904 && ((bfd_vma) s->filepos + s->size \
6905 <= p->p_offset + p->p_filesz))
6907 /* Decide if the given section should be included in the given segment.
6908 A section will be included if:
6909 1. It is within the address space of the segment -- we use the LMA
6910 if that is set for the segment and the VMA otherwise,
6911 2. It is an allocated section or a NOTE section in a PT_NOTE
6913 3. There is an output section associated with it,
6914 4. The section has not already been allocated to a previous segment.
6915 5. PT_GNU_STACK segments do not include any sections.
6916 6. PT_TLS segment includes only SHF_TLS sections.
6917 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6918 8. PT_DYNAMIC should not contain empty sections at the beginning
6919 (with the possible exception of .dynamic). */
6920 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6921 ((((segment->p_paddr \
6922 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6923 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
6924 && (section->flags & SEC_ALLOC) != 0) \
6925 || IS_NOTE (segment, section)) \
6926 && segment->p_type != PT_GNU_STACK \
6927 && (segment->p_type != PT_TLS \
6928 || (section->flags & SEC_THREAD_LOCAL)) \
6929 && (segment->p_type == PT_LOAD \
6930 || segment->p_type == PT_TLS \
6931 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6932 && (segment->p_type != PT_DYNAMIC \
6933 || SECTION_SIZE (section, segment) > 0 \
6934 || (segment->p_paddr \
6935 ? segment->p_paddr != section->lma * (opb) \
6936 : segment->p_vaddr != section->vma * (opb)) \
6937 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6938 && (segment->p_type != PT_LOAD || !section->segment_mark))
6940 /* If the output section of a section in the input segment is NULL,
6941 it is removed from the corresponding output segment. */
6942 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6943 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
6944 && section->output_section != NULL)
6946 /* Returns TRUE iff seg1 starts after the end of seg2. */
6947 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6948 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6950 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6951 their VMA address ranges and their LMA address ranges overlap.
6952 It is possible to have overlapping VMA ranges without overlapping LMA
6953 ranges. RedBoot images for example can have both .data and .bss mapped
6954 to the same VMA range, but with the .data section mapped to a different
6956 #define SEGMENT_OVERLAPS(seg1, seg2) \
6957 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6958 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6959 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6960 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6962 /* Initialise the segment mark field. */
6963 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6964 section
->segment_mark
= FALSE
;
6966 /* The Solaris linker creates program headers in which all the
6967 p_paddr fields are zero. When we try to objcopy or strip such a
6968 file, we get confused. Check for this case, and if we find it
6969 don't set the p_paddr_valid fields. */
6970 p_paddr_valid
= FALSE
;
6971 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6974 if (segment
->p_paddr
!= 0)
6976 p_paddr_valid
= TRUE
;
6980 /* Scan through the segments specified in the program header
6981 of the input BFD. For this first scan we look for overlaps
6982 in the loadable segments. These can be created by weird
6983 parameters to objcopy. Also, fix some solaris weirdness. */
6984 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6989 Elf_Internal_Phdr
*segment2
;
6991 if (segment
->p_type
== PT_INTERP
)
6992 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6993 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6995 /* Mininal change so that the normal section to segment
6996 assignment code will work. */
6997 segment
->p_vaddr
= section
->vma
* opb
;
7001 if (segment
->p_type
!= PT_LOAD
)
7003 /* Remove PT_GNU_RELRO segment. */
7004 if (segment
->p_type
== PT_GNU_RELRO
)
7005 segment
->p_type
= PT_NULL
;
7009 /* Determine if this segment overlaps any previous segments. */
7010 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
7012 bfd_signed_vma extra_length
;
7014 if (segment2
->p_type
!= PT_LOAD
7015 || !SEGMENT_OVERLAPS (segment
, segment2
))
7018 /* Merge the two segments together. */
7019 if (segment2
->p_vaddr
< segment
->p_vaddr
)
7021 /* Extend SEGMENT2 to include SEGMENT and then delete
7023 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
7024 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
7026 if (extra_length
> 0)
7028 segment2
->p_memsz
+= extra_length
;
7029 segment2
->p_filesz
+= extra_length
;
7032 segment
->p_type
= PT_NULL
;
7034 /* Since we have deleted P we must restart the outer loop. */
7036 segment
= elf_tdata (ibfd
)->phdr
;
7041 /* Extend SEGMENT to include SEGMENT2 and then delete
7043 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
7044 - SEGMENT_END (segment
, segment
->p_vaddr
));
7046 if (extra_length
> 0)
7048 segment
->p_memsz
+= extra_length
;
7049 segment
->p_filesz
+= extra_length
;
7052 segment2
->p_type
= PT_NULL
;
7057 /* The second scan attempts to assign sections to segments. */
7058 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7062 unsigned int section_count
;
7063 asection
**sections
;
7064 asection
*output_section
;
7066 asection
*matching_lma
;
7067 asection
*suggested_lma
;
7070 asection
*first_section
;
7072 if (segment
->p_type
== PT_NULL
)
7075 first_section
= NULL
;
7076 /* Compute how many sections might be placed into this segment. */
7077 for (section
= ibfd
->sections
, section_count
= 0;
7079 section
= section
->next
)
7081 /* Find the first section in the input segment, which may be
7082 removed from the corresponding output segment. */
7083 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7085 if (first_section
== NULL
)
7086 first_section
= section
;
7087 if (section
->output_section
!= NULL
)
7092 /* Allocate a segment map big enough to contain
7093 all of the sections we have selected. */
7094 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7095 amt
+= section_count
* sizeof (asection
*);
7096 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7100 /* Initialise the fields of the segment map. Default to
7101 using the physical address of the segment in the input BFD. */
7103 map
->p_type
= segment
->p_type
;
7104 map
->p_flags
= segment
->p_flags
;
7105 map
->p_flags_valid
= 1;
7107 /* If the first section in the input segment is removed, there is
7108 no need to preserve segment physical address in the corresponding
7110 if (!first_section
|| first_section
->output_section
!= NULL
)
7112 map
->p_paddr
= segment
->p_paddr
;
7113 map
->p_paddr_valid
= p_paddr_valid
;
7116 /* Determine if this segment contains the ELF file header
7117 and if it contains the program headers themselves. */
7118 map
->includes_filehdr
= (segment
->p_offset
== 0
7119 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7120 map
->includes_phdrs
= 0;
7122 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7124 map
->includes_phdrs
=
7125 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7126 && (segment
->p_offset
+ segment
->p_filesz
7127 >= ((bfd_vma
) iehdr
->e_phoff
7128 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7130 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7131 phdr_included
= TRUE
;
7134 if (section_count
== 0)
7136 /* Special segments, such as the PT_PHDR segment, may contain
7137 no sections, but ordinary, loadable segments should contain
7138 something. They are allowed by the ELF spec however, so only
7139 a warning is produced.
7140 There is however the valid use case of embedded systems which
7141 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7142 flash memory with zeros. No warning is shown for that case. */
7143 if (segment
->p_type
== PT_LOAD
7144 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7145 /* xgettext:c-format */
7147 (_("%pB: warning: empty loadable segment detected"
7148 " at vaddr=%#" PRIx64
", is this intentional?"),
7149 ibfd
, (uint64_t) segment
->p_vaddr
);
7151 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7153 *pointer_to_map
= map
;
7154 pointer_to_map
= &map
->next
;
7159 /* Now scan the sections in the input BFD again and attempt
7160 to add their corresponding output sections to the segment map.
7161 The problem here is how to handle an output section which has
7162 been moved (ie had its LMA changed). There are four possibilities:
7164 1. None of the sections have been moved.
7165 In this case we can continue to use the segment LMA from the
7168 2. All of the sections have been moved by the same amount.
7169 In this case we can change the segment's LMA to match the LMA
7170 of the first section.
7172 3. Some of the sections have been moved, others have not.
7173 In this case those sections which have not been moved can be
7174 placed in the current segment which will have to have its size,
7175 and possibly its LMA changed, and a new segment or segments will
7176 have to be created to contain the other sections.
7178 4. The sections have been moved, but not by the same amount.
7179 In this case we can change the segment's LMA to match the LMA
7180 of the first section and we will have to create a new segment
7181 or segments to contain the other sections.
7183 In order to save time, we allocate an array to hold the section
7184 pointers that we are interested in. As these sections get assigned
7185 to a segment, they are removed from this array. */
7187 amt
= section_count
* sizeof (asection
*);
7188 sections
= (asection
**) bfd_malloc (amt
);
7189 if (sections
== NULL
)
7192 /* Step One: Scan for segment vs section LMA conflicts.
7193 Also add the sections to the section array allocated above.
7194 Also add the sections to the current segment. In the common
7195 case, where the sections have not been moved, this means that
7196 we have completely filled the segment, and there is nothing
7199 matching_lma
= NULL
;
7200 suggested_lma
= NULL
;
7202 for (section
= first_section
, j
= 0;
7204 section
= section
->next
)
7206 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7208 output_section
= section
->output_section
;
7210 sections
[j
++] = section
;
7212 /* The Solaris native linker always sets p_paddr to 0.
7213 We try to catch that case here, and set it to the
7214 correct value. Note - some backends require that
7215 p_paddr be left as zero. */
7217 && segment
->p_vaddr
!= 0
7218 && !bed
->want_p_paddr_set_to_zero
7220 && output_section
->lma
!= 0
7221 && (align_power (segment
->p_vaddr
7222 + (map
->includes_filehdr
7223 ? iehdr
->e_ehsize
: 0)
7224 + (map
->includes_phdrs
7225 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7227 output_section
->alignment_power
* opb
)
7228 == (output_section
->vma
* opb
)))
7229 map
->p_paddr
= segment
->p_vaddr
;
7231 /* Match up the physical address of the segment with the
7232 LMA address of the output section. */
7233 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7235 || IS_COREFILE_NOTE (segment
, section
)
7236 || (bed
->want_p_paddr_set_to_zero
7237 && IS_CONTAINED_BY_VMA (output_section
, segment
, opb
)))
7239 if (matching_lma
== NULL
7240 || output_section
->lma
< matching_lma
->lma
)
7241 matching_lma
= output_section
;
7243 /* We assume that if the section fits within the segment
7244 then it does not overlap any other section within that
7246 map
->sections
[isec
++] = output_section
;
7248 else if (suggested_lma
== NULL
)
7249 suggested_lma
= output_section
;
7251 if (j
== section_count
)
7256 BFD_ASSERT (j
== section_count
);
7258 /* Step Two: Adjust the physical address of the current segment,
7260 if (isec
== section_count
)
7262 /* All of the sections fitted within the segment as currently
7263 specified. This is the default case. Add the segment to
7264 the list of built segments and carry on to process the next
7265 program header in the input BFD. */
7266 map
->count
= section_count
;
7267 *pointer_to_map
= map
;
7268 pointer_to_map
= &map
->next
;
7271 && !bed
->want_p_paddr_set_to_zero
)
7273 bfd_vma hdr_size
= 0;
7274 if (map
->includes_filehdr
)
7275 hdr_size
= iehdr
->e_ehsize
;
7276 if (map
->includes_phdrs
)
7277 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7279 /* Account for padding before the first section in the
7281 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7282 - matching_lma
->lma
);
7290 /* Change the current segment's physical address to match
7291 the LMA of the first section that fitted, or if no
7292 section fitted, the first section. */
7293 if (matching_lma
== NULL
)
7294 matching_lma
= suggested_lma
;
7296 map
->p_paddr
= matching_lma
->lma
* opb
;
7298 /* Offset the segment physical address from the lma
7299 to allow for space taken up by elf headers. */
7300 if (map
->includes_phdrs
)
7302 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7304 /* iehdr->e_phnum is just an estimate of the number
7305 of program headers that we will need. Make a note
7306 here of the number we used and the segment we chose
7307 to hold these headers, so that we can adjust the
7308 offset when we know the correct value. */
7309 phdr_adjust_num
= iehdr
->e_phnum
;
7310 phdr_adjust_seg
= map
;
7313 if (map
->includes_filehdr
)
7315 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7316 map
->p_paddr
-= iehdr
->e_ehsize
;
7317 /* We've subtracted off the size of headers from the
7318 first section lma, but there may have been some
7319 alignment padding before that section too. Try to
7320 account for that by adjusting the segment lma down to
7321 the same alignment. */
7322 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7323 align
= segment
->p_align
;
7324 map
->p_paddr
&= -(align
* opb
);
7328 /* Step Three: Loop over the sections again, this time assigning
7329 those that fit to the current segment and removing them from the
7330 sections array; but making sure not to leave large gaps. Once all
7331 possible sections have been assigned to the current segment it is
7332 added to the list of built segments and if sections still remain
7333 to be assigned, a new segment is constructed before repeating
7339 suggested_lma
= NULL
;
7341 /* Fill the current segment with sections that fit. */
7342 for (j
= 0; j
< section_count
; j
++)
7344 section
= sections
[j
];
7346 if (section
== NULL
)
7349 output_section
= section
->output_section
;
7351 BFD_ASSERT (output_section
!= NULL
);
7353 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7355 || IS_COREFILE_NOTE (segment
, section
))
7357 if (map
->count
== 0)
7359 /* If the first section in a segment does not start at
7360 the beginning of the segment, then something is
7362 if (align_power (map
->p_paddr
7363 + (map
->includes_filehdr
7364 ? iehdr
->e_ehsize
: 0)
7365 + (map
->includes_phdrs
7366 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7368 output_section
->alignment_power
* opb
)
7369 != output_section
->lma
* opb
)
7376 prev_sec
= map
->sections
[map
->count
- 1];
7378 /* If the gap between the end of the previous section
7379 and the start of this section is more than
7380 maxpagesize then we need to start a new segment. */
7381 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7383 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7384 || (prev_sec
->lma
+ prev_sec
->size
7385 > output_section
->lma
))
7387 if (suggested_lma
== NULL
)
7388 suggested_lma
= output_section
;
7394 map
->sections
[map
->count
++] = output_section
;
7397 if (segment
->p_type
== PT_LOAD
)
7398 section
->segment_mark
= TRUE
;
7400 else if (suggested_lma
== NULL
)
7401 suggested_lma
= output_section
;
7404 /* PR 23932. A corrupt input file may contain sections that cannot
7405 be assigned to any segment - because for example they have a
7406 negative size - or segments that do not contain any sections.
7407 But there are also valid reasons why a segment can be empty.
7408 So allow a count of zero. */
7410 /* Add the current segment to the list of built segments. */
7411 *pointer_to_map
= map
;
7412 pointer_to_map
= &map
->next
;
7414 if (isec
< section_count
)
7416 /* We still have not allocated all of the sections to
7417 segments. Create a new segment here, initialise it
7418 and carry on looping. */
7419 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7420 amt
+= section_count
* sizeof (asection
*);
7421 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7428 /* Initialise the fields of the segment map. Set the physical
7429 physical address to the LMA of the first section that has
7430 not yet been assigned. */
7432 map
->p_type
= segment
->p_type
;
7433 map
->p_flags
= segment
->p_flags
;
7434 map
->p_flags_valid
= 1;
7435 map
->p_paddr
= suggested_lma
->lma
* opb
;
7436 map
->p_paddr_valid
= p_paddr_valid
;
7437 map
->includes_filehdr
= 0;
7438 map
->includes_phdrs
= 0;
7443 bfd_set_error (bfd_error_sorry
);
7447 while (isec
< section_count
);
7452 elf_seg_map (obfd
) = map_first
;
7454 /* If we had to estimate the number of program headers that were
7455 going to be needed, then check our estimate now and adjust
7456 the offset if necessary. */
7457 if (phdr_adjust_seg
!= NULL
)
7461 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7464 if (count
> phdr_adjust_num
)
7465 phdr_adjust_seg
->p_paddr
7466 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7468 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7469 if (map
->p_type
== PT_PHDR
)
7472 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7473 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7480 #undef IS_CONTAINED_BY_VMA
7481 #undef IS_CONTAINED_BY_LMA
7483 #undef IS_COREFILE_NOTE
7484 #undef IS_SOLARIS_PT_INTERP
7485 #undef IS_SECTION_IN_INPUT_SEGMENT
7486 #undef INCLUDE_SECTION_IN_SEGMENT
7487 #undef SEGMENT_AFTER_SEGMENT
7488 #undef SEGMENT_OVERLAPS
7492 /* Copy ELF program header information. */
7495 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7497 Elf_Internal_Ehdr
*iehdr
;
7498 struct elf_segment_map
*map
;
7499 struct elf_segment_map
*map_first
;
7500 struct elf_segment_map
**pointer_to_map
;
7501 Elf_Internal_Phdr
*segment
;
7503 unsigned int num_segments
;
7504 bfd_boolean phdr_included
= FALSE
;
7505 bfd_boolean p_paddr_valid
;
7506 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7508 iehdr
= elf_elfheader (ibfd
);
7511 pointer_to_map
= &map_first
;
7513 /* If all the segment p_paddr fields are zero, don't set
7514 map->p_paddr_valid. */
7515 p_paddr_valid
= FALSE
;
7516 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7517 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7520 if (segment
->p_paddr
!= 0)
7522 p_paddr_valid
= TRUE
;
7526 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7531 unsigned int section_count
;
7533 Elf_Internal_Shdr
*this_hdr
;
7534 asection
*first_section
= NULL
;
7535 asection
*lowest_section
;
7537 /* Compute how many sections are in this segment. */
7538 for (section
= ibfd
->sections
, section_count
= 0;
7540 section
= section
->next
)
7542 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7543 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7545 if (first_section
== NULL
)
7546 first_section
= section
;
7551 /* Allocate a segment map big enough to contain
7552 all of the sections we have selected. */
7553 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7554 amt
+= section_count
* sizeof (asection
*);
7555 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7559 /* Initialize the fields of the output segment map with the
7562 map
->p_type
= segment
->p_type
;
7563 map
->p_flags
= segment
->p_flags
;
7564 map
->p_flags_valid
= 1;
7565 map
->p_paddr
= segment
->p_paddr
;
7566 map
->p_paddr_valid
= p_paddr_valid
;
7567 map
->p_align
= segment
->p_align
;
7568 map
->p_align_valid
= 1;
7569 map
->p_vaddr_offset
= 0;
7571 if (map
->p_type
== PT_GNU_RELRO
7572 || map
->p_type
== PT_GNU_STACK
)
7574 /* The PT_GNU_RELRO segment may contain the first a few
7575 bytes in the .got.plt section even if the whole .got.plt
7576 section isn't in the PT_GNU_RELRO segment. We won't
7577 change the size of the PT_GNU_RELRO segment.
7578 Similarly, PT_GNU_STACK size is significant on uclinux
7580 map
->p_size
= segment
->p_memsz
;
7581 map
->p_size_valid
= 1;
7584 /* Determine if this segment contains the ELF file header
7585 and if it contains the program headers themselves. */
7586 map
->includes_filehdr
= (segment
->p_offset
== 0
7587 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7589 map
->includes_phdrs
= 0;
7590 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7592 map
->includes_phdrs
=
7593 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7594 && (segment
->p_offset
+ segment
->p_filesz
7595 >= ((bfd_vma
) iehdr
->e_phoff
7596 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7598 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7599 phdr_included
= TRUE
;
7602 lowest_section
= NULL
;
7603 if (section_count
!= 0)
7605 unsigned int isec
= 0;
7607 for (section
= first_section
;
7609 section
= section
->next
)
7611 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7612 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7614 map
->sections
[isec
++] = section
->output_section
;
7615 if ((section
->flags
& SEC_ALLOC
) != 0)
7619 if (lowest_section
== NULL
7620 || section
->lma
< lowest_section
->lma
)
7621 lowest_section
= section
;
7623 /* Section lmas are set up from PT_LOAD header
7624 p_paddr in _bfd_elf_make_section_from_shdr.
7625 If this header has a p_paddr that disagrees
7626 with the section lma, flag the p_paddr as
7628 if ((section
->flags
& SEC_LOAD
) != 0)
7629 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7631 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7632 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7633 map
->p_paddr_valid
= FALSE
;
7635 if (isec
== section_count
)
7641 if (section_count
== 0)
7642 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7643 else if (map
->p_paddr_valid
)
7645 /* Account for padding before the first section in the segment. */
7646 bfd_vma hdr_size
= 0;
7647 if (map
->includes_filehdr
)
7648 hdr_size
= iehdr
->e_ehsize
;
7649 if (map
->includes_phdrs
)
7650 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7652 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7653 - (lowest_section
? lowest_section
->lma
: 0));
7656 map
->count
= section_count
;
7657 *pointer_to_map
= map
;
7658 pointer_to_map
= &map
->next
;
7661 elf_seg_map (obfd
) = map_first
;
7665 /* Copy private BFD data. This copies or rewrites ELF program header
7669 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7671 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7672 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7675 if (elf_tdata (ibfd
)->phdr
== NULL
)
7678 if (ibfd
->xvec
== obfd
->xvec
)
7680 /* Check to see if any sections in the input BFD
7681 covered by ELF program header have changed. */
7682 Elf_Internal_Phdr
*segment
;
7683 asection
*section
, *osec
;
7684 unsigned int i
, num_segments
;
7685 Elf_Internal_Shdr
*this_hdr
;
7686 const struct elf_backend_data
*bed
;
7688 bed
= get_elf_backend_data (ibfd
);
7690 /* Regenerate the segment map if p_paddr is set to 0. */
7691 if (bed
->want_p_paddr_set_to_zero
)
7694 /* Initialize the segment mark field. */
7695 for (section
= obfd
->sections
; section
!= NULL
;
7696 section
= section
->next
)
7697 section
->segment_mark
= FALSE
;
7699 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7700 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7704 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7705 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7706 which severly confuses things, so always regenerate the segment
7707 map in this case. */
7708 if (segment
->p_paddr
== 0
7709 && segment
->p_memsz
== 0
7710 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7713 for (section
= ibfd
->sections
;
7714 section
!= NULL
; section
= section
->next
)
7716 /* We mark the output section so that we know it comes
7717 from the input BFD. */
7718 osec
= section
->output_section
;
7720 osec
->segment_mark
= TRUE
;
7722 /* Check if this section is covered by the segment. */
7723 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7724 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7726 /* FIXME: Check if its output section is changed or
7727 removed. What else do we need to check? */
7729 || section
->flags
!= osec
->flags
7730 || section
->lma
!= osec
->lma
7731 || section
->vma
!= osec
->vma
7732 || section
->size
!= osec
->size
7733 || section
->rawsize
!= osec
->rawsize
7734 || section
->alignment_power
!= osec
->alignment_power
)
7740 /* Check to see if any output section do not come from the
7742 for (section
= obfd
->sections
; section
!= NULL
;
7743 section
= section
->next
)
7745 if (!section
->segment_mark
)
7748 section
->segment_mark
= FALSE
;
7751 return copy_elf_program_header (ibfd
, obfd
);
7755 if (ibfd
->xvec
== obfd
->xvec
)
7757 /* When rewriting program header, set the output maxpagesize to
7758 the maximum alignment of input PT_LOAD segments. */
7759 Elf_Internal_Phdr
*segment
;
7761 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7762 bfd_vma maxpagesize
= 0;
7764 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7767 if (segment
->p_type
== PT_LOAD
7768 && maxpagesize
< segment
->p_align
)
7770 /* PR 17512: file: f17299af. */
7771 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7772 /* xgettext:c-format */
7773 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7774 PRIx64
" is too large"),
7775 ibfd
, (uint64_t) segment
->p_align
);
7777 maxpagesize
= segment
->p_align
;
7780 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7781 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7784 return rewrite_elf_program_header (ibfd
, obfd
);
7787 /* Initialize private output section information from input section. */
7790 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7794 struct bfd_link_info
*link_info
)
7797 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7798 bfd_boolean final_link
= (link_info
!= NULL
7799 && !bfd_link_relocatable (link_info
));
7801 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7802 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7805 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7807 /* If this is a known ABI section, ELF section type and flags may
7808 have been set up when OSEC was created. For normal sections we
7809 allow the user to override the type and flags other than
7810 SHF_MASKOS and SHF_MASKPROC. */
7811 if (elf_section_type (osec
) == SHT_PROGBITS
7812 || elf_section_type (osec
) == SHT_NOTE
7813 || elf_section_type (osec
) == SHT_NOBITS
)
7814 elf_section_type (osec
) = SHT_NULL
;
7815 /* For objcopy and relocatable link, copy the ELF section type from
7816 the input file if the BFD section flags are the same. (If they
7817 are different the user may be doing something like
7818 "objcopy --set-section-flags .text=alloc,data".) For a final
7819 link allow some flags that the linker clears to differ. */
7820 if (elf_section_type (osec
) == SHT_NULL
7821 && (osec
->flags
== isec
->flags
7823 && ((osec
->flags
^ isec
->flags
)
7824 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7825 elf_section_type (osec
) = elf_section_type (isec
);
7827 /* FIXME: Is this correct for all OS/PROC specific flags? */
7828 elf_section_flags (osec
) = (elf_section_flags (isec
)
7829 & (SHF_MASKOS
| SHF_MASKPROC
));
7831 /* Copy sh_info from input for mbind section. */
7832 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7833 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7834 elf_section_data (osec
)->this_hdr
.sh_info
7835 = elf_section_data (isec
)->this_hdr
.sh_info
;
7837 /* Set things up for objcopy and relocatable link. The output
7838 SHT_GROUP section will have its elf_next_in_group pointing back
7839 to the input group members. Ignore linker created group section.
7840 See elfNN_ia64_object_p in elfxx-ia64.c. */
7841 if ((link_info
== NULL
7842 || !link_info
->resolve_section_groups
)
7843 && (elf_sec_group (isec
) == NULL
7844 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7846 if (elf_section_flags (isec
) & SHF_GROUP
)
7847 elf_section_flags (osec
) |= SHF_GROUP
;
7848 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7849 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7852 /* If not decompress, preserve SHF_COMPRESSED. */
7853 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7854 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7857 ihdr
= &elf_section_data (isec
)->this_hdr
;
7859 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7860 don't use the output section of the linked-to section since it
7861 may be NULL at this point. */
7862 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7864 ohdr
= &elf_section_data (osec
)->this_hdr
;
7865 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7866 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7869 osec
->use_rela_p
= isec
->use_rela_p
;
7874 /* Copy private section information. This copies over the entsize
7875 field, and sometimes the info field. */
7878 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7883 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7885 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7886 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7889 ihdr
= &elf_section_data (isec
)->this_hdr
;
7890 ohdr
= &elf_section_data (osec
)->this_hdr
;
7892 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7894 if (ihdr
->sh_type
== SHT_SYMTAB
7895 || ihdr
->sh_type
== SHT_DYNSYM
7896 || ihdr
->sh_type
== SHT_GNU_verneed
7897 || ihdr
->sh_type
== SHT_GNU_verdef
)
7898 ohdr
->sh_info
= ihdr
->sh_info
;
7900 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7904 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7905 necessary if we are removing either the SHT_GROUP section or any of
7906 the group member sections. DISCARDED is the value that a section's
7907 output_section has if the section will be discarded, NULL when this
7908 function is called from objcopy, bfd_abs_section_ptr when called
7912 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7916 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7917 if (elf_section_type (isec
) == SHT_GROUP
)
7919 asection
*first
= elf_next_in_group (isec
);
7920 asection
*s
= first
;
7921 bfd_size_type removed
= 0;
7925 /* If this member section is being output but the
7926 SHT_GROUP section is not, then clear the group info
7927 set up by _bfd_elf_copy_private_section_data. */
7928 if (s
->output_section
!= discarded
7929 && isec
->output_section
== discarded
)
7931 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7932 elf_group_name (s
->output_section
) = NULL
;
7936 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7937 if (s
->output_section
== discarded
7938 && isec
->output_section
!= discarded
)
7940 /* Conversely, if the member section is not being
7941 output but the SHT_GROUP section is, then adjust
7944 if (elf_sec
->rel
.hdr
!= NULL
7945 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7947 if (elf_sec
->rela
.hdr
!= NULL
7948 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7953 /* Also adjust for zero-sized relocation member
7955 if (elf_sec
->rel
.hdr
!= NULL
7956 && elf_sec
->rel
.hdr
->sh_size
== 0)
7958 if (elf_sec
->rela
.hdr
!= NULL
7959 && elf_sec
->rela
.hdr
->sh_size
== 0)
7963 s
= elf_next_in_group (s
);
7969 if (discarded
!= NULL
)
7971 /* If we've been called for ld -r, then we need to
7972 adjust the input section size. */
7973 if (isec
->rawsize
== 0)
7974 isec
->rawsize
= isec
->size
;
7975 isec
->size
= isec
->rawsize
- removed
;
7976 if (isec
->size
<= 4)
7979 isec
->flags
|= SEC_EXCLUDE
;
7984 /* Adjust the output section size when called from
7986 isec
->output_section
->size
-= removed
;
7987 if (isec
->output_section
->size
<= 4)
7989 isec
->output_section
->size
= 0;
7990 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7999 /* Copy private header information. */
8002 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
8004 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8005 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8008 /* Copy over private BFD data if it has not already been copied.
8009 This must be done here, rather than in the copy_private_bfd_data
8010 entry point, because the latter is called after the section
8011 contents have been set, which means that the program headers have
8012 already been worked out. */
8013 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
8015 if (! copy_private_bfd_data (ibfd
, obfd
))
8019 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
8022 /* Copy private symbol information. If this symbol is in a section
8023 which we did not map into a BFD section, try to map the section
8024 index correctly. We use special macro definitions for the mapped
8025 section indices; these definitions are interpreted by the
8026 swap_out_syms function. */
8028 #define MAP_ONESYMTAB (SHN_HIOS + 1)
8029 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8030 #define MAP_STRTAB (SHN_HIOS + 3)
8031 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8032 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8035 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8040 elf_symbol_type
*isym
, *osym
;
8042 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8043 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8046 isym
= elf_symbol_from (ibfd
, isymarg
);
8047 osym
= elf_symbol_from (obfd
, osymarg
);
8050 && isym
->internal_elf_sym
.st_shndx
!= 0
8052 && bfd_is_abs_section (isym
->symbol
.section
))
8056 shndx
= isym
->internal_elf_sym
.st_shndx
;
8057 if (shndx
== elf_onesymtab (ibfd
))
8058 shndx
= MAP_ONESYMTAB
;
8059 else if (shndx
== elf_dynsymtab (ibfd
))
8060 shndx
= MAP_DYNSYMTAB
;
8061 else if (shndx
== elf_strtab_sec (ibfd
))
8063 else if (shndx
== elf_shstrtab_sec (ibfd
))
8064 shndx
= MAP_SHSTRTAB
;
8065 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8066 shndx
= MAP_SYM_SHNDX
;
8067 osym
->internal_elf_sym
.st_shndx
= shndx
;
8073 /* Swap out the symbols. */
8076 swap_out_syms (bfd
*abfd
,
8077 struct elf_strtab_hash
**sttp
,
8080 const struct elf_backend_data
*bed
;
8081 unsigned int symcount
;
8083 struct elf_strtab_hash
*stt
;
8084 Elf_Internal_Shdr
*symtab_hdr
;
8085 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8086 Elf_Internal_Shdr
*symstrtab_hdr
;
8087 struct elf_sym_strtab
*symstrtab
;
8088 bfd_byte
*outbound_syms
;
8089 bfd_byte
*outbound_shndx
;
8090 unsigned long outbound_syms_index
;
8091 unsigned long outbound_shndx_index
;
8093 unsigned int num_locals
;
8095 bfd_boolean name_local_sections
;
8097 if (!elf_map_symbols (abfd
, &num_locals
))
8100 /* Dump out the symtabs. */
8101 stt
= _bfd_elf_strtab_init ();
8105 bed
= get_elf_backend_data (abfd
);
8106 symcount
= bfd_get_symcount (abfd
);
8107 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8108 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8109 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8110 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8111 symtab_hdr
->sh_info
= num_locals
+ 1;
8112 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8114 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8115 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8117 /* Allocate buffer to swap out the .strtab section. */
8118 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8119 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8121 bfd_set_error (bfd_error_no_memory
);
8122 _bfd_elf_strtab_free (stt
);
8126 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8127 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8130 bfd_set_error (bfd_error_no_memory
);
8133 _bfd_elf_strtab_free (stt
);
8136 symtab_hdr
->contents
= outbound_syms
;
8137 outbound_syms_index
= 0;
8139 outbound_shndx
= NULL
;
8140 outbound_shndx_index
= 0;
8142 if (elf_symtab_shndx_list (abfd
))
8144 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8145 if (symtab_shndx_hdr
->sh_name
!= 0)
8147 if (_bfd_mul_overflow (symcount
+ 1,
8148 sizeof (Elf_External_Sym_Shndx
), &amt
))
8150 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8151 if (outbound_shndx
== NULL
)
8154 symtab_shndx_hdr
->contents
= outbound_shndx
;
8155 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8156 symtab_shndx_hdr
->sh_size
= amt
;
8157 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8158 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8160 /* FIXME: What about any other headers in the list ? */
8163 /* Now generate the data (for "contents"). */
8165 /* Fill in zeroth symbol and swap it out. */
8166 Elf_Internal_Sym sym
;
8172 sym
.st_shndx
= SHN_UNDEF
;
8173 sym
.st_target_internal
= 0;
8174 symstrtab
[0].sym
= sym
;
8175 symstrtab
[0].dest_index
= outbound_syms_index
;
8176 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8177 outbound_syms_index
++;
8178 if (outbound_shndx
!= NULL
)
8179 outbound_shndx_index
++;
8183 = (bed
->elf_backend_name_local_section_symbols
8184 && bed
->elf_backend_name_local_section_symbols (abfd
));
8186 syms
= bfd_get_outsymbols (abfd
);
8187 for (idx
= 0; idx
< symcount
;)
8189 Elf_Internal_Sym sym
;
8190 bfd_vma value
= syms
[idx
]->value
;
8191 elf_symbol_type
*type_ptr
;
8192 flagword flags
= syms
[idx
]->flags
;
8195 if (!name_local_sections
8196 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8198 /* Local section symbols have no name. */
8199 sym
.st_name
= (unsigned long) -1;
8203 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8204 to get the final offset for st_name. */
8206 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8208 if (sym
.st_name
== (unsigned long) -1)
8212 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
8214 if ((flags
& BSF_SECTION_SYM
) == 0
8215 && bfd_is_com_section (syms
[idx
]->section
))
8217 /* ELF common symbols put the alignment into the `value' field,
8218 and the size into the `size' field. This is backwards from
8219 how BFD handles it, so reverse it here. */
8220 sym
.st_size
= value
;
8221 if (type_ptr
== NULL
8222 || type_ptr
->internal_elf_sym
.st_value
== 0)
8223 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8225 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8226 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8227 (abfd
, syms
[idx
]->section
);
8231 asection
*sec
= syms
[idx
]->section
;
8234 if (sec
->output_section
)
8236 value
+= sec
->output_offset
;
8237 sec
= sec
->output_section
;
8240 /* Don't add in the section vma for relocatable output. */
8241 if (! relocatable_p
)
8243 sym
.st_value
= value
;
8244 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8246 if (bfd_is_abs_section (sec
)
8248 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8250 /* This symbol is in a real ELF section which we did
8251 not create as a BFD section. Undo the mapping done
8252 by copy_private_symbol_data. */
8253 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8257 shndx
= elf_onesymtab (abfd
);
8260 shndx
= elf_dynsymtab (abfd
);
8263 shndx
= elf_strtab_sec (abfd
);
8266 shndx
= elf_shstrtab_sec (abfd
);
8269 if (elf_symtab_shndx_list (abfd
))
8270 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8277 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8279 if (bed
->symbol_section_index
)
8280 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8281 /* Otherwise just leave the index alone. */
8285 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8286 _bfd_error_handler (_("%pB: \
8287 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8296 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8298 if (shndx
== SHN_BAD
)
8302 /* Writing this would be a hell of a lot easier if
8303 we had some decent documentation on bfd, and
8304 knew what to expect of the library, and what to
8305 demand of applications. For example, it
8306 appears that `objcopy' might not set the
8307 section of a symbol to be a section that is
8308 actually in the output file. */
8309 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8311 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8312 if (shndx
== SHN_BAD
)
8314 /* xgettext:c-format */
8316 (_("unable to find equivalent output section"
8317 " for symbol '%s' from section '%s'"),
8318 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8320 bfd_set_error (bfd_error_invalid_operation
);
8326 sym
.st_shndx
= shndx
;
8329 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8331 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8332 type
= STT_GNU_IFUNC
;
8333 else if ((flags
& BSF_FUNCTION
) != 0)
8335 else if ((flags
& BSF_OBJECT
) != 0)
8337 else if ((flags
& BSF_RELC
) != 0)
8339 else if ((flags
& BSF_SRELC
) != 0)
8344 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8347 /* Processor-specific types. */
8348 if (type_ptr
!= NULL
8349 && bed
->elf_backend_get_symbol_type
)
8350 type
= ((*bed
->elf_backend_get_symbol_type
)
8351 (&type_ptr
->internal_elf_sym
, type
));
8353 if (flags
& BSF_SECTION_SYM
)
8355 if (flags
& BSF_GLOBAL
)
8356 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8358 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8360 else if (bfd_is_com_section (syms
[idx
]->section
))
8362 if (type
!= STT_TLS
)
8364 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8365 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8366 ? STT_COMMON
: STT_OBJECT
);
8368 type
= ((flags
& BSF_ELF_COMMON
) != 0
8369 ? STT_COMMON
: STT_OBJECT
);
8371 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8373 else if (bfd_is_und_section (syms
[idx
]->section
))
8374 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8378 else if (flags
& BSF_FILE
)
8379 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8382 int bind
= STB_LOCAL
;
8384 if (flags
& BSF_LOCAL
)
8386 else if (flags
& BSF_GNU_UNIQUE
)
8387 bind
= STB_GNU_UNIQUE
;
8388 else if (flags
& BSF_WEAK
)
8390 else if (flags
& BSF_GLOBAL
)
8393 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8396 if (type_ptr
!= NULL
)
8398 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8399 sym
.st_target_internal
8400 = type_ptr
->internal_elf_sym
.st_target_internal
;
8405 sym
.st_target_internal
= 0;
8409 symstrtab
[idx
].sym
= sym
;
8410 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8411 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8413 outbound_syms_index
++;
8414 if (outbound_shndx
!= NULL
)
8415 outbound_shndx_index
++;
8418 /* Finalize the .strtab section. */
8419 _bfd_elf_strtab_finalize (stt
);
8421 /* Swap out the .strtab section. */
8422 for (idx
= 0; idx
<= symcount
; idx
++)
8424 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8425 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8426 elfsym
->sym
.st_name
= 0;
8428 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8429 elfsym
->sym
.st_name
);
8430 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8432 + (elfsym
->dest_index
8433 * bed
->s
->sizeof_sym
)),
8435 + (elfsym
->destshndx_index
8436 * sizeof (Elf_External_Sym_Shndx
))));
8441 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8442 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8443 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8444 symstrtab_hdr
->sh_addr
= 0;
8445 symstrtab_hdr
->sh_entsize
= 0;
8446 symstrtab_hdr
->sh_link
= 0;
8447 symstrtab_hdr
->sh_info
= 0;
8448 symstrtab_hdr
->sh_addralign
= 1;
8453 /* Return the number of bytes required to hold the symtab vector.
8455 Note that we base it on the count plus 1, since we will null terminate
8456 the vector allocated based on this size. However, the ELF symbol table
8457 always has a dummy entry as symbol #0, so it ends up even. */
8460 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8462 bfd_size_type symcount
;
8464 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8466 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8467 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8469 bfd_set_error (bfd_error_file_too_big
);
8472 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8474 symtab_size
-= sizeof (asymbol
*);
8480 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8482 bfd_size_type symcount
;
8484 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8486 if (elf_dynsymtab (abfd
) == 0)
8488 bfd_set_error (bfd_error_invalid_operation
);
8492 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8493 if (symcount
>= LONG_MAX
/ sizeof (asymbol
*))
8495 bfd_set_error (bfd_error_file_too_big
);
8498 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8500 symtab_size
-= sizeof (asymbol
*);
8506 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8509 #if SIZEOF_LONG == SIZEOF_INT
8510 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8512 bfd_set_error (bfd_error_file_too_big
);
8516 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8519 /* Canonicalize the relocs. */
8522 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8529 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8531 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8534 tblptr
= section
->relocation
;
8535 for (i
= 0; i
< section
->reloc_count
; i
++)
8536 *relptr
++ = tblptr
++;
8540 return section
->reloc_count
;
8544 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8546 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8547 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8550 abfd
->symcount
= symcount
;
8555 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8556 asymbol
**allocation
)
8558 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8559 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8562 abfd
->dynsymcount
= symcount
;
8566 /* Return the size required for the dynamic reloc entries. Any loadable
8567 section that was actually installed in the BFD, and has type SHT_REL
8568 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8569 dynamic reloc section. */
8572 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8574 bfd_size_type count
;
8577 if (elf_dynsymtab (abfd
) == 0)
8579 bfd_set_error (bfd_error_invalid_operation
);
8584 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8585 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8586 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8587 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8589 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8590 if (count
> LONG_MAX
/ sizeof (arelent
*))
8592 bfd_set_error (bfd_error_file_too_big
);
8596 return count
* sizeof (arelent
*);
8599 /* Canonicalize the dynamic relocation entries. Note that we return the
8600 dynamic relocations as a single block, although they are actually
8601 associated with particular sections; the interface, which was
8602 designed for SunOS style shared libraries, expects that there is only
8603 one set of dynamic relocs. Any loadable section that was actually
8604 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8605 dynamic symbol table, is considered to be a dynamic reloc section. */
8608 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8612 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8616 if (elf_dynsymtab (abfd
) == 0)
8618 bfd_set_error (bfd_error_invalid_operation
);
8622 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8624 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8626 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8627 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8628 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8633 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8635 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8637 for (i
= 0; i
< count
; i
++)
8648 /* Read in the version information. */
8651 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8653 bfd_byte
*contents
= NULL
;
8654 unsigned int freeidx
= 0;
8657 if (elf_dynverref (abfd
) != 0)
8659 Elf_Internal_Shdr
*hdr
;
8660 Elf_External_Verneed
*everneed
;
8661 Elf_Internal_Verneed
*iverneed
;
8663 bfd_byte
*contents_end
;
8665 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8667 if (hdr
->sh_info
== 0
8668 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8670 error_return_bad_verref
:
8672 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8673 bfd_set_error (bfd_error_bad_value
);
8674 error_return_verref
:
8675 elf_tdata (abfd
)->verref
= NULL
;
8676 elf_tdata (abfd
)->cverrefs
= 0;
8680 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8681 goto error_return_verref
;
8682 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8683 if (contents
== NULL
)
8684 goto error_return_verref
;
8686 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8688 bfd_set_error (bfd_error_file_too_big
);
8689 goto error_return_verref
;
8691 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8692 if (elf_tdata (abfd
)->verref
== NULL
)
8693 goto error_return_verref
;
8695 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8696 == sizeof (Elf_External_Vernaux
));
8697 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8698 everneed
= (Elf_External_Verneed
*) contents
;
8699 iverneed
= elf_tdata (abfd
)->verref
;
8700 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8702 Elf_External_Vernaux
*evernaux
;
8703 Elf_Internal_Vernaux
*ivernaux
;
8706 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8708 iverneed
->vn_bfd
= abfd
;
8710 iverneed
->vn_filename
=
8711 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8713 if (iverneed
->vn_filename
== NULL
)
8714 goto error_return_bad_verref
;
8716 if (iverneed
->vn_cnt
== 0)
8717 iverneed
->vn_auxptr
= NULL
;
8720 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8721 sizeof (Elf_Internal_Vernaux
), &amt
))
8723 bfd_set_error (bfd_error_file_too_big
);
8724 goto error_return_verref
;
8726 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8727 bfd_alloc (abfd
, amt
);
8728 if (iverneed
->vn_auxptr
== NULL
)
8729 goto error_return_verref
;
8732 if (iverneed
->vn_aux
8733 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8734 goto error_return_bad_verref
;
8736 evernaux
= ((Elf_External_Vernaux
*)
8737 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8738 ivernaux
= iverneed
->vn_auxptr
;
8739 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8741 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8743 ivernaux
->vna_nodename
=
8744 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8745 ivernaux
->vna_name
);
8746 if (ivernaux
->vna_nodename
== NULL
)
8747 goto error_return_bad_verref
;
8749 if (ivernaux
->vna_other
> freeidx
)
8750 freeidx
= ivernaux
->vna_other
;
8752 ivernaux
->vna_nextptr
= NULL
;
8753 if (ivernaux
->vna_next
== 0)
8755 iverneed
->vn_cnt
= j
+ 1;
8758 if (j
+ 1 < iverneed
->vn_cnt
)
8759 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8761 if (ivernaux
->vna_next
8762 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8763 goto error_return_bad_verref
;
8765 evernaux
= ((Elf_External_Vernaux
*)
8766 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8769 iverneed
->vn_nextref
= NULL
;
8770 if (iverneed
->vn_next
== 0)
8772 if (i
+ 1 < hdr
->sh_info
)
8773 iverneed
->vn_nextref
= iverneed
+ 1;
8775 if (iverneed
->vn_next
8776 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8777 goto error_return_bad_verref
;
8779 everneed
= ((Elf_External_Verneed
*)
8780 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8782 elf_tdata (abfd
)->cverrefs
= i
;
8788 if (elf_dynverdef (abfd
) != 0)
8790 Elf_Internal_Shdr
*hdr
;
8791 Elf_External_Verdef
*everdef
;
8792 Elf_Internal_Verdef
*iverdef
;
8793 Elf_Internal_Verdef
*iverdefarr
;
8794 Elf_Internal_Verdef iverdefmem
;
8796 unsigned int maxidx
;
8797 bfd_byte
*contents_end_def
, *contents_end_aux
;
8799 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8801 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8803 error_return_bad_verdef
:
8805 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8806 bfd_set_error (bfd_error_bad_value
);
8807 error_return_verdef
:
8808 elf_tdata (abfd
)->verdef
= NULL
;
8809 elf_tdata (abfd
)->cverdefs
= 0;
8813 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8814 goto error_return_verdef
;
8815 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8816 if (contents
== NULL
)
8817 goto error_return_verdef
;
8819 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8820 >= sizeof (Elf_External_Verdaux
));
8821 contents_end_def
= contents
+ hdr
->sh_size
8822 - sizeof (Elf_External_Verdef
);
8823 contents_end_aux
= contents
+ hdr
->sh_size
8824 - sizeof (Elf_External_Verdaux
);
8826 /* We know the number of entries in the section but not the maximum
8827 index. Therefore we have to run through all entries and find
8829 everdef
= (Elf_External_Verdef
*) contents
;
8831 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8833 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8835 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8836 goto error_return_bad_verdef
;
8837 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8838 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8840 if (iverdefmem
.vd_next
== 0)
8843 if (iverdefmem
.vd_next
8844 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8845 goto error_return_bad_verdef
;
8847 everdef
= ((Elf_External_Verdef
*)
8848 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8851 if (default_imported_symver
)
8853 if (freeidx
> maxidx
)
8858 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8860 bfd_set_error (bfd_error_file_too_big
);
8861 goto error_return_verdef
;
8863 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8864 if (elf_tdata (abfd
)->verdef
== NULL
)
8865 goto error_return_verdef
;
8867 elf_tdata (abfd
)->cverdefs
= maxidx
;
8869 everdef
= (Elf_External_Verdef
*) contents
;
8870 iverdefarr
= elf_tdata (abfd
)->verdef
;
8871 for (i
= 0; i
< hdr
->sh_info
; i
++)
8873 Elf_External_Verdaux
*everdaux
;
8874 Elf_Internal_Verdaux
*iverdaux
;
8877 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8879 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8880 goto error_return_bad_verdef
;
8882 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8883 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8885 iverdef
->vd_bfd
= abfd
;
8887 if (iverdef
->vd_cnt
== 0)
8888 iverdef
->vd_auxptr
= NULL
;
8891 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
8892 sizeof (Elf_Internal_Verdaux
), &amt
))
8894 bfd_set_error (bfd_error_file_too_big
);
8895 goto error_return_verdef
;
8897 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8898 bfd_alloc (abfd
, amt
);
8899 if (iverdef
->vd_auxptr
== NULL
)
8900 goto error_return_verdef
;
8904 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8905 goto error_return_bad_verdef
;
8907 everdaux
= ((Elf_External_Verdaux
*)
8908 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8909 iverdaux
= iverdef
->vd_auxptr
;
8910 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8912 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8914 iverdaux
->vda_nodename
=
8915 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8916 iverdaux
->vda_name
);
8917 if (iverdaux
->vda_nodename
== NULL
)
8918 goto error_return_bad_verdef
;
8920 iverdaux
->vda_nextptr
= NULL
;
8921 if (iverdaux
->vda_next
== 0)
8923 iverdef
->vd_cnt
= j
+ 1;
8926 if (j
+ 1 < iverdef
->vd_cnt
)
8927 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8929 if (iverdaux
->vda_next
8930 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8931 goto error_return_bad_verdef
;
8933 everdaux
= ((Elf_External_Verdaux
*)
8934 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8937 iverdef
->vd_nodename
= NULL
;
8938 if (iverdef
->vd_cnt
)
8939 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8941 iverdef
->vd_nextdef
= NULL
;
8942 if (iverdef
->vd_next
== 0)
8944 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8945 iverdef
->vd_nextdef
= iverdef
+ 1;
8947 everdef
= ((Elf_External_Verdef
*)
8948 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8954 else if (default_imported_symver
)
8961 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8963 bfd_set_error (bfd_error_file_too_big
);
8966 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8967 if (elf_tdata (abfd
)->verdef
== NULL
)
8970 elf_tdata (abfd
)->cverdefs
= freeidx
;
8973 /* Create a default version based on the soname. */
8974 if (default_imported_symver
)
8976 Elf_Internal_Verdef
*iverdef
;
8977 Elf_Internal_Verdaux
*iverdaux
;
8979 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8981 iverdef
->vd_version
= VER_DEF_CURRENT
;
8982 iverdef
->vd_flags
= 0;
8983 iverdef
->vd_ndx
= freeidx
;
8984 iverdef
->vd_cnt
= 1;
8986 iverdef
->vd_bfd
= abfd
;
8988 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8989 if (iverdef
->vd_nodename
== NULL
)
8990 goto error_return_verdef
;
8991 iverdef
->vd_nextdef
= NULL
;
8992 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8993 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8994 if (iverdef
->vd_auxptr
== NULL
)
8995 goto error_return_verdef
;
8997 iverdaux
= iverdef
->vd_auxptr
;
8998 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9004 if (contents
!= NULL
)
9010 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9012 elf_symbol_type
*newsym
;
9014 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9017 newsym
->symbol
.the_bfd
= abfd
;
9018 return &newsym
->symbol
;
9022 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9026 bfd_symbol_info (symbol
, ret
);
9029 /* Return whether a symbol name implies a local symbol. Most targets
9030 use this function for the is_local_label_name entry point, but some
9034 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9037 /* Normal local symbols start with ``.L''. */
9038 if (name
[0] == '.' && name
[1] == 'L')
9041 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9042 DWARF debugging symbols starting with ``..''. */
9043 if (name
[0] == '.' && name
[1] == '.')
9046 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9047 emitting DWARF debugging output. I suspect this is actually a
9048 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9049 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9050 underscore to be emitted on some ELF targets). For ease of use,
9051 we treat such symbols as local. */
9052 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9055 /* Treat assembler generated fake symbols, dollar local labels and
9056 forward-backward labels (aka local labels) as locals.
9057 These labels have the form:
9059 L0^A.* (fake symbols)
9061 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9063 Versions which start with .L will have already been matched above,
9064 so we only need to match the rest. */
9065 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9067 bfd_boolean ret
= FALSE
;
9071 for (p
= name
+ 2; (c
= *p
); p
++)
9073 if (c
== 1 || c
== 2)
9075 if (c
== 1 && p
== name
+ 2)
9076 /* A fake symbol. */
9079 /* FIXME: We are being paranoid here and treating symbols like
9080 L0^Bfoo as if there were non-local, on the grounds that the
9081 assembler will never generate them. But can any symbol
9082 containing an ASCII value in the range 1-31 ever be anything
9083 other than some kind of local ? */
9100 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9101 asymbol
*symbol ATTRIBUTE_UNUSED
)
9108 _bfd_elf_set_arch_mach (bfd
*abfd
,
9109 enum bfd_architecture arch
,
9110 unsigned long machine
)
9112 /* If this isn't the right architecture for this backend, and this
9113 isn't the generic backend, fail. */
9114 if (arch
!= get_elf_backend_data (abfd
)->arch
9115 && arch
!= bfd_arch_unknown
9116 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9119 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9122 /* Find the nearest line to a particular section and offset,
9123 for error reporting. */
9126 _bfd_elf_find_nearest_line (bfd
*abfd
,
9130 const char **filename_ptr
,
9131 const char **functionname_ptr
,
9132 unsigned int *line_ptr
,
9133 unsigned int *discriminator_ptr
)
9137 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
9138 filename_ptr
, functionname_ptr
,
9139 line_ptr
, discriminator_ptr
,
9140 dwarf_debug_sections
,
9141 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9144 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9145 filename_ptr
, functionname_ptr
, line_ptr
))
9147 if (!*functionname_ptr
)
9148 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9149 *filename_ptr
? NULL
: filename_ptr
,
9154 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9155 &found
, filename_ptr
,
9156 functionname_ptr
, line_ptr
,
9157 &elf_tdata (abfd
)->line_info
))
9159 if (found
&& (*functionname_ptr
|| *line_ptr
))
9162 if (symbols
== NULL
)
9165 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9166 filename_ptr
, functionname_ptr
))
9173 /* Find the line for a symbol. */
9176 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9177 const char **filename_ptr
, unsigned int *line_ptr
)
9179 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9180 filename_ptr
, NULL
, line_ptr
, NULL
,
9181 dwarf_debug_sections
,
9182 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9185 /* After a call to bfd_find_nearest_line, successive calls to
9186 bfd_find_inliner_info can be used to get source information about
9187 each level of function inlining that terminated at the address
9188 passed to bfd_find_nearest_line. Currently this is only supported
9189 for DWARF2 with appropriate DWARF3 extensions. */
9192 _bfd_elf_find_inliner_info (bfd
*abfd
,
9193 const char **filename_ptr
,
9194 const char **functionname_ptr
,
9195 unsigned int *line_ptr
)
9198 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9199 functionname_ptr
, line_ptr
,
9200 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9205 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9207 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9208 int ret
= bed
->s
->sizeof_ehdr
;
9210 if (!bfd_link_relocatable (info
))
9212 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9214 if (phdr_size
== (bfd_size_type
) -1)
9216 struct elf_segment_map
*m
;
9219 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9220 phdr_size
+= bed
->s
->sizeof_phdr
;
9223 phdr_size
= get_program_header_size (abfd
, info
);
9226 elf_program_header_size (abfd
) = phdr_size
;
9234 _bfd_elf_set_section_contents (bfd
*abfd
,
9236 const void *location
,
9238 bfd_size_type count
)
9240 Elf_Internal_Shdr
*hdr
;
9243 if (! abfd
->output_has_begun
9244 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9250 hdr
= &elf_section_data (section
)->this_hdr
;
9251 if (hdr
->sh_offset
== (file_ptr
) -1)
9253 unsigned char *contents
;
9255 if (bfd_section_is_ctf (section
))
9256 /* Nothing to do with this section: the contents are generated
9260 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9263 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9265 bfd_set_error (bfd_error_invalid_operation
);
9269 if ((offset
+ count
) > hdr
->sh_size
)
9272 (_("%pB:%pA: error: attempting to write over the end of the section"),
9275 bfd_set_error (bfd_error_invalid_operation
);
9279 contents
= hdr
->contents
;
9280 if (contents
== NULL
)
9283 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9286 bfd_set_error (bfd_error_invalid_operation
);
9290 memcpy (contents
+ offset
, location
, count
);
9294 pos
= hdr
->sh_offset
+ offset
;
9295 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9296 || bfd_bwrite (location
, count
, abfd
) != count
)
9303 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9304 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9305 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9311 /* Try to convert a non-ELF reloc into an ELF one. */
9314 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9316 /* Check whether we really have an ELF howto. */
9318 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9320 bfd_reloc_code_real_type code
;
9321 reloc_howto_type
*howto
;
9323 /* Alien reloc: Try to determine its type to replace it with an
9324 equivalent ELF reloc. */
9326 if (areloc
->howto
->pc_relative
)
9328 switch (areloc
->howto
->bitsize
)
9331 code
= BFD_RELOC_8_PCREL
;
9334 code
= BFD_RELOC_12_PCREL
;
9337 code
= BFD_RELOC_16_PCREL
;
9340 code
= BFD_RELOC_24_PCREL
;
9343 code
= BFD_RELOC_32_PCREL
;
9346 code
= BFD_RELOC_64_PCREL
;
9352 howto
= bfd_reloc_type_lookup (abfd
, code
);
9354 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9356 if (howto
->pcrel_offset
)
9357 areloc
->addend
+= areloc
->address
;
9359 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9364 switch (areloc
->howto
->bitsize
)
9370 code
= BFD_RELOC_14
;
9373 code
= BFD_RELOC_16
;
9376 code
= BFD_RELOC_26
;
9379 code
= BFD_RELOC_32
;
9382 code
= BFD_RELOC_64
;
9388 howto
= bfd_reloc_type_lookup (abfd
, code
);
9392 areloc
->howto
= howto
;
9400 /* xgettext:c-format */
9401 _bfd_error_handler (_("%pB: %s unsupported"),
9402 abfd
, areloc
->howto
->name
);
9403 bfd_set_error (bfd_error_sorry
);
9408 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9410 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9411 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
9413 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9414 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9415 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9418 return _bfd_generic_close_and_cleanup (abfd
);
9421 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9422 in the relocation's offset. Thus we cannot allow any sort of sanity
9423 range-checking to interfere. There is nothing else to do in processing
9426 bfd_reloc_status_type
9427 _bfd_elf_rel_vtable_reloc_fn
9428 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9429 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9430 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9431 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9433 return bfd_reloc_ok
;
9436 /* Elf core file support. Much of this only works on native
9437 toolchains, since we rely on knowing the
9438 machine-dependent procfs structure in order to pick
9439 out details about the corefile. */
9441 #ifdef HAVE_SYS_PROCFS_H
9442 /* Needed for new procfs interface on sparc-solaris. */
9443 # define _STRUCTURED_PROC 1
9444 # include <sys/procfs.h>
9447 /* Return a PID that identifies a "thread" for threaded cores, or the
9448 PID of the main process for non-threaded cores. */
9451 elfcore_make_pid (bfd
*abfd
)
9455 pid
= elf_tdata (abfd
)->core
->lwpid
;
9457 pid
= elf_tdata (abfd
)->core
->pid
;
9462 /* If there isn't a section called NAME, make one, using
9463 data from SECT. Note, this function will generate a
9464 reference to NAME, so you shouldn't deallocate or
9468 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9472 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9475 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9479 sect2
->size
= sect
->size
;
9480 sect2
->filepos
= sect
->filepos
;
9481 sect2
->alignment_power
= sect
->alignment_power
;
9485 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9486 actually creates up to two pseudosections:
9487 - For the single-threaded case, a section named NAME, unless
9488 such a section already exists.
9489 - For the multi-threaded case, a section named "NAME/PID", where
9490 PID is elfcore_make_pid (abfd).
9491 Both pseudosections have identical contents. */
9493 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9499 char *threaded_name
;
9503 /* Build the section name. */
9505 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9506 len
= strlen (buf
) + 1;
9507 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9508 if (threaded_name
== NULL
)
9510 memcpy (threaded_name
, buf
, len
);
9512 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9517 sect
->filepos
= filepos
;
9518 sect
->alignment_power
= 2;
9520 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9524 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9527 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9533 sect
->size
= note
->descsz
- offs
;
9534 sect
->filepos
= note
->descpos
+ offs
;
9535 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9540 /* prstatus_t exists on:
9542 linux 2.[01] + glibc
9546 #if defined (HAVE_PRSTATUS_T)
9549 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9554 if (note
->descsz
== sizeof (prstatus_t
))
9558 size
= sizeof (prstat
.pr_reg
);
9559 offset
= offsetof (prstatus_t
, pr_reg
);
9560 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9562 /* Do not overwrite the core signal if it
9563 has already been set by another thread. */
9564 if (elf_tdata (abfd
)->core
->signal
== 0)
9565 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9566 if (elf_tdata (abfd
)->core
->pid
== 0)
9567 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9569 /* pr_who exists on:
9572 pr_who doesn't exist on:
9575 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9576 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9578 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9581 #if defined (HAVE_PRSTATUS32_T)
9582 else if (note
->descsz
== sizeof (prstatus32_t
))
9584 /* 64-bit host, 32-bit corefile */
9585 prstatus32_t prstat
;
9587 size
= sizeof (prstat
.pr_reg
);
9588 offset
= offsetof (prstatus32_t
, pr_reg
);
9589 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9591 /* Do not overwrite the core signal if it
9592 has already been set by another thread. */
9593 if (elf_tdata (abfd
)->core
->signal
== 0)
9594 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9595 if (elf_tdata (abfd
)->core
->pid
== 0)
9596 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9598 /* pr_who exists on:
9601 pr_who doesn't exist on:
9604 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9605 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9607 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9610 #endif /* HAVE_PRSTATUS32_T */
9613 /* Fail - we don't know how to handle any other
9614 note size (ie. data object type). */
9618 /* Make a ".reg/999" section and a ".reg" section. */
9619 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9620 size
, note
->descpos
+ offset
);
9622 #endif /* defined (HAVE_PRSTATUS_T) */
9624 /* Create a pseudosection containing the exact contents of NOTE. */
9626 elfcore_make_note_pseudosection (bfd
*abfd
,
9628 Elf_Internal_Note
*note
)
9630 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9631 note
->descsz
, note
->descpos
);
9634 /* There isn't a consistent prfpregset_t across platforms,
9635 but it doesn't matter, because we don't have to pick this
9636 data structure apart. */
9639 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9641 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9644 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9645 type of NT_PRXFPREG. Just include the whole note's contents
9649 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9651 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9654 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9655 with a note type of NT_X86_XSTATE. Just include the whole note's
9656 contents literally. */
9659 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9661 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9665 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9667 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9671 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9673 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9677 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9679 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9683 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9685 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9689 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9691 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9695 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9697 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9701 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9703 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9707 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9709 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9713 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9715 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9719 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9721 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9725 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9727 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9731 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9733 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9737 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9739 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9743 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9745 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9749 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9751 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9755 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9757 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9761 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9763 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9767 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9769 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9773 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9775 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9779 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9781 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9785 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9787 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9791 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9793 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9797 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9799 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9803 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9805 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9809 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9811 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9815 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9817 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9821 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9823 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9827 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9829 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9833 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9835 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9839 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9841 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9845 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9847 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9851 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9853 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9857 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9859 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9863 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9865 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9868 #if defined (HAVE_PRPSINFO_T)
9869 typedef prpsinfo_t elfcore_psinfo_t
;
9870 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9871 typedef prpsinfo32_t elfcore_psinfo32_t
;
9875 #if defined (HAVE_PSINFO_T)
9876 typedef psinfo_t elfcore_psinfo_t
;
9877 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9878 typedef psinfo32_t elfcore_psinfo32_t
;
9882 /* return a malloc'ed copy of a string at START which is at
9883 most MAX bytes long, possibly without a terminating '\0'.
9884 the copy will always have a terminating '\0'. */
9887 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9890 char *end
= (char *) memchr (start
, '\0', max
);
9898 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9902 memcpy (dups
, start
, len
);
9908 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9910 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9912 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9914 elfcore_psinfo_t psinfo
;
9916 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9918 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9919 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9921 elf_tdata (abfd
)->core
->program
9922 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9923 sizeof (psinfo
.pr_fname
));
9925 elf_tdata (abfd
)->core
->command
9926 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9927 sizeof (psinfo
.pr_psargs
));
9929 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9930 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9932 /* 64-bit host, 32-bit corefile */
9933 elfcore_psinfo32_t psinfo
;
9935 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9937 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9938 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9940 elf_tdata (abfd
)->core
->program
9941 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9942 sizeof (psinfo
.pr_fname
));
9944 elf_tdata (abfd
)->core
->command
9945 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9946 sizeof (psinfo
.pr_psargs
));
9952 /* Fail - we don't know how to handle any other
9953 note size (ie. data object type). */
9957 /* Note that for some reason, a spurious space is tacked
9958 onto the end of the args in some (at least one anyway)
9959 implementations, so strip it off if it exists. */
9962 char *command
= elf_tdata (abfd
)->core
->command
;
9963 int n
= strlen (command
);
9965 if (0 < n
&& command
[n
- 1] == ' ')
9966 command
[n
- 1] = '\0';
9971 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9973 #if defined (HAVE_PSTATUS_T)
9975 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9977 if (note
->descsz
== sizeof (pstatus_t
)
9978 #if defined (HAVE_PXSTATUS_T)
9979 || note
->descsz
== sizeof (pxstatus_t
)
9985 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9987 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9989 #if defined (HAVE_PSTATUS32_T)
9990 else if (note
->descsz
== sizeof (pstatus32_t
))
9992 /* 64-bit host, 32-bit corefile */
9995 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9997 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10000 /* Could grab some more details from the "representative"
10001 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10002 NT_LWPSTATUS note, presumably. */
10006 #endif /* defined (HAVE_PSTATUS_T) */
10008 #if defined (HAVE_LWPSTATUS_T)
10010 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10012 lwpstatus_t lwpstat
;
10018 if (note
->descsz
!= sizeof (lwpstat
)
10019 #if defined (HAVE_LWPXSTATUS_T)
10020 && note
->descsz
!= sizeof (lwpxstatus_t
)
10025 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10027 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10028 /* Do not overwrite the core signal if it has already been set by
10030 if (elf_tdata (abfd
)->core
->signal
== 0)
10031 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10033 /* Make a ".reg/999" section. */
10035 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10036 len
= strlen (buf
) + 1;
10037 name
= bfd_alloc (abfd
, len
);
10040 memcpy (name
, buf
, len
);
10042 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10046 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10047 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10048 sect
->filepos
= note
->descpos
10049 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10052 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10053 sect
->size
= sizeof (lwpstat
.pr_reg
);
10054 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10057 sect
->alignment_power
= 2;
10059 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10062 /* Make a ".reg2/999" section */
10064 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10065 len
= strlen (buf
) + 1;
10066 name
= bfd_alloc (abfd
, len
);
10069 memcpy (name
, buf
, len
);
10071 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10075 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10076 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10077 sect
->filepos
= note
->descpos
10078 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10081 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10082 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10083 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10086 sect
->alignment_power
= 2;
10088 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10090 #endif /* defined (HAVE_LWPSTATUS_T) */
10093 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10100 int is_active_thread
;
10103 if (note
->descsz
< 728)
10106 if (! CONST_STRNEQ (note
->namedata
, "win32"))
10109 type
= bfd_get_32 (abfd
, note
->descdata
);
10113 case 1 /* NOTE_INFO_PROCESS */:
10114 /* FIXME: need to add ->core->command. */
10115 /* process_info.pid */
10116 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10117 /* process_info.signal */
10118 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10121 case 2 /* NOTE_INFO_THREAD */:
10122 /* Make a ".reg/999" section. */
10123 /* thread_info.tid */
10124 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
10126 len
= strlen (buf
) + 1;
10127 name
= (char *) bfd_alloc (abfd
, len
);
10131 memcpy (name
, buf
, len
);
10133 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10137 /* sizeof (thread_info.thread_context) */
10139 /* offsetof (thread_info.thread_context) */
10140 sect
->filepos
= note
->descpos
+ 12;
10141 sect
->alignment_power
= 2;
10143 /* thread_info.is_active_thread */
10144 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10146 if (is_active_thread
)
10147 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10151 case 3 /* NOTE_INFO_MODULE */:
10152 /* Make a ".module/xxxxxxxx" section. */
10153 /* module_info.base_address */
10154 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10155 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10157 len
= strlen (buf
) + 1;
10158 name
= (char *) bfd_alloc (abfd
, len
);
10162 memcpy (name
, buf
, len
);
10164 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10169 sect
->size
= note
->descsz
;
10170 sect
->filepos
= note
->descpos
;
10171 sect
->alignment_power
= 2;
10182 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10184 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10186 switch (note
->type
)
10192 if (bed
->elf_backend_grok_prstatus
)
10193 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10195 #if defined (HAVE_PRSTATUS_T)
10196 return elfcore_grok_prstatus (abfd
, note
);
10201 #if defined (HAVE_PSTATUS_T)
10203 return elfcore_grok_pstatus (abfd
, note
);
10206 #if defined (HAVE_LWPSTATUS_T)
10208 return elfcore_grok_lwpstatus (abfd
, note
);
10211 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10212 return elfcore_grok_prfpreg (abfd
, note
);
10214 case NT_WIN32PSTATUS
:
10215 return elfcore_grok_win32pstatus (abfd
, note
);
10217 case NT_PRXFPREG
: /* Linux SSE extension */
10218 if (note
->namesz
== 6
10219 && strcmp (note
->namedata
, "LINUX") == 0)
10220 return elfcore_grok_prxfpreg (abfd
, note
);
10224 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10225 if (note
->namesz
== 6
10226 && strcmp (note
->namedata
, "LINUX") == 0)
10227 return elfcore_grok_xstatereg (abfd
, note
);
10232 if (note
->namesz
== 6
10233 && strcmp (note
->namedata
, "LINUX") == 0)
10234 return elfcore_grok_ppc_vmx (abfd
, note
);
10239 if (note
->namesz
== 6
10240 && strcmp (note
->namedata
, "LINUX") == 0)
10241 return elfcore_grok_ppc_vsx (abfd
, note
);
10246 if (note
->namesz
== 6
10247 && strcmp (note
->namedata
, "LINUX") == 0)
10248 return elfcore_grok_ppc_tar (abfd
, note
);
10253 if (note
->namesz
== 6
10254 && strcmp (note
->namedata
, "LINUX") == 0)
10255 return elfcore_grok_ppc_ppr (abfd
, note
);
10260 if (note
->namesz
== 6
10261 && strcmp (note
->namedata
, "LINUX") == 0)
10262 return elfcore_grok_ppc_dscr (abfd
, note
);
10267 if (note
->namesz
== 6
10268 && strcmp (note
->namedata
, "LINUX") == 0)
10269 return elfcore_grok_ppc_ebb (abfd
, note
);
10274 if (note
->namesz
== 6
10275 && strcmp (note
->namedata
, "LINUX") == 0)
10276 return elfcore_grok_ppc_pmu (abfd
, note
);
10280 case NT_PPC_TM_CGPR
:
10281 if (note
->namesz
== 6
10282 && strcmp (note
->namedata
, "LINUX") == 0)
10283 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10287 case NT_PPC_TM_CFPR
:
10288 if (note
->namesz
== 6
10289 && strcmp (note
->namedata
, "LINUX") == 0)
10290 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10294 case NT_PPC_TM_CVMX
:
10295 if (note
->namesz
== 6
10296 && strcmp (note
->namedata
, "LINUX") == 0)
10297 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10301 case NT_PPC_TM_CVSX
:
10302 if (note
->namesz
== 6
10303 && strcmp (note
->namedata
, "LINUX") == 0)
10304 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10308 case NT_PPC_TM_SPR
:
10309 if (note
->namesz
== 6
10310 && strcmp (note
->namedata
, "LINUX") == 0)
10311 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10315 case NT_PPC_TM_CTAR
:
10316 if (note
->namesz
== 6
10317 && strcmp (note
->namedata
, "LINUX") == 0)
10318 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10322 case NT_PPC_TM_CPPR
:
10323 if (note
->namesz
== 6
10324 && strcmp (note
->namedata
, "LINUX") == 0)
10325 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10329 case NT_PPC_TM_CDSCR
:
10330 if (note
->namesz
== 6
10331 && strcmp (note
->namedata
, "LINUX") == 0)
10332 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10336 case NT_S390_HIGH_GPRS
:
10337 if (note
->namesz
== 6
10338 && strcmp (note
->namedata
, "LINUX") == 0)
10339 return elfcore_grok_s390_high_gprs (abfd
, note
);
10343 case NT_S390_TIMER
:
10344 if (note
->namesz
== 6
10345 && strcmp (note
->namedata
, "LINUX") == 0)
10346 return elfcore_grok_s390_timer (abfd
, note
);
10350 case NT_S390_TODCMP
:
10351 if (note
->namesz
== 6
10352 && strcmp (note
->namedata
, "LINUX") == 0)
10353 return elfcore_grok_s390_todcmp (abfd
, note
);
10357 case NT_S390_TODPREG
:
10358 if (note
->namesz
== 6
10359 && strcmp (note
->namedata
, "LINUX") == 0)
10360 return elfcore_grok_s390_todpreg (abfd
, note
);
10365 if (note
->namesz
== 6
10366 && strcmp (note
->namedata
, "LINUX") == 0)
10367 return elfcore_grok_s390_ctrs (abfd
, note
);
10371 case NT_S390_PREFIX
:
10372 if (note
->namesz
== 6
10373 && strcmp (note
->namedata
, "LINUX") == 0)
10374 return elfcore_grok_s390_prefix (abfd
, note
);
10378 case NT_S390_LAST_BREAK
:
10379 if (note
->namesz
== 6
10380 && strcmp (note
->namedata
, "LINUX") == 0)
10381 return elfcore_grok_s390_last_break (abfd
, note
);
10385 case NT_S390_SYSTEM_CALL
:
10386 if (note
->namesz
== 6
10387 && strcmp (note
->namedata
, "LINUX") == 0)
10388 return elfcore_grok_s390_system_call (abfd
, note
);
10393 if (note
->namesz
== 6
10394 && strcmp (note
->namedata
, "LINUX") == 0)
10395 return elfcore_grok_s390_tdb (abfd
, note
);
10399 case NT_S390_VXRS_LOW
:
10400 if (note
->namesz
== 6
10401 && strcmp (note
->namedata
, "LINUX") == 0)
10402 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10406 case NT_S390_VXRS_HIGH
:
10407 if (note
->namesz
== 6
10408 && strcmp (note
->namedata
, "LINUX") == 0)
10409 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10413 case NT_S390_GS_CB
:
10414 if (note
->namesz
== 6
10415 && strcmp (note
->namedata
, "LINUX") == 0)
10416 return elfcore_grok_s390_gs_cb (abfd
, note
);
10420 case NT_S390_GS_BC
:
10421 if (note
->namesz
== 6
10422 && strcmp (note
->namedata
, "LINUX") == 0)
10423 return elfcore_grok_s390_gs_bc (abfd
, note
);
10428 if (note
->namesz
== 6
10429 && strcmp (note
->namedata
, "LINUX") == 0)
10430 return elfcore_grok_arm_vfp (abfd
, note
);
10435 if (note
->namesz
== 6
10436 && strcmp (note
->namedata
, "LINUX") == 0)
10437 return elfcore_grok_aarch_tls (abfd
, note
);
10441 case NT_ARM_HW_BREAK
:
10442 if (note
->namesz
== 6
10443 && strcmp (note
->namedata
, "LINUX") == 0)
10444 return elfcore_grok_aarch_hw_break (abfd
, note
);
10448 case NT_ARM_HW_WATCH
:
10449 if (note
->namesz
== 6
10450 && strcmp (note
->namedata
, "LINUX") == 0)
10451 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10456 if (note
->namesz
== 6
10457 && strcmp (note
->namedata
, "LINUX") == 0)
10458 return elfcore_grok_aarch_sve (abfd
, note
);
10462 case NT_ARM_PAC_MASK
:
10463 if (note
->namesz
== 6
10464 && strcmp (note
->namedata
, "LINUX") == 0)
10465 return elfcore_grok_aarch_pauth (abfd
, note
);
10471 if (bed
->elf_backend_grok_psinfo
)
10472 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10474 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10475 return elfcore_grok_psinfo (abfd
, note
);
10481 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10484 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10488 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10495 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10497 struct bfd_build_id
* build_id
;
10499 if (note
->descsz
== 0)
10502 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10503 if (build_id
== NULL
)
10506 build_id
->size
= note
->descsz
;
10507 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10508 abfd
->build_id
= build_id
;
10514 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10516 switch (note
->type
)
10521 case NT_GNU_PROPERTY_TYPE_0
:
10522 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10524 case NT_GNU_BUILD_ID
:
10525 return elfobj_grok_gnu_build_id (abfd
, note
);
10530 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10532 struct sdt_note
*cur
=
10533 (struct sdt_note
*) bfd_alloc (abfd
,
10534 sizeof (struct sdt_note
) + note
->descsz
);
10536 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10537 cur
->size
= (bfd_size_type
) note
->descsz
;
10538 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10540 elf_tdata (abfd
)->sdt_note_head
= cur
;
10546 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10548 switch (note
->type
)
10551 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10559 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10563 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10566 if (note
->descsz
< 108)
10571 if (note
->descsz
< 120)
10579 /* Check for version 1 in pr_version. */
10580 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10585 /* Skip over pr_psinfosz. */
10586 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10590 offset
+= 4; /* Padding before pr_psinfosz. */
10594 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10595 elf_tdata (abfd
)->core
->program
10596 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10599 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10600 elf_tdata (abfd
)->core
->command
10601 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10604 /* Padding before pr_pid. */
10607 /* The pr_pid field was added in version "1a". */
10608 if (note
->descsz
< offset
+ 4)
10611 elf_tdata (abfd
)->core
->pid
10612 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10618 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10624 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10625 Also compute minimum size of this note. */
10626 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10630 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10634 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10635 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10642 if (note
->descsz
< min_size
)
10645 /* Check for version 1 in pr_version. */
10646 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10649 /* Extract size of pr_reg from pr_gregsetsz. */
10650 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10651 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10653 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10658 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10662 /* Skip over pr_osreldate. */
10665 /* Read signal from pr_cursig. */
10666 if (elf_tdata (abfd
)->core
->signal
== 0)
10667 elf_tdata (abfd
)->core
->signal
10668 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10671 /* Read TID from pr_pid. */
10672 elf_tdata (abfd
)->core
->lwpid
10673 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10676 /* Padding before pr_reg. */
10677 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10680 /* Make sure that there is enough data remaining in the note. */
10681 if ((note
->descsz
- offset
) < size
)
10684 /* Make a ".reg/999" section and a ".reg" section. */
10685 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10686 size
, note
->descpos
+ offset
);
10690 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10692 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10694 switch (note
->type
)
10697 if (bed
->elf_backend_grok_freebsd_prstatus
)
10698 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10700 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10703 return elfcore_grok_prfpreg (abfd
, note
);
10706 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10708 case NT_FREEBSD_THRMISC
:
10709 if (note
->namesz
== 8)
10710 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10714 case NT_FREEBSD_PROCSTAT_PROC
:
10715 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10718 case NT_FREEBSD_PROCSTAT_FILES
:
10719 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10722 case NT_FREEBSD_PROCSTAT_VMMAP
:
10723 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10726 case NT_FREEBSD_PROCSTAT_AUXV
:
10727 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10729 case NT_X86_XSTATE
:
10730 if (note
->namesz
== 8)
10731 return elfcore_grok_xstatereg (abfd
, note
);
10735 case NT_FREEBSD_PTLWPINFO
:
10736 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10740 return elfcore_grok_arm_vfp (abfd
, note
);
10748 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10752 cp
= strchr (note
->namedata
, '@');
10755 *lwpidp
= atoi(cp
+ 1);
10762 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10764 if (note
->descsz
<= 0x7c + 31)
10767 /* Signal number at offset 0x08. */
10768 elf_tdata (abfd
)->core
->signal
10769 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10771 /* Process ID at offset 0x50. */
10772 elf_tdata (abfd
)->core
->pid
10773 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10775 /* Command name at 0x7c (max 32 bytes, including nul). */
10776 elf_tdata (abfd
)->core
->command
10777 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10779 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10784 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10788 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10789 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10791 switch (note
->type
)
10793 case NT_NETBSDCORE_PROCINFO
:
10794 /* NetBSD-specific core "procinfo". Note that we expect to
10795 find this note before any of the others, which is fine,
10796 since the kernel writes this note out first when it
10797 creates a core file. */
10798 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10799 #ifdef NT_NETBSDCORE_AUXV
10800 case NT_NETBSDCORE_AUXV
:
10801 /* NetBSD-specific Elf Auxiliary Vector data. */
10802 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10804 #ifdef NT_NETBSDCORE_LWPSTATUS
10805 case NT_NETBSDCORE_LWPSTATUS
:
10806 return elfcore_make_note_pseudosection (abfd
,
10807 ".note.netbsdcore.lwpstatus",
10814 /* As of March 2020 there are no other machine-independent notes
10815 defined for NetBSD core files. If the note type is less
10816 than the start of the machine-dependent note types, we don't
10819 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10823 switch (bfd_get_arch (abfd
))
10825 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10826 PT_GETFPREGS == mach+2. */
10828 case bfd_arch_aarch64
:
10829 case bfd_arch_alpha
:
10830 case bfd_arch_sparc
:
10831 switch (note
->type
)
10833 case NT_NETBSDCORE_FIRSTMACH
+0:
10834 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10836 case NT_NETBSDCORE_FIRSTMACH
+2:
10837 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10843 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10844 There's also old PT___GETREGS40 == mach + 1 for old reg
10845 structure which lacks GBR. */
10848 switch (note
->type
)
10850 case NT_NETBSDCORE_FIRSTMACH
+3:
10851 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10853 case NT_NETBSDCORE_FIRSTMACH
+5:
10854 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10860 /* On all other arch's, PT_GETREGS == mach+1 and
10861 PT_GETFPREGS == mach+3. */
10864 switch (note
->type
)
10866 case NT_NETBSDCORE_FIRSTMACH
+1:
10867 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10869 case NT_NETBSDCORE_FIRSTMACH
+3:
10870 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10880 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10882 if (note
->descsz
<= 0x48 + 31)
10885 /* Signal number at offset 0x08. */
10886 elf_tdata (abfd
)->core
->signal
10887 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10889 /* Process ID at offset 0x20. */
10890 elf_tdata (abfd
)->core
->pid
10891 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10893 /* Command name at 0x48 (max 32 bytes, including nul). */
10894 elf_tdata (abfd
)->core
->command
10895 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10901 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10903 if (note
->type
== NT_OPENBSD_PROCINFO
)
10904 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10906 if (note
->type
== NT_OPENBSD_REGS
)
10907 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10909 if (note
->type
== NT_OPENBSD_FPREGS
)
10910 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10912 if (note
->type
== NT_OPENBSD_XFPREGS
)
10913 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10915 if (note
->type
== NT_OPENBSD_AUXV
)
10916 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10918 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10920 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10925 sect
->size
= note
->descsz
;
10926 sect
->filepos
= note
->descpos
;
10927 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10936 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10938 void *ddata
= note
->descdata
;
10945 if (note
->descsz
< 16)
10948 /* nto_procfs_status 'pid' field is at offset 0. */
10949 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10951 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10952 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10954 /* nto_procfs_status 'flags' field is at offset 8. */
10955 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10957 /* nto_procfs_status 'what' field is at offset 14. */
10958 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10960 elf_tdata (abfd
)->core
->signal
= sig
;
10961 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10964 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10965 do not come from signals so we make sure we set the current
10966 thread just in case. */
10967 if (flags
& 0x00000080)
10968 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10970 /* Make a ".qnx_core_status/%d" section. */
10971 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10973 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10976 strcpy (name
, buf
);
10978 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10982 sect
->size
= note
->descsz
;
10983 sect
->filepos
= note
->descpos
;
10984 sect
->alignment_power
= 2;
10986 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10990 elfcore_grok_nto_regs (bfd
*abfd
,
10991 Elf_Internal_Note
*note
,
10999 /* Make a "(base)/%d" section. */
11000 sprintf (buf
, "%s/%ld", base
, tid
);
11002 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11005 strcpy (name
, buf
);
11007 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11011 sect
->size
= note
->descsz
;
11012 sect
->filepos
= note
->descpos
;
11013 sect
->alignment_power
= 2;
11015 /* This is the current thread. */
11016 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
11017 return elfcore_maybe_make_sect (abfd
, base
, sect
);
11022 #define BFD_QNT_CORE_INFO 7
11023 #define BFD_QNT_CORE_STATUS 8
11024 #define BFD_QNT_CORE_GREG 9
11025 #define BFD_QNT_CORE_FPREG 10
11028 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11030 /* Every GREG section has a STATUS section before it. Store the
11031 tid from the previous call to pass down to the next gregs
11033 static long tid
= 1;
11035 switch (note
->type
)
11037 case BFD_QNT_CORE_INFO
:
11038 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
11039 case BFD_QNT_CORE_STATUS
:
11040 return elfcore_grok_nto_status (abfd
, note
, &tid
);
11041 case BFD_QNT_CORE_GREG
:
11042 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
11043 case BFD_QNT_CORE_FPREG
:
11044 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
11051 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11057 /* Use note name as section name. */
11058 len
= note
->namesz
;
11059 name
= (char *) bfd_alloc (abfd
, len
);
11062 memcpy (name
, note
->namedata
, len
);
11063 name
[len
- 1] = '\0';
11065 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11069 sect
->size
= note
->descsz
;
11070 sect
->filepos
= note
->descpos
;
11071 sect
->alignment_power
= 1;
11076 /* Function: elfcore_write_note
11079 buffer to hold note, and current size of buffer
11083 size of data for note
11085 Writes note to end of buffer. ELF64 notes are written exactly as
11086 for ELF32, despite the current (as of 2006) ELF gabi specifying
11087 that they ought to have 8-byte namesz and descsz field, and have
11088 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11091 Pointer to realloc'd buffer, *BUFSIZ updated. */
11094 elfcore_write_note (bfd
*abfd
,
11102 Elf_External_Note
*xnp
;
11109 namesz
= strlen (name
) + 1;
11111 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11113 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11116 dest
= buf
+ *bufsiz
;
11117 *bufsiz
+= newspace
;
11118 xnp
= (Elf_External_Note
*) dest
;
11119 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11120 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11121 H_PUT_32 (abfd
, type
, xnp
->type
);
11125 memcpy (dest
, name
, namesz
);
11133 memcpy (dest
, input
, size
);
11143 /* gcc-8 warns (*) on all the strncpy calls in this function about
11144 possible string truncation. The "truncation" is not a bug. We
11145 have an external representation of structs with fields that are not
11146 necessarily NULL terminated and corresponding internal
11147 representation fields that are one larger so that they can always
11148 be NULL terminated.
11149 gcc versions between 4.2 and 4.6 do not allow pragma control of
11150 diagnostics inside functions, giving a hard error if you try to use
11151 the finer control available with later versions.
11152 gcc prior to 4.2 warns about diagnostic push and pop.
11153 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11154 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11155 (*) Depending on your system header files! */
11156 #if GCC_VERSION >= 8000
11157 # pragma GCC diagnostic push
11158 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11161 elfcore_write_prpsinfo (bfd
*abfd
,
11165 const char *psargs
)
11167 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11169 if (bed
->elf_backend_write_core_note
!= NULL
)
11172 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11173 NT_PRPSINFO
, fname
, psargs
);
11178 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11179 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11180 if (bed
->s
->elfclass
== ELFCLASS32
)
11182 # if defined (HAVE_PSINFO32_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
));
11199 # if defined (HAVE_PSINFO_T)
11201 int note_type
= NT_PSINFO
;
11204 int note_type
= NT_PRPSINFO
;
11207 memset (&data
, 0, sizeof (data
));
11208 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11209 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11210 return elfcore_write_note (abfd
, buf
, bufsiz
,
11211 "CORE", note_type
, &data
, sizeof (data
));
11213 #endif /* PSINFO_T or PRPSINFO_T */
11218 #if GCC_VERSION >= 8000
11219 # pragma GCC diagnostic pop
11223 elfcore_write_linux_prpsinfo32
11224 (bfd
*abfd
, char *buf
, int *bufsiz
,
11225 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11227 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11229 struct elf_external_linux_prpsinfo32_ugid16 data
;
11231 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11232 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11233 &data
, sizeof (data
));
11237 struct elf_external_linux_prpsinfo32_ugid32 data
;
11239 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11240 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11241 &data
, sizeof (data
));
11246 elfcore_write_linux_prpsinfo64
11247 (bfd
*abfd
, char *buf
, int *bufsiz
,
11248 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11250 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11252 struct elf_external_linux_prpsinfo64_ugid16 data
;
11254 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11255 return elfcore_write_note (abfd
, buf
, bufsiz
,
11256 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11260 struct elf_external_linux_prpsinfo64_ugid32 data
;
11262 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11263 return elfcore_write_note (abfd
, buf
, bufsiz
,
11264 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11269 elfcore_write_prstatus (bfd
*abfd
,
11276 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11278 if (bed
->elf_backend_write_core_note
!= NULL
)
11281 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11283 pid
, cursig
, gregs
);
11288 #if defined (HAVE_PRSTATUS_T)
11289 #if defined (HAVE_PRSTATUS32_T)
11290 if (bed
->s
->elfclass
== ELFCLASS32
)
11292 prstatus32_t prstat
;
11294 memset (&prstat
, 0, sizeof (prstat
));
11295 prstat
.pr_pid
= pid
;
11296 prstat
.pr_cursig
= cursig
;
11297 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11298 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11299 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11306 memset (&prstat
, 0, sizeof (prstat
));
11307 prstat
.pr_pid
= pid
;
11308 prstat
.pr_cursig
= cursig
;
11309 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11310 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11311 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11313 #endif /* HAVE_PRSTATUS_T */
11319 #if defined (HAVE_LWPSTATUS_T)
11321 elfcore_write_lwpstatus (bfd
*abfd
,
11328 lwpstatus_t lwpstat
;
11329 const char *note_name
= "CORE";
11331 memset (&lwpstat
, 0, sizeof (lwpstat
));
11332 lwpstat
.pr_lwpid
= pid
>> 16;
11333 lwpstat
.pr_cursig
= cursig
;
11334 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11335 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11336 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11337 #if !defined(gregs)
11338 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11339 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11341 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11342 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11345 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11346 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11348 #endif /* HAVE_LWPSTATUS_T */
11350 #if defined (HAVE_PSTATUS_T)
11352 elfcore_write_pstatus (bfd
*abfd
,
11356 int cursig ATTRIBUTE_UNUSED
,
11357 const void *gregs ATTRIBUTE_UNUSED
)
11359 const char *note_name
= "CORE";
11360 #if defined (HAVE_PSTATUS32_T)
11361 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11363 if (bed
->s
->elfclass
== ELFCLASS32
)
11367 memset (&pstat
, 0, sizeof (pstat
));
11368 pstat
.pr_pid
= pid
& 0xffff;
11369 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11370 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11378 memset (&pstat
, 0, sizeof (pstat
));
11379 pstat
.pr_pid
= pid
& 0xffff;
11380 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11381 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11385 #endif /* HAVE_PSTATUS_T */
11388 elfcore_write_prfpreg (bfd
*abfd
,
11391 const void *fpregs
,
11394 const char *note_name
= "CORE";
11395 return elfcore_write_note (abfd
, buf
, bufsiz
,
11396 note_name
, NT_FPREGSET
, fpregs
, size
);
11400 elfcore_write_prxfpreg (bfd
*abfd
,
11403 const void *xfpregs
,
11406 char *note_name
= "LINUX";
11407 return elfcore_write_note (abfd
, buf
, bufsiz
,
11408 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11412 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11413 const void *xfpregs
, int size
)
11416 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11417 note_name
= "FreeBSD";
11419 note_name
= "LINUX";
11420 return elfcore_write_note (abfd
, buf
, bufsiz
,
11421 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11425 elfcore_write_ppc_vmx (bfd
*abfd
,
11428 const void *ppc_vmx
,
11431 char *note_name
= "LINUX";
11432 return elfcore_write_note (abfd
, buf
, bufsiz
,
11433 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11437 elfcore_write_ppc_vsx (bfd
*abfd
,
11440 const void *ppc_vsx
,
11443 char *note_name
= "LINUX";
11444 return elfcore_write_note (abfd
, buf
, bufsiz
,
11445 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11449 elfcore_write_ppc_tar (bfd
*abfd
,
11452 const void *ppc_tar
,
11455 char *note_name
= "LINUX";
11456 return elfcore_write_note (abfd
, buf
, bufsiz
,
11457 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11461 elfcore_write_ppc_ppr (bfd
*abfd
,
11464 const void *ppc_ppr
,
11467 char *note_name
= "LINUX";
11468 return elfcore_write_note (abfd
, buf
, bufsiz
,
11469 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11473 elfcore_write_ppc_dscr (bfd
*abfd
,
11476 const void *ppc_dscr
,
11479 char *note_name
= "LINUX";
11480 return elfcore_write_note (abfd
, buf
, bufsiz
,
11481 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11485 elfcore_write_ppc_ebb (bfd
*abfd
,
11488 const void *ppc_ebb
,
11491 char *note_name
= "LINUX";
11492 return elfcore_write_note (abfd
, buf
, bufsiz
,
11493 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11497 elfcore_write_ppc_pmu (bfd
*abfd
,
11500 const void *ppc_pmu
,
11503 char *note_name
= "LINUX";
11504 return elfcore_write_note (abfd
, buf
, bufsiz
,
11505 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11509 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11512 const void *ppc_tm_cgpr
,
11515 char *note_name
= "LINUX";
11516 return elfcore_write_note (abfd
, buf
, bufsiz
,
11517 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11521 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11524 const void *ppc_tm_cfpr
,
11527 char *note_name
= "LINUX";
11528 return elfcore_write_note (abfd
, buf
, bufsiz
,
11529 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11533 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11536 const void *ppc_tm_cvmx
,
11539 char *note_name
= "LINUX";
11540 return elfcore_write_note (abfd
, buf
, bufsiz
,
11541 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11545 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11548 const void *ppc_tm_cvsx
,
11551 char *note_name
= "LINUX";
11552 return elfcore_write_note (abfd
, buf
, bufsiz
,
11553 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11557 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11560 const void *ppc_tm_spr
,
11563 char *note_name
= "LINUX";
11564 return elfcore_write_note (abfd
, buf
, bufsiz
,
11565 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11569 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11572 const void *ppc_tm_ctar
,
11575 char *note_name
= "LINUX";
11576 return elfcore_write_note (abfd
, buf
, bufsiz
,
11577 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11581 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11584 const void *ppc_tm_cppr
,
11587 char *note_name
= "LINUX";
11588 return elfcore_write_note (abfd
, buf
, bufsiz
,
11589 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11593 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11596 const void *ppc_tm_cdscr
,
11599 char *note_name
= "LINUX";
11600 return elfcore_write_note (abfd
, buf
, bufsiz
,
11601 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11605 elfcore_write_s390_high_gprs (bfd
*abfd
,
11608 const void *s390_high_gprs
,
11611 char *note_name
= "LINUX";
11612 return elfcore_write_note (abfd
, buf
, bufsiz
,
11613 note_name
, NT_S390_HIGH_GPRS
,
11614 s390_high_gprs
, size
);
11618 elfcore_write_s390_timer (bfd
*abfd
,
11621 const void *s390_timer
,
11624 char *note_name
= "LINUX";
11625 return elfcore_write_note (abfd
, buf
, bufsiz
,
11626 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11630 elfcore_write_s390_todcmp (bfd
*abfd
,
11633 const void *s390_todcmp
,
11636 char *note_name
= "LINUX";
11637 return elfcore_write_note (abfd
, buf
, bufsiz
,
11638 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11642 elfcore_write_s390_todpreg (bfd
*abfd
,
11645 const void *s390_todpreg
,
11648 char *note_name
= "LINUX";
11649 return elfcore_write_note (abfd
, buf
, bufsiz
,
11650 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11654 elfcore_write_s390_ctrs (bfd
*abfd
,
11657 const void *s390_ctrs
,
11660 char *note_name
= "LINUX";
11661 return elfcore_write_note (abfd
, buf
, bufsiz
,
11662 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11666 elfcore_write_s390_prefix (bfd
*abfd
,
11669 const void *s390_prefix
,
11672 char *note_name
= "LINUX";
11673 return elfcore_write_note (abfd
, buf
, bufsiz
,
11674 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11678 elfcore_write_s390_last_break (bfd
*abfd
,
11681 const void *s390_last_break
,
11684 char *note_name
= "LINUX";
11685 return elfcore_write_note (abfd
, buf
, bufsiz
,
11686 note_name
, NT_S390_LAST_BREAK
,
11687 s390_last_break
, size
);
11691 elfcore_write_s390_system_call (bfd
*abfd
,
11694 const void *s390_system_call
,
11697 char *note_name
= "LINUX";
11698 return elfcore_write_note (abfd
, buf
, bufsiz
,
11699 note_name
, NT_S390_SYSTEM_CALL
,
11700 s390_system_call
, size
);
11704 elfcore_write_s390_tdb (bfd
*abfd
,
11707 const void *s390_tdb
,
11710 char *note_name
= "LINUX";
11711 return elfcore_write_note (abfd
, buf
, bufsiz
,
11712 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11716 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11719 const void *s390_vxrs_low
,
11722 char *note_name
= "LINUX";
11723 return elfcore_write_note (abfd
, buf
, bufsiz
,
11724 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11728 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11731 const void *s390_vxrs_high
,
11734 char *note_name
= "LINUX";
11735 return elfcore_write_note (abfd
, buf
, bufsiz
,
11736 note_name
, NT_S390_VXRS_HIGH
,
11737 s390_vxrs_high
, size
);
11741 elfcore_write_s390_gs_cb (bfd
*abfd
,
11744 const void *s390_gs_cb
,
11747 char *note_name
= "LINUX";
11748 return elfcore_write_note (abfd
, buf
, bufsiz
,
11749 note_name
, NT_S390_GS_CB
,
11754 elfcore_write_s390_gs_bc (bfd
*abfd
,
11757 const void *s390_gs_bc
,
11760 char *note_name
= "LINUX";
11761 return elfcore_write_note (abfd
, buf
, bufsiz
,
11762 note_name
, NT_S390_GS_BC
,
11767 elfcore_write_arm_vfp (bfd
*abfd
,
11770 const void *arm_vfp
,
11773 char *note_name
= "LINUX";
11774 return elfcore_write_note (abfd
, buf
, bufsiz
,
11775 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11779 elfcore_write_aarch_tls (bfd
*abfd
,
11782 const void *aarch_tls
,
11785 char *note_name
= "LINUX";
11786 return elfcore_write_note (abfd
, buf
, bufsiz
,
11787 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11791 elfcore_write_aarch_hw_break (bfd
*abfd
,
11794 const void *aarch_hw_break
,
11797 char *note_name
= "LINUX";
11798 return elfcore_write_note (abfd
, buf
, bufsiz
,
11799 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11803 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11806 const void *aarch_hw_watch
,
11809 char *note_name
= "LINUX";
11810 return elfcore_write_note (abfd
, buf
, bufsiz
,
11811 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11815 elfcore_write_aarch_sve (bfd
*abfd
,
11818 const void *aarch_sve
,
11821 char *note_name
= "LINUX";
11822 return elfcore_write_note (abfd
, buf
, bufsiz
,
11823 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11827 elfcore_write_aarch_pauth (bfd
*abfd
,
11830 const void *aarch_pauth
,
11833 char *note_name
= "LINUX";
11834 return elfcore_write_note (abfd
, buf
, bufsiz
,
11835 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11839 elfcore_write_register_note (bfd
*abfd
,
11842 const char *section
,
11846 if (strcmp (section
, ".reg2") == 0)
11847 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11848 if (strcmp (section
, ".reg-xfp") == 0)
11849 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11850 if (strcmp (section
, ".reg-xstate") == 0)
11851 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11852 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11853 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11854 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11855 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11856 if (strcmp (section
, ".reg-ppc-tar") == 0)
11857 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11858 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11859 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11860 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11861 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11862 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11863 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11864 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11865 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11866 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11867 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11868 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11869 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11870 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11871 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11872 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11873 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11874 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11875 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11876 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11877 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11878 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11879 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11880 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11881 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11882 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11883 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11884 if (strcmp (section
, ".reg-s390-timer") == 0)
11885 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11886 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11887 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11888 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11889 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11890 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11891 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11892 if (strcmp (section
, ".reg-s390-prefix") == 0)
11893 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11894 if (strcmp (section
, ".reg-s390-last-break") == 0)
11895 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11896 if (strcmp (section
, ".reg-s390-system-call") == 0)
11897 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11898 if (strcmp (section
, ".reg-s390-tdb") == 0)
11899 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11900 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11901 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11902 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11903 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11904 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11905 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
11906 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
11907 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
11908 if (strcmp (section
, ".reg-arm-vfp") == 0)
11909 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11910 if (strcmp (section
, ".reg-aarch-tls") == 0)
11911 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11912 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11913 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11914 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11915 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11916 if (strcmp (section
, ".reg-aarch-sve") == 0)
11917 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
11918 if (strcmp (section
, ".reg-aarch-pauth") == 0)
11919 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
11924 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11929 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11930 gABI specifies that PT_NOTE alignment should be aligned to 4
11931 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11932 align is less than 4, we use 4 byte alignment. */
11935 if (align
!= 4 && align
!= 8)
11939 while (p
< buf
+ size
)
11941 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11942 Elf_Internal_Note in
;
11944 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11947 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11949 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11950 in
.namedata
= xnp
->name
;
11951 if (in
.namesz
> buf
- in
.namedata
+ size
)
11954 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11955 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11956 in
.descpos
= offset
+ (in
.descdata
- buf
);
11958 && (in
.descdata
>= buf
+ size
11959 || in
.descsz
> buf
- in
.descdata
+ size
))
11962 switch (bfd_get_format (abfd
))
11969 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11972 const char * string
;
11974 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11978 GROKER_ELEMENT ("", elfcore_grok_note
),
11979 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11980 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11981 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11982 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11983 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
11984 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
)
11986 #undef GROKER_ELEMENT
11989 for (i
= ARRAY_SIZE (grokers
); i
--;)
11991 if (in
.namesz
>= grokers
[i
].len
11992 && strncmp (in
.namedata
, grokers
[i
].string
,
11993 grokers
[i
].len
) == 0)
11995 if (! grokers
[i
].func (abfd
, & in
))
12004 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
12006 if (! elfobj_grok_gnu_note (abfd
, &in
))
12009 else if (in
.namesz
== sizeof "stapsdt"
12010 && strcmp (in
.namedata
, "stapsdt") == 0)
12012 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
12018 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
12025 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
12030 if (size
== 0 || (size
+ 1) == 0)
12033 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
12036 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
12040 /* PR 17512: file: ec08f814
12041 0-termintate the buffer so that string searches will not overflow. */
12044 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
12054 /* Providing external access to the ELF program header table. */
12056 /* Return an upper bound on the number of bytes required to store a
12057 copy of ABFD's program header table entries. Return -1 if an error
12058 occurs; bfd_get_error will return an appropriate code. */
12061 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12063 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12065 bfd_set_error (bfd_error_wrong_format
);
12069 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12072 /* Copy ABFD's program header table entries to *PHDRS. The entries
12073 will be stored as an array of Elf_Internal_Phdr structures, as
12074 defined in include/elf/internal.h. To find out how large the
12075 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12077 Return the number of program header table entries read, or -1 if an
12078 error occurs; bfd_get_error will return an appropriate code. */
12081 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12085 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12087 bfd_set_error (bfd_error_wrong_format
);
12091 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12092 if (num_phdrs
!= 0)
12093 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12094 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12099 enum elf_reloc_type_class
12100 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12101 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12102 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12104 return reloc_class_normal
;
12107 /* For RELA architectures, return the relocation value for a
12108 relocation against a local symbol. */
12111 _bfd_elf_rela_local_sym (bfd
*abfd
,
12112 Elf_Internal_Sym
*sym
,
12114 Elf_Internal_Rela
*rel
)
12116 asection
*sec
= *psec
;
12117 bfd_vma relocation
;
12119 relocation
= (sec
->output_section
->vma
12120 + sec
->output_offset
12122 if ((sec
->flags
& SEC_MERGE
)
12123 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12124 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12127 _bfd_merged_section_offset (abfd
, psec
,
12128 elf_section_data (sec
)->sec_info
,
12129 sym
->st_value
+ rel
->r_addend
);
12132 /* If we have changed the section, and our original section is
12133 marked with SEC_EXCLUDE, it means that the original
12134 SEC_MERGE section has been completely subsumed in some
12135 other SEC_MERGE section. In this case, we need to leave
12136 some info around for --emit-relocs. */
12137 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12138 sec
->kept_section
= *psec
;
12141 rel
->r_addend
-= relocation
;
12142 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12148 _bfd_elf_rel_local_sym (bfd
*abfd
,
12149 Elf_Internal_Sym
*sym
,
12153 asection
*sec
= *psec
;
12155 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12156 return sym
->st_value
+ addend
;
12158 return _bfd_merged_section_offset (abfd
, psec
,
12159 elf_section_data (sec
)->sec_info
,
12160 sym
->st_value
+ addend
);
12163 /* Adjust an address within a section. Given OFFSET within SEC, return
12164 the new offset within the section, based upon changes made to the
12165 section. Returns -1 if the offset is now invalid.
12166 The offset (in abnd out) is in target sized bytes, however big a
12170 _bfd_elf_section_offset (bfd
*abfd
,
12171 struct bfd_link_info
*info
,
12175 switch (sec
->sec_info_type
)
12177 case SEC_INFO_TYPE_STABS
:
12178 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12180 case SEC_INFO_TYPE_EH_FRAME
:
12181 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12184 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12186 /* Reverse the offset. */
12187 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12188 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12190 /* address_size and sec->size are in octets. Convert
12191 to bytes before subtracting the original offset. */
12192 offset
= ((sec
->size
- address_size
)
12193 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12199 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12200 reconstruct an ELF file by reading the segments out of remote memory
12201 based on the ELF file header at EHDR_VMA and the ELF program headers it
12202 points to. If not null, *LOADBASEP is filled in with the difference
12203 between the VMAs from which the segments were read, and the VMAs the
12204 file headers (and hence BFD's idea of each section's VMA) put them at.
12206 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12207 remote memory at target address VMA into the local buffer at MYADDR; it
12208 should return zero on success or an `errno' code on failure. TEMPL must
12209 be a BFD for an ELF target with the word size and byte order found in
12210 the remote memory. */
12213 bfd_elf_bfd_from_remote_memory
12216 bfd_size_type size
,
12217 bfd_vma
*loadbasep
,
12218 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12220 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12221 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12225 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12226 long symcount ATTRIBUTE_UNUSED
,
12227 asymbol
**syms ATTRIBUTE_UNUSED
,
12232 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12235 const char *relplt_name
;
12236 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12240 Elf_Internal_Shdr
*hdr
;
12246 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12249 if (dynsymcount
<= 0)
12252 if (!bed
->plt_sym_val
)
12255 relplt_name
= bed
->relplt_name
;
12256 if (relplt_name
== NULL
)
12257 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12258 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12259 if (relplt
== NULL
)
12262 hdr
= &elf_section_data (relplt
)->this_hdr
;
12263 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12264 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12267 plt
= bfd_get_section_by_name (abfd
, ".plt");
12271 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12272 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12275 count
= relplt
->size
/ hdr
->sh_entsize
;
12276 size
= count
* sizeof (asymbol
);
12277 p
= relplt
->relocation
;
12278 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12280 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12281 if (p
->addend
!= 0)
12284 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12286 size
+= sizeof ("+0x") - 1 + 8;
12291 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12295 names
= (char *) (s
+ count
);
12296 p
= relplt
->relocation
;
12298 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12303 addr
= bed
->plt_sym_val (i
, plt
, p
);
12304 if (addr
== (bfd_vma
) -1)
12307 *s
= **p
->sym_ptr_ptr
;
12308 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12309 we are defining a symbol, ensure one of them is set. */
12310 if ((s
->flags
& BSF_LOCAL
) == 0)
12311 s
->flags
|= BSF_GLOBAL
;
12312 s
->flags
|= BSF_SYNTHETIC
;
12314 s
->value
= addr
- plt
->vma
;
12317 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12318 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12320 if (p
->addend
!= 0)
12324 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12325 names
+= sizeof ("+0x") - 1;
12326 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12327 for (a
= buf
; *a
== '0'; ++a
)
12330 memcpy (names
, a
, len
);
12333 memcpy (names
, "@plt", sizeof ("@plt"));
12334 names
+= sizeof ("@plt");
12341 /* It is only used by x86-64 so far.
12342 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12343 but current usage would allow all of _bfd_std_section to be zero. */
12344 static const asymbol lcomm_sym
12345 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12346 asection _bfd_elf_large_com_section
12347 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12348 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12351 _bfd_elf_final_write_processing (bfd
*abfd
)
12353 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12355 i_ehdrp
= elf_elfheader (abfd
);
12357 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12358 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12360 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12361 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12362 STB_GNU_UNIQUE binding. */
12363 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12365 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12366 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12367 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12368 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12370 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12371 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12372 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12373 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12374 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12375 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12376 bfd_set_error (bfd_error_sorry
);
12384 /* Return TRUE for ELF symbol types that represent functions.
12385 This is the default version of this function, which is sufficient for
12386 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12389 _bfd_elf_is_function_type (unsigned int type
)
12391 return (type
== STT_FUNC
12392 || type
== STT_GNU_IFUNC
);
12395 /* If the ELF symbol SYM might be a function in SEC, return the
12396 function size and set *CODE_OFF to the function's entry point,
12397 otherwise return zero. */
12400 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12403 bfd_size_type size
;
12405 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12406 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12407 || sym
->section
!= sec
)
12410 *code_off
= sym
->value
;
12412 if (!(sym
->flags
& BSF_SYNTHETIC
))
12413 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
12419 /* Set to non-zero to enable some debug messages. */
12420 #define DEBUG_SECONDARY_RELOCS 0
12422 /* An internal-to-the-bfd-library only section type
12423 used to indicate a cached secondary reloc section. */
12424 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12426 /* Create a BFD section to hold a secondary reloc section. */
12429 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
12430 Elf_Internal_Shdr
*hdr
,
12432 unsigned int shindex
)
12434 /* We only support RELA secondary relocs. */
12435 if (hdr
->sh_type
!= SHT_RELA
)
12438 #if DEBUG_SECONDARY_RELOCS
12439 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
12441 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
12442 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
12445 /* Read in any secondary relocs associated with SEC. */
12448 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
12450 asymbol
** symbols
)
12452 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12454 bfd_boolean result
= TRUE
;
12455 bfd_vma (*r_sym
) (bfd_vma
);
12457 #if BFD_DEFAULT_TARGET_SIZE > 32
12458 if (bfd_arch_bits_per_address (abfd
) != 32)
12459 r_sym
= elf64_r_sym
;
12462 r_sym
= elf32_r_sym
;
12464 /* Discover if there are any secondary reloc sections
12465 associated with SEC. */
12466 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12468 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
12470 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
12471 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12473 bfd_byte
* native_relocs
;
12474 bfd_byte
* native_reloc
;
12475 arelent
* internal_relocs
;
12476 arelent
* internal_reloc
;
12478 unsigned int entsize
;
12479 unsigned int symcount
;
12480 unsigned int reloc_count
;
12483 if (ebd
->elf_info_to_howto
== NULL
)
12486 #if DEBUG_SECONDARY_RELOCS
12487 fprintf (stderr
, "read secondary relocs for %s from %s\n",
12488 sec
->name
, relsec
->name
);
12490 entsize
= hdr
->sh_entsize
;
12492 native_relocs
= bfd_malloc (hdr
->sh_size
);
12493 if (native_relocs
== NULL
)
12499 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
12500 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
12502 free (native_relocs
);
12503 bfd_set_error (bfd_error_file_too_big
);
12508 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
12509 if (internal_relocs
== NULL
)
12511 free (native_relocs
);
12516 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
12517 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
12520 free (native_relocs
);
12521 /* The internal_relocs will be freed when
12522 the memory for the bfd is released. */
12527 symcount
= bfd_get_symcount (abfd
);
12529 for (i
= 0, internal_reloc
= internal_relocs
,
12530 native_reloc
= native_relocs
;
12532 i
++, internal_reloc
++, native_reloc
+= entsize
)
12535 Elf_Internal_Rela rela
;
12537 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
12539 /* The address of an ELF reloc is section relative for an object
12540 file, and absolute for an executable file or shared library.
12541 The address of a normal BFD reloc is always section relative,
12542 and the address of a dynamic reloc is absolute.. */
12543 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
12544 internal_reloc
->address
= rela
.r_offset
;
12546 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
12548 if (r_sym (rela
.r_info
) == STN_UNDEF
)
12550 /* FIXME: This and the error case below mean that we
12551 have a symbol on relocs that is not elf_symbol_type. */
12552 internal_reloc
->sym_ptr_ptr
=
12553 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12555 else if (r_sym (rela
.r_info
) > symcount
)
12558 /* xgettext:c-format */
12559 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12560 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
12561 bfd_set_error (bfd_error_bad_value
);
12562 internal_reloc
->sym_ptr_ptr
=
12563 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12570 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
12572 internal_reloc
->sym_ptr_ptr
= ps
;
12573 /* Make sure that this symbol is not removed by strip. */
12574 (*ps
)->flags
|= BSF_KEEP
;
12577 internal_reloc
->addend
= rela
.r_addend
;
12579 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
12580 if (! res
|| internal_reloc
->howto
== NULL
)
12582 #if DEBUG_SECONDARY_RELOCS
12583 fprintf (stderr
, "there is no howto associated with reloc %lx\n",
12590 free (native_relocs
);
12591 /* Store the internal relocs. */
12592 elf_section_data (relsec
)->sec_info
= internal_relocs
;
12599 /* Set the ELF section header fields of an output secondary reloc section. */
12602 _bfd_elf_copy_special_section_fields (const bfd
* ibfd ATTRIBUTE_UNUSED
,
12603 bfd
* obfd ATTRIBUTE_UNUSED
,
12604 const Elf_Internal_Shdr
* isection
,
12605 Elf_Internal_Shdr
* osection
)
12610 if (isection
== NULL
)
12613 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
12616 isec
= isection
->bfd_section
;
12620 osec
= osection
->bfd_section
;
12624 BFD_ASSERT (elf_section_data (osec
)->sec_info
== NULL
);
12625 elf_section_data (osec
)->sec_info
= elf_section_data (isec
)->sec_info
;
12626 osection
->sh_type
= SHT_RELA
;
12627 osection
->sh_link
= elf_onesymtab (obfd
);
12628 if (osection
->sh_link
== 0)
12630 /* There is no symbol table - we are hosed... */
12632 /* xgettext:c-format */
12633 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12635 bfd_set_error (bfd_error_bad_value
);
12639 /* Find the output section that corresponds to the isection's sh_info link. */
12640 if (isection
->sh_info
== 0
12641 || isection
->sh_info
>= elf_numsections (ibfd
))
12644 /* xgettext:c-format */
12645 (_("%pB(%pA): info section index is invalid"),
12647 bfd_set_error (bfd_error_bad_value
);
12651 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
12653 if (isection
== NULL
12654 || isection
->bfd_section
== NULL
12655 || isection
->bfd_section
->output_section
== NULL
)
12658 /* xgettext:c-format */
12659 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12661 bfd_set_error (bfd_error_bad_value
);
12665 osection
->sh_info
=
12666 elf_section_data (isection
->bfd_section
->output_section
)->this_idx
;
12668 #if DEBUG_SECONDARY_RELOCS
12669 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
12670 osec
->name
, osection
->sh_link
, osection
->sh_info
);
12676 /* Write out a secondary reloc section. */
12679 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
12681 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12682 bfd_vma addr_offset
;
12684 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
12685 bfd_boolean result
= TRUE
;
12690 #if BFD_DEFAULT_TARGET_SIZE > 32
12691 if (bfd_arch_bits_per_address (abfd
) != 32)
12692 r_info
= elf64_r_info
;
12695 r_info
= elf32_r_info
;
12697 /* The address of an ELF reloc is section relative for an object
12698 file, and absolute for an executable file or shared library.
12699 The address of a BFD reloc is always section relative. */
12701 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
12702 addr_offset
= sec
->vma
;
12704 /* Discover if there are any secondary reloc sections
12705 associated with SEC. */
12706 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12708 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
12709 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
12711 if (hdr
->sh_type
== SHT_RELA
12712 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12714 asymbol
* last_sym
;
12716 unsigned int reloc_count
;
12718 arelent
* src_irel
;
12719 bfd_byte
* dst_rela
;
12721 if (hdr
->contents
!= NULL
)
12724 /* xgettext:c-format */
12725 (_("%pB(%pA): error: secondary reloc section processed twice"),
12727 bfd_set_error (bfd_error_bad_value
);
12732 reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
12733 if (reloc_count
<= 0)
12736 /* xgettext:c-format */
12737 (_("%pB(%pA): error: secondary reloc section is empty!"),
12739 bfd_set_error (bfd_error_bad_value
);
12744 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
12745 if (hdr
->contents
== NULL
)
12748 #if DEBUG_SECONDARY_RELOCS
12749 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
12750 reloc_count
, sec
->name
, relsec
->name
);
12754 dst_rela
= hdr
->contents
;
12755 src_irel
= (arelent
*) esd
->sec_info
;
12756 if (src_irel
== NULL
)
12759 /* xgettext:c-format */
12760 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12762 bfd_set_error (bfd_error_bad_value
);
12767 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= hdr
->sh_entsize
)
12769 Elf_Internal_Rela src_rela
;
12774 ptr
= src_irel
+ idx
;
12778 /* xgettext:c-format */
12779 (_("%pB(%pA): error: reloc table entry %u is empty"),
12780 abfd
, relsec
, idx
);
12781 bfd_set_error (bfd_error_bad_value
);
12786 if (ptr
->sym_ptr_ptr
== NULL
)
12788 /* FIXME: Is this an error ? */
12793 sym
= *ptr
->sym_ptr_ptr
;
12795 if (sym
== last_sym
)
12799 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
12803 /* xgettext:c-format */
12804 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12805 abfd
, relsec
, idx
);
12806 bfd_set_error (bfd_error_bad_value
);
12815 if (sym
->the_bfd
!= NULL
12816 && sym
->the_bfd
->xvec
!= abfd
->xvec
12817 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
12820 /* xgettext:c-format */
12821 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12822 abfd
, relsec
, idx
);
12823 bfd_set_error (bfd_error_bad_value
);
12829 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
12830 if (ptr
->howto
== NULL
)
12833 /* xgettext:c-format */
12834 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12835 abfd
, relsec
, idx
);
12836 bfd_set_error (bfd_error_bad_value
);
12838 src_rela
.r_info
= r_info (0, 0);
12841 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
12842 src_rela
.r_addend
= ptr
->addend
;
12843 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);