1 /* ELF executable support for BFD.
3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
6 Free Software Foundation, Inc.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
30 BFD support for ELF formats is being worked on.
31 Currently, the best supported back ends are for sparc and i386
32 (running svr4 or Solaris 2).
34 Documentation of the internals of the support code still needs
35 to be written. The code is changing quickly enough that we
36 haven't bothered yet. */
38 /* For sparc64-cross-sparc32. */
46 #include "libiberty.h"
47 #include "safe-ctype.h"
48 #include "elf-linux-psinfo.h"
54 static int elf_sort_sections (const void *, const void *);
55 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
56 static bfd_boolean
prep_headers (bfd
*);
57 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
58 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
59 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
62 /* Swap version information in and out. The version information is
63 currently size independent. If that ever changes, this code will
64 need to move into elfcode.h. */
66 /* Swap in a Verdef structure. */
69 _bfd_elf_swap_verdef_in (bfd
*abfd
,
70 const Elf_External_Verdef
*src
,
71 Elf_Internal_Verdef
*dst
)
73 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
74 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
75 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
76 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
77 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
78 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
79 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
82 /* Swap out a Verdef structure. */
85 _bfd_elf_swap_verdef_out (bfd
*abfd
,
86 const Elf_Internal_Verdef
*src
,
87 Elf_External_Verdef
*dst
)
89 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
90 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
91 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
92 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
93 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
94 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
95 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
98 /* Swap in a Verdaux structure. */
101 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
102 const Elf_External_Verdaux
*src
,
103 Elf_Internal_Verdaux
*dst
)
105 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
106 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
109 /* Swap out a Verdaux structure. */
112 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
113 const Elf_Internal_Verdaux
*src
,
114 Elf_External_Verdaux
*dst
)
116 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
117 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
120 /* Swap in a Verneed structure. */
123 _bfd_elf_swap_verneed_in (bfd
*abfd
,
124 const Elf_External_Verneed
*src
,
125 Elf_Internal_Verneed
*dst
)
127 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
128 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
129 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
130 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
131 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
134 /* Swap out a Verneed structure. */
137 _bfd_elf_swap_verneed_out (bfd
*abfd
,
138 const Elf_Internal_Verneed
*src
,
139 Elf_External_Verneed
*dst
)
141 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
142 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
143 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
144 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
145 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
148 /* Swap in a Vernaux structure. */
151 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
152 const Elf_External_Vernaux
*src
,
153 Elf_Internal_Vernaux
*dst
)
155 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
156 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
157 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
158 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
159 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
162 /* Swap out a Vernaux structure. */
165 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
166 const Elf_Internal_Vernaux
*src
,
167 Elf_External_Vernaux
*dst
)
169 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
170 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
171 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
172 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
173 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
176 /* Swap in a Versym structure. */
179 _bfd_elf_swap_versym_in (bfd
*abfd
,
180 const Elf_External_Versym
*src
,
181 Elf_Internal_Versym
*dst
)
183 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
186 /* Swap out a Versym structure. */
189 _bfd_elf_swap_versym_out (bfd
*abfd
,
190 const Elf_Internal_Versym
*src
,
191 Elf_External_Versym
*dst
)
193 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
196 /* Standard ELF hash function. Do not change this function; you will
197 cause invalid hash tables to be generated. */
200 bfd_elf_hash (const char *namearg
)
202 const unsigned char *name
= (const unsigned char *) namearg
;
207 while ((ch
= *name
++) != '\0')
210 if ((g
= (h
& 0xf0000000)) != 0)
213 /* The ELF ABI says `h &= ~g', but this is equivalent in
214 this case and on some machines one insn instead of two. */
218 return h
& 0xffffffff;
221 /* DT_GNU_HASH hash function. Do not change this function; you will
222 cause invalid hash tables to be generated. */
225 bfd_elf_gnu_hash (const char *namearg
)
227 const unsigned char *name
= (const unsigned char *) namearg
;
228 unsigned long h
= 5381;
231 while ((ch
= *name
++) != '\0')
232 h
= (h
<< 5) + h
+ ch
;
233 return h
& 0xffffffff;
236 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
237 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
239 bfd_elf_allocate_object (bfd
*abfd
,
241 enum elf_target_id object_id
)
243 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
244 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
245 if (abfd
->tdata
.any
== NULL
)
248 elf_object_id (abfd
) = object_id
;
249 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
255 bfd_elf_make_object (bfd
*abfd
)
257 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
258 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
263 bfd_elf_mkcorefile (bfd
*abfd
)
265 /* I think this can be done just like an object file. */
266 return abfd
->xvec
->_bfd_set_format
[(int) bfd_object
] (abfd
);
270 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
272 Elf_Internal_Shdr
**i_shdrp
;
273 bfd_byte
*shstrtab
= NULL
;
275 bfd_size_type shstrtabsize
;
277 i_shdrp
= elf_elfsections (abfd
);
279 || shindex
>= elf_numsections (abfd
)
280 || i_shdrp
[shindex
] == 0)
283 shstrtab
= i_shdrp
[shindex
]->contents
;
284 if (shstrtab
== NULL
)
286 /* No cached one, attempt to read, and cache what we read. */
287 offset
= i_shdrp
[shindex
]->sh_offset
;
288 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
290 /* Allocate and clear an extra byte at the end, to prevent crashes
291 in case the string table is not terminated. */
292 if (shstrtabsize
+ 1 <= 1
293 || (shstrtab
= (bfd_byte
*) bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
294 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
296 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
298 if (bfd_get_error () != bfd_error_system_call
)
299 bfd_set_error (bfd_error_file_truncated
);
301 /* Once we've failed to read it, make sure we don't keep
302 trying. Otherwise, we'll keep allocating space for
303 the string table over and over. */
304 i_shdrp
[shindex
]->sh_size
= 0;
307 shstrtab
[shstrtabsize
] = '\0';
308 i_shdrp
[shindex
]->contents
= shstrtab
;
310 return (char *) shstrtab
;
314 bfd_elf_string_from_elf_section (bfd
*abfd
,
315 unsigned int shindex
,
316 unsigned int strindex
)
318 Elf_Internal_Shdr
*hdr
;
323 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
326 hdr
= elf_elfsections (abfd
)[shindex
];
328 if (hdr
->contents
== NULL
329 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
332 if (strindex
>= hdr
->sh_size
)
334 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
335 (*_bfd_error_handler
)
336 (_("%B: invalid string offset %u >= %lu for section `%s'"),
337 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
338 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
340 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
344 return ((char *) hdr
->contents
) + strindex
;
347 /* Read and convert symbols to internal format.
348 SYMCOUNT specifies the number of symbols to read, starting from
349 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
350 are non-NULL, they are used to store the internal symbols, external
351 symbols, and symbol section index extensions, respectively.
352 Returns a pointer to the internal symbol buffer (malloced if necessary)
353 or NULL if there were no symbols or some kind of problem. */
356 bfd_elf_get_elf_syms (bfd
*ibfd
,
357 Elf_Internal_Shdr
*symtab_hdr
,
360 Elf_Internal_Sym
*intsym_buf
,
362 Elf_External_Sym_Shndx
*extshndx_buf
)
364 Elf_Internal_Shdr
*shndx_hdr
;
366 const bfd_byte
*esym
;
367 Elf_External_Sym_Shndx
*alloc_extshndx
;
368 Elf_External_Sym_Shndx
*shndx
;
369 Elf_Internal_Sym
*alloc_intsym
;
370 Elf_Internal_Sym
*isym
;
371 Elf_Internal_Sym
*isymend
;
372 const struct elf_backend_data
*bed
;
377 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
383 /* Normal syms might have section extension entries. */
385 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
386 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
388 /* Read the symbols. */
390 alloc_extshndx
= NULL
;
392 bed
= get_elf_backend_data (ibfd
);
393 extsym_size
= bed
->s
->sizeof_sym
;
394 amt
= symcount
* extsym_size
;
395 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
396 if (extsym_buf
== NULL
)
398 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
399 extsym_buf
= alloc_ext
;
401 if (extsym_buf
== NULL
402 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
403 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
409 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
413 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
414 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
415 if (extshndx_buf
== NULL
)
417 alloc_extshndx
= (Elf_External_Sym_Shndx
*)
418 bfd_malloc2 (symcount
, sizeof (Elf_External_Sym_Shndx
));
419 extshndx_buf
= alloc_extshndx
;
421 if (extshndx_buf
== NULL
422 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
423 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
430 if (intsym_buf
== NULL
)
432 alloc_intsym
= (Elf_Internal_Sym
*)
433 bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
434 intsym_buf
= alloc_intsym
;
435 if (intsym_buf
== NULL
)
439 /* Convert the symbols to internal form. */
440 isymend
= intsym_buf
+ symcount
;
441 for (esym
= (const bfd_byte
*) extsym_buf
, isym
= intsym_buf
,
442 shndx
= extshndx_buf
;
444 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
445 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
447 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
448 (*_bfd_error_handler
) (_("%B symbol number %lu references "
449 "nonexistent SHT_SYMTAB_SHNDX section"),
450 ibfd
, (unsigned long) symoffset
);
451 if (alloc_intsym
!= NULL
)
458 if (alloc_ext
!= NULL
)
460 if (alloc_extshndx
!= NULL
)
461 free (alloc_extshndx
);
466 /* Look up a symbol name. */
468 bfd_elf_sym_name (bfd
*abfd
,
469 Elf_Internal_Shdr
*symtab_hdr
,
470 Elf_Internal_Sym
*isym
,
474 unsigned int iname
= isym
->st_name
;
475 unsigned int shindex
= symtab_hdr
->sh_link
;
477 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
478 /* Check for a bogus st_shndx to avoid crashing. */
479 && isym
->st_shndx
< elf_numsections (abfd
))
481 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
482 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
485 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
488 else if (sym_sec
&& *name
== '\0')
489 name
= bfd_section_name (abfd
, sym_sec
);
494 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
495 sections. The first element is the flags, the rest are section
498 typedef union elf_internal_group
{
499 Elf_Internal_Shdr
*shdr
;
501 } Elf_Internal_Group
;
503 /* Return the name of the group signature symbol. Why isn't the
504 signature just a string? */
507 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
509 Elf_Internal_Shdr
*hdr
;
510 unsigned char esym
[sizeof (Elf64_External_Sym
)];
511 Elf_External_Sym_Shndx eshndx
;
512 Elf_Internal_Sym isym
;
514 /* First we need to ensure the symbol table is available. Make sure
515 that it is a symbol table section. */
516 if (ghdr
->sh_link
>= elf_numsections (abfd
))
518 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
519 if (hdr
->sh_type
!= SHT_SYMTAB
520 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
523 /* Go read the symbol. */
524 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
525 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
526 &isym
, esym
, &eshndx
) == NULL
)
529 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
532 /* Set next_in_group list pointer, and group name for NEWSECT. */
535 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
537 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
539 /* If num_group is zero, read in all SHT_GROUP sections. The count
540 is set to -1 if there are no SHT_GROUP sections. */
543 unsigned int i
, shnum
;
545 /* First count the number of groups. If we have a SHT_GROUP
546 section with just a flag word (ie. sh_size is 4), ignore it. */
547 shnum
= elf_numsections (abfd
);
550 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \
551 ( (shdr)->sh_type == SHT_GROUP \
552 && (shdr)->sh_size >= minsize \
553 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
554 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
556 for (i
= 0; i
< shnum
; i
++)
558 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
560 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
566 num_group
= (unsigned) -1;
567 elf_tdata (abfd
)->num_group
= num_group
;
571 /* We keep a list of elf section headers for group sections,
572 so we can find them quickly. */
575 elf_tdata (abfd
)->num_group
= num_group
;
576 elf_tdata (abfd
)->group_sect_ptr
= (Elf_Internal_Shdr
**)
577 bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
578 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
582 for (i
= 0; i
< shnum
; i
++)
584 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
586 if (IS_VALID_GROUP_SECTION_HEADER (shdr
, 2 * GRP_ENTRY_SIZE
))
589 Elf_Internal_Group
*dest
;
591 /* Add to list of sections. */
592 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
595 /* Read the raw contents. */
596 BFD_ASSERT (sizeof (*dest
) >= 4);
597 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
598 shdr
->contents
= (unsigned char *)
599 bfd_alloc2 (abfd
, shdr
->sh_size
, sizeof (*dest
) / 4);
600 /* PR binutils/4110: Handle corrupt group headers. */
601 if (shdr
->contents
== NULL
)
604 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
605 bfd_set_error (bfd_error_bad_value
);
609 memset (shdr
->contents
, 0, amt
);
611 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
612 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
616 /* Translate raw contents, a flag word followed by an
617 array of elf section indices all in target byte order,
618 to the flag word followed by an array of elf section
620 src
= shdr
->contents
+ shdr
->sh_size
;
621 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
628 idx
= H_GET_32 (abfd
, src
);
629 if (src
== shdr
->contents
)
632 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
633 shdr
->bfd_section
->flags
634 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
639 ((*_bfd_error_handler
)
640 (_("%B: invalid SHT_GROUP entry"), abfd
));
643 dest
->shdr
= elf_elfsections (abfd
)[idx
];
650 if (num_group
!= (unsigned) -1)
654 for (i
= 0; i
< num_group
; i
++)
656 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
657 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
658 unsigned int n_elt
= shdr
->sh_size
/ 4;
660 /* Look through this group's sections to see if current
661 section is a member. */
663 if ((++idx
)->shdr
== hdr
)
667 /* We are a member of this group. Go looking through
668 other members to see if any others are linked via
670 idx
= (Elf_Internal_Group
*) shdr
->contents
;
671 n_elt
= shdr
->sh_size
/ 4;
673 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
674 && elf_next_in_group (s
) != NULL
)
678 /* Snarf the group name from other member, and
679 insert current section in circular list. */
680 elf_group_name (newsect
) = elf_group_name (s
);
681 elf_next_in_group (newsect
) = elf_next_in_group (s
);
682 elf_next_in_group (s
) = newsect
;
688 gname
= group_signature (abfd
, shdr
);
691 elf_group_name (newsect
) = gname
;
693 /* Start a circular list with one element. */
694 elf_next_in_group (newsect
) = newsect
;
697 /* If the group section has been created, point to the
699 if (shdr
->bfd_section
!= NULL
)
700 elf_next_in_group (shdr
->bfd_section
) = newsect
;
708 if (elf_group_name (newsect
) == NULL
)
710 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
717 _bfd_elf_setup_sections (bfd
*abfd
)
720 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
721 bfd_boolean result
= TRUE
;
724 /* Process SHF_LINK_ORDER. */
725 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
727 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
728 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
730 unsigned int elfsec
= this_hdr
->sh_link
;
731 /* FIXME: The old Intel compiler and old strip/objcopy may
732 not set the sh_link or sh_info fields. Hence we could
733 get the situation where elfsec is 0. */
736 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
737 if (bed
->link_order_error_handler
)
738 bed
->link_order_error_handler
739 (_("%B: warning: sh_link not set for section `%A'"),
744 asection
*linksec
= NULL
;
746 if (elfsec
< elf_numsections (abfd
))
748 this_hdr
= elf_elfsections (abfd
)[elfsec
];
749 linksec
= this_hdr
->bfd_section
;
753 Some strip/objcopy may leave an incorrect value in
754 sh_link. We don't want to proceed. */
757 (*_bfd_error_handler
)
758 (_("%B: sh_link [%d] in section `%A' is incorrect"),
759 s
->owner
, s
, elfsec
);
763 elf_linked_to_section (s
) = linksec
;
768 /* Process section groups. */
769 if (num_group
== (unsigned) -1)
772 for (i
= 0; i
< num_group
; i
++)
774 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
775 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
776 unsigned int n_elt
= shdr
->sh_size
/ 4;
779 if ((++idx
)->shdr
->bfd_section
)
780 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
781 else if (idx
->shdr
->sh_type
== SHT_RELA
782 || idx
->shdr
->sh_type
== SHT_REL
)
783 /* We won't include relocation sections in section groups in
784 output object files. We adjust the group section size here
785 so that relocatable link will work correctly when
786 relocation sections are in section group in input object
788 shdr
->bfd_section
->size
-= 4;
791 /* There are some unknown sections in the group. */
792 (*_bfd_error_handler
)
793 (_("%B: unknown [%d] section `%s' in group [%s]"),
795 (unsigned int) idx
->shdr
->sh_type
,
796 bfd_elf_string_from_elf_section (abfd
,
797 (elf_elfheader (abfd
)
800 shdr
->bfd_section
->name
);
808 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
810 return elf_next_in_group (sec
) != NULL
;
813 /* Make a BFD section from an ELF section. We store a pointer to the
814 BFD section in the bfd_section field of the header. */
817 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
818 Elf_Internal_Shdr
*hdr
,
824 const struct elf_backend_data
*bed
;
826 if (hdr
->bfd_section
!= NULL
)
829 newsect
= bfd_make_section_anyway (abfd
, name
);
833 hdr
->bfd_section
= newsect
;
834 elf_section_data (newsect
)->this_hdr
= *hdr
;
835 elf_section_data (newsect
)->this_idx
= shindex
;
837 /* Always use the real type/flags. */
838 elf_section_type (newsect
) = hdr
->sh_type
;
839 elf_section_flags (newsect
) = hdr
->sh_flags
;
841 newsect
->filepos
= hdr
->sh_offset
;
843 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
844 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
845 || ! bfd_set_section_alignment (abfd
, newsect
,
846 bfd_log2 (hdr
->sh_addralign
)))
849 flags
= SEC_NO_FLAGS
;
850 if (hdr
->sh_type
!= SHT_NOBITS
)
851 flags
|= SEC_HAS_CONTENTS
;
852 if (hdr
->sh_type
== SHT_GROUP
)
853 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
854 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
857 if (hdr
->sh_type
!= SHT_NOBITS
)
860 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
861 flags
|= SEC_READONLY
;
862 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
864 else if ((flags
& SEC_LOAD
) != 0)
866 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
869 newsect
->entsize
= hdr
->sh_entsize
;
870 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
871 flags
|= SEC_STRINGS
;
873 if (hdr
->sh_flags
& SHF_GROUP
)
874 if (!setup_group (abfd
, hdr
, newsect
))
876 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
877 flags
|= SEC_THREAD_LOCAL
;
878 if ((hdr
->sh_flags
& SHF_EXCLUDE
) != 0)
879 flags
|= SEC_EXCLUDE
;
881 if ((flags
& SEC_ALLOC
) == 0)
883 /* The debugging sections appear to be recognized only by name,
884 not any sort of flag. Their SEC_ALLOC bits are cleared. */
891 else if (name
[1] == 'g' && name
[2] == 'n')
892 p
= ".gnu.linkonce.wi.", n
= 17;
893 else if (name
[1] == 'g' && name
[2] == 'd')
894 p
= ".gdb_index", n
= 11; /* yes we really do mean 11. */
895 else if (name
[1] == 'l')
897 else if (name
[1] == 's')
899 else if (name
[1] == 'z')
900 p
= ".zdebug", n
= 7;
903 if (p
!= NULL
&& strncmp (name
, p
, n
) == 0)
904 flags
|= SEC_DEBUGGING
;
908 /* As a GNU extension, if the name begins with .gnu.linkonce, we
909 only link a single copy of the section. This is used to support
910 g++. g++ will emit each template expansion in its own section.
911 The symbols will be defined as weak, so that multiple definitions
912 are permitted. The GNU linker extension is to actually discard
913 all but one of the sections. */
914 if (CONST_STRNEQ (name
, ".gnu.linkonce")
915 && elf_next_in_group (newsect
) == NULL
)
916 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
918 bed
= get_elf_backend_data (abfd
);
919 if (bed
->elf_backend_section_flags
)
920 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
923 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
926 /* We do not parse the PT_NOTE segments as we are interested even in the
927 separate debug info files which may have the segments offsets corrupted.
928 PT_NOTEs from the core files are currently not parsed using BFD. */
929 if (hdr
->sh_type
== SHT_NOTE
)
933 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
936 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
940 if ((flags
& SEC_ALLOC
) != 0)
942 Elf_Internal_Phdr
*phdr
;
943 unsigned int i
, nload
;
945 /* Some ELF linkers produce binaries with all the program header
946 p_paddr fields zero. If we have such a binary with more than
947 one PT_LOAD header, then leave the section lma equal to vma
948 so that we don't create sections with overlapping lma. */
949 phdr
= elf_tdata (abfd
)->phdr
;
950 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
951 if (phdr
->p_paddr
!= 0)
953 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
955 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
958 phdr
= elf_tdata (abfd
)->phdr
;
959 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
961 if (((phdr
->p_type
== PT_LOAD
962 && (hdr
->sh_flags
& SHF_TLS
) == 0)
963 || phdr
->p_type
== PT_TLS
)
964 && ELF_SECTION_IN_SEGMENT (hdr
, phdr
))
966 if ((flags
& SEC_LOAD
) == 0)
967 newsect
->lma
= (phdr
->p_paddr
968 + hdr
->sh_addr
- phdr
->p_vaddr
);
970 /* We used to use the same adjustment for SEC_LOAD
971 sections, but that doesn't work if the segment
972 is packed with code from multiple VMAs.
973 Instead we calculate the section LMA based on
974 the segment LMA. It is assumed that the
975 segment will contain sections with contiguous
976 LMAs, even if the VMAs are not. */
977 newsect
->lma
= (phdr
->p_paddr
978 + hdr
->sh_offset
- phdr
->p_offset
);
980 /* With contiguous segments, we can't tell from file
981 offsets whether a section with zero size should
982 be placed at the end of one segment or the
983 beginning of the next. Decide based on vaddr. */
984 if (hdr
->sh_addr
>= phdr
->p_vaddr
985 && (hdr
->sh_addr
+ hdr
->sh_size
986 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
992 /* Compress/decompress DWARF debug sections with names: .debug_* and
993 .zdebug_*, after the section flags is set. */
994 if ((flags
& SEC_DEBUGGING
)
995 && ((name
[1] == 'd' && name
[6] == '_')
996 || (name
[1] == 'z' && name
[7] == '_')))
998 enum { nothing
, compress
, decompress
} action
= nothing
;
1001 if (bfd_is_section_compressed (abfd
, newsect
))
1003 /* Compressed section. Check if we should decompress. */
1004 if ((abfd
->flags
& BFD_DECOMPRESS
))
1005 action
= decompress
;
1009 /* Normal section. Check if we should compress. */
1010 if ((abfd
->flags
& BFD_COMPRESS
) && newsect
->size
!= 0)
1020 if (!bfd_init_section_compress_status (abfd
, newsect
))
1022 (*_bfd_error_handler
)
1023 (_("%B: unable to initialize compress status for section %s"),
1029 unsigned int len
= strlen (name
);
1031 new_name
= bfd_alloc (abfd
, len
+ 2);
1032 if (new_name
== NULL
)
1036 memcpy (new_name
+ 2, name
+ 1, len
);
1040 if (!bfd_init_section_decompress_status (abfd
, newsect
))
1042 (*_bfd_error_handler
)
1043 (_("%B: unable to initialize decompress status for section %s"),
1049 unsigned int len
= strlen (name
);
1051 new_name
= bfd_alloc (abfd
, len
);
1052 if (new_name
== NULL
)
1055 memcpy (new_name
+ 1, name
+ 2, len
- 1);
1059 if (new_name
!= NULL
)
1060 bfd_rename_section (abfd
, newsect
, new_name
);
1066 const char *const bfd_elf_section_type_names
[] = {
1067 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1068 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1069 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1072 /* ELF relocs are against symbols. If we are producing relocatable
1073 output, and the reloc is against an external symbol, and nothing
1074 has given us any additional addend, the resulting reloc will also
1075 be against the same symbol. In such a case, we don't want to
1076 change anything about the way the reloc is handled, since it will
1077 all be done at final link time. Rather than put special case code
1078 into bfd_perform_relocation, all the reloc types use this howto
1079 function. It just short circuits the reloc if producing
1080 relocatable output against an external symbol. */
1082 bfd_reloc_status_type
1083 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1084 arelent
*reloc_entry
,
1086 void *data ATTRIBUTE_UNUSED
,
1087 asection
*input_section
,
1089 char **error_message ATTRIBUTE_UNUSED
)
1091 if (output_bfd
!= NULL
1092 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1093 && (! reloc_entry
->howto
->partial_inplace
1094 || reloc_entry
->addend
== 0))
1096 reloc_entry
->address
+= input_section
->output_offset
;
1097 return bfd_reloc_ok
;
1100 return bfd_reloc_continue
;
1103 /* Copy the program header and other data from one object module to
1107 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1109 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1110 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1113 BFD_ASSERT (!elf_flags_init (obfd
)
1114 || (elf_elfheader (obfd
)->e_flags
1115 == elf_elfheader (ibfd
)->e_flags
));
1117 elf_gp (obfd
) = elf_gp (ibfd
);
1118 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1119 elf_flags_init (obfd
) = TRUE
;
1121 /* Copy object attributes. */
1122 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1127 get_segment_type (unsigned int p_type
)
1132 case PT_NULL
: pt
= "NULL"; break;
1133 case PT_LOAD
: pt
= "LOAD"; break;
1134 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1135 case PT_INTERP
: pt
= "INTERP"; break;
1136 case PT_NOTE
: pt
= "NOTE"; break;
1137 case PT_SHLIB
: pt
= "SHLIB"; break;
1138 case PT_PHDR
: pt
= "PHDR"; break;
1139 case PT_TLS
: pt
= "TLS"; break;
1140 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1141 case PT_GNU_STACK
: pt
= "STACK"; break;
1142 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1143 default: pt
= NULL
; break;
1148 /* Print out the program headers. */
1151 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1153 FILE *f
= (FILE *) farg
;
1154 Elf_Internal_Phdr
*p
;
1156 bfd_byte
*dynbuf
= NULL
;
1158 p
= elf_tdata (abfd
)->phdr
;
1163 fprintf (f
, _("\nProgram Header:\n"));
1164 c
= elf_elfheader (abfd
)->e_phnum
;
1165 for (i
= 0; i
< c
; i
++, p
++)
1167 const char *pt
= get_segment_type (p
->p_type
);
1172 sprintf (buf
, "0x%lx", p
->p_type
);
1175 fprintf (f
, "%8s off 0x", pt
);
1176 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1177 fprintf (f
, " vaddr 0x");
1178 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1179 fprintf (f
, " paddr 0x");
1180 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1181 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1182 fprintf (f
, " filesz 0x");
1183 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1184 fprintf (f
, " memsz 0x");
1185 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1186 fprintf (f
, " flags %c%c%c",
1187 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1188 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1189 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1190 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1191 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1196 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1199 unsigned int elfsec
;
1200 unsigned long shlink
;
1201 bfd_byte
*extdyn
, *extdynend
;
1203 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1205 fprintf (f
, _("\nDynamic Section:\n"));
1207 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1210 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1211 if (elfsec
== SHN_BAD
)
1213 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1215 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1216 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1219 extdynend
= extdyn
+ s
->size
;
1220 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1222 Elf_Internal_Dyn dyn
;
1223 const char *name
= "";
1225 bfd_boolean stringp
;
1226 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1228 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1230 if (dyn
.d_tag
== DT_NULL
)
1237 if (bed
->elf_backend_get_target_dtag
)
1238 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1240 if (!strcmp (name
, ""))
1242 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1247 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1248 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1249 case DT_PLTGOT
: name
= "PLTGOT"; break;
1250 case DT_HASH
: name
= "HASH"; break;
1251 case DT_STRTAB
: name
= "STRTAB"; break;
1252 case DT_SYMTAB
: name
= "SYMTAB"; break;
1253 case DT_RELA
: name
= "RELA"; break;
1254 case DT_RELASZ
: name
= "RELASZ"; break;
1255 case DT_RELAENT
: name
= "RELAENT"; break;
1256 case DT_STRSZ
: name
= "STRSZ"; break;
1257 case DT_SYMENT
: name
= "SYMENT"; break;
1258 case DT_INIT
: name
= "INIT"; break;
1259 case DT_FINI
: name
= "FINI"; break;
1260 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1261 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1262 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1263 case DT_REL
: name
= "REL"; break;
1264 case DT_RELSZ
: name
= "RELSZ"; break;
1265 case DT_RELENT
: name
= "RELENT"; break;
1266 case DT_PLTREL
: name
= "PLTREL"; break;
1267 case DT_DEBUG
: name
= "DEBUG"; break;
1268 case DT_TEXTREL
: name
= "TEXTREL"; break;
1269 case DT_JMPREL
: name
= "JMPREL"; break;
1270 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1271 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1272 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1273 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1274 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1275 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1276 case DT_FLAGS
: name
= "FLAGS"; break;
1277 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1278 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1279 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1280 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1281 case DT_MOVEENT
: name
= "MOVEENT"; break;
1282 case DT_MOVESZ
: name
= "MOVESZ"; break;
1283 case DT_FEATURE
: name
= "FEATURE"; break;
1284 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1285 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1286 case DT_SYMINENT
: name
= "SYMINENT"; break;
1287 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1288 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1289 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1290 case DT_PLTPAD
: name
= "PLTPAD"; break;
1291 case DT_MOVETAB
: name
= "MOVETAB"; break;
1292 case DT_SYMINFO
: name
= "SYMINFO"; break;
1293 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1294 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1295 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1296 case DT_VERSYM
: name
= "VERSYM"; break;
1297 case DT_VERDEF
: name
= "VERDEF"; break;
1298 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1299 case DT_VERNEED
: name
= "VERNEED"; break;
1300 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1301 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1302 case DT_USED
: name
= "USED"; break;
1303 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1304 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1307 fprintf (f
, " %-20s ", name
);
1311 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1316 unsigned int tagv
= dyn
.d_un
.d_val
;
1318 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1321 fprintf (f
, "%s", string
);
1330 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1331 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1333 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1337 if (elf_dynverdef (abfd
) != 0)
1339 Elf_Internal_Verdef
*t
;
1341 fprintf (f
, _("\nVersion definitions:\n"));
1342 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1344 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1345 t
->vd_flags
, t
->vd_hash
,
1346 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1347 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1349 Elf_Internal_Verdaux
*a
;
1352 for (a
= t
->vd_auxptr
->vda_nextptr
;
1356 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1362 if (elf_dynverref (abfd
) != 0)
1364 Elf_Internal_Verneed
*t
;
1366 fprintf (f
, _("\nVersion References:\n"));
1367 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1369 Elf_Internal_Vernaux
*a
;
1371 fprintf (f
, _(" required from %s:\n"),
1372 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1373 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1374 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1375 a
->vna_flags
, a
->vna_other
,
1376 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1388 /* Display ELF-specific fields of a symbol. */
1391 bfd_elf_print_symbol (bfd
*abfd
,
1394 bfd_print_symbol_type how
)
1396 FILE *file
= (FILE *) filep
;
1399 case bfd_print_symbol_name
:
1400 fprintf (file
, "%s", symbol
->name
);
1402 case bfd_print_symbol_more
:
1403 fprintf (file
, "elf ");
1404 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1405 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1407 case bfd_print_symbol_all
:
1409 const char *section_name
;
1410 const char *name
= NULL
;
1411 const struct elf_backend_data
*bed
;
1412 unsigned char st_other
;
1415 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1417 bed
= get_elf_backend_data (abfd
);
1418 if (bed
->elf_backend_print_symbol_all
)
1419 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1423 name
= symbol
->name
;
1424 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1427 fprintf (file
, " %s\t", section_name
);
1428 /* Print the "other" value for a symbol. For common symbols,
1429 we've already printed the size; now print the alignment.
1430 For other symbols, we have no specified alignment, and
1431 we've printed the address; now print the size. */
1432 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1433 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1435 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1436 bfd_fprintf_vma (abfd
, file
, val
);
1438 /* If we have version information, print it. */
1439 if (elf_tdata (abfd
)->dynversym_section
!= 0
1440 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1441 || elf_tdata (abfd
)->dynverref_section
!= 0))
1443 unsigned int vernum
;
1444 const char *version_string
;
1446 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1449 version_string
= "";
1450 else if (vernum
== 1)
1451 version_string
= "Base";
1452 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1454 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1457 Elf_Internal_Verneed
*t
;
1459 version_string
= "";
1460 for (t
= elf_tdata (abfd
)->verref
;
1464 Elf_Internal_Vernaux
*a
;
1466 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1468 if (a
->vna_other
== vernum
)
1470 version_string
= a
->vna_nodename
;
1477 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1478 fprintf (file
, " %-11s", version_string
);
1483 fprintf (file
, " (%s)", version_string
);
1484 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1489 /* If the st_other field is not zero, print it. */
1490 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1495 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1496 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1497 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1499 /* Some other non-defined flags are also present, so print
1501 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1504 fprintf (file
, " %s", name
);
1510 /* Allocate an ELF string table--force the first byte to be zero. */
1512 struct bfd_strtab_hash
*
1513 _bfd_elf_stringtab_init (void)
1515 struct bfd_strtab_hash
*ret
;
1517 ret
= _bfd_stringtab_init ();
1522 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1523 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1524 if (loc
== (bfd_size_type
) -1)
1526 _bfd_stringtab_free (ret
);
1533 /* ELF .o/exec file reading */
1535 /* Create a new bfd section from an ELF section header. */
1538 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1540 Elf_Internal_Shdr
*hdr
;
1541 Elf_Internal_Ehdr
*ehdr
;
1542 const struct elf_backend_data
*bed
;
1545 if (shindex
>= elf_numsections (abfd
))
1548 hdr
= elf_elfsections (abfd
)[shindex
];
1549 ehdr
= elf_elfheader (abfd
);
1550 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1555 bed
= get_elf_backend_data (abfd
);
1556 switch (hdr
->sh_type
)
1559 /* Inactive section. Throw it away. */
1562 case SHT_PROGBITS
: /* Normal section with contents. */
1563 case SHT_NOBITS
: /* .bss section. */
1564 case SHT_HASH
: /* .hash section. */
1565 case SHT_NOTE
: /* .note section. */
1566 case SHT_INIT_ARRAY
: /* .init_array section. */
1567 case SHT_FINI_ARRAY
: /* .fini_array section. */
1568 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1569 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1570 case SHT_GNU_HASH
: /* .gnu.hash section. */
1571 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1573 case SHT_DYNAMIC
: /* Dynamic linking information. */
1574 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1576 if (hdr
->sh_link
> elf_numsections (abfd
))
1578 /* PR 10478: Accept Solaris binaries with a sh_link
1579 field set to SHN_BEFORE or SHN_AFTER. */
1580 switch (bfd_get_arch (abfd
))
1583 case bfd_arch_sparc
:
1584 if (hdr
->sh_link
== (SHN_LORESERVE
& 0xffff) /* SHN_BEFORE */
1585 || hdr
->sh_link
== ((SHN_LORESERVE
+ 1) & 0xffff) /* SHN_AFTER */)
1587 /* Otherwise fall through. */
1592 else if (elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1594 else if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1596 Elf_Internal_Shdr
*dynsymhdr
;
1598 /* The shared libraries distributed with hpux11 have a bogus
1599 sh_link field for the ".dynamic" section. Find the
1600 string table for the ".dynsym" section instead. */
1601 if (elf_dynsymtab (abfd
) != 0)
1603 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1604 hdr
->sh_link
= dynsymhdr
->sh_link
;
1608 unsigned int i
, num_sec
;
1610 num_sec
= elf_numsections (abfd
);
1611 for (i
= 1; i
< num_sec
; i
++)
1613 dynsymhdr
= elf_elfsections (abfd
)[i
];
1614 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1616 hdr
->sh_link
= dynsymhdr
->sh_link
;
1624 case SHT_SYMTAB
: /* A symbol table */
1625 if (elf_onesymtab (abfd
) == shindex
)
1628 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1630 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1632 if (hdr
->sh_size
!= 0)
1634 /* Some assemblers erroneously set sh_info to one with a
1635 zero sh_size. ld sees this as a global symbol count
1636 of (unsigned) -1. Fix it here. */
1640 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1641 elf_onesymtab (abfd
) = shindex
;
1642 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1643 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1644 abfd
->flags
|= HAS_SYMS
;
1646 /* Sometimes a shared object will map in the symbol table. If
1647 SHF_ALLOC is set, and this is a shared object, then we also
1648 treat this section as a BFD section. We can not base the
1649 decision purely on SHF_ALLOC, because that flag is sometimes
1650 set in a relocatable object file, which would confuse the
1652 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1653 && (abfd
->flags
& DYNAMIC
) != 0
1654 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1658 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1659 can't read symbols without that section loaded as well. It
1660 is most likely specified by the next section header. */
1661 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1663 unsigned int i
, num_sec
;
1665 num_sec
= elf_numsections (abfd
);
1666 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1668 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1669 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1670 && hdr2
->sh_link
== shindex
)
1674 for (i
= 1; i
< shindex
; i
++)
1676 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1677 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1678 && hdr2
->sh_link
== shindex
)
1682 return bfd_section_from_shdr (abfd
, i
);
1686 case SHT_DYNSYM
: /* A dynamic symbol table */
1687 if (elf_dynsymtab (abfd
) == shindex
)
1690 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1692 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1694 if (hdr
->sh_size
!= 0)
1696 /* Some linkers erroneously set sh_info to one with a
1697 zero sh_size. ld sees this as a global symbol count
1698 of (unsigned) -1. Fix it here. */
1702 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1703 elf_dynsymtab (abfd
) = shindex
;
1704 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1705 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1706 abfd
->flags
|= HAS_SYMS
;
1708 /* Besides being a symbol table, we also treat this as a regular
1709 section, so that objcopy can handle it. */
1710 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1712 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1713 if (elf_symtab_shndx (abfd
) == shindex
)
1716 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1717 elf_symtab_shndx (abfd
) = shindex
;
1718 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1719 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1722 case SHT_STRTAB
: /* A string table */
1723 if (hdr
->bfd_section
!= NULL
)
1725 if (ehdr
->e_shstrndx
== shindex
)
1727 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1728 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1731 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1734 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1735 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1738 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1741 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1742 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1743 elf_elfsections (abfd
)[shindex
] = hdr
;
1744 /* We also treat this as a regular section, so that objcopy
1746 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1750 /* If the string table isn't one of the above, then treat it as a
1751 regular section. We need to scan all the headers to be sure,
1752 just in case this strtab section appeared before the above. */
1753 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1755 unsigned int i
, num_sec
;
1757 num_sec
= elf_numsections (abfd
);
1758 for (i
= 1; i
< num_sec
; i
++)
1760 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1761 if (hdr2
->sh_link
== shindex
)
1763 /* Prevent endless recursion on broken objects. */
1766 if (! bfd_section_from_shdr (abfd
, i
))
1768 if (elf_onesymtab (abfd
) == i
)
1770 if (elf_dynsymtab (abfd
) == i
)
1771 goto dynsymtab_strtab
;
1775 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1779 /* *These* do a lot of work -- but build no sections! */
1781 asection
*target_sect
;
1782 Elf_Internal_Shdr
*hdr2
, **p_hdr
;
1783 unsigned int num_sec
= elf_numsections (abfd
);
1784 struct bfd_elf_section_data
*esdt
;
1788 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1789 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1792 /* Check for a bogus link to avoid crashing. */
1793 if (hdr
->sh_link
>= num_sec
)
1795 ((*_bfd_error_handler
)
1796 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1797 abfd
, hdr
->sh_link
, name
, shindex
));
1798 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1802 /* For some incomprehensible reason Oracle distributes
1803 libraries for Solaris in which some of the objects have
1804 bogus sh_link fields. It would be nice if we could just
1805 reject them, but, unfortunately, some people need to use
1806 them. We scan through the section headers; if we find only
1807 one suitable symbol table, we clobber the sh_link to point
1808 to it. I hope this doesn't break anything.
1810 Don't do it on executable nor shared library. */
1811 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0
1812 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1813 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1819 for (scan
= 1; scan
< num_sec
; scan
++)
1821 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1822 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1833 hdr
->sh_link
= found
;
1836 /* Get the symbol table. */
1837 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1838 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1839 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1842 /* If this reloc section does not use the main symbol table we
1843 don't treat it as a reloc section. BFD can't adequately
1844 represent such a section, so at least for now, we don't
1845 try. We just present it as a normal section. We also
1846 can't use it as a reloc section if it points to the null
1847 section, an invalid section, another reloc section, or its
1848 sh_link points to the null section. */
1849 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1850 || hdr
->sh_link
== SHN_UNDEF
1851 || hdr
->sh_info
== SHN_UNDEF
1852 || hdr
->sh_info
>= num_sec
1853 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1854 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1855 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1858 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1860 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1861 if (target_sect
== NULL
)
1864 esdt
= elf_section_data (target_sect
);
1865 if (hdr
->sh_type
== SHT_RELA
)
1866 p_hdr
= &esdt
->rela
.hdr
;
1868 p_hdr
= &esdt
->rel
.hdr
;
1870 BFD_ASSERT (*p_hdr
== NULL
);
1871 amt
= sizeof (*hdr2
);
1872 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1877 elf_elfsections (abfd
)[shindex
] = hdr2
;
1878 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1879 target_sect
->flags
|= SEC_RELOC
;
1880 target_sect
->relocation
= NULL
;
1881 target_sect
->rel_filepos
= hdr
->sh_offset
;
1882 /* In the section to which the relocations apply, mark whether
1883 its relocations are of the REL or RELA variety. */
1884 if (hdr
->sh_size
!= 0)
1886 if (hdr
->sh_type
== SHT_RELA
)
1887 target_sect
->use_rela_p
= 1;
1889 abfd
->flags
|= HAS_RELOC
;
1893 case SHT_GNU_verdef
:
1894 elf_dynverdef (abfd
) = shindex
;
1895 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1896 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1898 case SHT_GNU_versym
:
1899 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1901 elf_dynversym (abfd
) = shindex
;
1902 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1903 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1905 case SHT_GNU_verneed
:
1906 elf_dynverref (abfd
) = shindex
;
1907 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1908 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1914 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
, GRP_ENTRY_SIZE
))
1916 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1918 if (hdr
->contents
!= NULL
)
1920 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1921 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1924 if (idx
->flags
& GRP_COMDAT
)
1925 hdr
->bfd_section
->flags
1926 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1928 /* We try to keep the same section order as it comes in. */
1930 while (--n_elt
!= 0)
1934 if (idx
->shdr
!= NULL
1935 && (s
= idx
->shdr
->bfd_section
) != NULL
1936 && elf_next_in_group (s
) != NULL
)
1938 elf_next_in_group (hdr
->bfd_section
) = s
;
1946 /* Possibly an attributes section. */
1947 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1948 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1950 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1952 _bfd_elf_parse_attributes (abfd
, hdr
);
1956 /* Check for any processor-specific section types. */
1957 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1960 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1962 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1963 /* FIXME: How to properly handle allocated section reserved
1964 for applications? */
1965 (*_bfd_error_handler
)
1966 (_("%B: don't know how to handle allocated, application "
1967 "specific section `%s' [0x%8x]"),
1968 abfd
, name
, hdr
->sh_type
);
1970 /* Allow sections reserved for applications. */
1971 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1974 else if (hdr
->sh_type
>= SHT_LOPROC
1975 && hdr
->sh_type
<= SHT_HIPROC
)
1976 /* FIXME: We should handle this section. */
1977 (*_bfd_error_handler
)
1978 (_("%B: don't know how to handle processor specific section "
1980 abfd
, name
, hdr
->sh_type
);
1981 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1983 /* Unrecognised OS-specific sections. */
1984 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1985 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1986 required to correctly process the section and the file should
1987 be rejected with an error message. */
1988 (*_bfd_error_handler
)
1989 (_("%B: don't know how to handle OS specific section "
1991 abfd
, name
, hdr
->sh_type
);
1993 /* Otherwise it should be processed. */
1994 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1997 /* FIXME: We should handle this section. */
1998 (*_bfd_error_handler
)
1999 (_("%B: don't know how to handle section `%s' [0x%8x]"),
2000 abfd
, name
, hdr
->sh_type
);
2008 /* Return the local symbol specified by ABFD, R_SYMNDX. */
2011 bfd_sym_from_r_symndx (struct sym_cache
*cache
,
2013 unsigned long r_symndx
)
2015 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2017 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
2019 Elf_Internal_Shdr
*symtab_hdr
;
2020 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2021 Elf_External_Sym_Shndx eshndx
;
2023 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2024 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2025 &cache
->sym
[ent
], esym
, &eshndx
) == NULL
)
2028 if (cache
->abfd
!= abfd
)
2030 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2033 cache
->indx
[ent
] = r_symndx
;
2036 return &cache
->sym
[ent
];
2039 /* Given an ELF section number, retrieve the corresponding BFD
2043 bfd_section_from_elf_index (bfd
*abfd
, unsigned int sec_index
)
2045 if (sec_index
>= elf_numsections (abfd
))
2047 return elf_elfsections (abfd
)[sec_index
]->bfd_section
;
2050 static const struct bfd_elf_special_section special_sections_b
[] =
2052 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2053 { NULL
, 0, 0, 0, 0 }
2056 static const struct bfd_elf_special_section special_sections_c
[] =
2058 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
2059 { NULL
, 0, 0, 0, 0 }
2062 static const struct bfd_elf_special_section special_sections_d
[] =
2064 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2065 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2066 /* There are more DWARF sections than these, but they needn't be added here
2067 unless you have to cope with broken compilers that don't emit section
2068 attributes or you want to help the user writing assembler. */
2069 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
2070 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
2071 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
2072 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
2073 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
2074 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
2075 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
2076 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
2077 { NULL
, 0, 0, 0, 0 }
2080 static const struct bfd_elf_special_section special_sections_f
[] =
2082 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2083 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2084 { NULL
, 0, 0, 0, 0 }
2087 static const struct bfd_elf_special_section special_sections_g
[] =
2089 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2090 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS
, SHF_EXCLUDE
},
2091 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2092 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2093 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2094 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2095 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2096 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2097 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2098 { NULL
, 0, 0, 0, 0 }
2101 static const struct bfd_elf_special_section special_sections_h
[] =
2103 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2104 { NULL
, 0, 0, 0, 0 }
2107 static const struct bfd_elf_special_section special_sections_i
[] =
2109 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2110 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2111 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2112 { NULL
, 0, 0, 0, 0 }
2115 static const struct bfd_elf_special_section special_sections_l
[] =
2117 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2118 { NULL
, 0, 0, 0, 0 }
2121 static const struct bfd_elf_special_section special_sections_n
[] =
2123 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2124 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2125 { NULL
, 0, 0, 0, 0 }
2128 static const struct bfd_elf_special_section special_sections_p
[] =
2130 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2131 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2132 { NULL
, 0, 0, 0, 0 }
2135 static const struct bfd_elf_special_section special_sections_r
[] =
2137 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2138 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2139 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2140 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2141 { NULL
, 0, 0, 0, 0 }
2144 static const struct bfd_elf_special_section special_sections_s
[] =
2146 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2147 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2148 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2149 /* See struct bfd_elf_special_section declaration for the semantics of
2150 this special case where .prefix_length != strlen (.prefix). */
2151 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2152 { NULL
, 0, 0, 0, 0 }
2155 static const struct bfd_elf_special_section special_sections_t
[] =
2157 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2158 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2159 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2160 { NULL
, 0, 0, 0, 0 }
2163 static const struct bfd_elf_special_section special_sections_z
[] =
2165 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2166 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2167 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2168 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2169 { NULL
, 0, 0, 0, 0 }
2172 static const struct bfd_elf_special_section
* const special_sections
[] =
2174 special_sections_b
, /* 'b' */
2175 special_sections_c
, /* 'c' */
2176 special_sections_d
, /* 'd' */
2178 special_sections_f
, /* 'f' */
2179 special_sections_g
, /* 'g' */
2180 special_sections_h
, /* 'h' */
2181 special_sections_i
, /* 'i' */
2184 special_sections_l
, /* 'l' */
2186 special_sections_n
, /* 'n' */
2188 special_sections_p
, /* 'p' */
2190 special_sections_r
, /* 'r' */
2191 special_sections_s
, /* 's' */
2192 special_sections_t
, /* 't' */
2198 special_sections_z
/* 'z' */
2201 const struct bfd_elf_special_section
*
2202 _bfd_elf_get_special_section (const char *name
,
2203 const struct bfd_elf_special_section
*spec
,
2209 len
= strlen (name
);
2211 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2214 int prefix_len
= spec
[i
].prefix_length
;
2216 if (len
< prefix_len
)
2218 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2221 suffix_len
= spec
[i
].suffix_length
;
2222 if (suffix_len
<= 0)
2224 if (name
[prefix_len
] != 0)
2226 if (suffix_len
== 0)
2228 if (name
[prefix_len
] != '.'
2229 && (suffix_len
== -2
2230 || (rela
&& spec
[i
].type
== SHT_REL
)))
2236 if (len
< prefix_len
+ suffix_len
)
2238 if (memcmp (name
+ len
- suffix_len
,
2239 spec
[i
].prefix
+ prefix_len
,
2249 const struct bfd_elf_special_section
*
2250 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2253 const struct bfd_elf_special_section
*spec
;
2254 const struct elf_backend_data
*bed
;
2256 /* See if this is one of the special sections. */
2257 if (sec
->name
== NULL
)
2260 bed
= get_elf_backend_data (abfd
);
2261 spec
= bed
->special_sections
;
2264 spec
= _bfd_elf_get_special_section (sec
->name
,
2265 bed
->special_sections
,
2271 if (sec
->name
[0] != '.')
2274 i
= sec
->name
[1] - 'b';
2275 if (i
< 0 || i
> 'z' - 'b')
2278 spec
= special_sections
[i
];
2283 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2287 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2289 struct bfd_elf_section_data
*sdata
;
2290 const struct elf_backend_data
*bed
;
2291 const struct bfd_elf_special_section
*ssect
;
2293 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2296 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
,
2300 sec
->used_by_bfd
= sdata
;
2303 /* Indicate whether or not this section should use RELA relocations. */
2304 bed
= get_elf_backend_data (abfd
);
2305 sec
->use_rela_p
= bed
->default_use_rela_p
;
2307 /* When we read a file, we don't need to set ELF section type and
2308 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2309 anyway. We will set ELF section type and flags for all linker
2310 created sections. If user specifies BFD section flags, we will
2311 set ELF section type and flags based on BFD section flags in
2312 elf_fake_sections. Special handling for .init_array/.fini_array
2313 output sections since they may contain .ctors/.dtors input
2314 sections. We don't want _bfd_elf_init_private_section_data to
2315 copy ELF section type from .ctors/.dtors input sections. */
2316 if (abfd
->direction
!= read_direction
2317 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2319 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2322 || (sec
->flags
& SEC_LINKER_CREATED
) != 0
2323 || ssect
->type
== SHT_INIT_ARRAY
2324 || ssect
->type
== SHT_FINI_ARRAY
))
2326 elf_section_type (sec
) = ssect
->type
;
2327 elf_section_flags (sec
) = ssect
->attr
;
2331 return _bfd_generic_new_section_hook (abfd
, sec
);
2334 /* Create a new bfd section from an ELF program header.
2336 Since program segments have no names, we generate a synthetic name
2337 of the form segment<NUM>, where NUM is generally the index in the
2338 program header table. For segments that are split (see below) we
2339 generate the names segment<NUM>a and segment<NUM>b.
2341 Note that some program segments may have a file size that is different than
2342 (less than) the memory size. All this means is that at execution the
2343 system must allocate the amount of memory specified by the memory size,
2344 but only initialize it with the first "file size" bytes read from the
2345 file. This would occur for example, with program segments consisting
2346 of combined data+bss.
2348 To handle the above situation, this routine generates TWO bfd sections
2349 for the single program segment. The first has the length specified by
2350 the file size of the segment, and the second has the length specified
2351 by the difference between the two sizes. In effect, the segment is split
2352 into its initialized and uninitialized parts.
2357 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2358 Elf_Internal_Phdr
*hdr
,
2360 const char *type_name
)
2368 split
= ((hdr
->p_memsz
> 0)
2369 && (hdr
->p_filesz
> 0)
2370 && (hdr
->p_memsz
> hdr
->p_filesz
));
2372 if (hdr
->p_filesz
> 0)
2374 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "a" : "");
2375 len
= strlen (namebuf
) + 1;
2376 name
= (char *) bfd_alloc (abfd
, len
);
2379 memcpy (name
, namebuf
, len
);
2380 newsect
= bfd_make_section (abfd
, name
);
2381 if (newsect
== NULL
)
2383 newsect
->vma
= hdr
->p_vaddr
;
2384 newsect
->lma
= hdr
->p_paddr
;
2385 newsect
->size
= hdr
->p_filesz
;
2386 newsect
->filepos
= hdr
->p_offset
;
2387 newsect
->flags
|= SEC_HAS_CONTENTS
;
2388 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2389 if (hdr
->p_type
== PT_LOAD
)
2391 newsect
->flags
|= SEC_ALLOC
;
2392 newsect
->flags
|= SEC_LOAD
;
2393 if (hdr
->p_flags
& PF_X
)
2395 /* FIXME: all we known is that it has execute PERMISSION,
2397 newsect
->flags
|= SEC_CODE
;
2400 if (!(hdr
->p_flags
& PF_W
))
2402 newsect
->flags
|= SEC_READONLY
;
2406 if (hdr
->p_memsz
> hdr
->p_filesz
)
2410 sprintf (namebuf
, "%s%d%s", type_name
, hdr_index
, split
? "b" : "");
2411 len
= strlen (namebuf
) + 1;
2412 name
= (char *) bfd_alloc (abfd
, len
);
2415 memcpy (name
, namebuf
, len
);
2416 newsect
= bfd_make_section (abfd
, name
);
2417 if (newsect
== NULL
)
2419 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2420 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2421 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2422 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2423 align
= newsect
->vma
& -newsect
->vma
;
2424 if (align
== 0 || align
> hdr
->p_align
)
2425 align
= hdr
->p_align
;
2426 newsect
->alignment_power
= bfd_log2 (align
);
2427 if (hdr
->p_type
== PT_LOAD
)
2429 /* Hack for gdb. Segments that have not been modified do
2430 not have their contents written to a core file, on the
2431 assumption that a debugger can find the contents in the
2432 executable. We flag this case by setting the fake
2433 section size to zero. Note that "real" bss sections will
2434 always have their contents dumped to the core file. */
2435 if (bfd_get_format (abfd
) == bfd_core
)
2437 newsect
->flags
|= SEC_ALLOC
;
2438 if (hdr
->p_flags
& PF_X
)
2439 newsect
->flags
|= SEC_CODE
;
2441 if (!(hdr
->p_flags
& PF_W
))
2442 newsect
->flags
|= SEC_READONLY
;
2449 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int hdr_index
)
2451 const struct elf_backend_data
*bed
;
2453 switch (hdr
->p_type
)
2456 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "null");
2459 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "load");
2462 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "dynamic");
2465 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "interp");
2468 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "note"))
2470 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2475 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "shlib");
2478 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "phdr");
2480 case PT_GNU_EH_FRAME
:
2481 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
,
2485 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "stack");
2488 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, hdr_index
, "relro");
2491 /* Check for any processor-specific program segment types. */
2492 bed
= get_elf_backend_data (abfd
);
2493 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, hdr_index
, "proc");
2497 /* Return the REL_HDR for SEC, assuming there is only a single one, either
2501 _bfd_elf_single_rel_hdr (asection
*sec
)
2503 if (elf_section_data (sec
)->rel
.hdr
)
2505 BFD_ASSERT (elf_section_data (sec
)->rela
.hdr
== NULL
);
2506 return elf_section_data (sec
)->rel
.hdr
;
2509 return elf_section_data (sec
)->rela
.hdr
;
2512 /* Allocate and initialize a section-header for a new reloc section,
2513 containing relocations against ASECT. It is stored in RELDATA. If
2514 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2518 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2519 struct bfd_elf_section_reloc_data
*reldata
,
2521 bfd_boolean use_rela_p
)
2523 Elf_Internal_Shdr
*rel_hdr
;
2525 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2528 amt
= sizeof (Elf_Internal_Shdr
);
2529 BFD_ASSERT (reldata
->hdr
== NULL
);
2530 rel_hdr
= bfd_zalloc (abfd
, amt
);
2531 reldata
->hdr
= rel_hdr
;
2533 amt
= sizeof ".rela" + strlen (asect
->name
);
2534 name
= (char *) bfd_alloc (abfd
, amt
);
2537 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2539 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2541 if (rel_hdr
->sh_name
== (unsigned int) -1)
2543 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2544 rel_hdr
->sh_entsize
= (use_rela_p
2545 ? bed
->s
->sizeof_rela
2546 : bed
->s
->sizeof_rel
);
2547 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2548 rel_hdr
->sh_flags
= 0;
2549 rel_hdr
->sh_addr
= 0;
2550 rel_hdr
->sh_size
= 0;
2551 rel_hdr
->sh_offset
= 0;
2556 /* Return the default section type based on the passed in section flags. */
2559 bfd_elf_get_default_section_type (flagword flags
)
2561 if ((flags
& SEC_ALLOC
) != 0
2562 && (flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2564 return SHT_PROGBITS
;
2567 struct fake_section_arg
2569 struct bfd_link_info
*link_info
;
2573 /* Set up an ELF internal section header for a section. */
2576 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *fsarg
)
2578 struct fake_section_arg
*arg
= (struct fake_section_arg
*)fsarg
;
2579 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2580 struct bfd_elf_section_data
*esd
= elf_section_data (asect
);
2581 Elf_Internal_Shdr
*this_hdr
;
2582 unsigned int sh_type
;
2586 /* We already failed; just get out of the bfd_map_over_sections
2591 this_hdr
= &esd
->this_hdr
;
2593 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2594 asect
->name
, FALSE
);
2595 if (this_hdr
->sh_name
== (unsigned int) -1)
2601 /* Don't clear sh_flags. Assembler may set additional bits. */
2603 if ((asect
->flags
& SEC_ALLOC
) != 0
2604 || asect
->user_set_vma
)
2605 this_hdr
->sh_addr
= asect
->vma
;
2607 this_hdr
->sh_addr
= 0;
2609 this_hdr
->sh_offset
= 0;
2610 this_hdr
->sh_size
= asect
->size
;
2611 this_hdr
->sh_link
= 0;
2612 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2613 /* The sh_entsize and sh_info fields may have been set already by
2614 copy_private_section_data. */
2616 this_hdr
->bfd_section
= asect
;
2617 this_hdr
->contents
= NULL
;
2619 /* If the section type is unspecified, we set it based on
2621 if ((asect
->flags
& SEC_GROUP
) != 0)
2622 sh_type
= SHT_GROUP
;
2624 sh_type
= bfd_elf_get_default_section_type (asect
->flags
);
2626 if (this_hdr
->sh_type
== SHT_NULL
)
2627 this_hdr
->sh_type
= sh_type
;
2628 else if (this_hdr
->sh_type
== SHT_NOBITS
2629 && sh_type
== SHT_PROGBITS
2630 && (asect
->flags
& SEC_ALLOC
) != 0)
2632 /* Warn if we are changing a NOBITS section to PROGBITS, but
2633 allow the link to proceed. This can happen when users link
2634 non-bss input sections to bss output sections, or emit data
2635 to a bss output section via a linker script. */
2636 (*_bfd_error_handler
)
2637 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2638 this_hdr
->sh_type
= sh_type
;
2641 switch (this_hdr
->sh_type
)
2647 case SHT_INIT_ARRAY
:
2648 case SHT_FINI_ARRAY
:
2649 case SHT_PREINIT_ARRAY
:
2656 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2660 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2664 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2668 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2669 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2673 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2674 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2677 case SHT_GNU_versym
:
2678 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2681 case SHT_GNU_verdef
:
2682 this_hdr
->sh_entsize
= 0;
2683 /* objcopy or strip will copy over sh_info, but may not set
2684 cverdefs. The linker will set cverdefs, but sh_info will be
2686 if (this_hdr
->sh_info
== 0)
2687 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2689 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2690 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2693 case SHT_GNU_verneed
:
2694 this_hdr
->sh_entsize
= 0;
2695 /* objcopy or strip will copy over sh_info, but may not set
2696 cverrefs. The linker will set cverrefs, but sh_info will be
2698 if (this_hdr
->sh_info
== 0)
2699 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2701 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2702 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2706 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2710 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2714 if ((asect
->flags
& SEC_ALLOC
) != 0)
2715 this_hdr
->sh_flags
|= SHF_ALLOC
;
2716 if ((asect
->flags
& SEC_READONLY
) == 0)
2717 this_hdr
->sh_flags
|= SHF_WRITE
;
2718 if ((asect
->flags
& SEC_CODE
) != 0)
2719 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2720 if ((asect
->flags
& SEC_MERGE
) != 0)
2722 this_hdr
->sh_flags
|= SHF_MERGE
;
2723 this_hdr
->sh_entsize
= asect
->entsize
;
2724 if ((asect
->flags
& SEC_STRINGS
) != 0)
2725 this_hdr
->sh_flags
|= SHF_STRINGS
;
2727 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2728 this_hdr
->sh_flags
|= SHF_GROUP
;
2729 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2731 this_hdr
->sh_flags
|= SHF_TLS
;
2732 if (asect
->size
== 0
2733 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2735 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2737 this_hdr
->sh_size
= 0;
2740 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2741 if (this_hdr
->sh_size
!= 0)
2742 this_hdr
->sh_type
= SHT_NOBITS
;
2746 if ((asect
->flags
& (SEC_GROUP
| SEC_EXCLUDE
)) == SEC_EXCLUDE
)
2747 this_hdr
->sh_flags
|= SHF_EXCLUDE
;
2749 /* If the section has relocs, set up a section header for the
2750 SHT_REL[A] section. If two relocation sections are required for
2751 this section, it is up to the processor-specific back-end to
2752 create the other. */
2753 if ((asect
->flags
& SEC_RELOC
) != 0)
2755 /* When doing a relocatable link, create both REL and RELA sections if
2758 /* Do the normal setup if we wouldn't create any sections here. */
2759 && esd
->rel
.count
+ esd
->rela
.count
> 0
2760 && (arg
->link_info
->relocatable
|| arg
->link_info
->emitrelocations
))
2762 if (esd
->rel
.count
&& esd
->rel
.hdr
== NULL
2763 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rel
, asect
, FALSE
))
2768 if (esd
->rela
.count
&& esd
->rela
.hdr
== NULL
2769 && !_bfd_elf_init_reloc_shdr (abfd
, &esd
->rela
, asect
, TRUE
))
2775 else if (!_bfd_elf_init_reloc_shdr (abfd
,
2777 ? &esd
->rela
: &esd
->rel
),
2783 /* Check for processor-specific section types. */
2784 sh_type
= this_hdr
->sh_type
;
2785 if (bed
->elf_backend_fake_sections
2786 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2789 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2791 /* Don't change the header type from NOBITS if we are being
2792 called for objcopy --only-keep-debug. */
2793 this_hdr
->sh_type
= sh_type
;
2797 /* Fill in the contents of a SHT_GROUP section. Called from
2798 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2799 when ELF targets use the generic linker, ld. Called for ld -r
2800 from bfd_elf_final_link. */
2803 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2805 bfd_boolean
*failedptr
= (bfd_boolean
*) failedptrarg
;
2806 asection
*elt
, *first
;
2810 /* Ignore linker created group section. See elfNN_ia64_object_p in
2812 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2816 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2818 unsigned long symindx
= 0;
2820 /* elf_group_id will have been set up by objcopy and the
2822 if (elf_group_id (sec
) != NULL
)
2823 symindx
= elf_group_id (sec
)->udata
.i
;
2827 /* If called from the assembler, swap_out_syms will have set up
2828 elf_section_syms. */
2829 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2830 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2832 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2834 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2836 /* The ELF backend linker sets sh_info to -2 when the group
2837 signature symbol is global, and thus the index can't be
2838 set until all local symbols are output. */
2839 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2840 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2841 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2842 unsigned long extsymoff
= 0;
2843 struct elf_link_hash_entry
*h
;
2845 if (!elf_bad_symtab (igroup
->owner
))
2847 Elf_Internal_Shdr
*symtab_hdr
;
2849 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2850 extsymoff
= symtab_hdr
->sh_info
;
2852 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2853 while (h
->root
.type
== bfd_link_hash_indirect
2854 || h
->root
.type
== bfd_link_hash_warning
)
2855 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2857 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2860 /* The contents won't be allocated for "ld -r" or objcopy. */
2862 if (sec
->contents
== NULL
)
2865 sec
->contents
= (unsigned char *) bfd_alloc (abfd
, sec
->size
);
2867 /* Arrange for the section to be written out. */
2868 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2869 if (sec
->contents
== NULL
)
2876 loc
= sec
->contents
+ sec
->size
;
2878 /* Get the pointer to the first section in the group that gas
2879 squirreled away here. objcopy arranges for this to be set to the
2880 start of the input section group. */
2881 first
= elt
= elf_next_in_group (sec
);
2883 /* First element is a flag word. Rest of section is elf section
2884 indices for all the sections of the group. Write them backwards
2885 just to keep the group in the same order as given in .section
2886 directives, not that it matters. */
2893 s
= s
->output_section
;
2895 && !bfd_is_abs_section (s
))
2897 unsigned int idx
= elf_section_data (s
)->this_idx
;
2900 H_PUT_32 (abfd
, idx
, loc
);
2902 elt
= elf_next_in_group (elt
);
2907 if ((loc
-= 4) != sec
->contents
)
2910 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2913 /* Assign all ELF section numbers. The dummy first section is handled here
2914 too. The link/info pointers for the standard section types are filled
2915 in here too, while we're at it. */
2918 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2920 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2922 unsigned int section_number
, secn
;
2923 Elf_Internal_Shdr
**i_shdrp
;
2924 struct bfd_elf_section_data
*d
;
2925 bfd_boolean need_symtab
;
2929 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2931 /* SHT_GROUP sections are in relocatable files only. */
2932 if (link_info
== NULL
|| link_info
->relocatable
)
2934 /* Put SHT_GROUP sections first. */
2935 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2937 d
= elf_section_data (sec
);
2939 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2941 if (sec
->flags
& SEC_LINKER_CREATED
)
2943 /* Remove the linker created SHT_GROUP sections. */
2944 bfd_section_list_remove (abfd
, sec
);
2945 abfd
->section_count
--;
2948 d
->this_idx
= section_number
++;
2953 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2955 d
= elf_section_data (sec
);
2957 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2958 d
->this_idx
= section_number
++;
2959 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2962 d
->rel
.idx
= section_number
++;
2963 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel
.hdr
->sh_name
);
2970 d
->rela
.idx
= section_number
++;
2971 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rela
.hdr
->sh_name
);
2977 t
->shstrtab_section
= section_number
++;
2978 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2979 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2981 need_symtab
= (bfd_get_symcount (abfd
) > 0
2982 || (link_info
== NULL
2983 && ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
2987 t
->symtab_section
= section_number
++;
2988 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2989 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
2991 t
->symtab_shndx_section
= section_number
++;
2992 t
->symtab_shndx_hdr
.sh_name
2993 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2994 ".symtab_shndx", FALSE
);
2995 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2998 t
->strtab_section
= section_number
++;
2999 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
3002 if (section_number
>= SHN_LORESERVE
)
3004 _bfd_error_handler (_("%B: too many sections: %u"),
3005 abfd
, section_number
);
3009 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
3010 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3012 elf_numsections (abfd
) = section_number
;
3013 elf_elfheader (abfd
)->e_shnum
= section_number
;
3015 /* Set up the list of section header pointers, in agreement with the
3017 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc2 (abfd
, section_number
,
3018 sizeof (Elf_Internal_Shdr
*));
3019 if (i_shdrp
== NULL
)
3022 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
,
3023 sizeof (Elf_Internal_Shdr
));
3024 if (i_shdrp
[0] == NULL
)
3026 bfd_release (abfd
, i_shdrp
);
3030 elf_elfsections (abfd
) = i_shdrp
;
3032 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
3035 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
3036 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
3038 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
3039 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
3041 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
3042 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
3045 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
3050 d
= elf_section_data (sec
);
3052 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
3053 if (d
->rel
.idx
!= 0)
3054 i_shdrp
[d
->rel
.idx
] = d
->rel
.hdr
;
3055 if (d
->rela
.idx
!= 0)
3056 i_shdrp
[d
->rela
.idx
] = d
->rela
.hdr
;
3058 /* Fill in the sh_link and sh_info fields while we're at it. */
3060 /* sh_link of a reloc section is the section index of the symbol
3061 table. sh_info is the section index of the section to which
3062 the relocation entries apply. */
3063 if (d
->rel
.idx
!= 0)
3065 d
->rel
.hdr
->sh_link
= t
->symtab_section
;
3066 d
->rel
.hdr
->sh_info
= d
->this_idx
;
3068 if (d
->rela
.idx
!= 0)
3070 d
->rela
.hdr
->sh_link
= t
->symtab_section
;
3071 d
->rela
.hdr
->sh_info
= d
->this_idx
;
3074 /* We need to set up sh_link for SHF_LINK_ORDER. */
3075 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
3077 s
= elf_linked_to_section (sec
);
3080 /* elf_linked_to_section points to the input section. */
3081 if (link_info
!= NULL
)
3083 /* Check discarded linkonce section. */
3084 if (discarded_section (s
))
3087 (*_bfd_error_handler
)
3088 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3089 abfd
, d
->this_hdr
.bfd_section
,
3091 /* Point to the kept section if it has the same
3092 size as the discarded one. */
3093 kept
= _bfd_elf_check_kept_section (s
, link_info
);
3096 bfd_set_error (bfd_error_bad_value
);
3102 s
= s
->output_section
;
3103 BFD_ASSERT (s
!= NULL
);
3107 /* Handle objcopy. */
3108 if (s
->output_section
== NULL
)
3110 (*_bfd_error_handler
)
3111 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3112 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
3113 bfd_set_error (bfd_error_bad_value
);
3116 s
= s
->output_section
;
3118 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3123 The Intel C compiler generates SHT_IA_64_UNWIND with
3124 SHF_LINK_ORDER. But it doesn't set the sh_link or
3125 sh_info fields. Hence we could get the situation
3127 const struct elf_backend_data
*bed
3128 = get_elf_backend_data (abfd
);
3129 if (bed
->link_order_error_handler
)
3130 bed
->link_order_error_handler
3131 (_("%B: warning: sh_link not set for section `%A'"),
3136 switch (d
->this_hdr
.sh_type
)
3140 /* A reloc section which we are treating as a normal BFD
3141 section. sh_link is the section index of the symbol
3142 table. sh_info is the section index of the section to
3143 which the relocation entries apply. We assume that an
3144 allocated reloc section uses the dynamic symbol table.
3145 FIXME: How can we be sure? */
3146 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3148 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3150 /* We look up the section the relocs apply to by name. */
3152 if (d
->this_hdr
.sh_type
== SHT_REL
)
3156 s
= bfd_get_section_by_name (abfd
, name
);
3158 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
3162 /* We assume that a section named .stab*str is a stabs
3163 string section. We look for a section with the same name
3164 but without the trailing ``str'', and set its sh_link
3165 field to point to this section. */
3166 if (CONST_STRNEQ (sec
->name
, ".stab")
3167 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3172 len
= strlen (sec
->name
);
3173 alc
= (char *) bfd_malloc (len
- 2);
3176 memcpy (alc
, sec
->name
, len
- 3);
3177 alc
[len
- 3] = '\0';
3178 s
= bfd_get_section_by_name (abfd
, alc
);
3182 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3184 /* This is a .stab section. */
3185 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3186 elf_section_data (s
)->this_hdr
.sh_entsize
3187 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3194 case SHT_GNU_verneed
:
3195 case SHT_GNU_verdef
:
3196 /* sh_link is the section header index of the string table
3197 used for the dynamic entries, or the symbol table, or the
3199 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3201 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3204 case SHT_GNU_LIBLIST
:
3205 /* sh_link is the section header index of the prelink library
3206 list used for the dynamic entries, or the symbol table, or
3207 the version strings. */
3208 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3209 ? ".dynstr" : ".gnu.libstr");
3211 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3216 case SHT_GNU_versym
:
3217 /* sh_link is the section header index of the symbol table
3218 this hash table or version table is for. */
3219 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3221 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3225 d
->this_hdr
.sh_link
= t
->symtab_section
;
3229 for (secn
= 1; secn
< section_number
; ++secn
)
3230 if (i_shdrp
[secn
] == NULL
)
3231 i_shdrp
[secn
] = i_shdrp
[0];
3233 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3234 i_shdrp
[secn
]->sh_name
);
3239 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3241 /* If the backend has a special mapping, use it. */
3242 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3243 if (bed
->elf_backend_sym_is_global
)
3244 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3246 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
| BSF_GNU_UNIQUE
)) != 0
3247 || bfd_is_und_section (bfd_get_section (sym
))
3248 || bfd_is_com_section (bfd_get_section (sym
)));
3251 /* Don't output section symbols for sections that are not going to be
3252 output, that are duplicates or there is no BFD section. */
3255 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3257 elf_symbol_type
*type_ptr
;
3259 if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
3262 type_ptr
= elf_symbol_from (abfd
, sym
);
3263 return ((type_ptr
!= NULL
3264 && type_ptr
->internal_elf_sym
.st_shndx
!= 0
3265 && bfd_is_abs_section (sym
->section
))
3266 || !(sym
->section
->owner
== abfd
3267 || (sym
->section
->output_section
->owner
== abfd
3268 && sym
->section
->output_offset
== 0)
3269 || bfd_is_abs_section (sym
->section
)));
3272 /* Map symbol from it's internal number to the external number, moving
3273 all local symbols to be at the head of the list. */
3276 elf_map_symbols (bfd
*abfd
)
3278 unsigned int symcount
= bfd_get_symcount (abfd
);
3279 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3280 asymbol
**sect_syms
;
3281 unsigned int num_locals
= 0;
3282 unsigned int num_globals
= 0;
3283 unsigned int num_locals2
= 0;
3284 unsigned int num_globals2
= 0;
3291 fprintf (stderr
, "elf_map_symbols\n");
3295 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3297 if (max_index
< asect
->index
)
3298 max_index
= asect
->index
;
3302 sect_syms
= (asymbol
**) bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3303 if (sect_syms
== NULL
)
3305 elf_section_syms (abfd
) = sect_syms
;
3306 elf_num_section_syms (abfd
) = max_index
;
3308 /* Init sect_syms entries for any section symbols we have already
3309 decided to output. */
3310 for (idx
= 0; idx
< symcount
; idx
++)
3312 asymbol
*sym
= syms
[idx
];
3314 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3316 && !ignore_section_sym (abfd
, sym
)
3317 && !bfd_is_abs_section (sym
->section
))
3319 asection
*sec
= sym
->section
;
3321 if (sec
->owner
!= abfd
)
3322 sec
= sec
->output_section
;
3324 sect_syms
[sec
->index
] = syms
[idx
];
3328 /* Classify all of the symbols. */
3329 for (idx
= 0; idx
< symcount
; idx
++)
3331 if (sym_is_global (abfd
, syms
[idx
]))
3333 else if (!ignore_section_sym (abfd
, syms
[idx
]))
3337 /* We will be adding a section symbol for each normal BFD section. Most
3338 sections will already have a section symbol in outsymbols, but
3339 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3340 at least in that case. */
3341 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3343 if (sect_syms
[asect
->index
] == NULL
)
3345 if (!sym_is_global (abfd
, asect
->symbol
))
3352 /* Now sort the symbols so the local symbols are first. */
3353 new_syms
= (asymbol
**) bfd_alloc2 (abfd
, num_locals
+ num_globals
,
3354 sizeof (asymbol
*));
3356 if (new_syms
== NULL
)
3359 for (idx
= 0; idx
< symcount
; idx
++)
3361 asymbol
*sym
= syms
[idx
];
3364 if (sym_is_global (abfd
, sym
))
3365 i
= num_locals
+ num_globals2
++;
3366 else if (!ignore_section_sym (abfd
, sym
))
3371 sym
->udata
.i
= i
+ 1;
3373 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3375 if (sect_syms
[asect
->index
] == NULL
)
3377 asymbol
*sym
= asect
->symbol
;
3380 sect_syms
[asect
->index
] = sym
;
3381 if (!sym_is_global (abfd
, sym
))
3384 i
= num_locals
+ num_globals2
++;
3386 sym
->udata
.i
= i
+ 1;
3390 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3392 elf_num_locals (abfd
) = num_locals
;
3393 elf_num_globals (abfd
) = num_globals
;
3397 /* Align to the maximum file alignment that could be required for any
3398 ELF data structure. */
3400 static inline file_ptr
3401 align_file_position (file_ptr off
, int align
)
3403 return (off
+ align
- 1) & ~(align
- 1);
3406 /* Assign a file position to a section, optionally aligning to the
3407 required section alignment. */
3410 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3414 if (align
&& i_shdrp
->sh_addralign
> 1)
3415 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3416 i_shdrp
->sh_offset
= offset
;
3417 if (i_shdrp
->bfd_section
!= NULL
)
3418 i_shdrp
->bfd_section
->filepos
= offset
;
3419 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3420 offset
+= i_shdrp
->sh_size
;
3424 /* Compute the file positions we are going to put the sections at, and
3425 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3426 is not NULL, this is being called by the ELF backend linker. */
3429 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3430 struct bfd_link_info
*link_info
)
3432 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3433 struct fake_section_arg fsargs
;
3435 struct bfd_strtab_hash
*strtab
= NULL
;
3436 Elf_Internal_Shdr
*shstrtab_hdr
;
3437 bfd_boolean need_symtab
;
3439 if (abfd
->output_has_begun
)
3442 /* Do any elf backend specific processing first. */
3443 if (bed
->elf_backend_begin_write_processing
)
3444 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3446 if (! prep_headers (abfd
))
3449 /* Post process the headers if necessary. */
3450 if (bed
->elf_backend_post_process_headers
)
3451 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3453 fsargs
.failed
= FALSE
;
3454 fsargs
.link_info
= link_info
;
3455 bfd_map_over_sections (abfd
, elf_fake_sections
, &fsargs
);
3459 if (!assign_section_numbers (abfd
, link_info
))
3462 /* The backend linker builds symbol table information itself. */
3463 need_symtab
= (link_info
== NULL
3464 && (bfd_get_symcount (abfd
) > 0
3465 || ((abfd
->flags
& (EXEC_P
| DYNAMIC
| HAS_RELOC
))
3469 /* Non-zero if doing a relocatable link. */
3470 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3472 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3477 if (link_info
== NULL
)
3479 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3484 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3485 /* sh_name was set in prep_headers. */
3486 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3487 shstrtab_hdr
->sh_flags
= 0;
3488 shstrtab_hdr
->sh_addr
= 0;
3489 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3490 shstrtab_hdr
->sh_entsize
= 0;
3491 shstrtab_hdr
->sh_link
= 0;
3492 shstrtab_hdr
->sh_info
= 0;
3493 /* sh_offset is set in assign_file_positions_except_relocs. */
3494 shstrtab_hdr
->sh_addralign
= 1;
3496 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3502 Elf_Internal_Shdr
*hdr
;
3504 off
= elf_tdata (abfd
)->next_file_pos
;
3506 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3507 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3509 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3510 if (hdr
->sh_size
!= 0)
3511 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3513 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3514 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3516 elf_tdata (abfd
)->next_file_pos
= off
;
3518 /* Now that we know where the .strtab section goes, write it
3520 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3521 || ! _bfd_stringtab_emit (abfd
, strtab
))
3523 _bfd_stringtab_free (strtab
);
3526 abfd
->output_has_begun
= TRUE
;
3531 /* Make an initial estimate of the size of the program header. If we
3532 get the number wrong here, we'll redo section placement. */
3534 static bfd_size_type
3535 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3539 const struct elf_backend_data
*bed
;
3541 /* Assume we will need exactly two PT_LOAD segments: one for text
3542 and one for data. */
3545 s
= bfd_get_section_by_name (abfd
, ".interp");
3546 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3548 /* If we have a loadable interpreter section, we need a
3549 PT_INTERP segment. In this case, assume we also need a
3550 PT_PHDR segment, although that may not be true for all
3555 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3557 /* We need a PT_DYNAMIC segment. */
3561 if (info
!= NULL
&& info
->relro
)
3563 /* We need a PT_GNU_RELRO segment. */
3567 if (elf_tdata (abfd
)->eh_frame_hdr
)
3569 /* We need a PT_GNU_EH_FRAME segment. */
3573 if (elf_tdata (abfd
)->stack_flags
)
3575 /* We need a PT_GNU_STACK segment. */
3579 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3581 if ((s
->flags
& SEC_LOAD
) != 0
3582 && CONST_STRNEQ (s
->name
, ".note"))
3584 /* We need a PT_NOTE segment. */
3586 /* Try to create just one PT_NOTE segment
3587 for all adjacent loadable .note* sections.
3588 gABI requires that within a PT_NOTE segment
3589 (and also inside of each SHT_NOTE section)
3590 each note is padded to a multiple of 4 size,
3591 so we check whether the sections are correctly
3593 if (s
->alignment_power
== 2)
3594 while (s
->next
!= NULL
3595 && s
->next
->alignment_power
== 2
3596 && (s
->next
->flags
& SEC_LOAD
) != 0
3597 && CONST_STRNEQ (s
->next
->name
, ".note"))
3602 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3604 if (s
->flags
& SEC_THREAD_LOCAL
)
3606 /* We need a PT_TLS segment. */
3612 /* Let the backend count up any program headers it might need. */
3613 bed
= get_elf_backend_data (abfd
);
3614 if (bed
->elf_backend_additional_program_headers
)
3618 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3624 return segs
* bed
->s
->sizeof_phdr
;
3627 /* Find the segment that contains the output_section of section. */
3630 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3632 struct elf_segment_map
*m
;
3633 Elf_Internal_Phdr
*p
;
3635 for (m
= elf_tdata (abfd
)->segment_map
,
3636 p
= elf_tdata (abfd
)->phdr
;
3642 for (i
= m
->count
- 1; i
>= 0; i
--)
3643 if (m
->sections
[i
] == section
)
3650 /* Create a mapping from a set of sections to a program segment. */
3652 static struct elf_segment_map
*
3653 make_mapping (bfd
*abfd
,
3654 asection
**sections
,
3659 struct elf_segment_map
*m
;
3664 amt
= sizeof (struct elf_segment_map
);
3665 amt
+= (to
- from
- 1) * sizeof (asection
*);
3666 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3670 m
->p_type
= PT_LOAD
;
3671 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3672 m
->sections
[i
- from
] = *hdrpp
;
3673 m
->count
= to
- from
;
3675 if (from
== 0 && phdr
)
3677 /* Include the headers in the first PT_LOAD segment. */
3678 m
->includes_filehdr
= 1;
3679 m
->includes_phdrs
= 1;
3685 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3688 struct elf_segment_map
*
3689 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3691 struct elf_segment_map
*m
;
3693 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
,
3694 sizeof (struct elf_segment_map
));
3698 m
->p_type
= PT_DYNAMIC
;
3700 m
->sections
[0] = dynsec
;
3705 /* Possibly add or remove segments from the segment map. */
3708 elf_modify_segment_map (bfd
*abfd
,
3709 struct bfd_link_info
*info
,
3710 bfd_boolean remove_empty_load
)
3712 struct elf_segment_map
**m
;
3713 const struct elf_backend_data
*bed
;
3715 /* The placement algorithm assumes that non allocated sections are
3716 not in PT_LOAD segments. We ensure this here by removing such
3717 sections from the segment map. We also remove excluded
3718 sections. Finally, any PT_LOAD segment without sections is
3720 m
= &elf_tdata (abfd
)->segment_map
;
3723 unsigned int i
, new_count
;
3725 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3727 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3728 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3729 || (*m
)->p_type
!= PT_LOAD
))
3731 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3735 (*m
)->count
= new_count
;
3737 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3743 bed
= get_elf_backend_data (abfd
);
3744 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3746 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3753 /* Set up a mapping from BFD sections to program segments. */
3756 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3759 struct elf_segment_map
*m
;
3760 asection
**sections
= NULL
;
3761 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3762 bfd_boolean no_user_phdrs
;
3764 no_user_phdrs
= elf_tdata (abfd
)->segment_map
== NULL
;
3767 info
->user_phdrs
= !no_user_phdrs
;
3769 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3773 struct elf_segment_map
*mfirst
;
3774 struct elf_segment_map
**pm
;
3777 unsigned int phdr_index
;
3778 bfd_vma maxpagesize
;
3780 bfd_boolean phdr_in_segment
= TRUE
;
3781 bfd_boolean writable
;
3783 asection
*first_tls
= NULL
;
3784 asection
*dynsec
, *eh_frame_hdr
;
3786 bfd_vma addr_mask
, wrap_to
= 0;
3788 /* Select the allocated sections, and sort them. */
3790 sections
= (asection
**) bfd_malloc2 (bfd_count_sections (abfd
),
3791 sizeof (asection
*));
3792 if (sections
== NULL
)
3795 /* Calculate top address, avoiding undefined behaviour of shift
3796 left operator when shift count is equal to size of type
3798 addr_mask
= ((bfd_vma
) 1 << (bfd_arch_bits_per_address (abfd
) - 1)) - 1;
3799 addr_mask
= (addr_mask
<< 1) + 1;
3802 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3804 if ((s
->flags
& SEC_ALLOC
) != 0)
3808 /* A wrapping section potentially clashes with header. */
3809 if (((s
->lma
+ s
->size
) & addr_mask
) < (s
->lma
& addr_mask
))
3810 wrap_to
= (s
->lma
+ s
->size
) & addr_mask
;
3813 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3816 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3818 /* Build the mapping. */
3823 /* If we have a .interp section, then create a PT_PHDR segment for
3824 the program headers and a PT_INTERP segment for the .interp
3826 s
= bfd_get_section_by_name (abfd
, ".interp");
3827 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3829 amt
= sizeof (struct elf_segment_map
);
3830 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3834 m
->p_type
= PT_PHDR
;
3835 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3836 m
->p_flags
= PF_R
| PF_X
;
3837 m
->p_flags_valid
= 1;
3838 m
->includes_phdrs
= 1;
3843 amt
= sizeof (struct elf_segment_map
);
3844 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3848 m
->p_type
= PT_INTERP
;
3856 /* Look through the sections. We put sections in the same program
3857 segment when the start of the second section can be placed within
3858 a few bytes of the end of the first section. */
3862 maxpagesize
= bed
->maxpagesize
;
3864 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3866 && (dynsec
->flags
& SEC_LOAD
) == 0)
3869 /* Deal with -Ttext or something similar such that the first section
3870 is not adjacent to the program headers. This is an
3871 approximation, since at this point we don't know exactly how many
3872 program headers we will need. */
3875 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
3877 if (phdr_size
== (bfd_size_type
) -1)
3878 phdr_size
= get_program_header_size (abfd
, info
);
3879 phdr_size
+= bed
->s
->sizeof_ehdr
;
3880 if ((abfd
->flags
& D_PAGED
) == 0
3881 || (sections
[0]->lma
& addr_mask
) < phdr_size
3882 || ((sections
[0]->lma
& addr_mask
) % maxpagesize
3883 < phdr_size
% maxpagesize
)
3884 || (sections
[0]->lma
& addr_mask
& -maxpagesize
) < wrap_to
)
3885 phdr_in_segment
= FALSE
;
3888 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3891 bfd_boolean new_segment
;
3895 /* See if this section and the last one will fit in the same
3898 if (last_hdr
== NULL
)
3900 /* If we don't have a segment yet, then we don't need a new
3901 one (we build the last one after this loop). */
3902 new_segment
= FALSE
;
3904 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3906 /* If this section has a different relation between the
3907 virtual address and the load address, then we need a new
3911 else if (hdr
->lma
< last_hdr
->lma
+ last_size
3912 || last_hdr
->lma
+ last_size
< last_hdr
->lma
)
3914 /* If this section has a load address that makes it overlap
3915 the previous section, then we need a new segment. */
3918 /* In the next test we have to be careful when last_hdr->lma is close
3919 to the end of the address space. If the aligned address wraps
3920 around to the start of the address space, then there are no more
3921 pages left in memory and it is OK to assume that the current
3922 section can be included in the current segment. */
3923 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3925 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3928 /* If putting this section in this segment would force us to
3929 skip a page in the segment, then we need a new segment. */
3932 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3933 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3935 /* We don't want to put a loadable section after a
3936 nonloadable section in the same segment.
3937 Consider .tbss sections as loadable for this purpose. */
3940 else if ((abfd
->flags
& D_PAGED
) == 0)
3942 /* If the file is not demand paged, which means that we
3943 don't require the sections to be correctly aligned in the
3944 file, then there is no other reason for a new segment. */
3945 new_segment
= FALSE
;
3948 && (hdr
->flags
& SEC_READONLY
) == 0
3949 && (((last_hdr
->lma
+ last_size
- 1) & -maxpagesize
)
3950 != (hdr
->lma
& -maxpagesize
)))
3952 /* We don't want to put a writable section in a read only
3953 segment, unless they are on the same page in memory
3954 anyhow. We already know that the last section does not
3955 bring us past the current section on the page, so the
3956 only case in which the new section is not on the same
3957 page as the previous section is when the previous section
3958 ends precisely on a page boundary. */
3963 /* Otherwise, we can use the same segment. */
3964 new_segment
= FALSE
;
3967 /* Allow interested parties a chance to override our decision. */
3968 if (last_hdr
!= NULL
3970 && info
->callbacks
->override_segment_assignment
!= NULL
)
3972 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
3978 if ((hdr
->flags
& SEC_READONLY
) == 0)
3981 /* .tbss sections effectively have zero size. */
3982 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3983 != SEC_THREAD_LOCAL
)
3984 last_size
= hdr
->size
;
3990 /* We need a new program segment. We must create a new program
3991 header holding all the sections from phdr_index until hdr. */
3993 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4000 if ((hdr
->flags
& SEC_READONLY
) == 0)
4006 /* .tbss sections effectively have zero size. */
4007 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
4008 last_size
= hdr
->size
;
4012 phdr_in_segment
= FALSE
;
4015 /* Create a final PT_LOAD program segment, but not if it's just
4017 if (last_hdr
!= NULL
4018 && (i
- phdr_index
!= 1
4019 || ((last_hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
4020 != SEC_THREAD_LOCAL
)))
4022 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
4030 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
4033 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
4040 /* For each batch of consecutive loadable .note sections,
4041 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
4042 because if we link together nonloadable .note sections and
4043 loadable .note sections, we will generate two .note sections
4044 in the output file. FIXME: Using names for section types is
4046 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4048 if ((s
->flags
& SEC_LOAD
) != 0
4049 && CONST_STRNEQ (s
->name
, ".note"))
4054 amt
= sizeof (struct elf_segment_map
);
4055 if (s
->alignment_power
== 2)
4056 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
4058 if (s2
->next
->alignment_power
== 2
4059 && (s2
->next
->flags
& SEC_LOAD
) != 0
4060 && CONST_STRNEQ (s2
->next
->name
, ".note")
4061 && align_power (s2
->lma
+ s2
->size
, 2)
4067 amt
+= (count
- 1) * sizeof (asection
*);
4068 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4072 m
->p_type
= PT_NOTE
;
4076 m
->sections
[m
->count
- count
--] = s
;
4077 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4080 m
->sections
[m
->count
- 1] = s
;
4081 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
4085 if (s
->flags
& SEC_THREAD_LOCAL
)
4093 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
4096 amt
= sizeof (struct elf_segment_map
);
4097 amt
+= (tls_count
- 1) * sizeof (asection
*);
4098 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4103 m
->count
= tls_count
;
4104 /* Mandated PF_R. */
4106 m
->p_flags_valid
= 1;
4107 for (i
= 0; i
< (unsigned int) tls_count
; ++i
)
4109 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
4110 m
->sections
[i
] = first_tls
;
4111 first_tls
= first_tls
->next
;
4118 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4120 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
4121 if (eh_frame_hdr
!= NULL
4122 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
4124 amt
= sizeof (struct elf_segment_map
);
4125 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4129 m
->p_type
= PT_GNU_EH_FRAME
;
4131 m
->sections
[0] = eh_frame_hdr
->output_section
;
4137 if (elf_tdata (abfd
)->stack_flags
)
4139 amt
= sizeof (struct elf_segment_map
);
4140 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4144 m
->p_type
= PT_GNU_STACK
;
4145 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
4146 m
->p_align
= bed
->stack_align
;
4147 m
->p_flags_valid
= 1;
4148 m
->p_align_valid
= m
->p_align
!= 0;
4149 if (info
->stacksize
> 0)
4151 m
->p_size
= info
->stacksize
;
4152 m
->p_size_valid
= 1;
4159 if (info
!= NULL
&& info
->relro
)
4161 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4163 if (m
->p_type
== PT_LOAD
4165 && m
->sections
[0]->vma
>= info
->relro_start
4166 && m
->sections
[0]->vma
< info
->relro_end
)
4169 while (--i
!= (unsigned) -1)
4170 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
))
4171 == (SEC_LOAD
| SEC_HAS_CONTENTS
))
4174 if (i
== (unsigned) -1)
4177 if (m
->sections
[i
]->vma
+ m
->sections
[i
]->size
4183 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
4186 amt
= sizeof (struct elf_segment_map
);
4187 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
4191 m
->p_type
= PT_GNU_RELRO
;
4193 m
->p_flags_valid
= 1;
4201 elf_tdata (abfd
)->segment_map
= mfirst
;
4204 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
4207 for (count
= 0, m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4209 elf_tdata (abfd
)->program_header_size
= count
* bed
->s
->sizeof_phdr
;
4214 if (sections
!= NULL
)
4219 /* Sort sections by address. */
4222 elf_sort_sections (const void *arg1
, const void *arg2
)
4224 const asection
*sec1
= *(const asection
**) arg1
;
4225 const asection
*sec2
= *(const asection
**) arg2
;
4226 bfd_size_type size1
, size2
;
4228 /* Sort by LMA first, since this is the address used to
4229 place the section into a segment. */
4230 if (sec1
->lma
< sec2
->lma
)
4232 else if (sec1
->lma
> sec2
->lma
)
4235 /* Then sort by VMA. Normally the LMA and the VMA will be
4236 the same, and this will do nothing. */
4237 if (sec1
->vma
< sec2
->vma
)
4239 else if (sec1
->vma
> sec2
->vma
)
4242 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4244 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4250 /* If the indicies are the same, do not return 0
4251 here, but continue to try the next comparison. */
4252 if (sec1
->target_index
- sec2
->target_index
!= 0)
4253 return sec1
->target_index
- sec2
->target_index
;
4258 else if (TOEND (sec2
))
4263 /* Sort by size, to put zero sized sections
4264 before others at the same address. */
4266 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4267 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4274 return sec1
->target_index
- sec2
->target_index
;
4277 /* Ian Lance Taylor writes:
4279 We shouldn't be using % with a negative signed number. That's just
4280 not good. We have to make sure either that the number is not
4281 negative, or that the number has an unsigned type. When the types
4282 are all the same size they wind up as unsigned. When file_ptr is a
4283 larger signed type, the arithmetic winds up as signed long long,
4286 What we're trying to say here is something like ``increase OFF by
4287 the least amount that will cause it to be equal to the VMA modulo
4289 /* In other words, something like:
4291 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4292 off_offset = off % bed->maxpagesize;
4293 if (vma_offset < off_offset)
4294 adjustment = vma_offset + bed->maxpagesize - off_offset;
4296 adjustment = vma_offset - off_offset;
4298 which can can be collapsed into the expression below. */
4301 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4303 return ((vma
- off
) % maxpagesize
);
4307 print_segment_map (const struct elf_segment_map
*m
)
4310 const char *pt
= get_segment_type (m
->p_type
);
4315 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4316 sprintf (buf
, "LOPROC+%7.7x",
4317 (unsigned int) (m
->p_type
- PT_LOPROC
));
4318 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4319 sprintf (buf
, "LOOS+%7.7x",
4320 (unsigned int) (m
->p_type
- PT_LOOS
));
4322 snprintf (buf
, sizeof (buf
), "%8.8x",
4323 (unsigned int) m
->p_type
);
4327 fprintf (stderr
, "%s:", pt
);
4328 for (j
= 0; j
< m
->count
; j
++)
4329 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4335 write_zeros (bfd
*abfd
, file_ptr pos
, bfd_size_type len
)
4340 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
4342 buf
= bfd_zmalloc (len
);
4345 ret
= bfd_bwrite (buf
, len
, abfd
) == len
;
4350 /* Assign file positions to the sections based on the mapping from
4351 sections to segments. This function also sets up some fields in
4355 assign_file_positions_for_load_sections (bfd
*abfd
,
4356 struct bfd_link_info
*link_info
)
4358 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4359 struct elf_segment_map
*m
;
4360 Elf_Internal_Phdr
*phdrs
;
4361 Elf_Internal_Phdr
*p
;
4363 bfd_size_type maxpagesize
;
4366 bfd_vma header_pad
= 0;
4368 if (link_info
== NULL
4369 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4373 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4377 header_pad
= m
->header_size
;
4382 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4383 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4387 /* PR binutils/12467. */
4388 elf_elfheader (abfd
)->e_phoff
= 0;
4389 elf_elfheader (abfd
)->e_phentsize
= 0;
4392 elf_elfheader (abfd
)->e_phnum
= alloc
;
4394 if (elf_tdata (abfd
)->program_header_size
== (bfd_size_type
) -1)
4395 elf_tdata (abfd
)->program_header_size
= alloc
* bed
->s
->sizeof_phdr
;
4397 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
4398 >= alloc
* bed
->s
->sizeof_phdr
);
4402 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
4406 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4407 see assign_file_positions_except_relocs, so make sure we have
4408 that amount allocated, with trailing space cleared.
4409 The variable alloc contains the computed need, while elf_tdata
4410 (abfd)->program_header_size contains the size used for the
4412 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4413 where the layout is forced to according to a larger size in the
4414 last iterations for the testcase ld-elf/header. */
4415 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
% bed
->s
->sizeof_phdr
4417 phdrs
= (Elf_Internal_Phdr
*)
4419 (elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
),
4420 sizeof (Elf_Internal_Phdr
));
4421 elf_tdata (abfd
)->phdr
= phdrs
;
4426 if ((abfd
->flags
& D_PAGED
) != 0)
4427 maxpagesize
= bed
->maxpagesize
;
4429 off
= bed
->s
->sizeof_ehdr
;
4430 off
+= alloc
* bed
->s
->sizeof_phdr
;
4431 if (header_pad
< (bfd_vma
) off
)
4437 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
, j
= 0;
4439 m
= m
->next
, p
++, j
++)
4443 bfd_boolean no_contents
;
4445 /* If elf_segment_map is not from map_sections_to_segments, the
4446 sections may not be correctly ordered. NOTE: sorting should
4447 not be done to the PT_NOTE section of a corefile, which may
4448 contain several pseudo-sections artificially created by bfd.
4449 Sorting these pseudo-sections breaks things badly. */
4451 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4452 && m
->p_type
== PT_NOTE
))
4453 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4456 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4457 number of sections with contents contributing to both p_filesz
4458 and p_memsz, followed by a number of sections with no contents
4459 that just contribute to p_memsz. In this loop, OFF tracks next
4460 available file offset for PT_LOAD and PT_NOTE segments. */
4461 p
->p_type
= m
->p_type
;
4462 p
->p_flags
= m
->p_flags
;
4467 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4469 if (m
->p_paddr_valid
)
4470 p
->p_paddr
= m
->p_paddr
;
4471 else if (m
->count
== 0)
4474 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4476 if (p
->p_type
== PT_LOAD
4477 && (abfd
->flags
& D_PAGED
) != 0)
4479 /* p_align in demand paged PT_LOAD segments effectively stores
4480 the maximum page size. When copying an executable with
4481 objcopy, we set m->p_align from the input file. Use this
4482 value for maxpagesize rather than bed->maxpagesize, which
4483 may be different. Note that we use maxpagesize for PT_TLS
4484 segment alignment later in this function, so we are relying
4485 on at least one PT_LOAD segment appearing before a PT_TLS
4487 if (m
->p_align_valid
)
4488 maxpagesize
= m
->p_align
;
4490 p
->p_align
= maxpagesize
;
4492 else if (m
->p_align_valid
)
4493 p
->p_align
= m
->p_align
;
4494 else if (m
->count
== 0)
4495 p
->p_align
= 1 << bed
->s
->log_file_align
;
4499 no_contents
= FALSE
;
4501 if (p
->p_type
== PT_LOAD
4504 bfd_size_type align
;
4505 unsigned int align_power
= 0;
4507 if (m
->p_align_valid
)
4511 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4513 unsigned int secalign
;
4515 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4516 if (secalign
> align_power
)
4517 align_power
= secalign
;
4519 align
= (bfd_size_type
) 1 << align_power
;
4520 if (align
< maxpagesize
)
4521 align
= maxpagesize
;
4524 for (i
= 0; i
< m
->count
; i
++)
4525 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4526 /* If we aren't making room for this section, then
4527 it must be SHT_NOBITS regardless of what we've
4528 set via struct bfd_elf_special_section. */
4529 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4531 /* Find out whether this segment contains any loadable
4534 for (i
= 0; i
< m
->count
; i
++)
4535 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4537 no_contents
= FALSE
;
4541 off_adjust
= vma_page_aligned_bias (p
->p_vaddr
, off
, align
);
4545 /* We shouldn't need to align the segment on disk since
4546 the segment doesn't need file space, but the gABI
4547 arguably requires the alignment and glibc ld.so
4548 checks it. So to comply with the alignment
4549 requirement but not waste file space, we adjust
4550 p_offset for just this segment. (OFF_ADJUST is
4551 subtracted from OFF later.) This may put p_offset
4552 past the end of file, but that shouldn't matter. */
4557 /* Make sure the .dynamic section is the first section in the
4558 PT_DYNAMIC segment. */
4559 else if (p
->p_type
== PT_DYNAMIC
4561 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4564 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4566 bfd_set_error (bfd_error_bad_value
);
4569 /* Set the note section type to SHT_NOTE. */
4570 else if (p
->p_type
== PT_NOTE
)
4571 for (i
= 0; i
< m
->count
; i
++)
4572 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4578 if (m
->includes_filehdr
)
4580 if (!m
->p_flags_valid
)
4582 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4583 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4586 if (p
->p_vaddr
< (bfd_vma
) off
)
4588 (*_bfd_error_handler
)
4589 (_("%B: Not enough room for program headers, try linking with -N"),
4591 bfd_set_error (bfd_error_bad_value
);
4596 if (!m
->p_paddr_valid
)
4601 if (m
->includes_phdrs
)
4603 if (!m
->p_flags_valid
)
4606 if (!m
->includes_filehdr
)
4608 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4612 p
->p_vaddr
-= off
- p
->p_offset
;
4613 if (!m
->p_paddr_valid
)
4614 p
->p_paddr
-= off
- p
->p_offset
;
4618 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4619 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4622 p
->p_filesz
+= header_pad
;
4623 p
->p_memsz
+= header_pad
;
4627 if (p
->p_type
== PT_LOAD
4628 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4630 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4636 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4638 p
->p_filesz
+= adjust
;
4639 p
->p_memsz
+= adjust
;
4643 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4644 maps. Set filepos for sections in PT_LOAD segments, and in
4645 core files, for sections in PT_NOTE segments.
4646 assign_file_positions_for_non_load_sections will set filepos
4647 for other sections and update p_filesz for other segments. */
4648 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4651 bfd_size_type align
;
4652 Elf_Internal_Shdr
*this_hdr
;
4655 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4656 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4658 if ((p
->p_type
== PT_LOAD
4659 || p
->p_type
== PT_TLS
)
4660 && (this_hdr
->sh_type
!= SHT_NOBITS
4661 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4662 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4663 || p
->p_type
== PT_TLS
))))
4665 bfd_vma p_start
= p
->p_paddr
;
4666 bfd_vma p_end
= p_start
+ p
->p_memsz
;
4667 bfd_vma s_start
= sec
->lma
;
4668 bfd_vma adjust
= s_start
- p_end
;
4672 || p_end
< p_start
))
4674 (*_bfd_error_handler
)
4675 (_("%B: section %A lma %#lx adjusted to %#lx"), abfd
, sec
,
4676 (unsigned long) s_start
, (unsigned long) p_end
);
4680 p
->p_memsz
+= adjust
;
4682 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4684 if (p
->p_filesz
+ adjust
< p
->p_memsz
)
4686 /* We have a PROGBITS section following NOBITS ones.
4687 Allocate file space for the NOBITS section(s) and
4689 adjust
= p
->p_memsz
- p
->p_filesz
;
4690 if (!write_zeros (abfd
, off
, adjust
))
4694 p
->p_filesz
+= adjust
;
4698 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4700 /* The section at i == 0 is the one that actually contains
4704 this_hdr
->sh_offset
= sec
->filepos
= off
;
4705 off
+= this_hdr
->sh_size
;
4706 p
->p_filesz
= this_hdr
->sh_size
;
4712 /* The rest are fake sections that shouldn't be written. */
4721 if (p
->p_type
== PT_LOAD
)
4723 this_hdr
->sh_offset
= sec
->filepos
= off
;
4724 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4725 off
+= this_hdr
->sh_size
;
4727 else if (this_hdr
->sh_type
== SHT_NOBITS
4728 && (this_hdr
->sh_flags
& SHF_TLS
) != 0
4729 && this_hdr
->sh_offset
== 0)
4731 /* This is a .tbss section that didn't get a PT_LOAD.
4732 (See _bfd_elf_map_sections_to_segments "Create a
4733 final PT_LOAD".) Set sh_offset to the value it
4734 would have if we had created a zero p_filesz and
4735 p_memsz PT_LOAD header for the section. This
4736 also makes the PT_TLS header have the same
4738 bfd_vma adjust
= vma_page_aligned_bias (this_hdr
->sh_addr
,
4740 this_hdr
->sh_offset
= sec
->filepos
= off
+ adjust
;
4743 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4745 p
->p_filesz
+= this_hdr
->sh_size
;
4746 /* A load section without SHF_ALLOC is something like
4747 a note section in a PT_NOTE segment. These take
4748 file space but are not loaded into memory. */
4749 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4750 p
->p_memsz
+= this_hdr
->sh_size
;
4752 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4754 if (p
->p_type
== PT_TLS
)
4755 p
->p_memsz
+= this_hdr
->sh_size
;
4757 /* .tbss is special. It doesn't contribute to p_memsz of
4759 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4760 p
->p_memsz
+= this_hdr
->sh_size
;
4763 if (align
> p
->p_align
4764 && !m
->p_align_valid
4765 && (p
->p_type
!= PT_LOAD
4766 || (abfd
->flags
& D_PAGED
) == 0))
4770 if (!m
->p_flags_valid
)
4773 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4775 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4781 /* Check that all sections are in a PT_LOAD segment.
4782 Don't check funky gdb generated core files. */
4783 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4785 bfd_boolean check_vma
= TRUE
;
4787 for (i
= 1; i
< m
->count
; i
++)
4788 if (m
->sections
[i
]->vma
== m
->sections
[i
- 1]->vma
4789 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
])
4790 ->this_hdr
), p
) != 0
4791 && ELF_SECTION_SIZE (&(elf_section_data (m
->sections
[i
- 1])
4792 ->this_hdr
), p
) != 0)
4794 /* Looks like we have overlays packed into the segment. */
4799 for (i
= 0; i
< m
->count
; i
++)
4801 Elf_Internal_Shdr
*this_hdr
;
4804 sec
= m
->sections
[i
];
4805 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4806 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr
, p
, check_vma
, 0)
4807 && !ELF_TBSS_SPECIAL (this_hdr
, p
))
4809 (*_bfd_error_handler
)
4810 (_("%B: section `%A' can't be allocated in segment %d"),
4812 print_segment_map (m
);
4818 elf_tdata (abfd
)->next_file_pos
= off
;
4822 /* Assign file positions for the other sections. */
4825 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4826 struct bfd_link_info
*link_info
)
4828 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4829 Elf_Internal_Shdr
**i_shdrpp
;
4830 Elf_Internal_Shdr
**hdrpp
;
4831 Elf_Internal_Phdr
*phdrs
;
4832 Elf_Internal_Phdr
*p
;
4833 struct elf_segment_map
*m
;
4834 struct elf_segment_map
*hdrs_segment
;
4835 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4836 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4838 unsigned int num_sec
;
4842 i_shdrpp
= elf_elfsections (abfd
);
4843 num_sec
= elf_numsections (abfd
);
4844 off
= elf_tdata (abfd
)->next_file_pos
;
4845 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4847 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4848 Elf_Internal_Shdr
*hdr
;
4851 if (hdr
->bfd_section
!= NULL
4852 && (hdr
->bfd_section
->filepos
!= 0
4853 || (hdr
->sh_type
== SHT_NOBITS
4854 && hdr
->contents
== NULL
)))
4855 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4856 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4858 if (hdr
->sh_size
!= 0)
4859 (*_bfd_error_handler
)
4860 (_("%B: warning: allocated section `%s' not in segment"),
4862 (hdr
->bfd_section
== NULL
4864 : hdr
->bfd_section
->name
));
4865 /* We don't need to page align empty sections. */
4866 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4867 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4870 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4872 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4875 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4876 && hdr
->bfd_section
== NULL
)
4877 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4878 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4879 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4880 hdr
->sh_offset
= -1;
4882 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4885 /* Now that we have set the section file positions, we can set up
4886 the file positions for the non PT_LOAD segments. */
4890 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4892 hdrs_segment
= NULL
;
4893 phdrs
= elf_tdata (abfd
)->phdr
;
4894 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4899 if (p
->p_type
!= PT_LOAD
)
4902 if (m
->includes_filehdr
)
4904 filehdr_vaddr
= p
->p_vaddr
;
4905 filehdr_paddr
= p
->p_paddr
;
4907 if (m
->includes_phdrs
)
4909 phdrs_vaddr
= p
->p_vaddr
;
4910 phdrs_paddr
= p
->p_paddr
;
4911 if (m
->includes_filehdr
)
4914 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4915 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4920 if (hdrs_segment
!= NULL
&& link_info
!= NULL
)
4922 /* There is a segment that contains both the file headers and the
4923 program headers, so provide a symbol __ehdr_start pointing there.
4924 A program can use this to examine itself robustly. */
4926 struct elf_link_hash_entry
*hash
4927 = elf_link_hash_lookup (elf_hash_table (link_info
), "__ehdr_start",
4928 FALSE
, FALSE
, TRUE
);
4929 /* If the symbol was referenced and not defined, define it. */
4931 && (hash
->root
.type
== bfd_link_hash_new
4932 || hash
->root
.type
== bfd_link_hash_undefined
4933 || hash
->root
.type
== bfd_link_hash_undefweak
4934 || hash
->root
.type
== bfd_link_hash_common
))
4937 if (hdrs_segment
->count
!= 0)
4938 /* The segment contains sections, so use the first one. */
4939 s
= hdrs_segment
->sections
[0];
4941 /* Use the first (i.e. lowest-addressed) section in any segment. */
4942 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4951 hash
->root
.u
.def
.value
= filehdr_vaddr
- s
->vma
;
4952 hash
->root
.u
.def
.section
= s
;
4956 hash
->root
.u
.def
.value
= filehdr_vaddr
;
4957 hash
->root
.u
.def
.section
= bfd_abs_section_ptr
;
4960 hash
->root
.type
= bfd_link_hash_defined
;
4961 hash
->def_regular
= 1;
4966 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4970 if (p
->p_type
== PT_GNU_RELRO
)
4972 const Elf_Internal_Phdr
*lp
;
4973 struct elf_segment_map
*lm
;
4975 if (link_info
!= NULL
)
4977 /* During linking the range of the RELRO segment is passed
4979 for (lm
= elf_tdata (abfd
)->segment_map
, lp
= phdrs
;
4981 lm
= lm
->next
, lp
++)
4983 if (lp
->p_type
== PT_LOAD
4984 && lp
->p_vaddr
< link_info
->relro_end
4985 && lp
->p_vaddr
+ lp
->p_filesz
>= link_info
->relro_end
4987 && lm
->sections
[0]->vma
>= link_info
->relro_start
)
4991 /* PR ld/14207. If the RELRO segment doesn't fit in the
4992 LOAD segment, it should be removed. */
4993 BFD_ASSERT (lm
!= NULL
);
4997 /* Otherwise we are copying an executable or shared
4998 library, but we need to use the same linker logic. */
4999 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
5001 if (lp
->p_type
== PT_LOAD
5002 && lp
->p_paddr
== p
->p_paddr
)
5007 if (lp
< phdrs
+ count
)
5009 p
->p_vaddr
= lp
->p_vaddr
;
5010 p
->p_paddr
= lp
->p_paddr
;
5011 p
->p_offset
= lp
->p_offset
;
5012 if (link_info
!= NULL
)
5013 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
5014 else if (m
->p_size_valid
)
5015 p
->p_filesz
= m
->p_size
;
5018 p
->p_memsz
= p
->p_filesz
;
5019 /* Preserve the alignment and flags if they are valid. The
5020 gold linker generates RW/4 for the PT_GNU_RELRO section.
5021 It is better for objcopy/strip to honor these attributes
5022 otherwise gdb will choke when using separate debug files.
5024 if (!m
->p_align_valid
)
5026 if (!m
->p_flags_valid
)
5027 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
5031 memset (p
, 0, sizeof *p
);
5032 p
->p_type
= PT_NULL
;
5035 else if (p
->p_type
== PT_GNU_STACK
)
5037 if (m
->p_size_valid
)
5038 p
->p_memsz
= m
->p_size
;
5040 else if (m
->count
!= 0)
5042 if (p
->p_type
!= PT_LOAD
5043 && (p
->p_type
!= PT_NOTE
5044 || bfd_get_format (abfd
) != bfd_core
))
5046 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
5049 p
->p_offset
= m
->sections
[0]->filepos
;
5050 for (i
= m
->count
; i
-- != 0;)
5052 asection
*sect
= m
->sections
[i
];
5053 Elf_Internal_Shdr
*hdr
= &elf_section_data (sect
)->this_hdr
;
5054 if (hdr
->sh_type
!= SHT_NOBITS
)
5056 p
->p_filesz
= (sect
->filepos
- m
->sections
[0]->filepos
5063 else if (m
->includes_filehdr
)
5065 p
->p_vaddr
= filehdr_vaddr
;
5066 if (! m
->p_paddr_valid
)
5067 p
->p_paddr
= filehdr_paddr
;
5069 else if (m
->includes_phdrs
)
5071 p
->p_vaddr
= phdrs_vaddr
;
5072 if (! m
->p_paddr_valid
)
5073 p
->p_paddr
= phdrs_paddr
;
5077 elf_tdata (abfd
)->next_file_pos
= off
;
5082 /* Work out the file positions of all the sections. This is called by
5083 _bfd_elf_compute_section_file_positions. All the section sizes and
5084 VMAs must be known before this is called.
5086 Reloc sections come in two flavours: Those processed specially as
5087 "side-channel" data attached to a section to which they apply, and
5088 those that bfd doesn't process as relocations. The latter sort are
5089 stored in a normal bfd section by bfd_section_from_shdr. We don't
5090 consider the former sort here, unless they form part of the loadable
5091 image. Reloc sections not assigned here will be handled later by
5092 assign_file_positions_for_relocs.
5094 We also don't set the positions of the .symtab and .strtab here. */
5097 assign_file_positions_except_relocs (bfd
*abfd
,
5098 struct bfd_link_info
*link_info
)
5100 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
5101 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
5103 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5105 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
5106 && bfd_get_format (abfd
) != bfd_core
)
5108 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
5109 unsigned int num_sec
= elf_numsections (abfd
);
5110 Elf_Internal_Shdr
**hdrpp
;
5113 /* Start after the ELF header. */
5114 off
= i_ehdrp
->e_ehsize
;
5116 /* We are not creating an executable, which means that we are
5117 not creating a program header, and that the actual order of
5118 the sections in the file is unimportant. */
5119 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
5121 Elf_Internal_Shdr
*hdr
;
5124 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
5125 && hdr
->bfd_section
== NULL
)
5126 || i
== tdata
->symtab_section
5127 || i
== tdata
->symtab_shndx_section
5128 || i
== tdata
->strtab_section
)
5130 hdr
->sh_offset
= -1;
5133 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
5140 /* Assign file positions for the loaded sections based on the
5141 assignment of sections to segments. */
5142 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
5145 /* And for non-load sections. */
5146 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
5149 if (bed
->elf_backend_modify_program_headers
!= NULL
)
5151 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
5155 /* Write out the program headers. */
5156 alloc
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
5157 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
5158 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
5161 off
= tdata
->next_file_pos
;
5164 /* Place the section headers. */
5165 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
5166 i_ehdrp
->e_shoff
= off
;
5167 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
5169 tdata
->next_file_pos
= off
;
5175 prep_headers (bfd
*abfd
)
5177 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form. */
5178 struct elf_strtab_hash
*shstrtab
;
5179 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5181 i_ehdrp
= elf_elfheader (abfd
);
5183 shstrtab
= _bfd_elf_strtab_init ();
5184 if (shstrtab
== NULL
)
5187 elf_shstrtab (abfd
) = shstrtab
;
5189 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
5190 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
5191 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
5192 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
5194 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
5195 i_ehdrp
->e_ident
[EI_DATA
] =
5196 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
5197 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
5199 if ((abfd
->flags
& DYNAMIC
) != 0)
5200 i_ehdrp
->e_type
= ET_DYN
;
5201 else if ((abfd
->flags
& EXEC_P
) != 0)
5202 i_ehdrp
->e_type
= ET_EXEC
;
5203 else if (bfd_get_format (abfd
) == bfd_core
)
5204 i_ehdrp
->e_type
= ET_CORE
;
5206 i_ehdrp
->e_type
= ET_REL
;
5208 switch (bfd_get_arch (abfd
))
5210 case bfd_arch_unknown
:
5211 i_ehdrp
->e_machine
= EM_NONE
;
5214 /* There used to be a long list of cases here, each one setting
5215 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5216 in the corresponding bfd definition. To avoid duplication,
5217 the switch was removed. Machines that need special handling
5218 can generally do it in elf_backend_final_write_processing(),
5219 unless they need the information earlier than the final write.
5220 Such need can generally be supplied by replacing the tests for
5221 e_machine with the conditions used to determine it. */
5223 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
5226 i_ehdrp
->e_version
= bed
->s
->ev_current
;
5227 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
5229 /* No program header, for now. */
5230 i_ehdrp
->e_phoff
= 0;
5231 i_ehdrp
->e_phentsize
= 0;
5232 i_ehdrp
->e_phnum
= 0;
5234 /* Each bfd section is section header entry. */
5235 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
5236 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
5238 /* If we're building an executable, we'll need a program header table. */
5239 if (abfd
->flags
& EXEC_P
)
5240 /* It all happens later. */
5244 i_ehdrp
->e_phentsize
= 0;
5245 i_ehdrp
->e_phoff
= 0;
5248 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
5249 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
5250 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
5251 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
5252 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
5253 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
5254 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5255 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
5256 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
5262 /* Assign file positions for all the reloc sections which are not part
5263 of the loadable file image. */
5266 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
5269 unsigned int i
, num_sec
;
5270 Elf_Internal_Shdr
**shdrpp
;
5272 off
= elf_tdata (abfd
)->next_file_pos
;
5274 num_sec
= elf_numsections (abfd
);
5275 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
5277 Elf_Internal_Shdr
*shdrp
;
5280 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
5281 && shdrp
->sh_offset
== -1)
5282 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
5285 elf_tdata (abfd
)->next_file_pos
= off
;
5289 _bfd_elf_write_object_contents (bfd
*abfd
)
5291 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5292 Elf_Internal_Shdr
**i_shdrp
;
5294 unsigned int count
, num_sec
;
5296 if (! abfd
->output_has_begun
5297 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
5300 i_shdrp
= elf_elfsections (abfd
);
5303 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
5307 _bfd_elf_assign_file_positions_for_relocs (abfd
);
5309 /* After writing the headers, we need to write the sections too... */
5310 num_sec
= elf_numsections (abfd
);
5311 for (count
= 1; count
< num_sec
; count
++)
5313 if (bed
->elf_backend_section_processing
)
5314 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
5315 if (i_shdrp
[count
]->contents
)
5317 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
5319 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
5320 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
5325 /* Write out the section header names. */
5326 if (elf_shstrtab (abfd
) != NULL
5327 && (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
5328 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
5331 if (bed
->elf_backend_final_write_processing
)
5332 (*bed
->elf_backend_final_write_processing
) (abfd
,
5333 elf_tdata (abfd
)->linker
);
5335 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
5338 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
5339 if (elf_tdata (abfd
)->after_write_object_contents
)
5340 return (*elf_tdata (abfd
)->after_write_object_contents
) (abfd
);
5346 _bfd_elf_write_corefile_contents (bfd
*abfd
)
5348 /* Hopefully this can be done just like an object file. */
5349 return _bfd_elf_write_object_contents (abfd
);
5352 /* Given a section, search the header to find them. */
5355 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5357 const struct elf_backend_data
*bed
;
5358 unsigned int sec_index
;
5360 if (elf_section_data (asect
) != NULL
5361 && elf_section_data (asect
)->this_idx
!= 0)
5362 return elf_section_data (asect
)->this_idx
;
5364 if (bfd_is_abs_section (asect
))
5365 sec_index
= SHN_ABS
;
5366 else if (bfd_is_com_section (asect
))
5367 sec_index
= SHN_COMMON
;
5368 else if (bfd_is_und_section (asect
))
5369 sec_index
= SHN_UNDEF
;
5371 sec_index
= SHN_BAD
;
5373 bed
= get_elf_backend_data (abfd
);
5374 if (bed
->elf_backend_section_from_bfd_section
)
5376 int retval
= sec_index
;
5378 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5382 if (sec_index
== SHN_BAD
)
5383 bfd_set_error (bfd_error_nonrepresentable_section
);
5388 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5392 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5394 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5396 flagword flags
= asym_ptr
->flags
;
5398 /* When gas creates relocations against local labels, it creates its
5399 own symbol for the section, but does put the symbol into the
5400 symbol chain, so udata is 0. When the linker is generating
5401 relocatable output, this section symbol may be for one of the
5402 input sections rather than the output section. */
5403 if (asym_ptr
->udata
.i
== 0
5404 && (flags
& BSF_SECTION_SYM
)
5405 && asym_ptr
->section
)
5410 sec
= asym_ptr
->section
;
5411 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5412 sec
= sec
->output_section
;
5413 if (sec
->owner
== abfd
5414 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5415 && elf_section_syms (abfd
)[indx
] != NULL
)
5416 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5419 idx
= asym_ptr
->udata
.i
;
5423 /* This case can occur when using --strip-symbol on a symbol
5424 which is used in a relocation entry. */
5425 (*_bfd_error_handler
)
5426 (_("%B: symbol `%s' required but not present"),
5427 abfd
, bfd_asymbol_name (asym_ptr
));
5428 bfd_set_error (bfd_error_no_symbols
);
5435 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5436 (long) asym_ptr
, asym_ptr
->name
, idx
, (long) flags
);
5444 /* Rewrite program header information. */
5447 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5449 Elf_Internal_Ehdr
*iehdr
;
5450 struct elf_segment_map
*map
;
5451 struct elf_segment_map
*map_first
;
5452 struct elf_segment_map
**pointer_to_map
;
5453 Elf_Internal_Phdr
*segment
;
5456 unsigned int num_segments
;
5457 bfd_boolean phdr_included
= FALSE
;
5458 bfd_boolean p_paddr_valid
;
5459 bfd_vma maxpagesize
;
5460 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5461 unsigned int phdr_adjust_num
= 0;
5462 const struct elf_backend_data
*bed
;
5464 bed
= get_elf_backend_data (ibfd
);
5465 iehdr
= elf_elfheader (ibfd
);
5468 pointer_to_map
= &map_first
;
5470 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5471 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5473 /* Returns the end address of the segment + 1. */
5474 #define SEGMENT_END(segment, start) \
5475 (start + (segment->p_memsz > segment->p_filesz \
5476 ? segment->p_memsz : segment->p_filesz))
5478 #define SECTION_SIZE(section, segment) \
5479 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5480 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5481 ? section->size : 0)
5483 /* Returns TRUE if the given section is contained within
5484 the given segment. VMA addresses are compared. */
5485 #define IS_CONTAINED_BY_VMA(section, segment) \
5486 (section->vma >= segment->p_vaddr \
5487 && (section->vma + SECTION_SIZE (section, segment) \
5488 <= (SEGMENT_END (segment, segment->p_vaddr))))
5490 /* Returns TRUE if the given section is contained within
5491 the given segment. LMA addresses are compared. */
5492 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5493 (section->lma >= base \
5494 && (section->lma + SECTION_SIZE (section, segment) \
5495 <= SEGMENT_END (segment, base)))
5497 /* Handle PT_NOTE segment. */
5498 #define IS_NOTE(p, s) \
5499 (p->p_type == PT_NOTE \
5500 && elf_section_type (s) == SHT_NOTE \
5501 && (bfd_vma) s->filepos >= p->p_offset \
5502 && ((bfd_vma) s->filepos + s->size \
5503 <= p->p_offset + p->p_filesz))
5505 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5507 #define IS_COREFILE_NOTE(p, s) \
5509 && bfd_get_format (ibfd) == bfd_core \
5513 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5514 linker, which generates a PT_INTERP section with p_vaddr and
5515 p_memsz set to 0. */
5516 #define IS_SOLARIS_PT_INTERP(p, s) \
5518 && p->p_paddr == 0 \
5519 && p->p_memsz == 0 \
5520 && p->p_filesz > 0 \
5521 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5523 && (bfd_vma) s->filepos >= p->p_offset \
5524 && ((bfd_vma) s->filepos + s->size \
5525 <= p->p_offset + p->p_filesz))
5527 /* Decide if the given section should be included in the given segment.
5528 A section will be included if:
5529 1. It is within the address space of the segment -- we use the LMA
5530 if that is set for the segment and the VMA otherwise,
5531 2. It is an allocated section or a NOTE section in a PT_NOTE
5533 3. There is an output section associated with it,
5534 4. The section has not already been allocated to a previous segment.
5535 5. PT_GNU_STACK segments do not include any sections.
5536 6. PT_TLS segment includes only SHF_TLS sections.
5537 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5538 8. PT_DYNAMIC should not contain empty sections at the beginning
5539 (with the possible exception of .dynamic). */
5540 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5541 ((((segment->p_paddr \
5542 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5543 : IS_CONTAINED_BY_VMA (section, segment)) \
5544 && (section->flags & SEC_ALLOC) != 0) \
5545 || IS_NOTE (segment, section)) \
5546 && segment->p_type != PT_GNU_STACK \
5547 && (segment->p_type != PT_TLS \
5548 || (section->flags & SEC_THREAD_LOCAL)) \
5549 && (segment->p_type == PT_LOAD \
5550 || segment->p_type == PT_TLS \
5551 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5552 && (segment->p_type != PT_DYNAMIC \
5553 || SECTION_SIZE (section, segment) > 0 \
5554 || (segment->p_paddr \
5555 ? segment->p_paddr != section->lma \
5556 : segment->p_vaddr != section->vma) \
5557 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5559 && !section->segment_mark)
5561 /* If the output section of a section in the input segment is NULL,
5562 it is removed from the corresponding output segment. */
5563 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5564 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5565 && section->output_section != NULL)
5567 /* Returns TRUE iff seg1 starts after the end of seg2. */
5568 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5569 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5571 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5572 their VMA address ranges and their LMA address ranges overlap.
5573 It is possible to have overlapping VMA ranges without overlapping LMA
5574 ranges. RedBoot images for example can have both .data and .bss mapped
5575 to the same VMA range, but with the .data section mapped to a different
5577 #define SEGMENT_OVERLAPS(seg1, seg2) \
5578 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5579 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5580 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5581 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5583 /* Initialise the segment mark field. */
5584 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5585 section
->segment_mark
= FALSE
;
5587 /* The Solaris linker creates program headers in which all the
5588 p_paddr fields are zero. When we try to objcopy or strip such a
5589 file, we get confused. Check for this case, and if we find it
5590 don't set the p_paddr_valid fields. */
5591 p_paddr_valid
= FALSE
;
5592 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5595 if (segment
->p_paddr
!= 0)
5597 p_paddr_valid
= TRUE
;
5601 /* Scan through the segments specified in the program header
5602 of the input BFD. For this first scan we look for overlaps
5603 in the loadable segments. These can be created by weird
5604 parameters to objcopy. Also, fix some solaris weirdness. */
5605 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5610 Elf_Internal_Phdr
*segment2
;
5612 if (segment
->p_type
== PT_INTERP
)
5613 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5614 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5616 /* Mininal change so that the normal section to segment
5617 assignment code will work. */
5618 segment
->p_vaddr
= section
->vma
;
5622 if (segment
->p_type
!= PT_LOAD
)
5624 /* Remove PT_GNU_RELRO segment. */
5625 if (segment
->p_type
== PT_GNU_RELRO
)
5626 segment
->p_type
= PT_NULL
;
5630 /* Determine if this segment overlaps any previous segments. */
5631 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5633 bfd_signed_vma extra_length
;
5635 if (segment2
->p_type
!= PT_LOAD
5636 || !SEGMENT_OVERLAPS (segment
, segment2
))
5639 /* Merge the two segments together. */
5640 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5642 /* Extend SEGMENT2 to include SEGMENT and then delete
5644 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5645 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5647 if (extra_length
> 0)
5649 segment2
->p_memsz
+= extra_length
;
5650 segment2
->p_filesz
+= extra_length
;
5653 segment
->p_type
= PT_NULL
;
5655 /* Since we have deleted P we must restart the outer loop. */
5657 segment
= elf_tdata (ibfd
)->phdr
;
5662 /* Extend SEGMENT to include SEGMENT2 and then delete
5664 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5665 - SEGMENT_END (segment
, segment
->p_vaddr
));
5667 if (extra_length
> 0)
5669 segment
->p_memsz
+= extra_length
;
5670 segment
->p_filesz
+= extra_length
;
5673 segment2
->p_type
= PT_NULL
;
5678 /* The second scan attempts to assign sections to segments. */
5679 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5683 unsigned int section_count
;
5684 asection
**sections
;
5685 asection
*output_section
;
5687 bfd_vma matching_lma
;
5688 bfd_vma suggested_lma
;
5691 asection
*first_section
;
5692 bfd_boolean first_matching_lma
;
5693 bfd_boolean first_suggested_lma
;
5695 if (segment
->p_type
== PT_NULL
)
5698 first_section
= NULL
;
5699 /* Compute how many sections might be placed into this segment. */
5700 for (section
= ibfd
->sections
, section_count
= 0;
5702 section
= section
->next
)
5704 /* Find the first section in the input segment, which may be
5705 removed from the corresponding output segment. */
5706 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5708 if (first_section
== NULL
)
5709 first_section
= section
;
5710 if (section
->output_section
!= NULL
)
5715 /* Allocate a segment map big enough to contain
5716 all of the sections we have selected. */
5717 amt
= sizeof (struct elf_segment_map
);
5718 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5719 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
5723 /* Initialise the fields of the segment map. Default to
5724 using the physical address of the segment in the input BFD. */
5726 map
->p_type
= segment
->p_type
;
5727 map
->p_flags
= segment
->p_flags
;
5728 map
->p_flags_valid
= 1;
5730 /* If the first section in the input segment is removed, there is
5731 no need to preserve segment physical address in the corresponding
5733 if (!first_section
|| first_section
->output_section
!= NULL
)
5735 map
->p_paddr
= segment
->p_paddr
;
5736 map
->p_paddr_valid
= p_paddr_valid
;
5739 /* Determine if this segment contains the ELF file header
5740 and if it contains the program headers themselves. */
5741 map
->includes_filehdr
= (segment
->p_offset
== 0
5742 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5743 map
->includes_phdrs
= 0;
5745 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5747 map
->includes_phdrs
=
5748 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5749 && (segment
->p_offset
+ segment
->p_filesz
5750 >= ((bfd_vma
) iehdr
->e_phoff
5751 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5753 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5754 phdr_included
= TRUE
;
5757 if (section_count
== 0)
5759 /* Special segments, such as the PT_PHDR segment, may contain
5760 no sections, but ordinary, loadable segments should contain
5761 something. They are allowed by the ELF spec however, so only
5762 a warning is produced. */
5763 if (segment
->p_type
== PT_LOAD
)
5764 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5765 " detected, is this intentional ?\n"),
5769 *pointer_to_map
= map
;
5770 pointer_to_map
= &map
->next
;
5775 /* Now scan the sections in the input BFD again and attempt
5776 to add their corresponding output sections to the segment map.
5777 The problem here is how to handle an output section which has
5778 been moved (ie had its LMA changed). There are four possibilities:
5780 1. None of the sections have been moved.
5781 In this case we can continue to use the segment LMA from the
5784 2. All of the sections have been moved by the same amount.
5785 In this case we can change the segment's LMA to match the LMA
5786 of the first section.
5788 3. Some of the sections have been moved, others have not.
5789 In this case those sections which have not been moved can be
5790 placed in the current segment which will have to have its size,
5791 and possibly its LMA changed, and a new segment or segments will
5792 have to be created to contain the other sections.
5794 4. The sections have been moved, but not by the same amount.
5795 In this case we can change the segment's LMA to match the LMA
5796 of the first section and we will have to create a new segment
5797 or segments to contain the other sections.
5799 In order to save time, we allocate an array to hold the section
5800 pointers that we are interested in. As these sections get assigned
5801 to a segment, they are removed from this array. */
5803 sections
= (asection
**) bfd_malloc2 (section_count
, sizeof (asection
*));
5804 if (sections
== NULL
)
5807 /* Step One: Scan for segment vs section LMA conflicts.
5808 Also add the sections to the section array allocated above.
5809 Also add the sections to the current segment. In the common
5810 case, where the sections have not been moved, this means that
5811 we have completely filled the segment, and there is nothing
5816 first_matching_lma
= TRUE
;
5817 first_suggested_lma
= TRUE
;
5819 for (section
= ibfd
->sections
;
5821 section
= section
->next
)
5822 if (section
== first_section
)
5825 for (j
= 0; section
!= NULL
; section
= section
->next
)
5827 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5829 output_section
= section
->output_section
;
5831 sections
[j
++] = section
;
5833 /* The Solaris native linker always sets p_paddr to 0.
5834 We try to catch that case here, and set it to the
5835 correct value. Note - some backends require that
5836 p_paddr be left as zero. */
5838 && segment
->p_vaddr
!= 0
5839 && !bed
->want_p_paddr_set_to_zero
5841 && output_section
->lma
!= 0
5842 && output_section
->vma
== (segment
->p_vaddr
5843 + (map
->includes_filehdr
5846 + (map
->includes_phdrs
5848 * iehdr
->e_phentsize
)
5850 map
->p_paddr
= segment
->p_vaddr
;
5852 /* Match up the physical address of the segment with the
5853 LMA address of the output section. */
5854 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5855 || IS_COREFILE_NOTE (segment
, section
)
5856 || (bed
->want_p_paddr_set_to_zero
5857 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5859 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5861 matching_lma
= output_section
->lma
;
5862 first_matching_lma
= FALSE
;
5865 /* We assume that if the section fits within the segment
5866 then it does not overlap any other section within that
5868 map
->sections
[isec
++] = output_section
;
5870 else if (first_suggested_lma
)
5872 suggested_lma
= output_section
->lma
;
5873 first_suggested_lma
= FALSE
;
5876 if (j
== section_count
)
5881 BFD_ASSERT (j
== section_count
);
5883 /* Step Two: Adjust the physical address of the current segment,
5885 if (isec
== section_count
)
5887 /* All of the sections fitted within the segment as currently
5888 specified. This is the default case. Add the segment to
5889 the list of built segments and carry on to process the next
5890 program header in the input BFD. */
5891 map
->count
= section_count
;
5892 *pointer_to_map
= map
;
5893 pointer_to_map
= &map
->next
;
5896 && !bed
->want_p_paddr_set_to_zero
5897 && matching_lma
!= map
->p_paddr
5898 && !map
->includes_filehdr
5899 && !map
->includes_phdrs
)
5900 /* There is some padding before the first section in the
5901 segment. So, we must account for that in the output
5903 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5910 if (!first_matching_lma
)
5912 /* At least one section fits inside the current segment.
5913 Keep it, but modify its physical address to match the
5914 LMA of the first section that fitted. */
5915 map
->p_paddr
= matching_lma
;
5919 /* None of the sections fitted inside the current segment.
5920 Change the current segment's physical address to match
5921 the LMA of the first section. */
5922 map
->p_paddr
= suggested_lma
;
5925 /* Offset the segment physical address from the lma
5926 to allow for space taken up by elf headers. */
5927 if (map
->includes_filehdr
)
5929 if (map
->p_paddr
>= iehdr
->e_ehsize
)
5930 map
->p_paddr
-= iehdr
->e_ehsize
;
5933 map
->includes_filehdr
= FALSE
;
5934 map
->includes_phdrs
= FALSE
;
5938 if (map
->includes_phdrs
)
5940 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
5942 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5944 /* iehdr->e_phnum is just an estimate of the number
5945 of program headers that we will need. Make a note
5946 here of the number we used and the segment we chose
5947 to hold these headers, so that we can adjust the
5948 offset when we know the correct value. */
5949 phdr_adjust_num
= iehdr
->e_phnum
;
5950 phdr_adjust_seg
= map
;
5953 map
->includes_phdrs
= FALSE
;
5957 /* Step Three: Loop over the sections again, this time assigning
5958 those that fit to the current segment and removing them from the
5959 sections array; but making sure not to leave large gaps. Once all
5960 possible sections have been assigned to the current segment it is
5961 added to the list of built segments and if sections still remain
5962 to be assigned, a new segment is constructed before repeating
5969 first_suggested_lma
= TRUE
;
5971 /* Fill the current segment with sections that fit. */
5972 for (j
= 0; j
< section_count
; j
++)
5974 section
= sections
[j
];
5976 if (section
== NULL
)
5979 output_section
= section
->output_section
;
5981 BFD_ASSERT (output_section
!= NULL
);
5983 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5984 || IS_COREFILE_NOTE (segment
, section
))
5986 if (map
->count
== 0)
5988 /* If the first section in a segment does not start at
5989 the beginning of the segment, then something is
5991 if (output_section
->lma
5993 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5994 + (map
->includes_phdrs
5995 ? iehdr
->e_phnum
* iehdr
->e_phentsize
6003 prev_sec
= map
->sections
[map
->count
- 1];
6005 /* If the gap between the end of the previous section
6006 and the start of this section is more than
6007 maxpagesize then we need to start a new segment. */
6008 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
6010 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
6011 || (prev_sec
->lma
+ prev_sec
->size
6012 > output_section
->lma
))
6014 if (first_suggested_lma
)
6016 suggested_lma
= output_section
->lma
;
6017 first_suggested_lma
= FALSE
;
6024 map
->sections
[map
->count
++] = output_section
;
6027 section
->segment_mark
= TRUE
;
6029 else if (first_suggested_lma
)
6031 suggested_lma
= output_section
->lma
;
6032 first_suggested_lma
= FALSE
;
6036 BFD_ASSERT (map
->count
> 0);
6038 /* Add the current segment to the list of built segments. */
6039 *pointer_to_map
= map
;
6040 pointer_to_map
= &map
->next
;
6042 if (isec
< section_count
)
6044 /* We still have not allocated all of the sections to
6045 segments. Create a new segment here, initialise it
6046 and carry on looping. */
6047 amt
= sizeof (struct elf_segment_map
);
6048 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6049 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6056 /* Initialise the fields of the segment map. Set the physical
6057 physical address to the LMA of the first section that has
6058 not yet been assigned. */
6060 map
->p_type
= segment
->p_type
;
6061 map
->p_flags
= segment
->p_flags
;
6062 map
->p_flags_valid
= 1;
6063 map
->p_paddr
= suggested_lma
;
6064 map
->p_paddr_valid
= p_paddr_valid
;
6065 map
->includes_filehdr
= 0;
6066 map
->includes_phdrs
= 0;
6069 while (isec
< section_count
);
6074 elf_tdata (obfd
)->segment_map
= map_first
;
6076 /* If we had to estimate the number of program headers that were
6077 going to be needed, then check our estimate now and adjust
6078 the offset if necessary. */
6079 if (phdr_adjust_seg
!= NULL
)
6083 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
6086 if (count
> phdr_adjust_num
)
6087 phdr_adjust_seg
->p_paddr
6088 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
6093 #undef IS_CONTAINED_BY_VMA
6094 #undef IS_CONTAINED_BY_LMA
6096 #undef IS_COREFILE_NOTE
6097 #undef IS_SOLARIS_PT_INTERP
6098 #undef IS_SECTION_IN_INPUT_SEGMENT
6099 #undef INCLUDE_SECTION_IN_SEGMENT
6100 #undef SEGMENT_AFTER_SEGMENT
6101 #undef SEGMENT_OVERLAPS
6105 /* Copy ELF program header information. */
6108 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
6110 Elf_Internal_Ehdr
*iehdr
;
6111 struct elf_segment_map
*map
;
6112 struct elf_segment_map
*map_first
;
6113 struct elf_segment_map
**pointer_to_map
;
6114 Elf_Internal_Phdr
*segment
;
6116 unsigned int num_segments
;
6117 bfd_boolean phdr_included
= FALSE
;
6118 bfd_boolean p_paddr_valid
;
6120 iehdr
= elf_elfheader (ibfd
);
6123 pointer_to_map
= &map_first
;
6125 /* If all the segment p_paddr fields are zero, don't set
6126 map->p_paddr_valid. */
6127 p_paddr_valid
= FALSE
;
6128 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6129 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6132 if (segment
->p_paddr
!= 0)
6134 p_paddr_valid
= TRUE
;
6138 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6143 unsigned int section_count
;
6145 Elf_Internal_Shdr
*this_hdr
;
6146 asection
*first_section
= NULL
;
6147 asection
*lowest_section
;
6149 /* Compute how many sections are in this segment. */
6150 for (section
= ibfd
->sections
, section_count
= 0;
6152 section
= section
->next
)
6154 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6155 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6157 if (first_section
== NULL
)
6158 first_section
= section
;
6163 /* Allocate a segment map big enough to contain
6164 all of the sections we have selected. */
6165 amt
= sizeof (struct elf_segment_map
);
6166 if (section_count
!= 0)
6167 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
6168 map
= (struct elf_segment_map
*) bfd_zalloc (obfd
, amt
);
6172 /* Initialize the fields of the output segment map with the
6175 map
->p_type
= segment
->p_type
;
6176 map
->p_flags
= segment
->p_flags
;
6177 map
->p_flags_valid
= 1;
6178 map
->p_paddr
= segment
->p_paddr
;
6179 map
->p_paddr_valid
= p_paddr_valid
;
6180 map
->p_align
= segment
->p_align
;
6181 map
->p_align_valid
= 1;
6182 map
->p_vaddr_offset
= 0;
6184 if (map
->p_type
== PT_GNU_RELRO
6185 || map
->p_type
== PT_GNU_STACK
)
6187 /* The PT_GNU_RELRO segment may contain the first a few
6188 bytes in the .got.plt section even if the whole .got.plt
6189 section isn't in the PT_GNU_RELRO segment. We won't
6190 change the size of the PT_GNU_RELRO segment.
6191 Similarly, PT_GNU_STACK size is significant on uclinux
6193 map
->p_size
= segment
->p_memsz
;
6194 map
->p_size_valid
= 1;
6197 /* Determine if this segment contains the ELF file header
6198 and if it contains the program headers themselves. */
6199 map
->includes_filehdr
= (segment
->p_offset
== 0
6200 && segment
->p_filesz
>= iehdr
->e_ehsize
);
6202 map
->includes_phdrs
= 0;
6203 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
6205 map
->includes_phdrs
=
6206 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
6207 && (segment
->p_offset
+ segment
->p_filesz
6208 >= ((bfd_vma
) iehdr
->e_phoff
6209 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
6211 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
6212 phdr_included
= TRUE
;
6215 lowest_section
= first_section
;
6216 if (section_count
!= 0)
6218 unsigned int isec
= 0;
6220 for (section
= first_section
;
6222 section
= section
->next
)
6224 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6225 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6227 map
->sections
[isec
++] = section
->output_section
;
6228 if (section
->lma
< lowest_section
->lma
)
6229 lowest_section
= section
;
6230 if ((section
->flags
& SEC_ALLOC
) != 0)
6234 /* Section lmas are set up from PT_LOAD header
6235 p_paddr in _bfd_elf_make_section_from_shdr.
6236 If this header has a p_paddr that disagrees
6237 with the section lma, flag the p_paddr as
6239 if ((section
->flags
& SEC_LOAD
) != 0)
6240 seg_off
= this_hdr
->sh_offset
- segment
->p_offset
;
6242 seg_off
= this_hdr
->sh_addr
- segment
->p_vaddr
;
6243 if (section
->lma
- segment
->p_paddr
!= seg_off
)
6244 map
->p_paddr_valid
= FALSE
;
6246 if (isec
== section_count
)
6252 if (map
->includes_filehdr
&& lowest_section
!= NULL
)
6253 /* We need to keep the space used by the headers fixed. */
6254 map
->header_size
= lowest_section
->vma
- segment
->p_vaddr
;
6256 if (!map
->includes_phdrs
6257 && !map
->includes_filehdr
6258 && map
->p_paddr_valid
)
6259 /* There is some other padding before the first section. */
6260 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
6261 - segment
->p_paddr
);
6263 map
->count
= section_count
;
6264 *pointer_to_map
= map
;
6265 pointer_to_map
= &map
->next
;
6268 elf_tdata (obfd
)->segment_map
= map_first
;
6272 /* Copy private BFD data. This copies or rewrites ELF program header
6276 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
6278 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6279 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6282 if (elf_tdata (ibfd
)->phdr
== NULL
)
6285 if (ibfd
->xvec
== obfd
->xvec
)
6287 /* Check to see if any sections in the input BFD
6288 covered by ELF program header have changed. */
6289 Elf_Internal_Phdr
*segment
;
6290 asection
*section
, *osec
;
6291 unsigned int i
, num_segments
;
6292 Elf_Internal_Shdr
*this_hdr
;
6293 const struct elf_backend_data
*bed
;
6295 bed
= get_elf_backend_data (ibfd
);
6297 /* Regenerate the segment map if p_paddr is set to 0. */
6298 if (bed
->want_p_paddr_set_to_zero
)
6301 /* Initialize the segment mark field. */
6302 for (section
= obfd
->sections
; section
!= NULL
;
6303 section
= section
->next
)
6304 section
->segment_mark
= FALSE
;
6306 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6307 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6311 /* PR binutils/3535. The Solaris linker always sets the p_paddr
6312 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6313 which severly confuses things, so always regenerate the segment
6314 map in this case. */
6315 if (segment
->p_paddr
== 0
6316 && segment
->p_memsz
== 0
6317 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
6320 for (section
= ibfd
->sections
;
6321 section
!= NULL
; section
= section
->next
)
6323 /* We mark the output section so that we know it comes
6324 from the input BFD. */
6325 osec
= section
->output_section
;
6327 osec
->segment_mark
= TRUE
;
6329 /* Check if this section is covered by the segment. */
6330 this_hdr
= &(elf_section_data(section
)->this_hdr
);
6331 if (ELF_SECTION_IN_SEGMENT (this_hdr
, segment
))
6333 /* FIXME: Check if its output section is changed or
6334 removed. What else do we need to check? */
6336 || section
->flags
!= osec
->flags
6337 || section
->lma
!= osec
->lma
6338 || section
->vma
!= osec
->vma
6339 || section
->size
!= osec
->size
6340 || section
->rawsize
!= osec
->rawsize
6341 || section
->alignment_power
!= osec
->alignment_power
)
6347 /* Check to see if any output section do not come from the
6349 for (section
= obfd
->sections
; section
!= NULL
;
6350 section
= section
->next
)
6352 if (section
->segment_mark
== FALSE
)
6355 section
->segment_mark
= FALSE
;
6358 return copy_elf_program_header (ibfd
, obfd
);
6362 if (ibfd
->xvec
== obfd
->xvec
)
6364 /* When rewriting program header, set the output maxpagesize to
6365 the maximum alignment of input PT_LOAD segments. */
6366 Elf_Internal_Phdr
*segment
;
6368 unsigned int num_segments
= elf_elfheader (ibfd
)->e_phnum
;
6369 bfd_vma maxpagesize
= 0;
6371 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
6374 if (segment
->p_type
== PT_LOAD
6375 && maxpagesize
< segment
->p_align
)
6376 maxpagesize
= segment
->p_align
;
6378 if (maxpagesize
!= get_elf_backend_data (obfd
)->maxpagesize
)
6379 bfd_emul_set_maxpagesize (bfd_get_target (obfd
), maxpagesize
);
6382 return rewrite_elf_program_header (ibfd
, obfd
);
6385 /* Initialize private output section information from input section. */
6388 _bfd_elf_init_private_section_data (bfd
*ibfd
,
6392 struct bfd_link_info
*link_info
)
6395 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6396 bfd_boolean final_link
= link_info
!= NULL
&& !link_info
->relocatable
;
6398 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6399 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6402 BFD_ASSERT (elf_section_data (osec
) != NULL
);
6404 /* For objcopy and relocatable link, don't copy the output ELF
6405 section type from input if the output BFD section flags have been
6406 set to something different. For a final link allow some flags
6407 that the linker clears to differ. */
6408 if (elf_section_type (osec
) == SHT_NULL
6409 && (osec
->flags
== isec
->flags
6411 && ((osec
->flags
^ isec
->flags
)
6412 & ~(SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
| SEC_RELOC
)) == 0)))
6413 elf_section_type (osec
) = elf_section_type (isec
);
6415 /* FIXME: Is this correct for all OS/PROC specific flags? */
6416 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6417 & (SHF_MASKOS
| SHF_MASKPROC
));
6419 /* Set things up for objcopy and relocatable link. The output
6420 SHT_GROUP section will have its elf_next_in_group pointing back
6421 to the input group members. Ignore linker created group section.
6422 See elfNN_ia64_object_p in elfxx-ia64.c. */
6425 if (elf_sec_group (isec
) == NULL
6426 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6428 if (elf_section_flags (isec
) & SHF_GROUP
)
6429 elf_section_flags (osec
) |= SHF_GROUP
;
6430 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6431 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6435 ihdr
= &elf_section_data (isec
)->this_hdr
;
6437 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6438 don't use the output section of the linked-to section since it
6439 may be NULL at this point. */
6440 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6442 ohdr
= &elf_section_data (osec
)->this_hdr
;
6443 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6444 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6447 osec
->use_rela_p
= isec
->use_rela_p
;
6452 /* Copy private section information. This copies over the entsize
6453 field, and sometimes the info field. */
6456 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6461 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6463 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6464 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6467 ihdr
= &elf_section_data (isec
)->this_hdr
;
6468 ohdr
= &elf_section_data (osec
)->this_hdr
;
6470 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6472 if (ihdr
->sh_type
== SHT_SYMTAB
6473 || ihdr
->sh_type
== SHT_DYNSYM
6474 || ihdr
->sh_type
== SHT_GNU_verneed
6475 || ihdr
->sh_type
== SHT_GNU_verdef
)
6476 ohdr
->sh_info
= ihdr
->sh_info
;
6478 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6482 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6483 necessary if we are removing either the SHT_GROUP section or any of
6484 the group member sections. DISCARDED is the value that a section's
6485 output_section has if the section will be discarded, NULL when this
6486 function is called from objcopy, bfd_abs_section_ptr when called
6490 _bfd_elf_fixup_group_sections (bfd
*ibfd
, asection
*discarded
)
6494 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6495 if (elf_section_type (isec
) == SHT_GROUP
)
6497 asection
*first
= elf_next_in_group (isec
);
6498 asection
*s
= first
;
6499 bfd_size_type removed
= 0;
6503 /* If this member section is being output but the
6504 SHT_GROUP section is not, then clear the group info
6505 set up by _bfd_elf_copy_private_section_data. */
6506 if (s
->output_section
!= discarded
6507 && isec
->output_section
== discarded
)
6509 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6510 elf_group_name (s
->output_section
) = NULL
;
6512 /* Conversely, if the member section is not being output
6513 but the SHT_GROUP section is, then adjust its size. */
6514 else if (s
->output_section
== discarded
6515 && isec
->output_section
!= discarded
)
6517 s
= elf_next_in_group (s
);
6523 if (discarded
!= NULL
)
6525 /* If we've been called for ld -r, then we need to
6526 adjust the input section size. This function may
6527 be called multiple times, so save the original
6529 if (isec
->rawsize
== 0)
6530 isec
->rawsize
= isec
->size
;
6531 isec
->size
= isec
->rawsize
- removed
;
6535 /* Adjust the output section size when called from
6537 isec
->output_section
->size
-= removed
;
6545 /* Copy private header information. */
6548 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6550 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6551 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6554 /* Copy over private BFD data if it has not already been copied.
6555 This must be done here, rather than in the copy_private_bfd_data
6556 entry point, because the latter is called after the section
6557 contents have been set, which means that the program headers have
6558 already been worked out. */
6559 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6561 if (! copy_private_bfd_data (ibfd
, obfd
))
6565 return _bfd_elf_fixup_group_sections (ibfd
, NULL
);
6568 /* Copy private symbol information. If this symbol is in a section
6569 which we did not map into a BFD section, try to map the section
6570 index correctly. We use special macro definitions for the mapped
6571 section indices; these definitions are interpreted by the
6572 swap_out_syms function. */
6574 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6575 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6576 #define MAP_STRTAB (SHN_HIOS + 3)
6577 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6578 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6581 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6586 elf_symbol_type
*isym
, *osym
;
6588 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6589 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6592 isym
= elf_symbol_from (ibfd
, isymarg
);
6593 osym
= elf_symbol_from (obfd
, osymarg
);
6596 && isym
->internal_elf_sym
.st_shndx
!= 0
6598 && bfd_is_abs_section (isym
->symbol
.section
))
6602 shndx
= isym
->internal_elf_sym
.st_shndx
;
6603 if (shndx
== elf_onesymtab (ibfd
))
6604 shndx
= MAP_ONESYMTAB
;
6605 else if (shndx
== elf_dynsymtab (ibfd
))
6606 shndx
= MAP_DYNSYMTAB
;
6607 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
6609 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
6610 shndx
= MAP_SHSTRTAB
;
6611 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
6612 shndx
= MAP_SYM_SHNDX
;
6613 osym
->internal_elf_sym
.st_shndx
= shndx
;
6619 /* Swap out the symbols. */
6622 swap_out_syms (bfd
*abfd
,
6623 struct bfd_strtab_hash
**sttp
,
6626 const struct elf_backend_data
*bed
;
6629 struct bfd_strtab_hash
*stt
;
6630 Elf_Internal_Shdr
*symtab_hdr
;
6631 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6632 Elf_Internal_Shdr
*symstrtab_hdr
;
6633 bfd_byte
*outbound_syms
;
6634 bfd_byte
*outbound_shndx
;
6637 bfd_boolean name_local_sections
;
6639 if (!elf_map_symbols (abfd
))
6642 /* Dump out the symtabs. */
6643 stt
= _bfd_elf_stringtab_init ();
6647 bed
= get_elf_backend_data (abfd
);
6648 symcount
= bfd_get_symcount (abfd
);
6649 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6650 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6651 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6652 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6653 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
6654 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6656 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6657 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6659 outbound_syms
= (bfd_byte
*) bfd_alloc2 (abfd
, 1 + symcount
,
6660 bed
->s
->sizeof_sym
);
6661 if (outbound_syms
== NULL
)
6663 _bfd_stringtab_free (stt
);
6666 symtab_hdr
->contents
= outbound_syms
;
6668 outbound_shndx
= NULL
;
6669 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6670 if (symtab_shndx_hdr
->sh_name
!= 0)
6672 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6673 outbound_shndx
= (bfd_byte
*)
6674 bfd_zalloc2 (abfd
, 1 + symcount
, sizeof (Elf_External_Sym_Shndx
));
6675 if (outbound_shndx
== NULL
)
6677 _bfd_stringtab_free (stt
);
6681 symtab_shndx_hdr
->contents
= outbound_shndx
;
6682 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6683 symtab_shndx_hdr
->sh_size
= amt
;
6684 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6685 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6688 /* Now generate the data (for "contents"). */
6690 /* Fill in zeroth symbol and swap it out. */
6691 Elf_Internal_Sym sym
;
6697 sym
.st_shndx
= SHN_UNDEF
;
6698 sym
.st_target_internal
= 0;
6699 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6700 outbound_syms
+= bed
->s
->sizeof_sym
;
6701 if (outbound_shndx
!= NULL
)
6702 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6706 = (bed
->elf_backend_name_local_section_symbols
6707 && bed
->elf_backend_name_local_section_symbols (abfd
));
6709 syms
= bfd_get_outsymbols (abfd
);
6710 for (idx
= 0; idx
< symcount
; idx
++)
6712 Elf_Internal_Sym sym
;
6713 bfd_vma value
= syms
[idx
]->value
;
6714 elf_symbol_type
*type_ptr
;
6715 flagword flags
= syms
[idx
]->flags
;
6718 if (!name_local_sections
6719 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6721 /* Local section symbols have no name. */
6726 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6729 if (sym
.st_name
== (unsigned long) -1)
6731 _bfd_stringtab_free (stt
);
6736 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6738 if ((flags
& BSF_SECTION_SYM
) == 0
6739 && bfd_is_com_section (syms
[idx
]->section
))
6741 /* ELF common symbols put the alignment into the `value' field,
6742 and the size into the `size' field. This is backwards from
6743 how BFD handles it, so reverse it here. */
6744 sym
.st_size
= value
;
6745 if (type_ptr
== NULL
6746 || type_ptr
->internal_elf_sym
.st_value
== 0)
6747 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6749 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6750 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6751 (abfd
, syms
[idx
]->section
);
6755 asection
*sec
= syms
[idx
]->section
;
6758 if (sec
->output_section
)
6760 value
+= sec
->output_offset
;
6761 sec
= sec
->output_section
;
6764 /* Don't add in the section vma for relocatable output. */
6765 if (! relocatable_p
)
6767 sym
.st_value
= value
;
6768 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6770 if (bfd_is_abs_section (sec
)
6772 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6774 /* This symbol is in a real ELF section which we did
6775 not create as a BFD section. Undo the mapping done
6776 by copy_private_symbol_data. */
6777 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6781 shndx
= elf_onesymtab (abfd
);
6784 shndx
= elf_dynsymtab (abfd
);
6787 shndx
= elf_tdata (abfd
)->strtab_section
;
6790 shndx
= elf_tdata (abfd
)->shstrtab_section
;
6793 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
6802 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6804 if (shndx
== SHN_BAD
)
6808 /* Writing this would be a hell of a lot easier if
6809 we had some decent documentation on bfd, and
6810 knew what to expect of the library, and what to
6811 demand of applications. For example, it
6812 appears that `objcopy' might not set the
6813 section of a symbol to be a section that is
6814 actually in the output file. */
6815 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6818 _bfd_error_handler (_("\
6819 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6820 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6822 bfd_set_error (bfd_error_invalid_operation
);
6823 _bfd_stringtab_free (stt
);
6827 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6828 BFD_ASSERT (shndx
!= SHN_BAD
);
6832 sym
.st_shndx
= shndx
;
6835 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6837 else if ((flags
& BSF_GNU_INDIRECT_FUNCTION
) != 0)
6838 type
= STT_GNU_IFUNC
;
6839 else if ((flags
& BSF_FUNCTION
) != 0)
6841 else if ((flags
& BSF_OBJECT
) != 0)
6843 else if ((flags
& BSF_RELC
) != 0)
6845 else if ((flags
& BSF_SRELC
) != 0)
6850 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6853 /* Processor-specific types. */
6854 if (type_ptr
!= NULL
6855 && bed
->elf_backend_get_symbol_type
)
6856 type
= ((*bed
->elf_backend_get_symbol_type
)
6857 (&type_ptr
->internal_elf_sym
, type
));
6859 if (flags
& BSF_SECTION_SYM
)
6861 if (flags
& BSF_GLOBAL
)
6862 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6864 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6866 else if (bfd_is_com_section (syms
[idx
]->section
))
6868 #ifdef USE_STT_COMMON
6869 if (type
== STT_OBJECT
)
6870 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6873 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6875 else if (bfd_is_und_section (syms
[idx
]->section
))
6876 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6880 else if (flags
& BSF_FILE
)
6881 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6884 int bind
= STB_LOCAL
;
6886 if (flags
& BSF_LOCAL
)
6888 else if (flags
& BSF_GNU_UNIQUE
)
6889 bind
= STB_GNU_UNIQUE
;
6890 else if (flags
& BSF_WEAK
)
6892 else if (flags
& BSF_GLOBAL
)
6895 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6898 if (type_ptr
!= NULL
)
6900 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6901 sym
.st_target_internal
6902 = type_ptr
->internal_elf_sym
.st_target_internal
;
6907 sym
.st_target_internal
= 0;
6910 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6911 outbound_syms
+= bed
->s
->sizeof_sym
;
6912 if (outbound_shndx
!= NULL
)
6913 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6917 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6918 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6920 symstrtab_hdr
->sh_flags
= 0;
6921 symstrtab_hdr
->sh_addr
= 0;
6922 symstrtab_hdr
->sh_entsize
= 0;
6923 symstrtab_hdr
->sh_link
= 0;
6924 symstrtab_hdr
->sh_info
= 0;
6925 symstrtab_hdr
->sh_addralign
= 1;
6930 /* Return the number of bytes required to hold the symtab vector.
6932 Note that we base it on the count plus 1, since we will null terminate
6933 the vector allocated based on this size. However, the ELF symbol table
6934 always has a dummy entry as symbol #0, so it ends up even. */
6937 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6941 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6943 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6944 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6946 symtab_size
-= sizeof (asymbol
*);
6952 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6956 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6958 if (elf_dynsymtab (abfd
) == 0)
6960 bfd_set_error (bfd_error_invalid_operation
);
6964 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6965 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6967 symtab_size
-= sizeof (asymbol
*);
6973 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6976 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6979 /* Canonicalize the relocs. */
6982 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6989 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6991 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6994 tblptr
= section
->relocation
;
6995 for (i
= 0; i
< section
->reloc_count
; i
++)
6996 *relptr
++ = tblptr
++;
7000 return section
->reloc_count
;
7004 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
7006 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7007 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
7010 bfd_get_symcount (abfd
) = symcount
;
7015 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
7016 asymbol
**allocation
)
7018 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7019 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
7022 bfd_get_dynamic_symcount (abfd
) = symcount
;
7026 /* Return the size required for the dynamic reloc entries. Any loadable
7027 section that was actually installed in the BFD, and has type SHT_REL
7028 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7029 dynamic reloc section. */
7032 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
7037 if (elf_dynsymtab (abfd
) == 0)
7039 bfd_set_error (bfd_error_invalid_operation
);
7043 ret
= sizeof (arelent
*);
7044 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7045 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7046 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7047 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7048 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
7049 * sizeof (arelent
*));
7054 /* Canonicalize the dynamic relocation entries. Note that we return the
7055 dynamic relocations as a single block, although they are actually
7056 associated with particular sections; the interface, which was
7057 designed for SunOS style shared libraries, expects that there is only
7058 one set of dynamic relocs. Any loadable section that was actually
7059 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7060 dynamic symbol table, is considered to be a dynamic reloc section. */
7063 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
7067 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
7071 if (elf_dynsymtab (abfd
) == 0)
7073 bfd_set_error (bfd_error_invalid_operation
);
7077 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
7079 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
7081 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
7082 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
7083 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
7088 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
7090 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
7092 for (i
= 0; i
< count
; i
++)
7103 /* Read in the version information. */
7106 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
7108 bfd_byte
*contents
= NULL
;
7109 unsigned int freeidx
= 0;
7111 if (elf_dynverref (abfd
) != 0)
7113 Elf_Internal_Shdr
*hdr
;
7114 Elf_External_Verneed
*everneed
;
7115 Elf_Internal_Verneed
*iverneed
;
7117 bfd_byte
*contents_end
;
7119 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
7121 elf_tdata (abfd
)->verref
= (Elf_Internal_Verneed
*)
7122 bfd_zalloc2 (abfd
, hdr
->sh_info
, sizeof (Elf_Internal_Verneed
));
7123 if (elf_tdata (abfd
)->verref
== NULL
)
7126 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
7128 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7129 if (contents
== NULL
)
7131 error_return_verref
:
7132 elf_tdata (abfd
)->verref
= NULL
;
7133 elf_tdata (abfd
)->cverrefs
= 0;
7136 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7137 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7138 goto error_return_verref
;
7140 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
7141 goto error_return_verref
;
7143 BFD_ASSERT (sizeof (Elf_External_Verneed
)
7144 == sizeof (Elf_External_Vernaux
));
7145 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
7146 everneed
= (Elf_External_Verneed
*) contents
;
7147 iverneed
= elf_tdata (abfd
)->verref
;
7148 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
7150 Elf_External_Vernaux
*evernaux
;
7151 Elf_Internal_Vernaux
*ivernaux
;
7154 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
7156 iverneed
->vn_bfd
= abfd
;
7158 iverneed
->vn_filename
=
7159 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7161 if (iverneed
->vn_filename
== NULL
)
7162 goto error_return_verref
;
7164 if (iverneed
->vn_cnt
== 0)
7165 iverneed
->vn_auxptr
= NULL
;
7168 iverneed
->vn_auxptr
= (struct elf_internal_vernaux
*)
7169 bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
7170 sizeof (Elf_Internal_Vernaux
));
7171 if (iverneed
->vn_auxptr
== NULL
)
7172 goto error_return_verref
;
7175 if (iverneed
->vn_aux
7176 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7177 goto error_return_verref
;
7179 evernaux
= ((Elf_External_Vernaux
*)
7180 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
7181 ivernaux
= iverneed
->vn_auxptr
;
7182 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
7184 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
7186 ivernaux
->vna_nodename
=
7187 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7188 ivernaux
->vna_name
);
7189 if (ivernaux
->vna_nodename
== NULL
)
7190 goto error_return_verref
;
7192 if (j
+ 1 < iverneed
->vn_cnt
)
7193 ivernaux
->vna_nextptr
= ivernaux
+ 1;
7195 ivernaux
->vna_nextptr
= NULL
;
7197 if (ivernaux
->vna_next
7198 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
7199 goto error_return_verref
;
7201 evernaux
= ((Elf_External_Vernaux
*)
7202 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
7204 if (ivernaux
->vna_other
> freeidx
)
7205 freeidx
= ivernaux
->vna_other
;
7208 if (i
+ 1 < hdr
->sh_info
)
7209 iverneed
->vn_nextref
= iverneed
+ 1;
7211 iverneed
->vn_nextref
= NULL
;
7213 if (iverneed
->vn_next
7214 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
7215 goto error_return_verref
;
7217 everneed
= ((Elf_External_Verneed
*)
7218 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
7225 if (elf_dynverdef (abfd
) != 0)
7227 Elf_Internal_Shdr
*hdr
;
7228 Elf_External_Verdef
*everdef
;
7229 Elf_Internal_Verdef
*iverdef
;
7230 Elf_Internal_Verdef
*iverdefarr
;
7231 Elf_Internal_Verdef iverdefmem
;
7233 unsigned int maxidx
;
7234 bfd_byte
*contents_end_def
, *contents_end_aux
;
7236 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
7238 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
7239 if (contents
== NULL
)
7241 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
7242 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
7245 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
7248 BFD_ASSERT (sizeof (Elf_External_Verdef
)
7249 >= sizeof (Elf_External_Verdaux
));
7250 contents_end_def
= contents
+ hdr
->sh_size
7251 - sizeof (Elf_External_Verdef
);
7252 contents_end_aux
= contents
+ hdr
->sh_size
7253 - sizeof (Elf_External_Verdaux
);
7255 /* We know the number of entries in the section but not the maximum
7256 index. Therefore we have to run through all entries and find
7258 everdef
= (Elf_External_Verdef
*) contents
;
7260 for (i
= 0; i
< hdr
->sh_info
; ++i
)
7262 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7264 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
7265 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
7267 if (iverdefmem
.vd_next
7268 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
7271 everdef
= ((Elf_External_Verdef
*)
7272 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
7275 if (default_imported_symver
)
7277 if (freeidx
> maxidx
)
7282 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7283 bfd_zalloc2 (abfd
, maxidx
, sizeof (Elf_Internal_Verdef
));
7284 if (elf_tdata (abfd
)->verdef
== NULL
)
7287 elf_tdata (abfd
)->cverdefs
= maxidx
;
7289 everdef
= (Elf_External_Verdef
*) contents
;
7290 iverdefarr
= elf_tdata (abfd
)->verdef
;
7291 for (i
= 0; i
< hdr
->sh_info
; i
++)
7293 Elf_External_Verdaux
*everdaux
;
7294 Elf_Internal_Verdaux
*iverdaux
;
7297 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
7299 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
7301 error_return_verdef
:
7302 elf_tdata (abfd
)->verdef
= NULL
;
7303 elf_tdata (abfd
)->cverdefs
= 0;
7307 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
7308 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
7310 iverdef
->vd_bfd
= abfd
;
7312 if (iverdef
->vd_cnt
== 0)
7313 iverdef
->vd_auxptr
= NULL
;
7316 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7317 bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
7318 sizeof (Elf_Internal_Verdaux
));
7319 if (iverdef
->vd_auxptr
== NULL
)
7320 goto error_return_verdef
;
7324 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
7325 goto error_return_verdef
;
7327 everdaux
= ((Elf_External_Verdaux
*)
7328 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
7329 iverdaux
= iverdef
->vd_auxptr
;
7330 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
7332 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
7334 iverdaux
->vda_nodename
=
7335 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
7336 iverdaux
->vda_name
);
7337 if (iverdaux
->vda_nodename
== NULL
)
7338 goto error_return_verdef
;
7340 if (j
+ 1 < iverdef
->vd_cnt
)
7341 iverdaux
->vda_nextptr
= iverdaux
+ 1;
7343 iverdaux
->vda_nextptr
= NULL
;
7345 if (iverdaux
->vda_next
7346 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
7347 goto error_return_verdef
;
7349 everdaux
= ((Elf_External_Verdaux
*)
7350 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
7353 if (iverdef
->vd_cnt
)
7354 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
7356 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
7357 iverdef
->vd_nextdef
= iverdef
+ 1;
7359 iverdef
->vd_nextdef
= NULL
;
7361 everdef
= ((Elf_External_Verdef
*)
7362 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
7368 else if (default_imported_symver
)
7375 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*)
7376 bfd_zalloc2 (abfd
, freeidx
, sizeof (Elf_Internal_Verdef
));
7377 if (elf_tdata (abfd
)->verdef
== NULL
)
7380 elf_tdata (abfd
)->cverdefs
= freeidx
;
7383 /* Create a default version based on the soname. */
7384 if (default_imported_symver
)
7386 Elf_Internal_Verdef
*iverdef
;
7387 Elf_Internal_Verdaux
*iverdaux
;
7389 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];
7391 iverdef
->vd_version
= VER_DEF_CURRENT
;
7392 iverdef
->vd_flags
= 0;
7393 iverdef
->vd_ndx
= freeidx
;
7394 iverdef
->vd_cnt
= 1;
7396 iverdef
->vd_bfd
= abfd
;
7398 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
7399 if (iverdef
->vd_nodename
== NULL
)
7400 goto error_return_verdef
;
7401 iverdef
->vd_nextdef
= NULL
;
7402 iverdef
->vd_auxptr
= (struct elf_internal_verdaux
*)
7403 bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
7404 if (iverdef
->vd_auxptr
== NULL
)
7405 goto error_return_verdef
;
7407 iverdaux
= iverdef
->vd_auxptr
;
7408 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
7409 iverdaux
->vda_nextptr
= NULL
;
7415 if (contents
!= NULL
)
7421 _bfd_elf_make_empty_symbol (bfd
*abfd
)
7423 elf_symbol_type
*newsym
;
7424 bfd_size_type amt
= sizeof (elf_symbol_type
);
7426 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
7431 newsym
->symbol
.the_bfd
= abfd
;
7432 return &newsym
->symbol
;
7437 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
7441 bfd_symbol_info (symbol
, ret
);
7444 /* Return whether a symbol name implies a local symbol. Most targets
7445 use this function for the is_local_label_name entry point, but some
7449 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
7452 /* Normal local symbols start with ``.L''. */
7453 if (name
[0] == '.' && name
[1] == 'L')
7456 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7457 DWARF debugging symbols starting with ``..''. */
7458 if (name
[0] == '.' && name
[1] == '.')
7461 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7462 emitting DWARF debugging output. I suspect this is actually a
7463 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7464 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7465 underscore to be emitted on some ELF targets). For ease of use,
7466 we treat such symbols as local. */
7467 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7474 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7475 asymbol
*symbol ATTRIBUTE_UNUSED
)
7482 _bfd_elf_set_arch_mach (bfd
*abfd
,
7483 enum bfd_architecture arch
,
7484 unsigned long machine
)
7486 /* If this isn't the right architecture for this backend, and this
7487 isn't the generic backend, fail. */
7488 if (arch
!= get_elf_backend_data (abfd
)->arch
7489 && arch
!= bfd_arch_unknown
7490 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7493 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7496 /* Find the function to a particular section and offset,
7497 for error reporting. */
7500 elf_find_function (bfd
*abfd
,
7504 const char **filename_ptr
,
7505 const char **functionname_ptr
)
7507 struct elf_find_function_cache
7509 asection
*last_section
;
7511 const char *filename
;
7512 bfd_size_type func_size
;
7515 if (symbols
== NULL
)
7518 cache
= elf_tdata (abfd
)->elf_find_function_cache
;
7521 cache
= bfd_zalloc (abfd
, sizeof (*cache
));
7522 elf_tdata (abfd
)->elf_find_function_cache
= cache
;
7526 if (cache
->last_section
!= section
7527 || cache
->func
== NULL
7528 || offset
< cache
->func
->value
7529 || offset
>= cache
->func
->value
+ cache
->func_size
)
7534 /* ??? Given multiple file symbols, it is impossible to reliably
7535 choose the right file name for global symbols. File symbols are
7536 local symbols, and thus all file symbols must sort before any
7537 global symbols. The ELF spec may be interpreted to say that a
7538 file symbol must sort before other local symbols, but currently
7539 ld -r doesn't do this. So, for ld -r output, it is possible to
7540 make a better choice of file name for local symbols by ignoring
7541 file symbols appearing after a given local symbol. */
7542 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7543 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7547 state
= nothing_seen
;
7548 cache
->filename
= NULL
;
7550 cache
->func_size
= 0;
7551 cache
->last_section
= section
;
7553 for (p
= symbols
; *p
!= NULL
; p
++)
7559 if ((sym
->flags
& BSF_FILE
) != 0)
7562 if (state
== symbol_seen
)
7563 state
= file_after_symbol_seen
;
7567 size
= bed
->maybe_function_sym (sym
, section
, &code_off
);
7569 && code_off
<= offset
7570 && (code_off
> low_func
7571 || (code_off
== low_func
7572 && size
> cache
->func_size
)))
7575 cache
->func_size
= size
;
7576 cache
->filename
= NULL
;
7577 low_func
= code_off
;
7579 && ((sym
->flags
& BSF_LOCAL
) != 0
7580 || state
!= file_after_symbol_seen
))
7581 cache
->filename
= bfd_asymbol_name (file
);
7583 if (state
== nothing_seen
)
7584 state
= symbol_seen
;
7588 if (cache
->func
== NULL
)
7592 *filename_ptr
= cache
->filename
;
7593 if (functionname_ptr
)
7594 *functionname_ptr
= bfd_asymbol_name (cache
->func
);
7599 /* Find the nearest line to a particular section and offset,
7600 for error reporting. */
7603 _bfd_elf_find_nearest_line (bfd
*abfd
,
7607 const char **filename_ptr
,
7608 const char **functionname_ptr
,
7609 unsigned int *line_ptr
)
7611 return _bfd_elf_find_nearest_line_discriminator (abfd
, section
, symbols
,
7612 offset
, filename_ptr
,
7619 _bfd_elf_find_nearest_line_discriminator (bfd
*abfd
,
7623 const char **filename_ptr
,
7624 const char **functionname_ptr
,
7625 unsigned int *line_ptr
,
7626 unsigned int *discriminator_ptr
)
7630 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7631 filename_ptr
, functionname_ptr
,
7634 if (!*functionname_ptr
)
7635 elf_find_function (abfd
, section
, symbols
, offset
,
7636 *filename_ptr
? NULL
: filename_ptr
,
7642 if (_bfd_dwarf2_find_nearest_line (abfd
, dwarf_debug_sections
,
7643 section
, symbols
, offset
,
7644 filename_ptr
, functionname_ptr
,
7645 line_ptr
, discriminator_ptr
, 0,
7646 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7648 if (!*functionname_ptr
)
7649 elf_find_function (abfd
, section
, symbols
, offset
,
7650 *filename_ptr
? NULL
: filename_ptr
,
7656 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7657 &found
, filename_ptr
,
7658 functionname_ptr
, line_ptr
,
7659 &elf_tdata (abfd
)->line_info
))
7661 if (found
&& (*functionname_ptr
|| *line_ptr
))
7664 if (symbols
== NULL
)
7667 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7668 filename_ptr
, functionname_ptr
))
7675 /* Find the line for a symbol. */
7678 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7679 const char **filename_ptr
, unsigned int *line_ptr
)
7681 return _bfd_elf_find_line_discriminator (abfd
, symbols
, symbol
,
7682 filename_ptr
, line_ptr
,
7687 _bfd_elf_find_line_discriminator (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7688 const char **filename_ptr
,
7689 unsigned int *line_ptr
,
7690 unsigned int *discriminator_ptr
)
7692 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7693 filename_ptr
, line_ptr
, discriminator_ptr
, 0,
7694 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7697 /* After a call to bfd_find_nearest_line, successive calls to
7698 bfd_find_inliner_info can be used to get source information about
7699 each level of function inlining that terminated at the address
7700 passed to bfd_find_nearest_line. Currently this is only supported
7701 for DWARF2 with appropriate DWARF3 extensions. */
7704 _bfd_elf_find_inliner_info (bfd
*abfd
,
7705 const char **filename_ptr
,
7706 const char **functionname_ptr
,
7707 unsigned int *line_ptr
)
7710 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7711 functionname_ptr
, line_ptr
,
7712 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7717 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7719 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7720 int ret
= bed
->s
->sizeof_ehdr
;
7722 if (!info
->relocatable
)
7724 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
7726 if (phdr_size
== (bfd_size_type
) -1)
7728 struct elf_segment_map
*m
;
7731 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
7732 phdr_size
+= bed
->s
->sizeof_phdr
;
7735 phdr_size
= get_program_header_size (abfd
, info
);
7738 elf_tdata (abfd
)->program_header_size
= phdr_size
;
7746 _bfd_elf_set_section_contents (bfd
*abfd
,
7748 const void *location
,
7750 bfd_size_type count
)
7752 Elf_Internal_Shdr
*hdr
;
7755 if (! abfd
->output_has_begun
7756 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7759 hdr
= &elf_section_data (section
)->this_hdr
;
7760 pos
= hdr
->sh_offset
+ offset
;
7761 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7762 || bfd_bwrite (location
, count
, abfd
) != count
)
7769 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7770 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7771 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7776 /* Try to convert a non-ELF reloc into an ELF one. */
7779 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7781 /* Check whether we really have an ELF howto. */
7783 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7785 bfd_reloc_code_real_type code
;
7786 reloc_howto_type
*howto
;
7788 /* Alien reloc: Try to determine its type to replace it with an
7789 equivalent ELF reloc. */
7791 if (areloc
->howto
->pc_relative
)
7793 switch (areloc
->howto
->bitsize
)
7796 code
= BFD_RELOC_8_PCREL
;
7799 code
= BFD_RELOC_12_PCREL
;
7802 code
= BFD_RELOC_16_PCREL
;
7805 code
= BFD_RELOC_24_PCREL
;
7808 code
= BFD_RELOC_32_PCREL
;
7811 code
= BFD_RELOC_64_PCREL
;
7817 howto
= bfd_reloc_type_lookup (abfd
, code
);
7819 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7821 if (howto
->pcrel_offset
)
7822 areloc
->addend
+= areloc
->address
;
7824 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7829 switch (areloc
->howto
->bitsize
)
7835 code
= BFD_RELOC_14
;
7838 code
= BFD_RELOC_16
;
7841 code
= BFD_RELOC_26
;
7844 code
= BFD_RELOC_32
;
7847 code
= BFD_RELOC_64
;
7853 howto
= bfd_reloc_type_lookup (abfd
, code
);
7857 areloc
->howto
= howto
;
7865 (*_bfd_error_handler
)
7866 (_("%B: unsupported relocation type %s"),
7867 abfd
, areloc
->howto
->name
);
7868 bfd_set_error (bfd_error_bad_value
);
7873 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7875 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
7876 if (bfd_get_format (abfd
) == bfd_object
&& tdata
!= NULL
)
7878 if (elf_shstrtab (abfd
) != NULL
)
7879 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7880 _bfd_dwarf2_cleanup_debug_info (abfd
, &tdata
->dwarf2_find_line_info
);
7883 return _bfd_generic_close_and_cleanup (abfd
);
7886 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7887 in the relocation's offset. Thus we cannot allow any sort of sanity
7888 range-checking to interfere. There is nothing else to do in processing
7891 bfd_reloc_status_type
7892 _bfd_elf_rel_vtable_reloc_fn
7893 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7894 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7895 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7896 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7898 return bfd_reloc_ok
;
7901 /* Elf core file support. Much of this only works on native
7902 toolchains, since we rely on knowing the
7903 machine-dependent procfs structure in order to pick
7904 out details about the corefile. */
7906 #ifdef HAVE_SYS_PROCFS_H
7907 /* Needed for new procfs interface on sparc-solaris. */
7908 # define _STRUCTURED_PROC 1
7909 # include <sys/procfs.h>
7912 /* Return a PID that identifies a "thread" for threaded cores, or the
7913 PID of the main process for non-threaded cores. */
7916 elfcore_make_pid (bfd
*abfd
)
7920 pid
= elf_tdata (abfd
)->core_lwpid
;
7922 pid
= elf_tdata (abfd
)->core_pid
;
7927 /* If there isn't a section called NAME, make one, using
7928 data from SECT. Note, this function will generate a
7929 reference to NAME, so you shouldn't deallocate or
7933 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7937 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7940 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7944 sect2
->size
= sect
->size
;
7945 sect2
->filepos
= sect
->filepos
;
7946 sect2
->alignment_power
= sect
->alignment_power
;
7950 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7951 actually creates up to two pseudosections:
7952 - For the single-threaded case, a section named NAME, unless
7953 such a section already exists.
7954 - For the multi-threaded case, a section named "NAME/PID", where
7955 PID is elfcore_make_pid (abfd).
7956 Both pseudosections have identical contents. */
7958 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7964 char *threaded_name
;
7968 /* Build the section name. */
7970 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7971 len
= strlen (buf
) + 1;
7972 threaded_name
= (char *) bfd_alloc (abfd
, len
);
7973 if (threaded_name
== NULL
)
7975 memcpy (threaded_name
, buf
, len
);
7977 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7982 sect
->filepos
= filepos
;
7983 sect
->alignment_power
= 2;
7985 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7988 /* prstatus_t exists on:
7990 linux 2.[01] + glibc
7994 #if defined (HAVE_PRSTATUS_T)
7997 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8002 if (note
->descsz
== sizeof (prstatus_t
))
8006 size
= sizeof (prstat
.pr_reg
);
8007 offset
= offsetof (prstatus_t
, pr_reg
);
8008 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8010 /* Do not overwrite the core signal if it
8011 has already been set by another thread. */
8012 if (elf_tdata (abfd
)->core_signal
== 0)
8013 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
8014 if (elf_tdata (abfd
)->core_pid
== 0)
8015 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
8017 /* pr_who exists on:
8020 pr_who doesn't exist on:
8023 #if defined (HAVE_PRSTATUS_T_PR_WHO)
8024 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
8026 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_pid
;
8029 #if defined (HAVE_PRSTATUS32_T)
8030 else if (note
->descsz
== sizeof (prstatus32_t
))
8032 /* 64-bit host, 32-bit corefile */
8033 prstatus32_t prstat
;
8035 size
= sizeof (prstat
.pr_reg
);
8036 offset
= offsetof (prstatus32_t
, pr_reg
);
8037 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
8039 /* Do not overwrite the core signal if it
8040 has already been set by another thread. */
8041 if (elf_tdata (abfd
)->core_signal
== 0)
8042 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
8043 if (elf_tdata (abfd
)->core_pid
== 0)
8044 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
8046 /* pr_who exists on:
8049 pr_who doesn't exist on:
8052 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
8053 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
8055 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_pid
;
8058 #endif /* HAVE_PRSTATUS32_T */
8061 /* Fail - we don't know how to handle any other
8062 note size (ie. data object type). */
8066 /* Make a ".reg/999" section and a ".reg" section. */
8067 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
8068 size
, note
->descpos
+ offset
);
8070 #endif /* defined (HAVE_PRSTATUS_T) */
8072 /* Create a pseudosection containing the exact contents of NOTE. */
8074 elfcore_make_note_pseudosection (bfd
*abfd
,
8076 Elf_Internal_Note
*note
)
8078 return _bfd_elfcore_make_pseudosection (abfd
, name
,
8079 note
->descsz
, note
->descpos
);
8082 /* There isn't a consistent prfpregset_t across platforms,
8083 but it doesn't matter, because we don't have to pick this
8084 data structure apart. */
8087 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8089 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8092 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8093 type of NT_PRXFPREG. Just include the whole note's contents
8097 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8099 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8102 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8103 with a note type of NT_X86_XSTATE. Just include the whole note's
8104 contents literally. */
8107 elfcore_grok_xstatereg (bfd
*abfd
, Elf_Internal_Note
*note
)
8109 return elfcore_make_note_pseudosection (abfd
, ".reg-xstate", note
);
8113 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
8115 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
8119 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
8121 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
8125 elfcore_grok_s390_high_gprs (bfd
*abfd
, Elf_Internal_Note
*note
)
8127 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-high-gprs", note
);
8131 elfcore_grok_s390_timer (bfd
*abfd
, Elf_Internal_Note
*note
)
8133 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-timer", note
);
8137 elfcore_grok_s390_todcmp (bfd
*abfd
, Elf_Internal_Note
*note
)
8139 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todcmp", note
);
8143 elfcore_grok_s390_todpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
8145 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-todpreg", note
);
8149 elfcore_grok_s390_ctrs (bfd
*abfd
, Elf_Internal_Note
*note
)
8151 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-ctrs", note
);
8155 elfcore_grok_s390_prefix (bfd
*abfd
, Elf_Internal_Note
*note
)
8157 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-prefix", note
);
8161 elfcore_grok_s390_last_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8163 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-last-break", note
);
8167 elfcore_grok_s390_system_call (bfd
*abfd
, Elf_Internal_Note
*note
)
8169 return elfcore_make_note_pseudosection (abfd
, ".reg-s390-system-call", note
);
8173 elfcore_grok_arm_vfp (bfd
*abfd
, Elf_Internal_Note
*note
)
8175 return elfcore_make_note_pseudosection (abfd
, ".reg-arm-vfp", note
);
8179 elfcore_grok_aarch_tls (bfd
*abfd
, Elf_Internal_Note
*note
)
8181 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-tls", note
);
8185 elfcore_grok_aarch_hw_break (bfd
*abfd
, Elf_Internal_Note
*note
)
8187 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-break", note
);
8191 elfcore_grok_aarch_hw_watch (bfd
*abfd
, Elf_Internal_Note
*note
)
8193 return elfcore_make_note_pseudosection (abfd
, ".reg-aarch-hw-watch", note
);
8196 #if defined (HAVE_PRPSINFO_T)
8197 typedef prpsinfo_t elfcore_psinfo_t
;
8198 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
8199 typedef prpsinfo32_t elfcore_psinfo32_t
;
8203 #if defined (HAVE_PSINFO_T)
8204 typedef psinfo_t elfcore_psinfo_t
;
8205 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
8206 typedef psinfo32_t elfcore_psinfo32_t
;
8210 /* return a malloc'ed copy of a string at START which is at
8211 most MAX bytes long, possibly without a terminating '\0'.
8212 the copy will always have a terminating '\0'. */
8215 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
8218 char *end
= (char *) memchr (start
, '\0', max
);
8226 dups
= (char *) bfd_alloc (abfd
, len
+ 1);
8230 memcpy (dups
, start
, len
);
8236 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8238 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8240 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
8242 elfcore_psinfo_t psinfo
;
8244 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8246 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8247 elf_tdata (abfd
)->core_pid
= psinfo
.pr_pid
;
8249 elf_tdata (abfd
)->core_program
8250 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8251 sizeof (psinfo
.pr_fname
));
8253 elf_tdata (abfd
)->core_command
8254 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8255 sizeof (psinfo
.pr_psargs
));
8257 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8258 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
8260 /* 64-bit host, 32-bit corefile */
8261 elfcore_psinfo32_t psinfo
;
8263 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
8265 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8266 elf_tdata (abfd
)->core_pid
= psinfo
.pr_pid
;
8268 elf_tdata (abfd
)->core_program
8269 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
8270 sizeof (psinfo
.pr_fname
));
8272 elf_tdata (abfd
)->core_command
8273 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
8274 sizeof (psinfo
.pr_psargs
));
8280 /* Fail - we don't know how to handle any other
8281 note size (ie. data object type). */
8285 /* Note that for some reason, a spurious space is tacked
8286 onto the end of the args in some (at least one anyway)
8287 implementations, so strip it off if it exists. */
8290 char *command
= elf_tdata (abfd
)->core_command
;
8291 int n
= strlen (command
);
8293 if (0 < n
&& command
[n
- 1] == ' ')
8294 command
[n
- 1] = '\0';
8299 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8301 #if defined (HAVE_PSTATUS_T)
8303 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8305 if (note
->descsz
== sizeof (pstatus_t
)
8306 #if defined (HAVE_PXSTATUS_T)
8307 || note
->descsz
== sizeof (pxstatus_t
)
8313 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8315 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
8317 #if defined (HAVE_PSTATUS32_T)
8318 else if (note
->descsz
== sizeof (pstatus32_t
))
8320 /* 64-bit host, 32-bit corefile */
8323 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
8325 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
8328 /* Could grab some more details from the "representative"
8329 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8330 NT_LWPSTATUS note, presumably. */
8334 #endif /* defined (HAVE_PSTATUS_T) */
8336 #if defined (HAVE_LWPSTATUS_T)
8338 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8340 lwpstatus_t lwpstat
;
8346 if (note
->descsz
!= sizeof (lwpstat
)
8347 #if defined (HAVE_LWPXSTATUS_T)
8348 && note
->descsz
!= sizeof (lwpxstatus_t
)
8353 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
8355 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
8356 /* Do not overwrite the core signal if it has already been set by
8358 if (elf_tdata (abfd
)->core_signal
== 0)
8359 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
8361 /* Make a ".reg/999" section. */
8363 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
8364 len
= strlen (buf
) + 1;
8365 name
= bfd_alloc (abfd
, len
);
8368 memcpy (name
, buf
, len
);
8370 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8374 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8375 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
8376 sect
->filepos
= note
->descpos
8377 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
8380 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8381 sect
->size
= sizeof (lwpstat
.pr_reg
);
8382 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
8385 sect
->alignment_power
= 2;
8387 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8390 /* Make a ".reg2/999" section */
8392 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
8393 len
= strlen (buf
) + 1;
8394 name
= bfd_alloc (abfd
, len
);
8397 memcpy (name
, buf
, len
);
8399 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8403 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8404 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
8405 sect
->filepos
= note
->descpos
8406 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
8409 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8410 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
8411 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
8414 sect
->alignment_power
= 2;
8416 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
8418 #endif /* defined (HAVE_LWPSTATUS_T) */
8421 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
8428 int is_active_thread
;
8431 if (note
->descsz
< 728)
8434 if (! CONST_STRNEQ (note
->namedata
, "win32"))
8437 type
= bfd_get_32 (abfd
, note
->descdata
);
8441 case 1 /* NOTE_INFO_PROCESS */:
8442 /* FIXME: need to add ->core_command. */
8443 /* process_info.pid */
8444 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8445 /* process_info.signal */
8446 elf_tdata (abfd
)->core_signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
8449 case 2 /* NOTE_INFO_THREAD */:
8450 /* Make a ".reg/999" section. */
8451 /* thread_info.tid */
8452 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
8454 len
= strlen (buf
) + 1;
8455 name
= (char *) bfd_alloc (abfd
, len
);
8459 memcpy (name
, buf
, len
);
8461 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8465 /* sizeof (thread_info.thread_context) */
8467 /* offsetof (thread_info.thread_context) */
8468 sect
->filepos
= note
->descpos
+ 12;
8469 sect
->alignment_power
= 2;
8471 /* thread_info.is_active_thread */
8472 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
8474 if (is_active_thread
)
8475 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
8479 case 3 /* NOTE_INFO_MODULE */:
8480 /* Make a ".module/xxxxxxxx" section. */
8481 /* module_info.base_address */
8482 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
8483 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
8485 len
= strlen (buf
) + 1;
8486 name
= (char *) bfd_alloc (abfd
, len
);
8490 memcpy (name
, buf
, len
);
8492 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8497 sect
->size
= note
->descsz
;
8498 sect
->filepos
= note
->descpos
;
8499 sect
->alignment_power
= 2;
8510 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8512 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8520 if (bed
->elf_backend_grok_prstatus
)
8521 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
8523 #if defined (HAVE_PRSTATUS_T)
8524 return elfcore_grok_prstatus (abfd
, note
);
8529 #if defined (HAVE_PSTATUS_T)
8531 return elfcore_grok_pstatus (abfd
, note
);
8534 #if defined (HAVE_LWPSTATUS_T)
8536 return elfcore_grok_lwpstatus (abfd
, note
);
8539 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
8540 return elfcore_grok_prfpreg (abfd
, note
);
8542 case NT_WIN32PSTATUS
:
8543 return elfcore_grok_win32pstatus (abfd
, note
);
8545 case NT_PRXFPREG
: /* Linux SSE extension */
8546 if (note
->namesz
== 6
8547 && strcmp (note
->namedata
, "LINUX") == 0)
8548 return elfcore_grok_prxfpreg (abfd
, note
);
8552 case NT_X86_XSTATE
: /* Linux XSAVE extension */
8553 if (note
->namesz
== 6
8554 && strcmp (note
->namedata
, "LINUX") == 0)
8555 return elfcore_grok_xstatereg (abfd
, note
);
8560 if (note
->namesz
== 6
8561 && strcmp (note
->namedata
, "LINUX") == 0)
8562 return elfcore_grok_ppc_vmx (abfd
, note
);
8567 if (note
->namesz
== 6
8568 && strcmp (note
->namedata
, "LINUX") == 0)
8569 return elfcore_grok_ppc_vsx (abfd
, note
);
8573 case NT_S390_HIGH_GPRS
:
8574 if (note
->namesz
== 6
8575 && strcmp (note
->namedata
, "LINUX") == 0)
8576 return elfcore_grok_s390_high_gprs (abfd
, note
);
8581 if (note
->namesz
== 6
8582 && strcmp (note
->namedata
, "LINUX") == 0)
8583 return elfcore_grok_s390_timer (abfd
, note
);
8587 case NT_S390_TODCMP
:
8588 if (note
->namesz
== 6
8589 && strcmp (note
->namedata
, "LINUX") == 0)
8590 return elfcore_grok_s390_todcmp (abfd
, note
);
8594 case NT_S390_TODPREG
:
8595 if (note
->namesz
== 6
8596 && strcmp (note
->namedata
, "LINUX") == 0)
8597 return elfcore_grok_s390_todpreg (abfd
, note
);
8602 if (note
->namesz
== 6
8603 && strcmp (note
->namedata
, "LINUX") == 0)
8604 return elfcore_grok_s390_ctrs (abfd
, note
);
8608 case NT_S390_PREFIX
:
8609 if (note
->namesz
== 6
8610 && strcmp (note
->namedata
, "LINUX") == 0)
8611 return elfcore_grok_s390_prefix (abfd
, note
);
8615 case NT_S390_LAST_BREAK
:
8616 if (note
->namesz
== 6
8617 && strcmp (note
->namedata
, "LINUX") == 0)
8618 return elfcore_grok_s390_last_break (abfd
, note
);
8622 case NT_S390_SYSTEM_CALL
:
8623 if (note
->namesz
== 6
8624 && strcmp (note
->namedata
, "LINUX") == 0)
8625 return elfcore_grok_s390_system_call (abfd
, note
);
8630 if (note
->namesz
== 6
8631 && strcmp (note
->namedata
, "LINUX") == 0)
8632 return elfcore_grok_arm_vfp (abfd
, note
);
8637 if (note
->namesz
== 6
8638 && strcmp (note
->namedata
, "LINUX") == 0)
8639 return elfcore_grok_aarch_tls (abfd
, note
);
8643 case NT_ARM_HW_BREAK
:
8644 if (note
->namesz
== 6
8645 && strcmp (note
->namedata
, "LINUX") == 0)
8646 return elfcore_grok_aarch_hw_break (abfd
, note
);
8650 case NT_ARM_HW_WATCH
:
8651 if (note
->namesz
== 6
8652 && strcmp (note
->namedata
, "LINUX") == 0)
8653 return elfcore_grok_aarch_hw_watch (abfd
, note
);
8659 if (bed
->elf_backend_grok_psinfo
)
8660 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
8662 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8663 return elfcore_grok_psinfo (abfd
, note
);
8670 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8675 sect
->size
= note
->descsz
;
8676 sect
->filepos
= note
->descpos
;
8677 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8683 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.file",
8687 return elfcore_make_note_pseudosection (abfd
, ".note.linuxcore.siginfo",
8693 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
8695 elf_tdata (abfd
)->build_id_size
= note
->descsz
;
8696 elf_tdata (abfd
)->build_id
= (bfd_byte
*) bfd_alloc (abfd
, note
->descsz
);
8697 if (elf_tdata (abfd
)->build_id
== NULL
)
8700 memcpy (elf_tdata (abfd
)->build_id
, note
->descdata
, note
->descsz
);
8706 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8713 case NT_GNU_BUILD_ID
:
8714 return elfobj_grok_gnu_build_id (abfd
, note
);
8719 elfobj_grok_stapsdt_note_1 (bfd
*abfd
, Elf_Internal_Note
*note
)
8721 struct sdt_note
*cur
=
8722 (struct sdt_note
*) bfd_alloc (abfd
, sizeof (struct sdt_note
)
8725 cur
->next
= (struct sdt_note
*) (elf_tdata (abfd
))->sdt_note_head
;
8726 cur
->size
= (bfd_size_type
) note
->descsz
;
8727 memcpy (cur
->data
, note
->descdata
, note
->descsz
);
8729 elf_tdata (abfd
)->sdt_note_head
= cur
;
8735 elfobj_grok_stapsdt_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8740 return elfobj_grok_stapsdt_note_1 (abfd
, note
);
8748 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8752 cp
= strchr (note
->namedata
, '@');
8755 *lwpidp
= atoi(cp
+ 1);
8762 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8764 /* Signal number at offset 0x08. */
8765 elf_tdata (abfd
)->core_signal
8766 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8768 /* Process ID at offset 0x50. */
8769 elf_tdata (abfd
)->core_pid
8770 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8772 /* Command name at 0x7c (max 32 bytes, including nul). */
8773 elf_tdata (abfd
)->core_command
8774 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8776 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8781 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8785 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8786 elf_tdata (abfd
)->core_lwpid
= lwp
;
8788 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8790 /* NetBSD-specific core "procinfo". Note that we expect to
8791 find this note before any of the others, which is fine,
8792 since the kernel writes this note out first when it
8793 creates a core file. */
8795 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8798 /* As of Jan 2002 there are no other machine-independent notes
8799 defined for NetBSD core files. If the note type is less
8800 than the start of the machine-dependent note types, we don't
8803 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8807 switch (bfd_get_arch (abfd
))
8809 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8810 PT_GETFPREGS == mach+2. */
8812 case bfd_arch_alpha
:
8813 case bfd_arch_sparc
:
8816 case NT_NETBSDCORE_FIRSTMACH
+0:
8817 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8819 case NT_NETBSDCORE_FIRSTMACH
+2:
8820 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8826 /* On all other arch's, PT_GETREGS == mach+1 and
8827 PT_GETFPREGS == mach+3. */
8832 case NT_NETBSDCORE_FIRSTMACH
+1:
8833 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8835 case NT_NETBSDCORE_FIRSTMACH
+3:
8836 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8846 elfcore_grok_openbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8848 /* Signal number at offset 0x08. */
8849 elf_tdata (abfd
)->core_signal
8850 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8852 /* Process ID at offset 0x20. */
8853 elf_tdata (abfd
)->core_pid
8854 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x20);
8856 /* Command name at 0x48 (max 32 bytes, including nul). */
8857 elf_tdata (abfd
)->core_command
8858 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x48, 31);
8864 elfcore_grok_openbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8866 if (note
->type
== NT_OPENBSD_PROCINFO
)
8867 return elfcore_grok_openbsd_procinfo (abfd
, note
);
8869 if (note
->type
== NT_OPENBSD_REGS
)
8870 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8872 if (note
->type
== NT_OPENBSD_FPREGS
)
8873 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8875 if (note
->type
== NT_OPENBSD_XFPREGS
)
8876 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
8878 if (note
->type
== NT_OPENBSD_AUXV
)
8880 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
8885 sect
->size
= note
->descsz
;
8886 sect
->filepos
= note
->descpos
;
8887 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8892 if (note
->type
== NT_OPENBSD_WCOOKIE
)
8894 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".wcookie",
8899 sect
->size
= note
->descsz
;
8900 sect
->filepos
= note
->descpos
;
8901 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
8910 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8912 void *ddata
= note
->descdata
;
8919 /* nto_procfs_status 'pid' field is at offset 0. */
8920 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8922 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8923 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8925 /* nto_procfs_status 'flags' field is at offset 8. */
8926 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8928 /* nto_procfs_status 'what' field is at offset 14. */
8929 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8931 elf_tdata (abfd
)->core_signal
= sig
;
8932 elf_tdata (abfd
)->core_lwpid
= *tid
;
8935 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8936 do not come from signals so we make sure we set the current
8937 thread just in case. */
8938 if (flags
& 0x00000080)
8939 elf_tdata (abfd
)->core_lwpid
= *tid
;
8941 /* Make a ".qnx_core_status/%d" section. */
8942 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8944 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8949 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8953 sect
->size
= note
->descsz
;
8954 sect
->filepos
= note
->descpos
;
8955 sect
->alignment_power
= 2;
8957 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8961 elfcore_grok_nto_regs (bfd
*abfd
,
8962 Elf_Internal_Note
*note
,
8970 /* Make a "(base)/%d" section. */
8971 sprintf (buf
, "%s/%ld", base
, tid
);
8973 name
= (char *) bfd_alloc (abfd
, strlen (buf
) + 1);
8978 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8982 sect
->size
= note
->descsz
;
8983 sect
->filepos
= note
->descpos
;
8984 sect
->alignment_power
= 2;
8986 /* This is the current thread. */
8987 if (elf_tdata (abfd
)->core_lwpid
== tid
)
8988 return elfcore_maybe_make_sect (abfd
, base
, sect
);
8993 #define BFD_QNT_CORE_INFO 7
8994 #define BFD_QNT_CORE_STATUS 8
8995 #define BFD_QNT_CORE_GREG 9
8996 #define BFD_QNT_CORE_FPREG 10
8999 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9001 /* Every GREG section has a STATUS section before it. Store the
9002 tid from the previous call to pass down to the next gregs
9004 static long tid
= 1;
9008 case BFD_QNT_CORE_INFO
:
9009 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
9010 case BFD_QNT_CORE_STATUS
:
9011 return elfcore_grok_nto_status (abfd
, note
, &tid
);
9012 case BFD_QNT_CORE_GREG
:
9013 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
9014 case BFD_QNT_CORE_FPREG
:
9015 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
9022 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
9028 /* Use note name as section name. */
9030 name
= (char *) bfd_alloc (abfd
, len
);
9033 memcpy (name
, note
->namedata
, len
);
9034 name
[len
- 1] = '\0';
9036 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
9040 sect
->size
= note
->descsz
;
9041 sect
->filepos
= note
->descpos
;
9042 sect
->alignment_power
= 1;
9047 /* Function: elfcore_write_note
9050 buffer to hold note, and current size of buffer
9054 size of data for note
9056 Writes note to end of buffer. ELF64 notes are written exactly as
9057 for ELF32, despite the current (as of 2006) ELF gabi specifying
9058 that they ought to have 8-byte namesz and descsz field, and have
9059 8-byte alignment. Other writers, eg. Linux kernel, do the same.
9062 Pointer to realloc'd buffer, *BUFSIZ updated. */
9065 elfcore_write_note (bfd
*abfd
,
9073 Elf_External_Note
*xnp
;
9080 namesz
= strlen (name
) + 1;
9082 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
9084 buf
= (char *) realloc (buf
, *bufsiz
+ newspace
);
9087 dest
= buf
+ *bufsiz
;
9088 *bufsiz
+= newspace
;
9089 xnp
= (Elf_External_Note
*) dest
;
9090 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
9091 H_PUT_32 (abfd
, size
, xnp
->descsz
);
9092 H_PUT_32 (abfd
, type
, xnp
->type
);
9096 memcpy (dest
, name
, namesz
);
9104 memcpy (dest
, input
, size
);
9115 elfcore_write_prpsinfo (bfd
*abfd
,
9121 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9123 if (bed
->elf_backend_write_core_note
!= NULL
)
9126 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9127 NT_PRPSINFO
, fname
, psargs
);
9132 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9133 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9134 if (bed
->s
->elfclass
== ELFCLASS32
)
9136 #if defined (HAVE_PSINFO32_T)
9138 int note_type
= NT_PSINFO
;
9141 int note_type
= NT_PRPSINFO
;
9144 memset (&data
, 0, sizeof (data
));
9145 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9146 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9147 return elfcore_write_note (abfd
, buf
, bufsiz
,
9148 "CORE", note_type
, &data
, sizeof (data
));
9153 #if defined (HAVE_PSINFO_T)
9155 int note_type
= NT_PSINFO
;
9158 int note_type
= NT_PRPSINFO
;
9161 memset (&data
, 0, sizeof (data
));
9162 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
9163 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
9164 return elfcore_write_note (abfd
, buf
, bufsiz
,
9165 "CORE", note_type
, &data
, sizeof (data
));
9167 #endif /* PSINFO_T or PRPSINFO_T */
9174 elfcore_write_linux_prpsinfo32
9175 (bfd
*abfd
, char *buf
, int *bufsiz
,
9176 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9178 struct elf_external_linux_prpsinfo32 data
;
9180 memset (&data
, 0, sizeof (data
));
9181 LINUX_PRPSINFO32_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9183 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE", NT_PRPSINFO
,
9184 &data
, sizeof (data
));
9188 elfcore_write_linux_prpsinfo64
9189 (bfd
*abfd
, char *buf
, int *bufsiz
,
9190 const struct elf_internal_linux_prpsinfo
*prpsinfo
)
9192 struct elf_external_linux_prpsinfo64 data
;
9194 memset (&data
, 0, sizeof (data
));
9195 LINUX_PRPSINFO64_SWAP_FIELDS (abfd
, prpsinfo
, data
);
9197 return elfcore_write_note (abfd
, buf
, bufsiz
,
9198 "CORE", NT_PRPSINFO
, &data
, sizeof (data
));
9202 elfcore_write_prstatus (bfd
*abfd
,
9209 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9211 if (bed
->elf_backend_write_core_note
!= NULL
)
9214 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
9216 pid
, cursig
, gregs
);
9221 #if defined (HAVE_PRSTATUS_T)
9222 #if defined (HAVE_PRSTATUS32_T)
9223 if (bed
->s
->elfclass
== ELFCLASS32
)
9225 prstatus32_t prstat
;
9227 memset (&prstat
, 0, sizeof (prstat
));
9228 prstat
.pr_pid
= pid
;
9229 prstat
.pr_cursig
= cursig
;
9230 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9231 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9232 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9239 memset (&prstat
, 0, sizeof (prstat
));
9240 prstat
.pr_pid
= pid
;
9241 prstat
.pr_cursig
= cursig
;
9242 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
9243 return elfcore_write_note (abfd
, buf
, bufsiz
, "CORE",
9244 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
9246 #endif /* HAVE_PRSTATUS_T */
9252 #if defined (HAVE_LWPSTATUS_T)
9254 elfcore_write_lwpstatus (bfd
*abfd
,
9261 lwpstatus_t lwpstat
;
9262 const char *note_name
= "CORE";
9264 memset (&lwpstat
, 0, sizeof (lwpstat
));
9265 lwpstat
.pr_lwpid
= pid
>> 16;
9266 lwpstat
.pr_cursig
= cursig
;
9267 #if defined (HAVE_LWPSTATUS_T_PR_REG)
9268 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
9269 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9271 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
9272 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
9274 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
9275 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
9278 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9279 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
9281 #endif /* HAVE_LWPSTATUS_T */
9283 #if defined (HAVE_PSTATUS_T)
9285 elfcore_write_pstatus (bfd
*abfd
,
9289 int cursig ATTRIBUTE_UNUSED
,
9290 const void *gregs ATTRIBUTE_UNUSED
)
9292 const char *note_name
= "CORE";
9293 #if defined (HAVE_PSTATUS32_T)
9294 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9296 if (bed
->s
->elfclass
== ELFCLASS32
)
9300 memset (&pstat
, 0, sizeof (pstat
));
9301 pstat
.pr_pid
= pid
& 0xffff;
9302 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
9303 NT_PSTATUS
, &pstat
, sizeof (pstat
));
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
));
9318 #endif /* HAVE_PSTATUS_T */
9321 elfcore_write_prfpreg (bfd
*abfd
,
9327 const char *note_name
= "CORE";
9328 return elfcore_write_note (abfd
, buf
, bufsiz
,
9329 note_name
, NT_FPREGSET
, fpregs
, size
);
9333 elfcore_write_prxfpreg (bfd
*abfd
,
9336 const void *xfpregs
,
9339 char *note_name
= "LINUX";
9340 return elfcore_write_note (abfd
, buf
, bufsiz
,
9341 note_name
, NT_PRXFPREG
, xfpregs
, size
);
9345 elfcore_write_xstatereg (bfd
*abfd
, char *buf
, int *bufsiz
,
9346 const void *xfpregs
, int size
)
9348 char *note_name
= "LINUX";
9349 return elfcore_write_note (abfd
, buf
, bufsiz
,
9350 note_name
, NT_X86_XSTATE
, xfpregs
, size
);
9354 elfcore_write_ppc_vmx (bfd
*abfd
,
9357 const void *ppc_vmx
,
9360 char *note_name
= "LINUX";
9361 return elfcore_write_note (abfd
, buf
, bufsiz
,
9362 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
9366 elfcore_write_ppc_vsx (bfd
*abfd
,
9369 const void *ppc_vsx
,
9372 char *note_name
= "LINUX";
9373 return elfcore_write_note (abfd
, buf
, bufsiz
,
9374 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
9378 elfcore_write_s390_high_gprs (bfd
*abfd
,
9381 const void *s390_high_gprs
,
9384 char *note_name
= "LINUX";
9385 return elfcore_write_note (abfd
, buf
, bufsiz
,
9386 note_name
, NT_S390_HIGH_GPRS
,
9387 s390_high_gprs
, size
);
9391 elfcore_write_s390_timer (bfd
*abfd
,
9394 const void *s390_timer
,
9397 char *note_name
= "LINUX";
9398 return elfcore_write_note (abfd
, buf
, bufsiz
,
9399 note_name
, NT_S390_TIMER
, s390_timer
, size
);
9403 elfcore_write_s390_todcmp (bfd
*abfd
,
9406 const void *s390_todcmp
,
9409 char *note_name
= "LINUX";
9410 return elfcore_write_note (abfd
, buf
, bufsiz
,
9411 note_name
, NT_S390_TODCMP
, s390_todcmp
, size
);
9415 elfcore_write_s390_todpreg (bfd
*abfd
,
9418 const void *s390_todpreg
,
9421 char *note_name
= "LINUX";
9422 return elfcore_write_note (abfd
, buf
, bufsiz
,
9423 note_name
, NT_S390_TODPREG
, s390_todpreg
, size
);
9427 elfcore_write_s390_ctrs (bfd
*abfd
,
9430 const void *s390_ctrs
,
9433 char *note_name
= "LINUX";
9434 return elfcore_write_note (abfd
, buf
, bufsiz
,
9435 note_name
, NT_S390_CTRS
, s390_ctrs
, size
);
9439 elfcore_write_s390_prefix (bfd
*abfd
,
9442 const void *s390_prefix
,
9445 char *note_name
= "LINUX";
9446 return elfcore_write_note (abfd
, buf
, bufsiz
,
9447 note_name
, NT_S390_PREFIX
, s390_prefix
, size
);
9451 elfcore_write_s390_last_break (bfd
*abfd
,
9454 const void *s390_last_break
,
9457 char *note_name
= "LINUX";
9458 return elfcore_write_note (abfd
, buf
, bufsiz
,
9459 note_name
, NT_S390_LAST_BREAK
,
9460 s390_last_break
, size
);
9464 elfcore_write_s390_system_call (bfd
*abfd
,
9467 const void *s390_system_call
,
9470 char *note_name
= "LINUX";
9471 return elfcore_write_note (abfd
, buf
, bufsiz
,
9472 note_name
, NT_S390_SYSTEM_CALL
,
9473 s390_system_call
, size
);
9477 elfcore_write_arm_vfp (bfd
*abfd
,
9480 const void *arm_vfp
,
9483 char *note_name
= "LINUX";
9484 return elfcore_write_note (abfd
, buf
, bufsiz
,
9485 note_name
, NT_ARM_VFP
, arm_vfp
, size
);
9489 elfcore_write_aarch_tls (bfd
*abfd
,
9492 const void *aarch_tls
,
9495 char *note_name
= "LINUX";
9496 return elfcore_write_note (abfd
, buf
, bufsiz
,
9497 note_name
, NT_ARM_TLS
, aarch_tls
, size
);
9501 elfcore_write_aarch_hw_break (bfd
*abfd
,
9504 const void *aarch_hw_break
,
9507 char *note_name
= "LINUX";
9508 return elfcore_write_note (abfd
, buf
, bufsiz
,
9509 note_name
, NT_ARM_HW_BREAK
, aarch_hw_break
, size
);
9513 elfcore_write_aarch_hw_watch (bfd
*abfd
,
9516 const void *aarch_hw_watch
,
9519 char *note_name
= "LINUX";
9520 return elfcore_write_note (abfd
, buf
, bufsiz
,
9521 note_name
, NT_ARM_HW_WATCH
, aarch_hw_watch
, size
);
9525 elfcore_write_register_note (bfd
*abfd
,
9528 const char *section
,
9532 if (strcmp (section
, ".reg2") == 0)
9533 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
9534 if (strcmp (section
, ".reg-xfp") == 0)
9535 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
9536 if (strcmp (section
, ".reg-xstate") == 0)
9537 return elfcore_write_xstatereg (abfd
, buf
, bufsiz
, data
, size
);
9538 if (strcmp (section
, ".reg-ppc-vmx") == 0)
9539 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
9540 if (strcmp (section
, ".reg-ppc-vsx") == 0)
9541 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
9542 if (strcmp (section
, ".reg-s390-high-gprs") == 0)
9543 return elfcore_write_s390_high_gprs (abfd
, buf
, bufsiz
, data
, size
);
9544 if (strcmp (section
, ".reg-s390-timer") == 0)
9545 return elfcore_write_s390_timer (abfd
, buf
, bufsiz
, data
, size
);
9546 if (strcmp (section
, ".reg-s390-todcmp") == 0)
9547 return elfcore_write_s390_todcmp (abfd
, buf
, bufsiz
, data
, size
);
9548 if (strcmp (section
, ".reg-s390-todpreg") == 0)
9549 return elfcore_write_s390_todpreg (abfd
, buf
, bufsiz
, data
, size
);
9550 if (strcmp (section
, ".reg-s390-ctrs") == 0)
9551 return elfcore_write_s390_ctrs (abfd
, buf
, bufsiz
, data
, size
);
9552 if (strcmp (section
, ".reg-s390-prefix") == 0)
9553 return elfcore_write_s390_prefix (abfd
, buf
, bufsiz
, data
, size
);
9554 if (strcmp (section
, ".reg-s390-last-break") == 0)
9555 return elfcore_write_s390_last_break (abfd
, buf
, bufsiz
, data
, size
);
9556 if (strcmp (section
, ".reg-s390-system-call") == 0)
9557 return elfcore_write_s390_system_call (abfd
, buf
, bufsiz
, data
, size
);
9558 if (strcmp (section
, ".reg-arm-vfp") == 0)
9559 return elfcore_write_arm_vfp (abfd
, buf
, bufsiz
, data
, size
);
9560 if (strcmp (section
, ".reg-aarch-tls") == 0)
9561 return elfcore_write_aarch_tls (abfd
, buf
, bufsiz
, data
, size
);
9562 if (strcmp (section
, ".reg-aarch-hw-break") == 0)
9563 return elfcore_write_aarch_hw_break (abfd
, buf
, bufsiz
, data
, size
);
9564 if (strcmp (section
, ".reg-aarch-hw-watch") == 0)
9565 return elfcore_write_aarch_hw_watch (abfd
, buf
, bufsiz
, data
, size
);
9570 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
9575 while (p
< buf
+ size
)
9577 /* FIXME: bad alignment assumption. */
9578 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
9579 Elf_Internal_Note in
;
9581 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
9584 in
.type
= H_GET_32 (abfd
, xnp
->type
);
9586 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
9587 in
.namedata
= xnp
->name
;
9588 if (in
.namesz
> buf
- in
.namedata
+ size
)
9591 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
9592 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
9593 in
.descpos
= offset
+ (in
.descdata
- buf
);
9595 && (in
.descdata
>= buf
+ size
9596 || in
.descsz
> buf
- in
.descdata
+ size
))
9599 switch (bfd_get_format (abfd
))
9605 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
9607 if (! elfcore_grok_netbsd_note (abfd
, &in
))
9610 else if (CONST_STRNEQ (in
.namedata
, "OpenBSD"))
9612 if (! elfcore_grok_openbsd_note (abfd
, &in
))
9615 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
9617 if (! elfcore_grok_nto_note (abfd
, &in
))
9620 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
9622 if (! elfcore_grok_spu_note (abfd
, &in
))
9627 if (! elfcore_grok_note (abfd
, &in
))
9633 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
9635 if (! elfobj_grok_gnu_note (abfd
, &in
))
9638 else if (in
.namesz
== sizeof "stapsdt"
9639 && strcmp (in
.namedata
, "stapsdt") == 0)
9641 if (! elfobj_grok_stapsdt_note (abfd
, &in
))
9647 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
9654 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
9661 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
9664 buf
= (char *) bfd_malloc (size
);
9668 if (bfd_bread (buf
, size
, abfd
) != size
9669 || !elf_parse_notes (abfd
, buf
, size
, offset
))
9679 /* Providing external access to the ELF program header table. */
9681 /* Return an upper bound on the number of bytes required to store a
9682 copy of ABFD's program header table entries. Return -1 if an error
9683 occurs; bfd_get_error will return an appropriate code. */
9686 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
9688 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9690 bfd_set_error (bfd_error_wrong_format
);
9694 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
9697 /* Copy ABFD's program header table entries to *PHDRS. The entries
9698 will be stored as an array of Elf_Internal_Phdr structures, as
9699 defined in include/elf/internal.h. To find out how large the
9700 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9702 Return the number of program header table entries read, or -1 if an
9703 error occurs; bfd_get_error will return an appropriate code. */
9706 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
9710 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
9712 bfd_set_error (bfd_error_wrong_format
);
9716 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
9717 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
9718 num_phdrs
* sizeof (Elf_Internal_Phdr
));
9723 enum elf_reloc_type_class
9724 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
9726 return reloc_class_normal
;
9729 /* For RELA architectures, return the relocation value for a
9730 relocation against a local symbol. */
9733 _bfd_elf_rela_local_sym (bfd
*abfd
,
9734 Elf_Internal_Sym
*sym
,
9736 Elf_Internal_Rela
*rel
)
9738 asection
*sec
= *psec
;
9741 relocation
= (sec
->output_section
->vma
9742 + sec
->output_offset
9744 if ((sec
->flags
& SEC_MERGE
)
9745 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
9746 && sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
)
9749 _bfd_merged_section_offset (abfd
, psec
,
9750 elf_section_data (sec
)->sec_info
,
9751 sym
->st_value
+ rel
->r_addend
);
9754 /* If we have changed the section, and our original section is
9755 marked with SEC_EXCLUDE, it means that the original
9756 SEC_MERGE section has been completely subsumed in some
9757 other SEC_MERGE section. In this case, we need to leave
9758 some info around for --emit-relocs. */
9759 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
9760 sec
->kept_section
= *psec
;
9763 rel
->r_addend
-= relocation
;
9764 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
9770 _bfd_elf_rel_local_sym (bfd
*abfd
,
9771 Elf_Internal_Sym
*sym
,
9775 asection
*sec
= *psec
;
9777 if (sec
->sec_info_type
!= SEC_INFO_TYPE_MERGE
)
9778 return sym
->st_value
+ addend
;
9780 return _bfd_merged_section_offset (abfd
, psec
,
9781 elf_section_data (sec
)->sec_info
,
9782 sym
->st_value
+ addend
);
9786 _bfd_elf_section_offset (bfd
*abfd
,
9787 struct bfd_link_info
*info
,
9791 switch (sec
->sec_info_type
)
9793 case SEC_INFO_TYPE_STABS
:
9794 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
9796 case SEC_INFO_TYPE_EH_FRAME
:
9797 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
9799 if ((sec
->flags
& SEC_ELF_REVERSE_COPY
) != 0)
9801 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9802 bfd_size_type address_size
= bed
->s
->arch_size
/ 8;
9803 offset
= sec
->size
- offset
- address_size
;
9809 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9810 reconstruct an ELF file by reading the segments out of remote memory
9811 based on the ELF file header at EHDR_VMA and the ELF program headers it
9812 points to. If not null, *LOADBASEP is filled in with the difference
9813 between the VMAs from which the segments were read, and the VMAs the
9814 file headers (and hence BFD's idea of each section's VMA) put them at.
9816 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9817 remote memory at target address VMA into the local buffer at MYADDR; it
9818 should return zero on success or an `errno' code on failure. TEMPL must
9819 be a BFD for an ELF target with the word size and byte order found in
9820 the remote memory. */
9823 bfd_elf_bfd_from_remote_memory
9827 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, bfd_size_type
))
9829 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
9830 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
9834 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
9835 long symcount ATTRIBUTE_UNUSED
,
9836 asymbol
**syms ATTRIBUTE_UNUSED
,
9841 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
9844 const char *relplt_name
;
9845 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
9849 Elf_Internal_Shdr
*hdr
;
9855 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
9858 if (dynsymcount
<= 0)
9861 if (!bed
->plt_sym_val
)
9864 relplt_name
= bed
->relplt_name
;
9865 if (relplt_name
== NULL
)
9866 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
9867 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
9871 hdr
= &elf_section_data (relplt
)->this_hdr
;
9872 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
9873 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
9876 plt
= bfd_get_section_by_name (abfd
, ".plt");
9880 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
9881 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
9884 count
= relplt
->size
/ hdr
->sh_entsize
;
9885 size
= count
* sizeof (asymbol
);
9886 p
= relplt
->relocation
;
9887 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9889 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
9893 size
+= sizeof ("+0x") - 1 + 8 + 8 * (bed
->s
->elfclass
== ELFCLASS64
);
9895 size
+= sizeof ("+0x") - 1 + 8;
9900 s
= *ret
= (asymbol
*) bfd_malloc (size
);
9904 names
= (char *) (s
+ count
);
9905 p
= relplt
->relocation
;
9907 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
9912 addr
= bed
->plt_sym_val (i
, plt
, p
);
9913 if (addr
== (bfd_vma
) -1)
9916 *s
= **p
->sym_ptr_ptr
;
9917 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
9918 we are defining a symbol, ensure one of them is set. */
9919 if ((s
->flags
& BSF_LOCAL
) == 0)
9920 s
->flags
|= BSF_GLOBAL
;
9921 s
->flags
|= BSF_SYNTHETIC
;
9923 s
->value
= addr
- plt
->vma
;
9926 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
9927 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
9933 memcpy (names
, "+0x", sizeof ("+0x") - 1);
9934 names
+= sizeof ("+0x") - 1;
9935 bfd_sprintf_vma (abfd
, buf
, p
->addend
);
9936 for (a
= buf
; *a
== '0'; ++a
)
9939 memcpy (names
, a
, len
);
9942 memcpy (names
, "@plt", sizeof ("@plt"));
9943 names
+= sizeof ("@plt");
9950 /* It is only used by x86-64 so far. */
9951 asection _bfd_elf_large_com_section
9952 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
9953 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
9956 _bfd_elf_set_osabi (bfd
* abfd
,
9957 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
9959 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
9961 i_ehdrp
= elf_elfheader (abfd
);
9963 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
9965 /* To make things simpler for the loader on Linux systems we set the
9966 osabi field to ELFOSABI_GNU if the binary contains symbols of
9967 the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding. */
9968 if (i_ehdrp
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
9969 && elf_tdata (abfd
)->has_gnu_symbols
)
9970 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_GNU
;
9974 /* Return TRUE for ELF symbol types that represent functions.
9975 This is the default version of this function, which is sufficient for
9976 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */
9979 _bfd_elf_is_function_type (unsigned int type
)
9981 return (type
== STT_FUNC
9982 || type
== STT_GNU_IFUNC
);
9985 /* If the ELF symbol SYM might be a function in SEC, return the
9986 function size and set *CODE_OFF to the function's entry point,
9987 otherwise return zero. */
9990 _bfd_elf_maybe_function_sym (const asymbol
*sym
, asection
*sec
,
9995 if ((sym
->flags
& (BSF_SECTION_SYM
| BSF_FILE
| BSF_OBJECT
9996 | BSF_THREAD_LOCAL
| BSF_RELC
| BSF_SRELC
)) != 0
9997 || sym
->section
!= sec
)
10000 *code_off
= sym
->value
;
10002 if (!(sym
->flags
& BSF_SYNTHETIC
))
10003 size
= ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_size
;