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
5 Free Software Foundation, Inc.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
29 BFD support for ELF formats is being worked on.
30 Currently, the best supported back ends are for sparc and i386
31 (running svr4 or Solaris 2).
33 Documentation of the internals of the support code still needs
34 to be written. The code is changing quickly enough that we
35 haven't bothered yet. */
37 /* For sparc64-cross-sparc32. */
45 #include "libiberty.h"
46 #include "safe-ctype.h"
48 static int elf_sort_sections (const void *, const void *);
49 static bfd_boolean
assign_file_positions_except_relocs (bfd
*, struct bfd_link_info
*);
50 static bfd_boolean
prep_headers (bfd
*);
51 static bfd_boolean
swap_out_syms (bfd
*, struct bfd_strtab_hash
**, int) ;
52 static bfd_boolean
elf_read_notes (bfd
*, file_ptr
, bfd_size_type
) ;
53 static bfd_boolean
elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
,
56 /* Swap version information in and out. The version information is
57 currently size independent. If that ever changes, this code will
58 need to move into elfcode.h. */
60 /* Swap in a Verdef structure. */
63 _bfd_elf_swap_verdef_in (bfd
*abfd
,
64 const Elf_External_Verdef
*src
,
65 Elf_Internal_Verdef
*dst
)
67 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
68 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
69 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
70 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
71 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
72 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
73 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
76 /* Swap out a Verdef structure. */
79 _bfd_elf_swap_verdef_out (bfd
*abfd
,
80 const Elf_Internal_Verdef
*src
,
81 Elf_External_Verdef
*dst
)
83 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
84 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
85 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
86 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
87 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
88 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
89 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
92 /* Swap in a Verdaux structure. */
95 _bfd_elf_swap_verdaux_in (bfd
*abfd
,
96 const Elf_External_Verdaux
*src
,
97 Elf_Internal_Verdaux
*dst
)
99 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
100 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
103 /* Swap out a Verdaux structure. */
106 _bfd_elf_swap_verdaux_out (bfd
*abfd
,
107 const Elf_Internal_Verdaux
*src
,
108 Elf_External_Verdaux
*dst
)
110 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
111 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
114 /* Swap in a Verneed structure. */
117 _bfd_elf_swap_verneed_in (bfd
*abfd
,
118 const Elf_External_Verneed
*src
,
119 Elf_Internal_Verneed
*dst
)
121 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
122 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
123 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
124 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
125 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
128 /* Swap out a Verneed structure. */
131 _bfd_elf_swap_verneed_out (bfd
*abfd
,
132 const Elf_Internal_Verneed
*src
,
133 Elf_External_Verneed
*dst
)
135 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
136 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
137 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
138 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
139 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
142 /* Swap in a Vernaux structure. */
145 _bfd_elf_swap_vernaux_in (bfd
*abfd
,
146 const Elf_External_Vernaux
*src
,
147 Elf_Internal_Vernaux
*dst
)
149 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
150 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
151 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
152 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
153 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
156 /* Swap out a Vernaux structure. */
159 _bfd_elf_swap_vernaux_out (bfd
*abfd
,
160 const Elf_Internal_Vernaux
*src
,
161 Elf_External_Vernaux
*dst
)
163 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
164 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
165 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
166 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
167 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
170 /* Swap in a Versym structure. */
173 _bfd_elf_swap_versym_in (bfd
*abfd
,
174 const Elf_External_Versym
*src
,
175 Elf_Internal_Versym
*dst
)
177 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
180 /* Swap out a Versym structure. */
183 _bfd_elf_swap_versym_out (bfd
*abfd
,
184 const Elf_Internal_Versym
*src
,
185 Elf_External_Versym
*dst
)
187 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
190 /* Standard ELF hash function. Do not change this function; you will
191 cause invalid hash tables to be generated. */
194 bfd_elf_hash (const char *namearg
)
196 const unsigned char *name
= (const unsigned char *) namearg
;
201 while ((ch
= *name
++) != '\0')
204 if ((g
= (h
& 0xf0000000)) != 0)
207 /* The ELF ABI says `h &= ~g', but this is equivalent in
208 this case and on some machines one insn instead of two. */
212 return h
& 0xffffffff;
215 /* DT_GNU_HASH hash function. Do not change this function; you will
216 cause invalid hash tables to be generated. */
219 bfd_elf_gnu_hash (const char *namearg
)
221 const unsigned char *name
= (const unsigned char *) namearg
;
222 unsigned long h
= 5381;
225 while ((ch
= *name
++) != '\0')
226 h
= (h
<< 5) + h
+ ch
;
227 return h
& 0xffffffff;
230 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
231 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */
233 bfd_elf_allocate_object (bfd
*abfd
,
235 enum elf_object_id object_id
)
237 BFD_ASSERT (object_size
>= sizeof (struct elf_obj_tdata
));
238 abfd
->tdata
.any
= bfd_zalloc (abfd
, object_size
);
239 if (abfd
->tdata
.any
== NULL
)
242 elf_object_id (abfd
) = object_id
;
243 elf_program_header_size (abfd
) = (bfd_size_type
) -1;
249 bfd_elf_make_generic_object (bfd
*abfd
)
251 return bfd_elf_allocate_object (abfd
, sizeof (struct elf_obj_tdata
),
256 bfd_elf_mkcorefile (bfd
*abfd
)
258 /* I think this can be done just like an object file. */
259 return bfd_elf_make_generic_object (abfd
);
263 bfd_elf_get_str_section (bfd
*abfd
, unsigned int shindex
)
265 Elf_Internal_Shdr
**i_shdrp
;
266 bfd_byte
*shstrtab
= NULL
;
268 bfd_size_type shstrtabsize
;
270 i_shdrp
= elf_elfsections (abfd
);
272 || shindex
>= elf_numsections (abfd
)
273 || i_shdrp
[shindex
] == 0)
276 shstrtab
= i_shdrp
[shindex
]->contents
;
277 if (shstrtab
== NULL
)
279 /* No cached one, attempt to read, and cache what we read. */
280 offset
= i_shdrp
[shindex
]->sh_offset
;
281 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
283 /* Allocate and clear an extra byte at the end, to prevent crashes
284 in case the string table is not terminated. */
285 if (shstrtabsize
+ 1 <= 1
286 || (shstrtab
= bfd_alloc (abfd
, shstrtabsize
+ 1)) == NULL
287 || bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
289 else if (bfd_bread (shstrtab
, shstrtabsize
, abfd
) != shstrtabsize
)
291 if (bfd_get_error () != bfd_error_system_call
)
292 bfd_set_error (bfd_error_file_truncated
);
294 /* Once we've failed to read it, make sure we don't keep
295 trying. Otherwise, we'll keep allocating space for
296 the string table over and over. */
297 i_shdrp
[shindex
]->sh_size
= 0;
300 shstrtab
[shstrtabsize
] = '\0';
301 i_shdrp
[shindex
]->contents
= shstrtab
;
303 return (char *) shstrtab
;
307 bfd_elf_string_from_elf_section (bfd
*abfd
,
308 unsigned int shindex
,
309 unsigned int strindex
)
311 Elf_Internal_Shdr
*hdr
;
316 if (elf_elfsections (abfd
) == NULL
|| shindex
>= elf_numsections (abfd
))
319 hdr
= elf_elfsections (abfd
)[shindex
];
321 if (hdr
->contents
== NULL
322 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
325 if (strindex
>= hdr
->sh_size
)
327 unsigned int shstrndx
= elf_elfheader(abfd
)->e_shstrndx
;
328 (*_bfd_error_handler
)
329 (_("%B: invalid string offset %u >= %lu for section `%s'"),
330 abfd
, strindex
, (unsigned long) hdr
->sh_size
,
331 (shindex
== shstrndx
&& strindex
== hdr
->sh_name
333 : bfd_elf_string_from_elf_section (abfd
, shstrndx
, hdr
->sh_name
)));
337 return ((char *) hdr
->contents
) + strindex
;
340 /* Read and convert symbols to internal format.
341 SYMCOUNT specifies the number of symbols to read, starting from
342 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
343 are non-NULL, they are used to store the internal symbols, external
344 symbols, and symbol section index extensions, respectively.
345 Returns a pointer to the internal symbol buffer (malloced if necessary)
346 or NULL if there were no symbols or some kind of problem. */
349 bfd_elf_get_elf_syms (bfd
*ibfd
,
350 Elf_Internal_Shdr
*symtab_hdr
,
353 Elf_Internal_Sym
*intsym_buf
,
355 Elf_External_Sym_Shndx
*extshndx_buf
)
357 Elf_Internal_Shdr
*shndx_hdr
;
359 const bfd_byte
*esym
;
360 Elf_External_Sym_Shndx
*alloc_extshndx
;
361 Elf_External_Sym_Shndx
*shndx
;
362 Elf_Internal_Sym
*alloc_intsym
;
363 Elf_Internal_Sym
*isym
;
364 Elf_Internal_Sym
*isymend
;
365 const struct elf_backend_data
*bed
;
370 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
376 /* Normal syms might have section extension entries. */
378 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
379 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
381 /* Read the symbols. */
383 alloc_extshndx
= NULL
;
385 bed
= get_elf_backend_data (ibfd
);
386 extsym_size
= bed
->s
->sizeof_sym
;
387 amt
= symcount
* extsym_size
;
388 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
389 if (extsym_buf
== NULL
)
391 alloc_ext
= bfd_malloc2 (symcount
, extsym_size
);
392 extsym_buf
= alloc_ext
;
394 if (extsym_buf
== NULL
395 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
396 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
402 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
406 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
407 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
408 if (extshndx_buf
== NULL
)
410 alloc_extshndx
= bfd_malloc2 (symcount
,
411 sizeof (Elf_External_Sym_Shndx
));
412 extshndx_buf
= alloc_extshndx
;
414 if (extshndx_buf
== NULL
415 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
416 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
423 if (intsym_buf
== NULL
)
425 alloc_intsym
= bfd_malloc2 (symcount
, sizeof (Elf_Internal_Sym
));
426 intsym_buf
= alloc_intsym
;
427 if (intsym_buf
== NULL
)
431 /* Convert the symbols to internal form. */
432 isymend
= intsym_buf
+ symcount
;
433 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
435 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
436 if (!(*bed
->s
->swap_symbol_in
) (ibfd
, esym
, shndx
, isym
))
438 symoffset
+= (esym
- (bfd_byte
*) extsym_buf
) / extsym_size
;
439 (*_bfd_error_handler
) (_("%B symbol number %lu references "
440 "nonexistent SHT_SYMTAB_SHNDX section"),
441 ibfd
, (unsigned long) symoffset
);
442 if (alloc_intsym
!= NULL
)
449 if (alloc_ext
!= NULL
)
451 if (alloc_extshndx
!= NULL
)
452 free (alloc_extshndx
);
457 /* Look up a symbol name. */
459 bfd_elf_sym_name (bfd
*abfd
,
460 Elf_Internal_Shdr
*symtab_hdr
,
461 Elf_Internal_Sym
*isym
,
465 unsigned int iname
= isym
->st_name
;
466 unsigned int shindex
= symtab_hdr
->sh_link
;
468 if (iname
== 0 && ELF_ST_TYPE (isym
->st_info
) == STT_SECTION
469 /* Check for a bogus st_shndx to avoid crashing. */
470 && isym
->st_shndx
< elf_numsections (abfd
))
472 iname
= elf_elfsections (abfd
)[isym
->st_shndx
]->sh_name
;
473 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
476 name
= bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
479 else if (sym_sec
&& *name
== '\0')
480 name
= bfd_section_name (abfd
, sym_sec
);
485 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
486 sections. The first element is the flags, the rest are section
489 typedef union elf_internal_group
{
490 Elf_Internal_Shdr
*shdr
;
492 } Elf_Internal_Group
;
494 /* Return the name of the group signature symbol. Why isn't the
495 signature just a string? */
498 group_signature (bfd
*abfd
, Elf_Internal_Shdr
*ghdr
)
500 Elf_Internal_Shdr
*hdr
;
501 unsigned char esym
[sizeof (Elf64_External_Sym
)];
502 Elf_External_Sym_Shndx eshndx
;
503 Elf_Internal_Sym isym
;
505 /* First we need to ensure the symbol table is available. Make sure
506 that it is a symbol table section. */
507 if (ghdr
->sh_link
>= elf_numsections (abfd
))
509 hdr
= elf_elfsections (abfd
) [ghdr
->sh_link
];
510 if (hdr
->sh_type
!= SHT_SYMTAB
511 || ! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
514 /* Go read the symbol. */
515 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
516 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
517 &isym
, esym
, &eshndx
) == NULL
)
520 return bfd_elf_sym_name (abfd
, hdr
, &isym
, NULL
);
523 /* Set next_in_group list pointer, and group name for NEWSECT. */
526 setup_group (bfd
*abfd
, Elf_Internal_Shdr
*hdr
, asection
*newsect
)
528 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
530 /* If num_group is zero, read in all SHT_GROUP sections. The count
531 is set to -1 if there are no SHT_GROUP sections. */
534 unsigned int i
, shnum
;
536 /* First count the number of groups. If we have a SHT_GROUP
537 section with just a flag word (ie. sh_size is 4), ignore it. */
538 shnum
= elf_numsections (abfd
);
541 #define IS_VALID_GROUP_SECTION_HEADER(shdr) \
542 ( (shdr)->sh_type == SHT_GROUP \
543 && (shdr)->sh_size >= (2 * GRP_ENTRY_SIZE) \
544 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \
545 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
547 for (i
= 0; i
< shnum
; i
++)
549 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
551 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
557 num_group
= (unsigned) -1;
558 elf_tdata (abfd
)->num_group
= num_group
;
562 /* We keep a list of elf section headers for group sections,
563 so we can find them quickly. */
566 elf_tdata (abfd
)->num_group
= num_group
;
567 elf_tdata (abfd
)->group_sect_ptr
568 = bfd_alloc2 (abfd
, num_group
, sizeof (Elf_Internal_Shdr
*));
569 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
573 for (i
= 0; i
< shnum
; i
++)
575 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
577 if (IS_VALID_GROUP_SECTION_HEADER (shdr
))
580 Elf_Internal_Group
*dest
;
582 /* Add to list of sections. */
583 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
586 /* Read the raw contents. */
587 BFD_ASSERT (sizeof (*dest
) >= 4);
588 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
589 shdr
->contents
= bfd_alloc2 (abfd
, shdr
->sh_size
,
591 /* PR binutils/4110: Handle corrupt group headers. */
592 if (shdr
->contents
== NULL
)
595 (_("%B: Corrupt size field in group section header: 0x%lx"), abfd
, shdr
->sh_size
);
596 bfd_set_error (bfd_error_bad_value
);
600 memset (shdr
->contents
, 0, amt
);
602 if (bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
603 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
607 /* Translate raw contents, a flag word followed by an
608 array of elf section indices all in target byte order,
609 to the flag word followed by an array of elf section
611 src
= shdr
->contents
+ shdr
->sh_size
;
612 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
619 idx
= H_GET_32 (abfd
, src
);
620 if (src
== shdr
->contents
)
623 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
624 shdr
->bfd_section
->flags
625 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
630 ((*_bfd_error_handler
)
631 (_("%B: invalid SHT_GROUP entry"), abfd
));
634 dest
->shdr
= elf_elfsections (abfd
)[idx
];
641 if (num_group
!= (unsigned) -1)
645 for (i
= 0; i
< num_group
; i
++)
647 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
648 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
649 unsigned int n_elt
= shdr
->sh_size
/ 4;
651 /* Look through this group's sections to see if current
652 section is a member. */
654 if ((++idx
)->shdr
== hdr
)
658 /* We are a member of this group. Go looking through
659 other members to see if any others are linked via
661 idx
= (Elf_Internal_Group
*) shdr
->contents
;
662 n_elt
= shdr
->sh_size
/ 4;
664 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
665 && elf_next_in_group (s
) != NULL
)
669 /* Snarf the group name from other member, and
670 insert current section in circular list. */
671 elf_group_name (newsect
) = elf_group_name (s
);
672 elf_next_in_group (newsect
) = elf_next_in_group (s
);
673 elf_next_in_group (s
) = newsect
;
679 gname
= group_signature (abfd
, shdr
);
682 elf_group_name (newsect
) = gname
;
684 /* Start a circular list with one element. */
685 elf_next_in_group (newsect
) = newsect
;
688 /* If the group section has been created, point to the
690 if (shdr
->bfd_section
!= NULL
)
691 elf_next_in_group (shdr
->bfd_section
) = newsect
;
699 if (elf_group_name (newsect
) == NULL
)
701 (*_bfd_error_handler
) (_("%B: no group info for section %A"),
708 _bfd_elf_setup_sections (bfd
*abfd
)
711 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
712 bfd_boolean result
= TRUE
;
715 /* Process SHF_LINK_ORDER. */
716 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
718 Elf_Internal_Shdr
*this_hdr
= &elf_section_data (s
)->this_hdr
;
719 if ((this_hdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
721 unsigned int elfsec
= this_hdr
->sh_link
;
722 /* FIXME: The old Intel compiler and old strip/objcopy may
723 not set the sh_link or sh_info fields. Hence we could
724 get the situation where elfsec is 0. */
727 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
728 if (bed
->link_order_error_handler
)
729 bed
->link_order_error_handler
730 (_("%B: warning: sh_link not set for section `%A'"),
735 asection
*link
= NULL
;
737 if (elfsec
< elf_numsections (abfd
))
739 this_hdr
= elf_elfsections (abfd
)[elfsec
];
740 link
= this_hdr
->bfd_section
;
744 Some strip/objcopy may leave an incorrect value in
745 sh_link. We don't want to proceed. */
748 (*_bfd_error_handler
)
749 (_("%B: sh_link [%d] in section `%A' is incorrect"),
750 s
->owner
, s
, elfsec
);
754 elf_linked_to_section (s
) = link
;
759 /* Process section groups. */
760 if (num_group
== (unsigned) -1)
763 for (i
= 0; i
< num_group
; i
++)
765 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
766 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
767 unsigned int n_elt
= shdr
->sh_size
/ 4;
770 if ((++idx
)->shdr
->bfd_section
)
771 elf_sec_group (idx
->shdr
->bfd_section
) = shdr
->bfd_section
;
772 else if (idx
->shdr
->sh_type
== SHT_RELA
773 || idx
->shdr
->sh_type
== SHT_REL
)
774 /* We won't include relocation sections in section groups in
775 output object files. We adjust the group section size here
776 so that relocatable link will work correctly when
777 relocation sections are in section group in input object
779 shdr
->bfd_section
->size
-= 4;
782 /* There are some unknown sections in the group. */
783 (*_bfd_error_handler
)
784 (_("%B: unknown [%d] section `%s' in group [%s]"),
786 (unsigned int) idx
->shdr
->sh_type
,
787 bfd_elf_string_from_elf_section (abfd
,
788 (elf_elfheader (abfd
)
791 shdr
->bfd_section
->name
);
799 bfd_elf_is_group_section (bfd
*abfd ATTRIBUTE_UNUSED
, const asection
*sec
)
801 return elf_next_in_group (sec
) != NULL
;
804 /* Make a BFD section from an ELF section. We store a pointer to the
805 BFD section in the bfd_section field of the header. */
808 _bfd_elf_make_section_from_shdr (bfd
*abfd
,
809 Elf_Internal_Shdr
*hdr
,
815 const struct elf_backend_data
*bed
;
817 if (hdr
->bfd_section
!= NULL
)
819 BFD_ASSERT (strcmp (name
,
820 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
824 newsect
= bfd_make_section_anyway (abfd
, name
);
828 hdr
->bfd_section
= newsect
;
829 elf_section_data (newsect
)->this_hdr
= *hdr
;
830 elf_section_data (newsect
)->this_idx
= shindex
;
832 /* Always use the real type/flags. */
833 elf_section_type (newsect
) = hdr
->sh_type
;
834 elf_section_flags (newsect
) = hdr
->sh_flags
;
836 newsect
->filepos
= hdr
->sh_offset
;
838 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
839 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
840 || ! bfd_set_section_alignment (abfd
, newsect
,
841 bfd_log2 (hdr
->sh_addralign
)))
844 flags
= SEC_NO_FLAGS
;
845 if (hdr
->sh_type
!= SHT_NOBITS
)
846 flags
|= SEC_HAS_CONTENTS
;
847 if (hdr
->sh_type
== SHT_GROUP
)
848 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
849 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
852 if (hdr
->sh_type
!= SHT_NOBITS
)
855 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
856 flags
|= SEC_READONLY
;
857 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
859 else if ((flags
& SEC_LOAD
) != 0)
861 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
864 newsect
->entsize
= hdr
->sh_entsize
;
865 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
866 flags
|= SEC_STRINGS
;
868 if (hdr
->sh_flags
& SHF_GROUP
)
869 if (!setup_group (abfd
, hdr
, newsect
))
871 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
872 flags
|= SEC_THREAD_LOCAL
;
874 if ((flags
& SEC_ALLOC
) == 0)
876 /* The debugging sections appear to be recognized only by name,
877 not any sort of flag. Their SEC_ALLOC bits are cleared. */
882 } debug_sections
[] =
884 { STRING_COMMA_LEN ("debug") }, /* 'd' */
885 { NULL
, 0 }, /* 'e' */
886 { NULL
, 0 }, /* 'f' */
887 { STRING_COMMA_LEN ("gnu.linkonce.wi.") }, /* 'g' */
888 { NULL
, 0 }, /* 'h' */
889 { NULL
, 0 }, /* 'i' */
890 { NULL
, 0 }, /* 'j' */
891 { NULL
, 0 }, /* 'k' */
892 { STRING_COMMA_LEN ("line") }, /* 'l' */
893 { NULL
, 0 }, /* 'm' */
894 { NULL
, 0 }, /* 'n' */
895 { NULL
, 0 }, /* 'o' */
896 { NULL
, 0 }, /* 'p' */
897 { NULL
, 0 }, /* 'q' */
898 { NULL
, 0 }, /* 'r' */
899 { STRING_COMMA_LEN ("stab") }, /* 's' */
900 { NULL
, 0 }, /* 't' */
901 { NULL
, 0 }, /* 'u' */
902 { NULL
, 0 }, /* 'v' */
903 { NULL
, 0 }, /* 'w' */
904 { NULL
, 0 }, /* 'x' */
905 { NULL
, 0 }, /* 'y' */
906 { STRING_COMMA_LEN ("zdebug") } /* 'z' */
911 int i
= name
[1] - 'd';
913 && i
< (int) ARRAY_SIZE (debug_sections
)
914 && debug_sections
[i
].name
!= NULL
915 && strncmp (&name
[1], debug_sections
[i
].name
,
916 debug_sections
[i
].len
) == 0)
917 flags
|= SEC_DEBUGGING
;
921 /* As a GNU extension, if the name begins with .gnu.linkonce, we
922 only link a single copy of the section. This is used to support
923 g++. g++ will emit each template expansion in its own section.
924 The symbols will be defined as weak, so that multiple definitions
925 are permitted. The GNU linker extension is to actually discard
926 all but one of the sections. */
927 if (CONST_STRNEQ (name
, ".gnu.linkonce")
928 && elf_next_in_group (newsect
) == NULL
)
929 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
931 bed
= get_elf_backend_data (abfd
);
932 if (bed
->elf_backend_section_flags
)
933 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
936 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
939 /* We do not parse the PT_NOTE segments as we are interested even in the
940 separate debug info files which may have the segments offsets corrupted.
941 PT_NOTEs from the core files are currently not parsed using BFD. */
942 if (hdr
->sh_type
== SHT_NOTE
)
946 if (!bfd_malloc_and_get_section (abfd
, newsect
, &contents
))
949 elf_parse_notes (abfd
, (char *) contents
, hdr
->sh_size
, -1);
953 if ((flags
& SEC_ALLOC
) != 0)
955 Elf_Internal_Phdr
*phdr
;
956 unsigned int i
, nload
;
958 /* Some ELF linkers produce binaries with all the program header
959 p_paddr fields zero. If we have such a binary with more than
960 one PT_LOAD header, then leave the section lma equal to vma
961 so that we don't create sections with overlapping lma. */
962 phdr
= elf_tdata (abfd
)->phdr
;
963 for (nload
= 0, i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
964 if (phdr
->p_paddr
!= 0)
966 else if (phdr
->p_type
== PT_LOAD
&& phdr
->p_memsz
!= 0)
968 if (i
>= elf_elfheader (abfd
)->e_phnum
&& nload
> 1)
971 phdr
= elf_tdata (abfd
)->phdr
;
972 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
974 /* This section is part of this segment if its file
975 offset plus size lies within the segment's memory
976 span and, if the section is loaded, the extent of the
977 loaded data lies within the extent of the segment.
979 Note - we used to check the p_paddr field as well, and
980 refuse to set the LMA if it was 0. This is wrong
981 though, as a perfectly valid initialised segment can
982 have a p_paddr of zero. Some architectures, eg ARM,
983 place special significance on the address 0 and
984 executables need to be able to have a segment which
985 covers this address. */
986 if (phdr
->p_type
== PT_LOAD
987 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
988 && (hdr
->sh_offset
+ hdr
->sh_size
989 <= phdr
->p_offset
+ phdr
->p_memsz
)
990 && ((flags
& SEC_LOAD
) == 0
991 || (hdr
->sh_offset
+ hdr
->sh_size
992 <= phdr
->p_offset
+ phdr
->p_filesz
)))
994 if ((flags
& SEC_LOAD
) == 0)
995 newsect
->lma
= (phdr
->p_paddr
996 + hdr
->sh_addr
- phdr
->p_vaddr
);
998 /* We used to use the same adjustment for SEC_LOAD
999 sections, but that doesn't work if the segment
1000 is packed with code from multiple VMAs.
1001 Instead we calculate the section LMA based on
1002 the segment LMA. It is assumed that the
1003 segment will contain sections with contiguous
1004 LMAs, even if the VMAs are not. */
1005 newsect
->lma
= (phdr
->p_paddr
1006 + hdr
->sh_offset
- phdr
->p_offset
);
1008 /* With contiguous segments, we can't tell from file
1009 offsets whether a section with zero size should
1010 be placed at the end of one segment or the
1011 beginning of the next. Decide based on vaddr. */
1012 if (hdr
->sh_addr
>= phdr
->p_vaddr
1013 && (hdr
->sh_addr
+ hdr
->sh_size
1014 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
1023 const char *const bfd_elf_section_type_names
[] = {
1024 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1025 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1026 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1029 /* ELF relocs are against symbols. If we are producing relocatable
1030 output, and the reloc is against an external symbol, and nothing
1031 has given us any additional addend, the resulting reloc will also
1032 be against the same symbol. In such a case, we don't want to
1033 change anything about the way the reloc is handled, since it will
1034 all be done at final link time. Rather than put special case code
1035 into bfd_perform_relocation, all the reloc types use this howto
1036 function. It just short circuits the reloc if producing
1037 relocatable output against an external symbol. */
1039 bfd_reloc_status_type
1040 bfd_elf_generic_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1041 arelent
*reloc_entry
,
1043 void *data ATTRIBUTE_UNUSED
,
1044 asection
*input_section
,
1046 char **error_message ATTRIBUTE_UNUSED
)
1048 if (output_bfd
!= NULL
1049 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
1050 && (! reloc_entry
->howto
->partial_inplace
1051 || reloc_entry
->addend
== 0))
1053 reloc_entry
->address
+= input_section
->output_offset
;
1054 return bfd_reloc_ok
;
1057 return bfd_reloc_continue
;
1060 /* Copy the program header and other data from one object module to
1064 _bfd_elf_copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1066 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1067 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1070 BFD_ASSERT (!elf_flags_init (obfd
)
1071 || (elf_elfheader (obfd
)->e_flags
1072 == elf_elfheader (ibfd
)->e_flags
));
1074 elf_gp (obfd
) = elf_gp (ibfd
);
1075 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1076 elf_flags_init (obfd
) = TRUE
;
1078 /* Copy object attributes. */
1079 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
1085 get_segment_type (unsigned int p_type
)
1090 case PT_NULL
: pt
= "NULL"; break;
1091 case PT_LOAD
: pt
= "LOAD"; break;
1092 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1093 case PT_INTERP
: pt
= "INTERP"; break;
1094 case PT_NOTE
: pt
= "NOTE"; break;
1095 case PT_SHLIB
: pt
= "SHLIB"; break;
1096 case PT_PHDR
: pt
= "PHDR"; break;
1097 case PT_TLS
: pt
= "TLS"; break;
1098 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1099 case PT_GNU_STACK
: pt
= "STACK"; break;
1100 case PT_GNU_RELRO
: pt
= "RELRO"; break;
1101 default: pt
= NULL
; break;
1106 /* Print out the program headers. */
1109 _bfd_elf_print_private_bfd_data (bfd
*abfd
, void *farg
)
1112 Elf_Internal_Phdr
*p
;
1114 bfd_byte
*dynbuf
= NULL
;
1116 p
= elf_tdata (abfd
)->phdr
;
1121 fprintf (f
, _("\nProgram Header:\n"));
1122 c
= elf_elfheader (abfd
)->e_phnum
;
1123 for (i
= 0; i
< c
; i
++, p
++)
1125 const char *pt
= get_segment_type (p
->p_type
);
1130 sprintf (buf
, "0x%lx", p
->p_type
);
1133 fprintf (f
, "%8s off 0x", pt
);
1134 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1135 fprintf (f
, " vaddr 0x");
1136 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1137 fprintf (f
, " paddr 0x");
1138 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1139 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1140 fprintf (f
, " filesz 0x");
1141 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1142 fprintf (f
, " memsz 0x");
1143 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1144 fprintf (f
, " flags %c%c%c",
1145 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1146 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1147 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1148 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1149 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1154 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1157 unsigned int elfsec
;
1158 unsigned long shlink
;
1159 bfd_byte
*extdyn
, *extdynend
;
1161 void (*swap_dyn_in
) (bfd
*, const void *, Elf_Internal_Dyn
*);
1163 fprintf (f
, _("\nDynamic Section:\n"));
1165 if (!bfd_malloc_and_get_section (abfd
, s
, &dynbuf
))
1168 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1169 if (elfsec
== SHN_BAD
)
1171 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1173 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1174 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1177 extdynend
= extdyn
+ s
->size
;
1178 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1180 Elf_Internal_Dyn dyn
;
1181 const char *name
= "";
1183 bfd_boolean stringp
;
1184 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1186 (*swap_dyn_in
) (abfd
, extdyn
, &dyn
);
1188 if (dyn
.d_tag
== DT_NULL
)
1195 if (bed
->elf_backend_get_target_dtag
)
1196 name
= (*bed
->elf_backend_get_target_dtag
) (dyn
.d_tag
);
1198 if (!strcmp (name
, ""))
1200 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1205 case DT_NEEDED
: name
= "NEEDED"; stringp
= TRUE
; break;
1206 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1207 case DT_PLTGOT
: name
= "PLTGOT"; break;
1208 case DT_HASH
: name
= "HASH"; break;
1209 case DT_STRTAB
: name
= "STRTAB"; break;
1210 case DT_SYMTAB
: name
= "SYMTAB"; break;
1211 case DT_RELA
: name
= "RELA"; break;
1212 case DT_RELASZ
: name
= "RELASZ"; break;
1213 case DT_RELAENT
: name
= "RELAENT"; break;
1214 case DT_STRSZ
: name
= "STRSZ"; break;
1215 case DT_SYMENT
: name
= "SYMENT"; break;
1216 case DT_INIT
: name
= "INIT"; break;
1217 case DT_FINI
: name
= "FINI"; break;
1218 case DT_SONAME
: name
= "SONAME"; stringp
= TRUE
; break;
1219 case DT_RPATH
: name
= "RPATH"; stringp
= TRUE
; break;
1220 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1221 case DT_REL
: name
= "REL"; break;
1222 case DT_RELSZ
: name
= "RELSZ"; break;
1223 case DT_RELENT
: name
= "RELENT"; break;
1224 case DT_PLTREL
: name
= "PLTREL"; break;
1225 case DT_DEBUG
: name
= "DEBUG"; break;
1226 case DT_TEXTREL
: name
= "TEXTREL"; break;
1227 case DT_JMPREL
: name
= "JMPREL"; break;
1228 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1229 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1230 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1231 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1232 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1233 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= TRUE
; break;
1234 case DT_FLAGS
: name
= "FLAGS"; break;
1235 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1236 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1237 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1238 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1239 case DT_MOVEENT
: name
= "MOVEENT"; break;
1240 case DT_MOVESZ
: name
= "MOVESZ"; break;
1241 case DT_FEATURE
: name
= "FEATURE"; break;
1242 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1243 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1244 case DT_SYMINENT
: name
= "SYMINENT"; break;
1245 case DT_CONFIG
: name
= "CONFIG"; stringp
= TRUE
; break;
1246 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= TRUE
; break;
1247 case DT_AUDIT
: name
= "AUDIT"; stringp
= TRUE
; break;
1248 case DT_PLTPAD
: name
= "PLTPAD"; break;
1249 case DT_MOVETAB
: name
= "MOVETAB"; break;
1250 case DT_SYMINFO
: name
= "SYMINFO"; break;
1251 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1252 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1253 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1254 case DT_VERSYM
: name
= "VERSYM"; break;
1255 case DT_VERDEF
: name
= "VERDEF"; break;
1256 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1257 case DT_VERNEED
: name
= "VERNEED"; break;
1258 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1259 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= TRUE
; break;
1260 case DT_USED
: name
= "USED"; break;
1261 case DT_FILTER
: name
= "FILTER"; stringp
= TRUE
; break;
1262 case DT_GNU_HASH
: name
= "GNU_HASH"; break;
1265 fprintf (f
, " %-20s ", name
);
1269 bfd_fprintf_vma (abfd
, f
, dyn
.d_un
.d_val
);
1274 unsigned int tagv
= dyn
.d_un
.d_val
;
1276 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1279 fprintf (f
, "%s", string
);
1288 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1289 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1291 if (! _bfd_elf_slurp_version_tables (abfd
, FALSE
))
1295 if (elf_dynverdef (abfd
) != 0)
1297 Elf_Internal_Verdef
*t
;
1299 fprintf (f
, _("\nVersion definitions:\n"));
1300 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1302 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1303 t
->vd_flags
, t
->vd_hash
,
1304 t
->vd_nodename
? t
->vd_nodename
: "<corrupt>");
1305 if (t
->vd_auxptr
!= NULL
&& t
->vd_auxptr
->vda_nextptr
!= NULL
)
1307 Elf_Internal_Verdaux
*a
;
1310 for (a
= t
->vd_auxptr
->vda_nextptr
;
1314 a
->vda_nodename
? a
->vda_nodename
: "<corrupt>");
1320 if (elf_dynverref (abfd
) != 0)
1322 Elf_Internal_Verneed
*t
;
1324 fprintf (f
, _("\nVersion References:\n"));
1325 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1327 Elf_Internal_Vernaux
*a
;
1329 fprintf (f
, _(" required from %s:\n"),
1330 t
->vn_filename
? t
->vn_filename
: "<corrupt>");
1331 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1332 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1333 a
->vna_flags
, a
->vna_other
,
1334 a
->vna_nodename
? a
->vna_nodename
: "<corrupt>");
1346 /* Display ELF-specific fields of a symbol. */
1349 bfd_elf_print_symbol (bfd
*abfd
,
1352 bfd_print_symbol_type how
)
1357 case bfd_print_symbol_name
:
1358 fprintf (file
, "%s", symbol
->name
);
1360 case bfd_print_symbol_more
:
1361 fprintf (file
, "elf ");
1362 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1363 fprintf (file
, " %lx", (unsigned long) symbol
->flags
);
1365 case bfd_print_symbol_all
:
1367 const char *section_name
;
1368 const char *name
= NULL
;
1369 const struct elf_backend_data
*bed
;
1370 unsigned char st_other
;
1373 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1375 bed
= get_elf_backend_data (abfd
);
1376 if (bed
->elf_backend_print_symbol_all
)
1377 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1381 name
= symbol
->name
;
1382 bfd_print_symbol_vandf (abfd
, file
, symbol
);
1385 fprintf (file
, " %s\t", section_name
);
1386 /* Print the "other" value for a symbol. For common symbols,
1387 we've already printed the size; now print the alignment.
1388 For other symbols, we have no specified alignment, and
1389 we've printed the address; now print the size. */
1390 if (symbol
->section
&& bfd_is_com_section (symbol
->section
))
1391 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1393 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1394 bfd_fprintf_vma (abfd
, file
, val
);
1396 /* If we have version information, print it. */
1397 if (elf_tdata (abfd
)->dynversym_section
!= 0
1398 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1399 || elf_tdata (abfd
)->dynverref_section
!= 0))
1401 unsigned int vernum
;
1402 const char *version_string
;
1404 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1407 version_string
= "";
1408 else if (vernum
== 1)
1409 version_string
= "Base";
1410 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1412 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1415 Elf_Internal_Verneed
*t
;
1417 version_string
= "";
1418 for (t
= elf_tdata (abfd
)->verref
;
1422 Elf_Internal_Vernaux
*a
;
1424 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1426 if (a
->vna_other
== vernum
)
1428 version_string
= a
->vna_nodename
;
1435 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1436 fprintf (file
, " %-11s", version_string
);
1441 fprintf (file
, " (%s)", version_string
);
1442 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1447 /* If the st_other field is not zero, print it. */
1448 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1453 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1454 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1455 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1457 /* Some other non-defined flags are also present, so print
1459 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1462 fprintf (file
, " %s", name
);
1468 /* Allocate an ELF string table--force the first byte to be zero. */
1470 struct bfd_strtab_hash
*
1471 _bfd_elf_stringtab_init (void)
1473 struct bfd_strtab_hash
*ret
;
1475 ret
= _bfd_stringtab_init ();
1480 loc
= _bfd_stringtab_add (ret
, "", TRUE
, FALSE
);
1481 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1482 if (loc
== (bfd_size_type
) -1)
1484 _bfd_stringtab_free (ret
);
1491 /* ELF .o/exec file reading */
1493 /* Create a new bfd section from an ELF section header. */
1496 bfd_section_from_shdr (bfd
*abfd
, unsigned int shindex
)
1498 Elf_Internal_Shdr
*hdr
;
1499 Elf_Internal_Ehdr
*ehdr
;
1500 const struct elf_backend_data
*bed
;
1503 if (shindex
>= elf_numsections (abfd
))
1506 hdr
= elf_elfsections (abfd
)[shindex
];
1507 ehdr
= elf_elfheader (abfd
);
1508 name
= bfd_elf_string_from_elf_section (abfd
, ehdr
->e_shstrndx
,
1513 bed
= get_elf_backend_data (abfd
);
1514 switch (hdr
->sh_type
)
1517 /* Inactive section. Throw it away. */
1520 case SHT_PROGBITS
: /* Normal section with contents. */
1521 case SHT_NOBITS
: /* .bss section. */
1522 case SHT_HASH
: /* .hash section. */
1523 case SHT_NOTE
: /* .note section. */
1524 case SHT_INIT_ARRAY
: /* .init_array section. */
1525 case SHT_FINI_ARRAY
: /* .fini_array section. */
1526 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1527 case SHT_GNU_LIBLIST
: /* .gnu.liblist section. */
1528 case SHT_GNU_HASH
: /* .gnu.hash section. */
1529 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1531 case SHT_DYNAMIC
: /* Dynamic linking information. */
1532 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1534 if (hdr
->sh_link
> elf_numsections (abfd
)
1535 || elf_elfsections (abfd
)[hdr
->sh_link
] == NULL
)
1537 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1539 Elf_Internal_Shdr
*dynsymhdr
;
1541 /* The shared libraries distributed with hpux11 have a bogus
1542 sh_link field for the ".dynamic" section. Find the
1543 string table for the ".dynsym" section instead. */
1544 if (elf_dynsymtab (abfd
) != 0)
1546 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1547 hdr
->sh_link
= dynsymhdr
->sh_link
;
1551 unsigned int i
, num_sec
;
1553 num_sec
= elf_numsections (abfd
);
1554 for (i
= 1; i
< num_sec
; i
++)
1556 dynsymhdr
= elf_elfsections (abfd
)[i
];
1557 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1559 hdr
->sh_link
= dynsymhdr
->sh_link
;
1567 case SHT_SYMTAB
: /* A symbol table */
1568 if (elf_onesymtab (abfd
) == shindex
)
1571 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1573 if (hdr
->sh_info
* hdr
->sh_entsize
> hdr
->sh_size
)
1575 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1576 elf_onesymtab (abfd
) = shindex
;
1577 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1578 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1579 abfd
->flags
|= HAS_SYMS
;
1581 /* Sometimes a shared object will map in the symbol table. If
1582 SHF_ALLOC is set, and this is a shared object, then we also
1583 treat this section as a BFD section. We can not base the
1584 decision purely on SHF_ALLOC, because that flag is sometimes
1585 set in a relocatable object file, which would confuse the
1587 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1588 && (abfd
->flags
& DYNAMIC
) != 0
1589 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1593 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1594 can't read symbols without that section loaded as well. It
1595 is most likely specified by the next section header. */
1596 if (elf_elfsections (abfd
)[elf_symtab_shndx (abfd
)]->sh_link
!= shindex
)
1598 unsigned int i
, num_sec
;
1600 num_sec
= elf_numsections (abfd
);
1601 for (i
= shindex
+ 1; i
< num_sec
; i
++)
1603 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1604 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1605 && hdr2
->sh_link
== shindex
)
1609 for (i
= 1; i
< shindex
; i
++)
1611 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1612 if (hdr2
->sh_type
== SHT_SYMTAB_SHNDX
1613 && hdr2
->sh_link
== shindex
)
1617 return bfd_section_from_shdr (abfd
, i
);
1621 case SHT_DYNSYM
: /* A dynamic symbol table */
1622 if (elf_dynsymtab (abfd
) == shindex
)
1625 if (hdr
->sh_entsize
!= bed
->s
->sizeof_sym
)
1627 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1628 elf_dynsymtab (abfd
) = shindex
;
1629 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1630 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1631 abfd
->flags
|= HAS_SYMS
;
1633 /* Besides being a symbol table, we also treat this as a regular
1634 section, so that objcopy can handle it. */
1635 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1637 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1638 if (elf_symtab_shndx (abfd
) == shindex
)
1641 BFD_ASSERT (elf_symtab_shndx (abfd
) == 0);
1642 elf_symtab_shndx (abfd
) = shindex
;
1643 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1644 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1647 case SHT_STRTAB
: /* A string table */
1648 if (hdr
->bfd_section
!= NULL
)
1650 if (ehdr
->e_shstrndx
== shindex
)
1652 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1653 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1656 if (elf_elfsections (abfd
)[elf_onesymtab (abfd
)]->sh_link
== shindex
)
1659 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1660 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->strtab_hdr
;
1663 if (elf_elfsections (abfd
)[elf_dynsymtab (abfd
)]->sh_link
== shindex
)
1666 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1667 hdr
= &elf_tdata (abfd
)->dynstrtab_hdr
;
1668 elf_elfsections (abfd
)[shindex
] = hdr
;
1669 /* We also treat this as a regular section, so that objcopy
1671 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1675 /* If the string table isn't one of the above, then treat it as a
1676 regular section. We need to scan all the headers to be sure,
1677 just in case this strtab section appeared before the above. */
1678 if (elf_onesymtab (abfd
) == 0 || elf_dynsymtab (abfd
) == 0)
1680 unsigned int i
, num_sec
;
1682 num_sec
= elf_numsections (abfd
);
1683 for (i
= 1; i
< num_sec
; i
++)
1685 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1686 if (hdr2
->sh_link
== shindex
)
1688 /* Prevent endless recursion on broken objects. */
1691 if (! bfd_section_from_shdr (abfd
, i
))
1693 if (elf_onesymtab (abfd
) == i
)
1695 if (elf_dynsymtab (abfd
) == i
)
1696 goto dynsymtab_strtab
;
1700 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1704 /* *These* do a lot of work -- but build no sections! */
1706 asection
*target_sect
;
1707 Elf_Internal_Shdr
*hdr2
;
1708 unsigned int num_sec
= elf_numsections (abfd
);
1711 != (bfd_size_type
) (hdr
->sh_type
== SHT_REL
1712 ? bed
->s
->sizeof_rel
: bed
->s
->sizeof_rela
))
1715 /* Check for a bogus link to avoid crashing. */
1716 if (hdr
->sh_link
>= num_sec
)
1718 ((*_bfd_error_handler
)
1719 (_("%B: invalid link %lu for reloc section %s (index %u)"),
1720 abfd
, hdr
->sh_link
, name
, shindex
));
1721 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1725 /* For some incomprehensible reason Oracle distributes
1726 libraries for Solaris in which some of the objects have
1727 bogus sh_link fields. It would be nice if we could just
1728 reject them, but, unfortunately, some people need to use
1729 them. We scan through the section headers; if we find only
1730 one suitable symbol table, we clobber the sh_link to point
1731 to it. I hope this doesn't break anything. */
1732 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1733 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1739 for (scan
= 1; scan
< num_sec
; scan
++)
1741 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1742 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1753 hdr
->sh_link
= found
;
1756 /* Get the symbol table. */
1757 if ((elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1758 || elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_DYNSYM
)
1759 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1762 /* If this reloc section does not use the main symbol table we
1763 don't treat it as a reloc section. BFD can't adequately
1764 represent such a section, so at least for now, we don't
1765 try. We just present it as a normal section. We also
1766 can't use it as a reloc section if it points to the null
1767 section, an invalid section, or another reloc section. */
1768 if (hdr
->sh_link
!= elf_onesymtab (abfd
)
1769 || hdr
->sh_info
== SHN_UNDEF
1770 || hdr
->sh_info
>= num_sec
1771 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_REL
1772 || elf_elfsections (abfd
)[hdr
->sh_info
]->sh_type
== SHT_RELA
)
1773 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1776 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1778 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1779 if (target_sect
== NULL
)
1782 if ((target_sect
->flags
& SEC_RELOC
) == 0
1783 || target_sect
->reloc_count
== 0)
1784 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1788 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1789 amt
= sizeof (*hdr2
);
1790 hdr2
= bfd_alloc (abfd
, amt
);
1793 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1796 elf_elfsections (abfd
)[shindex
] = hdr2
;
1797 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1798 target_sect
->flags
|= SEC_RELOC
;
1799 target_sect
->relocation
= NULL
;
1800 target_sect
->rel_filepos
= hdr
->sh_offset
;
1801 /* In the section to which the relocations apply, mark whether
1802 its relocations are of the REL or RELA variety. */
1803 if (hdr
->sh_size
!= 0)
1804 target_sect
->use_rela_p
= hdr
->sh_type
== SHT_RELA
;
1805 abfd
->flags
|= HAS_RELOC
;
1809 case SHT_GNU_verdef
:
1810 elf_dynverdef (abfd
) = shindex
;
1811 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1812 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1814 case SHT_GNU_versym
:
1815 if (hdr
->sh_entsize
!= sizeof (Elf_External_Versym
))
1817 elf_dynversym (abfd
) = shindex
;
1818 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1819 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1821 case SHT_GNU_verneed
:
1822 elf_dynverref (abfd
) = shindex
;
1823 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1824 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1830 if (! IS_VALID_GROUP_SECTION_HEADER (hdr
))
1832 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1834 if (hdr
->contents
!= NULL
)
1836 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1837 unsigned int n_elt
= hdr
->sh_size
/ GRP_ENTRY_SIZE
;
1840 if (idx
->flags
& GRP_COMDAT
)
1841 hdr
->bfd_section
->flags
1842 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1844 /* We try to keep the same section order as it comes in. */
1846 while (--n_elt
!= 0)
1850 if (idx
->shdr
!= NULL
1851 && (s
= idx
->shdr
->bfd_section
) != NULL
1852 && elf_next_in_group (s
) != NULL
)
1854 elf_next_in_group (hdr
->bfd_section
) = s
;
1862 /* Possibly an attributes section. */
1863 if (hdr
->sh_type
== SHT_GNU_ATTRIBUTES
1864 || hdr
->sh_type
== bed
->obj_attrs_section_type
)
1866 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
))
1868 _bfd_elf_parse_attributes (abfd
, hdr
);
1872 /* Check for any processor-specific section types. */
1873 if (bed
->elf_backend_section_from_shdr (abfd
, hdr
, name
, shindex
))
1876 if (hdr
->sh_type
>= SHT_LOUSER
&& hdr
->sh_type
<= SHT_HIUSER
)
1878 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
1879 /* FIXME: How to properly handle allocated section reserved
1880 for applications? */
1881 (*_bfd_error_handler
)
1882 (_("%B: don't know how to handle allocated, application "
1883 "specific section `%s' [0x%8x]"),
1884 abfd
, name
, hdr
->sh_type
);
1886 /* Allow sections reserved for applications. */
1887 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
,
1890 else if (hdr
->sh_type
>= SHT_LOPROC
1891 && hdr
->sh_type
<= SHT_HIPROC
)
1892 /* FIXME: We should handle this section. */
1893 (*_bfd_error_handler
)
1894 (_("%B: don't know how to handle processor specific section "
1896 abfd
, name
, hdr
->sh_type
);
1897 else if (hdr
->sh_type
>= SHT_LOOS
&& hdr
->sh_type
<= SHT_HIOS
)
1899 /* Unrecognised OS-specific sections. */
1900 if ((hdr
->sh_flags
& SHF_OS_NONCONFORMING
) != 0)
1901 /* SHF_OS_NONCONFORMING indicates that special knowledge is
1902 required to correctly process the section and the file should
1903 be rejected with an error message. */
1904 (*_bfd_error_handler
)
1905 (_("%B: don't know how to handle OS specific section "
1907 abfd
, name
, hdr
->sh_type
);
1909 /* Otherwise it should be processed. */
1910 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
, shindex
);
1913 /* FIXME: We should handle this section. */
1914 (*_bfd_error_handler
)
1915 (_("%B: don't know how to handle section `%s' [0x%8x]"),
1916 abfd
, name
, hdr
->sh_type
);
1924 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1925 Return SEC for sections that have no elf section, and NULL on error. */
1928 bfd_section_from_r_symndx (bfd
*abfd
,
1929 struct sym_sec_cache
*cache
,
1931 unsigned long r_symndx
)
1933 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1936 if (cache
->abfd
!= abfd
|| cache
->indx
[ent
] != r_symndx
)
1938 Elf_Internal_Shdr
*symtab_hdr
;
1939 unsigned char esym
[sizeof (Elf64_External_Sym
)];
1940 Elf_External_Sym_Shndx eshndx
;
1941 Elf_Internal_Sym isym
;
1943 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1944 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
1945 &isym
, esym
, &eshndx
) == NULL
)
1948 if (cache
->abfd
!= abfd
)
1950 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1953 cache
->indx
[ent
] = r_symndx
;
1954 cache
->shndx
[ent
] = isym
.st_shndx
;
1957 s
= bfd_section_from_elf_index (abfd
, cache
->shndx
[ent
]);
1964 /* Given an ELF section number, retrieve the corresponding BFD
1968 bfd_section_from_elf_index (bfd
*abfd
, unsigned int index
)
1970 if (index
>= elf_numsections (abfd
))
1972 return elf_elfsections (abfd
)[index
]->bfd_section
;
1975 static const struct bfd_elf_special_section special_sections_b
[] =
1977 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
1978 { NULL
, 0, 0, 0, 0 }
1981 static const struct bfd_elf_special_section special_sections_c
[] =
1983 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS
, 0 },
1984 { NULL
, 0, 0, 0, 0 }
1987 static const struct bfd_elf_special_section special_sections_d
[] =
1989 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1990 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
1991 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS
, 0 },
1992 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS
, 0 },
1993 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS
, 0 },
1994 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS
, 0 },
1995 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS
, 0 },
1996 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC
, SHF_ALLOC
},
1997 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB
, SHF_ALLOC
},
1998 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM
, SHF_ALLOC
},
1999 { NULL
, 0, 0, 0, 0 }
2002 static const struct bfd_elf_special_section special_sections_f
[] =
2004 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2005 { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2006 { NULL
, 0, 0, 0, 0 }
2009 static const struct bfd_elf_special_section special_sections_g
[] =
2011 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
2012 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
2013 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym
, 0 },
2014 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef
, 0 },
2015 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed
, 0 },
2016 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST
, SHF_ALLOC
},
2017 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA
, SHF_ALLOC
},
2018 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH
, SHF_ALLOC
},
2019 { NULL
, 0, 0, 0, 0 }
2022 static const struct bfd_elf_special_section special_sections_h
[] =
2024 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH
, SHF_ALLOC
},
2025 { NULL
, 0, 0, 0, 0 }
2028 static const struct bfd_elf_special_section special_sections_i
[] =
2030 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2031 { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2032 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS
, 0 },
2033 { NULL
, 0, 0, 0, 0 }
2036 static const struct bfd_elf_special_section special_sections_l
[] =
2038 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS
, 0 },
2039 { NULL
, 0, 0, 0, 0 }
2042 static const struct bfd_elf_special_section special_sections_n
[] =
2044 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS
, 0 },
2045 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE
, 0 },
2046 { NULL
, 0, 0, 0, 0 }
2049 static const struct bfd_elf_special_section special_sections_p
[] =
2051 { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY
, SHF_ALLOC
+ SHF_WRITE
},
2052 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2053 { NULL
, 0, 0, 0, 0 }
2056 static const struct bfd_elf_special_section special_sections_r
[] =
2058 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS
, SHF_ALLOC
},
2059 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS
, SHF_ALLOC
},
2060 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA
, 0 },
2061 { STRING_COMMA_LEN (".rel"), -1, SHT_REL
, 0 },
2062 { NULL
, 0, 0, 0, 0 }
2065 static const struct bfd_elf_special_section special_sections_s
[] =
2067 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB
, 0 },
2068 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB
, 0 },
2069 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB
, 0 },
2070 /* See struct bfd_elf_special_section declaration for the semantics of
2071 this special case where .prefix_length != strlen (.prefix). */
2072 { ".stabstr", 5, 3, SHT_STRTAB
, 0 },
2073 { NULL
, 0, 0, 0, 0 }
2076 static const struct bfd_elf_special_section special_sections_t
[] =
2078 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_EXECINSTR
},
2079 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2080 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
+ SHF_TLS
},
2081 { NULL
, 0, 0, 0, 0 }
2084 static const struct bfd_elf_special_section special_sections_z
[] =
2086 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS
, 0 },
2087 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS
, 0 },
2088 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS
, 0 },
2089 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS
, 0 },
2090 { NULL
, 0, 0, 0, 0 }
2093 static const struct bfd_elf_special_section
*special_sections
[] =
2095 special_sections_b
, /* 'b' */
2096 special_sections_c
, /* 'c' */
2097 special_sections_d
, /* 'd' */
2099 special_sections_f
, /* 'f' */
2100 special_sections_g
, /* 'g' */
2101 special_sections_h
, /* 'h' */
2102 special_sections_i
, /* 'i' */
2105 special_sections_l
, /* 'l' */
2107 special_sections_n
, /* 'n' */
2109 special_sections_p
, /* 'p' */
2111 special_sections_r
, /* 'r' */
2112 special_sections_s
, /* 's' */
2113 special_sections_t
, /* 't' */
2119 special_sections_z
/* 'z' */
2122 const struct bfd_elf_special_section
*
2123 _bfd_elf_get_special_section (const char *name
,
2124 const struct bfd_elf_special_section
*spec
,
2130 len
= strlen (name
);
2132 for (i
= 0; spec
[i
].prefix
!= NULL
; i
++)
2135 int prefix_len
= spec
[i
].prefix_length
;
2137 if (len
< prefix_len
)
2139 if (memcmp (name
, spec
[i
].prefix
, prefix_len
) != 0)
2142 suffix_len
= spec
[i
].suffix_length
;
2143 if (suffix_len
<= 0)
2145 if (name
[prefix_len
] != 0)
2147 if (suffix_len
== 0)
2149 if (name
[prefix_len
] != '.'
2150 && (suffix_len
== -2
2151 || (rela
&& spec
[i
].type
== SHT_REL
)))
2157 if (len
< prefix_len
+ suffix_len
)
2159 if (memcmp (name
+ len
- suffix_len
,
2160 spec
[i
].prefix
+ prefix_len
,
2170 const struct bfd_elf_special_section
*
2171 _bfd_elf_get_sec_type_attr (bfd
*abfd
, asection
*sec
)
2174 const struct bfd_elf_special_section
*spec
;
2175 const struct elf_backend_data
*bed
;
2177 /* See if this is one of the special sections. */
2178 if (sec
->name
== NULL
)
2181 bed
= get_elf_backend_data (abfd
);
2182 spec
= bed
->special_sections
;
2185 spec
= _bfd_elf_get_special_section (sec
->name
,
2186 bed
->special_sections
,
2192 if (sec
->name
[0] != '.')
2195 i
= sec
->name
[1] - 'b';
2196 if (i
< 0 || i
> 'z' - 'b')
2199 spec
= special_sections
[i
];
2204 return _bfd_elf_get_special_section (sec
->name
, spec
, sec
->use_rela_p
);
2208 _bfd_elf_new_section_hook (bfd
*abfd
, asection
*sec
)
2210 struct bfd_elf_section_data
*sdata
;
2211 const struct elf_backend_data
*bed
;
2212 const struct bfd_elf_special_section
*ssect
;
2214 sdata
= (struct bfd_elf_section_data
*) sec
->used_by_bfd
;
2217 sdata
= bfd_zalloc (abfd
, sizeof (*sdata
));
2220 sec
->used_by_bfd
= sdata
;
2223 /* Indicate whether or not this section should use RELA relocations. */
2224 bed
= get_elf_backend_data (abfd
);
2225 sec
->use_rela_p
= bed
->default_use_rela_p
;
2227 /* When we read a file, we don't need to set ELF section type and
2228 flags. They will be overridden in _bfd_elf_make_section_from_shdr
2229 anyway. We will set ELF section type and flags for all linker
2230 created sections. If user specifies BFD section flags, we will
2231 set ELF section type and flags based on BFD section flags in
2232 elf_fake_sections. */
2233 if ((!sec
->flags
&& abfd
->direction
!= read_direction
)
2234 || (sec
->flags
& SEC_LINKER_CREATED
) != 0)
2236 ssect
= (*bed
->get_sec_type_attr
) (abfd
, sec
);
2239 elf_section_type (sec
) = ssect
->type
;
2240 elf_section_flags (sec
) = ssect
->attr
;
2244 return _bfd_generic_new_section_hook (abfd
, sec
);
2247 /* Create a new bfd section from an ELF program header.
2249 Since program segments have no names, we generate a synthetic name
2250 of the form segment<NUM>, where NUM is generally the index in the
2251 program header table. For segments that are split (see below) we
2252 generate the names segment<NUM>a and segment<NUM>b.
2254 Note that some program segments may have a file size that is different than
2255 (less than) the memory size. All this means is that at execution the
2256 system must allocate the amount of memory specified by the memory size,
2257 but only initialize it with the first "file size" bytes read from the
2258 file. This would occur for example, with program segments consisting
2259 of combined data+bss.
2261 To handle the above situation, this routine generates TWO bfd sections
2262 for the single program segment. The first has the length specified by
2263 the file size of the segment, and the second has the length specified
2264 by the difference between the two sizes. In effect, the segment is split
2265 into its initialized and uninitialized parts.
2270 _bfd_elf_make_section_from_phdr (bfd
*abfd
,
2271 Elf_Internal_Phdr
*hdr
,
2273 const char *typename
)
2281 split
= ((hdr
->p_memsz
> 0)
2282 && (hdr
->p_filesz
> 0)
2283 && (hdr
->p_memsz
> hdr
->p_filesz
));
2285 if (hdr
->p_filesz
> 0)
2287 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2288 len
= strlen (namebuf
) + 1;
2289 name
= bfd_alloc (abfd
, len
);
2292 memcpy (name
, namebuf
, len
);
2293 newsect
= bfd_make_section (abfd
, name
);
2294 if (newsect
== NULL
)
2296 newsect
->vma
= hdr
->p_vaddr
;
2297 newsect
->lma
= hdr
->p_paddr
;
2298 newsect
->size
= hdr
->p_filesz
;
2299 newsect
->filepos
= hdr
->p_offset
;
2300 newsect
->flags
|= SEC_HAS_CONTENTS
;
2301 newsect
->alignment_power
= bfd_log2 (hdr
->p_align
);
2302 if (hdr
->p_type
== PT_LOAD
)
2304 newsect
->flags
|= SEC_ALLOC
;
2305 newsect
->flags
|= SEC_LOAD
;
2306 if (hdr
->p_flags
& PF_X
)
2308 /* FIXME: all we known is that it has execute PERMISSION,
2310 newsect
->flags
|= SEC_CODE
;
2313 if (!(hdr
->p_flags
& PF_W
))
2315 newsect
->flags
|= SEC_READONLY
;
2319 if (hdr
->p_memsz
> hdr
->p_filesz
)
2323 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "b" : "");
2324 len
= strlen (namebuf
) + 1;
2325 name
= bfd_alloc (abfd
, len
);
2328 memcpy (name
, namebuf
, len
);
2329 newsect
= bfd_make_section (abfd
, name
);
2330 if (newsect
== NULL
)
2332 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2333 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2334 newsect
->size
= hdr
->p_memsz
- hdr
->p_filesz
;
2335 newsect
->filepos
= hdr
->p_offset
+ hdr
->p_filesz
;
2336 align
= newsect
->vma
& -newsect
->vma
;
2337 if (align
== 0 || align
> hdr
->p_align
)
2338 align
= hdr
->p_align
;
2339 newsect
->alignment_power
= bfd_log2 (align
);
2340 if (hdr
->p_type
== PT_LOAD
)
2342 /* Hack for gdb. Segments that have not been modified do
2343 not have their contents written to a core file, on the
2344 assumption that a debugger can find the contents in the
2345 executable. We flag this case by setting the fake
2346 section size to zero. Note that "real" bss sections will
2347 always have their contents dumped to the core file. */
2348 if (bfd_get_format (abfd
) == bfd_core
)
2350 newsect
->flags
|= SEC_ALLOC
;
2351 if (hdr
->p_flags
& PF_X
)
2352 newsect
->flags
|= SEC_CODE
;
2354 if (!(hdr
->p_flags
& PF_W
))
2355 newsect
->flags
|= SEC_READONLY
;
2362 bfd_section_from_phdr (bfd
*abfd
, Elf_Internal_Phdr
*hdr
, int index
)
2364 const struct elf_backend_data
*bed
;
2366 switch (hdr
->p_type
)
2369 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2372 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2375 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2378 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2381 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2383 if (! elf_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
2388 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2391 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2393 case PT_GNU_EH_FRAME
:
2394 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
,
2398 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "stack");
2401 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "relro");
2404 /* Check for any processor-specific program segment types. */
2405 bed
= get_elf_backend_data (abfd
);
2406 return bed
->elf_backend_section_from_phdr (abfd
, hdr
, index
, "proc");
2410 /* Initialize REL_HDR, the section-header for new section, containing
2411 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2412 relocations; otherwise, we use REL relocations. */
2415 _bfd_elf_init_reloc_shdr (bfd
*abfd
,
2416 Elf_Internal_Shdr
*rel_hdr
,
2418 bfd_boolean use_rela_p
)
2421 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2422 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2424 name
= bfd_alloc (abfd
, amt
);
2427 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2429 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2431 if (rel_hdr
->sh_name
== (unsigned int) -1)
2433 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2434 rel_hdr
->sh_entsize
= (use_rela_p
2435 ? bed
->s
->sizeof_rela
2436 : bed
->s
->sizeof_rel
);
2437 rel_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
2438 rel_hdr
->sh_flags
= 0;
2439 rel_hdr
->sh_addr
= 0;
2440 rel_hdr
->sh_size
= 0;
2441 rel_hdr
->sh_offset
= 0;
2446 /* Set up an ELF internal section header for a section. */
2449 elf_fake_sections (bfd
*abfd
, asection
*asect
, void *failedptrarg
)
2451 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2452 bfd_boolean
*failedptr
= failedptrarg
;
2453 Elf_Internal_Shdr
*this_hdr
;
2454 unsigned int sh_type
;
2458 /* We already failed; just get out of the bfd_map_over_sections
2463 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2465 this_hdr
->sh_name
= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2466 asect
->name
, FALSE
);
2467 if (this_hdr
->sh_name
== (unsigned int) -1)
2473 /* Don't clear sh_flags. Assembler may set additional bits. */
2475 if ((asect
->flags
& SEC_ALLOC
) != 0
2476 || asect
->user_set_vma
)
2477 this_hdr
->sh_addr
= asect
->vma
;
2479 this_hdr
->sh_addr
= 0;
2481 this_hdr
->sh_offset
= 0;
2482 this_hdr
->sh_size
= asect
->size
;
2483 this_hdr
->sh_link
= 0;
2484 this_hdr
->sh_addralign
= (bfd_vma
) 1 << asect
->alignment_power
;
2485 /* The sh_entsize and sh_info fields may have been set already by
2486 copy_private_section_data. */
2488 this_hdr
->bfd_section
= asect
;
2489 this_hdr
->contents
= NULL
;
2491 /* If the section type is unspecified, we set it based on
2493 if ((asect
->flags
& SEC_GROUP
) != 0)
2494 sh_type
= SHT_GROUP
;
2495 else if ((asect
->flags
& SEC_ALLOC
) != 0
2496 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2497 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2498 sh_type
= SHT_NOBITS
;
2500 sh_type
= SHT_PROGBITS
;
2502 if (this_hdr
->sh_type
== SHT_NULL
)
2503 this_hdr
->sh_type
= sh_type
;
2504 else if (this_hdr
->sh_type
== SHT_NOBITS
2505 && sh_type
== SHT_PROGBITS
2506 && (asect
->flags
& SEC_ALLOC
) != 0)
2508 /* Warn if we are changing a NOBITS section to PROGBITS, but
2509 allow the link to proceed. This can happen when users link
2510 non-bss input sections to bss output sections, or emit data
2511 to a bss output section via a linker script. */
2512 (*_bfd_error_handler
)
2513 (_("warning: section `%A' type changed to PROGBITS"), asect
);
2514 this_hdr
->sh_type
= sh_type
;
2517 switch (this_hdr
->sh_type
)
2523 case SHT_INIT_ARRAY
:
2524 case SHT_FINI_ARRAY
:
2525 case SHT_PREINIT_ARRAY
:
2532 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2536 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2540 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2544 if (get_elf_backend_data (abfd
)->may_use_rela_p
)
2545 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2549 if (get_elf_backend_data (abfd
)->may_use_rel_p
)
2550 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2553 case SHT_GNU_versym
:
2554 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2557 case SHT_GNU_verdef
:
2558 this_hdr
->sh_entsize
= 0;
2559 /* objcopy or strip will copy over sh_info, but may not set
2560 cverdefs. The linker will set cverdefs, but sh_info will be
2562 if (this_hdr
->sh_info
== 0)
2563 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2565 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2566 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2569 case SHT_GNU_verneed
:
2570 this_hdr
->sh_entsize
= 0;
2571 /* objcopy or strip will copy over sh_info, but may not set
2572 cverrefs. The linker will set cverrefs, but sh_info will be
2574 if (this_hdr
->sh_info
== 0)
2575 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2577 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2578 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2582 this_hdr
->sh_entsize
= GRP_ENTRY_SIZE
;
2586 this_hdr
->sh_entsize
= bed
->s
->arch_size
== 64 ? 0 : 4;
2590 if ((asect
->flags
& SEC_ALLOC
) != 0)
2591 this_hdr
->sh_flags
|= SHF_ALLOC
;
2592 if ((asect
->flags
& SEC_READONLY
) == 0)
2593 this_hdr
->sh_flags
|= SHF_WRITE
;
2594 if ((asect
->flags
& SEC_CODE
) != 0)
2595 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2596 if ((asect
->flags
& SEC_MERGE
) != 0)
2598 this_hdr
->sh_flags
|= SHF_MERGE
;
2599 this_hdr
->sh_entsize
= asect
->entsize
;
2600 if ((asect
->flags
& SEC_STRINGS
) != 0)
2601 this_hdr
->sh_flags
|= SHF_STRINGS
;
2603 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2604 this_hdr
->sh_flags
|= SHF_GROUP
;
2605 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2607 this_hdr
->sh_flags
|= SHF_TLS
;
2608 if (asect
->size
== 0
2609 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2611 struct bfd_link_order
*o
= asect
->map_tail
.link_order
;
2613 this_hdr
->sh_size
= 0;
2616 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2617 if (this_hdr
->sh_size
!= 0)
2618 this_hdr
->sh_type
= SHT_NOBITS
;
2623 /* Check for processor-specific section types. */
2624 sh_type
= this_hdr
->sh_type
;
2625 if (bed
->elf_backend_fake_sections
2626 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2629 if (sh_type
== SHT_NOBITS
&& asect
->size
!= 0)
2631 /* Don't change the header type from NOBITS if we are being
2632 called for objcopy --only-keep-debug. */
2633 this_hdr
->sh_type
= sh_type
;
2636 /* If the section has relocs, set up a section header for the
2637 SHT_REL[A] section. If two relocation sections are required for
2638 this section, it is up to the processor-specific back-end to
2639 create the other. */
2640 if ((asect
->flags
& SEC_RELOC
) != 0
2641 && !_bfd_elf_init_reloc_shdr (abfd
,
2642 &elf_section_data (asect
)->rel_hdr
,
2648 /* Fill in the contents of a SHT_GROUP section. Called from
2649 _bfd_elf_compute_section_file_positions for gas, objcopy, and
2650 when ELF targets use the generic linker, ld. Called for ld -r
2651 from bfd_elf_final_link. */
2654 bfd_elf_set_group_contents (bfd
*abfd
, asection
*sec
, void *failedptrarg
)
2656 bfd_boolean
*failedptr
= failedptrarg
;
2657 asection
*elt
, *first
;
2661 /* Ignore linker created group section. See elfNN_ia64_object_p in
2663 if (((sec
->flags
& (SEC_GROUP
| SEC_LINKER_CREATED
)) != SEC_GROUP
)
2667 if (elf_section_data (sec
)->this_hdr
.sh_info
== 0)
2669 unsigned long symindx
= 0;
2671 /* elf_group_id will have been set up by objcopy and the
2673 if (elf_group_id (sec
) != NULL
)
2674 symindx
= elf_group_id (sec
)->udata
.i
;
2678 /* If called from the assembler, swap_out_syms will have set up
2679 elf_section_syms. */
2680 BFD_ASSERT (elf_section_syms (abfd
) != NULL
);
2681 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2683 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2685 else if (elf_section_data (sec
)->this_hdr
.sh_info
== (unsigned int) -2)
2687 /* The ELF backend linker sets sh_info to -2 when the group
2688 signature symbol is global, and thus the index can't be
2689 set until all local symbols are output. */
2690 asection
*igroup
= elf_sec_group (elf_next_in_group (sec
));
2691 struct bfd_elf_section_data
*sec_data
= elf_section_data (igroup
);
2692 unsigned long symndx
= sec_data
->this_hdr
.sh_info
;
2693 unsigned long extsymoff
= 0;
2694 struct elf_link_hash_entry
*h
;
2696 if (!elf_bad_symtab (igroup
->owner
))
2698 Elf_Internal_Shdr
*symtab_hdr
;
2700 symtab_hdr
= &elf_tdata (igroup
->owner
)->symtab_hdr
;
2701 extsymoff
= symtab_hdr
->sh_info
;
2703 h
= elf_sym_hashes (igroup
->owner
)[symndx
- extsymoff
];
2704 while (h
->root
.type
== bfd_link_hash_indirect
2705 || h
->root
.type
== bfd_link_hash_warning
)
2706 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2708 elf_section_data (sec
)->this_hdr
.sh_info
= h
->indx
;
2711 /* The contents won't be allocated for "ld -r" or objcopy. */
2713 if (sec
->contents
== NULL
)
2716 sec
->contents
= bfd_alloc (abfd
, sec
->size
);
2718 /* Arrange for the section to be written out. */
2719 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2720 if (sec
->contents
== NULL
)
2727 loc
= sec
->contents
+ sec
->size
;
2729 /* Get the pointer to the first section in the group that gas
2730 squirreled away here. objcopy arranges for this to be set to the
2731 start of the input section group. */
2732 first
= elt
= elf_next_in_group (sec
);
2734 /* First element is a flag word. Rest of section is elf section
2735 indices for all the sections of the group. Write them backwards
2736 just to keep the group in the same order as given in .section
2737 directives, not that it matters. */
2744 if (! elf_discarded_section (s
))
2748 s
= s
->output_section
;
2751 idx
= elf_section_data (s
)->this_idx
;
2752 H_PUT_32 (abfd
, idx
, loc
);
2754 elt
= elf_next_in_group (elt
);
2759 if ((loc
-= 4) != sec
->contents
)
2762 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2765 /* Assign all ELF section numbers. The dummy first section is handled here
2766 too. The link/info pointers for the standard section types are filled
2767 in here too, while we're at it. */
2770 assign_section_numbers (bfd
*abfd
, struct bfd_link_info
*link_info
)
2772 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2774 unsigned int section_number
, secn
;
2775 Elf_Internal_Shdr
**i_shdrp
;
2776 struct bfd_elf_section_data
*d
;
2780 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2782 /* SHT_GROUP sections are in relocatable files only. */
2783 if (link_info
== NULL
|| link_info
->relocatable
)
2785 /* Put SHT_GROUP sections first. */
2786 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2788 d
= elf_section_data (sec
);
2790 if (d
->this_hdr
.sh_type
== SHT_GROUP
)
2792 if (sec
->flags
& SEC_LINKER_CREATED
)
2794 /* Remove the linker created SHT_GROUP sections. */
2795 bfd_section_list_remove (abfd
, sec
);
2796 abfd
->section_count
--;
2799 d
->this_idx
= section_number
++;
2804 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2806 d
= elf_section_data (sec
);
2808 if (d
->this_hdr
.sh_type
!= SHT_GROUP
)
2809 d
->this_idx
= section_number
++;
2810 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2811 if ((sec
->flags
& SEC_RELOC
) == 0)
2815 d
->rel_idx
= section_number
++;
2816 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2821 d
->rel_idx2
= section_number
++;
2822 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2828 t
->shstrtab_section
= section_number
++;
2829 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2830 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2832 if (bfd_get_symcount (abfd
) > 0)
2834 t
->symtab_section
= section_number
++;
2835 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2836 if (section_number
> ((SHN_LORESERVE
- 2) & 0xFFFF))
2838 t
->symtab_shndx_section
= section_number
++;
2839 t
->symtab_shndx_hdr
.sh_name
2840 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2841 ".symtab_shndx", FALSE
);
2842 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2845 t
->strtab_section
= section_number
++;
2846 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2849 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2850 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2852 elf_numsections (abfd
) = section_number
;
2853 elf_elfheader (abfd
)->e_shnum
= section_number
;
2855 /* Set up the list of section header pointers, in agreement with the
2857 i_shdrp
= bfd_zalloc2 (abfd
, section_number
, sizeof (Elf_Internal_Shdr
*));
2858 if (i_shdrp
== NULL
)
2861 i_shdrp
[0] = bfd_zalloc (abfd
, sizeof (Elf_Internal_Shdr
));
2862 if (i_shdrp
[0] == NULL
)
2864 bfd_release (abfd
, i_shdrp
);
2868 elf_elfsections (abfd
) = i_shdrp
;
2870 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2871 if (bfd_get_symcount (abfd
) > 0)
2873 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2874 if (elf_numsections (abfd
) > (SHN_LORESERVE
& 0xFFFF))
2876 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2877 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2879 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2880 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2883 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2885 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2889 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2890 if (d
->rel_idx
!= 0)
2891 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2892 if (d
->rel_idx2
!= 0)
2893 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2895 /* Fill in the sh_link and sh_info fields while we're at it. */
2897 /* sh_link of a reloc section is the section index of the symbol
2898 table. sh_info is the section index of the section to which
2899 the relocation entries apply. */
2900 if (d
->rel_idx
!= 0)
2902 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2903 d
->rel_hdr
.sh_info
= d
->this_idx
;
2905 if (d
->rel_idx2
!= 0)
2907 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2908 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2911 /* We need to set up sh_link for SHF_LINK_ORDER. */
2912 if ((d
->this_hdr
.sh_flags
& SHF_LINK_ORDER
) != 0)
2914 s
= elf_linked_to_section (sec
);
2917 /* elf_linked_to_section points to the input section. */
2918 if (link_info
!= NULL
)
2920 /* Check discarded linkonce section. */
2921 if (elf_discarded_section (s
))
2924 (*_bfd_error_handler
)
2925 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
2926 abfd
, d
->this_hdr
.bfd_section
,
2928 /* Point to the kept section if it has the same
2929 size as the discarded one. */
2930 kept
= _bfd_elf_check_kept_section (s
, link_info
);
2933 bfd_set_error (bfd_error_bad_value
);
2939 s
= s
->output_section
;
2940 BFD_ASSERT (s
!= NULL
);
2944 /* Handle objcopy. */
2945 if (s
->output_section
== NULL
)
2947 (*_bfd_error_handler
)
2948 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
2949 abfd
, d
->this_hdr
.bfd_section
, s
, s
->owner
);
2950 bfd_set_error (bfd_error_bad_value
);
2953 s
= s
->output_section
;
2955 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2960 The Intel C compiler generates SHT_IA_64_UNWIND with
2961 SHF_LINK_ORDER. But it doesn't set the sh_link or
2962 sh_info fields. Hence we could get the situation
2964 const struct elf_backend_data
*bed
2965 = get_elf_backend_data (abfd
);
2966 if (bed
->link_order_error_handler
)
2967 bed
->link_order_error_handler
2968 (_("%B: warning: sh_link not set for section `%A'"),
2973 switch (d
->this_hdr
.sh_type
)
2977 /* A reloc section which we are treating as a normal BFD
2978 section. sh_link is the section index of the symbol
2979 table. sh_info is the section index of the section to
2980 which the relocation entries apply. We assume that an
2981 allocated reloc section uses the dynamic symbol table.
2982 FIXME: How can we be sure? */
2983 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2985 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2987 /* We look up the section the relocs apply to by name. */
2989 if (d
->this_hdr
.sh_type
== SHT_REL
)
2993 s
= bfd_get_section_by_name (abfd
, name
);
2995 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2999 /* We assume that a section named .stab*str is a stabs
3000 string section. We look for a section with the same name
3001 but without the trailing ``str'', and set its sh_link
3002 field to point to this section. */
3003 if (CONST_STRNEQ (sec
->name
, ".stab")
3004 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
3009 len
= strlen (sec
->name
);
3010 alc
= bfd_malloc (len
- 2);
3013 memcpy (alc
, sec
->name
, len
- 3);
3014 alc
[len
- 3] = '\0';
3015 s
= bfd_get_section_by_name (abfd
, alc
);
3019 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
3021 /* This is a .stab section. */
3022 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
3023 elf_section_data (s
)->this_hdr
.sh_entsize
3024 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
3031 case SHT_GNU_verneed
:
3032 case SHT_GNU_verdef
:
3033 /* sh_link is the section header index of the string table
3034 used for the dynamic entries, or the symbol table, or the
3036 s
= bfd_get_section_by_name (abfd
, ".dynstr");
3038 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3041 case SHT_GNU_LIBLIST
:
3042 /* sh_link is the section header index of the prelink library
3043 list used for the dynamic entries, or the symbol table, or
3044 the version strings. */
3045 s
= bfd_get_section_by_name (abfd
, (sec
->flags
& SEC_ALLOC
)
3046 ? ".dynstr" : ".gnu.libstr");
3048 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3053 case SHT_GNU_versym
:
3054 /* sh_link is the section header index of the symbol table
3055 this hash table or version table is for. */
3056 s
= bfd_get_section_by_name (abfd
, ".dynsym");
3058 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
3062 d
->this_hdr
.sh_link
= t
->symtab_section
;
3066 for (secn
= 1; secn
< section_number
; ++secn
)
3067 if (i_shdrp
[secn
] == NULL
)
3068 i_shdrp
[secn
] = i_shdrp
[0];
3070 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
3071 i_shdrp
[secn
]->sh_name
);
3075 /* Map symbol from it's internal number to the external number, moving
3076 all local symbols to be at the head of the list. */
3079 sym_is_global (bfd
*abfd
, asymbol
*sym
)
3081 /* If the backend has a special mapping, use it. */
3082 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3083 if (bed
->elf_backend_sym_is_global
)
3084 return (*bed
->elf_backend_sym_is_global
) (abfd
, sym
);
3086 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
3087 || bfd_is_und_section (bfd_get_section (sym
))
3088 || bfd_is_com_section (bfd_get_section (sym
)));
3091 /* Don't output section symbols for sections that are not going to be
3095 ignore_section_sym (bfd
*abfd
, asymbol
*sym
)
3097 return ((sym
->flags
& BSF_SECTION_SYM
) != 0
3098 && !(sym
->section
->owner
== abfd
3099 || (sym
->section
->output_section
->owner
== abfd
3100 && sym
->section
->output_offset
== 0)));
3104 elf_map_symbols (bfd
*abfd
)
3106 unsigned int symcount
= bfd_get_symcount (abfd
);
3107 asymbol
**syms
= bfd_get_outsymbols (abfd
);
3108 asymbol
**sect_syms
;
3109 unsigned int num_locals
= 0;
3110 unsigned int num_globals
= 0;
3111 unsigned int num_locals2
= 0;
3112 unsigned int num_globals2
= 0;
3119 fprintf (stderr
, "elf_map_symbols\n");
3123 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3125 if (max_index
< asect
->index
)
3126 max_index
= asect
->index
;
3130 sect_syms
= bfd_zalloc2 (abfd
, max_index
, sizeof (asymbol
*));
3131 if (sect_syms
== NULL
)
3133 elf_section_syms (abfd
) = sect_syms
;
3134 elf_num_section_syms (abfd
) = max_index
;
3136 /* Init sect_syms entries for any section symbols we have already
3137 decided to output. */
3138 for (idx
= 0; idx
< symcount
; idx
++)
3140 asymbol
*sym
= syms
[idx
];
3142 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
3144 && !ignore_section_sym (abfd
, sym
))
3146 asection
*sec
= sym
->section
;
3148 if (sec
->owner
!= abfd
)
3149 sec
= sec
->output_section
;
3151 sect_syms
[sec
->index
] = syms
[idx
];
3155 /* Classify all of the symbols. */
3156 for (idx
= 0; idx
< symcount
; idx
++)
3158 if (ignore_section_sym (abfd
, syms
[idx
]))
3160 if (!sym_is_global (abfd
, syms
[idx
]))
3166 /* We will be adding a section symbol for each normal BFD section. Most
3167 sections will already have a section symbol in outsymbols, but
3168 eg. SHT_GROUP sections will not, and we need the section symbol mapped
3169 at least in that case. */
3170 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3172 if (sect_syms
[asect
->index
] == NULL
)
3174 if (!sym_is_global (abfd
, asect
->symbol
))
3181 /* Now sort the symbols so the local symbols are first. */
3182 new_syms
= bfd_alloc2 (abfd
, num_locals
+ num_globals
, sizeof (asymbol
*));
3184 if (new_syms
== NULL
)
3187 for (idx
= 0; idx
< symcount
; idx
++)
3189 asymbol
*sym
= syms
[idx
];
3192 if (ignore_section_sym (abfd
, sym
))
3194 if (!sym_is_global (abfd
, sym
))
3197 i
= num_locals
+ num_globals2
++;
3199 sym
->udata
.i
= i
+ 1;
3201 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
3203 if (sect_syms
[asect
->index
] == NULL
)
3205 asymbol
*sym
= asect
->symbol
;
3208 sect_syms
[asect
->index
] = sym
;
3209 if (!sym_is_global (abfd
, sym
))
3212 i
= num_locals
+ num_globals2
++;
3214 sym
->udata
.i
= i
+ 1;
3218 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
3220 elf_num_locals (abfd
) = num_locals
;
3221 elf_num_globals (abfd
) = num_globals
;
3225 /* Align to the maximum file alignment that could be required for any
3226 ELF data structure. */
3228 static inline file_ptr
3229 align_file_position (file_ptr off
, int align
)
3231 return (off
+ align
- 1) & ~(align
- 1);
3234 /* Assign a file position to a section, optionally aligning to the
3235 required section alignment. */
3238 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr
*i_shdrp
,
3242 if (align
&& i_shdrp
->sh_addralign
> 1)
3243 offset
= BFD_ALIGN (offset
, i_shdrp
->sh_addralign
);
3244 i_shdrp
->sh_offset
= offset
;
3245 if (i_shdrp
->bfd_section
!= NULL
)
3246 i_shdrp
->bfd_section
->filepos
= offset
;
3247 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3248 offset
+= i_shdrp
->sh_size
;
3252 /* Compute the file positions we are going to put the sections at, and
3253 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3254 is not NULL, this is being called by the ELF backend linker. */
3257 _bfd_elf_compute_section_file_positions (bfd
*abfd
,
3258 struct bfd_link_info
*link_info
)
3260 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3262 struct bfd_strtab_hash
*strtab
= NULL
;
3263 Elf_Internal_Shdr
*shstrtab_hdr
;
3265 if (abfd
->output_has_begun
)
3268 /* Do any elf backend specific processing first. */
3269 if (bed
->elf_backend_begin_write_processing
)
3270 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3272 if (! prep_headers (abfd
))
3275 /* Post process the headers if necessary. */
3276 if (bed
->elf_backend_post_process_headers
)
3277 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3280 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3284 if (!assign_section_numbers (abfd
, link_info
))
3287 /* The backend linker builds symbol table information itself. */
3288 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3290 /* Non-zero if doing a relocatable link. */
3291 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3293 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3297 if (link_info
== NULL
)
3299 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3304 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3305 /* sh_name was set in prep_headers. */
3306 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3307 shstrtab_hdr
->sh_flags
= 0;
3308 shstrtab_hdr
->sh_addr
= 0;
3309 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3310 shstrtab_hdr
->sh_entsize
= 0;
3311 shstrtab_hdr
->sh_link
= 0;
3312 shstrtab_hdr
->sh_info
= 0;
3313 /* sh_offset is set in assign_file_positions_except_relocs. */
3314 shstrtab_hdr
->sh_addralign
= 1;
3316 if (!assign_file_positions_except_relocs (abfd
, link_info
))
3319 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3322 Elf_Internal_Shdr
*hdr
;
3324 off
= elf_tdata (abfd
)->next_file_pos
;
3326 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3327 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3329 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3330 if (hdr
->sh_size
!= 0)
3331 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3333 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3334 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
3336 elf_tdata (abfd
)->next_file_pos
= off
;
3338 /* Now that we know where the .strtab section goes, write it
3340 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3341 || ! _bfd_stringtab_emit (abfd
, strtab
))
3343 _bfd_stringtab_free (strtab
);
3346 abfd
->output_has_begun
= TRUE
;
3351 /* Make an initial estimate of the size of the program header. If we
3352 get the number wrong here, we'll redo section placement. */
3354 static bfd_size_type
3355 get_program_header_size (bfd
*abfd
, struct bfd_link_info
*info
)
3359 const struct elf_backend_data
*bed
;
3361 /* Assume we will need exactly two PT_LOAD segments: one for text
3362 and one for data. */
3365 s
= bfd_get_section_by_name (abfd
, ".interp");
3366 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3368 /* If we have a loadable interpreter section, we need a
3369 PT_INTERP segment. In this case, assume we also need a
3370 PT_PHDR segment, although that may not be true for all
3375 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3377 /* We need a PT_DYNAMIC segment. */
3381 if (info
!= NULL
&& info
->relro
)
3383 /* We need a PT_GNU_RELRO segment. */
3387 if (elf_tdata (abfd
)->eh_frame_hdr
)
3389 /* We need a PT_GNU_EH_FRAME segment. */
3393 if (elf_tdata (abfd
)->stack_flags
)
3395 /* We need a PT_GNU_STACK segment. */
3399 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3401 if ((s
->flags
& SEC_LOAD
) != 0
3402 && CONST_STRNEQ (s
->name
, ".note"))
3404 /* We need a PT_NOTE segment. */
3406 /* Try to create just one PT_NOTE segment
3407 for all adjacent loadable .note* sections.
3408 gABI requires that within a PT_NOTE segment
3409 (and also inside of each SHT_NOTE section)
3410 each note is padded to a multiple of 4 size,
3411 so we check whether the sections are correctly
3413 if (s
->alignment_power
== 2)
3414 while (s
->next
!= NULL
3415 && s
->next
->alignment_power
== 2
3416 && (s
->next
->flags
& SEC_LOAD
) != 0
3417 && CONST_STRNEQ (s
->next
->name
, ".note"))
3422 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3424 if (s
->flags
& SEC_THREAD_LOCAL
)
3426 /* We need a PT_TLS segment. */
3432 /* Let the backend count up any program headers it might need. */
3433 bed
= get_elf_backend_data (abfd
);
3434 if (bed
->elf_backend_additional_program_headers
)
3438 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
, info
);
3444 return segs
* bed
->s
->sizeof_phdr
;
3447 /* Find the segment that contains the output_section of section. */
3450 _bfd_elf_find_segment_containing_section (bfd
* abfd
, asection
* section
)
3452 struct elf_segment_map
*m
;
3453 Elf_Internal_Phdr
*p
;
3455 for (m
= elf_tdata (abfd
)->segment_map
,
3456 p
= elf_tdata (abfd
)->phdr
;
3462 for (i
= m
->count
- 1; i
>= 0; i
--)
3463 if (m
->sections
[i
] == section
)
3470 /* Create a mapping from a set of sections to a program segment. */
3472 static struct elf_segment_map
*
3473 make_mapping (bfd
*abfd
,
3474 asection
**sections
,
3479 struct elf_segment_map
*m
;
3484 amt
= sizeof (struct elf_segment_map
);
3485 amt
+= (to
- from
- 1) * sizeof (asection
*);
3486 m
= bfd_zalloc (abfd
, amt
);
3490 m
->p_type
= PT_LOAD
;
3491 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3492 m
->sections
[i
- from
] = *hdrpp
;
3493 m
->count
= to
- from
;
3495 if (from
== 0 && phdr
)
3497 /* Include the headers in the first PT_LOAD segment. */
3498 m
->includes_filehdr
= 1;
3499 m
->includes_phdrs
= 1;
3505 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL
3508 struct elf_segment_map
*
3509 _bfd_elf_make_dynamic_segment (bfd
*abfd
, asection
*dynsec
)
3511 struct elf_segment_map
*m
;
3513 m
= bfd_zalloc (abfd
, sizeof (struct elf_segment_map
));
3517 m
->p_type
= PT_DYNAMIC
;
3519 m
->sections
[0] = dynsec
;
3524 /* Possibly add or remove segments from the segment map. */
3527 elf_modify_segment_map (bfd
*abfd
,
3528 struct bfd_link_info
*info
,
3529 bfd_boolean remove_empty_load
)
3531 struct elf_segment_map
**m
;
3532 const struct elf_backend_data
*bed
;
3534 /* The placement algorithm assumes that non allocated sections are
3535 not in PT_LOAD segments. We ensure this here by removing such
3536 sections from the segment map. We also remove excluded
3537 sections. Finally, any PT_LOAD segment without sections is
3539 m
= &elf_tdata (abfd
)->segment_map
;
3542 unsigned int i
, new_count
;
3544 for (new_count
= 0, i
= 0; i
< (*m
)->count
; i
++)
3546 if (((*m
)->sections
[i
]->flags
& SEC_EXCLUDE
) == 0
3547 && (((*m
)->sections
[i
]->flags
& SEC_ALLOC
) != 0
3548 || (*m
)->p_type
!= PT_LOAD
))
3550 (*m
)->sections
[new_count
] = (*m
)->sections
[i
];
3554 (*m
)->count
= new_count
;
3556 if (remove_empty_load
&& (*m
)->p_type
== PT_LOAD
&& (*m
)->count
== 0)
3562 bed
= get_elf_backend_data (abfd
);
3563 if (bed
->elf_backend_modify_segment_map
!= NULL
)
3565 if (!(*bed
->elf_backend_modify_segment_map
) (abfd
, info
))
3572 /* Set up a mapping from BFD sections to program segments. */
3575 _bfd_elf_map_sections_to_segments (bfd
*abfd
, struct bfd_link_info
*info
)
3578 struct elf_segment_map
*m
;
3579 asection
**sections
= NULL
;
3580 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3581 bfd_boolean no_user_phdrs
;
3583 no_user_phdrs
= elf_tdata (abfd
)->segment_map
== NULL
;
3584 if (no_user_phdrs
&& bfd_count_sections (abfd
) != 0)
3588 struct elf_segment_map
*mfirst
;
3589 struct elf_segment_map
**pm
;
3592 unsigned int phdr_index
;
3593 bfd_vma maxpagesize
;
3595 bfd_boolean phdr_in_segment
= TRUE
;
3596 bfd_boolean writable
;
3598 asection
*first_tls
= NULL
;
3599 asection
*dynsec
, *eh_frame_hdr
;
3602 /* Select the allocated sections, and sort them. */
3604 sections
= bfd_malloc2 (bfd_count_sections (abfd
), sizeof (asection
*));
3605 if (sections
== NULL
)
3609 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3611 if ((s
->flags
& SEC_ALLOC
) != 0)
3617 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3620 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3622 /* Build the mapping. */
3627 /* If we have a .interp section, then create a PT_PHDR segment for
3628 the program headers and a PT_INTERP segment for the .interp
3630 s
= bfd_get_section_by_name (abfd
, ".interp");
3631 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3633 amt
= sizeof (struct elf_segment_map
);
3634 m
= bfd_zalloc (abfd
, amt
);
3638 m
->p_type
= PT_PHDR
;
3639 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3640 m
->p_flags
= PF_R
| PF_X
;
3641 m
->p_flags_valid
= 1;
3642 m
->includes_phdrs
= 1;
3647 amt
= sizeof (struct elf_segment_map
);
3648 m
= bfd_zalloc (abfd
, amt
);
3652 m
->p_type
= PT_INTERP
;
3660 /* Look through the sections. We put sections in the same program
3661 segment when the start of the second section can be placed within
3662 a few bytes of the end of the first section. */
3666 maxpagesize
= bed
->maxpagesize
;
3668 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3670 && (dynsec
->flags
& SEC_LOAD
) == 0)
3673 /* Deal with -Ttext or something similar such that the first section
3674 is not adjacent to the program headers. This is an
3675 approximation, since at this point we don't know exactly how many
3676 program headers we will need. */
3679 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
3681 if (phdr_size
== (bfd_size_type
) -1)
3682 phdr_size
= get_program_header_size (abfd
, info
);
3683 if ((abfd
->flags
& D_PAGED
) == 0
3684 || sections
[0]->lma
< phdr_size
3685 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3686 phdr_in_segment
= FALSE
;
3689 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3692 bfd_boolean new_segment
;
3696 /* See if this section and the last one will fit in the same
3699 if (last_hdr
== NULL
)
3701 /* If we don't have a segment yet, then we don't need a new
3702 one (we build the last one after this loop). */
3703 new_segment
= FALSE
;
3705 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3707 /* If this section has a different relation between the
3708 virtual address and the load address, then we need a new
3712 /* In the next test we have to be careful when last_hdr->lma is close
3713 to the end of the address space. If the aligned address wraps
3714 around to the start of the address space, then there are no more
3715 pages left in memory and it is OK to assume that the current
3716 section can be included in the current segment. */
3717 else if ((BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3719 && (BFD_ALIGN (last_hdr
->lma
+ last_size
, maxpagesize
) + maxpagesize
3722 /* If putting this section in this segment would force us to
3723 skip a page in the segment, then we need a new segment. */
3726 else if ((last_hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) == 0
3727 && (hdr
->flags
& (SEC_LOAD
| SEC_THREAD_LOCAL
)) != 0)
3729 /* We don't want to put a loadable section after a
3730 nonloadable section in the same segment.
3731 Consider .tbss sections as loadable for this purpose. */
3734 else if ((abfd
->flags
& D_PAGED
) == 0)
3736 /* If the file is not demand paged, which means that we
3737 don't require the sections to be correctly aligned in the
3738 file, then there is no other reason for a new segment. */
3739 new_segment
= FALSE
;
3742 && (hdr
->flags
& SEC_READONLY
) == 0
3743 && (((last_hdr
->lma
+ last_size
- 1)
3744 & ~(maxpagesize
- 1))
3745 != (hdr
->lma
& ~(maxpagesize
- 1))))
3747 /* We don't want to put a writable section in a read only
3748 segment, unless they are on the same page in memory
3749 anyhow. We already know that the last section does not
3750 bring us past the current section on the page, so the
3751 only case in which the new section is not on the same
3752 page as the previous section is when the previous section
3753 ends precisely on a page boundary. */
3758 /* Otherwise, we can use the same segment. */
3759 new_segment
= FALSE
;
3762 /* Allow interested parties a chance to override our decision. */
3763 if (last_hdr
!= NULL
3765 && info
->callbacks
->override_segment_assignment
!= NULL
)
3767 = info
->callbacks
->override_segment_assignment (info
, abfd
, hdr
,
3773 if ((hdr
->flags
& SEC_READONLY
) == 0)
3776 /* .tbss sections effectively have zero size. */
3777 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
))
3778 != SEC_THREAD_LOCAL
)
3779 last_size
= hdr
->size
;
3785 /* We need a new program segment. We must create a new program
3786 header holding all the sections from phdr_index until hdr. */
3788 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3795 if ((hdr
->flags
& SEC_READONLY
) == 0)
3801 /* .tbss sections effectively have zero size. */
3802 if ((hdr
->flags
& (SEC_THREAD_LOCAL
| SEC_LOAD
)) != SEC_THREAD_LOCAL
)
3803 last_size
= hdr
->size
;
3807 phdr_in_segment
= FALSE
;
3810 /* Create a final PT_LOAD program segment. */
3811 if (last_hdr
!= NULL
)
3813 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3821 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3824 m
= _bfd_elf_make_dynamic_segment (abfd
, dynsec
);
3831 /* For each batch of consecutive loadable .note sections,
3832 add a PT_NOTE segment. We don't use bfd_get_section_by_name,
3833 because if we link together nonloadable .note sections and
3834 loadable .note sections, we will generate two .note sections
3835 in the output file. FIXME: Using names for section types is
3837 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3839 if ((s
->flags
& SEC_LOAD
) != 0
3840 && CONST_STRNEQ (s
->name
, ".note"))
3844 amt
= sizeof (struct elf_segment_map
);
3845 if (s
->alignment_power
== 2)
3846 for (s2
= s
; s2
->next
!= NULL
; s2
= s2
->next
)
3848 if (s2
->next
->alignment_power
== 2
3849 && (s2
->next
->flags
& SEC_LOAD
) != 0
3850 && CONST_STRNEQ (s2
->next
->name
, ".note")
3851 && align_power (s2
->vma
+ s2
->size
, 2)
3857 amt
+= (count
- 1) * sizeof (asection
*);
3858 m
= bfd_zalloc (abfd
, amt
);
3862 m
->p_type
= PT_NOTE
;
3866 m
->sections
[m
->count
- count
--] = s
;
3867 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3870 m
->sections
[m
->count
- 1] = s
;
3871 BFD_ASSERT ((s
->flags
& SEC_THREAD_LOCAL
) == 0);
3875 if (s
->flags
& SEC_THREAD_LOCAL
)
3883 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3888 amt
= sizeof (struct elf_segment_map
);
3889 amt
+= (tls_count
- 1) * sizeof (asection
*);
3890 m
= bfd_zalloc (abfd
, amt
);
3895 m
->count
= tls_count
;
3896 /* Mandated PF_R. */
3898 m
->p_flags_valid
= 1;
3899 for (i
= 0; i
< tls_count
; ++i
)
3901 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3902 m
->sections
[i
] = first_tls
;
3903 first_tls
= first_tls
->next
;
3910 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3912 eh_frame_hdr
= elf_tdata (abfd
)->eh_frame_hdr
;
3913 if (eh_frame_hdr
!= NULL
3914 && (eh_frame_hdr
->output_section
->flags
& SEC_LOAD
) != 0)
3916 amt
= sizeof (struct elf_segment_map
);
3917 m
= bfd_zalloc (abfd
, amt
);
3921 m
->p_type
= PT_GNU_EH_FRAME
;
3923 m
->sections
[0] = eh_frame_hdr
->output_section
;
3929 if (elf_tdata (abfd
)->stack_flags
)
3931 amt
= sizeof (struct elf_segment_map
);
3932 m
= bfd_zalloc (abfd
, amt
);
3936 m
->p_type
= PT_GNU_STACK
;
3937 m
->p_flags
= elf_tdata (abfd
)->stack_flags
;
3938 m
->p_flags_valid
= 1;
3944 if (info
!= NULL
&& info
->relro
)
3946 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
3948 if (m
->p_type
== PT_LOAD
)
3950 asection
*last
= m
->sections
[m
->count
- 1];
3951 bfd_vma vaddr
= m
->sections
[0]->vma
;
3952 bfd_vma filesz
= last
->vma
- vaddr
+ last
->size
;
3954 if (vaddr
< info
->relro_end
3955 && vaddr
>= info
->relro_start
3956 && (vaddr
+ filesz
) >= info
->relro_end
)
3961 /* Make a PT_GNU_RELRO segment only when it isn't empty. */
3964 amt
= sizeof (struct elf_segment_map
);
3965 m
= bfd_zalloc (abfd
, amt
);
3969 m
->p_type
= PT_GNU_RELRO
;
3971 m
->p_flags_valid
= 1;
3979 elf_tdata (abfd
)->segment_map
= mfirst
;
3982 if (!elf_modify_segment_map (abfd
, info
, no_user_phdrs
))
3985 for (count
= 0, m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3987 elf_tdata (abfd
)->program_header_size
= count
* bed
->s
->sizeof_phdr
;
3992 if (sections
!= NULL
)
3997 /* Sort sections by address. */
4000 elf_sort_sections (const void *arg1
, const void *arg2
)
4002 const asection
*sec1
= *(const asection
**) arg1
;
4003 const asection
*sec2
= *(const asection
**) arg2
;
4004 bfd_size_type size1
, size2
;
4006 /* Sort by LMA first, since this is the address used to
4007 place the section into a segment. */
4008 if (sec1
->lma
< sec2
->lma
)
4010 else if (sec1
->lma
> sec2
->lma
)
4013 /* Then sort by VMA. Normally the LMA and the VMA will be
4014 the same, and this will do nothing. */
4015 if (sec1
->vma
< sec2
->vma
)
4017 else if (sec1
->vma
> sec2
->vma
)
4020 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
4022 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4028 /* If the indicies are the same, do not return 0
4029 here, but continue to try the next comparison. */
4030 if (sec1
->target_index
- sec2
->target_index
!= 0)
4031 return sec1
->target_index
- sec2
->target_index
;
4036 else if (TOEND (sec2
))
4041 /* Sort by size, to put zero sized sections
4042 before others at the same address. */
4044 size1
= (sec1
->flags
& SEC_LOAD
) ? sec1
->size
: 0;
4045 size2
= (sec2
->flags
& SEC_LOAD
) ? sec2
->size
: 0;
4052 return sec1
->target_index
- sec2
->target_index
;
4055 /* Ian Lance Taylor writes:
4057 We shouldn't be using % with a negative signed number. That's just
4058 not good. We have to make sure either that the number is not
4059 negative, or that the number has an unsigned type. When the types
4060 are all the same size they wind up as unsigned. When file_ptr is a
4061 larger signed type, the arithmetic winds up as signed long long,
4064 What we're trying to say here is something like ``increase OFF by
4065 the least amount that will cause it to be equal to the VMA modulo
4067 /* In other words, something like:
4069 vma_offset = m->sections[0]->vma % bed->maxpagesize;
4070 off_offset = off % bed->maxpagesize;
4071 if (vma_offset < off_offset)
4072 adjustment = vma_offset + bed->maxpagesize - off_offset;
4074 adjustment = vma_offset - off_offset;
4076 which can can be collapsed into the expression below. */
4079 vma_page_aligned_bias (bfd_vma vma
, ufile_ptr off
, bfd_vma maxpagesize
)
4081 return ((vma
- off
) % maxpagesize
);
4085 print_segment_map (const struct elf_segment_map
*m
)
4088 const char *pt
= get_segment_type (m
->p_type
);
4093 if (m
->p_type
>= PT_LOPROC
&& m
->p_type
<= PT_HIPROC
)
4094 sprintf (buf
, "LOPROC+%7.7x",
4095 (unsigned int) (m
->p_type
- PT_LOPROC
));
4096 else if (m
->p_type
>= PT_LOOS
&& m
->p_type
<= PT_HIOS
)
4097 sprintf (buf
, "LOOS+%7.7x",
4098 (unsigned int) (m
->p_type
- PT_LOOS
));
4100 snprintf (buf
, sizeof (buf
), "%8.8x",
4101 (unsigned int) m
->p_type
);
4104 fprintf (stderr
, "%s:", pt
);
4105 for (j
= 0; j
< m
->count
; j
++)
4106 fprintf (stderr
, " %s", m
->sections
[j
]->name
);
4110 /* Assign file positions to the sections based on the mapping from
4111 sections to segments. This function also sets up some fields in
4115 assign_file_positions_for_load_sections (bfd
*abfd
,
4116 struct bfd_link_info
*link_info
)
4118 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4119 struct elf_segment_map
*m
;
4120 Elf_Internal_Phdr
*phdrs
;
4121 Elf_Internal_Phdr
*p
;
4123 bfd_size_type maxpagesize
;
4126 bfd_vma header_pad
= 0;
4128 if (link_info
== NULL
4129 && !_bfd_elf_map_sections_to_segments (abfd
, link_info
))
4133 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4137 header_pad
= m
->header_size
;
4140 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
4141 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
4142 elf_elfheader (abfd
)->e_phnum
= alloc
;
4144 if (elf_tdata (abfd
)->program_header_size
== (bfd_size_type
) -1)
4145 elf_tdata (abfd
)->program_header_size
= alloc
* bed
->s
->sizeof_phdr
;
4147 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
4148 >= alloc
* bed
->s
->sizeof_phdr
);
4152 elf_tdata (abfd
)->next_file_pos
= bed
->s
->sizeof_ehdr
;
4156 /* We're writing the size in elf_tdata (abfd)->program_header_size,
4157 see assign_file_positions_except_relocs, so make sure we have
4158 that amount allocated, with trailing space cleared.
4159 The variable alloc contains the computed need, while elf_tdata
4160 (abfd)->program_header_size contains the size used for the
4162 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4163 where the layout is forced to according to a larger size in the
4164 last iterations for the testcase ld-elf/header. */
4165 BFD_ASSERT (elf_tdata (abfd
)->program_header_size
% bed
->s
->sizeof_phdr
4167 phdrs
= bfd_zalloc2 (abfd
,
4168 (elf_tdata (abfd
)->program_header_size
4169 / bed
->s
->sizeof_phdr
),
4170 sizeof (Elf_Internal_Phdr
));
4171 elf_tdata (abfd
)->phdr
= phdrs
;
4176 if ((abfd
->flags
& D_PAGED
) != 0)
4177 maxpagesize
= bed
->maxpagesize
;
4179 off
= bed
->s
->sizeof_ehdr
;
4180 off
+= alloc
* bed
->s
->sizeof_phdr
;
4181 if (header_pad
< (bfd_vma
) off
)
4187 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
, j
= 0;
4189 m
= m
->next
, p
++, j
++)
4193 bfd_boolean no_contents
;
4195 /* If elf_segment_map is not from map_sections_to_segments, the
4196 sections may not be correctly ordered. NOTE: sorting should
4197 not be done to the PT_NOTE section of a corefile, which may
4198 contain several pseudo-sections artificially created by bfd.
4199 Sorting these pseudo-sections breaks things badly. */
4201 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
4202 && m
->p_type
== PT_NOTE
))
4203 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
4206 /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4207 number of sections with contents contributing to both p_filesz
4208 and p_memsz, followed by a number of sections with no contents
4209 that just contribute to p_memsz. In this loop, OFF tracks next
4210 available file offset for PT_LOAD and PT_NOTE segments. */
4211 p
->p_type
= m
->p_type
;
4212 p
->p_flags
= m
->p_flags
;
4217 p
->p_vaddr
= m
->sections
[0]->vma
- m
->p_vaddr_offset
;
4219 if (m
->p_paddr_valid
)
4220 p
->p_paddr
= m
->p_paddr
;
4221 else if (m
->count
== 0)
4224 p
->p_paddr
= m
->sections
[0]->lma
- m
->p_vaddr_offset
;
4226 if (p
->p_type
== PT_LOAD
4227 && (abfd
->flags
& D_PAGED
) != 0)
4229 /* p_align in demand paged PT_LOAD segments effectively stores
4230 the maximum page size. When copying an executable with
4231 objcopy, we set m->p_align from the input file. Use this
4232 value for maxpagesize rather than bed->maxpagesize, which
4233 may be different. Note that we use maxpagesize for PT_TLS
4234 segment alignment later in this function, so we are relying
4235 on at least one PT_LOAD segment appearing before a PT_TLS
4237 if (m
->p_align_valid
)
4238 maxpagesize
= m
->p_align
;
4240 p
->p_align
= maxpagesize
;
4242 else if (m
->p_align_valid
)
4243 p
->p_align
= m
->p_align
;
4244 else if (m
->count
== 0)
4245 p
->p_align
= 1 << bed
->s
->log_file_align
;
4249 no_contents
= FALSE
;
4251 if (p
->p_type
== PT_LOAD
4254 bfd_size_type align
;
4255 unsigned int align_power
= 0;
4257 if (m
->p_align_valid
)
4261 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4263 unsigned int secalign
;
4265 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
4266 if (secalign
> align_power
)
4267 align_power
= secalign
;
4269 align
= (bfd_size_type
) 1 << align_power
;
4270 if (align
< maxpagesize
)
4271 align
= maxpagesize
;
4274 for (i
= 0; i
< m
->count
; i
++)
4275 if ((m
->sections
[i
]->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
4276 /* If we aren't making room for this section, then
4277 it must be SHT_NOBITS regardless of what we've
4278 set via struct bfd_elf_special_section. */
4279 elf_section_type (m
->sections
[i
]) = SHT_NOBITS
;
4281 /* Find out whether this segment contains any loadable
4284 for (i
= 0; i
< m
->count
; i
++)
4285 if (elf_section_type (m
->sections
[i
]) != SHT_NOBITS
)
4287 no_contents
= FALSE
;
4291 off_adjust
= vma_page_aligned_bias (m
->sections
[0]->vma
, off
, align
);
4295 /* We shouldn't need to align the segment on disk since
4296 the segment doesn't need file space, but the gABI
4297 arguably requires the alignment and glibc ld.so
4298 checks it. So to comply with the alignment
4299 requirement but not waste file space, we adjust
4300 p_offset for just this segment. (OFF_ADJUST is
4301 subtracted from OFF later.) This may put p_offset
4302 past the end of file, but that shouldn't matter. */
4307 /* Make sure the .dynamic section is the first section in the
4308 PT_DYNAMIC segment. */
4309 else if (p
->p_type
== PT_DYNAMIC
4311 && strcmp (m
->sections
[0]->name
, ".dynamic") != 0)
4314 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4316 bfd_set_error (bfd_error_bad_value
);
4319 /* Set the note section type to SHT_NOTE. */
4320 else if (p
->p_type
== PT_NOTE
)
4321 for (i
= 0; i
< m
->count
; i
++)
4322 elf_section_type (m
->sections
[i
]) = SHT_NOTE
;
4328 if (m
->includes_filehdr
)
4330 if (!m
->p_flags_valid
)
4332 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
4333 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
4336 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4338 if (p
->p_vaddr
< (bfd_vma
) off
)
4340 (*_bfd_error_handler
)
4341 (_("%B: Not enough room for program headers, try linking with -N"),
4343 bfd_set_error (bfd_error_bad_value
);
4348 if (!m
->p_paddr_valid
)
4353 if (m
->includes_phdrs
)
4355 if (!m
->p_flags_valid
)
4358 if (!m
->includes_filehdr
)
4360 p
->p_offset
= bed
->s
->sizeof_ehdr
;
4364 BFD_ASSERT (p
->p_type
== PT_LOAD
);
4365 p
->p_vaddr
-= off
- p
->p_offset
;
4366 if (!m
->p_paddr_valid
)
4367 p
->p_paddr
-= off
- p
->p_offset
;
4371 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
4372 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
4375 p
->p_filesz
+= header_pad
;
4376 p
->p_memsz
+= header_pad
;
4380 if (p
->p_type
== PT_LOAD
4381 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
4383 if (!m
->includes_filehdr
&& !m
->includes_phdrs
)
4389 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
4391 p
->p_filesz
+= adjust
;
4392 p
->p_memsz
+= adjust
;
4396 /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4397 maps. Set filepos for sections in PT_LOAD segments, and in
4398 core files, for sections in PT_NOTE segments.
4399 assign_file_positions_for_non_load_sections will set filepos
4400 for other sections and update p_filesz for other segments. */
4401 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4404 bfd_size_type align
;
4405 Elf_Internal_Shdr
*this_hdr
;
4408 this_hdr
= &elf_section_data (sec
)->this_hdr
;
4409 align
= (bfd_size_type
) 1 << bfd_get_section_alignment (abfd
, sec
);
4411 if ((p
->p_type
== PT_LOAD
4412 || p
->p_type
== PT_TLS
)
4413 && (this_hdr
->sh_type
!= SHT_NOBITS
4414 || ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0
4415 && ((this_hdr
->sh_flags
& SHF_TLS
) == 0
4416 || p
->p_type
== PT_TLS
))))
4418 bfd_signed_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
4422 (*_bfd_error_handler
)
4423 (_("%B: section %A vma 0x%lx overlaps previous sections"),
4424 abfd
, sec
, (unsigned long) sec
->vma
);
4427 p
->p_memsz
+= adjust
;
4429 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4432 p
->p_filesz
+= adjust
;
4436 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
4438 /* The section at i == 0 is the one that actually contains
4442 this_hdr
->sh_offset
= sec
->filepos
= off
;
4443 off
+= this_hdr
->sh_size
;
4444 p
->p_filesz
= this_hdr
->sh_size
;
4450 /* The rest are fake sections that shouldn't be written. */
4459 if (p
->p_type
== PT_LOAD
)
4461 this_hdr
->sh_offset
= sec
->filepos
= off
;
4462 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4463 off
+= this_hdr
->sh_size
;
4466 if (this_hdr
->sh_type
!= SHT_NOBITS
)
4468 p
->p_filesz
+= this_hdr
->sh_size
;
4469 /* A load section without SHF_ALLOC is something like
4470 a note section in a PT_NOTE segment. These take
4471 file space but are not loaded into memory. */
4472 if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4473 p
->p_memsz
+= this_hdr
->sh_size
;
4475 else if ((this_hdr
->sh_flags
& SHF_ALLOC
) != 0)
4477 if (p
->p_type
== PT_TLS
)
4478 p
->p_memsz
+= this_hdr
->sh_size
;
4480 /* .tbss is special. It doesn't contribute to p_memsz of
4482 else if ((this_hdr
->sh_flags
& SHF_TLS
) == 0)
4483 p
->p_memsz
+= this_hdr
->sh_size
;
4486 if (align
> p
->p_align
4487 && !m
->p_align_valid
4488 && (p
->p_type
!= PT_LOAD
4489 || (abfd
->flags
& D_PAGED
) == 0))
4493 if (!m
->p_flags_valid
)
4496 if ((this_hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
4498 if ((this_hdr
->sh_flags
& SHF_WRITE
) != 0)
4504 /* Check that all sections are in a PT_LOAD segment.
4505 Don't check funky gdb generated core files. */
4506 if (p
->p_type
== PT_LOAD
&& bfd_get_format (abfd
) != bfd_core
)
4507 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
4509 Elf_Internal_Shdr
*this_hdr
;
4513 this_hdr
= &(elf_section_data(sec
)->this_hdr
);
4514 if (this_hdr
->sh_size
!= 0
4515 && !ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, p
))
4517 (*_bfd_error_handler
)
4518 (_("%B: section `%A' can't be allocated in segment %d"),
4520 print_segment_map (m
);
4521 bfd_set_error (bfd_error_bad_value
);
4527 elf_tdata (abfd
)->next_file_pos
= off
;
4531 /* Assign file positions for the other sections. */
4534 assign_file_positions_for_non_load_sections (bfd
*abfd
,
4535 struct bfd_link_info
*link_info
)
4537 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4538 Elf_Internal_Shdr
**i_shdrpp
;
4539 Elf_Internal_Shdr
**hdrpp
;
4540 Elf_Internal_Phdr
*phdrs
;
4541 Elf_Internal_Phdr
*p
;
4542 struct elf_segment_map
*m
;
4543 bfd_vma filehdr_vaddr
, filehdr_paddr
;
4544 bfd_vma phdrs_vaddr
, phdrs_paddr
;
4546 unsigned int num_sec
;
4550 i_shdrpp
= elf_elfsections (abfd
);
4551 num_sec
= elf_numsections (abfd
);
4552 off
= elf_tdata (abfd
)->next_file_pos
;
4553 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4555 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4556 Elf_Internal_Shdr
*hdr
;
4559 if (hdr
->bfd_section
!= NULL
4560 && (hdr
->bfd_section
->filepos
!= 0
4561 || (hdr
->sh_type
== SHT_NOBITS
4562 && hdr
->contents
== NULL
)))
4563 BFD_ASSERT (hdr
->sh_offset
== hdr
->bfd_section
->filepos
);
4564 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4566 if (hdr
->sh_size
!= 0)
4567 ((*_bfd_error_handler
)
4568 (_("%B: warning: allocated section `%s' not in segment"),
4570 (hdr
->bfd_section
== NULL
4572 : hdr
->bfd_section
->name
)));
4573 /* We don't need to page align empty sections. */
4574 if ((abfd
->flags
& D_PAGED
) != 0 && hdr
->sh_size
!= 0)
4575 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4578 off
+= vma_page_aligned_bias (hdr
->sh_addr
, off
,
4580 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4583 else if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4584 && hdr
->bfd_section
== NULL
)
4585 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4586 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4587 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4588 hdr
->sh_offset
= -1;
4590 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4593 /* Now that we have set the section file positions, we can set up
4594 the file positions for the non PT_LOAD segments. */
4598 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
4600 phdrs
= elf_tdata (abfd
)->phdr
;
4601 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4606 if (p
->p_type
!= PT_LOAD
)
4609 if (m
->includes_filehdr
)
4611 filehdr_vaddr
= p
->p_vaddr
;
4612 filehdr_paddr
= p
->p_paddr
;
4614 if (m
->includes_phdrs
)
4616 phdrs_vaddr
= p
->p_vaddr
;
4617 phdrs_paddr
= p
->p_paddr
;
4618 if (m
->includes_filehdr
)
4620 phdrs_vaddr
+= bed
->s
->sizeof_ehdr
;
4621 phdrs_paddr
+= bed
->s
->sizeof_ehdr
;
4626 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
4630 if (p
->p_type
== PT_GNU_RELRO
)
4632 const Elf_Internal_Phdr
*lp
;
4634 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4636 if (link_info
!= NULL
)
4638 /* During linking the range of the RELRO segment is passed
4640 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4642 if (lp
->p_type
== PT_LOAD
4643 && lp
->p_vaddr
>= link_info
->relro_start
4644 && lp
->p_vaddr
< link_info
->relro_end
4645 && lp
->p_vaddr
+ lp
->p_filesz
>= link_info
->relro_end
)
4651 /* Otherwise we are copying an executable or shared
4652 library, but we need to use the same linker logic. */
4653 for (lp
= phdrs
; lp
< phdrs
+ count
; ++lp
)
4655 if (lp
->p_type
== PT_LOAD
4656 && lp
->p_paddr
== p
->p_paddr
)
4661 if (lp
< phdrs
+ count
)
4663 p
->p_vaddr
= lp
->p_vaddr
;
4664 p
->p_paddr
= lp
->p_paddr
;
4665 p
->p_offset
= lp
->p_offset
;
4666 if (link_info
!= NULL
)
4667 p
->p_filesz
= link_info
->relro_end
- lp
->p_vaddr
;
4668 else if (m
->p_size_valid
)
4669 p
->p_filesz
= m
->p_size
;
4672 p
->p_memsz
= p
->p_filesz
;
4674 p
->p_flags
= (lp
->p_flags
& ~PF_W
);
4676 else if (link_info
!= NULL
)
4678 memset (p
, 0, sizeof *p
);
4679 p
->p_type
= PT_NULL
;
4684 else if (m
->count
!= 0)
4686 if (p
->p_type
!= PT_LOAD
4687 && (p
->p_type
!= PT_NOTE
4688 || bfd_get_format (abfd
) != bfd_core
))
4690 Elf_Internal_Shdr
*hdr
;
4693 BFD_ASSERT (!m
->includes_filehdr
&& !m
->includes_phdrs
);
4695 sect
= m
->sections
[m
->count
- 1];
4696 hdr
= &elf_section_data (sect
)->this_hdr
;
4697 p
->p_filesz
= sect
->filepos
- m
->sections
[0]->filepos
;
4698 if (hdr
->sh_type
!= SHT_NOBITS
)
4699 p
->p_filesz
+= hdr
->sh_size
;
4700 p
->p_offset
= m
->sections
[0]->filepos
;
4703 else if (m
->includes_filehdr
)
4705 p
->p_vaddr
= filehdr_vaddr
;
4706 if (! m
->p_paddr_valid
)
4707 p
->p_paddr
= filehdr_paddr
;
4709 else if (m
->includes_phdrs
)
4711 p
->p_vaddr
= phdrs_vaddr
;
4712 if (! m
->p_paddr_valid
)
4713 p
->p_paddr
= phdrs_paddr
;
4717 elf_tdata (abfd
)->next_file_pos
= off
;
4722 /* Work out the file positions of all the sections. This is called by
4723 _bfd_elf_compute_section_file_positions. All the section sizes and
4724 VMAs must be known before this is called.
4726 Reloc sections come in two flavours: Those processed specially as
4727 "side-channel" data attached to a section to which they apply, and
4728 those that bfd doesn't process as relocations. The latter sort are
4729 stored in a normal bfd section by bfd_section_from_shdr. We don't
4730 consider the former sort here, unless they form part of the loadable
4731 image. Reloc sections not assigned here will be handled later by
4732 assign_file_positions_for_relocs.
4734 We also don't set the positions of the .symtab and .strtab here. */
4737 assign_file_positions_except_relocs (bfd
*abfd
,
4738 struct bfd_link_info
*link_info
)
4740 struct elf_obj_tdata
*tdata
= elf_tdata (abfd
);
4741 Elf_Internal_Ehdr
*i_ehdrp
= elf_elfheader (abfd
);
4743 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4745 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4746 && bfd_get_format (abfd
) != bfd_core
)
4748 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4749 unsigned int num_sec
= elf_numsections (abfd
);
4750 Elf_Internal_Shdr
**hdrpp
;
4753 /* Start after the ELF header. */
4754 off
= i_ehdrp
->e_ehsize
;
4756 /* We are not creating an executable, which means that we are
4757 not creating a program header, and that the actual order of
4758 the sections in the file is unimportant. */
4759 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4761 Elf_Internal_Shdr
*hdr
;
4764 if (((hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
4765 && hdr
->bfd_section
== NULL
)
4766 || i
== tdata
->symtab_section
4767 || i
== tdata
->symtab_shndx_section
4768 || i
== tdata
->strtab_section
)
4770 hdr
->sh_offset
= -1;
4773 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, TRUE
);
4780 /* Assign file positions for the loaded sections based on the
4781 assignment of sections to segments. */
4782 if (!assign_file_positions_for_load_sections (abfd
, link_info
))
4785 /* And for non-load sections. */
4786 if (!assign_file_positions_for_non_load_sections (abfd
, link_info
))
4789 if (bed
->elf_backend_modify_program_headers
!= NULL
)
4791 if (!(*bed
->elf_backend_modify_program_headers
) (abfd
, link_info
))
4795 /* Write out the program headers. */
4796 alloc
= tdata
->program_header_size
/ bed
->s
->sizeof_phdr
;
4797 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
4798 || bed
->s
->write_out_phdrs (abfd
, tdata
->phdr
, alloc
) != 0)
4801 off
= tdata
->next_file_pos
;
4804 /* Place the section headers. */
4805 off
= align_file_position (off
, 1 << bed
->s
->log_file_align
);
4806 i_ehdrp
->e_shoff
= off
;
4807 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4809 tdata
->next_file_pos
= off
;
4815 prep_headers (bfd
*abfd
)
4817 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4818 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4819 struct elf_strtab_hash
*shstrtab
;
4820 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4822 i_ehdrp
= elf_elfheader (abfd
);
4824 shstrtab
= _bfd_elf_strtab_init ();
4825 if (shstrtab
== NULL
)
4828 elf_shstrtab (abfd
) = shstrtab
;
4830 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4831 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4832 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4833 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4835 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4836 i_ehdrp
->e_ident
[EI_DATA
] =
4837 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4838 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4840 if ((abfd
->flags
& DYNAMIC
) != 0)
4841 i_ehdrp
->e_type
= ET_DYN
;
4842 else if ((abfd
->flags
& EXEC_P
) != 0)
4843 i_ehdrp
->e_type
= ET_EXEC
;
4844 else if (bfd_get_format (abfd
) == bfd_core
)
4845 i_ehdrp
->e_type
= ET_CORE
;
4847 i_ehdrp
->e_type
= ET_REL
;
4849 switch (bfd_get_arch (abfd
))
4851 case bfd_arch_unknown
:
4852 i_ehdrp
->e_machine
= EM_NONE
;
4855 /* There used to be a long list of cases here, each one setting
4856 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4857 in the corresponding bfd definition. To avoid duplication,
4858 the switch was removed. Machines that need special handling
4859 can generally do it in elf_backend_final_write_processing(),
4860 unless they need the information earlier than the final write.
4861 Such need can generally be supplied by replacing the tests for
4862 e_machine with the conditions used to determine it. */
4864 i_ehdrp
->e_machine
= bed
->elf_machine_code
;
4867 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4868 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4870 /* No program header, for now. */
4871 i_ehdrp
->e_phoff
= 0;
4872 i_ehdrp
->e_phentsize
= 0;
4873 i_ehdrp
->e_phnum
= 0;
4875 /* Each bfd section is section header entry. */
4876 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4877 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4879 /* If we're building an executable, we'll need a program header table. */
4880 if (abfd
->flags
& EXEC_P
)
4881 /* It all happens later. */
4885 i_ehdrp
->e_phentsize
= 0;
4887 i_ehdrp
->e_phoff
= 0;
4890 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4891 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", FALSE
);
4892 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4893 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", FALSE
);
4894 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4895 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", FALSE
);
4896 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4897 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4898 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4904 /* Assign file positions for all the reloc sections which are not part
4905 of the loadable file image. */
4908 _bfd_elf_assign_file_positions_for_relocs (bfd
*abfd
)
4911 unsigned int i
, num_sec
;
4912 Elf_Internal_Shdr
**shdrpp
;
4914 off
= elf_tdata (abfd
)->next_file_pos
;
4916 num_sec
= elf_numsections (abfd
);
4917 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4919 Elf_Internal_Shdr
*shdrp
;
4922 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4923 && shdrp
->sh_offset
== -1)
4924 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, TRUE
);
4927 elf_tdata (abfd
)->next_file_pos
= off
;
4931 _bfd_elf_write_object_contents (bfd
*abfd
)
4933 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4934 Elf_Internal_Ehdr
*i_ehdrp
;
4935 Elf_Internal_Shdr
**i_shdrp
;
4937 unsigned int count
, num_sec
;
4939 if (! abfd
->output_has_begun
4940 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
4943 i_shdrp
= elf_elfsections (abfd
);
4944 i_ehdrp
= elf_elfheader (abfd
);
4947 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4951 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4953 /* After writing the headers, we need to write the sections too... */
4954 num_sec
= elf_numsections (abfd
);
4955 for (count
= 1; count
< num_sec
; count
++)
4957 if (bed
->elf_backend_section_processing
)
4958 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4959 if (i_shdrp
[count
]->contents
)
4961 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4963 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4964 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4969 /* Write out the section header names. */
4970 if (elf_shstrtab (abfd
) != NULL
4971 && (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4972 || !_bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
))))
4975 if (bed
->elf_backend_final_write_processing
)
4976 (*bed
->elf_backend_final_write_processing
) (abfd
,
4977 elf_tdata (abfd
)->linker
);
4979 if (!bed
->s
->write_shdrs_and_ehdr (abfd
))
4982 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */
4983 if (elf_tdata (abfd
)->after_write_object_contents
)
4984 return (*elf_tdata (abfd
)->after_write_object_contents
) (abfd
);
4990 _bfd_elf_write_corefile_contents (bfd
*abfd
)
4992 /* Hopefully this can be done just like an object file. */
4993 return _bfd_elf_write_object_contents (abfd
);
4996 /* Given a section, search the header to find them. */
4999 _bfd_elf_section_from_bfd_section (bfd
*abfd
, struct bfd_section
*asect
)
5001 const struct elf_backend_data
*bed
;
5004 if (elf_section_data (asect
) != NULL
5005 && elf_section_data (asect
)->this_idx
!= 0)
5006 return elf_section_data (asect
)->this_idx
;
5008 if (bfd_is_abs_section (asect
))
5010 else if (bfd_is_com_section (asect
))
5012 else if (bfd_is_und_section (asect
))
5017 bed
= get_elf_backend_data (abfd
);
5018 if (bed
->elf_backend_section_from_bfd_section
)
5022 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
5026 if (index
== SHN_BAD
)
5027 bfd_set_error (bfd_error_nonrepresentable_section
);
5032 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
5036 _bfd_elf_symbol_from_bfd_symbol (bfd
*abfd
, asymbol
**asym_ptr_ptr
)
5038 asymbol
*asym_ptr
= *asym_ptr_ptr
;
5040 flagword flags
= asym_ptr
->flags
;
5042 /* When gas creates relocations against local labels, it creates its
5043 own symbol for the section, but does put the symbol into the
5044 symbol chain, so udata is 0. When the linker is generating
5045 relocatable output, this section symbol may be for one of the
5046 input sections rather than the output section. */
5047 if (asym_ptr
->udata
.i
== 0
5048 && (flags
& BSF_SECTION_SYM
)
5049 && asym_ptr
->section
)
5054 sec
= asym_ptr
->section
;
5055 if (sec
->owner
!= abfd
&& sec
->output_section
!= NULL
)
5056 sec
= sec
->output_section
;
5057 if (sec
->owner
== abfd
5058 && (indx
= sec
->index
) < elf_num_section_syms (abfd
)
5059 && elf_section_syms (abfd
)[indx
] != NULL
)
5060 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
5063 idx
= asym_ptr
->udata
.i
;
5067 /* This case can occur when using --strip-symbol on a symbol
5068 which is used in a relocation entry. */
5069 (*_bfd_error_handler
)
5070 (_("%B: symbol `%s' required but not present"),
5071 abfd
, bfd_asymbol_name (asym_ptr
));
5072 bfd_set_error (bfd_error_no_symbols
);
5079 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
5080 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
5081 elf_symbol_flags (flags
));
5089 /* Rewrite program header information. */
5092 rewrite_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5094 Elf_Internal_Ehdr
*iehdr
;
5095 struct elf_segment_map
*map
;
5096 struct elf_segment_map
*map_first
;
5097 struct elf_segment_map
**pointer_to_map
;
5098 Elf_Internal_Phdr
*segment
;
5101 unsigned int num_segments
;
5102 bfd_boolean phdr_included
= FALSE
;
5103 bfd_boolean p_paddr_valid
;
5104 bfd_vma maxpagesize
;
5105 struct elf_segment_map
*phdr_adjust_seg
= NULL
;
5106 unsigned int phdr_adjust_num
= 0;
5107 const struct elf_backend_data
*bed
;
5109 bed
= get_elf_backend_data (ibfd
);
5110 iehdr
= elf_elfheader (ibfd
);
5113 pointer_to_map
= &map_first
;
5115 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5116 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
5118 /* Returns the end address of the segment + 1. */
5119 #define SEGMENT_END(segment, start) \
5120 (start + (segment->p_memsz > segment->p_filesz \
5121 ? segment->p_memsz : segment->p_filesz))
5123 #define SECTION_SIZE(section, segment) \
5124 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \
5125 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \
5126 ? section->size : 0)
5128 /* Returns TRUE if the given section is contained within
5129 the given segment. VMA addresses are compared. */
5130 #define IS_CONTAINED_BY_VMA(section, segment) \
5131 (section->vma >= segment->p_vaddr \
5132 && (section->vma + SECTION_SIZE (section, segment) \
5133 <= (SEGMENT_END (segment, segment->p_vaddr))))
5135 /* Returns TRUE if the given section is contained within
5136 the given segment. LMA addresses are compared. */
5137 #define IS_CONTAINED_BY_LMA(section, segment, base) \
5138 (section->lma >= base \
5139 && (section->lma + SECTION_SIZE (section, segment) \
5140 <= SEGMENT_END (segment, base)))
5142 /* Handle PT_NOTE segment. */
5143 #define IS_NOTE(p, s) \
5144 (p->p_type == PT_NOTE \
5145 && elf_section_type (s) == SHT_NOTE \
5146 && (bfd_vma) s->filepos >= p->p_offset \
5147 && ((bfd_vma) s->filepos + s->size \
5148 <= p->p_offset + p->p_filesz))
5150 /* Special case: corefile "NOTE" section containing regs, prpsinfo
5152 #define IS_COREFILE_NOTE(p, s) \
5154 && bfd_get_format (ibfd) == bfd_core \
5158 /* The complicated case when p_vaddr is 0 is to handle the Solaris
5159 linker, which generates a PT_INTERP section with p_vaddr and
5160 p_memsz set to 0. */
5161 #define IS_SOLARIS_PT_INTERP(p, s) \
5163 && p->p_paddr == 0 \
5164 && p->p_memsz == 0 \
5165 && p->p_filesz > 0 \
5166 && (s->flags & SEC_HAS_CONTENTS) != 0 \
5168 && (bfd_vma) s->filepos >= p->p_offset \
5169 && ((bfd_vma) s->filepos + s->size \
5170 <= p->p_offset + p->p_filesz))
5172 /* Decide if the given section should be included in the given segment.
5173 A section will be included if:
5174 1. It is within the address space of the segment -- we use the LMA
5175 if that is set for the segment and the VMA otherwise,
5176 2. It is an allocated section or a NOTE section in a PT_NOTE
5178 3. There is an output section associated with it,
5179 4. The section has not already been allocated to a previous segment.
5180 5. PT_GNU_STACK segments do not include any sections.
5181 6. PT_TLS segment includes only SHF_TLS sections.
5182 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5183 8. PT_DYNAMIC should not contain empty sections at the beginning
5184 (with the possible exception of .dynamic). */
5185 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed) \
5186 ((((segment->p_paddr \
5187 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
5188 : IS_CONTAINED_BY_VMA (section, segment)) \
5189 && (section->flags & SEC_ALLOC) != 0) \
5190 || IS_NOTE (segment, section)) \
5191 && segment->p_type != PT_GNU_STACK \
5192 && (segment->p_type != PT_TLS \
5193 || (section->flags & SEC_THREAD_LOCAL)) \
5194 && (segment->p_type == PT_LOAD \
5195 || segment->p_type == PT_TLS \
5196 || (section->flags & SEC_THREAD_LOCAL) == 0) \
5197 && (segment->p_type != PT_DYNAMIC \
5198 || SECTION_SIZE (section, segment) > 0 \
5199 || (segment->p_paddr \
5200 ? segment->p_paddr != section->lma \
5201 : segment->p_vaddr != section->vma) \
5202 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \
5204 && !section->segment_mark)
5206 /* If the output section of a section in the input segment is NULL,
5207 it is removed from the corresponding output segment. */
5208 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
5209 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed) \
5210 && section->output_section != NULL)
5212 /* Returns TRUE iff seg1 starts after the end of seg2. */
5213 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \
5214 (seg1->field >= SEGMENT_END (seg2, seg2->field))
5216 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5217 their VMA address ranges and their LMA address ranges overlap.
5218 It is possible to have overlapping VMA ranges without overlapping LMA
5219 ranges. RedBoot images for example can have both .data and .bss mapped
5220 to the same VMA range, but with the .data section mapped to a different
5222 #define SEGMENT_OVERLAPS(seg1, seg2) \
5223 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \
5224 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \
5225 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \
5226 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5228 /* Initialise the segment mark field. */
5229 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
5230 section
->segment_mark
= FALSE
;
5232 /* The Solaris linker creates program headers in which all the
5233 p_paddr fields are zero. When we try to objcopy or strip such a
5234 file, we get confused. Check for this case, and if we find it
5235 don't set the p_paddr_valid fields. */
5236 p_paddr_valid
= FALSE
;
5237 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5240 if (segment
->p_paddr
!= 0)
5242 p_paddr_valid
= TRUE
;
5246 /* Scan through the segments specified in the program header
5247 of the input BFD. For this first scan we look for overlaps
5248 in the loadable segments. These can be created by weird
5249 parameters to objcopy. Also, fix some solaris weirdness. */
5250 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5255 Elf_Internal_Phdr
*segment2
;
5257 if (segment
->p_type
== PT_INTERP
)
5258 for (section
= ibfd
->sections
; section
; section
= section
->next
)
5259 if (IS_SOLARIS_PT_INTERP (segment
, section
))
5261 /* Mininal change so that the normal section to segment
5262 assignment code will work. */
5263 segment
->p_vaddr
= section
->vma
;
5267 if (segment
->p_type
!= PT_LOAD
)
5269 /* Remove PT_GNU_RELRO segment. */
5270 if (segment
->p_type
== PT_GNU_RELRO
)
5271 segment
->p_type
= PT_NULL
;
5275 /* Determine if this segment overlaps any previous segments. */
5276 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
5278 bfd_signed_vma extra_length
;
5280 if (segment2
->p_type
!= PT_LOAD
5281 || !SEGMENT_OVERLAPS (segment
, segment2
))
5284 /* Merge the two segments together. */
5285 if (segment2
->p_vaddr
< segment
->p_vaddr
)
5287 /* Extend SEGMENT2 to include SEGMENT and then delete
5289 extra_length
= (SEGMENT_END (segment
, segment
->p_vaddr
)
5290 - SEGMENT_END (segment2
, segment2
->p_vaddr
));
5292 if (extra_length
> 0)
5294 segment2
->p_memsz
+= extra_length
;
5295 segment2
->p_filesz
+= extra_length
;
5298 segment
->p_type
= PT_NULL
;
5300 /* Since we have deleted P we must restart the outer loop. */
5302 segment
= elf_tdata (ibfd
)->phdr
;
5307 /* Extend SEGMENT to include SEGMENT2 and then delete
5309 extra_length
= (SEGMENT_END (segment2
, segment2
->p_vaddr
)
5310 - SEGMENT_END (segment
, segment
->p_vaddr
));
5312 if (extra_length
> 0)
5314 segment
->p_memsz
+= extra_length
;
5315 segment
->p_filesz
+= extra_length
;
5318 segment2
->p_type
= PT_NULL
;
5323 /* The second scan attempts to assign sections to segments. */
5324 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5328 unsigned int section_count
;
5329 asection
**sections
;
5330 asection
*output_section
;
5332 bfd_vma matching_lma
;
5333 bfd_vma suggested_lma
;
5336 asection
*first_section
;
5337 bfd_boolean first_matching_lma
;
5338 bfd_boolean first_suggested_lma
;
5340 if (segment
->p_type
== PT_NULL
)
5343 first_section
= NULL
;
5344 /* Compute how many sections might be placed into this segment. */
5345 for (section
= ibfd
->sections
, section_count
= 0;
5347 section
= section
->next
)
5349 /* Find the first section in the input segment, which may be
5350 removed from the corresponding output segment. */
5351 if (IS_SECTION_IN_INPUT_SEGMENT (section
, segment
, bed
))
5353 if (first_section
== NULL
)
5354 first_section
= section
;
5355 if (section
->output_section
!= NULL
)
5360 /* Allocate a segment map big enough to contain
5361 all of the sections we have selected. */
5362 amt
= sizeof (struct elf_segment_map
);
5363 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5364 map
= bfd_zalloc (obfd
, amt
);
5368 /* Initialise the fields of the segment map. Default to
5369 using the physical address of the segment in the input BFD. */
5371 map
->p_type
= segment
->p_type
;
5372 map
->p_flags
= segment
->p_flags
;
5373 map
->p_flags_valid
= 1;
5375 /* If the first section in the input segment is removed, there is
5376 no need to preserve segment physical address in the corresponding
5378 if (!first_section
|| first_section
->output_section
!= NULL
)
5380 map
->p_paddr
= segment
->p_paddr
;
5381 map
->p_paddr_valid
= p_paddr_valid
;
5384 /* Determine if this segment contains the ELF file header
5385 and if it contains the program headers themselves. */
5386 map
->includes_filehdr
= (segment
->p_offset
== 0
5387 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5388 map
->includes_phdrs
= 0;
5390 if (!phdr_included
|| segment
->p_type
!= PT_LOAD
)
5392 map
->includes_phdrs
=
5393 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5394 && (segment
->p_offset
+ segment
->p_filesz
5395 >= ((bfd_vma
) iehdr
->e_phoff
5396 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5398 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5399 phdr_included
= TRUE
;
5402 if (section_count
== 0)
5404 /* Special segments, such as the PT_PHDR segment, may contain
5405 no sections, but ordinary, loadable segments should contain
5406 something. They are allowed by the ELF spec however, so only
5407 a warning is produced. */
5408 if (segment
->p_type
== PT_LOAD
)
5409 (*_bfd_error_handler
) (_("%B: warning: Empty loadable segment"
5410 " detected, is this intentional ?\n"),
5414 *pointer_to_map
= map
;
5415 pointer_to_map
= &map
->next
;
5420 /* Now scan the sections in the input BFD again and attempt
5421 to add their corresponding output sections to the segment map.
5422 The problem here is how to handle an output section which has
5423 been moved (ie had its LMA changed). There are four possibilities:
5425 1. None of the sections have been moved.
5426 In this case we can continue to use the segment LMA from the
5429 2. All of the sections have been moved by the same amount.
5430 In this case we can change the segment's LMA to match the LMA
5431 of the first section.
5433 3. Some of the sections have been moved, others have not.
5434 In this case those sections which have not been moved can be
5435 placed in the current segment which will have to have its size,
5436 and possibly its LMA changed, and a new segment or segments will
5437 have to be created to contain the other sections.
5439 4. The sections have been moved, but not by the same amount.
5440 In this case we can change the segment's LMA to match the LMA
5441 of the first section and we will have to create a new segment
5442 or segments to contain the other sections.
5444 In order to save time, we allocate an array to hold the section
5445 pointers that we are interested in. As these sections get assigned
5446 to a segment, they are removed from this array. */
5448 sections
= bfd_malloc2 (section_count
, sizeof (asection
*));
5449 if (sections
== NULL
)
5452 /* Step One: Scan for segment vs section LMA conflicts.
5453 Also add the sections to the section array allocated above.
5454 Also add the sections to the current segment. In the common
5455 case, where the sections have not been moved, this means that
5456 we have completely filled the segment, and there is nothing
5461 first_matching_lma
= TRUE
;
5462 first_suggested_lma
= TRUE
;
5464 for (section
= ibfd
->sections
;
5466 section
= section
->next
)
5467 if (section
== first_section
)
5470 for (j
= 0; section
!= NULL
; section
= section
->next
)
5472 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
5474 output_section
= section
->output_section
;
5476 sections
[j
++] = section
;
5478 /* The Solaris native linker always sets p_paddr to 0.
5479 We try to catch that case here, and set it to the
5480 correct value. Note - some backends require that
5481 p_paddr be left as zero. */
5483 && segment
->p_vaddr
!= 0
5484 && !bed
->want_p_paddr_set_to_zero
5486 && output_section
->lma
!= 0
5487 && output_section
->vma
== (segment
->p_vaddr
5488 + (map
->includes_filehdr
5491 + (map
->includes_phdrs
5493 * iehdr
->e_phentsize
)
5495 map
->p_paddr
= segment
->p_vaddr
;
5497 /* Match up the physical address of the segment with the
5498 LMA address of the output section. */
5499 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5500 || IS_COREFILE_NOTE (segment
, section
)
5501 || (bed
->want_p_paddr_set_to_zero
5502 && IS_CONTAINED_BY_VMA (output_section
, segment
)))
5504 if (first_matching_lma
|| output_section
->lma
< matching_lma
)
5506 matching_lma
= output_section
->lma
;
5507 first_matching_lma
= FALSE
;
5510 /* We assume that if the section fits within the segment
5511 then it does not overlap any other section within that
5513 map
->sections
[isec
++] = output_section
;
5515 else if (first_suggested_lma
)
5517 suggested_lma
= output_section
->lma
;
5518 first_suggested_lma
= FALSE
;
5521 if (j
== section_count
)
5526 BFD_ASSERT (j
== section_count
);
5528 /* Step Two: Adjust the physical address of the current segment,
5530 if (isec
== section_count
)
5532 /* All of the sections fitted within the segment as currently
5533 specified. This is the default case. Add the segment to
5534 the list of built segments and carry on to process the next
5535 program header in the input BFD. */
5536 map
->count
= section_count
;
5537 *pointer_to_map
= map
;
5538 pointer_to_map
= &map
->next
;
5541 && !bed
->want_p_paddr_set_to_zero
5542 && matching_lma
!= map
->p_paddr
5543 && !map
->includes_filehdr
5544 && !map
->includes_phdrs
)
5545 /* There is some padding before the first section in the
5546 segment. So, we must account for that in the output
5548 map
->p_vaddr_offset
= matching_lma
- map
->p_paddr
;
5555 if (!first_matching_lma
)
5557 /* At least one section fits inside the current segment.
5558 Keep it, but modify its physical address to match the
5559 LMA of the first section that fitted. */
5560 map
->p_paddr
= matching_lma
;
5564 /* None of the sections fitted inside the current segment.
5565 Change the current segment's physical address to match
5566 the LMA of the first section. */
5567 map
->p_paddr
= suggested_lma
;
5570 /* Offset the segment physical address from the lma
5571 to allow for space taken up by elf headers. */
5572 if (map
->includes_filehdr
)
5574 if (map
->p_paddr
>= iehdr
->e_ehsize
)
5575 map
->p_paddr
-= iehdr
->e_ehsize
;
5578 map
->includes_filehdr
= FALSE
;
5579 map
->includes_phdrs
= FALSE
;
5583 if (map
->includes_phdrs
)
5585 if (map
->p_paddr
>= iehdr
->e_phnum
* iehdr
->e_phentsize
)
5587 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
5589 /* iehdr->e_phnum is just an estimate of the number
5590 of program headers that we will need. Make a note
5591 here of the number we used and the segment we chose
5592 to hold these headers, so that we can adjust the
5593 offset when we know the correct value. */
5594 phdr_adjust_num
= iehdr
->e_phnum
;
5595 phdr_adjust_seg
= map
;
5598 map
->includes_phdrs
= FALSE
;
5602 /* Step Three: Loop over the sections again, this time assigning
5603 those that fit to the current segment and removing them from the
5604 sections array; but making sure not to leave large gaps. Once all
5605 possible sections have been assigned to the current segment it is
5606 added to the list of built segments and if sections still remain
5607 to be assigned, a new segment is constructed before repeating
5614 first_suggested_lma
= TRUE
;
5616 /* Fill the current segment with sections that fit. */
5617 for (j
= 0; j
< section_count
; j
++)
5619 section
= sections
[j
];
5621 if (section
== NULL
)
5624 output_section
= section
->output_section
;
5626 BFD_ASSERT (output_section
!= NULL
);
5628 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
5629 || IS_COREFILE_NOTE (segment
, section
))
5631 if (map
->count
== 0)
5633 /* If the first section in a segment does not start at
5634 the beginning of the segment, then something is
5636 if (output_section
->lma
5638 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
5639 + (map
->includes_phdrs
5640 ? iehdr
->e_phnum
* iehdr
->e_phentsize
5648 prev_sec
= map
->sections
[map
->count
- 1];
5650 /* If the gap between the end of the previous section
5651 and the start of this section is more than
5652 maxpagesize then we need to start a new segment. */
5653 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->size
,
5655 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
5656 || (prev_sec
->lma
+ prev_sec
->size
5657 > output_section
->lma
))
5659 if (first_suggested_lma
)
5661 suggested_lma
= output_section
->lma
;
5662 first_suggested_lma
= FALSE
;
5669 map
->sections
[map
->count
++] = output_section
;
5672 section
->segment_mark
= TRUE
;
5674 else if (first_suggested_lma
)
5676 suggested_lma
= output_section
->lma
;
5677 first_suggested_lma
= FALSE
;
5681 BFD_ASSERT (map
->count
> 0);
5683 /* Add the current segment to the list of built segments. */
5684 *pointer_to_map
= map
;
5685 pointer_to_map
= &map
->next
;
5687 if (isec
< section_count
)
5689 /* We still have not allocated all of the sections to
5690 segments. Create a new segment here, initialise it
5691 and carry on looping. */
5692 amt
= sizeof (struct elf_segment_map
);
5693 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5694 map
= bfd_alloc (obfd
, amt
);
5701 /* Initialise the fields of the segment map. Set the physical
5702 physical address to the LMA of the first section that has
5703 not yet been assigned. */
5705 map
->p_type
= segment
->p_type
;
5706 map
->p_flags
= segment
->p_flags
;
5707 map
->p_flags_valid
= 1;
5708 map
->p_paddr
= suggested_lma
;
5709 map
->p_paddr_valid
= p_paddr_valid
;
5710 map
->includes_filehdr
= 0;
5711 map
->includes_phdrs
= 0;
5714 while (isec
< section_count
);
5719 elf_tdata (obfd
)->segment_map
= map_first
;
5721 /* If we had to estimate the number of program headers that were
5722 going to be needed, then check our estimate now and adjust
5723 the offset if necessary. */
5724 if (phdr_adjust_seg
!= NULL
)
5728 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5731 if (count
> phdr_adjust_num
)
5732 phdr_adjust_seg
->p_paddr
5733 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5738 #undef IS_CONTAINED_BY_VMA
5739 #undef IS_CONTAINED_BY_LMA
5741 #undef IS_COREFILE_NOTE
5742 #undef IS_SOLARIS_PT_INTERP
5743 #undef IS_SECTION_IN_INPUT_SEGMENT
5744 #undef INCLUDE_SECTION_IN_SEGMENT
5745 #undef SEGMENT_AFTER_SEGMENT
5746 #undef SEGMENT_OVERLAPS
5750 /* Copy ELF program header information. */
5753 copy_elf_program_header (bfd
*ibfd
, bfd
*obfd
)
5755 Elf_Internal_Ehdr
*iehdr
;
5756 struct elf_segment_map
*map
;
5757 struct elf_segment_map
*map_first
;
5758 struct elf_segment_map
**pointer_to_map
;
5759 Elf_Internal_Phdr
*segment
;
5761 unsigned int num_segments
;
5762 bfd_boolean phdr_included
= FALSE
;
5763 bfd_boolean p_paddr_valid
;
5765 iehdr
= elf_elfheader (ibfd
);
5768 pointer_to_map
= &map_first
;
5770 /* If all the segment p_paddr fields are zero, don't set
5771 map->p_paddr_valid. */
5772 p_paddr_valid
= FALSE
;
5773 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5774 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5777 if (segment
->p_paddr
!= 0)
5779 p_paddr_valid
= TRUE
;
5783 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5788 unsigned int section_count
;
5790 Elf_Internal_Shdr
*this_hdr
;
5791 asection
*first_section
= NULL
;
5792 asection
*lowest_section
= NULL
;
5794 /* Compute how many sections are in this segment. */
5795 for (section
= ibfd
->sections
, section_count
= 0;
5797 section
= section
->next
)
5799 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5800 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5803 first_section
= lowest_section
= section
;
5804 if (section
->lma
< lowest_section
->lma
)
5805 lowest_section
= section
;
5810 /* Allocate a segment map big enough to contain
5811 all of the sections we have selected. */
5812 amt
= sizeof (struct elf_segment_map
);
5813 if (section_count
!= 0)
5814 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
5815 map
= bfd_zalloc (obfd
, amt
);
5819 /* Initialize the fields of the output segment map with the
5822 map
->p_type
= segment
->p_type
;
5823 map
->p_flags
= segment
->p_flags
;
5824 map
->p_flags_valid
= 1;
5825 map
->p_paddr
= segment
->p_paddr
;
5826 map
->p_paddr_valid
= p_paddr_valid
;
5827 map
->p_align
= segment
->p_align
;
5828 map
->p_align_valid
= 1;
5829 map
->p_vaddr_offset
= 0;
5831 if (map
->p_type
== PT_GNU_RELRO
5832 && segment
->p_filesz
== segment
->p_memsz
)
5834 /* The PT_GNU_RELRO segment may contain the first a few
5835 bytes in the .got.plt section even if the whole .got.plt
5836 section isn't in the PT_GNU_RELRO segment. We won't
5837 change the size of the PT_GNU_RELRO segment. */
5838 map
->p_size
= segment
->p_filesz
;
5839 map
->p_size_valid
= 1;
5842 /* Determine if this segment contains the ELF file header
5843 and if it contains the program headers themselves. */
5844 map
->includes_filehdr
= (segment
->p_offset
== 0
5845 && segment
->p_filesz
>= iehdr
->e_ehsize
);
5847 map
->includes_phdrs
= 0;
5848 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
5850 map
->includes_phdrs
=
5851 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
5852 && (segment
->p_offset
+ segment
->p_filesz
5853 >= ((bfd_vma
) iehdr
->e_phoff
5854 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
5856 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
5857 phdr_included
= TRUE
;
5860 if (map
->includes_filehdr
&& first_section
)
5861 /* We need to keep the space used by the headers fixed. */
5862 map
->header_size
= first_section
->vma
- segment
->p_vaddr
;
5864 if (!map
->includes_phdrs
5865 && !map
->includes_filehdr
5866 && map
->p_paddr_valid
)
5867 /* There is some other padding before the first section. */
5868 map
->p_vaddr_offset
= ((lowest_section
? lowest_section
->lma
: 0)
5869 - segment
->p_paddr
);
5871 if (section_count
!= 0)
5873 unsigned int isec
= 0;
5875 for (section
= first_section
;
5877 section
= section
->next
)
5879 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5880 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5882 map
->sections
[isec
++] = section
->output_section
;
5883 if (isec
== section_count
)
5889 map
->count
= section_count
;
5890 *pointer_to_map
= map
;
5891 pointer_to_map
= &map
->next
;
5894 elf_tdata (obfd
)->segment_map
= map_first
;
5898 /* Copy private BFD data. This copies or rewrites ELF program header
5902 copy_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
5904 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5905 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5908 if (elf_tdata (ibfd
)->phdr
== NULL
)
5911 if (ibfd
->xvec
== obfd
->xvec
)
5913 /* Check to see if any sections in the input BFD
5914 covered by ELF program header have changed. */
5915 Elf_Internal_Phdr
*segment
;
5916 asection
*section
, *osec
;
5917 unsigned int i
, num_segments
;
5918 Elf_Internal_Shdr
*this_hdr
;
5919 const struct elf_backend_data
*bed
;
5921 bed
= get_elf_backend_data (ibfd
);
5923 /* Regenerate the segment map if p_paddr is set to 0. */
5924 if (bed
->want_p_paddr_set_to_zero
)
5927 /* Initialize the segment mark field. */
5928 for (section
= obfd
->sections
; section
!= NULL
;
5929 section
= section
->next
)
5930 section
->segment_mark
= FALSE
;
5932 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
5933 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
5937 /* PR binutils/3535. The Solaris linker always sets the p_paddr
5938 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
5939 which severly confuses things, so always regenerate the segment
5940 map in this case. */
5941 if (segment
->p_paddr
== 0
5942 && segment
->p_memsz
== 0
5943 && (segment
->p_type
== PT_INTERP
|| segment
->p_type
== PT_DYNAMIC
))
5946 for (section
= ibfd
->sections
;
5947 section
!= NULL
; section
= section
->next
)
5949 /* We mark the output section so that we know it comes
5950 from the input BFD. */
5951 osec
= section
->output_section
;
5953 osec
->segment_mark
= TRUE
;
5955 /* Check if this section is covered by the segment. */
5956 this_hdr
= &(elf_section_data(section
)->this_hdr
);
5957 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr
, segment
))
5959 /* FIXME: Check if its output section is changed or
5960 removed. What else do we need to check? */
5962 || section
->flags
!= osec
->flags
5963 || section
->lma
!= osec
->lma
5964 || section
->vma
!= osec
->vma
5965 || section
->size
!= osec
->size
5966 || section
->rawsize
!= osec
->rawsize
5967 || section
->alignment_power
!= osec
->alignment_power
)
5973 /* Check to see if any output section do not come from the
5975 for (section
= obfd
->sections
; section
!= NULL
;
5976 section
= section
->next
)
5978 if (section
->segment_mark
== FALSE
)
5981 section
->segment_mark
= FALSE
;
5984 return copy_elf_program_header (ibfd
, obfd
);
5988 return rewrite_elf_program_header (ibfd
, obfd
);
5991 /* Initialize private output section information from input section. */
5994 _bfd_elf_init_private_section_data (bfd
*ibfd
,
5998 struct bfd_link_info
*link_info
)
6001 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6002 bfd_boolean need_group
= link_info
== NULL
|| link_info
->relocatable
;
6004 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6005 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6008 /* Don't copy the output ELF section type from input if the
6009 output BFD section flags have been set to something different.
6010 elf_fake_sections will set ELF section type based on BFD
6012 if (elf_section_type (osec
) == SHT_NULL
6013 && (osec
->flags
== isec
->flags
|| !osec
->flags
))
6014 elf_section_type (osec
) = elf_section_type (isec
);
6016 /* FIXME: Is this correct for all OS/PROC specific flags? */
6017 elf_section_flags (osec
) |= (elf_section_flags (isec
)
6018 & (SHF_MASKOS
| SHF_MASKPROC
));
6020 /* Set things up for objcopy and relocatable link. The output
6021 SHT_GROUP section will have its elf_next_in_group pointing back
6022 to the input group members. Ignore linker created group section.
6023 See elfNN_ia64_object_p in elfxx-ia64.c. */
6026 if (elf_sec_group (isec
) == NULL
6027 || (elf_sec_group (isec
)->flags
& SEC_LINKER_CREATED
) == 0)
6029 if (elf_section_flags (isec
) & SHF_GROUP
)
6030 elf_section_flags (osec
) |= SHF_GROUP
;
6031 elf_next_in_group (osec
) = elf_next_in_group (isec
);
6032 elf_section_data (osec
)->group
= elf_section_data (isec
)->group
;
6036 ihdr
= &elf_section_data (isec
)->this_hdr
;
6038 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6039 don't use the output section of the linked-to section since it
6040 may be NULL at this point. */
6041 if ((ihdr
->sh_flags
& SHF_LINK_ORDER
) != 0)
6043 ohdr
= &elf_section_data (osec
)->this_hdr
;
6044 ohdr
->sh_flags
|= SHF_LINK_ORDER
;
6045 elf_linked_to_section (osec
) = elf_linked_to_section (isec
);
6048 osec
->use_rela_p
= isec
->use_rela_p
;
6053 /* Copy private section information. This copies over the entsize
6054 field, and sometimes the info field. */
6057 _bfd_elf_copy_private_section_data (bfd
*ibfd
,
6062 Elf_Internal_Shdr
*ihdr
, *ohdr
;
6064 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
6065 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6068 ihdr
= &elf_section_data (isec
)->this_hdr
;
6069 ohdr
= &elf_section_data (osec
)->this_hdr
;
6071 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
6073 if (ihdr
->sh_type
== SHT_SYMTAB
6074 || ihdr
->sh_type
== SHT_DYNSYM
6075 || ihdr
->sh_type
== SHT_GNU_verneed
6076 || ihdr
->sh_type
== SHT_GNU_verdef
)
6077 ohdr
->sh_info
= ihdr
->sh_info
;
6079 return _bfd_elf_init_private_section_data (ibfd
, isec
, obfd
, osec
,
6083 /* Copy private header information. */
6086 _bfd_elf_copy_private_header_data (bfd
*ibfd
, bfd
*obfd
)
6090 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6091 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6094 /* Copy over private BFD data if it has not already been copied.
6095 This must be done here, rather than in the copy_private_bfd_data
6096 entry point, because the latter is called after the section
6097 contents have been set, which means that the program headers have
6098 already been worked out. */
6099 if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
6101 if (! copy_private_bfd_data (ibfd
, obfd
))
6105 /* _bfd_elf_copy_private_section_data copied over the SHF_GROUP flag
6106 but this might be wrong if we deleted the group section. */
6107 for (isec
= ibfd
->sections
; isec
!= NULL
; isec
= isec
->next
)
6108 if (elf_section_type (isec
) == SHT_GROUP
6109 && isec
->output_section
== NULL
)
6111 asection
*first
= elf_next_in_group (isec
);
6112 asection
*s
= first
;
6115 if (s
->output_section
!= NULL
)
6117 elf_section_flags (s
->output_section
) &= ~SHF_GROUP
;
6118 elf_group_name (s
->output_section
) = NULL
;
6120 s
= elf_next_in_group (s
);
6129 /* Copy private symbol information. If this symbol is in a section
6130 which we did not map into a BFD section, try to map the section
6131 index correctly. We use special macro definitions for the mapped
6132 section indices; these definitions are interpreted by the
6133 swap_out_syms function. */
6135 #define MAP_ONESYMTAB (SHN_HIOS + 1)
6136 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
6137 #define MAP_STRTAB (SHN_HIOS + 3)
6138 #define MAP_SHSTRTAB (SHN_HIOS + 4)
6139 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
6142 _bfd_elf_copy_private_symbol_data (bfd
*ibfd
,
6147 elf_symbol_type
*isym
, *osym
;
6149 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
6150 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
6153 isym
= elf_symbol_from (ibfd
, isymarg
);
6154 osym
= elf_symbol_from (obfd
, osymarg
);
6157 && isym
->internal_elf_sym
.st_shndx
!= 0
6159 && bfd_is_abs_section (isym
->symbol
.section
))
6163 shndx
= isym
->internal_elf_sym
.st_shndx
;
6164 if (shndx
== elf_onesymtab (ibfd
))
6165 shndx
= MAP_ONESYMTAB
;
6166 else if (shndx
== elf_dynsymtab (ibfd
))
6167 shndx
= MAP_DYNSYMTAB
;
6168 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
6170 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
6171 shndx
= MAP_SHSTRTAB
;
6172 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
6173 shndx
= MAP_SYM_SHNDX
;
6174 osym
->internal_elf_sym
.st_shndx
= shndx
;
6180 /* Swap out the symbols. */
6183 swap_out_syms (bfd
*abfd
,
6184 struct bfd_strtab_hash
**sttp
,
6187 const struct elf_backend_data
*bed
;
6190 struct bfd_strtab_hash
*stt
;
6191 Elf_Internal_Shdr
*symtab_hdr
;
6192 Elf_Internal_Shdr
*symtab_shndx_hdr
;
6193 Elf_Internal_Shdr
*symstrtab_hdr
;
6194 bfd_byte
*outbound_syms
;
6195 bfd_byte
*outbound_shndx
;
6198 bfd_boolean name_local_sections
;
6200 if (!elf_map_symbols (abfd
))
6203 /* Dump out the symtabs. */
6204 stt
= _bfd_elf_stringtab_init ();
6208 bed
= get_elf_backend_data (abfd
);
6209 symcount
= bfd_get_symcount (abfd
);
6210 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6211 symtab_hdr
->sh_type
= SHT_SYMTAB
;
6212 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
6213 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
6214 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
6215 symtab_hdr
->sh_addralign
= (bfd_vma
) 1 << bed
->s
->log_file_align
;
6217 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
6218 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6220 outbound_syms
= bfd_alloc2 (abfd
, 1 + symcount
, bed
->s
->sizeof_sym
);
6221 if (outbound_syms
== NULL
)
6223 _bfd_stringtab_free (stt
);
6226 symtab_hdr
->contents
= outbound_syms
;
6228 outbound_shndx
= NULL
;
6229 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
6230 if (symtab_shndx_hdr
->sh_name
!= 0)
6232 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
6233 outbound_shndx
= bfd_zalloc2 (abfd
, 1 + symcount
,
6234 sizeof (Elf_External_Sym_Shndx
));
6235 if (outbound_shndx
== NULL
)
6237 _bfd_stringtab_free (stt
);
6241 symtab_shndx_hdr
->contents
= outbound_shndx
;
6242 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
6243 symtab_shndx_hdr
->sh_size
= amt
;
6244 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
6245 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
6248 /* Now generate the data (for "contents"). */
6250 /* Fill in zeroth symbol and swap it out. */
6251 Elf_Internal_Sym sym
;
6257 sym
.st_shndx
= SHN_UNDEF
;
6258 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6259 outbound_syms
+= bed
->s
->sizeof_sym
;
6260 if (outbound_shndx
!= NULL
)
6261 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6265 = (bed
->elf_backend_name_local_section_symbols
6266 && bed
->elf_backend_name_local_section_symbols (abfd
));
6268 syms
= bfd_get_outsymbols (abfd
);
6269 for (idx
= 0; idx
< symcount
; idx
++)
6271 Elf_Internal_Sym sym
;
6272 bfd_vma value
= syms
[idx
]->value
;
6273 elf_symbol_type
*type_ptr
;
6274 flagword flags
= syms
[idx
]->flags
;
6277 if (!name_local_sections
6278 && (flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
6280 /* Local section symbols have no name. */
6285 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
6288 if (sym
.st_name
== (unsigned long) -1)
6290 _bfd_stringtab_free (stt
);
6295 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
6297 if ((flags
& BSF_SECTION_SYM
) == 0
6298 && bfd_is_com_section (syms
[idx
]->section
))
6300 /* ELF common symbols put the alignment into the `value' field,
6301 and the size into the `size' field. This is backwards from
6302 how BFD handles it, so reverse it here. */
6303 sym
.st_size
= value
;
6304 if (type_ptr
== NULL
6305 || type_ptr
->internal_elf_sym
.st_value
== 0)
6306 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
6308 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
6309 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
6310 (abfd
, syms
[idx
]->section
);
6314 asection
*sec
= syms
[idx
]->section
;
6317 if (sec
->output_section
)
6319 value
+= sec
->output_offset
;
6320 sec
= sec
->output_section
;
6323 /* Don't add in the section vma for relocatable output. */
6324 if (! relocatable_p
)
6326 sym
.st_value
= value
;
6327 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
6329 if (bfd_is_abs_section (sec
)
6331 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
6333 /* This symbol is in a real ELF section which we did
6334 not create as a BFD section. Undo the mapping done
6335 by copy_private_symbol_data. */
6336 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
6340 shndx
= elf_onesymtab (abfd
);
6343 shndx
= elf_dynsymtab (abfd
);
6346 shndx
= elf_tdata (abfd
)->strtab_section
;
6349 shndx
= elf_tdata (abfd
)->shstrtab_section
;
6352 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
6360 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
6362 if (shndx
== SHN_BAD
)
6366 /* Writing this would be a hell of a lot easier if
6367 we had some decent documentation on bfd, and
6368 knew what to expect of the library, and what to
6369 demand of applications. For example, it
6370 appears that `objcopy' might not set the
6371 section of a symbol to be a section that is
6372 actually in the output file. */
6373 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
6376 _bfd_error_handler (_("\
6377 Unable to find equivalent output section for symbol '%s' from section '%s'"),
6378 syms
[idx
]->name
? syms
[idx
]->name
: "<Local sym>",
6380 bfd_set_error (bfd_error_invalid_operation
);
6381 _bfd_stringtab_free (stt
);
6385 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
6386 BFD_ASSERT (shndx
!= SHN_BAD
);
6390 sym
.st_shndx
= shndx
;
6393 if ((flags
& BSF_THREAD_LOCAL
) != 0)
6395 else if ((flags
& BSF_FUNCTION
) != 0)
6397 else if ((flags
& BSF_OBJECT
) != 0)
6399 else if ((flags
& BSF_RELC
) != 0)
6401 else if ((flags
& BSF_SRELC
) != 0)
6406 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
6409 /* Processor-specific types. */
6410 if (type_ptr
!= NULL
6411 && bed
->elf_backend_get_symbol_type
)
6412 type
= ((*bed
->elf_backend_get_symbol_type
)
6413 (&type_ptr
->internal_elf_sym
, type
));
6415 if (flags
& BSF_SECTION_SYM
)
6417 if (flags
& BSF_GLOBAL
)
6418 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
6420 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
6422 else if (bfd_is_com_section (syms
[idx
]->section
))
6424 #ifdef USE_STT_COMMON
6425 if (type
== STT_OBJECT
)
6426 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_COMMON
);
6429 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
6431 else if (bfd_is_und_section (syms
[idx
]->section
))
6432 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
6436 else if (flags
& BSF_FILE
)
6437 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
6440 int bind
= STB_LOCAL
;
6442 if (flags
& BSF_LOCAL
)
6444 else if (flags
& BSF_WEAK
)
6446 else if (flags
& BSF_GLOBAL
)
6449 sym
.st_info
= ELF_ST_INFO (bind
, type
);
6452 if (type_ptr
!= NULL
)
6453 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
6457 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
6458 outbound_syms
+= bed
->s
->sizeof_sym
;
6459 if (outbound_shndx
!= NULL
)
6460 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
6464 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
6465 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
6467 symstrtab_hdr
->sh_flags
= 0;
6468 symstrtab_hdr
->sh_addr
= 0;
6469 symstrtab_hdr
->sh_entsize
= 0;
6470 symstrtab_hdr
->sh_link
= 0;
6471 symstrtab_hdr
->sh_info
= 0;
6472 symstrtab_hdr
->sh_addralign
= 1;
6477 /* Return the number of bytes required to hold the symtab vector.
6479 Note that we base it on the count plus 1, since we will null terminate
6480 the vector allocated based on this size. However, the ELF symbol table
6481 always has a dummy entry as symbol #0, so it ends up even. */
6484 _bfd_elf_get_symtab_upper_bound (bfd
*abfd
)
6488 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
6490 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6491 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6493 symtab_size
-= sizeof (asymbol
*);
6499 _bfd_elf_get_dynamic_symtab_upper_bound (bfd
*abfd
)
6503 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
6505 if (elf_dynsymtab (abfd
) == 0)
6507 bfd_set_error (bfd_error_invalid_operation
);
6511 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
6512 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
6514 symtab_size
-= sizeof (asymbol
*);
6520 _bfd_elf_get_reloc_upper_bound (bfd
*abfd ATTRIBUTE_UNUSED
,
6523 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
6526 /* Canonicalize the relocs. */
6529 _bfd_elf_canonicalize_reloc (bfd
*abfd
,
6536 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6538 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, FALSE
))
6541 tblptr
= section
->relocation
;
6542 for (i
= 0; i
< section
->reloc_count
; i
++)
6543 *relptr
++ = tblptr
++;
6547 return section
->reloc_count
;
6551 _bfd_elf_canonicalize_symtab (bfd
*abfd
, asymbol
**allocation
)
6553 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6554 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, FALSE
);
6557 bfd_get_symcount (abfd
) = symcount
;
6562 _bfd_elf_canonicalize_dynamic_symtab (bfd
*abfd
,
6563 asymbol
**allocation
)
6565 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6566 long symcount
= bed
->s
->slurp_symbol_table (abfd
, allocation
, TRUE
);
6569 bfd_get_dynamic_symcount (abfd
) = symcount
;
6573 /* Return the size required for the dynamic reloc entries. Any loadable
6574 section that was actually installed in the BFD, and has type SHT_REL
6575 or SHT_RELA, and uses the dynamic symbol table, is considered to be a
6576 dynamic reloc section. */
6579 _bfd_elf_get_dynamic_reloc_upper_bound (bfd
*abfd
)
6584 if (elf_dynsymtab (abfd
) == 0)
6586 bfd_set_error (bfd_error_invalid_operation
);
6590 ret
= sizeof (arelent
*);
6591 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6592 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6593 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6594 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6595 ret
+= ((s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
6596 * sizeof (arelent
*));
6601 /* Canonicalize the dynamic relocation entries. Note that we return the
6602 dynamic relocations as a single block, although they are actually
6603 associated with particular sections; the interface, which was
6604 designed for SunOS style shared libraries, expects that there is only
6605 one set of dynamic relocs. Any loadable section that was actually
6606 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
6607 dynamic symbol table, is considered to be a dynamic reloc section. */
6610 _bfd_elf_canonicalize_dynamic_reloc (bfd
*abfd
,
6614 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
6618 if (elf_dynsymtab (abfd
) == 0)
6620 bfd_set_error (bfd_error_invalid_operation
);
6624 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
6626 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
6628 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
6629 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
6630 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
6635 if (! (*slurp_relocs
) (abfd
, s
, syms
, TRUE
))
6637 count
= s
->size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
6639 for (i
= 0; i
< count
; i
++)
6650 /* Read in the version information. */
6653 _bfd_elf_slurp_version_tables (bfd
*abfd
, bfd_boolean default_imported_symver
)
6655 bfd_byte
*contents
= NULL
;
6656 unsigned int freeidx
= 0;
6658 if (elf_dynverref (abfd
) != 0)
6660 Elf_Internal_Shdr
*hdr
;
6661 Elf_External_Verneed
*everneed
;
6662 Elf_Internal_Verneed
*iverneed
;
6664 bfd_byte
*contents_end
;
6666 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
6668 elf_tdata (abfd
)->verref
= bfd_zalloc2 (abfd
, hdr
->sh_info
,
6669 sizeof (Elf_Internal_Verneed
));
6670 if (elf_tdata (abfd
)->verref
== NULL
)
6673 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
6675 contents
= bfd_malloc (hdr
->sh_size
);
6676 if (contents
== NULL
)
6678 error_return_verref
:
6679 elf_tdata (abfd
)->verref
= NULL
;
6680 elf_tdata (abfd
)->cverrefs
= 0;
6683 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6684 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6685 goto error_return_verref
;
6687 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verneed
))
6688 goto error_return_verref
;
6690 BFD_ASSERT (sizeof (Elf_External_Verneed
)
6691 == sizeof (Elf_External_Vernaux
));
6692 contents_end
= contents
+ hdr
->sh_size
- sizeof (Elf_External_Verneed
);
6693 everneed
= (Elf_External_Verneed
*) contents
;
6694 iverneed
= elf_tdata (abfd
)->verref
;
6695 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
6697 Elf_External_Vernaux
*evernaux
;
6698 Elf_Internal_Vernaux
*ivernaux
;
6701 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
6703 iverneed
->vn_bfd
= abfd
;
6705 iverneed
->vn_filename
=
6706 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6708 if (iverneed
->vn_filename
== NULL
)
6709 goto error_return_verref
;
6711 if (iverneed
->vn_cnt
== 0)
6712 iverneed
->vn_auxptr
= NULL
;
6715 iverneed
->vn_auxptr
= bfd_alloc2 (abfd
, iverneed
->vn_cnt
,
6716 sizeof (Elf_Internal_Vernaux
));
6717 if (iverneed
->vn_auxptr
== NULL
)
6718 goto error_return_verref
;
6721 if (iverneed
->vn_aux
6722 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6723 goto error_return_verref
;
6725 evernaux
= ((Elf_External_Vernaux
*)
6726 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
6727 ivernaux
= iverneed
->vn_auxptr
;
6728 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
6730 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
6732 ivernaux
->vna_nodename
=
6733 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6734 ivernaux
->vna_name
);
6735 if (ivernaux
->vna_nodename
== NULL
)
6736 goto error_return_verref
;
6738 if (j
+ 1 < iverneed
->vn_cnt
)
6739 ivernaux
->vna_nextptr
= ivernaux
+ 1;
6741 ivernaux
->vna_nextptr
= NULL
;
6743 if (ivernaux
->vna_next
6744 > (size_t) (contents_end
- (bfd_byte
*) evernaux
))
6745 goto error_return_verref
;
6747 evernaux
= ((Elf_External_Vernaux
*)
6748 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
6750 if (ivernaux
->vna_other
> freeidx
)
6751 freeidx
= ivernaux
->vna_other
;
6754 if (i
+ 1 < hdr
->sh_info
)
6755 iverneed
->vn_nextref
= iverneed
+ 1;
6757 iverneed
->vn_nextref
= NULL
;
6759 if (iverneed
->vn_next
6760 > (size_t) (contents_end
- (bfd_byte
*) everneed
))
6761 goto error_return_verref
;
6763 everneed
= ((Elf_External_Verneed
*)
6764 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
6771 if (elf_dynverdef (abfd
) != 0)
6773 Elf_Internal_Shdr
*hdr
;
6774 Elf_External_Verdef
*everdef
;
6775 Elf_Internal_Verdef
*iverdef
;
6776 Elf_Internal_Verdef
*iverdefarr
;
6777 Elf_Internal_Verdef iverdefmem
;
6779 unsigned int maxidx
;
6780 bfd_byte
*contents_end_def
, *contents_end_aux
;
6782 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
6784 contents
= bfd_malloc (hdr
->sh_size
);
6785 if (contents
== NULL
)
6787 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
6788 || bfd_bread (contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
6791 if (hdr
->sh_info
&& hdr
->sh_size
< sizeof (Elf_External_Verdef
))
6794 BFD_ASSERT (sizeof (Elf_External_Verdef
)
6795 >= sizeof (Elf_External_Verdaux
));
6796 contents_end_def
= contents
+ hdr
->sh_size
6797 - sizeof (Elf_External_Verdef
);
6798 contents_end_aux
= contents
+ hdr
->sh_size
6799 - sizeof (Elf_External_Verdaux
);
6801 /* We know the number of entries in the section but not the maximum
6802 index. Therefore we have to run through all entries and find
6804 everdef
= (Elf_External_Verdef
*) contents
;
6806 for (i
= 0; i
< hdr
->sh_info
; ++i
)
6808 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6810 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
6811 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
6813 if (iverdefmem
.vd_next
6814 > (size_t) (contents_end_def
- (bfd_byte
*) everdef
))
6817 everdef
= ((Elf_External_Verdef
*)
6818 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
6821 if (default_imported_symver
)
6823 if (freeidx
> maxidx
)
6828 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, maxidx
,
6829 sizeof (Elf_Internal_Verdef
));
6830 if (elf_tdata (abfd
)->verdef
== NULL
)
6833 elf_tdata (abfd
)->cverdefs
= maxidx
;
6835 everdef
= (Elf_External_Verdef
*) contents
;
6836 iverdefarr
= elf_tdata (abfd
)->verdef
;
6837 for (i
= 0; i
< hdr
->sh_info
; i
++)
6839 Elf_External_Verdaux
*everdaux
;
6840 Elf_Internal_Verdaux
*iverdaux
;
6843 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
6845 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) == 0)
6847 error_return_verdef
:
6848 elf_tdata (abfd
)->verdef
= NULL
;
6849 elf_tdata (abfd
)->cverdefs
= 0;
6853 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
6854 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
6856 iverdef
->vd_bfd
= abfd
;
6858 if (iverdef
->vd_cnt
== 0)
6859 iverdef
->vd_auxptr
= NULL
;
6862 iverdef
->vd_auxptr
= bfd_alloc2 (abfd
, iverdef
->vd_cnt
,
6863 sizeof (Elf_Internal_Verdaux
));
6864 if (iverdef
->vd_auxptr
== NULL
)
6865 goto error_return_verdef
;
6869 > (size_t) (contents_end_aux
- (bfd_byte
*) everdef
))
6870 goto error_return_verdef
;
6872 everdaux
= ((Elf_External_Verdaux
*)
6873 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
6874 iverdaux
= iverdef
->vd_auxptr
;
6875 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
6877 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
6879 iverdaux
->vda_nodename
=
6880 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
6881 iverdaux
->vda_name
);
6882 if (iverdaux
->vda_nodename
== NULL
)
6883 goto error_return_verdef
;
6885 if (j
+ 1 < iverdef
->vd_cnt
)
6886 iverdaux
->vda_nextptr
= iverdaux
+ 1;
6888 iverdaux
->vda_nextptr
= NULL
;
6890 if (iverdaux
->vda_next
6891 > (size_t) (contents_end_aux
- (bfd_byte
*) everdaux
))
6892 goto error_return_verdef
;
6894 everdaux
= ((Elf_External_Verdaux
*)
6895 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
6898 if (iverdef
->vd_cnt
)
6899 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
6901 if ((size_t) (iverdef
- iverdefarr
) + 1 < maxidx
)
6902 iverdef
->vd_nextdef
= iverdef
+ 1;
6904 iverdef
->vd_nextdef
= NULL
;
6906 everdef
= ((Elf_External_Verdef
*)
6907 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
6913 else if (default_imported_symver
)
6920 elf_tdata (abfd
)->verdef
= bfd_zalloc2 (abfd
, freeidx
,
6921 sizeof (Elf_Internal_Verdef
));
6922 if (elf_tdata (abfd
)->verdef
== NULL
)
6925 elf_tdata (abfd
)->cverdefs
= freeidx
;
6928 /* Create a default version based on the soname. */
6929 if (default_imported_symver
)
6931 Elf_Internal_Verdef
*iverdef
;
6932 Elf_Internal_Verdaux
*iverdaux
;
6934 iverdef
= &elf_tdata (abfd
)->verdef
[freeidx
- 1];;
6936 iverdef
->vd_version
= VER_DEF_CURRENT
;
6937 iverdef
->vd_flags
= 0;
6938 iverdef
->vd_ndx
= freeidx
;
6939 iverdef
->vd_cnt
= 1;
6941 iverdef
->vd_bfd
= abfd
;
6943 iverdef
->vd_nodename
= bfd_elf_get_dt_soname (abfd
);
6944 if (iverdef
->vd_nodename
== NULL
)
6945 goto error_return_verdef
;
6946 iverdef
->vd_nextdef
= NULL
;
6947 iverdef
->vd_auxptr
= bfd_alloc (abfd
, sizeof (Elf_Internal_Verdaux
));
6948 if (iverdef
->vd_auxptr
== NULL
)
6949 goto error_return_verdef
;
6951 iverdaux
= iverdef
->vd_auxptr
;
6952 iverdaux
->vda_nodename
= iverdef
->vd_nodename
;
6953 iverdaux
->vda_nextptr
= NULL
;
6959 if (contents
!= NULL
)
6965 _bfd_elf_make_empty_symbol (bfd
*abfd
)
6967 elf_symbol_type
*newsym
;
6968 bfd_size_type amt
= sizeof (elf_symbol_type
);
6970 newsym
= bfd_zalloc (abfd
, amt
);
6975 newsym
->symbol
.the_bfd
= abfd
;
6976 return &newsym
->symbol
;
6981 _bfd_elf_get_symbol_info (bfd
*abfd ATTRIBUTE_UNUSED
,
6985 bfd_symbol_info (symbol
, ret
);
6988 /* Return whether a symbol name implies a local symbol. Most targets
6989 use this function for the is_local_label_name entry point, but some
6993 _bfd_elf_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
6996 /* Normal local symbols start with ``.L''. */
6997 if (name
[0] == '.' && name
[1] == 'L')
7000 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7001 DWARF debugging symbols starting with ``..''. */
7002 if (name
[0] == '.' && name
[1] == '.')
7005 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7006 emitting DWARF debugging output. I suspect this is actually a
7007 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7008 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7009 underscore to be emitted on some ELF targets). For ease of use,
7010 we treat such symbols as local. */
7011 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
7018 _bfd_elf_get_lineno (bfd
*abfd ATTRIBUTE_UNUSED
,
7019 asymbol
*symbol ATTRIBUTE_UNUSED
)
7026 _bfd_elf_set_arch_mach (bfd
*abfd
,
7027 enum bfd_architecture arch
,
7028 unsigned long machine
)
7030 /* If this isn't the right architecture for this backend, and this
7031 isn't the generic backend, fail. */
7032 if (arch
!= get_elf_backend_data (abfd
)->arch
7033 && arch
!= bfd_arch_unknown
7034 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
7037 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
7040 /* Find the function to a particular section and offset,
7041 for error reporting. */
7044 elf_find_function (bfd
*abfd
,
7048 const char **filename_ptr
,
7049 const char **functionname_ptr
)
7051 const char *filename
;
7052 asymbol
*func
, *file
;
7055 /* ??? Given multiple file symbols, it is impossible to reliably
7056 choose the right file name for global symbols. File symbols are
7057 local symbols, and thus all file symbols must sort before any
7058 global symbols. The ELF spec may be interpreted to say that a
7059 file symbol must sort before other local symbols, but currently
7060 ld -r doesn't do this. So, for ld -r output, it is possible to
7061 make a better choice of file name for local symbols by ignoring
7062 file symbols appearing after a given local symbol. */
7063 enum { nothing_seen
, symbol_seen
, file_after_symbol_seen
} state
;
7064 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7070 state
= nothing_seen
;
7072 for (p
= symbols
; *p
!= NULL
; p
++)
7077 q
= (elf_symbol_type
*) *p
;
7079 type
= ELF_ST_TYPE (q
->internal_elf_sym
.st_info
);
7084 if (state
== symbol_seen
)
7085 state
= file_after_symbol_seen
;
7088 if (!bed
->is_function_type (type
))
7091 if (bfd_get_section (&q
->symbol
) == section
7092 && q
->symbol
.value
>= low_func
7093 && q
->symbol
.value
<= offset
)
7095 func
= (asymbol
*) q
;
7096 low_func
= q
->symbol
.value
;
7099 && (ELF_ST_BIND (q
->internal_elf_sym
.st_info
) == STB_LOCAL
7100 || state
!= file_after_symbol_seen
))
7101 filename
= bfd_asymbol_name (file
);
7105 if (state
== nothing_seen
)
7106 state
= symbol_seen
;
7113 *filename_ptr
= filename
;
7114 if (functionname_ptr
)
7115 *functionname_ptr
= bfd_asymbol_name (func
);
7120 /* Find the nearest line to a particular section and offset,
7121 for error reporting. */
7124 _bfd_elf_find_nearest_line (bfd
*abfd
,
7128 const char **filename_ptr
,
7129 const char **functionname_ptr
,
7130 unsigned int *line_ptr
)
7134 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
7135 filename_ptr
, functionname_ptr
,
7138 if (!*functionname_ptr
)
7139 elf_find_function (abfd
, section
, symbols
, offset
,
7140 *filename_ptr
? NULL
: filename_ptr
,
7146 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
7147 filename_ptr
, functionname_ptr
,
7149 &elf_tdata (abfd
)->dwarf2_find_line_info
))
7151 if (!*functionname_ptr
)
7152 elf_find_function (abfd
, section
, symbols
, offset
,
7153 *filename_ptr
? NULL
: filename_ptr
,
7159 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
7160 &found
, filename_ptr
,
7161 functionname_ptr
, line_ptr
,
7162 &elf_tdata (abfd
)->line_info
))
7164 if (found
&& (*functionname_ptr
|| *line_ptr
))
7167 if (symbols
== NULL
)
7170 if (! elf_find_function (abfd
, section
, symbols
, offset
,
7171 filename_ptr
, functionname_ptr
))
7178 /* Find the line for a symbol. */
7181 _bfd_elf_find_line (bfd
*abfd
, asymbol
**symbols
, asymbol
*symbol
,
7182 const char **filename_ptr
, unsigned int *line_ptr
)
7184 return _bfd_dwarf2_find_line (abfd
, symbols
, symbol
,
7185 filename_ptr
, line_ptr
, 0,
7186 &elf_tdata (abfd
)->dwarf2_find_line_info
);
7189 /* After a call to bfd_find_nearest_line, successive calls to
7190 bfd_find_inliner_info can be used to get source information about
7191 each level of function inlining that terminated at the address
7192 passed to bfd_find_nearest_line. Currently this is only supported
7193 for DWARF2 with appropriate DWARF3 extensions. */
7196 _bfd_elf_find_inliner_info (bfd
*abfd
,
7197 const char **filename_ptr
,
7198 const char **functionname_ptr
,
7199 unsigned int *line_ptr
)
7202 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
7203 functionname_ptr
, line_ptr
,
7204 & elf_tdata (abfd
)->dwarf2_find_line_info
);
7209 _bfd_elf_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
7211 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7212 int ret
= bed
->s
->sizeof_ehdr
;
7214 if (!info
->relocatable
)
7216 bfd_size_type phdr_size
= elf_tdata (abfd
)->program_header_size
;
7218 if (phdr_size
== (bfd_size_type
) -1)
7220 struct elf_segment_map
*m
;
7223 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
7224 phdr_size
+= bed
->s
->sizeof_phdr
;
7227 phdr_size
= get_program_header_size (abfd
, info
);
7230 elf_tdata (abfd
)->program_header_size
= phdr_size
;
7238 _bfd_elf_set_section_contents (bfd
*abfd
,
7240 const void *location
,
7242 bfd_size_type count
)
7244 Elf_Internal_Shdr
*hdr
;
7247 if (! abfd
->output_has_begun
7248 && ! _bfd_elf_compute_section_file_positions (abfd
, NULL
))
7251 hdr
= &elf_section_data (section
)->this_hdr
;
7252 pos
= hdr
->sh_offset
+ offset
;
7253 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
7254 || bfd_bwrite (location
, count
, abfd
) != count
)
7261 _bfd_elf_no_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
7262 arelent
*cache_ptr ATTRIBUTE_UNUSED
,
7263 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
)
7268 /* Try to convert a non-ELF reloc into an ELF one. */
7271 _bfd_elf_validate_reloc (bfd
*abfd
, arelent
*areloc
)
7273 /* Check whether we really have an ELF howto. */
7275 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
7277 bfd_reloc_code_real_type code
;
7278 reloc_howto_type
*howto
;
7280 /* Alien reloc: Try to determine its type to replace it with an
7281 equivalent ELF reloc. */
7283 if (areloc
->howto
->pc_relative
)
7285 switch (areloc
->howto
->bitsize
)
7288 code
= BFD_RELOC_8_PCREL
;
7291 code
= BFD_RELOC_12_PCREL
;
7294 code
= BFD_RELOC_16_PCREL
;
7297 code
= BFD_RELOC_24_PCREL
;
7300 code
= BFD_RELOC_32_PCREL
;
7303 code
= BFD_RELOC_64_PCREL
;
7309 howto
= bfd_reloc_type_lookup (abfd
, code
);
7311 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
7313 if (howto
->pcrel_offset
)
7314 areloc
->addend
+= areloc
->address
;
7316 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
7321 switch (areloc
->howto
->bitsize
)
7327 code
= BFD_RELOC_14
;
7330 code
= BFD_RELOC_16
;
7333 code
= BFD_RELOC_26
;
7336 code
= BFD_RELOC_32
;
7339 code
= BFD_RELOC_64
;
7345 howto
= bfd_reloc_type_lookup (abfd
, code
);
7349 areloc
->howto
= howto
;
7357 (*_bfd_error_handler
)
7358 (_("%B: unsupported relocation type %s"),
7359 abfd
, areloc
->howto
->name
);
7360 bfd_set_error (bfd_error_bad_value
);
7365 _bfd_elf_close_and_cleanup (bfd
*abfd
)
7367 if (bfd_get_format (abfd
) == bfd_object
)
7369 if (elf_tdata (abfd
) != NULL
&& elf_shstrtab (abfd
) != NULL
)
7370 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
7371 _bfd_dwarf2_cleanup_debug_info (abfd
);
7374 return _bfd_generic_close_and_cleanup (abfd
);
7377 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7378 in the relocation's offset. Thus we cannot allow any sort of sanity
7379 range-checking to interfere. There is nothing else to do in processing
7382 bfd_reloc_status_type
7383 _bfd_elf_rel_vtable_reloc_fn
7384 (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*re ATTRIBUTE_UNUSED
,
7385 struct bfd_symbol
*symbol ATTRIBUTE_UNUSED
,
7386 void *data ATTRIBUTE_UNUSED
, asection
*is ATTRIBUTE_UNUSED
,
7387 bfd
*obfd ATTRIBUTE_UNUSED
, char **errmsg ATTRIBUTE_UNUSED
)
7389 return bfd_reloc_ok
;
7392 /* Elf core file support. Much of this only works on native
7393 toolchains, since we rely on knowing the
7394 machine-dependent procfs structure in order to pick
7395 out details about the corefile. */
7397 #ifdef HAVE_SYS_PROCFS_H
7398 # include <sys/procfs.h>
7401 /* FIXME: this is kinda wrong, but it's what gdb wants. */
7404 elfcore_make_pid (bfd
*abfd
)
7406 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
7407 + (elf_tdata (abfd
)->core_pid
));
7410 /* If there isn't a section called NAME, make one, using
7411 data from SECT. Note, this function will generate a
7412 reference to NAME, so you shouldn't deallocate or
7416 elfcore_maybe_make_sect (bfd
*abfd
, char *name
, asection
*sect
)
7420 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
7423 sect2
= bfd_make_section_with_flags (abfd
, name
, sect
->flags
);
7427 sect2
->size
= sect
->size
;
7428 sect2
->filepos
= sect
->filepos
;
7429 sect2
->alignment_power
= sect
->alignment_power
;
7433 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
7434 actually creates up to two pseudosections:
7435 - For the single-threaded case, a section named NAME, unless
7436 such a section already exists.
7437 - For the multi-threaded case, a section named "NAME/PID", where
7438 PID is elfcore_make_pid (abfd).
7439 Both pseudosections have identical contents. */
7441 _bfd_elfcore_make_pseudosection (bfd
*abfd
,
7447 char *threaded_name
;
7451 /* Build the section name. */
7453 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
7454 len
= strlen (buf
) + 1;
7455 threaded_name
= bfd_alloc (abfd
, len
);
7456 if (threaded_name
== NULL
)
7458 memcpy (threaded_name
, buf
, len
);
7460 sect
= bfd_make_section_anyway_with_flags (abfd
, threaded_name
,
7465 sect
->filepos
= filepos
;
7466 sect
->alignment_power
= 2;
7468 return elfcore_maybe_make_sect (abfd
, name
, sect
);
7471 /* prstatus_t exists on:
7473 linux 2.[01] + glibc
7477 #if defined (HAVE_PRSTATUS_T)
7480 elfcore_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7485 if (note
->descsz
== sizeof (prstatus_t
))
7489 size
= sizeof (prstat
.pr_reg
);
7490 offset
= offsetof (prstatus_t
, pr_reg
);
7491 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7493 /* Do not overwrite the core signal if it
7494 has already been set by another thread. */
7495 if (elf_tdata (abfd
)->core_signal
== 0)
7496 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7497 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7499 /* pr_who exists on:
7502 pr_who doesn't exist on:
7505 #if defined (HAVE_PRSTATUS_T_PR_WHO)
7506 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7509 #if defined (HAVE_PRSTATUS32_T)
7510 else if (note
->descsz
== sizeof (prstatus32_t
))
7512 /* 64-bit host, 32-bit corefile */
7513 prstatus32_t prstat
;
7515 size
= sizeof (prstat
.pr_reg
);
7516 offset
= offsetof (prstatus32_t
, pr_reg
);
7517 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
7519 /* Do not overwrite the core signal if it
7520 has already been set by another thread. */
7521 if (elf_tdata (abfd
)->core_signal
== 0)
7522 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
7523 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
7525 /* pr_who exists on:
7528 pr_who doesn't exist on:
7531 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
7532 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
7535 #endif /* HAVE_PRSTATUS32_T */
7538 /* Fail - we don't know how to handle any other
7539 note size (ie. data object type). */
7543 /* Make a ".reg/999" section and a ".reg" section. */
7544 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
7545 size
, note
->descpos
+ offset
);
7547 #endif /* defined (HAVE_PRSTATUS_T) */
7549 /* Create a pseudosection containing the exact contents of NOTE. */
7551 elfcore_make_note_pseudosection (bfd
*abfd
,
7553 Elf_Internal_Note
*note
)
7555 return _bfd_elfcore_make_pseudosection (abfd
, name
,
7556 note
->descsz
, note
->descpos
);
7559 /* There isn't a consistent prfpregset_t across platforms,
7560 but it doesn't matter, because we don't have to pick this
7561 data structure apart. */
7564 elfcore_grok_prfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7566 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
7569 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
7570 type of NT_PRXFPREG. Just include the whole note's contents
7574 elfcore_grok_prxfpreg (bfd
*abfd
, Elf_Internal_Note
*note
)
7576 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
7580 elfcore_grok_ppc_vmx (bfd
*abfd
, Elf_Internal_Note
*note
)
7582 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vmx", note
);
7586 elfcore_grok_ppc_vsx (bfd
*abfd
, Elf_Internal_Note
*note
)
7588 return elfcore_make_note_pseudosection (abfd
, ".reg-ppc-vsx", note
);
7591 #if defined (HAVE_PRPSINFO_T)
7592 typedef prpsinfo_t elfcore_psinfo_t
;
7593 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
7594 typedef prpsinfo32_t elfcore_psinfo32_t
;
7598 #if defined (HAVE_PSINFO_T)
7599 typedef psinfo_t elfcore_psinfo_t
;
7600 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
7601 typedef psinfo32_t elfcore_psinfo32_t
;
7605 /* return a malloc'ed copy of a string at START which is at
7606 most MAX bytes long, possibly without a terminating '\0'.
7607 the copy will always have a terminating '\0'. */
7610 _bfd_elfcore_strndup (bfd
*abfd
, char *start
, size_t max
)
7613 char *end
= memchr (start
, '\0', max
);
7621 dups
= bfd_alloc (abfd
, len
+ 1);
7625 memcpy (dups
, start
, len
);
7631 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7633 elfcore_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
7635 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
7637 elfcore_psinfo_t psinfo
;
7639 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7641 elf_tdata (abfd
)->core_program
7642 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7643 sizeof (psinfo
.pr_fname
));
7645 elf_tdata (abfd
)->core_command
7646 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7647 sizeof (psinfo
.pr_psargs
));
7649 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
7650 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
7652 /* 64-bit host, 32-bit corefile */
7653 elfcore_psinfo32_t psinfo
;
7655 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
7657 elf_tdata (abfd
)->core_program
7658 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
7659 sizeof (psinfo
.pr_fname
));
7661 elf_tdata (abfd
)->core_command
7662 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
7663 sizeof (psinfo
.pr_psargs
));
7669 /* Fail - we don't know how to handle any other
7670 note size (ie. data object type). */
7674 /* Note that for some reason, a spurious space is tacked
7675 onto the end of the args in some (at least one anyway)
7676 implementations, so strip it off if it exists. */
7679 char *command
= elf_tdata (abfd
)->core_command
;
7680 int n
= strlen (command
);
7682 if (0 < n
&& command
[n
- 1] == ' ')
7683 command
[n
- 1] = '\0';
7688 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
7690 #if defined (HAVE_PSTATUS_T)
7692 elfcore_grok_pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7694 if (note
->descsz
== sizeof (pstatus_t
)
7695 #if defined (HAVE_PXSTATUS_T)
7696 || note
->descsz
== sizeof (pxstatus_t
)
7702 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7704 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7706 #if defined (HAVE_PSTATUS32_T)
7707 else if (note
->descsz
== sizeof (pstatus32_t
))
7709 /* 64-bit host, 32-bit corefile */
7712 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
7714 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
7717 /* Could grab some more details from the "representative"
7718 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
7719 NT_LWPSTATUS note, presumably. */
7723 #endif /* defined (HAVE_PSTATUS_T) */
7725 #if defined (HAVE_LWPSTATUS_T)
7727 elfcore_grok_lwpstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7729 lwpstatus_t lwpstat
;
7735 if (note
->descsz
!= sizeof (lwpstat
)
7736 #if defined (HAVE_LWPXSTATUS_T)
7737 && note
->descsz
!= sizeof (lwpxstatus_t
)
7742 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
7744 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
7745 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
7747 /* Make a ".reg/999" section. */
7749 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
7750 len
= strlen (buf
) + 1;
7751 name
= bfd_alloc (abfd
, len
);
7754 memcpy (name
, buf
, len
);
7756 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7760 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7761 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
7762 sect
->filepos
= note
->descpos
7763 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
7766 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7767 sect
->size
= sizeof (lwpstat
.pr_reg
);
7768 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
7771 sect
->alignment_power
= 2;
7773 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7776 /* Make a ".reg2/999" section */
7778 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
7779 len
= strlen (buf
) + 1;
7780 name
= bfd_alloc (abfd
, len
);
7783 memcpy (name
, buf
, len
);
7785 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7789 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7790 sect
->size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
7791 sect
->filepos
= note
->descpos
7792 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
7795 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
7796 sect
->size
= sizeof (lwpstat
.pr_fpreg
);
7797 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
7800 sect
->alignment_power
= 2;
7802 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
7804 #endif /* defined (HAVE_LWPSTATUS_T) */
7807 elfcore_grok_win32pstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
7814 int is_active_thread
;
7817 if (note
->descsz
< 728)
7820 if (! CONST_STRNEQ (note
->namedata
, "win32"))
7823 type
= bfd_get_32 (abfd
, note
->descdata
);
7827 case 1 /* NOTE_INFO_PROCESS */:
7828 /* FIXME: need to add ->core_command. */
7829 /* process_info.pid */
7830 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7831 /* process_info.signal */
7832 elf_tdata (abfd
)->core_signal
= bfd_get_32 (abfd
, note
->descdata
+ 12);
7835 case 2 /* NOTE_INFO_THREAD */:
7836 /* Make a ".reg/999" section. */
7837 /* thread_info.tid */
7838 sprintf (buf
, ".reg/%ld", (long) bfd_get_32 (abfd
, note
->descdata
+ 8));
7840 len
= strlen (buf
) + 1;
7841 name
= bfd_alloc (abfd
, len
);
7845 memcpy (name
, buf
, len
);
7847 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7851 /* sizeof (thread_info.thread_context) */
7853 /* offsetof (thread_info.thread_context) */
7854 sect
->filepos
= note
->descpos
+ 12;
7855 sect
->alignment_power
= 2;
7857 /* thread_info.is_active_thread */
7858 is_active_thread
= bfd_get_32 (abfd
, note
->descdata
+ 8);
7860 if (is_active_thread
)
7861 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
7865 case 3 /* NOTE_INFO_MODULE */:
7866 /* Make a ".module/xxxxxxxx" section. */
7867 /* module_info.base_address */
7868 base_addr
= bfd_get_32 (abfd
, note
->descdata
+ 4);
7869 sprintf (buf
, ".module/%08lx", (unsigned long) base_addr
);
7871 len
= strlen (buf
) + 1;
7872 name
= bfd_alloc (abfd
, len
);
7876 memcpy (name
, buf
, len
);
7878 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
7883 sect
->size
= note
->descsz
;
7884 sect
->filepos
= note
->descpos
;
7885 sect
->alignment_power
= 2;
7896 elfcore_grok_note (bfd
*abfd
, Elf_Internal_Note
*note
)
7898 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
7906 if (bed
->elf_backend_grok_prstatus
)
7907 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
7909 #if defined (HAVE_PRSTATUS_T)
7910 return elfcore_grok_prstatus (abfd
, note
);
7915 #if defined (HAVE_PSTATUS_T)
7917 return elfcore_grok_pstatus (abfd
, note
);
7920 #if defined (HAVE_LWPSTATUS_T)
7922 return elfcore_grok_lwpstatus (abfd
, note
);
7925 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
7926 return elfcore_grok_prfpreg (abfd
, note
);
7928 case NT_WIN32PSTATUS
:
7929 return elfcore_grok_win32pstatus (abfd
, note
);
7931 case NT_PRXFPREG
: /* Linux SSE extension */
7932 if (note
->namesz
== 6
7933 && strcmp (note
->namedata
, "LINUX") == 0)
7934 return elfcore_grok_prxfpreg (abfd
, note
);
7939 if (note
->namesz
== 6
7940 && strcmp (note
->namedata
, "LINUX") == 0)
7941 return elfcore_grok_ppc_vmx (abfd
, note
);
7946 if (note
->namesz
== 6
7947 && strcmp (note
->namedata
, "LINUX") == 0)
7948 return elfcore_grok_ppc_vsx (abfd
, note
);
7954 if (bed
->elf_backend_grok_psinfo
)
7955 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
7957 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
7958 return elfcore_grok_psinfo (abfd
, note
);
7965 asection
*sect
= bfd_make_section_anyway_with_flags (abfd
, ".auxv",
7970 sect
->size
= note
->descsz
;
7971 sect
->filepos
= note
->descpos
;
7972 sect
->alignment_power
= 1 + bfd_get_arch_size (abfd
) / 32;
7980 elfobj_grok_gnu_build_id (bfd
*abfd
, Elf_Internal_Note
*note
)
7982 elf_tdata (abfd
)->build_id_size
= note
->descsz
;
7983 elf_tdata (abfd
)->build_id
= bfd_alloc (abfd
, note
->descsz
);
7984 if (elf_tdata (abfd
)->build_id
== NULL
)
7987 memcpy (elf_tdata (abfd
)->build_id
, note
->descdata
, note
->descsz
);
7993 elfobj_grok_gnu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8000 case NT_GNU_BUILD_ID
:
8001 return elfobj_grok_gnu_build_id (abfd
, note
);
8006 elfcore_netbsd_get_lwpid (Elf_Internal_Note
*note
, int *lwpidp
)
8010 cp
= strchr (note
->namedata
, '@');
8013 *lwpidp
= atoi(cp
+ 1);
8020 elfcore_grok_netbsd_procinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
8022 /* Signal number at offset 0x08. */
8023 elf_tdata (abfd
)->core_signal
8024 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
8026 /* Process ID at offset 0x50. */
8027 elf_tdata (abfd
)->core_pid
8028 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
8030 /* Command name at 0x7c (max 32 bytes, including nul). */
8031 elf_tdata (abfd
)->core_command
8032 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
8034 return elfcore_make_note_pseudosection (abfd
, ".note.netbsdcore.procinfo",
8039 elfcore_grok_netbsd_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8043 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
8044 elf_tdata (abfd
)->core_lwpid
= lwp
;
8046 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
8048 /* NetBSD-specific core "procinfo". Note that we expect to
8049 find this note before any of the others, which is fine,
8050 since the kernel writes this note out first when it
8051 creates a core file. */
8053 return elfcore_grok_netbsd_procinfo (abfd
, note
);
8056 /* As of Jan 2002 there are no other machine-independent notes
8057 defined for NetBSD core files. If the note type is less
8058 than the start of the machine-dependent note types, we don't
8061 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
8065 switch (bfd_get_arch (abfd
))
8067 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8068 PT_GETFPREGS == mach+2. */
8070 case bfd_arch_alpha
:
8071 case bfd_arch_sparc
:
8074 case NT_NETBSDCORE_FIRSTMACH
+0:
8075 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8077 case NT_NETBSDCORE_FIRSTMACH
+2:
8078 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8084 /* On all other arch's, PT_GETREGS == mach+1 and
8085 PT_GETFPREGS == mach+3. */
8090 case NT_NETBSDCORE_FIRSTMACH
+1:
8091 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
8093 case NT_NETBSDCORE_FIRSTMACH
+3:
8094 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
8104 elfcore_grok_nto_status (bfd
*abfd
, Elf_Internal_Note
*note
, long *tid
)
8106 void *ddata
= note
->descdata
;
8113 /* nto_procfs_status 'pid' field is at offset 0. */
8114 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
);
8116 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */
8117 *tid
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 4);
8119 /* nto_procfs_status 'flags' field is at offset 8. */
8120 flags
= bfd_get_32 (abfd
, (bfd_byte
*) ddata
+ 8);
8122 /* nto_procfs_status 'what' field is at offset 14. */
8123 if ((sig
= bfd_get_16 (abfd
, (bfd_byte
*) ddata
+ 14)) > 0)
8125 elf_tdata (abfd
)->core_signal
= sig
;
8126 elf_tdata (abfd
)->core_lwpid
= *tid
;
8129 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores
8130 do not come from signals so we make sure we set the current
8131 thread just in case. */
8132 if (flags
& 0x00000080)
8133 elf_tdata (abfd
)->core_lwpid
= *tid
;
8135 /* Make a ".qnx_core_status/%d" section. */
8136 sprintf (buf
, ".qnx_core_status/%ld", *tid
);
8138 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
8143 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8147 sect
->size
= note
->descsz
;
8148 sect
->filepos
= note
->descpos
;
8149 sect
->alignment_power
= 2;
8151 return (elfcore_maybe_make_sect (abfd
, ".qnx_core_status", sect
));
8155 elfcore_grok_nto_regs (bfd
*abfd
,
8156 Elf_Internal_Note
*note
,
8164 /* Make a "(base)/%d" section. */
8165 sprintf (buf
, "%s/%ld", base
, tid
);
8167 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
8172 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8176 sect
->size
= note
->descsz
;
8177 sect
->filepos
= note
->descpos
;
8178 sect
->alignment_power
= 2;
8180 /* This is the current thread. */
8181 if (elf_tdata (abfd
)->core_lwpid
== tid
)
8182 return elfcore_maybe_make_sect (abfd
, base
, sect
);
8187 #define BFD_QNT_CORE_INFO 7
8188 #define BFD_QNT_CORE_STATUS 8
8189 #define BFD_QNT_CORE_GREG 9
8190 #define BFD_QNT_CORE_FPREG 10
8193 elfcore_grok_nto_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8195 /* Every GREG section has a STATUS section before it. Store the
8196 tid from the previous call to pass down to the next gregs
8198 static long tid
= 1;
8202 case BFD_QNT_CORE_INFO
:
8203 return elfcore_make_note_pseudosection (abfd
, ".qnx_core_info", note
);
8204 case BFD_QNT_CORE_STATUS
:
8205 return elfcore_grok_nto_status (abfd
, note
, &tid
);
8206 case BFD_QNT_CORE_GREG
:
8207 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg");
8208 case BFD_QNT_CORE_FPREG
:
8209 return elfcore_grok_nto_regs (abfd
, note
, tid
, ".reg2");
8216 elfcore_grok_spu_note (bfd
*abfd
, Elf_Internal_Note
*note
)
8222 /* Use note name as section name. */
8224 name
= bfd_alloc (abfd
, len
);
8227 memcpy (name
, note
->namedata
, len
);
8228 name
[len
- 1] = '\0';
8230 sect
= bfd_make_section_anyway_with_flags (abfd
, name
, SEC_HAS_CONTENTS
);
8234 sect
->size
= note
->descsz
;
8235 sect
->filepos
= note
->descpos
;
8236 sect
->alignment_power
= 1;
8241 /* Function: elfcore_write_note
8244 buffer to hold note, and current size of buffer
8248 size of data for note
8250 Writes note to end of buffer. ELF64 notes are written exactly as
8251 for ELF32, despite the current (as of 2006) ELF gabi specifying
8252 that they ought to have 8-byte namesz and descsz field, and have
8253 8-byte alignment. Other writers, eg. Linux kernel, do the same.
8256 Pointer to realloc'd buffer, *BUFSIZ updated. */
8259 elfcore_write_note (bfd
*abfd
,
8267 Elf_External_Note
*xnp
;
8274 namesz
= strlen (name
) + 1;
8276 newspace
= 12 + ((namesz
+ 3) & -4) + ((size
+ 3) & -4);
8278 buf
= realloc (buf
, *bufsiz
+ newspace
);
8281 dest
= buf
+ *bufsiz
;
8282 *bufsiz
+= newspace
;
8283 xnp
= (Elf_External_Note
*) dest
;
8284 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
8285 H_PUT_32 (abfd
, size
, xnp
->descsz
);
8286 H_PUT_32 (abfd
, type
, xnp
->type
);
8290 memcpy (dest
, name
, namesz
);
8298 memcpy (dest
, input
, size
);
8308 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8310 elfcore_write_prpsinfo (bfd
*abfd
,
8316 const char *note_name
= "CORE";
8317 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8319 if (bed
->elf_backend_write_core_note
!= NULL
)
8322 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8323 NT_PRPSINFO
, fname
, psargs
);
8328 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8329 if (bed
->s
->elfclass
== ELFCLASS32
)
8331 #if defined (HAVE_PSINFO32_T)
8333 int note_type
= NT_PSINFO
;
8336 int note_type
= NT_PRPSINFO
;
8339 memset (&data
, 0, sizeof (data
));
8340 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8341 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8342 return elfcore_write_note (abfd
, buf
, bufsiz
,
8343 note_name
, note_type
, &data
, sizeof (data
));
8348 #if defined (HAVE_PSINFO_T)
8350 int note_type
= NT_PSINFO
;
8353 int note_type
= NT_PRPSINFO
;
8356 memset (&data
, 0, sizeof (data
));
8357 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
8358 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
8359 return elfcore_write_note (abfd
, buf
, bufsiz
,
8360 note_name
, note_type
, &data
, sizeof (data
));
8363 #endif /* PSINFO_T or PRPSINFO_T */
8365 #if defined (HAVE_PRSTATUS_T)
8367 elfcore_write_prstatus (bfd
*abfd
,
8374 const char *note_name
= "CORE";
8375 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8377 if (bed
->elf_backend_write_core_note
!= NULL
)
8380 ret
= (*bed
->elf_backend_write_core_note
) (abfd
, buf
, bufsiz
,
8382 pid
, cursig
, gregs
);
8387 #if defined (HAVE_PRSTATUS32_T)
8388 if (bed
->s
->elfclass
== ELFCLASS32
)
8390 prstatus32_t prstat
;
8392 memset (&prstat
, 0, sizeof (prstat
));
8393 prstat
.pr_pid
= pid
;
8394 prstat
.pr_cursig
= cursig
;
8395 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8396 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8397 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8404 memset (&prstat
, 0, sizeof (prstat
));
8405 prstat
.pr_pid
= pid
;
8406 prstat
.pr_cursig
= cursig
;
8407 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
8408 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8409 NT_PRSTATUS
, &prstat
, sizeof (prstat
));
8412 #endif /* HAVE_PRSTATUS_T */
8414 #if defined (HAVE_LWPSTATUS_T)
8416 elfcore_write_lwpstatus (bfd
*abfd
,
8423 lwpstatus_t lwpstat
;
8424 const char *note_name
= "CORE";
8426 memset (&lwpstat
, 0, sizeof (lwpstat
));
8427 lwpstat
.pr_lwpid
= pid
>> 16;
8428 lwpstat
.pr_cursig
= cursig
;
8429 #if defined (HAVE_LWPSTATUS_T_PR_REG)
8430 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
8431 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8433 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
8434 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
8436 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
8437 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
8440 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8441 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
8443 #endif /* HAVE_LWPSTATUS_T */
8445 #if defined (HAVE_PSTATUS_T)
8447 elfcore_write_pstatus (bfd
*abfd
,
8451 int cursig ATTRIBUTE_UNUSED
,
8452 const void *gregs ATTRIBUTE_UNUSED
)
8454 const char *note_name
= "CORE";
8455 #if defined (HAVE_PSTATUS32_T)
8456 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8458 if (bed
->s
->elfclass
== ELFCLASS32
)
8462 memset (&pstat
, 0, sizeof (pstat
));
8463 pstat
.pr_pid
= pid
& 0xffff;
8464 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8465 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8473 memset (&pstat
, 0, sizeof (pstat
));
8474 pstat
.pr_pid
= pid
& 0xffff;
8475 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
8476 NT_PSTATUS
, &pstat
, sizeof (pstat
));
8480 #endif /* HAVE_PSTATUS_T */
8483 elfcore_write_prfpreg (bfd
*abfd
,
8489 const char *note_name
= "CORE";
8490 return elfcore_write_note (abfd
, buf
, bufsiz
,
8491 note_name
, NT_FPREGSET
, fpregs
, size
);
8495 elfcore_write_prxfpreg (bfd
*abfd
,
8498 const void *xfpregs
,
8501 char *note_name
= "LINUX";
8502 return elfcore_write_note (abfd
, buf
, bufsiz
,
8503 note_name
, NT_PRXFPREG
, xfpregs
, size
);
8507 elfcore_write_ppc_vmx (bfd
*abfd
,
8510 const void *ppc_vmx
,
8513 char *note_name
= "LINUX";
8514 return elfcore_write_note (abfd
, buf
, bufsiz
,
8515 note_name
, NT_PPC_VMX
, ppc_vmx
, size
);
8519 elfcore_write_ppc_vsx (bfd
*abfd
,
8522 const void *ppc_vsx
,
8525 char *note_name
= "LINUX";
8526 return elfcore_write_note (abfd
, buf
, bufsiz
,
8527 note_name
, NT_PPC_VSX
, ppc_vsx
, size
);
8531 elfcore_write_register_note (bfd
*abfd
,
8534 const char *section
,
8538 if (strcmp (section
, ".reg2") == 0)
8539 return elfcore_write_prfpreg (abfd
, buf
, bufsiz
, data
, size
);
8540 if (strcmp (section
, ".reg-xfp") == 0)
8541 return elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, data
, size
);
8542 if (strcmp (section
, ".reg-ppc-vmx") == 0)
8543 return elfcore_write_ppc_vmx (abfd
, buf
, bufsiz
, data
, size
);
8544 if (strcmp (section
, ".reg-ppc-vsx") == 0)
8545 return elfcore_write_ppc_vsx (abfd
, buf
, bufsiz
, data
, size
);
8550 elf_parse_notes (bfd
*abfd
, char *buf
, size_t size
, file_ptr offset
)
8555 while (p
< buf
+ size
)
8557 /* FIXME: bad alignment assumption. */
8558 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
8559 Elf_Internal_Note in
;
8561 if (offsetof (Elf_External_Note
, name
) > buf
- p
+ size
)
8564 in
.type
= H_GET_32 (abfd
, xnp
->type
);
8566 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
8567 in
.namedata
= xnp
->name
;
8568 if (in
.namesz
> buf
- in
.namedata
+ size
)
8571 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
8572 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
8573 in
.descpos
= offset
+ (in
.descdata
- buf
);
8575 && (in
.descdata
>= buf
+ size
8576 || in
.descsz
> buf
- in
.descdata
+ size
))
8579 switch (bfd_get_format (abfd
))
8585 if (CONST_STRNEQ (in
.namedata
, "NetBSD-CORE"))
8587 if (! elfcore_grok_netbsd_note (abfd
, &in
))
8590 else if (CONST_STRNEQ (in
.namedata
, "QNX"))
8592 if (! elfcore_grok_nto_note (abfd
, &in
))
8595 else if (CONST_STRNEQ (in
.namedata
, "SPU/"))
8597 if (! elfcore_grok_spu_note (abfd
, &in
))
8602 if (! elfcore_grok_note (abfd
, &in
))
8608 if (in
.namesz
== sizeof "GNU" && strcmp (in
.namedata
, "GNU") == 0)
8610 if (! elfobj_grok_gnu_note (abfd
, &in
))
8616 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
8623 elf_read_notes (bfd
*abfd
, file_ptr offset
, bfd_size_type size
)
8630 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
8633 buf
= bfd_malloc (size
);
8637 if (bfd_bread (buf
, size
, abfd
) != size
8638 || !elf_parse_notes (abfd
, buf
, size
, offset
))
8648 /* Providing external access to the ELF program header table. */
8650 /* Return an upper bound on the number of bytes required to store a
8651 copy of ABFD's program header table entries. Return -1 if an error
8652 occurs; bfd_get_error will return an appropriate code. */
8655 bfd_get_elf_phdr_upper_bound (bfd
*abfd
)
8657 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8659 bfd_set_error (bfd_error_wrong_format
);
8663 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
8666 /* Copy ABFD's program header table entries to *PHDRS. The entries
8667 will be stored as an array of Elf_Internal_Phdr structures, as
8668 defined in include/elf/internal.h. To find out how large the
8669 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
8671 Return the number of program header table entries read, or -1 if an
8672 error occurs; bfd_get_error will return an appropriate code. */
8675 bfd_get_elf_phdrs (bfd
*abfd
, void *phdrs
)
8679 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
8681 bfd_set_error (bfd_error_wrong_format
);
8685 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
8686 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
8687 num_phdrs
* sizeof (Elf_Internal_Phdr
));
8692 enum elf_reloc_type_class
8693 _bfd_elf_reloc_type_class (const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
)
8695 return reloc_class_normal
;
8698 /* For RELA architectures, return the relocation value for a
8699 relocation against a local symbol. */
8702 _bfd_elf_rela_local_sym (bfd
*abfd
,
8703 Elf_Internal_Sym
*sym
,
8705 Elf_Internal_Rela
*rel
)
8707 asection
*sec
= *psec
;
8710 relocation
= (sec
->output_section
->vma
8711 + sec
->output_offset
8713 if ((sec
->flags
& SEC_MERGE
)
8714 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
8715 && sec
->sec_info_type
== ELF_INFO_TYPE_MERGE
)
8718 _bfd_merged_section_offset (abfd
, psec
,
8719 elf_section_data (sec
)->sec_info
,
8720 sym
->st_value
+ rel
->r_addend
);
8723 /* If we have changed the section, and our original section is
8724 marked with SEC_EXCLUDE, it means that the original
8725 SEC_MERGE section has been completely subsumed in some
8726 other SEC_MERGE section. In this case, we need to leave
8727 some info around for --emit-relocs. */
8728 if ((sec
->flags
& SEC_EXCLUDE
) != 0)
8729 sec
->kept_section
= *psec
;
8732 rel
->r_addend
-= relocation
;
8733 rel
->r_addend
+= sec
->output_section
->vma
+ sec
->output_offset
;
8739 _bfd_elf_rel_local_sym (bfd
*abfd
,
8740 Elf_Internal_Sym
*sym
,
8744 asection
*sec
= *psec
;
8746 if (sec
->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
8747 return sym
->st_value
+ addend
;
8749 return _bfd_merged_section_offset (abfd
, psec
,
8750 elf_section_data (sec
)->sec_info
,
8751 sym
->st_value
+ addend
);
8755 _bfd_elf_section_offset (bfd
*abfd
,
8756 struct bfd_link_info
*info
,
8760 switch (sec
->sec_info_type
)
8762 case ELF_INFO_TYPE_STABS
:
8763 return _bfd_stab_section_offset (sec
, elf_section_data (sec
)->sec_info
,
8765 case ELF_INFO_TYPE_EH_FRAME
:
8766 return _bfd_elf_eh_frame_section_offset (abfd
, info
, sec
, offset
);
8772 /* Create a new BFD as if by bfd_openr. Rather than opening a file,
8773 reconstruct an ELF file by reading the segments out of remote memory
8774 based on the ELF file header at EHDR_VMA and the ELF program headers it
8775 points to. If not null, *LOADBASEP is filled in with the difference
8776 between the VMAs from which the segments were read, and the VMAs the
8777 file headers (and hence BFD's idea of each section's VMA) put them at.
8779 The function TARGET_READ_MEMORY is called to copy LEN bytes from the
8780 remote memory at target address VMA into the local buffer at MYADDR; it
8781 should return zero on success or an `errno' code on failure. TEMPL must
8782 be a BFD for an ELF target with the word size and byte order found in
8783 the remote memory. */
8786 bfd_elf_bfd_from_remote_memory
8790 int (*target_read_memory
) (bfd_vma
, bfd_byte
*, int))
8792 return (*get_elf_backend_data (templ
)->elf_backend_bfd_from_remote_memory
)
8793 (templ
, ehdr_vma
, loadbasep
, target_read_memory
);
8797 _bfd_elf_get_synthetic_symtab (bfd
*abfd
,
8798 long symcount ATTRIBUTE_UNUSED
,
8799 asymbol
**syms ATTRIBUTE_UNUSED
,
8804 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
8807 const char *relplt_name
;
8808 bfd_boolean (*slurp_relocs
) (bfd
*, asection
*, asymbol
**, bfd_boolean
);
8812 Elf_Internal_Shdr
*hdr
;
8818 if ((abfd
->flags
& (DYNAMIC
| EXEC_P
)) == 0)
8821 if (dynsymcount
<= 0)
8824 if (!bed
->plt_sym_val
)
8827 relplt_name
= bed
->relplt_name
;
8828 if (relplt_name
== NULL
)
8829 relplt_name
= bed
->rela_plts_and_copies_p
? ".rela.plt" : ".rel.plt";
8830 relplt
= bfd_get_section_by_name (abfd
, relplt_name
);
8834 hdr
= &elf_section_data (relplt
)->this_hdr
;
8835 if (hdr
->sh_link
!= elf_dynsymtab (abfd
)
8836 || (hdr
->sh_type
!= SHT_REL
&& hdr
->sh_type
!= SHT_RELA
))
8839 plt
= bfd_get_section_by_name (abfd
, ".plt");
8843 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
8844 if (! (*slurp_relocs
) (abfd
, relplt
, dynsyms
, TRUE
))
8847 count
= relplt
->size
/ hdr
->sh_entsize
;
8848 size
= count
* sizeof (asymbol
);
8849 p
= relplt
->relocation
;
8850 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
8851 size
+= strlen ((*p
->sym_ptr_ptr
)->name
) + sizeof ("@plt");
8853 s
= *ret
= bfd_malloc (size
);
8857 names
= (char *) (s
+ count
);
8858 p
= relplt
->relocation
;
8860 for (i
= 0; i
< count
; i
++, p
+= bed
->s
->int_rels_per_ext_rel
)
8865 addr
= bed
->plt_sym_val (i
, plt
, p
);
8866 if (addr
== (bfd_vma
) -1)
8869 *s
= **p
->sym_ptr_ptr
;
8870 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
8871 we are defining a symbol, ensure one of them is set. */
8872 if ((s
->flags
& BSF_LOCAL
) == 0)
8873 s
->flags
|= BSF_GLOBAL
;
8874 s
->flags
|= BSF_SYNTHETIC
;
8876 s
->value
= addr
- plt
->vma
;
8879 len
= strlen ((*p
->sym_ptr_ptr
)->name
);
8880 memcpy (names
, (*p
->sym_ptr_ptr
)->name
, len
);
8882 memcpy (names
, "@plt", sizeof ("@plt"));
8883 names
+= sizeof ("@plt");
8890 /* It is only used by x86-64 so far. */
8891 asection _bfd_elf_large_com_section
8892 = BFD_FAKE_SECTION (_bfd_elf_large_com_section
,
8893 SEC_IS_COMMON
, NULL
, "LARGE_COMMON", 0);
8896 _bfd_elf_set_osabi (bfd
* abfd
,
8897 struct bfd_link_info
* link_info ATTRIBUTE_UNUSED
)
8899 Elf_Internal_Ehdr
* i_ehdrp
; /* ELF file header, internal form. */
8901 i_ehdrp
= elf_elfheader (abfd
);
8903 i_ehdrp
->e_ident
[EI_OSABI
] = get_elf_backend_data (abfd
)->elf_osabi
;
8907 /* Return TRUE for ELF symbol types that represent functions.
8908 This is the default version of this function, which is sufficient for
8909 most targets. It returns true if TYPE is STT_FUNC. */
8912 _bfd_elf_is_function_type (unsigned int type
)
8914 return (type
== STT_FUNC
);