1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
45 static INLINE
struct elf_segment_map
*make_mapping
46 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
47 static boolean map_sections_to_segments
PARAMS ((bfd
*));
48 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
49 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
50 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
51 static boolean prep_headers
PARAMS ((bfd
*));
52 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
53 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
54 static char *elf_read
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
55 static boolean setup_group
PARAMS ((bfd
*, Elf_Internal_Shdr
*, 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 /* Set next_in_group list pointer, and group name for NEWSECT. */
368 setup_group (abfd
, hdr
, newsect
)
370 Elf_Internal_Shdr
*hdr
;
373 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
375 /* If num_group is zero, read in all SHT_GROUP sections. The count
376 is set to -1 if there are no SHT_GROUP sections. */
379 unsigned int i
, shnum
;
381 /* First count the number of groups. If we have a SHT_GROUP
382 section with just a flag word (ie. sh_size is 4), ignore it. */
383 shnum
= elf_numsections (abfd
);
385 for (i
= 0; i
< shnum
; i
++)
387 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
388 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
393 num_group
= (unsigned) -1;
394 elf_tdata (abfd
)->num_group
= num_group
;
398 /* We keep a list of elf section headers for group sections,
399 so we can find them quickly. */
400 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
401 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
402 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
406 for (i
= 0; i
< shnum
; i
++)
408 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
409 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
412 Elf_Internal_Group
*dest
;
414 /* Add to list of sections. */
415 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
418 /* Read the raw contents. */
419 BFD_ASSERT (sizeof (*dest
) >= 4);
420 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
421 shdr
->contents
= bfd_alloc (abfd
, amt
);
422 if (shdr
->contents
== NULL
423 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
424 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
428 /* Translate raw contents, a flag word followed by an
429 array of elf section indices all in target byte order,
430 to the flag word followed by an array of elf section
432 src
= shdr
->contents
+ shdr
->sh_size
;
433 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
440 idx
= H_GET_32 (abfd
, src
);
441 if (src
== shdr
->contents
)
448 ((*_bfd_error_handler
)
449 (_("%s: invalid SHT_GROUP entry"),
450 bfd_archive_filename (abfd
)));
453 dest
->shdr
= elf_elfsections (abfd
)[idx
];
460 if (num_group
!= (unsigned) -1)
464 for (i
= 0; i
< num_group
; i
++)
466 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
467 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
468 unsigned int n_elt
= shdr
->sh_size
/ 4;
470 /* Look through this group's sections to see if current
471 section is a member. */
473 if ((++idx
)->shdr
== hdr
)
477 /* We are a member of this group. Go looking through
478 other members to see if any others are linked via
480 idx
= (Elf_Internal_Group
*) shdr
->contents
;
481 n_elt
= shdr
->sh_size
/ 4;
483 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
484 && elf_next_in_group (s
) != NULL
)
488 /* Snarf the group name from other member, and
489 insert current section in circular list. */
490 elf_group_name (newsect
) = elf_group_name (s
);
491 elf_next_in_group (newsect
) = elf_next_in_group (s
);
492 elf_next_in_group (s
) = newsect
;
496 struct elf_backend_data
*bed
;
498 unsigned char ename
[4];
502 /* Humbug. Get the name from the group signature
503 symbol. Why isn't the signature just a string?
504 Fortunately, the name index is at the same
505 place in the external symbol for both 32 and 64
507 bed
= get_elf_backend_data (abfd
);
508 pos
= elf_tdata (abfd
)->symtab_hdr
.sh_offset
;
509 pos
+= shdr
->sh_info
* bed
->s
->sizeof_sym
;
510 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
511 || bfd_bread (ename
, (bfd_size_type
) 4, abfd
) != 4)
513 iname
= H_GET_32 (abfd
, ename
);
514 gname
= elf_string_from_elf_strtab (abfd
, iname
);
515 elf_group_name (newsect
) = gname
;
517 /* Start a circular list with one element. */
518 elf_next_in_group (newsect
) = newsect
;
520 if (shdr
->bfd_section
!= NULL
)
521 elf_next_in_group (shdr
->bfd_section
) = newsect
;
528 if (elf_group_name (newsect
) == NULL
)
530 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
531 bfd_archive_filename (abfd
), newsect
->name
);
536 /* Make a BFD section from an ELF section. We store a pointer to the
537 BFD section in the bfd_section field of the header. */
540 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
542 Elf_Internal_Shdr
*hdr
;
547 struct elf_backend_data
*bed
;
549 if (hdr
->bfd_section
!= NULL
)
551 BFD_ASSERT (strcmp (name
,
552 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
556 newsect
= bfd_make_section_anyway (abfd
, name
);
560 newsect
->filepos
= hdr
->sh_offset
;
562 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
563 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
564 || ! bfd_set_section_alignment (abfd
, newsect
,
565 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
568 flags
= SEC_NO_FLAGS
;
569 if (hdr
->sh_type
!= SHT_NOBITS
)
570 flags
|= SEC_HAS_CONTENTS
;
571 if (hdr
->sh_type
== SHT_GROUP
)
572 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
573 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
576 if (hdr
->sh_type
!= SHT_NOBITS
)
579 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
580 flags
|= SEC_READONLY
;
581 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
583 else if ((flags
& SEC_LOAD
) != 0)
585 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
588 newsect
->entsize
= hdr
->sh_entsize
;
589 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
590 flags
|= SEC_STRINGS
;
592 if (hdr
->sh_flags
& SHF_GROUP
)
593 if (!setup_group (abfd
, hdr
, newsect
))
596 /* The debugging sections appear to be recognized only by name, not
599 static const char *debug_sec_names
[] =
608 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
609 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
613 flags
|= SEC_DEBUGGING
;
616 /* As a GNU extension, if the name begins with .gnu.linkonce, we
617 only link a single copy of the section. This is used to support
618 g++. g++ will emit each template expansion in its own section.
619 The symbols will be defined as weak, so that multiple definitions
620 are permitted. The GNU linker extension is to actually discard
621 all but one of the sections. */
622 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
623 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
625 bed
= get_elf_backend_data (abfd
);
626 if (bed
->elf_backend_section_flags
)
627 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
630 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
633 if ((flags
& SEC_ALLOC
) != 0)
635 Elf_Internal_Phdr
*phdr
;
638 /* Look through the phdrs to see if we need to adjust the lma.
639 If all the p_paddr fields are zero, we ignore them, since
640 some ELF linkers produce such output. */
641 phdr
= elf_tdata (abfd
)->phdr
;
642 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
644 if (phdr
->p_paddr
!= 0)
647 if (i
< elf_elfheader (abfd
)->e_phnum
)
649 phdr
= elf_tdata (abfd
)->phdr
;
650 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
652 /* This section is part of this segment if its file
653 offset plus size lies within the segment's memory
654 span and, if the section is loaded, the extent of the
655 loaded data lies within the extent of the segment.
656 If the p_paddr field is not set, we don't alter the
658 if (phdr
->p_type
== PT_LOAD
660 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
661 && (hdr
->sh_offset
+ hdr
->sh_size
662 <= phdr
->p_offset
+ phdr
->p_memsz
)
663 && ((flags
& SEC_LOAD
) == 0
664 || (phdr
->p_offset
+ phdr
->p_filesz
665 >= hdr
->sh_offset
+ hdr
->sh_size
)))
667 /* We used to do a relative adjustment here, but
668 that doesn't work if the segment is packed with
669 code from multiple VMAs. Instead we calculate
670 the LMA absoultely, based on the LMA of the
671 segment (it is assumed that the segment will
672 contain sections with contiguous LMAs, even if
673 the VMAs are not). */
674 newsect
->lma
= phdr
->p_paddr
675 + hdr
->sh_offset
- phdr
->p_offset
;
682 hdr
->bfd_section
= newsect
;
683 elf_section_data (newsect
)->this_hdr
= *hdr
;
693 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
696 Helper functions for GDB to locate the string tables.
697 Since BFD hides string tables from callers, GDB needs to use an
698 internal hook to find them. Sun's .stabstr, in particular,
699 isn't even pointed to by the .stab section, so ordinary
700 mechanisms wouldn't work to find it, even if we had some.
703 struct elf_internal_shdr
*
704 bfd_elf_find_section (abfd
, name
)
708 Elf_Internal_Shdr
**i_shdrp
;
713 i_shdrp
= elf_elfsections (abfd
);
716 shstrtab
= bfd_elf_get_str_section (abfd
,
717 elf_elfheader (abfd
)->e_shstrndx
);
718 if (shstrtab
!= NULL
)
720 max
= elf_numsections (abfd
);
721 for (i
= 1; i
< max
; i
++)
722 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
729 const char *const bfd_elf_section_type_names
[] = {
730 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
731 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
732 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
735 /* ELF relocs are against symbols. If we are producing relocateable
736 output, and the reloc is against an external symbol, and nothing
737 has given us any additional addend, the resulting reloc will also
738 be against the same symbol. In such a case, we don't want to
739 change anything about the way the reloc is handled, since it will
740 all be done at final link time. Rather than put special case code
741 into bfd_perform_relocation, all the reloc types use this howto
742 function. It just short circuits the reloc if producing
743 relocateable output against an external symbol. */
745 bfd_reloc_status_type
746 bfd_elf_generic_reloc (abfd
,
753 bfd
*abfd ATTRIBUTE_UNUSED
;
754 arelent
*reloc_entry
;
756 PTR data ATTRIBUTE_UNUSED
;
757 asection
*input_section
;
759 char **error_message ATTRIBUTE_UNUSED
;
761 if (output_bfd
!= (bfd
*) NULL
762 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
763 && (! reloc_entry
->howto
->partial_inplace
764 || reloc_entry
->addend
== 0))
766 reloc_entry
->address
+= input_section
->output_offset
;
770 return bfd_reloc_continue
;
773 /* Finish SHF_MERGE section merging. */
776 _bfd_elf_merge_sections (abfd
, info
)
778 struct bfd_link_info
*info
;
780 if (!is_elf_hash_table (info
))
782 if (elf_hash_table (info
)->merge_info
)
783 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
);
787 /* Print out the program headers. */
790 _bfd_elf_print_private_bfd_data (abfd
, farg
)
794 FILE *f
= (FILE *) farg
;
795 Elf_Internal_Phdr
*p
;
797 bfd_byte
*dynbuf
= NULL
;
799 p
= elf_tdata (abfd
)->phdr
;
804 fprintf (f
, _("\nProgram Header:\n"));
805 c
= elf_elfheader (abfd
)->e_phnum
;
806 for (i
= 0; i
< c
; i
++, p
++)
813 case PT_NULL
: pt
= "NULL"; break;
814 case PT_LOAD
: pt
= "LOAD"; break;
815 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
816 case PT_INTERP
: pt
= "INTERP"; break;
817 case PT_NOTE
: pt
= "NOTE"; break;
818 case PT_SHLIB
: pt
= "SHLIB"; break;
819 case PT_PHDR
: pt
= "PHDR"; break;
820 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
821 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
823 fprintf (f
, "%8s off 0x", pt
);
824 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
825 fprintf (f
, " vaddr 0x");
826 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
827 fprintf (f
, " paddr 0x");
828 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
829 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
830 fprintf (f
, " filesz 0x");
831 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
832 fprintf (f
, " memsz 0x");
833 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
834 fprintf (f
, " flags %c%c%c",
835 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
836 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
837 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
838 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
839 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
844 s
= bfd_get_section_by_name (abfd
, ".dynamic");
848 unsigned long shlink
;
849 bfd_byte
*extdyn
, *extdynend
;
851 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
853 fprintf (f
, _("\nDynamic Section:\n"));
855 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
858 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
862 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
865 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
867 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
868 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
871 extdynend
= extdyn
+ s
->_raw_size
;
872 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
874 Elf_Internal_Dyn dyn
;
879 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
881 if (dyn
.d_tag
== DT_NULL
)
888 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
892 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
893 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
894 case DT_PLTGOT
: name
= "PLTGOT"; break;
895 case DT_HASH
: name
= "HASH"; break;
896 case DT_STRTAB
: name
= "STRTAB"; break;
897 case DT_SYMTAB
: name
= "SYMTAB"; break;
898 case DT_RELA
: name
= "RELA"; break;
899 case DT_RELASZ
: name
= "RELASZ"; break;
900 case DT_RELAENT
: name
= "RELAENT"; break;
901 case DT_STRSZ
: name
= "STRSZ"; break;
902 case DT_SYMENT
: name
= "SYMENT"; break;
903 case DT_INIT
: name
= "INIT"; break;
904 case DT_FINI
: name
= "FINI"; break;
905 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
906 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
907 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
908 case DT_REL
: name
= "REL"; break;
909 case DT_RELSZ
: name
= "RELSZ"; break;
910 case DT_RELENT
: name
= "RELENT"; break;
911 case DT_PLTREL
: name
= "PLTREL"; break;
912 case DT_DEBUG
: name
= "DEBUG"; break;
913 case DT_TEXTREL
: name
= "TEXTREL"; break;
914 case DT_JMPREL
: name
= "JMPREL"; break;
915 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
916 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
917 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
918 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
919 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
920 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
921 case DT_FLAGS
: name
= "FLAGS"; break;
922 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
923 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
924 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
925 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
926 case DT_MOVEENT
: name
= "MOVEENT"; break;
927 case DT_MOVESZ
: name
= "MOVESZ"; break;
928 case DT_FEATURE
: name
= "FEATURE"; break;
929 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
930 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
931 case DT_SYMINENT
: name
= "SYMINENT"; break;
932 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
933 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
934 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
935 case DT_PLTPAD
: name
= "PLTPAD"; break;
936 case DT_MOVETAB
: name
= "MOVETAB"; break;
937 case DT_SYMINFO
: name
= "SYMINFO"; break;
938 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
939 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
940 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
941 case DT_VERSYM
: name
= "VERSYM"; break;
942 case DT_VERDEF
: name
= "VERDEF"; break;
943 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
944 case DT_VERNEED
: name
= "VERNEED"; break;
945 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
946 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
947 case DT_USED
: name
= "USED"; break;
948 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
951 fprintf (f
, " %-11s ", name
);
953 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
957 unsigned int tagv
= dyn
.d_un
.d_val
;
959 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
962 fprintf (f
, "%s", string
);
971 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
972 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
974 if (! _bfd_elf_slurp_version_tables (abfd
))
978 if (elf_dynverdef (abfd
) != 0)
980 Elf_Internal_Verdef
*t
;
982 fprintf (f
, _("\nVersion definitions:\n"));
983 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
985 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
986 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
987 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
989 Elf_Internal_Verdaux
*a
;
992 for (a
= t
->vd_auxptr
->vda_nextptr
;
995 fprintf (f
, "%s ", a
->vda_nodename
);
1001 if (elf_dynverref (abfd
) != 0)
1003 Elf_Internal_Verneed
*t
;
1005 fprintf (f
, _("\nVersion References:\n"));
1006 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1008 Elf_Internal_Vernaux
*a
;
1010 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1011 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1012 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1013 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1025 /* Display ELF-specific fields of a symbol. */
1028 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1032 bfd_print_symbol_type how
;
1034 FILE *file
= (FILE *) filep
;
1037 case bfd_print_symbol_name
:
1038 fprintf (file
, "%s", symbol
->name
);
1040 case bfd_print_symbol_more
:
1041 fprintf (file
, "elf ");
1042 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1043 fprintf (file
, " %lx", (long) symbol
->flags
);
1045 case bfd_print_symbol_all
:
1047 const char *section_name
;
1048 const char *name
= NULL
;
1049 struct elf_backend_data
*bed
;
1050 unsigned char st_other
;
1053 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1055 bed
= get_elf_backend_data (abfd
);
1056 if (bed
->elf_backend_print_symbol_all
)
1057 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1061 name
= symbol
->name
;
1062 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1065 fprintf (file
, " %s\t", section_name
);
1066 /* Print the "other" value for a symbol. For common symbols,
1067 we've already printed the size; now print the alignment.
1068 For other symbols, we have no specified alignment, and
1069 we've printed the address; now print the size. */
1070 if (bfd_is_com_section (symbol
->section
))
1071 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1073 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1074 bfd_fprintf_vma (abfd
, file
, val
);
1076 /* If we have version information, print it. */
1077 if (elf_tdata (abfd
)->dynversym_section
!= 0
1078 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1079 || elf_tdata (abfd
)->dynverref_section
!= 0))
1081 unsigned int vernum
;
1082 const char *version_string
;
1084 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1087 version_string
= "";
1088 else if (vernum
== 1)
1089 version_string
= "Base";
1090 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1092 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1095 Elf_Internal_Verneed
*t
;
1097 version_string
= "";
1098 for (t
= elf_tdata (abfd
)->verref
;
1102 Elf_Internal_Vernaux
*a
;
1104 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1106 if (a
->vna_other
== vernum
)
1108 version_string
= a
->vna_nodename
;
1115 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1116 fprintf (file
, " %-11s", version_string
);
1121 fprintf (file
, " (%s)", version_string
);
1122 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1127 /* If the st_other field is not zero, print it. */
1128 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1133 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1134 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1135 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1137 /* Some other non-defined flags are also present, so print
1139 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1142 fprintf (file
, " %s", name
);
1148 /* Create an entry in an ELF linker hash table. */
1150 struct bfd_hash_entry
*
1151 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1152 struct bfd_hash_entry
*entry
;
1153 struct bfd_hash_table
*table
;
1156 /* Allocate the structure if it has not already been allocated by a
1160 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1165 /* Call the allocation method of the superclass. */
1166 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1169 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1170 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1172 /* Set local fields. */
1176 ret
->dynstr_index
= 0;
1177 ret
->weakdef
= NULL
;
1178 ret
->got
.refcount
= htab
->init_refcount
;
1179 ret
->plt
.refcount
= htab
->init_refcount
;
1180 ret
->linker_section_pointer
= NULL
;
1181 ret
->verinfo
.verdef
= NULL
;
1182 ret
->vtable_entries_used
= NULL
;
1183 ret
->vtable_entries_size
= 0;
1184 ret
->vtable_parent
= NULL
;
1185 ret
->type
= STT_NOTYPE
;
1187 /* Assume that we have been called by a non-ELF symbol reader.
1188 This flag is then reset by the code which reads an ELF input
1189 file. This ensures that a symbol created by a non-ELF symbol
1190 reader will have the flag set correctly. */
1191 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1197 /* Copy data from an indirect symbol to its direct symbol, hiding the
1198 old indirect symbol. Also used for copying flags to a weakdef. */
1201 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1202 struct elf_link_hash_entry
*dir
, *ind
;
1206 /* Copy down any references that we may have already seen to the
1207 symbol which just became indirect. */
1209 dir
->elf_link_hash_flags
|=
1210 (ind
->elf_link_hash_flags
1211 & (ELF_LINK_HASH_REF_DYNAMIC
1212 | ELF_LINK_HASH_REF_REGULAR
1213 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1214 | ELF_LINK_NON_GOT_REF
));
1216 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1219 /* Copy over the global and procedure linkage table refcount entries.
1220 These may have been already set up by a check_relocs routine. */
1221 tmp
= dir
->got
.refcount
;
1224 dir
->got
.refcount
= ind
->got
.refcount
;
1225 ind
->got
.refcount
= tmp
;
1228 BFD_ASSERT (ind
->got
.refcount
<= 0);
1230 tmp
= dir
->plt
.refcount
;
1233 dir
->plt
.refcount
= ind
->plt
.refcount
;
1234 ind
->plt
.refcount
= tmp
;
1237 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1239 if (dir
->dynindx
== -1)
1241 dir
->dynindx
= ind
->dynindx
;
1242 dir
->dynstr_index
= ind
->dynstr_index
;
1244 ind
->dynstr_index
= 0;
1247 BFD_ASSERT (ind
->dynindx
== -1);
1251 _bfd_elf_link_hash_hide_symbol (info
, h
)
1252 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1253 struct elf_link_hash_entry
*h
;
1255 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1256 h
->plt
.offset
= (bfd_vma
) -1;
1257 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1261 /* Initialize an ELF linker hash table. */
1264 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1265 struct elf_link_hash_table
*table
;
1267 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1268 struct bfd_hash_table
*,
1273 table
->dynamic_sections_created
= false;
1274 table
->dynobj
= NULL
;
1275 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1276 /* The first dynamic symbol is a dummy. */
1277 table
->dynsymcount
= 1;
1278 table
->dynstr
= NULL
;
1279 table
->bucketcount
= 0;
1280 table
->needed
= NULL
;
1281 table
->runpath
= NULL
;
1283 table
->stab_info
= NULL
;
1284 table
->merge_info
= NULL
;
1285 table
->dynlocal
= NULL
;
1286 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1287 table
->root
.type
= bfd_link_elf_hash_table
;
1292 /* Create an ELF linker hash table. */
1294 struct bfd_link_hash_table
*
1295 _bfd_elf_link_hash_table_create (abfd
)
1298 struct elf_link_hash_table
*ret
;
1299 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1301 ret
= (struct elf_link_hash_table
*) bfd_alloc (abfd
, amt
);
1302 if (ret
== (struct elf_link_hash_table
*) NULL
)
1305 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1307 bfd_release (abfd
, ret
);
1314 /* This is a hook for the ELF emulation code in the generic linker to
1315 tell the backend linker what file name to use for the DT_NEEDED
1316 entry for a dynamic object. The generic linker passes name as an
1317 empty string to indicate that no DT_NEEDED entry should be made. */
1320 bfd_elf_set_dt_needed_name (abfd
, name
)
1324 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1325 && bfd_get_format (abfd
) == bfd_object
)
1326 elf_dt_name (abfd
) = name
;
1330 bfd_elf_set_dt_needed_soname (abfd
, name
)
1334 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1335 && bfd_get_format (abfd
) == bfd_object
)
1336 elf_dt_soname (abfd
) = name
;
1339 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1340 the linker ELF emulation code. */
1342 struct bfd_link_needed_list
*
1343 bfd_elf_get_needed_list (abfd
, info
)
1344 bfd
*abfd ATTRIBUTE_UNUSED
;
1345 struct bfd_link_info
*info
;
1347 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1349 return elf_hash_table (info
)->needed
;
1352 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1353 hook for the linker ELF emulation code. */
1355 struct bfd_link_needed_list
*
1356 bfd_elf_get_runpath_list (abfd
, info
)
1357 bfd
*abfd ATTRIBUTE_UNUSED
;
1358 struct bfd_link_info
*info
;
1360 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1362 return elf_hash_table (info
)->runpath
;
1365 /* Get the name actually used for a dynamic object for a link. This
1366 is the SONAME entry if there is one. Otherwise, it is the string
1367 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1370 bfd_elf_get_dt_soname (abfd
)
1373 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1374 && bfd_get_format (abfd
) == bfd_object
)
1375 return elf_dt_name (abfd
);
1379 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1380 the ELF linker emulation code. */
1383 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1385 struct bfd_link_needed_list
**pneeded
;
1388 bfd_byte
*dynbuf
= NULL
;
1390 unsigned long shlink
;
1391 bfd_byte
*extdyn
, *extdynend
;
1393 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1397 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1398 || bfd_get_format (abfd
) != bfd_object
)
1401 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1402 if (s
== NULL
|| s
->_raw_size
== 0)
1405 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1409 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1413 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1417 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1419 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1420 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1423 extdynend
= extdyn
+ s
->_raw_size
;
1424 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1426 Elf_Internal_Dyn dyn
;
1428 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1430 if (dyn
.d_tag
== DT_NULL
)
1433 if (dyn
.d_tag
== DT_NEEDED
)
1436 struct bfd_link_needed_list
*l
;
1437 unsigned int tagv
= dyn
.d_un
.d_val
;
1440 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1445 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1466 /* Allocate an ELF string table--force the first byte to be zero. */
1468 struct bfd_strtab_hash
*
1469 _bfd_elf_stringtab_init ()
1471 struct bfd_strtab_hash
*ret
;
1473 ret
= _bfd_stringtab_init ();
1478 loc
= _bfd_stringtab_add (ret
, "", true, false);
1479 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1480 if (loc
== (bfd_size_type
) -1)
1482 _bfd_stringtab_free (ret
);
1489 /* ELF .o/exec file reading */
1491 /* Create a new bfd section from an ELF section header. */
1494 bfd_section_from_shdr (abfd
, shindex
)
1496 unsigned int shindex
;
1498 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1499 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1500 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1503 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1505 switch (hdr
->sh_type
)
1508 /* Inactive section. Throw it away. */
1511 case SHT_PROGBITS
: /* Normal section with contents. */
1512 case SHT_DYNAMIC
: /* Dynamic linking information. */
1513 case SHT_NOBITS
: /* .bss section. */
1514 case SHT_HASH
: /* .hash section. */
1515 case SHT_NOTE
: /* .note section. */
1516 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1518 case SHT_SYMTAB
: /* A symbol table */
1519 if (elf_onesymtab (abfd
) == shindex
)
1522 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1523 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1524 elf_onesymtab (abfd
) = shindex
;
1525 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1526 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1527 abfd
->flags
|= HAS_SYMS
;
1529 /* Sometimes a shared object will map in the symbol table. If
1530 SHF_ALLOC is set, and this is a shared object, then we also
1531 treat this section as a BFD section. We can not base the
1532 decision purely on SHF_ALLOC, because that flag is sometimes
1533 set in a relocateable object file, which would confuse the
1535 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1536 && (abfd
->flags
& DYNAMIC
) != 0
1537 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1542 case SHT_DYNSYM
: /* A dynamic symbol table */
1543 if (elf_dynsymtab (abfd
) == shindex
)
1546 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1547 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1548 elf_dynsymtab (abfd
) = shindex
;
1549 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1550 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1551 abfd
->flags
|= HAS_SYMS
;
1553 /* Besides being a symbol table, we also treat this as a regular
1554 section, so that objcopy can handle it. */
1555 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1557 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1558 if (elf_symtab_shndx (abfd
) == shindex
)
1561 /* Get the associated symbol table. */
1562 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1563 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1566 elf_symtab_shndx (abfd
) = shindex
;
1567 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1568 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1571 case SHT_STRTAB
: /* A string table */
1572 if (hdr
->bfd_section
!= NULL
)
1574 if (ehdr
->e_shstrndx
== shindex
)
1576 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1577 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1581 unsigned int i
, num_sec
;
1583 num_sec
= elf_numsections (abfd
);
1584 for (i
= 1; i
< num_sec
; i
++)
1586 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1587 if (hdr2
->sh_link
== shindex
)
1589 if (! bfd_section_from_shdr (abfd
, i
))
1591 if (elf_onesymtab (abfd
) == i
)
1593 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1594 elf_elfsections (abfd
)[shindex
] =
1595 &elf_tdata (abfd
)->strtab_hdr
;
1598 if (elf_dynsymtab (abfd
) == i
)
1600 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1601 elf_elfsections (abfd
)[shindex
] = hdr
=
1602 &elf_tdata (abfd
)->dynstrtab_hdr
;
1603 /* We also treat this as a regular section, so
1604 that objcopy can handle it. */
1607 #if 0 /* Not handling other string tables specially right now. */
1608 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1609 /* We have a strtab for some random other section. */
1610 newsect
= (asection
*) hdr2
->bfd_section
;
1613 hdr
->bfd_section
= newsect
;
1614 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1616 elf_elfsections (abfd
)[shindex
] = hdr2
;
1622 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1626 /* *These* do a lot of work -- but build no sections! */
1628 asection
*target_sect
;
1629 Elf_Internal_Shdr
*hdr2
;
1630 unsigned int num_sec
= elf_numsections (abfd
);
1632 /* Check for a bogus link to avoid crashing. */
1633 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1634 || hdr
->sh_link
>= num_sec
)
1636 ((*_bfd_error_handler
)
1637 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1638 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1639 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1642 /* For some incomprehensible reason Oracle distributes
1643 libraries for Solaris in which some of the objects have
1644 bogus sh_link fields. It would be nice if we could just
1645 reject them, but, unfortunately, some people need to use
1646 them. We scan through the section headers; if we find only
1647 one suitable symbol table, we clobber the sh_link to point
1648 to it. I hope this doesn't break anything. */
1649 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1650 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1656 for (scan
= 1; scan
< num_sec
; scan
++)
1658 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1659 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1670 hdr
->sh_link
= found
;
1673 /* Get the symbol table. */
1674 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1675 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1678 /* If this reloc section does not use the main symbol table we
1679 don't treat it as a reloc section. BFD can't adequately
1680 represent such a section, so at least for now, we don't
1681 try. We just present it as a normal section. We also
1682 can't use it as a reloc section if it points to the null
1684 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1685 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1687 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1689 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1690 if (target_sect
== NULL
)
1693 if ((target_sect
->flags
& SEC_RELOC
) == 0
1694 || target_sect
->reloc_count
== 0)
1695 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1699 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1700 amt
= sizeof (*hdr2
);
1701 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1702 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1705 elf_elfsections (abfd
)[shindex
] = hdr2
;
1706 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1707 target_sect
->flags
|= SEC_RELOC
;
1708 target_sect
->relocation
= NULL
;
1709 target_sect
->rel_filepos
= hdr
->sh_offset
;
1710 /* In the section to which the relocations apply, mark whether
1711 its relocations are of the REL or RELA variety. */
1712 if (hdr
->sh_size
!= 0)
1713 elf_section_data (target_sect
)->use_rela_p
1714 = (hdr
->sh_type
== SHT_RELA
);
1715 abfd
->flags
|= HAS_RELOC
;
1720 case SHT_GNU_verdef
:
1721 elf_dynverdef (abfd
) = shindex
;
1722 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1723 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1726 case SHT_GNU_versym
:
1727 elf_dynversym (abfd
) = shindex
;
1728 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1729 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1732 case SHT_GNU_verneed
:
1733 elf_dynverref (abfd
) = shindex
;
1734 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1735 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1742 /* Make a section for objcopy and relocatable links. */
1743 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1745 if (hdr
->contents
!= NULL
)
1747 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1748 unsigned int n_elt
= hdr
->sh_size
/ 4;
1751 while (--n_elt
!= 0)
1752 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1753 && elf_next_in_group (s
) != NULL
)
1755 elf_next_in_group (hdr
->bfd_section
) = s
;
1762 /* Check for any processor-specific section types. */
1764 if (bed
->elf_backend_section_from_shdr
)
1765 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1773 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1774 Return SEC for sections that have no elf section, and NULL on error. */
1777 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
1779 struct sym_sec_cache
*cache
;
1781 unsigned long r_symndx
;
1783 unsigned char esym_shndx
[4];
1784 unsigned int isym_shndx
;
1785 Elf_Internal_Shdr
*symtab_hdr
;
1788 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1790 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1791 return cache
->sec
[ent
];
1793 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1794 pos
= symtab_hdr
->sh_offset
;
1795 if (get_elf_backend_data (abfd
)->s
->sizeof_sym
1796 == sizeof (Elf64_External_Sym
))
1798 pos
+= r_symndx
* sizeof (Elf64_External_Sym
);
1799 pos
+= offsetof (Elf64_External_Sym
, st_shndx
);
1800 amt
= sizeof (((Elf64_External_Sym
*) 0)->st_shndx
);
1804 pos
+= r_symndx
* sizeof (Elf32_External_Sym
);
1805 pos
+= offsetof (Elf32_External_Sym
, st_shndx
);
1806 amt
= sizeof (((Elf32_External_Sym
*) 0)->st_shndx
);
1808 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1809 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1811 isym_shndx
= H_GET_16 (abfd
, esym_shndx
);
1813 if (isym_shndx
== SHN_XINDEX
)
1815 Elf_Internal_Shdr
*shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1816 if (shndx_hdr
->sh_size
!= 0)
1818 pos
= shndx_hdr
->sh_offset
;
1819 pos
+= r_symndx
* sizeof (Elf_External_Sym_Shndx
);
1820 amt
= sizeof (Elf_External_Sym_Shndx
);
1821 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1822 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1824 isym_shndx
= H_GET_32 (abfd
, esym_shndx
);
1828 if (cache
->abfd
!= abfd
)
1830 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1833 cache
->indx
[ent
] = r_symndx
;
1834 cache
->sec
[ent
] = sec
;
1835 if (isym_shndx
< SHN_LORESERVE
|| isym_shndx
> SHN_HIRESERVE
)
1838 s
= bfd_section_from_elf_index (abfd
, isym_shndx
);
1840 cache
->sec
[ent
] = s
;
1842 return cache
->sec
[ent
];
1845 /* Given an ELF section number, retrieve the corresponding BFD
1849 bfd_section_from_elf_index (abfd
, index
)
1853 if (index
>= elf_numsections (abfd
))
1855 return elf_elfsections (abfd
)[index
]->bfd_section
;
1859 _bfd_elf_new_section_hook (abfd
, sec
)
1863 struct bfd_elf_section_data
*sdata
;
1864 bfd_size_type amt
= sizeof (*sdata
);
1866 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1869 sec
->used_by_bfd
= (PTR
) sdata
;
1871 /* Indicate whether or not this section should use RELA relocations. */
1873 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1878 /* Create a new bfd section from an ELF program header.
1880 Since program segments have no names, we generate a synthetic name
1881 of the form segment<NUM>, where NUM is generally the index in the
1882 program header table. For segments that are split (see below) we
1883 generate the names segment<NUM>a and segment<NUM>b.
1885 Note that some program segments may have a file size that is different than
1886 (less than) the memory size. All this means is that at execution the
1887 system must allocate the amount of memory specified by the memory size,
1888 but only initialize it with the first "file size" bytes read from the
1889 file. This would occur for example, with program segments consisting
1890 of combined data+bss.
1892 To handle the above situation, this routine generates TWO bfd sections
1893 for the single program segment. The first has the length specified by
1894 the file size of the segment, and the second has the length specified
1895 by the difference between the two sizes. In effect, the segment is split
1896 into it's initialized and uninitialized parts.
1901 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1903 Elf_Internal_Phdr
*hdr
;
1905 const char *typename
;
1912 split
= ((hdr
->p_memsz
> 0)
1913 && (hdr
->p_filesz
> 0)
1914 && (hdr
->p_memsz
> hdr
->p_filesz
));
1915 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1916 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1919 strcpy (name
, namebuf
);
1920 newsect
= bfd_make_section (abfd
, name
);
1921 if (newsect
== NULL
)
1923 newsect
->vma
= hdr
->p_vaddr
;
1924 newsect
->lma
= hdr
->p_paddr
;
1925 newsect
->_raw_size
= hdr
->p_filesz
;
1926 newsect
->filepos
= hdr
->p_offset
;
1927 newsect
->flags
|= SEC_HAS_CONTENTS
;
1928 if (hdr
->p_type
== PT_LOAD
)
1930 newsect
->flags
|= SEC_ALLOC
;
1931 newsect
->flags
|= SEC_LOAD
;
1932 if (hdr
->p_flags
& PF_X
)
1934 /* FIXME: all we known is that it has execute PERMISSION,
1936 newsect
->flags
|= SEC_CODE
;
1939 if (!(hdr
->p_flags
& PF_W
))
1941 newsect
->flags
|= SEC_READONLY
;
1946 sprintf (namebuf
, "%s%db", typename
, index
);
1947 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1950 strcpy (name
, namebuf
);
1951 newsect
= bfd_make_section (abfd
, name
);
1952 if (newsect
== NULL
)
1954 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1955 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1956 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1957 if (hdr
->p_type
== PT_LOAD
)
1959 newsect
->flags
|= SEC_ALLOC
;
1960 if (hdr
->p_flags
& PF_X
)
1961 newsect
->flags
|= SEC_CODE
;
1963 if (!(hdr
->p_flags
& PF_W
))
1964 newsect
->flags
|= SEC_READONLY
;
1971 bfd_section_from_phdr (abfd
, hdr
, index
)
1973 Elf_Internal_Phdr
*hdr
;
1976 struct elf_backend_data
*bed
;
1978 switch (hdr
->p_type
)
1981 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1984 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1987 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1990 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1993 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1995 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2000 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2003 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2006 /* Check for any processor-specific program segment types.
2007 If no handler for them, default to making "segment" sections. */
2008 bed
= get_elf_backend_data (abfd
);
2009 if (bed
->elf_backend_section_from_phdr
)
2010 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2012 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2016 /* Initialize REL_HDR, the section-header for new section, containing
2017 relocations against ASECT. If USE_RELA_P is true, we use RELA
2018 relocations; otherwise, we use REL relocations. */
2021 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2023 Elf_Internal_Shdr
*rel_hdr
;
2028 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2029 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2031 name
= bfd_alloc (abfd
, amt
);
2034 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2036 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2038 if (rel_hdr
->sh_name
== (unsigned int) -1)
2040 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2041 rel_hdr
->sh_entsize
= (use_rela_p
2042 ? bed
->s
->sizeof_rela
2043 : bed
->s
->sizeof_rel
);
2044 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2045 rel_hdr
->sh_flags
= 0;
2046 rel_hdr
->sh_addr
= 0;
2047 rel_hdr
->sh_size
= 0;
2048 rel_hdr
->sh_offset
= 0;
2053 /* Set up an ELF internal section header for a section. */
2056 elf_fake_sections (abfd
, asect
, failedptrarg
)
2061 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2062 boolean
*failedptr
= (boolean
*) failedptrarg
;
2063 Elf_Internal_Shdr
*this_hdr
;
2067 /* We already failed; just get out of the bfd_map_over_sections
2072 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2074 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2075 asect
->name
, false);
2076 if (this_hdr
->sh_name
== (unsigned long) -1)
2082 this_hdr
->sh_flags
= 0;
2084 if ((asect
->flags
& SEC_ALLOC
) != 0
2085 || asect
->user_set_vma
)
2086 this_hdr
->sh_addr
= asect
->vma
;
2088 this_hdr
->sh_addr
= 0;
2090 this_hdr
->sh_offset
= 0;
2091 this_hdr
->sh_size
= asect
->_raw_size
;
2092 this_hdr
->sh_link
= 0;
2093 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2094 /* The sh_entsize and sh_info fields may have been set already by
2095 copy_private_section_data. */
2097 this_hdr
->bfd_section
= asect
;
2098 this_hdr
->contents
= NULL
;
2100 /* FIXME: This should not be based on section names. */
2101 if (strcmp (asect
->name
, ".dynstr") == 0)
2102 this_hdr
->sh_type
= SHT_STRTAB
;
2103 else if (strcmp (asect
->name
, ".hash") == 0)
2105 this_hdr
->sh_type
= SHT_HASH
;
2106 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2108 else if (strcmp (asect
->name
, ".dynsym") == 0)
2110 this_hdr
->sh_type
= SHT_DYNSYM
;
2111 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2113 else if (strcmp (asect
->name
, ".dynamic") == 0)
2115 this_hdr
->sh_type
= SHT_DYNAMIC
;
2116 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2118 else if (strncmp (asect
->name
, ".rela", 5) == 0
2119 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2121 this_hdr
->sh_type
= SHT_RELA
;
2122 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2124 else if (strncmp (asect
->name
, ".rel", 4) == 0
2125 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2127 this_hdr
->sh_type
= SHT_REL
;
2128 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2130 else if (strncmp (asect
->name
, ".note", 5) == 0)
2131 this_hdr
->sh_type
= SHT_NOTE
;
2132 else if (strncmp (asect
->name
, ".stab", 5) == 0
2133 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2134 this_hdr
->sh_type
= SHT_STRTAB
;
2135 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2137 this_hdr
->sh_type
= SHT_GNU_versym
;
2138 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2140 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2142 this_hdr
->sh_type
= SHT_GNU_verdef
;
2143 this_hdr
->sh_entsize
= 0;
2144 /* objcopy or strip will copy over sh_info, but may not set
2145 cverdefs. The linker will set cverdefs, but sh_info will be
2147 if (this_hdr
->sh_info
== 0)
2148 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2150 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2151 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2153 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2155 this_hdr
->sh_type
= SHT_GNU_verneed
;
2156 this_hdr
->sh_entsize
= 0;
2157 /* objcopy or strip will copy over sh_info, but may not set
2158 cverrefs. The linker will set cverrefs, but sh_info will be
2160 if (this_hdr
->sh_info
== 0)
2161 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2163 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2164 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2166 else if ((asect
->flags
& SEC_GROUP
) != 0)
2168 this_hdr
->sh_type
= SHT_GROUP
;
2169 this_hdr
->sh_entsize
= 4;
2171 else if ((asect
->flags
& SEC_ALLOC
) != 0
2172 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
2173 this_hdr
->sh_type
= SHT_NOBITS
;
2175 this_hdr
->sh_type
= SHT_PROGBITS
;
2177 if ((asect
->flags
& SEC_ALLOC
) != 0)
2178 this_hdr
->sh_flags
|= SHF_ALLOC
;
2179 if ((asect
->flags
& SEC_READONLY
) == 0)
2180 this_hdr
->sh_flags
|= SHF_WRITE
;
2181 if ((asect
->flags
& SEC_CODE
) != 0)
2182 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2183 if ((asect
->flags
& SEC_MERGE
) != 0)
2185 this_hdr
->sh_flags
|= SHF_MERGE
;
2186 this_hdr
->sh_entsize
= asect
->entsize
;
2187 if ((asect
->flags
& SEC_STRINGS
) != 0)
2188 this_hdr
->sh_flags
|= SHF_STRINGS
;
2190 if (elf_group_name (asect
) != NULL
)
2191 this_hdr
->sh_flags
|= SHF_GROUP
;
2193 /* Check for processor-specific section types. */
2194 if (bed
->elf_backend_fake_sections
)
2195 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
2197 /* If the section has relocs, set up a section header for the
2198 SHT_REL[A] section. If two relocation sections are required for
2199 this section, it is up to the processor-specific back-end to
2200 create the other. */
2201 if ((asect
->flags
& SEC_RELOC
) != 0
2202 && !_bfd_elf_init_reloc_shdr (abfd
,
2203 &elf_section_data (asect
)->rel_hdr
,
2205 elf_section_data (asect
)->use_rela_p
))
2209 /* Fill in the contents of a SHT_GROUP section. */
2212 set_group_contents (abfd
, sec
, failedptrarg
)
2215 PTR failedptrarg ATTRIBUTE_UNUSED
;
2217 boolean
*failedptr
= (boolean
*) failedptrarg
;
2218 unsigned long symindx
;
2221 struct bfd_link_order
*l
;
2223 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2227 /* If called from the assembler, swap_out_syms will have set up
2228 elf_section_syms; If called for "ld -r", the symbols won't yet
2229 be mapped, so emulate elf_bfd_final_link. */
2230 if (elf_section_syms (abfd
) != NULL
)
2231 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2233 symindx
= elf_section_data (sec
)->this_idx
;
2234 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2236 /* Nor will the contents be allocated for "ld -r". */
2237 if (sec
->contents
== NULL
)
2239 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2240 if (sec
->contents
== NULL
)
2247 loc
= sec
->contents
+ sec
->_raw_size
;
2249 /* Get the pointer to the first section in the group that we
2250 squirreled away here. */
2251 elt
= elf_next_in_group (sec
);
2253 /* First element is a flag word. Rest of section is elf section
2254 indices for all the sections of the group. Write them backwards
2255 just to keep the group in the same order as given in .section
2256 directives, not that it matters. */
2260 H_PUT_32 (abfd
, elf_section_data (elt
)->this_idx
, loc
);
2261 elt
= elf_next_in_group (elt
);
2264 /* If this is a relocatable link, then the above did nothing because
2265 SEC is the output section. Look through the input sections
2267 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2268 if (l
->type
== bfd_indirect_link_order
2269 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2274 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2275 elt
= elf_next_in_group (elt
);
2276 /* During a relocatable link, the lists are circular. */
2278 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2281 H_PUT_32 (abfd
, 0, loc
);
2283 BFD_ASSERT (loc
== sec
->contents
);
2286 /* Assign all ELF section numbers. The dummy first section is handled here
2287 too. The link/info pointers for the standard section types are filled
2288 in here too, while we're at it. */
2291 assign_section_numbers (abfd
)
2294 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2296 unsigned int section_number
, secn
;
2297 Elf_Internal_Shdr
**i_shdrp
;
2302 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2304 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2306 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2308 if (section_number
== SHN_LORESERVE
)
2309 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2310 d
->this_idx
= section_number
++;
2311 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2312 if ((sec
->flags
& SEC_RELOC
) == 0)
2316 if (section_number
== SHN_LORESERVE
)
2317 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2318 d
->rel_idx
= section_number
++;
2319 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2324 if (section_number
== SHN_LORESERVE
)
2325 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2326 d
->rel_idx2
= section_number
++;
2327 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2333 if (section_number
== SHN_LORESERVE
)
2334 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2335 t
->shstrtab_section
= section_number
++;
2336 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2337 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2339 if (bfd_get_symcount (abfd
) > 0)
2341 if (section_number
== SHN_LORESERVE
)
2342 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2343 t
->symtab_section
= section_number
++;
2344 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2345 if (section_number
> SHN_LORESERVE
- 2)
2347 if (section_number
== SHN_LORESERVE
)
2348 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2349 t
->symtab_shndx_section
= section_number
++;
2350 t
->symtab_shndx_hdr
.sh_name
2351 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2352 ".symtab_shndx", false);
2353 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2356 if (section_number
== SHN_LORESERVE
)
2357 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2358 t
->strtab_section
= section_number
++;
2359 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2362 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2363 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2365 elf_numsections (abfd
) = section_number
;
2366 elf_elfheader (abfd
)->e_shnum
= section_number
;
2367 if (section_number
> SHN_LORESERVE
)
2368 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2370 /* Set up the list of section header pointers, in agreement with the
2372 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2373 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2374 if (i_shdrp
== NULL
)
2377 amt
= sizeof (Elf_Internal_Shdr
);
2378 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2379 if (i_shdrp
[0] == NULL
)
2381 bfd_release (abfd
, i_shdrp
);
2384 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2386 elf_elfsections (abfd
) = i_shdrp
;
2388 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2389 if (bfd_get_symcount (abfd
) > 0)
2391 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2392 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2394 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2395 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2397 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2398 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2400 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2402 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2406 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2407 if (d
->rel_idx
!= 0)
2408 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2409 if (d
->rel_idx2
!= 0)
2410 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2412 /* Fill in the sh_link and sh_info fields while we're at it. */
2414 /* sh_link of a reloc section is the section index of the symbol
2415 table. sh_info is the section index of the section to which
2416 the relocation entries apply. */
2417 if (d
->rel_idx
!= 0)
2419 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2420 d
->rel_hdr
.sh_info
= d
->this_idx
;
2422 if (d
->rel_idx2
!= 0)
2424 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2425 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2428 switch (d
->this_hdr
.sh_type
)
2432 /* A reloc section which we are treating as a normal BFD
2433 section. sh_link is the section index of the symbol
2434 table. sh_info is the section index of the section to
2435 which the relocation entries apply. We assume that an
2436 allocated reloc section uses the dynamic symbol table.
2437 FIXME: How can we be sure? */
2438 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2440 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2442 /* We look up the section the relocs apply to by name. */
2444 if (d
->this_hdr
.sh_type
== SHT_REL
)
2448 s
= bfd_get_section_by_name (abfd
, name
);
2450 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2454 /* We assume that a section named .stab*str is a stabs
2455 string section. We look for a section with the same name
2456 but without the trailing ``str'', and set its sh_link
2457 field to point to this section. */
2458 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2459 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2464 len
= strlen (sec
->name
);
2465 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2468 strncpy (alc
, sec
->name
, len
- 3);
2469 alc
[len
- 3] = '\0';
2470 s
= bfd_get_section_by_name (abfd
, alc
);
2474 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2476 /* This is a .stab section. */
2477 elf_section_data (s
)->this_hdr
.sh_entsize
=
2478 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2485 case SHT_GNU_verneed
:
2486 case SHT_GNU_verdef
:
2487 /* sh_link is the section header index of the string table
2488 used for the dynamic entries, or the symbol table, or the
2490 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2492 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2496 case SHT_GNU_versym
:
2497 /* sh_link is the section header index of the symbol table
2498 this hash table or version table is for. */
2499 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2501 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2505 d
->this_hdr
.sh_link
= t
->symtab_section
;
2509 for (secn
= 1; secn
< section_number
; ++secn
)
2510 if (i_shdrp
[secn
] == NULL
)
2511 i_shdrp
[secn
] = i_shdrp
[0];
2513 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2514 i_shdrp
[secn
]->sh_name
);
2518 /* Map symbol from it's internal number to the external number, moving
2519 all local symbols to be at the head of the list. */
2522 sym_is_global (abfd
, sym
)
2526 /* If the backend has a special mapping, use it. */
2527 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2528 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2531 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2532 || bfd_is_und_section (bfd_get_section (sym
))
2533 || bfd_is_com_section (bfd_get_section (sym
)));
2537 elf_map_symbols (abfd
)
2540 unsigned int symcount
= bfd_get_symcount (abfd
);
2541 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2542 asymbol
**sect_syms
;
2543 unsigned int num_locals
= 0;
2544 unsigned int num_globals
= 0;
2545 unsigned int num_locals2
= 0;
2546 unsigned int num_globals2
= 0;
2554 fprintf (stderr
, "elf_map_symbols\n");
2558 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2560 if (max_index
< asect
->index
)
2561 max_index
= asect
->index
;
2565 amt
= max_index
* sizeof (asymbol
*);
2566 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2567 if (sect_syms
== NULL
)
2569 elf_section_syms (abfd
) = sect_syms
;
2570 elf_num_section_syms (abfd
) = max_index
;
2572 /* Init sect_syms entries for any section symbols we have already
2573 decided to output. */
2574 for (idx
= 0; idx
< symcount
; idx
++)
2576 asymbol
*sym
= syms
[idx
];
2578 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2585 if (sec
->owner
!= NULL
)
2587 if (sec
->owner
!= abfd
)
2589 if (sec
->output_offset
!= 0)
2592 sec
= sec
->output_section
;
2594 /* Empty sections in the input files may have had a
2595 section symbol created for them. (See the comment
2596 near the end of _bfd_generic_link_output_symbols in
2597 linker.c). If the linker script discards such
2598 sections then we will reach this point. Since we know
2599 that we cannot avoid this case, we detect it and skip
2600 the abort and the assignment to the sect_syms array.
2601 To reproduce this particular case try running the
2602 linker testsuite test ld-scripts/weak.exp for an ELF
2603 port that uses the generic linker. */
2604 if (sec
->owner
== NULL
)
2607 BFD_ASSERT (sec
->owner
== abfd
);
2609 sect_syms
[sec
->index
] = syms
[idx
];
2614 /* Classify all of the symbols. */
2615 for (idx
= 0; idx
< symcount
; idx
++)
2617 if (!sym_is_global (abfd
, syms
[idx
]))
2623 /* We will be adding a section symbol for each BFD section. Most normal
2624 sections will already have a section symbol in outsymbols, but
2625 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2626 at least in that case. */
2627 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2629 if (sect_syms
[asect
->index
] == NULL
)
2631 if (!sym_is_global (abfd
, asect
->symbol
))
2638 /* Now sort the symbols so the local symbols are first. */
2639 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2640 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2642 if (new_syms
== NULL
)
2645 for (idx
= 0; idx
< symcount
; idx
++)
2647 asymbol
*sym
= syms
[idx
];
2650 if (!sym_is_global (abfd
, sym
))
2653 i
= num_locals
+ num_globals2
++;
2655 sym
->udata
.i
= i
+ 1;
2657 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2659 if (sect_syms
[asect
->index
] == NULL
)
2661 asymbol
*sym
= asect
->symbol
;
2664 sect_syms
[asect
->index
] = sym
;
2665 if (!sym_is_global (abfd
, sym
))
2668 i
= num_locals
+ num_globals2
++;
2670 sym
->udata
.i
= i
+ 1;
2674 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2676 elf_num_locals (abfd
) = num_locals
;
2677 elf_num_globals (abfd
) = num_globals
;
2681 /* Align to the maximum file alignment that could be required for any
2682 ELF data structure. */
2684 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2685 static INLINE file_ptr
2686 align_file_position (off
, align
)
2690 return (off
+ align
- 1) & ~(align
- 1);
2693 /* Assign a file position to a section, optionally aligning to the
2694 required section alignment. */
2697 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2698 Elf_Internal_Shdr
*i_shdrp
;
2706 al
= i_shdrp
->sh_addralign
;
2708 offset
= BFD_ALIGN (offset
, al
);
2710 i_shdrp
->sh_offset
= offset
;
2711 if (i_shdrp
->bfd_section
!= NULL
)
2712 i_shdrp
->bfd_section
->filepos
= offset
;
2713 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2714 offset
+= i_shdrp
->sh_size
;
2718 /* Compute the file positions we are going to put the sections at, and
2719 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2720 is not NULL, this is being called by the ELF backend linker. */
2723 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2725 struct bfd_link_info
*link_info
;
2727 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2729 struct bfd_strtab_hash
*strtab
;
2730 Elf_Internal_Shdr
*shstrtab_hdr
;
2732 if (abfd
->output_has_begun
)
2735 /* Do any elf backend specific processing first. */
2736 if (bed
->elf_backend_begin_write_processing
)
2737 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2739 if (! prep_headers (abfd
))
2742 /* Post process the headers if necessary. */
2743 if (bed
->elf_backend_post_process_headers
)
2744 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2747 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2751 if (!assign_section_numbers (abfd
))
2754 /* The backend linker builds symbol table information itself. */
2755 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2757 /* Non-zero if doing a relocatable link. */
2758 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2760 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2764 if (link_info
== NULL
|| link_info
->relocateable
)
2766 bfd_map_over_sections (abfd
, set_group_contents
, &failed
);
2771 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2772 /* sh_name was set in prep_headers. */
2773 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2774 shstrtab_hdr
->sh_flags
= 0;
2775 shstrtab_hdr
->sh_addr
= 0;
2776 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2777 shstrtab_hdr
->sh_entsize
= 0;
2778 shstrtab_hdr
->sh_link
= 0;
2779 shstrtab_hdr
->sh_info
= 0;
2780 /* sh_offset is set in assign_file_positions_except_relocs. */
2781 shstrtab_hdr
->sh_addralign
= 1;
2783 if (!assign_file_positions_except_relocs (abfd
))
2786 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2789 Elf_Internal_Shdr
*hdr
;
2791 off
= elf_tdata (abfd
)->next_file_pos
;
2793 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2794 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2796 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2797 if (hdr
->sh_size
!= 0)
2798 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2800 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2801 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2803 elf_tdata (abfd
)->next_file_pos
= off
;
2805 /* Now that we know where the .strtab section goes, write it
2807 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2808 || ! _bfd_stringtab_emit (abfd
, strtab
))
2810 _bfd_stringtab_free (strtab
);
2813 abfd
->output_has_begun
= true;
2818 /* Create a mapping from a set of sections to a program segment. */
2820 static INLINE
struct elf_segment_map
*
2821 make_mapping (abfd
, sections
, from
, to
, phdr
)
2823 asection
**sections
;
2828 struct elf_segment_map
*m
;
2833 amt
= sizeof (struct elf_segment_map
);
2834 amt
+= (to
- from
- 1) * sizeof (asection
*);
2835 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2839 m
->p_type
= PT_LOAD
;
2840 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2841 m
->sections
[i
- from
] = *hdrpp
;
2842 m
->count
= to
- from
;
2844 if (from
== 0 && phdr
)
2846 /* Include the headers in the first PT_LOAD segment. */
2847 m
->includes_filehdr
= 1;
2848 m
->includes_phdrs
= 1;
2854 /* Set up a mapping from BFD sections to program segments. */
2857 map_sections_to_segments (abfd
)
2860 asection
**sections
= NULL
;
2864 struct elf_segment_map
*mfirst
;
2865 struct elf_segment_map
**pm
;
2866 struct elf_segment_map
*m
;
2868 unsigned int phdr_index
;
2869 bfd_vma maxpagesize
;
2871 boolean phdr_in_segment
= true;
2873 asection
*dynsec
, *eh_frame_hdr
;
2876 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2879 if (bfd_count_sections (abfd
) == 0)
2882 /* Select the allocated sections, and sort them. */
2884 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
2885 sections
= (asection
**) bfd_malloc (amt
);
2886 if (sections
== NULL
)
2890 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2892 if ((s
->flags
& SEC_ALLOC
) != 0)
2898 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2901 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2903 /* Build the mapping. */
2908 /* If we have a .interp section, then create a PT_PHDR segment for
2909 the program headers and a PT_INTERP segment for the .interp
2911 s
= bfd_get_section_by_name (abfd
, ".interp");
2912 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2914 amt
= sizeof (struct elf_segment_map
);
2915 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2919 m
->p_type
= PT_PHDR
;
2920 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2921 m
->p_flags
= PF_R
| PF_X
;
2922 m
->p_flags_valid
= 1;
2923 m
->includes_phdrs
= 1;
2928 amt
= sizeof (struct elf_segment_map
);
2929 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2933 m
->p_type
= PT_INTERP
;
2941 /* Look through the sections. We put sections in the same program
2942 segment when the start of the second section can be placed within
2943 a few bytes of the end of the first section. */
2946 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2948 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2950 && (dynsec
->flags
& SEC_LOAD
) == 0)
2953 /* Deal with -Ttext or something similar such that the first section
2954 is not adjacent to the program headers. This is an
2955 approximation, since at this point we don't know exactly how many
2956 program headers we will need. */
2959 bfd_size_type phdr_size
;
2961 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2963 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2964 if ((abfd
->flags
& D_PAGED
) == 0
2965 || sections
[0]->lma
< phdr_size
2966 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2967 phdr_in_segment
= false;
2970 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2973 boolean new_segment
;
2977 /* See if this section and the last one will fit in the same
2980 if (last_hdr
== NULL
)
2982 /* If we don't have a segment yet, then we don't need a new
2983 one (we build the last one after this loop). */
2984 new_segment
= false;
2986 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2988 /* If this section has a different relation between the
2989 virtual address and the load address, then we need a new
2993 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2994 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2996 /* If putting this section in this segment would force us to
2997 skip a page in the segment, then we need a new segment. */
3000 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3001 && (hdr
->flags
& SEC_LOAD
) != 0)
3003 /* We don't want to put a loadable section after a
3004 nonloadable section in the same segment. */
3007 else if ((abfd
->flags
& D_PAGED
) == 0)
3009 /* If the file is not demand paged, which means that we
3010 don't require the sections to be correctly aligned in the
3011 file, then there is no other reason for a new segment. */
3012 new_segment
= false;
3015 && (hdr
->flags
& SEC_READONLY
) == 0
3016 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3019 /* We don't want to put a writable section in a read only
3020 segment, unless they are on the same page in memory
3021 anyhow. We already know that the last section does not
3022 bring us past the current section on the page, so the
3023 only case in which the new section is not on the same
3024 page as the previous section is when the previous section
3025 ends precisely on a page boundary. */
3030 /* Otherwise, we can use the same segment. */
3031 new_segment
= false;
3036 if ((hdr
->flags
& SEC_READONLY
) == 0)
3042 /* We need a new program segment. We must create a new program
3043 header holding all the sections from phdr_index until hdr. */
3045 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3052 if ((hdr
->flags
& SEC_READONLY
) == 0)
3059 phdr_in_segment
= false;
3062 /* Create a final PT_LOAD program segment. */
3063 if (last_hdr
!= NULL
)
3065 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3073 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3076 amt
= sizeof (struct elf_segment_map
);
3077 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3081 m
->p_type
= PT_DYNAMIC
;
3083 m
->sections
[0] = dynsec
;
3089 /* For each loadable .note section, add a PT_NOTE segment. We don't
3090 use bfd_get_section_by_name, because if we link together
3091 nonloadable .note sections and loadable .note sections, we will
3092 generate two .note sections in the output file. FIXME: Using
3093 names for section types is bogus anyhow. */
3094 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3096 if ((s
->flags
& SEC_LOAD
) != 0
3097 && strncmp (s
->name
, ".note", 5) == 0)
3099 amt
= sizeof (struct elf_segment_map
);
3100 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3104 m
->p_type
= PT_NOTE
;
3113 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3115 eh_frame_hdr
= NULL
;
3116 if (elf_tdata (abfd
)->eh_frame_hdr
)
3117 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3118 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3120 amt
= sizeof (struct elf_segment_map
);
3121 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3125 m
->p_type
= PT_GNU_EH_FRAME
;
3127 m
->sections
[0] = eh_frame_hdr
;
3136 elf_tdata (abfd
)->segment_map
= mfirst
;
3140 if (sections
!= NULL
)
3145 /* Sort sections by address. */
3148 elf_sort_sections (arg1
, arg2
)
3152 const asection
*sec1
= *(const asection
**) arg1
;
3153 const asection
*sec2
= *(const asection
**) arg2
;
3155 /* Sort by LMA first, since this is the address used to
3156 place the section into a segment. */
3157 if (sec1
->lma
< sec2
->lma
)
3159 else if (sec1
->lma
> sec2
->lma
)
3162 /* Then sort by VMA. Normally the LMA and the VMA will be
3163 the same, and this will do nothing. */
3164 if (sec1
->vma
< sec2
->vma
)
3166 else if (sec1
->vma
> sec2
->vma
)
3169 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3171 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3177 /* If the indicies are the same, do not return 0
3178 here, but continue to try the next comparison. */
3179 if (sec1
->target_index
- sec2
->target_index
!= 0)
3180 return sec1
->target_index
- sec2
->target_index
;
3185 else if (TOEND (sec2
))
3190 /* Sort by size, to put zero sized sections
3191 before others at the same address. */
3193 if (sec1
->_raw_size
< sec2
->_raw_size
)
3195 if (sec1
->_raw_size
> sec2
->_raw_size
)
3198 return sec1
->target_index
- sec2
->target_index
;
3201 /* Assign file positions to the sections based on the mapping from
3202 sections to segments. This function also sets up some fields in
3203 the file header, and writes out the program headers. */
3206 assign_file_positions_for_segments (abfd
)
3209 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3211 struct elf_segment_map
*m
;
3213 Elf_Internal_Phdr
*phdrs
;
3215 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3216 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3217 Elf_Internal_Phdr
*p
;
3220 if (elf_tdata (abfd
)->segment_map
== NULL
)
3222 if (! map_sections_to_segments (abfd
))
3226 if (bed
->elf_backend_modify_segment_map
)
3228 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3233 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3236 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3237 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3238 elf_elfheader (abfd
)->e_phnum
= count
;
3243 /* If we already counted the number of program segments, make sure
3244 that we allocated enough space. This happens when SIZEOF_HEADERS
3245 is used in a linker script. */
3246 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3247 if (alloc
!= 0 && count
> alloc
)
3249 ((*_bfd_error_handler
)
3250 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3251 bfd_get_filename (abfd
), alloc
, count
));
3252 bfd_set_error (bfd_error_bad_value
);
3259 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3260 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3264 off
= bed
->s
->sizeof_ehdr
;
3265 off
+= alloc
* bed
->s
->sizeof_phdr
;
3272 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3279 /* If elf_segment_map is not from map_sections_to_segments, the
3280 sections may not be correctly ordered. */
3282 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3285 p
->p_type
= m
->p_type
;
3286 p
->p_flags
= m
->p_flags
;
3288 if (p
->p_type
== PT_LOAD
3290 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3292 if ((abfd
->flags
& D_PAGED
) != 0)
3293 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3296 bfd_size_type align
;
3299 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3301 bfd_size_type secalign
;
3303 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3304 if (secalign
> align
)
3308 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3315 p
->p_vaddr
= m
->sections
[0]->vma
;
3317 if (m
->p_paddr_valid
)
3318 p
->p_paddr
= m
->p_paddr
;
3319 else if (m
->count
== 0)
3322 p
->p_paddr
= m
->sections
[0]->lma
;
3324 if (p
->p_type
== PT_LOAD
3325 && (abfd
->flags
& D_PAGED
) != 0)
3326 p
->p_align
= bed
->maxpagesize
;
3327 else if (m
->count
== 0)
3328 p
->p_align
= bed
->s
->file_align
;
3336 if (m
->includes_filehdr
)
3338 if (! m
->p_flags_valid
)
3341 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3342 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3345 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3347 if (p
->p_vaddr
< (bfd_vma
) off
)
3349 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3350 bfd_get_filename (abfd
));
3351 bfd_set_error (bfd_error_bad_value
);
3356 if (! m
->p_paddr_valid
)
3359 if (p
->p_type
== PT_LOAD
)
3361 filehdr_vaddr
= p
->p_vaddr
;
3362 filehdr_paddr
= p
->p_paddr
;
3366 if (m
->includes_phdrs
)
3368 if (! m
->p_flags_valid
)
3371 if (m
->includes_filehdr
)
3373 if (p
->p_type
== PT_LOAD
)
3375 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3376 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3381 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3385 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3386 p
->p_vaddr
-= off
- p
->p_offset
;
3387 if (! m
->p_paddr_valid
)
3388 p
->p_paddr
-= off
- p
->p_offset
;
3391 if (p
->p_type
== PT_LOAD
)
3393 phdrs_vaddr
= p
->p_vaddr
;
3394 phdrs_paddr
= p
->p_paddr
;
3397 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3400 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3401 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3404 if (p
->p_type
== PT_LOAD
3405 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3407 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3413 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3414 p
->p_filesz
+= adjust
;
3415 p
->p_memsz
+= adjust
;
3421 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3425 bfd_size_type align
;
3429 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3431 /* The section may have artificial alignment forced by a
3432 link script. Notice this case by the gap between the
3433 cumulative phdr lma and the section's lma. */
3434 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3436 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3438 p
->p_memsz
+= adjust
;
3441 if ((flags
& SEC_LOAD
) != 0)
3442 p
->p_filesz
+= adjust
;
3445 if (p
->p_type
== PT_LOAD
)
3447 bfd_signed_vma adjust
;
3449 if ((flags
& SEC_LOAD
) != 0)
3451 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3455 else if ((flags
& SEC_ALLOC
) != 0)
3457 /* The section VMA must equal the file position
3458 modulo the page size. FIXME: I'm not sure if
3459 this adjustment is really necessary. We used to
3460 not have the SEC_LOAD case just above, and then
3461 this was necessary, but now I'm not sure. */
3462 if ((abfd
->flags
& D_PAGED
) != 0)
3463 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3465 adjust
= (sec
->vma
- voff
) % align
;
3474 (* _bfd_error_handler
) (_("\
3475 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3476 bfd_section_name (abfd
, sec
),
3481 p
->p_memsz
+= adjust
;
3484 if ((flags
& SEC_LOAD
) != 0)
3485 p
->p_filesz
+= adjust
;
3490 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3491 used in a linker script we may have a section with
3492 SEC_LOAD clear but which is supposed to have
3494 if ((flags
& SEC_LOAD
) != 0
3495 || (flags
& SEC_HAS_CONTENTS
) != 0)
3496 off
+= sec
->_raw_size
;
3498 if ((flags
& SEC_ALLOC
) != 0)
3499 voff
+= sec
->_raw_size
;
3502 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3504 /* The actual "note" segment has i == 0.
3505 This is the one that actually contains everything. */
3509 p
->p_filesz
= sec
->_raw_size
;
3510 off
+= sec
->_raw_size
;
3515 /* Fake sections -- don't need to be written. */
3518 flags
= sec
->flags
= 0;
3525 p
->p_memsz
+= sec
->_raw_size
;
3527 if ((flags
& SEC_LOAD
) != 0)
3528 p
->p_filesz
+= sec
->_raw_size
;
3530 if (align
> p
->p_align
3531 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3535 if (! m
->p_flags_valid
)
3538 if ((flags
& SEC_CODE
) != 0)
3540 if ((flags
& SEC_READONLY
) == 0)
3546 /* Now that we have set the section file positions, we can set up
3547 the file positions for the non PT_LOAD segments. */
3548 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3552 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3554 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3555 p
->p_offset
= m
->sections
[0]->filepos
;
3559 if (m
->includes_filehdr
)
3561 p
->p_vaddr
= filehdr_vaddr
;
3562 if (! m
->p_paddr_valid
)
3563 p
->p_paddr
= filehdr_paddr
;
3565 else if (m
->includes_phdrs
)
3567 p
->p_vaddr
= phdrs_vaddr
;
3568 if (! m
->p_paddr_valid
)
3569 p
->p_paddr
= phdrs_paddr
;
3574 /* Clear out any program headers we allocated but did not use. */
3575 for (; count
< alloc
; count
++, p
++)
3577 memset (p
, 0, sizeof *p
);
3578 p
->p_type
= PT_NULL
;
3581 elf_tdata (abfd
)->phdr
= phdrs
;
3583 elf_tdata (abfd
)->next_file_pos
= off
;
3585 /* Write out the program headers. */
3586 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3587 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3593 /* Get the size of the program header.
3595 If this is called by the linker before any of the section VMA's are set, it
3596 can't calculate the correct value for a strange memory layout. This only
3597 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3598 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3599 data segment (exclusive of .interp and .dynamic).
3601 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3602 will be two segments. */
3604 static bfd_size_type
3605 get_program_header_size (abfd
)
3610 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3612 /* We can't return a different result each time we're called. */
3613 if (elf_tdata (abfd
)->program_header_size
!= 0)
3614 return elf_tdata (abfd
)->program_header_size
;
3616 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3618 struct elf_segment_map
*m
;
3621 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3623 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3624 return elf_tdata (abfd
)->program_header_size
;
3627 /* Assume we will need exactly two PT_LOAD segments: one for text
3628 and one for data. */
3631 s
= bfd_get_section_by_name (abfd
, ".interp");
3632 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3634 /* If we have a loadable interpreter section, we need a
3635 PT_INTERP segment. In this case, assume we also need a
3636 PT_PHDR segment, although that may not be true for all
3641 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3643 /* We need a PT_DYNAMIC segment. */
3647 if (elf_tdata (abfd
)->eh_frame_hdr
3648 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
3650 /* We need a PT_GNU_EH_FRAME segment. */
3654 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3656 if ((s
->flags
& SEC_LOAD
) != 0
3657 && strncmp (s
->name
, ".note", 5) == 0)
3659 /* We need a PT_NOTE segment. */
3664 /* Let the backend count up any program headers it might need. */
3665 if (bed
->elf_backend_additional_program_headers
)
3669 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3675 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3676 return elf_tdata (abfd
)->program_header_size
;
3679 /* Work out the file positions of all the sections. This is called by
3680 _bfd_elf_compute_section_file_positions. All the section sizes and
3681 VMAs must be known before this is called.
3683 We do not consider reloc sections at this point, unless they form
3684 part of the loadable image. Reloc sections are assigned file
3685 positions in assign_file_positions_for_relocs, which is called by
3686 write_object_contents and final_link.
3688 We also don't set the positions of the .symtab and .strtab here. */
3691 assign_file_positions_except_relocs (abfd
)
3694 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3695 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3696 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3697 unsigned int num_sec
= elf_numsections (abfd
);
3699 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3701 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3702 && bfd_get_format (abfd
) != bfd_core
)
3704 Elf_Internal_Shdr
**hdrpp
;
3707 /* Start after the ELF header. */
3708 off
= i_ehdrp
->e_ehsize
;
3710 /* We are not creating an executable, which means that we are
3711 not creating a program header, and that the actual order of
3712 the sections in the file is unimportant. */
3713 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3715 Elf_Internal_Shdr
*hdr
;
3718 if (hdr
->sh_type
== SHT_REL
3719 || hdr
->sh_type
== SHT_RELA
3720 || i
== tdata
->symtab_section
3721 || i
== tdata
->symtab_shndx_section
3722 || i
== tdata
->strtab_section
)
3724 hdr
->sh_offset
= -1;
3727 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3729 if (i
== SHN_LORESERVE
- 1)
3731 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3732 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3739 Elf_Internal_Shdr
**hdrpp
;
3741 /* Assign file positions for the loaded sections based on the
3742 assignment of sections to segments. */
3743 if (! assign_file_positions_for_segments (abfd
))
3746 /* Assign file positions for the other sections. */
3748 off
= elf_tdata (abfd
)->next_file_pos
;
3749 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3751 Elf_Internal_Shdr
*hdr
;
3754 if (hdr
->bfd_section
!= NULL
3755 && hdr
->bfd_section
->filepos
!= 0)
3756 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3757 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3759 ((*_bfd_error_handler
)
3760 (_("%s: warning: allocated section `%s' not in segment"),
3761 bfd_get_filename (abfd
),
3762 (hdr
->bfd_section
== NULL
3764 : hdr
->bfd_section
->name
)));
3765 if ((abfd
->flags
& D_PAGED
) != 0)
3766 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3768 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3769 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3772 else if (hdr
->sh_type
== SHT_REL
3773 || hdr
->sh_type
== SHT_RELA
3774 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3775 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
3776 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3777 hdr
->sh_offset
= -1;
3779 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3781 if (i
== SHN_LORESERVE
- 1)
3783 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3784 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3789 /* Place the section headers. */
3790 off
= align_file_position (off
, bed
->s
->file_align
);
3791 i_ehdrp
->e_shoff
= off
;
3792 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3794 elf_tdata (abfd
)->next_file_pos
= off
;
3803 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3804 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3805 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3807 struct elf_strtab_hash
*shstrtab
;
3808 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3810 i_ehdrp
= elf_elfheader (abfd
);
3811 i_shdrp
= elf_elfsections (abfd
);
3813 shstrtab
= _bfd_elf_strtab_init ();
3814 if (shstrtab
== NULL
)
3817 elf_shstrtab (abfd
) = shstrtab
;
3819 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3820 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3821 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3822 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3824 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3825 i_ehdrp
->e_ident
[EI_DATA
] =
3826 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3827 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3829 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3830 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3832 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3833 i_ehdrp
->e_ident
[count
] = 0;
3835 if ((abfd
->flags
& DYNAMIC
) != 0)
3836 i_ehdrp
->e_type
= ET_DYN
;
3837 else if ((abfd
->flags
& EXEC_P
) != 0)
3838 i_ehdrp
->e_type
= ET_EXEC
;
3839 else if (bfd_get_format (abfd
) == bfd_core
)
3840 i_ehdrp
->e_type
= ET_CORE
;
3842 i_ehdrp
->e_type
= ET_REL
;
3844 switch (bfd_get_arch (abfd
))
3846 case bfd_arch_unknown
:
3847 i_ehdrp
->e_machine
= EM_NONE
;
3850 /* There used to be a long list of cases here, each one setting
3851 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3852 in the corresponding bfd definition. To avoid duplication,
3853 the switch was removed. Machines that need special handling
3854 can generally do it in elf_backend_final_write_processing(),
3855 unless they need the information earlier than the final write.
3856 Such need can generally be supplied by replacing the tests for
3857 e_machine with the conditions used to determine it. */
3859 if (get_elf_backend_data (abfd
) != NULL
)
3860 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
3862 i_ehdrp
->e_machine
= EM_NONE
;
3865 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3866 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3868 /* No program header, for now. */
3869 i_ehdrp
->e_phoff
= 0;
3870 i_ehdrp
->e_phentsize
= 0;
3871 i_ehdrp
->e_phnum
= 0;
3873 /* Each bfd section is section header entry. */
3874 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3875 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3877 /* If we're building an executable, we'll need a program header table. */
3878 if (abfd
->flags
& EXEC_P
)
3880 /* It all happens later. */
3882 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3884 /* elf_build_phdrs() returns a (NULL-terminated) array of
3885 Elf_Internal_Phdrs. */
3886 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3887 i_ehdrp
->e_phoff
= outbase
;
3888 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3893 i_ehdrp
->e_phentsize
= 0;
3895 i_ehdrp
->e_phoff
= 0;
3898 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3899 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
3900 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3901 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
3902 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3903 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
3904 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3905 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3906 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3912 /* Assign file positions for all the reloc sections which are not part
3913 of the loadable file image. */
3916 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3920 unsigned int i
, num_sec
;
3921 Elf_Internal_Shdr
**shdrpp
;
3923 off
= elf_tdata (abfd
)->next_file_pos
;
3925 num_sec
= elf_numsections (abfd
);
3926 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
3928 Elf_Internal_Shdr
*shdrp
;
3931 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3932 && shdrp
->sh_offset
== -1)
3933 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3936 elf_tdata (abfd
)->next_file_pos
= off
;
3940 _bfd_elf_write_object_contents (abfd
)
3943 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3944 Elf_Internal_Ehdr
*i_ehdrp
;
3945 Elf_Internal_Shdr
**i_shdrp
;
3947 unsigned int count
, num_sec
;
3949 if (! abfd
->output_has_begun
3950 && ! _bfd_elf_compute_section_file_positions
3951 (abfd
, (struct bfd_link_info
*) NULL
))
3954 i_shdrp
= elf_elfsections (abfd
);
3955 i_ehdrp
= elf_elfheader (abfd
);
3958 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3962 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3964 /* After writing the headers, we need to write the sections too... */
3965 num_sec
= elf_numsections (abfd
);
3966 for (count
= 1; count
< num_sec
; count
++)
3968 if (bed
->elf_backend_section_processing
)
3969 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3970 if (i_shdrp
[count
]->contents
)
3972 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
3974 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3975 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
3978 if (count
== SHN_LORESERVE
- 1)
3979 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3982 /* Write out the section header names. */
3983 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3984 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
3987 if (bed
->elf_backend_final_write_processing
)
3988 (*bed
->elf_backend_final_write_processing
) (abfd
,
3989 elf_tdata (abfd
)->linker
);
3991 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3995 _bfd_elf_write_corefile_contents (abfd
)
3998 /* Hopefully this can be done just like an object file. */
3999 return _bfd_elf_write_object_contents (abfd
);
4002 /* Given a section, search the header to find them. */
4005 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4009 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4010 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4012 Elf_Internal_Shdr
*hdr
;
4013 int maxindex
= elf_numsections (abfd
);
4015 if (elf_section_data (asect
) != NULL
4016 && elf_section_data (asect
)->this_idx
!= 0)
4017 return elf_section_data (asect
)->this_idx
;
4019 if (bfd_is_abs_section (asect
))
4021 if (bfd_is_com_section (asect
))
4023 if (bfd_is_und_section (asect
))
4026 for (index
= 1; index
< maxindex
; index
++)
4028 hdr
= i_shdrp
[index
];
4029 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4033 if (bed
->elf_backend_section_from_bfd_section
)
4035 for (index
= 0; index
< maxindex
; index
++)
4039 hdr
= i_shdrp
[index
];
4044 if ((*bed
->elf_backend_section_from_bfd_section
)
4045 (abfd
, hdr
, asect
, &retval
))
4050 bfd_set_error (bfd_error_nonrepresentable_section
);
4055 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4059 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4061 asymbol
**asym_ptr_ptr
;
4063 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4065 flagword flags
= asym_ptr
->flags
;
4067 /* When gas creates relocations against local labels, it creates its
4068 own symbol for the section, but does put the symbol into the
4069 symbol chain, so udata is 0. When the linker is generating
4070 relocatable output, this section symbol may be for one of the
4071 input sections rather than the output section. */
4072 if (asym_ptr
->udata
.i
== 0
4073 && (flags
& BSF_SECTION_SYM
)
4074 && asym_ptr
->section
)
4078 if (asym_ptr
->section
->output_section
!= NULL
)
4079 indx
= asym_ptr
->section
->output_section
->index
;
4081 indx
= asym_ptr
->section
->index
;
4082 if (indx
< elf_num_section_syms (abfd
)
4083 && elf_section_syms (abfd
)[indx
] != NULL
)
4084 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4087 idx
= asym_ptr
->udata
.i
;
4091 /* This case can occur when using --strip-symbol on a symbol
4092 which is used in a relocation entry. */
4093 (*_bfd_error_handler
)
4094 (_("%s: symbol `%s' required but not present"),
4095 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4096 bfd_set_error (bfd_error_no_symbols
);
4103 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
4104 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4105 elf_symbol_flags (flags
));
4113 /* Copy private BFD data. This copies any program header information. */
4116 copy_private_bfd_data (ibfd
, obfd
)
4120 Elf_Internal_Ehdr
* iehdr
;
4121 struct elf_segment_map
* map
;
4122 struct elf_segment_map
* map_first
;
4123 struct elf_segment_map
** pointer_to_map
;
4124 Elf_Internal_Phdr
* segment
;
4127 unsigned int num_segments
;
4128 boolean phdr_included
= false;
4129 bfd_vma maxpagesize
;
4130 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4131 unsigned int phdr_adjust_num
= 0;
4133 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4134 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4137 if (elf_tdata (ibfd
)->phdr
== NULL
)
4140 iehdr
= elf_elfheader (ibfd
);
4143 pointer_to_map
= &map_first
;
4145 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4146 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4148 /* Returns the end address of the segment + 1. */
4149 #define SEGMENT_END(segment, start) \
4150 (start + (segment->p_memsz > segment->p_filesz \
4151 ? segment->p_memsz : segment->p_filesz))
4153 /* Returns true if the given section is contained within
4154 the given segment. VMA addresses are compared. */
4155 #define IS_CONTAINED_BY_VMA(section, segment) \
4156 (section->vma >= segment->p_vaddr \
4157 && (section->vma + section->_raw_size) \
4158 <= (SEGMENT_END (segment, segment->p_vaddr)))
4160 /* Returns true if the given section is contained within
4161 the given segment. LMA addresses are compared. */
4162 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4163 (section->lma >= base \
4164 && (section->lma + section->_raw_size) \
4165 <= SEGMENT_END (segment, base))
4167 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4168 #define IS_COREFILE_NOTE(p, s) \
4169 (p->p_type == PT_NOTE \
4170 && bfd_get_format (ibfd) == bfd_core \
4171 && s->vma == 0 && s->lma == 0 \
4172 && (bfd_vma) s->filepos >= p->p_offset \
4173 && (bfd_vma) s->filepos + s->_raw_size \
4174 <= p->p_offset + p->p_filesz)
4176 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4177 linker, which generates a PT_INTERP section with p_vaddr and
4178 p_memsz set to 0. */
4179 #define IS_SOLARIS_PT_INTERP(p, s) \
4181 && p->p_filesz > 0 \
4182 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4183 && s->_raw_size > 0 \
4184 && (bfd_vma) s->filepos >= p->p_offset \
4185 && ((bfd_vma) s->filepos + s->_raw_size \
4186 <= p->p_offset + p->p_filesz))
4188 /* Decide if the given section should be included in the given segment.
4189 A section will be included if:
4190 1. It is within the address space of the segment -- we use the LMA
4191 if that is set for the segment and the VMA otherwise,
4192 2. It is an allocated segment,
4193 3. There is an output section associated with it,
4194 4. The section has not already been allocated to a previous segment. */
4195 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4196 (((((segment->p_paddr \
4197 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4198 : IS_CONTAINED_BY_VMA (section, segment)) \
4199 || IS_SOLARIS_PT_INTERP (segment, section)) \
4200 && (section->flags & SEC_ALLOC) != 0) \
4201 || IS_COREFILE_NOTE (segment, section)) \
4202 && section->output_section != NULL \
4203 && section->segment_mark == false)
4205 /* Returns true iff seg1 starts after the end of seg2. */
4206 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4207 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4209 /* Returns true iff seg1 and seg2 overlap. */
4210 #define SEGMENT_OVERLAPS(seg1, seg2) \
4211 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4213 /* Initialise the segment mark field. */
4214 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4215 section
->segment_mark
= false;
4217 /* Scan through the segments specified in the program header
4218 of the input BFD. For this first scan we look for overlaps
4219 in the loadable segments. These can be created by weird
4220 parameters to objcopy. */
4221 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4226 Elf_Internal_Phdr
*segment2
;
4228 if (segment
->p_type
!= PT_LOAD
)
4231 /* Determine if this segment overlaps any previous segments. */
4232 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4234 bfd_signed_vma extra_length
;
4236 if (segment2
->p_type
!= PT_LOAD
4237 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4240 /* Merge the two segments together. */
4241 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4243 /* Extend SEGMENT2 to include SEGMENT and then delete
4246 SEGMENT_END (segment
, segment
->p_vaddr
)
4247 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4249 if (extra_length
> 0)
4251 segment2
->p_memsz
+= extra_length
;
4252 segment2
->p_filesz
+= extra_length
;
4255 segment
->p_type
= PT_NULL
;
4257 /* Since we have deleted P we must restart the outer loop. */
4259 segment
= elf_tdata (ibfd
)->phdr
;
4264 /* Extend SEGMENT to include SEGMENT2 and then delete
4267 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4268 - SEGMENT_END (segment
, segment
->p_vaddr
);
4270 if (extra_length
> 0)
4272 segment
->p_memsz
+= extra_length
;
4273 segment
->p_filesz
+= extra_length
;
4276 segment2
->p_type
= PT_NULL
;
4281 /* The second scan attempts to assign sections to segments. */
4282 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4286 unsigned int section_count
;
4287 asection
** sections
;
4288 asection
* output_section
;
4290 bfd_vma matching_lma
;
4291 bfd_vma suggested_lma
;
4295 if (segment
->p_type
== PT_NULL
)
4298 /* Compute how many sections might be placed into this segment. */
4300 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4301 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4304 /* Allocate a segment map big enough to contain all of the
4305 sections we have selected. */
4306 amt
= sizeof (struct elf_segment_map
);
4307 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4308 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4312 /* Initialise the fields of the segment map. Default to
4313 using the physical address of the segment in the input BFD. */
4315 map
->p_type
= segment
->p_type
;
4316 map
->p_flags
= segment
->p_flags
;
4317 map
->p_flags_valid
= 1;
4318 map
->p_paddr
= segment
->p_paddr
;
4319 map
->p_paddr_valid
= 1;
4321 /* Determine if this segment contains the ELF file header
4322 and if it contains the program headers themselves. */
4323 map
->includes_filehdr
= (segment
->p_offset
== 0
4324 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4326 map
->includes_phdrs
= 0;
4328 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4330 map
->includes_phdrs
=
4331 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4332 && (segment
->p_offset
+ segment
->p_filesz
4333 >= ((bfd_vma
) iehdr
->e_phoff
4334 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4336 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4337 phdr_included
= true;
4340 if (section_count
== 0)
4342 /* Special segments, such as the PT_PHDR segment, may contain
4343 no sections, but ordinary, loadable segments should contain
4345 if (segment
->p_type
== PT_LOAD
)
4347 (_("%s: warning: Empty loadable segment detected\n"),
4348 bfd_archive_filename (ibfd
));
4351 *pointer_to_map
= map
;
4352 pointer_to_map
= &map
->next
;
4357 /* Now scan the sections in the input BFD again and attempt
4358 to add their corresponding output sections to the segment map.
4359 The problem here is how to handle an output section which has
4360 been moved (ie had its LMA changed). There are four possibilities:
4362 1. None of the sections have been moved.
4363 In this case we can continue to use the segment LMA from the
4366 2. All of the sections have been moved by the same amount.
4367 In this case we can change the segment's LMA to match the LMA
4368 of the first section.
4370 3. Some of the sections have been moved, others have not.
4371 In this case those sections which have not been moved can be
4372 placed in the current segment which will have to have its size,
4373 and possibly its LMA changed, and a new segment or segments will
4374 have to be created to contain the other sections.
4376 4. The sections have been moved, but not be the same amount.
4377 In this case we can change the segment's LMA to match the LMA
4378 of the first section and we will have to create a new segment
4379 or segments to contain the other sections.
4381 In order to save time, we allocate an array to hold the section
4382 pointers that we are interested in. As these sections get assigned
4383 to a segment, they are removed from this array. */
4385 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4386 sections
= (asection
**) bfd_malloc (amt
);
4387 if (sections
== NULL
)
4390 /* Step One: Scan for segment vs section LMA conflicts.
4391 Also add the sections to the section array allocated above.
4392 Also add the sections to the current segment. In the common
4393 case, where the sections have not been moved, this means that
4394 we have completely filled the segment, and there is nothing
4400 for (j
= 0, section
= ibfd
->sections
;
4402 section
= section
->next
)
4404 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4406 output_section
= section
->output_section
;
4408 sections
[j
++] = section
;
4410 /* The Solaris native linker always sets p_paddr to 0.
4411 We try to catch that case here, and set it to the
4413 if (segment
->p_paddr
== 0
4414 && segment
->p_vaddr
!= 0
4416 && output_section
->lma
!= 0
4417 && (output_section
->vma
== (segment
->p_vaddr
4418 + (map
->includes_filehdr
4421 + (map
->includes_phdrs
4423 * iehdr
->e_phentsize
)
4425 map
->p_paddr
= segment
->p_vaddr
;
4427 /* Match up the physical address of the segment with the
4428 LMA address of the output section. */
4429 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4430 || IS_COREFILE_NOTE (segment
, section
))
4432 if (matching_lma
== 0)
4433 matching_lma
= output_section
->lma
;
4435 /* We assume that if the section fits within the segment
4436 then it does not overlap any other section within that
4438 map
->sections
[isec
++] = output_section
;
4440 else if (suggested_lma
== 0)
4441 suggested_lma
= output_section
->lma
;
4445 BFD_ASSERT (j
== section_count
);
4447 /* Step Two: Adjust the physical address of the current segment,
4449 if (isec
== section_count
)
4451 /* All of the sections fitted within the segment as currently
4452 specified. This is the default case. Add the segment to
4453 the list of built segments and carry on to process the next
4454 program header in the input BFD. */
4455 map
->count
= section_count
;
4456 *pointer_to_map
= map
;
4457 pointer_to_map
= &map
->next
;
4464 if (matching_lma
!= 0)
4466 /* At least one section fits inside the current segment.
4467 Keep it, but modify its physical address to match the
4468 LMA of the first section that fitted. */
4469 map
->p_paddr
= matching_lma
;
4473 /* None of the sections fitted inside the current segment.
4474 Change the current segment's physical address to match
4475 the LMA of the first section. */
4476 map
->p_paddr
= suggested_lma
;
4479 /* Offset the segment physical address from the lma
4480 to allow for space taken up by elf headers. */
4481 if (map
->includes_filehdr
)
4482 map
->p_paddr
-= iehdr
->e_ehsize
;
4484 if (map
->includes_phdrs
)
4486 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4488 /* iehdr->e_phnum is just an estimate of the number
4489 of program headers that we will need. Make a note
4490 here of the number we used and the segment we chose
4491 to hold these headers, so that we can adjust the
4492 offset when we know the correct value. */
4493 phdr_adjust_num
= iehdr
->e_phnum
;
4494 phdr_adjust_seg
= map
;
4498 /* Step Three: Loop over the sections again, this time assigning
4499 those that fit to the current segment and remvoing them from the
4500 sections array; but making sure not to leave large gaps. Once all
4501 possible sections have been assigned to the current segment it is
4502 added to the list of built segments and if sections still remain
4503 to be assigned, a new segment is constructed before repeating
4511 /* Fill the current segment with sections that fit. */
4512 for (j
= 0; j
< section_count
; j
++)
4514 section
= sections
[j
];
4516 if (section
== NULL
)
4519 output_section
= section
->output_section
;
4521 BFD_ASSERT (output_section
!= NULL
);
4523 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4524 || IS_COREFILE_NOTE (segment
, section
))
4526 if (map
->count
== 0)
4528 /* If the first section in a segment does not start at
4529 the beginning of the segment, then something is
4531 if (output_section
->lma
!=
4533 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4534 + (map
->includes_phdrs
4535 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4541 asection
* prev_sec
;
4543 prev_sec
= map
->sections
[map
->count
- 1];
4545 /* If the gap between the end of the previous section
4546 and the start of this section is more than
4547 maxpagesize then we need to start a new segment. */
4548 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4550 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4551 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4552 > output_section
->lma
))
4554 if (suggested_lma
== 0)
4555 suggested_lma
= output_section
->lma
;
4561 map
->sections
[map
->count
++] = output_section
;
4564 section
->segment_mark
= true;
4566 else if (suggested_lma
== 0)
4567 suggested_lma
= output_section
->lma
;
4570 BFD_ASSERT (map
->count
> 0);
4572 /* Add the current segment to the list of built segments. */
4573 *pointer_to_map
= map
;
4574 pointer_to_map
= &map
->next
;
4576 if (isec
< section_count
)
4578 /* We still have not allocated all of the sections to
4579 segments. Create a new segment here, initialise it
4580 and carry on looping. */
4581 amt
= sizeof (struct elf_segment_map
);
4582 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4583 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4587 /* Initialise the fields of the segment map. Set the physical
4588 physical address to the LMA of the first section that has
4589 not yet been assigned. */
4591 map
->p_type
= segment
->p_type
;
4592 map
->p_flags
= segment
->p_flags
;
4593 map
->p_flags_valid
= 1;
4594 map
->p_paddr
= suggested_lma
;
4595 map
->p_paddr_valid
= 1;
4596 map
->includes_filehdr
= 0;
4597 map
->includes_phdrs
= 0;
4600 while (isec
< section_count
);
4605 /* The Solaris linker creates program headers in which all the
4606 p_paddr fields are zero. When we try to objcopy or strip such a
4607 file, we get confused. Check for this case, and if we find it
4608 reset the p_paddr_valid fields. */
4609 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4610 if (map
->p_paddr
!= 0)
4614 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4615 map
->p_paddr_valid
= 0;
4618 elf_tdata (obfd
)->segment_map
= map_first
;
4620 /* If we had to estimate the number of program headers that were
4621 going to be needed, then check our estimate now and adjust
4622 the offset if necessary. */
4623 if (phdr_adjust_seg
!= NULL
)
4627 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4630 if (count
> phdr_adjust_num
)
4631 phdr_adjust_seg
->p_paddr
4632 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4636 /* Final Step: Sort the segments into ascending order of physical
4638 if (map_first
!= NULL
)
4640 struct elf_segment_map
*prev
;
4643 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4645 /* Yes I know - its a bubble sort.... */
4646 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4648 /* Swap map and map->next. */
4649 prev
->next
= map
->next
;
4650 map
->next
= map
->next
->next
;
4651 prev
->next
->next
= map
;
4661 #undef IS_CONTAINED_BY_VMA
4662 #undef IS_CONTAINED_BY_LMA
4663 #undef IS_COREFILE_NOTE
4664 #undef IS_SOLARIS_PT_INTERP
4665 #undef INCLUDE_SECTION_IN_SEGMENT
4666 #undef SEGMENT_AFTER_SEGMENT
4667 #undef SEGMENT_OVERLAPS
4671 /* Copy private section information. This copies over the entsize
4672 field, and sometimes the info field. */
4675 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4681 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4683 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4684 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4687 /* Copy over private BFD data if it has not already been copied.
4688 This must be done here, rather than in the copy_private_bfd_data
4689 entry point, because the latter is called after the section
4690 contents have been set, which means that the program headers have
4691 already been worked out. */
4692 if (elf_tdata (obfd
)->segment_map
== NULL
4693 && elf_tdata (ibfd
)->phdr
!= NULL
)
4697 /* Only set up the segments if there are no more SEC_ALLOC
4698 sections. FIXME: This won't do the right thing if objcopy is
4699 used to remove the last SEC_ALLOC section, since objcopy
4700 won't call this routine in that case. */
4701 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4702 if ((s
->flags
& SEC_ALLOC
) != 0)
4706 if (! copy_private_bfd_data (ibfd
, obfd
))
4711 ihdr
= &elf_section_data (isec
)->this_hdr
;
4712 ohdr
= &elf_section_data (osec
)->this_hdr
;
4714 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4716 if (ihdr
->sh_type
== SHT_SYMTAB
4717 || ihdr
->sh_type
== SHT_DYNSYM
4718 || ihdr
->sh_type
== SHT_GNU_verneed
4719 || ihdr
->sh_type
== SHT_GNU_verdef
)
4720 ohdr
->sh_info
= ihdr
->sh_info
;
4722 elf_section_data (osec
)->use_rela_p
4723 = elf_section_data (isec
)->use_rela_p
;
4728 /* Copy private symbol information. If this symbol is in a section
4729 which we did not map into a BFD section, try to map the section
4730 index correctly. We use special macro definitions for the mapped
4731 section indices; these definitions are interpreted by the
4732 swap_out_syms function. */
4734 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4735 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4736 #define MAP_STRTAB (SHN_HIOS + 3)
4737 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4738 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4741 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4747 elf_symbol_type
*isym
, *osym
;
4749 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4750 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4753 isym
= elf_symbol_from (ibfd
, isymarg
);
4754 osym
= elf_symbol_from (obfd
, osymarg
);
4758 && bfd_is_abs_section (isym
->symbol
.section
))
4762 shndx
= isym
->internal_elf_sym
.st_shndx
;
4763 if (shndx
== elf_onesymtab (ibfd
))
4764 shndx
= MAP_ONESYMTAB
;
4765 else if (shndx
== elf_dynsymtab (ibfd
))
4766 shndx
= MAP_DYNSYMTAB
;
4767 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4769 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4770 shndx
= MAP_SHSTRTAB
;
4771 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
4772 shndx
= MAP_SYM_SHNDX
;
4773 osym
->internal_elf_sym
.st_shndx
= shndx
;
4779 /* Swap out the symbols. */
4782 swap_out_syms (abfd
, sttp
, relocatable_p
)
4784 struct bfd_strtab_hash
**sttp
;
4787 struct elf_backend_data
*bed
;
4790 struct bfd_strtab_hash
*stt
;
4791 Elf_Internal_Shdr
*symtab_hdr
;
4792 Elf_Internal_Shdr
*symtab_shndx_hdr
;
4793 Elf_Internal_Shdr
*symstrtab_hdr
;
4794 char *outbound_syms
;
4795 char *outbound_shndx
;
4799 if (!elf_map_symbols (abfd
))
4802 /* Dump out the symtabs. */
4803 stt
= _bfd_elf_stringtab_init ();
4807 bed
= get_elf_backend_data (abfd
);
4808 symcount
= bfd_get_symcount (abfd
);
4809 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4810 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4811 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4812 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4813 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4814 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4816 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4817 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4819 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
4820 outbound_syms
= bfd_alloc (abfd
, amt
);
4821 if (outbound_syms
== NULL
)
4823 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4825 outbound_shndx
= NULL
;
4826 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
4827 if (symtab_shndx_hdr
->sh_name
!= 0)
4829 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
4830 outbound_shndx
= bfd_alloc (abfd
, amt
);
4831 if (outbound_shndx
== NULL
)
4833 memset (outbound_shndx
, 0, (unsigned long) amt
);
4834 symtab_shndx_hdr
->contents
= outbound_shndx
;
4835 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
4836 symtab_shndx_hdr
->sh_size
= amt
;
4837 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
4838 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
4841 /* now generate the data (for "contents") */
4843 /* Fill in zeroth symbol and swap it out. */
4844 Elf_Internal_Sym sym
;
4850 sym
.st_shndx
= SHN_UNDEF
;
4851 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
4852 outbound_syms
+= bed
->s
->sizeof_sym
;
4853 if (outbound_shndx
!= NULL
)
4854 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
4857 syms
= bfd_get_outsymbols (abfd
);
4858 for (idx
= 0; idx
< symcount
; idx
++)
4860 Elf_Internal_Sym sym
;
4861 bfd_vma value
= syms
[idx
]->value
;
4862 elf_symbol_type
*type_ptr
;
4863 flagword flags
= syms
[idx
]->flags
;
4866 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4868 /* Local section symbols have no name. */
4873 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4876 if (sym
.st_name
== (unsigned long) -1)
4880 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4882 if ((flags
& BSF_SECTION_SYM
) == 0
4883 && bfd_is_com_section (syms
[idx
]->section
))
4885 /* ELF common symbols put the alignment into the `value' field,
4886 and the size into the `size' field. This is backwards from
4887 how BFD handles it, so reverse it here. */
4888 sym
.st_size
= value
;
4889 if (type_ptr
== NULL
4890 || type_ptr
->internal_elf_sym
.st_value
== 0)
4891 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4893 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4894 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4895 (abfd
, syms
[idx
]->section
);
4899 asection
*sec
= syms
[idx
]->section
;
4902 if (sec
->output_section
)
4904 value
+= sec
->output_offset
;
4905 sec
= sec
->output_section
;
4907 /* Don't add in the section vma for relocatable output. */
4908 if (! relocatable_p
)
4910 sym
.st_value
= value
;
4911 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4913 if (bfd_is_abs_section (sec
)
4915 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4917 /* This symbol is in a real ELF section which we did
4918 not create as a BFD section. Undo the mapping done
4919 by copy_private_symbol_data. */
4920 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4924 shndx
= elf_onesymtab (abfd
);
4927 shndx
= elf_dynsymtab (abfd
);
4930 shndx
= elf_tdata (abfd
)->strtab_section
;
4933 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4936 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
4944 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4950 /* Writing this would be a hell of a lot easier if
4951 we had some decent documentation on bfd, and
4952 knew what to expect of the library, and what to
4953 demand of applications. For example, it
4954 appears that `objcopy' might not set the
4955 section of a symbol to be a section that is
4956 actually in the output file. */
4957 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4958 BFD_ASSERT (sec2
!= 0);
4959 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4960 BFD_ASSERT (shndx
!= -1);
4964 sym
.st_shndx
= shndx
;
4967 if ((flags
& BSF_FUNCTION
) != 0)
4969 else if ((flags
& BSF_OBJECT
) != 0)
4974 /* Processor-specific types */
4975 if (type_ptr
!= NULL
4976 && bed
->elf_backend_get_symbol_type
)
4977 type
= ((*bed
->elf_backend_get_symbol_type
)
4978 (&type_ptr
->internal_elf_sym
, type
));
4980 if (flags
& BSF_SECTION_SYM
)
4982 if (flags
& BSF_GLOBAL
)
4983 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
4985 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4987 else if (bfd_is_com_section (syms
[idx
]->section
))
4988 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4989 else if (bfd_is_und_section (syms
[idx
]->section
))
4990 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4994 else if (flags
& BSF_FILE
)
4995 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4998 int bind
= STB_LOCAL
;
5000 if (flags
& BSF_LOCAL
)
5002 else if (flags
& BSF_WEAK
)
5004 else if (flags
& BSF_GLOBAL
)
5007 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5010 if (type_ptr
!= NULL
)
5011 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5015 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5016 outbound_syms
+= bed
->s
->sizeof_sym
;
5017 if (outbound_shndx
!= NULL
)
5018 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5022 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5023 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5025 symstrtab_hdr
->sh_flags
= 0;
5026 symstrtab_hdr
->sh_addr
= 0;
5027 symstrtab_hdr
->sh_entsize
= 0;
5028 symstrtab_hdr
->sh_link
= 0;
5029 symstrtab_hdr
->sh_info
= 0;
5030 symstrtab_hdr
->sh_addralign
= 1;
5035 /* Return the number of bytes required to hold the symtab vector.
5037 Note that we base it on the count plus 1, since we will null terminate
5038 the vector allocated based on this size. However, the ELF symbol table
5039 always has a dummy entry as symbol #0, so it ends up even. */
5042 _bfd_elf_get_symtab_upper_bound (abfd
)
5047 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5049 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5050 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
5056 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5061 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5063 if (elf_dynsymtab (abfd
) == 0)
5065 bfd_set_error (bfd_error_invalid_operation
);
5069 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5070 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
5076 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5077 bfd
*abfd ATTRIBUTE_UNUSED
;
5080 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5083 /* Canonicalize the relocs. */
5086 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5094 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5096 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5099 tblptr
= section
->relocation
;
5100 for (i
= 0; i
< section
->reloc_count
; i
++)
5101 *relptr
++ = tblptr
++;
5105 return section
->reloc_count
;
5109 _bfd_elf_get_symtab (abfd
, alocation
)
5111 asymbol
**alocation
;
5113 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5114 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5117 bfd_get_symcount (abfd
) = symcount
;
5122 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5124 asymbol
**alocation
;
5126 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5127 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5130 /* Return the size required for the dynamic reloc entries. Any
5131 section that was actually installed in the BFD, and has type
5132 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5133 considered to be a dynamic reloc section. */
5136 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5142 if (elf_dynsymtab (abfd
) == 0)
5144 bfd_set_error (bfd_error_invalid_operation
);
5148 ret
= sizeof (arelent
*);
5149 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5150 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5151 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5152 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5153 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5154 * sizeof (arelent
*));
5159 /* Canonicalize the dynamic relocation entries. Note that we return
5160 the dynamic relocations as a single block, although they are
5161 actually associated with particular sections; the interface, which
5162 was designed for SunOS style shared libraries, expects that there
5163 is only one set of dynamic relocs. Any section that was actually
5164 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5165 the dynamic symbol table, is considered to be a dynamic reloc
5169 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5174 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5178 if (elf_dynsymtab (abfd
) == 0)
5180 bfd_set_error (bfd_error_invalid_operation
);
5184 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5186 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5188 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5189 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5190 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5195 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5197 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5199 for (i
= 0; i
< count
; i
++)
5210 /* Read in the version information. */
5213 _bfd_elf_slurp_version_tables (abfd
)
5216 bfd_byte
*contents
= NULL
;
5219 if (elf_dynverdef (abfd
) != 0)
5221 Elf_Internal_Shdr
*hdr
;
5222 Elf_External_Verdef
*everdef
;
5223 Elf_Internal_Verdef
*iverdef
;
5224 Elf_Internal_Verdef
*iverdefarr
;
5225 Elf_Internal_Verdef iverdefmem
;
5227 unsigned int maxidx
;
5229 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5231 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5232 if (contents
== NULL
)
5234 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5235 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5238 /* We know the number of entries in the section but not the maximum
5239 index. Therefore we have to run through all entries and find
5241 everdef
= (Elf_External_Verdef
*) contents
;
5243 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5245 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5247 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5248 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5250 everdef
= ((Elf_External_Verdef
*)
5251 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5254 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5255 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5256 if (elf_tdata (abfd
)->verdef
== NULL
)
5259 elf_tdata (abfd
)->cverdefs
= maxidx
;
5261 everdef
= (Elf_External_Verdef
*) contents
;
5262 iverdefarr
= elf_tdata (abfd
)->verdef
;
5263 for (i
= 0; i
< hdr
->sh_info
; i
++)
5265 Elf_External_Verdaux
*everdaux
;
5266 Elf_Internal_Verdaux
*iverdaux
;
5269 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5271 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5272 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5274 iverdef
->vd_bfd
= abfd
;
5276 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5277 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5278 if (iverdef
->vd_auxptr
== NULL
)
5281 everdaux
= ((Elf_External_Verdaux
*)
5282 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5283 iverdaux
= iverdef
->vd_auxptr
;
5284 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5286 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5288 iverdaux
->vda_nodename
=
5289 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5290 iverdaux
->vda_name
);
5291 if (iverdaux
->vda_nodename
== NULL
)
5294 if (j
+ 1 < iverdef
->vd_cnt
)
5295 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5297 iverdaux
->vda_nextptr
= NULL
;
5299 everdaux
= ((Elf_External_Verdaux
*)
5300 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5303 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5305 if (i
+ 1 < hdr
->sh_info
)
5306 iverdef
->vd_nextdef
= iverdef
+ 1;
5308 iverdef
->vd_nextdef
= NULL
;
5310 everdef
= ((Elf_External_Verdef
*)
5311 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5318 if (elf_dynverref (abfd
) != 0)
5320 Elf_Internal_Shdr
*hdr
;
5321 Elf_External_Verneed
*everneed
;
5322 Elf_Internal_Verneed
*iverneed
;
5325 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5327 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5328 elf_tdata (abfd
)->verref
=
5329 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5330 if (elf_tdata (abfd
)->verref
== NULL
)
5333 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5335 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5336 if (contents
== NULL
)
5338 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5339 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5342 everneed
= (Elf_External_Verneed
*) contents
;
5343 iverneed
= elf_tdata (abfd
)->verref
;
5344 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5346 Elf_External_Vernaux
*evernaux
;
5347 Elf_Internal_Vernaux
*ivernaux
;
5350 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5352 iverneed
->vn_bfd
= abfd
;
5354 iverneed
->vn_filename
=
5355 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5357 if (iverneed
->vn_filename
== NULL
)
5360 amt
= iverneed
->vn_cnt
;
5361 amt
*= sizeof (Elf_Internal_Vernaux
);
5362 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5364 evernaux
= ((Elf_External_Vernaux
*)
5365 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5366 ivernaux
= iverneed
->vn_auxptr
;
5367 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5369 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5371 ivernaux
->vna_nodename
=
5372 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5373 ivernaux
->vna_name
);
5374 if (ivernaux
->vna_nodename
== NULL
)
5377 if (j
+ 1 < iverneed
->vn_cnt
)
5378 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5380 ivernaux
->vna_nextptr
= NULL
;
5382 evernaux
= ((Elf_External_Vernaux
*)
5383 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5386 if (i
+ 1 < hdr
->sh_info
)
5387 iverneed
->vn_nextref
= iverneed
+ 1;
5389 iverneed
->vn_nextref
= NULL
;
5391 everneed
= ((Elf_External_Verneed
*)
5392 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5402 if (contents
== NULL
)
5408 _bfd_elf_make_empty_symbol (abfd
)
5411 elf_symbol_type
*newsym
;
5412 bfd_size_type amt
= sizeof (elf_symbol_type
);
5414 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5419 newsym
->symbol
.the_bfd
= abfd
;
5420 return &newsym
->symbol
;
5425 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5426 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5430 bfd_symbol_info (symbol
, ret
);
5433 /* Return whether a symbol name implies a local symbol. Most targets
5434 use this function for the is_local_label_name entry point, but some
5438 _bfd_elf_is_local_label_name (abfd
, name
)
5439 bfd
*abfd ATTRIBUTE_UNUSED
;
5442 /* Normal local symbols start with ``.L''. */
5443 if (name
[0] == '.' && name
[1] == 'L')
5446 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5447 DWARF debugging symbols starting with ``..''. */
5448 if (name
[0] == '.' && name
[1] == '.')
5451 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5452 emitting DWARF debugging output. I suspect this is actually a
5453 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5454 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5455 underscore to be emitted on some ELF targets). For ease of use,
5456 we treat such symbols as local. */
5457 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5464 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5465 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5466 asymbol
*symbol ATTRIBUTE_UNUSED
;
5473 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5475 enum bfd_architecture arch
;
5476 unsigned long machine
;
5478 /* If this isn't the right architecture for this backend, and this
5479 isn't the generic backend, fail. */
5480 if (arch
!= get_elf_backend_data (abfd
)->arch
5481 && arch
!= bfd_arch_unknown
5482 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5485 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5488 /* Find the function to a particular section and offset,
5489 for error reporting. */
5492 elf_find_function (abfd
, section
, symbols
, offset
,
5493 filename_ptr
, functionname_ptr
)
5494 bfd
*abfd ATTRIBUTE_UNUSED
;
5498 const char **filename_ptr
;
5499 const char **functionname_ptr
;
5501 const char *filename
;
5510 for (p
= symbols
; *p
!= NULL
; p
++)
5514 q
= (elf_symbol_type
*) *p
;
5516 if (bfd_get_section (&q
->symbol
) != section
)
5519 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5524 filename
= bfd_asymbol_name (&q
->symbol
);
5528 if (q
->symbol
.section
== section
5529 && q
->symbol
.value
>= low_func
5530 && q
->symbol
.value
<= offset
)
5532 func
= (asymbol
*) q
;
5533 low_func
= q
->symbol
.value
;
5543 *filename_ptr
= filename
;
5544 if (functionname_ptr
)
5545 *functionname_ptr
= bfd_asymbol_name (func
);
5550 /* Find the nearest line to a particular section and offset,
5551 for error reporting. */
5554 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5555 filename_ptr
, functionname_ptr
, line_ptr
)
5560 const char **filename_ptr
;
5561 const char **functionname_ptr
;
5562 unsigned int *line_ptr
;
5566 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5567 filename_ptr
, functionname_ptr
,
5570 if (!*functionname_ptr
)
5571 elf_find_function (abfd
, section
, symbols
, offset
,
5572 *filename_ptr
? NULL
: filename_ptr
,
5578 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5579 filename_ptr
, functionname_ptr
,
5581 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5583 if (!*functionname_ptr
)
5584 elf_find_function (abfd
, section
, symbols
, offset
,
5585 *filename_ptr
? NULL
: filename_ptr
,
5591 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5592 &found
, filename_ptr
,
5593 functionname_ptr
, line_ptr
,
5594 &elf_tdata (abfd
)->line_info
))
5599 if (symbols
== NULL
)
5602 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5603 filename_ptr
, functionname_ptr
))
5611 _bfd_elf_sizeof_headers (abfd
, reloc
)
5617 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5619 ret
+= get_program_header_size (abfd
);
5624 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5629 bfd_size_type count
;
5631 Elf_Internal_Shdr
*hdr
;
5634 if (! abfd
->output_has_begun
5635 && ! _bfd_elf_compute_section_file_positions
5636 (abfd
, (struct bfd_link_info
*) NULL
))
5639 hdr
= &elf_section_data (section
)->this_hdr
;
5640 pos
= hdr
->sh_offset
+ offset
;
5641 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5642 || bfd_bwrite (location
, count
, abfd
) != count
)
5649 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5650 bfd
*abfd ATTRIBUTE_UNUSED
;
5651 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5652 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5659 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5662 Elf_Internal_Rel
*dst
;
5668 /* Try to convert a non-ELF reloc into an ELF one. */
5671 _bfd_elf_validate_reloc (abfd
, areloc
)
5675 /* Check whether we really have an ELF howto. */
5677 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5679 bfd_reloc_code_real_type code
;
5680 reloc_howto_type
*howto
;
5682 /* Alien reloc: Try to determine its type to replace it with an
5683 equivalent ELF reloc. */
5685 if (areloc
->howto
->pc_relative
)
5687 switch (areloc
->howto
->bitsize
)
5690 code
= BFD_RELOC_8_PCREL
;
5693 code
= BFD_RELOC_12_PCREL
;
5696 code
= BFD_RELOC_16_PCREL
;
5699 code
= BFD_RELOC_24_PCREL
;
5702 code
= BFD_RELOC_32_PCREL
;
5705 code
= BFD_RELOC_64_PCREL
;
5711 howto
= bfd_reloc_type_lookup (abfd
, code
);
5713 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5715 if (howto
->pcrel_offset
)
5716 areloc
->addend
+= areloc
->address
;
5718 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5723 switch (areloc
->howto
->bitsize
)
5729 code
= BFD_RELOC_14
;
5732 code
= BFD_RELOC_16
;
5735 code
= BFD_RELOC_26
;
5738 code
= BFD_RELOC_32
;
5741 code
= BFD_RELOC_64
;
5747 howto
= bfd_reloc_type_lookup (abfd
, code
);
5751 areloc
->howto
= howto
;
5759 (*_bfd_error_handler
)
5760 (_("%s: unsupported relocation type %s"),
5761 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5762 bfd_set_error (bfd_error_bad_value
);
5767 _bfd_elf_close_and_cleanup (abfd
)
5770 if (bfd_get_format (abfd
) == bfd_object
)
5772 if (elf_shstrtab (abfd
) != NULL
)
5773 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
5776 return _bfd_generic_close_and_cleanup (abfd
);
5779 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5780 in the relocation's offset. Thus we cannot allow any sort of sanity
5781 range-checking to interfere. There is nothing else to do in processing
5784 bfd_reloc_status_type
5785 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5786 bfd
*abfd ATTRIBUTE_UNUSED
;
5787 arelent
*re ATTRIBUTE_UNUSED
;
5788 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5789 PTR data ATTRIBUTE_UNUSED
;
5790 asection
*is ATTRIBUTE_UNUSED
;
5791 bfd
*obfd ATTRIBUTE_UNUSED
;
5792 char **errmsg ATTRIBUTE_UNUSED
;
5794 return bfd_reloc_ok
;
5797 /* Elf core file support. Much of this only works on native
5798 toolchains, since we rely on knowing the
5799 machine-dependent procfs structure in order to pick
5800 out details about the corefile. */
5802 #ifdef HAVE_SYS_PROCFS_H
5803 # include <sys/procfs.h>
5806 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5809 elfcore_make_pid (abfd
)
5812 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5813 + (elf_tdata (abfd
)->core_pid
));
5816 /* If there isn't a section called NAME, make one, using
5817 data from SECT. Note, this function will generate a
5818 reference to NAME, so you shouldn't deallocate or
5822 elfcore_maybe_make_sect (abfd
, name
, sect
)
5829 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5832 sect2
= bfd_make_section (abfd
, name
);
5836 sect2
->_raw_size
= sect
->_raw_size
;
5837 sect2
->filepos
= sect
->filepos
;
5838 sect2
->flags
= sect
->flags
;
5839 sect2
->alignment_power
= sect
->alignment_power
;
5843 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5844 actually creates up to two pseudosections:
5845 - For the single-threaded case, a section named NAME, unless
5846 such a section already exists.
5847 - For the multi-threaded case, a section named "NAME/PID", where
5848 PID is elfcore_make_pid (abfd).
5849 Both pseudosections have identical contents. */
5851 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5858 char *threaded_name
;
5861 /* Build the section name. */
5863 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5864 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5865 if (threaded_name
== NULL
)
5867 strcpy (threaded_name
, buf
);
5869 sect
= bfd_make_section (abfd
, threaded_name
);
5872 sect
->_raw_size
= size
;
5873 sect
->filepos
= filepos
;
5874 sect
->flags
= SEC_HAS_CONTENTS
;
5875 sect
->alignment_power
= 2;
5877 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5880 /* prstatus_t exists on:
5882 linux 2.[01] + glibc
5886 #if defined (HAVE_PRSTATUS_T)
5887 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
5890 elfcore_grok_prstatus (abfd
, note
)
5892 Elf_Internal_Note
*note
;
5897 if (note
->descsz
== sizeof (prstatus_t
))
5901 raw_size
= sizeof (prstat
.pr_reg
);
5902 offset
= offsetof (prstatus_t
, pr_reg
);
5903 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5905 /* Do not overwrite the core signal if it
5906 has already been set by another thread. */
5907 if (elf_tdata (abfd
)->core_signal
== 0)
5908 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5909 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5911 /* pr_who exists on:
5914 pr_who doesn't exist on:
5917 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5918 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5921 #if defined (HAVE_PRSTATUS32_T)
5922 else if (note
->descsz
== sizeof (prstatus32_t
))
5924 /* 64-bit host, 32-bit corefile */
5925 prstatus32_t prstat
;
5927 raw_size
= sizeof (prstat
.pr_reg
);
5928 offset
= offsetof (prstatus32_t
, pr_reg
);
5929 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5931 /* Do not overwrite the core signal if it
5932 has already been set by another thread. */
5933 if (elf_tdata (abfd
)->core_signal
== 0)
5934 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5935 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5937 /* pr_who exists on:
5940 pr_who doesn't exist on:
5943 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5944 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5947 #endif /* HAVE_PRSTATUS32_T */
5950 /* Fail - we don't know how to handle any other
5951 note size (ie. data object type). */
5955 /* Make a ".reg/999" section and a ".reg" section. */
5956 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5957 raw_size
, note
->descpos
+ offset
);
5959 #endif /* defined (HAVE_PRSTATUS_T) */
5961 /* Create a pseudosection containing the exact contents of NOTE. */
5963 elfcore_make_note_pseudosection (abfd
, name
, note
)
5966 Elf_Internal_Note
*note
;
5968 return _bfd_elfcore_make_pseudosection (abfd
, name
,
5969 note
->descsz
, note
->descpos
);
5972 /* There isn't a consistent prfpregset_t across platforms,
5973 but it doesn't matter, because we don't have to pick this
5974 data structure apart. */
5977 elfcore_grok_prfpreg (abfd
, note
)
5979 Elf_Internal_Note
*note
;
5981 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5984 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5985 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5989 elfcore_grok_prxfpreg (abfd
, note
)
5991 Elf_Internal_Note
*note
;
5993 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5996 #if defined (HAVE_PRPSINFO_T)
5997 typedef prpsinfo_t elfcore_psinfo_t
;
5998 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5999 typedef prpsinfo32_t elfcore_psinfo32_t
;
6003 #if defined (HAVE_PSINFO_T)
6004 typedef psinfo_t elfcore_psinfo_t
;
6005 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6006 typedef psinfo32_t elfcore_psinfo32_t
;
6010 /* return a malloc'ed copy of a string at START which is at
6011 most MAX bytes long, possibly without a terminating '\0'.
6012 the copy will always have a terminating '\0'. */
6015 _bfd_elfcore_strndup (abfd
, start
, max
)
6021 char *end
= memchr (start
, '\0', max
);
6029 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6033 memcpy (dups
, start
, len
);
6039 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6040 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6043 elfcore_grok_psinfo (abfd
, note
)
6045 Elf_Internal_Note
*note
;
6047 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6049 elfcore_psinfo_t psinfo
;
6051 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6053 elf_tdata (abfd
)->core_program
6054 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6055 sizeof (psinfo
.pr_fname
));
6057 elf_tdata (abfd
)->core_command
6058 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6059 sizeof (psinfo
.pr_psargs
));
6061 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6062 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6064 /* 64-bit host, 32-bit corefile */
6065 elfcore_psinfo32_t psinfo
;
6067 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6069 elf_tdata (abfd
)->core_program
6070 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6071 sizeof (psinfo
.pr_fname
));
6073 elf_tdata (abfd
)->core_command
6074 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6075 sizeof (psinfo
.pr_psargs
));
6081 /* Fail - we don't know how to handle any other
6082 note size (ie. data object type). */
6086 /* Note that for some reason, a spurious space is tacked
6087 onto the end of the args in some (at least one anyway)
6088 implementations, so strip it off if it exists. */
6091 char *command
= elf_tdata (abfd
)->core_command
;
6092 int n
= strlen (command
);
6094 if (0 < n
&& command
[n
- 1] == ' ')
6095 command
[n
- 1] = '\0';
6100 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6102 #if defined (HAVE_PSTATUS_T)
6104 elfcore_grok_pstatus (abfd
, note
)
6106 Elf_Internal_Note
*note
;
6108 if (note
->descsz
== sizeof (pstatus_t
)
6109 #if defined (HAVE_PXSTATUS_T)
6110 || note
->descsz
== sizeof (pxstatus_t
)
6116 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6118 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6120 #if defined (HAVE_PSTATUS32_T)
6121 else if (note
->descsz
== sizeof (pstatus32_t
))
6123 /* 64-bit host, 32-bit corefile */
6126 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6128 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6131 /* Could grab some more details from the "representative"
6132 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6133 NT_LWPSTATUS note, presumably. */
6137 #endif /* defined (HAVE_PSTATUS_T) */
6139 #if defined (HAVE_LWPSTATUS_T)
6141 elfcore_grok_lwpstatus (abfd
, note
)
6143 Elf_Internal_Note
*note
;
6145 lwpstatus_t lwpstat
;
6150 if (note
->descsz
!= sizeof (lwpstat
)
6151 #if defined (HAVE_LWPXSTATUS_T)
6152 && note
->descsz
!= sizeof (lwpxstatus_t
)
6157 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6159 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6160 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6162 /* Make a ".reg/999" section. */
6164 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6165 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6170 sect
= bfd_make_section (abfd
, name
);
6174 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6175 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6176 sect
->filepos
= note
->descpos
6177 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6180 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6181 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6182 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6185 sect
->flags
= SEC_HAS_CONTENTS
;
6186 sect
->alignment_power
= 2;
6188 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6191 /* Make a ".reg2/999" section */
6193 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6194 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6199 sect
= bfd_make_section (abfd
, name
);
6203 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6204 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6205 sect
->filepos
= note
->descpos
6206 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6209 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6210 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6211 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6214 sect
->flags
= SEC_HAS_CONTENTS
;
6215 sect
->alignment_power
= 2;
6217 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6219 #endif /* defined (HAVE_LWPSTATUS_T) */
6221 #if defined (HAVE_WIN32_PSTATUS_T)
6223 elfcore_grok_win32pstatus (abfd
, note
)
6225 Elf_Internal_Note
*note
;
6230 win32_pstatus_t pstatus
;
6232 if (note
->descsz
< sizeof (pstatus
))
6235 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
6237 switch (pstatus
.data_type
)
6239 case NOTE_INFO_PROCESS
:
6240 /* FIXME: need to add ->core_command. */
6241 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6242 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6245 case NOTE_INFO_THREAD
:
6246 /* Make a ".reg/999" section. */
6247 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6249 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6255 sect
= bfd_make_section (abfd
, name
);
6259 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6260 sect
->filepos
= (note
->descpos
6261 + offsetof (struct win32_pstatus
,
6262 data
.thread_info
.thread_context
));
6263 sect
->flags
= SEC_HAS_CONTENTS
;
6264 sect
->alignment_power
= 2;
6266 if (pstatus
.data
.thread_info
.is_active_thread
)
6267 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6271 case NOTE_INFO_MODULE
:
6272 /* Make a ".module/xxxxxxxx" section. */
6273 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6275 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6281 sect
= bfd_make_section (abfd
, name
);
6286 sect
->_raw_size
= note
->descsz
;
6287 sect
->filepos
= note
->descpos
;
6288 sect
->flags
= SEC_HAS_CONTENTS
;
6289 sect
->alignment_power
= 2;
6298 #endif /* HAVE_WIN32_PSTATUS_T */
6301 elfcore_grok_note (abfd
, note
)
6303 Elf_Internal_Note
*note
;
6305 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6313 if (bed
->elf_backend_grok_prstatus
)
6314 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6316 #if defined (HAVE_PRSTATUS_T)
6317 return elfcore_grok_prstatus (abfd
, note
);
6322 #if defined (HAVE_PSTATUS_T)
6324 return elfcore_grok_pstatus (abfd
, note
);
6327 #if defined (HAVE_LWPSTATUS_T)
6329 return elfcore_grok_lwpstatus (abfd
, note
);
6332 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6333 return elfcore_grok_prfpreg (abfd
, note
);
6335 #if defined (HAVE_WIN32_PSTATUS_T)
6336 case NT_WIN32PSTATUS
:
6337 return elfcore_grok_win32pstatus (abfd
, note
);
6340 case NT_PRXFPREG
: /* Linux SSE extension */
6341 if (note
->namesz
== 5
6342 && ! strcmp (note
->namedata
, "LINUX"))
6343 return elfcore_grok_prxfpreg (abfd
, note
);
6349 if (bed
->elf_backend_grok_psinfo
)
6350 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6352 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6353 return elfcore_grok_psinfo (abfd
, note
);
6361 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6362 Elf_Internal_Note
*note
;
6367 cp
= strchr (note
->namedata
, '@');
6377 elfcore_grok_netbsd_procinfo (abfd
, note
)
6379 Elf_Internal_Note
*note
;
6382 /* Signal number at offset 0x08. */
6383 elf_tdata (abfd
)->core_signal
6384 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6386 /* Process ID at offset 0x50. */
6387 elf_tdata (abfd
)->core_pid
6388 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6390 /* Command name at 0x7c (max 32 bytes, including nul). */
6391 elf_tdata (abfd
)->core_command
6392 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6398 elfcore_grok_netbsd_note (abfd
, note
)
6400 Elf_Internal_Note
*note
;
6404 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6405 elf_tdata (abfd
)->core_lwpid
= lwp
;
6407 if (note
->type
== 1)
6409 /* NetBSD-specific core "procinfo". Note that we expect to
6410 find this note before any of the others, which is fine,
6411 since the kernel writes this note out first when it
6412 creates a core file. */
6414 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6417 /* There are not currently any other machine-independent notes defined
6418 for NetBSD ELF core files. If the note type is less than the start
6419 of the machine-dependent note types, we don't understand it. */
6421 if (note
->type
< 32)
6425 switch (bfd_get_arch (abfd
))
6427 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6428 PT_GETFPREGS == mach+2. */
6430 case bfd_arch_alpha
:
6431 case bfd_arch_sparc
:
6435 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6438 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6444 /* On all other arch's, PT_GETREGS == mach+1 and
6445 PT_GETFPREGS == mach+3. */
6451 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6454 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6464 elfcore_read_notes (abfd
, offset
, size
)
6475 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6478 buf
= bfd_malloc (size
);
6482 if (bfd_bread (buf
, size
, abfd
) != size
)
6490 while (p
< buf
+ size
)
6492 /* FIXME: bad alignment assumption. */
6493 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6494 Elf_Internal_Note in
;
6496 in
.type
= H_GET_32 (abfd
, xnp
->type
);
6498 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
6499 in
.namedata
= xnp
->name
;
6501 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
6502 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
6503 in
.descpos
= offset
+ (in
.descdata
- buf
);
6505 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
6507 if (! elfcore_grok_netbsd_note (abfd
, &in
))
6512 if (! elfcore_grok_note (abfd
, &in
))
6516 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
6523 /* Providing external access to the ELF program header table. */
6525 /* Return an upper bound on the number of bytes required to store a
6526 copy of ABFD's program header table entries. Return -1 if an error
6527 occurs; bfd_get_error will return an appropriate code. */
6530 bfd_get_elf_phdr_upper_bound (abfd
)
6533 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6535 bfd_set_error (bfd_error_wrong_format
);
6539 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
6542 /* Copy ABFD's program header table entries to *PHDRS. The entries
6543 will be stored as an array of Elf_Internal_Phdr structures, as
6544 defined in include/elf/internal.h. To find out how large the
6545 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6547 Return the number of program header table entries read, or -1 if an
6548 error occurs; bfd_get_error will return an appropriate code. */
6551 bfd_get_elf_phdrs (abfd
, phdrs
)
6557 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6559 bfd_set_error (bfd_error_wrong_format
);
6563 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
6564 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
6565 num_phdrs
* sizeof (Elf_Internal_Phdr
));
6571 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
6572 bfd
*abfd ATTRIBUTE_UNUSED
;
6577 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6579 i_ehdrp
= elf_elfheader (abfd
);
6580 if (i_ehdrp
== NULL
)
6581 sprintf_vma (buf
, value
);
6584 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6586 #if BFD_HOST_64BIT_LONG
6587 sprintf (buf
, "%016lx", value
);
6589 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
6590 _bfd_int64_low (value
));
6594 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
6597 sprintf_vma (buf
, value
);
6602 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
6603 bfd
*abfd ATTRIBUTE_UNUSED
;
6608 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6610 i_ehdrp
= elf_elfheader (abfd
);
6611 if (i_ehdrp
== NULL
)
6612 fprintf_vma ((FILE *) stream
, value
);
6615 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6617 #if BFD_HOST_64BIT_LONG
6618 fprintf ((FILE *) stream
, "%016lx", value
);
6620 fprintf ((FILE *) stream
, "%08lx%08lx",
6621 _bfd_int64_high (value
), _bfd_int64_low (value
));
6625 fprintf ((FILE *) stream
, "%08lx",
6626 (unsigned long) (value
& 0xffffffff));
6629 fprintf_vma ((FILE *) stream
, value
);
6633 enum elf_reloc_type_class
6634 _bfd_elf_reloc_type_class (rela
)
6635 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
6637 return reloc_class_normal
;
6640 /* For RELA architectures, return what the relocation value for
6641 relocation against a local symbol. */
6644 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
6646 Elf_Internal_Sym
*sym
;
6648 Elf_Internal_Rela
*rel
;
6652 relocation
= (sec
->output_section
->vma
6653 + sec
->output_offset
6655 if ((sec
->flags
& SEC_MERGE
)
6656 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
6657 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6663 _bfd_merged_section_offset (abfd
, &msec
,
6664 elf_section_data (sec
)->sec_info
,
6665 sym
->st_value
+ rel
->r_addend
,
6668 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
6674 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
6676 Elf_Internal_Sym
*sym
;
6680 asection
*sec
= *psec
;
6682 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
6683 return sym
->st_value
+ addend
;
6685 return _bfd_merged_section_offset (abfd
, psec
,
6686 elf_section_data (sec
)->sec_info
,
6687 sym
->st_value
+ addend
, (bfd_vma
) 0);
6691 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
6693 struct bfd_link_info
*info
;
6697 struct bfd_elf_section_data
*sec_data
;
6699 sec_data
= elf_section_data (sec
);
6700 switch (sec_data
->sec_info_type
)
6702 case ELF_INFO_TYPE_STABS
:
6703 return _bfd_stab_section_offset
6704 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
6706 case ELF_INFO_TYPE_EH_FRAME
:
6707 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);