1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
6 Free Software Foundation, Inc.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
30 BFD support for ELF formats is being worked on.
31 Currently, the best supported back ends are for sparc and i386
32 (running svr4 or Solaris 2).
34 Documentation of the internals of the support code still needs
35 to be written. The code is changing quickly enough that we
36 haven't bothered yet. */
38 /* For sparc64-cross-sparc32. */
46 #include "libiberty.h"
47 #include "safe-ctype.h"
48 #include "elf-linux-psinfo.h"
54 static int elf_sort_sections (const void *, const void *);
55 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
56 static bfd_boolean
prep_headers (bfd
*);
57 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
58 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
59 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
62 /* Swap version information in and out. The version information is
63 currently size independent. If that ever changes, this code will
64 need to move into elfcode.h. */
66 /* Swap in a Verdef structure. */
69 _bfd_elf_swap_verdef_in (bfd
*abfd
,
70 const Elf_External_Verdef
*src
,
71 Elf_Internal_Verdef
*dst
)
73 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
74 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
75 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
76 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
77 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
78 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
79 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
82 /* Swap out a Verdef structure. */
85 _bfd_elf_swap_verdef_out (bfd
*abfd
,
86 const Elf_Internal_Verdef
*src
,
87 Elf_External_Verdef
*dst
)
89 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
90 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
91 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
92 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
93 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
94 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
95 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
98 /* Swap in a Verdaux structure. */
101 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
102 const Elf_External_Verdaux
*src
,
103 Elf_Internal_Verdaux
*dst
)
105 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
106 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
109 /* Swap out a Verdaux structure. */
112 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
113 const Elf_Internal_Verdaux
*src
,
114 Elf_External_Verdaux
*dst
)
116 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
117 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
120 /* Swap in a Verneed structure. */
123 _bfd_elf_swap_verneed_in (bfd
*abfd
,
124 const Elf_External_Verneed
*src
,
125 Elf_Internal_Verneed
*dst
)
127 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
128 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
129 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
130 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
131 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
134 /* Swap out a Verneed structure. */
137 _bfd_elf_swap_verneed_out (bfd
*abfd
,
138 const Elf_Internal_Verneed
*src
,
139 Elf_External_Verneed
*dst
)
141 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
142 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
143 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
144 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
145 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
148 /* Swap in a Vernaux structure. */
151 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
152 const Elf_External_Vernaux
*src
,
153 Elf_Internal_Vernaux
*dst
)
155 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
156 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
157 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
158 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
159 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
162 /* Swap out a Vernaux structure. */
165 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
166 const Elf_Internal_Vernaux
*src
,
167 Elf_External_Vernaux
*dst
)
169 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
170 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
171 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
172 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
173 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
176 /* Swap in a Versym structure. */
179 _bfd_elf_swap_versym_in (bfd
*abfd
,
180 const Elf_External_Versym
*src
,
181 Elf_Internal_Versym
*dst
)
183 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
186 /* Swap out a Versym structure. */
189 _bfd_elf_swap_versym_out (bfd
*abfd
,
190 const Elf_Internal_Versym
*src
,
191 Elf_External_Versym
*dst
)
193 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
196 /* Standard ELF hash function. Do not change this function; you will
197 cause invalid hash tables to be generated. */
200 bfd_elf_hash (const char *namearg
)
202 const unsigned char *name
= (const unsigned char *) namearg
;
207 while ((ch
= *name
++) != '\0')
210 if ((g
= (h
& 0xf0000000)) != 0)
213 /* The ELF ABI says `h &= ~g', but this is equivalent in
214 this case and on some machines one insn instead of two. */
218 return h
& 0xffffffff;
221 /* DT_GNU_HASH hash function. Do not change this function; you will
222 cause invalid hash tables to be generated. */
225 bfd_elf_gnu_hash (const char *namearg
)
227 const unsigned char *name
= (const unsigned char *) namearg
;
228 unsigned long h
= 5381;
231 while ((ch
= *name
++) != '\0')
232 h
= (h
<< 5) + h
+ ch
;
233 return h
& 0xffffffff;
236 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
237 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
239 bfd_elf_allocate_object (bfd
*abfd
,
241 enum elf_target_id object_id
)
243 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
244 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
245 if (abfd
->tdata
.any
== NULL
)
248 elf_object_id (abfd
) = object_id
;
249 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
255 bfd_elf_make_object (bfd
*abfd
)
257 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
258 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
263 bfd_elf_mkcorefile (bfd
*abfd
)
265 /* I think this can be done just like an object file. */
266 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
268 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
269 return elf_tdata (abfd
)->core
!= NULL
;
273 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
275 Elf_Internal_Shdr
**i_shdrp
;
276 bfd_byte
*shstrtab
= NULL
;
278 bfd_size_type shstrtabsize
;
280 i_shdrp
= elf_elfsections (abfd
);
282 || shindex
>= elf_numsections (abfd
)
283 || i_shdrp
[shindex
] == 0)
286 shstrtab
= i_shdrp
[shindex
]->contents
;
287 if (shstrtab
== NULL
)
289 /* No cached one, attempt to read, and cache what we read. */
290 offset
= i_shdrp
[shindex
]->sh_offset
;
291 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
293 /* Allocate and clear an extra byte at the end, to prevent crashes
294 in case the string table is not terminated. */
295 if (shstrtabsize
+ 1 <= 1
296 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
297 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
299 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
301 if (bfd_get_error () != bfd_error_system_call
)
302 bfd_set_error (bfd_error_file_truncated
);
304 /* Once we've failed to read it, make sure we don't keep
305 trying. Otherwise, we'll keep allocating space for
306 the string table over and over. */
307 i_shdrp
[shindex
]->sh_size
= 0;
310 shstrtab
[shstrtabsize
] = '\0';
311 i_shdrp
[shindex
]->contents
= shstrtab
;
313 return (char *) shstrtab
;
317 bfd_elf_string_from_elf_section (bfd
*abfd
,
318 unsigned int shindex
,
319 unsigned int strindex
)
321 Elf_Internal_Shdr
*hdr
;
326 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
329 hdr
= elf_elfsections (abfd
)[shindex
];
331 if (hdr
->contents
== NULL
332 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
335 if (strindex
>= hdr
->sh_size
)
337 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
338 (*_bfd_error_handler
)
339 (_("%B: invalid string offset %u >= %lu for section `%s'"),
340 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
341 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
343 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
347 return ((char *) hdr
->contents
) + strindex
;
350 /* Read and convert symbols to internal format.
351 SYMCOUNT specifies the number of symbols to read, starting from
352 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
353 are non-NULL, they are used to store the internal symbols, external
354 symbols, and symbol section index extensions, respectively.
355 Returns a pointer to the internal symbol buffer (malloced if necessary)
356 or NULL if there were no symbols or some kind of problem. */
359 bfd_elf_get_elf_syms (bfd
*ibfd
,
360 Elf_Internal_Shdr
*symtab_hdr
,
363 Elf_Internal_Sym
*intsym_buf
,
365 Elf_External_Sym_Shndx
*extshndx_buf
)
367 Elf_Internal_Shdr
*shndx_hdr
;
369 const bfd_byte
*esym
;
370 Elf_External_Sym_Shndx
*alloc_extshndx
;
371 Elf_External_Sym_Shndx
*shndx
;
372 Elf_Internal_Sym
*alloc_intsym
;
373 Elf_Internal_Sym
*isym
;
374 Elf_Internal_Sym
*isymend
;
375 const struct elf_backend_data
*bed
;
380 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
386 /* Normal syms might have section extension entries. */
388 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
389 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
391 /* Read the symbols. */
393 alloc_extshndx
= NULL
;
395 bed
= get_elf_backend_data (ibfd
);
396 extsym_size
= bed
->s
->sizeof_sym
;
397 amt
= symcount
* extsym_size
;
398 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
399 if (extsym_buf
== NULL
)
401 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
402 extsym_buf
= alloc_ext
;
404 if (extsym_buf
== NULL
405 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
406 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
412 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
416 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
417 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
418 if (extshndx_buf
== NULL
)
420 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
421 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
422 extshndx_buf
= alloc_extshndx
;
424 if (extshndx_buf
== NULL
425 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
426 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
433 if (intsym_buf
== NULL
)
435 alloc_intsym
= (Elf_Internal_Sym
*)
436 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
437 intsym_buf
= alloc_intsym
;
438 if (intsym_buf
== NULL
)
442 /* Convert the symbols to internal form. */
443 isymend
= intsym_buf
+ symcount
;
444 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
445 shndx
= extshndx_buf
;
447 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
448 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
450 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
451 (*_bfd_error_handler
) (_("%B symbol number %lu references "
452 "nonexistent SHT_SYMTAB_SHNDX section"),
453 ibfd
, (unsigned long) symoffset
);
454 if (alloc_intsym
!= NULL
)
461 if (alloc_ext
!= NULL
)
463 if (alloc_extshndx
!= NULL
)
464 free (alloc_extshndx
);
469 /* Look up a symbol name. */
471 bfd_elf_sym_name (bfd
*abfd
,
472 Elf_Internal_Shdr
*symtab_hdr
,
473 Elf_Internal_Sym
*isym
,
477 unsigned int iname
= isym
->st_name
;
478 unsigned int shindex
= symtab_hdr
->sh_link
;
480 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
481 /* Check for a bogus st_shndx to avoid crashing. */
482 && isym
->st_shndx
< elf_numsections (abfd
))
484 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
485 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
488 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
491 else if (sym_sec
&& *name
== '\0')
492 name
= bfd_section_name (abfd
, sym_sec
);
497 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
498 sections. The first element is the flags, the rest are section
501 typedef union elf_internal_group
{
502 Elf_Internal_Shdr
*shdr
;
504 } Elf_Internal_Group
;
506 /* Return the name of the group signature symbol. Why isn't the
507 signature just a string? */
510 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
512 Elf_Internal_Shdr
*hdr
;
513 unsigned char esym
[sizeof (Elf64_External_Sym
)];
514 Elf_External_Sym_Shndx eshndx
;
515 Elf_Internal_Sym isym
;
517 /* First we need to ensure the symbol table is available. Make sure
518 that it is a symbol table section. */
519 if (ghdr
->sh_link
>= elf_numsections (abfd
))
521 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
522 if (hdr
->sh_type
!= SHT_SYMTAB
523 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
526 /* Go read the symbol. */
527 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
528 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
529 &isym
, esym
, &eshndx
) == NULL
)
532 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
535 /* Set next_in_group list pointer, and group name for NEWSECT. */
538 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
540 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
542 /* If num_group is zero, read in all SHT_GROUP sections. The count
543 is set to -1 if there are no SHT_GROUP sections. */
546 unsigned int i
, shnum
;
548 /* First count the number of groups. If we have a SHT_GROUP
549 section with just a flag word (ie. sh_size is 4), ignore it. */
550 shnum
= elf_numsections (abfd
);
553 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
554 ( (shdr)->sh_type == SHT_GROUP \
555 && (shdr)->sh_size >= minsize \
556 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
557 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
559 for (i
= 0; i
< shnum
; i
++)
561 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
563 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
569 num_group
= (unsigned) -1;
570 elf_tdata (abfd
)->num_group
= num_group
;
574 /* We keep a list of elf section headers for group sections,
575 so we can find them quickly. */
578 elf_tdata (abfd
)->num_group
= num_group
;
579 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
580 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
581 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
585 for (i
= 0; i
< shnum
; i
++)
587 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
589 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
592 Elf_Internal_Group
*dest
;
594 /* Add to list of sections. */
595 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
598 /* Read the raw contents. */
599 BFD_ASSERT (sizeof (*dest
) >= 4);
600 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
601 shdr
->contents
= (unsigned char *)
602 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
603 /* PR binutils/4110: Handle corrupt group headers. */
604 if (shdr
->contents
== NULL
)
607 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
608 bfd_set_error (bfd_error_bad_value
);
612 memset (shdr
->contents
, 0, amt
);
614 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
615 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
619 /* Translate raw contents, a flag word followed by an
620 array of elf section indices all in target byte order,
621 to the flag word followed by an array of elf section
623 src
= shdr
->contents
+ shdr
->sh_size
;
624 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
631 idx
= H_GET_32 (abfd
, src
);
632 if (src
== shdr
->contents
)
635 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
636 shdr
->bfd_section
->flags
637 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
642 ((*_bfd_error_handler
)
643 (_("%B: invalid SHT_GROUP entry"), abfd
));
646 dest
->shdr
= elf_elfsections (abfd
)[idx
];
653 if (num_group
!= (unsigned) -1)
657 for (i
= 0; i
< num_group
; i
++)
659 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
660 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
661 unsigned int n_elt
= shdr
->sh_size
/ 4;
663 /* Look through this group's sections to see if current
664 section is a member. */
666 if ((++idx
)->shdr
== hdr
)
670 /* We are a member of this group. Go looking through
671 other members to see if any others are linked via
673 idx
= (Elf_Internal_Group
*) shdr
->contents
;
674 n_elt
= shdr
->sh_size
/ 4;
676 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
677 && elf_next_in_group (s
) != NULL
)
681 /* Snarf the group name from other member, and
682 insert current section in circular list. */
683 elf_group_name (newsect
) = elf_group_name (s
);
684 elf_next_in_group (newsect
) = elf_next_in_group (s
);
685 elf_next_in_group (s
) = newsect
;
691 gname
= group_signature (abfd
, shdr
);
694 elf_group_name (newsect
) = gname
;
696 /* Start a circular list with one element. */
697 elf_next_in_group (newsect
) = newsect
;
700 /* If the group section has been created, point to the
702 if (shdr
->bfd_section
!= NULL
)
703 elf_next_in_group (shdr
->bfd_section
) = newsect
;
711 if (elf_group_name (newsect
) == NULL
)
713 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
720 _bfd_elf_setup_sections (bfd
*abfd
)
723 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
724 bfd_boolean result
= TRUE
;
727 /* Process SHF_LINK_ORDER. */
728 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
730 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
731 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
733 unsigned int elfsec
= this_hdr
->sh_link
;
734 /* FIXME: The old Intel compiler and old strip/objcopy may
735 not set the sh_link or sh_info fields. Hence we could
736 get the situation where elfsec is 0. */
739 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
740 if (bed
->link_order_error_handler
)
741 bed
->link_order_error_handler
742 (_("%B: warning: sh_link not set for section `%A'"),
747 asection
*linksec
= NULL
;
749 if (elfsec
< elf_numsections (abfd
))
751 this_hdr
= elf_elfsections (abfd
)[elfsec
];
752 linksec
= this_hdr
->bfd_section
;
756 Some strip/objcopy may leave an incorrect value in
757 sh_link. We don't want to proceed. */
760 (*_bfd_error_handler
)
761 (_("%B: sh_link [%d] in section `%A' is incorrect"),
762 s
->owner
, s
, elfsec
);
766 elf_linked_to_section (s
) = linksec
;
771 /* Process section groups. */
772 if (num_group
== (unsigned) -1)
775 for (i
= 0; i
< num_group
; i
++)
777 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
778 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
779 unsigned int n_elt
= shdr
->sh_size
/ 4;
782 if ((++idx
)->shdr
->bfd_section
)
783 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
784 else if (idx
->shdr
->sh_type
== SHT_RELA
785 || idx
->shdr
->sh_type
== SHT_REL
)
786 /* We won't include relocation sections in section groups in
787 output object files. We adjust the group section size here
788 so that relocatable link will work correctly when
789 relocation sections are in section group in input object
791 shdr
->bfd_section
->size
-= 4;
794 /* There are some unknown sections in the group. */
795 (*_bfd_error_handler
)
796 (_("%B: unknown [%d] section `%s' in group [%s]"),
798 (unsigned int) idx
->shdr
->sh_type
,
799 bfd_elf_string_from_elf_section (abfd
,
800 (elf_elfheader (abfd
)
803 shdr
->bfd_section
->name
);
811 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
813 return elf_next_in_group (sec
) != NULL
;
816 /* Make a BFD section from an ELF section. We store a pointer to the
817 BFD section in the bfd_section field of the header. */
820 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
821 Elf_Internal_Shdr
*hdr
,
827 const struct elf_backend_data
*bed
;
829 if (hdr
->bfd_section
!= NULL
)
832 newsect
= bfd_make_section_anyway (abfd
, name
);
836 hdr
->bfd_section
= newsect
;
837 elf_section_data (newsect
)->this_hdr
= *hdr
;
838 elf_section_data (newsect
)->this_idx
= shindex
;
840 /* Always use the real type/flags. */
841 elf_section_type (newsect
) = hdr
->sh_type
;
842 elf_section_flags (newsect
) = hdr
->sh_flags
;
844 newsect
->filepos
= hdr
->sh_offset
;
846 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
847 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
848 || ! bfd_set_section_alignment (abfd
, newsect
,
849 bfd_log2 (hdr
->sh_addralign
)))
852 flags
= SEC_NO_FLAGS
;
853 if (hdr
->sh_type
!= SHT_NOBITS
)
854 flags
|= SEC_HAS_CONTENTS
;
855 if (hdr
->sh_type
== SHT_GROUP
)
856 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
857 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
860 if (hdr
->sh_type
!= SHT_NOBITS
)
863 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
864 flags
|= SEC_READONLY
;
865 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
867 else if ((flags
& SEC_LOAD
) != 0)
869 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
872 newsect
->entsize
= hdr
->sh_entsize
;
873 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
874 flags
|= SEC_STRINGS
;
876 if (hdr
->sh_flags
& SHF_GROUP
)
877 if (!setup_group (abfd
, hdr
, newsect
))
879 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
880 flags
|= SEC_THREAD_LOCAL
;
881 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
882 flags
|= SEC_EXCLUDE
;
884 if ((flags
& SEC_ALLOC
) == 0)
886 /* The debugging sections appear to be recognized only by name,
887 not any sort of flag. Their SEC_ALLOC bits are cleared. */
894 else if (name
[1] == 'g' && name
[2] == 'n')
895 p
= ".gnu.linkonce.wi.", n
= 17;
896 else if (name
[1] == 'g' && name
[2] == 'd')
897 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
898 else if (name
[1] == 'l')
900 else if (name
[1] == 's')
902 else if (name
[1] == 'z')
903 p
= ".zdebug", n
= 7;
906 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
907 flags
|= SEC_DEBUGGING
;
911 /* As a GNU extension, if the name begins with .gnu.linkonce, we
912 only link a single copy of the section. This is used to support
913 g++. g++ will emit each template expansion in its own section.
914 The symbols will be defined as weak, so that multiple definitions
915 are permitted. The GNU linker extension is to actually discard
916 all but one of the sections. */
917 if (CONST_STRNEQ (name
, ".gnu.linkonce")
918 && elf_next_in_group (newsect
) == NULL
)
919 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
921 bed
= get_elf_backend_data (abfd
);
922 if (bed
->elf_backend_section_flags
)
923 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
926 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
929 /* We do not parse the PT_NOTE segments as we are interested even in the
930 separate debug info files which may have the segments offsets corrupted.
931 PT_NOTEs from the core files are currently not parsed using BFD. */
932 if (hdr
->sh_type
== SHT_NOTE
)
936 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
939 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
943 if ((flags
& SEC_ALLOC
) != 0)
945 Elf_Internal_Phdr
*phdr
;
946 unsigned int i
, nload
;
948 /* Some ELF linkers produce binaries with all the program header
949 p_paddr fields zero. If we have such a binary with more than
950 one PT_LOAD header, then leave the section lma equal to vma
951 so that we don't create sections with overlapping lma. */
952 phdr
= elf_tdata (abfd
)->phdr
;
953 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
954 if (phdr
->p_paddr
!= 0)
956 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
958 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
961 phdr
= elf_tdata (abfd
)->phdr
;
962 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
964 if (((phdr
->p_type
== PT_LOAD
965 && (hdr
->sh_flags
& SHF_TLS
) == 0)
966 || phdr
->p_type
== PT_TLS
)
967 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
969 if ((flags
& SEC_LOAD
) == 0)
970 newsect
->lma
= (phdr
->p_paddr
971 + hdr
->sh_addr
- phdr
->p_vaddr
);
973 /* We used to use the same adjustment for SEC_LOAD
974 sections, but that doesn't work if the segment
975 is packed with code from multiple VMAs.
976 Instead we calculate the section LMA based on
977 the segment LMA. It is assumed that the
978 segment will contain sections with contiguous
979 LMAs, even if the VMAs are not. */
980 newsect
->lma
= (phdr
->p_paddr
981 + hdr
->sh_offset
- phdr
->p_offset
);
983 /* With contiguous segments, we can't tell from file
984 offsets whether a section with zero size should
985 be placed at the end of one segment or the
986 beginning of the next. Decide based on vaddr. */
987 if (hdr
->sh_addr
>= phdr
->p_vaddr
988 && (hdr
->sh_addr
+ hdr
->sh_size
989 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
995 /* Compress/decompress DWARF debug sections with names: .debug_* and
996 .zdebug_*, after the section flags is set. */
997 if ((flags
& SEC_DEBUGGING
)
998 && ((name
[1] == 'd' && name
[6] == '_')
999 || (name
[1] == 'z' && name
[7] == '_')))
1001 enum { nothing
, compress
, decompress
} action
= nothing
;
1004 if (bfd_is_section_compressed (abfd
, newsect
))
1006 /* Compressed section. Check if we should decompress. */
1007 if ((abfd
->flags
& BFD_DECOMPRESS
))
1008 action
= decompress
;
1012 /* Normal section. Check if we should compress. */
1013 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
1023 if (!bfd_init_section_compress_status (abfd
, newsect
))
1025 (*_bfd_error_handler
)
1026 (_("%B: unable to initialize compress status for section %s"),
1032 unsigned int len
= strlen (name
);
1034 new_name
= bfd_alloc (abfd
, len
+ 2);
1035 if (new_name
== NULL
)
1039 memcpy (new_name
+ 2, name
+ 1, len
);
1043 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1045 (*_bfd_error_handler
)
1046 (_("%B: unable to initialize decompress status for section %s"),
1052 unsigned int len
= strlen (name
);
1054 new_name
= bfd_alloc (abfd
, len
);
1055 if (new_name
== NULL
)
1058 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1062 if (new_name
!= NULL
)
1063 bfd_rename_section (abfd
, newsect
, new_name
);
1069 const char *const bfd_elf_section_type_names
[] = {
1070 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1071 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1072 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1075 /* ELF relocs are against symbols. If we are producing relocatable
1076 output, and the reloc is against an external symbol, and nothing
1077 has given us any additional addend, the resulting reloc will also
1078 be against the same symbol. In such a case, we don't want to
1079 change anything about the way the reloc is handled, since it will
1080 all be done at final link time. Rather than put special case code
1081 into bfd_perform_relocation, all the reloc types use this howto
1082 function. It just short circuits the reloc if producing
1083 relocatable output against an external symbol. */
1085 bfd_reloc_status_type
1086 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1087 arelent
*reloc_entry
,
1089 void *data ATTRIBUTE_UNUSED
,
1090 asection
*input_section
,
1092 char **error_message ATTRIBUTE_UNUSED
)
1094 if (output_bfd
!= NULL
1095 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1096 && (! reloc_entry
->howto
->partial_inplace
1097 || reloc_entry
->addend
== 0))
1099 reloc_entry
->address
+= input_section
->output_offset
;
1100 return bfd_reloc_ok
;
1103 return bfd_reloc_continue
;
1106 /* Copy the program header and other data from one object module to
1110 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1112 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1113 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1116 BFD_ASSERT (!elf_flags_init (obfd
)
1117 || (elf_elfheader (obfd
)->e_flags
1118 == elf_elfheader (ibfd
)->e_flags
));
1120 elf_gp (obfd
) = elf_gp (ibfd
);
1121 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1122 elf_flags_init (obfd
) = TRUE
;
1124 /* Copy object attributes. */
1125 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1130 get_segment_type (unsigned int p_type
)
1135 case PT_NULL
: pt
= "NULL"; break;
1136 case PT_LOAD
: pt
= "LOAD"; break;
1137 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1138 case PT_INTERP
: pt
= "INTERP"; break;
1139 case PT_NOTE
: pt
= "NOTE"; break;
1140 case PT_SHLIB
: pt
= "SHLIB"; break;
1141 case PT_PHDR
: pt
= "PHDR"; break;
1142 case PT_TLS
: pt
= "TLS"; break;
1143 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1144 case PT_GNU_STACK
: pt
= "STACK"; break;
1145 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1146 default: pt
= NULL
; break;
1151 /* Print out the program headers. */
1154 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1156 FILE *f
= (FILE *) farg
;
1157 Elf_Internal_Phdr
*p
;
1159 bfd_byte
*dynbuf
= NULL
;
1161 p
= elf_tdata (abfd
)->phdr
;
1166 fprintf (f
, _("\nProgram Header:\n"));
1167 c
= elf_elfheader (abfd
)->e_phnum
;
1168 for (i
= 0; i
< c
; i
++, p
++)
1170 const char *pt
= get_segment_type (p
->p_type
);
1175 sprintf (buf
, "0x%lx", p
->p_type
);
1178 fprintf (f
, "%8s off 0x", pt
);
1179 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1180 fprintf (f
, " vaddr 0x");
1181 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1182 fprintf (f
, " paddr 0x");
1183 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1184 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1185 fprintf (f
, " filesz 0x");
1186 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1187 fprintf (f
, " memsz 0x");
1188 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1189 fprintf (f
, " flags %c%c%c",
1190 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1191 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1192 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1193 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1194 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1199 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1202 unsigned int elfsec
;
1203 unsigned long shlink
;
1204 bfd_byte
*extdyn
, *extdynend
;
1206 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1208 fprintf (f
, _("\nDynamic Section:\n"));
1210 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1213 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1214 if (elfsec
== SHN_BAD
)
1216 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1218 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1219 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1222 extdynend
= extdyn
+ s
->size
;
1223 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1225 Elf_Internal_Dyn dyn
;
1226 const char *name
= "";
1228 bfd_boolean stringp
;
1229 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1231 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1233 if (dyn
.d_tag
== DT_NULL
)
1240 if (bed
->elf_backend_get_target_dtag
)
1241 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1243 if (!strcmp (name
, ""))
1245 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1250 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1251 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1252 case DT_PLTGOT
: name
= "PLTGOT"; break;
1253 case DT_HASH
: name
= "HASH"; break;
1254 case DT_STRTAB
: name
= "STRTAB"; break;
1255 case DT_SYMTAB
: name
= "SYMTAB"; break;
1256 case DT_RELA
: name
= "RELA"; break;
1257 case DT_RELASZ
: name
= "RELASZ"; break;
1258 case DT_RELAENT
: name
= "RELAENT"; break;
1259 case DT_STRSZ
: name
= "STRSZ"; break;
1260 case DT_SYMENT
: name
= "SYMENT"; break;
1261 case DT_INIT
: name
= "INIT"; break;
1262 case DT_FINI
: name
= "FINI"; break;
1263 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1264 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1265 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1266 case DT_REL
: name
= "REL"; break;
1267 case DT_RELSZ
: name
= "RELSZ"; break;
1268 case DT_RELENT
: name
= "RELENT"; break;
1269 case DT_PLTREL
: name
= "PLTREL"; break;
1270 case DT_DEBUG
: name
= "DEBUG"; break;
1271 case DT_TEXTREL
: name
= "TEXTREL"; break;
1272 case DT_JMPREL
: name
= "JMPREL"; break;
1273 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1274 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1275 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1276 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1277 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1278 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1279 case DT_FLAGS
: name
= "FLAGS"; break;
1280 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1281 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1282 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1283 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1284 case DT_MOVEENT
: name
= "MOVEENT"; break;
1285 case DT_MOVESZ
: name
= "MOVESZ"; break;
1286 case DT_FEATURE
: name
= "FEATURE"; break;
1287 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1288 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1289 case DT_SYMINENT
: name
= "SYMINENT"; break;
1290 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1291 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1292 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1293 case DT_PLTPAD
: name
= "PLTPAD"; break;
1294 case DT_MOVETAB
: name
= "MOVETAB"; break;
1295 case DT_SYMINFO
: name
= "SYMINFO"; break;
1296 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1297 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1298 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1299 case DT_VERSYM
: name
= "VERSYM"; break;
1300 case DT_VERDEF
: name
= "VERDEF"; break;
1301 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1302 case DT_VERNEED
: name
= "VERNEED"; break;
1303 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1304 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1305 case DT_USED
: name
= "USED"; break;
1306 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1307 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1310 fprintf (f
, " %-20s ", name
);
1314 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1319 unsigned int tagv
= dyn
.d_un
.d_val
;
1321 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1324 fprintf (f
, "%s", string
);
1333 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1334 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1336 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1340 if (elf_dynverdef (abfd
) != 0)
1342 Elf_Internal_Verdef
*t
;
1344 fprintf (f
, _("\nVersion definitions:\n"));
1345 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1347 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1348 t
->vd_flags
, t
->vd_hash
,
1349 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1350 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1352 Elf_Internal_Verdaux
*a
;
1355 for (a
= t
->vd_auxptr
->vda_nextptr
;
1359 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1365 if (elf_dynverref (abfd
) != 0)
1367 Elf_Internal_Verneed
*t
;
1369 fprintf (f
, _("\nVersion References:\n"));
1370 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1372 Elf_Internal_Vernaux
*a
;
1374 fprintf (f
, _(" required from %s:\n"),
1375 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1376 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1377 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1378 a
->vna_flags
, a
->vna_other
,
1379 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1391 /* Display ELF-specific fields of a symbol. */
1394 bfd_elf_print_symbol (bfd
*abfd
,
1397 bfd_print_symbol_type how
)
1399 FILE *file
= (FILE *) filep
;
1402 case bfd_print_symbol_name
:
1403 fprintf (file
, "%s", symbol
->name
);
1405 case bfd_print_symbol_more
:
1406 fprintf (file
, "elf ");
1407 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1408 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1410 case bfd_print_symbol_all
:
1412 const char *section_name
;
1413 const char *name
= NULL
;
1414 const struct elf_backend_data
*bed
;
1415 unsigned char st_other
;
1418 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1420 bed
= get_elf_backend_data (abfd
);
1421 if (bed
->elf_backend_print_symbol_all
)
1422 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1426 name
= symbol
->name
;
1427 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1430 fprintf (file
, " %s\t", section_name
);
1431 /* Print the "other" value for a symbol. For common symbols,
1432 we've already printed the size; now print the alignment.
1433 For other symbols, we have no specified alignment, and
1434 we've printed the address; now print the size. */
1435 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1436 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1438 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1439 bfd_fprintf_vma (abfd
, file
, val
);
1441 /* If we have version information, print it. */
1442 if (elf_dynversym (abfd
) != 0
1443 && (elf_dynverdef (abfd
) != 0
1444 || elf_dynverref (abfd
) != 0))
1446 unsigned int vernum
;
1447 const char *version_string
;
1449 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1452 version_string
= "";
1453 else if (vernum
== 1)
1454 version_string
= "Base";
1455 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1457 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1460 Elf_Internal_Verneed
*t
;
1462 version_string
= "";
1463 for (t
= elf_tdata (abfd
)->verref
;
1467 Elf_Internal_Vernaux
*a
;
1469 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1471 if (a
->vna_other
== vernum
)
1473 version_string
= a
->vna_nodename
;
1480 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1481 fprintf (file
, " %-11s", version_string
);
1486 fprintf (file
, " (%s)", version_string
);
1487 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1492 /* If the st_other field is not zero, print it. */
1493 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1498 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1499 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1500 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1502 /* Some other non-defined flags are also present, so print
1504 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1507 fprintf (file
, " %s", name
);
1513 /* Allocate an ELF string table--force the first byte to be zero. */
1515 struct bfd_strtab_hash
*
1516 _bfd_elf_stringtab_init (void)
1518 struct bfd_strtab_hash
*ret
;
1520 ret
= _bfd_stringtab_init ();
1525 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1526 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1527 if (loc
== (bfd_size_type
) -1)
1529 _bfd_stringtab_free (ret
);
1536 /* ELF .o/exec file reading */
1538 /* Create a new bfd section from an ELF section header. */
1541 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1543 Elf_Internal_Shdr
*hdr
;
1544 Elf_Internal_Ehdr
*ehdr
;
1545 const struct elf_backend_data
*bed
;
1548 if (shindex
>= elf_numsections (abfd
))
1551 hdr
= elf_elfsections (abfd
)[shindex
];
1552 ehdr
= elf_elfheader (abfd
);
1553 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1558 bed
= get_elf_backend_data (abfd
);
1559 switch (hdr
->sh_type
)
1562 /* Inactive section. Throw it away. */
1565 case SHT_PROGBITS
: /* Normal section with contents. */
1566 case SHT_NOBITS
: /* .bss section. */
1567 case SHT_HASH
: /* .hash section. */
1568 case SHT_NOTE
: /* .note section. */
1569 case SHT_INIT_ARRAY
: /* .init_array section. */
1570 case SHT_FINI_ARRAY
: /* .fini_array section. */
1571 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1572 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1573 case SHT_GNU_HASH
: /* .gnu.hash section. */
1574 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1576 case SHT_DYNAMIC
: /* Dynamic linking information. */
1577 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1579 if (hdr
->sh_link
> elf_numsections (abfd
))
1581 /* PR 10478: Accept Solaris binaries with a sh_link
1582 field set to SHN_BEFORE or SHN_AFTER. */
1583 switch (bfd_get_arch (abfd
))
1586 case bfd_arch_sparc
:
1587 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1588 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1590 /* Otherwise fall through. */
1595 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1597 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1599 Elf_Internal_Shdr
*dynsymhdr
;
1601 /* The shared libraries distributed with hpux11 have a bogus
1602 sh_link field for the ".dynamic" section. Find the
1603 string table for the ".dynsym" section instead. */
1604 if (elf_dynsymtab (abfd
) != 0)
1606 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1607 hdr
->sh_link
= dynsymhdr
->sh_link
;
1611 unsigned int i
, num_sec
;
1613 num_sec
= elf_numsections (abfd
);
1614 for (i
= 1; i
< num_sec
; i
++)
1616 dynsymhdr
= elf_elfsections (abfd
)[i
];
1617 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1619 hdr
->sh_link
= dynsymhdr
->sh_link
;
1627 case SHT_SYMTAB
: /* A symbol table */
1628 if (elf_onesymtab (abfd
) == shindex
)
1631 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1633 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1635 if (hdr
->sh_size
!= 0)
1637 /* Some assemblers erroneously set sh_info to one with a
1638 zero sh_size. ld sees this as a global symbol count
1639 of (unsigned) -1. Fix it here. */
1643 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1644 elf_onesymtab (abfd
) = shindex
;
1645 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1646 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1647 abfd
->flags
|= HAS_SYMS
;
1649 /* Sometimes a shared object will map in the symbol table. If
1650 SHF_ALLOC is set, and this is a shared object, then we also
1651 treat this section as a BFD section. We can not base the
1652 decision purely on SHF_ALLOC, because that flag is sometimes
1653 set in a relocatable object file, which would confuse the
1655 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1656 && (abfd
->flags
& DYNAMIC
) != 0
1657 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1661 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1662 can't read symbols without that section loaded as well. It
1663 is most likely specified by the next section header. */
1664 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1666 unsigned int i
, num_sec
;
1668 num_sec
= elf_numsections (abfd
);
1669 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1671 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1672 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1673 && hdr2
->sh_link
== shindex
)
1677 for (i
= 1; i
< shindex
; i
++)
1679 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1680 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1681 && hdr2
->sh_link
== shindex
)
1685 return bfd_section_from_shdr (abfd
, i
);
1689 case SHT_DYNSYM
: /* A dynamic symbol table */
1690 if (elf_dynsymtab (abfd
) == shindex
)
1693 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1695 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1697 if (hdr
->sh_size
!= 0)
1699 /* Some linkers erroneously set sh_info to one with a
1700 zero sh_size. ld sees this as a global symbol count
1701 of (unsigned) -1. Fix it here. */
1705 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1706 elf_dynsymtab (abfd
) = shindex
;
1707 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1708 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1709 abfd
->flags
|= HAS_SYMS
;
1711 /* Besides being a symbol table, we also treat this as a regular
1712 section, so that objcopy can handle it. */
1713 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1715 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1716 if (elf_symtab_shndx (abfd
) == shindex
)
1719 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1720 elf_symtab_shndx (abfd
) = shindex
;
1721 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1722 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1725 case SHT_STRTAB
: /* A string table */
1726 if (hdr
->bfd_section
!= NULL
)
1728 if (ehdr
->e_shstrndx
== shindex
)
1730 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1731 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1734 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1737 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1738 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1741 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1744 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1745 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1746 elf_elfsections (abfd
)[shindex
] = hdr
;
1747 /* We also treat this as a regular section, so that objcopy
1749 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1753 /* If the string table isn't one of the above, then treat it as a
1754 regular section. We need to scan all the headers to be sure,
1755 just in case this strtab section appeared before the above. */
1756 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1758 unsigned int i
, num_sec
;
1760 num_sec
= elf_numsections (abfd
);
1761 for (i
= 1; i
< num_sec
; i
++)
1763 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1764 if (hdr2
->sh_link
== shindex
)
1766 /* Prevent endless recursion on broken objects. */
1769 if (! bfd_section_from_shdr (abfd
, i
))
1771 if (elf_onesymtab (abfd
) == i
)
1773 if (elf_dynsymtab (abfd
) == i
)
1774 goto dynsymtab_strtab
;
1778 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1782 /* *These* do a lot of work -- but build no sections! */
1784 asection
*target_sect
;
1785 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1786 unsigned int num_sec
= elf_numsections (abfd
);
1787 struct bfd_elf_section_data
*esdt
;
1791 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1792 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1795 /* Check for a bogus link to avoid crashing. */
1796 if (hdr
->sh_link
>= num_sec
)
1798 ((*_bfd_error_handler
)
1799 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1800 abfd
, hdr
->sh_link
, name
, shindex
));
1801 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1805 /* For some incomprehensible reason Oracle distributes
1806 libraries for Solaris in which some of the objects have
1807 bogus sh_link fields. It would be nice if we could just
1808 reject them, but, unfortunately, some people need to use
1809 them. We scan through the section headers; if we find only
1810 one suitable symbol table, we clobber the sh_link to point
1811 to it. I hope this doesn't break anything.
1813 Don't do it on executable nor shared library. */
1814 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1815 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1816 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1822 for (scan
= 1; scan
< num_sec
; scan
++)
1824 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1825 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1836 hdr
->sh_link
= found
;
1839 /* Get the symbol table. */
1840 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1841 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1842 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1845 /* If this reloc section does not use the main symbol table we
1846 don't treat it as a reloc section. BFD can't adequately
1847 represent such a section, so at least for now, we don't
1848 try. We just present it as a normal section. We also
1849 can't use it as a reloc section if it points to the null
1850 section, an invalid section, another reloc section, or its
1851 sh_link points to the null section. */
1852 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1853 || hdr
->sh_link
== SHN_UNDEF
1854 || hdr
->sh_info
== SHN_UNDEF
1855 || hdr
->sh_info
>= num_sec
1856 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1857 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1858 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1861 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1863 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1864 if (target_sect
== NULL
)
1867 esdt
= elf_section_data (target_sect
);
1868 if (hdr
->sh_type
== SHT_RELA
)
1869 p_hdr
= &esdt
->rela
.hdr
;
1871 p_hdr
= &esdt
->rel
.hdr
;
1873 BFD_ASSERT (*p_hdr
== NULL
);
1874 amt
= sizeof (*hdr2
);
1875 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1880 elf_elfsections (abfd
)[shindex
] = hdr2
;
1881 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1882 target_sect
->flags
|= SEC_RELOC
;
1883 target_sect
->relocation
= NULL
;
1884 target_sect
->rel_filepos
= hdr
->sh_offset
;
1885 /* In the section to which the relocations apply, mark whether
1886 its relocations are of the REL or RELA variety. */
1887 if (hdr
->sh_size
!= 0)
1889 if (hdr
->sh_type
== SHT_RELA
)
1890 target_sect
->use_rela_p
= 1;
1892 abfd
->flags
|= HAS_RELOC
;
1896 case SHT_GNU_verdef
:
1897 elf_dynverdef (abfd
) = shindex
;
1898 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1899 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1901 case SHT_GNU_versym
:
1902 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1904 elf_dynversym (abfd
) = shindex
;
1905 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1906 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1908 case SHT_GNU_verneed
:
1909 elf_dynverref (abfd
) = shindex
;
1910 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1911 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1917 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
1919 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1921 if (hdr
->contents
!= NULL
)
1923 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1924 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1927 if (idx
->flags
& GRP_COMDAT
)
1928 hdr
->bfd_section
->flags
1929 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1931 /* We try to keep the same section order as it comes in. */
1933 while (--n_elt
!= 0)
1937 if (idx
->shdr
!= NULL
1938 && (s
= idx
->shdr
->bfd_section
) != NULL
1939 && elf_next_in_group (s
) != NULL
)
1941 elf_next_in_group (hdr
->bfd_section
) = s
;
1949 /* Possibly an attributes section. */
1950 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1951 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1953 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1955 _bfd_elf_parse_attributes (abfd
, hdr
);
1959 /* Check for any processor-specific section types. */
1960 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1963 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1965 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1966 /* FIXME: How to properly handle allocated section reserved
1967 for applications? */
1968 (*_bfd_error_handler
)
1969 (_("%B: don't know how to handle allocated, application "
1970 "specific section `%s' [0x%8x]"),
1971 abfd
, name
, hdr
->sh_type
);
1973 /* Allow sections reserved for applications. */
1974 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1977 else if (hdr
->sh_type
>= SHT_LOPROC
1978 && hdr
->sh_type
<= SHT_HIPROC
)
1979 /* FIXME: We should handle this section. */
1980 (*_bfd_error_handler
)
1981 (_("%B: don't know how to handle processor specific section "
1983 abfd
, name
, hdr
->sh_type
);
1984 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1986 /* Unrecognised OS-specific sections. */
1987 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1988 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1989 required to correctly process the section and the file should
1990 be rejected with an error message. */
1991 (*_bfd_error_handler
)
1992 (_("%B: don't know how to handle OS specific section "
1994 abfd
, name
, hdr
->sh_type
);
1996 /* Otherwise it should be processed. */
1997 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2000 /* FIXME: We should handle this section. */
2001 (*_bfd_error_handler
)
2002 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2003 abfd
, name
, hdr
->sh_type
);
2011 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2014 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2016 unsigned long r_symndx
)
2018 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2020 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2022 Elf_Internal_Shdr
*symtab_hdr
;
2023 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2024 Elf_External_Sym_Shndx eshndx
;
2026 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2027 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2028 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2031 if (cache
->abfd
!= abfd
)
2033 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2036 cache
->indx
[ent
] = r_symndx
;
2039 return &cache
->sym
[ent
];
2042 /* Given an ELF section number, retrieve the corresponding BFD
2046 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2048 if (sec_index
>= elf_numsections (abfd
))
2050 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2053 static const struct bfd_elf_special_section special_sections_b
[] =
2055 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2056 { NULL
, 0, 0, 0, 0 }
2059 static const struct bfd_elf_special_section special_sections_c
[] =
2061 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2062 { NULL
, 0, 0, 0, 0 }
2065 static const struct bfd_elf_special_section special_sections_d
[] =
2067 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2068 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2069 /* There are more DWARF sections than these, but they needn't be added here
2070 unless you have to cope with broken compilers that don't emit section
2071 attributes or you want to help the user writing assembler. */
2072 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2073 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2074 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2075 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2076 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2077 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2078 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2079 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2080 { NULL
, 0, 0, 0, 0 }
2083 static const struct bfd_elf_special_section special_sections_f
[] =
2085 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2086 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2087 { NULL
, 0, 0, 0, 0 }
2090 static const struct bfd_elf_special_section special_sections_g
[] =
2092 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2093 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2094 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2095 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2096 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2097 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2098 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2099 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2100 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2101 { NULL
, 0, 0, 0, 0 }
2104 static const struct bfd_elf_special_section special_sections_h
[] =
2106 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2107 { NULL
, 0, 0, 0, 0 }
2110 static const struct bfd_elf_special_section special_sections_i
[] =
2112 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2113 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2114 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2115 { NULL
, 0, 0, 0, 0 }
2118 static const struct bfd_elf_special_section special_sections_l
[] =
2120 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2121 { NULL
, 0, 0, 0, 0 }
2124 static const struct bfd_elf_special_section special_sections_n
[] =
2126 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2127 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2128 { NULL
, 0, 0, 0, 0 }
2131 static const struct bfd_elf_special_section special_sections_p
[] =
2133 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2134 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2135 { NULL
, 0, 0, 0, 0 }
2138 static const struct bfd_elf_special_section special_sections_r
[] =
2140 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2141 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2142 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2143 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2144 { NULL
, 0, 0, 0, 0 }
2147 static const struct bfd_elf_special_section special_sections_s
[] =
2149 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2150 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2151 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2152 /* See struct bfd_elf_special_section declaration for the semantics of
2153 this special case where .prefix_length != strlen (.prefix). */
2154 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2155 { NULL
, 0, 0, 0, 0 }
2158 static const struct bfd_elf_special_section special_sections_t
[] =
2160 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2161 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2162 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2163 { NULL
, 0, 0, 0, 0 }
2166 static const struct bfd_elf_special_section special_sections_z
[] =
2168 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2169 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2170 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2171 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2172 { NULL
, 0, 0, 0, 0 }
2175 static const struct bfd_elf_special_section
* const special_sections
[] =
2177 special_sections_b
, /* 'b' */
2178 special_sections_c
, /* 'c' */
2179 special_sections_d
, /* 'd' */
2181 special_sections_f
, /* 'f' */
2182 special_sections_g
, /* 'g' */
2183 special_sections_h
, /* 'h' */
2184 special_sections_i
, /* 'i' */
2187 special_sections_l
, /* 'l' */
2189 special_sections_n
, /* 'n' */
2191 special_sections_p
, /* 'p' */
2193 special_sections_r
, /* 'r' */
2194 special_sections_s
, /* 's' */
2195 special_sections_t
, /* 't' */
2201 special_sections_z
/* 'z' */
2204 const struct bfd_elf_special_section
*
2205 _bfd_elf_get_special_section (const char *name
,
2206 const struct bfd_elf_special_section
*spec
,
2212 len
= strlen (name
);
2214 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2217 int prefix_len
= spec
[i
].prefix_length
;
2219 if (len
< prefix_len
)
2221 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2224 suffix_len
= spec
[i
].suffix_length
;
2225 if (suffix_len
<= 0)
2227 if (name
[prefix_len
] != 0)
2229 if (suffix_len
== 0)
2231 if (name
[prefix_len
] != '.'
2232 && (suffix_len
== -2
2233 || (rela
&& spec
[i
].type
== SHT_REL
)))
2239 if (len
< prefix_len
+ suffix_len
)
2241 if (memcmp (name
+ len
- suffix_len
,
2242 spec
[i
].prefix
+ prefix_len
,
2252 const struct bfd_elf_special_section
*
2253 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2256 const struct bfd_elf_special_section
*spec
;
2257 const struct elf_backend_data
*bed
;
2259 /* See if this is one of the special sections. */
2260 if (sec
->name
== NULL
)
2263 bed
= get_elf_backend_data (abfd
);
2264 spec
= bed
->special_sections
;
2267 spec
= _bfd_elf_get_special_section (sec
->name
,
2268 bed
->special_sections
,
2274 if (sec
->name
[0] != '.')
2277 i
= sec
->name
[1] - 'b';
2278 if (i
< 0 || i
> 'z' - 'b')
2281 spec
= special_sections
[i
];
2286 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2290 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2292 struct bfd_elf_section_data
*sdata
;
2293 const struct elf_backend_data
*bed
;
2294 const struct bfd_elf_special_section
*ssect
;
2296 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2299 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2303 sec
->used_by_bfd
= sdata
;
2306 /* Indicate whether or not this section should use RELA relocations. */
2307 bed
= get_elf_backend_data (abfd
);
2308 sec
->use_rela_p
= bed
->default_use_rela_p
;
2310 /* When we read a file, we don't need to set ELF section type and
2311 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2312 anyway. We will set ELF section type and flags for all linker
2313 created sections. If user specifies BFD section flags, we will
2314 set ELF section type and flags based on BFD section flags in
2315 elf_fake_sections. Special handling for .init_array/.fini_array
2316 output sections since they may contain .ctors/.dtors input
2317 sections. We don't want _bfd_elf_init_private_section_data to
2318 copy ELF section type from .ctors/.dtors input sections. */
2319 if (abfd
->direction
!= read_direction
2320 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2322 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2325 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2326 || ssect
->type
== SHT_INIT_ARRAY
2327 || ssect
->type
== SHT_FINI_ARRAY
))
2329 elf_section_type (sec
) = ssect
->type
;
2330 elf_section_flags (sec
) = ssect
->attr
;
2334 return _bfd_generic_new_section_hook (abfd
, sec
);
2337 /* Create a new bfd section from an ELF program header.
2339 Since program segments have no names, we generate a synthetic name
2340 of the form segment<NUM>, where NUM is generally the index in the
2341 program header table. For segments that are split (see below) we
2342 generate the names segment<NUM>a and segment<NUM>b.
2344 Note that some program segments may have a file size that is different than
2345 (less than) the memory size. All this means is that at execution the
2346 system must allocate the amount of memory specified by the memory size,
2347 but only initialize it with the first "file size" bytes read from the
2348 file. This would occur for example, with program segments consisting
2349 of combined data+bss.
2351 To handle the above situation, this routine generates TWO bfd sections
2352 for the single program segment. The first has the length specified by
2353 the file size of the segment, and the second has the length specified
2354 by the difference between the two sizes. In effect, the segment is split
2355 into its initialized and uninitialized parts.
2360 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2361 Elf_Internal_Phdr
*hdr
,
2363 const char *type_name
)
2371 split
= ((hdr
->p_memsz
> 0)
2372 && (hdr
->p_filesz
> 0)
2373 && (hdr
->p_memsz
> hdr
->p_filesz
));
2375 if (hdr
->p_filesz
> 0)
2377 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2378 len
= strlen (namebuf
) + 1;
2379 name
= (char *) bfd_alloc (abfd
, len
);
2382 memcpy (name
, namebuf
, len
);
2383 newsect
= bfd_make_section (abfd
, name
);
2384 if (newsect
== NULL
)
2386 newsect
->vma
= hdr
->p_vaddr
;
2387 newsect
->lma
= hdr
->p_paddr
;
2388 newsect
->size
= hdr
->p_filesz
;
2389 newsect
->filepos
= hdr
->p_offset
;
2390 newsect
->flags
|= SEC_HAS_CONTENTS
;
2391 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2392 if (hdr
->p_type
== PT_LOAD
)
2394 newsect
->flags
|= SEC_ALLOC
;
2395 newsect
->flags
|= SEC_LOAD
;
2396 if (hdr
->p_flags
& PF_X
)
2398 /* FIXME: all we known is that it has execute PERMISSION,
2400 newsect
->flags
|= SEC_CODE
;
2403 if (!(hdr
->p_flags
& PF_W
))
2405 newsect
->flags
|= SEC_READONLY
;
2409 if (hdr
->p_memsz
> hdr
->p_filesz
)
2413 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2414 len
= strlen (namebuf
) + 1;
2415 name
= (char *) bfd_alloc (abfd
, len
);
2418 memcpy (name
, namebuf
, len
);
2419 newsect
= bfd_make_section (abfd
, name
);
2420 if (newsect
== NULL
)
2422 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2423 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2424 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2425 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2426 align
= newsect
->vma
& -newsect
->vma
;
2427 if (align
== 0 || align
> hdr
->p_align
)
2428 align
= hdr
->p_align
;
2429 newsect
->alignment_power
= bfd_log2 (align
);
2430 if (hdr
->p_type
== PT_LOAD
)
2432 /* Hack for gdb. Segments that have not been modified do
2433 not have their contents written to a core file, on the
2434 assumption that a debugger can find the contents in the
2435 executable. We flag this case by setting the fake
2436 section size to zero. Note that "real" bss sections will
2437 always have their contents dumped to the core file. */
2438 if (bfd_get_format (abfd
) == bfd_core
)
2440 newsect
->flags
|= SEC_ALLOC
;
2441 if (hdr
->p_flags
& PF_X
)
2442 newsect
->flags
|= SEC_CODE
;
2444 if (!(hdr
->p_flags
& PF_W
))
2445 newsect
->flags
|= SEC_READONLY
;
2452 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2454 const struct elf_backend_data
*bed
;
2456 switch (hdr
->p_type
)
2459 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2462 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2465 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2468 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2471 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2473 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2478 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2481 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2483 case PT_GNU_EH_FRAME
:
2484 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2488 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2491 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2494 /* Check for any processor-specific program segment types. */
2495 bed
= get_elf_backend_data (abfd
);
2496 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2500 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2504 _bfd_elf_single_rel_hdr (asection
*sec
)
2506 if (elf_section_data (sec
)->rel
.hdr
)
2508 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2509 return elf_section_data (sec
)->rel
.hdr
;
2512 return elf_section_data (sec
)->rela
.hdr
;
2515 /* Allocate and initialize a section-header for a new reloc section,
2516 containing relocations against ASECT. It is stored in RELDATA. If
2517 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2521 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2522 struct bfd_elf_section_reloc_data
*reldata
,
2524 bfd_boolean use_rela_p
)
2526 Elf_Internal_Shdr
*rel_hdr
;
2528 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2531 amt
= sizeof (Elf_Internal_Shdr
);
2532 BFD_ASSERT (reldata
->hdr
== NULL
);
2533 rel_hdr
= bfd_zalloc (abfd
, amt
);
2534 reldata
->hdr
= rel_hdr
;
2536 amt
= sizeof ".rela" + strlen (asect
->name
);
2537 name
= (char *) bfd_alloc (abfd
, amt
);
2540 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2542 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2544 if (rel_hdr
->sh_name
== (unsigned int) -1)
2546 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2547 rel_hdr
->sh_entsize
= (use_rela_p
2548 ? bed
->s
->sizeof_rela
2549 : bed
->s
->sizeof_rel
);
2550 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2551 rel_hdr
->sh_flags
= 0;
2552 rel_hdr
->sh_addr
= 0;
2553 rel_hdr
->sh_size
= 0;
2554 rel_hdr
->sh_offset
= 0;
2559 /* Return the default section type based on the passed in section flags. */
2562 bfd_elf_get_default_section_type (flagword flags
)
2564 if ((flags
& SEC_ALLOC
) != 0
2565 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2567 return SHT_PROGBITS
;
2570 struct fake_section_arg
2572 struct bfd_link_info
*link_info
;
2576 /* Set up an ELF internal section header for a section. */
2579 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2581 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2582 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2583 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2584 Elf_Internal_Shdr
*this_hdr
;
2585 unsigned int sh_type
;
2589 /* We already failed; just get out of the bfd_map_over_sections
2594 this_hdr
= &esd
->this_hdr
;
2596 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2597 asect
->name
, FALSE
);
2598 if (this_hdr
->sh_name
== (unsigned int) -1)
2604 /* Don't clear sh_flags. Assembler may set additional bits. */
2606 if ((asect
->flags
& SEC_ALLOC
) != 0
2607 || asect
->user_set_vma
)
2608 this_hdr
->sh_addr
= asect
->vma
;
2610 this_hdr
->sh_addr
= 0;
2612 this_hdr
->sh_offset
= 0;
2613 this_hdr
->sh_size
= asect
->size
;
2614 this_hdr
->sh_link
= 0;
2615 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2616 /* The sh_entsize and sh_info fields may have been set already by
2617 copy_private_section_data. */
2619 this_hdr
->bfd_section
= asect
;
2620 this_hdr
->contents
= NULL
;
2622 /* If the section type is unspecified, we set it based on
2624 if ((asect
->flags
& SEC_GROUP
) != 0)
2625 sh_type
= SHT_GROUP
;
2627 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2629 if (this_hdr
->sh_type
== SHT_NULL
)
2630 this_hdr
->sh_type
= sh_type
;
2631 else if (this_hdr
->sh_type
== SHT_NOBITS
2632 && sh_type
== SHT_PROGBITS
2633 && (asect
->flags
& SEC_ALLOC
) != 0)
2635 /* Warn if we are changing a NOBITS section to PROGBITS, but
2636 allow the link to proceed. This can happen when users link
2637 non-bss input sections to bss output sections, or emit data
2638 to a bss output section via a linker script. */
2639 (*_bfd_error_handler
)
2640 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2641 this_hdr
->sh_type
= sh_type
;
2644 switch (this_hdr
->sh_type
)
2650 case SHT_INIT_ARRAY
:
2651 case SHT_FINI_ARRAY
:
2652 case SHT_PREINIT_ARRAY
:
2659 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2663 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2667 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2671 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2672 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2676 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2677 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2680 case SHT_GNU_versym
:
2681 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2684 case SHT_GNU_verdef
:
2685 this_hdr
->sh_entsize
= 0;
2686 /* objcopy or strip will copy over sh_info, but may not set
2687 cverdefs. The linker will set cverdefs, but sh_info will be
2689 if (this_hdr
->sh_info
== 0)
2690 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2692 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2693 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2696 case SHT_GNU_verneed
:
2697 this_hdr
->sh_entsize
= 0;
2698 /* objcopy or strip will copy over sh_info, but may not set
2699 cverrefs. The linker will set cverrefs, but sh_info will be
2701 if (this_hdr
->sh_info
== 0)
2702 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2704 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2705 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2709 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2713 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2717 if ((asect
->flags
& SEC_ALLOC
) != 0)
2718 this_hdr
->sh_flags
|= SHF_ALLOC
;
2719 if ((asect
->flags
& SEC_READONLY
) == 0)
2720 this_hdr
->sh_flags
|= SHF_WRITE
;
2721 if ((asect
->flags
& SEC_CODE
) != 0)
2722 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2723 if ((asect
->flags
& SEC_MERGE
) != 0)
2725 this_hdr
->sh_flags
|= SHF_MERGE
;
2726 this_hdr
->sh_entsize
= asect
->entsize
;
2727 if ((asect
->flags
& SEC_STRINGS
) != 0)
2728 this_hdr
->sh_flags
|= SHF_STRINGS
;
2730 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2731 this_hdr
->sh_flags
|= SHF_GROUP
;
2732 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2734 this_hdr
->sh_flags
|= SHF_TLS
;
2735 if (asect
->size
== 0
2736 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2738 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2740 this_hdr
->sh_size
= 0;
2743 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2744 if (this_hdr
->sh_size
!= 0)
2745 this_hdr
->sh_type
= SHT_NOBITS
;
2749 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2750 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2752 /* If the section has relocs, set up a section header for the
2753 SHT_REL[A] section. If two relocation sections are required for
2754 this section, it is up to the processor-specific back-end to
2755 create the other. */
2756 if ((asect
->flags
& SEC_RELOC
) != 0)
2758 /* When doing a relocatable link, create both REL and RELA sections if
2761 /* Do the normal setup if we wouldn't create any sections here. */
2762 && esd
->rel
.count
+ esd
->rela
.count
> 0
2763 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2765 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2766 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2771 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2772 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2778 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2780 ? &esd
->rela
: &esd
->rel
),
2786 /* Check for processor-specific section types. */
2787 sh_type
= this_hdr
->sh_type
;
2788 if (bed
->elf_backend_fake_sections
2789 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2792 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2794 /* Don't change the header type from NOBITS if we are being
2795 called for objcopy --only-keep-debug. */
2796 this_hdr
->sh_type
= sh_type
;
2800 /* Fill in the contents of a SHT_GROUP section. Called from
2801 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2802 when ELF targets use the generic linker, ld. Called for ld -r
2803 from bfd_elf_final_link. */
2806 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2808 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2809 asection
*elt
, *first
;
2813 /* Ignore linker created group section. See elfNN_ia64_object_p in
2815 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2819 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2821 unsigned long symindx
= 0;
2823 /* elf_group_id will have been set up by objcopy and the
2825 if (elf_group_id (sec
) != NULL
)
2826 symindx
= elf_group_id (sec
)->udata
.i
;
2830 /* If called from the assembler, swap_out_syms will have set up
2831 elf_section_syms. */
2832 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2833 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2835 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2837 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2839 /* The ELF backend linker sets sh_info to -2 when the group
2840 signature symbol is global, and thus the index can't be
2841 set until all local symbols are output. */
2842 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2843 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2844 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2845 unsigned long extsymoff
= 0;
2846 struct elf_link_hash_entry
*h
;
2848 if (!elf_bad_symtab (igroup
->owner
))
2850 Elf_Internal_Shdr
*symtab_hdr
;
2852 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2853 extsymoff
= symtab_hdr
->sh_info
;
2855 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2856 while (h
->root
.type
== bfd_link_hash_indirect
2857 || h
->root
.type
== bfd_link_hash_warning
)
2858 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2860 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2863 /* The contents won't be allocated for "ld -r" or objcopy. */
2865 if (sec
->contents
== NULL
)
2868 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2870 /* Arrange for the section to be written out. */
2871 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2872 if (sec
->contents
== NULL
)
2879 loc
= sec
->contents
+ sec
->size
;
2881 /* Get the pointer to the first section in the group that gas
2882 squirreled away here. objcopy arranges for this to be set to the
2883 start of the input section group. */
2884 first
= elt
= elf_next_in_group (sec
);
2886 /* First element is a flag word. Rest of section is elf section
2887 indices for all the sections of the group. Write them backwards
2888 just to keep the group in the same order as given in .section
2889 directives, not that it matters. */
2896 s
= s
->output_section
;
2898 && !bfd_is_abs_section (s
))
2900 unsigned int idx
= elf_section_data (s
)->this_idx
;
2903 H_PUT_32 (abfd
, idx
, loc
);
2905 elt
= elf_next_in_group (elt
);
2910 if ((loc
-= 4) != sec
->contents
)
2913 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2916 /* Assign all ELF section numbers. The dummy first section is handled here
2917 too. The link/info pointers for the standard section types are filled
2918 in here too, while we're at it. */
2921 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2923 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2925 unsigned int section_number
, secn
;
2926 Elf_Internal_Shdr
**i_shdrp
;
2927 struct bfd_elf_section_data
*d
;
2928 bfd_boolean need_symtab
;
2932 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2934 /* SHT_GROUP sections are in relocatable files only. */
2935 if (link_info
== NULL
|| link_info
->relocatable
)
2937 /* Put SHT_GROUP sections first. */
2938 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2940 d
= elf_section_data (sec
);
2942 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2944 if (sec
->flags
& SEC_LINKER_CREATED
)
2946 /* Remove the linker created SHT_GROUP sections. */
2947 bfd_section_list_remove (abfd
, sec
);
2948 abfd
->section_count
--;
2951 d
->this_idx
= section_number
++;
2956 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2958 d
= elf_section_data (sec
);
2960 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2961 d
->this_idx
= section_number
++;
2962 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2965 d
->rel
.idx
= section_number
++;
2966 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
2973 d
->rela
.idx
= section_number
++;
2974 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
2980 elf_shstrtab_sec (abfd
) = section_number
++;
2981 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2982 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
2984 need_symtab
= (bfd_get_symcount (abfd
) > 0
2985 || (link_info
== NULL
2986 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
2990 elf_onesymtab (abfd
) = section_number
++;
2991 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2992 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
2994 elf_symtab_shndx (abfd
) = section_number
++;
2995 t
->symtab_shndx_hdr
.sh_name
2996 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2997 ".symtab_shndx", FALSE
);
2998 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3001 elf_strtab_sec (abfd
) = section_number
++;
3002 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3005 if (section_number
>= SHN_LORESERVE
)
3007 _bfd_error_handler (_("%B: too many sections: %u"),
3008 abfd
, section_number
);
3012 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
3013 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3015 elf_numsections (abfd
) = section_number
;
3016 elf_elfheader (abfd
)->e_shnum
= section_number
;
3018 /* Set up the list of section header pointers, in agreement with the
3020 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3021 sizeof (Elf_Internal_Shdr
*));
3022 if (i_shdrp
== NULL
)
3025 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3026 sizeof (Elf_Internal_Shdr
));
3027 if (i_shdrp
[0] == NULL
)
3029 bfd_release (abfd
, i_shdrp
);
3033 elf_elfsections (abfd
) = i_shdrp
;
3035 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3038 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3039 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3041 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3042 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3044 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3045 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3048 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3053 d
= elf_section_data (sec
);
3055 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3056 if (d
->rel
.idx
!= 0)
3057 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3058 if (d
->rela
.idx
!= 0)
3059 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3061 /* Fill in the sh_link and sh_info fields while we're at it. */
3063 /* sh_link of a reloc section is the section index of the symbol
3064 table. sh_info is the section index of the section to which
3065 the relocation entries apply. */
3066 if (d
->rel
.idx
!= 0)
3068 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3069 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3071 if (d
->rela
.idx
!= 0)
3073 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3074 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3077 /* We need to set up sh_link for SHF_LINK_ORDER. */
3078 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3080 s
= elf_linked_to_section (sec
);
3083 /* elf_linked_to_section points to the input section. */
3084 if (link_info
!= NULL
)
3086 /* Check discarded linkonce section. */
3087 if (discarded_section (s
))
3090 (*_bfd_error_handler
)
3091 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3092 abfd
, d
->this_hdr
.bfd_section
,
3094 /* Point to the kept section if it has the same
3095 size as the discarded one. */
3096 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3099 bfd_set_error (bfd_error_bad_value
);
3105 s
= s
->output_section
;
3106 BFD_ASSERT (s
!= NULL
);
3110 /* Handle objcopy. */
3111 if (s
->output_section
== NULL
)
3113 (*_bfd_error_handler
)
3114 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3115 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3116 bfd_set_error (bfd_error_bad_value
);
3119 s
= s
->output_section
;
3121 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3126 The Intel C compiler generates SHT_IA_64_UNWIND with
3127 SHF_LINK_ORDER. But it doesn't set the sh_link or
3128 sh_info fields. Hence we could get the situation
3130 const struct elf_backend_data
*bed
3131 = get_elf_backend_data (abfd
);
3132 if (bed
->link_order_error_handler
)
3133 bed
->link_order_error_handler
3134 (_("%B: warning: sh_link not set for section `%A'"),
3139 switch (d
->this_hdr
.sh_type
)
3143 /* A reloc section which we are treating as a normal BFD
3144 section. sh_link is the section index of the symbol
3145 table. sh_info is the section index of the section to
3146 which the relocation entries apply. We assume that an
3147 allocated reloc section uses the dynamic symbol table.
3148 FIXME: How can we be sure? */
3149 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3151 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3153 /* We look up the section the relocs apply to by name. */
3155 if (d
->this_hdr
.sh_type
== SHT_REL
)
3159 s
= bfd_get_section_by_name (abfd
, name
);
3161 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3165 /* We assume that a section named .stab*str is a stabs
3166 string section. We look for a section with the same name
3167 but without the trailing ``str'', and set its sh_link
3168 field to point to this section. */
3169 if (CONST_STRNEQ (sec
->name
, ".stab")
3170 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3175 len
= strlen (sec
->name
);
3176 alc
= (char *) bfd_malloc (len
- 2);
3179 memcpy (alc
, sec
->name
, len
- 3);
3180 alc
[len
- 3] = '\0';
3181 s
= bfd_get_section_by_name (abfd
, alc
);
3185 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3187 /* This is a .stab section. */
3188 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3189 elf_section_data (s
)->this_hdr
.sh_entsize
3190 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3197 case SHT_GNU_verneed
:
3198 case SHT_GNU_verdef
:
3199 /* sh_link is the section header index of the string table
3200 used for the dynamic entries, or the symbol table, or the
3202 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3204 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3207 case SHT_GNU_LIBLIST
:
3208 /* sh_link is the section header index of the prelink library
3209 list used for the dynamic entries, or the symbol table, or
3210 the version strings. */
3211 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3212 ? ".dynstr" : ".gnu.libstr");
3214 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3219 case SHT_GNU_versym
:
3220 /* sh_link is the section header index of the symbol table
3221 this hash table or version table is for. */
3222 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3224 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3228 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3232 for (secn
= 1; secn
< section_number
; ++secn
)
3233 if (i_shdrp
[secn
] == NULL
)
3234 i_shdrp
[secn
] = i_shdrp
[0];
3236 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3237 i_shdrp
[secn
]->sh_name
);
3242 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3244 /* If the backend has a special mapping, use it. */
3245 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3246 if (bed
->elf_backend_sym_is_global
)
3247 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3249 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3250 || bfd_is_und_section (bfd_get_section (sym
))
3251 || bfd_is_com_section (bfd_get_section (sym
)));
3254 /* Don't output section symbols for sections that are not going to be
3255 output, that are duplicates or there is no BFD section. */
3258 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3260 elf_symbol_type
*type_ptr
;
3262 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3265 type_ptr
= elf_symbol_from (abfd
, sym
);
3266 return ((type_ptr
!= NULL
3267 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3268 && bfd_is_abs_section (sym
->section
))
3269 || !(sym
->section
->owner
== abfd
3270 || (sym
->section
->output_section
->owner
== abfd
3271 && sym
->section
->output_offset
== 0)
3272 || bfd_is_abs_section (sym
->section
)));
3275 /* Map symbol from it's internal number to the external number, moving
3276 all local symbols to be at the head of the list. */
3279 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3281 unsigned int symcount
= bfd_get_symcount (abfd
);
3282 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3283 asymbol
**sect_syms
;
3284 unsigned int num_locals
= 0;
3285 unsigned int num_globals
= 0;
3286 unsigned int num_locals2
= 0;
3287 unsigned int num_globals2
= 0;
3294 fprintf (stderr
, "elf_map_symbols\n");
3298 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3300 if (max_index
< asect
->index
)
3301 max_index
= asect
->index
;
3305 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3306 if (sect_syms
== NULL
)
3308 elf_section_syms (abfd
) = sect_syms
;
3309 elf_num_section_syms (abfd
) = max_index
;
3311 /* Init sect_syms entries for any section symbols we have already
3312 decided to output. */
3313 for (idx
= 0; idx
< symcount
; idx
++)
3315 asymbol
*sym
= syms
[idx
];
3317 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3319 && !ignore_section_sym (abfd
, sym
)
3320 && !bfd_is_abs_section (sym
->section
))
3322 asection
*sec
= sym
->section
;
3324 if (sec
->owner
!= abfd
)
3325 sec
= sec
->output_section
;
3327 sect_syms
[sec
->index
] = syms
[idx
];
3331 /* Classify all of the symbols. */
3332 for (idx
= 0; idx
< symcount
; idx
++)
3334 if (sym_is_global (abfd
, syms
[idx
]))
3336 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3340 /* We will be adding a section symbol for each normal BFD section. Most
3341 sections will already have a section symbol in outsymbols, but
3342 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3343 at least in that case. */
3344 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3346 if (sect_syms
[asect
->index
] == NULL
)
3348 if (!sym_is_global (abfd
, asect
->symbol
))
3355 /* Now sort the symbols so the local symbols are first. */
3356 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3357 sizeof (asymbol
*));
3359 if (new_syms
== NULL
)
3362 for (idx
= 0; idx
< symcount
; idx
++)
3364 asymbol
*sym
= syms
[idx
];
3367 if (sym_is_global (abfd
, sym
))
3368 i
= num_locals
+ num_globals2
++;
3369 else if (!ignore_section_sym (abfd
, sym
))
3374 sym
->udata
.i
= i
+ 1;
3376 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3378 if (sect_syms
[asect
->index
] == NULL
)
3380 asymbol
*sym
= asect
->symbol
;
3383 sect_syms
[asect
->index
] = sym
;
3384 if (!sym_is_global (abfd
, sym
))
3387 i
= num_locals
+ num_globals2
++;
3389 sym
->udata
.i
= i
+ 1;
3393 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3395 *pnum_locals
= num_locals
;
3399 /* Align to the maximum file alignment that could be required for any
3400 ELF data structure. */
3402 static inline file_ptr
3403 align_file_position (file_ptr off
, int align
)
3405 return (off
+ align
- 1) & ~(align
- 1);
3408 /* Assign a file position to a section, optionally aligning to the
3409 required section alignment. */
3412 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3416 if (align
&& i_shdrp
->sh_addralign
> 1)
3417 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3418 i_shdrp
->sh_offset
= offset
;
3419 if (i_shdrp
->bfd_section
!= NULL
)
3420 i_shdrp
->bfd_section
->filepos
= offset
;
3421 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3422 offset
+= i_shdrp
->sh_size
;
3426 /* Compute the file positions we are going to put the sections at, and
3427 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3428 is not NULL, this is being called by the ELF backend linker. */
3431 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3432 struct bfd_link_info
*link_info
)
3434 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3435 struct fake_section_arg fsargs
;
3437 struct bfd_strtab_hash
*strtab
= NULL
;
3438 Elf_Internal_Shdr
*shstrtab_hdr
;
3439 bfd_boolean need_symtab
;
3441 if (abfd
->output_has_begun
)
3444 /* Do any elf backend specific processing first. */
3445 if (bed
->elf_backend_begin_write_processing
)
3446 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3448 if (! prep_headers (abfd
))
3451 /* Post process the headers if necessary. */
3452 if (bed
->elf_backend_post_process_headers
)
3453 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3455 fsargs
.failed
= FALSE
;
3456 fsargs
.link_info
= link_info
;
3457 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3461 if (!assign_section_numbers (abfd
, link_info
))
3464 /* The backend linker builds symbol table information itself. */
3465 need_symtab
= (link_info
== NULL
3466 && (bfd_get_symcount (abfd
) > 0
3467 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3471 /* Non-zero if doing a relocatable link. */
3472 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3474 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3479 if (link_info
== NULL
)
3481 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3486 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3487 /* sh_name was set in prep_headers. */
3488 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3489 shstrtab_hdr
->sh_flags
= 0;
3490 shstrtab_hdr
->sh_addr
= 0;
3491 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3492 shstrtab_hdr
->sh_entsize
= 0;
3493 shstrtab_hdr
->sh_link
= 0;
3494 shstrtab_hdr
->sh_info
= 0;
3495 /* sh_offset is set in assign_file_positions_except_relocs. */
3496 shstrtab_hdr
->sh_addralign
= 1;
3498 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3504 Elf_Internal_Shdr
*hdr
;
3506 off
= elf_next_file_pos (abfd
);
3508 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3509 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3511 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3512 if (hdr
->sh_size
!= 0)
3513 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3515 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3516 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3518 elf_next_file_pos (abfd
) = off
;
3520 /* Now that we know where the .strtab section goes, write it
3522 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3523 || ! _bfd_stringtab_emit (abfd
, strtab
))
3525 _bfd_stringtab_free (strtab
);
3528 abfd
->output_has_begun
= TRUE
;
3533 /* Make an initial estimate of the size of the program header. If we
3534 get the number wrong here, we'll redo section placement. */
3536 static bfd_size_type
3537 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3541 const struct elf_backend_data
*bed
;
3543 /* Assume we will need exactly two PT_LOAD segments: one for text
3544 and one for data. */
3547 s
= bfd_get_section_by_name (abfd
, ".interp");
3548 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3550 /* If we have a loadable interpreter section, we need a
3551 PT_INTERP segment. In this case, assume we also need a
3552 PT_PHDR segment, although that may not be true for all
3557 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3559 /* We need a PT_DYNAMIC segment. */
3563 if (info
!= NULL
&& info
->relro
)
3565 /* We need a PT_GNU_RELRO segment. */
3569 if (elf_eh_frame_hdr (abfd
))
3571 /* We need a PT_GNU_EH_FRAME segment. */
3575 if (elf_stack_flags (abfd
))
3577 /* We need a PT_GNU_STACK segment. */
3581 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3583 if ((s
->flags
& SEC_LOAD
) != 0
3584 && CONST_STRNEQ (s
->name
, ".note"))
3586 /* We need a PT_NOTE segment. */
3588 /* Try to create just one PT_NOTE segment
3589 for all adjacent loadable .note* sections.
3590 gABI requires that within a PT_NOTE segment
3591 (and also inside of each SHT_NOTE section)
3592 each note is padded to a multiple of 4 size,
3593 so we check whether the sections are correctly
3595 if (s
->alignment_power
== 2)
3596 while (s
->next
!= NULL
3597 && s
->next
->alignment_power
== 2
3598 && (s
->next
->flags
& SEC_LOAD
) != 0
3599 && CONST_STRNEQ (s
->next
->name
, ".note"))
3604 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3606 if (s
->flags
& SEC_THREAD_LOCAL
)
3608 /* We need a PT_TLS segment. */
3614 /* Let the backend count up any program headers it might need. */
3615 bed
= get_elf_backend_data (abfd
);
3616 if (bed
->elf_backend_additional_program_headers
)
3620 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3626 return segs
* bed
->s
->sizeof_phdr
;
3629 /* Find the segment that contains the output_section of section. */
3632 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3634 struct elf_segment_map
*m
;
3635 Elf_Internal_Phdr
*p
;
3637 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3643 for (i
= m
->count
- 1; i
>= 0; i
--)
3644 if (m
->sections
[i
] == section
)
3651 /* Create a mapping from a set of sections to a program segment. */
3653 static struct elf_segment_map
*
3654 make_mapping (bfd
*abfd
,
3655 asection
**sections
,
3660 struct elf_segment_map
*m
;
3665 amt
= sizeof (struct elf_segment_map
);
3666 amt
+= (to
- from
- 1) * sizeof (asection
*);
3667 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3671 m
->p_type
= PT_LOAD
;
3672 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3673 m
->sections
[i
- from
] = *hdrpp
;
3674 m
->count
= to
- from
;
3676 if (from
== 0 && phdr
)
3678 /* Include the headers in the first PT_LOAD segment. */
3679 m
->includes_filehdr
= 1;
3680 m
->includes_phdrs
= 1;
3686 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3689 struct elf_segment_map
*
3690 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3692 struct elf_segment_map
*m
;
3694 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3695 sizeof (struct elf_segment_map
));
3699 m
->p_type
= PT_DYNAMIC
;
3701 m
->sections
[0] = dynsec
;
3706 /* Possibly add or remove segments from the segment map. */
3709 elf_modify_segment_map (bfd
*abfd
,
3710 struct bfd_link_info
*info
,
3711 bfd_boolean remove_empty_load
)
3713 struct elf_segment_map
**m
;
3714 const struct elf_backend_data
*bed
;
3716 /* The placement algorithm assumes that non allocated sections are
3717 not in PT_LOAD segments. We ensure this here by removing such
3718 sections from the segment map. We also remove excluded
3719 sections. Finally, any PT_LOAD segment without sections is
3721 m
= &elf_seg_map (abfd
);
3724 unsigned int i
, new_count
;
3726 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3728 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3729 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3730 || (*m
)->p_type
!= PT_LOAD
))
3732 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3736 (*m
)->count
= new_count
;
3738 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3744 bed
= get_elf_backend_data (abfd
);
3745 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3747 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3754 /* Set up a mapping from BFD sections to program segments. */
3757 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3760 struct elf_segment_map
*m
;
3761 asection
**sections
= NULL
;
3762 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3763 bfd_boolean no_user_phdrs
;
3765 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
3768 info
->user_phdrs
= !no_user_phdrs
;
3770 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3774 struct elf_segment_map
*mfirst
;
3775 struct elf_segment_map
**pm
;
3778 unsigned int phdr_index
;
3779 bfd_vma maxpagesize
;
3781 bfd_boolean phdr_in_segment
= TRUE
;
3782 bfd_boolean writable
;
3784 asection
*first_tls
= NULL
;
3785 asection
*dynsec
, *eh_frame_hdr
;
3787 bfd_vma addr_mask
, wrap_to
= 0;
3789 /* Select the allocated sections, and sort them. */
3791 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3792 sizeof (asection
*));
3793 if (sections
== NULL
)
3796 /* Calculate top address, avoiding undefined behaviour of shift
3797 left operator when shift count is equal to size of type
3799 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3800 addr_mask
= (addr_mask
<< 1) + 1;
3803 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3805 if ((s
->flags
& SEC_ALLOC
) != 0)
3809 /* A wrapping section potentially clashes with header. */
3810 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3811 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3814 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3817 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3819 /* Build the mapping. */
3824 /* If we have a .interp section, then create a PT_PHDR segment for
3825 the program headers and a PT_INTERP segment for the .interp
3827 s
= bfd_get_section_by_name (abfd
, ".interp");
3828 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3830 amt
= sizeof (struct elf_segment_map
);
3831 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3835 m
->p_type
= PT_PHDR
;
3836 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3837 m
->p_flags
= PF_R
| PF_X
;
3838 m
->p_flags_valid
= 1;
3839 m
->includes_phdrs
= 1;
3844 amt
= sizeof (struct elf_segment_map
);
3845 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3849 m
->p_type
= PT_INTERP
;
3857 /* Look through the sections. We put sections in the same program
3858 segment when the start of the second section can be placed within
3859 a few bytes of the end of the first section. */
3863 maxpagesize
= bed
->maxpagesize
;
3865 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3867 && (dynsec
->flags
& SEC_LOAD
) == 0)
3870 /* Deal with -Ttext or something similar such that the first section
3871 is not adjacent to the program headers. This is an
3872 approximation, since at this point we don't know exactly how many
3873 program headers we will need. */
3876 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
3878 if (phdr_size
== (bfd_size_type
) -1)
3879 phdr_size
= get_program_header_size (abfd
, info
);
3880 phdr_size
+= bed
->s
->sizeof_ehdr
;
3881 if ((abfd
->flags
& D_PAGED
) == 0
3882 || (sections
[0]->lma
& addr_mask
) < phdr_size
3883 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
3884 < phdr_size
% maxpagesize
)
3885 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
3886 phdr_in_segment
= FALSE
;
3889 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3892 bfd_boolean new_segment
;
3896 /* See if this section and the last one will fit in the same
3899 if (last_hdr
== NULL
)
3901 /* If we don't have a segment yet, then we don't need a new
3902 one (we build the last one after this loop). */
3903 new_segment
= FALSE
;
3905 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3907 /* If this section has a different relation between the
3908 virtual address and the load address, then we need a new
3912 else if (hdr
->lma
< last_hdr
->lma
+ last_size
3913 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
3915 /* If this section has a load address that makes it overlap
3916 the previous section, then we need a new segment. */
3919 /* In the next test we have to be careful when last_hdr->lma is close
3920 to the end of the address space. If the aligned address wraps
3921 around to the start of the address space, then there are no more
3922 pages left in memory and it is OK to assume that the current
3923 section can be included in the current segment. */
3924 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3926 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3929 /* If putting this section in this segment would force us to
3930 skip a page in the segment, then we need a new segment. */
3933 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3934 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3936 /* We don't want to put a loadable section after a
3937 nonloadable section in the same segment.
3938 Consider .tbss sections as loadable for this purpose. */
3941 else if ((abfd
->flags
& D_PAGED
) == 0)
3943 /* If the file is not demand paged, which means that we
3944 don't require the sections to be correctly aligned in the
3945 file, then there is no other reason for a new segment. */
3946 new_segment
= FALSE
;
3949 && (hdr
->flags
& SEC_READONLY
) == 0
3950 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
3951 != (hdr
->lma
& -maxpagesize
)))
3953 /* We don't want to put a writable section in a read only
3954 segment, unless they are on the same page in memory
3955 anyhow. We already know that the last section does not
3956 bring us past the current section on the page, so the
3957 only case in which the new section is not on the same
3958 page as the previous section is when the previous section
3959 ends precisely on a page boundary. */
3964 /* Otherwise, we can use the same segment. */
3965 new_segment
= FALSE
;
3968 /* Allow interested parties a chance to override our decision. */
3969 if (last_hdr
!= NULL
3971 && info
->callbacks
->override_segment_assignment
!= NULL
)
3973 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
3979 if ((hdr
->flags
& SEC_READONLY
) == 0)
3982 /* .tbss sections effectively have zero size. */
3983 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3984 != SEC_THREAD_LOCAL
)
3985 last_size
= hdr
->size
;
3991 /* We need a new program segment. We must create a new program
3992 header holding all the sections from phdr_index until hdr. */
3994 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4001 if ((hdr
->flags
& SEC_READONLY
) == 0)
4007 /* .tbss sections effectively have zero size. */
4008 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4009 last_size
= hdr
->size
;
4013 phdr_in_segment
= FALSE
;
4016 /* Create a final PT_LOAD program segment, but not if it's just
4018 if (last_hdr
!= NULL
4019 && (i
- phdr_index
!= 1
4020 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4021 != SEC_THREAD_LOCAL
)))
4023 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4031 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4034 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4041 /* For each batch of consecutive loadable .note sections,
4042 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4043 because if we link together nonloadable .note sections and
4044 loadable .note sections, we will generate two .note sections
4045 in the output file. FIXME: Using names for section types is
4047 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4049 if ((s
->flags
& SEC_LOAD
) != 0
4050 && CONST_STRNEQ (s
->name
, ".note"))
4055 amt
= sizeof (struct elf_segment_map
);
4056 if (s
->alignment_power
== 2)
4057 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4059 if (s2
->next
->alignment_power
== 2
4060 && (s2
->next
->flags
& SEC_LOAD
) != 0
4061 && CONST_STRNEQ (s2
->next
->name
, ".note")
4062 && align_power (s2
->lma
+ s2
->size
, 2)
4068 amt
+= (count
- 1) * sizeof (asection
*);
4069 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4073 m
->p_type
= PT_NOTE
;
4077 m
->sections
[m
->count
- count
--] = s
;
4078 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4081 m
->sections
[m
->count
- 1] = s
;
4082 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4086 if (s
->flags
& SEC_THREAD_LOCAL
)
4094 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4097 amt
= sizeof (struct elf_segment_map
);
4098 amt
+= (tls_count
- 1) * sizeof (asection
*);
4099 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4104 m
->count
= tls_count
;
4105 /* Mandated PF_R. */
4107 m
->p_flags_valid
= 1;
4108 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4110 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
4111 m
->sections
[i
] = first_tls
;
4112 first_tls
= first_tls
->next
;
4119 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4121 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4122 if (eh_frame_hdr
!= NULL
4123 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4125 amt
= sizeof (struct elf_segment_map
);
4126 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4130 m
->p_type
= PT_GNU_EH_FRAME
;
4132 m
->sections
[0] = eh_frame_hdr
->output_section
;
4138 if (elf_stack_flags (abfd
))
4140 amt
= sizeof (struct elf_segment_map
);
4141 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4145 m
->p_type
= PT_GNU_STACK
;
4146 m
->p_flags
= elf_stack_flags (abfd
);
4147 m
->p_align
= bed
->stack_align
;
4148 m
->p_flags_valid
= 1;
4149 m
->p_align_valid
= m
->p_align
!= 0;
4150 if (info
->stacksize
> 0)
4152 m
->p_size
= info
->stacksize
;
4153 m
->p_size_valid
= 1;
4160 if (info
!= NULL
&& info
->relro
)
4162 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4164 if (m
->p_type
== PT_LOAD
4166 && m
->sections
[0]->vma
>= info
->relro_start
4167 && m
->sections
[0]->vma
< info
->relro_end
)
4170 while (--i
!= (unsigned) -1)
4171 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4172 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4175 if (i
== (unsigned) -1)
4178 if (m
->sections
[i
]->vma
+ m
->sections
[i
]->size
4184 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4187 amt
= sizeof (struct elf_segment_map
);
4188 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4192 m
->p_type
= PT_GNU_RELRO
;
4194 m
->p_flags_valid
= 1;
4202 elf_seg_map (abfd
) = mfirst
;
4205 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4208 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4210 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4215 if (sections
!= NULL
)
4220 /* Sort sections by address. */
4223 elf_sort_sections (const void *arg1
, const void *arg2
)
4225 const asection
*sec1
= *(const asection
**) arg1
;
4226 const asection
*sec2
= *(const asection
**) arg2
;
4227 bfd_size_type size1
, size2
;
4229 /* Sort by LMA first, since this is the address used to
4230 place the section into a segment. */
4231 if (sec1
->lma
< sec2
->lma
)
4233 else if (sec1
->lma
> sec2
->lma
)
4236 /* Then sort by VMA. Normally the LMA and the VMA will be
4237 the same, and this will do nothing. */
4238 if (sec1
->vma
< sec2
->vma
)
4240 else if (sec1
->vma
> sec2
->vma
)
4243 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4245 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4251 /* If the indicies are the same, do not return 0
4252 here, but continue to try the next comparison. */
4253 if (sec1
->target_index
- sec2
->target_index
!= 0)
4254 return sec1
->target_index
- sec2
->target_index
;
4259 else if (TOEND (sec2
))
4264 /* Sort by size, to put zero sized sections
4265 before others at the same address. */
4267 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4268 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4275 return sec1
->target_index
- sec2
->target_index
;
4278 /* Ian Lance Taylor writes:
4280 We shouldn't be using % with a negative signed number. That's just
4281 not good. We have to make sure either that the number is not
4282 negative, or that the number has an unsigned type. When the types
4283 are all the same size they wind up as unsigned. When file_ptr is a
4284 larger signed type, the arithmetic winds up as signed long long,
4287 What we're trying to say here is something like ``increase OFF by
4288 the least amount that will cause it to be equal to the VMA modulo
4290 /* In other words, something like:
4292 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4293 off_offset = off % bed->maxpagesize;
4294 if (vma_offset < off_offset)
4295 adjustment = vma_offset + bed->maxpagesize - off_offset;
4297 adjustment = vma_offset - off_offset;
4299 which can can be collapsed into the expression below. */
4302 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4304 return ((vma
- off
) % maxpagesize
);
4308 print_segment_map (const struct elf_segment_map
*m
)
4311 const char *pt
= get_segment_type (m
->p_type
);
4316 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4317 sprintf (buf
, "LOPROC+%7.7x",
4318 (unsigned int) (m
->p_type
- PT_LOPROC
));
4319 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4320 sprintf (buf
, "LOOS+%7.7x",
4321 (unsigned int) (m
->p_type
- PT_LOOS
));
4323 snprintf (buf
, sizeof (buf
), "%8.8x",
4324 (unsigned int) m
->p_type
);
4328 fprintf (stderr
, "%s:", pt
);
4329 for (j
= 0; j
< m
->count
; j
++)
4330 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4336 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4341 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4343 buf
= bfd_zmalloc (len
);
4346 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4351 /* Assign file positions to the sections based on the mapping from
4352 sections to segments. This function also sets up some fields in
4356 assign_file_positions_for_load_sections (bfd
*abfd
,
4357 struct bfd_link_info
*link_info
)
4359 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4360 struct elf_segment_map
*m
;
4361 Elf_Internal_Phdr
*phdrs
;
4362 Elf_Internal_Phdr
*p
;
4364 bfd_size_type maxpagesize
;
4367 bfd_vma header_pad
= 0;
4369 if (link_info
== NULL
4370 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4374 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4378 header_pad
= m
->header_size
;
4383 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4384 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4388 /* PR binutils/12467. */
4389 elf_elfheader (abfd
)->e_phoff
= 0;
4390 elf_elfheader (abfd
)->e_phentsize
= 0;
4393 elf_elfheader (abfd
)->e_phnum
= alloc
;
4395 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4396 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4398 BFD_ASSERT (elf_program_header_size (abfd
)
4399 >= alloc
* bed
->s
->sizeof_phdr
);
4403 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4407 /* We're writing the size in elf_program_header_size (abfd),
4408 see assign_file_positions_except_relocs, so make sure we have
4409 that amount allocated, with trailing space cleared.
4410 The variable alloc contains the computed need, while
4411 elf_program_header_size (abfd) contains the size used for the
4413 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4414 where the layout is forced to according to a larger size in the
4415 last iterations for the testcase ld-elf/header. */
4416 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4418 phdrs
= (Elf_Internal_Phdr
*)
4420 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4421 sizeof (Elf_Internal_Phdr
));
4422 elf_tdata (abfd
)->phdr
= phdrs
;
4427 if ((abfd
->flags
& D_PAGED
) != 0)
4428 maxpagesize
= bed
->maxpagesize
;
4430 off
= bed
->s
->sizeof_ehdr
;
4431 off
+= alloc
* bed
->s
->sizeof_phdr
;
4432 if (header_pad
< (bfd_vma
) off
)
4438 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4440 m
= m
->next
, p
++, j
++)
4444 bfd_boolean no_contents
;
4446 /* If elf_segment_map is not from map_sections_to_segments, the
4447 sections may not be correctly ordered. NOTE: sorting should
4448 not be done to the PT_NOTE section of a corefile, which may
4449 contain several pseudo-sections artificially created by bfd.
4450 Sorting these pseudo-sections breaks things badly. */
4452 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4453 && m
->p_type
== PT_NOTE
))
4454 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4457 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4458 number of sections with contents contributing to both p_filesz
4459 and p_memsz, followed by a number of sections with no contents
4460 that just contribute to p_memsz. In this loop, OFF tracks next
4461 available file offset for PT_LOAD and PT_NOTE segments. */
4462 p
->p_type
= m
->p_type
;
4463 p
->p_flags
= m
->p_flags
;
4468 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4470 if (m
->p_paddr_valid
)
4471 p
->p_paddr
= m
->p_paddr
;
4472 else if (m
->count
== 0)
4475 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4477 if (p
->p_type
== PT_LOAD
4478 && (abfd
->flags
& D_PAGED
) != 0)
4480 /* p_align in demand paged PT_LOAD segments effectively stores
4481 the maximum page size. When copying an executable with
4482 objcopy, we set m->p_align from the input file. Use this
4483 value for maxpagesize rather than bed->maxpagesize, which
4484 may be different. Note that we use maxpagesize for PT_TLS
4485 segment alignment later in this function, so we are relying
4486 on at least one PT_LOAD segment appearing before a PT_TLS
4488 if (m
->p_align_valid
)
4489 maxpagesize
= m
->p_align
;
4491 p
->p_align
= maxpagesize
;
4493 else if (m
->p_align_valid
)
4494 p
->p_align
= m
->p_align
;
4495 else if (m
->count
== 0)
4496 p
->p_align
= 1 << bed
->s
->log_file_align
;
4500 no_contents
= FALSE
;
4502 if (p
->p_type
== PT_LOAD
4505 bfd_size_type align
;
4506 unsigned int align_power
= 0;
4508 if (m
->p_align_valid
)
4512 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4514 unsigned int secalign
;
4516 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4517 if (secalign
> align_power
)
4518 align_power
= secalign
;
4520 align
= (bfd_size_type
) 1 << align_power
;
4521 if (align
< maxpagesize
)
4522 align
= maxpagesize
;
4525 for (i
= 0; i
< m
->count
; i
++)
4526 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4527 /* If we aren't making room for this section, then
4528 it must be SHT_NOBITS regardless of what we've
4529 set via struct bfd_elf_special_section. */
4530 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4532 /* Find out whether this segment contains any loadable
4535 for (i
= 0; i
< m
->count
; i
++)
4536 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4538 no_contents
= FALSE
;
4542 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4546 /* We shouldn't need to align the segment on disk since
4547 the segment doesn't need file space, but the gABI
4548 arguably requires the alignment and glibc ld.so
4549 checks it. So to comply with the alignment
4550 requirement but not waste file space, we adjust
4551 p_offset for just this segment. (OFF_ADJUST is
4552 subtracted from OFF later.) This may put p_offset
4553 past the end of file, but that shouldn't matter. */
4558 /* Make sure the .dynamic section is the first section in the
4559 PT_DYNAMIC segment. */
4560 else if (p
->p_type
== PT_DYNAMIC
4562 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4565 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4567 bfd_set_error (bfd_error_bad_value
);
4570 /* Set the note section type to SHT_NOTE. */
4571 else if (p
->p_type
== PT_NOTE
)
4572 for (i
= 0; i
< m
->count
; i
++)
4573 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4579 if (m
->includes_filehdr
)
4581 if (!m
->p_flags_valid
)
4583 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4584 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4587 if (p
->p_vaddr
< (bfd_vma
) off
)
4589 (*_bfd_error_handler
)
4590 (_("%B: Not enough room for program headers, try linking with -N"),
4592 bfd_set_error (bfd_error_bad_value
);
4597 if (!m
->p_paddr_valid
)
4602 if (m
->includes_phdrs
)
4604 if (!m
->p_flags_valid
)
4607 if (!m
->includes_filehdr
)
4609 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4613 p
->p_vaddr
-= off
- p
->p_offset
;
4614 if (!m
->p_paddr_valid
)
4615 p
->p_paddr
-= off
- p
->p_offset
;
4619 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4620 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4623 p
->p_filesz
+= header_pad
;
4624 p
->p_memsz
+= header_pad
;
4628 if (p
->p_type
== PT_LOAD
4629 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4631 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4637 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4639 p
->p_filesz
+= adjust
;
4640 p
->p_memsz
+= adjust
;
4644 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4645 maps. Set filepos for sections in PT_LOAD segments, and in
4646 core files, for sections in PT_NOTE segments.
4647 assign_file_positions_for_non_load_sections will set filepos
4648 for other sections and update p_filesz for other segments. */
4649 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4652 bfd_size_type align
;
4653 Elf_Internal_Shdr
*this_hdr
;
4656 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4657 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4659 if ((p
->p_type
== PT_LOAD
4660 || p
->p_type
== PT_TLS
)
4661 && (this_hdr
->sh_type
!= SHT_NOBITS
4662 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4663 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4664 || p
->p_type
== PT_TLS
))))
4666 bfd_vma p_start
= p
->p_paddr
;
4667 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4668 bfd_vma s_start
= sec
->lma
;
4669 bfd_vma adjust
= s_start
- p_end
;
4673 || p_end
< p_start
))
4675 (*_bfd_error_handler
)
4676 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4677 (unsigned long) s_start
, (unsigned long) p_end
);
4681 p
->p_memsz
+= adjust
;
4683 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4685 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4687 /* We have a PROGBITS section following NOBITS ones.
4688 Allocate file space for the NOBITS section(s) and
4690 adjust
= p
->p_memsz
- p
->p_filesz
;
4691 if (!write_zeros (abfd
, off
, adjust
))
4695 p
->p_filesz
+= adjust
;
4699 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4701 /* The section at i == 0 is the one that actually contains
4705 this_hdr
->sh_offset
= sec
->filepos
= off
;
4706 off
+= this_hdr
->sh_size
;
4707 p
->p_filesz
= this_hdr
->sh_size
;
4713 /* The rest are fake sections that shouldn't be written. */
4722 if (p
->p_type
== PT_LOAD
)
4724 this_hdr
->sh_offset
= sec
->filepos
= off
;
4725 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4726 off
+= this_hdr
->sh_size
;
4728 else if (this_hdr
->sh_type
== SHT_NOBITS
4729 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
4730 && this_hdr
->sh_offset
== 0)
4732 /* This is a .tbss section that didn't get a PT_LOAD.
4733 (See _bfd_elf_map_sections_to_segments "Create a
4734 final PT_LOAD".) Set sh_offset to the value it
4735 would have if we had created a zero p_filesz and
4736 p_memsz PT_LOAD header for the section. This
4737 also makes the PT_TLS header have the same
4739 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
4741 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
4744 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4746 p
->p_filesz
+= this_hdr
->sh_size
;
4747 /* A load section without SHF_ALLOC is something like
4748 a note section in a PT_NOTE segment. These take
4749 file space but are not loaded into memory. */
4750 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4751 p
->p_memsz
+= this_hdr
->sh_size
;
4753 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4755 if (p
->p_type
== PT_TLS
)
4756 p
->p_memsz
+= this_hdr
->sh_size
;
4758 /* .tbss is special. It doesn't contribute to p_memsz of
4760 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4761 p
->p_memsz
+= this_hdr
->sh_size
;
4764 if (align
> p
->p_align
4765 && !m
->p_align_valid
4766 && (p
->p_type
!= PT_LOAD
4767 || (abfd
->flags
& D_PAGED
) == 0))
4771 if (!m
->p_flags_valid
)
4774 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4776 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4782 /* Check that all sections are in a PT_LOAD segment.
4783 Don't check funky gdb generated core files. */
4784 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4786 bfd_boolean check_vma
= TRUE
;
4788 for (i
= 1; i
< m
->count
; i
++)
4789 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4790 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4791 ->this_hdr
), p
) != 0
4792 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4793 ->this_hdr
), p
) != 0)
4795 /* Looks like we have overlays packed into the segment. */
4800 for (i
= 0; i
< m
->count
; i
++)
4802 Elf_Internal_Shdr
*this_hdr
;
4805 sec
= m
->sections
[i
];
4806 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4807 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
4808 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
4810 (*_bfd_error_handler
)
4811 (_("%B: section `%A' can't be allocated in segment %d"),
4813 print_segment_map (m
);
4819 elf_next_file_pos (abfd
) = off
;
4823 /* Assign file positions for the other sections. */
4826 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4827 struct bfd_link_info
*link_info
)
4829 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4830 Elf_Internal_Shdr
**i_shdrpp
;
4831 Elf_Internal_Shdr
**hdrpp
;
4832 Elf_Internal_Phdr
*phdrs
;
4833 Elf_Internal_Phdr
*p
;
4834 struct elf_segment_map
*m
;
4835 struct elf_segment_map
*hdrs_segment
;
4836 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4837 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4839 unsigned int num_sec
;
4843 i_shdrpp
= elf_elfsections (abfd
);
4844 num_sec
= elf_numsections (abfd
);
4845 off
= elf_next_file_pos (abfd
);
4846 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4848 Elf_Internal_Shdr
*hdr
;
4851 if (hdr
->bfd_section
!= NULL
4852 && (hdr
->bfd_section
->filepos
!= 0
4853 || (hdr
->sh_type
== SHT_NOBITS
4854 && hdr
->contents
== NULL
)))
4855 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4856 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4858 if (hdr
->sh_size
!= 0)
4859 (*_bfd_error_handler
)
4860 (_("%B: warning: allocated section `%s' not in segment"),
4862 (hdr
->bfd_section
== NULL
4864 : hdr
->bfd_section
->name
));
4865 /* We don't need to page align empty sections. */
4866 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4867 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4870 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4872 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4875 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4876 && hdr
->bfd_section
== NULL
)
4877 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
4878 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
4879 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)])
4880 hdr
->sh_offset
= -1;
4882 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4885 /* Now that we have set the section file positions, we can set up
4886 the file positions for the non PT_LOAD segments. */
4890 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4892 hdrs_segment
= NULL
;
4893 phdrs
= elf_tdata (abfd
)->phdr
;
4894 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
4897 if (p
->p_type
!= PT_LOAD
)
4900 if (m
->includes_filehdr
)
4902 filehdr_vaddr
= p
->p_vaddr
;
4903 filehdr_paddr
= p
->p_paddr
;
4905 if (m
->includes_phdrs
)
4907 phdrs_vaddr
= p
->p_vaddr
;
4908 phdrs_paddr
= p
->p_paddr
;
4909 if (m
->includes_filehdr
)
4912 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4913 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4918 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
4920 /* There is a segment that contains both the file headers and the
4921 program headers, so provide a symbol __ehdr_start pointing there.
4922 A program can use this to examine itself robustly. */
4924 struct elf_link_hash_entry
*hash
4925 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
4926 FALSE
, FALSE
, TRUE
);
4927 /* If the symbol was referenced and not defined, define it. */
4929 && (hash
->root
.type
== bfd_link_hash_new
4930 || hash
->root
.type
== bfd_link_hash_undefined
4931 || hash
->root
.type
== bfd_link_hash_undefweak
4932 || hash
->root
.type
== bfd_link_hash_common
))
4935 if (hdrs_segment
->count
!= 0)
4936 /* The segment contains sections, so use the first one. */
4937 s
= hdrs_segment
->sections
[0];
4939 /* Use the first (i.e. lowest-addressed) section in any segment. */
4940 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4949 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
4950 hash
->root
.u
.def
.section
= s
;
4954 hash
->root
.u
.def
.value
= filehdr_vaddr
;
4955 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
4958 hash
->root
.type
= bfd_link_hash_defined
;
4959 hash
->def_regular
= 1;
4964 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
4966 if (p
->p_type
== PT_GNU_RELRO
)
4968 const Elf_Internal_Phdr
*lp
;
4969 struct elf_segment_map
*lm
;
4971 if (link_info
!= NULL
)
4973 /* During linking the range of the RELRO segment is passed
4975 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
4977 lm
= lm
->next
, lp
++)
4979 if (lp
->p_type
== PT_LOAD
4980 && lp
->p_vaddr
< link_info
->relro_end
4981 && lp
->p_vaddr
+ lp
->p_filesz
>= link_info
->relro_end
4983 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
4987 /* PR ld/14207. If the RELRO segment doesn't fit in the
4988 LOAD segment, it should be removed. */
4989 BFD_ASSERT (lm
!= NULL
);
4993 /* Otherwise we are copying an executable or shared
4994 library, but we need to use the same linker logic. */
4995 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4997 if (lp
->p_type
== PT_LOAD
4998 && lp
->p_paddr
== p
->p_paddr
)
5003 if (lp
< phdrs
+ count
)
5005 p
->p_vaddr
= lp
->p_vaddr
;
5006 p
->p_paddr
= lp
->p_paddr
;
5007 p
->p_offset
= lp
->p_offset
;
5008 if (link_info
!= NULL
)
5009 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5010 else if (m
->p_size_valid
)
5011 p
->p_filesz
= m
->p_size
;
5014 p
->p_memsz
= p
->p_filesz
;
5015 /* Preserve the alignment and flags if they are valid. The
5016 gold linker generates RW/4 for the PT_GNU_RELRO section.
5017 It is better for objcopy/strip to honor these attributes
5018 otherwise gdb will choke when using separate debug files.
5020 if (!m
->p_align_valid
)
5022 if (!m
->p_flags_valid
)
5023 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5027 memset (p
, 0, sizeof *p
);
5028 p
->p_type
= PT_NULL
;
5031 else if (p
->p_type
== PT_GNU_STACK
)
5033 if (m
->p_size_valid
)
5034 p
->p_memsz
= m
->p_size
;
5036 else if (m
->count
!= 0)
5038 if (p
->p_type
!= PT_LOAD
5039 && (p
->p_type
!= PT_NOTE
5040 || bfd_get_format (abfd
) != bfd_core
))
5042 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
5045 p
->p_offset
= m
->sections
[0]->filepos
;
5046 for (i
= m
->count
; i
-- != 0;)
5048 asection
*sect
= m
->sections
[i
];
5049 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5050 if (hdr
->sh_type
!= SHT_NOBITS
)
5052 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5059 else if (m
->includes_filehdr
)
5061 p
->p_vaddr
= filehdr_vaddr
;
5062 if (! m
->p_paddr_valid
)
5063 p
->p_paddr
= filehdr_paddr
;
5065 else if (m
->includes_phdrs
)
5067 p
->p_vaddr
= phdrs_vaddr
;
5068 if (! m
->p_paddr_valid
)
5069 p
->p_paddr
= phdrs_paddr
;
5073 elf_next_file_pos (abfd
) = off
;
5078 /* Work out the file positions of all the sections. This is called by
5079 _bfd_elf_compute_section_file_positions. All the section sizes and
5080 VMAs must be known before this is called.
5082 Reloc sections come in two flavours: Those processed specially as
5083 "side-channel" data attached to a section to which they apply, and
5084 those that bfd doesn't process as relocations. The latter sort are
5085 stored in a normal bfd section by bfd_section_from_shdr. We don't
5086 consider the former sort here, unless they form part of the loadable
5087 image. Reloc sections not assigned here will be handled later by
5088 assign_file_positions_for_relocs.
5090 We also don't set the positions of the .symtab and .strtab here. */
5093 assign_file_positions_except_relocs (bfd
*abfd
,
5094 struct bfd_link_info
*link_info
)
5096 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5097 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5099 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5101 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5102 && bfd_get_format (abfd
) != bfd_core
)
5104 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5105 unsigned int num_sec
= elf_numsections (abfd
);
5106 Elf_Internal_Shdr
**hdrpp
;
5109 /* Start after the ELF header. */
5110 off
= i_ehdrp
->e_ehsize
;
5112 /* We are not creating an executable, which means that we are
5113 not creating a program header, and that the actual order of
5114 the sections in the file is unimportant. */
5115 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5117 Elf_Internal_Shdr
*hdr
;
5120 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5121 && hdr
->bfd_section
== NULL
)
5122 || i
== elf_onesymtab (abfd
)
5123 || i
== elf_symtab_shndx (abfd
)
5124 || i
== elf_strtab_sec (abfd
))
5126 hdr
->sh_offset
= -1;
5129 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5136 /* Assign file positions for the loaded sections based on the
5137 assignment of sections to segments. */
5138 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5141 /* And for non-load sections. */
5142 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5145 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5147 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5151 /* Write out the program headers. */
5152 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5153 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5154 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5157 off
= elf_next_file_pos (abfd
);
5160 /* Place the section headers. */
5161 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5162 i_ehdrp
->e_shoff
= off
;
5163 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5165 elf_next_file_pos (abfd
) = off
;
5171 prep_headers (bfd
*abfd
)
5173 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5174 struct elf_strtab_hash
*shstrtab
;
5175 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5177 i_ehdrp
= elf_elfheader (abfd
);
5179 shstrtab
= _bfd_elf_strtab_init ();
5180 if (shstrtab
== NULL
)
5183 elf_shstrtab (abfd
) = shstrtab
;
5185 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5186 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5187 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5188 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5190 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5191 i_ehdrp
->e_ident
[EI_DATA
] =
5192 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5193 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5195 if ((abfd
->flags
& DYNAMIC
) != 0)
5196 i_ehdrp
->e_type
= ET_DYN
;
5197 else if ((abfd
->flags
& EXEC_P
) != 0)
5198 i_ehdrp
->e_type
= ET_EXEC
;
5199 else if (bfd_get_format (abfd
) == bfd_core
)
5200 i_ehdrp
->e_type
= ET_CORE
;
5202 i_ehdrp
->e_type
= ET_REL
;
5204 switch (bfd_get_arch (abfd
))
5206 case bfd_arch_unknown
:
5207 i_ehdrp
->e_machine
= EM_NONE
;
5210 /* There used to be a long list of cases here, each one setting
5211 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5212 in the corresponding bfd definition. To avoid duplication,
5213 the switch was removed. Machines that need special handling
5214 can generally do it in elf_backend_final_write_processing(),
5215 unless they need the information earlier than the final write.
5216 Such need can generally be supplied by replacing the tests for
5217 e_machine with the conditions used to determine it. */
5219 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5222 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5223 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5225 /* No program header, for now. */
5226 i_ehdrp
->e_phoff
= 0;
5227 i_ehdrp
->e_phentsize
= 0;
5228 i_ehdrp
->e_phnum
= 0;
5230 /* Each bfd section is section header entry. */
5231 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5232 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5234 /* If we're building an executable, we'll need a program header table. */
5235 if (abfd
->flags
& EXEC_P
)
5236 /* It all happens later. */
5240 i_ehdrp
->e_phentsize
= 0;
5241 i_ehdrp
->e_phoff
= 0;
5244 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5245 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5246 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5247 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5248 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5249 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5250 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5251 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5252 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5258 /* Assign file positions for all the reloc sections which are not part
5259 of the loadable file image. */
5262 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5265 unsigned int i
, num_sec
;
5266 Elf_Internal_Shdr
**shdrpp
;
5268 off
= elf_next_file_pos (abfd
);
5270 num_sec
= elf_numsections (abfd
);
5271 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5273 Elf_Internal_Shdr
*shdrp
;
5276 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5277 && shdrp
->sh_offset
== -1)
5278 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5281 elf_next_file_pos (abfd
) = off
;
5285 _bfd_elf_write_object_contents (bfd
*abfd
)
5287 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5288 Elf_Internal_Shdr
**i_shdrp
;
5290 unsigned int count
, num_sec
;
5291 struct elf_obj_tdata
*t
;
5293 if (! abfd
->output_has_begun
5294 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5297 i_shdrp
= elf_elfsections (abfd
);
5300 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5304 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5306 /* After writing the headers, we need to write the sections too... */
5307 num_sec
= elf_numsections (abfd
);
5308 for (count
= 1; count
< num_sec
; count
++)
5310 if (bed
->elf_backend_section_processing
)
5311 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5312 if (i_shdrp
[count
]->contents
)
5314 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5316 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5317 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5322 /* Write out the section header names. */
5323 t
= elf_tdata (abfd
);
5324 if (elf_shstrtab (abfd
) != NULL
5325 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5326 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5329 if (bed
->elf_backend_final_write_processing
)
5330 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5332 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5335 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5336 if (t
->build_id
!= NULL
5337 && t
->build_id
->u
.o
.zero
== 0)
5338 return (*t
->build_id
->u
.o
.after_write_object_contents
) (abfd
);
5344 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5346 /* Hopefully this can be done just like an object file. */
5347 return _bfd_elf_write_object_contents (abfd
);
5350 /* Given a section, search the header to find them. */
5353 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5355 const struct elf_backend_data
*bed
;
5356 unsigned int sec_index
;
5358 if (elf_section_data (asect
) != NULL
5359 && elf_section_data (asect
)->this_idx
!= 0)
5360 return elf_section_data (asect
)->this_idx
;
5362 if (bfd_is_abs_section (asect
))
5363 sec_index
= SHN_ABS
;
5364 else if (bfd_is_com_section (asect
))
5365 sec_index
= SHN_COMMON
;
5366 else if (bfd_is_und_section (asect
))
5367 sec_index
= SHN_UNDEF
;
5369 sec_index
= SHN_BAD
;
5371 bed
= get_elf_backend_data (abfd
);
5372 if (bed
->elf_backend_section_from_bfd_section
)
5374 int retval
= sec_index
;
5376 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5380 if (sec_index
== SHN_BAD
)
5381 bfd_set_error (bfd_error_nonrepresentable_section
);
5386 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5390 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5392 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5394 flagword flags
= asym_ptr
->flags
;
5396 /* When gas creates relocations against local labels, it creates its
5397 own symbol for the section, but does put the symbol into the
5398 symbol chain, so udata is 0. When the linker is generating
5399 relocatable output, this section symbol may be for one of the
5400 input sections rather than the output section. */
5401 if (asym_ptr
->udata
.i
== 0
5402 && (flags
& BSF_SECTION_SYM
)
5403 && asym_ptr
->section
)
5408 sec
= asym_ptr
->section
;
5409 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5410 sec
= sec
->output_section
;
5411 if (sec
->owner
== abfd
5412 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5413 && elf_section_syms (abfd
)[indx
] != NULL
)
5414 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5417 idx
= asym_ptr
->udata
.i
;
5421 /* This case can occur when using --strip-symbol on a symbol
5422 which is used in a relocation entry. */
5423 (*_bfd_error_handler
)
5424 (_("%B: symbol `%s' required but not present"),
5425 abfd
, bfd_asymbol_name (asym_ptr
));
5426 bfd_set_error (bfd_error_no_symbols
);
5433 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5434 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5442 /* Rewrite program header information. */
5445 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5447 Elf_Internal_Ehdr
*iehdr
;
5448 struct elf_segment_map
*map
;
5449 struct elf_segment_map
*map_first
;
5450 struct elf_segment_map
**pointer_to_map
;
5451 Elf_Internal_Phdr
*segment
;
5454 unsigned int num_segments
;
5455 bfd_boolean phdr_included
= FALSE
;
5456 bfd_boolean p_paddr_valid
;
5457 bfd_vma maxpagesize
;
5458 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5459 unsigned int phdr_adjust_num
= 0;
5460 const struct elf_backend_data
*bed
;
5462 bed
= get_elf_backend_data (ibfd
);
5463 iehdr
= elf_elfheader (ibfd
);
5466 pointer_to_map
= &map_first
;
5468 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5469 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5471 /* Returns the end address of the segment + 1. */
5472 #define SEGMENT_END(segment, start) \
5473 (start + (segment->p_memsz > segment->p_filesz \
5474 ? segment->p_memsz : segment->p_filesz))
5476 #define SECTION_SIZE(section, segment) \
5477 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5478 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5479 ? section->size : 0)
5481 /* Returns TRUE if the given section is contained within
5482 the given segment. VMA addresses are compared. */
5483 #define IS_CONTAINED_BY_VMA(section, segment) \
5484 (section->vma >= segment->p_vaddr \
5485 && (section->vma + SECTION_SIZE (section, segment) \
5486 <= (SEGMENT_END (segment, segment->p_vaddr))))
5488 /* Returns TRUE if the given section is contained within
5489 the given segment. LMA addresses are compared. */
5490 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5491 (section->lma >= base \
5492 && (section->lma + SECTION_SIZE (section, segment) \
5493 <= SEGMENT_END (segment, base)))
5495 /* Handle PT_NOTE segment. */
5496 #define IS_NOTE(p, s) \
5497 (p->p_type == PT_NOTE \
5498 && elf_section_type (s) == SHT_NOTE \
5499 && (bfd_vma) s->filepos >= p->p_offset \
5500 && ((bfd_vma) s->filepos + s->size \
5501 <= p->p_offset + p->p_filesz))
5503 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5505 #define IS_COREFILE_NOTE(p, s) \
5507 && bfd_get_format (ibfd) == bfd_core \
5511 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5512 linker, which generates a PT_INTERP section with p_vaddr and
5513 p_memsz set to 0. */
5514 #define IS_SOLARIS_PT_INTERP(p, s) \
5516 && p->p_paddr == 0 \
5517 && p->p_memsz == 0 \
5518 && p->p_filesz > 0 \
5519 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5521 && (bfd_vma) s->filepos >= p->p_offset \
5522 && ((bfd_vma) s->filepos + s->size \
5523 <= p->p_offset + p->p_filesz))
5525 /* Decide if the given section should be included in the given segment.
5526 A section will be included if:
5527 1. It is within the address space of the segment -- we use the LMA
5528 if that is set for the segment and the VMA otherwise,
5529 2. It is an allocated section or a NOTE section in a PT_NOTE
5531 3. There is an output section associated with it,
5532 4. The section has not already been allocated to a previous segment.
5533 5. PT_GNU_STACK segments do not include any sections.
5534 6. PT_TLS segment includes only SHF_TLS sections.
5535 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5536 8. PT_DYNAMIC should not contain empty sections at the beginning
5537 (with the possible exception of .dynamic). */
5538 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5539 ((((segment->p_paddr \
5540 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5541 : IS_CONTAINED_BY_VMA (section, segment)) \
5542 && (section->flags & SEC_ALLOC) != 0) \
5543 || IS_NOTE (segment, section)) \
5544 && segment->p_type != PT_GNU_STACK \
5545 && (segment->p_type != PT_TLS \
5546 || (section->flags & SEC_THREAD_LOCAL)) \
5547 && (segment->p_type == PT_LOAD \
5548 || segment->p_type == PT_TLS \
5549 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5550 && (segment->p_type != PT_DYNAMIC \
5551 || SECTION_SIZE (section, segment) > 0 \
5552 || (segment->p_paddr \
5553 ? segment->p_paddr != section->lma \
5554 : segment->p_vaddr != section->vma) \
5555 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5557 && !section->segment_mark)
5559 /* If the output section of a section in the input segment is NULL,
5560 it is removed from the corresponding output segment. */
5561 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5562 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5563 && section->output_section != NULL)
5565 /* Returns TRUE iff seg1 starts after the end of seg2. */
5566 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5567 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5569 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5570 their VMA address ranges and their LMA address ranges overlap.
5571 It is possible to have overlapping VMA ranges without overlapping LMA
5572 ranges. RedBoot images for example can have both .data and .bss mapped
5573 to the same VMA range, but with the .data section mapped to a different
5575 #define SEGMENT_OVERLAPS(seg1, seg2) \
5576 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5577 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5578 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5579 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5581 /* Initialise the segment mark field. */
5582 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5583 section
->segment_mark
= FALSE
;
5585 /* The Solaris linker creates program headers in which all the
5586 p_paddr fields are zero. When we try to objcopy or strip such a
5587 file, we get confused. Check for this case, and if we find it
5588 don't set the p_paddr_valid fields. */
5589 p_paddr_valid
= FALSE
;
5590 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5593 if (segment
->p_paddr
!= 0)
5595 p_paddr_valid
= TRUE
;
5599 /* Scan through the segments specified in the program header
5600 of the input BFD. For this first scan we look for overlaps
5601 in the loadable segments. These can be created by weird
5602 parameters to objcopy. Also, fix some solaris weirdness. */
5603 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5608 Elf_Internal_Phdr
*segment2
;
5610 if (segment
->p_type
== PT_INTERP
)
5611 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5612 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5614 /* Mininal change so that the normal section to segment
5615 assignment code will work. */
5616 segment
->p_vaddr
= section
->vma
;
5620 if (segment
->p_type
!= PT_LOAD
)
5622 /* Remove PT_GNU_RELRO segment. */
5623 if (segment
->p_type
== PT_GNU_RELRO
)
5624 segment
->p_type
= PT_NULL
;
5628 /* Determine if this segment overlaps any previous segments. */
5629 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5631 bfd_signed_vma extra_length
;
5633 if (segment2
->p_type
!= PT_LOAD
5634 || !SEGMENT_OVERLAPS (segment
, segment2
))
5637 /* Merge the two segments together. */
5638 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5640 /* Extend SEGMENT2 to include SEGMENT and then delete
5642 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5643 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5645 if (extra_length
> 0)
5647 segment2
->p_memsz
+= extra_length
;
5648 segment2
->p_filesz
+= extra_length
;
5651 segment
->p_type
= PT_NULL
;
5653 /* Since we have deleted P we must restart the outer loop. */
5655 segment
= elf_tdata (ibfd
)->phdr
;
5660 /* Extend SEGMENT to include SEGMENT2 and then delete
5662 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5663 - SEGMENT_END (segment
, segment
->p_vaddr
));
5665 if (extra_length
> 0)
5667 segment
->p_memsz
+= extra_length
;
5668 segment
->p_filesz
+= extra_length
;
5671 segment2
->p_type
= PT_NULL
;
5676 /* The second scan attempts to assign sections to segments. */
5677 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5681 unsigned int section_count
;
5682 asection
**sections
;
5683 asection
*output_section
;
5685 bfd_vma matching_lma
;
5686 bfd_vma suggested_lma
;
5689 asection
*first_section
;
5690 bfd_boolean first_matching_lma
;
5691 bfd_boolean first_suggested_lma
;
5693 if (segment
->p_type
== PT_NULL
)
5696 first_section
= NULL
;
5697 /* Compute how many sections might be placed into this segment. */
5698 for (section
= ibfd
->sections
, section_count
= 0;
5700 section
= section
->next
)
5702 /* Find the first section in the input segment, which may be
5703 removed from the corresponding output segment. */
5704 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5706 if (first_section
== NULL
)
5707 first_section
= section
;
5708 if (section
->output_section
!= NULL
)
5713 /* Allocate a segment map big enough to contain
5714 all of the sections we have selected. */
5715 amt
= sizeof (struct elf_segment_map
);
5716 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5717 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5721 /* Initialise the fields of the segment map. Default to
5722 using the physical address of the segment in the input BFD. */
5724 map
->p_type
= segment
->p_type
;
5725 map
->p_flags
= segment
->p_flags
;
5726 map
->p_flags_valid
= 1;
5728 /* If the first section in the input segment is removed, there is
5729 no need to preserve segment physical address in the corresponding
5731 if (!first_section
|| first_section
->output_section
!= NULL
)
5733 map
->p_paddr
= segment
->p_paddr
;
5734 map
->p_paddr_valid
= p_paddr_valid
;
5737 /* Determine if this segment contains the ELF file header
5738 and if it contains the program headers themselves. */
5739 map
->includes_filehdr
= (segment
->p_offset
== 0
5740 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5741 map
->includes_phdrs
= 0;
5743 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5745 map
->includes_phdrs
=
5746 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5747 && (segment
->p_offset
+ segment
->p_filesz
5748 >= ((bfd_vma
) iehdr
->e_phoff
5749 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5751 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5752 phdr_included
= TRUE
;
5755 if (section_count
== 0)
5757 /* Special segments, such as the PT_PHDR segment, may contain
5758 no sections, but ordinary, loadable segments should contain
5759 something. They are allowed by the ELF spec however, so only
5760 a warning is produced. */
5761 if (segment
->p_type
== PT_LOAD
)
5762 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5763 " detected, is this intentional ?\n"),
5767 *pointer_to_map
= map
;
5768 pointer_to_map
= &map
->next
;
5773 /* Now scan the sections in the input BFD again and attempt
5774 to add their corresponding output sections to the segment map.
5775 The problem here is how to handle an output section which has
5776 been moved (ie had its LMA changed). There are four possibilities:
5778 1. None of the sections have been moved.
5779 In this case we can continue to use the segment LMA from the
5782 2. All of the sections have been moved by the same amount.
5783 In this case we can change the segment's LMA to match the LMA
5784 of the first section.
5786 3. Some of the sections have been moved, others have not.
5787 In this case those sections which have not been moved can be
5788 placed in the current segment which will have to have its size,
5789 and possibly its LMA changed, and a new segment or segments will
5790 have to be created to contain the other sections.
5792 4. The sections have been moved, but not by the same amount.
5793 In this case we can change the segment's LMA to match the LMA
5794 of the first section and we will have to create a new segment
5795 or segments to contain the other sections.
5797 In order to save time, we allocate an array to hold the section
5798 pointers that we are interested in. As these sections get assigned
5799 to a segment, they are removed from this array. */
5801 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5802 if (sections
== NULL
)
5805 /* Step One: Scan for segment vs section LMA conflicts.
5806 Also add the sections to the section array allocated above.
5807 Also add the sections to the current segment. In the common
5808 case, where the sections have not been moved, this means that
5809 we have completely filled the segment, and there is nothing
5814 first_matching_lma
= TRUE
;
5815 first_suggested_lma
= TRUE
;
5817 for (section
= ibfd
->sections
;
5819 section
= section
->next
)
5820 if (section
== first_section
)
5823 for (j
= 0; section
!= NULL
; section
= section
->next
)
5825 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5827 output_section
= section
->output_section
;
5829 sections
[j
++] = section
;
5831 /* The Solaris native linker always sets p_paddr to 0.
5832 We try to catch that case here, and set it to the
5833 correct value. Note - some backends require that
5834 p_paddr be left as zero. */
5836 && segment
->p_vaddr
!= 0
5837 && !bed
->want_p_paddr_set_to_zero
5839 && output_section
->lma
!= 0
5840 && output_section
->vma
== (segment
->p_vaddr
5841 + (map
->includes_filehdr
5844 + (map
->includes_phdrs
5846 * iehdr
->e_phentsize
)
5848 map
->p_paddr
= segment
->p_vaddr
;
5850 /* Match up the physical address of the segment with the
5851 LMA address of the output section. */
5852 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5853 || IS_COREFILE_NOTE (segment
, section
)
5854 || (bed
->want_p_paddr_set_to_zero
5855 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5857 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5859 matching_lma
= output_section
->lma
;
5860 first_matching_lma
= FALSE
;
5863 /* We assume that if the section fits within the segment
5864 then it does not overlap any other section within that
5866 map
->sections
[isec
++] = output_section
;
5868 else if (first_suggested_lma
)
5870 suggested_lma
= output_section
->lma
;
5871 first_suggested_lma
= FALSE
;
5874 if (j
== section_count
)
5879 BFD_ASSERT (j
== section_count
);
5881 /* Step Two: Adjust the physical address of the current segment,
5883 if (isec
== section_count
)
5885 /* All of the sections fitted within the segment as currently
5886 specified. This is the default case. Add the segment to
5887 the list of built segments and carry on to process the next
5888 program header in the input BFD. */
5889 map
->count
= section_count
;
5890 *pointer_to_map
= map
;
5891 pointer_to_map
= &map
->next
;
5894 && !bed
->want_p_paddr_set_to_zero
5895 && matching_lma
!= map
->p_paddr
5896 && !map
->includes_filehdr
5897 && !map
->includes_phdrs
)
5898 /* There is some padding before the first section in the
5899 segment. So, we must account for that in the output
5901 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5908 if (!first_matching_lma
)
5910 /* At least one section fits inside the current segment.
5911 Keep it, but modify its physical address to match the
5912 LMA of the first section that fitted. */
5913 map
->p_paddr
= matching_lma
;
5917 /* None of the sections fitted inside the current segment.
5918 Change the current segment's physical address to match
5919 the LMA of the first section. */
5920 map
->p_paddr
= suggested_lma
;
5923 /* Offset the segment physical address from the lma
5924 to allow for space taken up by elf headers. */
5925 if (map
->includes_filehdr
)
5927 if (map
->p_paddr
>= iehdr
->e_ehsize
)
5928 map
->p_paddr
-= iehdr
->e_ehsize
;
5931 map
->includes_filehdr
= FALSE
;
5932 map
->includes_phdrs
= FALSE
;
5936 if (map
->includes_phdrs
)
5938 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
5940 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5942 /* iehdr->e_phnum is just an estimate of the number
5943 of program headers that we will need. Make a note
5944 here of the number we used and the segment we chose
5945 to hold these headers, so that we can adjust the
5946 offset when we know the correct value. */
5947 phdr_adjust_num
= iehdr
->e_phnum
;
5948 phdr_adjust_seg
= map
;
5951 map
->includes_phdrs
= FALSE
;
5955 /* Step Three: Loop over the sections again, this time assigning
5956 those that fit to the current segment and removing them from the
5957 sections array; but making sure not to leave large gaps. Once all
5958 possible sections have been assigned to the current segment it is
5959 added to the list of built segments and if sections still remain
5960 to be assigned, a new segment is constructed before repeating
5967 first_suggested_lma
= TRUE
;
5969 /* Fill the current segment with sections that fit. */
5970 for (j
= 0; j
< section_count
; j
++)
5972 section
= sections
[j
];
5974 if (section
== NULL
)
5977 output_section
= section
->output_section
;
5979 BFD_ASSERT (output_section
!= NULL
);
5981 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5982 || IS_COREFILE_NOTE (segment
, section
))
5984 if (map
->count
== 0)
5986 /* If the first section in a segment does not start at
5987 the beginning of the segment, then something is
5989 if (output_section
->lma
5991 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5992 + (map
->includes_phdrs
5993 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6001 prev_sec
= map
->sections
[map
->count
- 1];
6003 /* If the gap between the end of the previous section
6004 and the start of this section is more than
6005 maxpagesize then we need to start a new segment. */
6006 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6008 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6009 || (prev_sec
->lma
+ prev_sec
->size
6010 > output_section
->lma
))
6012 if (first_suggested_lma
)
6014 suggested_lma
= output_section
->lma
;
6015 first_suggested_lma
= FALSE
;
6022 map
->sections
[map
->count
++] = output_section
;
6025 section
->segment_mark
= TRUE
;
6027 else if (first_suggested_lma
)
6029 suggested_lma
= output_section
->lma
;
6030 first_suggested_lma
= FALSE
;
6034 BFD_ASSERT (map
->count
> 0);
6036 /* Add the current segment to the list of built segments. */
6037 *pointer_to_map
= map
;
6038 pointer_to_map
= &map
->next
;
6040 if (isec
< section_count
)
6042 /* We still have not allocated all of the sections to
6043 segments. Create a new segment here, initialise it
6044 and carry on looping. */
6045 amt
= sizeof (struct elf_segment_map
);
6046 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6047 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6054 /* Initialise the fields of the segment map. Set the physical
6055 physical address to the LMA of the first section that has
6056 not yet been assigned. */
6058 map
->p_type
= segment
->p_type
;
6059 map
->p_flags
= segment
->p_flags
;
6060 map
->p_flags_valid
= 1;
6061 map
->p_paddr
= suggested_lma
;
6062 map
->p_paddr_valid
= p_paddr_valid
;
6063 map
->includes_filehdr
= 0;
6064 map
->includes_phdrs
= 0;
6067 while (isec
< section_count
);
6072 elf_seg_map (obfd
) = map_first
;
6074 /* If we had to estimate the number of program headers that were
6075 going to be needed, then check our estimate now and adjust
6076 the offset if necessary. */
6077 if (phdr_adjust_seg
!= NULL
)
6081 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6084 if (count
> phdr_adjust_num
)
6085 phdr_adjust_seg
->p_paddr
6086 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6091 #undef IS_CONTAINED_BY_VMA
6092 #undef IS_CONTAINED_BY_LMA
6094 #undef IS_COREFILE_NOTE
6095 #undef IS_SOLARIS_PT_INTERP
6096 #undef IS_SECTION_IN_INPUT_SEGMENT
6097 #undef INCLUDE_SECTION_IN_SEGMENT
6098 #undef SEGMENT_AFTER_SEGMENT
6099 #undef SEGMENT_OVERLAPS
6103 /* Copy ELF program header information. */
6106 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6108 Elf_Internal_Ehdr
*iehdr
;
6109 struct elf_segment_map
*map
;
6110 struct elf_segment_map
*map_first
;
6111 struct elf_segment_map
**pointer_to_map
;
6112 Elf_Internal_Phdr
*segment
;
6114 unsigned int num_segments
;
6115 bfd_boolean phdr_included
= FALSE
;
6116 bfd_boolean p_paddr_valid
;
6118 iehdr
= elf_elfheader (ibfd
);
6121 pointer_to_map
= &map_first
;
6123 /* If all the segment p_paddr fields are zero, don't set
6124 map->p_paddr_valid. */
6125 p_paddr_valid
= FALSE
;
6126 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6127 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6130 if (segment
->p_paddr
!= 0)
6132 p_paddr_valid
= TRUE
;
6136 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6141 unsigned int section_count
;
6143 Elf_Internal_Shdr
*this_hdr
;
6144 asection
*first_section
= NULL
;
6145 asection
*lowest_section
;
6147 /* Compute how many sections are in this segment. */
6148 for (section
= ibfd
->sections
, section_count
= 0;
6150 section
= section
->next
)
6152 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6153 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6155 if (first_section
== NULL
)
6156 first_section
= section
;
6161 /* Allocate a segment map big enough to contain
6162 all of the sections we have selected. */
6163 amt
= sizeof (struct elf_segment_map
);
6164 if (section_count
!= 0)
6165 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6166 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6170 /* Initialize the fields of the output segment map with the
6173 map
->p_type
= segment
->p_type
;
6174 map
->p_flags
= segment
->p_flags
;
6175 map
->p_flags_valid
= 1;
6176 map
->p_paddr
= segment
->p_paddr
;
6177 map
->p_paddr_valid
= p_paddr_valid
;
6178 map
->p_align
= segment
->p_align
;
6179 map
->p_align_valid
= 1;
6180 map
->p_vaddr_offset
= 0;
6182 if (map
->p_type
== PT_GNU_RELRO
6183 || map
->p_type
== PT_GNU_STACK
)
6185 /* The PT_GNU_RELRO segment may contain the first a few
6186 bytes in the .got.plt section even if the whole .got.plt
6187 section isn't in the PT_GNU_RELRO segment. We won't
6188 change the size of the PT_GNU_RELRO segment.
6189 Similarly, PT_GNU_STACK size is significant on uclinux
6191 map
->p_size
= segment
->p_memsz
;
6192 map
->p_size_valid
= 1;
6195 /* Determine if this segment contains the ELF file header
6196 and if it contains the program headers themselves. */
6197 map
->includes_filehdr
= (segment
->p_offset
== 0
6198 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6200 map
->includes_phdrs
= 0;
6201 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6203 map
->includes_phdrs
=
6204 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6205 && (segment
->p_offset
+ segment
->p_filesz
6206 >= ((bfd_vma
) iehdr
->e_phoff
6207 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6209 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6210 phdr_included
= TRUE
;
6213 lowest_section
= first_section
;
6214 if (section_count
!= 0)
6216 unsigned int isec
= 0;
6218 for (section
= first_section
;
6220 section
= section
->next
)
6222 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6223 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6225 map
->sections
[isec
++] = section
->output_section
;
6226 if (section
->lma
< lowest_section
->lma
)
6227 lowest_section
= section
;
6228 if ((section
->flags
& SEC_ALLOC
) != 0)
6232 /* Section lmas are set up from PT_LOAD header
6233 p_paddr in _bfd_elf_make_section_from_shdr.
6234 If this header has a p_paddr that disagrees
6235 with the section lma, flag the p_paddr as
6237 if ((section
->flags
& SEC_LOAD
) != 0)
6238 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6240 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6241 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6242 map
->p_paddr_valid
= FALSE
;
6244 if (isec
== section_count
)
6250 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6251 /* We need to keep the space used by the headers fixed. */
6252 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6254 if (!map
->includes_phdrs
6255 && !map
->includes_filehdr
6256 && map
->p_paddr_valid
)
6257 /* There is some other padding before the first section. */
6258 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6259 - segment
->p_paddr
);
6261 map
->count
= section_count
;
6262 *pointer_to_map
= map
;
6263 pointer_to_map
= &map
->next
;
6266 elf_seg_map (obfd
) = map_first
;
6270 /* Copy private BFD data. This copies or rewrites ELF program header
6274 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6276 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6277 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6280 if (elf_tdata (ibfd
)->phdr
== NULL
)
6283 if (ibfd
->xvec
== obfd
->xvec
)
6285 /* Check to see if any sections in the input BFD
6286 covered by ELF program header have changed. */
6287 Elf_Internal_Phdr
*segment
;
6288 asection
*section
, *osec
;
6289 unsigned int i
, num_segments
;
6290 Elf_Internal_Shdr
*this_hdr
;
6291 const struct elf_backend_data
*bed
;
6293 bed
= get_elf_backend_data (ibfd
);
6295 /* Regenerate the segment map if p_paddr is set to 0. */
6296 if (bed
->want_p_paddr_set_to_zero
)
6299 /* Initialize the segment mark field. */
6300 for (section
= obfd
->sections
; section
!= NULL
;
6301 section
= section
->next
)
6302 section
->segment_mark
= FALSE
;
6304 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6305 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6309 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6310 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6311 which severly confuses things, so always regenerate the segment
6312 map in this case. */
6313 if (segment
->p_paddr
== 0
6314 && segment
->p_memsz
== 0
6315 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6318 for (section
= ibfd
->sections
;
6319 section
!= NULL
; section
= section
->next
)
6321 /* We mark the output section so that we know it comes
6322 from the input BFD. */
6323 osec
= section
->output_section
;
6325 osec
->segment_mark
= TRUE
;
6327 /* Check if this section is covered by the segment. */
6328 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6329 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6331 /* FIXME: Check if its output section is changed or
6332 removed. What else do we need to check? */
6334 || section
->flags
!= osec
->flags
6335 || section
->lma
!= osec
->lma
6336 || section
->vma
!= osec
->vma
6337 || section
->size
!= osec
->size
6338 || section
->rawsize
!= osec
->rawsize
6339 || section
->alignment_power
!= osec
->alignment_power
)
6345 /* Check to see if any output section do not come from the
6347 for (section
= obfd
->sections
; section
!= NULL
;
6348 section
= section
->next
)
6350 if (section
->segment_mark
== FALSE
)
6353 section
->segment_mark
= FALSE
;
6356 return copy_elf_program_header (ibfd
, obfd
);
6360 if (ibfd
->xvec
== obfd
->xvec
)
6362 /* When rewriting program header, set the output maxpagesize to
6363 the maximum alignment of input PT_LOAD segments. */
6364 Elf_Internal_Phdr
*segment
;
6366 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6367 bfd_vma maxpagesize
= 0;
6369 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6372 if (segment
->p_type
== PT_LOAD
6373 && maxpagesize
< segment
->p_align
)
6374 maxpagesize
= segment
->p_align
;
6376 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6377 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6380 return rewrite_elf_program_header (ibfd
, obfd
);
6383 /* Initialize private output section information from input section. */
6386 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6390 struct bfd_link_info
*link_info
)
6393 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6394 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6396 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6397 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6400 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6402 /* For objcopy and relocatable link, don't copy the output ELF
6403 section type from input if the output BFD section flags have been
6404 set to something different. For a final link allow some flags
6405 that the linker clears to differ. */
6406 if (elf_section_type (osec
) == SHT_NULL
6407 && (osec
->flags
== isec
->flags
6409 && ((osec
->flags
^ isec
->flags
)
6410 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6411 elf_section_type (osec
) = elf_section_type (isec
);
6413 /* FIXME: Is this correct for all OS/PROC specific flags? */
6414 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6415 & (SHF_MASKOS
| SHF_MASKPROC
));
6417 /* Set things up for objcopy and relocatable link. The output
6418 SHT_GROUP section will have its elf_next_in_group pointing back
6419 to the input group members. Ignore linker created group section.
6420 See elfNN_ia64_object_p in elfxx-ia64.c. */
6423 if (elf_sec_group (isec
) == NULL
6424 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6426 if (elf_section_flags (isec
) & SHF_GROUP
)
6427 elf_section_flags (osec
) |= SHF_GROUP
;
6428 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6429 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6433 ihdr
= &elf_section_data (isec
)->this_hdr
;
6435 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6436 don't use the output section of the linked-to section since it
6437 may be NULL at this point. */
6438 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6440 ohdr
= &elf_section_data (osec
)->this_hdr
;
6441 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6442 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6445 osec
->use_rela_p
= isec
->use_rela_p
;
6450 /* Copy private section information. This copies over the entsize
6451 field, and sometimes the info field. */
6454 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6459 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6461 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6462 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6465 ihdr
= &elf_section_data (isec
)->this_hdr
;
6466 ohdr
= &elf_section_data (osec
)->this_hdr
;
6468 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6470 if (ihdr
->sh_type
== SHT_SYMTAB
6471 || ihdr
->sh_type
== SHT_DYNSYM
6472 || ihdr
->sh_type
== SHT_GNU_verneed
6473 || ihdr
->sh_type
== SHT_GNU_verdef
)
6474 ohdr
->sh_info
= ihdr
->sh_info
;
6476 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6480 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6481 necessary if we are removing either the SHT_GROUP section or any of
6482 the group member sections. DISCARDED is the value that a section's
6483 output_section has if the section will be discarded, NULL when this
6484 function is called from objcopy, bfd_abs_section_ptr when called
6488 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6492 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6493 if (elf_section_type (isec
) == SHT_GROUP
)
6495 asection
*first
= elf_next_in_group (isec
);
6496 asection
*s
= first
;
6497 bfd_size_type removed
= 0;
6501 /* If this member section is being output but the
6502 SHT_GROUP section is not, then clear the group info
6503 set up by _bfd_elf_copy_private_section_data. */
6504 if (s
->output_section
!= discarded
6505 && isec
->output_section
== discarded
)
6507 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6508 elf_group_name (s
->output_section
) = NULL
;
6510 /* Conversely, if the member section is not being output
6511 but the SHT_GROUP section is, then adjust its size. */
6512 else if (s
->output_section
== discarded
6513 && isec
->output_section
!= discarded
)
6515 s
= elf_next_in_group (s
);
6521 if (discarded
!= NULL
)
6523 /* If we've been called for ld -r, then we need to
6524 adjust the input section size. This function may
6525 be called multiple times, so save the original
6527 if (isec
->rawsize
== 0)
6528 isec
->rawsize
= isec
->size
;
6529 isec
->size
= isec
->rawsize
- removed
;
6533 /* Adjust the output section size when called from
6535 isec
->output_section
->size
-= removed
;
6543 /* Copy private header information. */
6546 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6548 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6549 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6552 /* Copy over private BFD data if it has not already been copied.
6553 This must be done here, rather than in the copy_private_bfd_data
6554 entry point, because the latter is called after the section
6555 contents have been set, which means that the program headers have
6556 already been worked out. */
6557 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6559 if (! copy_private_bfd_data (ibfd
, obfd
))
6563 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6566 /* Copy private symbol information. If this symbol is in a section
6567 which we did not map into a BFD section, try to map the section
6568 index correctly. We use special macro definitions for the mapped
6569 section indices; these definitions are interpreted by the
6570 swap_out_syms function. */
6572 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6573 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6574 #define MAP_STRTAB (SHN_HIOS + 3)
6575 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6576 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6579 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6584 elf_symbol_type
*isym
, *osym
;
6586 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6587 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6590 isym
= elf_symbol_from (ibfd
, isymarg
);
6591 osym
= elf_symbol_from (obfd
, osymarg
);
6594 && isym
->internal_elf_sym
.st_shndx
!= 0
6596 && bfd_is_abs_section (isym
->symbol
.section
))
6600 shndx
= isym
->internal_elf_sym
.st_shndx
;
6601 if (shndx
== elf_onesymtab (ibfd
))
6602 shndx
= MAP_ONESYMTAB
;
6603 else if (shndx
== elf_dynsymtab (ibfd
))
6604 shndx
= MAP_DYNSYMTAB
;
6605 else if (shndx
== elf_strtab_sec (ibfd
))
6607 else if (shndx
== elf_shstrtab_sec (ibfd
))
6608 shndx
= MAP_SHSTRTAB
;
6609 else if (shndx
== elf_symtab_shndx (ibfd
))
6610 shndx
= MAP_SYM_SHNDX
;
6611 osym
->internal_elf_sym
.st_shndx
= shndx
;
6617 /* Swap out the symbols. */
6620 swap_out_syms (bfd
*abfd
,
6621 struct bfd_strtab_hash
**sttp
,
6624 const struct elf_backend_data
*bed
;
6627 struct bfd_strtab_hash
*stt
;
6628 Elf_Internal_Shdr
*symtab_hdr
;
6629 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6630 Elf_Internal_Shdr
*symstrtab_hdr
;
6631 bfd_byte
*outbound_syms
;
6632 bfd_byte
*outbound_shndx
;
6634 unsigned int num_locals
;
6636 bfd_boolean name_local_sections
;
6638 if (!elf_map_symbols (abfd
, &num_locals
))
6641 /* Dump out the symtabs. */
6642 stt
= _bfd_elf_stringtab_init ();
6646 bed
= get_elf_backend_data (abfd
);
6647 symcount
= bfd_get_symcount (abfd
);
6648 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6649 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6650 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6651 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6652 symtab_hdr
->sh_info
= num_locals
+ 1;
6653 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6655 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6656 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6658 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6659 bed
->s
->sizeof_sym
);
6660 if (outbound_syms
== NULL
)
6662 _bfd_stringtab_free (stt
);
6665 symtab_hdr
->contents
= outbound_syms
;
6667 outbound_shndx
= NULL
;
6668 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6669 if (symtab_shndx_hdr
->sh_name
!= 0)
6671 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6672 outbound_shndx
= (bfd_byte
*)
6673 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6674 if (outbound_shndx
== NULL
)
6676 _bfd_stringtab_free (stt
);
6680 symtab_shndx_hdr
->contents
= outbound_shndx
;
6681 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6682 symtab_shndx_hdr
->sh_size
= amt
;
6683 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6684 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6687 /* Now generate the data (for "contents"). */
6689 /* Fill in zeroth symbol and swap it out. */
6690 Elf_Internal_Sym sym
;
6696 sym
.st_shndx
= SHN_UNDEF
;
6697 sym
.st_target_internal
= 0;
6698 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6699 outbound_syms
+= bed
->s
->sizeof_sym
;
6700 if (outbound_shndx
!= NULL
)
6701 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6705 = (bed
->elf_backend_name_local_section_symbols
6706 && bed
->elf_backend_name_local_section_symbols (abfd
));
6708 syms
= bfd_get_outsymbols (abfd
);
6709 for (idx
= 0; idx
< symcount
; idx
++)
6711 Elf_Internal_Sym sym
;
6712 bfd_vma value
= syms
[idx
]->value
;
6713 elf_symbol_type
*type_ptr
;
6714 flagword flags
= syms
[idx
]->flags
;
6717 if (!name_local_sections
6718 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6720 /* Local section symbols have no name. */
6725 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6728 if (sym
.st_name
== (unsigned long) -1)
6730 _bfd_stringtab_free (stt
);
6735 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6737 if ((flags
& BSF_SECTION_SYM
) == 0
6738 && bfd_is_com_section (syms
[idx
]->section
))
6740 /* ELF common symbols put the alignment into the `value' field,
6741 and the size into the `size' field. This is backwards from
6742 how BFD handles it, so reverse it here. */
6743 sym
.st_size
= value
;
6744 if (type_ptr
== NULL
6745 || type_ptr
->internal_elf_sym
.st_value
== 0)
6746 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6748 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6749 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6750 (abfd
, syms
[idx
]->section
);
6754 asection
*sec
= syms
[idx
]->section
;
6757 if (sec
->output_section
)
6759 value
+= sec
->output_offset
;
6760 sec
= sec
->output_section
;
6763 /* Don't add in the section vma for relocatable output. */
6764 if (! relocatable_p
)
6766 sym
.st_value
= value
;
6767 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6769 if (bfd_is_abs_section (sec
)
6771 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6773 /* This symbol is in a real ELF section which we did
6774 not create as a BFD section. Undo the mapping done
6775 by copy_private_symbol_data. */
6776 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6780 shndx
= elf_onesymtab (abfd
);
6783 shndx
= elf_dynsymtab (abfd
);
6786 shndx
= elf_strtab_sec (abfd
);
6789 shndx
= elf_shstrtab_sec (abfd
);
6792 shndx
= elf_symtab_shndx (abfd
);
6801 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6803 if (shndx
== SHN_BAD
)
6807 /* Writing this would be a hell of a lot easier if
6808 we had some decent documentation on bfd, and
6809 knew what to expect of the library, and what to
6810 demand of applications. For example, it
6811 appears that `objcopy' might not set the
6812 section of a symbol to be a section that is
6813 actually in the output file. */
6814 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6817 _bfd_error_handler (_("\
6818 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6819 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6821 bfd_set_error (bfd_error_invalid_operation
);
6822 _bfd_stringtab_free (stt
);
6826 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6827 BFD_ASSERT (shndx
!= SHN_BAD
);
6831 sym
.st_shndx
= shndx
;
6834 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6836 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6837 type
= STT_GNU_IFUNC
;
6838 else if ((flags
& BSF_FUNCTION
) != 0)
6840 else if ((flags
& BSF_OBJECT
) != 0)
6842 else if ((flags
& BSF_RELC
) != 0)
6844 else if ((flags
& BSF_SRELC
) != 0)
6849 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6852 /* Processor-specific types. */
6853 if (type_ptr
!= NULL
6854 && bed
->elf_backend_get_symbol_type
)
6855 type
= ((*bed
->elf_backend_get_symbol_type
)
6856 (&type_ptr
->internal_elf_sym
, type
));
6858 if (flags
& BSF_SECTION_SYM
)
6860 if (flags
& BSF_GLOBAL
)
6861 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6863 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6865 else if (bfd_is_com_section (syms
[idx
]->section
))
6867 #ifdef USE_STT_COMMON
6868 if (type
== STT_OBJECT
)
6869 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6872 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6874 else if (bfd_is_und_section (syms
[idx
]->section
))
6875 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6879 else if (flags
& BSF_FILE
)
6880 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6883 int bind
= STB_LOCAL
;
6885 if (flags
& BSF_LOCAL
)
6887 else if (flags
& BSF_GNU_UNIQUE
)
6888 bind
= STB_GNU_UNIQUE
;
6889 else if (flags
& BSF_WEAK
)
6891 else if (flags
& BSF_GLOBAL
)
6894 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6897 if (type_ptr
!= NULL
)
6899 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6900 sym
.st_target_internal
6901 = type_ptr
->internal_elf_sym
.st_target_internal
;
6906 sym
.st_target_internal
= 0;
6909 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6910 outbound_syms
+= bed
->s
->sizeof_sym
;
6911 if (outbound_shndx
!= NULL
)
6912 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6916 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6917 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6919 symstrtab_hdr
->sh_flags
= 0;
6920 symstrtab_hdr
->sh_addr
= 0;
6921 symstrtab_hdr
->sh_entsize
= 0;
6922 symstrtab_hdr
->sh_link
= 0;
6923 symstrtab_hdr
->sh_info
= 0;
6924 symstrtab_hdr
->sh_addralign
= 1;
6929 /* Return the number of bytes required to hold the symtab vector.
6931 Note that we base it on the count plus 1, since we will null terminate
6932 the vector allocated based on this size. However, the ELF symbol table
6933 always has a dummy entry as symbol #0, so it ends up even. */
6936 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6940 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6942 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6943 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6945 symtab_size
-= sizeof (asymbol
*);
6951 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6955 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6957 if (elf_dynsymtab (abfd
) == 0)
6959 bfd_set_error (bfd_error_invalid_operation
);
6963 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6964 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6966 symtab_size
-= sizeof (asymbol
*);
6972 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6975 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6978 /* Canonicalize the relocs. */
6981 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6988 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6990 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6993 tblptr
= section
->relocation
;
6994 for (i
= 0; i
< section
->reloc_count
; i
++)
6995 *relptr
++ = tblptr
++;
6999 return section
->reloc_count
;
7003 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7005 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7006 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7009 bfd_get_symcount (abfd
) = symcount
;
7014 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7015 asymbol
**allocation
)
7017 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7018 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7021 bfd_get_dynamic_symcount (abfd
) = symcount
;
7025 /* Return the size required for the dynamic reloc entries. Any loadable
7026 section that was actually installed in the BFD, and has type SHT_REL
7027 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7028 dynamic reloc section. */
7031 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7036 if (elf_dynsymtab (abfd
) == 0)
7038 bfd_set_error (bfd_error_invalid_operation
);
7042 ret
= sizeof (arelent
*);
7043 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7044 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7045 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7046 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7047 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7048 * sizeof (arelent
*));
7053 /* Canonicalize the dynamic relocation entries. Note that we return the
7054 dynamic relocations as a single block, although they are actually
7055 associated with particular sections; the interface, which was
7056 designed for SunOS style shared libraries, expects that there is only
7057 one set of dynamic relocs. Any loadable section that was actually
7058 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7059 dynamic symbol table, is considered to be a dynamic reloc section. */
7062 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7066 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7070 if (elf_dynsymtab (abfd
) == 0)
7072 bfd_set_error (bfd_error_invalid_operation
);
7076 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7078 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
))
7087 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7089 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7091 for (i
= 0; i
< count
; i
++)
7102 /* Read in the version information. */
7105 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7107 bfd_byte
*contents
= NULL
;
7108 unsigned int freeidx
= 0;
7110 if (elf_dynverref (abfd
) != 0)
7112 Elf_Internal_Shdr
*hdr
;
7113 Elf_External_Verneed
*everneed
;
7114 Elf_Internal_Verneed
*iverneed
;
7116 bfd_byte
*contents_end
;
7118 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7120 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7121 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7122 if (elf_tdata (abfd
)->verref
== NULL
)
7125 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
7127 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7128 if (contents
== NULL
)
7130 error_return_verref
:
7131 elf_tdata (abfd
)->verref
= NULL
;
7132 elf_tdata (abfd
)->cverrefs
= 0;
7135 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7136 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7137 goto error_return_verref
;
7139 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7140 goto error_return_verref
;
7142 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7143 == sizeof (Elf_External_Vernaux
));
7144 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7145 everneed
= (Elf_External_Verneed
*) contents
;
7146 iverneed
= elf_tdata (abfd
)->verref
;
7147 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7149 Elf_External_Vernaux
*evernaux
;
7150 Elf_Internal_Vernaux
*ivernaux
;
7153 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7155 iverneed
->vn_bfd
= abfd
;
7157 iverneed
->vn_filename
=
7158 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7160 if (iverneed
->vn_filename
== NULL
)
7161 goto error_return_verref
;
7163 if (iverneed
->vn_cnt
== 0)
7164 iverneed
->vn_auxptr
= NULL
;
7167 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7168 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7169 sizeof (Elf_Internal_Vernaux
));
7170 if (iverneed
->vn_auxptr
== NULL
)
7171 goto error_return_verref
;
7174 if (iverneed
->vn_aux
7175 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7176 goto error_return_verref
;
7178 evernaux
= ((Elf_External_Vernaux
*)
7179 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7180 ivernaux
= iverneed
->vn_auxptr
;
7181 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7183 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7185 ivernaux
->vna_nodename
=
7186 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7187 ivernaux
->vna_name
);
7188 if (ivernaux
->vna_nodename
== NULL
)
7189 goto error_return_verref
;
7191 if (j
+ 1 < iverneed
->vn_cnt
)
7192 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7194 ivernaux
->vna_nextptr
= NULL
;
7196 if (ivernaux
->vna_next
7197 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7198 goto error_return_verref
;
7200 evernaux
= ((Elf_External_Vernaux
*)
7201 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7203 if (ivernaux
->vna_other
> freeidx
)
7204 freeidx
= ivernaux
->vna_other
;
7207 if (i
+ 1 < hdr
->sh_info
)
7208 iverneed
->vn_nextref
= iverneed
+ 1;
7210 iverneed
->vn_nextref
= NULL
;
7212 if (iverneed
->vn_next
7213 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7214 goto error_return_verref
;
7216 everneed
= ((Elf_External_Verneed
*)
7217 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7224 if (elf_dynverdef (abfd
) != 0)
7226 Elf_Internal_Shdr
*hdr
;
7227 Elf_External_Verdef
*everdef
;
7228 Elf_Internal_Verdef
*iverdef
;
7229 Elf_Internal_Verdef
*iverdefarr
;
7230 Elf_Internal_Verdef iverdefmem
;
7232 unsigned int maxidx
;
7233 bfd_byte
*contents_end_def
, *contents_end_aux
;
7235 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7237 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7238 if (contents
== NULL
)
7240 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7241 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7244 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7247 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7248 >= sizeof (Elf_External_Verdaux
));
7249 contents_end_def
= contents
+ hdr
->sh_size
7250 - sizeof (Elf_External_Verdef
);
7251 contents_end_aux
= contents
+ hdr
->sh_size
7252 - sizeof (Elf_External_Verdaux
);
7254 /* We know the number of entries in the section but not the maximum
7255 index. Therefore we have to run through all entries and find
7257 everdef
= (Elf_External_Verdef
*) contents
;
7259 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7261 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7263 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7264 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7266 if (iverdefmem
.vd_next
7267 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7270 everdef
= ((Elf_External_Verdef
*)
7271 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7274 if (default_imported_symver
)
7276 if (freeidx
> maxidx
)
7281 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7282 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7283 if (elf_tdata (abfd
)->verdef
== NULL
)
7286 elf_tdata (abfd
)->cverdefs
= maxidx
;
7288 everdef
= (Elf_External_Verdef
*) contents
;
7289 iverdefarr
= elf_tdata (abfd
)->verdef
;
7290 for (i
= 0; i
< hdr
->sh_info
; i
++)
7292 Elf_External_Verdaux
*everdaux
;
7293 Elf_Internal_Verdaux
*iverdaux
;
7296 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7298 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7300 error_return_verdef
:
7301 elf_tdata (abfd
)->verdef
= NULL
;
7302 elf_tdata (abfd
)->cverdefs
= 0;
7306 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7307 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7309 iverdef
->vd_bfd
= abfd
;
7311 if (iverdef
->vd_cnt
== 0)
7312 iverdef
->vd_auxptr
= NULL
;
7315 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7316 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7317 sizeof (Elf_Internal_Verdaux
));
7318 if (iverdef
->vd_auxptr
== NULL
)
7319 goto error_return_verdef
;
7323 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7324 goto error_return_verdef
;
7326 everdaux
= ((Elf_External_Verdaux
*)
7327 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7328 iverdaux
= iverdef
->vd_auxptr
;
7329 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7331 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7333 iverdaux
->vda_nodename
=
7334 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7335 iverdaux
->vda_name
);
7336 if (iverdaux
->vda_nodename
== NULL
)
7337 goto error_return_verdef
;
7339 if (j
+ 1 < iverdef
->vd_cnt
)
7340 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7342 iverdaux
->vda_nextptr
= NULL
;
7344 if (iverdaux
->vda_next
7345 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7346 goto error_return_verdef
;
7348 everdaux
= ((Elf_External_Verdaux
*)
7349 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7352 if (iverdef
->vd_cnt
)
7353 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7355 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7356 iverdef
->vd_nextdef
= iverdef
+ 1;
7358 iverdef
->vd_nextdef
= NULL
;
7360 everdef
= ((Elf_External_Verdef
*)
7361 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7367 else if (default_imported_symver
)
7374 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7375 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7376 if (elf_tdata (abfd
)->verdef
== NULL
)
7379 elf_tdata (abfd
)->cverdefs
= freeidx
;
7382 /* Create a default version based on the soname. */
7383 if (default_imported_symver
)
7385 Elf_Internal_Verdef
*iverdef
;
7386 Elf_Internal_Verdaux
*iverdaux
;
7388 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7390 iverdef
->vd_version
= VER_DEF_CURRENT
;
7391 iverdef
->vd_flags
= 0;
7392 iverdef
->vd_ndx
= freeidx
;
7393 iverdef
->vd_cnt
= 1;
7395 iverdef
->vd_bfd
= abfd
;
7397 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7398 if (iverdef
->vd_nodename
== NULL
)
7399 goto error_return_verdef
;
7400 iverdef
->vd_nextdef
= NULL
;
7401 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7402 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7403 if (iverdef
->vd_auxptr
== NULL
)
7404 goto error_return_verdef
;
7406 iverdaux
= iverdef
->vd_auxptr
;
7407 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7408 iverdaux
->vda_nextptr
= NULL
;
7414 if (contents
!= NULL
)
7420 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7422 elf_symbol_type
*newsym
;
7423 bfd_size_type amt
= sizeof (elf_symbol_type
);
7425 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7430 newsym
->symbol
.the_bfd
= abfd
;
7431 return &newsym
->symbol
;
7436 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7440 bfd_symbol_info (symbol
, ret
);
7443 /* Return whether a symbol name implies a local symbol. Most targets
7444 use this function for the is_local_label_name entry point, but some
7448 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7451 /* Normal local symbols start with ``.L''. */
7452 if (name
[0] == '.' && name
[1] == 'L')
7455 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7456 DWARF debugging symbols starting with ``..''. */
7457 if (name
[0] == '.' && name
[1] == '.')
7460 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7461 emitting DWARF debugging output. I suspect this is actually a
7462 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7463 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7464 underscore to be emitted on some ELF targets). For ease of use,
7465 we treat such symbols as local. */
7466 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7473 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7474 asymbol
*symbol ATTRIBUTE_UNUSED
)
7481 _bfd_elf_set_arch_mach (bfd
*abfd
,
7482 enum bfd_architecture arch
,
7483 unsigned long machine
)
7485 /* If this isn't the right architecture for this backend, and this
7486 isn't the generic backend, fail. */
7487 if (arch
!= get_elf_backend_data (abfd
)->arch
7488 && arch
!= bfd_arch_unknown
7489 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7492 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7495 /* Find the function to a particular section and offset,
7496 for error reporting. */
7499 elf_find_function (bfd
*abfd
,
7503 const char **filename_ptr
,
7504 const char **functionname_ptr
)
7506 struct elf_find_function_cache
7508 asection
*last_section
;
7510 const char *filename
;
7511 bfd_size_type func_size
;
7514 if (symbols
== NULL
)
7517 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
7520 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
7521 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
7525 if (cache
->last_section
!= section
7526 || cache
->func
== NULL
7527 || offset
< cache
->func
->value
7528 || offset
>= cache
->func
->value
+ cache
->func_size
)
7533 /* ??? Given multiple file symbols, it is impossible to reliably
7534 choose the right file name for global symbols. File symbols are
7535 local symbols, and thus all file symbols must sort before any
7536 global symbols. The ELF spec may be interpreted to say that a
7537 file symbol must sort before other local symbols, but currently
7538 ld -r doesn't do this. So, for ld -r output, it is possible to
7539 make a better choice of file name for local symbols by ignoring
7540 file symbols appearing after a given local symbol. */
7541 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7542 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7546 state
= nothing_seen
;
7547 cache
->filename
= NULL
;
7549 cache
->func_size
= 0;
7550 cache
->last_section
= section
;
7552 for (p
= symbols
; *p
!= NULL
; p
++)
7558 if ((sym
->flags
& BSF_FILE
) != 0)
7561 if (state
== symbol_seen
)
7562 state
= file_after_symbol_seen
;
7566 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
7568 && code_off
<= offset
7569 && (code_off
> low_func
7570 || (code_off
== low_func
7571 && size
> cache
->func_size
)))
7574 cache
->func_size
= size
;
7575 cache
->filename
= NULL
;
7576 low_func
= code_off
;
7578 && ((sym
->flags
& BSF_LOCAL
) != 0
7579 || state
!= file_after_symbol_seen
))
7580 cache
->filename
= bfd_asymbol_name (file
);
7582 if (state
== nothing_seen
)
7583 state
= symbol_seen
;
7587 if (cache
->func
== NULL
)
7591 *filename_ptr
= cache
->filename
;
7592 if (functionname_ptr
)
7593 *functionname_ptr
= bfd_asymbol_name (cache
->func
);
7598 /* Find the nearest line to a particular section and offset,
7599 for error reporting. */
7602 _bfd_elf_find_nearest_line (bfd
*abfd
,
7606 const char **filename_ptr
,
7607 const char **functionname_ptr
,
7608 unsigned int *line_ptr
)
7610 return _bfd_elf_find_nearest_line_discriminator (abfd
, section
, symbols
,
7611 offset
, filename_ptr
,
7618 _bfd_elf_find_nearest_line_discriminator (bfd
*abfd
,
7622 const char **filename_ptr
,
7623 const char **functionname_ptr
,
7624 unsigned int *line_ptr
,
7625 unsigned int *discriminator_ptr
)
7629 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7630 filename_ptr
, functionname_ptr
,
7633 if (!*functionname_ptr
)
7634 elf_find_function (abfd
, section
, symbols
, offset
,
7635 *filename_ptr
? NULL
: filename_ptr
,
7641 if (_bfd_dwarf2_find_nearest_line (abfd
, dwarf_debug_sections
,
7642 section
, symbols
, offset
,
7643 filename_ptr
, functionname_ptr
,
7644 line_ptr
, discriminator_ptr
, 0,
7645 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7647 if (!*functionname_ptr
)
7648 elf_find_function (abfd
, section
, symbols
, offset
,
7649 *filename_ptr
? NULL
: filename_ptr
,
7655 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7656 &found
, filename_ptr
,
7657 functionname_ptr
, line_ptr
,
7658 &elf_tdata (abfd
)->line_info
))
7660 if (found
&& (*functionname_ptr
|| *line_ptr
))
7663 if (symbols
== NULL
)
7666 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7667 filename_ptr
, functionname_ptr
))
7674 /* Find the line for a symbol. */
7677 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7678 const char **filename_ptr
, unsigned int *line_ptr
)
7680 return _bfd_elf_find_line_discriminator (abfd
, symbols
, symbol
,
7681 filename_ptr
, line_ptr
,
7686 _bfd_elf_find_line_discriminator (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7687 const char **filename_ptr
,
7688 unsigned int *line_ptr
,
7689 unsigned int *discriminator_ptr
)
7691 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7692 filename_ptr
, line_ptr
, discriminator_ptr
, 0,
7693 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7696 /* After a call to bfd_find_nearest_line, successive calls to
7697 bfd_find_inliner_info can be used to get source information about
7698 each level of function inlining that terminated at the address
7699 passed to bfd_find_nearest_line. Currently this is only supported
7700 for DWARF2 with appropriate DWARF3 extensions. */
7703 _bfd_elf_find_inliner_info (bfd
*abfd
,
7704 const char **filename_ptr
,
7705 const char **functionname_ptr
,
7706 unsigned int *line_ptr
)
7709 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7710 functionname_ptr
, line_ptr
,
7711 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7716 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7718 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7719 int ret
= bed
->s
->sizeof_ehdr
;
7721 if (!info
->relocatable
)
7723 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
7725 if (phdr_size
== (bfd_size_type
) -1)
7727 struct elf_segment_map
*m
;
7730 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
7731 phdr_size
+= bed
->s
->sizeof_phdr
;
7734 phdr_size
= get_program_header_size (abfd
, info
);
7737 elf_program_header_size (abfd
) = phdr_size
;
7745 _bfd_elf_set_section_contents (bfd
*abfd
,
7747 const void *location
,
7749 bfd_size_type count
)
7751 Elf_Internal_Shdr
*hdr
;
7754 if (! abfd
->output_has_begun
7755 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7758 hdr
= &elf_section_data (section
)->this_hdr
;
7759 pos
= hdr
->sh_offset
+ offset
;
7760 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7761 || bfd_bwrite (location
, count
, abfd
) != count
)
7768 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7769 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7770 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7775 /* Try to convert a non-ELF reloc into an ELF one. */
7778 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7780 /* Check whether we really have an ELF howto. */
7782 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7784 bfd_reloc_code_real_type code
;
7785 reloc_howto_type
*howto
;
7787 /* Alien reloc: Try to determine its type to replace it with an
7788 equivalent ELF reloc. */
7790 if (areloc
->howto
->pc_relative
)
7792 switch (areloc
->howto
->bitsize
)
7795 code
= BFD_RELOC_8_PCREL
;
7798 code
= BFD_RELOC_12_PCREL
;
7801 code
= BFD_RELOC_16_PCREL
;
7804 code
= BFD_RELOC_24_PCREL
;
7807 code
= BFD_RELOC_32_PCREL
;
7810 code
= BFD_RELOC_64_PCREL
;
7816 howto
= bfd_reloc_type_lookup (abfd
, code
);
7818 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7820 if (howto
->pcrel_offset
)
7821 areloc
->addend
+= areloc
->address
;
7823 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7828 switch (areloc
->howto
->bitsize
)
7834 code
= BFD_RELOC_14
;
7837 code
= BFD_RELOC_16
;
7840 code
= BFD_RELOC_26
;
7843 code
= BFD_RELOC_32
;
7846 code
= BFD_RELOC_64
;
7852 howto
= bfd_reloc_type_lookup (abfd
, code
);
7856 areloc
->howto
= howto
;
7864 (*_bfd_error_handler
)
7865 (_("%B: unsupported relocation type %s"),
7866 abfd
, areloc
->howto
->name
);
7867 bfd_set_error (bfd_error_bad_value
);
7872 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7874 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
7875 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
7877 if (elf_shstrtab (abfd
) != NULL
)
7878 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7879 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
7882 return _bfd_generic_close_and_cleanup (abfd
);
7885 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7886 in the relocation's offset. Thus we cannot allow any sort of sanity
7887 range-checking to interfere. There is nothing else to do in processing
7890 bfd_reloc_status_type
7891 _bfd_elf_rel_vtable_reloc_fn
7892 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7893 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7894 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7895 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7897 return bfd_reloc_ok
;
7900 /* Elf core file support. Much of this only works on native
7901 toolchains, since we rely on knowing the
7902 machine-dependent procfs structure in order to pick
7903 out details about the corefile. */
7905 #ifdef HAVE_SYS_PROCFS_H
7906 /* Needed for new procfs interface on sparc-solaris. */
7907 # define _STRUCTURED_PROC 1
7908 # include <sys/procfs.h>
7911 /* Return a PID that identifies a "thread" for threaded cores, or the
7912 PID of the main process for non-threaded cores. */
7915 elfcore_make_pid (bfd
*abfd
)
7919 pid
= elf_tdata (abfd
)->core
->lwpid
;
7921 pid
= elf_tdata (abfd
)->core
->pid
;
7926 /* If there isn't a section called NAME, make one, using
7927 data from SECT. Note, this function will generate a
7928 reference to NAME, so you shouldn't deallocate or
7932 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7936 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7939 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7943 sect2
->size
= sect
->size
;
7944 sect2
->filepos
= sect
->filepos
;
7945 sect2
->alignment_power
= sect
->alignment_power
;
7949 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7950 actually creates up to two pseudosections:
7951 - For the single-threaded case, a section named NAME, unless
7952 such a section already exists.
7953 - For the multi-threaded case, a section named "NAME/PID", where
7954 PID is elfcore_make_pid (abfd).
7955 Both pseudosections have identical contents. */
7957 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7963 char *threaded_name
;
7967 /* Build the section name. */
7969 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7970 len
= strlen (buf
) + 1;
7971 threaded_name
= (char *) bfd_alloc (abfd
, len
);
7972 if (threaded_name
== NULL
)
7974 memcpy (threaded_name
, buf
, len
);
7976 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7981 sect
->filepos
= filepos
;
7982 sect
->alignment_power
= 2;
7984 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7987 /* prstatus_t exists on:
7989 linux 2.[01] + glibc
7993 #if defined (HAVE_PRSTATUS_T)
7996 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8001 if (note
->descsz
== sizeof (prstatus_t
))
8005 size
= sizeof (prstat
.pr_reg
);
8006 offset
= offsetof (prstatus_t
, pr_reg
);
8007 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8009 /* Do not overwrite the core signal if it
8010 has already been set by another thread. */
8011 if (elf_tdata (abfd
)->core
->signal
== 0)
8012 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8013 if (elf_tdata (abfd
)->core
->pid
== 0)
8014 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8016 /* pr_who exists on:
8019 pr_who doesn't exist on:
8022 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8023 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8025 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8028 #if defined (HAVE_PRSTATUS32_T)
8029 else if (note
->descsz
== sizeof (prstatus32_t
))
8031 /* 64-bit host, 32-bit corefile */
8032 prstatus32_t prstat
;
8034 size
= sizeof (prstat
.pr_reg
);
8035 offset
= offsetof (prstatus32_t
, pr_reg
);
8036 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8038 /* Do not overwrite the core signal if it
8039 has already been set by another thread. */
8040 if (elf_tdata (abfd
)->core
->signal
== 0)
8041 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8042 if (elf_tdata (abfd
)->core
->pid
== 0)
8043 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8045 /* pr_who exists on:
8048 pr_who doesn't exist on:
8051 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8052 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8054 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8057 #endif /* HAVE_PRSTATUS32_T */
8060 /* Fail - we don't know how to handle any other
8061 note size (ie. data object type). */
8065 /* Make a ".reg/999" section and a ".reg" section. */
8066 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8067 size
, note
->descpos
+ offset
);
8069 #endif /* defined (HAVE_PRSTATUS_T) */
8071 /* Create a pseudosection containing the exact contents of NOTE. */
8073 elfcore_make_note_pseudosection (bfd
*abfd
,
8075 Elf_Internal_Note
*note
)
8077 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8078 note
->descsz
, note
->descpos
);
8081 /* There isn't a consistent prfpregset_t across platforms,
8082 but it doesn't matter, because we don't have to pick this
8083 data structure apart. */
8086 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8088 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8091 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8092 type of NT_PRXFPREG. Just include the whole note's contents
8096 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8098 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8101 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8102 with a note type of NT_X86_XSTATE. Just include the whole note's
8103 contents literally. */
8106 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8108 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8112 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8114 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8118 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8120 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8124 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8126 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8130 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8132 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8136 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8138 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8142 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8144 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8148 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8150 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8154 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8156 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8160 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8162 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8166 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8168 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8172 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8174 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8178 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8180 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8184 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8186 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8190 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8192 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8195 #if defined (HAVE_PRPSINFO_T)
8196 typedef prpsinfo_t elfcore_psinfo_t
;
8197 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8198 typedef prpsinfo32_t elfcore_psinfo32_t
;
8202 #if defined (HAVE_PSINFO_T)
8203 typedef psinfo_t elfcore_psinfo_t
;
8204 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8205 typedef psinfo32_t elfcore_psinfo32_t
;
8209 /* return a malloc'ed copy of a string at START which is at
8210 most MAX bytes long, possibly without a terminating '\0'.
8211 the copy will always have a terminating '\0'. */
8214 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8217 char *end
= (char *) memchr (start
, '\0', max
);
8225 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8229 memcpy (dups
, start
, len
);
8235 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8237 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8239 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8241 elfcore_psinfo_t psinfo
;
8243 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8245 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8246 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8248 elf_tdata (abfd
)->core
->program
8249 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8250 sizeof (psinfo
.pr_fname
));
8252 elf_tdata (abfd
)->core
->command
8253 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8254 sizeof (psinfo
.pr_psargs
));
8256 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8257 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8259 /* 64-bit host, 32-bit corefile */
8260 elfcore_psinfo32_t psinfo
;
8262 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8264 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8265 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8267 elf_tdata (abfd
)->core
->program
8268 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8269 sizeof (psinfo
.pr_fname
));
8271 elf_tdata (abfd
)->core
->command
8272 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8273 sizeof (psinfo
.pr_psargs
));
8279 /* Fail - we don't know how to handle any other
8280 note size (ie. data object type). */
8284 /* Note that for some reason, a spurious space is tacked
8285 onto the end of the args in some (at least one anyway)
8286 implementations, so strip it off if it exists. */
8289 char *command
= elf_tdata (abfd
)->core
->command
;
8290 int n
= strlen (command
);
8292 if (0 < n
&& command
[n
- 1] == ' ')
8293 command
[n
- 1] = '\0';
8298 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8300 #if defined (HAVE_PSTATUS_T)
8302 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8304 if (note
->descsz
== sizeof (pstatus_t
)
8305 #if defined (HAVE_PXSTATUS_T)
8306 || note
->descsz
== sizeof (pxstatus_t
)
8312 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8314 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8316 #if defined (HAVE_PSTATUS32_T)
8317 else if (note
->descsz
== sizeof (pstatus32_t
))
8319 /* 64-bit host, 32-bit corefile */
8322 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8324 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8327 /* Could grab some more details from the "representative"
8328 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8329 NT_LWPSTATUS note, presumably. */
8333 #endif /* defined (HAVE_PSTATUS_T) */
8335 #if defined (HAVE_LWPSTATUS_T)
8337 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8339 lwpstatus_t lwpstat
;
8345 if (note
->descsz
!= sizeof (lwpstat
)
8346 #if defined (HAVE_LWPXSTATUS_T)
8347 && note
->descsz
!= sizeof (lwpxstatus_t
)
8352 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8354 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8355 /* Do not overwrite the core signal if it has already been set by
8357 if (elf_tdata (abfd
)->core
->signal
== 0)
8358 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8360 /* Make a ".reg/999" section. */
8362 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8363 len
= strlen (buf
) + 1;
8364 name
= bfd_alloc (abfd
, len
);
8367 memcpy (name
, buf
, len
);
8369 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8373 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8374 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8375 sect
->filepos
= note
->descpos
8376 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8379 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8380 sect
->size
= sizeof (lwpstat
.pr_reg
);
8381 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8384 sect
->alignment_power
= 2;
8386 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8389 /* Make a ".reg2/999" section */
8391 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8392 len
= strlen (buf
) + 1;
8393 name
= bfd_alloc (abfd
, len
);
8396 memcpy (name
, buf
, len
);
8398 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8402 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8403 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8404 sect
->filepos
= note
->descpos
8405 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8408 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8409 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8410 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8413 sect
->alignment_power
= 2;
8415 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8417 #endif /* defined (HAVE_LWPSTATUS_T) */
8420 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8427 int is_active_thread
;
8430 if (note
->descsz
< 728)
8433 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8436 type
= bfd_get_32 (abfd
, note
->descdata
);
8440 case 1 /* NOTE_INFO_PROCESS */:
8441 /* FIXME: need to add ->core->command. */
8442 /* process_info.pid */
8443 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8444 /* process_info.signal */
8445 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8448 case 2 /* NOTE_INFO_THREAD */:
8449 /* Make a ".reg/999" section. */
8450 /* thread_info.tid */
8451 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8453 len
= strlen (buf
) + 1;
8454 name
= (char *) bfd_alloc (abfd
, len
);
8458 memcpy (name
, buf
, len
);
8460 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8464 /* sizeof (thread_info.thread_context) */
8466 /* offsetof (thread_info.thread_context) */
8467 sect
->filepos
= note
->descpos
+ 12;
8468 sect
->alignment_power
= 2;
8470 /* thread_info.is_active_thread */
8471 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8473 if (is_active_thread
)
8474 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8478 case 3 /* NOTE_INFO_MODULE */:
8479 /* Make a ".module/xxxxxxxx" section. */
8480 /* module_info.base_address */
8481 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8482 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8484 len
= strlen (buf
) + 1;
8485 name
= (char *) bfd_alloc (abfd
, len
);
8489 memcpy (name
, buf
, len
);
8491 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8496 sect
->size
= note
->descsz
;
8497 sect
->filepos
= note
->descpos
;
8498 sect
->alignment_power
= 2;
8509 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8511 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8519 if (bed
->elf_backend_grok_prstatus
)
8520 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8522 #if defined (HAVE_PRSTATUS_T)
8523 return elfcore_grok_prstatus (abfd
, note
);
8528 #if defined (HAVE_PSTATUS_T)
8530 return elfcore_grok_pstatus (abfd
, note
);
8533 #if defined (HAVE_LWPSTATUS_T)
8535 return elfcore_grok_lwpstatus (abfd
, note
);
8538 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8539 return elfcore_grok_prfpreg (abfd
, note
);
8541 case NT_WIN32PSTATUS
:
8542 return elfcore_grok_win32pstatus (abfd
, note
);
8544 case NT_PRXFPREG
: /* Linux SSE extension */
8545 if (note
->namesz
== 6
8546 && strcmp (note
->namedata
, "LINUX") == 0)
8547 return elfcore_grok_prxfpreg (abfd
, note
);
8551 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8552 if (note
->namesz
== 6
8553 && strcmp (note
->namedata
, "LINUX") == 0)
8554 return elfcore_grok_xstatereg (abfd
, note
);
8559 if (note
->namesz
== 6
8560 && strcmp (note
->namedata
, "LINUX") == 0)
8561 return elfcore_grok_ppc_vmx (abfd
, note
);
8566 if (note
->namesz
== 6
8567 && strcmp (note
->namedata
, "LINUX") == 0)
8568 return elfcore_grok_ppc_vsx (abfd
, note
);
8572 case NT_S390_HIGH_GPRS
:
8573 if (note
->namesz
== 6
8574 && strcmp (note
->namedata
, "LINUX") == 0)
8575 return elfcore_grok_s390_high_gprs (abfd
, note
);
8580 if (note
->namesz
== 6
8581 && strcmp (note
->namedata
, "LINUX") == 0)
8582 return elfcore_grok_s390_timer (abfd
, note
);
8586 case NT_S390_TODCMP
:
8587 if (note
->namesz
== 6
8588 && strcmp (note
->namedata
, "LINUX") == 0)
8589 return elfcore_grok_s390_todcmp (abfd
, note
);
8593 case NT_S390_TODPREG
:
8594 if (note
->namesz
== 6
8595 && strcmp (note
->namedata
, "LINUX") == 0)
8596 return elfcore_grok_s390_todpreg (abfd
, note
);
8601 if (note
->namesz
== 6
8602 && strcmp (note
->namedata
, "LINUX") == 0)
8603 return elfcore_grok_s390_ctrs (abfd
, note
);
8607 case NT_S390_PREFIX
:
8608 if (note
->namesz
== 6
8609 && strcmp (note
->namedata
, "LINUX") == 0)
8610 return elfcore_grok_s390_prefix (abfd
, note
);
8614 case NT_S390_LAST_BREAK
:
8615 if (note
->namesz
== 6
8616 && strcmp (note
->namedata
, "LINUX") == 0)
8617 return elfcore_grok_s390_last_break (abfd
, note
);
8621 case NT_S390_SYSTEM_CALL
:
8622 if (note
->namesz
== 6
8623 && strcmp (note
->namedata
, "LINUX") == 0)
8624 return elfcore_grok_s390_system_call (abfd
, note
);
8629 if (note
->namesz
== 6
8630 && strcmp (note
->namedata
, "LINUX") == 0)
8631 return elfcore_grok_arm_vfp (abfd
, note
);
8636 if (note
->namesz
== 6
8637 && strcmp (note
->namedata
, "LINUX") == 0)
8638 return elfcore_grok_aarch_tls (abfd
, note
);
8642 case NT_ARM_HW_BREAK
:
8643 if (note
->namesz
== 6
8644 && strcmp (note
->namedata
, "LINUX") == 0)
8645 return elfcore_grok_aarch_hw_break (abfd
, note
);
8649 case NT_ARM_HW_WATCH
:
8650 if (note
->namesz
== 6
8651 && strcmp (note
->namedata
, "LINUX") == 0)
8652 return elfcore_grok_aarch_hw_watch (abfd
, note
);
8658 if (bed
->elf_backend_grok_psinfo
)
8659 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8661 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8662 return elfcore_grok_psinfo (abfd
, note
);
8669 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8674 sect
->size
= note
->descsz
;
8675 sect
->filepos
= note
->descpos
;
8676 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8682 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
8686 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
8692 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8694 struct elf_obj_tdata
*t
;
8696 if (note
->descsz
== 0)
8699 t
= elf_tdata (abfd
);
8700 t
->build_id
= bfd_alloc (abfd
, sizeof (t
->build_id
->u
.i
) - 1 + note
->descsz
);
8701 if (t
->build_id
== NULL
)
8704 t
->build_id
->u
.i
.size
= note
->descsz
;
8705 memcpy (t
->build_id
->u
.i
.data
, note
->descdata
, note
->descsz
);
8711 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8718 case NT_GNU_BUILD_ID
:
8719 return elfobj_grok_gnu_build_id (abfd
, note
);
8724 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
8726 struct sdt_note
*cur
=
8727 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
8730 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
8731 cur
->size
= (bfd_size_type
) note
->descsz
;
8732 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
8734 elf_tdata (abfd
)->sdt_note_head
= cur
;
8740 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8745 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
8753 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8757 cp
= strchr (note
->namedata
, '@');
8760 *lwpidp
= atoi(cp
+ 1);
8767 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8769 /* Signal number at offset 0x08. */
8770 elf_tdata (abfd
)->core
->signal
8771 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8773 /* Process ID at offset 0x50. */
8774 elf_tdata (abfd
)->core
->pid
8775 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8777 /* Command name at 0x7c (max 32 bytes, including nul). */
8778 elf_tdata (abfd
)->core
->command
8779 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8781 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8786 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8790 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8791 elf_tdata (abfd
)->core
->lwpid
= lwp
;
8793 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8795 /* NetBSD-specific core "procinfo". Note that we expect to
8796 find this note before any of the others, which is fine,
8797 since the kernel writes this note out first when it
8798 creates a core file. */
8800 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8803 /* As of Jan 2002 there are no other machine-independent notes
8804 defined for NetBSD core files. If the note type is less
8805 than the start of the machine-dependent note types, we don't
8808 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8812 switch (bfd_get_arch (abfd
))
8814 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8815 PT_GETFPREGS == mach+2. */
8817 case bfd_arch_alpha
:
8818 case bfd_arch_sparc
:
8821 case NT_NETBSDCORE_FIRSTMACH
+0:
8822 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8824 case NT_NETBSDCORE_FIRSTMACH
+2:
8825 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8831 /* On all other arch's, PT_GETREGS == mach+1 and
8832 PT_GETFPREGS == mach+3. */
8837 case NT_NETBSDCORE_FIRSTMACH
+1:
8838 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8840 case NT_NETBSDCORE_FIRSTMACH
+3:
8841 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8851 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8853 /* Signal number at offset 0x08. */
8854 elf_tdata (abfd
)->core
->signal
8855 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8857 /* Process ID at offset 0x20. */
8858 elf_tdata (abfd
)->core
->pid
8859 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
8861 /* Command name at 0x48 (max 32 bytes, including nul). */
8862 elf_tdata (abfd
)->core
->command
8863 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
8869 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8871 if (note
->type
== NT_OPENBSD_PROCINFO
)
8872 return elfcore_grok_openbsd_procinfo (abfd
, note
);
8874 if (note
->type
== NT_OPENBSD_REGS
)
8875 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8877 if (note
->type
== NT_OPENBSD_FPREGS
)
8878 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8880 if (note
->type
== NT_OPENBSD_XFPREGS
)
8881 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8883 if (note
->type
== NT_OPENBSD_AUXV
)
8885 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8890 sect
->size
= note
->descsz
;
8891 sect
->filepos
= note
->descpos
;
8892 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8897 if (note
->type
== NT_OPENBSD_WCOOKIE
)
8899 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
8904 sect
->size
= note
->descsz
;
8905 sect
->filepos
= note
->descpos
;
8906 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8915 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8917 void *ddata
= note
->descdata
;
8924 /* nto_procfs_status 'pid' field is at offset 0. */
8925 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8927 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8928 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8930 /* nto_procfs_status 'flags' field is at offset 8. */
8931 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8933 /* nto_procfs_status 'what' field is at offset 14. */
8934 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8936 elf_tdata (abfd
)->core
->signal
= sig
;
8937 elf_tdata (abfd
)->core
->lwpid
= *tid
;
8940 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8941 do not come from signals so we make sure we set the current
8942 thread just in case. */
8943 if (flags
& 0x00000080)
8944 elf_tdata (abfd
)->core
->lwpid
= *tid
;
8946 /* Make a ".qnx_core_status/%d" section. */
8947 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8949 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8954 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8958 sect
->size
= note
->descsz
;
8959 sect
->filepos
= note
->descpos
;
8960 sect
->alignment_power
= 2;
8962 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8966 elfcore_grok_nto_regs (bfd
*abfd
,
8967 Elf_Internal_Note
*note
,
8975 /* Make a "(base)/%d" section. */
8976 sprintf (buf
, "%s/%ld", base
, tid
);
8978 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8983 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8987 sect
->size
= note
->descsz
;
8988 sect
->filepos
= note
->descpos
;
8989 sect
->alignment_power
= 2;
8991 /* This is the current thread. */
8992 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
8993 return elfcore_maybe_make_sect (abfd
, base
, sect
);
8998 #define BFD_QNT_CORE_INFO 7
8999 #define BFD_QNT_CORE_STATUS 8
9000 #define BFD_QNT_CORE_GREG 9
9001 #define BFD_QNT_CORE_FPREG 10
9004 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9006 /* Every GREG section has a STATUS section before it. Store the
9007 tid from the previous call to pass down to the next gregs
9009 static long tid
= 1;
9013 case BFD_QNT_CORE_INFO
:
9014 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9015 case BFD_QNT_CORE_STATUS
:
9016 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9017 case BFD_QNT_CORE_GREG
:
9018 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9019 case BFD_QNT_CORE_FPREG
:
9020 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9027 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9033 /* Use note name as section name. */
9035 name
= (char *) bfd_alloc (abfd
, len
);
9038 memcpy (name
, note
->namedata
, len
);
9039 name
[len
- 1] = '\0';
9041 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9045 sect
->size
= note
->descsz
;
9046 sect
->filepos
= note
->descpos
;
9047 sect
->alignment_power
= 1;
9052 /* Function: elfcore_write_note
9055 buffer to hold note, and current size of buffer
9059 size of data for note
9061 Writes note to end of buffer. ELF64 notes are written exactly as
9062 for ELF32, despite the current (as of 2006) ELF gabi specifying
9063 that they ought to have 8-byte namesz and descsz field, and have
9064 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9067 Pointer to realloc'd buffer, *BUFSIZ updated. */
9070 elfcore_write_note (bfd
*abfd
,
9078 Elf_External_Note
*xnp
;
9085 namesz
= strlen (name
) + 1;
9087 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9089 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9092 dest
= buf
+ *bufsiz
;
9093 *bufsiz
+= newspace
;
9094 xnp
= (Elf_External_Note
*) dest
;
9095 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9096 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9097 H_PUT_32 (abfd
, type
, xnp
->type
);
9101 memcpy (dest
, name
, namesz
);
9109 memcpy (dest
, input
, size
);
9120 elfcore_write_prpsinfo (bfd
*abfd
,
9126 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9128 if (bed
->elf_backend_write_core_note
!= NULL
)
9131 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9132 NT_PRPSINFO
, fname
, psargs
);
9137 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9138 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9139 if (bed
->s
->elfclass
== ELFCLASS32
)
9141 #if defined (HAVE_PSINFO32_T)
9143 int note_type
= NT_PSINFO
;
9146 int note_type
= NT_PRPSINFO
;
9149 memset (&data
, 0, sizeof (data
));
9150 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9151 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9152 return elfcore_write_note (abfd
, buf
, bufsiz
,
9153 "CORE", note_type
, &data
, sizeof (data
));
9158 #if defined (HAVE_PSINFO_T)
9160 int note_type
= NT_PSINFO
;
9163 int note_type
= NT_PRPSINFO
;
9166 memset (&data
, 0, sizeof (data
));
9167 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9168 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9169 return elfcore_write_note (abfd
, buf
, bufsiz
,
9170 "CORE", note_type
, &data
, sizeof (data
));
9172 #endif /* PSINFO_T or PRPSINFO_T */
9179 elfcore_write_linux_prpsinfo32
9180 (bfd
*abfd
, char *buf
, int *bufsiz
,
9181 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9183 struct elf_external_linux_prpsinfo32 data
;
9185 memset (&data
, 0, sizeof (data
));
9186 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9188 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9189 &data
, sizeof (data
));
9193 elfcore_write_linux_prpsinfo64
9194 (bfd
*abfd
, char *buf
, int *bufsiz
,
9195 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9197 struct elf_external_linux_prpsinfo64 data
;
9199 memset (&data
, 0, sizeof (data
));
9200 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9202 return elfcore_write_note (abfd
, buf
, bufsiz
,
9203 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9207 elfcore_write_prstatus (bfd
*abfd
,
9214 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9216 if (bed
->elf_backend_write_core_note
!= NULL
)
9219 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9221 pid
, cursig
, gregs
);
9226 #if defined (HAVE_PRSTATUS_T)
9227 #if defined (HAVE_PRSTATUS32_T)
9228 if (bed
->s
->elfclass
== ELFCLASS32
)
9230 prstatus32_t prstat
;
9232 memset (&prstat
, 0, sizeof (prstat
));
9233 prstat
.pr_pid
= pid
;
9234 prstat
.pr_cursig
= cursig
;
9235 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9236 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9237 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9244 memset (&prstat
, 0, sizeof (prstat
));
9245 prstat
.pr_pid
= pid
;
9246 prstat
.pr_cursig
= cursig
;
9247 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9248 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9249 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9251 #endif /* HAVE_PRSTATUS_T */
9257 #if defined (HAVE_LWPSTATUS_T)
9259 elfcore_write_lwpstatus (bfd
*abfd
,
9266 lwpstatus_t lwpstat
;
9267 const char *note_name
= "CORE";
9269 memset (&lwpstat
, 0, sizeof (lwpstat
));
9270 lwpstat
.pr_lwpid
= pid
>> 16;
9271 lwpstat
.pr_cursig
= cursig
;
9272 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9273 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9274 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9276 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9277 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9279 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9280 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9283 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9284 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9286 #endif /* HAVE_LWPSTATUS_T */
9288 #if defined (HAVE_PSTATUS_T)
9290 elfcore_write_pstatus (bfd
*abfd
,
9294 int cursig ATTRIBUTE_UNUSED
,
9295 const void *gregs ATTRIBUTE_UNUSED
)
9297 const char *note_name
= "CORE";
9298 #if defined (HAVE_PSTATUS32_T)
9299 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9301 if (bed
->s
->elfclass
== ELFCLASS32
)
9305 memset (&pstat
, 0, sizeof (pstat
));
9306 pstat
.pr_pid
= pid
& 0xffff;
9307 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9308 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9316 memset (&pstat
, 0, sizeof (pstat
));
9317 pstat
.pr_pid
= pid
& 0xffff;
9318 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9319 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9323 #endif /* HAVE_PSTATUS_T */
9326 elfcore_write_prfpreg (bfd
*abfd
,
9332 const char *note_name
= "CORE";
9333 return elfcore_write_note (abfd
, buf
, bufsiz
,
9334 note_name
, NT_FPREGSET
, fpregs
, size
);
9338 elfcore_write_prxfpreg (bfd
*abfd
,
9341 const void *xfpregs
,
9344 char *note_name
= "LINUX";
9345 return elfcore_write_note (abfd
, buf
, bufsiz
,
9346 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9350 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9351 const void *xfpregs
, int size
)
9353 char *note_name
= "LINUX";
9354 return elfcore_write_note (abfd
, buf
, bufsiz
,
9355 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9359 elfcore_write_ppc_vmx (bfd
*abfd
,
9362 const void *ppc_vmx
,
9365 char *note_name
= "LINUX";
9366 return elfcore_write_note (abfd
, buf
, bufsiz
,
9367 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9371 elfcore_write_ppc_vsx (bfd
*abfd
,
9374 const void *ppc_vsx
,
9377 char *note_name
= "LINUX";
9378 return elfcore_write_note (abfd
, buf
, bufsiz
,
9379 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9383 elfcore_write_s390_high_gprs (bfd
*abfd
,
9386 const void *s390_high_gprs
,
9389 char *note_name
= "LINUX";
9390 return elfcore_write_note (abfd
, buf
, bufsiz
,
9391 note_name
, NT_S390_HIGH_GPRS
,
9392 s390_high_gprs
, size
);
9396 elfcore_write_s390_timer (bfd
*abfd
,
9399 const void *s390_timer
,
9402 char *note_name
= "LINUX";
9403 return elfcore_write_note (abfd
, buf
, bufsiz
,
9404 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9408 elfcore_write_s390_todcmp (bfd
*abfd
,
9411 const void *s390_todcmp
,
9414 char *note_name
= "LINUX";
9415 return elfcore_write_note (abfd
, buf
, bufsiz
,
9416 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9420 elfcore_write_s390_todpreg (bfd
*abfd
,
9423 const void *s390_todpreg
,
9426 char *note_name
= "LINUX";
9427 return elfcore_write_note (abfd
, buf
, bufsiz
,
9428 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9432 elfcore_write_s390_ctrs (bfd
*abfd
,
9435 const void *s390_ctrs
,
9438 char *note_name
= "LINUX";
9439 return elfcore_write_note (abfd
, buf
, bufsiz
,
9440 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9444 elfcore_write_s390_prefix (bfd
*abfd
,
9447 const void *s390_prefix
,
9450 char *note_name
= "LINUX";
9451 return elfcore_write_note (abfd
, buf
, bufsiz
,
9452 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9456 elfcore_write_s390_last_break (bfd
*abfd
,
9459 const void *s390_last_break
,
9462 char *note_name
= "LINUX";
9463 return elfcore_write_note (abfd
, buf
, bufsiz
,
9464 note_name
, NT_S390_LAST_BREAK
,
9465 s390_last_break
, size
);
9469 elfcore_write_s390_system_call (bfd
*abfd
,
9472 const void *s390_system_call
,
9475 char *note_name
= "LINUX";
9476 return elfcore_write_note (abfd
, buf
, bufsiz
,
9477 note_name
, NT_S390_SYSTEM_CALL
,
9478 s390_system_call
, size
);
9482 elfcore_write_arm_vfp (bfd
*abfd
,
9485 const void *arm_vfp
,
9488 char *note_name
= "LINUX";
9489 return elfcore_write_note (abfd
, buf
, bufsiz
,
9490 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
9494 elfcore_write_aarch_tls (bfd
*abfd
,
9497 const void *aarch_tls
,
9500 char *note_name
= "LINUX";
9501 return elfcore_write_note (abfd
, buf
, bufsiz
,
9502 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
9506 elfcore_write_aarch_hw_break (bfd
*abfd
,
9509 const void *aarch_hw_break
,
9512 char *note_name
= "LINUX";
9513 return elfcore_write_note (abfd
, buf
, bufsiz
,
9514 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
9518 elfcore_write_aarch_hw_watch (bfd
*abfd
,
9521 const void *aarch_hw_watch
,
9524 char *note_name
= "LINUX";
9525 return elfcore_write_note (abfd
, buf
, bufsiz
,
9526 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
9530 elfcore_write_register_note (bfd
*abfd
,
9533 const char *section
,
9537 if (strcmp (section
, ".reg2") == 0)
9538 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9539 if (strcmp (section
, ".reg-xfp") == 0)
9540 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9541 if (strcmp (section
, ".reg-xstate") == 0)
9542 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9543 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9544 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9545 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9546 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9547 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9548 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9549 if (strcmp (section
, ".reg-s390-timer") == 0)
9550 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9551 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9552 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9553 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9554 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9555 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9556 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9557 if (strcmp (section
, ".reg-s390-prefix") == 0)
9558 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9559 if (strcmp (section
, ".reg-s390-last-break") == 0)
9560 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
9561 if (strcmp (section
, ".reg-s390-system-call") == 0)
9562 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
9563 if (strcmp (section
, ".reg-arm-vfp") == 0)
9564 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
9565 if (strcmp (section
, ".reg-aarch-tls") == 0)
9566 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
9567 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
9568 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
9569 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
9570 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
9575 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9580 while (p
< buf
+ size
)
9582 /* FIXME: bad alignment assumption. */
9583 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9584 Elf_Internal_Note in
;
9586 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9589 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9591 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9592 in
.namedata
= xnp
->name
;
9593 if (in
.namesz
> buf
- in
.namedata
+ size
)
9596 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9597 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9598 in
.descpos
= offset
+ (in
.descdata
- buf
);
9600 && (in
.descdata
>= buf
+ size
9601 || in
.descsz
> buf
- in
.descdata
+ size
))
9604 switch (bfd_get_format (abfd
))
9610 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
9612 if (! elfcore_grok_netbsd_note (abfd
, &in
))
9615 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
9617 if (! elfcore_grok_openbsd_note (abfd
, &in
))
9620 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
9622 if (! elfcore_grok_nto_note (abfd
, &in
))
9625 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
9627 if (! elfcore_grok_spu_note (abfd
, &in
))
9632 if (! elfcore_grok_note (abfd
, &in
))
9638 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9640 if (! elfobj_grok_gnu_note (abfd
, &in
))
9643 else if (in
.namesz
== sizeof "stapsdt"
9644 && strcmp (in
.namedata
, "stapsdt") == 0)
9646 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
9652 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9659 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9666 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9669 buf
= (char *) bfd_malloc (size
);
9673 if (bfd_bread (buf
, size
, abfd
) != size
9674 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9684 /* Providing external access to the ELF program header table. */
9686 /* Return an upper bound on the number of bytes required to store a
9687 copy of ABFD's program header table entries. Return -1 if an error
9688 occurs; bfd_get_error will return an appropriate code. */
9691 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9693 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9695 bfd_set_error (bfd_error_wrong_format
);
9699 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9702 /* Copy ABFD's program header table entries to *PHDRS. The entries
9703 will be stored as an array of Elf_Internal_Phdr structures, as
9704 defined in include/elf/internal.h. To find out how large the
9705 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9707 Return the number of program header table entries read, or -1 if an
9708 error occurs; bfd_get_error will return an appropriate code. */
9711 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9715 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9717 bfd_set_error (bfd_error_wrong_format
);
9721 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9722 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9723 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9728 enum elf_reloc_type_class
9729 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9731 return reloc_class_normal
;
9734 /* For RELA architectures, return the relocation value for a
9735 relocation against a local symbol. */
9738 _bfd_elf_rela_local_sym (bfd
*abfd
,
9739 Elf_Internal_Sym
*sym
,
9741 Elf_Internal_Rela
*rel
)
9743 asection
*sec
= *psec
;
9746 relocation
= (sec
->output_section
->vma
9747 + sec
->output_offset
9749 if ((sec
->flags
& SEC_MERGE
)
9750 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9751 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
9754 _bfd_merged_section_offset (abfd
, psec
,
9755 elf_section_data (sec
)->sec_info
,
9756 sym
->st_value
+ rel
->r_addend
);
9759 /* If we have changed the section, and our original section is
9760 marked with SEC_EXCLUDE, it means that the original
9761 SEC_MERGE section has been completely subsumed in some
9762 other SEC_MERGE section. In this case, we need to leave
9763 some info around for --emit-relocs. */
9764 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9765 sec
->kept_section
= *psec
;
9768 rel
->r_addend
-= relocation
;
9769 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9775 _bfd_elf_rel_local_sym (bfd
*abfd
,
9776 Elf_Internal_Sym
*sym
,
9780 asection
*sec
= *psec
;
9782 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
9783 return sym
->st_value
+ addend
;
9785 return _bfd_merged_section_offset (abfd
, psec
,
9786 elf_section_data (sec
)->sec_info
,
9787 sym
->st_value
+ addend
);
9791 _bfd_elf_section_offset (bfd
*abfd
,
9792 struct bfd_link_info
*info
,
9796 switch (sec
->sec_info_type
)
9798 case SEC_INFO_TYPE_STABS
:
9799 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
9801 case SEC_INFO_TYPE_EH_FRAME
:
9802 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
9804 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
9806 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9807 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
9808 offset
= sec
->size
- offset
- address_size
;
9814 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9815 reconstruct an ELF file by reading the segments out of remote memory
9816 based on the ELF file header at EHDR_VMA and the ELF program headers it
9817 points to. If not null, *LOADBASEP is filled in with the difference
9818 between the VMAs from which the segments were read, and the VMAs the
9819 file headers (and hence BFD's idea of each section's VMA) put them at.
9821 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9822 remote memory at target address VMA into the local buffer at MYADDR; it
9823 should return zero on success or an `errno' code on failure. TEMPL must
9824 be a BFD for an ELF target with the word size and byte order found in
9825 the remote memory. */
9828 bfd_elf_bfd_from_remote_memory
9832 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
9834 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
9835 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
9839 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
9840 long symcount ATTRIBUTE_UNUSED
,
9841 asymbol
**syms ATTRIBUTE_UNUSED
,
9846 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9849 const char *relplt_name
;
9850 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
9854 Elf_Internal_Shdr
*hdr
;
9860 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
9863 if (dynsymcount
<= 0)
9866 if (!bed
->plt_sym_val
)
9869 relplt_name
= bed
->relplt_name
;
9870 if (relplt_name
== NULL
)
9871 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
9872 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
9876 hdr
= &elf_section_data (relplt
)->this_hdr
;
9877 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
9878 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
9881 plt
= bfd_get_section_by_name (abfd
, ".plt");
9885 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9886 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
9889 count
= relplt
->size
/ hdr
->sh_entsize
;
9890 size
= count
* sizeof (asymbol
);
9891 p
= relplt
->relocation
;
9892 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9894 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
9898 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
9900 size
+= sizeof ("+0x") - 1 + 8;
9905 s
= *ret
= (asymbol
*) bfd_malloc (size
);
9909 names
= (char *) (s
+ count
);
9910 p
= relplt
->relocation
;
9912 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9917 addr
= bed
->plt_sym_val (i
, plt
, p
);
9918 if (addr
== (bfd_vma
) -1)
9921 *s
= **p
->sym_ptr_ptr
;
9922 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9923 we are defining a symbol, ensure one of them is set. */
9924 if ((s
->flags
& BSF_LOCAL
) == 0)
9925 s
->flags
|= BSF_GLOBAL
;
9926 s
->flags
|= BSF_SYNTHETIC
;
9928 s
->value
= addr
- plt
->vma
;
9931 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
9932 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
9938 memcpy (names
, "+0x", sizeof ("+0x") - 1);
9939 names
+= sizeof ("+0x") - 1;
9940 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
9941 for (a
= buf
; *a
== '0'; ++a
)
9944 memcpy (names
, a
, len
);
9947 memcpy (names
, "@plt", sizeof ("@plt"));
9948 names
+= sizeof ("@plt");
9955 /* It is only used by x86-64 so far. */
9956 asection _bfd_elf_large_com_section
9957 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
9958 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
9961 _bfd_elf_set_osabi (bfd
* abfd
,
9962 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
9964 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
9966 i_ehdrp
= elf_elfheader (abfd
);
9968 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
9970 /* To make things simpler for the loader on Linux systems we set the
9971 osabi field to ELFOSABI_GNU if the binary contains symbols of
9972 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
9973 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
9974 && elf_tdata (abfd
)->has_gnu_symbols
)
9975 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
9979 /* Return TRUE for ELF symbol types that represent functions.
9980 This is the default version of this function, which is sufficient for
9981 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9984 _bfd_elf_is_function_type (unsigned int type
)
9986 return (type
== STT_FUNC
9987 || type
== STT_GNU_IFUNC
);
9990 /* If the ELF symbol SYM might be a function in SEC, return the
9991 function size and set *CODE_OFF to the function's entry point,
9992 otherwise return zero. */
9995 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10000 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10001 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10002 || sym
->section
!= sec
)
10005 *code_off
= sym
->value
;
10007 if (!(sym
->flags
& BSF_SYNTHETIC
))
10008 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;