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
);
523 free (alloc_extshndx
);
528 /* Look up a symbol name. */
530 bfd_elf_sym_name (bfd
*abfd
,
531 Elf_Internal_Shdr
*symtab_hdr
,
532 Elf_Internal_Sym
*isym
,
536 unsigned int iname
= isym
->st_name
;
537 unsigned int shindex
= symtab_hdr
->sh_link
;
539 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
540 /* Check for a bogus st_shndx to avoid crashing. */
541 && isym
->st_shndx
< elf_numsections (abfd
))
543 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
544 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
547 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
550 else if (sym_sec
&& *name
== '\0')
551 name
= bfd_section_name (sym_sec
);
556 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
557 sections. The first element is the flags, the rest are section
560 typedef union elf_internal_group
{
561 Elf_Internal_Shdr
*shdr
;
563 } Elf_Internal_Group
;
565 /* Return the name of the group signature symbol. Why isn't the
566 signature just a string? */
569 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
571 Elf_Internal_Shdr
*hdr
;
572 unsigned char esym
[sizeof (Elf64_External_Sym
)];
573 Elf_External_Sym_Shndx eshndx
;
574 Elf_Internal_Sym isym
;
576 /* First we need to ensure the symbol table is available. Make sure
577 that it is a symbol table section. */
578 if (ghdr
->sh_link
>= elf_numsections (abfd
))
580 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
581 if (hdr
->sh_type
!= SHT_SYMTAB
582 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
585 /* Go read the symbol. */
586 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
587 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
588 &isym
, esym
, &eshndx
) == NULL
)
591 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
594 /* Set next_in_group list pointer, and group name for NEWSECT. */
597 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
599 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
601 /* If num_group is zero, read in all SHT_GROUP sections. The count
602 is set to -1 if there are no SHT_GROUP sections. */
605 unsigned int i
, shnum
;
607 /* First count the number of groups. If we have a SHT_GROUP
608 section with just a flag word (ie. sh_size is 4), ignore it. */
609 shnum
= elf_numsections (abfd
);
612 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
613 ( (shdr)->sh_type == SHT_GROUP \
614 && (shdr)->sh_size >= minsize \
615 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
616 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
618 for (i
= 0; i
< shnum
; i
++)
620 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
622 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
628 num_group
= (unsigned) -1;
629 elf_tdata (abfd
)->num_group
= num_group
;
630 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
634 /* We keep a list of elf section headers for group sections,
635 so we can find them quickly. */
638 elf_tdata (abfd
)->num_group
= num_group
;
639 amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
640 elf_tdata (abfd
)->group_sect_ptr
641 = (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
642 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
646 for (i
= 0; i
< shnum
; i
++)
648 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
650 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
653 Elf_Internal_Group
*dest
;
655 /* Make sure the group section has a BFD section
657 if (!bfd_section_from_shdr (abfd
, i
))
660 /* Add to list of sections. */
661 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
664 /* Read the raw contents. */
665 BFD_ASSERT (sizeof (*dest
) >= 4 && sizeof (*dest
) % 4 == 0);
666 shdr
->contents
= NULL
;
667 if (_bfd_mul_overflow (shdr
->sh_size
,
668 sizeof (*dest
) / 4, &amt
)
669 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
671 = _bfd_alloc_and_read (abfd
, amt
, shdr
->sh_size
)))
674 /* xgettext:c-format */
675 (_("%pB: invalid size field in group section"
676 " header: %#" PRIx64
""),
677 abfd
, (uint64_t) shdr
->sh_size
);
678 bfd_set_error (bfd_error_bad_value
);
683 /* Translate raw contents, a flag word followed by an
684 array of elf section indices all in target byte order,
685 to the flag word followed by an array of elf section
687 src
= shdr
->contents
+ shdr
->sh_size
;
688 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
696 idx
= H_GET_32 (abfd
, src
);
697 if (src
== shdr
->contents
)
701 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
702 shdr
->bfd_section
->flags
703 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
708 dest
->shdr
= elf_elfsections (abfd
)[idx
];
709 /* PR binutils/23199: All sections in a
710 section group should be marked with
711 SHF_GROUP. But some tools generate
712 broken objects without SHF_GROUP. Fix
714 dest
->shdr
->sh_flags
|= SHF_GROUP
;
717 || dest
->shdr
->sh_type
== SHT_GROUP
)
720 (_("%pB: invalid entry in SHT_GROUP section [%u]"),
728 /* PR 17510: Corrupt binaries might contain invalid groups. */
729 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
731 elf_tdata (abfd
)->num_group
= num_group
;
733 /* If all groups are invalid then fail. */
736 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
737 elf_tdata (abfd
)->num_group
= num_group
= -1;
739 (_("%pB: no valid group sections found"), abfd
);
740 bfd_set_error (bfd_error_bad_value
);
746 if (num_group
!= (unsigned) -1)
748 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
751 for (j
= 0; j
< num_group
; j
++)
753 /* Begin search from previous found group. */
754 unsigned i
= (j
+ search_offset
) % num_group
;
756 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
757 Elf_Internal_Group
*idx
;
763 idx
= (Elf_Internal_Group
*) shdr
->contents
;
764 if (idx
== NULL
|| shdr
->sh_size
< 4)
766 /* See PR 21957 for a reproducer. */
767 /* xgettext:c-format */
768 _bfd_error_handler (_("%pB: group section '%pA' has no contents"),
769 abfd
, shdr
->bfd_section
);
770 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
771 bfd_set_error (bfd_error_bad_value
);
774 n_elt
= shdr
->sh_size
/ 4;
776 /* Look through this group's sections to see if current
777 section is a member. */
779 if ((++idx
)->shdr
== hdr
)
783 /* We are a member of this group. Go looking through
784 other members to see if any others are linked via
786 idx
= (Elf_Internal_Group
*) shdr
->contents
;
787 n_elt
= shdr
->sh_size
/ 4;
789 if ((++idx
)->shdr
!= NULL
790 && (s
= idx
->shdr
->bfd_section
) != NULL
791 && elf_next_in_group (s
) != NULL
)
795 /* Snarf the group name from other member, and
796 insert current section in circular list. */
797 elf_group_name (newsect
) = elf_group_name (s
);
798 elf_next_in_group (newsect
) = elf_next_in_group (s
);
799 elf_next_in_group (s
) = newsect
;
805 gname
= group_signature (abfd
, shdr
);
808 elf_group_name (newsect
) = gname
;
810 /* Start a circular list with one element. */
811 elf_next_in_group (newsect
) = newsect
;
814 /* If the group section has been created, point to the
816 if (shdr
->bfd_section
!= NULL
)
817 elf_next_in_group (shdr
->bfd_section
) = newsect
;
819 elf_tdata (abfd
)->group_search_offset
= i
;
826 if (elf_group_name (newsect
) == NULL
)
828 /* xgettext:c-format */
829 _bfd_error_handler (_("%pB: no group info for section '%pA'"),
837 _bfd_elf_setup_sections (bfd
*abfd
)
840 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
841 bfd_boolean result
= TRUE
;
844 /* Process SHF_LINK_ORDER. */
845 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
847 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
848 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
850 unsigned int elfsec
= this_hdr
->sh_link
;
851 /* An sh_link value of 0 is now allowed. It indicates that linked
852 to section has already been discarded, but that the current
853 section has been retained for some other reason. This linking
854 section is still a candidate for later garbage collection
858 elf_linked_to_section (s
) = NULL
;
862 asection
*linksec
= NULL
;
864 if (elfsec
< elf_numsections (abfd
))
866 this_hdr
= elf_elfsections (abfd
)[elfsec
];
867 linksec
= this_hdr
->bfd_section
;
871 Some strip/objcopy may leave an incorrect value in
872 sh_link. We don't want to proceed. */
876 /* xgettext:c-format */
877 (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
878 s
->owner
, elfsec
, s
);
882 elf_linked_to_section (s
) = linksec
;
885 else if (this_hdr
->sh_type
== SHT_GROUP
886 && elf_next_in_group (s
) == NULL
)
889 /* xgettext:c-format */
890 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
891 abfd
, elf_section_data (s
)->this_idx
);
896 /* Process section groups. */
897 if (num_group
== (unsigned) -1)
900 for (i
= 0; i
< num_group
; i
++)
902 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
903 Elf_Internal_Group
*idx
;
906 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
907 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
910 /* xgettext:c-format */
911 (_("%pB: section group entry number %u is corrupt"),
917 idx
= (Elf_Internal_Group
*) shdr
->contents
;
918 n_elt
= shdr
->sh_size
/ 4;
924 if (idx
->shdr
== NULL
)
926 else if (idx
->shdr
->bfd_section
)
927 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
928 else if (idx
->shdr
->sh_type
!= SHT_RELA
929 && idx
->shdr
->sh_type
!= SHT_REL
)
931 /* There are some unknown sections in the group. */
933 /* xgettext:c-format */
934 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"),
937 bfd_elf_string_from_elf_section (abfd
,
938 (elf_elfheader (abfd
)
951 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
953 return elf_next_in_group (sec
) != NULL
;
957 bfd_elf_group_name (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
959 if (elf_sec_group (sec
) != NULL
)
960 return elf_group_name (sec
);
965 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
967 unsigned int len
= strlen (name
);
968 char *new_name
= bfd_alloc (abfd
, len
+ 2);
969 if (new_name
== NULL
)
973 memcpy (new_name
+ 2, name
+ 1, len
);
978 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
980 unsigned int len
= strlen (name
);
981 char *new_name
= bfd_alloc (abfd
, len
);
982 if (new_name
== NULL
)
985 memcpy (new_name
+ 1, name
+ 2, len
- 1);
989 /* This a copy of lto_section defined in GCC (lto-streamer.h). */
993 int16_t major_version
;
994 int16_t minor_version
;
995 unsigned char slim_object
;
997 /* Flags is a private field that is not defined publicly. */
1001 /* Make a BFD section from an ELF section. We store a pointer to the
1002 BFD section in the bfd_section field of the header. */
1005 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
1006 Elf_Internal_Shdr
*hdr
,
1012 const struct elf_backend_data
*bed
;
1013 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
1015 if (hdr
->bfd_section
!= NULL
)
1018 newsect
= bfd_make_section_anyway (abfd
, name
);
1019 if (newsect
== NULL
)
1022 hdr
->bfd_section
= newsect
;
1023 elf_section_data (newsect
)->this_hdr
= *hdr
;
1024 elf_section_data (newsect
)->this_idx
= shindex
;
1026 /* Always use the real type/flags. */
1027 elf_section_type (newsect
) = hdr
->sh_type
;
1028 elf_section_flags (newsect
) = hdr
->sh_flags
;
1030 newsect
->filepos
= hdr
->sh_offset
;
1032 flags
= SEC_NO_FLAGS
;
1033 if (hdr
->sh_type
!= SHT_NOBITS
)
1034 flags
|= SEC_HAS_CONTENTS
;
1035 if (hdr
->sh_type
== SHT_GROUP
)
1037 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1040 if (hdr
->sh_type
!= SHT_NOBITS
)
1043 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1044 flags
|= SEC_READONLY
;
1045 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1047 else if ((flags
& SEC_LOAD
) != 0)
1049 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1052 newsect
->entsize
= hdr
->sh_entsize
;
1054 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1055 flags
|= SEC_STRINGS
;
1056 if (hdr
->sh_flags
& SHF_GROUP
)
1057 if (!setup_group (abfd
, hdr
, newsect
))
1059 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1060 flags
|= SEC_THREAD_LOCAL
;
1061 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1062 flags
|= SEC_EXCLUDE
;
1064 switch (elf_elfheader (abfd
)->e_ident
[EI_OSABI
])
1066 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
1067 but binutils as of 2019-07-23 did not set the EI_OSABI header
1071 case ELFOSABI_FREEBSD
:
1072 if ((hdr
->sh_flags
& SHF_GNU_MBIND
) != 0)
1073 elf_tdata (abfd
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1077 if ((flags
& SEC_ALLOC
) == 0)
1079 /* The debugging sections appear to be recognized only by name,
1080 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1081 if (name
[0] == '.')
1083 if (strncmp (name
, ".debug", 6) == 0
1084 || strncmp (name
, ".gnu.linkonce.wi.", 17) == 0
1085 || strncmp (name
, ".zdebug", 7) == 0)
1086 flags
|= SEC_DEBUGGING
| SEC_ELF_OCTETS
;
1087 else if (strncmp (name
, GNU_BUILD_ATTRS_SECTION_NAME
, 21) == 0
1088 || strncmp (name
, ".note.gnu", 9) == 0)
1090 flags
|= SEC_ELF_OCTETS
;
1093 else if (strncmp (name
, ".line", 5) == 0
1094 || strncmp (name
, ".stab", 5) == 0
1095 || strcmp (name
, ".gdb_index") == 0)
1096 flags
|= SEC_DEBUGGING
;
1100 if (!bfd_set_section_vma (newsect
, hdr
->sh_addr
/ opb
)
1101 || !bfd_set_section_size (newsect
, hdr
->sh_size
)
1102 || !bfd_set_section_alignment (newsect
, bfd_log2 (hdr
->sh_addralign
)))
1105 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1106 only link a single copy of the section. This is used to support
1107 g++. g++ will emit each template expansion in its own section.
1108 The symbols will be defined as weak, so that multiple definitions
1109 are permitted. The GNU linker extension is to actually discard
1110 all but one of the sections. */
1111 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1112 && elf_next_in_group (newsect
) == NULL
)
1113 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1115 if (!bfd_set_section_flags (newsect
, flags
))
1118 bed
= get_elf_backend_data (abfd
);
1119 if (bed
->elf_backend_section_flags
)
1120 if (!bed
->elf_backend_section_flags (hdr
))
1123 /* We do not parse the PT_NOTE segments as we are interested even in the
1124 separate debug info files which may have the segments offsets corrupted.
1125 PT_NOTEs from the core files are currently not parsed using BFD. */
1126 if (hdr
->sh_type
== SHT_NOTE
)
1130 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1133 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1134 hdr
->sh_offset
, hdr
->sh_addralign
);
1138 if ((newsect
->flags
& SEC_ALLOC
) != 0)
1140 Elf_Internal_Phdr
*phdr
;
1141 unsigned int i
, nload
;
1143 /* Some ELF linkers produce binaries with all the program header
1144 p_paddr fields zero. If we have such a binary with more than
1145 one PT_LOAD header, then leave the section lma equal to vma
1146 so that we don't create sections with overlapping lma. */
1147 phdr
= elf_tdata (abfd
)->phdr
;
1148 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1149 if (phdr
->p_paddr
!= 0)
1151 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1153 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1156 phdr
= elf_tdata (abfd
)->phdr
;
1157 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1159 if (((phdr
->p_type
== PT_LOAD
1160 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1161 || phdr
->p_type
== PT_TLS
)
1162 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1164 if ((newsect
->flags
& SEC_LOAD
) == 0)
1165 newsect
->lma
= (phdr
->p_paddr
1166 + hdr
->sh_addr
- phdr
->p_vaddr
) / opb
;
1168 /* We used to use the same adjustment for SEC_LOAD
1169 sections, but that doesn't work if the segment
1170 is packed with code from multiple VMAs.
1171 Instead we calculate the section LMA based on
1172 the segment LMA. It is assumed that the
1173 segment will contain sections with contiguous
1174 LMAs, even if the VMAs are not. */
1175 newsect
->lma
= (phdr
->p_paddr
1176 + hdr
->sh_offset
- phdr
->p_offset
) / opb
;
1178 /* With contiguous segments, we can't tell from file
1179 offsets whether a section with zero size should
1180 be placed at the end of one segment or the
1181 beginning of the next. Decide based on vaddr. */
1182 if (hdr
->sh_addr
>= phdr
->p_vaddr
1183 && (hdr
->sh_addr
+ hdr
->sh_size
1184 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1190 /* Compress/decompress DWARF debug sections with names: .debug_* and
1191 .zdebug_*, after the section flags is set. */
1192 if ((newsect
->flags
& SEC_DEBUGGING
)
1193 && ((name
[1] == 'd' && name
[6] == '_')
1194 || (name
[1] == 'z' && name
[7] == '_')))
1196 enum { nothing
, compress
, decompress
} action
= nothing
;
1197 int compression_header_size
;
1198 bfd_size_type uncompressed_size
;
1199 unsigned int uncompressed_align_power
;
1200 bfd_boolean compressed
1201 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1202 &compression_header_size
,
1204 &uncompressed_align_power
);
1207 /* Compressed section. Check if we should decompress. */
1208 if ((abfd
->flags
& BFD_DECOMPRESS
))
1209 action
= decompress
;
1212 /* Compress the uncompressed section or convert from/to .zdebug*
1213 section. Check if we should compress. */
1214 if (action
== nothing
)
1216 if (newsect
->size
!= 0
1217 && (abfd
->flags
& BFD_COMPRESS
)
1218 && compression_header_size
>= 0
1219 && uncompressed_size
> 0
1221 || ((compression_header_size
> 0)
1222 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1228 if (action
== compress
)
1230 if (!bfd_init_section_compress_status (abfd
, newsect
))
1233 /* xgettext:c-format */
1234 (_("%pB: unable to initialize compress status for section %s"),
1241 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1244 /* xgettext:c-format */
1245 (_("%pB: unable to initialize decompress status for section %s"),
1251 if (abfd
->is_linker_input
)
1254 && (action
== decompress
1255 || (action
== compress
1256 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1258 /* Convert section name from .zdebug_* to .debug_* so
1259 that linker will consider this section as a debug
1261 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1262 if (new_name
== NULL
)
1264 bfd_rename_section (newsect
, new_name
);
1268 /* For objdump, don't rename the section. For objcopy, delay
1269 section rename to elf_fake_sections. */
1270 newsect
->flags
|= SEC_ELF_RENAME
;
1273 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
1275 const char *lto_section_name
= ".gnu.lto_.lto.";
1276 if (strncmp (name
, lto_section_name
, strlen (lto_section_name
)) == 0)
1278 struct lto_section lsection
;
1279 if (bfd_get_section_contents (abfd
, newsect
, &lsection
, 0,
1280 sizeof (struct lto_section
)))
1281 abfd
->lto_slim_object
= lsection
.slim_object
;
1287 const char *const bfd_elf_section_type_names
[] =
1289 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1290 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1291 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1294 /* ELF relocs are against symbols. If we are producing relocatable
1295 output, and the reloc is against an external symbol, and nothing
1296 has given us any additional addend, the resulting reloc will also
1297 be against the same symbol. In such a case, we don't want to
1298 change anything about the way the reloc is handled, since it will
1299 all be done at final link time. Rather than put special case code
1300 into bfd_perform_relocation, all the reloc types use this howto
1301 function. It just short circuits the reloc if producing
1302 relocatable output against an external symbol. */
1304 bfd_reloc_status_type
1305 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1306 arelent
*reloc_entry
,
1308 void *data ATTRIBUTE_UNUSED
,
1309 asection
*input_section
,
1311 char **error_message ATTRIBUTE_UNUSED
)
1313 if (output_bfd
!= NULL
1314 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1315 && (! reloc_entry
->howto
->partial_inplace
1316 || reloc_entry
->addend
== 0))
1318 reloc_entry
->address
+= input_section
->output_offset
;
1319 return bfd_reloc_ok
;
1322 return bfd_reloc_continue
;
1325 /* Returns TRUE if section A matches section B.
1326 Names, addresses and links may be different, but everything else
1327 should be the same. */
1330 section_match (const Elf_Internal_Shdr
* a
,
1331 const Elf_Internal_Shdr
* b
)
1333 if (a
->sh_type
!= b
->sh_type
1334 || ((a
->sh_flags
^ b
->sh_flags
) & ~SHF_INFO_LINK
) != 0
1335 || a
->sh_addralign
!= b
->sh_addralign
1336 || a
->sh_entsize
!= b
->sh_entsize
)
1338 if (a
->sh_type
== SHT_SYMTAB
1339 || a
->sh_type
== SHT_STRTAB
)
1341 return a
->sh_size
== b
->sh_size
;
1344 /* Find a section in OBFD that has the same characteristics
1345 as IHEADER. Return the index of this section or SHN_UNDEF if
1346 none can be found. Check's section HINT first, as this is likely
1347 to be the correct section. */
1350 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1351 const unsigned int hint
)
1353 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1356 BFD_ASSERT (iheader
!= NULL
);
1358 /* See PR 20922 for a reproducer of the NULL test. */
1359 if (hint
< elf_numsections (obfd
)
1360 && oheaders
[hint
] != NULL
1361 && section_match (oheaders
[hint
], iheader
))
1364 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1366 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1368 if (oheader
== NULL
)
1370 if (section_match (oheader
, iheader
))
1371 /* FIXME: Do we care if there is a potential for
1372 multiple matches ? */
1379 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1380 Processor specific section, based upon a matching input section.
1381 Returns TRUE upon success, FALSE otherwise. */
1384 copy_special_section_fields (const bfd
*ibfd
,
1386 const Elf_Internal_Shdr
*iheader
,
1387 Elf_Internal_Shdr
*oheader
,
1388 const unsigned int secnum
)
1390 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1391 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1392 bfd_boolean changed
= FALSE
;
1393 unsigned int sh_link
;
1395 if (oheader
->sh_type
== SHT_NOBITS
)
1397 /* This is a feature for objcopy --only-keep-debug:
1398 When a section's type is changed to NOBITS, we preserve
1399 the sh_link and sh_info fields so that they can be
1400 matched up with the original.
1402 Note: Strictly speaking these assignments are wrong.
1403 The sh_link and sh_info fields should point to the
1404 relevent sections in the output BFD, which may not be in
1405 the same location as they were in the input BFD. But
1406 the whole point of this action is to preserve the
1407 original values of the sh_link and sh_info fields, so
1408 that they can be matched up with the section headers in
1409 the original file. So strictly speaking we may be
1410 creating an invalid ELF file, but it is only for a file
1411 that just contains debug info and only for sections
1412 without any contents. */
1413 if (oheader
->sh_link
== 0)
1414 oheader
->sh_link
= iheader
->sh_link
;
1415 if (oheader
->sh_info
== 0)
1416 oheader
->sh_info
= iheader
->sh_info
;
1420 /* Allow the target a chance to decide how these fields should be set. */
1421 if (bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1425 /* We have an iheader which might match oheader, and which has non-zero
1426 sh_info and/or sh_link fields. Attempt to follow those links and find
1427 the section in the output bfd which corresponds to the linked section
1428 in the input bfd. */
1429 if (iheader
->sh_link
!= SHN_UNDEF
)
1431 /* See PR 20931 for a reproducer. */
1432 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1435 /* xgettext:c-format */
1436 (_("%pB: invalid sh_link field (%d) in section number %d"),
1437 ibfd
, iheader
->sh_link
, secnum
);
1441 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1442 if (sh_link
!= SHN_UNDEF
)
1444 oheader
->sh_link
= sh_link
;
1448 /* FIXME: Should we install iheader->sh_link
1449 if we could not find a match ? */
1451 /* xgettext:c-format */
1452 (_("%pB: failed to find link section for section %d"), obfd
, secnum
);
1455 if (iheader
->sh_info
)
1457 /* The sh_info field can hold arbitrary information, but if the
1458 SHF_LINK_INFO flag is set then it should be interpreted as a
1460 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1462 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1464 if (sh_link
!= SHN_UNDEF
)
1465 oheader
->sh_flags
|= SHF_INFO_LINK
;
1468 /* No idea what it means - just copy it. */
1469 sh_link
= iheader
->sh_info
;
1471 if (sh_link
!= SHN_UNDEF
)
1473 oheader
->sh_info
= sh_link
;
1478 /* xgettext:c-format */
1479 (_("%pB: failed to find info section for section %d"), obfd
, secnum
);
1485 /* Copy the program header and other data from one object module to
1489 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1491 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1492 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1493 const struct elf_backend_data
*bed
;
1496 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1497 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1500 if (!elf_flags_init (obfd
))
1502 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1503 elf_flags_init (obfd
) = TRUE
;
1506 elf_gp (obfd
) = elf_gp (ibfd
);
1508 /* Also copy the EI_OSABI field. */
1509 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1510 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1512 /* If set, copy the EI_ABIVERSION field. */
1513 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1514 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1515 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1517 /* Copy object attributes. */
1518 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1520 if (iheaders
== NULL
|| oheaders
== NULL
)
1523 bed
= get_elf_backend_data (obfd
);
1525 /* Possibly copy other fields in the section header. */
1526 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1529 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1531 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1532 because of a special case need for generating separate debug info
1533 files. See below for more details. */
1535 || (oheader
->sh_type
!= SHT_NOBITS
1536 && oheader
->sh_type
< SHT_LOOS
))
1539 /* Ignore empty sections, and sections whose
1540 fields have already been initialised. */
1541 if (oheader
->sh_size
== 0
1542 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1545 /* Scan for the matching section in the input bfd.
1546 First we try for a direct mapping between the input and output sections. */
1547 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1549 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1551 if (iheader
== NULL
)
1554 if (oheader
->bfd_section
!= NULL
1555 && iheader
->bfd_section
!= NULL
1556 && iheader
->bfd_section
->output_section
!= NULL
1557 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1559 /* We have found a connection from the input section to the
1560 output section. Attempt to copy the header fields. If
1561 this fails then do not try any further sections - there
1562 should only be a one-to-one mapping between input and output. */
1563 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1564 j
= elf_numsections (ibfd
);
1569 if (j
< elf_numsections (ibfd
))
1572 /* That failed. So try to deduce the corresponding input section.
1573 Unfortunately we cannot compare names as the output string table
1574 is empty, so instead we check size, address and type. */
1575 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1577 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1579 if (iheader
== NULL
)
1582 /* Try matching fields in the input section's header.
1583 Since --only-keep-debug turns all non-debug sections into
1584 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1586 if ((oheader
->sh_type
== SHT_NOBITS
1587 || iheader
->sh_type
== oheader
->sh_type
)
1588 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1589 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1590 && iheader
->sh_addralign
== oheader
->sh_addralign
1591 && iheader
->sh_entsize
== oheader
->sh_entsize
1592 && iheader
->sh_size
== oheader
->sh_size
1593 && iheader
->sh_addr
== oheader
->sh_addr
1594 && (iheader
->sh_info
!= oheader
->sh_info
1595 || iheader
->sh_link
!= oheader
->sh_link
))
1597 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1602 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1604 /* Final attempt. Call the backend copy function
1605 with a NULL input section. */
1606 (void) bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
,
1615 get_segment_type (unsigned int p_type
)
1620 case PT_NULL
: pt
= "NULL"; break;
1621 case PT_LOAD
: pt
= "LOAD"; break;
1622 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1623 case PT_INTERP
: pt
= "INTERP"; break;
1624 case PT_NOTE
: pt
= "NOTE"; break;
1625 case PT_SHLIB
: pt
= "SHLIB"; break;
1626 case PT_PHDR
: pt
= "PHDR"; break;
1627 case PT_TLS
: pt
= "TLS"; break;
1628 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1629 case PT_GNU_STACK
: pt
= "STACK"; break;
1630 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1631 default: pt
= NULL
; break;
1636 /* Print out the program headers. */
1639 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1641 FILE *f
= (FILE *) farg
;
1642 Elf_Internal_Phdr
*p
;
1644 bfd_byte
*dynbuf
= NULL
;
1646 p
= elf_tdata (abfd
)->phdr
;
1651 fprintf (f
, _("\nProgram Header:\n"));
1652 c
= elf_elfheader (abfd
)->e_phnum
;
1653 for (i
= 0; i
< c
; i
++, p
++)
1655 const char *pt
= get_segment_type (p
->p_type
);
1660 sprintf (buf
, "0x%lx", p
->p_type
);
1663 fprintf (f
, "%8s off 0x", pt
);
1664 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1665 fprintf (f
, " vaddr 0x");
1666 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1667 fprintf (f
, " paddr 0x");
1668 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1669 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1670 fprintf (f
, " filesz 0x");
1671 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1672 fprintf (f
, " memsz 0x");
1673 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1674 fprintf (f
, " flags %c%c%c",
1675 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1676 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1677 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1678 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1679 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1684 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1687 unsigned int elfsec
;
1688 unsigned long shlink
;
1689 bfd_byte
*extdyn
, *extdynend
;
1691 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1693 fprintf (f
, _("\nDynamic Section:\n"));
1695 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1698 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1699 if (elfsec
== SHN_BAD
)
1701 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1703 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1704 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1707 /* PR 17512: file: 6f427532. */
1708 if (s
->size
< extdynsize
)
1710 extdynend
= extdyn
+ s
->size
;
1711 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1713 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1715 Elf_Internal_Dyn dyn
;
1716 const char *name
= "";
1718 bfd_boolean stringp
;
1719 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1721 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1723 if (dyn
.d_tag
== DT_NULL
)
1730 if (bed
->elf_backend_get_target_dtag
)
1731 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1733 if (!strcmp (name
, ""))
1735 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1740 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1741 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1742 case DT_PLTGOT
: name
= "PLTGOT"; break;
1743 case DT_HASH
: name
= "HASH"; break;
1744 case DT_STRTAB
: name
= "STRTAB"; break;
1745 case DT_SYMTAB
: name
= "SYMTAB"; break;
1746 case DT_RELA
: name
= "RELA"; break;
1747 case DT_RELASZ
: name
= "RELASZ"; break;
1748 case DT_RELAENT
: name
= "RELAENT"; break;
1749 case DT_STRSZ
: name
= "STRSZ"; break;
1750 case DT_SYMENT
: name
= "SYMENT"; break;
1751 case DT_INIT
: name
= "INIT"; break;
1752 case DT_FINI
: name
= "FINI"; break;
1753 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1754 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1755 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1756 case DT_REL
: name
= "REL"; break;
1757 case DT_RELSZ
: name
= "RELSZ"; break;
1758 case DT_RELENT
: name
= "RELENT"; break;
1759 case DT_PLTREL
: name
= "PLTREL"; break;
1760 case DT_DEBUG
: name
= "DEBUG"; break;
1761 case DT_TEXTREL
: name
= "TEXTREL"; break;
1762 case DT_JMPREL
: name
= "JMPREL"; break;
1763 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1764 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1765 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1766 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1767 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1768 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1769 case DT_FLAGS
: name
= "FLAGS"; break;
1770 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1771 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1772 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1773 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1774 case DT_MOVEENT
: name
= "MOVEENT"; break;
1775 case DT_MOVESZ
: name
= "MOVESZ"; break;
1776 case DT_FEATURE
: name
= "FEATURE"; break;
1777 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1778 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1779 case DT_SYMINENT
: name
= "SYMINENT"; break;
1780 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1781 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1782 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1783 case DT_PLTPAD
: name
= "PLTPAD"; break;
1784 case DT_MOVETAB
: name
= "MOVETAB"; break;
1785 case DT_SYMINFO
: name
= "SYMINFO"; break;
1786 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1787 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1788 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1789 case DT_VERSYM
: name
= "VERSYM"; break;
1790 case DT_VERDEF
: name
= "VERDEF"; break;
1791 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1792 case DT_VERNEED
: name
= "VERNEED"; break;
1793 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1794 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1795 case DT_USED
: name
= "USED"; break;
1796 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1797 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1800 fprintf (f
, " %-20s ", name
);
1804 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1809 unsigned int tagv
= dyn
.d_un
.d_val
;
1811 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1814 fprintf (f
, "%s", string
);
1823 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1824 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1826 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1830 if (elf_dynverdef (abfd
) != 0)
1832 Elf_Internal_Verdef
*t
;
1834 fprintf (f
, _("\nVersion definitions:\n"));
1835 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1837 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1838 t
->vd_flags
, t
->vd_hash
,
1839 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1840 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1842 Elf_Internal_Verdaux
*a
;
1845 for (a
= t
->vd_auxptr
->vda_nextptr
;
1849 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1855 if (elf_dynverref (abfd
) != 0)
1857 Elf_Internal_Verneed
*t
;
1859 fprintf (f
, _("\nVersion References:\n"));
1860 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1862 Elf_Internal_Vernaux
*a
;
1864 fprintf (f
, _(" required from %s:\n"),
1865 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1866 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1867 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1868 a
->vna_flags
, a
->vna_other
,
1869 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1880 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE
1881 and return symbol version for symbol version itself. */
1884 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1886 bfd_boolean
*hidden
)
1888 const char *version_string
= NULL
;
1889 if (elf_dynversym (abfd
) != 0
1890 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1892 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1894 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1895 vernum
&= VERSYM_VERSION
;
1898 version_string
= "";
1899 else if (vernum
== 1
1900 && (vernum
> elf_tdata (abfd
)->cverdefs
1901 || (elf_tdata (abfd
)->verdef
[0].vd_flags
1903 version_string
= base_p
? "Base" : "";
1904 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1906 const char *nodename
1907 = elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1908 version_string
= "";
1911 || symbol
->name
== NULL
1912 || strcmp (symbol
->name
, nodename
) != 0)
1913 version_string
= nodename
;
1917 Elf_Internal_Verneed
*t
;
1919 version_string
= _("<corrupt>");
1920 for (t
= elf_tdata (abfd
)->verref
;
1924 Elf_Internal_Vernaux
*a
;
1926 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1928 if (a
->vna_other
== vernum
)
1930 version_string
= a
->vna_nodename
;
1937 return version_string
;
1940 /* Display ELF-specific fields of a symbol. */
1943 bfd_elf_print_symbol (bfd
*abfd
,
1946 bfd_print_symbol_type how
)
1948 FILE *file
= (FILE *) filep
;
1951 case bfd_print_symbol_name
:
1952 fprintf (file
, "%s", symbol
->name
);
1954 case bfd_print_symbol_more
:
1955 fprintf (file
, "elf ");
1956 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1957 fprintf (file
, " %x", symbol
->flags
);
1959 case bfd_print_symbol_all
:
1961 const char *section_name
;
1962 const char *name
= NULL
;
1963 const struct elf_backend_data
*bed
;
1964 unsigned char st_other
;
1966 const char *version_string
;
1969 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1971 bed
= get_elf_backend_data (abfd
);
1972 if (bed
->elf_backend_print_symbol_all
)
1973 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1977 name
= symbol
->name
;
1978 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1981 fprintf (file
, " %s\t", section_name
);
1982 /* Print the "other" value for a symbol. For common symbols,
1983 we've already printed the size; now print the alignment.
1984 For other symbols, we have no specified alignment, and
1985 we've printed the address; now print the size. */
1986 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1987 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1989 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1990 bfd_fprintf_vma (abfd
, file
, val
);
1992 /* If we have version information, print it. */
1993 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
2000 fprintf (file
, " %-11s", version_string
);
2005 fprintf (file
, " (%s)", version_string
);
2006 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
2011 /* If the st_other field is not zero, print it. */
2012 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
2017 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
2018 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
2019 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
2021 /* Some other non-defined flags are also present, so print
2023 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
2026 fprintf (file
, " %s", name
);
2032 /* ELF .o/exec file reading */
2034 /* Create a new bfd section from an ELF section header. */
2037 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
2039 Elf_Internal_Shdr
*hdr
;
2040 Elf_Internal_Ehdr
*ehdr
;
2041 const struct elf_backend_data
*bed
;
2043 bfd_boolean ret
= TRUE
;
2044 static bfd_boolean
* sections_being_created
= NULL
;
2045 static bfd
* sections_being_created_abfd
= NULL
;
2046 static unsigned int nesting
= 0;
2048 if (shindex
>= elf_numsections (abfd
))
2053 /* PR17512: A corrupt ELF binary might contain a recursive group of
2054 sections, with each the string indices pointing to the next in the
2055 loop. Detect this here, by refusing to load a section that we are
2056 already in the process of loading. We only trigger this test if
2057 we have nested at least three sections deep as normal ELF binaries
2058 can expect to recurse at least once.
2060 FIXME: It would be better if this array was attached to the bfd,
2061 rather than being held in a static pointer. */
2063 if (sections_being_created_abfd
!= abfd
)
2064 sections_being_created
= NULL
;
2065 if (sections_being_created
== NULL
)
2067 size_t amt
= elf_numsections (abfd
) * sizeof (bfd_boolean
);
2069 /* PR 26005: Do not use bfd_zalloc here as the memory might
2070 be released before the bfd has been fully scanned. */
2071 sections_being_created
= (bfd_boolean
*) bfd_malloc (amt
);
2072 if (sections_being_created
== NULL
)
2074 memset (sections_being_created
, FALSE
, amt
);
2075 sections_being_created_abfd
= abfd
;
2077 if (sections_being_created
[shindex
])
2080 (_("%pB: warning: loop in section dependencies detected"), abfd
);
2083 sections_being_created
[shindex
] = TRUE
;
2086 hdr
= elf_elfsections (abfd
)[shindex
];
2087 ehdr
= elf_elfheader (abfd
);
2088 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2093 bed
= get_elf_backend_data (abfd
);
2094 switch (hdr
->sh_type
)
2097 /* Inactive section. Throw it away. */
2100 case SHT_PROGBITS
: /* Normal section with contents. */
2101 case SHT_NOBITS
: /* .bss section. */
2102 case SHT_HASH
: /* .hash section. */
2103 case SHT_NOTE
: /* .note section. */
2104 case SHT_INIT_ARRAY
: /* .init_array section. */
2105 case SHT_FINI_ARRAY
: /* .fini_array section. */
2106 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2107 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2108 case SHT_GNU_HASH
: /* .gnu.hash section. */
2109 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2112 case SHT_DYNAMIC
: /* Dynamic linking information. */
2113 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2116 if (hdr
->sh_link
> elf_numsections (abfd
))
2118 /* PR 10478: Accept Solaris binaries with a sh_link
2119 field set to SHN_BEFORE or SHN_AFTER. */
2120 switch (bfd_get_arch (abfd
))
2123 case bfd_arch_sparc
:
2124 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2125 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2127 /* Otherwise fall through. */
2132 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2134 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2136 Elf_Internal_Shdr
*dynsymhdr
;
2138 /* The shared libraries distributed with hpux11 have a bogus
2139 sh_link field for the ".dynamic" section. Find the
2140 string table for the ".dynsym" section instead. */
2141 if (elf_dynsymtab (abfd
) != 0)
2143 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2144 hdr
->sh_link
= dynsymhdr
->sh_link
;
2148 unsigned int i
, num_sec
;
2150 num_sec
= elf_numsections (abfd
);
2151 for (i
= 1; i
< num_sec
; i
++)
2153 dynsymhdr
= elf_elfsections (abfd
)[i
];
2154 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2156 hdr
->sh_link
= dynsymhdr
->sh_link
;
2164 case SHT_SYMTAB
: /* A symbol table. */
2165 if (elf_onesymtab (abfd
) == shindex
)
2168 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2171 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2173 if (hdr
->sh_size
!= 0)
2175 /* Some assemblers erroneously set sh_info to one with a
2176 zero sh_size. ld sees this as a global symbol count
2177 of (unsigned) -1. Fix it here. */
2182 /* PR 18854: A binary might contain more than one symbol table.
2183 Unusual, but possible. Warn, but continue. */
2184 if (elf_onesymtab (abfd
) != 0)
2187 /* xgettext:c-format */
2188 (_("%pB: warning: multiple symbol tables detected"
2189 " - ignoring the table in section %u"),
2193 elf_onesymtab (abfd
) = shindex
;
2194 elf_symtab_hdr (abfd
) = *hdr
;
2195 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2196 abfd
->flags
|= HAS_SYMS
;
2198 /* Sometimes a shared object will map in the symbol table. If
2199 SHF_ALLOC is set, and this is a shared object, then we also
2200 treat this section as a BFD section. We can not base the
2201 decision purely on SHF_ALLOC, because that flag is sometimes
2202 set in a relocatable object file, which would confuse the
2204 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2205 && (abfd
->flags
& DYNAMIC
) != 0
2206 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2210 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2211 can't read symbols without that section loaded as well. It
2212 is most likely specified by the next section header. */
2214 elf_section_list
* entry
;
2215 unsigned int i
, num_sec
;
2217 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2218 if (entry
->hdr
.sh_link
== shindex
)
2221 num_sec
= elf_numsections (abfd
);
2222 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2224 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2226 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2227 && hdr2
->sh_link
== shindex
)
2232 for (i
= 1; i
< shindex
; i
++)
2234 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2236 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2237 && hdr2
->sh_link
== shindex
)
2242 ret
= bfd_section_from_shdr (abfd
, i
);
2243 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2247 case SHT_DYNSYM
: /* A dynamic symbol table. */
2248 if (elf_dynsymtab (abfd
) == shindex
)
2251 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2254 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2256 if (hdr
->sh_size
!= 0)
2259 /* Some linkers erroneously set sh_info to one with a
2260 zero sh_size. ld sees this as a global symbol count
2261 of (unsigned) -1. Fix it here. */
2266 /* PR 18854: A binary might contain more than one dynamic symbol table.
2267 Unusual, but possible. Warn, but continue. */
2268 if (elf_dynsymtab (abfd
) != 0)
2271 /* xgettext:c-format */
2272 (_("%pB: warning: multiple dynamic symbol tables detected"
2273 " - ignoring the table in section %u"),
2277 elf_dynsymtab (abfd
) = shindex
;
2278 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2279 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2280 abfd
->flags
|= HAS_SYMS
;
2282 /* Besides being a symbol table, we also treat this as a regular
2283 section, so that objcopy can handle it. */
2284 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2287 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2289 elf_section_list
* entry
;
2291 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2292 if (entry
->ndx
== shindex
)
2295 entry
= bfd_alloc (abfd
, sizeof (*entry
));
2298 entry
->ndx
= shindex
;
2300 entry
->next
= elf_symtab_shndx_list (abfd
);
2301 elf_symtab_shndx_list (abfd
) = entry
;
2302 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2306 case SHT_STRTAB
: /* A string table. */
2307 if (hdr
->bfd_section
!= NULL
)
2310 if (ehdr
->e_shstrndx
== shindex
)
2312 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2313 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2317 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2320 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2321 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2325 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2328 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2329 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2330 elf_elfsections (abfd
)[shindex
] = hdr
;
2331 /* We also treat this as a regular section, so that objcopy
2333 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2338 /* If the string table isn't one of the above, then treat it as a
2339 regular section. We need to scan all the headers to be sure,
2340 just in case this strtab section appeared before the above. */
2341 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2343 unsigned int i
, num_sec
;
2345 num_sec
= elf_numsections (abfd
);
2346 for (i
= 1; i
< num_sec
; i
++)
2348 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2349 if (hdr2
->sh_link
== shindex
)
2351 /* Prevent endless recursion on broken objects. */
2354 if (! bfd_section_from_shdr (abfd
, i
))
2356 if (elf_onesymtab (abfd
) == i
)
2358 if (elf_dynsymtab (abfd
) == i
)
2359 goto dynsymtab_strtab
;
2363 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2368 /* *These* do a lot of work -- but build no sections! */
2370 asection
*target_sect
;
2371 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2372 unsigned int num_sec
= elf_numsections (abfd
);
2373 struct bfd_elf_section_data
*esdt
;
2376 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2377 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2380 /* Check for a bogus link to avoid crashing. */
2381 if (hdr
->sh_link
>= num_sec
)
2384 /* xgettext:c-format */
2385 (_("%pB: invalid link %u for reloc section %s (index %u)"),
2386 abfd
, hdr
->sh_link
, name
, shindex
);
2387 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2392 /* For some incomprehensible reason Oracle distributes
2393 libraries for Solaris in which some of the objects have
2394 bogus sh_link fields. It would be nice if we could just
2395 reject them, but, unfortunately, some people need to use
2396 them. We scan through the section headers; if we find only
2397 one suitable symbol table, we clobber the sh_link to point
2398 to it. I hope this doesn't break anything.
2400 Don't do it on executable nor shared library. */
2401 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2402 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2403 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2409 for (scan
= 1; scan
< num_sec
; scan
++)
2411 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2412 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2423 hdr
->sh_link
= found
;
2426 /* Get the symbol table. */
2427 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2428 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2429 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2432 /* If this is an alloc section in an executable or shared
2433 library, or the reloc section does not use the main symbol
2434 table we don't treat it as a reloc section. BFD can't
2435 adequately represent such a section, so at least for now,
2436 we don't try. We just present it as a normal section. We
2437 also can't use it as a reloc section if it points to the
2438 null section, an invalid section, another reloc section, or
2439 its sh_link points to the null section. */
2440 if (((abfd
->flags
& (DYNAMIC
| EXEC_P
)) != 0
2441 && (hdr
->sh_flags
& SHF_ALLOC
) != 0)
2442 || hdr
->sh_link
== SHN_UNDEF
2443 || hdr
->sh_link
!= elf_onesymtab (abfd
)
2444 || hdr
->sh_info
== SHN_UNDEF
2445 || hdr
->sh_info
>= num_sec
2446 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2447 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2449 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2454 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2457 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2458 if (target_sect
== NULL
)
2461 esdt
= elf_section_data (target_sect
);
2462 if (hdr
->sh_type
== SHT_RELA
)
2463 p_hdr
= &esdt
->rela
.hdr
;
2465 p_hdr
= &esdt
->rel
.hdr
;
2467 /* PR 17512: file: 0b4f81b7.
2468 Also see PR 24456, for a file which deliberately has two reloc
2472 if (!bed
->init_secondary_reloc_section (abfd
, hdr
, name
, shindex
))
2475 /* xgettext:c-format */
2476 (_("%pB: warning: secondary relocation section '%s' "
2477 "for section %pA found - ignoring"),
2478 abfd
, name
, target_sect
);
2483 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2488 elf_elfsections (abfd
)[shindex
] = hdr2
;
2489 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2490 * bed
->s
->int_rels_per_ext_rel
);
2491 target_sect
->flags
|= SEC_RELOC
;
2492 target_sect
->relocation
= NULL
;
2493 target_sect
->rel_filepos
= hdr
->sh_offset
;
2494 /* In the section to which the relocations apply, mark whether
2495 its relocations are of the REL or RELA variety. */
2496 if (hdr
->sh_size
!= 0)
2498 if (hdr
->sh_type
== SHT_RELA
)
2499 target_sect
->use_rela_p
= 1;
2501 abfd
->flags
|= HAS_RELOC
;
2505 case SHT_GNU_verdef
:
2506 elf_dynverdef (abfd
) = shindex
;
2507 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2508 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2511 case SHT_GNU_versym
:
2512 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2515 elf_dynversym (abfd
) = shindex
;
2516 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2517 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2520 case SHT_GNU_verneed
:
2521 elf_dynverref (abfd
) = shindex
;
2522 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2523 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2530 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2533 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2539 /* Possibly an attributes section. */
2540 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2541 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2543 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2545 _bfd_elf_parse_attributes (abfd
, hdr
);
2549 /* Check for any processor-specific section types. */
2550 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2553 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2555 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2556 /* FIXME: How to properly handle allocated section reserved
2557 for applications? */
2559 /* xgettext:c-format */
2560 (_("%pB: unknown type [%#x] section `%s'"),
2561 abfd
, hdr
->sh_type
, name
);
2564 /* Allow sections reserved for applications. */
2565 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2570 else if (hdr
->sh_type
>= SHT_LOPROC
2571 && hdr
->sh_type
<= SHT_HIPROC
)
2572 /* FIXME: We should handle this section. */
2574 /* xgettext:c-format */
2575 (_("%pB: unknown type [%#x] section `%s'"),
2576 abfd
, hdr
->sh_type
, name
);
2577 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2579 /* Unrecognised OS-specific sections. */
2580 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2581 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2582 required to correctly process the section and the file should
2583 be rejected with an error message. */
2585 /* xgettext:c-format */
2586 (_("%pB: unknown type [%#x] section `%s'"),
2587 abfd
, hdr
->sh_type
, name
);
2590 /* Otherwise it should be processed. */
2591 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2596 /* FIXME: We should handle this section. */
2598 /* xgettext:c-format */
2599 (_("%pB: unknown type [%#x] section `%s'"),
2600 abfd
, hdr
->sh_type
, name
);
2608 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2609 sections_being_created
[shindex
] = FALSE
;
2610 if (-- nesting
== 0)
2612 free (sections_being_created
);
2613 sections_being_created
= NULL
;
2614 sections_being_created_abfd
= NULL
;
2619 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2622 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2624 unsigned long r_symndx
)
2626 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2628 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2630 Elf_Internal_Shdr
*symtab_hdr
;
2631 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2632 Elf_External_Sym_Shndx eshndx
;
2634 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2635 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2636 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2639 if (cache
->abfd
!= abfd
)
2641 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2644 cache
->indx
[ent
] = r_symndx
;
2647 return &cache
->sym
[ent
];
2650 /* Given an ELF section number, retrieve the corresponding BFD
2654 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2656 if (sec_index
>= elf_numsections (abfd
))
2658 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2661 static const struct bfd_elf_special_section special_sections_b
[] =
2663 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2664 { NULL
, 0, 0, 0, 0 }
2667 static const struct bfd_elf_special_section special_sections_c
[] =
2669 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2670 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS
, 0 },
2671 { NULL
, 0, 0, 0, 0 }
2674 static const struct bfd_elf_special_section special_sections_d
[] =
2676 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2677 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2678 /* There are more DWARF sections than these, but they needn't be added here
2679 unless you have to cope with broken compilers that don't emit section
2680 attributes or you want to help the user writing assembler. */
2681 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2682 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2683 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2684 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2685 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2686 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2687 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2688 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2689 { NULL
, 0, 0, 0, 0 }
2692 static const struct bfd_elf_special_section special_sections_f
[] =
2694 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2695 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2696 { NULL
, 0 , 0, 0, 0 }
2699 static const struct bfd_elf_special_section special_sections_g
[] =
2701 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2702 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2703 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2704 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2705 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2706 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2707 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2708 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2709 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2710 { NULL
, 0, 0, 0, 0 }
2713 static const struct bfd_elf_special_section special_sections_h
[] =
2715 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2716 { NULL
, 0, 0, 0, 0 }
2719 static const struct bfd_elf_special_section special_sections_i
[] =
2721 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2722 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2723 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2724 { NULL
, 0, 0, 0, 0 }
2727 static const struct bfd_elf_special_section special_sections_l
[] =
2729 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2730 { NULL
, 0, 0, 0, 0 }
2733 static const struct bfd_elf_special_section special_sections_n
[] =
2735 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2736 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2737 { NULL
, 0, 0, 0, 0 }
2740 static const struct bfd_elf_special_section special_sections_p
[] =
2742 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2743 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2744 { NULL
, 0, 0, 0, 0 }
2747 static const struct bfd_elf_special_section special_sections_r
[] =
2749 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2750 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2751 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2752 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2753 { NULL
, 0, 0, 0, 0 }
2756 static const struct bfd_elf_special_section special_sections_s
[] =
2758 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2759 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2760 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2761 /* See struct bfd_elf_special_section declaration for the semantics of
2762 this special case where .prefix_length != strlen (.prefix). */
2763 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2764 { NULL
, 0, 0, 0, 0 }
2767 static const struct bfd_elf_special_section special_sections_t
[] =
2769 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2770 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2771 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2772 { NULL
, 0, 0, 0, 0 }
2775 static const struct bfd_elf_special_section special_sections_z
[] =
2777 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2778 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2779 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2780 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2781 { NULL
, 0, 0, 0, 0 }
2784 static const struct bfd_elf_special_section
* const special_sections
[] =
2786 special_sections_b
, /* 'b' */
2787 special_sections_c
, /* 'c' */
2788 special_sections_d
, /* 'd' */
2790 special_sections_f
, /* 'f' */
2791 special_sections_g
, /* 'g' */
2792 special_sections_h
, /* 'h' */
2793 special_sections_i
, /* 'i' */
2796 special_sections_l
, /* 'l' */
2798 special_sections_n
, /* 'n' */
2800 special_sections_p
, /* 'p' */
2802 special_sections_r
, /* 'r' */
2803 special_sections_s
, /* 's' */
2804 special_sections_t
, /* 't' */
2810 special_sections_z
/* 'z' */
2813 const struct bfd_elf_special_section
*
2814 _bfd_elf_get_special_section (const char *name
,
2815 const struct bfd_elf_special_section
*spec
,
2821 len
= strlen (name
);
2823 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2826 int prefix_len
= spec
[i
].prefix_length
;
2828 if (len
< prefix_len
)
2830 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2833 suffix_len
= spec
[i
].suffix_length
;
2834 if (suffix_len
<= 0)
2836 if (name
[prefix_len
] != 0)
2838 if (suffix_len
== 0)
2840 if (name
[prefix_len
] != '.'
2841 && (suffix_len
== -2
2842 || (rela
&& spec
[i
].type
== SHT_REL
)))
2848 if (len
< prefix_len
+ suffix_len
)
2850 if (memcmp (name
+ len
- suffix_len
,
2851 spec
[i
].prefix
+ prefix_len
,
2861 const struct bfd_elf_special_section
*
2862 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2865 const struct bfd_elf_special_section
*spec
;
2866 const struct elf_backend_data
*bed
;
2868 /* See if this is one of the special sections. */
2869 if (sec
->name
== NULL
)
2872 bed
= get_elf_backend_data (abfd
);
2873 spec
= bed
->special_sections
;
2876 spec
= _bfd_elf_get_special_section (sec
->name
,
2877 bed
->special_sections
,
2883 if (sec
->name
[0] != '.')
2886 i
= sec
->name
[1] - 'b';
2887 if (i
< 0 || i
> 'z' - 'b')
2890 spec
= special_sections
[i
];
2895 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2899 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2901 struct bfd_elf_section_data
*sdata
;
2902 const struct elf_backend_data
*bed
;
2903 const struct bfd_elf_special_section
*ssect
;
2905 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2908 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2912 sec
->used_by_bfd
= sdata
;
2915 /* Indicate whether or not this section should use RELA relocations. */
2916 bed
= get_elf_backend_data (abfd
);
2917 sec
->use_rela_p
= bed
->default_use_rela_p
;
2919 /* Set up ELF section type and flags for newly created sections, if
2920 there is an ABI mandated section. */
2921 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2924 elf_section_type (sec
) = ssect
->type
;
2925 elf_section_flags (sec
) = ssect
->attr
;
2928 return _bfd_generic_new_section_hook (abfd
, sec
);
2931 /* Create a new bfd section from an ELF program header.
2933 Since program segments have no names, we generate a synthetic name
2934 of the form segment<NUM>, where NUM is generally the index in the
2935 program header table. For segments that are split (see below) we
2936 generate the names segment<NUM>a and segment<NUM>b.
2938 Note that some program segments may have a file size that is different than
2939 (less than) the memory size. All this means is that at execution the
2940 system must allocate the amount of memory specified by the memory size,
2941 but only initialize it with the first "file size" bytes read from the
2942 file. This would occur for example, with program segments consisting
2943 of combined data+bss.
2945 To handle the above situation, this routine generates TWO bfd sections
2946 for the single program segment. The first has the length specified by
2947 the file size of the segment, and the second has the length specified
2948 by the difference between the two sizes. In effect, the segment is split
2949 into its initialized and uninitialized parts.
2954 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2955 Elf_Internal_Phdr
*hdr
,
2957 const char *type_name
)
2964 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
2966 split
= ((hdr
->p_memsz
> 0)
2967 && (hdr
->p_filesz
> 0)
2968 && (hdr
->p_memsz
> hdr
->p_filesz
));
2970 if (hdr
->p_filesz
> 0)
2972 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2973 len
= strlen (namebuf
) + 1;
2974 name
= (char *) bfd_alloc (abfd
, len
);
2977 memcpy (name
, namebuf
, len
);
2978 newsect
= bfd_make_section (abfd
, name
);
2979 if (newsect
== NULL
)
2981 newsect
->vma
= hdr
->p_vaddr
/ opb
;
2982 newsect
->lma
= hdr
->p_paddr
/ opb
;
2983 newsect
->size
= hdr
->p_filesz
;
2984 newsect
->filepos
= hdr
->p_offset
;
2985 newsect
->flags
|= SEC_HAS_CONTENTS
;
2986 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2987 if (hdr
->p_type
== PT_LOAD
)
2989 newsect
->flags
|= SEC_ALLOC
;
2990 newsect
->flags
|= SEC_LOAD
;
2991 if (hdr
->p_flags
& PF_X
)
2993 /* FIXME: all we known is that it has execute PERMISSION,
2995 newsect
->flags
|= SEC_CODE
;
2998 if (!(hdr
->p_flags
& PF_W
))
3000 newsect
->flags
|= SEC_READONLY
;
3004 if (hdr
->p_memsz
> hdr
->p_filesz
)
3008 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
3009 len
= strlen (namebuf
) + 1;
3010 name
= (char *) bfd_alloc (abfd
, len
);
3013 memcpy (name
, namebuf
, len
);
3014 newsect
= bfd_make_section (abfd
, name
);
3015 if (newsect
== NULL
)
3017 newsect
->vma
= (hdr
->p_vaddr
+ hdr
->p_filesz
) / opb
;
3018 newsect
->lma
= (hdr
->p_paddr
+ hdr
->p_filesz
) / opb
;
3019 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
3020 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
3021 align
= newsect
->vma
& -newsect
->vma
;
3022 if (align
== 0 || align
> hdr
->p_align
)
3023 align
= hdr
->p_align
;
3024 newsect
->alignment_power
= bfd_log2 (align
);
3025 if (hdr
->p_type
== PT_LOAD
)
3027 newsect
->flags
|= SEC_ALLOC
;
3028 if (hdr
->p_flags
& PF_X
)
3029 newsect
->flags
|= SEC_CODE
;
3031 if (!(hdr
->p_flags
& PF_W
))
3032 newsect
->flags
|= SEC_READONLY
;
3039 _bfd_elf_core_find_build_id (bfd
*templ
, bfd_vma offset
)
3041 /* The return value is ignored. Build-ids are considered optional. */
3042 if (templ
->xvec
->flavour
== bfd_target_elf_flavour
)
3043 return (*get_elf_backend_data (templ
)->elf_backend_core_find_build_id
)
3049 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
3051 const struct elf_backend_data
*bed
;
3053 switch (hdr
->p_type
)
3056 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
3059 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load"))
3061 if (bfd_get_format (abfd
) == bfd_core
&& abfd
->build_id
== NULL
)
3062 _bfd_elf_core_find_build_id (abfd
, hdr
->p_offset
);
3066 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
3069 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
3072 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3074 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3080 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3083 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3085 case PT_GNU_EH_FRAME
:
3086 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3090 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3093 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3096 /* Check for any processor-specific program segment types. */
3097 bed
= get_elf_backend_data (abfd
);
3098 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3102 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3106 _bfd_elf_single_rel_hdr (asection
*sec
)
3108 if (elf_section_data (sec
)->rel
.hdr
)
3110 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3111 return elf_section_data (sec
)->rel
.hdr
;
3114 return elf_section_data (sec
)->rela
.hdr
;
3118 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3119 Elf_Internal_Shdr
*rel_hdr
,
3120 const char *sec_name
,
3121 bfd_boolean use_rela_p
)
3123 char *name
= (char *) bfd_alloc (abfd
,
3124 sizeof ".rela" + strlen (sec_name
));
3128 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3130 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3132 if (rel_hdr
->sh_name
== (unsigned int) -1)
3138 /* Allocate and initialize a section-header for a new reloc section,
3139 containing relocations against ASECT. It is stored in RELDATA. If
3140 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3144 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3145 struct bfd_elf_section_reloc_data
*reldata
,
3146 const char *sec_name
,
3147 bfd_boolean use_rela_p
,
3148 bfd_boolean delay_st_name_p
)
3150 Elf_Internal_Shdr
*rel_hdr
;
3151 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3153 BFD_ASSERT (reldata
->hdr
== NULL
);
3154 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3155 reldata
->hdr
= rel_hdr
;
3157 if (delay_st_name_p
)
3158 rel_hdr
->sh_name
= (unsigned int) -1;
3159 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3162 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3163 rel_hdr
->sh_entsize
= (use_rela_p
3164 ? bed
->s
->sizeof_rela
3165 : bed
->s
->sizeof_rel
);
3166 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3167 rel_hdr
->sh_flags
= 0;
3168 rel_hdr
->sh_addr
= 0;
3169 rel_hdr
->sh_size
= 0;
3170 rel_hdr
->sh_offset
= 0;
3175 /* Return the default section type based on the passed in section flags. */
3178 bfd_elf_get_default_section_type (flagword flags
)
3180 if ((flags
& (SEC_ALLOC
| SEC_IS_COMMON
)) != 0
3181 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3183 return SHT_PROGBITS
;
3186 struct fake_section_arg
3188 struct bfd_link_info
*link_info
;
3192 /* Set up an ELF internal section header for a section. */
3195 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3197 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3198 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3199 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3200 Elf_Internal_Shdr
*this_hdr
;
3201 unsigned int sh_type
;
3202 const char *name
= asect
->name
;
3203 bfd_boolean delay_st_name_p
= FALSE
;
3208 /* We already failed; just get out of the bfd_map_over_sections
3213 this_hdr
= &esd
->this_hdr
;
3217 /* ld: compress DWARF debug sections with names: .debug_*. */
3218 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3219 && (asect
->flags
& SEC_DEBUGGING
)
3223 /* Set SEC_ELF_COMPRESS to indicate this section should be
3225 asect
->flags
|= SEC_ELF_COMPRESS
;
3226 /* If this section will be compressed, delay adding section
3227 name to section name section after it is compressed in
3228 _bfd_elf_assign_file_positions_for_non_load. */
3229 delay_st_name_p
= TRUE
;
3232 else if ((asect
->flags
& SEC_ELF_RENAME
))
3234 /* objcopy: rename output DWARF debug section. */
3235 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3237 /* When we decompress or compress with SHF_COMPRESSED,
3238 convert section name from .zdebug_* to .debug_* if
3242 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3243 if (new_name
== NULL
)
3251 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3253 /* PR binutils/18087: Compression does not always make a
3254 section smaller. So only rename the section when
3255 compression has actually taken place. If input section
3256 name is .zdebug_*, we should never compress it again. */
3257 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3258 if (new_name
== NULL
)
3263 BFD_ASSERT (name
[1] != 'z');
3268 if (delay_st_name_p
)
3269 this_hdr
->sh_name
= (unsigned int) -1;
3273 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3275 if (this_hdr
->sh_name
== (unsigned int) -1)
3282 /* Don't clear sh_flags. Assembler may set additional bits. */
3284 if ((asect
->flags
& SEC_ALLOC
) != 0
3285 || asect
->user_set_vma
)
3286 this_hdr
->sh_addr
= asect
->vma
* bfd_octets_per_byte (abfd
, asect
);
3288 this_hdr
->sh_addr
= 0;
3290 this_hdr
->sh_offset
= 0;
3291 this_hdr
->sh_size
= asect
->size
;
3292 this_hdr
->sh_link
= 0;
3293 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3294 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3297 /* xgettext:c-format */
3298 (_("%pB: error: alignment power %d of section `%pA' is too big"),
3299 abfd
, asect
->alignment_power
, asect
);
3303 /* Set sh_addralign to the highest power of two given by alignment
3304 consistent with the section VMA. Linker scripts can force VMA. */
3305 mask
= ((bfd_vma
) 1 << asect
->alignment_power
) | this_hdr
->sh_addr
;
3306 this_hdr
->sh_addralign
= mask
& -mask
;
3307 /* The sh_entsize and sh_info fields may have been set already by
3308 copy_private_section_data. */
3310 this_hdr
->bfd_section
= asect
;
3311 this_hdr
->contents
= NULL
;
3313 /* If the section type is unspecified, we set it based on
3315 if ((asect
->flags
& SEC_GROUP
) != 0)
3316 sh_type
= SHT_GROUP
;
3318 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3320 if (this_hdr
->sh_type
== SHT_NULL
)
3321 this_hdr
->sh_type
= sh_type
;
3322 else if (this_hdr
->sh_type
== SHT_NOBITS
3323 && sh_type
== SHT_PROGBITS
3324 && (asect
->flags
& SEC_ALLOC
) != 0)
3326 /* Warn if we are changing a NOBITS section to PROGBITS, but
3327 allow the link to proceed. This can happen when users link
3328 non-bss input sections to bss output sections, or emit data
3329 to a bss output section via a linker script. */
3331 (_("warning: section `%pA' type changed to PROGBITS"), asect
);
3332 this_hdr
->sh_type
= sh_type
;
3335 switch (this_hdr
->sh_type
)
3346 case SHT_INIT_ARRAY
:
3347 case SHT_FINI_ARRAY
:
3348 case SHT_PREINIT_ARRAY
:
3349 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3353 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3357 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3361 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3365 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3366 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3370 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3371 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3374 case SHT_GNU_versym
:
3375 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3378 case SHT_GNU_verdef
:
3379 this_hdr
->sh_entsize
= 0;
3380 /* objcopy or strip will copy over sh_info, but may not set
3381 cverdefs. The linker will set cverdefs, but sh_info will be
3383 if (this_hdr
->sh_info
== 0)
3384 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3386 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3387 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3390 case SHT_GNU_verneed
:
3391 this_hdr
->sh_entsize
= 0;
3392 /* objcopy or strip will copy over sh_info, but may not set
3393 cverrefs. The linker will set cverrefs, but sh_info will be
3395 if (this_hdr
->sh_info
== 0)
3396 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3398 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3399 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3403 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3407 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3411 if ((asect
->flags
& SEC_ALLOC
) != 0)
3412 this_hdr
->sh_flags
|= SHF_ALLOC
;
3413 if ((asect
->flags
& SEC_READONLY
) == 0)
3414 this_hdr
->sh_flags
|= SHF_WRITE
;
3415 if ((asect
->flags
& SEC_CODE
) != 0)
3416 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3417 if ((asect
->flags
& SEC_MERGE
) != 0)
3419 this_hdr
->sh_flags
|= SHF_MERGE
;
3420 this_hdr
->sh_entsize
= asect
->entsize
;
3422 if ((asect
->flags
& SEC_STRINGS
) != 0)
3423 this_hdr
->sh_flags
|= SHF_STRINGS
;
3424 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3425 this_hdr
->sh_flags
|= SHF_GROUP
;
3426 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3428 this_hdr
->sh_flags
|= SHF_TLS
;
3429 if (asect
->size
== 0
3430 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3432 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3434 this_hdr
->sh_size
= 0;
3437 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3438 if (this_hdr
->sh_size
!= 0)
3439 this_hdr
->sh_type
= SHT_NOBITS
;
3443 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3444 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3446 /* If the section has relocs, set up a section header for the
3447 SHT_REL[A] section. If two relocation sections are required for
3448 this section, it is up to the processor-specific back-end to
3449 create the other. */
3450 if ((asect
->flags
& SEC_RELOC
) != 0)
3452 /* When doing a relocatable link, create both REL and RELA sections if
3455 /* Do the normal setup if we wouldn't create any sections here. */
3456 && esd
->rel
.count
+ esd
->rela
.count
> 0
3457 && (bfd_link_relocatable (arg
->link_info
)
3458 || arg
->link_info
->emitrelocations
))
3460 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3461 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3462 FALSE
, delay_st_name_p
))
3467 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3468 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3469 TRUE
, delay_st_name_p
))
3475 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3477 ? &esd
->rela
: &esd
->rel
),
3487 /* Check for processor-specific section types. */
3488 sh_type
= this_hdr
->sh_type
;
3489 if (bed
->elf_backend_fake_sections
3490 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3496 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3498 /* Don't change the header type from NOBITS if we are being
3499 called for objcopy --only-keep-debug. */
3500 this_hdr
->sh_type
= sh_type
;
3504 /* Fill in the contents of a SHT_GROUP section. Called from
3505 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3506 when ELF targets use the generic linker, ld. Called for ld -r
3507 from bfd_elf_final_link. */
3510 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3512 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3513 asection
*elt
, *first
;
3517 /* Ignore linker created group section. See elfNN_ia64_object_p in
3519 if ((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
3524 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3526 unsigned long symindx
= 0;
3528 /* elf_group_id will have been set up by objcopy and the
3530 if (elf_group_id (sec
) != NULL
)
3531 symindx
= elf_group_id (sec
)->udata
.i
;
3535 /* If called from the assembler, swap_out_syms will have set up
3537 PR 25699: A corrupt input file could contain bogus group info. */
3538 if (elf_section_syms (abfd
) == NULL
)
3543 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3545 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3547 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3549 /* The ELF backend linker sets sh_info to -2 when the group
3550 signature symbol is global, and thus the index can't be
3551 set until all local symbols are output. */
3553 struct bfd_elf_section_data
*sec_data
;
3554 unsigned long symndx
;
3555 unsigned long extsymoff
;
3556 struct elf_link_hash_entry
*h
;
3558 /* The point of this little dance to the first SHF_GROUP section
3559 then back to the SHT_GROUP section is that this gets us to
3560 the SHT_GROUP in the input object. */
3561 igroup
= elf_sec_group (elf_next_in_group (sec
));
3562 sec_data
= elf_section_data (igroup
);
3563 symndx
= sec_data
->this_hdr
.sh_info
;
3565 if (!elf_bad_symtab (igroup
->owner
))
3567 Elf_Internal_Shdr
*symtab_hdr
;
3569 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3570 extsymoff
= symtab_hdr
->sh_info
;
3572 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3573 while (h
->root
.type
== bfd_link_hash_indirect
3574 || h
->root
.type
== bfd_link_hash_warning
)
3575 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3577 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3580 /* The contents won't be allocated for "ld -r" or objcopy. */
3582 if (sec
->contents
== NULL
)
3585 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3587 /* Arrange for the section to be written out. */
3588 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3589 if (sec
->contents
== NULL
)
3596 loc
= sec
->contents
+ sec
->size
;
3598 /* Get the pointer to the first section in the group that gas
3599 squirreled away here. objcopy arranges for this to be set to the
3600 start of the input section group. */
3601 first
= elt
= elf_next_in_group (sec
);
3603 /* First element is a flag word. Rest of section is elf section
3604 indices for all the sections of the group. Write them backwards
3605 just to keep the group in the same order as given in .section
3606 directives, not that it matters. */
3613 s
= s
->output_section
;
3615 && !bfd_is_abs_section (s
))
3617 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3618 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3620 if (elf_sec
->rel
.hdr
!= NULL
3622 || (input_elf_sec
->rel
.hdr
!= NULL
3623 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3625 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3627 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3629 if (elf_sec
->rela
.hdr
!= NULL
3631 || (input_elf_sec
->rela
.hdr
!= NULL
3632 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3634 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3636 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3639 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3641 elt
= elf_next_in_group (elt
);
3647 BFD_ASSERT (loc
== sec
->contents
);
3649 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3652 /* Given NAME, the name of a relocation section stripped of its
3653 .rel/.rela prefix, return the section in ABFD to which the
3654 relocations apply. */
3657 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3659 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3660 section likely apply to .got.plt or .got section. */
3661 if (get_elf_backend_data (abfd
)->want_got_plt
3662 && strcmp (name
, ".plt") == 0)
3667 sec
= bfd_get_section_by_name (abfd
, name
);
3673 return bfd_get_section_by_name (abfd
, name
);
3676 /* Return the section to which RELOC_SEC applies. */
3679 elf_get_reloc_section (asection
*reloc_sec
)
3684 const struct elf_backend_data
*bed
;
3686 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3687 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3690 /* We look up the section the relocs apply to by name. */
3691 name
= reloc_sec
->name
;
3692 if (strncmp (name
, ".rel", 4) != 0)
3695 if (type
== SHT_RELA
&& *name
++ != 'a')
3698 abfd
= reloc_sec
->owner
;
3699 bed
= get_elf_backend_data (abfd
);
3700 return bed
->get_reloc_section (abfd
, name
);
3703 /* Assign all ELF section numbers. The dummy first section is handled here
3704 too. The link/info pointers for the standard section types are filled
3705 in here too, while we're at it. LINK_INFO will be 0 when arriving
3706 here for objcopy, and when using the generic ELF linker. */
3709 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3711 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3713 unsigned int section_number
;
3714 Elf_Internal_Shdr
**i_shdrp
;
3715 struct bfd_elf_section_data
*d
;
3716 bfd_boolean need_symtab
;
3721 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3723 /* SHT_GROUP sections are in relocatable files only. */
3724 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3726 size_t reloc_count
= 0;
3728 /* Put SHT_GROUP sections first. */
3729 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3731 d
= elf_section_data (sec
);
3733 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3735 if (sec
->flags
& SEC_LINKER_CREATED
)
3737 /* Remove the linker created SHT_GROUP sections. */
3738 bfd_section_list_remove (abfd
, sec
);
3739 abfd
->section_count
--;
3742 d
->this_idx
= section_number
++;
3745 /* Count relocations. */
3746 reloc_count
+= sec
->reloc_count
;
3749 /* Clear HAS_RELOC if there are no relocations. */
3750 if (reloc_count
== 0)
3751 abfd
->flags
&= ~HAS_RELOC
;
3754 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3756 d
= elf_section_data (sec
);
3758 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3759 d
->this_idx
= section_number
++;
3760 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3761 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3764 d
->rel
.idx
= section_number
++;
3765 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3766 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3773 d
->rela
.idx
= section_number
++;
3774 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3775 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3781 need_symtab
= (bfd_get_symcount (abfd
) > 0
3782 || (link_info
== NULL
3783 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3787 elf_onesymtab (abfd
) = section_number
++;
3788 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3789 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3791 elf_section_list
*entry
;
3793 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3795 entry
= bfd_zalloc (abfd
, sizeof (*entry
));
3796 entry
->ndx
= section_number
++;
3797 elf_symtab_shndx_list (abfd
) = entry
;
3799 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3800 ".symtab_shndx", FALSE
);
3801 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3804 elf_strtab_sec (abfd
) = section_number
++;
3805 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3808 elf_shstrtab_sec (abfd
) = section_number
++;
3809 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3810 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3812 if (section_number
>= SHN_LORESERVE
)
3814 /* xgettext:c-format */
3815 _bfd_error_handler (_("%pB: too many sections: %u"),
3816 abfd
, section_number
);
3820 elf_numsections (abfd
) = section_number
;
3821 elf_elfheader (abfd
)->e_shnum
= section_number
;
3823 /* Set up the list of section header pointers, in agreement with the
3825 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
3826 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
3827 if (i_shdrp
== NULL
)
3830 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3831 sizeof (Elf_Internal_Shdr
));
3832 if (i_shdrp
[0] == NULL
)
3834 bfd_release (abfd
, i_shdrp
);
3838 elf_elfsections (abfd
) = i_shdrp
;
3840 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3843 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3844 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3846 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3847 BFD_ASSERT (entry
!= NULL
);
3848 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3849 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3851 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3852 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3855 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3859 d
= elf_section_data (sec
);
3861 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3862 if (d
->rel
.idx
!= 0)
3863 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3864 if (d
->rela
.idx
!= 0)
3865 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3867 /* Fill in the sh_link and sh_info fields while we're at it. */
3869 /* sh_link of a reloc section is the section index of the symbol
3870 table. sh_info is the section index of the section to which
3871 the relocation entries apply. */
3872 if (d
->rel
.idx
!= 0)
3874 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3875 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3876 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3878 if (d
->rela
.idx
!= 0)
3880 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3881 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3882 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3885 /* We need to set up sh_link for SHF_LINK_ORDER. */
3886 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3888 s
= elf_linked_to_section (sec
);
3889 /* We can now have a NULL linked section pointer.
3890 This happens when the sh_link field is 0, which is done
3891 when a linked to section is discarded but the linking
3892 section has been retained for some reason. */
3895 /* Check discarded linkonce section. */
3896 if (discarded_section (s
))
3900 /* xgettext:c-format */
3901 (_("%pB: sh_link of section `%pA' points to"
3902 " discarded section `%pA' of `%pB'"),
3903 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3904 /* Point to the kept section if it has the same
3905 size as the discarded one. */
3906 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3909 bfd_set_error (bfd_error_bad_value
);
3914 /* Handle objcopy. */
3915 else if (s
->output_section
== NULL
)
3918 /* xgettext:c-format */
3919 (_("%pB: sh_link of section `%pA' points to"
3920 " removed section `%pA' of `%pB'"),
3921 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3922 bfd_set_error (bfd_error_bad_value
);
3925 s
= s
->output_section
;
3926 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3930 switch (d
->this_hdr
.sh_type
)
3934 /* A reloc section which we are treating as a normal BFD
3935 section. sh_link is the section index of the symbol
3936 table. sh_info is the section index of the section to
3937 which the relocation entries apply. We assume that an
3938 allocated reloc section uses the dynamic symbol table.
3939 FIXME: How can we be sure? */
3940 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3942 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3944 s
= elf_get_reloc_section (sec
);
3947 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3948 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3953 /* We assume that a section named .stab*str is a stabs
3954 string section. We look for a section with the same name
3955 but without the trailing ``str'', and set its sh_link
3956 field to point to this section. */
3957 if (CONST_STRNEQ (sec
->name
, ".stab")
3958 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3963 len
= strlen (sec
->name
);
3964 alc
= (char *) bfd_malloc (len
- 2);
3967 memcpy (alc
, sec
->name
, len
- 3);
3968 alc
[len
- 3] = '\0';
3969 s
= bfd_get_section_by_name (abfd
, alc
);
3973 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3975 /* This is a .stab section. */
3976 elf_section_data (s
)->this_hdr
.sh_entsize
= 12;
3983 case SHT_GNU_verneed
:
3984 case SHT_GNU_verdef
:
3985 /* sh_link is the section header index of the string table
3986 used for the dynamic entries, or the symbol table, or the
3988 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3990 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3993 case SHT_GNU_LIBLIST
:
3994 /* sh_link is the section header index of the prelink library
3995 list used for the dynamic entries, or the symbol table, or
3996 the version strings. */
3997 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3998 ? ".dynstr" : ".gnu.libstr");
4000 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4005 case SHT_GNU_versym
:
4006 /* sh_link is the section header index of the symbol table
4007 this hash table or version table is for. */
4008 s
= bfd_get_section_by_name (abfd
, ".dynsym");
4010 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
4014 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
4018 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4019 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4020 debug section name from .debug_* to .zdebug_* if needed. */
4026 sym_is_global (bfd
*abfd
, asymbol
*sym
)
4028 /* If the backend has a special mapping, use it. */
4029 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4030 if (bed
->elf_backend_sym_is_global
)
4031 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
4033 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
4034 || bfd_is_und_section (bfd_asymbol_section (sym
))
4035 || bfd_is_com_section (bfd_asymbol_section (sym
)));
4038 /* Filter global symbols of ABFD to include in the import library. All
4039 SYMCOUNT symbols of ABFD can be examined from their pointers in
4040 SYMS. Pointers of symbols to keep should be stored contiguously at
4041 the beginning of that array.
4043 Returns the number of symbols to keep. */
4046 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
4047 asymbol
**syms
, long symcount
)
4049 long src_count
, dst_count
= 0;
4051 for (src_count
= 0; src_count
< symcount
; src_count
++)
4053 asymbol
*sym
= syms
[src_count
];
4054 char *name
= (char *) bfd_asymbol_name (sym
);
4055 struct bfd_link_hash_entry
*h
;
4057 if (!sym_is_global (abfd
, sym
))
4060 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4063 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4065 if (h
->linker_def
|| h
->ldscript_def
)
4068 syms
[dst_count
++] = sym
;
4071 syms
[dst_count
] = NULL
;
4076 /* Don't output section symbols for sections that are not going to be
4077 output, that are duplicates or there is no BFD section. */
4080 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4082 elf_symbol_type
*type_ptr
;
4087 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4090 if (sym
->section
== NULL
)
4093 type_ptr
= elf_symbol_from (sym
);
4094 return ((type_ptr
!= NULL
4095 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4096 && bfd_is_abs_section (sym
->section
))
4097 || !(sym
->section
->owner
== abfd
4098 || (sym
->section
->output_section
!= NULL
4099 && sym
->section
->output_section
->owner
== abfd
4100 && sym
->section
->output_offset
== 0)
4101 || bfd_is_abs_section (sym
->section
)));
4104 /* Map symbol from it's internal number to the external number, moving
4105 all local symbols to be at the head of the list. */
4108 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4110 unsigned int symcount
= bfd_get_symcount (abfd
);
4111 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4112 asymbol
**sect_syms
;
4113 unsigned int num_locals
= 0;
4114 unsigned int num_globals
= 0;
4115 unsigned int num_locals2
= 0;
4116 unsigned int num_globals2
= 0;
4117 unsigned int max_index
= 0;
4124 fprintf (stderr
, "elf_map_symbols\n");
4128 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4130 if (max_index
< asect
->index
)
4131 max_index
= asect
->index
;
4135 amt
= max_index
* sizeof (asymbol
*);
4136 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
4137 if (sect_syms
== NULL
)
4139 elf_section_syms (abfd
) = sect_syms
;
4140 elf_num_section_syms (abfd
) = max_index
;
4142 /* Init sect_syms entries for any section symbols we have already
4143 decided to output. */
4144 for (idx
= 0; idx
< symcount
; idx
++)
4146 asymbol
*sym
= syms
[idx
];
4148 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4150 && !ignore_section_sym (abfd
, sym
)
4151 && !bfd_is_abs_section (sym
->section
))
4153 asection
*sec
= sym
->section
;
4155 if (sec
->owner
!= abfd
)
4156 sec
= sec
->output_section
;
4158 sect_syms
[sec
->index
] = syms
[idx
];
4162 /* Classify all of the symbols. */
4163 for (idx
= 0; idx
< symcount
; idx
++)
4165 if (sym_is_global (abfd
, syms
[idx
]))
4167 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4171 /* We will be adding a section symbol for each normal BFD section. Most
4172 sections will already have a section symbol in outsymbols, but
4173 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4174 at least in that case. */
4175 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4177 if (sect_syms
[asect
->index
] == NULL
)
4179 if (!sym_is_global (abfd
, asect
->symbol
))
4186 /* Now sort the symbols so the local symbols are first. */
4187 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
4188 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
4189 if (new_syms
== NULL
)
4192 for (idx
= 0; idx
< symcount
; idx
++)
4194 asymbol
*sym
= syms
[idx
];
4197 if (sym_is_global (abfd
, sym
))
4198 i
= num_locals
+ num_globals2
++;
4199 else if (!ignore_section_sym (abfd
, sym
))
4204 sym
->udata
.i
= i
+ 1;
4206 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4208 if (sect_syms
[asect
->index
] == NULL
)
4210 asymbol
*sym
= asect
->symbol
;
4213 sect_syms
[asect
->index
] = sym
;
4214 if (!sym_is_global (abfd
, sym
))
4217 i
= num_locals
+ num_globals2
++;
4219 sym
->udata
.i
= i
+ 1;
4223 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4225 *pnum_locals
= num_locals
;
4229 /* Align to the maximum file alignment that could be required for any
4230 ELF data structure. */
4232 static inline file_ptr
4233 align_file_position (file_ptr off
, int align
)
4235 return (off
+ align
- 1) & ~(align
- 1);
4238 /* Assign a file position to a section, optionally aligning to the
4239 required section alignment. */
4242 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4246 if (align
&& i_shdrp
->sh_addralign
> 1)
4247 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4248 i_shdrp
->sh_offset
= offset
;
4249 if (i_shdrp
->bfd_section
!= NULL
)
4250 i_shdrp
->bfd_section
->filepos
= offset
;
4251 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4252 offset
+= i_shdrp
->sh_size
;
4256 /* Compute the file positions we are going to put the sections at, and
4257 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4258 is not NULL, this is being called by the ELF backend linker. */
4261 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4262 struct bfd_link_info
*link_info
)
4264 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4265 struct fake_section_arg fsargs
;
4267 struct elf_strtab_hash
*strtab
= NULL
;
4268 Elf_Internal_Shdr
*shstrtab_hdr
;
4269 bfd_boolean need_symtab
;
4271 if (abfd
->output_has_begun
)
4274 /* Do any elf backend specific processing first. */
4275 if (bed
->elf_backend_begin_write_processing
)
4276 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4278 if (!(*bed
->elf_backend_init_file_header
) (abfd
, link_info
))
4281 fsargs
.failed
= FALSE
;
4282 fsargs
.link_info
= link_info
;
4283 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4287 if (!assign_section_numbers (abfd
, link_info
))
4290 /* The backend linker builds symbol table information itself. */
4291 need_symtab
= (link_info
== NULL
4292 && (bfd_get_symcount (abfd
) > 0
4293 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4297 /* Non-zero if doing a relocatable link. */
4298 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4300 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4305 if (link_info
== NULL
)
4307 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4312 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4313 /* sh_name was set in init_file_header. */
4314 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4315 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4316 shstrtab_hdr
->sh_addr
= 0;
4317 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4318 shstrtab_hdr
->sh_entsize
= 0;
4319 shstrtab_hdr
->sh_link
= 0;
4320 shstrtab_hdr
->sh_info
= 0;
4321 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4322 shstrtab_hdr
->sh_addralign
= 1;
4324 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4330 Elf_Internal_Shdr
*hdr
;
4332 off
= elf_next_file_pos (abfd
);
4334 hdr
= & elf_symtab_hdr (abfd
);
4335 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4337 if (elf_symtab_shndx_list (abfd
) != NULL
)
4339 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4340 if (hdr
->sh_size
!= 0)
4341 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4342 /* FIXME: What about other symtab_shndx sections in the list ? */
4345 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4346 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4348 elf_next_file_pos (abfd
) = off
;
4350 /* Now that we know where the .strtab section goes, write it
4352 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4353 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4355 _bfd_elf_strtab_free (strtab
);
4358 abfd
->output_has_begun
= TRUE
;
4363 /* Make an initial estimate of the size of the program header. If we
4364 get the number wrong here, we'll redo section placement. */
4366 static bfd_size_type
4367 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4371 const struct elf_backend_data
*bed
;
4373 /* Assume we will need exactly two PT_LOAD segments: one for text
4374 and one for data. */
4377 s
= bfd_get_section_by_name (abfd
, ".interp");
4378 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4380 /* If we have a loadable interpreter section, we need a
4381 PT_INTERP segment. In this case, assume we also need a
4382 PT_PHDR segment, although that may not be true for all
4387 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4389 /* We need a PT_DYNAMIC segment. */
4393 if (info
!= NULL
&& info
->relro
)
4395 /* We need a PT_GNU_RELRO segment. */
4399 if (elf_eh_frame_hdr (abfd
))
4401 /* We need a PT_GNU_EH_FRAME segment. */
4405 if (elf_stack_flags (abfd
))
4407 /* We need a PT_GNU_STACK segment. */
4411 s
= bfd_get_section_by_name (abfd
,
4412 NOTE_GNU_PROPERTY_SECTION_NAME
);
4413 if (s
!= NULL
&& s
->size
!= 0)
4415 /* We need a PT_GNU_PROPERTY segment. */
4419 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4421 if ((s
->flags
& SEC_LOAD
) != 0
4422 && elf_section_type (s
) == SHT_NOTE
)
4424 unsigned int alignment_power
;
4425 /* We need a PT_NOTE segment. */
4427 /* Try to create just one PT_NOTE segment for all adjacent
4428 loadable SHT_NOTE sections. gABI requires that within a
4429 PT_NOTE segment (and also inside of each SHT_NOTE section)
4430 each note should have the same alignment. So we check
4431 whether the sections are correctly aligned. */
4432 alignment_power
= s
->alignment_power
;
4433 while (s
->next
!= NULL
4434 && s
->next
->alignment_power
== alignment_power
4435 && (s
->next
->flags
& SEC_LOAD
) != 0
4436 && elf_section_type (s
->next
) == SHT_NOTE
)
4441 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4443 if (s
->flags
& SEC_THREAD_LOCAL
)
4445 /* We need a PT_TLS segment. */
4451 bed
= get_elf_backend_data (abfd
);
4453 if ((abfd
->flags
& D_PAGED
) != 0
4454 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
4456 /* Add a PT_GNU_MBIND segment for each mbind section. */
4457 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4458 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4459 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4461 if (elf_section_data (s
)->this_hdr
.sh_info
> PT_GNU_MBIND_NUM
)
4464 /* xgettext:c-format */
4465 (_("%pB: GNU_MBIND section `%pA' has invalid "
4466 "sh_info field: %d"),
4467 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4470 /* Align mbind section to page size. */
4471 if (s
->alignment_power
< page_align_power
)
4472 s
->alignment_power
= page_align_power
;
4477 /* Let the backend count up any program headers it might need. */
4478 if (bed
->elf_backend_additional_program_headers
)
4482 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4488 return segs
* bed
->s
->sizeof_phdr
;
4491 /* Find the segment that contains the output_section of section. */
4494 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4496 struct elf_segment_map
*m
;
4497 Elf_Internal_Phdr
*p
;
4499 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4505 for (i
= m
->count
- 1; i
>= 0; i
--)
4506 if (m
->sections
[i
] == section
)
4513 /* Create a mapping from a set of sections to a program segment. */
4515 static struct elf_segment_map
*
4516 make_mapping (bfd
*abfd
,
4517 asection
**sections
,
4522 struct elf_segment_map
*m
;
4527 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
4528 amt
+= (to
- from
) * sizeof (asection
*);
4529 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4533 m
->p_type
= PT_LOAD
;
4534 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4535 m
->sections
[i
- from
] = *hdrpp
;
4536 m
->count
= to
- from
;
4538 if (from
== 0 && phdr
)
4540 /* Include the headers in the first PT_LOAD segment. */
4541 m
->includes_filehdr
= 1;
4542 m
->includes_phdrs
= 1;
4548 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4551 struct elf_segment_map
*
4552 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4554 struct elf_segment_map
*m
;
4556 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4557 sizeof (struct elf_segment_map
));
4561 m
->p_type
= PT_DYNAMIC
;
4563 m
->sections
[0] = dynsec
;
4568 /* Possibly add or remove segments from the segment map. */
4571 elf_modify_segment_map (bfd
*abfd
,
4572 struct bfd_link_info
*info
,
4573 bfd_boolean remove_empty_load
)
4575 struct elf_segment_map
**m
;
4576 const struct elf_backend_data
*bed
;
4578 /* The placement algorithm assumes that non allocated sections are
4579 not in PT_LOAD segments. We ensure this here by removing such
4580 sections from the segment map. We also remove excluded
4581 sections. Finally, any PT_LOAD segment without sections is
4583 m
= &elf_seg_map (abfd
);
4586 unsigned int i
, new_count
;
4588 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4590 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4591 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4592 || (*m
)->p_type
!= PT_LOAD
))
4594 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4598 (*m
)->count
= new_count
;
4600 if (remove_empty_load
4601 && (*m
)->p_type
== PT_LOAD
4603 && !(*m
)->includes_phdrs
)
4609 bed
= get_elf_backend_data (abfd
);
4610 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4612 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4619 #define IS_TBSS(s) \
4620 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
4622 /* Set up a mapping from BFD sections to program segments. */
4625 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4628 struct elf_segment_map
*m
;
4629 asection
**sections
= NULL
;
4630 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4631 bfd_boolean no_user_phdrs
;
4633 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4636 info
->user_phdrs
= !no_user_phdrs
;
4638 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4642 struct elf_segment_map
*mfirst
;
4643 struct elf_segment_map
**pm
;
4646 unsigned int hdr_index
;
4647 bfd_vma maxpagesize
;
4649 bfd_boolean phdr_in_segment
;
4650 bfd_boolean writable
;
4651 bfd_boolean executable
;
4652 unsigned int tls_count
= 0;
4653 asection
*first_tls
= NULL
;
4654 asection
*first_mbind
= NULL
;
4655 asection
*dynsec
, *eh_frame_hdr
;
4657 bfd_vma addr_mask
, wrap_to
= 0; /* Bytes. */
4658 bfd_size_type phdr_size
; /* Octets/bytes. */
4659 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
4661 /* Select the allocated sections, and sort them. */
4663 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
4664 sections
= (asection
**) bfd_malloc (amt
);
4665 if (sections
== NULL
)
4668 /* Calculate top address, avoiding undefined behaviour of shift
4669 left operator when shift count is equal to size of type
4671 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4672 addr_mask
= (addr_mask
<< 1) + 1;
4675 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4677 if ((s
->flags
& SEC_ALLOC
) != 0)
4679 /* target_index is unused until bfd_elf_final_link
4680 starts output of section symbols. Use it to make
4682 s
->target_index
= i
;
4685 /* A wrapping section potentially clashes with header. */
4686 if (((s
->lma
+ s
->size
/ opb
) & addr_mask
) < (s
->lma
& addr_mask
))
4687 wrap_to
= (s
->lma
+ s
->size
/ opb
) & addr_mask
;
4690 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4693 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4695 phdr_size
= elf_program_header_size (abfd
);
4696 if (phdr_size
== (bfd_size_type
) -1)
4697 phdr_size
= get_program_header_size (abfd
, info
);
4698 phdr_size
+= bed
->s
->sizeof_ehdr
;
4699 /* phdr_size is compared to LMA values which are in bytes. */
4701 maxpagesize
= bed
->maxpagesize
;
4702 if (maxpagesize
== 0)
4704 phdr_in_segment
= info
!= NULL
&& info
->load_phdrs
;
4706 && (((sections
[0]->lma
& addr_mask
) & (maxpagesize
- 1))
4707 >= (phdr_size
& (maxpagesize
- 1))))
4708 /* For compatibility with old scripts that may not be using
4709 SIZEOF_HEADERS, add headers when it looks like space has
4710 been left for them. */
4711 phdr_in_segment
= TRUE
;
4713 /* Build the mapping. */
4717 /* If we have a .interp section, then create a PT_PHDR segment for
4718 the program headers and a PT_INTERP segment for the .interp
4720 s
= bfd_get_section_by_name (abfd
, ".interp");
4721 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0 && s
->size
!= 0)
4723 amt
= sizeof (struct elf_segment_map
);
4724 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4728 m
->p_type
= PT_PHDR
;
4730 m
->p_flags_valid
= 1;
4731 m
->includes_phdrs
= 1;
4732 phdr_in_segment
= TRUE
;
4736 amt
= sizeof (struct elf_segment_map
);
4737 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4741 m
->p_type
= PT_INTERP
;
4749 /* Look through the sections. We put sections in the same program
4750 segment when the start of the second section can be placed within
4751 a few bytes of the end of the first section. */
4757 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4759 && (dynsec
->flags
& SEC_LOAD
) == 0)
4762 if ((abfd
->flags
& D_PAGED
) == 0)
4763 phdr_in_segment
= FALSE
;
4765 /* Deal with -Ttext or something similar such that the first section
4766 is not adjacent to the program headers. This is an
4767 approximation, since at this point we don't know exactly how many
4768 program headers we will need. */
4769 if (phdr_in_segment
&& count
> 0)
4771 bfd_vma phdr_lma
; /* Bytes. */
4772 bfd_boolean separate_phdr
= FALSE
;
4774 phdr_lma
= (sections
[0]->lma
- phdr_size
) & addr_mask
& -maxpagesize
;
4776 && info
->separate_code
4777 && (sections
[0]->flags
& SEC_CODE
) != 0)
4779 /* If data sections should be separate from code and
4780 thus not executable, and the first section is
4781 executable then put the file and program headers in
4782 their own PT_LOAD. */
4783 separate_phdr
= TRUE
;
4784 if ((((phdr_lma
+ phdr_size
- 1) & addr_mask
& -maxpagesize
)
4785 == (sections
[0]->lma
& addr_mask
& -maxpagesize
)))
4787 /* The file and program headers are currently on the
4788 same page as the first section. Put them on the
4789 previous page if we can. */
4790 if (phdr_lma
>= maxpagesize
)
4791 phdr_lma
-= maxpagesize
;
4793 separate_phdr
= FALSE
;
4796 if ((sections
[0]->lma
& addr_mask
) < phdr_lma
4797 || (sections
[0]->lma
& addr_mask
) < phdr_size
)
4798 /* If file and program headers would be placed at the end
4799 of memory then it's probably better to omit them. */
4800 phdr_in_segment
= FALSE
;
4801 else if (phdr_lma
< wrap_to
)
4802 /* If a section wraps around to where we'll be placing
4803 file and program headers, then the headers will be
4805 phdr_in_segment
= FALSE
;
4806 else if (separate_phdr
)
4808 m
= make_mapping (abfd
, sections
, 0, 0, phdr_in_segment
);
4811 m
->p_paddr
= phdr_lma
* opb
;
4813 = (sections
[0]->vma
- phdr_size
) & addr_mask
& -maxpagesize
;
4814 m
->p_paddr_valid
= 1;
4817 phdr_in_segment
= FALSE
;
4821 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4824 bfd_boolean new_segment
;
4828 /* See if this section and the last one will fit in the same
4831 if (last_hdr
== NULL
)
4833 /* If we don't have a segment yet, then we don't need a new
4834 one (we build the last one after this loop). */
4835 new_segment
= FALSE
;
4837 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4839 /* If this section has a different relation between the
4840 virtual address and the load address, then we need a new
4844 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4845 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4847 /* If this section has a load address that makes it overlap
4848 the previous section, then we need a new segment. */
4851 else if ((abfd
->flags
& D_PAGED
) != 0
4852 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4853 == (hdr
->lma
& -maxpagesize
)))
4855 /* If we are demand paged then we can't map two disk
4856 pages onto the same memory page. */
4857 new_segment
= FALSE
;
4859 /* In the next test we have to be careful when last_hdr->lma is close
4860 to the end of the address space. If the aligned address wraps
4861 around to the start of the address space, then there are no more
4862 pages left in memory and it is OK to assume that the current
4863 section can be included in the current segment. */
4864 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4865 + maxpagesize
> last_hdr
->lma
)
4866 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
)
4867 + maxpagesize
<= hdr
->lma
))
4869 /* If putting this section in this segment would force us to
4870 skip a page in the segment, then we need a new segment. */
4873 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4874 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4876 /* We don't want to put a loaded section after a
4877 nonloaded (ie. bss style) section in the same segment
4878 as that will force the non-loaded section to be loaded.
4879 Consider .tbss sections as loaded for this purpose. */
4882 else if ((abfd
->flags
& D_PAGED
) == 0)
4884 /* If the file is not demand paged, which means that we
4885 don't require the sections to be correctly aligned in the
4886 file, then there is no other reason for a new segment. */
4887 new_segment
= FALSE
;
4889 else if (info
!= NULL
4890 && info
->separate_code
4891 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4896 && (hdr
->flags
& SEC_READONLY
) == 0)
4898 /* We don't want to put a writable section in a read only
4904 /* Otherwise, we can use the same segment. */
4905 new_segment
= FALSE
;
4908 /* Allow interested parties a chance to override our decision. */
4909 if (last_hdr
!= NULL
4911 && info
->callbacks
->override_segment_assignment
!= NULL
)
4913 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4919 if ((hdr
->flags
& SEC_READONLY
) == 0)
4921 if ((hdr
->flags
& SEC_CODE
) != 0)
4924 /* .tbss sections effectively have zero size. */
4925 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4929 /* We need a new program segment. We must create a new program
4930 header holding all the sections from hdr_index until hdr. */
4932 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4939 if ((hdr
->flags
& SEC_READONLY
) == 0)
4944 if ((hdr
->flags
& SEC_CODE
) == 0)
4950 /* .tbss sections effectively have zero size. */
4951 last_size
= (!IS_TBSS (hdr
) ? hdr
->size
: 0) / opb
;
4953 phdr_in_segment
= FALSE
;
4956 /* Create a final PT_LOAD program segment, but not if it's just
4958 if (last_hdr
!= NULL
4959 && (i
- hdr_index
!= 1
4960 || !IS_TBSS (last_hdr
)))
4962 m
= make_mapping (abfd
, sections
, hdr_index
, i
, phdr_in_segment
);
4970 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4973 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4980 /* For each batch of consecutive loadable SHT_NOTE sections,
4981 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4982 because if we link together nonloadable .note sections and
4983 loadable .note sections, we will generate two .note sections
4984 in the output file. */
4985 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4987 if ((s
->flags
& SEC_LOAD
) != 0
4988 && elf_section_type (s
) == SHT_NOTE
)
4991 unsigned int alignment_power
= s
->alignment_power
;
4994 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4996 if (s2
->next
->alignment_power
== alignment_power
4997 && (s2
->next
->flags
& SEC_LOAD
) != 0
4998 && elf_section_type (s2
->next
) == SHT_NOTE
4999 && align_power (s2
->lma
+ s2
->size
/ opb
,
5006 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5007 amt
+= count
* sizeof (asection
*);
5008 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5012 m
->p_type
= PT_NOTE
;
5016 m
->sections
[m
->count
- count
--] = s
;
5017 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5020 m
->sections
[m
->count
- 1] = s
;
5021 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
5025 if (s
->flags
& SEC_THREAD_LOCAL
)
5031 if (first_mbind
== NULL
5032 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
5036 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
5039 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
5040 amt
+= tls_count
* sizeof (asection
*);
5041 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5046 m
->count
= tls_count
;
5047 /* Mandated PF_R. */
5049 m
->p_flags_valid
= 1;
5051 for (i
= 0; i
< tls_count
; ++i
)
5053 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
5056 (_("%pB: TLS sections are not adjacent:"), abfd
);
5059 while (i
< tls_count
)
5061 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
5063 _bfd_error_handler (_(" TLS: %pA"), s
);
5067 _bfd_error_handler (_(" non-TLS: %pA"), s
);
5070 bfd_set_error (bfd_error_bad_value
);
5082 && (abfd
->flags
& D_PAGED
) != 0
5083 && (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0)
5084 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
5085 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
5086 && elf_section_data (s
)->this_hdr
.sh_info
<= PT_GNU_MBIND_NUM
)
5088 /* Mandated PF_R. */
5089 unsigned long p_flags
= PF_R
;
5090 if ((s
->flags
& SEC_READONLY
) == 0)
5092 if ((s
->flags
& SEC_CODE
) != 0)
5095 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5096 m
= bfd_zalloc (abfd
, amt
);
5100 m
->p_type
= (PT_GNU_MBIND_LO
5101 + elf_section_data (s
)->this_hdr
.sh_info
);
5103 m
->p_flags_valid
= 1;
5105 m
->p_flags
= p_flags
;
5111 s
= bfd_get_section_by_name (abfd
,
5112 NOTE_GNU_PROPERTY_SECTION_NAME
);
5113 if (s
!= NULL
&& s
->size
!= 0)
5115 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
5116 m
= bfd_zalloc (abfd
, amt
);
5120 m
->p_type
= PT_GNU_PROPERTY
;
5122 m
->p_flags_valid
= 1;
5129 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5131 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5132 if (eh_frame_hdr
!= NULL
5133 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5135 amt
= sizeof (struct elf_segment_map
);
5136 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5140 m
->p_type
= PT_GNU_EH_FRAME
;
5142 m
->sections
[0] = eh_frame_hdr
->output_section
;
5148 if (elf_stack_flags (abfd
))
5150 amt
= sizeof (struct elf_segment_map
);
5151 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5155 m
->p_type
= PT_GNU_STACK
;
5156 m
->p_flags
= elf_stack_flags (abfd
);
5157 m
->p_align
= bed
->stack_align
;
5158 m
->p_flags_valid
= 1;
5159 m
->p_align_valid
= m
->p_align
!= 0;
5160 if (info
->stacksize
> 0)
5162 m
->p_size
= info
->stacksize
;
5163 m
->p_size_valid
= 1;
5170 if (info
!= NULL
&& info
->relro
)
5172 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5174 if (m
->p_type
== PT_LOAD
5176 && m
->sections
[0]->vma
>= info
->relro_start
5177 && m
->sections
[0]->vma
< info
->relro_end
)
5180 while (--i
!= (unsigned) -1)
5182 if (m
->sections
[i
]->size
> 0
5183 && (m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5184 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5188 if (i
!= (unsigned) -1)
5193 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5196 amt
= sizeof (struct elf_segment_map
);
5197 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5201 m
->p_type
= PT_GNU_RELRO
;
5208 elf_seg_map (abfd
) = mfirst
;
5211 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5214 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5216 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5225 /* Sort sections by address. */
5228 elf_sort_sections (const void *arg1
, const void *arg2
)
5230 const asection
*sec1
= *(const asection
**) arg1
;
5231 const asection
*sec2
= *(const asection
**) arg2
;
5232 bfd_size_type size1
, size2
;
5234 /* Sort by LMA first, since this is the address used to
5235 place the section into a segment. */
5236 if (sec1
->lma
< sec2
->lma
)
5238 else if (sec1
->lma
> sec2
->lma
)
5241 /* Then sort by VMA. Normally the LMA and the VMA will be
5242 the same, and this will do nothing. */
5243 if (sec1
->vma
< sec2
->vma
)
5245 else if (sec1
->vma
> sec2
->vma
)
5248 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5250 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5257 else if (TOEND (sec2
))
5262 /* Sort by size, to put zero sized sections
5263 before others at the same address. */
5265 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5266 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5273 return sec1
->target_index
- sec2
->target_index
;
5276 /* This qsort comparison functions sorts PT_LOAD segments first and
5277 by p_paddr, for assign_file_positions_for_load_sections. */
5280 elf_sort_segments (const void *arg1
, const void *arg2
)
5282 const struct elf_segment_map
*m1
= *(const struct elf_segment_map
**) arg1
;
5283 const struct elf_segment_map
*m2
= *(const struct elf_segment_map
**) arg2
;
5285 if (m1
->p_type
!= m2
->p_type
)
5287 if (m1
->p_type
== PT_NULL
)
5289 if (m2
->p_type
== PT_NULL
)
5291 return m1
->p_type
< m2
->p_type
? -1 : 1;
5293 if (m1
->includes_filehdr
!= m2
->includes_filehdr
)
5294 return m1
->includes_filehdr
? -1 : 1;
5295 if (m1
->no_sort_lma
!= m2
->no_sort_lma
)
5296 return m1
->no_sort_lma
? -1 : 1;
5297 if (m1
->p_type
== PT_LOAD
&& !m1
->no_sort_lma
)
5299 bfd_vma lma1
, lma2
; /* Octets. */
5301 if (m1
->p_paddr_valid
)
5303 else if (m1
->count
!= 0)
5305 unsigned int opb
= bfd_octets_per_byte (m1
->sections
[0]->owner
,
5307 lma1
= (m1
->sections
[0]->lma
+ m1
->p_vaddr_offset
) * opb
;
5310 if (m2
->p_paddr_valid
)
5312 else if (m2
->count
!= 0)
5314 unsigned int opb
= bfd_octets_per_byte (m2
->sections
[0]->owner
,
5316 lma2
= (m2
->sections
[0]->lma
+ m2
->p_vaddr_offset
) * opb
;
5319 return lma1
< lma2
? -1 : 1;
5321 if (m1
->idx
!= m2
->idx
)
5322 return m1
->idx
< m2
->idx
? -1 : 1;
5326 /* Ian Lance Taylor writes:
5328 We shouldn't be using % with a negative signed number. That's just
5329 not good. We have to make sure either that the number is not
5330 negative, or that the number has an unsigned type. When the types
5331 are all the same size they wind up as unsigned. When file_ptr is a
5332 larger signed type, the arithmetic winds up as signed long long,
5335 What we're trying to say here is something like ``increase OFF by
5336 the least amount that will cause it to be equal to the VMA modulo
5338 /* In other words, something like:
5340 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5341 off_offset = off % bed->maxpagesize;
5342 if (vma_offset < off_offset)
5343 adjustment = vma_offset + bed->maxpagesize - off_offset;
5345 adjustment = vma_offset - off_offset;
5347 which can be collapsed into the expression below. */
5350 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5352 /* PR binutils/16199: Handle an alignment of zero. */
5353 if (maxpagesize
== 0)
5355 return ((vma
- off
) % maxpagesize
);
5359 print_segment_map (const struct elf_segment_map
*m
)
5362 const char *pt
= get_segment_type (m
->p_type
);
5367 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5368 sprintf (buf
, "LOPROC+%7.7x",
5369 (unsigned int) (m
->p_type
- PT_LOPROC
));
5370 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5371 sprintf (buf
, "LOOS+%7.7x",
5372 (unsigned int) (m
->p_type
- PT_LOOS
));
5374 snprintf (buf
, sizeof (buf
), "%8.8x",
5375 (unsigned int) m
->p_type
);
5379 fprintf (stderr
, "%s:", pt
);
5380 for (j
= 0; j
< m
->count
; j
++)
5381 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5387 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5392 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5394 buf
= bfd_zmalloc (len
);
5397 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5402 /* Assign file positions to the sections based on the mapping from
5403 sections to segments. This function also sets up some fields in
5407 assign_file_positions_for_load_sections (bfd
*abfd
,
5408 struct bfd_link_info
*link_info
)
5410 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5411 struct elf_segment_map
*m
;
5412 struct elf_segment_map
*phdr_load_seg
;
5413 Elf_Internal_Phdr
*phdrs
;
5414 Elf_Internal_Phdr
*p
;
5415 file_ptr off
; /* Octets. */
5416 bfd_size_type maxpagesize
;
5417 unsigned int alloc
, actual
;
5419 struct elf_segment_map
**sorted_seg_map
;
5420 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
5422 if (link_info
== NULL
5423 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5427 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5432 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5433 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5437 /* PR binutils/12467. */
5438 elf_elfheader (abfd
)->e_phoff
= 0;
5439 elf_elfheader (abfd
)->e_phentsize
= 0;
5442 elf_elfheader (abfd
)->e_phnum
= alloc
;
5444 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5447 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5451 actual
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5452 BFD_ASSERT (elf_program_header_size (abfd
)
5453 == actual
* bed
->s
->sizeof_phdr
);
5454 BFD_ASSERT (actual
>= alloc
);
5459 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5463 /* We're writing the size in elf_program_header_size (abfd),
5464 see assign_file_positions_except_relocs, so make sure we have
5465 that amount allocated, with trailing space cleared.
5466 The variable alloc contains the computed need, while
5467 elf_program_header_size (abfd) contains the size used for the
5469 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5470 where the layout is forced to according to a larger size in the
5471 last iterations for the testcase ld-elf/header. */
5472 phdrs
= bfd_zalloc (abfd
, (actual
* sizeof (*phdrs
)
5473 + alloc
* sizeof (*sorted_seg_map
)));
5474 sorted_seg_map
= (struct elf_segment_map
**) (phdrs
+ actual
);
5475 elf_tdata (abfd
)->phdr
= phdrs
;
5479 for (m
= elf_seg_map (abfd
), j
= 0; m
!= NULL
; m
= m
->next
, j
++)
5481 sorted_seg_map
[j
] = m
;
5482 /* If elf_segment_map is not from map_sections_to_segments, the
5483 sections may not be correctly ordered. NOTE: sorting should
5484 not be done to the PT_NOTE section of a corefile, which may
5485 contain several pseudo-sections artificially created by bfd.
5486 Sorting these pseudo-sections breaks things badly. */
5488 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5489 && m
->p_type
== PT_NOTE
))
5491 for (i
= 0; i
< m
->count
; i
++)
5492 m
->sections
[i
]->target_index
= i
;
5493 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5498 qsort (sorted_seg_map
, alloc
, sizeof (*sorted_seg_map
),
5502 if ((abfd
->flags
& D_PAGED
) != 0)
5503 maxpagesize
= bed
->maxpagesize
;
5505 /* Sections must map to file offsets past the ELF file header. */
5506 off
= bed
->s
->sizeof_ehdr
;
5507 /* And if one of the PT_LOAD headers doesn't include the program
5508 headers then we'll be mapping program headers in the usual
5509 position after the ELF file header. */
5510 phdr_load_seg
= NULL
;
5511 for (j
= 0; j
< alloc
; j
++)
5513 m
= sorted_seg_map
[j
];
5514 if (m
->p_type
!= PT_LOAD
)
5516 if (m
->includes_phdrs
)
5522 if (phdr_load_seg
== NULL
)
5523 off
+= actual
* bed
->s
->sizeof_phdr
;
5525 for (j
= 0; j
< alloc
; j
++)
5528 bfd_vma off_adjust
; /* Octets. */
5529 bfd_boolean no_contents
;
5531 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5532 number of sections with contents contributing to both p_filesz
5533 and p_memsz, followed by a number of sections with no contents
5534 that just contribute to p_memsz. In this loop, OFF tracks next
5535 available file offset for PT_LOAD and PT_NOTE segments. */
5536 m
= sorted_seg_map
[j
];
5538 p
->p_type
= m
->p_type
;
5539 p
->p_flags
= m
->p_flags
;
5542 p
->p_vaddr
= m
->p_vaddr_offset
* opb
;
5544 p
->p_vaddr
= (m
->sections
[0]->vma
+ m
->p_vaddr_offset
) * opb
;
5546 if (m
->p_paddr_valid
)
5547 p
->p_paddr
= m
->p_paddr
;
5548 else if (m
->count
== 0)
5551 p
->p_paddr
= (m
->sections
[0]->lma
+ m
->p_vaddr_offset
) * opb
;
5553 if (p
->p_type
== PT_LOAD
5554 && (abfd
->flags
& D_PAGED
) != 0)
5556 /* p_align in demand paged PT_LOAD segments effectively stores
5557 the maximum page size. When copying an executable with
5558 objcopy, we set m->p_align from the input file. Use this
5559 value for maxpagesize rather than bed->maxpagesize, which
5560 may be different. Note that we use maxpagesize for PT_TLS
5561 segment alignment later in this function, so we are relying
5562 on at least one PT_LOAD segment appearing before a PT_TLS
5564 if (m
->p_align_valid
)
5565 maxpagesize
= m
->p_align
;
5567 p
->p_align
= maxpagesize
;
5569 else if (m
->p_align_valid
)
5570 p
->p_align
= m
->p_align
;
5571 else if (m
->count
== 0)
5572 p
->p_align
= 1 << bed
->s
->log_file_align
;
5574 if (m
== phdr_load_seg
)
5576 if (!m
->includes_filehdr
)
5578 off
+= actual
* bed
->s
->sizeof_phdr
;
5581 no_contents
= FALSE
;
5583 if (p
->p_type
== PT_LOAD
5586 bfd_size_type align
; /* Bytes. */
5587 unsigned int align_power
= 0;
5589 if (m
->p_align_valid
)
5593 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5595 unsigned int secalign
;
5597 secalign
= bfd_section_alignment (*secpp
);
5598 if (secalign
> align_power
)
5599 align_power
= secalign
;
5601 align
= (bfd_size_type
) 1 << align_power
;
5602 if (align
< maxpagesize
)
5603 align
= maxpagesize
;
5606 for (i
= 0; i
< m
->count
; i
++)
5607 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5608 /* If we aren't making room for this section, then
5609 it must be SHT_NOBITS regardless of what we've
5610 set via struct bfd_elf_special_section. */
5611 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5613 /* Find out whether this segment contains any loadable
5616 for (i
= 0; i
< m
->count
; i
++)
5617 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5619 no_contents
= FALSE
;
5623 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
* opb
);
5625 /* Broken hardware and/or kernel require that files do not
5626 map the same page with different permissions on some hppa
5629 && (abfd
->flags
& D_PAGED
) != 0
5630 && bed
->no_page_alias
5631 && (off
& (maxpagesize
- 1)) != 0
5632 && ((off
& -maxpagesize
)
5633 == ((off
+ off_adjust
) & -maxpagesize
)))
5634 off_adjust
+= maxpagesize
;
5638 /* We shouldn't need to align the segment on disk since
5639 the segment doesn't need file space, but the gABI
5640 arguably requires the alignment and glibc ld.so
5641 checks it. So to comply with the alignment
5642 requirement but not waste file space, we adjust
5643 p_offset for just this segment. (OFF_ADJUST is
5644 subtracted from OFF later.) This may put p_offset
5645 past the end of file, but that shouldn't matter. */
5650 /* Make sure the .dynamic section is the first section in the
5651 PT_DYNAMIC segment. */
5652 else if (p
->p_type
== PT_DYNAMIC
5654 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5657 (_("%pB: The first section in the PT_DYNAMIC segment"
5658 " is not the .dynamic section"),
5660 bfd_set_error (bfd_error_bad_value
);
5663 /* Set the note section type to SHT_NOTE. */
5664 else if (p
->p_type
== PT_NOTE
)
5665 for (i
= 0; i
< m
->count
; i
++)
5666 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5668 if (m
->includes_filehdr
)
5670 if (!m
->p_flags_valid
)
5672 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5673 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5674 if (p
->p_type
== PT_LOAD
)
5678 if (p
->p_vaddr
< (bfd_vma
) off
5679 || (!m
->p_paddr_valid
5680 && p
->p_paddr
< (bfd_vma
) off
))
5683 (_("%pB: not enough room for program headers,"
5684 " try linking with -N"),
5686 bfd_set_error (bfd_error_bad_value
);
5690 if (!m
->p_paddr_valid
)
5694 else if (sorted_seg_map
[0]->includes_filehdr
)
5696 Elf_Internal_Phdr
*filehdr
= phdrs
+ sorted_seg_map
[0]->idx
;
5697 p
->p_vaddr
= filehdr
->p_vaddr
;
5698 if (!m
->p_paddr_valid
)
5699 p
->p_paddr
= filehdr
->p_paddr
;
5703 if (m
->includes_phdrs
)
5705 if (!m
->p_flags_valid
)
5707 p
->p_filesz
+= actual
* bed
->s
->sizeof_phdr
;
5708 p
->p_memsz
+= actual
* bed
->s
->sizeof_phdr
;
5709 if (!m
->includes_filehdr
)
5711 if (p
->p_type
== PT_LOAD
)
5713 elf_elfheader (abfd
)->e_phoff
= p
->p_offset
;
5716 p
->p_vaddr
-= off
- p
->p_offset
;
5717 if (!m
->p_paddr_valid
)
5718 p
->p_paddr
-= off
- p
->p_offset
;
5721 else if (phdr_load_seg
!= NULL
)
5723 Elf_Internal_Phdr
*phdr
= phdrs
+ phdr_load_seg
->idx
;
5724 bfd_vma phdr_off
= 0; /* Octets. */
5725 if (phdr_load_seg
->includes_filehdr
)
5726 phdr_off
= bed
->s
->sizeof_ehdr
;
5727 p
->p_vaddr
= phdr
->p_vaddr
+ phdr_off
;
5728 if (!m
->p_paddr_valid
)
5729 p
->p_paddr
= phdr
->p_paddr
+ phdr_off
;
5730 p
->p_offset
= phdr
->p_offset
+ phdr_off
;
5733 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5737 if (p
->p_type
== PT_LOAD
5738 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5740 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5745 /* Put meaningless p_offset for PT_LOAD segments
5746 without file contents somewhere within the first
5747 page, in an attempt to not point past EOF. */
5748 bfd_size_type align
= maxpagesize
;
5749 if (align
< p
->p_align
)
5753 p
->p_offset
= off
% align
;
5758 file_ptr adjust
; /* Octets. */
5760 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5762 p
->p_filesz
+= adjust
;
5763 p
->p_memsz
+= adjust
;
5767 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5768 maps. Set filepos for sections in PT_LOAD segments, and in
5769 core files, for sections in PT_NOTE segments.
5770 assign_file_positions_for_non_load_sections will set filepos
5771 for other sections and update p_filesz for other segments. */
5772 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5775 bfd_size_type align
;
5776 Elf_Internal_Shdr
*this_hdr
;
5779 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5780 align
= (bfd_size_type
) 1 << bfd_section_alignment (sec
);
5782 if ((p
->p_type
== PT_LOAD
5783 || p
->p_type
== PT_TLS
)
5784 && (this_hdr
->sh_type
!= SHT_NOBITS
5785 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5786 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5787 || p
->p_type
== PT_TLS
))))
5789 bfd_vma p_start
= p
->p_paddr
; /* Octets. */
5790 bfd_vma p_end
= p_start
+ p
->p_memsz
; /* Octets. */
5791 bfd_vma s_start
= sec
->lma
* opb
; /* Octets. */
5792 bfd_vma adjust
= s_start
- p_end
; /* Octets. */
5796 || p_end
< p_start
))
5799 /* xgettext:c-format */
5800 (_("%pB: section %pA lma %#" PRIx64
" adjusted to %#" PRIx64
),
5801 abfd
, sec
, (uint64_t) s_start
/ opb
,
5802 (uint64_t) p_end
/ opb
);
5804 sec
->lma
= p_end
/ opb
;
5806 p
->p_memsz
+= adjust
;
5808 if (p
->p_type
== PT_LOAD
)
5810 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5813 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5815 /* We have a PROGBITS section following NOBITS ones.
5816 Allocate file space for the NOBITS section(s) and
5818 adjust
= p
->p_memsz
- p
->p_filesz
;
5819 if (!write_zeros (abfd
, off
, adjust
))
5823 /* We only adjust sh_offset in SHT_NOBITS sections
5824 as would seem proper for their address when the
5825 section is first in the segment. sh_offset
5826 doesn't really have any significance for
5827 SHT_NOBITS anyway, apart from a notional position
5828 relative to other sections. Historically we
5829 didn't bother with adjusting sh_offset and some
5830 programs depend on it not being adjusted. See
5831 pr12921 and pr25662. */
5832 if (this_hdr
->sh_type
!= SHT_NOBITS
|| i
== 0)
5835 if (this_hdr
->sh_type
== SHT_NOBITS
)
5836 off_adjust
+= adjust
;
5839 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5840 p
->p_filesz
+= adjust
;
5843 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5845 /* The section at i == 0 is the one that actually contains
5849 this_hdr
->sh_offset
= sec
->filepos
= off
;
5850 off
+= this_hdr
->sh_size
;
5851 p
->p_filesz
= this_hdr
->sh_size
;
5857 /* The rest are fake sections that shouldn't be written. */
5866 if (p
->p_type
== PT_LOAD
)
5868 this_hdr
->sh_offset
= sec
->filepos
= off
;
5869 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5870 off
+= this_hdr
->sh_size
;
5872 else if (this_hdr
->sh_type
== SHT_NOBITS
5873 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5874 && this_hdr
->sh_offset
== 0)
5876 /* This is a .tbss section that didn't get a PT_LOAD.
5877 (See _bfd_elf_map_sections_to_segments "Create a
5878 final PT_LOAD".) Set sh_offset to the value it
5879 would have if we had created a zero p_filesz and
5880 p_memsz PT_LOAD header for the section. This
5881 also makes the PT_TLS header have the same
5883 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5885 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5888 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5890 p
->p_filesz
+= this_hdr
->sh_size
;
5891 /* A load section without SHF_ALLOC is something like
5892 a note section in a PT_NOTE segment. These take
5893 file space but are not loaded into memory. */
5894 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5895 p
->p_memsz
+= this_hdr
->sh_size
;
5897 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5899 if (p
->p_type
== PT_TLS
)
5900 p
->p_memsz
+= this_hdr
->sh_size
;
5902 /* .tbss is special. It doesn't contribute to p_memsz of
5904 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5905 p
->p_memsz
+= this_hdr
->sh_size
;
5908 if (align
> p
->p_align
5909 && !m
->p_align_valid
5910 && (p
->p_type
!= PT_LOAD
5911 || (abfd
->flags
& D_PAGED
) == 0))
5915 if (!m
->p_flags_valid
)
5918 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5920 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5927 /* PR ld/20815 - Check that the program header segment, if
5928 present, will be loaded into memory. */
5929 if (p
->p_type
== PT_PHDR
5930 && phdr_load_seg
== NULL
5931 && !(bed
->elf_backend_allow_non_load_phdr
!= NULL
5932 && bed
->elf_backend_allow_non_load_phdr (abfd
, phdrs
, alloc
)))
5934 /* The fix for this error is usually to edit the linker script being
5935 used and set up the program headers manually. Either that or
5936 leave room for the headers at the start of the SECTIONS. */
5937 _bfd_error_handler (_("%pB: error: PHDR segment not covered"
5938 " by LOAD segment"),
5940 if (link_info
== NULL
)
5942 /* Arrange for the linker to exit with an error, deleting
5943 the output file unless --noinhibit-exec is given. */
5944 link_info
->callbacks
->info ("%X");
5947 /* Check that all sections are in a PT_LOAD segment.
5948 Don't check funky gdb generated core files. */
5949 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5951 bfd_boolean check_vma
= TRUE
;
5953 for (i
= 1; i
< m
->count
; i
++)
5954 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5955 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5956 ->this_hdr
), p
) != 0
5957 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5958 ->this_hdr
), p
) != 0)
5960 /* Looks like we have overlays packed into the segment. */
5965 for (i
= 0; i
< m
->count
; i
++)
5967 Elf_Internal_Shdr
*this_hdr
;
5970 sec
= m
->sections
[i
];
5971 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5972 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5973 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5976 /* xgettext:c-format */
5977 (_("%pB: section `%pA' can't be allocated in segment %d"),
5979 print_segment_map (m
);
5985 elf_next_file_pos (abfd
) = off
;
5987 if (link_info
!= NULL
5988 && phdr_load_seg
!= NULL
5989 && phdr_load_seg
->includes_filehdr
)
5991 /* There is a segment that contains both the file headers and the
5992 program headers, so provide a symbol __ehdr_start pointing there.
5993 A program can use this to examine itself robustly. */
5995 struct elf_link_hash_entry
*hash
5996 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5997 FALSE
, FALSE
, TRUE
);
5998 /* If the symbol was referenced and not defined, define it. */
6000 && (hash
->root
.type
== bfd_link_hash_new
6001 || hash
->root
.type
== bfd_link_hash_undefined
6002 || hash
->root
.type
== bfd_link_hash_undefweak
6003 || hash
->root
.type
== bfd_link_hash_common
))
6006 bfd_vma filehdr_vaddr
= phdrs
[phdr_load_seg
->idx
].p_vaddr
/ opb
;
6008 if (phdr_load_seg
->count
!= 0)
6009 /* The segment contains sections, so use the first one. */
6010 s
= phdr_load_seg
->sections
[0];
6012 /* Use the first (i.e. lowest-addressed) section in any segment. */
6013 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
6014 if (m
->p_type
== PT_LOAD
&& m
->count
!= 0)
6022 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
6023 hash
->root
.u
.def
.section
= s
;
6027 hash
->root
.u
.def
.value
= filehdr_vaddr
;
6028 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
6031 hash
->root
.type
= bfd_link_hash_defined
;
6032 hash
->def_regular
= 1;
6040 /* Determine if a bfd is a debuginfo file. Unfortunately there
6041 is no defined method for detecting such files, so we have to
6042 use heuristics instead. */
6045 is_debuginfo_file (bfd
*abfd
)
6047 if (abfd
== NULL
|| bfd_get_flavour (abfd
) != bfd_target_elf_flavour
)
6050 Elf_Internal_Shdr
**start_headers
= elf_elfsections (abfd
);
6051 Elf_Internal_Shdr
**end_headers
= start_headers
+ elf_numsections (abfd
);
6052 Elf_Internal_Shdr
**headerp
;
6054 for (headerp
= start_headers
; headerp
< end_headers
; headerp
++)
6056 Elf_Internal_Shdr
*header
= * headerp
;
6058 /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6059 The only allocated sections are SHT_NOBITS or SHT_NOTES. */
6060 if ((header
->sh_flags
& SHF_ALLOC
) == SHF_ALLOC
6061 && header
->sh_type
!= SHT_NOBITS
6062 && header
->sh_type
!= SHT_NOTE
)
6069 /* Assign file positions for the other sections, except for compressed debugging
6070 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */
6073 assign_file_positions_for_non_load_sections (bfd
*abfd
,
6074 struct bfd_link_info
*link_info
)
6076 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6077 Elf_Internal_Shdr
**i_shdrpp
;
6078 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
6079 Elf_Internal_Phdr
*phdrs
;
6080 Elf_Internal_Phdr
*p
;
6081 struct elf_segment_map
*m
;
6083 unsigned int opb
= bfd_octets_per_byte (abfd
, NULL
);
6085 i_shdrpp
= elf_elfsections (abfd
);
6086 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
6087 off
= elf_next_file_pos (abfd
);
6088 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
6090 Elf_Internal_Shdr
*hdr
;
6093 if (hdr
->bfd_section
!= NULL
6094 && (hdr
->bfd_section
->filepos
!= 0
6095 || (hdr
->sh_type
== SHT_NOBITS
6096 && hdr
->contents
== NULL
)))
6097 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
6098 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
6100 if (hdr
->sh_size
!= 0
6101 /* PR 24717 - debuginfo files are known to be not strictly
6102 compliant with the ELF standard. In particular they often
6103 have .note.gnu.property sections that are outside of any
6104 loadable segment. This is not a problem for such files,
6105 so do not warn about them. */
6106 && ! is_debuginfo_file (abfd
))
6108 /* xgettext:c-format */
6109 (_("%pB: warning: allocated section `%s' not in segment"),
6111 (hdr
->bfd_section
== NULL
6113 : hdr
->bfd_section
->name
));
6114 /* We don't need to page align empty sections. */
6115 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
6116 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6119 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
6121 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
6124 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6125 && hdr
->bfd_section
== NULL
)
6126 /* We don't know the offset of these sections yet: their size has
6127 not been decided. */
6128 || (hdr
->bfd_section
!= NULL
6129 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6130 || (bfd_section_is_ctf (hdr
->bfd_section
)
6131 && abfd
->is_linker_output
)))
6132 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
6133 || (elf_symtab_shndx_list (abfd
) != NULL
6134 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6135 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
6136 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
6137 hdr
->sh_offset
= -1;
6139 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6141 elf_next_file_pos (abfd
) = off
;
6143 /* Now that we have set the section file positions, we can set up
6144 the file positions for the non PT_LOAD segments. */
6145 phdrs
= elf_tdata (abfd
)->phdr
;
6146 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
6148 if (p
->p_type
== PT_GNU_RELRO
)
6150 bfd_vma start
, end
; /* Bytes. */
6153 if (link_info
!= NULL
)
6155 /* During linking the range of the RELRO segment is passed
6156 in link_info. Note that there may be padding between
6157 relro_start and the first RELRO section. */
6158 start
= link_info
->relro_start
;
6159 end
= link_info
->relro_end
;
6161 else if (m
->count
!= 0)
6163 if (!m
->p_size_valid
)
6165 start
= m
->sections
[0]->vma
;
6166 end
= start
+ m
->p_size
/ opb
;
6177 struct elf_segment_map
*lm
;
6178 const Elf_Internal_Phdr
*lp
;
6181 /* Find a LOAD segment containing a section in the RELRO
6183 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
6185 lm
= lm
->next
, lp
++)
6187 if (lp
->p_type
== PT_LOAD
6189 && (lm
->sections
[lm
->count
- 1]->vma
6190 + (!IS_TBSS (lm
->sections
[lm
->count
- 1])
6191 ? lm
->sections
[lm
->count
- 1]->size
/ opb
6193 && lm
->sections
[0]->vma
< end
)
6199 /* Find the section starting the RELRO segment. */
6200 for (i
= 0; i
< lm
->count
; i
++)
6202 asection
*s
= lm
->sections
[i
];
6211 p
->p_vaddr
= lm
->sections
[i
]->vma
* opb
;
6212 p
->p_paddr
= lm
->sections
[i
]->lma
* opb
;
6213 p
->p_offset
= lm
->sections
[i
]->filepos
;
6214 p
->p_memsz
= end
* opb
- p
->p_vaddr
;
6215 p
->p_filesz
= p
->p_memsz
;
6217 /* The RELRO segment typically ends a few bytes
6218 into .got.plt but other layouts are possible.
6219 In cases where the end does not match any
6220 loaded section (for instance is in file
6221 padding), trim p_filesz back to correspond to
6222 the end of loaded section contents. */
6223 if (p
->p_filesz
> lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
)
6224 p
->p_filesz
= lp
->p_vaddr
+ lp
->p_filesz
- p
->p_vaddr
;
6226 /* Preserve the alignment and flags if they are
6227 valid. The gold linker generates RW/4 for
6228 the PT_GNU_RELRO section. It is better for
6229 objcopy/strip to honor these attributes
6230 otherwise gdb will choke when using separate
6232 if (!m
->p_align_valid
)
6234 if (!m
->p_flags_valid
)
6240 if (link_info
!= NULL
)
6243 memset (p
, 0, sizeof *p
);
6245 else if (p
->p_type
== PT_GNU_STACK
)
6247 if (m
->p_size_valid
)
6248 p
->p_memsz
= m
->p_size
;
6250 else if (m
->count
!= 0)
6254 if (p
->p_type
!= PT_LOAD
6255 && (p
->p_type
!= PT_NOTE
6256 || bfd_get_format (abfd
) != bfd_core
))
6258 /* A user specified segment layout may include a PHDR
6259 segment that overlaps with a LOAD segment... */
6260 if (p
->p_type
== PT_PHDR
)
6266 if (m
->includes_filehdr
|| m
->includes_phdrs
)
6268 /* PR 17512: file: 2195325e. */
6270 (_("%pB: error: non-load segment %d includes file header "
6271 "and/or program header"),
6272 abfd
, (int) (p
- phdrs
));
6277 p
->p_offset
= m
->sections
[0]->filepos
;
6278 for (i
= m
->count
; i
-- != 0;)
6280 asection
*sect
= m
->sections
[i
];
6281 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
6282 if (hdr
->sh_type
!= SHT_NOBITS
)
6284 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
6296 static elf_section_list
*
6297 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6299 for (;list
!= NULL
; list
= list
->next
)
6305 /* Work out the file positions of all the sections. This is called by
6306 _bfd_elf_compute_section_file_positions. All the section sizes and
6307 VMAs must be known before this is called.
6309 Reloc sections come in two flavours: Those processed specially as
6310 "side-channel" data attached to a section to which they apply, and those that
6311 bfd doesn't process as relocations. The latter sort are stored in a normal
6312 bfd section by bfd_section_from_shdr. We don't consider the former sort
6313 here, unless they form part of the loadable image. Reloc sections not
6314 assigned here (and compressed debugging sections and CTF sections which
6315 nothing else in the file can rely upon) will be handled later by
6316 assign_file_positions_for_relocs.
6318 We also don't set the positions of the .symtab and .strtab here. */
6321 assign_file_positions_except_relocs (bfd
*abfd
,
6322 struct bfd_link_info
*link_info
)
6324 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6325 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6326 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6329 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6330 && bfd_get_format (abfd
) != bfd_core
)
6332 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6333 unsigned int num_sec
= elf_numsections (abfd
);
6334 Elf_Internal_Shdr
**hdrpp
;
6338 /* Start after the ELF header. */
6339 off
= i_ehdrp
->e_ehsize
;
6341 /* We are not creating an executable, which means that we are
6342 not creating a program header, and that the actual order of
6343 the sections in the file is unimportant. */
6344 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6346 Elf_Internal_Shdr
*hdr
;
6349 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6350 && hdr
->bfd_section
== NULL
)
6351 /* Do not assign offsets for these sections yet: we don't know
6353 || (hdr
->bfd_section
!= NULL
6354 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
6355 || (bfd_section_is_ctf (hdr
->bfd_section
)
6356 && abfd
->is_linker_output
)))
6357 || i
== elf_onesymtab (abfd
)
6358 || (elf_symtab_shndx_list (abfd
) != NULL
6359 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6360 || i
== elf_strtab_sec (abfd
)
6361 || i
== elf_shstrtab_sec (abfd
))
6363 hdr
->sh_offset
= -1;
6366 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6369 elf_next_file_pos (abfd
) = off
;
6370 elf_program_header_size (abfd
) = 0;
6374 /* Assign file positions for the loaded sections based on the
6375 assignment of sections to segments. */
6376 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6379 /* And for non-load sections. */
6380 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6384 if (!(*bed
->elf_backend_modify_headers
) (abfd
, link_info
))
6387 /* Write out the program headers. */
6388 alloc
= i_ehdrp
->e_phnum
;
6391 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) != 0
6392 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6400 _bfd_elf_init_file_header (bfd
*abfd
,
6401 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
6403 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6404 struct elf_strtab_hash
*shstrtab
;
6405 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6407 i_ehdrp
= elf_elfheader (abfd
);
6409 shstrtab
= _bfd_elf_strtab_init ();
6410 if (shstrtab
== NULL
)
6413 elf_shstrtab (abfd
) = shstrtab
;
6415 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6416 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6417 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6418 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6420 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6421 i_ehdrp
->e_ident
[EI_DATA
] =
6422 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6423 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6425 if ((abfd
->flags
& DYNAMIC
) != 0)
6426 i_ehdrp
->e_type
= ET_DYN
;
6427 else if ((abfd
->flags
& EXEC_P
) != 0)
6428 i_ehdrp
->e_type
= ET_EXEC
;
6429 else if (bfd_get_format (abfd
) == bfd_core
)
6430 i_ehdrp
->e_type
= ET_CORE
;
6432 i_ehdrp
->e_type
= ET_REL
;
6434 switch (bfd_get_arch (abfd
))
6436 case bfd_arch_unknown
:
6437 i_ehdrp
->e_machine
= EM_NONE
;
6440 /* There used to be a long list of cases here, each one setting
6441 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6442 in the corresponding bfd definition. To avoid duplication,
6443 the switch was removed. Machines that need special handling
6444 can generally do it in elf_backend_final_write_processing(),
6445 unless they need the information earlier than the final write.
6446 Such need can generally be supplied by replacing the tests for
6447 e_machine with the conditions used to determine it. */
6449 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6452 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6453 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6455 /* No program header, for now. */
6456 i_ehdrp
->e_phoff
= 0;
6457 i_ehdrp
->e_phentsize
= 0;
6458 i_ehdrp
->e_phnum
= 0;
6460 /* Each bfd section is section header entry. */
6461 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6462 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6464 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6465 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6466 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6467 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6468 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6469 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6470 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6471 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6472 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6478 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
6480 FIXME: We used to have code here to sort the PT_LOAD segments into
6481 ascending order, as per the ELF spec. But this breaks some programs,
6482 including the Linux kernel. But really either the spec should be
6483 changed or the programs updated. */
6486 _bfd_elf_modify_headers (bfd
*obfd
, struct bfd_link_info
*link_info
)
6488 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6490 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (obfd
);
6491 unsigned int num_segments
= i_ehdrp
->e_phnum
;
6492 struct elf_obj_tdata
*tdata
= elf_tdata (obfd
);
6493 Elf_Internal_Phdr
*segment
= tdata
->phdr
;
6494 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6496 /* Find the lowest p_vaddr in PT_LOAD segments. */
6497 bfd_vma p_vaddr
= (bfd_vma
) -1;
6498 for (; segment
< end_segment
; segment
++)
6499 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6500 p_vaddr
= segment
->p_vaddr
;
6502 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6503 segments is non-zero. */
6505 i_ehdrp
->e_type
= ET_EXEC
;
6510 /* Assign file positions for all the reloc sections which are not part
6511 of the loadable file image, and the file position of section headers. */
6514 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6517 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6518 Elf_Internal_Shdr
*shdrp
;
6519 Elf_Internal_Ehdr
*i_ehdrp
;
6520 const struct elf_backend_data
*bed
;
6522 off
= elf_next_file_pos (abfd
);
6524 shdrpp
= elf_elfsections (abfd
);
6525 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6526 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6529 if (shdrp
->sh_offset
== -1)
6531 asection
*sec
= shdrp
->bfd_section
;
6532 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6533 || shdrp
->sh_type
== SHT_RELA
);
6534 bfd_boolean is_ctf
= sec
&& bfd_section_is_ctf (sec
);
6537 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6539 if (!is_rel
&& !is_ctf
)
6541 const char *name
= sec
->name
;
6542 struct bfd_elf_section_data
*d
;
6544 /* Compress DWARF debug sections. */
6545 if (!bfd_compress_section (abfd
, sec
,
6549 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6550 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6552 /* If section is compressed with zlib-gnu, convert
6553 section name from .debug_* to .zdebug_*. */
6555 = convert_debug_to_zdebug (abfd
, name
);
6556 if (new_name
== NULL
)
6560 /* Add section name to section name section. */
6561 if (shdrp
->sh_name
!= (unsigned int) -1)
6564 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6566 d
= elf_section_data (sec
);
6568 /* Add reloc section name to section name section. */
6570 && !_bfd_elf_set_reloc_sh_name (abfd
,
6575 && !_bfd_elf_set_reloc_sh_name (abfd
,
6580 /* Update section size and contents. */
6581 shdrp
->sh_size
= sec
->size
;
6582 shdrp
->contents
= sec
->contents
;
6583 shdrp
->bfd_section
->contents
= NULL
;
6587 /* Update section size and contents. */
6588 shdrp
->sh_size
= sec
->size
;
6589 shdrp
->contents
= sec
->contents
;
6592 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6599 /* Place section name section after DWARF debug sections have been
6601 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6602 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6603 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6604 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6606 /* Place the section headers. */
6607 i_ehdrp
= elf_elfheader (abfd
);
6608 bed
= get_elf_backend_data (abfd
);
6609 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6610 i_ehdrp
->e_shoff
= off
;
6611 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6612 elf_next_file_pos (abfd
) = off
;
6618 _bfd_elf_write_object_contents (bfd
*abfd
)
6620 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6621 Elf_Internal_Shdr
**i_shdrp
;
6623 unsigned int count
, num_sec
;
6624 struct elf_obj_tdata
*t
;
6626 if (! abfd
->output_has_begun
6627 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6629 /* Do not rewrite ELF data when the BFD has been opened for update.
6630 abfd->output_has_begun was set to TRUE on opening, so creation of new
6631 sections, and modification of existing section sizes was restricted.
6632 This means the ELF header, program headers and section headers can't have
6634 If the contents of any sections has been modified, then those changes have
6635 already been written to the BFD. */
6636 else if (abfd
->direction
== both_direction
)
6638 BFD_ASSERT (abfd
->output_has_begun
);
6642 i_shdrp
= elf_elfsections (abfd
);
6645 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6649 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6652 /* After writing the headers, we need to write the sections too... */
6653 num_sec
= elf_numsections (abfd
);
6654 for (count
= 1; count
< num_sec
; count
++)
6656 i_shdrp
[count
]->sh_name
6657 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6658 i_shdrp
[count
]->sh_name
);
6659 if (bed
->elf_backend_section_processing
)
6660 if (!(*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]))
6662 if (i_shdrp
[count
]->contents
)
6664 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6666 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6667 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6672 /* Write out the section header names. */
6673 t
= elf_tdata (abfd
);
6674 if (elf_shstrtab (abfd
) != NULL
6675 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6676 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6679 if (!(*bed
->elf_backend_final_write_processing
) (abfd
))
6682 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6685 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6686 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6687 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6693 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6695 /* Hopefully this can be done just like an object file. */
6696 return _bfd_elf_write_object_contents (abfd
);
6699 /* Given a section, search the header to find them. */
6702 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6704 const struct elf_backend_data
*bed
;
6705 unsigned int sec_index
;
6707 if (elf_section_data (asect
) != NULL
6708 && elf_section_data (asect
)->this_idx
!= 0)
6709 return elf_section_data (asect
)->this_idx
;
6711 if (bfd_is_abs_section (asect
))
6712 sec_index
= SHN_ABS
;
6713 else if (bfd_is_com_section (asect
))
6714 sec_index
= SHN_COMMON
;
6715 else if (bfd_is_und_section (asect
))
6716 sec_index
= SHN_UNDEF
;
6718 sec_index
= SHN_BAD
;
6720 bed
= get_elf_backend_data (abfd
);
6721 if (bed
->elf_backend_section_from_bfd_section
)
6723 int retval
= sec_index
;
6725 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6729 if (sec_index
== SHN_BAD
)
6730 bfd_set_error (bfd_error_nonrepresentable_section
);
6735 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6739 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6741 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6743 flagword flags
= asym_ptr
->flags
;
6745 /* When gas creates relocations against local labels, it creates its
6746 own symbol for the section, but does put the symbol into the
6747 symbol chain, so udata is 0. When the linker is generating
6748 relocatable output, this section symbol may be for one of the
6749 input sections rather than the output section. */
6750 if (asym_ptr
->udata
.i
== 0
6751 && (flags
& BSF_SECTION_SYM
)
6752 && asym_ptr
->section
)
6757 sec
= asym_ptr
->section
;
6758 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6759 sec
= sec
->output_section
;
6760 if (sec
->owner
== abfd
6761 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6762 && elf_section_syms (abfd
)[indx
] != NULL
)
6763 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6766 idx
= asym_ptr
->udata
.i
;
6770 /* This case can occur when using --strip-symbol on a symbol
6771 which is used in a relocation entry. */
6773 /* xgettext:c-format */
6774 (_("%pB: symbol `%s' required but not present"),
6775 abfd
, bfd_asymbol_name (asym_ptr
));
6776 bfd_set_error (bfd_error_no_symbols
);
6783 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6784 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6792 /* Rewrite program header information. */
6795 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6797 Elf_Internal_Ehdr
*iehdr
;
6798 struct elf_segment_map
*map
;
6799 struct elf_segment_map
*map_first
;
6800 struct elf_segment_map
**pointer_to_map
;
6801 Elf_Internal_Phdr
*segment
;
6804 unsigned int num_segments
;
6805 bfd_boolean phdr_included
= FALSE
;
6806 bfd_boolean p_paddr_valid
;
6807 bfd_vma maxpagesize
;
6808 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6809 unsigned int phdr_adjust_num
= 0;
6810 const struct elf_backend_data
*bed
;
6811 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
6813 bed
= get_elf_backend_data (ibfd
);
6814 iehdr
= elf_elfheader (ibfd
);
6817 pointer_to_map
= &map_first
;
6819 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6820 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6822 /* Returns the end address of the segment + 1. */
6823 #define SEGMENT_END(segment, start) \
6824 (start + (segment->p_memsz > segment->p_filesz \
6825 ? segment->p_memsz : segment->p_filesz))
6827 #define SECTION_SIZE(section, segment) \
6828 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6829 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6830 ? section->size : 0)
6832 /* Returns TRUE if the given section is contained within
6833 the given segment. VMA addresses are compared. */
6834 #define IS_CONTAINED_BY_VMA(section, segment, opb) \
6835 (section->vma * (opb) >= segment->p_vaddr \
6836 && (section->vma * (opb) + SECTION_SIZE (section, segment) \
6837 <= (SEGMENT_END (segment, segment->p_vaddr))))
6839 /* Returns TRUE if the given section is contained within
6840 the given segment. LMA addresses are compared. */
6841 #define IS_CONTAINED_BY_LMA(section, segment, base, opb) \
6842 (section->lma * (opb) >= base \
6843 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \
6844 && (section->lma * (opb) + SECTION_SIZE (section, segment) \
6845 <= SEGMENT_END (segment, base)))
6847 /* Handle PT_NOTE segment. */
6848 #define IS_NOTE(p, s) \
6849 (p->p_type == PT_NOTE \
6850 && elf_section_type (s) == SHT_NOTE \
6851 && (bfd_vma) s->filepos >= p->p_offset \
6852 && ((bfd_vma) s->filepos + s->size \
6853 <= p->p_offset + p->p_filesz))
6855 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6857 #define IS_COREFILE_NOTE(p, s) \
6859 && bfd_get_format (ibfd) == bfd_core \
6863 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6864 linker, which generates a PT_INTERP section with p_vaddr and
6865 p_memsz set to 0. */
6866 #define IS_SOLARIS_PT_INTERP(p, s) \
6868 && p->p_paddr == 0 \
6869 && p->p_memsz == 0 \
6870 && p->p_filesz > 0 \
6871 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6873 && (bfd_vma) s->filepos >= p->p_offset \
6874 && ((bfd_vma) s->filepos + s->size \
6875 <= p->p_offset + p->p_filesz))
6877 /* Decide if the given section should be included in the given segment.
6878 A section will be included if:
6879 1. It is within the address space of the segment -- we use the LMA
6880 if that is set for the segment and the VMA otherwise,
6881 2. It is an allocated section or a NOTE section in a PT_NOTE
6883 3. There is an output section associated with it,
6884 4. The section has not already been allocated to a previous segment.
6885 5. PT_GNU_STACK segments do not include any sections.
6886 6. PT_TLS segment includes only SHF_TLS sections.
6887 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6888 8. PT_DYNAMIC should not contain empty sections at the beginning
6889 (with the possible exception of .dynamic). */
6890 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \
6891 ((((segment->p_paddr \
6892 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \
6893 : IS_CONTAINED_BY_VMA (section, segment, opb)) \
6894 && (section->flags & SEC_ALLOC) != 0) \
6895 || IS_NOTE (segment, section)) \
6896 && segment->p_type != PT_GNU_STACK \
6897 && (segment->p_type != PT_TLS \
6898 || (section->flags & SEC_THREAD_LOCAL)) \
6899 && (segment->p_type == PT_LOAD \
6900 || segment->p_type == PT_TLS \
6901 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6902 && (segment->p_type != PT_DYNAMIC \
6903 || SECTION_SIZE (section, segment) > 0 \
6904 || (segment->p_paddr \
6905 ? segment->p_paddr != section->lma * (opb) \
6906 : segment->p_vaddr != section->vma * (opb)) \
6907 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \
6908 && (segment->p_type != PT_LOAD || !section->segment_mark))
6910 /* If the output section of a section in the input segment is NULL,
6911 it is removed from the corresponding output segment. */
6912 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \
6913 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \
6914 && section->output_section != NULL)
6916 /* Returns TRUE iff seg1 starts after the end of seg2. */
6917 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6918 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6920 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6921 their VMA address ranges and their LMA address ranges overlap.
6922 It is possible to have overlapping VMA ranges without overlapping LMA
6923 ranges. RedBoot images for example can have both .data and .bss mapped
6924 to the same VMA range, but with the .data section mapped to a different
6926 #define SEGMENT_OVERLAPS(seg1, seg2) \
6927 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6928 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6929 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6930 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6932 /* Initialise the segment mark field. */
6933 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6934 section
->segment_mark
= FALSE
;
6936 /* The Solaris linker creates program headers in which all the
6937 p_paddr fields are zero. When we try to objcopy or strip such a
6938 file, we get confused. Check for this case, and if we find it
6939 don't set the p_paddr_valid fields. */
6940 p_paddr_valid
= FALSE
;
6941 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6944 if (segment
->p_paddr
!= 0)
6946 p_paddr_valid
= TRUE
;
6950 /* Scan through the segments specified in the program header
6951 of the input BFD. For this first scan we look for overlaps
6952 in the loadable segments. These can be created by weird
6953 parameters to objcopy. Also, fix some solaris weirdness. */
6954 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6959 Elf_Internal_Phdr
*segment2
;
6961 if (segment
->p_type
== PT_INTERP
)
6962 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6963 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6965 /* Mininal change so that the normal section to segment
6966 assignment code will work. */
6967 segment
->p_vaddr
= section
->vma
* opb
;
6971 if (segment
->p_type
!= PT_LOAD
)
6973 /* Remove PT_GNU_RELRO segment. */
6974 if (segment
->p_type
== PT_GNU_RELRO
)
6975 segment
->p_type
= PT_NULL
;
6979 /* Determine if this segment overlaps any previous segments. */
6980 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6982 bfd_signed_vma extra_length
;
6984 if (segment2
->p_type
!= PT_LOAD
6985 || !SEGMENT_OVERLAPS (segment
, segment2
))
6988 /* Merge the two segments together. */
6989 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6991 /* Extend SEGMENT2 to include SEGMENT and then delete
6993 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6994 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6996 if (extra_length
> 0)
6998 segment2
->p_memsz
+= extra_length
;
6999 segment2
->p_filesz
+= extra_length
;
7002 segment
->p_type
= PT_NULL
;
7004 /* Since we have deleted P we must restart the outer loop. */
7006 segment
= elf_tdata (ibfd
)->phdr
;
7011 /* Extend SEGMENT to include SEGMENT2 and then delete
7013 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
7014 - SEGMENT_END (segment
, segment
->p_vaddr
));
7016 if (extra_length
> 0)
7018 segment
->p_memsz
+= extra_length
;
7019 segment
->p_filesz
+= extra_length
;
7022 segment2
->p_type
= PT_NULL
;
7027 /* The second scan attempts to assign sections to segments. */
7028 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7032 unsigned int section_count
;
7033 asection
**sections
;
7034 asection
*output_section
;
7036 asection
*matching_lma
;
7037 asection
*suggested_lma
;
7040 asection
*first_section
;
7042 if (segment
->p_type
== PT_NULL
)
7045 first_section
= NULL
;
7046 /* Compute how many sections might be placed into this segment. */
7047 for (section
= ibfd
->sections
, section_count
= 0;
7049 section
= section
->next
)
7051 /* Find the first section in the input segment, which may be
7052 removed from the corresponding output segment. */
7053 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
, opb
))
7055 if (first_section
== NULL
)
7056 first_section
= section
;
7057 if (section
->output_section
!= NULL
)
7062 /* Allocate a segment map big enough to contain
7063 all of the sections we have selected. */
7064 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7065 amt
+= section_count
* sizeof (asection
*);
7066 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7070 /* Initialise the fields of the segment map. Default to
7071 using the physical address of the segment in the input BFD. */
7073 map
->p_type
= segment
->p_type
;
7074 map
->p_flags
= segment
->p_flags
;
7075 map
->p_flags_valid
= 1;
7077 /* If the first section in the input segment is removed, there is
7078 no need to preserve segment physical address in the corresponding
7080 if (!first_section
|| first_section
->output_section
!= NULL
)
7082 map
->p_paddr
= segment
->p_paddr
;
7083 map
->p_paddr_valid
= p_paddr_valid
;
7086 /* Determine if this segment contains the ELF file header
7087 and if it contains the program headers themselves. */
7088 map
->includes_filehdr
= (segment
->p_offset
== 0
7089 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7090 map
->includes_phdrs
= 0;
7092 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
7094 map
->includes_phdrs
=
7095 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7096 && (segment
->p_offset
+ segment
->p_filesz
7097 >= ((bfd_vma
) iehdr
->e_phoff
7098 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7100 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7101 phdr_included
= TRUE
;
7104 if (section_count
== 0)
7106 /* Special segments, such as the PT_PHDR segment, may contain
7107 no sections, but ordinary, loadable segments should contain
7108 something. They are allowed by the ELF spec however, so only
7109 a warning is produced.
7110 There is however the valid use case of embedded systems which
7111 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7112 flash memory with zeros. No warning is shown for that case. */
7113 if (segment
->p_type
== PT_LOAD
7114 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
7115 /* xgettext:c-format */
7117 (_("%pB: warning: empty loadable segment detected"
7118 " at vaddr=%#" PRIx64
", is this intentional?"),
7119 ibfd
, (uint64_t) segment
->p_vaddr
);
7121 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7123 *pointer_to_map
= map
;
7124 pointer_to_map
= &map
->next
;
7129 /* Now scan the sections in the input BFD again and attempt
7130 to add their corresponding output sections to the segment map.
7131 The problem here is how to handle an output section which has
7132 been moved (ie had its LMA changed). There are four possibilities:
7134 1. None of the sections have been moved.
7135 In this case we can continue to use the segment LMA from the
7138 2. All of the sections have been moved by the same amount.
7139 In this case we can change the segment's LMA to match the LMA
7140 of the first section.
7142 3. Some of the sections have been moved, others have not.
7143 In this case those sections which have not been moved can be
7144 placed in the current segment which will have to have its size,
7145 and possibly its LMA changed, and a new segment or segments will
7146 have to be created to contain the other sections.
7148 4. The sections have been moved, but not by the same amount.
7149 In this case we can change the segment's LMA to match the LMA
7150 of the first section and we will have to create a new segment
7151 or segments to contain the other sections.
7153 In order to save time, we allocate an array to hold the section
7154 pointers that we are interested in. As these sections get assigned
7155 to a segment, they are removed from this array. */
7157 amt
= section_count
* sizeof (asection
*);
7158 sections
= (asection
**) bfd_malloc (amt
);
7159 if (sections
== NULL
)
7162 /* Step One: Scan for segment vs section LMA conflicts.
7163 Also add the sections to the section array allocated above.
7164 Also add the sections to the current segment. In the common
7165 case, where the sections have not been moved, this means that
7166 we have completely filled the segment, and there is nothing
7169 matching_lma
= NULL
;
7170 suggested_lma
= NULL
;
7172 for (section
= first_section
, j
= 0;
7174 section
= section
->next
)
7176 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
, opb
))
7178 output_section
= section
->output_section
;
7180 sections
[j
++] = section
;
7182 /* The Solaris native linker always sets p_paddr to 0.
7183 We try to catch that case here, and set it to the
7184 correct value. Note - some backends require that
7185 p_paddr be left as zero. */
7187 && segment
->p_vaddr
!= 0
7188 && !bed
->want_p_paddr_set_to_zero
7190 && output_section
->lma
!= 0
7191 && (align_power (segment
->p_vaddr
7192 + (map
->includes_filehdr
7193 ? iehdr
->e_ehsize
: 0)
7194 + (map
->includes_phdrs
7195 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7197 output_section
->alignment_power
* opb
)
7198 == (output_section
->vma
* opb
)))
7199 map
->p_paddr
= segment
->p_vaddr
;
7201 /* Match up the physical address of the segment with the
7202 LMA address of the output section. */
7203 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7205 || IS_COREFILE_NOTE (segment
, section
)
7206 || (bed
->want_p_paddr_set_to_zero
7207 && IS_CONTAINED_BY_VMA (output_section
, segment
, opb
)))
7209 if (matching_lma
== NULL
7210 || output_section
->lma
< matching_lma
->lma
)
7211 matching_lma
= output_section
;
7213 /* We assume that if the section fits within the segment
7214 then it does not overlap any other section within that
7216 map
->sections
[isec
++] = output_section
;
7218 else if (suggested_lma
== NULL
)
7219 suggested_lma
= output_section
;
7221 if (j
== section_count
)
7226 BFD_ASSERT (j
== section_count
);
7228 /* Step Two: Adjust the physical address of the current segment,
7230 if (isec
== section_count
)
7232 /* All of the sections fitted within the segment as currently
7233 specified. This is the default case. Add the segment to
7234 the list of built segments and carry on to process the next
7235 program header in the input BFD. */
7236 map
->count
= section_count
;
7237 *pointer_to_map
= map
;
7238 pointer_to_map
= &map
->next
;
7241 && !bed
->want_p_paddr_set_to_zero
)
7243 bfd_vma hdr_size
= 0;
7244 if (map
->includes_filehdr
)
7245 hdr_size
= iehdr
->e_ehsize
;
7246 if (map
->includes_phdrs
)
7247 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7249 /* Account for padding before the first section in the
7251 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7252 - matching_lma
->lma
);
7260 /* Change the current segment's physical address to match
7261 the LMA of the first section that fitted, or if no
7262 section fitted, the first section. */
7263 if (matching_lma
== NULL
)
7264 matching_lma
= suggested_lma
;
7266 map
->p_paddr
= matching_lma
->lma
* opb
;
7268 /* Offset the segment physical address from the lma
7269 to allow for space taken up by elf headers. */
7270 if (map
->includes_phdrs
)
7272 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7274 /* iehdr->e_phnum is just an estimate of the number
7275 of program headers that we will need. Make a note
7276 here of the number we used and the segment we chose
7277 to hold these headers, so that we can adjust the
7278 offset when we know the correct value. */
7279 phdr_adjust_num
= iehdr
->e_phnum
;
7280 phdr_adjust_seg
= map
;
7283 if (map
->includes_filehdr
)
7285 bfd_vma align
= (bfd_vma
) 1 << matching_lma
->alignment_power
;
7286 map
->p_paddr
-= iehdr
->e_ehsize
;
7287 /* We've subtracted off the size of headers from the
7288 first section lma, but there may have been some
7289 alignment padding before that section too. Try to
7290 account for that by adjusting the segment lma down to
7291 the same alignment. */
7292 if (segment
->p_align
!= 0 && segment
->p_align
< align
)
7293 align
= segment
->p_align
;
7294 map
->p_paddr
&= -(align
* opb
);
7298 /* Step Three: Loop over the sections again, this time assigning
7299 those that fit to the current segment and removing them from the
7300 sections array; but making sure not to leave large gaps. Once all
7301 possible sections have been assigned to the current segment it is
7302 added to the list of built segments and if sections still remain
7303 to be assigned, a new segment is constructed before repeating
7309 suggested_lma
= NULL
;
7311 /* Fill the current segment with sections that fit. */
7312 for (j
= 0; j
< section_count
; j
++)
7314 section
= sections
[j
];
7316 if (section
== NULL
)
7319 output_section
= section
->output_section
;
7321 BFD_ASSERT (output_section
!= NULL
);
7323 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
,
7325 || IS_COREFILE_NOTE (segment
, section
))
7327 if (map
->count
== 0)
7329 /* If the first section in a segment does not start at
7330 the beginning of the segment, then something is
7332 if (align_power (map
->p_paddr
7333 + (map
->includes_filehdr
7334 ? iehdr
->e_ehsize
: 0)
7335 + (map
->includes_phdrs
7336 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7338 output_section
->alignment_power
* opb
)
7339 != output_section
->lma
* opb
)
7346 prev_sec
= map
->sections
[map
->count
- 1];
7348 /* If the gap between the end of the previous section
7349 and the start of this section is more than
7350 maxpagesize then we need to start a new segment. */
7351 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7353 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7354 || (prev_sec
->lma
+ prev_sec
->size
7355 > output_section
->lma
))
7357 if (suggested_lma
== NULL
)
7358 suggested_lma
= output_section
;
7364 map
->sections
[map
->count
++] = output_section
;
7367 if (segment
->p_type
== PT_LOAD
)
7368 section
->segment_mark
= TRUE
;
7370 else if (suggested_lma
== NULL
)
7371 suggested_lma
= output_section
;
7374 /* PR 23932. A corrupt input file may contain sections that cannot
7375 be assigned to any segment - because for example they have a
7376 negative size - or segments that do not contain any sections.
7377 But there are also valid reasons why a segment can be empty.
7378 So allow a count of zero. */
7380 /* Add the current segment to the list of built segments. */
7381 *pointer_to_map
= map
;
7382 pointer_to_map
= &map
->next
;
7384 if (isec
< section_count
)
7386 /* We still have not allocated all of the sections to
7387 segments. Create a new segment here, initialise it
7388 and carry on looping. */
7389 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7390 amt
+= section_count
* sizeof (asection
*);
7391 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7398 /* Initialise the fields of the segment map. Set the physical
7399 physical address to the LMA of the first section that has
7400 not yet been assigned. */
7402 map
->p_type
= segment
->p_type
;
7403 map
->p_flags
= segment
->p_flags
;
7404 map
->p_flags_valid
= 1;
7405 map
->p_paddr
= suggested_lma
->lma
* opb
;
7406 map
->p_paddr_valid
= p_paddr_valid
;
7407 map
->includes_filehdr
= 0;
7408 map
->includes_phdrs
= 0;
7413 bfd_set_error (bfd_error_sorry
);
7417 while (isec
< section_count
);
7422 elf_seg_map (obfd
) = map_first
;
7424 /* If we had to estimate the number of program headers that were
7425 going to be needed, then check our estimate now and adjust
7426 the offset if necessary. */
7427 if (phdr_adjust_seg
!= NULL
)
7431 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7434 if (count
> phdr_adjust_num
)
7435 phdr_adjust_seg
->p_paddr
7436 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7438 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
7439 if (map
->p_type
== PT_PHDR
)
7442 = phdr_adjust_seg
->includes_filehdr
? iehdr
->e_ehsize
: 0;
7443 map
->p_paddr
= phdr_adjust_seg
->p_paddr
+ adjust
;
7450 #undef IS_CONTAINED_BY_VMA
7451 #undef IS_CONTAINED_BY_LMA
7453 #undef IS_COREFILE_NOTE
7454 #undef IS_SOLARIS_PT_INTERP
7455 #undef IS_SECTION_IN_INPUT_SEGMENT
7456 #undef INCLUDE_SECTION_IN_SEGMENT
7457 #undef SEGMENT_AFTER_SEGMENT
7458 #undef SEGMENT_OVERLAPS
7462 /* Copy ELF program header information. */
7465 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7467 Elf_Internal_Ehdr
*iehdr
;
7468 struct elf_segment_map
*map
;
7469 struct elf_segment_map
*map_first
;
7470 struct elf_segment_map
**pointer_to_map
;
7471 Elf_Internal_Phdr
*segment
;
7473 unsigned int num_segments
;
7474 bfd_boolean phdr_included
= FALSE
;
7475 bfd_boolean p_paddr_valid
;
7476 unsigned int opb
= bfd_octets_per_byte (ibfd
, NULL
);
7478 iehdr
= elf_elfheader (ibfd
);
7481 pointer_to_map
= &map_first
;
7483 /* If all the segment p_paddr fields are zero, don't set
7484 map->p_paddr_valid. */
7485 p_paddr_valid
= FALSE
;
7486 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7487 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7490 if (segment
->p_paddr
!= 0)
7492 p_paddr_valid
= TRUE
;
7496 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7501 unsigned int section_count
;
7503 Elf_Internal_Shdr
*this_hdr
;
7504 asection
*first_section
= NULL
;
7505 asection
*lowest_section
;
7507 /* Compute how many sections are in this segment. */
7508 for (section
= ibfd
->sections
, section_count
= 0;
7510 section
= section
->next
)
7512 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7513 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7515 if (first_section
== NULL
)
7516 first_section
= section
;
7521 /* Allocate a segment map big enough to contain
7522 all of the sections we have selected. */
7523 amt
= sizeof (struct elf_segment_map
) - sizeof (asection
*);
7524 amt
+= section_count
* sizeof (asection
*);
7525 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7529 /* Initialize the fields of the output segment map with the
7532 map
->p_type
= segment
->p_type
;
7533 map
->p_flags
= segment
->p_flags
;
7534 map
->p_flags_valid
= 1;
7535 map
->p_paddr
= segment
->p_paddr
;
7536 map
->p_paddr_valid
= p_paddr_valid
;
7537 map
->p_align
= segment
->p_align
;
7538 map
->p_align_valid
= 1;
7539 map
->p_vaddr_offset
= 0;
7541 if (map
->p_type
== PT_GNU_RELRO
7542 || map
->p_type
== PT_GNU_STACK
)
7544 /* The PT_GNU_RELRO segment may contain the first a few
7545 bytes in the .got.plt section even if the whole .got.plt
7546 section isn't in the PT_GNU_RELRO segment. We won't
7547 change the size of the PT_GNU_RELRO segment.
7548 Similarly, PT_GNU_STACK size is significant on uclinux
7550 map
->p_size
= segment
->p_memsz
;
7551 map
->p_size_valid
= 1;
7554 /* Determine if this segment contains the ELF file header
7555 and if it contains the program headers themselves. */
7556 map
->includes_filehdr
= (segment
->p_offset
== 0
7557 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7559 map
->includes_phdrs
= 0;
7560 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7562 map
->includes_phdrs
=
7563 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7564 && (segment
->p_offset
+ segment
->p_filesz
7565 >= ((bfd_vma
) iehdr
->e_phoff
7566 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7568 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7569 phdr_included
= TRUE
;
7572 lowest_section
= NULL
;
7573 if (section_count
!= 0)
7575 unsigned int isec
= 0;
7577 for (section
= first_section
;
7579 section
= section
->next
)
7581 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7582 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7584 map
->sections
[isec
++] = section
->output_section
;
7585 if ((section
->flags
& SEC_ALLOC
) != 0)
7589 if (lowest_section
== NULL
7590 || section
->lma
< lowest_section
->lma
)
7591 lowest_section
= section
;
7593 /* Section lmas are set up from PT_LOAD header
7594 p_paddr in _bfd_elf_make_section_from_shdr.
7595 If this header has a p_paddr that disagrees
7596 with the section lma, flag the p_paddr as
7598 if ((section
->flags
& SEC_LOAD
) != 0)
7599 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7601 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7602 if (section
->lma
* opb
- segment
->p_paddr
!= seg_off
)
7603 map
->p_paddr_valid
= FALSE
;
7605 if (isec
== section_count
)
7611 if (section_count
== 0)
7612 map
->p_vaddr_offset
= segment
->p_vaddr
/ opb
;
7613 else if (map
->p_paddr_valid
)
7615 /* Account for padding before the first section in the segment. */
7616 bfd_vma hdr_size
= 0;
7617 if (map
->includes_filehdr
)
7618 hdr_size
= iehdr
->e_ehsize
;
7619 if (map
->includes_phdrs
)
7620 hdr_size
+= iehdr
->e_phnum
* iehdr
->e_phentsize
;
7622 map
->p_vaddr_offset
= ((map
->p_paddr
+ hdr_size
) / opb
7623 - (lowest_section
? lowest_section
->lma
: 0));
7626 map
->count
= section_count
;
7627 *pointer_to_map
= map
;
7628 pointer_to_map
= &map
->next
;
7631 elf_seg_map (obfd
) = map_first
;
7635 /* Copy private BFD data. This copies or rewrites ELF program header
7639 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7641 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7642 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7645 if (elf_tdata (ibfd
)->phdr
== NULL
)
7648 if (ibfd
->xvec
== obfd
->xvec
)
7650 /* Check to see if any sections in the input BFD
7651 covered by ELF program header have changed. */
7652 Elf_Internal_Phdr
*segment
;
7653 asection
*section
, *osec
;
7654 unsigned int i
, num_segments
;
7655 Elf_Internal_Shdr
*this_hdr
;
7656 const struct elf_backend_data
*bed
;
7658 bed
= get_elf_backend_data (ibfd
);
7660 /* Regenerate the segment map if p_paddr is set to 0. */
7661 if (bed
->want_p_paddr_set_to_zero
)
7664 /* Initialize the segment mark field. */
7665 for (section
= obfd
->sections
; section
!= NULL
;
7666 section
= section
->next
)
7667 section
->segment_mark
= FALSE
;
7669 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7670 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7674 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7675 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7676 which severly confuses things, so always regenerate the segment
7677 map in this case. */
7678 if (segment
->p_paddr
== 0
7679 && segment
->p_memsz
== 0
7680 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7683 for (section
= ibfd
->sections
;
7684 section
!= NULL
; section
= section
->next
)
7686 /* We mark the output section so that we know it comes
7687 from the input BFD. */
7688 osec
= section
->output_section
;
7690 osec
->segment_mark
= TRUE
;
7692 /* Check if this section is covered by the segment. */
7693 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7694 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7696 /* FIXME: Check if its output section is changed or
7697 removed. What else do we need to check? */
7699 || section
->flags
!= osec
->flags
7700 || section
->lma
!= osec
->lma
7701 || section
->vma
!= osec
->vma
7702 || section
->size
!= osec
->size
7703 || section
->rawsize
!= osec
->rawsize
7704 || section
->alignment_power
!= osec
->alignment_power
)
7710 /* Check to see if any output section do not come from the
7712 for (section
= obfd
->sections
; section
!= NULL
;
7713 section
= section
->next
)
7715 if (!section
->segment_mark
)
7718 section
->segment_mark
= FALSE
;
7721 return copy_elf_program_header (ibfd
, obfd
);
7725 if (ibfd
->xvec
== obfd
->xvec
)
7727 /* When rewriting program header, set the output maxpagesize to
7728 the maximum alignment of input PT_LOAD segments. */
7729 Elf_Internal_Phdr
*segment
;
7731 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7732 bfd_vma maxpagesize
= 0;
7734 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7737 if (segment
->p_type
== PT_LOAD
7738 && maxpagesize
< segment
->p_align
)
7740 /* PR 17512: file: f17299af. */
7741 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7742 /* xgettext:c-format */
7743 _bfd_error_handler (_("%pB: warning: segment alignment of %#"
7744 PRIx64
" is too large"),
7745 ibfd
, (uint64_t) segment
->p_align
);
7747 maxpagesize
= segment
->p_align
;
7750 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7751 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7754 return rewrite_elf_program_header (ibfd
, obfd
);
7757 /* Initialize private output section information from input section. */
7760 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7764 struct bfd_link_info
*link_info
)
7767 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7768 bfd_boolean final_link
= (link_info
!= NULL
7769 && !bfd_link_relocatable (link_info
));
7771 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7772 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7775 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7777 /* If this is a known ABI section, ELF section type and flags may
7778 have been set up when OSEC was created. For normal sections we
7779 allow the user to override the type and flags other than
7780 SHF_MASKOS and SHF_MASKPROC. */
7781 if (elf_section_type (osec
) == SHT_PROGBITS
7782 || elf_section_type (osec
) == SHT_NOTE
7783 || elf_section_type (osec
) == SHT_NOBITS
)
7784 elf_section_type (osec
) = SHT_NULL
;
7785 /* For objcopy and relocatable link, copy the ELF section type from
7786 the input file if the BFD section flags are the same. (If they
7787 are different the user may be doing something like
7788 "objcopy --set-section-flags .text=alloc,data".) For a final
7789 link allow some flags that the linker clears to differ. */
7790 if (elf_section_type (osec
) == SHT_NULL
7791 && (osec
->flags
== isec
->flags
7793 && ((osec
->flags
^ isec
->flags
)
7794 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7795 elf_section_type (osec
) = elf_section_type (isec
);
7797 /* FIXME: Is this correct for all OS/PROC specific flags? */
7798 elf_section_flags (osec
) = (elf_section_flags (isec
)
7799 & (SHF_MASKOS
| SHF_MASKPROC
));
7801 /* Copy sh_info from input for mbind section. */
7802 if ((elf_tdata (ibfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
) != 0
7803 && elf_section_flags (isec
) & SHF_GNU_MBIND
)
7804 elf_section_data (osec
)->this_hdr
.sh_info
7805 = elf_section_data (isec
)->this_hdr
.sh_info
;
7807 /* Set things up for objcopy and relocatable link. The output
7808 SHT_GROUP section will have its elf_next_in_group pointing back
7809 to the input group members. Ignore linker created group section.
7810 See elfNN_ia64_object_p in elfxx-ia64.c. */
7811 if ((link_info
== NULL
7812 || !link_info
->resolve_section_groups
)
7813 && (elf_sec_group (isec
) == NULL
7814 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7816 if (elf_section_flags (isec
) & SHF_GROUP
)
7817 elf_section_flags (osec
) |= SHF_GROUP
;
7818 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7819 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7822 /* If not decompress, preserve SHF_COMPRESSED. */
7823 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7824 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7827 ihdr
= &elf_section_data (isec
)->this_hdr
;
7829 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7830 don't use the output section of the linked-to section since it
7831 may be NULL at this point. */
7832 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7834 ohdr
= &elf_section_data (osec
)->this_hdr
;
7835 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7836 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7839 osec
->use_rela_p
= isec
->use_rela_p
;
7844 /* Copy private section information. This copies over the entsize
7845 field, and sometimes the info field. */
7848 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7853 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7855 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7856 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7859 ihdr
= &elf_section_data (isec
)->this_hdr
;
7860 ohdr
= &elf_section_data (osec
)->this_hdr
;
7862 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7864 if (ihdr
->sh_type
== SHT_SYMTAB
7865 || ihdr
->sh_type
== SHT_DYNSYM
7866 || ihdr
->sh_type
== SHT_GNU_verneed
7867 || ihdr
->sh_type
== SHT_GNU_verdef
)
7868 ohdr
->sh_info
= ihdr
->sh_info
;
7870 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7874 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7875 necessary if we are removing either the SHT_GROUP section or any of
7876 the group member sections. DISCARDED is the value that a section's
7877 output_section has if the section will be discarded, NULL when this
7878 function is called from objcopy, bfd_abs_section_ptr when called
7882 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7886 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7887 if (elf_section_type (isec
) == SHT_GROUP
)
7889 asection
*first
= elf_next_in_group (isec
);
7890 asection
*s
= first
;
7891 bfd_size_type removed
= 0;
7895 /* If this member section is being output but the
7896 SHT_GROUP section is not, then clear the group info
7897 set up by _bfd_elf_copy_private_section_data. */
7898 if (s
->output_section
!= discarded
7899 && isec
->output_section
== discarded
)
7901 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7902 elf_group_name (s
->output_section
) = NULL
;
7906 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
7907 if (s
->output_section
== discarded
7908 && isec
->output_section
!= discarded
)
7910 /* Conversely, if the member section is not being
7911 output but the SHT_GROUP section is, then adjust
7914 if (elf_sec
->rel
.hdr
!= NULL
7915 && (elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7917 if (elf_sec
->rela
.hdr
!= NULL
7918 && (elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0)
7923 /* Also adjust for zero-sized relocation member
7925 if (elf_sec
->rel
.hdr
!= NULL
7926 && elf_sec
->rel
.hdr
->sh_size
== 0)
7928 if (elf_sec
->rela
.hdr
!= NULL
7929 && elf_sec
->rela
.hdr
->sh_size
== 0)
7933 s
= elf_next_in_group (s
);
7939 if (discarded
!= NULL
)
7941 /* If we've been called for ld -r, then we need to
7942 adjust the input section size. */
7943 if (isec
->rawsize
== 0)
7944 isec
->rawsize
= isec
->size
;
7945 isec
->size
= isec
->rawsize
- removed
;
7946 if (isec
->size
<= 4)
7949 isec
->flags
|= SEC_EXCLUDE
;
7954 /* Adjust the output section size when called from
7956 isec
->output_section
->size
-= removed
;
7957 if (isec
->output_section
->size
<= 4)
7959 isec
->output_section
->size
= 0;
7960 isec
->output_section
->flags
|= SEC_EXCLUDE
;
7969 /* Copy private header information. */
7972 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7974 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7975 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7978 /* Copy over private BFD data if it has not already been copied.
7979 This must be done here, rather than in the copy_private_bfd_data
7980 entry point, because the latter is called after the section
7981 contents have been set, which means that the program headers have
7982 already been worked out. */
7983 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7985 if (! copy_private_bfd_data (ibfd
, obfd
))
7989 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7992 /* Copy private symbol information. If this symbol is in a section
7993 which we did not map into a BFD section, try to map the section
7994 index correctly. We use special macro definitions for the mapped
7995 section indices; these definitions are interpreted by the
7996 swap_out_syms function. */
7998 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7999 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
8000 #define MAP_STRTAB (SHN_HIOS + 3)
8001 #define MAP_SHSTRTAB (SHN_HIOS + 4)
8002 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
8005 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
8010 elf_symbol_type
*isym
, *osym
;
8012 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
8013 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
8016 isym
= elf_symbol_from (isymarg
);
8017 osym
= elf_symbol_from (osymarg
);
8020 && isym
->internal_elf_sym
.st_shndx
!= 0
8022 && bfd_is_abs_section (isym
->symbol
.section
))
8026 shndx
= isym
->internal_elf_sym
.st_shndx
;
8027 if (shndx
== elf_onesymtab (ibfd
))
8028 shndx
= MAP_ONESYMTAB
;
8029 else if (shndx
== elf_dynsymtab (ibfd
))
8030 shndx
= MAP_DYNSYMTAB
;
8031 else if (shndx
== elf_strtab_sec (ibfd
))
8033 else if (shndx
== elf_shstrtab_sec (ibfd
))
8034 shndx
= MAP_SHSTRTAB
;
8035 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
8036 shndx
= MAP_SYM_SHNDX
;
8037 osym
->internal_elf_sym
.st_shndx
= shndx
;
8043 /* Swap out the symbols. */
8046 swap_out_syms (bfd
*abfd
,
8047 struct elf_strtab_hash
**sttp
,
8050 const struct elf_backend_data
*bed
;
8051 unsigned int symcount
;
8053 struct elf_strtab_hash
*stt
;
8054 Elf_Internal_Shdr
*symtab_hdr
;
8055 Elf_Internal_Shdr
*symtab_shndx_hdr
;
8056 Elf_Internal_Shdr
*symstrtab_hdr
;
8057 struct elf_sym_strtab
*symstrtab
;
8058 bfd_byte
*outbound_syms
;
8059 bfd_byte
*outbound_shndx
;
8060 unsigned long outbound_syms_index
;
8061 unsigned long outbound_shndx_index
;
8063 unsigned int num_locals
;
8065 bfd_boolean name_local_sections
;
8067 if (!elf_map_symbols (abfd
, &num_locals
))
8070 /* Dump out the symtabs. */
8071 stt
= _bfd_elf_strtab_init ();
8075 bed
= get_elf_backend_data (abfd
);
8076 symcount
= bfd_get_symcount (abfd
);
8077 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8078 symtab_hdr
->sh_type
= SHT_SYMTAB
;
8079 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
8080 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
8081 symtab_hdr
->sh_info
= num_locals
+ 1;
8082 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
8084 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
8085 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8087 /* Allocate buffer to swap out the .strtab section. */
8088 if (_bfd_mul_overflow (symcount
+ 1, sizeof (*symstrtab
), &amt
)
8089 || (symstrtab
= (struct elf_sym_strtab
*) bfd_malloc (amt
)) == NULL
)
8091 bfd_set_error (bfd_error_no_memory
);
8092 _bfd_elf_strtab_free (stt
);
8096 if (_bfd_mul_overflow (symcount
+ 1, bed
->s
->sizeof_sym
, &amt
)
8097 || (outbound_syms
= (bfd_byte
*) bfd_alloc (abfd
, amt
)) == NULL
)
8100 bfd_set_error (bfd_error_no_memory
);
8103 _bfd_elf_strtab_free (stt
);
8106 symtab_hdr
->contents
= outbound_syms
;
8107 outbound_syms_index
= 0;
8109 outbound_shndx
= NULL
;
8110 outbound_shndx_index
= 0;
8112 if (elf_symtab_shndx_list (abfd
))
8114 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
8115 if (symtab_shndx_hdr
->sh_name
!= 0)
8117 if (_bfd_mul_overflow (symcount
+ 1,
8118 sizeof (Elf_External_Sym_Shndx
), &amt
))
8120 outbound_shndx
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
8121 if (outbound_shndx
== NULL
)
8124 symtab_shndx_hdr
->contents
= outbound_shndx
;
8125 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
8126 symtab_shndx_hdr
->sh_size
= amt
;
8127 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
8128 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
8130 /* FIXME: What about any other headers in the list ? */
8133 /* Now generate the data (for "contents"). */
8135 /* Fill in zeroth symbol and swap it out. */
8136 Elf_Internal_Sym sym
;
8142 sym
.st_shndx
= SHN_UNDEF
;
8143 sym
.st_target_internal
= 0;
8144 symstrtab
[0].sym
= sym
;
8145 symstrtab
[0].dest_index
= outbound_syms_index
;
8146 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
8147 outbound_syms_index
++;
8148 if (outbound_shndx
!= NULL
)
8149 outbound_shndx_index
++;
8153 = (bed
->elf_backend_name_local_section_symbols
8154 && bed
->elf_backend_name_local_section_symbols (abfd
));
8156 syms
= bfd_get_outsymbols (abfd
);
8157 for (idx
= 0; idx
< symcount
;)
8159 Elf_Internal_Sym sym
;
8160 bfd_vma value
= syms
[idx
]->value
;
8161 elf_symbol_type
*type_ptr
;
8162 flagword flags
= syms
[idx
]->flags
;
8165 if (!name_local_sections
8166 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
8168 /* Local section symbols have no name. */
8169 sym
.st_name
= (unsigned long) -1;
8173 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8174 to get the final offset for st_name. */
8176 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
8178 if (sym
.st_name
== (unsigned long) -1)
8182 type_ptr
= elf_symbol_from (syms
[idx
]);
8184 if ((flags
& BSF_SECTION_SYM
) == 0
8185 && bfd_is_com_section (syms
[idx
]->section
))
8187 /* ELF common symbols put the alignment into the `value' field,
8188 and the size into the `size' field. This is backwards from
8189 how BFD handles it, so reverse it here. */
8190 sym
.st_size
= value
;
8191 if (type_ptr
== NULL
8192 || type_ptr
->internal_elf_sym
.st_value
== 0)
8193 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
8195 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
8196 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
8197 (abfd
, syms
[idx
]->section
);
8201 asection
*sec
= syms
[idx
]->section
;
8204 if (sec
->output_section
)
8206 value
+= sec
->output_offset
;
8207 sec
= sec
->output_section
;
8210 /* Don't add in the section vma for relocatable output. */
8211 if (! relocatable_p
)
8213 sym
.st_value
= value
;
8214 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
8216 if (bfd_is_abs_section (sec
)
8218 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
8220 /* This symbol is in a real ELF section which we did
8221 not create as a BFD section. Undo the mapping done
8222 by copy_private_symbol_data. */
8223 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
8227 shndx
= elf_onesymtab (abfd
);
8230 shndx
= elf_dynsymtab (abfd
);
8233 shndx
= elf_strtab_sec (abfd
);
8236 shndx
= elf_shstrtab_sec (abfd
);
8239 if (elf_symtab_shndx_list (abfd
))
8240 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
8247 if (shndx
>= SHN_LOPROC
&& shndx
<= SHN_HIOS
)
8249 if (bed
->symbol_section_index
)
8250 shndx
= bed
->symbol_section_index (abfd
, type_ptr
);
8251 /* Otherwise just leave the index alone. */
8255 if (shndx
> SHN_HIOS
&& shndx
< SHN_HIRESERVE
)
8256 _bfd_error_handler (_("%pB: \
8257 Unable to handle section index %x in ELF symbol. Using ABS instead."),
8266 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
8268 if (shndx
== SHN_BAD
)
8272 /* Writing this would be a hell of a lot easier if
8273 we had some decent documentation on bfd, and
8274 knew what to expect of the library, and what to
8275 demand of applications. For example, it
8276 appears that `objcopy' might not set the
8277 section of a symbol to be a section that is
8278 actually in the output file. */
8279 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
8281 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
8282 if (shndx
== SHN_BAD
)
8284 /* xgettext:c-format */
8286 (_("unable to find equivalent output section"
8287 " for symbol '%s' from section '%s'"),
8288 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
8290 bfd_set_error (bfd_error_invalid_operation
);
8296 sym
.st_shndx
= shndx
;
8299 if ((flags
& BSF_THREAD_LOCAL
) != 0)
8301 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
8302 type
= STT_GNU_IFUNC
;
8303 else if ((flags
& BSF_FUNCTION
) != 0)
8305 else if ((flags
& BSF_OBJECT
) != 0)
8307 else if ((flags
& BSF_RELC
) != 0)
8309 else if ((flags
& BSF_SRELC
) != 0)
8314 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
8317 /* Processor-specific types. */
8318 if (type_ptr
!= NULL
8319 && bed
->elf_backend_get_symbol_type
)
8320 type
= ((*bed
->elf_backend_get_symbol_type
)
8321 (&type_ptr
->internal_elf_sym
, type
));
8323 if (flags
& BSF_SECTION_SYM
)
8325 if (flags
& BSF_GLOBAL
)
8326 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
8328 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
8330 else if (bfd_is_com_section (syms
[idx
]->section
))
8332 if (type
!= STT_TLS
)
8334 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
8335 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
8336 ? STT_COMMON
: STT_OBJECT
);
8338 type
= ((flags
& BSF_ELF_COMMON
) != 0
8339 ? STT_COMMON
: STT_OBJECT
);
8341 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
8343 else if (bfd_is_und_section (syms
[idx
]->section
))
8344 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
8348 else if (flags
& BSF_FILE
)
8349 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
8352 int bind
= STB_LOCAL
;
8354 if (flags
& BSF_LOCAL
)
8356 else if (flags
& BSF_GNU_UNIQUE
)
8357 bind
= STB_GNU_UNIQUE
;
8358 else if (flags
& BSF_WEAK
)
8360 else if (flags
& BSF_GLOBAL
)
8363 sym
.st_info
= ELF_ST_INFO (bind
, type
);
8366 if (type_ptr
!= NULL
)
8368 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
8369 sym
.st_target_internal
8370 = type_ptr
->internal_elf_sym
.st_target_internal
;
8375 sym
.st_target_internal
= 0;
8379 symstrtab
[idx
].sym
= sym
;
8380 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8381 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8383 outbound_syms_index
++;
8384 if (outbound_shndx
!= NULL
)
8385 outbound_shndx_index
++;
8388 /* Finalize the .strtab section. */
8389 _bfd_elf_strtab_finalize (stt
);
8391 /* Swap out the .strtab section. */
8392 for (idx
= 0; idx
<= symcount
; idx
++)
8394 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8395 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8396 elfsym
->sym
.st_name
= 0;
8398 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8399 elfsym
->sym
.st_name
);
8400 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8402 + (elfsym
->dest_index
8403 * bed
->s
->sizeof_sym
)),
8405 + (elfsym
->destshndx_index
8406 * sizeof (Elf_External_Sym_Shndx
))));
8411 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8412 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8413 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8414 symstrtab_hdr
->sh_addr
= 0;
8415 symstrtab_hdr
->sh_entsize
= 0;
8416 symstrtab_hdr
->sh_link
= 0;
8417 symstrtab_hdr
->sh_info
= 0;
8418 symstrtab_hdr
->sh_addralign
= 1;
8423 /* Return the number of bytes required to hold the symtab vector.
8425 Note that we base it on the count plus 1, since we will null terminate
8426 the vector allocated based on this size. However, the ELF symbol table
8427 always has a dummy entry as symbol #0, so it ends up even. */
8430 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8432 bfd_size_type symcount
;
8434 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8436 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8437 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
8439 bfd_set_error (bfd_error_file_too_big
);
8442 symtab_size
= symcount
* (sizeof (asymbol
*));
8444 symtab_size
= sizeof (asymbol
*);
8445 else if (!bfd_write_p (abfd
))
8447 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8449 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
8451 bfd_set_error (bfd_error_file_truncated
);
8460 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8462 bfd_size_type symcount
;
8464 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8466 if (elf_dynsymtab (abfd
) == 0)
8468 bfd_set_error (bfd_error_invalid_operation
);
8472 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8473 if (symcount
> LONG_MAX
/ sizeof (asymbol
*))
8475 bfd_set_error (bfd_error_file_too_big
);
8478 symtab_size
= symcount
* (sizeof (asymbol
*));
8480 symtab_size
= sizeof (asymbol
*);
8481 else if (!bfd_write_p (abfd
))
8483 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8485 if (filesize
!= 0 && (unsigned long) symtab_size
> filesize
)
8487 bfd_set_error (bfd_error_file_truncated
);
8496 _bfd_elf_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
8498 if (asect
->reloc_count
!= 0 && !bfd_write_p (abfd
))
8500 /* Sanity check reloc section size. */
8501 struct bfd_elf_section_data
*d
= elf_section_data (asect
);
8502 Elf_Internal_Shdr
*rel_hdr
= &d
->this_hdr
;
8503 bfd_size_type ext_rel_size
= rel_hdr
->sh_size
;
8504 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8506 if (filesize
!= 0 && ext_rel_size
> filesize
)
8508 bfd_set_error (bfd_error_file_truncated
);
8513 #if SIZEOF_LONG == SIZEOF_INT
8514 if (asect
->reloc_count
>= LONG_MAX
/ sizeof (arelent
*))
8516 bfd_set_error (bfd_error_file_too_big
);
8520 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8523 /* Canonicalize the relocs. */
8526 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8533 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8535 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8538 tblptr
= section
->relocation
;
8539 for (i
= 0; i
< section
->reloc_count
; i
++)
8540 *relptr
++ = tblptr
++;
8544 return section
->reloc_count
;
8548 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8550 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8551 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8554 abfd
->symcount
= symcount
;
8559 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8560 asymbol
**allocation
)
8562 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8563 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8566 abfd
->dynsymcount
= symcount
;
8570 /* Return the size required for the dynamic reloc entries. Any loadable
8571 section that was actually installed in the BFD, and has type SHT_REL
8572 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8573 dynamic reloc section. */
8576 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8578 bfd_size_type count
, ext_rel_size
;
8581 if (elf_dynsymtab (abfd
) == 0)
8583 bfd_set_error (bfd_error_invalid_operation
);
8589 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8590 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8591 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8592 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8594 ext_rel_size
+= s
->size
;
8595 if (ext_rel_size
< s
->size
)
8597 bfd_set_error (bfd_error_file_truncated
);
8600 count
+= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8601 if (count
> LONG_MAX
/ sizeof (arelent
*))
8603 bfd_set_error (bfd_error_file_too_big
);
8607 if (count
> 1 && !bfd_write_p (abfd
))
8609 /* Sanity check reloc section sizes. */
8610 ufile_ptr filesize
= bfd_get_file_size (abfd
);
8611 if (filesize
!= 0 && ext_rel_size
> filesize
)
8613 bfd_set_error (bfd_error_file_truncated
);
8617 return count
* sizeof (arelent
*);
8620 /* Canonicalize the dynamic relocation entries. Note that we return the
8621 dynamic relocations as a single block, although they are actually
8622 associated with particular sections; the interface, which was
8623 designed for SunOS style shared libraries, expects that there is only
8624 one set of dynamic relocs. Any loadable section that was actually
8625 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8626 dynamic symbol table, is considered to be a dynamic reloc section. */
8629 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8633 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8637 if (elf_dynsymtab (abfd
) == 0)
8639 bfd_set_error (bfd_error_invalid_operation
);
8643 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8645 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8647 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8648 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8649 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8654 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8656 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8658 for (i
= 0; i
< count
; i
++)
8669 /* Read in the version information. */
8672 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8674 bfd_byte
*contents
= NULL
;
8675 unsigned int freeidx
= 0;
8678 if (elf_dynverref (abfd
) != 0)
8680 Elf_Internal_Shdr
*hdr
;
8681 Elf_External_Verneed
*everneed
;
8682 Elf_Internal_Verneed
*iverneed
;
8684 bfd_byte
*contents_end
;
8686 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8688 if (hdr
->sh_info
== 0
8689 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8691 error_return_bad_verref
:
8693 (_("%pB: .gnu.version_r invalid entry"), abfd
);
8694 bfd_set_error (bfd_error_bad_value
);
8695 error_return_verref
:
8696 elf_tdata (abfd
)->verref
= NULL
;
8697 elf_tdata (abfd
)->cverrefs
= 0;
8701 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8702 goto error_return_verref
;
8703 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8704 if (contents
== NULL
)
8705 goto error_return_verref
;
8707 if (_bfd_mul_overflow (hdr
->sh_info
, sizeof (Elf_Internal_Verneed
), &amt
))
8709 bfd_set_error (bfd_error_file_too_big
);
8710 goto error_return_verref
;
8712 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*) bfd_alloc (abfd
, amt
);
8713 if (elf_tdata (abfd
)->verref
== NULL
)
8714 goto error_return_verref
;
8716 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8717 == sizeof (Elf_External_Vernaux
));
8718 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8719 everneed
= (Elf_External_Verneed
*) contents
;
8720 iverneed
= elf_tdata (abfd
)->verref
;
8721 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8723 Elf_External_Vernaux
*evernaux
;
8724 Elf_Internal_Vernaux
*ivernaux
;
8727 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8729 iverneed
->vn_bfd
= abfd
;
8731 iverneed
->vn_filename
=
8732 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8734 if (iverneed
->vn_filename
== NULL
)
8735 goto error_return_bad_verref
;
8737 if (iverneed
->vn_cnt
== 0)
8738 iverneed
->vn_auxptr
= NULL
;
8741 if (_bfd_mul_overflow (iverneed
->vn_cnt
,
8742 sizeof (Elf_Internal_Vernaux
), &amt
))
8744 bfd_set_error (bfd_error_file_too_big
);
8745 goto error_return_verref
;
8747 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8748 bfd_alloc (abfd
, amt
);
8749 if (iverneed
->vn_auxptr
== NULL
)
8750 goto error_return_verref
;
8753 if (iverneed
->vn_aux
8754 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8755 goto error_return_bad_verref
;
8757 evernaux
= ((Elf_External_Vernaux
*)
8758 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8759 ivernaux
= iverneed
->vn_auxptr
;
8760 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8762 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8764 ivernaux
->vna_nodename
=
8765 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8766 ivernaux
->vna_name
);
8767 if (ivernaux
->vna_nodename
== NULL
)
8768 goto error_return_bad_verref
;
8770 if (ivernaux
->vna_other
> freeidx
)
8771 freeidx
= ivernaux
->vna_other
;
8773 ivernaux
->vna_nextptr
= NULL
;
8774 if (ivernaux
->vna_next
== 0)
8776 iverneed
->vn_cnt
= j
+ 1;
8779 if (j
+ 1 < iverneed
->vn_cnt
)
8780 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8782 if (ivernaux
->vna_next
8783 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8784 goto error_return_bad_verref
;
8786 evernaux
= ((Elf_External_Vernaux
*)
8787 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8790 iverneed
->vn_nextref
= NULL
;
8791 if (iverneed
->vn_next
== 0)
8793 if (i
+ 1 < hdr
->sh_info
)
8794 iverneed
->vn_nextref
= iverneed
+ 1;
8796 if (iverneed
->vn_next
8797 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8798 goto error_return_bad_verref
;
8800 everneed
= ((Elf_External_Verneed
*)
8801 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8803 elf_tdata (abfd
)->cverrefs
= i
;
8809 if (elf_dynverdef (abfd
) != 0)
8811 Elf_Internal_Shdr
*hdr
;
8812 Elf_External_Verdef
*everdef
;
8813 Elf_Internal_Verdef
*iverdef
;
8814 Elf_Internal_Verdef
*iverdefarr
;
8815 Elf_Internal_Verdef iverdefmem
;
8817 unsigned int maxidx
;
8818 bfd_byte
*contents_end_def
, *contents_end_aux
;
8820 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8822 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8824 error_return_bad_verdef
:
8826 (_("%pB: .gnu.version_d invalid entry"), abfd
);
8827 bfd_set_error (bfd_error_bad_value
);
8828 error_return_verdef
:
8829 elf_tdata (abfd
)->verdef
= NULL
;
8830 elf_tdata (abfd
)->cverdefs
= 0;
8834 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0)
8835 goto error_return_verdef
;
8836 contents
= _bfd_malloc_and_read (abfd
, hdr
->sh_size
, hdr
->sh_size
);
8837 if (contents
== NULL
)
8838 goto error_return_verdef
;
8840 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8841 >= sizeof (Elf_External_Verdaux
));
8842 contents_end_def
= contents
+ hdr
->sh_size
8843 - sizeof (Elf_External_Verdef
);
8844 contents_end_aux
= contents
+ hdr
->sh_size
8845 - sizeof (Elf_External_Verdaux
);
8847 /* We know the number of entries in the section but not the maximum
8848 index. Therefore we have to run through all entries and find
8850 everdef
= (Elf_External_Verdef
*) contents
;
8852 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8854 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8856 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8857 goto error_return_bad_verdef
;
8858 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8859 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8861 if (iverdefmem
.vd_next
== 0)
8864 if (iverdefmem
.vd_next
8865 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8866 goto error_return_bad_verdef
;
8868 everdef
= ((Elf_External_Verdef
*)
8869 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8872 if (default_imported_symver
)
8874 if (freeidx
> maxidx
)
8879 if (_bfd_mul_overflow (maxidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8881 bfd_set_error (bfd_error_file_too_big
);
8882 goto error_return_verdef
;
8884 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8885 if (elf_tdata (abfd
)->verdef
== NULL
)
8886 goto error_return_verdef
;
8888 elf_tdata (abfd
)->cverdefs
= maxidx
;
8890 everdef
= (Elf_External_Verdef
*) contents
;
8891 iverdefarr
= elf_tdata (abfd
)->verdef
;
8892 for (i
= 0; i
< hdr
->sh_info
; i
++)
8894 Elf_External_Verdaux
*everdaux
;
8895 Elf_Internal_Verdaux
*iverdaux
;
8898 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8900 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8901 goto error_return_bad_verdef
;
8903 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8904 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8906 iverdef
->vd_bfd
= abfd
;
8908 if (iverdef
->vd_cnt
== 0)
8909 iverdef
->vd_auxptr
= NULL
;
8912 if (_bfd_mul_overflow (iverdef
->vd_cnt
,
8913 sizeof (Elf_Internal_Verdaux
), &amt
))
8915 bfd_set_error (bfd_error_file_too_big
);
8916 goto error_return_verdef
;
8918 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8919 bfd_alloc (abfd
, amt
);
8920 if (iverdef
->vd_auxptr
== NULL
)
8921 goto error_return_verdef
;
8925 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8926 goto error_return_bad_verdef
;
8928 everdaux
= ((Elf_External_Verdaux
*)
8929 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8930 iverdaux
= iverdef
->vd_auxptr
;
8931 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8933 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8935 iverdaux
->vda_nodename
=
8936 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8937 iverdaux
->vda_name
);
8938 if (iverdaux
->vda_nodename
== NULL
)
8939 goto error_return_bad_verdef
;
8941 iverdaux
->vda_nextptr
= NULL
;
8942 if (iverdaux
->vda_next
== 0)
8944 iverdef
->vd_cnt
= j
+ 1;
8947 if (j
+ 1 < iverdef
->vd_cnt
)
8948 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8950 if (iverdaux
->vda_next
8951 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8952 goto error_return_bad_verdef
;
8954 everdaux
= ((Elf_External_Verdaux
*)
8955 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8958 iverdef
->vd_nodename
= NULL
;
8959 if (iverdef
->vd_cnt
)
8960 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8962 iverdef
->vd_nextdef
= NULL
;
8963 if (iverdef
->vd_next
== 0)
8965 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8966 iverdef
->vd_nextdef
= iverdef
+ 1;
8968 everdef
= ((Elf_External_Verdef
*)
8969 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8975 else if (default_imported_symver
)
8982 if (_bfd_mul_overflow (freeidx
, sizeof (Elf_Internal_Verdef
), &amt
))
8984 bfd_set_error (bfd_error_file_too_big
);
8987 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
8988 if (elf_tdata (abfd
)->verdef
== NULL
)
8991 elf_tdata (abfd
)->cverdefs
= freeidx
;
8994 /* Create a default version based on the soname. */
8995 if (default_imported_symver
)
8997 Elf_Internal_Verdef
*iverdef
;
8998 Elf_Internal_Verdaux
*iverdaux
;
9000 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
9002 iverdef
->vd_version
= VER_DEF_CURRENT
;
9003 iverdef
->vd_flags
= 0;
9004 iverdef
->vd_ndx
= freeidx
;
9005 iverdef
->vd_cnt
= 1;
9007 iverdef
->vd_bfd
= abfd
;
9009 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
9010 if (iverdef
->vd_nodename
== NULL
)
9011 goto error_return_verdef
;
9012 iverdef
->vd_nextdef
= NULL
;
9013 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
9014 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
9015 if (iverdef
->vd_auxptr
== NULL
)
9016 goto error_return_verdef
;
9018 iverdaux
= iverdef
->vd_auxptr
;
9019 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
9030 _bfd_elf_make_empty_symbol (bfd
*abfd
)
9032 elf_symbol_type
*newsym
;
9034 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (*newsym
));
9037 newsym
->symbol
.the_bfd
= abfd
;
9038 return &newsym
->symbol
;
9042 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
9046 bfd_symbol_info (symbol
, ret
);
9049 /* Return whether a symbol name implies a local symbol. Most targets
9050 use this function for the is_local_label_name entry point, but some
9054 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
9057 /* Normal local symbols start with ``.L''. */
9058 if (name
[0] == '.' && name
[1] == 'L')
9061 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9062 DWARF debugging symbols starting with ``..''. */
9063 if (name
[0] == '.' && name
[1] == '.')
9066 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9067 emitting DWARF debugging output. I suspect this is actually a
9068 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9069 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9070 underscore to be emitted on some ELF targets). For ease of use,
9071 we treat such symbols as local. */
9072 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
9075 /* Treat assembler generated fake symbols, dollar local labels and
9076 forward-backward labels (aka local labels) as locals.
9077 These labels have the form:
9079 L0^A.* (fake symbols)
9081 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
9083 Versions which start with .L will have already been matched above,
9084 so we only need to match the rest. */
9085 if (name
[0] == 'L' && ISDIGIT (name
[1]))
9087 bfd_boolean ret
= FALSE
;
9091 for (p
= name
+ 2; (c
= *p
); p
++)
9093 if (c
== 1 || c
== 2)
9095 if (c
== 1 && p
== name
+ 2)
9096 /* A fake symbol. */
9099 /* FIXME: We are being paranoid here and treating symbols like
9100 L0^Bfoo as if there were non-local, on the grounds that the
9101 assembler will never generate them. But can any symbol
9102 containing an ASCII value in the range 1-31 ever be anything
9103 other than some kind of local ? */
9120 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
9121 asymbol
*symbol ATTRIBUTE_UNUSED
)
9128 _bfd_elf_set_arch_mach (bfd
*abfd
,
9129 enum bfd_architecture arch
,
9130 unsigned long machine
)
9132 /* If this isn't the right architecture for this backend, and this
9133 isn't the generic backend, fail. */
9134 if (arch
!= get_elf_backend_data (abfd
)->arch
9135 && arch
!= bfd_arch_unknown
9136 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
9139 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
9142 /* Find the nearest line to a particular section and offset,
9143 for error reporting. */
9146 _bfd_elf_find_nearest_line (bfd
*abfd
,
9150 const char **filename_ptr
,
9151 const char **functionname_ptr
,
9152 unsigned int *line_ptr
,
9153 unsigned int *discriminator_ptr
)
9157 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
9158 filename_ptr
, functionname_ptr
,
9159 line_ptr
, discriminator_ptr
,
9160 dwarf_debug_sections
,
9161 &elf_tdata (abfd
)->dwarf2_find_line_info
))
9164 if (_bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
9165 filename_ptr
, functionname_ptr
, line_ptr
))
9167 if (!*functionname_ptr
)
9168 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9169 *filename_ptr
? NULL
: filename_ptr
,
9174 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
9175 &found
, filename_ptr
,
9176 functionname_ptr
, line_ptr
,
9177 &elf_tdata (abfd
)->line_info
))
9179 if (found
&& (*functionname_ptr
|| *line_ptr
))
9182 if (symbols
== NULL
)
9185 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
9186 filename_ptr
, functionname_ptr
))
9193 /* Find the line for a symbol. */
9196 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
9197 const char **filename_ptr
, unsigned int *line_ptr
)
9199 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
9200 filename_ptr
, NULL
, line_ptr
, NULL
,
9201 dwarf_debug_sections
,
9202 &elf_tdata (abfd
)->dwarf2_find_line_info
);
9205 /* After a call to bfd_find_nearest_line, successive calls to
9206 bfd_find_inliner_info can be used to get source information about
9207 each level of function inlining that terminated at the address
9208 passed to bfd_find_nearest_line. Currently this is only supported
9209 for DWARF2 with appropriate DWARF3 extensions. */
9212 _bfd_elf_find_inliner_info (bfd
*abfd
,
9213 const char **filename_ptr
,
9214 const char **functionname_ptr
,
9215 unsigned int *line_ptr
)
9218 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
9219 functionname_ptr
, line_ptr
,
9220 & elf_tdata (abfd
)->dwarf2_find_line_info
);
9225 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
9227 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9228 int ret
= bed
->s
->sizeof_ehdr
;
9230 if (!bfd_link_relocatable (info
))
9232 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
9234 if (phdr_size
== (bfd_size_type
) -1)
9236 struct elf_segment_map
*m
;
9239 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
9240 phdr_size
+= bed
->s
->sizeof_phdr
;
9243 phdr_size
= get_program_header_size (abfd
, info
);
9246 elf_program_header_size (abfd
) = phdr_size
;
9254 _bfd_elf_set_section_contents (bfd
*abfd
,
9256 const void *location
,
9258 bfd_size_type count
)
9260 Elf_Internal_Shdr
*hdr
;
9263 if (! abfd
->output_has_begun
9264 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
9270 hdr
= &elf_section_data (section
)->this_hdr
;
9271 if (hdr
->sh_offset
== (file_ptr
) -1)
9273 unsigned char *contents
;
9275 if (bfd_section_is_ctf (section
))
9276 /* Nothing to do with this section: the contents are generated
9280 if ((section
->flags
& SEC_ELF_COMPRESS
) == 0)
9283 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"),
9285 bfd_set_error (bfd_error_invalid_operation
);
9289 if ((offset
+ count
) > hdr
->sh_size
)
9292 (_("%pB:%pA: error: attempting to write over the end of the section"),
9295 bfd_set_error (bfd_error_invalid_operation
);
9299 contents
= hdr
->contents
;
9300 if (contents
== NULL
)
9303 (_("%pB:%pA: error: attempting to write section into an empty buffer"),
9306 bfd_set_error (bfd_error_invalid_operation
);
9310 memcpy (contents
+ offset
, location
, count
);
9314 pos
= hdr
->sh_offset
+ offset
;
9315 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
9316 || bfd_bwrite (location
, count
, abfd
) != count
)
9323 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
9324 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
9325 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
9331 /* Try to convert a non-ELF reloc into an ELF one. */
9334 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
9336 /* Check whether we really have an ELF howto. */
9338 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
9340 bfd_reloc_code_real_type code
;
9341 reloc_howto_type
*howto
;
9343 /* Alien reloc: Try to determine its type to replace it with an
9344 equivalent ELF reloc. */
9346 if (areloc
->howto
->pc_relative
)
9348 switch (areloc
->howto
->bitsize
)
9351 code
= BFD_RELOC_8_PCREL
;
9354 code
= BFD_RELOC_12_PCREL
;
9357 code
= BFD_RELOC_16_PCREL
;
9360 code
= BFD_RELOC_24_PCREL
;
9363 code
= BFD_RELOC_32_PCREL
;
9366 code
= BFD_RELOC_64_PCREL
;
9372 howto
= bfd_reloc_type_lookup (abfd
, code
);
9374 if (howto
&& areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
9376 if (howto
->pcrel_offset
)
9377 areloc
->addend
+= areloc
->address
;
9379 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
9384 switch (areloc
->howto
->bitsize
)
9390 code
= BFD_RELOC_14
;
9393 code
= BFD_RELOC_16
;
9396 code
= BFD_RELOC_26
;
9399 code
= BFD_RELOC_32
;
9402 code
= BFD_RELOC_64
;
9408 howto
= bfd_reloc_type_lookup (abfd
, code
);
9412 areloc
->howto
= howto
;
9420 /* xgettext:c-format */
9421 _bfd_error_handler (_("%pB: %s unsupported"),
9422 abfd
, areloc
->howto
->name
);
9423 bfd_set_error (bfd_error_sorry
);
9428 _bfd_elf_close_and_cleanup (bfd
*abfd
)
9430 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
9432 && (bfd_get_format (abfd
) == bfd_object
9433 || bfd_get_format (abfd
) == bfd_core
))
9435 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
9436 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
9437 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
9440 return _bfd_generic_close_and_cleanup (abfd
);
9443 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
9444 in the relocation's offset. Thus we cannot allow any sort of sanity
9445 range-checking to interfere. There is nothing else to do in processing
9448 bfd_reloc_status_type
9449 _bfd_elf_rel_vtable_reloc_fn
9450 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
9451 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
9452 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
9453 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
9455 return bfd_reloc_ok
;
9458 /* Elf core file support. Much of this only works on native
9459 toolchains, since we rely on knowing the
9460 machine-dependent procfs structure in order to pick
9461 out details about the corefile. */
9463 #ifdef HAVE_SYS_PROCFS_H
9464 # include <sys/procfs.h>
9467 /* Return a PID that identifies a "thread" for threaded cores, or the
9468 PID of the main process for non-threaded cores. */
9471 elfcore_make_pid (bfd
*abfd
)
9475 pid
= elf_tdata (abfd
)->core
->lwpid
;
9477 pid
= elf_tdata (abfd
)->core
->pid
;
9482 /* If there isn't a section called NAME, make one, using
9483 data from SECT. Note, this function will generate a
9484 reference to NAME, so you shouldn't deallocate or
9488 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
9492 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9495 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9499 sect2
->size
= sect
->size
;
9500 sect2
->filepos
= sect
->filepos
;
9501 sect2
->alignment_power
= sect
->alignment_power
;
9505 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9506 actually creates up to two pseudosections:
9507 - For the single-threaded case, a section named NAME, unless
9508 such a section already exists.
9509 - For the multi-threaded case, a section named "NAME/PID", where
9510 PID is elfcore_make_pid (abfd).
9511 Both pseudosections have identical contents. */
9513 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9519 char *threaded_name
;
9523 /* Build the section name. */
9525 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9526 len
= strlen (buf
) + 1;
9527 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9528 if (threaded_name
== NULL
)
9530 memcpy (threaded_name
, buf
, len
);
9532 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9537 sect
->filepos
= filepos
;
9538 sect
->alignment_power
= 2;
9540 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9544 elfcore_make_auxv_note_section (bfd
*abfd
, Elf_Internal_Note
*note
,
9547 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9553 sect
->size
= note
->descsz
- offs
;
9554 sect
->filepos
= note
->descpos
+ offs
;
9555 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9560 /* prstatus_t exists on:
9562 linux 2.[01] + glibc
9566 #if defined (HAVE_PRSTATUS_T)
9569 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9574 if (note
->descsz
== sizeof (prstatus_t
))
9578 size
= sizeof (prstat
.pr_reg
);
9579 offset
= offsetof (prstatus_t
, pr_reg
);
9580 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9582 /* Do not overwrite the core signal if it
9583 has already been set by another thread. */
9584 if (elf_tdata (abfd
)->core
->signal
== 0)
9585 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9586 if (elf_tdata (abfd
)->core
->pid
== 0)
9587 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9589 /* pr_who exists on:
9592 pr_who doesn't exist on:
9595 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9596 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9598 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9601 #if defined (HAVE_PRSTATUS32_T)
9602 else if (note
->descsz
== sizeof (prstatus32_t
))
9604 /* 64-bit host, 32-bit corefile */
9605 prstatus32_t prstat
;
9607 size
= sizeof (prstat
.pr_reg
);
9608 offset
= offsetof (prstatus32_t
, pr_reg
);
9609 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9611 /* Do not overwrite the core signal if it
9612 has already been set by another thread. */
9613 if (elf_tdata (abfd
)->core
->signal
== 0)
9614 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9615 if (elf_tdata (abfd
)->core
->pid
== 0)
9616 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9618 /* pr_who exists on:
9621 pr_who doesn't exist on:
9624 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9625 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9627 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9630 #endif /* HAVE_PRSTATUS32_T */
9633 /* Fail - we don't know how to handle any other
9634 note size (ie. data object type). */
9638 /* Make a ".reg/999" section and a ".reg" section. */
9639 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9640 size
, note
->descpos
+ offset
);
9642 #endif /* defined (HAVE_PRSTATUS_T) */
9644 /* Create a pseudosection containing the exact contents of NOTE. */
9646 elfcore_make_note_pseudosection (bfd
*abfd
,
9648 Elf_Internal_Note
*note
)
9650 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9651 note
->descsz
, note
->descpos
);
9654 /* There isn't a consistent prfpregset_t across platforms,
9655 but it doesn't matter, because we don't have to pick this
9656 data structure apart. */
9659 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9661 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9664 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9665 type of NT_PRXFPREG. Just include the whole note's contents
9669 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9671 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9674 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9675 with a note type of NT_X86_XSTATE. Just include the whole note's
9676 contents literally. */
9679 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9681 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9685 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9687 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9691 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9693 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9697 elfcore_grok_ppc_tar (bfd
*abfd
, Elf_Internal_Note
*note
)
9699 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tar", note
);
9703 elfcore_grok_ppc_ppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9705 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ppr", note
);
9709 elfcore_grok_ppc_dscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9711 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-dscr", note
);
9715 elfcore_grok_ppc_ebb (bfd
*abfd
, Elf_Internal_Note
*note
)
9717 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-ebb", note
);
9721 elfcore_grok_ppc_pmu (bfd
*abfd
, Elf_Internal_Note
*note
)
9723 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-pmu", note
);
9727 elfcore_grok_ppc_tm_cgpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9729 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cgpr", note
);
9733 elfcore_grok_ppc_tm_cfpr (bfd
*abfd
, Elf_Internal_Note
*note
)
9735 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cfpr", note
);
9739 elfcore_grok_ppc_tm_cvmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9741 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvmx", note
);
9745 elfcore_grok_ppc_tm_cvsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9747 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cvsx", note
);
9751 elfcore_grok_ppc_tm_spr (bfd
*abfd
, Elf_Internal_Note
*note
)
9753 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-spr", note
);
9757 elfcore_grok_ppc_tm_ctar (bfd
*abfd
, Elf_Internal_Note
*note
)
9759 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-ctar", note
);
9763 elfcore_grok_ppc_tm_cppr (bfd
*abfd
, Elf_Internal_Note
*note
)
9765 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cppr", note
);
9769 elfcore_grok_ppc_tm_cdscr (bfd
*abfd
, Elf_Internal_Note
*note
)
9771 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-tm-cdscr", note
);
9775 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9777 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9781 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9783 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9787 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9789 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9793 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9795 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9799 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9801 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9805 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9807 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9811 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9813 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9817 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9819 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9823 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9825 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9829 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9831 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9835 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9837 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9841 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9843 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9847 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9849 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9853 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9855 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9859 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9861 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9865 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9867 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9871 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9873 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9877 elfcore_grok_aarch_sve (bfd
*abfd
, Elf_Internal_Note
*note
)
9879 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-sve", note
);
9883 elfcore_grok_aarch_pauth (bfd
*abfd
, Elf_Internal_Note
*note
)
9885 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-pauth", note
);
9889 elfcore_grok_arc_v2 (bfd
*abfd
, Elf_Internal_Note
*note
)
9891 return elfcore_make_note_pseudosection (abfd
, ".reg-arc-v2", note
);
9894 #if defined (HAVE_PRPSINFO_T)
9895 typedef prpsinfo_t elfcore_psinfo_t
;
9896 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9897 typedef prpsinfo32_t elfcore_psinfo32_t
;
9901 #if defined (HAVE_PSINFO_T)
9902 typedef psinfo_t elfcore_psinfo_t
;
9903 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9904 typedef psinfo32_t elfcore_psinfo32_t
;
9908 /* return a malloc'ed copy of a string at START which is at
9909 most MAX bytes long, possibly without a terminating '\0'.
9910 the copy will always have a terminating '\0'. */
9913 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9916 char *end
= (char *) memchr (start
, '\0', max
);
9924 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9928 memcpy (dups
, start
, len
);
9934 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9936 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9938 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9940 elfcore_psinfo_t psinfo
;
9942 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9944 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9945 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9947 elf_tdata (abfd
)->core
->program
9948 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9949 sizeof (psinfo
.pr_fname
));
9951 elf_tdata (abfd
)->core
->command
9952 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9953 sizeof (psinfo
.pr_psargs
));
9955 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9956 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9958 /* 64-bit host, 32-bit corefile */
9959 elfcore_psinfo32_t psinfo
;
9961 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9963 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9964 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9966 elf_tdata (abfd
)->core
->program
9967 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9968 sizeof (psinfo
.pr_fname
));
9970 elf_tdata (abfd
)->core
->command
9971 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9972 sizeof (psinfo
.pr_psargs
));
9978 /* Fail - we don't know how to handle any other
9979 note size (ie. data object type). */
9983 /* Note that for some reason, a spurious space is tacked
9984 onto the end of the args in some (at least one anyway)
9985 implementations, so strip it off if it exists. */
9988 char *command
= elf_tdata (abfd
)->core
->command
;
9989 int n
= strlen (command
);
9991 if (0 < n
&& command
[n
- 1] == ' ')
9992 command
[n
- 1] = '\0';
9997 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9999 #if defined (HAVE_PSTATUS_T)
10001 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10003 if (note
->descsz
== sizeof (pstatus_t
)
10004 #if defined (HAVE_PXSTATUS_T)
10005 || note
->descsz
== sizeof (pxstatus_t
)
10011 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10013 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10015 #if defined (HAVE_PSTATUS32_T)
10016 else if (note
->descsz
== sizeof (pstatus32_t
))
10018 /* 64-bit host, 32-bit corefile */
10021 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
10023 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
10026 /* Could grab some more details from the "representative"
10027 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10028 NT_LWPSTATUS note, presumably. */
10032 #endif /* defined (HAVE_PSTATUS_T) */
10034 #if defined (HAVE_LWPSTATUS_T)
10036 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10038 lwpstatus_t lwpstat
;
10044 if (note
->descsz
!= sizeof (lwpstat
)
10045 #if defined (HAVE_LWPXSTATUS_T)
10046 && note
->descsz
!= sizeof (lwpxstatus_t
)
10051 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
10053 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
10054 /* Do not overwrite the core signal if it has already been set by
10056 if (elf_tdata (abfd
)->core
->signal
== 0)
10057 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
10059 /* Make a ".reg/999" section. */
10061 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
10062 len
= strlen (buf
) + 1;
10063 name
= bfd_alloc (abfd
, len
);
10066 memcpy (name
, buf
, len
);
10068 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10072 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10073 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
10074 sect
->filepos
= note
->descpos
10075 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
10078 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10079 sect
->size
= sizeof (lwpstat
.pr_reg
);
10080 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
10083 sect
->alignment_power
= 2;
10085 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10088 /* Make a ".reg2/999" section */
10090 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
10091 len
= strlen (buf
) + 1;
10092 name
= bfd_alloc (abfd
, len
);
10095 memcpy (name
, buf
, len
);
10097 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10101 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10102 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
10103 sect
->filepos
= note
->descpos
10104 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
10107 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
10108 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
10109 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
10112 sect
->alignment_power
= 2;
10114 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
10116 #endif /* defined (HAVE_LWPSTATUS_T) */
10118 /* These constants, and the structure offsets used below, are defined by
10119 Cygwin's core_dump.h */
10120 #define NOTE_INFO_PROCESS 1
10121 #define NOTE_INFO_THREAD 2
10122 #define NOTE_INFO_MODULE 3
10123 #define NOTE_INFO_MODULE64 4
10126 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10131 unsigned int name_size
;
10134 int is_active_thread
;
10137 if (note
->descsz
< 4)
10140 if (! CONST_STRNEQ (note
->namedata
, "win32"))
10143 type
= bfd_get_32 (abfd
, note
->descdata
);
10146 const char *type_name
;
10147 unsigned long min_size
;
10150 { "NOTE_INFO_PROCESS", 12 },
10151 { "NOTE_INFO_THREAD", 12 },
10152 { "NOTE_INFO_MODULE", 12 },
10153 { "NOTE_INFO_MODULE64", 16 },
10156 if (type
> (sizeof(size_check
)/sizeof(size_check
[0])))
10159 if (note
->descsz
< size_check
[type
- 1].min_size
)
10161 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"),
10162 abfd
, size_check
[type
- 1].type_name
, note
->descsz
);
10168 case NOTE_INFO_PROCESS
:
10169 /* FIXME: need to add ->core->command. */
10170 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10171 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10174 case NOTE_INFO_THREAD
:
10175 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
10177 /* thread_info.tid */
10178 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 4));
10180 len
= strlen (buf
) + 1;
10181 name
= (char *) bfd_alloc (abfd
, len
);
10185 memcpy (name
, buf
, len
);
10187 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10191 /* sizeof (thread_info.thread_context) */
10192 sect
->size
= note
->descsz
- 12;
10193 /* offsetof (thread_info.thread_context) */
10194 sect
->filepos
= note
->descpos
+ 12;
10195 sect
->alignment_power
= 2;
10197 /* thread_info.is_active_thread */
10198 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10200 if (is_active_thread
)
10201 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
10205 case NOTE_INFO_MODULE
:
10206 case NOTE_INFO_MODULE64
:
10207 /* Make a ".module/xxxxxxxx" section. */
10208 if (type
== NOTE_INFO_MODULE
)
10210 /* module_info.base_address */
10211 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
10212 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
10213 /* module_info.module_name_size */
10214 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
10216 else /* NOTE_INFO_MODULE64 */
10218 /* module_info.base_address */
10219 base_addr
= bfd_get_64 (abfd
, note
->descdata
+ 4);
10220 sprintf (buf
, ".module/%016lx", (unsigned long) base_addr
);
10221 /* module_info.module_name_size */
10222 name_size
= bfd_get_32 (abfd
, note
->descdata
+ 12);
10225 len
= strlen (buf
) + 1;
10226 name
= (char *) bfd_alloc (abfd
, len
);
10230 memcpy (name
, buf
, len
);
10232 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10237 if (note
->descsz
< 12 + name_size
)
10239 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"),
10240 abfd
, note
->descsz
, name_size
);
10244 sect
->size
= note
->descsz
;
10245 sect
->filepos
= note
->descpos
;
10246 sect
->alignment_power
= 2;
10257 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10259 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10261 switch (note
->type
)
10267 if (bed
->elf_backend_grok_prstatus
)
10268 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
10270 #if defined (HAVE_PRSTATUS_T)
10271 return elfcore_grok_prstatus (abfd
, note
);
10276 #if defined (HAVE_PSTATUS_T)
10278 return elfcore_grok_pstatus (abfd
, note
);
10281 #if defined (HAVE_LWPSTATUS_T)
10283 return elfcore_grok_lwpstatus (abfd
, note
);
10286 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
10287 return elfcore_grok_prfpreg (abfd
, note
);
10289 case NT_WIN32PSTATUS
:
10290 return elfcore_grok_win32pstatus (abfd
, note
);
10292 case NT_PRXFPREG
: /* Linux SSE extension */
10293 if (note
->namesz
== 6
10294 && strcmp (note
->namedata
, "LINUX") == 0)
10295 return elfcore_grok_prxfpreg (abfd
, note
);
10299 case NT_X86_XSTATE
: /* Linux XSAVE extension */
10300 if (note
->namesz
== 6
10301 && strcmp (note
->namedata
, "LINUX") == 0)
10302 return elfcore_grok_xstatereg (abfd
, note
);
10307 if (note
->namesz
== 6
10308 && strcmp (note
->namedata
, "LINUX") == 0)
10309 return elfcore_grok_ppc_vmx (abfd
, note
);
10314 if (note
->namesz
== 6
10315 && strcmp (note
->namedata
, "LINUX") == 0)
10316 return elfcore_grok_ppc_vsx (abfd
, note
);
10321 if (note
->namesz
== 6
10322 && strcmp (note
->namedata
, "LINUX") == 0)
10323 return elfcore_grok_ppc_tar (abfd
, note
);
10328 if (note
->namesz
== 6
10329 && strcmp (note
->namedata
, "LINUX") == 0)
10330 return elfcore_grok_ppc_ppr (abfd
, note
);
10335 if (note
->namesz
== 6
10336 && strcmp (note
->namedata
, "LINUX") == 0)
10337 return elfcore_grok_ppc_dscr (abfd
, note
);
10342 if (note
->namesz
== 6
10343 && strcmp (note
->namedata
, "LINUX") == 0)
10344 return elfcore_grok_ppc_ebb (abfd
, note
);
10349 if (note
->namesz
== 6
10350 && strcmp (note
->namedata
, "LINUX") == 0)
10351 return elfcore_grok_ppc_pmu (abfd
, note
);
10355 case NT_PPC_TM_CGPR
:
10356 if (note
->namesz
== 6
10357 && strcmp (note
->namedata
, "LINUX") == 0)
10358 return elfcore_grok_ppc_tm_cgpr (abfd
, note
);
10362 case NT_PPC_TM_CFPR
:
10363 if (note
->namesz
== 6
10364 && strcmp (note
->namedata
, "LINUX") == 0)
10365 return elfcore_grok_ppc_tm_cfpr (abfd
, note
);
10369 case NT_PPC_TM_CVMX
:
10370 if (note
->namesz
== 6
10371 && strcmp (note
->namedata
, "LINUX") == 0)
10372 return elfcore_grok_ppc_tm_cvmx (abfd
, note
);
10376 case NT_PPC_TM_CVSX
:
10377 if (note
->namesz
== 6
10378 && strcmp (note
->namedata
, "LINUX") == 0)
10379 return elfcore_grok_ppc_tm_cvsx (abfd
, note
);
10383 case NT_PPC_TM_SPR
:
10384 if (note
->namesz
== 6
10385 && strcmp (note
->namedata
, "LINUX") == 0)
10386 return elfcore_grok_ppc_tm_spr (abfd
, note
);
10390 case NT_PPC_TM_CTAR
:
10391 if (note
->namesz
== 6
10392 && strcmp (note
->namedata
, "LINUX") == 0)
10393 return elfcore_grok_ppc_tm_ctar (abfd
, note
);
10397 case NT_PPC_TM_CPPR
:
10398 if (note
->namesz
== 6
10399 && strcmp (note
->namedata
, "LINUX") == 0)
10400 return elfcore_grok_ppc_tm_cppr (abfd
, note
);
10404 case NT_PPC_TM_CDSCR
:
10405 if (note
->namesz
== 6
10406 && strcmp (note
->namedata
, "LINUX") == 0)
10407 return elfcore_grok_ppc_tm_cdscr (abfd
, note
);
10411 case NT_S390_HIGH_GPRS
:
10412 if (note
->namesz
== 6
10413 && strcmp (note
->namedata
, "LINUX") == 0)
10414 return elfcore_grok_s390_high_gprs (abfd
, note
);
10418 case NT_S390_TIMER
:
10419 if (note
->namesz
== 6
10420 && strcmp (note
->namedata
, "LINUX") == 0)
10421 return elfcore_grok_s390_timer (abfd
, note
);
10425 case NT_S390_TODCMP
:
10426 if (note
->namesz
== 6
10427 && strcmp (note
->namedata
, "LINUX") == 0)
10428 return elfcore_grok_s390_todcmp (abfd
, note
);
10432 case NT_S390_TODPREG
:
10433 if (note
->namesz
== 6
10434 && strcmp (note
->namedata
, "LINUX") == 0)
10435 return elfcore_grok_s390_todpreg (abfd
, note
);
10440 if (note
->namesz
== 6
10441 && strcmp (note
->namedata
, "LINUX") == 0)
10442 return elfcore_grok_s390_ctrs (abfd
, note
);
10446 case NT_S390_PREFIX
:
10447 if (note
->namesz
== 6
10448 && strcmp (note
->namedata
, "LINUX") == 0)
10449 return elfcore_grok_s390_prefix (abfd
, note
);
10453 case NT_S390_LAST_BREAK
:
10454 if (note
->namesz
== 6
10455 && strcmp (note
->namedata
, "LINUX") == 0)
10456 return elfcore_grok_s390_last_break (abfd
, note
);
10460 case NT_S390_SYSTEM_CALL
:
10461 if (note
->namesz
== 6
10462 && strcmp (note
->namedata
, "LINUX") == 0)
10463 return elfcore_grok_s390_system_call (abfd
, note
);
10468 if (note
->namesz
== 6
10469 && strcmp (note
->namedata
, "LINUX") == 0)
10470 return elfcore_grok_s390_tdb (abfd
, note
);
10474 case NT_S390_VXRS_LOW
:
10475 if (note
->namesz
== 6
10476 && strcmp (note
->namedata
, "LINUX") == 0)
10477 return elfcore_grok_s390_vxrs_low (abfd
, note
);
10481 case NT_S390_VXRS_HIGH
:
10482 if (note
->namesz
== 6
10483 && strcmp (note
->namedata
, "LINUX") == 0)
10484 return elfcore_grok_s390_vxrs_high (abfd
, note
);
10488 case NT_S390_GS_CB
:
10489 if (note
->namesz
== 6
10490 && strcmp (note
->namedata
, "LINUX") == 0)
10491 return elfcore_grok_s390_gs_cb (abfd
, note
);
10495 case NT_S390_GS_BC
:
10496 if (note
->namesz
== 6
10497 && strcmp (note
->namedata
, "LINUX") == 0)
10498 return elfcore_grok_s390_gs_bc (abfd
, note
);
10503 if (note
->namesz
== 6
10504 && strcmp (note
->namedata
, "LINUX") == 0)
10505 return elfcore_grok_arc_v2 (abfd
, note
);
10510 if (note
->namesz
== 6
10511 && strcmp (note
->namedata
, "LINUX") == 0)
10512 return elfcore_grok_arm_vfp (abfd
, note
);
10517 if (note
->namesz
== 6
10518 && strcmp (note
->namedata
, "LINUX") == 0)
10519 return elfcore_grok_aarch_tls (abfd
, note
);
10523 case NT_ARM_HW_BREAK
:
10524 if (note
->namesz
== 6
10525 && strcmp (note
->namedata
, "LINUX") == 0)
10526 return elfcore_grok_aarch_hw_break (abfd
, note
);
10530 case NT_ARM_HW_WATCH
:
10531 if (note
->namesz
== 6
10532 && strcmp (note
->namedata
, "LINUX") == 0)
10533 return elfcore_grok_aarch_hw_watch (abfd
, note
);
10538 if (note
->namesz
== 6
10539 && strcmp (note
->namedata
, "LINUX") == 0)
10540 return elfcore_grok_aarch_sve (abfd
, note
);
10544 case NT_ARM_PAC_MASK
:
10545 if (note
->namesz
== 6
10546 && strcmp (note
->namedata
, "LINUX") == 0)
10547 return elfcore_grok_aarch_pauth (abfd
, note
);
10553 if (bed
->elf_backend_grok_psinfo
)
10554 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
10556 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10557 return elfcore_grok_psinfo (abfd
, note
);
10563 return elfcore_make_auxv_note_section (abfd
, note
, 0);
10566 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
10570 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
10577 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
10579 struct bfd_build_id
* build_id
;
10581 if (note
->descsz
== 0)
10584 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
10585 if (build_id
== NULL
)
10588 build_id
->size
= note
->descsz
;
10589 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
10590 abfd
->build_id
= build_id
;
10596 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10598 switch (note
->type
)
10603 case NT_GNU_PROPERTY_TYPE_0
:
10604 return _bfd_elf_parse_gnu_properties (abfd
, note
);
10606 case NT_GNU_BUILD_ID
:
10607 return elfobj_grok_gnu_build_id (abfd
, note
);
10612 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
10614 struct sdt_note
*cur
=
10615 (struct sdt_note
*) bfd_alloc (abfd
,
10616 sizeof (struct sdt_note
) + note
->descsz
);
10618 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
10619 cur
->size
= (bfd_size_type
) note
->descsz
;
10620 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
10622 elf_tdata (abfd
)->sdt_note_head
= cur
;
10628 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10630 switch (note
->type
)
10633 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
10641 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10645 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10648 if (note
->descsz
< 108)
10653 if (note
->descsz
< 120)
10661 /* Check for version 1 in pr_version. */
10662 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10667 /* Skip over pr_psinfosz. */
10668 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10672 offset
+= 4; /* Padding before pr_psinfosz. */
10676 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
10677 elf_tdata (abfd
)->core
->program
10678 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
10681 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
10682 elf_tdata (abfd
)->core
->command
10683 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
10686 /* Padding before pr_pid. */
10689 /* The pr_pid field was added in version "1a". */
10690 if (note
->descsz
< offset
+ 4)
10693 elf_tdata (abfd
)->core
->pid
10694 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10700 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
10706 /* Compute offset of pr_getregsz, skipping over pr_statussz.
10707 Also compute minimum size of this note. */
10708 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
10712 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
10716 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
10717 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
10724 if (note
->descsz
< min_size
)
10727 /* Check for version 1 in pr_version. */
10728 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
10731 /* Extract size of pr_reg from pr_gregsetsz. */
10732 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
10733 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
10735 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10740 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10744 /* Skip over pr_osreldate. */
10747 /* Read signal from pr_cursig. */
10748 if (elf_tdata (abfd
)->core
->signal
== 0)
10749 elf_tdata (abfd
)->core
->signal
10750 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10753 /* Read TID from pr_pid. */
10754 elf_tdata (abfd
)->core
->lwpid
10755 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10758 /* Padding before pr_reg. */
10759 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10762 /* Make sure that there is enough data remaining in the note. */
10763 if ((note
->descsz
- offset
) < size
)
10766 /* Make a ".reg/999" section and a ".reg" section. */
10767 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10768 size
, note
->descpos
+ offset
);
10772 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10774 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10776 switch (note
->type
)
10779 if (bed
->elf_backend_grok_freebsd_prstatus
)
10780 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10782 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10785 return elfcore_grok_prfpreg (abfd
, note
);
10788 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10790 case NT_FREEBSD_THRMISC
:
10791 if (note
->namesz
== 8)
10792 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10796 case NT_FREEBSD_PROCSTAT_PROC
:
10797 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10800 case NT_FREEBSD_PROCSTAT_FILES
:
10801 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10804 case NT_FREEBSD_PROCSTAT_VMMAP
:
10805 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10808 case NT_FREEBSD_PROCSTAT_AUXV
:
10809 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10811 case NT_X86_XSTATE
:
10812 if (note
->namesz
== 8)
10813 return elfcore_grok_xstatereg (abfd
, note
);
10817 case NT_FREEBSD_PTLWPINFO
:
10818 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10822 return elfcore_grok_arm_vfp (abfd
, note
);
10830 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10834 cp
= strchr (note
->namedata
, '@');
10837 *lwpidp
= atoi(cp
+ 1);
10844 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10846 if (note
->descsz
<= 0x7c + 31)
10849 /* Signal number at offset 0x08. */
10850 elf_tdata (abfd
)->core
->signal
10851 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10853 /* Process ID at offset 0x50. */
10854 elf_tdata (abfd
)->core
->pid
10855 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10857 /* Command name at 0x7c (max 32 bytes, including nul). */
10858 elf_tdata (abfd
)->core
->command
10859 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10861 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10866 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10870 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10871 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10873 switch (note
->type
)
10875 case NT_NETBSDCORE_PROCINFO
:
10876 /* NetBSD-specific core "procinfo". Note that we expect to
10877 find this note before any of the others, which is fine,
10878 since the kernel writes this note out first when it
10879 creates a core file. */
10880 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10881 #ifdef NT_NETBSDCORE_AUXV
10882 case NT_NETBSDCORE_AUXV
:
10883 /* NetBSD-specific Elf Auxiliary Vector data. */
10884 return elfcore_make_auxv_note_section (abfd
, note
, 4);
10886 #ifdef NT_NETBSDCORE_LWPSTATUS
10887 case NT_NETBSDCORE_LWPSTATUS
:
10888 return elfcore_make_note_pseudosection (abfd
,
10889 ".note.netbsdcore.lwpstatus",
10896 /* As of March 2020 there are no other machine-independent notes
10897 defined for NetBSD core files. If the note type is less
10898 than the start of the machine-dependent note types, we don't
10901 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10905 switch (bfd_get_arch (abfd
))
10907 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10908 PT_GETFPREGS == mach+2. */
10910 case bfd_arch_aarch64
:
10911 case bfd_arch_alpha
:
10912 case bfd_arch_sparc
:
10913 switch (note
->type
)
10915 case NT_NETBSDCORE_FIRSTMACH
+0:
10916 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10918 case NT_NETBSDCORE_FIRSTMACH
+2:
10919 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10925 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
10926 There's also old PT___GETREGS40 == mach + 1 for old reg
10927 structure which lacks GBR. */
10930 switch (note
->type
)
10932 case NT_NETBSDCORE_FIRSTMACH
+3:
10933 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10935 case NT_NETBSDCORE_FIRSTMACH
+5:
10936 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10942 /* On all other arch's, PT_GETREGS == mach+1 and
10943 PT_GETFPREGS == mach+3. */
10946 switch (note
->type
)
10948 case NT_NETBSDCORE_FIRSTMACH
+1:
10949 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10951 case NT_NETBSDCORE_FIRSTMACH
+3:
10952 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10962 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10964 if (note
->descsz
<= 0x48 + 31)
10967 /* Signal number at offset 0x08. */
10968 elf_tdata (abfd
)->core
->signal
10969 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10971 /* Process ID at offset 0x20. */
10972 elf_tdata (abfd
)->core
->pid
10973 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10975 /* Command name at 0x48 (max 32 bytes, including nul). */
10976 elf_tdata (abfd
)->core
->command
10977 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10983 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10985 if (note
->type
== NT_OPENBSD_PROCINFO
)
10986 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10988 if (note
->type
== NT_OPENBSD_REGS
)
10989 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10991 if (note
->type
== NT_OPENBSD_FPREGS
)
10992 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10994 if (note
->type
== NT_OPENBSD_XFPREGS
)
10995 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10997 if (note
->type
== NT_OPENBSD_AUXV
)
10998 return elfcore_make_auxv_note_section (abfd
, note
, 0);
11000 if (note
->type
== NT_OPENBSD_WCOOKIE
)
11002 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
11007 sect
->size
= note
->descsz
;
11008 sect
->filepos
= note
->descpos
;
11009 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
11018 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
11020 void *ddata
= note
->descdata
;
11027 if (note
->descsz
< 16)
11030 /* nto_procfs_status 'pid' field is at offset 0. */
11031 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
11033 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
11034 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
11036 /* nto_procfs_status 'flags' field is at offset 8. */
11037 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
11039 /* nto_procfs_status 'what' field is at offset 14. */
11040 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
11042 elf_tdata (abfd
)->core
->signal
= sig
;
11043 elf_tdata (abfd
)->core
->lwpid
= *tid
;
11046 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
11047 do not come from signals so we make sure we set the current
11048 thread just in case. */
11049 if (flags
& 0x00000080)
11050 elf_tdata (abfd
)->core
->lwpid
= *tid
;
11052 /* Make a ".qnx_core_status/%d" section. */
11053 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
11055 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11058 strcpy (name
, buf
);
11060 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11064 sect
->size
= note
->descsz
;
11065 sect
->filepos
= note
->descpos
;
11066 sect
->alignment_power
= 2;
11068 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
11072 elfcore_grok_nto_regs (bfd
*abfd
,
11073 Elf_Internal_Note
*note
,
11081 /* Make a "(base)/%d" section. */
11082 sprintf (buf
, "%s/%ld", base
, tid
);
11084 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
11087 strcpy (name
, buf
);
11089 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11093 sect
->size
= note
->descsz
;
11094 sect
->filepos
= note
->descpos
;
11095 sect
->alignment_power
= 2;
11097 /* This is the current thread. */
11098 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
11099 return elfcore_maybe_make_sect (abfd
, base
, sect
);
11104 #define BFD_QNT_CORE_INFO 7
11105 #define BFD_QNT_CORE_STATUS 8
11106 #define BFD_QNT_CORE_GREG 9
11107 #define BFD_QNT_CORE_FPREG 10
11110 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11112 /* Every GREG section has a STATUS section before it. Store the
11113 tid from the previous call to pass down to the next gregs
11115 static long tid
= 1;
11117 switch (note
->type
)
11119 case BFD_QNT_CORE_INFO
:
11120 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
11121 case BFD_QNT_CORE_STATUS
:
11122 return elfcore_grok_nto_status (abfd
, note
, &tid
);
11123 case BFD_QNT_CORE_GREG
:
11124 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
11125 case BFD_QNT_CORE_FPREG
:
11126 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
11133 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
11139 /* Use note name as section name. */
11140 len
= note
->namesz
;
11141 name
= (char *) bfd_alloc (abfd
, len
);
11144 memcpy (name
, note
->namedata
, len
);
11145 name
[len
- 1] = '\0';
11147 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
11151 sect
->size
= note
->descsz
;
11152 sect
->filepos
= note
->descpos
;
11153 sect
->alignment_power
= 1;
11158 /* Function: elfcore_write_note
11161 buffer to hold note, and current size of buffer
11165 size of data for note
11167 Writes note to end of buffer. ELF64 notes are written exactly as
11168 for ELF32, despite the current (as of 2006) ELF gabi specifying
11169 that they ought to have 8-byte namesz and descsz field, and have
11170 8-byte alignment. Other writers, eg. Linux kernel, do the same.
11173 Pointer to realloc'd buffer, *BUFSIZ updated. */
11176 elfcore_write_note (bfd
*abfd
,
11184 Elf_External_Note
*xnp
;
11191 namesz
= strlen (name
) + 1;
11193 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
11195 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
11198 dest
= buf
+ *bufsiz
;
11199 *bufsiz
+= newspace
;
11200 xnp
= (Elf_External_Note
*) dest
;
11201 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
11202 H_PUT_32 (abfd
, size
, xnp
->descsz
);
11203 H_PUT_32 (abfd
, type
, xnp
->type
);
11207 memcpy (dest
, name
, namesz
);
11215 memcpy (dest
, input
, size
);
11225 /* gcc-8 warns (*) on all the strncpy calls in this function about
11226 possible string truncation. The "truncation" is not a bug. We
11227 have an external representation of structs with fields that are not
11228 necessarily NULL terminated and corresponding internal
11229 representation fields that are one larger so that they can always
11230 be NULL terminated.
11231 gcc versions between 4.2 and 4.6 do not allow pragma control of
11232 diagnostics inside functions, giving a hard error if you try to use
11233 the finer control available with later versions.
11234 gcc prior to 4.2 warns about diagnostic push and pop.
11235 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
11236 unless you also add #pragma GCC diagnostic ignored "-Wpragma".
11237 (*) Depending on your system header files! */
11238 #if GCC_VERSION >= 8000
11239 # pragma GCC diagnostic push
11240 # pragma GCC diagnostic ignored "-Wstringop-truncation"
11243 elfcore_write_prpsinfo (bfd
*abfd
,
11247 const char *psargs
)
11249 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11251 if (bed
->elf_backend_write_core_note
!= NULL
)
11254 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11255 NT_PRPSINFO
, fname
, psargs
);
11260 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11261 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
11262 if (bed
->s
->elfclass
== ELFCLASS32
)
11264 # if defined (HAVE_PSINFO32_T)
11266 int note_type
= NT_PSINFO
;
11269 int note_type
= NT_PRPSINFO
;
11272 memset (&data
, 0, sizeof (data
));
11273 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11274 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11275 return elfcore_write_note (abfd
, buf
, bufsiz
,
11276 "CORE", note_type
, &data
, sizeof (data
));
11281 # if defined (HAVE_PSINFO_T)
11283 int note_type
= NT_PSINFO
;
11286 int note_type
= NT_PRPSINFO
;
11289 memset (&data
, 0, sizeof (data
));
11290 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
11291 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
11292 return elfcore_write_note (abfd
, buf
, bufsiz
,
11293 "CORE", note_type
, &data
, sizeof (data
));
11295 #endif /* PSINFO_T or PRPSINFO_T */
11300 #if GCC_VERSION >= 8000
11301 # pragma GCC diagnostic pop
11305 elfcore_write_linux_prpsinfo32
11306 (bfd
*abfd
, char *buf
, int *bufsiz
,
11307 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11309 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
11311 struct elf_external_linux_prpsinfo32_ugid16 data
;
11313 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
11314 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11315 &data
, sizeof (data
));
11319 struct elf_external_linux_prpsinfo32_ugid32 data
;
11321 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
11322 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
11323 &data
, sizeof (data
));
11328 elfcore_write_linux_prpsinfo64
11329 (bfd
*abfd
, char *buf
, int *bufsiz
,
11330 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
11332 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
11334 struct elf_external_linux_prpsinfo64_ugid16 data
;
11336 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
11337 return elfcore_write_note (abfd
, buf
, bufsiz
,
11338 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11342 struct elf_external_linux_prpsinfo64_ugid32 data
;
11344 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
11345 return elfcore_write_note (abfd
, buf
, bufsiz
,
11346 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
11351 elfcore_write_prstatus (bfd
*abfd
,
11358 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11360 if (bed
->elf_backend_write_core_note
!= NULL
)
11363 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
11365 pid
, cursig
, gregs
);
11370 #if defined (HAVE_PRSTATUS_T)
11371 #if defined (HAVE_PRSTATUS32_T)
11372 if (bed
->s
->elfclass
== ELFCLASS32
)
11374 prstatus32_t prstat
;
11376 memset (&prstat
, 0, sizeof (prstat
));
11377 prstat
.pr_pid
= pid
;
11378 prstat
.pr_cursig
= cursig
;
11379 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11380 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11381 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11388 memset (&prstat
, 0, sizeof (prstat
));
11389 prstat
.pr_pid
= pid
;
11390 prstat
.pr_cursig
= cursig
;
11391 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
11392 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
11393 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
11395 #endif /* HAVE_PRSTATUS_T */
11401 #if defined (HAVE_LWPSTATUS_T)
11403 elfcore_write_lwpstatus (bfd
*abfd
,
11410 lwpstatus_t lwpstat
;
11411 const char *note_name
= "CORE";
11413 memset (&lwpstat
, 0, sizeof (lwpstat
));
11414 lwpstat
.pr_lwpid
= pid
>> 16;
11415 lwpstat
.pr_cursig
= cursig
;
11416 #if defined (HAVE_LWPSTATUS_T_PR_REG)
11417 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
11418 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11419 #if !defined(gregs)
11420 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
11421 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
11423 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
11424 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
11427 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11428 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
11430 #endif /* HAVE_LWPSTATUS_T */
11432 #if defined (HAVE_PSTATUS_T)
11434 elfcore_write_pstatus (bfd
*abfd
,
11438 int cursig ATTRIBUTE_UNUSED
,
11439 const void *gregs ATTRIBUTE_UNUSED
)
11441 const char *note_name
= "CORE";
11442 #if defined (HAVE_PSTATUS32_T)
11443 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11445 if (bed
->s
->elfclass
== ELFCLASS32
)
11449 memset (&pstat
, 0, sizeof (pstat
));
11450 pstat
.pr_pid
= pid
& 0xffff;
11451 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11452 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11460 memset (&pstat
, 0, sizeof (pstat
));
11461 pstat
.pr_pid
= pid
& 0xffff;
11462 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
11463 NT_PSTATUS
, &pstat
, sizeof (pstat
));
11467 #endif /* HAVE_PSTATUS_T */
11470 elfcore_write_prfpreg (bfd
*abfd
,
11473 const void *fpregs
,
11476 const char *note_name
= "CORE";
11477 return elfcore_write_note (abfd
, buf
, bufsiz
,
11478 note_name
, NT_FPREGSET
, fpregs
, size
);
11482 elfcore_write_prxfpreg (bfd
*abfd
,
11485 const void *xfpregs
,
11488 char *note_name
= "LINUX";
11489 return elfcore_write_note (abfd
, buf
, bufsiz
,
11490 note_name
, NT_PRXFPREG
, xfpregs
, size
);
11494 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
11495 const void *xfpregs
, int size
)
11498 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
11499 note_name
= "FreeBSD";
11501 note_name
= "LINUX";
11502 return elfcore_write_note (abfd
, buf
, bufsiz
,
11503 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
11507 elfcore_write_ppc_vmx (bfd
*abfd
,
11510 const void *ppc_vmx
,
11513 char *note_name
= "LINUX";
11514 return elfcore_write_note (abfd
, buf
, bufsiz
,
11515 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
11519 elfcore_write_ppc_vsx (bfd
*abfd
,
11522 const void *ppc_vsx
,
11525 char *note_name
= "LINUX";
11526 return elfcore_write_note (abfd
, buf
, bufsiz
,
11527 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
11531 elfcore_write_ppc_tar (bfd
*abfd
,
11534 const void *ppc_tar
,
11537 char *note_name
= "LINUX";
11538 return elfcore_write_note (abfd
, buf
, bufsiz
,
11539 note_name
, NT_PPC_TAR
, ppc_tar
, size
);
11543 elfcore_write_ppc_ppr (bfd
*abfd
,
11546 const void *ppc_ppr
,
11549 char *note_name
= "LINUX";
11550 return elfcore_write_note (abfd
, buf
, bufsiz
,
11551 note_name
, NT_PPC_PPR
, ppc_ppr
, size
);
11555 elfcore_write_ppc_dscr (bfd
*abfd
,
11558 const void *ppc_dscr
,
11561 char *note_name
= "LINUX";
11562 return elfcore_write_note (abfd
, buf
, bufsiz
,
11563 note_name
, NT_PPC_DSCR
, ppc_dscr
, size
);
11567 elfcore_write_ppc_ebb (bfd
*abfd
,
11570 const void *ppc_ebb
,
11573 char *note_name
= "LINUX";
11574 return elfcore_write_note (abfd
, buf
, bufsiz
,
11575 note_name
, NT_PPC_EBB
, ppc_ebb
, size
);
11579 elfcore_write_ppc_pmu (bfd
*abfd
,
11582 const void *ppc_pmu
,
11585 char *note_name
= "LINUX";
11586 return elfcore_write_note (abfd
, buf
, bufsiz
,
11587 note_name
, NT_PPC_PMU
, ppc_pmu
, size
);
11591 elfcore_write_ppc_tm_cgpr (bfd
*abfd
,
11594 const void *ppc_tm_cgpr
,
11597 char *note_name
= "LINUX";
11598 return elfcore_write_note (abfd
, buf
, bufsiz
,
11599 note_name
, NT_PPC_TM_CGPR
, ppc_tm_cgpr
, size
);
11603 elfcore_write_ppc_tm_cfpr (bfd
*abfd
,
11606 const void *ppc_tm_cfpr
,
11609 char *note_name
= "LINUX";
11610 return elfcore_write_note (abfd
, buf
, bufsiz
,
11611 note_name
, NT_PPC_TM_CFPR
, ppc_tm_cfpr
, size
);
11615 elfcore_write_ppc_tm_cvmx (bfd
*abfd
,
11618 const void *ppc_tm_cvmx
,
11621 char *note_name
= "LINUX";
11622 return elfcore_write_note (abfd
, buf
, bufsiz
,
11623 note_name
, NT_PPC_TM_CVMX
, ppc_tm_cvmx
, size
);
11627 elfcore_write_ppc_tm_cvsx (bfd
*abfd
,
11630 const void *ppc_tm_cvsx
,
11633 char *note_name
= "LINUX";
11634 return elfcore_write_note (abfd
, buf
, bufsiz
,
11635 note_name
, NT_PPC_TM_CVSX
, ppc_tm_cvsx
, size
);
11639 elfcore_write_ppc_tm_spr (bfd
*abfd
,
11642 const void *ppc_tm_spr
,
11645 char *note_name
= "LINUX";
11646 return elfcore_write_note (abfd
, buf
, bufsiz
,
11647 note_name
, NT_PPC_TM_SPR
, ppc_tm_spr
, size
);
11651 elfcore_write_ppc_tm_ctar (bfd
*abfd
,
11654 const void *ppc_tm_ctar
,
11657 char *note_name
= "LINUX";
11658 return elfcore_write_note (abfd
, buf
, bufsiz
,
11659 note_name
, NT_PPC_TM_CTAR
, ppc_tm_ctar
, size
);
11663 elfcore_write_ppc_tm_cppr (bfd
*abfd
,
11666 const void *ppc_tm_cppr
,
11669 char *note_name
= "LINUX";
11670 return elfcore_write_note (abfd
, buf
, bufsiz
,
11671 note_name
, NT_PPC_TM_CPPR
, ppc_tm_cppr
, size
);
11675 elfcore_write_ppc_tm_cdscr (bfd
*abfd
,
11678 const void *ppc_tm_cdscr
,
11681 char *note_name
= "LINUX";
11682 return elfcore_write_note (abfd
, buf
, bufsiz
,
11683 note_name
, NT_PPC_TM_CDSCR
, ppc_tm_cdscr
, size
);
11687 elfcore_write_s390_high_gprs (bfd
*abfd
,
11690 const void *s390_high_gprs
,
11693 char *note_name
= "LINUX";
11694 return elfcore_write_note (abfd
, buf
, bufsiz
,
11695 note_name
, NT_S390_HIGH_GPRS
,
11696 s390_high_gprs
, size
);
11700 elfcore_write_s390_timer (bfd
*abfd
,
11703 const void *s390_timer
,
11706 char *note_name
= "LINUX";
11707 return elfcore_write_note (abfd
, buf
, bufsiz
,
11708 note_name
, NT_S390_TIMER
, s390_timer
, size
);
11712 elfcore_write_s390_todcmp (bfd
*abfd
,
11715 const void *s390_todcmp
,
11718 char *note_name
= "LINUX";
11719 return elfcore_write_note (abfd
, buf
, bufsiz
,
11720 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
11724 elfcore_write_s390_todpreg (bfd
*abfd
,
11727 const void *s390_todpreg
,
11730 char *note_name
= "LINUX";
11731 return elfcore_write_note (abfd
, buf
, bufsiz
,
11732 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
11736 elfcore_write_s390_ctrs (bfd
*abfd
,
11739 const void *s390_ctrs
,
11742 char *note_name
= "LINUX";
11743 return elfcore_write_note (abfd
, buf
, bufsiz
,
11744 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
11748 elfcore_write_s390_prefix (bfd
*abfd
,
11751 const void *s390_prefix
,
11754 char *note_name
= "LINUX";
11755 return elfcore_write_note (abfd
, buf
, bufsiz
,
11756 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
11760 elfcore_write_s390_last_break (bfd
*abfd
,
11763 const void *s390_last_break
,
11766 char *note_name
= "LINUX";
11767 return elfcore_write_note (abfd
, buf
, bufsiz
,
11768 note_name
, NT_S390_LAST_BREAK
,
11769 s390_last_break
, size
);
11773 elfcore_write_s390_system_call (bfd
*abfd
,
11776 const void *s390_system_call
,
11779 char *note_name
= "LINUX";
11780 return elfcore_write_note (abfd
, buf
, bufsiz
,
11781 note_name
, NT_S390_SYSTEM_CALL
,
11782 s390_system_call
, size
);
11786 elfcore_write_s390_tdb (bfd
*abfd
,
11789 const void *s390_tdb
,
11792 char *note_name
= "LINUX";
11793 return elfcore_write_note (abfd
, buf
, bufsiz
,
11794 note_name
, NT_S390_TDB
, s390_tdb
, size
);
11798 elfcore_write_s390_vxrs_low (bfd
*abfd
,
11801 const void *s390_vxrs_low
,
11804 char *note_name
= "LINUX";
11805 return elfcore_write_note (abfd
, buf
, bufsiz
,
11806 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
11810 elfcore_write_s390_vxrs_high (bfd
*abfd
,
11813 const void *s390_vxrs_high
,
11816 char *note_name
= "LINUX";
11817 return elfcore_write_note (abfd
, buf
, bufsiz
,
11818 note_name
, NT_S390_VXRS_HIGH
,
11819 s390_vxrs_high
, size
);
11823 elfcore_write_s390_gs_cb (bfd
*abfd
,
11826 const void *s390_gs_cb
,
11829 char *note_name
= "LINUX";
11830 return elfcore_write_note (abfd
, buf
, bufsiz
,
11831 note_name
, NT_S390_GS_CB
,
11836 elfcore_write_s390_gs_bc (bfd
*abfd
,
11839 const void *s390_gs_bc
,
11842 char *note_name
= "LINUX";
11843 return elfcore_write_note (abfd
, buf
, bufsiz
,
11844 note_name
, NT_S390_GS_BC
,
11849 elfcore_write_arm_vfp (bfd
*abfd
,
11852 const void *arm_vfp
,
11855 char *note_name
= "LINUX";
11856 return elfcore_write_note (abfd
, buf
, bufsiz
,
11857 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
11861 elfcore_write_aarch_tls (bfd
*abfd
,
11864 const void *aarch_tls
,
11867 char *note_name
= "LINUX";
11868 return elfcore_write_note (abfd
, buf
, bufsiz
,
11869 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
11873 elfcore_write_aarch_hw_break (bfd
*abfd
,
11876 const void *aarch_hw_break
,
11879 char *note_name
= "LINUX";
11880 return elfcore_write_note (abfd
, buf
, bufsiz
,
11881 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
11885 elfcore_write_aarch_hw_watch (bfd
*abfd
,
11888 const void *aarch_hw_watch
,
11891 char *note_name
= "LINUX";
11892 return elfcore_write_note (abfd
, buf
, bufsiz
,
11893 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
11897 elfcore_write_aarch_sve (bfd
*abfd
,
11900 const void *aarch_sve
,
11903 char *note_name
= "LINUX";
11904 return elfcore_write_note (abfd
, buf
, bufsiz
,
11905 note_name
, NT_ARM_SVE
, aarch_sve
, size
);
11909 elfcore_write_aarch_pauth (bfd
*abfd
,
11912 const void *aarch_pauth
,
11915 char *note_name
= "LINUX";
11916 return elfcore_write_note (abfd
, buf
, bufsiz
,
11917 note_name
, NT_ARM_PAC_MASK
, aarch_pauth
, size
);
11921 elfcore_write_arc_v2 (bfd
*abfd
,
11924 const void *arc_v2
,
11927 char *note_name
= "LINUX";
11928 return elfcore_write_note (abfd
, buf
, bufsiz
,
11929 note_name
, NT_ARC_V2
, arc_v2
, size
);
11933 elfcore_write_register_note (bfd
*abfd
,
11936 const char *section
,
11940 if (strcmp (section
, ".reg2") == 0)
11941 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
11942 if (strcmp (section
, ".reg-xfp") == 0)
11943 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
11944 if (strcmp (section
, ".reg-xstate") == 0)
11945 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
11946 if (strcmp (section
, ".reg-ppc-vmx") == 0)
11947 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
11948 if (strcmp (section
, ".reg-ppc-vsx") == 0)
11949 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
11950 if (strcmp (section
, ".reg-ppc-tar") == 0)
11951 return elfcore_write_ppc_tar (abfd
, buf
, bufsiz
, data
, size
);
11952 if (strcmp (section
, ".reg-ppc-ppr") == 0)
11953 return elfcore_write_ppc_ppr (abfd
, buf
, bufsiz
, data
, size
);
11954 if (strcmp (section
, ".reg-ppc-dscr") == 0)
11955 return elfcore_write_ppc_dscr (abfd
, buf
, bufsiz
, data
, size
);
11956 if (strcmp (section
, ".reg-ppc-ebb") == 0)
11957 return elfcore_write_ppc_ebb (abfd
, buf
, bufsiz
, data
, size
);
11958 if (strcmp (section
, ".reg-ppc-pmu") == 0)
11959 return elfcore_write_ppc_pmu (abfd
, buf
, bufsiz
, data
, size
);
11960 if (strcmp (section
, ".reg-ppc-tm-cgpr") == 0)
11961 return elfcore_write_ppc_tm_cgpr (abfd
, buf
, bufsiz
, data
, size
);
11962 if (strcmp (section
, ".reg-ppc-tm-cfpr") == 0)
11963 return elfcore_write_ppc_tm_cfpr (abfd
, buf
, bufsiz
, data
, size
);
11964 if (strcmp (section
, ".reg-ppc-tm-cvmx") == 0)
11965 return elfcore_write_ppc_tm_cvmx (abfd
, buf
, bufsiz
, data
, size
);
11966 if (strcmp (section
, ".reg-ppc-tm-cvsx") == 0)
11967 return elfcore_write_ppc_tm_cvsx (abfd
, buf
, bufsiz
, data
, size
);
11968 if (strcmp (section
, ".reg-ppc-tm-spr") == 0)
11969 return elfcore_write_ppc_tm_spr (abfd
, buf
, bufsiz
, data
, size
);
11970 if (strcmp (section
, ".reg-ppc-tm-ctar") == 0)
11971 return elfcore_write_ppc_tm_ctar (abfd
, buf
, bufsiz
, data
, size
);
11972 if (strcmp (section
, ".reg-ppc-tm-cppr") == 0)
11973 return elfcore_write_ppc_tm_cppr (abfd
, buf
, bufsiz
, data
, size
);
11974 if (strcmp (section
, ".reg-ppc-tm-cdscr") == 0)
11975 return elfcore_write_ppc_tm_cdscr (abfd
, buf
, bufsiz
, data
, size
);
11976 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
11977 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
11978 if (strcmp (section
, ".reg-s390-timer") == 0)
11979 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
11980 if (strcmp (section
, ".reg-s390-todcmp") == 0)
11981 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
11982 if (strcmp (section
, ".reg-s390-todpreg") == 0)
11983 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
11984 if (strcmp (section
, ".reg-s390-ctrs") == 0)
11985 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
11986 if (strcmp (section
, ".reg-s390-prefix") == 0)
11987 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
11988 if (strcmp (section
, ".reg-s390-last-break") == 0)
11989 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
11990 if (strcmp (section
, ".reg-s390-system-call") == 0)
11991 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
11992 if (strcmp (section
, ".reg-s390-tdb") == 0)
11993 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
11994 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
11995 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
11996 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
11997 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
11998 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
11999 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
12000 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
12001 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
12002 if (strcmp (section
, ".reg-arm-vfp") == 0)
12003 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
12004 if (strcmp (section
, ".reg-aarch-tls") == 0)
12005 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
12006 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
12007 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
12008 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
12009 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
12010 if (strcmp (section
, ".reg-aarch-sve") == 0)
12011 return elfcore_write_aarch_sve (abfd
, buf
, bufsiz
, data
, size
);
12012 if (strcmp (section
, ".reg-aarch-pauth") == 0)
12013 return elfcore_write_aarch_pauth (abfd
, buf
, bufsiz
, data
, size
);
12014 if (strcmp (section
, ".reg-arc-v2") == 0)
12015 return elfcore_write_arc_v2 (abfd
, buf
, bufsiz
, data
, size
);
12020 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
12025 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
12026 gABI specifies that PT_NOTE alignment should be aligned to 4
12027 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
12028 align is less than 4, we use 4 byte alignment. */
12031 if (align
!= 4 && align
!= 8)
12035 while (p
< buf
+ size
)
12037 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
12038 Elf_Internal_Note in
;
12040 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
12043 in
.type
= H_GET_32 (abfd
, xnp
->type
);
12045 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
12046 in
.namedata
= xnp
->name
;
12047 if (in
.namesz
> buf
- in
.namedata
+ size
)
12050 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
12051 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
12052 in
.descpos
= offset
+ (in
.descdata
- buf
);
12054 && (in
.descdata
>= buf
+ size
12055 || in
.descsz
> buf
- in
.descdata
+ size
))
12058 switch (bfd_get_format (abfd
))
12065 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
12068 const char * string
;
12070 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
12074 GROKER_ELEMENT ("", elfcore_grok_note
),
12075 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
12076 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
12077 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
12078 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
12079 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
),
12080 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note
)
12082 #undef GROKER_ELEMENT
12085 for (i
= ARRAY_SIZE (grokers
); i
--;)
12087 if (in
.namesz
>= grokers
[i
].len
12088 && strncmp (in
.namedata
, grokers
[i
].string
,
12089 grokers
[i
].len
) == 0)
12091 if (! grokers
[i
].func (abfd
, & in
))
12100 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
12102 if (! elfobj_grok_gnu_note (abfd
, &in
))
12105 else if (in
.namesz
== sizeof "stapsdt"
12106 && strcmp (in
.namedata
, "stapsdt") == 0)
12108 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
12114 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
12121 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
12126 if (size
== 0 || (size
+ 1) == 0)
12129 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
12132 buf
= (char *) _bfd_malloc_and_read (abfd
, size
+ 1, size
);
12136 /* PR 17512: file: ec08f814
12137 0-termintate the buffer so that string searches will not overflow. */
12140 if (!elf_parse_notes (abfd
, buf
, size
, offset
, align
))
12150 /* Providing external access to the ELF program header table. */
12152 /* Return an upper bound on the number of bytes required to store a
12153 copy of ABFD's program header table entries. Return -1 if an error
12154 occurs; bfd_get_error will return an appropriate code. */
12157 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
12159 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12161 bfd_set_error (bfd_error_wrong_format
);
12165 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
12168 /* Copy ABFD's program header table entries to *PHDRS. The entries
12169 will be stored as an array of Elf_Internal_Phdr structures, as
12170 defined in include/elf/internal.h. To find out how large the
12171 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
12173 Return the number of program header table entries read, or -1 if an
12174 error occurs; bfd_get_error will return an appropriate code. */
12177 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
12181 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
12183 bfd_set_error (bfd_error_wrong_format
);
12187 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
12188 if (num_phdrs
!= 0)
12189 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
12190 num_phdrs
* sizeof (Elf_Internal_Phdr
));
12195 enum elf_reloc_type_class
12196 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
12197 const asection
*rel_sec ATTRIBUTE_UNUSED
,
12198 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
12200 return reloc_class_normal
;
12203 /* For RELA architectures, return the relocation value for a
12204 relocation against a local symbol. */
12207 _bfd_elf_rela_local_sym (bfd
*abfd
,
12208 Elf_Internal_Sym
*sym
,
12210 Elf_Internal_Rela
*rel
)
12212 asection
*sec
= *psec
;
12213 bfd_vma relocation
;
12215 relocation
= (sec
->output_section
->vma
12216 + sec
->output_offset
12218 if ((sec
->flags
& SEC_MERGE
)
12219 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
12220 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
12223 _bfd_merged_section_offset (abfd
, psec
,
12224 elf_section_data (sec
)->sec_info
,
12225 sym
->st_value
+ rel
->r_addend
);
12228 /* If we have changed the section, and our original section is
12229 marked with SEC_EXCLUDE, it means that the original
12230 SEC_MERGE section has been completely subsumed in some
12231 other SEC_MERGE section. In this case, we need to leave
12232 some info around for --emit-relocs. */
12233 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
12234 sec
->kept_section
= *psec
;
12237 rel
->r_addend
-= relocation
;
12238 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
12244 _bfd_elf_rel_local_sym (bfd
*abfd
,
12245 Elf_Internal_Sym
*sym
,
12249 asection
*sec
= *psec
;
12251 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
12252 return sym
->st_value
+ addend
;
12254 return _bfd_merged_section_offset (abfd
, psec
,
12255 elf_section_data (sec
)->sec_info
,
12256 sym
->st_value
+ addend
);
12259 /* Adjust an address within a section. Given OFFSET within SEC, return
12260 the new offset within the section, based upon changes made to the
12261 section. Returns -1 if the offset is now invalid.
12262 The offset (in abnd out) is in target sized bytes, however big a
12266 _bfd_elf_section_offset (bfd
*abfd
,
12267 struct bfd_link_info
*info
,
12271 switch (sec
->sec_info_type
)
12273 case SEC_INFO_TYPE_STABS
:
12274 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
12276 case SEC_INFO_TYPE_EH_FRAME
:
12277 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
12280 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
12282 /* Reverse the offset. */
12283 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12284 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
12286 /* address_size and sec->size are in octets. Convert
12287 to bytes before subtracting the original offset. */
12288 offset
= ((sec
->size
- address_size
)
12289 / bfd_octets_per_byte (abfd
, sec
) - offset
);
12295 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
12296 reconstruct an ELF file by reading the segments out of remote memory
12297 based on the ELF file header at EHDR_VMA and the ELF program headers it
12298 points to. If not null, *LOADBASEP is filled in with the difference
12299 between the VMAs from which the segments were read, and the VMAs the
12300 file headers (and hence BFD's idea of each section's VMA) put them at.
12302 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
12303 remote memory at target address VMA into the local buffer at MYADDR; it
12304 should return zero on success or an `errno' code on failure. TEMPL must
12305 be a BFD for an ELF target with the word size and byte order found in
12306 the remote memory. */
12309 bfd_elf_bfd_from_remote_memory
12312 bfd_size_type size
,
12313 bfd_vma
*loadbasep
,
12314 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
12316 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
12317 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
12321 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
12322 long symcount ATTRIBUTE_UNUSED
,
12323 asymbol
**syms ATTRIBUTE_UNUSED
,
12328 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
12331 const char *relplt_name
;
12332 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
12336 Elf_Internal_Shdr
*hdr
;
12342 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
12345 if (dynsymcount
<= 0)
12348 if (!bed
->plt_sym_val
)
12351 relplt_name
= bed
->relplt_name
;
12352 if (relplt_name
== NULL
)
12353 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
12354 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
12355 if (relplt
== NULL
)
12358 hdr
= &elf_section_data (relplt
)->this_hdr
;
12359 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
12360 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
12363 plt
= bfd_get_section_by_name (abfd
, ".plt");
12367 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
12368 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
12371 count
= relplt
->size
/ hdr
->sh_entsize
;
12372 size
= count
* sizeof (asymbol
);
12373 p
= relplt
->relocation
;
12374 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12376 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
12377 if (p
->addend
!= 0)
12380 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
12382 size
+= sizeof ("+0x") - 1 + 8;
12387 s
= *ret
= (asymbol
*) bfd_malloc (size
);
12391 names
= (char *) (s
+ count
);
12392 p
= relplt
->relocation
;
12394 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
12399 addr
= bed
->plt_sym_val (i
, plt
, p
);
12400 if (addr
== (bfd_vma
) -1)
12403 *s
= **p
->sym_ptr_ptr
;
12404 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
12405 we are defining a symbol, ensure one of them is set. */
12406 if ((s
->flags
& BSF_LOCAL
) == 0)
12407 s
->flags
|= BSF_GLOBAL
;
12408 s
->flags
|= BSF_SYNTHETIC
;
12410 s
->value
= addr
- plt
->vma
;
12413 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
12414 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
12416 if (p
->addend
!= 0)
12420 memcpy (names
, "+0x", sizeof ("+0x") - 1);
12421 names
+= sizeof ("+0x") - 1;
12422 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
12423 for (a
= buf
; *a
== '0'; ++a
)
12426 memcpy (names
, a
, len
);
12429 memcpy (names
, "@plt", sizeof ("@plt"));
12430 names
+= sizeof ("@plt");
12437 /* It is only used by x86-64 so far.
12438 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
12439 but current usage would allow all of _bfd_std_section to be zero. */
12440 static const asymbol lcomm_sym
12441 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
12442 asection _bfd_elf_large_com_section
12443 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
12444 "LARGE_COMMON", 0, SEC_IS_COMMON
);
12447 _bfd_elf_final_write_processing (bfd
*abfd
)
12449 Elf_Internal_Ehdr
*i_ehdrp
; /* ELF file header, internal form. */
12451 i_ehdrp
= elf_elfheader (abfd
);
12453 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12454 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
12456 /* Set the osabi field to ELFOSABI_GNU if the binary contains
12457 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or
12458 STB_GNU_UNIQUE binding. */
12459 if (elf_tdata (abfd
)->has_gnu_osabi
!= 0)
12461 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
)
12462 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
12463 else if (i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_GNU
12464 && i_ehdrp
->e_ident
[EI_OSABI
] != ELFOSABI_FREEBSD
)
12466 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_mbind
)
12467 _bfd_error_handler (_("GNU_MBIND section is unsupported"));
12468 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_ifunc
)
12469 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported"));
12470 if (elf_tdata (abfd
)->has_gnu_osabi
& elf_gnu_osabi_unique
)
12471 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported"));
12472 bfd_set_error (bfd_error_sorry
);
12480 /* Return TRUE for ELF symbol types that represent functions.
12481 This is the default version of this function, which is sufficient for
12482 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
12485 _bfd_elf_is_function_type (unsigned int type
)
12487 return (type
== STT_FUNC
12488 || type
== STT_GNU_IFUNC
);
12491 /* If the ELF symbol SYM might be a function in SEC, return the
12492 function size and set *CODE_OFF to the function's entry point,
12493 otherwise return zero. */
12496 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
12499 bfd_size_type size
;
12501 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
12502 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
12503 || sym
->section
!= sec
)
12506 *code_off
= sym
->value
;
12508 if (!(sym
->flags
& BSF_SYNTHETIC
))
12509 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;
12515 /* Set to non-zero to enable some debug messages. */
12516 #define DEBUG_SECONDARY_RELOCS 0
12518 /* An internal-to-the-bfd-library only section type
12519 used to indicate a cached secondary reloc section. */
12520 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA)
12522 /* Create a BFD section to hold a secondary reloc section. */
12525 _bfd_elf_init_secondary_reloc_section (bfd
* abfd
,
12526 Elf_Internal_Shdr
*hdr
,
12528 unsigned int shindex
)
12530 /* We only support RELA secondary relocs. */
12531 if (hdr
->sh_type
!= SHT_RELA
)
12534 #if DEBUG_SECONDARY_RELOCS
12535 fprintf (stderr
, "secondary reloc section %s encountered\n", name
);
12537 hdr
->sh_type
= SHT_SECONDARY_RELOC
;
12538 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
12541 /* Read in any secondary relocs associated with SEC. */
12544 _bfd_elf_slurp_secondary_reloc_section (bfd
* abfd
,
12546 asymbol
** symbols
)
12548 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12550 bfd_boolean result
= TRUE
;
12551 bfd_vma (*r_sym
) (bfd_vma
);
12553 #if BFD_DEFAULT_TARGET_SIZE > 32
12554 if (bfd_arch_bits_per_address (abfd
) != 32)
12555 r_sym
= elf64_r_sym
;
12558 r_sym
= elf32_r_sym
;
12560 /* Discover if there are any secondary reloc sections
12561 associated with SEC. */
12562 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12564 Elf_Internal_Shdr
* hdr
= & elf_section_data (relsec
)->this_hdr
;
12566 if (hdr
->sh_type
== SHT_SECONDARY_RELOC
12567 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
12568 && (hdr
->sh_entsize
== ebd
->s
->sizeof_rel
12569 || hdr
->sh_entsize
== ebd
->s
->sizeof_rela
))
12571 bfd_byte
* native_relocs
;
12572 bfd_byte
* native_reloc
;
12573 arelent
* internal_relocs
;
12574 arelent
* internal_reloc
;
12576 unsigned int entsize
;
12577 unsigned int symcount
;
12578 unsigned int reloc_count
;
12581 if (ebd
->elf_info_to_howto
== NULL
)
12584 #if DEBUG_SECONDARY_RELOCS
12585 fprintf (stderr
, "read secondary relocs for %s from %s\n",
12586 sec
->name
, relsec
->name
);
12588 entsize
= hdr
->sh_entsize
;
12590 native_relocs
= bfd_malloc (hdr
->sh_size
);
12591 if (native_relocs
== NULL
)
12597 reloc_count
= NUM_SHDR_ENTRIES (hdr
);
12598 if (_bfd_mul_overflow (reloc_count
, sizeof (arelent
), & amt
))
12600 free (native_relocs
);
12601 bfd_set_error (bfd_error_file_too_big
);
12606 internal_relocs
= (arelent
*) bfd_alloc (abfd
, amt
);
12607 if (internal_relocs
== NULL
)
12609 free (native_relocs
);
12614 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
12615 || (bfd_bread (native_relocs
, hdr
->sh_size
, abfd
)
12618 free (native_relocs
);
12619 /* The internal_relocs will be freed when
12620 the memory for the bfd is released. */
12625 symcount
= bfd_get_symcount (abfd
);
12627 for (i
= 0, internal_reloc
= internal_relocs
,
12628 native_reloc
= native_relocs
;
12630 i
++, internal_reloc
++, native_reloc
+= entsize
)
12633 Elf_Internal_Rela rela
;
12635 if (entsize
== ebd
->s
->sizeof_rel
)
12636 ebd
->s
->swap_reloc_in (abfd
, native_reloc
, & rela
);
12637 else /* entsize == ebd->s->sizeof_rela */
12638 ebd
->s
->swap_reloca_in (abfd
, native_reloc
, & rela
);
12640 /* The address of an ELF reloc is section relative for an object
12641 file, and absolute for an executable file or shared library.
12642 The address of a normal BFD reloc is always section relative,
12643 and the address of a dynamic reloc is absolute.. */
12644 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0)
12645 internal_reloc
->address
= rela
.r_offset
;
12647 internal_reloc
->address
= rela
.r_offset
- sec
->vma
;
12649 if (r_sym (rela
.r_info
) == STN_UNDEF
)
12651 /* FIXME: This and the error case below mean that we
12652 have a symbol on relocs that is not elf_symbol_type. */
12653 internal_reloc
->sym_ptr_ptr
=
12654 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12656 else if (r_sym (rela
.r_info
) > symcount
)
12659 /* xgettext:c-format */
12660 (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
12661 abfd
, sec
, i
, (long) r_sym (rela
.r_info
));
12662 bfd_set_error (bfd_error_bad_value
);
12663 internal_reloc
->sym_ptr_ptr
=
12664 bfd_abs_section_ptr
->symbol_ptr_ptr
;
12671 ps
= symbols
+ r_sym (rela
.r_info
) - 1;
12673 internal_reloc
->sym_ptr_ptr
= ps
;
12674 /* Make sure that this symbol is not removed by strip. */
12675 (*ps
)->flags
|= BSF_KEEP
;
12678 internal_reloc
->addend
= rela
.r_addend
;
12680 res
= ebd
->elf_info_to_howto (abfd
, internal_reloc
, & rela
);
12681 if (! res
|| internal_reloc
->howto
== NULL
)
12683 #if DEBUG_SECONDARY_RELOCS
12684 fprintf (stderr
, "there is no howto associated with reloc %lx\n",
12691 free (native_relocs
);
12692 /* Store the internal relocs. */
12693 elf_section_data (relsec
)->sec_info
= internal_relocs
;
12700 /* Set the ELF section header fields of an output secondary reloc section. */
12703 _bfd_elf_copy_special_section_fields (const bfd
* ibfd ATTRIBUTE_UNUSED
,
12704 bfd
* obfd ATTRIBUTE_UNUSED
,
12705 const Elf_Internal_Shdr
* isection
,
12706 Elf_Internal_Shdr
* osection
)
12710 struct bfd_elf_section_data
* esd
;
12712 if (isection
== NULL
)
12715 if (isection
->sh_type
!= SHT_SECONDARY_RELOC
)
12718 isec
= isection
->bfd_section
;
12722 osec
= osection
->bfd_section
;
12726 esd
= elf_section_data (osec
);
12727 BFD_ASSERT (esd
->sec_info
== NULL
);
12728 esd
->sec_info
= elf_section_data (isec
)->sec_info
;
12729 osection
->sh_type
= SHT_RELA
;
12730 osection
->sh_link
= elf_onesymtab (obfd
);
12731 if (osection
->sh_link
== 0)
12733 /* There is no symbol table - we are hosed... */
12735 /* xgettext:c-format */
12736 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"),
12738 bfd_set_error (bfd_error_bad_value
);
12742 /* Find the output section that corresponds to the isection's sh_info link. */
12743 if (isection
->sh_info
== 0
12744 || isection
->sh_info
>= elf_numsections (ibfd
))
12747 /* xgettext:c-format */
12748 (_("%pB(%pA): info section index is invalid"),
12750 bfd_set_error (bfd_error_bad_value
);
12754 isection
= elf_elfsections (ibfd
)[isection
->sh_info
];
12756 if (isection
== NULL
12757 || isection
->bfd_section
== NULL
12758 || isection
->bfd_section
->output_section
== NULL
)
12761 /* xgettext:c-format */
12762 (_("%pB(%pA): info section index cannot be set because the section is not in the output"),
12764 bfd_set_error (bfd_error_bad_value
);
12768 esd
= elf_section_data (isection
->bfd_section
->output_section
);
12769 BFD_ASSERT (esd
!= NULL
);
12770 osection
->sh_info
= esd
->this_idx
;
12771 esd
->has_secondary_relocs
= TRUE
;
12772 #if DEBUG_SECONDARY_RELOCS
12773 fprintf (stderr
, "update header of %s, sh_link = %u, sh_info = %u\n",
12774 osec
->name
, osection
->sh_link
, osection
->sh_info
);
12775 fprintf (stderr
, "mark section %s as having secondary relocs\n",
12776 bfd_section_name (isection
->bfd_section
->output_section
));
12782 /* Write out a secondary reloc section.
12784 FIXME: Currently this function can result in a serious performance penalty
12785 for files with secondary relocs and lots of sections. The proper way to
12786 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
12787 relocs together and then to have this function just walk that chain. */
12790 _bfd_elf_write_secondary_reloc_section (bfd
*abfd
, asection
*sec
)
12792 const struct elf_backend_data
* const ebd
= get_elf_backend_data (abfd
);
12793 bfd_vma addr_offset
;
12795 bfd_vma (*r_info
) (bfd_vma
, bfd_vma
);
12796 bfd_boolean result
= TRUE
;
12801 #if BFD_DEFAULT_TARGET_SIZE > 32
12802 if (bfd_arch_bits_per_address (abfd
) != 32)
12803 r_info
= elf64_r_info
;
12806 r_info
= elf32_r_info
;
12808 /* The address of an ELF reloc is section relative for an object
12809 file, and absolute for an executable file or shared library.
12810 The address of a BFD reloc is always section relative. */
12812 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) != 0)
12813 addr_offset
= sec
->vma
;
12815 /* Discover if there are any secondary reloc sections
12816 associated with SEC. */
12817 for (relsec
= abfd
->sections
; relsec
!= NULL
; relsec
= relsec
->next
)
12819 const struct bfd_elf_section_data
* const esd
= elf_section_data (relsec
);
12820 Elf_Internal_Shdr
* const hdr
= (Elf_Internal_Shdr
*) & esd
->this_hdr
;
12822 if (hdr
->sh_type
== SHT_RELA
12823 && hdr
->sh_info
== (unsigned) elf_section_data (sec
)->this_idx
)
12825 asymbol
* last_sym
;
12827 unsigned int reloc_count
;
12829 unsigned int entsize
;
12830 arelent
* src_irel
;
12831 bfd_byte
* dst_rela
;
12833 if (hdr
->contents
!= NULL
)
12836 /* xgettext:c-format */
12837 (_("%pB(%pA): error: secondary reloc section processed twice"),
12839 bfd_set_error (bfd_error_bad_value
);
12844 entsize
= hdr
->sh_entsize
;
12848 /* xgettext:c-format */
12849 (_("%pB(%pA): error: secondary reloc section has zero sized entries"),
12851 bfd_set_error (bfd_error_bad_value
);
12855 else if (entsize
!= ebd
->s
->sizeof_rel
12856 && entsize
!= ebd
->s
->sizeof_rela
)
12859 /* xgettext:c-format */
12860 (_("%pB(%pA): error: secondary reloc section has non-standard sized entries"),
12862 bfd_set_error (bfd_error_bad_value
);
12867 reloc_count
= hdr
->sh_size
/ entsize
;
12868 if (reloc_count
<= 0)
12871 /* xgettext:c-format */
12872 (_("%pB(%pA): error: secondary reloc section is empty!"),
12874 bfd_set_error (bfd_error_bad_value
);
12879 hdr
->contents
= bfd_alloc (abfd
, hdr
->sh_size
);
12880 if (hdr
->contents
== NULL
)
12883 #if DEBUG_SECONDARY_RELOCS
12884 fprintf (stderr
, "write %u secondary relocs for %s from %s\n",
12885 reloc_count
, sec
->name
, relsec
->name
);
12889 dst_rela
= hdr
->contents
;
12890 src_irel
= (arelent
*) esd
->sec_info
;
12891 if (src_irel
== NULL
)
12894 /* xgettext:c-format */
12895 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"),
12897 bfd_set_error (bfd_error_bad_value
);
12902 for (idx
= 0; idx
< reloc_count
; idx
++, dst_rela
+= entsize
)
12904 Elf_Internal_Rela src_rela
;
12909 ptr
= src_irel
+ idx
;
12913 /* xgettext:c-format */
12914 (_("%pB(%pA): error: reloc table entry %u is empty"),
12915 abfd
, relsec
, idx
);
12916 bfd_set_error (bfd_error_bad_value
);
12921 if (ptr
->sym_ptr_ptr
== NULL
)
12923 /* FIXME: Is this an error ? */
12928 sym
= *ptr
->sym_ptr_ptr
;
12930 if (sym
== last_sym
)
12934 n
= _bfd_elf_symbol_from_bfd_symbol (abfd
, & sym
);
12938 /* xgettext:c-format */
12939 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"),
12940 abfd
, relsec
, idx
);
12941 bfd_set_error (bfd_error_bad_value
);
12950 if (sym
->the_bfd
!= NULL
12951 && sym
->the_bfd
->xvec
!= abfd
->xvec
12952 && ! _bfd_elf_validate_reloc (abfd
, ptr
))
12955 /* xgettext:c-format */
12956 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"),
12957 abfd
, relsec
, idx
);
12958 bfd_set_error (bfd_error_bad_value
);
12964 src_rela
.r_offset
= ptr
->address
+ addr_offset
;
12965 if (ptr
->howto
== NULL
)
12968 /* xgettext:c-format */
12969 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"),
12970 abfd
, relsec
, idx
);
12971 bfd_set_error (bfd_error_bad_value
);
12973 src_rela
.r_info
= r_info (0, 0);
12976 src_rela
.r_info
= r_info (n
, ptr
->howto
->type
);
12977 src_rela
.r_addend
= ptr
->addend
;
12979 if (entsize
== ebd
->s
->sizeof_rel
)
12980 ebd
->s
->swap_reloc_out (abfd
, &src_rela
, dst_rela
);
12981 else /* entsize == ebd->s->sizeof_rela */
12982 ebd
->s
->swap_reloca_out (abfd
, &src_rela
, dst_rela
);