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 elf_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
;
859 convert_debug_to_zdebug (bfd
*abfd
, const char *name
)
861 unsigned int len
= strlen (name
);
862 char *new_name
= bfd_alloc (abfd
, len
+ 2);
863 if (new_name
== NULL
)
867 memcpy (new_name
+ 2, name
+ 1, len
);
872 convert_zdebug_to_debug (bfd
*abfd
, const char *name
)
874 unsigned int len
= strlen (name
);
875 char *new_name
= bfd_alloc (abfd
, len
);
876 if (new_name
== NULL
)
879 memcpy (new_name
+ 1, name
+ 2, len
- 1);
883 /* Make a BFD section from an ELF section. We store a pointer to the
884 BFD section in the bfd_section field of the header. */
887 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
888 Elf_Internal_Shdr
*hdr
,
894 const struct elf_backend_data
*bed
;
896 if (hdr
->bfd_section
!= NULL
)
899 newsect
= bfd_make_section_anyway (abfd
, name
);
903 hdr
->bfd_section
= newsect
;
904 elf_section_data (newsect
)->this_hdr
= *hdr
;
905 elf_section_data (newsect
)->this_idx
= shindex
;
907 /* Always use the real type/flags. */
908 elf_section_type (newsect
) = hdr
->sh_type
;
909 elf_section_flags (newsect
) = hdr
->sh_flags
;
911 newsect
->filepos
= hdr
->sh_offset
;
913 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
914 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
915 || ! bfd_set_section_alignment (abfd
, newsect
,
916 bfd_log2 (hdr
->sh_addralign
)))
919 flags
= SEC_NO_FLAGS
;
920 if (hdr
->sh_type
!= SHT_NOBITS
)
921 flags
|= SEC_HAS_CONTENTS
;
922 if (hdr
->sh_type
== SHT_GROUP
)
923 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
924 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
927 if (hdr
->sh_type
!= SHT_NOBITS
)
930 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
931 flags
|= SEC_READONLY
;
932 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
934 else if ((flags
& SEC_LOAD
) != 0)
936 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
939 newsect
->entsize
= hdr
->sh_entsize
;
940 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
941 flags
|= SEC_STRINGS
;
943 if (hdr
->sh_flags
& SHF_GROUP
)
944 if (!setup_group (abfd
, hdr
, newsect
))
946 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
947 flags
|= SEC_THREAD_LOCAL
;
948 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
949 flags
|= SEC_EXCLUDE
;
951 if ((flags
& SEC_ALLOC
) == 0)
953 /* The debugging sections appear to be recognized only by name,
954 not any sort of flag. Their SEC_ALLOC bits are cleared. */
961 else if (name
[1] == 'g' && name
[2] == 'n')
962 p
= ".gnu.linkonce.wi.", n
= 17;
963 else if (name
[1] == 'g' && name
[2] == 'd')
964 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
965 else if (name
[1] == 'l')
967 else if (name
[1] == 's')
969 else if (name
[1] == 'z')
970 p
= ".zdebug", n
= 7;
973 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
974 flags
|= SEC_DEBUGGING
;
978 /* As a GNU extension, if the name begins with .gnu.linkonce, we
979 only link a single copy of the section. This is used to support
980 g++. g++ will emit each template expansion in its own section.
981 The symbols will be defined as weak, so that multiple definitions
982 are permitted. The GNU linker extension is to actually discard
983 all but one of the sections. */
984 if (CONST_STRNEQ (name
, ".gnu.linkonce")
985 && elf_next_in_group (newsect
) == NULL
)
986 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
988 bed
= get_elf_backend_data (abfd
);
989 if (bed
->elf_backend_section_flags
)
990 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
993 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
996 /* We do not parse the PT_NOTE segments as we are interested even in the
997 separate debug info files which may have the segments offsets corrupted.
998 PT_NOTEs from the core files are currently not parsed using BFD. */
999 if (hdr
->sh_type
== SHT_NOTE
)
1003 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
1006 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
1010 if ((flags
& SEC_ALLOC
) != 0)
1012 Elf_Internal_Phdr
*phdr
;
1013 unsigned int i
, nload
;
1015 /* Some ELF linkers produce binaries with all the program header
1016 p_paddr fields zero. If we have such a binary with more than
1017 one PT_LOAD header, then leave the section lma equal to vma
1018 so that we don't create sections with overlapping lma. */
1019 phdr
= elf_tdata (abfd
)->phdr
;
1020 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1021 if (phdr
->p_paddr
!= 0)
1023 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
1025 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
1028 phdr
= elf_tdata (abfd
)->phdr
;
1029 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
1031 if (((phdr
->p_type
== PT_LOAD
1032 && (hdr
->sh_flags
& SHF_TLS
) == 0)
1033 || phdr
->p_type
== PT_TLS
)
1034 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
1036 if ((flags
& SEC_LOAD
) == 0)
1037 newsect
->lma
= (phdr
->p_paddr
1038 + hdr
->sh_addr
- phdr
->p_vaddr
);
1040 /* We used to use the same adjustment for SEC_LOAD
1041 sections, but that doesn't work if the segment
1042 is packed with code from multiple VMAs.
1043 Instead we calculate the section LMA based on
1044 the segment LMA. It is assumed that the
1045 segment will contain sections with contiguous
1046 LMAs, even if the VMAs are not. */
1047 newsect
->lma
= (phdr
->p_paddr
1048 + hdr
->sh_offset
- phdr
->p_offset
);
1050 /* With contiguous segments, we can't tell from file
1051 offsets whether a section with zero size should
1052 be placed at the end of one segment or the
1053 beginning of the next. Decide based on vaddr. */
1054 if (hdr
->sh_addr
>= phdr
->p_vaddr
1055 && (hdr
->sh_addr
+ hdr
->sh_size
1056 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1062 /* Compress/decompress DWARF debug sections with names: .debug_* and
1063 .zdebug_*, after the section flags is set. */
1064 if ((flags
& SEC_DEBUGGING
)
1065 && ((name
[1] == 'd' && name
[6] == '_')
1066 || (name
[1] == 'z' && name
[7] == '_')))
1068 enum { nothing
, compress
, decompress
} action
= nothing
;
1069 int compression_header_size
;
1070 bfd_size_type uncompressed_size
;
1071 bfd_boolean compressed
1072 = bfd_is_section_compressed_with_header (abfd
, newsect
,
1073 &compression_header_size
,
1074 &uncompressed_size
);
1078 /* Compressed section. Check if we should decompress. */
1079 if ((abfd
->flags
& BFD_DECOMPRESS
))
1080 action
= decompress
;
1083 /* Compress the uncompressed section or convert from/to .zdebug*
1084 section. Check if we should compress. */
1085 if (action
== nothing
)
1087 if (newsect
->size
!= 0
1088 && (abfd
->flags
& BFD_COMPRESS
)
1089 && compression_header_size
>= 0
1090 && uncompressed_size
> 0
1092 || ((compression_header_size
> 0)
1093 != ((abfd
->flags
& BFD_COMPRESS_GABI
) != 0))))
1099 if (action
== compress
)
1101 if (!bfd_init_section_compress_status (abfd
, newsect
))
1103 (*_bfd_error_handler
)
1104 (_("%B: unable to initialize compress status for section %s"),
1111 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1113 (*_bfd_error_handler
)
1114 (_("%B: unable to initialize decompress status for section %s"),
1120 if (abfd
->is_linker_input
)
1123 && (action
== decompress
1124 || (action
== compress
1125 && (abfd
->flags
& BFD_COMPRESS_GABI
) != 0)))
1127 /* Convert section name from .zdebug_* to .debug_* so
1128 that linker will consider this section as a debug
1130 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
1131 if (new_name
== NULL
)
1133 bfd_rename_section (abfd
, newsect
, new_name
);
1137 /* For objdump, don't rename the section. For objcopy, delay
1138 section rename to elf_fake_sections. */
1139 newsect
->flags
|= SEC_ELF_RENAME
;
1145 const char *const bfd_elf_section_type_names
[] = {
1146 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1147 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1148 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1151 /* ELF relocs are against symbols. If we are producing relocatable
1152 output, and the reloc is against an external symbol, and nothing
1153 has given us any additional addend, the resulting reloc will also
1154 be against the same symbol. In such a case, we don't want to
1155 change anything about the way the reloc is handled, since it will
1156 all be done at final link time. Rather than put special case code
1157 into bfd_perform_relocation, all the reloc types use this howto
1158 function. It just short circuits the reloc if producing
1159 relocatable output against an external symbol. */
1161 bfd_reloc_status_type
1162 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1163 arelent
*reloc_entry
,
1165 void *data ATTRIBUTE_UNUSED
,
1166 asection
*input_section
,
1168 char **error_message ATTRIBUTE_UNUSED
)
1170 if (output_bfd
!= NULL
1171 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1172 && (! reloc_entry
->howto
->partial_inplace
1173 || reloc_entry
->addend
== 0))
1175 reloc_entry
->address
+= input_section
->output_offset
;
1176 return bfd_reloc_ok
;
1179 return bfd_reloc_continue
;
1182 /* Copy the program header and other data from one object module to
1186 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1188 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1189 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1192 if (!elf_flags_init (obfd
))
1194 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1195 elf_flags_init (obfd
) = TRUE
;
1198 elf_gp (obfd
) = elf_gp (ibfd
);
1200 /* Also copy the EI_OSABI field. */
1201 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1202 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1204 /* Copy object attributes. */
1205 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1207 /* This is an feature for objcopy --only-keep-debug: When a section's type
1208 is changed to NOBITS, we preserve the sh_link and sh_info fields so that
1209 they can be matched up with the original. */
1210 Elf_Internal_Shdr
** iheaders
= elf_elfsections (ibfd
);
1211 Elf_Internal_Shdr
** oheaders
= elf_elfsections (obfd
);
1213 if (iheaders
!= NULL
&& oheaders
!= NULL
)
1217 for (i
= 0; i
< elf_numsections (obfd
); i
++)
1220 Elf_Internal_Shdr
* oheader
= oheaders
[i
];
1223 || oheader
->sh_type
!= SHT_NOBITS
1224 || oheader
->sh_size
== 0
1225 || (oheader
->sh_info
!= 0 && oheader
->sh_link
!= 0))
1228 /* Scan for the matching section in the input bfd.
1229 FIXME: We could use something better than a linear scan here.
1230 Unfortunately we cannot compare names as the output string table
1231 is empty, so instead we check size, address and type. */
1232 for (j
= 0; j
< elf_numsections (ibfd
); j
++)
1234 Elf_Internal_Shdr
* iheader
= iheaders
[j
];
1236 if (iheader
->sh_type
!= SHT_NOBITS
1237 && iheader
->sh_size
== oheader
->sh_size
1238 && iheader
->sh_addr
== oheader
->sh_addr
1239 && (iheader
->sh_info
!= oheader
->sh_info
1240 || iheader
->sh_link
!= oheader
->sh_link
))
1242 /* Note: Strictly speaking these assignments are wrong.
1243 The sh_link and sh_info fields should point to the
1244 relevent sections in the output BFD, which may not be in
1245 the same location as they were in the input BFD. But the
1246 whole point of this action is to preserve the original
1247 values of the sh_link and sh_info fields, so that they
1248 can be matched up with the section headers in the
1249 original file. So strictly speaking we may be creating
1250 an invalid ELF file, but it is only for a file that just
1251 contains debug info and only for sections without any
1253 if (oheader
->sh_link
== 0)
1254 oheader
->sh_link
= iheader
->sh_link
;
1255 if (oheader
->sh_info
== 0)
1256 oheader
->sh_info
= iheader
->sh_info
;
1267 get_segment_type (unsigned int p_type
)
1272 case PT_NULL
: pt
= "NULL"; break;
1273 case PT_LOAD
: pt
= "LOAD"; break;
1274 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1275 case PT_INTERP
: pt
= "INTERP"; break;
1276 case PT_NOTE
: pt
= "NOTE"; break;
1277 case PT_SHLIB
: pt
= "SHLIB"; break;
1278 case PT_PHDR
: pt
= "PHDR"; break;
1279 case PT_TLS
: pt
= "TLS"; break;
1280 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1281 case PT_GNU_STACK
: pt
= "STACK"; break;
1282 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1283 default: pt
= NULL
; break;
1288 /* Print out the program headers. */
1291 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1293 FILE *f
= (FILE *) farg
;
1294 Elf_Internal_Phdr
*p
;
1296 bfd_byte
*dynbuf
= NULL
;
1298 p
= elf_tdata (abfd
)->phdr
;
1303 fprintf (f
, _("\nProgram Header:\n"));
1304 c
= elf_elfheader (abfd
)->e_phnum
;
1305 for (i
= 0; i
< c
; i
++, p
++)
1307 const char *pt
= get_segment_type (p
->p_type
);
1312 sprintf (buf
, "0x%lx", p
->p_type
);
1315 fprintf (f
, "%8s off 0x", pt
);
1316 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1317 fprintf (f
, " vaddr 0x");
1318 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1319 fprintf (f
, " paddr 0x");
1320 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1321 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1322 fprintf (f
, " filesz 0x");
1323 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1324 fprintf (f
, " memsz 0x");
1325 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1326 fprintf (f
, " flags %c%c%c",
1327 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1328 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1329 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1330 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1331 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1336 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1339 unsigned int elfsec
;
1340 unsigned long shlink
;
1341 bfd_byte
*extdyn
, *extdynend
;
1343 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1345 fprintf (f
, _("\nDynamic Section:\n"));
1347 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1350 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1351 if (elfsec
== SHN_BAD
)
1353 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1355 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1356 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1359 /* PR 17512: file: 6f427532. */
1360 if (s
->size
< extdynsize
)
1362 extdynend
= extdyn
+ s
->size
;
1363 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
1365 for (; extdyn
<= (extdynend
- extdynsize
); extdyn
+= extdynsize
)
1367 Elf_Internal_Dyn dyn
;
1368 const char *name
= "";
1370 bfd_boolean stringp
;
1371 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1373 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1375 if (dyn
.d_tag
== DT_NULL
)
1382 if (bed
->elf_backend_get_target_dtag
)
1383 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1385 if (!strcmp (name
, ""))
1387 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1392 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1393 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1394 case DT_PLTGOT
: name
= "PLTGOT"; break;
1395 case DT_HASH
: name
= "HASH"; break;
1396 case DT_STRTAB
: name
= "STRTAB"; break;
1397 case DT_SYMTAB
: name
= "SYMTAB"; break;
1398 case DT_RELA
: name
= "RELA"; break;
1399 case DT_RELASZ
: name
= "RELASZ"; break;
1400 case DT_RELAENT
: name
= "RELAENT"; break;
1401 case DT_STRSZ
: name
= "STRSZ"; break;
1402 case DT_SYMENT
: name
= "SYMENT"; break;
1403 case DT_INIT
: name
= "INIT"; break;
1404 case DT_FINI
: name
= "FINI"; break;
1405 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1406 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1407 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1408 case DT_REL
: name
= "REL"; break;
1409 case DT_RELSZ
: name
= "RELSZ"; break;
1410 case DT_RELENT
: name
= "RELENT"; break;
1411 case DT_PLTREL
: name
= "PLTREL"; break;
1412 case DT_DEBUG
: name
= "DEBUG"; break;
1413 case DT_TEXTREL
: name
= "TEXTREL"; break;
1414 case DT_JMPREL
: name
= "JMPREL"; break;
1415 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1416 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1417 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1418 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1419 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1420 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1421 case DT_FLAGS
: name
= "FLAGS"; break;
1422 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1423 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1424 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1425 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1426 case DT_MOVEENT
: name
= "MOVEENT"; break;
1427 case DT_MOVESZ
: name
= "MOVESZ"; break;
1428 case DT_FEATURE
: name
= "FEATURE"; break;
1429 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1430 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1431 case DT_SYMINENT
: name
= "SYMINENT"; break;
1432 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1433 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1434 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1435 case DT_PLTPAD
: name
= "PLTPAD"; break;
1436 case DT_MOVETAB
: name
= "MOVETAB"; break;
1437 case DT_SYMINFO
: name
= "SYMINFO"; break;
1438 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1439 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1440 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1441 case DT_VERSYM
: name
= "VERSYM"; break;
1442 case DT_VERDEF
: name
= "VERDEF"; break;
1443 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1444 case DT_VERNEED
: name
= "VERNEED"; break;
1445 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1446 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1447 case DT_USED
: name
= "USED"; break;
1448 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1449 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1452 fprintf (f
, " %-20s ", name
);
1456 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1461 unsigned int tagv
= dyn
.d_un
.d_val
;
1463 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1466 fprintf (f
, "%s", string
);
1475 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1476 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1478 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1482 if (elf_dynverdef (abfd
) != 0)
1484 Elf_Internal_Verdef
*t
;
1486 fprintf (f
, _("\nVersion definitions:\n"));
1487 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1489 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1490 t
->vd_flags
, t
->vd_hash
,
1491 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1492 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1494 Elf_Internal_Verdaux
*a
;
1497 for (a
= t
->vd_auxptr
->vda_nextptr
;
1501 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1507 if (elf_dynverref (abfd
) != 0)
1509 Elf_Internal_Verneed
*t
;
1511 fprintf (f
, _("\nVersion References:\n"));
1512 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1514 Elf_Internal_Vernaux
*a
;
1516 fprintf (f
, _(" required from %s:\n"),
1517 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1518 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1519 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1520 a
->vna_flags
, a
->vna_other
,
1521 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1533 /* Get version string. */
1536 _bfd_elf_get_symbol_version_string (bfd
*abfd
, asymbol
*symbol
,
1537 bfd_boolean
*hidden
)
1539 const char *version_string
= NULL
;
1540 if (elf_dynversym (abfd
) != 0
1541 && (elf_dynverdef (abfd
) != 0 || elf_dynverref (abfd
) != 0))
1543 unsigned int vernum
= ((elf_symbol_type
*) symbol
)->version
;
1545 *hidden
= (vernum
& VERSYM_HIDDEN
) != 0;
1546 vernum
&= VERSYM_VERSION
;
1549 version_string
= "";
1550 else if (vernum
== 1)
1551 version_string
= "Base";
1552 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1554 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1557 Elf_Internal_Verneed
*t
;
1559 version_string
= "";
1560 for (t
= elf_tdata (abfd
)->verref
;
1564 Elf_Internal_Vernaux
*a
;
1566 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1568 if (a
->vna_other
== vernum
)
1570 version_string
= a
->vna_nodename
;
1577 return version_string
;
1580 /* Display ELF-specific fields of a symbol. */
1583 bfd_elf_print_symbol (bfd
*abfd
,
1586 bfd_print_symbol_type how
)
1588 FILE *file
= (FILE *) filep
;
1591 case bfd_print_symbol_name
:
1592 fprintf (file
, "%s", symbol
->name
);
1594 case bfd_print_symbol_more
:
1595 fprintf (file
, "elf ");
1596 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1597 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1599 case bfd_print_symbol_all
:
1601 const char *section_name
;
1602 const char *name
= NULL
;
1603 const struct elf_backend_data
*bed
;
1604 unsigned char st_other
;
1606 const char *version_string
;
1609 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1611 bed
= get_elf_backend_data (abfd
);
1612 if (bed
->elf_backend_print_symbol_all
)
1613 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1617 name
= symbol
->name
;
1618 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1621 fprintf (file
, " %s\t", section_name
);
1622 /* Print the "other" value for a symbol. For common symbols,
1623 we've already printed the size; now print the alignment.
1624 For other symbols, we have no specified alignment, and
1625 we've printed the address; now print the size. */
1626 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1627 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1629 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1630 bfd_fprintf_vma (abfd
, file
, val
);
1632 /* If we have version information, print it. */
1633 version_string
= _bfd_elf_get_symbol_version_string (abfd
,
1639 fprintf (file
, " %-11s", version_string
);
1644 fprintf (file
, " (%s)", version_string
);
1645 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1650 /* If the st_other field is not zero, print it. */
1651 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1656 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1657 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1658 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1660 /* Some other non-defined flags are also present, so print
1662 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1665 fprintf (file
, " %s", name
);
1671 /* ELF .o/exec file reading */
1673 /* Create a new bfd section from an ELF section header. */
1676 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1678 Elf_Internal_Shdr
*hdr
;
1679 Elf_Internal_Ehdr
*ehdr
;
1680 const struct elf_backend_data
*bed
;
1682 bfd_boolean ret
= TRUE
;
1683 static bfd_boolean
* sections_being_created
= NULL
;
1684 static bfd
* sections_being_created_abfd
= NULL
;
1685 static unsigned int nesting
= 0;
1687 if (shindex
>= elf_numsections (abfd
))
1692 /* PR17512: A corrupt ELF binary might contain a recursive group of
1693 sections, with each the string indicies pointing to the next in the
1694 loop. Detect this here, by refusing to load a section that we are
1695 already in the process of loading. We only trigger this test if
1696 we have nested at least three sections deep as normal ELF binaries
1697 can expect to recurse at least once.
1699 FIXME: It would be better if this array was attached to the bfd,
1700 rather than being held in a static pointer. */
1702 if (sections_being_created_abfd
!= abfd
)
1703 sections_being_created
= NULL
;
1704 if (sections_being_created
== NULL
)
1706 /* FIXME: It would be more efficient to attach this array to the bfd somehow. */
1707 sections_being_created
= (bfd_boolean
*)
1708 bfd_zalloc (abfd
, elf_numsections (abfd
) * sizeof (bfd_boolean
));
1709 sections_being_created_abfd
= abfd
;
1711 if (sections_being_created
[shindex
])
1713 (*_bfd_error_handler
)
1714 (_("%B: warning: loop in section dependencies detected"), abfd
);
1717 sections_being_created
[shindex
] = TRUE
;
1720 hdr
= elf_elfsections (abfd
)[shindex
];
1721 ehdr
= elf_elfheader (abfd
);
1722 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1727 bed
= get_elf_backend_data (abfd
);
1728 switch (hdr
->sh_type
)
1731 /* Inactive section. Throw it away. */
1734 case SHT_PROGBITS
: /* Normal section with contents. */
1735 case SHT_NOBITS
: /* .bss section. */
1736 case SHT_HASH
: /* .hash section. */
1737 case SHT_NOTE
: /* .note section. */
1738 case SHT_INIT_ARRAY
: /* .init_array section. */
1739 case SHT_FINI_ARRAY
: /* .fini_array section. */
1740 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1741 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1742 case SHT_GNU_HASH
: /* .gnu.hash section. */
1743 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1746 case SHT_DYNAMIC
: /* Dynamic linking information. */
1747 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1750 if (hdr
->sh_link
> elf_numsections (abfd
))
1752 /* PR 10478: Accept Solaris binaries with a sh_link
1753 field set to SHN_BEFORE or SHN_AFTER. */
1754 switch (bfd_get_arch (abfd
))
1757 case bfd_arch_sparc
:
1758 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1759 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1761 /* Otherwise fall through. */
1766 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1768 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1770 Elf_Internal_Shdr
*dynsymhdr
;
1772 /* The shared libraries distributed with hpux11 have a bogus
1773 sh_link field for the ".dynamic" section. Find the
1774 string table for the ".dynsym" section instead. */
1775 if (elf_dynsymtab (abfd
) != 0)
1777 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1778 hdr
->sh_link
= dynsymhdr
->sh_link
;
1782 unsigned int i
, num_sec
;
1784 num_sec
= elf_numsections (abfd
);
1785 for (i
= 1; i
< num_sec
; i
++)
1787 dynsymhdr
= elf_elfsections (abfd
)[i
];
1788 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1790 hdr
->sh_link
= dynsymhdr
->sh_link
;
1798 case SHT_SYMTAB
: /* A symbol table. */
1799 if (elf_onesymtab (abfd
) == shindex
)
1802 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1805 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1807 if (hdr
->sh_size
!= 0)
1809 /* Some assemblers erroneously set sh_info to one with a
1810 zero sh_size. ld sees this as a global symbol count
1811 of (unsigned) -1. Fix it here. */
1816 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1817 elf_onesymtab (abfd
) = shindex
;
1818 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1819 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1820 abfd
->flags
|= HAS_SYMS
;
1822 /* Sometimes a shared object will map in the symbol table. If
1823 SHF_ALLOC is set, and this is a shared object, then we also
1824 treat this section as a BFD section. We can not base the
1825 decision purely on SHF_ALLOC, because that flag is sometimes
1826 set in a relocatable object file, which would confuse the
1828 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1829 && (abfd
->flags
& DYNAMIC
) != 0
1830 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1834 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1835 can't read symbols without that section loaded as well. It
1836 is most likely specified by the next section header. */
1837 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1839 unsigned int i
, num_sec
;
1841 num_sec
= elf_numsections (abfd
);
1842 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1844 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1845 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1846 && hdr2
->sh_link
== shindex
)
1850 for (i
= 1; i
< shindex
; i
++)
1852 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1853 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1854 && hdr2
->sh_link
== shindex
)
1858 ret
= bfd_section_from_shdr (abfd
, i
);
1862 case SHT_DYNSYM
: /* A dynamic symbol table. */
1863 if (elf_dynsymtab (abfd
) == shindex
)
1866 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1869 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1871 if (hdr
->sh_size
!= 0)
1874 /* Some linkers erroneously set sh_info to one with a
1875 zero sh_size. ld sees this as a global symbol count
1876 of (unsigned) -1. Fix it here. */
1881 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1882 elf_dynsymtab (abfd
) = shindex
;
1883 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1884 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1885 abfd
->flags
|= HAS_SYMS
;
1887 /* Besides being a symbol table, we also treat this as a regular
1888 section, so that objcopy can handle it. */
1889 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1892 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections. */
1893 if (elf_symtab_shndx (abfd
) == shindex
)
1896 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1897 elf_symtab_shndx (abfd
) = shindex
;
1898 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1899 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1902 case SHT_STRTAB
: /* A string table. */
1903 if (hdr
->bfd_section
!= NULL
)
1906 if (ehdr
->e_shstrndx
== shindex
)
1908 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1909 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1913 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1916 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1917 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1921 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1924 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1925 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1926 elf_elfsections (abfd
)[shindex
] = hdr
;
1927 /* We also treat this as a regular section, so that objcopy
1929 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1934 /* If the string table isn't one of the above, then treat it as a
1935 regular section. We need to scan all the headers to be sure,
1936 just in case this strtab section appeared before the above. */
1937 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1939 unsigned int i
, num_sec
;
1941 num_sec
= elf_numsections (abfd
);
1942 for (i
= 1; i
< num_sec
; i
++)
1944 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1945 if (hdr2
->sh_link
== shindex
)
1947 /* Prevent endless recursion on broken objects. */
1950 if (! bfd_section_from_shdr (abfd
, i
))
1952 if (elf_onesymtab (abfd
) == i
)
1954 if (elf_dynsymtab (abfd
) == i
)
1955 goto dynsymtab_strtab
;
1959 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1964 /* *These* do a lot of work -- but build no sections! */
1966 asection
*target_sect
;
1967 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1968 unsigned int num_sec
= elf_numsections (abfd
);
1969 struct bfd_elf_section_data
*esdt
;
1973 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1974 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1977 /* Check for a bogus link to avoid crashing. */
1978 if (hdr
->sh_link
>= num_sec
)
1980 ((*_bfd_error_handler
)
1981 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1982 abfd
, hdr
->sh_link
, name
, shindex
));
1983 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1988 /* For some incomprehensible reason Oracle distributes
1989 libraries for Solaris in which some of the objects have
1990 bogus sh_link fields. It would be nice if we could just
1991 reject them, but, unfortunately, some people need to use
1992 them. We scan through the section headers; if we find only
1993 one suitable symbol table, we clobber the sh_link to point
1994 to it. I hope this doesn't break anything.
1996 Don't do it on executable nor shared library. */
1997 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1998 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1999 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
2005 for (scan
= 1; scan
< num_sec
; scan
++)
2007 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
2008 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
2019 hdr
->sh_link
= found
;
2022 /* Get the symbol table. */
2023 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
2024 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
2025 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
2028 /* If this reloc section does not use the main symbol table we
2029 don't treat it as a reloc section. BFD can't adequately
2030 represent such a section, so at least for now, we don't
2031 try. We just present it as a normal section. We also
2032 can't use it as a reloc section if it points to the null
2033 section, an invalid section, another reloc section, or its
2034 sh_link points to the null section. */
2035 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
2036 || hdr
->sh_link
== SHN_UNDEF
2037 || hdr
->sh_info
== SHN_UNDEF
2038 || hdr
->sh_info
>= num_sec
2039 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
2040 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
2042 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2047 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
2050 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
2051 if (target_sect
== NULL
)
2054 esdt
= elf_section_data (target_sect
);
2055 if (hdr
->sh_type
== SHT_RELA
)
2056 p_hdr
= &esdt
->rela
.hdr
;
2058 p_hdr
= &esdt
->rel
.hdr
;
2060 /* PR 17512: file: 0b4f81b7. */
2063 amt
= sizeof (*hdr2
);
2064 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2069 elf_elfsections (abfd
)[shindex
] = hdr2
;
2070 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
2071 target_sect
->flags
|= SEC_RELOC
;
2072 target_sect
->relocation
= NULL
;
2073 target_sect
->rel_filepos
= hdr
->sh_offset
;
2074 /* In the section to which the relocations apply, mark whether
2075 its relocations are of the REL or RELA variety. */
2076 if (hdr
->sh_size
!= 0)
2078 if (hdr
->sh_type
== SHT_RELA
)
2079 target_sect
->use_rela_p
= 1;
2081 abfd
->flags
|= HAS_RELOC
;
2085 case SHT_GNU_verdef
:
2086 elf_dynverdef (abfd
) = shindex
;
2087 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
2088 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2091 case SHT_GNU_versym
:
2092 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
2095 elf_dynversym (abfd
) = shindex
;
2096 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
2097 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2100 case SHT_GNU_verneed
:
2101 elf_dynverref (abfd
) = shindex
;
2102 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2103 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2110 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
2113 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2116 if (hdr
->contents
!= NULL
)
2118 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2119 unsigned int n_elt
= hdr
->sh_size
/ sizeof (* idx
);
2124 if (idx
->flags
& GRP_COMDAT
)
2125 hdr
->bfd_section
->flags
2126 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2128 /* We try to keep the same section order as it comes in. */
2131 while (--n_elt
!= 0)
2135 if (idx
->shdr
!= NULL
2136 && (s
= idx
->shdr
->bfd_section
) != NULL
2137 && elf_next_in_group (s
) != NULL
)
2139 elf_next_in_group (hdr
->bfd_section
) = s
;
2147 /* Possibly an attributes section. */
2148 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
2149 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
2151 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
2153 _bfd_elf_parse_attributes (abfd
, hdr
);
2157 /* Check for any processor-specific section types. */
2158 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
2161 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
2163 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
2164 /* FIXME: How to properly handle allocated section reserved
2165 for applications? */
2166 (*_bfd_error_handler
)
2167 (_("%B: don't know how to handle allocated, application "
2168 "specific section `%s' [0x%8x]"),
2169 abfd
, name
, hdr
->sh_type
);
2172 /* Allow sections reserved for applications. */
2173 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
2178 else if (hdr
->sh_type
>= SHT_LOPROC
2179 && hdr
->sh_type
<= SHT_HIPROC
)
2180 /* FIXME: We should handle this section. */
2181 (*_bfd_error_handler
)
2182 (_("%B: don't know how to handle processor specific section "
2184 abfd
, name
, hdr
->sh_type
);
2185 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
2187 /* Unrecognised OS-specific sections. */
2188 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
2189 /* SHF_OS_NONCONFORMING indicates that special knowledge is
2190 required to correctly process the section and the file should
2191 be rejected with an error message. */
2192 (*_bfd_error_handler
)
2193 (_("%B: don't know how to handle OS specific section "
2195 abfd
, name
, hdr
->sh_type
);
2198 /* Otherwise it should be processed. */
2199 ret
= _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2204 /* FIXME: We should handle this section. */
2205 (*_bfd_error_handler
)
2206 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2207 abfd
, name
, hdr
->sh_type
);
2215 if (sections_being_created
&& sections_being_created_abfd
== abfd
)
2216 sections_being_created
[shindex
] = FALSE
;
2217 if (-- nesting
== 0)
2219 sections_being_created
= NULL
;
2220 sections_being_created_abfd
= abfd
;
2225 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2228 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2230 unsigned long r_symndx
)
2232 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2234 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2236 Elf_Internal_Shdr
*symtab_hdr
;
2237 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2238 Elf_External_Sym_Shndx eshndx
;
2240 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2241 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2242 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2245 if (cache
->abfd
!= abfd
)
2247 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2250 cache
->indx
[ent
] = r_symndx
;
2253 return &cache
->sym
[ent
];
2256 /* Given an ELF section number, retrieve the corresponding BFD
2260 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2262 if (sec_index
>= elf_numsections (abfd
))
2264 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2267 static const struct bfd_elf_special_section special_sections_b
[] =
2269 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2270 { NULL
, 0, 0, 0, 0 }
2273 static const struct bfd_elf_special_section special_sections_c
[] =
2275 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2276 { NULL
, 0, 0, 0, 0 }
2279 static const struct bfd_elf_special_section special_sections_d
[] =
2281 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2282 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2283 /* There are more DWARF sections than these, but they needn't be added here
2284 unless you have to cope with broken compilers that don't emit section
2285 attributes or you want to help the user writing assembler. */
2286 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2287 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2288 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2289 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2290 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2291 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2292 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2293 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2294 { NULL
, 0, 0, 0, 0 }
2297 static const struct bfd_elf_special_section special_sections_f
[] =
2299 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2300 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2301 { NULL
, 0, 0, 0, 0 }
2304 static const struct bfd_elf_special_section special_sections_g
[] =
2306 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2307 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2308 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2309 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2310 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2311 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2312 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2313 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2314 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2315 { NULL
, 0, 0, 0, 0 }
2318 static const struct bfd_elf_special_section special_sections_h
[] =
2320 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2321 { NULL
, 0, 0, 0, 0 }
2324 static const struct bfd_elf_special_section special_sections_i
[] =
2326 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2327 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2328 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2329 { NULL
, 0, 0, 0, 0 }
2332 static const struct bfd_elf_special_section special_sections_l
[] =
2334 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2335 { NULL
, 0, 0, 0, 0 }
2338 static const struct bfd_elf_special_section special_sections_n
[] =
2340 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2341 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2342 { NULL
, 0, 0, 0, 0 }
2345 static const struct bfd_elf_special_section special_sections_p
[] =
2347 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2348 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2349 { NULL
, 0, 0, 0, 0 }
2352 static const struct bfd_elf_special_section special_sections_r
[] =
2354 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2355 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2356 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2357 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2358 { NULL
, 0, 0, 0, 0 }
2361 static const struct bfd_elf_special_section special_sections_s
[] =
2363 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2364 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2365 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2366 /* See struct bfd_elf_special_section declaration for the semantics of
2367 this special case where .prefix_length != strlen (.prefix). */
2368 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2369 { NULL
, 0, 0, 0, 0 }
2372 static const struct bfd_elf_special_section special_sections_t
[] =
2374 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2375 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2376 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2377 { NULL
, 0, 0, 0, 0 }
2380 static const struct bfd_elf_special_section special_sections_z
[] =
2382 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2383 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2384 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2385 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2386 { NULL
, 0, 0, 0, 0 }
2389 static const struct bfd_elf_special_section
* const special_sections
[] =
2391 special_sections_b
, /* 'b' */
2392 special_sections_c
, /* 'c' */
2393 special_sections_d
, /* 'd' */
2395 special_sections_f
, /* 'f' */
2396 special_sections_g
, /* 'g' */
2397 special_sections_h
, /* 'h' */
2398 special_sections_i
, /* 'i' */
2401 special_sections_l
, /* 'l' */
2403 special_sections_n
, /* 'n' */
2405 special_sections_p
, /* 'p' */
2407 special_sections_r
, /* 'r' */
2408 special_sections_s
, /* 's' */
2409 special_sections_t
, /* 't' */
2415 special_sections_z
/* 'z' */
2418 const struct bfd_elf_special_section
*
2419 _bfd_elf_get_special_section (const char *name
,
2420 const struct bfd_elf_special_section
*spec
,
2426 len
= strlen (name
);
2428 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2431 int prefix_len
= spec
[i
].prefix_length
;
2433 if (len
< prefix_len
)
2435 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2438 suffix_len
= spec
[i
].suffix_length
;
2439 if (suffix_len
<= 0)
2441 if (name
[prefix_len
] != 0)
2443 if (suffix_len
== 0)
2445 if (name
[prefix_len
] != '.'
2446 && (suffix_len
== -2
2447 || (rela
&& spec
[i
].type
== SHT_REL
)))
2453 if (len
< prefix_len
+ suffix_len
)
2455 if (memcmp (name
+ len
- suffix_len
,
2456 spec
[i
].prefix
+ prefix_len
,
2466 const struct bfd_elf_special_section
*
2467 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2470 const struct bfd_elf_special_section
*spec
;
2471 const struct elf_backend_data
*bed
;
2473 /* See if this is one of the special sections. */
2474 if (sec
->name
== NULL
)
2477 bed
= get_elf_backend_data (abfd
);
2478 spec
= bed
->special_sections
;
2481 spec
= _bfd_elf_get_special_section (sec
->name
,
2482 bed
->special_sections
,
2488 if (sec
->name
[0] != '.')
2491 i
= sec
->name
[1] - 'b';
2492 if (i
< 0 || i
> 'z' - 'b')
2495 spec
= special_sections
[i
];
2500 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2504 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2506 struct bfd_elf_section_data
*sdata
;
2507 const struct elf_backend_data
*bed
;
2508 const struct bfd_elf_special_section
*ssect
;
2510 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2513 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2517 sec
->used_by_bfd
= sdata
;
2520 /* Indicate whether or not this section should use RELA relocations. */
2521 bed
= get_elf_backend_data (abfd
);
2522 sec
->use_rela_p
= bed
->default_use_rela_p
;
2524 /* When we read a file, we don't need to set ELF section type and
2525 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2526 anyway. We will set ELF section type and flags for all linker
2527 created sections. If user specifies BFD section flags, we will
2528 set ELF section type and flags based on BFD section flags in
2529 elf_fake_sections. Special handling for .init_array/.fini_array
2530 output sections since they may contain .ctors/.dtors input
2531 sections. We don't want _bfd_elf_init_private_section_data to
2532 copy ELF section type from .ctors/.dtors input sections. */
2533 if (abfd
->direction
!= read_direction
2534 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2536 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2539 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2540 || ssect
->type
== SHT_INIT_ARRAY
2541 || ssect
->type
== SHT_FINI_ARRAY
))
2543 elf_section_type (sec
) = ssect
->type
;
2544 elf_section_flags (sec
) = ssect
->attr
;
2548 return _bfd_generic_new_section_hook (abfd
, sec
);
2551 /* Create a new bfd section from an ELF program header.
2553 Since program segments have no names, we generate a synthetic name
2554 of the form segment<NUM>, where NUM is generally the index in the
2555 program header table. For segments that are split (see below) we
2556 generate the names segment<NUM>a and segment<NUM>b.
2558 Note that some program segments may have a file size that is different than
2559 (less than) the memory size. All this means is that at execution the
2560 system must allocate the amount of memory specified by the memory size,
2561 but only initialize it with the first "file size" bytes read from the
2562 file. This would occur for example, with program segments consisting
2563 of combined data+bss.
2565 To handle the above situation, this routine generates TWO bfd sections
2566 for the single program segment. The first has the length specified by
2567 the file size of the segment, and the second has the length specified
2568 by the difference between the two sizes. In effect, the segment is split
2569 into its initialized and uninitialized parts.
2574 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2575 Elf_Internal_Phdr
*hdr
,
2577 const char *type_name
)
2585 split
= ((hdr
->p_memsz
> 0)
2586 && (hdr
->p_filesz
> 0)
2587 && (hdr
->p_memsz
> hdr
->p_filesz
));
2589 if (hdr
->p_filesz
> 0)
2591 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2592 len
= strlen (namebuf
) + 1;
2593 name
= (char *) bfd_alloc (abfd
, len
);
2596 memcpy (name
, namebuf
, len
);
2597 newsect
= bfd_make_section (abfd
, name
);
2598 if (newsect
== NULL
)
2600 newsect
->vma
= hdr
->p_vaddr
;
2601 newsect
->lma
= hdr
->p_paddr
;
2602 newsect
->size
= hdr
->p_filesz
;
2603 newsect
->filepos
= hdr
->p_offset
;
2604 newsect
->flags
|= SEC_HAS_CONTENTS
;
2605 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2606 if (hdr
->p_type
== PT_LOAD
)
2608 newsect
->flags
|= SEC_ALLOC
;
2609 newsect
->flags
|= SEC_LOAD
;
2610 if (hdr
->p_flags
& PF_X
)
2612 /* FIXME: all we known is that it has execute PERMISSION,
2614 newsect
->flags
|= SEC_CODE
;
2617 if (!(hdr
->p_flags
& PF_W
))
2619 newsect
->flags
|= SEC_READONLY
;
2623 if (hdr
->p_memsz
> hdr
->p_filesz
)
2627 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2628 len
= strlen (namebuf
) + 1;
2629 name
= (char *) bfd_alloc (abfd
, len
);
2632 memcpy (name
, namebuf
, len
);
2633 newsect
= bfd_make_section (abfd
, name
);
2634 if (newsect
== NULL
)
2636 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2637 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2638 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2639 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2640 align
= newsect
->vma
& -newsect
->vma
;
2641 if (align
== 0 || align
> hdr
->p_align
)
2642 align
= hdr
->p_align
;
2643 newsect
->alignment_power
= bfd_log2 (align
);
2644 if (hdr
->p_type
== PT_LOAD
)
2646 /* Hack for gdb. Segments that have not been modified do
2647 not have their contents written to a core file, on the
2648 assumption that a debugger can find the contents in the
2649 executable. We flag this case by setting the fake
2650 section size to zero. Note that "real" bss sections will
2651 always have their contents dumped to the core file. */
2652 if (bfd_get_format (abfd
) == bfd_core
)
2654 newsect
->flags
|= SEC_ALLOC
;
2655 if (hdr
->p_flags
& PF_X
)
2656 newsect
->flags
|= SEC_CODE
;
2658 if (!(hdr
->p_flags
& PF_W
))
2659 newsect
->flags
|= SEC_READONLY
;
2666 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2668 const struct elf_backend_data
*bed
;
2670 switch (hdr
->p_type
)
2673 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2676 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2679 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2682 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2685 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2687 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2692 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2695 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2697 case PT_GNU_EH_FRAME
:
2698 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2702 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2705 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2708 /* Check for any processor-specific program segment types. */
2709 bed
= get_elf_backend_data (abfd
);
2710 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2714 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2718 _bfd_elf_single_rel_hdr (asection
*sec
)
2720 if (elf_section_data (sec
)->rel
.hdr
)
2722 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2723 return elf_section_data (sec
)->rel
.hdr
;
2726 return elf_section_data (sec
)->rela
.hdr
;
2730 _bfd_elf_set_reloc_sh_name (bfd
*abfd
,
2731 Elf_Internal_Shdr
*rel_hdr
,
2732 const char *sec_name
,
2733 bfd_boolean use_rela_p
)
2735 char *name
= (char *) bfd_alloc (abfd
,
2736 sizeof ".rela" + strlen (sec_name
));
2740 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", sec_name
);
2742 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2744 if (rel_hdr
->sh_name
== (unsigned int) -1)
2750 /* Allocate and initialize a section-header for a new reloc section,
2751 containing relocations against ASECT. It is stored in RELDATA. If
2752 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2756 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2757 struct bfd_elf_section_reloc_data
*reldata
,
2758 const char *sec_name
,
2759 bfd_boolean use_rela_p
,
2760 bfd_boolean delay_st_name_p
)
2762 Elf_Internal_Shdr
*rel_hdr
;
2763 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2766 amt
= sizeof (Elf_Internal_Shdr
);
2767 BFD_ASSERT (reldata
->hdr
== NULL
);
2768 rel_hdr
= bfd_zalloc (abfd
, amt
);
2769 reldata
->hdr
= rel_hdr
;
2771 if (delay_st_name_p
)
2772 rel_hdr
->sh_name
= (unsigned int) -1;
2773 else if (!_bfd_elf_set_reloc_sh_name (abfd
, rel_hdr
, sec_name
,
2776 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2777 rel_hdr
->sh_entsize
= (use_rela_p
2778 ? bed
->s
->sizeof_rela
2779 : bed
->s
->sizeof_rel
);
2780 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2781 rel_hdr
->sh_flags
= 0;
2782 rel_hdr
->sh_addr
= 0;
2783 rel_hdr
->sh_size
= 0;
2784 rel_hdr
->sh_offset
= 0;
2789 /* Return the default section type based on the passed in section flags. */
2792 bfd_elf_get_default_section_type (flagword flags
)
2794 if ((flags
& SEC_ALLOC
) != 0
2795 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2797 return SHT_PROGBITS
;
2800 struct fake_section_arg
2802 struct bfd_link_info
*link_info
;
2806 /* Set up an ELF internal section header for a section. */
2809 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2811 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2812 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2813 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2814 Elf_Internal_Shdr
*this_hdr
;
2815 unsigned int sh_type
;
2816 const char *name
= asect
->name
;
2817 bfd_boolean delay_st_name_p
= FALSE
;
2821 /* We already failed; just get out of the bfd_map_over_sections
2826 this_hdr
= &esd
->this_hdr
;
2830 /* ld: compress DWARF debug sections with names: .debug_*. */
2831 if ((arg
->link_info
->compress_debug
& COMPRESS_DEBUG
)
2832 && (asect
->flags
& SEC_DEBUGGING
)
2836 /* Set SEC_ELF_COMPRESS to indicate this section should be
2838 asect
->flags
|= SEC_ELF_COMPRESS
;
2840 /* If this section will be compressed, delay adding setion
2841 name to section name section after it is compressed in
2842 _bfd_elf_assign_file_positions_for_non_load. */
2843 delay_st_name_p
= TRUE
;
2846 else if ((asect
->flags
& SEC_ELF_RENAME
))
2848 /* objcopy: rename output DWARF debug section. */
2849 if ((abfd
->flags
& (BFD_DECOMPRESS
| BFD_COMPRESS_GABI
)))
2851 /* When we decompress or compress with SHF_COMPRESSED,
2852 convert section name from .zdebug_* to .debug_* if
2856 char *new_name
= convert_zdebug_to_debug (abfd
, name
);
2857 if (new_name
== NULL
)
2865 else if (asect
->compress_status
== COMPRESS_SECTION_DONE
)
2867 /* PR binutils/18087: Compression does not always make a
2868 section smaller. So only rename the section when
2869 compression has actually taken place. If input section
2870 name is .zdebug_*, we should never compress it again. */
2871 char *new_name
= convert_debug_to_zdebug (abfd
, name
);
2872 if (new_name
== NULL
)
2877 BFD_ASSERT (name
[1] != 'z');
2882 if (delay_st_name_p
)
2883 this_hdr
->sh_name
= (unsigned int) -1;
2887 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2889 if (this_hdr
->sh_name
== (unsigned int) -1)
2896 /* Don't clear sh_flags. Assembler may set additional bits. */
2898 if ((asect
->flags
& SEC_ALLOC
) != 0
2899 || asect
->user_set_vma
)
2900 this_hdr
->sh_addr
= asect
->vma
;
2902 this_hdr
->sh_addr
= 0;
2904 this_hdr
->sh_offset
= 0;
2905 this_hdr
->sh_size
= asect
->size
;
2906 this_hdr
->sh_link
= 0;
2907 /* PR 17512: file: 0eb809fe, 8b0535ee. */
2908 if (asect
->alignment_power
>= (sizeof (bfd_vma
) * 8) - 1)
2910 (*_bfd_error_handler
)
2911 (_("%B: error: Alignment power %d of section `%A' is too big"),
2912 abfd
, asect
, asect
->alignment_power
);
2916 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2917 /* The sh_entsize and sh_info fields may have been set already by
2918 copy_private_section_data. */
2920 this_hdr
->bfd_section
= asect
;
2921 this_hdr
->contents
= NULL
;
2923 /* If the section type is unspecified, we set it based on
2925 if ((asect
->flags
& SEC_GROUP
) != 0)
2926 sh_type
= SHT_GROUP
;
2928 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2930 if (this_hdr
->sh_type
== SHT_NULL
)
2931 this_hdr
->sh_type
= sh_type
;
2932 else if (this_hdr
->sh_type
== SHT_NOBITS
2933 && sh_type
== SHT_PROGBITS
2934 && (asect
->flags
& SEC_ALLOC
) != 0)
2936 /* Warn if we are changing a NOBITS section to PROGBITS, but
2937 allow the link to proceed. This can happen when users link
2938 non-bss input sections to bss output sections, or emit data
2939 to a bss output section via a linker script. */
2940 (*_bfd_error_handler
)
2941 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2942 this_hdr
->sh_type
= sh_type
;
2945 switch (this_hdr
->sh_type
)
2951 case SHT_INIT_ARRAY
:
2952 case SHT_FINI_ARRAY
:
2953 case SHT_PREINIT_ARRAY
:
2960 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2964 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2968 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2972 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2973 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2977 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2978 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2981 case SHT_GNU_versym
:
2982 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2985 case SHT_GNU_verdef
:
2986 this_hdr
->sh_entsize
= 0;
2987 /* objcopy or strip will copy over sh_info, but may not set
2988 cverdefs. The linker will set cverdefs, but sh_info will be
2990 if (this_hdr
->sh_info
== 0)
2991 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2993 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2994 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2997 case SHT_GNU_verneed
:
2998 this_hdr
->sh_entsize
= 0;
2999 /* objcopy or strip will copy over sh_info, but may not set
3000 cverrefs. The linker will set cverrefs, but sh_info will be
3002 if (this_hdr
->sh_info
== 0)
3003 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
3005 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
3006 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
3010 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
3014 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
3018 if ((asect
->flags
& SEC_ALLOC
) != 0)
3019 this_hdr
->sh_flags
|= SHF_ALLOC
;
3020 if ((asect
->flags
& SEC_READONLY
) == 0)
3021 this_hdr
->sh_flags
|= SHF_WRITE
;
3022 if ((asect
->flags
& SEC_CODE
) != 0)
3023 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
3024 if ((asect
->flags
& SEC_MERGE
) != 0)
3026 this_hdr
->sh_flags
|= SHF_MERGE
;
3027 this_hdr
->sh_entsize
= asect
->entsize
;
3028 if ((asect
->flags
& SEC_STRINGS
) != 0)
3029 this_hdr
->sh_flags
|= SHF_STRINGS
;
3031 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
3032 this_hdr
->sh_flags
|= SHF_GROUP
;
3033 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
3035 this_hdr
->sh_flags
|= SHF_TLS
;
3036 if (asect
->size
== 0
3037 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
3039 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
3041 this_hdr
->sh_size
= 0;
3044 this_hdr
->sh_size
= o
->offset
+ o
->size
;
3045 if (this_hdr
->sh_size
!= 0)
3046 this_hdr
->sh_type
= SHT_NOBITS
;
3050 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
3051 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
3053 /* If the section has relocs, set up a section header for the
3054 SHT_REL[A] section. If two relocation sections are required for
3055 this section, it is up to the processor-specific back-end to
3056 create the other. */
3057 if ((asect
->flags
& SEC_RELOC
) != 0)
3059 /* When doing a relocatable link, create both REL and RELA sections if
3062 /* Do the normal setup if we wouldn't create any sections here. */
3063 && esd
->rel
.count
+ esd
->rela
.count
> 0
3064 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
3066 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
3067 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, name
, FALSE
,
3073 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
3074 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, name
, TRUE
,
3081 else if (!_bfd_elf_init_reloc_shdr (abfd
,
3083 ? &esd
->rela
: &esd
->rel
),
3090 /* Check for processor-specific section types. */
3091 sh_type
= this_hdr
->sh_type
;
3092 if (bed
->elf_backend_fake_sections
3093 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
3096 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
3098 /* Don't change the header type from NOBITS if we are being
3099 called for objcopy --only-keep-debug. */
3100 this_hdr
->sh_type
= sh_type
;
3104 /* Fill in the contents of a SHT_GROUP section. Called from
3105 _bfd_elf_compute_section_file_positions for gas, objcopy, and
3106 when ELF targets use the generic linker, ld. Called for ld -r
3107 from bfd_elf_final_link. */
3110 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
3112 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
3113 asection
*elt
, *first
;
3117 /* Ignore linker created group section. See elfNN_ia64_object_p in
3119 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
3123 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
3125 unsigned long symindx
= 0;
3127 /* elf_group_id will have been set up by objcopy and the
3129 if (elf_group_id (sec
) != NULL
)
3130 symindx
= elf_group_id (sec
)->udata
.i
;
3134 /* If called from the assembler, swap_out_syms will have set up
3135 elf_section_syms. */
3136 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
3137 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
3139 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
3141 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
3143 /* The ELF backend linker sets sh_info to -2 when the group
3144 signature symbol is global, and thus the index can't be
3145 set until all local symbols are output. */
3146 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
3147 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
3148 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
3149 unsigned long extsymoff
= 0;
3150 struct elf_link_hash_entry
*h
;
3152 if (!elf_bad_symtab (igroup
->owner
))
3154 Elf_Internal_Shdr
*symtab_hdr
;
3156 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
3157 extsymoff
= symtab_hdr
->sh_info
;
3159 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
3160 while (h
->root
.type
== bfd_link_hash_indirect
3161 || h
->root
.type
== bfd_link_hash_warning
)
3162 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
3164 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
3167 /* The contents won't be allocated for "ld -r" or objcopy. */
3169 if (sec
->contents
== NULL
)
3172 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
3174 /* Arrange for the section to be written out. */
3175 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
3176 if (sec
->contents
== NULL
)
3183 loc
= sec
->contents
+ sec
->size
;
3185 /* Get the pointer to the first section in the group that gas
3186 squirreled away here. objcopy arranges for this to be set to the
3187 start of the input section group. */
3188 first
= elt
= elf_next_in_group (sec
);
3190 /* First element is a flag word. Rest of section is elf section
3191 indices for all the sections of the group. Write them backwards
3192 just to keep the group in the same order as given in .section
3193 directives, not that it matters. */
3200 s
= s
->output_section
;
3202 && !bfd_is_abs_section (s
))
3204 unsigned int idx
= elf_section_data (s
)->this_idx
;
3207 H_PUT_32 (abfd
, idx
, loc
);
3209 elt
= elf_next_in_group (elt
);
3214 if ((loc
-= 4) != sec
->contents
)
3217 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
3220 /* Return the section which RELOC_SEC applies to. */
3223 _bfd_elf_get_reloc_section (asection
*reloc_sec
)
3229 if (reloc_sec
== NULL
)
3232 type
= elf_section_data (reloc_sec
)->this_hdr
.sh_type
;
3233 if (type
!= SHT_REL
&& type
!= SHT_RELA
)
3236 /* We look up the section the relocs apply to by name. */
3237 name
= reloc_sec
->name
;
3238 if (type
== SHT_REL
)
3243 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
3244 section apply to .got.plt section. */
3245 abfd
= reloc_sec
->owner
;
3246 if (get_elf_backend_data (abfd
)->want_got_plt
3247 && strcmp (name
, ".plt") == 0)
3249 /* .got.plt is a linker created input section. It may be mapped
3250 to some other output section. Try two likely sections. */
3252 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3253 if (reloc_sec
!= NULL
)
3258 reloc_sec
= bfd_get_section_by_name (abfd
, name
);
3262 /* Assign all ELF section numbers. The dummy first section is handled here
3263 too. The link/info pointers for the standard section types are filled
3264 in here too, while we're at it. */
3267 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
3269 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
3271 unsigned int section_number
;
3272 Elf_Internal_Shdr
**i_shdrp
;
3273 struct bfd_elf_section_data
*d
;
3274 bfd_boolean need_symtab
;
3278 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
3280 /* SHT_GROUP sections are in relocatable files only. */
3281 if (link_info
== NULL
|| link_info
->relocatable
)
3283 /* Put SHT_GROUP sections first. */
3284 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3286 d
= elf_section_data (sec
);
3288 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
3290 if (sec
->flags
& SEC_LINKER_CREATED
)
3292 /* Remove the linker created SHT_GROUP sections. */
3293 bfd_section_list_remove (abfd
, sec
);
3294 abfd
->section_count
--;
3297 d
->this_idx
= section_number
++;
3302 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3304 d
= elf_section_data (sec
);
3306 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
3307 d
->this_idx
= section_number
++;
3308 if (d
->this_hdr
.sh_name
!= (unsigned int) -1)
3309 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
3312 d
->rel
.idx
= section_number
++;
3313 if (d
->rel
.hdr
->sh_name
!= (unsigned int) -1)
3314 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
3321 d
->rela
.idx
= section_number
++;
3322 if (d
->rela
.hdr
->sh_name
!= (unsigned int) -1)
3323 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
3329 elf_shstrtab_sec (abfd
) = section_number
++;
3330 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
3331 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
3333 need_symtab
= (bfd_get_symcount (abfd
) > 0
3334 || (link_info
== NULL
3335 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3339 elf_onesymtab (abfd
) = section_number
++;
3340 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3341 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3343 elf_symtab_shndx (abfd
) = section_number
++;
3344 t
->symtab_shndx_hdr
.sh_name
3345 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3346 ".symtab_shndx", FALSE
);
3347 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3350 elf_strtab_sec (abfd
) = section_number
++;
3351 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3354 if (section_number
>= SHN_LORESERVE
)
3356 _bfd_error_handler (_("%B: too many sections: %u"),
3357 abfd
, section_number
);
3361 elf_numsections (abfd
) = section_number
;
3362 elf_elfheader (abfd
)->e_shnum
= section_number
;
3364 /* Set up the list of section header pointers, in agreement with the
3366 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3367 sizeof (Elf_Internal_Shdr
*));
3368 if (i_shdrp
== NULL
)
3371 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3372 sizeof (Elf_Internal_Shdr
));
3373 if (i_shdrp
[0] == NULL
)
3375 bfd_release (abfd
, i_shdrp
);
3379 elf_elfsections (abfd
) = i_shdrp
;
3381 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3384 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3385 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3387 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3388 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3390 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3391 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3394 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3398 d
= elf_section_data (sec
);
3400 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3401 if (d
->rel
.idx
!= 0)
3402 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3403 if (d
->rela
.idx
!= 0)
3404 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3406 /* Fill in the sh_link and sh_info fields while we're at it. */
3408 /* sh_link of a reloc section is the section index of the symbol
3409 table. sh_info is the section index of the section to which
3410 the relocation entries apply. */
3411 if (d
->rel
.idx
!= 0)
3413 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3414 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3415 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3417 if (d
->rela
.idx
!= 0)
3419 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3420 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3421 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3424 /* We need to set up sh_link for SHF_LINK_ORDER. */
3425 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3427 s
= elf_linked_to_section (sec
);
3430 /* elf_linked_to_section points to the input section. */
3431 if (link_info
!= NULL
)
3433 /* Check discarded linkonce section. */
3434 if (discarded_section (s
))
3437 (*_bfd_error_handler
)
3438 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3439 abfd
, d
->this_hdr
.bfd_section
,
3441 /* Point to the kept section if it has the same
3442 size as the discarded one. */
3443 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3446 bfd_set_error (bfd_error_bad_value
);
3452 s
= s
->output_section
;
3453 BFD_ASSERT (s
!= NULL
);
3457 /* Handle objcopy. */
3458 if (s
->output_section
== NULL
)
3460 (*_bfd_error_handler
)
3461 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3462 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3463 bfd_set_error (bfd_error_bad_value
);
3466 s
= s
->output_section
;
3468 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3473 The Intel C compiler generates SHT_IA_64_UNWIND with
3474 SHF_LINK_ORDER. But it doesn't set the sh_link or
3475 sh_info fields. Hence we could get the situation
3477 const struct elf_backend_data
*bed
3478 = get_elf_backend_data (abfd
);
3479 if (bed
->link_order_error_handler
)
3480 bed
->link_order_error_handler
3481 (_("%B: warning: sh_link not set for section `%A'"),
3486 switch (d
->this_hdr
.sh_type
)
3490 /* A reloc section which we are treating as a normal BFD
3491 section. sh_link is the section index of the symbol
3492 table. sh_info is the section index of the section to
3493 which the relocation entries apply. We assume that an
3494 allocated reloc section uses the dynamic symbol table.
3495 FIXME: How can we be sure? */
3496 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3498 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3500 s
= get_elf_backend_data (abfd
)->get_reloc_section (sec
);
3503 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3504 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3509 /* We assume that a section named .stab*str is a stabs
3510 string section. We look for a section with the same name
3511 but without the trailing ``str'', and set its sh_link
3512 field to point to this section. */
3513 if (CONST_STRNEQ (sec
->name
, ".stab")
3514 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3519 len
= strlen (sec
->name
);
3520 alc
= (char *) bfd_malloc (len
- 2);
3523 memcpy (alc
, sec
->name
, len
- 3);
3524 alc
[len
- 3] = '\0';
3525 s
= bfd_get_section_by_name (abfd
, alc
);
3529 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3531 /* This is a .stab section. */
3532 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3533 elf_section_data (s
)->this_hdr
.sh_entsize
3534 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3541 case SHT_GNU_verneed
:
3542 case SHT_GNU_verdef
:
3543 /* sh_link is the section header index of the string table
3544 used for the dynamic entries, or the symbol table, or the
3546 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3548 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3551 case SHT_GNU_LIBLIST
:
3552 /* sh_link is the section header index of the prelink library
3553 list used for the dynamic entries, or the symbol table, or
3554 the version strings. */
3555 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3556 ? ".dynstr" : ".gnu.libstr");
3558 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3563 case SHT_GNU_versym
:
3564 /* sh_link is the section header index of the symbol table
3565 this hash table or version table is for. */
3566 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3568 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3572 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3576 /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3577 _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3578 debug section name from .debug_* to .zdebug_* if needed. */
3584 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3586 /* If the backend has a special mapping, use it. */
3587 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3588 if (bed
->elf_backend_sym_is_global
)
3589 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3591 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3592 || bfd_is_und_section (bfd_get_section (sym
))
3593 || bfd_is_com_section (bfd_get_section (sym
)));
3596 /* Don't output section symbols for sections that are not going to be
3597 output, that are duplicates or there is no BFD section. */
3600 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3602 elf_symbol_type
*type_ptr
;
3604 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3607 type_ptr
= elf_symbol_from (abfd
, sym
);
3608 return ((type_ptr
!= NULL
3609 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3610 && bfd_is_abs_section (sym
->section
))
3611 || !(sym
->section
->owner
== abfd
3612 || (sym
->section
->output_section
->owner
== abfd
3613 && sym
->section
->output_offset
== 0)
3614 || bfd_is_abs_section (sym
->section
)));
3617 /* Map symbol from it's internal number to the external number, moving
3618 all local symbols to be at the head of the list. */
3621 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3623 unsigned int symcount
= bfd_get_symcount (abfd
);
3624 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3625 asymbol
**sect_syms
;
3626 unsigned int num_locals
= 0;
3627 unsigned int num_globals
= 0;
3628 unsigned int num_locals2
= 0;
3629 unsigned int num_globals2
= 0;
3636 fprintf (stderr
, "elf_map_symbols\n");
3640 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3642 if (max_index
< asect
->index
)
3643 max_index
= asect
->index
;
3647 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3648 if (sect_syms
== NULL
)
3650 elf_section_syms (abfd
) = sect_syms
;
3651 elf_num_section_syms (abfd
) = max_index
;
3653 /* Init sect_syms entries for any section symbols we have already
3654 decided to output. */
3655 for (idx
= 0; idx
< symcount
; idx
++)
3657 asymbol
*sym
= syms
[idx
];
3659 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3661 && !ignore_section_sym (abfd
, sym
)
3662 && !bfd_is_abs_section (sym
->section
))
3664 asection
*sec
= sym
->section
;
3666 if (sec
->owner
!= abfd
)
3667 sec
= sec
->output_section
;
3669 sect_syms
[sec
->index
] = syms
[idx
];
3673 /* Classify all of the symbols. */
3674 for (idx
= 0; idx
< symcount
; idx
++)
3676 if (sym_is_global (abfd
, syms
[idx
]))
3678 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3682 /* We will be adding a section symbol for each normal BFD section. Most
3683 sections will already have a section symbol in outsymbols, but
3684 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3685 at least in that case. */
3686 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3688 if (sect_syms
[asect
->index
] == NULL
)
3690 if (!sym_is_global (abfd
, asect
->symbol
))
3697 /* Now sort the symbols so the local symbols are first. */
3698 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3699 sizeof (asymbol
*));
3701 if (new_syms
== NULL
)
3704 for (idx
= 0; idx
< symcount
; idx
++)
3706 asymbol
*sym
= syms
[idx
];
3709 if (sym_is_global (abfd
, sym
))
3710 i
= num_locals
+ num_globals2
++;
3711 else if (!ignore_section_sym (abfd
, sym
))
3716 sym
->udata
.i
= i
+ 1;
3718 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3720 if (sect_syms
[asect
->index
] == NULL
)
3722 asymbol
*sym
= asect
->symbol
;
3725 sect_syms
[asect
->index
] = sym
;
3726 if (!sym_is_global (abfd
, sym
))
3729 i
= num_locals
+ num_globals2
++;
3731 sym
->udata
.i
= i
+ 1;
3735 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3737 *pnum_locals
= num_locals
;
3741 /* Align to the maximum file alignment that could be required for any
3742 ELF data structure. */
3744 static inline file_ptr
3745 align_file_position (file_ptr off
, int align
)
3747 return (off
+ align
- 1) & ~(align
- 1);
3750 /* Assign a file position to a section, optionally aligning to the
3751 required section alignment. */
3754 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3758 if (align
&& i_shdrp
->sh_addralign
> 1)
3759 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3760 i_shdrp
->sh_offset
= offset
;
3761 if (i_shdrp
->bfd_section
!= NULL
)
3762 i_shdrp
->bfd_section
->filepos
= offset
;
3763 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3764 offset
+= i_shdrp
->sh_size
;
3768 /* Compute the file positions we are going to put the sections at, and
3769 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3770 is not NULL, this is being called by the ELF backend linker. */
3773 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3774 struct bfd_link_info
*link_info
)
3776 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3777 struct fake_section_arg fsargs
;
3779 struct elf_strtab_hash
*strtab
= NULL
;
3780 Elf_Internal_Shdr
*shstrtab_hdr
;
3781 bfd_boolean need_symtab
;
3783 if (abfd
->output_has_begun
)
3786 /* Do any elf backend specific processing first. */
3787 if (bed
->elf_backend_begin_write_processing
)
3788 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3790 if (! prep_headers (abfd
))
3793 /* Post process the headers if necessary. */
3794 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3796 fsargs
.failed
= FALSE
;
3797 fsargs
.link_info
= link_info
;
3798 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3802 if (!assign_section_numbers (abfd
, link_info
))
3805 /* The backend linker builds symbol table information itself. */
3806 need_symtab
= (link_info
== NULL
3807 && (bfd_get_symcount (abfd
) > 0
3808 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3812 /* Non-zero if doing a relocatable link. */
3813 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3815 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3820 if (link_info
== NULL
)
3822 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3827 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3828 /* sh_name was set in prep_headers. */
3829 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3830 shstrtab_hdr
->sh_flags
= 0;
3831 shstrtab_hdr
->sh_addr
= 0;
3832 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */
3833 shstrtab_hdr
->sh_entsize
= 0;
3834 shstrtab_hdr
->sh_link
= 0;
3835 shstrtab_hdr
->sh_info
= 0;
3836 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */
3837 shstrtab_hdr
->sh_addralign
= 1;
3839 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3845 Elf_Internal_Shdr
*hdr
;
3847 off
= elf_next_file_pos (abfd
);
3849 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3850 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3852 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3853 if (hdr
->sh_size
!= 0)
3854 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3856 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3857 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3859 elf_next_file_pos (abfd
) = off
;
3861 /* Now that we know where the .strtab section goes, write it
3863 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3864 || ! _bfd_elf_strtab_emit (abfd
, strtab
))
3866 _bfd_elf_strtab_free (strtab
);
3869 abfd
->output_has_begun
= TRUE
;
3874 /* Make an initial estimate of the size of the program header. If we
3875 get the number wrong here, we'll redo section placement. */
3877 static bfd_size_type
3878 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3882 const struct elf_backend_data
*bed
;
3884 /* Assume we will need exactly two PT_LOAD segments: one for text
3885 and one for data. */
3888 s
= bfd_get_section_by_name (abfd
, ".interp");
3889 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3891 /* If we have a loadable interpreter section, we need a
3892 PT_INTERP segment. In this case, assume we also need a
3893 PT_PHDR segment, although that may not be true for all
3898 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3900 /* We need a PT_DYNAMIC segment. */
3904 if (info
!= NULL
&& info
->relro
)
3906 /* We need a PT_GNU_RELRO segment. */
3910 if (elf_eh_frame_hdr (abfd
))
3912 /* We need a PT_GNU_EH_FRAME segment. */
3916 if (elf_stack_flags (abfd
))
3918 /* We need a PT_GNU_STACK segment. */
3922 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3924 if ((s
->flags
& SEC_LOAD
) != 0
3925 && CONST_STRNEQ (s
->name
, ".note"))
3927 /* We need a PT_NOTE segment. */
3929 /* Try to create just one PT_NOTE segment
3930 for all adjacent loadable .note* sections.
3931 gABI requires that within a PT_NOTE segment
3932 (and also inside of each SHT_NOTE section)
3933 each note is padded to a multiple of 4 size,
3934 so we check whether the sections are correctly
3936 if (s
->alignment_power
== 2)
3937 while (s
->next
!= NULL
3938 && s
->next
->alignment_power
== 2
3939 && (s
->next
->flags
& SEC_LOAD
) != 0
3940 && CONST_STRNEQ (s
->next
->name
, ".note"))
3945 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3947 if (s
->flags
& SEC_THREAD_LOCAL
)
3949 /* We need a PT_TLS segment. */
3955 /* Let the backend count up any program headers it might need. */
3956 bed
= get_elf_backend_data (abfd
);
3957 if (bed
->elf_backend_additional_program_headers
)
3961 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3967 return segs
* bed
->s
->sizeof_phdr
;
3970 /* Find the segment that contains the output_section of section. */
3973 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3975 struct elf_segment_map
*m
;
3976 Elf_Internal_Phdr
*p
;
3978 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3984 for (i
= m
->count
- 1; i
>= 0; i
--)
3985 if (m
->sections
[i
] == section
)
3992 /* Create a mapping from a set of sections to a program segment. */
3994 static struct elf_segment_map
*
3995 make_mapping (bfd
*abfd
,
3996 asection
**sections
,
4001 struct elf_segment_map
*m
;
4006 amt
= sizeof (struct elf_segment_map
);
4007 amt
+= (to
- from
- 1) * sizeof (asection
*);
4008 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4012 m
->p_type
= PT_LOAD
;
4013 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
4014 m
->sections
[i
- from
] = *hdrpp
;
4015 m
->count
= to
- from
;
4017 if (from
== 0 && phdr
)
4019 /* Include the headers in the first PT_LOAD segment. */
4020 m
->includes_filehdr
= 1;
4021 m
->includes_phdrs
= 1;
4027 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
4030 struct elf_segment_map
*
4031 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
4033 struct elf_segment_map
*m
;
4035 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
4036 sizeof (struct elf_segment_map
));
4040 m
->p_type
= PT_DYNAMIC
;
4042 m
->sections
[0] = dynsec
;
4047 /* Possibly add or remove segments from the segment map. */
4050 elf_modify_segment_map (bfd
*abfd
,
4051 struct bfd_link_info
*info
,
4052 bfd_boolean remove_empty_load
)
4054 struct elf_segment_map
**m
;
4055 const struct elf_backend_data
*bed
;
4057 /* The placement algorithm assumes that non allocated sections are
4058 not in PT_LOAD segments. We ensure this here by removing such
4059 sections from the segment map. We also remove excluded
4060 sections. Finally, any PT_LOAD segment without sections is
4062 m
= &elf_seg_map (abfd
);
4065 unsigned int i
, new_count
;
4067 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
4069 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
4070 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
4071 || (*m
)->p_type
!= PT_LOAD
))
4073 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
4077 (*m
)->count
= new_count
;
4079 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
4085 bed
= get_elf_backend_data (abfd
);
4086 if (bed
->elf_backend_modify_segment_map
!= NULL
)
4088 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
4095 /* Set up a mapping from BFD sections to program segments. */
4098 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
4101 struct elf_segment_map
*m
;
4102 asection
**sections
= NULL
;
4103 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4104 bfd_boolean no_user_phdrs
;
4106 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
4109 info
->user_phdrs
= !no_user_phdrs
;
4111 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
4115 struct elf_segment_map
*mfirst
;
4116 struct elf_segment_map
**pm
;
4119 unsigned int phdr_index
;
4120 bfd_vma maxpagesize
;
4122 bfd_boolean phdr_in_segment
= TRUE
;
4123 bfd_boolean writable
;
4125 asection
*first_tls
= NULL
;
4126 asection
*dynsec
, *eh_frame_hdr
;
4128 bfd_vma addr_mask
, wrap_to
= 0;
4130 /* Select the allocated sections, and sort them. */
4132 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
4133 sizeof (asection
*));
4134 if (sections
== NULL
)
4137 /* Calculate top address, avoiding undefined behaviour of shift
4138 left operator when shift count is equal to size of type
4140 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
4141 addr_mask
= (addr_mask
<< 1) + 1;
4144 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4146 if ((s
->flags
& SEC_ALLOC
) != 0)
4150 /* A wrapping section potentially clashes with header. */
4151 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
4152 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
4155 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
4158 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
4160 /* Build the mapping. */
4165 /* If we have a .interp section, then create a PT_PHDR segment for
4166 the program headers and a PT_INTERP segment for the .interp
4168 s
= bfd_get_section_by_name (abfd
, ".interp");
4169 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4171 amt
= sizeof (struct elf_segment_map
);
4172 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4176 m
->p_type
= PT_PHDR
;
4177 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
4178 m
->p_flags
= PF_R
| PF_X
;
4179 m
->p_flags_valid
= 1;
4180 m
->includes_phdrs
= 1;
4185 amt
= sizeof (struct elf_segment_map
);
4186 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4190 m
->p_type
= PT_INTERP
;
4198 /* Look through the sections. We put sections in the same program
4199 segment when the start of the second section can be placed within
4200 a few bytes of the end of the first section. */
4204 maxpagesize
= bed
->maxpagesize
;
4205 /* PR 17512: file: c8455299.
4206 Avoid divide-by-zero errors later on.
4207 FIXME: Should we abort if the maxpagesize is zero ? */
4208 if (maxpagesize
== 0)
4211 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
4213 && (dynsec
->flags
& SEC_LOAD
) == 0)
4216 /* Deal with -Ttext or something similar such that the first section
4217 is not adjacent to the program headers. This is an
4218 approximation, since at this point we don't know exactly how many
4219 program headers we will need. */
4222 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
4224 if (phdr_size
== (bfd_size_type
) -1)
4225 phdr_size
= get_program_header_size (abfd
, info
);
4226 phdr_size
+= bed
->s
->sizeof_ehdr
;
4227 if ((abfd
->flags
& D_PAGED
) == 0
4228 || (sections
[0]->lma
& addr_mask
) < phdr_size
4229 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
4230 < phdr_size
% maxpagesize
)
4231 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
4232 phdr_in_segment
= FALSE
;
4235 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
4238 bfd_boolean new_segment
;
4242 /* See if this section and the last one will fit in the same
4245 if (last_hdr
== NULL
)
4247 /* If we don't have a segment yet, then we don't need a new
4248 one (we build the last one after this loop). */
4249 new_segment
= FALSE
;
4251 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
4253 /* If this section has a different relation between the
4254 virtual address and the load address, then we need a new
4258 else if (hdr
->lma
< last_hdr
->lma
+ last_size
4259 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
4261 /* If this section has a load address that makes it overlap
4262 the previous section, then we need a new segment. */
4265 /* In the next test we have to be careful when last_hdr->lma is close
4266 to the end of the address space. If the aligned address wraps
4267 around to the start of the address space, then there are no more
4268 pages left in memory and it is OK to assume that the current
4269 section can be included in the current segment. */
4270 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4272 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
4275 /* If putting this section in this segment would force us to
4276 skip a page in the segment, then we need a new segment. */
4279 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
4280 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0
4281 && ((abfd
->flags
& D_PAGED
) == 0
4282 || (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4283 != (hdr
->lma
& -maxpagesize
))))
4285 /* We don't want to put a loaded section after a
4286 nonloaded (ie. bss style) section in the same segment
4287 as that will force the non-loaded section to be loaded.
4288 Consider .tbss sections as loaded for this purpose.
4289 However, like the writable/non-writable case below,
4290 if they are on the same page then they must be put
4291 in the same segment. */
4294 else if ((abfd
->flags
& D_PAGED
) == 0)
4296 /* If the file is not demand paged, which means that we
4297 don't require the sections to be correctly aligned in the
4298 file, then there is no other reason for a new segment. */
4299 new_segment
= FALSE
;
4302 && (hdr
->flags
& SEC_READONLY
) == 0
4303 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
4304 != (hdr
->lma
& -maxpagesize
)))
4306 /* We don't want to put a writable section in a read only
4307 segment, unless they are on the same page in memory
4308 anyhow. We already know that the last section does not
4309 bring us past the current section on the page, so the
4310 only case in which the new section is not on the same
4311 page as the previous section is when the previous section
4312 ends precisely on a page boundary. */
4317 /* Otherwise, we can use the same segment. */
4318 new_segment
= FALSE
;
4321 /* Allow interested parties a chance to override our decision. */
4322 if (last_hdr
!= NULL
4324 && info
->callbacks
->override_segment_assignment
!= NULL
)
4326 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
4332 if ((hdr
->flags
& SEC_READONLY
) == 0)
4335 /* .tbss sections effectively have zero size. */
4336 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4337 != SEC_THREAD_LOCAL
)
4338 last_size
= hdr
->size
;
4344 /* We need a new program segment. We must create a new program
4345 header holding all the sections from phdr_index until hdr. */
4347 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4354 if ((hdr
->flags
& SEC_READONLY
) == 0)
4360 /* .tbss sections effectively have zero size. */
4361 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4362 last_size
= hdr
->size
;
4366 phdr_in_segment
= FALSE
;
4369 /* Create a final PT_LOAD program segment, but not if it's just
4371 if (last_hdr
!= NULL
4372 && (i
- phdr_index
!= 1
4373 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4374 != SEC_THREAD_LOCAL
)))
4376 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4384 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4387 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4394 /* For each batch of consecutive loadable .note sections,
4395 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4396 because if we link together nonloadable .note sections and
4397 loadable .note sections, we will generate two .note sections
4398 in the output file. FIXME: Using names for section types is
4400 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4402 if ((s
->flags
& SEC_LOAD
) != 0
4403 && CONST_STRNEQ (s
->name
, ".note"))
4408 amt
= sizeof (struct elf_segment_map
);
4409 if (s
->alignment_power
== 2)
4410 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4412 if (s2
->next
->alignment_power
== 2
4413 && (s2
->next
->flags
& SEC_LOAD
) != 0
4414 && CONST_STRNEQ (s2
->next
->name
, ".note")
4415 && align_power (s2
->lma
+ s2
->size
, 2)
4421 amt
+= (count
- 1) * sizeof (asection
*);
4422 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4426 m
->p_type
= PT_NOTE
;
4430 m
->sections
[m
->count
- count
--] = s
;
4431 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4434 m
->sections
[m
->count
- 1] = s
;
4435 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4439 if (s
->flags
& SEC_THREAD_LOCAL
)
4447 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4450 amt
= sizeof (struct elf_segment_map
);
4451 amt
+= (tls_count
- 1) * sizeof (asection
*);
4452 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4457 m
->count
= tls_count
;
4458 /* Mandated PF_R. */
4460 m
->p_flags_valid
= 1;
4462 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4464 if ((s
->flags
& SEC_THREAD_LOCAL
) == 0)
4467 (_("%B: TLS sections are not adjacent:"), abfd
);
4470 while (i
< (unsigned int) tls_count
)
4472 if ((s
->flags
& SEC_THREAD_LOCAL
) != 0)
4474 _bfd_error_handler (_(" TLS: %A"), s
);
4478 _bfd_error_handler (_(" non-TLS: %A"), s
);
4481 bfd_set_error (bfd_error_bad_value
);
4492 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4494 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4495 if (eh_frame_hdr
!= NULL
4496 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4498 amt
= sizeof (struct elf_segment_map
);
4499 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4503 m
->p_type
= PT_GNU_EH_FRAME
;
4505 m
->sections
[0] = eh_frame_hdr
->output_section
;
4511 if (elf_stack_flags (abfd
))
4513 amt
= sizeof (struct elf_segment_map
);
4514 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4518 m
->p_type
= PT_GNU_STACK
;
4519 m
->p_flags
= elf_stack_flags (abfd
);
4520 m
->p_align
= bed
->stack_align
;
4521 m
->p_flags_valid
= 1;
4522 m
->p_align_valid
= m
->p_align
!= 0;
4523 if (info
->stacksize
> 0)
4525 m
->p_size
= info
->stacksize
;
4526 m
->p_size_valid
= 1;
4533 if (info
!= NULL
&& info
->relro
)
4535 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4537 if (m
->p_type
== PT_LOAD
4539 && m
->sections
[0]->vma
>= info
->relro_start
4540 && m
->sections
[0]->vma
< info
->relro_end
)
4543 while (--i
!= (unsigned) -1)
4544 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4545 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4548 if (i
!= (unsigned) -1)
4553 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4556 amt
= sizeof (struct elf_segment_map
);
4557 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4561 m
->p_type
= PT_GNU_RELRO
;
4563 m
->p_flags_valid
= 1;
4571 elf_seg_map (abfd
) = mfirst
;
4574 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4577 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4579 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4584 if (sections
!= NULL
)
4589 /* Sort sections by address. */
4592 elf_sort_sections (const void *arg1
, const void *arg2
)
4594 const asection
*sec1
= *(const asection
**) arg1
;
4595 const asection
*sec2
= *(const asection
**) arg2
;
4596 bfd_size_type size1
, size2
;
4598 /* Sort by LMA first, since this is the address used to
4599 place the section into a segment. */
4600 if (sec1
->lma
< sec2
->lma
)
4602 else if (sec1
->lma
> sec2
->lma
)
4605 /* Then sort by VMA. Normally the LMA and the VMA will be
4606 the same, and this will do nothing. */
4607 if (sec1
->vma
< sec2
->vma
)
4609 else if (sec1
->vma
> sec2
->vma
)
4612 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4614 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4620 /* If the indicies are the same, do not return 0
4621 here, but continue to try the next comparison. */
4622 if (sec1
->target_index
- sec2
->target_index
!= 0)
4623 return sec1
->target_index
- sec2
->target_index
;
4628 else if (TOEND (sec2
))
4633 /* Sort by size, to put zero sized sections
4634 before others at the same address. */
4636 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4637 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4644 return sec1
->target_index
- sec2
->target_index
;
4647 /* Ian Lance Taylor writes:
4649 We shouldn't be using % with a negative signed number. That's just
4650 not good. We have to make sure either that the number is not
4651 negative, or that the number has an unsigned type. When the types
4652 are all the same size they wind up as unsigned. When file_ptr is a
4653 larger signed type, the arithmetic winds up as signed long long,
4656 What we're trying to say here is something like ``increase OFF by
4657 the least amount that will cause it to be equal to the VMA modulo
4659 /* In other words, something like:
4661 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4662 off_offset = off % bed->maxpagesize;
4663 if (vma_offset < off_offset)
4664 adjustment = vma_offset + bed->maxpagesize - off_offset;
4666 adjustment = vma_offset - off_offset;
4668 which can can be collapsed into the expression below. */
4671 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4673 /* PR binutils/16199: Handle an alignment of zero. */
4674 if (maxpagesize
== 0)
4676 return ((vma
- off
) % maxpagesize
);
4680 print_segment_map (const struct elf_segment_map
*m
)
4683 const char *pt
= get_segment_type (m
->p_type
);
4688 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4689 sprintf (buf
, "LOPROC+%7.7x",
4690 (unsigned int) (m
->p_type
- PT_LOPROC
));
4691 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4692 sprintf (buf
, "LOOS+%7.7x",
4693 (unsigned int) (m
->p_type
- PT_LOOS
));
4695 snprintf (buf
, sizeof (buf
), "%8.8x",
4696 (unsigned int) m
->p_type
);
4700 fprintf (stderr
, "%s:", pt
);
4701 for (j
= 0; j
< m
->count
; j
++)
4702 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4708 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4713 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4715 buf
= bfd_zmalloc (len
);
4718 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4723 /* Assign file positions to the sections based on the mapping from
4724 sections to segments. This function also sets up some fields in
4728 assign_file_positions_for_load_sections (bfd
*abfd
,
4729 struct bfd_link_info
*link_info
)
4731 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4732 struct elf_segment_map
*m
;
4733 Elf_Internal_Phdr
*phdrs
;
4734 Elf_Internal_Phdr
*p
;
4736 bfd_size_type maxpagesize
;
4739 bfd_vma header_pad
= 0;
4741 if (link_info
== NULL
4742 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4746 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4750 header_pad
= m
->header_size
;
4755 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4756 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4760 /* PR binutils/12467. */
4761 elf_elfheader (abfd
)->e_phoff
= 0;
4762 elf_elfheader (abfd
)->e_phentsize
= 0;
4765 elf_elfheader (abfd
)->e_phnum
= alloc
;
4767 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4768 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4770 BFD_ASSERT (elf_program_header_size (abfd
)
4771 >= alloc
* bed
->s
->sizeof_phdr
);
4775 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4779 /* We're writing the size in elf_program_header_size (abfd),
4780 see assign_file_positions_except_relocs, so make sure we have
4781 that amount allocated, with trailing space cleared.
4782 The variable alloc contains the computed need, while
4783 elf_program_header_size (abfd) contains the size used for the
4785 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4786 where the layout is forced to according to a larger size in the
4787 last iterations for the testcase ld-elf/header. */
4788 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4790 phdrs
= (Elf_Internal_Phdr
*)
4792 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4793 sizeof (Elf_Internal_Phdr
));
4794 elf_tdata (abfd
)->phdr
= phdrs
;
4799 if ((abfd
->flags
& D_PAGED
) != 0)
4800 maxpagesize
= bed
->maxpagesize
;
4802 off
= bed
->s
->sizeof_ehdr
;
4803 off
+= alloc
* bed
->s
->sizeof_phdr
;
4804 if (header_pad
< (bfd_vma
) off
)
4810 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4812 m
= m
->next
, p
++, j
++)
4816 bfd_boolean no_contents
;
4818 /* If elf_segment_map is not from map_sections_to_segments, the
4819 sections may not be correctly ordered. NOTE: sorting should
4820 not be done to the PT_NOTE section of a corefile, which may
4821 contain several pseudo-sections artificially created by bfd.
4822 Sorting these pseudo-sections breaks things badly. */
4824 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4825 && m
->p_type
== PT_NOTE
))
4826 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4829 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4830 number of sections with contents contributing to both p_filesz
4831 and p_memsz, followed by a number of sections with no contents
4832 that just contribute to p_memsz. In this loop, OFF tracks next
4833 available file offset for PT_LOAD and PT_NOTE segments. */
4834 p
->p_type
= m
->p_type
;
4835 p
->p_flags
= m
->p_flags
;
4840 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4842 if (m
->p_paddr_valid
)
4843 p
->p_paddr
= m
->p_paddr
;
4844 else if (m
->count
== 0)
4847 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4849 if (p
->p_type
== PT_LOAD
4850 && (abfd
->flags
& D_PAGED
) != 0)
4852 /* p_align in demand paged PT_LOAD segments effectively stores
4853 the maximum page size. When copying an executable with
4854 objcopy, we set m->p_align from the input file. Use this
4855 value for maxpagesize rather than bed->maxpagesize, which
4856 may be different. Note that we use maxpagesize for PT_TLS
4857 segment alignment later in this function, so we are relying
4858 on at least one PT_LOAD segment appearing before a PT_TLS
4860 if (m
->p_align_valid
)
4861 maxpagesize
= m
->p_align
;
4863 p
->p_align
= maxpagesize
;
4865 else if (m
->p_align_valid
)
4866 p
->p_align
= m
->p_align
;
4867 else if (m
->count
== 0)
4868 p
->p_align
= 1 << bed
->s
->log_file_align
;
4872 no_contents
= FALSE
;
4874 if (p
->p_type
== PT_LOAD
4877 bfd_size_type align
;
4878 unsigned int align_power
= 0;
4880 if (m
->p_align_valid
)
4884 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4886 unsigned int secalign
;
4888 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4889 if (secalign
> align_power
)
4890 align_power
= secalign
;
4892 align
= (bfd_size_type
) 1 << align_power
;
4893 if (align
< maxpagesize
)
4894 align
= maxpagesize
;
4897 for (i
= 0; i
< m
->count
; i
++)
4898 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4899 /* If we aren't making room for this section, then
4900 it must be SHT_NOBITS regardless of what we've
4901 set via struct bfd_elf_special_section. */
4902 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4904 /* Find out whether this segment contains any loadable
4907 for (i
= 0; i
< m
->count
; i
++)
4908 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4910 no_contents
= FALSE
;
4914 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4918 /* We shouldn't need to align the segment on disk since
4919 the segment doesn't need file space, but the gABI
4920 arguably requires the alignment and glibc ld.so
4921 checks it. So to comply with the alignment
4922 requirement but not waste file space, we adjust
4923 p_offset for just this segment. (OFF_ADJUST is
4924 subtracted from OFF later.) This may put p_offset
4925 past the end of file, but that shouldn't matter. */
4930 /* Make sure the .dynamic section is the first section in the
4931 PT_DYNAMIC segment. */
4932 else if (p
->p_type
== PT_DYNAMIC
4934 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4937 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4939 bfd_set_error (bfd_error_bad_value
);
4942 /* Set the note section type to SHT_NOTE. */
4943 else if (p
->p_type
== PT_NOTE
)
4944 for (i
= 0; i
< m
->count
; i
++)
4945 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4951 if (m
->includes_filehdr
)
4953 if (!m
->p_flags_valid
)
4955 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4956 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4959 if (p
->p_vaddr
< (bfd_vma
) off
)
4961 (*_bfd_error_handler
)
4962 (_("%B: Not enough room for program headers, try linking with -N"),
4964 bfd_set_error (bfd_error_bad_value
);
4969 if (!m
->p_paddr_valid
)
4974 if (m
->includes_phdrs
)
4976 if (!m
->p_flags_valid
)
4979 if (!m
->includes_filehdr
)
4981 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4985 p
->p_vaddr
-= off
- p
->p_offset
;
4986 if (!m
->p_paddr_valid
)
4987 p
->p_paddr
-= off
- p
->p_offset
;
4991 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4992 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4995 p
->p_filesz
+= header_pad
;
4996 p
->p_memsz
+= header_pad
;
5000 if (p
->p_type
== PT_LOAD
5001 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
5003 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
5009 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
5011 p
->p_filesz
+= adjust
;
5012 p
->p_memsz
+= adjust
;
5016 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5017 maps. Set filepos for sections in PT_LOAD segments, and in
5018 core files, for sections in PT_NOTE segments.
5019 assign_file_positions_for_non_load_sections will set filepos
5020 for other sections and update p_filesz for other segments. */
5021 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
5024 bfd_size_type align
;
5025 Elf_Internal_Shdr
*this_hdr
;
5028 this_hdr
= &elf_section_data (sec
)->this_hdr
;
5029 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
5031 if ((p
->p_type
== PT_LOAD
5032 || p
->p_type
== PT_TLS
)
5033 && (this_hdr
->sh_type
!= SHT_NOBITS
5034 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
5035 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
5036 || p
->p_type
== PT_TLS
))))
5038 bfd_vma p_start
= p
->p_paddr
;
5039 bfd_vma p_end
= p_start
+ p
->p_memsz
;
5040 bfd_vma s_start
= sec
->lma
;
5041 bfd_vma adjust
= s_start
- p_end
;
5045 || p_end
< p_start
))
5047 (*_bfd_error_handler
)
5048 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
5049 (unsigned long) s_start
, (unsigned long) p_end
);
5053 p
->p_memsz
+= adjust
;
5055 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5057 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
5059 /* We have a PROGBITS section following NOBITS ones.
5060 Allocate file space for the NOBITS section(s) and
5062 adjust
= p
->p_memsz
- p
->p_filesz
;
5063 if (!write_zeros (abfd
, off
, adjust
))
5067 p
->p_filesz
+= adjust
;
5071 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
5073 /* The section at i == 0 is the one that actually contains
5077 this_hdr
->sh_offset
= sec
->filepos
= off
;
5078 off
+= this_hdr
->sh_size
;
5079 p
->p_filesz
= this_hdr
->sh_size
;
5085 /* The rest are fake sections that shouldn't be written. */
5094 if (p
->p_type
== PT_LOAD
)
5096 this_hdr
->sh_offset
= sec
->filepos
= off
;
5097 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5098 off
+= this_hdr
->sh_size
;
5100 else if (this_hdr
->sh_type
== SHT_NOBITS
5101 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
5102 && this_hdr
->sh_offset
== 0)
5104 /* This is a .tbss section that didn't get a PT_LOAD.
5105 (See _bfd_elf_map_sections_to_segments "Create a
5106 final PT_LOAD".) Set sh_offset to the value it
5107 would have if we had created a zero p_filesz and
5108 p_memsz PT_LOAD header for the section. This
5109 also makes the PT_TLS header have the same
5111 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
5113 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
5116 if (this_hdr
->sh_type
!= SHT_NOBITS
)
5118 p
->p_filesz
+= this_hdr
->sh_size
;
5119 /* A load section without SHF_ALLOC is something like
5120 a note section in a PT_NOTE segment. These take
5121 file space but are not loaded into memory. */
5122 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5123 p
->p_memsz
+= this_hdr
->sh_size
;
5125 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
5127 if (p
->p_type
== PT_TLS
)
5128 p
->p_memsz
+= this_hdr
->sh_size
;
5130 /* .tbss is special. It doesn't contribute to p_memsz of
5132 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
5133 p
->p_memsz
+= this_hdr
->sh_size
;
5136 if (align
> p
->p_align
5137 && !m
->p_align_valid
5138 && (p
->p_type
!= PT_LOAD
5139 || (abfd
->flags
& D_PAGED
) == 0))
5143 if (!m
->p_flags_valid
)
5146 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
5148 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
5155 /* Check that all sections are in a PT_LOAD segment.
5156 Don't check funky gdb generated core files. */
5157 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
5159 bfd_boolean check_vma
= TRUE
;
5161 for (i
= 1; i
< m
->count
; i
++)
5162 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
5163 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
5164 ->this_hdr
), p
) != 0
5165 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
5166 ->this_hdr
), p
) != 0)
5168 /* Looks like we have overlays packed into the segment. */
5173 for (i
= 0; i
< m
->count
; i
++)
5175 Elf_Internal_Shdr
*this_hdr
;
5178 sec
= m
->sections
[i
];
5179 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
5180 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
5181 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
5183 (*_bfd_error_handler
)
5184 (_("%B: section `%A' can't be allocated in segment %d"),
5186 print_segment_map (m
);
5192 elf_next_file_pos (abfd
) = off
;
5196 /* Assign file positions for the other sections. */
5199 assign_file_positions_for_non_load_sections (bfd
*abfd
,
5200 struct bfd_link_info
*link_info
)
5202 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5203 Elf_Internal_Shdr
**i_shdrpp
;
5204 Elf_Internal_Shdr
**hdrpp
, **end_hdrpp
;
5205 Elf_Internal_Phdr
*phdrs
;
5206 Elf_Internal_Phdr
*p
;
5207 struct elf_segment_map
*m
;
5208 struct elf_segment_map
*hdrs_segment
;
5209 bfd_vma filehdr_vaddr
, filehdr_paddr
;
5210 bfd_vma phdrs_vaddr
, phdrs_paddr
;
5214 i_shdrpp
= elf_elfsections (abfd
);
5215 end_hdrpp
= i_shdrpp
+ elf_numsections (abfd
);
5216 off
= elf_next_file_pos (abfd
);
5217 for (hdrpp
= i_shdrpp
+ 1; hdrpp
< end_hdrpp
; hdrpp
++)
5219 Elf_Internal_Shdr
*hdr
;
5222 if (hdr
->bfd_section
!= NULL
5223 && (hdr
->bfd_section
->filepos
!= 0
5224 || (hdr
->sh_type
== SHT_NOBITS
5225 && hdr
->contents
== NULL
)))
5226 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
5227 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
5229 if (hdr
->sh_size
!= 0)
5230 (*_bfd_error_handler
)
5231 (_("%B: warning: allocated section `%s' not in segment"),
5233 (hdr
->bfd_section
== NULL
5235 : hdr
->bfd_section
->name
));
5236 /* We don't need to page align empty sections. */
5237 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
5238 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5241 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
5243 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
5246 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5247 && hdr
->bfd_section
== NULL
)
5248 || (hdr
->bfd_section
!= NULL
5249 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5250 /* Compress DWARF debug sections. */
5251 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
5252 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
5253 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)]
5254 || hdr
== i_shdrpp
[elf_shstrtab_sec (abfd
)])
5255 hdr
->sh_offset
= -1;
5257 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5260 /* Now that we have set the section file positions, we can set up
5261 the file positions for the non PT_LOAD segments. */
5265 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
5267 hdrs_segment
= NULL
;
5268 phdrs
= elf_tdata (abfd
)->phdr
;
5269 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5272 if (p
->p_type
!= PT_LOAD
)
5275 if (m
->includes_filehdr
)
5277 filehdr_vaddr
= p
->p_vaddr
;
5278 filehdr_paddr
= p
->p_paddr
;
5280 if (m
->includes_phdrs
)
5282 phdrs_vaddr
= p
->p_vaddr
;
5283 phdrs_paddr
= p
->p_paddr
;
5284 if (m
->includes_filehdr
)
5287 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
5288 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
5293 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
5295 /* There is a segment that contains both the file headers and the
5296 program headers, so provide a symbol __ehdr_start pointing there.
5297 A program can use this to examine itself robustly. */
5299 struct elf_link_hash_entry
*hash
5300 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
5301 FALSE
, FALSE
, TRUE
);
5302 /* If the symbol was referenced and not defined, define it. */
5304 && (hash
->root
.type
== bfd_link_hash_new
5305 || hash
->root
.type
== bfd_link_hash_undefined
5306 || hash
->root
.type
== bfd_link_hash_undefweak
5307 || hash
->root
.type
== bfd_link_hash_common
))
5310 if (hdrs_segment
->count
!= 0)
5311 /* The segment contains sections, so use the first one. */
5312 s
= hdrs_segment
->sections
[0];
5314 /* Use the first (i.e. lowest-addressed) section in any segment. */
5315 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5324 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
5325 hash
->root
.u
.def
.section
= s
;
5329 hash
->root
.u
.def
.value
= filehdr_vaddr
;
5330 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
5333 hash
->root
.type
= bfd_link_hash_defined
;
5334 hash
->def_regular
= 1;
5339 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
5341 if (p
->p_type
== PT_GNU_RELRO
)
5343 const Elf_Internal_Phdr
*lp
;
5344 struct elf_segment_map
*lm
;
5346 if (link_info
!= NULL
)
5348 /* During linking the range of the RELRO segment is passed
5350 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
5352 lm
= lm
->next
, lp
++)
5354 if (lp
->p_type
== PT_LOAD
5355 && lp
->p_vaddr
< link_info
->relro_end
5357 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5361 BFD_ASSERT (lm
!= NULL
);
5365 /* Otherwise we are copying an executable or shared
5366 library, but we need to use the same linker logic. */
5367 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5369 if (lp
->p_type
== PT_LOAD
5370 && lp
->p_paddr
== p
->p_paddr
)
5375 if (lp
< phdrs
+ count
)
5377 p
->p_vaddr
= lp
->p_vaddr
;
5378 p
->p_paddr
= lp
->p_paddr
;
5379 p
->p_offset
= lp
->p_offset
;
5380 if (link_info
!= NULL
)
5381 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5382 else if (m
->p_size_valid
)
5383 p
->p_filesz
= m
->p_size
;
5386 p
->p_memsz
= p
->p_filesz
;
5387 /* Preserve the alignment and flags if they are valid. The
5388 gold linker generates RW/4 for the PT_GNU_RELRO section.
5389 It is better for objcopy/strip to honor these attributes
5390 otherwise gdb will choke when using separate debug files.
5392 if (!m
->p_align_valid
)
5394 if (!m
->p_flags_valid
)
5395 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5399 memset (p
, 0, sizeof *p
);
5400 p
->p_type
= PT_NULL
;
5403 else if (p
->p_type
== PT_GNU_STACK
)
5405 if (m
->p_size_valid
)
5406 p
->p_memsz
= m
->p_size
;
5408 else if (m
->count
!= 0)
5411 if (p
->p_type
!= PT_LOAD
5412 && (p
->p_type
!= PT_NOTE
5413 || bfd_get_format (abfd
) != bfd_core
))
5415 if (m
->includes_filehdr
|| m
->includes_phdrs
)
5417 /* PR 17512: file: 2195325e. */
5418 (*_bfd_error_handler
)
5419 (_("%B: warning: non-load segment includes file header and/or program header"),
5425 p
->p_offset
= m
->sections
[0]->filepos
;
5426 for (i
= m
->count
; i
-- != 0;)
5428 asection
*sect
= m
->sections
[i
];
5429 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5430 if (hdr
->sh_type
!= SHT_NOBITS
)
5432 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5439 else if (m
->includes_filehdr
)
5441 p
->p_vaddr
= filehdr_vaddr
;
5442 if (! m
->p_paddr_valid
)
5443 p
->p_paddr
= filehdr_paddr
;
5445 else if (m
->includes_phdrs
)
5447 p
->p_vaddr
= phdrs_vaddr
;
5448 if (! m
->p_paddr_valid
)
5449 p
->p_paddr
= phdrs_paddr
;
5453 elf_next_file_pos (abfd
) = off
;
5458 /* Work out the file positions of all the sections. This is called by
5459 _bfd_elf_compute_section_file_positions. All the section sizes and
5460 VMAs must be known before this is called.
5462 Reloc sections come in two flavours: Those processed specially as
5463 "side-channel" data attached to a section to which they apply, and
5464 those that bfd doesn't process as relocations. The latter sort are
5465 stored in a normal bfd section by bfd_section_from_shdr. We don't
5466 consider the former sort here, unless they form part of the loadable
5467 image. Reloc sections not assigned here will be handled later by
5468 assign_file_positions_for_relocs.
5470 We also don't set the positions of the .symtab and .strtab here. */
5473 assign_file_positions_except_relocs (bfd
*abfd
,
5474 struct bfd_link_info
*link_info
)
5476 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5477 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5478 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5480 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5481 && bfd_get_format (abfd
) != bfd_core
)
5483 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5484 unsigned int num_sec
= elf_numsections (abfd
);
5485 Elf_Internal_Shdr
**hdrpp
;
5489 /* Start after the ELF header. */
5490 off
= i_ehdrp
->e_ehsize
;
5492 /* We are not creating an executable, which means that we are
5493 not creating a program header, and that the actual order of
5494 the sections in the file is unimportant. */
5495 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5497 Elf_Internal_Shdr
*hdr
;
5500 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5501 && hdr
->bfd_section
== NULL
)
5502 || (hdr
->bfd_section
!= NULL
5503 && (hdr
->bfd_section
->flags
& SEC_ELF_COMPRESS
))
5504 /* Compress DWARF debug sections. */
5505 || i
== elf_onesymtab (abfd
)
5506 || i
== elf_symtab_shndx (abfd
)
5507 || i
== elf_strtab_sec (abfd
)
5508 || i
== elf_shstrtab_sec (abfd
))
5510 hdr
->sh_offset
= -1;
5513 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5516 elf_next_file_pos (abfd
) = off
;
5522 /* Assign file positions for the loaded sections based on the
5523 assignment of sections to segments. */
5524 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5527 /* And for non-load sections. */
5528 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5531 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5533 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5537 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5538 if (link_info
!= NULL
5539 && link_info
->executable
5540 && link_info
->shared
)
5542 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5543 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5544 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5546 /* Find the lowest p_vaddr in PT_LOAD segments. */
5547 bfd_vma p_vaddr
= (bfd_vma
) -1;
5548 for (; segment
< end_segment
; segment
++)
5549 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5550 p_vaddr
= segment
->p_vaddr
;
5552 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5553 segments is non-zero. */
5555 i_ehdrp
->e_type
= ET_EXEC
;
5558 /* Write out the program headers. */
5559 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5560 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5561 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5569 prep_headers (bfd
*abfd
)
5571 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5572 struct elf_strtab_hash
*shstrtab
;
5573 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5575 i_ehdrp
= elf_elfheader (abfd
);
5577 shstrtab
= _bfd_elf_strtab_init ();
5578 if (shstrtab
== NULL
)
5581 elf_shstrtab (abfd
) = shstrtab
;
5583 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5584 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5585 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5586 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5588 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5589 i_ehdrp
->e_ident
[EI_DATA
] =
5590 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5591 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5593 if ((abfd
->flags
& DYNAMIC
) != 0)
5594 i_ehdrp
->e_type
= ET_DYN
;
5595 else if ((abfd
->flags
& EXEC_P
) != 0)
5596 i_ehdrp
->e_type
= ET_EXEC
;
5597 else if (bfd_get_format (abfd
) == bfd_core
)
5598 i_ehdrp
->e_type
= ET_CORE
;
5600 i_ehdrp
->e_type
= ET_REL
;
5602 switch (bfd_get_arch (abfd
))
5604 case bfd_arch_unknown
:
5605 i_ehdrp
->e_machine
= EM_NONE
;
5608 /* There used to be a long list of cases here, each one setting
5609 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5610 in the corresponding bfd definition. To avoid duplication,
5611 the switch was removed. Machines that need special handling
5612 can generally do it in elf_backend_final_write_processing(),
5613 unless they need the information earlier than the final write.
5614 Such need can generally be supplied by replacing the tests for
5615 e_machine with the conditions used to determine it. */
5617 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5620 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5621 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5623 /* No program header, for now. */
5624 i_ehdrp
->e_phoff
= 0;
5625 i_ehdrp
->e_phentsize
= 0;
5626 i_ehdrp
->e_phnum
= 0;
5628 /* Each bfd section is section header entry. */
5629 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5630 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5632 /* If we're building an executable, we'll need a program header table. */
5633 if (abfd
->flags
& EXEC_P
)
5634 /* It all happens later. */
5638 i_ehdrp
->e_phentsize
= 0;
5639 i_ehdrp
->e_phoff
= 0;
5642 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5643 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5644 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5645 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5646 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5647 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5648 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5649 || elf_tdata (abfd
)->strtab_hdr
.sh_name
== (unsigned int) -1
5650 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5656 /* Assign file positions for all the reloc sections which are not part
5657 of the loadable file image, and the file position of section headers. */
5660 _bfd_elf_assign_file_positions_for_non_load (bfd
*abfd
)
5663 Elf_Internal_Shdr
**shdrpp
, **end_shdrpp
;
5664 Elf_Internal_Shdr
*shdrp
;
5665 Elf_Internal_Ehdr
*i_ehdrp
;
5666 const struct elf_backend_data
*bed
;
5668 off
= elf_next_file_pos (abfd
);
5670 shdrpp
= elf_elfsections (abfd
);
5671 end_shdrpp
= shdrpp
+ elf_numsections (abfd
);
5672 for (shdrpp
++; shdrpp
< end_shdrpp
; shdrpp
++)
5675 if (shdrp
->sh_offset
== -1)
5677 asection
*sec
= shdrp
->bfd_section
;
5678 bfd_boolean is_rel
= (shdrp
->sh_type
== SHT_REL
5679 || shdrp
->sh_type
== SHT_RELA
);
5681 || (sec
!= NULL
&& (sec
->flags
& SEC_ELF_COMPRESS
)))
5685 const char *name
= sec
->name
;
5686 struct bfd_elf_section_data
*d
;
5688 /* Compress DWARF debug sections. */
5689 if (!bfd_compress_section (abfd
, sec
,
5693 if (sec
->compress_status
== COMPRESS_SECTION_DONE
5694 && (abfd
->flags
& BFD_COMPRESS_GABI
) == 0)
5696 /* If section is compressed with zlib-gnu, convert
5697 section name from .debug_* to .zdebug_*. */
5699 = convert_debug_to_zdebug (abfd
, name
);
5700 if (new_name
== NULL
)
5704 /* Add setion name to section name section. */
5705 if (shdrp
->sh_name
!= (unsigned int) -1)
5708 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
5710 d
= elf_section_data (sec
);
5712 /* Add reloc setion name to section name section. */
5714 && !_bfd_elf_set_reloc_sh_name (abfd
,
5719 && !_bfd_elf_set_reloc_sh_name (abfd
,
5724 /* Update section size and contents. */
5725 shdrp
->sh_size
= sec
->size
;
5726 shdrp
->contents
= sec
->contents
;
5727 shdrp
->bfd_section
->contents
= NULL
;
5729 off
= _bfd_elf_assign_file_position_for_section (shdrp
,
5736 /* Place section name section after DWARF debug sections have been
5738 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
5739 shdrp
= &elf_tdata (abfd
)->shstrtab_hdr
;
5740 shdrp
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
5741 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5743 /* Place the section headers. */
5744 i_ehdrp
= elf_elfheader (abfd
);
5745 bed
= get_elf_backend_data (abfd
);
5746 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5747 i_ehdrp
->e_shoff
= off
;
5748 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5749 elf_next_file_pos (abfd
) = off
;
5755 _bfd_elf_write_object_contents (bfd
*abfd
)
5757 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5758 Elf_Internal_Shdr
**i_shdrp
;
5760 unsigned int count
, num_sec
;
5761 struct elf_obj_tdata
*t
;
5763 if (! abfd
->output_has_begun
5764 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5767 i_shdrp
= elf_elfsections (abfd
);
5770 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5774 if (!_bfd_elf_assign_file_positions_for_non_load (abfd
))
5777 /* After writing the headers, we need to write the sections too... */
5778 num_sec
= elf_numsections (abfd
);
5779 for (count
= 1; count
< num_sec
; count
++)
5781 i_shdrp
[count
]->sh_name
5782 = _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
5783 i_shdrp
[count
]->sh_name
);
5784 if (bed
->elf_backend_section_processing
)
5785 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5786 if (i_shdrp
[count
]->contents
)
5788 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5790 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5791 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5796 /* Write out the section header names. */
5797 t
= elf_tdata (abfd
);
5798 if (elf_shstrtab (abfd
) != NULL
5799 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5800 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5803 if (bed
->elf_backend_final_write_processing
)
5804 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5806 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5809 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5810 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5811 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5817 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5819 /* Hopefully this can be done just like an object file. */
5820 return _bfd_elf_write_object_contents (abfd
);
5823 /* Given a section, search the header to find them. */
5826 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5828 const struct elf_backend_data
*bed
;
5829 unsigned int sec_index
;
5831 if (elf_section_data (asect
) != NULL
5832 && elf_section_data (asect
)->this_idx
!= 0)
5833 return elf_section_data (asect
)->this_idx
;
5835 if (bfd_is_abs_section (asect
))
5836 sec_index
= SHN_ABS
;
5837 else if (bfd_is_com_section (asect
))
5838 sec_index
= SHN_COMMON
;
5839 else if (bfd_is_und_section (asect
))
5840 sec_index
= SHN_UNDEF
;
5842 sec_index
= SHN_BAD
;
5844 bed
= get_elf_backend_data (abfd
);
5845 if (bed
->elf_backend_section_from_bfd_section
)
5847 int retval
= sec_index
;
5849 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5853 if (sec_index
== SHN_BAD
)
5854 bfd_set_error (bfd_error_nonrepresentable_section
);
5859 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5863 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5865 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5867 flagword flags
= asym_ptr
->flags
;
5869 /* When gas creates relocations against local labels, it creates its
5870 own symbol for the section, but does put the symbol into the
5871 symbol chain, so udata is 0. When the linker is generating
5872 relocatable output, this section symbol may be for one of the
5873 input sections rather than the output section. */
5874 if (asym_ptr
->udata
.i
== 0
5875 && (flags
& BSF_SECTION_SYM
)
5876 && asym_ptr
->section
)
5881 sec
= asym_ptr
->section
;
5882 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5883 sec
= sec
->output_section
;
5884 if (sec
->owner
== abfd
5885 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5886 && elf_section_syms (abfd
)[indx
] != NULL
)
5887 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5890 idx
= asym_ptr
->udata
.i
;
5894 /* This case can occur when using --strip-symbol on a symbol
5895 which is used in a relocation entry. */
5896 (*_bfd_error_handler
)
5897 (_("%B: symbol `%s' required but not present"),
5898 abfd
, bfd_asymbol_name (asym_ptr
));
5899 bfd_set_error (bfd_error_no_symbols
);
5906 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5907 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5915 /* Rewrite program header information. */
5918 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5920 Elf_Internal_Ehdr
*iehdr
;
5921 struct elf_segment_map
*map
;
5922 struct elf_segment_map
*map_first
;
5923 struct elf_segment_map
**pointer_to_map
;
5924 Elf_Internal_Phdr
*segment
;
5927 unsigned int num_segments
;
5928 bfd_boolean phdr_included
= FALSE
;
5929 bfd_boolean p_paddr_valid
;
5930 bfd_vma maxpagesize
;
5931 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5932 unsigned int phdr_adjust_num
= 0;
5933 const struct elf_backend_data
*bed
;
5935 bed
= get_elf_backend_data (ibfd
);
5936 iehdr
= elf_elfheader (ibfd
);
5939 pointer_to_map
= &map_first
;
5941 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5942 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5944 /* Returns the end address of the segment + 1. */
5945 #define SEGMENT_END(segment, start) \
5946 (start + (segment->p_memsz > segment->p_filesz \
5947 ? segment->p_memsz : segment->p_filesz))
5949 #define SECTION_SIZE(section, segment) \
5950 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5951 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5952 ? section->size : 0)
5954 /* Returns TRUE if the given section is contained within
5955 the given segment. VMA addresses are compared. */
5956 #define IS_CONTAINED_BY_VMA(section, segment) \
5957 (section->vma >= segment->p_vaddr \
5958 && (section->vma + SECTION_SIZE (section, segment) \
5959 <= (SEGMENT_END (segment, segment->p_vaddr))))
5961 /* Returns TRUE if the given section is contained within
5962 the given segment. LMA addresses are compared. */
5963 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5964 (section->lma >= base \
5965 && (section->lma + SECTION_SIZE (section, segment) \
5966 <= SEGMENT_END (segment, base)))
5968 /* Handle PT_NOTE segment. */
5969 #define IS_NOTE(p, s) \
5970 (p->p_type == PT_NOTE \
5971 && elf_section_type (s) == SHT_NOTE \
5972 && (bfd_vma) s->filepos >= p->p_offset \
5973 && ((bfd_vma) s->filepos + s->size \
5974 <= p->p_offset + p->p_filesz))
5976 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5978 #define IS_COREFILE_NOTE(p, s) \
5980 && bfd_get_format (ibfd) == bfd_core \
5984 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5985 linker, which generates a PT_INTERP section with p_vaddr and
5986 p_memsz set to 0. */
5987 #define IS_SOLARIS_PT_INTERP(p, s) \
5989 && p->p_paddr == 0 \
5990 && p->p_memsz == 0 \
5991 && p->p_filesz > 0 \
5992 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5994 && (bfd_vma) s->filepos >= p->p_offset \
5995 && ((bfd_vma) s->filepos + s->size \
5996 <= p->p_offset + p->p_filesz))
5998 /* Decide if the given section should be included in the given segment.
5999 A section will be included if:
6000 1. It is within the address space of the segment -- we use the LMA
6001 if that is set for the segment and the VMA otherwise,
6002 2. It is an allocated section or a NOTE section in a PT_NOTE
6004 3. There is an output section associated with it,
6005 4. The section has not already been allocated to a previous segment.
6006 5. PT_GNU_STACK segments do not include any sections.
6007 6. PT_TLS segment includes only SHF_TLS sections.
6008 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6009 8. PT_DYNAMIC should not contain empty sections at the beginning
6010 (with the possible exception of .dynamic). */
6011 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
6012 ((((segment->p_paddr \
6013 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
6014 : IS_CONTAINED_BY_VMA (section, segment)) \
6015 && (section->flags & SEC_ALLOC) != 0) \
6016 || IS_NOTE (segment, section)) \
6017 && segment->p_type != PT_GNU_STACK \
6018 && (segment->p_type != PT_TLS \
6019 || (section->flags & SEC_THREAD_LOCAL)) \
6020 && (segment->p_type == PT_LOAD \
6021 || segment->p_type == PT_TLS \
6022 || (section->flags & SEC_THREAD_LOCAL) == 0) \
6023 && (segment->p_type != PT_DYNAMIC \
6024 || SECTION_SIZE (section, segment) > 0 \
6025 || (segment->p_paddr \
6026 ? segment->p_paddr != section->lma \
6027 : segment->p_vaddr != section->vma) \
6028 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
6030 && !section->segment_mark)
6032 /* If the output section of a section in the input segment is NULL,
6033 it is removed from the corresponding output segment. */
6034 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
6035 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
6036 && section->output_section != NULL)
6038 /* Returns TRUE iff seg1 starts after the end of seg2. */
6039 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
6040 (seg1->field >= SEGMENT_END (seg2, seg2->field))
6042 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6043 their VMA address ranges and their LMA address ranges overlap.
6044 It is possible to have overlapping VMA ranges without overlapping LMA
6045 ranges. RedBoot images for example can have both .data and .bss mapped
6046 to the same VMA range, but with the .data section mapped to a different
6048 #define SEGMENT_OVERLAPS(seg1, seg2) \
6049 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
6050 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
6051 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
6052 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6054 /* Initialise the segment mark field. */
6055 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
6056 section
->segment_mark
= FALSE
;
6058 /* The Solaris linker creates program headers in which all the
6059 p_paddr fields are zero. When we try to objcopy or strip such a
6060 file, we get confused. Check for this case, and if we find it
6061 don't set the p_paddr_valid fields. */
6062 p_paddr_valid
= FALSE
;
6063 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6066 if (segment
->p_paddr
!= 0)
6068 p_paddr_valid
= TRUE
;
6072 /* Scan through the segments specified in the program header
6073 of the input BFD. For this first scan we look for overlaps
6074 in the loadable segments. These can be created by weird
6075 parameters to objcopy. Also, fix some solaris weirdness. */
6076 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6081 Elf_Internal_Phdr
*segment2
;
6083 if (segment
->p_type
== PT_INTERP
)
6084 for (section
= ibfd
->sections
; section
; section
= section
->next
)
6085 if (IS_SOLARIS_PT_INTERP (segment
, section
))
6087 /* Mininal change so that the normal section to segment
6088 assignment code will work. */
6089 segment
->p_vaddr
= section
->vma
;
6093 if (segment
->p_type
!= PT_LOAD
)
6095 /* Remove PT_GNU_RELRO segment. */
6096 if (segment
->p_type
== PT_GNU_RELRO
)
6097 segment
->p_type
= PT_NULL
;
6101 /* Determine if this segment overlaps any previous segments. */
6102 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
6104 bfd_signed_vma extra_length
;
6106 if (segment2
->p_type
!= PT_LOAD
6107 || !SEGMENT_OVERLAPS (segment
, segment2
))
6110 /* Merge the two segments together. */
6111 if (segment2
->p_vaddr
< segment
->p_vaddr
)
6113 /* Extend SEGMENT2 to include SEGMENT and then delete
6115 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
6116 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
6118 if (extra_length
> 0)
6120 segment2
->p_memsz
+= extra_length
;
6121 segment2
->p_filesz
+= extra_length
;
6124 segment
->p_type
= PT_NULL
;
6126 /* Since we have deleted P we must restart the outer loop. */
6128 segment
= elf_tdata (ibfd
)->phdr
;
6133 /* Extend SEGMENT to include SEGMENT2 and then delete
6135 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
6136 - SEGMENT_END (segment
, segment
->p_vaddr
));
6138 if (extra_length
> 0)
6140 segment
->p_memsz
+= extra_length
;
6141 segment
->p_filesz
+= extra_length
;
6144 segment2
->p_type
= PT_NULL
;
6149 /* The second scan attempts to assign sections to segments. */
6150 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6154 unsigned int section_count
;
6155 asection
**sections
;
6156 asection
*output_section
;
6158 bfd_vma matching_lma
;
6159 bfd_vma suggested_lma
;
6162 asection
*first_section
;
6163 bfd_boolean first_matching_lma
;
6164 bfd_boolean first_suggested_lma
;
6166 if (segment
->p_type
== PT_NULL
)
6169 first_section
= NULL
;
6170 /* Compute how many sections might be placed into this segment. */
6171 for (section
= ibfd
->sections
, section_count
= 0;
6173 section
= section
->next
)
6175 /* Find the first section in the input segment, which may be
6176 removed from the corresponding output segment. */
6177 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
6179 if (first_section
== NULL
)
6180 first_section
= section
;
6181 if (section
->output_section
!= NULL
)
6186 /* Allocate a segment map big enough to contain
6187 all of the sections we have selected. */
6188 amt
= sizeof (struct elf_segment_map
);
6189 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6190 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6194 /* Initialise the fields of the segment map. Default to
6195 using the physical address of the segment in the input BFD. */
6197 map
->p_type
= segment
->p_type
;
6198 map
->p_flags
= segment
->p_flags
;
6199 map
->p_flags_valid
= 1;
6201 /* If the first section in the input segment is removed, there is
6202 no need to preserve segment physical address in the corresponding
6204 if (!first_section
|| first_section
->output_section
!= NULL
)
6206 map
->p_paddr
= segment
->p_paddr
;
6207 map
->p_paddr_valid
= p_paddr_valid
;
6210 /* Determine if this segment contains the ELF file header
6211 and if it contains the program headers themselves. */
6212 map
->includes_filehdr
= (segment
->p_offset
== 0
6213 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6214 map
->includes_phdrs
= 0;
6216 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
6218 map
->includes_phdrs
=
6219 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6220 && (segment
->p_offset
+ segment
->p_filesz
6221 >= ((bfd_vma
) iehdr
->e_phoff
6222 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6224 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6225 phdr_included
= TRUE
;
6228 if (section_count
== 0)
6230 /* Special segments, such as the PT_PHDR segment, may contain
6231 no sections, but ordinary, loadable segments should contain
6232 something. They are allowed by the ELF spec however, so only
6233 a warning is produced. */
6234 if (segment
->p_type
== PT_LOAD
)
6235 (*_bfd_error_handler
) (_("\
6236 %B: warning: Empty loadable segment detected, is this intentional ?"),
6240 *pointer_to_map
= map
;
6241 pointer_to_map
= &map
->next
;
6246 /* Now scan the sections in the input BFD again and attempt
6247 to add their corresponding output sections to the segment map.
6248 The problem here is how to handle an output section which has
6249 been moved (ie had its LMA changed). There are four possibilities:
6251 1. None of the sections have been moved.
6252 In this case we can continue to use the segment LMA from the
6255 2. All of the sections have been moved by the same amount.
6256 In this case we can change the segment's LMA to match the LMA
6257 of the first section.
6259 3. Some of the sections have been moved, others have not.
6260 In this case those sections which have not been moved can be
6261 placed in the current segment which will have to have its size,
6262 and possibly its LMA changed, and a new segment or segments will
6263 have to be created to contain the other sections.
6265 4. The sections have been moved, but not by the same amount.
6266 In this case we can change the segment's LMA to match the LMA
6267 of the first section and we will have to create a new segment
6268 or segments to contain the other sections.
6270 In order to save time, we allocate an array to hold the section
6271 pointers that we are interested in. As these sections get assigned
6272 to a segment, they are removed from this array. */
6274 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
6275 if (sections
== NULL
)
6278 /* Step One: Scan for segment vs section LMA conflicts.
6279 Also add the sections to the section array allocated above.
6280 Also add the sections to the current segment. In the common
6281 case, where the sections have not been moved, this means that
6282 we have completely filled the segment, and there is nothing
6287 first_matching_lma
= TRUE
;
6288 first_suggested_lma
= TRUE
;
6290 for (section
= ibfd
->sections
;
6292 section
= section
->next
)
6293 if (section
== first_section
)
6296 for (j
= 0; section
!= NULL
; section
= section
->next
)
6298 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
6300 output_section
= section
->output_section
;
6302 sections
[j
++] = section
;
6304 /* The Solaris native linker always sets p_paddr to 0.
6305 We try to catch that case here, and set it to the
6306 correct value. Note - some backends require that
6307 p_paddr be left as zero. */
6309 && segment
->p_vaddr
!= 0
6310 && !bed
->want_p_paddr_set_to_zero
6312 && output_section
->lma
!= 0
6313 && output_section
->vma
== (segment
->p_vaddr
6314 + (map
->includes_filehdr
6317 + (map
->includes_phdrs
6319 * iehdr
->e_phentsize
)
6321 map
->p_paddr
= segment
->p_vaddr
;
6323 /* Match up the physical address of the segment with the
6324 LMA address of the output section. */
6325 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6326 || IS_COREFILE_NOTE (segment
, section
)
6327 || (bed
->want_p_paddr_set_to_zero
6328 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
6330 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
6332 matching_lma
= output_section
->lma
;
6333 first_matching_lma
= FALSE
;
6336 /* We assume that if the section fits within the segment
6337 then it does not overlap any other section within that
6339 map
->sections
[isec
++] = output_section
;
6341 else if (first_suggested_lma
)
6343 suggested_lma
= output_section
->lma
;
6344 first_suggested_lma
= FALSE
;
6347 if (j
== section_count
)
6352 BFD_ASSERT (j
== section_count
);
6354 /* Step Two: Adjust the physical address of the current segment,
6356 if (isec
== section_count
)
6358 /* All of the sections fitted within the segment as currently
6359 specified. This is the default case. Add the segment to
6360 the list of built segments and carry on to process the next
6361 program header in the input BFD. */
6362 map
->count
= section_count
;
6363 *pointer_to_map
= map
;
6364 pointer_to_map
= &map
->next
;
6367 && !bed
->want_p_paddr_set_to_zero
6368 && matching_lma
!= map
->p_paddr
6369 && !map
->includes_filehdr
6370 && !map
->includes_phdrs
)
6371 /* There is some padding before the first section in the
6372 segment. So, we must account for that in the output
6374 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
6381 if (!first_matching_lma
)
6383 /* At least one section fits inside the current segment.
6384 Keep it, but modify its physical address to match the
6385 LMA of the first section that fitted. */
6386 map
->p_paddr
= matching_lma
;
6390 /* None of the sections fitted inside the current segment.
6391 Change the current segment's physical address to match
6392 the LMA of the first section. */
6393 map
->p_paddr
= suggested_lma
;
6396 /* Offset the segment physical address from the lma
6397 to allow for space taken up by elf headers. */
6398 if (map
->includes_filehdr
)
6400 if (map
->p_paddr
>= iehdr
->e_ehsize
)
6401 map
->p_paddr
-= iehdr
->e_ehsize
;
6404 map
->includes_filehdr
= FALSE
;
6405 map
->includes_phdrs
= FALSE
;
6409 if (map
->includes_phdrs
)
6411 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
6413 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
6415 /* iehdr->e_phnum is just an estimate of the number
6416 of program headers that we will need. Make a note
6417 here of the number we used and the segment we chose
6418 to hold these headers, so that we can adjust the
6419 offset when we know the correct value. */
6420 phdr_adjust_num
= iehdr
->e_phnum
;
6421 phdr_adjust_seg
= map
;
6424 map
->includes_phdrs
= FALSE
;
6428 /* Step Three: Loop over the sections again, this time assigning
6429 those that fit to the current segment and removing them from the
6430 sections array; but making sure not to leave large gaps. Once all
6431 possible sections have been assigned to the current segment it is
6432 added to the list of built segments and if sections still remain
6433 to be assigned, a new segment is constructed before repeating
6440 first_suggested_lma
= TRUE
;
6442 /* Fill the current segment with sections that fit. */
6443 for (j
= 0; j
< section_count
; j
++)
6445 section
= sections
[j
];
6447 if (section
== NULL
)
6450 output_section
= section
->output_section
;
6452 BFD_ASSERT (output_section
!= NULL
);
6454 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6455 || IS_COREFILE_NOTE (segment
, section
))
6457 if (map
->count
== 0)
6459 /* If the first section in a segment does not start at
6460 the beginning of the segment, then something is
6462 if (output_section
->lma
6464 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6465 + (map
->includes_phdrs
6466 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6474 prev_sec
= map
->sections
[map
->count
- 1];
6476 /* If the gap between the end of the previous section
6477 and the start of this section is more than
6478 maxpagesize then we need to start a new segment. */
6479 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6481 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6482 || (prev_sec
->lma
+ prev_sec
->size
6483 > output_section
->lma
))
6485 if (first_suggested_lma
)
6487 suggested_lma
= output_section
->lma
;
6488 first_suggested_lma
= FALSE
;
6495 map
->sections
[map
->count
++] = output_section
;
6498 section
->segment_mark
= TRUE
;
6500 else if (first_suggested_lma
)
6502 suggested_lma
= output_section
->lma
;
6503 first_suggested_lma
= FALSE
;
6507 BFD_ASSERT (map
->count
> 0);
6509 /* Add the current segment to the list of built segments. */
6510 *pointer_to_map
= map
;
6511 pointer_to_map
= &map
->next
;
6513 if (isec
< section_count
)
6515 /* We still have not allocated all of the sections to
6516 segments. Create a new segment here, initialise it
6517 and carry on looping. */
6518 amt
= sizeof (struct elf_segment_map
);
6519 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6520 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6527 /* Initialise the fields of the segment map. Set the physical
6528 physical address to the LMA of the first section that has
6529 not yet been assigned. */
6531 map
->p_type
= segment
->p_type
;
6532 map
->p_flags
= segment
->p_flags
;
6533 map
->p_flags_valid
= 1;
6534 map
->p_paddr
= suggested_lma
;
6535 map
->p_paddr_valid
= p_paddr_valid
;
6536 map
->includes_filehdr
= 0;
6537 map
->includes_phdrs
= 0;
6540 while (isec
< section_count
);
6545 elf_seg_map (obfd
) = map_first
;
6547 /* If we had to estimate the number of program headers that were
6548 going to be needed, then check our estimate now and adjust
6549 the offset if necessary. */
6550 if (phdr_adjust_seg
!= NULL
)
6554 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6557 if (count
> phdr_adjust_num
)
6558 phdr_adjust_seg
->p_paddr
6559 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6564 #undef IS_CONTAINED_BY_VMA
6565 #undef IS_CONTAINED_BY_LMA
6567 #undef IS_COREFILE_NOTE
6568 #undef IS_SOLARIS_PT_INTERP
6569 #undef IS_SECTION_IN_INPUT_SEGMENT
6570 #undef INCLUDE_SECTION_IN_SEGMENT
6571 #undef SEGMENT_AFTER_SEGMENT
6572 #undef SEGMENT_OVERLAPS
6576 /* Copy ELF program header information. */
6579 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6581 Elf_Internal_Ehdr
*iehdr
;
6582 struct elf_segment_map
*map
;
6583 struct elf_segment_map
*map_first
;
6584 struct elf_segment_map
**pointer_to_map
;
6585 Elf_Internal_Phdr
*segment
;
6587 unsigned int num_segments
;
6588 bfd_boolean phdr_included
= FALSE
;
6589 bfd_boolean p_paddr_valid
;
6591 iehdr
= elf_elfheader (ibfd
);
6594 pointer_to_map
= &map_first
;
6596 /* If all the segment p_paddr fields are zero, don't set
6597 map->p_paddr_valid. */
6598 p_paddr_valid
= FALSE
;
6599 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6600 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6603 if (segment
->p_paddr
!= 0)
6605 p_paddr_valid
= TRUE
;
6609 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6614 unsigned int section_count
;
6616 Elf_Internal_Shdr
*this_hdr
;
6617 asection
*first_section
= NULL
;
6618 asection
*lowest_section
;
6620 /* Compute how many sections are in this segment. */
6621 for (section
= ibfd
->sections
, section_count
= 0;
6623 section
= section
->next
)
6625 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6626 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6628 if (first_section
== NULL
)
6629 first_section
= section
;
6634 /* Allocate a segment map big enough to contain
6635 all of the sections we have selected. */
6636 amt
= sizeof (struct elf_segment_map
);
6637 if (section_count
!= 0)
6638 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6639 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6643 /* Initialize the fields of the output segment map with the
6646 map
->p_type
= segment
->p_type
;
6647 map
->p_flags
= segment
->p_flags
;
6648 map
->p_flags_valid
= 1;
6649 map
->p_paddr
= segment
->p_paddr
;
6650 map
->p_paddr_valid
= p_paddr_valid
;
6651 map
->p_align
= segment
->p_align
;
6652 map
->p_align_valid
= 1;
6653 map
->p_vaddr_offset
= 0;
6655 if (map
->p_type
== PT_GNU_RELRO
6656 || map
->p_type
== PT_GNU_STACK
)
6658 /* The PT_GNU_RELRO segment may contain the first a few
6659 bytes in the .got.plt section even if the whole .got.plt
6660 section isn't in the PT_GNU_RELRO segment. We won't
6661 change the size of the PT_GNU_RELRO segment.
6662 Similarly, PT_GNU_STACK size is significant on uclinux
6664 map
->p_size
= segment
->p_memsz
;
6665 map
->p_size_valid
= 1;
6668 /* Determine if this segment contains the ELF file header
6669 and if it contains the program headers themselves. */
6670 map
->includes_filehdr
= (segment
->p_offset
== 0
6671 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6673 map
->includes_phdrs
= 0;
6674 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6676 map
->includes_phdrs
=
6677 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6678 && (segment
->p_offset
+ segment
->p_filesz
6679 >= ((bfd_vma
) iehdr
->e_phoff
6680 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6682 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6683 phdr_included
= TRUE
;
6686 lowest_section
= NULL
;
6687 if (section_count
!= 0)
6689 unsigned int isec
= 0;
6691 for (section
= first_section
;
6693 section
= section
->next
)
6695 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6696 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6698 map
->sections
[isec
++] = section
->output_section
;
6699 if ((section
->flags
& SEC_ALLOC
) != 0)
6703 if (lowest_section
== NULL
6704 || section
->lma
< lowest_section
->lma
)
6705 lowest_section
= section
;
6707 /* Section lmas are set up from PT_LOAD header
6708 p_paddr in _bfd_elf_make_section_from_shdr.
6709 If this header has a p_paddr that disagrees
6710 with the section lma, flag the p_paddr as
6712 if ((section
->flags
& SEC_LOAD
) != 0)
6713 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6715 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6716 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6717 map
->p_paddr_valid
= FALSE
;
6719 if (isec
== section_count
)
6725 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6726 /* We need to keep the space used by the headers fixed. */
6727 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6729 if (!map
->includes_phdrs
6730 && !map
->includes_filehdr
6731 && map
->p_paddr_valid
)
6732 /* There is some other padding before the first section. */
6733 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6734 - segment
->p_paddr
);
6736 map
->count
= section_count
;
6737 *pointer_to_map
= map
;
6738 pointer_to_map
= &map
->next
;
6741 elf_seg_map (obfd
) = map_first
;
6745 /* Copy private BFD data. This copies or rewrites ELF program header
6749 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6751 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6752 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6755 if (elf_tdata (ibfd
)->phdr
== NULL
)
6758 if (ibfd
->xvec
== obfd
->xvec
)
6760 /* Check to see if any sections in the input BFD
6761 covered by ELF program header have changed. */
6762 Elf_Internal_Phdr
*segment
;
6763 asection
*section
, *osec
;
6764 unsigned int i
, num_segments
;
6765 Elf_Internal_Shdr
*this_hdr
;
6766 const struct elf_backend_data
*bed
;
6768 bed
= get_elf_backend_data (ibfd
);
6770 /* Regenerate the segment map if p_paddr is set to 0. */
6771 if (bed
->want_p_paddr_set_to_zero
)
6774 /* Initialize the segment mark field. */
6775 for (section
= obfd
->sections
; section
!= NULL
;
6776 section
= section
->next
)
6777 section
->segment_mark
= FALSE
;
6779 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6780 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6784 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6785 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6786 which severly confuses things, so always regenerate the segment
6787 map in this case. */
6788 if (segment
->p_paddr
== 0
6789 && segment
->p_memsz
== 0
6790 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6793 for (section
= ibfd
->sections
;
6794 section
!= NULL
; section
= section
->next
)
6796 /* We mark the output section so that we know it comes
6797 from the input BFD. */
6798 osec
= section
->output_section
;
6800 osec
->segment_mark
= TRUE
;
6802 /* Check if this section is covered by the segment. */
6803 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6804 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6806 /* FIXME: Check if its output section is changed or
6807 removed. What else do we need to check? */
6809 || section
->flags
!= osec
->flags
6810 || section
->lma
!= osec
->lma
6811 || section
->vma
!= osec
->vma
6812 || section
->size
!= osec
->size
6813 || section
->rawsize
!= osec
->rawsize
6814 || section
->alignment_power
!= osec
->alignment_power
)
6820 /* Check to see if any output section do not come from the
6822 for (section
= obfd
->sections
; section
!= NULL
;
6823 section
= section
->next
)
6825 if (section
->segment_mark
== FALSE
)
6828 section
->segment_mark
= FALSE
;
6831 return copy_elf_program_header (ibfd
, obfd
);
6835 if (ibfd
->xvec
== obfd
->xvec
)
6837 /* When rewriting program header, set the output maxpagesize to
6838 the maximum alignment of input PT_LOAD segments. */
6839 Elf_Internal_Phdr
*segment
;
6841 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6842 bfd_vma maxpagesize
= 0;
6844 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6847 if (segment
->p_type
== PT_LOAD
6848 && maxpagesize
< segment
->p_align
)
6850 /* PR 17512: file: f17299af. */
6851 if (segment
->p_align
> (bfd_vma
) 1 << ((sizeof (bfd_vma
) * 8) - 2))
6852 (*_bfd_error_handler
) (_("\
6853 %B: warning: segment alignment of 0x%llx is too large"),
6854 ibfd
, (long long) segment
->p_align
);
6856 maxpagesize
= segment
->p_align
;
6859 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6860 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6863 return rewrite_elf_program_header (ibfd
, obfd
);
6866 /* Initialize private output section information from input section. */
6869 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6873 struct bfd_link_info
*link_info
)
6876 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6877 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6879 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6880 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6883 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6885 /* For objcopy and relocatable link, don't copy the output ELF
6886 section type from input if the output BFD section flags have been
6887 set to something different. For a final link allow some flags
6888 that the linker clears to differ. */
6889 if (elf_section_type (osec
) == SHT_NULL
6890 && (osec
->flags
== isec
->flags
6892 && ((osec
->flags
^ isec
->flags
)
6893 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6894 elf_section_type (osec
) = elf_section_type (isec
);
6896 /* FIXME: Is this correct for all OS/PROC specific flags? */
6897 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6898 & (SHF_MASKOS
| SHF_MASKPROC
));
6900 /* Set things up for objcopy and relocatable link. The output
6901 SHT_GROUP section will have its elf_next_in_group pointing back
6902 to the input group members. Ignore linker created group section.
6903 See elfNN_ia64_object_p in elfxx-ia64.c. */
6906 if (elf_sec_group (isec
) == NULL
6907 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6909 if (elf_section_flags (isec
) & SHF_GROUP
)
6910 elf_section_flags (osec
) |= SHF_GROUP
;
6911 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6912 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6915 /* If not decompress, preserve SHF_COMPRESSED. */
6916 if ((ibfd
->flags
& BFD_DECOMPRESS
) == 0)
6917 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6921 ihdr
= &elf_section_data (isec
)->this_hdr
;
6923 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6924 don't use the output section of the linked-to section since it
6925 may be NULL at this point. */
6926 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6928 ohdr
= &elf_section_data (osec
)->this_hdr
;
6929 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6930 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6933 osec
->use_rela_p
= isec
->use_rela_p
;
6938 /* Copy private section information. This copies over the entsize
6939 field, and sometimes the info field. */
6942 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6947 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6949 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6950 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6953 ihdr
= &elf_section_data (isec
)->this_hdr
;
6954 ohdr
= &elf_section_data (osec
)->this_hdr
;
6956 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6958 if (ihdr
->sh_type
== SHT_SYMTAB
6959 || ihdr
->sh_type
== SHT_DYNSYM
6960 || ihdr
->sh_type
== SHT_GNU_verneed
6961 || ihdr
->sh_type
== SHT_GNU_verdef
)
6962 ohdr
->sh_info
= ihdr
->sh_info
;
6964 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6968 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6969 necessary if we are removing either the SHT_GROUP section or any of
6970 the group member sections. DISCARDED is the value that a section's
6971 output_section has if the section will be discarded, NULL when this
6972 function is called from objcopy, bfd_abs_section_ptr when called
6976 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6980 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6981 if (elf_section_type (isec
) == SHT_GROUP
)
6983 asection
*first
= elf_next_in_group (isec
);
6984 asection
*s
= first
;
6985 bfd_size_type removed
= 0;
6989 /* If this member section is being output but the
6990 SHT_GROUP section is not, then clear the group info
6991 set up by _bfd_elf_copy_private_section_data. */
6992 if (s
->output_section
!= discarded
6993 && isec
->output_section
== discarded
)
6995 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6996 elf_group_name (s
->output_section
) = NULL
;
6998 /* Conversely, if the member section is not being output
6999 but the SHT_GROUP section is, then adjust its size. */
7000 else if (s
->output_section
== discarded
7001 && isec
->output_section
!= discarded
)
7003 s
= elf_next_in_group (s
);
7009 if (discarded
!= NULL
)
7011 /* If we've been called for ld -r, then we need to
7012 adjust the input section size. This function may
7013 be called multiple times, so save the original
7015 if (isec
->rawsize
== 0)
7016 isec
->rawsize
= isec
->size
;
7017 isec
->size
= isec
->rawsize
- removed
;
7021 /* Adjust the output section size when called from
7023 isec
->output_section
->size
-= removed
;
7031 /* Copy private header information. */
7034 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
7036 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7037 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7040 /* Copy over private BFD data if it has not already been copied.
7041 This must be done here, rather than in the copy_private_bfd_data
7042 entry point, because the latter is called after the section
7043 contents have been set, which means that the program headers have
7044 already been worked out. */
7045 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
7047 if (! copy_private_bfd_data (ibfd
, obfd
))
7051 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
7054 /* Copy private symbol information. If this symbol is in a section
7055 which we did not map into a BFD section, try to map the section
7056 index correctly. We use special macro definitions for the mapped
7057 section indices; these definitions are interpreted by the
7058 swap_out_syms function. */
7060 #define MAP_ONESYMTAB (SHN_HIOS + 1)
7061 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
7062 #define MAP_STRTAB (SHN_HIOS + 3)
7063 #define MAP_SHSTRTAB (SHN_HIOS + 4)
7064 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
7067 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
7072 elf_symbol_type
*isym
, *osym
;
7074 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
7075 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
7078 isym
= elf_symbol_from (ibfd
, isymarg
);
7079 osym
= elf_symbol_from (obfd
, osymarg
);
7082 && isym
->internal_elf_sym
.st_shndx
!= 0
7084 && bfd_is_abs_section (isym
->symbol
.section
))
7088 shndx
= isym
->internal_elf_sym
.st_shndx
;
7089 if (shndx
== elf_onesymtab (ibfd
))
7090 shndx
= MAP_ONESYMTAB
;
7091 else if (shndx
== elf_dynsymtab (ibfd
))
7092 shndx
= MAP_DYNSYMTAB
;
7093 else if (shndx
== elf_strtab_sec (ibfd
))
7095 else if (shndx
== elf_shstrtab_sec (ibfd
))
7096 shndx
= MAP_SHSTRTAB
;
7097 else if (shndx
== elf_symtab_shndx (ibfd
))
7098 shndx
= MAP_SYM_SHNDX
;
7099 osym
->internal_elf_sym
.st_shndx
= shndx
;
7105 /* Swap out the symbols. */
7108 swap_out_syms (bfd
*abfd
,
7109 struct elf_strtab_hash
**sttp
,
7112 const struct elf_backend_data
*bed
;
7115 struct elf_strtab_hash
*stt
;
7116 Elf_Internal_Shdr
*symtab_hdr
;
7117 Elf_Internal_Shdr
*symtab_shndx_hdr
;
7118 Elf_Internal_Shdr
*symstrtab_hdr
;
7119 struct elf_sym_strtab
*symstrtab
;
7120 bfd_byte
*outbound_syms
;
7121 bfd_byte
*outbound_shndx
;
7122 unsigned long outbound_syms_index
;
7123 unsigned long outbound_shndx_index
;
7125 unsigned int num_locals
;
7127 bfd_boolean name_local_sections
;
7129 if (!elf_map_symbols (abfd
, &num_locals
))
7132 /* Dump out the symtabs. */
7133 stt
= _bfd_elf_strtab_init ();
7137 bed
= get_elf_backend_data (abfd
);
7138 symcount
= bfd_get_symcount (abfd
);
7139 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7140 symtab_hdr
->sh_type
= SHT_SYMTAB
;
7141 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
7142 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
7143 symtab_hdr
->sh_info
= num_locals
+ 1;
7144 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
7146 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
7147 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7149 /* Allocate buffer to swap out the .strtab section. */
7150 symstrtab
= (struct elf_sym_strtab
*) bfd_malloc ((symcount
+ 1)
7151 * sizeof (*symstrtab
));
7152 if (symstrtab
== NULL
)
7154 _bfd_elf_strtab_free (stt
);
7158 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
7159 bed
->s
->sizeof_sym
);
7160 if (outbound_syms
== NULL
)
7163 _bfd_elf_strtab_free (stt
);
7167 symtab_hdr
->contents
= outbound_syms
;
7168 outbound_syms_index
= 0;
7170 outbound_shndx
= NULL
;
7171 outbound_shndx_index
= 0;
7172 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
7173 if (symtab_shndx_hdr
->sh_name
!= 0)
7175 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
7176 outbound_shndx
= (bfd_byte
*)
7177 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
7178 if (outbound_shndx
== NULL
)
7181 symtab_shndx_hdr
->contents
= outbound_shndx
;
7182 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
7183 symtab_shndx_hdr
->sh_size
= amt
;
7184 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
7185 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
7188 /* Now generate the data (for "contents"). */
7190 /* Fill in zeroth symbol and swap it out. */
7191 Elf_Internal_Sym sym
;
7197 sym
.st_shndx
= SHN_UNDEF
;
7198 sym
.st_target_internal
= 0;
7199 symstrtab
[0].sym
= sym
;
7200 symstrtab
[0].dest_index
= outbound_syms_index
;
7201 symstrtab
[0].destshndx_index
= outbound_shndx_index
;
7202 outbound_syms_index
++;
7203 if (outbound_shndx
!= NULL
)
7204 outbound_shndx_index
++;
7208 = (bed
->elf_backend_name_local_section_symbols
7209 && bed
->elf_backend_name_local_section_symbols (abfd
));
7211 syms
= bfd_get_outsymbols (abfd
);
7212 for (idx
= 0; idx
< symcount
;)
7214 Elf_Internal_Sym sym
;
7215 bfd_vma value
= syms
[idx
]->value
;
7216 elf_symbol_type
*type_ptr
;
7217 flagword flags
= syms
[idx
]->flags
;
7220 if (!name_local_sections
7221 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
7223 /* Local section symbols have no name. */
7224 sym
.st_name
= (unsigned long) -1;
7228 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
7229 to get the final offset for st_name. */
7231 = (unsigned long) _bfd_elf_strtab_add (stt
, syms
[idx
]->name
,
7233 if (sym
.st_name
== (unsigned long) -1)
7237 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
7239 if ((flags
& BSF_SECTION_SYM
) == 0
7240 && bfd_is_com_section (syms
[idx
]->section
))
7242 /* ELF common symbols put the alignment into the `value' field,
7243 and the size into the `size' field. This is backwards from
7244 how BFD handles it, so reverse it here. */
7245 sym
.st_size
= value
;
7246 if (type_ptr
== NULL
7247 || type_ptr
->internal_elf_sym
.st_value
== 0)
7248 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
7250 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
7251 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
7252 (abfd
, syms
[idx
]->section
);
7256 asection
*sec
= syms
[idx
]->section
;
7259 if (sec
->output_section
)
7261 value
+= sec
->output_offset
;
7262 sec
= sec
->output_section
;
7265 /* Don't add in the section vma for relocatable output. */
7266 if (! relocatable_p
)
7268 sym
.st_value
= value
;
7269 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
7271 if (bfd_is_abs_section (sec
)
7273 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
7275 /* This symbol is in a real ELF section which we did
7276 not create as a BFD section. Undo the mapping done
7277 by copy_private_symbol_data. */
7278 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
7282 shndx
= elf_onesymtab (abfd
);
7285 shndx
= elf_dynsymtab (abfd
);
7288 shndx
= elf_strtab_sec (abfd
);
7291 shndx
= elf_shstrtab_sec (abfd
);
7294 shndx
= elf_symtab_shndx (abfd
);
7303 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
7305 if (shndx
== SHN_BAD
)
7309 /* Writing this would be a hell of a lot easier if
7310 we had some decent documentation on bfd, and
7311 knew what to expect of the library, and what to
7312 demand of applications. For example, it
7313 appears that `objcopy' might not set the
7314 section of a symbol to be a section that is
7315 actually in the output file. */
7316 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
7319 _bfd_error_handler (_("\
7320 Unable to find equivalent output section for symbol '%s' from section '%s'"),
7321 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
7323 bfd_set_error (bfd_error_invalid_operation
);
7327 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
7328 BFD_ASSERT (shndx
!= SHN_BAD
);
7332 sym
.st_shndx
= shndx
;
7335 if ((flags
& BSF_THREAD_LOCAL
) != 0)
7337 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
7338 type
= STT_GNU_IFUNC
;
7339 else if ((flags
& BSF_FUNCTION
) != 0)
7341 else if ((flags
& BSF_OBJECT
) != 0)
7343 else if ((flags
& BSF_RELC
) != 0)
7345 else if ((flags
& BSF_SRELC
) != 0)
7350 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
7353 /* Processor-specific types. */
7354 if (type_ptr
!= NULL
7355 && bed
->elf_backend_get_symbol_type
)
7356 type
= ((*bed
->elf_backend_get_symbol_type
)
7357 (&type_ptr
->internal_elf_sym
, type
));
7359 if (flags
& BSF_SECTION_SYM
)
7361 if (flags
& BSF_GLOBAL
)
7362 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
7364 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
7366 else if (bfd_is_com_section (syms
[idx
]->section
))
7368 #ifdef USE_STT_COMMON
7369 if (type
== STT_OBJECT
)
7370 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
7373 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
7375 else if (bfd_is_und_section (syms
[idx
]->section
))
7376 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
7380 else if (flags
& BSF_FILE
)
7381 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
7384 int bind
= STB_LOCAL
;
7386 if (flags
& BSF_LOCAL
)
7388 else if (flags
& BSF_GNU_UNIQUE
)
7389 bind
= STB_GNU_UNIQUE
;
7390 else if (flags
& BSF_WEAK
)
7392 else if (flags
& BSF_GLOBAL
)
7395 sym
.st_info
= ELF_ST_INFO (bind
, type
);
7398 if (type_ptr
!= NULL
)
7400 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
7401 sym
.st_target_internal
7402 = type_ptr
->internal_elf_sym
.st_target_internal
;
7407 sym
.st_target_internal
= 0;
7411 symstrtab
[idx
].sym
= sym
;
7412 symstrtab
[idx
].dest_index
= outbound_syms_index
;
7413 symstrtab
[idx
].destshndx_index
= outbound_shndx_index
;
7415 outbound_syms_index
++;
7416 if (outbound_shndx
!= NULL
)
7417 outbound_shndx_index
++;
7420 /* Finalize the .strtab section. */
7421 _bfd_elf_strtab_finalize (stt
);
7423 /* Swap out the .strtab section. */
7424 for (idx
= 0; idx
<= symcount
; idx
++)
7426 struct elf_sym_strtab
*elfsym
= &symstrtab
[idx
];
7427 if (elfsym
->sym
.st_name
== (unsigned long) -1)
7428 elfsym
->sym
.st_name
= 0;
7430 elfsym
->sym
.st_name
= _bfd_elf_strtab_offset (stt
,
7431 elfsym
->sym
.st_name
);
7432 bed
->s
->swap_symbol_out (abfd
, &elfsym
->sym
,
7434 + (elfsym
->dest_index
7435 * bed
->s
->sizeof_sym
)),
7437 + (elfsym
->destshndx_index
7438 * sizeof (Elf_External_Sym_Shndx
))));
7443 symstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (stt
);
7444 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
7446 symstrtab_hdr
->sh_flags
= 0;
7447 symstrtab_hdr
->sh_addr
= 0;
7448 symstrtab_hdr
->sh_entsize
= 0;
7449 symstrtab_hdr
->sh_link
= 0;
7450 symstrtab_hdr
->sh_info
= 0;
7451 symstrtab_hdr
->sh_addralign
= 1;
7456 /* Return the number of bytes required to hold the symtab vector.
7458 Note that we base it on the count plus 1, since we will null terminate
7459 the vector allocated based on this size. However, the ELF symbol table
7460 always has a dummy entry as symbol #0, so it ends up even. */
7463 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
7467 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
7469 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7470 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7472 symtab_size
-= sizeof (asymbol
*);
7478 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
7482 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
7484 if (elf_dynsymtab (abfd
) == 0)
7486 bfd_set_error (bfd_error_invalid_operation
);
7490 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7491 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7493 symtab_size
-= sizeof (asymbol
*);
7499 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7502 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7505 /* Canonicalize the relocs. */
7508 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7515 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7517 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7520 tblptr
= section
->relocation
;
7521 for (i
= 0; i
< section
->reloc_count
; i
++)
7522 *relptr
++ = tblptr
++;
7526 return section
->reloc_count
;
7530 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7532 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7533 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7536 bfd_get_symcount (abfd
) = symcount
;
7541 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7542 asymbol
**allocation
)
7544 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7545 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7548 bfd_get_dynamic_symcount (abfd
) = symcount
;
7552 /* Return the size required for the dynamic reloc entries. Any loadable
7553 section that was actually installed in the BFD, and has type SHT_REL
7554 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7555 dynamic reloc section. */
7558 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7563 if (elf_dynsymtab (abfd
) == 0)
7565 bfd_set_error (bfd_error_invalid_operation
);
7569 ret
= sizeof (arelent
*);
7570 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7571 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7572 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7573 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7574 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7575 * sizeof (arelent
*));
7580 /* Canonicalize the dynamic relocation entries. Note that we return the
7581 dynamic relocations as a single block, although they are actually
7582 associated with particular sections; the interface, which was
7583 designed for SunOS style shared libraries, expects that there is only
7584 one set of dynamic relocs. Any loadable section that was actually
7585 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7586 dynamic symbol table, is considered to be a dynamic reloc section. */
7589 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7593 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7597 if (elf_dynsymtab (abfd
) == 0)
7599 bfd_set_error (bfd_error_invalid_operation
);
7603 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7605 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7607 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7608 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7609 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7614 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7616 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7618 for (i
= 0; i
< count
; i
++)
7629 /* Read in the version information. */
7632 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7634 bfd_byte
*contents
= NULL
;
7635 unsigned int freeidx
= 0;
7637 if (elf_dynverref (abfd
) != 0)
7639 Elf_Internal_Shdr
*hdr
;
7640 Elf_External_Verneed
*everneed
;
7641 Elf_Internal_Verneed
*iverneed
;
7643 bfd_byte
*contents_end
;
7645 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7647 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7649 error_return_bad_verref
:
7650 (*_bfd_error_handler
)
7651 (_("%B: .gnu.version_r invalid entry"), abfd
);
7652 bfd_set_error (bfd_error_bad_value
);
7653 error_return_verref
:
7654 elf_tdata (abfd
)->verref
= NULL
;
7655 elf_tdata (abfd
)->cverrefs
= 0;
7659 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7660 if (contents
== NULL
)
7661 goto error_return_verref
;
7663 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7664 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7665 goto error_return_verref
;
7667 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7668 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7670 if (elf_tdata (abfd
)->verref
== NULL
)
7671 goto error_return_verref
;
7673 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7674 == sizeof (Elf_External_Vernaux
));
7675 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7676 everneed
= (Elf_External_Verneed
*) contents
;
7677 iverneed
= elf_tdata (abfd
)->verref
;
7678 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7680 Elf_External_Vernaux
*evernaux
;
7681 Elf_Internal_Vernaux
*ivernaux
;
7684 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7686 iverneed
->vn_bfd
= abfd
;
7688 iverneed
->vn_filename
=
7689 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7691 if (iverneed
->vn_filename
== NULL
)
7692 goto error_return_bad_verref
;
7694 if (iverneed
->vn_cnt
== 0)
7695 iverneed
->vn_auxptr
= NULL
;
7698 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7699 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7700 sizeof (Elf_Internal_Vernaux
));
7701 if (iverneed
->vn_auxptr
== NULL
)
7702 goto error_return_verref
;
7705 if (iverneed
->vn_aux
7706 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7707 goto error_return_bad_verref
;
7709 evernaux
= ((Elf_External_Vernaux
*)
7710 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7711 ivernaux
= iverneed
->vn_auxptr
;
7712 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7714 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7716 ivernaux
->vna_nodename
=
7717 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7718 ivernaux
->vna_name
);
7719 if (ivernaux
->vna_nodename
== NULL
)
7720 goto error_return_bad_verref
;
7722 if (ivernaux
->vna_other
> freeidx
)
7723 freeidx
= ivernaux
->vna_other
;
7725 ivernaux
->vna_nextptr
= NULL
;
7726 if (ivernaux
->vna_next
== 0)
7728 iverneed
->vn_cnt
= j
+ 1;
7731 if (j
+ 1 < iverneed
->vn_cnt
)
7732 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7734 if (ivernaux
->vna_next
7735 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7736 goto error_return_bad_verref
;
7738 evernaux
= ((Elf_External_Vernaux
*)
7739 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7742 iverneed
->vn_nextref
= NULL
;
7743 if (iverneed
->vn_next
== 0)
7745 if (i
+ 1 < hdr
->sh_info
)
7746 iverneed
->vn_nextref
= iverneed
+ 1;
7748 if (iverneed
->vn_next
7749 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7750 goto error_return_bad_verref
;
7752 everneed
= ((Elf_External_Verneed
*)
7753 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7755 elf_tdata (abfd
)->cverrefs
= i
;
7761 if (elf_dynverdef (abfd
) != 0)
7763 Elf_Internal_Shdr
*hdr
;
7764 Elf_External_Verdef
*everdef
;
7765 Elf_Internal_Verdef
*iverdef
;
7766 Elf_Internal_Verdef
*iverdefarr
;
7767 Elf_Internal_Verdef iverdefmem
;
7769 unsigned int maxidx
;
7770 bfd_byte
*contents_end_def
, *contents_end_aux
;
7772 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7774 if (hdr
->sh_info
== 0 || hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7776 error_return_bad_verdef
:
7777 (*_bfd_error_handler
)
7778 (_("%B: .gnu.version_d invalid entry"), abfd
);
7779 bfd_set_error (bfd_error_bad_value
);
7780 error_return_verdef
:
7781 elf_tdata (abfd
)->verdef
= NULL
;
7782 elf_tdata (abfd
)->cverdefs
= 0;
7786 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7787 if (contents
== NULL
)
7788 goto error_return_verdef
;
7789 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7790 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7791 goto error_return_verdef
;
7793 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7794 >= sizeof (Elf_External_Verdaux
));
7795 contents_end_def
= contents
+ hdr
->sh_size
7796 - sizeof (Elf_External_Verdef
);
7797 contents_end_aux
= contents
+ hdr
->sh_size
7798 - sizeof (Elf_External_Verdaux
);
7800 /* We know the number of entries in the section but not the maximum
7801 index. Therefore we have to run through all entries and find
7803 everdef
= (Elf_External_Verdef
*) contents
;
7805 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7807 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7809 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) == 0)
7810 goto error_return_bad_verdef
;
7811 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7812 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7814 if (iverdefmem
.vd_next
== 0)
7817 if (iverdefmem
.vd_next
7818 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7819 goto error_return_bad_verdef
;
7821 everdef
= ((Elf_External_Verdef
*)
7822 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7825 if (default_imported_symver
)
7827 if (freeidx
> maxidx
)
7833 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7834 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7835 if (elf_tdata (abfd
)->verdef
== NULL
)
7836 goto error_return_verdef
;
7838 elf_tdata (abfd
)->cverdefs
= maxidx
;
7840 everdef
= (Elf_External_Verdef
*) contents
;
7841 iverdefarr
= elf_tdata (abfd
)->verdef
;
7842 for (i
= 0; i
< hdr
->sh_info
; i
++)
7844 Elf_External_Verdaux
*everdaux
;
7845 Elf_Internal_Verdaux
*iverdaux
;
7848 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7850 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7851 goto error_return_bad_verdef
;
7853 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7854 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7856 iverdef
->vd_bfd
= abfd
;
7858 if (iverdef
->vd_cnt
== 0)
7859 iverdef
->vd_auxptr
= NULL
;
7862 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7863 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7864 sizeof (Elf_Internal_Verdaux
));
7865 if (iverdef
->vd_auxptr
== NULL
)
7866 goto error_return_verdef
;
7870 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7871 goto error_return_bad_verdef
;
7873 everdaux
= ((Elf_External_Verdaux
*)
7874 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7875 iverdaux
= iverdef
->vd_auxptr
;
7876 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7878 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7880 iverdaux
->vda_nodename
=
7881 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7882 iverdaux
->vda_name
);
7883 if (iverdaux
->vda_nodename
== NULL
)
7884 goto error_return_bad_verdef
;
7886 iverdaux
->vda_nextptr
= NULL
;
7887 if (iverdaux
->vda_next
== 0)
7889 iverdef
->vd_cnt
= j
+ 1;
7892 if (j
+ 1 < iverdef
->vd_cnt
)
7893 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7895 if (iverdaux
->vda_next
7896 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7897 goto error_return_bad_verdef
;
7899 everdaux
= ((Elf_External_Verdaux
*)
7900 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7903 if (iverdef
->vd_cnt
)
7904 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7906 iverdef
->vd_nextdef
= NULL
;
7907 if (iverdef
->vd_next
== 0)
7909 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7910 iverdef
->vd_nextdef
= iverdef
+ 1;
7912 everdef
= ((Elf_External_Verdef
*)
7913 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7919 else if (default_imported_symver
)
7926 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7927 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7928 if (elf_tdata (abfd
)->verdef
== NULL
)
7931 elf_tdata (abfd
)->cverdefs
= freeidx
;
7934 /* Create a default version based on the soname. */
7935 if (default_imported_symver
)
7937 Elf_Internal_Verdef
*iverdef
;
7938 Elf_Internal_Verdaux
*iverdaux
;
7940 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7942 iverdef
->vd_version
= VER_DEF_CURRENT
;
7943 iverdef
->vd_flags
= 0;
7944 iverdef
->vd_ndx
= freeidx
;
7945 iverdef
->vd_cnt
= 1;
7947 iverdef
->vd_bfd
= abfd
;
7949 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7950 if (iverdef
->vd_nodename
== NULL
)
7951 goto error_return_verdef
;
7952 iverdef
->vd_nextdef
= NULL
;
7953 iverdef
->vd_auxptr
= ((struct elf_internal_verdaux
*)
7954 bfd_zalloc (abfd
, sizeof (Elf_Internal_Verdaux
)));
7955 if (iverdef
->vd_auxptr
== NULL
)
7956 goto error_return_verdef
;
7958 iverdaux
= iverdef
->vd_auxptr
;
7959 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7965 if (contents
!= NULL
)
7971 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7973 elf_symbol_type
*newsym
;
7975 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof * newsym
);
7978 newsym
->symbol
.the_bfd
= abfd
;
7979 return &newsym
->symbol
;
7983 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7987 bfd_symbol_info (symbol
, ret
);
7990 /* Return whether a symbol name implies a local symbol. Most targets
7991 use this function for the is_local_label_name entry point, but some
7995 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7998 /* Normal local symbols start with ``.L''. */
7999 if (name
[0] == '.' && name
[1] == 'L')
8002 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8003 DWARF debugging symbols starting with ``..''. */
8004 if (name
[0] == '.' && name
[1] == '.')
8007 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8008 emitting DWARF debugging output. I suspect this is actually a
8009 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8010 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8011 underscore to be emitted on some ELF targets). For ease of use,
8012 we treat such symbols as local. */
8013 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
8016 /* Treat assembler generated fake symbols, dollar local labels and
8017 forward-backward labels (aka local labels) as locals.
8018 These labels have the form:
8020 L0^A.* (fake symbols)
8022 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels)
8024 Versions which start with .L will have already been matched above,
8025 so we only need to match the rest. */
8026 if (name
[0] == 'L' && ISDIGIT (name
[1]))
8028 bfd_boolean ret
= FALSE
;
8032 for (p
= name
+ 2; (c
= *p
); p
++)
8034 if (c
== 1 || c
== 2)
8036 if (c
== 1 && p
== name
+ 2)
8037 /* A fake symbol. */
8040 /* FIXME: We are being paranoid here and treating symbols like
8041 L0^Bfoo as if there were non-local, on the grounds that the
8042 assembler will never generate them. But can any symbol
8043 containing an ASCII value in the range 1-31 ever be anything
8044 other than some kind of local ? */
8061 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
8062 asymbol
*symbol ATTRIBUTE_UNUSED
)
8069 _bfd_elf_set_arch_mach (bfd
*abfd
,
8070 enum bfd_architecture arch
,
8071 unsigned long machine
)
8073 /* If this isn't the right architecture for this backend, and this
8074 isn't the generic backend, fail. */
8075 if (arch
!= get_elf_backend_data (abfd
)->arch
8076 && arch
!= bfd_arch_unknown
8077 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
8080 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
8083 /* Find the nearest line to a particular section and offset,
8084 for error reporting. */
8087 _bfd_elf_find_nearest_line (bfd
*abfd
,
8091 const char **filename_ptr
,
8092 const char **functionname_ptr
,
8093 unsigned int *line_ptr
,
8094 unsigned int *discriminator_ptr
)
8098 if (_bfd_dwarf2_find_nearest_line (abfd
, symbols
, NULL
, section
, offset
,
8099 filename_ptr
, functionname_ptr
,
8100 line_ptr
, discriminator_ptr
,
8101 dwarf_debug_sections
, 0,
8102 &elf_tdata (abfd
)->dwarf2_find_line_info
)
8103 || _bfd_dwarf1_find_nearest_line (abfd
, symbols
, section
, offset
,
8104 filename_ptr
, functionname_ptr
,
8107 if (!*functionname_ptr
)
8108 _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8109 *filename_ptr
? NULL
: filename_ptr
,
8114 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
8115 &found
, filename_ptr
,
8116 functionname_ptr
, line_ptr
,
8117 &elf_tdata (abfd
)->line_info
))
8119 if (found
&& (*functionname_ptr
|| *line_ptr
))
8122 if (symbols
== NULL
)
8125 if (! _bfd_elf_find_function (abfd
, symbols
, section
, offset
,
8126 filename_ptr
, functionname_ptr
))
8133 /* Find the line for a symbol. */
8136 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
8137 const char **filename_ptr
, unsigned int *line_ptr
)
8139 return _bfd_dwarf2_find_nearest_line (abfd
, symbols
, symbol
, NULL
, 0,
8140 filename_ptr
, NULL
, line_ptr
, NULL
,
8141 dwarf_debug_sections
, 0,
8142 &elf_tdata (abfd
)->dwarf2_find_line_info
);
8145 /* After a call to bfd_find_nearest_line, successive calls to
8146 bfd_find_inliner_info can be used to get source information about
8147 each level of function inlining that terminated at the address
8148 passed to bfd_find_nearest_line. Currently this is only supported
8149 for DWARF2 with appropriate DWARF3 extensions. */
8152 _bfd_elf_find_inliner_info (bfd
*abfd
,
8153 const char **filename_ptr
,
8154 const char **functionname_ptr
,
8155 unsigned int *line_ptr
)
8158 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
8159 functionname_ptr
, line_ptr
,
8160 & elf_tdata (abfd
)->dwarf2_find_line_info
);
8165 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
8167 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8168 int ret
= bed
->s
->sizeof_ehdr
;
8170 if (!info
->relocatable
)
8172 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
8174 if (phdr_size
== (bfd_size_type
) -1)
8176 struct elf_segment_map
*m
;
8179 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
8180 phdr_size
+= bed
->s
->sizeof_phdr
;
8183 phdr_size
= get_program_header_size (abfd
, info
);
8186 elf_program_header_size (abfd
) = phdr_size
;
8194 _bfd_elf_set_section_contents (bfd
*abfd
,
8196 const void *location
,
8198 bfd_size_type count
)
8200 Elf_Internal_Shdr
*hdr
;
8203 if (! abfd
->output_has_begun
8204 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
8210 hdr
= &elf_section_data (section
)->this_hdr
;
8211 if (hdr
->sh_offset
== (file_ptr
) -1)
8213 /* We must compress this section. Write output to the buffer. */
8214 unsigned char *contents
= hdr
->contents
;
8215 if ((offset
+ count
) > hdr
->sh_size
8216 || (section
->flags
& SEC_ELF_COMPRESS
) == 0
8217 || contents
== NULL
)
8219 memcpy (contents
+ offset
, location
, count
);
8222 pos
= hdr
->sh_offset
+ offset
;
8223 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
8224 || bfd_bwrite (location
, count
, abfd
) != count
)
8231 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
8232 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
8233 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
8238 /* Try to convert a non-ELF reloc into an ELF one. */
8241 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
8243 /* Check whether we really have an ELF howto. */
8245 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
8247 bfd_reloc_code_real_type code
;
8248 reloc_howto_type
*howto
;
8250 /* Alien reloc: Try to determine its type to replace it with an
8251 equivalent ELF reloc. */
8253 if (areloc
->howto
->pc_relative
)
8255 switch (areloc
->howto
->bitsize
)
8258 code
= BFD_RELOC_8_PCREL
;
8261 code
= BFD_RELOC_12_PCREL
;
8264 code
= BFD_RELOC_16_PCREL
;
8267 code
= BFD_RELOC_24_PCREL
;
8270 code
= BFD_RELOC_32_PCREL
;
8273 code
= BFD_RELOC_64_PCREL
;
8279 howto
= bfd_reloc_type_lookup (abfd
, code
);
8281 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
8283 if (howto
->pcrel_offset
)
8284 areloc
->addend
+= areloc
->address
;
8286 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
8291 switch (areloc
->howto
->bitsize
)
8297 code
= BFD_RELOC_14
;
8300 code
= BFD_RELOC_16
;
8303 code
= BFD_RELOC_26
;
8306 code
= BFD_RELOC_32
;
8309 code
= BFD_RELOC_64
;
8315 howto
= bfd_reloc_type_lookup (abfd
, code
);
8319 areloc
->howto
= howto
;
8327 (*_bfd_error_handler
)
8328 (_("%B: unsupported relocation type %s"),
8329 abfd
, areloc
->howto
->name
);
8330 bfd_set_error (bfd_error_bad_value
);
8335 _bfd_elf_close_and_cleanup (bfd
*abfd
)
8337 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
8338 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
8340 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
8341 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
8342 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
8345 return _bfd_generic_close_and_cleanup (abfd
);
8348 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8349 in the relocation's offset. Thus we cannot allow any sort of sanity
8350 range-checking to interfere. There is nothing else to do in processing
8353 bfd_reloc_status_type
8354 _bfd_elf_rel_vtable_reloc_fn
8355 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
8356 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
8357 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
8358 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
8360 return bfd_reloc_ok
;
8363 /* Elf core file support. Much of this only works on native
8364 toolchains, since we rely on knowing the
8365 machine-dependent procfs structure in order to pick
8366 out details about the corefile. */
8368 #ifdef HAVE_SYS_PROCFS_H
8369 /* Needed for new procfs interface on sparc-solaris. */
8370 # define _STRUCTURED_PROC 1
8371 # include <sys/procfs.h>
8374 /* Return a PID that identifies a "thread" for threaded cores, or the
8375 PID of the main process for non-threaded cores. */
8378 elfcore_make_pid (bfd
*abfd
)
8382 pid
= elf_tdata (abfd
)->core
->lwpid
;
8384 pid
= elf_tdata (abfd
)->core
->pid
;
8389 /* If there isn't a section called NAME, make one, using
8390 data from SECT. Note, this function will generate a
8391 reference to NAME, so you shouldn't deallocate or
8395 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
8399 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
8402 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
8406 sect2
->size
= sect
->size
;
8407 sect2
->filepos
= sect
->filepos
;
8408 sect2
->alignment_power
= sect
->alignment_power
;
8412 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
8413 actually creates up to two pseudosections:
8414 - For the single-threaded case, a section named NAME, unless
8415 such a section already exists.
8416 - For the multi-threaded case, a section named "NAME/PID", where
8417 PID is elfcore_make_pid (abfd).
8418 Both pseudosections have identical contents. */
8420 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
8426 char *threaded_name
;
8430 /* Build the section name. */
8432 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8433 len
= strlen (buf
) + 1;
8434 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8435 if (threaded_name
== NULL
)
8437 memcpy (threaded_name
, buf
, len
);
8439 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8444 sect
->filepos
= filepos
;
8445 sect
->alignment_power
= 2;
8447 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8450 /* prstatus_t exists on:
8452 linux 2.[01] + glibc
8456 #if defined (HAVE_PRSTATUS_T)
8459 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8464 if (note
->descsz
== sizeof (prstatus_t
))
8468 size
= sizeof (prstat
.pr_reg
);
8469 offset
= offsetof (prstatus_t
, pr_reg
);
8470 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8472 /* Do not overwrite the core signal if it
8473 has already been set by another thread. */
8474 if (elf_tdata (abfd
)->core
->signal
== 0)
8475 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8476 if (elf_tdata (abfd
)->core
->pid
== 0)
8477 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8479 /* pr_who exists on:
8482 pr_who doesn't exist on:
8485 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8486 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8488 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8491 #if defined (HAVE_PRSTATUS32_T)
8492 else if (note
->descsz
== sizeof (prstatus32_t
))
8494 /* 64-bit host, 32-bit corefile */
8495 prstatus32_t prstat
;
8497 size
= sizeof (prstat
.pr_reg
);
8498 offset
= offsetof (prstatus32_t
, pr_reg
);
8499 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8501 /* Do not overwrite the core signal if it
8502 has already been set by another thread. */
8503 if (elf_tdata (abfd
)->core
->signal
== 0)
8504 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8505 if (elf_tdata (abfd
)->core
->pid
== 0)
8506 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8508 /* pr_who exists on:
8511 pr_who doesn't exist on:
8514 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8515 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8517 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8520 #endif /* HAVE_PRSTATUS32_T */
8523 /* Fail - we don't know how to handle any other
8524 note size (ie. data object type). */
8528 /* Make a ".reg/999" section and a ".reg" section. */
8529 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8530 size
, note
->descpos
+ offset
);
8532 #endif /* defined (HAVE_PRSTATUS_T) */
8534 /* Create a pseudosection containing the exact contents of NOTE. */
8536 elfcore_make_note_pseudosection (bfd
*abfd
,
8538 Elf_Internal_Note
*note
)
8540 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8541 note
->descsz
, note
->descpos
);
8544 /* There isn't a consistent prfpregset_t across platforms,
8545 but it doesn't matter, because we don't have to pick this
8546 data structure apart. */
8549 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8551 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8554 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8555 type of NT_PRXFPREG. Just include the whole note's contents
8559 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8561 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8564 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8565 with a note type of NT_X86_XSTATE. Just include the whole note's
8566 contents literally. */
8569 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8571 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8575 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8577 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8581 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8583 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8587 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8589 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8593 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8595 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8599 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8601 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8605 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8607 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8611 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8613 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8617 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8619 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8623 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8625 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8629 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8631 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8635 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8637 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8641 elfcore_grok_s390_vxrs_low (bfd
*abfd
, Elf_Internal_Note
*note
)
8643 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-low", note
);
8647 elfcore_grok_s390_vxrs_high (bfd
*abfd
, Elf_Internal_Note
*note
)
8649 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-vxrs-high", note
);
8653 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8655 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8659 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8661 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8665 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8667 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8671 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8673 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8676 #if defined (HAVE_PRPSINFO_T)
8677 typedef prpsinfo_t elfcore_psinfo_t
;
8678 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8679 typedef prpsinfo32_t elfcore_psinfo32_t
;
8683 #if defined (HAVE_PSINFO_T)
8684 typedef psinfo_t elfcore_psinfo_t
;
8685 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8686 typedef psinfo32_t elfcore_psinfo32_t
;
8690 /* return a malloc'ed copy of a string at START which is at
8691 most MAX bytes long, possibly without a terminating '\0'.
8692 the copy will always have a terminating '\0'. */
8695 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8698 char *end
= (char *) memchr (start
, '\0', max
);
8706 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8710 memcpy (dups
, start
, len
);
8716 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8718 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8720 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8722 elfcore_psinfo_t psinfo
;
8724 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8726 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8727 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8729 elf_tdata (abfd
)->core
->program
8730 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8731 sizeof (psinfo
.pr_fname
));
8733 elf_tdata (abfd
)->core
->command
8734 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8735 sizeof (psinfo
.pr_psargs
));
8737 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8738 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8740 /* 64-bit host, 32-bit corefile */
8741 elfcore_psinfo32_t psinfo
;
8743 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8745 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8746 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8748 elf_tdata (abfd
)->core
->program
8749 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8750 sizeof (psinfo
.pr_fname
));
8752 elf_tdata (abfd
)->core
->command
8753 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8754 sizeof (psinfo
.pr_psargs
));
8760 /* Fail - we don't know how to handle any other
8761 note size (ie. data object type). */
8765 /* Note that for some reason, a spurious space is tacked
8766 onto the end of the args in some (at least one anyway)
8767 implementations, so strip it off if it exists. */
8770 char *command
= elf_tdata (abfd
)->core
->command
;
8771 int n
= strlen (command
);
8773 if (0 < n
&& command
[n
- 1] == ' ')
8774 command
[n
- 1] = '\0';
8779 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8781 #if defined (HAVE_PSTATUS_T)
8783 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8785 if (note
->descsz
== sizeof (pstatus_t
)
8786 #if defined (HAVE_PXSTATUS_T)
8787 || note
->descsz
== sizeof (pxstatus_t
)
8793 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8795 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8797 #if defined (HAVE_PSTATUS32_T)
8798 else if (note
->descsz
== sizeof (pstatus32_t
))
8800 /* 64-bit host, 32-bit corefile */
8803 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8805 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8808 /* Could grab some more details from the "representative"
8809 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8810 NT_LWPSTATUS note, presumably. */
8814 #endif /* defined (HAVE_PSTATUS_T) */
8816 #if defined (HAVE_LWPSTATUS_T)
8818 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8820 lwpstatus_t lwpstat
;
8826 if (note
->descsz
!= sizeof (lwpstat
)
8827 #if defined (HAVE_LWPXSTATUS_T)
8828 && note
->descsz
!= sizeof (lwpxstatus_t
)
8833 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8835 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8836 /* Do not overwrite the core signal if it has already been set by
8838 if (elf_tdata (abfd
)->core
->signal
== 0)
8839 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8841 /* Make a ".reg/999" section. */
8843 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8844 len
= strlen (buf
) + 1;
8845 name
= bfd_alloc (abfd
, len
);
8848 memcpy (name
, buf
, len
);
8850 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8854 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8855 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8856 sect
->filepos
= note
->descpos
8857 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8860 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8861 sect
->size
= sizeof (lwpstat
.pr_reg
);
8862 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8865 sect
->alignment_power
= 2;
8867 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8870 /* Make a ".reg2/999" section */
8872 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8873 len
= strlen (buf
) + 1;
8874 name
= bfd_alloc (abfd
, len
);
8877 memcpy (name
, buf
, len
);
8879 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8883 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8884 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8885 sect
->filepos
= note
->descpos
8886 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8889 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8890 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8891 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8894 sect
->alignment_power
= 2;
8896 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8898 #endif /* defined (HAVE_LWPSTATUS_T) */
8901 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8908 int is_active_thread
;
8911 if (note
->descsz
< 728)
8914 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8917 type
= bfd_get_32 (abfd
, note
->descdata
);
8921 case 1 /* NOTE_INFO_PROCESS */:
8922 /* FIXME: need to add ->core->command. */
8923 /* process_info.pid */
8924 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8925 /* process_info.signal */
8926 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8929 case 2 /* NOTE_INFO_THREAD */:
8930 /* Make a ".reg/999" section. */
8931 /* thread_info.tid */
8932 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8934 len
= strlen (buf
) + 1;
8935 name
= (char *) bfd_alloc (abfd
, len
);
8939 memcpy (name
, buf
, len
);
8941 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8945 /* sizeof (thread_info.thread_context) */
8947 /* offsetof (thread_info.thread_context) */
8948 sect
->filepos
= note
->descpos
+ 12;
8949 sect
->alignment_power
= 2;
8951 /* thread_info.is_active_thread */
8952 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8954 if (is_active_thread
)
8955 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8959 case 3 /* NOTE_INFO_MODULE */:
8960 /* Make a ".module/xxxxxxxx" section. */
8961 /* module_info.base_address */
8962 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8963 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8965 len
= strlen (buf
) + 1;
8966 name
= (char *) bfd_alloc (abfd
, len
);
8970 memcpy (name
, buf
, len
);
8972 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8977 sect
->size
= note
->descsz
;
8978 sect
->filepos
= note
->descpos
;
8979 sect
->alignment_power
= 2;
8990 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8992 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9000 if (bed
->elf_backend_grok_prstatus
)
9001 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
9003 #if defined (HAVE_PRSTATUS_T)
9004 return elfcore_grok_prstatus (abfd
, note
);
9009 #if defined (HAVE_PSTATUS_T)
9011 return elfcore_grok_pstatus (abfd
, note
);
9014 #if defined (HAVE_LWPSTATUS_T)
9016 return elfcore_grok_lwpstatus (abfd
, note
);
9019 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
9020 return elfcore_grok_prfpreg (abfd
, note
);
9022 case NT_WIN32PSTATUS
:
9023 return elfcore_grok_win32pstatus (abfd
, note
);
9025 case NT_PRXFPREG
: /* Linux SSE extension */
9026 if (note
->namesz
== 6
9027 && strcmp (note
->namedata
, "LINUX") == 0)
9028 return elfcore_grok_prxfpreg (abfd
, note
);
9032 case NT_X86_XSTATE
: /* Linux XSAVE extension */
9033 if (note
->namesz
== 6
9034 && strcmp (note
->namedata
, "LINUX") == 0)
9035 return elfcore_grok_xstatereg (abfd
, note
);
9036 else if (note
->namesz
== 8
9037 && strcmp (note
->namedata
, "FreeBSD") == 0)
9038 return elfcore_grok_xstatereg (abfd
, note
);
9043 if (note
->namesz
== 6
9044 && strcmp (note
->namedata
, "LINUX") == 0)
9045 return elfcore_grok_ppc_vmx (abfd
, note
);
9050 if (note
->namesz
== 6
9051 && strcmp (note
->namedata
, "LINUX") == 0)
9052 return elfcore_grok_ppc_vsx (abfd
, note
);
9056 case NT_S390_HIGH_GPRS
:
9057 if (note
->namesz
== 6
9058 && strcmp (note
->namedata
, "LINUX") == 0)
9059 return elfcore_grok_s390_high_gprs (abfd
, note
);
9064 if (note
->namesz
== 6
9065 && strcmp (note
->namedata
, "LINUX") == 0)
9066 return elfcore_grok_s390_timer (abfd
, note
);
9070 case NT_S390_TODCMP
:
9071 if (note
->namesz
== 6
9072 && strcmp (note
->namedata
, "LINUX") == 0)
9073 return elfcore_grok_s390_todcmp (abfd
, note
);
9077 case NT_S390_TODPREG
:
9078 if (note
->namesz
== 6
9079 && strcmp (note
->namedata
, "LINUX") == 0)
9080 return elfcore_grok_s390_todpreg (abfd
, note
);
9085 if (note
->namesz
== 6
9086 && strcmp (note
->namedata
, "LINUX") == 0)
9087 return elfcore_grok_s390_ctrs (abfd
, note
);
9091 case NT_S390_PREFIX
:
9092 if (note
->namesz
== 6
9093 && strcmp (note
->namedata
, "LINUX") == 0)
9094 return elfcore_grok_s390_prefix (abfd
, note
);
9098 case NT_S390_LAST_BREAK
:
9099 if (note
->namesz
== 6
9100 && strcmp (note
->namedata
, "LINUX") == 0)
9101 return elfcore_grok_s390_last_break (abfd
, note
);
9105 case NT_S390_SYSTEM_CALL
:
9106 if (note
->namesz
== 6
9107 && strcmp (note
->namedata
, "LINUX") == 0)
9108 return elfcore_grok_s390_system_call (abfd
, note
);
9113 if (note
->namesz
== 6
9114 && strcmp (note
->namedata
, "LINUX") == 0)
9115 return elfcore_grok_s390_tdb (abfd
, note
);
9119 case NT_S390_VXRS_LOW
:
9120 if (note
->namesz
== 6
9121 && strcmp (note
->namedata
, "LINUX") == 0)
9122 return elfcore_grok_s390_vxrs_low (abfd
, note
);
9126 case NT_S390_VXRS_HIGH
:
9127 if (note
->namesz
== 6
9128 && strcmp (note
->namedata
, "LINUX") == 0)
9129 return elfcore_grok_s390_vxrs_high (abfd
, note
);
9134 if (note
->namesz
== 6
9135 && strcmp (note
->namedata
, "LINUX") == 0)
9136 return elfcore_grok_arm_vfp (abfd
, note
);
9141 if (note
->namesz
== 6
9142 && strcmp (note
->namedata
, "LINUX") == 0)
9143 return elfcore_grok_aarch_tls (abfd
, note
);
9147 case NT_ARM_HW_BREAK
:
9148 if (note
->namesz
== 6
9149 && strcmp (note
->namedata
, "LINUX") == 0)
9150 return elfcore_grok_aarch_hw_break (abfd
, note
);
9154 case NT_ARM_HW_WATCH
:
9155 if (note
->namesz
== 6
9156 && strcmp (note
->namedata
, "LINUX") == 0)
9157 return elfcore_grok_aarch_hw_watch (abfd
, note
);
9163 if (bed
->elf_backend_grok_psinfo
)
9164 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
9166 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9167 return elfcore_grok_psinfo (abfd
, note
);
9174 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9179 sect
->size
= note
->descsz
;
9180 sect
->filepos
= note
->descpos
;
9181 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9187 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
9191 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
9197 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
9199 struct bfd_build_id
* build_id
;
9201 if (note
->descsz
== 0)
9204 build_id
= bfd_alloc (abfd
, sizeof (struct bfd_build_id
) - 1 + note
->descsz
);
9205 if (build_id
== NULL
)
9208 build_id
->size
= note
->descsz
;
9209 memcpy (build_id
->data
, note
->descdata
, note
->descsz
);
9210 abfd
->build_id
= build_id
;
9216 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9223 case NT_GNU_BUILD_ID
:
9224 return elfobj_grok_gnu_build_id (abfd
, note
);
9229 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
9231 struct sdt_note
*cur
=
9232 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
9235 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
9236 cur
->size
= (bfd_size_type
) note
->descsz
;
9237 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
9239 elf_tdata (abfd
)->sdt_note_head
= cur
;
9245 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9250 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
9258 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
9262 cp
= strchr (note
->namedata
, '@');
9265 *lwpidp
= atoi(cp
+ 1);
9272 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9274 /* Signal number at offset 0x08. */
9275 elf_tdata (abfd
)->core
->signal
9276 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9278 /* Process ID at offset 0x50. */
9279 elf_tdata (abfd
)->core
->pid
9280 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
9282 /* Command name at 0x7c (max 32 bytes, including nul). */
9283 elf_tdata (abfd
)->core
->command
9284 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
9286 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
9291 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9295 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
9296 elf_tdata (abfd
)->core
->lwpid
= lwp
;
9298 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
9300 /* NetBSD-specific core "procinfo". Note that we expect to
9301 find this note before any of the others, which is fine,
9302 since the kernel writes this note out first when it
9303 creates a core file. */
9305 return elfcore_grok_netbsd_procinfo (abfd
, note
);
9308 /* As of Jan 2002 there are no other machine-independent notes
9309 defined for NetBSD core files. If the note type is less
9310 than the start of the machine-dependent note types, we don't
9313 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
9317 switch (bfd_get_arch (abfd
))
9319 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9320 PT_GETFPREGS == mach+2. */
9322 case bfd_arch_alpha
:
9323 case bfd_arch_sparc
:
9326 case NT_NETBSDCORE_FIRSTMACH
+0:
9327 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9329 case NT_NETBSDCORE_FIRSTMACH
+2:
9330 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9336 /* On all other arch's, PT_GETREGS == mach+1 and
9337 PT_GETFPREGS == mach+3. */
9342 case NT_NETBSDCORE_FIRSTMACH
+1:
9343 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9345 case NT_NETBSDCORE_FIRSTMACH
+3:
9346 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9356 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
9358 /* Signal number at offset 0x08. */
9359 elf_tdata (abfd
)->core
->signal
9360 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
9362 /* Process ID at offset 0x20. */
9363 elf_tdata (abfd
)->core
->pid
9364 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
9366 /* Command name at 0x48 (max 32 bytes, including nul). */
9367 elf_tdata (abfd
)->core
->command
9368 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
9374 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9376 if (note
->type
== NT_OPENBSD_PROCINFO
)
9377 return elfcore_grok_openbsd_procinfo (abfd
, note
);
9379 if (note
->type
== NT_OPENBSD_REGS
)
9380 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
9382 if (note
->type
== NT_OPENBSD_FPREGS
)
9383 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
9385 if (note
->type
== NT_OPENBSD_XFPREGS
)
9386 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
9388 if (note
->type
== NT_OPENBSD_AUXV
)
9390 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
9395 sect
->size
= note
->descsz
;
9396 sect
->filepos
= note
->descpos
;
9397 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9402 if (note
->type
== NT_OPENBSD_WCOOKIE
)
9404 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
9409 sect
->size
= note
->descsz
;
9410 sect
->filepos
= note
->descpos
;
9411 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
9420 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
9422 void *ddata
= note
->descdata
;
9429 /* nto_procfs_status 'pid' field is at offset 0. */
9430 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
9432 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
9433 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
9435 /* nto_procfs_status 'flags' field is at offset 8. */
9436 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
9438 /* nto_procfs_status 'what' field is at offset 14. */
9439 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
9441 elf_tdata (abfd
)->core
->signal
= sig
;
9442 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9445 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
9446 do not come from signals so we make sure we set the current
9447 thread just in case. */
9448 if (flags
& 0x00000080)
9449 elf_tdata (abfd
)->core
->lwpid
= *tid
;
9451 /* Make a ".qnx_core_status/%d" section. */
9452 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
9454 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9459 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9463 sect
->size
= note
->descsz
;
9464 sect
->filepos
= note
->descpos
;
9465 sect
->alignment_power
= 2;
9467 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9471 elfcore_grok_nto_regs (bfd
*abfd
,
9472 Elf_Internal_Note
*note
,
9480 /* Make a "(base)/%d" section. */
9481 sprintf (buf
, "%s/%ld", base
, tid
);
9483 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9488 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9492 sect
->size
= note
->descsz
;
9493 sect
->filepos
= note
->descpos
;
9494 sect
->alignment_power
= 2;
9496 /* This is the current thread. */
9497 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9498 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9503 #define BFD_QNT_CORE_INFO 7
9504 #define BFD_QNT_CORE_STATUS 8
9505 #define BFD_QNT_CORE_GREG 9
9506 #define BFD_QNT_CORE_FPREG 10
9509 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9511 /* Every GREG section has a STATUS section before it. Store the
9512 tid from the previous call to pass down to the next gregs
9514 static long tid
= 1;
9518 case BFD_QNT_CORE_INFO
:
9519 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9520 case BFD_QNT_CORE_STATUS
:
9521 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9522 case BFD_QNT_CORE_GREG
:
9523 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9524 case BFD_QNT_CORE_FPREG
:
9525 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9532 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9538 /* Use note name as section name. */
9540 name
= (char *) bfd_alloc (abfd
, len
);
9543 memcpy (name
, note
->namedata
, len
);
9544 name
[len
- 1] = '\0';
9546 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9550 sect
->size
= note
->descsz
;
9551 sect
->filepos
= note
->descpos
;
9552 sect
->alignment_power
= 1;
9557 /* Function: elfcore_write_note
9560 buffer to hold note, and current size of buffer
9564 size of data for note
9566 Writes note to end of buffer. ELF64 notes are written exactly as
9567 for ELF32, despite the current (as of 2006) ELF gabi specifying
9568 that they ought to have 8-byte namesz and descsz field, and have
9569 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9572 Pointer to realloc'd buffer, *BUFSIZ updated. */
9575 elfcore_write_note (bfd
*abfd
,
9583 Elf_External_Note
*xnp
;
9590 namesz
= strlen (name
) + 1;
9592 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9594 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9597 dest
= buf
+ *bufsiz
;
9598 *bufsiz
+= newspace
;
9599 xnp
= (Elf_External_Note
*) dest
;
9600 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9601 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9602 H_PUT_32 (abfd
, type
, xnp
->type
);
9606 memcpy (dest
, name
, namesz
);
9614 memcpy (dest
, input
, size
);
9625 elfcore_write_prpsinfo (bfd
*abfd
,
9631 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9633 if (bed
->elf_backend_write_core_note
!= NULL
)
9636 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9637 NT_PRPSINFO
, fname
, psargs
);
9642 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9643 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9644 if (bed
->s
->elfclass
== ELFCLASS32
)
9646 #if defined (HAVE_PSINFO32_T)
9648 int note_type
= NT_PSINFO
;
9651 int note_type
= NT_PRPSINFO
;
9654 memset (&data
, 0, sizeof (data
));
9655 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9656 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9657 return elfcore_write_note (abfd
, buf
, bufsiz
,
9658 "CORE", note_type
, &data
, sizeof (data
));
9663 #if defined (HAVE_PSINFO_T)
9665 int note_type
= NT_PSINFO
;
9668 int note_type
= NT_PRPSINFO
;
9671 memset (&data
, 0, sizeof (data
));
9672 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9673 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9674 return elfcore_write_note (abfd
, buf
, bufsiz
,
9675 "CORE", note_type
, &data
, sizeof (data
));
9677 #endif /* PSINFO_T or PRPSINFO_T */
9684 elfcore_write_linux_prpsinfo32
9685 (bfd
*abfd
, char *buf
, int *bufsiz
,
9686 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9688 struct elf_external_linux_prpsinfo32 data
;
9690 memset (&data
, 0, sizeof (data
));
9691 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9693 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9694 &data
, sizeof (data
));
9698 elfcore_write_linux_prpsinfo64
9699 (bfd
*abfd
, char *buf
, int *bufsiz
,
9700 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9702 struct elf_external_linux_prpsinfo64 data
;
9704 memset (&data
, 0, sizeof (data
));
9705 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9707 return elfcore_write_note (abfd
, buf
, bufsiz
,
9708 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9712 elfcore_write_prstatus (bfd
*abfd
,
9719 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9721 if (bed
->elf_backend_write_core_note
!= NULL
)
9724 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9726 pid
, cursig
, gregs
);
9731 #if defined (HAVE_PRSTATUS_T)
9732 #if defined (HAVE_PRSTATUS32_T)
9733 if (bed
->s
->elfclass
== ELFCLASS32
)
9735 prstatus32_t prstat
;
9737 memset (&prstat
, 0, sizeof (prstat
));
9738 prstat
.pr_pid
= pid
;
9739 prstat
.pr_cursig
= cursig
;
9740 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9741 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9742 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9749 memset (&prstat
, 0, sizeof (prstat
));
9750 prstat
.pr_pid
= pid
;
9751 prstat
.pr_cursig
= cursig
;
9752 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9753 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9754 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9756 #endif /* HAVE_PRSTATUS_T */
9762 #if defined (HAVE_LWPSTATUS_T)
9764 elfcore_write_lwpstatus (bfd
*abfd
,
9771 lwpstatus_t lwpstat
;
9772 const char *note_name
= "CORE";
9774 memset (&lwpstat
, 0, sizeof (lwpstat
));
9775 lwpstat
.pr_lwpid
= pid
>> 16;
9776 lwpstat
.pr_cursig
= cursig
;
9777 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9778 memcpy (&lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9779 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9781 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9782 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9784 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9785 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9788 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9789 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9791 #endif /* HAVE_LWPSTATUS_T */
9793 #if defined (HAVE_PSTATUS_T)
9795 elfcore_write_pstatus (bfd
*abfd
,
9799 int cursig ATTRIBUTE_UNUSED
,
9800 const void *gregs ATTRIBUTE_UNUSED
)
9802 const char *note_name
= "CORE";
9803 #if defined (HAVE_PSTATUS32_T)
9804 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9806 if (bed
->s
->elfclass
== ELFCLASS32
)
9810 memset (&pstat
, 0, sizeof (pstat
));
9811 pstat
.pr_pid
= pid
& 0xffff;
9812 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9813 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9821 memset (&pstat
, 0, sizeof (pstat
));
9822 pstat
.pr_pid
= pid
& 0xffff;
9823 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9824 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9828 #endif /* HAVE_PSTATUS_T */
9831 elfcore_write_prfpreg (bfd
*abfd
,
9837 const char *note_name
= "CORE";
9838 return elfcore_write_note (abfd
, buf
, bufsiz
,
9839 note_name
, NT_FPREGSET
, fpregs
, size
);
9843 elfcore_write_prxfpreg (bfd
*abfd
,
9846 const void *xfpregs
,
9849 char *note_name
= "LINUX";
9850 return elfcore_write_note (abfd
, buf
, bufsiz
,
9851 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9855 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9856 const void *xfpregs
, int size
)
9859 if (get_elf_backend_data (abfd
)->elf_osabi
== ELFOSABI_FREEBSD
)
9860 note_name
= "FreeBSD";
9862 note_name
= "LINUX";
9863 return elfcore_write_note (abfd
, buf
, bufsiz
,
9864 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9868 elfcore_write_ppc_vmx (bfd
*abfd
,
9871 const void *ppc_vmx
,
9874 char *note_name
= "LINUX";
9875 return elfcore_write_note (abfd
, buf
, bufsiz
,
9876 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9880 elfcore_write_ppc_vsx (bfd
*abfd
,
9883 const void *ppc_vsx
,
9886 char *note_name
= "LINUX";
9887 return elfcore_write_note (abfd
, buf
, bufsiz
,
9888 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9892 elfcore_write_s390_high_gprs (bfd
*abfd
,
9895 const void *s390_high_gprs
,
9898 char *note_name
= "LINUX";
9899 return elfcore_write_note (abfd
, buf
, bufsiz
,
9900 note_name
, NT_S390_HIGH_GPRS
,
9901 s390_high_gprs
, size
);
9905 elfcore_write_s390_timer (bfd
*abfd
,
9908 const void *s390_timer
,
9911 char *note_name
= "LINUX";
9912 return elfcore_write_note (abfd
, buf
, bufsiz
,
9913 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9917 elfcore_write_s390_todcmp (bfd
*abfd
,
9920 const void *s390_todcmp
,
9923 char *note_name
= "LINUX";
9924 return elfcore_write_note (abfd
, buf
, bufsiz
,
9925 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9929 elfcore_write_s390_todpreg (bfd
*abfd
,
9932 const void *s390_todpreg
,
9935 char *note_name
= "LINUX";
9936 return elfcore_write_note (abfd
, buf
, bufsiz
,
9937 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9941 elfcore_write_s390_ctrs (bfd
*abfd
,
9944 const void *s390_ctrs
,
9947 char *note_name
= "LINUX";
9948 return elfcore_write_note (abfd
, buf
, bufsiz
,
9949 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9953 elfcore_write_s390_prefix (bfd
*abfd
,
9956 const void *s390_prefix
,
9959 char *note_name
= "LINUX";
9960 return elfcore_write_note (abfd
, buf
, bufsiz
,
9961 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9965 elfcore_write_s390_last_break (bfd
*abfd
,
9968 const void *s390_last_break
,
9971 char *note_name
= "LINUX";
9972 return elfcore_write_note (abfd
, buf
, bufsiz
,
9973 note_name
, NT_S390_LAST_BREAK
,
9974 s390_last_break
, size
);
9978 elfcore_write_s390_system_call (bfd
*abfd
,
9981 const void *s390_system_call
,
9984 char *note_name
= "LINUX";
9985 return elfcore_write_note (abfd
, buf
, bufsiz
,
9986 note_name
, NT_S390_SYSTEM_CALL
,
9987 s390_system_call
, size
);
9991 elfcore_write_s390_tdb (bfd
*abfd
,
9994 const void *s390_tdb
,
9997 char *note_name
= "LINUX";
9998 return elfcore_write_note (abfd
, buf
, bufsiz
,
9999 note_name
, NT_S390_TDB
, s390_tdb
, size
);
10003 elfcore_write_s390_vxrs_low (bfd
*abfd
,
10006 const void *s390_vxrs_low
,
10009 char *note_name
= "LINUX";
10010 return elfcore_write_note (abfd
, buf
, bufsiz
,
10011 note_name
, NT_S390_VXRS_LOW
, s390_vxrs_low
, size
);
10015 elfcore_write_s390_vxrs_high (bfd
*abfd
,
10018 const void *s390_vxrs_high
,
10021 char *note_name
= "LINUX";
10022 return elfcore_write_note (abfd
, buf
, bufsiz
,
10023 note_name
, NT_S390_VXRS_HIGH
,
10024 s390_vxrs_high
, size
);
10028 elfcore_write_arm_vfp (bfd
*abfd
,
10031 const void *arm_vfp
,
10034 char *note_name
= "LINUX";
10035 return elfcore_write_note (abfd
, buf
, bufsiz
,
10036 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
10040 elfcore_write_aarch_tls (bfd
*abfd
,
10043 const void *aarch_tls
,
10046 char *note_name
= "LINUX";
10047 return elfcore_write_note (abfd
, buf
, bufsiz
,
10048 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
10052 elfcore_write_aarch_hw_break (bfd
*abfd
,
10055 const void *aarch_hw_break
,
10058 char *note_name
= "LINUX";
10059 return elfcore_write_note (abfd
, buf
, bufsiz
,
10060 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
10064 elfcore_write_aarch_hw_watch (bfd
*abfd
,
10067 const void *aarch_hw_watch
,
10070 char *note_name
= "LINUX";
10071 return elfcore_write_note (abfd
, buf
, bufsiz
,
10072 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
10076 elfcore_write_register_note (bfd
*abfd
,
10079 const char *section
,
10083 if (strcmp (section
, ".reg2") == 0)
10084 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
10085 if (strcmp (section
, ".reg-xfp") == 0)
10086 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
10087 if (strcmp (section
, ".reg-xstate") == 0)
10088 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
10089 if (strcmp (section
, ".reg-ppc-vmx") == 0)
10090 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
10091 if (strcmp (section
, ".reg-ppc-vsx") == 0)
10092 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
10093 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
10094 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
10095 if (strcmp (section
, ".reg-s390-timer") == 0)
10096 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
10097 if (strcmp (section
, ".reg-s390-todcmp") == 0)
10098 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
10099 if (strcmp (section
, ".reg-s390-todpreg") == 0)
10100 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
10101 if (strcmp (section
, ".reg-s390-ctrs") == 0)
10102 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
10103 if (strcmp (section
, ".reg-s390-prefix") == 0)
10104 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
10105 if (strcmp (section
, ".reg-s390-last-break") == 0)
10106 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
10107 if (strcmp (section
, ".reg-s390-system-call") == 0)
10108 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
10109 if (strcmp (section
, ".reg-s390-tdb") == 0)
10110 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
10111 if (strcmp (section
, ".reg-s390-vxrs-low") == 0)
10112 return elfcore_write_s390_vxrs_low (abfd
, buf
, bufsiz
, data
, size
);
10113 if (strcmp (section
, ".reg-s390-vxrs-high") == 0)
10114 return elfcore_write_s390_vxrs_high (abfd
, buf
, bufsiz
, data
, size
);
10115 if (strcmp (section
, ".reg-arm-vfp") == 0)
10116 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
10117 if (strcmp (section
, ".reg-aarch-tls") == 0)
10118 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
10119 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
10120 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
10121 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
10122 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
10127 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
10132 while (p
< buf
+ size
)
10134 /* FIXME: bad alignment assumption. */
10135 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
10136 Elf_Internal_Note in
;
10138 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
10141 in
.type
= H_GET_32 (abfd
, xnp
->type
);
10143 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
10144 in
.namedata
= xnp
->name
;
10145 if (in
.namesz
> buf
- in
.namedata
+ size
)
10148 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
10149 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
10150 in
.descpos
= offset
+ (in
.descdata
- buf
);
10152 && (in
.descdata
>= buf
+ size
10153 || in
.descsz
> buf
- in
.descdata
+ size
))
10156 switch (bfd_get_format (abfd
))
10163 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
10166 const char * string
;
10168 bfd_boolean (* func
)(bfd
*, Elf_Internal_Note
*);
10172 GROKER_ELEMENT ("", elfcore_grok_note
),
10173 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note
),
10174 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note
),
10175 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note
),
10176 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note
)
10178 #undef GROKER_ELEMENT
10181 for (i
= ARRAY_SIZE (grokers
); i
--;)
10183 if (in
.namesz
>= grokers
[i
].len
10184 && strncmp (in
.namedata
, grokers
[i
].string
,
10185 grokers
[i
].len
) == 0)
10187 if (! grokers
[i
].func (abfd
, & in
))
10196 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
10198 if (! elfobj_grok_gnu_note (abfd
, &in
))
10201 else if (in
.namesz
== sizeof "stapsdt"
10202 && strcmp (in
.namedata
, "stapsdt") == 0)
10204 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
10210 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
10217 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
10224 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
10227 buf
= (char *) bfd_malloc (size
+ 1);
10231 /* PR 17512: file: ec08f814
10232 0-termintate the buffer so that string searches will not overflow. */
10235 if (bfd_bread (buf
, size
, abfd
) != size
10236 || !elf_parse_notes (abfd
, buf
, size
, offset
))
10246 /* Providing external access to the ELF program header table. */
10248 /* Return an upper bound on the number of bytes required to store a
10249 copy of ABFD's program header table entries. Return -1 if an error
10250 occurs; bfd_get_error will return an appropriate code. */
10253 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
10255 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10257 bfd_set_error (bfd_error_wrong_format
);
10261 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
10264 /* Copy ABFD's program header table entries to *PHDRS. The entries
10265 will be stored as an array of Elf_Internal_Phdr structures, as
10266 defined in include/elf/internal.h. To find out how large the
10267 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10269 Return the number of program header table entries read, or -1 if an
10270 error occurs; bfd_get_error will return an appropriate code. */
10273 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
10277 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
10279 bfd_set_error (bfd_error_wrong_format
);
10283 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
10284 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
10285 num_phdrs
* sizeof (Elf_Internal_Phdr
));
10290 enum elf_reloc_type_class
10291 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
10292 const asection
*rel_sec ATTRIBUTE_UNUSED
,
10293 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
10295 return reloc_class_normal
;
10298 /* For RELA architectures, return the relocation value for a
10299 relocation against a local symbol. */
10302 _bfd_elf_rela_local_sym (bfd
*abfd
,
10303 Elf_Internal_Sym
*sym
,
10305 Elf_Internal_Rela
*rel
)
10307 asection
*sec
= *psec
;
10308 bfd_vma relocation
;
10310 relocation
= (sec
->output_section
->vma
10311 + sec
->output_offset
10313 if ((sec
->flags
& SEC_MERGE
)
10314 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
10315 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
10318 _bfd_merged_section_offset (abfd
, psec
,
10319 elf_section_data (sec
)->sec_info
,
10320 sym
->st_value
+ rel
->r_addend
);
10323 /* If we have changed the section, and our original section is
10324 marked with SEC_EXCLUDE, it means that the original
10325 SEC_MERGE section has been completely subsumed in some
10326 other SEC_MERGE section. In this case, we need to leave
10327 some info around for --emit-relocs. */
10328 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
10329 sec
->kept_section
= *psec
;
10332 rel
->r_addend
-= relocation
;
10333 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
10339 _bfd_elf_rel_local_sym (bfd
*abfd
,
10340 Elf_Internal_Sym
*sym
,
10344 asection
*sec
= *psec
;
10346 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
10347 return sym
->st_value
+ addend
;
10349 return _bfd_merged_section_offset (abfd
, psec
,
10350 elf_section_data (sec
)->sec_info
,
10351 sym
->st_value
+ addend
);
10355 _bfd_elf_section_offset (bfd
*abfd
,
10356 struct bfd_link_info
*info
,
10360 switch (sec
->sec_info_type
)
10362 case SEC_INFO_TYPE_STABS
:
10363 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
10365 case SEC_INFO_TYPE_EH_FRAME
:
10366 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
10368 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
10370 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10371 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
10372 offset
= sec
->size
- offset
- address_size
;
10378 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
10379 reconstruct an ELF file by reading the segments out of remote memory
10380 based on the ELF file header at EHDR_VMA and the ELF program headers it
10381 points to. If not null, *LOADBASEP is filled in with the difference
10382 between the VMAs from which the segments were read, and the VMAs the
10383 file headers (and hence BFD's idea of each section's VMA) put them at.
10385 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10386 remote memory at target address VMA into the local buffer at MYADDR; it
10387 should return zero on success or an `errno' code on failure. TEMPL must
10388 be a BFD for an ELF target with the word size and byte order found in
10389 the remote memory. */
10392 bfd_elf_bfd_from_remote_memory
10395 bfd_size_type size
,
10396 bfd_vma
*loadbasep
,
10397 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
10399 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
10400 (templ
, ehdr_vma
, size
, loadbasep
, target_read_memory
);
10404 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
10405 long symcount ATTRIBUTE_UNUSED
,
10406 asymbol
**syms ATTRIBUTE_UNUSED
,
10411 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
10414 const char *relplt_name
;
10415 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
10419 Elf_Internal_Shdr
*hdr
;
10425 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
10428 if (dynsymcount
<= 0)
10431 if (!bed
->plt_sym_val
)
10434 relplt_name
= bed
->relplt_name
;
10435 if (relplt_name
== NULL
)
10436 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
10437 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
10438 if (relplt
== NULL
)
10441 hdr
= &elf_section_data (relplt
)->this_hdr
;
10442 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
10443 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
10446 plt
= bfd_get_section_by_name (abfd
, ".plt");
10450 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
10451 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
10454 count
= relplt
->size
/ hdr
->sh_entsize
;
10455 size
= count
* sizeof (asymbol
);
10456 p
= relplt
->relocation
;
10457 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10459 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
10460 if (p
->addend
!= 0)
10463 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
10465 size
+= sizeof ("+0x") - 1 + 8;
10470 s
= *ret
= (asymbol
*) bfd_malloc (size
);
10474 names
= (char *) (s
+ count
);
10475 p
= relplt
->relocation
;
10477 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
10482 addr
= bed
->plt_sym_val (i
, plt
, p
);
10483 if (addr
== (bfd_vma
) -1)
10486 *s
= **p
->sym_ptr_ptr
;
10487 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
10488 we are defining a symbol, ensure one of them is set. */
10489 if ((s
->flags
& BSF_LOCAL
) == 0)
10490 s
->flags
|= BSF_GLOBAL
;
10491 s
->flags
|= BSF_SYNTHETIC
;
10493 s
->value
= addr
- plt
->vma
;
10496 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
10497 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10499 if (p
->addend
!= 0)
10503 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10504 names
+= sizeof ("+0x") - 1;
10505 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10506 for (a
= buf
; *a
== '0'; ++a
)
10509 memcpy (names
, a
, len
);
10512 memcpy (names
, "@plt", sizeof ("@plt"));
10513 names
+= sizeof ("@plt");
10520 /* It is only used by x86-64 so far. */
10521 asection _bfd_elf_large_com_section
10522 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10523 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10526 _bfd_elf_post_process_headers (bfd
* abfd
,
10527 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10529 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10531 i_ehdrp
= elf_elfheader (abfd
);
10533 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10535 /* To make things simpler for the loader on Linux systems we set the
10536 osabi field to ELFOSABI_GNU if the binary contains symbols of
10537 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10538 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10539 && elf_tdata (abfd
)->has_gnu_symbols
)
10540 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10544 /* Return TRUE for ELF symbol types that represent functions.
10545 This is the default version of this function, which is sufficient for
10546 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10549 _bfd_elf_is_function_type (unsigned int type
)
10551 return (type
== STT_FUNC
10552 || type
== STT_GNU_IFUNC
);
10555 /* If the ELF symbol SYM might be a function in SEC, return the
10556 function size and set *CODE_OFF to the function's entry point,
10557 otherwise return zero. */
10560 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10563 bfd_size_type size
;
10565 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10566 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10567 || sym
->section
!= sec
)
10570 *code_off
= sym
->value
;
10572 if (!(sym
->flags
& BSF_SYNTHETIC
))
10573 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;