1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet. */
33 /* For sparc64-cross-sparc32. */
41 #include "libiberty.h"
43 static INLINE
struct elf_segment_map
*make_mapping
44 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
45 static boolean map_sections_to_segments
PARAMS ((bfd
*));
46 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
47 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
48 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
49 static boolean prep_headers
PARAMS ((bfd
*));
50 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
51 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
52 static char *elf_read
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
53 static boolean setup_group
PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
54 static void merge_sections_remove_hook
PARAMS ((bfd
*, asection
*));
55 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
56 static void set_group_contents
PARAMS ((bfd
*, asection
*, PTR
));
57 static boolean assign_section_numbers
PARAMS ((bfd
*));
58 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
59 static boolean elf_map_symbols
PARAMS ((bfd
*));
60 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
61 static boolean elfcore_read_notes
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
62 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
63 bfd_vma
, const char **,
65 static int elfcore_make_pid
PARAMS ((bfd
*));
66 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
67 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
68 Elf_Internal_Note
*));
69 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
70 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
71 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
73 static boolean elfcore_netbsd_get_lwpid
PARAMS ((Elf_Internal_Note
*, int *));
74 static boolean elfcore_grok_netbsd_procinfo
PARAMS ((bfd
*,
75 Elf_Internal_Note
*));
76 static boolean elfcore_grok_netbsd_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
78 /* Swap version information in and out. The version information is
79 currently size independent. If that ever changes, this code will
80 need to move into elfcode.h. */
82 /* Swap in a Verdef structure. */
85 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
87 const Elf_External_Verdef
*src
;
88 Elf_Internal_Verdef
*dst
;
90 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
91 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
92 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
93 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
94 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
95 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
96 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
99 /* Swap out a Verdef structure. */
102 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
104 const Elf_Internal_Verdef
*src
;
105 Elf_External_Verdef
*dst
;
107 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
108 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
109 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
110 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
111 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
112 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
113 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
116 /* Swap in a Verdaux structure. */
119 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
121 const Elf_External_Verdaux
*src
;
122 Elf_Internal_Verdaux
*dst
;
124 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
125 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
128 /* Swap out a Verdaux structure. */
131 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
133 const Elf_Internal_Verdaux
*src
;
134 Elf_External_Verdaux
*dst
;
136 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
137 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
140 /* Swap in a Verneed structure. */
143 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
145 const Elf_External_Verneed
*src
;
146 Elf_Internal_Verneed
*dst
;
148 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
149 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
150 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
151 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
152 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
155 /* Swap out a Verneed structure. */
158 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
160 const Elf_Internal_Verneed
*src
;
161 Elf_External_Verneed
*dst
;
163 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
164 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
165 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
166 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
167 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
170 /* Swap in a Vernaux structure. */
173 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
175 const Elf_External_Vernaux
*src
;
176 Elf_Internal_Vernaux
*dst
;
178 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
179 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
180 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
181 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
182 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
185 /* Swap out a Vernaux structure. */
188 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
190 const Elf_Internal_Vernaux
*src
;
191 Elf_External_Vernaux
*dst
;
193 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
194 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
195 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
196 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
197 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
200 /* Swap in a Versym structure. */
203 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
205 const Elf_External_Versym
*src
;
206 Elf_Internal_Versym
*dst
;
208 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
211 /* Swap out a Versym structure. */
214 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
216 const Elf_Internal_Versym
*src
;
217 Elf_External_Versym
*dst
;
219 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
222 /* Standard ELF hash function. Do not change this function; you will
223 cause invalid hash tables to be generated. */
226 bfd_elf_hash (namearg
)
229 const unsigned char *name
= (const unsigned char *) namearg
;
234 while ((ch
= *name
++) != '\0')
237 if ((g
= (h
& 0xf0000000)) != 0)
240 /* The ELF ABI says `h &= ~g', but this is equivalent in
241 this case and on some machines one insn instead of two. */
248 /* Read a specified number of bytes at a specified offset in an ELF
249 file, into a newly allocated buffer, and return a pointer to the
253 elf_read (abfd
, offset
, size
)
260 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
262 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
264 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
266 if (bfd_get_error () != bfd_error_system_call
)
267 bfd_set_error (bfd_error_file_truncated
);
274 bfd_elf_mkobject (abfd
)
277 /* This just does initialization. */
278 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
279 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
280 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
281 if (elf_tdata (abfd
) == 0)
283 /* Since everything is done at close time, do we need any
290 bfd_elf_mkcorefile (abfd
)
293 /* I think this can be done just like an object file. */
294 return bfd_elf_mkobject (abfd
);
298 bfd_elf_get_str_section (abfd
, shindex
)
300 unsigned int shindex
;
302 Elf_Internal_Shdr
**i_shdrp
;
303 char *shstrtab
= NULL
;
305 bfd_size_type shstrtabsize
;
307 i_shdrp
= elf_elfsections (abfd
);
308 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
311 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
312 if (shstrtab
== NULL
)
314 /* No cached one, attempt to read, and cache what we read. */
315 offset
= i_shdrp
[shindex
]->sh_offset
;
316 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
317 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
318 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
324 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
326 unsigned int shindex
;
327 unsigned int strindex
;
329 Elf_Internal_Shdr
*hdr
;
334 hdr
= elf_elfsections (abfd
)[shindex
];
336 if (hdr
->contents
== NULL
337 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
340 if (strindex
>= hdr
->sh_size
)
342 (*_bfd_error_handler
)
343 (_("%s: invalid string offset %u >= %lu for section `%s'"),
344 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
345 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
346 && strindex
== hdr
->sh_name
)
348 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
352 return ((char *) hdr
->contents
) + strindex
;
355 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
356 sections. The first element is the flags, the rest are section
359 typedef union elf_internal_group
{
360 Elf_Internal_Shdr
*shdr
;
362 } Elf_Internal_Group
;
364 /* Set next_in_group list pointer, and group name for NEWSECT. */
367 setup_group (abfd
, hdr
, newsect
)
369 Elf_Internal_Shdr
*hdr
;
372 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
374 /* If num_group is zero, read in all SHT_GROUP sections. The count
375 is set to -1 if there are no SHT_GROUP sections. */
378 unsigned int i
, shnum
;
380 /* First count the number of groups. If we have a SHT_GROUP
381 section with just a flag word (ie. sh_size is 4), ignore it. */
382 shnum
= elf_numsections (abfd
);
384 for (i
= 0; i
< shnum
; i
++)
386 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
387 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
392 num_group
= (unsigned) -1;
393 elf_tdata (abfd
)->num_group
= num_group
;
397 /* We keep a list of elf section headers for group sections,
398 so we can find them quickly. */
399 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
400 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
401 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
405 for (i
= 0; i
< shnum
; i
++)
407 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
408 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
411 Elf_Internal_Group
*dest
;
413 /* Add to list of sections. */
414 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
417 /* Read the raw contents. */
418 BFD_ASSERT (sizeof (*dest
) >= 4);
419 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
420 shdr
->contents
= bfd_alloc (abfd
, amt
);
421 if (shdr
->contents
== NULL
422 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
423 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
427 /* Translate raw contents, a flag word followed by an
428 array of elf section indices all in target byte order,
429 to the flag word followed by an array of elf section
431 src
= shdr
->contents
+ shdr
->sh_size
;
432 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
439 idx
= H_GET_32 (abfd
, src
);
440 if (src
== shdr
->contents
)
447 ((*_bfd_error_handler
)
448 (_("%s: invalid SHT_GROUP entry"),
449 bfd_archive_filename (abfd
)));
452 dest
->shdr
= elf_elfsections (abfd
)[idx
];
459 if (num_group
!= (unsigned) -1)
463 for (i
= 0; i
< num_group
; i
++)
465 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
466 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
467 unsigned int n_elt
= shdr
->sh_size
/ 4;
469 /* Look through this group's sections to see if current
470 section is a member. */
472 if ((++idx
)->shdr
== hdr
)
476 /* We are a member of this group. Go looking through
477 other members to see if any others are linked via
479 idx
= (Elf_Internal_Group
*) shdr
->contents
;
480 n_elt
= shdr
->sh_size
/ 4;
482 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
483 && elf_next_in_group (s
) != NULL
)
487 /* Snarf the group name from other member, and
488 insert current section in circular list. */
489 elf_group_name (newsect
) = elf_group_name (s
);
490 elf_next_in_group (newsect
) = elf_next_in_group (s
);
491 elf_next_in_group (s
) = newsect
;
495 struct elf_backend_data
*bed
;
497 unsigned char ename
[4];
501 /* Humbug. Get the name from the group signature
502 symbol. Why isn't the signature just a string?
503 Fortunately, the name index is at the same
504 place in the external symbol for both 32 and 64
506 bed
= get_elf_backend_data (abfd
);
507 pos
= elf_tdata (abfd
)->symtab_hdr
.sh_offset
;
508 pos
+= shdr
->sh_info
* bed
->s
->sizeof_sym
;
509 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
510 || bfd_bread (ename
, (bfd_size_type
) 4, abfd
) != 4)
512 iname
= H_GET_32 (abfd
, ename
);
513 gname
= elf_string_from_elf_strtab (abfd
, iname
);
514 elf_group_name (newsect
) = gname
;
516 /* Start a circular list with one element. */
517 elf_next_in_group (newsect
) = newsect
;
519 if (shdr
->bfd_section
!= NULL
)
520 elf_next_in_group (shdr
->bfd_section
) = newsect
;
527 if (elf_group_name (newsect
) == NULL
)
529 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
530 bfd_archive_filename (abfd
), newsect
->name
);
535 /* Make a BFD section from an ELF section. We store a pointer to the
536 BFD section in the bfd_section field of the header. */
539 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
541 Elf_Internal_Shdr
*hdr
;
546 struct elf_backend_data
*bed
;
548 if (hdr
->bfd_section
!= NULL
)
550 BFD_ASSERT (strcmp (name
,
551 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
555 newsect
= bfd_make_section_anyway (abfd
, name
);
559 newsect
->filepos
= hdr
->sh_offset
;
561 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
562 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
563 || ! bfd_set_section_alignment (abfd
, newsect
,
564 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
567 flags
= SEC_NO_FLAGS
;
568 if (hdr
->sh_type
!= SHT_NOBITS
)
569 flags
|= SEC_HAS_CONTENTS
;
570 if (hdr
->sh_type
== SHT_GROUP
)
571 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
572 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
575 if (hdr
->sh_type
!= SHT_NOBITS
)
578 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
579 flags
|= SEC_READONLY
;
580 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
582 else if ((flags
& SEC_LOAD
) != 0)
584 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
587 newsect
->entsize
= hdr
->sh_entsize
;
588 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
589 flags
|= SEC_STRINGS
;
591 if (hdr
->sh_flags
& SHF_GROUP
)
592 if (!setup_group (abfd
, hdr
, newsect
))
594 if ((hdr
->sh_flags
& SHF_TLS
) != 0)
595 flags
|= SEC_THREAD_LOCAL
;
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.
658 Note - we used to check the p_paddr field as well, and
659 refuse to set the LMA if it was 0. This is wrong
660 though, as a perfectly valid initialised segment can
661 have a p_paddr of zero. Some architectures, eg ARM,
662 place special significance on the address 0 and
663 executables need to be able to have a segment which
664 covers this address. */
665 if (phdr
->p_type
== PT_LOAD
666 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
667 && (hdr
->sh_offset
+ hdr
->sh_size
668 <= phdr
->p_offset
+ phdr
->p_memsz
)
669 && ((flags
& SEC_LOAD
) == 0
670 || (hdr
->sh_offset
+ hdr
->sh_size
671 <= phdr
->p_offset
+ phdr
->p_filesz
)))
673 if ((flags
& SEC_LOAD
) == 0)
674 newsect
->lma
= (phdr
->p_paddr
675 + hdr
->sh_addr
- phdr
->p_vaddr
);
677 /* We used to use the same adjustment for SEC_LOAD
678 sections, but that doesn't work if the segment
679 is packed with code from multiple VMAs.
680 Instead we calculate the section LMA based on
681 the segment LMA. It is assumed that the
682 segment will contain sections with contiguous
683 LMAs, even if the VMAs are not. */
684 newsect
->lma
= (phdr
->p_paddr
685 + hdr
->sh_offset
- phdr
->p_offset
);
687 /* With contiguous segments, we can't tell from file
688 offsets whether a section with zero size should
689 be placed at the end of one segment or the
690 beginning of the next. Decide based on vaddr. */
691 if (hdr
->sh_addr
>= phdr
->p_vaddr
692 && (hdr
->sh_addr
+ hdr
->sh_size
693 <= phdr
->p_vaddr
+ phdr
->p_memsz
))
700 hdr
->bfd_section
= newsect
;
701 elf_section_data (newsect
)->this_hdr
= *hdr
;
711 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
714 Helper functions for GDB to locate the string tables.
715 Since BFD hides string tables from callers, GDB needs to use an
716 internal hook to find them. Sun's .stabstr, in particular,
717 isn't even pointed to by the .stab section, so ordinary
718 mechanisms wouldn't work to find it, even if we had some.
721 struct elf_internal_shdr
*
722 bfd_elf_find_section (abfd
, name
)
726 Elf_Internal_Shdr
**i_shdrp
;
731 i_shdrp
= elf_elfsections (abfd
);
734 shstrtab
= bfd_elf_get_str_section (abfd
,
735 elf_elfheader (abfd
)->e_shstrndx
);
736 if (shstrtab
!= NULL
)
738 max
= elf_numsections (abfd
);
739 for (i
= 1; i
< max
; i
++)
740 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
747 const char *const bfd_elf_section_type_names
[] = {
748 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
749 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
750 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
753 /* ELF relocs are against symbols. If we are producing relocateable
754 output, and the reloc is against an external symbol, and nothing
755 has given us any additional addend, the resulting reloc will also
756 be against the same symbol. In such a case, we don't want to
757 change anything about the way the reloc is handled, since it will
758 all be done at final link time. Rather than put special case code
759 into bfd_perform_relocation, all the reloc types use this howto
760 function. It just short circuits the reloc if producing
761 relocateable output against an external symbol. */
763 bfd_reloc_status_type
764 bfd_elf_generic_reloc (abfd
,
771 bfd
*abfd ATTRIBUTE_UNUSED
;
772 arelent
*reloc_entry
;
774 PTR data ATTRIBUTE_UNUSED
;
775 asection
*input_section
;
777 char **error_message ATTRIBUTE_UNUSED
;
779 if (output_bfd
!= (bfd
*) NULL
780 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
781 && (! reloc_entry
->howto
->partial_inplace
782 || reloc_entry
->addend
== 0))
784 reloc_entry
->address
+= input_section
->output_offset
;
788 return bfd_reloc_continue
;
791 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
794 merge_sections_remove_hook (abfd
, sec
)
795 bfd
*abfd ATTRIBUTE_UNUSED
;
798 struct bfd_elf_section_data
*sec_data
;
800 sec_data
= elf_section_data (sec
);
801 BFD_ASSERT (sec_data
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
802 sec_data
->sec_info_type
= ELF_INFO_TYPE_NONE
;
805 /* Finish SHF_MERGE section merging. */
808 _bfd_elf_merge_sections (abfd
, info
)
810 struct bfd_link_info
*info
;
812 if (!is_elf_hash_table (info
))
814 if (elf_hash_table (info
)->merge_info
)
815 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
816 merge_sections_remove_hook
);
821 _bfd_elf_link_just_syms (sec
, info
)
823 struct bfd_link_info
*info
;
825 sec
->output_section
= bfd_abs_section_ptr
;
826 sec
->output_offset
= sec
->vma
;
827 if (!is_elf_hash_table (info
))
830 elf_section_data (sec
)->sec_info_type
= ELF_INFO_TYPE_JUST_SYMS
;
833 /* Copy the program header and other data from one object module to
837 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
841 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
842 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
845 BFD_ASSERT (!elf_flags_init (obfd
)
846 || (elf_elfheader (obfd
)->e_flags
847 == elf_elfheader (ibfd
)->e_flags
));
849 elf_gp (obfd
) = elf_gp (ibfd
);
850 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
851 elf_flags_init (obfd
) = true;
855 /* Print out the program headers. */
858 _bfd_elf_print_private_bfd_data (abfd
, farg
)
862 FILE *f
= (FILE *) farg
;
863 Elf_Internal_Phdr
*p
;
865 bfd_byte
*dynbuf
= NULL
;
867 p
= elf_tdata (abfd
)->phdr
;
872 fprintf (f
, _("\nProgram Header:\n"));
873 c
= elf_elfheader (abfd
)->e_phnum
;
874 for (i
= 0; i
< c
; i
++, p
++)
881 case PT_NULL
: pt
= "NULL"; break;
882 case PT_LOAD
: pt
= "LOAD"; break;
883 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
884 case PT_INTERP
: pt
= "INTERP"; break;
885 case PT_NOTE
: pt
= "NOTE"; break;
886 case PT_SHLIB
: pt
= "SHLIB"; break;
887 case PT_PHDR
: pt
= "PHDR"; break;
888 case PT_TLS
: pt
= "TLS"; break;
889 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
890 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
892 fprintf (f
, "%8s off 0x", pt
);
893 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
894 fprintf (f
, " vaddr 0x");
895 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
896 fprintf (f
, " paddr 0x");
897 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
898 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
899 fprintf (f
, " filesz 0x");
900 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
901 fprintf (f
, " memsz 0x");
902 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
903 fprintf (f
, " flags %c%c%c",
904 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
905 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
906 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
907 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
908 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
913 s
= bfd_get_section_by_name (abfd
, ".dynamic");
917 unsigned long shlink
;
918 bfd_byte
*extdyn
, *extdynend
;
920 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
922 fprintf (f
, _("\nDynamic Section:\n"));
924 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
927 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
931 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
934 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
936 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
937 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
940 extdynend
= extdyn
+ s
->_raw_size
;
941 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
943 Elf_Internal_Dyn dyn
;
948 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
950 if (dyn
.d_tag
== DT_NULL
)
957 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
961 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
962 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
963 case DT_PLTGOT
: name
= "PLTGOT"; break;
964 case DT_HASH
: name
= "HASH"; break;
965 case DT_STRTAB
: name
= "STRTAB"; break;
966 case DT_SYMTAB
: name
= "SYMTAB"; break;
967 case DT_RELA
: name
= "RELA"; break;
968 case DT_RELASZ
: name
= "RELASZ"; break;
969 case DT_RELAENT
: name
= "RELAENT"; break;
970 case DT_STRSZ
: name
= "STRSZ"; break;
971 case DT_SYMENT
: name
= "SYMENT"; break;
972 case DT_INIT
: name
= "INIT"; break;
973 case DT_FINI
: name
= "FINI"; break;
974 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
975 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
976 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
977 case DT_REL
: name
= "REL"; break;
978 case DT_RELSZ
: name
= "RELSZ"; break;
979 case DT_RELENT
: name
= "RELENT"; break;
980 case DT_PLTREL
: name
= "PLTREL"; break;
981 case DT_DEBUG
: name
= "DEBUG"; break;
982 case DT_TEXTREL
: name
= "TEXTREL"; break;
983 case DT_JMPREL
: name
= "JMPREL"; break;
984 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
985 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
986 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
987 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
988 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
989 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
990 case DT_FLAGS
: name
= "FLAGS"; break;
991 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
992 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
993 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
994 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
995 case DT_MOVEENT
: name
= "MOVEENT"; break;
996 case DT_MOVESZ
: name
= "MOVESZ"; break;
997 case DT_FEATURE
: name
= "FEATURE"; break;
998 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
999 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
1000 case DT_SYMINENT
: name
= "SYMINENT"; break;
1001 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
1002 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
1003 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
1004 case DT_PLTPAD
: name
= "PLTPAD"; break;
1005 case DT_MOVETAB
: name
= "MOVETAB"; break;
1006 case DT_SYMINFO
: name
= "SYMINFO"; break;
1007 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
1008 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
1009 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
1010 case DT_VERSYM
: name
= "VERSYM"; break;
1011 case DT_VERDEF
: name
= "VERDEF"; break;
1012 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
1013 case DT_VERNEED
: name
= "VERNEED"; break;
1014 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
1015 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
1016 case DT_USED
: name
= "USED"; break;
1017 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
1020 fprintf (f
, " %-11s ", name
);
1022 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
1026 unsigned int tagv
= dyn
.d_un
.d_val
;
1028 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1031 fprintf (f
, "%s", string
);
1040 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1041 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1043 if (! _bfd_elf_slurp_version_tables (abfd
))
1047 if (elf_dynverdef (abfd
) != 0)
1049 Elf_Internal_Verdef
*t
;
1051 fprintf (f
, _("\nVersion definitions:\n"));
1052 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1054 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1055 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1056 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1058 Elf_Internal_Verdaux
*a
;
1061 for (a
= t
->vd_auxptr
->vda_nextptr
;
1064 fprintf (f
, "%s ", a
->vda_nodename
);
1070 if (elf_dynverref (abfd
) != 0)
1072 Elf_Internal_Verneed
*t
;
1074 fprintf (f
, _("\nVersion References:\n"));
1075 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1077 Elf_Internal_Vernaux
*a
;
1079 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1080 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1081 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1082 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1094 /* Display ELF-specific fields of a symbol. */
1097 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1101 bfd_print_symbol_type how
;
1103 FILE *file
= (FILE *) filep
;
1106 case bfd_print_symbol_name
:
1107 fprintf (file
, "%s", symbol
->name
);
1109 case bfd_print_symbol_more
:
1110 fprintf (file
, "elf ");
1111 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1112 fprintf (file
, " %lx", (long) symbol
->flags
);
1114 case bfd_print_symbol_all
:
1116 const char *section_name
;
1117 const char *name
= NULL
;
1118 struct elf_backend_data
*bed
;
1119 unsigned char st_other
;
1122 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1124 bed
= get_elf_backend_data (abfd
);
1125 if (bed
->elf_backend_print_symbol_all
)
1126 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1130 name
= symbol
->name
;
1131 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1134 fprintf (file
, " %s\t", section_name
);
1135 /* Print the "other" value for a symbol. For common symbols,
1136 we've already printed the size; now print the alignment.
1137 For other symbols, we have no specified alignment, and
1138 we've printed the address; now print the size. */
1139 if (bfd_is_com_section (symbol
->section
))
1140 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1142 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1143 bfd_fprintf_vma (abfd
, file
, val
);
1145 /* If we have version information, print it. */
1146 if (elf_tdata (abfd
)->dynversym_section
!= 0
1147 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1148 || elf_tdata (abfd
)->dynverref_section
!= 0))
1150 unsigned int vernum
;
1151 const char *version_string
;
1153 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1156 version_string
= "";
1157 else if (vernum
== 1)
1158 version_string
= "Base";
1159 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1161 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1164 Elf_Internal_Verneed
*t
;
1166 version_string
= "";
1167 for (t
= elf_tdata (abfd
)->verref
;
1171 Elf_Internal_Vernaux
*a
;
1173 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1175 if (a
->vna_other
== vernum
)
1177 version_string
= a
->vna_nodename
;
1184 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1185 fprintf (file
, " %-11s", version_string
);
1190 fprintf (file
, " (%s)", version_string
);
1191 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1196 /* If the st_other field is not zero, print it. */
1197 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1202 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1203 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1204 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1206 /* Some other non-defined flags are also present, so print
1208 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1211 fprintf (file
, " %s", name
);
1217 /* Create an entry in an ELF linker hash table. */
1219 struct bfd_hash_entry
*
1220 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1221 struct bfd_hash_entry
*entry
;
1222 struct bfd_hash_table
*table
;
1225 /* Allocate the structure if it has not already been allocated by a
1229 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1234 /* Call the allocation method of the superclass. */
1235 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1238 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1239 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1241 /* Set local fields. */
1245 ret
->dynstr_index
= 0;
1246 ret
->weakdef
= NULL
;
1247 ret
->got
.refcount
= htab
->init_refcount
;
1248 ret
->plt
.refcount
= htab
->init_refcount
;
1249 ret
->linker_section_pointer
= NULL
;
1250 ret
->verinfo
.verdef
= NULL
;
1251 ret
->vtable_entries_used
= NULL
;
1252 ret
->vtable_entries_size
= 0;
1253 ret
->vtable_parent
= NULL
;
1254 ret
->type
= STT_NOTYPE
;
1256 /* Assume that we have been called by a non-ELF symbol reader.
1257 This flag is then reset by the code which reads an ELF input
1258 file. This ensures that a symbol created by a non-ELF symbol
1259 reader will have the flag set correctly. */
1260 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1266 /* Copy data from an indirect symbol to its direct symbol, hiding the
1267 old indirect symbol. Also used for copying flags to a weakdef. */
1270 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1271 struct elf_link_hash_entry
*dir
, *ind
;
1275 /* Copy down any references that we may have already seen to the
1276 symbol which just became indirect. */
1278 dir
->elf_link_hash_flags
|=
1279 (ind
->elf_link_hash_flags
1280 & (ELF_LINK_HASH_REF_DYNAMIC
1281 | ELF_LINK_HASH_REF_REGULAR
1282 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1283 | ELF_LINK_NON_GOT_REF
));
1285 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1288 /* Copy over the global and procedure linkage table refcount entries.
1289 These may have been already set up by a check_relocs routine. */
1290 tmp
= dir
->got
.refcount
;
1293 dir
->got
.refcount
= ind
->got
.refcount
;
1294 ind
->got
.refcount
= tmp
;
1297 BFD_ASSERT (ind
->got
.refcount
<= 0);
1299 tmp
= dir
->plt
.refcount
;
1302 dir
->plt
.refcount
= ind
->plt
.refcount
;
1303 ind
->plt
.refcount
= tmp
;
1306 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1308 if (dir
->dynindx
== -1)
1310 dir
->dynindx
= ind
->dynindx
;
1311 dir
->dynstr_index
= ind
->dynstr_index
;
1313 ind
->dynstr_index
= 0;
1316 BFD_ASSERT (ind
->dynindx
== -1);
1320 _bfd_elf_link_hash_hide_symbol (info
, h
, force_local
)
1321 struct bfd_link_info
*info
;
1322 struct elf_link_hash_entry
*h
;
1323 boolean force_local
;
1325 h
->plt
.offset
= (bfd_vma
) -1;
1326 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1329 h
->elf_link_hash_flags
|= ELF_LINK_FORCED_LOCAL
;
1330 if (h
->dynindx
!= -1)
1333 _bfd_elf_strtab_delref (elf_hash_table (info
)->dynstr
,
1339 /* Initialize an ELF linker hash table. */
1342 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1343 struct elf_link_hash_table
*table
;
1345 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1346 struct bfd_hash_table
*,
1351 table
->dynamic_sections_created
= false;
1352 table
->dynobj
= NULL
;
1353 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1354 /* The first dynamic symbol is a dummy. */
1355 table
->dynsymcount
= 1;
1356 table
->dynstr
= NULL
;
1357 table
->bucketcount
= 0;
1358 table
->needed
= NULL
;
1359 table
->runpath
= NULL
;
1361 table
->stab_info
= NULL
;
1362 table
->merge_info
= NULL
;
1363 table
->dynlocal
= NULL
;
1364 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1365 table
->root
.type
= bfd_link_elf_hash_table
;
1370 /* Create an ELF linker hash table. */
1372 struct bfd_link_hash_table
*
1373 _bfd_elf_link_hash_table_create (abfd
)
1376 struct elf_link_hash_table
*ret
;
1377 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1379 ret
= (struct elf_link_hash_table
*) bfd_malloc (amt
);
1380 if (ret
== (struct elf_link_hash_table
*) NULL
)
1383 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1392 /* This is a hook for the ELF emulation code in the generic linker to
1393 tell the backend linker what file name to use for the DT_NEEDED
1394 entry for a dynamic object. The generic linker passes name as an
1395 empty string to indicate that no DT_NEEDED entry should be made. */
1398 bfd_elf_set_dt_needed_name (abfd
, name
)
1402 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1403 && bfd_get_format (abfd
) == bfd_object
)
1404 elf_dt_name (abfd
) = name
;
1408 bfd_elf_set_dt_needed_soname (abfd
, name
)
1412 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1413 && bfd_get_format (abfd
) == bfd_object
)
1414 elf_dt_soname (abfd
) = name
;
1417 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1418 the linker ELF emulation code. */
1420 struct bfd_link_needed_list
*
1421 bfd_elf_get_needed_list (abfd
, info
)
1422 bfd
*abfd ATTRIBUTE_UNUSED
;
1423 struct bfd_link_info
*info
;
1425 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1427 return elf_hash_table (info
)->needed
;
1430 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1431 hook for the linker ELF emulation code. */
1433 struct bfd_link_needed_list
*
1434 bfd_elf_get_runpath_list (abfd
, info
)
1435 bfd
*abfd ATTRIBUTE_UNUSED
;
1436 struct bfd_link_info
*info
;
1438 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1440 return elf_hash_table (info
)->runpath
;
1443 /* Get the name actually used for a dynamic object for a link. This
1444 is the SONAME entry if there is one. Otherwise, it is the string
1445 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1448 bfd_elf_get_dt_soname (abfd
)
1451 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1452 && bfd_get_format (abfd
) == bfd_object
)
1453 return elf_dt_name (abfd
);
1457 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1458 the ELF linker emulation code. */
1461 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1463 struct bfd_link_needed_list
**pneeded
;
1466 bfd_byte
*dynbuf
= NULL
;
1468 unsigned long shlink
;
1469 bfd_byte
*extdyn
, *extdynend
;
1471 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1475 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1476 || bfd_get_format (abfd
) != bfd_object
)
1479 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1480 if (s
== NULL
|| s
->_raw_size
== 0)
1483 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1487 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1491 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1495 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1497 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1498 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1501 extdynend
= extdyn
+ s
->_raw_size
;
1502 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1504 Elf_Internal_Dyn dyn
;
1506 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1508 if (dyn
.d_tag
== DT_NULL
)
1511 if (dyn
.d_tag
== DT_NEEDED
)
1514 struct bfd_link_needed_list
*l
;
1515 unsigned int tagv
= dyn
.d_un
.d_val
;
1518 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1523 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1544 /* Allocate an ELF string table--force the first byte to be zero. */
1546 struct bfd_strtab_hash
*
1547 _bfd_elf_stringtab_init ()
1549 struct bfd_strtab_hash
*ret
;
1551 ret
= _bfd_stringtab_init ();
1556 loc
= _bfd_stringtab_add (ret
, "", true, false);
1557 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1558 if (loc
== (bfd_size_type
) -1)
1560 _bfd_stringtab_free (ret
);
1567 /* ELF .o/exec file reading */
1569 /* Create a new bfd section from an ELF section header. */
1572 bfd_section_from_shdr (abfd
, shindex
)
1574 unsigned int shindex
;
1576 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1577 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1578 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1581 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1583 switch (hdr
->sh_type
)
1586 /* Inactive section. Throw it away. */
1589 case SHT_PROGBITS
: /* Normal section with contents. */
1590 case SHT_DYNAMIC
: /* Dynamic linking information. */
1591 case SHT_NOBITS
: /* .bss section. */
1592 case SHT_HASH
: /* .hash section. */
1593 case SHT_NOTE
: /* .note section. */
1594 case SHT_INIT_ARRAY
: /* .init_array section. */
1595 case SHT_FINI_ARRAY
: /* .fini_array section. */
1596 case SHT_PREINIT_ARRAY
: /* .preinit_array section. */
1597 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1599 case SHT_SYMTAB
: /* A symbol table */
1600 if (elf_onesymtab (abfd
) == shindex
)
1603 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1604 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1605 elf_onesymtab (abfd
) = shindex
;
1606 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1607 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1608 abfd
->flags
|= HAS_SYMS
;
1610 /* Sometimes a shared object will map in the symbol table. If
1611 SHF_ALLOC is set, and this is a shared object, then we also
1612 treat this section as a BFD section. We can not base the
1613 decision purely on SHF_ALLOC, because that flag is sometimes
1614 set in a relocateable object file, which would confuse the
1616 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1617 && (abfd
->flags
& DYNAMIC
) != 0
1618 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1623 case SHT_DYNSYM
: /* A dynamic symbol table */
1624 if (elf_dynsymtab (abfd
) == shindex
)
1627 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1628 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1629 elf_dynsymtab (abfd
) = shindex
;
1630 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1631 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1632 abfd
->flags
|= HAS_SYMS
;
1634 /* Besides being a symbol table, we also treat this as a regular
1635 section, so that objcopy can handle it. */
1636 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1638 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1639 if (elf_symtab_shndx (abfd
) == shindex
)
1642 /* Get the associated symbol table. */
1643 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1644 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1647 elf_symtab_shndx (abfd
) = shindex
;
1648 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1649 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1652 case SHT_STRTAB
: /* A string table */
1653 if (hdr
->bfd_section
!= NULL
)
1655 if (ehdr
->e_shstrndx
== shindex
)
1657 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1658 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1662 unsigned int i
, num_sec
;
1664 num_sec
= elf_numsections (abfd
);
1665 for (i
= 1; i
< num_sec
; i
++)
1667 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1668 if (hdr2
->sh_link
== shindex
)
1670 if (! bfd_section_from_shdr (abfd
, i
))
1672 if (elf_onesymtab (abfd
) == i
)
1674 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1675 elf_elfsections (abfd
)[shindex
] =
1676 &elf_tdata (abfd
)->strtab_hdr
;
1679 if (elf_dynsymtab (abfd
) == i
)
1681 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1682 elf_elfsections (abfd
)[shindex
] = hdr
=
1683 &elf_tdata (abfd
)->dynstrtab_hdr
;
1684 /* We also treat this as a regular section, so
1685 that objcopy can handle it. */
1688 #if 0 /* Not handling other string tables specially right now. */
1689 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1690 /* We have a strtab for some random other section. */
1691 newsect
= (asection
*) hdr2
->bfd_section
;
1694 hdr
->bfd_section
= newsect
;
1695 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1697 elf_elfsections (abfd
)[shindex
] = hdr2
;
1703 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1707 /* *These* do a lot of work -- but build no sections! */
1709 asection
*target_sect
;
1710 Elf_Internal_Shdr
*hdr2
;
1711 unsigned int num_sec
= elf_numsections (abfd
);
1713 /* Check for a bogus link to avoid crashing. */
1714 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1715 || hdr
->sh_link
>= num_sec
)
1717 ((*_bfd_error_handler
)
1718 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1719 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1720 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1723 /* For some incomprehensible reason Oracle distributes
1724 libraries for Solaris in which some of the objects have
1725 bogus sh_link fields. It would be nice if we could just
1726 reject them, but, unfortunately, some people need to use
1727 them. We scan through the section headers; if we find only
1728 one suitable symbol table, we clobber the sh_link to point
1729 to it. I hope this doesn't break anything. */
1730 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1731 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1737 for (scan
= 1; scan
< num_sec
; scan
++)
1739 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1740 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1751 hdr
->sh_link
= found
;
1754 /* Get the symbol table. */
1755 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1756 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1759 /* If this reloc section does not use the main symbol table we
1760 don't treat it as a reloc section. BFD can't adequately
1761 represent such a section, so at least for now, we don't
1762 try. We just present it as a normal section. We also
1763 can't use it as a reloc section if it points to the null
1765 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1766 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1768 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1770 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1771 if (target_sect
== NULL
)
1774 if ((target_sect
->flags
& SEC_RELOC
) == 0
1775 || target_sect
->reloc_count
== 0)
1776 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1780 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1781 amt
= sizeof (*hdr2
);
1782 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1783 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1786 elf_elfsections (abfd
)[shindex
] = hdr2
;
1787 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1788 target_sect
->flags
|= SEC_RELOC
;
1789 target_sect
->relocation
= NULL
;
1790 target_sect
->rel_filepos
= hdr
->sh_offset
;
1791 /* In the section to which the relocations apply, mark whether
1792 its relocations are of the REL or RELA variety. */
1793 if (hdr
->sh_size
!= 0)
1794 elf_section_data (target_sect
)->use_rela_p
1795 = (hdr
->sh_type
== SHT_RELA
);
1796 abfd
->flags
|= HAS_RELOC
;
1801 case SHT_GNU_verdef
:
1802 elf_dynverdef (abfd
) = shindex
;
1803 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1804 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1807 case SHT_GNU_versym
:
1808 elf_dynversym (abfd
) = shindex
;
1809 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1810 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1813 case SHT_GNU_verneed
:
1814 elf_dynverref (abfd
) = shindex
;
1815 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1816 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1823 /* Make a section for objcopy and relocatable links. */
1824 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1826 if (hdr
->contents
!= NULL
)
1828 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1829 unsigned int n_elt
= hdr
->sh_size
/ 4;
1832 while (--n_elt
!= 0)
1833 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1834 && elf_next_in_group (s
) != NULL
)
1836 elf_next_in_group (hdr
->bfd_section
) = s
;
1843 /* Check for any processor-specific section types. */
1845 if (bed
->elf_backend_section_from_shdr
)
1846 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1854 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1855 Return SEC for sections that have no elf section, and NULL on error. */
1858 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
1860 struct sym_sec_cache
*cache
;
1862 unsigned long r_symndx
;
1864 unsigned char esym_shndx
[4];
1865 unsigned int isym_shndx
;
1866 Elf_Internal_Shdr
*symtab_hdr
;
1869 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1871 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1872 return cache
->sec
[ent
];
1874 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1875 pos
= symtab_hdr
->sh_offset
;
1876 if (get_elf_backend_data (abfd
)->s
->sizeof_sym
1877 == sizeof (Elf64_External_Sym
))
1879 pos
+= r_symndx
* sizeof (Elf64_External_Sym
);
1880 pos
+= offsetof (Elf64_External_Sym
, st_shndx
);
1881 amt
= sizeof (((Elf64_External_Sym
*) 0)->st_shndx
);
1885 pos
+= r_symndx
* sizeof (Elf32_External_Sym
);
1886 pos
+= offsetof (Elf32_External_Sym
, st_shndx
);
1887 amt
= sizeof (((Elf32_External_Sym
*) 0)->st_shndx
);
1889 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1890 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1892 isym_shndx
= H_GET_16 (abfd
, esym_shndx
);
1894 if (isym_shndx
== SHN_XINDEX
)
1896 Elf_Internal_Shdr
*shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1897 if (shndx_hdr
->sh_size
!= 0)
1899 pos
= shndx_hdr
->sh_offset
;
1900 pos
+= r_symndx
* sizeof (Elf_External_Sym_Shndx
);
1901 amt
= sizeof (Elf_External_Sym_Shndx
);
1902 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1903 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1905 isym_shndx
= H_GET_32 (abfd
, esym_shndx
);
1909 if (cache
->abfd
!= abfd
)
1911 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1914 cache
->indx
[ent
] = r_symndx
;
1915 cache
->sec
[ent
] = sec
;
1916 if (isym_shndx
< SHN_LORESERVE
|| isym_shndx
> SHN_HIRESERVE
)
1919 s
= bfd_section_from_elf_index (abfd
, isym_shndx
);
1921 cache
->sec
[ent
] = s
;
1923 return cache
->sec
[ent
];
1926 /* Given an ELF section number, retrieve the corresponding BFD
1930 bfd_section_from_elf_index (abfd
, index
)
1934 if (index
>= elf_numsections (abfd
))
1936 return elf_elfsections (abfd
)[index
]->bfd_section
;
1940 _bfd_elf_new_section_hook (abfd
, sec
)
1944 struct bfd_elf_section_data
*sdata
;
1945 bfd_size_type amt
= sizeof (*sdata
);
1947 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1950 sec
->used_by_bfd
= (PTR
) sdata
;
1952 /* Indicate whether or not this section should use RELA relocations. */
1954 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1959 /* Create a new bfd section from an ELF program header.
1961 Since program segments have no names, we generate a synthetic name
1962 of the form segment<NUM>, where NUM is generally the index in the
1963 program header table. For segments that are split (see below) we
1964 generate the names segment<NUM>a and segment<NUM>b.
1966 Note that some program segments may have a file size that is different than
1967 (less than) the memory size. All this means is that at execution the
1968 system must allocate the amount of memory specified by the memory size,
1969 but only initialize it with the first "file size" bytes read from the
1970 file. This would occur for example, with program segments consisting
1971 of combined data+bss.
1973 To handle the above situation, this routine generates TWO bfd sections
1974 for the single program segment. The first has the length specified by
1975 the file size of the segment, and the second has the length specified
1976 by the difference between the two sizes. In effect, the segment is split
1977 into it's initialized and uninitialized parts.
1982 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1984 Elf_Internal_Phdr
*hdr
;
1986 const char *typename
;
1993 split
= ((hdr
->p_memsz
> 0)
1994 && (hdr
->p_filesz
> 0)
1995 && (hdr
->p_memsz
> hdr
->p_filesz
));
1996 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1997 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2000 strcpy (name
, namebuf
);
2001 newsect
= bfd_make_section (abfd
, name
);
2002 if (newsect
== NULL
)
2004 newsect
->vma
= hdr
->p_vaddr
;
2005 newsect
->lma
= hdr
->p_paddr
;
2006 newsect
->_raw_size
= hdr
->p_filesz
;
2007 newsect
->filepos
= hdr
->p_offset
;
2008 newsect
->flags
|= SEC_HAS_CONTENTS
;
2009 if (hdr
->p_type
== PT_LOAD
)
2011 newsect
->flags
|= SEC_ALLOC
;
2012 newsect
->flags
|= SEC_LOAD
;
2013 if (hdr
->p_flags
& PF_X
)
2015 /* FIXME: all we known is that it has execute PERMISSION,
2017 newsect
->flags
|= SEC_CODE
;
2020 if (!(hdr
->p_flags
& PF_W
))
2022 newsect
->flags
|= SEC_READONLY
;
2027 sprintf (namebuf
, "%s%db", typename
, index
);
2028 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
2031 strcpy (name
, namebuf
);
2032 newsect
= bfd_make_section (abfd
, name
);
2033 if (newsect
== NULL
)
2035 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
2036 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
2037 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
2038 if (hdr
->p_type
== PT_LOAD
)
2040 newsect
->flags
|= SEC_ALLOC
;
2041 if (hdr
->p_flags
& PF_X
)
2042 newsect
->flags
|= SEC_CODE
;
2044 if (!(hdr
->p_flags
& PF_W
))
2045 newsect
->flags
|= SEC_READONLY
;
2052 bfd_section_from_phdr (abfd
, hdr
, index
)
2054 Elf_Internal_Phdr
*hdr
;
2057 struct elf_backend_data
*bed
;
2059 switch (hdr
->p_type
)
2062 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2065 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2068 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2071 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2074 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2076 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2081 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2084 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2087 /* Check for any processor-specific program segment types.
2088 If no handler for them, default to making "segment" sections. */
2089 bed
= get_elf_backend_data (abfd
);
2090 if (bed
->elf_backend_section_from_phdr
)
2091 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2093 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2097 /* Initialize REL_HDR, the section-header for new section, containing
2098 relocations against ASECT. If USE_RELA_P is true, we use RELA
2099 relocations; otherwise, we use REL relocations. */
2102 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2104 Elf_Internal_Shdr
*rel_hdr
;
2109 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2110 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2112 name
= bfd_alloc (abfd
, amt
);
2115 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2117 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2119 if (rel_hdr
->sh_name
== (unsigned int) -1)
2121 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2122 rel_hdr
->sh_entsize
= (use_rela_p
2123 ? bed
->s
->sizeof_rela
2124 : bed
->s
->sizeof_rel
);
2125 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2126 rel_hdr
->sh_flags
= 0;
2127 rel_hdr
->sh_addr
= 0;
2128 rel_hdr
->sh_size
= 0;
2129 rel_hdr
->sh_offset
= 0;
2134 /* Set up an ELF internal section header for a section. */
2137 elf_fake_sections (abfd
, asect
, failedptrarg
)
2142 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2143 boolean
*failedptr
= (boolean
*) failedptrarg
;
2144 Elf_Internal_Shdr
*this_hdr
;
2148 /* We already failed; just get out of the bfd_map_over_sections
2153 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2155 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2156 asect
->name
, false);
2157 if (this_hdr
->sh_name
== (unsigned long) -1)
2163 this_hdr
->sh_flags
= 0;
2165 if ((asect
->flags
& SEC_ALLOC
) != 0
2166 || asect
->user_set_vma
)
2167 this_hdr
->sh_addr
= asect
->vma
;
2169 this_hdr
->sh_addr
= 0;
2171 this_hdr
->sh_offset
= 0;
2172 this_hdr
->sh_size
= asect
->_raw_size
;
2173 this_hdr
->sh_link
= 0;
2174 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2175 /* The sh_entsize and sh_info fields may have been set already by
2176 copy_private_section_data. */
2178 this_hdr
->bfd_section
= asect
;
2179 this_hdr
->contents
= NULL
;
2181 /* FIXME: This should not be based on section names. */
2182 if (strcmp (asect
->name
, ".dynstr") == 0)
2183 this_hdr
->sh_type
= SHT_STRTAB
;
2184 else if (strcmp (asect
->name
, ".hash") == 0)
2186 this_hdr
->sh_type
= SHT_HASH
;
2187 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2189 else if (strcmp (asect
->name
, ".dynsym") == 0)
2191 this_hdr
->sh_type
= SHT_DYNSYM
;
2192 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2194 else if (strcmp (asect
->name
, ".dynamic") == 0)
2196 this_hdr
->sh_type
= SHT_DYNAMIC
;
2197 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2199 else if (strncmp (asect
->name
, ".rela", 5) == 0
2200 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2202 this_hdr
->sh_type
= SHT_RELA
;
2203 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2205 else if (strncmp (asect
->name
, ".rel", 4) == 0
2206 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2208 this_hdr
->sh_type
= SHT_REL
;
2209 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2211 else if (strcmp (asect
->name
, ".init_array") == 0)
2212 this_hdr
->sh_type
= SHT_INIT_ARRAY
;
2213 else if (strcmp (asect
->name
, ".fini_array") == 0)
2214 this_hdr
->sh_type
= SHT_FINI_ARRAY
;
2215 else if (strcmp (asect
->name
, ".preinit_array") == 0)
2216 this_hdr
->sh_type
= SHT_PREINIT_ARRAY
;
2217 else if (strncmp (asect
->name
, ".note", 5) == 0)
2218 this_hdr
->sh_type
= SHT_NOTE
;
2219 else if (strncmp (asect
->name
, ".stab", 5) == 0
2220 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2221 this_hdr
->sh_type
= SHT_STRTAB
;
2222 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2224 this_hdr
->sh_type
= SHT_GNU_versym
;
2225 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2227 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2229 this_hdr
->sh_type
= SHT_GNU_verdef
;
2230 this_hdr
->sh_entsize
= 0;
2231 /* objcopy or strip will copy over sh_info, but may not set
2232 cverdefs. The linker will set cverdefs, but sh_info will be
2234 if (this_hdr
->sh_info
== 0)
2235 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2237 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2238 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2240 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2242 this_hdr
->sh_type
= SHT_GNU_verneed
;
2243 this_hdr
->sh_entsize
= 0;
2244 /* objcopy or strip will copy over sh_info, but may not set
2245 cverrefs. The linker will set cverrefs, but sh_info will be
2247 if (this_hdr
->sh_info
== 0)
2248 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2250 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2251 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2253 else if ((asect
->flags
& SEC_GROUP
) != 0)
2255 this_hdr
->sh_type
= SHT_GROUP
;
2256 this_hdr
->sh_entsize
= 4;
2258 else if ((asect
->flags
& SEC_ALLOC
) != 0
2259 && (((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0)
2260 || (asect
->flags
& SEC_NEVER_LOAD
) != 0))
2261 this_hdr
->sh_type
= SHT_NOBITS
;
2263 this_hdr
->sh_type
= SHT_PROGBITS
;
2265 if ((asect
->flags
& SEC_ALLOC
) != 0)
2266 this_hdr
->sh_flags
|= SHF_ALLOC
;
2267 if ((asect
->flags
& SEC_READONLY
) == 0)
2268 this_hdr
->sh_flags
|= SHF_WRITE
;
2269 if ((asect
->flags
& SEC_CODE
) != 0)
2270 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2271 if ((asect
->flags
& SEC_MERGE
) != 0)
2273 this_hdr
->sh_flags
|= SHF_MERGE
;
2274 this_hdr
->sh_entsize
= asect
->entsize
;
2275 if ((asect
->flags
& SEC_STRINGS
) != 0)
2276 this_hdr
->sh_flags
|= SHF_STRINGS
;
2278 if (elf_group_name (asect
) != NULL
)
2279 this_hdr
->sh_flags
|= SHF_GROUP
;
2280 if ((asect
->flags
& SEC_THREAD_LOCAL
) != 0)
2281 this_hdr
->sh_flags
|= SHF_TLS
;
2283 /* Check for processor-specific section types. */
2284 if (bed
->elf_backend_fake_sections
2285 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2288 /* If the section has relocs, set up a section header for the
2289 SHT_REL[A] section. If two relocation sections are required for
2290 this section, it is up to the processor-specific back-end to
2291 create the other. */
2292 if ((asect
->flags
& SEC_RELOC
) != 0
2293 && !_bfd_elf_init_reloc_shdr (abfd
,
2294 &elf_section_data (asect
)->rel_hdr
,
2296 elf_section_data (asect
)->use_rela_p
))
2300 /* Fill in the contents of a SHT_GROUP section. */
2303 set_group_contents (abfd
, sec
, failedptrarg
)
2306 PTR failedptrarg ATTRIBUTE_UNUSED
;
2308 boolean
*failedptr
= (boolean
*) failedptrarg
;
2309 unsigned long symindx
;
2312 struct bfd_link_order
*l
;
2314 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2318 /* If called from the assembler, swap_out_syms will have set up
2319 elf_section_syms; If called for "ld -r", the symbols won't yet
2320 be mapped, so emulate elf_bfd_final_link. */
2321 if (elf_section_syms (abfd
) != NULL
)
2322 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2324 symindx
= elf_section_data (sec
)->this_idx
;
2325 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2327 /* Nor will the contents be allocated for "ld -r". */
2328 if (sec
->contents
== NULL
)
2330 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2331 if (sec
->contents
== NULL
)
2338 loc
= sec
->contents
+ sec
->_raw_size
;
2340 /* Get the pointer to the first section in the group that we
2341 squirreled away here. */
2342 elt
= elf_next_in_group (sec
);
2344 /* First element is a flag word. Rest of section is elf section
2345 indices for all the sections of the group. Write them backwards
2346 just to keep the group in the same order as given in .section
2347 directives, not that it matters. */
2351 H_PUT_32 (abfd
, elf_section_data (elt
)->this_idx
, loc
);
2352 elt
= elf_next_in_group (elt
);
2355 /* If this is a relocatable link, then the above did nothing because
2356 SEC is the output section. Look through the input sections
2358 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2359 if (l
->type
== bfd_indirect_link_order
2360 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2365 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2366 elt
= elf_next_in_group (elt
);
2367 /* During a relocatable link, the lists are circular. */
2369 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2372 H_PUT_32 (abfd
, 0, loc
);
2374 BFD_ASSERT (loc
== sec
->contents
);
2377 /* Assign all ELF section numbers. The dummy first section is handled here
2378 too. The link/info pointers for the standard section types are filled
2379 in here too, while we're at it. */
2382 assign_section_numbers (abfd
)
2385 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2387 unsigned int section_number
, secn
;
2388 Elf_Internal_Shdr
**i_shdrp
;
2393 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2395 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2397 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2399 if (section_number
== SHN_LORESERVE
)
2400 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2401 d
->this_idx
= section_number
++;
2402 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2403 if ((sec
->flags
& SEC_RELOC
) == 0)
2407 if (section_number
== SHN_LORESERVE
)
2408 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2409 d
->rel_idx
= section_number
++;
2410 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2415 if (section_number
== SHN_LORESERVE
)
2416 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2417 d
->rel_idx2
= section_number
++;
2418 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2424 if (section_number
== SHN_LORESERVE
)
2425 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2426 t
->shstrtab_section
= section_number
++;
2427 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2428 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2430 if (bfd_get_symcount (abfd
) > 0)
2432 if (section_number
== SHN_LORESERVE
)
2433 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2434 t
->symtab_section
= section_number
++;
2435 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2436 if (section_number
> SHN_LORESERVE
- 2)
2438 if (section_number
== SHN_LORESERVE
)
2439 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2440 t
->symtab_shndx_section
= section_number
++;
2441 t
->symtab_shndx_hdr
.sh_name
2442 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2443 ".symtab_shndx", false);
2444 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2447 if (section_number
== SHN_LORESERVE
)
2448 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2449 t
->strtab_section
= section_number
++;
2450 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2453 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2454 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2456 elf_numsections (abfd
) = section_number
;
2457 elf_elfheader (abfd
)->e_shnum
= section_number
;
2458 if (section_number
> SHN_LORESERVE
)
2459 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2461 /* Set up the list of section header pointers, in agreement with the
2463 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2464 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2465 if (i_shdrp
== NULL
)
2468 amt
= sizeof (Elf_Internal_Shdr
);
2469 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2470 if (i_shdrp
[0] == NULL
)
2472 bfd_release (abfd
, i_shdrp
);
2475 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2477 elf_elfsections (abfd
) = i_shdrp
;
2479 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2480 if (bfd_get_symcount (abfd
) > 0)
2482 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2483 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2485 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2486 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2488 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2489 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2491 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2493 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2497 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2498 if (d
->rel_idx
!= 0)
2499 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2500 if (d
->rel_idx2
!= 0)
2501 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2503 /* Fill in the sh_link and sh_info fields while we're at it. */
2505 /* sh_link of a reloc section is the section index of the symbol
2506 table. sh_info is the section index of the section to which
2507 the relocation entries apply. */
2508 if (d
->rel_idx
!= 0)
2510 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2511 d
->rel_hdr
.sh_info
= d
->this_idx
;
2513 if (d
->rel_idx2
!= 0)
2515 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2516 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2519 switch (d
->this_hdr
.sh_type
)
2523 /* A reloc section which we are treating as a normal BFD
2524 section. sh_link is the section index of the symbol
2525 table. sh_info is the section index of the section to
2526 which the relocation entries apply. We assume that an
2527 allocated reloc section uses the dynamic symbol table.
2528 FIXME: How can we be sure? */
2529 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2531 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2533 /* We look up the section the relocs apply to by name. */
2535 if (d
->this_hdr
.sh_type
== SHT_REL
)
2539 s
= bfd_get_section_by_name (abfd
, name
);
2541 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2545 /* We assume that a section named .stab*str is a stabs
2546 string section. We look for a section with the same name
2547 but without the trailing ``str'', and set its sh_link
2548 field to point to this section. */
2549 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2550 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2555 len
= strlen (sec
->name
);
2556 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2559 strncpy (alc
, sec
->name
, len
- 3);
2560 alc
[len
- 3] = '\0';
2561 s
= bfd_get_section_by_name (abfd
, alc
);
2565 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2567 /* This is a .stab section. */
2568 elf_section_data (s
)->this_hdr
.sh_entsize
=
2569 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2576 case SHT_GNU_verneed
:
2577 case SHT_GNU_verdef
:
2578 /* sh_link is the section header index of the string table
2579 used for the dynamic entries, or the symbol table, or the
2581 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2583 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2587 case SHT_GNU_versym
:
2588 /* sh_link is the section header index of the symbol table
2589 this hash table or version table is for. */
2590 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2592 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2596 d
->this_hdr
.sh_link
= t
->symtab_section
;
2600 for (secn
= 1; secn
< section_number
; ++secn
)
2601 if (i_shdrp
[secn
] == NULL
)
2602 i_shdrp
[secn
] = i_shdrp
[0];
2604 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2605 i_shdrp
[secn
]->sh_name
);
2609 /* Map symbol from it's internal number to the external number, moving
2610 all local symbols to be at the head of the list. */
2613 sym_is_global (abfd
, sym
)
2617 /* If the backend has a special mapping, use it. */
2618 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2619 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2622 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2623 || bfd_is_und_section (bfd_get_section (sym
))
2624 || bfd_is_com_section (bfd_get_section (sym
)));
2628 elf_map_symbols (abfd
)
2631 unsigned int symcount
= bfd_get_symcount (abfd
);
2632 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2633 asymbol
**sect_syms
;
2634 unsigned int num_locals
= 0;
2635 unsigned int num_globals
= 0;
2636 unsigned int num_locals2
= 0;
2637 unsigned int num_globals2
= 0;
2645 fprintf (stderr
, "elf_map_symbols\n");
2649 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2651 if (max_index
< asect
->index
)
2652 max_index
= asect
->index
;
2656 amt
= max_index
* sizeof (asymbol
*);
2657 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2658 if (sect_syms
== NULL
)
2660 elf_section_syms (abfd
) = sect_syms
;
2661 elf_num_section_syms (abfd
) = max_index
;
2663 /* Init sect_syms entries for any section symbols we have already
2664 decided to output. */
2665 for (idx
= 0; idx
< symcount
; idx
++)
2667 asymbol
*sym
= syms
[idx
];
2669 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2676 if (sec
->owner
!= NULL
)
2678 if (sec
->owner
!= abfd
)
2680 if (sec
->output_offset
!= 0)
2683 sec
= sec
->output_section
;
2685 /* Empty sections in the input files may have had a
2686 section symbol created for them. (See the comment
2687 near the end of _bfd_generic_link_output_symbols in
2688 linker.c). If the linker script discards such
2689 sections then we will reach this point. Since we know
2690 that we cannot avoid this case, we detect it and skip
2691 the abort and the assignment to the sect_syms array.
2692 To reproduce this particular case try running the
2693 linker testsuite test ld-scripts/weak.exp for an ELF
2694 port that uses the generic linker. */
2695 if (sec
->owner
== NULL
)
2698 BFD_ASSERT (sec
->owner
== abfd
);
2700 sect_syms
[sec
->index
] = syms
[idx
];
2705 /* Classify all of the symbols. */
2706 for (idx
= 0; idx
< symcount
; idx
++)
2708 if (!sym_is_global (abfd
, syms
[idx
]))
2714 /* We will be adding a section symbol for each BFD section. Most normal
2715 sections will already have a section symbol in outsymbols, but
2716 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2717 at least in that case. */
2718 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2720 if (sect_syms
[asect
->index
] == NULL
)
2722 if (!sym_is_global (abfd
, asect
->symbol
))
2729 /* Now sort the symbols so the local symbols are first. */
2730 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2731 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2733 if (new_syms
== NULL
)
2736 for (idx
= 0; idx
< symcount
; idx
++)
2738 asymbol
*sym
= syms
[idx
];
2741 if (!sym_is_global (abfd
, sym
))
2744 i
= num_locals
+ num_globals2
++;
2746 sym
->udata
.i
= i
+ 1;
2748 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2750 if (sect_syms
[asect
->index
] == NULL
)
2752 asymbol
*sym
= asect
->symbol
;
2755 sect_syms
[asect
->index
] = sym
;
2756 if (!sym_is_global (abfd
, sym
))
2759 i
= num_locals
+ num_globals2
++;
2761 sym
->udata
.i
= i
+ 1;
2765 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2767 elf_num_locals (abfd
) = num_locals
;
2768 elf_num_globals (abfd
) = num_globals
;
2772 /* Align to the maximum file alignment that could be required for any
2773 ELF data structure. */
2775 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2776 static INLINE file_ptr
2777 align_file_position (off
, align
)
2781 return (off
+ align
- 1) & ~(align
- 1);
2784 /* Assign a file position to a section, optionally aligning to the
2785 required section alignment. */
2788 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2789 Elf_Internal_Shdr
*i_shdrp
;
2797 al
= i_shdrp
->sh_addralign
;
2799 offset
= BFD_ALIGN (offset
, al
);
2801 i_shdrp
->sh_offset
= offset
;
2802 if (i_shdrp
->bfd_section
!= NULL
)
2803 i_shdrp
->bfd_section
->filepos
= offset
;
2804 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2805 offset
+= i_shdrp
->sh_size
;
2809 /* Compute the file positions we are going to put the sections at, and
2810 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2811 is not NULL, this is being called by the ELF backend linker. */
2814 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2816 struct bfd_link_info
*link_info
;
2818 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2820 struct bfd_strtab_hash
*strtab
;
2821 Elf_Internal_Shdr
*shstrtab_hdr
;
2823 if (abfd
->output_has_begun
)
2826 /* Do any elf backend specific processing first. */
2827 if (bed
->elf_backend_begin_write_processing
)
2828 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2830 if (! prep_headers (abfd
))
2833 /* Post process the headers if necessary. */
2834 if (bed
->elf_backend_post_process_headers
)
2835 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2838 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2842 if (!assign_section_numbers (abfd
))
2845 /* The backend linker builds symbol table information itself. */
2846 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2848 /* Non-zero if doing a relocatable link. */
2849 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2851 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2855 if (link_info
== NULL
|| link_info
->relocateable
)
2857 bfd_map_over_sections (abfd
, set_group_contents
, &failed
);
2862 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2863 /* sh_name was set in prep_headers. */
2864 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2865 shstrtab_hdr
->sh_flags
= 0;
2866 shstrtab_hdr
->sh_addr
= 0;
2867 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2868 shstrtab_hdr
->sh_entsize
= 0;
2869 shstrtab_hdr
->sh_link
= 0;
2870 shstrtab_hdr
->sh_info
= 0;
2871 /* sh_offset is set in assign_file_positions_except_relocs. */
2872 shstrtab_hdr
->sh_addralign
= 1;
2874 if (!assign_file_positions_except_relocs (abfd
))
2877 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2880 Elf_Internal_Shdr
*hdr
;
2882 off
= elf_tdata (abfd
)->next_file_pos
;
2884 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2885 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2887 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2888 if (hdr
->sh_size
!= 0)
2889 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2891 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2892 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2894 elf_tdata (abfd
)->next_file_pos
= off
;
2896 /* Now that we know where the .strtab section goes, write it
2898 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2899 || ! _bfd_stringtab_emit (abfd
, strtab
))
2901 _bfd_stringtab_free (strtab
);
2904 abfd
->output_has_begun
= true;
2909 /* Create a mapping from a set of sections to a program segment. */
2911 static INLINE
struct elf_segment_map
*
2912 make_mapping (abfd
, sections
, from
, to
, phdr
)
2914 asection
**sections
;
2919 struct elf_segment_map
*m
;
2924 amt
= sizeof (struct elf_segment_map
);
2925 amt
+= (to
- from
- 1) * sizeof (asection
*);
2926 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2930 m
->p_type
= PT_LOAD
;
2931 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2932 m
->sections
[i
- from
] = *hdrpp
;
2933 m
->count
= to
- from
;
2935 if (from
== 0 && phdr
)
2937 /* Include the headers in the first PT_LOAD segment. */
2938 m
->includes_filehdr
= 1;
2939 m
->includes_phdrs
= 1;
2945 /* Set up a mapping from BFD sections to program segments. */
2948 map_sections_to_segments (abfd
)
2951 asection
**sections
= NULL
;
2955 struct elf_segment_map
*mfirst
;
2956 struct elf_segment_map
**pm
;
2957 struct elf_segment_map
*m
;
2959 unsigned int phdr_index
;
2960 bfd_vma maxpagesize
;
2962 boolean phdr_in_segment
= true;
2965 asection
*first_tls
= NULL
;
2966 asection
*dynsec
, *eh_frame_hdr
;
2969 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2972 if (bfd_count_sections (abfd
) == 0)
2975 /* Select the allocated sections, and sort them. */
2977 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
2978 sections
= (asection
**) bfd_malloc (amt
);
2979 if (sections
== NULL
)
2983 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2985 if ((s
->flags
& SEC_ALLOC
) != 0)
2991 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2994 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2996 /* Build the mapping. */
3001 /* If we have a .interp section, then create a PT_PHDR segment for
3002 the program headers and a PT_INTERP segment for the .interp
3004 s
= bfd_get_section_by_name (abfd
, ".interp");
3005 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3007 amt
= sizeof (struct elf_segment_map
);
3008 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3012 m
->p_type
= PT_PHDR
;
3013 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3014 m
->p_flags
= PF_R
| PF_X
;
3015 m
->p_flags_valid
= 1;
3016 m
->includes_phdrs
= 1;
3021 amt
= sizeof (struct elf_segment_map
);
3022 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3026 m
->p_type
= PT_INTERP
;
3034 /* Look through the sections. We put sections in the same program
3035 segment when the start of the second section can be placed within
3036 a few bytes of the end of the first section. */
3039 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
3041 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
3043 && (dynsec
->flags
& SEC_LOAD
) == 0)
3046 /* Deal with -Ttext or something similar such that the first section
3047 is not adjacent to the program headers. This is an
3048 approximation, since at this point we don't know exactly how many
3049 program headers we will need. */
3052 bfd_size_type phdr_size
;
3054 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3056 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3057 if ((abfd
->flags
& D_PAGED
) == 0
3058 || sections
[0]->lma
< phdr_size
3059 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3060 phdr_in_segment
= false;
3063 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3066 boolean new_segment
;
3070 /* See if this section and the last one will fit in the same
3073 if (last_hdr
== NULL
)
3075 /* If we don't have a segment yet, then we don't need a new
3076 one (we build the last one after this loop). */
3077 new_segment
= false;
3079 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3081 /* If this section has a different relation between the
3082 virtual address and the load address, then we need a new
3086 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3087 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3089 /* If putting this section in this segment would force us to
3090 skip a page in the segment, then we need a new segment. */
3093 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3094 && (hdr
->flags
& SEC_LOAD
) != 0)
3096 /* We don't want to put a loadable section after a
3097 nonloadable section in the same segment. */
3100 else if ((abfd
->flags
& D_PAGED
) == 0)
3102 /* If the file is not demand paged, which means that we
3103 don't require the sections to be correctly aligned in the
3104 file, then there is no other reason for a new segment. */
3105 new_segment
= false;
3108 && (hdr
->flags
& SEC_READONLY
) == 0
3109 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3112 /* We don't want to put a writable section in a read only
3113 segment, unless they are on the same page in memory
3114 anyhow. We already know that the last section does not
3115 bring us past the current section on the page, so the
3116 only case in which the new section is not on the same
3117 page as the previous section is when the previous section
3118 ends precisely on a page boundary. */
3123 /* Otherwise, we can use the same segment. */
3124 new_segment
= false;
3129 if ((hdr
->flags
& SEC_READONLY
) == 0)
3135 /* We need a new program segment. We must create a new program
3136 header holding all the sections from phdr_index until hdr. */
3138 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3145 if ((hdr
->flags
& SEC_READONLY
) == 0)
3152 phdr_in_segment
= false;
3155 /* Create a final PT_LOAD program segment. */
3156 if (last_hdr
!= NULL
)
3158 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3166 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3169 amt
= sizeof (struct elf_segment_map
);
3170 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3174 m
->p_type
= PT_DYNAMIC
;
3176 m
->sections
[0] = dynsec
;
3182 /* For each loadable .note section, add a PT_NOTE segment. We don't
3183 use bfd_get_section_by_name, because if we link together
3184 nonloadable .note sections and loadable .note sections, we will
3185 generate two .note sections in the output file. FIXME: Using
3186 names for section types is bogus anyhow. */
3187 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3189 if ((s
->flags
& SEC_LOAD
) != 0
3190 && strncmp (s
->name
, ".note", 5) == 0)
3192 amt
= sizeof (struct elf_segment_map
);
3193 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3197 m
->p_type
= PT_NOTE
;
3204 if (s
->flags
& SEC_THREAD_LOCAL
)
3212 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3217 amt
= sizeof (struct elf_segment_map
);
3218 amt
+= (tls_count
- 1) * sizeof (asection
*);
3219 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3224 m
->count
= tls_count
;
3225 /* Mandated PF_R. */
3227 m
->p_flags_valid
= 1;
3228 for (i
= 0; i
< tls_count
; ++i
)
3230 BFD_ASSERT (first_tls
->flags
& SEC_THREAD_LOCAL
);
3231 m
->sections
[i
] = first_tls
;
3232 first_tls
= first_tls
->next
;
3239 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3241 eh_frame_hdr
= NULL
;
3242 if (elf_tdata (abfd
)->eh_frame_hdr
)
3243 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3244 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3246 amt
= sizeof (struct elf_segment_map
);
3247 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3251 m
->p_type
= PT_GNU_EH_FRAME
;
3253 m
->sections
[0] = eh_frame_hdr
;
3262 elf_tdata (abfd
)->segment_map
= mfirst
;
3266 if (sections
!= NULL
)
3271 /* Sort sections by address. */
3274 elf_sort_sections (arg1
, arg2
)
3278 const asection
*sec1
= *(const asection
**) arg1
;
3279 const asection
*sec2
= *(const asection
**) arg2
;
3281 /* Sort by LMA first, since this is the address used to
3282 place the section into a segment. */
3283 if (sec1
->lma
< sec2
->lma
)
3285 else if (sec1
->lma
> sec2
->lma
)
3288 /* Then sort by VMA. Normally the LMA and the VMA will be
3289 the same, and this will do nothing. */
3290 if (sec1
->vma
< sec2
->vma
)
3292 else if (sec1
->vma
> sec2
->vma
)
3295 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3297 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3303 /* If the indicies are the same, do not return 0
3304 here, but continue to try the next comparison. */
3305 if (sec1
->target_index
- sec2
->target_index
!= 0)
3306 return sec1
->target_index
- sec2
->target_index
;
3311 else if (TOEND (sec2
))
3316 /* Sort by size, to put zero sized sections
3317 before others at the same address. */
3319 if (sec1
->_raw_size
< sec2
->_raw_size
)
3321 if (sec1
->_raw_size
> sec2
->_raw_size
)
3324 return sec1
->target_index
- sec2
->target_index
;
3327 /* Assign file positions to the sections based on the mapping from
3328 sections to segments. This function also sets up some fields in
3329 the file header, and writes out the program headers. */
3332 assign_file_positions_for_segments (abfd
)
3335 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3337 struct elf_segment_map
*m
;
3339 Elf_Internal_Phdr
*phdrs
;
3341 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3342 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3343 Elf_Internal_Phdr
*p
;
3346 if (elf_tdata (abfd
)->segment_map
== NULL
)
3348 if (! map_sections_to_segments (abfd
))
3352 if (bed
->elf_backend_modify_segment_map
)
3354 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3359 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3362 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3363 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3364 elf_elfheader (abfd
)->e_phnum
= count
;
3369 /* If we already counted the number of program segments, make sure
3370 that we allocated enough space. This happens when SIZEOF_HEADERS
3371 is used in a linker script. */
3372 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3373 if (alloc
!= 0 && count
> alloc
)
3375 ((*_bfd_error_handler
)
3376 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3377 bfd_get_filename (abfd
), alloc
, count
));
3378 bfd_set_error (bfd_error_bad_value
);
3385 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3386 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3390 off
= bed
->s
->sizeof_ehdr
;
3391 off
+= alloc
* bed
->s
->sizeof_phdr
;
3398 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3405 /* If elf_segment_map is not from map_sections_to_segments, the
3406 sections may not be correctly ordered. NOTE: sorting should
3407 not be done to the PT_NOTE section of a corefile, which may
3408 contain several pseudo-sections artificially created by bfd.
3409 Sorting these pseudo-sections breaks things badly. */
3411 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3412 && m
->p_type
== PT_NOTE
))
3413 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3416 p
->p_type
= m
->p_type
;
3417 p
->p_flags
= m
->p_flags
;
3419 if (p
->p_type
== PT_LOAD
3421 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3423 if ((abfd
->flags
& D_PAGED
) != 0)
3424 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3427 bfd_size_type align
;
3430 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3432 bfd_size_type secalign
;
3434 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3435 if (secalign
> align
)
3439 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3446 p
->p_vaddr
= m
->sections
[0]->vma
;
3448 if (m
->p_paddr_valid
)
3449 p
->p_paddr
= m
->p_paddr
;
3450 else if (m
->count
== 0)
3453 p
->p_paddr
= m
->sections
[0]->lma
;
3455 if (p
->p_type
== PT_LOAD
3456 && (abfd
->flags
& D_PAGED
) != 0)
3457 p
->p_align
= bed
->maxpagesize
;
3458 else if (m
->count
== 0)
3459 p
->p_align
= bed
->s
->file_align
;
3467 if (m
->includes_filehdr
)
3469 if (! m
->p_flags_valid
)
3472 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3473 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3476 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3478 if (p
->p_vaddr
< (bfd_vma
) off
)
3480 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3481 bfd_get_filename (abfd
));
3482 bfd_set_error (bfd_error_bad_value
);
3487 if (! m
->p_paddr_valid
)
3490 if (p
->p_type
== PT_LOAD
)
3492 filehdr_vaddr
= p
->p_vaddr
;
3493 filehdr_paddr
= p
->p_paddr
;
3497 if (m
->includes_phdrs
)
3499 if (! m
->p_flags_valid
)
3502 if (m
->includes_filehdr
)
3504 if (p
->p_type
== PT_LOAD
)
3506 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3507 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3512 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3516 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3517 p
->p_vaddr
-= off
- p
->p_offset
;
3518 if (! m
->p_paddr_valid
)
3519 p
->p_paddr
-= off
- p
->p_offset
;
3522 if (p
->p_type
== PT_LOAD
)
3524 phdrs_vaddr
= p
->p_vaddr
;
3525 phdrs_paddr
= p
->p_paddr
;
3528 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3531 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3532 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3535 if (p
->p_type
== PT_LOAD
3536 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3538 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3544 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3545 p
->p_filesz
+= adjust
;
3546 p
->p_memsz
+= adjust
;
3552 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3556 bfd_size_type align
;
3560 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3562 /* The section may have artificial alignment forced by a
3563 link script. Notice this case by the gap between the
3564 cumulative phdr lma and the section's lma. */
3565 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3567 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3569 p
->p_memsz
+= adjust
;
3572 if ((flags
& SEC_LOAD
) != 0)
3573 p
->p_filesz
+= adjust
;
3576 if (p
->p_type
== PT_LOAD
)
3578 bfd_signed_vma adjust
;
3580 if ((flags
& SEC_LOAD
) != 0)
3582 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3586 else if ((flags
& SEC_ALLOC
) != 0)
3588 /* The section VMA must equal the file position
3589 modulo the page size. FIXME: I'm not sure if
3590 this adjustment is really necessary. We used to
3591 not have the SEC_LOAD case just above, and then
3592 this was necessary, but now I'm not sure. */
3593 if ((abfd
->flags
& D_PAGED
) != 0)
3594 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3596 adjust
= (sec
->vma
- voff
) % align
;
3605 (* _bfd_error_handler
) (_("\
3606 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3607 bfd_section_name (abfd
, sec
),
3612 p
->p_memsz
+= adjust
;
3615 if ((flags
& SEC_LOAD
) != 0)
3616 p
->p_filesz
+= adjust
;
3621 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3622 used in a linker script we may have a section with
3623 SEC_LOAD clear but which is supposed to have
3625 if ((flags
& SEC_LOAD
) != 0
3626 || (flags
& SEC_HAS_CONTENTS
) != 0)
3627 off
+= sec
->_raw_size
;
3629 if ((flags
& SEC_ALLOC
) != 0)
3630 voff
+= sec
->_raw_size
;
3633 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3635 /* The actual "note" segment has i == 0.
3636 This is the one that actually contains everything. */
3640 p
->p_filesz
= sec
->_raw_size
;
3641 off
+= sec
->_raw_size
;
3646 /* Fake sections -- don't need to be written. */
3649 flags
= sec
->flags
= 0;
3656 p
->p_memsz
+= sec
->_raw_size
;
3658 if ((flags
& SEC_LOAD
) != 0)
3659 p
->p_filesz
+= sec
->_raw_size
;
3661 if (p
->p_type
== PT_TLS
3662 && sec
->_raw_size
== 0
3663 && (sec
->flags
& SEC_HAS_CONTENTS
) == 0)
3665 struct bfd_link_order
*o
;
3666 bfd_vma tbss_size
= 0;
3668 for (o
= sec
->link_order_head
; o
!= NULL
; o
= o
->next
)
3669 if (tbss_size
< o
->offset
+ o
->size
)
3670 tbss_size
= o
->offset
+ o
->size
;
3672 p
->p_memsz
+= tbss_size
;
3675 if (align
> p
->p_align
3676 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3680 if (! m
->p_flags_valid
)
3683 if ((flags
& SEC_CODE
) != 0)
3685 if ((flags
& SEC_READONLY
) == 0)
3691 /* Now that we have set the section file positions, we can set up
3692 the file positions for the non PT_LOAD segments. */
3693 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3697 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3699 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3700 p
->p_offset
= m
->sections
[0]->filepos
;
3704 if (m
->includes_filehdr
)
3706 p
->p_vaddr
= filehdr_vaddr
;
3707 if (! m
->p_paddr_valid
)
3708 p
->p_paddr
= filehdr_paddr
;
3710 else if (m
->includes_phdrs
)
3712 p
->p_vaddr
= phdrs_vaddr
;
3713 if (! m
->p_paddr_valid
)
3714 p
->p_paddr
= phdrs_paddr
;
3719 /* Clear out any program headers we allocated but did not use. */
3720 for (; count
< alloc
; count
++, p
++)
3722 memset (p
, 0, sizeof *p
);
3723 p
->p_type
= PT_NULL
;
3726 elf_tdata (abfd
)->phdr
= phdrs
;
3728 elf_tdata (abfd
)->next_file_pos
= off
;
3730 /* Write out the program headers. */
3731 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3732 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3738 /* Get the size of the program header.
3740 If this is called by the linker before any of the section VMA's are set, it
3741 can't calculate the correct value for a strange memory layout. This only
3742 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3743 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3744 data segment (exclusive of .interp and .dynamic).
3746 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3747 will be two segments. */
3749 static bfd_size_type
3750 get_program_header_size (abfd
)
3755 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3757 /* We can't return a different result each time we're called. */
3758 if (elf_tdata (abfd
)->program_header_size
!= 0)
3759 return elf_tdata (abfd
)->program_header_size
;
3761 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3763 struct elf_segment_map
*m
;
3766 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3768 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3769 return elf_tdata (abfd
)->program_header_size
;
3772 /* Assume we will need exactly two PT_LOAD segments: one for text
3773 and one for data. */
3776 s
= bfd_get_section_by_name (abfd
, ".interp");
3777 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3779 /* If we have a loadable interpreter section, we need a
3780 PT_INTERP segment. In this case, assume we also need a
3781 PT_PHDR segment, although that may not be true for all
3786 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3788 /* We need a PT_DYNAMIC segment. */
3792 if (elf_tdata (abfd
)->eh_frame_hdr
3793 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
3795 /* We need a PT_GNU_EH_FRAME segment. */
3799 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3801 if ((s
->flags
& SEC_LOAD
) != 0
3802 && strncmp (s
->name
, ".note", 5) == 0)
3804 /* We need a PT_NOTE segment. */
3809 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3811 if (s
->flags
& SEC_THREAD_LOCAL
)
3813 /* We need a PT_TLS segment. */
3819 /* Let the backend count up any program headers it might need. */
3820 if (bed
->elf_backend_additional_program_headers
)
3824 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3830 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3831 return elf_tdata (abfd
)->program_header_size
;
3834 /* Work out the file positions of all the sections. This is called by
3835 _bfd_elf_compute_section_file_positions. All the section sizes and
3836 VMAs must be known before this is called.
3838 We do not consider reloc sections at this point, unless they form
3839 part of the loadable image. Reloc sections are assigned file
3840 positions in assign_file_positions_for_relocs, which is called by
3841 write_object_contents and final_link.
3843 We also don't set the positions of the .symtab and .strtab here. */
3846 assign_file_positions_except_relocs (abfd
)
3849 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3850 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3851 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3852 unsigned int num_sec
= elf_numsections (abfd
);
3854 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3856 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3857 && bfd_get_format (abfd
) != bfd_core
)
3859 Elf_Internal_Shdr
**hdrpp
;
3862 /* Start after the ELF header. */
3863 off
= i_ehdrp
->e_ehsize
;
3865 /* We are not creating an executable, which means that we are
3866 not creating a program header, and that the actual order of
3867 the sections in the file is unimportant. */
3868 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3870 Elf_Internal_Shdr
*hdr
;
3873 if (hdr
->sh_type
== SHT_REL
3874 || hdr
->sh_type
== SHT_RELA
3875 || i
== tdata
->symtab_section
3876 || i
== tdata
->symtab_shndx_section
3877 || i
== tdata
->strtab_section
)
3879 hdr
->sh_offset
= -1;
3882 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3884 if (i
== SHN_LORESERVE
- 1)
3886 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3887 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3894 Elf_Internal_Shdr
**hdrpp
;
3896 /* Assign file positions for the loaded sections based on the
3897 assignment of sections to segments. */
3898 if (! assign_file_positions_for_segments (abfd
))
3901 /* Assign file positions for the other sections. */
3903 off
= elf_tdata (abfd
)->next_file_pos
;
3904 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3906 Elf_Internal_Shdr
*hdr
;
3909 if (hdr
->bfd_section
!= NULL
3910 && hdr
->bfd_section
->filepos
!= 0)
3911 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3912 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3914 ((*_bfd_error_handler
)
3915 (_("%s: warning: allocated section `%s' not in segment"),
3916 bfd_get_filename (abfd
),
3917 (hdr
->bfd_section
== NULL
3919 : hdr
->bfd_section
->name
)));
3920 if ((abfd
->flags
& D_PAGED
) != 0)
3921 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3923 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3924 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3927 else if (hdr
->sh_type
== SHT_REL
3928 || hdr
->sh_type
== SHT_RELA
3929 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3930 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
3931 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3932 hdr
->sh_offset
= -1;
3934 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3936 if (i
== SHN_LORESERVE
- 1)
3938 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3939 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3944 /* Place the section headers. */
3945 off
= align_file_position (off
, bed
->s
->file_align
);
3946 i_ehdrp
->e_shoff
= off
;
3947 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3949 elf_tdata (abfd
)->next_file_pos
= off
;
3958 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3959 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3960 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3961 struct elf_strtab_hash
*shstrtab
;
3962 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3964 i_ehdrp
= elf_elfheader (abfd
);
3965 i_shdrp
= elf_elfsections (abfd
);
3967 shstrtab
= _bfd_elf_strtab_init ();
3968 if (shstrtab
== NULL
)
3971 elf_shstrtab (abfd
) = shstrtab
;
3973 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3974 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3975 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3976 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3978 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3979 i_ehdrp
->e_ident
[EI_DATA
] =
3980 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3981 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3983 if ((abfd
->flags
& DYNAMIC
) != 0)
3984 i_ehdrp
->e_type
= ET_DYN
;
3985 else if ((abfd
->flags
& EXEC_P
) != 0)
3986 i_ehdrp
->e_type
= ET_EXEC
;
3987 else if (bfd_get_format (abfd
) == bfd_core
)
3988 i_ehdrp
->e_type
= ET_CORE
;
3990 i_ehdrp
->e_type
= ET_REL
;
3992 switch (bfd_get_arch (abfd
))
3994 case bfd_arch_unknown
:
3995 i_ehdrp
->e_machine
= EM_NONE
;
3998 /* There used to be a long list of cases here, each one setting
3999 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4000 in the corresponding bfd definition. To avoid duplication,
4001 the switch was removed. Machines that need special handling
4002 can generally do it in elf_backend_final_write_processing(),
4003 unless they need the information earlier than the final write.
4004 Such need can generally be supplied by replacing the tests for
4005 e_machine with the conditions used to determine it. */
4007 if (get_elf_backend_data (abfd
) != NULL
)
4008 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
4010 i_ehdrp
->e_machine
= EM_NONE
;
4013 i_ehdrp
->e_version
= bed
->s
->ev_current
;
4014 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
4016 /* No program header, for now. */
4017 i_ehdrp
->e_phoff
= 0;
4018 i_ehdrp
->e_phentsize
= 0;
4019 i_ehdrp
->e_phnum
= 0;
4021 /* Each bfd section is section header entry. */
4022 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
4023 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
4025 /* If we're building an executable, we'll need a program header table. */
4026 if (abfd
->flags
& EXEC_P
)
4028 /* It all happens later. */
4030 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
4032 /* elf_build_phdrs() returns a (NULL-terminated) array of
4033 Elf_Internal_Phdrs. */
4034 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
4035 i_ehdrp
->e_phoff
= outbase
;
4036 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
4041 i_ehdrp
->e_phentsize
= 0;
4043 i_ehdrp
->e_phoff
= 0;
4046 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
4047 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
4048 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
4049 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
4050 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
4051 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
4052 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4053 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
4054 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
4060 /* Assign file positions for all the reloc sections which are not part
4061 of the loadable file image. */
4064 _bfd_elf_assign_file_positions_for_relocs (abfd
)
4068 unsigned int i
, num_sec
;
4069 Elf_Internal_Shdr
**shdrpp
;
4071 off
= elf_tdata (abfd
)->next_file_pos
;
4073 num_sec
= elf_numsections (abfd
);
4074 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
4076 Elf_Internal_Shdr
*shdrp
;
4079 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
4080 && shdrp
->sh_offset
== -1)
4081 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
4084 elf_tdata (abfd
)->next_file_pos
= off
;
4088 _bfd_elf_write_object_contents (abfd
)
4091 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4092 Elf_Internal_Ehdr
*i_ehdrp
;
4093 Elf_Internal_Shdr
**i_shdrp
;
4095 unsigned int count
, num_sec
;
4097 if (! abfd
->output_has_begun
4098 && ! _bfd_elf_compute_section_file_positions
4099 (abfd
, (struct bfd_link_info
*) NULL
))
4102 i_shdrp
= elf_elfsections (abfd
);
4103 i_ehdrp
= elf_elfheader (abfd
);
4106 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4110 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4112 /* After writing the headers, we need to write the sections too... */
4113 num_sec
= elf_numsections (abfd
);
4114 for (count
= 1; count
< num_sec
; count
++)
4116 if (bed
->elf_backend_section_processing
)
4117 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4118 if (i_shdrp
[count
]->contents
)
4120 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4122 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4123 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4126 if (count
== SHN_LORESERVE
- 1)
4127 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4130 /* Write out the section header names. */
4131 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4132 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4135 if (bed
->elf_backend_final_write_processing
)
4136 (*bed
->elf_backend_final_write_processing
) (abfd
,
4137 elf_tdata (abfd
)->linker
);
4139 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4143 _bfd_elf_write_corefile_contents (abfd
)
4146 /* Hopefully this can be done just like an object file. */
4147 return _bfd_elf_write_object_contents (abfd
);
4150 /* Given a section, search the header to find them. */
4153 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4157 struct elf_backend_data
*bed
;
4160 if (elf_section_data (asect
) != NULL
4161 && elf_section_data (asect
)->this_idx
!= 0)
4162 return elf_section_data (asect
)->this_idx
;
4164 if (bfd_is_abs_section (asect
))
4166 else if (bfd_is_com_section (asect
))
4168 else if (bfd_is_und_section (asect
))
4172 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4173 int maxindex
= elf_numsections (abfd
);
4175 for (index
= 1; index
< maxindex
; index
++)
4177 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4179 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4185 bed
= get_elf_backend_data (abfd
);
4186 if (bed
->elf_backend_section_from_bfd_section
)
4190 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4195 bfd_set_error (bfd_error_nonrepresentable_section
);
4200 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4204 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4206 asymbol
**asym_ptr_ptr
;
4208 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4210 flagword flags
= asym_ptr
->flags
;
4212 /* When gas creates relocations against local labels, it creates its
4213 own symbol for the section, but does put the symbol into the
4214 symbol chain, so udata is 0. When the linker is generating
4215 relocatable output, this section symbol may be for one of the
4216 input sections rather than the output section. */
4217 if (asym_ptr
->udata
.i
== 0
4218 && (flags
& BSF_SECTION_SYM
)
4219 && asym_ptr
->section
)
4223 if (asym_ptr
->section
->output_section
!= NULL
)
4224 indx
= asym_ptr
->section
->output_section
->index
;
4226 indx
= asym_ptr
->section
->index
;
4227 if (indx
< elf_num_section_syms (abfd
)
4228 && elf_section_syms (abfd
)[indx
] != NULL
)
4229 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4232 idx
= asym_ptr
->udata
.i
;
4236 /* This case can occur when using --strip-symbol on a symbol
4237 which is used in a relocation entry. */
4238 (*_bfd_error_handler
)
4239 (_("%s: symbol `%s' required but not present"),
4240 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4241 bfd_set_error (bfd_error_no_symbols
);
4248 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4249 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4250 elf_symbol_flags (flags
));
4258 /* Copy private BFD data. This copies any program header information. */
4261 copy_private_bfd_data (ibfd
, obfd
)
4265 Elf_Internal_Ehdr
* iehdr
;
4266 struct elf_segment_map
* map
;
4267 struct elf_segment_map
* map_first
;
4268 struct elf_segment_map
** pointer_to_map
;
4269 Elf_Internal_Phdr
* segment
;
4272 unsigned int num_segments
;
4273 boolean phdr_included
= false;
4274 bfd_vma maxpagesize
;
4275 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4276 unsigned int phdr_adjust_num
= 0;
4278 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4279 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4282 if (elf_tdata (ibfd
)->phdr
== NULL
)
4285 iehdr
= elf_elfheader (ibfd
);
4288 pointer_to_map
= &map_first
;
4290 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4291 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4293 /* Returns the end address of the segment + 1. */
4294 #define SEGMENT_END(segment, start) \
4295 (start + (segment->p_memsz > segment->p_filesz \
4296 ? segment->p_memsz : segment->p_filesz))
4298 /* Returns true if the given section is contained within
4299 the given segment. VMA addresses are compared. */
4300 #define IS_CONTAINED_BY_VMA(section, segment) \
4301 (section->vma >= segment->p_vaddr \
4302 && (section->vma + section->_raw_size) \
4303 <= (SEGMENT_END (segment, segment->p_vaddr)))
4305 /* Returns true if the given section is contained within
4306 the given segment. LMA addresses are compared. */
4307 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4308 (section->lma >= base \
4309 && (section->lma + section->_raw_size) \
4310 <= SEGMENT_END (segment, base))
4312 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4313 #define IS_COREFILE_NOTE(p, s) \
4314 (p->p_type == PT_NOTE \
4315 && bfd_get_format (ibfd) == bfd_core \
4316 && s->vma == 0 && s->lma == 0 \
4317 && (bfd_vma) s->filepos >= p->p_offset \
4318 && (bfd_vma) s->filepos + s->_raw_size \
4319 <= p->p_offset + p->p_filesz)
4321 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4322 linker, which generates a PT_INTERP section with p_vaddr and
4323 p_memsz set to 0. */
4324 #define IS_SOLARIS_PT_INTERP(p, s) \
4326 && p->p_filesz > 0 \
4327 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4328 && s->_raw_size > 0 \
4329 && (bfd_vma) s->filepos >= p->p_offset \
4330 && ((bfd_vma) s->filepos + s->_raw_size \
4331 <= p->p_offset + p->p_filesz))
4333 /* Decide if the given section should be included in the given segment.
4334 A section will be included if:
4335 1. It is within the address space of the segment -- we use the LMA
4336 if that is set for the segment and the VMA otherwise,
4337 2. It is an allocated segment,
4338 3. There is an output section associated with it,
4339 4. The section has not already been allocated to a previous segment. */
4340 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4341 (((((segment->p_paddr \
4342 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4343 : IS_CONTAINED_BY_VMA (section, segment)) \
4344 || IS_SOLARIS_PT_INTERP (segment, section)) \
4345 && (section->flags & SEC_ALLOC) != 0) \
4346 || IS_COREFILE_NOTE (segment, section)) \
4347 && section->output_section != NULL \
4348 && section->segment_mark == false)
4350 /* Returns true iff seg1 starts after the end of seg2. */
4351 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4352 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4354 /* Returns true iff seg1 and seg2 overlap. */
4355 #define SEGMENT_OVERLAPS(seg1, seg2) \
4356 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4358 /* Initialise the segment mark field. */
4359 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4360 section
->segment_mark
= false;
4362 /* Scan through the segments specified in the program header
4363 of the input BFD. For this first scan we look for overlaps
4364 in the loadable segments. These can be created by weird
4365 parameters to objcopy. */
4366 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4371 Elf_Internal_Phdr
*segment2
;
4373 if (segment
->p_type
!= PT_LOAD
)
4376 /* Determine if this segment overlaps any previous segments. */
4377 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4379 bfd_signed_vma extra_length
;
4381 if (segment2
->p_type
!= PT_LOAD
4382 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4385 /* Merge the two segments together. */
4386 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4388 /* Extend SEGMENT2 to include SEGMENT and then delete
4391 SEGMENT_END (segment
, segment
->p_vaddr
)
4392 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4394 if (extra_length
> 0)
4396 segment2
->p_memsz
+= extra_length
;
4397 segment2
->p_filesz
+= extra_length
;
4400 segment
->p_type
= PT_NULL
;
4402 /* Since we have deleted P we must restart the outer loop. */
4404 segment
= elf_tdata (ibfd
)->phdr
;
4409 /* Extend SEGMENT to include SEGMENT2 and then delete
4412 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4413 - SEGMENT_END (segment
, segment
->p_vaddr
);
4415 if (extra_length
> 0)
4417 segment
->p_memsz
+= extra_length
;
4418 segment
->p_filesz
+= extra_length
;
4421 segment2
->p_type
= PT_NULL
;
4426 /* The second scan attempts to assign sections to segments. */
4427 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4431 unsigned int section_count
;
4432 asection
** sections
;
4433 asection
* output_section
;
4435 bfd_vma matching_lma
;
4436 bfd_vma suggested_lma
;
4440 if (segment
->p_type
== PT_NULL
)
4443 /* Compute how many sections might be placed into this segment. */
4445 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4446 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4449 /* Allocate a segment map big enough to contain all of the
4450 sections we have selected. */
4451 amt
= sizeof (struct elf_segment_map
);
4452 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4453 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4457 /* Initialise the fields of the segment map. Default to
4458 using the physical address of the segment in the input BFD. */
4460 map
->p_type
= segment
->p_type
;
4461 map
->p_flags
= segment
->p_flags
;
4462 map
->p_flags_valid
= 1;
4463 map
->p_paddr
= segment
->p_paddr
;
4464 map
->p_paddr_valid
= 1;
4466 /* Determine if this segment contains the ELF file header
4467 and if it contains the program headers themselves. */
4468 map
->includes_filehdr
= (segment
->p_offset
== 0
4469 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4471 map
->includes_phdrs
= 0;
4473 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4475 map
->includes_phdrs
=
4476 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4477 && (segment
->p_offset
+ segment
->p_filesz
4478 >= ((bfd_vma
) iehdr
->e_phoff
4479 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4481 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4482 phdr_included
= true;
4485 if (section_count
== 0)
4487 /* Special segments, such as the PT_PHDR segment, may contain
4488 no sections, but ordinary, loadable segments should contain
4490 if (segment
->p_type
== PT_LOAD
)
4492 (_("%s: warning: Empty loadable segment detected\n"),
4493 bfd_archive_filename (ibfd
));
4496 *pointer_to_map
= map
;
4497 pointer_to_map
= &map
->next
;
4502 /* Now scan the sections in the input BFD again and attempt
4503 to add their corresponding output sections to the segment map.
4504 The problem here is how to handle an output section which has
4505 been moved (ie had its LMA changed). There are four possibilities:
4507 1. None of the sections have been moved.
4508 In this case we can continue to use the segment LMA from the
4511 2. All of the sections have been moved by the same amount.
4512 In this case we can change the segment's LMA to match the LMA
4513 of the first section.
4515 3. Some of the sections have been moved, others have not.
4516 In this case those sections which have not been moved can be
4517 placed in the current segment which will have to have its size,
4518 and possibly its LMA changed, and a new segment or segments will
4519 have to be created to contain the other sections.
4521 4. The sections have been moved, but not be the same amount.
4522 In this case we can change the segment's LMA to match the LMA
4523 of the first section and we will have to create a new segment
4524 or segments to contain the other sections.
4526 In order to save time, we allocate an array to hold the section
4527 pointers that we are interested in. As these sections get assigned
4528 to a segment, they are removed from this array. */
4530 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4531 sections
= (asection
**) bfd_malloc (amt
);
4532 if (sections
== NULL
)
4535 /* Step One: Scan for segment vs section LMA conflicts.
4536 Also add the sections to the section array allocated above.
4537 Also add the sections to the current segment. In the common
4538 case, where the sections have not been moved, this means that
4539 we have completely filled the segment, and there is nothing
4545 for (j
= 0, section
= ibfd
->sections
;
4547 section
= section
->next
)
4549 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4551 output_section
= section
->output_section
;
4553 sections
[j
++] = section
;
4555 /* The Solaris native linker always sets p_paddr to 0.
4556 We try to catch that case here, and set it to the
4558 if (segment
->p_paddr
== 0
4559 && segment
->p_vaddr
!= 0
4561 && output_section
->lma
!= 0
4562 && (output_section
->vma
== (segment
->p_vaddr
4563 + (map
->includes_filehdr
4566 + (map
->includes_phdrs
4568 * iehdr
->e_phentsize
)
4570 map
->p_paddr
= segment
->p_vaddr
;
4572 /* Match up the physical address of the segment with the
4573 LMA address of the output section. */
4574 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4575 || IS_COREFILE_NOTE (segment
, section
))
4577 if (matching_lma
== 0)
4578 matching_lma
= output_section
->lma
;
4580 /* We assume that if the section fits within the segment
4581 then it does not overlap any other section within that
4583 map
->sections
[isec
++] = output_section
;
4585 else if (suggested_lma
== 0)
4586 suggested_lma
= output_section
->lma
;
4590 BFD_ASSERT (j
== section_count
);
4592 /* Step Two: Adjust the physical address of the current segment,
4594 if (isec
== section_count
)
4596 /* All of the sections fitted within the segment as currently
4597 specified. This is the default case. Add the segment to
4598 the list of built segments and carry on to process the next
4599 program header in the input BFD. */
4600 map
->count
= section_count
;
4601 *pointer_to_map
= map
;
4602 pointer_to_map
= &map
->next
;
4609 if (matching_lma
!= 0)
4611 /* At least one section fits inside the current segment.
4612 Keep it, but modify its physical address to match the
4613 LMA of the first section that fitted. */
4614 map
->p_paddr
= matching_lma
;
4618 /* None of the sections fitted inside the current segment.
4619 Change the current segment's physical address to match
4620 the LMA of the first section. */
4621 map
->p_paddr
= suggested_lma
;
4624 /* Offset the segment physical address from the lma
4625 to allow for space taken up by elf headers. */
4626 if (map
->includes_filehdr
)
4627 map
->p_paddr
-= iehdr
->e_ehsize
;
4629 if (map
->includes_phdrs
)
4631 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4633 /* iehdr->e_phnum is just an estimate of the number
4634 of program headers that we will need. Make a note
4635 here of the number we used and the segment we chose
4636 to hold these headers, so that we can adjust the
4637 offset when we know the correct value. */
4638 phdr_adjust_num
= iehdr
->e_phnum
;
4639 phdr_adjust_seg
= map
;
4643 /* Step Three: Loop over the sections again, this time assigning
4644 those that fit to the current segment and remvoing them from the
4645 sections array; but making sure not to leave large gaps. Once all
4646 possible sections have been assigned to the current segment it is
4647 added to the list of built segments and if sections still remain
4648 to be assigned, a new segment is constructed before repeating
4656 /* Fill the current segment with sections that fit. */
4657 for (j
= 0; j
< section_count
; j
++)
4659 section
= sections
[j
];
4661 if (section
== NULL
)
4664 output_section
= section
->output_section
;
4666 BFD_ASSERT (output_section
!= NULL
);
4668 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4669 || IS_COREFILE_NOTE (segment
, section
))
4671 if (map
->count
== 0)
4673 /* If the first section in a segment does not start at
4674 the beginning of the segment, then something is
4676 if (output_section
->lma
!=
4678 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4679 + (map
->includes_phdrs
4680 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4686 asection
* prev_sec
;
4688 prev_sec
= map
->sections
[map
->count
- 1];
4690 /* If the gap between the end of the previous section
4691 and the start of this section is more than
4692 maxpagesize then we need to start a new segment. */
4693 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4695 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4696 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4697 > output_section
->lma
))
4699 if (suggested_lma
== 0)
4700 suggested_lma
= output_section
->lma
;
4706 map
->sections
[map
->count
++] = output_section
;
4709 section
->segment_mark
= true;
4711 else if (suggested_lma
== 0)
4712 suggested_lma
= output_section
->lma
;
4715 BFD_ASSERT (map
->count
> 0);
4717 /* Add the current segment to the list of built segments. */
4718 *pointer_to_map
= map
;
4719 pointer_to_map
= &map
->next
;
4721 if (isec
< section_count
)
4723 /* We still have not allocated all of the sections to
4724 segments. Create a new segment here, initialise it
4725 and carry on looping. */
4726 amt
= sizeof (struct elf_segment_map
);
4727 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4728 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4732 /* Initialise the fields of the segment map. Set the physical
4733 physical address to the LMA of the first section that has
4734 not yet been assigned. */
4736 map
->p_type
= segment
->p_type
;
4737 map
->p_flags
= segment
->p_flags
;
4738 map
->p_flags_valid
= 1;
4739 map
->p_paddr
= suggested_lma
;
4740 map
->p_paddr_valid
= 1;
4741 map
->includes_filehdr
= 0;
4742 map
->includes_phdrs
= 0;
4745 while (isec
< section_count
);
4750 /* The Solaris linker creates program headers in which all the
4751 p_paddr fields are zero. When we try to objcopy or strip such a
4752 file, we get confused. Check for this case, and if we find it
4753 reset the p_paddr_valid fields. */
4754 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4755 if (map
->p_paddr
!= 0)
4759 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4760 map
->p_paddr_valid
= 0;
4763 elf_tdata (obfd
)->segment_map
= map_first
;
4765 /* If we had to estimate the number of program headers that were
4766 going to be needed, then check our estimate now and adjust
4767 the offset if necessary. */
4768 if (phdr_adjust_seg
!= NULL
)
4772 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4775 if (count
> phdr_adjust_num
)
4776 phdr_adjust_seg
->p_paddr
4777 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4781 /* Final Step: Sort the segments into ascending order of physical
4783 if (map_first
!= NULL
)
4785 struct elf_segment_map
*prev
;
4788 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4790 /* Yes I know - its a bubble sort.... */
4791 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4793 /* Swap map and map->next. */
4794 prev
->next
= map
->next
;
4795 map
->next
= map
->next
->next
;
4796 prev
->next
->next
= map
;
4806 #undef IS_CONTAINED_BY_VMA
4807 #undef IS_CONTAINED_BY_LMA
4808 #undef IS_COREFILE_NOTE
4809 #undef IS_SOLARIS_PT_INTERP
4810 #undef INCLUDE_SECTION_IN_SEGMENT
4811 #undef SEGMENT_AFTER_SEGMENT
4812 #undef SEGMENT_OVERLAPS
4816 /* Copy private section information. This copies over the entsize
4817 field, and sometimes the info field. */
4820 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4826 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4828 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4829 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4832 /* Copy over private BFD data if it has not already been copied.
4833 This must be done here, rather than in the copy_private_bfd_data
4834 entry point, because the latter is called after the section
4835 contents have been set, which means that the program headers have
4836 already been worked out. */
4837 if (elf_tdata (obfd
)->segment_map
== NULL
4838 && elf_tdata (ibfd
)->phdr
!= NULL
)
4842 /* Only set up the segments if there are no more SEC_ALLOC
4843 sections. FIXME: This won't do the right thing if objcopy is
4844 used to remove the last SEC_ALLOC section, since objcopy
4845 won't call this routine in that case. */
4846 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4847 if ((s
->flags
& SEC_ALLOC
) != 0)
4851 if (! copy_private_bfd_data (ibfd
, obfd
))
4856 ihdr
= &elf_section_data (isec
)->this_hdr
;
4857 ohdr
= &elf_section_data (osec
)->this_hdr
;
4859 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4861 if (ihdr
->sh_type
== SHT_SYMTAB
4862 || ihdr
->sh_type
== SHT_DYNSYM
4863 || ihdr
->sh_type
== SHT_GNU_verneed
4864 || ihdr
->sh_type
== SHT_GNU_verdef
)
4865 ohdr
->sh_info
= ihdr
->sh_info
;
4867 elf_section_data (osec
)->use_rela_p
4868 = elf_section_data (isec
)->use_rela_p
;
4873 /* Copy private symbol information. If this symbol is in a section
4874 which we did not map into a BFD section, try to map the section
4875 index correctly. We use special macro definitions for the mapped
4876 section indices; these definitions are interpreted by the
4877 swap_out_syms function. */
4879 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4880 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4881 #define MAP_STRTAB (SHN_HIOS + 3)
4882 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4883 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4886 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4892 elf_symbol_type
*isym
, *osym
;
4894 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4895 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4898 isym
= elf_symbol_from (ibfd
, isymarg
);
4899 osym
= elf_symbol_from (obfd
, osymarg
);
4903 && bfd_is_abs_section (isym
->symbol
.section
))
4907 shndx
= isym
->internal_elf_sym
.st_shndx
;
4908 if (shndx
== elf_onesymtab (ibfd
))
4909 shndx
= MAP_ONESYMTAB
;
4910 else if (shndx
== elf_dynsymtab (ibfd
))
4911 shndx
= MAP_DYNSYMTAB
;
4912 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4914 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4915 shndx
= MAP_SHSTRTAB
;
4916 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
4917 shndx
= MAP_SYM_SHNDX
;
4918 osym
->internal_elf_sym
.st_shndx
= shndx
;
4924 /* Swap out the symbols. */
4927 swap_out_syms (abfd
, sttp
, relocatable_p
)
4929 struct bfd_strtab_hash
**sttp
;
4932 struct elf_backend_data
*bed
;
4935 struct bfd_strtab_hash
*stt
;
4936 Elf_Internal_Shdr
*symtab_hdr
;
4937 Elf_Internal_Shdr
*symtab_shndx_hdr
;
4938 Elf_Internal_Shdr
*symstrtab_hdr
;
4939 char *outbound_syms
;
4940 char *outbound_shndx
;
4944 if (!elf_map_symbols (abfd
))
4947 /* Dump out the symtabs. */
4948 stt
= _bfd_elf_stringtab_init ();
4952 bed
= get_elf_backend_data (abfd
);
4953 symcount
= bfd_get_symcount (abfd
);
4954 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4955 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4956 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4957 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4958 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4959 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4961 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4962 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4964 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
4965 outbound_syms
= bfd_alloc (abfd
, amt
);
4966 if (outbound_syms
== NULL
)
4968 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4970 outbound_shndx
= NULL
;
4971 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
4972 if (symtab_shndx_hdr
->sh_name
!= 0)
4974 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
4975 outbound_shndx
= bfd_alloc (abfd
, amt
);
4976 if (outbound_shndx
== NULL
)
4978 memset (outbound_shndx
, 0, (unsigned long) amt
);
4979 symtab_shndx_hdr
->contents
= outbound_shndx
;
4980 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
4981 symtab_shndx_hdr
->sh_size
= amt
;
4982 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
4983 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
4986 /* now generate the data (for "contents") */
4988 /* Fill in zeroth symbol and swap it out. */
4989 Elf_Internal_Sym sym
;
4995 sym
.st_shndx
= SHN_UNDEF
;
4996 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
4997 outbound_syms
+= bed
->s
->sizeof_sym
;
4998 if (outbound_shndx
!= NULL
)
4999 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5002 syms
= bfd_get_outsymbols (abfd
);
5003 for (idx
= 0; idx
< symcount
; idx
++)
5005 Elf_Internal_Sym sym
;
5006 bfd_vma value
= syms
[idx
]->value
;
5007 elf_symbol_type
*type_ptr
;
5008 flagword flags
= syms
[idx
]->flags
;
5011 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
5013 /* Local section symbols have no name. */
5018 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
5021 if (sym
.st_name
== (unsigned long) -1)
5025 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
5027 if ((flags
& BSF_SECTION_SYM
) == 0
5028 && bfd_is_com_section (syms
[idx
]->section
))
5030 /* ELF common symbols put the alignment into the `value' field,
5031 and the size into the `size' field. This is backwards from
5032 how BFD handles it, so reverse it here. */
5033 sym
.st_size
= value
;
5034 if (type_ptr
== NULL
5035 || type_ptr
->internal_elf_sym
.st_value
== 0)
5036 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
5038 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
5039 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
5040 (abfd
, syms
[idx
]->section
);
5044 asection
*sec
= syms
[idx
]->section
;
5047 if (sec
->output_section
)
5049 value
+= sec
->output_offset
;
5050 sec
= sec
->output_section
;
5052 /* Don't add in the section vma for relocatable output. */
5053 if (! relocatable_p
)
5055 sym
.st_value
= value
;
5056 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
5058 if (bfd_is_abs_section (sec
)
5060 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
5062 /* This symbol is in a real ELF section which we did
5063 not create as a BFD section. Undo the mapping done
5064 by copy_private_symbol_data. */
5065 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
5069 shndx
= elf_onesymtab (abfd
);
5072 shndx
= elf_dynsymtab (abfd
);
5075 shndx
= elf_tdata (abfd
)->strtab_section
;
5078 shndx
= elf_tdata (abfd
)->shstrtab_section
;
5081 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
5089 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
5095 /* Writing this would be a hell of a lot easier if
5096 we had some decent documentation on bfd, and
5097 knew what to expect of the library, and what to
5098 demand of applications. For example, it
5099 appears that `objcopy' might not set the
5100 section of a symbol to be a section that is
5101 actually in the output file. */
5102 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
5103 BFD_ASSERT (sec2
!= 0);
5104 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5105 BFD_ASSERT (shndx
!= -1);
5109 sym
.st_shndx
= shndx
;
5112 if ((flags
& BSF_THREAD_LOCAL
) != 0)
5114 else if ((flags
& BSF_FUNCTION
) != 0)
5116 else if ((flags
& BSF_OBJECT
) != 0)
5121 if (syms
[idx
]->section
->flags
& SEC_THREAD_LOCAL
)
5124 /* Processor-specific types */
5125 if (type_ptr
!= NULL
5126 && bed
->elf_backend_get_symbol_type
)
5127 type
= ((*bed
->elf_backend_get_symbol_type
)
5128 (&type_ptr
->internal_elf_sym
, type
));
5130 if (flags
& BSF_SECTION_SYM
)
5132 if (flags
& BSF_GLOBAL
)
5133 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5135 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5137 else if (bfd_is_com_section (syms
[idx
]->section
))
5138 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5139 else if (bfd_is_und_section (syms
[idx
]->section
))
5140 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5144 else if (flags
& BSF_FILE
)
5145 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5148 int bind
= STB_LOCAL
;
5150 if (flags
& BSF_LOCAL
)
5152 else if (flags
& BSF_WEAK
)
5154 else if (flags
& BSF_GLOBAL
)
5157 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5160 if (type_ptr
!= NULL
)
5161 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5165 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5166 outbound_syms
+= bed
->s
->sizeof_sym
;
5167 if (outbound_shndx
!= NULL
)
5168 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5172 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5173 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5175 symstrtab_hdr
->sh_flags
= 0;
5176 symstrtab_hdr
->sh_addr
= 0;
5177 symstrtab_hdr
->sh_entsize
= 0;
5178 symstrtab_hdr
->sh_link
= 0;
5179 symstrtab_hdr
->sh_info
= 0;
5180 symstrtab_hdr
->sh_addralign
= 1;
5185 /* Return the number of bytes required to hold the symtab vector.
5187 Note that we base it on the count plus 1, since we will null terminate
5188 the vector allocated based on this size. However, the ELF symbol table
5189 always has a dummy entry as symbol #0, so it ends up even. */
5192 _bfd_elf_get_symtab_upper_bound (abfd
)
5197 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5199 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5200 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5202 symtab_size
-= sizeof (asymbol
*);
5208 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5213 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5215 if (elf_dynsymtab (abfd
) == 0)
5217 bfd_set_error (bfd_error_invalid_operation
);
5221 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5222 symtab_size
= (symcount
+ 1) * (sizeof (asymbol
*));
5224 symtab_size
-= sizeof (asymbol
*);
5230 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5231 bfd
*abfd ATTRIBUTE_UNUSED
;
5234 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5237 /* Canonicalize the relocs. */
5240 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5248 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5250 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5253 tblptr
= section
->relocation
;
5254 for (i
= 0; i
< section
->reloc_count
; i
++)
5255 *relptr
++ = tblptr
++;
5259 return section
->reloc_count
;
5263 _bfd_elf_get_symtab (abfd
, alocation
)
5265 asymbol
**alocation
;
5267 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5268 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5271 bfd_get_symcount (abfd
) = symcount
;
5276 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5278 asymbol
**alocation
;
5280 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5281 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5284 /* Return the size required for the dynamic reloc entries. Any
5285 section that was actually installed in the BFD, and has type
5286 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5287 considered to be a dynamic reloc section. */
5290 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5296 if (elf_dynsymtab (abfd
) == 0)
5298 bfd_set_error (bfd_error_invalid_operation
);
5302 ret
= sizeof (arelent
*);
5303 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5304 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5305 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5306 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5307 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5308 * sizeof (arelent
*));
5313 /* Canonicalize the dynamic relocation entries. Note that we return
5314 the dynamic relocations as a single block, although they are
5315 actually associated with particular sections; the interface, which
5316 was designed for SunOS style shared libraries, expects that there
5317 is only one set of dynamic relocs. Any section that was actually
5318 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5319 the dynamic symbol table, is considered to be a dynamic reloc
5323 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5328 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5332 if (elf_dynsymtab (abfd
) == 0)
5334 bfd_set_error (bfd_error_invalid_operation
);
5338 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5340 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5342 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5343 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5344 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5349 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5351 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5353 for (i
= 0; i
< count
; i
++)
5364 /* Read in the version information. */
5367 _bfd_elf_slurp_version_tables (abfd
)
5370 bfd_byte
*contents
= NULL
;
5373 if (elf_dynverdef (abfd
) != 0)
5375 Elf_Internal_Shdr
*hdr
;
5376 Elf_External_Verdef
*everdef
;
5377 Elf_Internal_Verdef
*iverdef
;
5378 Elf_Internal_Verdef
*iverdefarr
;
5379 Elf_Internal_Verdef iverdefmem
;
5381 unsigned int maxidx
;
5383 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5385 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5386 if (contents
== NULL
)
5388 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5389 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5392 /* We know the number of entries in the section but not the maximum
5393 index. Therefore we have to run through all entries and find
5395 everdef
= (Elf_External_Verdef
*) contents
;
5397 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5399 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5401 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5402 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5404 everdef
= ((Elf_External_Verdef
*)
5405 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5408 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5409 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5410 if (elf_tdata (abfd
)->verdef
== NULL
)
5413 elf_tdata (abfd
)->cverdefs
= maxidx
;
5415 everdef
= (Elf_External_Verdef
*) contents
;
5416 iverdefarr
= elf_tdata (abfd
)->verdef
;
5417 for (i
= 0; i
< hdr
->sh_info
; i
++)
5419 Elf_External_Verdaux
*everdaux
;
5420 Elf_Internal_Verdaux
*iverdaux
;
5423 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5425 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5426 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5428 iverdef
->vd_bfd
= abfd
;
5430 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5431 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5432 if (iverdef
->vd_auxptr
== NULL
)
5435 everdaux
= ((Elf_External_Verdaux
*)
5436 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5437 iverdaux
= iverdef
->vd_auxptr
;
5438 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5440 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5442 iverdaux
->vda_nodename
=
5443 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5444 iverdaux
->vda_name
);
5445 if (iverdaux
->vda_nodename
== NULL
)
5448 if (j
+ 1 < iverdef
->vd_cnt
)
5449 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5451 iverdaux
->vda_nextptr
= NULL
;
5453 everdaux
= ((Elf_External_Verdaux
*)
5454 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5457 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5459 if (i
+ 1 < hdr
->sh_info
)
5460 iverdef
->vd_nextdef
= iverdef
+ 1;
5462 iverdef
->vd_nextdef
= NULL
;
5464 everdef
= ((Elf_External_Verdef
*)
5465 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5472 if (elf_dynverref (abfd
) != 0)
5474 Elf_Internal_Shdr
*hdr
;
5475 Elf_External_Verneed
*everneed
;
5476 Elf_Internal_Verneed
*iverneed
;
5479 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5481 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5482 elf_tdata (abfd
)->verref
=
5483 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5484 if (elf_tdata (abfd
)->verref
== NULL
)
5487 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5489 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5490 if (contents
== NULL
)
5492 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5493 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5496 everneed
= (Elf_External_Verneed
*) contents
;
5497 iverneed
= elf_tdata (abfd
)->verref
;
5498 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5500 Elf_External_Vernaux
*evernaux
;
5501 Elf_Internal_Vernaux
*ivernaux
;
5504 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5506 iverneed
->vn_bfd
= abfd
;
5508 iverneed
->vn_filename
=
5509 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5511 if (iverneed
->vn_filename
== NULL
)
5514 amt
= iverneed
->vn_cnt
;
5515 amt
*= sizeof (Elf_Internal_Vernaux
);
5516 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5518 evernaux
= ((Elf_External_Vernaux
*)
5519 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5520 ivernaux
= iverneed
->vn_auxptr
;
5521 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5523 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5525 ivernaux
->vna_nodename
=
5526 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5527 ivernaux
->vna_name
);
5528 if (ivernaux
->vna_nodename
== NULL
)
5531 if (j
+ 1 < iverneed
->vn_cnt
)
5532 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5534 ivernaux
->vna_nextptr
= NULL
;
5536 evernaux
= ((Elf_External_Vernaux
*)
5537 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5540 if (i
+ 1 < hdr
->sh_info
)
5541 iverneed
->vn_nextref
= iverneed
+ 1;
5543 iverneed
->vn_nextref
= NULL
;
5545 everneed
= ((Elf_External_Verneed
*)
5546 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5556 if (contents
== NULL
)
5562 _bfd_elf_make_empty_symbol (abfd
)
5565 elf_symbol_type
*newsym
;
5566 bfd_size_type amt
= sizeof (elf_symbol_type
);
5568 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5573 newsym
->symbol
.the_bfd
= abfd
;
5574 return &newsym
->symbol
;
5579 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5580 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5584 bfd_symbol_info (symbol
, ret
);
5587 /* Return whether a symbol name implies a local symbol. Most targets
5588 use this function for the is_local_label_name entry point, but some
5592 _bfd_elf_is_local_label_name (abfd
, name
)
5593 bfd
*abfd ATTRIBUTE_UNUSED
;
5596 /* Normal local symbols start with ``.L''. */
5597 if (name
[0] == '.' && name
[1] == 'L')
5600 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5601 DWARF debugging symbols starting with ``..''. */
5602 if (name
[0] == '.' && name
[1] == '.')
5605 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5606 emitting DWARF debugging output. I suspect this is actually a
5607 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5608 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5609 underscore to be emitted on some ELF targets). For ease of use,
5610 we treat such symbols as local. */
5611 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5618 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5619 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5620 asymbol
*symbol ATTRIBUTE_UNUSED
;
5627 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5629 enum bfd_architecture arch
;
5630 unsigned long machine
;
5632 /* If this isn't the right architecture for this backend, and this
5633 isn't the generic backend, fail. */
5634 if (arch
!= get_elf_backend_data (abfd
)->arch
5635 && arch
!= bfd_arch_unknown
5636 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5639 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5642 /* Find the function to a particular section and offset,
5643 for error reporting. */
5646 elf_find_function (abfd
, section
, symbols
, offset
,
5647 filename_ptr
, functionname_ptr
)
5648 bfd
*abfd ATTRIBUTE_UNUSED
;
5652 const char **filename_ptr
;
5653 const char **functionname_ptr
;
5655 const char *filename
;
5664 for (p
= symbols
; *p
!= NULL
; p
++)
5668 q
= (elf_symbol_type
*) *p
;
5670 if (bfd_get_section (&q
->symbol
) != section
)
5673 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5678 filename
= bfd_asymbol_name (&q
->symbol
);
5682 if (q
->symbol
.section
== section
5683 && q
->symbol
.value
>= low_func
5684 && q
->symbol
.value
<= offset
)
5686 func
= (asymbol
*) q
;
5687 low_func
= q
->symbol
.value
;
5697 *filename_ptr
= filename
;
5698 if (functionname_ptr
)
5699 *functionname_ptr
= bfd_asymbol_name (func
);
5704 /* Find the nearest line to a particular section and offset,
5705 for error reporting. */
5708 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5709 filename_ptr
, functionname_ptr
, line_ptr
)
5714 const char **filename_ptr
;
5715 const char **functionname_ptr
;
5716 unsigned int *line_ptr
;
5720 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5721 filename_ptr
, functionname_ptr
,
5724 if (!*functionname_ptr
)
5725 elf_find_function (abfd
, section
, symbols
, offset
,
5726 *filename_ptr
? NULL
: filename_ptr
,
5732 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5733 filename_ptr
, functionname_ptr
,
5735 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5737 if (!*functionname_ptr
)
5738 elf_find_function (abfd
, section
, symbols
, offset
,
5739 *filename_ptr
? NULL
: filename_ptr
,
5745 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5746 &found
, filename_ptr
,
5747 functionname_ptr
, line_ptr
,
5748 &elf_tdata (abfd
)->line_info
))
5753 if (symbols
== NULL
)
5756 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5757 filename_ptr
, functionname_ptr
))
5765 _bfd_elf_sizeof_headers (abfd
, reloc
)
5771 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5773 ret
+= get_program_header_size (abfd
);
5778 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5783 bfd_size_type count
;
5785 Elf_Internal_Shdr
*hdr
;
5788 if (! abfd
->output_has_begun
5789 && ! _bfd_elf_compute_section_file_positions
5790 (abfd
, (struct bfd_link_info
*) NULL
))
5793 hdr
= &elf_section_data (section
)->this_hdr
;
5794 pos
= hdr
->sh_offset
+ offset
;
5795 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5796 || bfd_bwrite (location
, count
, abfd
) != count
)
5803 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5804 bfd
*abfd ATTRIBUTE_UNUSED
;
5805 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5806 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5813 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5816 Elf_Internal_Rel
*dst
;
5822 /* Try to convert a non-ELF reloc into an ELF one. */
5825 _bfd_elf_validate_reloc (abfd
, areloc
)
5829 /* Check whether we really have an ELF howto. */
5831 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5833 bfd_reloc_code_real_type code
;
5834 reloc_howto_type
*howto
;
5836 /* Alien reloc: Try to determine its type to replace it with an
5837 equivalent ELF reloc. */
5839 if (areloc
->howto
->pc_relative
)
5841 switch (areloc
->howto
->bitsize
)
5844 code
= BFD_RELOC_8_PCREL
;
5847 code
= BFD_RELOC_12_PCREL
;
5850 code
= BFD_RELOC_16_PCREL
;
5853 code
= BFD_RELOC_24_PCREL
;
5856 code
= BFD_RELOC_32_PCREL
;
5859 code
= BFD_RELOC_64_PCREL
;
5865 howto
= bfd_reloc_type_lookup (abfd
, code
);
5867 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5869 if (howto
->pcrel_offset
)
5870 areloc
->addend
+= areloc
->address
;
5872 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5877 switch (areloc
->howto
->bitsize
)
5883 code
= BFD_RELOC_14
;
5886 code
= BFD_RELOC_16
;
5889 code
= BFD_RELOC_26
;
5892 code
= BFD_RELOC_32
;
5895 code
= BFD_RELOC_64
;
5901 howto
= bfd_reloc_type_lookup (abfd
, code
);
5905 areloc
->howto
= howto
;
5913 (*_bfd_error_handler
)
5914 (_("%s: unsupported relocation type %s"),
5915 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5916 bfd_set_error (bfd_error_bad_value
);
5921 _bfd_elf_close_and_cleanup (abfd
)
5924 if (bfd_get_format (abfd
) == bfd_object
)
5926 if (elf_shstrtab (abfd
) != NULL
)
5927 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
5930 return _bfd_generic_close_and_cleanup (abfd
);
5933 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5934 in the relocation's offset. Thus we cannot allow any sort of sanity
5935 range-checking to interfere. There is nothing else to do in processing
5938 bfd_reloc_status_type
5939 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5940 bfd
*abfd ATTRIBUTE_UNUSED
;
5941 arelent
*re ATTRIBUTE_UNUSED
;
5942 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5943 PTR data ATTRIBUTE_UNUSED
;
5944 asection
*is ATTRIBUTE_UNUSED
;
5945 bfd
*obfd ATTRIBUTE_UNUSED
;
5946 char **errmsg ATTRIBUTE_UNUSED
;
5948 return bfd_reloc_ok
;
5951 /* Elf core file support. Much of this only works on native
5952 toolchains, since we rely on knowing the
5953 machine-dependent procfs structure in order to pick
5954 out details about the corefile. */
5956 #ifdef HAVE_SYS_PROCFS_H
5957 # include <sys/procfs.h>
5960 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5963 elfcore_make_pid (abfd
)
5966 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5967 + (elf_tdata (abfd
)->core_pid
));
5970 /* If there isn't a section called NAME, make one, using
5971 data from SECT. Note, this function will generate a
5972 reference to NAME, so you shouldn't deallocate or
5976 elfcore_maybe_make_sect (abfd
, name
, sect
)
5983 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5986 sect2
= bfd_make_section (abfd
, name
);
5990 sect2
->_raw_size
= sect
->_raw_size
;
5991 sect2
->filepos
= sect
->filepos
;
5992 sect2
->flags
= sect
->flags
;
5993 sect2
->alignment_power
= sect
->alignment_power
;
5997 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5998 actually creates up to two pseudosections:
5999 - For the single-threaded case, a section named NAME, unless
6000 such a section already exists.
6001 - For the multi-threaded case, a section named "NAME/PID", where
6002 PID is elfcore_make_pid (abfd).
6003 Both pseudosections have identical contents. */
6005 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
6012 char *threaded_name
;
6015 /* Build the section name. */
6017 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
6018 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6019 if (threaded_name
== NULL
)
6021 strcpy (threaded_name
, buf
);
6023 sect
= bfd_make_section (abfd
, threaded_name
);
6026 sect
->_raw_size
= size
;
6027 sect
->filepos
= filepos
;
6028 sect
->flags
= SEC_HAS_CONTENTS
;
6029 sect
->alignment_power
= 2;
6031 return elfcore_maybe_make_sect (abfd
, name
, sect
);
6034 /* prstatus_t exists on:
6036 linux 2.[01] + glibc
6040 #if defined (HAVE_PRSTATUS_T)
6041 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6044 elfcore_grok_prstatus (abfd
, note
)
6046 Elf_Internal_Note
*note
;
6051 if (note
->descsz
== sizeof (prstatus_t
))
6055 raw_size
= sizeof (prstat
.pr_reg
);
6056 offset
= offsetof (prstatus_t
, pr_reg
);
6057 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6059 /* Do not overwrite the core signal if it
6060 has already been set by another thread. */
6061 if (elf_tdata (abfd
)->core_signal
== 0)
6062 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6063 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6065 /* pr_who exists on:
6068 pr_who doesn't exist on:
6071 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6072 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6075 #if defined (HAVE_PRSTATUS32_T)
6076 else if (note
->descsz
== sizeof (prstatus32_t
))
6078 /* 64-bit host, 32-bit corefile */
6079 prstatus32_t prstat
;
6081 raw_size
= sizeof (prstat
.pr_reg
);
6082 offset
= offsetof (prstatus32_t
, pr_reg
);
6083 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
6085 /* Do not overwrite the core signal if it
6086 has already been set by another thread. */
6087 if (elf_tdata (abfd
)->core_signal
== 0)
6088 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
6089 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
6091 /* pr_who exists on:
6094 pr_who doesn't exist on:
6097 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
6098 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
6101 #endif /* HAVE_PRSTATUS32_T */
6104 /* Fail - we don't know how to handle any other
6105 note size (ie. data object type). */
6109 /* Make a ".reg/999" section and a ".reg" section. */
6110 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
6111 raw_size
, note
->descpos
+ offset
);
6113 #endif /* defined (HAVE_PRSTATUS_T) */
6115 /* Create a pseudosection containing the exact contents of NOTE. */
6117 elfcore_make_note_pseudosection (abfd
, name
, note
)
6120 Elf_Internal_Note
*note
;
6122 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6123 note
->descsz
, note
->descpos
);
6126 /* There isn't a consistent prfpregset_t across platforms,
6127 but it doesn't matter, because we don't have to pick this
6128 data structure apart. */
6131 elfcore_grok_prfpreg (abfd
, note
)
6133 Elf_Internal_Note
*note
;
6135 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6138 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6139 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6143 elfcore_grok_prxfpreg (abfd
, note
)
6145 Elf_Internal_Note
*note
;
6147 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6150 #if defined (HAVE_PRPSINFO_T)
6151 typedef prpsinfo_t elfcore_psinfo_t
;
6152 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6153 typedef prpsinfo32_t elfcore_psinfo32_t
;
6157 #if defined (HAVE_PSINFO_T)
6158 typedef psinfo_t elfcore_psinfo_t
;
6159 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6160 typedef psinfo32_t elfcore_psinfo32_t
;
6164 /* return a malloc'ed copy of a string at START which is at
6165 most MAX bytes long, possibly without a terminating '\0'.
6166 the copy will always have a terminating '\0'. */
6169 _bfd_elfcore_strndup (abfd
, start
, max
)
6175 char *end
= memchr (start
, '\0', max
);
6183 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6187 memcpy (dups
, start
, len
);
6193 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6194 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6197 elfcore_grok_psinfo (abfd
, note
)
6199 Elf_Internal_Note
*note
;
6201 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6203 elfcore_psinfo_t psinfo
;
6205 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6207 elf_tdata (abfd
)->core_program
6208 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6209 sizeof (psinfo
.pr_fname
));
6211 elf_tdata (abfd
)->core_command
6212 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6213 sizeof (psinfo
.pr_psargs
));
6215 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6216 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6218 /* 64-bit host, 32-bit corefile */
6219 elfcore_psinfo32_t psinfo
;
6221 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6223 elf_tdata (abfd
)->core_program
6224 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6225 sizeof (psinfo
.pr_fname
));
6227 elf_tdata (abfd
)->core_command
6228 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6229 sizeof (psinfo
.pr_psargs
));
6235 /* Fail - we don't know how to handle any other
6236 note size (ie. data object type). */
6240 /* Note that for some reason, a spurious space is tacked
6241 onto the end of the args in some (at least one anyway)
6242 implementations, so strip it off if it exists. */
6245 char *command
= elf_tdata (abfd
)->core_command
;
6246 int n
= strlen (command
);
6248 if (0 < n
&& command
[n
- 1] == ' ')
6249 command
[n
- 1] = '\0';
6254 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6256 #if defined (HAVE_PSTATUS_T)
6257 static boolean elfcore_grok_pstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6260 elfcore_grok_pstatus (abfd
, note
)
6262 Elf_Internal_Note
*note
;
6264 if (note
->descsz
== sizeof (pstatus_t
)
6265 #if defined (HAVE_PXSTATUS_T)
6266 || note
->descsz
== sizeof (pxstatus_t
)
6272 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6274 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6276 #if defined (HAVE_PSTATUS32_T)
6277 else if (note
->descsz
== sizeof (pstatus32_t
))
6279 /* 64-bit host, 32-bit corefile */
6282 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6284 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6287 /* Could grab some more details from the "representative"
6288 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6289 NT_LWPSTATUS note, presumably. */
6293 #endif /* defined (HAVE_PSTATUS_T) */
6295 #if defined (HAVE_LWPSTATUS_T)
6296 static boolean elfcore_grok_lwpstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
6299 elfcore_grok_lwpstatus (abfd
, note
)
6301 Elf_Internal_Note
*note
;
6303 lwpstatus_t lwpstat
;
6308 if (note
->descsz
!= sizeof (lwpstat
)
6309 #if defined (HAVE_LWPXSTATUS_T)
6310 && note
->descsz
!= sizeof (lwpxstatus_t
)
6315 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6317 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6318 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6320 /* Make a ".reg/999" section. */
6322 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6323 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6328 sect
= bfd_make_section (abfd
, name
);
6332 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6333 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6334 sect
->filepos
= note
->descpos
6335 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6338 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6339 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6340 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6343 sect
->flags
= SEC_HAS_CONTENTS
;
6344 sect
->alignment_power
= 2;
6346 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6349 /* Make a ".reg2/999" section */
6351 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6352 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6357 sect
= bfd_make_section (abfd
, name
);
6361 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6362 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6363 sect
->filepos
= note
->descpos
6364 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6367 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6368 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6369 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6372 sect
->flags
= SEC_HAS_CONTENTS
;
6373 sect
->alignment_power
= 2;
6375 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6377 #endif /* defined (HAVE_LWPSTATUS_T) */
6379 #if defined (HAVE_WIN32_PSTATUS_T)
6381 elfcore_grok_win32pstatus (abfd
, note
)
6383 Elf_Internal_Note
*note
;
6388 win32_pstatus_t pstatus
;
6390 if (note
->descsz
< sizeof (pstatus
))
6393 memcpy (&pstatus
, note
->descdata
, sizeof (pstatus
));
6395 switch (pstatus
.data_type
)
6397 case NOTE_INFO_PROCESS
:
6398 /* FIXME: need to add ->core_command. */
6399 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6400 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6403 case NOTE_INFO_THREAD
:
6404 /* Make a ".reg/999" section. */
6405 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6407 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6413 sect
= bfd_make_section (abfd
, name
);
6417 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6418 sect
->filepos
= (note
->descpos
6419 + offsetof (struct win32_pstatus
,
6420 data
.thread_info
.thread_context
));
6421 sect
->flags
= SEC_HAS_CONTENTS
;
6422 sect
->alignment_power
= 2;
6424 if (pstatus
.data
.thread_info
.is_active_thread
)
6425 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6429 case NOTE_INFO_MODULE
:
6430 /* Make a ".module/xxxxxxxx" section. */
6431 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6433 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6439 sect
= bfd_make_section (abfd
, name
);
6444 sect
->_raw_size
= note
->descsz
;
6445 sect
->filepos
= note
->descpos
;
6446 sect
->flags
= SEC_HAS_CONTENTS
;
6447 sect
->alignment_power
= 2;
6456 #endif /* HAVE_WIN32_PSTATUS_T */
6459 elfcore_grok_note (abfd
, note
)
6461 Elf_Internal_Note
*note
;
6463 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6471 if (bed
->elf_backend_grok_prstatus
)
6472 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6474 #if defined (HAVE_PRSTATUS_T)
6475 return elfcore_grok_prstatus (abfd
, note
);
6480 #if defined (HAVE_PSTATUS_T)
6482 return elfcore_grok_pstatus (abfd
, note
);
6485 #if defined (HAVE_LWPSTATUS_T)
6487 return elfcore_grok_lwpstatus (abfd
, note
);
6490 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6491 return elfcore_grok_prfpreg (abfd
, note
);
6493 #if defined (HAVE_WIN32_PSTATUS_T)
6494 case NT_WIN32PSTATUS
:
6495 return elfcore_grok_win32pstatus (abfd
, note
);
6498 case NT_PRXFPREG
: /* Linux SSE extension */
6499 if (note
->namesz
== 5
6500 && ! strcmp (note
->namedata
, "LINUX"))
6501 return elfcore_grok_prxfpreg (abfd
, note
);
6507 if (bed
->elf_backend_grok_psinfo
)
6508 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6510 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6511 return elfcore_grok_psinfo (abfd
, note
);
6519 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6520 Elf_Internal_Note
*note
;
6525 cp
= strchr (note
->namedata
, '@');
6528 *lwpidp
= atoi(cp
+ 1);
6535 elfcore_grok_netbsd_procinfo (abfd
, note
)
6537 Elf_Internal_Note
*note
;
6540 /* Signal number at offset 0x08. */
6541 elf_tdata (abfd
)->core_signal
6542 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6544 /* Process ID at offset 0x50. */
6545 elf_tdata (abfd
)->core_pid
6546 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6548 /* Command name at 0x7c (max 32 bytes, including nul). */
6549 elf_tdata (abfd
)->core_command
6550 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6556 elfcore_grok_netbsd_note (abfd
, note
)
6558 Elf_Internal_Note
*note
;
6562 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6563 elf_tdata (abfd
)->core_lwpid
= lwp
;
6565 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6567 /* NetBSD-specific core "procinfo". Note that we expect to
6568 find this note before any of the others, which is fine,
6569 since the kernel writes this note out first when it
6570 creates a core file. */
6572 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6575 /* As of Jan 2002 there are no other machine-independent notes
6576 defined for NetBSD core files. If the note type is less
6577 than the start of the machine-dependent note types, we don't
6580 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6584 switch (bfd_get_arch (abfd
))
6586 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6587 PT_GETFPREGS == mach+2. */
6589 case bfd_arch_alpha
:
6590 case bfd_arch_sparc
:
6593 case NT_NETBSDCORE_FIRSTMACH
+0:
6594 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6596 case NT_NETBSDCORE_FIRSTMACH
+2:
6597 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6603 /* On all other arch's, PT_GETREGS == mach+1 and
6604 PT_GETFPREGS == mach+3. */
6609 case NT_NETBSDCORE_FIRSTMACH
+1:
6610 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6612 case NT_NETBSDCORE_FIRSTMACH
+3:
6613 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6622 /* Function: elfcore_write_note
6629 size of data for note
6632 End of buffer containing note. */
6635 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6644 Elf_External_Note
*xnp
;
6645 int namesz
= strlen (name
);
6646 int newspace
= BFD_ALIGN (sizeof (Elf_External_Note
) + size
+ namesz
- 1, 4);
6649 p
= realloc (buf
, *bufsiz
+ newspace
);
6651 *bufsiz
+= newspace
;
6652 xnp
= (Elf_External_Note
*) dest
;
6653 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6654 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6655 H_PUT_32 (abfd
, type
, xnp
->type
);
6656 strcpy (xnp
->name
, name
);
6657 memcpy (xnp
->name
+ BFD_ALIGN (namesz
, 4), input
, size
);
6661 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6663 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6671 char *note_name
= "CORE";
6673 #if defined (HAVE_PSINFO_T)
6675 note_type
= NT_PSINFO
;
6678 note_type
= NT_PRPSINFO
;
6681 memset (&data
, 0, sizeof (data
));
6682 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6683 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6684 return elfcore_write_note (abfd
, buf
, bufsiz
,
6685 note_name
, note_type
, &data
, sizeof (data
));
6687 #endif /* PSINFO_T or PRPSINFO_T */
6689 #if defined (HAVE_PRSTATUS_T)
6691 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6700 char *note_name
= "CORE";
6702 memset (&prstat
, 0, sizeof (prstat
));
6703 prstat
.pr_pid
= pid
;
6704 prstat
.pr_cursig
= cursig
;
6705 memcpy (&prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
6706 return elfcore_write_note (abfd
, buf
, bufsiz
,
6707 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
6709 #endif /* HAVE_PRSTATUS_T */
6711 #if defined (HAVE_LWPSTATUS_T)
6713 elfcore_write_lwpstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6721 lwpstatus_t lwpstat
;
6722 char *note_name
= "CORE";
6724 memset (&lwpstat
, 0, sizeof (lwpstat
));
6725 lwpstat
.pr_lwpid
= pid
>> 16;
6726 lwpstat
.pr_cursig
= cursig
;
6727 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6728 memcpy (lwpstat
.pr_reg
, gregs
, sizeof (lwpstat
.pr_reg
));
6729 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6731 memcpy (lwpstat
.pr_context
.uc_mcontext
.gregs
,
6732 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
));
6734 memcpy (lwpstat
.pr_context
.uc_mcontext
.__gregs
,
6735 gregs
, sizeof (lwpstat
.pr_context
.uc_mcontext
.__gregs
));
6738 return elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6739 NT_LWPSTATUS
, &lwpstat
, sizeof (lwpstat
));
6741 #endif /* HAVE_LWPSTATUS_T */
6743 #if defined (HAVE_PSTATUS_T)
6745 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6754 char *note_name
= "CORE";
6756 memset (&pstat
, 0, sizeof (pstat
));
6757 pstat
.pr_pid
= pid
& 0xffff;
6758 buf
= elfcore_write_note (abfd
, buf
, bufsiz
, note_name
,
6759 NT_PSTATUS
, &pstat
, sizeof (pstat
));
6762 #endif /* HAVE_PSTATUS_T */
6765 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
6772 char *note_name
= "CORE";
6773 return elfcore_write_note (abfd
, buf
, bufsiz
,
6774 note_name
, NT_FPREGSET
, fpregs
, size
);
6778 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
6785 char *note_name
= "LINUX";
6786 return elfcore_write_note (abfd
, buf
, bufsiz
,
6787 note_name
, NT_PRXFPREG
, xfpregs
, size
);
6791 elfcore_read_notes (abfd
, offset
, size
)
6802 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6805 buf
= bfd_malloc (size
);
6809 if (bfd_bread (buf
, size
, abfd
) != size
)
6817 while (p
< buf
+ size
)
6819 /* FIXME: bad alignment assumption. */
6820 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6821 Elf_Internal_Note in
;
6823 in
.type
= H_GET_32 (abfd
, xnp
->type
);
6825 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
6826 in
.namedata
= xnp
->name
;
6828 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
6829 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
6830 in
.descpos
= offset
+ (in
.descdata
- buf
);
6832 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
6834 if (! elfcore_grok_netbsd_note (abfd
, &in
))
6839 if (! elfcore_grok_note (abfd
, &in
))
6843 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
6850 /* Providing external access to the ELF program header table. */
6852 /* Return an upper bound on the number of bytes required to store a
6853 copy of ABFD's program header table entries. Return -1 if an error
6854 occurs; bfd_get_error will return an appropriate code. */
6857 bfd_get_elf_phdr_upper_bound (abfd
)
6860 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6862 bfd_set_error (bfd_error_wrong_format
);
6866 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
6869 /* Copy ABFD's program header table entries to *PHDRS. The entries
6870 will be stored as an array of Elf_Internal_Phdr structures, as
6871 defined in include/elf/internal.h. To find out how large the
6872 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6874 Return the number of program header table entries read, or -1 if an
6875 error occurs; bfd_get_error will return an appropriate code. */
6878 bfd_get_elf_phdrs (abfd
, phdrs
)
6884 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6886 bfd_set_error (bfd_error_wrong_format
);
6890 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
6891 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
6892 num_phdrs
* sizeof (Elf_Internal_Phdr
));
6898 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
6899 bfd
*abfd ATTRIBUTE_UNUSED
;
6904 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6906 i_ehdrp
= elf_elfheader (abfd
);
6907 if (i_ehdrp
== NULL
)
6908 sprintf_vma (buf
, value
);
6911 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6913 #if BFD_HOST_64BIT_LONG
6914 sprintf (buf
, "%016lx", value
);
6916 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
6917 _bfd_int64_low (value
));
6921 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
6924 sprintf_vma (buf
, value
);
6929 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
6930 bfd
*abfd ATTRIBUTE_UNUSED
;
6935 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6937 i_ehdrp
= elf_elfheader (abfd
);
6938 if (i_ehdrp
== NULL
)
6939 fprintf_vma ((FILE *) stream
, value
);
6942 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6944 #if BFD_HOST_64BIT_LONG
6945 fprintf ((FILE *) stream
, "%016lx", value
);
6947 fprintf ((FILE *) stream
, "%08lx%08lx",
6948 _bfd_int64_high (value
), _bfd_int64_low (value
));
6952 fprintf ((FILE *) stream
, "%08lx",
6953 (unsigned long) (value
& 0xffffffff));
6956 fprintf_vma ((FILE *) stream
, value
);
6960 enum elf_reloc_type_class
6961 _bfd_elf_reloc_type_class (rela
)
6962 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
6964 return reloc_class_normal
;
6967 /* For RELA architectures, return what the relocation value for
6968 relocation against a local symbol. */
6971 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
6973 Elf_Internal_Sym
*sym
;
6975 Elf_Internal_Rela
*rel
;
6979 relocation
= (sec
->output_section
->vma
6980 + sec
->output_offset
6982 if ((sec
->flags
& SEC_MERGE
)
6983 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
6984 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6990 _bfd_merged_section_offset (abfd
, &msec
,
6991 elf_section_data (sec
)->sec_info
,
6992 sym
->st_value
+ rel
->r_addend
,
6995 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
7001 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
7003 Elf_Internal_Sym
*sym
;
7007 asection
*sec
= *psec
;
7009 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
7010 return sym
->st_value
+ addend
;
7012 return _bfd_merged_section_offset (abfd
, psec
,
7013 elf_section_data (sec
)->sec_info
,
7014 sym
->st_value
+ addend
, (bfd_vma
) 0);
7018 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
7020 struct bfd_link_info
*info
;
7024 struct bfd_elf_section_data
*sec_data
;
7026 sec_data
= elf_section_data (sec
);
7027 switch (sec_data
->sec_info_type
)
7029 case ELF_INFO_TYPE_STABS
:
7030 return _bfd_stab_section_offset
7031 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
7033 case ELF_INFO_TYPE_EH_FRAME
:
7034 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);