1 /* ELF executable support for BFD.
3 Copyright (C) 1993-2015 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-psinfo.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 bfd_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
56 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
199 const unsigned char *name
= (const unsigned char *) namearg
;
204 while ((ch
= *name
++) != '\0')
207 if ((g
= (h
& 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h
& 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
225 unsigned long h
= 5381;
228 while ((ch
= *name
++) != '\0')
229 h
= (h
<< 5) + h
+ ch
;
230 return h
& 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd
*abfd
,
238 enum elf_target_id object_id
)
240 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
241 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
242 if (abfd
->tdata
.any
== NULL
)
245 elf_object_id (abfd
) = object_id
;
246 if (abfd
->direction
!= read_direction
)
248 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
251 elf_tdata (abfd
)->o
= o
;
252 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
259 bfd_elf_make_object (bfd
*abfd
)
261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
262 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
267 bfd_elf_mkcorefile (bfd
*abfd
)
269 /* I think this can be done just like an object file. */
270 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
272 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
273 return elf_tdata (abfd
)->core
!= NULL
;
277 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
279 Elf_Internal_Shdr
**i_shdrp
;
280 bfd_byte
*shstrtab
= NULL
;
282 bfd_size_type shstrtabsize
;
284 i_shdrp
= elf_elfsections (abfd
);
286 || shindex
>= elf_numsections (abfd
)
287 || i_shdrp
[shindex
] == 0)
290 shstrtab
= i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize
+ 1 <= 1
300 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0
301 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
)
303 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
305 if (bfd_get_error () != bfd_error_system_call
)
306 bfd_set_error (bfd_error_file_truncated
);
307 bfd_release (abfd
, shstrtab
);
309 /* Once we've failed to read it, make sure we don't keep
310 trying. Otherwise, we'll keep allocating space for
311 the string table over and over. */
312 i_shdrp
[shindex
]->sh_size
= 0;
315 shstrtab
[shstrtabsize
] = '\0';
316 i_shdrp
[shindex
]->contents
= shstrtab
;
318 return (char *) shstrtab
;
322 bfd_elf_string_from_elf_section (bfd
*abfd
,
323 unsigned int shindex
,
324 unsigned int strindex
)
326 Elf_Internal_Shdr
*hdr
;
331 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
)
338 if (hdr
->sh_type
!= SHT_STRTAB
&& hdr
->sh_type
< SHT_LOOS
)
340 /* PR 17512: file: f057ec89. */
341 _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
346 if (bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
350 if (strindex
>= hdr
->sh_size
)
352 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
353 (*_bfd_error_handler
)
354 (_("%B: invalid string offset %u >= %lu for section `%s'"),
355 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
356 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
358 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
362 return ((char *) hdr
->contents
) + strindex
;
365 /* Read and convert symbols to internal format.
366 SYMCOUNT specifies the number of symbols to read, starting from
367 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
368 are non-NULL, they are used to store the internal symbols, external
369 symbols, and symbol section index extensions, respectively.
370 Returns a pointer to the internal symbol buffer (malloced if necessary)
371 or NULL if there were no symbols or some kind of problem. */
374 bfd_elf_get_elf_syms (bfd
*ibfd
,
375 Elf_Internal_Shdr
*symtab_hdr
,
378 Elf_Internal_Sym
*intsym_buf
,
380 Elf_External_Sym_Shndx
*extshndx_buf
)
382 Elf_Internal_Shdr
*shndx_hdr
;
384 const bfd_byte
*esym
;
385 Elf_External_Sym_Shndx
*alloc_extshndx
;
386 Elf_External_Sym_Shndx
*shndx
;
387 Elf_Internal_Sym
*alloc_intsym
;
388 Elf_Internal_Sym
*isym
;
389 Elf_Internal_Sym
*isymend
;
390 const struct elf_backend_data
*bed
;
395 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
401 /* Normal syms might have section extension entries. */
403 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
404 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
406 /* Read the symbols. */
408 alloc_extshndx
= NULL
;
410 bed
= get_elf_backend_data (ibfd
);
411 extsym_size
= bed
->s
->sizeof_sym
;
412 amt
= symcount
* extsym_size
;
413 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
414 if (extsym_buf
== NULL
)
416 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
417 extsym_buf
= alloc_ext
;
419 if (extsym_buf
== NULL
420 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
421 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
427 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
431 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
432 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
433 if (extshndx_buf
== NULL
)
435 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
436 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
437 extshndx_buf
= alloc_extshndx
;
439 if (extshndx_buf
== NULL
440 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
441 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
448 if (intsym_buf
== NULL
)
450 alloc_intsym
= (Elf_Internal_Sym
*)
451 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
452 intsym_buf
= alloc_intsym
;
453 if (intsym_buf
== NULL
)
457 /* Convert the symbols to internal form. */
458 isymend
= intsym_buf
+ symcount
;
459 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
460 shndx
= extshndx_buf
;
462 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
463 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
465 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
466 (*_bfd_error_handler
) (_("%B symbol number %lu references "
467 "nonexistent SHT_SYMTAB_SHNDX section"),
468 ibfd
, (unsigned long) symoffset
);
469 if (alloc_intsym
!= NULL
)
476 if (alloc_ext
!= NULL
)
478 if (alloc_extshndx
!= NULL
)
479 free (alloc_extshndx
);
484 /* Look up a symbol name. */
486 bfd_elf_sym_name (bfd
*abfd
,
487 Elf_Internal_Shdr
*symtab_hdr
,
488 Elf_Internal_Sym
*isym
,
492 unsigned int iname
= isym
->st_name
;
493 unsigned int shindex
= symtab_hdr
->sh_link
;
495 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
496 /* Check for a bogus st_shndx to avoid crashing. */
497 && isym
->st_shndx
< elf_numsections (abfd
))
499 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
500 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
503 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
506 else if (sym_sec
&& *name
== '\0')
507 name
= bfd_section_name (abfd
, sym_sec
);
512 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
513 sections. The first element is the flags, the rest are section
516 typedef union elf_internal_group
{
517 Elf_Internal_Shdr
*shdr
;
519 } Elf_Internal_Group
;
521 /* Return the name of the group signature symbol. Why isn't the
522 signature just a string? */
525 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
527 Elf_Internal_Shdr
*hdr
;
528 unsigned char esym
[sizeof (Elf64_External_Sym
)];
529 Elf_External_Sym_Shndx eshndx
;
530 Elf_Internal_Sym isym
;
532 /* First we need to ensure the symbol table is available. Make sure
533 that it is a symbol table section. */
534 if (ghdr
->sh_link
>= elf_numsections (abfd
))
536 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
537 if (hdr
->sh_type
!= SHT_SYMTAB
538 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
541 /* Go read the symbol. */
542 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
543 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
544 &isym
, esym
, &eshndx
) == NULL
)
547 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
550 /* Set next_in_group list pointer, and group name for NEWSECT. */
553 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
555 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
557 /* If num_group is zero, read in all SHT_GROUP sections. The count
558 is set to -1 if there are no SHT_GROUP sections. */
561 unsigned int i
, shnum
;
563 /* First count the number of groups. If we have a SHT_GROUP
564 section with just a flag word (ie. sh_size is 4), ignore it. */
565 shnum
= elf_numsections (abfd
);
568 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
569 ( (shdr)->sh_type == SHT_GROUP \
570 && (shdr)->sh_size >= minsize \
571 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
572 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
574 for (i
= 0; i
< shnum
; i
++)
576 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
578 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
584 num_group
= (unsigned) -1;
585 elf_tdata (abfd
)->num_group
= num_group
;
589 /* We keep a list of elf section headers for group sections,
590 so we can find them quickly. */
593 elf_tdata (abfd
)->num_group
= num_group
;
594 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
595 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
596 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
600 for (i
= 0; i
< shnum
; i
++)
602 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
604 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
607 Elf_Internal_Group
*dest
;
609 /* Add to list of sections. */
610 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
613 /* Read the raw contents. */
614 BFD_ASSERT (sizeof (*dest
) >= 4);
615 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
616 shdr
->contents
= (unsigned char *)
617 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
618 /* PR binutils/4110: Handle corrupt group headers. */
619 if (shdr
->contents
== NULL
)
622 (_("%B: corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
623 bfd_set_error (bfd_error_bad_value
);
628 memset (shdr
->contents
, 0, amt
);
630 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
631 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
635 (_("%B: invalid size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
636 bfd_set_error (bfd_error_bad_value
);
638 /* PR 17510: If the group contents are even partially
639 corrupt, do not allow any of the contents to be used. */
640 memset (shdr
->contents
, 0, amt
);
644 /* Translate raw contents, a flag word followed by an
645 array of elf section indices all in target byte order,
646 to the flag word followed by an array of elf section
648 src
= shdr
->contents
+ shdr
->sh_size
;
649 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
657 idx
= H_GET_32 (abfd
, src
);
658 if (src
== shdr
->contents
)
661 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
662 shdr
->bfd_section
->flags
663 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
668 ((*_bfd_error_handler
)
669 (_("%B: invalid SHT_GROUP entry"), abfd
));
672 dest
->shdr
= elf_elfsections (abfd
)[idx
];
677 /* PR 17510: Corrupt binaries might contain invalid groups. */
678 if (num_group
!= (unsigned) elf_tdata (abfd
)->num_group
)
680 elf_tdata (abfd
)->num_group
= num_group
;
682 /* If all groups are invalid then fail. */
685 elf_tdata (abfd
)->group_sect_ptr
= NULL
;
686 elf_tdata (abfd
)->num_group
= num_group
= -1;
687 (*_bfd_error_handler
) (_("%B: no valid group sections found"), abfd
);
688 bfd_set_error (bfd_error_bad_value
);
694 if (num_group
!= (unsigned) -1)
698 for (i
= 0; i
< num_group
; i
++)
700 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
701 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
702 unsigned int n_elt
= shdr
->sh_size
/ 4;
704 /* Look through this group's sections to see if current
705 section is a member. */
707 if ((++idx
)->shdr
== hdr
)
711 /* We are a member of this group. Go looking through
712 other members to see if any others are linked via
714 idx
= (Elf_Internal_Group
*) shdr
->contents
;
715 n_elt
= shdr
->sh_size
/ 4;
717 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
718 && elf_next_in_group (s
) != NULL
)
722 /* Snarf the group name from other member, and
723 insert current section in circular list. */
724 elf_group_name (newsect
) = elf_group_name (s
);
725 elf_next_in_group (newsect
) = elf_next_in_group (s
);
726 elf_next_in_group (s
) = newsect
;
732 gname
= group_signature (abfd
, shdr
);
735 elf_group_name (newsect
) = gname
;
737 /* Start a circular list with one element. */
738 elf_next_in_group (newsect
) = newsect
;
741 /* If the group section has been created, point to the
743 if (shdr
->bfd_section
!= NULL
)
744 elf_next_in_group (shdr
->bfd_section
) = newsect
;
752 if (elf_group_name (newsect
) == NULL
)
754 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
762 _bfd_elf_setup_sections (bfd
*abfd
)
765 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
766 bfd_boolean result
= TRUE
;
769 /* Process SHF_LINK_ORDER. */
770 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
772 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
773 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
775 unsigned int elfsec
= this_hdr
->sh_link
;
776 /* FIXME: The old Intel compiler and old strip/objcopy may
777 not set the sh_link or sh_info fields. Hence we could
778 get the situation where elfsec is 0. */
781 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
782 if (bed
->link_order_error_handler
)
783 bed
->link_order_error_handler
784 (_("%B: warning: sh_link not set for section `%A'"),
789 asection
*linksec
= NULL
;
791 if (elfsec
< elf_numsections (abfd
))
793 this_hdr
= elf_elfsections (abfd
)[elfsec
];
794 linksec
= this_hdr
->bfd_section
;
798 Some strip/objcopy may leave an incorrect value in
799 sh_link. We don't want to proceed. */
802 (*_bfd_error_handler
)
803 (_("%B: sh_link [%d] in section `%A' is incorrect"),
804 s
->owner
, s
, elfsec
);
808 elf_linked_to_section (s
) = linksec
;
813 /* Process section groups. */
814 if (num_group
== (unsigned) -1)
817 for (i
= 0; i
< num_group
; i
++)
819 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
820 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
821 unsigned int n_elt
= shdr
->sh_size
/ 4;
824 if ((++idx
)->shdr
->bfd_section
)
825 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
826 else if (idx
->shdr
->sh_type
== SHT_RELA
827 || idx
->shdr
->sh_type
== SHT_REL
)
828 /* We won't include relocation sections in section groups in
829 output object files. We adjust the group section size here
830 so that relocatable link will work correctly when
831 relocation sections are in section group in input object
833 shdr
->bfd_section
->size
-= 4;
836 /* There are some unknown sections in the group. */
837 (*_bfd_error_handler
)
838 (_("%B: unknown [%d] section `%s' in group [%s]"),
840 (unsigned int) idx
->shdr
->sh_type
,
841 bfd_elf_string_from_elf_section (abfd
,
842 (elf_elfheader (abfd
)
845 shdr
->bfd_section
->name
);
853 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
855 return elf_next_in_group (sec
) != NULL
;
858 /* Make a BFD section from an ELF section. We store a pointer to the
859 BFD section in the bfd_section field of the header. */
862 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
863 Elf_Internal_Shdr
*hdr
,
869 const struct elf_backend_data
*bed
;
871 if (hdr
->bfd_section
!= NULL
)
874 newsect
= bfd_make_section_anyway (abfd
, name
);
878 hdr
->bfd_section
= newsect
;
879 elf_section_data (newsect
)->this_hdr
= *hdr
;
880 elf_section_data (newsect
)->this_idx
= shindex
;
882 /* Always use the real type/flags. */
883 elf_section_type (newsect
) = hdr
->sh_type
;
884 elf_section_flags (newsect
) = hdr
->sh_flags
;
886 newsect
->filepos
= hdr
->sh_offset
;
888 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
889 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
890 || ! bfd_set_section_alignment (abfd
, newsect
,
891 bfd_log2 (hdr
->sh_addralign
)))
894 flags
= SEC_NO_FLAGS
;
895 if (hdr
->sh_type
!= SHT_NOBITS
)
896 flags
|= SEC_HAS_CONTENTS
;
897 if (hdr
->sh_type
== SHT_GROUP
)
898 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
899 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
902 if (hdr
->sh_type
!= SHT_NOBITS
)
905 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
906 flags
|= SEC_READONLY
;
907 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
909 else if ((flags
& SEC_LOAD
) != 0)
911 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
914 newsect
->entsize
= hdr
->sh_entsize
;
915 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
916 flags
|= SEC_STRINGS
;
918 if (hdr
->sh_flags
& SHF_GROUP
)
919 if (!setup_group (abfd
, hdr
, newsect
))
921 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
922 flags
|= SEC_THREAD_LOCAL
;
923 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
924 flags
|= SEC_EXCLUDE
;
926 if ((flags
& SEC_ALLOC
) == 0)
928 /* The debugging sections appear to be recognized only by name,
929 not any sort of flag. Their SEC_ALLOC bits are cleared. */
936 else if (name
[1] == 'g' && name
[2] == 'n')
937 p
= ".gnu.linkonce.wi.", n
= 17;
938 else if (name
[1] == 'g' && name
[2] == 'd')
939 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
940 else if (name
[1] == 'l')
942 else if (name
[1] == 's')
944 else if (name
[1] == 'z')
945 p
= ".zdebug", n
= 7;
948 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
949 flags
|= SEC_DEBUGGING
;
953 /* As a GNU extension, if the name begins with .gnu.linkonce, we
954 only link a single copy of the section. This is used to support
955 g++. g++ will emit each template expansion in its own section.
956 The symbols will be defined as weak, so that multiple definitions
957 are permitted. The GNU linker extension is to actually discard
958 all but one of the sections. */
959 if (CONST_STRNEQ (name
, ".gnu.linkonce")
960 && elf_next_in_group (newsect
) == NULL
)
961 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
963 bed
= get_elf_backend_data (abfd
);
964 if (bed
->elf_backend_section_flags
)
965 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
968 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
971 /* We do not parse the PT_NOTE segments as we are interested even in the
972 separate debug info files which may have the segments offsets corrupted.
973 PT_NOTEs from the core files are currently not parsed using BFD. */
974 if (hdr
->sh_type
== SHT_NOTE
)
978 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
981 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
985 if ((flags
& SEC_ALLOC
) != 0)
987 Elf_Internal_Phdr
*phdr
;
988 unsigned int i
, nload
;
990 /* Some ELF linkers produce binaries with all the program header
991 p_paddr fields zero. If we have such a binary with more than
992 one PT_LOAD header, then leave the section lma equal to vma
993 so that we don't create sections with overlapping lma. */
994 phdr
= elf_tdata (abfd
)->phdr
;
995 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
996 if (phdr
->p_paddr
!= 0)
998 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1000 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1003 phdr
= elf_tdata (abfd
)->phdr
;
1004 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1006 if (((phdr
->p_type
== PT_LOAD
1007 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1008 || phdr
->p_type
== PT_TLS
)
1009 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1011 if ((flags
& SEC_LOAD
) == 0)
1012 newsect
->lma
= (phdr
->p_paddr
1013 + hdr
->sh_addr
- phdr
->p_vaddr
);
1015 /* We used to use the same adjustment for SEC_LOAD
1016 sections, but that doesn't work if the segment
1017 is packed with code from multiple VMAs.
1018 Instead we calculate the section LMA based on
1019 the segment LMA. It is assumed that the
1020 segment will contain sections with contiguous
1021 LMAs, even if the VMAs are not. */
1022 newsect
->lma
= (phdr
->p_paddr
1023 + hdr
->sh_offset
- phdr
->p_offset
);
1025 /* With contiguous segments, we can't tell from file
1026 offsets whether a section with zero size should
1027 be placed at the end of one segment or the
1028 beginning of the next. Decide based on vaddr. */
1029 if (hdr
->sh_addr
>= phdr
->p_vaddr
1030 && (hdr
->sh_addr
+ hdr
->sh_size
1031 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1037 /* Compress/decompress DWARF debug sections with names: .debug_* and
1038 .zdebug_*, after the section flags is set. */
1039 if ((flags
& SEC_DEBUGGING
)
1040 && ((name
[1] == 'd' && name
[6] == '_')
1041 || (name
[1] == 'z' && name
[7] == '_')))
1043 enum { nothing
, compress
, decompress
} action
= nothing
;
1046 if (bfd_is_section_compressed (abfd
, newsect
))
1048 /* Compressed section. Check if we should decompress. */
1049 if ((abfd
->flags
& BFD_DECOMPRESS
))
1050 action
= decompress
;
1054 /* Normal section. Check if we should compress. */
1055 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
1065 if (!bfd_init_section_compress_status (abfd
, newsect
))
1067 (*_bfd_error_handler
)
1068 (_("%B: unable to initialize compress status for section %s"),
1072 /* PR binutils/18087: Compression does not always make a section
1073 smaller. So only rename the section when compression has
1074 actually taken place. */
1075 if (newsect
->compress_status
== COMPRESS_SECTION_DONE
)
1079 unsigned int len
= strlen (name
);
1081 new_name
= bfd_alloc (abfd
, len
+ 2);
1082 if (new_name
== NULL
)
1086 memcpy (new_name
+ 2, name
+ 1, len
);
1091 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1093 (*_bfd_error_handler
)
1094 (_("%B: unable to initialize decompress status for section %s"),
1100 unsigned int len
= strlen (name
);
1102 new_name
= bfd_alloc (abfd
, len
);
1103 if (new_name
== NULL
)
1106 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1110 if (new_name
!= NULL
)
1111 bfd_rename_section (abfd
, newsect
, new_name
);
1117 const char *const bfd_elf_section_type_names
[] = {
1118 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1119 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1120 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1123 /* ELF relocs are against symbols. If we are producing relocatable
1124 output, and the reloc is against an external symbol, and nothing
1125 has given us any additional addend, the resulting reloc will also
1126 be against the same symbol. In such a case, we don't want to
1127 change anything about the way the reloc is handled, since it will
1128 all be done at final link time. Rather than put special case code
1129 into bfd_perform_relocation, all the reloc types use this howto
1130 function. It just short circuits the reloc if producing
1131 relocatable output against an external symbol. */
1133 bfd_reloc_status_type
1134 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1135 arelent
*reloc_entry
,
1137 void *data ATTRIBUTE_UNUSED
,
1138 asection
*input_section
,
1140 char **error_message ATTRIBUTE_UNUSED
)
1142 if (output_bfd
!= NULL
1143 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1144 && (! reloc_entry
->howto
->partial_inplace
1145 || reloc_entry
->addend
== 0))
1147 reloc_entry
->address
+= input_section
->output_offset
;
1148 return bfd_reloc_ok
;
1151 return bfd_reloc_continue
;
1154 /* Copy the program header and other data from one object module to
1158 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1160 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1161 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1164 if (!elf_flags_init (obfd
))
1166 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1167 elf_flags_init (obfd
) = TRUE
;
1170 elf_gp (obfd
) = elf_gp (ibfd
);
1172 /* Also copy the EI_OSABI field. */
1173 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1174 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1176 /* Copy object attributes. */
1177 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1182 get_segment_type (unsigned int p_type
)
1187 case PT_NULL
: pt
= "NULL"; break;
1188 case PT_LOAD
: pt
= "LOAD"; break;
1189 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1190 case PT_INTERP
: pt
= "INTERP"; break;
1191 case PT_NOTE
: pt
= "NOTE"; break;
1192 case PT_SHLIB
: pt
= "SHLIB"; break;
1193 case PT_PHDR
: pt
= "PHDR"; break;
1194 case PT_TLS
: pt
= "TLS"; break;
1195 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1196 case PT_GNU_STACK
: pt
= "STACK"; break;
1197 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1198 default: pt
= NULL
; break;
1203 /* Print out the program headers. */
1206 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1208 FILE *f
= (FILE *) farg
;
1209 Elf_Internal_Phdr
*p
;
1211 bfd_byte
*dynbuf
= NULL
;
1213 p
= elf_tdata (abfd
)->phdr
;
1218 fprintf (f
, _("\nProgram Header:\n"));
1219 c
= elf_elfheader (abfd
)->e_phnum
;
1220 for (i
= 0; i
< c
; i
++, p
++)
1222 const char *pt
= get_segment_type (p
->p_type
);
1227 sprintf (buf
, "0x%lx", p
->p_type
);
1230 fprintf (f
, "%8s off 0x", pt
);
1231 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1232 fprintf (f
, " vaddr 0x");
1233 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1234 fprintf (f
, " paddr 0x");
1235 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1236 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1237 fprintf (f
, " filesz 0x");
1238 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1239 fprintf (f
, " memsz 0x");
1240 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1241 fprintf (f
, " flags %c%c%c",
1242 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1243 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1244 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1245 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1246 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1251 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1254 unsigned int elfsec
;
1255 unsigned long shlink
;
1256 bfd_byte
*extdyn
, *extdynend
;
1258 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1260 fprintf (f
, _("\nDynamic Section:\n"));
1262 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1265 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1266 if (elfsec
== SHN_BAD
)
1268 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1270 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1271 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1274 /* PR 17512: file: 6f427532. */
1275 if (s
->size
< extdynsize
)
1277 extdynend
= extdyn
+ s
->size
;
1278 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1280 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1282 Elf_Internal_Dyn dyn
;
1283 const char *name
= "";
1285 bfd_boolean stringp
;
1286 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1288 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1290 if (dyn
.d_tag
== DT_NULL
)
1297 if (bed
->elf_backend_get_target_dtag
)
1298 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1300 if (!strcmp (name
, ""))
1302 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1307 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1308 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1309 case DT_PLTGOT
: name
= "PLTGOT"; break;
1310 case DT_HASH
: name
= "HASH"; break;
1311 case DT_STRTAB
: name
= "STRTAB"; break;
1312 case DT_SYMTAB
: name
= "SYMTAB"; break;
1313 case DT_RELA
: name
= "RELA"; break;
1314 case DT_RELASZ
: name
= "RELASZ"; break;
1315 case DT_RELAENT
: name
= "RELAENT"; break;
1316 case DT_STRSZ
: name
= "STRSZ"; break;
1317 case DT_SYMENT
: name
= "SYMENT"; break;
1318 case DT_INIT
: name
= "INIT"; break;
1319 case DT_FINI
: name
= "FINI"; break;
1320 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1321 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1322 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1323 case DT_REL
: name
= "REL"; break;
1324 case DT_RELSZ
: name
= "RELSZ"; break;
1325 case DT_RELENT
: name
= "RELENT"; break;
1326 case DT_PLTREL
: name
= "PLTREL"; break;
1327 case DT_DEBUG
: name
= "DEBUG"; break;
1328 case DT_TEXTREL
: name
= "TEXTREL"; break;
1329 case DT_JMPREL
: name
= "JMPREL"; break;
1330 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1331 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1332 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1333 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1334 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1335 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1336 case DT_FLAGS
: name
= "FLAGS"; break;
1337 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1338 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1339 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1340 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1341 case DT_MOVEENT
: name
= "MOVEENT"; break;
1342 case DT_MOVESZ
: name
= "MOVESZ"; break;
1343 case DT_FEATURE
: name
= "FEATURE"; break;
1344 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1345 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1346 case DT_SYMINENT
: name
= "SYMINENT"; break;
1347 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1348 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1349 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1350 case DT_PLTPAD
: name
= "PLTPAD"; break;
1351 case DT_MOVETAB
: name
= "MOVETAB"; break;
1352 case DT_SYMINFO
: name
= "SYMINFO"; break;
1353 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1354 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1355 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1356 case DT_VERSYM
: name
= "VERSYM"; break;
1357 case DT_VERDEF
: name
= "VERDEF"; break;
1358 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1359 case DT_VERNEED
: name
= "VERNEED"; break;
1360 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1361 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1362 case DT_USED
: name
= "USED"; break;
1363 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1364 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1367 fprintf (f
, " %-20s ", name
);
1371 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1376 unsigned int tagv
= dyn
.d_un
.d_val
;
1378 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1381 fprintf (f
, "%s", string
);
1390 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1391 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1393 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1397 if (elf_dynverdef (abfd
) != 0)
1399 Elf_Internal_Verdef
*t
;
1401 fprintf (f
, _("\nVersion definitions:\n"));
1402 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1404 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1405 t
->vd_flags
, t
->vd_hash
,
1406 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1407 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1409 Elf_Internal_Verdaux
*a
;
1412 for (a
= t
->vd_auxptr
->vda_nextptr
;
1416 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1422 if (elf_dynverref (abfd
) != 0)
1424 Elf_Internal_Verneed
*t
;
1426 fprintf (f
, _("\nVersion References:\n"));
1427 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1429 Elf_Internal_Vernaux
*a
;
1431 fprintf (f
, _(" required from %s:\n"),
1432 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1433 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1434 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1435 a
->vna_flags
, a
->vna_other
,
1436 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1448 /* Get version string. */
1451 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1452 bfd_boolean
*hidden
)
1454 const char *version_string
= NULL
;
1455 if (elf_dynversym (abfd
) != 0
1456 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1458 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1460 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1461 vernum
&= VERSYM_VERSION
;
1464 version_string
= "";
1465 else if (vernum
== 1)
1466 version_string
= "Base";
1467 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1469 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1472 Elf_Internal_Verneed
*t
;
1474 version_string
= "";
1475 for (t
= elf_tdata (abfd
)->verref
;
1479 Elf_Internal_Vernaux
*a
;
1481 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1483 if (a
->vna_other
== vernum
)
1485 version_string
= a
->vna_nodename
;
1492 return version_string
;
1495 /* Display ELF-specific fields of a symbol. */
1498 bfd_elf_print_symbol (bfd
*abfd
,
1501 bfd_print_symbol_type how
)
1503 FILE *file
= (FILE *) filep
;
1506 case bfd_print_symbol_name
:
1507 fprintf (file
, "%s", symbol
->name
);
1509 case bfd_print_symbol_more
:
1510 fprintf (file
, "elf ");
1511 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1512 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1514 case bfd_print_symbol_all
:
1516 const char *section_name
;
1517 const char *name
= NULL
;
1518 const struct elf_backend_data
*bed
;
1519 unsigned char st_other
;
1521 const char *version_string
;
1524 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1526 bed
= get_elf_backend_data (abfd
);
1527 if (bed
->elf_backend_print_symbol_all
)
1528 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1532 name
= symbol
->name
;
1533 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1536 fprintf (file
, " %s\t", section_name
);
1537 /* Print the "other" value for a symbol. For common symbols,
1538 we've already printed the size; now print the alignment.
1539 For other symbols, we have no specified alignment, and
1540 we've printed the address; now print the size. */
1541 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1542 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1544 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1545 bfd_fprintf_vma (abfd
, file
, val
);
1547 /* If we have version information, print it. */
1548 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1554 fprintf (file
, " %-11s", version_string
);
1559 fprintf (file
, " (%s)", version_string
);
1560 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1565 /* If the st_other field is not zero, print it. */
1566 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1571 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1572 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1573 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1575 /* Some other non-defined flags are also present, so print
1577 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1580 fprintf (file
, " %s", name
);
1586 /* Allocate an ELF string table--force the first byte to be zero. */
1588 struct bfd_strtab_hash
*
1589 _bfd_elf_stringtab_init (void)
1591 struct bfd_strtab_hash
*ret
;
1593 ret
= _bfd_stringtab_init ();
1598 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1599 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1600 if (loc
== (bfd_size_type
) -1)
1602 _bfd_stringtab_free (ret
);
1609 /* ELF .o/exec file reading */
1611 /* Create a new bfd section from an ELF section header. */
1614 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1616 Elf_Internal_Shdr
*hdr
;
1617 Elf_Internal_Ehdr
*ehdr
;
1618 const struct elf_backend_data
*bed
;
1620 bfd_boolean ret
= TRUE
;
1621 static bfd_boolean
* sections_being_created
= NULL
;
1622 static bfd
* sections_being_created_abfd
= NULL
;
1623 static unsigned int nesting
= 0;
1625 if (shindex
>= elf_numsections (abfd
))
1630 /* PR17512: A corrupt ELF binary might contain a recursive group of
1631 sections, with each the string indicies pointing to the next in the
1632 loop. Detect this here, by refusing to load a section that we are
1633 already in the process of loading. We only trigger this test if
1634 we have nested at least three sections deep as normal ELF binaries
1635 can expect to recurse at least once.
1637 FIXME: It would be better if this array was attached to the bfd,
1638 rather than being held in a static pointer. */
1640 if (sections_being_created_abfd
!= abfd
)
1641 sections_being_created
= NULL
;
1642 if (sections_being_created
== NULL
)
1644 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1645 sections_being_created
= (bfd_boolean
*)
1646 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1647 sections_being_created_abfd
= abfd
;
1649 if (sections_being_created
[shindex
])
1651 (*_bfd_error_handler
)
1652 (_("%B: warning: loop in section dependencies detected"), abfd
);
1655 sections_being_created
[shindex
] = TRUE
;
1658 hdr
= elf_elfsections (abfd
)[shindex
];
1659 ehdr
= elf_elfheader (abfd
);
1660 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1665 bed
= get_elf_backend_data (abfd
);
1666 switch (hdr
->sh_type
)
1669 /* Inactive section. Throw it away. */
1672 case SHT_PROGBITS
: /* Normal section with contents. */
1673 case SHT_NOBITS
: /* .bss section. */
1674 case SHT_HASH
: /* .hash section. */
1675 case SHT_NOTE
: /* .note section. */
1676 case SHT_INIT_ARRAY
: /* .init_array section. */
1677 case SHT_FINI_ARRAY
: /* .fini_array section. */
1678 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1679 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1680 case SHT_GNU_HASH
: /* .gnu.hash section. */
1681 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1684 case SHT_DYNAMIC
: /* Dynamic linking information. */
1685 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1688 if (hdr
->sh_link
> elf_numsections (abfd
))
1690 /* PR 10478: Accept Solaris binaries with a sh_link
1691 field set to SHN_BEFORE or SHN_AFTER. */
1692 switch (bfd_get_arch (abfd
))
1695 case bfd_arch_sparc
:
1696 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1697 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1699 /* Otherwise fall through. */
1704 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1706 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1708 Elf_Internal_Shdr
*dynsymhdr
;
1710 /* The shared libraries distributed with hpux11 have a bogus
1711 sh_link field for the ".dynamic" section. Find the
1712 string table for the ".dynsym" section instead. */
1713 if (elf_dynsymtab (abfd
) != 0)
1715 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1716 hdr
->sh_link
= dynsymhdr
->sh_link
;
1720 unsigned int i
, num_sec
;
1722 num_sec
= elf_numsections (abfd
);
1723 for (i
= 1; i
< num_sec
; i
++)
1725 dynsymhdr
= elf_elfsections (abfd
)[i
];
1726 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1728 hdr
->sh_link
= dynsymhdr
->sh_link
;
1736 case SHT_SYMTAB
: /* A symbol table. */
1737 if (elf_onesymtab (abfd
) == shindex
)
1740 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1743 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1745 if (hdr
->sh_size
!= 0)
1747 /* Some assemblers erroneously set sh_info to one with a
1748 zero sh_size. ld sees this as a global symbol count
1749 of (unsigned) -1. Fix it here. */
1754 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1755 elf_onesymtab (abfd
) = shindex
;
1756 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1757 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1758 abfd
->flags
|= HAS_SYMS
;
1760 /* Sometimes a shared object will map in the symbol table. If
1761 SHF_ALLOC is set, and this is a shared object, then we also
1762 treat this section as a BFD section. We can not base the
1763 decision purely on SHF_ALLOC, because that flag is sometimes
1764 set in a relocatable object file, which would confuse the
1766 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1767 && (abfd
->flags
& DYNAMIC
) != 0
1768 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1772 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1773 can't read symbols without that section loaded as well. It
1774 is most likely specified by the next section header. */
1775 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1777 unsigned int i
, num_sec
;
1779 num_sec
= elf_numsections (abfd
);
1780 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1782 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1783 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1784 && hdr2
->sh_link
== shindex
)
1788 for (i
= 1; i
< shindex
; i
++)
1790 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1791 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1792 && hdr2
->sh_link
== shindex
)
1796 ret
= bfd_section_from_shdr (abfd
, i
);
1800 case SHT_DYNSYM
: /* A dynamic symbol table. */
1801 if (elf_dynsymtab (abfd
) == shindex
)
1804 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1807 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1809 if (hdr
->sh_size
!= 0)
1812 /* Some linkers erroneously set sh_info to one with a
1813 zero sh_size. ld sees this as a global symbol count
1814 of (unsigned) -1. Fix it here. */
1819 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1820 elf_dynsymtab (abfd
) = shindex
;
1821 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1822 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1823 abfd
->flags
|= HAS_SYMS
;
1825 /* Besides being a symbol table, we also treat this as a regular
1826 section, so that objcopy can handle it. */
1827 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1830 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
1831 if (elf_symtab_shndx (abfd
) == shindex
)
1834 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1835 elf_symtab_shndx (abfd
) = shindex
;
1836 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1837 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1840 case SHT_STRTAB
: /* A string table. */
1841 if (hdr
->bfd_section
!= NULL
)
1844 if (ehdr
->e_shstrndx
== shindex
)
1846 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1847 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1851 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1854 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1855 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1859 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1862 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1863 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1864 elf_elfsections (abfd
)[shindex
] = hdr
;
1865 /* We also treat this as a regular section, so that objcopy
1867 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1872 /* If the string table isn't one of the above, then treat it as a
1873 regular section. We need to scan all the headers to be sure,
1874 just in case this strtab section appeared before the above. */
1875 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1877 unsigned int i
, num_sec
;
1879 num_sec
= elf_numsections (abfd
);
1880 for (i
= 1; i
< num_sec
; i
++)
1882 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1883 if (hdr2
->sh_link
== shindex
)
1885 /* Prevent endless recursion on broken objects. */
1888 if (! bfd_section_from_shdr (abfd
, i
))
1890 if (elf_onesymtab (abfd
) == i
)
1892 if (elf_dynsymtab (abfd
) == i
)
1893 goto dynsymtab_strtab
;
1897 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1902 /* *These* do a lot of work -- but build no sections! */
1904 asection
*target_sect
;
1905 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1906 unsigned int num_sec
= elf_numsections (abfd
);
1907 struct bfd_elf_section_data
*esdt
;
1911 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1912 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1915 /* Check for a bogus link to avoid crashing. */
1916 if (hdr
->sh_link
>= num_sec
)
1918 ((*_bfd_error_handler
)
1919 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1920 abfd
, hdr
->sh_link
, name
, shindex
));
1921 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1926 /* For some incomprehensible reason Oracle distributes
1927 libraries for Solaris in which some of the objects have
1928 bogus sh_link fields. It would be nice if we could just
1929 reject them, but, unfortunately, some people need to use
1930 them. We scan through the section headers; if we find only
1931 one suitable symbol table, we clobber the sh_link to point
1932 to it. I hope this doesn't break anything.
1934 Don't do it on executable nor shared library. */
1935 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1936 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1937 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1943 for (scan
= 1; scan
< num_sec
; scan
++)
1945 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1946 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1957 hdr
->sh_link
= found
;
1960 /* Get the symbol table. */
1961 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1962 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1963 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1966 /* If this reloc section does not use the main symbol table we
1967 don't treat it as a reloc section. BFD can't adequately
1968 represent such a section, so at least for now, we don't
1969 try. We just present it as a normal section. We also
1970 can't use it as a reloc section if it points to the null
1971 section, an invalid section, another reloc section, or its
1972 sh_link points to the null section. */
1973 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1974 || hdr
->sh_link
== SHN_UNDEF
1975 || hdr
->sh_info
== SHN_UNDEF
1976 || hdr
->sh_info
>= num_sec
1977 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1978 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1980 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1985 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1988 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1989 if (target_sect
== NULL
)
1992 esdt
= elf_section_data (target_sect
);
1993 if (hdr
->sh_type
== SHT_RELA
)
1994 p_hdr
= &esdt
->rela
.hdr
;
1996 p_hdr
= &esdt
->rel
.hdr
;
1998 /* PR 17512: file: 0b4f81b7. */
2001 amt
= sizeof (*hdr2
);
2002 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2007 elf_elfsections (abfd
)[shindex
] = hdr2
;
2008 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2009 target_sect
->flags
|= SEC_RELOC
;
2010 target_sect
->relocation
= NULL
;
2011 target_sect
->rel_filepos
= hdr
->sh_offset
;
2012 /* In the section to which the relocations apply, mark whether
2013 its relocations are of the REL or RELA variety. */
2014 if (hdr
->sh_size
!= 0)
2016 if (hdr
->sh_type
== SHT_RELA
)
2017 target_sect
->use_rela_p
= 1;
2019 abfd
->flags
|= HAS_RELOC
;
2023 case SHT_GNU_verdef
:
2024 elf_dynverdef (abfd
) = shindex
;
2025 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2026 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2029 case SHT_GNU_versym
:
2030 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2033 elf_dynversym (abfd
) = shindex
;
2034 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2035 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2038 case SHT_GNU_verneed
:
2039 elf_dynverref (abfd
) = shindex
;
2040 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2041 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2048 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2051 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2054 if (hdr
->contents
!= NULL
)
2056 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2057 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2062 if (idx
->flags
& GRP_COMDAT
)
2063 hdr
->bfd_section
->flags
2064 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2066 /* We try to keep the same section order as it comes in. */
2069 while (--n_elt
!= 0)
2073 if (idx
->shdr
!= NULL
2074 && (s
= idx
->shdr
->bfd_section
) != NULL
2075 && elf_next_in_group (s
) != NULL
)
2077 elf_next_in_group (hdr
->bfd_section
) = s
;
2085 /* Possibly an attributes section. */
2086 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2087 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2089 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2091 _bfd_elf_parse_attributes (abfd
, hdr
);
2095 /* Check for any processor-specific section types. */
2096 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2099 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2101 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2102 /* FIXME: How to properly handle allocated section reserved
2103 for applications? */
2104 (*_bfd_error_handler
)
2105 (_("%B: don't know how to handle allocated, application "
2106 "specific section `%s' [0x%8x]"),
2107 abfd
, name
, hdr
->sh_type
);
2110 /* Allow sections reserved for applications. */
2111 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2116 else if (hdr
->sh_type
>= SHT_LOPROC
2117 && hdr
->sh_type
<= SHT_HIPROC
)
2118 /* FIXME: We should handle this section. */
2119 (*_bfd_error_handler
)
2120 (_("%B: don't know how to handle processor specific section "
2122 abfd
, name
, hdr
->sh_type
);
2123 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2125 /* Unrecognised OS-specific sections. */
2126 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2127 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2128 required to correctly process the section and the file should
2129 be rejected with an error message. */
2130 (*_bfd_error_handler
)
2131 (_("%B: don't know how to handle OS specific section "
2133 abfd
, name
, hdr
->sh_type
);
2136 /* Otherwise it should be processed. */
2137 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2142 /* FIXME: We should handle this section. */
2143 (*_bfd_error_handler
)
2144 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2145 abfd
, name
, hdr
->sh_type
);
2153 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2154 sections_being_created
[shindex
] = FALSE
;
2155 if (-- nesting
== 0)
2157 sections_being_created
= NULL
;
2158 sections_being_created_abfd
= abfd
;
2163 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2166 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2168 unsigned long r_symndx
)
2170 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2172 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2174 Elf_Internal_Shdr
*symtab_hdr
;
2175 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2176 Elf_External_Sym_Shndx eshndx
;
2178 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2179 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2180 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2183 if (cache
->abfd
!= abfd
)
2185 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2188 cache
->indx
[ent
] = r_symndx
;
2191 return &cache
->sym
[ent
];
2194 /* Given an ELF section number, retrieve the corresponding BFD
2198 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2200 if (sec_index
>= elf_numsections (abfd
))
2202 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2205 static const struct bfd_elf_special_section special_sections_b
[] =
2207 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2208 { NULL
, 0, 0, 0, 0 }
2211 static const struct bfd_elf_special_section special_sections_c
[] =
2213 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2214 { NULL
, 0, 0, 0, 0 }
2217 static const struct bfd_elf_special_section special_sections_d
[] =
2219 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2220 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2221 /* There are more DWARF sections than these, but they needn't be added here
2222 unless you have to cope with broken compilers that don't emit section
2223 attributes or you want to help the user writing assembler. */
2224 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2225 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2226 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2227 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2228 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2229 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2230 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2231 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2232 { NULL
, 0, 0, 0, 0 }
2235 static const struct bfd_elf_special_section special_sections_f
[] =
2237 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2238 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2239 { NULL
, 0, 0, 0, 0 }
2242 static const struct bfd_elf_special_section special_sections_g
[] =
2244 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2245 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2246 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2247 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2248 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2249 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2250 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2251 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2252 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2253 { NULL
, 0, 0, 0, 0 }
2256 static const struct bfd_elf_special_section special_sections_h
[] =
2258 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2259 { NULL
, 0, 0, 0, 0 }
2262 static const struct bfd_elf_special_section special_sections_i
[] =
2264 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2265 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2266 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2267 { NULL
, 0, 0, 0, 0 }
2270 static const struct bfd_elf_special_section special_sections_l
[] =
2272 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2273 { NULL
, 0, 0, 0, 0 }
2276 static const struct bfd_elf_special_section special_sections_n
[] =
2278 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2279 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2280 { NULL
, 0, 0, 0, 0 }
2283 static const struct bfd_elf_special_section special_sections_p
[] =
2285 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2286 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2287 { NULL
, 0, 0, 0, 0 }
2290 static const struct bfd_elf_special_section special_sections_r
[] =
2292 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2293 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2294 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2295 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2296 { NULL
, 0, 0, 0, 0 }
2299 static const struct bfd_elf_special_section special_sections_s
[] =
2301 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2302 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2303 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2304 /* See struct bfd_elf_special_section declaration for the semantics of
2305 this special case where .prefix_length != strlen (.prefix). */
2306 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2307 { NULL
, 0, 0, 0, 0 }
2310 static const struct bfd_elf_special_section special_sections_t
[] =
2312 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2313 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2314 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2315 { NULL
, 0, 0, 0, 0 }
2318 static const struct bfd_elf_special_section special_sections_z
[] =
2320 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2321 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2322 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2323 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2324 { NULL
, 0, 0, 0, 0 }
2327 static const struct bfd_elf_special_section
* const special_sections
[] =
2329 special_sections_b
, /* 'b' */
2330 special_sections_c
, /* 'c' */
2331 special_sections_d
, /* 'd' */
2333 special_sections_f
, /* 'f' */
2334 special_sections_g
, /* 'g' */
2335 special_sections_h
, /* 'h' */
2336 special_sections_i
, /* 'i' */
2339 special_sections_l
, /* 'l' */
2341 special_sections_n
, /* 'n' */
2343 special_sections_p
, /* 'p' */
2345 special_sections_r
, /* 'r' */
2346 special_sections_s
, /* 's' */
2347 special_sections_t
, /* 't' */
2353 special_sections_z
/* 'z' */
2356 const struct bfd_elf_special_section
*
2357 _bfd_elf_get_special_section (const char *name
,
2358 const struct bfd_elf_special_section
*spec
,
2364 len
= strlen (name
);
2366 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2369 int prefix_len
= spec
[i
].prefix_length
;
2371 if (len
< prefix_len
)
2373 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2376 suffix_len
= spec
[i
].suffix_length
;
2377 if (suffix_len
<= 0)
2379 if (name
[prefix_len
] != 0)
2381 if (suffix_len
== 0)
2383 if (name
[prefix_len
] != '.'
2384 && (suffix_len
== -2
2385 || (rela
&& spec
[i
].type
== SHT_REL
)))
2391 if (len
< prefix_len
+ suffix_len
)
2393 if (memcmp (name
+ len
- suffix_len
,
2394 spec
[i
].prefix
+ prefix_len
,
2404 const struct bfd_elf_special_section
*
2405 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2408 const struct bfd_elf_special_section
*spec
;
2409 const struct elf_backend_data
*bed
;
2411 /* See if this is one of the special sections. */
2412 if (sec
->name
== NULL
)
2415 bed
= get_elf_backend_data (abfd
);
2416 spec
= bed
->special_sections
;
2419 spec
= _bfd_elf_get_special_section (sec
->name
,
2420 bed
->special_sections
,
2426 if (sec
->name
[0] != '.')
2429 i
= sec
->name
[1] - 'b';
2430 if (i
< 0 || i
> 'z' - 'b')
2433 spec
= special_sections
[i
];
2438 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2442 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2444 struct bfd_elf_section_data
*sdata
;
2445 const struct elf_backend_data
*bed
;
2446 const struct bfd_elf_special_section
*ssect
;
2448 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2451 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2455 sec
->used_by_bfd
= sdata
;
2458 /* Indicate whether or not this section should use RELA relocations. */
2459 bed
= get_elf_backend_data (abfd
);
2460 sec
->use_rela_p
= bed
->default_use_rela_p
;
2462 /* When we read a file, we don't need to set ELF section type and
2463 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2464 anyway. We will set ELF section type and flags for all linker
2465 created sections. If user specifies BFD section flags, we will
2466 set ELF section type and flags based on BFD section flags in
2467 elf_fake_sections. Special handling for .init_array/.fini_array
2468 output sections since they may contain .ctors/.dtors input
2469 sections. We don't want _bfd_elf_init_private_section_data to
2470 copy ELF section type from .ctors/.dtors input sections. */
2471 if (abfd
->direction
!= read_direction
2472 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2474 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2477 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2478 || ssect
->type
== SHT_INIT_ARRAY
2479 || ssect
->type
== SHT_FINI_ARRAY
))
2481 elf_section_type (sec
) = ssect
->type
;
2482 elf_section_flags (sec
) = ssect
->attr
;
2486 return _bfd_generic_new_section_hook (abfd
, sec
);
2489 /* Create a new bfd section from an ELF program header.
2491 Since program segments have no names, we generate a synthetic name
2492 of the form segment<NUM>, where NUM is generally the index in the
2493 program header table. For segments that are split (see below) we
2494 generate the names segment<NUM>a and segment<NUM>b.
2496 Note that some program segments may have a file size that is different than
2497 (less than) the memory size. All this means is that at execution the
2498 system must allocate the amount of memory specified by the memory size,
2499 but only initialize it with the first "file size" bytes read from the
2500 file. This would occur for example, with program segments consisting
2501 of combined data+bss.
2503 To handle the above situation, this routine generates TWO bfd sections
2504 for the single program segment. The first has the length specified by
2505 the file size of the segment, and the second has the length specified
2506 by the difference between the two sizes. In effect, the segment is split
2507 into its initialized and uninitialized parts.
2512 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2513 Elf_Internal_Phdr
*hdr
,
2515 const char *type_name
)
2523 split
= ((hdr
->p_memsz
> 0)
2524 && (hdr
->p_filesz
> 0)
2525 && (hdr
->p_memsz
> hdr
->p_filesz
));
2527 if (hdr
->p_filesz
> 0)
2529 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2530 len
= strlen (namebuf
) + 1;
2531 name
= (char *) bfd_alloc (abfd
, len
);
2534 memcpy (name
, namebuf
, len
);
2535 newsect
= bfd_make_section (abfd
, name
);
2536 if (newsect
== NULL
)
2538 newsect
->vma
= hdr
->p_vaddr
;
2539 newsect
->lma
= hdr
->p_paddr
;
2540 newsect
->size
= hdr
->p_filesz
;
2541 newsect
->filepos
= hdr
->p_offset
;
2542 newsect
->flags
|= SEC_HAS_CONTENTS
;
2543 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2544 if (hdr
->p_type
== PT_LOAD
)
2546 newsect
->flags
|= SEC_ALLOC
;
2547 newsect
->flags
|= SEC_LOAD
;
2548 if (hdr
->p_flags
& PF_X
)
2550 /* FIXME: all we known is that it has execute PERMISSION,
2552 newsect
->flags
|= SEC_CODE
;
2555 if (!(hdr
->p_flags
& PF_W
))
2557 newsect
->flags
|= SEC_READONLY
;
2561 if (hdr
->p_memsz
> hdr
->p_filesz
)
2565 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2566 len
= strlen (namebuf
) + 1;
2567 name
= (char *) bfd_alloc (abfd
, len
);
2570 memcpy (name
, namebuf
, len
);
2571 newsect
= bfd_make_section (abfd
, name
);
2572 if (newsect
== NULL
)
2574 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2575 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2576 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2577 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2578 align
= newsect
->vma
& -newsect
->vma
;
2579 if (align
== 0 || align
> hdr
->p_align
)
2580 align
= hdr
->p_align
;
2581 newsect
->alignment_power
= bfd_log2 (align
);
2582 if (hdr
->p_type
== PT_LOAD
)
2584 /* Hack for gdb. Segments that have not been modified do
2585 not have their contents written to a core file, on the
2586 assumption that a debugger can find the contents in the
2587 executable. We flag this case by setting the fake
2588 section size to zero. Note that "real" bss sections will
2589 always have their contents dumped to the core file. */
2590 if (bfd_get_format (abfd
) == bfd_core
)
2592 newsect
->flags
|= SEC_ALLOC
;
2593 if (hdr
->p_flags
& PF_X
)
2594 newsect
->flags
|= SEC_CODE
;
2596 if (!(hdr
->p_flags
& PF_W
))
2597 newsect
->flags
|= SEC_READONLY
;
2604 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2606 const struct elf_backend_data
*bed
;
2608 switch (hdr
->p_type
)
2611 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2614 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2617 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2620 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2623 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2625 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2630 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2633 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2635 case PT_GNU_EH_FRAME
:
2636 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2640 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2643 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2646 /* Check for any processor-specific program segment types. */
2647 bed
= get_elf_backend_data (abfd
);
2648 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2652 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2656 _bfd_elf_single_rel_hdr (asection
*sec
)
2658 if (elf_section_data (sec
)->rel
.hdr
)
2660 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2661 return elf_section_data (sec
)->rel
.hdr
;
2664 return elf_section_data (sec
)->rela
.hdr
;
2667 /* Allocate and initialize a section-header for a new reloc section,
2668 containing relocations against ASECT. It is stored in RELDATA. If
2669 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2673 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2674 struct bfd_elf_section_reloc_data
*reldata
,
2676 bfd_boolean use_rela_p
)
2678 Elf_Internal_Shdr
*rel_hdr
;
2680 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2683 amt
= sizeof (Elf_Internal_Shdr
);
2684 BFD_ASSERT (reldata
->hdr
== NULL
);
2685 rel_hdr
= bfd_zalloc (abfd
, amt
);
2686 reldata
->hdr
= rel_hdr
;
2688 amt
= sizeof ".rela" + strlen (asect
->name
);
2689 name
= (char *) bfd_alloc (abfd
, amt
);
2692 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2694 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2696 if (rel_hdr
->sh_name
== (unsigned int) -1)
2698 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2699 rel_hdr
->sh_entsize
= (use_rela_p
2700 ? bed
->s
->sizeof_rela
2701 : bed
->s
->sizeof_rel
);
2702 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2703 rel_hdr
->sh_flags
= 0;
2704 rel_hdr
->sh_addr
= 0;
2705 rel_hdr
->sh_size
= 0;
2706 rel_hdr
->sh_offset
= 0;
2711 /* Return the default section type based on the passed in section flags. */
2714 bfd_elf_get_default_section_type (flagword flags
)
2716 if ((flags
& SEC_ALLOC
) != 0
2717 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2719 return SHT_PROGBITS
;
2722 struct fake_section_arg
2724 struct bfd_link_info
*link_info
;
2728 /* Set up an ELF internal section header for a section. */
2731 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2733 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2734 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2735 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2736 Elf_Internal_Shdr
*this_hdr
;
2737 unsigned int sh_type
;
2741 /* We already failed; just get out of the bfd_map_over_sections
2746 this_hdr
= &esd
->this_hdr
;
2748 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2749 asect
->name
, FALSE
);
2750 if (this_hdr
->sh_name
== (unsigned int) -1)
2756 /* Don't clear sh_flags. Assembler may set additional bits. */
2758 if ((asect
->flags
& SEC_ALLOC
) != 0
2759 || asect
->user_set_vma
)
2760 this_hdr
->sh_addr
= asect
->vma
;
2762 this_hdr
->sh_addr
= 0;
2764 this_hdr
->sh_offset
= 0;
2765 this_hdr
->sh_size
= asect
->size
;
2766 this_hdr
->sh_link
= 0;
2767 /* PR 17512: file: 0eb809fe, 8b0535ee. */
2768 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
2770 (*_bfd_error_handler
)
2771 (_("%B: error: Alignment power %d of section `%A' is too big"),
2772 abfd
, asect
, asect
->alignment_power
);
2776 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2777 /* The sh_entsize and sh_info fields may have been set already by
2778 copy_private_section_data. */
2780 this_hdr
->bfd_section
= asect
;
2781 this_hdr
->contents
= NULL
;
2783 /* If the section type is unspecified, we set it based on
2785 if ((asect
->flags
& SEC_GROUP
) != 0)
2786 sh_type
= SHT_GROUP
;
2788 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2790 if (this_hdr
->sh_type
== SHT_NULL
)
2791 this_hdr
->sh_type
= sh_type
;
2792 else if (this_hdr
->sh_type
== SHT_NOBITS
2793 && sh_type
== SHT_PROGBITS
2794 && (asect
->flags
& SEC_ALLOC
) != 0)
2796 /* Warn if we are changing a NOBITS section to PROGBITS, but
2797 allow the link to proceed. This can happen when users link
2798 non-bss input sections to bss output sections, or emit data
2799 to a bss output section via a linker script. */
2800 (*_bfd_error_handler
)
2801 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2802 this_hdr
->sh_type
= sh_type
;
2805 switch (this_hdr
->sh_type
)
2811 case SHT_INIT_ARRAY
:
2812 case SHT_FINI_ARRAY
:
2813 case SHT_PREINIT_ARRAY
:
2820 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2824 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2828 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2832 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2833 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2837 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2838 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2841 case SHT_GNU_versym
:
2842 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2845 case SHT_GNU_verdef
:
2846 this_hdr
->sh_entsize
= 0;
2847 /* objcopy or strip will copy over sh_info, but may not set
2848 cverdefs. The linker will set cverdefs, but sh_info will be
2850 if (this_hdr
->sh_info
== 0)
2851 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2853 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2854 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2857 case SHT_GNU_verneed
:
2858 this_hdr
->sh_entsize
= 0;
2859 /* objcopy or strip will copy over sh_info, but may not set
2860 cverrefs. The linker will set cverrefs, but sh_info will be
2862 if (this_hdr
->sh_info
== 0)
2863 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2865 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2866 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2870 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2874 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2878 if ((asect
->flags
& SEC_ALLOC
) != 0)
2879 this_hdr
->sh_flags
|= SHF_ALLOC
;
2880 if ((asect
->flags
& SEC_READONLY
) == 0)
2881 this_hdr
->sh_flags
|= SHF_WRITE
;
2882 if ((asect
->flags
& SEC_CODE
) != 0)
2883 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2884 if ((asect
->flags
& SEC_MERGE
) != 0)
2886 this_hdr
->sh_flags
|= SHF_MERGE
;
2887 this_hdr
->sh_entsize
= asect
->entsize
;
2888 if ((asect
->flags
& SEC_STRINGS
) != 0)
2889 this_hdr
->sh_flags
|= SHF_STRINGS
;
2891 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2892 this_hdr
->sh_flags
|= SHF_GROUP
;
2893 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2895 this_hdr
->sh_flags
|= SHF_TLS
;
2896 if (asect
->size
== 0
2897 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2899 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2901 this_hdr
->sh_size
= 0;
2904 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2905 if (this_hdr
->sh_size
!= 0)
2906 this_hdr
->sh_type
= SHT_NOBITS
;
2910 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2911 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2913 /* If the section has relocs, set up a section header for the
2914 SHT_REL[A] section. If two relocation sections are required for
2915 this section, it is up to the processor-specific back-end to
2916 create the other. */
2917 if ((asect
->flags
& SEC_RELOC
) != 0)
2919 /* When doing a relocatable link, create both REL and RELA sections if
2922 /* Do the normal setup if we wouldn't create any sections here. */
2923 && esd
->rel
.count
+ esd
->rela
.count
> 0
2924 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2926 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2927 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2932 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2933 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2939 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2941 ? &esd
->rela
: &esd
->rel
),
2947 /* Check for processor-specific section types. */
2948 sh_type
= this_hdr
->sh_type
;
2949 if (bed
->elf_backend_fake_sections
2950 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2953 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2955 /* Don't change the header type from NOBITS if we are being
2956 called for objcopy --only-keep-debug. */
2957 this_hdr
->sh_type
= sh_type
;
2961 /* Fill in the contents of a SHT_GROUP section. Called from
2962 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2963 when ELF targets use the generic linker, ld. Called for ld -r
2964 from bfd_elf_final_link. */
2967 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2969 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2970 asection
*elt
, *first
;
2974 /* Ignore linker created group section. See elfNN_ia64_object_p in
2976 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2980 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2982 unsigned long symindx
= 0;
2984 /* elf_group_id will have been set up by objcopy and the
2986 if (elf_group_id (sec
) != NULL
)
2987 symindx
= elf_group_id (sec
)->udata
.i
;
2991 /* If called from the assembler, swap_out_syms will have set up
2992 elf_section_syms. */
2993 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2994 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2996 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2998 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3000 /* The ELF backend linker sets sh_info to -2 when the group
3001 signature symbol is global, and thus the index can't be
3002 set until all local symbols are output. */
3003 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
3004 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
3005 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
3006 unsigned long extsymoff
= 0;
3007 struct elf_link_hash_entry
*h
;
3009 if (!elf_bad_symtab (igroup
->owner
))
3011 Elf_Internal_Shdr
*symtab_hdr
;
3013 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3014 extsymoff
= symtab_hdr
->sh_info
;
3016 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3017 while (h
->root
.type
== bfd_link_hash_indirect
3018 || h
->root
.type
== bfd_link_hash_warning
)
3019 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3021 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3024 /* The contents won't be allocated for "ld -r" or objcopy. */
3026 if (sec
->contents
== NULL
)
3029 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3031 /* Arrange for the section to be written out. */
3032 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3033 if (sec
->contents
== NULL
)
3040 loc
= sec
->contents
+ sec
->size
;
3042 /* Get the pointer to the first section in the group that gas
3043 squirreled away here. objcopy arranges for this to be set to the
3044 start of the input section group. */
3045 first
= elt
= elf_next_in_group (sec
);
3047 /* First element is a flag word. Rest of section is elf section
3048 indices for all the sections of the group. Write them backwards
3049 just to keep the group in the same order as given in .section
3050 directives, not that it matters. */
3057 s
= s
->output_section
;
3059 && !bfd_is_abs_section (s
))
3061 unsigned int idx
= elf_section_data (s
)->this_idx
;
3064 H_PUT_32 (abfd
, idx
, loc
);
3066 elt
= elf_next_in_group (elt
);
3071 if ((loc
-= 4) != sec
->contents
)
3074 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3077 /* Assign all ELF section numbers. The dummy first section is handled here
3078 too. The link/info pointers for the standard section types are filled
3079 in here too, while we're at it. */
3082 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3084 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3086 unsigned int section_number
, secn
;
3087 Elf_Internal_Shdr
**i_shdrp
;
3088 struct bfd_elf_section_data
*d
;
3089 bfd_boolean need_symtab
;
3093 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3095 /* SHT_GROUP sections are in relocatable files only. */
3096 if (link_info
== NULL
|| link_info
->relocatable
)
3098 /* Put SHT_GROUP sections first. */
3099 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3101 d
= elf_section_data (sec
);
3103 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3105 if (sec
->flags
& SEC_LINKER_CREATED
)
3107 /* Remove the linker created SHT_GROUP sections. */
3108 bfd_section_list_remove (abfd
, sec
);
3109 abfd
->section_count
--;
3112 d
->this_idx
= section_number
++;
3117 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3119 d
= elf_section_data (sec
);
3121 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3122 d
->this_idx
= section_number
++;
3123 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3126 d
->rel
.idx
= section_number
++;
3127 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3134 d
->rela
.idx
= section_number
++;
3135 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3141 elf_shstrtab_sec (abfd
) = section_number
++;
3142 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3143 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3145 need_symtab
= (bfd_get_symcount (abfd
) > 0
3146 || (link_info
== NULL
3147 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3151 elf_onesymtab (abfd
) = section_number
++;
3152 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3153 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3155 elf_symtab_shndx (abfd
) = section_number
++;
3156 t
->symtab_shndx_hdr
.sh_name
3157 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3158 ".symtab_shndx", FALSE
);
3159 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3162 elf_strtab_sec (abfd
) = section_number
++;
3163 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3166 if (section_number
>= SHN_LORESERVE
)
3168 _bfd_error_handler (_("%B: too many sections: %u"),
3169 abfd
, section_number
);
3173 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
3174 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3176 elf_numsections (abfd
) = section_number
;
3177 elf_elfheader (abfd
)->e_shnum
= section_number
;
3179 /* Set up the list of section header pointers, in agreement with the
3181 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3182 sizeof (Elf_Internal_Shdr
*));
3183 if (i_shdrp
== NULL
)
3186 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3187 sizeof (Elf_Internal_Shdr
));
3188 if (i_shdrp
[0] == NULL
)
3190 bfd_release (abfd
, i_shdrp
);
3194 elf_elfsections (abfd
) = i_shdrp
;
3196 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3199 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3200 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3202 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3203 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3205 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3206 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3209 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3214 d
= elf_section_data (sec
);
3216 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3217 if (d
->rel
.idx
!= 0)
3218 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3219 if (d
->rela
.idx
!= 0)
3220 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3222 /* Fill in the sh_link and sh_info fields while we're at it. */
3224 /* sh_link of a reloc section is the section index of the symbol
3225 table. sh_info is the section index of the section to which
3226 the relocation entries apply. */
3227 if (d
->rel
.idx
!= 0)
3229 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3230 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3231 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3233 if (d
->rela
.idx
!= 0)
3235 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3236 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3237 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3240 /* We need to set up sh_link for SHF_LINK_ORDER. */
3241 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3243 s
= elf_linked_to_section (sec
);
3246 /* elf_linked_to_section points to the input section. */
3247 if (link_info
!= NULL
)
3249 /* Check discarded linkonce section. */
3250 if (discarded_section (s
))
3253 (*_bfd_error_handler
)
3254 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3255 abfd
, d
->this_hdr
.bfd_section
,
3257 /* Point to the kept section if it has the same
3258 size as the discarded one. */
3259 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3262 bfd_set_error (bfd_error_bad_value
);
3268 s
= s
->output_section
;
3269 BFD_ASSERT (s
!= NULL
);
3273 /* Handle objcopy. */
3274 if (s
->output_section
== NULL
)
3276 (*_bfd_error_handler
)
3277 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3278 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3279 bfd_set_error (bfd_error_bad_value
);
3282 s
= s
->output_section
;
3284 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3289 The Intel C compiler generates SHT_IA_64_UNWIND with
3290 SHF_LINK_ORDER. But it doesn't set the sh_link or
3291 sh_info fields. Hence we could get the situation
3293 const struct elf_backend_data
*bed
3294 = get_elf_backend_data (abfd
);
3295 if (bed
->link_order_error_handler
)
3296 bed
->link_order_error_handler
3297 (_("%B: warning: sh_link not set for section `%A'"),
3302 switch (d
->this_hdr
.sh_type
)
3306 /* A reloc section which we are treating as a normal BFD
3307 section. sh_link is the section index of the symbol
3308 table. sh_info is the section index of the section to
3309 which the relocation entries apply. We assume that an
3310 allocated reloc section uses the dynamic symbol table.
3311 FIXME: How can we be sure? */
3312 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3314 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3316 /* We look up the section the relocs apply to by name. */
3318 if (d
->this_hdr
.sh_type
== SHT_REL
)
3322 s
= bfd_get_section_by_name (abfd
, name
);
3325 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3326 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3331 /* We assume that a section named .stab*str is a stabs
3332 string section. We look for a section with the same name
3333 but without the trailing ``str'', and set its sh_link
3334 field to point to this section. */
3335 if (CONST_STRNEQ (sec
->name
, ".stab")
3336 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3341 len
= strlen (sec
->name
);
3342 alc
= (char *) bfd_malloc (len
- 2);
3345 memcpy (alc
, sec
->name
, len
- 3);
3346 alc
[len
- 3] = '\0';
3347 s
= bfd_get_section_by_name (abfd
, alc
);
3351 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3353 /* This is a .stab section. */
3354 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3355 elf_section_data (s
)->this_hdr
.sh_entsize
3356 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3363 case SHT_GNU_verneed
:
3364 case SHT_GNU_verdef
:
3365 /* sh_link is the section header index of the string table
3366 used for the dynamic entries, or the symbol table, or the
3368 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3370 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3373 case SHT_GNU_LIBLIST
:
3374 /* sh_link is the section header index of the prelink library
3375 list used for the dynamic entries, or the symbol table, or
3376 the version strings. */
3377 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3378 ? ".dynstr" : ".gnu.libstr");
3380 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3385 case SHT_GNU_versym
:
3386 /* sh_link is the section header index of the symbol table
3387 this hash table or version table is for. */
3388 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3390 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3394 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3398 for (secn
= 1; secn
< section_number
; ++secn
)
3399 if (i_shdrp
[secn
] == NULL
)
3400 i_shdrp
[secn
] = i_shdrp
[0];
3402 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3403 i_shdrp
[secn
]->sh_name
);
3408 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3410 /* If the backend has a special mapping, use it. */
3411 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3412 if (bed
->elf_backend_sym_is_global
)
3413 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3415 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3416 || bfd_is_und_section (bfd_get_section (sym
))
3417 || bfd_is_com_section (bfd_get_section (sym
)));
3420 /* Don't output section symbols for sections that are not going to be
3421 output, that are duplicates or there is no BFD section. */
3424 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3426 elf_symbol_type
*type_ptr
;
3428 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3431 type_ptr
= elf_symbol_from (abfd
, sym
);
3432 return ((type_ptr
!= NULL
3433 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3434 && bfd_is_abs_section (sym
->section
))
3435 || !(sym
->section
->owner
== abfd
3436 || (sym
->section
->output_section
->owner
== abfd
3437 && sym
->section
->output_offset
== 0)
3438 || bfd_is_abs_section (sym
->section
)));
3441 /* Map symbol from it's internal number to the external number, moving
3442 all local symbols to be at the head of the list. */
3445 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3447 unsigned int symcount
= bfd_get_symcount (abfd
);
3448 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3449 asymbol
**sect_syms
;
3450 unsigned int num_locals
= 0;
3451 unsigned int num_globals
= 0;
3452 unsigned int num_locals2
= 0;
3453 unsigned int num_globals2
= 0;
3460 fprintf (stderr
, "elf_map_symbols\n");
3464 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3466 if (max_index
< asect
->index
)
3467 max_index
= asect
->index
;
3471 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3472 if (sect_syms
== NULL
)
3474 elf_section_syms (abfd
) = sect_syms
;
3475 elf_num_section_syms (abfd
) = max_index
;
3477 /* Init sect_syms entries for any section symbols we have already
3478 decided to output. */
3479 for (idx
= 0; idx
< symcount
; idx
++)
3481 asymbol
*sym
= syms
[idx
];
3483 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3485 && !ignore_section_sym (abfd
, sym
)
3486 && !bfd_is_abs_section (sym
->section
))
3488 asection
*sec
= sym
->section
;
3490 if (sec
->owner
!= abfd
)
3491 sec
= sec
->output_section
;
3493 sect_syms
[sec
->index
] = syms
[idx
];
3497 /* Classify all of the symbols. */
3498 for (idx
= 0; idx
< symcount
; idx
++)
3500 if (sym_is_global (abfd
, syms
[idx
]))
3502 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3506 /* We will be adding a section symbol for each normal BFD section. Most
3507 sections will already have a section symbol in outsymbols, but
3508 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3509 at least in that case. */
3510 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3512 if (sect_syms
[asect
->index
] == NULL
)
3514 if (!sym_is_global (abfd
, asect
->symbol
))
3521 /* Now sort the symbols so the local symbols are first. */
3522 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3523 sizeof (asymbol
*));
3525 if (new_syms
== NULL
)
3528 for (idx
= 0; idx
< symcount
; idx
++)
3530 asymbol
*sym
= syms
[idx
];
3533 if (sym_is_global (abfd
, sym
))
3534 i
= num_locals
+ num_globals2
++;
3535 else if (!ignore_section_sym (abfd
, sym
))
3540 sym
->udata
.i
= i
+ 1;
3542 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3544 if (sect_syms
[asect
->index
] == NULL
)
3546 asymbol
*sym
= asect
->symbol
;
3549 sect_syms
[asect
->index
] = sym
;
3550 if (!sym_is_global (abfd
, sym
))
3553 i
= num_locals
+ num_globals2
++;
3555 sym
->udata
.i
= i
+ 1;
3559 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3561 *pnum_locals
= num_locals
;
3565 /* Align to the maximum file alignment that could be required for any
3566 ELF data structure. */
3568 static inline file_ptr
3569 align_file_position (file_ptr off
, int align
)
3571 return (off
+ align
- 1) & ~(align
- 1);
3574 /* Assign a file position to a section, optionally aligning to the
3575 required section alignment. */
3578 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3582 if (align
&& i_shdrp
->sh_addralign
> 1)
3583 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3584 i_shdrp
->sh_offset
= offset
;
3585 if (i_shdrp
->bfd_section
!= NULL
)
3586 i_shdrp
->bfd_section
->filepos
= offset
;
3587 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3588 offset
+= i_shdrp
->sh_size
;
3592 /* Compute the file positions we are going to put the sections at, and
3593 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3594 is not NULL, this is being called by the ELF backend linker. */
3597 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3598 struct bfd_link_info
*link_info
)
3600 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3601 struct fake_section_arg fsargs
;
3603 struct bfd_strtab_hash
*strtab
= NULL
;
3604 Elf_Internal_Shdr
*shstrtab_hdr
;
3605 bfd_boolean need_symtab
;
3607 if (abfd
->output_has_begun
)
3610 /* Do any elf backend specific processing first. */
3611 if (bed
->elf_backend_begin_write_processing
)
3612 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3614 if (! prep_headers (abfd
))
3617 /* Post process the headers if necessary. */
3618 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3620 fsargs
.failed
= FALSE
;
3621 fsargs
.link_info
= link_info
;
3622 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3626 if (!assign_section_numbers (abfd
, link_info
))
3629 /* The backend linker builds symbol table information itself. */
3630 need_symtab
= (link_info
== NULL
3631 && (bfd_get_symcount (abfd
) > 0
3632 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3636 /* Non-zero if doing a relocatable link. */
3637 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3639 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3644 if (link_info
== NULL
)
3646 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3651 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3652 /* sh_name was set in prep_headers. */
3653 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3654 shstrtab_hdr
->sh_flags
= 0;
3655 shstrtab_hdr
->sh_addr
= 0;
3656 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3657 shstrtab_hdr
->sh_entsize
= 0;
3658 shstrtab_hdr
->sh_link
= 0;
3659 shstrtab_hdr
->sh_info
= 0;
3660 /* sh_offset is set in assign_file_positions_except_relocs. */
3661 shstrtab_hdr
->sh_addralign
= 1;
3663 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3669 Elf_Internal_Shdr
*hdr
;
3671 off
= elf_next_file_pos (abfd
);
3673 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3674 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3676 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3677 if (hdr
->sh_size
!= 0)
3678 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3680 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3681 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3683 elf_next_file_pos (abfd
) = off
;
3685 /* Now that we know where the .strtab section goes, write it
3687 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3688 || ! _bfd_stringtab_emit (abfd
, strtab
))
3690 _bfd_stringtab_free (strtab
);
3693 abfd
->output_has_begun
= TRUE
;
3698 /* Make an initial estimate of the size of the program header. If we
3699 get the number wrong here, we'll redo section placement. */
3701 static bfd_size_type
3702 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3706 const struct elf_backend_data
*bed
;
3708 /* Assume we will need exactly two PT_LOAD segments: one for text
3709 and one for data. */
3712 s
= bfd_get_section_by_name (abfd
, ".interp");
3713 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3715 /* If we have a loadable interpreter section, we need a
3716 PT_INTERP segment. In this case, assume we also need a
3717 PT_PHDR segment, although that may not be true for all
3722 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3724 /* We need a PT_DYNAMIC segment. */
3728 if (info
!= NULL
&& info
->relro
)
3730 /* We need a PT_GNU_RELRO segment. */
3734 if (elf_eh_frame_hdr (abfd
))
3736 /* We need a PT_GNU_EH_FRAME segment. */
3740 if (elf_stack_flags (abfd
))
3742 /* We need a PT_GNU_STACK segment. */
3746 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3748 if ((s
->flags
& SEC_LOAD
) != 0
3749 && CONST_STRNEQ (s
->name
, ".note"))
3751 /* We need a PT_NOTE segment. */
3753 /* Try to create just one PT_NOTE segment
3754 for all adjacent loadable .note* sections.
3755 gABI requires that within a PT_NOTE segment
3756 (and also inside of each SHT_NOTE section)
3757 each note is padded to a multiple of 4 size,
3758 so we check whether the sections are correctly
3760 if (s
->alignment_power
== 2)
3761 while (s
->next
!= NULL
3762 && s
->next
->alignment_power
== 2
3763 && (s
->next
->flags
& SEC_LOAD
) != 0
3764 && CONST_STRNEQ (s
->next
->name
, ".note"))
3769 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3771 if (s
->flags
& SEC_THREAD_LOCAL
)
3773 /* We need a PT_TLS segment. */
3779 /* Let the backend count up any program headers it might need. */
3780 bed
= get_elf_backend_data (abfd
);
3781 if (bed
->elf_backend_additional_program_headers
)
3785 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3791 return segs
* bed
->s
->sizeof_phdr
;
3794 /* Find the segment that contains the output_section of section. */
3797 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3799 struct elf_segment_map
*m
;
3800 Elf_Internal_Phdr
*p
;
3802 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3808 for (i
= m
->count
- 1; i
>= 0; i
--)
3809 if (m
->sections
[i
] == section
)
3816 /* Create a mapping from a set of sections to a program segment. */
3818 static struct elf_segment_map
*
3819 make_mapping (bfd
*abfd
,
3820 asection
**sections
,
3825 struct elf_segment_map
*m
;
3830 amt
= sizeof (struct elf_segment_map
);
3831 amt
+= (to
- from
- 1) * sizeof (asection
*);
3832 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3836 m
->p_type
= PT_LOAD
;
3837 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3838 m
->sections
[i
- from
] = *hdrpp
;
3839 m
->count
= to
- from
;
3841 if (from
== 0 && phdr
)
3843 /* Include the headers in the first PT_LOAD segment. */
3844 m
->includes_filehdr
= 1;
3845 m
->includes_phdrs
= 1;
3851 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3854 struct elf_segment_map
*
3855 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3857 struct elf_segment_map
*m
;
3859 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3860 sizeof (struct elf_segment_map
));
3864 m
->p_type
= PT_DYNAMIC
;
3866 m
->sections
[0] = dynsec
;
3871 /* Possibly add or remove segments from the segment map. */
3874 elf_modify_segment_map (bfd
*abfd
,
3875 struct bfd_link_info
*info
,
3876 bfd_boolean remove_empty_load
)
3878 struct elf_segment_map
**m
;
3879 const struct elf_backend_data
*bed
;
3881 /* The placement algorithm assumes that non allocated sections are
3882 not in PT_LOAD segments. We ensure this here by removing such
3883 sections from the segment map. We also remove excluded
3884 sections. Finally, any PT_LOAD segment without sections is
3886 m
= &elf_seg_map (abfd
);
3889 unsigned int i
, new_count
;
3891 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3893 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3894 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3895 || (*m
)->p_type
!= PT_LOAD
))
3897 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3901 (*m
)->count
= new_count
;
3903 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3909 bed
= get_elf_backend_data (abfd
);
3910 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3912 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3919 /* Set up a mapping from BFD sections to program segments. */
3922 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3925 struct elf_segment_map
*m
;
3926 asection
**sections
= NULL
;
3927 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3928 bfd_boolean no_user_phdrs
;
3930 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
3933 info
->user_phdrs
= !no_user_phdrs
;
3935 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3939 struct elf_segment_map
*mfirst
;
3940 struct elf_segment_map
**pm
;
3943 unsigned int phdr_index
;
3944 bfd_vma maxpagesize
;
3946 bfd_boolean phdr_in_segment
= TRUE
;
3947 bfd_boolean writable
;
3949 asection
*first_tls
= NULL
;
3950 asection
*dynsec
, *eh_frame_hdr
;
3952 bfd_vma addr_mask
, wrap_to
= 0;
3954 /* Select the allocated sections, and sort them. */
3956 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3957 sizeof (asection
*));
3958 if (sections
== NULL
)
3961 /* Calculate top address, avoiding undefined behaviour of shift
3962 left operator when shift count is equal to size of type
3964 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3965 addr_mask
= (addr_mask
<< 1) + 1;
3968 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3970 if ((s
->flags
& SEC_ALLOC
) != 0)
3974 /* A wrapping section potentially clashes with header. */
3975 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3976 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3979 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3982 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3984 /* Build the mapping. */
3989 /* If we have a .interp section, then create a PT_PHDR segment for
3990 the program headers and a PT_INTERP segment for the .interp
3992 s
= bfd_get_section_by_name (abfd
, ".interp");
3993 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3995 amt
= sizeof (struct elf_segment_map
);
3996 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4000 m
->p_type
= PT_PHDR
;
4001 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4002 m
->p_flags
= PF_R
| PF_X
;
4003 m
->p_flags_valid
= 1;
4004 m
->includes_phdrs
= 1;
4009 amt
= sizeof (struct elf_segment_map
);
4010 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4014 m
->p_type
= PT_INTERP
;
4022 /* Look through the sections. We put sections in the same program
4023 segment when the start of the second section can be placed within
4024 a few bytes of the end of the first section. */
4028 maxpagesize
= bed
->maxpagesize
;
4029 /* PR 17512: file: c8455299.
4030 Avoid divide-by-zero errors later on.
4031 FIXME: Should we abort if the maxpagesize is zero ? */
4032 if (maxpagesize
== 0)
4035 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4037 && (dynsec
->flags
& SEC_LOAD
) == 0)
4040 /* Deal with -Ttext or something similar such that the first section
4041 is not adjacent to the program headers. This is an
4042 approximation, since at this point we don't know exactly how many
4043 program headers we will need. */
4046 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4048 if (phdr_size
== (bfd_size_type
) -1)
4049 phdr_size
= get_program_header_size (abfd
, info
);
4050 phdr_size
+= bed
->s
->sizeof_ehdr
;
4051 if ((abfd
->flags
& D_PAGED
) == 0
4052 || (sections
[0]->lma
& addr_mask
) < phdr_size
4053 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4054 < phdr_size
% maxpagesize
)
4055 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4056 phdr_in_segment
= FALSE
;
4059 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4062 bfd_boolean new_segment
;
4066 /* See if this section and the last one will fit in the same
4069 if (last_hdr
== NULL
)
4071 /* If we don't have a segment yet, then we don't need a new
4072 one (we build the last one after this loop). */
4073 new_segment
= FALSE
;
4075 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4077 /* If this section has a different relation between the
4078 virtual address and the load address, then we need a new
4082 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4083 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4085 /* If this section has a load address that makes it overlap
4086 the previous section, then we need a new segment. */
4089 /* In the next test we have to be careful when last_hdr->lma is close
4090 to the end of the address space. If the aligned address wraps
4091 around to the start of the address space, then there are no more
4092 pages left in memory and it is OK to assume that the current
4093 section can be included in the current segment. */
4094 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4096 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4099 /* If putting this section in this segment would force us to
4100 skip a page in the segment, then we need a new segment. */
4103 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4104 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
4106 /* We don't want to put a loadable section after a
4107 nonloadable section in the same segment.
4108 Consider .tbss sections as loadable for this purpose. */
4111 else if ((abfd
->flags
& D_PAGED
) == 0)
4113 /* If the file is not demand paged, which means that we
4114 don't require the sections to be correctly aligned in the
4115 file, then there is no other reason for a new segment. */
4116 new_segment
= FALSE
;
4119 && (hdr
->flags
& SEC_READONLY
) == 0
4120 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4121 != (hdr
->lma
& -maxpagesize
)))
4123 /* We don't want to put a writable section in a read only
4124 segment, unless they are on the same page in memory
4125 anyhow. We already know that the last section does not
4126 bring us past the current section on the page, so the
4127 only case in which the new section is not on the same
4128 page as the previous section is when the previous section
4129 ends precisely on a page boundary. */
4134 /* Otherwise, we can use the same segment. */
4135 new_segment
= FALSE
;
4138 /* Allow interested parties a chance to override our decision. */
4139 if (last_hdr
!= NULL
4141 && info
->callbacks
->override_segment_assignment
!= NULL
)
4143 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4149 if ((hdr
->flags
& SEC_READONLY
) == 0)
4152 /* .tbss sections effectively have zero size. */
4153 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4154 != SEC_THREAD_LOCAL
)
4155 last_size
= hdr
->size
;
4161 /* We need a new program segment. We must create a new program
4162 header holding all the sections from phdr_index until hdr. */
4164 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4171 if ((hdr
->flags
& SEC_READONLY
) == 0)
4177 /* .tbss sections effectively have zero size. */
4178 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4179 last_size
= hdr
->size
;
4183 phdr_in_segment
= FALSE
;
4186 /* Create a final PT_LOAD program segment, but not if it's just
4188 if (last_hdr
!= NULL
4189 && (i
- phdr_index
!= 1
4190 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4191 != SEC_THREAD_LOCAL
)))
4193 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4201 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4204 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4211 /* For each batch of consecutive loadable .note sections,
4212 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4213 because if we link together nonloadable .note sections and
4214 loadable .note sections, we will generate two .note sections
4215 in the output file. FIXME: Using names for section types is
4217 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4219 if ((s
->flags
& SEC_LOAD
) != 0
4220 && CONST_STRNEQ (s
->name
, ".note"))
4225 amt
= sizeof (struct elf_segment_map
);
4226 if (s
->alignment_power
== 2)
4227 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4229 if (s2
->next
->alignment_power
== 2
4230 && (s2
->next
->flags
& SEC_LOAD
) != 0
4231 && CONST_STRNEQ (s2
->next
->name
, ".note")
4232 && align_power (s2
->lma
+ s2
->size
, 2)
4238 amt
+= (count
- 1) * sizeof (asection
*);
4239 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4243 m
->p_type
= PT_NOTE
;
4247 m
->sections
[m
->count
- count
--] = s
;
4248 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4251 m
->sections
[m
->count
- 1] = s
;
4252 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4256 if (s
->flags
& SEC_THREAD_LOCAL
)
4264 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4267 amt
= sizeof (struct elf_segment_map
);
4268 amt
+= (tls_count
- 1) * sizeof (asection
*);
4269 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4274 m
->count
= tls_count
;
4275 /* Mandated PF_R. */
4277 m
->p_flags_valid
= 1;
4279 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4281 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4284 (_("%B: TLS sections are not adjacent:"), abfd
);
4287 while (i
< (unsigned int) tls_count
)
4289 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4291 _bfd_error_handler (_(" TLS: %A"), s
);
4295 _bfd_error_handler (_(" non-TLS: %A"), s
);
4298 bfd_set_error (bfd_error_bad_value
);
4309 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4311 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4312 if (eh_frame_hdr
!= NULL
4313 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4315 amt
= sizeof (struct elf_segment_map
);
4316 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4320 m
->p_type
= PT_GNU_EH_FRAME
;
4322 m
->sections
[0] = eh_frame_hdr
->output_section
;
4328 if (elf_stack_flags (abfd
))
4330 amt
= sizeof (struct elf_segment_map
);
4331 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4335 m
->p_type
= PT_GNU_STACK
;
4336 m
->p_flags
= elf_stack_flags (abfd
);
4337 m
->p_align
= bed
->stack_align
;
4338 m
->p_flags_valid
= 1;
4339 m
->p_align_valid
= m
->p_align
!= 0;
4340 if (info
->stacksize
> 0)
4342 m
->p_size
= info
->stacksize
;
4343 m
->p_size_valid
= 1;
4350 if (info
!= NULL
&& info
->relro
)
4352 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4354 if (m
->p_type
== PT_LOAD
4356 && m
->sections
[0]->vma
>= info
->relro_start
4357 && m
->sections
[0]->vma
< info
->relro_end
)
4360 while (--i
!= (unsigned) -1)
4361 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4362 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4365 if (i
!= (unsigned) -1)
4370 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4373 amt
= sizeof (struct elf_segment_map
);
4374 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4378 m
->p_type
= PT_GNU_RELRO
;
4380 m
->p_flags_valid
= 1;
4388 elf_seg_map (abfd
) = mfirst
;
4391 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4394 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4396 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4401 if (sections
!= NULL
)
4406 /* Sort sections by address. */
4409 elf_sort_sections (const void *arg1
, const void *arg2
)
4411 const asection
*sec1
= *(const asection
**) arg1
;
4412 const asection
*sec2
= *(const asection
**) arg2
;
4413 bfd_size_type size1
, size2
;
4415 /* Sort by LMA first, since this is the address used to
4416 place the section into a segment. */
4417 if (sec1
->lma
< sec2
->lma
)
4419 else if (sec1
->lma
> sec2
->lma
)
4422 /* Then sort by VMA. Normally the LMA and the VMA will be
4423 the same, and this will do nothing. */
4424 if (sec1
->vma
< sec2
->vma
)
4426 else if (sec1
->vma
> sec2
->vma
)
4429 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4431 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4437 /* If the indicies are the same, do not return 0
4438 here, but continue to try the next comparison. */
4439 if (sec1
->target_index
- sec2
->target_index
!= 0)
4440 return sec1
->target_index
- sec2
->target_index
;
4445 else if (TOEND (sec2
))
4450 /* Sort by size, to put zero sized sections
4451 before others at the same address. */
4453 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4454 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4461 return sec1
->target_index
- sec2
->target_index
;
4464 /* Ian Lance Taylor writes:
4466 We shouldn't be using % with a negative signed number. That's just
4467 not good. We have to make sure either that the number is not
4468 negative, or that the number has an unsigned type. When the types
4469 are all the same size they wind up as unsigned. When file_ptr is a
4470 larger signed type, the arithmetic winds up as signed long long,
4473 What we're trying to say here is something like ``increase OFF by
4474 the least amount that will cause it to be equal to the VMA modulo
4476 /* In other words, something like:
4478 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4479 off_offset = off % bed->maxpagesize;
4480 if (vma_offset < off_offset)
4481 adjustment = vma_offset + bed->maxpagesize - off_offset;
4483 adjustment = vma_offset - off_offset;
4485 which can can be collapsed into the expression below. */
4488 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4490 /* PR binutils/16199: Handle an alignment of zero. */
4491 if (maxpagesize
== 0)
4493 return ((vma
- off
) % maxpagesize
);
4497 print_segment_map (const struct elf_segment_map
*m
)
4500 const char *pt
= get_segment_type (m
->p_type
);
4505 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4506 sprintf (buf
, "LOPROC+%7.7x",
4507 (unsigned int) (m
->p_type
- PT_LOPROC
));
4508 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4509 sprintf (buf
, "LOOS+%7.7x",
4510 (unsigned int) (m
->p_type
- PT_LOOS
));
4512 snprintf (buf
, sizeof (buf
), "%8.8x",
4513 (unsigned int) m
->p_type
);
4517 fprintf (stderr
, "%s:", pt
);
4518 for (j
= 0; j
< m
->count
; j
++)
4519 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4525 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4530 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4532 buf
= bfd_zmalloc (len
);
4535 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4540 /* Assign file positions to the sections based on the mapping from
4541 sections to segments. This function also sets up some fields in
4545 assign_file_positions_for_load_sections (bfd
*abfd
,
4546 struct bfd_link_info
*link_info
)
4548 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4549 struct elf_segment_map
*m
;
4550 Elf_Internal_Phdr
*phdrs
;
4551 Elf_Internal_Phdr
*p
;
4553 bfd_size_type maxpagesize
;
4556 bfd_vma header_pad
= 0;
4558 if (link_info
== NULL
4559 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4563 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4567 header_pad
= m
->header_size
;
4572 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4573 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4577 /* PR binutils/12467. */
4578 elf_elfheader (abfd
)->e_phoff
= 0;
4579 elf_elfheader (abfd
)->e_phentsize
= 0;
4582 elf_elfheader (abfd
)->e_phnum
= alloc
;
4584 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4585 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4587 BFD_ASSERT (elf_program_header_size (abfd
)
4588 >= alloc
* bed
->s
->sizeof_phdr
);
4592 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4596 /* We're writing the size in elf_program_header_size (abfd),
4597 see assign_file_positions_except_relocs, so make sure we have
4598 that amount allocated, with trailing space cleared.
4599 The variable alloc contains the computed need, while
4600 elf_program_header_size (abfd) contains the size used for the
4602 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4603 where the layout is forced to according to a larger size in the
4604 last iterations for the testcase ld-elf/header. */
4605 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4607 phdrs
= (Elf_Internal_Phdr
*)
4609 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4610 sizeof (Elf_Internal_Phdr
));
4611 elf_tdata (abfd
)->phdr
= phdrs
;
4616 if ((abfd
->flags
& D_PAGED
) != 0)
4617 maxpagesize
= bed
->maxpagesize
;
4619 off
= bed
->s
->sizeof_ehdr
;
4620 off
+= alloc
* bed
->s
->sizeof_phdr
;
4621 if (header_pad
< (bfd_vma
) off
)
4627 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4629 m
= m
->next
, p
++, j
++)
4633 bfd_boolean no_contents
;
4635 /* If elf_segment_map is not from map_sections_to_segments, the
4636 sections may not be correctly ordered. NOTE: sorting should
4637 not be done to the PT_NOTE section of a corefile, which may
4638 contain several pseudo-sections artificially created by bfd.
4639 Sorting these pseudo-sections breaks things badly. */
4641 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4642 && m
->p_type
== PT_NOTE
))
4643 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4646 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4647 number of sections with contents contributing to both p_filesz
4648 and p_memsz, followed by a number of sections with no contents
4649 that just contribute to p_memsz. In this loop, OFF tracks next
4650 available file offset for PT_LOAD and PT_NOTE segments. */
4651 p
->p_type
= m
->p_type
;
4652 p
->p_flags
= m
->p_flags
;
4657 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4659 if (m
->p_paddr_valid
)
4660 p
->p_paddr
= m
->p_paddr
;
4661 else if (m
->count
== 0)
4664 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4666 if (p
->p_type
== PT_LOAD
4667 && (abfd
->flags
& D_PAGED
) != 0)
4669 /* p_align in demand paged PT_LOAD segments effectively stores
4670 the maximum page size. When copying an executable with
4671 objcopy, we set m->p_align from the input file. Use this
4672 value for maxpagesize rather than bed->maxpagesize, which
4673 may be different. Note that we use maxpagesize for PT_TLS
4674 segment alignment later in this function, so we are relying
4675 on at least one PT_LOAD segment appearing before a PT_TLS
4677 if (m
->p_align_valid
)
4678 maxpagesize
= m
->p_align
;
4680 p
->p_align
= maxpagesize
;
4682 else if (m
->p_align_valid
)
4683 p
->p_align
= m
->p_align
;
4684 else if (m
->count
== 0)
4685 p
->p_align
= 1 << bed
->s
->log_file_align
;
4689 no_contents
= FALSE
;
4691 if (p
->p_type
== PT_LOAD
4694 bfd_size_type align
;
4695 unsigned int align_power
= 0;
4697 if (m
->p_align_valid
)
4701 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4703 unsigned int secalign
;
4705 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4706 if (secalign
> align_power
)
4707 align_power
= secalign
;
4709 align
= (bfd_size_type
) 1 << align_power
;
4710 if (align
< maxpagesize
)
4711 align
= maxpagesize
;
4714 for (i
= 0; i
< m
->count
; i
++)
4715 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4716 /* If we aren't making room for this section, then
4717 it must be SHT_NOBITS regardless of what we've
4718 set via struct bfd_elf_special_section. */
4719 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4721 /* Find out whether this segment contains any loadable
4724 for (i
= 0; i
< m
->count
; i
++)
4725 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4727 no_contents
= FALSE
;
4731 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4735 /* We shouldn't need to align the segment on disk since
4736 the segment doesn't need file space, but the gABI
4737 arguably requires the alignment and glibc ld.so
4738 checks it. So to comply with the alignment
4739 requirement but not waste file space, we adjust
4740 p_offset for just this segment. (OFF_ADJUST is
4741 subtracted from OFF later.) This may put p_offset
4742 past the end of file, but that shouldn't matter. */
4747 /* Make sure the .dynamic section is the first section in the
4748 PT_DYNAMIC segment. */
4749 else if (p
->p_type
== PT_DYNAMIC
4751 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4754 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4756 bfd_set_error (bfd_error_bad_value
);
4759 /* Set the note section type to SHT_NOTE. */
4760 else if (p
->p_type
== PT_NOTE
)
4761 for (i
= 0; i
< m
->count
; i
++)
4762 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4768 if (m
->includes_filehdr
)
4770 if (!m
->p_flags_valid
)
4772 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4773 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4776 if (p
->p_vaddr
< (bfd_vma
) off
)
4778 (*_bfd_error_handler
)
4779 (_("%B: Not enough room for program headers, try linking with -N"),
4781 bfd_set_error (bfd_error_bad_value
);
4786 if (!m
->p_paddr_valid
)
4791 if (m
->includes_phdrs
)
4793 if (!m
->p_flags_valid
)
4796 if (!m
->includes_filehdr
)
4798 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4802 p
->p_vaddr
-= off
- p
->p_offset
;
4803 if (!m
->p_paddr_valid
)
4804 p
->p_paddr
-= off
- p
->p_offset
;
4808 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4809 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4812 p
->p_filesz
+= header_pad
;
4813 p
->p_memsz
+= header_pad
;
4817 if (p
->p_type
== PT_LOAD
4818 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4820 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4826 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4828 p
->p_filesz
+= adjust
;
4829 p
->p_memsz
+= adjust
;
4833 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4834 maps. Set filepos for sections in PT_LOAD segments, and in
4835 core files, for sections in PT_NOTE segments.
4836 assign_file_positions_for_non_load_sections will set filepos
4837 for other sections and update p_filesz for other segments. */
4838 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4841 bfd_size_type align
;
4842 Elf_Internal_Shdr
*this_hdr
;
4845 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4846 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4848 if ((p
->p_type
== PT_LOAD
4849 || p
->p_type
== PT_TLS
)
4850 && (this_hdr
->sh_type
!= SHT_NOBITS
4851 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4852 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4853 || p
->p_type
== PT_TLS
))))
4855 bfd_vma p_start
= p
->p_paddr
;
4856 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4857 bfd_vma s_start
= sec
->lma
;
4858 bfd_vma adjust
= s_start
- p_end
;
4862 || p_end
< p_start
))
4864 (*_bfd_error_handler
)
4865 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4866 (unsigned long) s_start
, (unsigned long) p_end
);
4870 p
->p_memsz
+= adjust
;
4872 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4874 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4876 /* We have a PROGBITS section following NOBITS ones.
4877 Allocate file space for the NOBITS section(s) and
4879 adjust
= p
->p_memsz
- p
->p_filesz
;
4880 if (!write_zeros (abfd
, off
, adjust
))
4884 p
->p_filesz
+= adjust
;
4888 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4890 /* The section at i == 0 is the one that actually contains
4894 this_hdr
->sh_offset
= sec
->filepos
= off
;
4895 off
+= this_hdr
->sh_size
;
4896 p
->p_filesz
= this_hdr
->sh_size
;
4902 /* The rest are fake sections that shouldn't be written. */
4911 if (p
->p_type
== PT_LOAD
)
4913 this_hdr
->sh_offset
= sec
->filepos
= off
;
4914 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4915 off
+= this_hdr
->sh_size
;
4917 else if (this_hdr
->sh_type
== SHT_NOBITS
4918 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
4919 && this_hdr
->sh_offset
== 0)
4921 /* This is a .tbss section that didn't get a PT_LOAD.
4922 (See _bfd_elf_map_sections_to_segments "Create a
4923 final PT_LOAD".) Set sh_offset to the value it
4924 would have if we had created a zero p_filesz and
4925 p_memsz PT_LOAD header for the section. This
4926 also makes the PT_TLS header have the same
4928 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
4930 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
4933 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4935 p
->p_filesz
+= this_hdr
->sh_size
;
4936 /* A load section without SHF_ALLOC is something like
4937 a note section in a PT_NOTE segment. These take
4938 file space but are not loaded into memory. */
4939 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4940 p
->p_memsz
+= this_hdr
->sh_size
;
4942 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4944 if (p
->p_type
== PT_TLS
)
4945 p
->p_memsz
+= this_hdr
->sh_size
;
4947 /* .tbss is special. It doesn't contribute to p_memsz of
4949 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4950 p
->p_memsz
+= this_hdr
->sh_size
;
4953 if (align
> p
->p_align
4954 && !m
->p_align_valid
4955 && (p
->p_type
!= PT_LOAD
4956 || (abfd
->flags
& D_PAGED
) == 0))
4960 if (!m
->p_flags_valid
)
4963 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4965 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4972 /* Check that all sections are in a PT_LOAD segment.
4973 Don't check funky gdb generated core files. */
4974 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4976 bfd_boolean check_vma
= TRUE
;
4978 for (i
= 1; i
< m
->count
; i
++)
4979 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4980 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4981 ->this_hdr
), p
) != 0
4982 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4983 ->this_hdr
), p
) != 0)
4985 /* Looks like we have overlays packed into the segment. */
4990 for (i
= 0; i
< m
->count
; i
++)
4992 Elf_Internal_Shdr
*this_hdr
;
4995 sec
= m
->sections
[i
];
4996 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4997 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
4998 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5000 (*_bfd_error_handler
)
5001 (_("%B: section `%A' can't be allocated in segment %d"),
5003 print_segment_map (m
);
5009 elf_next_file_pos (abfd
) = off
;
5013 /* Assign file positions for the other sections. */
5016 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5017 struct bfd_link_info
*link_info
)
5019 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5020 Elf_Internal_Shdr
**i_shdrpp
;
5021 Elf_Internal_Shdr
**hdrpp
;
5022 Elf_Internal_Phdr
*phdrs
;
5023 Elf_Internal_Phdr
*p
;
5024 struct elf_segment_map
*m
;
5025 struct elf_segment_map
*hdrs_segment
;
5026 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5027 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5029 unsigned int num_sec
;
5033 i_shdrpp
= elf_elfsections (abfd
);
5034 num_sec
= elf_numsections (abfd
);
5035 off
= elf_next_file_pos (abfd
);
5036 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5038 Elf_Internal_Shdr
*hdr
;
5041 if (hdr
->bfd_section
!= NULL
5042 && (hdr
->bfd_section
->filepos
!= 0
5043 || (hdr
->sh_type
== SHT_NOBITS
5044 && hdr
->contents
== NULL
)))
5045 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5046 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5048 if (hdr
->sh_size
!= 0)
5049 (*_bfd_error_handler
)
5050 (_("%B: warning: allocated section `%s' not in segment"),
5052 (hdr
->bfd_section
== NULL
5054 : hdr
->bfd_section
->name
));
5055 /* We don't need to page align empty sections. */
5056 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5057 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5060 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5062 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5065 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5066 && hdr
->bfd_section
== NULL
)
5067 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5068 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
5069 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)])
5070 hdr
->sh_offset
= -1;
5072 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5075 /* Now that we have set the section file positions, we can set up
5076 the file positions for the non PT_LOAD segments. */
5080 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5082 hdrs_segment
= NULL
;
5083 phdrs
= elf_tdata (abfd
)->phdr
;
5084 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5087 if (p
->p_type
!= PT_LOAD
)
5090 if (m
->includes_filehdr
)
5092 filehdr_vaddr
= p
->p_vaddr
;
5093 filehdr_paddr
= p
->p_paddr
;
5095 if (m
->includes_phdrs
)
5097 phdrs_vaddr
= p
->p_vaddr
;
5098 phdrs_paddr
= p
->p_paddr
;
5099 if (m
->includes_filehdr
)
5102 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5103 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5108 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5110 /* There is a segment that contains both the file headers and the
5111 program headers, so provide a symbol __ehdr_start pointing there.
5112 A program can use this to examine itself robustly. */
5114 struct elf_link_hash_entry
*hash
5115 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5116 FALSE
, FALSE
, TRUE
);
5117 /* If the symbol was referenced and not defined, define it. */
5119 && (hash
->root
.type
== bfd_link_hash_new
5120 || hash
->root
.type
== bfd_link_hash_undefined
5121 || hash
->root
.type
== bfd_link_hash_undefweak
5122 || hash
->root
.type
== bfd_link_hash_common
))
5125 if (hdrs_segment
->count
!= 0)
5126 /* The segment contains sections, so use the first one. */
5127 s
= hdrs_segment
->sections
[0];
5129 /* Use the first (i.e. lowest-addressed) section in any segment. */
5130 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5139 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5140 hash
->root
.u
.def
.section
= s
;
5144 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5145 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5148 hash
->root
.type
= bfd_link_hash_defined
;
5149 hash
->def_regular
= 1;
5154 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5156 if (p
->p_type
== PT_GNU_RELRO
)
5158 const Elf_Internal_Phdr
*lp
;
5159 struct elf_segment_map
*lm
;
5161 if (link_info
!= NULL
)
5163 /* During linking the range of the RELRO segment is passed
5165 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5167 lm
= lm
->next
, lp
++)
5169 if (lp
->p_type
== PT_LOAD
5170 && lp
->p_vaddr
< link_info
->relro_end
5172 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5176 BFD_ASSERT (lm
!= NULL
);
5180 /* Otherwise we are copying an executable or shared
5181 library, but we need to use the same linker logic. */
5182 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5184 if (lp
->p_type
== PT_LOAD
5185 && lp
->p_paddr
== p
->p_paddr
)
5190 if (lp
< phdrs
+ count
)
5192 p
->p_vaddr
= lp
->p_vaddr
;
5193 p
->p_paddr
= lp
->p_paddr
;
5194 p
->p_offset
= lp
->p_offset
;
5195 if (link_info
!= NULL
)
5196 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5197 else if (m
->p_size_valid
)
5198 p
->p_filesz
= m
->p_size
;
5201 p
->p_memsz
= p
->p_filesz
;
5202 /* Preserve the alignment and flags if they are valid. The
5203 gold linker generates RW/4 for the PT_GNU_RELRO section.
5204 It is better for objcopy/strip to honor these attributes
5205 otherwise gdb will choke when using separate debug files.
5207 if (!m
->p_align_valid
)
5209 if (!m
->p_flags_valid
)
5210 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5214 memset (p
, 0, sizeof *p
);
5215 p
->p_type
= PT_NULL
;
5218 else if (p
->p_type
== PT_GNU_STACK
)
5220 if (m
->p_size_valid
)
5221 p
->p_memsz
= m
->p_size
;
5223 else if (m
->count
!= 0)
5225 if (p
->p_type
!= PT_LOAD
5226 && (p
->p_type
!= PT_NOTE
5227 || bfd_get_format (abfd
) != bfd_core
))
5229 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5231 /* PR 17512: file: 2195325e. */
5232 (*_bfd_error_handler
)
5233 (_("%B: warning: non-load segment includes file header and/or program header"),
5239 p
->p_offset
= m
->sections
[0]->filepos
;
5240 for (i
= m
->count
; i
-- != 0;)
5242 asection
*sect
= m
->sections
[i
];
5243 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5244 if (hdr
->sh_type
!= SHT_NOBITS
)
5246 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5253 else if (m
->includes_filehdr
)
5255 p
->p_vaddr
= filehdr_vaddr
;
5256 if (! m
->p_paddr_valid
)
5257 p
->p_paddr
= filehdr_paddr
;
5259 else if (m
->includes_phdrs
)
5261 p
->p_vaddr
= phdrs_vaddr
;
5262 if (! m
->p_paddr_valid
)
5263 p
->p_paddr
= phdrs_paddr
;
5267 elf_next_file_pos (abfd
) = off
;
5272 /* Work out the file positions of all the sections. This is called by
5273 _bfd_elf_compute_section_file_positions. All the section sizes and
5274 VMAs must be known before this is called.
5276 Reloc sections come in two flavours: Those processed specially as
5277 "side-channel" data attached to a section to which they apply, and
5278 those that bfd doesn't process as relocations. The latter sort are
5279 stored in a normal bfd section by bfd_section_from_shdr. We don't
5280 consider the former sort here, unless they form part of the loadable
5281 image. Reloc sections not assigned here will be handled later by
5282 assign_file_positions_for_relocs.
5284 We also don't set the positions of the .symtab and .strtab here. */
5287 assign_file_positions_except_relocs (bfd
*abfd
,
5288 struct bfd_link_info
*link_info
)
5290 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5291 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5292 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5294 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5295 && bfd_get_format (abfd
) != bfd_core
)
5297 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5298 unsigned int num_sec
= elf_numsections (abfd
);
5299 Elf_Internal_Shdr
**hdrpp
;
5303 /* Start after the ELF header. */
5304 off
= i_ehdrp
->e_ehsize
;
5306 /* We are not creating an executable, which means that we are
5307 not creating a program header, and that the actual order of
5308 the sections in the file is unimportant. */
5309 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5311 Elf_Internal_Shdr
*hdr
;
5314 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5315 && hdr
->bfd_section
== NULL
)
5316 || i
== elf_onesymtab (abfd
)
5317 || i
== elf_symtab_shndx (abfd
)
5318 || i
== elf_strtab_sec (abfd
))
5320 hdr
->sh_offset
= -1;
5323 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5326 elf_next_file_pos (abfd
) = off
;
5332 /* Assign file positions for the loaded sections based on the
5333 assignment of sections to segments. */
5334 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5337 /* And for non-load sections. */
5338 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5341 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5343 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5347 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5348 if (link_info
!= NULL
5349 && link_info
->executable
5350 && link_info
->shared
)
5352 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5353 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5354 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5356 /* Find the lowest p_vaddr in PT_LOAD segments. */
5357 bfd_vma p_vaddr
= (bfd_vma
) -1;
5358 for (; segment
< end_segment
; segment
++)
5359 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5360 p_vaddr
= segment
->p_vaddr
;
5362 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5363 segments is non-zero. */
5365 i_ehdrp
->e_type
= ET_EXEC
;
5368 /* Write out the program headers. */
5369 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5370 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5371 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5379 prep_headers (bfd
*abfd
)
5381 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5382 struct elf_strtab_hash
*shstrtab
;
5383 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5385 i_ehdrp
= elf_elfheader (abfd
);
5387 shstrtab
= _bfd_elf_strtab_init ();
5388 if (shstrtab
== NULL
)
5391 elf_shstrtab (abfd
) = shstrtab
;
5393 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5394 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5395 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5396 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5398 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5399 i_ehdrp
->e_ident
[EI_DATA
] =
5400 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5401 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5403 if ((abfd
->flags
& DYNAMIC
) != 0)
5404 i_ehdrp
->e_type
= ET_DYN
;
5405 else if ((abfd
->flags
& EXEC_P
) != 0)
5406 i_ehdrp
->e_type
= ET_EXEC
;
5407 else if (bfd_get_format (abfd
) == bfd_core
)
5408 i_ehdrp
->e_type
= ET_CORE
;
5410 i_ehdrp
->e_type
= ET_REL
;
5412 switch (bfd_get_arch (abfd
))
5414 case bfd_arch_unknown
:
5415 i_ehdrp
->e_machine
= EM_NONE
;
5418 /* There used to be a long list of cases here, each one setting
5419 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5420 in the corresponding bfd definition. To avoid duplication,
5421 the switch was removed. Machines that need special handling
5422 can generally do it in elf_backend_final_write_processing(),
5423 unless they need the information earlier than the final write.
5424 Such need can generally be supplied by replacing the tests for
5425 e_machine with the conditions used to determine it. */
5427 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5430 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5431 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5433 /* No program header, for now. */
5434 i_ehdrp
->e_phoff
= 0;
5435 i_ehdrp
->e_phentsize
= 0;
5436 i_ehdrp
->e_phnum
= 0;
5438 /* Each bfd section is section header entry. */
5439 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5440 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5442 /* If we're building an executable, we'll need a program header table. */
5443 if (abfd
->flags
& EXEC_P
)
5444 /* It all happens later. */
5448 i_ehdrp
->e_phentsize
= 0;
5449 i_ehdrp
->e_phoff
= 0;
5452 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5453 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5454 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5455 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5456 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5457 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5458 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5459 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5460 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5466 /* Assign file positions for all the reloc sections which are not part
5467 of the loadable file image, and the file position of section headers. */
5470 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5473 unsigned int i
, num_sec
;
5474 Elf_Internal_Shdr
**shdrpp
;
5475 Elf_Internal_Ehdr
*i_ehdrp
;
5476 const struct elf_backend_data
*bed
;
5478 off
= elf_next_file_pos (abfd
);
5480 num_sec
= elf_numsections (abfd
);
5481 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5483 Elf_Internal_Shdr
*shdrp
;
5486 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5487 && shdrp
->sh_offset
== -1)
5488 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5491 /* Place the section headers. */
5492 i_ehdrp
= elf_elfheader (abfd
);
5493 bed
= get_elf_backend_data (abfd
);
5494 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5495 i_ehdrp
->e_shoff
= off
;
5496 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5497 elf_next_file_pos (abfd
) = off
;
5501 _bfd_elf_write_object_contents (bfd
*abfd
)
5503 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5504 Elf_Internal_Shdr
**i_shdrp
;
5506 unsigned int count
, num_sec
;
5507 struct elf_obj_tdata
*t
;
5509 if (! abfd
->output_has_begun
5510 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5513 i_shdrp
= elf_elfsections (abfd
);
5516 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5520 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5522 /* After writing the headers, we need to write the sections too... */
5523 num_sec
= elf_numsections (abfd
);
5524 for (count
= 1; count
< num_sec
; count
++)
5526 if (bed
->elf_backend_section_processing
)
5527 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5528 if (i_shdrp
[count
]->contents
)
5530 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5532 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5533 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5538 /* Write out the section header names. */
5539 t
= elf_tdata (abfd
);
5540 if (elf_shstrtab (abfd
) != NULL
5541 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5542 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5545 if (bed
->elf_backend_final_write_processing
)
5546 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5548 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5551 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5552 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5553 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5559 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5561 /* Hopefully this can be done just like an object file. */
5562 return _bfd_elf_write_object_contents (abfd
);
5565 /* Given a section, search the header to find them. */
5568 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5570 const struct elf_backend_data
*bed
;
5571 unsigned int sec_index
;
5573 if (elf_section_data (asect
) != NULL
5574 && elf_section_data (asect
)->this_idx
!= 0)
5575 return elf_section_data (asect
)->this_idx
;
5577 if (bfd_is_abs_section (asect
))
5578 sec_index
= SHN_ABS
;
5579 else if (bfd_is_com_section (asect
))
5580 sec_index
= SHN_COMMON
;
5581 else if (bfd_is_und_section (asect
))
5582 sec_index
= SHN_UNDEF
;
5584 sec_index
= SHN_BAD
;
5586 bed
= get_elf_backend_data (abfd
);
5587 if (bed
->elf_backend_section_from_bfd_section
)
5589 int retval
= sec_index
;
5591 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5595 if (sec_index
== SHN_BAD
)
5596 bfd_set_error (bfd_error_nonrepresentable_section
);
5601 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5605 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5607 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5609 flagword flags
= asym_ptr
->flags
;
5611 /* When gas creates relocations against local labels, it creates its
5612 own symbol for the section, but does put the symbol into the
5613 symbol chain, so udata is 0. When the linker is generating
5614 relocatable output, this section symbol may be for one of the
5615 input sections rather than the output section. */
5616 if (asym_ptr
->udata
.i
== 0
5617 && (flags
& BSF_SECTION_SYM
)
5618 && asym_ptr
->section
)
5623 sec
= asym_ptr
->section
;
5624 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5625 sec
= sec
->output_section
;
5626 if (sec
->owner
== abfd
5627 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5628 && elf_section_syms (abfd
)[indx
] != NULL
)
5629 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5632 idx
= asym_ptr
->udata
.i
;
5636 /* This case can occur when using --strip-symbol on a symbol
5637 which is used in a relocation entry. */
5638 (*_bfd_error_handler
)
5639 (_("%B: symbol `%s' required but not present"),
5640 abfd
, bfd_asymbol_name (asym_ptr
));
5641 bfd_set_error (bfd_error_no_symbols
);
5648 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5649 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5657 /* Rewrite program header information. */
5660 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5662 Elf_Internal_Ehdr
*iehdr
;
5663 struct elf_segment_map
*map
;
5664 struct elf_segment_map
*map_first
;
5665 struct elf_segment_map
**pointer_to_map
;
5666 Elf_Internal_Phdr
*segment
;
5669 unsigned int num_segments
;
5670 bfd_boolean phdr_included
= FALSE
;
5671 bfd_boolean p_paddr_valid
;
5672 bfd_vma maxpagesize
;
5673 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5674 unsigned int phdr_adjust_num
= 0;
5675 const struct elf_backend_data
*bed
;
5677 bed
= get_elf_backend_data (ibfd
);
5678 iehdr
= elf_elfheader (ibfd
);
5681 pointer_to_map
= &map_first
;
5683 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5684 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5686 /* Returns the end address of the segment + 1. */
5687 #define SEGMENT_END(segment, start) \
5688 (start + (segment->p_memsz > segment->p_filesz \
5689 ? segment->p_memsz : segment->p_filesz))
5691 #define SECTION_SIZE(section, segment) \
5692 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5693 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5694 ? section->size : 0)
5696 /* Returns TRUE if the given section is contained within
5697 the given segment. VMA addresses are compared. */
5698 #define IS_CONTAINED_BY_VMA(section, segment) \
5699 (section->vma >= segment->p_vaddr \
5700 && (section->vma + SECTION_SIZE (section, segment) \
5701 <= (SEGMENT_END (segment, segment->p_vaddr))))
5703 /* Returns TRUE if the given section is contained within
5704 the given segment. LMA addresses are compared. */
5705 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5706 (section->lma >= base \
5707 && (section->lma + SECTION_SIZE (section, segment) \
5708 <= SEGMENT_END (segment, base)))
5710 /* Handle PT_NOTE segment. */
5711 #define IS_NOTE(p, s) \
5712 (p->p_type == PT_NOTE \
5713 && elf_section_type (s) == SHT_NOTE \
5714 && (bfd_vma) s->filepos >= p->p_offset \
5715 && ((bfd_vma) s->filepos + s->size \
5716 <= p->p_offset + p->p_filesz))
5718 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5720 #define IS_COREFILE_NOTE(p, s) \
5722 && bfd_get_format (ibfd) == bfd_core \
5726 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5727 linker, which generates a PT_INTERP section with p_vaddr and
5728 p_memsz set to 0. */
5729 #define IS_SOLARIS_PT_INTERP(p, s) \
5731 && p->p_paddr == 0 \
5732 && p->p_memsz == 0 \
5733 && p->p_filesz > 0 \
5734 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5736 && (bfd_vma) s->filepos >= p->p_offset \
5737 && ((bfd_vma) s->filepos + s->size \
5738 <= p->p_offset + p->p_filesz))
5740 /* Decide if the given section should be included in the given segment.
5741 A section will be included if:
5742 1. It is within the address space of the segment -- we use the LMA
5743 if that is set for the segment and the VMA otherwise,
5744 2. It is an allocated section or a NOTE section in a PT_NOTE
5746 3. There is an output section associated with it,
5747 4. The section has not already been allocated to a previous segment.
5748 5. PT_GNU_STACK segments do not include any sections.
5749 6. PT_TLS segment includes only SHF_TLS sections.
5750 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5751 8. PT_DYNAMIC should not contain empty sections at the beginning
5752 (with the possible exception of .dynamic). */
5753 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5754 ((((segment->p_paddr \
5755 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5756 : IS_CONTAINED_BY_VMA (section, segment)) \
5757 && (section->flags & SEC_ALLOC) != 0) \
5758 || IS_NOTE (segment, section)) \
5759 && segment->p_type != PT_GNU_STACK \
5760 && (segment->p_type != PT_TLS \
5761 || (section->flags & SEC_THREAD_LOCAL)) \
5762 && (segment->p_type == PT_LOAD \
5763 || segment->p_type == PT_TLS \
5764 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5765 && (segment->p_type != PT_DYNAMIC \
5766 || SECTION_SIZE (section, segment) > 0 \
5767 || (segment->p_paddr \
5768 ? segment->p_paddr != section->lma \
5769 : segment->p_vaddr != section->vma) \
5770 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5772 && !section->segment_mark)
5774 /* If the output section of a section in the input segment is NULL,
5775 it is removed from the corresponding output segment. */
5776 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5777 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5778 && section->output_section != NULL)
5780 /* Returns TRUE iff seg1 starts after the end of seg2. */
5781 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5782 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5784 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5785 their VMA address ranges and their LMA address ranges overlap.
5786 It is possible to have overlapping VMA ranges without overlapping LMA
5787 ranges. RedBoot images for example can have both .data and .bss mapped
5788 to the same VMA range, but with the .data section mapped to a different
5790 #define SEGMENT_OVERLAPS(seg1, seg2) \
5791 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5792 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5793 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5794 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5796 /* Initialise the segment mark field. */
5797 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5798 section
->segment_mark
= FALSE
;
5800 /* The Solaris linker creates program headers in which all the
5801 p_paddr fields are zero. When we try to objcopy or strip such a
5802 file, we get confused. Check for this case, and if we find it
5803 don't set the p_paddr_valid fields. */
5804 p_paddr_valid
= FALSE
;
5805 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5808 if (segment
->p_paddr
!= 0)
5810 p_paddr_valid
= TRUE
;
5814 /* Scan through the segments specified in the program header
5815 of the input BFD. For this first scan we look for overlaps
5816 in the loadable segments. These can be created by weird
5817 parameters to objcopy. Also, fix some solaris weirdness. */
5818 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5823 Elf_Internal_Phdr
*segment2
;
5825 if (segment
->p_type
== PT_INTERP
)
5826 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5827 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5829 /* Mininal change so that the normal section to segment
5830 assignment code will work. */
5831 segment
->p_vaddr
= section
->vma
;
5835 if (segment
->p_type
!= PT_LOAD
)
5837 /* Remove PT_GNU_RELRO segment. */
5838 if (segment
->p_type
== PT_GNU_RELRO
)
5839 segment
->p_type
= PT_NULL
;
5843 /* Determine if this segment overlaps any previous segments. */
5844 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5846 bfd_signed_vma extra_length
;
5848 if (segment2
->p_type
!= PT_LOAD
5849 || !SEGMENT_OVERLAPS (segment
, segment2
))
5852 /* Merge the two segments together. */
5853 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5855 /* Extend SEGMENT2 to include SEGMENT and then delete
5857 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5858 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5860 if (extra_length
> 0)
5862 segment2
->p_memsz
+= extra_length
;
5863 segment2
->p_filesz
+= extra_length
;
5866 segment
->p_type
= PT_NULL
;
5868 /* Since we have deleted P we must restart the outer loop. */
5870 segment
= elf_tdata (ibfd
)->phdr
;
5875 /* Extend SEGMENT to include SEGMENT2 and then delete
5877 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5878 - SEGMENT_END (segment
, segment
->p_vaddr
));
5880 if (extra_length
> 0)
5882 segment
->p_memsz
+= extra_length
;
5883 segment
->p_filesz
+= extra_length
;
5886 segment2
->p_type
= PT_NULL
;
5891 /* The second scan attempts to assign sections to segments. */
5892 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5896 unsigned int section_count
;
5897 asection
**sections
;
5898 asection
*output_section
;
5900 bfd_vma matching_lma
;
5901 bfd_vma suggested_lma
;
5904 asection
*first_section
;
5905 bfd_boolean first_matching_lma
;
5906 bfd_boolean first_suggested_lma
;
5908 if (segment
->p_type
== PT_NULL
)
5911 first_section
= NULL
;
5912 /* Compute how many sections might be placed into this segment. */
5913 for (section
= ibfd
->sections
, section_count
= 0;
5915 section
= section
->next
)
5917 /* Find the first section in the input segment, which may be
5918 removed from the corresponding output segment. */
5919 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5921 if (first_section
== NULL
)
5922 first_section
= section
;
5923 if (section
->output_section
!= NULL
)
5928 /* Allocate a segment map big enough to contain
5929 all of the sections we have selected. */
5930 amt
= sizeof (struct elf_segment_map
);
5931 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5932 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5936 /* Initialise the fields of the segment map. Default to
5937 using the physical address of the segment in the input BFD. */
5939 map
->p_type
= segment
->p_type
;
5940 map
->p_flags
= segment
->p_flags
;
5941 map
->p_flags_valid
= 1;
5943 /* If the first section in the input segment is removed, there is
5944 no need to preserve segment physical address in the corresponding
5946 if (!first_section
|| first_section
->output_section
!= NULL
)
5948 map
->p_paddr
= segment
->p_paddr
;
5949 map
->p_paddr_valid
= p_paddr_valid
;
5952 /* Determine if this segment contains the ELF file header
5953 and if it contains the program headers themselves. */
5954 map
->includes_filehdr
= (segment
->p_offset
== 0
5955 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5956 map
->includes_phdrs
= 0;
5958 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5960 map
->includes_phdrs
=
5961 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5962 && (segment
->p_offset
+ segment
->p_filesz
5963 >= ((bfd_vma
) iehdr
->e_phoff
5964 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5966 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5967 phdr_included
= TRUE
;
5970 if (section_count
== 0)
5972 /* Special segments, such as the PT_PHDR segment, may contain
5973 no sections, but ordinary, loadable segments should contain
5974 something. They are allowed by the ELF spec however, so only
5975 a warning is produced. */
5976 if (segment
->p_type
== PT_LOAD
)
5977 (*_bfd_error_handler
) (_("\
5978 %B: warning: Empty loadable segment detected, is this intentional ?"),
5982 *pointer_to_map
= map
;
5983 pointer_to_map
= &map
->next
;
5988 /* Now scan the sections in the input BFD again and attempt
5989 to add their corresponding output sections to the segment map.
5990 The problem here is how to handle an output section which has
5991 been moved (ie had its LMA changed). There are four possibilities:
5993 1. None of the sections have been moved.
5994 In this case we can continue to use the segment LMA from the
5997 2. All of the sections have been moved by the same amount.
5998 In this case we can change the segment's LMA to match the LMA
5999 of the first section.
6001 3. Some of the sections have been moved, others have not.
6002 In this case those sections which have not been moved can be
6003 placed in the current segment which will have to have its size,
6004 and possibly its LMA changed, and a new segment or segments will
6005 have to be created to contain the other sections.
6007 4. The sections have been moved, but not by the same amount.
6008 In this case we can change the segment's LMA to match the LMA
6009 of the first section and we will have to create a new segment
6010 or segments to contain the other sections.
6012 In order to save time, we allocate an array to hold the section
6013 pointers that we are interested in. As these sections get assigned
6014 to a segment, they are removed from this array. */
6016 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6017 if (sections
== NULL
)
6020 /* Step One: Scan for segment vs section LMA conflicts.
6021 Also add the sections to the section array allocated above.
6022 Also add the sections to the current segment. In the common
6023 case, where the sections have not been moved, this means that
6024 we have completely filled the segment, and there is nothing
6029 first_matching_lma
= TRUE
;
6030 first_suggested_lma
= TRUE
;
6032 for (section
= ibfd
->sections
;
6034 section
= section
->next
)
6035 if (section
== first_section
)
6038 for (j
= 0; section
!= NULL
; section
= section
->next
)
6040 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6042 output_section
= section
->output_section
;
6044 sections
[j
++] = section
;
6046 /* The Solaris native linker always sets p_paddr to 0.
6047 We try to catch that case here, and set it to the
6048 correct value. Note - some backends require that
6049 p_paddr be left as zero. */
6051 && segment
->p_vaddr
!= 0
6052 && !bed
->want_p_paddr_set_to_zero
6054 && output_section
->lma
!= 0
6055 && output_section
->vma
== (segment
->p_vaddr
6056 + (map
->includes_filehdr
6059 + (map
->includes_phdrs
6061 * iehdr
->e_phentsize
)
6063 map
->p_paddr
= segment
->p_vaddr
;
6065 /* Match up the physical address of the segment with the
6066 LMA address of the output section. */
6067 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6068 || IS_COREFILE_NOTE (segment
, section
)
6069 || (bed
->want_p_paddr_set_to_zero
6070 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6072 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6074 matching_lma
= output_section
->lma
;
6075 first_matching_lma
= FALSE
;
6078 /* We assume that if the section fits within the segment
6079 then it does not overlap any other section within that
6081 map
->sections
[isec
++] = output_section
;
6083 else if (first_suggested_lma
)
6085 suggested_lma
= output_section
->lma
;
6086 first_suggested_lma
= FALSE
;
6089 if (j
== section_count
)
6094 BFD_ASSERT (j
== section_count
);
6096 /* Step Two: Adjust the physical address of the current segment,
6098 if (isec
== section_count
)
6100 /* All of the sections fitted within the segment as currently
6101 specified. This is the default case. Add the segment to
6102 the list of built segments and carry on to process the next
6103 program header in the input BFD. */
6104 map
->count
= section_count
;
6105 *pointer_to_map
= map
;
6106 pointer_to_map
= &map
->next
;
6109 && !bed
->want_p_paddr_set_to_zero
6110 && matching_lma
!= map
->p_paddr
6111 && !map
->includes_filehdr
6112 && !map
->includes_phdrs
)
6113 /* There is some padding before the first section in the
6114 segment. So, we must account for that in the output
6116 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6123 if (!first_matching_lma
)
6125 /* At least one section fits inside the current segment.
6126 Keep it, but modify its physical address to match the
6127 LMA of the first section that fitted. */
6128 map
->p_paddr
= matching_lma
;
6132 /* None of the sections fitted inside the current segment.
6133 Change the current segment's physical address to match
6134 the LMA of the first section. */
6135 map
->p_paddr
= suggested_lma
;
6138 /* Offset the segment physical address from the lma
6139 to allow for space taken up by elf headers. */
6140 if (map
->includes_filehdr
)
6142 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6143 map
->p_paddr
-= iehdr
->e_ehsize
;
6146 map
->includes_filehdr
= FALSE
;
6147 map
->includes_phdrs
= FALSE
;
6151 if (map
->includes_phdrs
)
6153 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6155 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6157 /* iehdr->e_phnum is just an estimate of the number
6158 of program headers that we will need. Make a note
6159 here of the number we used and the segment we chose
6160 to hold these headers, so that we can adjust the
6161 offset when we know the correct value. */
6162 phdr_adjust_num
= iehdr
->e_phnum
;
6163 phdr_adjust_seg
= map
;
6166 map
->includes_phdrs
= FALSE
;
6170 /* Step Three: Loop over the sections again, this time assigning
6171 those that fit to the current segment and removing them from the
6172 sections array; but making sure not to leave large gaps. Once all
6173 possible sections have been assigned to the current segment it is
6174 added to the list of built segments and if sections still remain
6175 to be assigned, a new segment is constructed before repeating
6182 first_suggested_lma
= TRUE
;
6184 /* Fill the current segment with sections that fit. */
6185 for (j
= 0; j
< section_count
; j
++)
6187 section
= sections
[j
];
6189 if (section
== NULL
)
6192 output_section
= section
->output_section
;
6194 BFD_ASSERT (output_section
!= NULL
);
6196 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6197 || IS_COREFILE_NOTE (segment
, section
))
6199 if (map
->count
== 0)
6201 /* If the first section in a segment does not start at
6202 the beginning of the segment, then something is
6204 if (output_section
->lma
6206 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6207 + (map
->includes_phdrs
6208 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6216 prev_sec
= map
->sections
[map
->count
- 1];
6218 /* If the gap between the end of the previous section
6219 and the start of this section is more than
6220 maxpagesize then we need to start a new segment. */
6221 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6223 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6224 || (prev_sec
->lma
+ prev_sec
->size
6225 > output_section
->lma
))
6227 if (first_suggested_lma
)
6229 suggested_lma
= output_section
->lma
;
6230 first_suggested_lma
= FALSE
;
6237 map
->sections
[map
->count
++] = output_section
;
6240 section
->segment_mark
= TRUE
;
6242 else if (first_suggested_lma
)
6244 suggested_lma
= output_section
->lma
;
6245 first_suggested_lma
= FALSE
;
6249 BFD_ASSERT (map
->count
> 0);
6251 /* Add the current segment to the list of built segments. */
6252 *pointer_to_map
= map
;
6253 pointer_to_map
= &map
->next
;
6255 if (isec
< section_count
)
6257 /* We still have not allocated all of the sections to
6258 segments. Create a new segment here, initialise it
6259 and carry on looping. */
6260 amt
= sizeof (struct elf_segment_map
);
6261 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6262 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6269 /* Initialise the fields of the segment map. Set the physical
6270 physical address to the LMA of the first section that has
6271 not yet been assigned. */
6273 map
->p_type
= segment
->p_type
;
6274 map
->p_flags
= segment
->p_flags
;
6275 map
->p_flags_valid
= 1;
6276 map
->p_paddr
= suggested_lma
;
6277 map
->p_paddr_valid
= p_paddr_valid
;
6278 map
->includes_filehdr
= 0;
6279 map
->includes_phdrs
= 0;
6282 while (isec
< section_count
);
6287 elf_seg_map (obfd
) = map_first
;
6289 /* If we had to estimate the number of program headers that were
6290 going to be needed, then check our estimate now and adjust
6291 the offset if necessary. */
6292 if (phdr_adjust_seg
!= NULL
)
6296 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6299 if (count
> phdr_adjust_num
)
6300 phdr_adjust_seg
->p_paddr
6301 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6306 #undef IS_CONTAINED_BY_VMA
6307 #undef IS_CONTAINED_BY_LMA
6309 #undef IS_COREFILE_NOTE
6310 #undef IS_SOLARIS_PT_INTERP
6311 #undef IS_SECTION_IN_INPUT_SEGMENT
6312 #undef INCLUDE_SECTION_IN_SEGMENT
6313 #undef SEGMENT_AFTER_SEGMENT
6314 #undef SEGMENT_OVERLAPS
6318 /* Copy ELF program header information. */
6321 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6323 Elf_Internal_Ehdr
*iehdr
;
6324 struct elf_segment_map
*map
;
6325 struct elf_segment_map
*map_first
;
6326 struct elf_segment_map
**pointer_to_map
;
6327 Elf_Internal_Phdr
*segment
;
6329 unsigned int num_segments
;
6330 bfd_boolean phdr_included
= FALSE
;
6331 bfd_boolean p_paddr_valid
;
6333 iehdr
= elf_elfheader (ibfd
);
6336 pointer_to_map
= &map_first
;
6338 /* If all the segment p_paddr fields are zero, don't set
6339 map->p_paddr_valid. */
6340 p_paddr_valid
= FALSE
;
6341 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6342 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6345 if (segment
->p_paddr
!= 0)
6347 p_paddr_valid
= TRUE
;
6351 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6356 unsigned int section_count
;
6358 Elf_Internal_Shdr
*this_hdr
;
6359 asection
*first_section
= NULL
;
6360 asection
*lowest_section
;
6362 /* Compute how many sections are in this segment. */
6363 for (section
= ibfd
->sections
, section_count
= 0;
6365 section
= section
->next
)
6367 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6368 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6370 if (first_section
== NULL
)
6371 first_section
= section
;
6376 /* Allocate a segment map big enough to contain
6377 all of the sections we have selected. */
6378 amt
= sizeof (struct elf_segment_map
);
6379 if (section_count
!= 0)
6380 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6381 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6385 /* Initialize the fields of the output segment map with the
6388 map
->p_type
= segment
->p_type
;
6389 map
->p_flags
= segment
->p_flags
;
6390 map
->p_flags_valid
= 1;
6391 map
->p_paddr
= segment
->p_paddr
;
6392 map
->p_paddr_valid
= p_paddr_valid
;
6393 map
->p_align
= segment
->p_align
;
6394 map
->p_align_valid
= 1;
6395 map
->p_vaddr_offset
= 0;
6397 if (map
->p_type
== PT_GNU_RELRO
6398 || map
->p_type
== PT_GNU_STACK
)
6400 /* The PT_GNU_RELRO segment may contain the first a few
6401 bytes in the .got.plt section even if the whole .got.plt
6402 section isn't in the PT_GNU_RELRO segment. We won't
6403 change the size of the PT_GNU_RELRO segment.
6404 Similarly, PT_GNU_STACK size is significant on uclinux
6406 map
->p_size
= segment
->p_memsz
;
6407 map
->p_size_valid
= 1;
6410 /* Determine if this segment contains the ELF file header
6411 and if it contains the program headers themselves. */
6412 map
->includes_filehdr
= (segment
->p_offset
== 0
6413 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6415 map
->includes_phdrs
= 0;
6416 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6418 map
->includes_phdrs
=
6419 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6420 && (segment
->p_offset
+ segment
->p_filesz
6421 >= ((bfd_vma
) iehdr
->e_phoff
6422 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6424 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6425 phdr_included
= TRUE
;
6428 lowest_section
= NULL
;
6429 if (section_count
!= 0)
6431 unsigned int isec
= 0;
6433 for (section
= first_section
;
6435 section
= section
->next
)
6437 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6438 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6440 map
->sections
[isec
++] = section
->output_section
;
6441 if ((section
->flags
& SEC_ALLOC
) != 0)
6445 if (lowest_section
== NULL
6446 || section
->lma
< lowest_section
->lma
)
6447 lowest_section
= section
;
6449 /* Section lmas are set up from PT_LOAD header
6450 p_paddr in _bfd_elf_make_section_from_shdr.
6451 If this header has a p_paddr that disagrees
6452 with the section lma, flag the p_paddr as
6454 if ((section
->flags
& SEC_LOAD
) != 0)
6455 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6457 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6458 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6459 map
->p_paddr_valid
= FALSE
;
6461 if (isec
== section_count
)
6467 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6468 /* We need to keep the space used by the headers fixed. */
6469 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6471 if (!map
->includes_phdrs
6472 && !map
->includes_filehdr
6473 && map
->p_paddr_valid
)
6474 /* There is some other padding before the first section. */
6475 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6476 - segment
->p_paddr
);
6478 map
->count
= section_count
;
6479 *pointer_to_map
= map
;
6480 pointer_to_map
= &map
->next
;
6483 elf_seg_map (obfd
) = map_first
;
6487 /* Copy private BFD data. This copies or rewrites ELF program header
6491 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6493 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6494 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6497 if (elf_tdata (ibfd
)->phdr
== NULL
)
6500 if (ibfd
->xvec
== obfd
->xvec
)
6502 /* Check to see if any sections in the input BFD
6503 covered by ELF program header have changed. */
6504 Elf_Internal_Phdr
*segment
;
6505 asection
*section
, *osec
;
6506 unsigned int i
, num_segments
;
6507 Elf_Internal_Shdr
*this_hdr
;
6508 const struct elf_backend_data
*bed
;
6510 bed
= get_elf_backend_data (ibfd
);
6512 /* Regenerate the segment map if p_paddr is set to 0. */
6513 if (bed
->want_p_paddr_set_to_zero
)
6516 /* Initialize the segment mark field. */
6517 for (section
= obfd
->sections
; section
!= NULL
;
6518 section
= section
->next
)
6519 section
->segment_mark
= FALSE
;
6521 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6522 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6526 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6527 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6528 which severly confuses things, so always regenerate the segment
6529 map in this case. */
6530 if (segment
->p_paddr
== 0
6531 && segment
->p_memsz
== 0
6532 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6535 for (section
= ibfd
->sections
;
6536 section
!= NULL
; section
= section
->next
)
6538 /* We mark the output section so that we know it comes
6539 from the input BFD. */
6540 osec
= section
->output_section
;
6542 osec
->segment_mark
= TRUE
;
6544 /* Check if this section is covered by the segment. */
6545 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6546 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6548 /* FIXME: Check if its output section is changed or
6549 removed. What else do we need to check? */
6551 || section
->flags
!= osec
->flags
6552 || section
->lma
!= osec
->lma
6553 || section
->vma
!= osec
->vma
6554 || section
->size
!= osec
->size
6555 || section
->rawsize
!= osec
->rawsize
6556 || section
->alignment_power
!= osec
->alignment_power
)
6562 /* Check to see if any output section do not come from the
6564 for (section
= obfd
->sections
; section
!= NULL
;
6565 section
= section
->next
)
6567 if (section
->segment_mark
== FALSE
)
6570 section
->segment_mark
= FALSE
;
6573 return copy_elf_program_header (ibfd
, obfd
);
6577 if (ibfd
->xvec
== obfd
->xvec
)
6579 /* When rewriting program header, set the output maxpagesize to
6580 the maximum alignment of input PT_LOAD segments. */
6581 Elf_Internal_Phdr
*segment
;
6583 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6584 bfd_vma maxpagesize
= 0;
6586 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6589 if (segment
->p_type
== PT_LOAD
6590 && maxpagesize
< segment
->p_align
)
6592 /* PR 17512: file: f17299af. */
6593 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
6594 (*_bfd_error_handler
) (_("\
6595 %B: warning: segment alignment of 0x%llx is too large"),
6596 ibfd
, (long long) segment
->p_align
);
6598 maxpagesize
= segment
->p_align
;
6601 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6602 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6605 return rewrite_elf_program_header (ibfd
, obfd
);
6608 /* Initialize private output section information from input section. */
6611 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6615 struct bfd_link_info
*link_info
)
6618 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6619 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6621 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6622 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6625 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6627 /* For objcopy and relocatable link, don't copy the output ELF
6628 section type from input if the output BFD section flags have been
6629 set to something different. For a final link allow some flags
6630 that the linker clears to differ. */
6631 if (elf_section_type (osec
) == SHT_NULL
6632 && (osec
->flags
== isec
->flags
6634 && ((osec
->flags
^ isec
->flags
)
6635 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6636 elf_section_type (osec
) = elf_section_type (isec
);
6638 /* FIXME: Is this correct for all OS/PROC specific flags? */
6639 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6640 & (SHF_MASKOS
| SHF_MASKPROC
));
6642 /* Set things up for objcopy and relocatable link. The output
6643 SHT_GROUP section will have its elf_next_in_group pointing back
6644 to the input group members. Ignore linker created group section.
6645 See elfNN_ia64_object_p in elfxx-ia64.c. */
6648 if (elf_sec_group (isec
) == NULL
6649 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6651 if (elf_section_flags (isec
) & SHF_GROUP
)
6652 elf_section_flags (osec
) |= SHF_GROUP
;
6653 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6654 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6658 ihdr
= &elf_section_data (isec
)->this_hdr
;
6660 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6661 don't use the output section of the linked-to section since it
6662 may be NULL at this point. */
6663 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6665 ohdr
= &elf_section_data (osec
)->this_hdr
;
6666 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6667 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6670 osec
->use_rela_p
= isec
->use_rela_p
;
6675 /* Copy private section information. This copies over the entsize
6676 field, and sometimes the info field. */
6679 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6684 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6686 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6687 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6690 ihdr
= &elf_section_data (isec
)->this_hdr
;
6691 ohdr
= &elf_section_data (osec
)->this_hdr
;
6693 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6695 if (ihdr
->sh_type
== SHT_SYMTAB
6696 || ihdr
->sh_type
== SHT_DYNSYM
6697 || ihdr
->sh_type
== SHT_GNU_verneed
6698 || ihdr
->sh_type
== SHT_GNU_verdef
)
6699 ohdr
->sh_info
= ihdr
->sh_info
;
6701 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6705 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6706 necessary if we are removing either the SHT_GROUP section or any of
6707 the group member sections. DISCARDED is the value that a section's
6708 output_section has if the section will be discarded, NULL when this
6709 function is called from objcopy, bfd_abs_section_ptr when called
6713 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6717 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6718 if (elf_section_type (isec
) == SHT_GROUP
)
6720 asection
*first
= elf_next_in_group (isec
);
6721 asection
*s
= first
;
6722 bfd_size_type removed
= 0;
6726 /* If this member section is being output but the
6727 SHT_GROUP section is not, then clear the group info
6728 set up by _bfd_elf_copy_private_section_data. */
6729 if (s
->output_section
!= discarded
6730 && isec
->output_section
== discarded
)
6732 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6733 elf_group_name (s
->output_section
) = NULL
;
6735 /* Conversely, if the member section is not being output
6736 but the SHT_GROUP section is, then adjust its size. */
6737 else if (s
->output_section
== discarded
6738 && isec
->output_section
!= discarded
)
6740 s
= elf_next_in_group (s
);
6746 if (discarded
!= NULL
)
6748 /* If we've been called for ld -r, then we need to
6749 adjust the input section size. This function may
6750 be called multiple times, so save the original
6752 if (isec
->rawsize
== 0)
6753 isec
->rawsize
= isec
->size
;
6754 isec
->size
= isec
->rawsize
- removed
;
6758 /* Adjust the output section size when called from
6760 isec
->output_section
->size
-= removed
;
6768 /* Copy private header information. */
6771 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6773 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6774 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6777 /* Copy over private BFD data if it has not already been copied.
6778 This must be done here, rather than in the copy_private_bfd_data
6779 entry point, because the latter is called after the section
6780 contents have been set, which means that the program headers have
6781 already been worked out. */
6782 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6784 if (! copy_private_bfd_data (ibfd
, obfd
))
6788 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6791 /* Copy private symbol information. If this symbol is in a section
6792 which we did not map into a BFD section, try to map the section
6793 index correctly. We use special macro definitions for the mapped
6794 section indices; these definitions are interpreted by the
6795 swap_out_syms function. */
6797 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6798 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6799 #define MAP_STRTAB (SHN_HIOS + 3)
6800 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6801 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6804 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6809 elf_symbol_type
*isym
, *osym
;
6811 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6812 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6815 isym
= elf_symbol_from (ibfd
, isymarg
);
6816 osym
= elf_symbol_from (obfd
, osymarg
);
6819 && isym
->internal_elf_sym
.st_shndx
!= 0
6821 && bfd_is_abs_section (isym
->symbol
.section
))
6825 shndx
= isym
->internal_elf_sym
.st_shndx
;
6826 if (shndx
== elf_onesymtab (ibfd
))
6827 shndx
= MAP_ONESYMTAB
;
6828 else if (shndx
== elf_dynsymtab (ibfd
))
6829 shndx
= MAP_DYNSYMTAB
;
6830 else if (shndx
== elf_strtab_sec (ibfd
))
6832 else if (shndx
== elf_shstrtab_sec (ibfd
))
6833 shndx
= MAP_SHSTRTAB
;
6834 else if (shndx
== elf_symtab_shndx (ibfd
))
6835 shndx
= MAP_SYM_SHNDX
;
6836 osym
->internal_elf_sym
.st_shndx
= shndx
;
6842 /* Swap out the symbols. */
6845 swap_out_syms (bfd
*abfd
,
6846 struct bfd_strtab_hash
**sttp
,
6849 const struct elf_backend_data
*bed
;
6852 struct bfd_strtab_hash
*stt
;
6853 Elf_Internal_Shdr
*symtab_hdr
;
6854 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6855 Elf_Internal_Shdr
*symstrtab_hdr
;
6856 bfd_byte
*outbound_syms
;
6857 bfd_byte
*outbound_shndx
;
6859 unsigned int num_locals
;
6861 bfd_boolean name_local_sections
;
6863 if (!elf_map_symbols (abfd
, &num_locals
))
6866 /* Dump out the symtabs. */
6867 stt
= _bfd_elf_stringtab_init ();
6871 bed
= get_elf_backend_data (abfd
);
6872 symcount
= bfd_get_symcount (abfd
);
6873 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6874 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6875 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6876 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6877 symtab_hdr
->sh_info
= num_locals
+ 1;
6878 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6880 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6881 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6883 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6884 bed
->s
->sizeof_sym
);
6885 if (outbound_syms
== NULL
)
6887 _bfd_stringtab_free (stt
);
6890 symtab_hdr
->contents
= outbound_syms
;
6892 outbound_shndx
= NULL
;
6893 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6894 if (symtab_shndx_hdr
->sh_name
!= 0)
6896 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6897 outbound_shndx
= (bfd_byte
*)
6898 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6899 if (outbound_shndx
== NULL
)
6901 _bfd_stringtab_free (stt
);
6905 symtab_shndx_hdr
->contents
= outbound_shndx
;
6906 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6907 symtab_shndx_hdr
->sh_size
= amt
;
6908 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6909 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6912 /* Now generate the data (for "contents"). */
6914 /* Fill in zeroth symbol and swap it out. */
6915 Elf_Internal_Sym sym
;
6921 sym
.st_shndx
= SHN_UNDEF
;
6922 sym
.st_target_internal
= 0;
6923 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6924 outbound_syms
+= bed
->s
->sizeof_sym
;
6925 if (outbound_shndx
!= NULL
)
6926 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6930 = (bed
->elf_backend_name_local_section_symbols
6931 && bed
->elf_backend_name_local_section_symbols (abfd
));
6933 syms
= bfd_get_outsymbols (abfd
);
6934 for (idx
= 0; idx
< symcount
; idx
++)
6936 Elf_Internal_Sym sym
;
6937 bfd_vma value
= syms
[idx
]->value
;
6938 elf_symbol_type
*type_ptr
;
6939 flagword flags
= syms
[idx
]->flags
;
6942 if (!name_local_sections
6943 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6945 /* Local section symbols have no name. */
6950 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6953 if (sym
.st_name
== (unsigned long) -1)
6955 _bfd_stringtab_free (stt
);
6960 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6962 if ((flags
& BSF_SECTION_SYM
) == 0
6963 && bfd_is_com_section (syms
[idx
]->section
))
6965 /* ELF common symbols put the alignment into the `value' field,
6966 and the size into the `size' field. This is backwards from
6967 how BFD handles it, so reverse it here. */
6968 sym
.st_size
= value
;
6969 if (type_ptr
== NULL
6970 || type_ptr
->internal_elf_sym
.st_value
== 0)
6971 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6973 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6974 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6975 (abfd
, syms
[idx
]->section
);
6979 asection
*sec
= syms
[idx
]->section
;
6982 if (sec
->output_section
)
6984 value
+= sec
->output_offset
;
6985 sec
= sec
->output_section
;
6988 /* Don't add in the section vma for relocatable output. */
6989 if (! relocatable_p
)
6991 sym
.st_value
= value
;
6992 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6994 if (bfd_is_abs_section (sec
)
6996 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6998 /* This symbol is in a real ELF section which we did
6999 not create as a BFD section. Undo the mapping done
7000 by copy_private_symbol_data. */
7001 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7005 shndx
= elf_onesymtab (abfd
);
7008 shndx
= elf_dynsymtab (abfd
);
7011 shndx
= elf_strtab_sec (abfd
);
7014 shndx
= elf_shstrtab_sec (abfd
);
7017 shndx
= elf_symtab_shndx (abfd
);
7026 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7028 if (shndx
== SHN_BAD
)
7032 /* Writing this would be a hell of a lot easier if
7033 we had some decent documentation on bfd, and
7034 knew what to expect of the library, and what to
7035 demand of applications. For example, it
7036 appears that `objcopy' might not set the
7037 section of a symbol to be a section that is
7038 actually in the output file. */
7039 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7042 _bfd_error_handler (_("\
7043 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7044 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7046 bfd_set_error (bfd_error_invalid_operation
);
7047 _bfd_stringtab_free (stt
);
7051 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7052 BFD_ASSERT (shndx
!= SHN_BAD
);
7056 sym
.st_shndx
= shndx
;
7059 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7061 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7062 type
= STT_GNU_IFUNC
;
7063 else if ((flags
& BSF_FUNCTION
) != 0)
7065 else if ((flags
& BSF_OBJECT
) != 0)
7067 else if ((flags
& BSF_RELC
) != 0)
7069 else if ((flags
& BSF_SRELC
) != 0)
7074 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7077 /* Processor-specific types. */
7078 if (type_ptr
!= NULL
7079 && bed
->elf_backend_get_symbol_type
)
7080 type
= ((*bed
->elf_backend_get_symbol_type
)
7081 (&type_ptr
->internal_elf_sym
, type
));
7083 if (flags
& BSF_SECTION_SYM
)
7085 if (flags
& BSF_GLOBAL
)
7086 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7088 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7090 else if (bfd_is_com_section (syms
[idx
]->section
))
7092 #ifdef USE_STT_COMMON
7093 if (type
== STT_OBJECT
)
7094 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
7097 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7099 else if (bfd_is_und_section (syms
[idx
]->section
))
7100 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7104 else if (flags
& BSF_FILE
)
7105 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7108 int bind
= STB_LOCAL
;
7110 if (flags
& BSF_LOCAL
)
7112 else if (flags
& BSF_GNU_UNIQUE
)
7113 bind
= STB_GNU_UNIQUE
;
7114 else if (flags
& BSF_WEAK
)
7116 else if (flags
& BSF_GLOBAL
)
7119 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7122 if (type_ptr
!= NULL
)
7124 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7125 sym
.st_target_internal
7126 = type_ptr
->internal_elf_sym
.st_target_internal
;
7131 sym
.st_target_internal
= 0;
7134 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
7135 outbound_syms
+= bed
->s
->sizeof_sym
;
7136 if (outbound_shndx
!= NULL
)
7137 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
7141 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
7142 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7144 symstrtab_hdr
->sh_flags
= 0;
7145 symstrtab_hdr
->sh_addr
= 0;
7146 symstrtab_hdr
->sh_entsize
= 0;
7147 symstrtab_hdr
->sh_link
= 0;
7148 symstrtab_hdr
->sh_info
= 0;
7149 symstrtab_hdr
->sh_addralign
= 1;
7154 /* Return the number of bytes required to hold the symtab vector.
7156 Note that we base it on the count plus 1, since we will null terminate
7157 the vector allocated based on this size. However, the ELF symbol table
7158 always has a dummy entry as symbol #0, so it ends up even. */
7161 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7165 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7167 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7168 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7170 symtab_size
-= sizeof (asymbol
*);
7176 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7180 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7182 if (elf_dynsymtab (abfd
) == 0)
7184 bfd_set_error (bfd_error_invalid_operation
);
7188 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7189 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7191 symtab_size
-= sizeof (asymbol
*);
7197 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7200 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7203 /* Canonicalize the relocs. */
7206 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7213 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7215 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7218 tblptr
= section
->relocation
;
7219 for (i
= 0; i
< section
->reloc_count
; i
++)
7220 *relptr
++ = tblptr
++;
7224 return section
->reloc_count
;
7228 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7230 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7231 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7234 bfd_get_symcount (abfd
) = symcount
;
7239 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7240 asymbol
**allocation
)
7242 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7243 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7246 bfd_get_dynamic_symcount (abfd
) = symcount
;
7250 /* Return the size required for the dynamic reloc entries. Any loadable
7251 section that was actually installed in the BFD, and has type SHT_REL
7252 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7253 dynamic reloc section. */
7256 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7261 if (elf_dynsymtab (abfd
) == 0)
7263 bfd_set_error (bfd_error_invalid_operation
);
7267 ret
= sizeof (arelent
*);
7268 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7269 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7270 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7271 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7272 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7273 * sizeof (arelent
*));
7278 /* Canonicalize the dynamic relocation entries. Note that we return the
7279 dynamic relocations as a single block, although they are actually
7280 associated with particular sections; the interface, which was
7281 designed for SunOS style shared libraries, expects that there is only
7282 one set of dynamic relocs. Any loadable section that was actually
7283 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7284 dynamic symbol table, is considered to be a dynamic reloc section. */
7287 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7291 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7295 if (elf_dynsymtab (abfd
) == 0)
7297 bfd_set_error (bfd_error_invalid_operation
);
7301 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7303 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7305 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7306 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7307 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7312 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7314 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7316 for (i
= 0; i
< count
; i
++)
7327 /* Read in the version information. */
7330 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7332 bfd_byte
*contents
= NULL
;
7333 unsigned int freeidx
= 0;
7335 if (elf_dynverref (abfd
) != 0)
7337 Elf_Internal_Shdr
*hdr
;
7338 Elf_External_Verneed
*everneed
;
7339 Elf_Internal_Verneed
*iverneed
;
7341 bfd_byte
*contents_end
;
7343 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7345 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7347 error_return_bad_verref
:
7348 (*_bfd_error_handler
)
7349 (_("%B: .gnu.version_r invalid entry"), abfd
);
7350 bfd_set_error (bfd_error_bad_value
);
7351 error_return_verref
:
7352 elf_tdata (abfd
)->verref
= NULL
;
7353 elf_tdata (abfd
)->cverrefs
= 0;
7357 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7358 if (contents
== NULL
)
7359 goto error_return_verref
;
7361 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7362 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7363 goto error_return_verref
;
7365 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7366 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7368 if (elf_tdata (abfd
)->verref
== NULL
)
7369 goto error_return_verref
;
7371 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7372 == sizeof (Elf_External_Vernaux
));
7373 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7374 everneed
= (Elf_External_Verneed
*) contents
;
7375 iverneed
= elf_tdata (abfd
)->verref
;
7376 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7378 Elf_External_Vernaux
*evernaux
;
7379 Elf_Internal_Vernaux
*ivernaux
;
7382 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7384 iverneed
->vn_bfd
= abfd
;
7386 iverneed
->vn_filename
=
7387 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7389 if (iverneed
->vn_filename
== NULL
)
7390 goto error_return_bad_verref
;
7392 if (iverneed
->vn_cnt
== 0)
7393 iverneed
->vn_auxptr
= NULL
;
7396 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7397 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7398 sizeof (Elf_Internal_Vernaux
));
7399 if (iverneed
->vn_auxptr
== NULL
)
7400 goto error_return_verref
;
7403 if (iverneed
->vn_aux
7404 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7405 goto error_return_bad_verref
;
7407 evernaux
= ((Elf_External_Vernaux
*)
7408 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7409 ivernaux
= iverneed
->vn_auxptr
;
7410 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7412 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7414 ivernaux
->vna_nodename
=
7415 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7416 ivernaux
->vna_name
);
7417 if (ivernaux
->vna_nodename
== NULL
)
7418 goto error_return_bad_verref
;
7420 if (ivernaux
->vna_other
> freeidx
)
7421 freeidx
= ivernaux
->vna_other
;
7423 ivernaux
->vna_nextptr
= NULL
;
7424 if (ivernaux
->vna_next
== 0)
7426 iverneed
->vn_cnt
= j
+ 1;
7429 if (j
+ 1 < iverneed
->vn_cnt
)
7430 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7432 if (ivernaux
->vna_next
7433 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7434 goto error_return_bad_verref
;
7436 evernaux
= ((Elf_External_Vernaux
*)
7437 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7440 iverneed
->vn_nextref
= NULL
;
7441 if (iverneed
->vn_next
== 0)
7443 if (i
+ 1 < hdr
->sh_info
)
7444 iverneed
->vn_nextref
= iverneed
+ 1;
7446 if (iverneed
->vn_next
7447 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7448 goto error_return_bad_verref
;
7450 everneed
= ((Elf_External_Verneed
*)
7451 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7453 elf_tdata (abfd
)->cverrefs
= i
;
7459 if (elf_dynverdef (abfd
) != 0)
7461 Elf_Internal_Shdr
*hdr
;
7462 Elf_External_Verdef
*everdef
;
7463 Elf_Internal_Verdef
*iverdef
;
7464 Elf_Internal_Verdef
*iverdefarr
;
7465 Elf_Internal_Verdef iverdefmem
;
7467 unsigned int maxidx
;
7468 bfd_byte
*contents_end_def
, *contents_end_aux
;
7470 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7472 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7474 error_return_bad_verdef
:
7475 (*_bfd_error_handler
)
7476 (_("%B: .gnu.version_d invalid entry"), abfd
);
7477 bfd_set_error (bfd_error_bad_value
);
7478 error_return_verdef
:
7479 elf_tdata (abfd
)->verdef
= NULL
;
7480 elf_tdata (abfd
)->cverdefs
= 0;
7484 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7485 if (contents
== NULL
)
7486 goto error_return_verdef
;
7487 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7488 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7489 goto error_return_verdef
;
7491 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7492 >= sizeof (Elf_External_Verdaux
));
7493 contents_end_def
= contents
+ hdr
->sh_size
7494 - sizeof (Elf_External_Verdef
);
7495 contents_end_aux
= contents
+ hdr
->sh_size
7496 - sizeof (Elf_External_Verdaux
);
7498 /* We know the number of entries in the section but not the maximum
7499 index. Therefore we have to run through all entries and find
7501 everdef
= (Elf_External_Verdef
*) contents
;
7503 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7505 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7507 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
7508 goto error_return_bad_verdef
;
7509 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7510 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7512 if (iverdefmem
.vd_next
== 0)
7515 if (iverdefmem
.vd_next
7516 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7517 goto error_return_bad_verdef
;
7519 everdef
= ((Elf_External_Verdef
*)
7520 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7523 if (default_imported_symver
)
7525 if (freeidx
> maxidx
)
7531 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7532 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7533 if (elf_tdata (abfd
)->verdef
== NULL
)
7534 goto error_return_verdef
;
7536 elf_tdata (abfd
)->cverdefs
= maxidx
;
7538 everdef
= (Elf_External_Verdef
*) contents
;
7539 iverdefarr
= elf_tdata (abfd
)->verdef
;
7540 for (i
= 0; i
< hdr
->sh_info
; i
++)
7542 Elf_External_Verdaux
*everdaux
;
7543 Elf_Internal_Verdaux
*iverdaux
;
7546 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7548 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7549 goto error_return_bad_verdef
;
7551 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7552 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7554 iverdef
->vd_bfd
= abfd
;
7556 if (iverdef
->vd_cnt
== 0)
7557 iverdef
->vd_auxptr
= NULL
;
7560 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7561 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7562 sizeof (Elf_Internal_Verdaux
));
7563 if (iverdef
->vd_auxptr
== NULL
)
7564 goto error_return_verdef
;
7568 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7569 goto error_return_bad_verdef
;
7571 everdaux
= ((Elf_External_Verdaux
*)
7572 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7573 iverdaux
= iverdef
->vd_auxptr
;
7574 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7576 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7578 iverdaux
->vda_nodename
=
7579 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7580 iverdaux
->vda_name
);
7581 if (iverdaux
->vda_nodename
== NULL
)
7582 goto error_return_bad_verdef
;
7584 iverdaux
->vda_nextptr
= NULL
;
7585 if (iverdaux
->vda_next
== 0)
7587 iverdef
->vd_cnt
= j
+ 1;
7590 if (j
+ 1 < iverdef
->vd_cnt
)
7591 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7593 if (iverdaux
->vda_next
7594 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7595 goto error_return_bad_verdef
;
7597 everdaux
= ((Elf_External_Verdaux
*)
7598 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7601 if (iverdef
->vd_cnt
)
7602 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7604 iverdef
->vd_nextdef
= NULL
;
7605 if (iverdef
->vd_next
== 0)
7607 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7608 iverdef
->vd_nextdef
= iverdef
+ 1;
7610 everdef
= ((Elf_External_Verdef
*)
7611 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7617 else if (default_imported_symver
)
7624 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7625 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7626 if (elf_tdata (abfd
)->verdef
== NULL
)
7629 elf_tdata (abfd
)->cverdefs
= freeidx
;
7632 /* Create a default version based on the soname. */
7633 if (default_imported_symver
)
7635 Elf_Internal_Verdef
*iverdef
;
7636 Elf_Internal_Verdaux
*iverdaux
;
7638 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7640 iverdef
->vd_version
= VER_DEF_CURRENT
;
7641 iverdef
->vd_flags
= 0;
7642 iverdef
->vd_ndx
= freeidx
;
7643 iverdef
->vd_cnt
= 1;
7645 iverdef
->vd_bfd
= abfd
;
7647 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7648 if (iverdef
->vd_nodename
== NULL
)
7649 goto error_return_verdef
;
7650 iverdef
->vd_nextdef
= NULL
;
7651 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
7652 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
7653 if (iverdef
->vd_auxptr
== NULL
)
7654 goto error_return_verdef
;
7656 iverdaux
= iverdef
->vd_auxptr
;
7657 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7663 if (contents
!= NULL
)
7669 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7671 elf_symbol_type
*newsym
;
7673 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
7676 newsym
->symbol
.the_bfd
= abfd
;
7677 return &newsym
->symbol
;
7681 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7685 bfd_symbol_info (symbol
, ret
);
7688 /* Return whether a symbol name implies a local symbol. Most targets
7689 use this function for the is_local_label_name entry point, but some
7693 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7696 /* Normal local symbols start with ``.L''. */
7697 if (name
[0] == '.' && name
[1] == 'L')
7700 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7701 DWARF debugging symbols starting with ``..''. */
7702 if (name
[0] == '.' && name
[1] == '.')
7705 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7706 emitting DWARF debugging output. I suspect this is actually a
7707 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7708 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7709 underscore to be emitted on some ELF targets). For ease of use,
7710 we treat such symbols as local. */
7711 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7718 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7719 asymbol
*symbol ATTRIBUTE_UNUSED
)
7726 _bfd_elf_set_arch_mach (bfd
*abfd
,
7727 enum bfd_architecture arch
,
7728 unsigned long machine
)
7730 /* If this isn't the right architecture for this backend, and this
7731 isn't the generic backend, fail. */
7732 if (arch
!= get_elf_backend_data (abfd
)->arch
7733 && arch
!= bfd_arch_unknown
7734 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7737 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7740 /* Find the nearest line to a particular section and offset,
7741 for error reporting. */
7744 _bfd_elf_find_nearest_line (bfd
*abfd
,
7748 const char **filename_ptr
,
7749 const char **functionname_ptr
,
7750 unsigned int *line_ptr
,
7751 unsigned int *discriminator_ptr
)
7755 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
7756 filename_ptr
, functionname_ptr
,
7757 line_ptr
, discriminator_ptr
,
7758 dwarf_debug_sections
, 0,
7759 &elf_tdata (abfd
)->dwarf2_find_line_info
)
7760 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
7761 filename_ptr
, functionname_ptr
,
7764 if (!*functionname_ptr
)
7765 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
7766 *filename_ptr
? NULL
: filename_ptr
,
7771 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7772 &found
, filename_ptr
,
7773 functionname_ptr
, line_ptr
,
7774 &elf_tdata (abfd
)->line_info
))
7776 if (found
&& (*functionname_ptr
|| *line_ptr
))
7779 if (symbols
== NULL
)
7782 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
7783 filename_ptr
, functionname_ptr
))
7790 /* Find the line for a symbol. */
7793 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7794 const char **filename_ptr
, unsigned int *line_ptr
)
7796 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
7797 filename_ptr
, NULL
, line_ptr
, NULL
,
7798 dwarf_debug_sections
, 0,
7799 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7802 /* After a call to bfd_find_nearest_line, successive calls to
7803 bfd_find_inliner_info can be used to get source information about
7804 each level of function inlining that terminated at the address
7805 passed to bfd_find_nearest_line. Currently this is only supported
7806 for DWARF2 with appropriate DWARF3 extensions. */
7809 _bfd_elf_find_inliner_info (bfd
*abfd
,
7810 const char **filename_ptr
,
7811 const char **functionname_ptr
,
7812 unsigned int *line_ptr
)
7815 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7816 functionname_ptr
, line_ptr
,
7817 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7822 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7824 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7825 int ret
= bed
->s
->sizeof_ehdr
;
7827 if (!info
->relocatable
)
7829 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
7831 if (phdr_size
== (bfd_size_type
) -1)
7833 struct elf_segment_map
*m
;
7836 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
7837 phdr_size
+= bed
->s
->sizeof_phdr
;
7840 phdr_size
= get_program_header_size (abfd
, info
);
7843 elf_program_header_size (abfd
) = phdr_size
;
7851 _bfd_elf_set_section_contents (bfd
*abfd
,
7853 const void *location
,
7855 bfd_size_type count
)
7857 Elf_Internal_Shdr
*hdr
;
7860 if (! abfd
->output_has_begun
7861 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7864 hdr
= &elf_section_data (section
)->this_hdr
;
7865 pos
= hdr
->sh_offset
+ offset
;
7866 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7867 || bfd_bwrite (location
, count
, abfd
) != count
)
7874 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7875 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7876 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7881 /* Try to convert a non-ELF reloc into an ELF one. */
7884 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7886 /* Check whether we really have an ELF howto. */
7888 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7890 bfd_reloc_code_real_type code
;
7891 reloc_howto_type
*howto
;
7893 /* Alien reloc: Try to determine its type to replace it with an
7894 equivalent ELF reloc. */
7896 if (areloc
->howto
->pc_relative
)
7898 switch (areloc
->howto
->bitsize
)
7901 code
= BFD_RELOC_8_PCREL
;
7904 code
= BFD_RELOC_12_PCREL
;
7907 code
= BFD_RELOC_16_PCREL
;
7910 code
= BFD_RELOC_24_PCREL
;
7913 code
= BFD_RELOC_32_PCREL
;
7916 code
= BFD_RELOC_64_PCREL
;
7922 howto
= bfd_reloc_type_lookup (abfd
, code
);
7924 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7926 if (howto
->pcrel_offset
)
7927 areloc
->addend
+= areloc
->address
;
7929 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7934 switch (areloc
->howto
->bitsize
)
7940 code
= BFD_RELOC_14
;
7943 code
= BFD_RELOC_16
;
7946 code
= BFD_RELOC_26
;
7949 code
= BFD_RELOC_32
;
7952 code
= BFD_RELOC_64
;
7958 howto
= bfd_reloc_type_lookup (abfd
, code
);
7962 areloc
->howto
= howto
;
7970 (*_bfd_error_handler
)
7971 (_("%B: unsupported relocation type %s"),
7972 abfd
, areloc
->howto
->name
);
7973 bfd_set_error (bfd_error_bad_value
);
7978 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7980 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
7981 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
7983 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
7984 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7985 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
7988 return _bfd_generic_close_and_cleanup (abfd
);
7991 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7992 in the relocation's offset. Thus we cannot allow any sort of sanity
7993 range-checking to interfere. There is nothing else to do in processing
7996 bfd_reloc_status_type
7997 _bfd_elf_rel_vtable_reloc_fn
7998 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7999 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8000 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8001 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8003 return bfd_reloc_ok
;
8006 /* Elf core file support. Much of this only works on native
8007 toolchains, since we rely on knowing the
8008 machine-dependent procfs structure in order to pick
8009 out details about the corefile. */
8011 #ifdef HAVE_SYS_PROCFS_H
8012 /* Needed for new procfs interface on sparc-solaris. */
8013 # define _STRUCTURED_PROC 1
8014 # include <sys/procfs.h>
8017 /* Return a PID that identifies a "thread" for threaded cores, or the
8018 PID of the main process for non-threaded cores. */
8021 elfcore_make_pid (bfd
*abfd
)
8025 pid
= elf_tdata (abfd
)->core
->lwpid
;
8027 pid
= elf_tdata (abfd
)->core
->pid
;
8032 /* If there isn't a section called NAME, make one, using
8033 data from SECT. Note, this function will generate a
8034 reference to NAME, so you shouldn't deallocate or
8038 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8042 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8045 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8049 sect2
->size
= sect
->size
;
8050 sect2
->filepos
= sect
->filepos
;
8051 sect2
->alignment_power
= sect
->alignment_power
;
8055 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8056 actually creates up to two pseudosections:
8057 - For the single-threaded case, a section named NAME, unless
8058 such a section already exists.
8059 - For the multi-threaded case, a section named "NAME/PID", where
8060 PID is elfcore_make_pid (abfd).
8061 Both pseudosections have identical contents. */
8063 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8069 char *threaded_name
;
8073 /* Build the section name. */
8075 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8076 len
= strlen (buf
) + 1;
8077 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8078 if (threaded_name
== NULL
)
8080 memcpy (threaded_name
, buf
, len
);
8082 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8087 sect
->filepos
= filepos
;
8088 sect
->alignment_power
= 2;
8090 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8093 /* prstatus_t exists on:
8095 linux 2.[01] + glibc
8099 #if defined (HAVE_PRSTATUS_T)
8102 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8107 if (note
->descsz
== sizeof (prstatus_t
))
8111 size
= sizeof (prstat
.pr_reg
);
8112 offset
= offsetof (prstatus_t
, pr_reg
);
8113 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8115 /* Do not overwrite the core signal if it
8116 has already been set by another thread. */
8117 if (elf_tdata (abfd
)->core
->signal
== 0)
8118 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8119 if (elf_tdata (abfd
)->core
->pid
== 0)
8120 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8122 /* pr_who exists on:
8125 pr_who doesn't exist on:
8128 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8129 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8131 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8134 #if defined (HAVE_PRSTATUS32_T)
8135 else if (note
->descsz
== sizeof (prstatus32_t
))
8137 /* 64-bit host, 32-bit corefile */
8138 prstatus32_t prstat
;
8140 size
= sizeof (prstat
.pr_reg
);
8141 offset
= offsetof (prstatus32_t
, pr_reg
);
8142 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8144 /* Do not overwrite the core signal if it
8145 has already been set by another thread. */
8146 if (elf_tdata (abfd
)->core
->signal
== 0)
8147 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8148 if (elf_tdata (abfd
)->core
->pid
== 0)
8149 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8151 /* pr_who exists on:
8154 pr_who doesn't exist on:
8157 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8158 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8160 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8163 #endif /* HAVE_PRSTATUS32_T */
8166 /* Fail - we don't know how to handle any other
8167 note size (ie. data object type). */
8171 /* Make a ".reg/999" section and a ".reg" section. */
8172 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8173 size
, note
->descpos
+ offset
);
8175 #endif /* defined (HAVE_PRSTATUS_T) */
8177 /* Create a pseudosection containing the exact contents of NOTE. */
8179 elfcore_make_note_pseudosection (bfd
*abfd
,
8181 Elf_Internal_Note
*note
)
8183 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8184 note
->descsz
, note
->descpos
);
8187 /* There isn't a consistent prfpregset_t across platforms,
8188 but it doesn't matter, because we don't have to pick this
8189 data structure apart. */
8192 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8194 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8197 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8198 type of NT_PRXFPREG. Just include the whole note's contents
8202 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8204 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8207 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8208 with a note type of NT_X86_XSTATE. Just include the whole note's
8209 contents literally. */
8212 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8214 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8218 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8220 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8224 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8226 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8230 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8232 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8236 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8238 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8242 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8244 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8248 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8250 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8254 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8256 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8260 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8262 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8266 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8268 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8272 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8274 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8278 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8280 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8284 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
8286 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
8290 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
8292 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
8296 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8298 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8302 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8304 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8308 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8310 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8314 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8316 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8319 #if defined (HAVE_PRPSINFO_T)
8320 typedef prpsinfo_t elfcore_psinfo_t
;
8321 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8322 typedef prpsinfo32_t elfcore_psinfo32_t
;
8326 #if defined (HAVE_PSINFO_T)
8327 typedef psinfo_t elfcore_psinfo_t
;
8328 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8329 typedef psinfo32_t elfcore_psinfo32_t
;
8333 /* return a malloc'ed copy of a string at START which is at
8334 most MAX bytes long, possibly without a terminating '\0'.
8335 the copy will always have a terminating '\0'. */
8338 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8341 char *end
= (char *) memchr (start
, '\0', max
);
8349 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8353 memcpy (dups
, start
, len
);
8359 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8361 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8363 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8365 elfcore_psinfo_t psinfo
;
8367 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8369 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8370 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8372 elf_tdata (abfd
)->core
->program
8373 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8374 sizeof (psinfo
.pr_fname
));
8376 elf_tdata (abfd
)->core
->command
8377 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8378 sizeof (psinfo
.pr_psargs
));
8380 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8381 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8383 /* 64-bit host, 32-bit corefile */
8384 elfcore_psinfo32_t psinfo
;
8386 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8388 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8389 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8391 elf_tdata (abfd
)->core
->program
8392 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8393 sizeof (psinfo
.pr_fname
));
8395 elf_tdata (abfd
)->core
->command
8396 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8397 sizeof (psinfo
.pr_psargs
));
8403 /* Fail - we don't know how to handle any other
8404 note size (ie. data object type). */
8408 /* Note that for some reason, a spurious space is tacked
8409 onto the end of the args in some (at least one anyway)
8410 implementations, so strip it off if it exists. */
8413 char *command
= elf_tdata (abfd
)->core
->command
;
8414 int n
= strlen (command
);
8416 if (0 < n
&& command
[n
- 1] == ' ')
8417 command
[n
- 1] = '\0';
8422 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8424 #if defined (HAVE_PSTATUS_T)
8426 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8428 if (note
->descsz
== sizeof (pstatus_t
)
8429 #if defined (HAVE_PXSTATUS_T)
8430 || note
->descsz
== sizeof (pxstatus_t
)
8436 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8438 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8440 #if defined (HAVE_PSTATUS32_T)
8441 else if (note
->descsz
== sizeof (pstatus32_t
))
8443 /* 64-bit host, 32-bit corefile */
8446 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8448 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8451 /* Could grab some more details from the "representative"
8452 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8453 NT_LWPSTATUS note, presumably. */
8457 #endif /* defined (HAVE_PSTATUS_T) */
8459 #if defined (HAVE_LWPSTATUS_T)
8461 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8463 lwpstatus_t lwpstat
;
8469 if (note
->descsz
!= sizeof (lwpstat
)
8470 #if defined (HAVE_LWPXSTATUS_T)
8471 && note
->descsz
!= sizeof (lwpxstatus_t
)
8476 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8478 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8479 /* Do not overwrite the core signal if it has already been set by
8481 if (elf_tdata (abfd
)->core
->signal
== 0)
8482 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8484 /* Make a ".reg/999" section. */
8486 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8487 len
= strlen (buf
) + 1;
8488 name
= bfd_alloc (abfd
, len
);
8491 memcpy (name
, buf
, len
);
8493 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8497 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8498 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8499 sect
->filepos
= note
->descpos
8500 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8503 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8504 sect
->size
= sizeof (lwpstat
.pr_reg
);
8505 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8508 sect
->alignment_power
= 2;
8510 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8513 /* Make a ".reg2/999" section */
8515 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8516 len
= strlen (buf
) + 1;
8517 name
= bfd_alloc (abfd
, len
);
8520 memcpy (name
, buf
, len
);
8522 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8526 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8527 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8528 sect
->filepos
= note
->descpos
8529 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8532 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8533 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8534 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8537 sect
->alignment_power
= 2;
8539 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8541 #endif /* defined (HAVE_LWPSTATUS_T) */
8544 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8551 int is_active_thread
;
8554 if (note
->descsz
< 728)
8557 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8560 type
= bfd_get_32 (abfd
, note
->descdata
);
8564 case 1 /* NOTE_INFO_PROCESS */:
8565 /* FIXME: need to add ->core->command. */
8566 /* process_info.pid */
8567 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8568 /* process_info.signal */
8569 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8572 case 2 /* NOTE_INFO_THREAD */:
8573 /* Make a ".reg/999" section. */
8574 /* thread_info.tid */
8575 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8577 len
= strlen (buf
) + 1;
8578 name
= (char *) bfd_alloc (abfd
, len
);
8582 memcpy (name
, buf
, len
);
8584 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8588 /* sizeof (thread_info.thread_context) */
8590 /* offsetof (thread_info.thread_context) */
8591 sect
->filepos
= note
->descpos
+ 12;
8592 sect
->alignment_power
= 2;
8594 /* thread_info.is_active_thread */
8595 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8597 if (is_active_thread
)
8598 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8602 case 3 /* NOTE_INFO_MODULE */:
8603 /* Make a ".module/xxxxxxxx" section. */
8604 /* module_info.base_address */
8605 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8606 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8608 len
= strlen (buf
) + 1;
8609 name
= (char *) bfd_alloc (abfd
, len
);
8613 memcpy (name
, buf
, len
);
8615 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8620 sect
->size
= note
->descsz
;
8621 sect
->filepos
= note
->descpos
;
8622 sect
->alignment_power
= 2;
8633 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8635 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8643 if (bed
->elf_backend_grok_prstatus
)
8644 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8646 #if defined (HAVE_PRSTATUS_T)
8647 return elfcore_grok_prstatus (abfd
, note
);
8652 #if defined (HAVE_PSTATUS_T)
8654 return elfcore_grok_pstatus (abfd
, note
);
8657 #if defined (HAVE_LWPSTATUS_T)
8659 return elfcore_grok_lwpstatus (abfd
, note
);
8662 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8663 return elfcore_grok_prfpreg (abfd
, note
);
8665 case NT_WIN32PSTATUS
:
8666 return elfcore_grok_win32pstatus (abfd
, note
);
8668 case NT_PRXFPREG
: /* Linux SSE extension */
8669 if (note
->namesz
== 6
8670 && strcmp (note
->namedata
, "LINUX") == 0)
8671 return elfcore_grok_prxfpreg (abfd
, note
);
8675 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8676 if (note
->namesz
== 6
8677 && strcmp (note
->namedata
, "LINUX") == 0)
8678 return elfcore_grok_xstatereg (abfd
, note
);
8683 if (note
->namesz
== 6
8684 && strcmp (note
->namedata
, "LINUX") == 0)
8685 return elfcore_grok_ppc_vmx (abfd
, note
);
8690 if (note
->namesz
== 6
8691 && strcmp (note
->namedata
, "LINUX") == 0)
8692 return elfcore_grok_ppc_vsx (abfd
, note
);
8696 case NT_S390_HIGH_GPRS
:
8697 if (note
->namesz
== 6
8698 && strcmp (note
->namedata
, "LINUX") == 0)
8699 return elfcore_grok_s390_high_gprs (abfd
, note
);
8704 if (note
->namesz
== 6
8705 && strcmp (note
->namedata
, "LINUX") == 0)
8706 return elfcore_grok_s390_timer (abfd
, note
);
8710 case NT_S390_TODCMP
:
8711 if (note
->namesz
== 6
8712 && strcmp (note
->namedata
, "LINUX") == 0)
8713 return elfcore_grok_s390_todcmp (abfd
, note
);
8717 case NT_S390_TODPREG
:
8718 if (note
->namesz
== 6
8719 && strcmp (note
->namedata
, "LINUX") == 0)
8720 return elfcore_grok_s390_todpreg (abfd
, note
);
8725 if (note
->namesz
== 6
8726 && strcmp (note
->namedata
, "LINUX") == 0)
8727 return elfcore_grok_s390_ctrs (abfd
, note
);
8731 case NT_S390_PREFIX
:
8732 if (note
->namesz
== 6
8733 && strcmp (note
->namedata
, "LINUX") == 0)
8734 return elfcore_grok_s390_prefix (abfd
, note
);
8738 case NT_S390_LAST_BREAK
:
8739 if (note
->namesz
== 6
8740 && strcmp (note
->namedata
, "LINUX") == 0)
8741 return elfcore_grok_s390_last_break (abfd
, note
);
8745 case NT_S390_SYSTEM_CALL
:
8746 if (note
->namesz
== 6
8747 && strcmp (note
->namedata
, "LINUX") == 0)
8748 return elfcore_grok_s390_system_call (abfd
, note
);
8753 if (note
->namesz
== 6
8754 && strcmp (note
->namedata
, "LINUX") == 0)
8755 return elfcore_grok_s390_tdb (abfd
, note
);
8759 case NT_S390_VXRS_LOW
:
8760 if (note
->namesz
== 6
8761 && strcmp (note
->namedata
, "LINUX") == 0)
8762 return elfcore_grok_s390_vxrs_low (abfd
, note
);
8766 case NT_S390_VXRS_HIGH
:
8767 if (note
->namesz
== 6
8768 && strcmp (note
->namedata
, "LINUX") == 0)
8769 return elfcore_grok_s390_vxrs_high (abfd
, note
);
8774 if (note
->namesz
== 6
8775 && strcmp (note
->namedata
, "LINUX") == 0)
8776 return elfcore_grok_arm_vfp (abfd
, note
);
8781 if (note
->namesz
== 6
8782 && strcmp (note
->namedata
, "LINUX") == 0)
8783 return elfcore_grok_aarch_tls (abfd
, note
);
8787 case NT_ARM_HW_BREAK
:
8788 if (note
->namesz
== 6
8789 && strcmp (note
->namedata
, "LINUX") == 0)
8790 return elfcore_grok_aarch_hw_break (abfd
, note
);
8794 case NT_ARM_HW_WATCH
:
8795 if (note
->namesz
== 6
8796 && strcmp (note
->namedata
, "LINUX") == 0)
8797 return elfcore_grok_aarch_hw_watch (abfd
, note
);
8803 if (bed
->elf_backend_grok_psinfo
)
8804 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8806 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8807 return elfcore_grok_psinfo (abfd
, note
);
8814 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8819 sect
->size
= note
->descsz
;
8820 sect
->filepos
= note
->descpos
;
8821 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8827 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
8831 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
8837 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8839 struct elf_obj_tdata
*t
;
8841 if (note
->descsz
== 0)
8844 t
= elf_tdata (abfd
);
8845 t
->build_id
= bfd_alloc (abfd
, sizeof (*t
->build_id
) - 1 + note
->descsz
);
8846 if (t
->build_id
== NULL
)
8849 t
->build_id
->size
= note
->descsz
;
8850 memcpy (t
->build_id
->data
, note
->descdata
, note
->descsz
);
8856 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8863 case NT_GNU_BUILD_ID
:
8864 return elfobj_grok_gnu_build_id (abfd
, note
);
8869 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
8871 struct sdt_note
*cur
=
8872 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
8875 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
8876 cur
->size
= (bfd_size_type
) note
->descsz
;
8877 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
8879 elf_tdata (abfd
)->sdt_note_head
= cur
;
8885 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8890 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
8898 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8902 cp
= strchr (note
->namedata
, '@');
8905 *lwpidp
= atoi(cp
+ 1);
8912 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8914 /* Signal number at offset 0x08. */
8915 elf_tdata (abfd
)->core
->signal
8916 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8918 /* Process ID at offset 0x50. */
8919 elf_tdata (abfd
)->core
->pid
8920 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8922 /* Command name at 0x7c (max 32 bytes, including nul). */
8923 elf_tdata (abfd
)->core
->command
8924 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8926 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8931 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8935 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8936 elf_tdata (abfd
)->core
->lwpid
= lwp
;
8938 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8940 /* NetBSD-specific core "procinfo". Note that we expect to
8941 find this note before any of the others, which is fine,
8942 since the kernel writes this note out first when it
8943 creates a core file. */
8945 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8948 /* As of Jan 2002 there are no other machine-independent notes
8949 defined for NetBSD core files. If the note type is less
8950 than the start of the machine-dependent note types, we don't
8953 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8957 switch (bfd_get_arch (abfd
))
8959 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8960 PT_GETFPREGS == mach+2. */
8962 case bfd_arch_alpha
:
8963 case bfd_arch_sparc
:
8966 case NT_NETBSDCORE_FIRSTMACH
+0:
8967 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8969 case NT_NETBSDCORE_FIRSTMACH
+2:
8970 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8976 /* On all other arch's, PT_GETREGS == mach+1 and
8977 PT_GETFPREGS == mach+3. */
8982 case NT_NETBSDCORE_FIRSTMACH
+1:
8983 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8985 case NT_NETBSDCORE_FIRSTMACH
+3:
8986 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8996 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8998 /* Signal number at offset 0x08. */
8999 elf_tdata (abfd
)->core
->signal
9000 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9002 /* Process ID at offset 0x20. */
9003 elf_tdata (abfd
)->core
->pid
9004 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9006 /* Command name at 0x48 (max 32 bytes, including nul). */
9007 elf_tdata (abfd
)->core
->command
9008 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9014 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9016 if (note
->type
== NT_OPENBSD_PROCINFO
)
9017 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9019 if (note
->type
== NT_OPENBSD_REGS
)
9020 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9022 if (note
->type
== NT_OPENBSD_FPREGS
)
9023 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9025 if (note
->type
== NT_OPENBSD_XFPREGS
)
9026 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9028 if (note
->type
== NT_OPENBSD_AUXV
)
9030 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9035 sect
->size
= note
->descsz
;
9036 sect
->filepos
= note
->descpos
;
9037 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9042 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9044 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9049 sect
->size
= note
->descsz
;
9050 sect
->filepos
= note
->descpos
;
9051 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9060 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9062 void *ddata
= note
->descdata
;
9069 /* nto_procfs_status 'pid' field is at offset 0. */
9070 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9072 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9073 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9075 /* nto_procfs_status 'flags' field is at offset 8. */
9076 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9078 /* nto_procfs_status 'what' field is at offset 14. */
9079 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9081 elf_tdata (abfd
)->core
->signal
= sig
;
9082 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9085 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9086 do not come from signals so we make sure we set the current
9087 thread just in case. */
9088 if (flags
& 0x00000080)
9089 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9091 /* Make a ".qnx_core_status/%d" section. */
9092 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9094 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9099 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9103 sect
->size
= note
->descsz
;
9104 sect
->filepos
= note
->descpos
;
9105 sect
->alignment_power
= 2;
9107 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9111 elfcore_grok_nto_regs (bfd
*abfd
,
9112 Elf_Internal_Note
*note
,
9120 /* Make a "(base)/%d" section. */
9121 sprintf (buf
, "%s/%ld", base
, tid
);
9123 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9128 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9132 sect
->size
= note
->descsz
;
9133 sect
->filepos
= note
->descpos
;
9134 sect
->alignment_power
= 2;
9136 /* This is the current thread. */
9137 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9138 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9143 #define BFD_QNT_CORE_INFO 7
9144 #define BFD_QNT_CORE_STATUS 8
9145 #define BFD_QNT_CORE_GREG 9
9146 #define BFD_QNT_CORE_FPREG 10
9149 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9151 /* Every GREG section has a STATUS section before it. Store the
9152 tid from the previous call to pass down to the next gregs
9154 static long tid
= 1;
9158 case BFD_QNT_CORE_INFO
:
9159 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9160 case BFD_QNT_CORE_STATUS
:
9161 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9162 case BFD_QNT_CORE_GREG
:
9163 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9164 case BFD_QNT_CORE_FPREG
:
9165 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9172 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9178 /* Use note name as section name. */
9180 name
= (char *) bfd_alloc (abfd
, len
);
9183 memcpy (name
, note
->namedata
, len
);
9184 name
[len
- 1] = '\0';
9186 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9190 sect
->size
= note
->descsz
;
9191 sect
->filepos
= note
->descpos
;
9192 sect
->alignment_power
= 1;
9197 /* Function: elfcore_write_note
9200 buffer to hold note, and current size of buffer
9204 size of data for note
9206 Writes note to end of buffer. ELF64 notes are written exactly as
9207 for ELF32, despite the current (as of 2006) ELF gabi specifying
9208 that they ought to have 8-byte namesz and descsz field, and have
9209 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9212 Pointer to realloc'd buffer, *BUFSIZ updated. */
9215 elfcore_write_note (bfd
*abfd
,
9223 Elf_External_Note
*xnp
;
9230 namesz
= strlen (name
) + 1;
9232 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9234 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9237 dest
= buf
+ *bufsiz
;
9238 *bufsiz
+= newspace
;
9239 xnp
= (Elf_External_Note
*) dest
;
9240 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9241 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9242 H_PUT_32 (abfd
, type
, xnp
->type
);
9246 memcpy (dest
, name
, namesz
);
9254 memcpy (dest
, input
, size
);
9265 elfcore_write_prpsinfo (bfd
*abfd
,
9271 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9273 if (bed
->elf_backend_write_core_note
!= NULL
)
9276 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9277 NT_PRPSINFO
, fname
, psargs
);
9282 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9283 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9284 if (bed
->s
->elfclass
== ELFCLASS32
)
9286 #if defined (HAVE_PSINFO32_T)
9288 int note_type
= NT_PSINFO
;
9291 int note_type
= NT_PRPSINFO
;
9294 memset (&data
, 0, sizeof (data
));
9295 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9296 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9297 return elfcore_write_note (abfd
, buf
, bufsiz
,
9298 "CORE", note_type
, &data
, sizeof (data
));
9303 #if defined (HAVE_PSINFO_T)
9305 int note_type
= NT_PSINFO
;
9308 int note_type
= NT_PRPSINFO
;
9311 memset (&data
, 0, sizeof (data
));
9312 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9313 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9314 return elfcore_write_note (abfd
, buf
, bufsiz
,
9315 "CORE", note_type
, &data
, sizeof (data
));
9317 #endif /* PSINFO_T or PRPSINFO_T */
9324 elfcore_write_linux_prpsinfo32
9325 (bfd
*abfd
, char *buf
, int *bufsiz
,
9326 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9328 struct elf_external_linux_prpsinfo32 data
;
9330 memset (&data
, 0, sizeof (data
));
9331 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9333 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9334 &data
, sizeof (data
));
9338 elfcore_write_linux_prpsinfo64
9339 (bfd
*abfd
, char *buf
, int *bufsiz
,
9340 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9342 struct elf_external_linux_prpsinfo64 data
;
9344 memset (&data
, 0, sizeof (data
));
9345 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9347 return elfcore_write_note (abfd
, buf
, bufsiz
,
9348 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9352 elfcore_write_prstatus (bfd
*abfd
,
9359 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9361 if (bed
->elf_backend_write_core_note
!= NULL
)
9364 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9366 pid
, cursig
, gregs
);
9371 #if defined (HAVE_PRSTATUS_T)
9372 #if defined (HAVE_PRSTATUS32_T)
9373 if (bed
->s
->elfclass
== ELFCLASS32
)
9375 prstatus32_t prstat
;
9377 memset (&prstat
, 0, sizeof (prstat
));
9378 prstat
.pr_pid
= pid
;
9379 prstat
.pr_cursig
= cursig
;
9380 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9381 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9382 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9389 memset (&prstat
, 0, sizeof (prstat
));
9390 prstat
.pr_pid
= pid
;
9391 prstat
.pr_cursig
= cursig
;
9392 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9393 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9394 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9396 #endif /* HAVE_PRSTATUS_T */
9402 #if defined (HAVE_LWPSTATUS_T)
9404 elfcore_write_lwpstatus (bfd
*abfd
,
9411 lwpstatus_t lwpstat
;
9412 const char *note_name
= "CORE";
9414 memset (&lwpstat
, 0, sizeof (lwpstat
));
9415 lwpstat
.pr_lwpid
= pid
>> 16;
9416 lwpstat
.pr_cursig
= cursig
;
9417 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9418 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9419 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9421 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9422 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9424 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9425 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9428 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9429 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9431 #endif /* HAVE_LWPSTATUS_T */
9433 #if defined (HAVE_PSTATUS_T)
9435 elfcore_write_pstatus (bfd
*abfd
,
9439 int cursig ATTRIBUTE_UNUSED
,
9440 const void *gregs ATTRIBUTE_UNUSED
)
9442 const char *note_name
= "CORE";
9443 #if defined (HAVE_PSTATUS32_T)
9444 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9446 if (bed
->s
->elfclass
== ELFCLASS32
)
9450 memset (&pstat
, 0, sizeof (pstat
));
9451 pstat
.pr_pid
= pid
& 0xffff;
9452 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9453 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9461 memset (&pstat
, 0, sizeof (pstat
));
9462 pstat
.pr_pid
= pid
& 0xffff;
9463 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9464 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9468 #endif /* HAVE_PSTATUS_T */
9471 elfcore_write_prfpreg (bfd
*abfd
,
9477 const char *note_name
= "CORE";
9478 return elfcore_write_note (abfd
, buf
, bufsiz
,
9479 note_name
, NT_FPREGSET
, fpregs
, size
);
9483 elfcore_write_prxfpreg (bfd
*abfd
,
9486 const void *xfpregs
,
9489 char *note_name
= "LINUX";
9490 return elfcore_write_note (abfd
, buf
, bufsiz
,
9491 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9495 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9496 const void *xfpregs
, int size
)
9498 char *note_name
= "LINUX";
9499 return elfcore_write_note (abfd
, buf
, bufsiz
,
9500 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9504 elfcore_write_ppc_vmx (bfd
*abfd
,
9507 const void *ppc_vmx
,
9510 char *note_name
= "LINUX";
9511 return elfcore_write_note (abfd
, buf
, bufsiz
,
9512 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9516 elfcore_write_ppc_vsx (bfd
*abfd
,
9519 const void *ppc_vsx
,
9522 char *note_name
= "LINUX";
9523 return elfcore_write_note (abfd
, buf
, bufsiz
,
9524 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9528 elfcore_write_s390_high_gprs (bfd
*abfd
,
9531 const void *s390_high_gprs
,
9534 char *note_name
= "LINUX";
9535 return elfcore_write_note (abfd
, buf
, bufsiz
,
9536 note_name
, NT_S390_HIGH_GPRS
,
9537 s390_high_gprs
, size
);
9541 elfcore_write_s390_timer (bfd
*abfd
,
9544 const void *s390_timer
,
9547 char *note_name
= "LINUX";
9548 return elfcore_write_note (abfd
, buf
, bufsiz
,
9549 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9553 elfcore_write_s390_todcmp (bfd
*abfd
,
9556 const void *s390_todcmp
,
9559 char *note_name
= "LINUX";
9560 return elfcore_write_note (abfd
, buf
, bufsiz
,
9561 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9565 elfcore_write_s390_todpreg (bfd
*abfd
,
9568 const void *s390_todpreg
,
9571 char *note_name
= "LINUX";
9572 return elfcore_write_note (abfd
, buf
, bufsiz
,
9573 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9577 elfcore_write_s390_ctrs (bfd
*abfd
,
9580 const void *s390_ctrs
,
9583 char *note_name
= "LINUX";
9584 return elfcore_write_note (abfd
, buf
, bufsiz
,
9585 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9589 elfcore_write_s390_prefix (bfd
*abfd
,
9592 const void *s390_prefix
,
9595 char *note_name
= "LINUX";
9596 return elfcore_write_note (abfd
, buf
, bufsiz
,
9597 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9601 elfcore_write_s390_last_break (bfd
*abfd
,
9604 const void *s390_last_break
,
9607 char *note_name
= "LINUX";
9608 return elfcore_write_note (abfd
, buf
, bufsiz
,
9609 note_name
, NT_S390_LAST_BREAK
,
9610 s390_last_break
, size
);
9614 elfcore_write_s390_system_call (bfd
*abfd
,
9617 const void *s390_system_call
,
9620 char *note_name
= "LINUX";
9621 return elfcore_write_note (abfd
, buf
, bufsiz
,
9622 note_name
, NT_S390_SYSTEM_CALL
,
9623 s390_system_call
, size
);
9627 elfcore_write_s390_tdb (bfd
*abfd
,
9630 const void *s390_tdb
,
9633 char *note_name
= "LINUX";
9634 return elfcore_write_note (abfd
, buf
, bufsiz
,
9635 note_name
, NT_S390_TDB
, s390_tdb
, size
);
9639 elfcore_write_s390_vxrs_low (bfd
*abfd
,
9642 const void *s390_vxrs_low
,
9645 char *note_name
= "LINUX";
9646 return elfcore_write_note (abfd
, buf
, bufsiz
,
9647 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
9651 elfcore_write_s390_vxrs_high (bfd
*abfd
,
9654 const void *s390_vxrs_high
,
9657 char *note_name
= "LINUX";
9658 return elfcore_write_note (abfd
, buf
, bufsiz
,
9659 note_name
, NT_S390_VXRS_HIGH
,
9660 s390_vxrs_high
, size
);
9664 elfcore_write_arm_vfp (bfd
*abfd
,
9667 const void *arm_vfp
,
9670 char *note_name
= "LINUX";
9671 return elfcore_write_note (abfd
, buf
, bufsiz
,
9672 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
9676 elfcore_write_aarch_tls (bfd
*abfd
,
9679 const void *aarch_tls
,
9682 char *note_name
= "LINUX";
9683 return elfcore_write_note (abfd
, buf
, bufsiz
,
9684 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
9688 elfcore_write_aarch_hw_break (bfd
*abfd
,
9691 const void *aarch_hw_break
,
9694 char *note_name
= "LINUX";
9695 return elfcore_write_note (abfd
, buf
, bufsiz
,
9696 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
9700 elfcore_write_aarch_hw_watch (bfd
*abfd
,
9703 const void *aarch_hw_watch
,
9706 char *note_name
= "LINUX";
9707 return elfcore_write_note (abfd
, buf
, bufsiz
,
9708 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
9712 elfcore_write_register_note (bfd
*abfd
,
9715 const char *section
,
9719 if (strcmp (section
, ".reg2") == 0)
9720 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9721 if (strcmp (section
, ".reg-xfp") == 0)
9722 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9723 if (strcmp (section
, ".reg-xstate") == 0)
9724 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9725 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9726 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9727 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9728 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9729 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9730 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9731 if (strcmp (section
, ".reg-s390-timer") == 0)
9732 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9733 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9734 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9735 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9736 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9737 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9738 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9739 if (strcmp (section
, ".reg-s390-prefix") == 0)
9740 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9741 if (strcmp (section
, ".reg-s390-last-break") == 0)
9742 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
9743 if (strcmp (section
, ".reg-s390-system-call") == 0)
9744 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
9745 if (strcmp (section
, ".reg-s390-tdb") == 0)
9746 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
9747 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
9748 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
9749 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
9750 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
9751 if (strcmp (section
, ".reg-arm-vfp") == 0)
9752 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
9753 if (strcmp (section
, ".reg-aarch-tls") == 0)
9754 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
9755 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
9756 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
9757 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
9758 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
9763 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9768 while (p
< buf
+ size
)
9770 /* FIXME: bad alignment assumption. */
9771 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9772 Elf_Internal_Note in
;
9774 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9777 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9779 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9780 in
.namedata
= xnp
->name
;
9781 if (in
.namesz
> buf
- in
.namedata
+ size
)
9784 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9785 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9786 in
.descpos
= offset
+ (in
.descdata
- buf
);
9788 && (in
.descdata
>= buf
+ size
9789 || in
.descsz
> buf
- in
.descdata
+ size
))
9792 switch (bfd_get_format (abfd
))
9799 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
9802 const char * string
;
9804 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
9808 GROKER_ELEMENT ("", elfcore_grok_note
),
9809 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
9810 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
9811 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
9812 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
9814 #undef GROKER_ELEMENT
9817 for (i
= ARRAY_SIZE (grokers
); i
--;)
9819 if (in
.namesz
>= grokers
[i
].len
9820 && strncmp (in
.namedata
, grokers
[i
].string
,
9821 grokers
[i
].len
) == 0)
9823 if (! grokers
[i
].func (abfd
, & in
))
9832 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9834 if (! elfobj_grok_gnu_note (abfd
, &in
))
9837 else if (in
.namesz
== sizeof "stapsdt"
9838 && strcmp (in
.namedata
, "stapsdt") == 0)
9840 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
9846 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9853 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9860 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9863 buf
= (char *) bfd_malloc (size
+ 1);
9867 /* PR 17512: file: ec08f814
9868 0-termintate the buffer so that string searches will not overflow. */
9871 if (bfd_bread (buf
, size
, abfd
) != size
9872 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9882 /* Providing external access to the ELF program header table. */
9884 /* Return an upper bound on the number of bytes required to store a
9885 copy of ABFD's program header table entries. Return -1 if an error
9886 occurs; bfd_get_error will return an appropriate code. */
9889 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9891 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9893 bfd_set_error (bfd_error_wrong_format
);
9897 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9900 /* Copy ABFD's program header table entries to *PHDRS. The entries
9901 will be stored as an array of Elf_Internal_Phdr structures, as
9902 defined in include/elf/internal.h. To find out how large the
9903 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9905 Return the number of program header table entries read, or -1 if an
9906 error occurs; bfd_get_error will return an appropriate code. */
9909 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9913 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9915 bfd_set_error (bfd_error_wrong_format
);
9919 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9920 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9921 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9926 enum elf_reloc_type_class
9927 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
9928 const asection
*rel_sec ATTRIBUTE_UNUSED
,
9929 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9931 return reloc_class_normal
;
9934 /* For RELA architectures, return the relocation value for a
9935 relocation against a local symbol. */
9938 _bfd_elf_rela_local_sym (bfd
*abfd
,
9939 Elf_Internal_Sym
*sym
,
9941 Elf_Internal_Rela
*rel
)
9943 asection
*sec
= *psec
;
9946 relocation
= (sec
->output_section
->vma
9947 + sec
->output_offset
9949 if ((sec
->flags
& SEC_MERGE
)
9950 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9951 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
9954 _bfd_merged_section_offset (abfd
, psec
,
9955 elf_section_data (sec
)->sec_info
,
9956 sym
->st_value
+ rel
->r_addend
);
9959 /* If we have changed the section, and our original section is
9960 marked with SEC_EXCLUDE, it means that the original
9961 SEC_MERGE section has been completely subsumed in some
9962 other SEC_MERGE section. In this case, we need to leave
9963 some info around for --emit-relocs. */
9964 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9965 sec
->kept_section
= *psec
;
9968 rel
->r_addend
-= relocation
;
9969 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9975 _bfd_elf_rel_local_sym (bfd
*abfd
,
9976 Elf_Internal_Sym
*sym
,
9980 asection
*sec
= *psec
;
9982 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
9983 return sym
->st_value
+ addend
;
9985 return _bfd_merged_section_offset (abfd
, psec
,
9986 elf_section_data (sec
)->sec_info
,
9987 sym
->st_value
+ addend
);
9991 _bfd_elf_section_offset (bfd
*abfd
,
9992 struct bfd_link_info
*info
,
9996 switch (sec
->sec_info_type
)
9998 case SEC_INFO_TYPE_STABS
:
9999 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10001 case SEC_INFO_TYPE_EH_FRAME
:
10002 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10004 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10006 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10007 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10008 offset
= sec
->size
- offset
- address_size
;
10014 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10015 reconstruct an ELF file by reading the segments out of remote memory
10016 based on the ELF file header at EHDR_VMA and the ELF program headers it
10017 points to. If not null, *LOADBASEP is filled in with the difference
10018 between the VMAs from which the segments were read, and the VMAs the
10019 file headers (and hence BFD's idea of each section's VMA) put them at.
10021 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10022 remote memory at target address VMA into the local buffer at MYADDR; it
10023 should return zero on success or an `errno' code on failure. TEMPL must
10024 be a BFD for an ELF target with the word size and byte order found in
10025 the remote memory. */
10028 bfd_elf_bfd_from_remote_memory
10031 bfd_size_type size
,
10032 bfd_vma
*loadbasep
,
10033 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
10035 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
10036 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
10040 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10041 long symcount ATTRIBUTE_UNUSED
,
10042 asymbol
**syms ATTRIBUTE_UNUSED
,
10047 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10050 const char *relplt_name
;
10051 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10055 Elf_Internal_Shdr
*hdr
;
10061 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10064 if (dynsymcount
<= 0)
10067 if (!bed
->plt_sym_val
)
10070 relplt_name
= bed
->relplt_name
;
10071 if (relplt_name
== NULL
)
10072 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10073 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10074 if (relplt
== NULL
)
10077 hdr
= &elf_section_data (relplt
)->this_hdr
;
10078 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10079 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10082 plt
= bfd_get_section_by_name (abfd
, ".plt");
10086 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10087 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10090 count
= relplt
->size
/ hdr
->sh_entsize
;
10091 size
= count
* sizeof (asymbol
);
10092 p
= relplt
->relocation
;
10093 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10095 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10096 if (p
->addend
!= 0)
10099 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10101 size
+= sizeof ("+0x") - 1 + 8;
10106 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10110 names
= (char *) (s
+ count
);
10111 p
= relplt
->relocation
;
10113 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10118 addr
= bed
->plt_sym_val (i
, plt
, p
);
10119 if (addr
== (bfd_vma
) -1)
10122 *s
= **p
->sym_ptr_ptr
;
10123 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10124 we are defining a symbol, ensure one of them is set. */
10125 if ((s
->flags
& BSF_LOCAL
) == 0)
10126 s
->flags
|= BSF_GLOBAL
;
10127 s
->flags
|= BSF_SYNTHETIC
;
10129 s
->value
= addr
- plt
->vma
;
10132 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
10133 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10135 if (p
->addend
!= 0)
10139 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10140 names
+= sizeof ("+0x") - 1;
10141 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10142 for (a
= buf
; *a
== '0'; ++a
)
10145 memcpy (names
, a
, len
);
10148 memcpy (names
, "@plt", sizeof ("@plt"));
10149 names
+= sizeof ("@plt");
10156 /* It is only used by x86-64 so far. */
10157 asection _bfd_elf_large_com_section
10158 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10159 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10162 _bfd_elf_post_process_headers (bfd
* abfd
,
10163 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10165 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10167 i_ehdrp
= elf_elfheader (abfd
);
10169 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10171 /* To make things simpler for the loader on Linux systems we set the
10172 osabi field to ELFOSABI_GNU if the binary contains symbols of
10173 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10174 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10175 && elf_tdata (abfd
)->has_gnu_symbols
)
10176 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10180 /* Return TRUE for ELF symbol types that represent functions.
10181 This is the default version of this function, which is sufficient for
10182 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10185 _bfd_elf_is_function_type (unsigned int type
)
10187 return (type
== STT_FUNC
10188 || type
== STT_GNU_IFUNC
);
10191 /* If the ELF symbol SYM might be a function in SEC, return the
10192 function size and set *CODE_OFF to the function's entry point,
10193 otherwise return zero. */
10196 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10199 bfd_size_type size
;
10201 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10202 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10203 || sym
->section
!= sec
)
10206 *code_off
= sym
->value
;
10208 if (!(sym
->flags
& BSF_SYNTHETIC
))
10209 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;