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. */
574 /* Make a BFD section from an ELF section. We store a pointer to the
575 BFD section in the bfd_section field of the header. */
578 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
580 Elf_Internal_Shdr
*hdr
;
585 struct elf_backend_data
*bed
;
587 if (hdr
->bfd_section
!= NULL
)
589 BFD_ASSERT (strcmp (name
,
590 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
594 newsect
= bfd_make_section_anyway (abfd
, name
);
598 newsect
->filepos
= hdr
->sh_offset
;
600 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
601 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
602 || ! bfd_set_section_alignment (abfd
, newsect
,
603 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
606 flags
= SEC_NO_FLAGS
;
607 if (hdr
->sh_type
!= SHT_NOBITS
)
608 flags
|= SEC_HAS_CONTENTS
;
609 if (hdr
->sh_type
== SHT_GROUP
)
610 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
611 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
614 if (hdr
->sh_type
!= SHT_NOBITS
)
617 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
618 flags
|= SEC_READONLY
;
619 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
621 else if ((flags
& SEC_LOAD
) != 0)
623 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
626 newsect
->entsize
= hdr
->sh_entsize
;
627 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
628 flags
|= SEC_STRINGS
;
630 if (hdr
->sh_flags
& SHF_GROUP
)
631 if (!setup_group (abfd
, hdr
, newsect
))
633 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
634 flags
|= SEC_THREAD_LOCAL
;
636 /* The debugging sections appear to be recognized only by name, not
639 static const char *debug_sec_names
[] =
648 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
649 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
653 flags
|= SEC_DEBUGGING
;
656 /* As a GNU extension, if the name begins with .gnu.linkonce, we
657 only link a single copy of the section. This is used to support
658 g++. g++ will emit each template expansion in its own section.
659 The symbols will be defined as weak, so that multiple definitions
660 are permitted. The GNU linker extension is to actually discard
661 all but one of the sections. */
662 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
663 && elf_next_in_group (newsect
) == NULL
)
664 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
666 bed
= get_elf_backend_data (abfd
);
667 if (bed
->elf_backend_section_flags
)
668 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
671 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
674 if ((flags
& SEC_ALLOC
) != 0)
676 Elf_Internal_Phdr
*phdr
;
679 /* Look through the phdrs to see if we need to adjust the lma.
680 If all the p_paddr fields are zero, we ignore them, since
681 some ELF linkers produce such output. */
682 phdr
= elf_tdata (abfd
)->phdr
;
683 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
685 if (phdr
->p_paddr
!= 0)
688 if (i
< elf_elfheader (abfd
)->e_phnum
)
690 phdr
= elf_tdata (abfd
)->phdr
;
691 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
693 /* This section is part of this segment if its file
694 offset plus size lies within the segment's memory
695 span and, if the section is loaded, the extent of the
696 loaded data lies within the extent of the segment.
698 Note - we used to check the p_paddr field as well, and
699 refuse to set the LMA if it was 0. This is wrong
700 though, as a perfectly valid initialised segment can
701 have a p_paddr of zero. Some architectures, eg ARM,
702 place special significance on the address 0 and
703 executables need to be able to have a segment which
704 covers this address. */
705 if (phdr
->p_type
== PT_LOAD
706 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
707 && (hdr
->sh_offset
+ hdr
->sh_size
708 <= phdr
->p_offset
+ phdr
->p_memsz
)
709 && ((flags
& SEC_LOAD
) == 0
710 || (hdr
->sh_offset
+ hdr
->sh_size
711 <= phdr
->p_offset
+ phdr
->p_filesz
)))
713 if ((flags
& SEC_LOAD
) == 0)
714 newsect
->lma
= (phdr
->p_paddr
715 + hdr
->sh_addr
- phdr
->p_vaddr
);
717 /* We used to use the same adjustment for SEC_LOAD
718 sections, but that doesn't work if the segment
719 is packed with code from multiple VMAs.
720 Instead we calculate the section LMA based on
721 the segment LMA. It is assumed that the
722 segment will contain sections with contiguous
723 LMAs, even if the VMAs are not. */
724 newsect
->lma
= (phdr
->p_paddr
725 + hdr
->sh_offset
- phdr
->p_offset
);
727 /* With contiguous segments, we can't tell from file
728 offsets whether a section with zero size should
729 be placed at the end of one segment or the
730 beginning of the next. Decide based on vaddr. */
731 if (hdr
->sh_addr
>= phdr
->p_vaddr
732 && (hdr
->sh_addr
+ hdr
->sh_size
733 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
740 hdr
->bfd_section
= newsect
;
741 elf_section_data (newsect
)->this_hdr
= *hdr
;
751 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
754 Helper functions for GDB to locate the string tables.
755 Since BFD hides string tables from callers, GDB needs to use an
756 internal hook to find them. Sun's .stabstr, in particular,
757 isn't even pointed to by the .stab section, so ordinary
758 mechanisms wouldn't work to find it, even if we had some.
761 struct elf_internal_shdr
*
762 bfd_elf_find_section (abfd
, name
)
766 Elf_Internal_Shdr
**i_shdrp
;
771 i_shdrp
= elf_elfsections (abfd
);
774 shstrtab
= bfd_elf_get_str_section (abfd
,
775 elf_elfheader (abfd
)->e_shstrndx
);
776 if (shstrtab
!= NULL
)
778 max
= elf_numsections (abfd
);
779 for (i
= 1; i
< max
; i
++)
780 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
787 const char *const bfd_elf_section_type_names
[] = {
788 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
789 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
790 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
793 /* ELF relocs are against symbols. If we are producing relocateable
794 output, and the reloc is against an external symbol, and nothing
795 has given us any additional addend, the resulting reloc will also
796 be against the same symbol. In such a case, we don't want to
797 change anything about the way the reloc is handled, since it will
798 all be done at final link time. Rather than put special case code
799 into bfd_perform_relocation, all the reloc types use this howto
800 function. It just short circuits the reloc if producing
801 relocateable output against an external symbol. */
803 bfd_reloc_status_type
804 bfd_elf_generic_reloc (abfd
,
811 bfd
*abfd ATTRIBUTE_UNUSED
;
812 arelent
*reloc_entry
;
814 PTR data ATTRIBUTE_UNUSED
;
815 asection
*input_section
;
817 char **error_message ATTRIBUTE_UNUSED
;
819 if (output_bfd
!= (bfd
*) NULL
820 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
821 && (! reloc_entry
->howto
->partial_inplace
822 || reloc_entry
->addend
== 0))
824 reloc_entry
->address
+= input_section
->output_offset
;
828 return bfd_reloc_continue
;
831 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
834 merge_sections_remove_hook (abfd
, sec
)
835 bfd
*abfd ATTRIBUTE_UNUSED
;
838 struct bfd_elf_section_data
*sec_data
;
840 sec_data
= elf_section_data (sec
);
841 BFD_ASSERT (sec_data
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
842 sec_data
->sec_info_type
= ELF_INFO_TYPE_NONE
;
845 /* Finish SHF_MERGE section merging. */
848 _bfd_elf_merge_sections (abfd
, info
)
850 struct bfd_link_info
*info
;
852 if (!is_elf_hash_table (info
))
854 if (elf_hash_table (info
)->merge_info
)
855 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
856 merge_sections_remove_hook
);
861 _bfd_elf_link_just_syms (sec
, info
)
863 struct bfd_link_info
*info
;
865 sec
->output_section
= bfd_abs_section_ptr
;
866 sec
->output_offset
= sec
->vma
;
867 if (!is_elf_hash_table (info
))
870 elf_section_data (sec
)->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
873 /* Copy the program header and other data from one object module to
877 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
881 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
882 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
885 BFD_ASSERT (!elf_flags_init (obfd
)
886 || (elf_elfheader (obfd
)->e_flags
887 == elf_elfheader (ibfd
)->e_flags
));
889 elf_gp (obfd
) = elf_gp (ibfd
);
890 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
891 elf_flags_init (obfd
) = true;
895 /* Print out the program headers. */
898 _bfd_elf_print_private_bfd_data (abfd
, farg
)
902 FILE *f
= (FILE *) farg
;
903 Elf_Internal_Phdr
*p
;
905 bfd_byte
*dynbuf
= NULL
;
907 p
= elf_tdata (abfd
)->phdr
;
912 fprintf (f
, _("\nProgram Header:\n"));
913 c
= elf_elfheader (abfd
)->e_phnum
;
914 for (i
= 0; i
< c
; i
++, p
++)
921 case PT_NULL
: pt
= "NULL"; break;
922 case PT_LOAD
: pt
= "LOAD"; break;
923 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
924 case PT_INTERP
: pt
= "INTERP"; break;
925 case PT_NOTE
: pt
= "NOTE"; break;
926 case PT_SHLIB
: pt
= "SHLIB"; break;
927 case PT_PHDR
: pt
= "PHDR"; break;
928 case PT_TLS
: pt
= "TLS"; break;
929 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
930 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
932 fprintf (f
, "%8s off 0x", pt
);
933 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
934 fprintf (f
, " vaddr 0x");
935 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
936 fprintf (f
, " paddr 0x");
937 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
938 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
939 fprintf (f
, " filesz 0x");
940 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
941 fprintf (f
, " memsz 0x");
942 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
943 fprintf (f
, " flags %c%c%c",
944 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
945 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
946 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
947 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
948 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
953 s
= bfd_get_section_by_name (abfd
, ".dynamic");
957 unsigned long shlink
;
958 bfd_byte
*extdyn
, *extdynend
;
960 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
962 fprintf (f
, _("\nDynamic Section:\n"));
964 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
967 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
971 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
974 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
976 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
977 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
980 extdynend
= extdyn
+ s
->_raw_size
;
981 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
983 Elf_Internal_Dyn dyn
;
988 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
990 if (dyn
.d_tag
== DT_NULL
)
997 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
1001 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
1002 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
1003 case DT_PLTGOT
: name
= "PLTGOT"; break;
1004 case DT_HASH
: name
= "HASH"; break;
1005 case DT_STRTAB
: name
= "STRTAB"; break;
1006 case DT_SYMTAB
: name
= "SYMTAB"; break;
1007 case DT_RELA
: name
= "RELA"; break;
1008 case DT_RELASZ
: name
= "RELASZ"; break;
1009 case DT_RELAENT
: name
= "RELAENT"; break;
1010 case DT_STRSZ
: name
= "STRSZ"; break;
1011 case DT_SYMENT
: name
= "SYMENT"; break;
1012 case DT_INIT
: name
= "INIT"; break;
1013 case DT_FINI
: name
= "FINI"; break;
1014 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
1015 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
1016 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
1017 case DT_REL
: name
= "REL"; break;
1018 case DT_RELSZ
: name
= "RELSZ"; break;
1019 case DT_RELENT
: name
= "RELENT"; break;
1020 case DT_PLTREL
: name
= "PLTREL"; break;
1021 case DT_DEBUG
: name
= "DEBUG"; break;
1022 case DT_TEXTREL
: name
= "TEXTREL"; break;
1023 case DT_JMPREL
: name
= "JMPREL"; break;
1024 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
1025 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
1026 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
1027 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
1028 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
1029 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
1030 case DT_FLAGS
: name
= "FLAGS"; break;
1031 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
1032 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
1033 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
1034 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
1035 case DT_MOVEENT
: name
= "MOVEENT"; break;
1036 case DT_MOVESZ
: name
= "MOVESZ"; break;
1037 case DT_FEATURE
: name
= "FEATURE"; break;
1038 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
1039 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1040 case DT_SYMINENT
: name
= "SYMINENT"; break;
1041 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1042 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1043 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1044 case DT_PLTPAD
: name
= "PLTPAD"; break;
1045 case DT_MOVETAB
: name
= "MOVETAB"; break;
1046 case DT_SYMINFO
: name
= "SYMINFO"; break;
1047 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1048 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1049 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1050 case DT_VERSYM
: name
= "VERSYM"; break;
1051 case DT_VERDEF
: name
= "VERDEF"; break;
1052 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1053 case DT_VERNEED
: name
= "VERNEED"; break;
1054 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1055 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1056 case DT_USED
: name
= "USED"; break;
1057 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1060 fprintf (f
, " %-11s ", name
);
1062 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1066 unsigned int tagv
= dyn
.d_un
.d_val
;
1068 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1071 fprintf (f
, "%s", string
);
1080 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1081 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1083 if (! _bfd_elf_slurp_version_tables (abfd
))
1087 if (elf_dynverdef (abfd
) != 0)
1089 Elf_Internal_Verdef
*t
;
1091 fprintf (f
, _("\nVersion definitions:\n"));
1092 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1094 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1095 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1096 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1098 Elf_Internal_Verdaux
*a
;
1101 for (a
= t
->vd_auxptr
->vda_nextptr
;
1104 fprintf (f
, "%s ", a
->vda_nodename
);
1110 if (elf_dynverref (abfd
) != 0)
1112 Elf_Internal_Verneed
*t
;
1114 fprintf (f
, _("\nVersion References:\n"));
1115 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1117 Elf_Internal_Vernaux
*a
;
1119 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1120 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1121 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1122 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1134 /* Display ELF-specific fields of a symbol. */
1137 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1141 bfd_print_symbol_type how
;
1143 FILE *file
= (FILE *) filep
;
1146 case bfd_print_symbol_name
:
1147 fprintf (file
, "%s", symbol
->name
);
1149 case bfd_print_symbol_more
:
1150 fprintf (file
, "elf ");
1151 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1152 fprintf (file
, " %lx", (long) symbol
->flags
);
1154 case bfd_print_symbol_all
:
1156 const char *section_name
;
1157 const char *name
= NULL
;
1158 struct elf_backend_data
*bed
;
1159 unsigned char st_other
;
1162 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1164 bed
= get_elf_backend_data (abfd
);
1165 if (bed
->elf_backend_print_symbol_all
)
1166 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1170 name
= symbol
->name
;
1171 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1174 fprintf (file
, " %s\t", section_name
);
1175 /* Print the "other" value for a symbol. For common symbols,
1176 we've already printed the size; now print the alignment.
1177 For other symbols, we have no specified alignment, and
1178 we've printed the address; now print the size. */
1179 if (bfd_is_com_section (symbol
->section
))
1180 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1182 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1183 bfd_fprintf_vma (abfd
, file
, val
);
1185 /* If we have version information, print it. */
1186 if (elf_tdata (abfd
)->dynversym_section
!= 0
1187 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1188 || elf_tdata (abfd
)->dynverref_section
!= 0))
1190 unsigned int vernum
;
1191 const char *version_string
;
1193 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1196 version_string
= "";
1197 else if (vernum
== 1)
1198 version_string
= "Base";
1199 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1201 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1204 Elf_Internal_Verneed
*t
;
1206 version_string
= "";
1207 for (t
= elf_tdata (abfd
)->verref
;
1211 Elf_Internal_Vernaux
*a
;
1213 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1215 if (a
->vna_other
== vernum
)
1217 version_string
= a
->vna_nodename
;
1224 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1225 fprintf (file
, " %-11s", version_string
);
1230 fprintf (file
, " (%s)", version_string
);
1231 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1236 /* If the st_other field is not zero, print it. */
1237 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1242 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1243 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1244 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1246 /* Some other non-defined flags are also present, so print
1248 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1251 fprintf (file
, " %s", name
);
1257 /* Create an entry in an ELF linker hash table. */
1259 struct bfd_hash_entry
*
1260 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1261 struct bfd_hash_entry
*entry
;
1262 struct bfd_hash_table
*table
;
1265 /* Allocate the structure if it has not already been allocated by a
1269 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1274 /* Call the allocation method of the superclass. */
1275 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1278 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1279 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1281 /* Set local fields. */
1285 ret
->dynstr_index
= 0;
1286 ret
->weakdef
= NULL
;
1287 ret
->got
.refcount
= htab
->init_refcount
;
1288 ret
->plt
.refcount
= htab
->init_refcount
;
1289 ret
->linker_section_pointer
= NULL
;
1290 ret
->verinfo
.verdef
= NULL
;
1291 ret
->vtable_entries_used
= NULL
;
1292 ret
->vtable_entries_size
= 0;
1293 ret
->vtable_parent
= NULL
;
1294 ret
->type
= STT_NOTYPE
;
1296 /* Assume that we have been called by a non-ELF symbol reader.
1297 This flag is then reset by the code which reads an ELF input
1298 file. This ensures that a symbol created by a non-ELF symbol
1299 reader will have the flag set correctly. */
1300 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1306 /* Copy data from an indirect symbol to its direct symbol, hiding the
1307 old indirect symbol. Also used for copying flags to a weakdef. */
1310 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1311 struct elf_link_hash_entry
*dir
, *ind
;
1315 /* Copy down any references that we may have already seen to the
1316 symbol which just became indirect. */
1318 dir
->elf_link_hash_flags
|=
1319 (ind
->elf_link_hash_flags
1320 & (ELF_LINK_HASH_REF_DYNAMIC
1321 | ELF_LINK_HASH_REF_REGULAR
1322 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1323 | ELF_LINK_NON_GOT_REF
));
1325 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1328 /* Copy over the global and procedure linkage table refcount entries.
1329 These may have been already set up by a check_relocs routine. */
1330 tmp
= dir
->got
.refcount
;
1333 dir
->got
.refcount
= ind
->got
.refcount
;
1334 ind
->got
.refcount
= tmp
;
1337 BFD_ASSERT (ind
->got
.refcount
<= 0);
1339 tmp
= dir
->plt
.refcount
;
1342 dir
->plt
.refcount
= ind
->plt
.refcount
;
1343 ind
->plt
.refcount
= tmp
;
1346 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1348 if (dir
->dynindx
== -1)
1350 dir
->dynindx
= ind
->dynindx
;
1351 dir
->dynstr_index
= ind
->dynstr_index
;
1353 ind
->dynstr_index
= 0;
1356 BFD_ASSERT (ind
->dynindx
== -1);
1360 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
)
1361 struct bfd_link_info
*info
;
1362 struct elf_link_hash_entry
*h
;
1363 boolean force_local
;
1365 h
->plt
.offset
= (bfd_vma
) -1;
1366 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1369 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1370 if (h
->dynindx
!= -1)
1373 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1379 /* Initialize an ELF linker hash table. */
1382 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1383 struct elf_link_hash_table
*table
;
1385 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1386 struct bfd_hash_table
*,
1391 table
->dynamic_sections_created
= false;
1392 table
->dynobj
= NULL
;
1393 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1394 /* The first dynamic symbol is a dummy. */
1395 table
->dynsymcount
= 1;
1396 table
->dynstr
= NULL
;
1397 table
->bucketcount
= 0;
1398 table
->needed
= NULL
;
1399 table
->runpath
= NULL
;
1401 table
->stab_info
= NULL
;
1402 table
->merge_info
= NULL
;
1403 table
->dynlocal
= NULL
;
1404 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1405 table
->root
.type
= bfd_link_elf_hash_table
;
1410 /* Create an ELF linker hash table. */
1412 struct bfd_link_hash_table
*
1413 _bfd_elf_link_hash_table_create (abfd
)
1416 struct elf_link_hash_table
*ret
;
1417 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1419 ret
= (struct elf_link_hash_table
*) bfd_malloc (amt
);
1420 if (ret
== (struct elf_link_hash_table
*) NULL
)
1423 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1432 /* This is a hook for the ELF emulation code in the generic linker to
1433 tell the backend linker what file name to use for the DT_NEEDED
1434 entry for a dynamic object. The generic linker passes name as an
1435 empty string to indicate that no DT_NEEDED entry should be made. */
1438 bfd_elf_set_dt_needed_name (abfd
, name
)
1442 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1443 && bfd_get_format (abfd
) == bfd_object
)
1444 elf_dt_name (abfd
) = name
;
1448 bfd_elf_set_dt_needed_soname (abfd
, name
)
1452 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1453 && bfd_get_format (abfd
) == bfd_object
)
1454 elf_dt_soname (abfd
) = name
;
1457 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1458 the linker ELF emulation code. */
1460 struct bfd_link_needed_list
*
1461 bfd_elf_get_needed_list (abfd
, info
)
1462 bfd
*abfd ATTRIBUTE_UNUSED
;
1463 struct bfd_link_info
*info
;
1465 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1467 return elf_hash_table (info
)->needed
;
1470 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1471 hook for the linker ELF emulation code. */
1473 struct bfd_link_needed_list
*
1474 bfd_elf_get_runpath_list (abfd
, info
)
1475 bfd
*abfd ATTRIBUTE_UNUSED
;
1476 struct bfd_link_info
*info
;
1478 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1480 return elf_hash_table (info
)->runpath
;
1483 /* Get the name actually used for a dynamic object for a link. This
1484 is the SONAME entry if there is one. Otherwise, it is the string
1485 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1488 bfd_elf_get_dt_soname (abfd
)
1491 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1492 && bfd_get_format (abfd
) == bfd_object
)
1493 return elf_dt_name (abfd
);
1497 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1498 the ELF linker emulation code. */
1501 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1503 struct bfd_link_needed_list
**pneeded
;
1506 bfd_byte
*dynbuf
= NULL
;
1508 unsigned long shlink
;
1509 bfd_byte
*extdyn
, *extdynend
;
1511 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1515 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1516 || bfd_get_format (abfd
) != bfd_object
)
1519 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1520 if (s
== NULL
|| s
->_raw_size
== 0)
1523 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1527 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1531 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1535 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1537 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1538 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1541 extdynend
= extdyn
+ s
->_raw_size
;
1542 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1544 Elf_Internal_Dyn dyn
;
1546 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1548 if (dyn
.d_tag
== DT_NULL
)
1551 if (dyn
.d_tag
== DT_NEEDED
)
1554 struct bfd_link_needed_list
*l
;
1555 unsigned int tagv
= dyn
.d_un
.d_val
;
1558 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1563 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1584 /* Allocate an ELF string table--force the first byte to be zero. */
1586 struct bfd_strtab_hash
*
1587 _bfd_elf_stringtab_init ()
1589 struct bfd_strtab_hash
*ret
;
1591 ret
= _bfd_stringtab_init ();
1596 loc
= _bfd_stringtab_add (ret
, "", true, false);
1597 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1598 if (loc
== (bfd_size_type
) -1)
1600 _bfd_stringtab_free (ret
);
1607 /* ELF .o/exec file reading */
1609 /* Create a new bfd section from an ELF section header. */
1612 bfd_section_from_shdr (abfd
, shindex
)
1614 unsigned int shindex
;
1616 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1617 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1618 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1621 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1623 switch (hdr
->sh_type
)
1626 /* Inactive section. Throw it away. */
1629 case SHT_PROGBITS
: /* Normal section with contents. */
1630 case SHT_DYNAMIC
: /* Dynamic linking information. */
1631 case SHT_NOBITS
: /* .bss section. */
1632 case SHT_HASH
: /* .hash section. */
1633 case SHT_NOTE
: /* .note section. */
1634 case SHT_INIT_ARRAY
: /* .init_array section. */
1635 case SHT_FINI_ARRAY
: /* .fini_array section. */
1636 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1637 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1639 case SHT_SYMTAB
: /* A symbol table */
1640 if (elf_onesymtab (abfd
) == shindex
)
1643 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1644 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1645 elf_onesymtab (abfd
) = shindex
;
1646 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1647 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1648 abfd
->flags
|= HAS_SYMS
;
1650 /* Sometimes a shared object will map in the symbol table. If
1651 SHF_ALLOC is set, and this is a shared object, then we also
1652 treat this section as a BFD section. We can not base the
1653 decision purely on SHF_ALLOC, because that flag is sometimes
1654 set in a relocateable object file, which would confuse the
1656 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1657 && (abfd
->flags
& DYNAMIC
) != 0
1658 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1663 case SHT_DYNSYM
: /* A dynamic symbol table */
1664 if (elf_dynsymtab (abfd
) == shindex
)
1667 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1668 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1669 elf_dynsymtab (abfd
) = shindex
;
1670 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1671 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1672 abfd
->flags
|= HAS_SYMS
;
1674 /* Besides being a symbol table, we also treat this as a regular
1675 section, so that objcopy can handle it. */
1676 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1678 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1679 if (elf_symtab_shndx (abfd
) == shindex
)
1682 /* Get the associated symbol table. */
1683 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1684 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1687 elf_symtab_shndx (abfd
) = shindex
;
1688 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1689 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1692 case SHT_STRTAB
: /* A string table */
1693 if (hdr
->bfd_section
!= NULL
)
1695 if (ehdr
->e_shstrndx
== shindex
)
1697 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1698 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1702 unsigned int i
, num_sec
;
1704 num_sec
= elf_numsections (abfd
);
1705 for (i
= 1; i
< num_sec
; i
++)
1707 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1708 if (hdr2
->sh_link
== shindex
)
1710 if (! bfd_section_from_shdr (abfd
, i
))
1712 if (elf_onesymtab (abfd
) == i
)
1714 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1715 elf_elfsections (abfd
)[shindex
] =
1716 &elf_tdata (abfd
)->strtab_hdr
;
1719 if (elf_dynsymtab (abfd
) == i
)
1721 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1722 elf_elfsections (abfd
)[shindex
] = hdr
=
1723 &elf_tdata (abfd
)->dynstrtab_hdr
;
1724 /* We also treat this as a regular section, so
1725 that objcopy can handle it. */
1728 #if 0 /* Not handling other string tables specially right now. */
1729 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1730 /* We have a strtab for some random other section. */
1731 newsect
= (asection
*) hdr2
->bfd_section
;
1734 hdr
->bfd_section
= newsect
;
1735 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1737 elf_elfsections (abfd
)[shindex
] = hdr2
;
1743 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1747 /* *These* do a lot of work -- but build no sections! */
1749 asection
*target_sect
;
1750 Elf_Internal_Shdr
*hdr2
;
1751 unsigned int num_sec
= elf_numsections (abfd
);
1753 /* Check for a bogus link to avoid crashing. */
1754 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1755 || hdr
->sh_link
>= num_sec
)
1757 ((*_bfd_error_handler
)
1758 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1759 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1760 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1763 /* For some incomprehensible reason Oracle distributes
1764 libraries for Solaris in which some of the objects have
1765 bogus sh_link fields. It would be nice if we could just
1766 reject them, but, unfortunately, some people need to use
1767 them. We scan through the section headers; if we find only
1768 one suitable symbol table, we clobber the sh_link to point
1769 to it. I hope this doesn't break anything. */
1770 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1771 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1777 for (scan
= 1; scan
< num_sec
; scan
++)
1779 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1780 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1791 hdr
->sh_link
= found
;
1794 /* Get the symbol table. */
1795 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1796 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1799 /* If this reloc section does not use the main symbol table we
1800 don't treat it as a reloc section. BFD can't adequately
1801 represent such a section, so at least for now, we don't
1802 try. We just present it as a normal section. We also
1803 can't use it as a reloc section if it points to the null
1805 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1806 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1808 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1810 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1811 if (target_sect
== NULL
)
1814 if ((target_sect
->flags
& SEC_RELOC
) == 0
1815 || target_sect
->reloc_count
== 0)
1816 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1820 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1821 amt
= sizeof (*hdr2
);
1822 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1823 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1826 elf_elfsections (abfd
)[shindex
] = hdr2
;
1827 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1828 target_sect
->flags
|= SEC_RELOC
;
1829 target_sect
->relocation
= NULL
;
1830 target_sect
->rel_filepos
= hdr
->sh_offset
;
1831 /* In the section to which the relocations apply, mark whether
1832 its relocations are of the REL or RELA variety. */
1833 if (hdr
->sh_size
!= 0)
1834 elf_section_data (target_sect
)->use_rela_p
1835 = (hdr
->sh_type
== SHT_RELA
);
1836 abfd
->flags
|= HAS_RELOC
;
1841 case SHT_GNU_verdef
:
1842 elf_dynverdef (abfd
) = shindex
;
1843 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1844 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1847 case SHT_GNU_versym
:
1848 elf_dynversym (abfd
) = shindex
;
1849 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1850 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1853 case SHT_GNU_verneed
:
1854 elf_dynverref (abfd
) = shindex
;
1855 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1856 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1863 /* We need a BFD section for objcopy and relocatable linking,
1864 and it's handy to have the signature available as the section
1866 name
= group_signature (abfd
, hdr
);
1869 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1871 if (hdr
->contents
!= NULL
)
1873 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1874 unsigned int n_elt
= hdr
->sh_size
/ 4;
1877 if (idx
->flags
& GRP_COMDAT
)
1878 hdr
->bfd_section
->flags
1879 |= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
1881 while (--n_elt
!= 0)
1882 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1883 && elf_next_in_group (s
) != NULL
)
1885 elf_next_in_group (hdr
->bfd_section
) = s
;
1892 /* Check for any processor-specific section types. */
1894 if (bed
->elf_backend_section_from_shdr
)
1895 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1903 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1904 Return SEC for sections that have no elf section, and NULL on error. */
1907 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
1909 struct sym_sec_cache
*cache
;
1911 unsigned long r_symndx
;
1913 unsigned char esym_shndx
[4];
1914 unsigned int isym_shndx
;
1915 Elf_Internal_Shdr
*symtab_hdr
;
1918 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1920 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1921 return cache
->sec
[ent
];
1923 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1924 pos
= symtab_hdr
->sh_offset
;
1925 if (get_elf_backend_data (abfd
)->s
->sizeof_sym
1926 == sizeof (Elf64_External_Sym
))
1928 pos
+= r_symndx
* sizeof (Elf64_External_Sym
);
1929 pos
+= offsetof (Elf64_External_Sym
, st_shndx
);
1930 amt
= sizeof (((Elf64_External_Sym
*) 0)->st_shndx
);
1934 pos
+= r_symndx
* sizeof (Elf32_External_Sym
);
1935 pos
+= offsetof (Elf32_External_Sym
, st_shndx
);
1936 amt
= sizeof (((Elf32_External_Sym
*) 0)->st_shndx
);
1938 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1939 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1941 isym_shndx
= H_GET_16 (abfd
, esym_shndx
);
1943 if (isym_shndx
== SHN_XINDEX
)
1945 Elf_Internal_Shdr
*shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1946 if (shndx_hdr
->sh_size
!= 0)
1948 pos
= shndx_hdr
->sh_offset
;
1949 pos
+= r_symndx
* sizeof (Elf_External_Sym_Shndx
);
1950 amt
= sizeof (Elf_External_Sym_Shndx
);
1951 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1952 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1954 isym_shndx
= H_GET_32 (abfd
, esym_shndx
);
1958 if (cache
->abfd
!= abfd
)
1960 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1963 cache
->indx
[ent
] = r_symndx
;
1964 cache
->sec
[ent
] = sec
;
1965 if (isym_shndx
< SHN_LORESERVE
|| isym_shndx
> SHN_HIRESERVE
)
1968 s
= bfd_section_from_elf_index (abfd
, isym_shndx
);
1970 cache
->sec
[ent
] = s
;
1972 return cache
->sec
[ent
];
1975 /* Given an ELF section number, retrieve the corresponding BFD
1979 bfd_section_from_elf_index (abfd
, index
)
1983 if (index
>= elf_numsections (abfd
))
1985 return elf_elfsections (abfd
)[index
]->bfd_section
;
1989 _bfd_elf_new_section_hook (abfd
, sec
)
1993 struct bfd_elf_section_data
*sdata
;
1994 bfd_size_type amt
= sizeof (*sdata
);
1996 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1999 sec
->used_by_bfd
= (PTR
) sdata
;
2001 /* Indicate whether or not this section should use RELA relocations. */
2003 = get_elf_backend_data (abfd
)->default_use_rela_p
;
2008 /* Create a new bfd section from an ELF program header.
2010 Since program segments have no names, we generate a synthetic name
2011 of the form segment<NUM>, where NUM is generally the index in the
2012 program header table. For segments that are split (see below) we
2013 generate the names segment<NUM>a and segment<NUM>b.
2015 Note that some program segments may have a file size that is different than
2016 (less than) the memory size. All this means is that at execution the
2017 system must allocate the amount of memory specified by the memory size,
2018 but only initialize it with the first "file size" bytes read from the
2019 file. This would occur for example, with program segments consisting
2020 of combined data+bss.
2022 To handle the above situation, this routine generates TWO bfd sections
2023 for the single program segment. The first has the length specified by
2024 the file size of the segment, and the second has the length specified
2025 by the difference between the two sizes. In effect, the segment is split
2026 into it's initialized and uninitialized parts.
2031 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
2033 Elf_Internal_Phdr
*hdr
;
2035 const char *typename
;
2042 split
= ((hdr
->p_memsz
> 0)
2043 && (hdr
->p_filesz
> 0)
2044 && (hdr
->p_memsz
> hdr
->p_filesz
));
2045 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
2046 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2049 strcpy (name
, namebuf
);
2050 newsect
= bfd_make_section (abfd
, name
);
2051 if (newsect
== NULL
)
2053 newsect
->vma
= hdr
->p_vaddr
;
2054 newsect
->lma
= hdr
->p_paddr
;
2055 newsect
->_raw_size
= hdr
->p_filesz
;
2056 newsect
->filepos
= hdr
->p_offset
;
2057 newsect
->flags
|= SEC_HAS_CONTENTS
;
2058 if (hdr
->p_type
== PT_LOAD
)
2060 newsect
->flags
|= SEC_ALLOC
;
2061 newsect
->flags
|= SEC_LOAD
;
2062 if (hdr
->p_flags
& PF_X
)
2064 /* FIXME: all we known is that it has execute PERMISSION,
2066 newsect
->flags
|= SEC_CODE
;
2069 if (!(hdr
->p_flags
& PF_W
))
2071 newsect
->flags
|= SEC_READONLY
;
2076 sprintf (namebuf
, "%s%db", typename
, index
);
2077 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2080 strcpy (name
, namebuf
);
2081 newsect
= bfd_make_section (abfd
, name
);
2082 if (newsect
== NULL
)
2084 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2085 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2086 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2087 if (hdr
->p_type
== PT_LOAD
)
2089 newsect
->flags
|= SEC_ALLOC
;
2090 if (hdr
->p_flags
& PF_X
)
2091 newsect
->flags
|= SEC_CODE
;
2093 if (!(hdr
->p_flags
& PF_W
))
2094 newsect
->flags
|= SEC_READONLY
;
2101 bfd_section_from_phdr (abfd
, hdr
, index
)
2103 Elf_Internal_Phdr
*hdr
;
2106 struct elf_backend_data
*bed
;
2108 switch (hdr
->p_type
)
2111 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2114 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2117 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2120 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2123 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2125 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2130 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2133 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2136 /* Check for any processor-specific program segment types.
2137 If no handler for them, default to making "segment" sections. */
2138 bed
= get_elf_backend_data (abfd
);
2139 if (bed
->elf_backend_section_from_phdr
)
2140 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2142 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2146 /* Initialize REL_HDR, the section-header for new section, containing
2147 relocations against ASECT. If USE_RELA_P is true, we use RELA
2148 relocations; otherwise, we use REL relocations. */
2151 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2153 Elf_Internal_Shdr
*rel_hdr
;
2158 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2159 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2161 name
= bfd_alloc (abfd
, amt
);
2164 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2166 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2168 if (rel_hdr
->sh_name
== (unsigned int) -1)
2170 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2171 rel_hdr
->sh_entsize
= (use_rela_p
2172 ? bed
->s
->sizeof_rela
2173 : bed
->s
->sizeof_rel
);
2174 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2175 rel_hdr
->sh_flags
= 0;
2176 rel_hdr
->sh_addr
= 0;
2177 rel_hdr
->sh_size
= 0;
2178 rel_hdr
->sh_offset
= 0;
2183 /* Set up an ELF internal section header for a section. */
2186 elf_fake_sections (abfd
, asect
, failedptrarg
)
2191 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2192 boolean
*failedptr
= (boolean
*) failedptrarg
;
2193 Elf_Internal_Shdr
*this_hdr
;
2197 /* We already failed; just get out of the bfd_map_over_sections
2202 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2204 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2205 asect
->name
, false);
2206 if (this_hdr
->sh_name
== (unsigned long) -1)
2212 this_hdr
->sh_flags
= 0;
2214 if ((asect
->flags
& SEC_ALLOC
) != 0
2215 || asect
->user_set_vma
)
2216 this_hdr
->sh_addr
= asect
->vma
;
2218 this_hdr
->sh_addr
= 0;
2220 this_hdr
->sh_offset
= 0;
2221 this_hdr
->sh_size
= asect
->_raw_size
;
2222 this_hdr
->sh_link
= 0;
2223 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2224 /* The sh_entsize and sh_info fields may have been set already by
2225 copy_private_section_data. */
2227 this_hdr
->bfd_section
= asect
;
2228 this_hdr
->contents
= NULL
;
2230 /* FIXME: This should not be based on section names. */
2231 if (strcmp (asect
->name
, ".dynstr") == 0)
2232 this_hdr
->sh_type
= SHT_STRTAB
;
2233 else if (strcmp (asect
->name
, ".hash") == 0)
2235 this_hdr
->sh_type
= SHT_HASH
;
2236 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2238 else if (strcmp (asect
->name
, ".dynsym") == 0)
2240 this_hdr
->sh_type
= SHT_DYNSYM
;
2241 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2243 else if (strcmp (asect
->name
, ".dynamic") == 0)
2245 this_hdr
->sh_type
= SHT_DYNAMIC
;
2246 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2248 else if (strncmp (asect
->name
, ".rela", 5) == 0
2249 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2251 this_hdr
->sh_type
= SHT_RELA
;
2252 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2254 else if (strncmp (asect
->name
, ".rel", 4) == 0
2255 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2257 this_hdr
->sh_type
= SHT_REL
;
2258 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2260 else if (strcmp (asect
->name
, ".init_array") == 0)
2261 this_hdr
->sh_type
= SHT_INIT_ARRAY
;
2262 else if (strcmp (asect
->name
, ".fini_array") == 0)
2263 this_hdr
->sh_type
= SHT_FINI_ARRAY
;
2264 else if (strcmp (asect
->name
, ".preinit_array") == 0)
2265 this_hdr
->sh_type
= SHT_PREINIT_ARRAY
;
2266 else if (strncmp (asect
->name
, ".note", 5) == 0)
2267 this_hdr
->sh_type
= SHT_NOTE
;
2268 else if (strncmp (asect
->name
, ".stab", 5) == 0
2269 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2270 this_hdr
->sh_type
= SHT_STRTAB
;
2271 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2273 this_hdr
->sh_type
= SHT_GNU_versym
;
2274 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2276 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2278 this_hdr
->sh_type
= SHT_GNU_verdef
;
2279 this_hdr
->sh_entsize
= 0;
2280 /* objcopy or strip will copy over sh_info, but may not set
2281 cverdefs. The linker will set cverdefs, but sh_info will be
2283 if (this_hdr
->sh_info
== 0)
2284 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2286 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2287 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2289 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2291 this_hdr
->sh_type
= SHT_GNU_verneed
;
2292 this_hdr
->sh_entsize
= 0;
2293 /* objcopy or strip will copy over sh_info, but may not set
2294 cverrefs. The linker will set cverrefs, but sh_info will be
2296 if (this_hdr
->sh_info
== 0)
2297 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2299 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2300 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2302 else if ((asect
->flags
& SEC_GROUP
) != 0)
2304 this_hdr
->sh_type
= SHT_GROUP
;
2305 this_hdr
->sh_entsize
= 4;
2307 else if ((asect
->flags
& SEC_ALLOC
) != 0
2308 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2309 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2310 this_hdr
->sh_type
= SHT_NOBITS
;
2312 this_hdr
->sh_type
= SHT_PROGBITS
;
2314 if ((asect
->flags
& SEC_ALLOC
) != 0)
2315 this_hdr
->sh_flags
|= SHF_ALLOC
;
2316 if ((asect
->flags
& SEC_READONLY
) == 0)
2317 this_hdr
->sh_flags
|= SHF_WRITE
;
2318 if ((asect
->flags
& SEC_CODE
) != 0)
2319 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2320 if ((asect
->flags
& SEC_MERGE
) != 0)
2322 this_hdr
->sh_flags
|= SHF_MERGE
;
2323 this_hdr
->sh_entsize
= asect
->entsize
;
2324 if ((asect
->flags
& SEC_STRINGS
) != 0)
2325 this_hdr
->sh_flags
|= SHF_STRINGS
;
2327 if (elf_group_name (asect
) != NULL
)
2328 this_hdr
->sh_flags
|= SHF_GROUP
;
2329 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2330 this_hdr
->sh_flags
|= SHF_TLS
;
2332 /* Check for processor-specific section types. */
2333 if (bed
->elf_backend_fake_sections
2334 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2337 /* If the section has relocs, set up a section header for the
2338 SHT_REL[A] section. If two relocation sections are required for
2339 this section, it is up to the processor-specific back-end to
2340 create the other. */
2341 if ((asect
->flags
& SEC_RELOC
) != 0
2342 && !_bfd_elf_init_reloc_shdr (abfd
,
2343 &elf_section_data (asect
)->rel_hdr
,
2345 elf_section_data (asect
)->use_rela_p
))
2349 /* Fill in the contents of a SHT_GROUP section. */
2352 set_group_contents (abfd
, sec
, failedptrarg
)
2355 PTR failedptrarg ATTRIBUTE_UNUSED
;
2357 boolean
*failedptr
= (boolean
*) failedptrarg
;
2358 unsigned long symindx
;
2361 struct bfd_link_order
*l
;
2363 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2367 /* If called from the assembler, swap_out_syms will have set up
2368 elf_section_syms; If called for "ld -r", the symbols won't yet
2369 be mapped, so emulate elf_bfd_final_link. */
2370 if (elf_section_syms (abfd
) != NULL
)
2371 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2373 symindx
= elf_section_data (sec
)->this_idx
;
2374 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2376 /* Nor will the contents be allocated for "ld -r". */
2377 if (sec
->contents
== NULL
)
2379 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2380 if (sec
->contents
== NULL
)
2387 loc
= sec
->contents
+ sec
->_raw_size
;
2389 /* Get the pointer to the first section in the group that we
2390 squirreled away here. */
2391 elt
= elf_next_in_group (sec
);
2393 /* First element is a flag word. Rest of section is elf section
2394 indices for all the sections of the group. Write them backwards
2395 just to keep the group in the same order as given in .section
2396 directives, not that it matters. */
2400 H_PUT_32 (abfd
, elf_section_data (elt
)->this_idx
, loc
);
2401 elt
= elf_next_in_group (elt
);
2404 /* If this is a relocatable link, then the above did nothing because
2405 SEC is the output section. Look through the input sections
2407 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2408 if (l
->type
== bfd_indirect_link_order
2409 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2414 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2415 elt
= elf_next_in_group (elt
);
2416 /* During a relocatable link, the lists are circular. */
2418 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2421 H_PUT_32 (abfd
, sec
->flags
& SEC_LINK_ONCE
? GRP_COMDAT
: 0, loc
);
2423 BFD_ASSERT (loc
== sec
->contents
);
2426 /* Assign all ELF section numbers. The dummy first section is handled here
2427 too. The link/info pointers for the standard section types are filled
2428 in here too, while we're at it. */
2431 assign_section_numbers (abfd
)
2434 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2436 unsigned int section_number
, secn
;
2437 Elf_Internal_Shdr
**i_shdrp
;
2442 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2444 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2446 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2448 if (section_number
== SHN_LORESERVE
)
2449 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2450 d
->this_idx
= section_number
++;
2451 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2452 if ((sec
->flags
& SEC_RELOC
) == 0)
2456 if (section_number
== SHN_LORESERVE
)
2457 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2458 d
->rel_idx
= section_number
++;
2459 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2464 if (section_number
== SHN_LORESERVE
)
2465 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2466 d
->rel_idx2
= section_number
++;
2467 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2473 if (section_number
== SHN_LORESERVE
)
2474 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2475 t
->shstrtab_section
= section_number
++;
2476 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2477 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2479 if (bfd_get_symcount (abfd
) > 0)
2481 if (section_number
== SHN_LORESERVE
)
2482 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2483 t
->symtab_section
= section_number
++;
2484 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2485 if (section_number
> SHN_LORESERVE
- 2)
2487 if (section_number
== SHN_LORESERVE
)
2488 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2489 t
->symtab_shndx_section
= section_number
++;
2490 t
->symtab_shndx_hdr
.sh_name
2491 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2492 ".symtab_shndx", false);
2493 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2496 if (section_number
== SHN_LORESERVE
)
2497 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2498 t
->strtab_section
= section_number
++;
2499 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2502 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2503 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2505 elf_numsections (abfd
) = section_number
;
2506 elf_elfheader (abfd
)->e_shnum
= section_number
;
2507 if (section_number
> SHN_LORESERVE
)
2508 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2510 /* Set up the list of section header pointers, in agreement with the
2512 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2513 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2514 if (i_shdrp
== NULL
)
2517 amt
= sizeof (Elf_Internal_Shdr
);
2518 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2519 if (i_shdrp
[0] == NULL
)
2521 bfd_release (abfd
, i_shdrp
);
2524 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2526 elf_elfsections (abfd
) = i_shdrp
;
2528 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2529 if (bfd_get_symcount (abfd
) > 0)
2531 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2532 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2534 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2535 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2537 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2538 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2540 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2542 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2546 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2547 if (d
->rel_idx
!= 0)
2548 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2549 if (d
->rel_idx2
!= 0)
2550 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2552 /* Fill in the sh_link and sh_info fields while we're at it. */
2554 /* sh_link of a reloc section is the section index of the symbol
2555 table. sh_info is the section index of the section to which
2556 the relocation entries apply. */
2557 if (d
->rel_idx
!= 0)
2559 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2560 d
->rel_hdr
.sh_info
= d
->this_idx
;
2562 if (d
->rel_idx2
!= 0)
2564 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2565 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2568 switch (d
->this_hdr
.sh_type
)
2572 /* A reloc section which we are treating as a normal BFD
2573 section. sh_link is the section index of the symbol
2574 table. sh_info is the section index of the section to
2575 which the relocation entries apply. We assume that an
2576 allocated reloc section uses the dynamic symbol table.
2577 FIXME: How can we be sure? */
2578 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2580 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2582 /* We look up the section the relocs apply to by name. */
2584 if (d
->this_hdr
.sh_type
== SHT_REL
)
2588 s
= bfd_get_section_by_name (abfd
, name
);
2590 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2594 /* We assume that a section named .stab*str is a stabs
2595 string section. We look for a section with the same name
2596 but without the trailing ``str'', and set its sh_link
2597 field to point to this section. */
2598 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2599 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2604 len
= strlen (sec
->name
);
2605 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2608 strncpy (alc
, sec
->name
, len
- 3);
2609 alc
[len
- 3] = '\0';
2610 s
= bfd_get_section_by_name (abfd
, alc
);
2614 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2616 /* This is a .stab section. */
2617 elf_section_data (s
)->this_hdr
.sh_entsize
=
2618 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2625 case SHT_GNU_verneed
:
2626 case SHT_GNU_verdef
:
2627 /* sh_link is the section header index of the string table
2628 used for the dynamic entries, or the symbol table, or the
2630 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2632 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2636 case SHT_GNU_versym
:
2637 /* sh_link is the section header index of the symbol table
2638 this hash table or version table is for. */
2639 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2641 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2645 d
->this_hdr
.sh_link
= t
->symtab_section
;
2649 for (secn
= 1; secn
< section_number
; ++secn
)
2650 if (i_shdrp
[secn
] == NULL
)
2651 i_shdrp
[secn
] = i_shdrp
[0];
2653 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2654 i_shdrp
[secn
]->sh_name
);
2658 /* Map symbol from it's internal number to the external number, moving
2659 all local symbols to be at the head of the list. */
2662 sym_is_global (abfd
, sym
)
2666 /* If the backend has a special mapping, use it. */
2667 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2668 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2671 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2672 || bfd_is_und_section (bfd_get_section (sym
))
2673 || bfd_is_com_section (bfd_get_section (sym
)));
2677 elf_map_symbols (abfd
)
2680 unsigned int symcount
= bfd_get_symcount (abfd
);
2681 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2682 asymbol
**sect_syms
;
2683 unsigned int num_locals
= 0;
2684 unsigned int num_globals
= 0;
2685 unsigned int num_locals2
= 0;
2686 unsigned int num_globals2
= 0;
2694 fprintf (stderr
, "elf_map_symbols\n");
2698 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2700 if (max_index
< asect
->index
)
2701 max_index
= asect
->index
;
2705 amt
= max_index
* sizeof (asymbol
*);
2706 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2707 if (sect_syms
== NULL
)
2709 elf_section_syms (abfd
) = sect_syms
;
2710 elf_num_section_syms (abfd
) = max_index
;
2712 /* Init sect_syms entries for any section symbols we have already
2713 decided to output. */
2714 for (idx
= 0; idx
< symcount
; idx
++)
2716 asymbol
*sym
= syms
[idx
];
2718 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2725 if (sec
->owner
!= NULL
)
2727 if (sec
->owner
!= abfd
)
2729 if (sec
->output_offset
!= 0)
2732 sec
= sec
->output_section
;
2734 /* Empty sections in the input files may have had a
2735 section symbol created for them. (See the comment
2736 near the end of _bfd_generic_link_output_symbols in
2737 linker.c). If the linker script discards such
2738 sections then we will reach this point. Since we know
2739 that we cannot avoid this case, we detect it and skip
2740 the abort and the assignment to the sect_syms array.
2741 To reproduce this particular case try running the
2742 linker testsuite test ld-scripts/weak.exp for an ELF
2743 port that uses the generic linker. */
2744 if (sec
->owner
== NULL
)
2747 BFD_ASSERT (sec
->owner
== abfd
);
2749 sect_syms
[sec
->index
] = syms
[idx
];
2754 /* Classify all of the symbols. */
2755 for (idx
= 0; idx
< symcount
; idx
++)
2757 if (!sym_is_global (abfd
, syms
[idx
]))
2763 /* We will be adding a section symbol for each BFD section. Most normal
2764 sections will already have a section symbol in outsymbols, but
2765 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2766 at least in that case. */
2767 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2769 if (sect_syms
[asect
->index
] == NULL
)
2771 if (!sym_is_global (abfd
, asect
->symbol
))
2778 /* Now sort the symbols so the local symbols are first. */
2779 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2780 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2782 if (new_syms
== NULL
)
2785 for (idx
= 0; idx
< symcount
; idx
++)
2787 asymbol
*sym
= syms
[idx
];
2790 if (!sym_is_global (abfd
, sym
))
2793 i
= num_locals
+ num_globals2
++;
2795 sym
->udata
.i
= i
+ 1;
2797 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2799 if (sect_syms
[asect
->index
] == NULL
)
2801 asymbol
*sym
= asect
->symbol
;
2804 sect_syms
[asect
->index
] = sym
;
2805 if (!sym_is_global (abfd
, sym
))
2808 i
= num_locals
+ num_globals2
++;
2810 sym
->udata
.i
= i
+ 1;
2814 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2816 elf_num_locals (abfd
) = num_locals
;
2817 elf_num_globals (abfd
) = num_globals
;
2821 /* Align to the maximum file alignment that could be required for any
2822 ELF data structure. */
2824 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2825 static INLINE file_ptr
2826 align_file_position (off
, align
)
2830 return (off
+ align
- 1) & ~(align
- 1);
2833 /* Assign a file position to a section, optionally aligning to the
2834 required section alignment. */
2837 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2838 Elf_Internal_Shdr
*i_shdrp
;
2846 al
= i_shdrp
->sh_addralign
;
2848 offset
= BFD_ALIGN (offset
, al
);
2850 i_shdrp
->sh_offset
= offset
;
2851 if (i_shdrp
->bfd_section
!= NULL
)
2852 i_shdrp
->bfd_section
->filepos
= offset
;
2853 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2854 offset
+= i_shdrp
->sh_size
;
2858 /* Compute the file positions we are going to put the sections at, and
2859 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2860 is not NULL, this is being called by the ELF backend linker. */
2863 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2865 struct bfd_link_info
*link_info
;
2867 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2869 struct bfd_strtab_hash
*strtab
;
2870 Elf_Internal_Shdr
*shstrtab_hdr
;
2872 if (abfd
->output_has_begun
)
2875 /* Do any elf backend specific processing first. */
2876 if (bed
->elf_backend_begin_write_processing
)
2877 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2879 if (! prep_headers (abfd
))
2882 /* Post process the headers if necessary. */
2883 if (bed
->elf_backend_post_process_headers
)
2884 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2887 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2891 if (!assign_section_numbers (abfd
))
2894 /* The backend linker builds symbol table information itself. */
2895 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2897 /* Non-zero if doing a relocatable link. */
2898 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2900 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2904 if (link_info
== NULL
|| link_info
->relocateable
)
2906 bfd_map_over_sections (abfd
, set_group_contents
, &failed
);
2911 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2912 /* sh_name was set in prep_headers. */
2913 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2914 shstrtab_hdr
->sh_flags
= 0;
2915 shstrtab_hdr
->sh_addr
= 0;
2916 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2917 shstrtab_hdr
->sh_entsize
= 0;
2918 shstrtab_hdr
->sh_link
= 0;
2919 shstrtab_hdr
->sh_info
= 0;
2920 /* sh_offset is set in assign_file_positions_except_relocs. */
2921 shstrtab_hdr
->sh_addralign
= 1;
2923 if (!assign_file_positions_except_relocs (abfd
))
2926 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2929 Elf_Internal_Shdr
*hdr
;
2931 off
= elf_tdata (abfd
)->next_file_pos
;
2933 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2934 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2936 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2937 if (hdr
->sh_size
!= 0)
2938 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2940 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2941 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2943 elf_tdata (abfd
)->next_file_pos
= off
;
2945 /* Now that we know where the .strtab section goes, write it
2947 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2948 || ! _bfd_stringtab_emit (abfd
, strtab
))
2950 _bfd_stringtab_free (strtab
);
2953 abfd
->output_has_begun
= true;
2958 /* Create a mapping from a set of sections to a program segment. */
2960 static INLINE
struct elf_segment_map
*
2961 make_mapping (abfd
, sections
, from
, to
, phdr
)
2963 asection
**sections
;
2968 struct elf_segment_map
*m
;
2973 amt
= sizeof (struct elf_segment_map
);
2974 amt
+= (to
- from
- 1) * sizeof (asection
*);
2975 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2979 m
->p_type
= PT_LOAD
;
2980 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2981 m
->sections
[i
- from
] = *hdrpp
;
2982 m
->count
= to
- from
;
2984 if (from
== 0 && phdr
)
2986 /* Include the headers in the first PT_LOAD segment. */
2987 m
->includes_filehdr
= 1;
2988 m
->includes_phdrs
= 1;
2994 /* Set up a mapping from BFD sections to program segments. */
2997 map_sections_to_segments (abfd
)
3000 asection
**sections
= NULL
;
3004 struct elf_segment_map
*mfirst
;
3005 struct elf_segment_map
**pm
;
3006 struct elf_segment_map
*m
;
3008 unsigned int phdr_index
;
3009 bfd_vma maxpagesize
;
3011 boolean phdr_in_segment
= true;
3014 asection
*first_tls
= NULL
;
3015 asection
*dynsec
, *eh_frame_hdr
;
3018 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3021 if (bfd_count_sections (abfd
) == 0)
3024 /* Select the allocated sections, and sort them. */
3026 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
3027 sections
= (asection
**) bfd_malloc (amt
);
3028 if (sections
== NULL
)
3032 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3034 if ((s
->flags
& SEC_ALLOC
) != 0)
3040 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
3043 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
3045 /* Build the mapping. */
3050 /* If we have a .interp section, then create a PT_PHDR segment for
3051 the program headers and a PT_INTERP segment for the .interp
3053 s
= bfd_get_section_by_name (abfd
, ".interp");
3054 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3056 amt
= sizeof (struct elf_segment_map
);
3057 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3061 m
->p_type
= PT_PHDR
;
3062 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3063 m
->p_flags
= PF_R
| PF_X
;
3064 m
->p_flags_valid
= 1;
3065 m
->includes_phdrs
= 1;
3070 amt
= sizeof (struct elf_segment_map
);
3071 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3075 m
->p_type
= PT_INTERP
;
3083 /* Look through the sections. We put sections in the same program
3084 segment when the start of the second section can be placed within
3085 a few bytes of the end of the first section. */
3088 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3090 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3092 && (dynsec
->flags
& SEC_LOAD
) == 0)
3095 /* Deal with -Ttext or something similar such that the first section
3096 is not adjacent to the program headers. This is an
3097 approximation, since at this point we don't know exactly how many
3098 program headers we will need. */
3101 bfd_size_type phdr_size
;
3103 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3105 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3106 if ((abfd
->flags
& D_PAGED
) == 0
3107 || sections
[0]->lma
< phdr_size
3108 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3109 phdr_in_segment
= false;
3112 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3115 boolean new_segment
;
3119 /* See if this section and the last one will fit in the same
3122 if (last_hdr
== NULL
)
3124 /* If we don't have a segment yet, then we don't need a new
3125 one (we build the last one after this loop). */
3126 new_segment
= false;
3128 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3130 /* If this section has a different relation between the
3131 virtual address and the load address, then we need a new
3135 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3136 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3138 /* If putting this section in this segment would force us to
3139 skip a page in the segment, then we need a new segment. */
3142 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3143 && (hdr
->flags
& SEC_LOAD
) != 0)
3145 /* We don't want to put a loadable section after a
3146 nonloadable section in the same segment. */
3149 else if ((abfd
->flags
& D_PAGED
) == 0)
3151 /* If the file is not demand paged, which means that we
3152 don't require the sections to be correctly aligned in the
3153 file, then there is no other reason for a new segment. */
3154 new_segment
= false;
3157 && (hdr
->flags
& SEC_READONLY
) == 0
3158 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3161 /* We don't want to put a writable section in a read only
3162 segment, unless they are on the same page in memory
3163 anyhow. We already know that the last section does not
3164 bring us past the current section on the page, so the
3165 only case in which the new section is not on the same
3166 page as the previous section is when the previous section
3167 ends precisely on a page boundary. */
3172 /* Otherwise, we can use the same segment. */
3173 new_segment
= false;
3178 if ((hdr
->flags
& SEC_READONLY
) == 0)
3184 /* We need a new program segment. We must create a new program
3185 header holding all the sections from phdr_index until hdr. */
3187 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3194 if ((hdr
->flags
& SEC_READONLY
) == 0)
3201 phdr_in_segment
= false;
3204 /* Create a final PT_LOAD program segment. */
3205 if (last_hdr
!= NULL
)
3207 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3215 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3218 amt
= sizeof (struct elf_segment_map
);
3219 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3223 m
->p_type
= PT_DYNAMIC
;
3225 m
->sections
[0] = dynsec
;
3231 /* For each loadable .note section, add a PT_NOTE segment. We don't
3232 use bfd_get_section_by_name, because if we link together
3233 nonloadable .note sections and loadable .note sections, we will
3234 generate two .note sections in the output file. FIXME: Using
3235 names for section types is bogus anyhow. */
3236 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3238 if ((s
->flags
& SEC_LOAD
) != 0
3239 && strncmp (s
->name
, ".note", 5) == 0)
3241 amt
= sizeof (struct elf_segment_map
);
3242 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3246 m
->p_type
= PT_NOTE
;
3253 if (s
->flags
& SEC_THREAD_LOCAL
)
3261 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3266 amt
= sizeof (struct elf_segment_map
);
3267 amt
+= (tls_count
- 1) * sizeof (asection
*);
3268 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3273 m
->count
= tls_count
;
3274 /* Mandated PF_R. */
3276 m
->p_flags_valid
= 1;
3277 for (i
= 0; i
< tls_count
; ++i
)
3279 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3280 m
->sections
[i
] = first_tls
;
3281 first_tls
= first_tls
->next
;
3288 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3290 eh_frame_hdr
= NULL
;
3291 if (elf_tdata (abfd
)->eh_frame_hdr
)
3292 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3293 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3295 amt
= sizeof (struct elf_segment_map
);
3296 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3300 m
->p_type
= PT_GNU_EH_FRAME
;
3302 m
->sections
[0] = eh_frame_hdr
;
3311 elf_tdata (abfd
)->segment_map
= mfirst
;
3315 if (sections
!= NULL
)
3320 /* Sort sections by address. */
3323 elf_sort_sections (arg1
, arg2
)
3327 const asection
*sec1
= *(const asection
**) arg1
;
3328 const asection
*sec2
= *(const asection
**) arg2
;
3330 /* Sort by LMA first, since this is the address used to
3331 place the section into a segment. */
3332 if (sec1
->lma
< sec2
->lma
)
3334 else if (sec1
->lma
> sec2
->lma
)
3337 /* Then sort by VMA. Normally the LMA and the VMA will be
3338 the same, and this will do nothing. */
3339 if (sec1
->vma
< sec2
->vma
)
3341 else if (sec1
->vma
> sec2
->vma
)
3344 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3346 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3352 /* If the indicies are the same, do not return 0
3353 here, but continue to try the next comparison. */
3354 if (sec1
->target_index
- sec2
->target_index
!= 0)
3355 return sec1
->target_index
- sec2
->target_index
;
3360 else if (TOEND (sec2
))
3365 /* Sort by size, to put zero sized sections
3366 before others at the same address. */
3368 if (sec1
->_raw_size
< sec2
->_raw_size
)
3370 if (sec1
->_raw_size
> sec2
->_raw_size
)
3373 return sec1
->target_index
- sec2
->target_index
;
3376 /* Assign file positions to the sections based on the mapping from
3377 sections to segments. This function also sets up some fields in
3378 the file header, and writes out the program headers. */
3381 assign_file_positions_for_segments (abfd
)
3384 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3386 struct elf_segment_map
*m
;
3388 Elf_Internal_Phdr
*phdrs
;
3390 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3391 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3392 Elf_Internal_Phdr
*p
;
3395 if (elf_tdata (abfd
)->segment_map
== NULL
)
3397 if (! map_sections_to_segments (abfd
))
3401 if (bed
->elf_backend_modify_segment_map
)
3403 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3408 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3411 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3412 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3413 elf_elfheader (abfd
)->e_phnum
= count
;
3418 /* If we already counted the number of program segments, make sure
3419 that we allocated enough space. This happens when SIZEOF_HEADERS
3420 is used in a linker script. */
3421 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3422 if (alloc
!= 0 && count
> alloc
)
3424 ((*_bfd_error_handler
)
3425 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3426 bfd_get_filename (abfd
), alloc
, count
));
3427 bfd_set_error (bfd_error_bad_value
);
3434 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3435 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3439 off
= bed
->s
->sizeof_ehdr
;
3440 off
+= alloc
* bed
->s
->sizeof_phdr
;
3447 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3454 /* If elf_segment_map is not from map_sections_to_segments, the
3455 sections may not be correctly ordered. NOTE: sorting should
3456 not be done to the PT_NOTE section of a corefile, which may
3457 contain several pseudo-sections artificially created by bfd.
3458 Sorting these pseudo-sections breaks things badly. */
3460 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3461 && m
->p_type
== PT_NOTE
))
3462 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3465 p
->p_type
= m
->p_type
;
3466 p
->p_flags
= m
->p_flags
;
3468 if (p
->p_type
== PT_LOAD
3470 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3472 if ((abfd
->flags
& D_PAGED
) != 0)
3473 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3476 bfd_size_type align
;
3479 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3481 bfd_size_type secalign
;
3483 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3484 if (secalign
> align
)
3488 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3495 p
->p_vaddr
= m
->sections
[0]->vma
;
3497 if (m
->p_paddr_valid
)
3498 p
->p_paddr
= m
->p_paddr
;
3499 else if (m
->count
== 0)
3502 p
->p_paddr
= m
->sections
[0]->lma
;
3504 if (p
->p_type
== PT_LOAD
3505 && (abfd
->flags
& D_PAGED
) != 0)
3506 p
->p_align
= bed
->maxpagesize
;
3507 else if (m
->count
== 0)
3508 p
->p_align
= bed
->s
->file_align
;
3516 if (m
->includes_filehdr
)
3518 if (! m
->p_flags_valid
)
3521 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3522 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3525 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3527 if (p
->p_vaddr
< (bfd_vma
) off
)
3529 (*_bfd_error_handler
)
3530 (_("%s: Not enough room for program headers, try linking with -N"),
3531 bfd_get_filename (abfd
));
3532 bfd_set_error (bfd_error_bad_value
);
3537 if (! m
->p_paddr_valid
)
3540 if (p
->p_type
== PT_LOAD
)
3542 filehdr_vaddr
= p
->p_vaddr
;
3543 filehdr_paddr
= p
->p_paddr
;
3547 if (m
->includes_phdrs
)
3549 if (! m
->p_flags_valid
)
3552 if (m
->includes_filehdr
)
3554 if (p
->p_type
== PT_LOAD
)
3556 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3557 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3562 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3566 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3567 p
->p_vaddr
-= off
- p
->p_offset
;
3568 if (! m
->p_paddr_valid
)
3569 p
->p_paddr
-= off
- p
->p_offset
;
3572 if (p
->p_type
== PT_LOAD
)
3574 phdrs_vaddr
= p
->p_vaddr
;
3575 phdrs_paddr
= p
->p_paddr
;
3578 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3581 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3582 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3585 if (p
->p_type
== PT_LOAD
3586 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3588 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3594 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3595 p
->p_filesz
+= adjust
;
3596 p
->p_memsz
+= adjust
;
3602 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3606 bfd_size_type align
;
3610 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3612 /* The section may have artificial alignment forced by a
3613 link script. Notice this case by the gap between the
3614 cumulative phdr lma and the section's lma. */
3615 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3617 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3619 p
->p_memsz
+= adjust
;
3622 if ((flags
& SEC_LOAD
) != 0)
3623 p
->p_filesz
+= adjust
;
3626 if (p
->p_type
== PT_LOAD
)
3628 bfd_signed_vma adjust
;
3630 if ((flags
& SEC_LOAD
) != 0)
3632 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3636 else if ((flags
& SEC_ALLOC
) != 0)
3638 /* The section VMA must equal the file position
3639 modulo the page size. FIXME: I'm not sure if
3640 this adjustment is really necessary. We used to
3641 not have the SEC_LOAD case just above, and then
3642 this was necessary, but now I'm not sure. */
3643 if ((abfd
->flags
& D_PAGED
) != 0)
3644 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3646 adjust
= (sec
->vma
- voff
) % align
;
3655 (* _bfd_error_handler
) (_("\
3656 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3657 bfd_section_name (abfd
, sec
),
3662 p
->p_memsz
+= adjust
;
3665 if ((flags
& SEC_LOAD
) != 0)
3666 p
->p_filesz
+= adjust
;
3671 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3672 used in a linker script we may have a section with
3673 SEC_LOAD clear but which is supposed to have
3675 if ((flags
& SEC_LOAD
) != 0
3676 || (flags
& SEC_HAS_CONTENTS
) != 0)
3677 off
+= sec
->_raw_size
;
3679 if ((flags
& SEC_ALLOC
) != 0)
3680 voff
+= sec
->_raw_size
;
3683 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3685 /* The actual "note" segment has i == 0.
3686 This is the one that actually contains everything. */
3690 p
->p_filesz
= sec
->_raw_size
;
3691 off
+= sec
->_raw_size
;
3696 /* Fake sections -- don't need to be written. */
3699 flags
= sec
->flags
= 0;
3706 p
->p_memsz
+= sec
->_raw_size
;
3708 if ((flags
& SEC_LOAD
) != 0)
3709 p
->p_filesz
+= sec
->_raw_size
;
3711 if (p
->p_type
== PT_TLS
3712 && sec
->_raw_size
== 0
3713 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3715 struct bfd_link_order
*o
;
3716 bfd_vma tbss_size
= 0;
3718 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3719 if (tbss_size
< o
->offset
+ o
->size
)
3720 tbss_size
= o
->offset
+ o
->size
;
3722 p
->p_memsz
+= tbss_size
;
3725 if (align
> p
->p_align
3726 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3730 if (! m
->p_flags_valid
)
3733 if ((flags
& SEC_CODE
) != 0)
3735 if ((flags
& SEC_READONLY
) == 0)
3741 /* Now that we have set the section file positions, we can set up
3742 the file positions for the non PT_LOAD segments. */
3743 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3747 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3749 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3750 p
->p_offset
= m
->sections
[0]->filepos
;
3754 if (m
->includes_filehdr
)
3756 p
->p_vaddr
= filehdr_vaddr
;
3757 if (! m
->p_paddr_valid
)
3758 p
->p_paddr
= filehdr_paddr
;
3760 else if (m
->includes_phdrs
)
3762 p
->p_vaddr
= phdrs_vaddr
;
3763 if (! m
->p_paddr_valid
)
3764 p
->p_paddr
= phdrs_paddr
;
3769 /* If additional nonloadable filepos adjustments are required,
3771 if (bed
->set_nonloadable_filepos
)
3772 (*bed
->set_nonloadable_filepos
) (abfd
, phdrs
);
3774 /* Clear out any program headers we allocated but did not use. */
3775 for (; count
< alloc
; count
++, p
++)
3777 memset (p
, 0, sizeof *p
);
3778 p
->p_type
= PT_NULL
;
3781 elf_tdata (abfd
)->phdr
= phdrs
;
3783 elf_tdata (abfd
)->next_file_pos
= off
;
3785 /* Write out the program headers. */
3786 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3787 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3793 /* Get the size of the program header.
3795 If this is called by the linker before any of the section VMA's are set, it
3796 can't calculate the correct value for a strange memory layout. This only
3797 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3798 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3799 data segment (exclusive of .interp and .dynamic).
3801 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3802 will be two segments. */
3804 static bfd_size_type
3805 get_program_header_size (abfd
)
3810 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3812 /* We can't return a different result each time we're called. */
3813 if (elf_tdata (abfd
)->program_header_size
!= 0)
3814 return elf_tdata (abfd
)->program_header_size
;
3816 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3818 struct elf_segment_map
*m
;
3821 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3823 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3824 return elf_tdata (abfd
)->program_header_size
;
3827 /* Assume we will need exactly two PT_LOAD segments: one for text
3828 and one for data. */
3831 s
= bfd_get_section_by_name (abfd
, ".interp");
3832 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3834 /* If we have a loadable interpreter section, we need a
3835 PT_INTERP segment. In this case, assume we also need a
3836 PT_PHDR segment, although that may not be true for all
3841 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3843 /* We need a PT_DYNAMIC segment. */
3847 if (elf_tdata (abfd
)->eh_frame_hdr
3848 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
3850 /* We need a PT_GNU_EH_FRAME segment. */
3854 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3856 if ((s
->flags
& SEC_LOAD
) != 0
3857 && strncmp (s
->name
, ".note", 5) == 0)
3859 /* We need a PT_NOTE segment. */
3864 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3866 if (s
->flags
& SEC_THREAD_LOCAL
)
3868 /* We need a PT_TLS segment. */
3874 /* Let the backend count up any program headers it might need. */
3875 if (bed
->elf_backend_additional_program_headers
)
3879 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3885 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3886 return elf_tdata (abfd
)->program_header_size
;
3889 /* Work out the file positions of all the sections. This is called by
3890 _bfd_elf_compute_section_file_positions. All the section sizes and
3891 VMAs must be known before this is called.
3893 We do not consider reloc sections at this point, unless they form
3894 part of the loadable image. Reloc sections are assigned file
3895 positions in assign_file_positions_for_relocs, which is called by
3896 write_object_contents and final_link.
3898 We also don't set the positions of the .symtab and .strtab here. */
3901 assign_file_positions_except_relocs (abfd
)
3904 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3905 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3906 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3907 unsigned int num_sec
= elf_numsections (abfd
);
3909 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3911 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3912 && bfd_get_format (abfd
) != bfd_core
)
3914 Elf_Internal_Shdr
**hdrpp
;
3917 /* Start after the ELF header. */
3918 off
= i_ehdrp
->e_ehsize
;
3920 /* We are not creating an executable, which means that we are
3921 not creating a program header, and that the actual order of
3922 the sections in the file is unimportant. */
3923 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3925 Elf_Internal_Shdr
*hdr
;
3928 if (hdr
->sh_type
== SHT_REL
3929 || hdr
->sh_type
== SHT_RELA
3930 || i
== tdata
->symtab_section
3931 || i
== tdata
->symtab_shndx_section
3932 || i
== tdata
->strtab_section
)
3934 hdr
->sh_offset
= -1;
3937 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3939 if (i
== SHN_LORESERVE
- 1)
3941 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3942 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3949 Elf_Internal_Shdr
**hdrpp
;
3951 /* Assign file positions for the loaded sections based on the
3952 assignment of sections to segments. */
3953 if (! assign_file_positions_for_segments (abfd
))
3956 /* Assign file positions for the other sections. */
3958 off
= elf_tdata (abfd
)->next_file_pos
;
3959 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3961 Elf_Internal_Shdr
*hdr
;
3964 if (hdr
->bfd_section
!= NULL
3965 && hdr
->bfd_section
->filepos
!= 0)
3966 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3967 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3969 ((*_bfd_error_handler
)
3970 (_("%s: warning: allocated section `%s' not in segment"),
3971 bfd_get_filename (abfd
),
3972 (hdr
->bfd_section
== NULL
3974 : hdr
->bfd_section
->name
)));
3975 if ((abfd
->flags
& D_PAGED
) != 0)
3976 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3978 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3979 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3982 else if (hdr
->sh_type
== SHT_REL
3983 || hdr
->sh_type
== SHT_RELA
3984 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3985 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
3986 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3987 hdr
->sh_offset
= -1;
3989 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3991 if (i
== SHN_LORESERVE
- 1)
3993 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3994 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3999 /* Place the section headers. */
4000 off
= align_file_position (off
, bed
->s
->file_align
);
4001 i_ehdrp
->e_shoff
= off
;
4002 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
4004 elf_tdata (abfd
)->next_file_pos
= off
;
4013 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
4014 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
4015 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
4016 struct elf_strtab_hash
*shstrtab
;
4017 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4019 i_ehdrp
= elf_elfheader (abfd
);
4020 i_shdrp
= elf_elfsections (abfd
);
4022 shstrtab
= _bfd_elf_strtab_init ();
4023 if (shstrtab
== NULL
)
4026 elf_shstrtab (abfd
) = shstrtab
;
4028 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
4029 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
4030 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
4031 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
4033 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
4034 i_ehdrp
->e_ident
[EI_DATA
] =
4035 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
4036 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
4038 if ((abfd
->flags
& DYNAMIC
) != 0)
4039 i_ehdrp
->e_type
= ET_DYN
;
4040 else if ((abfd
->flags
& EXEC_P
) != 0)
4041 i_ehdrp
->e_type
= ET_EXEC
;
4042 else if (bfd_get_format (abfd
) == bfd_core
)
4043 i_ehdrp
->e_type
= ET_CORE
;
4045 i_ehdrp
->e_type
= ET_REL
;
4047 switch (bfd_get_arch (abfd
))
4049 case bfd_arch_unknown
:
4050 i_ehdrp
->e_machine
= EM_NONE
;
4053 /* There used to be a long list of cases here, each one setting
4054 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4055 in the corresponding bfd definition. To avoid duplication,
4056 the switch was removed. Machines that need special handling
4057 can generally do it in elf_backend_final_write_processing(),
4058 unless they need the information earlier than the final write.
4059 Such need can generally be supplied by replacing the tests for
4060 e_machine with the conditions used to determine it. */
4062 if (get_elf_backend_data (abfd
) != NULL
)
4063 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
4065 i_ehdrp
->e_machine
= EM_NONE
;
4068 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4069 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4071 /* No program header, for now. */
4072 i_ehdrp
->e_phoff
= 0;
4073 i_ehdrp
->e_phentsize
= 0;
4074 i_ehdrp
->e_phnum
= 0;
4076 /* Each bfd section is section header entry. */
4077 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4078 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4080 /* If we're building an executable, we'll need a program header table. */
4081 if (abfd
->flags
& EXEC_P
)
4083 /* It all happens later. */
4085 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4087 /* elf_build_phdrs() returns a (NULL-terminated) array of
4088 Elf_Internal_Phdrs. */
4089 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4090 i_ehdrp
->e_phoff
= outbase
;
4091 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4096 i_ehdrp
->e_phentsize
= 0;
4098 i_ehdrp
->e_phoff
= 0;
4101 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4102 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
4103 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4104 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
4105 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4106 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
4107 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4108 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4109 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4115 /* Assign file positions for all the reloc sections which are not part
4116 of the loadable file image. */
4119 _bfd_elf_assign_file_positions_for_relocs (abfd
)
4123 unsigned int i
, num_sec
;
4124 Elf_Internal_Shdr
**shdrpp
;
4126 off
= elf_tdata (abfd
)->next_file_pos
;
4128 num_sec
= elf_numsections (abfd
);
4129 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4131 Elf_Internal_Shdr
*shdrp
;
4134 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4135 && shdrp
->sh_offset
== -1)
4136 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
4139 elf_tdata (abfd
)->next_file_pos
= off
;
4143 _bfd_elf_write_object_contents (abfd
)
4146 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4147 Elf_Internal_Ehdr
*i_ehdrp
;
4148 Elf_Internal_Shdr
**i_shdrp
;
4150 unsigned int count
, num_sec
;
4152 if (! abfd
->output_has_begun
4153 && ! _bfd_elf_compute_section_file_positions
4154 (abfd
, (struct bfd_link_info
*) NULL
))
4157 i_shdrp
= elf_elfsections (abfd
);
4158 i_ehdrp
= elf_elfheader (abfd
);
4161 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4165 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4167 /* After writing the headers, we need to write the sections too... */
4168 num_sec
= elf_numsections (abfd
);
4169 for (count
= 1; count
< num_sec
; count
++)
4171 if (bed
->elf_backend_section_processing
)
4172 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4173 if (i_shdrp
[count
]->contents
)
4175 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4177 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4178 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4181 if (count
== SHN_LORESERVE
- 1)
4182 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4185 /* Write out the section header names. */
4186 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4187 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4190 if (bed
->elf_backend_final_write_processing
)
4191 (*bed
->elf_backend_final_write_processing
) (abfd
,
4192 elf_tdata (abfd
)->linker
);
4194 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4198 _bfd_elf_write_corefile_contents (abfd
)
4201 /* Hopefully this can be done just like an object file. */
4202 return _bfd_elf_write_object_contents (abfd
);
4205 /* Given a section, search the header to find them. */
4208 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4212 struct elf_backend_data
*bed
;
4215 if (elf_section_data (asect
) != NULL
4216 && elf_section_data (asect
)->this_idx
!= 0)
4217 return elf_section_data (asect
)->this_idx
;
4219 if (bfd_is_abs_section (asect
))
4221 else if (bfd_is_com_section (asect
))
4223 else if (bfd_is_und_section (asect
))
4227 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4228 int maxindex
= elf_numsections (abfd
);
4230 for (index
= 1; index
< maxindex
; index
++)
4232 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4234 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4240 bed
= get_elf_backend_data (abfd
);
4241 if (bed
->elf_backend_section_from_bfd_section
)
4245 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4250 bfd_set_error (bfd_error_nonrepresentable_section
);
4255 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4259 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4261 asymbol
**asym_ptr_ptr
;
4263 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4265 flagword flags
= asym_ptr
->flags
;
4267 /* When gas creates relocations against local labels, it creates its
4268 own symbol for the section, but does put the symbol into the
4269 symbol chain, so udata is 0. When the linker is generating
4270 relocatable output, this section symbol may be for one of the
4271 input sections rather than the output section. */
4272 if (asym_ptr
->udata
.i
== 0
4273 && (flags
& BSF_SECTION_SYM
)
4274 && asym_ptr
->section
)
4278 if (asym_ptr
->section
->output_section
!= NULL
)
4279 indx
= asym_ptr
->section
->output_section
->index
;
4281 indx
= asym_ptr
->section
->index
;
4282 if (indx
< elf_num_section_syms (abfd
)
4283 && elf_section_syms (abfd
)[indx
] != NULL
)
4284 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4287 idx
= asym_ptr
->udata
.i
;
4291 /* This case can occur when using --strip-symbol on a symbol
4292 which is used in a relocation entry. */
4293 (*_bfd_error_handler
)
4294 (_("%s: symbol `%s' required but not present"),
4295 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4296 bfd_set_error (bfd_error_no_symbols
);
4303 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4304 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4305 elf_symbol_flags (flags
));
4313 /* Copy private BFD data. This copies any program header information. */
4316 copy_private_bfd_data (ibfd
, obfd
)
4320 Elf_Internal_Ehdr
* iehdr
;
4321 struct elf_segment_map
* map
;
4322 struct elf_segment_map
* map_first
;
4323 struct elf_segment_map
** pointer_to_map
;
4324 Elf_Internal_Phdr
* segment
;
4327 unsigned int num_segments
;
4328 boolean phdr_included
= false;
4329 bfd_vma maxpagesize
;
4330 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4331 unsigned int phdr_adjust_num
= 0;
4332 struct elf_backend_data
* bed
;
4334 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4335 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4338 if (elf_tdata (ibfd
)->phdr
== NULL
)
4341 bed
= get_elf_backend_data (ibfd
);
4342 iehdr
= elf_elfheader (ibfd
);
4345 pointer_to_map
= &map_first
;
4347 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4348 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4350 /* Returns the end address of the segment + 1. */
4351 #define SEGMENT_END(segment, start) \
4352 (start + (segment->p_memsz > segment->p_filesz \
4353 ? segment->p_memsz : segment->p_filesz))
4355 /* Returns true if the given section is contained within
4356 the given segment. VMA addresses are compared. */
4357 #define IS_CONTAINED_BY_VMA(section, segment) \
4358 (section->vma >= segment->p_vaddr \
4359 && (section->vma + section->_raw_size) \
4360 <= (SEGMENT_END (segment, segment->p_vaddr)))
4362 /* Returns true if the given section is contained within
4363 the given segment. LMA addresses are compared. */
4364 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4365 (section->lma >= base \
4366 && (section->lma + section->_raw_size) \
4367 <= SEGMENT_END (segment, base))
4369 /* Returns true if the given section is contained within the
4370 given segment. Filepos addresses are compared in an elf
4371 backend function. */
4372 #define IS_CONTAINED_BY_FILEPOS(sec, seg, bed) \
4373 (bed->is_contained_by_filepos \
4374 && (*bed->is_contained_by_filepos) (sec, seg))
4376 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4377 #define IS_COREFILE_NOTE(p, s) \
4378 (p->p_type == PT_NOTE \
4379 && bfd_get_format (ibfd) == bfd_core \
4380 && s->vma == 0 && s->lma == 0 \
4381 && (bfd_vma) s->filepos >= p->p_offset \
4382 && (bfd_vma) s->filepos + s->_raw_size \
4383 <= p->p_offset + p->p_filesz)
4385 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4386 linker, which generates a PT_INTERP section with p_vaddr and
4387 p_memsz set to 0. */
4388 #define IS_SOLARIS_PT_INTERP(p, s) \
4390 && p->p_filesz > 0 \
4391 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4392 && s->_raw_size > 0 \
4393 && (bfd_vma) s->filepos >= p->p_offset \
4394 && ((bfd_vma) s->filepos + s->_raw_size \
4395 <= p->p_offset + p->p_filesz))
4397 /* Decide if the given section should be included in the given segment.
4398 A section will be included if:
4399 1. It is within the address space of the segment -- we use the LMA
4400 if that is set for the segment and the VMA otherwise,
4401 2. It is an allocated segment,
4402 3. There is an output section associated with it,
4403 4. The section has not already been allocated to a previous segment. */
4404 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4405 (((((segment->p_paddr \
4406 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4407 : IS_CONTAINED_BY_VMA (section, segment)) \
4408 || IS_SOLARIS_PT_INTERP (segment, section)) \
4409 && (section->flags & SEC_ALLOC) != 0) \
4410 || IS_COREFILE_NOTE (segment, section) \
4411 || (IS_CONTAINED_BY_FILEPOS (section, segment, bed) \
4412 && (section->flags & SEC_ALLOC) == 0)) \
4413 && section->output_section != NULL \
4414 && section->segment_mark == false)
4416 /* Returns true iff seg1 starts after the end of seg2. */
4417 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4418 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4420 /* Returns true iff seg1 and seg2 overlap. */
4421 #define SEGMENT_OVERLAPS(seg1, seg2) \
4422 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4424 /* Initialise the segment mark field. */
4425 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4426 section
->segment_mark
= false;
4428 /* Scan through the segments specified in the program header
4429 of the input BFD. For this first scan we look for overlaps
4430 in the loadable segments. These can be created by weird
4431 parameters to objcopy. */
4432 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4437 Elf_Internal_Phdr
*segment2
;
4439 if (segment
->p_type
!= PT_LOAD
)
4442 /* Determine if this segment overlaps any previous segments. */
4443 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4445 bfd_signed_vma extra_length
;
4447 if (segment2
->p_type
!= PT_LOAD
4448 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4451 /* Merge the two segments together. */
4452 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4454 /* Extend SEGMENT2 to include SEGMENT and then delete
4457 SEGMENT_END (segment
, segment
->p_vaddr
)
4458 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4460 if (extra_length
> 0)
4462 segment2
->p_memsz
+= extra_length
;
4463 segment2
->p_filesz
+= extra_length
;
4466 segment
->p_type
= PT_NULL
;
4468 /* Since we have deleted P we must restart the outer loop. */
4470 segment
= elf_tdata (ibfd
)->phdr
;
4475 /* Extend SEGMENT to include SEGMENT2 and then delete
4478 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4479 - SEGMENT_END (segment
, segment
->p_vaddr
);
4481 if (extra_length
> 0)
4483 segment
->p_memsz
+= extra_length
;
4484 segment
->p_filesz
+= extra_length
;
4487 segment2
->p_type
= PT_NULL
;
4492 /* The second scan attempts to assign sections to segments. */
4493 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4497 unsigned int section_count
;
4498 asection
** sections
;
4499 asection
* output_section
;
4501 bfd_vma matching_lma
;
4502 bfd_vma suggested_lma
;
4506 if (segment
->p_type
== PT_NULL
)
4509 /* Compute how many sections might be placed into this segment. */
4511 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4512 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4515 /* Allocate a segment map big enough to contain all of the
4516 sections we have selected. */
4517 amt
= sizeof (struct elf_segment_map
);
4518 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4519 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4523 /* Initialise the fields of the segment map. Default to
4524 using the physical address of the segment in the input BFD. */
4526 map
->p_type
= segment
->p_type
;
4527 map
->p_flags
= segment
->p_flags
;
4528 map
->p_flags_valid
= 1;
4529 map
->p_paddr
= segment
->p_paddr
;
4530 map
->p_paddr_valid
= 1;
4532 /* Determine if this segment contains the ELF file header
4533 and if it contains the program headers themselves. */
4534 map
->includes_filehdr
= (segment
->p_offset
== 0
4535 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4537 map
->includes_phdrs
= 0;
4539 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4541 map
->includes_phdrs
=
4542 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4543 && (segment
->p_offset
+ segment
->p_filesz
4544 >= ((bfd_vma
) iehdr
->e_phoff
4545 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4547 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4548 phdr_included
= true;
4551 if (section_count
== 0)
4553 /* Special segments, such as the PT_PHDR segment, may contain
4554 no sections, but ordinary, loadable segments should contain
4556 if (segment
->p_type
== PT_LOAD
)
4557 (*_bfd_error_handler
)
4558 (_("%s: warning: Empty loadable segment detected\n"),
4559 bfd_archive_filename (ibfd
));
4562 *pointer_to_map
= map
;
4563 pointer_to_map
= &map
->next
;
4568 /* Now scan the sections in the input BFD again and attempt
4569 to add their corresponding output sections to the segment map.
4570 The problem here is how to handle an output section which has
4571 been moved (ie had its LMA changed). There are four possibilities:
4573 1. None of the sections have been moved.
4574 In this case we can continue to use the segment LMA from the
4577 2. All of the sections have been moved by the same amount.
4578 In this case we can change the segment's LMA to match the LMA
4579 of the first section.
4581 3. Some of the sections have been moved, others have not.
4582 In this case those sections which have not been moved can be
4583 placed in the current segment which will have to have its size,
4584 and possibly its LMA changed, and a new segment or segments will
4585 have to be created to contain the other sections.
4587 4. The sections have been moved, but not be the same amount.
4588 In this case we can change the segment's LMA to match the LMA
4589 of the first section and we will have to create a new segment
4590 or segments to contain the other sections.
4592 In order to save time, we allocate an array to hold the section
4593 pointers that we are interested in. As these sections get assigned
4594 to a segment, they are removed from this array. */
4596 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4597 sections
= (asection
**) bfd_malloc (amt
);
4598 if (sections
== NULL
)
4601 /* Step One: Scan for segment vs section LMA conflicts.
4602 Also add the sections to the section array allocated above.
4603 Also add the sections to the current segment. In the common
4604 case, where the sections have not been moved, this means that
4605 we have completely filled the segment, and there is nothing
4611 for (j
= 0, section
= ibfd
->sections
;
4613 section
= section
->next
)
4615 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
, bed
))
4617 output_section
= section
->output_section
;
4619 sections
[j
++] = section
;
4621 /* The Solaris native linker always sets p_paddr to 0.
4622 We try to catch that case here, and set it to the
4624 if (segment
->p_paddr
== 0
4625 && segment
->p_vaddr
!= 0
4627 && output_section
->lma
!= 0
4628 && (output_section
->vma
== (segment
->p_vaddr
4629 + (map
->includes_filehdr
4632 + (map
->includes_phdrs
4634 * iehdr
->e_phentsize
)
4636 map
->p_paddr
= segment
->p_vaddr
;
4638 /* Match up the physical address of the segment with the
4639 LMA address of the output section. */
4640 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4641 || IS_CONTAINED_BY_FILEPOS (section
, segment
, bed
)
4642 || IS_COREFILE_NOTE (segment
, section
))
4644 if (matching_lma
== 0)
4645 matching_lma
= output_section
->lma
;
4647 /* We assume that if the section fits within the segment
4648 then it does not overlap any other section within that
4650 map
->sections
[isec
++] = output_section
;
4652 else if (suggested_lma
== 0)
4653 suggested_lma
= output_section
->lma
;
4657 BFD_ASSERT (j
== section_count
);
4659 /* Step Two: Adjust the physical address of the current segment,
4661 if (isec
== section_count
)
4663 /* All of the sections fitted within the segment as currently
4664 specified. This is the default case. Add the segment to
4665 the list of built segments and carry on to process the next
4666 program header in the input BFD. */
4667 map
->count
= section_count
;
4668 *pointer_to_map
= map
;
4669 pointer_to_map
= &map
->next
;
4676 if (matching_lma
!= 0)
4678 /* At least one section fits inside the current segment.
4679 Keep it, but modify its physical address to match the
4680 LMA of the first section that fitted. */
4681 map
->p_paddr
= matching_lma
;
4685 /* None of the sections fitted inside the current segment.
4686 Change the current segment's physical address to match
4687 the LMA of the first section. */
4688 map
->p_paddr
= suggested_lma
;
4691 /* Offset the segment physical address from the lma
4692 to allow for space taken up by elf headers. */
4693 if (map
->includes_filehdr
)
4694 map
->p_paddr
-= iehdr
->e_ehsize
;
4696 if (map
->includes_phdrs
)
4698 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4700 /* iehdr->e_phnum is just an estimate of the number
4701 of program headers that we will need. Make a note
4702 here of the number we used and the segment we chose
4703 to hold these headers, so that we can adjust the
4704 offset when we know the correct value. */
4705 phdr_adjust_num
= iehdr
->e_phnum
;
4706 phdr_adjust_seg
= map
;
4710 /* Step Three: Loop over the sections again, this time assigning
4711 those that fit to the current segment and removing them from the
4712 sections array; but making sure not to leave large gaps. Once all
4713 possible sections have been assigned to the current segment it is
4714 added to the list of built segments and if sections still remain
4715 to be assigned, a new segment is constructed before repeating
4723 /* Fill the current segment with sections that fit. */
4724 for (j
= 0; j
< section_count
; j
++)
4726 section
= sections
[j
];
4728 if (section
== NULL
)
4731 output_section
= section
->output_section
;
4733 BFD_ASSERT (output_section
!= NULL
);
4735 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4736 || IS_COREFILE_NOTE (segment
, section
))
4738 if (map
->count
== 0)
4740 /* If the first section in a segment does not start at
4741 the beginning of the segment, then something is
4743 if (output_section
->lma
!=
4745 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4746 + (map
->includes_phdrs
4747 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4753 asection
* prev_sec
;
4755 prev_sec
= map
->sections
[map
->count
- 1];
4757 /* If the gap between the end of the previous section
4758 and the start of this section is more than
4759 maxpagesize then we need to start a new segment. */
4760 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4762 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4763 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4764 > output_section
->lma
))
4766 if (suggested_lma
== 0)
4767 suggested_lma
= output_section
->lma
;
4773 map
->sections
[map
->count
++] = output_section
;
4776 section
->segment_mark
= true;
4778 else if (suggested_lma
== 0)
4779 suggested_lma
= output_section
->lma
;
4782 BFD_ASSERT (map
->count
> 0);
4784 /* Add the current segment to the list of built segments. */
4785 *pointer_to_map
= map
;
4786 pointer_to_map
= &map
->next
;
4788 if (isec
< section_count
)
4790 /* We still have not allocated all of the sections to
4791 segments. Create a new segment here, initialise it
4792 and carry on looping. */
4793 amt
= sizeof (struct elf_segment_map
);
4794 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4795 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4799 /* Initialise the fields of the segment map. Set the physical
4800 physical address to the LMA of the first section that has
4801 not yet been assigned. */
4803 map
->p_type
= segment
->p_type
;
4804 map
->p_flags
= segment
->p_flags
;
4805 map
->p_flags_valid
= 1;
4806 map
->p_paddr
= suggested_lma
;
4807 map
->p_paddr_valid
= 1;
4808 map
->includes_filehdr
= 0;
4809 map
->includes_phdrs
= 0;
4812 while (isec
< section_count
);
4817 /* The Solaris linker creates program headers in which all the
4818 p_paddr fields are zero. When we try to objcopy or strip such a
4819 file, we get confused. Check for this case, and if we find it
4820 reset the p_paddr_valid fields. */
4821 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4822 if (map
->p_paddr
!= 0)
4826 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4827 map
->p_paddr_valid
= 0;
4830 elf_tdata (obfd
)->segment_map
= map_first
;
4832 /* If we had to estimate the number of program headers that were
4833 going to be needed, then check our estimate now and adjust
4834 the offset if necessary. */
4835 if (phdr_adjust_seg
!= NULL
)
4839 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4842 if (count
> phdr_adjust_num
)
4843 phdr_adjust_seg
->p_paddr
4844 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4848 /* Final Step: Sort the segments into ascending order of physical
4850 if (map_first
!= NULL
)
4852 struct elf_segment_map
*prev
;
4855 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4857 /* Yes I know - its a bubble sort.... */
4858 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4860 /* Swap map and map->next. */
4861 prev
->next
= map
->next
;
4862 map
->next
= map
->next
->next
;
4863 prev
->next
->next
= map
;
4873 #undef IS_CONTAINED_BY_VMA
4874 #undef IS_CONTAINED_BY_LMA
4875 #undef IS_CONTAINED_BY_FILEPOS
4876 #undef IS_COREFILE_NOTE
4877 #undef IS_SOLARIS_PT_INTERP
4878 #undef INCLUDE_SECTION_IN_SEGMENT
4879 #undef SEGMENT_AFTER_SEGMENT
4880 #undef SEGMENT_OVERLAPS
4884 /* Copy private section information. This copies over the entsize
4885 field, and sometimes the info field. */
4888 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4894 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4895 const struct elf_backend_data
*bed
= get_elf_backend_data (ibfd
);
4897 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4898 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4901 /* Copy over private BFD data if it has not already been copied.
4902 This must be done here, rather than in the copy_private_bfd_data
4903 entry point, because the latter is called after the section
4904 contents have been set, which means that the program headers have
4905 already been worked out. The backend function provides a way to
4906 override the test conditions and code path for the call to
4907 copy_private_bfd_data. */
4908 if (bed
->copy_private_bfd_data_p
)
4910 if ((*bed
->copy_private_bfd_data_p
) (ibfd
, isec
, obfd
, osec
))
4911 if (! copy_private_bfd_data (ibfd
, obfd
))
4914 else if (elf_tdata (obfd
)->segment_map
== NULL
&& elf_tdata (ibfd
)->phdr
!= NULL
)
4918 /* Only set up the segments if there are no more SEC_ALLOC
4919 sections. FIXME: This won't do the right thing if objcopy is
4920 used to remove the last SEC_ALLOC section, since objcopy
4921 won't call this routine in that case. */
4922 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4923 if ((s
->flags
& SEC_ALLOC
) != 0)
4927 if (! copy_private_bfd_data (ibfd
, obfd
))
4932 ihdr
= &elf_section_data (isec
)->this_hdr
;
4933 ohdr
= &elf_section_data (osec
)->this_hdr
;
4935 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4937 if (ihdr
->sh_type
== SHT_SYMTAB
4938 || ihdr
->sh_type
== SHT_DYNSYM
4939 || ihdr
->sh_type
== SHT_GNU_verneed
4940 || ihdr
->sh_type
== SHT_GNU_verdef
)
4941 ohdr
->sh_info
= ihdr
->sh_info
;
4943 elf_section_data (osec
)->use_rela_p
4944 = elf_section_data (isec
)->use_rela_p
;
4949 /* Copy private symbol information. If this symbol is in a section
4950 which we did not map into a BFD section, try to map the section
4951 index correctly. We use special macro definitions for the mapped
4952 section indices; these definitions are interpreted by the
4953 swap_out_syms function. */
4955 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4956 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4957 #define MAP_STRTAB (SHN_HIOS + 3)
4958 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4959 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4962 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4968 elf_symbol_type
*isym
, *osym
;
4970 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4971 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4974 isym
= elf_symbol_from (ibfd
, isymarg
);
4975 osym
= elf_symbol_from (obfd
, osymarg
);
4979 && bfd_is_abs_section (isym
->symbol
.section
))
4983 shndx
= isym
->internal_elf_sym
.st_shndx
;
4984 if (shndx
== elf_onesymtab (ibfd
))
4985 shndx
= MAP_ONESYMTAB
;
4986 else if (shndx
== elf_dynsymtab (ibfd
))
4987 shndx
= MAP_DYNSYMTAB
;
4988 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4990 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4991 shndx
= MAP_SHSTRTAB
;
4992 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
4993 shndx
= MAP_SYM_SHNDX
;
4994 osym
->internal_elf_sym
.st_shndx
= shndx
;
5000 /* Swap out the symbols. */
5003 swap_out_syms (abfd
, sttp
, relocatable_p
)
5005 struct bfd_strtab_hash
**sttp
;
5008 struct elf_backend_data
*bed
;
5011 struct bfd_strtab_hash
*stt
;
5012 Elf_Internal_Shdr
*symtab_hdr
;
5013 Elf_Internal_Shdr
*symtab_shndx_hdr
;
5014 Elf_Internal_Shdr
*symstrtab_hdr
;
5015 char *outbound_syms
;
5016 char *outbound_shndx
;
5020 if (!elf_map_symbols (abfd
))
5023 /* Dump out the symtabs. */
5024 stt
= _bfd_elf_stringtab_init ();
5028 bed
= get_elf_backend_data (abfd
);
5029 symcount
= bfd_get_symcount (abfd
);
5030 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5031 symtab_hdr
->sh_type
= SHT_SYMTAB
;
5032 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
5033 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
5034 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
5035 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
5037 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
5038 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5040 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
5041 outbound_syms
= bfd_alloc (abfd
, amt
);
5042 if (outbound_syms
== NULL
)
5044 symtab_hdr
->contents
= (PTR
) outbound_syms
;
5046 outbound_shndx
= NULL
;
5047 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
5048 if (symtab_shndx_hdr
->sh_name
!= 0)
5050 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
5051 outbound_shndx
= bfd_alloc (abfd
, amt
);
5052 if (outbound_shndx
== NULL
)
5054 memset (outbound_shndx
, 0, (unsigned long) amt
);
5055 symtab_shndx_hdr
->contents
= outbound_shndx
;
5056 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
5057 symtab_shndx_hdr
->sh_size
= amt
;
5058 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
5059 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
5062 /* now generate the data (for "contents") */
5064 /* Fill in zeroth symbol and swap it out. */
5065 Elf_Internal_Sym sym
;
5071 sym
.st_shndx
= SHN_UNDEF
;
5072 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5073 outbound_syms
+= bed
->s
->sizeof_sym
;
5074 if (outbound_shndx
!= NULL
)
5075 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5078 syms
= bfd_get_outsymbols (abfd
);
5079 for (idx
= 0; idx
< symcount
; idx
++)
5081 Elf_Internal_Sym sym
;
5082 bfd_vma value
= syms
[idx
]->value
;
5083 elf_symbol_type
*type_ptr
;
5084 flagword flags
= syms
[idx
]->flags
;
5087 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5089 /* Local section symbols have no name. */
5094 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5097 if (sym
.st_name
== (unsigned long) -1)
5101 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5103 if ((flags
& BSF_SECTION_SYM
) == 0
5104 && bfd_is_com_section (syms
[idx
]->section
))
5106 /* ELF common symbols put the alignment into the `value' field,
5107 and the size into the `size' field. This is backwards from
5108 how BFD handles it, so reverse it here. */
5109 sym
.st_size
= value
;
5110 if (type_ptr
== NULL
5111 || type_ptr
->internal_elf_sym
.st_value
== 0)
5112 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5114 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5115 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5116 (abfd
, syms
[idx
]->section
);
5120 asection
*sec
= syms
[idx
]->section
;
5123 if (sec
->output_section
)
5125 value
+= sec
->output_offset
;
5126 sec
= sec
->output_section
;
5128 /* Don't add in the section vma for relocatable output. */
5129 if (! relocatable_p
)
5131 sym
.st_value
= value
;
5132 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5134 if (bfd_is_abs_section (sec
)
5136 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5138 /* This symbol is in a real ELF section which we did
5139 not create as a BFD section. Undo the mapping done
5140 by copy_private_symbol_data. */
5141 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5145 shndx
= elf_onesymtab (abfd
);
5148 shndx
= elf_dynsymtab (abfd
);
5151 shndx
= elf_tdata (abfd
)->strtab_section
;
5154 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5157 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5165 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5171 /* Writing this would be a hell of a lot easier if
5172 we had some decent documentation on bfd, and
5173 knew what to expect of the library, and what to
5174 demand of applications. For example, it
5175 appears that `objcopy' might not set the
5176 section of a symbol to be a section that is
5177 actually in the output file. */
5178 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5179 BFD_ASSERT (sec2
!= 0);
5180 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5181 BFD_ASSERT (shndx
!= -1);
5185 sym
.st_shndx
= shndx
;
5188 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5190 else if ((flags
& BSF_FUNCTION
) != 0)
5192 else if ((flags
& BSF_OBJECT
) != 0)
5197 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5200 /* Processor-specific types */
5201 if (type_ptr
!= NULL
5202 && bed
->elf_backend_get_symbol_type
)
5203 type
= ((*bed
->elf_backend_get_symbol_type
)
5204 (&type_ptr
->internal_elf_sym
, type
));
5206 if (flags
& BSF_SECTION_SYM
)
5208 if (flags
& BSF_GLOBAL
)
5209 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5211 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5213 else if (bfd_is_com_section (syms
[idx
]->section
))
5214 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5215 else if (bfd_is_und_section (syms
[idx
]->section
))
5216 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5220 else if (flags
& BSF_FILE
)
5221 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5224 int bind
= STB_LOCAL
;
5226 if (flags
& BSF_LOCAL
)
5228 else if (flags
& BSF_WEAK
)
5230 else if (flags
& BSF_GLOBAL
)
5233 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5236 if (type_ptr
!= NULL
)
5237 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5241 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5242 outbound_syms
+= bed
->s
->sizeof_sym
;
5243 if (outbound_shndx
!= NULL
)
5244 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5248 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5249 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5251 symstrtab_hdr
->sh_flags
= 0;
5252 symstrtab_hdr
->sh_addr
= 0;
5253 symstrtab_hdr
->sh_entsize
= 0;
5254 symstrtab_hdr
->sh_link
= 0;
5255 symstrtab_hdr
->sh_info
= 0;
5256 symstrtab_hdr
->sh_addralign
= 1;
5261 /* Return the number of bytes required to hold the symtab vector.
5263 Note that we base it on the count plus 1, since we will null terminate
5264 the vector allocated based on this size. However, the ELF symbol table
5265 always has a dummy entry as symbol #0, so it ends up even. */
5268 _bfd_elf_get_symtab_upper_bound (abfd
)
5273 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5275 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5276 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5278 symtab_size
-= sizeof (asymbol
*);
5284 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5289 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5291 if (elf_dynsymtab (abfd
) == 0)
5293 bfd_set_error (bfd_error_invalid_operation
);
5297 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5298 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5300 symtab_size
-= sizeof (asymbol
*);
5306 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5307 bfd
*abfd ATTRIBUTE_UNUSED
;
5310 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5313 /* Canonicalize the relocs. */
5316 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5324 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5326 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5329 tblptr
= section
->relocation
;
5330 for (i
= 0; i
< section
->reloc_count
; i
++)
5331 *relptr
++ = tblptr
++;
5335 return section
->reloc_count
;
5339 _bfd_elf_get_symtab (abfd
, alocation
)
5341 asymbol
**alocation
;
5343 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5344 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5347 bfd_get_symcount (abfd
) = symcount
;
5352 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5354 asymbol
**alocation
;
5356 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5357 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5360 /* Return the size required for the dynamic reloc entries. Any
5361 section that was actually installed in the BFD, and has type
5362 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5363 considered to be a dynamic reloc section. */
5366 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5372 if (elf_dynsymtab (abfd
) == 0)
5374 bfd_set_error (bfd_error_invalid_operation
);
5378 ret
= sizeof (arelent
*);
5379 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5380 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5381 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5382 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5383 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5384 * sizeof (arelent
*));
5389 /* Canonicalize the dynamic relocation entries. Note that we return
5390 the dynamic relocations as a single block, although they are
5391 actually associated with particular sections; the interface, which
5392 was designed for SunOS style shared libraries, expects that there
5393 is only one set of dynamic relocs. Any section that was actually
5394 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5395 the dynamic symbol table, is considered to be a dynamic reloc
5399 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5404 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5408 if (elf_dynsymtab (abfd
) == 0)
5410 bfd_set_error (bfd_error_invalid_operation
);
5414 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5416 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5418 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5419 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5420 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5425 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5427 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5429 for (i
= 0; i
< count
; i
++)
5440 /* Read in the version information. */
5443 _bfd_elf_slurp_version_tables (abfd
)
5446 bfd_byte
*contents
= NULL
;
5449 if (elf_dynverdef (abfd
) != 0)
5451 Elf_Internal_Shdr
*hdr
;
5452 Elf_External_Verdef
*everdef
;
5453 Elf_Internal_Verdef
*iverdef
;
5454 Elf_Internal_Verdef
*iverdefarr
;
5455 Elf_Internal_Verdef iverdefmem
;
5457 unsigned int maxidx
;
5459 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5461 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5462 if (contents
== NULL
)
5464 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5465 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5468 /* We know the number of entries in the section but not the maximum
5469 index. Therefore we have to run through all entries and find
5471 everdef
= (Elf_External_Verdef
*) contents
;
5473 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5475 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5477 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5478 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5480 everdef
= ((Elf_External_Verdef
*)
5481 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5484 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5485 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5486 if (elf_tdata (abfd
)->verdef
== NULL
)
5489 elf_tdata (abfd
)->cverdefs
= maxidx
;
5491 everdef
= (Elf_External_Verdef
*) contents
;
5492 iverdefarr
= elf_tdata (abfd
)->verdef
;
5493 for (i
= 0; i
< hdr
->sh_info
; i
++)
5495 Elf_External_Verdaux
*everdaux
;
5496 Elf_Internal_Verdaux
*iverdaux
;
5499 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5501 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5502 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5504 iverdef
->vd_bfd
= abfd
;
5506 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5507 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5508 if (iverdef
->vd_auxptr
== NULL
)
5511 everdaux
= ((Elf_External_Verdaux
*)
5512 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5513 iverdaux
= iverdef
->vd_auxptr
;
5514 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5516 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5518 iverdaux
->vda_nodename
=
5519 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5520 iverdaux
->vda_name
);
5521 if (iverdaux
->vda_nodename
== NULL
)
5524 if (j
+ 1 < iverdef
->vd_cnt
)
5525 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5527 iverdaux
->vda_nextptr
= NULL
;
5529 everdaux
= ((Elf_External_Verdaux
*)
5530 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5533 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5535 if (i
+ 1 < hdr
->sh_info
)
5536 iverdef
->vd_nextdef
= iverdef
+ 1;
5538 iverdef
->vd_nextdef
= NULL
;
5540 everdef
= ((Elf_External_Verdef
*)
5541 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5548 if (elf_dynverref (abfd
) != 0)
5550 Elf_Internal_Shdr
*hdr
;
5551 Elf_External_Verneed
*everneed
;
5552 Elf_Internal_Verneed
*iverneed
;
5555 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5557 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5558 elf_tdata (abfd
)->verref
=
5559 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5560 if (elf_tdata (abfd
)->verref
== NULL
)
5563 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5565 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5566 if (contents
== NULL
)
5568 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5569 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5572 everneed
= (Elf_External_Verneed
*) contents
;
5573 iverneed
= elf_tdata (abfd
)->verref
;
5574 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5576 Elf_External_Vernaux
*evernaux
;
5577 Elf_Internal_Vernaux
*ivernaux
;
5580 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5582 iverneed
->vn_bfd
= abfd
;
5584 iverneed
->vn_filename
=
5585 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5587 if (iverneed
->vn_filename
== NULL
)
5590 amt
= iverneed
->vn_cnt
;
5591 amt
*= sizeof (Elf_Internal_Vernaux
);
5592 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5594 evernaux
= ((Elf_External_Vernaux
*)
5595 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5596 ivernaux
= iverneed
->vn_auxptr
;
5597 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5599 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5601 ivernaux
->vna_nodename
=
5602 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5603 ivernaux
->vna_name
);
5604 if (ivernaux
->vna_nodename
== NULL
)
5607 if (j
+ 1 < iverneed
->vn_cnt
)
5608 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5610 ivernaux
->vna_nextptr
= NULL
;
5612 evernaux
= ((Elf_External_Vernaux
*)
5613 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5616 if (i
+ 1 < hdr
->sh_info
)
5617 iverneed
->vn_nextref
= iverneed
+ 1;
5619 iverneed
->vn_nextref
= NULL
;
5621 everneed
= ((Elf_External_Verneed
*)
5622 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5632 if (contents
== NULL
)
5638 _bfd_elf_make_empty_symbol (abfd
)
5641 elf_symbol_type
*newsym
;
5642 bfd_size_type amt
= sizeof (elf_symbol_type
);
5644 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5649 newsym
->symbol
.the_bfd
= abfd
;
5650 return &newsym
->symbol
;
5655 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5656 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5660 bfd_symbol_info (symbol
, ret
);
5663 /* Return whether a symbol name implies a local symbol. Most targets
5664 use this function for the is_local_label_name entry point, but some
5668 _bfd_elf_is_local_label_name (abfd
, name
)
5669 bfd
*abfd ATTRIBUTE_UNUSED
;
5672 /* Normal local symbols start with ``.L''. */
5673 if (name
[0] == '.' && name
[1] == 'L')
5676 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5677 DWARF debugging symbols starting with ``..''. */
5678 if (name
[0] == '.' && name
[1] == '.')
5681 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5682 emitting DWARF debugging output. I suspect this is actually a
5683 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5684 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5685 underscore to be emitted on some ELF targets). For ease of use,
5686 we treat such symbols as local. */
5687 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5694 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5695 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5696 asymbol
*symbol ATTRIBUTE_UNUSED
;
5703 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5705 enum bfd_architecture arch
;
5706 unsigned long machine
;
5708 /* If this isn't the right architecture for this backend, and this
5709 isn't the generic backend, fail. */
5710 if (arch
!= get_elf_backend_data (abfd
)->arch
5711 && arch
!= bfd_arch_unknown
5712 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5715 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5718 /* Find the function to a particular section and offset,
5719 for error reporting. */
5722 elf_find_function (abfd
, section
, symbols
, offset
,
5723 filename_ptr
, functionname_ptr
)
5724 bfd
*abfd ATTRIBUTE_UNUSED
;
5728 const char **filename_ptr
;
5729 const char **functionname_ptr
;
5731 const char *filename
;
5740 for (p
= symbols
; *p
!= NULL
; p
++)
5744 q
= (elf_symbol_type
*) *p
;
5746 if (bfd_get_section (&q
->symbol
) != section
)
5749 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5754 filename
= bfd_asymbol_name (&q
->symbol
);
5758 if (q
->symbol
.section
== section
5759 && q
->symbol
.value
>= low_func
5760 && q
->symbol
.value
<= offset
)
5762 func
= (asymbol
*) q
;
5763 low_func
= q
->symbol
.value
;
5773 *filename_ptr
= filename
;
5774 if (functionname_ptr
)
5775 *functionname_ptr
= bfd_asymbol_name (func
);
5780 /* Find the nearest line to a particular section and offset,
5781 for error reporting. */
5784 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5785 filename_ptr
, functionname_ptr
, line_ptr
)
5790 const char **filename_ptr
;
5791 const char **functionname_ptr
;
5792 unsigned int *line_ptr
;
5796 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5797 filename_ptr
, functionname_ptr
,
5800 if (!*functionname_ptr
)
5801 elf_find_function (abfd
, section
, symbols
, offset
,
5802 *filename_ptr
? NULL
: filename_ptr
,
5808 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5809 filename_ptr
, functionname_ptr
,
5811 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5813 if (!*functionname_ptr
)
5814 elf_find_function (abfd
, section
, symbols
, offset
,
5815 *filename_ptr
? NULL
: filename_ptr
,
5821 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5822 &found
, filename_ptr
,
5823 functionname_ptr
, line_ptr
,
5824 &elf_tdata (abfd
)->line_info
))
5829 if (symbols
== NULL
)
5832 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5833 filename_ptr
, functionname_ptr
))
5841 _bfd_elf_sizeof_headers (abfd
, reloc
)
5847 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5849 ret
+= get_program_header_size (abfd
);
5854 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5859 bfd_size_type count
;
5861 Elf_Internal_Shdr
*hdr
;
5864 if (! abfd
->output_has_begun
5865 && ! _bfd_elf_compute_section_file_positions
5866 (abfd
, (struct bfd_link_info
*) NULL
))
5869 hdr
= &elf_section_data (section
)->this_hdr
;
5870 pos
= hdr
->sh_offset
+ offset
;
5871 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5872 || bfd_bwrite (location
, count
, abfd
) != count
)
5879 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5880 bfd
*abfd ATTRIBUTE_UNUSED
;
5881 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5882 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5889 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5892 Elf_Internal_Rel
*dst
;
5898 /* Try to convert a non-ELF reloc into an ELF one. */
5901 _bfd_elf_validate_reloc (abfd
, areloc
)
5905 /* Check whether we really have an ELF howto. */
5907 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5909 bfd_reloc_code_real_type code
;
5910 reloc_howto_type
*howto
;
5912 /* Alien reloc: Try to determine its type to replace it with an
5913 equivalent ELF reloc. */
5915 if (areloc
->howto
->pc_relative
)
5917 switch (areloc
->howto
->bitsize
)
5920 code
= BFD_RELOC_8_PCREL
;
5923 code
= BFD_RELOC_12_PCREL
;
5926 code
= BFD_RELOC_16_PCREL
;
5929 code
= BFD_RELOC_24_PCREL
;
5932 code
= BFD_RELOC_32_PCREL
;
5935 code
= BFD_RELOC_64_PCREL
;
5941 howto
= bfd_reloc_type_lookup (abfd
, code
);
5943 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5945 if (howto
->pcrel_offset
)
5946 areloc
->addend
+= areloc
->address
;
5948 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5953 switch (areloc
->howto
->bitsize
)
5959 code
= BFD_RELOC_14
;
5962 code
= BFD_RELOC_16
;
5965 code
= BFD_RELOC_26
;
5968 code
= BFD_RELOC_32
;
5971 code
= BFD_RELOC_64
;
5977 howto
= bfd_reloc_type_lookup (abfd
, code
);
5981 areloc
->howto
= howto
;
5989 (*_bfd_error_handler
)
5990 (_("%s: unsupported relocation type %s"),
5991 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5992 bfd_set_error (bfd_error_bad_value
);
5997 _bfd_elf_close_and_cleanup (abfd
)
6000 if (bfd_get_format (abfd
) == bfd_object
)
6002 if (elf_shstrtab (abfd
) != NULL
)
6003 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
6006 return _bfd_generic_close_and_cleanup (abfd
);
6009 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6010 in the relocation's offset. Thus we cannot allow any sort of sanity
6011 range-checking to interfere. There is nothing else to do in processing
6014 bfd_reloc_status_type
6015 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
6016 bfd
*abfd ATTRIBUTE_UNUSED
;
6017 arelent
*re ATTRIBUTE_UNUSED
;
6018 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
6019 PTR data ATTRIBUTE_UNUSED
;
6020 asection
*is ATTRIBUTE_UNUSED
;
6021 bfd
*obfd ATTRIBUTE_UNUSED
;
6022 char **errmsg ATTRIBUTE_UNUSED
;
6024 return bfd_reloc_ok
;
6027 /* Elf core file support. Much of this only works on native
6028 toolchains, since we rely on knowing the
6029 machine-dependent procfs structure in order to pick
6030 out details about the corefile. */
6032 #ifdef HAVE_SYS_PROCFS_H
6033 # include <sys/procfs.h>
6036 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6039 elfcore_make_pid (abfd
)
6042 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
6043 + (elf_tdata (abfd
)->core_pid
));
6046 /* If there isn't a section called NAME, make one, using
6047 data from SECT. Note, this function will generate a
6048 reference to NAME, so you shouldn't deallocate or
6052 elfcore_maybe_make_sect (abfd
, name
, sect
)
6059 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
6062 sect2
= bfd_make_section (abfd
, name
);
6066 sect2
->_raw_size
= sect
->_raw_size
;
6067 sect2
->filepos
= sect
->filepos
;
6068 sect2
->flags
= sect
->flags
;
6069 sect2
->alignment_power
= sect
->alignment_power
;
6073 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6074 actually creates up to two pseudosections:
6075 - For the single-threaded case, a section named NAME, unless
6076 such a section already exists.
6077 - For the multi-threaded case, a section named "NAME/PID", where
6078 PID is elfcore_make_pid (abfd).
6079 Both pseudosections have identical contents. */
6081 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
6088 char *threaded_name
;
6091 /* Build the section name. */
6093 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6094 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6095 if (threaded_name
== NULL
)
6097 strcpy (threaded_name
, buf
);
6099 sect
= bfd_make_section (abfd
, threaded_name
);
6102 sect
->_raw_size
= size
;
6103 sect
->filepos
= filepos
;
6104 sect
->flags
= SEC_HAS_CONTENTS
;
6105 sect
->alignment_power
= 2;
6107 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6110 /* prstatus_t exists on:
6112 linux 2.[01] + glibc
6116 #if defined (HAVE_PRSTATUS_T)
6117 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6120 elfcore_grok_prstatus (abfd
, note
)
6122 Elf_Internal_Note
*note
;
6127 if (note
->descsz
== sizeof (prstatus_t
))
6131 raw_size
= sizeof (prstat
.pr_reg
);
6132 offset
= offsetof (prstatus_t
, pr_reg
);
6133 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6135 /* Do not overwrite the core signal if it
6136 has already been set by another thread. */
6137 if (elf_tdata (abfd
)->core_signal
== 0)
6138 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6139 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6141 /* pr_who exists on:
6144 pr_who doesn't exist on:
6147 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6148 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6151 #if defined (HAVE_PRSTATUS32_T)
6152 else if (note
->descsz
== sizeof (prstatus32_t
))
6154 /* 64-bit host, 32-bit corefile */
6155 prstatus32_t prstat
;
6157 raw_size
= sizeof (prstat
.pr_reg
);
6158 offset
= offsetof (prstatus32_t
, pr_reg
);
6159 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6161 /* Do not overwrite the core signal if it
6162 has already been set by another thread. */
6163 if (elf_tdata (abfd
)->core_signal
== 0)
6164 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6165 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6167 /* pr_who exists on:
6170 pr_who doesn't exist on:
6173 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6174 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6177 #endif /* HAVE_PRSTATUS32_T */
6180 /* Fail - we don't know how to handle any other
6181 note size (ie. data object type). */
6185 /* Make a ".reg/999" section and a ".reg" section. */
6186 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6187 raw_size
, note
->descpos
+ offset
);
6189 #endif /* defined (HAVE_PRSTATUS_T) */
6191 /* Create a pseudosection containing the exact contents of NOTE. */
6193 elfcore_make_note_pseudosection (abfd
, name
, note
)
6196 Elf_Internal_Note
*note
;
6198 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6199 note
->descsz
, note
->descpos
);
6202 /* There isn't a consistent prfpregset_t across platforms,
6203 but it doesn't matter, because we don't have to pick this
6204 data structure apart. */
6207 elfcore_grok_prfpreg (abfd
, note
)
6209 Elf_Internal_Note
*note
;
6211 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6214 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6215 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6219 elfcore_grok_prxfpreg (abfd
, note
)
6221 Elf_Internal_Note
*note
;
6223 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6226 #if defined (HAVE_PRPSINFO_T)
6227 typedef prpsinfo_t elfcore_psinfo_t
;
6228 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6229 typedef prpsinfo32_t elfcore_psinfo32_t
;
6233 #if defined (HAVE_PSINFO_T)
6234 typedef psinfo_t elfcore_psinfo_t
;
6235 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6236 typedef psinfo32_t elfcore_psinfo32_t
;
6240 /* return a malloc'ed copy of a string at START which is at
6241 most MAX bytes long, possibly without a terminating '\0'.
6242 the copy will always have a terminating '\0'. */
6245 _bfd_elfcore_strndup (abfd
, start
, max
)
6251 char *end
= memchr (start
, '\0', max
);
6259 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6263 memcpy (dups
, start
, len
);
6269 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6270 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6273 elfcore_grok_psinfo (abfd
, note
)
6275 Elf_Internal_Note
*note
;
6277 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6279 elfcore_psinfo_t psinfo
;
6281 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6283 elf_tdata (abfd
)->core_program
6284 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6285 sizeof (psinfo
.pr_fname
));
6287 elf_tdata (abfd
)->core_command
6288 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6289 sizeof (psinfo
.pr_psargs
));
6291 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6292 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6294 /* 64-bit host, 32-bit corefile */
6295 elfcore_psinfo32_t psinfo
;
6297 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6299 elf_tdata (abfd
)->core_program
6300 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6301 sizeof (psinfo
.pr_fname
));
6303 elf_tdata (abfd
)->core_command
6304 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6305 sizeof (psinfo
.pr_psargs
));
6311 /* Fail - we don't know how to handle any other
6312 note size (ie. data object type). */
6316 /* Note that for some reason, a spurious space is tacked
6317 onto the end of the args in some (at least one anyway)
6318 implementations, so strip it off if it exists. */
6321 char *command
= elf_tdata (abfd
)->core_command
;
6322 int n
= strlen (command
);
6324 if (0 < n
&& command
[n
- 1] == ' ')
6325 command
[n
- 1] = '\0';
6330 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6332 #if defined (HAVE_PSTATUS_T)
6333 static boolean elfcore_grok_pstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6336 elfcore_grok_pstatus (abfd
, note
)
6338 Elf_Internal_Note
*note
;
6340 if (note
->descsz
== sizeof (pstatus_t
)
6341 #if defined (HAVE_PXSTATUS_T)
6342 || note
->descsz
== sizeof (pxstatus_t
)
6348 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6350 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6352 #if defined (HAVE_PSTATUS32_T)
6353 else if (note
->descsz
== sizeof (pstatus32_t
))
6355 /* 64-bit host, 32-bit corefile */
6358 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6360 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6363 /* Could grab some more details from the "representative"
6364 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6365 NT_LWPSTATUS note, presumably. */
6369 #endif /* defined (HAVE_PSTATUS_T) */
6371 #if defined (HAVE_LWPSTATUS_T)
6372 static boolean elfcore_grok_lwpstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6375 elfcore_grok_lwpstatus (abfd
, note
)
6377 Elf_Internal_Note
*note
;
6379 lwpstatus_t lwpstat
;
6384 if (note
->descsz
!= sizeof (lwpstat
)
6385 #if defined (HAVE_LWPXSTATUS_T)
6386 && note
->descsz
!= sizeof (lwpxstatus_t
)
6391 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6393 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6394 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6396 /* Make a ".reg/999" section. */
6398 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6399 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6404 sect
= bfd_make_section (abfd
, name
);
6408 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6409 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6410 sect
->filepos
= note
->descpos
6411 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6414 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6415 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6416 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6419 sect
->flags
= SEC_HAS_CONTENTS
;
6420 sect
->alignment_power
= 2;
6422 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6425 /* Make a ".reg2/999" section */
6427 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6428 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6433 sect
= bfd_make_section (abfd
, name
);
6437 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6438 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6439 sect
->filepos
= note
->descpos
6440 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6443 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6444 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6445 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6448 sect
->flags
= SEC_HAS_CONTENTS
;
6449 sect
->alignment_power
= 2;
6451 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6453 #endif /* defined (HAVE_LWPSTATUS_T) */
6455 #if defined (HAVE_WIN32_PSTATUS_T)
6457 elfcore_grok_win32pstatus (abfd
, note
)
6459 Elf_Internal_Note
*note
;
6464 win32_pstatus_t pstatus
;
6466 if (note
->descsz
< sizeof (pstatus
))
6469 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6471 switch (pstatus
.data_type
)
6473 case NOTE_INFO_PROCESS
:
6474 /* FIXME: need to add ->core_command. */
6475 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6476 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6479 case NOTE_INFO_THREAD
:
6480 /* Make a ".reg/999" section. */
6481 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6483 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6489 sect
= bfd_make_section (abfd
, name
);
6493 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6494 sect
->filepos
= (note
->descpos
6495 + offsetof (struct win32_pstatus
,
6496 data
.thread_info
.thread_context
));
6497 sect
->flags
= SEC_HAS_CONTENTS
;
6498 sect
->alignment_power
= 2;
6500 if (pstatus
.data
.thread_info
.is_active_thread
)
6501 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6505 case NOTE_INFO_MODULE
:
6506 /* Make a ".module/xxxxxxxx" section. */
6507 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6509 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6515 sect
= bfd_make_section (abfd
, name
);
6520 sect
->_raw_size
= note
->descsz
;
6521 sect
->filepos
= note
->descpos
;
6522 sect
->flags
= SEC_HAS_CONTENTS
;
6523 sect
->alignment_power
= 2;
6532 #endif /* HAVE_WIN32_PSTATUS_T */
6535 elfcore_grok_note (abfd
, note
)
6537 Elf_Internal_Note
*note
;
6539 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6547 if (bed
->elf_backend_grok_prstatus
)
6548 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6550 #if defined (HAVE_PRSTATUS_T)
6551 return elfcore_grok_prstatus (abfd
, note
);
6556 #if defined (HAVE_PSTATUS_T)
6558 return elfcore_grok_pstatus (abfd
, note
);
6561 #if defined (HAVE_LWPSTATUS_T)
6563 return elfcore_grok_lwpstatus (abfd
, note
);
6566 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6567 return elfcore_grok_prfpreg (abfd
, note
);
6569 #if defined (HAVE_WIN32_PSTATUS_T)
6570 case NT_WIN32PSTATUS
:
6571 return elfcore_grok_win32pstatus (abfd
, note
);
6574 case NT_PRXFPREG
: /* Linux SSE extension */
6575 if (note
->namesz
== 5
6576 && ! strcmp (note
->namedata
, "LINUX"))
6577 return elfcore_grok_prxfpreg (abfd
, note
);
6583 if (bed
->elf_backend_grok_psinfo
)
6584 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6586 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6587 return elfcore_grok_psinfo (abfd
, note
);
6595 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6596 Elf_Internal_Note
*note
;
6601 cp
= strchr (note
->namedata
, '@');
6604 *lwpidp
= atoi(cp
+ 1);
6611 elfcore_grok_netbsd_procinfo (abfd
, note
)
6613 Elf_Internal_Note
*note
;
6616 /* Signal number at offset 0x08. */
6617 elf_tdata (abfd
)->core_signal
6618 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6620 /* Process ID at offset 0x50. */
6621 elf_tdata (abfd
)->core_pid
6622 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6624 /* Command name at 0x7c (max 32 bytes, including nul). */
6625 elf_tdata (abfd
)->core_command
6626 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6632 elfcore_grok_netbsd_note (abfd
, note
)
6634 Elf_Internal_Note
*note
;
6638 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6639 elf_tdata (abfd
)->core_lwpid
= lwp
;
6641 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6643 /* NetBSD-specific core "procinfo". Note that we expect to
6644 find this note before any of the others, which is fine,
6645 since the kernel writes this note out first when it
6646 creates a core file. */
6648 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6651 /* As of Jan 2002 there are no other machine-independent notes
6652 defined for NetBSD core files. If the note type is less
6653 than the start of the machine-dependent note types, we don't
6656 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6660 switch (bfd_get_arch (abfd
))
6662 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6663 PT_GETFPREGS == mach+2. */
6665 case bfd_arch_alpha
:
6666 case bfd_arch_sparc
:
6669 case NT_NETBSDCORE_FIRSTMACH
+0:
6670 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6672 case NT_NETBSDCORE_FIRSTMACH
+2:
6673 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6679 /* On all other arch's, PT_GETREGS == mach+1 and
6680 PT_GETFPREGS == mach+3. */
6685 case NT_NETBSDCORE_FIRSTMACH
+1:
6686 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6688 case NT_NETBSDCORE_FIRSTMACH
+3:
6689 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6698 /* Function: elfcore_write_note
6705 size of data for note
6708 End of buffer containing note. */
6711 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6720 Elf_External_Note
*xnp
;
6721 int namesz
= strlen (name
);
6722 int newspace
= BFD_ALIGN (sizeof (Elf_External_Note
) + size
+ namesz
- 1, 4);
6725 p
= realloc (buf
, *bufsiz
+ newspace
);
6727 *bufsiz
+= newspace
;
6728 xnp
= (Elf_External_Note
*) dest
;
6729 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6730 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6731 H_PUT_32 (abfd
, type
, xnp
->type
);
6732 strcpy (xnp
->name
, name
);
6733 memcpy (xnp
->name
+ BFD_ALIGN (namesz
, 4), input
, size
);
6737 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6739 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6747 char *note_name
= "CORE";
6749 #if defined (HAVE_PSINFO_T)
6751 note_type
= NT_PSINFO
;
6754 note_type
= NT_PRPSINFO
;
6757 memset (&data
, 0, sizeof (data
));
6758 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6759 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6760 return elfcore_write_note (abfd
, buf
, bufsiz
,
6761 note_name
, note_type
, &data
, sizeof (data
));
6763 #endif /* PSINFO_T or PRPSINFO_T */
6765 #if defined (HAVE_PRSTATUS_T)
6767 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6776 char *note_name
= "CORE";
6778 memset (&prstat
, 0, sizeof (prstat
));
6779 prstat
.pr_pid
= pid
;
6780 prstat
.pr_cursig
= cursig
;
6781 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
6782 return elfcore_write_note (abfd
, buf
, bufsiz
,
6783 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
6785 #endif /* HAVE_PRSTATUS_T */
6787 #if defined (HAVE_LWPSTATUS_T)
6789 elfcore_write_lwpstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6797 lwpstatus_t lwpstat
;
6798 char *note_name
= "CORE";
6800 memset (&lwpstat
, 0, sizeof (lwpstat
));
6801 lwpstat
.pr_lwpid
= pid
>> 16;
6802 lwpstat
.pr_cursig
= cursig
;
6803 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6804 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
6805 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6807 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
6808 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
6810 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
6811 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
6814 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6815 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
6817 #endif /* HAVE_LWPSTATUS_T */
6819 #if defined (HAVE_PSTATUS_T)
6821 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6830 char *note_name
= "CORE";
6832 memset (&pstat
, 0, sizeof (pstat
));
6833 pstat
.pr_pid
= pid
& 0xffff;
6834 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6835 NT_PSTATUS
, &pstat
, sizeof (pstat
));
6838 #endif /* HAVE_PSTATUS_T */
6841 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
6848 char *note_name
= "CORE";
6849 return elfcore_write_note (abfd
, buf
, bufsiz
,
6850 note_name
, NT_FPREGSET
, fpregs
, size
);
6854 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
6861 char *note_name
= "LINUX";
6862 return elfcore_write_note (abfd
, buf
, bufsiz
,
6863 note_name
, NT_PRXFPREG
, xfpregs
, size
);
6867 elfcore_read_notes (abfd
, offset
, size
)
6878 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6881 buf
= bfd_malloc (size
);
6885 if (bfd_bread (buf
, size
, abfd
) != size
)
6893 while (p
< buf
+ size
)
6895 /* FIXME: bad alignment assumption. */
6896 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6897 Elf_Internal_Note in
;
6899 in
.type
= H_GET_32 (abfd
, xnp
->type
);
6901 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
6902 in
.namedata
= xnp
->name
;
6904 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
6905 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
6906 in
.descpos
= offset
+ (in
.descdata
- buf
);
6908 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
6910 if (! elfcore_grok_netbsd_note (abfd
, &in
))
6915 if (! elfcore_grok_note (abfd
, &in
))
6919 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
6926 /* Providing external access to the ELF program header table. */
6928 /* Return an upper bound on the number of bytes required to store a
6929 copy of ABFD's program header table entries. Return -1 if an error
6930 occurs; bfd_get_error will return an appropriate code. */
6933 bfd_get_elf_phdr_upper_bound (abfd
)
6936 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6938 bfd_set_error (bfd_error_wrong_format
);
6942 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
6945 /* Copy ABFD's program header table entries to *PHDRS. The entries
6946 will be stored as an array of Elf_Internal_Phdr structures, as
6947 defined in include/elf/internal.h. To find out how large the
6948 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6950 Return the number of program header table entries read, or -1 if an
6951 error occurs; bfd_get_error will return an appropriate code. */
6954 bfd_get_elf_phdrs (abfd
, phdrs
)
6960 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6962 bfd_set_error (bfd_error_wrong_format
);
6966 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
6967 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
6968 num_phdrs
* sizeof (Elf_Internal_Phdr
));
6974 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
6975 bfd
*abfd ATTRIBUTE_UNUSED
;
6980 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6982 i_ehdrp
= elf_elfheader (abfd
);
6983 if (i_ehdrp
== NULL
)
6984 sprintf_vma (buf
, value
);
6987 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6989 #if BFD_HOST_64BIT_LONG
6990 sprintf (buf
, "%016lx", value
);
6992 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
6993 _bfd_int64_low (value
));
6997 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
7000 sprintf_vma (buf
, value
);
7005 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
7006 bfd
*abfd ATTRIBUTE_UNUSED
;
7011 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
7013 i_ehdrp
= elf_elfheader (abfd
);
7014 if (i_ehdrp
== NULL
)
7015 fprintf_vma ((FILE *) stream
, value
);
7018 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
7020 #if BFD_HOST_64BIT_LONG
7021 fprintf ((FILE *) stream
, "%016lx", value
);
7023 fprintf ((FILE *) stream
, "%08lx%08lx",
7024 _bfd_int64_high (value
), _bfd_int64_low (value
));
7028 fprintf ((FILE *) stream
, "%08lx",
7029 (unsigned long) (value
& 0xffffffff));
7032 fprintf_vma ((FILE *) stream
, value
);
7036 enum elf_reloc_type_class
7037 _bfd_elf_reloc_type_class (rela
)
7038 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
7040 return reloc_class_normal
;
7043 /* For RELA architectures, return what the relocation value for
7044 relocation against a local symbol. */
7047 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
7049 Elf_Internal_Sym
*sym
;
7051 Elf_Internal_Rela
*rel
;
7055 relocation
= (sec
->output_section
->vma
7056 + sec
->output_offset
7058 if ((sec
->flags
& SEC_MERGE
)
7059 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
7060 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
7066 _bfd_merged_section_offset (abfd
, &msec
,
7067 elf_section_data (sec
)->sec_info
,
7068 sym
->st_value
+ rel
->r_addend
,
7071 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7077 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
7079 Elf_Internal_Sym
*sym
;
7083 asection
*sec
= *psec
;
7085 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7086 return sym
->st_value
+ addend
;
7088 return _bfd_merged_section_offset (abfd
, psec
,
7089 elf_section_data (sec
)->sec_info
,
7090 sym
->st_value
+ addend
, (bfd_vma
) 0);
7094 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
7096 struct bfd_link_info
*info
;
7100 struct bfd_elf_section_data
*sec_data
;
7102 sec_data
= elf_section_data (sec
);
7103 switch (sec_data
->sec_info_type
)
7105 case ELF_INFO_TYPE_STABS
:
7106 return _bfd_stab_section_offset
7107 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
7109 case ELF_INFO_TYPE_EH_FRAME
:
7110 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);