1 /* ELF executable support for BFD.
3 Copyright 1993-2014 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 if (!elf_flags_init (obfd
))
1122 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1123 elf_flags_init (obfd
) = TRUE
;
1126 elf_gp (obfd
) = elf_gp (ibfd
);
1128 /* Also copy the EI_OSABI field. */
1129 elf_elfheader (obfd
)->e_ident
[EI_OSABI
] =
1130 elf_elfheader (ibfd
)->e_ident
[EI_OSABI
];
1132 /* Copy object attributes. */
1133 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1138 get_segment_type (unsigned int p_type
)
1143 case PT_NULL
: pt
= "NULL"; break;
1144 case PT_LOAD
: pt
= "LOAD"; break;
1145 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1146 case PT_INTERP
: pt
= "INTERP"; break;
1147 case PT_NOTE
: pt
= "NOTE"; break;
1148 case PT_SHLIB
: pt
= "SHLIB"; break;
1149 case PT_PHDR
: pt
= "PHDR"; break;
1150 case PT_TLS
: pt
= "TLS"; break;
1151 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1152 case PT_GNU_STACK
: pt
= "STACK"; break;
1153 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1154 default: pt
= NULL
; break;
1159 /* Print out the program headers. */
1162 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1164 FILE *f
= (FILE *) farg
;
1165 Elf_Internal_Phdr
*p
;
1167 bfd_byte
*dynbuf
= NULL
;
1169 p
= elf_tdata (abfd
)->phdr
;
1174 fprintf (f
, _("\nProgram Header:\n"));
1175 c
= elf_elfheader (abfd
)->e_phnum
;
1176 for (i
= 0; i
< c
; i
++, p
++)
1178 const char *pt
= get_segment_type (p
->p_type
);
1183 sprintf (buf
, "0x%lx", p
->p_type
);
1186 fprintf (f
, "%8s off 0x", pt
);
1187 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1188 fprintf (f
, " vaddr 0x");
1189 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1190 fprintf (f
, " paddr 0x");
1191 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1192 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1193 fprintf (f
, " filesz 0x");
1194 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1195 fprintf (f
, " memsz 0x");
1196 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1197 fprintf (f
, " flags %c%c%c",
1198 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1199 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1200 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1201 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1202 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1207 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1210 unsigned int elfsec
;
1211 unsigned long shlink
;
1212 bfd_byte
*extdyn
, *extdynend
;
1214 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1216 fprintf (f
, _("\nDynamic Section:\n"));
1218 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1221 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1222 if (elfsec
== SHN_BAD
)
1224 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1226 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1227 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1230 extdynend
= extdyn
+ s
->size
;
1231 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1233 Elf_Internal_Dyn dyn
;
1234 const char *name
= "";
1236 bfd_boolean stringp
;
1237 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1239 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1241 if (dyn
.d_tag
== DT_NULL
)
1248 if (bed
->elf_backend_get_target_dtag
)
1249 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1251 if (!strcmp (name
, ""))
1253 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1258 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1259 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1260 case DT_PLTGOT
: name
= "PLTGOT"; break;
1261 case DT_HASH
: name
= "HASH"; break;
1262 case DT_STRTAB
: name
= "STRTAB"; break;
1263 case DT_SYMTAB
: name
= "SYMTAB"; break;
1264 case DT_RELA
: name
= "RELA"; break;
1265 case DT_RELASZ
: name
= "RELASZ"; break;
1266 case DT_RELAENT
: name
= "RELAENT"; break;
1267 case DT_STRSZ
: name
= "STRSZ"; break;
1268 case DT_SYMENT
: name
= "SYMENT"; break;
1269 case DT_INIT
: name
= "INIT"; break;
1270 case DT_FINI
: name
= "FINI"; break;
1271 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1272 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1273 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1274 case DT_REL
: name
= "REL"; break;
1275 case DT_RELSZ
: name
= "RELSZ"; break;
1276 case DT_RELENT
: name
= "RELENT"; break;
1277 case DT_PLTREL
: name
= "PLTREL"; break;
1278 case DT_DEBUG
: name
= "DEBUG"; break;
1279 case DT_TEXTREL
: name
= "TEXTREL"; break;
1280 case DT_JMPREL
: name
= "JMPREL"; break;
1281 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1282 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1283 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1284 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1285 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1286 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1287 case DT_FLAGS
: name
= "FLAGS"; break;
1288 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1289 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1290 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1291 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1292 case DT_MOVEENT
: name
= "MOVEENT"; break;
1293 case DT_MOVESZ
: name
= "MOVESZ"; break;
1294 case DT_FEATURE
: name
= "FEATURE"; break;
1295 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1296 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1297 case DT_SYMINENT
: name
= "SYMINENT"; break;
1298 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1299 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1300 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1301 case DT_PLTPAD
: name
= "PLTPAD"; break;
1302 case DT_MOVETAB
: name
= "MOVETAB"; break;
1303 case DT_SYMINFO
: name
= "SYMINFO"; break;
1304 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1305 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1306 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1307 case DT_VERSYM
: name
= "VERSYM"; break;
1308 case DT_VERDEF
: name
= "VERDEF"; break;
1309 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1310 case DT_VERNEED
: name
= "VERNEED"; break;
1311 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1312 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1313 case DT_USED
: name
= "USED"; break;
1314 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1315 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1318 fprintf (f
, " %-20s ", name
);
1322 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1327 unsigned int tagv
= dyn
.d_un
.d_val
;
1329 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1332 fprintf (f
, "%s", string
);
1341 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1342 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1344 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1348 if (elf_dynverdef (abfd
) != 0)
1350 Elf_Internal_Verdef
*t
;
1352 fprintf (f
, _("\nVersion definitions:\n"));
1353 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1355 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1356 t
->vd_flags
, t
->vd_hash
,
1357 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1358 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1360 Elf_Internal_Verdaux
*a
;
1363 for (a
= t
->vd_auxptr
->vda_nextptr
;
1367 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1373 if (elf_dynverref (abfd
) != 0)
1375 Elf_Internal_Verneed
*t
;
1377 fprintf (f
, _("\nVersion References:\n"));
1378 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1380 Elf_Internal_Vernaux
*a
;
1382 fprintf (f
, _(" required from %s:\n"),
1383 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1384 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1385 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1386 a
->vna_flags
, a
->vna_other
,
1387 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1399 /* Display ELF-specific fields of a symbol. */
1402 bfd_elf_print_symbol (bfd
*abfd
,
1405 bfd_print_symbol_type how
)
1407 FILE *file
= (FILE *) filep
;
1410 case bfd_print_symbol_name
:
1411 fprintf (file
, "%s", symbol
->name
);
1413 case bfd_print_symbol_more
:
1414 fprintf (file
, "elf ");
1415 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1416 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1418 case bfd_print_symbol_all
:
1420 const char *section_name
;
1421 const char *name
= NULL
;
1422 const struct elf_backend_data
*bed
;
1423 unsigned char st_other
;
1426 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1428 bed
= get_elf_backend_data (abfd
);
1429 if (bed
->elf_backend_print_symbol_all
)
1430 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1434 name
= symbol
->name
;
1435 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1438 fprintf (file
, " %s\t", section_name
);
1439 /* Print the "other" value for a symbol. For common symbols,
1440 we've already printed the size; now print the alignment.
1441 For other symbols, we have no specified alignment, and
1442 we've printed the address; now print the size. */
1443 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1444 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1446 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1447 bfd_fprintf_vma (abfd
, file
, val
);
1449 /* If we have version information, print it. */
1450 if (elf_dynversym (abfd
) != 0
1451 && (elf_dynverdef (abfd
) != 0
1452 || elf_dynverref (abfd
) != 0))
1454 unsigned int vernum
;
1455 const char *version_string
;
1457 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1460 version_string
= "";
1461 else if (vernum
== 1)
1462 version_string
= "Base";
1463 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1465 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1468 Elf_Internal_Verneed
*t
;
1470 version_string
= "";
1471 for (t
= elf_tdata (abfd
)->verref
;
1475 Elf_Internal_Vernaux
*a
;
1477 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1479 if (a
->vna_other
== vernum
)
1481 version_string
= a
->vna_nodename
;
1488 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1489 fprintf (file
, " %-11s", version_string
);
1494 fprintf (file
, " (%s)", version_string
);
1495 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1500 /* If the st_other field is not zero, print it. */
1501 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1506 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1507 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1508 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1510 /* Some other non-defined flags are also present, so print
1512 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1515 fprintf (file
, " %s", name
);
1521 /* Allocate an ELF string table--force the first byte to be zero. */
1523 struct bfd_strtab_hash
*
1524 _bfd_elf_stringtab_init (void)
1526 struct bfd_strtab_hash
*ret
;
1528 ret
= _bfd_stringtab_init ();
1533 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1534 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1535 if (loc
== (bfd_size_type
) -1)
1537 _bfd_stringtab_free (ret
);
1544 /* ELF .o/exec file reading */
1546 /* Create a new bfd section from an ELF section header. */
1549 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1551 Elf_Internal_Shdr
*hdr
;
1552 Elf_Internal_Ehdr
*ehdr
;
1553 const struct elf_backend_data
*bed
;
1556 if (shindex
>= elf_numsections (abfd
))
1559 hdr
= elf_elfsections (abfd
)[shindex
];
1560 ehdr
= elf_elfheader (abfd
);
1561 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1566 bed
= get_elf_backend_data (abfd
);
1567 switch (hdr
->sh_type
)
1570 /* Inactive section. Throw it away. */
1573 case SHT_PROGBITS
: /* Normal section with contents. */
1574 case SHT_NOBITS
: /* .bss section. */
1575 case SHT_HASH
: /* .hash section. */
1576 case SHT_NOTE
: /* .note section. */
1577 case SHT_INIT_ARRAY
: /* .init_array section. */
1578 case SHT_FINI_ARRAY
: /* .fini_array section. */
1579 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1580 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1581 case SHT_GNU_HASH
: /* .gnu.hash section. */
1582 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1584 case SHT_DYNAMIC
: /* Dynamic linking information. */
1585 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1587 if (hdr
->sh_link
> elf_numsections (abfd
))
1589 /* PR 10478: Accept Solaris binaries with a sh_link
1590 field set to SHN_BEFORE or SHN_AFTER. */
1591 switch (bfd_get_arch (abfd
))
1594 case bfd_arch_sparc
:
1595 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1596 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1598 /* Otherwise fall through. */
1603 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1605 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1607 Elf_Internal_Shdr
*dynsymhdr
;
1609 /* The shared libraries distributed with hpux11 have a bogus
1610 sh_link field for the ".dynamic" section. Find the
1611 string table for the ".dynsym" section instead. */
1612 if (elf_dynsymtab (abfd
) != 0)
1614 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1615 hdr
->sh_link
= dynsymhdr
->sh_link
;
1619 unsigned int i
, num_sec
;
1621 num_sec
= elf_numsections (abfd
);
1622 for (i
= 1; i
< num_sec
; i
++)
1624 dynsymhdr
= elf_elfsections (abfd
)[i
];
1625 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1627 hdr
->sh_link
= dynsymhdr
->sh_link
;
1635 case SHT_SYMTAB
: /* A symbol table */
1636 if (elf_onesymtab (abfd
) == shindex
)
1639 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1641 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1643 if (hdr
->sh_size
!= 0)
1645 /* Some assemblers erroneously set sh_info to one with a
1646 zero sh_size. ld sees this as a global symbol count
1647 of (unsigned) -1. Fix it here. */
1651 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1652 elf_onesymtab (abfd
) = shindex
;
1653 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1654 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1655 abfd
->flags
|= HAS_SYMS
;
1657 /* Sometimes a shared object will map in the symbol table. If
1658 SHF_ALLOC is set, and this is a shared object, then we also
1659 treat this section as a BFD section. We can not base the
1660 decision purely on SHF_ALLOC, because that flag is sometimes
1661 set in a relocatable object file, which would confuse the
1663 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1664 && (abfd
->flags
& DYNAMIC
) != 0
1665 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1669 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1670 can't read symbols without that section loaded as well. It
1671 is most likely specified by the next section header. */
1672 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1674 unsigned int i
, num_sec
;
1676 num_sec
= elf_numsections (abfd
);
1677 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1679 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1680 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1681 && hdr2
->sh_link
== shindex
)
1685 for (i
= 1; i
< shindex
; i
++)
1687 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1688 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1689 && hdr2
->sh_link
== shindex
)
1693 return bfd_section_from_shdr (abfd
, i
);
1697 case SHT_DYNSYM
: /* A dynamic symbol table */
1698 if (elf_dynsymtab (abfd
) == shindex
)
1701 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1703 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1705 if (hdr
->sh_size
!= 0)
1707 /* Some linkers erroneously set sh_info to one with a
1708 zero sh_size. ld sees this as a global symbol count
1709 of (unsigned) -1. Fix it here. */
1713 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1714 elf_dynsymtab (abfd
) = shindex
;
1715 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1716 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1717 abfd
->flags
|= HAS_SYMS
;
1719 /* Besides being a symbol table, we also treat this as a regular
1720 section, so that objcopy can handle it. */
1721 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1723 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1724 if (elf_symtab_shndx (abfd
) == shindex
)
1727 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1728 elf_symtab_shndx (abfd
) = shindex
;
1729 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1730 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1733 case SHT_STRTAB
: /* A string table */
1734 if (hdr
->bfd_section
!= NULL
)
1736 if (ehdr
->e_shstrndx
== shindex
)
1738 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1739 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1742 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1745 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1746 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1749 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1752 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1753 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1754 elf_elfsections (abfd
)[shindex
] = hdr
;
1755 /* We also treat this as a regular section, so that objcopy
1757 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1761 /* If the string table isn't one of the above, then treat it as a
1762 regular section. We need to scan all the headers to be sure,
1763 just in case this strtab section appeared before the above. */
1764 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1766 unsigned int i
, num_sec
;
1768 num_sec
= elf_numsections (abfd
);
1769 for (i
= 1; i
< num_sec
; i
++)
1771 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1772 if (hdr2
->sh_link
== shindex
)
1774 /* Prevent endless recursion on broken objects. */
1777 if (! bfd_section_from_shdr (abfd
, i
))
1779 if (elf_onesymtab (abfd
) == i
)
1781 if (elf_dynsymtab (abfd
) == i
)
1782 goto dynsymtab_strtab
;
1786 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1790 /* *These* do a lot of work -- but build no sections! */
1792 asection
*target_sect
;
1793 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1794 unsigned int num_sec
= elf_numsections (abfd
);
1795 struct bfd_elf_section_data
*esdt
;
1799 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1800 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1803 /* Check for a bogus link to avoid crashing. */
1804 if (hdr
->sh_link
>= num_sec
)
1806 ((*_bfd_error_handler
)
1807 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1808 abfd
, hdr
->sh_link
, name
, shindex
));
1809 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1813 /* For some incomprehensible reason Oracle distributes
1814 libraries for Solaris in which some of the objects have
1815 bogus sh_link fields. It would be nice if we could just
1816 reject them, but, unfortunately, some people need to use
1817 them. We scan through the section headers; if we find only
1818 one suitable symbol table, we clobber the sh_link to point
1819 to it. I hope this doesn't break anything.
1821 Don't do it on executable nor shared library. */
1822 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1823 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1824 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1830 for (scan
= 1; scan
< num_sec
; scan
++)
1832 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1833 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1844 hdr
->sh_link
= found
;
1847 /* Get the symbol table. */
1848 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1849 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1850 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1853 /* If this reloc section does not use the main symbol table we
1854 don't treat it as a reloc section. BFD can't adequately
1855 represent such a section, so at least for now, we don't
1856 try. We just present it as a normal section. We also
1857 can't use it as a reloc section if it points to the null
1858 section, an invalid section, another reloc section, or its
1859 sh_link points to the null section. */
1860 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1861 || hdr
->sh_link
== SHN_UNDEF
1862 || hdr
->sh_info
== SHN_UNDEF
1863 || hdr
->sh_info
>= num_sec
1864 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1865 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1866 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1869 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1871 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1872 if (target_sect
== NULL
)
1875 esdt
= elf_section_data (target_sect
);
1876 if (hdr
->sh_type
== SHT_RELA
)
1877 p_hdr
= &esdt
->rela
.hdr
;
1879 p_hdr
= &esdt
->rel
.hdr
;
1881 BFD_ASSERT (*p_hdr
== NULL
);
1882 amt
= sizeof (*hdr2
);
1883 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1888 elf_elfsections (abfd
)[shindex
] = hdr2
;
1889 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1890 target_sect
->flags
|= SEC_RELOC
;
1891 target_sect
->relocation
= NULL
;
1892 target_sect
->rel_filepos
= hdr
->sh_offset
;
1893 /* In the section to which the relocations apply, mark whether
1894 its relocations are of the REL or RELA variety. */
1895 if (hdr
->sh_size
!= 0)
1897 if (hdr
->sh_type
== SHT_RELA
)
1898 target_sect
->use_rela_p
= 1;
1900 abfd
->flags
|= HAS_RELOC
;
1904 case SHT_GNU_verdef
:
1905 elf_dynverdef (abfd
) = shindex
;
1906 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1907 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1909 case SHT_GNU_versym
:
1910 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1912 elf_dynversym (abfd
) = shindex
;
1913 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1914 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1916 case SHT_GNU_verneed
:
1917 elf_dynverref (abfd
) = shindex
;
1918 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1919 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1925 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
1927 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1929 if (hdr
->contents
!= NULL
)
1931 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1932 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1935 if (idx
->flags
& GRP_COMDAT
)
1936 hdr
->bfd_section
->flags
1937 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1939 /* We try to keep the same section order as it comes in. */
1941 while (--n_elt
!= 0)
1945 if (idx
->shdr
!= NULL
1946 && (s
= idx
->shdr
->bfd_section
) != NULL
1947 && elf_next_in_group (s
) != NULL
)
1949 elf_next_in_group (hdr
->bfd_section
) = s
;
1957 /* Possibly an attributes section. */
1958 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1959 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1961 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1963 _bfd_elf_parse_attributes (abfd
, hdr
);
1967 /* Check for any processor-specific section types. */
1968 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1971 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1973 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1974 /* FIXME: How to properly handle allocated section reserved
1975 for applications? */
1976 (*_bfd_error_handler
)
1977 (_("%B: don't know how to handle allocated, application "
1978 "specific section `%s' [0x%8x]"),
1979 abfd
, name
, hdr
->sh_type
);
1981 /* Allow sections reserved for applications. */
1982 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1985 else if (hdr
->sh_type
>= SHT_LOPROC
1986 && hdr
->sh_type
<= SHT_HIPROC
)
1987 /* FIXME: We should handle this section. */
1988 (*_bfd_error_handler
)
1989 (_("%B: don't know how to handle processor specific section "
1991 abfd
, name
, hdr
->sh_type
);
1992 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1994 /* Unrecognised OS-specific sections. */
1995 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1996 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1997 required to correctly process the section and the file should
1998 be rejected with an error message. */
1999 (*_bfd_error_handler
)
2000 (_("%B: don't know how to handle OS specific section "
2002 abfd
, name
, hdr
->sh_type
);
2004 /* Otherwise it should be processed. */
2005 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2008 /* FIXME: We should handle this section. */
2009 (*_bfd_error_handler
)
2010 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2011 abfd
, name
, hdr
->sh_type
);
2019 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2022 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2024 unsigned long r_symndx
)
2026 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2028 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2030 Elf_Internal_Shdr
*symtab_hdr
;
2031 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2032 Elf_External_Sym_Shndx eshndx
;
2034 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2035 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2036 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2039 if (cache
->abfd
!= abfd
)
2041 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2044 cache
->indx
[ent
] = r_symndx
;
2047 return &cache
->sym
[ent
];
2050 /* Given an ELF section number, retrieve the corresponding BFD
2054 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2056 if (sec_index
>= elf_numsections (abfd
))
2058 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2061 static const struct bfd_elf_special_section special_sections_b
[] =
2063 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2064 { NULL
, 0, 0, 0, 0 }
2067 static const struct bfd_elf_special_section special_sections_c
[] =
2069 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2070 { NULL
, 0, 0, 0, 0 }
2073 static const struct bfd_elf_special_section special_sections_d
[] =
2075 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2076 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2077 /* There are more DWARF sections than these, but they needn't be added here
2078 unless you have to cope with broken compilers that don't emit section
2079 attributes or you want to help the user writing assembler. */
2080 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2081 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2082 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2083 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2084 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2085 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2086 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2087 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2088 { NULL
, 0, 0, 0, 0 }
2091 static const struct bfd_elf_special_section special_sections_f
[] =
2093 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2094 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2095 { NULL
, 0, 0, 0, 0 }
2098 static const struct bfd_elf_special_section special_sections_g
[] =
2100 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2101 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2102 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2103 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2104 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2105 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2106 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2107 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2108 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2109 { NULL
, 0, 0, 0, 0 }
2112 static const struct bfd_elf_special_section special_sections_h
[] =
2114 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2115 { NULL
, 0, 0, 0, 0 }
2118 static const struct bfd_elf_special_section special_sections_i
[] =
2120 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2121 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2122 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2123 { NULL
, 0, 0, 0, 0 }
2126 static const struct bfd_elf_special_section special_sections_l
[] =
2128 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2129 { NULL
, 0, 0, 0, 0 }
2132 static const struct bfd_elf_special_section special_sections_n
[] =
2134 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2135 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2136 { NULL
, 0, 0, 0, 0 }
2139 static const struct bfd_elf_special_section special_sections_p
[] =
2141 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2142 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2143 { NULL
, 0, 0, 0, 0 }
2146 static const struct bfd_elf_special_section special_sections_r
[] =
2148 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2149 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2150 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2151 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2152 { NULL
, 0, 0, 0, 0 }
2155 static const struct bfd_elf_special_section special_sections_s
[] =
2157 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2158 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2159 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2160 /* See struct bfd_elf_special_section declaration for the semantics of
2161 this special case where .prefix_length != strlen (.prefix). */
2162 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2163 { NULL
, 0, 0, 0, 0 }
2166 static const struct bfd_elf_special_section special_sections_t
[] =
2168 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2169 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2170 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2171 { NULL
, 0, 0, 0, 0 }
2174 static const struct bfd_elf_special_section special_sections_z
[] =
2176 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2177 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2178 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2179 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2180 { NULL
, 0, 0, 0, 0 }
2183 static const struct bfd_elf_special_section
* const special_sections
[] =
2185 special_sections_b
, /* 'b' */
2186 special_sections_c
, /* 'c' */
2187 special_sections_d
, /* 'd' */
2189 special_sections_f
, /* 'f' */
2190 special_sections_g
, /* 'g' */
2191 special_sections_h
, /* 'h' */
2192 special_sections_i
, /* 'i' */
2195 special_sections_l
, /* 'l' */
2197 special_sections_n
, /* 'n' */
2199 special_sections_p
, /* 'p' */
2201 special_sections_r
, /* 'r' */
2202 special_sections_s
, /* 's' */
2203 special_sections_t
, /* 't' */
2209 special_sections_z
/* 'z' */
2212 const struct bfd_elf_special_section
*
2213 _bfd_elf_get_special_section (const char *name
,
2214 const struct bfd_elf_special_section
*spec
,
2220 len
= strlen (name
);
2222 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2225 int prefix_len
= spec
[i
].prefix_length
;
2227 if (len
< prefix_len
)
2229 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2232 suffix_len
= spec
[i
].suffix_length
;
2233 if (suffix_len
<= 0)
2235 if (name
[prefix_len
] != 0)
2237 if (suffix_len
== 0)
2239 if (name
[prefix_len
] != '.'
2240 && (suffix_len
== -2
2241 || (rela
&& spec
[i
].type
== SHT_REL
)))
2247 if (len
< prefix_len
+ suffix_len
)
2249 if (memcmp (name
+ len
- suffix_len
,
2250 spec
[i
].prefix
+ prefix_len
,
2260 const struct bfd_elf_special_section
*
2261 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2264 const struct bfd_elf_special_section
*spec
;
2265 const struct elf_backend_data
*bed
;
2267 /* See if this is one of the special sections. */
2268 if (sec
->name
== NULL
)
2271 bed
= get_elf_backend_data (abfd
);
2272 spec
= bed
->special_sections
;
2275 spec
= _bfd_elf_get_special_section (sec
->name
,
2276 bed
->special_sections
,
2282 if (sec
->name
[0] != '.')
2285 i
= sec
->name
[1] - 'b';
2286 if (i
< 0 || i
> 'z' - 'b')
2289 spec
= special_sections
[i
];
2294 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2298 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2300 struct bfd_elf_section_data
*sdata
;
2301 const struct elf_backend_data
*bed
;
2302 const struct bfd_elf_special_section
*ssect
;
2304 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2307 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2311 sec
->used_by_bfd
= sdata
;
2314 /* Indicate whether or not this section should use RELA relocations. */
2315 bed
= get_elf_backend_data (abfd
);
2316 sec
->use_rela_p
= bed
->default_use_rela_p
;
2318 /* When we read a file, we don't need to set ELF section type and
2319 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2320 anyway. We will set ELF section type and flags for all linker
2321 created sections. If user specifies BFD section flags, we will
2322 set ELF section type and flags based on BFD section flags in
2323 elf_fake_sections. Special handling for .init_array/.fini_array
2324 output sections since they may contain .ctors/.dtors input
2325 sections. We don't want _bfd_elf_init_private_section_data to
2326 copy ELF section type from .ctors/.dtors input sections. */
2327 if (abfd
->direction
!= read_direction
2328 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2330 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2333 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2334 || ssect
->type
== SHT_INIT_ARRAY
2335 || ssect
->type
== SHT_FINI_ARRAY
))
2337 elf_section_type (sec
) = ssect
->type
;
2338 elf_section_flags (sec
) = ssect
->attr
;
2342 return _bfd_generic_new_section_hook (abfd
, sec
);
2345 /* Create a new bfd section from an ELF program header.
2347 Since program segments have no names, we generate a synthetic name
2348 of the form segment<NUM>, where NUM is generally the index in the
2349 program header table. For segments that are split (see below) we
2350 generate the names segment<NUM>a and segment<NUM>b.
2352 Note that some program segments may have a file size that is different than
2353 (less than) the memory size. All this means is that at execution the
2354 system must allocate the amount of memory specified by the memory size,
2355 but only initialize it with the first "file size" bytes read from the
2356 file. This would occur for example, with program segments consisting
2357 of combined data+bss.
2359 To handle the above situation, this routine generates TWO bfd sections
2360 for the single program segment. The first has the length specified by
2361 the file size of the segment, and the second has the length specified
2362 by the difference between the two sizes. In effect, the segment is split
2363 into its initialized and uninitialized parts.
2368 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2369 Elf_Internal_Phdr
*hdr
,
2371 const char *type_name
)
2379 split
= ((hdr
->p_memsz
> 0)
2380 && (hdr
->p_filesz
> 0)
2381 && (hdr
->p_memsz
> hdr
->p_filesz
));
2383 if (hdr
->p_filesz
> 0)
2385 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2386 len
= strlen (namebuf
) + 1;
2387 name
= (char *) bfd_alloc (abfd
, len
);
2390 memcpy (name
, namebuf
, len
);
2391 newsect
= bfd_make_section (abfd
, name
);
2392 if (newsect
== NULL
)
2394 newsect
->vma
= hdr
->p_vaddr
;
2395 newsect
->lma
= hdr
->p_paddr
;
2396 newsect
->size
= hdr
->p_filesz
;
2397 newsect
->filepos
= hdr
->p_offset
;
2398 newsect
->flags
|= SEC_HAS_CONTENTS
;
2399 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2400 if (hdr
->p_type
== PT_LOAD
)
2402 newsect
->flags
|= SEC_ALLOC
;
2403 newsect
->flags
|= SEC_LOAD
;
2404 if (hdr
->p_flags
& PF_X
)
2406 /* FIXME: all we known is that it has execute PERMISSION,
2408 newsect
->flags
|= SEC_CODE
;
2411 if (!(hdr
->p_flags
& PF_W
))
2413 newsect
->flags
|= SEC_READONLY
;
2417 if (hdr
->p_memsz
> hdr
->p_filesz
)
2421 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2422 len
= strlen (namebuf
) + 1;
2423 name
= (char *) bfd_alloc (abfd
, len
);
2426 memcpy (name
, namebuf
, len
);
2427 newsect
= bfd_make_section (abfd
, name
);
2428 if (newsect
== NULL
)
2430 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2431 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2432 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2433 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2434 align
= newsect
->vma
& -newsect
->vma
;
2435 if (align
== 0 || align
> hdr
->p_align
)
2436 align
= hdr
->p_align
;
2437 newsect
->alignment_power
= bfd_log2 (align
);
2438 if (hdr
->p_type
== PT_LOAD
)
2440 /* Hack for gdb. Segments that have not been modified do
2441 not have their contents written to a core file, on the
2442 assumption that a debugger can find the contents in the
2443 executable. We flag this case by setting the fake
2444 section size to zero. Note that "real" bss sections will
2445 always have their contents dumped to the core file. */
2446 if (bfd_get_format (abfd
) == bfd_core
)
2448 newsect
->flags
|= SEC_ALLOC
;
2449 if (hdr
->p_flags
& PF_X
)
2450 newsect
->flags
|= SEC_CODE
;
2452 if (!(hdr
->p_flags
& PF_W
))
2453 newsect
->flags
|= SEC_READONLY
;
2460 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2462 const struct elf_backend_data
*bed
;
2464 switch (hdr
->p_type
)
2467 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2470 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2473 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2476 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2479 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2481 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2486 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2489 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2491 case PT_GNU_EH_FRAME
:
2492 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2496 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2499 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2502 /* Check for any processor-specific program segment types. */
2503 bed
= get_elf_backend_data (abfd
);
2504 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2508 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2512 _bfd_elf_single_rel_hdr (asection
*sec
)
2514 if (elf_section_data (sec
)->rel
.hdr
)
2516 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2517 return elf_section_data (sec
)->rel
.hdr
;
2520 return elf_section_data (sec
)->rela
.hdr
;
2523 /* Allocate and initialize a section-header for a new reloc section,
2524 containing relocations against ASECT. It is stored in RELDATA. If
2525 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2529 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2530 struct bfd_elf_section_reloc_data
*reldata
,
2532 bfd_boolean use_rela_p
)
2534 Elf_Internal_Shdr
*rel_hdr
;
2536 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2539 amt
= sizeof (Elf_Internal_Shdr
);
2540 BFD_ASSERT (reldata
->hdr
== NULL
);
2541 rel_hdr
= bfd_zalloc (abfd
, amt
);
2542 reldata
->hdr
= rel_hdr
;
2544 amt
= sizeof ".rela" + strlen (asect
->name
);
2545 name
= (char *) bfd_alloc (abfd
, amt
);
2548 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2550 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2552 if (rel_hdr
->sh_name
== (unsigned int) -1)
2554 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2555 rel_hdr
->sh_entsize
= (use_rela_p
2556 ? bed
->s
->sizeof_rela
2557 : bed
->s
->sizeof_rel
);
2558 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2559 rel_hdr
->sh_flags
= 0;
2560 rel_hdr
->sh_addr
= 0;
2561 rel_hdr
->sh_size
= 0;
2562 rel_hdr
->sh_offset
= 0;
2567 /* Return the default section type based on the passed in section flags. */
2570 bfd_elf_get_default_section_type (flagword flags
)
2572 if ((flags
& SEC_ALLOC
) != 0
2573 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2575 return SHT_PROGBITS
;
2578 struct fake_section_arg
2580 struct bfd_link_info
*link_info
;
2584 /* Set up an ELF internal section header for a section. */
2587 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2589 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2590 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2591 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2592 Elf_Internal_Shdr
*this_hdr
;
2593 unsigned int sh_type
;
2597 /* We already failed; just get out of the bfd_map_over_sections
2602 this_hdr
= &esd
->this_hdr
;
2604 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2605 asect
->name
, FALSE
);
2606 if (this_hdr
->sh_name
== (unsigned int) -1)
2612 /* Don't clear sh_flags. Assembler may set additional bits. */
2614 if ((asect
->flags
& SEC_ALLOC
) != 0
2615 || asect
->user_set_vma
)
2616 this_hdr
->sh_addr
= asect
->vma
;
2618 this_hdr
->sh_addr
= 0;
2620 this_hdr
->sh_offset
= 0;
2621 this_hdr
->sh_size
= asect
->size
;
2622 this_hdr
->sh_link
= 0;
2623 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2624 /* The sh_entsize and sh_info fields may have been set already by
2625 copy_private_section_data. */
2627 this_hdr
->bfd_section
= asect
;
2628 this_hdr
->contents
= NULL
;
2630 /* If the section type is unspecified, we set it based on
2632 if ((asect
->flags
& SEC_GROUP
) != 0)
2633 sh_type
= SHT_GROUP
;
2635 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2637 if (this_hdr
->sh_type
== SHT_NULL
)
2638 this_hdr
->sh_type
= sh_type
;
2639 else if (this_hdr
->sh_type
== SHT_NOBITS
2640 && sh_type
== SHT_PROGBITS
2641 && (asect
->flags
& SEC_ALLOC
) != 0)
2643 /* Warn if we are changing a NOBITS section to PROGBITS, but
2644 allow the link to proceed. This can happen when users link
2645 non-bss input sections to bss output sections, or emit data
2646 to a bss output section via a linker script. */
2647 (*_bfd_error_handler
)
2648 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2649 this_hdr
->sh_type
= sh_type
;
2652 switch (this_hdr
->sh_type
)
2658 case SHT_INIT_ARRAY
:
2659 case SHT_FINI_ARRAY
:
2660 case SHT_PREINIT_ARRAY
:
2667 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2671 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2675 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2679 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2680 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2684 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2685 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2688 case SHT_GNU_versym
:
2689 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2692 case SHT_GNU_verdef
:
2693 this_hdr
->sh_entsize
= 0;
2694 /* objcopy or strip will copy over sh_info, but may not set
2695 cverdefs. The linker will set cverdefs, but sh_info will be
2697 if (this_hdr
->sh_info
== 0)
2698 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2700 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2701 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2704 case SHT_GNU_verneed
:
2705 this_hdr
->sh_entsize
= 0;
2706 /* objcopy or strip will copy over sh_info, but may not set
2707 cverrefs. The linker will set cverrefs, but sh_info will be
2709 if (this_hdr
->sh_info
== 0)
2710 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2712 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2713 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2717 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2721 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2725 if ((asect
->flags
& SEC_ALLOC
) != 0)
2726 this_hdr
->sh_flags
|= SHF_ALLOC
;
2727 if ((asect
->flags
& SEC_READONLY
) == 0)
2728 this_hdr
->sh_flags
|= SHF_WRITE
;
2729 if ((asect
->flags
& SEC_CODE
) != 0)
2730 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2731 if ((asect
->flags
& SEC_MERGE
) != 0)
2733 this_hdr
->sh_flags
|= SHF_MERGE
;
2734 this_hdr
->sh_entsize
= asect
->entsize
;
2735 if ((asect
->flags
& SEC_STRINGS
) != 0)
2736 this_hdr
->sh_flags
|= SHF_STRINGS
;
2738 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2739 this_hdr
->sh_flags
|= SHF_GROUP
;
2740 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2742 this_hdr
->sh_flags
|= SHF_TLS
;
2743 if (asect
->size
== 0
2744 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2746 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2748 this_hdr
->sh_size
= 0;
2751 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2752 if (this_hdr
->sh_size
!= 0)
2753 this_hdr
->sh_type
= SHT_NOBITS
;
2757 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2758 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2760 /* If the section has relocs, set up a section header for the
2761 SHT_REL[A] section. If two relocation sections are required for
2762 this section, it is up to the processor-specific back-end to
2763 create the other. */
2764 if ((asect
->flags
& SEC_RELOC
) != 0)
2766 /* When doing a relocatable link, create both REL and RELA sections if
2769 /* Do the normal setup if we wouldn't create any sections here. */
2770 && esd
->rel
.count
+ esd
->rela
.count
> 0
2771 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2773 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2774 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2779 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2780 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2786 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2788 ? &esd
->rela
: &esd
->rel
),
2794 /* Check for processor-specific section types. */
2795 sh_type
= this_hdr
->sh_type
;
2796 if (bed
->elf_backend_fake_sections
2797 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2800 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2802 /* Don't change the header type from NOBITS if we are being
2803 called for objcopy --only-keep-debug. */
2804 this_hdr
->sh_type
= sh_type
;
2808 /* Fill in the contents of a SHT_GROUP section. Called from
2809 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2810 when ELF targets use the generic linker, ld. Called for ld -r
2811 from bfd_elf_final_link. */
2814 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2816 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2817 asection
*elt
, *first
;
2821 /* Ignore linker created group section. See elfNN_ia64_object_p in
2823 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2827 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2829 unsigned long symindx
= 0;
2831 /* elf_group_id will have been set up by objcopy and the
2833 if (elf_group_id (sec
) != NULL
)
2834 symindx
= elf_group_id (sec
)->udata
.i
;
2838 /* If called from the assembler, swap_out_syms will have set up
2839 elf_section_syms. */
2840 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2841 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2843 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2845 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2847 /* The ELF backend linker sets sh_info to -2 when the group
2848 signature symbol is global, and thus the index can't be
2849 set until all local symbols are output. */
2850 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2851 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2852 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2853 unsigned long extsymoff
= 0;
2854 struct elf_link_hash_entry
*h
;
2856 if (!elf_bad_symtab (igroup
->owner
))
2858 Elf_Internal_Shdr
*symtab_hdr
;
2860 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2861 extsymoff
= symtab_hdr
->sh_info
;
2863 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2864 while (h
->root
.type
== bfd_link_hash_indirect
2865 || h
->root
.type
== bfd_link_hash_warning
)
2866 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2868 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2871 /* The contents won't be allocated for "ld -r" or objcopy. */
2873 if (sec
->contents
== NULL
)
2876 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2878 /* Arrange for the section to be written out. */
2879 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2880 if (sec
->contents
== NULL
)
2887 loc
= sec
->contents
+ sec
->size
;
2889 /* Get the pointer to the first section in the group that gas
2890 squirreled away here. objcopy arranges for this to be set to the
2891 start of the input section group. */
2892 first
= elt
= elf_next_in_group (sec
);
2894 /* First element is a flag word. Rest of section is elf section
2895 indices for all the sections of the group. Write them backwards
2896 just to keep the group in the same order as given in .section
2897 directives, not that it matters. */
2904 s
= s
->output_section
;
2906 && !bfd_is_abs_section (s
))
2908 unsigned int idx
= elf_section_data (s
)->this_idx
;
2911 H_PUT_32 (abfd
, idx
, loc
);
2913 elt
= elf_next_in_group (elt
);
2918 if ((loc
-= 4) != sec
->contents
)
2921 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2924 /* Assign all ELF section numbers. The dummy first section is handled here
2925 too. The link/info pointers for the standard section types are filled
2926 in here too, while we're at it. */
2929 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2931 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2933 unsigned int section_number
, secn
;
2934 Elf_Internal_Shdr
**i_shdrp
;
2935 struct bfd_elf_section_data
*d
;
2936 bfd_boolean need_symtab
;
2940 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2942 /* SHT_GROUP sections are in relocatable files only. */
2943 if (link_info
== NULL
|| link_info
->relocatable
)
2945 /* Put SHT_GROUP sections first. */
2946 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2948 d
= elf_section_data (sec
);
2950 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2952 if (sec
->flags
& SEC_LINKER_CREATED
)
2954 /* Remove the linker created SHT_GROUP sections. */
2955 bfd_section_list_remove (abfd
, sec
);
2956 abfd
->section_count
--;
2959 d
->this_idx
= section_number
++;
2964 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2966 d
= elf_section_data (sec
);
2968 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2969 d
->this_idx
= section_number
++;
2970 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2973 d
->rel
.idx
= section_number
++;
2974 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
2981 d
->rela
.idx
= section_number
++;
2982 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
2988 elf_shstrtab_sec (abfd
) = section_number
++;
2989 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2990 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
2992 need_symtab
= (bfd_get_symcount (abfd
) > 0
2993 || (link_info
== NULL
2994 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
2998 elf_onesymtab (abfd
) = section_number
++;
2999 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
3000 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3002 elf_symtab_shndx (abfd
) = section_number
++;
3003 t
->symtab_shndx_hdr
.sh_name
3004 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3005 ".symtab_shndx", FALSE
);
3006 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3009 elf_strtab_sec (abfd
) = section_number
++;
3010 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3013 if (section_number
>= SHN_LORESERVE
)
3015 _bfd_error_handler (_("%B: too many sections: %u"),
3016 abfd
, section_number
);
3020 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
3021 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3023 elf_numsections (abfd
) = section_number
;
3024 elf_elfheader (abfd
)->e_shnum
= section_number
;
3026 /* Set up the list of section header pointers, in agreement with the
3028 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3029 sizeof (Elf_Internal_Shdr
*));
3030 if (i_shdrp
== NULL
)
3033 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3034 sizeof (Elf_Internal_Shdr
));
3035 if (i_shdrp
[0] == NULL
)
3037 bfd_release (abfd
, i_shdrp
);
3041 elf_elfsections (abfd
) = i_shdrp
;
3043 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3046 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3047 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3049 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3050 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3052 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3053 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3056 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3061 d
= elf_section_data (sec
);
3063 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3064 if (d
->rel
.idx
!= 0)
3065 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3066 if (d
->rela
.idx
!= 0)
3067 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3069 /* Fill in the sh_link and sh_info fields while we're at it. */
3071 /* sh_link of a reloc section is the section index of the symbol
3072 table. sh_info is the section index of the section to which
3073 the relocation entries apply. */
3074 if (d
->rel
.idx
!= 0)
3076 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3077 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3078 d
->rel
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3080 if (d
->rela
.idx
!= 0)
3082 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3083 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3084 d
->rela
.hdr
->sh_flags
|= SHF_INFO_LINK
;
3087 /* We need to set up sh_link for SHF_LINK_ORDER. */
3088 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3090 s
= elf_linked_to_section (sec
);
3093 /* elf_linked_to_section points to the input section. */
3094 if (link_info
!= NULL
)
3096 /* Check discarded linkonce section. */
3097 if (discarded_section (s
))
3100 (*_bfd_error_handler
)
3101 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3102 abfd
, d
->this_hdr
.bfd_section
,
3104 /* Point to the kept section if it has the same
3105 size as the discarded one. */
3106 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3109 bfd_set_error (bfd_error_bad_value
);
3115 s
= s
->output_section
;
3116 BFD_ASSERT (s
!= NULL
);
3120 /* Handle objcopy. */
3121 if (s
->output_section
== NULL
)
3123 (*_bfd_error_handler
)
3124 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3125 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3126 bfd_set_error (bfd_error_bad_value
);
3129 s
= s
->output_section
;
3131 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3136 The Intel C compiler generates SHT_IA_64_UNWIND with
3137 SHF_LINK_ORDER. But it doesn't set the sh_link or
3138 sh_info fields. Hence we could get the situation
3140 const struct elf_backend_data
*bed
3141 = get_elf_backend_data (abfd
);
3142 if (bed
->link_order_error_handler
)
3143 bed
->link_order_error_handler
3144 (_("%B: warning: sh_link not set for section `%A'"),
3149 switch (d
->this_hdr
.sh_type
)
3153 /* A reloc section which we are treating as a normal BFD
3154 section. sh_link is the section index of the symbol
3155 table. sh_info is the section index of the section to
3156 which the relocation entries apply. We assume that an
3157 allocated reloc section uses the dynamic symbol table.
3158 FIXME: How can we be sure? */
3159 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3161 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3163 /* We look up the section the relocs apply to by name. */
3165 if (d
->this_hdr
.sh_type
== SHT_REL
)
3169 s
= bfd_get_section_by_name (abfd
, name
);
3172 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3173 d
->this_hdr
.sh_flags
|= SHF_INFO_LINK
;
3178 /* We assume that a section named .stab*str is a stabs
3179 string section. We look for a section with the same name
3180 but without the trailing ``str'', and set its sh_link
3181 field to point to this section. */
3182 if (CONST_STRNEQ (sec
->name
, ".stab")
3183 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3188 len
= strlen (sec
->name
);
3189 alc
= (char *) bfd_malloc (len
- 2);
3192 memcpy (alc
, sec
->name
, len
- 3);
3193 alc
[len
- 3] = '\0';
3194 s
= bfd_get_section_by_name (abfd
, alc
);
3198 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3200 /* This is a .stab section. */
3201 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3202 elf_section_data (s
)->this_hdr
.sh_entsize
3203 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3210 case SHT_GNU_verneed
:
3211 case SHT_GNU_verdef
:
3212 /* sh_link is the section header index of the string table
3213 used for the dynamic entries, or the symbol table, or the
3215 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3217 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3220 case SHT_GNU_LIBLIST
:
3221 /* sh_link is the section header index of the prelink library
3222 list used for the dynamic entries, or the symbol table, or
3223 the version strings. */
3224 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3225 ? ".dynstr" : ".gnu.libstr");
3227 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3232 case SHT_GNU_versym
:
3233 /* sh_link is the section header index of the symbol table
3234 this hash table or version table is for. */
3235 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3237 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3241 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3245 for (secn
= 1; secn
< section_number
; ++secn
)
3246 if (i_shdrp
[secn
] == NULL
)
3247 i_shdrp
[secn
] = i_shdrp
[0];
3249 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3250 i_shdrp
[secn
]->sh_name
);
3255 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3257 /* If the backend has a special mapping, use it. */
3258 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3259 if (bed
->elf_backend_sym_is_global
)
3260 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3262 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3263 || bfd_is_und_section (bfd_get_section (sym
))
3264 || bfd_is_com_section (bfd_get_section (sym
)));
3267 /* Don't output section symbols for sections that are not going to be
3268 output, that are duplicates or there is no BFD section. */
3271 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3273 elf_symbol_type
*type_ptr
;
3275 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3278 type_ptr
= elf_symbol_from (abfd
, sym
);
3279 return ((type_ptr
!= NULL
3280 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3281 && bfd_is_abs_section (sym
->section
))
3282 || !(sym
->section
->owner
== abfd
3283 || (sym
->section
->output_section
->owner
== abfd
3284 && sym
->section
->output_offset
== 0)
3285 || bfd_is_abs_section (sym
->section
)));
3288 /* Map symbol from it's internal number to the external number, moving
3289 all local symbols to be at the head of the list. */
3292 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3294 unsigned int symcount
= bfd_get_symcount (abfd
);
3295 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3296 asymbol
**sect_syms
;
3297 unsigned int num_locals
= 0;
3298 unsigned int num_globals
= 0;
3299 unsigned int num_locals2
= 0;
3300 unsigned int num_globals2
= 0;
3307 fprintf (stderr
, "elf_map_symbols\n");
3311 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3313 if (max_index
< asect
->index
)
3314 max_index
= asect
->index
;
3318 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3319 if (sect_syms
== NULL
)
3321 elf_section_syms (abfd
) = sect_syms
;
3322 elf_num_section_syms (abfd
) = max_index
;
3324 /* Init sect_syms entries for any section symbols we have already
3325 decided to output. */
3326 for (idx
= 0; idx
< symcount
; idx
++)
3328 asymbol
*sym
= syms
[idx
];
3330 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3332 && !ignore_section_sym (abfd
, sym
)
3333 && !bfd_is_abs_section (sym
->section
))
3335 asection
*sec
= sym
->section
;
3337 if (sec
->owner
!= abfd
)
3338 sec
= sec
->output_section
;
3340 sect_syms
[sec
->index
] = syms
[idx
];
3344 /* Classify all of the symbols. */
3345 for (idx
= 0; idx
< symcount
; idx
++)
3347 if (sym_is_global (abfd
, syms
[idx
]))
3349 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3353 /* We will be adding a section symbol for each normal BFD section. Most
3354 sections will already have a section symbol in outsymbols, but
3355 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3356 at least in that case. */
3357 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3359 if (sect_syms
[asect
->index
] == NULL
)
3361 if (!sym_is_global (abfd
, asect
->symbol
))
3368 /* Now sort the symbols so the local symbols are first. */
3369 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3370 sizeof (asymbol
*));
3372 if (new_syms
== NULL
)
3375 for (idx
= 0; idx
< symcount
; idx
++)
3377 asymbol
*sym
= syms
[idx
];
3380 if (sym_is_global (abfd
, sym
))
3381 i
= num_locals
+ num_globals2
++;
3382 else if (!ignore_section_sym (abfd
, sym
))
3387 sym
->udata
.i
= i
+ 1;
3389 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3391 if (sect_syms
[asect
->index
] == NULL
)
3393 asymbol
*sym
= asect
->symbol
;
3396 sect_syms
[asect
->index
] = sym
;
3397 if (!sym_is_global (abfd
, sym
))
3400 i
= num_locals
+ num_globals2
++;
3402 sym
->udata
.i
= i
+ 1;
3406 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3408 *pnum_locals
= num_locals
;
3412 /* Align to the maximum file alignment that could be required for any
3413 ELF data structure. */
3415 static inline file_ptr
3416 align_file_position (file_ptr off
, int align
)
3418 return (off
+ align
- 1) & ~(align
- 1);
3421 /* Assign a file position to a section, optionally aligning to the
3422 required section alignment. */
3425 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3429 if (align
&& i_shdrp
->sh_addralign
> 1)
3430 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3431 i_shdrp
->sh_offset
= offset
;
3432 if (i_shdrp
->bfd_section
!= NULL
)
3433 i_shdrp
->bfd_section
->filepos
= offset
;
3434 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3435 offset
+= i_shdrp
->sh_size
;
3439 /* Compute the file positions we are going to put the sections at, and
3440 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3441 is not NULL, this is being called by the ELF backend linker. */
3444 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3445 struct bfd_link_info
*link_info
)
3447 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3448 struct fake_section_arg fsargs
;
3450 struct bfd_strtab_hash
*strtab
= NULL
;
3451 Elf_Internal_Shdr
*shstrtab_hdr
;
3452 bfd_boolean need_symtab
;
3454 if (abfd
->output_has_begun
)
3457 /* Do any elf backend specific processing first. */
3458 if (bed
->elf_backend_begin_write_processing
)
3459 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3461 if (! prep_headers (abfd
))
3464 /* Post process the headers if necessary. */
3465 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3467 fsargs
.failed
= FALSE
;
3468 fsargs
.link_info
= link_info
;
3469 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3473 if (!assign_section_numbers (abfd
, link_info
))
3476 /* The backend linker builds symbol table information itself. */
3477 need_symtab
= (link_info
== NULL
3478 && (bfd_get_symcount (abfd
) > 0
3479 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3483 /* Non-zero if doing a relocatable link. */
3484 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3486 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3491 if (link_info
== NULL
)
3493 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3498 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3499 /* sh_name was set in prep_headers. */
3500 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3501 shstrtab_hdr
->sh_flags
= 0;
3502 shstrtab_hdr
->sh_addr
= 0;
3503 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3504 shstrtab_hdr
->sh_entsize
= 0;
3505 shstrtab_hdr
->sh_link
= 0;
3506 shstrtab_hdr
->sh_info
= 0;
3507 /* sh_offset is set in assign_file_positions_except_relocs. */
3508 shstrtab_hdr
->sh_addralign
= 1;
3510 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3516 Elf_Internal_Shdr
*hdr
;
3518 off
= elf_next_file_pos (abfd
);
3520 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3521 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3523 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3524 if (hdr
->sh_size
!= 0)
3525 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3527 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3528 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3530 elf_next_file_pos (abfd
) = off
;
3532 /* Now that we know where the .strtab section goes, write it
3534 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3535 || ! _bfd_stringtab_emit (abfd
, strtab
))
3537 _bfd_stringtab_free (strtab
);
3540 abfd
->output_has_begun
= TRUE
;
3545 /* Make an initial estimate of the size of the program header. If we
3546 get the number wrong here, we'll redo section placement. */
3548 static bfd_size_type
3549 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3553 const struct elf_backend_data
*bed
;
3555 /* Assume we will need exactly two PT_LOAD segments: one for text
3556 and one for data. */
3559 s
= bfd_get_section_by_name (abfd
, ".interp");
3560 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3562 /* If we have a loadable interpreter section, we need a
3563 PT_INTERP segment. In this case, assume we also need a
3564 PT_PHDR segment, although that may not be true for all
3569 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3571 /* We need a PT_DYNAMIC segment. */
3575 if (info
!= NULL
&& info
->relro
)
3577 /* We need a PT_GNU_RELRO segment. */
3581 if (elf_eh_frame_hdr (abfd
))
3583 /* We need a PT_GNU_EH_FRAME segment. */
3587 if (elf_stack_flags (abfd
))
3589 /* We need a PT_GNU_STACK segment. */
3593 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3595 if ((s
->flags
& SEC_LOAD
) != 0
3596 && CONST_STRNEQ (s
->name
, ".note"))
3598 /* We need a PT_NOTE segment. */
3600 /* Try to create just one PT_NOTE segment
3601 for all adjacent loadable .note* sections.
3602 gABI requires that within a PT_NOTE segment
3603 (and also inside of each SHT_NOTE section)
3604 each note is padded to a multiple of 4 size,
3605 so we check whether the sections are correctly
3607 if (s
->alignment_power
== 2)
3608 while (s
->next
!= NULL
3609 && s
->next
->alignment_power
== 2
3610 && (s
->next
->flags
& SEC_LOAD
) != 0
3611 && CONST_STRNEQ (s
->next
->name
, ".note"))
3616 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3618 if (s
->flags
& SEC_THREAD_LOCAL
)
3620 /* We need a PT_TLS segment. */
3626 /* Let the backend count up any program headers it might need. */
3627 bed
= get_elf_backend_data (abfd
);
3628 if (bed
->elf_backend_additional_program_headers
)
3632 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3638 return segs
* bed
->s
->sizeof_phdr
;
3641 /* Find the segment that contains the output_section of section. */
3644 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3646 struct elf_segment_map
*m
;
3647 Elf_Internal_Phdr
*p
;
3649 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3655 for (i
= m
->count
- 1; i
>= 0; i
--)
3656 if (m
->sections
[i
] == section
)
3663 /* Create a mapping from a set of sections to a program segment. */
3665 static struct elf_segment_map
*
3666 make_mapping (bfd
*abfd
,
3667 asection
**sections
,
3672 struct elf_segment_map
*m
;
3677 amt
= sizeof (struct elf_segment_map
);
3678 amt
+= (to
- from
- 1) * sizeof (asection
*);
3679 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3683 m
->p_type
= PT_LOAD
;
3684 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3685 m
->sections
[i
- from
] = *hdrpp
;
3686 m
->count
= to
- from
;
3688 if (from
== 0 && phdr
)
3690 /* Include the headers in the first PT_LOAD segment. */
3691 m
->includes_filehdr
= 1;
3692 m
->includes_phdrs
= 1;
3698 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3701 struct elf_segment_map
*
3702 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3704 struct elf_segment_map
*m
;
3706 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3707 sizeof (struct elf_segment_map
));
3711 m
->p_type
= PT_DYNAMIC
;
3713 m
->sections
[0] = dynsec
;
3718 /* Possibly add or remove segments from the segment map. */
3721 elf_modify_segment_map (bfd
*abfd
,
3722 struct bfd_link_info
*info
,
3723 bfd_boolean remove_empty_load
)
3725 struct elf_segment_map
**m
;
3726 const struct elf_backend_data
*bed
;
3728 /* The placement algorithm assumes that non allocated sections are
3729 not in PT_LOAD segments. We ensure this here by removing such
3730 sections from the segment map. We also remove excluded
3731 sections. Finally, any PT_LOAD segment without sections is
3733 m
= &elf_seg_map (abfd
);
3736 unsigned int i
, new_count
;
3738 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3740 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3741 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3742 || (*m
)->p_type
!= PT_LOAD
))
3744 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3748 (*m
)->count
= new_count
;
3750 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3756 bed
= get_elf_backend_data (abfd
);
3757 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3759 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3766 /* Set up a mapping from BFD sections to program segments. */
3769 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3772 struct elf_segment_map
*m
;
3773 asection
**sections
= NULL
;
3774 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3775 bfd_boolean no_user_phdrs
;
3777 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
3780 info
->user_phdrs
= !no_user_phdrs
;
3782 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3786 struct elf_segment_map
*mfirst
;
3787 struct elf_segment_map
**pm
;
3790 unsigned int phdr_index
;
3791 bfd_vma maxpagesize
;
3793 bfd_boolean phdr_in_segment
= TRUE
;
3794 bfd_boolean writable
;
3796 asection
*first_tls
= NULL
;
3797 asection
*dynsec
, *eh_frame_hdr
;
3799 bfd_vma addr_mask
, wrap_to
= 0;
3801 /* Select the allocated sections, and sort them. */
3803 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3804 sizeof (asection
*));
3805 if (sections
== NULL
)
3808 /* Calculate top address, avoiding undefined behaviour of shift
3809 left operator when shift count is equal to size of type
3811 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3812 addr_mask
= (addr_mask
<< 1) + 1;
3815 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3817 if ((s
->flags
& SEC_ALLOC
) != 0)
3821 /* A wrapping section potentially clashes with header. */
3822 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3823 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3826 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3829 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3831 /* Build the mapping. */
3836 /* If we have a .interp section, then create a PT_PHDR segment for
3837 the program headers and a PT_INTERP segment for the .interp
3839 s
= bfd_get_section_by_name (abfd
, ".interp");
3840 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3842 amt
= sizeof (struct elf_segment_map
);
3843 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3847 m
->p_type
= PT_PHDR
;
3848 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3849 m
->p_flags
= PF_R
| PF_X
;
3850 m
->p_flags_valid
= 1;
3851 m
->includes_phdrs
= 1;
3856 amt
= sizeof (struct elf_segment_map
);
3857 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3861 m
->p_type
= PT_INTERP
;
3869 /* Look through the sections. We put sections in the same program
3870 segment when the start of the second section can be placed within
3871 a few bytes of the end of the first section. */
3875 maxpagesize
= bed
->maxpagesize
;
3877 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3879 && (dynsec
->flags
& SEC_LOAD
) == 0)
3882 /* Deal with -Ttext or something similar such that the first section
3883 is not adjacent to the program headers. This is an
3884 approximation, since at this point we don't know exactly how many
3885 program headers we will need. */
3888 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
3890 if (phdr_size
== (bfd_size_type
) -1)
3891 phdr_size
= get_program_header_size (abfd
, info
);
3892 phdr_size
+= bed
->s
->sizeof_ehdr
;
3893 if ((abfd
->flags
& D_PAGED
) == 0
3894 || (sections
[0]->lma
& addr_mask
) < phdr_size
3895 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
3896 < phdr_size
% maxpagesize
)
3897 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
3898 phdr_in_segment
= FALSE
;
3901 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3904 bfd_boolean new_segment
;
3908 /* See if this section and the last one will fit in the same
3911 if (last_hdr
== NULL
)
3913 /* If we don't have a segment yet, then we don't need a new
3914 one (we build the last one after this loop). */
3915 new_segment
= FALSE
;
3917 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3919 /* If this section has a different relation between the
3920 virtual address and the load address, then we need a new
3924 else if (hdr
->lma
< last_hdr
->lma
+ last_size
3925 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
3927 /* If this section has a load address that makes it overlap
3928 the previous section, then we need a new segment. */
3931 /* In the next test we have to be careful when last_hdr->lma is close
3932 to the end of the address space. If the aligned address wraps
3933 around to the start of the address space, then there are no more
3934 pages left in memory and it is OK to assume that the current
3935 section can be included in the current segment. */
3936 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3938 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3941 /* If putting this section in this segment would force us to
3942 skip a page in the segment, then we need a new segment. */
3945 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3946 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3948 /* We don't want to put a loadable section after a
3949 nonloadable section in the same segment.
3950 Consider .tbss sections as loadable for this purpose. */
3953 else if ((abfd
->flags
& D_PAGED
) == 0)
3955 /* If the file is not demand paged, which means that we
3956 don't require the sections to be correctly aligned in the
3957 file, then there is no other reason for a new segment. */
3958 new_segment
= FALSE
;
3961 && (hdr
->flags
& SEC_READONLY
) == 0
3962 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
3963 != (hdr
->lma
& -maxpagesize
)))
3965 /* We don't want to put a writable section in a read only
3966 segment, unless they are on the same page in memory
3967 anyhow. We already know that the last section does not
3968 bring us past the current section on the page, so the
3969 only case in which the new section is not on the same
3970 page as the previous section is when the previous section
3971 ends precisely on a page boundary. */
3976 /* Otherwise, we can use the same segment. */
3977 new_segment
= FALSE
;
3980 /* Allow interested parties a chance to override our decision. */
3981 if (last_hdr
!= NULL
3983 && info
->callbacks
->override_segment_assignment
!= NULL
)
3985 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
3991 if ((hdr
->flags
& SEC_READONLY
) == 0)
3994 /* .tbss sections effectively have zero size. */
3995 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3996 != SEC_THREAD_LOCAL
)
3997 last_size
= hdr
->size
;
4003 /* We need a new program segment. We must create a new program
4004 header holding all the sections from phdr_index until hdr. */
4006 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4013 if ((hdr
->flags
& SEC_READONLY
) == 0)
4019 /* .tbss sections effectively have zero size. */
4020 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4021 last_size
= hdr
->size
;
4025 phdr_in_segment
= FALSE
;
4028 /* Create a final PT_LOAD program segment, but not if it's just
4030 if (last_hdr
!= NULL
4031 && (i
- phdr_index
!= 1
4032 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4033 != SEC_THREAD_LOCAL
)))
4035 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4043 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4046 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4053 /* For each batch of consecutive loadable .note sections,
4054 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4055 because if we link together nonloadable .note sections and
4056 loadable .note sections, we will generate two .note sections
4057 in the output file. FIXME: Using names for section types is
4059 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4061 if ((s
->flags
& SEC_LOAD
) != 0
4062 && CONST_STRNEQ (s
->name
, ".note"))
4067 amt
= sizeof (struct elf_segment_map
);
4068 if (s
->alignment_power
== 2)
4069 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4071 if (s2
->next
->alignment_power
== 2
4072 && (s2
->next
->flags
& SEC_LOAD
) != 0
4073 && CONST_STRNEQ (s2
->next
->name
, ".note")
4074 && align_power (s2
->lma
+ s2
->size
, 2)
4080 amt
+= (count
- 1) * sizeof (asection
*);
4081 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4085 m
->p_type
= PT_NOTE
;
4089 m
->sections
[m
->count
- count
--] = s
;
4090 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4093 m
->sections
[m
->count
- 1] = s
;
4094 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4098 if (s
->flags
& SEC_THREAD_LOCAL
)
4106 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4109 amt
= sizeof (struct elf_segment_map
);
4110 amt
+= (tls_count
- 1) * sizeof (asection
*);
4111 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4116 m
->count
= tls_count
;
4117 /* Mandated PF_R. */
4119 m
->p_flags_valid
= 1;
4120 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4122 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
4123 m
->sections
[i
] = first_tls
;
4124 first_tls
= first_tls
->next
;
4131 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4133 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4134 if (eh_frame_hdr
!= NULL
4135 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4137 amt
= sizeof (struct elf_segment_map
);
4138 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4142 m
->p_type
= PT_GNU_EH_FRAME
;
4144 m
->sections
[0] = eh_frame_hdr
->output_section
;
4150 if (elf_stack_flags (abfd
))
4152 amt
= sizeof (struct elf_segment_map
);
4153 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4157 m
->p_type
= PT_GNU_STACK
;
4158 m
->p_flags
= elf_stack_flags (abfd
);
4159 m
->p_align
= bed
->stack_align
;
4160 m
->p_flags_valid
= 1;
4161 m
->p_align_valid
= m
->p_align
!= 0;
4162 if (info
->stacksize
> 0)
4164 m
->p_size
= info
->stacksize
;
4165 m
->p_size_valid
= 1;
4172 if (info
!= NULL
&& info
->relro
)
4174 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4176 if (m
->p_type
== PT_LOAD
4178 && m
->sections
[0]->vma
>= info
->relro_start
4179 && m
->sections
[0]->vma
< info
->relro_end
)
4182 while (--i
!= (unsigned) -1)
4183 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4184 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4187 if (i
== (unsigned) -1)
4190 if (m
->sections
[i
]->vma
+ m
->sections
[i
]->size
4196 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4199 amt
= sizeof (struct elf_segment_map
);
4200 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4204 m
->p_type
= PT_GNU_RELRO
;
4206 m
->p_flags_valid
= 1;
4214 elf_seg_map (abfd
) = mfirst
;
4217 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4220 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4222 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4227 if (sections
!= NULL
)
4232 /* Sort sections by address. */
4235 elf_sort_sections (const void *arg1
, const void *arg2
)
4237 const asection
*sec1
= *(const asection
**) arg1
;
4238 const asection
*sec2
= *(const asection
**) arg2
;
4239 bfd_size_type size1
, size2
;
4241 /* Sort by LMA first, since this is the address used to
4242 place the section into a segment. */
4243 if (sec1
->lma
< sec2
->lma
)
4245 else if (sec1
->lma
> sec2
->lma
)
4248 /* Then sort by VMA. Normally the LMA and the VMA will be
4249 the same, and this will do nothing. */
4250 if (sec1
->vma
< sec2
->vma
)
4252 else if (sec1
->vma
> sec2
->vma
)
4255 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4257 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4263 /* If the indicies are the same, do not return 0
4264 here, but continue to try the next comparison. */
4265 if (sec1
->target_index
- sec2
->target_index
!= 0)
4266 return sec1
->target_index
- sec2
->target_index
;
4271 else if (TOEND (sec2
))
4276 /* Sort by size, to put zero sized sections
4277 before others at the same address. */
4279 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4280 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4287 return sec1
->target_index
- sec2
->target_index
;
4290 /* Ian Lance Taylor writes:
4292 We shouldn't be using % with a negative signed number. That's just
4293 not good. We have to make sure either that the number is not
4294 negative, or that the number has an unsigned type. When the types
4295 are all the same size they wind up as unsigned. When file_ptr is a
4296 larger signed type, the arithmetic winds up as signed long long,
4299 What we're trying to say here is something like ``increase OFF by
4300 the least amount that will cause it to be equal to the VMA modulo
4302 /* In other words, something like:
4304 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4305 off_offset = off % bed->maxpagesize;
4306 if (vma_offset < off_offset)
4307 adjustment = vma_offset + bed->maxpagesize - off_offset;
4309 adjustment = vma_offset - off_offset;
4311 which can can be collapsed into the expression below. */
4314 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4316 /* PR binutils/16199: Handle an alignment of zero. */
4317 if (maxpagesize
== 0)
4319 return ((vma
- off
) % maxpagesize
);
4323 print_segment_map (const struct elf_segment_map
*m
)
4326 const char *pt
= get_segment_type (m
->p_type
);
4331 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4332 sprintf (buf
, "LOPROC+%7.7x",
4333 (unsigned int) (m
->p_type
- PT_LOPROC
));
4334 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4335 sprintf (buf
, "LOOS+%7.7x",
4336 (unsigned int) (m
->p_type
- PT_LOOS
));
4338 snprintf (buf
, sizeof (buf
), "%8.8x",
4339 (unsigned int) m
->p_type
);
4343 fprintf (stderr
, "%s:", pt
);
4344 for (j
= 0; j
< m
->count
; j
++)
4345 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4351 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4356 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4358 buf
= bfd_zmalloc (len
);
4361 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4366 /* Assign file positions to the sections based on the mapping from
4367 sections to segments. This function also sets up some fields in
4371 assign_file_positions_for_load_sections (bfd
*abfd
,
4372 struct bfd_link_info
*link_info
)
4374 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4375 struct elf_segment_map
*m
;
4376 Elf_Internal_Phdr
*phdrs
;
4377 Elf_Internal_Phdr
*p
;
4379 bfd_size_type maxpagesize
;
4382 bfd_vma header_pad
= 0;
4384 if (link_info
== NULL
4385 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4389 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4393 header_pad
= m
->header_size
;
4398 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4399 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4403 /* PR binutils/12467. */
4404 elf_elfheader (abfd
)->e_phoff
= 0;
4405 elf_elfheader (abfd
)->e_phentsize
= 0;
4408 elf_elfheader (abfd
)->e_phnum
= alloc
;
4410 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4411 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4413 BFD_ASSERT (elf_program_header_size (abfd
)
4414 >= alloc
* bed
->s
->sizeof_phdr
);
4418 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4422 /* We're writing the size in elf_program_header_size (abfd),
4423 see assign_file_positions_except_relocs, so make sure we have
4424 that amount allocated, with trailing space cleared.
4425 The variable alloc contains the computed need, while
4426 elf_program_header_size (abfd) contains the size used for the
4428 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4429 where the layout is forced to according to a larger size in the
4430 last iterations for the testcase ld-elf/header. */
4431 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4433 phdrs
= (Elf_Internal_Phdr
*)
4435 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4436 sizeof (Elf_Internal_Phdr
));
4437 elf_tdata (abfd
)->phdr
= phdrs
;
4442 if ((abfd
->flags
& D_PAGED
) != 0)
4443 maxpagesize
= bed
->maxpagesize
;
4445 off
= bed
->s
->sizeof_ehdr
;
4446 off
+= alloc
* bed
->s
->sizeof_phdr
;
4447 if (header_pad
< (bfd_vma
) off
)
4453 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4455 m
= m
->next
, p
++, j
++)
4459 bfd_boolean no_contents
;
4461 /* If elf_segment_map is not from map_sections_to_segments, the
4462 sections may not be correctly ordered. NOTE: sorting should
4463 not be done to the PT_NOTE section of a corefile, which may
4464 contain several pseudo-sections artificially created by bfd.
4465 Sorting these pseudo-sections breaks things badly. */
4467 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4468 && m
->p_type
== PT_NOTE
))
4469 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4472 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4473 number of sections with contents contributing to both p_filesz
4474 and p_memsz, followed by a number of sections with no contents
4475 that just contribute to p_memsz. In this loop, OFF tracks next
4476 available file offset for PT_LOAD and PT_NOTE segments. */
4477 p
->p_type
= m
->p_type
;
4478 p
->p_flags
= m
->p_flags
;
4483 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4485 if (m
->p_paddr_valid
)
4486 p
->p_paddr
= m
->p_paddr
;
4487 else if (m
->count
== 0)
4490 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4492 if (p
->p_type
== PT_LOAD
4493 && (abfd
->flags
& D_PAGED
) != 0)
4495 /* p_align in demand paged PT_LOAD segments effectively stores
4496 the maximum page size. When copying an executable with
4497 objcopy, we set m->p_align from the input file. Use this
4498 value for maxpagesize rather than bed->maxpagesize, which
4499 may be different. Note that we use maxpagesize for PT_TLS
4500 segment alignment later in this function, so we are relying
4501 on at least one PT_LOAD segment appearing before a PT_TLS
4503 if (m
->p_align_valid
)
4504 maxpagesize
= m
->p_align
;
4506 p
->p_align
= maxpagesize
;
4508 else if (m
->p_align_valid
)
4509 p
->p_align
= m
->p_align
;
4510 else if (m
->count
== 0)
4511 p
->p_align
= 1 << bed
->s
->log_file_align
;
4515 no_contents
= FALSE
;
4517 if (p
->p_type
== PT_LOAD
4520 bfd_size_type align
;
4521 unsigned int align_power
= 0;
4523 if (m
->p_align_valid
)
4527 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4529 unsigned int secalign
;
4531 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4532 if (secalign
> align_power
)
4533 align_power
= secalign
;
4535 align
= (bfd_size_type
) 1 << align_power
;
4536 if (align
< maxpagesize
)
4537 align
= maxpagesize
;
4540 for (i
= 0; i
< m
->count
; i
++)
4541 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4542 /* If we aren't making room for this section, then
4543 it must be SHT_NOBITS regardless of what we've
4544 set via struct bfd_elf_special_section. */
4545 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4547 /* Find out whether this segment contains any loadable
4550 for (i
= 0; i
< m
->count
; i
++)
4551 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4553 no_contents
= FALSE
;
4557 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4561 /* We shouldn't need to align the segment on disk since
4562 the segment doesn't need file space, but the gABI
4563 arguably requires the alignment and glibc ld.so
4564 checks it. So to comply with the alignment
4565 requirement but not waste file space, we adjust
4566 p_offset for just this segment. (OFF_ADJUST is
4567 subtracted from OFF later.) This may put p_offset
4568 past the end of file, but that shouldn't matter. */
4573 /* Make sure the .dynamic section is the first section in the
4574 PT_DYNAMIC segment. */
4575 else if (p
->p_type
== PT_DYNAMIC
4577 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4580 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4582 bfd_set_error (bfd_error_bad_value
);
4585 /* Set the note section type to SHT_NOTE. */
4586 else if (p
->p_type
== PT_NOTE
)
4587 for (i
= 0; i
< m
->count
; i
++)
4588 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4594 if (m
->includes_filehdr
)
4596 if (!m
->p_flags_valid
)
4598 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4599 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4602 if (p
->p_vaddr
< (bfd_vma
) off
)
4604 (*_bfd_error_handler
)
4605 (_("%B: Not enough room for program headers, try linking with -N"),
4607 bfd_set_error (bfd_error_bad_value
);
4612 if (!m
->p_paddr_valid
)
4617 if (m
->includes_phdrs
)
4619 if (!m
->p_flags_valid
)
4622 if (!m
->includes_filehdr
)
4624 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4628 p
->p_vaddr
-= off
- p
->p_offset
;
4629 if (!m
->p_paddr_valid
)
4630 p
->p_paddr
-= off
- p
->p_offset
;
4634 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4635 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4638 p
->p_filesz
+= header_pad
;
4639 p
->p_memsz
+= header_pad
;
4643 if (p
->p_type
== PT_LOAD
4644 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4646 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4652 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4654 p
->p_filesz
+= adjust
;
4655 p
->p_memsz
+= adjust
;
4659 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4660 maps. Set filepos for sections in PT_LOAD segments, and in
4661 core files, for sections in PT_NOTE segments.
4662 assign_file_positions_for_non_load_sections will set filepos
4663 for other sections and update p_filesz for other segments. */
4664 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4667 bfd_size_type align
;
4668 Elf_Internal_Shdr
*this_hdr
;
4671 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4672 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4674 if ((p
->p_type
== PT_LOAD
4675 || p
->p_type
== PT_TLS
)
4676 && (this_hdr
->sh_type
!= SHT_NOBITS
4677 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4678 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4679 || p
->p_type
== PT_TLS
))))
4681 bfd_vma p_start
= p
->p_paddr
;
4682 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4683 bfd_vma s_start
= sec
->lma
;
4684 bfd_vma adjust
= s_start
- p_end
;
4688 || p_end
< p_start
))
4690 (*_bfd_error_handler
)
4691 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4692 (unsigned long) s_start
, (unsigned long) p_end
);
4696 p
->p_memsz
+= adjust
;
4698 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4700 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4702 /* We have a PROGBITS section following NOBITS ones.
4703 Allocate file space for the NOBITS section(s) and
4705 adjust
= p
->p_memsz
- p
->p_filesz
;
4706 if (!write_zeros (abfd
, off
, adjust
))
4710 p
->p_filesz
+= adjust
;
4714 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4716 /* The section at i == 0 is the one that actually contains
4720 this_hdr
->sh_offset
= sec
->filepos
= off
;
4721 off
+= this_hdr
->sh_size
;
4722 p
->p_filesz
= this_hdr
->sh_size
;
4728 /* The rest are fake sections that shouldn't be written. */
4737 if (p
->p_type
== PT_LOAD
)
4739 this_hdr
->sh_offset
= sec
->filepos
= off
;
4740 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4741 off
+= this_hdr
->sh_size
;
4743 else if (this_hdr
->sh_type
== SHT_NOBITS
4744 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
4745 && this_hdr
->sh_offset
== 0)
4747 /* This is a .tbss section that didn't get a PT_LOAD.
4748 (See _bfd_elf_map_sections_to_segments "Create a
4749 final PT_LOAD".) Set sh_offset to the value it
4750 would have if we had created a zero p_filesz and
4751 p_memsz PT_LOAD header for the section. This
4752 also makes the PT_TLS header have the same
4754 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
4756 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
4759 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4761 p
->p_filesz
+= this_hdr
->sh_size
;
4762 /* A load section without SHF_ALLOC is something like
4763 a note section in a PT_NOTE segment. These take
4764 file space but are not loaded into memory. */
4765 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4766 p
->p_memsz
+= this_hdr
->sh_size
;
4768 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4770 if (p
->p_type
== PT_TLS
)
4771 p
->p_memsz
+= this_hdr
->sh_size
;
4773 /* .tbss is special. It doesn't contribute to p_memsz of
4775 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4776 p
->p_memsz
+= this_hdr
->sh_size
;
4779 if (align
> p
->p_align
4780 && !m
->p_align_valid
4781 && (p
->p_type
!= PT_LOAD
4782 || (abfd
->flags
& D_PAGED
) == 0))
4786 if (!m
->p_flags_valid
)
4789 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4791 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4797 /* Check that all sections are in a PT_LOAD segment.
4798 Don't check funky gdb generated core files. */
4799 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4801 bfd_boolean check_vma
= TRUE
;
4803 for (i
= 1; i
< m
->count
; i
++)
4804 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4805 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4806 ->this_hdr
), p
) != 0
4807 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4808 ->this_hdr
), p
) != 0)
4810 /* Looks like we have overlays packed into the segment. */
4815 for (i
= 0; i
< m
->count
; i
++)
4817 Elf_Internal_Shdr
*this_hdr
;
4820 sec
= m
->sections
[i
];
4821 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4822 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
4823 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
4825 (*_bfd_error_handler
)
4826 (_("%B: section `%A' can't be allocated in segment %d"),
4828 print_segment_map (m
);
4834 elf_next_file_pos (abfd
) = off
;
4838 /* Assign file positions for the other sections. */
4841 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4842 struct bfd_link_info
*link_info
)
4844 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4845 Elf_Internal_Shdr
**i_shdrpp
;
4846 Elf_Internal_Shdr
**hdrpp
;
4847 Elf_Internal_Phdr
*phdrs
;
4848 Elf_Internal_Phdr
*p
;
4849 struct elf_segment_map
*m
;
4850 struct elf_segment_map
*hdrs_segment
;
4851 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4852 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4854 unsigned int num_sec
;
4858 i_shdrpp
= elf_elfsections (abfd
);
4859 num_sec
= elf_numsections (abfd
);
4860 off
= elf_next_file_pos (abfd
);
4861 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4863 Elf_Internal_Shdr
*hdr
;
4866 if (hdr
->bfd_section
!= NULL
4867 && (hdr
->bfd_section
->filepos
!= 0
4868 || (hdr
->sh_type
== SHT_NOBITS
4869 && hdr
->contents
== NULL
)))
4870 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4871 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4873 if (hdr
->sh_size
!= 0)
4874 (*_bfd_error_handler
)
4875 (_("%B: warning: allocated section `%s' not in segment"),
4877 (hdr
->bfd_section
== NULL
4879 : hdr
->bfd_section
->name
));
4880 /* We don't need to page align empty sections. */
4881 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4882 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4885 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4887 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4890 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4891 && hdr
->bfd_section
== NULL
)
4892 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
4893 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
4894 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)])
4895 hdr
->sh_offset
= -1;
4897 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4900 /* Now that we have set the section file positions, we can set up
4901 the file positions for the non PT_LOAD segments. */
4905 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4907 hdrs_segment
= NULL
;
4908 phdrs
= elf_tdata (abfd
)->phdr
;
4909 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
4912 if (p
->p_type
!= PT_LOAD
)
4915 if (m
->includes_filehdr
)
4917 filehdr_vaddr
= p
->p_vaddr
;
4918 filehdr_paddr
= p
->p_paddr
;
4920 if (m
->includes_phdrs
)
4922 phdrs_vaddr
= p
->p_vaddr
;
4923 phdrs_paddr
= p
->p_paddr
;
4924 if (m
->includes_filehdr
)
4927 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4928 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4933 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
4935 /* There is a segment that contains both the file headers and the
4936 program headers, so provide a symbol __ehdr_start pointing there.
4937 A program can use this to examine itself robustly. */
4939 struct elf_link_hash_entry
*hash
4940 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
4941 FALSE
, FALSE
, TRUE
);
4942 /* If the symbol was referenced and not defined, define it. */
4944 && (hash
->root
.type
== bfd_link_hash_new
4945 || hash
->root
.type
== bfd_link_hash_undefined
4946 || hash
->root
.type
== bfd_link_hash_undefweak
4947 || hash
->root
.type
== bfd_link_hash_common
))
4950 if (hdrs_segment
->count
!= 0)
4951 /* The segment contains sections, so use the first one. */
4952 s
= hdrs_segment
->sections
[0];
4954 /* Use the first (i.e. lowest-addressed) section in any segment. */
4955 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4964 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
4965 hash
->root
.u
.def
.section
= s
;
4969 hash
->root
.u
.def
.value
= filehdr_vaddr
;
4970 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
4973 hash
->root
.type
= bfd_link_hash_defined
;
4974 hash
->def_regular
= 1;
4979 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
4981 if (p
->p_type
== PT_GNU_RELRO
)
4983 const Elf_Internal_Phdr
*lp
;
4984 struct elf_segment_map
*lm
;
4986 if (link_info
!= NULL
)
4988 /* During linking the range of the RELRO segment is passed
4990 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
4992 lm
= lm
->next
, lp
++)
4994 if (lp
->p_type
== PT_LOAD
4995 && lp
->p_vaddr
< link_info
->relro_end
4996 && lp
->p_vaddr
+ lp
->p_filesz
>= link_info
->relro_end
4998 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
5002 /* PR ld/14207. If the RELRO segment doesn't fit in the
5003 LOAD segment, it should be removed. */
5004 BFD_ASSERT (lm
!= NULL
);
5008 /* Otherwise we are copying an executable or shared
5009 library, but we need to use the same linker logic. */
5010 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5012 if (lp
->p_type
== PT_LOAD
5013 && lp
->p_paddr
== p
->p_paddr
)
5018 if (lp
< phdrs
+ count
)
5020 p
->p_vaddr
= lp
->p_vaddr
;
5021 p
->p_paddr
= lp
->p_paddr
;
5022 p
->p_offset
= lp
->p_offset
;
5023 if (link_info
!= NULL
)
5024 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5025 else if (m
->p_size_valid
)
5026 p
->p_filesz
= m
->p_size
;
5029 p
->p_memsz
= p
->p_filesz
;
5030 /* Preserve the alignment and flags if they are valid. The
5031 gold linker generates RW/4 for the PT_GNU_RELRO section.
5032 It is better for objcopy/strip to honor these attributes
5033 otherwise gdb will choke when using separate debug files.
5035 if (!m
->p_align_valid
)
5037 if (!m
->p_flags_valid
)
5038 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5042 memset (p
, 0, sizeof *p
);
5043 p
->p_type
= PT_NULL
;
5046 else if (p
->p_type
== PT_GNU_STACK
)
5048 if (m
->p_size_valid
)
5049 p
->p_memsz
= m
->p_size
;
5051 else if (m
->count
!= 0)
5053 if (p
->p_type
!= PT_LOAD
5054 && (p
->p_type
!= PT_NOTE
5055 || bfd_get_format (abfd
) != bfd_core
))
5057 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
5060 p
->p_offset
= m
->sections
[0]->filepos
;
5061 for (i
= m
->count
; i
-- != 0;)
5063 asection
*sect
= m
->sections
[i
];
5064 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5065 if (hdr
->sh_type
!= SHT_NOBITS
)
5067 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5074 else if (m
->includes_filehdr
)
5076 p
->p_vaddr
= filehdr_vaddr
;
5077 if (! m
->p_paddr_valid
)
5078 p
->p_paddr
= filehdr_paddr
;
5080 else if (m
->includes_phdrs
)
5082 p
->p_vaddr
= phdrs_vaddr
;
5083 if (! m
->p_paddr_valid
)
5084 p
->p_paddr
= phdrs_paddr
;
5088 elf_next_file_pos (abfd
) = off
;
5093 /* Work out the file positions of all the sections. This is called by
5094 _bfd_elf_compute_section_file_positions. All the section sizes and
5095 VMAs must be known before this is called.
5097 Reloc sections come in two flavours: Those processed specially as
5098 "side-channel" data attached to a section to which they apply, and
5099 those that bfd doesn't process as relocations. The latter sort are
5100 stored in a normal bfd section by bfd_section_from_shdr. We don't
5101 consider the former sort here, unless they form part of the loadable
5102 image. Reloc sections not assigned here will be handled later by
5103 assign_file_positions_for_relocs.
5105 We also don't set the positions of the .symtab and .strtab here. */
5108 assign_file_positions_except_relocs (bfd
*abfd
,
5109 struct bfd_link_info
*link_info
)
5111 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5112 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5114 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5116 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5117 && bfd_get_format (abfd
) != bfd_core
)
5119 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5120 unsigned int num_sec
= elf_numsections (abfd
);
5121 Elf_Internal_Shdr
**hdrpp
;
5124 /* Start after the ELF header. */
5125 off
= i_ehdrp
->e_ehsize
;
5127 /* We are not creating an executable, which means that we are
5128 not creating a program header, and that the actual order of
5129 the sections in the file is unimportant. */
5130 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5132 Elf_Internal_Shdr
*hdr
;
5135 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5136 && hdr
->bfd_section
== NULL
)
5137 || i
== elf_onesymtab (abfd
)
5138 || i
== elf_symtab_shndx (abfd
)
5139 || i
== elf_strtab_sec (abfd
))
5141 hdr
->sh_offset
= -1;
5144 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5151 /* Assign file positions for the loaded sections based on the
5152 assignment of sections to segments. */
5153 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5156 /* And for non-load sections. */
5157 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5160 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5162 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5166 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. */
5167 if (link_info
!= NULL
5168 && link_info
->executable
5169 && link_info
->shared
)
5171 unsigned int num_segments
= elf_elfheader (abfd
)->e_phnum
;
5172 Elf_Internal_Phdr
*segment
= elf_tdata (abfd
)->phdr
;
5173 Elf_Internal_Phdr
*end_segment
= &segment
[num_segments
];
5175 /* Find the lowest p_vaddr in PT_LOAD segments. */
5176 bfd_vma p_vaddr
= (bfd_vma
) -1;
5177 for (; segment
< end_segment
; segment
++)
5178 if (segment
->p_type
== PT_LOAD
&& p_vaddr
> segment
->p_vaddr
)
5179 p_vaddr
= segment
->p_vaddr
;
5181 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
5182 segments is non-zero. */
5184 i_ehdrp
->e_type
= ET_EXEC
;
5187 /* Write out the program headers. */
5188 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5189 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5190 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5193 off
= elf_next_file_pos (abfd
);
5196 /* Place the section headers. */
5197 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5198 i_ehdrp
->e_shoff
= off
;
5199 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5201 elf_next_file_pos (abfd
) = off
;
5207 prep_headers (bfd
*abfd
)
5209 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5210 struct elf_strtab_hash
*shstrtab
;
5211 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5213 i_ehdrp
= elf_elfheader (abfd
);
5215 shstrtab
= _bfd_elf_strtab_init ();
5216 if (shstrtab
== NULL
)
5219 elf_shstrtab (abfd
) = shstrtab
;
5221 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5222 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5223 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5224 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5226 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5227 i_ehdrp
->e_ident
[EI_DATA
] =
5228 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5229 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5231 if ((abfd
->flags
& DYNAMIC
) != 0)
5232 i_ehdrp
->e_type
= ET_DYN
;
5233 else if ((abfd
->flags
& EXEC_P
) != 0)
5234 i_ehdrp
->e_type
= ET_EXEC
;
5235 else if (bfd_get_format (abfd
) == bfd_core
)
5236 i_ehdrp
->e_type
= ET_CORE
;
5238 i_ehdrp
->e_type
= ET_REL
;
5240 switch (bfd_get_arch (abfd
))
5242 case bfd_arch_unknown
:
5243 i_ehdrp
->e_machine
= EM_NONE
;
5246 /* There used to be a long list of cases here, each one setting
5247 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5248 in the corresponding bfd definition. To avoid duplication,
5249 the switch was removed. Machines that need special handling
5250 can generally do it in elf_backend_final_write_processing(),
5251 unless they need the information earlier than the final write.
5252 Such need can generally be supplied by replacing the tests for
5253 e_machine with the conditions used to determine it. */
5255 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5258 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5259 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5261 /* No program header, for now. */
5262 i_ehdrp
->e_phoff
= 0;
5263 i_ehdrp
->e_phentsize
= 0;
5264 i_ehdrp
->e_phnum
= 0;
5266 /* Each bfd section is section header entry. */
5267 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5268 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5270 /* If we're building an executable, we'll need a program header table. */
5271 if (abfd
->flags
& EXEC_P
)
5272 /* It all happens later. */
5276 i_ehdrp
->e_phentsize
= 0;
5277 i_ehdrp
->e_phoff
= 0;
5280 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5281 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5282 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5283 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5284 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5285 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5286 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5287 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5288 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5294 /* Assign file positions for all the reloc sections which are not part
5295 of the loadable file image. */
5298 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5301 unsigned int i
, num_sec
;
5302 Elf_Internal_Shdr
**shdrpp
;
5304 off
= elf_next_file_pos (abfd
);
5306 num_sec
= elf_numsections (abfd
);
5307 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5309 Elf_Internal_Shdr
*shdrp
;
5312 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5313 && shdrp
->sh_offset
== -1)
5314 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5317 elf_next_file_pos (abfd
) = off
;
5321 _bfd_elf_write_object_contents (bfd
*abfd
)
5323 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5324 Elf_Internal_Shdr
**i_shdrp
;
5326 unsigned int count
, num_sec
;
5327 struct elf_obj_tdata
*t
;
5329 if (! abfd
->output_has_begun
5330 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5333 i_shdrp
= elf_elfsections (abfd
);
5336 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5340 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5342 /* After writing the headers, we need to write the sections too... */
5343 num_sec
= elf_numsections (abfd
);
5344 for (count
= 1; count
< num_sec
; count
++)
5346 if (bed
->elf_backend_section_processing
)
5347 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5348 if (i_shdrp
[count
]->contents
)
5350 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5352 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5353 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5358 /* Write out the section header names. */
5359 t
= elf_tdata (abfd
);
5360 if (elf_shstrtab (abfd
) != NULL
5361 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5362 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5365 if (bed
->elf_backend_final_write_processing
)
5366 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5368 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5371 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5372 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5373 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5379 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5381 /* Hopefully this can be done just like an object file. */
5382 return _bfd_elf_write_object_contents (abfd
);
5385 /* Given a section, search the header to find them. */
5388 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5390 const struct elf_backend_data
*bed
;
5391 unsigned int sec_index
;
5393 if (elf_section_data (asect
) != NULL
5394 && elf_section_data (asect
)->this_idx
!= 0)
5395 return elf_section_data (asect
)->this_idx
;
5397 if (bfd_is_abs_section (asect
))
5398 sec_index
= SHN_ABS
;
5399 else if (bfd_is_com_section (asect
))
5400 sec_index
= SHN_COMMON
;
5401 else if (bfd_is_und_section (asect
))
5402 sec_index
= SHN_UNDEF
;
5404 sec_index
= SHN_BAD
;
5406 bed
= get_elf_backend_data (abfd
);
5407 if (bed
->elf_backend_section_from_bfd_section
)
5409 int retval
= sec_index
;
5411 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5415 if (sec_index
== SHN_BAD
)
5416 bfd_set_error (bfd_error_nonrepresentable_section
);
5421 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5425 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5427 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5429 flagword flags
= asym_ptr
->flags
;
5431 /* When gas creates relocations against local labels, it creates its
5432 own symbol for the section, but does put the symbol into the
5433 symbol chain, so udata is 0. When the linker is generating
5434 relocatable output, this section symbol may be for one of the
5435 input sections rather than the output section. */
5436 if (asym_ptr
->udata
.i
== 0
5437 && (flags
& BSF_SECTION_SYM
)
5438 && asym_ptr
->section
)
5443 sec
= asym_ptr
->section
;
5444 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5445 sec
= sec
->output_section
;
5446 if (sec
->owner
== abfd
5447 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5448 && elf_section_syms (abfd
)[indx
] != NULL
)
5449 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5452 idx
= asym_ptr
->udata
.i
;
5456 /* This case can occur when using --strip-symbol on a symbol
5457 which is used in a relocation entry. */
5458 (*_bfd_error_handler
)
5459 (_("%B: symbol `%s' required but not present"),
5460 abfd
, bfd_asymbol_name (asym_ptr
));
5461 bfd_set_error (bfd_error_no_symbols
);
5468 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5469 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5477 /* Rewrite program header information. */
5480 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5482 Elf_Internal_Ehdr
*iehdr
;
5483 struct elf_segment_map
*map
;
5484 struct elf_segment_map
*map_first
;
5485 struct elf_segment_map
**pointer_to_map
;
5486 Elf_Internal_Phdr
*segment
;
5489 unsigned int num_segments
;
5490 bfd_boolean phdr_included
= FALSE
;
5491 bfd_boolean p_paddr_valid
;
5492 bfd_vma maxpagesize
;
5493 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5494 unsigned int phdr_adjust_num
= 0;
5495 const struct elf_backend_data
*bed
;
5497 bed
= get_elf_backend_data (ibfd
);
5498 iehdr
= elf_elfheader (ibfd
);
5501 pointer_to_map
= &map_first
;
5503 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5504 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5506 /* Returns the end address of the segment + 1. */
5507 #define SEGMENT_END(segment, start) \
5508 (start + (segment->p_memsz > segment->p_filesz \
5509 ? segment->p_memsz : segment->p_filesz))
5511 #define SECTION_SIZE(section, segment) \
5512 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5513 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5514 ? section->size : 0)
5516 /* Returns TRUE if the given section is contained within
5517 the given segment. VMA addresses are compared. */
5518 #define IS_CONTAINED_BY_VMA(section, segment) \
5519 (section->vma >= segment->p_vaddr \
5520 && (section->vma + SECTION_SIZE (section, segment) \
5521 <= (SEGMENT_END (segment, segment->p_vaddr))))
5523 /* Returns TRUE if the given section is contained within
5524 the given segment. LMA addresses are compared. */
5525 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5526 (section->lma >= base \
5527 && (section->lma + SECTION_SIZE (section, segment) \
5528 <= SEGMENT_END (segment, base)))
5530 /* Handle PT_NOTE segment. */
5531 #define IS_NOTE(p, s) \
5532 (p->p_type == PT_NOTE \
5533 && elf_section_type (s) == SHT_NOTE \
5534 && (bfd_vma) s->filepos >= p->p_offset \
5535 && ((bfd_vma) s->filepos + s->size \
5536 <= p->p_offset + p->p_filesz))
5538 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5540 #define IS_COREFILE_NOTE(p, s) \
5542 && bfd_get_format (ibfd) == bfd_core \
5546 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5547 linker, which generates a PT_INTERP section with p_vaddr and
5548 p_memsz set to 0. */
5549 #define IS_SOLARIS_PT_INTERP(p, s) \
5551 && p->p_paddr == 0 \
5552 && p->p_memsz == 0 \
5553 && p->p_filesz > 0 \
5554 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5556 && (bfd_vma) s->filepos >= p->p_offset \
5557 && ((bfd_vma) s->filepos + s->size \
5558 <= p->p_offset + p->p_filesz))
5560 /* Decide if the given section should be included in the given segment.
5561 A section will be included if:
5562 1. It is within the address space of the segment -- we use the LMA
5563 if that is set for the segment and the VMA otherwise,
5564 2. It is an allocated section or a NOTE section in a PT_NOTE
5566 3. There is an output section associated with it,
5567 4. The section has not already been allocated to a previous segment.
5568 5. PT_GNU_STACK segments do not include any sections.
5569 6. PT_TLS segment includes only SHF_TLS sections.
5570 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5571 8. PT_DYNAMIC should not contain empty sections at the beginning
5572 (with the possible exception of .dynamic). */
5573 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5574 ((((segment->p_paddr \
5575 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5576 : IS_CONTAINED_BY_VMA (section, segment)) \
5577 && (section->flags & SEC_ALLOC) != 0) \
5578 || IS_NOTE (segment, section)) \
5579 && segment->p_type != PT_GNU_STACK \
5580 && (segment->p_type != PT_TLS \
5581 || (section->flags & SEC_THREAD_LOCAL)) \
5582 && (segment->p_type == PT_LOAD \
5583 || segment->p_type == PT_TLS \
5584 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5585 && (segment->p_type != PT_DYNAMIC \
5586 || SECTION_SIZE (section, segment) > 0 \
5587 || (segment->p_paddr \
5588 ? segment->p_paddr != section->lma \
5589 : segment->p_vaddr != section->vma) \
5590 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5592 && !section->segment_mark)
5594 /* If the output section of a section in the input segment is NULL,
5595 it is removed from the corresponding output segment. */
5596 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5597 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5598 && section->output_section != NULL)
5600 /* Returns TRUE iff seg1 starts after the end of seg2. */
5601 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5602 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5604 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5605 their VMA address ranges and their LMA address ranges overlap.
5606 It is possible to have overlapping VMA ranges without overlapping LMA
5607 ranges. RedBoot images for example can have both .data and .bss mapped
5608 to the same VMA range, but with the .data section mapped to a different
5610 #define SEGMENT_OVERLAPS(seg1, seg2) \
5611 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5612 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5613 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5614 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5616 /* Initialise the segment mark field. */
5617 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5618 section
->segment_mark
= FALSE
;
5620 /* The Solaris linker creates program headers in which all the
5621 p_paddr fields are zero. When we try to objcopy or strip such a
5622 file, we get confused. Check for this case, and if we find it
5623 don't set the p_paddr_valid fields. */
5624 p_paddr_valid
= FALSE
;
5625 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5628 if (segment
->p_paddr
!= 0)
5630 p_paddr_valid
= TRUE
;
5634 /* Scan through the segments specified in the program header
5635 of the input BFD. For this first scan we look for overlaps
5636 in the loadable segments. These can be created by weird
5637 parameters to objcopy. Also, fix some solaris weirdness. */
5638 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5643 Elf_Internal_Phdr
*segment2
;
5645 if (segment
->p_type
== PT_INTERP
)
5646 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5647 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5649 /* Mininal change so that the normal section to segment
5650 assignment code will work. */
5651 segment
->p_vaddr
= section
->vma
;
5655 if (segment
->p_type
!= PT_LOAD
)
5657 /* Remove PT_GNU_RELRO segment. */
5658 if (segment
->p_type
== PT_GNU_RELRO
)
5659 segment
->p_type
= PT_NULL
;
5663 /* Determine if this segment overlaps any previous segments. */
5664 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5666 bfd_signed_vma extra_length
;
5668 if (segment2
->p_type
!= PT_LOAD
5669 || !SEGMENT_OVERLAPS (segment
, segment2
))
5672 /* Merge the two segments together. */
5673 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5675 /* Extend SEGMENT2 to include SEGMENT and then delete
5677 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5678 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5680 if (extra_length
> 0)
5682 segment2
->p_memsz
+= extra_length
;
5683 segment2
->p_filesz
+= extra_length
;
5686 segment
->p_type
= PT_NULL
;
5688 /* Since we have deleted P we must restart the outer loop. */
5690 segment
= elf_tdata (ibfd
)->phdr
;
5695 /* Extend SEGMENT to include SEGMENT2 and then delete
5697 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5698 - SEGMENT_END (segment
, segment
->p_vaddr
));
5700 if (extra_length
> 0)
5702 segment
->p_memsz
+= extra_length
;
5703 segment
->p_filesz
+= extra_length
;
5706 segment2
->p_type
= PT_NULL
;
5711 /* The second scan attempts to assign sections to segments. */
5712 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5716 unsigned int section_count
;
5717 asection
**sections
;
5718 asection
*output_section
;
5720 bfd_vma matching_lma
;
5721 bfd_vma suggested_lma
;
5724 asection
*first_section
;
5725 bfd_boolean first_matching_lma
;
5726 bfd_boolean first_suggested_lma
;
5728 if (segment
->p_type
== PT_NULL
)
5731 first_section
= NULL
;
5732 /* Compute how many sections might be placed into this segment. */
5733 for (section
= ibfd
->sections
, section_count
= 0;
5735 section
= section
->next
)
5737 /* Find the first section in the input segment, which may be
5738 removed from the corresponding output segment. */
5739 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5741 if (first_section
== NULL
)
5742 first_section
= section
;
5743 if (section
->output_section
!= NULL
)
5748 /* Allocate a segment map big enough to contain
5749 all of the sections we have selected. */
5750 amt
= sizeof (struct elf_segment_map
);
5751 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5752 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5756 /* Initialise the fields of the segment map. Default to
5757 using the physical address of the segment in the input BFD. */
5759 map
->p_type
= segment
->p_type
;
5760 map
->p_flags
= segment
->p_flags
;
5761 map
->p_flags_valid
= 1;
5763 /* If the first section in the input segment is removed, there is
5764 no need to preserve segment physical address in the corresponding
5766 if (!first_section
|| first_section
->output_section
!= NULL
)
5768 map
->p_paddr
= segment
->p_paddr
;
5769 map
->p_paddr_valid
= p_paddr_valid
;
5772 /* Determine if this segment contains the ELF file header
5773 and if it contains the program headers themselves. */
5774 map
->includes_filehdr
= (segment
->p_offset
== 0
5775 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5776 map
->includes_phdrs
= 0;
5778 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5780 map
->includes_phdrs
=
5781 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5782 && (segment
->p_offset
+ segment
->p_filesz
5783 >= ((bfd_vma
) iehdr
->e_phoff
5784 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5786 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5787 phdr_included
= TRUE
;
5790 if (section_count
== 0)
5792 /* Special segments, such as the PT_PHDR segment, may contain
5793 no sections, but ordinary, loadable segments should contain
5794 something. They are allowed by the ELF spec however, so only
5795 a warning is produced. */
5796 if (segment
->p_type
== PT_LOAD
)
5797 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5798 " detected, is this intentional ?\n"),
5802 *pointer_to_map
= map
;
5803 pointer_to_map
= &map
->next
;
5808 /* Now scan the sections in the input BFD again and attempt
5809 to add their corresponding output sections to the segment map.
5810 The problem here is how to handle an output section which has
5811 been moved (ie had its LMA changed). There are four possibilities:
5813 1. None of the sections have been moved.
5814 In this case we can continue to use the segment LMA from the
5817 2. All of the sections have been moved by the same amount.
5818 In this case we can change the segment's LMA to match the LMA
5819 of the first section.
5821 3. Some of the sections have been moved, others have not.
5822 In this case those sections which have not been moved can be
5823 placed in the current segment which will have to have its size,
5824 and possibly its LMA changed, and a new segment or segments will
5825 have to be created to contain the other sections.
5827 4. The sections have been moved, but not by the same amount.
5828 In this case we can change the segment's LMA to match the LMA
5829 of the first section and we will have to create a new segment
5830 or segments to contain the other sections.
5832 In order to save time, we allocate an array to hold the section
5833 pointers that we are interested in. As these sections get assigned
5834 to a segment, they are removed from this array. */
5836 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5837 if (sections
== NULL
)
5840 /* Step One: Scan for segment vs section LMA conflicts.
5841 Also add the sections to the section array allocated above.
5842 Also add the sections to the current segment. In the common
5843 case, where the sections have not been moved, this means that
5844 we have completely filled the segment, and there is nothing
5849 first_matching_lma
= TRUE
;
5850 first_suggested_lma
= TRUE
;
5852 for (section
= ibfd
->sections
;
5854 section
= section
->next
)
5855 if (section
== first_section
)
5858 for (j
= 0; section
!= NULL
; section
= section
->next
)
5860 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5862 output_section
= section
->output_section
;
5864 sections
[j
++] = section
;
5866 /* The Solaris native linker always sets p_paddr to 0.
5867 We try to catch that case here, and set it to the
5868 correct value. Note - some backends require that
5869 p_paddr be left as zero. */
5871 && segment
->p_vaddr
!= 0
5872 && !bed
->want_p_paddr_set_to_zero
5874 && output_section
->lma
!= 0
5875 && output_section
->vma
== (segment
->p_vaddr
5876 + (map
->includes_filehdr
5879 + (map
->includes_phdrs
5881 * iehdr
->e_phentsize
)
5883 map
->p_paddr
= segment
->p_vaddr
;
5885 /* Match up the physical address of the segment with the
5886 LMA address of the output section. */
5887 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5888 || IS_COREFILE_NOTE (segment
, section
)
5889 || (bed
->want_p_paddr_set_to_zero
5890 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5892 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5894 matching_lma
= output_section
->lma
;
5895 first_matching_lma
= FALSE
;
5898 /* We assume that if the section fits within the segment
5899 then it does not overlap any other section within that
5901 map
->sections
[isec
++] = output_section
;
5903 else if (first_suggested_lma
)
5905 suggested_lma
= output_section
->lma
;
5906 first_suggested_lma
= FALSE
;
5909 if (j
== section_count
)
5914 BFD_ASSERT (j
== section_count
);
5916 /* Step Two: Adjust the physical address of the current segment,
5918 if (isec
== section_count
)
5920 /* All of the sections fitted within the segment as currently
5921 specified. This is the default case. Add the segment to
5922 the list of built segments and carry on to process the next
5923 program header in the input BFD. */
5924 map
->count
= section_count
;
5925 *pointer_to_map
= map
;
5926 pointer_to_map
= &map
->next
;
5929 && !bed
->want_p_paddr_set_to_zero
5930 && matching_lma
!= map
->p_paddr
5931 && !map
->includes_filehdr
5932 && !map
->includes_phdrs
)
5933 /* There is some padding before the first section in the
5934 segment. So, we must account for that in the output
5936 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5943 if (!first_matching_lma
)
5945 /* At least one section fits inside the current segment.
5946 Keep it, but modify its physical address to match the
5947 LMA of the first section that fitted. */
5948 map
->p_paddr
= matching_lma
;
5952 /* None of the sections fitted inside the current segment.
5953 Change the current segment's physical address to match
5954 the LMA of the first section. */
5955 map
->p_paddr
= suggested_lma
;
5958 /* Offset the segment physical address from the lma
5959 to allow for space taken up by elf headers. */
5960 if (map
->includes_filehdr
)
5962 if (map
->p_paddr
>= iehdr
->e_ehsize
)
5963 map
->p_paddr
-= iehdr
->e_ehsize
;
5966 map
->includes_filehdr
= FALSE
;
5967 map
->includes_phdrs
= FALSE
;
5971 if (map
->includes_phdrs
)
5973 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
5975 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5977 /* iehdr->e_phnum is just an estimate of the number
5978 of program headers that we will need. Make a note
5979 here of the number we used and the segment we chose
5980 to hold these headers, so that we can adjust the
5981 offset when we know the correct value. */
5982 phdr_adjust_num
= iehdr
->e_phnum
;
5983 phdr_adjust_seg
= map
;
5986 map
->includes_phdrs
= FALSE
;
5990 /* Step Three: Loop over the sections again, this time assigning
5991 those that fit to the current segment and removing them from the
5992 sections array; but making sure not to leave large gaps. Once all
5993 possible sections have been assigned to the current segment it is
5994 added to the list of built segments and if sections still remain
5995 to be assigned, a new segment is constructed before repeating
6002 first_suggested_lma
= TRUE
;
6004 /* Fill the current segment with sections that fit. */
6005 for (j
= 0; j
< section_count
; j
++)
6007 section
= sections
[j
];
6009 if (section
== NULL
)
6012 output_section
= section
->output_section
;
6014 BFD_ASSERT (output_section
!= NULL
);
6016 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
6017 || IS_COREFILE_NOTE (segment
, section
))
6019 if (map
->count
== 0)
6021 /* If the first section in a segment does not start at
6022 the beginning of the segment, then something is
6024 if (output_section
->lma
6026 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
6027 + (map
->includes_phdrs
6028 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6036 prev_sec
= map
->sections
[map
->count
- 1];
6038 /* If the gap between the end of the previous section
6039 and the start of this section is more than
6040 maxpagesize then we need to start a new segment. */
6041 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6043 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6044 || (prev_sec
->lma
+ prev_sec
->size
6045 > output_section
->lma
))
6047 if (first_suggested_lma
)
6049 suggested_lma
= output_section
->lma
;
6050 first_suggested_lma
= FALSE
;
6057 map
->sections
[map
->count
++] = output_section
;
6060 section
->segment_mark
= TRUE
;
6062 else if (first_suggested_lma
)
6064 suggested_lma
= output_section
->lma
;
6065 first_suggested_lma
= FALSE
;
6069 BFD_ASSERT (map
->count
> 0);
6071 /* Add the current segment to the list of built segments. */
6072 *pointer_to_map
= map
;
6073 pointer_to_map
= &map
->next
;
6075 if (isec
< section_count
)
6077 /* We still have not allocated all of the sections to
6078 segments. Create a new segment here, initialise it
6079 and carry on looping. */
6080 amt
= sizeof (struct elf_segment_map
);
6081 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6082 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6089 /* Initialise the fields of the segment map. Set the physical
6090 physical address to the LMA of the first section that has
6091 not yet been assigned. */
6093 map
->p_type
= segment
->p_type
;
6094 map
->p_flags
= segment
->p_flags
;
6095 map
->p_flags_valid
= 1;
6096 map
->p_paddr
= suggested_lma
;
6097 map
->p_paddr_valid
= p_paddr_valid
;
6098 map
->includes_filehdr
= 0;
6099 map
->includes_phdrs
= 0;
6102 while (isec
< section_count
);
6107 elf_seg_map (obfd
) = map_first
;
6109 /* If we had to estimate the number of program headers that were
6110 going to be needed, then check our estimate now and adjust
6111 the offset if necessary. */
6112 if (phdr_adjust_seg
!= NULL
)
6116 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6119 if (count
> phdr_adjust_num
)
6120 phdr_adjust_seg
->p_paddr
6121 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6126 #undef IS_CONTAINED_BY_VMA
6127 #undef IS_CONTAINED_BY_LMA
6129 #undef IS_COREFILE_NOTE
6130 #undef IS_SOLARIS_PT_INTERP
6131 #undef IS_SECTION_IN_INPUT_SEGMENT
6132 #undef INCLUDE_SECTION_IN_SEGMENT
6133 #undef SEGMENT_AFTER_SEGMENT
6134 #undef SEGMENT_OVERLAPS
6138 /* Copy ELF program header information. */
6141 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6143 Elf_Internal_Ehdr
*iehdr
;
6144 struct elf_segment_map
*map
;
6145 struct elf_segment_map
*map_first
;
6146 struct elf_segment_map
**pointer_to_map
;
6147 Elf_Internal_Phdr
*segment
;
6149 unsigned int num_segments
;
6150 bfd_boolean phdr_included
= FALSE
;
6151 bfd_boolean p_paddr_valid
;
6153 iehdr
= elf_elfheader (ibfd
);
6156 pointer_to_map
= &map_first
;
6158 /* If all the segment p_paddr fields are zero, don't set
6159 map->p_paddr_valid. */
6160 p_paddr_valid
= FALSE
;
6161 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6162 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6165 if (segment
->p_paddr
!= 0)
6167 p_paddr_valid
= TRUE
;
6171 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6176 unsigned int section_count
;
6178 Elf_Internal_Shdr
*this_hdr
;
6179 asection
*first_section
= NULL
;
6180 asection
*lowest_section
;
6182 /* Compute how many sections are in this segment. */
6183 for (section
= ibfd
->sections
, section_count
= 0;
6185 section
= section
->next
)
6187 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6188 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6190 if (first_section
== NULL
)
6191 first_section
= section
;
6196 /* Allocate a segment map big enough to contain
6197 all of the sections we have selected. */
6198 amt
= sizeof (struct elf_segment_map
);
6199 if (section_count
!= 0)
6200 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6201 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6205 /* Initialize the fields of the output segment map with the
6208 map
->p_type
= segment
->p_type
;
6209 map
->p_flags
= segment
->p_flags
;
6210 map
->p_flags_valid
= 1;
6211 map
->p_paddr
= segment
->p_paddr
;
6212 map
->p_paddr_valid
= p_paddr_valid
;
6213 map
->p_align
= segment
->p_align
;
6214 map
->p_align_valid
= 1;
6215 map
->p_vaddr_offset
= 0;
6217 if (map
->p_type
== PT_GNU_RELRO
6218 || map
->p_type
== PT_GNU_STACK
)
6220 /* The PT_GNU_RELRO segment may contain the first a few
6221 bytes in the .got.plt section even if the whole .got.plt
6222 section isn't in the PT_GNU_RELRO segment. We won't
6223 change the size of the PT_GNU_RELRO segment.
6224 Similarly, PT_GNU_STACK size is significant on uclinux
6226 map
->p_size
= segment
->p_memsz
;
6227 map
->p_size_valid
= 1;
6230 /* Determine if this segment contains the ELF file header
6231 and if it contains the program headers themselves. */
6232 map
->includes_filehdr
= (segment
->p_offset
== 0
6233 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6235 map
->includes_phdrs
= 0;
6236 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6238 map
->includes_phdrs
=
6239 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6240 && (segment
->p_offset
+ segment
->p_filesz
6241 >= ((bfd_vma
) iehdr
->e_phoff
6242 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6244 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6245 phdr_included
= TRUE
;
6248 lowest_section
= first_section
;
6249 if (section_count
!= 0)
6251 unsigned int isec
= 0;
6253 for (section
= first_section
;
6255 section
= section
->next
)
6257 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6258 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6260 map
->sections
[isec
++] = section
->output_section
;
6261 if ((section
->flags
& SEC_ALLOC
) != 0)
6265 if (section
->lma
< lowest_section
->lma
)
6266 lowest_section
= section
;
6268 /* Section lmas are set up from PT_LOAD header
6269 p_paddr in _bfd_elf_make_section_from_shdr.
6270 If this header has a p_paddr that disagrees
6271 with the section lma, flag the p_paddr as
6273 if ((section
->flags
& SEC_LOAD
) != 0)
6274 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6276 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6277 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6278 map
->p_paddr_valid
= FALSE
;
6280 if (isec
== section_count
)
6286 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6287 /* We need to keep the space used by the headers fixed. */
6288 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6290 if (!map
->includes_phdrs
6291 && !map
->includes_filehdr
6292 && map
->p_paddr_valid
)
6293 /* There is some other padding before the first section. */
6294 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6295 - segment
->p_paddr
);
6297 map
->count
= section_count
;
6298 *pointer_to_map
= map
;
6299 pointer_to_map
= &map
->next
;
6302 elf_seg_map (obfd
) = map_first
;
6306 /* Copy private BFD data. This copies or rewrites ELF program header
6310 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6312 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6313 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6316 if (elf_tdata (ibfd
)->phdr
== NULL
)
6319 if (ibfd
->xvec
== obfd
->xvec
)
6321 /* Check to see if any sections in the input BFD
6322 covered by ELF program header have changed. */
6323 Elf_Internal_Phdr
*segment
;
6324 asection
*section
, *osec
;
6325 unsigned int i
, num_segments
;
6326 Elf_Internal_Shdr
*this_hdr
;
6327 const struct elf_backend_data
*bed
;
6329 bed
= get_elf_backend_data (ibfd
);
6331 /* Regenerate the segment map if p_paddr is set to 0. */
6332 if (bed
->want_p_paddr_set_to_zero
)
6335 /* Initialize the segment mark field. */
6336 for (section
= obfd
->sections
; section
!= NULL
;
6337 section
= section
->next
)
6338 section
->segment_mark
= FALSE
;
6340 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6341 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6345 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6346 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6347 which severly confuses things, so always regenerate the segment
6348 map in this case. */
6349 if (segment
->p_paddr
== 0
6350 && segment
->p_memsz
== 0
6351 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6354 for (section
= ibfd
->sections
;
6355 section
!= NULL
; section
= section
->next
)
6357 /* We mark the output section so that we know it comes
6358 from the input BFD. */
6359 osec
= section
->output_section
;
6361 osec
->segment_mark
= TRUE
;
6363 /* Check if this section is covered by the segment. */
6364 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6365 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6367 /* FIXME: Check if its output section is changed or
6368 removed. What else do we need to check? */
6370 || section
->flags
!= osec
->flags
6371 || section
->lma
!= osec
->lma
6372 || section
->vma
!= osec
->vma
6373 || section
->size
!= osec
->size
6374 || section
->rawsize
!= osec
->rawsize
6375 || section
->alignment_power
!= osec
->alignment_power
)
6381 /* Check to see if any output section do not come from the
6383 for (section
= obfd
->sections
; section
!= NULL
;
6384 section
= section
->next
)
6386 if (section
->segment_mark
== FALSE
)
6389 section
->segment_mark
= FALSE
;
6392 return copy_elf_program_header (ibfd
, obfd
);
6396 if (ibfd
->xvec
== obfd
->xvec
)
6398 /* When rewriting program header, set the output maxpagesize to
6399 the maximum alignment of input PT_LOAD segments. */
6400 Elf_Internal_Phdr
*segment
;
6402 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6403 bfd_vma maxpagesize
= 0;
6405 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6408 if (segment
->p_type
== PT_LOAD
6409 && maxpagesize
< segment
->p_align
)
6410 maxpagesize
= segment
->p_align
;
6412 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6413 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6416 return rewrite_elf_program_header (ibfd
, obfd
);
6419 /* Initialize private output section information from input section. */
6422 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6426 struct bfd_link_info
*link_info
)
6429 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6430 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6432 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6433 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6436 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6438 /* For objcopy and relocatable link, don't copy the output ELF
6439 section type from input if the output BFD section flags have been
6440 set to something different. For a final link allow some flags
6441 that the linker clears to differ. */
6442 if (elf_section_type (osec
) == SHT_NULL
6443 && (osec
->flags
== isec
->flags
6445 && ((osec
->flags
^ isec
->flags
)
6446 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6447 elf_section_type (osec
) = elf_section_type (isec
);
6449 /* FIXME: Is this correct for all OS/PROC specific flags? */
6450 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6451 & (SHF_MASKOS
| SHF_MASKPROC
));
6453 /* Set things up for objcopy and relocatable link. The output
6454 SHT_GROUP section will have its elf_next_in_group pointing back
6455 to the input group members. Ignore linker created group section.
6456 See elfNN_ia64_object_p in elfxx-ia64.c. */
6459 if (elf_sec_group (isec
) == NULL
6460 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6462 if (elf_section_flags (isec
) & SHF_GROUP
)
6463 elf_section_flags (osec
) |= SHF_GROUP
;
6464 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6465 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6469 ihdr
= &elf_section_data (isec
)->this_hdr
;
6471 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6472 don't use the output section of the linked-to section since it
6473 may be NULL at this point. */
6474 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6476 ohdr
= &elf_section_data (osec
)->this_hdr
;
6477 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6478 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6481 osec
->use_rela_p
= isec
->use_rela_p
;
6486 /* Copy private section information. This copies over the entsize
6487 field, and sometimes the info field. */
6490 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6495 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6497 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6498 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6501 ihdr
= &elf_section_data (isec
)->this_hdr
;
6502 ohdr
= &elf_section_data (osec
)->this_hdr
;
6504 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6506 if (ihdr
->sh_type
== SHT_SYMTAB
6507 || ihdr
->sh_type
== SHT_DYNSYM
6508 || ihdr
->sh_type
== SHT_GNU_verneed
6509 || ihdr
->sh_type
== SHT_GNU_verdef
)
6510 ohdr
->sh_info
= ihdr
->sh_info
;
6512 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6516 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6517 necessary if we are removing either the SHT_GROUP section or any of
6518 the group member sections. DISCARDED is the value that a section's
6519 output_section has if the section will be discarded, NULL when this
6520 function is called from objcopy, bfd_abs_section_ptr when called
6524 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6528 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6529 if (elf_section_type (isec
) == SHT_GROUP
)
6531 asection
*first
= elf_next_in_group (isec
);
6532 asection
*s
= first
;
6533 bfd_size_type removed
= 0;
6537 /* If this member section is being output but the
6538 SHT_GROUP section is not, then clear the group info
6539 set up by _bfd_elf_copy_private_section_data. */
6540 if (s
->output_section
!= discarded
6541 && isec
->output_section
== discarded
)
6543 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6544 elf_group_name (s
->output_section
) = NULL
;
6546 /* Conversely, if the member section is not being output
6547 but the SHT_GROUP section is, then adjust its size. */
6548 else if (s
->output_section
== discarded
6549 && isec
->output_section
!= discarded
)
6551 s
= elf_next_in_group (s
);
6557 if (discarded
!= NULL
)
6559 /* If we've been called for ld -r, then we need to
6560 adjust the input section size. This function may
6561 be called multiple times, so save the original
6563 if (isec
->rawsize
== 0)
6564 isec
->rawsize
= isec
->size
;
6565 isec
->size
= isec
->rawsize
- removed
;
6569 /* Adjust the output section size when called from
6571 isec
->output_section
->size
-= removed
;
6579 /* Copy private header information. */
6582 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6584 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6585 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6588 /* Copy over private BFD data if it has not already been copied.
6589 This must be done here, rather than in the copy_private_bfd_data
6590 entry point, because the latter is called after the section
6591 contents have been set, which means that the program headers have
6592 already been worked out. */
6593 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6595 if (! copy_private_bfd_data (ibfd
, obfd
))
6599 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6602 /* Copy private symbol information. If this symbol is in a section
6603 which we did not map into a BFD section, try to map the section
6604 index correctly. We use special macro definitions for the mapped
6605 section indices; these definitions are interpreted by the
6606 swap_out_syms function. */
6608 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6609 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6610 #define MAP_STRTAB (SHN_HIOS + 3)
6611 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6612 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6615 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6620 elf_symbol_type
*isym
, *osym
;
6622 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6623 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6626 isym
= elf_symbol_from (ibfd
, isymarg
);
6627 osym
= elf_symbol_from (obfd
, osymarg
);
6630 && isym
->internal_elf_sym
.st_shndx
!= 0
6632 && bfd_is_abs_section (isym
->symbol
.section
))
6636 shndx
= isym
->internal_elf_sym
.st_shndx
;
6637 if (shndx
== elf_onesymtab (ibfd
))
6638 shndx
= MAP_ONESYMTAB
;
6639 else if (shndx
== elf_dynsymtab (ibfd
))
6640 shndx
= MAP_DYNSYMTAB
;
6641 else if (shndx
== elf_strtab_sec (ibfd
))
6643 else if (shndx
== elf_shstrtab_sec (ibfd
))
6644 shndx
= MAP_SHSTRTAB
;
6645 else if (shndx
== elf_symtab_shndx (ibfd
))
6646 shndx
= MAP_SYM_SHNDX
;
6647 osym
->internal_elf_sym
.st_shndx
= shndx
;
6653 /* Swap out the symbols. */
6656 swap_out_syms (bfd
*abfd
,
6657 struct bfd_strtab_hash
**sttp
,
6660 const struct elf_backend_data
*bed
;
6663 struct bfd_strtab_hash
*stt
;
6664 Elf_Internal_Shdr
*symtab_hdr
;
6665 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6666 Elf_Internal_Shdr
*symstrtab_hdr
;
6667 bfd_byte
*outbound_syms
;
6668 bfd_byte
*outbound_shndx
;
6670 unsigned int num_locals
;
6672 bfd_boolean name_local_sections
;
6674 if (!elf_map_symbols (abfd
, &num_locals
))
6677 /* Dump out the symtabs. */
6678 stt
= _bfd_elf_stringtab_init ();
6682 bed
= get_elf_backend_data (abfd
);
6683 symcount
= bfd_get_symcount (abfd
);
6684 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6685 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6686 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6687 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6688 symtab_hdr
->sh_info
= num_locals
+ 1;
6689 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6691 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6692 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6694 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6695 bed
->s
->sizeof_sym
);
6696 if (outbound_syms
== NULL
)
6698 _bfd_stringtab_free (stt
);
6701 symtab_hdr
->contents
= outbound_syms
;
6703 outbound_shndx
= NULL
;
6704 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6705 if (symtab_shndx_hdr
->sh_name
!= 0)
6707 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6708 outbound_shndx
= (bfd_byte
*)
6709 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6710 if (outbound_shndx
== NULL
)
6712 _bfd_stringtab_free (stt
);
6716 symtab_shndx_hdr
->contents
= outbound_shndx
;
6717 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6718 symtab_shndx_hdr
->sh_size
= amt
;
6719 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6720 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6723 /* Now generate the data (for "contents"). */
6725 /* Fill in zeroth symbol and swap it out. */
6726 Elf_Internal_Sym sym
;
6732 sym
.st_shndx
= SHN_UNDEF
;
6733 sym
.st_target_internal
= 0;
6734 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6735 outbound_syms
+= bed
->s
->sizeof_sym
;
6736 if (outbound_shndx
!= NULL
)
6737 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6741 = (bed
->elf_backend_name_local_section_symbols
6742 && bed
->elf_backend_name_local_section_symbols (abfd
));
6744 syms
= bfd_get_outsymbols (abfd
);
6745 for (idx
= 0; idx
< symcount
; idx
++)
6747 Elf_Internal_Sym sym
;
6748 bfd_vma value
= syms
[idx
]->value
;
6749 elf_symbol_type
*type_ptr
;
6750 flagword flags
= syms
[idx
]->flags
;
6753 if (!name_local_sections
6754 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6756 /* Local section symbols have no name. */
6761 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6764 if (sym
.st_name
== (unsigned long) -1)
6766 _bfd_stringtab_free (stt
);
6771 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6773 if ((flags
& BSF_SECTION_SYM
) == 0
6774 && bfd_is_com_section (syms
[idx
]->section
))
6776 /* ELF common symbols put the alignment into the `value' field,
6777 and the size into the `size' field. This is backwards from
6778 how BFD handles it, so reverse it here. */
6779 sym
.st_size
= value
;
6780 if (type_ptr
== NULL
6781 || type_ptr
->internal_elf_sym
.st_value
== 0)
6782 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6784 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6785 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6786 (abfd
, syms
[idx
]->section
);
6790 asection
*sec
= syms
[idx
]->section
;
6793 if (sec
->output_section
)
6795 value
+= sec
->output_offset
;
6796 sec
= sec
->output_section
;
6799 /* Don't add in the section vma for relocatable output. */
6800 if (! relocatable_p
)
6802 sym
.st_value
= value
;
6803 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6805 if (bfd_is_abs_section (sec
)
6807 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6809 /* This symbol is in a real ELF section which we did
6810 not create as a BFD section. Undo the mapping done
6811 by copy_private_symbol_data. */
6812 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6816 shndx
= elf_onesymtab (abfd
);
6819 shndx
= elf_dynsymtab (abfd
);
6822 shndx
= elf_strtab_sec (abfd
);
6825 shndx
= elf_shstrtab_sec (abfd
);
6828 shndx
= elf_symtab_shndx (abfd
);
6837 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6839 if (shndx
== SHN_BAD
)
6843 /* Writing this would be a hell of a lot easier if
6844 we had some decent documentation on bfd, and
6845 knew what to expect of the library, and what to
6846 demand of applications. For example, it
6847 appears that `objcopy' might not set the
6848 section of a symbol to be a section that is
6849 actually in the output file. */
6850 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6853 _bfd_error_handler (_("\
6854 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6855 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6857 bfd_set_error (bfd_error_invalid_operation
);
6858 _bfd_stringtab_free (stt
);
6862 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6863 BFD_ASSERT (shndx
!= SHN_BAD
);
6867 sym
.st_shndx
= shndx
;
6870 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6872 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6873 type
= STT_GNU_IFUNC
;
6874 else if ((flags
& BSF_FUNCTION
) != 0)
6876 else if ((flags
& BSF_OBJECT
) != 0)
6878 else if ((flags
& BSF_RELC
) != 0)
6880 else if ((flags
& BSF_SRELC
) != 0)
6885 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6888 /* Processor-specific types. */
6889 if (type_ptr
!= NULL
6890 && bed
->elf_backend_get_symbol_type
)
6891 type
= ((*bed
->elf_backend_get_symbol_type
)
6892 (&type_ptr
->internal_elf_sym
, type
));
6894 if (flags
& BSF_SECTION_SYM
)
6896 if (flags
& BSF_GLOBAL
)
6897 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6899 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6901 else if (bfd_is_com_section (syms
[idx
]->section
))
6903 #ifdef USE_STT_COMMON
6904 if (type
== STT_OBJECT
)
6905 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6908 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6910 else if (bfd_is_und_section (syms
[idx
]->section
))
6911 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6915 else if (flags
& BSF_FILE
)
6916 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6919 int bind
= STB_LOCAL
;
6921 if (flags
& BSF_LOCAL
)
6923 else if (flags
& BSF_GNU_UNIQUE
)
6924 bind
= STB_GNU_UNIQUE
;
6925 else if (flags
& BSF_WEAK
)
6927 else if (flags
& BSF_GLOBAL
)
6930 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6933 if (type_ptr
!= NULL
)
6935 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6936 sym
.st_target_internal
6937 = type_ptr
->internal_elf_sym
.st_target_internal
;
6942 sym
.st_target_internal
= 0;
6945 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6946 outbound_syms
+= bed
->s
->sizeof_sym
;
6947 if (outbound_shndx
!= NULL
)
6948 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6952 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6953 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6955 symstrtab_hdr
->sh_flags
= 0;
6956 symstrtab_hdr
->sh_addr
= 0;
6957 symstrtab_hdr
->sh_entsize
= 0;
6958 symstrtab_hdr
->sh_link
= 0;
6959 symstrtab_hdr
->sh_info
= 0;
6960 symstrtab_hdr
->sh_addralign
= 1;
6965 /* Return the number of bytes required to hold the symtab vector.
6967 Note that we base it on the count plus 1, since we will null terminate
6968 the vector allocated based on this size. However, the ELF symbol table
6969 always has a dummy entry as symbol #0, so it ends up even. */
6972 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6976 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6978 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6979 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6981 symtab_size
-= sizeof (asymbol
*);
6987 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6991 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6993 if (elf_dynsymtab (abfd
) == 0)
6995 bfd_set_error (bfd_error_invalid_operation
);
6999 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
7000 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
7002 symtab_size
-= sizeof (asymbol
*);
7008 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
7011 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
7014 /* Canonicalize the relocs. */
7017 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
7024 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7026 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
7029 tblptr
= section
->relocation
;
7030 for (i
= 0; i
< section
->reloc_count
; i
++)
7031 *relptr
++ = tblptr
++;
7035 return section
->reloc_count
;
7039 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7041 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7042 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7045 bfd_get_symcount (abfd
) = symcount
;
7050 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7051 asymbol
**allocation
)
7053 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7054 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7057 bfd_get_dynamic_symcount (abfd
) = symcount
;
7061 /* Return the size required for the dynamic reloc entries. Any loadable
7062 section that was actually installed in the BFD, and has type SHT_REL
7063 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7064 dynamic reloc section. */
7067 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7072 if (elf_dynsymtab (abfd
) == 0)
7074 bfd_set_error (bfd_error_invalid_operation
);
7078 ret
= sizeof (arelent
*);
7079 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7080 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7081 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7082 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7083 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7084 * sizeof (arelent
*));
7089 /* Canonicalize the dynamic relocation entries. Note that we return the
7090 dynamic relocations as a single block, although they are actually
7091 associated with particular sections; the interface, which was
7092 designed for SunOS style shared libraries, expects that there is only
7093 one set of dynamic relocs. Any loadable section that was actually
7094 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7095 dynamic symbol table, is considered to be a dynamic reloc section. */
7098 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7102 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7106 if (elf_dynsymtab (abfd
) == 0)
7108 bfd_set_error (bfd_error_invalid_operation
);
7112 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7114 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7116 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7117 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7118 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7123 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7125 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7127 for (i
= 0; i
< count
; i
++)
7138 /* Read in the version information. */
7141 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7143 bfd_byte
*contents
= NULL
;
7144 unsigned int freeidx
= 0;
7146 if (elf_dynverref (abfd
) != 0)
7148 Elf_Internal_Shdr
*hdr
;
7149 Elf_External_Verneed
*everneed
;
7150 Elf_Internal_Verneed
*iverneed
;
7152 bfd_byte
*contents_end
;
7154 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7156 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7157 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7158 if (elf_tdata (abfd
)->verref
== NULL
)
7161 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
7163 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7164 if (contents
== NULL
)
7166 error_return_verref
:
7167 elf_tdata (abfd
)->verref
= NULL
;
7168 elf_tdata (abfd
)->cverrefs
= 0;
7171 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7172 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7173 goto error_return_verref
;
7175 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7176 goto error_return_verref
;
7178 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7179 == sizeof (Elf_External_Vernaux
));
7180 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7181 everneed
= (Elf_External_Verneed
*) contents
;
7182 iverneed
= elf_tdata (abfd
)->verref
;
7183 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7185 Elf_External_Vernaux
*evernaux
;
7186 Elf_Internal_Vernaux
*ivernaux
;
7189 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7191 iverneed
->vn_bfd
= abfd
;
7193 iverneed
->vn_filename
=
7194 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7196 if (iverneed
->vn_filename
== NULL
)
7197 goto error_return_verref
;
7199 if (iverneed
->vn_cnt
== 0)
7200 iverneed
->vn_auxptr
= NULL
;
7203 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7204 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7205 sizeof (Elf_Internal_Vernaux
));
7206 if (iverneed
->vn_auxptr
== NULL
)
7207 goto error_return_verref
;
7210 if (iverneed
->vn_aux
7211 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7212 goto error_return_verref
;
7214 evernaux
= ((Elf_External_Vernaux
*)
7215 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7216 ivernaux
= iverneed
->vn_auxptr
;
7217 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7219 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7221 ivernaux
->vna_nodename
=
7222 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7223 ivernaux
->vna_name
);
7224 if (ivernaux
->vna_nodename
== NULL
)
7225 goto error_return_verref
;
7227 if (j
+ 1 < iverneed
->vn_cnt
)
7228 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7230 ivernaux
->vna_nextptr
= NULL
;
7232 if (ivernaux
->vna_next
7233 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7234 goto error_return_verref
;
7236 evernaux
= ((Elf_External_Vernaux
*)
7237 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7239 if (ivernaux
->vna_other
> freeidx
)
7240 freeidx
= ivernaux
->vna_other
;
7243 if (i
+ 1 < hdr
->sh_info
)
7244 iverneed
->vn_nextref
= iverneed
+ 1;
7246 iverneed
->vn_nextref
= NULL
;
7248 if (iverneed
->vn_next
7249 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7250 goto error_return_verref
;
7252 everneed
= ((Elf_External_Verneed
*)
7253 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7260 if (elf_dynverdef (abfd
) != 0)
7262 Elf_Internal_Shdr
*hdr
;
7263 Elf_External_Verdef
*everdef
;
7264 Elf_Internal_Verdef
*iverdef
;
7265 Elf_Internal_Verdef
*iverdefarr
;
7266 Elf_Internal_Verdef iverdefmem
;
7268 unsigned int maxidx
;
7269 bfd_byte
*contents_end_def
, *contents_end_aux
;
7271 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7273 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7274 if (contents
== NULL
)
7276 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7277 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7280 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7283 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7284 >= sizeof (Elf_External_Verdaux
));
7285 contents_end_def
= contents
+ hdr
->sh_size
7286 - sizeof (Elf_External_Verdef
);
7287 contents_end_aux
= contents
+ hdr
->sh_size
7288 - sizeof (Elf_External_Verdaux
);
7290 /* We know the number of entries in the section but not the maximum
7291 index. Therefore we have to run through all entries and find
7293 everdef
= (Elf_External_Verdef
*) contents
;
7295 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7297 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7299 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7300 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7302 if (iverdefmem
.vd_next
7303 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7306 everdef
= ((Elf_External_Verdef
*)
7307 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7310 if (default_imported_symver
)
7312 if (freeidx
> maxidx
)
7317 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7318 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7319 if (elf_tdata (abfd
)->verdef
== NULL
)
7322 elf_tdata (abfd
)->cverdefs
= maxidx
;
7324 everdef
= (Elf_External_Verdef
*) contents
;
7325 iverdefarr
= elf_tdata (abfd
)->verdef
;
7326 for (i
= 0; i
< hdr
->sh_info
; i
++)
7328 Elf_External_Verdaux
*everdaux
;
7329 Elf_Internal_Verdaux
*iverdaux
;
7332 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7334 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7336 error_return_verdef
:
7337 elf_tdata (abfd
)->verdef
= NULL
;
7338 elf_tdata (abfd
)->cverdefs
= 0;
7342 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7343 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7345 iverdef
->vd_bfd
= abfd
;
7347 if (iverdef
->vd_cnt
== 0)
7348 iverdef
->vd_auxptr
= NULL
;
7351 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7352 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7353 sizeof (Elf_Internal_Verdaux
));
7354 if (iverdef
->vd_auxptr
== NULL
)
7355 goto error_return_verdef
;
7359 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7360 goto error_return_verdef
;
7362 everdaux
= ((Elf_External_Verdaux
*)
7363 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7364 iverdaux
= iverdef
->vd_auxptr
;
7365 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7367 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7369 iverdaux
->vda_nodename
=
7370 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7371 iverdaux
->vda_name
);
7372 if (iverdaux
->vda_nodename
== NULL
)
7373 goto error_return_verdef
;
7375 if (j
+ 1 < iverdef
->vd_cnt
)
7376 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7378 iverdaux
->vda_nextptr
= NULL
;
7380 if (iverdaux
->vda_next
7381 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7382 goto error_return_verdef
;
7384 everdaux
= ((Elf_External_Verdaux
*)
7385 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7388 if (iverdef
->vd_cnt
)
7389 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7391 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7392 iverdef
->vd_nextdef
= iverdef
+ 1;
7394 iverdef
->vd_nextdef
= NULL
;
7396 everdef
= ((Elf_External_Verdef
*)
7397 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7403 else if (default_imported_symver
)
7410 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7411 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7412 if (elf_tdata (abfd
)->verdef
== NULL
)
7415 elf_tdata (abfd
)->cverdefs
= freeidx
;
7418 /* Create a default version based on the soname. */
7419 if (default_imported_symver
)
7421 Elf_Internal_Verdef
*iverdef
;
7422 Elf_Internal_Verdaux
*iverdaux
;
7424 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7426 iverdef
->vd_version
= VER_DEF_CURRENT
;
7427 iverdef
->vd_flags
= 0;
7428 iverdef
->vd_ndx
= freeidx
;
7429 iverdef
->vd_cnt
= 1;
7431 iverdef
->vd_bfd
= abfd
;
7433 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7434 if (iverdef
->vd_nodename
== NULL
)
7435 goto error_return_verdef
;
7436 iverdef
->vd_nextdef
= NULL
;
7437 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7438 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7439 if (iverdef
->vd_auxptr
== NULL
)
7440 goto error_return_verdef
;
7442 iverdaux
= iverdef
->vd_auxptr
;
7443 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7444 iverdaux
->vda_nextptr
= NULL
;
7450 if (contents
!= NULL
)
7456 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7458 elf_symbol_type
*newsym
;
7459 bfd_size_type amt
= sizeof (elf_symbol_type
);
7461 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7466 newsym
->symbol
.the_bfd
= abfd
;
7467 return &newsym
->symbol
;
7472 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7476 bfd_symbol_info (symbol
, ret
);
7479 /* Return whether a symbol name implies a local symbol. Most targets
7480 use this function for the is_local_label_name entry point, but some
7484 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7487 /* Normal local symbols start with ``.L''. */
7488 if (name
[0] == '.' && name
[1] == 'L')
7491 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7492 DWARF debugging symbols starting with ``..''. */
7493 if (name
[0] == '.' && name
[1] == '.')
7496 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7497 emitting DWARF debugging output. I suspect this is actually a
7498 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7499 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7500 underscore to be emitted on some ELF targets). For ease of use,
7501 we treat such symbols as local. */
7502 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7509 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7510 asymbol
*symbol ATTRIBUTE_UNUSED
)
7517 _bfd_elf_set_arch_mach (bfd
*abfd
,
7518 enum bfd_architecture arch
,
7519 unsigned long machine
)
7521 /* If this isn't the right architecture for this backend, and this
7522 isn't the generic backend, fail. */
7523 if (arch
!= get_elf_backend_data (abfd
)->arch
7524 && arch
!= bfd_arch_unknown
7525 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7528 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7531 /* Find the function to a particular section and offset,
7532 for error reporting. */
7535 elf_find_function (bfd
*abfd
,
7539 const char **filename_ptr
,
7540 const char **functionname_ptr
)
7542 struct elf_find_function_cache
7544 asection
*last_section
;
7546 const char *filename
;
7547 bfd_size_type func_size
;
7550 if (symbols
== NULL
)
7553 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
7556 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
7557 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
7561 if (cache
->last_section
!= section
7562 || cache
->func
== NULL
7563 || offset
< cache
->func
->value
7564 || offset
>= cache
->func
->value
+ cache
->func_size
)
7569 /* ??? Given multiple file symbols, it is impossible to reliably
7570 choose the right file name for global symbols. File symbols are
7571 local symbols, and thus all file symbols must sort before any
7572 global symbols. The ELF spec may be interpreted to say that a
7573 file symbol must sort before other local symbols, but currently
7574 ld -r doesn't do this. So, for ld -r output, it is possible to
7575 make a better choice of file name for local symbols by ignoring
7576 file symbols appearing after a given local symbol. */
7577 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7578 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7582 state
= nothing_seen
;
7583 cache
->filename
= NULL
;
7585 cache
->func_size
= 0;
7586 cache
->last_section
= section
;
7588 for (p
= symbols
; *p
!= NULL
; p
++)
7594 if ((sym
->flags
& BSF_FILE
) != 0)
7597 if (state
== symbol_seen
)
7598 state
= file_after_symbol_seen
;
7602 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
7604 && code_off
<= offset
7605 && (code_off
> low_func
7606 || (code_off
== low_func
7607 && size
> cache
->func_size
)))
7610 cache
->func_size
= size
;
7611 cache
->filename
= NULL
;
7612 low_func
= code_off
;
7614 && ((sym
->flags
& BSF_LOCAL
) != 0
7615 || state
!= file_after_symbol_seen
))
7616 cache
->filename
= bfd_asymbol_name (file
);
7618 if (state
== nothing_seen
)
7619 state
= symbol_seen
;
7623 if (cache
->func
== NULL
)
7627 *filename_ptr
= cache
->filename
;
7628 if (functionname_ptr
)
7629 *functionname_ptr
= bfd_asymbol_name (cache
->func
);
7634 /* Find the nearest line to a particular section and offset,
7635 for error reporting. */
7638 _bfd_elf_find_nearest_line (bfd
*abfd
,
7642 const char **filename_ptr
,
7643 const char **functionname_ptr
,
7644 unsigned int *line_ptr
)
7646 return _bfd_elf_find_nearest_line_discriminator (abfd
, section
, symbols
,
7647 offset
, filename_ptr
,
7654 _bfd_elf_find_nearest_line_discriminator (bfd
*abfd
,
7658 const char **filename_ptr
,
7659 const char **functionname_ptr
,
7660 unsigned int *line_ptr
,
7661 unsigned int *discriminator_ptr
)
7665 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7666 filename_ptr
, functionname_ptr
,
7669 if (!*functionname_ptr
)
7670 elf_find_function (abfd
, section
, symbols
, offset
,
7671 *filename_ptr
? NULL
: filename_ptr
,
7677 if (_bfd_dwarf2_find_nearest_line (abfd
, dwarf_debug_sections
,
7678 section
, symbols
, offset
,
7679 filename_ptr
, functionname_ptr
,
7680 line_ptr
, discriminator_ptr
, 0,
7681 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7683 if (!*functionname_ptr
)
7684 elf_find_function (abfd
, section
, symbols
, offset
,
7685 *filename_ptr
? NULL
: filename_ptr
,
7691 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7692 &found
, filename_ptr
,
7693 functionname_ptr
, line_ptr
,
7694 &elf_tdata (abfd
)->line_info
))
7696 if (found
&& (*functionname_ptr
|| *line_ptr
))
7699 if (symbols
== NULL
)
7702 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7703 filename_ptr
, functionname_ptr
))
7710 /* Find the line for a symbol. */
7713 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7714 const char **filename_ptr
, unsigned int *line_ptr
)
7716 return _bfd_elf_find_line_discriminator (abfd
, symbols
, symbol
,
7717 filename_ptr
, line_ptr
,
7722 _bfd_elf_find_line_discriminator (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7723 const char **filename_ptr
,
7724 unsigned int *line_ptr
,
7725 unsigned int *discriminator_ptr
)
7727 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7728 filename_ptr
, line_ptr
, discriminator_ptr
, 0,
7729 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7732 /* After a call to bfd_find_nearest_line, successive calls to
7733 bfd_find_inliner_info can be used to get source information about
7734 each level of function inlining that terminated at the address
7735 passed to bfd_find_nearest_line. Currently this is only supported
7736 for DWARF2 with appropriate DWARF3 extensions. */
7739 _bfd_elf_find_inliner_info (bfd
*abfd
,
7740 const char **filename_ptr
,
7741 const char **functionname_ptr
,
7742 unsigned int *line_ptr
)
7745 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7746 functionname_ptr
, line_ptr
,
7747 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7752 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7754 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7755 int ret
= bed
->s
->sizeof_ehdr
;
7757 if (!info
->relocatable
)
7759 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
7761 if (phdr_size
== (bfd_size_type
) -1)
7763 struct elf_segment_map
*m
;
7766 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
7767 phdr_size
+= bed
->s
->sizeof_phdr
;
7770 phdr_size
= get_program_header_size (abfd
, info
);
7773 elf_program_header_size (abfd
) = phdr_size
;
7781 _bfd_elf_set_section_contents (bfd
*abfd
,
7783 const void *location
,
7785 bfd_size_type count
)
7787 Elf_Internal_Shdr
*hdr
;
7790 if (! abfd
->output_has_begun
7791 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7794 hdr
= &elf_section_data (section
)->this_hdr
;
7795 pos
= hdr
->sh_offset
+ offset
;
7796 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7797 || bfd_bwrite (location
, count
, abfd
) != count
)
7804 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7805 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7806 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7811 /* Try to convert a non-ELF reloc into an ELF one. */
7814 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7816 /* Check whether we really have an ELF howto. */
7818 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7820 bfd_reloc_code_real_type code
;
7821 reloc_howto_type
*howto
;
7823 /* Alien reloc: Try to determine its type to replace it with an
7824 equivalent ELF reloc. */
7826 if (areloc
->howto
->pc_relative
)
7828 switch (areloc
->howto
->bitsize
)
7831 code
= BFD_RELOC_8_PCREL
;
7834 code
= BFD_RELOC_12_PCREL
;
7837 code
= BFD_RELOC_16_PCREL
;
7840 code
= BFD_RELOC_24_PCREL
;
7843 code
= BFD_RELOC_32_PCREL
;
7846 code
= BFD_RELOC_64_PCREL
;
7852 howto
= bfd_reloc_type_lookup (abfd
, code
);
7854 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7856 if (howto
->pcrel_offset
)
7857 areloc
->addend
+= areloc
->address
;
7859 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7864 switch (areloc
->howto
->bitsize
)
7870 code
= BFD_RELOC_14
;
7873 code
= BFD_RELOC_16
;
7876 code
= BFD_RELOC_26
;
7879 code
= BFD_RELOC_32
;
7882 code
= BFD_RELOC_64
;
7888 howto
= bfd_reloc_type_lookup (abfd
, code
);
7892 areloc
->howto
= howto
;
7900 (*_bfd_error_handler
)
7901 (_("%B: unsupported relocation type %s"),
7902 abfd
, areloc
->howto
->name
);
7903 bfd_set_error (bfd_error_bad_value
);
7908 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7910 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
7911 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
7913 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
7914 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7915 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
7918 return _bfd_generic_close_and_cleanup (abfd
);
7921 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7922 in the relocation's offset. Thus we cannot allow any sort of sanity
7923 range-checking to interfere. There is nothing else to do in processing
7926 bfd_reloc_status_type
7927 _bfd_elf_rel_vtable_reloc_fn
7928 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7929 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7930 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7931 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7933 return bfd_reloc_ok
;
7936 /* Elf core file support. Much of this only works on native
7937 toolchains, since we rely on knowing the
7938 machine-dependent procfs structure in order to pick
7939 out details about the corefile. */
7941 #ifdef HAVE_SYS_PROCFS_H
7942 /* Needed for new procfs interface on sparc-solaris. */
7943 # define _STRUCTURED_PROC 1
7944 # include <sys/procfs.h>
7947 /* Return a PID that identifies a "thread" for threaded cores, or the
7948 PID of the main process for non-threaded cores. */
7951 elfcore_make_pid (bfd
*abfd
)
7955 pid
= elf_tdata (abfd
)->core
->lwpid
;
7957 pid
= elf_tdata (abfd
)->core
->pid
;
7962 /* If there isn't a section called NAME, make one, using
7963 data from SECT. Note, this function will generate a
7964 reference to NAME, so you shouldn't deallocate or
7968 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7972 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7975 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7979 sect2
->size
= sect
->size
;
7980 sect2
->filepos
= sect
->filepos
;
7981 sect2
->alignment_power
= sect
->alignment_power
;
7985 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7986 actually creates up to two pseudosections:
7987 - For the single-threaded case, a section named NAME, unless
7988 such a section already exists.
7989 - For the multi-threaded case, a section named "NAME/PID", where
7990 PID is elfcore_make_pid (abfd).
7991 Both pseudosections have identical contents. */
7993 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7999 char *threaded_name
;
8003 /* Build the section name. */
8005 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
8006 len
= strlen (buf
) + 1;
8007 threaded_name
= (char *) bfd_alloc (abfd
, len
);
8008 if (threaded_name
== NULL
)
8010 memcpy (threaded_name
, buf
, len
);
8012 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
8017 sect
->filepos
= filepos
;
8018 sect
->alignment_power
= 2;
8020 return elfcore_maybe_make_sect (abfd
, name
, sect
);
8023 /* prstatus_t exists on:
8025 linux 2.[01] + glibc
8029 #if defined (HAVE_PRSTATUS_T)
8032 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8037 if (note
->descsz
== sizeof (prstatus_t
))
8041 size
= sizeof (prstat
.pr_reg
);
8042 offset
= offsetof (prstatus_t
, pr_reg
);
8043 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8045 /* Do not overwrite the core signal if it
8046 has already been set by another thread. */
8047 if (elf_tdata (abfd
)->core
->signal
== 0)
8048 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8049 if (elf_tdata (abfd
)->core
->pid
== 0)
8050 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8052 /* pr_who exists on:
8055 pr_who doesn't exist on:
8058 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8059 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8061 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8064 #if defined (HAVE_PRSTATUS32_T)
8065 else if (note
->descsz
== sizeof (prstatus32_t
))
8067 /* 64-bit host, 32-bit corefile */
8068 prstatus32_t prstat
;
8070 size
= sizeof (prstat
.pr_reg
);
8071 offset
= offsetof (prstatus32_t
, pr_reg
);
8072 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8074 /* Do not overwrite the core signal if it
8075 has already been set by another thread. */
8076 if (elf_tdata (abfd
)->core
->signal
== 0)
8077 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8078 if (elf_tdata (abfd
)->core
->pid
== 0)
8079 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8081 /* pr_who exists on:
8084 pr_who doesn't exist on:
8087 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8088 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8090 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8093 #endif /* HAVE_PRSTATUS32_T */
8096 /* Fail - we don't know how to handle any other
8097 note size (ie. data object type). */
8101 /* Make a ".reg/999" section and a ".reg" section. */
8102 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8103 size
, note
->descpos
+ offset
);
8105 #endif /* defined (HAVE_PRSTATUS_T) */
8107 /* Create a pseudosection containing the exact contents of NOTE. */
8109 elfcore_make_note_pseudosection (bfd
*abfd
,
8111 Elf_Internal_Note
*note
)
8113 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8114 note
->descsz
, note
->descpos
);
8117 /* There isn't a consistent prfpregset_t across platforms,
8118 but it doesn't matter, because we don't have to pick this
8119 data structure apart. */
8122 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8124 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8127 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8128 type of NT_PRXFPREG. Just include the whole note's contents
8132 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8134 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8137 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8138 with a note type of NT_X86_XSTATE. Just include the whole note's
8139 contents literally. */
8142 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8144 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8148 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8150 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8154 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8156 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8160 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8162 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8166 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8168 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8172 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8174 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8178 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8180 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8184 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8186 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8190 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8192 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8196 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8198 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8202 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8204 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8208 elfcore_grok_s390_tdb (bfd
*abfd
, Elf_Internal_Note
*note
)
8210 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-tdb", note
);
8214 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8216 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8220 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8222 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8226 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8228 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8232 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8234 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8237 #if defined (HAVE_PRPSINFO_T)
8238 typedef prpsinfo_t elfcore_psinfo_t
;
8239 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8240 typedef prpsinfo32_t elfcore_psinfo32_t
;
8244 #if defined (HAVE_PSINFO_T)
8245 typedef psinfo_t elfcore_psinfo_t
;
8246 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8247 typedef psinfo32_t elfcore_psinfo32_t
;
8251 /* return a malloc'ed copy of a string at START which is at
8252 most MAX bytes long, possibly without a terminating '\0'.
8253 the copy will always have a terminating '\0'. */
8256 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8259 char *end
= (char *) memchr (start
, '\0', max
);
8267 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8271 memcpy (dups
, start
, len
);
8277 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8279 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8281 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8283 elfcore_psinfo_t psinfo
;
8285 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8287 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8288 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8290 elf_tdata (abfd
)->core
->program
8291 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8292 sizeof (psinfo
.pr_fname
));
8294 elf_tdata (abfd
)->core
->command
8295 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8296 sizeof (psinfo
.pr_psargs
));
8298 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8299 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8301 /* 64-bit host, 32-bit corefile */
8302 elfcore_psinfo32_t psinfo
;
8304 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8306 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8307 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8309 elf_tdata (abfd
)->core
->program
8310 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8311 sizeof (psinfo
.pr_fname
));
8313 elf_tdata (abfd
)->core
->command
8314 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8315 sizeof (psinfo
.pr_psargs
));
8321 /* Fail - we don't know how to handle any other
8322 note size (ie. data object type). */
8326 /* Note that for some reason, a spurious space is tacked
8327 onto the end of the args in some (at least one anyway)
8328 implementations, so strip it off if it exists. */
8331 char *command
= elf_tdata (abfd
)->core
->command
;
8332 int n
= strlen (command
);
8334 if (0 < n
&& command
[n
- 1] == ' ')
8335 command
[n
- 1] = '\0';
8340 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8342 #if defined (HAVE_PSTATUS_T)
8344 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8346 if (note
->descsz
== sizeof (pstatus_t
)
8347 #if defined (HAVE_PXSTATUS_T)
8348 || note
->descsz
== sizeof (pxstatus_t
)
8354 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8356 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8358 #if defined (HAVE_PSTATUS32_T)
8359 else if (note
->descsz
== sizeof (pstatus32_t
))
8361 /* 64-bit host, 32-bit corefile */
8364 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8366 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8369 /* Could grab some more details from the "representative"
8370 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8371 NT_LWPSTATUS note, presumably. */
8375 #endif /* defined (HAVE_PSTATUS_T) */
8377 #if defined (HAVE_LWPSTATUS_T)
8379 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8381 lwpstatus_t lwpstat
;
8387 if (note
->descsz
!= sizeof (lwpstat
)
8388 #if defined (HAVE_LWPXSTATUS_T)
8389 && note
->descsz
!= sizeof (lwpxstatus_t
)
8394 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8396 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8397 /* Do not overwrite the core signal if it has already been set by
8399 if (elf_tdata (abfd
)->core
->signal
== 0)
8400 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8402 /* Make a ".reg/999" section. */
8404 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8405 len
= strlen (buf
) + 1;
8406 name
= bfd_alloc (abfd
, len
);
8409 memcpy (name
, buf
, len
);
8411 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8415 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8416 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8417 sect
->filepos
= note
->descpos
8418 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8421 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8422 sect
->size
= sizeof (lwpstat
.pr_reg
);
8423 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8426 sect
->alignment_power
= 2;
8428 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8431 /* Make a ".reg2/999" section */
8433 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8434 len
= strlen (buf
) + 1;
8435 name
= bfd_alloc (abfd
, len
);
8438 memcpy (name
, buf
, len
);
8440 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8444 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8445 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8446 sect
->filepos
= note
->descpos
8447 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8450 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8451 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8452 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8455 sect
->alignment_power
= 2;
8457 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8459 #endif /* defined (HAVE_LWPSTATUS_T) */
8462 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8469 int is_active_thread
;
8472 if (note
->descsz
< 728)
8475 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8478 type
= bfd_get_32 (abfd
, note
->descdata
);
8482 case 1 /* NOTE_INFO_PROCESS */:
8483 /* FIXME: need to add ->core->command. */
8484 /* process_info.pid */
8485 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8486 /* process_info.signal */
8487 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8490 case 2 /* NOTE_INFO_THREAD */:
8491 /* Make a ".reg/999" section. */
8492 /* thread_info.tid */
8493 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8495 len
= strlen (buf
) + 1;
8496 name
= (char *) bfd_alloc (abfd
, len
);
8500 memcpy (name
, buf
, len
);
8502 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8506 /* sizeof (thread_info.thread_context) */
8508 /* offsetof (thread_info.thread_context) */
8509 sect
->filepos
= note
->descpos
+ 12;
8510 sect
->alignment_power
= 2;
8512 /* thread_info.is_active_thread */
8513 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8515 if (is_active_thread
)
8516 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8520 case 3 /* NOTE_INFO_MODULE */:
8521 /* Make a ".module/xxxxxxxx" section. */
8522 /* module_info.base_address */
8523 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8524 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8526 len
= strlen (buf
) + 1;
8527 name
= (char *) bfd_alloc (abfd
, len
);
8531 memcpy (name
, buf
, len
);
8533 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8538 sect
->size
= note
->descsz
;
8539 sect
->filepos
= note
->descpos
;
8540 sect
->alignment_power
= 2;
8551 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8553 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8561 if (bed
->elf_backend_grok_prstatus
)
8562 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8564 #if defined (HAVE_PRSTATUS_T)
8565 return elfcore_grok_prstatus (abfd
, note
);
8570 #if defined (HAVE_PSTATUS_T)
8572 return elfcore_grok_pstatus (abfd
, note
);
8575 #if defined (HAVE_LWPSTATUS_T)
8577 return elfcore_grok_lwpstatus (abfd
, note
);
8580 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8581 return elfcore_grok_prfpreg (abfd
, note
);
8583 case NT_WIN32PSTATUS
:
8584 return elfcore_grok_win32pstatus (abfd
, note
);
8586 case NT_PRXFPREG
: /* Linux SSE extension */
8587 if (note
->namesz
== 6
8588 && strcmp (note
->namedata
, "LINUX") == 0)
8589 return elfcore_grok_prxfpreg (abfd
, note
);
8593 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8594 if (note
->namesz
== 6
8595 && strcmp (note
->namedata
, "LINUX") == 0)
8596 return elfcore_grok_xstatereg (abfd
, note
);
8601 if (note
->namesz
== 6
8602 && strcmp (note
->namedata
, "LINUX") == 0)
8603 return elfcore_grok_ppc_vmx (abfd
, note
);
8608 if (note
->namesz
== 6
8609 && strcmp (note
->namedata
, "LINUX") == 0)
8610 return elfcore_grok_ppc_vsx (abfd
, note
);
8614 case NT_S390_HIGH_GPRS
:
8615 if (note
->namesz
== 6
8616 && strcmp (note
->namedata
, "LINUX") == 0)
8617 return elfcore_grok_s390_high_gprs (abfd
, note
);
8622 if (note
->namesz
== 6
8623 && strcmp (note
->namedata
, "LINUX") == 0)
8624 return elfcore_grok_s390_timer (abfd
, note
);
8628 case NT_S390_TODCMP
:
8629 if (note
->namesz
== 6
8630 && strcmp (note
->namedata
, "LINUX") == 0)
8631 return elfcore_grok_s390_todcmp (abfd
, note
);
8635 case NT_S390_TODPREG
:
8636 if (note
->namesz
== 6
8637 && strcmp (note
->namedata
, "LINUX") == 0)
8638 return elfcore_grok_s390_todpreg (abfd
, note
);
8643 if (note
->namesz
== 6
8644 && strcmp (note
->namedata
, "LINUX") == 0)
8645 return elfcore_grok_s390_ctrs (abfd
, note
);
8649 case NT_S390_PREFIX
:
8650 if (note
->namesz
== 6
8651 && strcmp (note
->namedata
, "LINUX") == 0)
8652 return elfcore_grok_s390_prefix (abfd
, note
);
8656 case NT_S390_LAST_BREAK
:
8657 if (note
->namesz
== 6
8658 && strcmp (note
->namedata
, "LINUX") == 0)
8659 return elfcore_grok_s390_last_break (abfd
, note
);
8663 case NT_S390_SYSTEM_CALL
:
8664 if (note
->namesz
== 6
8665 && strcmp (note
->namedata
, "LINUX") == 0)
8666 return elfcore_grok_s390_system_call (abfd
, note
);
8671 if (note
->namesz
== 6
8672 && strcmp (note
->namedata
, "LINUX") == 0)
8673 return elfcore_grok_s390_tdb (abfd
, note
);
8678 if (note
->namesz
== 6
8679 && strcmp (note
->namedata
, "LINUX") == 0)
8680 return elfcore_grok_arm_vfp (abfd
, note
);
8685 if (note
->namesz
== 6
8686 && strcmp (note
->namedata
, "LINUX") == 0)
8687 return elfcore_grok_aarch_tls (abfd
, note
);
8691 case NT_ARM_HW_BREAK
:
8692 if (note
->namesz
== 6
8693 && strcmp (note
->namedata
, "LINUX") == 0)
8694 return elfcore_grok_aarch_hw_break (abfd
, note
);
8698 case NT_ARM_HW_WATCH
:
8699 if (note
->namesz
== 6
8700 && strcmp (note
->namedata
, "LINUX") == 0)
8701 return elfcore_grok_aarch_hw_watch (abfd
, note
);
8707 if (bed
->elf_backend_grok_psinfo
)
8708 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8710 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8711 return elfcore_grok_psinfo (abfd
, note
);
8718 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8723 sect
->size
= note
->descsz
;
8724 sect
->filepos
= note
->descpos
;
8725 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8731 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
8735 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
8741 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8743 struct elf_obj_tdata
*t
;
8745 if (note
->descsz
== 0)
8748 t
= elf_tdata (abfd
);
8749 t
->build_id
= bfd_alloc (abfd
, sizeof (*t
->build_id
) - 1 + note
->descsz
);
8750 if (t
->build_id
== NULL
)
8753 t
->build_id
->size
= note
->descsz
;
8754 memcpy (t
->build_id
->data
, note
->descdata
, note
->descsz
);
8760 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8767 case NT_GNU_BUILD_ID
:
8768 return elfobj_grok_gnu_build_id (abfd
, note
);
8773 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
8775 struct sdt_note
*cur
=
8776 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
8779 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
8780 cur
->size
= (bfd_size_type
) note
->descsz
;
8781 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
8783 elf_tdata (abfd
)->sdt_note_head
= cur
;
8789 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8794 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
8802 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8806 cp
= strchr (note
->namedata
, '@');
8809 *lwpidp
= atoi(cp
+ 1);
8816 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8818 /* Signal number at offset 0x08. */
8819 elf_tdata (abfd
)->core
->signal
8820 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8822 /* Process ID at offset 0x50. */
8823 elf_tdata (abfd
)->core
->pid
8824 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8826 /* Command name at 0x7c (max 32 bytes, including nul). */
8827 elf_tdata (abfd
)->core
->command
8828 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8830 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8835 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8839 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8840 elf_tdata (abfd
)->core
->lwpid
= lwp
;
8842 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8844 /* NetBSD-specific core "procinfo". Note that we expect to
8845 find this note before any of the others, which is fine,
8846 since the kernel writes this note out first when it
8847 creates a core file. */
8849 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8852 /* As of Jan 2002 there are no other machine-independent notes
8853 defined for NetBSD core files. If the note type is less
8854 than the start of the machine-dependent note types, we don't
8857 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8861 switch (bfd_get_arch (abfd
))
8863 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8864 PT_GETFPREGS == mach+2. */
8866 case bfd_arch_alpha
:
8867 case bfd_arch_sparc
:
8870 case NT_NETBSDCORE_FIRSTMACH
+0:
8871 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8873 case NT_NETBSDCORE_FIRSTMACH
+2:
8874 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8880 /* On all other arch's, PT_GETREGS == mach+1 and
8881 PT_GETFPREGS == mach+3. */
8886 case NT_NETBSDCORE_FIRSTMACH
+1:
8887 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8889 case NT_NETBSDCORE_FIRSTMACH
+3:
8890 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8900 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8902 /* Signal number at offset 0x08. */
8903 elf_tdata (abfd
)->core
->signal
8904 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8906 /* Process ID at offset 0x20. */
8907 elf_tdata (abfd
)->core
->pid
8908 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
8910 /* Command name at 0x48 (max 32 bytes, including nul). */
8911 elf_tdata (abfd
)->core
->command
8912 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
8918 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8920 if (note
->type
== NT_OPENBSD_PROCINFO
)
8921 return elfcore_grok_openbsd_procinfo (abfd
, note
);
8923 if (note
->type
== NT_OPENBSD_REGS
)
8924 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8926 if (note
->type
== NT_OPENBSD_FPREGS
)
8927 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8929 if (note
->type
== NT_OPENBSD_XFPREGS
)
8930 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8932 if (note
->type
== NT_OPENBSD_AUXV
)
8934 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8939 sect
->size
= note
->descsz
;
8940 sect
->filepos
= note
->descpos
;
8941 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8946 if (note
->type
== NT_OPENBSD_WCOOKIE
)
8948 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
8953 sect
->size
= note
->descsz
;
8954 sect
->filepos
= note
->descpos
;
8955 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8964 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8966 void *ddata
= note
->descdata
;
8973 /* nto_procfs_status 'pid' field is at offset 0. */
8974 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8976 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8977 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8979 /* nto_procfs_status 'flags' field is at offset 8. */
8980 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8982 /* nto_procfs_status 'what' field is at offset 14. */
8983 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8985 elf_tdata (abfd
)->core
->signal
= sig
;
8986 elf_tdata (abfd
)->core
->lwpid
= *tid
;
8989 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8990 do not come from signals so we make sure we set the current
8991 thread just in case. */
8992 if (flags
& 0x00000080)
8993 elf_tdata (abfd
)->core
->lwpid
= *tid
;
8995 /* Make a ".qnx_core_status/%d" section. */
8996 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8998 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9003 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9007 sect
->size
= note
->descsz
;
9008 sect
->filepos
= note
->descpos
;
9009 sect
->alignment_power
= 2;
9011 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
9015 elfcore_grok_nto_regs (bfd
*abfd
,
9016 Elf_Internal_Note
*note
,
9024 /* Make a "(base)/%d" section. */
9025 sprintf (buf
, "%s/%ld", base
, tid
);
9027 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
9032 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9036 sect
->size
= note
->descsz
;
9037 sect
->filepos
= note
->descpos
;
9038 sect
->alignment_power
= 2;
9040 /* This is the current thread. */
9041 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
9042 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9047 #define BFD_QNT_CORE_INFO 7
9048 #define BFD_QNT_CORE_STATUS 8
9049 #define BFD_QNT_CORE_GREG 9
9050 #define BFD_QNT_CORE_FPREG 10
9053 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9055 /* Every GREG section has a STATUS section before it. Store the
9056 tid from the previous call to pass down to the next gregs
9058 static long tid
= 1;
9062 case BFD_QNT_CORE_INFO
:
9063 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9064 case BFD_QNT_CORE_STATUS
:
9065 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9066 case BFD_QNT_CORE_GREG
:
9067 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9068 case BFD_QNT_CORE_FPREG
:
9069 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9076 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9082 /* Use note name as section name. */
9084 name
= (char *) bfd_alloc (abfd
, len
);
9087 memcpy (name
, note
->namedata
, len
);
9088 name
[len
- 1] = '\0';
9090 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9094 sect
->size
= note
->descsz
;
9095 sect
->filepos
= note
->descpos
;
9096 sect
->alignment_power
= 1;
9101 /* Function: elfcore_write_note
9104 buffer to hold note, and current size of buffer
9108 size of data for note
9110 Writes note to end of buffer. ELF64 notes are written exactly as
9111 for ELF32, despite the current (as of 2006) ELF gabi specifying
9112 that they ought to have 8-byte namesz and descsz field, and have
9113 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9116 Pointer to realloc'd buffer, *BUFSIZ updated. */
9119 elfcore_write_note (bfd
*abfd
,
9127 Elf_External_Note
*xnp
;
9134 namesz
= strlen (name
) + 1;
9136 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9138 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9141 dest
= buf
+ *bufsiz
;
9142 *bufsiz
+= newspace
;
9143 xnp
= (Elf_External_Note
*) dest
;
9144 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9145 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9146 H_PUT_32 (abfd
, type
, xnp
->type
);
9150 memcpy (dest
, name
, namesz
);
9158 memcpy (dest
, input
, size
);
9169 elfcore_write_prpsinfo (bfd
*abfd
,
9175 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9177 if (bed
->elf_backend_write_core_note
!= NULL
)
9180 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9181 NT_PRPSINFO
, fname
, psargs
);
9186 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9187 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9188 if (bed
->s
->elfclass
== ELFCLASS32
)
9190 #if defined (HAVE_PSINFO32_T)
9192 int note_type
= NT_PSINFO
;
9195 int note_type
= NT_PRPSINFO
;
9198 memset (&data
, 0, sizeof (data
));
9199 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9200 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9201 return elfcore_write_note (abfd
, buf
, bufsiz
,
9202 "CORE", note_type
, &data
, sizeof (data
));
9207 #if defined (HAVE_PSINFO_T)
9209 int note_type
= NT_PSINFO
;
9212 int note_type
= NT_PRPSINFO
;
9215 memset (&data
, 0, sizeof (data
));
9216 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9217 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9218 return elfcore_write_note (abfd
, buf
, bufsiz
,
9219 "CORE", note_type
, &data
, sizeof (data
));
9221 #endif /* PSINFO_T or PRPSINFO_T */
9228 elfcore_write_linux_prpsinfo32
9229 (bfd
*abfd
, char *buf
, int *bufsiz
,
9230 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9232 struct elf_external_linux_prpsinfo32 data
;
9234 memset (&data
, 0, sizeof (data
));
9235 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9237 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9238 &data
, sizeof (data
));
9242 elfcore_write_linux_prpsinfo64
9243 (bfd
*abfd
, char *buf
, int *bufsiz
,
9244 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9246 struct elf_external_linux_prpsinfo64 data
;
9248 memset (&data
, 0, sizeof (data
));
9249 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9251 return elfcore_write_note (abfd
, buf
, bufsiz
,
9252 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9256 elfcore_write_prstatus (bfd
*abfd
,
9263 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9265 if (bed
->elf_backend_write_core_note
!= NULL
)
9268 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9270 pid
, cursig
, gregs
);
9275 #if defined (HAVE_PRSTATUS_T)
9276 #if defined (HAVE_PRSTATUS32_T)
9277 if (bed
->s
->elfclass
== ELFCLASS32
)
9279 prstatus32_t prstat
;
9281 memset (&prstat
, 0, sizeof (prstat
));
9282 prstat
.pr_pid
= pid
;
9283 prstat
.pr_cursig
= cursig
;
9284 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9285 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9286 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9293 memset (&prstat
, 0, sizeof (prstat
));
9294 prstat
.pr_pid
= pid
;
9295 prstat
.pr_cursig
= cursig
;
9296 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9297 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9298 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9300 #endif /* HAVE_PRSTATUS_T */
9306 #if defined (HAVE_LWPSTATUS_T)
9308 elfcore_write_lwpstatus (bfd
*abfd
,
9315 lwpstatus_t lwpstat
;
9316 const char *note_name
= "CORE";
9318 memset (&lwpstat
, 0, sizeof (lwpstat
));
9319 lwpstat
.pr_lwpid
= pid
>> 16;
9320 lwpstat
.pr_cursig
= cursig
;
9321 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9322 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9323 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9325 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9326 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9328 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9329 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9332 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9333 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9335 #endif /* HAVE_LWPSTATUS_T */
9337 #if defined (HAVE_PSTATUS_T)
9339 elfcore_write_pstatus (bfd
*abfd
,
9343 int cursig ATTRIBUTE_UNUSED
,
9344 const void *gregs ATTRIBUTE_UNUSED
)
9346 const char *note_name
= "CORE";
9347 #if defined (HAVE_PSTATUS32_T)
9348 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9350 if (bed
->s
->elfclass
== ELFCLASS32
)
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
));
9365 memset (&pstat
, 0, sizeof (pstat
));
9366 pstat
.pr_pid
= pid
& 0xffff;
9367 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9368 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9372 #endif /* HAVE_PSTATUS_T */
9375 elfcore_write_prfpreg (bfd
*abfd
,
9381 const char *note_name
= "CORE";
9382 return elfcore_write_note (abfd
, buf
, bufsiz
,
9383 note_name
, NT_FPREGSET
, fpregs
, size
);
9387 elfcore_write_prxfpreg (bfd
*abfd
,
9390 const void *xfpregs
,
9393 char *note_name
= "LINUX";
9394 return elfcore_write_note (abfd
, buf
, bufsiz
,
9395 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9399 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9400 const void *xfpregs
, int size
)
9402 char *note_name
= "LINUX";
9403 return elfcore_write_note (abfd
, buf
, bufsiz
,
9404 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9408 elfcore_write_ppc_vmx (bfd
*abfd
,
9411 const void *ppc_vmx
,
9414 char *note_name
= "LINUX";
9415 return elfcore_write_note (abfd
, buf
, bufsiz
,
9416 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9420 elfcore_write_ppc_vsx (bfd
*abfd
,
9423 const void *ppc_vsx
,
9426 char *note_name
= "LINUX";
9427 return elfcore_write_note (abfd
, buf
, bufsiz
,
9428 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9432 elfcore_write_s390_high_gprs (bfd
*abfd
,
9435 const void *s390_high_gprs
,
9438 char *note_name
= "LINUX";
9439 return elfcore_write_note (abfd
, buf
, bufsiz
,
9440 note_name
, NT_S390_HIGH_GPRS
,
9441 s390_high_gprs
, size
);
9445 elfcore_write_s390_timer (bfd
*abfd
,
9448 const void *s390_timer
,
9451 char *note_name
= "LINUX";
9452 return elfcore_write_note (abfd
, buf
, bufsiz
,
9453 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9457 elfcore_write_s390_todcmp (bfd
*abfd
,
9460 const void *s390_todcmp
,
9463 char *note_name
= "LINUX";
9464 return elfcore_write_note (abfd
, buf
, bufsiz
,
9465 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9469 elfcore_write_s390_todpreg (bfd
*abfd
,
9472 const void *s390_todpreg
,
9475 char *note_name
= "LINUX";
9476 return elfcore_write_note (abfd
, buf
, bufsiz
,
9477 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9481 elfcore_write_s390_ctrs (bfd
*abfd
,
9484 const void *s390_ctrs
,
9487 char *note_name
= "LINUX";
9488 return elfcore_write_note (abfd
, buf
, bufsiz
,
9489 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9493 elfcore_write_s390_prefix (bfd
*abfd
,
9496 const void *s390_prefix
,
9499 char *note_name
= "LINUX";
9500 return elfcore_write_note (abfd
, buf
, bufsiz
,
9501 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9505 elfcore_write_s390_last_break (bfd
*abfd
,
9508 const void *s390_last_break
,
9511 char *note_name
= "LINUX";
9512 return elfcore_write_note (abfd
, buf
, bufsiz
,
9513 note_name
, NT_S390_LAST_BREAK
,
9514 s390_last_break
, size
);
9518 elfcore_write_s390_system_call (bfd
*abfd
,
9521 const void *s390_system_call
,
9524 char *note_name
= "LINUX";
9525 return elfcore_write_note (abfd
, buf
, bufsiz
,
9526 note_name
, NT_S390_SYSTEM_CALL
,
9527 s390_system_call
, size
);
9531 elfcore_write_s390_tdb (bfd
*abfd
,
9534 const void *s390_tdb
,
9537 char *note_name
= "LINUX";
9538 return elfcore_write_note (abfd
, buf
, bufsiz
,
9539 note_name
, NT_S390_TDB
, s390_tdb
, size
);
9543 elfcore_write_arm_vfp (bfd
*abfd
,
9546 const void *arm_vfp
,
9549 char *note_name
= "LINUX";
9550 return elfcore_write_note (abfd
, buf
, bufsiz
,
9551 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
9555 elfcore_write_aarch_tls (bfd
*abfd
,
9558 const void *aarch_tls
,
9561 char *note_name
= "LINUX";
9562 return elfcore_write_note (abfd
, buf
, bufsiz
,
9563 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
9567 elfcore_write_aarch_hw_break (bfd
*abfd
,
9570 const void *aarch_hw_break
,
9573 char *note_name
= "LINUX";
9574 return elfcore_write_note (abfd
, buf
, bufsiz
,
9575 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
9579 elfcore_write_aarch_hw_watch (bfd
*abfd
,
9582 const void *aarch_hw_watch
,
9585 char *note_name
= "LINUX";
9586 return elfcore_write_note (abfd
, buf
, bufsiz
,
9587 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
9591 elfcore_write_register_note (bfd
*abfd
,
9594 const char *section
,
9598 if (strcmp (section
, ".reg2") == 0)
9599 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9600 if (strcmp (section
, ".reg-xfp") == 0)
9601 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9602 if (strcmp (section
, ".reg-xstate") == 0)
9603 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9604 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9605 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9606 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9607 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9608 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9609 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9610 if (strcmp (section
, ".reg-s390-timer") == 0)
9611 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9612 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9613 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9614 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9615 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9616 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9617 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9618 if (strcmp (section
, ".reg-s390-prefix") == 0)
9619 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9620 if (strcmp (section
, ".reg-s390-last-break") == 0)
9621 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
9622 if (strcmp (section
, ".reg-s390-system-call") == 0)
9623 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
9624 if (strcmp (section
, ".reg-s390-tdb") == 0)
9625 return elfcore_write_s390_tdb (abfd
, buf
, bufsiz
, data
, size
);
9626 if (strcmp (section
, ".reg-arm-vfp") == 0)
9627 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
9628 if (strcmp (section
, ".reg-aarch-tls") == 0)
9629 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
9630 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
9631 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
9632 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
9633 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
9638 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9643 while (p
< buf
+ size
)
9645 /* FIXME: bad alignment assumption. */
9646 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9647 Elf_Internal_Note in
;
9649 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9652 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9654 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9655 in
.namedata
= xnp
->name
;
9656 if (in
.namesz
> buf
- in
.namedata
+ size
)
9659 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9660 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9661 in
.descpos
= offset
+ (in
.descdata
- buf
);
9663 && (in
.descdata
>= buf
+ size
9664 || in
.descsz
> buf
- in
.descdata
+ size
))
9667 switch (bfd_get_format (abfd
))
9673 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
9675 if (! elfcore_grok_netbsd_note (abfd
, &in
))
9678 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
9680 if (! elfcore_grok_openbsd_note (abfd
, &in
))
9683 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
9685 if (! elfcore_grok_nto_note (abfd
, &in
))
9688 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
9690 if (! elfcore_grok_spu_note (abfd
, &in
))
9695 if (! elfcore_grok_note (abfd
, &in
))
9701 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9703 if (! elfobj_grok_gnu_note (abfd
, &in
))
9706 else if (in
.namesz
== sizeof "stapsdt"
9707 && strcmp (in
.namedata
, "stapsdt") == 0)
9709 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
9715 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9722 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9729 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9732 buf
= (char *) bfd_malloc (size
);
9736 if (bfd_bread (buf
, size
, abfd
) != size
9737 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9747 /* Providing external access to the ELF program header table. */
9749 /* Return an upper bound on the number of bytes required to store a
9750 copy of ABFD's program header table entries. Return -1 if an error
9751 occurs; bfd_get_error will return an appropriate code. */
9754 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9756 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9758 bfd_set_error (bfd_error_wrong_format
);
9762 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9765 /* Copy ABFD's program header table entries to *PHDRS. The entries
9766 will be stored as an array of Elf_Internal_Phdr structures, as
9767 defined in include/elf/internal.h. To find out how large the
9768 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9770 Return the number of program header table entries read, or -1 if an
9771 error occurs; bfd_get_error will return an appropriate code. */
9774 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9778 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9780 bfd_set_error (bfd_error_wrong_format
);
9784 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9785 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9786 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9791 enum elf_reloc_type_class
9792 _bfd_elf_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
9793 const asection
*rel_sec ATTRIBUTE_UNUSED
,
9794 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9796 return reloc_class_normal
;
9799 /* For RELA architectures, return the relocation value for a
9800 relocation against a local symbol. */
9803 _bfd_elf_rela_local_sym (bfd
*abfd
,
9804 Elf_Internal_Sym
*sym
,
9806 Elf_Internal_Rela
*rel
)
9808 asection
*sec
= *psec
;
9811 relocation
= (sec
->output_section
->vma
9812 + sec
->output_offset
9814 if ((sec
->flags
& SEC_MERGE
)
9815 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9816 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
9819 _bfd_merged_section_offset (abfd
, psec
,
9820 elf_section_data (sec
)->sec_info
,
9821 sym
->st_value
+ rel
->r_addend
);
9824 /* If we have changed the section, and our original section is
9825 marked with SEC_EXCLUDE, it means that the original
9826 SEC_MERGE section has been completely subsumed in some
9827 other SEC_MERGE section. In this case, we need to leave
9828 some info around for --emit-relocs. */
9829 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9830 sec
->kept_section
= *psec
;
9833 rel
->r_addend
-= relocation
;
9834 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9840 _bfd_elf_rel_local_sym (bfd
*abfd
,
9841 Elf_Internal_Sym
*sym
,
9845 asection
*sec
= *psec
;
9847 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
9848 return sym
->st_value
+ addend
;
9850 return _bfd_merged_section_offset (abfd
, psec
,
9851 elf_section_data (sec
)->sec_info
,
9852 sym
->st_value
+ addend
);
9856 _bfd_elf_section_offset (bfd
*abfd
,
9857 struct bfd_link_info
*info
,
9861 switch (sec
->sec_info_type
)
9863 case SEC_INFO_TYPE_STABS
:
9864 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
9866 case SEC_INFO_TYPE_EH_FRAME
:
9867 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
9869 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
9871 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9872 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
9873 offset
= sec
->size
- offset
- address_size
;
9879 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9880 reconstruct an ELF file by reading the segments out of remote memory
9881 based on the ELF file header at EHDR_VMA and the ELF program headers it
9882 points to. If not null, *LOADBASEP is filled in with the difference
9883 between the VMAs from which the segments were read, and the VMAs the
9884 file headers (and hence BFD's idea of each section's VMA) put them at.
9886 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9887 remote memory at target address VMA into the local buffer at MYADDR; it
9888 should return zero on success or an `errno' code on failure. TEMPL must
9889 be a BFD for an ELF target with the word size and byte order found in
9890 the remote memory. */
9893 bfd_elf_bfd_from_remote_memory
9897 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
9899 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
9900 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
9904 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
9905 long symcount ATTRIBUTE_UNUSED
,
9906 asymbol
**syms ATTRIBUTE_UNUSED
,
9911 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9914 const char *relplt_name
;
9915 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
9919 Elf_Internal_Shdr
*hdr
;
9925 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
9928 if (dynsymcount
<= 0)
9931 if (!bed
->plt_sym_val
)
9934 relplt_name
= bed
->relplt_name
;
9935 if (relplt_name
== NULL
)
9936 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
9937 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
9941 hdr
= &elf_section_data (relplt
)->this_hdr
;
9942 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
9943 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
9946 plt
= bfd_get_section_by_name (abfd
, ".plt");
9950 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9951 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
9954 count
= relplt
->size
/ hdr
->sh_entsize
;
9955 size
= count
* sizeof (asymbol
);
9956 p
= relplt
->relocation
;
9957 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9959 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
9963 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
9965 size
+= sizeof ("+0x") - 1 + 8;
9970 s
= *ret
= (asymbol
*) bfd_malloc (size
);
9974 names
= (char *) (s
+ count
);
9975 p
= relplt
->relocation
;
9977 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9982 addr
= bed
->plt_sym_val (i
, plt
, p
);
9983 if (addr
== (bfd_vma
) -1)
9986 *s
= **p
->sym_ptr_ptr
;
9987 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9988 we are defining a symbol, ensure one of them is set. */
9989 if ((s
->flags
& BSF_LOCAL
) == 0)
9990 s
->flags
|= BSF_GLOBAL
;
9991 s
->flags
|= BSF_SYNTHETIC
;
9993 s
->value
= addr
- plt
->vma
;
9996 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
9997 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
10003 memcpy (names
, "+0x", sizeof ("+0x") - 1);
10004 names
+= sizeof ("+0x") - 1;
10005 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
10006 for (a
= buf
; *a
== '0'; ++a
)
10009 memcpy (names
, a
, len
);
10012 memcpy (names
, "@plt", sizeof ("@plt"));
10013 names
+= sizeof ("@plt");
10020 /* It is only used by x86-64 so far. */
10021 asection _bfd_elf_large_com_section
10022 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
10023 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
10026 _bfd_elf_post_process_headers (bfd
* abfd
,
10027 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
10029 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
10031 i_ehdrp
= elf_elfheader (abfd
);
10033 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
10035 /* To make things simpler for the loader on Linux systems we set the
10036 osabi field to ELFOSABI_GNU if the binary contains symbols of
10037 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
10038 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
10039 && elf_tdata (abfd
)->has_gnu_symbols
)
10040 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
10044 /* Return TRUE for ELF symbol types that represent functions.
10045 This is the default version of this function, which is sufficient for
10046 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
10049 _bfd_elf_is_function_type (unsigned int type
)
10051 return (type
== STT_FUNC
10052 || type
== STT_GNU_IFUNC
);
10055 /* If the ELF symbol SYM might be a function in SEC, return the
10056 function size and set *CODE_OFF to the function's entry point,
10057 otherwise return zero. */
10060 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10063 bfd_size_type size
;
10065 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10066 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10067 || sym
->section
!= sec
)
10070 *code_off
= sym
->value
;
10072 if (!(sym
->flags
& BSF_SYNTHETIC
))
10073 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;