1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet. */
33 /* For sparc64-cross-sparc32. */
41 #include "libiberty.h"
43 static INLINE
struct elf_segment_map
*make_mapping
44 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
45 static boolean map_sections_to_segments
PARAMS ((bfd
*));
46 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
47 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
48 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
49 static boolean prep_headers
PARAMS ((bfd
*));
50 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
51 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
52 static char *elf_read
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
53 static const char *group_signature
PARAMS ((bfd
*, Elf_Internal_Shdr
*));
54 static boolean setup_group
PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
55 static void merge_sections_remove_hook
PARAMS ((bfd
*, asection
*));
56 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
57 static boolean assign_section_numbers
PARAMS ((bfd
*));
58 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
59 static boolean elf_map_symbols
PARAMS ((bfd
*));
60 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
61 static boolean elfcore_read_notes
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
62 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
63 bfd_vma
, const char **,
65 static int elfcore_make_pid
PARAMS ((bfd
*));
66 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
67 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
68 Elf_Internal_Note
*));
69 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
70 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
71 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
73 static boolean elfcore_netbsd_get_lwpid
PARAMS ((Elf_Internal_Note
*, int *));
74 static boolean elfcore_grok_netbsd_procinfo
PARAMS ((bfd
*,
75 Elf_Internal_Note
*));
76 static boolean elfcore_grok_netbsd_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
78 /* Swap version information in and out. The version information is
79 currently size independent. If that ever changes, this code will
80 need to move into elfcode.h. */
82 /* Swap in a Verdef structure. */
85 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
87 const Elf_External_Verdef
*src
;
88 Elf_Internal_Verdef
*dst
;
90 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
91 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
92 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
93 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
94 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
95 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
96 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
99 /* Swap out a Verdef structure. */
102 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
104 const Elf_Internal_Verdef
*src
;
105 Elf_External_Verdef
*dst
;
107 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
108 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
109 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
110 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
111 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
112 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
113 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
116 /* Swap in a Verdaux structure. */
119 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
121 const Elf_External_Verdaux
*src
;
122 Elf_Internal_Verdaux
*dst
;
124 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
125 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
128 /* Swap out a Verdaux structure. */
131 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
133 const Elf_Internal_Verdaux
*src
;
134 Elf_External_Verdaux
*dst
;
136 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
137 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
140 /* Swap in a Verneed structure. */
143 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
145 const Elf_External_Verneed
*src
;
146 Elf_Internal_Verneed
*dst
;
148 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
149 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
150 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
151 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
152 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
155 /* Swap out a Verneed structure. */
158 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
160 const Elf_Internal_Verneed
*src
;
161 Elf_External_Verneed
*dst
;
163 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
164 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
165 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
166 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
167 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
170 /* Swap in a Vernaux structure. */
173 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
175 const Elf_External_Vernaux
*src
;
176 Elf_Internal_Vernaux
*dst
;
178 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
179 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
180 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
181 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
182 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
185 /* Swap out a Vernaux structure. */
188 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
190 const Elf_Internal_Vernaux
*src
;
191 Elf_External_Vernaux
*dst
;
193 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
194 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
195 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
196 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
197 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
200 /* Swap in a Versym structure. */
203 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
205 const Elf_External_Versym
*src
;
206 Elf_Internal_Versym
*dst
;
208 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
211 /* Swap out a Versym structure. */
214 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
216 const Elf_Internal_Versym
*src
;
217 Elf_External_Versym
*dst
;
219 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
222 /* Standard ELF hash function. Do not change this function; you will
223 cause invalid hash tables to be generated. */
226 bfd_elf_hash (namearg
)
229 const unsigned char *name
= (const unsigned char *) namearg
;
234 while ((ch
= *name
++) != '\0')
237 if ((g
= (h
& 0xf0000000)) != 0)
240 /* The ELF ABI says `h &= ~g', but this is equivalent in
241 this case and on some machines one insn instead of two. */
248 /* Read a specified number of bytes at a specified offset in an ELF
249 file, into a newly allocated buffer, and return a pointer to the
253 elf_read (abfd
, offset
, size
)
260 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
262 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
264 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
266 if (bfd_get_error () != bfd_error_system_call
)
267 bfd_set_error (bfd_error_file_truncated
);
274 bfd_elf_mkobject (abfd
)
277 /* This just does initialization. */
278 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
279 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
280 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
281 if (elf_tdata (abfd
) == 0)
283 /* Since everything is done at close time, do we need any
290 bfd_elf_mkcorefile (abfd
)
293 /* I think this can be done just like an object file. */
294 return bfd_elf_mkobject (abfd
);
298 bfd_elf_get_str_section (abfd
, shindex
)
300 unsigned int shindex
;
302 Elf_Internal_Shdr
**i_shdrp
;
303 char *shstrtab
= NULL
;
305 bfd_size_type shstrtabsize
;
307 i_shdrp
= elf_elfsections (abfd
);
308 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
311 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
312 if (shstrtab
== NULL
)
314 /* No cached one, attempt to read, and cache what we read. */
315 offset
= i_shdrp
[shindex
]->sh_offset
;
316 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
317 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
318 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
324 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
326 unsigned int shindex
;
327 unsigned int strindex
;
329 Elf_Internal_Shdr
*hdr
;
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
337 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
340 if (strindex
>= hdr
->sh_size
)
342 (*_bfd_error_handler
)
343 (_("%s: invalid string offset %u >= %lu for section `%s'"),
344 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
345 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
346 && strindex
== hdr
->sh_name
)
348 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
352 return ((char *) hdr
->contents
) + strindex
;
355 /* Read and convert symbols to internal format.
356 SYMCOUNT specifies the number of symbols to read, starting from
357 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
358 are non-NULL, they are used to store the internal symbols, external
359 symbols, and symbol section index extensions, respectively. */
362 bfd_elf_get_elf_syms (ibfd
, symtab_hdr
, symcount
, symoffset
,
363 intsym_buf
, extsym_buf
, extshndx_buf
)
365 Elf_Internal_Shdr
*symtab_hdr
;
368 Elf_Internal_Sym
*intsym_buf
;
370 Elf_External_Sym_Shndx
*extshndx_buf
;
372 Elf_Internal_Shdr
*shndx_hdr
;
374 const bfd_byte
*esym
;
375 Elf_External_Sym_Shndx
*alloc_extshndx
;
376 Elf_External_Sym_Shndx
*shndx
;
377 Elf_Internal_Sym
*isym
;
378 Elf_Internal_Sym
*isymend
;
379 struct elf_backend_data
*bed
;
387 /* Normal syms might have section extension entries. */
389 if (symtab_hdr
== &elf_tdata (ibfd
)->symtab_hdr
)
390 shndx_hdr
= &elf_tdata (ibfd
)->symtab_shndx_hdr
;
392 /* Read the symbols. */
394 alloc_extshndx
= NULL
;
395 bed
= get_elf_backend_data (ibfd
);
396 extsym_size
= bed
->s
->sizeof_sym
;
397 amt
= symcount
* extsym_size
;
398 pos
= symtab_hdr
->sh_offset
+ symoffset
* extsym_size
;
399 if (extsym_buf
== NULL
)
401 alloc_ext
= bfd_malloc (amt
);
402 extsym_buf
= alloc_ext
;
404 if (extsym_buf
== NULL
405 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
406 || bfd_bread (extsym_buf
, amt
, ibfd
) != amt
)
412 if (shndx_hdr
== NULL
|| shndx_hdr
->sh_size
== 0)
416 amt
= symcount
* sizeof (Elf_External_Sym_Shndx
);
417 pos
= shndx_hdr
->sh_offset
+ symoffset
* sizeof (Elf_External_Sym_Shndx
);
418 if (extshndx_buf
== NULL
)
420 alloc_extshndx
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
421 extshndx_buf
= alloc_extshndx
;
423 if (extshndx_buf
== NULL
424 || bfd_seek (ibfd
, pos
, SEEK_SET
) != 0
425 || bfd_bread (extshndx_buf
, amt
, ibfd
) != amt
)
432 if (intsym_buf
== NULL
)
434 bfd_size_type amt
= symcount
* sizeof (Elf_Internal_Sym
);
435 intsym_buf
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
436 if (intsym_buf
== NULL
)
440 /* Convert the symbols to internal form. */
441 isymend
= intsym_buf
+ symcount
;
442 for (esym
= extsym_buf
, isym
= intsym_buf
, shndx
= extshndx_buf
;
444 esym
+= extsym_size
, isym
++, shndx
= shndx
!= NULL
? shndx
+ 1 : NULL
)
445 (*bed
->s
->swap_symbol_in
) (ibfd
, esym
, (const PTR
) shndx
, isym
);
448 if (alloc_ext
!= NULL
)
450 if (alloc_extshndx
!= NULL
)
451 free (alloc_extshndx
);
456 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
457 sections. The first element is the flags, the rest are section
460 typedef union elf_internal_group
{
461 Elf_Internal_Shdr
*shdr
;
463 } Elf_Internal_Group
;
465 /* Return the name of the group signature symbol. Why isn't the
466 signature just a string? */
469 group_signature (abfd
, ghdr
)
471 Elf_Internal_Shdr
*ghdr
;
473 Elf_Internal_Shdr
*hdr
;
474 unsigned char esym
[sizeof (Elf64_External_Sym
)];
475 Elf_External_Sym_Shndx eshndx
;
476 Elf_Internal_Sym isym
;
478 unsigned int shindex
;
480 /* First we need to ensure the symbol table is available. */
481 if (! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
484 /* Go read the symbol. */
485 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
486 if (bfd_elf_get_elf_syms (abfd
, hdr
, 1, ghdr
->sh_info
,
487 &isym
, esym
, &eshndx
) == NULL
)
490 /* Look up the symbol name. */
491 iname
= isym
.st_name
;
492 shindex
= hdr
->sh_link
;
493 if (iname
== 0 && ELF_ST_TYPE (isym
.st_info
) == STT_SECTION
)
495 iname
= elf_elfsections (abfd
)[isym
.st_shndx
]->sh_name
;
496 shindex
= elf_elfheader (abfd
)->e_shstrndx
;
499 return bfd_elf_string_from_elf_section (abfd
, shindex
, iname
);
502 /* Set next_in_group list pointer, and group name for NEWSECT. */
505 setup_group (abfd
, hdr
, newsect
)
507 Elf_Internal_Shdr
*hdr
;
510 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
512 /* If num_group is zero, read in all SHT_GROUP sections. The count
513 is set to -1 if there are no SHT_GROUP sections. */
516 unsigned int i
, shnum
;
518 /* First count the number of groups. If we have a SHT_GROUP
519 section with just a flag word (ie. sh_size is 4), ignore it. */
520 shnum
= elf_numsections (abfd
);
522 for (i
= 0; i
< shnum
; i
++)
524 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
525 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
530 num_group
= (unsigned) -1;
531 elf_tdata (abfd
)->num_group
= num_group
;
535 /* We keep a list of elf section headers for group sections,
536 so we can find them quickly. */
537 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
538 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
539 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
543 for (i
= 0; i
< shnum
; i
++)
545 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
546 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
549 Elf_Internal_Group
*dest
;
551 /* Add to list of sections. */
552 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
555 /* Read the raw contents. */
556 BFD_ASSERT (sizeof (*dest
) >= 4);
557 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
558 shdr
->contents
= bfd_alloc (abfd
, amt
);
559 if (shdr
->contents
== NULL
560 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
561 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
565 /* Translate raw contents, a flag word followed by an
566 array of elf section indices all in target byte order,
567 to the flag word followed by an array of elf section
569 src
= shdr
->contents
+ shdr
->sh_size
;
570 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
577 idx
= H_GET_32 (abfd
, src
);
578 if (src
== shdr
->contents
)
581 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
582 shdr
->bfd_section
->flags
583 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
588 ((*_bfd_error_handler
)
589 (_("%s: invalid SHT_GROUP entry"),
590 bfd_archive_filename (abfd
)));
593 dest
->shdr
= elf_elfsections (abfd
)[idx
];
600 if (num_group
!= (unsigned) -1)
604 for (i
= 0; i
< num_group
; i
++)
606 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
607 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
608 unsigned int n_elt
= shdr
->sh_size
/ 4;
610 /* Look through this group's sections to see if current
611 section is a member. */
613 if ((++idx
)->shdr
== hdr
)
617 /* We are a member of this group. Go looking through
618 other members to see if any others are linked via
620 idx
= (Elf_Internal_Group
*) shdr
->contents
;
621 n_elt
= shdr
->sh_size
/ 4;
623 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
624 && elf_next_in_group (s
) != NULL
)
628 /* Snarf the group name from other member, and
629 insert current section in circular list. */
630 elf_group_name (newsect
) = elf_group_name (s
);
631 elf_next_in_group (newsect
) = elf_next_in_group (s
);
632 elf_next_in_group (s
) = newsect
;
638 gname
= group_signature (abfd
, shdr
);
641 elf_group_name (newsect
) = gname
;
643 /* Start a circular list with one element. */
644 elf_next_in_group (newsect
) = newsect
;
647 /* If the group section has been created, point to the
649 if (shdr
->bfd_section
!= NULL
)
650 elf_next_in_group (shdr
->bfd_section
) = newsect
;
658 if (elf_group_name (newsect
) == NULL
)
660 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
661 bfd_archive_filename (abfd
), newsect
->name
);
667 bfd_elf_discard_group (abfd
, group
)
668 bfd
*abfd ATTRIBUTE_UNUSED
;
671 asection
*first
= elf_next_in_group (group
);
676 s
->output_section
= bfd_abs_section_ptr
;
677 s
= elf_next_in_group (s
);
678 /* These lists are circular. */
685 /* Make a BFD section from an ELF section. We store a pointer to the
686 BFD section in the bfd_section field of the header. */
689 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
691 Elf_Internal_Shdr
*hdr
;
696 struct elf_backend_data
*bed
;
698 if (hdr
->bfd_section
!= NULL
)
700 BFD_ASSERT (strcmp (name
,
701 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
705 newsect
= bfd_make_section_anyway (abfd
, name
);
709 newsect
->filepos
= hdr
->sh_offset
;
711 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
712 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
713 || ! bfd_set_section_alignment (abfd
, newsect
,
714 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
717 flags
= SEC_NO_FLAGS
;
718 if (hdr
->sh_type
!= SHT_NOBITS
)
719 flags
|= SEC_HAS_CONTENTS
;
720 if (hdr
->sh_type
== SHT_GROUP
)
721 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
722 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
725 if (hdr
->sh_type
!= SHT_NOBITS
)
728 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
729 flags
|= SEC_READONLY
;
730 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
732 else if ((flags
& SEC_LOAD
) != 0)
734 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
737 newsect
->entsize
= hdr
->sh_entsize
;
738 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
739 flags
|= SEC_STRINGS
;
741 if (hdr
->sh_flags
& SHF_GROUP
)
742 if (!setup_group (abfd
, hdr
, newsect
))
744 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
745 flags
|= SEC_THREAD_LOCAL
;
747 /* The debugging sections appear to be recognized only by name, not
750 static const char *debug_sec_names
[] =
759 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
760 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
764 flags
|= SEC_DEBUGGING
;
767 /* As a GNU extension, if the name begins with .gnu.linkonce, we
768 only link a single copy of the section. This is used to support
769 g++. g++ will emit each template expansion in its own section.
770 The symbols will be defined as weak, so that multiple definitions
771 are permitted. The GNU linker extension is to actually discard
772 all but one of the sections. */
773 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
774 && elf_next_in_group (newsect
) == NULL
)
775 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
777 bed
= get_elf_backend_data (abfd
);
778 if (bed
->elf_backend_section_flags
)
779 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
782 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
785 if ((flags
& SEC_ALLOC
) != 0)
787 Elf_Internal_Phdr
*phdr
;
790 /* Look through the phdrs to see if we need to adjust the lma.
791 If all the p_paddr fields are zero, we ignore them, since
792 some ELF linkers produce such output. */
793 phdr
= elf_tdata (abfd
)->phdr
;
794 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
796 if (phdr
->p_paddr
!= 0)
799 if (i
< elf_elfheader (abfd
)->e_phnum
)
801 phdr
= elf_tdata (abfd
)->phdr
;
802 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
804 /* This section is part of this segment if its file
805 offset plus size lies within the segment's memory
806 span and, if the section is loaded, the extent of the
807 loaded data lies within the extent of the segment.
809 Note - we used to check the p_paddr field as well, and
810 refuse to set the LMA if it was 0. This is wrong
811 though, as a perfectly valid initialised segment can
812 have a p_paddr of zero. Some architectures, eg ARM,
813 place special significance on the address 0 and
814 executables need to be able to have a segment which
815 covers this address. */
816 if (phdr
->p_type
== PT_LOAD
817 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
818 && (hdr
->sh_offset
+ hdr
->sh_size
819 <= phdr
->p_offset
+ phdr
->p_memsz
)
820 && ((flags
& SEC_LOAD
) == 0
821 || (hdr
->sh_offset
+ hdr
->sh_size
822 <= phdr
->p_offset
+ phdr
->p_filesz
)))
824 if ((flags
& SEC_LOAD
) == 0)
825 newsect
->lma
= (phdr
->p_paddr
826 + hdr
->sh_addr
- phdr
->p_vaddr
);
828 /* We used to use the same adjustment for SEC_LOAD
829 sections, but that doesn't work if the segment
830 is packed with code from multiple VMAs.
831 Instead we calculate the section LMA based on
832 the segment LMA. It is assumed that the
833 segment will contain sections with contiguous
834 LMAs, even if the VMAs are not. */
835 newsect
->lma
= (phdr
->p_paddr
836 + hdr
->sh_offset
- phdr
->p_offset
);
838 /* With contiguous segments, we can't tell from file
839 offsets whether a section with zero size should
840 be placed at the end of one segment or the
841 beginning of the next. Decide based on vaddr. */
842 if (hdr
->sh_addr
>= phdr
->p_vaddr
843 && (hdr
->sh_addr
+ hdr
->sh_size
844 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
851 hdr
->bfd_section
= newsect
;
852 elf_section_data (newsect
)->this_hdr
= *hdr
;
862 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
865 Helper functions for GDB to locate the string tables.
866 Since BFD hides string tables from callers, GDB needs to use an
867 internal hook to find them. Sun's .stabstr, in particular,
868 isn't even pointed to by the .stab section, so ordinary
869 mechanisms wouldn't work to find it, even if we had some.
872 struct elf_internal_shdr
*
873 bfd_elf_find_section (abfd
, name
)
877 Elf_Internal_Shdr
**i_shdrp
;
882 i_shdrp
= elf_elfsections (abfd
);
885 shstrtab
= bfd_elf_get_str_section (abfd
,
886 elf_elfheader (abfd
)->e_shstrndx
);
887 if (shstrtab
!= NULL
)
889 max
= elf_numsections (abfd
);
890 for (i
= 1; i
< max
; i
++)
891 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
898 const char *const bfd_elf_section_type_names
[] = {
899 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
900 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
901 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
904 /* ELF relocs are against symbols. If we are producing relocateable
905 output, and the reloc is against an external symbol, and nothing
906 has given us any additional addend, the resulting reloc will also
907 be against the same symbol. In such a case, we don't want to
908 change anything about the way the reloc is handled, since it will
909 all be done at final link time. Rather than put special case code
910 into bfd_perform_relocation, all the reloc types use this howto
911 function. It just short circuits the reloc if producing
912 relocateable output against an external symbol. */
914 bfd_reloc_status_type
915 bfd_elf_generic_reloc (abfd
,
922 bfd
*abfd ATTRIBUTE_UNUSED
;
923 arelent
*reloc_entry
;
925 PTR data ATTRIBUTE_UNUSED
;
926 asection
*input_section
;
928 char **error_message ATTRIBUTE_UNUSED
;
930 if (output_bfd
!= (bfd
*) NULL
931 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
932 && (! reloc_entry
->howto
->partial_inplace
933 || reloc_entry
->addend
== 0))
935 reloc_entry
->address
+= input_section
->output_offset
;
939 return bfd_reloc_continue
;
942 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
945 merge_sections_remove_hook (abfd
, sec
)
946 bfd
*abfd ATTRIBUTE_UNUSED
;
949 struct bfd_elf_section_data
*sec_data
;
951 sec_data
= elf_section_data (sec
);
952 BFD_ASSERT (sec_data
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
953 sec_data
->sec_info_type
= ELF_INFO_TYPE_NONE
;
956 /* Finish SHF_MERGE section merging. */
959 _bfd_elf_merge_sections (abfd
, info
)
961 struct bfd_link_info
*info
;
963 if (!is_elf_hash_table (info
))
965 if (elf_hash_table (info
)->merge_info
)
966 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
967 merge_sections_remove_hook
);
972 _bfd_elf_link_just_syms (sec
, info
)
974 struct bfd_link_info
*info
;
976 sec
->output_section
= bfd_abs_section_ptr
;
977 sec
->output_offset
= sec
->vma
;
978 if (!is_elf_hash_table (info
))
981 elf_section_data (sec
)->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
984 /* Copy the program header and other data from one object module to
988 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
992 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
993 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
996 BFD_ASSERT (!elf_flags_init (obfd
)
997 || (elf_elfheader (obfd
)->e_flags
998 == elf_elfheader (ibfd
)->e_flags
));
1000 elf_gp (obfd
) = elf_gp (ibfd
);
1001 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
1002 elf_flags_init (obfd
) = true;
1006 /* Print out the program headers. */
1009 _bfd_elf_print_private_bfd_data (abfd
, farg
)
1013 FILE *f
= (FILE *) farg
;
1014 Elf_Internal_Phdr
*p
;
1016 bfd_byte
*dynbuf
= NULL
;
1018 p
= elf_tdata (abfd
)->phdr
;
1023 fprintf (f
, _("\nProgram Header:\n"));
1024 c
= elf_elfheader (abfd
)->e_phnum
;
1025 for (i
= 0; i
< c
; i
++, p
++)
1032 case PT_NULL
: pt
= "NULL"; break;
1033 case PT_LOAD
: pt
= "LOAD"; break;
1034 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
1035 case PT_INTERP
: pt
= "INTERP"; break;
1036 case PT_NOTE
: pt
= "NOTE"; break;
1037 case PT_SHLIB
: pt
= "SHLIB"; break;
1038 case PT_PHDR
: pt
= "PHDR"; break;
1039 case PT_TLS
: pt
= "TLS"; break;
1040 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
1041 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
1043 fprintf (f
, "%8s off 0x", pt
);
1044 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
1045 fprintf (f
, " vaddr 0x");
1046 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
1047 fprintf (f
, " paddr 0x");
1048 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
1049 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
1050 fprintf (f
, " filesz 0x");
1051 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
1052 fprintf (f
, " memsz 0x");
1053 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
1054 fprintf (f
, " flags %c%c%c",
1055 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
1056 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
1057 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
1058 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
1059 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
1064 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1068 unsigned long shlink
;
1069 bfd_byte
*extdyn
, *extdynend
;
1071 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1073 fprintf (f
, _("\nDynamic Section:\n"));
1075 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1078 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1082 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1085 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1087 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1088 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1091 extdynend
= extdyn
+ s
->_raw_size
;
1092 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1094 Elf_Internal_Dyn dyn
;
1099 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1101 if (dyn
.d_tag
== DT_NULL
)
1108 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1112 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1113 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1114 case DT_PLTGOT
: name
= "PLTGOT"; break;
1115 case DT_HASH
: name
= "HASH"; break;
1116 case DT_STRTAB
: name
= "STRTAB"; break;
1117 case DT_SYMTAB
: name
= "SYMTAB"; break;
1118 case DT_RELA
: name
= "RELA"; break;
1119 case DT_RELASZ
: name
= "RELASZ"; break;
1120 case DT_RELAENT
: name
= "RELAENT"; break;
1121 case DT_STRSZ
: name
= "STRSZ"; break;
1122 case DT_SYMENT
: name
= "SYMENT"; break;
1123 case DT_INIT
: name
= "INIT"; break;
1124 case DT_FINI
: name
= "FINI"; break;
1125 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1126 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1127 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1128 case DT_REL
: name
= "REL"; break;
1129 case DT_RELSZ
: name
= "RELSZ"; break;
1130 case DT_RELENT
: name
= "RELENT"; break;
1131 case DT_PLTREL
: name
= "PLTREL"; break;
1132 case DT_DEBUG
: name
= "DEBUG"; break;
1133 case DT_TEXTREL
: name
= "TEXTREL"; break;
1134 case DT_JMPREL
: name
= "JMPREL"; break;
1135 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1136 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1137 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1138 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1139 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1140 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1141 case DT_FLAGS
: name
= "FLAGS"; break;
1142 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1143 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1144 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1145 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1146 case DT_MOVEENT
: name
= "MOVEENT"; break;
1147 case DT_MOVESZ
: name
= "MOVESZ"; break;
1148 case DT_FEATURE
: name
= "FEATURE"; break;
1149 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1150 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1151 case DT_SYMINENT
: name
= "SYMINENT"; break;
1152 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1153 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1154 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1155 case DT_PLTPAD
: name
= "PLTPAD"; break;
1156 case DT_MOVETAB
: name
= "MOVETAB"; break;
1157 case DT_SYMINFO
: name
= "SYMINFO"; break;
1158 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1159 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1160 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1161 case DT_VERSYM
: name
= "VERSYM"; break;
1162 case DT_VERDEF
: name
= "VERDEF"; break;
1163 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1164 case DT_VERNEED
: name
= "VERNEED"; break;
1165 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1166 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1167 case DT_USED
: name
= "USED"; break;
1168 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1171 fprintf (f
, " %-11s ", name
);
1173 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1177 unsigned int tagv
= dyn
.d_un
.d_val
;
1179 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1182 fprintf (f
, "%s", string
);
1191 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1192 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1194 if (! _bfd_elf_slurp_version_tables (abfd
))
1198 if (elf_dynverdef (abfd
) != 0)
1200 Elf_Internal_Verdef
*t
;
1202 fprintf (f
, _("\nVersion definitions:\n"));
1203 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1205 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1206 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1207 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1209 Elf_Internal_Verdaux
*a
;
1212 for (a
= t
->vd_auxptr
->vda_nextptr
;
1215 fprintf (f
, "%s ", a
->vda_nodename
);
1221 if (elf_dynverref (abfd
) != 0)
1223 Elf_Internal_Verneed
*t
;
1225 fprintf (f
, _("\nVersion References:\n"));
1226 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1228 Elf_Internal_Vernaux
*a
;
1230 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1231 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1232 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1233 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1245 /* Display ELF-specific fields of a symbol. */
1248 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1252 bfd_print_symbol_type how
;
1254 FILE *file
= (FILE *) filep
;
1257 case bfd_print_symbol_name
:
1258 fprintf (file
, "%s", symbol
->name
);
1260 case bfd_print_symbol_more
:
1261 fprintf (file
, "elf ");
1262 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1263 fprintf (file
, " %lx", (long) symbol
->flags
);
1265 case bfd_print_symbol_all
:
1267 const char *section_name
;
1268 const char *name
= NULL
;
1269 struct elf_backend_data
*bed
;
1270 unsigned char st_other
;
1273 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1275 bed
= get_elf_backend_data (abfd
);
1276 if (bed
->elf_backend_print_symbol_all
)
1277 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1281 name
= symbol
->name
;
1282 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1285 fprintf (file
, " %s\t", section_name
);
1286 /* Print the "other" value for a symbol. For common symbols,
1287 we've already printed the size; now print the alignment.
1288 For other symbols, we have no specified alignment, and
1289 we've printed the address; now print the size. */
1290 if (bfd_is_com_section (symbol
->section
))
1291 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1293 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1294 bfd_fprintf_vma (abfd
, file
, val
);
1296 /* If we have version information, print it. */
1297 if (elf_tdata (abfd
)->dynversym_section
!= 0
1298 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1299 || elf_tdata (abfd
)->dynverref_section
!= 0))
1301 unsigned int vernum
;
1302 const char *version_string
;
1304 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1307 version_string
= "";
1308 else if (vernum
== 1)
1309 version_string
= "Base";
1310 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1312 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1315 Elf_Internal_Verneed
*t
;
1317 version_string
= "";
1318 for (t
= elf_tdata (abfd
)->verref
;
1322 Elf_Internal_Vernaux
*a
;
1324 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1326 if (a
->vna_other
== vernum
)
1328 version_string
= a
->vna_nodename
;
1335 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1336 fprintf (file
, " %-11s", version_string
);
1341 fprintf (file
, " (%s)", version_string
);
1342 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1347 /* If the st_other field is not zero, print it. */
1348 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1353 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1354 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1355 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1357 /* Some other non-defined flags are also present, so print
1359 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1362 fprintf (file
, " %s", name
);
1368 /* Create an entry in an ELF linker hash table. */
1370 struct bfd_hash_entry
*
1371 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1372 struct bfd_hash_entry
*entry
;
1373 struct bfd_hash_table
*table
;
1376 /* Allocate the structure if it has not already been allocated by a
1380 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1385 /* Call the allocation method of the superclass. */
1386 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1389 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1390 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1392 /* Set local fields. */
1396 ret
->dynstr_index
= 0;
1397 ret
->weakdef
= NULL
;
1398 ret
->got
.refcount
= htab
->init_refcount
;
1399 ret
->plt
.refcount
= htab
->init_refcount
;
1400 ret
->linker_section_pointer
= NULL
;
1401 ret
->verinfo
.verdef
= NULL
;
1402 ret
->vtable_entries_used
= NULL
;
1403 ret
->vtable_entries_size
= 0;
1404 ret
->vtable_parent
= NULL
;
1405 ret
->type
= STT_NOTYPE
;
1407 /* Assume that we have been called by a non-ELF symbol reader.
1408 This flag is then reset by the code which reads an ELF input
1409 file. This ensures that a symbol created by a non-ELF symbol
1410 reader will have the flag set correctly. */
1411 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1417 /* Copy data from an indirect symbol to its direct symbol, hiding the
1418 old indirect symbol. Also used for copying flags to a weakdef. */
1421 _bfd_elf_link_hash_copy_indirect (bed
, dir
, ind
)
1422 struct elf_backend_data
*bed
;
1423 struct elf_link_hash_entry
*dir
, *ind
;
1426 bfd_signed_vma lowest_valid
= bed
->can_refcount
;
1428 /* Copy down any references that we may have already seen to the
1429 symbol which just became indirect. */
1431 dir
->elf_link_hash_flags
|=
1432 (ind
->elf_link_hash_flags
1433 & (ELF_LINK_HASH_REF_DYNAMIC
1434 | ELF_LINK_HASH_REF_REGULAR
1435 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1436 | ELF_LINK_NON_GOT_REF
));
1438 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1441 /* Copy over the global and procedure linkage table refcount entries.
1442 These may have been already set up by a check_relocs routine. */
1443 tmp
= dir
->got
.refcount
;
1444 if (tmp
< lowest_valid
)
1446 dir
->got
.refcount
= ind
->got
.refcount
;
1447 ind
->got
.refcount
= tmp
;
1450 BFD_ASSERT (ind
->got
.refcount
< lowest_valid
);
1452 tmp
= dir
->plt
.refcount
;
1453 if (tmp
< lowest_valid
)
1455 dir
->plt
.refcount
= ind
->plt
.refcount
;
1456 ind
->plt
.refcount
= tmp
;
1459 BFD_ASSERT (ind
->plt
.refcount
< lowest_valid
);
1461 if (dir
->dynindx
== -1)
1463 dir
->dynindx
= ind
->dynindx
;
1464 dir
->dynstr_index
= ind
->dynstr_index
;
1466 ind
->dynstr_index
= 0;
1469 BFD_ASSERT (ind
->dynindx
== -1);
1473 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
)
1474 struct bfd_link_info
*info
;
1475 struct elf_link_hash_entry
*h
;
1476 boolean force_local
;
1478 h
->plt
.offset
= (bfd_vma
) -1;
1479 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1482 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1483 if (h
->dynindx
!= -1)
1486 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1492 /* Initialize an ELF linker hash table. */
1495 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1496 struct elf_link_hash_table
*table
;
1498 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1499 struct bfd_hash_table
*,
1504 table
->dynamic_sections_created
= false;
1505 table
->dynobj
= NULL
;
1506 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1507 /* The first dynamic symbol is a dummy. */
1508 table
->dynsymcount
= 1;
1509 table
->dynstr
= NULL
;
1510 table
->bucketcount
= 0;
1511 table
->needed
= NULL
;
1512 table
->runpath
= NULL
;
1513 table
->loaded
= NULL
;
1515 table
->stab_info
= NULL
;
1516 table
->merge_info
= NULL
;
1517 table
->dynlocal
= NULL
;
1518 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1519 table
->root
.type
= bfd_link_elf_hash_table
;
1524 /* Create an ELF linker hash table. */
1526 struct bfd_link_hash_table
*
1527 _bfd_elf_link_hash_table_create (abfd
)
1530 struct elf_link_hash_table
*ret
;
1531 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1533 ret
= (struct elf_link_hash_table
*) bfd_malloc (amt
);
1534 if (ret
== (struct elf_link_hash_table
*) NULL
)
1537 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1546 /* This is a hook for the ELF emulation code in the generic linker to
1547 tell the backend linker what file name to use for the DT_NEEDED
1548 entry for a dynamic object. The generic linker passes name as an
1549 empty string to indicate that no DT_NEEDED entry should be made. */
1552 bfd_elf_set_dt_needed_name (abfd
, name
)
1556 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1557 && bfd_get_format (abfd
) == bfd_object
)
1558 elf_dt_name (abfd
) = name
;
1562 bfd_elf_set_dt_needed_soname (abfd
, name
)
1566 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1567 && bfd_get_format (abfd
) == bfd_object
)
1568 elf_dt_soname (abfd
) = name
;
1571 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1572 the linker ELF emulation code. */
1574 struct bfd_link_needed_list
*
1575 bfd_elf_get_needed_list (abfd
, info
)
1576 bfd
*abfd ATTRIBUTE_UNUSED
;
1577 struct bfd_link_info
*info
;
1579 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1581 return elf_hash_table (info
)->needed
;
1584 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1585 hook for the linker ELF emulation code. */
1587 struct bfd_link_needed_list
*
1588 bfd_elf_get_runpath_list (abfd
, info
)
1589 bfd
*abfd ATTRIBUTE_UNUSED
;
1590 struct bfd_link_info
*info
;
1592 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1594 return elf_hash_table (info
)->runpath
;
1597 /* Get the name actually used for a dynamic object for a link. This
1598 is the SONAME entry if there is one. Otherwise, it is the string
1599 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1602 bfd_elf_get_dt_soname (abfd
)
1605 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1606 && bfd_get_format (abfd
) == bfd_object
)
1607 return elf_dt_name (abfd
);
1611 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1612 the ELF linker emulation code. */
1615 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1617 struct bfd_link_needed_list
**pneeded
;
1620 bfd_byte
*dynbuf
= NULL
;
1622 unsigned long shlink
;
1623 bfd_byte
*extdyn
, *extdynend
;
1625 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1629 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1630 || bfd_get_format (abfd
) != bfd_object
)
1633 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1634 if (s
== NULL
|| s
->_raw_size
== 0)
1637 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1641 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1645 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1649 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1651 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1652 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1655 extdynend
= extdyn
+ s
->_raw_size
;
1656 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1658 Elf_Internal_Dyn dyn
;
1660 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1662 if (dyn
.d_tag
== DT_NULL
)
1665 if (dyn
.d_tag
== DT_NEEDED
)
1668 struct bfd_link_needed_list
*l
;
1669 unsigned int tagv
= dyn
.d_un
.d_val
;
1672 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1677 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1698 /* Allocate an ELF string table--force the first byte to be zero. */
1700 struct bfd_strtab_hash
*
1701 _bfd_elf_stringtab_init ()
1703 struct bfd_strtab_hash
*ret
;
1705 ret
= _bfd_stringtab_init ();
1710 loc
= _bfd_stringtab_add (ret
, "", true, false);
1711 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1712 if (loc
== (bfd_size_type
) -1)
1714 _bfd_stringtab_free (ret
);
1721 /* ELF .o/exec file reading */
1723 /* Create a new bfd section from an ELF section header. */
1726 bfd_section_from_shdr (abfd
, shindex
)
1728 unsigned int shindex
;
1730 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1731 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1732 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1735 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1737 switch (hdr
->sh_type
)
1740 /* Inactive section. Throw it away. */
1743 case SHT_PROGBITS
: /* Normal section with contents. */
1744 case SHT_NOBITS
: /* .bss section. */
1745 case SHT_HASH
: /* .hash section. */
1746 case SHT_NOTE
: /* .note section. */
1747 case SHT_INIT_ARRAY
: /* .init_array section. */
1748 case SHT_FINI_ARRAY
: /* .fini_array section. */
1749 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1750 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1752 case SHT_DYNAMIC
: /* Dynamic linking information. */
1753 if (! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1755 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_STRTAB
)
1757 Elf_Internal_Shdr
*dynsymhdr
;
1759 /* The shared libraries distributed with hpux11 have a bogus
1760 sh_link field for the ".dynamic" section. Find the
1761 string table for the ".dynsym" section instead. */
1762 if (elf_dynsymtab (abfd
) != 0)
1764 dynsymhdr
= elf_elfsections (abfd
)[elf_dynsymtab (abfd
)];
1765 hdr
->sh_link
= dynsymhdr
->sh_link
;
1769 unsigned int i
, num_sec
;
1771 num_sec
= elf_numsections (abfd
);
1772 for (i
= 1; i
< num_sec
; i
++)
1774 dynsymhdr
= elf_elfsections (abfd
)[i
];
1775 if (dynsymhdr
->sh_type
== SHT_DYNSYM
)
1777 hdr
->sh_link
= dynsymhdr
->sh_link
;
1785 case SHT_SYMTAB
: /* A symbol table */
1786 if (elf_onesymtab (abfd
) == shindex
)
1789 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1790 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1791 elf_onesymtab (abfd
) = shindex
;
1792 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1793 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1794 abfd
->flags
|= HAS_SYMS
;
1796 /* Sometimes a shared object will map in the symbol table. If
1797 SHF_ALLOC is set, and this is a shared object, then we also
1798 treat this section as a BFD section. We can not base the
1799 decision purely on SHF_ALLOC, because that flag is sometimes
1800 set in a relocateable object file, which would confuse the
1802 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1803 && (abfd
->flags
& DYNAMIC
) != 0
1804 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1809 case SHT_DYNSYM
: /* A dynamic symbol table */
1810 if (elf_dynsymtab (abfd
) == shindex
)
1813 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1814 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1815 elf_dynsymtab (abfd
) = shindex
;
1816 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1817 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1818 abfd
->flags
|= HAS_SYMS
;
1820 /* Besides being a symbol table, we also treat this as a regular
1821 section, so that objcopy can handle it. */
1822 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1824 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1825 if (elf_symtab_shndx (abfd
) == shindex
)
1828 /* Get the associated symbol table. */
1829 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1830 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1833 elf_symtab_shndx (abfd
) = shindex
;
1834 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1835 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1838 case SHT_STRTAB
: /* A string table */
1839 if (hdr
->bfd_section
!= NULL
)
1841 if (ehdr
->e_shstrndx
== shindex
)
1843 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1844 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1848 unsigned int i
, num_sec
;
1850 num_sec
= elf_numsections (abfd
);
1851 for (i
= 1; i
< num_sec
; i
++)
1853 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1854 if (hdr2
->sh_link
== shindex
)
1856 if (! bfd_section_from_shdr (abfd
, i
))
1858 if (elf_onesymtab (abfd
) == i
)
1860 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1861 elf_elfsections (abfd
)[shindex
] =
1862 &elf_tdata (abfd
)->strtab_hdr
;
1865 if (elf_dynsymtab (abfd
) == i
)
1867 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1868 elf_elfsections (abfd
)[shindex
] = hdr
=
1869 &elf_tdata (abfd
)->dynstrtab_hdr
;
1870 /* We also treat this as a regular section, so
1871 that objcopy can handle it. */
1874 #if 0 /* Not handling other string tables specially right now. */
1875 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1876 /* We have a strtab for some random other section. */
1877 newsect
= (asection
*) hdr2
->bfd_section
;
1880 hdr
->bfd_section
= newsect
;
1881 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1883 elf_elfsections (abfd
)[shindex
] = hdr2
;
1889 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1893 /* *These* do a lot of work -- but build no sections! */
1895 asection
*target_sect
;
1896 Elf_Internal_Shdr
*hdr2
;
1897 unsigned int num_sec
= elf_numsections (abfd
);
1899 /* Check for a bogus link to avoid crashing. */
1900 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1901 || hdr
->sh_link
>= num_sec
)
1903 ((*_bfd_error_handler
)
1904 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1905 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1906 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1909 /* For some incomprehensible reason Oracle distributes
1910 libraries for Solaris in which some of the objects have
1911 bogus sh_link fields. It would be nice if we could just
1912 reject them, but, unfortunately, some people need to use
1913 them. We scan through the section headers; if we find only
1914 one suitable symbol table, we clobber the sh_link to point
1915 to it. I hope this doesn't break anything. */
1916 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1917 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1923 for (scan
= 1; scan
< num_sec
; scan
++)
1925 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1926 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1937 hdr
->sh_link
= found
;
1940 /* Get the symbol table. */
1941 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1942 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1945 /* If this reloc section does not use the main symbol table we
1946 don't treat it as a reloc section. BFD can't adequately
1947 represent such a section, so at least for now, we don't
1948 try. We just present it as a normal section. We also
1949 can't use it as a reloc section if it points to the null
1951 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1952 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1954 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1956 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1957 if (target_sect
== NULL
)
1960 if ((target_sect
->flags
& SEC_RELOC
) == 0
1961 || target_sect
->reloc_count
== 0)
1962 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1966 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1967 amt
= sizeof (*hdr2
);
1968 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1969 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1972 elf_elfsections (abfd
)[shindex
] = hdr2
;
1973 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1974 target_sect
->flags
|= SEC_RELOC
;
1975 target_sect
->relocation
= NULL
;
1976 target_sect
->rel_filepos
= hdr
->sh_offset
;
1977 /* In the section to which the relocations apply, mark whether
1978 its relocations are of the REL or RELA variety. */
1979 if (hdr
->sh_size
!= 0)
1980 elf_section_data (target_sect
)->use_rela_p
1981 = (hdr
->sh_type
== SHT_RELA
);
1982 abfd
->flags
|= HAS_RELOC
;
1987 case SHT_GNU_verdef
:
1988 elf_dynverdef (abfd
) = shindex
;
1989 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1990 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1993 case SHT_GNU_versym
:
1994 elf_dynversym (abfd
) = shindex
;
1995 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1996 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1999 case SHT_GNU_verneed
:
2000 elf_dynverref (abfd
) = shindex
;
2001 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
2002 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
2009 /* We need a BFD section for objcopy and relocatable linking,
2010 and it's handy to have the signature available as the section
2012 name
= group_signature (abfd
, hdr
);
2015 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
2017 if (hdr
->contents
!= NULL
)
2019 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
2020 unsigned int n_elt
= hdr
->sh_size
/ 4;
2023 if (idx
->flags
& GRP_COMDAT
)
2024 hdr
->bfd_section
->flags
2025 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2027 while (--n_elt
!= 0)
2028 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
2029 && elf_next_in_group (s
) != NULL
)
2031 elf_next_in_group (hdr
->bfd_section
) = s
;
2038 /* Check for any processor-specific section types. */
2040 if (bed
->elf_backend_section_from_shdr
)
2041 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
2049 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2050 Return SEC for sections that have no elf section, and NULL on error. */
2053 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
2055 struct sym_sec_cache
*cache
;
2057 unsigned long r_symndx
;
2059 Elf_Internal_Shdr
*symtab_hdr
;
2060 unsigned char esym
[sizeof (Elf64_External_Sym
)];
2061 Elf_External_Sym_Shndx eshndx
;
2062 Elf_Internal_Sym isym
;
2063 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
2065 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
2066 return cache
->sec
[ent
];
2068 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2069 if (bfd_elf_get_elf_syms (abfd
, symtab_hdr
, 1, r_symndx
,
2070 &isym
, esym
, &eshndx
) == NULL
)
2073 if (cache
->abfd
!= abfd
)
2075 memset (cache
->indx
, -1, sizeof (cache
->indx
));
2078 cache
->indx
[ent
] = r_symndx
;
2079 cache
->sec
[ent
] = sec
;
2080 if (isym
.st_shndx
< SHN_LORESERVE
|| isym
.st_shndx
> SHN_HIRESERVE
)
2083 s
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
2085 cache
->sec
[ent
] = s
;
2087 return cache
->sec
[ent
];
2090 /* Given an ELF section number, retrieve the corresponding BFD
2094 bfd_section_from_elf_index (abfd
, index
)
2098 if (index
>= elf_numsections (abfd
))
2100 return elf_elfsections (abfd
)[index
]->bfd_section
;
2104 _bfd_elf_new_section_hook (abfd
, sec
)
2108 struct bfd_elf_section_data
*sdata
;
2109 bfd_size_type amt
= sizeof (*sdata
);
2111 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
2114 sec
->used_by_bfd
= (PTR
) sdata
;
2116 /* Indicate whether or not this section should use RELA relocations. */
2118 = get_elf_backend_data (abfd
)->default_use_rela_p
;
2123 /* Create a new bfd section from an ELF program header.
2125 Since program segments have no names, we generate a synthetic name
2126 of the form segment<NUM>, where NUM is generally the index in the
2127 program header table. For segments that are split (see below) we
2128 generate the names segment<NUM>a and segment<NUM>b.
2130 Note that some program segments may have a file size that is different than
2131 (less than) the memory size. All this means is that at execution the
2132 system must allocate the amount of memory specified by the memory size,
2133 but only initialize it with the first "file size" bytes read from the
2134 file. This would occur for example, with program segments consisting
2135 of combined data+bss.
2137 To handle the above situation, this routine generates TWO bfd sections
2138 for the single program segment. The first has the length specified by
2139 the file size of the segment, and the second has the length specified
2140 by the difference between the two sizes. In effect, the segment is split
2141 into it's initialized and uninitialized parts.
2146 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
2148 Elf_Internal_Phdr
*hdr
;
2150 const char *typename
;
2158 split
= ((hdr
->p_memsz
> 0)
2159 && (hdr
->p_filesz
> 0)
2160 && (hdr
->p_memsz
> hdr
->p_filesz
));
2161 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2162 len
= strlen (namebuf
) + 1;
2163 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
2166 memcpy (name
, namebuf
, len
);
2167 newsect
= bfd_make_section (abfd
, name
);
2168 if (newsect
== NULL
)
2170 newsect
->vma
= hdr
->p_vaddr
;
2171 newsect
->lma
= hdr
->p_paddr
;
2172 newsect
->_raw_size
= hdr
->p_filesz
;
2173 newsect
->filepos
= hdr
->p_offset
;
2174 newsect
->flags
|= SEC_HAS_CONTENTS
;
2175 if (hdr
->p_type
== PT_LOAD
)
2177 newsect
->flags
|= SEC_ALLOC
;
2178 newsect
->flags
|= SEC_LOAD
;
2179 if (hdr
->p_flags
& PF_X
)
2181 /* FIXME: all we known is that it has execute PERMISSION,
2183 newsect
->flags
|= SEC_CODE
;
2186 if (!(hdr
->p_flags
& PF_W
))
2188 newsect
->flags
|= SEC_READONLY
;
2193 sprintf (namebuf
, "%s%db", typename
, index
);
2194 len
= strlen (namebuf
) + 1;
2195 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
2198 memcpy (name
, namebuf
, len
);
2199 newsect
= bfd_make_section (abfd
, name
);
2200 if (newsect
== NULL
)
2202 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2203 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2204 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2205 if (hdr
->p_type
== PT_LOAD
)
2207 newsect
->flags
|= SEC_ALLOC
;
2208 if (hdr
->p_flags
& PF_X
)
2209 newsect
->flags
|= SEC_CODE
;
2211 if (!(hdr
->p_flags
& PF_W
))
2212 newsect
->flags
|= SEC_READONLY
;
2219 bfd_section_from_phdr (abfd
, hdr
, index
)
2221 Elf_Internal_Phdr
*hdr
;
2224 struct elf_backend_data
*bed
;
2226 switch (hdr
->p_type
)
2229 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2232 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2235 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2238 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2241 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2243 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2248 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2251 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2254 /* Check for any processor-specific program segment types.
2255 If no handler for them, default to making "segment" sections. */
2256 bed
= get_elf_backend_data (abfd
);
2257 if (bed
->elf_backend_section_from_phdr
)
2258 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2260 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2264 /* Initialize REL_HDR, the section-header for new section, containing
2265 relocations against ASECT. If USE_RELA_P is true, we use RELA
2266 relocations; otherwise, we use REL relocations. */
2269 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2271 Elf_Internal_Shdr
*rel_hdr
;
2276 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2277 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2279 name
= bfd_alloc (abfd
, amt
);
2282 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2284 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2286 if (rel_hdr
->sh_name
== (unsigned int) -1)
2288 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2289 rel_hdr
->sh_entsize
= (use_rela_p
2290 ? bed
->s
->sizeof_rela
2291 : bed
->s
->sizeof_rel
);
2292 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2293 rel_hdr
->sh_flags
= 0;
2294 rel_hdr
->sh_addr
= 0;
2295 rel_hdr
->sh_size
= 0;
2296 rel_hdr
->sh_offset
= 0;
2301 /* Set up an ELF internal section header for a section. */
2304 elf_fake_sections (abfd
, asect
, failedptrarg
)
2309 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2310 boolean
*failedptr
= (boolean
*) failedptrarg
;
2311 Elf_Internal_Shdr
*this_hdr
;
2315 /* We already failed; just get out of the bfd_map_over_sections
2320 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2322 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2323 asect
->name
, false);
2324 if (this_hdr
->sh_name
== (unsigned long) -1)
2330 this_hdr
->sh_flags
= 0;
2332 if ((asect
->flags
& SEC_ALLOC
) != 0
2333 || asect
->user_set_vma
)
2334 this_hdr
->sh_addr
= asect
->vma
;
2336 this_hdr
->sh_addr
= 0;
2338 this_hdr
->sh_offset
= 0;
2339 this_hdr
->sh_size
= asect
->_raw_size
;
2340 this_hdr
->sh_link
= 0;
2341 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2342 /* The sh_entsize and sh_info fields may have been set already by
2343 copy_private_section_data. */
2345 this_hdr
->bfd_section
= asect
;
2346 this_hdr
->contents
= NULL
;
2348 /* FIXME: This should not be based on section names. */
2349 if (strcmp (asect
->name
, ".dynstr") == 0)
2350 this_hdr
->sh_type
= SHT_STRTAB
;
2351 else if (strcmp (asect
->name
, ".hash") == 0)
2353 this_hdr
->sh_type
= SHT_HASH
;
2354 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2356 else if (strcmp (asect
->name
, ".dynsym") == 0)
2358 this_hdr
->sh_type
= SHT_DYNSYM
;
2359 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2361 else if (strcmp (asect
->name
, ".dynamic") == 0)
2363 this_hdr
->sh_type
= SHT_DYNAMIC
;
2364 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2366 else if (strncmp (asect
->name
, ".rela", 5) == 0
2367 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2369 this_hdr
->sh_type
= SHT_RELA
;
2370 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2372 else if (strncmp (asect
->name
, ".rel", 4) == 0
2373 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2375 this_hdr
->sh_type
= SHT_REL
;
2376 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2378 else if (strcmp (asect
->name
, ".init_array") == 0)
2379 this_hdr
->sh_type
= SHT_INIT_ARRAY
;
2380 else if (strcmp (asect
->name
, ".fini_array") == 0)
2381 this_hdr
->sh_type
= SHT_FINI_ARRAY
;
2382 else if (strcmp (asect
->name
, ".preinit_array") == 0)
2383 this_hdr
->sh_type
= SHT_PREINIT_ARRAY
;
2384 else if (strncmp (asect
->name
, ".note", 5) == 0)
2385 this_hdr
->sh_type
= SHT_NOTE
;
2386 else if (strncmp (asect
->name
, ".stab", 5) == 0
2387 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2388 this_hdr
->sh_type
= SHT_STRTAB
;
2389 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2391 this_hdr
->sh_type
= SHT_GNU_versym
;
2392 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2394 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2396 this_hdr
->sh_type
= SHT_GNU_verdef
;
2397 this_hdr
->sh_entsize
= 0;
2398 /* objcopy or strip will copy over sh_info, but may not set
2399 cverdefs. The linker will set cverdefs, but sh_info will be
2401 if (this_hdr
->sh_info
== 0)
2402 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2404 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2405 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2407 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2409 this_hdr
->sh_type
= SHT_GNU_verneed
;
2410 this_hdr
->sh_entsize
= 0;
2411 /* objcopy or strip will copy over sh_info, but may not set
2412 cverrefs. The linker will set cverrefs, but sh_info will be
2414 if (this_hdr
->sh_info
== 0)
2415 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2417 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2418 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2420 else if ((asect
->flags
& SEC_GROUP
) != 0)
2422 this_hdr
->sh_type
= SHT_GROUP
;
2423 this_hdr
->sh_entsize
= 4;
2425 else if ((asect
->flags
& SEC_ALLOC
) != 0
2426 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2427 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2428 this_hdr
->sh_type
= SHT_NOBITS
;
2430 this_hdr
->sh_type
= SHT_PROGBITS
;
2432 if ((asect
->flags
& SEC_ALLOC
) != 0)
2433 this_hdr
->sh_flags
|= SHF_ALLOC
;
2434 if ((asect
->flags
& SEC_READONLY
) == 0)
2435 this_hdr
->sh_flags
|= SHF_WRITE
;
2436 if ((asect
->flags
& SEC_CODE
) != 0)
2437 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2438 if ((asect
->flags
& SEC_MERGE
) != 0)
2440 this_hdr
->sh_flags
|= SHF_MERGE
;
2441 this_hdr
->sh_entsize
= asect
->entsize
;
2442 if ((asect
->flags
& SEC_STRINGS
) != 0)
2443 this_hdr
->sh_flags
|= SHF_STRINGS
;
2445 if ((asect
->flags
& SEC_GROUP
) == 0 && elf_group_name (asect
) != NULL
)
2446 this_hdr
->sh_flags
|= SHF_GROUP
;
2447 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2449 this_hdr
->sh_flags
|= SHF_TLS
;
2450 if (asect
->_raw_size
== 0 && (asect
->flags
& SEC_HAS_CONTENTS
) == 0)
2452 struct bfd_link_order
*o
;
2454 this_hdr
->sh_size
= 0;
2455 for (o
= asect
->link_order_head
; o
!= NULL
; o
= o
->next
)
2456 if (this_hdr
->sh_size
< o
->offset
+ o
->size
)
2457 this_hdr
->sh_size
= o
->offset
+ o
->size
;
2458 if (this_hdr
->sh_size
)
2459 this_hdr
->sh_type
= SHT_NOBITS
;
2463 /* Check for processor-specific section types. */
2464 if (bed
->elf_backend_fake_sections
2465 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2468 /* If the section has relocs, set up a section header for the
2469 SHT_REL[A] section. If two relocation sections are required for
2470 this section, it is up to the processor-specific back-end to
2471 create the other. */
2472 if ((asect
->flags
& SEC_RELOC
) != 0
2473 && !_bfd_elf_init_reloc_shdr (abfd
,
2474 &elf_section_data (asect
)->rel_hdr
,
2476 elf_section_data (asect
)->use_rela_p
))
2480 /* Fill in the contents of a SHT_GROUP section. */
2483 bfd_elf_set_group_contents (abfd
, sec
, failedptrarg
)
2488 boolean
*failedptr
= (boolean
*) failedptrarg
;
2489 unsigned long symindx
;
2490 asection
*elt
, *first
;
2492 struct bfd_link_order
*l
;
2495 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2500 if (elf_group_id (sec
) != NULL
)
2501 symindx
= elf_group_id (sec
)->udata
.i
;
2505 /* If called from the assembler, swap_out_syms will have set up
2506 elf_section_syms; If called for "ld -r", use target_index. */
2507 if (elf_section_syms (abfd
) != NULL
)
2508 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2510 symindx
= sec
->target_index
;
2512 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2514 /* The contents won't be allocated for "ld -r" or objcopy. */
2516 if (sec
->contents
== NULL
)
2519 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2521 /* Arrange for the section to be written out. */
2522 elf_section_data (sec
)->this_hdr
.contents
= sec
->contents
;
2523 if (sec
->contents
== NULL
)
2530 loc
= sec
->contents
+ sec
->_raw_size
;
2532 /* Get the pointer to the first section in the group that gas
2533 squirreled away here. objcopy arranges for this to be set to the
2534 start of the input section group. */
2535 first
= elt
= elf_next_in_group (sec
);
2537 /* First element is a flag word. Rest of section is elf section
2538 indices for all the sections of the group. Write them backwards
2539 just to keep the group in the same order as given in .section
2540 directives, not that it matters. */
2549 s
= s
->output_section
;
2552 idx
= elf_section_data (s
)->this_idx
;
2553 H_PUT_32 (abfd
, idx
, loc
);
2554 elt
= elf_next_in_group (elt
);
2559 /* If this is a relocatable link, then the above did nothing because
2560 SEC is the output section. Look through the input sections
2562 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2563 if (l
->type
== bfd_indirect_link_order
2564 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2569 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2570 elt
= elf_next_in_group (elt
);
2571 /* During a relocatable link, the lists are circular. */
2573 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2575 /* With ld -r, merging SHT_GROUP sections results in wasted space
2576 due to allowing for the flag word on each input. We may well
2577 duplicate entries too. */
2578 while ((loc
-= 4) > sec
->contents
)
2579 H_PUT_32 (abfd
, 0, loc
);
2581 if (loc
!= sec
->contents
)
2584 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2587 /* Assign all ELF section numbers. The dummy first section is handled here
2588 too. The link/info pointers for the standard section types are filled
2589 in here too, while we're at it. */
2592 assign_section_numbers (abfd
)
2595 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2597 unsigned int section_number
, secn
;
2598 Elf_Internal_Shdr
**i_shdrp
;
2603 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2605 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2607 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2609 if (section_number
== SHN_LORESERVE
)
2610 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2611 d
->this_idx
= section_number
++;
2612 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2613 if ((sec
->flags
& SEC_RELOC
) == 0)
2617 if (section_number
== SHN_LORESERVE
)
2618 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2619 d
->rel_idx
= section_number
++;
2620 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2625 if (section_number
== SHN_LORESERVE
)
2626 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2627 d
->rel_idx2
= section_number
++;
2628 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2634 if (section_number
== SHN_LORESERVE
)
2635 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2636 t
->shstrtab_section
= section_number
++;
2637 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2638 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2640 if (bfd_get_symcount (abfd
) > 0)
2642 if (section_number
== SHN_LORESERVE
)
2643 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2644 t
->symtab_section
= section_number
++;
2645 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2646 if (section_number
> SHN_LORESERVE
- 2)
2648 if (section_number
== SHN_LORESERVE
)
2649 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2650 t
->symtab_shndx_section
= section_number
++;
2651 t
->symtab_shndx_hdr
.sh_name
2652 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2653 ".symtab_shndx", false);
2654 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2657 if (section_number
== SHN_LORESERVE
)
2658 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2659 t
->strtab_section
= section_number
++;
2660 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2663 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2664 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2666 elf_numsections (abfd
) = section_number
;
2667 elf_elfheader (abfd
)->e_shnum
= section_number
;
2668 if (section_number
> SHN_LORESERVE
)
2669 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2671 /* Set up the list of section header pointers, in agreement with the
2673 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2674 i_shdrp
= (Elf_Internal_Shdr
**) bfd_zalloc (abfd
, amt
);
2675 if (i_shdrp
== NULL
)
2678 amt
= sizeof (Elf_Internal_Shdr
);
2679 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_zalloc (abfd
, amt
);
2680 if (i_shdrp
[0] == NULL
)
2682 bfd_release (abfd
, i_shdrp
);
2686 elf_elfsections (abfd
) = i_shdrp
;
2688 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2689 if (bfd_get_symcount (abfd
) > 0)
2691 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2692 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2694 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2695 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2697 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2698 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2700 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2702 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2706 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2707 if (d
->rel_idx
!= 0)
2708 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2709 if (d
->rel_idx2
!= 0)
2710 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2712 /* Fill in the sh_link and sh_info fields while we're at it. */
2714 /* sh_link of a reloc section is the section index of the symbol
2715 table. sh_info is the section index of the section to which
2716 the relocation entries apply. */
2717 if (d
->rel_idx
!= 0)
2719 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2720 d
->rel_hdr
.sh_info
= d
->this_idx
;
2722 if (d
->rel_idx2
!= 0)
2724 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2725 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2728 switch (d
->this_hdr
.sh_type
)
2732 /* A reloc section which we are treating as a normal BFD
2733 section. sh_link is the section index of the symbol
2734 table. sh_info is the section index of the section to
2735 which the relocation entries apply. We assume that an
2736 allocated reloc section uses the dynamic symbol table.
2737 FIXME: How can we be sure? */
2738 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2740 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2742 /* We look up the section the relocs apply to by name. */
2744 if (d
->this_hdr
.sh_type
== SHT_REL
)
2748 s
= bfd_get_section_by_name (abfd
, name
);
2750 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2754 /* We assume that a section named .stab*str is a stabs
2755 string section. We look for a section with the same name
2756 but without the trailing ``str'', and set its sh_link
2757 field to point to this section. */
2758 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2759 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2764 len
= strlen (sec
->name
);
2765 alc
= (char *) bfd_malloc ((bfd_size_type
) (len
- 2));
2768 memcpy (alc
, sec
->name
, len
- 3);
2769 alc
[len
- 3] = '\0';
2770 s
= bfd_get_section_by_name (abfd
, alc
);
2774 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2776 /* This is a .stab section. */
2777 if (elf_section_data (s
)->this_hdr
.sh_entsize
== 0)
2778 elf_section_data (s
)->this_hdr
.sh_entsize
2779 = 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2786 case SHT_GNU_verneed
:
2787 case SHT_GNU_verdef
:
2788 /* sh_link is the section header index of the string table
2789 used for the dynamic entries, or the symbol table, or the
2791 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2793 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2797 case SHT_GNU_versym
:
2798 /* sh_link is the section header index of the symbol table
2799 this hash table or version table is for. */
2800 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2802 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2806 d
->this_hdr
.sh_link
= t
->symtab_section
;
2810 for (secn
= 1; secn
< section_number
; ++secn
)
2811 if (i_shdrp
[secn
] == NULL
)
2812 i_shdrp
[secn
] = i_shdrp
[0];
2814 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2815 i_shdrp
[secn
]->sh_name
);
2819 /* Map symbol from it's internal number to the external number, moving
2820 all local symbols to be at the head of the list. */
2823 sym_is_global (abfd
, sym
)
2827 /* If the backend has a special mapping, use it. */
2828 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2829 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2832 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2833 || bfd_is_und_section (bfd_get_section (sym
))
2834 || bfd_is_com_section (bfd_get_section (sym
)));
2838 elf_map_symbols (abfd
)
2841 unsigned int symcount
= bfd_get_symcount (abfd
);
2842 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2843 asymbol
**sect_syms
;
2844 unsigned int num_locals
= 0;
2845 unsigned int num_globals
= 0;
2846 unsigned int num_locals2
= 0;
2847 unsigned int num_globals2
= 0;
2855 fprintf (stderr
, "elf_map_symbols\n");
2859 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2861 if (max_index
< asect
->index
)
2862 max_index
= asect
->index
;
2866 amt
= max_index
* sizeof (asymbol
*);
2867 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2868 if (sect_syms
== NULL
)
2870 elf_section_syms (abfd
) = sect_syms
;
2871 elf_num_section_syms (abfd
) = max_index
;
2873 /* Init sect_syms entries for any section symbols we have already
2874 decided to output. */
2875 for (idx
= 0; idx
< symcount
; idx
++)
2877 asymbol
*sym
= syms
[idx
];
2879 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2886 if (sec
->owner
!= NULL
)
2888 if (sec
->owner
!= abfd
)
2890 if (sec
->output_offset
!= 0)
2893 sec
= sec
->output_section
;
2895 /* Empty sections in the input files may have had a
2896 section symbol created for them. (See the comment
2897 near the end of _bfd_generic_link_output_symbols in
2898 linker.c). If the linker script discards such
2899 sections then we will reach this point. Since we know
2900 that we cannot avoid this case, we detect it and skip
2901 the abort and the assignment to the sect_syms array.
2902 To reproduce this particular case try running the
2903 linker testsuite test ld-scripts/weak.exp for an ELF
2904 port that uses the generic linker. */
2905 if (sec
->owner
== NULL
)
2908 BFD_ASSERT (sec
->owner
== abfd
);
2910 sect_syms
[sec
->index
] = syms
[idx
];
2915 /* Classify all of the symbols. */
2916 for (idx
= 0; idx
< symcount
; idx
++)
2918 if (!sym_is_global (abfd
, syms
[idx
]))
2924 /* We will be adding a section symbol for each BFD section. Most normal
2925 sections will already have a section symbol in outsymbols, but
2926 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2927 at least in that case. */
2928 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2930 if (sect_syms
[asect
->index
] == NULL
)
2932 if (!sym_is_global (abfd
, asect
->symbol
))
2939 /* Now sort the symbols so the local symbols are first. */
2940 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2941 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2943 if (new_syms
== NULL
)
2946 for (idx
= 0; idx
< symcount
; idx
++)
2948 asymbol
*sym
= syms
[idx
];
2951 if (!sym_is_global (abfd
, sym
))
2954 i
= num_locals
+ num_globals2
++;
2956 sym
->udata
.i
= i
+ 1;
2958 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2960 if (sect_syms
[asect
->index
] == NULL
)
2962 asymbol
*sym
= asect
->symbol
;
2965 sect_syms
[asect
->index
] = sym
;
2966 if (!sym_is_global (abfd
, sym
))
2969 i
= num_locals
+ num_globals2
++;
2971 sym
->udata
.i
= i
+ 1;
2975 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2977 elf_num_locals (abfd
) = num_locals
;
2978 elf_num_globals (abfd
) = num_globals
;
2982 /* Align to the maximum file alignment that could be required for any
2983 ELF data structure. */
2985 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2986 static INLINE file_ptr
2987 align_file_position (off
, align
)
2991 return (off
+ align
- 1) & ~(align
- 1);
2994 /* Assign a file position to a section, optionally aligning to the
2995 required section alignment. */
2998 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2999 Elf_Internal_Shdr
*i_shdrp
;
3007 al
= i_shdrp
->sh_addralign
;
3009 offset
= BFD_ALIGN (offset
, al
);
3011 i_shdrp
->sh_offset
= offset
;
3012 if (i_shdrp
->bfd_section
!= NULL
)
3013 i_shdrp
->bfd_section
->filepos
= offset
;
3014 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
3015 offset
+= i_shdrp
->sh_size
;
3019 /* Compute the file positions we are going to put the sections at, and
3020 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3021 is not NULL, this is being called by the ELF backend linker. */
3024 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
3026 struct bfd_link_info
*link_info
;
3028 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3030 struct bfd_strtab_hash
*strtab
;
3031 Elf_Internal_Shdr
*shstrtab_hdr
;
3033 if (abfd
->output_has_begun
)
3036 /* Do any elf backend specific processing first. */
3037 if (bed
->elf_backend_begin_write_processing
)
3038 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
3040 if (! prep_headers (abfd
))
3043 /* Post process the headers if necessary. */
3044 if (bed
->elf_backend_post_process_headers
)
3045 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
3048 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
3052 if (!assign_section_numbers (abfd
))
3055 /* The backend linker builds symbol table information itself. */
3056 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3058 /* Non-zero if doing a relocatable link. */
3059 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
3061 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
3065 if (link_info
== NULL
)
3067 bfd_map_over_sections (abfd
, bfd_elf_set_group_contents
, &failed
);
3072 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
3073 /* sh_name was set in prep_headers. */
3074 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
3075 shstrtab_hdr
->sh_flags
= 0;
3076 shstrtab_hdr
->sh_addr
= 0;
3077 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
3078 shstrtab_hdr
->sh_entsize
= 0;
3079 shstrtab_hdr
->sh_link
= 0;
3080 shstrtab_hdr
->sh_info
= 0;
3081 /* sh_offset is set in assign_file_positions_except_relocs. */
3082 shstrtab_hdr
->sh_addralign
= 1;
3084 if (!assign_file_positions_except_relocs (abfd
))
3087 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
3090 Elf_Internal_Shdr
*hdr
;
3092 off
= elf_tdata (abfd
)->next_file_pos
;
3094 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3095 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3097 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
3098 if (hdr
->sh_size
!= 0)
3099 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3101 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
3102 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3104 elf_tdata (abfd
)->next_file_pos
= off
;
3106 /* Now that we know where the .strtab section goes, write it
3108 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
3109 || ! _bfd_stringtab_emit (abfd
, strtab
))
3111 _bfd_stringtab_free (strtab
);
3114 abfd
->output_has_begun
= true;
3119 /* Create a mapping from a set of sections to a program segment. */
3121 static INLINE
struct elf_segment_map
*
3122 make_mapping (abfd
, sections
, from
, to
, phdr
)
3124 asection
**sections
;
3129 struct elf_segment_map
*m
;
3134 amt
= sizeof (struct elf_segment_map
);
3135 amt
+= (to
- from
- 1) * sizeof (asection
*);
3136 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3140 m
->p_type
= PT_LOAD
;
3141 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
3142 m
->sections
[i
- from
] = *hdrpp
;
3143 m
->count
= to
- from
;
3145 if (from
== 0 && phdr
)
3147 /* Include the headers in the first PT_LOAD segment. */
3148 m
->includes_filehdr
= 1;
3149 m
->includes_phdrs
= 1;
3155 /* Set up a mapping from BFD sections to program segments. */
3158 map_sections_to_segments (abfd
)
3161 asection
**sections
= NULL
;
3165 struct elf_segment_map
*mfirst
;
3166 struct elf_segment_map
**pm
;
3167 struct elf_segment_map
*m
;
3169 unsigned int phdr_index
;
3170 bfd_vma maxpagesize
;
3172 boolean phdr_in_segment
= true;
3175 asection
*first_tls
= NULL
;
3176 asection
*dynsec
, *eh_frame_hdr
;
3179 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3182 if (bfd_count_sections (abfd
) == 0)
3185 /* Select the allocated sections, and sort them. */
3187 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3188 sections
= (asection
**) bfd_malloc (amt
);
3189 if (sections
== NULL
)
3193 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3195 if ((s
->flags
& SEC_ALLOC
) != 0)
3201 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3204 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3206 /* Build the mapping. */
3211 /* If we have a .interp section, then create a PT_PHDR segment for
3212 the program headers and a PT_INTERP segment for the .interp
3214 s
= bfd_get_section_by_name (abfd
, ".interp");
3215 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3217 amt
= sizeof (struct elf_segment_map
);
3218 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3222 m
->p_type
= PT_PHDR
;
3223 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3224 m
->p_flags
= PF_R
| PF_X
;
3225 m
->p_flags_valid
= 1;
3226 m
->includes_phdrs
= 1;
3231 amt
= sizeof (struct elf_segment_map
);
3232 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3236 m
->p_type
= PT_INTERP
;
3244 /* Look through the sections. We put sections in the same program
3245 segment when the start of the second section can be placed within
3246 a few bytes of the end of the first section. */
3249 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3251 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3253 && (dynsec
->flags
& SEC_LOAD
) == 0)
3256 /* Deal with -Ttext or something similar such that the first section
3257 is not adjacent to the program headers. This is an
3258 approximation, since at this point we don't know exactly how many
3259 program headers we will need. */
3262 bfd_size_type phdr_size
;
3264 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3266 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3267 if ((abfd
->flags
& D_PAGED
) == 0
3268 || sections
[0]->lma
< phdr_size
3269 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3270 phdr_in_segment
= false;
3273 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3276 boolean new_segment
;
3280 /* See if this section and the last one will fit in the same
3283 if (last_hdr
== NULL
)
3285 /* If we don't have a segment yet, then we don't need a new
3286 one (we build the last one after this loop). */
3287 new_segment
= false;
3289 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3291 /* If this section has a different relation between the
3292 virtual address and the load address, then we need a new
3296 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3297 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3299 /* If putting this section in this segment would force us to
3300 skip a page in the segment, then we need a new segment. */
3303 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3304 && (hdr
->flags
& SEC_LOAD
) != 0)
3306 /* We don't want to put a loadable section after a
3307 nonloadable section in the same segment. */
3310 else if ((abfd
->flags
& D_PAGED
) == 0)
3312 /* If the file is not demand paged, which means that we
3313 don't require the sections to be correctly aligned in the
3314 file, then there is no other reason for a new segment. */
3315 new_segment
= false;
3318 && (hdr
->flags
& SEC_READONLY
) == 0
3319 && (((last_hdr
->lma
+ last_hdr
->_raw_size
- 1)
3320 & ~(maxpagesize
- 1))
3321 != (hdr
->lma
& ~(maxpagesize
- 1))))
3323 /* We don't want to put a writable section in a read only
3324 segment, unless they are on the same page in memory
3325 anyhow. We already know that the last section does not
3326 bring us past the current section on the page, so the
3327 only case in which the new section is not on the same
3328 page as the previous section is when the previous section
3329 ends precisely on a page boundary. */
3334 /* Otherwise, we can use the same segment. */
3335 new_segment
= false;
3340 if ((hdr
->flags
& SEC_READONLY
) == 0)
3346 /* We need a new program segment. We must create a new program
3347 header holding all the sections from phdr_index until hdr. */
3349 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3356 if ((hdr
->flags
& SEC_READONLY
) == 0)
3363 phdr_in_segment
= false;
3366 /* Create a final PT_LOAD program segment. */
3367 if (last_hdr
!= NULL
)
3369 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3377 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3380 amt
= sizeof (struct elf_segment_map
);
3381 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3385 m
->p_type
= PT_DYNAMIC
;
3387 m
->sections
[0] = dynsec
;
3393 /* For each loadable .note section, add a PT_NOTE segment. We don't
3394 use bfd_get_section_by_name, because if we link together
3395 nonloadable .note sections and loadable .note sections, we will
3396 generate two .note sections in the output file. FIXME: Using
3397 names for section types is bogus anyhow. */
3398 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3400 if ((s
->flags
& SEC_LOAD
) != 0
3401 && strncmp (s
->name
, ".note", 5) == 0)
3403 amt
= sizeof (struct elf_segment_map
);
3404 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3408 m
->p_type
= PT_NOTE
;
3415 if (s
->flags
& SEC_THREAD_LOCAL
)
3423 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3428 amt
= sizeof (struct elf_segment_map
);
3429 amt
+= (tls_count
- 1) * sizeof (asection
*);
3430 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3435 m
->count
= tls_count
;
3436 /* Mandated PF_R. */
3438 m
->p_flags_valid
= 1;
3439 for (i
= 0; i
< tls_count
; ++i
)
3441 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3442 m
->sections
[i
] = first_tls
;
3443 first_tls
= first_tls
->next
;
3450 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3452 eh_frame_hdr
= NULL
;
3453 if (elf_tdata (abfd
)->eh_frame_hdr
)
3454 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3455 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3457 amt
= sizeof (struct elf_segment_map
);
3458 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3462 m
->p_type
= PT_GNU_EH_FRAME
;
3464 m
->sections
[0] = eh_frame_hdr
;
3473 elf_tdata (abfd
)->segment_map
= mfirst
;
3477 if (sections
!= NULL
)
3482 /* Sort sections by address. */
3485 elf_sort_sections (arg1
, arg2
)
3489 const asection
*sec1
= *(const asection
**) arg1
;
3490 const asection
*sec2
= *(const asection
**) arg2
;
3492 /* Sort by LMA first, since this is the address used to
3493 place the section into a segment. */
3494 if (sec1
->lma
< sec2
->lma
)
3496 else if (sec1
->lma
> sec2
->lma
)
3499 /* Then sort by VMA. Normally the LMA and the VMA will be
3500 the same, and this will do nothing. */
3501 if (sec1
->vma
< sec2
->vma
)
3503 else if (sec1
->vma
> sec2
->vma
)
3506 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3508 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3514 /* If the indicies are the same, do not return 0
3515 here, but continue to try the next comparison. */
3516 if (sec1
->target_index
- sec2
->target_index
!= 0)
3517 return sec1
->target_index
- sec2
->target_index
;
3522 else if (TOEND (sec2
))
3527 /* Sort by size, to put zero sized sections
3528 before others at the same address. */
3530 if (sec1
->_raw_size
< sec2
->_raw_size
)
3532 if (sec1
->_raw_size
> sec2
->_raw_size
)
3535 return sec1
->target_index
- sec2
->target_index
;
3538 /* Assign file positions to the sections based on the mapping from
3539 sections to segments. This function also sets up some fields in
3540 the file header, and writes out the program headers. */
3543 assign_file_positions_for_segments (abfd
)
3546 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3548 struct elf_segment_map
*m
;
3550 Elf_Internal_Phdr
*phdrs
;
3552 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3553 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3554 Elf_Internal_Phdr
*p
;
3557 if (elf_tdata (abfd
)->segment_map
== NULL
)
3559 if (! map_sections_to_segments (abfd
))
3564 /* The placement algorithm assumes that non allocated sections are
3565 not in PT_LOAD segments. We ensure this here by removing such
3566 sections from the segment map. */
3567 for (m
= elf_tdata (abfd
)->segment_map
;
3571 unsigned int new_count
;
3574 if (m
->p_type
!= PT_LOAD
)
3578 for (i
= 0; i
< m
->count
; i
++)
3580 if ((m
->sections
[i
]->flags
& SEC_ALLOC
) != 0)
3583 m
->sections
[new_count
] = m
->sections
[i
];
3589 if (new_count
!= m
->count
)
3590 m
->count
= new_count
;
3594 if (bed
->elf_backend_modify_segment_map
)
3596 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3601 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3604 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3605 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3606 elf_elfheader (abfd
)->e_phnum
= count
;
3611 /* If we already counted the number of program segments, make sure
3612 that we allocated enough space. This happens when SIZEOF_HEADERS
3613 is used in a linker script. */
3614 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3615 if (alloc
!= 0 && count
> alloc
)
3617 ((*_bfd_error_handler
)
3618 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3619 bfd_get_filename (abfd
), alloc
, count
));
3620 bfd_set_error (bfd_error_bad_value
);
3627 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3628 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3632 off
= bed
->s
->sizeof_ehdr
;
3633 off
+= alloc
* bed
->s
->sizeof_phdr
;
3640 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3647 /* If elf_segment_map is not from map_sections_to_segments, the
3648 sections may not be correctly ordered. NOTE: sorting should
3649 not be done to the PT_NOTE section of a corefile, which may
3650 contain several pseudo-sections artificially created by bfd.
3651 Sorting these pseudo-sections breaks things badly. */
3653 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3654 && m
->p_type
== PT_NOTE
))
3655 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3658 p
->p_type
= m
->p_type
;
3659 p
->p_flags
= m
->p_flags
;
3661 if (p
->p_type
== PT_LOAD
3663 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3665 if ((abfd
->flags
& D_PAGED
) != 0)
3666 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3669 bfd_size_type align
;
3672 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3674 bfd_size_type secalign
;
3676 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3677 if (secalign
> align
)
3681 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3688 p
->p_vaddr
= m
->sections
[0]->vma
;
3690 if (m
->p_paddr_valid
)
3691 p
->p_paddr
= m
->p_paddr
;
3692 else if (m
->count
== 0)
3695 p
->p_paddr
= m
->sections
[0]->lma
;
3697 if (p
->p_type
== PT_LOAD
3698 && (abfd
->flags
& D_PAGED
) != 0)
3699 p
->p_align
= bed
->maxpagesize
;
3700 else if (m
->count
== 0)
3701 p
->p_align
= bed
->s
->file_align
;
3709 if (m
->includes_filehdr
)
3711 if (! m
->p_flags_valid
)
3714 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3715 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3718 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3720 if (p
->p_vaddr
< (bfd_vma
) off
)
3722 (*_bfd_error_handler
)
3723 (_("%s: Not enough room for program headers, try linking with -N"),
3724 bfd_get_filename (abfd
));
3725 bfd_set_error (bfd_error_bad_value
);
3730 if (! m
->p_paddr_valid
)
3733 if (p
->p_type
== PT_LOAD
)
3735 filehdr_vaddr
= p
->p_vaddr
;
3736 filehdr_paddr
= p
->p_paddr
;
3740 if (m
->includes_phdrs
)
3742 if (! m
->p_flags_valid
)
3745 if (m
->includes_filehdr
)
3747 if (p
->p_type
== PT_LOAD
)
3749 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3750 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3755 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3759 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3760 p
->p_vaddr
-= off
- p
->p_offset
;
3761 if (! m
->p_paddr_valid
)
3762 p
->p_paddr
-= off
- p
->p_offset
;
3765 if (p
->p_type
== PT_LOAD
)
3767 phdrs_vaddr
= p
->p_vaddr
;
3768 phdrs_paddr
= p
->p_paddr
;
3771 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3774 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3775 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3778 if (p
->p_type
== PT_LOAD
3779 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3781 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3787 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3788 p
->p_filesz
+= adjust
;
3789 p
->p_memsz
+= adjust
;
3795 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3799 bfd_size_type align
;
3803 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3805 /* The section may have artificial alignment forced by a
3806 link script. Notice this case by the gap between the
3807 cumulative phdr lma and the section's lma. */
3808 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3810 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3812 p
->p_memsz
+= adjust
;
3815 if ((flags
& SEC_LOAD
) != 0)
3816 p
->p_filesz
+= adjust
;
3819 if (p
->p_type
== PT_LOAD
)
3821 bfd_signed_vma adjust
;
3823 if ((flags
& SEC_LOAD
) != 0)
3825 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3829 else if ((flags
& SEC_ALLOC
) != 0)
3831 /* The section VMA must equal the file position
3832 modulo the page size. FIXME: I'm not sure if
3833 this adjustment is really necessary. We used to
3834 not have the SEC_LOAD case just above, and then
3835 this was necessary, but now I'm not sure. */
3836 if ((abfd
->flags
& D_PAGED
) != 0)
3837 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3839 adjust
= (sec
->vma
- voff
) % align
;
3848 (* _bfd_error_handler
) (_("\
3849 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3850 bfd_section_name (abfd
, sec
),
3855 p
->p_memsz
+= adjust
;
3858 if ((flags
& SEC_LOAD
) != 0)
3859 p
->p_filesz
+= adjust
;
3864 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3865 used in a linker script we may have a section with
3866 SEC_LOAD clear but which is supposed to have
3868 if ((flags
& SEC_LOAD
) != 0
3869 || (flags
& SEC_HAS_CONTENTS
) != 0)
3870 off
+= sec
->_raw_size
;
3872 if ((flags
& SEC_ALLOC
) != 0)
3873 voff
+= sec
->_raw_size
;
3876 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3878 /* The actual "note" segment has i == 0.
3879 This is the one that actually contains everything. */
3883 p
->p_filesz
= sec
->_raw_size
;
3884 off
+= sec
->_raw_size
;
3889 /* Fake sections -- don't need to be written. */
3892 flags
= sec
->flags
= 0;
3899 p
->p_memsz
+= sec
->_raw_size
;
3901 if ((flags
& SEC_LOAD
) != 0)
3902 p
->p_filesz
+= sec
->_raw_size
;
3904 if (p
->p_type
== PT_TLS
3905 && sec
->_raw_size
== 0
3906 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3908 struct bfd_link_order
*o
;
3909 bfd_vma tbss_size
= 0;
3911 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3912 if (tbss_size
< o
->offset
+ o
->size
)
3913 tbss_size
= o
->offset
+ o
->size
;
3915 p
->p_memsz
+= tbss_size
;
3918 if (align
> p
->p_align
3919 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3923 if (! m
->p_flags_valid
)
3926 if ((flags
& SEC_CODE
) != 0)
3928 if ((flags
& SEC_READONLY
) == 0)
3934 /* Now that we have set the section file positions, we can set up
3935 the file positions for the non PT_LOAD segments. */
3936 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3940 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3942 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3943 p
->p_offset
= m
->sections
[0]->filepos
;
3947 if (m
->includes_filehdr
)
3949 p
->p_vaddr
= filehdr_vaddr
;
3950 if (! m
->p_paddr_valid
)
3951 p
->p_paddr
= filehdr_paddr
;
3953 else if (m
->includes_phdrs
)
3955 p
->p_vaddr
= phdrs_vaddr
;
3956 if (! m
->p_paddr_valid
)
3957 p
->p_paddr
= phdrs_paddr
;
3962 /* Clear out any program headers we allocated but did not use. */
3963 for (; count
< alloc
; count
++, p
++)
3965 memset (p
, 0, sizeof *p
);
3966 p
->p_type
= PT_NULL
;
3969 elf_tdata (abfd
)->phdr
= phdrs
;
3971 elf_tdata (abfd
)->next_file_pos
= off
;
3973 /* Write out the program headers. */
3974 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3975 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3981 /* Get the size of the program header.
3983 If this is called by the linker before any of the section VMA's are set, it
3984 can't calculate the correct value for a strange memory layout. This only
3985 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3986 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3987 data segment (exclusive of .interp and .dynamic).
3989 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3990 will be two segments. */
3992 static bfd_size_type
3993 get_program_header_size (abfd
)
3998 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4000 /* We can't return a different result each time we're called. */
4001 if (elf_tdata (abfd
)->program_header_size
!= 0)
4002 return elf_tdata (abfd
)->program_header_size
;
4004 if (elf_tdata (abfd
)->segment_map
!= NULL
)
4006 struct elf_segment_map
*m
;
4009 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
4011 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4012 return elf_tdata (abfd
)->program_header_size
;
4015 /* Assume we will need exactly two PT_LOAD segments: one for text
4016 and one for data. */
4019 s
= bfd_get_section_by_name (abfd
, ".interp");
4020 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
4022 /* If we have a loadable interpreter section, we need a
4023 PT_INTERP segment. In this case, assume we also need a
4024 PT_PHDR segment, although that may not be true for all
4029 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
4031 /* We need a PT_DYNAMIC segment. */
4035 if (elf_tdata (abfd
)->eh_frame_hdr
4036 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
4038 /* We need a PT_GNU_EH_FRAME segment. */
4042 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4044 if ((s
->flags
& SEC_LOAD
) != 0
4045 && strncmp (s
->name
, ".note", 5) == 0)
4047 /* We need a PT_NOTE segment. */
4052 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4054 if (s
->flags
& SEC_THREAD_LOCAL
)
4056 /* We need a PT_TLS segment. */
4062 /* Let the backend count up any program headers it might need. */
4063 if (bed
->elf_backend_additional_program_headers
)
4067 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
4073 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
4074 return elf_tdata (abfd
)->program_header_size
;
4077 /* Work out the file positions of all the sections. This is called by
4078 _bfd_elf_compute_section_file_positions. All the section sizes and
4079 VMAs must be known before this is called.
4081 We do not consider reloc sections at this point, unless they form
4082 part of the loadable image. Reloc sections are assigned file
4083 positions in assign_file_positions_for_relocs, which is called by
4084 write_object_contents and final_link.
4086 We also don't set the positions of the .symtab and .strtab here. */
4089 assign_file_positions_except_relocs (abfd
)
4092 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
4093 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
4094 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
4095 unsigned int num_sec
= elf_numsections (abfd
);
4097 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4099 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
4100 && bfd_get_format (abfd
) != bfd_core
)
4102 Elf_Internal_Shdr
**hdrpp
;
4105 /* Start after the ELF header. */
4106 off
= i_ehdrp
->e_ehsize
;
4108 /* We are not creating an executable, which means that we are
4109 not creating a program header, and that the actual order of
4110 the sections in the file is unimportant. */
4111 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4113 Elf_Internal_Shdr
*hdr
;
4116 if (hdr
->sh_type
== SHT_REL
4117 || hdr
->sh_type
== SHT_RELA
4118 || i
== tdata
->symtab_section
4119 || i
== tdata
->symtab_shndx_section
4120 || i
== tdata
->strtab_section
)
4122 hdr
->sh_offset
= -1;
4125 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4127 if (i
== SHN_LORESERVE
- 1)
4129 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4130 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4137 Elf_Internal_Shdr
**hdrpp
;
4139 /* Assign file positions for the loaded sections based on the
4140 assignment of sections to segments. */
4141 if (! assign_file_positions_for_segments (abfd
))
4144 /* Assign file positions for the other sections. */
4146 off
= elf_tdata (abfd
)->next_file_pos
;
4147 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
4149 Elf_Internal_Shdr
*hdr
;
4152 if (hdr
->bfd_section
!= NULL
4153 && hdr
->bfd_section
->filepos
!= 0)
4154 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
4155 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
4157 ((*_bfd_error_handler
)
4158 (_("%s: warning: allocated section `%s' not in segment"),
4159 bfd_get_filename (abfd
),
4160 (hdr
->bfd_section
== NULL
4162 : hdr
->bfd_section
->name
)));
4163 if ((abfd
->flags
& D_PAGED
) != 0)
4164 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
4166 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
4167 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
4170 else if (hdr
->sh_type
== SHT_REL
4171 || hdr
->sh_type
== SHT_RELA
4172 || hdr
== i_shdrpp
[tdata
->symtab_section
]
4173 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
4174 || hdr
== i_shdrpp
[tdata
->strtab_section
])
4175 hdr
->sh_offset
= -1;
4177 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
4179 if (i
== SHN_LORESERVE
- 1)
4181 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4182 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4187 /* Place the section headers. */
4188 off
= align_file_position (off
, bed
->s
->file_align
);
4189 i_ehdrp
->e_shoff
= off
;
4190 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4192 elf_tdata (abfd
)->next_file_pos
= off
;
4201 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4202 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4203 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4204 struct elf_strtab_hash
*shstrtab
;
4205 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4207 i_ehdrp
= elf_elfheader (abfd
);
4208 i_shdrp
= elf_elfsections (abfd
);
4210 shstrtab
= _bfd_elf_strtab_init ();
4211 if (shstrtab
== NULL
)
4214 elf_shstrtab (abfd
) = shstrtab
;
4216 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4217 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4218 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4219 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4221 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4222 i_ehdrp
->e_ident
[EI_DATA
] =
4223 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4224 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4226 if ((abfd
->flags
& DYNAMIC
) != 0)
4227 i_ehdrp
->e_type
= ET_DYN
;
4228 else if ((abfd
->flags
& EXEC_P
) != 0)
4229 i_ehdrp
->e_type
= ET_EXEC
;
4230 else if (bfd_get_format (abfd
) == bfd_core
)
4231 i_ehdrp
->e_type
= ET_CORE
;
4233 i_ehdrp
->e_type
= ET_REL
;
4235 switch (bfd_get_arch (abfd
))
4237 case bfd_arch_unknown
:
4238 i_ehdrp
->e_machine
= EM_NONE
;
4241 /* There used to be a long list of cases here, each one setting
4242 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4243 in the corresponding bfd definition. To avoid duplication,
4244 the switch was removed. Machines that need special handling
4245 can generally do it in elf_backend_final_write_processing(),
4246 unless they need the information earlier than the final write.
4247 Such need can generally be supplied by replacing the tests for
4248 e_machine with the conditions used to determine it. */
4250 if (get_elf_backend_data (abfd
) != NULL
)
4251 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
4253 i_ehdrp
->e_machine
= EM_NONE
;
4256 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4257 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4259 /* No program header, for now. */
4260 i_ehdrp
->e_phoff
= 0;
4261 i_ehdrp
->e_phentsize
= 0;
4262 i_ehdrp
->e_phnum
= 0;
4264 /* Each bfd section is section header entry. */
4265 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4266 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4268 /* If we're building an executable, we'll need a program header table. */
4269 if (abfd
->flags
& EXEC_P
)
4271 /* It all happens later. */
4273 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4275 /* elf_build_phdrs() returns a (NULL-terminated) array of
4276 Elf_Internal_Phdrs. */
4277 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4278 i_ehdrp
->e_phoff
= outbase
;
4279 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4284 i_ehdrp
->e_phentsize
= 0;
4286 i_ehdrp
->e_phoff
= 0;
4289 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4290 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
4291 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4292 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
4293 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4294 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
4295 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4296 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4297 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4303 /* Assign file positions for all the reloc sections which are not part
4304 of the loadable file image. */
4307 _bfd_elf_assign_file_positions_for_relocs (abfd
)
4311 unsigned int i
, num_sec
;
4312 Elf_Internal_Shdr
**shdrpp
;
4314 off
= elf_tdata (abfd
)->next_file_pos
;
4316 num_sec
= elf_numsections (abfd
);
4317 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4319 Elf_Internal_Shdr
*shdrp
;
4322 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4323 && shdrp
->sh_offset
== -1)
4324 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
4327 elf_tdata (abfd
)->next_file_pos
= off
;
4331 _bfd_elf_write_object_contents (abfd
)
4334 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4335 Elf_Internal_Ehdr
*i_ehdrp
;
4336 Elf_Internal_Shdr
**i_shdrp
;
4338 unsigned int count
, num_sec
;
4340 if (! abfd
->output_has_begun
4341 && ! _bfd_elf_compute_section_file_positions
4342 (abfd
, (struct bfd_link_info
*) NULL
))
4345 i_shdrp
= elf_elfsections (abfd
);
4346 i_ehdrp
= elf_elfheader (abfd
);
4349 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4353 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4355 /* After writing the headers, we need to write the sections too... */
4356 num_sec
= elf_numsections (abfd
);
4357 for (count
= 1; count
< num_sec
; count
++)
4359 if (bed
->elf_backend_section_processing
)
4360 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4361 if (i_shdrp
[count
]->contents
)
4363 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4365 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4366 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4369 if (count
== SHN_LORESERVE
- 1)
4370 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4373 /* Write out the section header names. */
4374 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4375 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4378 if (bed
->elf_backend_final_write_processing
)
4379 (*bed
->elf_backend_final_write_processing
) (abfd
,
4380 elf_tdata (abfd
)->linker
);
4382 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4386 _bfd_elf_write_corefile_contents (abfd
)
4389 /* Hopefully this can be done just like an object file. */
4390 return _bfd_elf_write_object_contents (abfd
);
4393 /* Given a section, search the header to find them. */
4396 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4400 struct elf_backend_data
*bed
;
4403 if (elf_section_data (asect
) != NULL
4404 && elf_section_data (asect
)->this_idx
!= 0)
4405 return elf_section_data (asect
)->this_idx
;
4407 if (bfd_is_abs_section (asect
))
4409 else if (bfd_is_com_section (asect
))
4411 else if (bfd_is_und_section (asect
))
4415 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4416 int maxindex
= elf_numsections (abfd
);
4418 for (index
= 1; index
< maxindex
; index
++)
4420 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4422 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4428 bed
= get_elf_backend_data (abfd
);
4429 if (bed
->elf_backend_section_from_bfd_section
)
4433 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4438 bfd_set_error (bfd_error_nonrepresentable_section
);
4443 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4447 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4449 asymbol
**asym_ptr_ptr
;
4451 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4453 flagword flags
= asym_ptr
->flags
;
4455 /* When gas creates relocations against local labels, it creates its
4456 own symbol for the section, but does put the symbol into the
4457 symbol chain, so udata is 0. When the linker is generating
4458 relocatable output, this section symbol may be for one of the
4459 input sections rather than the output section. */
4460 if (asym_ptr
->udata
.i
== 0
4461 && (flags
& BSF_SECTION_SYM
)
4462 && asym_ptr
->section
)
4466 if (asym_ptr
->section
->output_section
!= NULL
)
4467 indx
= asym_ptr
->section
->output_section
->index
;
4469 indx
= asym_ptr
->section
->index
;
4470 if (indx
< elf_num_section_syms (abfd
)
4471 && elf_section_syms (abfd
)[indx
] != NULL
)
4472 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4475 idx
= asym_ptr
->udata
.i
;
4479 /* This case can occur when using --strip-symbol on a symbol
4480 which is used in a relocation entry. */
4481 (*_bfd_error_handler
)
4482 (_("%s: symbol `%s' required but not present"),
4483 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4484 bfd_set_error (bfd_error_no_symbols
);
4491 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4492 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4493 elf_symbol_flags (flags
));
4501 /* Copy private BFD data. This copies any program header information. */
4504 copy_private_bfd_data (ibfd
, obfd
)
4508 Elf_Internal_Ehdr
* iehdr
;
4509 struct elf_segment_map
* map
;
4510 struct elf_segment_map
* map_first
;
4511 struct elf_segment_map
** pointer_to_map
;
4512 Elf_Internal_Phdr
* segment
;
4515 unsigned int num_segments
;
4516 boolean phdr_included
= false;
4517 bfd_vma maxpagesize
;
4518 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4519 unsigned int phdr_adjust_num
= 0;
4520 struct elf_backend_data
* bed
;
4522 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4523 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4526 if (elf_tdata (ibfd
)->phdr
== NULL
)
4529 bed
= get_elf_backend_data (ibfd
);
4530 iehdr
= elf_elfheader (ibfd
);
4533 pointer_to_map
= &map_first
;
4535 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4536 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4538 /* Returns the end address of the segment + 1. */
4539 #define SEGMENT_END(segment, start) \
4540 (start + (segment->p_memsz > segment->p_filesz \
4541 ? segment->p_memsz : segment->p_filesz))
4543 /* Returns true if the given section is contained within
4544 the given segment. VMA addresses are compared. */
4545 #define IS_CONTAINED_BY_VMA(section, segment) \
4546 (section->vma >= segment->p_vaddr \
4547 && (section->vma + section->_raw_size \
4548 <= (SEGMENT_END (segment, segment->p_vaddr))))
4550 /* Returns true if the given section is contained within
4551 the given segment. LMA addresses are compared. */
4552 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4553 (section->lma >= base \
4554 && (section->lma + section->_raw_size \
4555 <= SEGMENT_END (segment, base)))
4557 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4558 #define IS_COREFILE_NOTE(p, s) \
4559 (p->p_type == PT_NOTE \
4560 && bfd_get_format (ibfd) == bfd_core \
4561 && s->vma == 0 && s->lma == 0 \
4562 && (bfd_vma) s->filepos >= p->p_offset \
4563 && ((bfd_vma) s->filepos + s->_raw_size \
4564 <= p->p_offset + p->p_filesz))
4566 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4567 linker, which generates a PT_INTERP section with p_vaddr and
4568 p_memsz set to 0. */
4569 #define IS_SOLARIS_PT_INTERP(p, s) \
4571 && p->p_paddr == 0 \
4572 && p->p_memsz == 0 \
4573 && p->p_filesz > 0 \
4574 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4575 && s->_raw_size > 0 \
4576 && (bfd_vma) s->filepos >= p->p_offset \
4577 && ((bfd_vma) s->filepos + s->_raw_size \
4578 <= p->p_offset + p->p_filesz))
4580 /* Decide if the given section should be included in the given segment.
4581 A section will be included if:
4582 1. It is within the address space of the segment -- we use the LMA
4583 if that is set for the segment and the VMA otherwise,
4584 2. It is an allocated segment,
4585 3. There is an output section associated with it,
4586 4. The section has not already been allocated to a previous segment. */
4587 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4588 ((((segment->p_paddr \
4589 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4590 : IS_CONTAINED_BY_VMA (section, segment)) \
4591 && (section->flags & SEC_ALLOC) != 0) \
4592 || IS_COREFILE_NOTE (segment, section)) \
4593 && section->output_section != NULL \
4594 && ! section->segment_mark)
4596 /* Returns true iff seg1 starts after the end of seg2. */
4597 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4598 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4600 /* Returns true iff seg1 and seg2 overlap. */
4601 #define SEGMENT_OVERLAPS(seg1, seg2) \
4602 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) \
4603 || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4605 /* Initialise the segment mark field. */
4606 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4607 section
->segment_mark
= false;
4609 /* Scan through the segments specified in the program header
4610 of the input BFD. For this first scan we look for overlaps
4611 in the loadable segments. These can be created by weird
4612 parameters to objcopy. Also, fix some solaris weirdness. */
4613 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4618 Elf_Internal_Phdr
*segment2
;
4620 if (segment
->p_type
== PT_INTERP
)
4621 for (section
= ibfd
->sections
; section
; section
= section
->next
)
4622 if (IS_SOLARIS_PT_INTERP (segment
, section
))
4624 /* Mininal change so that the normal section to segment
4625 assigment code will work. */
4626 segment
->p_vaddr
= section
->vma
;
4630 if (segment
->p_type
!= PT_LOAD
)
4633 /* Determine if this segment overlaps any previous segments. */
4634 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4636 bfd_signed_vma extra_length
;
4638 if (segment2
->p_type
!= PT_LOAD
4639 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4642 /* Merge the two segments together. */
4643 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4645 /* Extend SEGMENT2 to include SEGMENT and then delete
4648 SEGMENT_END (segment
, segment
->p_vaddr
)
4649 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4651 if (extra_length
> 0)
4653 segment2
->p_memsz
+= extra_length
;
4654 segment2
->p_filesz
+= extra_length
;
4657 segment
->p_type
= PT_NULL
;
4659 /* Since we have deleted P we must restart the outer loop. */
4661 segment
= elf_tdata (ibfd
)->phdr
;
4666 /* Extend SEGMENT to include SEGMENT2 and then delete
4669 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4670 - SEGMENT_END (segment
, segment
->p_vaddr
);
4672 if (extra_length
> 0)
4674 segment
->p_memsz
+= extra_length
;
4675 segment
->p_filesz
+= extra_length
;
4678 segment2
->p_type
= PT_NULL
;
4683 /* The second scan attempts to assign sections to segments. */
4684 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4688 unsigned int section_count
;
4689 asection
** sections
;
4690 asection
* output_section
;
4692 bfd_vma matching_lma
;
4693 bfd_vma suggested_lma
;
4697 if (segment
->p_type
== PT_NULL
)
4700 /* Compute how many sections might be placed into this segment. */
4702 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4703 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4706 /* Allocate a segment map big enough to contain all of the
4707 sections we have selected. */
4708 amt
= sizeof (struct elf_segment_map
);
4709 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4710 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4714 /* Initialise the fields of the segment map. Default to
4715 using the physical address of the segment in the input BFD. */
4717 map
->p_type
= segment
->p_type
;
4718 map
->p_flags
= segment
->p_flags
;
4719 map
->p_flags_valid
= 1;
4720 map
->p_paddr
= segment
->p_paddr
;
4721 map
->p_paddr_valid
= 1;
4723 /* Determine if this segment contains the ELF file header
4724 and if it contains the program headers themselves. */
4725 map
->includes_filehdr
= (segment
->p_offset
== 0
4726 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4728 map
->includes_phdrs
= 0;
4730 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4732 map
->includes_phdrs
=
4733 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4734 && (segment
->p_offset
+ segment
->p_filesz
4735 >= ((bfd_vma
) iehdr
->e_phoff
4736 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4738 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4739 phdr_included
= true;
4742 if (section_count
== 0)
4744 /* Special segments, such as the PT_PHDR segment, may contain
4745 no sections, but ordinary, loadable segments should contain
4746 something. They are allowed by the ELF spec however, so only
4747 a warning is produced. */
4748 if (segment
->p_type
== PT_LOAD
)
4749 (*_bfd_error_handler
)
4750 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4751 bfd_archive_filename (ibfd
));
4754 *pointer_to_map
= map
;
4755 pointer_to_map
= &map
->next
;
4760 /* Now scan the sections in the input BFD again and attempt
4761 to add their corresponding output sections to the segment map.
4762 The problem here is how to handle an output section which has
4763 been moved (ie had its LMA changed). There are four possibilities:
4765 1. None of the sections have been moved.
4766 In this case we can continue to use the segment LMA from the
4769 2. All of the sections have been moved by the same amount.
4770 In this case we can change the segment's LMA to match the LMA
4771 of the first section.
4773 3. Some of the sections have been moved, others have not.
4774 In this case those sections which have not been moved can be
4775 placed in the current segment which will have to have its size,
4776 and possibly its LMA changed, and a new segment or segments will
4777 have to be created to contain the other sections.
4779 4. The sections have been moved, but not be the same amount.
4780 In this case we can change the segment's LMA to match the LMA
4781 of the first section and we will have to create a new segment
4782 or segments to contain the other sections.
4784 In order to save time, we allocate an array to hold the section
4785 pointers that we are interested in. As these sections get assigned
4786 to a segment, they are removed from this array. */
4788 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4789 to work around this long long bug. */
4790 amt
= section_count
* sizeof (asection
*);
4791 sections
= (asection
**) bfd_malloc (amt
);
4792 if (sections
== NULL
)
4795 /* Step One: Scan for segment vs section LMA conflicts.
4796 Also add the sections to the section array allocated above.
4797 Also add the sections to the current segment. In the common
4798 case, where the sections have not been moved, this means that
4799 we have completely filled the segment, and there is nothing
4805 for (j
= 0, section
= ibfd
->sections
;
4807 section
= section
->next
)
4809 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4811 output_section
= section
->output_section
;
4813 sections
[j
++] = section
;
4815 /* The Solaris native linker always sets p_paddr to 0.
4816 We try to catch that case here, and set it to the
4817 correct value. Note - some backends require that
4818 p_paddr be left as zero. */
4819 if (segment
->p_paddr
== 0
4820 && segment
->p_vaddr
!= 0
4821 && (! bed
->want_p_paddr_set_to_zero
)
4823 && output_section
->lma
!= 0
4824 && (output_section
->vma
== (segment
->p_vaddr
4825 + (map
->includes_filehdr
4828 + (map
->includes_phdrs
4830 * iehdr
->e_phentsize
)
4832 map
->p_paddr
= segment
->p_vaddr
;
4834 /* Match up the physical address of the segment with the
4835 LMA address of the output section. */
4836 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4837 || IS_COREFILE_NOTE (segment
, section
)
4838 || (bed
->want_p_paddr_set_to_zero
&&
4839 IS_CONTAINED_BY_VMA (output_section
, segment
))
4842 if (matching_lma
== 0)
4843 matching_lma
= output_section
->lma
;
4845 /* We assume that if the section fits within the segment
4846 then it does not overlap any other section within that
4848 map
->sections
[isec
++] = output_section
;
4850 else if (suggested_lma
== 0)
4851 suggested_lma
= output_section
->lma
;
4855 BFD_ASSERT (j
== section_count
);
4857 /* Step Two: Adjust the physical address of the current segment,
4859 if (isec
== section_count
)
4861 /* All of the sections fitted within the segment as currently
4862 specified. This is the default case. Add the segment to
4863 the list of built segments and carry on to process the next
4864 program header in the input BFD. */
4865 map
->count
= section_count
;
4866 *pointer_to_map
= map
;
4867 pointer_to_map
= &map
->next
;
4874 if (matching_lma
!= 0)
4876 /* At least one section fits inside the current segment.
4877 Keep it, but modify its physical address to match the
4878 LMA of the first section that fitted. */
4879 map
->p_paddr
= matching_lma
;
4883 /* None of the sections fitted inside the current segment.
4884 Change the current segment's physical address to match
4885 the LMA of the first section. */
4886 map
->p_paddr
= suggested_lma
;
4889 /* Offset the segment physical address from the lma
4890 to allow for space taken up by elf headers. */
4891 if (map
->includes_filehdr
)
4892 map
->p_paddr
-= iehdr
->e_ehsize
;
4894 if (map
->includes_phdrs
)
4896 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4898 /* iehdr->e_phnum is just an estimate of the number
4899 of program headers that we will need. Make a note
4900 here of the number we used and the segment we chose
4901 to hold these headers, so that we can adjust the
4902 offset when we know the correct value. */
4903 phdr_adjust_num
= iehdr
->e_phnum
;
4904 phdr_adjust_seg
= map
;
4908 /* Step Three: Loop over the sections again, this time assigning
4909 those that fit to the current segment and removing them from the
4910 sections array; but making sure not to leave large gaps. Once all
4911 possible sections have been assigned to the current segment it is
4912 added to the list of built segments and if sections still remain
4913 to be assigned, a new segment is constructed before repeating
4921 /* Fill the current segment with sections that fit. */
4922 for (j
= 0; j
< section_count
; j
++)
4924 section
= sections
[j
];
4926 if (section
== NULL
)
4929 output_section
= section
->output_section
;
4931 BFD_ASSERT (output_section
!= NULL
);
4933 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4934 || IS_COREFILE_NOTE (segment
, section
))
4936 if (map
->count
== 0)
4938 /* If the first section in a segment does not start at
4939 the beginning of the segment, then something is
4941 if (output_section
->lma
!=
4943 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4944 + (map
->includes_phdrs
4945 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4951 asection
* prev_sec
;
4953 prev_sec
= map
->sections
[map
->count
- 1];
4955 /* If the gap between the end of the previous section
4956 and the start of this section is more than
4957 maxpagesize then we need to start a new segment. */
4958 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4960 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4961 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4962 > output_section
->lma
))
4964 if (suggested_lma
== 0)
4965 suggested_lma
= output_section
->lma
;
4971 map
->sections
[map
->count
++] = output_section
;
4974 section
->segment_mark
= true;
4976 else if (suggested_lma
== 0)
4977 suggested_lma
= output_section
->lma
;
4980 BFD_ASSERT (map
->count
> 0);
4982 /* Add the current segment to the list of built segments. */
4983 *pointer_to_map
= map
;
4984 pointer_to_map
= &map
->next
;
4986 if (isec
< section_count
)
4988 /* We still have not allocated all of the sections to
4989 segments. Create a new segment here, initialise it
4990 and carry on looping. */
4991 amt
= sizeof (struct elf_segment_map
);
4992 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4993 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4997 /* Initialise the fields of the segment map. Set the physical
4998 physical address to the LMA of the first section that has
4999 not yet been assigned. */
5001 map
->p_type
= segment
->p_type
;
5002 map
->p_flags
= segment
->p_flags
;
5003 map
->p_flags_valid
= 1;
5004 map
->p_paddr
= suggested_lma
;
5005 map
->p_paddr_valid
= 1;
5006 map
->includes_filehdr
= 0;
5007 map
->includes_phdrs
= 0;
5010 while (isec
< section_count
);
5015 /* The Solaris linker creates program headers in which all the
5016 p_paddr fields are zero. When we try to objcopy or strip such a
5017 file, we get confused. Check for this case, and if we find it
5018 reset the p_paddr_valid fields. */
5019 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5020 if (map
->p_paddr
!= 0)
5024 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
5025 map
->p_paddr_valid
= 0;
5028 elf_tdata (obfd
)->segment_map
= map_first
;
5030 /* If we had to estimate the number of program headers that were
5031 going to be needed, then check our estimate now and adjust
5032 the offset if necessary. */
5033 if (phdr_adjust_seg
!= NULL
)
5037 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
5040 if (count
> phdr_adjust_num
)
5041 phdr_adjust_seg
->p_paddr
5042 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
5046 /* Final Step: Sort the segments into ascending order of physical
5048 if (map_first
!= NULL
)
5050 struct elf_segment_map
*prev
;
5053 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
5055 /* Yes I know - its a bubble sort.... */
5056 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
5058 /* Swap map and map->next. */
5059 prev
->next
= map
->next
;
5060 map
->next
= map
->next
->next
;
5061 prev
->next
->next
= map
;
5071 #undef IS_CONTAINED_BY_VMA
5072 #undef IS_CONTAINED_BY_LMA
5073 #undef IS_COREFILE_NOTE
5074 #undef IS_SOLARIS_PT_INTERP
5075 #undef INCLUDE_SECTION_IN_SEGMENT
5076 #undef SEGMENT_AFTER_SEGMENT
5077 #undef SEGMENT_OVERLAPS
5081 /* Copy private section information. This copies over the entsize
5082 field, and sometimes the info field. */
5085 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
5091 Elf_Internal_Shdr
*ihdr
, *ohdr
;
5092 const struct elf_backend_data
*bed
= get_elf_backend_data (ibfd
);
5094 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
5095 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5098 /* Copy over private BFD data if it has not already been copied.
5099 This must be done here, rather than in the copy_private_bfd_data
5100 entry point, because the latter is called after the section
5101 contents have been set, which means that the program headers have
5102 already been worked out. The backend function provides a way to
5103 override the test conditions and code path for the call to
5104 copy_private_bfd_data. */
5105 if (bed
->copy_private_bfd_data_p
)
5107 if ((*bed
->copy_private_bfd_data_p
) (ibfd
, isec
, obfd
, osec
))
5108 if (! copy_private_bfd_data (ibfd
, obfd
))
5111 else if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
5115 /* Only set up the segments if there are no more SEC_ALLOC
5116 sections. FIXME: This won't do the right thing if objcopy is
5117 used to remove the last SEC_ALLOC section, since objcopy
5118 won't call this routine in that case. */
5119 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
5120 if ((s
->flags
& SEC_ALLOC
) != 0)
5124 if (! copy_private_bfd_data (ibfd
, obfd
))
5129 ihdr
= &elf_section_data (isec
)->this_hdr
;
5130 ohdr
= &elf_section_data (osec
)->this_hdr
;
5132 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
5134 if (ihdr
->sh_type
== SHT_SYMTAB
5135 || ihdr
->sh_type
== SHT_DYNSYM
5136 || ihdr
->sh_type
== SHT_GNU_verneed
5137 || ihdr
->sh_type
== SHT_GNU_verdef
)
5138 ohdr
->sh_info
= ihdr
->sh_info
;
5140 /* Set things up for objcopy. The output SHT_GROUP section will
5141 have its elf_next_in_group pointing back to the input group
5143 elf_next_in_group (osec
) = elf_next_in_group (isec
);
5144 elf_group_name (osec
) = elf_group_name (isec
);
5146 elf_section_data (osec
)->use_rela_p
5147 = elf_section_data (isec
)->use_rela_p
;
5152 /* Copy private symbol information. If this symbol is in a section
5153 which we did not map into a BFD section, try to map the section
5154 index correctly. We use special macro definitions for the mapped
5155 section indices; these definitions are interpreted by the
5156 swap_out_syms function. */
5158 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5159 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5160 #define MAP_STRTAB (SHN_HIOS + 3)
5161 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5162 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5165 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
5171 elf_symbol_type
*isym
, *osym
;
5173 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
5174 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
5177 isym
= elf_symbol_from (ibfd
, isymarg
);
5178 osym
= elf_symbol_from (obfd
, osymarg
);
5182 && bfd_is_abs_section (isym
->symbol
.section
))
5186 shndx
= isym
->internal_elf_sym
.st_shndx
;
5187 if (shndx
== elf_onesymtab (ibfd
))
5188 shndx
= MAP_ONESYMTAB
;
5189 else if (shndx
== elf_dynsymtab (ibfd
))
5190 shndx
= MAP_DYNSYMTAB
;
5191 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
5193 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
5194 shndx
= MAP_SHSTRTAB
;
5195 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
5196 shndx
= MAP_SYM_SHNDX
;
5197 osym
->internal_elf_sym
.st_shndx
= shndx
;
5203 /* Swap out the symbols. */
5206 swap_out_syms (abfd
, sttp
, relocatable_p
)
5208 struct bfd_strtab_hash
**sttp
;
5211 struct elf_backend_data
*bed
;
5214 struct bfd_strtab_hash
*stt
;
5215 Elf_Internal_Shdr
*symtab_hdr
;
5216 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5217 Elf_Internal_Shdr
*symstrtab_hdr
;
5218 char *outbound_syms
;
5219 char *outbound_shndx
;
5223 if (!elf_map_symbols (abfd
))
5226 /* Dump out the symtabs. */
5227 stt
= _bfd_elf_stringtab_init ();
5231 bed
= get_elf_backend_data (abfd
);
5232 symcount
= bfd_get_symcount (abfd
);
5233 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5234 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5235 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5236 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5237 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5238 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5240 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5241 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5243 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5244 outbound_syms
= bfd_alloc (abfd
, amt
);
5245 if (outbound_syms
== NULL
)
5247 symtab_hdr
->contents
= (PTR
) outbound_syms
;
5249 outbound_shndx
= NULL
;
5250 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5251 if (symtab_shndx_hdr
->sh_name
!= 0)
5253 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5254 outbound_shndx
= bfd_zalloc (abfd
, amt
);
5255 if (outbound_shndx
== NULL
)
5257 symtab_shndx_hdr
->contents
= outbound_shndx
;
5258 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5259 symtab_shndx_hdr
->sh_size
= amt
;
5260 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5261 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5264 /* now generate the data (for "contents") */
5266 /* Fill in zeroth symbol and swap it out. */
5267 Elf_Internal_Sym sym
;
5273 sym
.st_shndx
= SHN_UNDEF
;
5274 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5275 outbound_syms
+= bed
->s
->sizeof_sym
;
5276 if (outbound_shndx
!= NULL
)
5277 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5280 syms
= bfd_get_outsymbols (abfd
);
5281 for (idx
= 0; idx
< symcount
; idx
++)
5283 Elf_Internal_Sym sym
;
5284 bfd_vma value
= syms
[idx
]->value
;
5285 elf_symbol_type
*type_ptr
;
5286 flagword flags
= syms
[idx
]->flags
;
5289 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5291 /* Local section symbols have no name. */
5296 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5299 if (sym
.st_name
== (unsigned long) -1)
5303 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5305 if ((flags
& BSF_SECTION_SYM
) == 0
5306 && bfd_is_com_section (syms
[idx
]->section
))
5308 /* ELF common symbols put the alignment into the `value' field,
5309 and the size into the `size' field. This is backwards from
5310 how BFD handles it, so reverse it here. */
5311 sym
.st_size
= value
;
5312 if (type_ptr
== NULL
5313 || type_ptr
->internal_elf_sym
.st_value
== 0)
5314 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5316 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5317 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5318 (abfd
, syms
[idx
]->section
);
5322 asection
*sec
= syms
[idx
]->section
;
5325 if (sec
->output_section
)
5327 value
+= sec
->output_offset
;
5328 sec
= sec
->output_section
;
5330 /* Don't add in the section vma for relocatable output. */
5331 if (! relocatable_p
)
5333 sym
.st_value
= value
;
5334 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5336 if (bfd_is_abs_section (sec
)
5338 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5340 /* This symbol is in a real ELF section which we did
5341 not create as a BFD section. Undo the mapping done
5342 by copy_private_symbol_data. */
5343 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5347 shndx
= elf_onesymtab (abfd
);
5350 shndx
= elf_dynsymtab (abfd
);
5353 shndx
= elf_tdata (abfd
)->strtab_section
;
5356 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5359 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5367 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5373 /* Writing this would be a hell of a lot easier if
5374 we had some decent documentation on bfd, and
5375 knew what to expect of the library, and what to
5376 demand of applications. For example, it
5377 appears that `objcopy' might not set the
5378 section of a symbol to be a section that is
5379 actually in the output file. */
5380 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5381 BFD_ASSERT (sec2
!= 0);
5382 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5383 BFD_ASSERT (shndx
!= -1);
5387 sym
.st_shndx
= shndx
;
5390 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5392 else if ((flags
& BSF_FUNCTION
) != 0)
5394 else if ((flags
& BSF_OBJECT
) != 0)
5399 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5402 /* Processor-specific types */
5403 if (type_ptr
!= NULL
5404 && bed
->elf_backend_get_symbol_type
)
5405 type
= ((*bed
->elf_backend_get_symbol_type
)
5406 (&type_ptr
->internal_elf_sym
, type
));
5408 if (flags
& BSF_SECTION_SYM
)
5410 if (flags
& BSF_GLOBAL
)
5411 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5413 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5415 else if (bfd_is_com_section (syms
[idx
]->section
))
5416 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5417 else if (bfd_is_und_section (syms
[idx
]->section
))
5418 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5422 else if (flags
& BSF_FILE
)
5423 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5426 int bind
= STB_LOCAL
;
5428 if (flags
& BSF_LOCAL
)
5430 else if (flags
& BSF_WEAK
)
5432 else if (flags
& BSF_GLOBAL
)
5435 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5438 if (type_ptr
!= NULL
)
5439 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5443 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5444 outbound_syms
+= bed
->s
->sizeof_sym
;
5445 if (outbound_shndx
!= NULL
)
5446 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5450 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5451 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5453 symstrtab_hdr
->sh_flags
= 0;
5454 symstrtab_hdr
->sh_addr
= 0;
5455 symstrtab_hdr
->sh_entsize
= 0;
5456 symstrtab_hdr
->sh_link
= 0;
5457 symstrtab_hdr
->sh_info
= 0;
5458 symstrtab_hdr
->sh_addralign
= 1;
5463 /* Return the number of bytes required to hold the symtab vector.
5465 Note that we base it on the count plus 1, since we will null terminate
5466 the vector allocated based on this size. However, the ELF symbol table
5467 always has a dummy entry as symbol #0, so it ends up even. */
5470 _bfd_elf_get_symtab_upper_bound (abfd
)
5475 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5477 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5478 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5480 symtab_size
-= sizeof (asymbol
*);
5486 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5491 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5493 if (elf_dynsymtab (abfd
) == 0)
5495 bfd_set_error (bfd_error_invalid_operation
);
5499 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5500 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5502 symtab_size
-= sizeof (asymbol
*);
5508 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5509 bfd
*abfd ATTRIBUTE_UNUSED
;
5512 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5515 /* Canonicalize the relocs. */
5518 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5526 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5528 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5531 tblptr
= section
->relocation
;
5532 for (i
= 0; i
< section
->reloc_count
; i
++)
5533 *relptr
++ = tblptr
++;
5537 return section
->reloc_count
;
5541 _bfd_elf_get_symtab (abfd
, alocation
)
5543 asymbol
**alocation
;
5545 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5546 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5549 bfd_get_symcount (abfd
) = symcount
;
5554 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5556 asymbol
**alocation
;
5558 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5559 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5562 bfd_get_dynamic_symcount (abfd
) = symcount
;
5566 /* Return the size required for the dynamic reloc entries. Any
5567 section that was actually installed in the BFD, and has type
5568 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5569 considered to be a dynamic reloc section. */
5572 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5578 if (elf_dynsymtab (abfd
) == 0)
5580 bfd_set_error (bfd_error_invalid_operation
);
5584 ret
= sizeof (arelent
*);
5585 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5586 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5587 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5588 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5589 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5590 * sizeof (arelent
*));
5595 /* Canonicalize the dynamic relocation entries. Note that we return
5596 the dynamic relocations as a single block, although they are
5597 actually associated with particular sections; the interface, which
5598 was designed for SunOS style shared libraries, expects that there
5599 is only one set of dynamic relocs. Any section that was actually
5600 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5601 the dynamic symbol table, is considered to be a dynamic reloc
5605 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5610 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5614 if (elf_dynsymtab (abfd
) == 0)
5616 bfd_set_error (bfd_error_invalid_operation
);
5620 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5622 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5624 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5625 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5626 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5631 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5633 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5635 for (i
= 0; i
< count
; i
++)
5646 /* Read in the version information. */
5649 _bfd_elf_slurp_version_tables (abfd
)
5652 bfd_byte
*contents
= NULL
;
5655 if (elf_dynverdef (abfd
) != 0)
5657 Elf_Internal_Shdr
*hdr
;
5658 Elf_External_Verdef
*everdef
;
5659 Elf_Internal_Verdef
*iverdef
;
5660 Elf_Internal_Verdef
*iverdefarr
;
5661 Elf_Internal_Verdef iverdefmem
;
5663 unsigned int maxidx
;
5665 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5667 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5668 if (contents
== NULL
)
5670 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5671 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5674 /* We know the number of entries in the section but not the maximum
5675 index. Therefore we have to run through all entries and find
5677 everdef
= (Elf_External_Verdef
*) contents
;
5679 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5681 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5683 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5684 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5686 everdef
= ((Elf_External_Verdef
*)
5687 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5690 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5691 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5692 if (elf_tdata (abfd
)->verdef
== NULL
)
5695 elf_tdata (abfd
)->cverdefs
= maxidx
;
5697 everdef
= (Elf_External_Verdef
*) contents
;
5698 iverdefarr
= elf_tdata (abfd
)->verdef
;
5699 for (i
= 0; i
< hdr
->sh_info
; i
++)
5701 Elf_External_Verdaux
*everdaux
;
5702 Elf_Internal_Verdaux
*iverdaux
;
5705 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5707 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5708 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5710 iverdef
->vd_bfd
= abfd
;
5712 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5713 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5714 if (iverdef
->vd_auxptr
== NULL
)
5717 everdaux
= ((Elf_External_Verdaux
*)
5718 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5719 iverdaux
= iverdef
->vd_auxptr
;
5720 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5722 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5724 iverdaux
->vda_nodename
=
5725 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5726 iverdaux
->vda_name
);
5727 if (iverdaux
->vda_nodename
== NULL
)
5730 if (j
+ 1 < iverdef
->vd_cnt
)
5731 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5733 iverdaux
->vda_nextptr
= NULL
;
5735 everdaux
= ((Elf_External_Verdaux
*)
5736 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5739 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5741 if (i
+ 1 < hdr
->sh_info
)
5742 iverdef
->vd_nextdef
= iverdef
+ 1;
5744 iverdef
->vd_nextdef
= NULL
;
5746 everdef
= ((Elf_External_Verdef
*)
5747 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5754 if (elf_dynverref (abfd
) != 0)
5756 Elf_Internal_Shdr
*hdr
;
5757 Elf_External_Verneed
*everneed
;
5758 Elf_Internal_Verneed
*iverneed
;
5761 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5763 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5764 elf_tdata (abfd
)->verref
=
5765 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5766 if (elf_tdata (abfd
)->verref
== NULL
)
5769 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5771 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5772 if (contents
== NULL
)
5774 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5775 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5778 everneed
= (Elf_External_Verneed
*) contents
;
5779 iverneed
= elf_tdata (abfd
)->verref
;
5780 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5782 Elf_External_Vernaux
*evernaux
;
5783 Elf_Internal_Vernaux
*ivernaux
;
5786 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5788 iverneed
->vn_bfd
= abfd
;
5790 iverneed
->vn_filename
=
5791 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5793 if (iverneed
->vn_filename
== NULL
)
5796 amt
= iverneed
->vn_cnt
;
5797 amt
*= sizeof (Elf_Internal_Vernaux
);
5798 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5800 evernaux
= ((Elf_External_Vernaux
*)
5801 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5802 ivernaux
= iverneed
->vn_auxptr
;
5803 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5805 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5807 ivernaux
->vna_nodename
=
5808 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5809 ivernaux
->vna_name
);
5810 if (ivernaux
->vna_nodename
== NULL
)
5813 if (j
+ 1 < iverneed
->vn_cnt
)
5814 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5816 ivernaux
->vna_nextptr
= NULL
;
5818 evernaux
= ((Elf_External_Vernaux
*)
5819 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5822 if (i
+ 1 < hdr
->sh_info
)
5823 iverneed
->vn_nextref
= iverneed
+ 1;
5825 iverneed
->vn_nextref
= NULL
;
5827 everneed
= ((Elf_External_Verneed
*)
5828 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5838 if (contents
== NULL
)
5844 _bfd_elf_make_empty_symbol (abfd
)
5847 elf_symbol_type
*newsym
;
5848 bfd_size_type amt
= sizeof (elf_symbol_type
);
5850 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5855 newsym
->symbol
.the_bfd
= abfd
;
5856 return &newsym
->symbol
;
5861 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5862 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5866 bfd_symbol_info (symbol
, ret
);
5869 /* Return whether a symbol name implies a local symbol. Most targets
5870 use this function for the is_local_label_name entry point, but some
5874 _bfd_elf_is_local_label_name (abfd
, name
)
5875 bfd
*abfd ATTRIBUTE_UNUSED
;
5878 /* Normal local symbols start with ``.L''. */
5879 if (name
[0] == '.' && name
[1] == 'L')
5882 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5883 DWARF debugging symbols starting with ``..''. */
5884 if (name
[0] == '.' && name
[1] == '.')
5887 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5888 emitting DWARF debugging output. I suspect this is actually a
5889 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5890 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5891 underscore to be emitted on some ELF targets). For ease of use,
5892 we treat such symbols as local. */
5893 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5900 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5901 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5902 asymbol
*symbol ATTRIBUTE_UNUSED
;
5909 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5911 enum bfd_architecture arch
;
5912 unsigned long machine
;
5914 /* If this isn't the right architecture for this backend, and this
5915 isn't the generic backend, fail. */
5916 if (arch
!= get_elf_backend_data (abfd
)->arch
5917 && arch
!= bfd_arch_unknown
5918 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5921 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5924 /* Find the function to a particular section and offset,
5925 for error reporting. */
5928 elf_find_function (abfd
, section
, symbols
, offset
,
5929 filename_ptr
, functionname_ptr
)
5930 bfd
*abfd ATTRIBUTE_UNUSED
;
5934 const char **filename_ptr
;
5935 const char **functionname_ptr
;
5937 const char *filename
;
5946 for (p
= symbols
; *p
!= NULL
; p
++)
5950 q
= (elf_symbol_type
*) *p
;
5952 if (bfd_get_section (&q
->symbol
) != section
)
5955 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5960 filename
= bfd_asymbol_name (&q
->symbol
);
5964 if (q
->symbol
.section
== section
5965 && q
->symbol
.value
>= low_func
5966 && q
->symbol
.value
<= offset
)
5968 func
= (asymbol
*) q
;
5969 low_func
= q
->symbol
.value
;
5979 *filename_ptr
= filename
;
5980 if (functionname_ptr
)
5981 *functionname_ptr
= bfd_asymbol_name (func
);
5986 /* Find the nearest line to a particular section and offset,
5987 for error reporting. */
5990 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5991 filename_ptr
, functionname_ptr
, line_ptr
)
5996 const char **filename_ptr
;
5997 const char **functionname_ptr
;
5998 unsigned int *line_ptr
;
6002 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
6003 filename_ptr
, functionname_ptr
,
6006 if (!*functionname_ptr
)
6007 elf_find_function (abfd
, section
, symbols
, offset
,
6008 *filename_ptr
? NULL
: filename_ptr
,
6014 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
6015 filename_ptr
, functionname_ptr
,
6017 &elf_tdata (abfd
)->dwarf2_find_line_info
))
6019 if (!*functionname_ptr
)
6020 elf_find_function (abfd
, section
, symbols
, offset
,
6021 *filename_ptr
? NULL
: filename_ptr
,
6027 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
6028 &found
, filename_ptr
,
6029 functionname_ptr
, line_ptr
,
6030 &elf_tdata (abfd
)->line_info
))
6032 if (found
&& (*functionname_ptr
|| *line_ptr
))
6035 if (symbols
== NULL
)
6038 if (! elf_find_function (abfd
, section
, symbols
, offset
,
6039 filename_ptr
, functionname_ptr
))
6047 _bfd_elf_sizeof_headers (abfd
, reloc
)
6053 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
6055 ret
+= get_program_header_size (abfd
);
6060 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
6065 bfd_size_type count
;
6067 Elf_Internal_Shdr
*hdr
;
6070 if (! abfd
->output_has_begun
6071 && ! (_bfd_elf_compute_section_file_positions
6072 (abfd
, (struct bfd_link_info
*) NULL
)))
6075 hdr
= &elf_section_data (section
)->this_hdr
;
6076 pos
= hdr
->sh_offset
+ offset
;
6077 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
6078 || bfd_bwrite (location
, count
, abfd
) != count
)
6085 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
6086 bfd
*abfd ATTRIBUTE_UNUSED
;
6087 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
6088 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
6095 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
6098 Elf_Internal_Rel
*dst
;
6104 /* Try to convert a non-ELF reloc into an ELF one. */
6107 _bfd_elf_validate_reloc (abfd
, areloc
)
6111 /* Check whether we really have an ELF howto. */
6113 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
6115 bfd_reloc_code_real_type code
;
6116 reloc_howto_type
*howto
;
6118 /* Alien reloc: Try to determine its type to replace it with an
6119 equivalent ELF reloc. */
6121 if (areloc
->howto
->pc_relative
)
6123 switch (areloc
->howto
->bitsize
)
6126 code
= BFD_RELOC_8_PCREL
;
6129 code
= BFD_RELOC_12_PCREL
;
6132 code
= BFD_RELOC_16_PCREL
;
6135 code
= BFD_RELOC_24_PCREL
;
6138 code
= BFD_RELOC_32_PCREL
;
6141 code
= BFD_RELOC_64_PCREL
;
6147 howto
= bfd_reloc_type_lookup (abfd
, code
);
6149 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
6151 if (howto
->pcrel_offset
)
6152 areloc
->addend
+= areloc
->address
;
6154 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
6159 switch (areloc
->howto
->bitsize
)
6165 code
= BFD_RELOC_14
;
6168 code
= BFD_RELOC_16
;
6171 code
= BFD_RELOC_26
;
6174 code
= BFD_RELOC_32
;
6177 code
= BFD_RELOC_64
;
6183 howto
= bfd_reloc_type_lookup (abfd
, code
);
6187 areloc
->howto
= howto
;
6195 (*_bfd_error_handler
)
6196 (_("%s: unsupported relocation type %s"),
6197 bfd_archive_filename (abfd
), areloc
->howto
->name
);
6198 bfd_set_error (bfd_error_bad_value
);
6203 _bfd_elf_close_and_cleanup (abfd
)
6206 if (bfd_get_format (abfd
) == bfd_object
)
6208 if (elf_shstrtab (abfd
) != NULL
)
6209 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6212 return _bfd_generic_close_and_cleanup (abfd
);
6215 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6216 in the relocation's offset. Thus we cannot allow any sort of sanity
6217 range-checking to interfere. There is nothing else to do in processing
6220 bfd_reloc_status_type
6221 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
6222 bfd
*abfd ATTRIBUTE_UNUSED
;
6223 arelent
*re ATTRIBUTE_UNUSED
;
6224 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
6225 PTR data ATTRIBUTE_UNUSED
;
6226 asection
*is ATTRIBUTE_UNUSED
;
6227 bfd
*obfd ATTRIBUTE_UNUSED
;
6228 char **errmsg ATTRIBUTE_UNUSED
;
6230 return bfd_reloc_ok
;
6233 /* Elf core file support. Much of this only works on native
6234 toolchains, since we rely on knowing the
6235 machine-dependent procfs structure in order to pick
6236 out details about the corefile. */
6238 #ifdef HAVE_SYS_PROCFS_H
6239 # include <sys/procfs.h>
6242 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6245 elfcore_make_pid (abfd
)
6248 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6249 + (elf_tdata (abfd
)->core_pid
));
6252 /* If there isn't a section called NAME, make one, using
6253 data from SECT. Note, this function will generate a
6254 reference to NAME, so you shouldn't deallocate or
6258 elfcore_maybe_make_sect (abfd
, name
, sect
)
6265 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6268 sect2
= bfd_make_section (abfd
, name
);
6272 sect2
->_raw_size
= sect
->_raw_size
;
6273 sect2
->filepos
= sect
->filepos
;
6274 sect2
->flags
= sect
->flags
;
6275 sect2
->alignment_power
= sect
->alignment_power
;
6279 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6280 actually creates up to two pseudosections:
6281 - For the single-threaded case, a section named NAME, unless
6282 such a section already exists.
6283 - For the multi-threaded case, a section named "NAME/PID", where
6284 PID is elfcore_make_pid (abfd).
6285 Both pseudosections have identical contents. */
6287 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
6294 char *threaded_name
;
6298 /* Build the section name. */
6300 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6301 len
= strlen (buf
) + 1;
6302 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6303 if (threaded_name
== NULL
)
6305 memcpy (threaded_name
, buf
, len
);
6307 sect
= bfd_make_section (abfd
, threaded_name
);
6310 sect
->_raw_size
= size
;
6311 sect
->filepos
= filepos
;
6312 sect
->flags
= SEC_HAS_CONTENTS
;
6313 sect
->alignment_power
= 2;
6315 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6318 /* prstatus_t exists on:
6320 linux 2.[01] + glibc
6324 #if defined (HAVE_PRSTATUS_T)
6325 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6328 elfcore_grok_prstatus (abfd
, note
)
6330 Elf_Internal_Note
*note
;
6335 if (note
->descsz
== sizeof (prstatus_t
))
6339 raw_size
= sizeof (prstat
.pr_reg
);
6340 offset
= offsetof (prstatus_t
, pr_reg
);
6341 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6343 /* Do not overwrite the core signal if it
6344 has already been set by another thread. */
6345 if (elf_tdata (abfd
)->core_signal
== 0)
6346 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6347 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6349 /* pr_who exists on:
6352 pr_who doesn't exist on:
6355 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6356 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6359 #if defined (HAVE_PRSTATUS32_T)
6360 else if (note
->descsz
== sizeof (prstatus32_t
))
6362 /* 64-bit host, 32-bit corefile */
6363 prstatus32_t prstat
;
6365 raw_size
= sizeof (prstat
.pr_reg
);
6366 offset
= offsetof (prstatus32_t
, pr_reg
);
6367 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6369 /* Do not overwrite the core signal if it
6370 has already been set by another thread. */
6371 if (elf_tdata (abfd
)->core_signal
== 0)
6372 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6373 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6375 /* pr_who exists on:
6378 pr_who doesn't exist on:
6381 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6382 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6385 #endif /* HAVE_PRSTATUS32_T */
6388 /* Fail - we don't know how to handle any other
6389 note size (ie. data object type). */
6393 /* Make a ".reg/999" section and a ".reg" section. */
6394 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6395 raw_size
, note
->descpos
+ offset
);
6397 #endif /* defined (HAVE_PRSTATUS_T) */
6399 /* Create a pseudosection containing the exact contents of NOTE. */
6401 elfcore_make_note_pseudosection (abfd
, name
, note
)
6404 Elf_Internal_Note
*note
;
6406 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6407 note
->descsz
, note
->descpos
);
6410 /* There isn't a consistent prfpregset_t across platforms,
6411 but it doesn't matter, because we don't have to pick this
6412 data structure apart. */
6415 elfcore_grok_prfpreg (abfd
, note
)
6417 Elf_Internal_Note
*note
;
6419 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6422 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6423 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6427 elfcore_grok_prxfpreg (abfd
, note
)
6429 Elf_Internal_Note
*note
;
6431 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6434 #if defined (HAVE_PRPSINFO_T)
6435 typedef prpsinfo_t elfcore_psinfo_t
;
6436 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6437 typedef prpsinfo32_t elfcore_psinfo32_t
;
6441 #if defined (HAVE_PSINFO_T)
6442 typedef psinfo_t elfcore_psinfo_t
;
6443 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6444 typedef psinfo32_t elfcore_psinfo32_t
;
6448 /* return a malloc'ed copy of a string at START which is at
6449 most MAX bytes long, possibly without a terminating '\0'.
6450 the copy will always have a terminating '\0'. */
6453 _bfd_elfcore_strndup (abfd
, start
, max
)
6459 char *end
= memchr (start
, '\0', max
);
6467 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6471 memcpy (dups
, start
, len
);
6477 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6478 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6481 elfcore_grok_psinfo (abfd
, note
)
6483 Elf_Internal_Note
*note
;
6485 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6487 elfcore_psinfo_t psinfo
;
6489 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6491 elf_tdata (abfd
)->core_program
6492 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6493 sizeof (psinfo
.pr_fname
));
6495 elf_tdata (abfd
)->core_command
6496 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6497 sizeof (psinfo
.pr_psargs
));
6499 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6500 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6502 /* 64-bit host, 32-bit corefile */
6503 elfcore_psinfo32_t psinfo
;
6505 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6507 elf_tdata (abfd
)->core_program
6508 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6509 sizeof (psinfo
.pr_fname
));
6511 elf_tdata (abfd
)->core_command
6512 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6513 sizeof (psinfo
.pr_psargs
));
6519 /* Fail - we don't know how to handle any other
6520 note size (ie. data object type). */
6524 /* Note that for some reason, a spurious space is tacked
6525 onto the end of the args in some (at least one anyway)
6526 implementations, so strip it off if it exists. */
6529 char *command
= elf_tdata (abfd
)->core_command
;
6530 int n
= strlen (command
);
6532 if (0 < n
&& command
[n
- 1] == ' ')
6533 command
[n
- 1] = '\0';
6538 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6540 #if defined (HAVE_PSTATUS_T)
6541 static boolean elfcore_grok_pstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6544 elfcore_grok_pstatus (abfd
, note
)
6546 Elf_Internal_Note
*note
;
6548 if (note
->descsz
== sizeof (pstatus_t
)
6549 #if defined (HAVE_PXSTATUS_T)
6550 || note
->descsz
== sizeof (pxstatus_t
)
6556 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6558 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6560 #if defined (HAVE_PSTATUS32_T)
6561 else if (note
->descsz
== sizeof (pstatus32_t
))
6563 /* 64-bit host, 32-bit corefile */
6566 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6568 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6571 /* Could grab some more details from the "representative"
6572 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6573 NT_LWPSTATUS note, presumably. */
6577 #endif /* defined (HAVE_PSTATUS_T) */
6579 #if defined (HAVE_LWPSTATUS_T)
6580 static boolean elfcore_grok_lwpstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6583 elfcore_grok_lwpstatus (abfd
, note
)
6585 Elf_Internal_Note
*note
;
6587 lwpstatus_t lwpstat
;
6593 if (note
->descsz
!= sizeof (lwpstat
)
6594 #if defined (HAVE_LWPXSTATUS_T)
6595 && note
->descsz
!= sizeof (lwpxstatus_t
)
6600 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6602 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6603 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6605 /* Make a ".reg/999" section. */
6607 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6608 len
= strlen (buf
) + 1;
6609 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6612 memcpy (name
, buf
, len
);
6614 sect
= bfd_make_section (abfd
, name
);
6618 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6619 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6620 sect
->filepos
= note
->descpos
6621 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6624 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6625 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6626 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6629 sect
->flags
= SEC_HAS_CONTENTS
;
6630 sect
->alignment_power
= 2;
6632 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6635 /* Make a ".reg2/999" section */
6637 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6638 len
= strlen (buf
) + 1;
6639 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6642 memcpy (name
, buf
, len
);
6644 sect
= bfd_make_section (abfd
, name
);
6648 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6649 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6650 sect
->filepos
= note
->descpos
6651 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6654 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6655 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6656 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6659 sect
->flags
= SEC_HAS_CONTENTS
;
6660 sect
->alignment_power
= 2;
6662 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6664 #endif /* defined (HAVE_LWPSTATUS_T) */
6666 #if defined (HAVE_WIN32_PSTATUS_T)
6668 elfcore_grok_win32pstatus (abfd
, note
)
6670 Elf_Internal_Note
*note
;
6676 win32_pstatus_t pstatus
;
6678 if (note
->descsz
< sizeof (pstatus
))
6681 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6683 switch (pstatus
.data_type
)
6685 case NOTE_INFO_PROCESS
:
6686 /* FIXME: need to add ->core_command. */
6687 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6688 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6691 case NOTE_INFO_THREAD
:
6692 /* Make a ".reg/999" section. */
6693 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6695 len
= strlen (buf
) + 1;
6696 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6700 memcpy (name
, buf
, len
);
6702 sect
= bfd_make_section (abfd
, name
);
6706 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6707 sect
->filepos
= (note
->descpos
6708 + offsetof (struct win32_pstatus
,
6709 data
.thread_info
.thread_context
));
6710 sect
->flags
= SEC_HAS_CONTENTS
;
6711 sect
->alignment_power
= 2;
6713 if (pstatus
.data
.thread_info
.is_active_thread
)
6714 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6718 case NOTE_INFO_MODULE
:
6719 /* Make a ".module/xxxxxxxx" section. */
6720 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6722 len
= strlen (buf
) + 1;
6723 name
= bfd_alloc (abfd
, (bfd_size_type
) len
);
6727 memcpy (name
, buf
, len
);
6729 sect
= bfd_make_section (abfd
, name
);
6734 sect
->_raw_size
= note
->descsz
;
6735 sect
->filepos
= note
->descpos
;
6736 sect
->flags
= SEC_HAS_CONTENTS
;
6737 sect
->alignment_power
= 2;
6746 #endif /* HAVE_WIN32_PSTATUS_T */
6749 elfcore_grok_note (abfd
, note
)
6751 Elf_Internal_Note
*note
;
6753 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6761 if (bed
->elf_backend_grok_prstatus
)
6762 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6764 #if defined (HAVE_PRSTATUS_T)
6765 return elfcore_grok_prstatus (abfd
, note
);
6770 #if defined (HAVE_PSTATUS_T)
6772 return elfcore_grok_pstatus (abfd
, note
);
6775 #if defined (HAVE_LWPSTATUS_T)
6777 return elfcore_grok_lwpstatus (abfd
, note
);
6780 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6781 return elfcore_grok_prfpreg (abfd
, note
);
6783 #if defined (HAVE_WIN32_PSTATUS_T)
6784 case NT_WIN32PSTATUS
:
6785 return elfcore_grok_win32pstatus (abfd
, note
);
6788 case NT_PRXFPREG
: /* Linux SSE extension */
6789 if (note
->namesz
== 6
6790 && strcmp (note
->namedata
, "LINUX") == 0)
6791 return elfcore_grok_prxfpreg (abfd
, note
);
6797 if (bed
->elf_backend_grok_psinfo
)
6798 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6800 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6801 return elfcore_grok_psinfo (abfd
, note
);
6809 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6810 Elf_Internal_Note
*note
;
6815 cp
= strchr (note
->namedata
, '@');
6818 *lwpidp
= atoi(cp
+ 1);
6825 elfcore_grok_netbsd_procinfo (abfd
, note
)
6827 Elf_Internal_Note
*note
;
6830 /* Signal number at offset 0x08. */
6831 elf_tdata (abfd
)->core_signal
6832 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6834 /* Process ID at offset 0x50. */
6835 elf_tdata (abfd
)->core_pid
6836 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6838 /* Command name at 0x7c (max 32 bytes, including nul). */
6839 elf_tdata (abfd
)->core_command
6840 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6846 elfcore_grok_netbsd_note (abfd
, note
)
6848 Elf_Internal_Note
*note
;
6852 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6853 elf_tdata (abfd
)->core_lwpid
= lwp
;
6855 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6857 /* NetBSD-specific core "procinfo". Note that we expect to
6858 find this note before any of the others, which is fine,
6859 since the kernel writes this note out first when it
6860 creates a core file. */
6862 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6865 /* As of Jan 2002 there are no other machine-independent notes
6866 defined for NetBSD core files. If the note type is less
6867 than the start of the machine-dependent note types, we don't
6870 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6874 switch (bfd_get_arch (abfd
))
6876 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6877 PT_GETFPREGS == mach+2. */
6879 case bfd_arch_alpha
:
6880 case bfd_arch_sparc
:
6883 case NT_NETBSDCORE_FIRSTMACH
+0:
6884 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6886 case NT_NETBSDCORE_FIRSTMACH
+2:
6887 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6893 /* On all other arch's, PT_GETREGS == mach+1 and
6894 PT_GETFPREGS == mach+3. */
6899 case NT_NETBSDCORE_FIRSTMACH
+1:
6900 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6902 case NT_NETBSDCORE_FIRSTMACH
+3:
6903 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6912 /* Function: elfcore_write_note
6919 size of data for note
6922 End of buffer containing note. */
6925 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6934 Elf_External_Note
*xnp
;
6944 struct elf_backend_data
*bed
;
6946 namesz
= strlen (name
) + 1;
6947 bed
= get_elf_backend_data (abfd
);
6948 pad
= -namesz
& (bed
->s
->file_align
- 1);
6951 newspace
= sizeof (Elf_External_Note
) - 1 + namesz
+ pad
+ size
;
6953 p
= realloc (buf
, *bufsiz
+ newspace
);
6955 *bufsiz
+= newspace
;
6956 xnp
= (Elf_External_Note
*) dest
;
6957 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6958 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6959 H_PUT_32 (abfd
, type
, xnp
->type
);
6963 memcpy (dest
, name
, namesz
);
6971 memcpy (dest
, input
, size
);
6975 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6977 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6985 char *note_name
= "CORE";
6987 #if defined (HAVE_PSINFO_T)
6989 note_type
= NT_PSINFO
;
6992 note_type
= NT_PRPSINFO
;
6995 memset (&data
, 0, sizeof (data
));
6996 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6997 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6998 return elfcore_write_note (abfd
, buf
, bufsiz
,
6999 note_name
, note_type
, &data
, sizeof (data
));
7001 #endif /* PSINFO_T or PRPSINFO_T */
7003 #if defined (HAVE_PRSTATUS_T)
7005 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
7014 char *note_name
= "CORE";
7016 memset (&prstat
, 0, sizeof (prstat
));
7017 prstat
.pr_pid
= pid
;
7018 prstat
.pr_cursig
= cursig
;
7019 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
7020 return elfcore_write_note (abfd
, buf
, bufsiz
,
7021 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
7023 #endif /* HAVE_PRSTATUS_T */
7025 #if defined (HAVE_LWPSTATUS_T)
7027 elfcore_write_lwpstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
7035 lwpstatus_t lwpstat
;
7036 char *note_name
= "CORE";
7038 memset (&lwpstat
, 0, sizeof (lwpstat
));
7039 lwpstat
.pr_lwpid
= pid
>> 16;
7040 lwpstat
.pr_cursig
= cursig
;
7041 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7042 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
7043 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7045 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
7046 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
7048 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
7049 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
7052 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7053 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
7055 #endif /* HAVE_LWPSTATUS_T */
7057 #if defined (HAVE_PSTATUS_T)
7059 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
7068 char *note_name
= "CORE";
7070 memset (&pstat
, 0, sizeof (pstat
));
7071 pstat
.pr_pid
= pid
& 0xffff;
7072 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
7073 NT_PSTATUS
, &pstat
, sizeof (pstat
));
7076 #endif /* HAVE_PSTATUS_T */
7079 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
7086 char *note_name
= "CORE";
7087 return elfcore_write_note (abfd
, buf
, bufsiz
,
7088 note_name
, NT_FPREGSET
, fpregs
, size
);
7092 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
7099 char *note_name
= "LINUX";
7100 return elfcore_write_note (abfd
, buf
, bufsiz
,
7101 note_name
, NT_PRXFPREG
, xfpregs
, size
);
7105 elfcore_read_notes (abfd
, offset
, size
)
7116 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
7119 buf
= bfd_malloc (size
);
7123 if (bfd_bread (buf
, size
, abfd
) != size
)
7131 while (p
< buf
+ size
)
7133 /* FIXME: bad alignment assumption. */
7134 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
7135 Elf_Internal_Note in
;
7137 in
.type
= H_GET_32 (abfd
, xnp
->type
);
7139 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
7140 in
.namedata
= xnp
->name
;
7142 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
7143 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
7144 in
.descpos
= offset
+ (in
.descdata
- buf
);
7146 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
7148 if (! elfcore_grok_netbsd_note (abfd
, &in
))
7153 if (! elfcore_grok_note (abfd
, &in
))
7157 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
7164 /* Providing external access to the ELF program header table. */
7166 /* Return an upper bound on the number of bytes required to store a
7167 copy of ABFD's program header table entries. Return -1 if an error
7168 occurs; bfd_get_error will return an appropriate code. */
7171 bfd_get_elf_phdr_upper_bound (abfd
)
7174 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7176 bfd_set_error (bfd_error_wrong_format
);
7180 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
7183 /* Copy ABFD's program header table entries to *PHDRS. The entries
7184 will be stored as an array of Elf_Internal_Phdr structures, as
7185 defined in include/elf/internal.h. To find out how large the
7186 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7188 Return the number of program header table entries read, or -1 if an
7189 error occurs; bfd_get_error will return an appropriate code. */
7192 bfd_get_elf_phdrs (abfd
, phdrs
)
7198 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
7200 bfd_set_error (bfd_error_wrong_format
);
7204 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
7205 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
7206 num_phdrs
* sizeof (Elf_Internal_Phdr
));
7212 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
7213 bfd
*abfd ATTRIBUTE_UNUSED
;
7218 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7220 i_ehdrp
= elf_elfheader (abfd
);
7221 if (i_ehdrp
== NULL
)
7222 sprintf_vma (buf
, value
);
7225 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7227 #if BFD_HOST_64BIT_LONG
7228 sprintf (buf
, "%016lx", value
);
7230 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
7231 _bfd_int64_low (value
));
7235 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7238 sprintf_vma (buf
, value
);
7243 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
7244 bfd
*abfd ATTRIBUTE_UNUSED
;
7249 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7251 i_ehdrp
= elf_elfheader (abfd
);
7252 if (i_ehdrp
== NULL
)
7253 fprintf_vma ((FILE *) stream
, value
);
7256 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7258 #if BFD_HOST_64BIT_LONG
7259 fprintf ((FILE *) stream
, "%016lx", value
);
7261 fprintf ((FILE *) stream
, "%08lx%08lx",
7262 _bfd_int64_high (value
), _bfd_int64_low (value
));
7266 fprintf ((FILE *) stream
, "%08lx",
7267 (unsigned long) (value
& 0xffffffff));
7270 fprintf_vma ((FILE *) stream
, value
);
7274 enum elf_reloc_type_class
7275 _bfd_elf_reloc_type_class (rela
)
7276 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
7278 return reloc_class_normal
;
7281 /* For RELA architectures, return the relocation value for a
7282 relocation against a local symbol. */
7285 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
7287 Elf_Internal_Sym
*sym
;
7289 Elf_Internal_Rela
*rel
;
7293 relocation
= (sec
->output_section
->vma
7294 + sec
->output_offset
7296 if ((sec
->flags
& SEC_MERGE
)
7297 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7298 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7304 _bfd_merged_section_offset (abfd
, &msec
,
7305 elf_section_data (sec
)->sec_info
,
7306 sym
->st_value
+ rel
->r_addend
,
7309 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7315 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
7317 Elf_Internal_Sym
*sym
;
7321 asection
*sec
= *psec
;
7323 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7324 return sym
->st_value
+ addend
;
7326 return _bfd_merged_section_offset (abfd
, psec
,
7327 elf_section_data (sec
)->sec_info
,
7328 sym
->st_value
+ addend
, (bfd_vma
) 0);
7332 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
7334 struct bfd_link_info
*info
;
7338 struct bfd_elf_section_data
*sec_data
;
7340 sec_data
= elf_section_data (sec
);
7341 switch (sec_data
->sec_info_type
)
7343 case ELF_INFO_TYPE_STABS
:
7344 return _bfd_stab_section_offset
7345 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
7347 case ELF_INFO_TYPE_EH_FRAME
:
7348 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);