1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2018 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. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-core.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
53 static bfd_boolean
prep_headers (bfd
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct elf_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
,
57 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
58 file_ptr offset
, size_t align
);
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
67 _bfd_elf_swap_verdef_in (bfd
*abfd
,
68 const Elf_External_Verdef
*src
,
69 Elf_Internal_Verdef
*dst
)
71 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
72 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
73 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
74 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
75 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
76 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
77 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
80 /* Swap out a Verdef structure. */
83 _bfd_elf_swap_verdef_out (bfd
*abfd
,
84 const Elf_Internal_Verdef
*src
,
85 Elf_External_Verdef
*dst
)
87 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
88 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
89 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
90 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
91 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
92 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
93 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
96 /* Swap in a Verdaux structure. */
99 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
100 const Elf_External_Verdaux
*src
,
101 Elf_Internal_Verdaux
*dst
)
103 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
104 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
107 /* Swap out a Verdaux structure. */
110 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
111 const Elf_Internal_Verdaux
*src
,
112 Elf_External_Verdaux
*dst
)
114 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
115 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
118 /* Swap in a Verneed structure. */
121 _bfd_elf_swap_verneed_in (bfd
*abfd
,
122 const Elf_External_Verneed
*src
,
123 Elf_Internal_Verneed
*dst
)
125 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
126 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
127 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
128 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
129 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
132 /* Swap out a Verneed structure. */
135 _bfd_elf_swap_verneed_out (bfd
*abfd
,
136 const Elf_Internal_Verneed
*src
,
137 Elf_External_Verneed
*dst
)
139 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
140 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
141 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
142 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
143 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
146 /* Swap in a Vernaux structure. */
149 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
150 const Elf_External_Vernaux
*src
,
151 Elf_Internal_Vernaux
*dst
)
153 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
154 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
155 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
156 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
157 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
160 /* Swap out a Vernaux structure. */
163 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
164 const Elf_Internal_Vernaux
*src
,
165 Elf_External_Vernaux
*dst
)
167 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
168 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
169 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
170 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
171 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
174 /* Swap in a Versym structure. */
177 _bfd_elf_swap_versym_in (bfd
*abfd
,
178 const Elf_External_Versym
*src
,
179 Elf_Internal_Versym
*dst
)
181 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
184 /* Swap out a Versym structure. */
187 _bfd_elf_swap_versym_out (bfd
*abfd
,
188 const Elf_Internal_Versym
*src
,
189 Elf_External_Versym
*dst
)
191 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
194 /* Standard ELF hash function. Do not change this function; you will
195 cause invalid hash tables to be generated. */
198 bfd_elf_hash (const char *namearg
)
200 const unsigned char *name
= (const unsigned char *) namearg
;
205 while ((ch
= *name
++) != '\0')
208 if ((g
= (h
& 0xf0000000)) != 0)
211 /* The ELF ABI says `h &= ~g', but this is equivalent in
212 this case and on some machines one insn instead of two. */
216 return h
& 0xffffffff;
219 /* DT_GNU_HASH hash function. Do not change this function; you will
220 cause invalid hash tables to be generated. */
223 bfd_elf_gnu_hash (const char *namearg
)
225 const unsigned char *name
= (const unsigned char *) namearg
;
226 unsigned long h
= 5381;
229 while ((ch
= *name
++) != '\0')
230 h
= (h
<< 5) + h
+ ch
;
231 return h
& 0xffffffff;
234 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
235 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
237 bfd_elf_allocate_object (bfd
*abfd
,
239 enum elf_target_id object_id
)
241 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
242 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
243 if (abfd
->tdata
.any
== NULL
)
246 elf_object_id (abfd
) = object_id
;
247 if (abfd
->direction
!= read_direction
)
249 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
252 elf_tdata (abfd
)->o
= o
;
253 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
260 bfd_elf_make_object (bfd
*abfd
)
262 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
263 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
268 bfd_elf_mkcorefile (bfd
*abfd
)
270 /* I think this can be done just like an object file. */
271 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
273 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
274 return elf_tdata (abfd
)->core
!= NULL
;
278 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
280 Elf_Internal_Shdr
**i_shdrp
;
281 bfd_byte
*shstrtab
= NULL
;
283 bfd_size_type shstrtabsize
;
285 i_shdrp
= elf_elfsections (abfd
);
287 || shindex
>= elf_numsections (abfd
)
288 || i_shdrp
[shindex
] == 0)
291 shstrtab
= i_shdrp
[shindex
]->contents
;
292 if (shstrtab
== NULL
)
294 /* No cached one, attempt to read, and cache what we read. */
295 offset
= i_shdrp
[shindex
]->sh_offset
;
296 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
298 /* Allocate and clear an extra byte at the end, to prevent crashes
299 in case the string table is not terminated. */
300 if (shstrtabsize
+ 1 <= 1
301 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
302 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
304 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
306 if (bfd_get_error () != bfd_error_system_call
)
307 bfd_set_error (bfd_error_file_truncated
);
308 bfd_release (abfd
, shstrtab
);
310 /* Once we've failed to read it, make sure we don't keep
311 trying. Otherwise, we'll keep allocating space for
312 the string table over and over. */
313 i_shdrp
[shindex
]->sh_size
= 0;
316 shstrtab
[shstrtabsize
] = '\0';
317 i_shdrp
[shindex
]->contents
= shstrtab
;
319 return (char *) shstrtab
;
323 bfd_elf_string_from_elf_section (bfd
*abfd
,
324 unsigned int shindex
,
325 unsigned int strindex
)
327 Elf_Internal_Shdr
*hdr
;
332 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
335 hdr
= elf_elfsections (abfd
)[shindex
];
337 if (hdr
->contents
== NULL
)
339 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
341 /* PR 17512: file: f057ec89. */
342 /* xgettext:c-format */
343 _bfd_error_handler (_("%B: attempt to load strings from"
344 " a non-string section (number %d)"),
349 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
353 if (strindex
>= hdr
->sh_size
)
355 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
357 /* xgettext:c-format */
358 (_("%B: invalid string offset %u >= %Lu for section `%s'"),
359 abfd
, strindex
, hdr
->sh_size
,
360 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
362 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
366 return ((char *) hdr
->contents
) + strindex
;
369 /* Read and convert symbols to internal format.
370 SYMCOUNT specifies the number of symbols to read, starting from
371 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
372 are non-NULL, they are used to store the internal symbols, external
373 symbols, and symbol section index extensions, respectively.
374 Returns a pointer to the internal symbol buffer (malloced if necessary)
375 or NULL if there were no symbols or some kind of problem. */
378 bfd_elf_get_elf_syms (bfd
*ibfd
,
379 Elf_Internal_Shdr
*symtab_hdr
,
382 Elf_Internal_Sym
*intsym_buf
,
384 Elf_External_Sym_Shndx
*extshndx_buf
)
386 Elf_Internal_Shdr
*shndx_hdr
;
388 const bfd_byte
*esym
;
389 Elf_External_Sym_Shndx
*alloc_extshndx
;
390 Elf_External_Sym_Shndx
*shndx
;
391 Elf_Internal_Sym
*alloc_intsym
;
392 Elf_Internal_Sym
*isym
;
393 Elf_Internal_Sym
*isymend
;
394 const struct elf_backend_data
*bed
;
399 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
405 /* Normal syms might have section extension entries. */
407 if (elf_symtab_shndx_list (ibfd
) != NULL
)
409 elf_section_list
* entry
;
410 Elf_Internal_Shdr
**sections
= elf_elfsections (ibfd
);
412 /* Find an index section that is linked to this symtab section. */
413 for (entry
= elf_symtab_shndx_list (ibfd
); entry
!= NULL
; entry
= entry
->next
)
416 if (entry
->hdr
.sh_link
>= elf_numsections (ibfd
))
419 if (sections
[entry
->hdr
.sh_link
] == symtab_hdr
)
421 shndx_hdr
= & entry
->hdr
;
426 if (shndx_hdr
== NULL
)
428 if (symtab_hdr
== & elf_symtab_hdr (ibfd
))
429 /* Not really accurate, but this was how the old code used to work. */
430 shndx_hdr
= & elf_symtab_shndx_list (ibfd
)->hdr
;
431 /* Otherwise we do nothing. The assumption is that
432 the index table will not be needed. */
436 /* Read the symbols. */
438 alloc_extshndx
= NULL
;
440 bed
= get_elf_backend_data (ibfd
);
441 extsym_size
= bed
->s
->sizeof_sym
;
442 amt
= (bfd_size_type
) symcount
* extsym_size
;
443 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
444 if (extsym_buf
== NULL
)
446 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
447 extsym_buf
= alloc_ext
;
449 if (extsym_buf
== NULL
450 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
451 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
457 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
461 amt
= (bfd_size_type
) symcount
* sizeof (Elf_External_Sym_Shndx
);
462 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
463 if (extshndx_buf
== NULL
)
465 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
466 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
467 extshndx_buf
= alloc_extshndx
;
469 if (extshndx_buf
== NULL
470 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
471 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
478 if (intsym_buf
== NULL
)
480 alloc_intsym
= (Elf_Internal_Sym
*)
481 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
482 intsym_buf
= alloc_intsym
;
483 if (intsym_buf
== NULL
)
487 /* Convert the symbols to internal form. */
488 isymend
= intsym_buf
+ symcount
;
489 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
490 shndx
= extshndx_buf
;
492 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
493 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
495 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
496 /* xgettext:c-format */
497 _bfd_error_handler (_("%B symbol number %lu references"
498 " nonexistent SHT_SYMTAB_SHNDX section"),
499 ibfd
, (unsigned long) symoffset
);
500 if (alloc_intsym
!= NULL
)
507 if (alloc_ext
!= NULL
)
509 if (alloc_extshndx
!= NULL
)
510 free (alloc_extshndx
);
515 /* Look up a symbol name. */
517 bfd_elf_sym_name (bfd
*abfd
,
518 Elf_Internal_Shdr
*symtab_hdr
,
519 Elf_Internal_Sym
*isym
,
523 unsigned int iname
= isym
->st_name
;
524 unsigned int shindex
= symtab_hdr
->sh_link
;
526 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
527 /* Check for a bogus st_shndx to avoid crashing. */
528 && isym
->st_shndx
< elf_numsections (abfd
))
530 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
531 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
534 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
537 else if (sym_sec
&& *name
== '\0')
538 name
= bfd_section_name (abfd
, sym_sec
);
543 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
544 sections. The first element is the flags, the rest are section
547 typedef union elf_internal_group
{
548 Elf_Internal_Shdr
*shdr
;
550 } Elf_Internal_Group
;
552 /* Return the name of the group signature symbol. Why isn't the
553 signature just a string? */
556 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
558 Elf_Internal_Shdr
*hdr
;
559 unsigned char esym
[sizeof (Elf64_External_Sym
)];
560 Elf_External_Sym_Shndx eshndx
;
561 Elf_Internal_Sym isym
;
563 /* First we need to ensure the symbol table is available. Make sure
564 that it is a symbol table section. */
565 if (ghdr
->sh_link
>= elf_numsections (abfd
))
567 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
568 if (hdr
->sh_type
!= SHT_SYMTAB
569 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
572 /* Go read the symbol. */
573 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
574 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
575 &isym
, esym
, &eshndx
) == NULL
)
578 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
581 /* Set next_in_group list pointer, and group name for NEWSECT. */
584 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
586 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
588 /* If num_group is zero, read in all SHT_GROUP sections. The count
589 is set to -1 if there are no SHT_GROUP sections. */
592 unsigned int i
, shnum
;
594 /* First count the number of groups. If we have a SHT_GROUP
595 section with just a flag word (ie. sh_size is 4), ignore it. */
596 shnum
= elf_numsections (abfd
);
599 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
600 ( (shdr)->sh_type == SHT_GROUP \
601 && (shdr)->sh_size >= minsize \
602 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
603 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
605 for (i
= 0; i
< shnum
; i
++)
607 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
609 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
615 num_group
= (unsigned) -1;
616 elf_tdata (abfd
)->num_group
= num_group
;
617 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
621 /* We keep a list of elf section headers for group sections,
622 so we can find them quickly. */
625 elf_tdata (abfd
)->num_group
= num_group
;
626 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
627 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
628 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
630 memset (elf_tdata (abfd
)->group_sect_ptr
, 0, num_group
* sizeof (Elf_Internal_Shdr
*));
633 for (i
= 0; i
< shnum
; i
++)
635 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
637 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
640 Elf_Internal_Group
*dest
;
642 /* Make sure the group section has a BFD section
644 if (!bfd_section_from_shdr (abfd
, i
))
647 /* Add to list of sections. */
648 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
651 /* Read the raw contents. */
652 BFD_ASSERT (sizeof (*dest
) >= 4);
653 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
654 shdr
->contents
= (unsigned char *)
655 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
656 /* PR binutils/4110: Handle corrupt group headers. */
657 if (shdr
->contents
== NULL
)
660 /* xgettext:c-format */
661 (_("%B: corrupt size field in group section"
662 " header: %#Lx"), abfd
, shdr
->sh_size
);
663 bfd_set_error (bfd_error_bad_value
);
668 memset (shdr
->contents
, 0, amt
);
670 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
671 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
675 /* xgettext:c-format */
676 (_("%B: invalid size field in group section"
677 " header: %#Lx"), abfd
, shdr
->sh_size
);
678 bfd_set_error (bfd_error_bad_value
);
680 /* PR 17510: If the group contents are even
681 partially corrupt, do not allow any of the
682 contents to be used. */
683 memset (shdr
->contents
, 0, amt
);
687 /* Translate raw contents, a flag word followed by an
688 array of elf section indices all in target byte order,
689 to the flag word followed by an array of elf section
691 src
= shdr
->contents
+ shdr
->sh_size
;
692 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
700 idx
= H_GET_32 (abfd
, src
);
701 if (src
== shdr
->contents
)
704 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
705 shdr
->bfd_section
->flags
706 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
712 (_("%B: invalid SHT_GROUP entry"), abfd
);
715 dest
->shdr
= elf_elfsections (abfd
)[idx
];
720 /* PR 17510: Corrupt binaries might contain invalid groups. */
721 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
723 elf_tdata (abfd
)->num_group
= num_group
;
725 /* If all groups are invalid then fail. */
728 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
729 elf_tdata (abfd
)->num_group
= num_group
= -1;
731 (_("%B: no valid group sections found"), abfd
);
732 bfd_set_error (bfd_error_bad_value
);
738 if (num_group
!= (unsigned) -1)
740 unsigned int search_offset
= elf_tdata (abfd
)->group_search_offset
;
743 for (j
= 0; j
< num_group
; j
++)
745 /* Begin search from previous found group. */
746 unsigned i
= (j
+ search_offset
) % num_group
;
748 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
749 Elf_Internal_Group
*idx
;
755 idx
= (Elf_Internal_Group
*) shdr
->contents
;
756 if (idx
== NULL
|| shdr
->sh_size
< 4)
758 /* See PR 21957 for a reproducer. */
759 /* xgettext:c-format */
760 _bfd_error_handler (_("%B: group section '%A' has no contents"),
761 abfd
, shdr
->bfd_section
);
762 elf_tdata (abfd
)->group_sect_ptr
[i
] = NULL
;
763 bfd_set_error (bfd_error_bad_value
);
766 n_elt
= shdr
->sh_size
/ 4;
768 /* Look through this group's sections to see if current
769 section is a member. */
771 if ((++idx
)->shdr
== hdr
)
775 /* We are a member of this group. Go looking through
776 other members to see if any others are linked via
778 idx
= (Elf_Internal_Group
*) shdr
->contents
;
779 n_elt
= shdr
->sh_size
/ 4;
781 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
782 && elf_next_in_group (s
) != NULL
)
786 /* Snarf the group name from other member, and
787 insert current section in circular list. */
788 elf_group_name (newsect
) = elf_group_name (s
);
789 elf_next_in_group (newsect
) = elf_next_in_group (s
);
790 elf_next_in_group (s
) = newsect
;
796 gname
= group_signature (abfd
, shdr
);
799 elf_group_name (newsect
) = gname
;
801 /* Start a circular list with one element. */
802 elf_next_in_group (newsect
) = newsect
;
805 /* If the group section has been created, point to the
807 if (shdr
->bfd_section
!= NULL
)
808 elf_next_in_group (shdr
->bfd_section
) = newsect
;
810 elf_tdata (abfd
)->group_search_offset
= i
;
817 if (elf_group_name (newsect
) == NULL
)
819 /* xgettext:c-format */
820 _bfd_error_handler (_("%B: no group info for section '%A'"),
828 _bfd_elf_setup_sections (bfd
*abfd
)
831 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
832 bfd_boolean result
= TRUE
;
835 /* Process SHF_LINK_ORDER. */
836 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
838 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
839 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
841 unsigned int elfsec
= this_hdr
->sh_link
;
842 /* FIXME: The old Intel compiler and old strip/objcopy may
843 not set the sh_link or sh_info fields. Hence we could
844 get the situation where elfsec is 0. */
847 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
848 if (bed
->link_order_error_handler
)
849 bed
->link_order_error_handler
850 /* xgettext:c-format */
851 (_("%B: warning: sh_link not set for section `%A'"),
856 asection
*linksec
= NULL
;
858 if (elfsec
< elf_numsections (abfd
))
860 this_hdr
= elf_elfsections (abfd
)[elfsec
];
861 linksec
= this_hdr
->bfd_section
;
865 Some strip/objcopy may leave an incorrect value in
866 sh_link. We don't want to proceed. */
870 /* xgettext:c-format */
871 (_("%B: sh_link [%d] in section `%A' is incorrect"),
872 s
->owner
, elfsec
, s
);
876 elf_linked_to_section (s
) = linksec
;
879 else if (this_hdr
->sh_type
== SHT_GROUP
880 && elf_next_in_group (s
) == NULL
)
883 /* xgettext:c-format */
884 (_("%B: SHT_GROUP section [index %d] has no SHF_GROUP sections"),
885 abfd
, elf_section_data (s
)->this_idx
);
890 /* Process section groups. */
891 if (num_group
== (unsigned) -1)
894 for (i
= 0; i
< num_group
; i
++)
896 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
897 Elf_Internal_Group
*idx
;
900 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */
901 if (shdr
== NULL
|| shdr
->bfd_section
== NULL
|| shdr
->contents
== NULL
)
904 /* xgettext:c-format */
905 (_("%B: section group entry number %u is corrupt"),
911 idx
= (Elf_Internal_Group
*) shdr
->contents
;
912 n_elt
= shdr
->sh_size
/ 4;
918 if (idx
->shdr
== NULL
)
920 else if (idx
->shdr
->bfd_section
)
921 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
922 else if (idx
->shdr
->sh_type
!= SHT_RELA
923 && idx
->shdr
->sh_type
!= SHT_REL
)
925 /* There are some unknown sections in the group. */
927 /* xgettext:c-format */
928 (_("%B: unknown type [%#x] section `%s' in group [%A]"),
931 bfd_elf_string_from_elf_section (abfd
,
932 (elf_elfheader (abfd
)
945 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
947 return elf_next_in_group (sec
) != NULL
;
951 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
953 unsigned int len
= strlen (name
);
954 char *new_name
= bfd_alloc (abfd
, len
+ 2);
955 if (new_name
== NULL
)
959 memcpy (new_name
+ 2, name
+ 1, len
);
964 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
966 unsigned int len
= strlen (name
);
967 char *new_name
= bfd_alloc (abfd
, len
);
968 if (new_name
== NULL
)
971 memcpy (new_name
+ 1, name
+ 2, len
- 1);
975 /* Make a BFD section from an ELF section. We store a pointer to the
976 BFD section in the bfd_section field of the header. */
979 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
980 Elf_Internal_Shdr
*hdr
,
986 const struct elf_backend_data
*bed
;
988 if (hdr
->bfd_section
!= NULL
)
991 newsect
= bfd_make_section_anyway (abfd
, name
);
995 hdr
->bfd_section
= newsect
;
996 elf_section_data (newsect
)->this_hdr
= *hdr
;
997 elf_section_data (newsect
)->this_idx
= shindex
;
999 /* Always use the real type/flags. */
1000 elf_section_type (newsect
) = hdr
->sh_type
;
1001 elf_section_flags (newsect
) = hdr
->sh_flags
;
1003 newsect
->filepos
= hdr
->sh_offset
;
1005 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
1006 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
1007 || ! bfd_set_section_alignment (abfd
, newsect
,
1008 bfd_log2 (hdr
->sh_addralign
)))
1011 flags
= SEC_NO_FLAGS
;
1012 if (hdr
->sh_type
!= SHT_NOBITS
)
1013 flags
|= SEC_HAS_CONTENTS
;
1014 if (hdr
->sh_type
== SHT_GROUP
)
1016 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1019 if (hdr
->sh_type
!= SHT_NOBITS
)
1022 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
1023 flags
|= SEC_READONLY
;
1024 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
1026 else if ((flags
& SEC_LOAD
) != 0)
1028 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
1031 newsect
->entsize
= hdr
->sh_entsize
;
1033 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
1034 flags
|= SEC_STRINGS
;
1035 if (hdr
->sh_flags
& SHF_GROUP
)
1036 if (!setup_group (abfd
, hdr
, newsect
))
1038 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
1039 flags
|= SEC_THREAD_LOCAL
;
1040 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
1041 flags
|= SEC_EXCLUDE
;
1043 if ((flags
& SEC_ALLOC
) == 0)
1045 /* The debugging sections appear to be recognized only by name,
1046 not any sort of flag. Their SEC_ALLOC bits are cleared. */
1047 if (name
[0] == '.')
1052 p
= ".debug", n
= 6;
1053 else if (name
[1] == 'g' && name
[2] == 'n')
1054 p
= ".gnu.linkonce.wi.", n
= 17;
1055 else if (name
[1] == 'g' && name
[2] == 'd')
1056 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
1057 else if (name
[1] == 'l')
1059 else if (name
[1] == 's')
1061 else if (name
[1] == 'z')
1062 p
= ".zdebug", n
= 7;
1065 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
1066 flags
|= SEC_DEBUGGING
;
1070 /* As a GNU extension, if the name begins with .gnu.linkonce, we
1071 only link a single copy of the section. This is used to support
1072 g++. g++ will emit each template expansion in its own section.
1073 The symbols will be defined as weak, so that multiple definitions
1074 are permitted. The GNU linker extension is to actually discard
1075 all but one of the sections. */
1076 if (CONST_STRNEQ (name
, ".gnu.linkonce")
1077 && elf_next_in_group (newsect
) == NULL
)
1078 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1080 bed
= get_elf_backend_data (abfd
);
1081 if (bed
->elf_backend_section_flags
)
1082 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
1085 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
1088 /* We do not parse the PT_NOTE segments as we are interested even in the
1089 separate debug info files which may have the segments offsets corrupted.
1090 PT_NOTEs from the core files are currently not parsed using BFD. */
1091 if (hdr
->sh_type
== SHT_NOTE
)
1095 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1098 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
,
1099 hdr
->sh_offset
, hdr
->sh_addralign
);
1103 if ((flags
& SEC_ALLOC
) != 0)
1105 Elf_Internal_Phdr
*phdr
;
1106 unsigned int i
, nload
;
1108 /* Some ELF linkers produce binaries with all the program header
1109 p_paddr fields zero. If we have such a binary with more than
1110 one PT_LOAD header, then leave the section lma equal to vma
1111 so that we don't create sections with overlapping lma. */
1112 phdr
= elf_tdata (abfd
)->phdr
;
1113 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1114 if (phdr
->p_paddr
!= 0)
1116 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1118 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1121 phdr
= elf_tdata (abfd
)->phdr
;
1122 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1124 if (((phdr
->p_type
== PT_LOAD
1125 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1126 || phdr
->p_type
== PT_TLS
)
1127 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1129 if ((flags
& SEC_LOAD
) == 0)
1130 newsect
->lma
= (phdr
->p_paddr
1131 + hdr
->sh_addr
- phdr
->p_vaddr
);
1133 /* We used to use the same adjustment for SEC_LOAD
1134 sections, but that doesn't work if the segment
1135 is packed with code from multiple VMAs.
1136 Instead we calculate the section LMA based on
1137 the segment LMA. It is assumed that the
1138 segment will contain sections with contiguous
1139 LMAs, even if the VMAs are not. */
1140 newsect
->lma
= (phdr
->p_paddr
1141 + hdr
->sh_offset
- phdr
->p_offset
);
1143 /* With contiguous segments, we can't tell from file
1144 offsets whether a section with zero size should
1145 be placed at the end of one segment or the
1146 beginning of the next. Decide based on vaddr. */
1147 if (hdr
->sh_addr
>= phdr
->p_vaddr
1148 && (hdr
->sh_addr
+ hdr
->sh_size
1149 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1155 /* Compress/decompress DWARF debug sections with names: .debug_* and
1156 .zdebug_*, after the section flags is set. */
1157 if ((flags
& SEC_DEBUGGING
)
1158 && ((name
[1] == 'd' && name
[6] == '_')
1159 || (name
[1] == 'z' && name
[7] == '_')))
1161 enum { nothing
, compress
, decompress
} action
= nothing
;
1162 int compression_header_size
;
1163 bfd_size_type uncompressed_size
;
1164 bfd_boolean compressed
1165 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1166 &compression_header_size
,
1167 &uncompressed_size
);
1171 /* Compressed section. Check if we should decompress. */
1172 if ((abfd
->flags
& BFD_DECOMPRESS
))
1173 action
= decompress
;
1176 /* Compress the uncompressed section or convert from/to .zdebug*
1177 section. Check if we should compress. */
1178 if (action
== nothing
)
1180 if (newsect
->size
!= 0
1181 && (abfd
->flags
& BFD_COMPRESS
)
1182 && compression_header_size
>= 0
1183 && uncompressed_size
> 0
1185 || ((compression_header_size
> 0)
1186 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1192 if (action
== compress
)
1194 if (!bfd_init_section_compress_status (abfd
, newsect
))
1197 /* xgettext:c-format */
1198 (_("%B: unable to initialize compress status for section %s"),
1205 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1208 /* xgettext:c-format */
1209 (_("%B: unable to initialize decompress status for section %s"),
1215 if (abfd
->is_linker_input
)
1218 && (action
== decompress
1219 || (action
== compress
1220 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1222 /* Convert section name from .zdebug_* to .debug_* so
1223 that linker will consider this section as a debug
1225 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1226 if (new_name
== NULL
)
1228 bfd_rename_section (abfd
, newsect
, new_name
);
1232 /* For objdump, don't rename the section. For objcopy, delay
1233 section rename to elf_fake_sections. */
1234 newsect
->flags
|= SEC_ELF_RENAME
;
1240 const char *const bfd_elf_section_type_names
[] =
1242 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1243 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1244 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1247 /* ELF relocs are against symbols. If we are producing relocatable
1248 output, and the reloc is against an external symbol, and nothing
1249 has given us any additional addend, the resulting reloc will also
1250 be against the same symbol. In such a case, we don't want to
1251 change anything about the way the reloc is handled, since it will
1252 all be done at final link time. Rather than put special case code
1253 into bfd_perform_relocation, all the reloc types use this howto
1254 function. It just short circuits the reloc if producing
1255 relocatable output against an external symbol. */
1257 bfd_reloc_status_type
1258 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1259 arelent
*reloc_entry
,
1261 void *data ATTRIBUTE_UNUSED
,
1262 asection
*input_section
,
1264 char **error_message ATTRIBUTE_UNUSED
)
1266 if (output_bfd
!= NULL
1267 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1268 && (! reloc_entry
->howto
->partial_inplace
1269 || reloc_entry
->addend
== 0))
1271 reloc_entry
->address
+= input_section
->output_offset
;
1272 return bfd_reloc_ok
;
1275 return bfd_reloc_continue
;
1278 /* Returns TRUE if section A matches section B.
1279 Names, addresses and links may be different, but everything else
1280 should be the same. */
1283 section_match (const Elf_Internal_Shdr
* a
,
1284 const Elf_Internal_Shdr
* b
)
1287 a
->sh_type
== b
->sh_type
1288 && (a
->sh_flags
& ~ SHF_INFO_LINK
)
1289 == (b
->sh_flags
& ~ SHF_INFO_LINK
)
1290 && a
->sh_addralign
== b
->sh_addralign
1291 && a
->sh_size
== b
->sh_size
1292 && a
->sh_entsize
== b
->sh_entsize
1293 /* FIXME: Check sh_addr ? */
1297 /* Find a section in OBFD that has the same characteristics
1298 as IHEADER. Return the index of this section or SHN_UNDEF if
1299 none can be found. Check's section HINT first, as this is likely
1300 to be the correct section. */
1303 find_link (const bfd
*obfd
, const Elf_Internal_Shdr
*iheader
,
1304 const unsigned int hint
)
1306 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1309 BFD_ASSERT (iheader
!= NULL
);
1311 /* See PR 20922 for a reproducer of the NULL test. */
1312 if (hint
< elf_numsections (obfd
)
1313 && oheaders
[hint
] != NULL
1314 && section_match (oheaders
[hint
], iheader
))
1317 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1319 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1321 if (oheader
== NULL
)
1323 if (section_match (oheader
, iheader
))
1324 /* FIXME: Do we care if there is a potential for
1325 multiple matches ? */
1332 /* PR 19938: Attempt to set the ELF section header fields of an OS or
1333 Processor specific section, based upon a matching input section.
1334 Returns TRUE upon success, FALSE otherwise. */
1337 copy_special_section_fields (const bfd
*ibfd
,
1339 const Elf_Internal_Shdr
*iheader
,
1340 Elf_Internal_Shdr
*oheader
,
1341 const unsigned int secnum
)
1343 const struct elf_backend_data
*bed
= get_elf_backend_data (obfd
);
1344 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1345 bfd_boolean changed
= FALSE
;
1346 unsigned int sh_link
;
1348 if (oheader
->sh_type
== SHT_NOBITS
)
1350 /* This is a feature for objcopy --only-keep-debug:
1351 When a section's type is changed to NOBITS, we preserve
1352 the sh_link and sh_info fields so that they can be
1353 matched up with the original.
1355 Note: Strictly speaking these assignments are wrong.
1356 The sh_link and sh_info fields should point to the
1357 relevent sections in the output BFD, which may not be in
1358 the same location as they were in the input BFD. But
1359 the whole point of this action is to preserve the
1360 original values of the sh_link and sh_info fields, so
1361 that they can be matched up with the section headers in
1362 the original file. So strictly speaking we may be
1363 creating an invalid ELF file, but it is only for a file
1364 that just contains debug info and only for sections
1365 without any contents. */
1366 if (oheader
->sh_link
== 0)
1367 oheader
->sh_link
= iheader
->sh_link
;
1368 if (oheader
->sh_info
== 0)
1369 oheader
->sh_info
= iheader
->sh_info
;
1373 /* Allow the target a chance to decide how these fields should be set. */
1374 if (bed
->elf_backend_copy_special_section_fields
!= NULL
1375 && bed
->elf_backend_copy_special_section_fields
1376 (ibfd
, obfd
, iheader
, oheader
))
1379 /* We have an iheader which might match oheader, and which has non-zero
1380 sh_info and/or sh_link fields. Attempt to follow those links and find
1381 the section in the output bfd which corresponds to the linked section
1382 in the input bfd. */
1383 if (iheader
->sh_link
!= SHN_UNDEF
)
1385 /* See PR 20931 for a reproducer. */
1386 if (iheader
->sh_link
>= elf_numsections (ibfd
))
1389 /* xgettext:c-format */
1390 (_("%B: Invalid sh_link field (%d) in section number %d"),
1391 ibfd
, iheader
->sh_link
, secnum
);
1395 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_link
], iheader
->sh_link
);
1396 if (sh_link
!= SHN_UNDEF
)
1398 oheader
->sh_link
= sh_link
;
1402 /* FIXME: Should we install iheader->sh_link
1403 if we could not find a match ? */
1405 /* xgettext:c-format */
1406 (_("%B: Failed to find link section for section %d"), obfd
, secnum
);
1409 if (iheader
->sh_info
)
1411 /* The sh_info field can hold arbitrary information, but if the
1412 SHF_LINK_INFO flag is set then it should be interpreted as a
1414 if (iheader
->sh_flags
& SHF_INFO_LINK
)
1416 sh_link
= find_link (obfd
, iheaders
[iheader
->sh_info
],
1418 if (sh_link
!= SHN_UNDEF
)
1419 oheader
->sh_flags
|= SHF_INFO_LINK
;
1422 /* No idea what it means - just copy it. */
1423 sh_link
= iheader
->sh_info
;
1425 if (sh_link
!= SHN_UNDEF
)
1427 oheader
->sh_info
= sh_link
;
1432 /* xgettext:c-format */
1433 (_("%B: Failed to find info section for section %d"), obfd
, secnum
);
1439 /* Copy the program header and other data from one object module to
1443 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1445 const Elf_Internal_Shdr
**iheaders
= (const Elf_Internal_Shdr
**) elf_elfsections (ibfd
);
1446 Elf_Internal_Shdr
**oheaders
= elf_elfsections (obfd
);
1447 const struct elf_backend_data
*bed
;
1450 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1451 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1454 if (!elf_flags_init (obfd
))
1456 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1457 elf_flags_init (obfd
) = TRUE
;
1460 elf_gp (obfd
) = elf_gp (ibfd
);
1462 /* Also copy the EI_OSABI field. */
1463 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1464 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1466 /* If set, copy the EI_ABIVERSION field. */
1467 if (elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
])
1468 elf_elfheader (obfd
)->e_ident
[EI_ABIVERSION
]
1469 = elf_elfheader (ibfd
)->e_ident
[EI_ABIVERSION
];
1471 /* Copy object attributes. */
1472 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1474 if (iheaders
== NULL
|| oheaders
== NULL
)
1477 bed
= get_elf_backend_data (obfd
);
1479 /* Possibly copy other fields in the section header. */
1480 for (i
= 1; i
< elf_numsections (obfd
); i
++)
1483 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1485 /* Ignore ordinary sections. SHT_NOBITS sections are considered however
1486 because of a special case need for generating separate debug info
1487 files. See below for more details. */
1489 || (oheader
->sh_type
!= SHT_NOBITS
1490 && oheader
->sh_type
< SHT_LOOS
))
1493 /* Ignore empty sections, and sections whose
1494 fields have already been initialised. */
1495 if (oheader
->sh_size
== 0
1496 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1499 /* Scan for the matching section in the input bfd.
1500 First we try for a direct mapping between the input and output sections. */
1501 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1503 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1505 if (iheader
== NULL
)
1508 if (oheader
->bfd_section
!= NULL
1509 && iheader
->bfd_section
!= NULL
1510 && iheader
->bfd_section
->output_section
!= NULL
1511 && iheader
->bfd_section
->output_section
== oheader
->bfd_section
)
1513 /* We have found a connection from the input section to the
1514 output section. Attempt to copy the header fields. If
1515 this fails then do not try any further sections - there
1516 should only be a one-to-one mapping between input and output. */
1517 if (! copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1518 j
= elf_numsections (ibfd
);
1523 if (j
< elf_numsections (ibfd
))
1526 /* That failed. So try to deduce the corresponding input section.
1527 Unfortunately we cannot compare names as the output string table
1528 is empty, so instead we check size, address and type. */
1529 for (j
= 1; j
< elf_numsections (ibfd
); j
++)
1531 const Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1533 if (iheader
== NULL
)
1536 /* Try matching fields in the input section's header.
1537 Since --only-keep-debug turns all non-debug sections into
1538 SHT_NOBITS sections, the output SHT_NOBITS type matches any
1540 if ((oheader
->sh_type
== SHT_NOBITS
1541 || iheader
->sh_type
== oheader
->sh_type
)
1542 && (iheader
->sh_flags
& ~ SHF_INFO_LINK
)
1543 == (oheader
->sh_flags
& ~ SHF_INFO_LINK
)
1544 && iheader
->sh_addralign
== oheader
->sh_addralign
1545 && iheader
->sh_entsize
== oheader
->sh_entsize
1546 && iheader
->sh_size
== oheader
->sh_size
1547 && iheader
->sh_addr
== oheader
->sh_addr
1548 && (iheader
->sh_info
!= oheader
->sh_info
1549 || iheader
->sh_link
!= oheader
->sh_link
))
1551 if (copy_special_section_fields (ibfd
, obfd
, iheader
, oheader
, i
))
1556 if (j
== elf_numsections (ibfd
) && oheader
->sh_type
>= SHT_LOOS
)
1558 /* Final attempt. Call the backend copy function
1559 with a NULL input section. */
1560 if (bed
->elf_backend_copy_special_section_fields
!= NULL
)
1561 bed
->elf_backend_copy_special_section_fields (ibfd
, obfd
, NULL
, oheader
);
1569 get_segment_type (unsigned int p_type
)
1574 case PT_NULL
: pt
= "NULL"; break;
1575 case PT_LOAD
: pt
= "LOAD"; break;
1576 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1577 case PT_INTERP
: pt
= "INTERP"; break;
1578 case PT_NOTE
: pt
= "NOTE"; break;
1579 case PT_SHLIB
: pt
= "SHLIB"; break;
1580 case PT_PHDR
: pt
= "PHDR"; break;
1581 case PT_TLS
: pt
= "TLS"; break;
1582 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1583 case PT_GNU_STACK
: pt
= "STACK"; break;
1584 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1585 default: pt
= NULL
; break;
1590 /* Print out the program headers. */
1593 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1595 FILE *f
= (FILE *) farg
;
1596 Elf_Internal_Phdr
*p
;
1598 bfd_byte
*dynbuf
= NULL
;
1600 p
= elf_tdata (abfd
)->phdr
;
1605 fprintf (f
, _("\nProgram Header:\n"));
1606 c
= elf_elfheader (abfd
)->e_phnum
;
1607 for (i
= 0; i
< c
; i
++, p
++)
1609 const char *pt
= get_segment_type (p
->p_type
);
1614 sprintf (buf
, "0x%lx", p
->p_type
);
1617 fprintf (f
, "%8s off 0x", pt
);
1618 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1619 fprintf (f
, " vaddr 0x");
1620 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1621 fprintf (f
, " paddr 0x");
1622 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1623 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1624 fprintf (f
, " filesz 0x");
1625 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1626 fprintf (f
, " memsz 0x");
1627 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1628 fprintf (f
, " flags %c%c%c",
1629 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1630 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1631 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1632 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1633 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1638 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1641 unsigned int elfsec
;
1642 unsigned long shlink
;
1643 bfd_byte
*extdyn
, *extdynend
;
1645 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1647 fprintf (f
, _("\nDynamic Section:\n"));
1649 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1652 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1653 if (elfsec
== SHN_BAD
)
1655 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1657 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1658 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1661 /* PR 17512: file: 6f427532. */
1662 if (s
->size
< extdynsize
)
1664 extdynend
= extdyn
+ s
->size
;
1665 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1667 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1669 Elf_Internal_Dyn dyn
;
1670 const char *name
= "";
1672 bfd_boolean stringp
;
1673 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1675 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1677 if (dyn
.d_tag
== DT_NULL
)
1684 if (bed
->elf_backend_get_target_dtag
)
1685 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1687 if (!strcmp (name
, ""))
1689 sprintf (ab
, "%#" BFD_VMA_FMT
"x", dyn
.d_tag
);
1694 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1695 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1696 case DT_PLTGOT
: name
= "PLTGOT"; break;
1697 case DT_HASH
: name
= "HASH"; break;
1698 case DT_STRTAB
: name
= "STRTAB"; break;
1699 case DT_SYMTAB
: name
= "SYMTAB"; break;
1700 case DT_RELA
: name
= "RELA"; break;
1701 case DT_RELASZ
: name
= "RELASZ"; break;
1702 case DT_RELAENT
: name
= "RELAENT"; break;
1703 case DT_STRSZ
: name
= "STRSZ"; break;
1704 case DT_SYMENT
: name
= "SYMENT"; break;
1705 case DT_INIT
: name
= "INIT"; break;
1706 case DT_FINI
: name
= "FINI"; break;
1707 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1708 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1709 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1710 case DT_REL
: name
= "REL"; break;
1711 case DT_RELSZ
: name
= "RELSZ"; break;
1712 case DT_RELENT
: name
= "RELENT"; break;
1713 case DT_PLTREL
: name
= "PLTREL"; break;
1714 case DT_DEBUG
: name
= "DEBUG"; break;
1715 case DT_TEXTREL
: name
= "TEXTREL"; break;
1716 case DT_JMPREL
: name
= "JMPREL"; break;
1717 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1718 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1719 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1720 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1721 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1722 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1723 case DT_FLAGS
: name
= "FLAGS"; break;
1724 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1725 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1726 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1727 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1728 case DT_MOVEENT
: name
= "MOVEENT"; break;
1729 case DT_MOVESZ
: name
= "MOVESZ"; break;
1730 case DT_FEATURE
: name
= "FEATURE"; break;
1731 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1732 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1733 case DT_SYMINENT
: name
= "SYMINENT"; break;
1734 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1735 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1736 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1737 case DT_PLTPAD
: name
= "PLTPAD"; break;
1738 case DT_MOVETAB
: name
= "MOVETAB"; break;
1739 case DT_SYMINFO
: name
= "SYMINFO"; break;
1740 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1741 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1742 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1743 case DT_VERSYM
: name
= "VERSYM"; break;
1744 case DT_VERDEF
: name
= "VERDEF"; break;
1745 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1746 case DT_VERNEED
: name
= "VERNEED"; break;
1747 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1748 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1749 case DT_USED
: name
= "USED"; break;
1750 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1751 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1754 fprintf (f
, " %-20s ", name
);
1758 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1763 unsigned int tagv
= dyn
.d_un
.d_val
;
1765 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1768 fprintf (f
, "%s", string
);
1777 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1778 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1780 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1784 if (elf_dynverdef (abfd
) != 0)
1786 Elf_Internal_Verdef
*t
;
1788 fprintf (f
, _("\nVersion definitions:\n"));
1789 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1791 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1792 t
->vd_flags
, t
->vd_hash
,
1793 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1794 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1796 Elf_Internal_Verdaux
*a
;
1799 for (a
= t
->vd_auxptr
->vda_nextptr
;
1803 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1809 if (elf_dynverref (abfd
) != 0)
1811 Elf_Internal_Verneed
*t
;
1813 fprintf (f
, _("\nVersion References:\n"));
1814 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1816 Elf_Internal_Vernaux
*a
;
1818 fprintf (f
, _(" required from %s:\n"),
1819 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1820 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1821 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1822 a
->vna_flags
, a
->vna_other
,
1823 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1835 /* Get version string. */
1838 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1839 bfd_boolean
*hidden
)
1841 const char *version_string
= NULL
;
1842 if (elf_dynversym (abfd
) != 0
1843 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1845 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1847 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1848 vernum
&= VERSYM_VERSION
;
1851 version_string
= "";
1852 else if (vernum
== 1)
1853 version_string
= "Base";
1854 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1856 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1859 Elf_Internal_Verneed
*t
;
1861 version_string
= "";
1862 for (t
= elf_tdata (abfd
)->verref
;
1866 Elf_Internal_Vernaux
*a
;
1868 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1870 if (a
->vna_other
== vernum
)
1872 version_string
= a
->vna_nodename
;
1879 return version_string
;
1882 /* Display ELF-specific fields of a symbol. */
1885 bfd_elf_print_symbol (bfd
*abfd
,
1888 bfd_print_symbol_type how
)
1890 FILE *file
= (FILE *) filep
;
1893 case bfd_print_symbol_name
:
1894 fprintf (file
, "%s", symbol
->name
);
1896 case bfd_print_symbol_more
:
1897 fprintf (file
, "elf ");
1898 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1899 fprintf (file
, " %x", symbol
->flags
);
1901 case bfd_print_symbol_all
:
1903 const char *section_name
;
1904 const char *name
= NULL
;
1905 const struct elf_backend_data
*bed
;
1906 unsigned char st_other
;
1908 const char *version_string
;
1911 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1913 bed
= get_elf_backend_data (abfd
);
1914 if (bed
->elf_backend_print_symbol_all
)
1915 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1919 name
= symbol
->name
;
1920 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1923 fprintf (file
, " %s\t", section_name
);
1924 /* Print the "other" value for a symbol. For common symbols,
1925 we've already printed the size; now print the alignment.
1926 For other symbols, we have no specified alignment, and
1927 we've printed the address; now print the size. */
1928 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1929 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1931 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1932 bfd_fprintf_vma (abfd
, file
, val
);
1934 /* If we have version information, print it. */
1935 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1941 fprintf (file
, " %-11s", version_string
);
1946 fprintf (file
, " (%s)", version_string
);
1947 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1952 /* If the st_other field is not zero, print it. */
1953 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1958 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1959 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1960 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1962 /* Some other non-defined flags are also present, so print
1964 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1967 fprintf (file
, " %s", name
);
1973 /* ELF .o/exec file reading */
1975 /* Create a new bfd section from an ELF section header. */
1978 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1980 Elf_Internal_Shdr
*hdr
;
1981 Elf_Internal_Ehdr
*ehdr
;
1982 const struct elf_backend_data
*bed
;
1984 bfd_boolean ret
= TRUE
;
1985 static bfd_boolean
* sections_being_created
= NULL
;
1986 static bfd
* sections_being_created_abfd
= NULL
;
1987 static unsigned int nesting
= 0;
1989 if (shindex
>= elf_numsections (abfd
))
1994 /* PR17512: A corrupt ELF binary might contain a recursive group of
1995 sections, with each the string indicies pointing to the next in the
1996 loop. Detect this here, by refusing to load a section that we are
1997 already in the process of loading. We only trigger this test if
1998 we have nested at least three sections deep as normal ELF binaries
1999 can expect to recurse at least once.
2001 FIXME: It would be better if this array was attached to the bfd,
2002 rather than being held in a static pointer. */
2004 if (sections_being_created_abfd
!= abfd
)
2005 sections_being_created
= NULL
;
2006 if (sections_being_created
== NULL
)
2008 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
2009 sections_being_created
= (bfd_boolean
*)
2010 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
2011 sections_being_created_abfd
= abfd
;
2013 if (sections_being_created
[shindex
])
2016 (_("%B: warning: loop in section dependencies detected"), abfd
);
2019 sections_being_created
[shindex
] = TRUE
;
2022 hdr
= elf_elfsections (abfd
)[shindex
];
2023 ehdr
= elf_elfheader (abfd
);
2024 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
2029 bed
= get_elf_backend_data (abfd
);
2030 switch (hdr
->sh_type
)
2033 /* Inactive section. Throw it away. */
2036 case SHT_PROGBITS
: /* Normal section with contents. */
2037 case SHT_NOBITS
: /* .bss section. */
2038 case SHT_HASH
: /* .hash section. */
2039 case SHT_NOTE
: /* .note section. */
2040 case SHT_INIT_ARRAY
: /* .init_array section. */
2041 case SHT_FINI_ARRAY
: /* .fini_array section. */
2042 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
2043 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
2044 case SHT_GNU_HASH
: /* .gnu.hash section. */
2045 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2048 case SHT_DYNAMIC
: /* Dynamic linking information. */
2049 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2052 if (hdr
->sh_link
> elf_numsections (abfd
))
2054 /* PR 10478: Accept Solaris binaries with a sh_link
2055 field set to SHN_BEFORE or SHN_AFTER. */
2056 switch (bfd_get_arch (abfd
))
2059 case bfd_arch_sparc
:
2060 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
2061 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
2063 /* Otherwise fall through. */
2068 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
2070 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
2072 Elf_Internal_Shdr
*dynsymhdr
;
2074 /* The shared libraries distributed with hpux11 have a bogus
2075 sh_link field for the ".dynamic" section. Find the
2076 string table for the ".dynsym" section instead. */
2077 if (elf_dynsymtab (abfd
) != 0)
2079 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
2080 hdr
->sh_link
= dynsymhdr
->sh_link
;
2084 unsigned int i
, num_sec
;
2086 num_sec
= elf_numsections (abfd
);
2087 for (i
= 1; i
< num_sec
; i
++)
2089 dynsymhdr
= elf_elfsections (abfd
)[i
];
2090 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
2092 hdr
->sh_link
= dynsymhdr
->sh_link
;
2100 case SHT_SYMTAB
: /* A symbol table. */
2101 if (elf_onesymtab (abfd
) == shindex
)
2104 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2107 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2109 if (hdr
->sh_size
!= 0)
2111 /* Some assemblers erroneously set sh_info to one with a
2112 zero sh_size. ld sees this as a global symbol count
2113 of (unsigned) -1. Fix it here. */
2118 /* PR 18854: A binary might contain more than one symbol table.
2119 Unusual, but possible. Warn, but continue. */
2120 if (elf_onesymtab (abfd
) != 0)
2123 /* xgettext:c-format */
2124 (_("%B: warning: multiple symbol tables detected"
2125 " - ignoring the table in section %u"),
2129 elf_onesymtab (abfd
) = shindex
;
2130 elf_symtab_hdr (abfd
) = *hdr
;
2131 elf_elfsections (abfd
)[shindex
] = hdr
= & elf_symtab_hdr (abfd
);
2132 abfd
->flags
|= HAS_SYMS
;
2134 /* Sometimes a shared object will map in the symbol table. If
2135 SHF_ALLOC is set, and this is a shared object, then we also
2136 treat this section as a BFD section. We can not base the
2137 decision purely on SHF_ALLOC, because that flag is sometimes
2138 set in a relocatable object file, which would confuse the
2140 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
2141 && (abfd
->flags
& DYNAMIC
) != 0
2142 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2146 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2147 can't read symbols without that section loaded as well. It
2148 is most likely specified by the next section header. */
2150 elf_section_list
* entry
;
2151 unsigned int i
, num_sec
;
2153 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2154 if (entry
->hdr
.sh_link
== shindex
)
2157 num_sec
= elf_numsections (abfd
);
2158 for (i
= shindex
+ 1; i
< num_sec
; i
++)
2160 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2162 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2163 && hdr2
->sh_link
== shindex
)
2168 for (i
= 1; i
< shindex
; i
++)
2170 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2172 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
2173 && hdr2
->sh_link
== shindex
)
2178 ret
= bfd_section_from_shdr (abfd
, i
);
2179 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */
2183 case SHT_DYNSYM
: /* A dynamic symbol table. */
2184 if (elf_dynsymtab (abfd
) == shindex
)
2187 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
2190 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
2192 if (hdr
->sh_size
!= 0)
2195 /* Some linkers erroneously set sh_info to one with a
2196 zero sh_size. ld sees this as a global symbol count
2197 of (unsigned) -1. Fix it here. */
2202 /* PR 18854: A binary might contain more than one dynamic symbol table.
2203 Unusual, but possible. Warn, but continue. */
2204 if (elf_dynsymtab (abfd
) != 0)
2207 /* xgettext:c-format */
2208 (_("%B: warning: multiple dynamic symbol tables detected"
2209 " - ignoring the table in section %u"),
2213 elf_dynsymtab (abfd
) = shindex
;
2214 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
2215 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
2216 abfd
->flags
|= HAS_SYMS
;
2218 /* Besides being a symbol table, we also treat this as a regular
2219 section, so that objcopy can handle it. */
2220 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2223 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
2225 elf_section_list
* entry
;
2227 for (entry
= elf_symtab_shndx_list (abfd
); entry
!= NULL
; entry
= entry
->next
)
2228 if (entry
->ndx
== shindex
)
2231 entry
= bfd_alloc (abfd
, sizeof * entry
);
2234 entry
->ndx
= shindex
;
2236 entry
->next
= elf_symtab_shndx_list (abfd
);
2237 elf_symtab_shndx_list (abfd
) = entry
;
2238 elf_elfsections (abfd
)[shindex
] = & entry
->hdr
;
2242 case SHT_STRTAB
: /* A string table. */
2243 if (hdr
->bfd_section
!= NULL
)
2246 if (ehdr
->e_shstrndx
== shindex
)
2248 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
2249 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
2253 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
2256 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
2257 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
2261 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
2264 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
2265 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
2266 elf_elfsections (abfd
)[shindex
] = hdr
;
2267 /* We also treat this as a regular section, so that objcopy
2269 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2274 /* If the string table isn't one of the above, then treat it as a
2275 regular section. We need to scan all the headers to be sure,
2276 just in case this strtab section appeared before the above. */
2277 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
2279 unsigned int i
, num_sec
;
2281 num_sec
= elf_numsections (abfd
);
2282 for (i
= 1; i
< num_sec
; i
++)
2284 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
2285 if (hdr2
->sh_link
== shindex
)
2287 /* Prevent endless recursion on broken objects. */
2290 if (! bfd_section_from_shdr (abfd
, i
))
2292 if (elf_onesymtab (abfd
) == i
)
2294 if (elf_dynsymtab (abfd
) == i
)
2295 goto dynsymtab_strtab
;
2299 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2304 /* *These* do a lot of work -- but build no sections! */
2306 asection
*target_sect
;
2307 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
2308 unsigned int num_sec
= elf_numsections (abfd
);
2309 struct bfd_elf_section_data
*esdt
;
2312 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
2313 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
2316 /* Check for a bogus link to avoid crashing. */
2317 if (hdr
->sh_link
>= num_sec
)
2320 /* xgettext:c-format */
2321 (_("%B: invalid link %u for reloc section %s (index %u)"),
2322 abfd
, hdr
->sh_link
, name
, shindex
);
2323 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2328 /* For some incomprehensible reason Oracle distributes
2329 libraries for Solaris in which some of the objects have
2330 bogus sh_link fields. It would be nice if we could just
2331 reject them, but, unfortunately, some people need to use
2332 them. We scan through the section headers; if we find only
2333 one suitable symbol table, we clobber the sh_link to point
2334 to it. I hope this doesn't break anything.
2336 Don't do it on executable nor shared library. */
2337 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
2338 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
2339 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2345 for (scan
= 1; scan
< num_sec
; scan
++)
2347 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2348 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2359 hdr
->sh_link
= found
;
2362 /* Get the symbol table. */
2363 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2364 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2365 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2368 /* If this reloc section does not use the main symbol table we
2369 don't treat it as a reloc section. BFD can't adequately
2370 represent such a section, so at least for now, we don't
2371 try. We just present it as a normal section. We also
2372 can't use it as a reloc section if it points to the null
2373 section, an invalid section, another reloc section, or its
2374 sh_link points to the null section. */
2375 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2376 || hdr
->sh_link
== SHN_UNDEF
2377 || hdr
->sh_info
== SHN_UNDEF
2378 || hdr
->sh_info
>= num_sec
2379 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2380 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2382 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2387 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2390 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2391 if (target_sect
== NULL
)
2394 esdt
= elf_section_data (target_sect
);
2395 if (hdr
->sh_type
== SHT_RELA
)
2396 p_hdr
= &esdt
->rela
.hdr
;
2398 p_hdr
= &esdt
->rel
.hdr
;
2400 /* PR 17512: file: 0b4f81b7. */
2403 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
2408 elf_elfsections (abfd
)[shindex
] = hdr2
;
2409 target_sect
->reloc_count
+= (NUM_SHDR_ENTRIES (hdr
)
2410 * bed
->s
->int_rels_per_ext_rel
);
2411 target_sect
->flags
|= SEC_RELOC
;
2412 target_sect
->relocation
= NULL
;
2413 target_sect
->rel_filepos
= hdr
->sh_offset
;
2414 /* In the section to which the relocations apply, mark whether
2415 its relocations are of the REL or RELA variety. */
2416 if (hdr
->sh_size
!= 0)
2418 if (hdr
->sh_type
== SHT_RELA
)
2419 target_sect
->use_rela_p
= 1;
2421 abfd
->flags
|= HAS_RELOC
;
2425 case SHT_GNU_verdef
:
2426 elf_dynverdef (abfd
) = shindex
;
2427 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2428 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2431 case SHT_GNU_versym
:
2432 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2435 elf_dynversym (abfd
) = shindex
;
2436 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2437 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2440 case SHT_GNU_verneed
:
2441 elf_dynverref (abfd
) = shindex
;
2442 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2443 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2450 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2453 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2459 /* Possibly an attributes section. */
2460 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2461 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2463 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2465 _bfd_elf_parse_attributes (abfd
, hdr
);
2469 /* Check for any processor-specific section types. */
2470 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2473 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2475 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2476 /* FIXME: How to properly handle allocated section reserved
2477 for applications? */
2479 /* xgettext:c-format */
2480 (_("%B: unknown type [%#x] section `%s'"),
2481 abfd
, hdr
->sh_type
, name
);
2484 /* Allow sections reserved for applications. */
2485 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2490 else if (hdr
->sh_type
>= SHT_LOPROC
2491 && hdr
->sh_type
<= SHT_HIPROC
)
2492 /* FIXME: We should handle this section. */
2494 /* xgettext:c-format */
2495 (_("%B: unknown type [%#x] section `%s'"),
2496 abfd
, hdr
->sh_type
, name
);
2497 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2499 /* Unrecognised OS-specific sections. */
2500 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2501 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2502 required to correctly process the section and the file should
2503 be rejected with an error message. */
2505 /* xgettext:c-format */
2506 (_("%B: unknown type [%#x] section `%s'"),
2507 abfd
, hdr
->sh_type
, name
);
2510 /* Otherwise it should be processed. */
2511 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2516 /* FIXME: We should handle this section. */
2518 /* xgettext:c-format */
2519 (_("%B: unknown type [%#x] section `%s'"),
2520 abfd
, hdr
->sh_type
, name
);
2528 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2529 sections_being_created
[shindex
] = FALSE
;
2530 if (-- nesting
== 0)
2532 sections_being_created
= NULL
;
2533 sections_being_created_abfd
= abfd
;
2538 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2541 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2543 unsigned long r_symndx
)
2545 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2547 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2549 Elf_Internal_Shdr
*symtab_hdr
;
2550 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2551 Elf_External_Sym_Shndx eshndx
;
2553 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2554 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2555 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2558 if (cache
->abfd
!= abfd
)
2560 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2563 cache
->indx
[ent
] = r_symndx
;
2566 return &cache
->sym
[ent
];
2569 /* Given an ELF section number, retrieve the corresponding BFD
2573 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2575 if (sec_index
>= elf_numsections (abfd
))
2577 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2580 static const struct bfd_elf_special_section special_sections_b
[] =
2582 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2583 { NULL
, 0, 0, 0, 0 }
2586 static const struct bfd_elf_special_section special_sections_c
[] =
2588 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2589 { NULL
, 0, 0, 0, 0 }
2592 static const struct bfd_elf_special_section special_sections_d
[] =
2594 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2595 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2596 /* There are more DWARF sections than these, but they needn't be added here
2597 unless you have to cope with broken compilers that don't emit section
2598 attributes or you want to help the user writing assembler. */
2599 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2600 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2601 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2602 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2603 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2604 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2605 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2606 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2607 { NULL
, 0, 0, 0, 0 }
2610 static const struct bfd_elf_special_section special_sections_f
[] =
2612 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2613 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2614 { NULL
, 0 , 0, 0, 0 }
2617 static const struct bfd_elf_special_section special_sections_g
[] =
2619 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2620 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2621 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2622 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2623 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2624 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2625 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2626 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2627 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2628 { NULL
, 0, 0, 0, 0 }
2631 static const struct bfd_elf_special_section special_sections_h
[] =
2633 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2634 { NULL
, 0, 0, 0, 0 }
2637 static const struct bfd_elf_special_section special_sections_i
[] =
2639 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2640 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2641 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2642 { NULL
, 0, 0, 0, 0 }
2645 static const struct bfd_elf_special_section special_sections_l
[] =
2647 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2648 { NULL
, 0, 0, 0, 0 }
2651 static const struct bfd_elf_special_section special_sections_n
[] =
2653 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2654 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2655 { NULL
, 0, 0, 0, 0 }
2658 static const struct bfd_elf_special_section special_sections_p
[] =
2660 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2661 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2662 { NULL
, 0, 0, 0, 0 }
2665 static const struct bfd_elf_special_section special_sections_r
[] =
2667 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2668 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2669 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2670 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2671 { NULL
, 0, 0, 0, 0 }
2674 static const struct bfd_elf_special_section special_sections_s
[] =
2676 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2677 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2678 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2679 /* See struct bfd_elf_special_section declaration for the semantics of
2680 this special case where .prefix_length != strlen (.prefix). */
2681 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2682 { NULL
, 0, 0, 0, 0 }
2685 static const struct bfd_elf_special_section special_sections_t
[] =
2687 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2688 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2689 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2690 { NULL
, 0, 0, 0, 0 }
2693 static const struct bfd_elf_special_section special_sections_z
[] =
2695 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2696 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2697 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2698 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2699 { NULL
, 0, 0, 0, 0 }
2702 static const struct bfd_elf_special_section
* const special_sections
[] =
2704 special_sections_b
, /* 'b' */
2705 special_sections_c
, /* 'c' */
2706 special_sections_d
, /* 'd' */
2708 special_sections_f
, /* 'f' */
2709 special_sections_g
, /* 'g' */
2710 special_sections_h
, /* 'h' */
2711 special_sections_i
, /* 'i' */
2714 special_sections_l
, /* 'l' */
2716 special_sections_n
, /* 'n' */
2718 special_sections_p
, /* 'p' */
2720 special_sections_r
, /* 'r' */
2721 special_sections_s
, /* 's' */
2722 special_sections_t
, /* 't' */
2728 special_sections_z
/* 'z' */
2731 const struct bfd_elf_special_section
*
2732 _bfd_elf_get_special_section (const char *name
,
2733 const struct bfd_elf_special_section
*spec
,
2739 len
= strlen (name
);
2741 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2744 int prefix_len
= spec
[i
].prefix_length
;
2746 if (len
< prefix_len
)
2748 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2751 suffix_len
= spec
[i
].suffix_length
;
2752 if (suffix_len
<= 0)
2754 if (name
[prefix_len
] != 0)
2756 if (suffix_len
== 0)
2758 if (name
[prefix_len
] != '.'
2759 && (suffix_len
== -2
2760 || (rela
&& spec
[i
].type
== SHT_REL
)))
2766 if (len
< prefix_len
+ suffix_len
)
2768 if (memcmp (name
+ len
- suffix_len
,
2769 spec
[i
].prefix
+ prefix_len
,
2779 const struct bfd_elf_special_section
*
2780 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2783 const struct bfd_elf_special_section
*spec
;
2784 const struct elf_backend_data
*bed
;
2786 /* See if this is one of the special sections. */
2787 if (sec
->name
== NULL
)
2790 bed
= get_elf_backend_data (abfd
);
2791 spec
= bed
->special_sections
;
2794 spec
= _bfd_elf_get_special_section (sec
->name
,
2795 bed
->special_sections
,
2801 if (sec
->name
[0] != '.')
2804 i
= sec
->name
[1] - 'b';
2805 if (i
< 0 || i
> 'z' - 'b')
2808 spec
= special_sections
[i
];
2813 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2817 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2819 struct bfd_elf_section_data
*sdata
;
2820 const struct elf_backend_data
*bed
;
2821 const struct bfd_elf_special_section
*ssect
;
2823 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2826 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2830 sec
->used_by_bfd
= sdata
;
2833 /* Indicate whether or not this section should use RELA relocations. */
2834 bed
= get_elf_backend_data (abfd
);
2835 sec
->use_rela_p
= bed
->default_use_rela_p
;
2837 /* When we read a file, we don't need to set ELF section type and
2838 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2839 anyway. We will set ELF section type and flags for all linker
2840 created sections. If user specifies BFD section flags, we will
2841 set ELF section type and flags based on BFD section flags in
2842 elf_fake_sections. Special handling for .init_array/.fini_array
2843 output sections since they may contain .ctors/.dtors input
2844 sections. We don't want _bfd_elf_init_private_section_data to
2845 copy ELF section type from .ctors/.dtors input sections. */
2846 if (abfd
->direction
!= read_direction
2847 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2849 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2852 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2853 || ssect
->type
== SHT_INIT_ARRAY
2854 || ssect
->type
== SHT_FINI_ARRAY
))
2856 elf_section_type (sec
) = ssect
->type
;
2857 elf_section_flags (sec
) = ssect
->attr
;
2861 return _bfd_generic_new_section_hook (abfd
, sec
);
2864 /* Create a new bfd section from an ELF program header.
2866 Since program segments have no names, we generate a synthetic name
2867 of the form segment<NUM>, where NUM is generally the index in the
2868 program header table. For segments that are split (see below) we
2869 generate the names segment<NUM>a and segment<NUM>b.
2871 Note that some program segments may have a file size that is different than
2872 (less than) the memory size. All this means is that at execution the
2873 system must allocate the amount of memory specified by the memory size,
2874 but only initialize it with the first "file size" bytes read from the
2875 file. This would occur for example, with program segments consisting
2876 of combined data+bss.
2878 To handle the above situation, this routine generates TWO bfd sections
2879 for the single program segment. The first has the length specified by
2880 the file size of the segment, and the second has the length specified
2881 by the difference between the two sizes. In effect, the segment is split
2882 into its initialized and uninitialized parts.
2887 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2888 Elf_Internal_Phdr
*hdr
,
2890 const char *type_name
)
2898 split
= ((hdr
->p_memsz
> 0)
2899 && (hdr
->p_filesz
> 0)
2900 && (hdr
->p_memsz
> hdr
->p_filesz
));
2902 if (hdr
->p_filesz
> 0)
2904 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2905 len
= strlen (namebuf
) + 1;
2906 name
= (char *) bfd_alloc (abfd
, len
);
2909 memcpy (name
, namebuf
, len
);
2910 newsect
= bfd_make_section (abfd
, name
);
2911 if (newsect
== NULL
)
2913 newsect
->vma
= hdr
->p_vaddr
;
2914 newsect
->lma
= hdr
->p_paddr
;
2915 newsect
->size
= hdr
->p_filesz
;
2916 newsect
->filepos
= hdr
->p_offset
;
2917 newsect
->flags
|= SEC_HAS_CONTENTS
;
2918 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2919 if (hdr
->p_type
== PT_LOAD
)
2921 newsect
->flags
|= SEC_ALLOC
;
2922 newsect
->flags
|= SEC_LOAD
;
2923 if (hdr
->p_flags
& PF_X
)
2925 /* FIXME: all we known is that it has execute PERMISSION,
2927 newsect
->flags
|= SEC_CODE
;
2930 if (!(hdr
->p_flags
& PF_W
))
2932 newsect
->flags
|= SEC_READONLY
;
2936 if (hdr
->p_memsz
> hdr
->p_filesz
)
2940 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2941 len
= strlen (namebuf
) + 1;
2942 name
= (char *) bfd_alloc (abfd
, len
);
2945 memcpy (name
, namebuf
, len
);
2946 newsect
= bfd_make_section (abfd
, name
);
2947 if (newsect
== NULL
)
2949 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2950 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2951 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2952 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2953 align
= newsect
->vma
& -newsect
->vma
;
2954 if (align
== 0 || align
> hdr
->p_align
)
2955 align
= hdr
->p_align
;
2956 newsect
->alignment_power
= bfd_log2 (align
);
2957 if (hdr
->p_type
== PT_LOAD
)
2959 /* Hack for gdb. Segments that have not been modified do
2960 not have their contents written to a core file, on the
2961 assumption that a debugger can find the contents in the
2962 executable. We flag this case by setting the fake
2963 section size to zero. Note that "real" bss sections will
2964 always have their contents dumped to the core file. */
2965 if (bfd_get_format (abfd
) == bfd_core
)
2967 newsect
->flags
|= SEC_ALLOC
;
2968 if (hdr
->p_flags
& PF_X
)
2969 newsect
->flags
|= SEC_CODE
;
2971 if (!(hdr
->p_flags
& PF_W
))
2972 newsect
->flags
|= SEC_READONLY
;
2979 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2981 const struct elf_backend_data
*bed
;
2983 switch (hdr
->p_type
)
2986 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2989 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2992 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2995 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2998 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
3000 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
,
3006 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
3009 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
3011 case PT_GNU_EH_FRAME
:
3012 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
3016 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
3019 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
3022 /* Check for any processor-specific program segment types. */
3023 bed
= get_elf_backend_data (abfd
);
3024 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
3028 /* Return the REL_HDR for SEC, assuming there is only a single one, either
3032 _bfd_elf_single_rel_hdr (asection
*sec
)
3034 if (elf_section_data (sec
)->rel
.hdr
)
3036 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
3037 return elf_section_data (sec
)->rel
.hdr
;
3040 return elf_section_data (sec
)->rela
.hdr
;
3044 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
3045 Elf_Internal_Shdr
*rel_hdr
,
3046 const char *sec_name
,
3047 bfd_boolean use_rela_p
)
3049 char *name
= (char *) bfd_alloc (abfd
,
3050 sizeof ".rela" + strlen (sec_name
));
3054 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
3056 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
3058 if (rel_hdr
->sh_name
== (unsigned int) -1)
3064 /* Allocate and initialize a section-header for a new reloc section,
3065 containing relocations against ASECT. It is stored in RELDATA. If
3066 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3070 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
3071 struct bfd_elf_section_reloc_data
*reldata
,
3072 const char *sec_name
,
3073 bfd_boolean use_rela_p
,
3074 bfd_boolean delay_st_name_p
)
3076 Elf_Internal_Shdr
*rel_hdr
;
3077 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3079 BFD_ASSERT (reldata
->hdr
== NULL
);
3080 rel_hdr
= bfd_zalloc (abfd
, sizeof (*rel_hdr
));
3081 reldata
->hdr
= rel_hdr
;
3083 if (delay_st_name_p
)
3084 rel_hdr
->sh_name
= (unsigned int) -1;
3085 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
3088 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
3089 rel_hdr
->sh_entsize
= (use_rela_p
3090 ? bed
->s
->sizeof_rela
3091 : bed
->s
->sizeof_rel
);
3092 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
3093 rel_hdr
->sh_flags
= 0;
3094 rel_hdr
->sh_addr
= 0;
3095 rel_hdr
->sh_size
= 0;
3096 rel_hdr
->sh_offset
= 0;
3101 /* Return the default section type based on the passed in section flags. */
3104 bfd_elf_get_default_section_type (flagword flags
)
3106 if ((flags
& SEC_ALLOC
) != 0
3107 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
3109 return SHT_PROGBITS
;
3112 struct fake_section_arg
3114 struct bfd_link_info
*link_info
;
3118 /* Set up an ELF internal section header for a section. */
3121 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
3123 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
3124 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3125 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
3126 Elf_Internal_Shdr
*this_hdr
;
3127 unsigned int sh_type
;
3128 const char *name
= asect
->name
;
3129 bfd_boolean delay_st_name_p
= FALSE
;
3133 /* We already failed; just get out of the bfd_map_over_sections
3138 this_hdr
= &esd
->this_hdr
;
3142 /* ld: compress DWARF debug sections with names: .debug_*. */
3143 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
3144 && (asect
->flags
& SEC_DEBUGGING
)
3148 /* Set SEC_ELF_COMPRESS to indicate this section should be
3150 asect
->flags
|= SEC_ELF_COMPRESS
;
3152 /* If this section will be compressed, delay adding section
3153 name to section name section after it is compressed in
3154 _bfd_elf_assign_file_positions_for_non_load. */
3155 delay_st_name_p
= TRUE
;
3158 else if ((asect
->flags
& SEC_ELF_RENAME
))
3160 /* objcopy: rename output DWARF debug section. */
3161 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
3163 /* When we decompress or compress with SHF_COMPRESSED,
3164 convert section name from .zdebug_* to .debug_* if
3168 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
3169 if (new_name
== NULL
)
3177 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
3179 /* PR binutils/18087: Compression does not always make a
3180 section smaller. So only rename the section when
3181 compression has actually taken place. If input section
3182 name is .zdebug_*, we should never compress it again. */
3183 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
3184 if (new_name
== NULL
)
3189 BFD_ASSERT (name
[1] != 'z');
3194 if (delay_st_name_p
)
3195 this_hdr
->sh_name
= (unsigned int) -1;
3199 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3201 if (this_hdr
->sh_name
== (unsigned int) -1)
3208 /* Don't clear sh_flags. Assembler may set additional bits. */
3210 if ((asect
->flags
& SEC_ALLOC
) != 0
3211 || asect
->user_set_vma
)
3212 this_hdr
->sh_addr
= asect
->vma
;
3214 this_hdr
->sh_addr
= 0;
3216 this_hdr
->sh_offset
= 0;
3217 this_hdr
->sh_size
= asect
->size
;
3218 this_hdr
->sh_link
= 0;
3219 /* PR 17512: file: 0eb809fe, 8b0535ee. */
3220 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
3223 /* xgettext:c-format */
3224 (_("%B: error: Alignment power %d of section `%A' is too big"),
3225 abfd
, asect
->alignment_power
, asect
);
3229 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
3230 /* The sh_entsize and sh_info fields may have been set already by
3231 copy_private_section_data. */
3233 this_hdr
->bfd_section
= asect
;
3234 this_hdr
->contents
= NULL
;
3236 /* If the section type is unspecified, we set it based on
3238 if ((asect
->flags
& SEC_GROUP
) != 0)
3239 sh_type
= SHT_GROUP
;
3241 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
3243 if (this_hdr
->sh_type
== SHT_NULL
)
3244 this_hdr
->sh_type
= sh_type
;
3245 else if (this_hdr
->sh_type
== SHT_NOBITS
3246 && sh_type
== SHT_PROGBITS
3247 && (asect
->flags
& SEC_ALLOC
) != 0)
3249 /* Warn if we are changing a NOBITS section to PROGBITS, but
3250 allow the link to proceed. This can happen when users link
3251 non-bss input sections to bss output sections, or emit data
3252 to a bss output section via a linker script. */
3254 (_("warning: section `%A' type changed to PROGBITS"), asect
);
3255 this_hdr
->sh_type
= sh_type
;
3258 switch (this_hdr
->sh_type
)
3269 case SHT_INIT_ARRAY
:
3270 case SHT_FINI_ARRAY
:
3271 case SHT_PREINIT_ARRAY
:
3272 this_hdr
->sh_entsize
= bed
->s
->arch_size
/ 8;
3276 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
3280 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
3284 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
3288 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
3289 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
3293 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
3294 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
3297 case SHT_GNU_versym
:
3298 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
3301 case SHT_GNU_verdef
:
3302 this_hdr
->sh_entsize
= 0;
3303 /* objcopy or strip will copy over sh_info, but may not set
3304 cverdefs. The linker will set cverdefs, but sh_info will be
3306 if (this_hdr
->sh_info
== 0)
3307 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
3309 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
3310 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
3313 case SHT_GNU_verneed
:
3314 this_hdr
->sh_entsize
= 0;
3315 /* objcopy or strip will copy over sh_info, but may not set
3316 cverrefs. The linker will set cverrefs, but sh_info will be
3318 if (this_hdr
->sh_info
== 0)
3319 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3321 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3322 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3326 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3330 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3334 if ((asect
->flags
& SEC_ALLOC
) != 0)
3335 this_hdr
->sh_flags
|= SHF_ALLOC
;
3336 if ((asect
->flags
& SEC_READONLY
) == 0)
3337 this_hdr
->sh_flags
|= SHF_WRITE
;
3338 if ((asect
->flags
& SEC_CODE
) != 0)
3339 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3340 if ((asect
->flags
& SEC_MERGE
) != 0)
3342 this_hdr
->sh_flags
|= SHF_MERGE
;
3343 this_hdr
->sh_entsize
= asect
->entsize
;
3345 if ((asect
->flags
& SEC_STRINGS
) != 0)
3346 this_hdr
->sh_flags
|= SHF_STRINGS
;
3347 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3348 this_hdr
->sh_flags
|= SHF_GROUP
;
3349 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3351 this_hdr
->sh_flags
|= SHF_TLS
;
3352 if (asect
->size
== 0
3353 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3355 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3357 this_hdr
->sh_size
= 0;
3360 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3361 if (this_hdr
->sh_size
!= 0)
3362 this_hdr
->sh_type
= SHT_NOBITS
;
3366 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3367 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3369 /* If the section has relocs, set up a section header for the
3370 SHT_REL[A] section. If two relocation sections are required for
3371 this section, it is up to the processor-specific back-end to
3372 create the other. */
3373 if ((asect
->flags
& SEC_RELOC
) != 0)
3375 /* When doing a relocatable link, create both REL and RELA sections if
3378 /* Do the normal setup if we wouldn't create any sections here. */
3379 && esd
->rel
.count
+ esd
->rela
.count
> 0
3380 && (bfd_link_relocatable (arg
->link_info
)
3381 || arg
->link_info
->emitrelocations
))
3383 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3384 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
,
3385 FALSE
, delay_st_name_p
))
3390 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3391 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
,
3392 TRUE
, delay_st_name_p
))
3398 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3400 ? &esd
->rela
: &esd
->rel
),
3410 /* Check for processor-specific section types. */
3411 sh_type
= this_hdr
->sh_type
;
3412 if (bed
->elf_backend_fake_sections
3413 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3419 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3421 /* Don't change the header type from NOBITS if we are being
3422 called for objcopy --only-keep-debug. */
3423 this_hdr
->sh_type
= sh_type
;
3427 /* Fill in the contents of a SHT_GROUP section. Called from
3428 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3429 when ELF targets use the generic linker, ld. Called for ld -r
3430 from bfd_elf_final_link. */
3433 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3435 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3436 asection
*elt
, *first
;
3440 /* Ignore linker created group section. See elfNN_ia64_object_p in
3442 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3446 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3448 unsigned long symindx
= 0;
3450 /* elf_group_id will have been set up by objcopy and the
3452 if (elf_group_id (sec
) != NULL
)
3453 symindx
= elf_group_id (sec
)->udata
.i
;
3457 /* If called from the assembler, swap_out_syms will have set up
3458 elf_section_syms. */
3459 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3460 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3462 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3464 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3466 /* The ELF backend linker sets sh_info to -2 when the group
3467 signature symbol is global, and thus the index can't be
3468 set until all local symbols are output. */
3470 struct bfd_elf_section_data
*sec_data
;
3471 unsigned long symndx
;
3472 unsigned long extsymoff
;
3473 struct elf_link_hash_entry
*h
;
3475 /* The point of this little dance to the first SHF_GROUP section
3476 then back to the SHT_GROUP section is that this gets us to
3477 the SHT_GROUP in the input object. */
3478 igroup
= elf_sec_group (elf_next_in_group (sec
));
3479 sec_data
= elf_section_data (igroup
);
3480 symndx
= sec_data
->this_hdr
.sh_info
;
3482 if (!elf_bad_symtab (igroup
->owner
))
3484 Elf_Internal_Shdr
*symtab_hdr
;
3486 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3487 extsymoff
= symtab_hdr
->sh_info
;
3489 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3490 while (h
->root
.type
== bfd_link_hash_indirect
3491 || h
->root
.type
== bfd_link_hash_warning
)
3492 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3494 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3497 /* The contents won't be allocated for "ld -r" or objcopy. */
3499 if (sec
->contents
== NULL
)
3502 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3504 /* Arrange for the section to be written out. */
3505 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3506 if (sec
->contents
== NULL
)
3513 loc
= sec
->contents
+ sec
->size
;
3515 /* Get the pointer to the first section in the group that gas
3516 squirreled away here. objcopy arranges for this to be set to the
3517 start of the input section group. */
3518 first
= elt
= elf_next_in_group (sec
);
3520 /* First element is a flag word. Rest of section is elf section
3521 indices for all the sections of the group. Write them backwards
3522 just to keep the group in the same order as given in .section
3523 directives, not that it matters. */
3530 s
= s
->output_section
;
3532 && !bfd_is_abs_section (s
))
3534 struct bfd_elf_section_data
*elf_sec
= elf_section_data (s
);
3535 struct bfd_elf_section_data
*input_elf_sec
= elf_section_data (elt
);
3537 if (elf_sec
->rel
.hdr
!= NULL
3539 || (input_elf_sec
->rel
.hdr
!= NULL
3540 && input_elf_sec
->rel
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3542 elf_sec
->rel
.hdr
->sh_flags
|= SHF_GROUP
;
3544 H_PUT_32 (abfd
, elf_sec
->rel
.idx
, loc
);
3546 if (elf_sec
->rela
.hdr
!= NULL
3548 || (input_elf_sec
->rela
.hdr
!= NULL
3549 && input_elf_sec
->rela
.hdr
->sh_flags
& SHF_GROUP
) != 0))
3551 elf_sec
->rela
.hdr
->sh_flags
|= SHF_GROUP
;
3553 H_PUT_32 (abfd
, elf_sec
->rela
.idx
, loc
);
3556 H_PUT_32 (abfd
, elf_sec
->this_idx
, loc
);
3558 elt
= elf_next_in_group (elt
);
3564 BFD_ASSERT (loc
== sec
->contents
);
3566 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3569 /* Given NAME, the name of a relocation section stripped of its
3570 .rel/.rela prefix, return the section in ABFD to which the
3571 relocations apply. */
3574 _bfd_elf_plt_get_reloc_section (bfd
*abfd
, const char *name
)
3576 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3577 section likely apply to .got.plt or .got section. */
3578 if (get_elf_backend_data (abfd
)->want_got_plt
3579 && strcmp (name
, ".plt") == 0)
3584 sec
= bfd_get_section_by_name (abfd
, name
);
3590 return bfd_get_section_by_name (abfd
, name
);
3593 /* Return the section to which RELOC_SEC applies. */
3596 elf_get_reloc_section (asection
*reloc_sec
)
3601 const struct elf_backend_data
*bed
;
3603 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3604 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3607 /* We look up the section the relocs apply to by name. */
3608 name
= reloc_sec
->name
;
3609 if (strncmp (name
, ".rel", 4) != 0)
3612 if (type
== SHT_RELA
&& *name
++ != 'a')
3615 abfd
= reloc_sec
->owner
;
3616 bed
= get_elf_backend_data (abfd
);
3617 return bed
->get_reloc_section (abfd
, name
);
3620 /* Assign all ELF section numbers. The dummy first section is handled here
3621 too. The link/info pointers for the standard section types are filled
3622 in here too, while we're at it. */
3625 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3627 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3629 unsigned int section_number
;
3630 Elf_Internal_Shdr
**i_shdrp
;
3631 struct bfd_elf_section_data
*d
;
3632 bfd_boolean need_symtab
;
3636 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3638 /* SHT_GROUP sections are in relocatable files only. */
3639 if (link_info
== NULL
|| !link_info
->resolve_section_groups
)
3641 size_t reloc_count
= 0;
3643 /* Put SHT_GROUP sections first. */
3644 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3646 d
= elf_section_data (sec
);
3648 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3650 if (sec
->flags
& SEC_LINKER_CREATED
)
3652 /* Remove the linker created SHT_GROUP sections. */
3653 bfd_section_list_remove (abfd
, sec
);
3654 abfd
->section_count
--;
3657 d
->this_idx
= section_number
++;
3660 /* Count relocations. */
3661 reloc_count
+= sec
->reloc_count
;
3664 /* Clear HAS_RELOC if there are no relocations. */
3665 if (reloc_count
== 0)
3666 abfd
->flags
&= ~HAS_RELOC
;
3669 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3671 d
= elf_section_data (sec
);
3673 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3674 d
->this_idx
= section_number
++;
3675 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3676 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3679 d
->rel
.idx
= section_number
++;
3680 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3681 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3688 d
->rela
.idx
= section_number
++;
3689 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3690 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3696 need_symtab
= (bfd_get_symcount (abfd
) > 0
3697 || (link_info
== NULL
3698 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3702 elf_onesymtab (abfd
) = section_number
++;
3703 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3704 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3706 elf_section_list
* entry
;
3708 BFD_ASSERT (elf_symtab_shndx_list (abfd
) == NULL
);
3710 entry
= bfd_zalloc (abfd
, sizeof * entry
);
3711 entry
->ndx
= section_number
++;
3712 elf_symtab_shndx_list (abfd
) = entry
;
3714 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3715 ".symtab_shndx", FALSE
);
3716 if (entry
->hdr
.sh_name
== (unsigned int) -1)
3719 elf_strtab_sec (abfd
) = section_number
++;
3720 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3723 elf_shstrtab_sec (abfd
) = section_number
++;
3724 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3725 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3727 if (section_number
>= SHN_LORESERVE
)
3729 /* xgettext:c-format */
3730 _bfd_error_handler (_("%B: too many sections: %u"),
3731 abfd
, section_number
);
3735 elf_numsections (abfd
) = section_number
;
3736 elf_elfheader (abfd
)->e_shnum
= section_number
;
3738 /* Set up the list of section header pointers, in agreement with the
3740 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3741 sizeof (Elf_Internal_Shdr
*));
3742 if (i_shdrp
== NULL
)
3745 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3746 sizeof (Elf_Internal_Shdr
));
3747 if (i_shdrp
[0] == NULL
)
3749 bfd_release (abfd
, i_shdrp
);
3753 elf_elfsections (abfd
) = i_shdrp
;
3755 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3758 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3759 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3761 elf_section_list
* entry
= elf_symtab_shndx_list (abfd
);
3762 BFD_ASSERT (entry
!= NULL
);
3763 i_shdrp
[entry
->ndx
] = & entry
->hdr
;
3764 entry
->hdr
.sh_link
= elf_onesymtab (abfd
);
3766 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3767 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3770 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3774 d
= elf_section_data (sec
);
3776 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3777 if (d
->rel
.idx
!= 0)
3778 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3779 if (d
->rela
.idx
!= 0)
3780 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3782 /* Fill in the sh_link and sh_info fields while we're at it. */
3784 /* sh_link of a reloc section is the section index of the symbol
3785 table. sh_info is the section index of the section to which
3786 the relocation entries apply. */
3787 if (d
->rel
.idx
!= 0)
3789 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3790 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3791 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3793 if (d
->rela
.idx
!= 0)
3795 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3796 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3797 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3800 /* We need to set up sh_link for SHF_LINK_ORDER. */
3801 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3803 s
= elf_linked_to_section (sec
);
3806 /* elf_linked_to_section points to the input section. */
3807 if (link_info
!= NULL
)
3809 /* Check discarded linkonce section. */
3810 if (discarded_section (s
))
3814 /* xgettext:c-format */
3815 (_("%B: sh_link of section `%A' points to"
3816 " discarded section `%A' of `%B'"),
3817 abfd
, d
->this_hdr
.bfd_section
,
3819 /* Point to the kept section if it has the same
3820 size as the discarded one. */
3821 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3824 bfd_set_error (bfd_error_bad_value
);
3830 s
= s
->output_section
;
3831 BFD_ASSERT (s
!= NULL
);
3835 /* Handle objcopy. */
3836 if (s
->output_section
== NULL
)
3839 /* xgettext:c-format */
3840 (_("%B: sh_link of section `%A' points to"
3841 " removed section `%A' of `%B'"),
3842 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3843 bfd_set_error (bfd_error_bad_value
);
3846 s
= s
->output_section
;
3848 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3853 The Intel C compiler generates SHT_IA_64_UNWIND with
3854 SHF_LINK_ORDER. But it doesn't set the sh_link or
3855 sh_info fields. Hence we could get the situation
3857 const struct elf_backend_data
*bed
3858 = get_elf_backend_data (abfd
);
3859 if (bed
->link_order_error_handler
)
3860 bed
->link_order_error_handler
3861 /* xgettext:c-format */
3862 (_("%B: warning: sh_link not set for section `%A'"),
3867 switch (d
->this_hdr
.sh_type
)
3871 /* A reloc section which we are treating as a normal BFD
3872 section. sh_link is the section index of the symbol
3873 table. sh_info is the section index of the section to
3874 which the relocation entries apply. We assume that an
3875 allocated reloc section uses the dynamic symbol table.
3876 FIXME: How can we be sure? */
3877 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3879 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3881 s
= elf_get_reloc_section (sec
);
3884 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3885 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3890 /* We assume that a section named .stab*str is a stabs
3891 string section. We look for a section with the same name
3892 but without the trailing ``str'', and set its sh_link
3893 field to point to this section. */
3894 if (CONST_STRNEQ (sec
->name
, ".stab")
3895 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3900 len
= strlen (sec
->name
);
3901 alc
= (char *) bfd_malloc (len
- 2);
3904 memcpy (alc
, sec
->name
, len
- 3);
3905 alc
[len
- 3] = '\0';
3906 s
= bfd_get_section_by_name (abfd
, alc
);
3910 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3912 /* This is a .stab section. */
3913 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3914 elf_section_data (s
)->this_hdr
.sh_entsize
3915 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3922 case SHT_GNU_verneed
:
3923 case SHT_GNU_verdef
:
3924 /* sh_link is the section header index of the string table
3925 used for the dynamic entries, or the symbol table, or the
3927 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3929 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3932 case SHT_GNU_LIBLIST
:
3933 /* sh_link is the section header index of the prelink library
3934 list used for the dynamic entries, or the symbol table, or
3935 the version strings. */
3936 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3937 ? ".dynstr" : ".gnu.libstr");
3939 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3944 case SHT_GNU_versym
:
3945 /* sh_link is the section header index of the symbol table
3946 this hash table or version table is for. */
3947 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3949 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3953 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3957 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3958 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3959 debug section name from .debug_* to .zdebug_* if needed. */
3965 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3967 /* If the backend has a special mapping, use it. */
3968 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3969 if (bed
->elf_backend_sym_is_global
)
3970 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3972 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3973 || bfd_is_und_section (bfd_get_section (sym
))
3974 || bfd_is_com_section (bfd_get_section (sym
)));
3977 /* Filter global symbols of ABFD to include in the import library. All
3978 SYMCOUNT symbols of ABFD can be examined from their pointers in
3979 SYMS. Pointers of symbols to keep should be stored contiguously at
3980 the beginning of that array.
3982 Returns the number of symbols to keep. */
3985 _bfd_elf_filter_global_symbols (bfd
*abfd
, struct bfd_link_info
*info
,
3986 asymbol
**syms
, long symcount
)
3988 long src_count
, dst_count
= 0;
3990 for (src_count
= 0; src_count
< symcount
; src_count
++)
3992 asymbol
*sym
= syms
[src_count
];
3993 char *name
= (char *) bfd_asymbol_name (sym
);
3994 struct bfd_link_hash_entry
*h
;
3996 if (!sym_is_global (abfd
, sym
))
3999 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
4002 if (h
->type
!= bfd_link_hash_defined
&& h
->type
!= bfd_link_hash_defweak
)
4004 if (h
->linker_def
|| h
->ldscript_def
)
4007 syms
[dst_count
++] = sym
;
4010 syms
[dst_count
] = NULL
;
4015 /* Don't output section symbols for sections that are not going to be
4016 output, that are duplicates or there is no BFD section. */
4019 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
4021 elf_symbol_type
*type_ptr
;
4023 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
4026 type_ptr
= elf_symbol_from (abfd
, sym
);
4027 return ((type_ptr
!= NULL
4028 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
4029 && bfd_is_abs_section (sym
->section
))
4030 || !(sym
->section
->owner
== abfd
4031 || (sym
->section
->output_section
->owner
== abfd
4032 && sym
->section
->output_offset
== 0)
4033 || bfd_is_abs_section (sym
->section
)));
4036 /* Map symbol from it's internal number to the external number, moving
4037 all local symbols to be at the head of the list. */
4040 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
4042 unsigned int symcount
= bfd_get_symcount (abfd
);
4043 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4044 asymbol
**sect_syms
;
4045 unsigned int num_locals
= 0;
4046 unsigned int num_globals
= 0;
4047 unsigned int num_locals2
= 0;
4048 unsigned int num_globals2
= 0;
4049 unsigned int max_index
= 0;
4055 fprintf (stderr
, "elf_map_symbols\n");
4059 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4061 if (max_index
< asect
->index
)
4062 max_index
= asect
->index
;
4066 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
4067 if (sect_syms
== NULL
)
4069 elf_section_syms (abfd
) = sect_syms
;
4070 elf_num_section_syms (abfd
) = max_index
;
4072 /* Init sect_syms entries for any section symbols we have already
4073 decided to output. */
4074 for (idx
= 0; idx
< symcount
; idx
++)
4076 asymbol
*sym
= syms
[idx
];
4078 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
4080 && !ignore_section_sym (abfd
, sym
)
4081 && !bfd_is_abs_section (sym
->section
))
4083 asection
*sec
= sym
->section
;
4085 if (sec
->owner
!= abfd
)
4086 sec
= sec
->output_section
;
4088 sect_syms
[sec
->index
] = syms
[idx
];
4092 /* Classify all of the symbols. */
4093 for (idx
= 0; idx
< symcount
; idx
++)
4095 if (sym_is_global (abfd
, syms
[idx
]))
4097 else if (!ignore_section_sym (abfd
, syms
[idx
]))
4101 /* We will be adding a section symbol for each normal BFD section. Most
4102 sections will already have a section symbol in outsymbols, but
4103 eg. SHT_GROUP sections will not, and we need the section symbol mapped
4104 at least in that case. */
4105 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4107 if (sect_syms
[asect
->index
] == NULL
)
4109 if (!sym_is_global (abfd
, asect
->symbol
))
4116 /* Now sort the symbols so the local symbols are first. */
4117 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
4118 sizeof (asymbol
*));
4120 if (new_syms
== NULL
)
4123 for (idx
= 0; idx
< symcount
; idx
++)
4125 asymbol
*sym
= syms
[idx
];
4128 if (sym_is_global (abfd
, sym
))
4129 i
= num_locals
+ num_globals2
++;
4130 else if (!ignore_section_sym (abfd
, sym
))
4135 sym
->udata
.i
= i
+ 1;
4137 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
4139 if (sect_syms
[asect
->index
] == NULL
)
4141 asymbol
*sym
= asect
->symbol
;
4144 sect_syms
[asect
->index
] = sym
;
4145 if (!sym_is_global (abfd
, sym
))
4148 i
= num_locals
+ num_globals2
++;
4150 sym
->udata
.i
= i
+ 1;
4154 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
4156 *pnum_locals
= num_locals
;
4160 /* Align to the maximum file alignment that could be required for any
4161 ELF data structure. */
4163 static inline file_ptr
4164 align_file_position (file_ptr off
, int align
)
4166 return (off
+ align
- 1) & ~(align
- 1);
4169 /* Assign a file position to a section, optionally aligning to the
4170 required section alignment. */
4173 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
4177 if (align
&& i_shdrp
->sh_addralign
> 1)
4178 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
4179 i_shdrp
->sh_offset
= offset
;
4180 if (i_shdrp
->bfd_section
!= NULL
)
4181 i_shdrp
->bfd_section
->filepos
= offset
;
4182 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
4183 offset
+= i_shdrp
->sh_size
;
4187 /* Compute the file positions we are going to put the sections at, and
4188 otherwise prepare to begin writing out the ELF file. If LINK_INFO
4189 is not NULL, this is being called by the ELF backend linker. */
4192 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
4193 struct bfd_link_info
*link_info
)
4195 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4196 struct fake_section_arg fsargs
;
4198 struct elf_strtab_hash
*strtab
= NULL
;
4199 Elf_Internal_Shdr
*shstrtab_hdr
;
4200 bfd_boolean need_symtab
;
4202 if (abfd
->output_has_begun
)
4205 /* Do any elf backend specific processing first. */
4206 if (bed
->elf_backend_begin_write_processing
)
4207 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
4209 if (! prep_headers (abfd
))
4212 /* Post process the headers if necessary. */
4213 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
4215 fsargs
.failed
= FALSE
;
4216 fsargs
.link_info
= link_info
;
4217 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
4221 if (!assign_section_numbers (abfd
, link_info
))
4224 /* The backend linker builds symbol table information itself. */
4225 need_symtab
= (link_info
== NULL
4226 && (bfd_get_symcount (abfd
) > 0
4227 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
4231 /* Non-zero if doing a relocatable link. */
4232 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
4234 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
4239 if (link_info
== NULL
)
4241 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
4246 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
4247 /* sh_name was set in prep_headers. */
4248 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
4249 shstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
4250 shstrtab_hdr
->sh_addr
= 0;
4251 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
4252 shstrtab_hdr
->sh_entsize
= 0;
4253 shstrtab_hdr
->sh_link
= 0;
4254 shstrtab_hdr
->sh_info
= 0;
4255 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
4256 shstrtab_hdr
->sh_addralign
= 1;
4258 if (!assign_file_positions_except_relocs (abfd
, link_info
))
4264 Elf_Internal_Shdr
*hdr
;
4266 off
= elf_next_file_pos (abfd
);
4268 hdr
= & elf_symtab_hdr (abfd
);
4269 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4271 if (elf_symtab_shndx_list (abfd
) != NULL
)
4273 hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
4274 if (hdr
->sh_size
!= 0)
4275 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4276 /* FIXME: What about other symtab_shndx sections in the list ? */
4279 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4280 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4282 elf_next_file_pos (abfd
) = off
;
4284 /* Now that we know where the .strtab section goes, write it
4286 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4287 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
4289 _bfd_elf_strtab_free (strtab
);
4292 abfd
->output_has_begun
= TRUE
;
4297 /* Make an initial estimate of the size of the program header. If we
4298 get the number wrong here, we'll redo section placement. */
4300 static bfd_size_type
4301 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
4305 const struct elf_backend_data
*bed
;
4307 /* Assume we will need exactly two PT_LOAD segments: one for text
4308 and one for data. */
4311 s
= bfd_get_section_by_name (abfd
, ".interp");
4312 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4314 /* If we have a loadable interpreter section, we need a
4315 PT_INTERP segment. In this case, assume we also need a
4316 PT_PHDR segment, although that may not be true for all
4321 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4323 /* We need a PT_DYNAMIC segment. */
4327 if (info
!= NULL
&& info
->relro
)
4329 /* We need a PT_GNU_RELRO segment. */
4333 if (elf_eh_frame_hdr (abfd
))
4335 /* We need a PT_GNU_EH_FRAME segment. */
4339 if (elf_stack_flags (abfd
))
4341 /* We need a PT_GNU_STACK segment. */
4345 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4347 if ((s
->flags
& SEC_LOAD
) != 0
4348 && CONST_STRNEQ (s
->name
, ".note"))
4350 /* We need a PT_NOTE segment. */
4352 /* Try to create just one PT_NOTE segment
4353 for all adjacent loadable .note* sections.
4354 gABI requires that within a PT_NOTE segment
4355 (and also inside of each SHT_NOTE section)
4356 each note is padded to a multiple of 4 size,
4357 so we check whether the sections are correctly
4359 if (s
->alignment_power
== 2)
4360 while (s
->next
!= NULL
4361 && s
->next
->alignment_power
== 2
4362 && (s
->next
->flags
& SEC_LOAD
) != 0
4363 && CONST_STRNEQ (s
->next
->name
, ".note"))
4368 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4370 if (s
->flags
& SEC_THREAD_LOCAL
)
4372 /* We need a PT_TLS segment. */
4378 bed
= get_elf_backend_data (abfd
);
4380 if ((abfd
->flags
& D_PAGED
) != 0)
4382 /* Add a PT_GNU_MBIND segment for each mbind section. */
4383 unsigned int page_align_power
= bfd_log2 (bed
->commonpagesize
);
4384 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4385 if (elf_section_flags (s
) & SHF_GNU_MBIND
)
4387 if (elf_section_data (s
)->this_hdr
.sh_info
4391 /* xgettext:c-format */
4392 (_("%B: GNU_MBIN section `%A' has invalid sh_info field: %d"),
4393 abfd
, s
, elf_section_data (s
)->this_hdr
.sh_info
);
4396 /* Align mbind section to page size. */
4397 if (s
->alignment_power
< page_align_power
)
4398 s
->alignment_power
= page_align_power
;
4403 /* Let the backend count up any program headers it might need. */
4404 if (bed
->elf_backend_additional_program_headers
)
4408 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
4414 return segs
* bed
->s
->sizeof_phdr
;
4417 /* Find the segment that contains the output_section of section. */
4420 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
4422 struct elf_segment_map
*m
;
4423 Elf_Internal_Phdr
*p
;
4425 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
4431 for (i
= m
->count
- 1; i
>= 0; i
--)
4432 if (m
->sections
[i
] == section
)
4439 /* Create a mapping from a set of sections to a program segment. */
4441 static struct elf_segment_map
*
4442 make_mapping (bfd
*abfd
,
4443 asection
**sections
,
4448 struct elf_segment_map
*m
;
4453 amt
= sizeof (struct elf_segment_map
);
4454 amt
+= (to
- from
- 1) * sizeof (asection
*);
4455 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4459 m
->p_type
= PT_LOAD
;
4460 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4461 m
->sections
[i
- from
] = *hdrpp
;
4462 m
->count
= to
- from
;
4464 if (from
== 0 && phdr
)
4466 /* Include the headers in the first PT_LOAD segment. */
4467 m
->includes_filehdr
= 1;
4468 m
->includes_phdrs
= 1;
4474 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4477 struct elf_segment_map
*
4478 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4480 struct elf_segment_map
*m
;
4482 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4483 sizeof (struct elf_segment_map
));
4487 m
->p_type
= PT_DYNAMIC
;
4489 m
->sections
[0] = dynsec
;
4494 /* Possibly add or remove segments from the segment map. */
4497 elf_modify_segment_map (bfd
*abfd
,
4498 struct bfd_link_info
*info
,
4499 bfd_boolean remove_empty_load
)
4501 struct elf_segment_map
**m
;
4502 const struct elf_backend_data
*bed
;
4504 /* The placement algorithm assumes that non allocated sections are
4505 not in PT_LOAD segments. We ensure this here by removing such
4506 sections from the segment map. We also remove excluded
4507 sections. Finally, any PT_LOAD segment without sections is
4509 m
= &elf_seg_map (abfd
);
4512 unsigned int i
, new_count
;
4514 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4516 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4517 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4518 || (*m
)->p_type
!= PT_LOAD
))
4520 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4524 (*m
)->count
= new_count
;
4526 if (remove_empty_load
4527 && (*m
)->p_type
== PT_LOAD
4529 && !(*m
)->includes_phdrs
)
4535 bed
= get_elf_backend_data (abfd
);
4536 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4538 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4545 /* Set up a mapping from BFD sections to program segments. */
4548 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4551 struct elf_segment_map
*m
;
4552 asection
**sections
= NULL
;
4553 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4554 bfd_boolean no_user_phdrs
;
4556 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4559 info
->user_phdrs
= !no_user_phdrs
;
4561 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4565 struct elf_segment_map
*mfirst
;
4566 struct elf_segment_map
**pm
;
4569 unsigned int phdr_index
;
4570 bfd_vma maxpagesize
;
4572 bfd_boolean phdr_in_segment
= TRUE
;
4573 bfd_boolean writable
;
4574 bfd_boolean executable
;
4576 asection
*first_tls
= NULL
;
4577 asection
*first_mbind
= NULL
;
4578 asection
*dynsec
, *eh_frame_hdr
;
4580 bfd_vma addr_mask
, wrap_to
= 0;
4581 bfd_boolean linker_created_pt_phdr_segment
= FALSE
;
4583 /* Select the allocated sections, and sort them. */
4585 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4586 sizeof (asection
*));
4587 if (sections
== NULL
)
4590 /* Calculate top address, avoiding undefined behaviour of shift
4591 left operator when shift count is equal to size of type
4593 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4594 addr_mask
= (addr_mask
<< 1) + 1;
4597 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4599 if ((s
->flags
& SEC_ALLOC
) != 0)
4603 /* A wrapping section potentially clashes with header. */
4604 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4605 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4608 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4611 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4613 /* Build the mapping. */
4618 /* If we have a .interp section, then create a PT_PHDR segment for
4619 the program headers and a PT_INTERP segment for the .interp
4621 s
= bfd_get_section_by_name (abfd
, ".interp");
4622 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4624 amt
= sizeof (struct elf_segment_map
);
4625 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4629 m
->p_type
= PT_PHDR
;
4631 m
->p_flags_valid
= 1;
4632 m
->includes_phdrs
= 1;
4633 linker_created_pt_phdr_segment
= TRUE
;
4637 amt
= sizeof (struct elf_segment_map
);
4638 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4642 m
->p_type
= PT_INTERP
;
4650 /* Look through the sections. We put sections in the same program
4651 segment when the start of the second section can be placed within
4652 a few bytes of the end of the first section. */
4656 maxpagesize
= bed
->maxpagesize
;
4657 /* PR 17512: file: c8455299.
4658 Avoid divide-by-zero errors later on.
4659 FIXME: Should we abort if the maxpagesize is zero ? */
4660 if (maxpagesize
== 0)
4664 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4666 && (dynsec
->flags
& SEC_LOAD
) == 0)
4669 /* Deal with -Ttext or something similar such that the first section
4670 is not adjacent to the program headers. This is an
4671 approximation, since at this point we don't know exactly how many
4672 program headers we will need. */
4675 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4677 if (phdr_size
== (bfd_size_type
) -1)
4678 phdr_size
= get_program_header_size (abfd
, info
);
4679 phdr_size
+= bed
->s
->sizeof_ehdr
;
4680 if ((abfd
->flags
& D_PAGED
) == 0
4681 || (sections
[0]->lma
& addr_mask
) < phdr_size
4682 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4683 < phdr_size
% maxpagesize
)
4684 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4686 /* PR 20815: The ELF standard says that a PT_PHDR segment, if
4687 present, must be included as part of the memory image of the
4688 program. Ie it must be part of a PT_LOAD segment as well.
4689 If we have had to create our own PT_PHDR segment, but it is
4690 not going to be covered by the first PT_LOAD segment, then
4691 force the inclusion if we can... */
4692 if ((abfd
->flags
& D_PAGED
) != 0
4693 && linker_created_pt_phdr_segment
)
4694 phdr_in_segment
= TRUE
;
4696 phdr_in_segment
= FALSE
;
4700 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4703 bfd_boolean new_segment
;
4707 /* See if this section and the last one will fit in the same
4710 if (last_hdr
== NULL
)
4712 /* If we don't have a segment yet, then we don't need a new
4713 one (we build the last one after this loop). */
4714 new_segment
= FALSE
;
4716 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4718 /* If this section has a different relation between the
4719 virtual address and the load address, then we need a new
4723 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4724 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4726 /* If this section has a load address that makes it overlap
4727 the previous section, then we need a new segment. */
4730 /* In the next test we have to be careful when last_hdr->lma is close
4731 to the end of the address space. If the aligned address wraps
4732 around to the start of the address space, then there are no more
4733 pages left in memory and it is OK to assume that the current
4734 section can be included in the current segment. */
4735 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4737 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4740 /* If putting this section in this segment would force us to
4741 skip a page in the segment, then we need a new segment. */
4744 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4745 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4746 && ((abfd
->flags
& D_PAGED
) == 0
4747 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4748 != (hdr
->lma
& -maxpagesize
))))
4750 /* We don't want to put a loaded section after a
4751 nonloaded (ie. bss style) section in the same segment
4752 as that will force the non-loaded section to be loaded.
4753 Consider .tbss sections as loaded for this purpose.
4754 However, like the writable/non-writable case below,
4755 if they are on the same page then they must be put
4756 in the same segment. */
4759 else if ((abfd
->flags
& D_PAGED
) == 0)
4761 /* If the file is not demand paged, which means that we
4762 don't require the sections to be correctly aligned in the
4763 file, then there is no other reason for a new segment. */
4764 new_segment
= FALSE
;
4766 else if (info
!= NULL
4767 && info
->separate_code
4768 && executable
!= ((hdr
->flags
& SEC_CODE
) != 0))
4773 && (hdr
->flags
& SEC_READONLY
) == 0
4775 && info
->relro_end
> info
->relro_start
)
4776 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4777 != (hdr
->lma
& -maxpagesize
))))
4779 /* We don't want to put a writable section in a read only
4780 segment, unless they are on the same page in memory
4781 anyhow and there is no RELRO segment. We already
4782 know that the last section does not bring us past the
4783 current section on the page, so the only case in which
4784 the new section is not on the same page as the previous
4785 section is when the previous section ends precisely on
4791 /* Otherwise, we can use the same segment. */
4792 new_segment
= FALSE
;
4795 /* Allow interested parties a chance to override our decision. */
4796 if (last_hdr
!= NULL
4798 && info
->callbacks
->override_segment_assignment
!= NULL
)
4800 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4806 if ((hdr
->flags
& SEC_READONLY
) == 0)
4808 if ((hdr
->flags
& SEC_CODE
) != 0)
4811 /* .tbss sections effectively have zero size. */
4812 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4813 != SEC_THREAD_LOCAL
)
4814 last_size
= hdr
->size
;
4820 /* We need a new program segment. We must create a new program
4821 header holding all the sections from phdr_index until hdr. */
4823 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4830 if ((hdr
->flags
& SEC_READONLY
) == 0)
4835 if ((hdr
->flags
& SEC_CODE
) == 0)
4841 /* .tbss sections effectively have zero size. */
4842 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4843 last_size
= hdr
->size
;
4847 phdr_in_segment
= FALSE
;
4850 /* Create a final PT_LOAD program segment, but not if it's just
4852 if (last_hdr
!= NULL
4853 && (i
- phdr_index
!= 1
4854 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4855 != SEC_THREAD_LOCAL
)))
4857 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4865 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4868 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4875 /* For each batch of consecutive loadable .note sections,
4876 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4877 because if we link together nonloadable .note sections and
4878 loadable .note sections, we will generate two .note sections
4879 in the output file. FIXME: Using names for section types is
4881 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4883 if ((s
->flags
& SEC_LOAD
) != 0
4884 && CONST_STRNEQ (s
->name
, ".note"))
4889 amt
= sizeof (struct elf_segment_map
);
4890 if (s
->alignment_power
== 2)
4891 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4893 if (s2
->next
->alignment_power
== 2
4894 && (s2
->next
->flags
& SEC_LOAD
) != 0
4895 && CONST_STRNEQ (s2
->next
->name
, ".note")
4896 && align_power (s2
->lma
+ s2
->size
, 2)
4902 amt
+= (count
- 1) * sizeof (asection
*);
4903 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4907 m
->p_type
= PT_NOTE
;
4911 m
->sections
[m
->count
- count
--] = s
;
4912 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4915 m
->sections
[m
->count
- 1] = s
;
4916 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4920 if (s
->flags
& SEC_THREAD_LOCAL
)
4926 if (first_mbind
== NULL
4927 && (elf_section_flags (s
) & SHF_GNU_MBIND
) != 0)
4931 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4934 amt
= sizeof (struct elf_segment_map
);
4935 amt
+= (tls_count
- 1) * sizeof (asection
*);
4936 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4941 m
->count
= tls_count
;
4942 /* Mandated PF_R. */
4944 m
->p_flags_valid
= 1;
4946 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4948 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4951 (_("%B: TLS sections are not adjacent:"), abfd
);
4954 while (i
< (unsigned int) tls_count
)
4956 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4958 _bfd_error_handler (_(" TLS: %A"), s
);
4962 _bfd_error_handler (_(" non-TLS: %A"), s
);
4965 bfd_set_error (bfd_error_bad_value
);
4976 if (first_mbind
&& (abfd
->flags
& D_PAGED
) != 0)
4977 for (s
= first_mbind
; s
!= NULL
; s
= s
->next
)
4978 if ((elf_section_flags (s
) & SHF_GNU_MBIND
) != 0
4979 && (elf_section_data (s
)->this_hdr
.sh_info
4980 <= PT_GNU_MBIND_NUM
))
4982 /* Mandated PF_R. */
4983 unsigned long p_flags
= PF_R
;
4984 if ((s
->flags
& SEC_READONLY
) == 0)
4986 if ((s
->flags
& SEC_CODE
) != 0)
4989 amt
= sizeof (struct elf_segment_map
) + sizeof (asection
*);
4990 m
= bfd_zalloc (abfd
, amt
);
4994 m
->p_type
= (PT_GNU_MBIND_LO
4995 + elf_section_data (s
)->this_hdr
.sh_info
);
4997 m
->p_flags_valid
= 1;
4999 m
->p_flags
= p_flags
;
5005 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5007 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
5008 if (eh_frame_hdr
!= NULL
5009 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
5011 amt
= sizeof (struct elf_segment_map
);
5012 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5016 m
->p_type
= PT_GNU_EH_FRAME
;
5018 m
->sections
[0] = eh_frame_hdr
->output_section
;
5024 if (elf_stack_flags (abfd
))
5026 amt
= sizeof (struct elf_segment_map
);
5027 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5031 m
->p_type
= PT_GNU_STACK
;
5032 m
->p_flags
= elf_stack_flags (abfd
);
5033 m
->p_align
= bed
->stack_align
;
5034 m
->p_flags_valid
= 1;
5035 m
->p_align_valid
= m
->p_align
!= 0;
5036 if (info
->stacksize
> 0)
5038 m
->p_size
= info
->stacksize
;
5039 m
->p_size_valid
= 1;
5046 if (info
!= NULL
&& info
->relro
)
5048 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
5050 if (m
->p_type
== PT_LOAD
5052 && m
->sections
[0]->vma
>= info
->relro_start
5053 && m
->sections
[0]->vma
< info
->relro_end
)
5056 while (--i
!= (unsigned) -1)
5057 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
5058 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
5061 if (i
!= (unsigned) -1)
5066 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
5069 amt
= sizeof (struct elf_segment_map
);
5070 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
5074 m
->p_type
= PT_GNU_RELRO
;
5081 elf_seg_map (abfd
) = mfirst
;
5084 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
5087 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5089 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
5094 if (sections
!= NULL
)
5099 /* Sort sections by address. */
5102 elf_sort_sections (const void *arg1
, const void *arg2
)
5104 const asection
*sec1
= *(const asection
**) arg1
;
5105 const asection
*sec2
= *(const asection
**) arg2
;
5106 bfd_size_type size1
, size2
;
5108 /* Sort by LMA first, since this is the address used to
5109 place the section into a segment. */
5110 if (sec1
->lma
< sec2
->lma
)
5112 else if (sec1
->lma
> sec2
->lma
)
5115 /* Then sort by VMA. Normally the LMA and the VMA will be
5116 the same, and this will do nothing. */
5117 if (sec1
->vma
< sec2
->vma
)
5119 else if (sec1
->vma
> sec2
->vma
)
5122 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
5124 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
5130 /* If the indicies are the same, do not return 0
5131 here, but continue to try the next comparison. */
5132 if (sec1
->target_index
- sec2
->target_index
!= 0)
5133 return sec1
->target_index
- sec2
->target_index
;
5138 else if (TOEND (sec2
))
5143 /* Sort by size, to put zero sized sections
5144 before others at the same address. */
5146 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
5147 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
5154 return sec1
->target_index
- sec2
->target_index
;
5157 /* Ian Lance Taylor writes:
5159 We shouldn't be using % with a negative signed number. That's just
5160 not good. We have to make sure either that the number is not
5161 negative, or that the number has an unsigned type. When the types
5162 are all the same size they wind up as unsigned. When file_ptr is a
5163 larger signed type, the arithmetic winds up as signed long long,
5166 What we're trying to say here is something like ``increase OFF by
5167 the least amount that will cause it to be equal to the VMA modulo
5169 /* In other words, something like:
5171 vma_offset = m->sections[0]->vma % bed->maxpagesize;
5172 off_offset = off % bed->maxpagesize;
5173 if (vma_offset < off_offset)
5174 adjustment = vma_offset + bed->maxpagesize - off_offset;
5176 adjustment = vma_offset - off_offset;
5178 which can be collapsed into the expression below. */
5181 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
5183 /* PR binutils/16199: Handle an alignment of zero. */
5184 if (maxpagesize
== 0)
5186 return ((vma
- off
) % maxpagesize
);
5190 print_segment_map (const struct elf_segment_map
*m
)
5193 const char *pt
= get_segment_type (m
->p_type
);
5198 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
5199 sprintf (buf
, "LOPROC+%7.7x",
5200 (unsigned int) (m
->p_type
- PT_LOPROC
));
5201 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
5202 sprintf (buf
, "LOOS+%7.7x",
5203 (unsigned int) (m
->p_type
- PT_LOOS
));
5205 snprintf (buf
, sizeof (buf
), "%8.8x",
5206 (unsigned int) m
->p_type
);
5210 fprintf (stderr
, "%s:", pt
);
5211 for (j
= 0; j
< m
->count
; j
++)
5212 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
5218 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
5223 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
5225 buf
= bfd_zmalloc (len
);
5228 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
5233 /* Assign file positions to the sections based on the mapping from
5234 sections to segments. This function also sets up some fields in
5238 assign_file_positions_for_load_sections (bfd
*abfd
,
5239 struct bfd_link_info
*link_info
)
5241 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5242 struct elf_segment_map
*m
;
5243 Elf_Internal_Phdr
*phdrs
;
5244 Elf_Internal_Phdr
*p
;
5246 bfd_size_type maxpagesize
;
5247 unsigned int pt_load_count
= 0;
5250 bfd_vma header_pad
= 0;
5252 if (link_info
== NULL
5253 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
5257 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5261 header_pad
= m
->header_size
;
5266 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
5267 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
5271 /* PR binutils/12467. */
5272 elf_elfheader (abfd
)->e_phoff
= 0;
5273 elf_elfheader (abfd
)->e_phentsize
= 0;
5276 elf_elfheader (abfd
)->e_phnum
= alloc
;
5278 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
5279 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
5281 BFD_ASSERT (elf_program_header_size (abfd
)
5282 >= alloc
* bed
->s
->sizeof_phdr
);
5286 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
5290 /* We're writing the size in elf_program_header_size (abfd),
5291 see assign_file_positions_except_relocs, so make sure we have
5292 that amount allocated, with trailing space cleared.
5293 The variable alloc contains the computed need, while
5294 elf_program_header_size (abfd) contains the size used for the
5296 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5297 where the layout is forced to according to a larger size in the
5298 last iterations for the testcase ld-elf/header. */
5299 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
5301 phdrs
= (Elf_Internal_Phdr
*)
5303 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
5304 sizeof (Elf_Internal_Phdr
));
5305 elf_tdata (abfd
)->phdr
= phdrs
;
5310 if ((abfd
->flags
& D_PAGED
) != 0)
5311 maxpagesize
= bed
->maxpagesize
;
5313 off
= bed
->s
->sizeof_ehdr
;
5314 off
+= alloc
* bed
->s
->sizeof_phdr
;
5315 if (header_pad
< (bfd_vma
) off
)
5321 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
5323 m
= m
->next
, p
++, j
++)
5327 bfd_boolean no_contents
;
5329 /* If elf_segment_map is not from map_sections_to_segments, the
5330 sections may not be correctly ordered. NOTE: sorting should
5331 not be done to the PT_NOTE section of a corefile, which may
5332 contain several pseudo-sections artificially created by bfd.
5333 Sorting these pseudo-sections breaks things badly. */
5335 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
5336 && m
->p_type
== PT_NOTE
))
5337 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
5340 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
5341 number of sections with contents contributing to both p_filesz
5342 and p_memsz, followed by a number of sections with no contents
5343 that just contribute to p_memsz. In this loop, OFF tracks next
5344 available file offset for PT_LOAD and PT_NOTE segments. */
5345 p
->p_type
= m
->p_type
;
5346 p
->p_flags
= m
->p_flags
;
5351 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
5353 if (m
->p_paddr_valid
)
5354 p
->p_paddr
= m
->p_paddr
;
5355 else if (m
->count
== 0)
5358 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
5360 if (p
->p_type
== PT_LOAD
5361 && (abfd
->flags
& D_PAGED
) != 0)
5363 /* p_align in demand paged PT_LOAD segments effectively stores
5364 the maximum page size. When copying an executable with
5365 objcopy, we set m->p_align from the input file. Use this
5366 value for maxpagesize rather than bed->maxpagesize, which
5367 may be different. Note that we use maxpagesize for PT_TLS
5368 segment alignment later in this function, so we are relying
5369 on at least one PT_LOAD segment appearing before a PT_TLS
5371 if (m
->p_align_valid
)
5372 maxpagesize
= m
->p_align
;
5374 p
->p_align
= maxpagesize
;
5377 else if (m
->p_align_valid
)
5378 p
->p_align
= m
->p_align
;
5379 else if (m
->count
== 0)
5380 p
->p_align
= 1 << bed
->s
->log_file_align
;
5384 no_contents
= FALSE
;
5386 if (p
->p_type
== PT_LOAD
5389 bfd_size_type align
;
5390 unsigned int align_power
= 0;
5392 if (m
->p_align_valid
)
5396 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5398 unsigned int secalign
;
5400 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
5401 if (secalign
> align_power
)
5402 align_power
= secalign
;
5404 align
= (bfd_size_type
) 1 << align_power
;
5405 if (align
< maxpagesize
)
5406 align
= maxpagesize
;
5409 for (i
= 0; i
< m
->count
; i
++)
5410 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
5411 /* If we aren't making room for this section, then
5412 it must be SHT_NOBITS regardless of what we've
5413 set via struct bfd_elf_special_section. */
5414 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
5416 /* Find out whether this segment contains any loadable
5419 for (i
= 0; i
< m
->count
; i
++)
5420 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
5422 no_contents
= FALSE
;
5426 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
5428 /* Broken hardware and/or kernel require that files do not
5429 map the same page with different permissions on some hppa
5431 if (pt_load_count
> 1
5432 && bed
->no_page_alias
5433 && (off
& (maxpagesize
- 1)) != 0
5434 && (off
& -maxpagesize
) == ((off
+ off_adjust
) & -maxpagesize
))
5435 off_adjust
+= maxpagesize
;
5439 /* We shouldn't need to align the segment on disk since
5440 the segment doesn't need file space, but the gABI
5441 arguably requires the alignment and glibc ld.so
5442 checks it. So to comply with the alignment
5443 requirement but not waste file space, we adjust
5444 p_offset for just this segment. (OFF_ADJUST is
5445 subtracted from OFF later.) This may put p_offset
5446 past the end of file, but that shouldn't matter. */
5451 /* Make sure the .dynamic section is the first section in the
5452 PT_DYNAMIC segment. */
5453 else if (p
->p_type
== PT_DYNAMIC
5455 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
5458 (_("%B: The first section in the PT_DYNAMIC segment"
5459 " is not the .dynamic section"),
5461 bfd_set_error (bfd_error_bad_value
);
5464 /* Set the note section type to SHT_NOTE. */
5465 else if (p
->p_type
== PT_NOTE
)
5466 for (i
= 0; i
< m
->count
; i
++)
5467 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
5473 if (m
->includes_filehdr
)
5475 if (!m
->p_flags_valid
)
5477 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
5478 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
5481 if (p
->p_vaddr
< (bfd_vma
) off
5482 || (!m
->p_paddr_valid
5483 && p
->p_paddr
< (bfd_vma
) off
))
5486 (_("%B: Not enough room for program headers,"
5487 " try linking with -N"),
5489 bfd_set_error (bfd_error_bad_value
);
5494 if (!m
->p_paddr_valid
)
5499 if (m
->includes_phdrs
)
5501 if (!m
->p_flags_valid
)
5504 if (!m
->includes_filehdr
)
5506 p
->p_offset
= bed
->s
->sizeof_ehdr
;
5510 p
->p_vaddr
-= off
- p
->p_offset
;
5511 if (!m
->p_paddr_valid
)
5512 p
->p_paddr
-= off
- p
->p_offset
;
5516 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
5517 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
5520 p
->p_filesz
+= header_pad
;
5521 p
->p_memsz
+= header_pad
;
5525 if (p
->p_type
== PT_LOAD
5526 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5528 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5534 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5536 p
->p_filesz
+= adjust
;
5537 p
->p_memsz
+= adjust
;
5541 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5542 maps. Set filepos for sections in PT_LOAD segments, and in
5543 core files, for sections in PT_NOTE segments.
5544 assign_file_positions_for_non_load_sections will set filepos
5545 for other sections and update p_filesz for other segments. */
5546 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5549 bfd_size_type align
;
5550 Elf_Internal_Shdr
*this_hdr
;
5553 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5554 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5556 if ((p
->p_type
== PT_LOAD
5557 || p
->p_type
== PT_TLS
)
5558 && (this_hdr
->sh_type
!= SHT_NOBITS
5559 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5560 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5561 || p
->p_type
== PT_TLS
))))
5563 bfd_vma p_start
= p
->p_paddr
;
5564 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5565 bfd_vma s_start
= sec
->lma
;
5566 bfd_vma adjust
= s_start
- p_end
;
5570 || p_end
< p_start
))
5573 /* xgettext:c-format */
5574 (_("%B: section %A lma %#Lx adjusted to %#Lx"),
5575 abfd
, sec
, s_start
, p_end
);
5579 p
->p_memsz
+= adjust
;
5581 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5583 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5585 /* We have a PROGBITS section following NOBITS ones.
5586 Allocate file space for the NOBITS section(s) and
5588 adjust
= p
->p_memsz
- p
->p_filesz
;
5589 if (!write_zeros (abfd
, off
, adjust
))
5593 p
->p_filesz
+= adjust
;
5597 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5599 /* The section at i == 0 is the one that actually contains
5603 this_hdr
->sh_offset
= sec
->filepos
= off
;
5604 off
+= this_hdr
->sh_size
;
5605 p
->p_filesz
= this_hdr
->sh_size
;
5611 /* The rest are fake sections that shouldn't be written. */
5620 if (p
->p_type
== PT_LOAD
)
5622 this_hdr
->sh_offset
= sec
->filepos
= off
;
5623 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5624 off
+= this_hdr
->sh_size
;
5626 else if (this_hdr
->sh_type
== SHT_NOBITS
5627 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5628 && this_hdr
->sh_offset
== 0)
5630 /* This is a .tbss section that didn't get a PT_LOAD.
5631 (See _bfd_elf_map_sections_to_segments "Create a
5632 final PT_LOAD".) Set sh_offset to the value it
5633 would have if we had created a zero p_filesz and
5634 p_memsz PT_LOAD header for the section. This
5635 also makes the PT_TLS header have the same
5637 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5639 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5642 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5644 p
->p_filesz
+= this_hdr
->sh_size
;
5645 /* A load section without SHF_ALLOC is something like
5646 a note section in a PT_NOTE segment. These take
5647 file space but are not loaded into memory. */
5648 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5649 p
->p_memsz
+= this_hdr
->sh_size
;
5651 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5653 if (p
->p_type
== PT_TLS
)
5654 p
->p_memsz
+= this_hdr
->sh_size
;
5656 /* .tbss is special. It doesn't contribute to p_memsz of
5658 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5659 p
->p_memsz
+= this_hdr
->sh_size
;
5662 if (align
> p
->p_align
5663 && !m
->p_align_valid
5664 && (p
->p_type
!= PT_LOAD
5665 || (abfd
->flags
& D_PAGED
) == 0))
5669 if (!m
->p_flags_valid
)
5672 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5674 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5681 /* Check that all sections are in a PT_LOAD segment.
5682 Don't check funky gdb generated core files. */
5683 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5685 bfd_boolean check_vma
= TRUE
;
5687 for (i
= 1; i
< m
->count
; i
++)
5688 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5689 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5690 ->this_hdr
), p
) != 0
5691 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5692 ->this_hdr
), p
) != 0)
5694 /* Looks like we have overlays packed into the segment. */
5699 for (i
= 0; i
< m
->count
; i
++)
5701 Elf_Internal_Shdr
*this_hdr
;
5704 sec
= m
->sections
[i
];
5705 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5706 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5707 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5710 /* xgettext:c-format */
5711 (_("%B: section `%A' can't be allocated in segment %d"),
5713 print_segment_map (m
);
5719 elf_next_file_pos (abfd
) = off
;
5723 /* Assign file positions for the other sections. */
5726 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5727 struct bfd_link_info
*link_info
)
5729 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5730 Elf_Internal_Shdr
**i_shdrpp
;
5731 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5732 Elf_Internal_Phdr
*phdrs
;
5733 Elf_Internal_Phdr
*p
;
5734 struct elf_segment_map
*m
;
5735 struct elf_segment_map
*hdrs_segment
;
5736 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5737 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5741 i_shdrpp
= elf_elfsections (abfd
);
5742 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5743 off
= elf_next_file_pos (abfd
);
5744 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5746 Elf_Internal_Shdr
*hdr
;
5749 if (hdr
->bfd_section
!= NULL
5750 && (hdr
->bfd_section
->filepos
!= 0
5751 || (hdr
->sh_type
== SHT_NOBITS
5752 && hdr
->contents
== NULL
)))
5753 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5754 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5756 if (hdr
->sh_size
!= 0)
5758 /* xgettext:c-format */
5759 (_("%B: warning: allocated section `%s' not in segment"),
5761 (hdr
->bfd_section
== NULL
5763 : hdr
->bfd_section
->name
));
5764 /* We don't need to page align empty sections. */
5765 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5766 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5769 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5771 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5774 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5775 && hdr
->bfd_section
== NULL
)
5776 || (hdr
->bfd_section
!= NULL
5777 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5778 /* Compress DWARF debug sections. */
5779 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5780 || (elf_symtab_shndx_list (abfd
) != NULL
5781 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
5782 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5783 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5784 hdr
->sh_offset
= -1;
5786 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5789 /* Now that we have set the section file positions, we can set up
5790 the file positions for the non PT_LOAD segments. */
5794 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5796 hdrs_segment
= NULL
;
5797 phdrs
= elf_tdata (abfd
)->phdr
;
5798 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5801 if (p
->p_type
!= PT_LOAD
)
5804 if (m
->includes_filehdr
)
5806 filehdr_vaddr
= p
->p_vaddr
;
5807 filehdr_paddr
= p
->p_paddr
;
5809 if (m
->includes_phdrs
)
5811 phdrs_vaddr
= p
->p_vaddr
;
5812 phdrs_paddr
= p
->p_paddr
;
5813 if (m
->includes_filehdr
)
5816 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5817 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5822 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5824 /* There is a segment that contains both the file headers and the
5825 program headers, so provide a symbol __ehdr_start pointing there.
5826 A program can use this to examine itself robustly. */
5828 struct elf_link_hash_entry
*hash
5829 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5830 FALSE
, FALSE
, TRUE
);
5831 /* If the symbol was referenced and not defined, define it. */
5833 && (hash
->root
.type
== bfd_link_hash_new
5834 || hash
->root
.type
== bfd_link_hash_undefined
5835 || hash
->root
.type
== bfd_link_hash_undefweak
5836 || hash
->root
.type
== bfd_link_hash_common
))
5839 if (hdrs_segment
->count
!= 0)
5840 /* The segment contains sections, so use the first one. */
5841 s
= hdrs_segment
->sections
[0];
5843 /* Use the first (i.e. lowest-addressed) section in any segment. */
5844 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5853 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5854 hash
->root
.u
.def
.section
= s
;
5858 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5859 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5862 hash
->root
.type
= bfd_link_hash_defined
;
5863 hash
->def_regular
= 1;
5868 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5870 if (p
->p_type
== PT_GNU_RELRO
)
5872 const Elf_Internal_Phdr
*lp
;
5873 struct elf_segment_map
*lm
;
5875 if (link_info
!= NULL
)
5877 /* During linking the range of the RELRO segment is passed
5879 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5881 lm
= lm
->next
, lp
++)
5883 if (lp
->p_type
== PT_LOAD
5884 && lp
->p_vaddr
< link_info
->relro_end
5886 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5890 BFD_ASSERT (lm
!= NULL
);
5894 /* Otherwise we are copying an executable or shared
5895 library, but we need to use the same linker logic. */
5896 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5898 if (lp
->p_type
== PT_LOAD
5899 && lp
->p_paddr
== p
->p_paddr
)
5904 if (lp
< phdrs
+ count
)
5906 p
->p_vaddr
= lp
->p_vaddr
;
5907 p
->p_paddr
= lp
->p_paddr
;
5908 p
->p_offset
= lp
->p_offset
;
5909 if (link_info
!= NULL
)
5910 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5911 else if (m
->p_size_valid
)
5912 p
->p_filesz
= m
->p_size
;
5915 p
->p_memsz
= p
->p_filesz
;
5916 /* Preserve the alignment and flags if they are valid. The
5917 gold linker generates RW/4 for the PT_GNU_RELRO section.
5918 It is better for objcopy/strip to honor these attributes
5919 otherwise gdb will choke when using separate debug files.
5921 if (!m
->p_align_valid
)
5923 if (!m
->p_flags_valid
)
5928 memset (p
, 0, sizeof *p
);
5929 p
->p_type
= PT_NULL
;
5932 else if (p
->p_type
== PT_GNU_STACK
)
5934 if (m
->p_size_valid
)
5935 p
->p_memsz
= m
->p_size
;
5937 else if (m
->count
!= 0)
5941 if (p
->p_type
!= PT_LOAD
5942 && (p
->p_type
!= PT_NOTE
5943 || bfd_get_format (abfd
) != bfd_core
))
5945 /* A user specified segment layout may include a PHDR
5946 segment that overlaps with a LOAD segment... */
5947 if (p
->p_type
== PT_PHDR
)
5953 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5955 /* PR 17512: file: 2195325e. */
5957 (_("%B: error: non-load segment %d includes file header "
5958 "and/or program header"),
5959 abfd
, (int) (p
- phdrs
));
5964 p
->p_offset
= m
->sections
[0]->filepos
;
5965 for (i
= m
->count
; i
-- != 0;)
5967 asection
*sect
= m
->sections
[i
];
5968 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5969 if (hdr
->sh_type
!= SHT_NOBITS
)
5971 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5978 else if (m
->includes_filehdr
)
5980 p
->p_vaddr
= filehdr_vaddr
;
5981 if (! m
->p_paddr_valid
)
5982 p
->p_paddr
= filehdr_paddr
;
5984 else if (m
->includes_phdrs
)
5986 p
->p_vaddr
= phdrs_vaddr
;
5987 if (! m
->p_paddr_valid
)
5988 p
->p_paddr
= phdrs_paddr
;
5992 elf_next_file_pos (abfd
) = off
;
5997 static elf_section_list
*
5998 find_section_in_list (unsigned int i
, elf_section_list
* list
)
6000 for (;list
!= NULL
; list
= list
->next
)
6006 /* Work out the file positions of all the sections. This is called by
6007 _bfd_elf_compute_section_file_positions. All the section sizes and
6008 VMAs must be known before this is called.
6010 Reloc sections come in two flavours: Those processed specially as
6011 "side-channel" data attached to a section to which they apply, and
6012 those that bfd doesn't process as relocations. The latter sort are
6013 stored in a normal bfd section by bfd_section_from_shdr. We don't
6014 consider the former sort here, unless they form part of the loadable
6015 image. Reloc sections not assigned here will be handled later by
6016 assign_file_positions_for_relocs.
6018 We also don't set the positions of the .symtab and .strtab here. */
6021 assign_file_positions_except_relocs (bfd
*abfd
,
6022 struct bfd_link_info
*link_info
)
6024 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
6025 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
6026 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6028 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
6029 && bfd_get_format (abfd
) != bfd_core
)
6031 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
6032 unsigned int num_sec
= elf_numsections (abfd
);
6033 Elf_Internal_Shdr
**hdrpp
;
6037 /* Start after the ELF header. */
6038 off
= i_ehdrp
->e_ehsize
;
6040 /* We are not creating an executable, which means that we are
6041 not creating a program header, and that the actual order of
6042 the sections in the file is unimportant. */
6043 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
6045 Elf_Internal_Shdr
*hdr
;
6048 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
6049 && hdr
->bfd_section
== NULL
)
6050 || (hdr
->bfd_section
!= NULL
6051 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
6052 /* Compress DWARF debug sections. */
6053 || i
== elf_onesymtab (abfd
)
6054 || (elf_symtab_shndx_list (abfd
) != NULL
6055 && hdr
== i_shdrpp
[elf_symtab_shndx_list (abfd
)->ndx
])
6056 || i
== elf_strtab_sec (abfd
)
6057 || i
== elf_shstrtab_sec (abfd
))
6059 hdr
->sh_offset
= -1;
6062 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
6065 elf_next_file_pos (abfd
) = off
;
6071 /* Assign file positions for the loaded sections based on the
6072 assignment of sections to segments. */
6073 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
6076 /* And for non-load sections. */
6077 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
6080 if (bed
->elf_backend_modify_program_headers
!= NULL
)
6082 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
6086 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
6087 if (link_info
!= NULL
&& bfd_link_pie (link_info
))
6089 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
6090 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
6091 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
6093 /* Find the lowest p_vaddr in PT_LOAD segments. */
6094 bfd_vma p_vaddr
= (bfd_vma
) -1;
6095 for (; segment
< end_segment
; segment
++)
6096 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
6097 p_vaddr
= segment
->p_vaddr
;
6099 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
6100 segments is non-zero. */
6102 i_ehdrp
->e_type
= ET_EXEC
;
6105 /* Write out the program headers. */
6106 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
6108 /* Sort the program headers into the ordering required by the ELF standard. */
6112 /* PR ld/20815 - Check that the program header segment, if present, will
6113 be loaded into memory. FIXME: The check below is not sufficient as
6114 really all PT_LOAD segments should be checked before issuing an error
6115 message. Plus the PHDR segment does not have to be the first segment
6116 in the program header table. But this version of the check should
6117 catch all real world use cases.
6119 FIXME: We used to have code here to sort the PT_LOAD segments into
6120 ascending order, as per the ELF spec. But this breaks some programs,
6121 including the Linux kernel. But really either the spec should be
6122 changed or the programs updated. */
6124 && tdata
->phdr
[0].p_type
== PT_PHDR
6125 && ! bed
->elf_backend_allow_non_load_phdr (abfd
, tdata
->phdr
, alloc
)
6126 && tdata
->phdr
[1].p_type
== PT_LOAD
6127 && (tdata
->phdr
[1].p_vaddr
> tdata
->phdr
[0].p_vaddr
6128 || (tdata
->phdr
[1].p_vaddr
+ tdata
->phdr
[1].p_memsz
)
6129 < (tdata
->phdr
[0].p_vaddr
+ tdata
->phdr
[0].p_memsz
)))
6131 /* The fix for this error is usually to edit the linker script being
6132 used and set up the program headers manually. Either that or
6133 leave room for the headers at the start of the SECTIONS. */
6134 _bfd_error_handler (_("\
6135 %B: error: PHDR segment not covered by LOAD segment"),
6140 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
6141 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
6149 prep_headers (bfd
*abfd
)
6151 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
6152 struct elf_strtab_hash
*shstrtab
;
6153 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6155 i_ehdrp
= elf_elfheader (abfd
);
6157 shstrtab
= _bfd_elf_strtab_init ();
6158 if (shstrtab
== NULL
)
6161 elf_shstrtab (abfd
) = shstrtab
;
6163 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
6164 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
6165 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
6166 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
6168 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
6169 i_ehdrp
->e_ident
[EI_DATA
] =
6170 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
6171 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
6173 if ((abfd
->flags
& DYNAMIC
) != 0)
6174 i_ehdrp
->e_type
= ET_DYN
;
6175 else if ((abfd
->flags
& EXEC_P
) != 0)
6176 i_ehdrp
->e_type
= ET_EXEC
;
6177 else if (bfd_get_format (abfd
) == bfd_core
)
6178 i_ehdrp
->e_type
= ET_CORE
;
6180 i_ehdrp
->e_type
= ET_REL
;
6182 switch (bfd_get_arch (abfd
))
6184 case bfd_arch_unknown
:
6185 i_ehdrp
->e_machine
= EM_NONE
;
6188 /* There used to be a long list of cases here, each one setting
6189 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
6190 in the corresponding bfd definition. To avoid duplication,
6191 the switch was removed. Machines that need special handling
6192 can generally do it in elf_backend_final_write_processing(),
6193 unless they need the information earlier than the final write.
6194 Such need can generally be supplied by replacing the tests for
6195 e_machine with the conditions used to determine it. */
6197 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
6200 i_ehdrp
->e_version
= bed
->s
->ev_current
;
6201 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
6203 /* No program header, for now. */
6204 i_ehdrp
->e_phoff
= 0;
6205 i_ehdrp
->e_phentsize
= 0;
6206 i_ehdrp
->e_phnum
= 0;
6208 /* Each bfd section is section header entry. */
6209 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
6210 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
6212 /* If we're building an executable, we'll need a program header table. */
6213 if (abfd
->flags
& EXEC_P
)
6214 /* It all happens later. */
6218 i_ehdrp
->e_phentsize
= 0;
6219 i_ehdrp
->e_phoff
= 0;
6222 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
6223 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
6224 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
6225 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
6226 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
6227 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
6228 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
6229 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
6230 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
6236 /* Assign file positions for all the reloc sections which are not part
6237 of the loadable file image, and the file position of section headers. */
6240 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
6243 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
6244 Elf_Internal_Shdr
*shdrp
;
6245 Elf_Internal_Ehdr
*i_ehdrp
;
6246 const struct elf_backend_data
*bed
;
6248 off
= elf_next_file_pos (abfd
);
6250 shdrpp
= elf_elfsections (abfd
);
6251 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
6252 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
6255 if (shdrp
->sh_offset
== -1)
6257 asection
*sec
= shdrp
->bfd_section
;
6258 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
6259 || shdrp
->sh_type
== SHT_RELA
);
6261 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
6265 const char *name
= sec
->name
;
6266 struct bfd_elf_section_data
*d
;
6268 /* Compress DWARF debug sections. */
6269 if (!bfd_compress_section (abfd
, sec
,
6273 if (sec
->compress_status
== COMPRESS_SECTION_DONE
6274 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
6276 /* If section is compressed with zlib-gnu, convert
6277 section name from .debug_* to .zdebug_*. */
6279 = convert_debug_to_zdebug (abfd
, name
);
6280 if (new_name
== NULL
)
6284 /* Add section name to section name section. */
6285 if (shdrp
->sh_name
!= (unsigned int) -1)
6288 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
6290 d
= elf_section_data (sec
);
6292 /* Add reloc section name to section name section. */
6294 && !_bfd_elf_set_reloc_sh_name (abfd
,
6299 && !_bfd_elf_set_reloc_sh_name (abfd
,
6304 /* Update section size and contents. */
6305 shdrp
->sh_size
= sec
->size
;
6306 shdrp
->contents
= sec
->contents
;
6307 shdrp
->bfd_section
->contents
= NULL
;
6309 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
6316 /* Place section name section after DWARF debug sections have been
6318 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
6319 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
6320 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
6321 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
6323 /* Place the section headers. */
6324 i_ehdrp
= elf_elfheader (abfd
);
6325 bed
= get_elf_backend_data (abfd
);
6326 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
6327 i_ehdrp
->e_shoff
= off
;
6328 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
6329 elf_next_file_pos (abfd
) = off
;
6335 _bfd_elf_write_object_contents (bfd
*abfd
)
6337 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6338 Elf_Internal_Shdr
**i_shdrp
;
6340 unsigned int count
, num_sec
;
6341 struct elf_obj_tdata
*t
;
6343 if (! abfd
->output_has_begun
6344 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
6347 i_shdrp
= elf_elfsections (abfd
);
6350 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
6354 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
6357 /* After writing the headers, we need to write the sections too... */
6358 num_sec
= elf_numsections (abfd
);
6359 for (count
= 1; count
< num_sec
; count
++)
6361 i_shdrp
[count
]->sh_name
6362 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
6363 i_shdrp
[count
]->sh_name
);
6364 if (bed
->elf_backend_section_processing
)
6365 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
6366 if (i_shdrp
[count
]->contents
)
6368 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
6370 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
6371 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
6376 /* Write out the section header names. */
6377 t
= elf_tdata (abfd
);
6378 if (elf_shstrtab (abfd
) != NULL
6379 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
6380 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
6383 if (bed
->elf_backend_final_write_processing
)
6384 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
6386 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
6389 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
6390 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
6391 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
6397 _bfd_elf_write_corefile_contents (bfd
*abfd
)
6399 /* Hopefully this can be done just like an object file. */
6400 return _bfd_elf_write_object_contents (abfd
);
6403 /* Given a section, search the header to find them. */
6406 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
6408 const struct elf_backend_data
*bed
;
6409 unsigned int sec_index
;
6411 if (elf_section_data (asect
) != NULL
6412 && elf_section_data (asect
)->this_idx
!= 0)
6413 return elf_section_data (asect
)->this_idx
;
6415 if (bfd_is_abs_section (asect
))
6416 sec_index
= SHN_ABS
;
6417 else if (bfd_is_com_section (asect
))
6418 sec_index
= SHN_COMMON
;
6419 else if (bfd_is_und_section (asect
))
6420 sec_index
= SHN_UNDEF
;
6422 sec_index
= SHN_BAD
;
6424 bed
= get_elf_backend_data (abfd
);
6425 if (bed
->elf_backend_section_from_bfd_section
)
6427 int retval
= sec_index
;
6429 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
6433 if (sec_index
== SHN_BAD
)
6434 bfd_set_error (bfd_error_nonrepresentable_section
);
6439 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
6443 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
6445 asymbol
*asym_ptr
= *asym_ptr_ptr
;
6447 flagword flags
= asym_ptr
->flags
;
6449 /* When gas creates relocations against local labels, it creates its
6450 own symbol for the section, but does put the symbol into the
6451 symbol chain, so udata is 0. When the linker is generating
6452 relocatable output, this section symbol may be for one of the
6453 input sections rather than the output section. */
6454 if (asym_ptr
->udata
.i
== 0
6455 && (flags
& BSF_SECTION_SYM
)
6456 && asym_ptr
->section
)
6461 sec
= asym_ptr
->section
;
6462 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
6463 sec
= sec
->output_section
;
6464 if (sec
->owner
== abfd
6465 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
6466 && elf_section_syms (abfd
)[indx
] != NULL
)
6467 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
6470 idx
= asym_ptr
->udata
.i
;
6474 /* This case can occur when using --strip-symbol on a symbol
6475 which is used in a relocation entry. */
6477 /* xgettext:c-format */
6478 (_("%B: symbol `%s' required but not present"),
6479 abfd
, bfd_asymbol_name (asym_ptr
));
6480 bfd_set_error (bfd_error_no_symbols
);
6487 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n",
6488 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
);
6496 /* Rewrite program header information. */
6499 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6501 Elf_Internal_Ehdr
*iehdr
;
6502 struct elf_segment_map
*map
;
6503 struct elf_segment_map
*map_first
;
6504 struct elf_segment_map
**pointer_to_map
;
6505 Elf_Internal_Phdr
*segment
;
6508 unsigned int num_segments
;
6509 bfd_boolean phdr_included
= FALSE
;
6510 bfd_boolean p_paddr_valid
;
6511 bfd_vma maxpagesize
;
6512 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
6513 unsigned int phdr_adjust_num
= 0;
6514 const struct elf_backend_data
*bed
;
6516 bed
= get_elf_backend_data (ibfd
);
6517 iehdr
= elf_elfheader (ibfd
);
6520 pointer_to_map
= &map_first
;
6522 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6523 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
6525 /* Returns the end address of the segment + 1. */
6526 #define SEGMENT_END(segment, start) \
6527 (start + (segment->p_memsz > segment->p_filesz \
6528 ? segment->p_memsz : segment->p_filesz))
6530 #define SECTION_SIZE(section, segment) \
6531 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
6532 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
6533 ? section->size : 0)
6535 /* Returns TRUE if the given section is contained within
6536 the given segment. VMA addresses are compared. */
6537 #define IS_CONTAINED_BY_VMA(section, segment) \
6538 (section->vma >= segment->p_vaddr \
6539 && (section->vma + SECTION_SIZE (section, segment) \
6540 <= (SEGMENT_END (segment, segment->p_vaddr))))
6542 /* Returns TRUE if the given section is contained within
6543 the given segment. LMA addresses are compared. */
6544 #define IS_CONTAINED_BY_LMA(section, segment, base) \
6545 (section->lma >= base \
6546 && (section->lma + SECTION_SIZE (section, segment) \
6547 <= SEGMENT_END (segment, base)))
6549 /* Handle PT_NOTE segment. */
6550 #define IS_NOTE(p, s) \
6551 (p->p_type == PT_NOTE \
6552 && elf_section_type (s) == SHT_NOTE \
6553 && (bfd_vma) s->filepos >= p->p_offset \
6554 && ((bfd_vma) s->filepos + s->size \
6555 <= p->p_offset + p->p_filesz))
6557 /* Special case: corefile "NOTE" section containing regs, prpsinfo
6559 #define IS_COREFILE_NOTE(p, s) \
6561 && bfd_get_format (ibfd) == bfd_core \
6565 /* The complicated case when p_vaddr is 0 is to handle the Solaris
6566 linker, which generates a PT_INTERP section with p_vaddr and
6567 p_memsz set to 0. */
6568 #define IS_SOLARIS_PT_INTERP(p, s) \
6570 && p->p_paddr == 0 \
6571 && p->p_memsz == 0 \
6572 && p->p_filesz > 0 \
6573 && (s->flags & SEC_HAS_CONTENTS) != 0 \
6575 && (bfd_vma) s->filepos >= p->p_offset \
6576 && ((bfd_vma) s->filepos + s->size \
6577 <= p->p_offset + p->p_filesz))
6579 /* Decide if the given section should be included in the given segment.
6580 A section will be included if:
6581 1. It is within the address space of the segment -- we use the LMA
6582 if that is set for the segment and the VMA otherwise,
6583 2. It is an allocated section or a NOTE section in a PT_NOTE
6585 3. There is an output section associated with it,
6586 4. The section has not already been allocated to a previous segment.
6587 5. PT_GNU_STACK segments do not include any sections.
6588 6. PT_TLS segment includes only SHF_TLS sections.
6589 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6590 8. PT_DYNAMIC should not contain empty sections at the beginning
6591 (with the possible exception of .dynamic). */
6592 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6593 ((((segment->p_paddr \
6594 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6595 : IS_CONTAINED_BY_VMA (section, segment)) \
6596 && (section->flags & SEC_ALLOC) != 0) \
6597 || IS_NOTE (segment, section)) \
6598 && segment->p_type != PT_GNU_STACK \
6599 && (segment->p_type != PT_TLS \
6600 || (section->flags & SEC_THREAD_LOCAL)) \
6601 && (segment->p_type == PT_LOAD \
6602 || segment->p_type == PT_TLS \
6603 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6604 && (segment->p_type != PT_DYNAMIC \
6605 || SECTION_SIZE (section, segment) > 0 \
6606 || (segment->p_paddr \
6607 ? segment->p_paddr != section->lma \
6608 : segment->p_vaddr != section->vma) \
6609 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6611 && !section->segment_mark)
6613 /* If the output section of a section in the input segment is NULL,
6614 it is removed from the corresponding output segment. */
6615 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6616 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6617 && section->output_section != NULL)
6619 /* Returns TRUE iff seg1 starts after the end of seg2. */
6620 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6621 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6623 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6624 their VMA address ranges and their LMA address ranges overlap.
6625 It is possible to have overlapping VMA ranges without overlapping LMA
6626 ranges. RedBoot images for example can have both .data and .bss mapped
6627 to the same VMA range, but with the .data section mapped to a different
6629 #define SEGMENT_OVERLAPS(seg1, seg2) \
6630 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6631 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6632 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6633 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6635 /* Initialise the segment mark field. */
6636 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6637 section
->segment_mark
= FALSE
;
6639 /* The Solaris linker creates program headers in which all the
6640 p_paddr fields are zero. When we try to objcopy or strip such a
6641 file, we get confused. Check for this case, and if we find it
6642 don't set the p_paddr_valid fields. */
6643 p_paddr_valid
= FALSE
;
6644 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6647 if (segment
->p_paddr
!= 0)
6649 p_paddr_valid
= TRUE
;
6653 /* Scan through the segments specified in the program header
6654 of the input BFD. For this first scan we look for overlaps
6655 in the loadable segments. These can be created by weird
6656 parameters to objcopy. Also, fix some solaris weirdness. */
6657 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6662 Elf_Internal_Phdr
*segment2
;
6664 if (segment
->p_type
== PT_INTERP
)
6665 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6666 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6668 /* Mininal change so that the normal section to segment
6669 assignment code will work. */
6670 segment
->p_vaddr
= section
->vma
;
6674 if (segment
->p_type
!= PT_LOAD
)
6676 /* Remove PT_GNU_RELRO segment. */
6677 if (segment
->p_type
== PT_GNU_RELRO
)
6678 segment
->p_type
= PT_NULL
;
6682 /* Determine if this segment overlaps any previous segments. */
6683 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6685 bfd_signed_vma extra_length
;
6687 if (segment2
->p_type
!= PT_LOAD
6688 || !SEGMENT_OVERLAPS (segment
, segment2
))
6691 /* Merge the two segments together. */
6692 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6694 /* Extend SEGMENT2 to include SEGMENT and then delete
6696 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6697 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6699 if (extra_length
> 0)
6701 segment2
->p_memsz
+= extra_length
;
6702 segment2
->p_filesz
+= extra_length
;
6705 segment
->p_type
= PT_NULL
;
6707 /* Since we have deleted P we must restart the outer loop. */
6709 segment
= elf_tdata (ibfd
)->phdr
;
6714 /* Extend SEGMENT to include SEGMENT2 and then delete
6716 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6717 - SEGMENT_END (segment
, segment
->p_vaddr
));
6719 if (extra_length
> 0)
6721 segment
->p_memsz
+= extra_length
;
6722 segment
->p_filesz
+= extra_length
;
6725 segment2
->p_type
= PT_NULL
;
6730 /* The second scan attempts to assign sections to segments. */
6731 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6735 unsigned int section_count
;
6736 asection
**sections
;
6737 asection
*output_section
;
6739 bfd_vma matching_lma
;
6740 bfd_vma suggested_lma
;
6743 asection
*first_section
;
6744 bfd_boolean first_matching_lma
;
6745 bfd_boolean first_suggested_lma
;
6747 if (segment
->p_type
== PT_NULL
)
6750 first_section
= NULL
;
6751 /* Compute how many sections might be placed into this segment. */
6752 for (section
= ibfd
->sections
, section_count
= 0;
6754 section
= section
->next
)
6756 /* Find the first section in the input segment, which may be
6757 removed from the corresponding output segment. */
6758 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6760 if (first_section
== NULL
)
6761 first_section
= section
;
6762 if (section
->output_section
!= NULL
)
6767 /* Allocate a segment map big enough to contain
6768 all of the sections we have selected. */
6769 amt
= sizeof (struct elf_segment_map
);
6770 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6771 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6775 /* Initialise the fields of the segment map. Default to
6776 using the physical address of the segment in the input BFD. */
6778 map
->p_type
= segment
->p_type
;
6779 map
->p_flags
= segment
->p_flags
;
6780 map
->p_flags_valid
= 1;
6782 /* If the first section in the input segment is removed, there is
6783 no need to preserve segment physical address in the corresponding
6785 if (!first_section
|| first_section
->output_section
!= NULL
)
6787 map
->p_paddr
= segment
->p_paddr
;
6788 map
->p_paddr_valid
= p_paddr_valid
;
6791 /* Determine if this segment contains the ELF file header
6792 and if it contains the program headers themselves. */
6793 map
->includes_filehdr
= (segment
->p_offset
== 0
6794 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6795 map
->includes_phdrs
= 0;
6797 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6799 map
->includes_phdrs
=
6800 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6801 && (segment
->p_offset
+ segment
->p_filesz
6802 >= ((bfd_vma
) iehdr
->e_phoff
6803 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6805 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6806 phdr_included
= TRUE
;
6809 if (section_count
== 0)
6811 /* Special segments, such as the PT_PHDR segment, may contain
6812 no sections, but ordinary, loadable segments should contain
6813 something. They are allowed by the ELF spec however, so only
6814 a warning is produced.
6815 There is however the valid use case of embedded systems which
6816 have segments with p_filesz of 0 and a p_memsz > 0 to initialize
6817 flash memory with zeros. No warning is shown for that case. */
6818 if (segment
->p_type
== PT_LOAD
6819 && (segment
->p_filesz
> 0 || segment
->p_memsz
== 0))
6820 /* xgettext:c-format */
6821 _bfd_error_handler (_("%B: warning: Empty loadable segment detected"
6822 " at vaddr=%#Lx, is this intentional?"),
6823 ibfd
, segment
->p_vaddr
);
6826 *pointer_to_map
= map
;
6827 pointer_to_map
= &map
->next
;
6832 /* Now scan the sections in the input BFD again and attempt
6833 to add their corresponding output sections to the segment map.
6834 The problem here is how to handle an output section which has
6835 been moved (ie had its LMA changed). There are four possibilities:
6837 1. None of the sections have been moved.
6838 In this case we can continue to use the segment LMA from the
6841 2. All of the sections have been moved by the same amount.
6842 In this case we can change the segment's LMA to match the LMA
6843 of the first section.
6845 3. Some of the sections have been moved, others have not.
6846 In this case those sections which have not been moved can be
6847 placed in the current segment which will have to have its size,
6848 and possibly its LMA changed, and a new segment or segments will
6849 have to be created to contain the other sections.
6851 4. The sections have been moved, but not by the same amount.
6852 In this case we can change the segment's LMA to match the LMA
6853 of the first section and we will have to create a new segment
6854 or segments to contain the other sections.
6856 In order to save time, we allocate an array to hold the section
6857 pointers that we are interested in. As these sections get assigned
6858 to a segment, they are removed from this array. */
6860 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6861 if (sections
== NULL
)
6864 /* Step One: Scan for segment vs section LMA conflicts.
6865 Also add the sections to the section array allocated above.
6866 Also add the sections to the current segment. In the common
6867 case, where the sections have not been moved, this means that
6868 we have completely filled the segment, and there is nothing
6873 first_matching_lma
= TRUE
;
6874 first_suggested_lma
= TRUE
;
6876 for (section
= first_section
, j
= 0;
6878 section
= section
->next
)
6880 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6882 output_section
= section
->output_section
;
6884 sections
[j
++] = section
;
6886 /* The Solaris native linker always sets p_paddr to 0.
6887 We try to catch that case here, and set it to the
6888 correct value. Note - some backends require that
6889 p_paddr be left as zero. */
6891 && segment
->p_vaddr
!= 0
6892 && !bed
->want_p_paddr_set_to_zero
6894 && output_section
->lma
!= 0
6895 && output_section
->vma
== (segment
->p_vaddr
6896 + (map
->includes_filehdr
6899 + (map
->includes_phdrs
6901 * iehdr
->e_phentsize
)
6903 map
->p_paddr
= segment
->p_vaddr
;
6905 /* Match up the physical address of the segment with the
6906 LMA address of the output section. */
6907 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6908 || IS_COREFILE_NOTE (segment
, section
)
6909 || (bed
->want_p_paddr_set_to_zero
6910 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6912 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6914 matching_lma
= output_section
->lma
;
6915 first_matching_lma
= FALSE
;
6918 /* We assume that if the section fits within the segment
6919 then it does not overlap any other section within that
6921 map
->sections
[isec
++] = output_section
;
6923 else if (first_suggested_lma
)
6925 suggested_lma
= output_section
->lma
;
6926 first_suggested_lma
= FALSE
;
6929 if (j
== section_count
)
6934 BFD_ASSERT (j
== section_count
);
6936 /* Step Two: Adjust the physical address of the current segment,
6938 if (isec
== section_count
)
6940 /* All of the sections fitted within the segment as currently
6941 specified. This is the default case. Add the segment to
6942 the list of built segments and carry on to process the next
6943 program header in the input BFD. */
6944 map
->count
= section_count
;
6945 *pointer_to_map
= map
;
6946 pointer_to_map
= &map
->next
;
6949 && !bed
->want_p_paddr_set_to_zero
6950 && matching_lma
!= map
->p_paddr
6951 && !map
->includes_filehdr
6952 && !map
->includes_phdrs
)
6953 /* There is some padding before the first section in the
6954 segment. So, we must account for that in the output
6956 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6963 if (!first_matching_lma
)
6965 /* At least one section fits inside the current segment.
6966 Keep it, but modify its physical address to match the
6967 LMA of the first section that fitted. */
6968 map
->p_paddr
= matching_lma
;
6972 /* None of the sections fitted inside the current segment.
6973 Change the current segment's physical address to match
6974 the LMA of the first section. */
6975 map
->p_paddr
= suggested_lma
;
6978 /* Offset the segment physical address from the lma
6979 to allow for space taken up by elf headers. */
6980 if (map
->includes_filehdr
)
6982 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6983 map
->p_paddr
-= iehdr
->e_ehsize
;
6986 map
->includes_filehdr
= FALSE
;
6987 map
->includes_phdrs
= FALSE
;
6991 if (map
->includes_phdrs
)
6993 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6995 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6997 /* iehdr->e_phnum is just an estimate of the number
6998 of program headers that we will need. Make a note
6999 here of the number we used and the segment we chose
7000 to hold these headers, so that we can adjust the
7001 offset when we know the correct value. */
7002 phdr_adjust_num
= iehdr
->e_phnum
;
7003 phdr_adjust_seg
= map
;
7006 map
->includes_phdrs
= FALSE
;
7010 /* Step Three: Loop over the sections again, this time assigning
7011 those that fit to the current segment and removing them from the
7012 sections array; but making sure not to leave large gaps. Once all
7013 possible sections have been assigned to the current segment it is
7014 added to the list of built segments and if sections still remain
7015 to be assigned, a new segment is constructed before repeating
7022 first_suggested_lma
= TRUE
;
7024 /* Fill the current segment with sections that fit. */
7025 for (j
= 0; j
< section_count
; j
++)
7027 section
= sections
[j
];
7029 if (section
== NULL
)
7032 output_section
= section
->output_section
;
7034 BFD_ASSERT (output_section
!= NULL
);
7036 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
7037 || IS_COREFILE_NOTE (segment
, section
))
7039 if (map
->count
== 0)
7041 /* If the first section in a segment does not start at
7042 the beginning of the segment, then something is
7044 if (output_section
->lma
7046 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
7047 + (map
->includes_phdrs
7048 ? iehdr
->e_phnum
* iehdr
->e_phentsize
7056 prev_sec
= map
->sections
[map
->count
- 1];
7058 /* If the gap between the end of the previous section
7059 and the start of this section is more than
7060 maxpagesize then we need to start a new segment. */
7061 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
7063 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
7064 || (prev_sec
->lma
+ prev_sec
->size
7065 > output_section
->lma
))
7067 if (first_suggested_lma
)
7069 suggested_lma
= output_section
->lma
;
7070 first_suggested_lma
= FALSE
;
7077 map
->sections
[map
->count
++] = output_section
;
7080 section
->segment_mark
= TRUE
;
7082 else if (first_suggested_lma
)
7084 suggested_lma
= output_section
->lma
;
7085 first_suggested_lma
= FALSE
;
7089 BFD_ASSERT (map
->count
> 0);
7091 /* Add the current segment to the list of built segments. */
7092 *pointer_to_map
= map
;
7093 pointer_to_map
= &map
->next
;
7095 if (isec
< section_count
)
7097 /* We still have not allocated all of the sections to
7098 segments. Create a new segment here, initialise it
7099 and carry on looping. */
7100 amt
= sizeof (struct elf_segment_map
);
7101 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
7102 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7109 /* Initialise the fields of the segment map. Set the physical
7110 physical address to the LMA of the first section that has
7111 not yet been assigned. */
7113 map
->p_type
= segment
->p_type
;
7114 map
->p_flags
= segment
->p_flags
;
7115 map
->p_flags_valid
= 1;
7116 map
->p_paddr
= suggested_lma
;
7117 map
->p_paddr_valid
= p_paddr_valid
;
7118 map
->includes_filehdr
= 0;
7119 map
->includes_phdrs
= 0;
7122 while (isec
< section_count
);
7127 elf_seg_map (obfd
) = map_first
;
7129 /* If we had to estimate the number of program headers that were
7130 going to be needed, then check our estimate now and adjust
7131 the offset if necessary. */
7132 if (phdr_adjust_seg
!= NULL
)
7136 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
7139 if (count
> phdr_adjust_num
)
7140 phdr_adjust_seg
->p_paddr
7141 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
7146 #undef IS_CONTAINED_BY_VMA
7147 #undef IS_CONTAINED_BY_LMA
7149 #undef IS_COREFILE_NOTE
7150 #undef IS_SOLARIS_PT_INTERP
7151 #undef IS_SECTION_IN_INPUT_SEGMENT
7152 #undef INCLUDE_SECTION_IN_SEGMENT
7153 #undef SEGMENT_AFTER_SEGMENT
7154 #undef SEGMENT_OVERLAPS
7158 /* Copy ELF program header information. */
7161 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
7163 Elf_Internal_Ehdr
*iehdr
;
7164 struct elf_segment_map
*map
;
7165 struct elf_segment_map
*map_first
;
7166 struct elf_segment_map
**pointer_to_map
;
7167 Elf_Internal_Phdr
*segment
;
7169 unsigned int num_segments
;
7170 bfd_boolean phdr_included
= FALSE
;
7171 bfd_boolean p_paddr_valid
;
7173 iehdr
= elf_elfheader (ibfd
);
7176 pointer_to_map
= &map_first
;
7178 /* If all the segment p_paddr fields are zero, don't set
7179 map->p_paddr_valid. */
7180 p_paddr_valid
= FALSE
;
7181 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7182 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7185 if (segment
->p_paddr
!= 0)
7187 p_paddr_valid
= TRUE
;
7191 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7196 unsigned int section_count
;
7198 Elf_Internal_Shdr
*this_hdr
;
7199 asection
*first_section
= NULL
;
7200 asection
*lowest_section
;
7202 /* Compute how many sections are in this segment. */
7203 for (section
= ibfd
->sections
, section_count
= 0;
7205 section
= section
->next
)
7207 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7208 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7210 if (first_section
== NULL
)
7211 first_section
= section
;
7216 /* Allocate a segment map big enough to contain
7217 all of the sections we have selected. */
7218 amt
= sizeof (struct elf_segment_map
);
7219 if (section_count
!= 0)
7220 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
7221 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
7225 /* Initialize the fields of the output segment map with the
7228 map
->p_type
= segment
->p_type
;
7229 map
->p_flags
= segment
->p_flags
;
7230 map
->p_flags_valid
= 1;
7231 map
->p_paddr
= segment
->p_paddr
;
7232 map
->p_paddr_valid
= p_paddr_valid
;
7233 map
->p_align
= segment
->p_align
;
7234 map
->p_align_valid
= 1;
7235 map
->p_vaddr_offset
= 0;
7237 if (map
->p_type
== PT_GNU_RELRO
7238 || map
->p_type
== PT_GNU_STACK
)
7240 /* The PT_GNU_RELRO segment may contain the first a few
7241 bytes in the .got.plt section even if the whole .got.plt
7242 section isn't in the PT_GNU_RELRO segment. We won't
7243 change the size of the PT_GNU_RELRO segment.
7244 Similarly, PT_GNU_STACK size is significant on uclinux
7246 map
->p_size
= segment
->p_memsz
;
7247 map
->p_size_valid
= 1;
7250 /* Determine if this segment contains the ELF file header
7251 and if it contains the program headers themselves. */
7252 map
->includes_filehdr
= (segment
->p_offset
== 0
7253 && segment
->p_filesz
>= iehdr
->e_ehsize
);
7255 map
->includes_phdrs
= 0;
7256 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
7258 map
->includes_phdrs
=
7259 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
7260 && (segment
->p_offset
+ segment
->p_filesz
7261 >= ((bfd_vma
) iehdr
->e_phoff
7262 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
7264 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
7265 phdr_included
= TRUE
;
7268 lowest_section
= NULL
;
7269 if (section_count
!= 0)
7271 unsigned int isec
= 0;
7273 for (section
= first_section
;
7275 section
= section
->next
)
7277 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7278 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7280 map
->sections
[isec
++] = section
->output_section
;
7281 if ((section
->flags
& SEC_ALLOC
) != 0)
7285 if (lowest_section
== NULL
7286 || section
->lma
< lowest_section
->lma
)
7287 lowest_section
= section
;
7289 /* Section lmas are set up from PT_LOAD header
7290 p_paddr in _bfd_elf_make_section_from_shdr.
7291 If this header has a p_paddr that disagrees
7292 with the section lma, flag the p_paddr as
7294 if ((section
->flags
& SEC_LOAD
) != 0)
7295 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
7297 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
7298 if (section
->lma
- segment
->p_paddr
!= seg_off
)
7299 map
->p_paddr_valid
= FALSE
;
7301 if (isec
== section_count
)
7307 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
7308 /* We need to keep the space used by the headers fixed. */
7309 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
7311 if (!map
->includes_phdrs
7312 && !map
->includes_filehdr
7313 && map
->p_paddr_valid
)
7314 /* There is some other padding before the first section. */
7315 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
7316 - segment
->p_paddr
);
7318 map
->count
= section_count
;
7319 *pointer_to_map
= map
;
7320 pointer_to_map
= &map
->next
;
7323 elf_seg_map (obfd
) = map_first
;
7327 /* Copy private BFD data. This copies or rewrites ELF program header
7331 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
7333 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7334 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7337 if (elf_tdata (ibfd
)->phdr
== NULL
)
7340 if (ibfd
->xvec
== obfd
->xvec
)
7342 /* Check to see if any sections in the input BFD
7343 covered by ELF program header have changed. */
7344 Elf_Internal_Phdr
*segment
;
7345 asection
*section
, *osec
;
7346 unsigned int i
, num_segments
;
7347 Elf_Internal_Shdr
*this_hdr
;
7348 const struct elf_backend_data
*bed
;
7350 bed
= get_elf_backend_data (ibfd
);
7352 /* Regenerate the segment map if p_paddr is set to 0. */
7353 if (bed
->want_p_paddr_set_to_zero
)
7356 /* Initialize the segment mark field. */
7357 for (section
= obfd
->sections
; section
!= NULL
;
7358 section
= section
->next
)
7359 section
->segment_mark
= FALSE
;
7361 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7362 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7366 /* PR binutils/3535. The Solaris linker always sets the p_paddr
7367 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
7368 which severly confuses things, so always regenerate the segment
7369 map in this case. */
7370 if (segment
->p_paddr
== 0
7371 && segment
->p_memsz
== 0
7372 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
7375 for (section
= ibfd
->sections
;
7376 section
!= NULL
; section
= section
->next
)
7378 /* We mark the output section so that we know it comes
7379 from the input BFD. */
7380 osec
= section
->output_section
;
7382 osec
->segment_mark
= TRUE
;
7384 /* Check if this section is covered by the segment. */
7385 this_hdr
= &(elf_section_data(section
)->this_hdr
);
7386 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
7388 /* FIXME: Check if its output section is changed or
7389 removed. What else do we need to check? */
7391 || section
->flags
!= osec
->flags
7392 || section
->lma
!= osec
->lma
7393 || section
->vma
!= osec
->vma
7394 || section
->size
!= osec
->size
7395 || section
->rawsize
!= osec
->rawsize
7396 || section
->alignment_power
!= osec
->alignment_power
)
7402 /* Check to see if any output section do not come from the
7404 for (section
= obfd
->sections
; section
!= NULL
;
7405 section
= section
->next
)
7407 if (!section
->segment_mark
)
7410 section
->segment_mark
= FALSE
;
7413 return copy_elf_program_header (ibfd
, obfd
);
7417 if (ibfd
->xvec
== obfd
->xvec
)
7419 /* When rewriting program header, set the output maxpagesize to
7420 the maximum alignment of input PT_LOAD segments. */
7421 Elf_Internal_Phdr
*segment
;
7423 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
7424 bfd_vma maxpagesize
= 0;
7426 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
7429 if (segment
->p_type
== PT_LOAD
7430 && maxpagesize
< segment
->p_align
)
7432 /* PR 17512: file: f17299af. */
7433 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
7434 /* xgettext:c-format */
7435 _bfd_error_handler (_("%B: warning: segment alignment of %#Lx"
7437 ibfd
, segment
->p_align
);
7439 maxpagesize
= segment
->p_align
;
7442 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
7443 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
7446 return rewrite_elf_program_header (ibfd
, obfd
);
7449 /* Initialize private output section information from input section. */
7452 _bfd_elf_init_private_section_data (bfd
*ibfd
,
7456 struct bfd_link_info
*link_info
)
7459 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7460 bfd_boolean final_link
= (link_info
!= NULL
7461 && !bfd_link_relocatable (link_info
));
7463 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7464 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7467 BFD_ASSERT (elf_section_data (osec
) != NULL
);
7469 /* For objcopy and relocatable link, don't copy the output ELF
7470 section type from input if the output BFD section flags have been
7471 set to something different. For a final link allow some flags
7472 that the linker clears to differ. */
7473 if (elf_section_type (osec
) == SHT_NULL
7474 && (osec
->flags
== isec
->flags
7476 && ((osec
->flags
^ isec
->flags
)
7477 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
7478 elf_section_type (osec
) = elf_section_type (isec
);
7480 /* FIXME: Is this correct for all OS/PROC specific flags? */
7481 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7482 & (SHF_MASKOS
| SHF_MASKPROC
));
7484 /* Copy sh_info from input for mbind section. */
7485 if (elf_section_flags (isec
) & SHF_GNU_MBIND
)
7486 elf_section_data (osec
)->this_hdr
.sh_info
7487 = elf_section_data (isec
)->this_hdr
.sh_info
;
7489 /* Set things up for objcopy and relocatable link. The output
7490 SHT_GROUP section will have its elf_next_in_group pointing back
7491 to the input group members. Ignore linker created group section.
7492 See elfNN_ia64_object_p in elfxx-ia64.c. */
7493 if ((link_info
== NULL
7494 || !link_info
->resolve_section_groups
)
7495 && (elf_sec_group (isec
) == NULL
7496 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0))
7498 if (elf_section_flags (isec
) & SHF_GROUP
)
7499 elf_section_flags (osec
) |= SHF_GROUP
;
7500 elf_next_in_group (osec
) = elf_next_in_group (isec
);
7501 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
7504 /* If not decompress, preserve SHF_COMPRESSED. */
7505 if (!final_link
&& (ibfd
->flags
& BFD_DECOMPRESS
) == 0)
7506 elf_section_flags (osec
) |= (elf_section_flags (isec
)
7509 ihdr
= &elf_section_data (isec
)->this_hdr
;
7511 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7512 don't use the output section of the linked-to section since it
7513 may be NULL at this point. */
7514 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
7516 ohdr
= &elf_section_data (osec
)->this_hdr
;
7517 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
7518 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
7521 osec
->use_rela_p
= isec
->use_rela_p
;
7526 /* Copy private section information. This copies over the entsize
7527 field, and sometimes the info field. */
7530 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
7535 Elf_Internal_Shdr
*ihdr
, *ohdr
;
7537 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
7538 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7541 ihdr
= &elf_section_data (isec
)->this_hdr
;
7542 ohdr
= &elf_section_data (osec
)->this_hdr
;
7544 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
7546 if (ihdr
->sh_type
== SHT_SYMTAB
7547 || ihdr
->sh_type
== SHT_DYNSYM
7548 || ihdr
->sh_type
== SHT_GNU_verneed
7549 || ihdr
->sh_type
== SHT_GNU_verdef
)
7550 ohdr
->sh_info
= ihdr
->sh_info
;
7552 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
7556 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7557 necessary if we are removing either the SHT_GROUP section or any of
7558 the group member sections. DISCARDED is the value that a section's
7559 output_section has if the section will be discarded, NULL when this
7560 function is called from objcopy, bfd_abs_section_ptr when called
7564 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
7568 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
7569 if (elf_section_type (isec
) == SHT_GROUP
)
7571 asection
*first
= elf_next_in_group (isec
);
7572 asection
*s
= first
;
7573 bfd_size_type removed
= 0;
7577 /* If this member section is being output but the
7578 SHT_GROUP section is not, then clear the group info
7579 set up by _bfd_elf_copy_private_section_data. */
7580 if (s
->output_section
!= discarded
7581 && isec
->output_section
== discarded
)
7583 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
7584 elf_group_name (s
->output_section
) = NULL
;
7586 /* Conversely, if the member section is not being output
7587 but the SHT_GROUP section is, then adjust its size. */
7588 else if (s
->output_section
== discarded
7589 && isec
->output_section
!= discarded
)
7591 s
= elf_next_in_group (s
);
7597 if (discarded
!= NULL
)
7599 /* If we've been called for ld -r, then we need to
7600 adjust the input section size. This function may
7601 be called multiple times, so save the original
7603 if (isec
->rawsize
== 0)
7604 isec
->rawsize
= isec
->size
;
7605 isec
->size
= isec
->rawsize
- removed
;
7609 /* Adjust the output section size when called from
7611 isec
->output_section
->size
-= removed
;
7619 /* Copy private header information. */
7622 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7624 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7625 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7628 /* Copy over private BFD data if it has not already been copied.
7629 This must be done here, rather than in the copy_private_bfd_data
7630 entry point, because the latter is called after the section
7631 contents have been set, which means that the program headers have
7632 already been worked out. */
7633 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7635 if (! copy_private_bfd_data (ibfd
, obfd
))
7639 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7642 /* Copy private symbol information. If this symbol is in a section
7643 which we did not map into a BFD section, try to map the section
7644 index correctly. We use special macro definitions for the mapped
7645 section indices; these definitions are interpreted by the
7646 swap_out_syms function. */
7648 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7649 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7650 #define MAP_STRTAB (SHN_HIOS + 3)
7651 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7652 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7655 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7660 elf_symbol_type
*isym
, *osym
;
7662 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7663 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7666 isym
= elf_symbol_from (ibfd
, isymarg
);
7667 osym
= elf_symbol_from (obfd
, osymarg
);
7670 && isym
->internal_elf_sym
.st_shndx
!= 0
7672 && bfd_is_abs_section (isym
->symbol
.section
))
7676 shndx
= isym
->internal_elf_sym
.st_shndx
;
7677 if (shndx
== elf_onesymtab (ibfd
))
7678 shndx
= MAP_ONESYMTAB
;
7679 else if (shndx
== elf_dynsymtab (ibfd
))
7680 shndx
= MAP_DYNSYMTAB
;
7681 else if (shndx
== elf_strtab_sec (ibfd
))
7683 else if (shndx
== elf_shstrtab_sec (ibfd
))
7684 shndx
= MAP_SHSTRTAB
;
7685 else if (find_section_in_list (shndx
, elf_symtab_shndx_list (ibfd
)))
7686 shndx
= MAP_SYM_SHNDX
;
7687 osym
->internal_elf_sym
.st_shndx
= shndx
;
7693 /* Swap out the symbols. */
7696 swap_out_syms (bfd
*abfd
,
7697 struct elf_strtab_hash
**sttp
,
7700 const struct elf_backend_data
*bed
;
7703 struct elf_strtab_hash
*stt
;
7704 Elf_Internal_Shdr
*symtab_hdr
;
7705 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7706 Elf_Internal_Shdr
*symstrtab_hdr
;
7707 struct elf_sym_strtab
*symstrtab
;
7708 bfd_byte
*outbound_syms
;
7709 bfd_byte
*outbound_shndx
;
7710 unsigned long outbound_syms_index
;
7711 unsigned long outbound_shndx_index
;
7713 unsigned int num_locals
;
7715 bfd_boolean name_local_sections
;
7717 if (!elf_map_symbols (abfd
, &num_locals
))
7720 /* Dump out the symtabs. */
7721 stt
= _bfd_elf_strtab_init ();
7725 bed
= get_elf_backend_data (abfd
);
7726 symcount
= bfd_get_symcount (abfd
);
7727 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7728 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7729 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7730 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7731 symtab_hdr
->sh_info
= num_locals
+ 1;
7732 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7734 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7735 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7737 /* Allocate buffer to swap out the .strtab section. */
7738 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7739 * sizeof (*symstrtab
));
7740 if (symstrtab
== NULL
)
7742 _bfd_elf_strtab_free (stt
);
7746 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7747 bed
->s
->sizeof_sym
);
7748 if (outbound_syms
== NULL
)
7751 _bfd_elf_strtab_free (stt
);
7755 symtab_hdr
->contents
= outbound_syms
;
7756 outbound_syms_index
= 0;
7758 outbound_shndx
= NULL
;
7759 outbound_shndx_index
= 0;
7761 if (elf_symtab_shndx_list (abfd
))
7763 symtab_shndx_hdr
= & elf_symtab_shndx_list (abfd
)->hdr
;
7764 if (symtab_shndx_hdr
->sh_name
!= 0)
7766 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7767 outbound_shndx
= (bfd_byte
*)
7768 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7769 if (outbound_shndx
== NULL
)
7772 symtab_shndx_hdr
->contents
= outbound_shndx
;
7773 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7774 symtab_shndx_hdr
->sh_size
= amt
;
7775 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7776 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7778 /* FIXME: What about any other headers in the list ? */
7781 /* Now generate the data (for "contents"). */
7783 /* Fill in zeroth symbol and swap it out. */
7784 Elf_Internal_Sym sym
;
7790 sym
.st_shndx
= SHN_UNDEF
;
7791 sym
.st_target_internal
= 0;
7792 symstrtab
[0].sym
= sym
;
7793 symstrtab
[0].dest_index
= outbound_syms_index
;
7794 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7795 outbound_syms_index
++;
7796 if (outbound_shndx
!= NULL
)
7797 outbound_shndx_index
++;
7801 = (bed
->elf_backend_name_local_section_symbols
7802 && bed
->elf_backend_name_local_section_symbols (abfd
));
7804 syms
= bfd_get_outsymbols (abfd
);
7805 for (idx
= 0; idx
< symcount
;)
7807 Elf_Internal_Sym sym
;
7808 bfd_vma value
= syms
[idx
]->value
;
7809 elf_symbol_type
*type_ptr
;
7810 flagword flags
= syms
[idx
]->flags
;
7813 if (!name_local_sections
7814 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7816 /* Local section symbols have no name. */
7817 sym
.st_name
= (unsigned long) -1;
7821 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7822 to get the final offset for st_name. */
7824 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7826 if (sym
.st_name
== (unsigned long) -1)
7830 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7832 if ((flags
& BSF_SECTION_SYM
) == 0
7833 && bfd_is_com_section (syms
[idx
]->section
))
7835 /* ELF common symbols put the alignment into the `value' field,
7836 and the size into the `size' field. This is backwards from
7837 how BFD handles it, so reverse it here. */
7838 sym
.st_size
= value
;
7839 if (type_ptr
== NULL
7840 || type_ptr
->internal_elf_sym
.st_value
== 0)
7841 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7843 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7844 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7845 (abfd
, syms
[idx
]->section
);
7849 asection
*sec
= syms
[idx
]->section
;
7852 if (sec
->output_section
)
7854 value
+= sec
->output_offset
;
7855 sec
= sec
->output_section
;
7858 /* Don't add in the section vma for relocatable output. */
7859 if (! relocatable_p
)
7861 sym
.st_value
= value
;
7862 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7864 if (bfd_is_abs_section (sec
)
7866 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7868 /* This symbol is in a real ELF section which we did
7869 not create as a BFD section. Undo the mapping done
7870 by copy_private_symbol_data. */
7871 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7875 shndx
= elf_onesymtab (abfd
);
7878 shndx
= elf_dynsymtab (abfd
);
7881 shndx
= elf_strtab_sec (abfd
);
7884 shndx
= elf_shstrtab_sec (abfd
);
7887 if (elf_symtab_shndx_list (abfd
))
7888 shndx
= elf_symtab_shndx_list (abfd
)->ndx
;
7897 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7899 if (shndx
== SHN_BAD
)
7903 /* Writing this would be a hell of a lot easier if
7904 we had some decent documentation on bfd, and
7905 knew what to expect of the library, and what to
7906 demand of applications. For example, it
7907 appears that `objcopy' might not set the
7908 section of a symbol to be a section that is
7909 actually in the output file. */
7910 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7912 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7913 if (shndx
== SHN_BAD
)
7915 /* xgettext:c-format */
7916 _bfd_error_handler (_("\
7917 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7918 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7920 bfd_set_error (bfd_error_invalid_operation
);
7926 sym
.st_shndx
= shndx
;
7929 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7931 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7932 type
= STT_GNU_IFUNC
;
7933 else if ((flags
& BSF_FUNCTION
) != 0)
7935 else if ((flags
& BSF_OBJECT
) != 0)
7937 else if ((flags
& BSF_RELC
) != 0)
7939 else if ((flags
& BSF_SRELC
) != 0)
7944 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7947 /* Processor-specific types. */
7948 if (type_ptr
!= NULL
7949 && bed
->elf_backend_get_symbol_type
)
7950 type
= ((*bed
->elf_backend_get_symbol_type
)
7951 (&type_ptr
->internal_elf_sym
, type
));
7953 if (flags
& BSF_SECTION_SYM
)
7955 if (flags
& BSF_GLOBAL
)
7956 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7958 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7960 else if (bfd_is_com_section (syms
[idx
]->section
))
7962 if (type
!= STT_TLS
)
7964 if ((abfd
->flags
& BFD_CONVERT_ELF_COMMON
))
7965 type
= ((abfd
->flags
& BFD_USE_ELF_STT_COMMON
)
7966 ? STT_COMMON
: STT_OBJECT
);
7968 type
= ((flags
& BSF_ELF_COMMON
) != 0
7969 ? STT_COMMON
: STT_OBJECT
);
7971 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7973 else if (bfd_is_und_section (syms
[idx
]->section
))
7974 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7978 else if (flags
& BSF_FILE
)
7979 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7982 int bind
= STB_LOCAL
;
7984 if (flags
& BSF_LOCAL
)
7986 else if (flags
& BSF_GNU_UNIQUE
)
7987 bind
= STB_GNU_UNIQUE
;
7988 else if (flags
& BSF_WEAK
)
7990 else if (flags
& BSF_GLOBAL
)
7993 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7996 if (type_ptr
!= NULL
)
7998 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7999 sym
.st_target_internal
8000 = type_ptr
->internal_elf_sym
.st_target_internal
;
8005 sym
.st_target_internal
= 0;
8009 symstrtab
[idx
].sym
= sym
;
8010 symstrtab
[idx
].dest_index
= outbound_syms_index
;
8011 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
8013 outbound_syms_index
++;
8014 if (outbound_shndx
!= NULL
)
8015 outbound_shndx_index
++;
8018 /* Finalize the .strtab section. */
8019 _bfd_elf_strtab_finalize (stt
);
8021 /* Swap out the .strtab section. */
8022 for (idx
= 0; idx
<= symcount
; idx
++)
8024 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
8025 if (elfsym
->sym
.st_name
== (unsigned long) -1)
8026 elfsym
->sym
.st_name
= 0;
8028 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
8029 elfsym
->sym
.st_name
);
8030 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
8032 + (elfsym
->dest_index
8033 * bed
->s
->sizeof_sym
)),
8035 + (elfsym
->destshndx_index
8036 * sizeof (Elf_External_Sym_Shndx
))));
8041 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
8042 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
8043 symstrtab_hdr
->sh_flags
= bed
->elf_strtab_flags
;
8044 symstrtab_hdr
->sh_addr
= 0;
8045 symstrtab_hdr
->sh_entsize
= 0;
8046 symstrtab_hdr
->sh_link
= 0;
8047 symstrtab_hdr
->sh_info
= 0;
8048 symstrtab_hdr
->sh_addralign
= 1;
8053 /* Return the number of bytes required to hold the symtab vector.
8055 Note that we base it on the count plus 1, since we will null terminate
8056 the vector allocated based on this size. However, the ELF symbol table
8057 always has a dummy entry as symbol #0, so it ends up even. */
8060 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
8064 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
8066 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8067 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8069 symtab_size
-= sizeof (asymbol
*);
8075 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
8079 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
8081 if (elf_dynsymtab (abfd
) == 0)
8083 bfd_set_error (bfd_error_invalid_operation
);
8087 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
8088 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
8090 symtab_size
-= sizeof (asymbol
*);
8096 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
8099 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
8102 /* Canonicalize the relocs. */
8105 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
8112 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8114 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
8117 tblptr
= section
->relocation
;
8118 for (i
= 0; i
< section
->reloc_count
; i
++)
8119 *relptr
++ = tblptr
++;
8123 return section
->reloc_count
;
8127 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
8129 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8130 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
8133 bfd_get_symcount (abfd
) = symcount
;
8138 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
8139 asymbol
**allocation
)
8141 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8142 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
8145 bfd_get_dynamic_symcount (abfd
) = symcount
;
8149 /* Return the size required for the dynamic reloc entries. Any loadable
8150 section that was actually installed in the BFD, and has type SHT_REL
8151 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
8152 dynamic reloc section. */
8155 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
8160 if (elf_dynsymtab (abfd
) == 0)
8162 bfd_set_error (bfd_error_invalid_operation
);
8166 ret
= sizeof (arelent
*);
8167 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8168 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8169 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8170 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8171 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
8172 * sizeof (arelent
*));
8177 /* Canonicalize the dynamic relocation entries. Note that we return the
8178 dynamic relocations as a single block, although they are actually
8179 associated with particular sections; the interface, which was
8180 designed for SunOS style shared libraries, expects that there is only
8181 one set of dynamic relocs. Any loadable section that was actually
8182 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
8183 dynamic symbol table, is considered to be a dynamic reloc section. */
8186 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
8190 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8194 if (elf_dynsymtab (abfd
) == 0)
8196 bfd_set_error (bfd_error_invalid_operation
);
8200 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8202 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
8204 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
8205 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
8206 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
8211 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
8213 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
8215 for (i
= 0; i
< count
; i
++)
8226 /* Read in the version information. */
8229 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
8231 bfd_byte
*contents
= NULL
;
8232 unsigned int freeidx
= 0;
8234 if (elf_dynverref (abfd
) != 0)
8236 Elf_Internal_Shdr
*hdr
;
8237 Elf_External_Verneed
*everneed
;
8238 Elf_Internal_Verneed
*iverneed
;
8240 bfd_byte
*contents_end
;
8242 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
8244 if (hdr
->sh_info
== 0
8245 || hdr
->sh_info
> hdr
->sh_size
/ sizeof (Elf_External_Verneed
))
8247 error_return_bad_verref
:
8249 (_("%B: .gnu.version_r invalid entry"), abfd
);
8250 bfd_set_error (bfd_error_bad_value
);
8251 error_return_verref
:
8252 elf_tdata (abfd
)->verref
= NULL
;
8253 elf_tdata (abfd
)->cverrefs
= 0;
8257 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8258 if (contents
== NULL
)
8259 goto error_return_verref
;
8261 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8262 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8263 goto error_return_verref
;
8265 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
8266 bfd_alloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
8268 if (elf_tdata (abfd
)->verref
== NULL
)
8269 goto error_return_verref
;
8271 BFD_ASSERT (sizeof (Elf_External_Verneed
)
8272 == sizeof (Elf_External_Vernaux
));
8273 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
8274 everneed
= (Elf_External_Verneed
*) contents
;
8275 iverneed
= elf_tdata (abfd
)->verref
;
8276 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
8278 Elf_External_Vernaux
*evernaux
;
8279 Elf_Internal_Vernaux
*ivernaux
;
8282 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
8284 iverneed
->vn_bfd
= abfd
;
8286 iverneed
->vn_filename
=
8287 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8289 if (iverneed
->vn_filename
== NULL
)
8290 goto error_return_bad_verref
;
8292 if (iverneed
->vn_cnt
== 0)
8293 iverneed
->vn_auxptr
= NULL
;
8296 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
8297 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
8298 sizeof (Elf_Internal_Vernaux
));
8299 if (iverneed
->vn_auxptr
== NULL
)
8300 goto error_return_verref
;
8303 if (iverneed
->vn_aux
8304 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8305 goto error_return_bad_verref
;
8307 evernaux
= ((Elf_External_Vernaux
*)
8308 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
8309 ivernaux
= iverneed
->vn_auxptr
;
8310 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
8312 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
8314 ivernaux
->vna_nodename
=
8315 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8316 ivernaux
->vna_name
);
8317 if (ivernaux
->vna_nodename
== NULL
)
8318 goto error_return_bad_verref
;
8320 if (ivernaux
->vna_other
> freeidx
)
8321 freeidx
= ivernaux
->vna_other
;
8323 ivernaux
->vna_nextptr
= NULL
;
8324 if (ivernaux
->vna_next
== 0)
8326 iverneed
->vn_cnt
= j
+ 1;
8329 if (j
+ 1 < iverneed
->vn_cnt
)
8330 ivernaux
->vna_nextptr
= ivernaux
+ 1;
8332 if (ivernaux
->vna_next
8333 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
8334 goto error_return_bad_verref
;
8336 evernaux
= ((Elf_External_Vernaux
*)
8337 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
8340 iverneed
->vn_nextref
= NULL
;
8341 if (iverneed
->vn_next
== 0)
8343 if (i
+ 1 < hdr
->sh_info
)
8344 iverneed
->vn_nextref
= iverneed
+ 1;
8346 if (iverneed
->vn_next
8347 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
8348 goto error_return_bad_verref
;
8350 everneed
= ((Elf_External_Verneed
*)
8351 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
8353 elf_tdata (abfd
)->cverrefs
= i
;
8359 if (elf_dynverdef (abfd
) != 0)
8361 Elf_Internal_Shdr
*hdr
;
8362 Elf_External_Verdef
*everdef
;
8363 Elf_Internal_Verdef
*iverdef
;
8364 Elf_Internal_Verdef
*iverdefarr
;
8365 Elf_Internal_Verdef iverdefmem
;
8367 unsigned int maxidx
;
8368 bfd_byte
*contents_end_def
, *contents_end_aux
;
8370 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
8372 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
8374 error_return_bad_verdef
:
8376 (_("%B: .gnu.version_d invalid entry"), abfd
);
8377 bfd_set_error (bfd_error_bad_value
);
8378 error_return_verdef
:
8379 elf_tdata (abfd
)->verdef
= NULL
;
8380 elf_tdata (abfd
)->cverdefs
= 0;
8384 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
8385 if (contents
== NULL
)
8386 goto error_return_verdef
;
8387 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
8388 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
8389 goto error_return_verdef
;
8391 BFD_ASSERT (sizeof (Elf_External_Verdef
)
8392 >= sizeof (Elf_External_Verdaux
));
8393 contents_end_def
= contents
+ hdr
->sh_size
8394 - sizeof (Elf_External_Verdef
);
8395 contents_end_aux
= contents
+ hdr
->sh_size
8396 - sizeof (Elf_External_Verdaux
);
8398 /* We know the number of entries in the section but not the maximum
8399 index. Therefore we have to run through all entries and find
8401 everdef
= (Elf_External_Verdef
*) contents
;
8403 for (i
= 0; i
< hdr
->sh_info
; ++i
)
8405 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8407 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
8408 goto error_return_bad_verdef
;
8409 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
8410 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
8412 if (iverdefmem
.vd_next
== 0)
8415 if (iverdefmem
.vd_next
8416 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
8417 goto error_return_bad_verdef
;
8419 everdef
= ((Elf_External_Verdef
*)
8420 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
8423 if (default_imported_symver
)
8425 if (freeidx
> maxidx
)
8431 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8432 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
8433 if (elf_tdata (abfd
)->verdef
== NULL
)
8434 goto error_return_verdef
;
8436 elf_tdata (abfd
)->cverdefs
= maxidx
;
8438 everdef
= (Elf_External_Verdef
*) contents
;
8439 iverdefarr
= elf_tdata (abfd
)->verdef
;
8440 for (i
= 0; i
< hdr
->sh_info
; i
++)
8442 Elf_External_Verdaux
*everdaux
;
8443 Elf_Internal_Verdaux
*iverdaux
;
8446 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
8448 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
8449 goto error_return_bad_verdef
;
8451 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
8452 memcpy (iverdef
, &iverdefmem
, offsetof (Elf_Internal_Verdef
, vd_bfd
));
8454 iverdef
->vd_bfd
= abfd
;
8456 if (iverdef
->vd_cnt
== 0)
8457 iverdef
->vd_auxptr
= NULL
;
8460 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
8461 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
8462 sizeof (Elf_Internal_Verdaux
));
8463 if (iverdef
->vd_auxptr
== NULL
)
8464 goto error_return_verdef
;
8468 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
8469 goto error_return_bad_verdef
;
8471 everdaux
= ((Elf_External_Verdaux
*)
8472 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
8473 iverdaux
= iverdef
->vd_auxptr
;
8474 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
8476 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
8478 iverdaux
->vda_nodename
=
8479 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
8480 iverdaux
->vda_name
);
8481 if (iverdaux
->vda_nodename
== NULL
)
8482 goto error_return_bad_verdef
;
8484 iverdaux
->vda_nextptr
= NULL
;
8485 if (iverdaux
->vda_next
== 0)
8487 iverdef
->vd_cnt
= j
+ 1;
8490 if (j
+ 1 < iverdef
->vd_cnt
)
8491 iverdaux
->vda_nextptr
= iverdaux
+ 1;
8493 if (iverdaux
->vda_next
8494 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
8495 goto error_return_bad_verdef
;
8497 everdaux
= ((Elf_External_Verdaux
*)
8498 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
8501 iverdef
->vd_nodename
= NULL
;
8502 if (iverdef
->vd_cnt
)
8503 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
8505 iverdef
->vd_nextdef
= NULL
;
8506 if (iverdef
->vd_next
== 0)
8508 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
8509 iverdef
->vd_nextdef
= iverdef
+ 1;
8511 everdef
= ((Elf_External_Verdef
*)
8512 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
8518 else if (default_imported_symver
)
8525 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
8526 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
8527 if (elf_tdata (abfd
)->verdef
== NULL
)
8530 elf_tdata (abfd
)->cverdefs
= freeidx
;
8533 /* Create a default version based on the soname. */
8534 if (default_imported_symver
)
8536 Elf_Internal_Verdef
*iverdef
;
8537 Elf_Internal_Verdaux
*iverdaux
;
8539 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
8541 iverdef
->vd_version
= VER_DEF_CURRENT
;
8542 iverdef
->vd_flags
= 0;
8543 iverdef
->vd_ndx
= freeidx
;
8544 iverdef
->vd_cnt
= 1;
8546 iverdef
->vd_bfd
= abfd
;
8548 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
8549 if (iverdef
->vd_nodename
== NULL
)
8550 goto error_return_verdef
;
8551 iverdef
->vd_nextdef
= NULL
;
8552 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
8553 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
8554 if (iverdef
->vd_auxptr
== NULL
)
8555 goto error_return_verdef
;
8557 iverdaux
= iverdef
->vd_auxptr
;
8558 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
8564 if (contents
!= NULL
)
8570 _bfd_elf_make_empty_symbol (bfd
*abfd
)
8572 elf_symbol_type
*newsym
;
8574 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
8577 newsym
->symbol
.the_bfd
= abfd
;
8578 return &newsym
->symbol
;
8582 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
8586 bfd_symbol_info (symbol
, ret
);
8589 /* Return whether a symbol name implies a local symbol. Most targets
8590 use this function for the is_local_label_name entry point, but some
8594 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
8597 /* Normal local symbols start with ``.L''. */
8598 if (name
[0] == '.' && name
[1] == 'L')
8601 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8602 DWARF debugging symbols starting with ``..''. */
8603 if (name
[0] == '.' && name
[1] == '.')
8606 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8607 emitting DWARF debugging output. I suspect this is actually a
8608 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8609 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8610 underscore to be emitted on some ELF targets). For ease of use,
8611 we treat such symbols as local. */
8612 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8615 /* Treat assembler generated fake symbols, dollar local labels and
8616 forward-backward labels (aka local labels) as locals.
8617 These labels have the form:
8619 L0^A.* (fake symbols)
8621 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8623 Versions which start with .L will have already been matched above,
8624 so we only need to match the rest. */
8625 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8627 bfd_boolean ret
= FALSE
;
8631 for (p
= name
+ 2; (c
= *p
); p
++)
8633 if (c
== 1 || c
== 2)
8635 if (c
== 1 && p
== name
+ 2)
8636 /* A fake symbol. */
8639 /* FIXME: We are being paranoid here and treating symbols like
8640 L0^Bfoo as if there were non-local, on the grounds that the
8641 assembler will never generate them. But can any symbol
8642 containing an ASCII value in the range 1-31 ever be anything
8643 other than some kind of local ? */
8660 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8661 asymbol
*symbol ATTRIBUTE_UNUSED
)
8668 _bfd_elf_set_arch_mach (bfd
*abfd
,
8669 enum bfd_architecture arch
,
8670 unsigned long machine
)
8672 /* If this isn't the right architecture for this backend, and this
8673 isn't the generic backend, fail. */
8674 if (arch
!= get_elf_backend_data (abfd
)->arch
8675 && arch
!= bfd_arch_unknown
8676 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8679 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8682 /* Find the nearest line to a particular section and offset,
8683 for error reporting. */
8686 _bfd_elf_find_nearest_line (bfd
*abfd
,
8690 const char **filename_ptr
,
8691 const char **functionname_ptr
,
8692 unsigned int *line_ptr
,
8693 unsigned int *discriminator_ptr
)
8697 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8698 filename_ptr
, functionname_ptr
,
8699 line_ptr
, discriminator_ptr
,
8700 dwarf_debug_sections
, 0,
8701 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8702 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8703 filename_ptr
, functionname_ptr
,
8706 if (!*functionname_ptr
)
8707 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8708 *filename_ptr
? NULL
: filename_ptr
,
8713 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8714 &found
, filename_ptr
,
8715 functionname_ptr
, line_ptr
,
8716 &elf_tdata (abfd
)->line_info
))
8718 if (found
&& (*functionname_ptr
|| *line_ptr
))
8721 if (symbols
== NULL
)
8724 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8725 filename_ptr
, functionname_ptr
))
8732 /* Find the line for a symbol. */
8735 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8736 const char **filename_ptr
, unsigned int *line_ptr
)
8738 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8739 filename_ptr
, NULL
, line_ptr
, NULL
,
8740 dwarf_debug_sections
, 0,
8741 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8744 /* After a call to bfd_find_nearest_line, successive calls to
8745 bfd_find_inliner_info can be used to get source information about
8746 each level of function inlining that terminated at the address
8747 passed to bfd_find_nearest_line. Currently this is only supported
8748 for DWARF2 with appropriate DWARF3 extensions. */
8751 _bfd_elf_find_inliner_info (bfd
*abfd
,
8752 const char **filename_ptr
,
8753 const char **functionname_ptr
,
8754 unsigned int *line_ptr
)
8757 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8758 functionname_ptr
, line_ptr
,
8759 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8764 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8766 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8767 int ret
= bed
->s
->sizeof_ehdr
;
8769 if (!bfd_link_relocatable (info
))
8771 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8773 if (phdr_size
== (bfd_size_type
) -1)
8775 struct elf_segment_map
*m
;
8778 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8779 phdr_size
+= bed
->s
->sizeof_phdr
;
8782 phdr_size
= get_program_header_size (abfd
, info
);
8785 elf_program_header_size (abfd
) = phdr_size
;
8793 _bfd_elf_set_section_contents (bfd
*abfd
,
8795 const void *location
,
8797 bfd_size_type count
)
8799 Elf_Internal_Shdr
*hdr
;
8802 if (! abfd
->output_has_begun
8803 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8809 hdr
= &elf_section_data (section
)->this_hdr
;
8810 if (hdr
->sh_offset
== (file_ptr
) -1)
8812 /* We must compress this section. Write output to the buffer. */
8813 unsigned char *contents
= hdr
->contents
;
8814 if ((offset
+ count
) > hdr
->sh_size
8815 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8816 || contents
== NULL
)
8818 memcpy (contents
+ offset
, location
, count
);
8821 pos
= hdr
->sh_offset
+ offset
;
8822 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8823 || bfd_bwrite (location
, count
, abfd
) != count
)
8830 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8831 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8832 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8837 /* Try to convert a non-ELF reloc into an ELF one. */
8840 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8842 /* Check whether we really have an ELF howto. */
8844 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8846 bfd_reloc_code_real_type code
;
8847 reloc_howto_type
*howto
;
8849 /* Alien reloc: Try to determine its type to replace it with an
8850 equivalent ELF reloc. */
8852 if (areloc
->howto
->pc_relative
)
8854 switch (areloc
->howto
->bitsize
)
8857 code
= BFD_RELOC_8_PCREL
;
8860 code
= BFD_RELOC_12_PCREL
;
8863 code
= BFD_RELOC_16_PCREL
;
8866 code
= BFD_RELOC_24_PCREL
;
8869 code
= BFD_RELOC_32_PCREL
;
8872 code
= BFD_RELOC_64_PCREL
;
8878 howto
= bfd_reloc_type_lookup (abfd
, code
);
8880 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8882 if (howto
->pcrel_offset
)
8883 areloc
->addend
+= areloc
->address
;
8885 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8890 switch (areloc
->howto
->bitsize
)
8896 code
= BFD_RELOC_14
;
8899 code
= BFD_RELOC_16
;
8902 code
= BFD_RELOC_26
;
8905 code
= BFD_RELOC_32
;
8908 code
= BFD_RELOC_64
;
8914 howto
= bfd_reloc_type_lookup (abfd
, code
);
8918 areloc
->howto
= howto
;
8927 /* xgettext:c-format */
8928 (_("%B: unsupported relocation type %s"),
8929 abfd
, areloc
->howto
->name
);
8930 bfd_set_error (bfd_error_bad_value
);
8935 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8937 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8938 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8940 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8941 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8942 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8945 return _bfd_generic_close_and_cleanup (abfd
);
8948 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8949 in the relocation's offset. Thus we cannot allow any sort of sanity
8950 range-checking to interfere. There is nothing else to do in processing
8953 bfd_reloc_status_type
8954 _bfd_elf_rel_vtable_reloc_fn
8955 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8956 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8957 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8958 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8960 return bfd_reloc_ok
;
8963 /* Elf core file support. Much of this only works on native
8964 toolchains, since we rely on knowing the
8965 machine-dependent procfs structure in order to pick
8966 out details about the corefile. */
8968 #ifdef HAVE_SYS_PROCFS_H
8969 /* Needed for new procfs interface on sparc-solaris. */
8970 # define _STRUCTURED_PROC 1
8971 # include <sys/procfs.h>
8974 /* Return a PID that identifies a "thread" for threaded cores, or the
8975 PID of the main process for non-threaded cores. */
8978 elfcore_make_pid (bfd
*abfd
)
8982 pid
= elf_tdata (abfd
)->core
->lwpid
;
8984 pid
= elf_tdata (abfd
)->core
->pid
;
8989 /* If there isn't a section called NAME, make one, using
8990 data from SECT. Note, this function will generate a
8991 reference to NAME, so you shouldn't deallocate or
8995 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8999 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
9002 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
9006 sect2
->size
= sect
->size
;
9007 sect2
->filepos
= sect
->filepos
;
9008 sect2
->alignment_power
= sect
->alignment_power
;
9012 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
9013 actually creates up to two pseudosections:
9014 - For the single-threaded case, a section named NAME, unless
9015 such a section already exists.
9016 - For the multi-threaded case, a section named "NAME/PID", where
9017 PID is elfcore_make_pid (abfd).
9018 Both pseudosections have identical contents. */
9020 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
9026 char *threaded_name
;
9030 /* Build the section name. */
9032 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
9033 len
= strlen (buf
) + 1;
9034 threaded_name
= (char *) bfd_alloc (abfd
, len
);
9035 if (threaded_name
== NULL
)
9037 memcpy (threaded_name
, buf
, len
);
9039 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
9044 sect
->filepos
= filepos
;
9045 sect
->alignment_power
= 2;
9047 return elfcore_maybe_make_sect (abfd
, name
, sect
);
9050 /* prstatus_t exists on:
9052 linux 2.[01] + glibc
9056 #if defined (HAVE_PRSTATUS_T)
9059 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9064 if (note
->descsz
== sizeof (prstatus_t
))
9068 size
= sizeof (prstat
.pr_reg
);
9069 offset
= offsetof (prstatus_t
, pr_reg
);
9070 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9072 /* Do not overwrite the core signal if it
9073 has already been set by another thread. */
9074 if (elf_tdata (abfd
)->core
->signal
== 0)
9075 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9076 if (elf_tdata (abfd
)->core
->pid
== 0)
9077 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9079 /* pr_who exists on:
9082 pr_who doesn't exist on:
9085 #if defined (HAVE_PRSTATUS_T_PR_WHO)
9086 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9088 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9091 #if defined (HAVE_PRSTATUS32_T)
9092 else if (note
->descsz
== sizeof (prstatus32_t
))
9094 /* 64-bit host, 32-bit corefile */
9095 prstatus32_t prstat
;
9097 size
= sizeof (prstat
.pr_reg
);
9098 offset
= offsetof (prstatus32_t
, pr_reg
);
9099 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
9101 /* Do not overwrite the core signal if it
9102 has already been set by another thread. */
9103 if (elf_tdata (abfd
)->core
->signal
== 0)
9104 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
9105 if (elf_tdata (abfd
)->core
->pid
== 0)
9106 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
9108 /* pr_who exists on:
9111 pr_who doesn't exist on:
9114 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
9115 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
9117 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
9120 #endif /* HAVE_PRSTATUS32_T */
9123 /* Fail - we don't know how to handle any other
9124 note size (ie. data object type). */
9128 /* Make a ".reg/999" section and a ".reg" section. */
9129 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
9130 size
, note
->descpos
+ offset
);
9132 #endif /* defined (HAVE_PRSTATUS_T) */
9134 /* Create a pseudosection containing the exact contents of NOTE. */
9136 elfcore_make_note_pseudosection (bfd
*abfd
,
9138 Elf_Internal_Note
*note
)
9140 return _bfd_elfcore_make_pseudosection (abfd
, name
,
9141 note
->descsz
, note
->descpos
);
9144 /* There isn't a consistent prfpregset_t across platforms,
9145 but it doesn't matter, because we don't have to pick this
9146 data structure apart. */
9149 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9151 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9154 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
9155 type of NT_PRXFPREG. Just include the whole note's contents
9159 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9161 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9164 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
9165 with a note type of NT_X86_XSTATE. Just include the whole note's
9166 contents literally. */
9169 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
9171 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
9175 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
9177 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
9181 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
9183 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
9187 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
9189 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
9193 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
9195 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
9199 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
9201 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
9205 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
9207 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
9211 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
9213 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
9217 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
9219 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
9223 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9225 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
9229 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
9231 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
9235 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
9237 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
9241 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
9243 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
9247 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
9249 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
9253 elfcore_grok_s390_gs_cb (bfd
*abfd
, Elf_Internal_Note
*note
)
9255 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-cb", note
);
9259 elfcore_grok_s390_gs_bc (bfd
*abfd
, Elf_Internal_Note
*note
)
9261 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-gs-bc", note
);
9265 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
9267 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
9271 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
9273 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
9277 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
9279 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
9283 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
9285 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
9288 #if defined (HAVE_PRPSINFO_T)
9289 typedef prpsinfo_t elfcore_psinfo_t
;
9290 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
9291 typedef prpsinfo32_t elfcore_psinfo32_t
;
9295 #if defined (HAVE_PSINFO_T)
9296 typedef psinfo_t elfcore_psinfo_t
;
9297 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
9298 typedef psinfo32_t elfcore_psinfo32_t
;
9302 /* return a malloc'ed copy of a string at START which is at
9303 most MAX bytes long, possibly without a terminating '\0'.
9304 the copy will always have a terminating '\0'. */
9307 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
9310 char *end
= (char *) memchr (start
, '\0', max
);
9318 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
9322 memcpy (dups
, start
, len
);
9328 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9330 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9332 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
9334 elfcore_psinfo_t psinfo
;
9336 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9338 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
9339 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9341 elf_tdata (abfd
)->core
->program
9342 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9343 sizeof (psinfo
.pr_fname
));
9345 elf_tdata (abfd
)->core
->command
9346 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9347 sizeof (psinfo
.pr_psargs
));
9349 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9350 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
9352 /* 64-bit host, 32-bit corefile */
9353 elfcore_psinfo32_t psinfo
;
9355 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
9357 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
9358 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
9360 elf_tdata (abfd
)->core
->program
9361 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
9362 sizeof (psinfo
.pr_fname
));
9364 elf_tdata (abfd
)->core
->command
9365 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
9366 sizeof (psinfo
.pr_psargs
));
9372 /* Fail - we don't know how to handle any other
9373 note size (ie. data object type). */
9377 /* Note that for some reason, a spurious space is tacked
9378 onto the end of the args in some (at least one anyway)
9379 implementations, so strip it off if it exists. */
9382 char *command
= elf_tdata (abfd
)->core
->command
;
9383 int n
= strlen (command
);
9385 if (0 < n
&& command
[n
- 1] == ' ')
9386 command
[n
- 1] = '\0';
9391 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
9393 #if defined (HAVE_PSTATUS_T)
9395 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9397 if (note
->descsz
== sizeof (pstatus_t
)
9398 #if defined (HAVE_PXSTATUS_T)
9399 || note
->descsz
== sizeof (pxstatus_t
)
9405 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9407 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9409 #if defined (HAVE_PSTATUS32_T)
9410 else if (note
->descsz
== sizeof (pstatus32_t
))
9412 /* 64-bit host, 32-bit corefile */
9415 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
9417 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
9420 /* Could grab some more details from the "representative"
9421 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
9422 NT_LWPSTATUS note, presumably. */
9426 #endif /* defined (HAVE_PSTATUS_T) */
9428 #if defined (HAVE_LWPSTATUS_T)
9430 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9432 lwpstatus_t lwpstat
;
9438 if (note
->descsz
!= sizeof (lwpstat
)
9439 #if defined (HAVE_LWPXSTATUS_T)
9440 && note
->descsz
!= sizeof (lwpxstatus_t
)
9445 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
9447 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
9448 /* Do not overwrite the core signal if it has already been set by
9450 if (elf_tdata (abfd
)->core
->signal
== 0)
9451 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
9453 /* Make a ".reg/999" section. */
9455 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
9456 len
= strlen (buf
) + 1;
9457 name
= bfd_alloc (abfd
, len
);
9460 memcpy (name
, buf
, len
);
9462 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9466 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9467 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
9468 sect
->filepos
= note
->descpos
9469 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
9472 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9473 sect
->size
= sizeof (lwpstat
.pr_reg
);
9474 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
9477 sect
->alignment_power
= 2;
9479 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9482 /* Make a ".reg2/999" section */
9484 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
9485 len
= strlen (buf
) + 1;
9486 name
= bfd_alloc (abfd
, len
);
9489 memcpy (name
, buf
, len
);
9491 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9495 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9496 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
9497 sect
->filepos
= note
->descpos
9498 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
9501 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
9502 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
9503 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
9506 sect
->alignment_power
= 2;
9508 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
9510 #endif /* defined (HAVE_LWPSTATUS_T) */
9513 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9520 int is_active_thread
;
9523 if (note
->descsz
< 728)
9526 if (! CONST_STRNEQ (note
->namedata
, "win32"))
9529 type
= bfd_get_32 (abfd
, note
->descdata
);
9533 case 1 /* NOTE_INFO_PROCESS */:
9534 /* FIXME: need to add ->core->command. */
9535 /* process_info.pid */
9536 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9537 /* process_info.signal */
9538 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
9541 case 2 /* NOTE_INFO_THREAD */:
9542 /* Make a ".reg/999" section. */
9543 /* thread_info.tid */
9544 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
9546 len
= strlen (buf
) + 1;
9547 name
= (char *) bfd_alloc (abfd
, len
);
9551 memcpy (name
, buf
, len
);
9553 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9557 /* sizeof (thread_info.thread_context) */
9559 /* offsetof (thread_info.thread_context) */
9560 sect
->filepos
= note
->descpos
+ 12;
9561 sect
->alignment_power
= 2;
9563 /* thread_info.is_active_thread */
9564 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
9566 if (is_active_thread
)
9567 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
9571 case 3 /* NOTE_INFO_MODULE */:
9572 /* Make a ".module/xxxxxxxx" section. */
9573 /* module_info.base_address */
9574 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
9575 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
9577 len
= strlen (buf
) + 1;
9578 name
= (char *) bfd_alloc (abfd
, len
);
9582 memcpy (name
, buf
, len
);
9584 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9589 sect
->size
= note
->descsz
;
9590 sect
->filepos
= note
->descpos
;
9591 sect
->alignment_power
= 2;
9602 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9604 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9612 if (bed
->elf_backend_grok_prstatus
)
9613 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9615 #if defined (HAVE_PRSTATUS_T)
9616 return elfcore_grok_prstatus (abfd
, note
);
9621 #if defined (HAVE_PSTATUS_T)
9623 return elfcore_grok_pstatus (abfd
, note
);
9626 #if defined (HAVE_LWPSTATUS_T)
9628 return elfcore_grok_lwpstatus (abfd
, note
);
9631 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9632 return elfcore_grok_prfpreg (abfd
, note
);
9634 case NT_WIN32PSTATUS
:
9635 return elfcore_grok_win32pstatus (abfd
, note
);
9637 case NT_PRXFPREG
: /* Linux SSE extension */
9638 if (note
->namesz
== 6
9639 && strcmp (note
->namedata
, "LINUX") == 0)
9640 return elfcore_grok_prxfpreg (abfd
, note
);
9644 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9645 if (note
->namesz
== 6
9646 && strcmp (note
->namedata
, "LINUX") == 0)
9647 return elfcore_grok_xstatereg (abfd
, note
);
9652 if (note
->namesz
== 6
9653 && strcmp (note
->namedata
, "LINUX") == 0)
9654 return elfcore_grok_ppc_vmx (abfd
, note
);
9659 if (note
->namesz
== 6
9660 && strcmp (note
->namedata
, "LINUX") == 0)
9661 return elfcore_grok_ppc_vsx (abfd
, note
);
9665 case NT_S390_HIGH_GPRS
:
9666 if (note
->namesz
== 6
9667 && strcmp (note
->namedata
, "LINUX") == 0)
9668 return elfcore_grok_s390_high_gprs (abfd
, note
);
9673 if (note
->namesz
== 6
9674 && strcmp (note
->namedata
, "LINUX") == 0)
9675 return elfcore_grok_s390_timer (abfd
, note
);
9679 case NT_S390_TODCMP
:
9680 if (note
->namesz
== 6
9681 && strcmp (note
->namedata
, "LINUX") == 0)
9682 return elfcore_grok_s390_todcmp (abfd
, note
);
9686 case NT_S390_TODPREG
:
9687 if (note
->namesz
== 6
9688 && strcmp (note
->namedata
, "LINUX") == 0)
9689 return elfcore_grok_s390_todpreg (abfd
, note
);
9694 if (note
->namesz
== 6
9695 && strcmp (note
->namedata
, "LINUX") == 0)
9696 return elfcore_grok_s390_ctrs (abfd
, note
);
9700 case NT_S390_PREFIX
:
9701 if (note
->namesz
== 6
9702 && strcmp (note
->namedata
, "LINUX") == 0)
9703 return elfcore_grok_s390_prefix (abfd
, note
);
9707 case NT_S390_LAST_BREAK
:
9708 if (note
->namesz
== 6
9709 && strcmp (note
->namedata
, "LINUX") == 0)
9710 return elfcore_grok_s390_last_break (abfd
, note
);
9714 case NT_S390_SYSTEM_CALL
:
9715 if (note
->namesz
== 6
9716 && strcmp (note
->namedata
, "LINUX") == 0)
9717 return elfcore_grok_s390_system_call (abfd
, note
);
9722 if (note
->namesz
== 6
9723 && strcmp (note
->namedata
, "LINUX") == 0)
9724 return elfcore_grok_s390_tdb (abfd
, note
);
9728 case NT_S390_VXRS_LOW
:
9729 if (note
->namesz
== 6
9730 && strcmp (note
->namedata
, "LINUX") == 0)
9731 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9735 case NT_S390_VXRS_HIGH
:
9736 if (note
->namesz
== 6
9737 && strcmp (note
->namedata
, "LINUX") == 0)
9738 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9743 if (note
->namesz
== 6
9744 && strcmp (note
->namedata
, "LINUX") == 0)
9745 return elfcore_grok_s390_gs_cb (abfd
, note
);
9750 if (note
->namesz
== 6
9751 && strcmp (note
->namedata
, "LINUX") == 0)
9752 return elfcore_grok_s390_gs_bc (abfd
, note
);
9757 if (note
->namesz
== 6
9758 && strcmp (note
->namedata
, "LINUX") == 0)
9759 return elfcore_grok_arm_vfp (abfd
, note
);
9764 if (note
->namesz
== 6
9765 && strcmp (note
->namedata
, "LINUX") == 0)
9766 return elfcore_grok_aarch_tls (abfd
, note
);
9770 case NT_ARM_HW_BREAK
:
9771 if (note
->namesz
== 6
9772 && strcmp (note
->namedata
, "LINUX") == 0)
9773 return elfcore_grok_aarch_hw_break (abfd
, note
);
9777 case NT_ARM_HW_WATCH
:
9778 if (note
->namesz
== 6
9779 && strcmp (note
->namedata
, "LINUX") == 0)
9780 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9786 if (bed
->elf_backend_grok_psinfo
)
9787 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9789 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9790 return elfcore_grok_psinfo (abfd
, note
);
9797 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9802 sect
->size
= note
->descsz
;
9803 sect
->filepos
= note
->descpos
;
9804 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9810 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9814 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9821 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9823 struct bfd_build_id
* build_id
;
9825 if (note
->descsz
== 0)
9828 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9829 if (build_id
== NULL
)
9832 build_id
->size
= note
->descsz
;
9833 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9834 abfd
->build_id
= build_id
;
9840 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9847 case NT_GNU_PROPERTY_TYPE_0
:
9848 return _bfd_elf_parse_gnu_properties (abfd
, note
);
9850 case NT_GNU_BUILD_ID
:
9851 return elfobj_grok_gnu_build_id (abfd
, note
);
9856 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9858 struct sdt_note
*cur
=
9859 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9862 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9863 cur
->size
= (bfd_size_type
) note
->descsz
;
9864 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9866 elf_tdata (abfd
)->sdt_note_head
= cur
;
9872 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9877 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9885 elfcore_grok_freebsd_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9889 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
9892 if (note
->descsz
< 108)
9897 if (note
->descsz
< 120)
9905 /* Check for version 1 in pr_version. */
9906 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9911 /* Skip over pr_psinfosz. */
9912 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
9916 offset
+= 4; /* Padding before pr_psinfosz. */
9920 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */
9921 elf_tdata (abfd
)->core
->program
9922 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 17);
9925 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */
9926 elf_tdata (abfd
)->core
->command
9927 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ offset
, 81);
9930 /* Padding before pr_pid. */
9933 /* The pr_pid field was added in version "1a". */
9934 if (note
->descsz
< offset
+ 4)
9937 elf_tdata (abfd
)->core
->pid
9938 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9944 elfcore_grok_freebsd_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
9950 /* Compute offset of pr_getregsz, skipping over pr_statussz.
9951 Also compute minimum size of this note. */
9952 switch (elf_elfheader (abfd
)->e_ident
[EI_CLASS
])
9956 min_size
= offset
+ (4 * 2) + 4 + 4 + 4;
9960 offset
= 4 + 4 + 8; /* Includes padding before pr_statussz. */
9961 min_size
= offset
+ (8 * 2) + 4 + 4 + 4 + 4;
9968 if (note
->descsz
< min_size
)
9971 /* Check for version 1 in pr_version. */
9972 if (bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
) != 1)
9975 /* Extract size of pr_reg from pr_gregsetsz. */
9976 /* Skip over pr_gregsetsz and pr_fpregsetsz. */
9977 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS32
)
9979 size
= bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9984 size
= bfd_h_get_64 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9988 /* Skip over pr_osreldate. */
9991 /* Read signal from pr_cursig. */
9992 if (elf_tdata (abfd
)->core
->signal
== 0)
9993 elf_tdata (abfd
)->core
->signal
9994 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
9997 /* Read TID from pr_pid. */
9998 elf_tdata (abfd
)->core
->lwpid
9999 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ offset
);
10002 /* Padding before pr_reg. */
10003 if (elf_elfheader (abfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
)
10006 /* Make sure that there is enough data remaining in the note. */
10007 if ((note
->descsz
- offset
) < size
)
10010 /* Make a ".reg/999" section and a ".reg" section. */
10011 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
10012 size
, note
->descpos
+ offset
);
10016 elfcore_grok_freebsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10018 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10020 switch (note
->type
)
10023 if (bed
->elf_backend_grok_freebsd_prstatus
)
10024 if ((*bed
->elf_backend_grok_freebsd_prstatus
) (abfd
, note
))
10026 return elfcore_grok_freebsd_prstatus (abfd
, note
);
10029 return elfcore_grok_prfpreg (abfd
, note
);
10032 return elfcore_grok_freebsd_psinfo (abfd
, note
);
10034 case NT_FREEBSD_THRMISC
:
10035 if (note
->namesz
== 8)
10036 return elfcore_make_note_pseudosection (abfd
, ".thrmisc", note
);
10040 case NT_FREEBSD_PROCSTAT_PROC
:
10041 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.proc",
10044 case NT_FREEBSD_PROCSTAT_FILES
:
10045 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.files",
10048 case NT_FREEBSD_PROCSTAT_VMMAP
:
10049 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.vmmap",
10052 case NT_FREEBSD_PROCSTAT_AUXV
:
10054 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10059 sect
->size
= note
->descsz
- 4;
10060 sect
->filepos
= note
->descpos
+ 4;
10061 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10066 case NT_X86_XSTATE
:
10067 if (note
->namesz
== 8)
10068 return elfcore_grok_xstatereg (abfd
, note
);
10072 case NT_FREEBSD_PTLWPINFO
:
10073 return elfcore_make_note_pseudosection (abfd
, ".note.freebsdcore.lwpinfo",
10077 return elfcore_grok_arm_vfp (abfd
, note
);
10085 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
10089 cp
= strchr (note
->namedata
, '@');
10092 *lwpidp
= atoi(cp
+ 1);
10099 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10101 if (note
->descsz
<= 0x7c + 31)
10104 /* Signal number at offset 0x08. */
10105 elf_tdata (abfd
)->core
->signal
10106 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10108 /* Process ID at offset 0x50. */
10109 elf_tdata (abfd
)->core
->pid
10110 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
10112 /* Command name at 0x7c (max 32 bytes, including nul). */
10113 elf_tdata (abfd
)->core
->command
10114 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
10116 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
10121 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10125 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
10126 elf_tdata (abfd
)->core
->lwpid
= lwp
;
10128 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
10130 /* NetBSD-specific core "procinfo". Note that we expect to
10131 find this note before any of the others, which is fine,
10132 since the kernel writes this note out first when it
10133 creates a core file. */
10135 return elfcore_grok_netbsd_procinfo (abfd
, note
);
10138 /* As of Jan 2002 there are no other machine-independent notes
10139 defined for NetBSD core files. If the note type is less
10140 than the start of the machine-dependent note types, we don't
10143 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
10147 switch (bfd_get_arch (abfd
))
10149 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
10150 PT_GETFPREGS == mach+2. */
10152 case bfd_arch_alpha
:
10153 case bfd_arch_sparc
:
10154 switch (note
->type
)
10156 case NT_NETBSDCORE_FIRSTMACH
+0:
10157 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10159 case NT_NETBSDCORE_FIRSTMACH
+2:
10160 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10166 /* On all other arch's, PT_GETREGS == mach+1 and
10167 PT_GETFPREGS == mach+3. */
10170 switch (note
->type
)
10172 case NT_NETBSDCORE_FIRSTMACH
+1:
10173 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10175 case NT_NETBSDCORE_FIRSTMACH
+3:
10176 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10186 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
10188 if (note
->descsz
<= 0x48 + 31)
10191 /* Signal number at offset 0x08. */
10192 elf_tdata (abfd
)->core
->signal
10193 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
10195 /* Process ID at offset 0x20. */
10196 elf_tdata (abfd
)->core
->pid
10197 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
10199 /* Command name at 0x48 (max 32 bytes, including nul). */
10200 elf_tdata (abfd
)->core
->command
10201 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
10207 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10209 if (note
->type
== NT_OPENBSD_PROCINFO
)
10210 return elfcore_grok_openbsd_procinfo (abfd
, note
);
10212 if (note
->type
== NT_OPENBSD_REGS
)
10213 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
10215 if (note
->type
== NT_OPENBSD_FPREGS
)
10216 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
10218 if (note
->type
== NT_OPENBSD_XFPREGS
)
10219 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
10221 if (note
->type
== NT_OPENBSD_AUXV
)
10223 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
10228 sect
->size
= note
->descsz
;
10229 sect
->filepos
= note
->descpos
;
10230 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10235 if (note
->type
== NT_OPENBSD_WCOOKIE
)
10237 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
10242 sect
->size
= note
->descsz
;
10243 sect
->filepos
= note
->descpos
;
10244 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
10253 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
10255 void *ddata
= note
->descdata
;
10262 if (note
->descsz
< 16)
10265 /* nto_procfs_status 'pid' field is at offset 0. */
10266 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
10268 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
10269 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
10271 /* nto_procfs_status 'flags' field is at offset 8. */
10272 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
10274 /* nto_procfs_status 'what' field is at offset 14. */
10275 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
10277 elf_tdata (abfd
)->core
->signal
= sig
;
10278 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10281 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
10282 do not come from signals so we make sure we set the current
10283 thread just in case. */
10284 if (flags
& 0x00000080)
10285 elf_tdata (abfd
)->core
->lwpid
= *tid
;
10287 /* Make a ".qnx_core_status/%d" section. */
10288 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
10290 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10293 strcpy (name
, buf
);
10295 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10299 sect
->size
= note
->descsz
;
10300 sect
->filepos
= note
->descpos
;
10301 sect
->alignment_power
= 2;
10303 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
10307 elfcore_grok_nto_regs (bfd
*abfd
,
10308 Elf_Internal_Note
*note
,
10316 /* Make a "(base)/%d" section. */
10317 sprintf (buf
, "%s/%ld", base
, tid
);
10319 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
10322 strcpy (name
, buf
);
10324 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10328 sect
->size
= note
->descsz
;
10329 sect
->filepos
= note
->descpos
;
10330 sect
->alignment_power
= 2;
10332 /* This is the current thread. */
10333 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
10334 return elfcore_maybe_make_sect (abfd
, base
, sect
);
10339 #define BFD_QNT_CORE_INFO 7
10340 #define BFD_QNT_CORE_STATUS 8
10341 #define BFD_QNT_CORE_GREG 9
10342 #define BFD_QNT_CORE_FPREG 10
10345 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10347 /* Every GREG section has a STATUS section before it. Store the
10348 tid from the previous call to pass down to the next gregs
10350 static long tid
= 1;
10352 switch (note
->type
)
10354 case BFD_QNT_CORE_INFO
:
10355 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
10356 case BFD_QNT_CORE_STATUS
:
10357 return elfcore_grok_nto_status (abfd
, note
, &tid
);
10358 case BFD_QNT_CORE_GREG
:
10359 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
10360 case BFD_QNT_CORE_FPREG
:
10361 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
10368 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
10374 /* Use note name as section name. */
10375 len
= note
->namesz
;
10376 name
= (char *) bfd_alloc (abfd
, len
);
10379 memcpy (name
, note
->namedata
, len
);
10380 name
[len
- 1] = '\0';
10382 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
10386 sect
->size
= note
->descsz
;
10387 sect
->filepos
= note
->descpos
;
10388 sect
->alignment_power
= 1;
10393 /* Function: elfcore_write_note
10396 buffer to hold note, and current size of buffer
10400 size of data for note
10402 Writes note to end of buffer. ELF64 notes are written exactly as
10403 for ELF32, despite the current (as of 2006) ELF gabi specifying
10404 that they ought to have 8-byte namesz and descsz field, and have
10405 8-byte alignment. Other writers, eg. Linux kernel, do the same.
10408 Pointer to realloc'd buffer, *BUFSIZ updated. */
10411 elfcore_write_note (bfd
*abfd
,
10419 Elf_External_Note
*xnp
;
10426 namesz
= strlen (name
) + 1;
10428 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
10430 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
10433 dest
= buf
+ *bufsiz
;
10434 *bufsiz
+= newspace
;
10435 xnp
= (Elf_External_Note
*) dest
;
10436 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
10437 H_PUT_32 (abfd
, size
, xnp
->descsz
);
10438 H_PUT_32 (abfd
, type
, xnp
->type
);
10442 memcpy (dest
, name
, namesz
);
10450 memcpy (dest
, input
, size
);
10461 elfcore_write_prpsinfo (bfd
*abfd
,
10465 const char *psargs
)
10467 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10469 if (bed
->elf_backend_write_core_note
!= NULL
)
10472 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10473 NT_PRPSINFO
, fname
, psargs
);
10478 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10479 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10480 if (bed
->s
->elfclass
== ELFCLASS32
)
10482 #if defined (HAVE_PSINFO32_T)
10484 int note_type
= NT_PSINFO
;
10487 int note_type
= NT_PRPSINFO
;
10490 memset (&data
, 0, sizeof (data
));
10491 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10492 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10493 return elfcore_write_note (abfd
, buf
, bufsiz
,
10494 "CORE", note_type
, &data
, sizeof (data
));
10499 #if defined (HAVE_PSINFO_T)
10501 int note_type
= NT_PSINFO
;
10504 int note_type
= NT_PRPSINFO
;
10507 memset (&data
, 0, sizeof (data
));
10508 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
10509 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
10510 return elfcore_write_note (abfd
, buf
, bufsiz
,
10511 "CORE", note_type
, &data
, sizeof (data
));
10513 #endif /* PSINFO_T or PRPSINFO_T */
10520 elfcore_write_linux_prpsinfo32
10521 (bfd
*abfd
, char *buf
, int *bufsiz
,
10522 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10524 if (get_elf_backend_data (abfd
)->linux_prpsinfo32_ugid16
)
10526 struct elf_external_linux_prpsinfo32_ugid16 data
;
10528 swap_linux_prpsinfo32_ugid16_out (abfd
, prpsinfo
, &data
);
10529 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10530 &data
, sizeof (data
));
10534 struct elf_external_linux_prpsinfo32_ugid32 data
;
10536 swap_linux_prpsinfo32_ugid32_out (abfd
, prpsinfo
, &data
);
10537 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
10538 &data
, sizeof (data
));
10543 elfcore_write_linux_prpsinfo64
10544 (bfd
*abfd
, char *buf
, int *bufsiz
,
10545 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
10547 if (get_elf_backend_data (abfd
)->linux_prpsinfo64_ugid16
)
10549 struct elf_external_linux_prpsinfo64_ugid16 data
;
10551 swap_linux_prpsinfo64_ugid16_out (abfd
, prpsinfo
, &data
);
10552 return elfcore_write_note (abfd
, buf
, bufsiz
,
10553 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10557 struct elf_external_linux_prpsinfo64_ugid32 data
;
10559 swap_linux_prpsinfo64_ugid32_out (abfd
, prpsinfo
, &data
);
10560 return elfcore_write_note (abfd
, buf
, bufsiz
,
10561 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
10566 elfcore_write_prstatus (bfd
*abfd
,
10573 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10575 if (bed
->elf_backend_write_core_note
!= NULL
)
10578 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
10580 pid
, cursig
, gregs
);
10585 #if defined (HAVE_PRSTATUS_T)
10586 #if defined (HAVE_PRSTATUS32_T)
10587 if (bed
->s
->elfclass
== ELFCLASS32
)
10589 prstatus32_t prstat
;
10591 memset (&prstat
, 0, sizeof (prstat
));
10592 prstat
.pr_pid
= pid
;
10593 prstat
.pr_cursig
= cursig
;
10594 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10595 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10596 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10603 memset (&prstat
, 0, sizeof (prstat
));
10604 prstat
.pr_pid
= pid
;
10605 prstat
.pr_cursig
= cursig
;
10606 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
10607 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
10608 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
10610 #endif /* HAVE_PRSTATUS_T */
10616 #if defined (HAVE_LWPSTATUS_T)
10618 elfcore_write_lwpstatus (bfd
*abfd
,
10625 lwpstatus_t lwpstat
;
10626 const char *note_name
= "CORE";
10628 memset (&lwpstat
, 0, sizeof (lwpstat
));
10629 lwpstat
.pr_lwpid
= pid
>> 16;
10630 lwpstat
.pr_cursig
= cursig
;
10631 #if defined (HAVE_LWPSTATUS_T_PR_REG)
10632 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
10633 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10634 #if !defined(gregs)
10635 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
10636 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
10638 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
10639 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
10642 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10643 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
10645 #endif /* HAVE_LWPSTATUS_T */
10647 #if defined (HAVE_PSTATUS_T)
10649 elfcore_write_pstatus (bfd
*abfd
,
10653 int cursig ATTRIBUTE_UNUSED
,
10654 const void *gregs ATTRIBUTE_UNUSED
)
10656 const char *note_name
= "CORE";
10657 #if defined (HAVE_PSTATUS32_T)
10658 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10660 if (bed
->s
->elfclass
== ELFCLASS32
)
10664 memset (&pstat
, 0, sizeof (pstat
));
10665 pstat
.pr_pid
= pid
& 0xffff;
10666 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10667 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10675 memset (&pstat
, 0, sizeof (pstat
));
10676 pstat
.pr_pid
= pid
& 0xffff;
10677 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
10678 NT_PSTATUS
, &pstat
, sizeof (pstat
));
10682 #endif /* HAVE_PSTATUS_T */
10685 elfcore_write_prfpreg (bfd
*abfd
,
10688 const void *fpregs
,
10691 const char *note_name
= "CORE";
10692 return elfcore_write_note (abfd
, buf
, bufsiz
,
10693 note_name
, NT_FPREGSET
, fpregs
, size
);
10697 elfcore_write_prxfpreg (bfd
*abfd
,
10700 const void *xfpregs
,
10703 char *note_name
= "LINUX";
10704 return elfcore_write_note (abfd
, buf
, bufsiz
,
10705 note_name
, NT_PRXFPREG
, xfpregs
, size
);
10709 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
10710 const void *xfpregs
, int size
)
10713 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
10714 note_name
= "FreeBSD";
10716 note_name
= "LINUX";
10717 return elfcore_write_note (abfd
, buf
, bufsiz
,
10718 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
10722 elfcore_write_ppc_vmx (bfd
*abfd
,
10725 const void *ppc_vmx
,
10728 char *note_name
= "LINUX";
10729 return elfcore_write_note (abfd
, buf
, bufsiz
,
10730 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
10734 elfcore_write_ppc_vsx (bfd
*abfd
,
10737 const void *ppc_vsx
,
10740 char *note_name
= "LINUX";
10741 return elfcore_write_note (abfd
, buf
, bufsiz
,
10742 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
10746 elfcore_write_s390_high_gprs (bfd
*abfd
,
10749 const void *s390_high_gprs
,
10752 char *note_name
= "LINUX";
10753 return elfcore_write_note (abfd
, buf
, bufsiz
,
10754 note_name
, NT_S390_HIGH_GPRS
,
10755 s390_high_gprs
, size
);
10759 elfcore_write_s390_timer (bfd
*abfd
,
10762 const void *s390_timer
,
10765 char *note_name
= "LINUX";
10766 return elfcore_write_note (abfd
, buf
, bufsiz
,
10767 note_name
, NT_S390_TIMER
, s390_timer
, size
);
10771 elfcore_write_s390_todcmp (bfd
*abfd
,
10774 const void *s390_todcmp
,
10777 char *note_name
= "LINUX";
10778 return elfcore_write_note (abfd
, buf
, bufsiz
,
10779 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
10783 elfcore_write_s390_todpreg (bfd
*abfd
,
10786 const void *s390_todpreg
,
10789 char *note_name
= "LINUX";
10790 return elfcore_write_note (abfd
, buf
, bufsiz
,
10791 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
10795 elfcore_write_s390_ctrs (bfd
*abfd
,
10798 const void *s390_ctrs
,
10801 char *note_name
= "LINUX";
10802 return elfcore_write_note (abfd
, buf
, bufsiz
,
10803 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
10807 elfcore_write_s390_prefix (bfd
*abfd
,
10810 const void *s390_prefix
,
10813 char *note_name
= "LINUX";
10814 return elfcore_write_note (abfd
, buf
, bufsiz
,
10815 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
10819 elfcore_write_s390_last_break (bfd
*abfd
,
10822 const void *s390_last_break
,
10825 char *note_name
= "LINUX";
10826 return elfcore_write_note (abfd
, buf
, bufsiz
,
10827 note_name
, NT_S390_LAST_BREAK
,
10828 s390_last_break
, size
);
10832 elfcore_write_s390_system_call (bfd
*abfd
,
10835 const void *s390_system_call
,
10838 char *note_name
= "LINUX";
10839 return elfcore_write_note (abfd
, buf
, bufsiz
,
10840 note_name
, NT_S390_SYSTEM_CALL
,
10841 s390_system_call
, size
);
10845 elfcore_write_s390_tdb (bfd
*abfd
,
10848 const void *s390_tdb
,
10851 char *note_name
= "LINUX";
10852 return elfcore_write_note (abfd
, buf
, bufsiz
,
10853 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10857 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10860 const void *s390_vxrs_low
,
10863 char *note_name
= "LINUX";
10864 return elfcore_write_note (abfd
, buf
, bufsiz
,
10865 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10869 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10872 const void *s390_vxrs_high
,
10875 char *note_name
= "LINUX";
10876 return elfcore_write_note (abfd
, buf
, bufsiz
,
10877 note_name
, NT_S390_VXRS_HIGH
,
10878 s390_vxrs_high
, size
);
10882 elfcore_write_s390_gs_cb (bfd
*abfd
,
10885 const void *s390_gs_cb
,
10888 char *note_name
= "LINUX";
10889 return elfcore_write_note (abfd
, buf
, bufsiz
,
10890 note_name
, NT_S390_GS_CB
,
10895 elfcore_write_s390_gs_bc (bfd
*abfd
,
10898 const void *s390_gs_bc
,
10901 char *note_name
= "LINUX";
10902 return elfcore_write_note (abfd
, buf
, bufsiz
,
10903 note_name
, NT_S390_GS_BC
,
10908 elfcore_write_arm_vfp (bfd
*abfd
,
10911 const void *arm_vfp
,
10914 char *note_name
= "LINUX";
10915 return elfcore_write_note (abfd
, buf
, bufsiz
,
10916 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10920 elfcore_write_aarch_tls (bfd
*abfd
,
10923 const void *aarch_tls
,
10926 char *note_name
= "LINUX";
10927 return elfcore_write_note (abfd
, buf
, bufsiz
,
10928 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10932 elfcore_write_aarch_hw_break (bfd
*abfd
,
10935 const void *aarch_hw_break
,
10938 char *note_name
= "LINUX";
10939 return elfcore_write_note (abfd
, buf
, bufsiz
,
10940 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10944 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10947 const void *aarch_hw_watch
,
10950 char *note_name
= "LINUX";
10951 return elfcore_write_note (abfd
, buf
, bufsiz
,
10952 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10956 elfcore_write_register_note (bfd
*abfd
,
10959 const char *section
,
10963 if (strcmp (section
, ".reg2") == 0)
10964 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10965 if (strcmp (section
, ".reg-xfp") == 0)
10966 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10967 if (strcmp (section
, ".reg-xstate") == 0)
10968 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10969 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10970 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10971 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10972 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10973 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10974 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10975 if (strcmp (section
, ".reg-s390-timer") == 0)
10976 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10977 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10978 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10979 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10980 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10981 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10982 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10983 if (strcmp (section
, ".reg-s390-prefix") == 0)
10984 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10985 if (strcmp (section
, ".reg-s390-last-break") == 0)
10986 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10987 if (strcmp (section
, ".reg-s390-system-call") == 0)
10988 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10989 if (strcmp (section
, ".reg-s390-tdb") == 0)
10990 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10991 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10992 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10993 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10994 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10995 if (strcmp (section
, ".reg-s390-gs-cb") == 0)
10996 return elfcore_write_s390_gs_cb (abfd
, buf
, bufsiz
, data
, size
);
10997 if (strcmp (section
, ".reg-s390-gs-bc") == 0)
10998 return elfcore_write_s390_gs_bc (abfd
, buf
, bufsiz
, data
, size
);
10999 if (strcmp (section
, ".reg-arm-vfp") == 0)
11000 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
11001 if (strcmp (section
, ".reg-aarch-tls") == 0)
11002 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
11003 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
11004 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
11005 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
11006 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
11011 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
,
11016 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
11017 gABI specifies that PT_NOTE alignment should be aligned to 4
11018 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If
11019 align is less than 4, we use 4 byte alignment. */
11024 while (p
< buf
+ size
)
11026 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
11027 Elf_Internal_Note in
;
11029 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
11032 in
.type
= H_GET_32 (abfd
, xnp
->type
);
11034 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
11035 in
.namedata
= xnp
->name
;
11036 if (in
.namesz
> buf
- in
.namedata
+ size
)
11039 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
11040 in
.descdata
= p
+ ELF_NOTE_DESC_OFFSET (in
.namesz
, align
);
11041 in
.descpos
= offset
+ (in
.descdata
- buf
);
11043 && (in
.descdata
>= buf
+ size
11044 || in
.descsz
> buf
- in
.descdata
+ size
))
11047 switch (bfd_get_format (abfd
))
11054 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
11057 const char * string
;
11059 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
11063 GROKER_ELEMENT ("", elfcore_grok_note
),
11064 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note
),
11065 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
11066 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
11067 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
11068 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
11070 #undef GROKER_ELEMENT
11073 for (i
= ARRAY_SIZE (grokers
); i
--;)
11075 if (in
.namesz
>= grokers
[i
].len
11076 && strncmp (in
.namedata
, grokers
[i
].string
,
11077 grokers
[i
].len
) == 0)
11079 if (! grokers
[i
].func (abfd
, & in
))
11088 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
11090 if (! elfobj_grok_gnu_note (abfd
, &in
))
11093 else if (in
.namesz
== sizeof "stapsdt"
11094 && strcmp (in
.namedata
, "stapsdt") == 0)
11096 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
11102 p
+= ELF_NOTE_NEXT_OFFSET (in
.namesz
, in
.descsz
, align
);
11109 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
,
11114 if (size
== 0 || (size
+ 1) == 0)
11117 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
11120 buf
= (char *) bfd_malloc (size
+ 1);
11124 /* PR 17512: file: ec08f814
11125 0-termintate the buffer so that string searches will not overflow. */
11128 if (bfd_bread (buf
, size
, abfd
) != size
11129 || !elf_parse_notes (abfd
, buf
, size
, offset
, align
))
11139 /* Providing external access to the ELF program header table. */
11141 /* Return an upper bound on the number of bytes required to store a
11142 copy of ABFD's program header table entries. Return -1 if an error
11143 occurs; bfd_get_error will return an appropriate code. */
11146 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
11148 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11150 bfd_set_error (bfd_error_wrong_format
);
11154 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
11157 /* Copy ABFD's program header table entries to *PHDRS. The entries
11158 will be stored as an array of Elf_Internal_Phdr structures, as
11159 defined in include/elf/internal.h. To find out how large the
11160 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
11162 Return the number of program header table entries read, or -1 if an
11163 error occurs; bfd_get_error will return an appropriate code. */
11166 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
11170 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
11172 bfd_set_error (bfd_error_wrong_format
);
11176 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
11177 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
11178 num_phdrs
* sizeof (Elf_Internal_Phdr
));
11183 enum elf_reloc_type_class
11184 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
11185 const asection
*rel_sec ATTRIBUTE_UNUSED
,
11186 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
11188 return reloc_class_normal
;
11191 /* For RELA architectures, return the relocation value for a
11192 relocation against a local symbol. */
11195 _bfd_elf_rela_local_sym (bfd
*abfd
,
11196 Elf_Internal_Sym
*sym
,
11198 Elf_Internal_Rela
*rel
)
11200 asection
*sec
= *psec
;
11201 bfd_vma relocation
;
11203 relocation
= (sec
->output_section
->vma
11204 + sec
->output_offset
11206 if ((sec
->flags
& SEC_MERGE
)
11207 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
11208 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
11211 _bfd_merged_section_offset (abfd
, psec
,
11212 elf_section_data (sec
)->sec_info
,
11213 sym
->st_value
+ rel
->r_addend
);
11216 /* If we have changed the section, and our original section is
11217 marked with SEC_EXCLUDE, it means that the original
11218 SEC_MERGE section has been completely subsumed in some
11219 other SEC_MERGE section. In this case, we need to leave
11220 some info around for --emit-relocs. */
11221 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
11222 sec
->kept_section
= *psec
;
11225 rel
->r_addend
-= relocation
;
11226 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
11232 _bfd_elf_rel_local_sym (bfd
*abfd
,
11233 Elf_Internal_Sym
*sym
,
11237 asection
*sec
= *psec
;
11239 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
11240 return sym
->st_value
+ addend
;
11242 return _bfd_merged_section_offset (abfd
, psec
,
11243 elf_section_data (sec
)->sec_info
,
11244 sym
->st_value
+ addend
);
11247 /* Adjust an address within a section. Given OFFSET within SEC, return
11248 the new offset within the section, based upon changes made to the
11249 section. Returns -1 if the offset is now invalid.
11250 The offset (in abnd out) is in target sized bytes, however big a
11254 _bfd_elf_section_offset (bfd
*abfd
,
11255 struct bfd_link_info
*info
,
11259 switch (sec
->sec_info_type
)
11261 case SEC_INFO_TYPE_STABS
:
11262 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
11264 case SEC_INFO_TYPE_EH_FRAME
:
11265 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
11268 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
11270 /* Reverse the offset. */
11271 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11272 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
11274 /* address_size and sec->size are in octets. Convert
11275 to bytes before subtracting the original offset. */
11276 offset
= (sec
->size
- address_size
) / bfd_octets_per_byte (abfd
) - offset
;
11282 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
11283 reconstruct an ELF file by reading the segments out of remote memory
11284 based on the ELF file header at EHDR_VMA and the ELF program headers it
11285 points to. If not null, *LOADBASEP is filled in with the difference
11286 between the VMAs from which the segments were read, and the VMAs the
11287 file headers (and hence BFD's idea of each section's VMA) put them at.
11289 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
11290 remote memory at target address VMA into the local buffer at MYADDR; it
11291 should return zero on success or an `errno' code on failure. TEMPL must
11292 be a BFD for an ELF target with the word size and byte order found in
11293 the remote memory. */
11296 bfd_elf_bfd_from_remote_memory
11299 bfd_size_type size
,
11300 bfd_vma
*loadbasep
,
11301 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
11303 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
11304 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
11308 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
11309 long symcount ATTRIBUTE_UNUSED
,
11310 asymbol
**syms ATTRIBUTE_UNUSED
,
11315 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
11318 const char *relplt_name
;
11319 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
11323 Elf_Internal_Shdr
*hdr
;
11329 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
11332 if (dynsymcount
<= 0)
11335 if (!bed
->plt_sym_val
)
11338 relplt_name
= bed
->relplt_name
;
11339 if (relplt_name
== NULL
)
11340 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
11341 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
11342 if (relplt
== NULL
)
11345 hdr
= &elf_section_data (relplt
)->this_hdr
;
11346 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
11347 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
11350 plt
= bfd_get_section_by_name (abfd
, ".plt");
11354 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
11355 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
11358 count
= relplt
->size
/ hdr
->sh_entsize
;
11359 size
= count
* sizeof (asymbol
);
11360 p
= relplt
->relocation
;
11361 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11363 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
11364 if (p
->addend
!= 0)
11367 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
11369 size
+= sizeof ("+0x") - 1 + 8;
11374 s
= *ret
= (asymbol
*) bfd_malloc (size
);
11378 names
= (char *) (s
+ count
);
11379 p
= relplt
->relocation
;
11381 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
11386 addr
= bed
->plt_sym_val (i
, plt
, p
);
11387 if (addr
== (bfd_vma
) -1)
11390 *s
= **p
->sym_ptr_ptr
;
11391 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
11392 we are defining a symbol, ensure one of them is set. */
11393 if ((s
->flags
& BSF_LOCAL
) == 0)
11394 s
->flags
|= BSF_GLOBAL
;
11395 s
->flags
|= BSF_SYNTHETIC
;
11397 s
->value
= addr
- plt
->vma
;
11400 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
11401 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
11403 if (p
->addend
!= 0)
11407 memcpy (names
, "+0x", sizeof ("+0x") - 1);
11408 names
+= sizeof ("+0x") - 1;
11409 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
11410 for (a
= buf
; *a
== '0'; ++a
)
11413 memcpy (names
, a
, len
);
11416 memcpy (names
, "@plt", sizeof ("@plt"));
11417 names
+= sizeof ("@plt");
11424 /* It is only used by x86-64 so far.
11425 ??? This repeats *COM* id of zero. sec->id is supposed to be unique,
11426 but current usage would allow all of _bfd_std_section to be zero. */
11427 static const asymbol lcomm_sym
11428 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section
);
11429 asection _bfd_elf_large_com_section
11430 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
, &lcomm_sym
,
11431 "LARGE_COMMON", 0, SEC_IS_COMMON
);
11434 _bfd_elf_post_process_headers (bfd
* abfd
,
11435 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
11437 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
11439 i_ehdrp
= elf_elfheader (abfd
);
11441 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
11443 /* To make things simpler for the loader on Linux systems we set the
11444 osabi field to ELFOSABI_GNU if the binary contains symbols of
11445 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
11446 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
11447 && elf_tdata (abfd
)->has_gnu_symbols
)
11448 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
11452 /* Return TRUE for ELF symbol types that represent functions.
11453 This is the default version of this function, which is sufficient for
11454 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
11457 _bfd_elf_is_function_type (unsigned int type
)
11459 return (type
== STT_FUNC
11460 || type
== STT_GNU_IFUNC
);
11463 /* If the ELF symbol SYM might be a function in SEC, return the
11464 function size and set *CODE_OFF to the function's entry point,
11465 otherwise return zero. */
11468 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
11471 bfd_size_type size
;
11473 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
11474 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
11475 || sym
->section
!= sec
)
11478 *code_off
= sym
->value
;
11480 if (!(sym
->flags
& BSF_SYNTHETIC
))
11481 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;