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 if (abfd
->direction
!= read_direction
)
251 struct output_elf_obj_tdata
*o
= bfd_zalloc (abfd
, sizeof *o
);
254 elf_tdata (abfd
)->o
= o
;
255 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
262 bfd_elf_make_object (bfd
*abfd
)
264 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
265 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
270 bfd_elf_mkcorefile (bfd
*abfd
)
272 /* I think this can be done just like an object file. */
273 if (!abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
))
275 elf_tdata (abfd
)->core
= bfd_zalloc (abfd
, sizeof (*elf_tdata (abfd
)->core
));
276 return elf_tdata (abfd
)->core
!= NULL
;
280 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
282 Elf_Internal_Shdr
**i_shdrp
;
283 bfd_byte
*shstrtab
= NULL
;
285 bfd_size_type shstrtabsize
;
287 i_shdrp
= elf_elfsections (abfd
);
289 || shindex
>= elf_numsections (abfd
)
290 || i_shdrp
[shindex
] == 0)
293 shstrtab
= i_shdrp
[shindex
]->contents
;
294 if (shstrtab
== NULL
)
296 /* No cached one, attempt to read, and cache what we read. */
297 offset
= i_shdrp
[shindex
]->sh_offset
;
298 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
300 /* Allocate and clear an extra byte at the end, to prevent crashes
301 in case the string table is not terminated. */
302 if (shstrtabsize
+ 1 <= 1
303 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
304 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
306 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
308 if (bfd_get_error () != bfd_error_system_call
)
309 bfd_set_error (bfd_error_file_truncated
);
311 /* Once we've failed to read it, make sure we don't keep
312 trying. Otherwise, we'll keep allocating space for
313 the string table over and over. */
314 i_shdrp
[shindex
]->sh_size
= 0;
317 shstrtab
[shstrtabsize
] = '\0';
318 i_shdrp
[shindex
]->contents
= shstrtab
;
320 return (char *) shstrtab
;
324 bfd_elf_string_from_elf_section (bfd
*abfd
,
325 unsigned int shindex
,
326 unsigned int strindex
)
328 Elf_Internal_Shdr
*hdr
;
333 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
336 hdr
= elf_elfsections (abfd
)[shindex
];
338 if (hdr
->contents
== NULL
339 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
342 if (strindex
>= hdr
->sh_size
)
344 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
345 (*_bfd_error_handler
)
346 (_("%B: invalid string offset %u >= %lu for section `%s'"),
347 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
348 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
350 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
354 return ((char *) hdr
->contents
) + strindex
;
357 /* Read and convert symbols to internal format.
358 SYMCOUNT specifies the number of symbols to read, starting from
359 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
360 are non-NULL, they are used to store the internal symbols, external
361 symbols, and symbol section index extensions, respectively.
362 Returns a pointer to the internal symbol buffer (malloced if necessary)
363 or NULL if there were no symbols or some kind of problem. */
366 bfd_elf_get_elf_syms (bfd
*ibfd
,
367 Elf_Internal_Shdr
*symtab_hdr
,
370 Elf_Internal_Sym
*intsym_buf
,
372 Elf_External_Sym_Shndx
*extshndx_buf
)
374 Elf_Internal_Shdr
*shndx_hdr
;
376 const bfd_byte
*esym
;
377 Elf_External_Sym_Shndx
*alloc_extshndx
;
378 Elf_External_Sym_Shndx
*shndx
;
379 Elf_Internal_Sym
*alloc_intsym
;
380 Elf_Internal_Sym
*isym
;
381 Elf_Internal_Sym
*isymend
;
382 const struct elf_backend_data
*bed
;
387 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
393 /* Normal syms might have section extension entries. */
395 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
396 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
398 /* Read the symbols. */
400 alloc_extshndx
= NULL
;
402 bed
= get_elf_backend_data (ibfd
);
403 extsym_size
= bed
->s
->sizeof_sym
;
404 amt
= symcount
* extsym_size
;
405 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
406 if (extsym_buf
== NULL
)
408 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
409 extsym_buf
= alloc_ext
;
411 if (extsym_buf
== NULL
412 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
413 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
419 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
423 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
424 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
425 if (extshndx_buf
== NULL
)
427 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
428 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
429 extshndx_buf
= alloc_extshndx
;
431 if (extshndx_buf
== NULL
432 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
433 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
440 if (intsym_buf
== NULL
)
442 alloc_intsym
= (Elf_Internal_Sym
*)
443 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
444 intsym_buf
= alloc_intsym
;
445 if (intsym_buf
== NULL
)
449 /* Convert the symbols to internal form. */
450 isymend
= intsym_buf
+ symcount
;
451 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
452 shndx
= extshndx_buf
;
454 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
455 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
457 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
458 (*_bfd_error_handler
) (_("%B symbol number %lu references "
459 "nonexistent SHT_SYMTAB_SHNDX section"),
460 ibfd
, (unsigned long) symoffset
);
461 if (alloc_intsym
!= NULL
)
468 if (alloc_ext
!= NULL
)
470 if (alloc_extshndx
!= NULL
)
471 free (alloc_extshndx
);
476 /* Look up a symbol name. */
478 bfd_elf_sym_name (bfd
*abfd
,
479 Elf_Internal_Shdr
*symtab_hdr
,
480 Elf_Internal_Sym
*isym
,
484 unsigned int iname
= isym
->st_name
;
485 unsigned int shindex
= symtab_hdr
->sh_link
;
487 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
488 /* Check for a bogus st_shndx to avoid crashing. */
489 && isym
->st_shndx
< elf_numsections (abfd
))
491 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
492 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
495 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
498 else if (sym_sec
&& *name
== '\0')
499 name
= bfd_section_name (abfd
, sym_sec
);
504 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
505 sections. The first element is the flags, the rest are section
508 typedef union elf_internal_group
{
509 Elf_Internal_Shdr
*shdr
;
511 } Elf_Internal_Group
;
513 /* Return the name of the group signature symbol. Why isn't the
514 signature just a string? */
517 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
519 Elf_Internal_Shdr
*hdr
;
520 unsigned char esym
[sizeof (Elf64_External_Sym
)];
521 Elf_External_Sym_Shndx eshndx
;
522 Elf_Internal_Sym isym
;
524 /* First we need to ensure the symbol table is available. Make sure
525 that it is a symbol table section. */
526 if (ghdr
->sh_link
>= elf_numsections (abfd
))
528 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
529 if (hdr
->sh_type
!= SHT_SYMTAB
530 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
533 /* Go read the symbol. */
534 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
535 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
536 &isym
, esym
, &eshndx
) == NULL
)
539 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
542 /* Set next_in_group list pointer, and group name for NEWSECT. */
545 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
547 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
549 /* If num_group is zero, read in all SHT_GROUP sections. The count
550 is set to -1 if there are no SHT_GROUP sections. */
553 unsigned int i
, shnum
;
555 /* First count the number of groups. If we have a SHT_GROUP
556 section with just a flag word (ie. sh_size is 4), ignore it. */
557 shnum
= elf_numsections (abfd
);
560 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
561 ( (shdr)->sh_type == SHT_GROUP \
562 && (shdr)->sh_size >= minsize \
563 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
564 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
566 for (i
= 0; i
< shnum
; i
++)
568 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
570 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
576 num_group
= (unsigned) -1;
577 elf_tdata (abfd
)->num_group
= num_group
;
581 /* We keep a list of elf section headers for group sections,
582 so we can find them quickly. */
585 elf_tdata (abfd
)->num_group
= num_group
;
586 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
587 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
588 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
592 for (i
= 0; i
< shnum
; i
++)
594 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
596 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
599 Elf_Internal_Group
*dest
;
601 /* Add to list of sections. */
602 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
605 /* Read the raw contents. */
606 BFD_ASSERT (sizeof (*dest
) >= 4);
607 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
608 shdr
->contents
= (unsigned char *)
609 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
610 /* PR binutils/4110: Handle corrupt group headers. */
611 if (shdr
->contents
== NULL
)
614 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
615 bfd_set_error (bfd_error_bad_value
);
619 memset (shdr
->contents
, 0, amt
);
621 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
622 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
626 /* Translate raw contents, a flag word followed by an
627 array of elf section indices all in target byte order,
628 to the flag word followed by an array of elf section
630 src
= shdr
->contents
+ shdr
->sh_size
;
631 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
638 idx
= H_GET_32 (abfd
, src
);
639 if (src
== shdr
->contents
)
642 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
643 shdr
->bfd_section
->flags
644 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
649 ((*_bfd_error_handler
)
650 (_("%B: invalid SHT_GROUP entry"), abfd
));
653 dest
->shdr
= elf_elfsections (abfd
)[idx
];
660 if (num_group
!= (unsigned) -1)
664 for (i
= 0; i
< num_group
; i
++)
666 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
667 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
668 unsigned int n_elt
= shdr
->sh_size
/ 4;
670 /* Look through this group's sections to see if current
671 section is a member. */
673 if ((++idx
)->shdr
== hdr
)
677 /* We are a member of this group. Go looking through
678 other members to see if any others are linked via
680 idx
= (Elf_Internal_Group
*) shdr
->contents
;
681 n_elt
= shdr
->sh_size
/ 4;
683 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
684 && elf_next_in_group (s
) != NULL
)
688 /* Snarf the group name from other member, and
689 insert current section in circular list. */
690 elf_group_name (newsect
) = elf_group_name (s
);
691 elf_next_in_group (newsect
) = elf_next_in_group (s
);
692 elf_next_in_group (s
) = newsect
;
698 gname
= group_signature (abfd
, shdr
);
701 elf_group_name (newsect
) = gname
;
703 /* Start a circular list with one element. */
704 elf_next_in_group (newsect
) = newsect
;
707 /* If the group section has been created, point to the
709 if (shdr
->bfd_section
!= NULL
)
710 elf_next_in_group (shdr
->bfd_section
) = newsect
;
718 if (elf_group_name (newsect
) == NULL
)
720 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
727 _bfd_elf_setup_sections (bfd
*abfd
)
730 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
731 bfd_boolean result
= TRUE
;
734 /* Process SHF_LINK_ORDER. */
735 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
737 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
738 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
740 unsigned int elfsec
= this_hdr
->sh_link
;
741 /* FIXME: The old Intel compiler and old strip/objcopy may
742 not set the sh_link or sh_info fields. Hence we could
743 get the situation where elfsec is 0. */
746 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
747 if (bed
->link_order_error_handler
)
748 bed
->link_order_error_handler
749 (_("%B: warning: sh_link not set for section `%A'"),
754 asection
*linksec
= NULL
;
756 if (elfsec
< elf_numsections (abfd
))
758 this_hdr
= elf_elfsections (abfd
)[elfsec
];
759 linksec
= this_hdr
->bfd_section
;
763 Some strip/objcopy may leave an incorrect value in
764 sh_link. We don't want to proceed. */
767 (*_bfd_error_handler
)
768 (_("%B: sh_link [%d] in section `%A' is incorrect"),
769 s
->owner
, s
, elfsec
);
773 elf_linked_to_section (s
) = linksec
;
778 /* Process section groups. */
779 if (num_group
== (unsigned) -1)
782 for (i
= 0; i
< num_group
; i
++)
784 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
785 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
786 unsigned int n_elt
= shdr
->sh_size
/ 4;
789 if ((++idx
)->shdr
->bfd_section
)
790 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
791 else if (idx
->shdr
->sh_type
== SHT_RELA
792 || idx
->shdr
->sh_type
== SHT_REL
)
793 /* We won't include relocation sections in section groups in
794 output object files. We adjust the group section size here
795 so that relocatable link will work correctly when
796 relocation sections are in section group in input object
798 shdr
->bfd_section
->size
-= 4;
801 /* There are some unknown sections in the group. */
802 (*_bfd_error_handler
)
803 (_("%B: unknown [%d] section `%s' in group [%s]"),
805 (unsigned int) idx
->shdr
->sh_type
,
806 bfd_elf_string_from_elf_section (abfd
,
807 (elf_elfheader (abfd
)
810 shdr
->bfd_section
->name
);
818 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
820 return elf_next_in_group (sec
) != NULL
;
823 /* Make a BFD section from an ELF section. We store a pointer to the
824 BFD section in the bfd_section field of the header. */
827 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
828 Elf_Internal_Shdr
*hdr
,
834 const struct elf_backend_data
*bed
;
836 if (hdr
->bfd_section
!= NULL
)
839 newsect
= bfd_make_section_anyway (abfd
, name
);
843 hdr
->bfd_section
= newsect
;
844 elf_section_data (newsect
)->this_hdr
= *hdr
;
845 elf_section_data (newsect
)->this_idx
= shindex
;
847 /* Always use the real type/flags. */
848 elf_section_type (newsect
) = hdr
->sh_type
;
849 elf_section_flags (newsect
) = hdr
->sh_flags
;
851 newsect
->filepos
= hdr
->sh_offset
;
853 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
854 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
855 || ! bfd_set_section_alignment (abfd
, newsect
,
856 bfd_log2 (hdr
->sh_addralign
)))
859 flags
= SEC_NO_FLAGS
;
860 if (hdr
->sh_type
!= SHT_NOBITS
)
861 flags
|= SEC_HAS_CONTENTS
;
862 if (hdr
->sh_type
== SHT_GROUP
)
863 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
864 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
867 if (hdr
->sh_type
!= SHT_NOBITS
)
870 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
871 flags
|= SEC_READONLY
;
872 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
874 else if ((flags
& SEC_LOAD
) != 0)
876 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
879 newsect
->entsize
= hdr
->sh_entsize
;
880 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
881 flags
|= SEC_STRINGS
;
883 if (hdr
->sh_flags
& SHF_GROUP
)
884 if (!setup_group (abfd
, hdr
, newsect
))
886 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
887 flags
|= SEC_THREAD_LOCAL
;
888 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
889 flags
|= SEC_EXCLUDE
;
891 if ((flags
& SEC_ALLOC
) == 0)
893 /* The debugging sections appear to be recognized only by name,
894 not any sort of flag. Their SEC_ALLOC bits are cleared. */
901 else if (name
[1] == 'g' && name
[2] == 'n')
902 p
= ".gnu.linkonce.wi.", n
= 17;
903 else if (name
[1] == 'g' && name
[2] == 'd')
904 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
905 else if (name
[1] == 'l')
907 else if (name
[1] == 's')
909 else if (name
[1] == 'z')
910 p
= ".zdebug", n
= 7;
913 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
914 flags
|= SEC_DEBUGGING
;
918 /* As a GNU extension, if the name begins with .gnu.linkonce, we
919 only link a single copy of the section. This is used to support
920 g++. g++ will emit each template expansion in its own section.
921 The symbols will be defined as weak, so that multiple definitions
922 are permitted. The GNU linker extension is to actually discard
923 all but one of the sections. */
924 if (CONST_STRNEQ (name
, ".gnu.linkonce")
925 && elf_next_in_group (newsect
) == NULL
)
926 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
928 bed
= get_elf_backend_data (abfd
);
929 if (bed
->elf_backend_section_flags
)
930 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
933 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
936 /* We do not parse the PT_NOTE segments as we are interested even in the
937 separate debug info files which may have the segments offsets corrupted.
938 PT_NOTEs from the core files are currently not parsed using BFD. */
939 if (hdr
->sh_type
== SHT_NOTE
)
943 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
946 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
950 if ((flags
& SEC_ALLOC
) != 0)
952 Elf_Internal_Phdr
*phdr
;
953 unsigned int i
, nload
;
955 /* Some ELF linkers produce binaries with all the program header
956 p_paddr fields zero. If we have such a binary with more than
957 one PT_LOAD header, then leave the section lma equal to vma
958 so that we don't create sections with overlapping lma. */
959 phdr
= elf_tdata (abfd
)->phdr
;
960 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
961 if (phdr
->p_paddr
!= 0)
963 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
965 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
968 phdr
= elf_tdata (abfd
)->phdr
;
969 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
971 if (((phdr
->p_type
== PT_LOAD
972 && (hdr
->sh_flags
& SHF_TLS
) == 0)
973 || phdr
->p_type
== PT_TLS
)
974 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
976 if ((flags
& SEC_LOAD
) == 0)
977 newsect
->lma
= (phdr
->p_paddr
978 + hdr
->sh_addr
- phdr
->p_vaddr
);
980 /* We used to use the same adjustment for SEC_LOAD
981 sections, but that doesn't work if the segment
982 is packed with code from multiple VMAs.
983 Instead we calculate the section LMA based on
984 the segment LMA. It is assumed that the
985 segment will contain sections with contiguous
986 LMAs, even if the VMAs are not. */
987 newsect
->lma
= (phdr
->p_paddr
988 + hdr
->sh_offset
- phdr
->p_offset
);
990 /* With contiguous segments, we can't tell from file
991 offsets whether a section with zero size should
992 be placed at the end of one segment or the
993 beginning of the next. Decide based on vaddr. */
994 if (hdr
->sh_addr
>= phdr
->p_vaddr
995 && (hdr
->sh_addr
+ hdr
->sh_size
996 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1002 /* Compress/decompress DWARF debug sections with names: .debug_* and
1003 .zdebug_*, after the section flags is set. */
1004 if ((flags
& SEC_DEBUGGING
)
1005 && ((name
[1] == 'd' && name
[6] == '_')
1006 || (name
[1] == 'z' && name
[7] == '_')))
1008 enum { nothing
, compress
, decompress
} action
= nothing
;
1011 if (bfd_is_section_compressed (abfd
, newsect
))
1013 /* Compressed section. Check if we should decompress. */
1014 if ((abfd
->flags
& BFD_DECOMPRESS
))
1015 action
= decompress
;
1019 /* Normal section. Check if we should compress. */
1020 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
1030 if (!bfd_init_section_compress_status (abfd
, newsect
))
1032 (*_bfd_error_handler
)
1033 (_("%B: unable to initialize compress status for section %s"),
1039 unsigned int len
= strlen (name
);
1041 new_name
= bfd_alloc (abfd
, len
+ 2);
1042 if (new_name
== NULL
)
1046 memcpy (new_name
+ 2, name
+ 1, len
);
1050 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1052 (*_bfd_error_handler
)
1053 (_("%B: unable to initialize decompress status for section %s"),
1059 unsigned int len
= strlen (name
);
1061 new_name
= bfd_alloc (abfd
, len
);
1062 if (new_name
== NULL
)
1065 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1069 if (new_name
!= NULL
)
1070 bfd_rename_section (abfd
, newsect
, new_name
);
1076 const char *const bfd_elf_section_type_names
[] = {
1077 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1078 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1079 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1082 /* ELF relocs are against symbols. If we are producing relocatable
1083 output, and the reloc is against an external symbol, and nothing
1084 has given us any additional addend, the resulting reloc will also
1085 be against the same symbol. In such a case, we don't want to
1086 change anything about the way the reloc is handled, since it will
1087 all be done at final link time. Rather than put special case code
1088 into bfd_perform_relocation, all the reloc types use this howto
1089 function. It just short circuits the reloc if producing
1090 relocatable output against an external symbol. */
1092 bfd_reloc_status_type
1093 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1094 arelent
*reloc_entry
,
1096 void *data ATTRIBUTE_UNUSED
,
1097 asection
*input_section
,
1099 char **error_message ATTRIBUTE_UNUSED
)
1101 if (output_bfd
!= NULL
1102 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1103 && (! reloc_entry
->howto
->partial_inplace
1104 || reloc_entry
->addend
== 0))
1106 reloc_entry
->address
+= input_section
->output_offset
;
1107 return bfd_reloc_ok
;
1110 return bfd_reloc_continue
;
1113 /* Copy the program header and other data from one object module to
1117 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1119 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1120 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1123 BFD_ASSERT (!elf_flags_init (obfd
)
1124 || (elf_elfheader (obfd
)->e_flags
1125 == elf_elfheader (ibfd
)->e_flags
));
1127 elf_gp (obfd
) = elf_gp (ibfd
);
1128 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1129 elf_flags_init (obfd
) = TRUE
;
1131 /* Copy object attributes. */
1132 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1137 get_segment_type (unsigned int p_type
)
1142 case PT_NULL
: pt
= "NULL"; break;
1143 case PT_LOAD
: pt
= "LOAD"; break;
1144 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1145 case PT_INTERP
: pt
= "INTERP"; break;
1146 case PT_NOTE
: pt
= "NOTE"; break;
1147 case PT_SHLIB
: pt
= "SHLIB"; break;
1148 case PT_PHDR
: pt
= "PHDR"; break;
1149 case PT_TLS
: pt
= "TLS"; break;
1150 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1151 case PT_GNU_STACK
: pt
= "STACK"; break;
1152 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1153 default: pt
= NULL
; break;
1158 /* Print out the program headers. */
1161 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1163 FILE *f
= (FILE *) farg
;
1164 Elf_Internal_Phdr
*p
;
1166 bfd_byte
*dynbuf
= NULL
;
1168 p
= elf_tdata (abfd
)->phdr
;
1173 fprintf (f
, _("\nProgram Header:\n"));
1174 c
= elf_elfheader (abfd
)->e_phnum
;
1175 for (i
= 0; i
< c
; i
++, p
++)
1177 const char *pt
= get_segment_type (p
->p_type
);
1182 sprintf (buf
, "0x%lx", p
->p_type
);
1185 fprintf (f
, "%8s off 0x", pt
);
1186 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1187 fprintf (f
, " vaddr 0x");
1188 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1189 fprintf (f
, " paddr 0x");
1190 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1191 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1192 fprintf (f
, " filesz 0x");
1193 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1194 fprintf (f
, " memsz 0x");
1195 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1196 fprintf (f
, " flags %c%c%c",
1197 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1198 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1199 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1200 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1201 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1206 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1209 unsigned int elfsec
;
1210 unsigned long shlink
;
1211 bfd_byte
*extdyn
, *extdynend
;
1213 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1215 fprintf (f
, _("\nDynamic Section:\n"));
1217 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1220 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1221 if (elfsec
== SHN_BAD
)
1223 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1225 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1226 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1229 extdynend
= extdyn
+ s
->size
;
1230 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1232 Elf_Internal_Dyn dyn
;
1233 const char *name
= "";
1235 bfd_boolean stringp
;
1236 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1238 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1240 if (dyn
.d_tag
== DT_NULL
)
1247 if (bed
->elf_backend_get_target_dtag
)
1248 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1250 if (!strcmp (name
, ""))
1252 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1257 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1258 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1259 case DT_PLTGOT
: name
= "PLTGOT"; break;
1260 case DT_HASH
: name
= "HASH"; break;
1261 case DT_STRTAB
: name
= "STRTAB"; break;
1262 case DT_SYMTAB
: name
= "SYMTAB"; break;
1263 case DT_RELA
: name
= "RELA"; break;
1264 case DT_RELASZ
: name
= "RELASZ"; break;
1265 case DT_RELAENT
: name
= "RELAENT"; break;
1266 case DT_STRSZ
: name
= "STRSZ"; break;
1267 case DT_SYMENT
: name
= "SYMENT"; break;
1268 case DT_INIT
: name
= "INIT"; break;
1269 case DT_FINI
: name
= "FINI"; break;
1270 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1271 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1272 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1273 case DT_REL
: name
= "REL"; break;
1274 case DT_RELSZ
: name
= "RELSZ"; break;
1275 case DT_RELENT
: name
= "RELENT"; break;
1276 case DT_PLTREL
: name
= "PLTREL"; break;
1277 case DT_DEBUG
: name
= "DEBUG"; break;
1278 case DT_TEXTREL
: name
= "TEXTREL"; break;
1279 case DT_JMPREL
: name
= "JMPREL"; break;
1280 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1281 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1282 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1283 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1284 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1285 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1286 case DT_FLAGS
: name
= "FLAGS"; break;
1287 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1288 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1289 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1290 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1291 case DT_MOVEENT
: name
= "MOVEENT"; break;
1292 case DT_MOVESZ
: name
= "MOVESZ"; break;
1293 case DT_FEATURE
: name
= "FEATURE"; break;
1294 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1295 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1296 case DT_SYMINENT
: name
= "SYMINENT"; break;
1297 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1298 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1299 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1300 case DT_PLTPAD
: name
= "PLTPAD"; break;
1301 case DT_MOVETAB
: name
= "MOVETAB"; break;
1302 case DT_SYMINFO
: name
= "SYMINFO"; break;
1303 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1304 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1305 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1306 case DT_VERSYM
: name
= "VERSYM"; break;
1307 case DT_VERDEF
: name
= "VERDEF"; break;
1308 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1309 case DT_VERNEED
: name
= "VERNEED"; break;
1310 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1311 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1312 case DT_USED
: name
= "USED"; break;
1313 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1314 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1317 fprintf (f
, " %-20s ", name
);
1321 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1326 unsigned int tagv
= dyn
.d_un
.d_val
;
1328 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1331 fprintf (f
, "%s", string
);
1340 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1341 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1343 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1347 if (elf_dynverdef (abfd
) != 0)
1349 Elf_Internal_Verdef
*t
;
1351 fprintf (f
, _("\nVersion definitions:\n"));
1352 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1354 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1355 t
->vd_flags
, t
->vd_hash
,
1356 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1357 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1359 Elf_Internal_Verdaux
*a
;
1362 for (a
= t
->vd_auxptr
->vda_nextptr
;
1366 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1372 if (elf_dynverref (abfd
) != 0)
1374 Elf_Internal_Verneed
*t
;
1376 fprintf (f
, _("\nVersion References:\n"));
1377 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1379 Elf_Internal_Vernaux
*a
;
1381 fprintf (f
, _(" required from %s:\n"),
1382 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1383 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1384 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1385 a
->vna_flags
, a
->vna_other
,
1386 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1398 /* Display ELF-specific fields of a symbol. */
1401 bfd_elf_print_symbol (bfd
*abfd
,
1404 bfd_print_symbol_type how
)
1406 FILE *file
= (FILE *) filep
;
1409 case bfd_print_symbol_name
:
1410 fprintf (file
, "%s", symbol
->name
);
1412 case bfd_print_symbol_more
:
1413 fprintf (file
, "elf ");
1414 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1415 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1417 case bfd_print_symbol_all
:
1419 const char *section_name
;
1420 const char *name
= NULL
;
1421 const struct elf_backend_data
*bed
;
1422 unsigned char st_other
;
1425 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1427 bed
= get_elf_backend_data (abfd
);
1428 if (bed
->elf_backend_print_symbol_all
)
1429 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1433 name
= symbol
->name
;
1434 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1437 fprintf (file
, " %s\t", section_name
);
1438 /* Print the "other" value for a symbol. For common symbols,
1439 we've already printed the size; now print the alignment.
1440 For other symbols, we have no specified alignment, and
1441 we've printed the address; now print the size. */
1442 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1443 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1445 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1446 bfd_fprintf_vma (abfd
, file
, val
);
1448 /* If we have version information, print it. */
1449 if (elf_dynversym (abfd
) != 0
1450 && (elf_dynverdef (abfd
) != 0
1451 || elf_dynverref (abfd
) != 0))
1453 unsigned int vernum
;
1454 const char *version_string
;
1456 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1459 version_string
= "";
1460 else if (vernum
== 1)
1461 version_string
= "Base";
1462 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1464 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1467 Elf_Internal_Verneed
*t
;
1469 version_string
= "";
1470 for (t
= elf_tdata (abfd
)->verref
;
1474 Elf_Internal_Vernaux
*a
;
1476 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1478 if (a
->vna_other
== vernum
)
1480 version_string
= a
->vna_nodename
;
1487 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1488 fprintf (file
, " %-11s", version_string
);
1493 fprintf (file
, " (%s)", version_string
);
1494 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1499 /* If the st_other field is not zero, print it. */
1500 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1505 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1506 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1507 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1509 /* Some other non-defined flags are also present, so print
1511 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1514 fprintf (file
, " %s", name
);
1520 /* Allocate an ELF string table--force the first byte to be zero. */
1522 struct bfd_strtab_hash
*
1523 _bfd_elf_stringtab_init (void)
1525 struct bfd_strtab_hash
*ret
;
1527 ret
= _bfd_stringtab_init ();
1532 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1533 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1534 if (loc
== (bfd_size_type
) -1)
1536 _bfd_stringtab_free (ret
);
1543 /* ELF .o/exec file reading */
1545 /* Create a new bfd section from an ELF section header. */
1548 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1550 Elf_Internal_Shdr
*hdr
;
1551 Elf_Internal_Ehdr
*ehdr
;
1552 const struct elf_backend_data
*bed
;
1555 if (shindex
>= elf_numsections (abfd
))
1558 hdr
= elf_elfsections (abfd
)[shindex
];
1559 ehdr
= elf_elfheader (abfd
);
1560 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1565 bed
= get_elf_backend_data (abfd
);
1566 switch (hdr
->sh_type
)
1569 /* Inactive section. Throw it away. */
1572 case SHT_PROGBITS
: /* Normal section with contents. */
1573 case SHT_NOBITS
: /* .bss section. */
1574 case SHT_HASH
: /* .hash section. */
1575 case SHT_NOTE
: /* .note section. */
1576 case SHT_INIT_ARRAY
: /* .init_array section. */
1577 case SHT_FINI_ARRAY
: /* .fini_array section. */
1578 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1579 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1580 case SHT_GNU_HASH
: /* .gnu.hash section. */
1581 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1583 case SHT_DYNAMIC
: /* Dynamic linking information. */
1584 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1586 if (hdr
->sh_link
> elf_numsections (abfd
))
1588 /* PR 10478: Accept Solaris binaries with a sh_link
1589 field set to SHN_BEFORE or SHN_AFTER. */
1590 switch (bfd_get_arch (abfd
))
1593 case bfd_arch_sparc
:
1594 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1595 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1597 /* Otherwise fall through. */
1602 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1604 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1606 Elf_Internal_Shdr
*dynsymhdr
;
1608 /* The shared libraries distributed with hpux11 have a bogus
1609 sh_link field for the ".dynamic" section. Find the
1610 string table for the ".dynsym" section instead. */
1611 if (elf_dynsymtab (abfd
) != 0)
1613 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1614 hdr
->sh_link
= dynsymhdr
->sh_link
;
1618 unsigned int i
, num_sec
;
1620 num_sec
= elf_numsections (abfd
);
1621 for (i
= 1; i
< num_sec
; i
++)
1623 dynsymhdr
= elf_elfsections (abfd
)[i
];
1624 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1626 hdr
->sh_link
= dynsymhdr
->sh_link
;
1634 case SHT_SYMTAB
: /* A symbol table */
1635 if (elf_onesymtab (abfd
) == shindex
)
1638 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1640 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1642 if (hdr
->sh_size
!= 0)
1644 /* Some assemblers erroneously set sh_info to one with a
1645 zero sh_size. ld sees this as a global symbol count
1646 of (unsigned) -1. Fix it here. */
1650 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1651 elf_onesymtab (abfd
) = shindex
;
1652 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1653 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1654 abfd
->flags
|= HAS_SYMS
;
1656 /* Sometimes a shared object will map in the symbol table. If
1657 SHF_ALLOC is set, and this is a shared object, then we also
1658 treat this section as a BFD section. We can not base the
1659 decision purely on SHF_ALLOC, because that flag is sometimes
1660 set in a relocatable object file, which would confuse the
1662 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1663 && (abfd
->flags
& DYNAMIC
) != 0
1664 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1668 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1669 can't read symbols without that section loaded as well. It
1670 is most likely specified by the next section header. */
1671 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1673 unsigned int i
, num_sec
;
1675 num_sec
= elf_numsections (abfd
);
1676 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1678 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1679 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1680 && hdr2
->sh_link
== shindex
)
1684 for (i
= 1; i
< shindex
; i
++)
1686 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1687 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1688 && hdr2
->sh_link
== shindex
)
1692 return bfd_section_from_shdr (abfd
, i
);
1696 case SHT_DYNSYM
: /* A dynamic symbol table */
1697 if (elf_dynsymtab (abfd
) == shindex
)
1700 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1702 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1704 if (hdr
->sh_size
!= 0)
1706 /* Some linkers erroneously set sh_info to one with a
1707 zero sh_size. ld sees this as a global symbol count
1708 of (unsigned) -1. Fix it here. */
1712 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1713 elf_dynsymtab (abfd
) = shindex
;
1714 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1715 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1716 abfd
->flags
|= HAS_SYMS
;
1718 /* Besides being a symbol table, we also treat this as a regular
1719 section, so that objcopy can handle it. */
1720 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1722 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1723 if (elf_symtab_shndx (abfd
) == shindex
)
1726 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1727 elf_symtab_shndx (abfd
) = shindex
;
1728 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1729 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1732 case SHT_STRTAB
: /* A string table */
1733 if (hdr
->bfd_section
!= NULL
)
1735 if (ehdr
->e_shstrndx
== shindex
)
1737 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1738 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1741 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1744 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1745 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1748 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1751 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1752 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1753 elf_elfsections (abfd
)[shindex
] = hdr
;
1754 /* We also treat this as a regular section, so that objcopy
1756 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1760 /* If the string table isn't one of the above, then treat it as a
1761 regular section. We need to scan all the headers to be sure,
1762 just in case this strtab section appeared before the above. */
1763 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1765 unsigned int i
, num_sec
;
1767 num_sec
= elf_numsections (abfd
);
1768 for (i
= 1; i
< num_sec
; i
++)
1770 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1771 if (hdr2
->sh_link
== shindex
)
1773 /* Prevent endless recursion on broken objects. */
1776 if (! bfd_section_from_shdr (abfd
, i
))
1778 if (elf_onesymtab (abfd
) == i
)
1780 if (elf_dynsymtab (abfd
) == i
)
1781 goto dynsymtab_strtab
;
1785 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1789 /* *These* do a lot of work -- but build no sections! */
1791 asection
*target_sect
;
1792 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1793 unsigned int num_sec
= elf_numsections (abfd
);
1794 struct bfd_elf_section_data
*esdt
;
1798 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1799 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1802 /* Check for a bogus link to avoid crashing. */
1803 if (hdr
->sh_link
>= num_sec
)
1805 ((*_bfd_error_handler
)
1806 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1807 abfd
, hdr
->sh_link
, name
, shindex
));
1808 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1812 /* For some incomprehensible reason Oracle distributes
1813 libraries for Solaris in which some of the objects have
1814 bogus sh_link fields. It would be nice if we could just
1815 reject them, but, unfortunately, some people need to use
1816 them. We scan through the section headers; if we find only
1817 one suitable symbol table, we clobber the sh_link to point
1818 to it. I hope this doesn't break anything.
1820 Don't do it on executable nor shared library. */
1821 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1822 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1823 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1829 for (scan
= 1; scan
< num_sec
; scan
++)
1831 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1832 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1843 hdr
->sh_link
= found
;
1846 /* Get the symbol table. */
1847 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1848 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1849 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1852 /* If this reloc section does not use the main symbol table we
1853 don't treat it as a reloc section. BFD can't adequately
1854 represent such a section, so at least for now, we don't
1855 try. We just present it as a normal section. We also
1856 can't use it as a reloc section if it points to the null
1857 section, an invalid section, another reloc section, or its
1858 sh_link points to the null section. */
1859 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1860 || hdr
->sh_link
== SHN_UNDEF
1861 || hdr
->sh_info
== SHN_UNDEF
1862 || hdr
->sh_info
>= num_sec
1863 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1864 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1865 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1868 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1870 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1871 if (target_sect
== NULL
)
1874 esdt
= elf_section_data (target_sect
);
1875 if (hdr
->sh_type
== SHT_RELA
)
1876 p_hdr
= &esdt
->rela
.hdr
;
1878 p_hdr
= &esdt
->rel
.hdr
;
1880 BFD_ASSERT (*p_hdr
== NULL
);
1881 amt
= sizeof (*hdr2
);
1882 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1887 elf_elfsections (abfd
)[shindex
] = hdr2
;
1888 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1889 target_sect
->flags
|= SEC_RELOC
;
1890 target_sect
->relocation
= NULL
;
1891 target_sect
->rel_filepos
= hdr
->sh_offset
;
1892 /* In the section to which the relocations apply, mark whether
1893 its relocations are of the REL or RELA variety. */
1894 if (hdr
->sh_size
!= 0)
1896 if (hdr
->sh_type
== SHT_RELA
)
1897 target_sect
->use_rela_p
= 1;
1899 abfd
->flags
|= HAS_RELOC
;
1903 case SHT_GNU_verdef
:
1904 elf_dynverdef (abfd
) = shindex
;
1905 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1906 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1908 case SHT_GNU_versym
:
1909 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1911 elf_dynversym (abfd
) = shindex
;
1912 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1913 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1915 case SHT_GNU_verneed
:
1916 elf_dynverref (abfd
) = shindex
;
1917 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1918 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1924 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
1926 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1928 if (hdr
->contents
!= NULL
)
1930 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1931 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1934 if (idx
->flags
& GRP_COMDAT
)
1935 hdr
->bfd_section
->flags
1936 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1938 /* We try to keep the same section order as it comes in. */
1940 while (--n_elt
!= 0)
1944 if (idx
->shdr
!= NULL
1945 && (s
= idx
->shdr
->bfd_section
) != NULL
1946 && elf_next_in_group (s
) != NULL
)
1948 elf_next_in_group (hdr
->bfd_section
) = s
;
1956 /* Possibly an attributes section. */
1957 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1958 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1960 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1962 _bfd_elf_parse_attributes (abfd
, hdr
);
1966 /* Check for any processor-specific section types. */
1967 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1970 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1972 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1973 /* FIXME: How to properly handle allocated section reserved
1974 for applications? */
1975 (*_bfd_error_handler
)
1976 (_("%B: don't know how to handle allocated, application "
1977 "specific section `%s' [0x%8x]"),
1978 abfd
, name
, hdr
->sh_type
);
1980 /* Allow sections reserved for applications. */
1981 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1984 else if (hdr
->sh_type
>= SHT_LOPROC
1985 && hdr
->sh_type
<= SHT_HIPROC
)
1986 /* FIXME: We should handle this section. */
1987 (*_bfd_error_handler
)
1988 (_("%B: don't know how to handle processor specific section "
1990 abfd
, name
, hdr
->sh_type
);
1991 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1993 /* Unrecognised OS-specific sections. */
1994 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1995 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1996 required to correctly process the section and the file should
1997 be rejected with an error message. */
1998 (*_bfd_error_handler
)
1999 (_("%B: don't know how to handle OS specific section "
2001 abfd
, name
, hdr
->sh_type
);
2003 /* Otherwise it should be processed. */
2004 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
2007 /* FIXME: We should handle this section. */
2008 (*_bfd_error_handler
)
2009 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2010 abfd
, name
, hdr
->sh_type
);
2018 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2021 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2023 unsigned long r_symndx
)
2025 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2027 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2029 Elf_Internal_Shdr
*symtab_hdr
;
2030 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2031 Elf_External_Sym_Shndx eshndx
;
2033 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2034 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2035 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2038 if (cache
->abfd
!= abfd
)
2040 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2043 cache
->indx
[ent
] = r_symndx
;
2046 return &cache
->sym
[ent
];
2049 /* Given an ELF section number, retrieve the corresponding BFD
2053 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2055 if (sec_index
>= elf_numsections (abfd
))
2057 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2060 static const struct bfd_elf_special_section special_sections_b
[] =
2062 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2063 { NULL
, 0, 0, 0, 0 }
2066 static const struct bfd_elf_special_section special_sections_c
[] =
2068 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2069 { NULL
, 0, 0, 0, 0 }
2072 static const struct bfd_elf_special_section special_sections_d
[] =
2074 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2075 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2076 /* There are more DWARF sections than these, but they needn't be added here
2077 unless you have to cope with broken compilers that don't emit section
2078 attributes or you want to help the user writing assembler. */
2079 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2080 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2081 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2082 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2083 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2084 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2085 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2086 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2087 { NULL
, 0, 0, 0, 0 }
2090 static const struct bfd_elf_special_section special_sections_f
[] =
2092 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2093 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2094 { NULL
, 0, 0, 0, 0 }
2097 static const struct bfd_elf_special_section special_sections_g
[] =
2099 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2100 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2101 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2102 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2103 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2104 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2105 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2106 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2107 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2108 { NULL
, 0, 0, 0, 0 }
2111 static const struct bfd_elf_special_section special_sections_h
[] =
2113 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2114 { NULL
, 0, 0, 0, 0 }
2117 static const struct bfd_elf_special_section special_sections_i
[] =
2119 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2120 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2121 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2122 { NULL
, 0, 0, 0, 0 }
2125 static const struct bfd_elf_special_section special_sections_l
[] =
2127 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2128 { NULL
, 0, 0, 0, 0 }
2131 static const struct bfd_elf_special_section special_sections_n
[] =
2133 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2134 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2135 { NULL
, 0, 0, 0, 0 }
2138 static const struct bfd_elf_special_section special_sections_p
[] =
2140 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2141 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2142 { NULL
, 0, 0, 0, 0 }
2145 static const struct bfd_elf_special_section special_sections_r
[] =
2147 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2148 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2149 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2150 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2151 { NULL
, 0, 0, 0, 0 }
2154 static const struct bfd_elf_special_section special_sections_s
[] =
2156 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2157 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2158 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2159 /* See struct bfd_elf_special_section declaration for the semantics of
2160 this special case where .prefix_length != strlen (.prefix). */
2161 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2162 { NULL
, 0, 0, 0, 0 }
2165 static const struct bfd_elf_special_section special_sections_t
[] =
2167 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2168 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2169 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2170 { NULL
, 0, 0, 0, 0 }
2173 static const struct bfd_elf_special_section special_sections_z
[] =
2175 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2176 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2177 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2178 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2179 { NULL
, 0, 0, 0, 0 }
2182 static const struct bfd_elf_special_section
* const special_sections
[] =
2184 special_sections_b
, /* 'b' */
2185 special_sections_c
, /* 'c' */
2186 special_sections_d
, /* 'd' */
2188 special_sections_f
, /* 'f' */
2189 special_sections_g
, /* 'g' */
2190 special_sections_h
, /* 'h' */
2191 special_sections_i
, /* 'i' */
2194 special_sections_l
, /* 'l' */
2196 special_sections_n
, /* 'n' */
2198 special_sections_p
, /* 'p' */
2200 special_sections_r
, /* 'r' */
2201 special_sections_s
, /* 's' */
2202 special_sections_t
, /* 't' */
2208 special_sections_z
/* 'z' */
2211 const struct bfd_elf_special_section
*
2212 _bfd_elf_get_special_section (const char *name
,
2213 const struct bfd_elf_special_section
*spec
,
2219 len
= strlen (name
);
2221 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2224 int prefix_len
= spec
[i
].prefix_length
;
2226 if (len
< prefix_len
)
2228 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2231 suffix_len
= spec
[i
].suffix_length
;
2232 if (suffix_len
<= 0)
2234 if (name
[prefix_len
] != 0)
2236 if (suffix_len
== 0)
2238 if (name
[prefix_len
] != '.'
2239 && (suffix_len
== -2
2240 || (rela
&& spec
[i
].type
== SHT_REL
)))
2246 if (len
< prefix_len
+ suffix_len
)
2248 if (memcmp (name
+ len
- suffix_len
,
2249 spec
[i
].prefix
+ prefix_len
,
2259 const struct bfd_elf_special_section
*
2260 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2263 const struct bfd_elf_special_section
*spec
;
2264 const struct elf_backend_data
*bed
;
2266 /* See if this is one of the special sections. */
2267 if (sec
->name
== NULL
)
2270 bed
= get_elf_backend_data (abfd
);
2271 spec
= bed
->special_sections
;
2274 spec
= _bfd_elf_get_special_section (sec
->name
,
2275 bed
->special_sections
,
2281 if (sec
->name
[0] != '.')
2284 i
= sec
->name
[1] - 'b';
2285 if (i
< 0 || i
> 'z' - 'b')
2288 spec
= special_sections
[i
];
2293 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2297 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2299 struct bfd_elf_section_data
*sdata
;
2300 const struct elf_backend_data
*bed
;
2301 const struct bfd_elf_special_section
*ssect
;
2303 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2306 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2310 sec
->used_by_bfd
= sdata
;
2313 /* Indicate whether or not this section should use RELA relocations. */
2314 bed
= get_elf_backend_data (abfd
);
2315 sec
->use_rela_p
= bed
->default_use_rela_p
;
2317 /* When we read a file, we don't need to set ELF section type and
2318 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2319 anyway. We will set ELF section type and flags for all linker
2320 created sections. If user specifies BFD section flags, we will
2321 set ELF section type and flags based on BFD section flags in
2322 elf_fake_sections. Special handling for .init_array/.fini_array
2323 output sections since they may contain .ctors/.dtors input
2324 sections. We don't want _bfd_elf_init_private_section_data to
2325 copy ELF section type from .ctors/.dtors input sections. */
2326 if (abfd
->direction
!= read_direction
2327 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2329 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2332 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2333 || ssect
->type
== SHT_INIT_ARRAY
2334 || ssect
->type
== SHT_FINI_ARRAY
))
2336 elf_section_type (sec
) = ssect
->type
;
2337 elf_section_flags (sec
) = ssect
->attr
;
2341 return _bfd_generic_new_section_hook (abfd
, sec
);
2344 /* Create a new bfd section from an ELF program header.
2346 Since program segments have no names, we generate a synthetic name
2347 of the form segment<NUM>, where NUM is generally the index in the
2348 program header table. For segments that are split (see below) we
2349 generate the names segment<NUM>a and segment<NUM>b.
2351 Note that some program segments may have a file size that is different than
2352 (less than) the memory size. All this means is that at execution the
2353 system must allocate the amount of memory specified by the memory size,
2354 but only initialize it with the first "file size" bytes read from the
2355 file. This would occur for example, with program segments consisting
2356 of combined data+bss.
2358 To handle the above situation, this routine generates TWO bfd sections
2359 for the single program segment. The first has the length specified by
2360 the file size of the segment, and the second has the length specified
2361 by the difference between the two sizes. In effect, the segment is split
2362 into its initialized and uninitialized parts.
2367 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2368 Elf_Internal_Phdr
*hdr
,
2370 const char *type_name
)
2378 split
= ((hdr
->p_memsz
> 0)
2379 && (hdr
->p_filesz
> 0)
2380 && (hdr
->p_memsz
> hdr
->p_filesz
));
2382 if (hdr
->p_filesz
> 0)
2384 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2385 len
= strlen (namebuf
) + 1;
2386 name
= (char *) bfd_alloc (abfd
, len
);
2389 memcpy (name
, namebuf
, len
);
2390 newsect
= bfd_make_section (abfd
, name
);
2391 if (newsect
== NULL
)
2393 newsect
->vma
= hdr
->p_vaddr
;
2394 newsect
->lma
= hdr
->p_paddr
;
2395 newsect
->size
= hdr
->p_filesz
;
2396 newsect
->filepos
= hdr
->p_offset
;
2397 newsect
->flags
|= SEC_HAS_CONTENTS
;
2398 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2399 if (hdr
->p_type
== PT_LOAD
)
2401 newsect
->flags
|= SEC_ALLOC
;
2402 newsect
->flags
|= SEC_LOAD
;
2403 if (hdr
->p_flags
& PF_X
)
2405 /* FIXME: all we known is that it has execute PERMISSION,
2407 newsect
->flags
|= SEC_CODE
;
2410 if (!(hdr
->p_flags
& PF_W
))
2412 newsect
->flags
|= SEC_READONLY
;
2416 if (hdr
->p_memsz
> hdr
->p_filesz
)
2420 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2421 len
= strlen (namebuf
) + 1;
2422 name
= (char *) bfd_alloc (abfd
, len
);
2425 memcpy (name
, namebuf
, len
);
2426 newsect
= bfd_make_section (abfd
, name
);
2427 if (newsect
== NULL
)
2429 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2430 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2431 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2432 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2433 align
= newsect
->vma
& -newsect
->vma
;
2434 if (align
== 0 || align
> hdr
->p_align
)
2435 align
= hdr
->p_align
;
2436 newsect
->alignment_power
= bfd_log2 (align
);
2437 if (hdr
->p_type
== PT_LOAD
)
2439 /* Hack for gdb. Segments that have not been modified do
2440 not have their contents written to a core file, on the
2441 assumption that a debugger can find the contents in the
2442 executable. We flag this case by setting the fake
2443 section size to zero. Note that "real" bss sections will
2444 always have their contents dumped to the core file. */
2445 if (bfd_get_format (abfd
) == bfd_core
)
2447 newsect
->flags
|= SEC_ALLOC
;
2448 if (hdr
->p_flags
& PF_X
)
2449 newsect
->flags
|= SEC_CODE
;
2451 if (!(hdr
->p_flags
& PF_W
))
2452 newsect
->flags
|= SEC_READONLY
;
2459 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2461 const struct elf_backend_data
*bed
;
2463 switch (hdr
->p_type
)
2466 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2469 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2472 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2475 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2478 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2480 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2485 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2488 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2490 case PT_GNU_EH_FRAME
:
2491 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2495 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2498 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2501 /* Check for any processor-specific program segment types. */
2502 bed
= get_elf_backend_data (abfd
);
2503 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2507 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2511 _bfd_elf_single_rel_hdr (asection
*sec
)
2513 if (elf_section_data (sec
)->rel
.hdr
)
2515 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2516 return elf_section_data (sec
)->rel
.hdr
;
2519 return elf_section_data (sec
)->rela
.hdr
;
2522 /* Allocate and initialize a section-header for a new reloc section,
2523 containing relocations against ASECT. It is stored in RELDATA. If
2524 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2528 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2529 struct bfd_elf_section_reloc_data
*reldata
,
2531 bfd_boolean use_rela_p
)
2533 Elf_Internal_Shdr
*rel_hdr
;
2535 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2538 amt
= sizeof (Elf_Internal_Shdr
);
2539 BFD_ASSERT (reldata
->hdr
== NULL
);
2540 rel_hdr
= bfd_zalloc (abfd
, amt
);
2541 reldata
->hdr
= rel_hdr
;
2543 amt
= sizeof ".rela" + strlen (asect
->name
);
2544 name
= (char *) bfd_alloc (abfd
, amt
);
2547 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2549 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2551 if (rel_hdr
->sh_name
== (unsigned int) -1)
2553 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2554 rel_hdr
->sh_entsize
= (use_rela_p
2555 ? bed
->s
->sizeof_rela
2556 : bed
->s
->sizeof_rel
);
2557 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2558 rel_hdr
->sh_flags
= 0;
2559 rel_hdr
->sh_addr
= 0;
2560 rel_hdr
->sh_size
= 0;
2561 rel_hdr
->sh_offset
= 0;
2566 /* Return the default section type based on the passed in section flags. */
2569 bfd_elf_get_default_section_type (flagword flags
)
2571 if ((flags
& SEC_ALLOC
) != 0
2572 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2574 return SHT_PROGBITS
;
2577 struct fake_section_arg
2579 struct bfd_link_info
*link_info
;
2583 /* Set up an ELF internal section header for a section. */
2586 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2588 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2589 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2590 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2591 Elf_Internal_Shdr
*this_hdr
;
2592 unsigned int sh_type
;
2596 /* We already failed; just get out of the bfd_map_over_sections
2601 this_hdr
= &esd
->this_hdr
;
2603 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2604 asect
->name
, FALSE
);
2605 if (this_hdr
->sh_name
== (unsigned int) -1)
2611 /* Don't clear sh_flags. Assembler may set additional bits. */
2613 if ((asect
->flags
& SEC_ALLOC
) != 0
2614 || asect
->user_set_vma
)
2615 this_hdr
->sh_addr
= asect
->vma
;
2617 this_hdr
->sh_addr
= 0;
2619 this_hdr
->sh_offset
= 0;
2620 this_hdr
->sh_size
= asect
->size
;
2621 this_hdr
->sh_link
= 0;
2622 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2623 /* The sh_entsize and sh_info fields may have been set already by
2624 copy_private_section_data. */
2626 this_hdr
->bfd_section
= asect
;
2627 this_hdr
->contents
= NULL
;
2629 /* If the section type is unspecified, we set it based on
2631 if ((asect
->flags
& SEC_GROUP
) != 0)
2632 sh_type
= SHT_GROUP
;
2634 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2636 if (this_hdr
->sh_type
== SHT_NULL
)
2637 this_hdr
->sh_type
= sh_type
;
2638 else if (this_hdr
->sh_type
== SHT_NOBITS
2639 && sh_type
== SHT_PROGBITS
2640 && (asect
->flags
& SEC_ALLOC
) != 0)
2642 /* Warn if we are changing a NOBITS section to PROGBITS, but
2643 allow the link to proceed. This can happen when users link
2644 non-bss input sections to bss output sections, or emit data
2645 to a bss output section via a linker script. */
2646 (*_bfd_error_handler
)
2647 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2648 this_hdr
->sh_type
= sh_type
;
2651 switch (this_hdr
->sh_type
)
2657 case SHT_INIT_ARRAY
:
2658 case SHT_FINI_ARRAY
:
2659 case SHT_PREINIT_ARRAY
:
2666 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2670 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2674 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2678 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2679 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2683 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2684 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2687 case SHT_GNU_versym
:
2688 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2691 case SHT_GNU_verdef
:
2692 this_hdr
->sh_entsize
= 0;
2693 /* objcopy or strip will copy over sh_info, but may not set
2694 cverdefs. The linker will set cverdefs, but sh_info will be
2696 if (this_hdr
->sh_info
== 0)
2697 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2699 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2700 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2703 case SHT_GNU_verneed
:
2704 this_hdr
->sh_entsize
= 0;
2705 /* objcopy or strip will copy over sh_info, but may not set
2706 cverrefs. The linker will set cverrefs, but sh_info will be
2708 if (this_hdr
->sh_info
== 0)
2709 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2711 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2712 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2716 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2720 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2724 if ((asect
->flags
& SEC_ALLOC
) != 0)
2725 this_hdr
->sh_flags
|= SHF_ALLOC
;
2726 if ((asect
->flags
& SEC_READONLY
) == 0)
2727 this_hdr
->sh_flags
|= SHF_WRITE
;
2728 if ((asect
->flags
& SEC_CODE
) != 0)
2729 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2730 if ((asect
->flags
& SEC_MERGE
) != 0)
2732 this_hdr
->sh_flags
|= SHF_MERGE
;
2733 this_hdr
->sh_entsize
= asect
->entsize
;
2734 if ((asect
->flags
& SEC_STRINGS
) != 0)
2735 this_hdr
->sh_flags
|= SHF_STRINGS
;
2737 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2738 this_hdr
->sh_flags
|= SHF_GROUP
;
2739 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2741 this_hdr
->sh_flags
|= SHF_TLS
;
2742 if (asect
->size
== 0
2743 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2745 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2747 this_hdr
->sh_size
= 0;
2750 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2751 if (this_hdr
->sh_size
!= 0)
2752 this_hdr
->sh_type
= SHT_NOBITS
;
2756 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2757 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2759 /* If the section has relocs, set up a section header for the
2760 SHT_REL[A] section. If two relocation sections are required for
2761 this section, it is up to the processor-specific back-end to
2762 create the other. */
2763 if ((asect
->flags
& SEC_RELOC
) != 0)
2765 /* When doing a relocatable link, create both REL and RELA sections if
2768 /* Do the normal setup if we wouldn't create any sections here. */
2769 && esd
->rel
.count
+ esd
->rela
.count
> 0
2770 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2772 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2773 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2778 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2779 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2785 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2787 ? &esd
->rela
: &esd
->rel
),
2793 /* Check for processor-specific section types. */
2794 sh_type
= this_hdr
->sh_type
;
2795 if (bed
->elf_backend_fake_sections
2796 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2799 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2801 /* Don't change the header type from NOBITS if we are being
2802 called for objcopy --only-keep-debug. */
2803 this_hdr
->sh_type
= sh_type
;
2807 /* Fill in the contents of a SHT_GROUP section. Called from
2808 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2809 when ELF targets use the generic linker, ld. Called for ld -r
2810 from bfd_elf_final_link. */
2813 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2815 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2816 asection
*elt
, *first
;
2820 /* Ignore linker created group section. See elfNN_ia64_object_p in
2822 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2826 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2828 unsigned long symindx
= 0;
2830 /* elf_group_id will have been set up by objcopy and the
2832 if (elf_group_id (sec
) != NULL
)
2833 symindx
= elf_group_id (sec
)->udata
.i
;
2837 /* If called from the assembler, swap_out_syms will have set up
2838 elf_section_syms. */
2839 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2840 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2842 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2844 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2846 /* The ELF backend linker sets sh_info to -2 when the group
2847 signature symbol is global, and thus the index can't be
2848 set until all local symbols are output. */
2849 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2850 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2851 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2852 unsigned long extsymoff
= 0;
2853 struct elf_link_hash_entry
*h
;
2855 if (!elf_bad_symtab (igroup
->owner
))
2857 Elf_Internal_Shdr
*symtab_hdr
;
2859 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2860 extsymoff
= symtab_hdr
->sh_info
;
2862 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2863 while (h
->root
.type
== bfd_link_hash_indirect
2864 || h
->root
.type
== bfd_link_hash_warning
)
2865 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2867 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2870 /* The contents won't be allocated for "ld -r" or objcopy. */
2872 if (sec
->contents
== NULL
)
2875 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2877 /* Arrange for the section to be written out. */
2878 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2879 if (sec
->contents
== NULL
)
2886 loc
= sec
->contents
+ sec
->size
;
2888 /* Get the pointer to the first section in the group that gas
2889 squirreled away here. objcopy arranges for this to be set to the
2890 start of the input section group. */
2891 first
= elt
= elf_next_in_group (sec
);
2893 /* First element is a flag word. Rest of section is elf section
2894 indices for all the sections of the group. Write them backwards
2895 just to keep the group in the same order as given in .section
2896 directives, not that it matters. */
2903 s
= s
->output_section
;
2905 && !bfd_is_abs_section (s
))
2907 unsigned int idx
= elf_section_data (s
)->this_idx
;
2910 H_PUT_32 (abfd
, idx
, loc
);
2912 elt
= elf_next_in_group (elt
);
2917 if ((loc
-= 4) != sec
->contents
)
2920 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2923 /* Assign all ELF section numbers. The dummy first section is handled here
2924 too. The link/info pointers for the standard section types are filled
2925 in here too, while we're at it. */
2928 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2930 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2932 unsigned int section_number
, secn
;
2933 Elf_Internal_Shdr
**i_shdrp
;
2934 struct bfd_elf_section_data
*d
;
2935 bfd_boolean need_symtab
;
2939 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2941 /* SHT_GROUP sections are in relocatable files only. */
2942 if (link_info
== NULL
|| link_info
->relocatable
)
2944 /* Put SHT_GROUP sections first. */
2945 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2947 d
= elf_section_data (sec
);
2949 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2951 if (sec
->flags
& SEC_LINKER_CREATED
)
2953 /* Remove the linker created SHT_GROUP sections. */
2954 bfd_section_list_remove (abfd
, sec
);
2955 abfd
->section_count
--;
2958 d
->this_idx
= section_number
++;
2963 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2965 d
= elf_section_data (sec
);
2967 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2968 d
->this_idx
= section_number
++;
2969 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2972 d
->rel
.idx
= section_number
++;
2973 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
2980 d
->rela
.idx
= section_number
++;
2981 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
2987 elf_shstrtab_sec (abfd
) = section_number
++;
2988 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2989 elf_elfheader (abfd
)->e_shstrndx
= elf_shstrtab_sec (abfd
);
2991 need_symtab
= (bfd_get_symcount (abfd
) > 0
2992 || (link_info
== NULL
2993 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
2997 elf_onesymtab (abfd
) = section_number
++;
2998 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2999 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
3001 elf_symtab_shndx (abfd
) = section_number
++;
3002 t
->symtab_shndx_hdr
.sh_name
3003 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
3004 ".symtab_shndx", FALSE
);
3005 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
3008 elf_strtab_sec (abfd
) = section_number
++;
3009 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3012 if (section_number
>= SHN_LORESERVE
)
3014 _bfd_error_handler (_("%B: too many sections: %u"),
3015 abfd
, section_number
);
3019 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
3020 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3022 elf_numsections (abfd
) = section_number
;
3023 elf_elfheader (abfd
)->e_shnum
= section_number
;
3025 /* Set up the list of section header pointers, in agreement with the
3027 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3028 sizeof (Elf_Internal_Shdr
*));
3029 if (i_shdrp
== NULL
)
3032 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3033 sizeof (Elf_Internal_Shdr
));
3034 if (i_shdrp
[0] == NULL
)
3036 bfd_release (abfd
, i_shdrp
);
3040 elf_elfsections (abfd
) = i_shdrp
;
3042 i_shdrp
[elf_shstrtab_sec (abfd
)] = &t
->shstrtab_hdr
;
3045 i_shdrp
[elf_onesymtab (abfd
)] = &t
->symtab_hdr
;
3046 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3048 i_shdrp
[elf_symtab_shndx (abfd
)] = &t
->symtab_shndx_hdr
;
3049 t
->symtab_shndx_hdr
.sh_link
= elf_onesymtab (abfd
);
3051 i_shdrp
[elf_strtab_sec (abfd
)] = &t
->strtab_hdr
;
3052 t
->symtab_hdr
.sh_link
= elf_strtab_sec (abfd
);
3055 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3060 d
= elf_section_data (sec
);
3062 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3063 if (d
->rel
.idx
!= 0)
3064 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3065 if (d
->rela
.idx
!= 0)
3066 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3068 /* Fill in the sh_link and sh_info fields while we're at it. */
3070 /* sh_link of a reloc section is the section index of the symbol
3071 table. sh_info is the section index of the section to which
3072 the relocation entries apply. */
3073 if (d
->rel
.idx
!= 0)
3075 d
->rel
.hdr
->sh_link
= elf_onesymtab (abfd
);
3076 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3078 if (d
->rela
.idx
!= 0)
3080 d
->rela
.hdr
->sh_link
= elf_onesymtab (abfd
);
3081 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3084 /* We need to set up sh_link for SHF_LINK_ORDER. */
3085 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3087 s
= elf_linked_to_section (sec
);
3090 /* elf_linked_to_section points to the input section. */
3091 if (link_info
!= NULL
)
3093 /* Check discarded linkonce section. */
3094 if (discarded_section (s
))
3097 (*_bfd_error_handler
)
3098 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3099 abfd
, d
->this_hdr
.bfd_section
,
3101 /* Point to the kept section if it has the same
3102 size as the discarded one. */
3103 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3106 bfd_set_error (bfd_error_bad_value
);
3112 s
= s
->output_section
;
3113 BFD_ASSERT (s
!= NULL
);
3117 /* Handle objcopy. */
3118 if (s
->output_section
== NULL
)
3120 (*_bfd_error_handler
)
3121 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3122 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3123 bfd_set_error (bfd_error_bad_value
);
3126 s
= s
->output_section
;
3128 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3133 The Intel C compiler generates SHT_IA_64_UNWIND with
3134 SHF_LINK_ORDER. But it doesn't set the sh_link or
3135 sh_info fields. Hence we could get the situation
3137 const struct elf_backend_data
*bed
3138 = get_elf_backend_data (abfd
);
3139 if (bed
->link_order_error_handler
)
3140 bed
->link_order_error_handler
3141 (_("%B: warning: sh_link not set for section `%A'"),
3146 switch (d
->this_hdr
.sh_type
)
3150 /* A reloc section which we are treating as a normal BFD
3151 section. sh_link is the section index of the symbol
3152 table. sh_info is the section index of the section to
3153 which the relocation entries apply. We assume that an
3154 allocated reloc section uses the dynamic symbol table.
3155 FIXME: How can we be sure? */
3156 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3158 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3160 /* We look up the section the relocs apply to by name. */
3162 if (d
->this_hdr
.sh_type
== SHT_REL
)
3166 s
= bfd_get_section_by_name (abfd
, name
);
3168 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3172 /* We assume that a section named .stab*str is a stabs
3173 string section. We look for a section with the same name
3174 but without the trailing ``str'', and set its sh_link
3175 field to point to this section. */
3176 if (CONST_STRNEQ (sec
->name
, ".stab")
3177 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3182 len
= strlen (sec
->name
);
3183 alc
= (char *) bfd_malloc (len
- 2);
3186 memcpy (alc
, sec
->name
, len
- 3);
3187 alc
[len
- 3] = '\0';
3188 s
= bfd_get_section_by_name (abfd
, alc
);
3192 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3194 /* This is a .stab section. */
3195 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3196 elf_section_data (s
)->this_hdr
.sh_entsize
3197 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3204 case SHT_GNU_verneed
:
3205 case SHT_GNU_verdef
:
3206 /* sh_link is the section header index of the string table
3207 used for the dynamic entries, or the symbol table, or the
3209 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3211 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3214 case SHT_GNU_LIBLIST
:
3215 /* sh_link is the section header index of the prelink library
3216 list used for the dynamic entries, or the symbol table, or
3217 the version strings. */
3218 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3219 ? ".dynstr" : ".gnu.libstr");
3221 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3226 case SHT_GNU_versym
:
3227 /* sh_link is the section header index of the symbol table
3228 this hash table or version table is for. */
3229 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3231 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3235 d
->this_hdr
.sh_link
= elf_onesymtab (abfd
);
3239 for (secn
= 1; secn
< section_number
; ++secn
)
3240 if (i_shdrp
[secn
] == NULL
)
3241 i_shdrp
[secn
] = i_shdrp
[0];
3243 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3244 i_shdrp
[secn
]->sh_name
);
3249 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3251 /* If the backend has a special mapping, use it. */
3252 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3253 if (bed
->elf_backend_sym_is_global
)
3254 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3256 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3257 || bfd_is_und_section (bfd_get_section (sym
))
3258 || bfd_is_com_section (bfd_get_section (sym
)));
3261 /* Don't output section symbols for sections that are not going to be
3262 output, that are duplicates or there is no BFD section. */
3265 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3267 elf_symbol_type
*type_ptr
;
3269 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3272 type_ptr
= elf_symbol_from (abfd
, sym
);
3273 return ((type_ptr
!= NULL
3274 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3275 && bfd_is_abs_section (sym
->section
))
3276 || !(sym
->section
->owner
== abfd
3277 || (sym
->section
->output_section
->owner
== abfd
3278 && sym
->section
->output_offset
== 0)
3279 || bfd_is_abs_section (sym
->section
)));
3282 /* Map symbol from it's internal number to the external number, moving
3283 all local symbols to be at the head of the list. */
3286 elf_map_symbols (bfd
*abfd
, unsigned int *pnum_locals
)
3288 unsigned int symcount
= bfd_get_symcount (abfd
);
3289 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3290 asymbol
**sect_syms
;
3291 unsigned int num_locals
= 0;
3292 unsigned int num_globals
= 0;
3293 unsigned int num_locals2
= 0;
3294 unsigned int num_globals2
= 0;
3301 fprintf (stderr
, "elf_map_symbols\n");
3305 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3307 if (max_index
< asect
->index
)
3308 max_index
= asect
->index
;
3312 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3313 if (sect_syms
== NULL
)
3315 elf_section_syms (abfd
) = sect_syms
;
3316 elf_num_section_syms (abfd
) = max_index
;
3318 /* Init sect_syms entries for any section symbols we have already
3319 decided to output. */
3320 for (idx
= 0; idx
< symcount
; idx
++)
3322 asymbol
*sym
= syms
[idx
];
3324 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3326 && !ignore_section_sym (abfd
, sym
)
3327 && !bfd_is_abs_section (sym
->section
))
3329 asection
*sec
= sym
->section
;
3331 if (sec
->owner
!= abfd
)
3332 sec
= sec
->output_section
;
3334 sect_syms
[sec
->index
] = syms
[idx
];
3338 /* Classify all of the symbols. */
3339 for (idx
= 0; idx
< symcount
; idx
++)
3341 if (sym_is_global (abfd
, syms
[idx
]))
3343 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3347 /* We will be adding a section symbol for each normal BFD section. Most
3348 sections will already have a section symbol in outsymbols, but
3349 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3350 at least in that case. */
3351 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3353 if (sect_syms
[asect
->index
] == NULL
)
3355 if (!sym_is_global (abfd
, asect
->symbol
))
3362 /* Now sort the symbols so the local symbols are first. */
3363 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3364 sizeof (asymbol
*));
3366 if (new_syms
== NULL
)
3369 for (idx
= 0; idx
< symcount
; idx
++)
3371 asymbol
*sym
= syms
[idx
];
3374 if (sym_is_global (abfd
, sym
))
3375 i
= num_locals
+ num_globals2
++;
3376 else if (!ignore_section_sym (abfd
, sym
))
3381 sym
->udata
.i
= i
+ 1;
3383 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3385 if (sect_syms
[asect
->index
] == NULL
)
3387 asymbol
*sym
= asect
->symbol
;
3390 sect_syms
[asect
->index
] = sym
;
3391 if (!sym_is_global (abfd
, sym
))
3394 i
= num_locals
+ num_globals2
++;
3396 sym
->udata
.i
= i
+ 1;
3400 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3402 *pnum_locals
= num_locals
;
3406 /* Align to the maximum file alignment that could be required for any
3407 ELF data structure. */
3409 static inline file_ptr
3410 align_file_position (file_ptr off
, int align
)
3412 return (off
+ align
- 1) & ~(align
- 1);
3415 /* Assign a file position to a section, optionally aligning to the
3416 required section alignment. */
3419 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3423 if (align
&& i_shdrp
->sh_addralign
> 1)
3424 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3425 i_shdrp
->sh_offset
= offset
;
3426 if (i_shdrp
->bfd_section
!= NULL
)
3427 i_shdrp
->bfd_section
->filepos
= offset
;
3428 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3429 offset
+= i_shdrp
->sh_size
;
3433 /* Compute the file positions we are going to put the sections at, and
3434 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3435 is not NULL, this is being called by the ELF backend linker. */
3438 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3439 struct bfd_link_info
*link_info
)
3441 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3442 struct fake_section_arg fsargs
;
3444 struct bfd_strtab_hash
*strtab
= NULL
;
3445 Elf_Internal_Shdr
*shstrtab_hdr
;
3446 bfd_boolean need_symtab
;
3448 if (abfd
->output_has_begun
)
3451 /* Do any elf backend specific processing first. */
3452 if (bed
->elf_backend_begin_write_processing
)
3453 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3455 if (! prep_headers (abfd
))
3458 /* Post process the headers if necessary. */
3459 if (bed
->elf_backend_post_process_headers
)
3460 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3462 fsargs
.failed
= FALSE
;
3463 fsargs
.link_info
= link_info
;
3464 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3468 if (!assign_section_numbers (abfd
, link_info
))
3471 /* The backend linker builds symbol table information itself. */
3472 need_symtab
= (link_info
== NULL
3473 && (bfd_get_symcount (abfd
) > 0
3474 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3478 /* Non-zero if doing a relocatable link. */
3479 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3481 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3486 if (link_info
== NULL
)
3488 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3493 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3494 /* sh_name was set in prep_headers. */
3495 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3496 shstrtab_hdr
->sh_flags
= 0;
3497 shstrtab_hdr
->sh_addr
= 0;
3498 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3499 shstrtab_hdr
->sh_entsize
= 0;
3500 shstrtab_hdr
->sh_link
= 0;
3501 shstrtab_hdr
->sh_info
= 0;
3502 /* sh_offset is set in assign_file_positions_except_relocs. */
3503 shstrtab_hdr
->sh_addralign
= 1;
3505 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3511 Elf_Internal_Shdr
*hdr
;
3513 off
= elf_next_file_pos (abfd
);
3515 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3516 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3518 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3519 if (hdr
->sh_size
!= 0)
3520 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3522 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3523 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3525 elf_next_file_pos (abfd
) = off
;
3527 /* Now that we know where the .strtab section goes, write it
3529 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3530 || ! _bfd_stringtab_emit (abfd
, strtab
))
3532 _bfd_stringtab_free (strtab
);
3535 abfd
->output_has_begun
= TRUE
;
3540 /* Make an initial estimate of the size of the program header. If we
3541 get the number wrong here, we'll redo section placement. */
3543 static bfd_size_type
3544 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3548 const struct elf_backend_data
*bed
;
3550 /* Assume we will need exactly two PT_LOAD segments: one for text
3551 and one for data. */
3554 s
= bfd_get_section_by_name (abfd
, ".interp");
3555 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3557 /* If we have a loadable interpreter section, we need a
3558 PT_INTERP segment. In this case, assume we also need a
3559 PT_PHDR segment, although that may not be true for all
3564 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3566 /* We need a PT_DYNAMIC segment. */
3570 if (info
!= NULL
&& info
->relro
)
3572 /* We need a PT_GNU_RELRO segment. */
3576 if (elf_eh_frame_hdr (abfd
))
3578 /* We need a PT_GNU_EH_FRAME segment. */
3582 if (elf_stack_flags (abfd
))
3584 /* We need a PT_GNU_STACK segment. */
3588 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3590 if ((s
->flags
& SEC_LOAD
) != 0
3591 && CONST_STRNEQ (s
->name
, ".note"))
3593 /* We need a PT_NOTE segment. */
3595 /* Try to create just one PT_NOTE segment
3596 for all adjacent loadable .note* sections.
3597 gABI requires that within a PT_NOTE segment
3598 (and also inside of each SHT_NOTE section)
3599 each note is padded to a multiple of 4 size,
3600 so we check whether the sections are correctly
3602 if (s
->alignment_power
== 2)
3603 while (s
->next
!= NULL
3604 && s
->next
->alignment_power
== 2
3605 && (s
->next
->flags
& SEC_LOAD
) != 0
3606 && CONST_STRNEQ (s
->next
->name
, ".note"))
3611 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3613 if (s
->flags
& SEC_THREAD_LOCAL
)
3615 /* We need a PT_TLS segment. */
3621 /* Let the backend count up any program headers it might need. */
3622 bed
= get_elf_backend_data (abfd
);
3623 if (bed
->elf_backend_additional_program_headers
)
3627 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3633 return segs
* bed
->s
->sizeof_phdr
;
3636 /* Find the segment that contains the output_section of section. */
3639 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3641 struct elf_segment_map
*m
;
3642 Elf_Internal_Phdr
*p
;
3644 for (m
= elf_seg_map (abfd
), p
= elf_tdata (abfd
)->phdr
;
3650 for (i
= m
->count
- 1; i
>= 0; i
--)
3651 if (m
->sections
[i
] == section
)
3658 /* Create a mapping from a set of sections to a program segment. */
3660 static struct elf_segment_map
*
3661 make_mapping (bfd
*abfd
,
3662 asection
**sections
,
3667 struct elf_segment_map
*m
;
3672 amt
= sizeof (struct elf_segment_map
);
3673 amt
+= (to
- from
- 1) * sizeof (asection
*);
3674 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3678 m
->p_type
= PT_LOAD
;
3679 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3680 m
->sections
[i
- from
] = *hdrpp
;
3681 m
->count
= to
- from
;
3683 if (from
== 0 && phdr
)
3685 /* Include the headers in the first PT_LOAD segment. */
3686 m
->includes_filehdr
= 1;
3687 m
->includes_phdrs
= 1;
3693 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3696 struct elf_segment_map
*
3697 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3699 struct elf_segment_map
*m
;
3701 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3702 sizeof (struct elf_segment_map
));
3706 m
->p_type
= PT_DYNAMIC
;
3708 m
->sections
[0] = dynsec
;
3713 /* Possibly add or remove segments from the segment map. */
3716 elf_modify_segment_map (bfd
*abfd
,
3717 struct bfd_link_info
*info
,
3718 bfd_boolean remove_empty_load
)
3720 struct elf_segment_map
**m
;
3721 const struct elf_backend_data
*bed
;
3723 /* The placement algorithm assumes that non allocated sections are
3724 not in PT_LOAD segments. We ensure this here by removing such
3725 sections from the segment map. We also remove excluded
3726 sections. Finally, any PT_LOAD segment without sections is
3728 m
= &elf_seg_map (abfd
);
3731 unsigned int i
, new_count
;
3733 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3735 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3736 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3737 || (*m
)->p_type
!= PT_LOAD
))
3739 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3743 (*m
)->count
= new_count
;
3745 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3751 bed
= get_elf_backend_data (abfd
);
3752 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3754 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3761 /* Set up a mapping from BFD sections to program segments. */
3764 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3767 struct elf_segment_map
*m
;
3768 asection
**sections
= NULL
;
3769 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3770 bfd_boolean no_user_phdrs
;
3772 no_user_phdrs
= elf_seg_map (abfd
) == NULL
;
3775 info
->user_phdrs
= !no_user_phdrs
;
3777 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3781 struct elf_segment_map
*mfirst
;
3782 struct elf_segment_map
**pm
;
3785 unsigned int phdr_index
;
3786 bfd_vma maxpagesize
;
3788 bfd_boolean phdr_in_segment
= TRUE
;
3789 bfd_boolean writable
;
3791 asection
*first_tls
= NULL
;
3792 asection
*dynsec
, *eh_frame_hdr
;
3794 bfd_vma addr_mask
, wrap_to
= 0;
3796 /* Select the allocated sections, and sort them. */
3798 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3799 sizeof (asection
*));
3800 if (sections
== NULL
)
3803 /* Calculate top address, avoiding undefined behaviour of shift
3804 left operator when shift count is equal to size of type
3806 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3807 addr_mask
= (addr_mask
<< 1) + 1;
3810 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3812 if ((s
->flags
& SEC_ALLOC
) != 0)
3816 /* A wrapping section potentially clashes with header. */
3817 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3818 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3821 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3824 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3826 /* Build the mapping. */
3831 /* If we have a .interp section, then create a PT_PHDR segment for
3832 the program headers and a PT_INTERP segment for the .interp
3834 s
= bfd_get_section_by_name (abfd
, ".interp");
3835 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3837 amt
= sizeof (struct elf_segment_map
);
3838 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3842 m
->p_type
= PT_PHDR
;
3843 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3844 m
->p_flags
= PF_R
| PF_X
;
3845 m
->p_flags_valid
= 1;
3846 m
->includes_phdrs
= 1;
3851 amt
= sizeof (struct elf_segment_map
);
3852 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3856 m
->p_type
= PT_INTERP
;
3864 /* Look through the sections. We put sections in the same program
3865 segment when the start of the second section can be placed within
3866 a few bytes of the end of the first section. */
3870 maxpagesize
= bed
->maxpagesize
;
3872 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3874 && (dynsec
->flags
& SEC_LOAD
) == 0)
3877 /* Deal with -Ttext or something similar such that the first section
3878 is not adjacent to the program headers. This is an
3879 approximation, since at this point we don't know exactly how many
3880 program headers we will need. */
3883 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
3885 if (phdr_size
== (bfd_size_type
) -1)
3886 phdr_size
= get_program_header_size (abfd
, info
);
3887 phdr_size
+= bed
->s
->sizeof_ehdr
;
3888 if ((abfd
->flags
& D_PAGED
) == 0
3889 || (sections
[0]->lma
& addr_mask
) < phdr_size
3890 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
3891 < phdr_size
% maxpagesize
)
3892 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
3893 phdr_in_segment
= FALSE
;
3896 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3899 bfd_boolean new_segment
;
3903 /* See if this section and the last one will fit in the same
3906 if (last_hdr
== NULL
)
3908 /* If we don't have a segment yet, then we don't need a new
3909 one (we build the last one after this loop). */
3910 new_segment
= FALSE
;
3912 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3914 /* If this section has a different relation between the
3915 virtual address and the load address, then we need a new
3919 else if (hdr
->lma
< last_hdr
->lma
+ last_size
3920 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
3922 /* If this section has a load address that makes it overlap
3923 the previous section, then we need a new segment. */
3926 /* In the next test we have to be careful when last_hdr->lma is close
3927 to the end of the address space. If the aligned address wraps
3928 around to the start of the address space, then there are no more
3929 pages left in memory and it is OK to assume that the current
3930 section can be included in the current segment. */
3931 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3933 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3936 /* If putting this section in this segment would force us to
3937 skip a page in the segment, then we need a new segment. */
3940 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3941 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3943 /* We don't want to put a loadable section after a
3944 nonloadable section in the same segment.
3945 Consider .tbss sections as loadable for this purpose. */
3948 else if ((abfd
->flags
& D_PAGED
) == 0)
3950 /* If the file is not demand paged, which means that we
3951 don't require the sections to be correctly aligned in the
3952 file, then there is no other reason for a new segment. */
3953 new_segment
= FALSE
;
3956 && (hdr
->flags
& SEC_READONLY
) == 0
3957 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
3958 != (hdr
->lma
& -maxpagesize
)))
3960 /* We don't want to put a writable section in a read only
3961 segment, unless they are on the same page in memory
3962 anyhow. We already know that the last section does not
3963 bring us past the current section on the page, so the
3964 only case in which the new section is not on the same
3965 page as the previous section is when the previous section
3966 ends precisely on a page boundary. */
3971 /* Otherwise, we can use the same segment. */
3972 new_segment
= FALSE
;
3975 /* Allow interested parties a chance to override our decision. */
3976 if (last_hdr
!= NULL
3978 && info
->callbacks
->override_segment_assignment
!= NULL
)
3980 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
3986 if ((hdr
->flags
& SEC_READONLY
) == 0)
3989 /* .tbss sections effectively have zero size. */
3990 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3991 != SEC_THREAD_LOCAL
)
3992 last_size
= hdr
->size
;
3998 /* We need a new program segment. We must create a new program
3999 header holding all the sections from phdr_index until hdr. */
4001 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4008 if ((hdr
->flags
& SEC_READONLY
) == 0)
4014 /* .tbss sections effectively have zero size. */
4015 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4016 last_size
= hdr
->size
;
4020 phdr_in_segment
= FALSE
;
4023 /* Create a final PT_LOAD program segment, but not if it's just
4025 if (last_hdr
!= NULL
4026 && (i
- phdr_index
!= 1
4027 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4028 != SEC_THREAD_LOCAL
)))
4030 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4038 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4041 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4048 /* For each batch of consecutive loadable .note sections,
4049 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4050 because if we link together nonloadable .note sections and
4051 loadable .note sections, we will generate two .note sections
4052 in the output file. FIXME: Using names for section types is
4054 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4056 if ((s
->flags
& SEC_LOAD
) != 0
4057 && CONST_STRNEQ (s
->name
, ".note"))
4062 amt
= sizeof (struct elf_segment_map
);
4063 if (s
->alignment_power
== 2)
4064 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4066 if (s2
->next
->alignment_power
== 2
4067 && (s2
->next
->flags
& SEC_LOAD
) != 0
4068 && CONST_STRNEQ (s2
->next
->name
, ".note")
4069 && align_power (s2
->lma
+ s2
->size
, 2)
4075 amt
+= (count
- 1) * sizeof (asection
*);
4076 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4080 m
->p_type
= PT_NOTE
;
4084 m
->sections
[m
->count
- count
--] = s
;
4085 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4088 m
->sections
[m
->count
- 1] = s
;
4089 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4093 if (s
->flags
& SEC_THREAD_LOCAL
)
4101 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4104 amt
= sizeof (struct elf_segment_map
);
4105 amt
+= (tls_count
- 1) * sizeof (asection
*);
4106 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4111 m
->count
= tls_count
;
4112 /* Mandated PF_R. */
4114 m
->p_flags_valid
= 1;
4115 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4117 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
4118 m
->sections
[i
] = first_tls
;
4119 first_tls
= first_tls
->next
;
4126 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4128 eh_frame_hdr
= elf_eh_frame_hdr (abfd
);
4129 if (eh_frame_hdr
!= NULL
4130 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4132 amt
= sizeof (struct elf_segment_map
);
4133 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4137 m
->p_type
= PT_GNU_EH_FRAME
;
4139 m
->sections
[0] = eh_frame_hdr
->output_section
;
4145 if (elf_stack_flags (abfd
))
4147 amt
= sizeof (struct elf_segment_map
);
4148 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4152 m
->p_type
= PT_GNU_STACK
;
4153 m
->p_flags
= elf_stack_flags (abfd
);
4154 m
->p_align
= bed
->stack_align
;
4155 m
->p_flags_valid
= 1;
4156 m
->p_align_valid
= m
->p_align
!= 0;
4157 if (info
->stacksize
> 0)
4159 m
->p_size
= info
->stacksize
;
4160 m
->p_size_valid
= 1;
4167 if (info
!= NULL
&& info
->relro
)
4169 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4171 if (m
->p_type
== PT_LOAD
4173 && m
->sections
[0]->vma
>= info
->relro_start
4174 && m
->sections
[0]->vma
< info
->relro_end
)
4177 while (--i
!= (unsigned) -1)
4178 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4179 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4182 if (i
== (unsigned) -1)
4185 if (m
->sections
[i
]->vma
+ m
->sections
[i
]->size
4191 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4194 amt
= sizeof (struct elf_segment_map
);
4195 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4199 m
->p_type
= PT_GNU_RELRO
;
4201 m
->p_flags_valid
= 1;
4209 elf_seg_map (abfd
) = mfirst
;
4212 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4215 for (count
= 0, m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4217 elf_program_header_size (abfd
) = count
* bed
->s
->sizeof_phdr
;
4222 if (sections
!= NULL
)
4227 /* Sort sections by address. */
4230 elf_sort_sections (const void *arg1
, const void *arg2
)
4232 const asection
*sec1
= *(const asection
**) arg1
;
4233 const asection
*sec2
= *(const asection
**) arg2
;
4234 bfd_size_type size1
, size2
;
4236 /* Sort by LMA first, since this is the address used to
4237 place the section into a segment. */
4238 if (sec1
->lma
< sec2
->lma
)
4240 else if (sec1
->lma
> sec2
->lma
)
4243 /* Then sort by VMA. Normally the LMA and the VMA will be
4244 the same, and this will do nothing. */
4245 if (sec1
->vma
< sec2
->vma
)
4247 else if (sec1
->vma
> sec2
->vma
)
4250 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4252 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4258 /* If the indicies are the same, do not return 0
4259 here, but continue to try the next comparison. */
4260 if (sec1
->target_index
- sec2
->target_index
!= 0)
4261 return sec1
->target_index
- sec2
->target_index
;
4266 else if (TOEND (sec2
))
4271 /* Sort by size, to put zero sized sections
4272 before others at the same address. */
4274 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4275 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4282 return sec1
->target_index
- sec2
->target_index
;
4285 /* Ian Lance Taylor writes:
4287 We shouldn't be using % with a negative signed number. That's just
4288 not good. We have to make sure either that the number is not
4289 negative, or that the number has an unsigned type. When the types
4290 are all the same size they wind up as unsigned. When file_ptr is a
4291 larger signed type, the arithmetic winds up as signed long long,
4294 What we're trying to say here is something like ``increase OFF by
4295 the least amount that will cause it to be equal to the VMA modulo
4297 /* In other words, something like:
4299 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4300 off_offset = off % bed->maxpagesize;
4301 if (vma_offset < off_offset)
4302 adjustment = vma_offset + bed->maxpagesize - off_offset;
4304 adjustment = vma_offset - off_offset;
4306 which can can be collapsed into the expression below. */
4309 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4311 return ((vma
- off
) % maxpagesize
);
4315 print_segment_map (const struct elf_segment_map
*m
)
4318 const char *pt
= get_segment_type (m
->p_type
);
4323 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4324 sprintf (buf
, "LOPROC+%7.7x",
4325 (unsigned int) (m
->p_type
- PT_LOPROC
));
4326 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4327 sprintf (buf
, "LOOS+%7.7x",
4328 (unsigned int) (m
->p_type
- PT_LOOS
));
4330 snprintf (buf
, sizeof (buf
), "%8.8x",
4331 (unsigned int) m
->p_type
);
4335 fprintf (stderr
, "%s:", pt
);
4336 for (j
= 0; j
< m
->count
; j
++)
4337 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4343 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4348 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4350 buf
= bfd_zmalloc (len
);
4353 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4358 /* Assign file positions to the sections based on the mapping from
4359 sections to segments. This function also sets up some fields in
4363 assign_file_positions_for_load_sections (bfd
*abfd
,
4364 struct bfd_link_info
*link_info
)
4366 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4367 struct elf_segment_map
*m
;
4368 Elf_Internal_Phdr
*phdrs
;
4369 Elf_Internal_Phdr
*p
;
4371 bfd_size_type maxpagesize
;
4374 bfd_vma header_pad
= 0;
4376 if (link_info
== NULL
4377 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4381 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4385 header_pad
= m
->header_size
;
4390 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4391 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4395 /* PR binutils/12467. */
4396 elf_elfheader (abfd
)->e_phoff
= 0;
4397 elf_elfheader (abfd
)->e_phentsize
= 0;
4400 elf_elfheader (abfd
)->e_phnum
= alloc
;
4402 if (elf_program_header_size (abfd
) == (bfd_size_type
) -1)
4403 elf_program_header_size (abfd
) = alloc
* bed
->s
->sizeof_phdr
;
4405 BFD_ASSERT (elf_program_header_size (abfd
)
4406 >= alloc
* bed
->s
->sizeof_phdr
);
4410 elf_next_file_pos (abfd
) = bed
->s
->sizeof_ehdr
;
4414 /* We're writing the size in elf_program_header_size (abfd),
4415 see assign_file_positions_except_relocs, so make sure we have
4416 that amount allocated, with trailing space cleared.
4417 The variable alloc contains the computed need, while
4418 elf_program_header_size (abfd) contains the size used for the
4420 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4421 where the layout is forced to according to a larger size in the
4422 last iterations for the testcase ld-elf/header. */
4423 BFD_ASSERT (elf_program_header_size (abfd
) % bed
->s
->sizeof_phdr
4425 phdrs
= (Elf_Internal_Phdr
*)
4427 (elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
),
4428 sizeof (Elf_Internal_Phdr
));
4429 elf_tdata (abfd
)->phdr
= phdrs
;
4434 if ((abfd
->flags
& D_PAGED
) != 0)
4435 maxpagesize
= bed
->maxpagesize
;
4437 off
= bed
->s
->sizeof_ehdr
;
4438 off
+= alloc
* bed
->s
->sizeof_phdr
;
4439 if (header_pad
< (bfd_vma
) off
)
4445 for (m
= elf_seg_map (abfd
), p
= phdrs
, j
= 0;
4447 m
= m
->next
, p
++, j
++)
4451 bfd_boolean no_contents
;
4453 /* If elf_segment_map is not from map_sections_to_segments, the
4454 sections may not be correctly ordered. NOTE: sorting should
4455 not be done to the PT_NOTE section of a corefile, which may
4456 contain several pseudo-sections artificially created by bfd.
4457 Sorting these pseudo-sections breaks things badly. */
4459 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4460 && m
->p_type
== PT_NOTE
))
4461 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4464 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4465 number of sections with contents contributing to both p_filesz
4466 and p_memsz, followed by a number of sections with no contents
4467 that just contribute to p_memsz. In this loop, OFF tracks next
4468 available file offset for PT_LOAD and PT_NOTE segments. */
4469 p
->p_type
= m
->p_type
;
4470 p
->p_flags
= m
->p_flags
;
4475 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4477 if (m
->p_paddr_valid
)
4478 p
->p_paddr
= m
->p_paddr
;
4479 else if (m
->count
== 0)
4482 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4484 if (p
->p_type
== PT_LOAD
4485 && (abfd
->flags
& D_PAGED
) != 0)
4487 /* p_align in demand paged PT_LOAD segments effectively stores
4488 the maximum page size. When copying an executable with
4489 objcopy, we set m->p_align from the input file. Use this
4490 value for maxpagesize rather than bed->maxpagesize, which
4491 may be different. Note that we use maxpagesize for PT_TLS
4492 segment alignment later in this function, so we are relying
4493 on at least one PT_LOAD segment appearing before a PT_TLS
4495 if (m
->p_align_valid
)
4496 maxpagesize
= m
->p_align
;
4498 p
->p_align
= maxpagesize
;
4500 else if (m
->p_align_valid
)
4501 p
->p_align
= m
->p_align
;
4502 else if (m
->count
== 0)
4503 p
->p_align
= 1 << bed
->s
->log_file_align
;
4507 no_contents
= FALSE
;
4509 if (p
->p_type
== PT_LOAD
4512 bfd_size_type align
;
4513 unsigned int align_power
= 0;
4515 if (m
->p_align_valid
)
4519 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4521 unsigned int secalign
;
4523 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4524 if (secalign
> align_power
)
4525 align_power
= secalign
;
4527 align
= (bfd_size_type
) 1 << align_power
;
4528 if (align
< maxpagesize
)
4529 align
= maxpagesize
;
4532 for (i
= 0; i
< m
->count
; i
++)
4533 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4534 /* If we aren't making room for this section, then
4535 it must be SHT_NOBITS regardless of what we've
4536 set via struct bfd_elf_special_section. */
4537 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4539 /* Find out whether this segment contains any loadable
4542 for (i
= 0; i
< m
->count
; i
++)
4543 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4545 no_contents
= FALSE
;
4549 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4553 /* We shouldn't need to align the segment on disk since
4554 the segment doesn't need file space, but the gABI
4555 arguably requires the alignment and glibc ld.so
4556 checks it. So to comply with the alignment
4557 requirement but not waste file space, we adjust
4558 p_offset for just this segment. (OFF_ADJUST is
4559 subtracted from OFF later.) This may put p_offset
4560 past the end of file, but that shouldn't matter. */
4565 /* Make sure the .dynamic section is the first section in the
4566 PT_DYNAMIC segment. */
4567 else if (p
->p_type
== PT_DYNAMIC
4569 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4572 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4574 bfd_set_error (bfd_error_bad_value
);
4577 /* Set the note section type to SHT_NOTE. */
4578 else if (p
->p_type
== PT_NOTE
)
4579 for (i
= 0; i
< m
->count
; i
++)
4580 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4586 if (m
->includes_filehdr
)
4588 if (!m
->p_flags_valid
)
4590 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4591 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4594 if (p
->p_vaddr
< (bfd_vma
) off
)
4596 (*_bfd_error_handler
)
4597 (_("%B: Not enough room for program headers, try linking with -N"),
4599 bfd_set_error (bfd_error_bad_value
);
4604 if (!m
->p_paddr_valid
)
4609 if (m
->includes_phdrs
)
4611 if (!m
->p_flags_valid
)
4614 if (!m
->includes_filehdr
)
4616 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4620 p
->p_vaddr
-= off
- p
->p_offset
;
4621 if (!m
->p_paddr_valid
)
4622 p
->p_paddr
-= off
- p
->p_offset
;
4626 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4627 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4630 p
->p_filesz
+= header_pad
;
4631 p
->p_memsz
+= header_pad
;
4635 if (p
->p_type
== PT_LOAD
4636 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4638 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4644 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4646 p
->p_filesz
+= adjust
;
4647 p
->p_memsz
+= adjust
;
4651 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4652 maps. Set filepos for sections in PT_LOAD segments, and in
4653 core files, for sections in PT_NOTE segments.
4654 assign_file_positions_for_non_load_sections will set filepos
4655 for other sections and update p_filesz for other segments. */
4656 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4659 bfd_size_type align
;
4660 Elf_Internal_Shdr
*this_hdr
;
4663 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4664 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4666 if ((p
->p_type
== PT_LOAD
4667 || p
->p_type
== PT_TLS
)
4668 && (this_hdr
->sh_type
!= SHT_NOBITS
4669 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4670 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4671 || p
->p_type
== PT_TLS
))))
4673 bfd_vma p_start
= p
->p_paddr
;
4674 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4675 bfd_vma s_start
= sec
->lma
;
4676 bfd_vma adjust
= s_start
- p_end
;
4680 || p_end
< p_start
))
4682 (*_bfd_error_handler
)
4683 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4684 (unsigned long) s_start
, (unsigned long) p_end
);
4688 p
->p_memsz
+= adjust
;
4690 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4692 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4694 /* We have a PROGBITS section following NOBITS ones.
4695 Allocate file space for the NOBITS section(s) and
4697 adjust
= p
->p_memsz
- p
->p_filesz
;
4698 if (!write_zeros (abfd
, off
, adjust
))
4702 p
->p_filesz
+= adjust
;
4706 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4708 /* The section at i == 0 is the one that actually contains
4712 this_hdr
->sh_offset
= sec
->filepos
= off
;
4713 off
+= this_hdr
->sh_size
;
4714 p
->p_filesz
= this_hdr
->sh_size
;
4720 /* The rest are fake sections that shouldn't be written. */
4729 if (p
->p_type
== PT_LOAD
)
4731 this_hdr
->sh_offset
= sec
->filepos
= off
;
4732 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4733 off
+= this_hdr
->sh_size
;
4735 else if (this_hdr
->sh_type
== SHT_NOBITS
4736 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
4737 && this_hdr
->sh_offset
== 0)
4739 /* This is a .tbss section that didn't get a PT_LOAD.
4740 (See _bfd_elf_map_sections_to_segments "Create a
4741 final PT_LOAD".) Set sh_offset to the value it
4742 would have if we had created a zero p_filesz and
4743 p_memsz PT_LOAD header for the section. This
4744 also makes the PT_TLS header have the same
4746 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
4748 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
4751 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4753 p
->p_filesz
+= this_hdr
->sh_size
;
4754 /* A load section without SHF_ALLOC is something like
4755 a note section in a PT_NOTE segment. These take
4756 file space but are not loaded into memory. */
4757 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4758 p
->p_memsz
+= this_hdr
->sh_size
;
4760 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4762 if (p
->p_type
== PT_TLS
)
4763 p
->p_memsz
+= this_hdr
->sh_size
;
4765 /* .tbss is special. It doesn't contribute to p_memsz of
4767 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4768 p
->p_memsz
+= this_hdr
->sh_size
;
4771 if (align
> p
->p_align
4772 && !m
->p_align_valid
4773 && (p
->p_type
!= PT_LOAD
4774 || (abfd
->flags
& D_PAGED
) == 0))
4778 if (!m
->p_flags_valid
)
4781 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4783 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4789 /* Check that all sections are in a PT_LOAD segment.
4790 Don't check funky gdb generated core files. */
4791 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4793 bfd_boolean check_vma
= TRUE
;
4795 for (i
= 1; i
< m
->count
; i
++)
4796 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4797 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4798 ->this_hdr
), p
) != 0
4799 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4800 ->this_hdr
), p
) != 0)
4802 /* Looks like we have overlays packed into the segment. */
4807 for (i
= 0; i
< m
->count
; i
++)
4809 Elf_Internal_Shdr
*this_hdr
;
4812 sec
= m
->sections
[i
];
4813 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4814 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
4815 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
4817 (*_bfd_error_handler
)
4818 (_("%B: section `%A' can't be allocated in segment %d"),
4820 print_segment_map (m
);
4826 elf_next_file_pos (abfd
) = off
;
4830 /* Assign file positions for the other sections. */
4833 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4834 struct bfd_link_info
*link_info
)
4836 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4837 Elf_Internal_Shdr
**i_shdrpp
;
4838 Elf_Internal_Shdr
**hdrpp
;
4839 Elf_Internal_Phdr
*phdrs
;
4840 Elf_Internal_Phdr
*p
;
4841 struct elf_segment_map
*m
;
4842 struct elf_segment_map
*hdrs_segment
;
4843 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4844 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4846 unsigned int num_sec
;
4850 i_shdrpp
= elf_elfsections (abfd
);
4851 num_sec
= elf_numsections (abfd
);
4852 off
= elf_next_file_pos (abfd
);
4853 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4855 Elf_Internal_Shdr
*hdr
;
4858 if (hdr
->bfd_section
!= NULL
4859 && (hdr
->bfd_section
->filepos
!= 0
4860 || (hdr
->sh_type
== SHT_NOBITS
4861 && hdr
->contents
== NULL
)))
4862 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4863 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4865 if (hdr
->sh_size
!= 0)
4866 (*_bfd_error_handler
)
4867 (_("%B: warning: allocated section `%s' not in segment"),
4869 (hdr
->bfd_section
== NULL
4871 : hdr
->bfd_section
->name
));
4872 /* We don't need to page align empty sections. */
4873 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4874 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4877 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4879 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4882 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4883 && hdr
->bfd_section
== NULL
)
4884 || hdr
== i_shdrpp
[elf_onesymtab (abfd
)]
4885 || hdr
== i_shdrpp
[elf_symtab_shndx (abfd
)]
4886 || hdr
== i_shdrpp
[elf_strtab_sec (abfd
)])
4887 hdr
->sh_offset
= -1;
4889 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4892 /* Now that we have set the section file positions, we can set up
4893 the file positions for the non PT_LOAD segments. */
4897 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4899 hdrs_segment
= NULL
;
4900 phdrs
= elf_tdata (abfd
)->phdr
;
4901 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
4904 if (p
->p_type
!= PT_LOAD
)
4907 if (m
->includes_filehdr
)
4909 filehdr_vaddr
= p
->p_vaddr
;
4910 filehdr_paddr
= p
->p_paddr
;
4912 if (m
->includes_phdrs
)
4914 phdrs_vaddr
= p
->p_vaddr
;
4915 phdrs_paddr
= p
->p_paddr
;
4916 if (m
->includes_filehdr
)
4919 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4920 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4925 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
4927 /* There is a segment that contains both the file headers and the
4928 program headers, so provide a symbol __ehdr_start pointing there.
4929 A program can use this to examine itself robustly. */
4931 struct elf_link_hash_entry
*hash
4932 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
4933 FALSE
, FALSE
, TRUE
);
4934 /* If the symbol was referenced and not defined, define it. */
4936 && (hash
->root
.type
== bfd_link_hash_new
4937 || hash
->root
.type
== bfd_link_hash_undefined
4938 || hash
->root
.type
== bfd_link_hash_undefweak
4939 || hash
->root
.type
== bfd_link_hash_common
))
4942 if (hdrs_segment
->count
!= 0)
4943 /* The segment contains sections, so use the first one. */
4944 s
= hdrs_segment
->sections
[0];
4946 /* Use the first (i.e. lowest-addressed) section in any segment. */
4947 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
4956 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
4957 hash
->root
.u
.def
.section
= s
;
4961 hash
->root
.u
.def
.value
= filehdr_vaddr
;
4962 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
4965 hash
->root
.type
= bfd_link_hash_defined
;
4966 hash
->def_regular
= 1;
4971 for (m
= elf_seg_map (abfd
), p
= phdrs
; m
!= NULL
; m
= m
->next
, p
++)
4973 if (p
->p_type
== PT_GNU_RELRO
)
4975 const Elf_Internal_Phdr
*lp
;
4976 struct elf_segment_map
*lm
;
4978 if (link_info
!= NULL
)
4980 /* During linking the range of the RELRO segment is passed
4982 for (lm
= elf_seg_map (abfd
), lp
= phdrs
;
4984 lm
= lm
->next
, lp
++)
4986 if (lp
->p_type
== PT_LOAD
4987 && lp
->p_vaddr
< link_info
->relro_end
4988 && lp
->p_vaddr
+ lp
->p_filesz
>= link_info
->relro_end
4990 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
4994 /* PR ld/14207. If the RELRO segment doesn't fit in the
4995 LOAD segment, it should be removed. */
4996 BFD_ASSERT (lm
!= NULL
);
5000 /* Otherwise we are copying an executable or shared
5001 library, but we need to use the same linker logic. */
5002 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5004 if (lp
->p_type
== PT_LOAD
5005 && lp
->p_paddr
== p
->p_paddr
)
5010 if (lp
< phdrs
+ count
)
5012 p
->p_vaddr
= lp
->p_vaddr
;
5013 p
->p_paddr
= lp
->p_paddr
;
5014 p
->p_offset
= lp
->p_offset
;
5015 if (link_info
!= NULL
)
5016 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5017 else if (m
->p_size_valid
)
5018 p
->p_filesz
= m
->p_size
;
5021 p
->p_memsz
= p
->p_filesz
;
5022 /* Preserve the alignment and flags if they are valid. The
5023 gold linker generates RW/4 for the PT_GNU_RELRO section.
5024 It is better for objcopy/strip to honor these attributes
5025 otherwise gdb will choke when using separate debug files.
5027 if (!m
->p_align_valid
)
5029 if (!m
->p_flags_valid
)
5030 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5034 memset (p
, 0, sizeof *p
);
5035 p
->p_type
= PT_NULL
;
5038 else if (p
->p_type
== PT_GNU_STACK
)
5040 if (m
->p_size_valid
)
5041 p
->p_memsz
= m
->p_size
;
5043 else if (m
->count
!= 0)
5045 if (p
->p_type
!= PT_LOAD
5046 && (p
->p_type
!= PT_NOTE
5047 || bfd_get_format (abfd
) != bfd_core
))
5049 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
5052 p
->p_offset
= m
->sections
[0]->filepos
;
5053 for (i
= m
->count
; i
-- != 0;)
5055 asection
*sect
= m
->sections
[i
];
5056 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5057 if (hdr
->sh_type
!= SHT_NOBITS
)
5059 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5066 else if (m
->includes_filehdr
)
5068 p
->p_vaddr
= filehdr_vaddr
;
5069 if (! m
->p_paddr_valid
)
5070 p
->p_paddr
= filehdr_paddr
;
5072 else if (m
->includes_phdrs
)
5074 p
->p_vaddr
= phdrs_vaddr
;
5075 if (! m
->p_paddr_valid
)
5076 p
->p_paddr
= phdrs_paddr
;
5080 elf_next_file_pos (abfd
) = off
;
5085 /* Work out the file positions of all the sections. This is called by
5086 _bfd_elf_compute_section_file_positions. All the section sizes and
5087 VMAs must be known before this is called.
5089 Reloc sections come in two flavours: Those processed specially as
5090 "side-channel" data attached to a section to which they apply, and
5091 those that bfd doesn't process as relocations. The latter sort are
5092 stored in a normal bfd section by bfd_section_from_shdr. We don't
5093 consider the former sort here, unless they form part of the loadable
5094 image. Reloc sections not assigned here will be handled later by
5095 assign_file_positions_for_relocs.
5097 We also don't set the positions of the .symtab and .strtab here. */
5100 assign_file_positions_except_relocs (bfd
*abfd
,
5101 struct bfd_link_info
*link_info
)
5103 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5104 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5106 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5108 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5109 && bfd_get_format (abfd
) != bfd_core
)
5111 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5112 unsigned int num_sec
= elf_numsections (abfd
);
5113 Elf_Internal_Shdr
**hdrpp
;
5116 /* Start after the ELF header. */
5117 off
= i_ehdrp
->e_ehsize
;
5119 /* We are not creating an executable, which means that we are
5120 not creating a program header, and that the actual order of
5121 the sections in the file is unimportant. */
5122 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5124 Elf_Internal_Shdr
*hdr
;
5127 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5128 && hdr
->bfd_section
== NULL
)
5129 || i
== elf_onesymtab (abfd
)
5130 || i
== elf_symtab_shndx (abfd
)
5131 || i
== elf_strtab_sec (abfd
))
5133 hdr
->sh_offset
= -1;
5136 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5143 /* Assign file positions for the loaded sections based on the
5144 assignment of sections to segments. */
5145 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5148 /* And for non-load sections. */
5149 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5152 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5154 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5158 /* Write out the program headers. */
5159 alloc
= elf_program_header_size (abfd
) / bed
->s
->sizeof_phdr
;
5160 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5161 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5164 off
= elf_next_file_pos (abfd
);
5167 /* Place the section headers. */
5168 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5169 i_ehdrp
->e_shoff
= off
;
5170 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5172 elf_next_file_pos (abfd
) = off
;
5178 prep_headers (bfd
*abfd
)
5180 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5181 struct elf_strtab_hash
*shstrtab
;
5182 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5184 i_ehdrp
= elf_elfheader (abfd
);
5186 shstrtab
= _bfd_elf_strtab_init ();
5187 if (shstrtab
== NULL
)
5190 elf_shstrtab (abfd
) = shstrtab
;
5192 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5193 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5194 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5195 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5197 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5198 i_ehdrp
->e_ident
[EI_DATA
] =
5199 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5200 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5202 if ((abfd
->flags
& DYNAMIC
) != 0)
5203 i_ehdrp
->e_type
= ET_DYN
;
5204 else if ((abfd
->flags
& EXEC_P
) != 0)
5205 i_ehdrp
->e_type
= ET_EXEC
;
5206 else if (bfd_get_format (abfd
) == bfd_core
)
5207 i_ehdrp
->e_type
= ET_CORE
;
5209 i_ehdrp
->e_type
= ET_REL
;
5211 switch (bfd_get_arch (abfd
))
5213 case bfd_arch_unknown
:
5214 i_ehdrp
->e_machine
= EM_NONE
;
5217 /* There used to be a long list of cases here, each one setting
5218 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5219 in the corresponding bfd definition. To avoid duplication,
5220 the switch was removed. Machines that need special handling
5221 can generally do it in elf_backend_final_write_processing(),
5222 unless they need the information earlier than the final write.
5223 Such need can generally be supplied by replacing the tests for
5224 e_machine with the conditions used to determine it. */
5226 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5229 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5230 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5232 /* No program header, for now. */
5233 i_ehdrp
->e_phoff
= 0;
5234 i_ehdrp
->e_phentsize
= 0;
5235 i_ehdrp
->e_phnum
= 0;
5237 /* Each bfd section is section header entry. */
5238 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5239 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5241 /* If we're building an executable, we'll need a program header table. */
5242 if (abfd
->flags
& EXEC_P
)
5243 /* It all happens later. */
5247 i_ehdrp
->e_phentsize
= 0;
5248 i_ehdrp
->e_phoff
= 0;
5251 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5252 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5253 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5254 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5255 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5256 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5257 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5258 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5259 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5265 /* Assign file positions for all the reloc sections which are not part
5266 of the loadable file image. */
5269 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5272 unsigned int i
, num_sec
;
5273 Elf_Internal_Shdr
**shdrpp
;
5275 off
= elf_next_file_pos (abfd
);
5277 num_sec
= elf_numsections (abfd
);
5278 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5280 Elf_Internal_Shdr
*shdrp
;
5283 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5284 && shdrp
->sh_offset
== -1)
5285 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5288 elf_next_file_pos (abfd
) = off
;
5292 _bfd_elf_write_object_contents (bfd
*abfd
)
5294 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5295 Elf_Internal_Shdr
**i_shdrp
;
5297 unsigned int count
, num_sec
;
5298 struct elf_obj_tdata
*t
;
5300 if (! abfd
->output_has_begun
5301 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5304 i_shdrp
= elf_elfsections (abfd
);
5307 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5311 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5313 /* After writing the headers, we need to write the sections too... */
5314 num_sec
= elf_numsections (abfd
);
5315 for (count
= 1; count
< num_sec
; count
++)
5317 if (bed
->elf_backend_section_processing
)
5318 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5319 if (i_shdrp
[count
]->contents
)
5321 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5323 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5324 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5329 /* Write out the section header names. */
5330 t
= elf_tdata (abfd
);
5331 if (elf_shstrtab (abfd
) != NULL
5332 && (bfd_seek (abfd
, t
->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5333 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5336 if (bed
->elf_backend_final_write_processing
)
5337 (*bed
->elf_backend_final_write_processing
) (abfd
, elf_linker (abfd
));
5339 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5342 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5343 if (t
->o
->build_id
.after_write_object_contents
!= NULL
)
5344 return (*t
->o
->build_id
.after_write_object_contents
) (abfd
);
5350 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5352 /* Hopefully this can be done just like an object file. */
5353 return _bfd_elf_write_object_contents (abfd
);
5356 /* Given a section, search the header to find them. */
5359 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5361 const struct elf_backend_data
*bed
;
5362 unsigned int sec_index
;
5364 if (elf_section_data (asect
) != NULL
5365 && elf_section_data (asect
)->this_idx
!= 0)
5366 return elf_section_data (asect
)->this_idx
;
5368 if (bfd_is_abs_section (asect
))
5369 sec_index
= SHN_ABS
;
5370 else if (bfd_is_com_section (asect
))
5371 sec_index
= SHN_COMMON
;
5372 else if (bfd_is_und_section (asect
))
5373 sec_index
= SHN_UNDEF
;
5375 sec_index
= SHN_BAD
;
5377 bed
= get_elf_backend_data (abfd
);
5378 if (bed
->elf_backend_section_from_bfd_section
)
5380 int retval
= sec_index
;
5382 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5386 if (sec_index
== SHN_BAD
)
5387 bfd_set_error (bfd_error_nonrepresentable_section
);
5392 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5396 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5398 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5400 flagword flags
= asym_ptr
->flags
;
5402 /* When gas creates relocations against local labels, it creates its
5403 own symbol for the section, but does put the symbol into the
5404 symbol chain, so udata is 0. When the linker is generating
5405 relocatable output, this section symbol may be for one of the
5406 input sections rather than the output section. */
5407 if (asym_ptr
->udata
.i
== 0
5408 && (flags
& BSF_SECTION_SYM
)
5409 && asym_ptr
->section
)
5414 sec
= asym_ptr
->section
;
5415 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5416 sec
= sec
->output_section
;
5417 if (sec
->owner
== abfd
5418 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5419 && elf_section_syms (abfd
)[indx
] != NULL
)
5420 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5423 idx
= asym_ptr
->udata
.i
;
5427 /* This case can occur when using --strip-symbol on a symbol
5428 which is used in a relocation entry. */
5429 (*_bfd_error_handler
)
5430 (_("%B: symbol `%s' required but not present"),
5431 abfd
, bfd_asymbol_name (asym_ptr
));
5432 bfd_set_error (bfd_error_no_symbols
);
5439 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5440 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5448 /* Rewrite program header information. */
5451 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5453 Elf_Internal_Ehdr
*iehdr
;
5454 struct elf_segment_map
*map
;
5455 struct elf_segment_map
*map_first
;
5456 struct elf_segment_map
**pointer_to_map
;
5457 Elf_Internal_Phdr
*segment
;
5460 unsigned int num_segments
;
5461 bfd_boolean phdr_included
= FALSE
;
5462 bfd_boolean p_paddr_valid
;
5463 bfd_vma maxpagesize
;
5464 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5465 unsigned int phdr_adjust_num
= 0;
5466 const struct elf_backend_data
*bed
;
5468 bed
= get_elf_backend_data (ibfd
);
5469 iehdr
= elf_elfheader (ibfd
);
5472 pointer_to_map
= &map_first
;
5474 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5475 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5477 /* Returns the end address of the segment + 1. */
5478 #define SEGMENT_END(segment, start) \
5479 (start + (segment->p_memsz > segment->p_filesz \
5480 ? segment->p_memsz : segment->p_filesz))
5482 #define SECTION_SIZE(section, segment) \
5483 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5484 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5485 ? section->size : 0)
5487 /* Returns TRUE if the given section is contained within
5488 the given segment. VMA addresses are compared. */
5489 #define IS_CONTAINED_BY_VMA(section, segment) \
5490 (section->vma >= segment->p_vaddr \
5491 && (section->vma + SECTION_SIZE (section, segment) \
5492 <= (SEGMENT_END (segment, segment->p_vaddr))))
5494 /* Returns TRUE if the given section is contained within
5495 the given segment. LMA addresses are compared. */
5496 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5497 (section->lma >= base \
5498 && (section->lma + SECTION_SIZE (section, segment) \
5499 <= SEGMENT_END (segment, base)))
5501 /* Handle PT_NOTE segment. */
5502 #define IS_NOTE(p, s) \
5503 (p->p_type == PT_NOTE \
5504 && elf_section_type (s) == SHT_NOTE \
5505 && (bfd_vma) s->filepos >= p->p_offset \
5506 && ((bfd_vma) s->filepos + s->size \
5507 <= p->p_offset + p->p_filesz))
5509 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5511 #define IS_COREFILE_NOTE(p, s) \
5513 && bfd_get_format (ibfd) == bfd_core \
5517 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5518 linker, which generates a PT_INTERP section with p_vaddr and
5519 p_memsz set to 0. */
5520 #define IS_SOLARIS_PT_INTERP(p, s) \
5522 && p->p_paddr == 0 \
5523 && p->p_memsz == 0 \
5524 && p->p_filesz > 0 \
5525 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5527 && (bfd_vma) s->filepos >= p->p_offset \
5528 && ((bfd_vma) s->filepos + s->size \
5529 <= p->p_offset + p->p_filesz))
5531 /* Decide if the given section should be included in the given segment.
5532 A section will be included if:
5533 1. It is within the address space of the segment -- we use the LMA
5534 if that is set for the segment and the VMA otherwise,
5535 2. It is an allocated section or a NOTE section in a PT_NOTE
5537 3. There is an output section associated with it,
5538 4. The section has not already been allocated to a previous segment.
5539 5. PT_GNU_STACK segments do not include any sections.
5540 6. PT_TLS segment includes only SHF_TLS sections.
5541 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5542 8. PT_DYNAMIC should not contain empty sections at the beginning
5543 (with the possible exception of .dynamic). */
5544 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5545 ((((segment->p_paddr \
5546 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5547 : IS_CONTAINED_BY_VMA (section, segment)) \
5548 && (section->flags & SEC_ALLOC) != 0) \
5549 || IS_NOTE (segment, section)) \
5550 && segment->p_type != PT_GNU_STACK \
5551 && (segment->p_type != PT_TLS \
5552 || (section->flags & SEC_THREAD_LOCAL)) \
5553 && (segment->p_type == PT_LOAD \
5554 || segment->p_type == PT_TLS \
5555 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5556 && (segment->p_type != PT_DYNAMIC \
5557 || SECTION_SIZE (section, segment) > 0 \
5558 || (segment->p_paddr \
5559 ? segment->p_paddr != section->lma \
5560 : segment->p_vaddr != section->vma) \
5561 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5563 && !section->segment_mark)
5565 /* If the output section of a section in the input segment is NULL,
5566 it is removed from the corresponding output segment. */
5567 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5568 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5569 && section->output_section != NULL)
5571 /* Returns TRUE iff seg1 starts after the end of seg2. */
5572 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5573 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5575 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5576 their VMA address ranges and their LMA address ranges overlap.
5577 It is possible to have overlapping VMA ranges without overlapping LMA
5578 ranges. RedBoot images for example can have both .data and .bss mapped
5579 to the same VMA range, but with the .data section mapped to a different
5581 #define SEGMENT_OVERLAPS(seg1, seg2) \
5582 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5583 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5584 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5585 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5587 /* Initialise the segment mark field. */
5588 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5589 section
->segment_mark
= FALSE
;
5591 /* The Solaris linker creates program headers in which all the
5592 p_paddr fields are zero. When we try to objcopy or strip such a
5593 file, we get confused. Check for this case, and if we find it
5594 don't set the p_paddr_valid fields. */
5595 p_paddr_valid
= FALSE
;
5596 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5599 if (segment
->p_paddr
!= 0)
5601 p_paddr_valid
= TRUE
;
5605 /* Scan through the segments specified in the program header
5606 of the input BFD. For this first scan we look for overlaps
5607 in the loadable segments. These can be created by weird
5608 parameters to objcopy. Also, fix some solaris weirdness. */
5609 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5614 Elf_Internal_Phdr
*segment2
;
5616 if (segment
->p_type
== PT_INTERP
)
5617 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5618 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5620 /* Mininal change so that the normal section to segment
5621 assignment code will work. */
5622 segment
->p_vaddr
= section
->vma
;
5626 if (segment
->p_type
!= PT_LOAD
)
5628 /* Remove PT_GNU_RELRO segment. */
5629 if (segment
->p_type
== PT_GNU_RELRO
)
5630 segment
->p_type
= PT_NULL
;
5634 /* Determine if this segment overlaps any previous segments. */
5635 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5637 bfd_signed_vma extra_length
;
5639 if (segment2
->p_type
!= PT_LOAD
5640 || !SEGMENT_OVERLAPS (segment
, segment2
))
5643 /* Merge the two segments together. */
5644 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5646 /* Extend SEGMENT2 to include SEGMENT and then delete
5648 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5649 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5651 if (extra_length
> 0)
5653 segment2
->p_memsz
+= extra_length
;
5654 segment2
->p_filesz
+= extra_length
;
5657 segment
->p_type
= PT_NULL
;
5659 /* Since we have deleted P we must restart the outer loop. */
5661 segment
= elf_tdata (ibfd
)->phdr
;
5666 /* Extend SEGMENT to include SEGMENT2 and then delete
5668 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5669 - SEGMENT_END (segment
, segment
->p_vaddr
));
5671 if (extra_length
> 0)
5673 segment
->p_memsz
+= extra_length
;
5674 segment
->p_filesz
+= extra_length
;
5677 segment2
->p_type
= PT_NULL
;
5682 /* The second scan attempts to assign sections to segments. */
5683 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5687 unsigned int section_count
;
5688 asection
**sections
;
5689 asection
*output_section
;
5691 bfd_vma matching_lma
;
5692 bfd_vma suggested_lma
;
5695 asection
*first_section
;
5696 bfd_boolean first_matching_lma
;
5697 bfd_boolean first_suggested_lma
;
5699 if (segment
->p_type
== PT_NULL
)
5702 first_section
= NULL
;
5703 /* Compute how many sections might be placed into this segment. */
5704 for (section
= ibfd
->sections
, section_count
= 0;
5706 section
= section
->next
)
5708 /* Find the first section in the input segment, which may be
5709 removed from the corresponding output segment. */
5710 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5712 if (first_section
== NULL
)
5713 first_section
= section
;
5714 if (section
->output_section
!= NULL
)
5719 /* Allocate a segment map big enough to contain
5720 all of the sections we have selected. */
5721 amt
= sizeof (struct elf_segment_map
);
5722 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5723 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5727 /* Initialise the fields of the segment map. Default to
5728 using the physical address of the segment in the input BFD. */
5730 map
->p_type
= segment
->p_type
;
5731 map
->p_flags
= segment
->p_flags
;
5732 map
->p_flags_valid
= 1;
5734 /* If the first section in the input segment is removed, there is
5735 no need to preserve segment physical address in the corresponding
5737 if (!first_section
|| first_section
->output_section
!= NULL
)
5739 map
->p_paddr
= segment
->p_paddr
;
5740 map
->p_paddr_valid
= p_paddr_valid
;
5743 /* Determine if this segment contains the ELF file header
5744 and if it contains the program headers themselves. */
5745 map
->includes_filehdr
= (segment
->p_offset
== 0
5746 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5747 map
->includes_phdrs
= 0;
5749 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5751 map
->includes_phdrs
=
5752 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5753 && (segment
->p_offset
+ segment
->p_filesz
5754 >= ((bfd_vma
) iehdr
->e_phoff
5755 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5757 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5758 phdr_included
= TRUE
;
5761 if (section_count
== 0)
5763 /* Special segments, such as the PT_PHDR segment, may contain
5764 no sections, but ordinary, loadable segments should contain
5765 something. They are allowed by the ELF spec however, so only
5766 a warning is produced. */
5767 if (segment
->p_type
== PT_LOAD
)
5768 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5769 " detected, is this intentional ?\n"),
5773 *pointer_to_map
= map
;
5774 pointer_to_map
= &map
->next
;
5779 /* Now scan the sections in the input BFD again and attempt
5780 to add their corresponding output sections to the segment map.
5781 The problem here is how to handle an output section which has
5782 been moved (ie had its LMA changed). There are four possibilities:
5784 1. None of the sections have been moved.
5785 In this case we can continue to use the segment LMA from the
5788 2. All of the sections have been moved by the same amount.
5789 In this case we can change the segment's LMA to match the LMA
5790 of the first section.
5792 3. Some of the sections have been moved, others have not.
5793 In this case those sections which have not been moved can be
5794 placed in the current segment which will have to have its size,
5795 and possibly its LMA changed, and a new segment or segments will
5796 have to be created to contain the other sections.
5798 4. The sections have been moved, but not by the same amount.
5799 In this case we can change the segment's LMA to match the LMA
5800 of the first section and we will have to create a new segment
5801 or segments to contain the other sections.
5803 In order to save time, we allocate an array to hold the section
5804 pointers that we are interested in. As these sections get assigned
5805 to a segment, they are removed from this array. */
5807 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5808 if (sections
== NULL
)
5811 /* Step One: Scan for segment vs section LMA conflicts.
5812 Also add the sections to the section array allocated above.
5813 Also add the sections to the current segment. In the common
5814 case, where the sections have not been moved, this means that
5815 we have completely filled the segment, and there is nothing
5820 first_matching_lma
= TRUE
;
5821 first_suggested_lma
= TRUE
;
5823 for (section
= ibfd
->sections
;
5825 section
= section
->next
)
5826 if (section
== first_section
)
5829 for (j
= 0; section
!= NULL
; section
= section
->next
)
5831 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5833 output_section
= section
->output_section
;
5835 sections
[j
++] = section
;
5837 /* The Solaris native linker always sets p_paddr to 0.
5838 We try to catch that case here, and set it to the
5839 correct value. Note - some backends require that
5840 p_paddr be left as zero. */
5842 && segment
->p_vaddr
!= 0
5843 && !bed
->want_p_paddr_set_to_zero
5845 && output_section
->lma
!= 0
5846 && output_section
->vma
== (segment
->p_vaddr
5847 + (map
->includes_filehdr
5850 + (map
->includes_phdrs
5852 * iehdr
->e_phentsize
)
5854 map
->p_paddr
= segment
->p_vaddr
;
5856 /* Match up the physical address of the segment with the
5857 LMA address of the output section. */
5858 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5859 || IS_COREFILE_NOTE (segment
, section
)
5860 || (bed
->want_p_paddr_set_to_zero
5861 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5863 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5865 matching_lma
= output_section
->lma
;
5866 first_matching_lma
= FALSE
;
5869 /* We assume that if the section fits within the segment
5870 then it does not overlap any other section within that
5872 map
->sections
[isec
++] = output_section
;
5874 else if (first_suggested_lma
)
5876 suggested_lma
= output_section
->lma
;
5877 first_suggested_lma
= FALSE
;
5880 if (j
== section_count
)
5885 BFD_ASSERT (j
== section_count
);
5887 /* Step Two: Adjust the physical address of the current segment,
5889 if (isec
== section_count
)
5891 /* All of the sections fitted within the segment as currently
5892 specified. This is the default case. Add the segment to
5893 the list of built segments and carry on to process the next
5894 program header in the input BFD. */
5895 map
->count
= section_count
;
5896 *pointer_to_map
= map
;
5897 pointer_to_map
= &map
->next
;
5900 && !bed
->want_p_paddr_set_to_zero
5901 && matching_lma
!= map
->p_paddr
5902 && !map
->includes_filehdr
5903 && !map
->includes_phdrs
)
5904 /* There is some padding before the first section in the
5905 segment. So, we must account for that in the output
5907 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5914 if (!first_matching_lma
)
5916 /* At least one section fits inside the current segment.
5917 Keep it, but modify its physical address to match the
5918 LMA of the first section that fitted. */
5919 map
->p_paddr
= matching_lma
;
5923 /* None of the sections fitted inside the current segment.
5924 Change the current segment's physical address to match
5925 the LMA of the first section. */
5926 map
->p_paddr
= suggested_lma
;
5929 /* Offset the segment physical address from the lma
5930 to allow for space taken up by elf headers. */
5931 if (map
->includes_filehdr
)
5933 if (map
->p_paddr
>= iehdr
->e_ehsize
)
5934 map
->p_paddr
-= iehdr
->e_ehsize
;
5937 map
->includes_filehdr
= FALSE
;
5938 map
->includes_phdrs
= FALSE
;
5942 if (map
->includes_phdrs
)
5944 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
5946 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5948 /* iehdr->e_phnum is just an estimate of the number
5949 of program headers that we will need. Make a note
5950 here of the number we used and the segment we chose
5951 to hold these headers, so that we can adjust the
5952 offset when we know the correct value. */
5953 phdr_adjust_num
= iehdr
->e_phnum
;
5954 phdr_adjust_seg
= map
;
5957 map
->includes_phdrs
= FALSE
;
5961 /* Step Three: Loop over the sections again, this time assigning
5962 those that fit to the current segment and removing them from the
5963 sections array; but making sure not to leave large gaps. Once all
5964 possible sections have been assigned to the current segment it is
5965 added to the list of built segments and if sections still remain
5966 to be assigned, a new segment is constructed before repeating
5973 first_suggested_lma
= TRUE
;
5975 /* Fill the current segment with sections that fit. */
5976 for (j
= 0; j
< section_count
; j
++)
5978 section
= sections
[j
];
5980 if (section
== NULL
)
5983 output_section
= section
->output_section
;
5985 BFD_ASSERT (output_section
!= NULL
);
5987 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5988 || IS_COREFILE_NOTE (segment
, section
))
5990 if (map
->count
== 0)
5992 /* If the first section in a segment does not start at
5993 the beginning of the segment, then something is
5995 if (output_section
->lma
5997 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5998 + (map
->includes_phdrs
5999 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6007 prev_sec
= map
->sections
[map
->count
- 1];
6009 /* If the gap between the end of the previous section
6010 and the start of this section is more than
6011 maxpagesize then we need to start a new segment. */
6012 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6014 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6015 || (prev_sec
->lma
+ prev_sec
->size
6016 > output_section
->lma
))
6018 if (first_suggested_lma
)
6020 suggested_lma
= output_section
->lma
;
6021 first_suggested_lma
= FALSE
;
6028 map
->sections
[map
->count
++] = output_section
;
6031 section
->segment_mark
= TRUE
;
6033 else if (first_suggested_lma
)
6035 suggested_lma
= output_section
->lma
;
6036 first_suggested_lma
= FALSE
;
6040 BFD_ASSERT (map
->count
> 0);
6042 /* Add the current segment to the list of built segments. */
6043 *pointer_to_map
= map
;
6044 pointer_to_map
= &map
->next
;
6046 if (isec
< section_count
)
6048 /* We still have not allocated all of the sections to
6049 segments. Create a new segment here, initialise it
6050 and carry on looping. */
6051 amt
= sizeof (struct elf_segment_map
);
6052 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6053 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6060 /* Initialise the fields of the segment map. Set the physical
6061 physical address to the LMA of the first section that has
6062 not yet been assigned. */
6064 map
->p_type
= segment
->p_type
;
6065 map
->p_flags
= segment
->p_flags
;
6066 map
->p_flags_valid
= 1;
6067 map
->p_paddr
= suggested_lma
;
6068 map
->p_paddr_valid
= p_paddr_valid
;
6069 map
->includes_filehdr
= 0;
6070 map
->includes_phdrs
= 0;
6073 while (isec
< section_count
);
6078 elf_seg_map (obfd
) = map_first
;
6080 /* If we had to estimate the number of program headers that were
6081 going to be needed, then check our estimate now and adjust
6082 the offset if necessary. */
6083 if (phdr_adjust_seg
!= NULL
)
6087 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6090 if (count
> phdr_adjust_num
)
6091 phdr_adjust_seg
->p_paddr
6092 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6097 #undef IS_CONTAINED_BY_VMA
6098 #undef IS_CONTAINED_BY_LMA
6100 #undef IS_COREFILE_NOTE
6101 #undef IS_SOLARIS_PT_INTERP
6102 #undef IS_SECTION_IN_INPUT_SEGMENT
6103 #undef INCLUDE_SECTION_IN_SEGMENT
6104 #undef SEGMENT_AFTER_SEGMENT
6105 #undef SEGMENT_OVERLAPS
6109 /* Copy ELF program header information. */
6112 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6114 Elf_Internal_Ehdr
*iehdr
;
6115 struct elf_segment_map
*map
;
6116 struct elf_segment_map
*map_first
;
6117 struct elf_segment_map
**pointer_to_map
;
6118 Elf_Internal_Phdr
*segment
;
6120 unsigned int num_segments
;
6121 bfd_boolean phdr_included
= FALSE
;
6122 bfd_boolean p_paddr_valid
;
6124 iehdr
= elf_elfheader (ibfd
);
6127 pointer_to_map
= &map_first
;
6129 /* If all the segment p_paddr fields are zero, don't set
6130 map->p_paddr_valid. */
6131 p_paddr_valid
= FALSE
;
6132 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6133 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6136 if (segment
->p_paddr
!= 0)
6138 p_paddr_valid
= TRUE
;
6142 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6147 unsigned int section_count
;
6149 Elf_Internal_Shdr
*this_hdr
;
6150 asection
*first_section
= NULL
;
6151 asection
*lowest_section
;
6153 /* Compute how many sections are in this segment. */
6154 for (section
= ibfd
->sections
, section_count
= 0;
6156 section
= section
->next
)
6158 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6159 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6161 if (first_section
== NULL
)
6162 first_section
= section
;
6167 /* Allocate a segment map big enough to contain
6168 all of the sections we have selected. */
6169 amt
= sizeof (struct elf_segment_map
);
6170 if (section_count
!= 0)
6171 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6172 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6176 /* Initialize the fields of the output segment map with the
6179 map
->p_type
= segment
->p_type
;
6180 map
->p_flags
= segment
->p_flags
;
6181 map
->p_flags_valid
= 1;
6182 map
->p_paddr
= segment
->p_paddr
;
6183 map
->p_paddr_valid
= p_paddr_valid
;
6184 map
->p_align
= segment
->p_align
;
6185 map
->p_align_valid
= 1;
6186 map
->p_vaddr_offset
= 0;
6188 if (map
->p_type
== PT_GNU_RELRO
6189 || map
->p_type
== PT_GNU_STACK
)
6191 /* The PT_GNU_RELRO segment may contain the first a few
6192 bytes in the .got.plt section even if the whole .got.plt
6193 section isn't in the PT_GNU_RELRO segment. We won't
6194 change the size of the PT_GNU_RELRO segment.
6195 Similarly, PT_GNU_STACK size is significant on uclinux
6197 map
->p_size
= segment
->p_memsz
;
6198 map
->p_size_valid
= 1;
6201 /* Determine if this segment contains the ELF file header
6202 and if it contains the program headers themselves. */
6203 map
->includes_filehdr
= (segment
->p_offset
== 0
6204 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6206 map
->includes_phdrs
= 0;
6207 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6209 map
->includes_phdrs
=
6210 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6211 && (segment
->p_offset
+ segment
->p_filesz
6212 >= ((bfd_vma
) iehdr
->e_phoff
6213 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6215 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6216 phdr_included
= TRUE
;
6219 lowest_section
= first_section
;
6220 if (section_count
!= 0)
6222 unsigned int isec
= 0;
6224 for (section
= first_section
;
6226 section
= section
->next
)
6228 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6229 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6231 map
->sections
[isec
++] = section
->output_section
;
6232 if (section
->lma
< lowest_section
->lma
)
6233 lowest_section
= section
;
6234 if ((section
->flags
& SEC_ALLOC
) != 0)
6238 /* Section lmas are set up from PT_LOAD header
6239 p_paddr in _bfd_elf_make_section_from_shdr.
6240 If this header has a p_paddr that disagrees
6241 with the section lma, flag the p_paddr as
6243 if ((section
->flags
& SEC_LOAD
) != 0)
6244 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6246 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6247 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6248 map
->p_paddr_valid
= FALSE
;
6250 if (isec
== section_count
)
6256 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6257 /* We need to keep the space used by the headers fixed. */
6258 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6260 if (!map
->includes_phdrs
6261 && !map
->includes_filehdr
6262 && map
->p_paddr_valid
)
6263 /* There is some other padding before the first section. */
6264 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6265 - segment
->p_paddr
);
6267 map
->count
= section_count
;
6268 *pointer_to_map
= map
;
6269 pointer_to_map
= &map
->next
;
6272 elf_seg_map (obfd
) = map_first
;
6276 /* Copy private BFD data. This copies or rewrites ELF program header
6280 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6282 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6283 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6286 if (elf_tdata (ibfd
)->phdr
== NULL
)
6289 if (ibfd
->xvec
== obfd
->xvec
)
6291 /* Check to see if any sections in the input BFD
6292 covered by ELF program header have changed. */
6293 Elf_Internal_Phdr
*segment
;
6294 asection
*section
, *osec
;
6295 unsigned int i
, num_segments
;
6296 Elf_Internal_Shdr
*this_hdr
;
6297 const struct elf_backend_data
*bed
;
6299 bed
= get_elf_backend_data (ibfd
);
6301 /* Regenerate the segment map if p_paddr is set to 0. */
6302 if (bed
->want_p_paddr_set_to_zero
)
6305 /* Initialize the segment mark field. */
6306 for (section
= obfd
->sections
; section
!= NULL
;
6307 section
= section
->next
)
6308 section
->segment_mark
= FALSE
;
6310 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6311 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6315 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6316 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6317 which severly confuses things, so always regenerate the segment
6318 map in this case. */
6319 if (segment
->p_paddr
== 0
6320 && segment
->p_memsz
== 0
6321 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6324 for (section
= ibfd
->sections
;
6325 section
!= NULL
; section
= section
->next
)
6327 /* We mark the output section so that we know it comes
6328 from the input BFD. */
6329 osec
= section
->output_section
;
6331 osec
->segment_mark
= TRUE
;
6333 /* Check if this section is covered by the segment. */
6334 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6335 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6337 /* FIXME: Check if its output section is changed or
6338 removed. What else do we need to check? */
6340 || section
->flags
!= osec
->flags
6341 || section
->lma
!= osec
->lma
6342 || section
->vma
!= osec
->vma
6343 || section
->size
!= osec
->size
6344 || section
->rawsize
!= osec
->rawsize
6345 || section
->alignment_power
!= osec
->alignment_power
)
6351 /* Check to see if any output section do not come from the
6353 for (section
= obfd
->sections
; section
!= NULL
;
6354 section
= section
->next
)
6356 if (section
->segment_mark
== FALSE
)
6359 section
->segment_mark
= FALSE
;
6362 return copy_elf_program_header (ibfd
, obfd
);
6366 if (ibfd
->xvec
== obfd
->xvec
)
6368 /* When rewriting program header, set the output maxpagesize to
6369 the maximum alignment of input PT_LOAD segments. */
6370 Elf_Internal_Phdr
*segment
;
6372 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6373 bfd_vma maxpagesize
= 0;
6375 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6378 if (segment
->p_type
== PT_LOAD
6379 && maxpagesize
< segment
->p_align
)
6380 maxpagesize
= segment
->p_align
;
6382 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6383 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6386 return rewrite_elf_program_header (ibfd
, obfd
);
6389 /* Initialize private output section information from input section. */
6392 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6396 struct bfd_link_info
*link_info
)
6399 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6400 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6402 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6403 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6406 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6408 /* For objcopy and relocatable link, don't copy the output ELF
6409 section type from input if the output BFD section flags have been
6410 set to something different. For a final link allow some flags
6411 that the linker clears to differ. */
6412 if (elf_section_type (osec
) == SHT_NULL
6413 && (osec
->flags
== isec
->flags
6415 && ((osec
->flags
^ isec
->flags
)
6416 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6417 elf_section_type (osec
) = elf_section_type (isec
);
6419 /* FIXME: Is this correct for all OS/PROC specific flags? */
6420 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6421 & (SHF_MASKOS
| SHF_MASKPROC
));
6423 /* Set things up for objcopy and relocatable link. The output
6424 SHT_GROUP section will have its elf_next_in_group pointing back
6425 to the input group members. Ignore linker created group section.
6426 See elfNN_ia64_object_p in elfxx-ia64.c. */
6429 if (elf_sec_group (isec
) == NULL
6430 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6432 if (elf_section_flags (isec
) & SHF_GROUP
)
6433 elf_section_flags (osec
) |= SHF_GROUP
;
6434 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6435 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6439 ihdr
= &elf_section_data (isec
)->this_hdr
;
6441 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6442 don't use the output section of the linked-to section since it
6443 may be NULL at this point. */
6444 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6446 ohdr
= &elf_section_data (osec
)->this_hdr
;
6447 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6448 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6451 osec
->use_rela_p
= isec
->use_rela_p
;
6456 /* Copy private section information. This copies over the entsize
6457 field, and sometimes the info field. */
6460 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6465 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6467 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6468 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6471 ihdr
= &elf_section_data (isec
)->this_hdr
;
6472 ohdr
= &elf_section_data (osec
)->this_hdr
;
6474 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6476 if (ihdr
->sh_type
== SHT_SYMTAB
6477 || ihdr
->sh_type
== SHT_DYNSYM
6478 || ihdr
->sh_type
== SHT_GNU_verneed
6479 || ihdr
->sh_type
== SHT_GNU_verdef
)
6480 ohdr
->sh_info
= ihdr
->sh_info
;
6482 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6486 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6487 necessary if we are removing either the SHT_GROUP section or any of
6488 the group member sections. DISCARDED is the value that a section's
6489 output_section has if the section will be discarded, NULL when this
6490 function is called from objcopy, bfd_abs_section_ptr when called
6494 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6498 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6499 if (elf_section_type (isec
) == SHT_GROUP
)
6501 asection
*first
= elf_next_in_group (isec
);
6502 asection
*s
= first
;
6503 bfd_size_type removed
= 0;
6507 /* If this member section is being output but the
6508 SHT_GROUP section is not, then clear the group info
6509 set up by _bfd_elf_copy_private_section_data. */
6510 if (s
->output_section
!= discarded
6511 && isec
->output_section
== discarded
)
6513 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6514 elf_group_name (s
->output_section
) = NULL
;
6516 /* Conversely, if the member section is not being output
6517 but the SHT_GROUP section is, then adjust its size. */
6518 else if (s
->output_section
== discarded
6519 && isec
->output_section
!= discarded
)
6521 s
= elf_next_in_group (s
);
6527 if (discarded
!= NULL
)
6529 /* If we've been called for ld -r, then we need to
6530 adjust the input section size. This function may
6531 be called multiple times, so save the original
6533 if (isec
->rawsize
== 0)
6534 isec
->rawsize
= isec
->size
;
6535 isec
->size
= isec
->rawsize
- removed
;
6539 /* Adjust the output section size when called from
6541 isec
->output_section
->size
-= removed
;
6549 /* Copy private header information. */
6552 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6554 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6555 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6558 /* Copy over private BFD data if it has not already been copied.
6559 This must be done here, rather than in the copy_private_bfd_data
6560 entry point, because the latter is called after the section
6561 contents have been set, which means that the program headers have
6562 already been worked out. */
6563 if (elf_seg_map (obfd
) == NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6565 if (! copy_private_bfd_data (ibfd
, obfd
))
6569 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6572 /* Copy private symbol information. If this symbol is in a section
6573 which we did not map into a BFD section, try to map the section
6574 index correctly. We use special macro definitions for the mapped
6575 section indices; these definitions are interpreted by the
6576 swap_out_syms function. */
6578 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6579 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6580 #define MAP_STRTAB (SHN_HIOS + 3)
6581 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6582 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6585 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6590 elf_symbol_type
*isym
, *osym
;
6592 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6593 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6596 isym
= elf_symbol_from (ibfd
, isymarg
);
6597 osym
= elf_symbol_from (obfd
, osymarg
);
6600 && isym
->internal_elf_sym
.st_shndx
!= 0
6602 && bfd_is_abs_section (isym
->symbol
.section
))
6606 shndx
= isym
->internal_elf_sym
.st_shndx
;
6607 if (shndx
== elf_onesymtab (ibfd
))
6608 shndx
= MAP_ONESYMTAB
;
6609 else if (shndx
== elf_dynsymtab (ibfd
))
6610 shndx
= MAP_DYNSYMTAB
;
6611 else if (shndx
== elf_strtab_sec (ibfd
))
6613 else if (shndx
== elf_shstrtab_sec (ibfd
))
6614 shndx
= MAP_SHSTRTAB
;
6615 else if (shndx
== elf_symtab_shndx (ibfd
))
6616 shndx
= MAP_SYM_SHNDX
;
6617 osym
->internal_elf_sym
.st_shndx
= shndx
;
6623 /* Swap out the symbols. */
6626 swap_out_syms (bfd
*abfd
,
6627 struct bfd_strtab_hash
**sttp
,
6630 const struct elf_backend_data
*bed
;
6633 struct bfd_strtab_hash
*stt
;
6634 Elf_Internal_Shdr
*symtab_hdr
;
6635 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6636 Elf_Internal_Shdr
*symstrtab_hdr
;
6637 bfd_byte
*outbound_syms
;
6638 bfd_byte
*outbound_shndx
;
6640 unsigned int num_locals
;
6642 bfd_boolean name_local_sections
;
6644 if (!elf_map_symbols (abfd
, &num_locals
))
6647 /* Dump out the symtabs. */
6648 stt
= _bfd_elf_stringtab_init ();
6652 bed
= get_elf_backend_data (abfd
);
6653 symcount
= bfd_get_symcount (abfd
);
6654 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6655 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6656 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6657 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6658 symtab_hdr
->sh_info
= num_locals
+ 1;
6659 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6661 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6662 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6664 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6665 bed
->s
->sizeof_sym
);
6666 if (outbound_syms
== NULL
)
6668 _bfd_stringtab_free (stt
);
6671 symtab_hdr
->contents
= outbound_syms
;
6673 outbound_shndx
= NULL
;
6674 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6675 if (symtab_shndx_hdr
->sh_name
!= 0)
6677 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6678 outbound_shndx
= (bfd_byte
*)
6679 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6680 if (outbound_shndx
== NULL
)
6682 _bfd_stringtab_free (stt
);
6686 symtab_shndx_hdr
->contents
= outbound_shndx
;
6687 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6688 symtab_shndx_hdr
->sh_size
= amt
;
6689 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6690 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6693 /* Now generate the data (for "contents"). */
6695 /* Fill in zeroth symbol and swap it out. */
6696 Elf_Internal_Sym sym
;
6702 sym
.st_shndx
= SHN_UNDEF
;
6703 sym
.st_target_internal
= 0;
6704 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6705 outbound_syms
+= bed
->s
->sizeof_sym
;
6706 if (outbound_shndx
!= NULL
)
6707 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6711 = (bed
->elf_backend_name_local_section_symbols
6712 && bed
->elf_backend_name_local_section_symbols (abfd
));
6714 syms
= bfd_get_outsymbols (abfd
);
6715 for (idx
= 0; idx
< symcount
; idx
++)
6717 Elf_Internal_Sym sym
;
6718 bfd_vma value
= syms
[idx
]->value
;
6719 elf_symbol_type
*type_ptr
;
6720 flagword flags
= syms
[idx
]->flags
;
6723 if (!name_local_sections
6724 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6726 /* Local section symbols have no name. */
6731 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6734 if (sym
.st_name
== (unsigned long) -1)
6736 _bfd_stringtab_free (stt
);
6741 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6743 if ((flags
& BSF_SECTION_SYM
) == 0
6744 && bfd_is_com_section (syms
[idx
]->section
))
6746 /* ELF common symbols put the alignment into the `value' field,
6747 and the size into the `size' field. This is backwards from
6748 how BFD handles it, so reverse it here. */
6749 sym
.st_size
= value
;
6750 if (type_ptr
== NULL
6751 || type_ptr
->internal_elf_sym
.st_value
== 0)
6752 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6754 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6755 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6756 (abfd
, syms
[idx
]->section
);
6760 asection
*sec
= syms
[idx
]->section
;
6763 if (sec
->output_section
)
6765 value
+= sec
->output_offset
;
6766 sec
= sec
->output_section
;
6769 /* Don't add in the section vma for relocatable output. */
6770 if (! relocatable_p
)
6772 sym
.st_value
= value
;
6773 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6775 if (bfd_is_abs_section (sec
)
6777 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6779 /* This symbol is in a real ELF section which we did
6780 not create as a BFD section. Undo the mapping done
6781 by copy_private_symbol_data. */
6782 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6786 shndx
= elf_onesymtab (abfd
);
6789 shndx
= elf_dynsymtab (abfd
);
6792 shndx
= elf_strtab_sec (abfd
);
6795 shndx
= elf_shstrtab_sec (abfd
);
6798 shndx
= elf_symtab_shndx (abfd
);
6807 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6809 if (shndx
== SHN_BAD
)
6813 /* Writing this would be a hell of a lot easier if
6814 we had some decent documentation on bfd, and
6815 knew what to expect of the library, and what to
6816 demand of applications. For example, it
6817 appears that `objcopy' might not set the
6818 section of a symbol to be a section that is
6819 actually in the output file. */
6820 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6823 _bfd_error_handler (_("\
6824 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6825 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6827 bfd_set_error (bfd_error_invalid_operation
);
6828 _bfd_stringtab_free (stt
);
6832 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6833 BFD_ASSERT (shndx
!= SHN_BAD
);
6837 sym
.st_shndx
= shndx
;
6840 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6842 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6843 type
= STT_GNU_IFUNC
;
6844 else if ((flags
& BSF_FUNCTION
) != 0)
6846 else if ((flags
& BSF_OBJECT
) != 0)
6848 else if ((flags
& BSF_RELC
) != 0)
6850 else if ((flags
& BSF_SRELC
) != 0)
6855 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6858 /* Processor-specific types. */
6859 if (type_ptr
!= NULL
6860 && bed
->elf_backend_get_symbol_type
)
6861 type
= ((*bed
->elf_backend_get_symbol_type
)
6862 (&type_ptr
->internal_elf_sym
, type
));
6864 if (flags
& BSF_SECTION_SYM
)
6866 if (flags
& BSF_GLOBAL
)
6867 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6869 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6871 else if (bfd_is_com_section (syms
[idx
]->section
))
6873 #ifdef USE_STT_COMMON
6874 if (type
== STT_OBJECT
)
6875 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6878 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6880 else if (bfd_is_und_section (syms
[idx
]->section
))
6881 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6885 else if (flags
& BSF_FILE
)
6886 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6889 int bind
= STB_LOCAL
;
6891 if (flags
& BSF_LOCAL
)
6893 else if (flags
& BSF_GNU_UNIQUE
)
6894 bind
= STB_GNU_UNIQUE
;
6895 else if (flags
& BSF_WEAK
)
6897 else if (flags
& BSF_GLOBAL
)
6900 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6903 if (type_ptr
!= NULL
)
6905 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6906 sym
.st_target_internal
6907 = type_ptr
->internal_elf_sym
.st_target_internal
;
6912 sym
.st_target_internal
= 0;
6915 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6916 outbound_syms
+= bed
->s
->sizeof_sym
;
6917 if (outbound_shndx
!= NULL
)
6918 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6922 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6923 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6925 symstrtab_hdr
->sh_flags
= 0;
6926 symstrtab_hdr
->sh_addr
= 0;
6927 symstrtab_hdr
->sh_entsize
= 0;
6928 symstrtab_hdr
->sh_link
= 0;
6929 symstrtab_hdr
->sh_info
= 0;
6930 symstrtab_hdr
->sh_addralign
= 1;
6935 /* Return the number of bytes required to hold the symtab vector.
6937 Note that we base it on the count plus 1, since we will null terminate
6938 the vector allocated based on this size. However, the ELF symbol table
6939 always has a dummy entry as symbol #0, so it ends up even. */
6942 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6946 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6948 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6949 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6951 symtab_size
-= sizeof (asymbol
*);
6957 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6961 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6963 if (elf_dynsymtab (abfd
) == 0)
6965 bfd_set_error (bfd_error_invalid_operation
);
6969 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6970 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6972 symtab_size
-= sizeof (asymbol
*);
6978 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6981 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6984 /* Canonicalize the relocs. */
6987 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6994 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6996 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6999 tblptr
= section
->relocation
;
7000 for (i
= 0; i
< section
->reloc_count
; i
++)
7001 *relptr
++ = tblptr
++;
7005 return section
->reloc_count
;
7009 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7011 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7012 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7015 bfd_get_symcount (abfd
) = symcount
;
7020 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7021 asymbol
**allocation
)
7023 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7024 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7027 bfd_get_dynamic_symcount (abfd
) = symcount
;
7031 /* Return the size required for the dynamic reloc entries. Any loadable
7032 section that was actually installed in the BFD, and has type SHT_REL
7033 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7034 dynamic reloc section. */
7037 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7042 if (elf_dynsymtab (abfd
) == 0)
7044 bfd_set_error (bfd_error_invalid_operation
);
7048 ret
= sizeof (arelent
*);
7049 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7050 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7051 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7052 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7053 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7054 * sizeof (arelent
*));
7059 /* Canonicalize the dynamic relocation entries. Note that we return the
7060 dynamic relocations as a single block, although they are actually
7061 associated with particular sections; the interface, which was
7062 designed for SunOS style shared libraries, expects that there is only
7063 one set of dynamic relocs. Any loadable section that was actually
7064 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7065 dynamic symbol table, is considered to be a dynamic reloc section. */
7068 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7072 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7076 if (elf_dynsymtab (abfd
) == 0)
7078 bfd_set_error (bfd_error_invalid_operation
);
7082 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7084 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7086 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7087 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7088 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7093 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7095 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7097 for (i
= 0; i
< count
; i
++)
7108 /* Read in the version information. */
7111 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7113 bfd_byte
*contents
= NULL
;
7114 unsigned int freeidx
= 0;
7116 if (elf_dynverref (abfd
) != 0)
7118 Elf_Internal_Shdr
*hdr
;
7119 Elf_External_Verneed
*everneed
;
7120 Elf_Internal_Verneed
*iverneed
;
7122 bfd_byte
*contents_end
;
7124 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7126 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7127 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7128 if (elf_tdata (abfd
)->verref
== NULL
)
7131 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
7133 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7134 if (contents
== NULL
)
7136 error_return_verref
:
7137 elf_tdata (abfd
)->verref
= NULL
;
7138 elf_tdata (abfd
)->cverrefs
= 0;
7141 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7142 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7143 goto error_return_verref
;
7145 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7146 goto error_return_verref
;
7148 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7149 == sizeof (Elf_External_Vernaux
));
7150 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7151 everneed
= (Elf_External_Verneed
*) contents
;
7152 iverneed
= elf_tdata (abfd
)->verref
;
7153 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7155 Elf_External_Vernaux
*evernaux
;
7156 Elf_Internal_Vernaux
*ivernaux
;
7159 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7161 iverneed
->vn_bfd
= abfd
;
7163 iverneed
->vn_filename
=
7164 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7166 if (iverneed
->vn_filename
== NULL
)
7167 goto error_return_verref
;
7169 if (iverneed
->vn_cnt
== 0)
7170 iverneed
->vn_auxptr
= NULL
;
7173 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7174 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7175 sizeof (Elf_Internal_Vernaux
));
7176 if (iverneed
->vn_auxptr
== NULL
)
7177 goto error_return_verref
;
7180 if (iverneed
->vn_aux
7181 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7182 goto error_return_verref
;
7184 evernaux
= ((Elf_External_Vernaux
*)
7185 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7186 ivernaux
= iverneed
->vn_auxptr
;
7187 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7189 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7191 ivernaux
->vna_nodename
=
7192 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7193 ivernaux
->vna_name
);
7194 if (ivernaux
->vna_nodename
== NULL
)
7195 goto error_return_verref
;
7197 if (j
+ 1 < iverneed
->vn_cnt
)
7198 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7200 ivernaux
->vna_nextptr
= NULL
;
7202 if (ivernaux
->vna_next
7203 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7204 goto error_return_verref
;
7206 evernaux
= ((Elf_External_Vernaux
*)
7207 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7209 if (ivernaux
->vna_other
> freeidx
)
7210 freeidx
= ivernaux
->vna_other
;
7213 if (i
+ 1 < hdr
->sh_info
)
7214 iverneed
->vn_nextref
= iverneed
+ 1;
7216 iverneed
->vn_nextref
= NULL
;
7218 if (iverneed
->vn_next
7219 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7220 goto error_return_verref
;
7222 everneed
= ((Elf_External_Verneed
*)
7223 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7230 if (elf_dynverdef (abfd
) != 0)
7232 Elf_Internal_Shdr
*hdr
;
7233 Elf_External_Verdef
*everdef
;
7234 Elf_Internal_Verdef
*iverdef
;
7235 Elf_Internal_Verdef
*iverdefarr
;
7236 Elf_Internal_Verdef iverdefmem
;
7238 unsigned int maxidx
;
7239 bfd_byte
*contents_end_def
, *contents_end_aux
;
7241 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7243 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7244 if (contents
== NULL
)
7246 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7247 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7250 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7253 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7254 >= sizeof (Elf_External_Verdaux
));
7255 contents_end_def
= contents
+ hdr
->sh_size
7256 - sizeof (Elf_External_Verdef
);
7257 contents_end_aux
= contents
+ hdr
->sh_size
7258 - sizeof (Elf_External_Verdaux
);
7260 /* We know the number of entries in the section but not the maximum
7261 index. Therefore we have to run through all entries and find
7263 everdef
= (Elf_External_Verdef
*) contents
;
7265 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7267 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7269 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7270 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7272 if (iverdefmem
.vd_next
7273 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7276 everdef
= ((Elf_External_Verdef
*)
7277 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7280 if (default_imported_symver
)
7282 if (freeidx
> maxidx
)
7287 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7288 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7289 if (elf_tdata (abfd
)->verdef
== NULL
)
7292 elf_tdata (abfd
)->cverdefs
= maxidx
;
7294 everdef
= (Elf_External_Verdef
*) contents
;
7295 iverdefarr
= elf_tdata (abfd
)->verdef
;
7296 for (i
= 0; i
< hdr
->sh_info
; i
++)
7298 Elf_External_Verdaux
*everdaux
;
7299 Elf_Internal_Verdaux
*iverdaux
;
7302 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7304 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7306 error_return_verdef
:
7307 elf_tdata (abfd
)->verdef
= NULL
;
7308 elf_tdata (abfd
)->cverdefs
= 0;
7312 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7313 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7315 iverdef
->vd_bfd
= abfd
;
7317 if (iverdef
->vd_cnt
== 0)
7318 iverdef
->vd_auxptr
= NULL
;
7321 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7322 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7323 sizeof (Elf_Internal_Verdaux
));
7324 if (iverdef
->vd_auxptr
== NULL
)
7325 goto error_return_verdef
;
7329 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7330 goto error_return_verdef
;
7332 everdaux
= ((Elf_External_Verdaux
*)
7333 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7334 iverdaux
= iverdef
->vd_auxptr
;
7335 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7337 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7339 iverdaux
->vda_nodename
=
7340 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7341 iverdaux
->vda_name
);
7342 if (iverdaux
->vda_nodename
== NULL
)
7343 goto error_return_verdef
;
7345 if (j
+ 1 < iverdef
->vd_cnt
)
7346 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7348 iverdaux
->vda_nextptr
= NULL
;
7350 if (iverdaux
->vda_next
7351 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7352 goto error_return_verdef
;
7354 everdaux
= ((Elf_External_Verdaux
*)
7355 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7358 if (iverdef
->vd_cnt
)
7359 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7361 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7362 iverdef
->vd_nextdef
= iverdef
+ 1;
7364 iverdef
->vd_nextdef
= NULL
;
7366 everdef
= ((Elf_External_Verdef
*)
7367 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7373 else if (default_imported_symver
)
7380 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7381 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7382 if (elf_tdata (abfd
)->verdef
== NULL
)
7385 elf_tdata (abfd
)->cverdefs
= freeidx
;
7388 /* Create a default version based on the soname. */
7389 if (default_imported_symver
)
7391 Elf_Internal_Verdef
*iverdef
;
7392 Elf_Internal_Verdaux
*iverdaux
;
7394 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7396 iverdef
->vd_version
= VER_DEF_CURRENT
;
7397 iverdef
->vd_flags
= 0;
7398 iverdef
->vd_ndx
= freeidx
;
7399 iverdef
->vd_cnt
= 1;
7401 iverdef
->vd_bfd
= abfd
;
7403 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7404 if (iverdef
->vd_nodename
== NULL
)
7405 goto error_return_verdef
;
7406 iverdef
->vd_nextdef
= NULL
;
7407 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7408 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7409 if (iverdef
->vd_auxptr
== NULL
)
7410 goto error_return_verdef
;
7412 iverdaux
= iverdef
->vd_auxptr
;
7413 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7414 iverdaux
->vda_nextptr
= NULL
;
7420 if (contents
!= NULL
)
7426 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7428 elf_symbol_type
*newsym
;
7429 bfd_size_type amt
= sizeof (elf_symbol_type
);
7431 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7436 newsym
->symbol
.the_bfd
= abfd
;
7437 return &newsym
->symbol
;
7442 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7446 bfd_symbol_info (symbol
, ret
);
7449 /* Return whether a symbol name implies a local symbol. Most targets
7450 use this function for the is_local_label_name entry point, but some
7454 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7457 /* Normal local symbols start with ``.L''. */
7458 if (name
[0] == '.' && name
[1] == 'L')
7461 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7462 DWARF debugging symbols starting with ``..''. */
7463 if (name
[0] == '.' && name
[1] == '.')
7466 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7467 emitting DWARF debugging output. I suspect this is actually a
7468 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7469 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7470 underscore to be emitted on some ELF targets). For ease of use,
7471 we treat such symbols as local. */
7472 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7479 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7480 asymbol
*symbol ATTRIBUTE_UNUSED
)
7487 _bfd_elf_set_arch_mach (bfd
*abfd
,
7488 enum bfd_architecture arch
,
7489 unsigned long machine
)
7491 /* If this isn't the right architecture for this backend, and this
7492 isn't the generic backend, fail. */
7493 if (arch
!= get_elf_backend_data (abfd
)->arch
7494 && arch
!= bfd_arch_unknown
7495 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7498 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7501 /* Find the function to a particular section and offset,
7502 for error reporting. */
7505 elf_find_function (bfd
*abfd
,
7509 const char **filename_ptr
,
7510 const char **functionname_ptr
)
7512 struct elf_find_function_cache
7514 asection
*last_section
;
7516 const char *filename
;
7517 bfd_size_type func_size
;
7520 if (symbols
== NULL
)
7523 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
7526 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
7527 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
7531 if (cache
->last_section
!= section
7532 || cache
->func
== NULL
7533 || offset
< cache
->func
->value
7534 || offset
>= cache
->func
->value
+ cache
->func_size
)
7539 /* ??? Given multiple file symbols, it is impossible to reliably
7540 choose the right file name for global symbols. File symbols are
7541 local symbols, and thus all file symbols must sort before any
7542 global symbols. The ELF spec may be interpreted to say that a
7543 file symbol must sort before other local symbols, but currently
7544 ld -r doesn't do this. So, for ld -r output, it is possible to
7545 make a better choice of file name for local symbols by ignoring
7546 file symbols appearing after a given local symbol. */
7547 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7548 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7552 state
= nothing_seen
;
7553 cache
->filename
= NULL
;
7555 cache
->func_size
= 0;
7556 cache
->last_section
= section
;
7558 for (p
= symbols
; *p
!= NULL
; p
++)
7564 if ((sym
->flags
& BSF_FILE
) != 0)
7567 if (state
== symbol_seen
)
7568 state
= file_after_symbol_seen
;
7572 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
7574 && code_off
<= offset
7575 && (code_off
> low_func
7576 || (code_off
== low_func
7577 && size
> cache
->func_size
)))
7580 cache
->func_size
= size
;
7581 cache
->filename
= NULL
;
7582 low_func
= code_off
;
7584 && ((sym
->flags
& BSF_LOCAL
) != 0
7585 || state
!= file_after_symbol_seen
))
7586 cache
->filename
= bfd_asymbol_name (file
);
7588 if (state
== nothing_seen
)
7589 state
= symbol_seen
;
7593 if (cache
->func
== NULL
)
7597 *filename_ptr
= cache
->filename
;
7598 if (functionname_ptr
)
7599 *functionname_ptr
= bfd_asymbol_name (cache
->func
);
7604 /* Find the nearest line to a particular section and offset,
7605 for error reporting. */
7608 _bfd_elf_find_nearest_line (bfd
*abfd
,
7612 const char **filename_ptr
,
7613 const char **functionname_ptr
,
7614 unsigned int *line_ptr
)
7616 return _bfd_elf_find_nearest_line_discriminator (abfd
, section
, symbols
,
7617 offset
, filename_ptr
,
7624 _bfd_elf_find_nearest_line_discriminator (bfd
*abfd
,
7628 const char **filename_ptr
,
7629 const char **functionname_ptr
,
7630 unsigned int *line_ptr
,
7631 unsigned int *discriminator_ptr
)
7635 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7636 filename_ptr
, functionname_ptr
,
7639 if (!*functionname_ptr
)
7640 elf_find_function (abfd
, section
, symbols
, offset
,
7641 *filename_ptr
? NULL
: filename_ptr
,
7647 if (_bfd_dwarf2_find_nearest_line (abfd
, dwarf_debug_sections
,
7648 section
, symbols
, offset
,
7649 filename_ptr
, functionname_ptr
,
7650 line_ptr
, discriminator_ptr
, 0,
7651 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7653 if (!*functionname_ptr
)
7654 elf_find_function (abfd
, section
, symbols
, offset
,
7655 *filename_ptr
? NULL
: filename_ptr
,
7661 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7662 &found
, filename_ptr
,
7663 functionname_ptr
, line_ptr
,
7664 &elf_tdata (abfd
)->line_info
))
7666 if (found
&& (*functionname_ptr
|| *line_ptr
))
7669 if (symbols
== NULL
)
7672 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7673 filename_ptr
, functionname_ptr
))
7680 /* Find the line for a symbol. */
7683 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7684 const char **filename_ptr
, unsigned int *line_ptr
)
7686 return _bfd_elf_find_line_discriminator (abfd
, symbols
, symbol
,
7687 filename_ptr
, line_ptr
,
7692 _bfd_elf_find_line_discriminator (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7693 const char **filename_ptr
,
7694 unsigned int *line_ptr
,
7695 unsigned int *discriminator_ptr
)
7697 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7698 filename_ptr
, line_ptr
, discriminator_ptr
, 0,
7699 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7702 /* After a call to bfd_find_nearest_line, successive calls to
7703 bfd_find_inliner_info can be used to get source information about
7704 each level of function inlining that terminated at the address
7705 passed to bfd_find_nearest_line. Currently this is only supported
7706 for DWARF2 with appropriate DWARF3 extensions. */
7709 _bfd_elf_find_inliner_info (bfd
*abfd
,
7710 const char **filename_ptr
,
7711 const char **functionname_ptr
,
7712 unsigned int *line_ptr
)
7715 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7716 functionname_ptr
, line_ptr
,
7717 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7722 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7724 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7725 int ret
= bed
->s
->sizeof_ehdr
;
7727 if (!info
->relocatable
)
7729 bfd_size_type phdr_size
= elf_program_header_size (abfd
);
7731 if (phdr_size
== (bfd_size_type
) -1)
7733 struct elf_segment_map
*m
;
7736 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
7737 phdr_size
+= bed
->s
->sizeof_phdr
;
7740 phdr_size
= get_program_header_size (abfd
, info
);
7743 elf_program_header_size (abfd
) = phdr_size
;
7751 _bfd_elf_set_section_contents (bfd
*abfd
,
7753 const void *location
,
7755 bfd_size_type count
)
7757 Elf_Internal_Shdr
*hdr
;
7760 if (! abfd
->output_has_begun
7761 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7764 hdr
= &elf_section_data (section
)->this_hdr
;
7765 pos
= hdr
->sh_offset
+ offset
;
7766 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7767 || bfd_bwrite (location
, count
, abfd
) != count
)
7774 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7775 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7776 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7781 /* Try to convert a non-ELF reloc into an ELF one. */
7784 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7786 /* Check whether we really have an ELF howto. */
7788 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7790 bfd_reloc_code_real_type code
;
7791 reloc_howto_type
*howto
;
7793 /* Alien reloc: Try to determine its type to replace it with an
7794 equivalent ELF reloc. */
7796 if (areloc
->howto
->pc_relative
)
7798 switch (areloc
->howto
->bitsize
)
7801 code
= BFD_RELOC_8_PCREL
;
7804 code
= BFD_RELOC_12_PCREL
;
7807 code
= BFD_RELOC_16_PCREL
;
7810 code
= BFD_RELOC_24_PCREL
;
7813 code
= BFD_RELOC_32_PCREL
;
7816 code
= BFD_RELOC_64_PCREL
;
7822 howto
= bfd_reloc_type_lookup (abfd
, code
);
7824 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7826 if (howto
->pcrel_offset
)
7827 areloc
->addend
+= areloc
->address
;
7829 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7834 switch (areloc
->howto
->bitsize
)
7840 code
= BFD_RELOC_14
;
7843 code
= BFD_RELOC_16
;
7846 code
= BFD_RELOC_26
;
7849 code
= BFD_RELOC_32
;
7852 code
= BFD_RELOC_64
;
7858 howto
= bfd_reloc_type_lookup (abfd
, code
);
7862 areloc
->howto
= howto
;
7870 (*_bfd_error_handler
)
7871 (_("%B: unsupported relocation type %s"),
7872 abfd
, areloc
->howto
->name
);
7873 bfd_set_error (bfd_error_bad_value
);
7878 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7880 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
7881 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
7883 if (elf_tdata (abfd
)->o
!= NULL
&& elf_shstrtab (abfd
) != NULL
)
7884 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7885 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
7888 return _bfd_generic_close_and_cleanup (abfd
);
7891 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7892 in the relocation's offset. Thus we cannot allow any sort of sanity
7893 range-checking to interfere. There is nothing else to do in processing
7896 bfd_reloc_status_type
7897 _bfd_elf_rel_vtable_reloc_fn
7898 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7899 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7900 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7901 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7903 return bfd_reloc_ok
;
7906 /* Elf core file support. Much of this only works on native
7907 toolchains, since we rely on knowing the
7908 machine-dependent procfs structure in order to pick
7909 out details about the corefile. */
7911 #ifdef HAVE_SYS_PROCFS_H
7912 /* Needed for new procfs interface on sparc-solaris. */
7913 # define _STRUCTURED_PROC 1
7914 # include <sys/procfs.h>
7917 /* Return a PID that identifies a "thread" for threaded cores, or the
7918 PID of the main process for non-threaded cores. */
7921 elfcore_make_pid (bfd
*abfd
)
7925 pid
= elf_tdata (abfd
)->core
->lwpid
;
7927 pid
= elf_tdata (abfd
)->core
->pid
;
7932 /* If there isn't a section called NAME, make one, using
7933 data from SECT. Note, this function will generate a
7934 reference to NAME, so you shouldn't deallocate or
7938 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7942 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7945 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7949 sect2
->size
= sect
->size
;
7950 sect2
->filepos
= sect
->filepos
;
7951 sect2
->alignment_power
= sect
->alignment_power
;
7955 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7956 actually creates up to two pseudosections:
7957 - For the single-threaded case, a section named NAME, unless
7958 such a section already exists.
7959 - For the multi-threaded case, a section named "NAME/PID", where
7960 PID is elfcore_make_pid (abfd).
7961 Both pseudosections have identical contents. */
7963 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7969 char *threaded_name
;
7973 /* Build the section name. */
7975 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7976 len
= strlen (buf
) + 1;
7977 threaded_name
= (char *) bfd_alloc (abfd
, len
);
7978 if (threaded_name
== NULL
)
7980 memcpy (threaded_name
, buf
, len
);
7982 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7987 sect
->filepos
= filepos
;
7988 sect
->alignment_power
= 2;
7990 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7993 /* prstatus_t exists on:
7995 linux 2.[01] + glibc
7999 #if defined (HAVE_PRSTATUS_T)
8002 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8007 if (note
->descsz
== sizeof (prstatus_t
))
8011 size
= sizeof (prstat
.pr_reg
);
8012 offset
= offsetof (prstatus_t
, pr_reg
);
8013 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8015 /* Do not overwrite the core signal if it
8016 has already been set by another thread. */
8017 if (elf_tdata (abfd
)->core
->signal
== 0)
8018 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8019 if (elf_tdata (abfd
)->core
->pid
== 0)
8020 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8022 /* pr_who exists on:
8025 pr_who doesn't exist on:
8028 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8029 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8031 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8034 #if defined (HAVE_PRSTATUS32_T)
8035 else if (note
->descsz
== sizeof (prstatus32_t
))
8037 /* 64-bit host, 32-bit corefile */
8038 prstatus32_t prstat
;
8040 size
= sizeof (prstat
.pr_reg
);
8041 offset
= offsetof (prstatus32_t
, pr_reg
);
8042 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8044 /* Do not overwrite the core signal if it
8045 has already been set by another thread. */
8046 if (elf_tdata (abfd
)->core
->signal
== 0)
8047 elf_tdata (abfd
)->core
->signal
= prstat
.pr_cursig
;
8048 if (elf_tdata (abfd
)->core
->pid
== 0)
8049 elf_tdata (abfd
)->core
->pid
= prstat
.pr_pid
;
8051 /* pr_who exists on:
8054 pr_who doesn't exist on:
8057 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8058 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_who
;
8060 elf_tdata (abfd
)->core
->lwpid
= prstat
.pr_pid
;
8063 #endif /* HAVE_PRSTATUS32_T */
8066 /* Fail - we don't know how to handle any other
8067 note size (ie. data object type). */
8071 /* Make a ".reg/999" section and a ".reg" section. */
8072 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8073 size
, note
->descpos
+ offset
);
8075 #endif /* defined (HAVE_PRSTATUS_T) */
8077 /* Create a pseudosection containing the exact contents of NOTE. */
8079 elfcore_make_note_pseudosection (bfd
*abfd
,
8081 Elf_Internal_Note
*note
)
8083 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8084 note
->descsz
, note
->descpos
);
8087 /* There isn't a consistent prfpregset_t across platforms,
8088 but it doesn't matter, because we don't have to pick this
8089 data structure apart. */
8092 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8094 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8097 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8098 type of NT_PRXFPREG. Just include the whole note's contents
8102 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8104 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8107 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8108 with a note type of NT_X86_XSTATE. Just include the whole note's
8109 contents literally. */
8112 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8114 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8118 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8120 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8124 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8126 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8130 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8132 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8136 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8138 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8142 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8144 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8148 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8150 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8154 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8156 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8160 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8162 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8166 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8168 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8172 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8174 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8178 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8180 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8184 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8186 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8190 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8192 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8196 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8198 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8201 #if defined (HAVE_PRPSINFO_T)
8202 typedef prpsinfo_t elfcore_psinfo_t
;
8203 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8204 typedef prpsinfo32_t elfcore_psinfo32_t
;
8208 #if defined (HAVE_PSINFO_T)
8209 typedef psinfo_t elfcore_psinfo_t
;
8210 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8211 typedef psinfo32_t elfcore_psinfo32_t
;
8215 /* return a malloc'ed copy of a string at START which is at
8216 most MAX bytes long, possibly without a terminating '\0'.
8217 the copy will always have a terminating '\0'. */
8220 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8223 char *end
= (char *) memchr (start
, '\0', max
);
8231 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8235 memcpy (dups
, start
, len
);
8241 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8243 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8245 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8247 elfcore_psinfo_t psinfo
;
8249 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8251 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8252 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8254 elf_tdata (abfd
)->core
->program
8255 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8256 sizeof (psinfo
.pr_fname
));
8258 elf_tdata (abfd
)->core
->command
8259 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8260 sizeof (psinfo
.pr_psargs
));
8262 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8263 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8265 /* 64-bit host, 32-bit corefile */
8266 elfcore_psinfo32_t psinfo
;
8268 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8270 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8271 elf_tdata (abfd
)->core
->pid
= psinfo
.pr_pid
;
8273 elf_tdata (abfd
)->core
->program
8274 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8275 sizeof (psinfo
.pr_fname
));
8277 elf_tdata (abfd
)->core
->command
8278 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8279 sizeof (psinfo
.pr_psargs
));
8285 /* Fail - we don't know how to handle any other
8286 note size (ie. data object type). */
8290 /* Note that for some reason, a spurious space is tacked
8291 onto the end of the args in some (at least one anyway)
8292 implementations, so strip it off if it exists. */
8295 char *command
= elf_tdata (abfd
)->core
->command
;
8296 int n
= strlen (command
);
8298 if (0 < n
&& command
[n
- 1] == ' ')
8299 command
[n
- 1] = '\0';
8304 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8306 #if defined (HAVE_PSTATUS_T)
8308 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8310 if (note
->descsz
== sizeof (pstatus_t
)
8311 #if defined (HAVE_PXSTATUS_T)
8312 || note
->descsz
== sizeof (pxstatus_t
)
8318 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8320 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8322 #if defined (HAVE_PSTATUS32_T)
8323 else if (note
->descsz
== sizeof (pstatus32_t
))
8325 /* 64-bit host, 32-bit corefile */
8328 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8330 elf_tdata (abfd
)->core
->pid
= pstat
.pr_pid
;
8333 /* Could grab some more details from the "representative"
8334 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8335 NT_LWPSTATUS note, presumably. */
8339 #endif /* defined (HAVE_PSTATUS_T) */
8341 #if defined (HAVE_LWPSTATUS_T)
8343 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8345 lwpstatus_t lwpstat
;
8351 if (note
->descsz
!= sizeof (lwpstat
)
8352 #if defined (HAVE_LWPXSTATUS_T)
8353 && note
->descsz
!= sizeof (lwpxstatus_t
)
8358 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8360 elf_tdata (abfd
)->core
->lwpid
= lwpstat
.pr_lwpid
;
8361 /* Do not overwrite the core signal if it has already been set by
8363 if (elf_tdata (abfd
)->core
->signal
== 0)
8364 elf_tdata (abfd
)->core
->signal
= lwpstat
.pr_cursig
;
8366 /* Make a ".reg/999" section. */
8368 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8369 len
= strlen (buf
) + 1;
8370 name
= bfd_alloc (abfd
, len
);
8373 memcpy (name
, buf
, len
);
8375 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8379 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8380 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8381 sect
->filepos
= note
->descpos
8382 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8385 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8386 sect
->size
= sizeof (lwpstat
.pr_reg
);
8387 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8390 sect
->alignment_power
= 2;
8392 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8395 /* Make a ".reg2/999" section */
8397 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8398 len
= strlen (buf
) + 1;
8399 name
= bfd_alloc (abfd
, len
);
8402 memcpy (name
, buf
, len
);
8404 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8408 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8409 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8410 sect
->filepos
= note
->descpos
8411 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8414 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8415 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8416 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8419 sect
->alignment_power
= 2;
8421 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8423 #endif /* defined (HAVE_LWPSTATUS_T) */
8426 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8433 int is_active_thread
;
8436 if (note
->descsz
< 728)
8439 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8442 type
= bfd_get_32 (abfd
, note
->descdata
);
8446 case 1 /* NOTE_INFO_PROCESS */:
8447 /* FIXME: need to add ->core->command. */
8448 /* process_info.pid */
8449 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8450 /* process_info.signal */
8451 elf_tdata (abfd
)->core
->signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8454 case 2 /* NOTE_INFO_THREAD */:
8455 /* Make a ".reg/999" section. */
8456 /* thread_info.tid */
8457 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8459 len
= strlen (buf
) + 1;
8460 name
= (char *) bfd_alloc (abfd
, len
);
8464 memcpy (name
, buf
, len
);
8466 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8470 /* sizeof (thread_info.thread_context) */
8472 /* offsetof (thread_info.thread_context) */
8473 sect
->filepos
= note
->descpos
+ 12;
8474 sect
->alignment_power
= 2;
8476 /* thread_info.is_active_thread */
8477 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8479 if (is_active_thread
)
8480 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8484 case 3 /* NOTE_INFO_MODULE */:
8485 /* Make a ".module/xxxxxxxx" section. */
8486 /* module_info.base_address */
8487 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8488 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8490 len
= strlen (buf
) + 1;
8491 name
= (char *) bfd_alloc (abfd
, len
);
8495 memcpy (name
, buf
, len
);
8497 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8502 sect
->size
= note
->descsz
;
8503 sect
->filepos
= note
->descpos
;
8504 sect
->alignment_power
= 2;
8515 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8517 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8525 if (bed
->elf_backend_grok_prstatus
)
8526 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8528 #if defined (HAVE_PRSTATUS_T)
8529 return elfcore_grok_prstatus (abfd
, note
);
8534 #if defined (HAVE_PSTATUS_T)
8536 return elfcore_grok_pstatus (abfd
, note
);
8539 #if defined (HAVE_LWPSTATUS_T)
8541 return elfcore_grok_lwpstatus (abfd
, note
);
8544 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8545 return elfcore_grok_prfpreg (abfd
, note
);
8547 case NT_WIN32PSTATUS
:
8548 return elfcore_grok_win32pstatus (abfd
, note
);
8550 case NT_PRXFPREG
: /* Linux SSE extension */
8551 if (note
->namesz
== 6
8552 && strcmp (note
->namedata
, "LINUX") == 0)
8553 return elfcore_grok_prxfpreg (abfd
, note
);
8557 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8558 if (note
->namesz
== 6
8559 && strcmp (note
->namedata
, "LINUX") == 0)
8560 return elfcore_grok_xstatereg (abfd
, note
);
8565 if (note
->namesz
== 6
8566 && strcmp (note
->namedata
, "LINUX") == 0)
8567 return elfcore_grok_ppc_vmx (abfd
, note
);
8572 if (note
->namesz
== 6
8573 && strcmp (note
->namedata
, "LINUX") == 0)
8574 return elfcore_grok_ppc_vsx (abfd
, note
);
8578 case NT_S390_HIGH_GPRS
:
8579 if (note
->namesz
== 6
8580 && strcmp (note
->namedata
, "LINUX") == 0)
8581 return elfcore_grok_s390_high_gprs (abfd
, note
);
8586 if (note
->namesz
== 6
8587 && strcmp (note
->namedata
, "LINUX") == 0)
8588 return elfcore_grok_s390_timer (abfd
, note
);
8592 case NT_S390_TODCMP
:
8593 if (note
->namesz
== 6
8594 && strcmp (note
->namedata
, "LINUX") == 0)
8595 return elfcore_grok_s390_todcmp (abfd
, note
);
8599 case NT_S390_TODPREG
:
8600 if (note
->namesz
== 6
8601 && strcmp (note
->namedata
, "LINUX") == 0)
8602 return elfcore_grok_s390_todpreg (abfd
, note
);
8607 if (note
->namesz
== 6
8608 && strcmp (note
->namedata
, "LINUX") == 0)
8609 return elfcore_grok_s390_ctrs (abfd
, note
);
8613 case NT_S390_PREFIX
:
8614 if (note
->namesz
== 6
8615 && strcmp (note
->namedata
, "LINUX") == 0)
8616 return elfcore_grok_s390_prefix (abfd
, note
);
8620 case NT_S390_LAST_BREAK
:
8621 if (note
->namesz
== 6
8622 && strcmp (note
->namedata
, "LINUX") == 0)
8623 return elfcore_grok_s390_last_break (abfd
, note
);
8627 case NT_S390_SYSTEM_CALL
:
8628 if (note
->namesz
== 6
8629 && strcmp (note
->namedata
, "LINUX") == 0)
8630 return elfcore_grok_s390_system_call (abfd
, note
);
8635 if (note
->namesz
== 6
8636 && strcmp (note
->namedata
, "LINUX") == 0)
8637 return elfcore_grok_arm_vfp (abfd
, note
);
8642 if (note
->namesz
== 6
8643 && strcmp (note
->namedata
, "LINUX") == 0)
8644 return elfcore_grok_aarch_tls (abfd
, note
);
8648 case NT_ARM_HW_BREAK
:
8649 if (note
->namesz
== 6
8650 && strcmp (note
->namedata
, "LINUX") == 0)
8651 return elfcore_grok_aarch_hw_break (abfd
, note
);
8655 case NT_ARM_HW_WATCH
:
8656 if (note
->namesz
== 6
8657 && strcmp (note
->namedata
, "LINUX") == 0)
8658 return elfcore_grok_aarch_hw_watch (abfd
, note
);
8664 if (bed
->elf_backend_grok_psinfo
)
8665 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8667 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8668 return elfcore_grok_psinfo (abfd
, note
);
8675 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8680 sect
->size
= note
->descsz
;
8681 sect
->filepos
= note
->descpos
;
8682 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8688 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
8692 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
8698 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8700 struct elf_obj_tdata
*t
;
8702 if (note
->descsz
== 0)
8705 t
= elf_tdata (abfd
);
8706 t
->build_id
= bfd_alloc (abfd
, sizeof (*t
->build_id
) - 1 + note
->descsz
);
8707 if (t
->build_id
== NULL
)
8710 t
->build_id
->size
= note
->descsz
;
8711 memcpy (t
->build_id
->data
, note
->descdata
, note
->descsz
);
8717 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8724 case NT_GNU_BUILD_ID
:
8725 return elfobj_grok_gnu_build_id (abfd
, note
);
8730 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
8732 struct sdt_note
*cur
=
8733 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
8736 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
8737 cur
->size
= (bfd_size_type
) note
->descsz
;
8738 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
8740 elf_tdata (abfd
)->sdt_note_head
= cur
;
8746 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8751 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
8759 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8763 cp
= strchr (note
->namedata
, '@');
8766 *lwpidp
= atoi(cp
+ 1);
8773 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8775 /* Signal number at offset 0x08. */
8776 elf_tdata (abfd
)->core
->signal
8777 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8779 /* Process ID at offset 0x50. */
8780 elf_tdata (abfd
)->core
->pid
8781 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8783 /* Command name at 0x7c (max 32 bytes, including nul). */
8784 elf_tdata (abfd
)->core
->command
8785 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8787 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8792 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8796 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8797 elf_tdata (abfd
)->core
->lwpid
= lwp
;
8799 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8801 /* NetBSD-specific core "procinfo". Note that we expect to
8802 find this note before any of the others, which is fine,
8803 since the kernel writes this note out first when it
8804 creates a core file. */
8806 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8809 /* As of Jan 2002 there are no other machine-independent notes
8810 defined for NetBSD core files. If the note type is less
8811 than the start of the machine-dependent note types, we don't
8814 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8818 switch (bfd_get_arch (abfd
))
8820 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8821 PT_GETFPREGS == mach+2. */
8823 case bfd_arch_alpha
:
8824 case bfd_arch_sparc
:
8827 case NT_NETBSDCORE_FIRSTMACH
+0:
8828 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8830 case NT_NETBSDCORE_FIRSTMACH
+2:
8831 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8837 /* On all other arch's, PT_GETREGS == mach+1 and
8838 PT_GETFPREGS == mach+3. */
8843 case NT_NETBSDCORE_FIRSTMACH
+1:
8844 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8846 case NT_NETBSDCORE_FIRSTMACH
+3:
8847 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8857 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8859 /* Signal number at offset 0x08. */
8860 elf_tdata (abfd
)->core
->signal
8861 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8863 /* Process ID at offset 0x20. */
8864 elf_tdata (abfd
)->core
->pid
8865 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
8867 /* Command name at 0x48 (max 32 bytes, including nul). */
8868 elf_tdata (abfd
)->core
->command
8869 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
8875 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8877 if (note
->type
== NT_OPENBSD_PROCINFO
)
8878 return elfcore_grok_openbsd_procinfo (abfd
, note
);
8880 if (note
->type
== NT_OPENBSD_REGS
)
8881 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8883 if (note
->type
== NT_OPENBSD_FPREGS
)
8884 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8886 if (note
->type
== NT_OPENBSD_XFPREGS
)
8887 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8889 if (note
->type
== NT_OPENBSD_AUXV
)
8891 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8896 sect
->size
= note
->descsz
;
8897 sect
->filepos
= note
->descpos
;
8898 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8903 if (note
->type
== NT_OPENBSD_WCOOKIE
)
8905 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
8910 sect
->size
= note
->descsz
;
8911 sect
->filepos
= note
->descpos
;
8912 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8921 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8923 void *ddata
= note
->descdata
;
8930 /* nto_procfs_status 'pid' field is at offset 0. */
8931 elf_tdata (abfd
)->core
->pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8933 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8934 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8936 /* nto_procfs_status 'flags' field is at offset 8. */
8937 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8939 /* nto_procfs_status 'what' field is at offset 14. */
8940 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8942 elf_tdata (abfd
)->core
->signal
= sig
;
8943 elf_tdata (abfd
)->core
->lwpid
= *tid
;
8946 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8947 do not come from signals so we make sure we set the current
8948 thread just in case. */
8949 if (flags
& 0x00000080)
8950 elf_tdata (abfd
)->core
->lwpid
= *tid
;
8952 /* Make a ".qnx_core_status/%d" section. */
8953 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8955 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8960 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8964 sect
->size
= note
->descsz
;
8965 sect
->filepos
= note
->descpos
;
8966 sect
->alignment_power
= 2;
8968 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8972 elfcore_grok_nto_regs (bfd
*abfd
,
8973 Elf_Internal_Note
*note
,
8981 /* Make a "(base)/%d" section. */
8982 sprintf (buf
, "%s/%ld", base
, tid
);
8984 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8989 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8993 sect
->size
= note
->descsz
;
8994 sect
->filepos
= note
->descpos
;
8995 sect
->alignment_power
= 2;
8997 /* This is the current thread. */
8998 if (elf_tdata (abfd
)->core
->lwpid
== tid
)
8999 return elfcore_maybe_make_sect (abfd
, base
, sect
);
9004 #define BFD_QNT_CORE_INFO 7
9005 #define BFD_QNT_CORE_STATUS 8
9006 #define BFD_QNT_CORE_GREG 9
9007 #define BFD_QNT_CORE_FPREG 10
9010 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9012 /* Every GREG section has a STATUS section before it. Store the
9013 tid from the previous call to pass down to the next gregs
9015 static long tid
= 1;
9019 case BFD_QNT_CORE_INFO
:
9020 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9021 case BFD_QNT_CORE_STATUS
:
9022 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9023 case BFD_QNT_CORE_GREG
:
9024 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9025 case BFD_QNT_CORE_FPREG
:
9026 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9033 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9039 /* Use note name as section name. */
9041 name
= (char *) bfd_alloc (abfd
, len
);
9044 memcpy (name
, note
->namedata
, len
);
9045 name
[len
- 1] = '\0';
9047 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9051 sect
->size
= note
->descsz
;
9052 sect
->filepos
= note
->descpos
;
9053 sect
->alignment_power
= 1;
9058 /* Function: elfcore_write_note
9061 buffer to hold note, and current size of buffer
9065 size of data for note
9067 Writes note to end of buffer. ELF64 notes are written exactly as
9068 for ELF32, despite the current (as of 2006) ELF gabi specifying
9069 that they ought to have 8-byte namesz and descsz field, and have
9070 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9073 Pointer to realloc'd buffer, *BUFSIZ updated. */
9076 elfcore_write_note (bfd
*abfd
,
9084 Elf_External_Note
*xnp
;
9091 namesz
= strlen (name
) + 1;
9093 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9095 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9098 dest
= buf
+ *bufsiz
;
9099 *bufsiz
+= newspace
;
9100 xnp
= (Elf_External_Note
*) dest
;
9101 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9102 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9103 H_PUT_32 (abfd
, type
, xnp
->type
);
9107 memcpy (dest
, name
, namesz
);
9115 memcpy (dest
, input
, size
);
9126 elfcore_write_prpsinfo (bfd
*abfd
,
9132 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9134 if (bed
->elf_backend_write_core_note
!= NULL
)
9137 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9138 NT_PRPSINFO
, fname
, psargs
);
9143 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9144 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9145 if (bed
->s
->elfclass
== ELFCLASS32
)
9147 #if defined (HAVE_PSINFO32_T)
9149 int note_type
= NT_PSINFO
;
9152 int note_type
= NT_PRPSINFO
;
9155 memset (&data
, 0, sizeof (data
));
9156 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9157 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9158 return elfcore_write_note (abfd
, buf
, bufsiz
,
9159 "CORE", note_type
, &data
, sizeof (data
));
9164 #if defined (HAVE_PSINFO_T)
9166 int note_type
= NT_PSINFO
;
9169 int note_type
= NT_PRPSINFO
;
9172 memset (&data
, 0, sizeof (data
));
9173 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9174 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9175 return elfcore_write_note (abfd
, buf
, bufsiz
,
9176 "CORE", note_type
, &data
, sizeof (data
));
9178 #endif /* PSINFO_T or PRPSINFO_T */
9185 elfcore_write_linux_prpsinfo32
9186 (bfd
*abfd
, char *buf
, int *bufsiz
,
9187 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9189 struct elf_external_linux_prpsinfo32 data
;
9191 memset (&data
, 0, sizeof (data
));
9192 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9194 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9195 &data
, sizeof (data
));
9199 elfcore_write_linux_prpsinfo64
9200 (bfd
*abfd
, char *buf
, int *bufsiz
,
9201 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9203 struct elf_external_linux_prpsinfo64 data
;
9205 memset (&data
, 0, sizeof (data
));
9206 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9208 return elfcore_write_note (abfd
, buf
, bufsiz
,
9209 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9213 elfcore_write_prstatus (bfd
*abfd
,
9220 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9222 if (bed
->elf_backend_write_core_note
!= NULL
)
9225 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9227 pid
, cursig
, gregs
);
9232 #if defined (HAVE_PRSTATUS_T)
9233 #if defined (HAVE_PRSTATUS32_T)
9234 if (bed
->s
->elfclass
== ELFCLASS32
)
9236 prstatus32_t prstat
;
9238 memset (&prstat
, 0, sizeof (prstat
));
9239 prstat
.pr_pid
= pid
;
9240 prstat
.pr_cursig
= cursig
;
9241 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9242 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9243 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9250 memset (&prstat
, 0, sizeof (prstat
));
9251 prstat
.pr_pid
= pid
;
9252 prstat
.pr_cursig
= cursig
;
9253 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9254 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9255 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9257 #endif /* HAVE_PRSTATUS_T */
9263 #if defined (HAVE_LWPSTATUS_T)
9265 elfcore_write_lwpstatus (bfd
*abfd
,
9272 lwpstatus_t lwpstat
;
9273 const char *note_name
= "CORE";
9275 memset (&lwpstat
, 0, sizeof (lwpstat
));
9276 lwpstat
.pr_lwpid
= pid
>> 16;
9277 lwpstat
.pr_cursig
= cursig
;
9278 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9279 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9280 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9282 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9283 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9285 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9286 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9289 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9290 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9292 #endif /* HAVE_LWPSTATUS_T */
9294 #if defined (HAVE_PSTATUS_T)
9296 elfcore_write_pstatus (bfd
*abfd
,
9300 int cursig ATTRIBUTE_UNUSED
,
9301 const void *gregs ATTRIBUTE_UNUSED
)
9303 const char *note_name
= "CORE";
9304 #if defined (HAVE_PSTATUS32_T)
9305 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9307 if (bed
->s
->elfclass
== ELFCLASS32
)
9311 memset (&pstat
, 0, sizeof (pstat
));
9312 pstat
.pr_pid
= pid
& 0xffff;
9313 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9314 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9322 memset (&pstat
, 0, sizeof (pstat
));
9323 pstat
.pr_pid
= pid
& 0xffff;
9324 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9325 NT_PSTATUS
, &pstat
, sizeof (pstat
));
9329 #endif /* HAVE_PSTATUS_T */
9332 elfcore_write_prfpreg (bfd
*abfd
,
9338 const char *note_name
= "CORE";
9339 return elfcore_write_note (abfd
, buf
, bufsiz
,
9340 note_name
, NT_FPREGSET
, fpregs
, size
);
9344 elfcore_write_prxfpreg (bfd
*abfd
,
9347 const void *xfpregs
,
9350 char *note_name
= "LINUX";
9351 return elfcore_write_note (abfd
, buf
, bufsiz
,
9352 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9356 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9357 const void *xfpregs
, int size
)
9359 char *note_name
= "LINUX";
9360 return elfcore_write_note (abfd
, buf
, bufsiz
,
9361 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9365 elfcore_write_ppc_vmx (bfd
*abfd
,
9368 const void *ppc_vmx
,
9371 char *note_name
= "LINUX";
9372 return elfcore_write_note (abfd
, buf
, bufsiz
,
9373 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9377 elfcore_write_ppc_vsx (bfd
*abfd
,
9380 const void *ppc_vsx
,
9383 char *note_name
= "LINUX";
9384 return elfcore_write_note (abfd
, buf
, bufsiz
,
9385 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9389 elfcore_write_s390_high_gprs (bfd
*abfd
,
9392 const void *s390_high_gprs
,
9395 char *note_name
= "LINUX";
9396 return elfcore_write_note (abfd
, buf
, bufsiz
,
9397 note_name
, NT_S390_HIGH_GPRS
,
9398 s390_high_gprs
, size
);
9402 elfcore_write_s390_timer (bfd
*abfd
,
9405 const void *s390_timer
,
9408 char *note_name
= "LINUX";
9409 return elfcore_write_note (abfd
, buf
, bufsiz
,
9410 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9414 elfcore_write_s390_todcmp (bfd
*abfd
,
9417 const void *s390_todcmp
,
9420 char *note_name
= "LINUX";
9421 return elfcore_write_note (abfd
, buf
, bufsiz
,
9422 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9426 elfcore_write_s390_todpreg (bfd
*abfd
,
9429 const void *s390_todpreg
,
9432 char *note_name
= "LINUX";
9433 return elfcore_write_note (abfd
, buf
, bufsiz
,
9434 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9438 elfcore_write_s390_ctrs (bfd
*abfd
,
9441 const void *s390_ctrs
,
9444 char *note_name
= "LINUX";
9445 return elfcore_write_note (abfd
, buf
, bufsiz
,
9446 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9450 elfcore_write_s390_prefix (bfd
*abfd
,
9453 const void *s390_prefix
,
9456 char *note_name
= "LINUX";
9457 return elfcore_write_note (abfd
, buf
, bufsiz
,
9458 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9462 elfcore_write_s390_last_break (bfd
*abfd
,
9465 const void *s390_last_break
,
9468 char *note_name
= "LINUX";
9469 return elfcore_write_note (abfd
, buf
, bufsiz
,
9470 note_name
, NT_S390_LAST_BREAK
,
9471 s390_last_break
, size
);
9475 elfcore_write_s390_system_call (bfd
*abfd
,
9478 const void *s390_system_call
,
9481 char *note_name
= "LINUX";
9482 return elfcore_write_note (abfd
, buf
, bufsiz
,
9483 note_name
, NT_S390_SYSTEM_CALL
,
9484 s390_system_call
, size
);
9488 elfcore_write_arm_vfp (bfd
*abfd
,
9491 const void *arm_vfp
,
9494 char *note_name
= "LINUX";
9495 return elfcore_write_note (abfd
, buf
, bufsiz
,
9496 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
9500 elfcore_write_aarch_tls (bfd
*abfd
,
9503 const void *aarch_tls
,
9506 char *note_name
= "LINUX";
9507 return elfcore_write_note (abfd
, buf
, bufsiz
,
9508 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
9512 elfcore_write_aarch_hw_break (bfd
*abfd
,
9515 const void *aarch_hw_break
,
9518 char *note_name
= "LINUX";
9519 return elfcore_write_note (abfd
, buf
, bufsiz
,
9520 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
9524 elfcore_write_aarch_hw_watch (bfd
*abfd
,
9527 const void *aarch_hw_watch
,
9530 char *note_name
= "LINUX";
9531 return elfcore_write_note (abfd
, buf
, bufsiz
,
9532 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
9536 elfcore_write_register_note (bfd
*abfd
,
9539 const char *section
,
9543 if (strcmp (section
, ".reg2") == 0)
9544 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9545 if (strcmp (section
, ".reg-xfp") == 0)
9546 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9547 if (strcmp (section
, ".reg-xstate") == 0)
9548 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9549 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9550 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9551 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9552 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9553 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9554 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9555 if (strcmp (section
, ".reg-s390-timer") == 0)
9556 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9557 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9558 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9559 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9560 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9561 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9562 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9563 if (strcmp (section
, ".reg-s390-prefix") == 0)
9564 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9565 if (strcmp (section
, ".reg-s390-last-break") == 0)
9566 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
9567 if (strcmp (section
, ".reg-s390-system-call") == 0)
9568 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
9569 if (strcmp (section
, ".reg-arm-vfp") == 0)
9570 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
9571 if (strcmp (section
, ".reg-aarch-tls") == 0)
9572 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
9573 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
9574 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
9575 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
9576 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
9581 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9586 while (p
< buf
+ size
)
9588 /* FIXME: bad alignment assumption. */
9589 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9590 Elf_Internal_Note in
;
9592 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9595 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9597 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9598 in
.namedata
= xnp
->name
;
9599 if (in
.namesz
> buf
- in
.namedata
+ size
)
9602 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9603 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9604 in
.descpos
= offset
+ (in
.descdata
- buf
);
9606 && (in
.descdata
>= buf
+ size
9607 || in
.descsz
> buf
- in
.descdata
+ size
))
9610 switch (bfd_get_format (abfd
))
9616 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
9618 if (! elfcore_grok_netbsd_note (abfd
, &in
))
9621 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
9623 if (! elfcore_grok_openbsd_note (abfd
, &in
))
9626 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
9628 if (! elfcore_grok_nto_note (abfd
, &in
))
9631 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
9633 if (! elfcore_grok_spu_note (abfd
, &in
))
9638 if (! elfcore_grok_note (abfd
, &in
))
9644 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9646 if (! elfobj_grok_gnu_note (abfd
, &in
))
9649 else if (in
.namesz
== sizeof "stapsdt"
9650 && strcmp (in
.namedata
, "stapsdt") == 0)
9652 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
9658 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9665 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9672 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9675 buf
= (char *) bfd_malloc (size
);
9679 if (bfd_bread (buf
, size
, abfd
) != size
9680 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9690 /* Providing external access to the ELF program header table. */
9692 /* Return an upper bound on the number of bytes required to store a
9693 copy of ABFD's program header table entries. Return -1 if an error
9694 occurs; bfd_get_error will return an appropriate code. */
9697 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9699 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9701 bfd_set_error (bfd_error_wrong_format
);
9705 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9708 /* Copy ABFD's program header table entries to *PHDRS. The entries
9709 will be stored as an array of Elf_Internal_Phdr structures, as
9710 defined in include/elf/internal.h. To find out how large the
9711 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9713 Return the number of program header table entries read, or -1 if an
9714 error occurs; bfd_get_error will return an appropriate code. */
9717 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9721 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9723 bfd_set_error (bfd_error_wrong_format
);
9727 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9728 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9729 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9734 enum elf_reloc_type_class
9735 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9737 return reloc_class_normal
;
9740 /* For RELA architectures, return the relocation value for a
9741 relocation against a local symbol. */
9744 _bfd_elf_rela_local_sym (bfd
*abfd
,
9745 Elf_Internal_Sym
*sym
,
9747 Elf_Internal_Rela
*rel
)
9749 asection
*sec
= *psec
;
9752 relocation
= (sec
->output_section
->vma
9753 + sec
->output_offset
9755 if ((sec
->flags
& SEC_MERGE
)
9756 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9757 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
9760 _bfd_merged_section_offset (abfd
, psec
,
9761 elf_section_data (sec
)->sec_info
,
9762 sym
->st_value
+ rel
->r_addend
);
9765 /* If we have changed the section, and our original section is
9766 marked with SEC_EXCLUDE, it means that the original
9767 SEC_MERGE section has been completely subsumed in some
9768 other SEC_MERGE section. In this case, we need to leave
9769 some info around for --emit-relocs. */
9770 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9771 sec
->kept_section
= *psec
;
9774 rel
->r_addend
-= relocation
;
9775 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9781 _bfd_elf_rel_local_sym (bfd
*abfd
,
9782 Elf_Internal_Sym
*sym
,
9786 asection
*sec
= *psec
;
9788 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
9789 return sym
->st_value
+ addend
;
9791 return _bfd_merged_section_offset (abfd
, psec
,
9792 elf_section_data (sec
)->sec_info
,
9793 sym
->st_value
+ addend
);
9797 _bfd_elf_section_offset (bfd
*abfd
,
9798 struct bfd_link_info
*info
,
9802 switch (sec
->sec_info_type
)
9804 case SEC_INFO_TYPE_STABS
:
9805 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
9807 case SEC_INFO_TYPE_EH_FRAME
:
9808 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
9810 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
9812 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9813 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
9814 offset
= sec
->size
- offset
- address_size
;
9820 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9821 reconstruct an ELF file by reading the segments out of remote memory
9822 based on the ELF file header at EHDR_VMA and the ELF program headers it
9823 points to. If not null, *LOADBASEP is filled in with the difference
9824 between the VMAs from which the segments were read, and the VMAs the
9825 file headers (and hence BFD's idea of each section's VMA) put them at.
9827 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9828 remote memory at target address VMA into the local buffer at MYADDR; it
9829 should return zero on success or an `errno' code on failure. TEMPL must
9830 be a BFD for an ELF target with the word size and byte order found in
9831 the remote memory. */
9834 bfd_elf_bfd_from_remote_memory
9838 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
9840 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
9841 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
9845 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
9846 long symcount ATTRIBUTE_UNUSED
,
9847 asymbol
**syms ATTRIBUTE_UNUSED
,
9852 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9855 const char *relplt_name
;
9856 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
9860 Elf_Internal_Shdr
*hdr
;
9866 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
9869 if (dynsymcount
<= 0)
9872 if (!bed
->plt_sym_val
)
9875 relplt_name
= bed
->relplt_name
;
9876 if (relplt_name
== NULL
)
9877 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
9878 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
9882 hdr
= &elf_section_data (relplt
)->this_hdr
;
9883 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
9884 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
9887 plt
= bfd_get_section_by_name (abfd
, ".plt");
9891 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9892 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
9895 count
= relplt
->size
/ hdr
->sh_entsize
;
9896 size
= count
* sizeof (asymbol
);
9897 p
= relplt
->relocation
;
9898 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9900 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
9904 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
9906 size
+= sizeof ("+0x") - 1 + 8;
9911 s
= *ret
= (asymbol
*) bfd_malloc (size
);
9915 names
= (char *) (s
+ count
);
9916 p
= relplt
->relocation
;
9918 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9923 addr
= bed
->plt_sym_val (i
, plt
, p
);
9924 if (addr
== (bfd_vma
) -1)
9927 *s
= **p
->sym_ptr_ptr
;
9928 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9929 we are defining a symbol, ensure one of them is set. */
9930 if ((s
->flags
& BSF_LOCAL
) == 0)
9931 s
->flags
|= BSF_GLOBAL
;
9932 s
->flags
|= BSF_SYNTHETIC
;
9934 s
->value
= addr
- plt
->vma
;
9937 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
9938 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
9944 memcpy (names
, "+0x", sizeof ("+0x") - 1);
9945 names
+= sizeof ("+0x") - 1;
9946 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
9947 for (a
= buf
; *a
== '0'; ++a
)
9950 memcpy (names
, a
, len
);
9953 memcpy (names
, "@plt", sizeof ("@plt"));
9954 names
+= sizeof ("@plt");
9961 /* It is only used by x86-64 so far. */
9962 asection _bfd_elf_large_com_section
9963 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
9964 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
9967 _bfd_elf_set_osabi (bfd
* abfd
,
9968 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
9970 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
9972 i_ehdrp
= elf_elfheader (abfd
);
9974 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
9976 /* To make things simpler for the loader on Linux systems we set the
9977 osabi field to ELFOSABI_GNU if the binary contains symbols of
9978 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
9979 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
9980 && elf_tdata (abfd
)->has_gnu_symbols
)
9981 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
9985 /* Return TRUE for ELF symbol types that represent functions.
9986 This is the default version of this function, which is sufficient for
9987 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9990 _bfd_elf_is_function_type (unsigned int type
)
9992 return (type
== STT_FUNC
9993 || type
== STT_GNU_IFUNC
);
9996 /* If the ELF symbol SYM might be a function in SEC, return the
9997 function size and set *CODE_OFF to the function's entry point,
9998 otherwise return zero. */
10001 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
10004 bfd_size_type size
;
10006 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
10007 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
10008 || sym
->section
!= sec
)
10011 *code_off
= sym
->value
;
10013 if (!(sym
->flags
& BSF_SYNTHETIC
))
10014 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;