1 /* ELF executable support for BFD.
3 Copyright 1993-2013 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 BFD support for ELF formats is being worked on.
28 Currently, the best supported back ends are for sparc and i386
29 (running svr4 or Solaris 2).
31 Documentation of the internals of the support code still needs
32 to be written. The code is changing quickly enough that we
33 haven't bothered yet. */
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
44 #include "safe-ctype.h"
45 #include "elf-linux-psinfo.h"
51 static int elf_sort_sections (const void *, const void *);
52 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
53 static bfd_boolean
prep_headers (bfd
*);
54 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
55 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
56 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
59 /* Swap version information in and out. The version information is
60 currently size independent. If that ever changes, this code will
61 need to move into elfcode.h. */
63 /* Swap in a Verdef structure. */
66 _bfd_elf_swap_verdef_in (bfd
*abfd
,
67 const Elf_External_Verdef
*src
,
68 Elf_Internal_Verdef
*dst
)
70 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
71 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
72 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
73 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
74 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
75 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
76 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
79 /* Swap out a Verdef structure. */
82 _bfd_elf_swap_verdef_out (bfd
*abfd
,
83 const Elf_Internal_Verdef
*src
,
84 Elf_External_Verdef
*dst
)
86 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
87 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
88 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
89 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
90 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
91 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
92 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
95 /* Swap in a Verdaux structure. */
98 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
99 const Elf_External_Verdaux
*src
,
100 Elf_Internal_Verdaux
*dst
)
102 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
103 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
106 /* Swap out a Verdaux structure. */
109 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
110 const Elf_Internal_Verdaux
*src
,
111 Elf_External_Verdaux
*dst
)
113 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
114 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
117 /* Swap in a Verneed structure. */
120 _bfd_elf_swap_verneed_in (bfd
*abfd
,
121 const Elf_External_Verneed
*src
,
122 Elf_Internal_Verneed
*dst
)
124 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
125 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
126 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
127 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
128 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
131 /* Swap out a Verneed structure. */
134 _bfd_elf_swap_verneed_out (bfd
*abfd
,
135 const Elf_Internal_Verneed
*src
,
136 Elf_External_Verneed
*dst
)
138 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
139 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
140 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
141 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
142 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
145 /* Swap in a Vernaux structure. */
148 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
149 const Elf_External_Vernaux
*src
,
150 Elf_Internal_Vernaux
*dst
)
152 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
153 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
154 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
155 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
156 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
159 /* Swap out a Vernaux structure. */
162 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
163 const Elf_Internal_Vernaux
*src
,
164 Elf_External_Vernaux
*dst
)
166 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
167 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
168 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
169 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
170 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
173 /* Swap in a Versym structure. */
176 _bfd_elf_swap_versym_in (bfd
*abfd
,
177 const Elf_External_Versym
*src
,
178 Elf_Internal_Versym
*dst
)
180 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
183 /* Swap out a Versym structure. */
186 _bfd_elf_swap_versym_out (bfd
*abfd
,
187 const Elf_Internal_Versym
*src
,
188 Elf_External_Versym
*dst
)
190 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
193 /* Standard ELF hash function. Do not change this function; you will
194 cause invalid hash tables to be generated. */
197 bfd_elf_hash (const char *namearg
)
199 const unsigned char *name
= (const unsigned char *) namearg
;
204 while ((ch
= *name
++) != '\0')
207 if ((g
= (h
& 0xf0000000)) != 0)
210 /* The ELF ABI says `h &= ~g', but this is equivalent in
211 this case and on some machines one insn instead of two. */
215 return h
& 0xffffffff;
218 /* DT_GNU_HASH hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
222 bfd_elf_gnu_hash (const char *namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
225 unsigned long h
= 5381;
228 while ((ch
= *name
++) != '\0')
229 h
= (h
<< 5) + h
+ ch
;
230 return h
& 0xffffffff;
233 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
236 bfd_elf_allocate_object (bfd
*abfd
,
238 enum elf_target_id object_id
)
240 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
241 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
242 if (abfd
->tdata
.any
== NULL
)
245 elf_object_id (abfd
) = object_id
;
246 if (abfd
->direction
!= read_direction
)
248 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
251 elf_tdata (abfd
)->o
= o
;
252 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
259 bfd_elf_make_object (bfd
*abfd
)
261 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
262 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
267 bfd_elf_mkcorefile (bfd
*abfd
)
269 /* I think this can be done just like an object file. */
270 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
272 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
273 return elf_tdata (abfd
)->core
!= NULL
;
277 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
279 Elf_Internal_Shdr
**i_shdrp
;
280 bfd_byte
*shstrtab
= NULL
;
282 bfd_size_type shstrtabsize
;
284 i_shdrp
= elf_elfsections (abfd
);
286 || shindex
>= elf_numsections (abfd
)
287 || i_shdrp
[shindex
] == 0)
290 shstrtab
= i_shdrp
[shindex
]->contents
;
291 if (shstrtab
== NULL
)
293 /* No cached one, attempt to read, and cache what we read. */
294 offset
= i_shdrp
[shindex
]->sh_offset
;
295 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
297 /* Allocate and clear an extra byte at the end, to prevent crashes
298 in case the string table is not terminated. */
299 if (shstrtabsize
+ 1 <= 1
300 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
301 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
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
);
308 /* Once we've failed to read it, make sure we don't keep
309 trying. Otherwise, we'll keep allocating space for
310 the string table over and over. */
311 i_shdrp
[shindex
]->sh_size
= 0;
314 shstrtab
[shstrtabsize
] = '\0';
315 i_shdrp
[shindex
]->contents
= shstrtab
;
317 return (char *) shstrtab
;
321 bfd_elf_string_from_elf_section (bfd
*abfd
,
322 unsigned int shindex
,
323 unsigned int strindex
)
325 Elf_Internal_Shdr
*hdr
;
330 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
333 hdr
= elf_elfsections (abfd
)[shindex
];
335 if (hdr
->contents
== NULL
336 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
339 if (strindex
>= hdr
->sh_size
)
341 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
342 (*_bfd_error_handler
)
343 (_("%B: invalid string offset %u >= %lu for section `%s'"),
344 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
345 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
347 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
351 return ((char *) hdr
->contents
) + strindex
;
354 /* Read and convert symbols to internal format.
355 SYMCOUNT specifies the number of symbols to read, starting from
356 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
357 are non-NULL, they are used to store the internal symbols, external
358 symbols, and symbol section index extensions, respectively.
359 Returns a pointer to the internal symbol buffer (malloced if necessary)
360 or NULL if there were no symbols or some kind of problem. */
363 bfd_elf_get_elf_syms (bfd
*ibfd
,
364 Elf_Internal_Shdr
*symtab_hdr
,
367 Elf_Internal_Sym
*intsym_buf
,
369 Elf_External_Sym_Shndx
*extshndx_buf
)
371 Elf_Internal_Shdr
*shndx_hdr
;
373 const bfd_byte
*esym
;
374 Elf_External_Sym_Shndx
*alloc_extshndx
;
375 Elf_External_Sym_Shndx
*shndx
;
376 Elf_Internal_Sym
*alloc_intsym
;
377 Elf_Internal_Sym
*isym
;
378 Elf_Internal_Sym
*isymend
;
379 const struct elf_backend_data
*bed
;
384 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
390 /* Normal syms might have section extension entries. */
392 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
393 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
395 /* Read the symbols. */
397 alloc_extshndx
= NULL
;
399 bed
= get_elf_backend_data (ibfd
);
400 extsym_size
= bed
->s
->sizeof_sym
;
401 amt
= symcount
* extsym_size
;
402 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
403 if (extsym_buf
== NULL
)
405 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
406 extsym_buf
= alloc_ext
;
408 if (extsym_buf
== NULL
409 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
410 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
416 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
420 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
421 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
422 if (extshndx_buf
== NULL
)
424 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
425 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
426 extshndx_buf
= alloc_extshndx
;
428 if (extshndx_buf
== NULL
429 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
430 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
437 if (intsym_buf
== NULL
)
439 alloc_intsym
= (Elf_Internal_Sym
*)
440 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
441 intsym_buf
= alloc_intsym
;
442 if (intsym_buf
== NULL
)
446 /* Convert the symbols to internal form. */
447 isymend
= intsym_buf
+ symcount
;
448 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
449 shndx
= extshndx_buf
;
451 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
452 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
454 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
455 (*_bfd_error_handler
) (_("%B symbol number %lu references "
456 "nonexistent SHT_SYMTAB_SHNDX section"),
457 ibfd
, (unsigned long) symoffset
);
458 if (alloc_intsym
!= NULL
)
465 if (alloc_ext
!= NULL
)
467 if (alloc_extshndx
!= NULL
)
468 free (alloc_extshndx
);
473 /* Look up a symbol name. */
475 bfd_elf_sym_name (bfd
*abfd
,
476 Elf_Internal_Shdr
*symtab_hdr
,
477 Elf_Internal_Sym
*isym
,
481 unsigned int iname
= isym
->st_name
;
482 unsigned int shindex
= symtab_hdr
->sh_link
;
484 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
485 /* Check for a bogus st_shndx to avoid crashing. */
486 && isym
->st_shndx
< elf_numsections (abfd
))
488 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
489 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
492 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
495 else if (sym_sec
&& *name
== '\0')
496 name
= bfd_section_name (abfd
, sym_sec
);
501 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
502 sections. The first element is the flags, the rest are section
505 typedef union elf_internal_group
{
506 Elf_Internal_Shdr
*shdr
;
508 } Elf_Internal_Group
;
510 /* Return the name of the group signature symbol. Why isn't the
511 signature just a string? */
514 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
516 Elf_Internal_Shdr
*hdr
;
517 unsigned char esym
[sizeof (Elf64_External_Sym
)];
518 Elf_External_Sym_Shndx eshndx
;
519 Elf_Internal_Sym isym
;
521 /* First we need to ensure the symbol table is available. Make sure
522 that it is a symbol table section. */
523 if (ghdr
->sh_link
>= elf_numsections (abfd
))
525 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
526 if (hdr
->sh_type
!= SHT_SYMTAB
527 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
530 /* Go read the symbol. */
531 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
532 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
533 &isym
, esym
, &eshndx
) == NULL
)
536 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
539 /* Set next_in_group list pointer, and group name for NEWSECT. */
542 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
544 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
546 /* If num_group is zero, read in all SHT_GROUP sections. The count
547 is set to -1 if there are no SHT_GROUP sections. */
550 unsigned int i
, shnum
;
552 /* First count the number of groups. If we have a SHT_GROUP
553 section with just a flag word (ie. sh_size is 4), ignore it. */
554 shnum
= elf_numsections (abfd
);
557 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
558 ( (shdr)->sh_type == SHT_GROUP \
559 && (shdr)->sh_size >= minsize \
560 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
561 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
563 for (i
= 0; i
< shnum
; i
++)
565 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
567 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
573 num_group
= (unsigned) -1;
574 elf_tdata (abfd
)->num_group
= num_group
;
578 /* We keep a list of elf section headers for group sections,
579 so we can find them quickly. */
582 elf_tdata (abfd
)->num_group
= num_group
;
583 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
584 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
585 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
589 for (i
= 0; i
< shnum
; i
++)
591 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
593 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
596 Elf_Internal_Group
*dest
;
598 /* Add to list of sections. */
599 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
602 /* Read the raw contents. */
603 BFD_ASSERT (sizeof (*dest
) >= 4);
604 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
605 shdr
->contents
= (unsigned char *)
606 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
607 /* PR binutils/4110: Handle corrupt group headers. */
608 if (shdr
->contents
== NULL
)
611 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
612 bfd_set_error (bfd_error_bad_value
);
616 memset (shdr
->contents
, 0, amt
);
618 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
619 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
623 /* Translate raw contents, a flag word followed by an
624 array of elf section indices all in target byte order,
625 to the flag word followed by an array of elf section
627 src
= shdr
->contents
+ shdr
->sh_size
;
628 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
635 idx
= H_GET_32 (abfd
, src
);
636 if (src
== shdr
->contents
)
639 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
640 shdr
->bfd_section
->flags
641 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
646 ((*_bfd_error_handler
)
647 (_("%B: invalid SHT_GROUP entry"), abfd
));
650 dest
->shdr
= elf_elfsections (abfd
)[idx
];
657 if (num_group
!= (unsigned) -1)
661 for (i
= 0; i
< num_group
; i
++)
663 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
664 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
665 unsigned int n_elt
= shdr
->sh_size
/ 4;
667 /* Look through this group's sections to see if current
668 section is a member. */
670 if ((++idx
)->shdr
== hdr
)
674 /* We are a member of this group. Go looking through
675 other members to see if any others are linked via
677 idx
= (Elf_Internal_Group
*) shdr
->contents
;
678 n_elt
= shdr
->sh_size
/ 4;
680 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
681 && elf_next_in_group (s
) != NULL
)
685 /* Snarf the group name from other member, and
686 insert current section in circular list. */
687 elf_group_name (newsect
) = elf_group_name (s
);
688 elf_next_in_group (newsect
) = elf_next_in_group (s
);
689 elf_next_in_group (s
) = newsect
;
695 gname
= group_signature (abfd
, shdr
);
698 elf_group_name (newsect
) = gname
;
700 /* Start a circular list with one element. */
701 elf_next_in_group (newsect
) = newsect
;
704 /* If the group section has been created, point to the
706 if (shdr
->bfd_section
!= NULL
)
707 elf_next_in_group (shdr
->bfd_section
) = newsect
;
715 if (elf_group_name (newsect
) == NULL
)
717 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
724 _bfd_elf_setup_sections (bfd
*abfd
)
727 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
728 bfd_boolean result
= TRUE
;
731 /* Process SHF_LINK_ORDER. */
732 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
734 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
735 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
737 unsigned int elfsec
= this_hdr
->sh_link
;
738 /* FIXME: The old Intel compiler and old strip/objcopy may
739 not set the sh_link or sh_info fields. Hence we could
740 get the situation where elfsec is 0. */
743 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
744 if (bed
->link_order_error_handler
)
745 bed
->link_order_error_handler
746 (_("%B: warning: sh_link not set for section `%A'"),
751 asection
*linksec
= NULL
;
753 if (elfsec
< elf_numsections (abfd
))
755 this_hdr
= elf_elfsections (abfd
)[elfsec
];
756 linksec
= this_hdr
->bfd_section
;
760 Some strip/objcopy may leave an incorrect value in
761 sh_link. We don't want to proceed. */
764 (*_bfd_error_handler
)
765 (_("%B: sh_link [%d] in section `%A' is incorrect"),
766 s
->owner
, s
, elfsec
);
770 elf_linked_to_section (s
) = linksec
;
775 /* Process section groups. */
776 if (num_group
== (unsigned) -1)
779 for (i
= 0; i
< num_group
; i
++)
781 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
782 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
783 unsigned int n_elt
= shdr
->sh_size
/ 4;
786 if ((++idx
)->shdr
->bfd_section
)
787 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
788 else if (idx
->shdr
->sh_type
== SHT_RELA
789 || idx
->shdr
->sh_type
== SHT_REL
)
790 /* We won't include relocation sections in section groups in
791 output object files. We adjust the group section size here
792 so that relocatable link will work correctly when
793 relocation sections are in section group in input object
795 shdr
->bfd_section
->size
-= 4;
798 /* There are some unknown sections in the group. */
799 (*_bfd_error_handler
)
800 (_("%B: unknown [%d] section `%s' in group [%s]"),
802 (unsigned int) idx
->shdr
->sh_type
,
803 bfd_elf_string_from_elf_section (abfd
,
804 (elf_elfheader (abfd
)
807 shdr
->bfd_section
->name
);
815 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
817 return elf_next_in_group (sec
) != NULL
;
820 /* Make a BFD section from an ELF section. We store a pointer to the
821 BFD section in the bfd_section field of the header. */
824 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
825 Elf_Internal_Shdr
*hdr
,
831 const struct elf_backend_data
*bed
;
833 if (hdr
->bfd_section
!= NULL
)
836 newsect
= bfd_make_section_anyway (abfd
, name
);
840 hdr
->bfd_section
= newsect
;
841 elf_section_data (newsect
)->this_hdr
= *hdr
;
842 elf_section_data (newsect
)->this_idx
= shindex
;
844 /* Always use the real type/flags. */
845 elf_section_type (newsect
) = hdr
->sh_type
;
846 elf_section_flags (newsect
) = hdr
->sh_flags
;
848 newsect
->filepos
= hdr
->sh_offset
;
850 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
851 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
852 || ! bfd_set_section_alignment (abfd
, newsect
,
853 bfd_log2 (hdr
->sh_addralign
)))
856 flags
= SEC_NO_FLAGS
;
857 if (hdr
->sh_type
!= SHT_NOBITS
)
858 flags
|= SEC_HAS_CONTENTS
;
859 if (hdr
->sh_type
== SHT_GROUP
)
860 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
861 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
864 if (hdr
->sh_type
!= SHT_NOBITS
)
867 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
868 flags
|= SEC_READONLY
;
869 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
871 else if ((flags
& SEC_LOAD
) != 0)
873 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
876 newsect
->entsize
= hdr
->sh_entsize
;
877 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
878 flags
|= SEC_STRINGS
;
880 if (hdr
->sh_flags
& SHF_GROUP
)
881 if (!setup_group (abfd
, hdr
, newsect
))
883 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
884 flags
|= SEC_THREAD_LOCAL
;
885 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
886 flags
|= SEC_EXCLUDE
;
888 if ((flags
& SEC_ALLOC
) == 0)
890 /* The debugging sections appear to be recognized only by name,
891 not any sort of flag. Their SEC_ALLOC bits are cleared. */
898 else if (name
[1] == 'g' && name
[2] == 'n')
899 p
= ".gnu.linkonce.wi.", n
= 17;
900 else if (name
[1] == 'g' && name
[2] == 'd')
901 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
902 else if (name
[1] == 'l')
904 else if (name
[1] == 's')
906 else if (name
[1] == 'z')
907 p
= ".zdebug", n
= 7;
910 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
911 flags
|= SEC_DEBUGGING
;
915 /* As a GNU extension, if the name begins with .gnu.linkonce, we
916 only link a single copy of the section. This is used to support
917 g++. g++ will emit each template expansion in its own section.
918 The symbols will be defined as weak, so that multiple definitions
919 are permitted. The GNU linker extension is to actually discard
920 all but one of the sections. */
921 if (CONST_STRNEQ (name
, ".gnu.linkonce")
922 && elf_next_in_group (newsect
) == NULL
)
923 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
925 bed
= get_elf_backend_data (abfd
);
926 if (bed
->elf_backend_section_flags
)
927 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
930 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
933 /* We do not parse the PT_NOTE segments as we are interested even in the
934 separate debug info files which may have the segments offsets corrupted.
935 PT_NOTEs from the core files are currently not parsed using BFD. */
936 if (hdr
->sh_type
== SHT_NOTE
)
940 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
943 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
947 if ((flags
& SEC_ALLOC
) != 0)
949 Elf_Internal_Phdr
*phdr
;
950 unsigned int i
, nload
;
952 /* Some ELF linkers produce binaries with all the program header
953 p_paddr fields zero. If we have such a binary with more than
954 one PT_LOAD header, then leave the section lma equal to vma
955 so that we don't create sections with overlapping lma. */
956 phdr
= elf_tdata (abfd
)->phdr
;
957 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
958 if (phdr
->p_paddr
!= 0)
960 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
962 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
965 phdr
= elf_tdata (abfd
)->phdr
;
966 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
968 if (((phdr
->p_type
== PT_LOAD
969 && (hdr
->sh_flags
& SHF_TLS
) == 0)
970 || phdr
->p_type
== PT_TLS
)
971 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
973 if ((flags
& SEC_LOAD
) == 0)
974 newsect
->lma
= (phdr
->p_paddr
975 + hdr
->sh_addr
- phdr
->p_vaddr
);
977 /* We used to use the same adjustment for SEC_LOAD
978 sections, but that doesn't work if the segment
979 is packed with code from multiple VMAs.
980 Instead we calculate the section LMA based on
981 the segment LMA. It is assumed that the
982 segment will contain sections with contiguous
983 LMAs, even if the VMAs are not. */
984 newsect
->lma
= (phdr
->p_paddr
985 + hdr
->sh_offset
- phdr
->p_offset
);
987 /* With contiguous segments, we can't tell from file
988 offsets whether a section with zero size should
989 be placed at the end of one segment or the
990 beginning of the next. Decide based on vaddr. */
991 if (hdr
->sh_addr
>= phdr
->p_vaddr
992 && (hdr
->sh_addr
+ hdr
->sh_size
993 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
999 /* Compress/decompress DWARF debug sections with names: .debug_* and
1000 .zdebug_*, after the section flags is set. */
1001 if ((flags
& SEC_DEBUGGING
)
1002 && ((name
[1] == 'd' && name
[6] == '_')
1003 || (name
[1] == 'z' && name
[7] == '_')))
1005 enum { nothing
, compress
, decompress
} action
= nothing
;
1008 if (bfd_is_section_compressed (abfd
, newsect
))
1010 /* Compressed section. Check if we should decompress. */
1011 if ((abfd
->flags
& BFD_DECOMPRESS
))
1012 action
= decompress
;
1016 /* Normal section. Check if we should compress. */
1017 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
1027 if (!bfd_init_section_compress_status (abfd
, newsect
))
1029 (*_bfd_error_handler
)
1030 (_("%B: unable to initialize compress status for section %s"),
1036 unsigned int len
= strlen (name
);
1038 new_name
= bfd_alloc (abfd
, len
+ 2);
1039 if (new_name
== NULL
)
1043 memcpy (new_name
+ 2, name
+ 1, len
);
1047 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1049 (*_bfd_error_handler
)
1050 (_("%B: unable to initialize decompress status for section %s"),
1056 unsigned int len
= strlen (name
);
1058 new_name
= bfd_alloc (abfd
, len
);
1059 if (new_name
== NULL
)
1062 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1066 if (new_name
!= NULL
)
1067 bfd_rename_section (abfd
, newsect
, new_name
);
1073 const char *const bfd_elf_section_type_names
[] = {
1074 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1075 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1076 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1079 /* ELF relocs are against symbols. If we are producing relocatable
1080 output, and the reloc is against an external symbol, and nothing
1081 has given us any additional addend, the resulting reloc will also
1082 be against the same symbol. In such a case, we don't want to
1083 change anything about the way the reloc is handled, since it will
1084 all be done at final link time. Rather than put special case code
1085 into bfd_perform_relocation, all the reloc types use this howto
1086 function. It just short circuits the reloc if producing
1087 relocatable output against an external symbol. */
1089 bfd_reloc_status_type
1090 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1091 arelent
*reloc_entry
,
1093 void *data ATTRIBUTE_UNUSED
,
1094 asection
*input_section
,
1096 char **error_message ATTRIBUTE_UNUSED
)
1098 if (output_bfd
!= NULL
1099 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1100 && (! reloc_entry
->howto
->partial_inplace
1101 || reloc_entry
->addend
== 0))
1103 reloc_entry
->address
+= input_section
->output_offset
;
1104 return bfd_reloc_ok
;
1107 return bfd_reloc_continue
;
1110 /* Copy the program header and other data from one object module to
1114 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1116 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1117 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1120 BFD_ASSERT (!elf_flags_init (obfd
)
1121 || (elf_elfheader (obfd
)->e_flags
1122 == elf_elfheader (ibfd
)->e_flags
));
1124 elf_gp (obfd
) = elf_gp (ibfd
);
1125 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1126 elf_flags_init (obfd
) = TRUE
;
1128 /* Copy object attributes. */
1129 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1134 get_segment_type (unsigned int p_type
)
1139 case PT_NULL
: pt
= "NULL"; break;
1140 case PT_LOAD
: pt
= "LOAD"; break;
1141 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1142 case PT_INTERP
: pt
= "INTERP"; break;
1143 case PT_NOTE
: pt
= "NOTE"; break;
1144 case PT_SHLIB
: pt
= "SHLIB"; break;
1145 case PT_PHDR
: pt
= "PHDR"; break;
1146 case PT_TLS
: pt
= "TLS"; break;
1147 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1148 case PT_GNU_STACK
: pt
= "STACK"; break;
1149 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1150 default: pt
= NULL
; break;
1155 /* Print out the program headers. */
1158 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1160 FILE *f
= (FILE *) farg
;
1161 Elf_Internal_Phdr
*p
;
1163 bfd_byte
*dynbuf
= NULL
;
1165 p
= elf_tdata (abfd
)->phdr
;
1170 fprintf (f
, _("\nProgram Header:\n"));
1171 c
= elf_elfheader (abfd
)->e_phnum
;
1172 for (i
= 0; i
< c
; i
++, p
++)
1174 const char *pt
= get_segment_type (p
->p_type
);
1179 sprintf (buf
, "0x%lx", p
->p_type
);
1182 fprintf (f
, "%8s off 0x", pt
);
1183 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1184 fprintf (f
, " vaddr 0x");
1185 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1186 fprintf (f
, " paddr 0x");
1187 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1188 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1189 fprintf (f
, " filesz 0x");
1190 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1191 fprintf (f
, " memsz 0x");
1192 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1193 fprintf (f
, " flags %c%c%c",
1194 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1195 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1196 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1197 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1198 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1203 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1206 unsigned int elfsec
;
1207 unsigned long shlink
;
1208 bfd_byte
*extdyn
, *extdynend
;
1210 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1212 fprintf (f
, _("\nDynamic Section:\n"));
1214 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1217 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1218 if (elfsec
== SHN_BAD
)
1220 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1222 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1223 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1226 extdynend
= extdyn
+ s
->size
;
1227 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1229 Elf_Internal_Dyn dyn
;
1230 const char *name
= "";
1232 bfd_boolean stringp
;
1233 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1235 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1237 if (dyn
.d_tag
== DT_NULL
)
1244 if (bed
->elf_backend_get_target_dtag
)
1245 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1247 if (!strcmp (name
, ""))
1249 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1254 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1255 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1256 case DT_PLTGOT
: name
= "PLTGOT"; break;
1257 case DT_HASH
: name
= "HASH"; break;
1258 case DT_STRTAB
: name
= "STRTAB"; break;
1259 case DT_SYMTAB
: name
= "SYMTAB"; break;
1260 case DT_RELA
: name
= "RELA"; break;
1261 case DT_RELASZ
: name
= "RELASZ"; break;
1262 case DT_RELAENT
: name
= "RELAENT"; break;
1263 case DT_STRSZ
: name
= "STRSZ"; break;
1264 case DT_SYMENT
: name
= "SYMENT"; break;
1265 case DT_INIT
: name
= "INIT"; break;
1266 case DT_FINI
: name
= "FINI"; break;
1267 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1268 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1269 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1270 case DT_REL
: name
= "REL"; break;
1271 case DT_RELSZ
: name
= "RELSZ"; break;
1272 case DT_RELENT
: name
= "RELENT"; break;
1273 case DT_PLTREL
: name
= "PLTREL"; break;
1274 case DT_DEBUG
: name
= "DEBUG"; break;
1275 case DT_TEXTREL
: name
= "TEXTREL"; break;
1276 case DT_JMPREL
: name
= "JMPREL"; break;
1277 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1278 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1279 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1280 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1281 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1282 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1283 case DT_FLAGS
: name
= "FLAGS"; break;
1284 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1285 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1286 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1287 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1288 case DT_MOVEENT
: name
= "MOVEENT"; break;
1289 case DT_MOVESZ
: name
= "MOVESZ"; break;
1290 case DT_FEATURE
: name
= "FEATURE"; break;
1291 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1292 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1293 case DT_SYMINENT
: name
= "SYMINENT"; break;
1294 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1295 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1296 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1297 case DT_PLTPAD
: name
= "PLTPAD"; break;
1298 case DT_MOVETAB
: name
= "MOVETAB"; break;
1299 case DT_SYMINFO
: name
= "SYMINFO"; break;
1300 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1301 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1302 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1303 case DT_VERSYM
: name
= "VERSYM"; break;
1304 case DT_VERDEF
: name
= "VERDEF"; break;
1305 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1306 case DT_VERNEED
: name
= "VERNEED"; break;
1307 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1308 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1309 case DT_USED
: name
= "USED"; break;
1310 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1311 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1314 fprintf (f
, " %-20s ", name
);
1318 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1323 unsigned int tagv
= dyn
.d_un
.d_val
;
1325 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1328 fprintf (f
, "%s", string
);
1337 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1338 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1340 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1344 if (elf_dynverdef (abfd
) != 0)
1346 Elf_Internal_Verdef
*t
;
1348 fprintf (f
, _("\nVersion definitions:\n"));
1349 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1351 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1352 t
->vd_flags
, t
->vd_hash
,
1353 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1354 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1356 Elf_Internal_Verdaux
*a
;
1359 for (a
= t
->vd_auxptr
->vda_nextptr
;
1363 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1369 if (elf_dynverref (abfd
) != 0)
1371 Elf_Internal_Verneed
*t
;
1373 fprintf (f
, _("\nVersion References:\n"));
1374 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1376 Elf_Internal_Vernaux
*a
;
1378 fprintf (f
, _(" required from %s:\n"),
1379 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1380 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1381 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1382 a
->vna_flags
, a
->vna_other
,
1383 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1395 /* Display ELF-specific fields of a symbol. */
1398 bfd_elf_print_symbol (bfd
*abfd
,
1401 bfd_print_symbol_type how
)
1403 FILE *file
= (FILE *) filep
;
1406 case bfd_print_symbol_name
:
1407 fprintf (file
, "%s", symbol
->name
);
1409 case bfd_print_symbol_more
:
1410 fprintf (file
, "elf ");
1411 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1412 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1414 case bfd_print_symbol_all
:
1416 const char *section_name
;
1417 const char *name
= NULL
;
1418 const struct elf_backend_data
*bed
;
1419 unsigned char st_other
;
1422 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1424 bed
= get_elf_backend_data (abfd
);
1425 if (bed
->elf_backend_print_symbol_all
)
1426 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1430 name
= symbol
->name
;
1431 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1434 fprintf (file
, " %s\t", section_name
);
1435 /* Print the "other" value for a symbol. For common symbols,
1436 we've already printed the size; now print the alignment.
1437 For other symbols, we have no specified alignment, and
1438 we've printed the address; now print the size. */
1439 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1440 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1442 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1443 bfd_fprintf_vma (abfd
, file
, val
);
1445 /* If we have version information, print it. */
1446 if (elf_dynversym (abfd
) != 0
1447 && (elf_dynverdef (abfd
) != 0
1448 || elf_dynverref (abfd
) != 0))
1450 unsigned int vernum
;
1451 const char *version_string
;
1453 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1456 version_string
= "";
1457 else if (vernum
== 1)
1458 version_string
= "Base";
1459 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1461 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1464 Elf_Internal_Verneed
*t
;
1466 version_string
= "";
1467 for (t
= elf_tdata (abfd
)->verref
;
1471 Elf_Internal_Vernaux
*a
;
1473 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1475 if (a
->vna_other
== vernum
)
1477 version_string
= a
->vna_nodename
;
1484 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1485 fprintf (file
, " %-11s", version_string
);
1490 fprintf (file
, " (%s)", version_string
);
1491 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1496 /* If the st_other field is not zero, print it. */
1497 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1502 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1503 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1504 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1506 /* Some other non-defined flags are also present, so print
1508 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1511 fprintf (file
, " %s", name
);
1517 /* Allocate an ELF string table--force the first byte to be zero. */
1519 struct bfd_strtab_hash
*
1520 _bfd_elf_stringtab_init (void)
1522 struct bfd_strtab_hash
*ret
;
1524 ret
= _bfd_stringtab_init ();
1529 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1530 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1531 if (loc
== (bfd_size_type
) -1)
1533 _bfd_stringtab_free (ret
);
1540 /* ELF .o/exec file reading */
1542 /* Create a new bfd section from an ELF section header. */
1545 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1547 Elf_Internal_Shdr
*hdr
;
1548 Elf_Internal_Ehdr
*ehdr
;
1549 const struct elf_backend_data
*bed
;
1552 if (shindex
>= elf_numsections (abfd
))
1555 hdr
= elf_elfsections (abfd
)[shindex
];
1556 ehdr
= elf_elfheader (abfd
);
1557 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1562 bed
= get_elf_backend_data (abfd
);
1563 switch (hdr
->sh_type
)
1566 /* Inactive section. Throw it away. */
1569 case SHT_PROGBITS
: /* Normal section with contents. */
1570 case SHT_NOBITS
: /* .bss section. */
1571 case SHT_HASH
: /* .hash section. */
1572 case SHT_NOTE
: /* .note section. */
1573 case SHT_INIT_ARRAY
: /* .init_array section. */
1574 case SHT_FINI_ARRAY
: /* .fini_array section. */
1575 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1576 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1577 case SHT_GNU_HASH
: /* .gnu.hash section. */
1578 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1580 case SHT_DYNAMIC
: /* Dynamic linking information. */
1581 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1583 if (hdr
->sh_link
> elf_numsections (abfd
))
1585 /* PR 10478: Accept Solaris binaries with a sh_link
1586 field set to SHN_BEFORE or SHN_AFTER. */
1587 switch (bfd_get_arch (abfd
))
1590 case bfd_arch_sparc
:
1591 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1592 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1594 /* Otherwise fall through. */
1599 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1601 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1603 Elf_Internal_Shdr
*dynsymhdr
;
1605 /* The shared libraries distributed with hpux11 have a bogus
1606 sh_link field for the ".dynamic" section. Find the
1607 string table for the ".dynsym" section instead. */
1608 if (elf_dynsymtab (abfd
) != 0)
1610 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1611 hdr
->sh_link
= dynsymhdr
->sh_link
;
1615 unsigned int i
, num_sec
;
1617 num_sec
= elf_numsections (abfd
);
1618 for (i
= 1; i
< num_sec
; i
++)
1620 dynsymhdr
= elf_elfsections (abfd
)[i
];
1621 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1623 hdr
->sh_link
= dynsymhdr
->sh_link
;
1631 case SHT_SYMTAB
: /* A symbol table */
1632 if (elf_onesymtab (abfd
) == shindex
)
1635 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1637 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1639 if (hdr
->sh_size
!= 0)
1641 /* Some assemblers erroneously set sh_info to one with a
1642 zero sh_size. ld sees this as a global symbol count
1643 of (unsigned) -1. Fix it here. */
1647 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1648 elf_onesymtab (abfd
) = shindex
;
1649 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1650 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1651 abfd
->flags
|= HAS_SYMS
;
1653 /* Sometimes a shared object will map in the symbol table. If
1654 SHF_ALLOC is set, and this is a shared object, then we also
1655 treat this section as a BFD section. We can not base the
1656 decision purely on SHF_ALLOC, because that flag is sometimes
1657 set in a relocatable object file, which would confuse the
1659 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1660 && (abfd
->flags
& DYNAMIC
) != 0
1661 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1665 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1666 can't read symbols without that section loaded as well. It
1667 is most likely specified by the next section header. */
1668 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1670 unsigned int i
, num_sec
;
1672 num_sec
= elf_numsections (abfd
);
1673 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1675 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1676 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1677 && hdr2
->sh_link
== shindex
)
1681 for (i
= 1; i
< shindex
; i
++)
1683 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1684 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1685 && hdr2
->sh_link
== shindex
)
1689 return bfd_section_from_shdr (abfd
, i
);
1693 case SHT_DYNSYM
: /* A dynamic symbol table */
1694 if (elf_dynsymtab (abfd
) == shindex
)
1697 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1699 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1701 if (hdr
->sh_size
!= 0)
1703 /* Some linkers erroneously set sh_info to one with a
1704 zero sh_size. ld sees this as a global symbol count
1705 of (unsigned) -1. Fix it here. */
1709 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1710 elf_dynsymtab (abfd
) = shindex
;
1711 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1712 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1713 abfd
->flags
|= HAS_SYMS
;
1715 /* Besides being a symbol table, we also treat this as a regular
1716 section, so that objcopy can handle it. */
1717 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1719 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1720 if (elf_symtab_shndx (abfd
) == shindex
)
1723 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1724 elf_symtab_shndx (abfd
) = shindex
;
1725 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1726 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1729 case SHT_STRTAB
: /* A string table */
1730 if (hdr
->bfd_section
!= NULL
)
1732 if (ehdr
->e_shstrndx
== shindex
)
1734 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1735 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1738 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1741 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1742 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1745 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1748 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1749 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1750 elf_elfsections (abfd
)[shindex
] = hdr
;
1751 /* We also treat this as a regular section, so that objcopy
1753 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1757 /* If the string table isn't one of the above, then treat it as a
1758 regular section. We need to scan all the headers to be sure,
1759 just in case this strtab section appeared before the above. */
1760 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1762 unsigned int i
, num_sec
;
1764 num_sec
= elf_numsections (abfd
);
1765 for (i
= 1; i
< num_sec
; i
++)
1767 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1768 if (hdr2
->sh_link
== shindex
)
1770 /* Prevent endless recursion on broken objects. */
1773 if (! bfd_section_from_shdr (abfd
, i
))
1775 if (elf_onesymtab (abfd
) == i
)
1777 if (elf_dynsymtab (abfd
) == i
)
1778 goto dynsymtab_strtab
;
1782 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1786 /* *These* do a lot of work -- but build no sections! */
1788 asection
*target_sect
;
1789 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1790 unsigned int num_sec
= elf_numsections (abfd
);
1791 struct bfd_elf_section_data
*esdt
;
1795 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1796 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1799 /* Check for a bogus link to avoid crashing. */
1800 if (hdr
->sh_link
>= num_sec
)
1802 ((*_bfd_error_handler
)
1803 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1804 abfd
, hdr
->sh_link
, name
, shindex
));
1805 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1809 /* For some incomprehensible reason Oracle distributes
1810 libraries for Solaris in which some of the objects have
1811 bogus sh_link fields. It would be nice if we could just
1812 reject them, but, unfortunately, some people need to use
1813 them. We scan through the section headers; if we find only
1814 one suitable symbol table, we clobber the sh_link to point
1815 to it. I hope this doesn't break anything.
1817 Don't do it on executable nor shared library. */
1818 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1819 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1820 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1826 for (scan
= 1; scan
< num_sec
; scan
++)
1828 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1829 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1840 hdr
->sh_link
= found
;
1843 /* Get the symbol table. */
1844 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1845 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1846 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1849 /* If this reloc section does not use the main symbol table we
1850 don't treat it as a reloc section. BFD can't adequately
1851 represent such a section, so at least for now, we don't
1852 try. We just present it as a normal section. We also
1853 can't use it as a reloc section if it points to the null
1854 section, an invalid section, another reloc section, or its
1855 sh_link points to the null section. */
1856 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1857 || hdr
->sh_link
== SHN_UNDEF
1858 || hdr
->sh_info
== SHN_UNDEF
1859 || hdr
->sh_info
>= num_sec
1860 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1861 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1862 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1865 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1867 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1868 if (target_sect
== NULL
)
1871 esdt
= elf_section_data (target_sect
);
1872 if (hdr
->sh_type
== SHT_RELA
)
1873 p_hdr
= &esdt
->rela
.hdr
;
1875 p_hdr
= &esdt
->rel
.hdr
;
1877 BFD_ASSERT (*p_hdr
== NULL
);
1878 amt
= sizeof (*hdr2
);
1879 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1884 elf_elfsections (abfd
)[shindex
] = hdr2
;
1885 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1886 target_sect
->flags
|= SEC_RELOC
;
1887 target_sect
->relocation
= NULL
;
1888 target_sect
->rel_filepos
= hdr
->sh_offset
;
1889 /* In the section to which the relocations apply, mark whether
1890 its relocations are of the REL or RELA variety. */
1891 if (hdr
->sh_size
!= 0)
1893 if (hdr
->sh_type
== SHT_RELA
)
1894 target_sect
->use_rela_p
= 1;
1896 abfd
->flags
|= HAS_RELOC
;
1900 case SHT_GNU_verdef
:
1901 elf_dynverdef (abfd
) = shindex
;
1902 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1903 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1905 case SHT_GNU_versym
:
1906 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1908 elf_dynversym (abfd
) = shindex
;
1909 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1910 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1912 case SHT_GNU_verneed
:
1913 elf_dynverref (abfd
) = shindex
;
1914 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1915 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1921 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
1923 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1925 if (hdr
->contents
!= NULL
)
1927 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1928 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1931 if (idx
->flags
& GRP_COMDAT
)
1932 hdr
->bfd_section
->flags
1933 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1935 /* We try to keep the same section order as it comes in. */
1937 while (--n_elt
!= 0)
1941 if (idx
->shdr
!= NULL
1942 && (s
= idx
->shdr
->bfd_section
) != NULL
1943 && elf_next_in_group (s
) != NULL
)
1945 elf_next_in_group (hdr
->bfd_section
) = s
;
1953 /* Possibly an attributes section. */
1954 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1955 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1957 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1959 _bfd_elf_parse_attributes (abfd
, hdr
);
1963 /* Check for any processor-specific section types. */
1964 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1967 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1969 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1970 /* FIXME: How to properly handle allocated section reserved
1971 for applications? */
1972 (*_bfd_error_handler
)
1973 (_("%B: don't know how to handle allocated, application "
1974 "specific section `%s' [0x%8x]"),
1975 abfd
, name
, hdr
->sh_type
);
1977 /* Allow sections reserved for applications. */
1978 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1981 else if (hdr
->sh_type
>= SHT_LOPROC
1982 && hdr
->sh_type
<= SHT_HIPROC
)
1983 /* FIXME: We should handle this section. */
1984 (*_bfd_error_handler
)
1985 (_("%B: don't know how to handle processor specific section "
1987 abfd
, name
, hdr
->sh_type
);
1988 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1990 /* Unrecognised OS-specific sections. */
1991 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1992 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1993 required to correctly process the section and the file should
1994 be rejected with an error message. */
1995 (*_bfd_error_handler
)
1996 (_("%B: don't know how to handle OS specific section "
1998 abfd
, name
, hdr
->sh_type
);
2000 /* Otherwise it should be processed. */
2001 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2004 /* FIXME: We should handle this section. */
2005 (*_bfd_error_handler
)
2006 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2007 abfd
, name
, hdr
->sh_type
);
2015 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2018 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2020 unsigned long r_symndx
)
2022 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2024 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2026 Elf_Internal_Shdr
*symtab_hdr
;
2027 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2028 Elf_External_Sym_Shndx eshndx
;
2030 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2031 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2032 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2035 if (cache
->abfd
!= abfd
)
2037 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2040 cache
->indx
[ent
] = r_symndx
;
2043 return &cache
->sym
[ent
];
2046 /* Given an ELF section number, retrieve the corresponding BFD
2050 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2052 if (sec_index
>= elf_numsections (abfd
))
2054 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2057 static const struct bfd_elf_special_section special_sections_b
[] =
2059 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2060 { NULL
, 0, 0, 0, 0 }
2063 static const struct bfd_elf_special_section special_sections_c
[] =
2065 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2066 { NULL
, 0, 0, 0, 0 }
2069 static const struct bfd_elf_special_section special_sections_d
[] =
2071 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2072 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2073 /* There are more DWARF sections than these, but they needn't be added here
2074 unless you have to cope with broken compilers that don't emit section
2075 attributes or you want to help the user writing assembler. */
2076 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2077 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2078 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2079 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2080 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2081 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2082 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2083 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2084 { NULL
, 0, 0, 0, 0 }
2087 static const struct bfd_elf_special_section special_sections_f
[] =
2089 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2090 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2091 { NULL
, 0, 0, 0, 0 }
2094 static const struct bfd_elf_special_section special_sections_g
[] =
2096 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2097 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2098 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2099 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2100 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2101 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2102 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2103 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2104 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2105 { NULL
, 0, 0, 0, 0 }
2108 static const struct bfd_elf_special_section special_sections_h
[] =
2110 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2111 { NULL
, 0, 0, 0, 0 }
2114 static const struct bfd_elf_special_section special_sections_i
[] =
2116 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2117 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2118 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2119 { NULL
, 0, 0, 0, 0 }
2122 static const struct bfd_elf_special_section special_sections_l
[] =
2124 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2125 { NULL
, 0, 0, 0, 0 }
2128 static const struct bfd_elf_special_section special_sections_n
[] =
2130 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2131 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2132 { NULL
, 0, 0, 0, 0 }
2135 static const struct bfd_elf_special_section special_sections_p
[] =
2137 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2138 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2139 { NULL
, 0, 0, 0, 0 }
2142 static const struct bfd_elf_special_section special_sections_r
[] =
2144 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2145 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2146 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2147 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2148 { NULL
, 0, 0, 0, 0 }
2151 static const struct bfd_elf_special_section special_sections_s
[] =
2153 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2154 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2155 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2156 /* See struct bfd_elf_special_section declaration for the semantics of
2157 this special case where .prefix_length != strlen (.prefix). */
2158 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2159 { NULL
, 0, 0, 0, 0 }
2162 static const struct bfd_elf_special_section special_sections_t
[] =
2164 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2165 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2166 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2167 { NULL
, 0, 0, 0, 0 }
2170 static const struct bfd_elf_special_section special_sections_z
[] =
2172 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2173 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2174 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2175 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2176 { NULL
, 0, 0, 0, 0 }
2179 static const struct bfd_elf_special_section
* const special_sections
[] =
2181 special_sections_b
, /* 'b' */
2182 special_sections_c
, /* 'c' */
2183 special_sections_d
, /* 'd' */
2185 special_sections_f
, /* 'f' */
2186 special_sections_g
, /* 'g' */
2187 special_sections_h
, /* 'h' */
2188 special_sections_i
, /* 'i' */
2191 special_sections_l
, /* 'l' */
2193 special_sections_n
, /* 'n' */
2195 special_sections_p
, /* 'p' */
2197 special_sections_r
, /* 'r' */
2198 special_sections_s
, /* 's' */
2199 special_sections_t
, /* 't' */
2205 special_sections_z
/* 'z' */
2208 const struct bfd_elf_special_section
*
2209 _bfd_elf_get_special_section (const char *name
,
2210 const struct bfd_elf_special_section
*spec
,
2216 len
= strlen (name
);
2218 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2221 int prefix_len
= spec
[i
].prefix_length
;
2223 if (len
< prefix_len
)
2225 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2228 suffix_len
= spec
[i
].suffix_length
;
2229 if (suffix_len
<= 0)
2231 if (name
[prefix_len
] != 0)
2233 if (suffix_len
== 0)
2235 if (name
[prefix_len
] != '.'
2236 && (suffix_len
== -2
2237 || (rela
&& spec
[i
].type
== SHT_REL
)))
2243 if (len
< prefix_len
+ suffix_len
)
2245 if (memcmp (name
+ len
- suffix_len
,
2246 spec
[i
].prefix
+ prefix_len
,
2256 const struct bfd_elf_special_section
*
2257 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2260 const struct bfd_elf_special_section
*spec
;
2261 const struct elf_backend_data
*bed
;
2263 /* See if this is one of the special sections. */
2264 if (sec
->name
== NULL
)
2267 bed
= get_elf_backend_data (abfd
);
2268 spec
= bed
->special_sections
;
2271 spec
= _bfd_elf_get_special_section (sec
->name
,
2272 bed
->special_sections
,
2278 if (sec
->name
[0] != '.')
2281 i
= sec
->name
[1] - 'b';
2282 if (i
< 0 || i
> 'z' - 'b')
2285 spec
= special_sections
[i
];
2290 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2294 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2296 struct bfd_elf_section_data
*sdata
;
2297 const struct elf_backend_data
*bed
;
2298 const struct bfd_elf_special_section
*ssect
;
2300 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2303 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2307 sec
->used_by_bfd
= sdata
;
2310 /* Indicate whether or not this section should use RELA relocations. */
2311 bed
= get_elf_backend_data (abfd
);
2312 sec
->use_rela_p
= bed
->default_use_rela_p
;
2314 /* When we read a file, we don't need to set ELF section type and
2315 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2316 anyway. We will set ELF section type and flags for all linker
2317 created sections. If user specifies BFD section flags, we will
2318 set ELF section type and flags based on BFD section flags in
2319 elf_fake_sections. Special handling for .init_array/.fini_array
2320 output sections since they may contain .ctors/.dtors input
2321 sections. We don't want _bfd_elf_init_private_section_data to
2322 copy ELF section type from .ctors/.dtors input sections. */
2323 if (abfd
->direction
!= read_direction
2324 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2326 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2329 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2330 || ssect
->type
== SHT_INIT_ARRAY
2331 || ssect
->type
== SHT_FINI_ARRAY
))
2333 elf_section_type (sec
) = ssect
->type
;
2334 elf_section_flags (sec
) = ssect
->attr
;
2338 return _bfd_generic_new_section_hook (abfd
, sec
);
2341 /* Create a new bfd section from an ELF program header.
2343 Since program segments have no names, we generate a synthetic name
2344 of the form segment<NUM>, where NUM is generally the index in the
2345 program header table. For segments that are split (see below) we
2346 generate the names segment<NUM>a and segment<NUM>b.
2348 Note that some program segments may have a file size that is different than
2349 (less than) the memory size. All this means is that at execution the
2350 system must allocate the amount of memory specified by the memory size,
2351 but only initialize it with the first "file size" bytes read from the
2352 file. This would occur for example, with program segments consisting
2353 of combined data+bss.
2355 To handle the above situation, this routine generates TWO bfd sections
2356 for the single program segment. The first has the length specified by
2357 the file size of the segment, and the second has the length specified
2358 by the difference between the two sizes. In effect, the segment is split
2359 into its initialized and uninitialized parts.
2364 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2365 Elf_Internal_Phdr
*hdr
,
2367 const char *type_name
)
2375 split
= ((hdr
->p_memsz
> 0)
2376 && (hdr
->p_filesz
> 0)
2377 && (hdr
->p_memsz
> hdr
->p_filesz
));
2379 if (hdr
->p_filesz
> 0)
2381 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2382 len
= strlen (namebuf
) + 1;
2383 name
= (char *) bfd_alloc (abfd
, len
);
2386 memcpy (name
, namebuf
, len
);
2387 newsect
= bfd_make_section (abfd
, name
);
2388 if (newsect
== NULL
)
2390 newsect
->vma
= hdr
->p_vaddr
;
2391 newsect
->lma
= hdr
->p_paddr
;
2392 newsect
->size
= hdr
->p_filesz
;
2393 newsect
->filepos
= hdr
->p_offset
;
2394 newsect
->flags
|= SEC_HAS_CONTENTS
;
2395 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2396 if (hdr
->p_type
== PT_LOAD
)
2398 newsect
->flags
|= SEC_ALLOC
;
2399 newsect
->flags
|= SEC_LOAD
;
2400 if (hdr
->p_flags
& PF_X
)
2402 /* FIXME: all we known is that it has execute PERMISSION,
2404 newsect
->flags
|= SEC_CODE
;
2407 if (!(hdr
->p_flags
& PF_W
))
2409 newsect
->flags
|= SEC_READONLY
;
2413 if (hdr
->p_memsz
> hdr
->p_filesz
)
2417 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2418 len
= strlen (namebuf
) + 1;
2419 name
= (char *) bfd_alloc (abfd
, len
);
2422 memcpy (name
, namebuf
, len
);
2423 newsect
= bfd_make_section (abfd
, name
);
2424 if (newsect
== NULL
)
2426 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2427 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2428 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2429 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2430 align
= newsect
->vma
& -newsect
->vma
;
2431 if (align
== 0 || align
> hdr
->p_align
)
2432 align
= hdr
->p_align
;
2433 newsect
->alignment_power
= bfd_log2 (align
);
2434 if (hdr
->p_type
== PT_LOAD
)
2436 /* Hack for gdb. Segments that have not been modified do
2437 not have their contents written to a core file, on the
2438 assumption that a debugger can find the contents in the
2439 executable. We flag this case by setting the fake
2440 section size to zero. Note that "real" bss sections will
2441 always have their contents dumped to the core file. */
2442 if (bfd_get_format (abfd
) == bfd_core
)
2444 newsect
->flags
|= SEC_ALLOC
;
2445 if (hdr
->p_flags
& PF_X
)
2446 newsect
->flags
|= SEC_CODE
;
2448 if (!(hdr
->p_flags
& PF_W
))
2449 newsect
->flags
|= SEC_READONLY
;
2456 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2458 const struct elf_backend_data
*bed
;
2460 switch (hdr
->p_type
)
2463 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2466 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2469 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2472 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2475 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2477 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2482 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2485 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2487 case PT_GNU_EH_FRAME
:
2488 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2492 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2495 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2498 /* Check for any processor-specific program segment types. */
2499 bed
= get_elf_backend_data (abfd
);
2500 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2504 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2508 _bfd_elf_single_rel_hdr (asection
*sec
)
2510 if (elf_section_data (sec
)->rel
.hdr
)
2512 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2513 return elf_section_data (sec
)->rel
.hdr
;
2516 return elf_section_data (sec
)->rela
.hdr
;
2519 /* Allocate and initialize a section-header for a new reloc section,
2520 containing relocations against ASECT. It is stored in RELDATA. If
2521 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2525 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2526 struct bfd_elf_section_reloc_data
*reldata
,
2528 bfd_boolean use_rela_p
)
2530 Elf_Internal_Shdr
*rel_hdr
;
2532 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2535 amt
= sizeof (Elf_Internal_Shdr
);
2536 BFD_ASSERT (reldata
->hdr
== NULL
);
2537 rel_hdr
= bfd_zalloc (abfd
, amt
);
2538 reldata
->hdr
= rel_hdr
;
2540 amt
= sizeof ".rela" + strlen (asect
->name
);
2541 name
= (char *) bfd_alloc (abfd
, amt
);
2544 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2546 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2548 if (rel_hdr
->sh_name
== (unsigned int) -1)
2550 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2551 rel_hdr
->sh_entsize
= (use_rela_p
2552 ? bed
->s
->sizeof_rela
2553 : bed
->s
->sizeof_rel
);
2554 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2555 rel_hdr
->sh_flags
= 0;
2556 rel_hdr
->sh_addr
= 0;
2557 rel_hdr
->sh_size
= 0;
2558 rel_hdr
->sh_offset
= 0;
2563 /* Return the default section type based on the passed in section flags. */
2566 bfd_elf_get_default_section_type (flagword flags
)
2568 if ((flags
& SEC_ALLOC
) != 0
2569 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2571 return SHT_PROGBITS
;
2574 struct fake_section_arg
2576 struct bfd_link_info
*link_info
;
2580 /* Set up an ELF internal section header for a section. */
2583 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2585 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2586 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2587 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2588 Elf_Internal_Shdr
*this_hdr
;
2589 unsigned int sh_type
;
2593 /* We already failed; just get out of the bfd_map_over_sections
2598 this_hdr
= &esd
->this_hdr
;
2600 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2601 asect
->name
, FALSE
);
2602 if (this_hdr
->sh_name
== (unsigned int) -1)
2608 /* Don't clear sh_flags. Assembler may set additional bits. */
2610 if ((asect
->flags
& SEC_ALLOC
) != 0
2611 || asect
->user_set_vma
)
2612 this_hdr
->sh_addr
= asect
->vma
;
2614 this_hdr
->sh_addr
= 0;
2616 this_hdr
->sh_offset
= 0;
2617 this_hdr
->sh_size
= asect
->size
;
2618 this_hdr
->sh_link
= 0;
2619 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2620 /* The sh_entsize and sh_info fields may have been set already by
2621 copy_private_section_data. */
2623 this_hdr
->bfd_section
= asect
;
2624 this_hdr
->contents
= NULL
;
2626 /* If the section type is unspecified, we set it based on
2628 if ((asect
->flags
& SEC_GROUP
) != 0)
2629 sh_type
= SHT_GROUP
;
2631 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2633 if (this_hdr
->sh_type
== SHT_NULL
)
2634 this_hdr
->sh_type
= sh_type
;
2635 else if (this_hdr
->sh_type
== SHT_NOBITS
2636 && sh_type
== SHT_PROGBITS
2637 && (asect
->flags
& SEC_ALLOC
) != 0)
2639 /* Warn if we are changing a NOBITS section to PROGBITS, but
2640 allow the link to proceed. This can happen when users link
2641 non-bss input sections to bss output sections, or emit data
2642 to a bss output section via a linker script. */
2643 (*_bfd_error_handler
)
2644 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2645 this_hdr
->sh_type
= sh_type
;
2648 switch (this_hdr
->sh_type
)
2654 case SHT_INIT_ARRAY
:
2655 case SHT_FINI_ARRAY
:
2656 case SHT_PREINIT_ARRAY
:
2663 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2667 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2671 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2675 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2676 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2680 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2681 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2684 case SHT_GNU_versym
:
2685 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2688 case SHT_GNU_verdef
:
2689 this_hdr
->sh_entsize
= 0;
2690 /* objcopy or strip will copy over sh_info, but may not set
2691 cverdefs. The linker will set cverdefs, but sh_info will be
2693 if (this_hdr
->sh_info
== 0)
2694 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2696 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2697 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2700 case SHT_GNU_verneed
:
2701 this_hdr
->sh_entsize
= 0;
2702 /* objcopy or strip will copy over sh_info, but may not set
2703 cverrefs. The linker will set cverrefs, but sh_info will be
2705 if (this_hdr
->sh_info
== 0)
2706 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2708 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2709 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2713 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2717 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2721 if ((asect
->flags
& SEC_ALLOC
) != 0)
2722 this_hdr
->sh_flags
|= SHF_ALLOC
;
2723 if ((asect
->flags
& SEC_READONLY
) == 0)
2724 this_hdr
->sh_flags
|= SHF_WRITE
;
2725 if ((asect
->flags
& SEC_CODE
) != 0)
2726 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2727 if ((asect
->flags
& SEC_MERGE
) != 0)
2729 this_hdr
->sh_flags
|= SHF_MERGE
;
2730 this_hdr
->sh_entsize
= asect
->entsize
;
2731 if ((asect
->flags
& SEC_STRINGS
) != 0)
2732 this_hdr
->sh_flags
|= SHF_STRINGS
;
2734 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2735 this_hdr
->sh_flags
|= SHF_GROUP
;
2736 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2738 this_hdr
->sh_flags
|= SHF_TLS
;
2739 if (asect
->size
== 0
2740 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2742 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2744 this_hdr
->sh_size
= 0;
2747 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2748 if (this_hdr
->sh_size
!= 0)
2749 this_hdr
->sh_type
= SHT_NOBITS
;
2753 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2754 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2756 /* If the section has relocs, set up a section header for the
2757 SHT_REL[A] section. If two relocation sections are required for
2758 this section, it is up to the processor-specific back-end to
2759 create the other. */
2760 if ((asect
->flags
& SEC_RELOC
) != 0)
2762 /* When doing a relocatable link, create both REL and RELA sections if
2765 /* Do the normal setup if we wouldn't create any sections here. */
2766 && esd
->rel
.count
+ esd
->rela
.count
> 0
2767 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2769 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2770 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2775 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2776 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2782 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2784 ? &esd
->rela
: &esd
->rel
),
2790 /* Check for processor-specific section types. */
2791 sh_type
= this_hdr
->sh_type
;
2792 if (bed
->elf_backend_fake_sections
2793 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2796 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2798 /* Don't change the header type from NOBITS if we are being
2799 called for objcopy --only-keep-debug. */
2800 this_hdr
->sh_type
= sh_type
;
2804 /* Fill in the contents of a SHT_GROUP section. Called from
2805 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2806 when ELF targets use the generic linker, ld. Called for ld -r
2807 from bfd_elf_final_link. */
2810 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2812 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2813 asection
*elt
, *first
;
2817 /* Ignore linker created group section. See elfNN_ia64_object_p in
2819 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2823 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2825 unsigned long symindx
= 0;
2827 /* elf_group_id will have been set up by objcopy and the
2829 if (elf_group_id (sec
) != NULL
)
2830 symindx
= elf_group_id (sec
)->udata
.i
;
2834 /* If called from the assembler, swap_out_syms will have set up
2835 elf_section_syms. */
2836 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2837 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2839 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2841 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2843 /* The ELF backend linker sets sh_info to -2 when the group
2844 signature symbol is global, and thus the index can't be
2845 set until all local symbols are output. */
2846 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2847 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2848 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2849 unsigned long extsymoff
= 0;
2850 struct elf_link_hash_entry
*h
;
2852 if (!elf_bad_symtab (igroup
->owner
))
2854 Elf_Internal_Shdr
*symtab_hdr
;
2856 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2857 extsymoff
= symtab_hdr
->sh_info
;
2859 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2860 while (h
->root
.type
== bfd_link_hash_indirect
2861 || h
->root
.type
== bfd_link_hash_warning
)
2862 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2864 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2867 /* The contents won't be allocated for "ld -r" or objcopy. */
2869 if (sec
->contents
== NULL
)
2872 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2874 /* Arrange for the section to be written out. */
2875 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2876 if (sec
->contents
== NULL
)
2883 loc
= sec
->contents
+ sec
->size
;
2885 /* Get the pointer to the first section in the group that gas
2886 squirreled away here. objcopy arranges for this to be set to the
2887 start of the input section group. */
2888 first
= elt
= elf_next_in_group (sec
);
2890 /* First element is a flag word. Rest of section is elf section
2891 indices for all the sections of the group. Write them backwards
2892 just to keep the group in the same order as given in .section
2893 directives, not that it matters. */
2900 s
= s
->output_section
;
2902 && !bfd_is_abs_section (s
))
2904 unsigned int idx
= elf_section_data (s
)->this_idx
;
2907 H_PUT_32 (abfd
, idx
, loc
);
2909 elt
= elf_next_in_group (elt
);
2914 if ((loc
-= 4) != sec
->contents
)
2917 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2920 /* Assign all ELF section numbers. The dummy first section is handled here
2921 too. The link/info pointers for the standard section types are filled
2922 in here too, while we're at it. */
2925 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2927 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2929 unsigned int section_number
, secn
;
2930 Elf_Internal_Shdr
**i_shdrp
;
2931 struct bfd_elf_section_data
*d
;
2932 bfd_boolean need_symtab
;
2936 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2938 /* SHT_GROUP sections are in relocatable files only. */
2939 if (link_info
== NULL
|| link_info
->relocatable
)
2941 /* Put SHT_GROUP sections first. */
2942 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2944 d
= elf_section_data (sec
);
2946 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2948 if (sec
->flags
& SEC_LINKER_CREATED
)
2950 /* Remove the linker created SHT_GROUP sections. */
2951 bfd_section_list_remove (abfd
, sec
);
2952 abfd
->section_count
--;
2955 d
->this_idx
= section_number
++;
2960 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2962 d
= elf_section_data (sec
);
2964 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2965 d
->this_idx
= section_number
++;
2966 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2969 d
->rel
.idx
= section_number
++;
2970 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
2977 d
->rela
.idx
= section_number
++;
2978 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
2984 elf_shstrtab_sec (abfd
) = section_number
++;
2985 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2986 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
2988 need_symtab
= (bfd_get_symcount (abfd
) > 0
2989 || (link_info
== NULL
2990 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
2994 elf_onesymtab (abfd
) = section_number
++;
2995 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2996 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
2998 elf_symtab_shndx (abfd
) = section_number
++;
2999 t
->symtab_shndx_hdr
.sh_name
3000 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3001 ".symtab_shndx", FALSE
);
3002 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3005 elf_strtab_sec (abfd
) = section_number
++;
3006 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3009 if (section_number
>= SHN_LORESERVE
)
3011 _bfd_error_handler (_("%B: too many sections: %u"),
3012 abfd
, section_number
);
3016 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
3017 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3019 elf_numsections (abfd
) = section_number
;
3020 elf_elfheader (abfd
)->e_shnum
= section_number
;
3022 /* Set up the list of section header pointers, in agreement with the
3024 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3025 sizeof (Elf_Internal_Shdr
*));
3026 if (i_shdrp
== NULL
)
3029 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3030 sizeof (Elf_Internal_Shdr
));
3031 if (i_shdrp
[0] == NULL
)
3033 bfd_release (abfd
, i_shdrp
);
3037 elf_elfsections (abfd
) = i_shdrp
;
3039 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3042 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3043 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3045 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3046 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3048 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3049 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3052 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3057 d
= elf_section_data (sec
);
3059 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3060 if (d
->rel
.idx
!= 0)
3061 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3062 if (d
->rela
.idx
!= 0)
3063 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3065 /* Fill in the sh_link and sh_info fields while we're at it. */
3067 /* sh_link of a reloc section is the section index of the symbol
3068 table. sh_info is the section index of the section to which
3069 the relocation entries apply. */
3070 if (d
->rel
.idx
!= 0)
3072 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3073 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3075 if (d
->rela
.idx
!= 0)
3077 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3078 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3081 /* We need to set up sh_link for SHF_LINK_ORDER. */
3082 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3084 s
= elf_linked_to_section (sec
);
3087 /* elf_linked_to_section points to the input section. */
3088 if (link_info
!= NULL
)
3090 /* Check discarded linkonce section. */
3091 if (discarded_section (s
))
3094 (*_bfd_error_handler
)
3095 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3096 abfd
, d
->this_hdr
.bfd_section
,
3098 /* Point to the kept section if it has the same
3099 size as the discarded one. */
3100 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3103 bfd_set_error (bfd_error_bad_value
);
3109 s
= s
->output_section
;
3110 BFD_ASSERT (s
!= NULL
);
3114 /* Handle objcopy. */
3115 if (s
->output_section
== NULL
)
3117 (*_bfd_error_handler
)
3118 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3119 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3120 bfd_set_error (bfd_error_bad_value
);
3123 s
= s
->output_section
;
3125 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3130 The Intel C compiler generates SHT_IA_64_UNWIND with
3131 SHF_LINK_ORDER. But it doesn't set the sh_link or
3132 sh_info fields. Hence we could get the situation
3134 const struct elf_backend_data
*bed
3135 = get_elf_backend_data (abfd
);
3136 if (bed
->link_order_error_handler
)
3137 bed
->link_order_error_handler
3138 (_("%B: warning: sh_link not set for section `%A'"),
3143 switch (d
->this_hdr
.sh_type
)
3147 /* A reloc section which we are treating as a normal BFD
3148 section. sh_link is the section index of the symbol
3149 table. sh_info is the section index of the section to
3150 which the relocation entries apply. We assume that an
3151 allocated reloc section uses the dynamic symbol table.
3152 FIXME: How can we be sure? */
3153 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3155 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3157 /* We look up the section the relocs apply to by name. */
3159 if (d
->this_hdr
.sh_type
== SHT_REL
)
3163 s
= bfd_get_section_by_name (abfd
, name
);
3165 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3169 /* We assume that a section named .stab*str is a stabs
3170 string section. We look for a section with the same name
3171 but without the trailing ``str'', and set its sh_link
3172 field to point to this section. */
3173 if (CONST_STRNEQ (sec
->name
, ".stab")
3174 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3179 len
= strlen (sec
->name
);
3180 alc
= (char *) bfd_malloc (len
- 2);
3183 memcpy (alc
, sec
->name
, len
- 3);
3184 alc
[len
- 3] = '\0';
3185 s
= bfd_get_section_by_name (abfd
, alc
);
3189 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3191 /* This is a .stab section. */
3192 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3193 elf_section_data (s
)->this_hdr
.sh_entsize
3194 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3201 case SHT_GNU_verneed
:
3202 case SHT_GNU_verdef
:
3203 /* sh_link is the section header index of the string table
3204 used for the dynamic entries, or the symbol table, or the
3206 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3208 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3211 case SHT_GNU_LIBLIST
:
3212 /* sh_link is the section header index of the prelink library
3213 list used for the dynamic entries, or the symbol table, or
3214 the version strings. */
3215 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3216 ? ".dynstr" : ".gnu.libstr");
3218 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3223 case SHT_GNU_versym
:
3224 /* sh_link is the section header index of the symbol table
3225 this hash table or version table is for. */
3226 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3228 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3232 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3236 for (secn
= 1; secn
< section_number
; ++secn
)
3237 if (i_shdrp
[secn
] == NULL
)
3238 i_shdrp
[secn
] = i_shdrp
[0];
3240 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3241 i_shdrp
[secn
]->sh_name
);
3246 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3248 /* If the backend has a special mapping, use it. */
3249 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3250 if (bed
->elf_backend_sym_is_global
)
3251 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3253 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3254 || bfd_is_und_section (bfd_get_section (sym
))
3255 || bfd_is_com_section (bfd_get_section (sym
)));
3258 /* Don't output section symbols for sections that are not going to be
3259 output, that are duplicates or there is no BFD section. */
3262 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3264 elf_symbol_type
*type_ptr
;
3266 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3269 type_ptr
= elf_symbol_from (abfd
, sym
);
3270 return ((type_ptr
!= NULL
3271 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3272 && bfd_is_abs_section (sym
->section
))
3273 || !(sym
->section
->owner
== abfd
3274 || (sym
->section
->output_section
->owner
== abfd
3275 && sym
->section
->output_offset
== 0)
3276 || bfd_is_abs_section (sym
->section
)));
3279 /* Map symbol from it's internal number to the external number, moving
3280 all local symbols to be at the head of the list. */
3283 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3285 unsigned int symcount
= bfd_get_symcount (abfd
);
3286 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3287 asymbol
**sect_syms
;
3288 unsigned int num_locals
= 0;
3289 unsigned int num_globals
= 0;
3290 unsigned int num_locals2
= 0;
3291 unsigned int num_globals2
= 0;
3298 fprintf (stderr
, "elf_map_symbols\n");
3302 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3304 if (max_index
< asect
->index
)
3305 max_index
= asect
->index
;
3309 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3310 if (sect_syms
== NULL
)
3312 elf_section_syms (abfd
) = sect_syms
;
3313 elf_num_section_syms (abfd
) = max_index
;
3315 /* Init sect_syms entries for any section symbols we have already
3316 decided to output. */
3317 for (idx
= 0; idx
< symcount
; idx
++)
3319 asymbol
*sym
= syms
[idx
];
3321 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3323 && !ignore_section_sym (abfd
, sym
)
3324 && !bfd_is_abs_section (sym
->section
))
3326 asection
*sec
= sym
->section
;
3328 if (sec
->owner
!= abfd
)
3329 sec
= sec
->output_section
;
3331 sect_syms
[sec
->index
] = syms
[idx
];
3335 /* Classify all of the symbols. */
3336 for (idx
= 0; idx
< symcount
; idx
++)
3338 if (sym_is_global (abfd
, syms
[idx
]))
3340 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3344 /* We will be adding a section symbol for each normal BFD section. Most
3345 sections will already have a section symbol in outsymbols, but
3346 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3347 at least in that case. */
3348 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3350 if (sect_syms
[asect
->index
] == NULL
)
3352 if (!sym_is_global (abfd
, asect
->symbol
))
3359 /* Now sort the symbols so the local symbols are first. */
3360 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3361 sizeof (asymbol
*));
3363 if (new_syms
== NULL
)
3366 for (idx
= 0; idx
< symcount
; idx
++)
3368 asymbol
*sym
= syms
[idx
];
3371 if (sym_is_global (abfd
, sym
))
3372 i
= num_locals
+ num_globals2
++;
3373 else if (!ignore_section_sym (abfd
, sym
))
3378 sym
->udata
.i
= i
+ 1;
3380 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3382 if (sect_syms
[asect
->index
] == NULL
)
3384 asymbol
*sym
= asect
->symbol
;
3387 sect_syms
[asect
->index
] = sym
;
3388 if (!sym_is_global (abfd
, sym
))
3391 i
= num_locals
+ num_globals2
++;
3393 sym
->udata
.i
= i
+ 1;
3397 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3399 *pnum_locals
= num_locals
;
3403 /* Align to the maximum file alignment that could be required for any
3404 ELF data structure. */
3406 static inline file_ptr
3407 align_file_position (file_ptr off
, int align
)
3409 return (off
+ align
- 1) & ~(align
- 1);
3412 /* Assign a file position to a section, optionally aligning to the
3413 required section alignment. */
3416 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3420 if (align
&& i_shdrp
->sh_addralign
> 1)
3421 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3422 i_shdrp
->sh_offset
= offset
;
3423 if (i_shdrp
->bfd_section
!= NULL
)
3424 i_shdrp
->bfd_section
->filepos
= offset
;
3425 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3426 offset
+= i_shdrp
->sh_size
;
3430 /* Compute the file positions we are going to put the sections at, and
3431 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3432 is not NULL, this is being called by the ELF backend linker. */
3435 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3436 struct bfd_link_info
*link_info
)
3438 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3439 struct fake_section_arg fsargs
;
3441 struct bfd_strtab_hash
*strtab
= NULL
;
3442 Elf_Internal_Shdr
*shstrtab_hdr
;
3443 bfd_boolean need_symtab
;
3445 if (abfd
->output_has_begun
)
3448 /* Do any elf backend specific processing first. */
3449 if (bed
->elf_backend_begin_write_processing
)
3450 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3452 if (! prep_headers (abfd
))
3455 /* Post process the headers if necessary. */
3456 if (bed
->elf_backend_post_process_headers
)
3457 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3459 fsargs
.failed
= FALSE
;
3460 fsargs
.link_info
= link_info
;
3461 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3465 if (!assign_section_numbers (abfd
, link_info
))
3468 /* The backend linker builds symbol table information itself. */
3469 need_symtab
= (link_info
== NULL
3470 && (bfd_get_symcount (abfd
) > 0
3471 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3475 /* Non-zero if doing a relocatable link. */
3476 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3478 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3483 if (link_info
== NULL
)
3485 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3490 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3491 /* sh_name was set in prep_headers. */
3492 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3493 shstrtab_hdr
->sh_flags
= 0;
3494 shstrtab_hdr
->sh_addr
= 0;
3495 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3496 shstrtab_hdr
->sh_entsize
= 0;
3497 shstrtab_hdr
->sh_link
= 0;
3498 shstrtab_hdr
->sh_info
= 0;
3499 /* sh_offset is set in assign_file_positions_except_relocs. */
3500 shstrtab_hdr
->sh_addralign
= 1;
3502 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3508 Elf_Internal_Shdr
*hdr
;
3510 off
= elf_next_file_pos (abfd
);
3512 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3513 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3515 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3516 if (hdr
->sh_size
!= 0)
3517 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3519 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3520 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3522 elf_next_file_pos (abfd
) = off
;
3524 /* Now that we know where the .strtab section goes, write it
3526 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3527 || ! _bfd_stringtab_emit (abfd
, strtab
))
3529 _bfd_stringtab_free (strtab
);
3532 abfd
->output_has_begun
= TRUE
;
3537 /* Make an initial estimate of the size of the program header. If we
3538 get the number wrong here, we'll redo section placement. */
3540 static bfd_size_type
3541 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3545 const struct elf_backend_data
*bed
;
3547 /* Assume we will need exactly two PT_LOAD segments: one for text
3548 and one for data. */
3551 s
= bfd_get_section_by_name (abfd
, ".interp");
3552 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3554 /* If we have a loadable interpreter section, we need a
3555 PT_INTERP segment. In this case, assume we also need a
3556 PT_PHDR segment, although that may not be true for all
3561 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3563 /* We need a PT_DYNAMIC segment. */
3567 if (info
!= NULL
&& info
->relro
)
3569 /* We need a PT_GNU_RELRO segment. */
3573 if (elf_eh_frame_hdr (abfd
))
3575 /* We need a PT_GNU_EH_FRAME segment. */
3579 if (elf_stack_flags (abfd
))
3581 /* We need a PT_GNU_STACK segment. */
3585 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3587 if ((s
->flags
& SEC_LOAD
) != 0
3588 && CONST_STRNEQ (s
->name
, ".note"))
3590 /* We need a PT_NOTE segment. */
3592 /* Try to create just one PT_NOTE segment
3593 for all adjacent loadable .note* sections.
3594 gABI requires that within a PT_NOTE segment
3595 (and also inside of each SHT_NOTE section)
3596 each note is padded to a multiple of 4 size,
3597 so we check whether the sections are correctly
3599 if (s
->alignment_power
== 2)
3600 while (s
->next
!= NULL
3601 && s
->next
->alignment_power
== 2
3602 && (s
->next
->flags
& SEC_LOAD
) != 0
3603 && CONST_STRNEQ (s
->next
->name
, ".note"))
3608 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3610 if (s
->flags
& SEC_THREAD_LOCAL
)
3612 /* We need a PT_TLS segment. */
3618 /* Let the backend count up any program headers it might need. */
3619 bed
= get_elf_backend_data (abfd
);
3620 if (bed
->elf_backend_additional_program_headers
)
3624 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3630 return segs
* bed
->s
->sizeof_phdr
;
3633 /* Find the segment that contains the output_section of section. */
3636 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3638 struct elf_segment_map
*m
;
3639 Elf_Internal_Phdr
*p
;
3641 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3647 for (i
= m
->count
- 1; i
>= 0; i
--)
3648 if (m
->sections
[i
] == section
)
3655 /* Create a mapping from a set of sections to a program segment. */
3657 static struct elf_segment_map
*
3658 make_mapping (bfd
*abfd
,
3659 asection
**sections
,
3664 struct elf_segment_map
*m
;
3669 amt
= sizeof (struct elf_segment_map
);
3670 amt
+= (to
- from
- 1) * sizeof (asection
*);
3671 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3675 m
->p_type
= PT_LOAD
;
3676 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3677 m
->sections
[i
- from
] = *hdrpp
;
3678 m
->count
= to
- from
;
3680 if (from
== 0 && phdr
)
3682 /* Include the headers in the first PT_LOAD segment. */
3683 m
->includes_filehdr
= 1;
3684 m
->includes_phdrs
= 1;
3690 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3693 struct elf_segment_map
*
3694 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3696 struct elf_segment_map
*m
;
3698 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3699 sizeof (struct elf_segment_map
));
3703 m
->p_type
= PT_DYNAMIC
;
3705 m
->sections
[0] = dynsec
;
3710 /* Possibly add or remove segments from the segment map. */
3713 elf_modify_segment_map (bfd
*abfd
,
3714 struct bfd_link_info
*info
,
3715 bfd_boolean remove_empty_load
)
3717 struct elf_segment_map
**m
;
3718 const struct elf_backend_data
*bed
;
3720 /* The placement algorithm assumes that non allocated sections are
3721 not in PT_LOAD segments. We ensure this here by removing such
3722 sections from the segment map. We also remove excluded
3723 sections. Finally, any PT_LOAD segment without sections is
3725 m
= &elf_seg_map (abfd
);
3728 unsigned int i
, new_count
;
3730 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3732 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3733 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3734 || (*m
)->p_type
!= PT_LOAD
))
3736 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3740 (*m
)->count
= new_count
;
3742 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3748 bed
= get_elf_backend_data (abfd
);
3749 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3751 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3758 /* Set up a mapping from BFD sections to program segments. */
3761 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3764 struct elf_segment_map
*m
;
3765 asection
**sections
= NULL
;
3766 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3767 bfd_boolean no_user_phdrs
;
3769 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
3772 info
->user_phdrs
= !no_user_phdrs
;
3774 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3778 struct elf_segment_map
*mfirst
;
3779 struct elf_segment_map
**pm
;
3782 unsigned int phdr_index
;
3783 bfd_vma maxpagesize
;
3785 bfd_boolean phdr_in_segment
= TRUE
;
3786 bfd_boolean writable
;
3788 asection
*first_tls
= NULL
;
3789 asection
*dynsec
, *eh_frame_hdr
;
3791 bfd_vma addr_mask
, wrap_to
= 0;
3793 /* Select the allocated sections, and sort them. */
3795 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3796 sizeof (asection
*));
3797 if (sections
== NULL
)
3800 /* Calculate top address, avoiding undefined behaviour of shift
3801 left operator when shift count is equal to size of type
3803 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3804 addr_mask
= (addr_mask
<< 1) + 1;
3807 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3809 if ((s
->flags
& SEC_ALLOC
) != 0)
3813 /* A wrapping section potentially clashes with header. */
3814 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3815 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3818 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3821 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3823 /* Build the mapping. */
3828 /* If we have a .interp section, then create a PT_PHDR segment for
3829 the program headers and a PT_INTERP segment for the .interp
3831 s
= bfd_get_section_by_name (abfd
, ".interp");
3832 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3834 amt
= sizeof (struct elf_segment_map
);
3835 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3839 m
->p_type
= PT_PHDR
;
3840 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3841 m
->p_flags
= PF_R
| PF_X
;
3842 m
->p_flags_valid
= 1;
3843 m
->includes_phdrs
= 1;
3848 amt
= sizeof (struct elf_segment_map
);
3849 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3853 m
->p_type
= PT_INTERP
;
3861 /* Look through the sections. We put sections in the same program
3862 segment when the start of the second section can be placed within
3863 a few bytes of the end of the first section. */
3867 maxpagesize
= bed
->maxpagesize
;
3869 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3871 && (dynsec
->flags
& SEC_LOAD
) == 0)
3874 /* Deal with -Ttext or something similar such that the first section
3875 is not adjacent to the program headers. This is an
3876 approximation, since at this point we don't know exactly how many
3877 program headers we will need. */
3880 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
3882 if (phdr_size
== (bfd_size_type
) -1)
3883 phdr_size
= get_program_header_size (abfd
, info
);
3884 phdr_size
+= bed
->s
->sizeof_ehdr
;
3885 if ((abfd
->flags
& D_PAGED
) == 0
3886 || (sections
[0]->lma
& addr_mask
) < phdr_size
3887 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
3888 < phdr_size
% maxpagesize
)
3889 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
3890 phdr_in_segment
= FALSE
;
3893 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3896 bfd_boolean new_segment
;
3900 /* See if this section and the last one will fit in the same
3903 if (last_hdr
== NULL
)
3905 /* If we don't have a segment yet, then we don't need a new
3906 one (we build the last one after this loop). */
3907 new_segment
= FALSE
;
3909 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3911 /* If this section has a different relation between the
3912 virtual address and the load address, then we need a new
3916 else if (hdr
->lma
< last_hdr
->lma
+ last_size
3917 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
3919 /* If this section has a load address that makes it overlap
3920 the previous section, then we need a new segment. */
3923 /* In the next test we have to be careful when last_hdr->lma is close
3924 to the end of the address space. If the aligned address wraps
3925 around to the start of the address space, then there are no more
3926 pages left in memory and it is OK to assume that the current
3927 section can be included in the current segment. */
3928 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3930 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3933 /* If putting this section in this segment would force us to
3934 skip a page in the segment, then we need a new segment. */
3937 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3938 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3940 /* We don't want to put a loadable section after a
3941 nonloadable section in the same segment.
3942 Consider .tbss sections as loadable for this purpose. */
3945 else if ((abfd
->flags
& D_PAGED
) == 0)
3947 /* If the file is not demand paged, which means that we
3948 don't require the sections to be correctly aligned in the
3949 file, then there is no other reason for a new segment. */
3950 new_segment
= FALSE
;
3953 && (hdr
->flags
& SEC_READONLY
) == 0
3954 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
3955 != (hdr
->lma
& -maxpagesize
)))
3957 /* We don't want to put a writable section in a read only
3958 segment, unless they are on the same page in memory
3959 anyhow. We already know that the last section does not
3960 bring us past the current section on the page, so the
3961 only case in which the new section is not on the same
3962 page as the previous section is when the previous section
3963 ends precisely on a page boundary. */
3968 /* Otherwise, we can use the same segment. */
3969 new_segment
= FALSE
;
3972 /* Allow interested parties a chance to override our decision. */
3973 if (last_hdr
!= NULL
3975 && info
->callbacks
->override_segment_assignment
!= NULL
)
3977 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
3983 if ((hdr
->flags
& SEC_READONLY
) == 0)
3986 /* .tbss sections effectively have zero size. */
3987 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3988 != SEC_THREAD_LOCAL
)
3989 last_size
= hdr
->size
;
3995 /* We need a new program segment. We must create a new program
3996 header holding all the sections from phdr_index until hdr. */
3998 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4005 if ((hdr
->flags
& SEC_READONLY
) == 0)
4011 /* .tbss sections effectively have zero size. */
4012 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4013 last_size
= hdr
->size
;
4017 phdr_in_segment
= FALSE
;
4020 /* Create a final PT_LOAD program segment, but not if it's just
4022 if (last_hdr
!= NULL
4023 && (i
- phdr_index
!= 1
4024 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4025 != SEC_THREAD_LOCAL
)))
4027 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4035 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4038 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4045 /* For each batch of consecutive loadable .note sections,
4046 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4047 because if we link together nonloadable .note sections and
4048 loadable .note sections, we will generate two .note sections
4049 in the output file. FIXME: Using names for section types is
4051 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4053 if ((s
->flags
& SEC_LOAD
) != 0
4054 && CONST_STRNEQ (s
->name
, ".note"))
4059 amt
= sizeof (struct elf_segment_map
);
4060 if (s
->alignment_power
== 2)
4061 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4063 if (s2
->next
->alignment_power
== 2
4064 && (s2
->next
->flags
& SEC_LOAD
) != 0
4065 && CONST_STRNEQ (s2
->next
->name
, ".note")
4066 && align_power (s2
->lma
+ s2
->size
, 2)
4072 amt
+= (count
- 1) * sizeof (asection
*);
4073 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4077 m
->p_type
= PT_NOTE
;
4081 m
->sections
[m
->count
- count
--] = s
;
4082 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4085 m
->sections
[m
->count
- 1] = s
;
4086 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4090 if (s
->flags
& SEC_THREAD_LOCAL
)
4098 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4101 amt
= sizeof (struct elf_segment_map
);
4102 amt
+= (tls_count
- 1) * sizeof (asection
*);
4103 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4108 m
->count
= tls_count
;
4109 /* Mandated PF_R. */
4111 m
->p_flags_valid
= 1;
4112 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4114 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
4115 m
->sections
[i
] = first_tls
;
4116 first_tls
= first_tls
->next
;
4123 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4125 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4126 if (eh_frame_hdr
!= NULL
4127 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4129 amt
= sizeof (struct elf_segment_map
);
4130 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4134 m
->p_type
= PT_GNU_EH_FRAME
;
4136 m
->sections
[0] = eh_frame_hdr
->output_section
;
4142 if (elf_stack_flags (abfd
))
4144 amt
= sizeof (struct elf_segment_map
);
4145 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4149 m
->p_type
= PT_GNU_STACK
;
4150 m
->p_flags
= elf_stack_flags (abfd
);
4151 m
->p_align
= bed
->stack_align
;
4152 m
->p_flags_valid
= 1;
4153 m
->p_align_valid
= m
->p_align
!= 0;
4154 if (info
->stacksize
> 0)
4156 m
->p_size
= info
->stacksize
;
4157 m
->p_size_valid
= 1;
4164 if (info
!= NULL
&& info
->relro
)
4166 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4168 if (m
->p_type
== PT_LOAD
4170 && m
->sections
[0]->vma
>= info
->relro_start
4171 && m
->sections
[0]->vma
< info
->relro_end
)
4174 while (--i
!= (unsigned) -1)
4175 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4176 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4179 if (i
== (unsigned) -1)
4182 if (m
->sections
[i
]->vma
+ m
->sections
[i
]->size
4188 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4191 amt
= sizeof (struct elf_segment_map
);
4192 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4196 m
->p_type
= PT_GNU_RELRO
;
4198 m
->p_flags_valid
= 1;
4206 elf_seg_map (abfd
) = mfirst
;
4209 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4212 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4214 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4219 if (sections
!= NULL
)
4224 /* Sort sections by address. */
4227 elf_sort_sections (const void *arg1
, const void *arg2
)
4229 const asection
*sec1
= *(const asection
**) arg1
;
4230 const asection
*sec2
= *(const asection
**) arg2
;
4231 bfd_size_type size1
, size2
;
4233 /* Sort by LMA first, since this is the address used to
4234 place the section into a segment. */
4235 if (sec1
->lma
< sec2
->lma
)
4237 else if (sec1
->lma
> sec2
->lma
)
4240 /* Then sort by VMA. Normally the LMA and the VMA will be
4241 the same, and this will do nothing. */
4242 if (sec1
->vma
< sec2
->vma
)
4244 else if (sec1
->vma
> sec2
->vma
)
4247 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4249 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4255 /* If the indicies are the same, do not return 0
4256 here, but continue to try the next comparison. */
4257 if (sec1
->target_index
- sec2
->target_index
!= 0)
4258 return sec1
->target_index
- sec2
->target_index
;
4263 else if (TOEND (sec2
))
4268 /* Sort by size, to put zero sized sections
4269 before others at the same address. */
4271 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4272 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4279 return sec1
->target_index
- sec2
->target_index
;
4282 /* Ian Lance Taylor writes:
4284 We shouldn't be using % with a negative signed number. That's just
4285 not good. We have to make sure either that the number is not
4286 negative, or that the number has an unsigned type. When the types
4287 are all the same size they wind up as unsigned. When file_ptr is a
4288 larger signed type, the arithmetic winds up as signed long long,
4291 What we're trying to say here is something like ``increase OFF by
4292 the least amount that will cause it to be equal to the VMA modulo
4294 /* In other words, something like:
4296 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4297 off_offset = off % bed->maxpagesize;
4298 if (vma_offset < off_offset)
4299 adjustment = vma_offset + bed->maxpagesize - off_offset;
4301 adjustment = vma_offset - off_offset;
4303 which can can be collapsed into the expression below. */
4306 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4308 return ((vma
- off
) % maxpagesize
);
4312 print_segment_map (const struct elf_segment_map
*m
)
4315 const char *pt
= get_segment_type (m
->p_type
);
4320 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4321 sprintf (buf
, "LOPROC+%7.7x",
4322 (unsigned int) (m
->p_type
- PT_LOPROC
));
4323 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4324 sprintf (buf
, "LOOS+%7.7x",
4325 (unsigned int) (m
->p_type
- PT_LOOS
));
4327 snprintf (buf
, sizeof (buf
), "%8.8x",
4328 (unsigned int) m
->p_type
);
4332 fprintf (stderr
, "%s:", pt
);
4333 for (j
= 0; j
< m
->count
; j
++)
4334 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4340 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4345 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4347 buf
= bfd_zmalloc (len
);
4350 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4355 /* Assign file positions to the sections based on the mapping from
4356 sections to segments. This function also sets up some fields in
4360 assign_file_positions_for_load_sections (bfd
*abfd
,
4361 struct bfd_link_info
*link_info
)
4363 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4364 struct elf_segment_map
*m
;
4365 Elf_Internal_Phdr
*phdrs
;
4366 Elf_Internal_Phdr
*p
;
4368 bfd_size_type maxpagesize
;
4371 bfd_vma header_pad
= 0;
4373 if (link_info
== NULL
4374 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4378 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4382 header_pad
= m
->header_size
;
4387 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4388 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4392 /* PR binutils/12467. */
4393 elf_elfheader (abfd
)->e_phoff
= 0;
4394 elf_elfheader (abfd
)->e_phentsize
= 0;
4397 elf_elfheader (abfd
)->e_phnum
= alloc
;
4399 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4400 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4402 BFD_ASSERT (elf_program_header_size (abfd
)
4403 >= alloc
* bed
->s
->sizeof_phdr
);
4407 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4411 /* We're writing the size in elf_program_header_size (abfd),
4412 see assign_file_positions_except_relocs, so make sure we have
4413 that amount allocated, with trailing space cleared.
4414 The variable alloc contains the computed need, while
4415 elf_program_header_size (abfd) contains the size used for the
4417 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4418 where the layout is forced to according to a larger size in the
4419 last iterations for the testcase ld-elf/header. */
4420 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4422 phdrs
= (Elf_Internal_Phdr
*)
4424 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4425 sizeof (Elf_Internal_Phdr
));
4426 elf_tdata (abfd
)->phdr
= phdrs
;
4431 if ((abfd
->flags
& D_PAGED
) != 0)
4432 maxpagesize
= bed
->maxpagesize
;
4434 off
= bed
->s
->sizeof_ehdr
;
4435 off
+= alloc
* bed
->s
->sizeof_phdr
;
4436 if (header_pad
< (bfd_vma
) off
)
4442 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4444 m
= m
->next
, p
++, j
++)
4448 bfd_boolean no_contents
;
4450 /* If elf_segment_map is not from map_sections_to_segments, the
4451 sections may not be correctly ordered. NOTE: sorting should
4452 not be done to the PT_NOTE section of a corefile, which may
4453 contain several pseudo-sections artificially created by bfd.
4454 Sorting these pseudo-sections breaks things badly. */
4456 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4457 && m
->p_type
== PT_NOTE
))
4458 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4461 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4462 number of sections with contents contributing to both p_filesz
4463 and p_memsz, followed by a number of sections with no contents
4464 that just contribute to p_memsz. In this loop, OFF tracks next
4465 available file offset for PT_LOAD and PT_NOTE segments. */
4466 p
->p_type
= m
->p_type
;
4467 p
->p_flags
= m
->p_flags
;
4472 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4474 if (m
->p_paddr_valid
)
4475 p
->p_paddr
= m
->p_paddr
;
4476 else if (m
->count
== 0)
4479 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4481 if (p
->p_type
== PT_LOAD
4482 && (abfd
->flags
& D_PAGED
) != 0)
4484 /* p_align in demand paged PT_LOAD segments effectively stores
4485 the maximum page size. When copying an executable with
4486 objcopy, we set m->p_align from the input file. Use this
4487 value for maxpagesize rather than bed->maxpagesize, which
4488 may be different. Note that we use maxpagesize for PT_TLS
4489 segment alignment later in this function, so we are relying
4490 on at least one PT_LOAD segment appearing before a PT_TLS
4492 if (m
->p_align_valid
)
4493 maxpagesize
= m
->p_align
;
4495 p
->p_align
= maxpagesize
;
4497 else if (m
->p_align_valid
)
4498 p
->p_align
= m
->p_align
;
4499 else if (m
->count
== 0)
4500 p
->p_align
= 1 << bed
->s
->log_file_align
;
4504 no_contents
= FALSE
;
4506 if (p
->p_type
== PT_LOAD
4509 bfd_size_type align
;
4510 unsigned int align_power
= 0;
4512 if (m
->p_align_valid
)
4516 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4518 unsigned int secalign
;
4520 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4521 if (secalign
> align_power
)
4522 align_power
= secalign
;
4524 align
= (bfd_size_type
) 1 << align_power
;
4525 if (align
< maxpagesize
)
4526 align
= maxpagesize
;
4529 for (i
= 0; i
< m
->count
; i
++)
4530 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4531 /* If we aren't making room for this section, then
4532 it must be SHT_NOBITS regardless of what we've
4533 set via struct bfd_elf_special_section. */
4534 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4536 /* Find out whether this segment contains any loadable
4539 for (i
= 0; i
< m
->count
; i
++)
4540 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4542 no_contents
= FALSE
;
4546 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4550 /* We shouldn't need to align the segment on disk since
4551 the segment doesn't need file space, but the gABI
4552 arguably requires the alignment and glibc ld.so
4553 checks it. So to comply with the alignment
4554 requirement but not waste file space, we adjust
4555 p_offset for just this segment. (OFF_ADJUST is
4556 subtracted from OFF later.) This may put p_offset
4557 past the end of file, but that shouldn't matter. */
4562 /* Make sure the .dynamic section is the first section in the
4563 PT_DYNAMIC segment. */
4564 else if (p
->p_type
== PT_DYNAMIC
4566 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4569 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4571 bfd_set_error (bfd_error_bad_value
);
4574 /* Set the note section type to SHT_NOTE. */
4575 else if (p
->p_type
== PT_NOTE
)
4576 for (i
= 0; i
< m
->count
; i
++)
4577 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4583 if (m
->includes_filehdr
)
4585 if (!m
->p_flags_valid
)
4587 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4588 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4591 if (p
->p_vaddr
< (bfd_vma
) off
)
4593 (*_bfd_error_handler
)
4594 (_("%B: Not enough room for program headers, try linking with -N"),
4596 bfd_set_error (bfd_error_bad_value
);
4601 if (!m
->p_paddr_valid
)
4606 if (m
->includes_phdrs
)
4608 if (!m
->p_flags_valid
)
4611 if (!m
->includes_filehdr
)
4613 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4617 p
->p_vaddr
-= off
- p
->p_offset
;
4618 if (!m
->p_paddr_valid
)
4619 p
->p_paddr
-= off
- p
->p_offset
;
4623 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4624 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4627 p
->p_filesz
+= header_pad
;
4628 p
->p_memsz
+= header_pad
;
4632 if (p
->p_type
== PT_LOAD
4633 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4635 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4641 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4643 p
->p_filesz
+= adjust
;
4644 p
->p_memsz
+= adjust
;
4648 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4649 maps. Set filepos for sections in PT_LOAD segments, and in
4650 core files, for sections in PT_NOTE segments.
4651 assign_file_positions_for_non_load_sections will set filepos
4652 for other sections and update p_filesz for other segments. */
4653 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4656 bfd_size_type align
;
4657 Elf_Internal_Shdr
*this_hdr
;
4660 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4661 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4663 if ((p
->p_type
== PT_LOAD
4664 || p
->p_type
== PT_TLS
)
4665 && (this_hdr
->sh_type
!= SHT_NOBITS
4666 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4667 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4668 || p
->p_type
== PT_TLS
))))
4670 bfd_vma p_start
= p
->p_paddr
;
4671 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4672 bfd_vma s_start
= sec
->lma
;
4673 bfd_vma adjust
= s_start
- p_end
;
4677 || p_end
< p_start
))
4679 (*_bfd_error_handler
)
4680 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4681 (unsigned long) s_start
, (unsigned long) p_end
);
4685 p
->p_memsz
+= adjust
;
4687 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4689 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4691 /* We have a PROGBITS section following NOBITS ones.
4692 Allocate file space for the NOBITS section(s) and
4694 adjust
= p
->p_memsz
- p
->p_filesz
;
4695 if (!write_zeros (abfd
, off
, adjust
))
4699 p
->p_filesz
+= adjust
;
4703 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4705 /* The section at i == 0 is the one that actually contains
4709 this_hdr
->sh_offset
= sec
->filepos
= off
;
4710 off
+= this_hdr
->sh_size
;
4711 p
->p_filesz
= this_hdr
->sh_size
;
4717 /* The rest are fake sections that shouldn't be written. */
4726 if (p
->p_type
== PT_LOAD
)
4728 this_hdr
->sh_offset
= sec
->filepos
= off
;
4729 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4730 off
+= this_hdr
->sh_size
;
4732 else if (this_hdr
->sh_type
== SHT_NOBITS
4733 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
4734 && this_hdr
->sh_offset
== 0)
4736 /* This is a .tbss section that didn't get a PT_LOAD.
4737 (See _bfd_elf_map_sections_to_segments "Create a
4738 final PT_LOAD".) Set sh_offset to the value it
4739 would have if we had created a zero p_filesz and
4740 p_memsz PT_LOAD header for the section. This
4741 also makes the PT_TLS header have the same
4743 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
4745 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
4748 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4750 p
->p_filesz
+= this_hdr
->sh_size
;
4751 /* A load section without SHF_ALLOC is something like
4752 a note section in a PT_NOTE segment. These take
4753 file space but are not loaded into memory. */
4754 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4755 p
->p_memsz
+= this_hdr
->sh_size
;
4757 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4759 if (p
->p_type
== PT_TLS
)
4760 p
->p_memsz
+= this_hdr
->sh_size
;
4762 /* .tbss is special. It doesn't contribute to p_memsz of
4764 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4765 p
->p_memsz
+= this_hdr
->sh_size
;
4768 if (align
> p
->p_align
4769 && !m
->p_align_valid
4770 && (p
->p_type
!= PT_LOAD
4771 || (abfd
->flags
& D_PAGED
) == 0))
4775 if (!m
->p_flags_valid
)
4778 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4780 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4786 /* Check that all sections are in a PT_LOAD segment.
4787 Don't check funky gdb generated core files. */
4788 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4790 bfd_boolean check_vma
= TRUE
;
4792 for (i
= 1; i
< m
->count
; i
++)
4793 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4794 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4795 ->this_hdr
), p
) != 0
4796 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4797 ->this_hdr
), p
) != 0)
4799 /* Looks like we have overlays packed into the segment. */
4804 for (i
= 0; i
< m
->count
; i
++)
4806 Elf_Internal_Shdr
*this_hdr
;
4809 sec
= m
->sections
[i
];
4810 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4811 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
4812 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
4814 (*_bfd_error_handler
)
4815 (_("%B: section `%A' can't be allocated in segment %d"),
4817 print_segment_map (m
);
4823 elf_next_file_pos (abfd
) = off
;
4827 /* Assign file positions for the other sections. */
4830 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4831 struct bfd_link_info
*link_info
)
4833 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4834 Elf_Internal_Shdr
**i_shdrpp
;
4835 Elf_Internal_Shdr
**hdrpp
;
4836 Elf_Internal_Phdr
*phdrs
;
4837 Elf_Internal_Phdr
*p
;
4838 struct elf_segment_map
*m
;
4839 struct elf_segment_map
*hdrs_segment
;
4840 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4841 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4843 unsigned int num_sec
;
4847 i_shdrpp
= elf_elfsections (abfd
);
4848 num_sec
= elf_numsections (abfd
);
4849 off
= elf_next_file_pos (abfd
);
4850 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4852 Elf_Internal_Shdr
*hdr
;
4855 if (hdr
->bfd_section
!= NULL
4856 && (hdr
->bfd_section
->filepos
!= 0
4857 || (hdr
->sh_type
== SHT_NOBITS
4858 && hdr
->contents
== NULL
)))
4859 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4860 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4862 if (hdr
->sh_size
!= 0)
4863 (*_bfd_error_handler
)
4864 (_("%B: warning: allocated section `%s' not in segment"),
4866 (hdr
->bfd_section
== NULL
4868 : hdr
->bfd_section
->name
));
4869 /* We don't need to page align empty sections. */
4870 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4871 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4874 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4876 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4879 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4880 && hdr
->bfd_section
== NULL
)
4881 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
4882 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
4883 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)])
4884 hdr
->sh_offset
= -1;
4886 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4889 /* Now that we have set the section file positions, we can set up
4890 the file positions for the non PT_LOAD segments. */
4894 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4896 hdrs_segment
= NULL
;
4897 phdrs
= elf_tdata (abfd
)->phdr
;
4898 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
4901 if (p
->p_type
!= PT_LOAD
)
4904 if (m
->includes_filehdr
)
4906 filehdr_vaddr
= p
->p_vaddr
;
4907 filehdr_paddr
= p
->p_paddr
;
4909 if (m
->includes_phdrs
)
4911 phdrs_vaddr
= p
->p_vaddr
;
4912 phdrs_paddr
= p
->p_paddr
;
4913 if (m
->includes_filehdr
)
4916 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4917 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4922 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
4924 /* There is a segment that contains both the file headers and the
4925 program headers, so provide a symbol __ehdr_start pointing there.
4926 A program can use this to examine itself robustly. */
4928 struct elf_link_hash_entry
*hash
4929 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
4930 FALSE
, FALSE
, TRUE
);
4931 /* If the symbol was referenced and not defined, define it. */
4933 && (hash
->root
.type
== bfd_link_hash_new
4934 || hash
->root
.type
== bfd_link_hash_undefined
4935 || hash
->root
.type
== bfd_link_hash_undefweak
4936 || hash
->root
.type
== bfd_link_hash_common
))
4939 if (hdrs_segment
->count
!= 0)
4940 /* The segment contains sections, so use the first one. */
4941 s
= hdrs_segment
->sections
[0];
4943 /* Use the first (i.e. lowest-addressed) section in any segment. */
4944 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4953 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
4954 hash
->root
.u
.def
.section
= s
;
4958 hash
->root
.u
.def
.value
= filehdr_vaddr
;
4959 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
4962 hash
->root
.type
= bfd_link_hash_defined
;
4963 hash
->def_regular
= 1;
4968 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
4970 if (p
->p_type
== PT_GNU_RELRO
)
4972 const Elf_Internal_Phdr
*lp
;
4973 struct elf_segment_map
*lm
;
4975 if (link_info
!= NULL
)
4977 /* During linking the range of the RELRO segment is passed
4979 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
4981 lm
= lm
->next
, lp
++)
4983 if (lp
->p_type
== PT_LOAD
4984 && lp
->p_vaddr
< link_info
->relro_end
4985 && lp
->p_vaddr
+ lp
->p_filesz
>= link_info
->relro_end
4987 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
4991 /* PR ld/14207. If the RELRO segment doesn't fit in the
4992 LOAD segment, it should be removed. */
4993 BFD_ASSERT (lm
!= NULL
);
4997 /* Otherwise we are copying an executable or shared
4998 library, but we need to use the same linker logic. */
4999 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5001 if (lp
->p_type
== PT_LOAD
5002 && lp
->p_paddr
== p
->p_paddr
)
5007 if (lp
< phdrs
+ count
)
5009 p
->p_vaddr
= lp
->p_vaddr
;
5010 p
->p_paddr
= lp
->p_paddr
;
5011 p
->p_offset
= lp
->p_offset
;
5012 if (link_info
!= NULL
)
5013 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5014 else if (m
->p_size_valid
)
5015 p
->p_filesz
= m
->p_size
;
5018 p
->p_memsz
= p
->p_filesz
;
5019 /* Preserve the alignment and flags if they are valid. The
5020 gold linker generates RW/4 for the PT_GNU_RELRO section.
5021 It is better for objcopy/strip to honor these attributes
5022 otherwise gdb will choke when using separate debug files.
5024 if (!m
->p_align_valid
)
5026 if (!m
->p_flags_valid
)
5027 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5031 memset (p
, 0, sizeof *p
);
5032 p
->p_type
= PT_NULL
;
5035 else if (p
->p_type
== PT_GNU_STACK
)
5037 if (m
->p_size_valid
)
5038 p
->p_memsz
= m
->p_size
;
5040 else if (m
->count
!= 0)
5042 if (p
->p_type
!= PT_LOAD
5043 && (p
->p_type
!= PT_NOTE
5044 || bfd_get_format (abfd
) != bfd_core
))
5046 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
5049 p
->p_offset
= m
->sections
[0]->filepos
;
5050 for (i
= m
->count
; i
-- != 0;)
5052 asection
*sect
= m
->sections
[i
];
5053 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5054 if (hdr
->sh_type
!= SHT_NOBITS
)
5056 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5063 else if (m
->includes_filehdr
)
5065 p
->p_vaddr
= filehdr_vaddr
;
5066 if (! m
->p_paddr_valid
)
5067 p
->p_paddr
= filehdr_paddr
;
5069 else if (m
->includes_phdrs
)
5071 p
->p_vaddr
= phdrs_vaddr
;
5072 if (! m
->p_paddr_valid
)
5073 p
->p_paddr
= phdrs_paddr
;
5077 elf_next_file_pos (abfd
) = off
;
5082 /* Work out the file positions of all the sections. This is called by
5083 _bfd_elf_compute_section_file_positions. All the section sizes and
5084 VMAs must be known before this is called.
5086 Reloc sections come in two flavours: Those processed specially as
5087 "side-channel" data attached to a section to which they apply, and
5088 those that bfd doesn't process as relocations. The latter sort are
5089 stored in a normal bfd section by bfd_section_from_shdr. We don't
5090 consider the former sort here, unless they form part of the loadable
5091 image. Reloc sections not assigned here will be handled later by
5092 assign_file_positions_for_relocs.
5094 We also don't set the positions of the .symtab and .strtab here. */
5097 assign_file_positions_except_relocs (bfd
*abfd
,
5098 struct bfd_link_info
*link_info
)
5100 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5101 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5103 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5105 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5106 && bfd_get_format (abfd
) != bfd_core
)
5108 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5109 unsigned int num_sec
= elf_numsections (abfd
);
5110 Elf_Internal_Shdr
**hdrpp
;
5113 /* Start after the ELF header. */
5114 off
= i_ehdrp
->e_ehsize
;
5116 /* We are not creating an executable, which means that we are
5117 not creating a program header, and that the actual order of
5118 the sections in the file is unimportant. */
5119 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5121 Elf_Internal_Shdr
*hdr
;
5124 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5125 && hdr
->bfd_section
== NULL
)
5126 || i
== elf_onesymtab (abfd
)
5127 || i
== elf_symtab_shndx (abfd
)
5128 || i
== elf_strtab_sec (abfd
))
5130 hdr
->sh_offset
= -1;
5133 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5140 /* Assign file positions for the loaded sections based on the
5141 assignment of sections to segments. */
5142 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5145 /* And for non-load sections. */
5146 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5149 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5151 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5155 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5156 if (link_info
!= NULL
5157 && link_info
->executable
5158 && link_info
->shared
)
5160 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5161 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5162 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5164 /* Find the lowest p_vaddr in PT_LOAD segments. */
5165 bfd_vma p_vaddr
= (bfd_vma
) -1;
5166 for (; segment
< end_segment
; segment
++)
5167 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5168 p_vaddr
= segment
->p_vaddr
;
5170 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5171 segments is non-zero. */
5173 i_ehdrp
->e_type
= ET_EXEC
;
5176 /* Write out the program headers. */
5177 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5178 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5179 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5182 off
= elf_next_file_pos (abfd
);
5185 /* Place the section headers. */
5186 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5187 i_ehdrp
->e_shoff
= off
;
5188 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5190 elf_next_file_pos (abfd
) = off
;
5196 prep_headers (bfd
*abfd
)
5198 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5199 struct elf_strtab_hash
*shstrtab
;
5200 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5202 i_ehdrp
= elf_elfheader (abfd
);
5204 shstrtab
= _bfd_elf_strtab_init ();
5205 if (shstrtab
== NULL
)
5208 elf_shstrtab (abfd
) = shstrtab
;
5210 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5211 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5212 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5213 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5215 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5216 i_ehdrp
->e_ident
[EI_DATA
] =
5217 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5218 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5220 if ((abfd
->flags
& DYNAMIC
) != 0)
5221 i_ehdrp
->e_type
= ET_DYN
;
5222 else if ((abfd
->flags
& EXEC_P
) != 0)
5223 i_ehdrp
->e_type
= ET_EXEC
;
5224 else if (bfd_get_format (abfd
) == bfd_core
)
5225 i_ehdrp
->e_type
= ET_CORE
;
5227 i_ehdrp
->e_type
= ET_REL
;
5229 switch (bfd_get_arch (abfd
))
5231 case bfd_arch_unknown
:
5232 i_ehdrp
->e_machine
= EM_NONE
;
5235 /* There used to be a long list of cases here, each one setting
5236 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5237 in the corresponding bfd definition. To avoid duplication,
5238 the switch was removed. Machines that need special handling
5239 can generally do it in elf_backend_final_write_processing(),
5240 unless they need the information earlier than the final write.
5241 Such need can generally be supplied by replacing the tests for
5242 e_machine with the conditions used to determine it. */
5244 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5247 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5248 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5250 /* No program header, for now. */
5251 i_ehdrp
->e_phoff
= 0;
5252 i_ehdrp
->e_phentsize
= 0;
5253 i_ehdrp
->e_phnum
= 0;
5255 /* Each bfd section is section header entry. */
5256 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5257 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5259 /* If we're building an executable, we'll need a program header table. */
5260 if (abfd
->flags
& EXEC_P
)
5261 /* It all happens later. */
5265 i_ehdrp
->e_phentsize
= 0;
5266 i_ehdrp
->e_phoff
= 0;
5269 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5270 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5271 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5272 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5273 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5274 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5275 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5276 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5277 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5283 /* Assign file positions for all the reloc sections which are not part
5284 of the loadable file image. */
5287 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5290 unsigned int i
, num_sec
;
5291 Elf_Internal_Shdr
**shdrpp
;
5293 off
= elf_next_file_pos (abfd
);
5295 num_sec
= elf_numsections (abfd
);
5296 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5298 Elf_Internal_Shdr
*shdrp
;
5301 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5302 && shdrp
->sh_offset
== -1)
5303 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5306 elf_next_file_pos (abfd
) = off
;
5310 _bfd_elf_write_object_contents (bfd
*abfd
)
5312 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5313 Elf_Internal_Shdr
**i_shdrp
;
5315 unsigned int count
, num_sec
;
5316 struct elf_obj_tdata
*t
;
5318 if (! abfd
->output_has_begun
5319 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5322 i_shdrp
= elf_elfsections (abfd
);
5325 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5329 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5331 /* After writing the headers, we need to write the sections too... */
5332 num_sec
= elf_numsections (abfd
);
5333 for (count
= 1; count
< num_sec
; count
++)
5335 if (bed
->elf_backend_section_processing
)
5336 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5337 if (i_shdrp
[count
]->contents
)
5339 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5341 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5342 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5347 /* Write out the section header names. */
5348 t
= elf_tdata (abfd
);
5349 if (elf_shstrtab (abfd
) != NULL
5350 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5351 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5354 if (bed
->elf_backend_final_write_processing
)
5355 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5357 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5360 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5361 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5362 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5368 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5370 /* Hopefully this can be done just like an object file. */
5371 return _bfd_elf_write_object_contents (abfd
);
5374 /* Given a section, search the header to find them. */
5377 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5379 const struct elf_backend_data
*bed
;
5380 unsigned int sec_index
;
5382 if (elf_section_data (asect
) != NULL
5383 && elf_section_data (asect
)->this_idx
!= 0)
5384 return elf_section_data (asect
)->this_idx
;
5386 if (bfd_is_abs_section (asect
))
5387 sec_index
= SHN_ABS
;
5388 else if (bfd_is_com_section (asect
))
5389 sec_index
= SHN_COMMON
;
5390 else if (bfd_is_und_section (asect
))
5391 sec_index
= SHN_UNDEF
;
5393 sec_index
= SHN_BAD
;
5395 bed
= get_elf_backend_data (abfd
);
5396 if (bed
->elf_backend_section_from_bfd_section
)
5398 int retval
= sec_index
;
5400 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5404 if (sec_index
== SHN_BAD
)
5405 bfd_set_error (bfd_error_nonrepresentable_section
);
5410 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5414 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5416 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5418 flagword flags
= asym_ptr
->flags
;
5420 /* When gas creates relocations against local labels, it creates its
5421 own symbol for the section, but does put the symbol into the
5422 symbol chain, so udata is 0. When the linker is generating
5423 relocatable output, this section symbol may be for one of the
5424 input sections rather than the output section. */
5425 if (asym_ptr
->udata
.i
== 0
5426 && (flags
& BSF_SECTION_SYM
)
5427 && asym_ptr
->section
)
5432 sec
= asym_ptr
->section
;
5433 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5434 sec
= sec
->output_section
;
5435 if (sec
->owner
== abfd
5436 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5437 && elf_section_syms (abfd
)[indx
] != NULL
)
5438 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5441 idx
= asym_ptr
->udata
.i
;
5445 /* This case can occur when using --strip-symbol on a symbol
5446 which is used in a relocation entry. */
5447 (*_bfd_error_handler
)
5448 (_("%B: symbol `%s' required but not present"),
5449 abfd
, bfd_asymbol_name (asym_ptr
));
5450 bfd_set_error (bfd_error_no_symbols
);
5457 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5458 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5466 /* Rewrite program header information. */
5469 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5471 Elf_Internal_Ehdr
*iehdr
;
5472 struct elf_segment_map
*map
;
5473 struct elf_segment_map
*map_first
;
5474 struct elf_segment_map
**pointer_to_map
;
5475 Elf_Internal_Phdr
*segment
;
5478 unsigned int num_segments
;
5479 bfd_boolean phdr_included
= FALSE
;
5480 bfd_boolean p_paddr_valid
;
5481 bfd_vma maxpagesize
;
5482 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5483 unsigned int phdr_adjust_num
= 0;
5484 const struct elf_backend_data
*bed
;
5486 bed
= get_elf_backend_data (ibfd
);
5487 iehdr
= elf_elfheader (ibfd
);
5490 pointer_to_map
= &map_first
;
5492 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5493 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5495 /* Returns the end address of the segment + 1. */
5496 #define SEGMENT_END(segment, start) \
5497 (start + (segment->p_memsz > segment->p_filesz \
5498 ? segment->p_memsz : segment->p_filesz))
5500 #define SECTION_SIZE(section, segment) \
5501 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5502 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5503 ? section->size : 0)
5505 /* Returns TRUE if the given section is contained within
5506 the given segment. VMA addresses are compared. */
5507 #define IS_CONTAINED_BY_VMA(section, segment) \
5508 (section->vma >= segment->p_vaddr \
5509 && (section->vma + SECTION_SIZE (section, segment) \
5510 <= (SEGMENT_END (segment, segment->p_vaddr))))
5512 /* Returns TRUE if the given section is contained within
5513 the given segment. LMA addresses are compared. */
5514 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5515 (section->lma >= base \
5516 && (section->lma + SECTION_SIZE (section, segment) \
5517 <= SEGMENT_END (segment, base)))
5519 /* Handle PT_NOTE segment. */
5520 #define IS_NOTE(p, s) \
5521 (p->p_type == PT_NOTE \
5522 && elf_section_type (s) == SHT_NOTE \
5523 && (bfd_vma) s->filepos >= p->p_offset \
5524 && ((bfd_vma) s->filepos + s->size \
5525 <= p->p_offset + p->p_filesz))
5527 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5529 #define IS_COREFILE_NOTE(p, s) \
5531 && bfd_get_format (ibfd) == bfd_core \
5535 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5536 linker, which generates a PT_INTERP section with p_vaddr and
5537 p_memsz set to 0. */
5538 #define IS_SOLARIS_PT_INTERP(p, s) \
5540 && p->p_paddr == 0 \
5541 && p->p_memsz == 0 \
5542 && p->p_filesz > 0 \
5543 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5545 && (bfd_vma) s->filepos >= p->p_offset \
5546 && ((bfd_vma) s->filepos + s->size \
5547 <= p->p_offset + p->p_filesz))
5549 /* Decide if the given section should be included in the given segment.
5550 A section will be included if:
5551 1. It is within the address space of the segment -- we use the LMA
5552 if that is set for the segment and the VMA otherwise,
5553 2. It is an allocated section or a NOTE section in a PT_NOTE
5555 3. There is an output section associated with it,
5556 4. The section has not already been allocated to a previous segment.
5557 5. PT_GNU_STACK segments do not include any sections.
5558 6. PT_TLS segment includes only SHF_TLS sections.
5559 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5560 8. PT_DYNAMIC should not contain empty sections at the beginning
5561 (with the possible exception of .dynamic). */
5562 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5563 ((((segment->p_paddr \
5564 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5565 : IS_CONTAINED_BY_VMA (section, segment)) \
5566 && (section->flags & SEC_ALLOC) != 0) \
5567 || IS_NOTE (segment, section)) \
5568 && segment->p_type != PT_GNU_STACK \
5569 && (segment->p_type != PT_TLS \
5570 || (section->flags & SEC_THREAD_LOCAL)) \
5571 && (segment->p_type == PT_LOAD \
5572 || segment->p_type == PT_TLS \
5573 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5574 && (segment->p_type != PT_DYNAMIC \
5575 || SECTION_SIZE (section, segment) > 0 \
5576 || (segment->p_paddr \
5577 ? segment->p_paddr != section->lma \
5578 : segment->p_vaddr != section->vma) \
5579 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5581 && !section->segment_mark)
5583 /* If the output section of a section in the input segment is NULL,
5584 it is removed from the corresponding output segment. */
5585 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5586 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5587 && section->output_section != NULL)
5589 /* Returns TRUE iff seg1 starts after the end of seg2. */
5590 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5591 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5593 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5594 their VMA address ranges and their LMA address ranges overlap.
5595 It is possible to have overlapping VMA ranges without overlapping LMA
5596 ranges. RedBoot images for example can have both .data and .bss mapped
5597 to the same VMA range, but with the .data section mapped to a different
5599 #define SEGMENT_OVERLAPS(seg1, seg2) \
5600 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5601 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5602 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5603 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5605 /* Initialise the segment mark field. */
5606 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5607 section
->segment_mark
= FALSE
;
5609 /* The Solaris linker creates program headers in which all the
5610 p_paddr fields are zero. When we try to objcopy or strip such a
5611 file, we get confused. Check for this case, and if we find it
5612 don't set the p_paddr_valid fields. */
5613 p_paddr_valid
= FALSE
;
5614 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5617 if (segment
->p_paddr
!= 0)
5619 p_paddr_valid
= TRUE
;
5623 /* Scan through the segments specified in the program header
5624 of the input BFD. For this first scan we look for overlaps
5625 in the loadable segments. These can be created by weird
5626 parameters to objcopy. Also, fix some solaris weirdness. */
5627 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5632 Elf_Internal_Phdr
*segment2
;
5634 if (segment
->p_type
== PT_INTERP
)
5635 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5636 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5638 /* Mininal change so that the normal section to segment
5639 assignment code will work. */
5640 segment
->p_vaddr
= section
->vma
;
5644 if (segment
->p_type
!= PT_LOAD
)
5646 /* Remove PT_GNU_RELRO segment. */
5647 if (segment
->p_type
== PT_GNU_RELRO
)
5648 segment
->p_type
= PT_NULL
;
5652 /* Determine if this segment overlaps any previous segments. */
5653 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5655 bfd_signed_vma extra_length
;
5657 if (segment2
->p_type
!= PT_LOAD
5658 || !SEGMENT_OVERLAPS (segment
, segment2
))
5661 /* Merge the two segments together. */
5662 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5664 /* Extend SEGMENT2 to include SEGMENT and then delete
5666 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5667 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5669 if (extra_length
> 0)
5671 segment2
->p_memsz
+= extra_length
;
5672 segment2
->p_filesz
+= extra_length
;
5675 segment
->p_type
= PT_NULL
;
5677 /* Since we have deleted P we must restart the outer loop. */
5679 segment
= elf_tdata (ibfd
)->phdr
;
5684 /* Extend SEGMENT to include SEGMENT2 and then delete
5686 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5687 - SEGMENT_END (segment
, segment
->p_vaddr
));
5689 if (extra_length
> 0)
5691 segment
->p_memsz
+= extra_length
;
5692 segment
->p_filesz
+= extra_length
;
5695 segment2
->p_type
= PT_NULL
;
5700 /* The second scan attempts to assign sections to segments. */
5701 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5705 unsigned int section_count
;
5706 asection
**sections
;
5707 asection
*output_section
;
5709 bfd_vma matching_lma
;
5710 bfd_vma suggested_lma
;
5713 asection
*first_section
;
5714 bfd_boolean first_matching_lma
;
5715 bfd_boolean first_suggested_lma
;
5717 if (segment
->p_type
== PT_NULL
)
5720 first_section
= NULL
;
5721 /* Compute how many sections might be placed into this segment. */
5722 for (section
= ibfd
->sections
, section_count
= 0;
5724 section
= section
->next
)
5726 /* Find the first section in the input segment, which may be
5727 removed from the corresponding output segment. */
5728 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5730 if (first_section
== NULL
)
5731 first_section
= section
;
5732 if (section
->output_section
!= NULL
)
5737 /* Allocate a segment map big enough to contain
5738 all of the sections we have selected. */
5739 amt
= sizeof (struct elf_segment_map
);
5740 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5741 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5745 /* Initialise the fields of the segment map. Default to
5746 using the physical address of the segment in the input BFD. */
5748 map
->p_type
= segment
->p_type
;
5749 map
->p_flags
= segment
->p_flags
;
5750 map
->p_flags_valid
= 1;
5752 /* If the first section in the input segment is removed, there is
5753 no need to preserve segment physical address in the corresponding
5755 if (!first_section
|| first_section
->output_section
!= NULL
)
5757 map
->p_paddr
= segment
->p_paddr
;
5758 map
->p_paddr_valid
= p_paddr_valid
;
5761 /* Determine if this segment contains the ELF file header
5762 and if it contains the program headers themselves. */
5763 map
->includes_filehdr
= (segment
->p_offset
== 0
5764 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5765 map
->includes_phdrs
= 0;
5767 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5769 map
->includes_phdrs
=
5770 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5771 && (segment
->p_offset
+ segment
->p_filesz
5772 >= ((bfd_vma
) iehdr
->e_phoff
5773 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5775 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5776 phdr_included
= TRUE
;
5779 if (section_count
== 0)
5781 /* Special segments, such as the PT_PHDR segment, may contain
5782 no sections, but ordinary, loadable segments should contain
5783 something. They are allowed by the ELF spec however, so only
5784 a warning is produced. */
5785 if (segment
->p_type
== PT_LOAD
)
5786 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5787 " detected, is this intentional ?\n"),
5791 *pointer_to_map
= map
;
5792 pointer_to_map
= &map
->next
;
5797 /* Now scan the sections in the input BFD again and attempt
5798 to add their corresponding output sections to the segment map.
5799 The problem here is how to handle an output section which has
5800 been moved (ie had its LMA changed). There are four possibilities:
5802 1. None of the sections have been moved.
5803 In this case we can continue to use the segment LMA from the
5806 2. All of the sections have been moved by the same amount.
5807 In this case we can change the segment's LMA to match the LMA
5808 of the first section.
5810 3. Some of the sections have been moved, others have not.
5811 In this case those sections which have not been moved can be
5812 placed in the current segment which will have to have its size,
5813 and possibly its LMA changed, and a new segment or segments will
5814 have to be created to contain the other sections.
5816 4. The sections have been moved, but not by the same amount.
5817 In this case we can change the segment's LMA to match the LMA
5818 of the first section and we will have to create a new segment
5819 or segments to contain the other sections.
5821 In order to save time, we allocate an array to hold the section
5822 pointers that we are interested in. As these sections get assigned
5823 to a segment, they are removed from this array. */
5825 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5826 if (sections
== NULL
)
5829 /* Step One: Scan for segment vs section LMA conflicts.
5830 Also add the sections to the section array allocated above.
5831 Also add the sections to the current segment. In the common
5832 case, where the sections have not been moved, this means that
5833 we have completely filled the segment, and there is nothing
5838 first_matching_lma
= TRUE
;
5839 first_suggested_lma
= TRUE
;
5841 for (section
= ibfd
->sections
;
5843 section
= section
->next
)
5844 if (section
== first_section
)
5847 for (j
= 0; section
!= NULL
; section
= section
->next
)
5849 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5851 output_section
= section
->output_section
;
5853 sections
[j
++] = section
;
5855 /* The Solaris native linker always sets p_paddr to 0.
5856 We try to catch that case here, and set it to the
5857 correct value. Note - some backends require that
5858 p_paddr be left as zero. */
5860 && segment
->p_vaddr
!= 0
5861 && !bed
->want_p_paddr_set_to_zero
5863 && output_section
->lma
!= 0
5864 && output_section
->vma
== (segment
->p_vaddr
5865 + (map
->includes_filehdr
5868 + (map
->includes_phdrs
5870 * iehdr
->e_phentsize
)
5872 map
->p_paddr
= segment
->p_vaddr
;
5874 /* Match up the physical address of the segment with the
5875 LMA address of the output section. */
5876 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5877 || IS_COREFILE_NOTE (segment
, section
)
5878 || (bed
->want_p_paddr_set_to_zero
5879 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5881 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5883 matching_lma
= output_section
->lma
;
5884 first_matching_lma
= FALSE
;
5887 /* We assume that if the section fits within the segment
5888 then it does not overlap any other section within that
5890 map
->sections
[isec
++] = output_section
;
5892 else if (first_suggested_lma
)
5894 suggested_lma
= output_section
->lma
;
5895 first_suggested_lma
= FALSE
;
5898 if (j
== section_count
)
5903 BFD_ASSERT (j
== section_count
);
5905 /* Step Two: Adjust the physical address of the current segment,
5907 if (isec
== section_count
)
5909 /* All of the sections fitted within the segment as currently
5910 specified. This is the default case. Add the segment to
5911 the list of built segments and carry on to process the next
5912 program header in the input BFD. */
5913 map
->count
= section_count
;
5914 *pointer_to_map
= map
;
5915 pointer_to_map
= &map
->next
;
5918 && !bed
->want_p_paddr_set_to_zero
5919 && matching_lma
!= map
->p_paddr
5920 && !map
->includes_filehdr
5921 && !map
->includes_phdrs
)
5922 /* There is some padding before the first section in the
5923 segment. So, we must account for that in the output
5925 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5932 if (!first_matching_lma
)
5934 /* At least one section fits inside the current segment.
5935 Keep it, but modify its physical address to match the
5936 LMA of the first section that fitted. */
5937 map
->p_paddr
= matching_lma
;
5941 /* None of the sections fitted inside the current segment.
5942 Change the current segment's physical address to match
5943 the LMA of the first section. */
5944 map
->p_paddr
= suggested_lma
;
5947 /* Offset the segment physical address from the lma
5948 to allow for space taken up by elf headers. */
5949 if (map
->includes_filehdr
)
5951 if (map
->p_paddr
>= iehdr
->e_ehsize
)
5952 map
->p_paddr
-= iehdr
->e_ehsize
;
5955 map
->includes_filehdr
= FALSE
;
5956 map
->includes_phdrs
= FALSE
;
5960 if (map
->includes_phdrs
)
5962 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
5964 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5966 /* iehdr->e_phnum is just an estimate of the number
5967 of program headers that we will need. Make a note
5968 here of the number we used and the segment we chose
5969 to hold these headers, so that we can adjust the
5970 offset when we know the correct value. */
5971 phdr_adjust_num
= iehdr
->e_phnum
;
5972 phdr_adjust_seg
= map
;
5975 map
->includes_phdrs
= FALSE
;
5979 /* Step Three: Loop over the sections again, this time assigning
5980 those that fit to the current segment and removing them from the
5981 sections array; but making sure not to leave large gaps. Once all
5982 possible sections have been assigned to the current segment it is
5983 added to the list of built segments and if sections still remain
5984 to be assigned, a new segment is constructed before repeating
5991 first_suggested_lma
= TRUE
;
5993 /* Fill the current segment with sections that fit. */
5994 for (j
= 0; j
< section_count
; j
++)
5996 section
= sections
[j
];
5998 if (section
== NULL
)
6001 output_section
= section
->output_section
;
6003 BFD_ASSERT (output_section
!= NULL
);
6005 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6006 || IS_COREFILE_NOTE (segment
, section
))
6008 if (map
->count
== 0)
6010 /* If the first section in a segment does not start at
6011 the beginning of the segment, then something is
6013 if (output_section
->lma
6015 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6016 + (map
->includes_phdrs
6017 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6025 prev_sec
= map
->sections
[map
->count
- 1];
6027 /* If the gap between the end of the previous section
6028 and the start of this section is more than
6029 maxpagesize then we need to start a new segment. */
6030 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6032 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6033 || (prev_sec
->lma
+ prev_sec
->size
6034 > output_section
->lma
))
6036 if (first_suggested_lma
)
6038 suggested_lma
= output_section
->lma
;
6039 first_suggested_lma
= FALSE
;
6046 map
->sections
[map
->count
++] = output_section
;
6049 section
->segment_mark
= TRUE
;
6051 else if (first_suggested_lma
)
6053 suggested_lma
= output_section
->lma
;
6054 first_suggested_lma
= FALSE
;
6058 BFD_ASSERT (map
->count
> 0);
6060 /* Add the current segment to the list of built segments. */
6061 *pointer_to_map
= map
;
6062 pointer_to_map
= &map
->next
;
6064 if (isec
< section_count
)
6066 /* We still have not allocated all of the sections to
6067 segments. Create a new segment here, initialise it
6068 and carry on looping. */
6069 amt
= sizeof (struct elf_segment_map
);
6070 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6071 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6078 /* Initialise the fields of the segment map. Set the physical
6079 physical address to the LMA of the first section that has
6080 not yet been assigned. */
6082 map
->p_type
= segment
->p_type
;
6083 map
->p_flags
= segment
->p_flags
;
6084 map
->p_flags_valid
= 1;
6085 map
->p_paddr
= suggested_lma
;
6086 map
->p_paddr_valid
= p_paddr_valid
;
6087 map
->includes_filehdr
= 0;
6088 map
->includes_phdrs
= 0;
6091 while (isec
< section_count
);
6096 elf_seg_map (obfd
) = map_first
;
6098 /* If we had to estimate the number of program headers that were
6099 going to be needed, then check our estimate now and adjust
6100 the offset if necessary. */
6101 if (phdr_adjust_seg
!= NULL
)
6105 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6108 if (count
> phdr_adjust_num
)
6109 phdr_adjust_seg
->p_paddr
6110 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6115 #undef IS_CONTAINED_BY_VMA
6116 #undef IS_CONTAINED_BY_LMA
6118 #undef IS_COREFILE_NOTE
6119 #undef IS_SOLARIS_PT_INTERP
6120 #undef IS_SECTION_IN_INPUT_SEGMENT
6121 #undef INCLUDE_SECTION_IN_SEGMENT
6122 #undef SEGMENT_AFTER_SEGMENT
6123 #undef SEGMENT_OVERLAPS
6127 /* Copy ELF program header information. */
6130 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6132 Elf_Internal_Ehdr
*iehdr
;
6133 struct elf_segment_map
*map
;
6134 struct elf_segment_map
*map_first
;
6135 struct elf_segment_map
**pointer_to_map
;
6136 Elf_Internal_Phdr
*segment
;
6138 unsigned int num_segments
;
6139 bfd_boolean phdr_included
= FALSE
;
6140 bfd_boolean p_paddr_valid
;
6142 iehdr
= elf_elfheader (ibfd
);
6145 pointer_to_map
= &map_first
;
6147 /* If all the segment p_paddr fields are zero, don't set
6148 map->p_paddr_valid. */
6149 p_paddr_valid
= FALSE
;
6150 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6151 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6154 if (segment
->p_paddr
!= 0)
6156 p_paddr_valid
= TRUE
;
6160 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6165 unsigned int section_count
;
6167 Elf_Internal_Shdr
*this_hdr
;
6168 asection
*first_section
= NULL
;
6169 asection
*lowest_section
;
6171 /* Compute how many sections are in this segment. */
6172 for (section
= ibfd
->sections
, section_count
= 0;
6174 section
= section
->next
)
6176 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6177 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6179 if (first_section
== NULL
)
6180 first_section
= section
;
6185 /* Allocate a segment map big enough to contain
6186 all of the sections we have selected. */
6187 amt
= sizeof (struct elf_segment_map
);
6188 if (section_count
!= 0)
6189 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6190 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6194 /* Initialize the fields of the output segment map with the
6197 map
->p_type
= segment
->p_type
;
6198 map
->p_flags
= segment
->p_flags
;
6199 map
->p_flags_valid
= 1;
6200 map
->p_paddr
= segment
->p_paddr
;
6201 map
->p_paddr_valid
= p_paddr_valid
;
6202 map
->p_align
= segment
->p_align
;
6203 map
->p_align_valid
= 1;
6204 map
->p_vaddr_offset
= 0;
6206 if (map
->p_type
== PT_GNU_RELRO
6207 || map
->p_type
== PT_GNU_STACK
)
6209 /* The PT_GNU_RELRO segment may contain the first a few
6210 bytes in the .got.plt section even if the whole .got.plt
6211 section isn't in the PT_GNU_RELRO segment. We won't
6212 change the size of the PT_GNU_RELRO segment.
6213 Similarly, PT_GNU_STACK size is significant on uclinux
6215 map
->p_size
= segment
->p_memsz
;
6216 map
->p_size_valid
= 1;
6219 /* Determine if this segment contains the ELF file header
6220 and if it contains the program headers themselves. */
6221 map
->includes_filehdr
= (segment
->p_offset
== 0
6222 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6224 map
->includes_phdrs
= 0;
6225 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6227 map
->includes_phdrs
=
6228 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6229 && (segment
->p_offset
+ segment
->p_filesz
6230 >= ((bfd_vma
) iehdr
->e_phoff
6231 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6233 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6234 phdr_included
= TRUE
;
6237 lowest_section
= first_section
;
6238 if (section_count
!= 0)
6240 unsigned int isec
= 0;
6242 for (section
= first_section
;
6244 section
= section
->next
)
6246 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6247 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6249 map
->sections
[isec
++] = section
->output_section
;
6250 if ((section
->flags
& SEC_ALLOC
) != 0)
6254 if (section
->lma
< lowest_section
->lma
)
6255 lowest_section
= section
;
6257 /* Section lmas are set up from PT_LOAD header
6258 p_paddr in _bfd_elf_make_section_from_shdr.
6259 If this header has a p_paddr that disagrees
6260 with the section lma, flag the p_paddr as
6262 if ((section
->flags
& SEC_LOAD
) != 0)
6263 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6265 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6266 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6267 map
->p_paddr_valid
= FALSE
;
6269 if (isec
== section_count
)
6275 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6276 /* We need to keep the space used by the headers fixed. */
6277 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6279 if (!map
->includes_phdrs
6280 && !map
->includes_filehdr
6281 && map
->p_paddr_valid
)
6282 /* There is some other padding before the first section. */
6283 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6284 - segment
->p_paddr
);
6286 map
->count
= section_count
;
6287 *pointer_to_map
= map
;
6288 pointer_to_map
= &map
->next
;
6291 elf_seg_map (obfd
) = map_first
;
6295 /* Copy private BFD data. This copies or rewrites ELF program header
6299 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6301 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6302 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6305 if (elf_tdata (ibfd
)->phdr
== NULL
)
6308 if (ibfd
->xvec
== obfd
->xvec
)
6310 /* Check to see if any sections in the input BFD
6311 covered by ELF program header have changed. */
6312 Elf_Internal_Phdr
*segment
;
6313 asection
*section
, *osec
;
6314 unsigned int i
, num_segments
;
6315 Elf_Internal_Shdr
*this_hdr
;
6316 const struct elf_backend_data
*bed
;
6318 bed
= get_elf_backend_data (ibfd
);
6320 /* Regenerate the segment map if p_paddr is set to 0. */
6321 if (bed
->want_p_paddr_set_to_zero
)
6324 /* Initialize the segment mark field. */
6325 for (section
= obfd
->sections
; section
!= NULL
;
6326 section
= section
->next
)
6327 section
->segment_mark
= FALSE
;
6329 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6330 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6334 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6335 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6336 which severly confuses things, so always regenerate the segment
6337 map in this case. */
6338 if (segment
->p_paddr
== 0
6339 && segment
->p_memsz
== 0
6340 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6343 for (section
= ibfd
->sections
;
6344 section
!= NULL
; section
= section
->next
)
6346 /* We mark the output section so that we know it comes
6347 from the input BFD. */
6348 osec
= section
->output_section
;
6350 osec
->segment_mark
= TRUE
;
6352 /* Check if this section is covered by the segment. */
6353 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6354 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6356 /* FIXME: Check if its output section is changed or
6357 removed. What else do we need to check? */
6359 || section
->flags
!= osec
->flags
6360 || section
->lma
!= osec
->lma
6361 || section
->vma
!= osec
->vma
6362 || section
->size
!= osec
->size
6363 || section
->rawsize
!= osec
->rawsize
6364 || section
->alignment_power
!= osec
->alignment_power
)
6370 /* Check to see if any output section do not come from the
6372 for (section
= obfd
->sections
; section
!= NULL
;
6373 section
= section
->next
)
6375 if (section
->segment_mark
== FALSE
)
6378 section
->segment_mark
= FALSE
;
6381 return copy_elf_program_header (ibfd
, obfd
);
6385 if (ibfd
->xvec
== obfd
->xvec
)
6387 /* When rewriting program header, set the output maxpagesize to
6388 the maximum alignment of input PT_LOAD segments. */
6389 Elf_Internal_Phdr
*segment
;
6391 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6392 bfd_vma maxpagesize
= 0;
6394 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6397 if (segment
->p_type
== PT_LOAD
6398 && maxpagesize
< segment
->p_align
)
6399 maxpagesize
= segment
->p_align
;
6401 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6402 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6405 return rewrite_elf_program_header (ibfd
, obfd
);
6408 /* Initialize private output section information from input section. */
6411 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6415 struct bfd_link_info
*link_info
)
6418 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6419 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6421 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6422 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6425 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6427 /* For objcopy and relocatable link, don't copy the output ELF
6428 section type from input if the output BFD section flags have been
6429 set to something different. For a final link allow some flags
6430 that the linker clears to differ. */
6431 if (elf_section_type (osec
) == SHT_NULL
6432 && (osec
->flags
== isec
->flags
6434 && ((osec
->flags
^ isec
->flags
)
6435 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6436 elf_section_type (osec
) = elf_section_type (isec
);
6438 /* FIXME: Is this correct for all OS/PROC specific flags? */
6439 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6440 & (SHF_MASKOS
| SHF_MASKPROC
));
6442 /* Set things up for objcopy and relocatable link. The output
6443 SHT_GROUP section will have its elf_next_in_group pointing back
6444 to the input group members. Ignore linker created group section.
6445 See elfNN_ia64_object_p in elfxx-ia64.c. */
6448 if (elf_sec_group (isec
) == NULL
6449 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6451 if (elf_section_flags (isec
) & SHF_GROUP
)
6452 elf_section_flags (osec
) |= SHF_GROUP
;
6453 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6454 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6458 ihdr
= &elf_section_data (isec
)->this_hdr
;
6460 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6461 don't use the output section of the linked-to section since it
6462 may be NULL at this point. */
6463 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6465 ohdr
= &elf_section_data (osec
)->this_hdr
;
6466 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6467 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6470 osec
->use_rela_p
= isec
->use_rela_p
;
6475 /* Copy private section information. This copies over the entsize
6476 field, and sometimes the info field. */
6479 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6484 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6486 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6487 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6490 ihdr
= &elf_section_data (isec
)->this_hdr
;
6491 ohdr
= &elf_section_data (osec
)->this_hdr
;
6493 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6495 if (ihdr
->sh_type
== SHT_SYMTAB
6496 || ihdr
->sh_type
== SHT_DYNSYM
6497 || ihdr
->sh_type
== SHT_GNU_verneed
6498 || ihdr
->sh_type
== SHT_GNU_verdef
)
6499 ohdr
->sh_info
= ihdr
->sh_info
;
6501 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6505 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6506 necessary if we are removing either the SHT_GROUP section or any of
6507 the group member sections. DISCARDED is the value that a section's
6508 output_section has if the section will be discarded, NULL when this
6509 function is called from objcopy, bfd_abs_section_ptr when called
6513 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6517 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6518 if (elf_section_type (isec
) == SHT_GROUP
)
6520 asection
*first
= elf_next_in_group (isec
);
6521 asection
*s
= first
;
6522 bfd_size_type removed
= 0;
6526 /* If this member section is being output but the
6527 SHT_GROUP section is not, then clear the group info
6528 set up by _bfd_elf_copy_private_section_data. */
6529 if (s
->output_section
!= discarded
6530 && isec
->output_section
== discarded
)
6532 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6533 elf_group_name (s
->output_section
) = NULL
;
6535 /* Conversely, if the member section is not being output
6536 but the SHT_GROUP section is, then adjust its size. */
6537 else if (s
->output_section
== discarded
6538 && isec
->output_section
!= discarded
)
6540 s
= elf_next_in_group (s
);
6546 if (discarded
!= NULL
)
6548 /* If we've been called for ld -r, then we need to
6549 adjust the input section size. This function may
6550 be called multiple times, so save the original
6552 if (isec
->rawsize
== 0)
6553 isec
->rawsize
= isec
->size
;
6554 isec
->size
= isec
->rawsize
- removed
;
6558 /* Adjust the output section size when called from
6560 isec
->output_section
->size
-= removed
;
6568 /* Copy private header information. */
6571 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6573 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6574 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6577 /* Copy over private BFD data if it has not already been copied.
6578 This must be done here, rather than in the copy_private_bfd_data
6579 entry point, because the latter is called after the section
6580 contents have been set, which means that the program headers have
6581 already been worked out. */
6582 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6584 if (! copy_private_bfd_data (ibfd
, obfd
))
6588 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6591 /* Copy private symbol information. If this symbol is in a section
6592 which we did not map into a BFD section, try to map the section
6593 index correctly. We use special macro definitions for the mapped
6594 section indices; these definitions are interpreted by the
6595 swap_out_syms function. */
6597 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6598 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6599 #define MAP_STRTAB (SHN_HIOS + 3)
6600 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6601 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6604 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6609 elf_symbol_type
*isym
, *osym
;
6611 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6612 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6615 isym
= elf_symbol_from (ibfd
, isymarg
);
6616 osym
= elf_symbol_from (obfd
, osymarg
);
6619 && isym
->internal_elf_sym
.st_shndx
!= 0
6621 && bfd_is_abs_section (isym
->symbol
.section
))
6625 shndx
= isym
->internal_elf_sym
.st_shndx
;
6626 if (shndx
== elf_onesymtab (ibfd
))
6627 shndx
= MAP_ONESYMTAB
;
6628 else if (shndx
== elf_dynsymtab (ibfd
))
6629 shndx
= MAP_DYNSYMTAB
;
6630 else if (shndx
== elf_strtab_sec (ibfd
))
6632 else if (shndx
== elf_shstrtab_sec (ibfd
))
6633 shndx
= MAP_SHSTRTAB
;
6634 else if (shndx
== elf_symtab_shndx (ibfd
))
6635 shndx
= MAP_SYM_SHNDX
;
6636 osym
->internal_elf_sym
.st_shndx
= shndx
;
6642 /* Swap out the symbols. */
6645 swap_out_syms (bfd
*abfd
,
6646 struct bfd_strtab_hash
**sttp
,
6649 const struct elf_backend_data
*bed
;
6652 struct bfd_strtab_hash
*stt
;
6653 Elf_Internal_Shdr
*symtab_hdr
;
6654 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6655 Elf_Internal_Shdr
*symstrtab_hdr
;
6656 bfd_byte
*outbound_syms
;
6657 bfd_byte
*outbound_shndx
;
6659 unsigned int num_locals
;
6661 bfd_boolean name_local_sections
;
6663 if (!elf_map_symbols (abfd
, &num_locals
))
6666 /* Dump out the symtabs. */
6667 stt
= _bfd_elf_stringtab_init ();
6671 bed
= get_elf_backend_data (abfd
);
6672 symcount
= bfd_get_symcount (abfd
);
6673 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6674 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6675 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6676 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6677 symtab_hdr
->sh_info
= num_locals
+ 1;
6678 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6680 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6681 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6683 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6684 bed
->s
->sizeof_sym
);
6685 if (outbound_syms
== NULL
)
6687 _bfd_stringtab_free (stt
);
6690 symtab_hdr
->contents
= outbound_syms
;
6692 outbound_shndx
= NULL
;
6693 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6694 if (symtab_shndx_hdr
->sh_name
!= 0)
6696 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6697 outbound_shndx
= (bfd_byte
*)
6698 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6699 if (outbound_shndx
== NULL
)
6701 _bfd_stringtab_free (stt
);
6705 symtab_shndx_hdr
->contents
= outbound_shndx
;
6706 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6707 symtab_shndx_hdr
->sh_size
= amt
;
6708 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6709 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6712 /* Now generate the data (for "contents"). */
6714 /* Fill in zeroth symbol and swap it out. */
6715 Elf_Internal_Sym sym
;
6721 sym
.st_shndx
= SHN_UNDEF
;
6722 sym
.st_target_internal
= 0;
6723 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6724 outbound_syms
+= bed
->s
->sizeof_sym
;
6725 if (outbound_shndx
!= NULL
)
6726 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6730 = (bed
->elf_backend_name_local_section_symbols
6731 && bed
->elf_backend_name_local_section_symbols (abfd
));
6733 syms
= bfd_get_outsymbols (abfd
);
6734 for (idx
= 0; idx
< symcount
; idx
++)
6736 Elf_Internal_Sym sym
;
6737 bfd_vma value
= syms
[idx
]->value
;
6738 elf_symbol_type
*type_ptr
;
6739 flagword flags
= syms
[idx
]->flags
;
6742 if (!name_local_sections
6743 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6745 /* Local section symbols have no name. */
6750 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6753 if (sym
.st_name
== (unsigned long) -1)
6755 _bfd_stringtab_free (stt
);
6760 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6762 if ((flags
& BSF_SECTION_SYM
) == 0
6763 && bfd_is_com_section (syms
[idx
]->section
))
6765 /* ELF common symbols put the alignment into the `value' field,
6766 and the size into the `size' field. This is backwards from
6767 how BFD handles it, so reverse it here. */
6768 sym
.st_size
= value
;
6769 if (type_ptr
== NULL
6770 || type_ptr
->internal_elf_sym
.st_value
== 0)
6771 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6773 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6774 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6775 (abfd
, syms
[idx
]->section
);
6779 asection
*sec
= syms
[idx
]->section
;
6782 if (sec
->output_section
)
6784 value
+= sec
->output_offset
;
6785 sec
= sec
->output_section
;
6788 /* Don't add in the section vma for relocatable output. */
6789 if (! relocatable_p
)
6791 sym
.st_value
= value
;
6792 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6794 if (bfd_is_abs_section (sec
)
6796 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6798 /* This symbol is in a real ELF section which we did
6799 not create as a BFD section. Undo the mapping done
6800 by copy_private_symbol_data. */
6801 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6805 shndx
= elf_onesymtab (abfd
);
6808 shndx
= elf_dynsymtab (abfd
);
6811 shndx
= elf_strtab_sec (abfd
);
6814 shndx
= elf_shstrtab_sec (abfd
);
6817 shndx
= elf_symtab_shndx (abfd
);
6826 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6828 if (shndx
== SHN_BAD
)
6832 /* Writing this would be a hell of a lot easier if
6833 we had some decent documentation on bfd, and
6834 knew what to expect of the library, and what to
6835 demand of applications. For example, it
6836 appears that `objcopy' might not set the
6837 section of a symbol to be a section that is
6838 actually in the output file. */
6839 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6842 _bfd_error_handler (_("\
6843 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6844 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6846 bfd_set_error (bfd_error_invalid_operation
);
6847 _bfd_stringtab_free (stt
);
6851 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6852 BFD_ASSERT (shndx
!= SHN_BAD
);
6856 sym
.st_shndx
= shndx
;
6859 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6861 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6862 type
= STT_GNU_IFUNC
;
6863 else if ((flags
& BSF_FUNCTION
) != 0)
6865 else if ((flags
& BSF_OBJECT
) != 0)
6867 else if ((flags
& BSF_RELC
) != 0)
6869 else if ((flags
& BSF_SRELC
) != 0)
6874 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6877 /* Processor-specific types. */
6878 if (type_ptr
!= NULL
6879 && bed
->elf_backend_get_symbol_type
)
6880 type
= ((*bed
->elf_backend_get_symbol_type
)
6881 (&type_ptr
->internal_elf_sym
, type
));
6883 if (flags
& BSF_SECTION_SYM
)
6885 if (flags
& BSF_GLOBAL
)
6886 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6888 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6890 else if (bfd_is_com_section (syms
[idx
]->section
))
6892 #ifdef USE_STT_COMMON
6893 if (type
== STT_OBJECT
)
6894 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6897 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6899 else if (bfd_is_und_section (syms
[idx
]->section
))
6900 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6904 else if (flags
& BSF_FILE
)
6905 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6908 int bind
= STB_LOCAL
;
6910 if (flags
& BSF_LOCAL
)
6912 else if (flags
& BSF_GNU_UNIQUE
)
6913 bind
= STB_GNU_UNIQUE
;
6914 else if (flags
& BSF_WEAK
)
6916 else if (flags
& BSF_GLOBAL
)
6919 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6922 if (type_ptr
!= NULL
)
6924 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6925 sym
.st_target_internal
6926 = type_ptr
->internal_elf_sym
.st_target_internal
;
6931 sym
.st_target_internal
= 0;
6934 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6935 outbound_syms
+= bed
->s
->sizeof_sym
;
6936 if (outbound_shndx
!= NULL
)
6937 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6941 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6942 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6944 symstrtab_hdr
->sh_flags
= 0;
6945 symstrtab_hdr
->sh_addr
= 0;
6946 symstrtab_hdr
->sh_entsize
= 0;
6947 symstrtab_hdr
->sh_link
= 0;
6948 symstrtab_hdr
->sh_info
= 0;
6949 symstrtab_hdr
->sh_addralign
= 1;
6954 /* Return the number of bytes required to hold the symtab vector.
6956 Note that we base it on the count plus 1, since we will null terminate
6957 the vector allocated based on this size. However, the ELF symbol table
6958 always has a dummy entry as symbol #0, so it ends up even. */
6961 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6965 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6967 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6968 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6970 symtab_size
-= sizeof (asymbol
*);
6976 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6980 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6982 if (elf_dynsymtab (abfd
) == 0)
6984 bfd_set_error (bfd_error_invalid_operation
);
6988 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6989 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6991 symtab_size
-= sizeof (asymbol
*);
6997 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7000 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7003 /* Canonicalize the relocs. */
7006 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7013 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7015 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7018 tblptr
= section
->relocation
;
7019 for (i
= 0; i
< section
->reloc_count
; i
++)
7020 *relptr
++ = tblptr
++;
7024 return section
->reloc_count
;
7028 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7030 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7031 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7034 bfd_get_symcount (abfd
) = symcount
;
7039 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7040 asymbol
**allocation
)
7042 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7043 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7046 bfd_get_dynamic_symcount (abfd
) = symcount
;
7050 /* Return the size required for the dynamic reloc entries. Any loadable
7051 section that was actually installed in the BFD, and has type SHT_REL
7052 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7053 dynamic reloc section. */
7056 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7061 if (elf_dynsymtab (abfd
) == 0)
7063 bfd_set_error (bfd_error_invalid_operation
);
7067 ret
= sizeof (arelent
*);
7068 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7069 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7070 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7071 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7072 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7073 * sizeof (arelent
*));
7078 /* Canonicalize the dynamic relocation entries. Note that we return the
7079 dynamic relocations as a single block, although they are actually
7080 associated with particular sections; the interface, which was
7081 designed for SunOS style shared libraries, expects that there is only
7082 one set of dynamic relocs. Any loadable section that was actually
7083 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7084 dynamic symbol table, is considered to be a dynamic reloc section. */
7087 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7091 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7095 if (elf_dynsymtab (abfd
) == 0)
7097 bfd_set_error (bfd_error_invalid_operation
);
7101 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7103 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7105 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7106 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7107 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7112 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7114 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7116 for (i
= 0; i
< count
; i
++)
7127 /* Read in the version information. */
7130 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7132 bfd_byte
*contents
= NULL
;
7133 unsigned int freeidx
= 0;
7135 if (elf_dynverref (abfd
) != 0)
7137 Elf_Internal_Shdr
*hdr
;
7138 Elf_External_Verneed
*everneed
;
7139 Elf_Internal_Verneed
*iverneed
;
7141 bfd_byte
*contents_end
;
7143 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7145 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7146 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7147 if (elf_tdata (abfd
)->verref
== NULL
)
7150 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
7152 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7153 if (contents
== NULL
)
7155 error_return_verref
:
7156 elf_tdata (abfd
)->verref
= NULL
;
7157 elf_tdata (abfd
)->cverrefs
= 0;
7160 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7161 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7162 goto error_return_verref
;
7164 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7165 goto error_return_verref
;
7167 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7168 == sizeof (Elf_External_Vernaux
));
7169 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7170 everneed
= (Elf_External_Verneed
*) contents
;
7171 iverneed
= elf_tdata (abfd
)->verref
;
7172 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7174 Elf_External_Vernaux
*evernaux
;
7175 Elf_Internal_Vernaux
*ivernaux
;
7178 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7180 iverneed
->vn_bfd
= abfd
;
7182 iverneed
->vn_filename
=
7183 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7185 if (iverneed
->vn_filename
== NULL
)
7186 goto error_return_verref
;
7188 if (iverneed
->vn_cnt
== 0)
7189 iverneed
->vn_auxptr
= NULL
;
7192 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7193 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7194 sizeof (Elf_Internal_Vernaux
));
7195 if (iverneed
->vn_auxptr
== NULL
)
7196 goto error_return_verref
;
7199 if (iverneed
->vn_aux
7200 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7201 goto error_return_verref
;
7203 evernaux
= ((Elf_External_Vernaux
*)
7204 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7205 ivernaux
= iverneed
->vn_auxptr
;
7206 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7208 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7210 ivernaux
->vna_nodename
=
7211 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7212 ivernaux
->vna_name
);
7213 if (ivernaux
->vna_nodename
== NULL
)
7214 goto error_return_verref
;
7216 if (j
+ 1 < iverneed
->vn_cnt
)
7217 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7219 ivernaux
->vna_nextptr
= NULL
;
7221 if (ivernaux
->vna_next
7222 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7223 goto error_return_verref
;
7225 evernaux
= ((Elf_External_Vernaux
*)
7226 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7228 if (ivernaux
->vna_other
> freeidx
)
7229 freeidx
= ivernaux
->vna_other
;
7232 if (i
+ 1 < hdr
->sh_info
)
7233 iverneed
->vn_nextref
= iverneed
+ 1;
7235 iverneed
->vn_nextref
= NULL
;
7237 if (iverneed
->vn_next
7238 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7239 goto error_return_verref
;
7241 everneed
= ((Elf_External_Verneed
*)
7242 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7249 if (elf_dynverdef (abfd
) != 0)
7251 Elf_Internal_Shdr
*hdr
;
7252 Elf_External_Verdef
*everdef
;
7253 Elf_Internal_Verdef
*iverdef
;
7254 Elf_Internal_Verdef
*iverdefarr
;
7255 Elf_Internal_Verdef iverdefmem
;
7257 unsigned int maxidx
;
7258 bfd_byte
*contents_end_def
, *contents_end_aux
;
7260 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7262 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7263 if (contents
== NULL
)
7265 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7266 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7269 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7272 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7273 >= sizeof (Elf_External_Verdaux
));
7274 contents_end_def
= contents
+ hdr
->sh_size
7275 - sizeof (Elf_External_Verdef
);
7276 contents_end_aux
= contents
+ hdr
->sh_size
7277 - sizeof (Elf_External_Verdaux
);
7279 /* We know the number of entries in the section but not the maximum
7280 index. Therefore we have to run through all entries and find
7282 everdef
= (Elf_External_Verdef
*) contents
;
7284 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7286 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7288 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7289 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7291 if (iverdefmem
.vd_next
7292 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7295 everdef
= ((Elf_External_Verdef
*)
7296 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7299 if (default_imported_symver
)
7301 if (freeidx
> maxidx
)
7306 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7307 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7308 if (elf_tdata (abfd
)->verdef
== NULL
)
7311 elf_tdata (abfd
)->cverdefs
= maxidx
;
7313 everdef
= (Elf_External_Verdef
*) contents
;
7314 iverdefarr
= elf_tdata (abfd
)->verdef
;
7315 for (i
= 0; i
< hdr
->sh_info
; i
++)
7317 Elf_External_Verdaux
*everdaux
;
7318 Elf_Internal_Verdaux
*iverdaux
;
7321 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7323 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7325 error_return_verdef
:
7326 elf_tdata (abfd
)->verdef
= NULL
;
7327 elf_tdata (abfd
)->cverdefs
= 0;
7331 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7332 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7334 iverdef
->vd_bfd
= abfd
;
7336 if (iverdef
->vd_cnt
== 0)
7337 iverdef
->vd_auxptr
= NULL
;
7340 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7341 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7342 sizeof (Elf_Internal_Verdaux
));
7343 if (iverdef
->vd_auxptr
== NULL
)
7344 goto error_return_verdef
;
7348 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7349 goto error_return_verdef
;
7351 everdaux
= ((Elf_External_Verdaux
*)
7352 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7353 iverdaux
= iverdef
->vd_auxptr
;
7354 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7356 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7358 iverdaux
->vda_nodename
=
7359 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7360 iverdaux
->vda_name
);
7361 if (iverdaux
->vda_nodename
== NULL
)
7362 goto error_return_verdef
;
7364 if (j
+ 1 < iverdef
->vd_cnt
)
7365 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7367 iverdaux
->vda_nextptr
= NULL
;
7369 if (iverdaux
->vda_next
7370 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7371 goto error_return_verdef
;
7373 everdaux
= ((Elf_External_Verdaux
*)
7374 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7377 if (iverdef
->vd_cnt
)
7378 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7380 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7381 iverdef
->vd_nextdef
= iverdef
+ 1;
7383 iverdef
->vd_nextdef
= NULL
;
7385 everdef
= ((Elf_External_Verdef
*)
7386 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7392 else if (default_imported_symver
)
7399 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7400 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7401 if (elf_tdata (abfd
)->verdef
== NULL
)
7404 elf_tdata (abfd
)->cverdefs
= freeidx
;
7407 /* Create a default version based on the soname. */
7408 if (default_imported_symver
)
7410 Elf_Internal_Verdef
*iverdef
;
7411 Elf_Internal_Verdaux
*iverdaux
;
7413 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7415 iverdef
->vd_version
= VER_DEF_CURRENT
;
7416 iverdef
->vd_flags
= 0;
7417 iverdef
->vd_ndx
= freeidx
;
7418 iverdef
->vd_cnt
= 1;
7420 iverdef
->vd_bfd
= abfd
;
7422 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7423 if (iverdef
->vd_nodename
== NULL
)
7424 goto error_return_verdef
;
7425 iverdef
->vd_nextdef
= NULL
;
7426 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7427 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7428 if (iverdef
->vd_auxptr
== NULL
)
7429 goto error_return_verdef
;
7431 iverdaux
= iverdef
->vd_auxptr
;
7432 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7433 iverdaux
->vda_nextptr
= NULL
;
7439 if (contents
!= NULL
)
7445 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7447 elf_symbol_type
*newsym
;
7448 bfd_size_type amt
= sizeof (elf_symbol_type
);
7450 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7455 newsym
->symbol
.the_bfd
= abfd
;
7456 return &newsym
->symbol
;
7461 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7465 bfd_symbol_info (symbol
, ret
);
7468 /* Return whether a symbol name implies a local symbol. Most targets
7469 use this function for the is_local_label_name entry point, but some
7473 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7476 /* Normal local symbols start with ``.L''. */
7477 if (name
[0] == '.' && name
[1] == 'L')
7480 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7481 DWARF debugging symbols starting with ``..''. */
7482 if (name
[0] == '.' && name
[1] == '.')
7485 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7486 emitting DWARF debugging output. I suspect this is actually a
7487 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7488 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7489 underscore to be emitted on some ELF targets). For ease of use,
7490 we treat such symbols as local. */
7491 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7498 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7499 asymbol
*symbol ATTRIBUTE_UNUSED
)
7506 _bfd_elf_set_arch_mach (bfd
*abfd
,
7507 enum bfd_architecture arch
,
7508 unsigned long machine
)
7510 /* If this isn't the right architecture for this backend, and this
7511 isn't the generic backend, fail. */
7512 if (arch
!= get_elf_backend_data (abfd
)->arch
7513 && arch
!= bfd_arch_unknown
7514 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7517 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7520 /* Find the function to a particular section and offset,
7521 for error reporting. */
7524 elf_find_function (bfd
*abfd
,
7528 const char **filename_ptr
,
7529 const char **functionname_ptr
)
7531 struct elf_find_function_cache
7533 asection
*last_section
;
7535 const char *filename
;
7536 bfd_size_type func_size
;
7539 if (symbols
== NULL
)
7542 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
7545 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
7546 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
7550 if (cache
->last_section
!= section
7551 || cache
->func
== NULL
7552 || offset
< cache
->func
->value
7553 || offset
>= cache
->func
->value
+ cache
->func_size
)
7558 /* ??? Given multiple file symbols, it is impossible to reliably
7559 choose the right file name for global symbols. File symbols are
7560 local symbols, and thus all file symbols must sort before any
7561 global symbols. The ELF spec may be interpreted to say that a
7562 file symbol must sort before other local symbols, but currently
7563 ld -r doesn't do this. So, for ld -r output, it is possible to
7564 make a better choice of file name for local symbols by ignoring
7565 file symbols appearing after a given local symbol. */
7566 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7567 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7571 state
= nothing_seen
;
7572 cache
->filename
= NULL
;
7574 cache
->func_size
= 0;
7575 cache
->last_section
= section
;
7577 for (p
= symbols
; *p
!= NULL
; p
++)
7583 if ((sym
->flags
& BSF_FILE
) != 0)
7586 if (state
== symbol_seen
)
7587 state
= file_after_symbol_seen
;
7591 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
7593 && code_off
<= offset
7594 && (code_off
> low_func
7595 || (code_off
== low_func
7596 && size
> cache
->func_size
)))
7599 cache
->func_size
= size
;
7600 cache
->filename
= NULL
;
7601 low_func
= code_off
;
7603 && ((sym
->flags
& BSF_LOCAL
) != 0
7604 || state
!= file_after_symbol_seen
))
7605 cache
->filename
= bfd_asymbol_name (file
);
7607 if (state
== nothing_seen
)
7608 state
= symbol_seen
;
7612 if (cache
->func
== NULL
)
7616 *filename_ptr
= cache
->filename
;
7617 if (functionname_ptr
)
7618 *functionname_ptr
= bfd_asymbol_name (cache
->func
);
7623 /* Find the nearest line to a particular section and offset,
7624 for error reporting. */
7627 _bfd_elf_find_nearest_line (bfd
*abfd
,
7631 const char **filename_ptr
,
7632 const char **functionname_ptr
,
7633 unsigned int *line_ptr
)
7635 return _bfd_elf_find_nearest_line_discriminator (abfd
, section
, symbols
,
7636 offset
, filename_ptr
,
7643 _bfd_elf_find_nearest_line_discriminator (bfd
*abfd
,
7647 const char **filename_ptr
,
7648 const char **functionname_ptr
,
7649 unsigned int *line_ptr
,
7650 unsigned int *discriminator_ptr
)
7654 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7655 filename_ptr
, functionname_ptr
,
7658 if (!*functionname_ptr
)
7659 elf_find_function (abfd
, section
, symbols
, offset
,
7660 *filename_ptr
? NULL
: filename_ptr
,
7666 if (_bfd_dwarf2_find_nearest_line (abfd
, dwarf_debug_sections
,
7667 section
, symbols
, offset
,
7668 filename_ptr
, functionname_ptr
,
7669 line_ptr
, discriminator_ptr
, 0,
7670 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7672 if (!*functionname_ptr
)
7673 elf_find_function (abfd
, section
, symbols
, offset
,
7674 *filename_ptr
? NULL
: filename_ptr
,
7680 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7681 &found
, filename_ptr
,
7682 functionname_ptr
, line_ptr
,
7683 &elf_tdata (abfd
)->line_info
))
7685 if (found
&& (*functionname_ptr
|| *line_ptr
))
7688 if (symbols
== NULL
)
7691 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7692 filename_ptr
, functionname_ptr
))
7699 /* Find the line for a symbol. */
7702 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7703 const char **filename_ptr
, unsigned int *line_ptr
)
7705 return _bfd_elf_find_line_discriminator (abfd
, symbols
, symbol
,
7706 filename_ptr
, line_ptr
,
7711 _bfd_elf_find_line_discriminator (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7712 const char **filename_ptr
,
7713 unsigned int *line_ptr
,
7714 unsigned int *discriminator_ptr
)
7716 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7717 filename_ptr
, line_ptr
, discriminator_ptr
, 0,
7718 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7721 /* After a call to bfd_find_nearest_line, successive calls to
7722 bfd_find_inliner_info can be used to get source information about
7723 each level of function inlining that terminated at the address
7724 passed to bfd_find_nearest_line. Currently this is only supported
7725 for DWARF2 with appropriate DWARF3 extensions. */
7728 _bfd_elf_find_inliner_info (bfd
*abfd
,
7729 const char **filename_ptr
,
7730 const char **functionname_ptr
,
7731 unsigned int *line_ptr
)
7734 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7735 functionname_ptr
, line_ptr
,
7736 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7741 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7743 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7744 int ret
= bed
->s
->sizeof_ehdr
;
7746 if (!info
->relocatable
)
7748 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
7750 if (phdr_size
== (bfd_size_type
) -1)
7752 struct elf_segment_map
*m
;
7755 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
7756 phdr_size
+= bed
->s
->sizeof_phdr
;
7759 phdr_size
= get_program_header_size (abfd
, info
);
7762 elf_program_header_size (abfd
) = phdr_size
;
7770 _bfd_elf_set_section_contents (bfd
*abfd
,
7772 const void *location
,
7774 bfd_size_type count
)
7776 Elf_Internal_Shdr
*hdr
;
7779 if (! abfd
->output_has_begun
7780 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7783 hdr
= &elf_section_data (section
)->this_hdr
;
7784 pos
= hdr
->sh_offset
+ offset
;
7785 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7786 || bfd_bwrite (location
, count
, abfd
) != count
)
7793 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7794 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7795 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7800 /* Try to convert a non-ELF reloc into an ELF one. */
7803 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7805 /* Check whether we really have an ELF howto. */
7807 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7809 bfd_reloc_code_real_type code
;
7810 reloc_howto_type
*howto
;
7812 /* Alien reloc: Try to determine its type to replace it with an
7813 equivalent ELF reloc. */
7815 if (areloc
->howto
->pc_relative
)
7817 switch (areloc
->howto
->bitsize
)
7820 code
= BFD_RELOC_8_PCREL
;
7823 code
= BFD_RELOC_12_PCREL
;
7826 code
= BFD_RELOC_16_PCREL
;
7829 code
= BFD_RELOC_24_PCREL
;
7832 code
= BFD_RELOC_32_PCREL
;
7835 code
= BFD_RELOC_64_PCREL
;
7841 howto
= bfd_reloc_type_lookup (abfd
, code
);
7843 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7845 if (howto
->pcrel_offset
)
7846 areloc
->addend
+= areloc
->address
;
7848 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7853 switch (areloc
->howto
->bitsize
)
7859 code
= BFD_RELOC_14
;
7862 code
= BFD_RELOC_16
;
7865 code
= BFD_RELOC_26
;
7868 code
= BFD_RELOC_32
;
7871 code
= BFD_RELOC_64
;
7877 howto
= bfd_reloc_type_lookup (abfd
, code
);
7881 areloc
->howto
= howto
;
7889 (*_bfd_error_handler
)
7890 (_("%B: unsupported relocation type %s"),
7891 abfd
, areloc
->howto
->name
);
7892 bfd_set_error (bfd_error_bad_value
);
7897 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7899 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
7900 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
7902 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
7903 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7904 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
7907 return _bfd_generic_close_and_cleanup (abfd
);
7910 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7911 in the relocation's offset. Thus we cannot allow any sort of sanity
7912 range-checking to interfere. There is nothing else to do in processing
7915 bfd_reloc_status_type
7916 _bfd_elf_rel_vtable_reloc_fn
7917 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7918 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7919 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7920 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7922 return bfd_reloc_ok
;
7925 /* Elf core file support. Much of this only works on native
7926 toolchains, since we rely on knowing the
7927 machine-dependent procfs structure in order to pick
7928 out details about the corefile. */
7930 #ifdef HAVE_SYS_PROCFS_H
7931 /* Needed for new procfs interface on sparc-solaris. */
7932 # define _STRUCTURED_PROC 1
7933 # include <sys/procfs.h>
7936 /* Return a PID that identifies a "thread" for threaded cores, or the
7937 PID of the main process for non-threaded cores. */
7940 elfcore_make_pid (bfd
*abfd
)
7944 pid
= elf_tdata (abfd
)->core
->lwpid
;
7946 pid
= elf_tdata (abfd
)->core
->pid
;
7951 /* If there isn't a section called NAME, make one, using
7952 data from SECT. Note, this function will generate a
7953 reference to NAME, so you shouldn't deallocate or
7957 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7961 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7964 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7968 sect2
->size
= sect
->size
;
7969 sect2
->filepos
= sect
->filepos
;
7970 sect2
->alignment_power
= sect
->alignment_power
;
7974 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7975 actually creates up to two pseudosections:
7976 - For the single-threaded case, a section named NAME, unless
7977 such a section already exists.
7978 - For the multi-threaded case, a section named "NAME/PID", where
7979 PID is elfcore_make_pid (abfd).
7980 Both pseudosections have identical contents. */
7982 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7988 char *threaded_name
;
7992 /* Build the section name. */
7994 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7995 len
= strlen (buf
) + 1;
7996 threaded_name
= (char *) bfd_alloc (abfd
, len
);
7997 if (threaded_name
== NULL
)
7999 memcpy (threaded_name
, buf
, len
);
8001 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8006 sect
->filepos
= filepos
;
8007 sect
->alignment_power
= 2;
8009 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8012 /* prstatus_t exists on:
8014 linux 2.[01] + glibc
8018 #if defined (HAVE_PRSTATUS_T)
8021 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8026 if (note
->descsz
== sizeof (prstatus_t
))
8030 size
= sizeof (prstat
.pr_reg
);
8031 offset
= offsetof (prstatus_t
, pr_reg
);
8032 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8034 /* Do not overwrite the core signal if it
8035 has already been set by another thread. */
8036 if (elf_tdata (abfd
)->core
->signal
== 0)
8037 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8038 if (elf_tdata (abfd
)->core
->pid
== 0)
8039 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8041 /* pr_who exists on:
8044 pr_who doesn't exist on:
8047 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8048 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8050 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8053 #if defined (HAVE_PRSTATUS32_T)
8054 else if (note
->descsz
== sizeof (prstatus32_t
))
8056 /* 64-bit host, 32-bit corefile */
8057 prstatus32_t prstat
;
8059 size
= sizeof (prstat
.pr_reg
);
8060 offset
= offsetof (prstatus32_t
, pr_reg
);
8061 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8063 /* Do not overwrite the core signal if it
8064 has already been set by another thread. */
8065 if (elf_tdata (abfd
)->core
->signal
== 0)
8066 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8067 if (elf_tdata (abfd
)->core
->pid
== 0)
8068 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8070 /* pr_who exists on:
8073 pr_who doesn't exist on:
8076 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8077 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8079 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8082 #endif /* HAVE_PRSTATUS32_T */
8085 /* Fail - we don't know how to handle any other
8086 note size (ie. data object type). */
8090 /* Make a ".reg/999" section and a ".reg" section. */
8091 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8092 size
, note
->descpos
+ offset
);
8094 #endif /* defined (HAVE_PRSTATUS_T) */
8096 /* Create a pseudosection containing the exact contents of NOTE. */
8098 elfcore_make_note_pseudosection (bfd
*abfd
,
8100 Elf_Internal_Note
*note
)
8102 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8103 note
->descsz
, note
->descpos
);
8106 /* There isn't a consistent prfpregset_t across platforms,
8107 but it doesn't matter, because we don't have to pick this
8108 data structure apart. */
8111 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8113 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8116 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8117 type of NT_PRXFPREG. Just include the whole note's contents
8121 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8123 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8126 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8127 with a note type of NT_X86_XSTATE. Just include the whole note's
8128 contents literally. */
8131 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8133 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8137 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8139 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8143 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8145 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8149 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8151 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8155 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8157 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8161 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8163 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8167 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8169 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8173 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8175 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8179 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8181 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8185 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8187 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8191 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8193 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8197 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8199 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8203 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8205 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8209 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8211 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8215 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8217 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8221 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8223 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8226 #if defined (HAVE_PRPSINFO_T)
8227 typedef prpsinfo_t elfcore_psinfo_t
;
8228 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8229 typedef prpsinfo32_t elfcore_psinfo32_t
;
8233 #if defined (HAVE_PSINFO_T)
8234 typedef psinfo_t elfcore_psinfo_t
;
8235 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8236 typedef psinfo32_t elfcore_psinfo32_t
;
8240 /* return a malloc'ed copy of a string at START which is at
8241 most MAX bytes long, possibly without a terminating '\0'.
8242 the copy will always have a terminating '\0'. */
8245 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8248 char *end
= (char *) memchr (start
, '\0', max
);
8256 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8260 memcpy (dups
, start
, len
);
8266 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8268 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8270 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8272 elfcore_psinfo_t psinfo
;
8274 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8276 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8277 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8279 elf_tdata (abfd
)->core
->program
8280 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8281 sizeof (psinfo
.pr_fname
));
8283 elf_tdata (abfd
)->core
->command
8284 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8285 sizeof (psinfo
.pr_psargs
));
8287 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8288 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8290 /* 64-bit host, 32-bit corefile */
8291 elfcore_psinfo32_t psinfo
;
8293 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8295 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8296 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8298 elf_tdata (abfd
)->core
->program
8299 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8300 sizeof (psinfo
.pr_fname
));
8302 elf_tdata (abfd
)->core
->command
8303 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8304 sizeof (psinfo
.pr_psargs
));
8310 /* Fail - we don't know how to handle any other
8311 note size (ie. data object type). */
8315 /* Note that for some reason, a spurious space is tacked
8316 onto the end of the args in some (at least one anyway)
8317 implementations, so strip it off if it exists. */
8320 char *command
= elf_tdata (abfd
)->core
->command
;
8321 int n
= strlen (command
);
8323 if (0 < n
&& command
[n
- 1] == ' ')
8324 command
[n
- 1] = '\0';
8329 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8331 #if defined (HAVE_PSTATUS_T)
8333 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8335 if (note
->descsz
== sizeof (pstatus_t
)
8336 #if defined (HAVE_PXSTATUS_T)
8337 || note
->descsz
== sizeof (pxstatus_t
)
8343 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8345 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8347 #if defined (HAVE_PSTATUS32_T)
8348 else if (note
->descsz
== sizeof (pstatus32_t
))
8350 /* 64-bit host, 32-bit corefile */
8353 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8355 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8358 /* Could grab some more details from the "representative"
8359 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8360 NT_LWPSTATUS note, presumably. */
8364 #endif /* defined (HAVE_PSTATUS_T) */
8366 #if defined (HAVE_LWPSTATUS_T)
8368 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8370 lwpstatus_t lwpstat
;
8376 if (note
->descsz
!= sizeof (lwpstat
)
8377 #if defined (HAVE_LWPXSTATUS_T)
8378 && note
->descsz
!= sizeof (lwpxstatus_t
)
8383 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8385 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8386 /* Do not overwrite the core signal if it has already been set by
8388 if (elf_tdata (abfd
)->core
->signal
== 0)
8389 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8391 /* Make a ".reg/999" section. */
8393 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8394 len
= strlen (buf
) + 1;
8395 name
= bfd_alloc (abfd
, len
);
8398 memcpy (name
, buf
, len
);
8400 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8404 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8405 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8406 sect
->filepos
= note
->descpos
8407 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8410 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8411 sect
->size
= sizeof (lwpstat
.pr_reg
);
8412 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8415 sect
->alignment_power
= 2;
8417 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8420 /* Make a ".reg2/999" section */
8422 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8423 len
= strlen (buf
) + 1;
8424 name
= bfd_alloc (abfd
, len
);
8427 memcpy (name
, buf
, len
);
8429 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8433 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8434 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8435 sect
->filepos
= note
->descpos
8436 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8439 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8440 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8441 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8444 sect
->alignment_power
= 2;
8446 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8448 #endif /* defined (HAVE_LWPSTATUS_T) */
8451 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8458 int is_active_thread
;
8461 if (note
->descsz
< 728)
8464 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8467 type
= bfd_get_32 (abfd
, note
->descdata
);
8471 case 1 /* NOTE_INFO_PROCESS */:
8472 /* FIXME: need to add ->core->command. */
8473 /* process_info.pid */
8474 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8475 /* process_info.signal */
8476 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8479 case 2 /* NOTE_INFO_THREAD */:
8480 /* Make a ".reg/999" section. */
8481 /* thread_info.tid */
8482 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8484 len
= strlen (buf
) + 1;
8485 name
= (char *) bfd_alloc (abfd
, len
);
8489 memcpy (name
, buf
, len
);
8491 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8495 /* sizeof (thread_info.thread_context) */
8497 /* offsetof (thread_info.thread_context) */
8498 sect
->filepos
= note
->descpos
+ 12;
8499 sect
->alignment_power
= 2;
8501 /* thread_info.is_active_thread */
8502 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8504 if (is_active_thread
)
8505 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8509 case 3 /* NOTE_INFO_MODULE */:
8510 /* Make a ".module/xxxxxxxx" section. */
8511 /* module_info.base_address */
8512 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8513 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8515 len
= strlen (buf
) + 1;
8516 name
= (char *) bfd_alloc (abfd
, len
);
8520 memcpy (name
, buf
, len
);
8522 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8527 sect
->size
= note
->descsz
;
8528 sect
->filepos
= note
->descpos
;
8529 sect
->alignment_power
= 2;
8540 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8542 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8550 if (bed
->elf_backend_grok_prstatus
)
8551 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8553 #if defined (HAVE_PRSTATUS_T)
8554 return elfcore_grok_prstatus (abfd
, note
);
8559 #if defined (HAVE_PSTATUS_T)
8561 return elfcore_grok_pstatus (abfd
, note
);
8564 #if defined (HAVE_LWPSTATUS_T)
8566 return elfcore_grok_lwpstatus (abfd
, note
);
8569 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8570 return elfcore_grok_prfpreg (abfd
, note
);
8572 case NT_WIN32PSTATUS
:
8573 return elfcore_grok_win32pstatus (abfd
, note
);
8575 case NT_PRXFPREG
: /* Linux SSE extension */
8576 if (note
->namesz
== 6
8577 && strcmp (note
->namedata
, "LINUX") == 0)
8578 return elfcore_grok_prxfpreg (abfd
, note
);
8582 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8583 if (note
->namesz
== 6
8584 && strcmp (note
->namedata
, "LINUX") == 0)
8585 return elfcore_grok_xstatereg (abfd
, note
);
8590 if (note
->namesz
== 6
8591 && strcmp (note
->namedata
, "LINUX") == 0)
8592 return elfcore_grok_ppc_vmx (abfd
, note
);
8597 if (note
->namesz
== 6
8598 && strcmp (note
->namedata
, "LINUX") == 0)
8599 return elfcore_grok_ppc_vsx (abfd
, note
);
8603 case NT_S390_HIGH_GPRS
:
8604 if (note
->namesz
== 6
8605 && strcmp (note
->namedata
, "LINUX") == 0)
8606 return elfcore_grok_s390_high_gprs (abfd
, note
);
8611 if (note
->namesz
== 6
8612 && strcmp (note
->namedata
, "LINUX") == 0)
8613 return elfcore_grok_s390_timer (abfd
, note
);
8617 case NT_S390_TODCMP
:
8618 if (note
->namesz
== 6
8619 && strcmp (note
->namedata
, "LINUX") == 0)
8620 return elfcore_grok_s390_todcmp (abfd
, note
);
8624 case NT_S390_TODPREG
:
8625 if (note
->namesz
== 6
8626 && strcmp (note
->namedata
, "LINUX") == 0)
8627 return elfcore_grok_s390_todpreg (abfd
, note
);
8632 if (note
->namesz
== 6
8633 && strcmp (note
->namedata
, "LINUX") == 0)
8634 return elfcore_grok_s390_ctrs (abfd
, note
);
8638 case NT_S390_PREFIX
:
8639 if (note
->namesz
== 6
8640 && strcmp (note
->namedata
, "LINUX") == 0)
8641 return elfcore_grok_s390_prefix (abfd
, note
);
8645 case NT_S390_LAST_BREAK
:
8646 if (note
->namesz
== 6
8647 && strcmp (note
->namedata
, "LINUX") == 0)
8648 return elfcore_grok_s390_last_break (abfd
, note
);
8652 case NT_S390_SYSTEM_CALL
:
8653 if (note
->namesz
== 6
8654 && strcmp (note
->namedata
, "LINUX") == 0)
8655 return elfcore_grok_s390_system_call (abfd
, note
);
8660 if (note
->namesz
== 6
8661 && strcmp (note
->namedata
, "LINUX") == 0)
8662 return elfcore_grok_s390_tdb (abfd
, note
);
8667 if (note
->namesz
== 6
8668 && strcmp (note
->namedata
, "LINUX") == 0)
8669 return elfcore_grok_arm_vfp (abfd
, note
);
8674 if (note
->namesz
== 6
8675 && strcmp (note
->namedata
, "LINUX") == 0)
8676 return elfcore_grok_aarch_tls (abfd
, note
);
8680 case NT_ARM_HW_BREAK
:
8681 if (note
->namesz
== 6
8682 && strcmp (note
->namedata
, "LINUX") == 0)
8683 return elfcore_grok_aarch_hw_break (abfd
, note
);
8687 case NT_ARM_HW_WATCH
:
8688 if (note
->namesz
== 6
8689 && strcmp (note
->namedata
, "LINUX") == 0)
8690 return elfcore_grok_aarch_hw_watch (abfd
, note
);
8696 if (bed
->elf_backend_grok_psinfo
)
8697 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8699 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8700 return elfcore_grok_psinfo (abfd
, note
);
8707 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8712 sect
->size
= note
->descsz
;
8713 sect
->filepos
= note
->descpos
;
8714 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8720 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
8724 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
8730 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8732 struct elf_obj_tdata
*t
;
8734 if (note
->descsz
== 0)
8737 t
= elf_tdata (abfd
);
8738 t
->build_id
= bfd_alloc (abfd
, sizeof (*t
->build_id
) - 1 + note
->descsz
);
8739 if (t
->build_id
== NULL
)
8742 t
->build_id
->size
= note
->descsz
;
8743 memcpy (t
->build_id
->data
, note
->descdata
, note
->descsz
);
8749 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8756 case NT_GNU_BUILD_ID
:
8757 return elfobj_grok_gnu_build_id (abfd
, note
);
8762 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
8764 struct sdt_note
*cur
=
8765 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
8768 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
8769 cur
->size
= (bfd_size_type
) note
->descsz
;
8770 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
8772 elf_tdata (abfd
)->sdt_note_head
= cur
;
8778 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8783 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
8791 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8795 cp
= strchr (note
->namedata
, '@');
8798 *lwpidp
= atoi(cp
+ 1);
8805 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8807 /* Signal number at offset 0x08. */
8808 elf_tdata (abfd
)->core
->signal
8809 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8811 /* Process ID at offset 0x50. */
8812 elf_tdata (abfd
)->core
->pid
8813 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8815 /* Command name at 0x7c (max 32 bytes, including nul). */
8816 elf_tdata (abfd
)->core
->command
8817 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8819 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8824 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8828 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8829 elf_tdata (abfd
)->core
->lwpid
= lwp
;
8831 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8833 /* NetBSD-specific core "procinfo". Note that we expect to
8834 find this note before any of the others, which is fine,
8835 since the kernel writes this note out first when it
8836 creates a core file. */
8838 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8841 /* As of Jan 2002 there are no other machine-independent notes
8842 defined for NetBSD core files. If the note type is less
8843 than the start of the machine-dependent note types, we don't
8846 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8850 switch (bfd_get_arch (abfd
))
8852 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8853 PT_GETFPREGS == mach+2. */
8855 case bfd_arch_alpha
:
8856 case bfd_arch_sparc
:
8859 case NT_NETBSDCORE_FIRSTMACH
+0:
8860 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8862 case NT_NETBSDCORE_FIRSTMACH
+2:
8863 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8869 /* On all other arch's, PT_GETREGS == mach+1 and
8870 PT_GETFPREGS == mach+3. */
8875 case NT_NETBSDCORE_FIRSTMACH
+1:
8876 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8878 case NT_NETBSDCORE_FIRSTMACH
+3:
8879 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8889 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8891 /* Signal number at offset 0x08. */
8892 elf_tdata (abfd
)->core
->signal
8893 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8895 /* Process ID at offset 0x20. */
8896 elf_tdata (abfd
)->core
->pid
8897 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
8899 /* Command name at 0x48 (max 32 bytes, including nul). */
8900 elf_tdata (abfd
)->core
->command
8901 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
8907 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8909 if (note
->type
== NT_OPENBSD_PROCINFO
)
8910 return elfcore_grok_openbsd_procinfo (abfd
, note
);
8912 if (note
->type
== NT_OPENBSD_REGS
)
8913 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8915 if (note
->type
== NT_OPENBSD_FPREGS
)
8916 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8918 if (note
->type
== NT_OPENBSD_XFPREGS
)
8919 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8921 if (note
->type
== NT_OPENBSD_AUXV
)
8923 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8928 sect
->size
= note
->descsz
;
8929 sect
->filepos
= note
->descpos
;
8930 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8935 if (note
->type
== NT_OPENBSD_WCOOKIE
)
8937 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
8942 sect
->size
= note
->descsz
;
8943 sect
->filepos
= note
->descpos
;
8944 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8953 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8955 void *ddata
= note
->descdata
;
8962 /* nto_procfs_status 'pid' field is at offset 0. */
8963 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8965 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8966 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8968 /* nto_procfs_status 'flags' field is at offset 8. */
8969 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8971 /* nto_procfs_status 'what' field is at offset 14. */
8972 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8974 elf_tdata (abfd
)->core
->signal
= sig
;
8975 elf_tdata (abfd
)->core
->lwpid
= *tid
;
8978 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8979 do not come from signals so we make sure we set the current
8980 thread just in case. */
8981 if (flags
& 0x00000080)
8982 elf_tdata (abfd
)->core
->lwpid
= *tid
;
8984 /* Make a ".qnx_core_status/%d" section. */
8985 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8987 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8992 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8996 sect
->size
= note
->descsz
;
8997 sect
->filepos
= note
->descpos
;
8998 sect
->alignment_power
= 2;
9000 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9004 elfcore_grok_nto_regs (bfd
*abfd
,
9005 Elf_Internal_Note
*note
,
9013 /* Make a "(base)/%d" section. */
9014 sprintf (buf
, "%s/%ld", base
, tid
);
9016 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9021 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9025 sect
->size
= note
->descsz
;
9026 sect
->filepos
= note
->descpos
;
9027 sect
->alignment_power
= 2;
9029 /* This is the current thread. */
9030 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9031 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9036 #define BFD_QNT_CORE_INFO 7
9037 #define BFD_QNT_CORE_STATUS 8
9038 #define BFD_QNT_CORE_GREG 9
9039 #define BFD_QNT_CORE_FPREG 10
9042 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9044 /* Every GREG section has a STATUS section before it. Store the
9045 tid from the previous call to pass down to the next gregs
9047 static long tid
= 1;
9051 case BFD_QNT_CORE_INFO
:
9052 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9053 case BFD_QNT_CORE_STATUS
:
9054 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9055 case BFD_QNT_CORE_GREG
:
9056 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9057 case BFD_QNT_CORE_FPREG
:
9058 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9065 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9071 /* Use note name as section name. */
9073 name
= (char *) bfd_alloc (abfd
, len
);
9076 memcpy (name
, note
->namedata
, len
);
9077 name
[len
- 1] = '\0';
9079 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9083 sect
->size
= note
->descsz
;
9084 sect
->filepos
= note
->descpos
;
9085 sect
->alignment_power
= 1;
9090 /* Function: elfcore_write_note
9093 buffer to hold note, and current size of buffer
9097 size of data for note
9099 Writes note to end of buffer. ELF64 notes are written exactly as
9100 for ELF32, despite the current (as of 2006) ELF gabi specifying
9101 that they ought to have 8-byte namesz and descsz field, and have
9102 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9105 Pointer to realloc'd buffer, *BUFSIZ updated. */
9108 elfcore_write_note (bfd
*abfd
,
9116 Elf_External_Note
*xnp
;
9123 namesz
= strlen (name
) + 1;
9125 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9127 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9130 dest
= buf
+ *bufsiz
;
9131 *bufsiz
+= newspace
;
9132 xnp
= (Elf_External_Note
*) dest
;
9133 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9134 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9135 H_PUT_32 (abfd
, type
, xnp
->type
);
9139 memcpy (dest
, name
, namesz
);
9147 memcpy (dest
, input
, size
);
9158 elfcore_write_prpsinfo (bfd
*abfd
,
9164 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9166 if (bed
->elf_backend_write_core_note
!= NULL
)
9169 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9170 NT_PRPSINFO
, fname
, psargs
);
9175 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9176 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9177 if (bed
->s
->elfclass
== ELFCLASS32
)
9179 #if defined (HAVE_PSINFO32_T)
9181 int note_type
= NT_PSINFO
;
9184 int note_type
= NT_PRPSINFO
;
9187 memset (&data
, 0, sizeof (data
));
9188 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9189 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9190 return elfcore_write_note (abfd
, buf
, bufsiz
,
9191 "CORE", note_type
, &data
, sizeof (data
));
9196 #if defined (HAVE_PSINFO_T)
9198 int note_type
= NT_PSINFO
;
9201 int note_type
= NT_PRPSINFO
;
9204 memset (&data
, 0, sizeof (data
));
9205 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9206 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9207 return elfcore_write_note (abfd
, buf
, bufsiz
,
9208 "CORE", note_type
, &data
, sizeof (data
));
9210 #endif /* PSINFO_T or PRPSINFO_T */
9217 elfcore_write_linux_prpsinfo32
9218 (bfd
*abfd
, char *buf
, int *bufsiz
,
9219 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9221 struct elf_external_linux_prpsinfo32 data
;
9223 memset (&data
, 0, sizeof (data
));
9224 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9226 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9227 &data
, sizeof (data
));
9231 elfcore_write_linux_prpsinfo64
9232 (bfd
*abfd
, char *buf
, int *bufsiz
,
9233 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9235 struct elf_external_linux_prpsinfo64 data
;
9237 memset (&data
, 0, sizeof (data
));
9238 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9240 return elfcore_write_note (abfd
, buf
, bufsiz
,
9241 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9245 elfcore_write_prstatus (bfd
*abfd
,
9252 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9254 if (bed
->elf_backend_write_core_note
!= NULL
)
9257 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9259 pid
, cursig
, gregs
);
9264 #if defined (HAVE_PRSTATUS_T)
9265 #if defined (HAVE_PRSTATUS32_T)
9266 if (bed
->s
->elfclass
== ELFCLASS32
)
9268 prstatus32_t prstat
;
9270 memset (&prstat
, 0, sizeof (prstat
));
9271 prstat
.pr_pid
= pid
;
9272 prstat
.pr_cursig
= cursig
;
9273 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9274 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9275 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9282 memset (&prstat
, 0, sizeof (prstat
));
9283 prstat
.pr_pid
= pid
;
9284 prstat
.pr_cursig
= cursig
;
9285 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9286 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9287 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9289 #endif /* HAVE_PRSTATUS_T */
9295 #if defined (HAVE_LWPSTATUS_T)
9297 elfcore_write_lwpstatus (bfd
*abfd
,
9304 lwpstatus_t lwpstat
;
9305 const char *note_name
= "CORE";
9307 memset (&lwpstat
, 0, sizeof (lwpstat
));
9308 lwpstat
.pr_lwpid
= pid
>> 16;
9309 lwpstat
.pr_cursig
= cursig
;
9310 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9311 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9312 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9314 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9315 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9317 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9318 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9321 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9322 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9324 #endif /* HAVE_LWPSTATUS_T */
9326 #if defined (HAVE_PSTATUS_T)
9328 elfcore_write_pstatus (bfd
*abfd
,
9332 int cursig ATTRIBUTE_UNUSED
,
9333 const void *gregs ATTRIBUTE_UNUSED
)
9335 const char *note_name
= "CORE";
9336 #if defined (HAVE_PSTATUS32_T)
9337 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9339 if (bed
->s
->elfclass
== ELFCLASS32
)
9343 memset (&pstat
, 0, sizeof (pstat
));
9344 pstat
.pr_pid
= pid
& 0xffff;
9345 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9346 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9354 memset (&pstat
, 0, sizeof (pstat
));
9355 pstat
.pr_pid
= pid
& 0xffff;
9356 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9357 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9361 #endif /* HAVE_PSTATUS_T */
9364 elfcore_write_prfpreg (bfd
*abfd
,
9370 const char *note_name
= "CORE";
9371 return elfcore_write_note (abfd
, buf
, bufsiz
,
9372 note_name
, NT_FPREGSET
, fpregs
, size
);
9376 elfcore_write_prxfpreg (bfd
*abfd
,
9379 const void *xfpregs
,
9382 char *note_name
= "LINUX";
9383 return elfcore_write_note (abfd
, buf
, bufsiz
,
9384 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9388 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9389 const void *xfpregs
, int size
)
9391 char *note_name
= "LINUX";
9392 return elfcore_write_note (abfd
, buf
, bufsiz
,
9393 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9397 elfcore_write_ppc_vmx (bfd
*abfd
,
9400 const void *ppc_vmx
,
9403 char *note_name
= "LINUX";
9404 return elfcore_write_note (abfd
, buf
, bufsiz
,
9405 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9409 elfcore_write_ppc_vsx (bfd
*abfd
,
9412 const void *ppc_vsx
,
9415 char *note_name
= "LINUX";
9416 return elfcore_write_note (abfd
, buf
, bufsiz
,
9417 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9421 elfcore_write_s390_high_gprs (bfd
*abfd
,
9424 const void *s390_high_gprs
,
9427 char *note_name
= "LINUX";
9428 return elfcore_write_note (abfd
, buf
, bufsiz
,
9429 note_name
, NT_S390_HIGH_GPRS
,
9430 s390_high_gprs
, size
);
9434 elfcore_write_s390_timer (bfd
*abfd
,
9437 const void *s390_timer
,
9440 char *note_name
= "LINUX";
9441 return elfcore_write_note (abfd
, buf
, bufsiz
,
9442 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9446 elfcore_write_s390_todcmp (bfd
*abfd
,
9449 const void *s390_todcmp
,
9452 char *note_name
= "LINUX";
9453 return elfcore_write_note (abfd
, buf
, bufsiz
,
9454 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9458 elfcore_write_s390_todpreg (bfd
*abfd
,
9461 const void *s390_todpreg
,
9464 char *note_name
= "LINUX";
9465 return elfcore_write_note (abfd
, buf
, bufsiz
,
9466 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9470 elfcore_write_s390_ctrs (bfd
*abfd
,
9473 const void *s390_ctrs
,
9476 char *note_name
= "LINUX";
9477 return elfcore_write_note (abfd
, buf
, bufsiz
,
9478 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9482 elfcore_write_s390_prefix (bfd
*abfd
,
9485 const void *s390_prefix
,
9488 char *note_name
= "LINUX";
9489 return elfcore_write_note (abfd
, buf
, bufsiz
,
9490 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9494 elfcore_write_s390_last_break (bfd
*abfd
,
9497 const void *s390_last_break
,
9500 char *note_name
= "LINUX";
9501 return elfcore_write_note (abfd
, buf
, bufsiz
,
9502 note_name
, NT_S390_LAST_BREAK
,
9503 s390_last_break
, size
);
9507 elfcore_write_s390_system_call (bfd
*abfd
,
9510 const void *s390_system_call
,
9513 char *note_name
= "LINUX";
9514 return elfcore_write_note (abfd
, buf
, bufsiz
,
9515 note_name
, NT_S390_SYSTEM_CALL
,
9516 s390_system_call
, size
);
9520 elfcore_write_s390_tdb (bfd
*abfd
,
9523 const void *s390_tdb
,
9526 char *note_name
= "LINUX";
9527 return elfcore_write_note (abfd
, buf
, bufsiz
,
9528 note_name
, NT_S390_TDB
, s390_tdb
, size
);
9532 elfcore_write_arm_vfp (bfd
*abfd
,
9535 const void *arm_vfp
,
9538 char *note_name
= "LINUX";
9539 return elfcore_write_note (abfd
, buf
, bufsiz
,
9540 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
9544 elfcore_write_aarch_tls (bfd
*abfd
,
9547 const void *aarch_tls
,
9550 char *note_name
= "LINUX";
9551 return elfcore_write_note (abfd
, buf
, bufsiz
,
9552 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
9556 elfcore_write_aarch_hw_break (bfd
*abfd
,
9559 const void *aarch_hw_break
,
9562 char *note_name
= "LINUX";
9563 return elfcore_write_note (abfd
, buf
, bufsiz
,
9564 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
9568 elfcore_write_aarch_hw_watch (bfd
*abfd
,
9571 const void *aarch_hw_watch
,
9574 char *note_name
= "LINUX";
9575 return elfcore_write_note (abfd
, buf
, bufsiz
,
9576 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
9580 elfcore_write_register_note (bfd
*abfd
,
9583 const char *section
,
9587 if (strcmp (section
, ".reg2") == 0)
9588 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9589 if (strcmp (section
, ".reg-xfp") == 0)
9590 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9591 if (strcmp (section
, ".reg-xstate") == 0)
9592 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9593 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9594 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9595 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9596 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9597 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9598 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9599 if (strcmp (section
, ".reg-s390-timer") == 0)
9600 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9601 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9602 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9603 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9604 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9605 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9606 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9607 if (strcmp (section
, ".reg-s390-prefix") == 0)
9608 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9609 if (strcmp (section
, ".reg-s390-last-break") == 0)
9610 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
9611 if (strcmp (section
, ".reg-s390-system-call") == 0)
9612 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
9613 if (strcmp (section
, ".reg-s390-tdb") == 0)
9614 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
9615 if (strcmp (section
, ".reg-arm-vfp") == 0)
9616 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
9617 if (strcmp (section
, ".reg-aarch-tls") == 0)
9618 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
9619 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
9620 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
9621 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
9622 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
9627 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9632 while (p
< buf
+ size
)
9634 /* FIXME: bad alignment assumption. */
9635 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9636 Elf_Internal_Note in
;
9638 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9641 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9643 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9644 in
.namedata
= xnp
->name
;
9645 if (in
.namesz
> buf
- in
.namedata
+ size
)
9648 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9649 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9650 in
.descpos
= offset
+ (in
.descdata
- buf
);
9652 && (in
.descdata
>= buf
+ size
9653 || in
.descsz
> buf
- in
.descdata
+ size
))
9656 switch (bfd_get_format (abfd
))
9662 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
9664 if (! elfcore_grok_netbsd_note (abfd
, &in
))
9667 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
9669 if (! elfcore_grok_openbsd_note (abfd
, &in
))
9672 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
9674 if (! elfcore_grok_nto_note (abfd
, &in
))
9677 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
9679 if (! elfcore_grok_spu_note (abfd
, &in
))
9684 if (! elfcore_grok_note (abfd
, &in
))
9690 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9692 if (! elfobj_grok_gnu_note (abfd
, &in
))
9695 else if (in
.namesz
== sizeof "stapsdt"
9696 && strcmp (in
.namedata
, "stapsdt") == 0)
9698 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
9704 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9711 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9718 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9721 buf
= (char *) bfd_malloc (size
);
9725 if (bfd_bread (buf
, size
, abfd
) != size
9726 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9736 /* Providing external access to the ELF program header table. */
9738 /* Return an upper bound on the number of bytes required to store a
9739 copy of ABFD's program header table entries. Return -1 if an error
9740 occurs; bfd_get_error will return an appropriate code. */
9743 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9745 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9747 bfd_set_error (bfd_error_wrong_format
);
9751 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9754 /* Copy ABFD's program header table entries to *PHDRS. The entries
9755 will be stored as an array of Elf_Internal_Phdr structures, as
9756 defined in include/elf/internal.h. To find out how large the
9757 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9759 Return the number of program header table entries read, or -1 if an
9760 error occurs; bfd_get_error will return an appropriate code. */
9763 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9767 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9769 bfd_set_error (bfd_error_wrong_format
);
9773 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9774 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9775 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9780 enum elf_reloc_type_class
9781 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
9782 const asection
*rel_sec ATTRIBUTE_UNUSED
,
9783 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9785 return reloc_class_normal
;
9788 /* For RELA architectures, return the relocation value for a
9789 relocation against a local symbol. */
9792 _bfd_elf_rela_local_sym (bfd
*abfd
,
9793 Elf_Internal_Sym
*sym
,
9795 Elf_Internal_Rela
*rel
)
9797 asection
*sec
= *psec
;
9800 relocation
= (sec
->output_section
->vma
9801 + sec
->output_offset
9803 if ((sec
->flags
& SEC_MERGE
)
9804 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9805 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
9808 _bfd_merged_section_offset (abfd
, psec
,
9809 elf_section_data (sec
)->sec_info
,
9810 sym
->st_value
+ rel
->r_addend
);
9813 /* If we have changed the section, and our original section is
9814 marked with SEC_EXCLUDE, it means that the original
9815 SEC_MERGE section has been completely subsumed in some
9816 other SEC_MERGE section. In this case, we need to leave
9817 some info around for --emit-relocs. */
9818 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9819 sec
->kept_section
= *psec
;
9822 rel
->r_addend
-= relocation
;
9823 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9829 _bfd_elf_rel_local_sym (bfd
*abfd
,
9830 Elf_Internal_Sym
*sym
,
9834 asection
*sec
= *psec
;
9836 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
9837 return sym
->st_value
+ addend
;
9839 return _bfd_merged_section_offset (abfd
, psec
,
9840 elf_section_data (sec
)->sec_info
,
9841 sym
->st_value
+ addend
);
9845 _bfd_elf_section_offset (bfd
*abfd
,
9846 struct bfd_link_info
*info
,
9850 switch (sec
->sec_info_type
)
9852 case SEC_INFO_TYPE_STABS
:
9853 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
9855 case SEC_INFO_TYPE_EH_FRAME
:
9856 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
9858 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
9860 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9861 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
9862 offset
= sec
->size
- offset
- address_size
;
9868 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9869 reconstruct an ELF file by reading the segments out of remote memory
9870 based on the ELF file header at EHDR_VMA and the ELF program headers it
9871 points to. If not null, *LOADBASEP is filled in with the difference
9872 between the VMAs from which the segments were read, and the VMAs the
9873 file headers (and hence BFD's idea of each section's VMA) put them at.
9875 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9876 remote memory at target address VMA into the local buffer at MYADDR; it
9877 should return zero on success or an `errno' code on failure. TEMPL must
9878 be a BFD for an ELF target with the word size and byte order found in
9879 the remote memory. */
9882 bfd_elf_bfd_from_remote_memory
9886 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
9888 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
9889 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
9893 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
9894 long symcount ATTRIBUTE_UNUSED
,
9895 asymbol
**syms ATTRIBUTE_UNUSED
,
9900 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9903 const char *relplt_name
;
9904 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
9908 Elf_Internal_Shdr
*hdr
;
9914 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
9917 if (dynsymcount
<= 0)
9920 if (!bed
->plt_sym_val
)
9923 relplt_name
= bed
->relplt_name
;
9924 if (relplt_name
== NULL
)
9925 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
9926 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
9930 hdr
= &elf_section_data (relplt
)->this_hdr
;
9931 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
9932 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
9935 plt
= bfd_get_section_by_name (abfd
, ".plt");
9939 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9940 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
9943 count
= relplt
->size
/ hdr
->sh_entsize
;
9944 size
= count
* sizeof (asymbol
);
9945 p
= relplt
->relocation
;
9946 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9948 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
9952 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
9954 size
+= sizeof ("+0x") - 1 + 8;
9959 s
= *ret
= (asymbol
*) bfd_malloc (size
);
9963 names
= (char *) (s
+ count
);
9964 p
= relplt
->relocation
;
9966 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9971 addr
= bed
->plt_sym_val (i
, plt
, p
);
9972 if (addr
== (bfd_vma
) -1)
9975 *s
= **p
->sym_ptr_ptr
;
9976 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9977 we are defining a symbol, ensure one of them is set. */
9978 if ((s
->flags
& BSF_LOCAL
) == 0)
9979 s
->flags
|= BSF_GLOBAL
;
9980 s
->flags
|= BSF_SYNTHETIC
;
9982 s
->value
= addr
- plt
->vma
;
9985 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
9986 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
9992 memcpy (names
, "+0x", sizeof ("+0x") - 1);
9993 names
+= sizeof ("+0x") - 1;
9994 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
9995 for (a
= buf
; *a
== '0'; ++a
)
9998 memcpy (names
, a
, len
);
10001 memcpy (names
, "@plt", sizeof ("@plt"));
10002 names
+= sizeof ("@plt");
10009 /* It is only used by x86-64 so far. */
10010 asection _bfd_elf_large_com_section
10011 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10012 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10015 _bfd_elf_set_osabi (bfd
* abfd
,
10016 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10018 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10020 i_ehdrp
= elf_elfheader (abfd
);
10022 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10024 /* To make things simpler for the loader on Linux systems we set the
10025 osabi field to ELFOSABI_GNU if the binary contains symbols of
10026 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10027 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10028 && elf_tdata (abfd
)->has_gnu_symbols
)
10029 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10033 /* Return TRUE for ELF symbol types that represent functions.
10034 This is the default version of this function, which is sufficient for
10035 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10038 _bfd_elf_is_function_type (unsigned int type
)
10040 return (type
== STT_FUNC
10041 || type
== STT_GNU_IFUNC
);
10044 /* If the ELF symbol SYM might be a function in SEC, return the
10045 function size and set *CODE_OFF to the function's entry point,
10046 otherwise return zero. */
10049 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10052 bfd_size_type size
;
10054 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10055 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10056 || sym
->section
!= sec
)
10059 *code_off
= sym
->value
;
10061 if (!(sym
->flags
& BSF_SYNTHETIC
))
10062 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;