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 void set_group_contents
PARAMS ((bfd
*, asection
*, PTR
));
58 static boolean assign_section_numbers
PARAMS ((bfd
*));
59 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
60 static boolean elf_map_symbols
PARAMS ((bfd
*));
61 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
62 static boolean elfcore_read_notes
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
63 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
64 bfd_vma
, const char **,
66 static int elfcore_make_pid
PARAMS ((bfd
*));
67 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
68 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
69 Elf_Internal_Note
*));
70 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
71 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
72 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
74 static boolean elfcore_netbsd_get_lwpid
PARAMS ((Elf_Internal_Note
*, int *));
75 static boolean elfcore_grok_netbsd_procinfo
PARAMS ((bfd
*,
76 Elf_Internal_Note
*));
77 static boolean elfcore_grok_netbsd_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
79 /* Swap version information in and out. The version information is
80 currently size independent. If that ever changes, this code will
81 need to move into elfcode.h. */
83 /* Swap in a Verdef structure. */
86 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
88 const Elf_External_Verdef
*src
;
89 Elf_Internal_Verdef
*dst
;
91 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
92 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
93 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
94 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
95 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
96 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
97 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
100 /* Swap out a Verdef structure. */
103 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
105 const Elf_Internal_Verdef
*src
;
106 Elf_External_Verdef
*dst
;
108 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
109 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
110 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
111 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
112 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
113 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
114 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
117 /* Swap in a Verdaux structure. */
120 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
122 const Elf_External_Verdaux
*src
;
123 Elf_Internal_Verdaux
*dst
;
125 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
126 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
129 /* Swap out a Verdaux structure. */
132 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
134 const Elf_Internal_Verdaux
*src
;
135 Elf_External_Verdaux
*dst
;
137 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
138 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
141 /* Swap in a Verneed structure. */
144 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
146 const Elf_External_Verneed
*src
;
147 Elf_Internal_Verneed
*dst
;
149 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
150 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
151 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
152 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
153 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
156 /* Swap out a Verneed structure. */
159 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
161 const Elf_Internal_Verneed
*src
;
162 Elf_External_Verneed
*dst
;
164 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
165 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
166 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
167 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
168 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
171 /* Swap in a Vernaux structure. */
174 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
176 const Elf_External_Vernaux
*src
;
177 Elf_Internal_Vernaux
*dst
;
179 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
180 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
181 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
182 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
183 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
186 /* Swap out a Vernaux structure. */
189 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
191 const Elf_Internal_Vernaux
*src
;
192 Elf_External_Vernaux
*dst
;
194 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
195 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
196 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
197 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
198 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
201 /* Swap in a Versym structure. */
204 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
206 const Elf_External_Versym
*src
;
207 Elf_Internal_Versym
*dst
;
209 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
212 /* Swap out a Versym structure. */
215 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
217 const Elf_Internal_Versym
*src
;
218 Elf_External_Versym
*dst
;
220 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
223 /* Standard ELF hash function. Do not change this function; you will
224 cause invalid hash tables to be generated. */
227 bfd_elf_hash (namearg
)
230 const unsigned char *name
= (const unsigned char *) namearg
;
235 while ((ch
= *name
++) != '\0')
238 if ((g
= (h
& 0xf0000000)) != 0)
241 /* The ELF ABI says `h &= ~g', but this is equivalent in
242 this case and on some machines one insn instead of two. */
249 /* Read a specified number of bytes at a specified offset in an ELF
250 file, into a newly allocated buffer, and return a pointer to the
254 elf_read (abfd
, offset
, size
)
261 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
263 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
265 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
267 if (bfd_get_error () != bfd_error_system_call
)
268 bfd_set_error (bfd_error_file_truncated
);
275 bfd_elf_mkobject (abfd
)
278 /* This just does initialization. */
279 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
280 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
281 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
282 if (elf_tdata (abfd
) == 0)
284 /* Since everything is done at close time, do we need any
291 bfd_elf_mkcorefile (abfd
)
294 /* I think this can be done just like an object file. */
295 return bfd_elf_mkobject (abfd
);
299 bfd_elf_get_str_section (abfd
, shindex
)
301 unsigned int shindex
;
303 Elf_Internal_Shdr
**i_shdrp
;
304 char *shstrtab
= NULL
;
306 bfd_size_type shstrtabsize
;
308 i_shdrp
= elf_elfsections (abfd
);
309 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
312 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
313 if (shstrtab
== NULL
)
315 /* No cached one, attempt to read, and cache what we read. */
316 offset
= i_shdrp
[shindex
]->sh_offset
;
317 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
318 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
319 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
325 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
327 unsigned int shindex
;
328 unsigned int strindex
;
330 Elf_Internal_Shdr
*hdr
;
335 hdr
= elf_elfsections (abfd
)[shindex
];
337 if (hdr
->contents
== NULL
338 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
341 if (strindex
>= hdr
->sh_size
)
343 (*_bfd_error_handler
)
344 (_("%s: invalid string offset %u >= %lu for section `%s'"),
345 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
346 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
347 && strindex
== hdr
->sh_name
)
349 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
353 return ((char *) hdr
->contents
) + strindex
;
356 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
357 sections. The first element is the flags, the rest are section
360 typedef union elf_internal_group
{
361 Elf_Internal_Shdr
*shdr
;
363 } Elf_Internal_Group
;
365 /* Return the name of the group signature symbol. Why isn't the
366 signature just a string? */
369 group_signature (abfd
, ghdr
)
371 Elf_Internal_Shdr
*ghdr
;
373 struct elf_backend_data
*bed
;
375 unsigned char ename
[4];
378 /* First we need to ensure the symbol table is available. */
379 if (! bfd_section_from_shdr (abfd
, ghdr
->sh_link
))
382 /* Fortunately, the name index is at the same place in the external
383 symbol for both 32 and 64 bit ELF. */
384 bed
= get_elf_backend_data (abfd
);
385 pos
= elf_tdata (abfd
)->symtab_hdr
.sh_offset
;
386 pos
+= ghdr
->sh_info
* bed
->s
->sizeof_sym
;
387 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
388 || bfd_bread (ename
, (bfd_size_type
) 4, abfd
) != 4)
390 iname
= H_GET_32 (abfd
, ename
);
391 return elf_string_from_elf_strtab (abfd
, iname
);
394 /* Set next_in_group list pointer, and group name for NEWSECT. */
397 setup_group (abfd
, hdr
, newsect
)
399 Elf_Internal_Shdr
*hdr
;
402 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
404 /* If num_group is zero, read in all SHT_GROUP sections. The count
405 is set to -1 if there are no SHT_GROUP sections. */
408 unsigned int i
, shnum
;
410 /* First count the number of groups. If we have a SHT_GROUP
411 section with just a flag word (ie. sh_size is 4), ignore it. */
412 shnum
= elf_numsections (abfd
);
414 for (i
= 0; i
< shnum
; i
++)
416 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
417 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
422 num_group
= (unsigned) -1;
423 elf_tdata (abfd
)->num_group
= num_group
;
427 /* We keep a list of elf section headers for group sections,
428 so we can find them quickly. */
429 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
430 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
431 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
435 for (i
= 0; i
< shnum
; i
++)
437 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
438 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
441 Elf_Internal_Group
*dest
;
443 /* Add to list of sections. */
444 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
447 /* Read the raw contents. */
448 BFD_ASSERT (sizeof (*dest
) >= 4);
449 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
450 shdr
->contents
= bfd_alloc (abfd
, amt
);
451 if (shdr
->contents
== NULL
452 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
453 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
457 /* Translate raw contents, a flag word followed by an
458 array of elf section indices all in target byte order,
459 to the flag word followed by an array of elf section
461 src
= shdr
->contents
+ shdr
->sh_size
;
462 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
469 idx
= H_GET_32 (abfd
, src
);
470 if (src
== shdr
->contents
)
473 if (shdr
->bfd_section
!= NULL
&& (idx
& GRP_COMDAT
))
474 shdr
->bfd_section
->flags
475 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
480 ((*_bfd_error_handler
)
481 (_("%s: invalid SHT_GROUP entry"),
482 bfd_archive_filename (abfd
)));
485 dest
->shdr
= elf_elfsections (abfd
)[idx
];
492 if (num_group
!= (unsigned) -1)
496 for (i
= 0; i
< num_group
; i
++)
498 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
499 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
500 unsigned int n_elt
= shdr
->sh_size
/ 4;
502 /* Look through this group's sections to see if current
503 section is a member. */
505 if ((++idx
)->shdr
== hdr
)
509 /* We are a member of this group. Go looking through
510 other members to see if any others are linked via
512 idx
= (Elf_Internal_Group
*) shdr
->contents
;
513 n_elt
= shdr
->sh_size
/ 4;
515 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
516 && elf_next_in_group (s
) != NULL
)
520 /* Snarf the group name from other member, and
521 insert current section in circular list. */
522 elf_group_name (newsect
) = elf_group_name (s
);
523 elf_next_in_group (newsect
) = elf_next_in_group (s
);
524 elf_next_in_group (s
) = newsect
;
530 gname
= group_signature (abfd
, shdr
);
533 elf_group_name (newsect
) = gname
;
535 /* Start a circular list with one element. */
536 elf_next_in_group (newsect
) = newsect
;
539 if (shdr
->bfd_section
!= NULL
)
540 elf_next_in_group (shdr
->bfd_section
) = newsect
;
548 if (elf_group_name (newsect
) == NULL
)
550 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
551 bfd_archive_filename (abfd
), newsect
->name
);
557 bfd_elf_discard_group (abfd
, group
)
558 bfd
*abfd ATTRIBUTE_UNUSED
;
561 asection
*first
= elf_next_in_group (group
);
566 s
->output_section
= bfd_abs_section_ptr
;
567 s
= elf_next_in_group (s
);
568 /* These lists are circular. */
575 /* Make a BFD section from an ELF section. We store a pointer to the
576 BFD section in the bfd_section field of the header. */
579 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
581 Elf_Internal_Shdr
*hdr
;
586 struct elf_backend_data
*bed
;
588 if (hdr
->bfd_section
!= NULL
)
590 BFD_ASSERT (strcmp (name
,
591 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
595 newsect
= bfd_make_section_anyway (abfd
, name
);
599 newsect
->filepos
= hdr
->sh_offset
;
601 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
602 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
603 || ! bfd_set_section_alignment (abfd
, newsect
,
604 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
607 flags
= SEC_NO_FLAGS
;
608 if (hdr
->sh_type
!= SHT_NOBITS
)
609 flags
|= SEC_HAS_CONTENTS
;
610 if (hdr
->sh_type
== SHT_GROUP
)
611 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
612 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
615 if (hdr
->sh_type
!= SHT_NOBITS
)
618 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
619 flags
|= SEC_READONLY
;
620 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
622 else if ((flags
& SEC_LOAD
) != 0)
624 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
627 newsect
->entsize
= hdr
->sh_entsize
;
628 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
629 flags
|= SEC_STRINGS
;
631 if (hdr
->sh_flags
& SHF_GROUP
)
632 if (!setup_group (abfd
, hdr
, newsect
))
634 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
635 flags
|= SEC_THREAD_LOCAL
;
637 /* The debugging sections appear to be recognized only by name, not
640 static const char *debug_sec_names
[] =
649 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
650 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
654 flags
|= SEC_DEBUGGING
;
657 /* As a GNU extension, if the name begins with .gnu.linkonce, we
658 only link a single copy of the section. This is used to support
659 g++. g++ will emit each template expansion in its own section.
660 The symbols will be defined as weak, so that multiple definitions
661 are permitted. The GNU linker extension is to actually discard
662 all but one of the sections. */
663 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
664 && elf_next_in_group (newsect
) == NULL
)
665 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
667 bed
= get_elf_backend_data (abfd
);
668 if (bed
->elf_backend_section_flags
)
669 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
672 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
675 if ((flags
& SEC_ALLOC
) != 0)
677 Elf_Internal_Phdr
*phdr
;
680 /* Look through the phdrs to see if we need to adjust the lma.
681 If all the p_paddr fields are zero, we ignore them, since
682 some ELF linkers produce such output. */
683 phdr
= elf_tdata (abfd
)->phdr
;
684 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
686 if (phdr
->p_paddr
!= 0)
689 if (i
< elf_elfheader (abfd
)->e_phnum
)
691 phdr
= elf_tdata (abfd
)->phdr
;
692 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
694 /* This section is part of this segment if its file
695 offset plus size lies within the segment's memory
696 span and, if the section is loaded, the extent of the
697 loaded data lies within the extent of the segment.
699 Note - we used to check the p_paddr field as well, and
700 refuse to set the LMA if it was 0. This is wrong
701 though, as a perfectly valid initialised segment can
702 have a p_paddr of zero. Some architectures, eg ARM,
703 place special significance on the address 0 and
704 executables need to be able to have a segment which
705 covers this address. */
706 if (phdr
->p_type
== PT_LOAD
707 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
708 && (hdr
->sh_offset
+ hdr
->sh_size
709 <= phdr
->p_offset
+ phdr
->p_memsz
)
710 && ((flags
& SEC_LOAD
) == 0
711 || (hdr
->sh_offset
+ hdr
->sh_size
712 <= phdr
->p_offset
+ phdr
->p_filesz
)))
714 if ((flags
& SEC_LOAD
) == 0)
715 newsect
->lma
= (phdr
->p_paddr
716 + hdr
->sh_addr
- phdr
->p_vaddr
);
718 /* We used to use the same adjustment for SEC_LOAD
719 sections, but that doesn't work if the segment
720 is packed with code from multiple VMAs.
721 Instead we calculate the section LMA based on
722 the segment LMA. It is assumed that the
723 segment will contain sections with contiguous
724 LMAs, even if the VMAs are not. */
725 newsect
->lma
= (phdr
->p_paddr
726 + hdr
->sh_offset
- phdr
->p_offset
);
728 /* With contiguous segments, we can't tell from file
729 offsets whether a section with zero size should
730 be placed at the end of one segment or the
731 beginning of the next. Decide based on vaddr. */
732 if (hdr
->sh_addr
>= phdr
->p_vaddr
733 && (hdr
->sh_addr
+ hdr
->sh_size
734 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
741 hdr
->bfd_section
= newsect
;
742 elf_section_data (newsect
)->this_hdr
= *hdr
;
752 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
755 Helper functions for GDB to locate the string tables.
756 Since BFD hides string tables from callers, GDB needs to use an
757 internal hook to find them. Sun's .stabstr, in particular,
758 isn't even pointed to by the .stab section, so ordinary
759 mechanisms wouldn't work to find it, even if we had some.
762 struct elf_internal_shdr
*
763 bfd_elf_find_section (abfd
, name
)
767 Elf_Internal_Shdr
**i_shdrp
;
772 i_shdrp
= elf_elfsections (abfd
);
775 shstrtab
= bfd_elf_get_str_section (abfd
,
776 elf_elfheader (abfd
)->e_shstrndx
);
777 if (shstrtab
!= NULL
)
779 max
= elf_numsections (abfd
);
780 for (i
= 1; i
< max
; i
++)
781 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
788 const char *const bfd_elf_section_type_names
[] = {
789 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
790 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
791 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
794 /* ELF relocs are against symbols. If we are producing relocateable
795 output, and the reloc is against an external symbol, and nothing
796 has given us any additional addend, the resulting reloc will also
797 be against the same symbol. In such a case, we don't want to
798 change anything about the way the reloc is handled, since it will
799 all be done at final link time. Rather than put special case code
800 into bfd_perform_relocation, all the reloc types use this howto
801 function. It just short circuits the reloc if producing
802 relocateable output against an external symbol. */
804 bfd_reloc_status_type
805 bfd_elf_generic_reloc (abfd
,
812 bfd
*abfd ATTRIBUTE_UNUSED
;
813 arelent
*reloc_entry
;
815 PTR data ATTRIBUTE_UNUSED
;
816 asection
*input_section
;
818 char **error_message ATTRIBUTE_UNUSED
;
820 if (output_bfd
!= (bfd
*) NULL
821 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
822 && (! reloc_entry
->howto
->partial_inplace
823 || reloc_entry
->addend
== 0))
825 reloc_entry
->address
+= input_section
->output_offset
;
829 return bfd_reloc_continue
;
832 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
835 merge_sections_remove_hook (abfd
, sec
)
836 bfd
*abfd ATTRIBUTE_UNUSED
;
839 struct bfd_elf_section_data
*sec_data
;
841 sec_data
= elf_section_data (sec
);
842 BFD_ASSERT (sec_data
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
843 sec_data
->sec_info_type
= ELF_INFO_TYPE_NONE
;
846 /* Finish SHF_MERGE section merging. */
849 _bfd_elf_merge_sections (abfd
, info
)
851 struct bfd_link_info
*info
;
853 if (!is_elf_hash_table (info
))
855 if (elf_hash_table (info
)->merge_info
)
856 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
857 merge_sections_remove_hook
);
862 _bfd_elf_link_just_syms (sec
, info
)
864 struct bfd_link_info
*info
;
866 sec
->output_section
= bfd_abs_section_ptr
;
867 sec
->output_offset
= sec
->vma
;
868 if (!is_elf_hash_table (info
))
871 elf_section_data (sec
)->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
874 /* Copy the program header and other data from one object module to
878 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
882 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
883 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
886 BFD_ASSERT (!elf_flags_init (obfd
)
887 || (elf_elfheader (obfd
)->e_flags
888 == elf_elfheader (ibfd
)->e_flags
));
890 elf_gp (obfd
) = elf_gp (ibfd
);
891 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
892 elf_flags_init (obfd
) = true;
896 /* Print out the program headers. */
899 _bfd_elf_print_private_bfd_data (abfd
, farg
)
903 FILE *f
= (FILE *) farg
;
904 Elf_Internal_Phdr
*p
;
906 bfd_byte
*dynbuf
= NULL
;
908 p
= elf_tdata (abfd
)->phdr
;
913 fprintf (f
, _("\nProgram Header:\n"));
914 c
= elf_elfheader (abfd
)->e_phnum
;
915 for (i
= 0; i
< c
; i
++, p
++)
922 case PT_NULL
: pt
= "NULL"; break;
923 case PT_LOAD
: pt
= "LOAD"; break;
924 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
925 case PT_INTERP
: pt
= "INTERP"; break;
926 case PT_NOTE
: pt
= "NOTE"; break;
927 case PT_SHLIB
: pt
= "SHLIB"; break;
928 case PT_PHDR
: pt
= "PHDR"; break;
929 case PT_TLS
: pt
= "TLS"; break;
930 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
931 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
933 fprintf (f
, "%8s off 0x", pt
);
934 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
935 fprintf (f
, " vaddr 0x");
936 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
937 fprintf (f
, " paddr 0x");
938 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
939 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
940 fprintf (f
, " filesz 0x");
941 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
942 fprintf (f
, " memsz 0x");
943 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
944 fprintf (f
, " flags %c%c%c",
945 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
946 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
947 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
948 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
949 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
954 s
= bfd_get_section_by_name (abfd
, ".dynamic");
958 unsigned long shlink
;
959 bfd_byte
*extdyn
, *extdynend
;
961 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
963 fprintf (f
, _("\nDynamic Section:\n"));
965 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
968 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
972 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
975 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
977 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
978 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
981 extdynend
= extdyn
+ s
->_raw_size
;
982 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
984 Elf_Internal_Dyn dyn
;
989 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
991 if (dyn
.d_tag
== DT_NULL
)
998 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1002 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1003 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1004 case DT_PLTGOT
: name
= "PLTGOT"; break;
1005 case DT_HASH
: name
= "HASH"; break;
1006 case DT_STRTAB
: name
= "STRTAB"; break;
1007 case DT_SYMTAB
: name
= "SYMTAB"; break;
1008 case DT_RELA
: name
= "RELA"; break;
1009 case DT_RELASZ
: name
= "RELASZ"; break;
1010 case DT_RELAENT
: name
= "RELAENT"; break;
1011 case DT_STRSZ
: name
= "STRSZ"; break;
1012 case DT_SYMENT
: name
= "SYMENT"; break;
1013 case DT_INIT
: name
= "INIT"; break;
1014 case DT_FINI
: name
= "FINI"; break;
1015 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1016 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1017 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1018 case DT_REL
: name
= "REL"; break;
1019 case DT_RELSZ
: name
= "RELSZ"; break;
1020 case DT_RELENT
: name
= "RELENT"; break;
1021 case DT_PLTREL
: name
= "PLTREL"; break;
1022 case DT_DEBUG
: name
= "DEBUG"; break;
1023 case DT_TEXTREL
: name
= "TEXTREL"; break;
1024 case DT_JMPREL
: name
= "JMPREL"; break;
1025 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1026 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1027 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1028 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1029 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1030 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1031 case DT_FLAGS
: name
= "FLAGS"; break;
1032 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1033 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1034 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1035 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1036 case DT_MOVEENT
: name
= "MOVEENT"; break;
1037 case DT_MOVESZ
: name
= "MOVESZ"; break;
1038 case DT_FEATURE
: name
= "FEATURE"; break;
1039 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1040 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1041 case DT_SYMINENT
: name
= "SYMINENT"; break;
1042 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1043 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1044 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1045 case DT_PLTPAD
: name
= "PLTPAD"; break;
1046 case DT_MOVETAB
: name
= "MOVETAB"; break;
1047 case DT_SYMINFO
: name
= "SYMINFO"; break;
1048 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1049 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1050 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1051 case DT_VERSYM
: name
= "VERSYM"; break;
1052 case DT_VERDEF
: name
= "VERDEF"; break;
1053 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1054 case DT_VERNEED
: name
= "VERNEED"; break;
1055 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1056 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1057 case DT_USED
: name
= "USED"; break;
1058 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1061 fprintf (f
, " %-11s ", name
);
1063 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1067 unsigned int tagv
= dyn
.d_un
.d_val
;
1069 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1072 fprintf (f
, "%s", string
);
1081 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1082 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1084 if (! _bfd_elf_slurp_version_tables (abfd
))
1088 if (elf_dynverdef (abfd
) != 0)
1090 Elf_Internal_Verdef
*t
;
1092 fprintf (f
, _("\nVersion definitions:\n"));
1093 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1095 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1096 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1097 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1099 Elf_Internal_Verdaux
*a
;
1102 for (a
= t
->vd_auxptr
->vda_nextptr
;
1105 fprintf (f
, "%s ", a
->vda_nodename
);
1111 if (elf_dynverref (abfd
) != 0)
1113 Elf_Internal_Verneed
*t
;
1115 fprintf (f
, _("\nVersion References:\n"));
1116 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1118 Elf_Internal_Vernaux
*a
;
1120 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1121 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1122 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1123 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1135 /* Display ELF-specific fields of a symbol. */
1138 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1142 bfd_print_symbol_type how
;
1144 FILE *file
= (FILE *) filep
;
1147 case bfd_print_symbol_name
:
1148 fprintf (file
, "%s", symbol
->name
);
1150 case bfd_print_symbol_more
:
1151 fprintf (file
, "elf ");
1152 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1153 fprintf (file
, " %lx", (long) symbol
->flags
);
1155 case bfd_print_symbol_all
:
1157 const char *section_name
;
1158 const char *name
= NULL
;
1159 struct elf_backend_data
*bed
;
1160 unsigned char st_other
;
1163 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1165 bed
= get_elf_backend_data (abfd
);
1166 if (bed
->elf_backend_print_symbol_all
)
1167 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1171 name
= symbol
->name
;
1172 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1175 fprintf (file
, " %s\t", section_name
);
1176 /* Print the "other" value for a symbol. For common symbols,
1177 we've already printed the size; now print the alignment.
1178 For other symbols, we have no specified alignment, and
1179 we've printed the address; now print the size. */
1180 if (bfd_is_com_section (symbol
->section
))
1181 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1183 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1184 bfd_fprintf_vma (abfd
, file
, val
);
1186 /* If we have version information, print it. */
1187 if (elf_tdata (abfd
)->dynversym_section
!= 0
1188 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1189 || elf_tdata (abfd
)->dynverref_section
!= 0))
1191 unsigned int vernum
;
1192 const char *version_string
;
1194 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1197 version_string
= "";
1198 else if (vernum
== 1)
1199 version_string
= "Base";
1200 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1202 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1205 Elf_Internal_Verneed
*t
;
1207 version_string
= "";
1208 for (t
= elf_tdata (abfd
)->verref
;
1212 Elf_Internal_Vernaux
*a
;
1214 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1216 if (a
->vna_other
== vernum
)
1218 version_string
= a
->vna_nodename
;
1225 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1226 fprintf (file
, " %-11s", version_string
);
1231 fprintf (file
, " (%s)", version_string
);
1232 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1237 /* If the st_other field is not zero, print it. */
1238 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1243 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1244 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1245 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1247 /* Some other non-defined flags are also present, so print
1249 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1252 fprintf (file
, " %s", name
);
1258 /* Create an entry in an ELF linker hash table. */
1260 struct bfd_hash_entry
*
1261 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1262 struct bfd_hash_entry
*entry
;
1263 struct bfd_hash_table
*table
;
1266 /* Allocate the structure if it has not already been allocated by a
1270 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1275 /* Call the allocation method of the superclass. */
1276 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1279 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1280 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1282 /* Set local fields. */
1286 ret
->dynstr_index
= 0;
1287 ret
->weakdef
= NULL
;
1288 ret
->got
.refcount
= htab
->init_refcount
;
1289 ret
->plt
.refcount
= htab
->init_refcount
;
1290 ret
->linker_section_pointer
= NULL
;
1291 ret
->verinfo
.verdef
= NULL
;
1292 ret
->vtable_entries_used
= NULL
;
1293 ret
->vtable_entries_size
= 0;
1294 ret
->vtable_parent
= NULL
;
1295 ret
->type
= STT_NOTYPE
;
1297 /* Assume that we have been called by a non-ELF symbol reader.
1298 This flag is then reset by the code which reads an ELF input
1299 file. This ensures that a symbol created by a non-ELF symbol
1300 reader will have the flag set correctly. */
1301 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1307 /* Copy data from an indirect symbol to its direct symbol, hiding the
1308 old indirect symbol. Also used for copying flags to a weakdef. */
1311 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1312 struct elf_link_hash_entry
*dir
, *ind
;
1316 /* Copy down any references that we may have already seen to the
1317 symbol which just became indirect. */
1319 dir
->elf_link_hash_flags
|=
1320 (ind
->elf_link_hash_flags
1321 & (ELF_LINK_HASH_REF_DYNAMIC
1322 | ELF_LINK_HASH_REF_REGULAR
1323 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1324 | ELF_LINK_NON_GOT_REF
));
1326 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1329 /* Copy over the global and procedure linkage table refcount entries.
1330 These may have been already set up by a check_relocs routine. */
1331 tmp
= dir
->got
.refcount
;
1334 dir
->got
.refcount
= ind
->got
.refcount
;
1335 ind
->got
.refcount
= tmp
;
1338 BFD_ASSERT (ind
->got
.refcount
<= 0);
1340 tmp
= dir
->plt
.refcount
;
1343 dir
->plt
.refcount
= ind
->plt
.refcount
;
1344 ind
->plt
.refcount
= tmp
;
1347 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1349 if (dir
->dynindx
== -1)
1351 dir
->dynindx
= ind
->dynindx
;
1352 dir
->dynstr_index
= ind
->dynstr_index
;
1354 ind
->dynstr_index
= 0;
1357 BFD_ASSERT (ind
->dynindx
== -1);
1361 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
)
1362 struct bfd_link_info
*info
;
1363 struct elf_link_hash_entry
*h
;
1364 boolean force_local
;
1366 h
->plt
.offset
= (bfd_vma
) -1;
1367 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1370 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1371 if (h
->dynindx
!= -1)
1374 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1380 /* Initialize an ELF linker hash table. */
1383 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1384 struct elf_link_hash_table
*table
;
1386 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1387 struct bfd_hash_table
*,
1392 table
->dynamic_sections_created
= false;
1393 table
->dynobj
= NULL
;
1394 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1395 /* The first dynamic symbol is a dummy. */
1396 table
->dynsymcount
= 1;
1397 table
->dynstr
= NULL
;
1398 table
->bucketcount
= 0;
1399 table
->needed
= NULL
;
1400 table
->runpath
= NULL
;
1402 table
->stab_info
= NULL
;
1403 table
->merge_info
= NULL
;
1404 table
->dynlocal
= NULL
;
1405 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1406 table
->root
.type
= bfd_link_elf_hash_table
;
1411 /* Create an ELF linker hash table. */
1413 struct bfd_link_hash_table
*
1414 _bfd_elf_link_hash_table_create (abfd
)
1417 struct elf_link_hash_table
*ret
;
1418 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1420 ret
= (struct elf_link_hash_table
*) bfd_malloc (amt
);
1421 if (ret
== (struct elf_link_hash_table
*) NULL
)
1424 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1433 /* This is a hook for the ELF emulation code in the generic linker to
1434 tell the backend linker what file name to use for the DT_NEEDED
1435 entry for a dynamic object. The generic linker passes name as an
1436 empty string to indicate that no DT_NEEDED entry should be made. */
1439 bfd_elf_set_dt_needed_name (abfd
, name
)
1443 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1444 && bfd_get_format (abfd
) == bfd_object
)
1445 elf_dt_name (abfd
) = name
;
1449 bfd_elf_set_dt_needed_soname (abfd
, name
)
1453 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1454 && bfd_get_format (abfd
) == bfd_object
)
1455 elf_dt_soname (abfd
) = name
;
1458 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1459 the linker ELF emulation code. */
1461 struct bfd_link_needed_list
*
1462 bfd_elf_get_needed_list (abfd
, info
)
1463 bfd
*abfd ATTRIBUTE_UNUSED
;
1464 struct bfd_link_info
*info
;
1466 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1468 return elf_hash_table (info
)->needed
;
1471 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1472 hook for the linker ELF emulation code. */
1474 struct bfd_link_needed_list
*
1475 bfd_elf_get_runpath_list (abfd
, info
)
1476 bfd
*abfd ATTRIBUTE_UNUSED
;
1477 struct bfd_link_info
*info
;
1479 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1481 return elf_hash_table (info
)->runpath
;
1484 /* Get the name actually used for a dynamic object for a link. This
1485 is the SONAME entry if there is one. Otherwise, it is the string
1486 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1489 bfd_elf_get_dt_soname (abfd
)
1492 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1493 && bfd_get_format (abfd
) == bfd_object
)
1494 return elf_dt_name (abfd
);
1498 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1499 the ELF linker emulation code. */
1502 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1504 struct bfd_link_needed_list
**pneeded
;
1507 bfd_byte
*dynbuf
= NULL
;
1509 unsigned long shlink
;
1510 bfd_byte
*extdyn
, *extdynend
;
1512 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1516 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1517 || bfd_get_format (abfd
) != bfd_object
)
1520 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1521 if (s
== NULL
|| s
->_raw_size
== 0)
1524 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1528 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1532 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1536 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1538 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1539 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1542 extdynend
= extdyn
+ s
->_raw_size
;
1543 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1545 Elf_Internal_Dyn dyn
;
1547 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1549 if (dyn
.d_tag
== DT_NULL
)
1552 if (dyn
.d_tag
== DT_NEEDED
)
1555 struct bfd_link_needed_list
*l
;
1556 unsigned int tagv
= dyn
.d_un
.d_val
;
1559 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1564 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1585 /* Allocate an ELF string table--force the first byte to be zero. */
1587 struct bfd_strtab_hash
*
1588 _bfd_elf_stringtab_init ()
1590 struct bfd_strtab_hash
*ret
;
1592 ret
= _bfd_stringtab_init ();
1597 loc
= _bfd_stringtab_add (ret
, "", true, false);
1598 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1599 if (loc
== (bfd_size_type
) -1)
1601 _bfd_stringtab_free (ret
);
1608 /* ELF .o/exec file reading */
1610 /* Create a new bfd section from an ELF section header. */
1613 bfd_section_from_shdr (abfd
, shindex
)
1615 unsigned int shindex
;
1617 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1618 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1619 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1622 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1624 switch (hdr
->sh_type
)
1627 /* Inactive section. Throw it away. */
1630 case SHT_PROGBITS
: /* Normal section with contents. */
1631 case SHT_DYNAMIC
: /* Dynamic linking information. */
1632 case SHT_NOBITS
: /* .bss section. */
1633 case SHT_HASH
: /* .hash section. */
1634 case SHT_NOTE
: /* .note section. */
1635 case SHT_INIT_ARRAY
: /* .init_array section. */
1636 case SHT_FINI_ARRAY
: /* .fini_array section. */
1637 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1638 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1640 case SHT_SYMTAB
: /* A symbol table */
1641 if (elf_onesymtab (abfd
) == shindex
)
1644 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1645 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1646 elf_onesymtab (abfd
) = shindex
;
1647 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1648 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1649 abfd
->flags
|= HAS_SYMS
;
1651 /* Sometimes a shared object will map in the symbol table. If
1652 SHF_ALLOC is set, and this is a shared object, then we also
1653 treat this section as a BFD section. We can not base the
1654 decision purely on SHF_ALLOC, because that flag is sometimes
1655 set in a relocateable object file, which would confuse the
1657 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1658 && (abfd
->flags
& DYNAMIC
) != 0
1659 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1664 case SHT_DYNSYM
: /* A dynamic symbol table */
1665 if (elf_dynsymtab (abfd
) == shindex
)
1668 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1669 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1670 elf_dynsymtab (abfd
) = shindex
;
1671 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1672 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1673 abfd
->flags
|= HAS_SYMS
;
1675 /* Besides being a symbol table, we also treat this as a regular
1676 section, so that objcopy can handle it. */
1677 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1679 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1680 if (elf_symtab_shndx (abfd
) == shindex
)
1683 /* Get the associated symbol table. */
1684 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1685 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1688 elf_symtab_shndx (abfd
) = shindex
;
1689 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1690 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1693 case SHT_STRTAB
: /* A string table */
1694 if (hdr
->bfd_section
!= NULL
)
1696 if (ehdr
->e_shstrndx
== shindex
)
1698 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1699 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1703 unsigned int i
, num_sec
;
1705 num_sec
= elf_numsections (abfd
);
1706 for (i
= 1; i
< num_sec
; i
++)
1708 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1709 if (hdr2
->sh_link
== shindex
)
1711 if (! bfd_section_from_shdr (abfd
, i
))
1713 if (elf_onesymtab (abfd
) == i
)
1715 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1716 elf_elfsections (abfd
)[shindex
] =
1717 &elf_tdata (abfd
)->strtab_hdr
;
1720 if (elf_dynsymtab (abfd
) == i
)
1722 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1723 elf_elfsections (abfd
)[shindex
] = hdr
=
1724 &elf_tdata (abfd
)->dynstrtab_hdr
;
1725 /* We also treat this as a regular section, so
1726 that objcopy can handle it. */
1729 #if 0 /* Not handling other string tables specially right now. */
1730 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1731 /* We have a strtab for some random other section. */
1732 newsect
= (asection
*) hdr2
->bfd_section
;
1735 hdr
->bfd_section
= newsect
;
1736 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1738 elf_elfsections (abfd
)[shindex
] = hdr2
;
1744 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1748 /* *These* do a lot of work -- but build no sections! */
1750 asection
*target_sect
;
1751 Elf_Internal_Shdr
*hdr2
;
1752 unsigned int num_sec
= elf_numsections (abfd
);
1754 /* Check for a bogus link to avoid crashing. */
1755 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1756 || hdr
->sh_link
>= num_sec
)
1758 ((*_bfd_error_handler
)
1759 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1760 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1761 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1764 /* For some incomprehensible reason Oracle distributes
1765 libraries for Solaris in which some of the objects have
1766 bogus sh_link fields. It would be nice if we could just
1767 reject them, but, unfortunately, some people need to use
1768 them. We scan through the section headers; if we find only
1769 one suitable symbol table, we clobber the sh_link to point
1770 to it. I hope this doesn't break anything. */
1771 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1772 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1778 for (scan
= 1; scan
< num_sec
; scan
++)
1780 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1781 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1792 hdr
->sh_link
= found
;
1795 /* Get the symbol table. */
1796 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1797 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1800 /* If this reloc section does not use the main symbol table we
1801 don't treat it as a reloc section. BFD can't adequately
1802 represent such a section, so at least for now, we don't
1803 try. We just present it as a normal section. We also
1804 can't use it as a reloc section if it points to the null
1806 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1807 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1809 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1811 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1812 if (target_sect
== NULL
)
1815 if ((target_sect
->flags
& SEC_RELOC
) == 0
1816 || target_sect
->reloc_count
== 0)
1817 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1821 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1822 amt
= sizeof (*hdr2
);
1823 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1824 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1827 elf_elfsections (abfd
)[shindex
] = hdr2
;
1828 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1829 target_sect
->flags
|= SEC_RELOC
;
1830 target_sect
->relocation
= NULL
;
1831 target_sect
->rel_filepos
= hdr
->sh_offset
;
1832 /* In the section to which the relocations apply, mark whether
1833 its relocations are of the REL or RELA variety. */
1834 if (hdr
->sh_size
!= 0)
1835 elf_section_data (target_sect
)->use_rela_p
1836 = (hdr
->sh_type
== SHT_RELA
);
1837 abfd
->flags
|= HAS_RELOC
;
1842 case SHT_GNU_verdef
:
1843 elf_dynverdef (abfd
) = shindex
;
1844 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1845 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1848 case SHT_GNU_versym
:
1849 elf_dynversym (abfd
) = shindex
;
1850 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1851 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1854 case SHT_GNU_verneed
:
1855 elf_dynverref (abfd
) = shindex
;
1856 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1857 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1864 /* We need a BFD section for objcopy and relocatable linking,
1865 and it's handy to have the signature available as the section
1867 name
= group_signature (abfd
, hdr
);
1870 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1872 if (hdr
->contents
!= NULL
)
1874 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1875 unsigned int n_elt
= hdr
->sh_size
/ 4;
1878 if (idx
->flags
& GRP_COMDAT
)
1879 hdr
->bfd_section
->flags
1880 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1882 while (--n_elt
!= 0)
1883 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1884 && elf_next_in_group (s
) != NULL
)
1886 elf_next_in_group (hdr
->bfd_section
) = s
;
1893 /* Check for any processor-specific section types. */
1895 if (bed
->elf_backend_section_from_shdr
)
1896 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1904 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1905 Return SEC for sections that have no elf section, and NULL on error. */
1908 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
1910 struct sym_sec_cache
*cache
;
1912 unsigned long r_symndx
;
1914 unsigned char esym_shndx
[4];
1915 unsigned int isym_shndx
;
1916 Elf_Internal_Shdr
*symtab_hdr
;
1919 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1921 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1922 return cache
->sec
[ent
];
1924 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1925 pos
= symtab_hdr
->sh_offset
;
1926 if (get_elf_backend_data (abfd
)->s
->sizeof_sym
1927 == sizeof (Elf64_External_Sym
))
1929 pos
+= r_symndx
* sizeof (Elf64_External_Sym
);
1930 pos
+= offsetof (Elf64_External_Sym
, st_shndx
);
1931 amt
= sizeof (((Elf64_External_Sym
*) 0)->st_shndx
);
1935 pos
+= r_symndx
* sizeof (Elf32_External_Sym
);
1936 pos
+= offsetof (Elf32_External_Sym
, st_shndx
);
1937 amt
= sizeof (((Elf32_External_Sym
*) 0)->st_shndx
);
1939 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1940 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1942 isym_shndx
= H_GET_16 (abfd
, esym_shndx
);
1944 if (isym_shndx
== SHN_XINDEX
)
1946 Elf_Internal_Shdr
*shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1947 if (shndx_hdr
->sh_size
!= 0)
1949 pos
= shndx_hdr
->sh_offset
;
1950 pos
+= r_symndx
* sizeof (Elf_External_Sym_Shndx
);
1951 amt
= sizeof (Elf_External_Sym_Shndx
);
1952 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1953 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1955 isym_shndx
= H_GET_32 (abfd
, esym_shndx
);
1959 if (cache
->abfd
!= abfd
)
1961 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1964 cache
->indx
[ent
] = r_symndx
;
1965 cache
->sec
[ent
] = sec
;
1966 if (isym_shndx
< SHN_LORESERVE
|| isym_shndx
> SHN_HIRESERVE
)
1969 s
= bfd_section_from_elf_index (abfd
, isym_shndx
);
1971 cache
->sec
[ent
] = s
;
1973 return cache
->sec
[ent
];
1976 /* Given an ELF section number, retrieve the corresponding BFD
1980 bfd_section_from_elf_index (abfd
, index
)
1984 if (index
>= elf_numsections (abfd
))
1986 return elf_elfsections (abfd
)[index
]->bfd_section
;
1990 _bfd_elf_new_section_hook (abfd
, sec
)
1994 struct bfd_elf_section_data
*sdata
;
1995 bfd_size_type amt
= sizeof (*sdata
);
1997 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
2000 sec
->used_by_bfd
= (PTR
) sdata
;
2002 /* Indicate whether or not this section should use RELA relocations. */
2004 = get_elf_backend_data (abfd
)->default_use_rela_p
;
2009 /* Create a new bfd section from an ELF program header.
2011 Since program segments have no names, we generate a synthetic name
2012 of the form segment<NUM>, where NUM is generally the index in the
2013 program header table. For segments that are split (see below) we
2014 generate the names segment<NUM>a and segment<NUM>b.
2016 Note that some program segments may have a file size that is different than
2017 (less than) the memory size. All this means is that at execution the
2018 system must allocate the amount of memory specified by the memory size,
2019 but only initialize it with the first "file size" bytes read from the
2020 file. This would occur for example, with program segments consisting
2021 of combined data+bss.
2023 To handle the above situation, this routine generates TWO bfd sections
2024 for the single program segment. The first has the length specified by
2025 the file size of the segment, and the second has the length specified
2026 by the difference between the two sizes. In effect, the segment is split
2027 into it's initialized and uninitialized parts.
2032 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
2034 Elf_Internal_Phdr
*hdr
;
2036 const char *typename
;
2043 split
= ((hdr
->p_memsz
> 0)
2044 && (hdr
->p_filesz
> 0)
2045 && (hdr
->p_memsz
> hdr
->p_filesz
));
2046 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2047 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2050 strcpy (name
, namebuf
);
2051 newsect
= bfd_make_section (abfd
, name
);
2052 if (newsect
== NULL
)
2054 newsect
->vma
= hdr
->p_vaddr
;
2055 newsect
->lma
= hdr
->p_paddr
;
2056 newsect
->_raw_size
= hdr
->p_filesz
;
2057 newsect
->filepos
= hdr
->p_offset
;
2058 newsect
->flags
|= SEC_HAS_CONTENTS
;
2059 if (hdr
->p_type
== PT_LOAD
)
2061 newsect
->flags
|= SEC_ALLOC
;
2062 newsect
->flags
|= SEC_LOAD
;
2063 if (hdr
->p_flags
& PF_X
)
2065 /* FIXME: all we known is that it has execute PERMISSION,
2067 newsect
->flags
|= SEC_CODE
;
2070 if (!(hdr
->p_flags
& PF_W
))
2072 newsect
->flags
|= SEC_READONLY
;
2077 sprintf (namebuf
, "%s%db", typename
, index
);
2078 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2081 strcpy (name
, namebuf
);
2082 newsect
= bfd_make_section (abfd
, name
);
2083 if (newsect
== NULL
)
2085 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2086 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2087 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2088 if (hdr
->p_type
== PT_LOAD
)
2090 newsect
->flags
|= SEC_ALLOC
;
2091 if (hdr
->p_flags
& PF_X
)
2092 newsect
->flags
|= SEC_CODE
;
2094 if (!(hdr
->p_flags
& PF_W
))
2095 newsect
->flags
|= SEC_READONLY
;
2102 bfd_section_from_phdr (abfd
, hdr
, index
)
2104 Elf_Internal_Phdr
*hdr
;
2107 struct elf_backend_data
*bed
;
2109 switch (hdr
->p_type
)
2112 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2115 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2118 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2121 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2124 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2126 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2131 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2134 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2137 /* Check for any processor-specific program segment types.
2138 If no handler for them, default to making "segment" sections. */
2139 bed
= get_elf_backend_data (abfd
);
2140 if (bed
->elf_backend_section_from_phdr
)
2141 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2143 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2147 /* Initialize REL_HDR, the section-header for new section, containing
2148 relocations against ASECT. If USE_RELA_P is true, we use RELA
2149 relocations; otherwise, we use REL relocations. */
2152 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2154 Elf_Internal_Shdr
*rel_hdr
;
2159 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2160 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2162 name
= bfd_alloc (abfd
, amt
);
2165 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2167 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2169 if (rel_hdr
->sh_name
== (unsigned int) -1)
2171 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2172 rel_hdr
->sh_entsize
= (use_rela_p
2173 ? bed
->s
->sizeof_rela
2174 : bed
->s
->sizeof_rel
);
2175 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2176 rel_hdr
->sh_flags
= 0;
2177 rel_hdr
->sh_addr
= 0;
2178 rel_hdr
->sh_size
= 0;
2179 rel_hdr
->sh_offset
= 0;
2184 /* Set up an ELF internal section header for a section. */
2187 elf_fake_sections (abfd
, asect
, failedptrarg
)
2192 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2193 boolean
*failedptr
= (boolean
*) failedptrarg
;
2194 Elf_Internal_Shdr
*this_hdr
;
2198 /* We already failed; just get out of the bfd_map_over_sections
2203 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2205 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2206 asect
->name
, false);
2207 if (this_hdr
->sh_name
== (unsigned long) -1)
2213 this_hdr
->sh_flags
= 0;
2215 if ((asect
->flags
& SEC_ALLOC
) != 0
2216 || asect
->user_set_vma
)
2217 this_hdr
->sh_addr
= asect
->vma
;
2219 this_hdr
->sh_addr
= 0;
2221 this_hdr
->sh_offset
= 0;
2222 this_hdr
->sh_size
= asect
->_raw_size
;
2223 this_hdr
->sh_link
= 0;
2224 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2225 /* The sh_entsize and sh_info fields may have been set already by
2226 copy_private_section_data. */
2228 this_hdr
->bfd_section
= asect
;
2229 this_hdr
->contents
= NULL
;
2231 /* FIXME: This should not be based on section names. */
2232 if (strcmp (asect
->name
, ".dynstr") == 0)
2233 this_hdr
->sh_type
= SHT_STRTAB
;
2234 else if (strcmp (asect
->name
, ".hash") == 0)
2236 this_hdr
->sh_type
= SHT_HASH
;
2237 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2239 else if (strcmp (asect
->name
, ".dynsym") == 0)
2241 this_hdr
->sh_type
= SHT_DYNSYM
;
2242 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2244 else if (strcmp (asect
->name
, ".dynamic") == 0)
2246 this_hdr
->sh_type
= SHT_DYNAMIC
;
2247 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2249 else if (strncmp (asect
->name
, ".rela", 5) == 0
2250 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2252 this_hdr
->sh_type
= SHT_RELA
;
2253 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2255 else if (strncmp (asect
->name
, ".rel", 4) == 0
2256 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2258 this_hdr
->sh_type
= SHT_REL
;
2259 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2261 else if (strcmp (asect
->name
, ".init_array") == 0)
2262 this_hdr
->sh_type
= SHT_INIT_ARRAY
;
2263 else if (strcmp (asect
->name
, ".fini_array") == 0)
2264 this_hdr
->sh_type
= SHT_FINI_ARRAY
;
2265 else if (strcmp (asect
->name
, ".preinit_array") == 0)
2266 this_hdr
->sh_type
= SHT_PREINIT_ARRAY
;
2267 else if (strncmp (asect
->name
, ".note", 5) == 0)
2268 this_hdr
->sh_type
= SHT_NOTE
;
2269 else if (strncmp (asect
->name
, ".stab", 5) == 0
2270 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2271 this_hdr
->sh_type
= SHT_STRTAB
;
2272 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2274 this_hdr
->sh_type
= SHT_GNU_versym
;
2275 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2277 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2279 this_hdr
->sh_type
= SHT_GNU_verdef
;
2280 this_hdr
->sh_entsize
= 0;
2281 /* objcopy or strip will copy over sh_info, but may not set
2282 cverdefs. The linker will set cverdefs, but sh_info will be
2284 if (this_hdr
->sh_info
== 0)
2285 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2287 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2288 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2290 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2292 this_hdr
->sh_type
= SHT_GNU_verneed
;
2293 this_hdr
->sh_entsize
= 0;
2294 /* objcopy or strip will copy over sh_info, but may not set
2295 cverrefs. The linker will set cverrefs, but sh_info will be
2297 if (this_hdr
->sh_info
== 0)
2298 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2300 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2301 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2303 else if ((asect
->flags
& SEC_GROUP
) != 0)
2305 this_hdr
->sh_type
= SHT_GROUP
;
2306 this_hdr
->sh_entsize
= 4;
2308 else if ((asect
->flags
& SEC_ALLOC
) != 0
2309 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2310 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2311 this_hdr
->sh_type
= SHT_NOBITS
;
2313 this_hdr
->sh_type
= SHT_PROGBITS
;
2315 if ((asect
->flags
& SEC_ALLOC
) != 0)
2316 this_hdr
->sh_flags
|= SHF_ALLOC
;
2317 if ((asect
->flags
& SEC_READONLY
) == 0)
2318 this_hdr
->sh_flags
|= SHF_WRITE
;
2319 if ((asect
->flags
& SEC_CODE
) != 0)
2320 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2321 if ((asect
->flags
& SEC_MERGE
) != 0)
2323 this_hdr
->sh_flags
|= SHF_MERGE
;
2324 this_hdr
->sh_entsize
= asect
->entsize
;
2325 if ((asect
->flags
& SEC_STRINGS
) != 0)
2326 this_hdr
->sh_flags
|= SHF_STRINGS
;
2328 if (elf_group_name (asect
) != NULL
)
2329 this_hdr
->sh_flags
|= SHF_GROUP
;
2330 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2331 this_hdr
->sh_flags
|= SHF_TLS
;
2333 /* Check for processor-specific section types. */
2334 if (bed
->elf_backend_fake_sections
2335 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2338 /* If the section has relocs, set up a section header for the
2339 SHT_REL[A] section. If two relocation sections are required for
2340 this section, it is up to the processor-specific back-end to
2341 create the other. */
2342 if ((asect
->flags
& SEC_RELOC
) != 0
2343 && !_bfd_elf_init_reloc_shdr (abfd
,
2344 &elf_section_data (asect
)->rel_hdr
,
2346 elf_section_data (asect
)->use_rela_p
))
2350 /* Fill in the contents of a SHT_GROUP section. */
2353 set_group_contents (abfd
, sec
, failedptrarg
)
2356 PTR failedptrarg ATTRIBUTE_UNUSED
;
2358 boolean
*failedptr
= (boolean
*) failedptrarg
;
2359 unsigned long symindx
;
2362 struct bfd_link_order
*l
;
2364 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2368 /* If called from the assembler, swap_out_syms will have set up
2369 elf_section_syms; If called for "ld -r", the symbols won't yet
2370 be mapped, so emulate elf_bfd_final_link. */
2371 if (elf_section_syms (abfd
) != NULL
)
2372 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2374 symindx
= elf_section_data (sec
)->this_idx
;
2375 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2377 /* Nor will the contents be allocated for "ld -r". */
2378 if (sec
->contents
== NULL
)
2380 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2381 if (sec
->contents
== NULL
)
2388 loc
= sec
->contents
+ sec
->_raw_size
;
2390 /* Get the pointer to the first section in the group that we
2391 squirreled away here. */
2392 elt
= elf_next_in_group (sec
);
2394 /* First element is a flag word. Rest of section is elf section
2395 indices for all the sections of the group. Write them backwards
2396 just to keep the group in the same order as given in .section
2397 directives, not that it matters. */
2401 H_PUT_32 (abfd
, elf_section_data (elt
)->this_idx
, loc
);
2402 elt
= elf_next_in_group (elt
);
2405 /* If this is a relocatable link, then the above did nothing because
2406 SEC is the output section. Look through the input sections
2408 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2409 if (l
->type
== bfd_indirect_link_order
2410 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2415 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2416 elt
= elf_next_in_group (elt
);
2417 /* During a relocatable link, the lists are circular. */
2419 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2422 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2424 BFD_ASSERT (loc
== sec
->contents
);
2427 /* Assign all ELF section numbers. The dummy first section is handled here
2428 too. The link/info pointers for the standard section types are filled
2429 in here too, while we're at it. */
2432 assign_section_numbers (abfd
)
2435 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2437 unsigned int section_number
, secn
;
2438 Elf_Internal_Shdr
**i_shdrp
;
2443 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2445 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2447 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2449 if (section_number
== SHN_LORESERVE
)
2450 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2451 d
->this_idx
= section_number
++;
2452 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2453 if ((sec
->flags
& SEC_RELOC
) == 0)
2457 if (section_number
== SHN_LORESERVE
)
2458 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2459 d
->rel_idx
= section_number
++;
2460 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2465 if (section_number
== SHN_LORESERVE
)
2466 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2467 d
->rel_idx2
= section_number
++;
2468 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2474 if (section_number
== SHN_LORESERVE
)
2475 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2476 t
->shstrtab_section
= section_number
++;
2477 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2478 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2480 if (bfd_get_symcount (abfd
) > 0)
2482 if (section_number
== SHN_LORESERVE
)
2483 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2484 t
->symtab_section
= section_number
++;
2485 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2486 if (section_number
> SHN_LORESERVE
- 2)
2488 if (section_number
== SHN_LORESERVE
)
2489 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2490 t
->symtab_shndx_section
= section_number
++;
2491 t
->symtab_shndx_hdr
.sh_name
2492 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2493 ".symtab_shndx", false);
2494 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2497 if (section_number
== SHN_LORESERVE
)
2498 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2499 t
->strtab_section
= section_number
++;
2500 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2503 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2504 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2506 elf_numsections (abfd
) = section_number
;
2507 elf_elfheader (abfd
)->e_shnum
= section_number
;
2508 if (section_number
> SHN_LORESERVE
)
2509 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2511 /* Set up the list of section header pointers, in agreement with the
2513 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2514 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2515 if (i_shdrp
== NULL
)
2518 amt
= sizeof (Elf_Internal_Shdr
);
2519 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2520 if (i_shdrp
[0] == NULL
)
2522 bfd_release (abfd
, i_shdrp
);
2525 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2527 elf_elfsections (abfd
) = i_shdrp
;
2529 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2530 if (bfd_get_symcount (abfd
) > 0)
2532 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2533 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2535 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2536 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2538 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2539 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2541 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2543 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2547 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2548 if (d
->rel_idx
!= 0)
2549 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2550 if (d
->rel_idx2
!= 0)
2551 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2553 /* Fill in the sh_link and sh_info fields while we're at it. */
2555 /* sh_link of a reloc section is the section index of the symbol
2556 table. sh_info is the section index of the section to which
2557 the relocation entries apply. */
2558 if (d
->rel_idx
!= 0)
2560 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2561 d
->rel_hdr
.sh_info
= d
->this_idx
;
2563 if (d
->rel_idx2
!= 0)
2565 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2566 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2569 switch (d
->this_hdr
.sh_type
)
2573 /* A reloc section which we are treating as a normal BFD
2574 section. sh_link is the section index of the symbol
2575 table. sh_info is the section index of the section to
2576 which the relocation entries apply. We assume that an
2577 allocated reloc section uses the dynamic symbol table.
2578 FIXME: How can we be sure? */
2579 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2581 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2583 /* We look up the section the relocs apply to by name. */
2585 if (d
->this_hdr
.sh_type
== SHT_REL
)
2589 s
= bfd_get_section_by_name (abfd
, name
);
2591 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2595 /* We assume that a section named .stab*str is a stabs
2596 string section. We look for a section with the same name
2597 but without the trailing ``str'', and set its sh_link
2598 field to point to this section. */
2599 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2600 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2605 len
= strlen (sec
->name
);
2606 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2609 strncpy (alc
, sec
->name
, len
- 3);
2610 alc
[len
- 3] = '\0';
2611 s
= bfd_get_section_by_name (abfd
, alc
);
2615 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2617 /* This is a .stab section. */
2618 elf_section_data (s
)->this_hdr
.sh_entsize
=
2619 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2626 case SHT_GNU_verneed
:
2627 case SHT_GNU_verdef
:
2628 /* sh_link is the section header index of the string table
2629 used for the dynamic entries, or the symbol table, or the
2631 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2633 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2637 case SHT_GNU_versym
:
2638 /* sh_link is the section header index of the symbol table
2639 this hash table or version table is for. */
2640 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2642 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2646 d
->this_hdr
.sh_link
= t
->symtab_section
;
2650 for (secn
= 1; secn
< section_number
; ++secn
)
2651 if (i_shdrp
[secn
] == NULL
)
2652 i_shdrp
[secn
] = i_shdrp
[0];
2654 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2655 i_shdrp
[secn
]->sh_name
);
2659 /* Map symbol from it's internal number to the external number, moving
2660 all local symbols to be at the head of the list. */
2663 sym_is_global (abfd
, sym
)
2667 /* If the backend has a special mapping, use it. */
2668 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2669 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2672 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2673 || bfd_is_und_section (bfd_get_section (sym
))
2674 || bfd_is_com_section (bfd_get_section (sym
)));
2678 elf_map_symbols (abfd
)
2681 unsigned int symcount
= bfd_get_symcount (abfd
);
2682 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2683 asymbol
**sect_syms
;
2684 unsigned int num_locals
= 0;
2685 unsigned int num_globals
= 0;
2686 unsigned int num_locals2
= 0;
2687 unsigned int num_globals2
= 0;
2695 fprintf (stderr
, "elf_map_symbols\n");
2699 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2701 if (max_index
< asect
->index
)
2702 max_index
= asect
->index
;
2706 amt
= max_index
* sizeof (asymbol
*);
2707 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2708 if (sect_syms
== NULL
)
2710 elf_section_syms (abfd
) = sect_syms
;
2711 elf_num_section_syms (abfd
) = max_index
;
2713 /* Init sect_syms entries for any section symbols we have already
2714 decided to output. */
2715 for (idx
= 0; idx
< symcount
; idx
++)
2717 asymbol
*sym
= syms
[idx
];
2719 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2726 if (sec
->owner
!= NULL
)
2728 if (sec
->owner
!= abfd
)
2730 if (sec
->output_offset
!= 0)
2733 sec
= sec
->output_section
;
2735 /* Empty sections in the input files may have had a
2736 section symbol created for them. (See the comment
2737 near the end of _bfd_generic_link_output_symbols in
2738 linker.c). If the linker script discards such
2739 sections then we will reach this point. Since we know
2740 that we cannot avoid this case, we detect it and skip
2741 the abort and the assignment to the sect_syms array.
2742 To reproduce this particular case try running the
2743 linker testsuite test ld-scripts/weak.exp for an ELF
2744 port that uses the generic linker. */
2745 if (sec
->owner
== NULL
)
2748 BFD_ASSERT (sec
->owner
== abfd
);
2750 sect_syms
[sec
->index
] = syms
[idx
];
2755 /* Classify all of the symbols. */
2756 for (idx
= 0; idx
< symcount
; idx
++)
2758 if (!sym_is_global (abfd
, syms
[idx
]))
2764 /* We will be adding a section symbol for each BFD section. Most normal
2765 sections will already have a section symbol in outsymbols, but
2766 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2767 at least in that case. */
2768 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2770 if (sect_syms
[asect
->index
] == NULL
)
2772 if (!sym_is_global (abfd
, asect
->symbol
))
2779 /* Now sort the symbols so the local symbols are first. */
2780 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2781 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2783 if (new_syms
== NULL
)
2786 for (idx
= 0; idx
< symcount
; idx
++)
2788 asymbol
*sym
= syms
[idx
];
2791 if (!sym_is_global (abfd
, sym
))
2794 i
= num_locals
+ num_globals2
++;
2796 sym
->udata
.i
= i
+ 1;
2798 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2800 if (sect_syms
[asect
->index
] == NULL
)
2802 asymbol
*sym
= asect
->symbol
;
2805 sect_syms
[asect
->index
] = sym
;
2806 if (!sym_is_global (abfd
, sym
))
2809 i
= num_locals
+ num_globals2
++;
2811 sym
->udata
.i
= i
+ 1;
2815 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2817 elf_num_locals (abfd
) = num_locals
;
2818 elf_num_globals (abfd
) = num_globals
;
2822 /* Align to the maximum file alignment that could be required for any
2823 ELF data structure. */
2825 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2826 static INLINE file_ptr
2827 align_file_position (off
, align
)
2831 return (off
+ align
- 1) & ~(align
- 1);
2834 /* Assign a file position to a section, optionally aligning to the
2835 required section alignment. */
2838 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2839 Elf_Internal_Shdr
*i_shdrp
;
2847 al
= i_shdrp
->sh_addralign
;
2849 offset
= BFD_ALIGN (offset
, al
);
2851 i_shdrp
->sh_offset
= offset
;
2852 if (i_shdrp
->bfd_section
!= NULL
)
2853 i_shdrp
->bfd_section
->filepos
= offset
;
2854 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2855 offset
+= i_shdrp
->sh_size
;
2859 /* Compute the file positions we are going to put the sections at, and
2860 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2861 is not NULL, this is being called by the ELF backend linker. */
2864 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2866 struct bfd_link_info
*link_info
;
2868 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2870 struct bfd_strtab_hash
*strtab
;
2871 Elf_Internal_Shdr
*shstrtab_hdr
;
2873 if (abfd
->output_has_begun
)
2876 /* Do any elf backend specific processing first. */
2877 if (bed
->elf_backend_begin_write_processing
)
2878 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2880 if (! prep_headers (abfd
))
2883 /* Post process the headers if necessary. */
2884 if (bed
->elf_backend_post_process_headers
)
2885 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2888 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2892 if (!assign_section_numbers (abfd
))
2895 /* The backend linker builds symbol table information itself. */
2896 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2898 /* Non-zero if doing a relocatable link. */
2899 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2901 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2905 if (link_info
== NULL
|| link_info
->relocateable
)
2907 bfd_map_over_sections (abfd
, set_group_contents
, &failed
);
2912 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2913 /* sh_name was set in prep_headers. */
2914 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2915 shstrtab_hdr
->sh_flags
= 0;
2916 shstrtab_hdr
->sh_addr
= 0;
2917 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2918 shstrtab_hdr
->sh_entsize
= 0;
2919 shstrtab_hdr
->sh_link
= 0;
2920 shstrtab_hdr
->sh_info
= 0;
2921 /* sh_offset is set in assign_file_positions_except_relocs. */
2922 shstrtab_hdr
->sh_addralign
= 1;
2924 if (!assign_file_positions_except_relocs (abfd
))
2927 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2930 Elf_Internal_Shdr
*hdr
;
2932 off
= elf_tdata (abfd
)->next_file_pos
;
2934 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2935 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2937 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2938 if (hdr
->sh_size
!= 0)
2939 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2941 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2942 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2944 elf_tdata (abfd
)->next_file_pos
= off
;
2946 /* Now that we know where the .strtab section goes, write it
2948 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2949 || ! _bfd_stringtab_emit (abfd
, strtab
))
2951 _bfd_stringtab_free (strtab
);
2954 abfd
->output_has_begun
= true;
2959 /* Create a mapping from a set of sections to a program segment. */
2961 static INLINE
struct elf_segment_map
*
2962 make_mapping (abfd
, sections
, from
, to
, phdr
)
2964 asection
**sections
;
2969 struct elf_segment_map
*m
;
2974 amt
= sizeof (struct elf_segment_map
);
2975 amt
+= (to
- from
- 1) * sizeof (asection
*);
2976 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2980 m
->p_type
= PT_LOAD
;
2981 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2982 m
->sections
[i
- from
] = *hdrpp
;
2983 m
->count
= to
- from
;
2985 if (from
== 0 && phdr
)
2987 /* Include the headers in the first PT_LOAD segment. */
2988 m
->includes_filehdr
= 1;
2989 m
->includes_phdrs
= 1;
2995 /* Set up a mapping from BFD sections to program segments. */
2998 map_sections_to_segments (abfd
)
3001 asection
**sections
= NULL
;
3005 struct elf_segment_map
*mfirst
;
3006 struct elf_segment_map
**pm
;
3007 struct elf_segment_map
*m
;
3009 unsigned int phdr_index
;
3010 bfd_vma maxpagesize
;
3012 boolean phdr_in_segment
= true;
3015 asection
*first_tls
= NULL
;
3016 asection
*dynsec
, *eh_frame_hdr
;
3019 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3022 if (bfd_count_sections (abfd
) == 0)
3025 /* Select the allocated sections, and sort them. */
3027 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3028 sections
= (asection
**) bfd_malloc (amt
);
3029 if (sections
== NULL
)
3033 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3035 if ((s
->flags
& SEC_ALLOC
) != 0)
3041 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3044 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3046 /* Build the mapping. */
3051 /* If we have a .interp section, then create a PT_PHDR segment for
3052 the program headers and a PT_INTERP segment for the .interp
3054 s
= bfd_get_section_by_name (abfd
, ".interp");
3055 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3057 amt
= sizeof (struct elf_segment_map
);
3058 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3062 m
->p_type
= PT_PHDR
;
3063 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3064 m
->p_flags
= PF_R
| PF_X
;
3065 m
->p_flags_valid
= 1;
3066 m
->includes_phdrs
= 1;
3071 amt
= sizeof (struct elf_segment_map
);
3072 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3076 m
->p_type
= PT_INTERP
;
3084 /* Look through the sections. We put sections in the same program
3085 segment when the start of the second section can be placed within
3086 a few bytes of the end of the first section. */
3089 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3091 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3093 && (dynsec
->flags
& SEC_LOAD
) == 0)
3096 /* Deal with -Ttext or something similar such that the first section
3097 is not adjacent to the program headers. This is an
3098 approximation, since at this point we don't know exactly how many
3099 program headers we will need. */
3102 bfd_size_type phdr_size
;
3104 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3106 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3107 if ((abfd
->flags
& D_PAGED
) == 0
3108 || sections
[0]->lma
< phdr_size
3109 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3110 phdr_in_segment
= false;
3113 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3116 boolean new_segment
;
3120 /* See if this section and the last one will fit in the same
3123 if (last_hdr
== NULL
)
3125 /* If we don't have a segment yet, then we don't need a new
3126 one (we build the last one after this loop). */
3127 new_segment
= false;
3129 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3131 /* If this section has a different relation between the
3132 virtual address and the load address, then we need a new
3136 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3137 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3139 /* If putting this section in this segment would force us to
3140 skip a page in the segment, then we need a new segment. */
3143 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3144 && (hdr
->flags
& SEC_LOAD
) != 0)
3146 /* We don't want to put a loadable section after a
3147 nonloadable section in the same segment. */
3150 else if ((abfd
->flags
& D_PAGED
) == 0)
3152 /* If the file is not demand paged, which means that we
3153 don't require the sections to be correctly aligned in the
3154 file, then there is no other reason for a new segment. */
3155 new_segment
= false;
3158 && (hdr
->flags
& SEC_READONLY
) == 0
3159 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3162 /* We don't want to put a writable section in a read only
3163 segment, unless they are on the same page in memory
3164 anyhow. We already know that the last section does not
3165 bring us past the current section on the page, so the
3166 only case in which the new section is not on the same
3167 page as the previous section is when the previous section
3168 ends precisely on a page boundary. */
3173 /* Otherwise, we can use the same segment. */
3174 new_segment
= false;
3179 if ((hdr
->flags
& SEC_READONLY
) == 0)
3185 /* We need a new program segment. We must create a new program
3186 header holding all the sections from phdr_index until hdr. */
3188 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3195 if ((hdr
->flags
& SEC_READONLY
) == 0)
3202 phdr_in_segment
= false;
3205 /* Create a final PT_LOAD program segment. */
3206 if (last_hdr
!= NULL
)
3208 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3216 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3219 amt
= sizeof (struct elf_segment_map
);
3220 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3224 m
->p_type
= PT_DYNAMIC
;
3226 m
->sections
[0] = dynsec
;
3232 /* For each loadable .note section, add a PT_NOTE segment. We don't
3233 use bfd_get_section_by_name, because if we link together
3234 nonloadable .note sections and loadable .note sections, we will
3235 generate two .note sections in the output file. FIXME: Using
3236 names for section types is bogus anyhow. */
3237 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3239 if ((s
->flags
& SEC_LOAD
) != 0
3240 && strncmp (s
->name
, ".note", 5) == 0)
3242 amt
= sizeof (struct elf_segment_map
);
3243 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3247 m
->p_type
= PT_NOTE
;
3254 if (s
->flags
& SEC_THREAD_LOCAL
)
3262 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3267 amt
= sizeof (struct elf_segment_map
);
3268 amt
+= (tls_count
- 1) * sizeof (asection
*);
3269 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3274 m
->count
= tls_count
;
3275 /* Mandated PF_R. */
3277 m
->p_flags_valid
= 1;
3278 for (i
= 0; i
< tls_count
; ++i
)
3280 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3281 m
->sections
[i
] = first_tls
;
3282 first_tls
= first_tls
->next
;
3289 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3291 eh_frame_hdr
= NULL
;
3292 if (elf_tdata (abfd
)->eh_frame_hdr
)
3293 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3294 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3296 amt
= sizeof (struct elf_segment_map
);
3297 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3301 m
->p_type
= PT_GNU_EH_FRAME
;
3303 m
->sections
[0] = eh_frame_hdr
;
3312 elf_tdata (abfd
)->segment_map
= mfirst
;
3316 if (sections
!= NULL
)
3321 /* Sort sections by address. */
3324 elf_sort_sections (arg1
, arg2
)
3328 const asection
*sec1
= *(const asection
**) arg1
;
3329 const asection
*sec2
= *(const asection
**) arg2
;
3331 /* Sort by LMA first, since this is the address used to
3332 place the section into a segment. */
3333 if (sec1
->lma
< sec2
->lma
)
3335 else if (sec1
->lma
> sec2
->lma
)
3338 /* Then sort by VMA. Normally the LMA and the VMA will be
3339 the same, and this will do nothing. */
3340 if (sec1
->vma
< sec2
->vma
)
3342 else if (sec1
->vma
> sec2
->vma
)
3345 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3347 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3353 /* If the indicies are the same, do not return 0
3354 here, but continue to try the next comparison. */
3355 if (sec1
->target_index
- sec2
->target_index
!= 0)
3356 return sec1
->target_index
- sec2
->target_index
;
3361 else if (TOEND (sec2
))
3366 /* Sort by size, to put zero sized sections
3367 before others at the same address. */
3369 if (sec1
->_raw_size
< sec2
->_raw_size
)
3371 if (sec1
->_raw_size
> sec2
->_raw_size
)
3374 return sec1
->target_index
- sec2
->target_index
;
3377 /* Assign file positions to the sections based on the mapping from
3378 sections to segments. This function also sets up some fields in
3379 the file header, and writes out the program headers. */
3382 assign_file_positions_for_segments (abfd
)
3385 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3387 struct elf_segment_map
*m
;
3389 Elf_Internal_Phdr
*phdrs
;
3391 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3392 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3393 Elf_Internal_Phdr
*p
;
3396 if (elf_tdata (abfd
)->segment_map
== NULL
)
3398 if (! map_sections_to_segments (abfd
))
3402 if (bed
->elf_backend_modify_segment_map
)
3404 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3409 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3412 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3413 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3414 elf_elfheader (abfd
)->e_phnum
= count
;
3419 /* If we already counted the number of program segments, make sure
3420 that we allocated enough space. This happens when SIZEOF_HEADERS
3421 is used in a linker script. */
3422 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3423 if (alloc
!= 0 && count
> alloc
)
3425 ((*_bfd_error_handler
)
3426 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3427 bfd_get_filename (abfd
), alloc
, count
));
3428 bfd_set_error (bfd_error_bad_value
);
3435 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3436 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3440 off
= bed
->s
->sizeof_ehdr
;
3441 off
+= alloc
* bed
->s
->sizeof_phdr
;
3448 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3455 /* If elf_segment_map is not from map_sections_to_segments, the
3456 sections may not be correctly ordered. NOTE: sorting should
3457 not be done to the PT_NOTE section of a corefile, which may
3458 contain several pseudo-sections artificially created by bfd.
3459 Sorting these pseudo-sections breaks things badly. */
3461 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3462 && m
->p_type
== PT_NOTE
))
3463 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3466 p
->p_type
= m
->p_type
;
3467 p
->p_flags
= m
->p_flags
;
3469 if (p
->p_type
== PT_LOAD
3471 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3473 if ((abfd
->flags
& D_PAGED
) != 0)
3474 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3477 bfd_size_type align
;
3480 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3482 bfd_size_type secalign
;
3484 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3485 if (secalign
> align
)
3489 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3496 p
->p_vaddr
= m
->sections
[0]->vma
;
3498 if (m
->p_paddr_valid
)
3499 p
->p_paddr
= m
->p_paddr
;
3500 else if (m
->count
== 0)
3503 p
->p_paddr
= m
->sections
[0]->lma
;
3505 if (p
->p_type
== PT_LOAD
3506 && (abfd
->flags
& D_PAGED
) != 0)
3507 p
->p_align
= bed
->maxpagesize
;
3508 else if (m
->count
== 0)
3509 p
->p_align
= bed
->s
->file_align
;
3517 if (m
->includes_filehdr
)
3519 if (! m
->p_flags_valid
)
3522 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3523 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3526 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3528 if (p
->p_vaddr
< (bfd_vma
) off
)
3530 (*_bfd_error_handler
)
3531 (_("%s: Not enough room for program headers, try linking with -N"),
3532 bfd_get_filename (abfd
));
3533 bfd_set_error (bfd_error_bad_value
);
3538 if (! m
->p_paddr_valid
)
3541 if (p
->p_type
== PT_LOAD
)
3543 filehdr_vaddr
= p
->p_vaddr
;
3544 filehdr_paddr
= p
->p_paddr
;
3548 if (m
->includes_phdrs
)
3550 if (! m
->p_flags_valid
)
3553 if (m
->includes_filehdr
)
3555 if (p
->p_type
== PT_LOAD
)
3557 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3558 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3563 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3567 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3568 p
->p_vaddr
-= off
- p
->p_offset
;
3569 if (! m
->p_paddr_valid
)
3570 p
->p_paddr
-= off
- p
->p_offset
;
3573 if (p
->p_type
== PT_LOAD
)
3575 phdrs_vaddr
= p
->p_vaddr
;
3576 phdrs_paddr
= p
->p_paddr
;
3579 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3582 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3583 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3586 if (p
->p_type
== PT_LOAD
3587 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3589 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3595 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3596 p
->p_filesz
+= adjust
;
3597 p
->p_memsz
+= adjust
;
3603 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3607 bfd_size_type align
;
3611 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3613 /* The section may have artificial alignment forced by a
3614 link script. Notice this case by the gap between the
3615 cumulative phdr lma and the section's lma. */
3616 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3618 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3620 p
->p_memsz
+= adjust
;
3623 if ((flags
& SEC_LOAD
) != 0)
3624 p
->p_filesz
+= adjust
;
3627 if (p
->p_type
== PT_LOAD
)
3629 bfd_signed_vma adjust
;
3631 if ((flags
& SEC_LOAD
) != 0)
3633 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3637 else if ((flags
& SEC_ALLOC
) != 0)
3639 /* The section VMA must equal the file position
3640 modulo the page size. FIXME: I'm not sure if
3641 this adjustment is really necessary. We used to
3642 not have the SEC_LOAD case just above, and then
3643 this was necessary, but now I'm not sure. */
3644 if ((abfd
->flags
& D_PAGED
) != 0)
3645 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3647 adjust
= (sec
->vma
- voff
) % align
;
3656 (* _bfd_error_handler
) (_("\
3657 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3658 bfd_section_name (abfd
, sec
),
3663 p
->p_memsz
+= adjust
;
3666 if ((flags
& SEC_LOAD
) != 0)
3667 p
->p_filesz
+= adjust
;
3672 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3673 used in a linker script we may have a section with
3674 SEC_LOAD clear but which is supposed to have
3676 if ((flags
& SEC_LOAD
) != 0
3677 || (flags
& SEC_HAS_CONTENTS
) != 0)
3678 off
+= sec
->_raw_size
;
3680 if ((flags
& SEC_ALLOC
) != 0)
3681 voff
+= sec
->_raw_size
;
3684 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3686 /* The actual "note" segment has i == 0.
3687 This is the one that actually contains everything. */
3691 p
->p_filesz
= sec
->_raw_size
;
3692 off
+= sec
->_raw_size
;
3697 /* Fake sections -- don't need to be written. */
3700 flags
= sec
->flags
= 0;
3707 p
->p_memsz
+= sec
->_raw_size
;
3709 if ((flags
& SEC_LOAD
) != 0)
3710 p
->p_filesz
+= sec
->_raw_size
;
3712 if (p
->p_type
== PT_TLS
3713 && sec
->_raw_size
== 0
3714 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3716 struct bfd_link_order
*o
;
3717 bfd_vma tbss_size
= 0;
3719 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3720 if (tbss_size
< o
->offset
+ o
->size
)
3721 tbss_size
= o
->offset
+ o
->size
;
3723 p
->p_memsz
+= tbss_size
;
3726 if (align
> p
->p_align
3727 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3731 if (! m
->p_flags_valid
)
3734 if ((flags
& SEC_CODE
) != 0)
3736 if ((flags
& SEC_READONLY
) == 0)
3742 /* Now that we have set the section file positions, we can set up
3743 the file positions for the non PT_LOAD segments. */
3744 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3748 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3750 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3751 p
->p_offset
= m
->sections
[0]->filepos
;
3755 if (m
->includes_filehdr
)
3757 p
->p_vaddr
= filehdr_vaddr
;
3758 if (! m
->p_paddr_valid
)
3759 p
->p_paddr
= filehdr_paddr
;
3761 else if (m
->includes_phdrs
)
3763 p
->p_vaddr
= phdrs_vaddr
;
3764 if (! m
->p_paddr_valid
)
3765 p
->p_paddr
= phdrs_paddr
;
3770 /* If additional nonloadable filepos adjustments are required,
3772 if (bed
->set_nonloadable_filepos
)
3773 (*bed
->set_nonloadable_filepos
) (abfd
, phdrs
);
3775 /* Clear out any program headers we allocated but did not use. */
3776 for (; count
< alloc
; count
++, p
++)
3778 memset (p
, 0, sizeof *p
);
3779 p
->p_type
= PT_NULL
;
3782 elf_tdata (abfd
)->phdr
= phdrs
;
3784 elf_tdata (abfd
)->next_file_pos
= off
;
3786 /* Write out the program headers. */
3787 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3788 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3794 /* Get the size of the program header.
3796 If this is called by the linker before any of the section VMA's are set, it
3797 can't calculate the correct value for a strange memory layout. This only
3798 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3799 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3800 data segment (exclusive of .interp and .dynamic).
3802 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3803 will be two segments. */
3805 static bfd_size_type
3806 get_program_header_size (abfd
)
3811 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3813 /* We can't return a different result each time we're called. */
3814 if (elf_tdata (abfd
)->program_header_size
!= 0)
3815 return elf_tdata (abfd
)->program_header_size
;
3817 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3819 struct elf_segment_map
*m
;
3822 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3824 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3825 return elf_tdata (abfd
)->program_header_size
;
3828 /* Assume we will need exactly two PT_LOAD segments: one for text
3829 and one for data. */
3832 s
= bfd_get_section_by_name (abfd
, ".interp");
3833 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3835 /* If we have a loadable interpreter section, we need a
3836 PT_INTERP segment. In this case, assume we also need a
3837 PT_PHDR segment, although that may not be true for all
3842 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3844 /* We need a PT_DYNAMIC segment. */
3848 if (elf_tdata (abfd
)->eh_frame_hdr
3849 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
3851 /* We need a PT_GNU_EH_FRAME segment. */
3855 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3857 if ((s
->flags
& SEC_LOAD
) != 0
3858 && strncmp (s
->name
, ".note", 5) == 0)
3860 /* We need a PT_NOTE segment. */
3865 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3867 if (s
->flags
& SEC_THREAD_LOCAL
)
3869 /* We need a PT_TLS segment. */
3875 /* Let the backend count up any program headers it might need. */
3876 if (bed
->elf_backend_additional_program_headers
)
3880 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3886 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3887 return elf_tdata (abfd
)->program_header_size
;
3890 /* Work out the file positions of all the sections. This is called by
3891 _bfd_elf_compute_section_file_positions. All the section sizes and
3892 VMAs must be known before this is called.
3894 We do not consider reloc sections at this point, unless they form
3895 part of the loadable image. Reloc sections are assigned file
3896 positions in assign_file_positions_for_relocs, which is called by
3897 write_object_contents and final_link.
3899 We also don't set the positions of the .symtab and .strtab here. */
3902 assign_file_positions_except_relocs (abfd
)
3905 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3906 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3907 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3908 unsigned int num_sec
= elf_numsections (abfd
);
3910 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3912 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3913 && bfd_get_format (abfd
) != bfd_core
)
3915 Elf_Internal_Shdr
**hdrpp
;
3918 /* Start after the ELF header. */
3919 off
= i_ehdrp
->e_ehsize
;
3921 /* We are not creating an executable, which means that we are
3922 not creating a program header, and that the actual order of
3923 the sections in the file is unimportant. */
3924 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3926 Elf_Internal_Shdr
*hdr
;
3929 if (hdr
->sh_type
== SHT_REL
3930 || hdr
->sh_type
== SHT_RELA
3931 || i
== tdata
->symtab_section
3932 || i
== tdata
->symtab_shndx_section
3933 || i
== tdata
->strtab_section
)
3935 hdr
->sh_offset
= -1;
3938 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3940 if (i
== SHN_LORESERVE
- 1)
3942 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3943 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3950 Elf_Internal_Shdr
**hdrpp
;
3952 /* Assign file positions for the loaded sections based on the
3953 assignment of sections to segments. */
3954 if (! assign_file_positions_for_segments (abfd
))
3957 /* Assign file positions for the other sections. */
3959 off
= elf_tdata (abfd
)->next_file_pos
;
3960 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3962 Elf_Internal_Shdr
*hdr
;
3965 if (hdr
->bfd_section
!= NULL
3966 && hdr
->bfd_section
->filepos
!= 0)
3967 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3968 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3970 ((*_bfd_error_handler
)
3971 (_("%s: warning: allocated section `%s' not in segment"),
3972 bfd_get_filename (abfd
),
3973 (hdr
->bfd_section
== NULL
3975 : hdr
->bfd_section
->name
)));
3976 if ((abfd
->flags
& D_PAGED
) != 0)
3977 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3979 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3980 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3983 else if (hdr
->sh_type
== SHT_REL
3984 || hdr
->sh_type
== SHT_RELA
3985 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3986 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
3987 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3988 hdr
->sh_offset
= -1;
3990 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3992 if (i
== SHN_LORESERVE
- 1)
3994 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3995 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4000 /* Place the section headers. */
4001 off
= align_file_position (off
, bed
->s
->file_align
);
4002 i_ehdrp
->e_shoff
= off
;
4003 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4005 elf_tdata (abfd
)->next_file_pos
= off
;
4014 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4015 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4016 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4017 struct elf_strtab_hash
*shstrtab
;
4018 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4020 i_ehdrp
= elf_elfheader (abfd
);
4021 i_shdrp
= elf_elfsections (abfd
);
4023 shstrtab
= _bfd_elf_strtab_init ();
4024 if (shstrtab
== NULL
)
4027 elf_shstrtab (abfd
) = shstrtab
;
4029 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4030 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4031 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4032 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4034 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4035 i_ehdrp
->e_ident
[EI_DATA
] =
4036 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4037 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4039 if ((abfd
->flags
& DYNAMIC
) != 0)
4040 i_ehdrp
->e_type
= ET_DYN
;
4041 else if ((abfd
->flags
& EXEC_P
) != 0)
4042 i_ehdrp
->e_type
= ET_EXEC
;
4043 else if (bfd_get_format (abfd
) == bfd_core
)
4044 i_ehdrp
->e_type
= ET_CORE
;
4046 i_ehdrp
->e_type
= ET_REL
;
4048 switch (bfd_get_arch (abfd
))
4050 case bfd_arch_unknown
:
4051 i_ehdrp
->e_machine
= EM_NONE
;
4054 /* There used to be a long list of cases here, each one setting
4055 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4056 in the corresponding bfd definition. To avoid duplication,
4057 the switch was removed. Machines that need special handling
4058 can generally do it in elf_backend_final_write_processing(),
4059 unless they need the information earlier than the final write.
4060 Such need can generally be supplied by replacing the tests for
4061 e_machine with the conditions used to determine it. */
4063 if (get_elf_backend_data (abfd
) != NULL
)
4064 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
4066 i_ehdrp
->e_machine
= EM_NONE
;
4069 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4070 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4072 /* No program header, for now. */
4073 i_ehdrp
->e_phoff
= 0;
4074 i_ehdrp
->e_phentsize
= 0;
4075 i_ehdrp
->e_phnum
= 0;
4077 /* Each bfd section is section header entry. */
4078 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4079 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4081 /* If we're building an executable, we'll need a program header table. */
4082 if (abfd
->flags
& EXEC_P
)
4084 /* It all happens later. */
4086 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4088 /* elf_build_phdrs() returns a (NULL-terminated) array of
4089 Elf_Internal_Phdrs. */
4090 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4091 i_ehdrp
->e_phoff
= outbase
;
4092 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4097 i_ehdrp
->e_phentsize
= 0;
4099 i_ehdrp
->e_phoff
= 0;
4102 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4103 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
4104 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4105 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
4106 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4107 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
4108 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4109 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4110 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4116 /* Assign file positions for all the reloc sections which are not part
4117 of the loadable file image. */
4120 _bfd_elf_assign_file_positions_for_relocs (abfd
)
4124 unsigned int i
, num_sec
;
4125 Elf_Internal_Shdr
**shdrpp
;
4127 off
= elf_tdata (abfd
)->next_file_pos
;
4129 num_sec
= elf_numsections (abfd
);
4130 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4132 Elf_Internal_Shdr
*shdrp
;
4135 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4136 && shdrp
->sh_offset
== -1)
4137 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
4140 elf_tdata (abfd
)->next_file_pos
= off
;
4144 _bfd_elf_write_object_contents (abfd
)
4147 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4148 Elf_Internal_Ehdr
*i_ehdrp
;
4149 Elf_Internal_Shdr
**i_shdrp
;
4151 unsigned int count
, num_sec
;
4153 if (! abfd
->output_has_begun
4154 && ! _bfd_elf_compute_section_file_positions
4155 (abfd
, (struct bfd_link_info
*) NULL
))
4158 i_shdrp
= elf_elfsections (abfd
);
4159 i_ehdrp
= elf_elfheader (abfd
);
4162 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4166 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4168 /* After writing the headers, we need to write the sections too... */
4169 num_sec
= elf_numsections (abfd
);
4170 for (count
= 1; count
< num_sec
; count
++)
4172 if (bed
->elf_backend_section_processing
)
4173 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4174 if (i_shdrp
[count
]->contents
)
4176 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4178 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4179 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4182 if (count
== SHN_LORESERVE
- 1)
4183 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4186 /* Write out the section header names. */
4187 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4188 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4191 if (bed
->elf_backend_final_write_processing
)
4192 (*bed
->elf_backend_final_write_processing
) (abfd
,
4193 elf_tdata (abfd
)->linker
);
4195 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4199 _bfd_elf_write_corefile_contents (abfd
)
4202 /* Hopefully this can be done just like an object file. */
4203 return _bfd_elf_write_object_contents (abfd
);
4206 /* Given a section, search the header to find them. */
4209 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4213 struct elf_backend_data
*bed
;
4216 if (elf_section_data (asect
) != NULL
4217 && elf_section_data (asect
)->this_idx
!= 0)
4218 return elf_section_data (asect
)->this_idx
;
4220 if (bfd_is_abs_section (asect
))
4222 else if (bfd_is_com_section (asect
))
4224 else if (bfd_is_und_section (asect
))
4228 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4229 int maxindex
= elf_numsections (abfd
);
4231 for (index
= 1; index
< maxindex
; index
++)
4233 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4235 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4241 bed
= get_elf_backend_data (abfd
);
4242 if (bed
->elf_backend_section_from_bfd_section
)
4246 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4251 bfd_set_error (bfd_error_nonrepresentable_section
);
4256 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4260 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4262 asymbol
**asym_ptr_ptr
;
4264 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4266 flagword flags
= asym_ptr
->flags
;
4268 /* When gas creates relocations against local labels, it creates its
4269 own symbol for the section, but does put the symbol into the
4270 symbol chain, so udata is 0. When the linker is generating
4271 relocatable output, this section symbol may be for one of the
4272 input sections rather than the output section. */
4273 if (asym_ptr
->udata
.i
== 0
4274 && (flags
& BSF_SECTION_SYM
)
4275 && asym_ptr
->section
)
4279 if (asym_ptr
->section
->output_section
!= NULL
)
4280 indx
= asym_ptr
->section
->output_section
->index
;
4282 indx
= asym_ptr
->section
->index
;
4283 if (indx
< elf_num_section_syms (abfd
)
4284 && elf_section_syms (abfd
)[indx
] != NULL
)
4285 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4288 idx
= asym_ptr
->udata
.i
;
4292 /* This case can occur when using --strip-symbol on a symbol
4293 which is used in a relocation entry. */
4294 (*_bfd_error_handler
)
4295 (_("%s: symbol `%s' required but not present"),
4296 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4297 bfd_set_error (bfd_error_no_symbols
);
4304 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4305 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4306 elf_symbol_flags (flags
));
4314 /* Copy private BFD data. This copies any program header information. */
4317 copy_private_bfd_data (ibfd
, obfd
)
4321 Elf_Internal_Ehdr
* iehdr
;
4322 struct elf_segment_map
* map
;
4323 struct elf_segment_map
* map_first
;
4324 struct elf_segment_map
** pointer_to_map
;
4325 Elf_Internal_Phdr
* segment
;
4328 unsigned int num_segments
;
4329 boolean phdr_included
= false;
4330 bfd_vma maxpagesize
;
4331 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4332 unsigned int phdr_adjust_num
= 0;
4333 struct elf_backend_data
* bed
;
4335 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4336 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4339 if (elf_tdata (ibfd
)->phdr
== NULL
)
4342 bed
= get_elf_backend_data (ibfd
);
4343 iehdr
= elf_elfheader (ibfd
);
4346 pointer_to_map
= &map_first
;
4348 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4349 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4351 /* Returns the end address of the segment + 1. */
4352 #define SEGMENT_END(segment, start) \
4353 (start + (segment->p_memsz > segment->p_filesz \
4354 ? segment->p_memsz : segment->p_filesz))
4356 /* Returns true if the given section is contained within
4357 the given segment. VMA addresses are compared. */
4358 #define IS_CONTAINED_BY_VMA(section, segment) \
4359 (section->vma >= segment->p_vaddr \
4360 && (section->vma + section->_raw_size) \
4361 <= (SEGMENT_END (segment, segment->p_vaddr)))
4363 /* Returns true if the given section is contained within
4364 the given segment. LMA addresses are compared. */
4365 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4366 (section->lma >= base \
4367 && (section->lma + section->_raw_size) \
4368 <= SEGMENT_END (segment, base))
4370 /* Returns true if the given section is contained within the
4371 given segment. Filepos addresses are compared in an elf
4372 backend function. */
4373 #define IS_CONTAINED_BY_FILEPOS(sec, seg, bed) \
4374 (bed->is_contained_by_filepos \
4375 && (*bed->is_contained_by_filepos) (sec, seg))
4377 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4378 #define IS_COREFILE_NOTE(p, s) \
4379 (p->p_type == PT_NOTE \
4380 && bfd_get_format (ibfd) == bfd_core \
4381 && s->vma == 0 && s->lma == 0 \
4382 && (bfd_vma) s->filepos >= p->p_offset \
4383 && (bfd_vma) s->filepos + s->_raw_size \
4384 <= p->p_offset + p->p_filesz)
4386 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4387 linker, which generates a PT_INTERP section with p_vaddr and
4388 p_memsz set to 0. */
4389 #define IS_SOLARIS_PT_INTERP(p, s) \
4391 && p->p_filesz > 0 \
4392 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4393 && s->_raw_size > 0 \
4394 && (bfd_vma) s->filepos >= p->p_offset \
4395 && ((bfd_vma) s->filepos + s->_raw_size \
4396 <= p->p_offset + p->p_filesz))
4398 /* Decide if the given section should be included in the given segment.
4399 A section will be included if:
4400 1. It is within the address space of the segment -- we use the LMA
4401 if that is set for the segment and the VMA otherwise,
4402 2. It is an allocated segment,
4403 3. There is an output section associated with it,
4404 4. The section has not already been allocated to a previous segment. */
4405 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4406 (((((segment->p_paddr \
4407 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4408 : IS_CONTAINED_BY_VMA (section, segment)) \
4409 || IS_SOLARIS_PT_INTERP (segment, section)) \
4410 && (section->flags & SEC_ALLOC) != 0) \
4411 || IS_COREFILE_NOTE (segment, section) \
4412 || (IS_CONTAINED_BY_FILEPOS (section, segment, bed) \
4413 && (section->flags & SEC_ALLOC) == 0)) \
4414 && section->output_section != NULL \
4415 && section->segment_mark == false)
4417 /* Returns true iff seg1 starts after the end of seg2. */
4418 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4419 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4421 /* Returns true iff seg1 and seg2 overlap. */
4422 #define SEGMENT_OVERLAPS(seg1, seg2) \
4423 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4425 /* Initialise the segment mark field. */
4426 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4427 section
->segment_mark
= false;
4429 /* Scan through the segments specified in the program header
4430 of the input BFD. For this first scan we look for overlaps
4431 in the loadable segments. These can be created by weird
4432 parameters to objcopy. */
4433 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4438 Elf_Internal_Phdr
*segment2
;
4440 if (segment
->p_type
!= PT_LOAD
)
4443 /* Determine if this segment overlaps any previous segments. */
4444 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4446 bfd_signed_vma extra_length
;
4448 if (segment2
->p_type
!= PT_LOAD
4449 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4452 /* Merge the two segments together. */
4453 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4455 /* Extend SEGMENT2 to include SEGMENT and then delete
4458 SEGMENT_END (segment
, segment
->p_vaddr
)
4459 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4461 if (extra_length
> 0)
4463 segment2
->p_memsz
+= extra_length
;
4464 segment2
->p_filesz
+= extra_length
;
4467 segment
->p_type
= PT_NULL
;
4469 /* Since we have deleted P we must restart the outer loop. */
4471 segment
= elf_tdata (ibfd
)->phdr
;
4476 /* Extend SEGMENT to include SEGMENT2 and then delete
4479 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4480 - SEGMENT_END (segment
, segment
->p_vaddr
);
4482 if (extra_length
> 0)
4484 segment
->p_memsz
+= extra_length
;
4485 segment
->p_filesz
+= extra_length
;
4488 segment2
->p_type
= PT_NULL
;
4493 /* The second scan attempts to assign sections to segments. */
4494 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4498 unsigned int section_count
;
4499 asection
** sections
;
4500 asection
* output_section
;
4502 bfd_vma matching_lma
;
4503 bfd_vma suggested_lma
;
4507 if (segment
->p_type
== PT_NULL
)
4510 /* Compute how many sections might be placed into this segment. */
4512 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4513 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4516 /* Allocate a segment map big enough to contain all of the
4517 sections we have selected. */
4518 amt
= sizeof (struct elf_segment_map
);
4519 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4520 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4524 /* Initialise the fields of the segment map. Default to
4525 using the physical address of the segment in the input BFD. */
4527 map
->p_type
= segment
->p_type
;
4528 map
->p_flags
= segment
->p_flags
;
4529 map
->p_flags_valid
= 1;
4530 map
->p_paddr
= segment
->p_paddr
;
4531 map
->p_paddr_valid
= 1;
4533 /* Determine if this segment contains the ELF file header
4534 and if it contains the program headers themselves. */
4535 map
->includes_filehdr
= (segment
->p_offset
== 0
4536 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4538 map
->includes_phdrs
= 0;
4540 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4542 map
->includes_phdrs
=
4543 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4544 && (segment
->p_offset
+ segment
->p_filesz
4545 >= ((bfd_vma
) iehdr
->e_phoff
4546 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4548 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4549 phdr_included
= true;
4552 if (section_count
== 0)
4554 /* Special segments, such as the PT_PHDR segment, may contain
4555 no sections, but ordinary, loadable segments should contain
4557 if (segment
->p_type
== PT_LOAD
)
4558 (*_bfd_error_handler
)
4559 (_("%s: warning: Empty loadable segment detected\n"),
4560 bfd_archive_filename (ibfd
));
4563 *pointer_to_map
= map
;
4564 pointer_to_map
= &map
->next
;
4569 /* Now scan the sections in the input BFD again and attempt
4570 to add their corresponding output sections to the segment map.
4571 The problem here is how to handle an output section which has
4572 been moved (ie had its LMA changed). There are four possibilities:
4574 1. None of the sections have been moved.
4575 In this case we can continue to use the segment LMA from the
4578 2. All of the sections have been moved by the same amount.
4579 In this case we can change the segment's LMA to match the LMA
4580 of the first section.
4582 3. Some of the sections have been moved, others have not.
4583 In this case those sections which have not been moved can be
4584 placed in the current segment which will have to have its size,
4585 and possibly its LMA changed, and a new segment or segments will
4586 have to be created to contain the other sections.
4588 4. The sections have been moved, but not be the same amount.
4589 In this case we can change the segment's LMA to match the LMA
4590 of the first section and we will have to create a new segment
4591 or segments to contain the other sections.
4593 In order to save time, we allocate an array to hold the section
4594 pointers that we are interested in. As these sections get assigned
4595 to a segment, they are removed from this array. */
4597 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4598 sections
= (asection
**) bfd_malloc (amt
);
4599 if (sections
== NULL
)
4602 /* Step One: Scan for segment vs section LMA conflicts.
4603 Also add the sections to the section array allocated above.
4604 Also add the sections to the current segment. In the common
4605 case, where the sections have not been moved, this means that
4606 we have completely filled the segment, and there is nothing
4612 for (j
= 0, section
= ibfd
->sections
;
4614 section
= section
->next
)
4616 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4618 output_section
= section
->output_section
;
4620 sections
[j
++] = section
;
4622 /* The Solaris native linker always sets p_paddr to 0.
4623 We try to catch that case here, and set it to the
4625 if (segment
->p_paddr
== 0
4626 && segment
->p_vaddr
!= 0
4628 && output_section
->lma
!= 0
4629 && (output_section
->vma
== (segment
->p_vaddr
4630 + (map
->includes_filehdr
4633 + (map
->includes_phdrs
4635 * iehdr
->e_phentsize
)
4637 map
->p_paddr
= segment
->p_vaddr
;
4639 /* Match up the physical address of the segment with the
4640 LMA address of the output section. */
4641 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4642 || IS_CONTAINED_BY_FILEPOS (section
, segment
, bed
)
4643 || IS_COREFILE_NOTE (segment
, section
))
4645 if (matching_lma
== 0)
4646 matching_lma
= output_section
->lma
;
4648 /* We assume that if the section fits within the segment
4649 then it does not overlap any other section within that
4651 map
->sections
[isec
++] = output_section
;
4653 else if (suggested_lma
== 0)
4654 suggested_lma
= output_section
->lma
;
4658 BFD_ASSERT (j
== section_count
);
4660 /* Step Two: Adjust the physical address of the current segment,
4662 if (isec
== section_count
)
4664 /* All of the sections fitted within the segment as currently
4665 specified. This is the default case. Add the segment to
4666 the list of built segments and carry on to process the next
4667 program header in the input BFD. */
4668 map
->count
= section_count
;
4669 *pointer_to_map
= map
;
4670 pointer_to_map
= &map
->next
;
4677 if (matching_lma
!= 0)
4679 /* At least one section fits inside the current segment.
4680 Keep it, but modify its physical address to match the
4681 LMA of the first section that fitted. */
4682 map
->p_paddr
= matching_lma
;
4686 /* None of the sections fitted inside the current segment.
4687 Change the current segment's physical address to match
4688 the LMA of the first section. */
4689 map
->p_paddr
= suggested_lma
;
4692 /* Offset the segment physical address from the lma
4693 to allow for space taken up by elf headers. */
4694 if (map
->includes_filehdr
)
4695 map
->p_paddr
-= iehdr
->e_ehsize
;
4697 if (map
->includes_phdrs
)
4699 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4701 /* iehdr->e_phnum is just an estimate of the number
4702 of program headers that we will need. Make a note
4703 here of the number we used and the segment we chose
4704 to hold these headers, so that we can adjust the
4705 offset when we know the correct value. */
4706 phdr_adjust_num
= iehdr
->e_phnum
;
4707 phdr_adjust_seg
= map
;
4711 /* Step Three: Loop over the sections again, this time assigning
4712 those that fit to the current segment and removing them from the
4713 sections array; but making sure not to leave large gaps. Once all
4714 possible sections have been assigned to the current segment it is
4715 added to the list of built segments and if sections still remain
4716 to be assigned, a new segment is constructed before repeating
4724 /* Fill the current segment with sections that fit. */
4725 for (j
= 0; j
< section_count
; j
++)
4727 section
= sections
[j
];
4729 if (section
== NULL
)
4732 output_section
= section
->output_section
;
4734 BFD_ASSERT (output_section
!= NULL
);
4736 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4737 || IS_COREFILE_NOTE (segment
, section
))
4739 if (map
->count
== 0)
4741 /* If the first section in a segment does not start at
4742 the beginning of the segment, then something is
4744 if (output_section
->lma
!=
4746 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4747 + (map
->includes_phdrs
4748 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4754 asection
* prev_sec
;
4756 prev_sec
= map
->sections
[map
->count
- 1];
4758 /* If the gap between the end of the previous section
4759 and the start of this section is more than
4760 maxpagesize then we need to start a new segment. */
4761 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4763 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4764 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4765 > output_section
->lma
))
4767 if (suggested_lma
== 0)
4768 suggested_lma
= output_section
->lma
;
4774 map
->sections
[map
->count
++] = output_section
;
4777 section
->segment_mark
= true;
4779 else if (suggested_lma
== 0)
4780 suggested_lma
= output_section
->lma
;
4783 BFD_ASSERT (map
->count
> 0);
4785 /* Add the current segment to the list of built segments. */
4786 *pointer_to_map
= map
;
4787 pointer_to_map
= &map
->next
;
4789 if (isec
< section_count
)
4791 /* We still have not allocated all of the sections to
4792 segments. Create a new segment here, initialise it
4793 and carry on looping. */
4794 amt
= sizeof (struct elf_segment_map
);
4795 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4796 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4800 /* Initialise the fields of the segment map. Set the physical
4801 physical address to the LMA of the first section that has
4802 not yet been assigned. */
4804 map
->p_type
= segment
->p_type
;
4805 map
->p_flags
= segment
->p_flags
;
4806 map
->p_flags_valid
= 1;
4807 map
->p_paddr
= suggested_lma
;
4808 map
->p_paddr_valid
= 1;
4809 map
->includes_filehdr
= 0;
4810 map
->includes_phdrs
= 0;
4813 while (isec
< section_count
);
4818 /* The Solaris linker creates program headers in which all the
4819 p_paddr fields are zero. When we try to objcopy or strip such a
4820 file, we get confused. Check for this case, and if we find it
4821 reset the p_paddr_valid fields. */
4822 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4823 if (map
->p_paddr
!= 0)
4827 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4828 map
->p_paddr_valid
= 0;
4831 elf_tdata (obfd
)->segment_map
= map_first
;
4833 /* If we had to estimate the number of program headers that were
4834 going to be needed, then check our estimate now and adjust
4835 the offset if necessary. */
4836 if (phdr_adjust_seg
!= NULL
)
4840 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4843 if (count
> phdr_adjust_num
)
4844 phdr_adjust_seg
->p_paddr
4845 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4849 /* Final Step: Sort the segments into ascending order of physical
4851 if (map_first
!= NULL
)
4853 struct elf_segment_map
*prev
;
4856 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4858 /* Yes I know - its a bubble sort.... */
4859 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4861 /* Swap map and map->next. */
4862 prev
->next
= map
->next
;
4863 map
->next
= map
->next
->next
;
4864 prev
->next
->next
= map
;
4874 #undef IS_CONTAINED_BY_VMA
4875 #undef IS_CONTAINED_BY_LMA
4876 #undef IS_CONTAINED_BY_FILEPOS
4877 #undef IS_COREFILE_NOTE
4878 #undef IS_SOLARIS_PT_INTERP
4879 #undef INCLUDE_SECTION_IN_SEGMENT
4880 #undef SEGMENT_AFTER_SEGMENT
4881 #undef SEGMENT_OVERLAPS
4885 /* Copy private section information. This copies over the entsize
4886 field, and sometimes the info field. */
4889 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4895 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4896 const struct elf_backend_data
*bed
= get_elf_backend_data (ibfd
);
4898 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4899 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4902 /* Copy over private BFD data if it has not already been copied.
4903 This must be done here, rather than in the copy_private_bfd_data
4904 entry point, because the latter is called after the section
4905 contents have been set, which means that the program headers have
4906 already been worked out. The backend function provides a way to
4907 override the test conditions and code path for the call to
4908 copy_private_bfd_data. */
4909 if (bed
->copy_private_bfd_data_p
)
4911 if ((*bed
->copy_private_bfd_data_p
) (ibfd
, isec
, obfd
, osec
))
4912 if (! copy_private_bfd_data (ibfd
, obfd
))
4915 else if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
4919 /* Only set up the segments if there are no more SEC_ALLOC
4920 sections. FIXME: This won't do the right thing if objcopy is
4921 used to remove the last SEC_ALLOC section, since objcopy
4922 won't call this routine in that case. */
4923 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4924 if ((s
->flags
& SEC_ALLOC
) != 0)
4928 if (! copy_private_bfd_data (ibfd
, obfd
))
4933 ihdr
= &elf_section_data (isec
)->this_hdr
;
4934 ohdr
= &elf_section_data (osec
)->this_hdr
;
4936 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4938 if (ihdr
->sh_type
== SHT_SYMTAB
4939 || ihdr
->sh_type
== SHT_DYNSYM
4940 || ihdr
->sh_type
== SHT_GNU_verneed
4941 || ihdr
->sh_type
== SHT_GNU_verdef
)
4942 ohdr
->sh_info
= ihdr
->sh_info
;
4944 elf_section_data (osec
)->use_rela_p
4945 = elf_section_data (isec
)->use_rela_p
;
4950 /* Copy private symbol information. If this symbol is in a section
4951 which we did not map into a BFD section, try to map the section
4952 index correctly. We use special macro definitions for the mapped
4953 section indices; these definitions are interpreted by the
4954 swap_out_syms function. */
4956 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4957 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4958 #define MAP_STRTAB (SHN_HIOS + 3)
4959 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4960 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4963 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4969 elf_symbol_type
*isym
, *osym
;
4971 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4972 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4975 isym
= elf_symbol_from (ibfd
, isymarg
);
4976 osym
= elf_symbol_from (obfd
, osymarg
);
4980 && bfd_is_abs_section (isym
->symbol
.section
))
4984 shndx
= isym
->internal_elf_sym
.st_shndx
;
4985 if (shndx
== elf_onesymtab (ibfd
))
4986 shndx
= MAP_ONESYMTAB
;
4987 else if (shndx
== elf_dynsymtab (ibfd
))
4988 shndx
= MAP_DYNSYMTAB
;
4989 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4991 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4992 shndx
= MAP_SHSTRTAB
;
4993 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
4994 shndx
= MAP_SYM_SHNDX
;
4995 osym
->internal_elf_sym
.st_shndx
= shndx
;
5001 /* Swap out the symbols. */
5004 swap_out_syms (abfd
, sttp
, relocatable_p
)
5006 struct bfd_strtab_hash
**sttp
;
5009 struct elf_backend_data
*bed
;
5012 struct bfd_strtab_hash
*stt
;
5013 Elf_Internal_Shdr
*symtab_hdr
;
5014 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5015 Elf_Internal_Shdr
*symstrtab_hdr
;
5016 char *outbound_syms
;
5017 char *outbound_shndx
;
5021 if (!elf_map_symbols (abfd
))
5024 /* Dump out the symtabs. */
5025 stt
= _bfd_elf_stringtab_init ();
5029 bed
= get_elf_backend_data (abfd
);
5030 symcount
= bfd_get_symcount (abfd
);
5031 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5032 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5033 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5034 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5035 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5036 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5038 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5039 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5041 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5042 outbound_syms
= bfd_alloc (abfd
, amt
);
5043 if (outbound_syms
== NULL
)
5045 symtab_hdr
->contents
= (PTR
) outbound_syms
;
5047 outbound_shndx
= NULL
;
5048 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5049 if (symtab_shndx_hdr
->sh_name
!= 0)
5051 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5052 outbound_shndx
= bfd_alloc (abfd
, amt
);
5053 if (outbound_shndx
== NULL
)
5055 memset (outbound_shndx
, 0, (unsigned long) amt
);
5056 symtab_shndx_hdr
->contents
= outbound_shndx
;
5057 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5058 symtab_shndx_hdr
->sh_size
= amt
;
5059 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5060 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5063 /* now generate the data (for "contents") */
5065 /* Fill in zeroth symbol and swap it out. */
5066 Elf_Internal_Sym sym
;
5072 sym
.st_shndx
= SHN_UNDEF
;
5073 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5074 outbound_syms
+= bed
->s
->sizeof_sym
;
5075 if (outbound_shndx
!= NULL
)
5076 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5079 syms
= bfd_get_outsymbols (abfd
);
5080 for (idx
= 0; idx
< symcount
; idx
++)
5082 Elf_Internal_Sym sym
;
5083 bfd_vma value
= syms
[idx
]->value
;
5084 elf_symbol_type
*type_ptr
;
5085 flagword flags
= syms
[idx
]->flags
;
5088 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5090 /* Local section symbols have no name. */
5095 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5098 if (sym
.st_name
== (unsigned long) -1)
5102 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5104 if ((flags
& BSF_SECTION_SYM
) == 0
5105 && bfd_is_com_section (syms
[idx
]->section
))
5107 /* ELF common symbols put the alignment into the `value' field,
5108 and the size into the `size' field. This is backwards from
5109 how BFD handles it, so reverse it here. */
5110 sym
.st_size
= value
;
5111 if (type_ptr
== NULL
5112 || type_ptr
->internal_elf_sym
.st_value
== 0)
5113 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5115 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5116 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5117 (abfd
, syms
[idx
]->section
);
5121 asection
*sec
= syms
[idx
]->section
;
5124 if (sec
->output_section
)
5126 value
+= sec
->output_offset
;
5127 sec
= sec
->output_section
;
5129 /* Don't add in the section vma for relocatable output. */
5130 if (! relocatable_p
)
5132 sym
.st_value
= value
;
5133 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5135 if (bfd_is_abs_section (sec
)
5137 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5139 /* This symbol is in a real ELF section which we did
5140 not create as a BFD section. Undo the mapping done
5141 by copy_private_symbol_data. */
5142 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5146 shndx
= elf_onesymtab (abfd
);
5149 shndx
= elf_dynsymtab (abfd
);
5152 shndx
= elf_tdata (abfd
)->strtab_section
;
5155 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5158 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5166 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5172 /* Writing this would be a hell of a lot easier if
5173 we had some decent documentation on bfd, and
5174 knew what to expect of the library, and what to
5175 demand of applications. For example, it
5176 appears that `objcopy' might not set the
5177 section of a symbol to be a section that is
5178 actually in the output file. */
5179 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5180 BFD_ASSERT (sec2
!= 0);
5181 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5182 BFD_ASSERT (shndx
!= -1);
5186 sym
.st_shndx
= shndx
;
5189 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5191 else if ((flags
& BSF_FUNCTION
) != 0)
5193 else if ((flags
& BSF_OBJECT
) != 0)
5198 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5201 /* Processor-specific types */
5202 if (type_ptr
!= NULL
5203 && bed
->elf_backend_get_symbol_type
)
5204 type
= ((*bed
->elf_backend_get_symbol_type
)
5205 (&type_ptr
->internal_elf_sym
, type
));
5207 if (flags
& BSF_SECTION_SYM
)
5209 if (flags
& BSF_GLOBAL
)
5210 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5212 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5214 else if (bfd_is_com_section (syms
[idx
]->section
))
5215 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5216 else if (bfd_is_und_section (syms
[idx
]->section
))
5217 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5221 else if (flags
& BSF_FILE
)
5222 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5225 int bind
= STB_LOCAL
;
5227 if (flags
& BSF_LOCAL
)
5229 else if (flags
& BSF_WEAK
)
5231 else if (flags
& BSF_GLOBAL
)
5234 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5237 if (type_ptr
!= NULL
)
5238 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5242 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5243 outbound_syms
+= bed
->s
->sizeof_sym
;
5244 if (outbound_shndx
!= NULL
)
5245 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5249 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5250 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5252 symstrtab_hdr
->sh_flags
= 0;
5253 symstrtab_hdr
->sh_addr
= 0;
5254 symstrtab_hdr
->sh_entsize
= 0;
5255 symstrtab_hdr
->sh_link
= 0;
5256 symstrtab_hdr
->sh_info
= 0;
5257 symstrtab_hdr
->sh_addralign
= 1;
5262 /* Return the number of bytes required to hold the symtab vector.
5264 Note that we base it on the count plus 1, since we will null terminate
5265 the vector allocated based on this size. However, the ELF symbol table
5266 always has a dummy entry as symbol #0, so it ends up even. */
5269 _bfd_elf_get_symtab_upper_bound (abfd
)
5274 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5276 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5277 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5279 symtab_size
-= sizeof (asymbol
*);
5285 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5290 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5292 if (elf_dynsymtab (abfd
) == 0)
5294 bfd_set_error (bfd_error_invalid_operation
);
5298 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5299 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5301 symtab_size
-= sizeof (asymbol
*);
5307 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5308 bfd
*abfd ATTRIBUTE_UNUSED
;
5311 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5314 /* Canonicalize the relocs. */
5317 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5325 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5327 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5330 tblptr
= section
->relocation
;
5331 for (i
= 0; i
< section
->reloc_count
; i
++)
5332 *relptr
++ = tblptr
++;
5336 return section
->reloc_count
;
5340 _bfd_elf_get_symtab (abfd
, alocation
)
5342 asymbol
**alocation
;
5344 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5345 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5348 bfd_get_symcount (abfd
) = symcount
;
5353 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5355 asymbol
**alocation
;
5357 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5358 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5361 /* Return the size required for the dynamic reloc entries. Any
5362 section that was actually installed in the BFD, and has type
5363 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5364 considered to be a dynamic reloc section. */
5367 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5373 if (elf_dynsymtab (abfd
) == 0)
5375 bfd_set_error (bfd_error_invalid_operation
);
5379 ret
= sizeof (arelent
*);
5380 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5381 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5382 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5383 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5384 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5385 * sizeof (arelent
*));
5390 /* Canonicalize the dynamic relocation entries. Note that we return
5391 the dynamic relocations as a single block, although they are
5392 actually associated with particular sections; the interface, which
5393 was designed for SunOS style shared libraries, expects that there
5394 is only one set of dynamic relocs. Any section that was actually
5395 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5396 the dynamic symbol table, is considered to be a dynamic reloc
5400 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5405 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5409 if (elf_dynsymtab (abfd
) == 0)
5411 bfd_set_error (bfd_error_invalid_operation
);
5415 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5417 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5419 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5420 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5421 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5426 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5428 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5430 for (i
= 0; i
< count
; i
++)
5441 /* Read in the version information. */
5444 _bfd_elf_slurp_version_tables (abfd
)
5447 bfd_byte
*contents
= NULL
;
5450 if (elf_dynverdef (abfd
) != 0)
5452 Elf_Internal_Shdr
*hdr
;
5453 Elf_External_Verdef
*everdef
;
5454 Elf_Internal_Verdef
*iverdef
;
5455 Elf_Internal_Verdef
*iverdefarr
;
5456 Elf_Internal_Verdef iverdefmem
;
5458 unsigned int maxidx
;
5460 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5462 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5463 if (contents
== NULL
)
5465 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5466 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5469 /* We know the number of entries in the section but not the maximum
5470 index. Therefore we have to run through all entries and find
5472 everdef
= (Elf_External_Verdef
*) contents
;
5474 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5476 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5478 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5479 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5481 everdef
= ((Elf_External_Verdef
*)
5482 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5485 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5486 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5487 if (elf_tdata (abfd
)->verdef
== NULL
)
5490 elf_tdata (abfd
)->cverdefs
= maxidx
;
5492 everdef
= (Elf_External_Verdef
*) contents
;
5493 iverdefarr
= elf_tdata (abfd
)->verdef
;
5494 for (i
= 0; i
< hdr
->sh_info
; i
++)
5496 Elf_External_Verdaux
*everdaux
;
5497 Elf_Internal_Verdaux
*iverdaux
;
5500 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5502 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5503 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5505 iverdef
->vd_bfd
= abfd
;
5507 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5508 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5509 if (iverdef
->vd_auxptr
== NULL
)
5512 everdaux
= ((Elf_External_Verdaux
*)
5513 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5514 iverdaux
= iverdef
->vd_auxptr
;
5515 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5517 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5519 iverdaux
->vda_nodename
=
5520 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5521 iverdaux
->vda_name
);
5522 if (iverdaux
->vda_nodename
== NULL
)
5525 if (j
+ 1 < iverdef
->vd_cnt
)
5526 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5528 iverdaux
->vda_nextptr
= NULL
;
5530 everdaux
= ((Elf_External_Verdaux
*)
5531 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5534 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5536 if (i
+ 1 < hdr
->sh_info
)
5537 iverdef
->vd_nextdef
= iverdef
+ 1;
5539 iverdef
->vd_nextdef
= NULL
;
5541 everdef
= ((Elf_External_Verdef
*)
5542 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5549 if (elf_dynverref (abfd
) != 0)
5551 Elf_Internal_Shdr
*hdr
;
5552 Elf_External_Verneed
*everneed
;
5553 Elf_Internal_Verneed
*iverneed
;
5556 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5558 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5559 elf_tdata (abfd
)->verref
=
5560 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5561 if (elf_tdata (abfd
)->verref
== NULL
)
5564 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5566 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5567 if (contents
== NULL
)
5569 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5570 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5573 everneed
= (Elf_External_Verneed
*) contents
;
5574 iverneed
= elf_tdata (abfd
)->verref
;
5575 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5577 Elf_External_Vernaux
*evernaux
;
5578 Elf_Internal_Vernaux
*ivernaux
;
5581 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5583 iverneed
->vn_bfd
= abfd
;
5585 iverneed
->vn_filename
=
5586 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5588 if (iverneed
->vn_filename
== NULL
)
5591 amt
= iverneed
->vn_cnt
;
5592 amt
*= sizeof (Elf_Internal_Vernaux
);
5593 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5595 evernaux
= ((Elf_External_Vernaux
*)
5596 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5597 ivernaux
= iverneed
->vn_auxptr
;
5598 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5600 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5602 ivernaux
->vna_nodename
=
5603 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5604 ivernaux
->vna_name
);
5605 if (ivernaux
->vna_nodename
== NULL
)
5608 if (j
+ 1 < iverneed
->vn_cnt
)
5609 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5611 ivernaux
->vna_nextptr
= NULL
;
5613 evernaux
= ((Elf_External_Vernaux
*)
5614 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5617 if (i
+ 1 < hdr
->sh_info
)
5618 iverneed
->vn_nextref
= iverneed
+ 1;
5620 iverneed
->vn_nextref
= NULL
;
5622 everneed
= ((Elf_External_Verneed
*)
5623 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5633 if (contents
== NULL
)
5639 _bfd_elf_make_empty_symbol (abfd
)
5642 elf_symbol_type
*newsym
;
5643 bfd_size_type amt
= sizeof (elf_symbol_type
);
5645 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5650 newsym
->symbol
.the_bfd
= abfd
;
5651 return &newsym
->symbol
;
5656 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5657 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5661 bfd_symbol_info (symbol
, ret
);
5664 /* Return whether a symbol name implies a local symbol. Most targets
5665 use this function for the is_local_label_name entry point, but some
5669 _bfd_elf_is_local_label_name (abfd
, name
)
5670 bfd
*abfd ATTRIBUTE_UNUSED
;
5673 /* Normal local symbols start with ``.L''. */
5674 if (name
[0] == '.' && name
[1] == 'L')
5677 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5678 DWARF debugging symbols starting with ``..''. */
5679 if (name
[0] == '.' && name
[1] == '.')
5682 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5683 emitting DWARF debugging output. I suspect this is actually a
5684 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5685 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5686 underscore to be emitted on some ELF targets). For ease of use,
5687 we treat such symbols as local. */
5688 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5695 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5696 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5697 asymbol
*symbol ATTRIBUTE_UNUSED
;
5704 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5706 enum bfd_architecture arch
;
5707 unsigned long machine
;
5709 /* If this isn't the right architecture for this backend, and this
5710 isn't the generic backend, fail. */
5711 if (arch
!= get_elf_backend_data (abfd
)->arch
5712 && arch
!= bfd_arch_unknown
5713 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5716 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5719 /* Find the function to a particular section and offset,
5720 for error reporting. */
5723 elf_find_function (abfd
, section
, symbols
, offset
,
5724 filename_ptr
, functionname_ptr
)
5725 bfd
*abfd ATTRIBUTE_UNUSED
;
5729 const char **filename_ptr
;
5730 const char **functionname_ptr
;
5732 const char *filename
;
5741 for (p
= symbols
; *p
!= NULL
; p
++)
5745 q
= (elf_symbol_type
*) *p
;
5747 if (bfd_get_section (&q
->symbol
) != section
)
5750 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5755 filename
= bfd_asymbol_name (&q
->symbol
);
5759 if (q
->symbol
.section
== section
5760 && q
->symbol
.value
>= low_func
5761 && q
->symbol
.value
<= offset
)
5763 func
= (asymbol
*) q
;
5764 low_func
= q
->symbol
.value
;
5774 *filename_ptr
= filename
;
5775 if (functionname_ptr
)
5776 *functionname_ptr
= bfd_asymbol_name (func
);
5781 /* Find the nearest line to a particular section and offset,
5782 for error reporting. */
5785 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5786 filename_ptr
, functionname_ptr
, line_ptr
)
5791 const char **filename_ptr
;
5792 const char **functionname_ptr
;
5793 unsigned int *line_ptr
;
5797 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5798 filename_ptr
, functionname_ptr
,
5801 if (!*functionname_ptr
)
5802 elf_find_function (abfd
, section
, symbols
, offset
,
5803 *filename_ptr
? NULL
: filename_ptr
,
5809 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5810 filename_ptr
, functionname_ptr
,
5812 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5814 if (!*functionname_ptr
)
5815 elf_find_function (abfd
, section
, symbols
, offset
,
5816 *filename_ptr
? NULL
: filename_ptr
,
5822 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5823 &found
, filename_ptr
,
5824 functionname_ptr
, line_ptr
,
5825 &elf_tdata (abfd
)->line_info
))
5830 if (symbols
== NULL
)
5833 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5834 filename_ptr
, functionname_ptr
))
5842 _bfd_elf_sizeof_headers (abfd
, reloc
)
5848 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5850 ret
+= get_program_header_size (abfd
);
5855 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5860 bfd_size_type count
;
5862 Elf_Internal_Shdr
*hdr
;
5865 if (! abfd
->output_has_begun
5866 && ! _bfd_elf_compute_section_file_positions
5867 (abfd
, (struct bfd_link_info
*) NULL
))
5870 hdr
= &elf_section_data (section
)->this_hdr
;
5871 pos
= hdr
->sh_offset
+ offset
;
5872 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5873 || bfd_bwrite (location
, count
, abfd
) != count
)
5880 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5881 bfd
*abfd ATTRIBUTE_UNUSED
;
5882 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5883 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5890 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5893 Elf_Internal_Rel
*dst
;
5899 /* Try to convert a non-ELF reloc into an ELF one. */
5902 _bfd_elf_validate_reloc (abfd
, areloc
)
5906 /* Check whether we really have an ELF howto. */
5908 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5910 bfd_reloc_code_real_type code
;
5911 reloc_howto_type
*howto
;
5913 /* Alien reloc: Try to determine its type to replace it with an
5914 equivalent ELF reloc. */
5916 if (areloc
->howto
->pc_relative
)
5918 switch (areloc
->howto
->bitsize
)
5921 code
= BFD_RELOC_8_PCREL
;
5924 code
= BFD_RELOC_12_PCREL
;
5927 code
= BFD_RELOC_16_PCREL
;
5930 code
= BFD_RELOC_24_PCREL
;
5933 code
= BFD_RELOC_32_PCREL
;
5936 code
= BFD_RELOC_64_PCREL
;
5942 howto
= bfd_reloc_type_lookup (abfd
, code
);
5944 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5946 if (howto
->pcrel_offset
)
5947 areloc
->addend
+= areloc
->address
;
5949 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5954 switch (areloc
->howto
->bitsize
)
5960 code
= BFD_RELOC_14
;
5963 code
= BFD_RELOC_16
;
5966 code
= BFD_RELOC_26
;
5969 code
= BFD_RELOC_32
;
5972 code
= BFD_RELOC_64
;
5978 howto
= bfd_reloc_type_lookup (abfd
, code
);
5982 areloc
->howto
= howto
;
5990 (*_bfd_error_handler
)
5991 (_("%s: unsupported relocation type %s"),
5992 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5993 bfd_set_error (bfd_error_bad_value
);
5998 _bfd_elf_close_and_cleanup (abfd
)
6001 if (bfd_get_format (abfd
) == bfd_object
)
6003 if (elf_shstrtab (abfd
) != NULL
)
6004 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6007 return _bfd_generic_close_and_cleanup (abfd
);
6010 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6011 in the relocation's offset. Thus we cannot allow any sort of sanity
6012 range-checking to interfere. There is nothing else to do in processing
6015 bfd_reloc_status_type
6016 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
6017 bfd
*abfd ATTRIBUTE_UNUSED
;
6018 arelent
*re ATTRIBUTE_UNUSED
;
6019 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
6020 PTR data ATTRIBUTE_UNUSED
;
6021 asection
*is ATTRIBUTE_UNUSED
;
6022 bfd
*obfd ATTRIBUTE_UNUSED
;
6023 char **errmsg ATTRIBUTE_UNUSED
;
6025 return bfd_reloc_ok
;
6028 /* Elf core file support. Much of this only works on native
6029 toolchains, since we rely on knowing the
6030 machine-dependent procfs structure in order to pick
6031 out details about the corefile. */
6033 #ifdef HAVE_SYS_PROCFS_H
6034 # include <sys/procfs.h>
6037 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6040 elfcore_make_pid (abfd
)
6043 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6044 + (elf_tdata (abfd
)->core_pid
));
6047 /* If there isn't a section called NAME, make one, using
6048 data from SECT. Note, this function will generate a
6049 reference to NAME, so you shouldn't deallocate or
6053 elfcore_maybe_make_sect (abfd
, name
, sect
)
6060 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6063 sect2
= bfd_make_section (abfd
, name
);
6067 sect2
->_raw_size
= sect
->_raw_size
;
6068 sect2
->filepos
= sect
->filepos
;
6069 sect2
->flags
= sect
->flags
;
6070 sect2
->alignment_power
= sect
->alignment_power
;
6074 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6075 actually creates up to two pseudosections:
6076 - For the single-threaded case, a section named NAME, unless
6077 such a section already exists.
6078 - For the multi-threaded case, a section named "NAME/PID", where
6079 PID is elfcore_make_pid (abfd).
6080 Both pseudosections have identical contents. */
6082 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
6089 char *threaded_name
;
6092 /* Build the section name. */
6094 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6095 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6096 if (threaded_name
== NULL
)
6098 strcpy (threaded_name
, buf
);
6100 sect
= bfd_make_section (abfd
, threaded_name
);
6103 sect
->_raw_size
= size
;
6104 sect
->filepos
= filepos
;
6105 sect
->flags
= SEC_HAS_CONTENTS
;
6106 sect
->alignment_power
= 2;
6108 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6111 /* prstatus_t exists on:
6113 linux 2.[01] + glibc
6117 #if defined (HAVE_PRSTATUS_T)
6118 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6121 elfcore_grok_prstatus (abfd
, note
)
6123 Elf_Internal_Note
*note
;
6128 if (note
->descsz
== sizeof (prstatus_t
))
6132 raw_size
= sizeof (prstat
.pr_reg
);
6133 offset
= offsetof (prstatus_t
, pr_reg
);
6134 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6136 /* Do not overwrite the core signal if it
6137 has already been set by another thread. */
6138 if (elf_tdata (abfd
)->core_signal
== 0)
6139 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6140 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6142 /* pr_who exists on:
6145 pr_who doesn't exist on:
6148 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6149 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6152 #if defined (HAVE_PRSTATUS32_T)
6153 else if (note
->descsz
== sizeof (prstatus32_t
))
6155 /* 64-bit host, 32-bit corefile */
6156 prstatus32_t prstat
;
6158 raw_size
= sizeof (prstat
.pr_reg
);
6159 offset
= offsetof (prstatus32_t
, pr_reg
);
6160 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6162 /* Do not overwrite the core signal if it
6163 has already been set by another thread. */
6164 if (elf_tdata (abfd
)->core_signal
== 0)
6165 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6166 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6168 /* pr_who exists on:
6171 pr_who doesn't exist on:
6174 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6175 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6178 #endif /* HAVE_PRSTATUS32_T */
6181 /* Fail - we don't know how to handle any other
6182 note size (ie. data object type). */
6186 /* Make a ".reg/999" section and a ".reg" section. */
6187 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6188 raw_size
, note
->descpos
+ offset
);
6190 #endif /* defined (HAVE_PRSTATUS_T) */
6192 /* Create a pseudosection containing the exact contents of NOTE. */
6194 elfcore_make_note_pseudosection (abfd
, name
, note
)
6197 Elf_Internal_Note
*note
;
6199 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6200 note
->descsz
, note
->descpos
);
6203 /* There isn't a consistent prfpregset_t across platforms,
6204 but it doesn't matter, because we don't have to pick this
6205 data structure apart. */
6208 elfcore_grok_prfpreg (abfd
, note
)
6210 Elf_Internal_Note
*note
;
6212 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6215 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6216 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6220 elfcore_grok_prxfpreg (abfd
, note
)
6222 Elf_Internal_Note
*note
;
6224 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6227 #if defined (HAVE_PRPSINFO_T)
6228 typedef prpsinfo_t elfcore_psinfo_t
;
6229 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6230 typedef prpsinfo32_t elfcore_psinfo32_t
;
6234 #if defined (HAVE_PSINFO_T)
6235 typedef psinfo_t elfcore_psinfo_t
;
6236 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6237 typedef psinfo32_t elfcore_psinfo32_t
;
6241 /* return a malloc'ed copy of a string at START which is at
6242 most MAX bytes long, possibly without a terminating '\0'.
6243 the copy will always have a terminating '\0'. */
6246 _bfd_elfcore_strndup (abfd
, start
, max
)
6252 char *end
= memchr (start
, '\0', max
);
6260 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6264 memcpy (dups
, start
, len
);
6270 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6271 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6274 elfcore_grok_psinfo (abfd
, note
)
6276 Elf_Internal_Note
*note
;
6278 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6280 elfcore_psinfo_t psinfo
;
6282 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6284 elf_tdata (abfd
)->core_program
6285 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6286 sizeof (psinfo
.pr_fname
));
6288 elf_tdata (abfd
)->core_command
6289 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6290 sizeof (psinfo
.pr_psargs
));
6292 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6293 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6295 /* 64-bit host, 32-bit corefile */
6296 elfcore_psinfo32_t psinfo
;
6298 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6300 elf_tdata (abfd
)->core_program
6301 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6302 sizeof (psinfo
.pr_fname
));
6304 elf_tdata (abfd
)->core_command
6305 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6306 sizeof (psinfo
.pr_psargs
));
6312 /* Fail - we don't know how to handle any other
6313 note size (ie. data object type). */
6317 /* Note that for some reason, a spurious space is tacked
6318 onto the end of the args in some (at least one anyway)
6319 implementations, so strip it off if it exists. */
6322 char *command
= elf_tdata (abfd
)->core_command
;
6323 int n
= strlen (command
);
6325 if (0 < n
&& command
[n
- 1] == ' ')
6326 command
[n
- 1] = '\0';
6331 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6333 #if defined (HAVE_PSTATUS_T)
6334 static boolean elfcore_grok_pstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6337 elfcore_grok_pstatus (abfd
, note
)
6339 Elf_Internal_Note
*note
;
6341 if (note
->descsz
== sizeof (pstatus_t
)
6342 #if defined (HAVE_PXSTATUS_T)
6343 || note
->descsz
== sizeof (pxstatus_t
)
6349 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6351 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6353 #if defined (HAVE_PSTATUS32_T)
6354 else if (note
->descsz
== sizeof (pstatus32_t
))
6356 /* 64-bit host, 32-bit corefile */
6359 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6361 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6364 /* Could grab some more details from the "representative"
6365 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6366 NT_LWPSTATUS note, presumably. */
6370 #endif /* defined (HAVE_PSTATUS_T) */
6372 #if defined (HAVE_LWPSTATUS_T)
6373 static boolean elfcore_grok_lwpstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6376 elfcore_grok_lwpstatus (abfd
, note
)
6378 Elf_Internal_Note
*note
;
6380 lwpstatus_t lwpstat
;
6385 if (note
->descsz
!= sizeof (lwpstat
)
6386 #if defined (HAVE_LWPXSTATUS_T)
6387 && note
->descsz
!= sizeof (lwpxstatus_t
)
6392 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6394 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6395 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6397 /* Make a ".reg/999" section. */
6399 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6400 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6405 sect
= bfd_make_section (abfd
, name
);
6409 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6410 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6411 sect
->filepos
= note
->descpos
6412 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6415 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6416 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6417 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6420 sect
->flags
= SEC_HAS_CONTENTS
;
6421 sect
->alignment_power
= 2;
6423 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6426 /* Make a ".reg2/999" section */
6428 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6429 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6434 sect
= bfd_make_section (abfd
, name
);
6438 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6439 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6440 sect
->filepos
= note
->descpos
6441 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6444 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6445 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6446 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6449 sect
->flags
= SEC_HAS_CONTENTS
;
6450 sect
->alignment_power
= 2;
6452 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6454 #endif /* defined (HAVE_LWPSTATUS_T) */
6456 #if defined (HAVE_WIN32_PSTATUS_T)
6458 elfcore_grok_win32pstatus (abfd
, note
)
6460 Elf_Internal_Note
*note
;
6465 win32_pstatus_t pstatus
;
6467 if (note
->descsz
< sizeof (pstatus
))
6470 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6472 switch (pstatus
.data_type
)
6474 case NOTE_INFO_PROCESS
:
6475 /* FIXME: need to add ->core_command. */
6476 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6477 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6480 case NOTE_INFO_THREAD
:
6481 /* Make a ".reg/999" section. */
6482 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6484 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6490 sect
= bfd_make_section (abfd
, name
);
6494 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6495 sect
->filepos
= (note
->descpos
6496 + offsetof (struct win32_pstatus
,
6497 data
.thread_info
.thread_context
));
6498 sect
->flags
= SEC_HAS_CONTENTS
;
6499 sect
->alignment_power
= 2;
6501 if (pstatus
.data
.thread_info
.is_active_thread
)
6502 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6506 case NOTE_INFO_MODULE
:
6507 /* Make a ".module/xxxxxxxx" section. */
6508 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6510 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6516 sect
= bfd_make_section (abfd
, name
);
6521 sect
->_raw_size
= note
->descsz
;
6522 sect
->filepos
= note
->descpos
;
6523 sect
->flags
= SEC_HAS_CONTENTS
;
6524 sect
->alignment_power
= 2;
6533 #endif /* HAVE_WIN32_PSTATUS_T */
6536 elfcore_grok_note (abfd
, note
)
6538 Elf_Internal_Note
*note
;
6540 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6548 if (bed
->elf_backend_grok_prstatus
)
6549 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6551 #if defined (HAVE_PRSTATUS_T)
6552 return elfcore_grok_prstatus (abfd
, note
);
6557 #if defined (HAVE_PSTATUS_T)
6559 return elfcore_grok_pstatus (abfd
, note
);
6562 #if defined (HAVE_LWPSTATUS_T)
6564 return elfcore_grok_lwpstatus (abfd
, note
);
6567 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6568 return elfcore_grok_prfpreg (abfd
, note
);
6570 #if defined (HAVE_WIN32_PSTATUS_T)
6571 case NT_WIN32PSTATUS
:
6572 return elfcore_grok_win32pstatus (abfd
, note
);
6575 case NT_PRXFPREG
: /* Linux SSE extension */
6576 if (note
->namesz
== 5
6577 && ! strcmp (note
->namedata
, "LINUX"))
6578 return elfcore_grok_prxfpreg (abfd
, note
);
6584 if (bed
->elf_backend_grok_psinfo
)
6585 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6587 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6588 return elfcore_grok_psinfo (abfd
, note
);
6596 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6597 Elf_Internal_Note
*note
;
6602 cp
= strchr (note
->namedata
, '@');
6605 *lwpidp
= atoi(cp
+ 1);
6612 elfcore_grok_netbsd_procinfo (abfd
, note
)
6614 Elf_Internal_Note
*note
;
6617 /* Signal number at offset 0x08. */
6618 elf_tdata (abfd
)->core_signal
6619 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6621 /* Process ID at offset 0x50. */
6622 elf_tdata (abfd
)->core_pid
6623 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6625 /* Command name at 0x7c (max 32 bytes, including nul). */
6626 elf_tdata (abfd
)->core_command
6627 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6633 elfcore_grok_netbsd_note (abfd
, note
)
6635 Elf_Internal_Note
*note
;
6639 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6640 elf_tdata (abfd
)->core_lwpid
= lwp
;
6642 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6644 /* NetBSD-specific core "procinfo". Note that we expect to
6645 find this note before any of the others, which is fine,
6646 since the kernel writes this note out first when it
6647 creates a core file. */
6649 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6652 /* As of Jan 2002 there are no other machine-independent notes
6653 defined for NetBSD core files. If the note type is less
6654 than the start of the machine-dependent note types, we don't
6657 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6661 switch (bfd_get_arch (abfd
))
6663 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6664 PT_GETFPREGS == mach+2. */
6666 case bfd_arch_alpha
:
6667 case bfd_arch_sparc
:
6670 case NT_NETBSDCORE_FIRSTMACH
+0:
6671 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6673 case NT_NETBSDCORE_FIRSTMACH
+2:
6674 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6680 /* On all other arch's, PT_GETREGS == mach+1 and
6681 PT_GETFPREGS == mach+3. */
6686 case NT_NETBSDCORE_FIRSTMACH
+1:
6687 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6689 case NT_NETBSDCORE_FIRSTMACH
+3:
6690 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6699 /* Function: elfcore_write_note
6706 size of data for note
6709 End of buffer containing note. */
6712 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6721 Elf_External_Note
*xnp
;
6722 int namesz
= strlen (name
);
6723 int newspace
= BFD_ALIGN (sizeof (Elf_External_Note
) + size
+ namesz
- 1, 4);
6726 p
= realloc (buf
, *bufsiz
+ newspace
);
6728 *bufsiz
+= newspace
;
6729 xnp
= (Elf_External_Note
*) dest
;
6730 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6731 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6732 H_PUT_32 (abfd
, type
, xnp
->type
);
6733 strcpy (xnp
->name
, name
);
6734 memcpy (xnp
->name
+ BFD_ALIGN (namesz
, 4), input
, size
);
6738 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6740 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6748 char *note_name
= "CORE";
6750 #if defined (HAVE_PSINFO_T)
6752 note_type
= NT_PSINFO
;
6755 note_type
= NT_PRPSINFO
;
6758 memset (&data
, 0, sizeof (data
));
6759 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6760 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6761 return elfcore_write_note (abfd
, buf
, bufsiz
,
6762 note_name
, note_type
, &data
, sizeof (data
));
6764 #endif /* PSINFO_T or PRPSINFO_T */
6766 #if defined (HAVE_PRSTATUS_T)
6768 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6777 char *note_name
= "CORE";
6779 memset (&prstat
, 0, sizeof (prstat
));
6780 prstat
.pr_pid
= pid
;
6781 prstat
.pr_cursig
= cursig
;
6782 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
6783 return elfcore_write_note (abfd
, buf
, bufsiz
,
6784 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
6786 #endif /* HAVE_PRSTATUS_T */
6788 #if defined (HAVE_LWPSTATUS_T)
6790 elfcore_write_lwpstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6798 lwpstatus_t lwpstat
;
6799 char *note_name
= "CORE";
6801 memset (&lwpstat
, 0, sizeof (lwpstat
));
6802 lwpstat
.pr_lwpid
= pid
>> 16;
6803 lwpstat
.pr_cursig
= cursig
;
6804 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6805 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
6806 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6808 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
6809 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
6811 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
6812 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
6815 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6816 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
6818 #endif /* HAVE_LWPSTATUS_T */
6820 #if defined (HAVE_PSTATUS_T)
6822 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6831 char *note_name
= "CORE";
6833 memset (&pstat
, 0, sizeof (pstat
));
6834 pstat
.pr_pid
= pid
& 0xffff;
6835 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6836 NT_PSTATUS
, &pstat
, sizeof (pstat
));
6839 #endif /* HAVE_PSTATUS_T */
6842 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
6849 char *note_name
= "CORE";
6850 return elfcore_write_note (abfd
, buf
, bufsiz
,
6851 note_name
, NT_FPREGSET
, fpregs
, size
);
6855 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
6862 char *note_name
= "LINUX";
6863 return elfcore_write_note (abfd
, buf
, bufsiz
,
6864 note_name
, NT_PRXFPREG
, xfpregs
, size
);
6868 elfcore_read_notes (abfd
, offset
, size
)
6879 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6882 buf
= bfd_malloc (size
);
6886 if (bfd_bread (buf
, size
, abfd
) != size
)
6894 while (p
< buf
+ size
)
6896 /* FIXME: bad alignment assumption. */
6897 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6898 Elf_Internal_Note in
;
6900 in
.type
= H_GET_32 (abfd
, xnp
->type
);
6902 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
6903 in
.namedata
= xnp
->name
;
6905 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
6906 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
6907 in
.descpos
= offset
+ (in
.descdata
- buf
);
6909 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
6911 if (! elfcore_grok_netbsd_note (abfd
, &in
))
6916 if (! elfcore_grok_note (abfd
, &in
))
6920 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
6927 /* Providing external access to the ELF program header table. */
6929 /* Return an upper bound on the number of bytes required to store a
6930 copy of ABFD's program header table entries. Return -1 if an error
6931 occurs; bfd_get_error will return an appropriate code. */
6934 bfd_get_elf_phdr_upper_bound (abfd
)
6937 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6939 bfd_set_error (bfd_error_wrong_format
);
6943 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
6946 /* Copy ABFD's program header table entries to *PHDRS. The entries
6947 will be stored as an array of Elf_Internal_Phdr structures, as
6948 defined in include/elf/internal.h. To find out how large the
6949 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6951 Return the number of program header table entries read, or -1 if an
6952 error occurs; bfd_get_error will return an appropriate code. */
6955 bfd_get_elf_phdrs (abfd
, phdrs
)
6961 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6963 bfd_set_error (bfd_error_wrong_format
);
6967 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
6968 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
6969 num_phdrs
* sizeof (Elf_Internal_Phdr
));
6975 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
6976 bfd
*abfd ATTRIBUTE_UNUSED
;
6981 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6983 i_ehdrp
= elf_elfheader (abfd
);
6984 if (i_ehdrp
== NULL
)
6985 sprintf_vma (buf
, value
);
6988 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6990 #if BFD_HOST_64BIT_LONG
6991 sprintf (buf
, "%016lx", value
);
6993 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
6994 _bfd_int64_low (value
));
6998 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7001 sprintf_vma (buf
, value
);
7006 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
7007 bfd
*abfd ATTRIBUTE_UNUSED
;
7012 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7014 i_ehdrp
= elf_elfheader (abfd
);
7015 if (i_ehdrp
== NULL
)
7016 fprintf_vma ((FILE *) stream
, value
);
7019 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7021 #if BFD_HOST_64BIT_LONG
7022 fprintf ((FILE *) stream
, "%016lx", value
);
7024 fprintf ((FILE *) stream
, "%08lx%08lx",
7025 _bfd_int64_high (value
), _bfd_int64_low (value
));
7029 fprintf ((FILE *) stream
, "%08lx",
7030 (unsigned long) (value
& 0xffffffff));
7033 fprintf_vma ((FILE *) stream
, value
);
7037 enum elf_reloc_type_class
7038 _bfd_elf_reloc_type_class (rela
)
7039 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
7041 return reloc_class_normal
;
7044 /* For RELA architectures, return what the relocation value for
7045 relocation against a local symbol. */
7048 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
7050 Elf_Internal_Sym
*sym
;
7052 Elf_Internal_Rela
*rel
;
7056 relocation
= (sec
->output_section
->vma
7057 + sec
->output_offset
7059 if ((sec
->flags
& SEC_MERGE
)
7060 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7061 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7067 _bfd_merged_section_offset (abfd
, &msec
,
7068 elf_section_data (sec
)->sec_info
,
7069 sym
->st_value
+ rel
->r_addend
,
7072 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7078 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
7080 Elf_Internal_Sym
*sym
;
7084 asection
*sec
= *psec
;
7086 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7087 return sym
->st_value
+ addend
;
7089 return _bfd_merged_section_offset (abfd
, psec
,
7090 elf_section_data (sec
)->sec_info
,
7091 sym
->st_value
+ addend
, (bfd_vma
) 0);
7095 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
7097 struct bfd_link_info
*info
;
7101 struct bfd_elf_section_data
*sec_data
;
7103 sec_data
= elf_section_data (sec
);
7104 switch (sec_data
->sec_info_type
)
7106 case ELF_INFO_TYPE_STABS
:
7107 return _bfd_stab_section_offset
7108 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
7110 case ELF_INFO_TYPE_EH_FRAME
:
7111 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);