1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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 merge_sections_remove_hook
PARAMS ((bfd
*, asection
*));
57 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
58 static void set_group_contents
PARAMS ((bfd
*, asection
*, PTR
));
59 static boolean assign_section_numbers
PARAMS ((bfd
*));
60 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
61 static boolean elf_map_symbols
PARAMS ((bfd
*));
62 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
63 static boolean elfcore_read_notes
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
64 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
65 bfd_vma
, const char **,
67 static int elfcore_make_pid
PARAMS ((bfd
*));
68 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
69 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
70 Elf_Internal_Note
*));
71 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
72 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
73 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
75 static boolean elfcore_netbsd_get_lwpid
PARAMS ((Elf_Internal_Note
*, int *));
76 static boolean elfcore_grok_netbsd_procinfo
PARAMS ((bfd
*,
77 Elf_Internal_Note
*));
78 static boolean elfcore_grok_netbsd_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
80 /* Swap version information in and out. The version information is
81 currently size independent. If that ever changes, this code will
82 need to move into elfcode.h. */
84 /* Swap in a Verdef structure. */
87 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
89 const Elf_External_Verdef
*src
;
90 Elf_Internal_Verdef
*dst
;
92 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
93 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
94 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
95 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
96 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
97 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
98 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
101 /* Swap out a Verdef structure. */
104 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
106 const Elf_Internal_Verdef
*src
;
107 Elf_External_Verdef
*dst
;
109 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
110 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
111 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
112 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
113 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
114 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
115 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
118 /* Swap in a Verdaux structure. */
121 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
123 const Elf_External_Verdaux
*src
;
124 Elf_Internal_Verdaux
*dst
;
126 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
127 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
130 /* Swap out a Verdaux structure. */
133 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
135 const Elf_Internal_Verdaux
*src
;
136 Elf_External_Verdaux
*dst
;
138 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
139 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
142 /* Swap in a Verneed structure. */
145 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
147 const Elf_External_Verneed
*src
;
148 Elf_Internal_Verneed
*dst
;
150 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
151 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
152 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
153 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
154 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
157 /* Swap out a Verneed structure. */
160 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
162 const Elf_Internal_Verneed
*src
;
163 Elf_External_Verneed
*dst
;
165 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
166 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
167 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
168 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
169 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
172 /* Swap in a Vernaux structure. */
175 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
177 const Elf_External_Vernaux
*src
;
178 Elf_Internal_Vernaux
*dst
;
180 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
181 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
182 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
183 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
184 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
187 /* Swap out a Vernaux structure. */
190 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
192 const Elf_Internal_Vernaux
*src
;
193 Elf_External_Vernaux
*dst
;
195 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
196 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
197 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
198 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
199 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
202 /* Swap in a Versym structure. */
205 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
207 const Elf_External_Versym
*src
;
208 Elf_Internal_Versym
*dst
;
210 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
213 /* Swap out a Versym structure. */
216 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
218 const Elf_Internal_Versym
*src
;
219 Elf_External_Versym
*dst
;
221 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
224 /* Standard ELF hash function. Do not change this function; you will
225 cause invalid hash tables to be generated. */
228 bfd_elf_hash (namearg
)
231 const unsigned char *name
= (const unsigned char *) namearg
;
236 while ((ch
= *name
++) != '\0')
239 if ((g
= (h
& 0xf0000000)) != 0)
242 /* The ELF ABI says `h &= ~g', but this is equivalent in
243 this case and on some machines one insn instead of two. */
250 /* Read a specified number of bytes at a specified offset in an ELF
251 file, into a newly allocated buffer, and return a pointer to the
255 elf_read (abfd
, offset
, size
)
262 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
264 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
266 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
268 if (bfd_get_error () != bfd_error_system_call
)
269 bfd_set_error (bfd_error_file_truncated
);
276 bfd_elf_mkobject (abfd
)
279 /* This just does initialization. */
280 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
281 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
282 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
283 if (elf_tdata (abfd
) == 0)
285 /* Since everything is done at close time, do we need any
292 bfd_elf_mkcorefile (abfd
)
295 /* I think this can be done just like an object file. */
296 return bfd_elf_mkobject (abfd
);
300 bfd_elf_get_str_section (abfd
, shindex
)
302 unsigned int shindex
;
304 Elf_Internal_Shdr
**i_shdrp
;
305 char *shstrtab
= NULL
;
307 bfd_size_type shstrtabsize
;
309 i_shdrp
= elf_elfsections (abfd
);
310 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
313 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
314 if (shstrtab
== NULL
)
316 /* No cached one, attempt to read, and cache what we read. */
317 offset
= i_shdrp
[shindex
]->sh_offset
;
318 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
319 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
320 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
326 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
328 unsigned int shindex
;
329 unsigned int strindex
;
331 Elf_Internal_Shdr
*hdr
;
336 hdr
= elf_elfsections (abfd
)[shindex
];
338 if (hdr
->contents
== NULL
339 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
342 if (strindex
>= hdr
->sh_size
)
344 (*_bfd_error_handler
)
345 (_("%s: invalid string offset %u >= %lu for section `%s'"),
346 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
347 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
348 && strindex
== hdr
->sh_name
)
350 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
354 return ((char *) hdr
->contents
) + strindex
;
357 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
358 sections. The first element is the flags, the rest are section
361 typedef union elf_internal_group
{
362 Elf_Internal_Shdr
*shdr
;
364 } Elf_Internal_Group
;
366 /* Set next_in_group list pointer, and group name for NEWSECT. */
369 setup_group (abfd
, hdr
, newsect
)
371 Elf_Internal_Shdr
*hdr
;
374 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
376 /* If num_group is zero, read in all SHT_GROUP sections. The count
377 is set to -1 if there are no SHT_GROUP sections. */
380 unsigned int i
, shnum
;
382 /* First count the number of groups. If we have a SHT_GROUP
383 section with just a flag word (ie. sh_size is 4), ignore it. */
384 shnum
= elf_numsections (abfd
);
386 for (i
= 0; i
< shnum
; i
++)
388 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
389 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
394 num_group
= (unsigned) -1;
395 elf_tdata (abfd
)->num_group
= num_group
;
399 /* We keep a list of elf section headers for group sections,
400 so we can find them quickly. */
401 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
402 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
403 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
407 for (i
= 0; i
< shnum
; i
++)
409 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
410 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
413 Elf_Internal_Group
*dest
;
415 /* Add to list of sections. */
416 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
419 /* Read the raw contents. */
420 BFD_ASSERT (sizeof (*dest
) >= 4);
421 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
422 shdr
->contents
= bfd_alloc (abfd
, amt
);
423 if (shdr
->contents
== NULL
424 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
425 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
429 /* Translate raw contents, a flag word followed by an
430 array of elf section indices all in target byte order,
431 to the flag word followed by an array of elf section
433 src
= shdr
->contents
+ shdr
->sh_size
;
434 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
441 idx
= H_GET_32 (abfd
, src
);
442 if (src
== shdr
->contents
)
449 ((*_bfd_error_handler
)
450 (_("%s: invalid SHT_GROUP entry"),
451 bfd_archive_filename (abfd
)));
454 dest
->shdr
= elf_elfsections (abfd
)[idx
];
461 if (num_group
!= (unsigned) -1)
465 for (i
= 0; i
< num_group
; i
++)
467 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
468 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
469 unsigned int n_elt
= shdr
->sh_size
/ 4;
471 /* Look through this group's sections to see if current
472 section is a member. */
474 if ((++idx
)->shdr
== hdr
)
478 /* We are a member of this group. Go looking through
479 other members to see if any others are linked via
481 idx
= (Elf_Internal_Group
*) shdr
->contents
;
482 n_elt
= shdr
->sh_size
/ 4;
484 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
485 && elf_next_in_group (s
) != NULL
)
489 /* Snarf the group name from other member, and
490 insert current section in circular list. */
491 elf_group_name (newsect
) = elf_group_name (s
);
492 elf_next_in_group (newsect
) = elf_next_in_group (s
);
493 elf_next_in_group (s
) = newsect
;
497 struct elf_backend_data
*bed
;
499 unsigned char ename
[4];
503 /* Humbug. Get the name from the group signature
504 symbol. Why isn't the signature just a string?
505 Fortunately, the name index is at the same
506 place in the external symbol for both 32 and 64
508 bed
= get_elf_backend_data (abfd
);
509 pos
= elf_tdata (abfd
)->symtab_hdr
.sh_offset
;
510 pos
+= shdr
->sh_info
* bed
->s
->sizeof_sym
;
511 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
512 || bfd_bread (ename
, (bfd_size_type
) 4, abfd
) != 4)
514 iname
= H_GET_32 (abfd
, ename
);
515 gname
= elf_string_from_elf_strtab (abfd
, iname
);
516 elf_group_name (newsect
) = gname
;
518 /* Start a circular list with one element. */
519 elf_next_in_group (newsect
) = newsect
;
521 if (shdr
->bfd_section
!= NULL
)
522 elf_next_in_group (shdr
->bfd_section
) = newsect
;
529 if (elf_group_name (newsect
) == NULL
)
531 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
532 bfd_archive_filename (abfd
), newsect
->name
);
537 /* Make a BFD section from an ELF section. We store a pointer to the
538 BFD section in the bfd_section field of the header. */
541 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
543 Elf_Internal_Shdr
*hdr
;
548 struct elf_backend_data
*bed
;
550 if (hdr
->bfd_section
!= NULL
)
552 BFD_ASSERT (strcmp (name
,
553 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
557 newsect
= bfd_make_section_anyway (abfd
, name
);
561 newsect
->filepos
= hdr
->sh_offset
;
563 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
564 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
565 || ! bfd_set_section_alignment (abfd
, newsect
,
566 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
569 flags
= SEC_NO_FLAGS
;
570 if (hdr
->sh_type
!= SHT_NOBITS
)
571 flags
|= SEC_HAS_CONTENTS
;
572 if (hdr
->sh_type
== SHT_GROUP
)
573 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
574 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
577 if (hdr
->sh_type
!= SHT_NOBITS
)
580 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
581 flags
|= SEC_READONLY
;
582 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
584 else if ((flags
& SEC_LOAD
) != 0)
586 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
589 newsect
->entsize
= hdr
->sh_entsize
;
590 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
591 flags
|= SEC_STRINGS
;
593 if (hdr
->sh_flags
& SHF_GROUP
)
594 if (!setup_group (abfd
, hdr
, newsect
))
597 /* The debugging sections appear to be recognized only by name, not
600 static const char *debug_sec_names
[] =
609 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
610 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
614 flags
|= SEC_DEBUGGING
;
617 /* As a GNU extension, if the name begins with .gnu.linkonce, we
618 only link a single copy of the section. This is used to support
619 g++. g++ will emit each template expansion in its own section.
620 The symbols will be defined as weak, so that multiple definitions
621 are permitted. The GNU linker extension is to actually discard
622 all but one of the sections. */
623 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
624 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
626 bed
= get_elf_backend_data (abfd
);
627 if (bed
->elf_backend_section_flags
)
628 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
631 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
634 if ((flags
& SEC_ALLOC
) != 0)
636 Elf_Internal_Phdr
*phdr
;
639 /* Look through the phdrs to see if we need to adjust the lma.
640 If all the p_paddr fields are zero, we ignore them, since
641 some ELF linkers produce such output. */
642 phdr
= elf_tdata (abfd
)->phdr
;
643 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
645 if (phdr
->p_paddr
!= 0)
648 if (i
< elf_elfheader (abfd
)->e_phnum
)
650 phdr
= elf_tdata (abfd
)->phdr
;
651 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
653 /* This section is part of this segment if its file
654 offset plus size lies within the segment's memory
655 span and, if the section is loaded, the extent of the
656 loaded data lies within the extent of the segment.
657 If the p_paddr field is not set, we don't alter the
659 if (phdr
->p_type
== PT_LOAD
661 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
662 && (hdr
->sh_offset
+ hdr
->sh_size
663 <= phdr
->p_offset
+ phdr
->p_memsz
)
664 && ((flags
& SEC_LOAD
) == 0
665 || (phdr
->p_offset
+ phdr
->p_filesz
666 >= hdr
->sh_offset
+ hdr
->sh_size
)))
668 /* We used to do a relative adjustment here, but
669 that doesn't work if the segment is packed with
670 code from multiple VMAs. Instead we calculate
671 the LMA absoultely, based on the LMA of the
672 segment (it is assumed that the segment will
673 contain sections with contiguous LMAs, even if
674 the VMAs are not). */
675 newsect
->lma
= phdr
->p_paddr
676 + hdr
->sh_offset
- phdr
->p_offset
;
683 hdr
->bfd_section
= newsect
;
684 elf_section_data (newsect
)->this_hdr
= *hdr
;
694 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
697 Helper functions for GDB to locate the string tables.
698 Since BFD hides string tables from callers, GDB needs to use an
699 internal hook to find them. Sun's .stabstr, in particular,
700 isn't even pointed to by the .stab section, so ordinary
701 mechanisms wouldn't work to find it, even if we had some.
704 struct elf_internal_shdr
*
705 bfd_elf_find_section (abfd
, name
)
709 Elf_Internal_Shdr
**i_shdrp
;
714 i_shdrp
= elf_elfsections (abfd
);
717 shstrtab
= bfd_elf_get_str_section (abfd
,
718 elf_elfheader (abfd
)->e_shstrndx
);
719 if (shstrtab
!= NULL
)
721 max
= elf_numsections (abfd
);
722 for (i
= 1; i
< max
; i
++)
723 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
730 const char *const bfd_elf_section_type_names
[] = {
731 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
732 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
733 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
736 /* ELF relocs are against symbols. If we are producing relocateable
737 output, and the reloc is against an external symbol, and nothing
738 has given us any additional addend, the resulting reloc will also
739 be against the same symbol. In such a case, we don't want to
740 change anything about the way the reloc is handled, since it will
741 all be done at final link time. Rather than put special case code
742 into bfd_perform_relocation, all the reloc types use this howto
743 function. It just short circuits the reloc if producing
744 relocateable output against an external symbol. */
746 bfd_reloc_status_type
747 bfd_elf_generic_reloc (abfd
,
754 bfd
*abfd ATTRIBUTE_UNUSED
;
755 arelent
*reloc_entry
;
757 PTR data ATTRIBUTE_UNUSED
;
758 asection
*input_section
;
760 char **error_message ATTRIBUTE_UNUSED
;
762 if (output_bfd
!= (bfd
*) NULL
763 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
764 && (! reloc_entry
->howto
->partial_inplace
765 || reloc_entry
->addend
== 0))
767 reloc_entry
->address
+= input_section
->output_offset
;
771 return bfd_reloc_continue
;
774 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
777 merge_sections_remove_hook (abfd
, sec
)
778 bfd
*abfd ATTRIBUTE_UNUSED
;
781 struct bfd_elf_section_data
*sec_data
;
783 sec_data
= elf_section_data (sec
);
784 BFD_ASSERT (sec_data
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
785 sec_data
->sec_info_type
= ELF_INFO_TYPE_NONE
;
788 /* Finish SHF_MERGE section merging. */
791 _bfd_elf_merge_sections (abfd
, info
)
793 struct bfd_link_info
*info
;
795 if (!is_elf_hash_table (info
))
797 if (elf_hash_table (info
)->merge_info
)
798 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
799 merge_sections_remove_hook
);
803 /* Copy the program header and other data from one object module to
807 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
811 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
812 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
815 BFD_ASSERT (!elf_flags_init (obfd
)
816 || (elf_elfheader (obfd
)->e_flags
817 == elf_elfheader (ibfd
)->e_flags
));
819 elf_gp (obfd
) = elf_gp (ibfd
);
820 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
821 elf_flags_init (obfd
) = true;
825 /* Print out the program headers. */
828 _bfd_elf_print_private_bfd_data (abfd
, farg
)
832 FILE *f
= (FILE *) farg
;
833 Elf_Internal_Phdr
*p
;
835 bfd_byte
*dynbuf
= NULL
;
837 p
= elf_tdata (abfd
)->phdr
;
842 fprintf (f
, _("\nProgram Header:\n"));
843 c
= elf_elfheader (abfd
)->e_phnum
;
844 for (i
= 0; i
< c
; i
++, p
++)
851 case PT_NULL
: pt
= "NULL"; break;
852 case PT_LOAD
: pt
= "LOAD"; break;
853 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
854 case PT_INTERP
: pt
= "INTERP"; break;
855 case PT_NOTE
: pt
= "NOTE"; break;
856 case PT_SHLIB
: pt
= "SHLIB"; break;
857 case PT_PHDR
: pt
= "PHDR"; break;
858 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
859 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
861 fprintf (f
, "%8s off 0x", pt
);
862 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
863 fprintf (f
, " vaddr 0x");
864 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
865 fprintf (f
, " paddr 0x");
866 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
867 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
868 fprintf (f
, " filesz 0x");
869 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
870 fprintf (f
, " memsz 0x");
871 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
872 fprintf (f
, " flags %c%c%c",
873 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
874 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
875 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
876 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
877 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
882 s
= bfd_get_section_by_name (abfd
, ".dynamic");
886 unsigned long shlink
;
887 bfd_byte
*extdyn
, *extdynend
;
889 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
891 fprintf (f
, _("\nDynamic Section:\n"));
893 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
896 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
900 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
903 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
905 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
906 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
909 extdynend
= extdyn
+ s
->_raw_size
;
910 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
912 Elf_Internal_Dyn dyn
;
917 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
919 if (dyn
.d_tag
== DT_NULL
)
926 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
930 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
931 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
932 case DT_PLTGOT
: name
= "PLTGOT"; break;
933 case DT_HASH
: name
= "HASH"; break;
934 case DT_STRTAB
: name
= "STRTAB"; break;
935 case DT_SYMTAB
: name
= "SYMTAB"; break;
936 case DT_RELA
: name
= "RELA"; break;
937 case DT_RELASZ
: name
= "RELASZ"; break;
938 case DT_RELAENT
: name
= "RELAENT"; break;
939 case DT_STRSZ
: name
= "STRSZ"; break;
940 case DT_SYMENT
: name
= "SYMENT"; break;
941 case DT_INIT
: name
= "INIT"; break;
942 case DT_FINI
: name
= "FINI"; break;
943 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
944 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
945 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
946 case DT_REL
: name
= "REL"; break;
947 case DT_RELSZ
: name
= "RELSZ"; break;
948 case DT_RELENT
: name
= "RELENT"; break;
949 case DT_PLTREL
: name
= "PLTREL"; break;
950 case DT_DEBUG
: name
= "DEBUG"; break;
951 case DT_TEXTREL
: name
= "TEXTREL"; break;
952 case DT_JMPREL
: name
= "JMPREL"; break;
953 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
954 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
955 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
956 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
957 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
958 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
959 case DT_FLAGS
: name
= "FLAGS"; break;
960 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
961 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
962 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
963 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
964 case DT_MOVEENT
: name
= "MOVEENT"; break;
965 case DT_MOVESZ
: name
= "MOVESZ"; break;
966 case DT_FEATURE
: name
= "FEATURE"; break;
967 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
968 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
969 case DT_SYMINENT
: name
= "SYMINENT"; break;
970 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
971 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
972 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
973 case DT_PLTPAD
: name
= "PLTPAD"; break;
974 case DT_MOVETAB
: name
= "MOVETAB"; break;
975 case DT_SYMINFO
: name
= "SYMINFO"; break;
976 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
977 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
978 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
979 case DT_VERSYM
: name
= "VERSYM"; break;
980 case DT_VERDEF
: name
= "VERDEF"; break;
981 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
982 case DT_VERNEED
: name
= "VERNEED"; break;
983 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
984 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
985 case DT_USED
: name
= "USED"; break;
986 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
989 fprintf (f
, " %-11s ", name
);
991 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
995 unsigned int tagv
= dyn
.d_un
.d_val
;
997 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1000 fprintf (f
, "%s", string
);
1009 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1010 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1012 if (! _bfd_elf_slurp_version_tables (abfd
))
1016 if (elf_dynverdef (abfd
) != 0)
1018 Elf_Internal_Verdef
*t
;
1020 fprintf (f
, _("\nVersion definitions:\n"));
1021 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1023 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1024 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1025 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1027 Elf_Internal_Verdaux
*a
;
1030 for (a
= t
->vd_auxptr
->vda_nextptr
;
1033 fprintf (f
, "%s ", a
->vda_nodename
);
1039 if (elf_dynverref (abfd
) != 0)
1041 Elf_Internal_Verneed
*t
;
1043 fprintf (f
, _("\nVersion References:\n"));
1044 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1046 Elf_Internal_Vernaux
*a
;
1048 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1049 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1050 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1051 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1063 /* Display ELF-specific fields of a symbol. */
1066 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1070 bfd_print_symbol_type how
;
1072 FILE *file
= (FILE *) filep
;
1075 case bfd_print_symbol_name
:
1076 fprintf (file
, "%s", symbol
->name
);
1078 case bfd_print_symbol_more
:
1079 fprintf (file
, "elf ");
1080 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1081 fprintf (file
, " %lx", (long) symbol
->flags
);
1083 case bfd_print_symbol_all
:
1085 const char *section_name
;
1086 const char *name
= NULL
;
1087 struct elf_backend_data
*bed
;
1088 unsigned char st_other
;
1091 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1093 bed
= get_elf_backend_data (abfd
);
1094 if (bed
->elf_backend_print_symbol_all
)
1095 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1099 name
= symbol
->name
;
1100 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1103 fprintf (file
, " %s\t", section_name
);
1104 /* Print the "other" value for a symbol. For common symbols,
1105 we've already printed the size; now print the alignment.
1106 For other symbols, we have no specified alignment, and
1107 we've printed the address; now print the size. */
1108 if (bfd_is_com_section (symbol
->section
))
1109 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1111 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1112 bfd_fprintf_vma (abfd
, file
, val
);
1114 /* If we have version information, print it. */
1115 if (elf_tdata (abfd
)->dynversym_section
!= 0
1116 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1117 || elf_tdata (abfd
)->dynverref_section
!= 0))
1119 unsigned int vernum
;
1120 const char *version_string
;
1122 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1125 version_string
= "";
1126 else if (vernum
== 1)
1127 version_string
= "Base";
1128 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1130 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1133 Elf_Internal_Verneed
*t
;
1135 version_string
= "";
1136 for (t
= elf_tdata (abfd
)->verref
;
1140 Elf_Internal_Vernaux
*a
;
1142 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1144 if (a
->vna_other
== vernum
)
1146 version_string
= a
->vna_nodename
;
1153 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1154 fprintf (file
, " %-11s", version_string
);
1159 fprintf (file
, " (%s)", version_string
);
1160 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1165 /* If the st_other field is not zero, print it. */
1166 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1171 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1172 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1173 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1175 /* Some other non-defined flags are also present, so print
1177 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1180 fprintf (file
, " %s", name
);
1186 /* Create an entry in an ELF linker hash table. */
1188 struct bfd_hash_entry
*
1189 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1190 struct bfd_hash_entry
*entry
;
1191 struct bfd_hash_table
*table
;
1194 /* Allocate the structure if it has not already been allocated by a
1198 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1203 /* Call the allocation method of the superclass. */
1204 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1207 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1208 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1210 /* Set local fields. */
1214 ret
->dynstr_index
= 0;
1215 ret
->weakdef
= NULL
;
1216 ret
->got
.refcount
= htab
->init_refcount
;
1217 ret
->plt
.refcount
= htab
->init_refcount
;
1218 ret
->linker_section_pointer
= NULL
;
1219 ret
->verinfo
.verdef
= NULL
;
1220 ret
->vtable_entries_used
= NULL
;
1221 ret
->vtable_entries_size
= 0;
1222 ret
->vtable_parent
= NULL
;
1223 ret
->type
= STT_NOTYPE
;
1225 /* Assume that we have been called by a non-ELF symbol reader.
1226 This flag is then reset by the code which reads an ELF input
1227 file. This ensures that a symbol created by a non-ELF symbol
1228 reader will have the flag set correctly. */
1229 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1235 /* Copy data from an indirect symbol to its direct symbol, hiding the
1236 old indirect symbol. Also used for copying flags to a weakdef. */
1239 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1240 struct elf_link_hash_entry
*dir
, *ind
;
1244 /* Copy down any references that we may have already seen to the
1245 symbol which just became indirect. */
1247 dir
->elf_link_hash_flags
|=
1248 (ind
->elf_link_hash_flags
1249 & (ELF_LINK_HASH_REF_DYNAMIC
1250 | ELF_LINK_HASH_REF_REGULAR
1251 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1252 | ELF_LINK_NON_GOT_REF
));
1254 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1257 /* Copy over the global and procedure linkage table refcount entries.
1258 These may have been already set up by a check_relocs routine. */
1259 tmp
= dir
->got
.refcount
;
1262 dir
->got
.refcount
= ind
->got
.refcount
;
1263 ind
->got
.refcount
= tmp
;
1266 BFD_ASSERT (ind
->got
.refcount
<= 0);
1268 tmp
= dir
->plt
.refcount
;
1271 dir
->plt
.refcount
= ind
->plt
.refcount
;
1272 ind
->plt
.refcount
= tmp
;
1275 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1277 if (dir
->dynindx
== -1)
1279 dir
->dynindx
= ind
->dynindx
;
1280 dir
->dynstr_index
= ind
->dynstr_index
;
1282 ind
->dynstr_index
= 0;
1285 BFD_ASSERT (ind
->dynindx
== -1);
1289 _bfd_elf_link_hash_hide_symbol (info
, h
)
1290 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1291 struct elf_link_hash_entry
*h
;
1293 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1294 h
->plt
.offset
= (bfd_vma
) -1;
1295 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1299 /* Initialize an ELF linker hash table. */
1302 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1303 struct elf_link_hash_table
*table
;
1305 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1306 struct bfd_hash_table
*,
1311 table
->dynamic_sections_created
= false;
1312 table
->dynobj
= NULL
;
1313 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1314 /* The first dynamic symbol is a dummy. */
1315 table
->dynsymcount
= 1;
1316 table
->dynstr
= NULL
;
1317 table
->bucketcount
= 0;
1318 table
->needed
= NULL
;
1319 table
->runpath
= NULL
;
1321 table
->stab_info
= NULL
;
1322 table
->merge_info
= NULL
;
1323 table
->dynlocal
= NULL
;
1324 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1325 table
->root
.type
= bfd_link_elf_hash_table
;
1330 /* Create an ELF linker hash table. */
1332 struct bfd_link_hash_table
*
1333 _bfd_elf_link_hash_table_create (abfd
)
1336 struct elf_link_hash_table
*ret
;
1337 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1339 ret
= (struct elf_link_hash_table
*) bfd_alloc (abfd
, amt
);
1340 if (ret
== (struct elf_link_hash_table
*) NULL
)
1343 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1345 bfd_release (abfd
, ret
);
1352 /* This is a hook for the ELF emulation code in the generic linker to
1353 tell the backend linker what file name to use for the DT_NEEDED
1354 entry for a dynamic object. The generic linker passes name as an
1355 empty string to indicate that no DT_NEEDED entry should be made. */
1358 bfd_elf_set_dt_needed_name (abfd
, name
)
1362 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1363 && bfd_get_format (abfd
) == bfd_object
)
1364 elf_dt_name (abfd
) = name
;
1368 bfd_elf_set_dt_needed_soname (abfd
, name
)
1372 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1373 && bfd_get_format (abfd
) == bfd_object
)
1374 elf_dt_soname (abfd
) = name
;
1377 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1378 the linker ELF emulation code. */
1380 struct bfd_link_needed_list
*
1381 bfd_elf_get_needed_list (abfd
, info
)
1382 bfd
*abfd ATTRIBUTE_UNUSED
;
1383 struct bfd_link_info
*info
;
1385 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1387 return elf_hash_table (info
)->needed
;
1390 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1391 hook for the linker ELF emulation code. */
1393 struct bfd_link_needed_list
*
1394 bfd_elf_get_runpath_list (abfd
, info
)
1395 bfd
*abfd ATTRIBUTE_UNUSED
;
1396 struct bfd_link_info
*info
;
1398 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1400 return elf_hash_table (info
)->runpath
;
1403 /* Get the name actually used for a dynamic object for a link. This
1404 is the SONAME entry if there is one. Otherwise, it is the string
1405 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1408 bfd_elf_get_dt_soname (abfd
)
1411 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1412 && bfd_get_format (abfd
) == bfd_object
)
1413 return elf_dt_name (abfd
);
1417 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1418 the ELF linker emulation code. */
1421 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1423 struct bfd_link_needed_list
**pneeded
;
1426 bfd_byte
*dynbuf
= NULL
;
1428 unsigned long shlink
;
1429 bfd_byte
*extdyn
, *extdynend
;
1431 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1435 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1436 || bfd_get_format (abfd
) != bfd_object
)
1439 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1440 if (s
== NULL
|| s
->_raw_size
== 0)
1443 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1447 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1451 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1455 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1457 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1458 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1461 extdynend
= extdyn
+ s
->_raw_size
;
1462 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1464 Elf_Internal_Dyn dyn
;
1466 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1468 if (dyn
.d_tag
== DT_NULL
)
1471 if (dyn
.d_tag
== DT_NEEDED
)
1474 struct bfd_link_needed_list
*l
;
1475 unsigned int tagv
= dyn
.d_un
.d_val
;
1478 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1483 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1504 /* Allocate an ELF string table--force the first byte to be zero. */
1506 struct bfd_strtab_hash
*
1507 _bfd_elf_stringtab_init ()
1509 struct bfd_strtab_hash
*ret
;
1511 ret
= _bfd_stringtab_init ();
1516 loc
= _bfd_stringtab_add (ret
, "", true, false);
1517 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1518 if (loc
== (bfd_size_type
) -1)
1520 _bfd_stringtab_free (ret
);
1527 /* ELF .o/exec file reading */
1529 /* Create a new bfd section from an ELF section header. */
1532 bfd_section_from_shdr (abfd
, shindex
)
1534 unsigned int shindex
;
1536 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1537 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1538 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1541 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1543 switch (hdr
->sh_type
)
1546 /* Inactive section. Throw it away. */
1549 case SHT_PROGBITS
: /* Normal section with contents. */
1550 case SHT_DYNAMIC
: /* Dynamic linking information. */
1551 case SHT_NOBITS
: /* .bss section. */
1552 case SHT_HASH
: /* .hash section. */
1553 case SHT_NOTE
: /* .note section. */
1554 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1556 case SHT_SYMTAB
: /* A symbol table */
1557 if (elf_onesymtab (abfd
) == shindex
)
1560 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1561 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1562 elf_onesymtab (abfd
) = shindex
;
1563 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1564 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1565 abfd
->flags
|= HAS_SYMS
;
1567 /* Sometimes a shared object will map in the symbol table. If
1568 SHF_ALLOC is set, and this is a shared object, then we also
1569 treat this section as a BFD section. We can not base the
1570 decision purely on SHF_ALLOC, because that flag is sometimes
1571 set in a relocateable object file, which would confuse the
1573 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1574 && (abfd
->flags
& DYNAMIC
) != 0
1575 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1580 case SHT_DYNSYM
: /* A dynamic symbol table */
1581 if (elf_dynsymtab (abfd
) == shindex
)
1584 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1585 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1586 elf_dynsymtab (abfd
) = shindex
;
1587 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1588 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1589 abfd
->flags
|= HAS_SYMS
;
1591 /* Besides being a symbol table, we also treat this as a regular
1592 section, so that objcopy can handle it. */
1593 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1595 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1596 if (elf_symtab_shndx (abfd
) == shindex
)
1599 /* Get the associated symbol table. */
1600 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1601 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1604 elf_symtab_shndx (abfd
) = shindex
;
1605 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1606 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1609 case SHT_STRTAB
: /* A string table */
1610 if (hdr
->bfd_section
!= NULL
)
1612 if (ehdr
->e_shstrndx
== shindex
)
1614 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1615 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1619 unsigned int i
, num_sec
;
1621 num_sec
= elf_numsections (abfd
);
1622 for (i
= 1; i
< num_sec
; i
++)
1624 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1625 if (hdr2
->sh_link
== shindex
)
1627 if (! bfd_section_from_shdr (abfd
, i
))
1629 if (elf_onesymtab (abfd
) == i
)
1631 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1632 elf_elfsections (abfd
)[shindex
] =
1633 &elf_tdata (abfd
)->strtab_hdr
;
1636 if (elf_dynsymtab (abfd
) == i
)
1638 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1639 elf_elfsections (abfd
)[shindex
] = hdr
=
1640 &elf_tdata (abfd
)->dynstrtab_hdr
;
1641 /* We also treat this as a regular section, so
1642 that objcopy can handle it. */
1645 #if 0 /* Not handling other string tables specially right now. */
1646 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1647 /* We have a strtab for some random other section. */
1648 newsect
= (asection
*) hdr2
->bfd_section
;
1651 hdr
->bfd_section
= newsect
;
1652 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1654 elf_elfsections (abfd
)[shindex
] = hdr2
;
1660 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1664 /* *These* do a lot of work -- but build no sections! */
1666 asection
*target_sect
;
1667 Elf_Internal_Shdr
*hdr2
;
1668 unsigned int num_sec
= elf_numsections (abfd
);
1670 /* Check for a bogus link to avoid crashing. */
1671 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1672 || hdr
->sh_link
>= num_sec
)
1674 ((*_bfd_error_handler
)
1675 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1676 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1677 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1680 /* For some incomprehensible reason Oracle distributes
1681 libraries for Solaris in which some of the objects have
1682 bogus sh_link fields. It would be nice if we could just
1683 reject them, but, unfortunately, some people need to use
1684 them. We scan through the section headers; if we find only
1685 one suitable symbol table, we clobber the sh_link to point
1686 to it. I hope this doesn't break anything. */
1687 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1688 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1694 for (scan
= 1; scan
< num_sec
; scan
++)
1696 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1697 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1708 hdr
->sh_link
= found
;
1711 /* Get the symbol table. */
1712 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1713 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1716 /* If this reloc section does not use the main symbol table we
1717 don't treat it as a reloc section. BFD can't adequately
1718 represent such a section, so at least for now, we don't
1719 try. We just present it as a normal section. We also
1720 can't use it as a reloc section if it points to the null
1722 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1723 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1725 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1727 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1728 if (target_sect
== NULL
)
1731 if ((target_sect
->flags
& SEC_RELOC
) == 0
1732 || target_sect
->reloc_count
== 0)
1733 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1737 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1738 amt
= sizeof (*hdr2
);
1739 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1740 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1743 elf_elfsections (abfd
)[shindex
] = hdr2
;
1744 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1745 target_sect
->flags
|= SEC_RELOC
;
1746 target_sect
->relocation
= NULL
;
1747 target_sect
->rel_filepos
= hdr
->sh_offset
;
1748 /* In the section to which the relocations apply, mark whether
1749 its relocations are of the REL or RELA variety. */
1750 if (hdr
->sh_size
!= 0)
1751 elf_section_data (target_sect
)->use_rela_p
1752 = (hdr
->sh_type
== SHT_RELA
);
1753 abfd
->flags
|= HAS_RELOC
;
1758 case SHT_GNU_verdef
:
1759 elf_dynverdef (abfd
) = shindex
;
1760 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1761 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1764 case SHT_GNU_versym
:
1765 elf_dynversym (abfd
) = shindex
;
1766 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1767 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1770 case SHT_GNU_verneed
:
1771 elf_dynverref (abfd
) = shindex
;
1772 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1773 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1780 /* Make a section for objcopy and relocatable links. */
1781 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1783 if (hdr
->contents
!= NULL
)
1785 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1786 unsigned int n_elt
= hdr
->sh_size
/ 4;
1789 while (--n_elt
!= 0)
1790 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1791 && elf_next_in_group (s
) != NULL
)
1793 elf_next_in_group (hdr
->bfd_section
) = s
;
1800 /* Check for any processor-specific section types. */
1802 if (bed
->elf_backend_section_from_shdr
)
1803 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1811 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1812 Return SEC for sections that have no elf section, and NULL on error. */
1815 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
1817 struct sym_sec_cache
*cache
;
1819 unsigned long r_symndx
;
1821 unsigned char esym_shndx
[4];
1822 unsigned int isym_shndx
;
1823 Elf_Internal_Shdr
*symtab_hdr
;
1826 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1828 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1829 return cache
->sec
[ent
];
1831 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1832 pos
= symtab_hdr
->sh_offset
;
1833 if (get_elf_backend_data (abfd
)->s
->sizeof_sym
1834 == sizeof (Elf64_External_Sym
))
1836 pos
+= r_symndx
* sizeof (Elf64_External_Sym
);
1837 pos
+= offsetof (Elf64_External_Sym
, st_shndx
);
1838 amt
= sizeof (((Elf64_External_Sym
*) 0)->st_shndx
);
1842 pos
+= r_symndx
* sizeof (Elf32_External_Sym
);
1843 pos
+= offsetof (Elf32_External_Sym
, st_shndx
);
1844 amt
= sizeof (((Elf32_External_Sym
*) 0)->st_shndx
);
1846 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1847 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1849 isym_shndx
= H_GET_16 (abfd
, esym_shndx
);
1851 if (isym_shndx
== SHN_XINDEX
)
1853 Elf_Internal_Shdr
*shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1854 if (shndx_hdr
->sh_size
!= 0)
1856 pos
= shndx_hdr
->sh_offset
;
1857 pos
+= r_symndx
* sizeof (Elf_External_Sym_Shndx
);
1858 amt
= sizeof (Elf_External_Sym_Shndx
);
1859 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1860 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1862 isym_shndx
= H_GET_32 (abfd
, esym_shndx
);
1866 if (cache
->abfd
!= abfd
)
1868 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1871 cache
->indx
[ent
] = r_symndx
;
1872 cache
->sec
[ent
] = sec
;
1873 if (isym_shndx
< SHN_LORESERVE
|| isym_shndx
> SHN_HIRESERVE
)
1876 s
= bfd_section_from_elf_index (abfd
, isym_shndx
);
1878 cache
->sec
[ent
] = s
;
1880 return cache
->sec
[ent
];
1883 /* Given an ELF section number, retrieve the corresponding BFD
1887 bfd_section_from_elf_index (abfd
, index
)
1891 if (index
>= elf_numsections (abfd
))
1893 return elf_elfsections (abfd
)[index
]->bfd_section
;
1897 _bfd_elf_new_section_hook (abfd
, sec
)
1901 struct bfd_elf_section_data
*sdata
;
1902 bfd_size_type amt
= sizeof (*sdata
);
1904 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1907 sec
->used_by_bfd
= (PTR
) sdata
;
1909 /* Indicate whether or not this section should use RELA relocations. */
1911 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1916 /* Create a new bfd section from an ELF program header.
1918 Since program segments have no names, we generate a synthetic name
1919 of the form segment<NUM>, where NUM is generally the index in the
1920 program header table. For segments that are split (see below) we
1921 generate the names segment<NUM>a and segment<NUM>b.
1923 Note that some program segments may have a file size that is different than
1924 (less than) the memory size. All this means is that at execution the
1925 system must allocate the amount of memory specified by the memory size,
1926 but only initialize it with the first "file size" bytes read from the
1927 file. This would occur for example, with program segments consisting
1928 of combined data+bss.
1930 To handle the above situation, this routine generates TWO bfd sections
1931 for the single program segment. The first has the length specified by
1932 the file size of the segment, and the second has the length specified
1933 by the difference between the two sizes. In effect, the segment is split
1934 into it's initialized and uninitialized parts.
1939 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1941 Elf_Internal_Phdr
*hdr
;
1943 const char *typename
;
1950 split
= ((hdr
->p_memsz
> 0)
1951 && (hdr
->p_filesz
> 0)
1952 && (hdr
->p_memsz
> hdr
->p_filesz
));
1953 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1954 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1957 strcpy (name
, namebuf
);
1958 newsect
= bfd_make_section (abfd
, name
);
1959 if (newsect
== NULL
)
1961 newsect
->vma
= hdr
->p_vaddr
;
1962 newsect
->lma
= hdr
->p_paddr
;
1963 newsect
->_raw_size
= hdr
->p_filesz
;
1964 newsect
->filepos
= hdr
->p_offset
;
1965 newsect
->flags
|= SEC_HAS_CONTENTS
;
1966 if (hdr
->p_type
== PT_LOAD
)
1968 newsect
->flags
|= SEC_ALLOC
;
1969 newsect
->flags
|= SEC_LOAD
;
1970 if (hdr
->p_flags
& PF_X
)
1972 /* FIXME: all we known is that it has execute PERMISSION,
1974 newsect
->flags
|= SEC_CODE
;
1977 if (!(hdr
->p_flags
& PF_W
))
1979 newsect
->flags
|= SEC_READONLY
;
1984 sprintf (namebuf
, "%s%db", typename
, index
);
1985 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1988 strcpy (name
, namebuf
);
1989 newsect
= bfd_make_section (abfd
, name
);
1990 if (newsect
== NULL
)
1992 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1993 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1994 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1995 if (hdr
->p_type
== PT_LOAD
)
1997 newsect
->flags
|= SEC_ALLOC
;
1998 if (hdr
->p_flags
& PF_X
)
1999 newsect
->flags
|= SEC_CODE
;
2001 if (!(hdr
->p_flags
& PF_W
))
2002 newsect
->flags
|= SEC_READONLY
;
2009 bfd_section_from_phdr (abfd
, hdr
, index
)
2011 Elf_Internal_Phdr
*hdr
;
2014 struct elf_backend_data
*bed
;
2016 switch (hdr
->p_type
)
2019 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2022 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2025 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2028 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2031 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2033 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2038 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2041 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2044 /* Check for any processor-specific program segment types.
2045 If no handler for them, default to making "segment" sections. */
2046 bed
= get_elf_backend_data (abfd
);
2047 if (bed
->elf_backend_section_from_phdr
)
2048 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2050 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2054 /* Initialize REL_HDR, the section-header for new section, containing
2055 relocations against ASECT. If USE_RELA_P is true, we use RELA
2056 relocations; otherwise, we use REL relocations. */
2059 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2061 Elf_Internal_Shdr
*rel_hdr
;
2066 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2067 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2069 name
= bfd_alloc (abfd
, amt
);
2072 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2074 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2076 if (rel_hdr
->sh_name
== (unsigned int) -1)
2078 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2079 rel_hdr
->sh_entsize
= (use_rela_p
2080 ? bed
->s
->sizeof_rela
2081 : bed
->s
->sizeof_rel
);
2082 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2083 rel_hdr
->sh_flags
= 0;
2084 rel_hdr
->sh_addr
= 0;
2085 rel_hdr
->sh_size
= 0;
2086 rel_hdr
->sh_offset
= 0;
2091 /* Set up an ELF internal section header for a section. */
2094 elf_fake_sections (abfd
, asect
, failedptrarg
)
2099 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2100 boolean
*failedptr
= (boolean
*) failedptrarg
;
2101 Elf_Internal_Shdr
*this_hdr
;
2105 /* We already failed; just get out of the bfd_map_over_sections
2110 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2112 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2113 asect
->name
, false);
2114 if (this_hdr
->sh_name
== (unsigned long) -1)
2120 this_hdr
->sh_flags
= 0;
2122 if ((asect
->flags
& SEC_ALLOC
) != 0
2123 || asect
->user_set_vma
)
2124 this_hdr
->sh_addr
= asect
->vma
;
2126 this_hdr
->sh_addr
= 0;
2128 this_hdr
->sh_offset
= 0;
2129 this_hdr
->sh_size
= asect
->_raw_size
;
2130 this_hdr
->sh_link
= 0;
2131 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2132 /* The sh_entsize and sh_info fields may have been set already by
2133 copy_private_section_data. */
2135 this_hdr
->bfd_section
= asect
;
2136 this_hdr
->contents
= NULL
;
2138 /* FIXME: This should not be based on section names. */
2139 if (strcmp (asect
->name
, ".dynstr") == 0)
2140 this_hdr
->sh_type
= SHT_STRTAB
;
2141 else if (strcmp (asect
->name
, ".hash") == 0)
2143 this_hdr
->sh_type
= SHT_HASH
;
2144 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2146 else if (strcmp (asect
->name
, ".dynsym") == 0)
2148 this_hdr
->sh_type
= SHT_DYNSYM
;
2149 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2151 else if (strcmp (asect
->name
, ".dynamic") == 0)
2153 this_hdr
->sh_type
= SHT_DYNAMIC
;
2154 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2156 else if (strncmp (asect
->name
, ".rela", 5) == 0
2157 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2159 this_hdr
->sh_type
= SHT_RELA
;
2160 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2162 else if (strncmp (asect
->name
, ".rel", 4) == 0
2163 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2165 this_hdr
->sh_type
= SHT_REL
;
2166 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2168 else if (strncmp (asect
->name
, ".note", 5) == 0)
2169 this_hdr
->sh_type
= SHT_NOTE
;
2170 else if (strncmp (asect
->name
, ".stab", 5) == 0
2171 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2172 this_hdr
->sh_type
= SHT_STRTAB
;
2173 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2175 this_hdr
->sh_type
= SHT_GNU_versym
;
2176 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2178 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2180 this_hdr
->sh_type
= SHT_GNU_verdef
;
2181 this_hdr
->sh_entsize
= 0;
2182 /* objcopy or strip will copy over sh_info, but may not set
2183 cverdefs. The linker will set cverdefs, but sh_info will be
2185 if (this_hdr
->sh_info
== 0)
2186 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2188 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2189 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2191 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2193 this_hdr
->sh_type
= SHT_GNU_verneed
;
2194 this_hdr
->sh_entsize
= 0;
2195 /* objcopy or strip will copy over sh_info, but may not set
2196 cverrefs. The linker will set cverrefs, but sh_info will be
2198 if (this_hdr
->sh_info
== 0)
2199 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2201 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2202 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2204 else if ((asect
->flags
& SEC_GROUP
) != 0)
2206 this_hdr
->sh_type
= SHT_GROUP
;
2207 this_hdr
->sh_entsize
= 4;
2209 else if ((asect
->flags
& SEC_ALLOC
) != 0
2210 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
2211 this_hdr
->sh_type
= SHT_NOBITS
;
2213 this_hdr
->sh_type
= SHT_PROGBITS
;
2215 if ((asect
->flags
& SEC_ALLOC
) != 0)
2216 this_hdr
->sh_flags
|= SHF_ALLOC
;
2217 if ((asect
->flags
& SEC_READONLY
) == 0)
2218 this_hdr
->sh_flags
|= SHF_WRITE
;
2219 if ((asect
->flags
& SEC_CODE
) != 0)
2220 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2221 if ((asect
->flags
& SEC_MERGE
) != 0)
2223 this_hdr
->sh_flags
|= SHF_MERGE
;
2224 this_hdr
->sh_entsize
= asect
->entsize
;
2225 if ((asect
->flags
& SEC_STRINGS
) != 0)
2226 this_hdr
->sh_flags
|= SHF_STRINGS
;
2228 if (elf_group_name (asect
) != NULL
)
2229 this_hdr
->sh_flags
|= SHF_GROUP
;
2231 /* Check for processor-specific section types. */
2232 if (bed
->elf_backend_fake_sections
2233 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2236 /* If the section has relocs, set up a section header for the
2237 SHT_REL[A] section. If two relocation sections are required for
2238 this section, it is up to the processor-specific back-end to
2239 create the other. */
2240 if ((asect
->flags
& SEC_RELOC
) != 0
2241 && !_bfd_elf_init_reloc_shdr (abfd
,
2242 &elf_section_data (asect
)->rel_hdr
,
2244 elf_section_data (asect
)->use_rela_p
))
2248 /* Fill in the contents of a SHT_GROUP section. */
2251 set_group_contents (abfd
, sec
, failedptrarg
)
2254 PTR failedptrarg ATTRIBUTE_UNUSED
;
2256 boolean
*failedptr
= (boolean
*) failedptrarg
;
2257 unsigned long symindx
;
2260 struct bfd_link_order
*l
;
2262 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2266 /* If called from the assembler, swap_out_syms will have set up
2267 elf_section_syms; If called for "ld -r", the symbols won't yet
2268 be mapped, so emulate elf_bfd_final_link. */
2269 if (elf_section_syms (abfd
) != NULL
)
2270 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2272 symindx
= elf_section_data (sec
)->this_idx
;
2273 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2275 /* Nor will the contents be allocated for "ld -r". */
2276 if (sec
->contents
== NULL
)
2278 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2279 if (sec
->contents
== NULL
)
2286 loc
= sec
->contents
+ sec
->_raw_size
;
2288 /* Get the pointer to the first section in the group that we
2289 squirreled away here. */
2290 elt
= elf_next_in_group (sec
);
2292 /* First element is a flag word. Rest of section is elf section
2293 indices for all the sections of the group. Write them backwards
2294 just to keep the group in the same order as given in .section
2295 directives, not that it matters. */
2299 H_PUT_32 (abfd
, elf_section_data (elt
)->this_idx
, loc
);
2300 elt
= elf_next_in_group (elt
);
2303 /* If this is a relocatable link, then the above did nothing because
2304 SEC is the output section. Look through the input sections
2306 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2307 if (l
->type
== bfd_indirect_link_order
2308 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2313 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2314 elt
= elf_next_in_group (elt
);
2315 /* During a relocatable link, the lists are circular. */
2317 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2320 H_PUT_32 (abfd
, 0, loc
);
2322 BFD_ASSERT (loc
== sec
->contents
);
2325 /* Assign all ELF section numbers. The dummy first section is handled here
2326 too. The link/info pointers for the standard section types are filled
2327 in here too, while we're at it. */
2330 assign_section_numbers (abfd
)
2333 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2335 unsigned int section_number
, secn
;
2336 Elf_Internal_Shdr
**i_shdrp
;
2341 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2343 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2345 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2347 if (section_number
== SHN_LORESERVE
)
2348 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2349 d
->this_idx
= section_number
++;
2350 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2351 if ((sec
->flags
& SEC_RELOC
) == 0)
2355 if (section_number
== SHN_LORESERVE
)
2356 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2357 d
->rel_idx
= section_number
++;
2358 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2363 if (section_number
== SHN_LORESERVE
)
2364 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2365 d
->rel_idx2
= section_number
++;
2366 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2372 if (section_number
== SHN_LORESERVE
)
2373 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2374 t
->shstrtab_section
= section_number
++;
2375 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2376 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2378 if (bfd_get_symcount (abfd
) > 0)
2380 if (section_number
== SHN_LORESERVE
)
2381 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2382 t
->symtab_section
= section_number
++;
2383 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2384 if (section_number
> SHN_LORESERVE
- 2)
2386 if (section_number
== SHN_LORESERVE
)
2387 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2388 t
->symtab_shndx_section
= section_number
++;
2389 t
->symtab_shndx_hdr
.sh_name
2390 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2391 ".symtab_shndx", false);
2392 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2395 if (section_number
== SHN_LORESERVE
)
2396 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2397 t
->strtab_section
= section_number
++;
2398 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2401 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2402 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2404 elf_numsections (abfd
) = section_number
;
2405 elf_elfheader (abfd
)->e_shnum
= section_number
;
2406 if (section_number
> SHN_LORESERVE
)
2407 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2409 /* Set up the list of section header pointers, in agreement with the
2411 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2412 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2413 if (i_shdrp
== NULL
)
2416 amt
= sizeof (Elf_Internal_Shdr
);
2417 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2418 if (i_shdrp
[0] == NULL
)
2420 bfd_release (abfd
, i_shdrp
);
2423 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2425 elf_elfsections (abfd
) = i_shdrp
;
2427 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2428 if (bfd_get_symcount (abfd
) > 0)
2430 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2431 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2433 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2434 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2436 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2437 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2439 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2441 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2445 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2446 if (d
->rel_idx
!= 0)
2447 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2448 if (d
->rel_idx2
!= 0)
2449 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2451 /* Fill in the sh_link and sh_info fields while we're at it. */
2453 /* sh_link of a reloc section is the section index of the symbol
2454 table. sh_info is the section index of the section to which
2455 the relocation entries apply. */
2456 if (d
->rel_idx
!= 0)
2458 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2459 d
->rel_hdr
.sh_info
= d
->this_idx
;
2461 if (d
->rel_idx2
!= 0)
2463 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2464 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2467 switch (d
->this_hdr
.sh_type
)
2471 /* A reloc section which we are treating as a normal BFD
2472 section. sh_link is the section index of the symbol
2473 table. sh_info is the section index of the section to
2474 which the relocation entries apply. We assume that an
2475 allocated reloc section uses the dynamic symbol table.
2476 FIXME: How can we be sure? */
2477 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2479 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2481 /* We look up the section the relocs apply to by name. */
2483 if (d
->this_hdr
.sh_type
== SHT_REL
)
2487 s
= bfd_get_section_by_name (abfd
, name
);
2489 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2493 /* We assume that a section named .stab*str is a stabs
2494 string section. We look for a section with the same name
2495 but without the trailing ``str'', and set its sh_link
2496 field to point to this section. */
2497 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2498 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2503 len
= strlen (sec
->name
);
2504 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2507 strncpy (alc
, sec
->name
, len
- 3);
2508 alc
[len
- 3] = '\0';
2509 s
= bfd_get_section_by_name (abfd
, alc
);
2513 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2515 /* This is a .stab section. */
2516 elf_section_data (s
)->this_hdr
.sh_entsize
=
2517 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2524 case SHT_GNU_verneed
:
2525 case SHT_GNU_verdef
:
2526 /* sh_link is the section header index of the string table
2527 used for the dynamic entries, or the symbol table, or the
2529 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2531 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2535 case SHT_GNU_versym
:
2536 /* sh_link is the section header index of the symbol table
2537 this hash table or version table is for. */
2538 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2540 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2544 d
->this_hdr
.sh_link
= t
->symtab_section
;
2548 for (secn
= 1; secn
< section_number
; ++secn
)
2549 if (i_shdrp
[secn
] == NULL
)
2550 i_shdrp
[secn
] = i_shdrp
[0];
2552 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2553 i_shdrp
[secn
]->sh_name
);
2557 /* Map symbol from it's internal number to the external number, moving
2558 all local symbols to be at the head of the list. */
2561 sym_is_global (abfd
, sym
)
2565 /* If the backend has a special mapping, use it. */
2566 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2567 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2570 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2571 || bfd_is_und_section (bfd_get_section (sym
))
2572 || bfd_is_com_section (bfd_get_section (sym
)));
2576 elf_map_symbols (abfd
)
2579 unsigned int symcount
= bfd_get_symcount (abfd
);
2580 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2581 asymbol
**sect_syms
;
2582 unsigned int num_locals
= 0;
2583 unsigned int num_globals
= 0;
2584 unsigned int num_locals2
= 0;
2585 unsigned int num_globals2
= 0;
2593 fprintf (stderr
, "elf_map_symbols\n");
2597 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2599 if (max_index
< asect
->index
)
2600 max_index
= asect
->index
;
2604 amt
= max_index
* sizeof (asymbol
*);
2605 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2606 if (sect_syms
== NULL
)
2608 elf_section_syms (abfd
) = sect_syms
;
2609 elf_num_section_syms (abfd
) = max_index
;
2611 /* Init sect_syms entries for any section symbols we have already
2612 decided to output. */
2613 for (idx
= 0; idx
< symcount
; idx
++)
2615 asymbol
*sym
= syms
[idx
];
2617 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2624 if (sec
->owner
!= NULL
)
2626 if (sec
->owner
!= abfd
)
2628 if (sec
->output_offset
!= 0)
2631 sec
= sec
->output_section
;
2633 /* Empty sections in the input files may have had a
2634 section symbol created for them. (See the comment
2635 near the end of _bfd_generic_link_output_symbols in
2636 linker.c). If the linker script discards such
2637 sections then we will reach this point. Since we know
2638 that we cannot avoid this case, we detect it and skip
2639 the abort and the assignment to the sect_syms array.
2640 To reproduce this particular case try running the
2641 linker testsuite test ld-scripts/weak.exp for an ELF
2642 port that uses the generic linker. */
2643 if (sec
->owner
== NULL
)
2646 BFD_ASSERT (sec
->owner
== abfd
);
2648 sect_syms
[sec
->index
] = syms
[idx
];
2653 /* Classify all of the symbols. */
2654 for (idx
= 0; idx
< symcount
; idx
++)
2656 if (!sym_is_global (abfd
, syms
[idx
]))
2662 /* We will be adding a section symbol for each BFD section. Most normal
2663 sections will already have a section symbol in outsymbols, but
2664 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2665 at least in that case. */
2666 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2668 if (sect_syms
[asect
->index
] == NULL
)
2670 if (!sym_is_global (abfd
, asect
->symbol
))
2677 /* Now sort the symbols so the local symbols are first. */
2678 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2679 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2681 if (new_syms
== NULL
)
2684 for (idx
= 0; idx
< symcount
; idx
++)
2686 asymbol
*sym
= syms
[idx
];
2689 if (!sym_is_global (abfd
, sym
))
2692 i
= num_locals
+ num_globals2
++;
2694 sym
->udata
.i
= i
+ 1;
2696 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2698 if (sect_syms
[asect
->index
] == NULL
)
2700 asymbol
*sym
= asect
->symbol
;
2703 sect_syms
[asect
->index
] = sym
;
2704 if (!sym_is_global (abfd
, sym
))
2707 i
= num_locals
+ num_globals2
++;
2709 sym
->udata
.i
= i
+ 1;
2713 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2715 elf_num_locals (abfd
) = num_locals
;
2716 elf_num_globals (abfd
) = num_globals
;
2720 /* Align to the maximum file alignment that could be required for any
2721 ELF data structure. */
2723 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2724 static INLINE file_ptr
2725 align_file_position (off
, align
)
2729 return (off
+ align
- 1) & ~(align
- 1);
2732 /* Assign a file position to a section, optionally aligning to the
2733 required section alignment. */
2736 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2737 Elf_Internal_Shdr
*i_shdrp
;
2745 al
= i_shdrp
->sh_addralign
;
2747 offset
= BFD_ALIGN (offset
, al
);
2749 i_shdrp
->sh_offset
= offset
;
2750 if (i_shdrp
->bfd_section
!= NULL
)
2751 i_shdrp
->bfd_section
->filepos
= offset
;
2752 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2753 offset
+= i_shdrp
->sh_size
;
2757 /* Compute the file positions we are going to put the sections at, and
2758 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2759 is not NULL, this is being called by the ELF backend linker. */
2762 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2764 struct bfd_link_info
*link_info
;
2766 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2768 struct bfd_strtab_hash
*strtab
;
2769 Elf_Internal_Shdr
*shstrtab_hdr
;
2771 if (abfd
->output_has_begun
)
2774 /* Do any elf backend specific processing first. */
2775 if (bed
->elf_backend_begin_write_processing
)
2776 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2778 if (! prep_headers (abfd
))
2781 /* Post process the headers if necessary. */
2782 if (bed
->elf_backend_post_process_headers
)
2783 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2786 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2790 if (!assign_section_numbers (abfd
))
2793 /* The backend linker builds symbol table information itself. */
2794 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2796 /* Non-zero if doing a relocatable link. */
2797 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2799 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2803 if (link_info
== NULL
|| link_info
->relocateable
)
2805 bfd_map_over_sections (abfd
, set_group_contents
, &failed
);
2810 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2811 /* sh_name was set in prep_headers. */
2812 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2813 shstrtab_hdr
->sh_flags
= 0;
2814 shstrtab_hdr
->sh_addr
= 0;
2815 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2816 shstrtab_hdr
->sh_entsize
= 0;
2817 shstrtab_hdr
->sh_link
= 0;
2818 shstrtab_hdr
->sh_info
= 0;
2819 /* sh_offset is set in assign_file_positions_except_relocs. */
2820 shstrtab_hdr
->sh_addralign
= 1;
2822 if (!assign_file_positions_except_relocs (abfd
))
2825 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2828 Elf_Internal_Shdr
*hdr
;
2830 off
= elf_tdata (abfd
)->next_file_pos
;
2832 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2833 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2835 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2836 if (hdr
->sh_size
!= 0)
2837 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2839 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2840 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2842 elf_tdata (abfd
)->next_file_pos
= off
;
2844 /* Now that we know where the .strtab section goes, write it
2846 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2847 || ! _bfd_stringtab_emit (abfd
, strtab
))
2849 _bfd_stringtab_free (strtab
);
2852 abfd
->output_has_begun
= true;
2857 /* Create a mapping from a set of sections to a program segment. */
2859 static INLINE
struct elf_segment_map
*
2860 make_mapping (abfd
, sections
, from
, to
, phdr
)
2862 asection
**sections
;
2867 struct elf_segment_map
*m
;
2872 amt
= sizeof (struct elf_segment_map
);
2873 amt
+= (to
- from
- 1) * sizeof (asection
*);
2874 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2878 m
->p_type
= PT_LOAD
;
2879 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2880 m
->sections
[i
- from
] = *hdrpp
;
2881 m
->count
= to
- from
;
2883 if (from
== 0 && phdr
)
2885 /* Include the headers in the first PT_LOAD segment. */
2886 m
->includes_filehdr
= 1;
2887 m
->includes_phdrs
= 1;
2893 /* Set up a mapping from BFD sections to program segments. */
2896 map_sections_to_segments (abfd
)
2899 asection
**sections
= NULL
;
2903 struct elf_segment_map
*mfirst
;
2904 struct elf_segment_map
**pm
;
2905 struct elf_segment_map
*m
;
2907 unsigned int phdr_index
;
2908 bfd_vma maxpagesize
;
2910 boolean phdr_in_segment
= true;
2912 asection
*dynsec
, *eh_frame_hdr
;
2915 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2918 if (bfd_count_sections (abfd
) == 0)
2921 /* Select the allocated sections, and sort them. */
2923 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
2924 sections
= (asection
**) bfd_malloc (amt
);
2925 if (sections
== NULL
)
2929 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2931 if ((s
->flags
& SEC_ALLOC
) != 0)
2937 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2940 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2942 /* Build the mapping. */
2947 /* If we have a .interp section, then create a PT_PHDR segment for
2948 the program headers and a PT_INTERP segment for the .interp
2950 s
= bfd_get_section_by_name (abfd
, ".interp");
2951 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2953 amt
= sizeof (struct elf_segment_map
);
2954 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2958 m
->p_type
= PT_PHDR
;
2959 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2960 m
->p_flags
= PF_R
| PF_X
;
2961 m
->p_flags_valid
= 1;
2962 m
->includes_phdrs
= 1;
2967 amt
= sizeof (struct elf_segment_map
);
2968 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2972 m
->p_type
= PT_INTERP
;
2980 /* Look through the sections. We put sections in the same program
2981 segment when the start of the second section can be placed within
2982 a few bytes of the end of the first section. */
2985 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2987 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2989 && (dynsec
->flags
& SEC_LOAD
) == 0)
2992 /* Deal with -Ttext or something similar such that the first section
2993 is not adjacent to the program headers. This is an
2994 approximation, since at this point we don't know exactly how many
2995 program headers we will need. */
2998 bfd_size_type phdr_size
;
3000 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3002 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3003 if ((abfd
->flags
& D_PAGED
) == 0
3004 || sections
[0]->lma
< phdr_size
3005 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3006 phdr_in_segment
= false;
3009 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3012 boolean new_segment
;
3016 /* See if this section and the last one will fit in the same
3019 if (last_hdr
== NULL
)
3021 /* If we don't have a segment yet, then we don't need a new
3022 one (we build the last one after this loop). */
3023 new_segment
= false;
3025 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3027 /* If this section has a different relation between the
3028 virtual address and the load address, then we need a new
3032 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3033 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3035 /* If putting this section in this segment would force us to
3036 skip a page in the segment, then we need a new segment. */
3039 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3040 && (hdr
->flags
& SEC_LOAD
) != 0)
3042 /* We don't want to put a loadable section after a
3043 nonloadable section in the same segment. */
3046 else if ((abfd
->flags
& D_PAGED
) == 0)
3048 /* If the file is not demand paged, which means that we
3049 don't require the sections to be correctly aligned in the
3050 file, then there is no other reason for a new segment. */
3051 new_segment
= false;
3054 && (hdr
->flags
& SEC_READONLY
) == 0
3055 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3058 /* We don't want to put a writable section in a read only
3059 segment, unless they are on the same page in memory
3060 anyhow. We already know that the last section does not
3061 bring us past the current section on the page, so the
3062 only case in which the new section is not on the same
3063 page as the previous section is when the previous section
3064 ends precisely on a page boundary. */
3069 /* Otherwise, we can use the same segment. */
3070 new_segment
= false;
3075 if ((hdr
->flags
& SEC_READONLY
) == 0)
3081 /* We need a new program segment. We must create a new program
3082 header holding all the sections from phdr_index until hdr. */
3084 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3091 if ((hdr
->flags
& SEC_READONLY
) == 0)
3098 phdr_in_segment
= false;
3101 /* Create a final PT_LOAD program segment. */
3102 if (last_hdr
!= NULL
)
3104 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3112 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3115 amt
= sizeof (struct elf_segment_map
);
3116 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3120 m
->p_type
= PT_DYNAMIC
;
3122 m
->sections
[0] = dynsec
;
3128 /* For each loadable .note section, add a PT_NOTE segment. We don't
3129 use bfd_get_section_by_name, because if we link together
3130 nonloadable .note sections and loadable .note sections, we will
3131 generate two .note sections in the output file. FIXME: Using
3132 names for section types is bogus anyhow. */
3133 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3135 if ((s
->flags
& SEC_LOAD
) != 0
3136 && strncmp (s
->name
, ".note", 5) == 0)
3138 amt
= sizeof (struct elf_segment_map
);
3139 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3143 m
->p_type
= PT_NOTE
;
3152 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3154 eh_frame_hdr
= NULL
;
3155 if (elf_tdata (abfd
)->eh_frame_hdr
)
3156 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3157 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3159 amt
= sizeof (struct elf_segment_map
);
3160 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3164 m
->p_type
= PT_GNU_EH_FRAME
;
3166 m
->sections
[0] = eh_frame_hdr
;
3175 elf_tdata (abfd
)->segment_map
= mfirst
;
3179 if (sections
!= NULL
)
3184 /* Sort sections by address. */
3187 elf_sort_sections (arg1
, arg2
)
3191 const asection
*sec1
= *(const asection
**) arg1
;
3192 const asection
*sec2
= *(const asection
**) arg2
;
3194 /* Sort by LMA first, since this is the address used to
3195 place the section into a segment. */
3196 if (sec1
->lma
< sec2
->lma
)
3198 else if (sec1
->lma
> sec2
->lma
)
3201 /* Then sort by VMA. Normally the LMA and the VMA will be
3202 the same, and this will do nothing. */
3203 if (sec1
->vma
< sec2
->vma
)
3205 else if (sec1
->vma
> sec2
->vma
)
3208 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3210 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3216 /* If the indicies are the same, do not return 0
3217 here, but continue to try the next comparison. */
3218 if (sec1
->target_index
- sec2
->target_index
!= 0)
3219 return sec1
->target_index
- sec2
->target_index
;
3224 else if (TOEND (sec2
))
3229 /* Sort by size, to put zero sized sections
3230 before others at the same address. */
3232 if (sec1
->_raw_size
< sec2
->_raw_size
)
3234 if (sec1
->_raw_size
> sec2
->_raw_size
)
3237 return sec1
->target_index
- sec2
->target_index
;
3240 /* Assign file positions to the sections based on the mapping from
3241 sections to segments. This function also sets up some fields in
3242 the file header, and writes out the program headers. */
3245 assign_file_positions_for_segments (abfd
)
3248 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3250 struct elf_segment_map
*m
;
3252 Elf_Internal_Phdr
*phdrs
;
3254 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3255 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3256 Elf_Internal_Phdr
*p
;
3259 if (elf_tdata (abfd
)->segment_map
== NULL
)
3261 if (! map_sections_to_segments (abfd
))
3265 if (bed
->elf_backend_modify_segment_map
)
3267 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3272 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3275 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3276 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3277 elf_elfheader (abfd
)->e_phnum
= count
;
3282 /* If we already counted the number of program segments, make sure
3283 that we allocated enough space. This happens when SIZEOF_HEADERS
3284 is used in a linker script. */
3285 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3286 if (alloc
!= 0 && count
> alloc
)
3288 ((*_bfd_error_handler
)
3289 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3290 bfd_get_filename (abfd
), alloc
, count
));
3291 bfd_set_error (bfd_error_bad_value
);
3298 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3299 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3303 off
= bed
->s
->sizeof_ehdr
;
3304 off
+= alloc
* bed
->s
->sizeof_phdr
;
3311 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3318 /* If elf_segment_map is not from map_sections_to_segments, the
3319 sections may not be correctly ordered. NOTE: sorting should
3320 not be done to the PT_NOTE section of a corefile, which may
3321 contain several pseudo-sections artificially created by bfd.
3322 Sorting these pseudo-sections breaks things badly. */
3324 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3325 && m
->p_type
== PT_NOTE
))
3326 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3329 p
->p_type
= m
->p_type
;
3330 p
->p_flags
= m
->p_flags
;
3332 if (p
->p_type
== PT_LOAD
3334 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3336 if ((abfd
->flags
& D_PAGED
) != 0)
3337 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3340 bfd_size_type align
;
3343 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3345 bfd_size_type secalign
;
3347 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3348 if (secalign
> align
)
3352 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3359 p
->p_vaddr
= m
->sections
[0]->vma
;
3361 if (m
->p_paddr_valid
)
3362 p
->p_paddr
= m
->p_paddr
;
3363 else if (m
->count
== 0)
3366 p
->p_paddr
= m
->sections
[0]->lma
;
3368 if (p
->p_type
== PT_LOAD
3369 && (abfd
->flags
& D_PAGED
) != 0)
3370 p
->p_align
= bed
->maxpagesize
;
3371 else if (m
->count
== 0)
3372 p
->p_align
= bed
->s
->file_align
;
3380 if (m
->includes_filehdr
)
3382 if (! m
->p_flags_valid
)
3385 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3386 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3389 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3391 if (p
->p_vaddr
< (bfd_vma
) off
)
3393 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3394 bfd_get_filename (abfd
));
3395 bfd_set_error (bfd_error_bad_value
);
3400 if (! m
->p_paddr_valid
)
3403 if (p
->p_type
== PT_LOAD
)
3405 filehdr_vaddr
= p
->p_vaddr
;
3406 filehdr_paddr
= p
->p_paddr
;
3410 if (m
->includes_phdrs
)
3412 if (! m
->p_flags_valid
)
3415 if (m
->includes_filehdr
)
3417 if (p
->p_type
== PT_LOAD
)
3419 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3420 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3425 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3429 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3430 p
->p_vaddr
-= off
- p
->p_offset
;
3431 if (! m
->p_paddr_valid
)
3432 p
->p_paddr
-= off
- p
->p_offset
;
3435 if (p
->p_type
== PT_LOAD
)
3437 phdrs_vaddr
= p
->p_vaddr
;
3438 phdrs_paddr
= p
->p_paddr
;
3441 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3444 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3445 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3448 if (p
->p_type
== PT_LOAD
3449 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3451 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3457 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3458 p
->p_filesz
+= adjust
;
3459 p
->p_memsz
+= adjust
;
3465 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3469 bfd_size_type align
;
3473 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3475 /* The section may have artificial alignment forced by a
3476 link script. Notice this case by the gap between the
3477 cumulative phdr lma and the section's lma. */
3478 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3480 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3482 p
->p_memsz
+= adjust
;
3485 if ((flags
& SEC_LOAD
) != 0)
3486 p
->p_filesz
+= adjust
;
3489 if (p
->p_type
== PT_LOAD
)
3491 bfd_signed_vma adjust
;
3493 if ((flags
& SEC_LOAD
) != 0)
3495 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3499 else if ((flags
& SEC_ALLOC
) != 0)
3501 /* The section VMA must equal the file position
3502 modulo the page size. FIXME: I'm not sure if
3503 this adjustment is really necessary. We used to
3504 not have the SEC_LOAD case just above, and then
3505 this was necessary, but now I'm not sure. */
3506 if ((abfd
->flags
& D_PAGED
) != 0)
3507 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3509 adjust
= (sec
->vma
- voff
) % align
;
3518 (* _bfd_error_handler
) (_("\
3519 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3520 bfd_section_name (abfd
, sec
),
3525 p
->p_memsz
+= adjust
;
3528 if ((flags
& SEC_LOAD
) != 0)
3529 p
->p_filesz
+= adjust
;
3534 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3535 used in a linker script we may have a section with
3536 SEC_LOAD clear but which is supposed to have
3538 if ((flags
& SEC_LOAD
) != 0
3539 || (flags
& SEC_HAS_CONTENTS
) != 0)
3540 off
+= sec
->_raw_size
;
3542 if ((flags
& SEC_ALLOC
) != 0)
3543 voff
+= sec
->_raw_size
;
3546 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3548 /* The actual "note" segment has i == 0.
3549 This is the one that actually contains everything. */
3553 p
->p_filesz
= sec
->_raw_size
;
3554 off
+= sec
->_raw_size
;
3559 /* Fake sections -- don't need to be written. */
3562 flags
= sec
->flags
= 0;
3569 p
->p_memsz
+= sec
->_raw_size
;
3571 if ((flags
& SEC_LOAD
) != 0)
3572 p
->p_filesz
+= sec
->_raw_size
;
3574 if (align
> p
->p_align
3575 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3579 if (! m
->p_flags_valid
)
3582 if ((flags
& SEC_CODE
) != 0)
3584 if ((flags
& SEC_READONLY
) == 0)
3590 /* Now that we have set the section file positions, we can set up
3591 the file positions for the non PT_LOAD segments. */
3592 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3596 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3598 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3599 p
->p_offset
= m
->sections
[0]->filepos
;
3603 if (m
->includes_filehdr
)
3605 p
->p_vaddr
= filehdr_vaddr
;
3606 if (! m
->p_paddr_valid
)
3607 p
->p_paddr
= filehdr_paddr
;
3609 else if (m
->includes_phdrs
)
3611 p
->p_vaddr
= phdrs_vaddr
;
3612 if (! m
->p_paddr_valid
)
3613 p
->p_paddr
= phdrs_paddr
;
3618 /* Clear out any program headers we allocated but did not use. */
3619 for (; count
< alloc
; count
++, p
++)
3621 memset (p
, 0, sizeof *p
);
3622 p
->p_type
= PT_NULL
;
3625 elf_tdata (abfd
)->phdr
= phdrs
;
3627 elf_tdata (abfd
)->next_file_pos
= off
;
3629 /* Write out the program headers. */
3630 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3631 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3637 /* Get the size of the program header.
3639 If this is called by the linker before any of the section VMA's are set, it
3640 can't calculate the correct value for a strange memory layout. This only
3641 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3642 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3643 data segment (exclusive of .interp and .dynamic).
3645 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3646 will be two segments. */
3648 static bfd_size_type
3649 get_program_header_size (abfd
)
3654 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3656 /* We can't return a different result each time we're called. */
3657 if (elf_tdata (abfd
)->program_header_size
!= 0)
3658 return elf_tdata (abfd
)->program_header_size
;
3660 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3662 struct elf_segment_map
*m
;
3665 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3667 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3668 return elf_tdata (abfd
)->program_header_size
;
3671 /* Assume we will need exactly two PT_LOAD segments: one for text
3672 and one for data. */
3675 s
= bfd_get_section_by_name (abfd
, ".interp");
3676 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3678 /* If we have a loadable interpreter section, we need a
3679 PT_INTERP segment. In this case, assume we also need a
3680 PT_PHDR segment, although that may not be true for all
3685 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3687 /* We need a PT_DYNAMIC segment. */
3691 if (elf_tdata (abfd
)->eh_frame_hdr
3692 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
3694 /* We need a PT_GNU_EH_FRAME segment. */
3698 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3700 if ((s
->flags
& SEC_LOAD
) != 0
3701 && strncmp (s
->name
, ".note", 5) == 0)
3703 /* We need a PT_NOTE segment. */
3708 /* Let the backend count up any program headers it might need. */
3709 if (bed
->elf_backend_additional_program_headers
)
3713 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3719 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3720 return elf_tdata (abfd
)->program_header_size
;
3723 /* Work out the file positions of all the sections. This is called by
3724 _bfd_elf_compute_section_file_positions. All the section sizes and
3725 VMAs must be known before this is called.
3727 We do not consider reloc sections at this point, unless they form
3728 part of the loadable image. Reloc sections are assigned file
3729 positions in assign_file_positions_for_relocs, which is called by
3730 write_object_contents and final_link.
3732 We also don't set the positions of the .symtab and .strtab here. */
3735 assign_file_positions_except_relocs (abfd
)
3738 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3739 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3740 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3741 unsigned int num_sec
= elf_numsections (abfd
);
3743 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3745 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3746 && bfd_get_format (abfd
) != bfd_core
)
3748 Elf_Internal_Shdr
**hdrpp
;
3751 /* Start after the ELF header. */
3752 off
= i_ehdrp
->e_ehsize
;
3754 /* We are not creating an executable, which means that we are
3755 not creating a program header, and that the actual order of
3756 the sections in the file is unimportant. */
3757 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3759 Elf_Internal_Shdr
*hdr
;
3762 if (hdr
->sh_type
== SHT_REL
3763 || hdr
->sh_type
== SHT_RELA
3764 || i
== tdata
->symtab_section
3765 || i
== tdata
->symtab_shndx_section
3766 || i
== tdata
->strtab_section
)
3768 hdr
->sh_offset
= -1;
3771 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3773 if (i
== SHN_LORESERVE
- 1)
3775 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3776 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3783 Elf_Internal_Shdr
**hdrpp
;
3785 /* Assign file positions for the loaded sections based on the
3786 assignment of sections to segments. */
3787 if (! assign_file_positions_for_segments (abfd
))
3790 /* Assign file positions for the other sections. */
3792 off
= elf_tdata (abfd
)->next_file_pos
;
3793 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3795 Elf_Internal_Shdr
*hdr
;
3798 if (hdr
->bfd_section
!= NULL
3799 && hdr
->bfd_section
->filepos
!= 0)
3800 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3801 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3803 ((*_bfd_error_handler
)
3804 (_("%s: warning: allocated section `%s' not in segment"),
3805 bfd_get_filename (abfd
),
3806 (hdr
->bfd_section
== NULL
3808 : hdr
->bfd_section
->name
)));
3809 if ((abfd
->flags
& D_PAGED
) != 0)
3810 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3812 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3813 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3816 else if (hdr
->sh_type
== SHT_REL
3817 || hdr
->sh_type
== SHT_RELA
3818 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3819 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
3820 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3821 hdr
->sh_offset
= -1;
3823 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3825 if (i
== SHN_LORESERVE
- 1)
3827 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3828 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3833 /* Place the section headers. */
3834 off
= align_file_position (off
, bed
->s
->file_align
);
3835 i_ehdrp
->e_shoff
= off
;
3836 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3838 elf_tdata (abfd
)->next_file_pos
= off
;
3847 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3848 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3849 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3851 struct elf_strtab_hash
*shstrtab
;
3852 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3854 i_ehdrp
= elf_elfheader (abfd
);
3855 i_shdrp
= elf_elfsections (abfd
);
3857 shstrtab
= _bfd_elf_strtab_init ();
3858 if (shstrtab
== NULL
)
3861 elf_shstrtab (abfd
) = shstrtab
;
3863 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3864 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3865 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3866 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3868 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3869 i_ehdrp
->e_ident
[EI_DATA
] =
3870 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3871 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3873 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3874 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3876 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3877 i_ehdrp
->e_ident
[count
] = 0;
3879 if ((abfd
->flags
& DYNAMIC
) != 0)
3880 i_ehdrp
->e_type
= ET_DYN
;
3881 else if ((abfd
->flags
& EXEC_P
) != 0)
3882 i_ehdrp
->e_type
= ET_EXEC
;
3883 else if (bfd_get_format (abfd
) == bfd_core
)
3884 i_ehdrp
->e_type
= ET_CORE
;
3886 i_ehdrp
->e_type
= ET_REL
;
3888 switch (bfd_get_arch (abfd
))
3890 case bfd_arch_unknown
:
3891 i_ehdrp
->e_machine
= EM_NONE
;
3894 /* There used to be a long list of cases here, each one setting
3895 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3896 in the corresponding bfd definition. To avoid duplication,
3897 the switch was removed. Machines that need special handling
3898 can generally do it in elf_backend_final_write_processing(),
3899 unless they need the information earlier than the final write.
3900 Such need can generally be supplied by replacing the tests for
3901 e_machine with the conditions used to determine it. */
3903 if (get_elf_backend_data (abfd
) != NULL
)
3904 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
3906 i_ehdrp
->e_machine
= EM_NONE
;
3909 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3910 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3912 /* No program header, for now. */
3913 i_ehdrp
->e_phoff
= 0;
3914 i_ehdrp
->e_phentsize
= 0;
3915 i_ehdrp
->e_phnum
= 0;
3917 /* Each bfd section is section header entry. */
3918 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3919 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3921 /* If we're building an executable, we'll need a program header table. */
3922 if (abfd
->flags
& EXEC_P
)
3924 /* It all happens later. */
3926 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3928 /* elf_build_phdrs() returns a (NULL-terminated) array of
3929 Elf_Internal_Phdrs. */
3930 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3931 i_ehdrp
->e_phoff
= outbase
;
3932 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3937 i_ehdrp
->e_phentsize
= 0;
3939 i_ehdrp
->e_phoff
= 0;
3942 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3943 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
3944 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3945 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
3946 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3947 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
3948 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3949 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3950 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3956 /* Assign file positions for all the reloc sections which are not part
3957 of the loadable file image. */
3960 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3964 unsigned int i
, num_sec
;
3965 Elf_Internal_Shdr
**shdrpp
;
3967 off
= elf_tdata (abfd
)->next_file_pos
;
3969 num_sec
= elf_numsections (abfd
);
3970 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
3972 Elf_Internal_Shdr
*shdrp
;
3975 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3976 && shdrp
->sh_offset
== -1)
3977 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3980 elf_tdata (abfd
)->next_file_pos
= off
;
3984 _bfd_elf_write_object_contents (abfd
)
3987 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3988 Elf_Internal_Ehdr
*i_ehdrp
;
3989 Elf_Internal_Shdr
**i_shdrp
;
3991 unsigned int count
, num_sec
;
3993 if (! abfd
->output_has_begun
3994 && ! _bfd_elf_compute_section_file_positions
3995 (abfd
, (struct bfd_link_info
*) NULL
))
3998 i_shdrp
= elf_elfsections (abfd
);
3999 i_ehdrp
= elf_elfheader (abfd
);
4002 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4006 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4008 /* After writing the headers, we need to write the sections too... */
4009 num_sec
= elf_numsections (abfd
);
4010 for (count
= 1; count
< num_sec
; count
++)
4012 if (bed
->elf_backend_section_processing
)
4013 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4014 if (i_shdrp
[count
]->contents
)
4016 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4018 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4019 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4022 if (count
== SHN_LORESERVE
- 1)
4023 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4026 /* Write out the section header names. */
4027 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4028 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4031 if (bed
->elf_backend_final_write_processing
)
4032 (*bed
->elf_backend_final_write_processing
) (abfd
,
4033 elf_tdata (abfd
)->linker
);
4035 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4039 _bfd_elf_write_corefile_contents (abfd
)
4042 /* Hopefully this can be done just like an object file. */
4043 return _bfd_elf_write_object_contents (abfd
);
4046 /* Given a section, search the header to find them. */
4049 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4053 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4054 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4056 Elf_Internal_Shdr
*hdr
;
4057 int maxindex
= elf_numsections (abfd
);
4059 if (elf_section_data (asect
) != NULL
4060 && elf_section_data (asect
)->this_idx
!= 0)
4061 return elf_section_data (asect
)->this_idx
;
4063 if (bfd_is_abs_section (asect
))
4065 if (bfd_is_com_section (asect
))
4067 if (bfd_is_und_section (asect
))
4070 for (index
= 1; index
< maxindex
; index
++)
4072 hdr
= i_shdrp
[index
];
4073 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4077 if (bed
->elf_backend_section_from_bfd_section
)
4079 for (index
= 0; index
< maxindex
; index
++)
4083 hdr
= i_shdrp
[index
];
4088 if ((*bed
->elf_backend_section_from_bfd_section
)
4089 (abfd
, hdr
, asect
, &retval
))
4094 bfd_set_error (bfd_error_nonrepresentable_section
);
4099 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4103 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4105 asymbol
**asym_ptr_ptr
;
4107 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4109 flagword flags
= asym_ptr
->flags
;
4111 /* When gas creates relocations against local labels, it creates its
4112 own symbol for the section, but does put the symbol into the
4113 symbol chain, so udata is 0. When the linker is generating
4114 relocatable output, this section symbol may be for one of the
4115 input sections rather than the output section. */
4116 if (asym_ptr
->udata
.i
== 0
4117 && (flags
& BSF_SECTION_SYM
)
4118 && asym_ptr
->section
)
4122 if (asym_ptr
->section
->output_section
!= NULL
)
4123 indx
= asym_ptr
->section
->output_section
->index
;
4125 indx
= asym_ptr
->section
->index
;
4126 if (indx
< elf_num_section_syms (abfd
)
4127 && elf_section_syms (abfd
)[indx
] != NULL
)
4128 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4131 idx
= asym_ptr
->udata
.i
;
4135 /* This case can occur when using --strip-symbol on a symbol
4136 which is used in a relocation entry. */
4137 (*_bfd_error_handler
)
4138 (_("%s: symbol `%s' required but not present"),
4139 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4140 bfd_set_error (bfd_error_no_symbols
);
4147 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
4148 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4149 elf_symbol_flags (flags
));
4157 /* Copy private BFD data. This copies any program header information. */
4160 copy_private_bfd_data (ibfd
, obfd
)
4164 Elf_Internal_Ehdr
* iehdr
;
4165 struct elf_segment_map
* map
;
4166 struct elf_segment_map
* map_first
;
4167 struct elf_segment_map
** pointer_to_map
;
4168 Elf_Internal_Phdr
* segment
;
4171 unsigned int num_segments
;
4172 boolean phdr_included
= false;
4173 bfd_vma maxpagesize
;
4174 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4175 unsigned int phdr_adjust_num
= 0;
4177 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4178 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4181 if (elf_tdata (ibfd
)->phdr
== NULL
)
4184 iehdr
= elf_elfheader (ibfd
);
4187 pointer_to_map
= &map_first
;
4189 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4190 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4192 /* Returns the end address of the segment + 1. */
4193 #define SEGMENT_END(segment, start) \
4194 (start + (segment->p_memsz > segment->p_filesz \
4195 ? segment->p_memsz : segment->p_filesz))
4197 /* Returns true if the given section is contained within
4198 the given segment. VMA addresses are compared. */
4199 #define IS_CONTAINED_BY_VMA(section, segment) \
4200 (section->vma >= segment->p_vaddr \
4201 && (section->vma + section->_raw_size) \
4202 <= (SEGMENT_END (segment, segment->p_vaddr)))
4204 /* Returns true if the given section is contained within
4205 the given segment. LMA addresses are compared. */
4206 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4207 (section->lma >= base \
4208 && (section->lma + section->_raw_size) \
4209 <= SEGMENT_END (segment, base))
4211 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4212 #define IS_COREFILE_NOTE(p, s) \
4213 (p->p_type == PT_NOTE \
4214 && bfd_get_format (ibfd) == bfd_core \
4215 && s->vma == 0 && s->lma == 0 \
4216 && (bfd_vma) s->filepos >= p->p_offset \
4217 && (bfd_vma) s->filepos + s->_raw_size \
4218 <= p->p_offset + p->p_filesz)
4220 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4221 linker, which generates a PT_INTERP section with p_vaddr and
4222 p_memsz set to 0. */
4223 #define IS_SOLARIS_PT_INTERP(p, s) \
4225 && p->p_filesz > 0 \
4226 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4227 && s->_raw_size > 0 \
4228 && (bfd_vma) s->filepos >= p->p_offset \
4229 && ((bfd_vma) s->filepos + s->_raw_size \
4230 <= p->p_offset + p->p_filesz))
4232 /* Decide if the given section should be included in the given segment.
4233 A section will be included if:
4234 1. It is within the address space of the segment -- we use the LMA
4235 if that is set for the segment and the VMA otherwise,
4236 2. It is an allocated segment,
4237 3. There is an output section associated with it,
4238 4. The section has not already been allocated to a previous segment. */
4239 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4240 (((((segment->p_paddr \
4241 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4242 : IS_CONTAINED_BY_VMA (section, segment)) \
4243 || IS_SOLARIS_PT_INTERP (segment, section)) \
4244 && (section->flags & SEC_ALLOC) != 0) \
4245 || IS_COREFILE_NOTE (segment, section)) \
4246 && section->output_section != NULL \
4247 && section->segment_mark == false)
4249 /* Returns true iff seg1 starts after the end of seg2. */
4250 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4251 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4253 /* Returns true iff seg1 and seg2 overlap. */
4254 #define SEGMENT_OVERLAPS(seg1, seg2) \
4255 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4257 /* Initialise the segment mark field. */
4258 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4259 section
->segment_mark
= false;
4261 /* Scan through the segments specified in the program header
4262 of the input BFD. For this first scan we look for overlaps
4263 in the loadable segments. These can be created by weird
4264 parameters to objcopy. */
4265 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4270 Elf_Internal_Phdr
*segment2
;
4272 if (segment
->p_type
!= PT_LOAD
)
4275 /* Determine if this segment overlaps any previous segments. */
4276 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4278 bfd_signed_vma extra_length
;
4280 if (segment2
->p_type
!= PT_LOAD
4281 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4284 /* Merge the two segments together. */
4285 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4287 /* Extend SEGMENT2 to include SEGMENT and then delete
4290 SEGMENT_END (segment
, segment
->p_vaddr
)
4291 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4293 if (extra_length
> 0)
4295 segment2
->p_memsz
+= extra_length
;
4296 segment2
->p_filesz
+= extra_length
;
4299 segment
->p_type
= PT_NULL
;
4301 /* Since we have deleted P we must restart the outer loop. */
4303 segment
= elf_tdata (ibfd
)->phdr
;
4308 /* Extend SEGMENT to include SEGMENT2 and then delete
4311 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4312 - SEGMENT_END (segment
, segment
->p_vaddr
);
4314 if (extra_length
> 0)
4316 segment
->p_memsz
+= extra_length
;
4317 segment
->p_filesz
+= extra_length
;
4320 segment2
->p_type
= PT_NULL
;
4325 /* The second scan attempts to assign sections to segments. */
4326 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4330 unsigned int section_count
;
4331 asection
** sections
;
4332 asection
* output_section
;
4334 bfd_vma matching_lma
;
4335 bfd_vma suggested_lma
;
4339 if (segment
->p_type
== PT_NULL
)
4342 /* Compute how many sections might be placed into this segment. */
4344 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4345 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4348 /* Allocate a segment map big enough to contain all of the
4349 sections we have selected. */
4350 amt
= sizeof (struct elf_segment_map
);
4351 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4352 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4356 /* Initialise the fields of the segment map. Default to
4357 using the physical address of the segment in the input BFD. */
4359 map
->p_type
= segment
->p_type
;
4360 map
->p_flags
= segment
->p_flags
;
4361 map
->p_flags_valid
= 1;
4362 map
->p_paddr
= segment
->p_paddr
;
4363 map
->p_paddr_valid
= 1;
4365 /* Determine if this segment contains the ELF file header
4366 and if it contains the program headers themselves. */
4367 map
->includes_filehdr
= (segment
->p_offset
== 0
4368 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4370 map
->includes_phdrs
= 0;
4372 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4374 map
->includes_phdrs
=
4375 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4376 && (segment
->p_offset
+ segment
->p_filesz
4377 >= ((bfd_vma
) iehdr
->e_phoff
4378 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4380 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4381 phdr_included
= true;
4384 if (section_count
== 0)
4386 /* Special segments, such as the PT_PHDR segment, may contain
4387 no sections, but ordinary, loadable segments should contain
4389 if (segment
->p_type
== PT_LOAD
)
4391 (_("%s: warning: Empty loadable segment detected\n"),
4392 bfd_archive_filename (ibfd
));
4395 *pointer_to_map
= map
;
4396 pointer_to_map
= &map
->next
;
4401 /* Now scan the sections in the input BFD again and attempt
4402 to add their corresponding output sections to the segment map.
4403 The problem here is how to handle an output section which has
4404 been moved (ie had its LMA changed). There are four possibilities:
4406 1. None of the sections have been moved.
4407 In this case we can continue to use the segment LMA from the
4410 2. All of the sections have been moved by the same amount.
4411 In this case we can change the segment's LMA to match the LMA
4412 of the first section.
4414 3. Some of the sections have been moved, others have not.
4415 In this case those sections which have not been moved can be
4416 placed in the current segment which will have to have its size,
4417 and possibly its LMA changed, and a new segment or segments will
4418 have to be created to contain the other sections.
4420 4. The sections have been moved, but not be the same amount.
4421 In this case we can change the segment's LMA to match the LMA
4422 of the first section and we will have to create a new segment
4423 or segments to contain the other sections.
4425 In order to save time, we allocate an array to hold the section
4426 pointers that we are interested in. As these sections get assigned
4427 to a segment, they are removed from this array. */
4429 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4430 sections
= (asection
**) bfd_malloc (amt
);
4431 if (sections
== NULL
)
4434 /* Step One: Scan for segment vs section LMA conflicts.
4435 Also add the sections to the section array allocated above.
4436 Also add the sections to the current segment. In the common
4437 case, where the sections have not been moved, this means that
4438 we have completely filled the segment, and there is nothing
4444 for (j
= 0, section
= ibfd
->sections
;
4446 section
= section
->next
)
4448 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4450 output_section
= section
->output_section
;
4452 sections
[j
++] = section
;
4454 /* The Solaris native linker always sets p_paddr to 0.
4455 We try to catch that case here, and set it to the
4457 if (segment
->p_paddr
== 0
4458 && segment
->p_vaddr
!= 0
4460 && output_section
->lma
!= 0
4461 && (output_section
->vma
== (segment
->p_vaddr
4462 + (map
->includes_filehdr
4465 + (map
->includes_phdrs
4467 * iehdr
->e_phentsize
)
4469 map
->p_paddr
= segment
->p_vaddr
;
4471 /* Match up the physical address of the segment with the
4472 LMA address of the output section. */
4473 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4474 || IS_COREFILE_NOTE (segment
, section
))
4476 if (matching_lma
== 0)
4477 matching_lma
= output_section
->lma
;
4479 /* We assume that if the section fits within the segment
4480 then it does not overlap any other section within that
4482 map
->sections
[isec
++] = output_section
;
4484 else if (suggested_lma
== 0)
4485 suggested_lma
= output_section
->lma
;
4489 BFD_ASSERT (j
== section_count
);
4491 /* Step Two: Adjust the physical address of the current segment,
4493 if (isec
== section_count
)
4495 /* All of the sections fitted within the segment as currently
4496 specified. This is the default case. Add the segment to
4497 the list of built segments and carry on to process the next
4498 program header in the input BFD. */
4499 map
->count
= section_count
;
4500 *pointer_to_map
= map
;
4501 pointer_to_map
= &map
->next
;
4508 if (matching_lma
!= 0)
4510 /* At least one section fits inside the current segment.
4511 Keep it, but modify its physical address to match the
4512 LMA of the first section that fitted. */
4513 map
->p_paddr
= matching_lma
;
4517 /* None of the sections fitted inside the current segment.
4518 Change the current segment's physical address to match
4519 the LMA of the first section. */
4520 map
->p_paddr
= suggested_lma
;
4523 /* Offset the segment physical address from the lma
4524 to allow for space taken up by elf headers. */
4525 if (map
->includes_filehdr
)
4526 map
->p_paddr
-= iehdr
->e_ehsize
;
4528 if (map
->includes_phdrs
)
4530 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4532 /* iehdr->e_phnum is just an estimate of the number
4533 of program headers that we will need. Make a note
4534 here of the number we used and the segment we chose
4535 to hold these headers, so that we can adjust the
4536 offset when we know the correct value. */
4537 phdr_adjust_num
= iehdr
->e_phnum
;
4538 phdr_adjust_seg
= map
;
4542 /* Step Three: Loop over the sections again, this time assigning
4543 those that fit to the current segment and remvoing them from the
4544 sections array; but making sure not to leave large gaps. Once all
4545 possible sections have been assigned to the current segment it is
4546 added to the list of built segments and if sections still remain
4547 to be assigned, a new segment is constructed before repeating
4555 /* Fill the current segment with sections that fit. */
4556 for (j
= 0; j
< section_count
; j
++)
4558 section
= sections
[j
];
4560 if (section
== NULL
)
4563 output_section
= section
->output_section
;
4565 BFD_ASSERT (output_section
!= NULL
);
4567 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4568 || IS_COREFILE_NOTE (segment
, section
))
4570 if (map
->count
== 0)
4572 /* If the first section in a segment does not start at
4573 the beginning of the segment, then something is
4575 if (output_section
->lma
!=
4577 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4578 + (map
->includes_phdrs
4579 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4585 asection
* prev_sec
;
4587 prev_sec
= map
->sections
[map
->count
- 1];
4589 /* If the gap between the end of the previous section
4590 and the start of this section is more than
4591 maxpagesize then we need to start a new segment. */
4592 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4594 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4595 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4596 > output_section
->lma
))
4598 if (suggested_lma
== 0)
4599 suggested_lma
= output_section
->lma
;
4605 map
->sections
[map
->count
++] = output_section
;
4608 section
->segment_mark
= true;
4610 else if (suggested_lma
== 0)
4611 suggested_lma
= output_section
->lma
;
4614 BFD_ASSERT (map
->count
> 0);
4616 /* Add the current segment to the list of built segments. */
4617 *pointer_to_map
= map
;
4618 pointer_to_map
= &map
->next
;
4620 if (isec
< section_count
)
4622 /* We still have not allocated all of the sections to
4623 segments. Create a new segment here, initialise it
4624 and carry on looping. */
4625 amt
= sizeof (struct elf_segment_map
);
4626 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4627 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4631 /* Initialise the fields of the segment map. Set the physical
4632 physical address to the LMA of the first section that has
4633 not yet been assigned. */
4635 map
->p_type
= segment
->p_type
;
4636 map
->p_flags
= segment
->p_flags
;
4637 map
->p_flags_valid
= 1;
4638 map
->p_paddr
= suggested_lma
;
4639 map
->p_paddr_valid
= 1;
4640 map
->includes_filehdr
= 0;
4641 map
->includes_phdrs
= 0;
4644 while (isec
< section_count
);
4649 /* The Solaris linker creates program headers in which all the
4650 p_paddr fields are zero. When we try to objcopy or strip such a
4651 file, we get confused. Check for this case, and if we find it
4652 reset the p_paddr_valid fields. */
4653 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4654 if (map
->p_paddr
!= 0)
4658 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4659 map
->p_paddr_valid
= 0;
4662 elf_tdata (obfd
)->segment_map
= map_first
;
4664 /* If we had to estimate the number of program headers that were
4665 going to be needed, then check our estimate now and adjust
4666 the offset if necessary. */
4667 if (phdr_adjust_seg
!= NULL
)
4671 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4674 if (count
> phdr_adjust_num
)
4675 phdr_adjust_seg
->p_paddr
4676 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4680 /* Final Step: Sort the segments into ascending order of physical
4682 if (map_first
!= NULL
)
4684 struct elf_segment_map
*prev
;
4687 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4689 /* Yes I know - its a bubble sort.... */
4690 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4692 /* Swap map and map->next. */
4693 prev
->next
= map
->next
;
4694 map
->next
= map
->next
->next
;
4695 prev
->next
->next
= map
;
4705 #undef IS_CONTAINED_BY_VMA
4706 #undef IS_CONTAINED_BY_LMA
4707 #undef IS_COREFILE_NOTE
4708 #undef IS_SOLARIS_PT_INTERP
4709 #undef INCLUDE_SECTION_IN_SEGMENT
4710 #undef SEGMENT_AFTER_SEGMENT
4711 #undef SEGMENT_OVERLAPS
4715 /* Copy private section information. This copies over the entsize
4716 field, and sometimes the info field. */
4719 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4725 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4727 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4728 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4731 /* Copy over private BFD data if it has not already been copied.
4732 This must be done here, rather than in the copy_private_bfd_data
4733 entry point, because the latter is called after the section
4734 contents have been set, which means that the program headers have
4735 already been worked out. */
4736 if (elf_tdata (obfd
)->segment_map
== NULL
4737 && elf_tdata (ibfd
)->phdr
!= NULL
)
4741 /* Only set up the segments if there are no more SEC_ALLOC
4742 sections. FIXME: This won't do the right thing if objcopy is
4743 used to remove the last SEC_ALLOC section, since objcopy
4744 won't call this routine in that case. */
4745 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4746 if ((s
->flags
& SEC_ALLOC
) != 0)
4750 if (! copy_private_bfd_data (ibfd
, obfd
))
4755 ihdr
= &elf_section_data (isec
)->this_hdr
;
4756 ohdr
= &elf_section_data (osec
)->this_hdr
;
4758 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4760 if (ihdr
->sh_type
== SHT_SYMTAB
4761 || ihdr
->sh_type
== SHT_DYNSYM
4762 || ihdr
->sh_type
== SHT_GNU_verneed
4763 || ihdr
->sh_type
== SHT_GNU_verdef
)
4764 ohdr
->sh_info
= ihdr
->sh_info
;
4766 elf_section_data (osec
)->use_rela_p
4767 = elf_section_data (isec
)->use_rela_p
;
4772 /* Copy private symbol information. If this symbol is in a section
4773 which we did not map into a BFD section, try to map the section
4774 index correctly. We use special macro definitions for the mapped
4775 section indices; these definitions are interpreted by the
4776 swap_out_syms function. */
4778 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4779 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4780 #define MAP_STRTAB (SHN_HIOS + 3)
4781 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4782 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4785 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4791 elf_symbol_type
*isym
, *osym
;
4793 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4794 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4797 isym
= elf_symbol_from (ibfd
, isymarg
);
4798 osym
= elf_symbol_from (obfd
, osymarg
);
4802 && bfd_is_abs_section (isym
->symbol
.section
))
4806 shndx
= isym
->internal_elf_sym
.st_shndx
;
4807 if (shndx
== elf_onesymtab (ibfd
))
4808 shndx
= MAP_ONESYMTAB
;
4809 else if (shndx
== elf_dynsymtab (ibfd
))
4810 shndx
= MAP_DYNSYMTAB
;
4811 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4813 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4814 shndx
= MAP_SHSTRTAB
;
4815 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
4816 shndx
= MAP_SYM_SHNDX
;
4817 osym
->internal_elf_sym
.st_shndx
= shndx
;
4823 /* Swap out the symbols. */
4826 swap_out_syms (abfd
, sttp
, relocatable_p
)
4828 struct bfd_strtab_hash
**sttp
;
4831 struct elf_backend_data
*bed
;
4834 struct bfd_strtab_hash
*stt
;
4835 Elf_Internal_Shdr
*symtab_hdr
;
4836 Elf_Internal_Shdr
*symtab_shndx_hdr
;
4837 Elf_Internal_Shdr
*symstrtab_hdr
;
4838 char *outbound_syms
;
4839 char *outbound_shndx
;
4843 if (!elf_map_symbols (abfd
))
4846 /* Dump out the symtabs. */
4847 stt
= _bfd_elf_stringtab_init ();
4851 bed
= get_elf_backend_data (abfd
);
4852 symcount
= bfd_get_symcount (abfd
);
4853 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4854 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4855 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4856 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4857 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4858 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4860 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4861 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4863 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
4864 outbound_syms
= bfd_alloc (abfd
, amt
);
4865 if (outbound_syms
== NULL
)
4867 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4869 outbound_shndx
= NULL
;
4870 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
4871 if (symtab_shndx_hdr
->sh_name
!= 0)
4873 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
4874 outbound_shndx
= bfd_alloc (abfd
, amt
);
4875 if (outbound_shndx
== NULL
)
4877 memset (outbound_shndx
, 0, (unsigned long) amt
);
4878 symtab_shndx_hdr
->contents
= outbound_shndx
;
4879 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
4880 symtab_shndx_hdr
->sh_size
= amt
;
4881 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
4882 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
4885 /* now generate the data (for "contents") */
4887 /* Fill in zeroth symbol and swap it out. */
4888 Elf_Internal_Sym sym
;
4894 sym
.st_shndx
= SHN_UNDEF
;
4895 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
4896 outbound_syms
+= bed
->s
->sizeof_sym
;
4897 if (outbound_shndx
!= NULL
)
4898 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
4901 syms
= bfd_get_outsymbols (abfd
);
4902 for (idx
= 0; idx
< symcount
; idx
++)
4904 Elf_Internal_Sym sym
;
4905 bfd_vma value
= syms
[idx
]->value
;
4906 elf_symbol_type
*type_ptr
;
4907 flagword flags
= syms
[idx
]->flags
;
4910 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4912 /* Local section symbols have no name. */
4917 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4920 if (sym
.st_name
== (unsigned long) -1)
4924 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4926 if ((flags
& BSF_SECTION_SYM
) == 0
4927 && bfd_is_com_section (syms
[idx
]->section
))
4929 /* ELF common symbols put the alignment into the `value' field,
4930 and the size into the `size' field. This is backwards from
4931 how BFD handles it, so reverse it here. */
4932 sym
.st_size
= value
;
4933 if (type_ptr
== NULL
4934 || type_ptr
->internal_elf_sym
.st_value
== 0)
4935 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4937 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4938 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4939 (abfd
, syms
[idx
]->section
);
4943 asection
*sec
= syms
[idx
]->section
;
4946 if (sec
->output_section
)
4948 value
+= sec
->output_offset
;
4949 sec
= sec
->output_section
;
4951 /* Don't add in the section vma for relocatable output. */
4952 if (! relocatable_p
)
4954 sym
.st_value
= value
;
4955 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4957 if (bfd_is_abs_section (sec
)
4959 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4961 /* This symbol is in a real ELF section which we did
4962 not create as a BFD section. Undo the mapping done
4963 by copy_private_symbol_data. */
4964 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4968 shndx
= elf_onesymtab (abfd
);
4971 shndx
= elf_dynsymtab (abfd
);
4974 shndx
= elf_tdata (abfd
)->strtab_section
;
4977 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4980 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
4988 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4994 /* Writing this would be a hell of a lot easier if
4995 we had some decent documentation on bfd, and
4996 knew what to expect of the library, and what to
4997 demand of applications. For example, it
4998 appears that `objcopy' might not set the
4999 section of a symbol to be a section that is
5000 actually in the output file. */
5001 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5002 BFD_ASSERT (sec2
!= 0);
5003 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5004 BFD_ASSERT (shndx
!= -1);
5008 sym
.st_shndx
= shndx
;
5011 if ((flags
& BSF_FUNCTION
) != 0)
5013 else if ((flags
& BSF_OBJECT
) != 0)
5018 /* Processor-specific types */
5019 if (type_ptr
!= NULL
5020 && bed
->elf_backend_get_symbol_type
)
5021 type
= ((*bed
->elf_backend_get_symbol_type
)
5022 (&type_ptr
->internal_elf_sym
, type
));
5024 if (flags
& BSF_SECTION_SYM
)
5026 if (flags
& BSF_GLOBAL
)
5027 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5029 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5031 else if (bfd_is_com_section (syms
[idx
]->section
))
5032 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5033 else if (bfd_is_und_section (syms
[idx
]->section
))
5034 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5038 else if (flags
& BSF_FILE
)
5039 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5042 int bind
= STB_LOCAL
;
5044 if (flags
& BSF_LOCAL
)
5046 else if (flags
& BSF_WEAK
)
5048 else if (flags
& BSF_GLOBAL
)
5051 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5054 if (type_ptr
!= NULL
)
5055 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5059 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5060 outbound_syms
+= bed
->s
->sizeof_sym
;
5061 if (outbound_shndx
!= NULL
)
5062 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5066 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5067 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5069 symstrtab_hdr
->sh_flags
= 0;
5070 symstrtab_hdr
->sh_addr
= 0;
5071 symstrtab_hdr
->sh_entsize
= 0;
5072 symstrtab_hdr
->sh_link
= 0;
5073 symstrtab_hdr
->sh_info
= 0;
5074 symstrtab_hdr
->sh_addralign
= 1;
5079 /* Return the number of bytes required to hold the symtab vector.
5081 Note that we base it on the count plus 1, since we will null terminate
5082 the vector allocated based on this size. However, the ELF symbol table
5083 always has a dummy entry as symbol #0, so it ends up even. */
5086 _bfd_elf_get_symtab_upper_bound (abfd
)
5091 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5093 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5094 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
5100 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5105 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5107 if (elf_dynsymtab (abfd
) == 0)
5109 bfd_set_error (bfd_error_invalid_operation
);
5113 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5114 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
5120 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5121 bfd
*abfd ATTRIBUTE_UNUSED
;
5124 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5127 /* Canonicalize the relocs. */
5130 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5138 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5140 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5143 tblptr
= section
->relocation
;
5144 for (i
= 0; i
< section
->reloc_count
; i
++)
5145 *relptr
++ = tblptr
++;
5149 return section
->reloc_count
;
5153 _bfd_elf_get_symtab (abfd
, alocation
)
5155 asymbol
**alocation
;
5157 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5158 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5161 bfd_get_symcount (abfd
) = symcount
;
5166 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5168 asymbol
**alocation
;
5170 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5171 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5174 /* Return the size required for the dynamic reloc entries. Any
5175 section that was actually installed in the BFD, and has type
5176 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5177 considered to be a dynamic reloc section. */
5180 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5186 if (elf_dynsymtab (abfd
) == 0)
5188 bfd_set_error (bfd_error_invalid_operation
);
5192 ret
= sizeof (arelent
*);
5193 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5194 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5195 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5196 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5197 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5198 * sizeof (arelent
*));
5203 /* Canonicalize the dynamic relocation entries. Note that we return
5204 the dynamic relocations as a single block, although they are
5205 actually associated with particular sections; the interface, which
5206 was designed for SunOS style shared libraries, expects that there
5207 is only one set of dynamic relocs. Any section that was actually
5208 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5209 the dynamic symbol table, is considered to be a dynamic reloc
5213 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5218 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5222 if (elf_dynsymtab (abfd
) == 0)
5224 bfd_set_error (bfd_error_invalid_operation
);
5228 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5230 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5232 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5233 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5234 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5239 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5241 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5243 for (i
= 0; i
< count
; i
++)
5254 /* Read in the version information. */
5257 _bfd_elf_slurp_version_tables (abfd
)
5260 bfd_byte
*contents
= NULL
;
5263 if (elf_dynverdef (abfd
) != 0)
5265 Elf_Internal_Shdr
*hdr
;
5266 Elf_External_Verdef
*everdef
;
5267 Elf_Internal_Verdef
*iverdef
;
5268 Elf_Internal_Verdef
*iverdefarr
;
5269 Elf_Internal_Verdef iverdefmem
;
5271 unsigned int maxidx
;
5273 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5275 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5276 if (contents
== NULL
)
5278 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5279 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5282 /* We know the number of entries in the section but not the maximum
5283 index. Therefore we have to run through all entries and find
5285 everdef
= (Elf_External_Verdef
*) contents
;
5287 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5289 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5291 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5292 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5294 everdef
= ((Elf_External_Verdef
*)
5295 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5298 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5299 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5300 if (elf_tdata (abfd
)->verdef
== NULL
)
5303 elf_tdata (abfd
)->cverdefs
= maxidx
;
5305 everdef
= (Elf_External_Verdef
*) contents
;
5306 iverdefarr
= elf_tdata (abfd
)->verdef
;
5307 for (i
= 0; i
< hdr
->sh_info
; i
++)
5309 Elf_External_Verdaux
*everdaux
;
5310 Elf_Internal_Verdaux
*iverdaux
;
5313 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5315 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5316 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5318 iverdef
->vd_bfd
= abfd
;
5320 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5321 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5322 if (iverdef
->vd_auxptr
== NULL
)
5325 everdaux
= ((Elf_External_Verdaux
*)
5326 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5327 iverdaux
= iverdef
->vd_auxptr
;
5328 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5330 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5332 iverdaux
->vda_nodename
=
5333 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5334 iverdaux
->vda_name
);
5335 if (iverdaux
->vda_nodename
== NULL
)
5338 if (j
+ 1 < iverdef
->vd_cnt
)
5339 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5341 iverdaux
->vda_nextptr
= NULL
;
5343 everdaux
= ((Elf_External_Verdaux
*)
5344 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5347 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5349 if (i
+ 1 < hdr
->sh_info
)
5350 iverdef
->vd_nextdef
= iverdef
+ 1;
5352 iverdef
->vd_nextdef
= NULL
;
5354 everdef
= ((Elf_External_Verdef
*)
5355 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5362 if (elf_dynverref (abfd
) != 0)
5364 Elf_Internal_Shdr
*hdr
;
5365 Elf_External_Verneed
*everneed
;
5366 Elf_Internal_Verneed
*iverneed
;
5369 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5371 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5372 elf_tdata (abfd
)->verref
=
5373 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5374 if (elf_tdata (abfd
)->verref
== NULL
)
5377 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5379 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5380 if (contents
== NULL
)
5382 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5383 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5386 everneed
= (Elf_External_Verneed
*) contents
;
5387 iverneed
= elf_tdata (abfd
)->verref
;
5388 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5390 Elf_External_Vernaux
*evernaux
;
5391 Elf_Internal_Vernaux
*ivernaux
;
5394 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5396 iverneed
->vn_bfd
= abfd
;
5398 iverneed
->vn_filename
=
5399 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5401 if (iverneed
->vn_filename
== NULL
)
5404 amt
= iverneed
->vn_cnt
;
5405 amt
*= sizeof (Elf_Internal_Vernaux
);
5406 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5408 evernaux
= ((Elf_External_Vernaux
*)
5409 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5410 ivernaux
= iverneed
->vn_auxptr
;
5411 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5413 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5415 ivernaux
->vna_nodename
=
5416 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5417 ivernaux
->vna_name
);
5418 if (ivernaux
->vna_nodename
== NULL
)
5421 if (j
+ 1 < iverneed
->vn_cnt
)
5422 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5424 ivernaux
->vna_nextptr
= NULL
;
5426 evernaux
= ((Elf_External_Vernaux
*)
5427 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5430 if (i
+ 1 < hdr
->sh_info
)
5431 iverneed
->vn_nextref
= iverneed
+ 1;
5433 iverneed
->vn_nextref
= NULL
;
5435 everneed
= ((Elf_External_Verneed
*)
5436 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5446 if (contents
== NULL
)
5452 _bfd_elf_make_empty_symbol (abfd
)
5455 elf_symbol_type
*newsym
;
5456 bfd_size_type amt
= sizeof (elf_symbol_type
);
5458 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5463 newsym
->symbol
.the_bfd
= abfd
;
5464 return &newsym
->symbol
;
5469 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5470 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5474 bfd_symbol_info (symbol
, ret
);
5477 /* Return whether a symbol name implies a local symbol. Most targets
5478 use this function for the is_local_label_name entry point, but some
5482 _bfd_elf_is_local_label_name (abfd
, name
)
5483 bfd
*abfd ATTRIBUTE_UNUSED
;
5486 /* Normal local symbols start with ``.L''. */
5487 if (name
[0] == '.' && name
[1] == 'L')
5490 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5491 DWARF debugging symbols starting with ``..''. */
5492 if (name
[0] == '.' && name
[1] == '.')
5495 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5496 emitting DWARF debugging output. I suspect this is actually a
5497 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5498 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5499 underscore to be emitted on some ELF targets). For ease of use,
5500 we treat such symbols as local. */
5501 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5508 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5509 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5510 asymbol
*symbol ATTRIBUTE_UNUSED
;
5517 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5519 enum bfd_architecture arch
;
5520 unsigned long machine
;
5522 /* If this isn't the right architecture for this backend, and this
5523 isn't the generic backend, fail. */
5524 if (arch
!= get_elf_backend_data (abfd
)->arch
5525 && arch
!= bfd_arch_unknown
5526 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5529 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5532 /* Find the function to a particular section and offset,
5533 for error reporting. */
5536 elf_find_function (abfd
, section
, symbols
, offset
,
5537 filename_ptr
, functionname_ptr
)
5538 bfd
*abfd ATTRIBUTE_UNUSED
;
5542 const char **filename_ptr
;
5543 const char **functionname_ptr
;
5545 const char *filename
;
5554 for (p
= symbols
; *p
!= NULL
; p
++)
5558 q
= (elf_symbol_type
*) *p
;
5560 if (bfd_get_section (&q
->symbol
) != section
)
5563 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5568 filename
= bfd_asymbol_name (&q
->symbol
);
5572 if (q
->symbol
.section
== section
5573 && q
->symbol
.value
>= low_func
5574 && q
->symbol
.value
<= offset
)
5576 func
= (asymbol
*) q
;
5577 low_func
= q
->symbol
.value
;
5587 *filename_ptr
= filename
;
5588 if (functionname_ptr
)
5589 *functionname_ptr
= bfd_asymbol_name (func
);
5594 /* Find the nearest line to a particular section and offset,
5595 for error reporting. */
5598 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5599 filename_ptr
, functionname_ptr
, line_ptr
)
5604 const char **filename_ptr
;
5605 const char **functionname_ptr
;
5606 unsigned int *line_ptr
;
5610 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5611 filename_ptr
, functionname_ptr
,
5614 if (!*functionname_ptr
)
5615 elf_find_function (abfd
, section
, symbols
, offset
,
5616 *filename_ptr
? NULL
: filename_ptr
,
5622 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5623 filename_ptr
, functionname_ptr
,
5625 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5627 if (!*functionname_ptr
)
5628 elf_find_function (abfd
, section
, symbols
, offset
,
5629 *filename_ptr
? NULL
: filename_ptr
,
5635 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5636 &found
, filename_ptr
,
5637 functionname_ptr
, line_ptr
,
5638 &elf_tdata (abfd
)->line_info
))
5643 if (symbols
== NULL
)
5646 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5647 filename_ptr
, functionname_ptr
))
5655 _bfd_elf_sizeof_headers (abfd
, reloc
)
5661 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5663 ret
+= get_program_header_size (abfd
);
5668 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5673 bfd_size_type count
;
5675 Elf_Internal_Shdr
*hdr
;
5678 if (! abfd
->output_has_begun
5679 && ! _bfd_elf_compute_section_file_positions
5680 (abfd
, (struct bfd_link_info
*) NULL
))
5683 hdr
= &elf_section_data (section
)->this_hdr
;
5684 pos
= hdr
->sh_offset
+ offset
;
5685 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5686 || bfd_bwrite (location
, count
, abfd
) != count
)
5693 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5694 bfd
*abfd ATTRIBUTE_UNUSED
;
5695 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5696 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5703 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5706 Elf_Internal_Rel
*dst
;
5712 /* Try to convert a non-ELF reloc into an ELF one. */
5715 _bfd_elf_validate_reloc (abfd
, areloc
)
5719 /* Check whether we really have an ELF howto. */
5721 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5723 bfd_reloc_code_real_type code
;
5724 reloc_howto_type
*howto
;
5726 /* Alien reloc: Try to determine its type to replace it with an
5727 equivalent ELF reloc. */
5729 if (areloc
->howto
->pc_relative
)
5731 switch (areloc
->howto
->bitsize
)
5734 code
= BFD_RELOC_8_PCREL
;
5737 code
= BFD_RELOC_12_PCREL
;
5740 code
= BFD_RELOC_16_PCREL
;
5743 code
= BFD_RELOC_24_PCREL
;
5746 code
= BFD_RELOC_32_PCREL
;
5749 code
= BFD_RELOC_64_PCREL
;
5755 howto
= bfd_reloc_type_lookup (abfd
, code
);
5757 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5759 if (howto
->pcrel_offset
)
5760 areloc
->addend
+= areloc
->address
;
5762 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5767 switch (areloc
->howto
->bitsize
)
5773 code
= BFD_RELOC_14
;
5776 code
= BFD_RELOC_16
;
5779 code
= BFD_RELOC_26
;
5782 code
= BFD_RELOC_32
;
5785 code
= BFD_RELOC_64
;
5791 howto
= bfd_reloc_type_lookup (abfd
, code
);
5795 areloc
->howto
= howto
;
5803 (*_bfd_error_handler
)
5804 (_("%s: unsupported relocation type %s"),
5805 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5806 bfd_set_error (bfd_error_bad_value
);
5811 _bfd_elf_close_and_cleanup (abfd
)
5814 if (bfd_get_format (abfd
) == bfd_object
)
5816 if (elf_shstrtab (abfd
) != NULL
)
5817 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
5820 return _bfd_generic_close_and_cleanup (abfd
);
5823 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5824 in the relocation's offset. Thus we cannot allow any sort of sanity
5825 range-checking to interfere. There is nothing else to do in processing
5828 bfd_reloc_status_type
5829 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5830 bfd
*abfd ATTRIBUTE_UNUSED
;
5831 arelent
*re ATTRIBUTE_UNUSED
;
5832 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5833 PTR data ATTRIBUTE_UNUSED
;
5834 asection
*is ATTRIBUTE_UNUSED
;
5835 bfd
*obfd ATTRIBUTE_UNUSED
;
5836 char **errmsg ATTRIBUTE_UNUSED
;
5838 return bfd_reloc_ok
;
5841 /* Elf core file support. Much of this only works on native
5842 toolchains, since we rely on knowing the
5843 machine-dependent procfs structure in order to pick
5844 out details about the corefile. */
5846 #ifdef HAVE_SYS_PROCFS_H
5847 # include <sys/procfs.h>
5850 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5853 elfcore_make_pid (abfd
)
5856 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5857 + (elf_tdata (abfd
)->core_pid
));
5860 /* If there isn't a section called NAME, make one, using
5861 data from SECT. Note, this function will generate a
5862 reference to NAME, so you shouldn't deallocate or
5866 elfcore_maybe_make_sect (abfd
, name
, sect
)
5873 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5876 sect2
= bfd_make_section (abfd
, name
);
5880 sect2
->_raw_size
= sect
->_raw_size
;
5881 sect2
->filepos
= sect
->filepos
;
5882 sect2
->flags
= sect
->flags
;
5883 sect2
->alignment_power
= sect
->alignment_power
;
5887 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5888 actually creates up to two pseudosections:
5889 - For the single-threaded case, a section named NAME, unless
5890 such a section already exists.
5891 - For the multi-threaded case, a section named "NAME/PID", where
5892 PID is elfcore_make_pid (abfd).
5893 Both pseudosections have identical contents. */
5895 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5902 char *threaded_name
;
5905 /* Build the section name. */
5907 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5908 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5909 if (threaded_name
== NULL
)
5911 strcpy (threaded_name
, buf
);
5913 sect
= bfd_make_section (abfd
, threaded_name
);
5916 sect
->_raw_size
= size
;
5917 sect
->filepos
= filepos
;
5918 sect
->flags
= SEC_HAS_CONTENTS
;
5919 sect
->alignment_power
= 2;
5921 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5924 /* prstatus_t exists on:
5926 linux 2.[01] + glibc
5930 #if defined (HAVE_PRSTATUS_T)
5931 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
5934 elfcore_grok_prstatus (abfd
, note
)
5936 Elf_Internal_Note
*note
;
5941 if (note
->descsz
== sizeof (prstatus_t
))
5945 raw_size
= sizeof (prstat
.pr_reg
);
5946 offset
= offsetof (prstatus_t
, pr_reg
);
5947 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5949 /* Do not overwrite the core signal if it
5950 has already been set by another thread. */
5951 if (elf_tdata (abfd
)->core_signal
== 0)
5952 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5953 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5955 /* pr_who exists on:
5958 pr_who doesn't exist on:
5961 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5962 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5965 #if defined (HAVE_PRSTATUS32_T)
5966 else if (note
->descsz
== sizeof (prstatus32_t
))
5968 /* 64-bit host, 32-bit corefile */
5969 prstatus32_t prstat
;
5971 raw_size
= sizeof (prstat
.pr_reg
);
5972 offset
= offsetof (prstatus32_t
, pr_reg
);
5973 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5975 /* Do not overwrite the core signal if it
5976 has already been set by another thread. */
5977 if (elf_tdata (abfd
)->core_signal
== 0)
5978 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5979 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5981 /* pr_who exists on:
5984 pr_who doesn't exist on:
5987 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5988 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5991 #endif /* HAVE_PRSTATUS32_T */
5994 /* Fail - we don't know how to handle any other
5995 note size (ie. data object type). */
5999 /* Make a ".reg/999" section and a ".reg" section. */
6000 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6001 raw_size
, note
->descpos
+ offset
);
6003 #endif /* defined (HAVE_PRSTATUS_T) */
6005 /* Create a pseudosection containing the exact contents of NOTE. */
6007 elfcore_make_note_pseudosection (abfd
, name
, note
)
6010 Elf_Internal_Note
*note
;
6012 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6013 note
->descsz
, note
->descpos
);
6016 /* There isn't a consistent prfpregset_t across platforms,
6017 but it doesn't matter, because we don't have to pick this
6018 data structure apart. */
6021 elfcore_grok_prfpreg (abfd
, note
)
6023 Elf_Internal_Note
*note
;
6025 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6028 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6029 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6033 elfcore_grok_prxfpreg (abfd
, note
)
6035 Elf_Internal_Note
*note
;
6037 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6040 #if defined (HAVE_PRPSINFO_T)
6041 typedef prpsinfo_t elfcore_psinfo_t
;
6042 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6043 typedef prpsinfo32_t elfcore_psinfo32_t
;
6047 #if defined (HAVE_PSINFO_T)
6048 typedef psinfo_t elfcore_psinfo_t
;
6049 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6050 typedef psinfo32_t elfcore_psinfo32_t
;
6054 /* return a malloc'ed copy of a string at START which is at
6055 most MAX bytes long, possibly without a terminating '\0'.
6056 the copy will always have a terminating '\0'. */
6059 _bfd_elfcore_strndup (abfd
, start
, max
)
6065 char *end
= memchr (start
, '\0', max
);
6073 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6077 memcpy (dups
, start
, len
);
6083 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6084 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6087 elfcore_grok_psinfo (abfd
, note
)
6089 Elf_Internal_Note
*note
;
6091 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6093 elfcore_psinfo_t psinfo
;
6095 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6097 elf_tdata (abfd
)->core_program
6098 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6099 sizeof (psinfo
.pr_fname
));
6101 elf_tdata (abfd
)->core_command
6102 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6103 sizeof (psinfo
.pr_psargs
));
6105 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6106 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6108 /* 64-bit host, 32-bit corefile */
6109 elfcore_psinfo32_t psinfo
;
6111 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6113 elf_tdata (abfd
)->core_program
6114 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6115 sizeof (psinfo
.pr_fname
));
6117 elf_tdata (abfd
)->core_command
6118 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6119 sizeof (psinfo
.pr_psargs
));
6125 /* Fail - we don't know how to handle any other
6126 note size (ie. data object type). */
6130 /* Note that for some reason, a spurious space is tacked
6131 onto the end of the args in some (at least one anyway)
6132 implementations, so strip it off if it exists. */
6135 char *command
= elf_tdata (abfd
)->core_command
;
6136 int n
= strlen (command
);
6138 if (0 < n
&& command
[n
- 1] == ' ')
6139 command
[n
- 1] = '\0';
6144 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6146 #if defined (HAVE_PSTATUS_T)
6148 elfcore_grok_pstatus (abfd
, note
)
6150 Elf_Internal_Note
*note
;
6152 if (note
->descsz
== sizeof (pstatus_t
)
6153 #if defined (HAVE_PXSTATUS_T)
6154 || note
->descsz
== sizeof (pxstatus_t
)
6160 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6162 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6164 #if defined (HAVE_PSTATUS32_T)
6165 else if (note
->descsz
== sizeof (pstatus32_t
))
6167 /* 64-bit host, 32-bit corefile */
6170 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6172 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6175 /* Could grab some more details from the "representative"
6176 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6177 NT_LWPSTATUS note, presumably. */
6181 #endif /* defined (HAVE_PSTATUS_T) */
6183 #if defined (HAVE_LWPSTATUS_T)
6185 elfcore_grok_lwpstatus (abfd
, note
)
6187 Elf_Internal_Note
*note
;
6189 lwpstatus_t lwpstat
;
6194 if (note
->descsz
!= sizeof (lwpstat
)
6195 #if defined (HAVE_LWPXSTATUS_T)
6196 && note
->descsz
!= sizeof (lwpxstatus_t
)
6201 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6203 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6204 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6206 /* Make a ".reg/999" section. */
6208 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6209 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6214 sect
= bfd_make_section (abfd
, name
);
6218 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6219 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6220 sect
->filepos
= note
->descpos
6221 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6224 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6225 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6226 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6229 sect
->flags
= SEC_HAS_CONTENTS
;
6230 sect
->alignment_power
= 2;
6232 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6235 /* Make a ".reg2/999" section */
6237 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6238 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6243 sect
= bfd_make_section (abfd
, name
);
6247 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6248 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6249 sect
->filepos
= note
->descpos
6250 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6253 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6254 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6255 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6258 sect
->flags
= SEC_HAS_CONTENTS
;
6259 sect
->alignment_power
= 2;
6261 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6263 #endif /* defined (HAVE_LWPSTATUS_T) */
6265 #if defined (HAVE_WIN32_PSTATUS_T)
6267 elfcore_grok_win32pstatus (abfd
, note
)
6269 Elf_Internal_Note
*note
;
6274 win32_pstatus_t pstatus
;
6276 if (note
->descsz
< sizeof (pstatus
))
6279 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
6281 switch (pstatus
.data_type
)
6283 case NOTE_INFO_PROCESS
:
6284 /* FIXME: need to add ->core_command. */
6285 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6286 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6289 case NOTE_INFO_THREAD
:
6290 /* Make a ".reg/999" section. */
6291 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6293 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6299 sect
= bfd_make_section (abfd
, name
);
6303 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6304 sect
->filepos
= (note
->descpos
6305 + offsetof (struct win32_pstatus
,
6306 data
.thread_info
.thread_context
));
6307 sect
->flags
= SEC_HAS_CONTENTS
;
6308 sect
->alignment_power
= 2;
6310 if (pstatus
.data
.thread_info
.is_active_thread
)
6311 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6315 case NOTE_INFO_MODULE
:
6316 /* Make a ".module/xxxxxxxx" section. */
6317 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6319 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6325 sect
= bfd_make_section (abfd
, name
);
6330 sect
->_raw_size
= note
->descsz
;
6331 sect
->filepos
= note
->descpos
;
6332 sect
->flags
= SEC_HAS_CONTENTS
;
6333 sect
->alignment_power
= 2;
6342 #endif /* HAVE_WIN32_PSTATUS_T */
6345 elfcore_grok_note (abfd
, note
)
6347 Elf_Internal_Note
*note
;
6349 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6357 if (bed
->elf_backend_grok_prstatus
)
6358 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6360 #if defined (HAVE_PRSTATUS_T)
6361 return elfcore_grok_prstatus (abfd
, note
);
6366 #if defined (HAVE_PSTATUS_T)
6368 return elfcore_grok_pstatus (abfd
, note
);
6371 #if defined (HAVE_LWPSTATUS_T)
6373 return elfcore_grok_lwpstatus (abfd
, note
);
6376 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6377 return elfcore_grok_prfpreg (abfd
, note
);
6379 #if defined (HAVE_WIN32_PSTATUS_T)
6380 case NT_WIN32PSTATUS
:
6381 return elfcore_grok_win32pstatus (abfd
, note
);
6384 case NT_PRXFPREG
: /* Linux SSE extension */
6385 if (note
->namesz
== 5
6386 && ! strcmp (note
->namedata
, "LINUX"))
6387 return elfcore_grok_prxfpreg (abfd
, note
);
6393 if (bed
->elf_backend_grok_psinfo
)
6394 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6396 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6397 return elfcore_grok_psinfo (abfd
, note
);
6405 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6406 Elf_Internal_Note
*note
;
6411 cp
= strchr (note
->namedata
, '@');
6421 elfcore_grok_netbsd_procinfo (abfd
, note
)
6423 Elf_Internal_Note
*note
;
6426 /* Signal number at offset 0x08. */
6427 elf_tdata (abfd
)->core_signal
6428 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6430 /* Process ID at offset 0x50. */
6431 elf_tdata (abfd
)->core_pid
6432 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6434 /* Command name at 0x7c (max 32 bytes, including nul). */
6435 elf_tdata (abfd
)->core_command
6436 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6442 elfcore_grok_netbsd_note (abfd
, note
)
6444 Elf_Internal_Note
*note
;
6448 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6449 elf_tdata (abfd
)->core_lwpid
= lwp
;
6451 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6453 /* NetBSD-specific core "procinfo". Note that we expect to
6454 find this note before any of the others, which is fine,
6455 since the kernel writes this note out first when it
6456 creates a core file. */
6458 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6461 /* As of Jan 2002 there are no other machine-independent notes
6462 defined for NetBSD core files. If the note type is less
6463 than the start of the machine-dependent note types, we don't
6466 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6470 switch (bfd_get_arch (abfd
))
6472 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6473 PT_GETFPREGS == mach+2. */
6475 case bfd_arch_alpha
:
6476 case bfd_arch_sparc
:
6479 case NT_NETBSDCORE_FIRSTMACH
+0:
6480 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6482 case NT_NETBSDCORE_FIRSTMACH
+2:
6483 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6489 /* On all other arch's, PT_GETREGS == mach+1 and
6490 PT_GETFPREGS == mach+3. */
6495 case NT_NETBSDCORE_FIRSTMACH
+1:
6496 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6498 case NT_NETBSDCORE_FIRSTMACH
+3:
6499 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6508 /* Function: elfcore_write_note
6515 size of data for note
6518 End of buffer containing note. */
6521 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6530 Elf_External_Note
*xnp
;
6531 int namesz
= strlen (name
);
6532 int newspace
= BFD_ALIGN (sizeof (Elf_External_Note
) + size
+ namesz
- 1, 4);
6535 p
= realloc (buf
, *bufsiz
+ newspace
);
6537 *bufsiz
+= newspace
;
6538 xnp
= (Elf_External_Note
*) dest
;
6539 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6540 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6541 H_PUT_32 (abfd
, type
, xnp
->type
);
6542 strcpy (xnp
->name
, name
);
6543 memcpy (xnp
->name
+ BFD_ALIGN (namesz
, 4), input
, size
);
6547 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6549 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6557 char *note_name
= "CORE";
6559 #if defined (HAVE_PSINFO_T)
6561 note_type
= NT_PSINFO
;
6564 note_type
= NT_PRPSINFO
;
6567 memset (&data
, 0, sizeof (data
));
6568 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6569 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6570 return elfcore_write_note (abfd
, buf
, bufsiz
,
6571 note_name
, note_type
, &data
, sizeof (data
));
6573 #endif /* PSINFO_T or PRPSINFO_T */
6575 #if defined (HAVE_PRSTATUS_T)
6577 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6586 char *note_name
= "CORE";
6588 memset (&prstat
, 0, sizeof (prstat
));
6589 prstat
.pr_pid
= pid
;
6590 prstat
.pr_cursig
= cursig
;
6591 memcpy (prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
6592 return elfcore_write_note (abfd
, buf
, bufsiz
,
6593 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
6595 #endif /* HAVE_PRSTATUS_T */
6597 #if defined (HAVE_PSTATUS_T)
6599 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6608 char *note_name
= "CORE";
6610 memset (&pstat
, 0, sizeof (prstat
));
6612 memcpy (pstat
.pr_reg
, gregs
, sizeof (pstat
.pr_reg
));
6613 return elfcore_write_note (abfd
, buf
, bufsiz
,
6614 note_name
, NT_PSTATUS
, &pstat
, sizeof (pstat
));
6616 #endif /* HAVE_PSTATUS_T */
6619 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
6626 char *note_name
= "CORE";
6627 return elfcore_write_note (abfd
, buf
, bufsiz
,
6628 note_name
, NT_FPREGSET
, fpregs
, size
);
6632 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
6639 char *note_name
= "LINUX";
6640 return elfcore_write_note (abfd
, buf
, bufsiz
,
6641 note_name
, NT_PRXFPREG
, xfpregs
, size
);
6645 elfcore_read_notes (abfd
, offset
, size
)
6656 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6659 buf
= bfd_malloc (size
);
6663 if (bfd_bread (buf
, size
, abfd
) != size
)
6671 while (p
< buf
+ size
)
6673 /* FIXME: bad alignment assumption. */
6674 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6675 Elf_Internal_Note in
;
6677 in
.type
= H_GET_32 (abfd
, xnp
->type
);
6679 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
6680 in
.namedata
= xnp
->name
;
6682 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
6683 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
6684 in
.descpos
= offset
+ (in
.descdata
- buf
);
6686 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
6688 if (! elfcore_grok_netbsd_note (abfd
, &in
))
6693 if (! elfcore_grok_note (abfd
, &in
))
6697 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
6704 /* Providing external access to the ELF program header table. */
6706 /* Return an upper bound on the number of bytes required to store a
6707 copy of ABFD's program header table entries. Return -1 if an error
6708 occurs; bfd_get_error will return an appropriate code. */
6711 bfd_get_elf_phdr_upper_bound (abfd
)
6714 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6716 bfd_set_error (bfd_error_wrong_format
);
6720 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
6723 /* Copy ABFD's program header table entries to *PHDRS. The entries
6724 will be stored as an array of Elf_Internal_Phdr structures, as
6725 defined in include/elf/internal.h. To find out how large the
6726 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6728 Return the number of program header table entries read, or -1 if an
6729 error occurs; bfd_get_error will return an appropriate code. */
6732 bfd_get_elf_phdrs (abfd
, phdrs
)
6738 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6740 bfd_set_error (bfd_error_wrong_format
);
6744 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
6745 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
6746 num_phdrs
* sizeof (Elf_Internal_Phdr
));
6752 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
6753 bfd
*abfd ATTRIBUTE_UNUSED
;
6758 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6760 i_ehdrp
= elf_elfheader (abfd
);
6761 if (i_ehdrp
== NULL
)
6762 sprintf_vma (buf
, value
);
6765 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6767 #if BFD_HOST_64BIT_LONG
6768 sprintf (buf
, "%016lx", value
);
6770 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
6771 _bfd_int64_low (value
));
6775 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
6778 sprintf_vma (buf
, value
);
6783 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
6784 bfd
*abfd ATTRIBUTE_UNUSED
;
6789 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6791 i_ehdrp
= elf_elfheader (abfd
);
6792 if (i_ehdrp
== NULL
)
6793 fprintf_vma ((FILE *) stream
, value
);
6796 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6798 #if BFD_HOST_64BIT_LONG
6799 fprintf ((FILE *) stream
, "%016lx", value
);
6801 fprintf ((FILE *) stream
, "%08lx%08lx",
6802 _bfd_int64_high (value
), _bfd_int64_low (value
));
6806 fprintf ((FILE *) stream
, "%08lx",
6807 (unsigned long) (value
& 0xffffffff));
6810 fprintf_vma ((FILE *) stream
, value
);
6814 enum elf_reloc_type_class
6815 _bfd_elf_reloc_type_class (rela
)
6816 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
6818 return reloc_class_normal
;
6821 /* For RELA architectures, return what the relocation value for
6822 relocation against a local symbol. */
6825 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
6827 Elf_Internal_Sym
*sym
;
6829 Elf_Internal_Rela
*rel
;
6833 relocation
= (sec
->output_section
->vma
6834 + sec
->output_offset
6836 if ((sec
->flags
& SEC_MERGE
)
6837 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
6838 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6844 _bfd_merged_section_offset (abfd
, &msec
,
6845 elf_section_data (sec
)->sec_info
,
6846 sym
->st_value
+ rel
->r_addend
,
6849 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
6855 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
6857 Elf_Internal_Sym
*sym
;
6861 asection
*sec
= *psec
;
6863 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
6864 return sym
->st_value
+ addend
;
6866 return _bfd_merged_section_offset (abfd
, psec
,
6867 elf_section_data (sec
)->sec_info
,
6868 sym
->st_value
+ addend
, (bfd_vma
) 0);
6872 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
6874 struct bfd_link_info
*info
;
6878 struct bfd_elf_section_data
*sec_data
;
6880 sec_data
= elf_section_data (sec
);
6881 switch (sec_data
->sec_info_type
)
6883 case ELF_INFO_TYPE_STABS
:
6884 return _bfd_stab_section_offset
6885 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
6887 case ELF_INFO_TYPE_EH_FRAME
:
6888 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);