1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
35 /* For sparc64-cross-sparc32. */
43 #include "libiberty.h"
45 static INLINE
struct elf_segment_map
*make_mapping
46 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
47 static boolean map_sections_to_segments
PARAMS ((bfd
*));
48 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
49 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
50 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
51 static boolean prep_headers
PARAMS ((bfd
*));
52 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
53 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
54 static char *elf_read
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
55 static boolean setup_group
PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
56 static void merge_sections_remove_hook
PARAMS ((bfd
*, asection
*));
57 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
58 static void set_group_contents
PARAMS ((bfd
*, asection
*, PTR
));
59 static boolean assign_section_numbers
PARAMS ((bfd
*));
60 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
61 static boolean elf_map_symbols
PARAMS ((bfd
*));
62 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
63 static boolean elfcore_read_notes
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
64 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
65 bfd_vma
, const char **,
67 static int elfcore_make_pid
PARAMS ((bfd
*));
68 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
69 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
70 Elf_Internal_Note
*));
71 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
72 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
73 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
75 static boolean elfcore_netbsd_get_lwpid
PARAMS ((Elf_Internal_Note
*, int *));
76 static boolean elfcore_grok_netbsd_procinfo
PARAMS ((bfd
*,
77 Elf_Internal_Note
*));
78 static boolean elfcore_grok_netbsd_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
80 /* Swap version information in and out. The version information is
81 currently size independent. If that ever changes, this code will
82 need to move into elfcode.h. */
84 /* Swap in a Verdef structure. */
87 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
89 const Elf_External_Verdef
*src
;
90 Elf_Internal_Verdef
*dst
;
92 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
93 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
94 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
95 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
96 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
97 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
98 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
101 /* Swap out a Verdef structure. */
104 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
106 const Elf_Internal_Verdef
*src
;
107 Elf_External_Verdef
*dst
;
109 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
110 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
111 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
112 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
113 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
114 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
115 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
118 /* Swap in a Verdaux structure. */
121 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
123 const Elf_External_Verdaux
*src
;
124 Elf_Internal_Verdaux
*dst
;
126 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
127 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
130 /* Swap out a Verdaux structure. */
133 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
135 const Elf_Internal_Verdaux
*src
;
136 Elf_External_Verdaux
*dst
;
138 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
139 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
142 /* Swap in a Verneed structure. */
145 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
147 const Elf_External_Verneed
*src
;
148 Elf_Internal_Verneed
*dst
;
150 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
151 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
152 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
153 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
154 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
157 /* Swap out a Verneed structure. */
160 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
162 const Elf_Internal_Verneed
*src
;
163 Elf_External_Verneed
*dst
;
165 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
166 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
167 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
168 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
169 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
172 /* Swap in a Vernaux structure. */
175 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
177 const Elf_External_Vernaux
*src
;
178 Elf_Internal_Vernaux
*dst
;
180 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
181 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
182 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
183 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
184 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
187 /* Swap out a Vernaux structure. */
190 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
192 const Elf_Internal_Vernaux
*src
;
193 Elf_External_Vernaux
*dst
;
195 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
196 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
197 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
198 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
199 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
202 /* Swap in a Versym structure. */
205 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
207 const Elf_External_Versym
*src
;
208 Elf_Internal_Versym
*dst
;
210 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
213 /* Swap out a Versym structure. */
216 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
218 const Elf_Internal_Versym
*src
;
219 Elf_External_Versym
*dst
;
221 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
224 /* Standard ELF hash function. Do not change this function; you will
225 cause invalid hash tables to be generated. */
228 bfd_elf_hash (namearg
)
231 const unsigned char *name
= (const unsigned char *) namearg
;
236 while ((ch
= *name
++) != '\0')
239 if ((g
= (h
& 0xf0000000)) != 0)
242 /* The ELF ABI says `h &= ~g', but this is equivalent in
243 this case and on some machines one insn instead of two. */
250 /* Read a specified number of bytes at a specified offset in an ELF
251 file, into a newly allocated buffer, and return a pointer to the
255 elf_read (abfd
, offset
, size
)
262 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
264 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
266 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
268 if (bfd_get_error () != bfd_error_system_call
)
269 bfd_set_error (bfd_error_file_truncated
);
276 bfd_elf_mkobject (abfd
)
279 /* This just does initialization. */
280 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
281 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
282 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
283 if (elf_tdata (abfd
) == 0)
285 /* Since everything is done at close time, do we need any
292 bfd_elf_mkcorefile (abfd
)
295 /* I think this can be done just like an object file. */
296 return bfd_elf_mkobject (abfd
);
300 bfd_elf_get_str_section (abfd
, shindex
)
302 unsigned int shindex
;
304 Elf_Internal_Shdr
**i_shdrp
;
305 char *shstrtab
= NULL
;
307 bfd_size_type shstrtabsize
;
309 i_shdrp
= elf_elfsections (abfd
);
310 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
313 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
314 if (shstrtab
== NULL
)
316 /* No cached one, attempt to read, and cache what we read. */
317 offset
= i_shdrp
[shindex
]->sh_offset
;
318 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
319 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
320 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
326 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
328 unsigned int shindex
;
329 unsigned int strindex
;
331 Elf_Internal_Shdr
*hdr
;
336 hdr
= elf_elfsections (abfd
)[shindex
];
338 if (hdr
->contents
== NULL
339 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
342 if (strindex
>= hdr
->sh_size
)
344 (*_bfd_error_handler
)
345 (_("%s: invalid string offset %u >= %lu for section `%s'"),
346 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
347 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
348 && strindex
== hdr
->sh_name
)
350 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
354 return ((char *) hdr
->contents
) + strindex
;
357 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
358 sections. The first element is the flags, the rest are section
361 typedef union elf_internal_group
{
362 Elf_Internal_Shdr
*shdr
;
364 } Elf_Internal_Group
;
366 /* Set next_in_group list pointer, and group name for NEWSECT. */
369 setup_group (abfd
, hdr
, newsect
)
371 Elf_Internal_Shdr
*hdr
;
374 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
376 /* If num_group is zero, read in all SHT_GROUP sections. The count
377 is set to -1 if there are no SHT_GROUP sections. */
380 unsigned int i
, shnum
;
382 /* First count the number of groups. If we have a SHT_GROUP
383 section with just a flag word (ie. sh_size is 4), ignore it. */
384 shnum
= elf_numsections (abfd
);
386 for (i
= 0; i
< shnum
; i
++)
388 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
389 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
394 num_group
= (unsigned) -1;
395 elf_tdata (abfd
)->num_group
= num_group
;
399 /* We keep a list of elf section headers for group sections,
400 so we can find them quickly. */
401 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
402 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
403 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
407 for (i
= 0; i
< shnum
; i
++)
409 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
410 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
413 Elf_Internal_Group
*dest
;
415 /* Add to list of sections. */
416 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
419 /* Read the raw contents. */
420 BFD_ASSERT (sizeof (*dest
) >= 4);
421 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
422 shdr
->contents
= bfd_alloc (abfd
, amt
);
423 if (shdr
->contents
== NULL
424 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
425 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
429 /* Translate raw contents, a flag word followed by an
430 array of elf section indices all in target byte order,
431 to the flag word followed by an array of elf section
433 src
= shdr
->contents
+ shdr
->sh_size
;
434 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
441 idx
= H_GET_32 (abfd
, src
);
442 if (src
== shdr
->contents
)
449 ((*_bfd_error_handler
)
450 (_("%s: invalid SHT_GROUP entry"),
451 bfd_archive_filename (abfd
)));
454 dest
->shdr
= elf_elfsections (abfd
)[idx
];
461 if (num_group
!= (unsigned) -1)
465 for (i
= 0; i
< num_group
; i
++)
467 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
468 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
469 unsigned int n_elt
= shdr
->sh_size
/ 4;
471 /* Look through this group's sections to see if current
472 section is a member. */
474 if ((++idx
)->shdr
== hdr
)
478 /* We are a member of this group. Go looking through
479 other members to see if any others are linked via
481 idx
= (Elf_Internal_Group
*) shdr
->contents
;
482 n_elt
= shdr
->sh_size
/ 4;
484 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
485 && elf_next_in_group (s
) != NULL
)
489 /* Snarf the group name from other member, and
490 insert current section in circular list. */
491 elf_group_name (newsect
) = elf_group_name (s
);
492 elf_next_in_group (newsect
) = elf_next_in_group (s
);
493 elf_next_in_group (s
) = newsect
;
497 struct elf_backend_data
*bed
;
499 unsigned char ename
[4];
503 /* Humbug. Get the name from the group signature
504 symbol. Why isn't the signature just a string?
505 Fortunately, the name index is at the same
506 place in the external symbol for both 32 and 64
508 bed
= get_elf_backend_data (abfd
);
509 pos
= elf_tdata (abfd
)->symtab_hdr
.sh_offset
;
510 pos
+= shdr
->sh_info
* bed
->s
->sizeof_sym
;
511 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
512 || bfd_bread (ename
, (bfd_size_type
) 4, abfd
) != 4)
514 iname
= H_GET_32 (abfd
, ename
);
515 gname
= elf_string_from_elf_strtab (abfd
, iname
);
516 elf_group_name (newsect
) = gname
;
518 /* Start a circular list with one element. */
519 elf_next_in_group (newsect
) = newsect
;
521 if (shdr
->bfd_section
!= NULL
)
522 elf_next_in_group (shdr
->bfd_section
) = newsect
;
529 if (elf_group_name (newsect
) == NULL
)
531 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
532 bfd_archive_filename (abfd
), newsect
->name
);
537 /* Make a BFD section from an ELF section. We store a pointer to the
538 BFD section in the bfd_section field of the header. */
541 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
543 Elf_Internal_Shdr
*hdr
;
548 struct elf_backend_data
*bed
;
550 if (hdr
->bfd_section
!= NULL
)
552 BFD_ASSERT (strcmp (name
,
553 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
557 newsect
= bfd_make_section_anyway (abfd
, name
);
561 newsect
->filepos
= hdr
->sh_offset
;
563 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
564 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
565 || ! bfd_set_section_alignment (abfd
, newsect
,
566 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
569 flags
= SEC_NO_FLAGS
;
570 if (hdr
->sh_type
!= SHT_NOBITS
)
571 flags
|= SEC_HAS_CONTENTS
;
572 if (hdr
->sh_type
== SHT_GROUP
)
573 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
574 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
577 if (hdr
->sh_type
!= SHT_NOBITS
)
580 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
581 flags
|= SEC_READONLY
;
582 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
584 else if ((flags
& SEC_LOAD
) != 0)
586 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
589 newsect
->entsize
= hdr
->sh_entsize
;
590 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
591 flags
|= SEC_STRINGS
;
593 if (hdr
->sh_flags
& SHF_GROUP
)
594 if (!setup_group (abfd
, hdr
, newsect
))
597 /* The debugging sections appear to be recognized only by name, not
600 static const char *debug_sec_names
[] =
609 for (i
= ARRAY_SIZE (debug_sec_names
); i
--;)
610 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
614 flags
|= SEC_DEBUGGING
;
617 /* As a GNU extension, if the name begins with .gnu.linkonce, we
618 only link a single copy of the section. This is used to support
619 g++. g++ will emit each template expansion in its own section.
620 The symbols will be defined as weak, so that multiple definitions
621 are permitted. The GNU linker extension is to actually discard
622 all but one of the sections. */
623 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
624 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
626 bed
= get_elf_backend_data (abfd
);
627 if (bed
->elf_backend_section_flags
)
628 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
631 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
634 if ((flags
& SEC_ALLOC
) != 0)
636 Elf_Internal_Phdr
*phdr
;
639 /* Look through the phdrs to see if we need to adjust the lma.
640 If all the p_paddr fields are zero, we ignore them, since
641 some ELF linkers produce such output. */
642 phdr
= elf_tdata (abfd
)->phdr
;
643 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
645 if (phdr
->p_paddr
!= 0)
648 if (i
< elf_elfheader (abfd
)->e_phnum
)
650 phdr
= elf_tdata (abfd
)->phdr
;
651 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
653 /* This section is part of this segment if its file
654 offset plus size lies within the segment's memory
655 span and, if the section is loaded, the extent of the
656 loaded data lies within the extent of the segment.
657 If the p_paddr field is not set, we don't alter the
659 if (phdr
->p_type
== PT_LOAD
661 && (bfd_vma
) hdr
->sh_offset
>= phdr
->p_offset
662 && (hdr
->sh_offset
+ hdr
->sh_size
663 <= phdr
->p_offset
+ phdr
->p_memsz
)
664 && ((flags
& SEC_LOAD
) == 0
665 || (phdr
->p_offset
+ phdr
->p_filesz
666 >= hdr
->sh_offset
+ hdr
->sh_size
)))
668 /* We used to do a relative adjustment here, but
669 that doesn't work if the segment is packed with
670 code from multiple VMAs. Instead we calculate
671 the LMA absoultely, based on the LMA of the
672 segment (it is assumed that the segment will
673 contain sections with contiguous LMAs, even if
674 the VMAs are not). */
675 newsect
->lma
= phdr
->p_paddr
676 + hdr
->sh_offset
- phdr
->p_offset
;
683 hdr
->bfd_section
= newsect
;
684 elf_section_data (newsect
)->this_hdr
= *hdr
;
694 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
697 Helper functions for GDB to locate the string tables.
698 Since BFD hides string tables from callers, GDB needs to use an
699 internal hook to find them. Sun's .stabstr, in particular,
700 isn't even pointed to by the .stab section, so ordinary
701 mechanisms wouldn't work to find it, even if we had some.
704 struct elf_internal_shdr
*
705 bfd_elf_find_section (abfd
, name
)
709 Elf_Internal_Shdr
**i_shdrp
;
714 i_shdrp
= elf_elfsections (abfd
);
717 shstrtab
= bfd_elf_get_str_section (abfd
,
718 elf_elfheader (abfd
)->e_shstrndx
);
719 if (shstrtab
!= NULL
)
721 max
= elf_numsections (abfd
);
722 for (i
= 1; i
< max
; i
++)
723 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
730 const char *const bfd_elf_section_type_names
[] = {
731 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
732 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
733 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
736 /* ELF relocs are against symbols. If we are producing relocateable
737 output, and the reloc is against an external symbol, and nothing
738 has given us any additional addend, the resulting reloc will also
739 be against the same symbol. In such a case, we don't want to
740 change anything about the way the reloc is handled, since it will
741 all be done at final link time. Rather than put special case code
742 into bfd_perform_relocation, all the reloc types use this howto
743 function. It just short circuits the reloc if producing
744 relocateable output against an external symbol. */
746 bfd_reloc_status_type
747 bfd_elf_generic_reloc (abfd
,
754 bfd
*abfd ATTRIBUTE_UNUSED
;
755 arelent
*reloc_entry
;
757 PTR data ATTRIBUTE_UNUSED
;
758 asection
*input_section
;
760 char **error_message ATTRIBUTE_UNUSED
;
762 if (output_bfd
!= (bfd
*) NULL
763 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
764 && (! reloc_entry
->howto
->partial_inplace
765 || reloc_entry
->addend
== 0))
767 reloc_entry
->address
+= input_section
->output_offset
;
771 return bfd_reloc_continue
;
774 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
777 merge_sections_remove_hook (abfd
, sec
)
778 bfd
*abfd ATTRIBUTE_UNUSED
;
781 struct bfd_elf_section_data
*sec_data
;
783 sec_data
= elf_section_data (sec
);
784 BFD_ASSERT (sec_data
->sec_info_type
== ELF_INFO_TYPE_MERGE
);
785 sec_data
->sec_info_type
= ELF_INFO_TYPE_NONE
;
788 /* Finish SHF_MERGE section merging. */
791 _bfd_elf_merge_sections (abfd
, info
)
793 struct bfd_link_info
*info
;
795 if (!is_elf_hash_table (info
))
797 if (elf_hash_table (info
)->merge_info
)
798 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
,
799 merge_sections_remove_hook
);
803 /* Copy the program header and other data from one object module to
807 _bfd_elf_copy_private_bfd_data (ibfd
, obfd
)
811 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
812 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
815 BFD_ASSERT (!elf_flags_init (obfd
)
816 || (elf_elfheader (obfd
)->e_flags
817 == elf_elfheader (ibfd
)->e_flags
));
819 elf_gp (obfd
) = elf_gp (ibfd
);
820 elf_elfheader (obfd
)->e_flags
= elf_elfheader (ibfd
)->e_flags
;
821 elf_flags_init (obfd
) = true;
825 /* Print out the program headers. */
828 _bfd_elf_print_private_bfd_data (abfd
, farg
)
832 FILE *f
= (FILE *) farg
;
833 Elf_Internal_Phdr
*p
;
835 bfd_byte
*dynbuf
= NULL
;
837 p
= elf_tdata (abfd
)->phdr
;
842 fprintf (f
, _("\nProgram Header:\n"));
843 c
= elf_elfheader (abfd
)->e_phnum
;
844 for (i
= 0; i
< c
; i
++, p
++)
851 case PT_NULL
: pt
= "NULL"; break;
852 case PT_LOAD
: pt
= "LOAD"; break;
853 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
854 case PT_INTERP
: pt
= "INTERP"; break;
855 case PT_NOTE
: pt
= "NOTE"; break;
856 case PT_SHLIB
: pt
= "SHLIB"; break;
857 case PT_PHDR
: pt
= "PHDR"; break;
858 case PT_GNU_EH_FRAME
: pt
= "EH_FRAME"; break;
859 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
861 fprintf (f
, "%8s off 0x", pt
);
862 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
863 fprintf (f
, " vaddr 0x");
864 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
865 fprintf (f
, " paddr 0x");
866 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
867 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
868 fprintf (f
, " filesz 0x");
869 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
870 fprintf (f
, " memsz 0x");
871 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
872 fprintf (f
, " flags %c%c%c",
873 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
874 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
875 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
876 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
877 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
882 s
= bfd_get_section_by_name (abfd
, ".dynamic");
886 unsigned long shlink
;
887 bfd_byte
*extdyn
, *extdynend
;
889 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
891 fprintf (f
, _("\nDynamic Section:\n"));
893 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
896 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
900 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
903 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
905 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
906 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
909 extdynend
= extdyn
+ s
->_raw_size
;
910 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
912 Elf_Internal_Dyn dyn
;
917 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
919 if (dyn
.d_tag
== DT_NULL
)
926 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
930 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
931 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
932 case DT_PLTGOT
: name
= "PLTGOT"; break;
933 case DT_HASH
: name
= "HASH"; break;
934 case DT_STRTAB
: name
= "STRTAB"; break;
935 case DT_SYMTAB
: name
= "SYMTAB"; break;
936 case DT_RELA
: name
= "RELA"; break;
937 case DT_RELASZ
: name
= "RELASZ"; break;
938 case DT_RELAENT
: name
= "RELAENT"; break;
939 case DT_STRSZ
: name
= "STRSZ"; break;
940 case DT_SYMENT
: name
= "SYMENT"; break;
941 case DT_INIT
: name
= "INIT"; break;
942 case DT_FINI
: name
= "FINI"; break;
943 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
944 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
945 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
946 case DT_REL
: name
= "REL"; break;
947 case DT_RELSZ
: name
= "RELSZ"; break;
948 case DT_RELENT
: name
= "RELENT"; break;
949 case DT_PLTREL
: name
= "PLTREL"; break;
950 case DT_DEBUG
: name
= "DEBUG"; break;
951 case DT_TEXTREL
: name
= "TEXTREL"; break;
952 case DT_JMPREL
: name
= "JMPREL"; break;
953 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
954 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
955 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
956 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
957 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
958 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
959 case DT_FLAGS
: name
= "FLAGS"; break;
960 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
961 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
962 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
963 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
964 case DT_MOVEENT
: name
= "MOVEENT"; break;
965 case DT_MOVESZ
: name
= "MOVESZ"; break;
966 case DT_FEATURE
: name
= "FEATURE"; break;
967 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
968 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
969 case DT_SYMINENT
: name
= "SYMINENT"; break;
970 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
971 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
972 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
973 case DT_PLTPAD
: name
= "PLTPAD"; break;
974 case DT_MOVETAB
: name
= "MOVETAB"; break;
975 case DT_SYMINFO
: name
= "SYMINFO"; break;
976 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
977 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
978 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
979 case DT_VERSYM
: name
= "VERSYM"; break;
980 case DT_VERDEF
: name
= "VERDEF"; break;
981 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
982 case DT_VERNEED
: name
= "VERNEED"; break;
983 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
984 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
985 case DT_USED
: name
= "USED"; break;
986 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
989 fprintf (f
, " %-11s ", name
);
991 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
995 unsigned int tagv
= dyn
.d_un
.d_val
;
997 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1000 fprintf (f
, "%s", string
);
1009 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
1010 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
1012 if (! _bfd_elf_slurp_version_tables (abfd
))
1016 if (elf_dynverdef (abfd
) != 0)
1018 Elf_Internal_Verdef
*t
;
1020 fprintf (f
, _("\nVersion definitions:\n"));
1021 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
1023 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
1024 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
1025 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
1027 Elf_Internal_Verdaux
*a
;
1030 for (a
= t
->vd_auxptr
->vda_nextptr
;
1033 fprintf (f
, "%s ", a
->vda_nodename
);
1039 if (elf_dynverref (abfd
) != 0)
1041 Elf_Internal_Verneed
*t
;
1043 fprintf (f
, _("\nVersion References:\n"));
1044 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
1046 Elf_Internal_Vernaux
*a
;
1048 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
1049 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1050 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
1051 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1063 /* Display ELF-specific fields of a symbol. */
1066 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1070 bfd_print_symbol_type how
;
1072 FILE *file
= (FILE *) filep
;
1075 case bfd_print_symbol_name
:
1076 fprintf (file
, "%s", symbol
->name
);
1078 case bfd_print_symbol_more
:
1079 fprintf (file
, "elf ");
1080 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1081 fprintf (file
, " %lx", (long) symbol
->flags
);
1083 case bfd_print_symbol_all
:
1085 const char *section_name
;
1086 const char *name
= NULL
;
1087 struct elf_backend_data
*bed
;
1088 unsigned char st_other
;
1091 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1093 bed
= get_elf_backend_data (abfd
);
1094 if (bed
->elf_backend_print_symbol_all
)
1095 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1099 name
= symbol
->name
;
1100 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1103 fprintf (file
, " %s\t", section_name
);
1104 /* Print the "other" value for a symbol. For common symbols,
1105 we've already printed the size; now print the alignment.
1106 For other symbols, we have no specified alignment, and
1107 we've printed the address; now print the size. */
1108 if (bfd_is_com_section (symbol
->section
))
1109 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1111 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1112 bfd_fprintf_vma (abfd
, file
, val
);
1114 /* If we have version information, print it. */
1115 if (elf_tdata (abfd
)->dynversym_section
!= 0
1116 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1117 || elf_tdata (abfd
)->dynverref_section
!= 0))
1119 unsigned int vernum
;
1120 const char *version_string
;
1122 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1125 version_string
= "";
1126 else if (vernum
== 1)
1127 version_string
= "Base";
1128 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1130 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1133 Elf_Internal_Verneed
*t
;
1135 version_string
= "";
1136 for (t
= elf_tdata (abfd
)->verref
;
1140 Elf_Internal_Vernaux
*a
;
1142 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1144 if (a
->vna_other
== vernum
)
1146 version_string
= a
->vna_nodename
;
1153 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1154 fprintf (file
, " %-11s", version_string
);
1159 fprintf (file
, " (%s)", version_string
);
1160 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1165 /* If the st_other field is not zero, print it. */
1166 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1171 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1172 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1173 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1175 /* Some other non-defined flags are also present, so print
1177 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1180 fprintf (file
, " %s", name
);
1186 /* Create an entry in an ELF linker hash table. */
1188 struct bfd_hash_entry
*
1189 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1190 struct bfd_hash_entry
*entry
;
1191 struct bfd_hash_table
*table
;
1194 /* Allocate the structure if it has not already been allocated by a
1198 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1203 /* Call the allocation method of the superclass. */
1204 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1207 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1208 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1210 /* Set local fields. */
1214 ret
->dynstr_index
= 0;
1215 ret
->weakdef
= NULL
;
1216 ret
->got
.refcount
= htab
->init_refcount
;
1217 ret
->plt
.refcount
= htab
->init_refcount
;
1218 ret
->linker_section_pointer
= NULL
;
1219 ret
->verinfo
.verdef
= NULL
;
1220 ret
->vtable_entries_used
= NULL
;
1221 ret
->vtable_entries_size
= 0;
1222 ret
->vtable_parent
= NULL
;
1223 ret
->type
= STT_NOTYPE
;
1225 /* Assume that we have been called by a non-ELF symbol reader.
1226 This flag is then reset by the code which reads an ELF input
1227 file. This ensures that a symbol created by a non-ELF symbol
1228 reader will have the flag set correctly. */
1229 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1235 /* Copy data from an indirect symbol to its direct symbol, hiding the
1236 old indirect symbol. Also used for copying flags to a weakdef. */
1239 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1240 struct elf_link_hash_entry
*dir
, *ind
;
1244 /* Copy down any references that we may have already seen to the
1245 symbol which just became indirect. */
1247 dir
->elf_link_hash_flags
|=
1248 (ind
->elf_link_hash_flags
1249 & (ELF_LINK_HASH_REF_DYNAMIC
1250 | ELF_LINK_HASH_REF_REGULAR
1251 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1252 | ELF_LINK_NON_GOT_REF
));
1254 if (ind
->root
.type
!= bfd_link_hash_indirect
)
1257 /* Copy over the global and procedure linkage table refcount entries.
1258 These may have been already set up by a check_relocs routine. */
1259 tmp
= dir
->got
.refcount
;
1262 dir
->got
.refcount
= ind
->got
.refcount
;
1263 ind
->got
.refcount
= tmp
;
1266 BFD_ASSERT (ind
->got
.refcount
<= 0);
1268 tmp
= dir
->plt
.refcount
;
1271 dir
->plt
.refcount
= ind
->plt
.refcount
;
1272 ind
->plt
.refcount
= tmp
;
1275 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1277 if (dir
->dynindx
== -1)
1279 dir
->dynindx
= ind
->dynindx
;
1280 dir
->dynstr_index
= ind
->dynstr_index
;
1282 ind
->dynstr_index
= 0;
1285 BFD_ASSERT (ind
->dynindx
== -1);
1289 _bfd_elf_link_hash_hide_symbol (info
, h
)
1290 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1291 struct elf_link_hash_entry
*h
;
1293 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1294 h
->plt
.offset
= (bfd_vma
) -1;
1295 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1299 /* Initialize an ELF linker hash table. */
1302 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1303 struct elf_link_hash_table
*table
;
1305 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1306 struct bfd_hash_table
*,
1311 table
->dynamic_sections_created
= false;
1312 table
->dynobj
= NULL
;
1313 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1314 /* The first dynamic symbol is a dummy. */
1315 table
->dynsymcount
= 1;
1316 table
->dynstr
= NULL
;
1317 table
->bucketcount
= 0;
1318 table
->needed
= NULL
;
1319 table
->runpath
= NULL
;
1321 table
->stab_info
= NULL
;
1322 table
->merge_info
= NULL
;
1323 table
->dynlocal
= NULL
;
1324 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1325 table
->root
.type
= bfd_link_elf_hash_table
;
1330 /* Create an ELF linker hash table. */
1332 struct bfd_link_hash_table
*
1333 _bfd_elf_link_hash_table_create (abfd
)
1336 struct elf_link_hash_table
*ret
;
1337 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1339 ret
= (struct elf_link_hash_table
*) bfd_alloc (abfd
, amt
);
1340 if (ret
== (struct elf_link_hash_table
*) NULL
)
1343 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1345 bfd_release (abfd
, ret
);
1352 /* This is a hook for the ELF emulation code in the generic linker to
1353 tell the backend linker what file name to use for the DT_NEEDED
1354 entry for a dynamic object. The generic linker passes name as an
1355 empty string to indicate that no DT_NEEDED entry should be made. */
1358 bfd_elf_set_dt_needed_name (abfd
, name
)
1362 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1363 && bfd_get_format (abfd
) == bfd_object
)
1364 elf_dt_name (abfd
) = name
;
1368 bfd_elf_set_dt_needed_soname (abfd
, name
)
1372 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1373 && bfd_get_format (abfd
) == bfd_object
)
1374 elf_dt_soname (abfd
) = name
;
1377 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1378 the linker ELF emulation code. */
1380 struct bfd_link_needed_list
*
1381 bfd_elf_get_needed_list (abfd
, info
)
1382 bfd
*abfd ATTRIBUTE_UNUSED
;
1383 struct bfd_link_info
*info
;
1385 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1387 return elf_hash_table (info
)->needed
;
1390 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1391 hook for the linker ELF emulation code. */
1393 struct bfd_link_needed_list
*
1394 bfd_elf_get_runpath_list (abfd
, info
)
1395 bfd
*abfd ATTRIBUTE_UNUSED
;
1396 struct bfd_link_info
*info
;
1398 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1400 return elf_hash_table (info
)->runpath
;
1403 /* Get the name actually used for a dynamic object for a link. This
1404 is the SONAME entry if there is one. Otherwise, it is the string
1405 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1408 bfd_elf_get_dt_soname (abfd
)
1411 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1412 && bfd_get_format (abfd
) == bfd_object
)
1413 return elf_dt_name (abfd
);
1417 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1418 the ELF linker emulation code. */
1421 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1423 struct bfd_link_needed_list
**pneeded
;
1426 bfd_byte
*dynbuf
= NULL
;
1428 unsigned long shlink
;
1429 bfd_byte
*extdyn
, *extdynend
;
1431 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1435 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1436 || bfd_get_format (abfd
) != bfd_object
)
1439 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1440 if (s
== NULL
|| s
->_raw_size
== 0)
1443 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1447 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1451 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1455 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1457 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1458 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1461 extdynend
= extdyn
+ s
->_raw_size
;
1462 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1464 Elf_Internal_Dyn dyn
;
1466 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1468 if (dyn
.d_tag
== DT_NULL
)
1471 if (dyn
.d_tag
== DT_NEEDED
)
1474 struct bfd_link_needed_list
*l
;
1475 unsigned int tagv
= dyn
.d_un
.d_val
;
1478 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1483 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1504 /* Allocate an ELF string table--force the first byte to be zero. */
1506 struct bfd_strtab_hash
*
1507 _bfd_elf_stringtab_init ()
1509 struct bfd_strtab_hash
*ret
;
1511 ret
= _bfd_stringtab_init ();
1516 loc
= _bfd_stringtab_add (ret
, "", true, false);
1517 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1518 if (loc
== (bfd_size_type
) -1)
1520 _bfd_stringtab_free (ret
);
1527 /* ELF .o/exec file reading */
1529 /* Create a new bfd section from an ELF section header. */
1532 bfd_section_from_shdr (abfd
, shindex
)
1534 unsigned int shindex
;
1536 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1537 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1538 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1541 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1543 switch (hdr
->sh_type
)
1546 /* Inactive section. Throw it away. */
1549 case SHT_PROGBITS
: /* Normal section with contents. */
1550 case SHT_DYNAMIC
: /* Dynamic linking information. */
1551 case SHT_NOBITS
: /* .bss section. */
1552 case SHT_HASH
: /* .hash section. */
1553 case SHT_NOTE
: /* .note section. */
1554 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1556 case SHT_SYMTAB
: /* A symbol table */
1557 if (elf_onesymtab (abfd
) == shindex
)
1560 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1561 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1562 elf_onesymtab (abfd
) = shindex
;
1563 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1564 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1565 abfd
->flags
|= HAS_SYMS
;
1567 /* Sometimes a shared object will map in the symbol table. If
1568 SHF_ALLOC is set, and this is a shared object, then we also
1569 treat this section as a BFD section. We can not base the
1570 decision purely on SHF_ALLOC, because that flag is sometimes
1571 set in a relocateable object file, which would confuse the
1573 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1574 && (abfd
->flags
& DYNAMIC
) != 0
1575 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1580 case SHT_DYNSYM
: /* A dynamic symbol table */
1581 if (elf_dynsymtab (abfd
) == shindex
)
1584 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1585 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1586 elf_dynsymtab (abfd
) = shindex
;
1587 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1588 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1589 abfd
->flags
|= HAS_SYMS
;
1591 /* Besides being a symbol table, we also treat this as a regular
1592 section, so that objcopy can handle it. */
1593 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1595 case SHT_SYMTAB_SHNDX
: /* Symbol section indices when >64k sections */
1596 if (elf_symtab_shndx (abfd
) == shindex
)
1599 /* Get the associated symbol table. */
1600 if (! bfd_section_from_shdr (abfd
, hdr
->sh_link
)
1601 || hdr
->sh_link
!= elf_onesymtab (abfd
))
1604 elf_symtab_shndx (abfd
) = shindex
;
1605 elf_tdata (abfd
)->symtab_shndx_hdr
= *hdr
;
1606 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->symtab_shndx_hdr
;
1609 case SHT_STRTAB
: /* A string table */
1610 if (hdr
->bfd_section
!= NULL
)
1612 if (ehdr
->e_shstrndx
== shindex
)
1614 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1615 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1619 unsigned int i
, num_sec
;
1621 num_sec
= elf_numsections (abfd
);
1622 for (i
= 1; i
< num_sec
; i
++)
1624 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1625 if (hdr2
->sh_link
== shindex
)
1627 if (! bfd_section_from_shdr (abfd
, i
))
1629 if (elf_onesymtab (abfd
) == i
)
1631 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1632 elf_elfsections (abfd
)[shindex
] =
1633 &elf_tdata (abfd
)->strtab_hdr
;
1636 if (elf_dynsymtab (abfd
) == i
)
1638 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1639 elf_elfsections (abfd
)[shindex
] = hdr
=
1640 &elf_tdata (abfd
)->dynstrtab_hdr
;
1641 /* We also treat this as a regular section, so
1642 that objcopy can handle it. */
1645 #if 0 /* Not handling other string tables specially right now. */
1646 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1647 /* We have a strtab for some random other section. */
1648 newsect
= (asection
*) hdr2
->bfd_section
;
1651 hdr
->bfd_section
= newsect
;
1652 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1654 elf_elfsections (abfd
)[shindex
] = hdr2
;
1660 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1664 /* *These* do a lot of work -- but build no sections! */
1666 asection
*target_sect
;
1667 Elf_Internal_Shdr
*hdr2
;
1668 unsigned int num_sec
= elf_numsections (abfd
);
1670 /* Check for a bogus link to avoid crashing. */
1671 if ((hdr
->sh_link
>= SHN_LORESERVE
&& hdr
->sh_link
<= SHN_HIRESERVE
)
1672 || hdr
->sh_link
>= num_sec
)
1674 ((*_bfd_error_handler
)
1675 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1676 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1677 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1680 /* For some incomprehensible reason Oracle distributes
1681 libraries for Solaris in which some of the objects have
1682 bogus sh_link fields. It would be nice if we could just
1683 reject them, but, unfortunately, some people need to use
1684 them. We scan through the section headers; if we find only
1685 one suitable symbol table, we clobber the sh_link to point
1686 to it. I hope this doesn't break anything. */
1687 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1688 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1694 for (scan
= 1; scan
< num_sec
; scan
++)
1696 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1697 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1708 hdr
->sh_link
= found
;
1711 /* Get the symbol table. */
1712 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1713 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1716 /* If this reloc section does not use the main symbol table we
1717 don't treat it as a reloc section. BFD can't adequately
1718 represent such a section, so at least for now, we don't
1719 try. We just present it as a normal section. We also
1720 can't use it as a reloc section if it points to the null
1722 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1723 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1725 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1727 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1728 if (target_sect
== NULL
)
1731 if ((target_sect
->flags
& SEC_RELOC
) == 0
1732 || target_sect
->reloc_count
== 0)
1733 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1737 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1738 amt
= sizeof (*hdr2
);
1739 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1740 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1743 elf_elfsections (abfd
)[shindex
] = hdr2
;
1744 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1745 target_sect
->flags
|= SEC_RELOC
;
1746 target_sect
->relocation
= NULL
;
1747 target_sect
->rel_filepos
= hdr
->sh_offset
;
1748 /* In the section to which the relocations apply, mark whether
1749 its relocations are of the REL or RELA variety. */
1750 if (hdr
->sh_size
!= 0)
1751 elf_section_data (target_sect
)->use_rela_p
1752 = (hdr
->sh_type
== SHT_RELA
);
1753 abfd
->flags
|= HAS_RELOC
;
1758 case SHT_GNU_verdef
:
1759 elf_dynverdef (abfd
) = shindex
;
1760 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1761 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1764 case SHT_GNU_versym
:
1765 elf_dynversym (abfd
) = shindex
;
1766 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1767 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1770 case SHT_GNU_verneed
:
1771 elf_dynverref (abfd
) = shindex
;
1772 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1773 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1780 /* Make a section for objcopy and relocatable links. */
1781 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1783 if (hdr
->contents
!= NULL
)
1785 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1786 unsigned int n_elt
= hdr
->sh_size
/ 4;
1789 while (--n_elt
!= 0)
1790 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1791 && elf_next_in_group (s
) != NULL
)
1793 elf_next_in_group (hdr
->bfd_section
) = s
;
1800 /* Check for any processor-specific section types. */
1802 if (bed
->elf_backend_section_from_shdr
)
1803 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1811 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1812 Return SEC for sections that have no elf section, and NULL on error. */
1815 bfd_section_from_r_symndx (abfd
, cache
, sec
, r_symndx
)
1817 struct sym_sec_cache
*cache
;
1819 unsigned long r_symndx
;
1821 unsigned char esym_shndx
[4];
1822 unsigned int isym_shndx
;
1823 Elf_Internal_Shdr
*symtab_hdr
;
1826 unsigned int ent
= r_symndx
% LOCAL_SYM_CACHE_SIZE
;
1828 if (cache
->abfd
== abfd
&& cache
->indx
[ent
] == r_symndx
)
1829 return cache
->sec
[ent
];
1831 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1832 pos
= symtab_hdr
->sh_offset
;
1833 if (get_elf_backend_data (abfd
)->s
->sizeof_sym
1834 == sizeof (Elf64_External_Sym
))
1836 pos
+= r_symndx
* sizeof (Elf64_External_Sym
);
1837 pos
+= offsetof (Elf64_External_Sym
, st_shndx
);
1838 amt
= sizeof (((Elf64_External_Sym
*) 0)->st_shndx
);
1842 pos
+= r_symndx
* sizeof (Elf32_External_Sym
);
1843 pos
+= offsetof (Elf32_External_Sym
, st_shndx
);
1844 amt
= sizeof (((Elf32_External_Sym
*) 0)->st_shndx
);
1846 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1847 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1849 isym_shndx
= H_GET_16 (abfd
, esym_shndx
);
1851 if (isym_shndx
== SHN_XINDEX
)
1853 Elf_Internal_Shdr
*shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1854 if (shndx_hdr
->sh_size
!= 0)
1856 pos
= shndx_hdr
->sh_offset
;
1857 pos
+= r_symndx
* sizeof (Elf_External_Sym_Shndx
);
1858 amt
= sizeof (Elf_External_Sym_Shndx
);
1859 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1860 || bfd_bread ((PTR
) esym_shndx
, amt
, abfd
) != amt
)
1862 isym_shndx
= H_GET_32 (abfd
, esym_shndx
);
1866 if (cache
->abfd
!= abfd
)
1868 memset (cache
->indx
, -1, sizeof (cache
->indx
));
1871 cache
->indx
[ent
] = r_symndx
;
1872 cache
->sec
[ent
] = sec
;
1873 if (isym_shndx
< SHN_LORESERVE
|| isym_shndx
> SHN_HIRESERVE
)
1876 s
= bfd_section_from_elf_index (abfd
, isym_shndx
);
1878 cache
->sec
[ent
] = s
;
1880 return cache
->sec
[ent
];
1883 /* Given an ELF section number, retrieve the corresponding BFD
1887 bfd_section_from_elf_index (abfd
, index
)
1891 if (index
>= elf_numsections (abfd
))
1893 return elf_elfsections (abfd
)[index
]->bfd_section
;
1897 _bfd_elf_new_section_hook (abfd
, sec
)
1901 struct bfd_elf_section_data
*sdata
;
1902 bfd_size_type amt
= sizeof (*sdata
);
1904 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1907 sec
->used_by_bfd
= (PTR
) sdata
;
1909 /* Indicate whether or not this section should use RELA relocations. */
1911 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1916 /* Create a new bfd section from an ELF program header.
1918 Since program segments have no names, we generate a synthetic name
1919 of the form segment<NUM>, where NUM is generally the index in the
1920 program header table. For segments that are split (see below) we
1921 generate the names segment<NUM>a and segment<NUM>b.
1923 Note that some program segments may have a file size that is different than
1924 (less than) the memory size. All this means is that at execution the
1925 system must allocate the amount of memory specified by the memory size,
1926 but only initialize it with the first "file size" bytes read from the
1927 file. This would occur for example, with program segments consisting
1928 of combined data+bss.
1930 To handle the above situation, this routine generates TWO bfd sections
1931 for the single program segment. The first has the length specified by
1932 the file size of the segment, and the second has the length specified
1933 by the difference between the two sizes. In effect, the segment is split
1934 into it's initialized and uninitialized parts.
1939 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1941 Elf_Internal_Phdr
*hdr
;
1943 const char *typename
;
1950 split
= ((hdr
->p_memsz
> 0)
1951 && (hdr
->p_filesz
> 0)
1952 && (hdr
->p_memsz
> hdr
->p_filesz
));
1953 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1954 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1957 strcpy (name
, namebuf
);
1958 newsect
= bfd_make_section (abfd
, name
);
1959 if (newsect
== NULL
)
1961 newsect
->vma
= hdr
->p_vaddr
;
1962 newsect
->lma
= hdr
->p_paddr
;
1963 newsect
->_raw_size
= hdr
->p_filesz
;
1964 newsect
->filepos
= hdr
->p_offset
;
1965 newsect
->flags
|= SEC_HAS_CONTENTS
;
1966 if (hdr
->p_type
== PT_LOAD
)
1968 newsect
->flags
|= SEC_ALLOC
;
1969 newsect
->flags
|= SEC_LOAD
;
1970 if (hdr
->p_flags
& PF_X
)
1972 /* FIXME: all we known is that it has execute PERMISSION,
1974 newsect
->flags
|= SEC_CODE
;
1977 if (!(hdr
->p_flags
& PF_W
))
1979 newsect
->flags
|= SEC_READONLY
;
1984 sprintf (namebuf
, "%s%db", typename
, index
);
1985 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1988 strcpy (name
, namebuf
);
1989 newsect
= bfd_make_section (abfd
, name
);
1990 if (newsect
== NULL
)
1992 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1993 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1994 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1995 if (hdr
->p_type
== PT_LOAD
)
1997 newsect
->flags
|= SEC_ALLOC
;
1998 if (hdr
->p_flags
& PF_X
)
1999 newsect
->flags
|= SEC_CODE
;
2001 if (!(hdr
->p_flags
& PF_W
))
2002 newsect
->flags
|= SEC_READONLY
;
2009 bfd_section_from_phdr (abfd
, hdr
, index
)
2011 Elf_Internal_Phdr
*hdr
;
2014 struct elf_backend_data
*bed
;
2016 switch (hdr
->p_type
)
2019 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
2022 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
2025 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
2028 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
2031 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
2033 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
2038 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
2041 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
2044 /* Check for any processor-specific program segment types.
2045 If no handler for them, default to making "segment" sections. */
2046 bed
= get_elf_backend_data (abfd
);
2047 if (bed
->elf_backend_section_from_phdr
)
2048 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
2050 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
2054 /* Initialize REL_HDR, the section-header for new section, containing
2055 relocations against ASECT. If USE_RELA_P is true, we use RELA
2056 relocations; otherwise, we use REL relocations. */
2059 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
2061 Elf_Internal_Shdr
*rel_hdr
;
2066 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2067 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
2069 name
= bfd_alloc (abfd
, amt
);
2072 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
2074 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
), name
,
2076 if (rel_hdr
->sh_name
== (unsigned int) -1)
2078 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
2079 rel_hdr
->sh_entsize
= (use_rela_p
2080 ? bed
->s
->sizeof_rela
2081 : bed
->s
->sizeof_rel
);
2082 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
2083 rel_hdr
->sh_flags
= 0;
2084 rel_hdr
->sh_addr
= 0;
2085 rel_hdr
->sh_size
= 0;
2086 rel_hdr
->sh_offset
= 0;
2091 /* Set up an ELF internal section header for a section. */
2094 elf_fake_sections (abfd
, asect
, failedptrarg
)
2099 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2100 boolean
*failedptr
= (boolean
*) failedptrarg
;
2101 Elf_Internal_Shdr
*this_hdr
;
2105 /* We already failed; just get out of the bfd_map_over_sections
2110 this_hdr
= &elf_section_data (asect
)->this_hdr
;
2112 this_hdr
->sh_name
= (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2113 asect
->name
, false);
2114 if (this_hdr
->sh_name
== (unsigned long) -1)
2120 this_hdr
->sh_flags
= 0;
2122 if ((asect
->flags
& SEC_ALLOC
) != 0
2123 || asect
->user_set_vma
)
2124 this_hdr
->sh_addr
= asect
->vma
;
2126 this_hdr
->sh_addr
= 0;
2128 this_hdr
->sh_offset
= 0;
2129 this_hdr
->sh_size
= asect
->_raw_size
;
2130 this_hdr
->sh_link
= 0;
2131 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
2132 /* The sh_entsize and sh_info fields may have been set already by
2133 copy_private_section_data. */
2135 this_hdr
->bfd_section
= asect
;
2136 this_hdr
->contents
= NULL
;
2138 /* FIXME: This should not be based on section names. */
2139 if (strcmp (asect
->name
, ".dynstr") == 0)
2140 this_hdr
->sh_type
= SHT_STRTAB
;
2141 else if (strcmp (asect
->name
, ".hash") == 0)
2143 this_hdr
->sh_type
= SHT_HASH
;
2144 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2146 else if (strcmp (asect
->name
, ".dynsym") == 0)
2148 this_hdr
->sh_type
= SHT_DYNSYM
;
2149 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2151 else if (strcmp (asect
->name
, ".dynamic") == 0)
2153 this_hdr
->sh_type
= SHT_DYNAMIC
;
2154 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2156 else if (strncmp (asect
->name
, ".rela", 5) == 0
2157 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2159 this_hdr
->sh_type
= SHT_RELA
;
2160 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2162 else if (strncmp (asect
->name
, ".rel", 4) == 0
2163 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2165 this_hdr
->sh_type
= SHT_REL
;
2166 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2168 else if (strncmp (asect
->name
, ".note", 5) == 0)
2169 this_hdr
->sh_type
= SHT_NOTE
;
2170 else if (strncmp (asect
->name
, ".stab", 5) == 0
2171 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2172 this_hdr
->sh_type
= SHT_STRTAB
;
2173 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2175 this_hdr
->sh_type
= SHT_GNU_versym
;
2176 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2178 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2180 this_hdr
->sh_type
= SHT_GNU_verdef
;
2181 this_hdr
->sh_entsize
= 0;
2182 /* objcopy or strip will copy over sh_info, but may not set
2183 cverdefs. The linker will set cverdefs, but sh_info will be
2185 if (this_hdr
->sh_info
== 0)
2186 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2188 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2189 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2191 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2193 this_hdr
->sh_type
= SHT_GNU_verneed
;
2194 this_hdr
->sh_entsize
= 0;
2195 /* objcopy or strip will copy over sh_info, but may not set
2196 cverrefs. The linker will set cverrefs, but sh_info will be
2198 if (this_hdr
->sh_info
== 0)
2199 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2201 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2202 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2204 else if ((asect
->flags
& SEC_GROUP
) != 0)
2206 this_hdr
->sh_type
= SHT_GROUP
;
2207 this_hdr
->sh_entsize
= 4;
2209 else if ((asect
->flags
& SEC_ALLOC
) != 0
2210 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
2211 this_hdr
->sh_type
= SHT_NOBITS
;
2213 this_hdr
->sh_type
= SHT_PROGBITS
;
2215 if ((asect
->flags
& SEC_ALLOC
) != 0)
2216 this_hdr
->sh_flags
|= SHF_ALLOC
;
2217 if ((asect
->flags
& SEC_READONLY
) == 0)
2218 this_hdr
->sh_flags
|= SHF_WRITE
;
2219 if ((asect
->flags
& SEC_CODE
) != 0)
2220 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2221 if ((asect
->flags
& SEC_MERGE
) != 0)
2223 this_hdr
->sh_flags
|= SHF_MERGE
;
2224 this_hdr
->sh_entsize
= asect
->entsize
;
2225 if ((asect
->flags
& SEC_STRINGS
) != 0)
2226 this_hdr
->sh_flags
|= SHF_STRINGS
;
2228 if (elf_group_name (asect
) != NULL
)
2229 this_hdr
->sh_flags
|= SHF_GROUP
;
2231 /* Check for processor-specific section types. */
2232 if (bed
->elf_backend_fake_sections
2233 && !(*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
))
2236 /* If the section has relocs, set up a section header for the
2237 SHT_REL[A] section. If two relocation sections are required for
2238 this section, it is up to the processor-specific back-end to
2239 create the other. */
2240 if ((asect
->flags
& SEC_RELOC
) != 0
2241 && !_bfd_elf_init_reloc_shdr (abfd
,
2242 &elf_section_data (asect
)->rel_hdr
,
2244 elf_section_data (asect
)->use_rela_p
))
2248 /* Fill in the contents of a SHT_GROUP section. */
2251 set_group_contents (abfd
, sec
, failedptrarg
)
2254 PTR failedptrarg ATTRIBUTE_UNUSED
;
2256 boolean
*failedptr
= (boolean
*) failedptrarg
;
2257 unsigned long symindx
;
2260 struct bfd_link_order
*l
;
2262 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2266 /* If called from the assembler, swap_out_syms will have set up
2267 elf_section_syms; If called for "ld -r", the symbols won't yet
2268 be mapped, so emulate elf_bfd_final_link. */
2269 if (elf_section_syms (abfd
) != NULL
)
2270 symindx
= elf_section_syms (abfd
)[sec
->index
]->udata
.i
;
2272 symindx
= elf_section_data (sec
)->this_idx
;
2273 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2275 /* Nor will the contents be allocated for "ld -r". */
2276 if (sec
->contents
== NULL
)
2278 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2279 if (sec
->contents
== NULL
)
2286 loc
= sec
->contents
+ sec
->_raw_size
;
2288 /* Get the pointer to the first section in the group that we
2289 squirreled away here. */
2290 elt
= elf_next_in_group (sec
);
2292 /* First element is a flag word. Rest of section is elf section
2293 indices for all the sections of the group. Write them backwards
2294 just to keep the group in the same order as given in .section
2295 directives, not that it matters. */
2299 H_PUT_32 (abfd
, elf_section_data (elt
)->this_idx
, loc
);
2300 elt
= elf_next_in_group (elt
);
2303 /* If this is a relocatable link, then the above did nothing because
2304 SEC is the output section. Look through the input sections
2306 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2307 if (l
->type
== bfd_indirect_link_order
2308 && (elt
= elf_next_in_group (l
->u
.indirect
.section
)) != NULL
)
2313 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2314 elt
= elf_next_in_group (elt
);
2315 /* During a relocatable link, the lists are circular. */
2317 while (elt
!= elf_next_in_group (l
->u
.indirect
.section
));
2320 H_PUT_32 (abfd
, 0, loc
);
2322 BFD_ASSERT (loc
== sec
->contents
);
2325 /* Assign all ELF section numbers. The dummy first section is handled here
2326 too. The link/info pointers for the standard section types are filled
2327 in here too, while we're at it. */
2330 assign_section_numbers (abfd
)
2333 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2335 unsigned int section_number
, secn
;
2336 Elf_Internal_Shdr
**i_shdrp
;
2341 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd
));
2343 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2345 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2347 if (section_number
== SHN_LORESERVE
)
2348 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2349 d
->this_idx
= section_number
++;
2350 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->this_hdr
.sh_name
);
2351 if ((sec
->flags
& SEC_RELOC
) == 0)
2355 if (section_number
== SHN_LORESERVE
)
2356 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2357 d
->rel_idx
= section_number
++;
2358 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr
.sh_name
);
2363 if (section_number
== SHN_LORESERVE
)
2364 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2365 d
->rel_idx2
= section_number
++;
2366 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), d
->rel_hdr2
->sh_name
);
2372 if (section_number
== SHN_LORESERVE
)
2373 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2374 t
->shstrtab_section
= section_number
++;
2375 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->shstrtab_hdr
.sh_name
);
2376 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2378 if (bfd_get_symcount (abfd
) > 0)
2380 if (section_number
== SHN_LORESERVE
)
2381 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2382 t
->symtab_section
= section_number
++;
2383 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->symtab_hdr
.sh_name
);
2384 if (section_number
> SHN_LORESERVE
- 2)
2386 if (section_number
== SHN_LORESERVE
)
2387 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2388 t
->symtab_shndx_section
= section_number
++;
2389 t
->symtab_shndx_hdr
.sh_name
2390 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd
),
2391 ".symtab_shndx", false);
2392 if (t
->symtab_shndx_hdr
.sh_name
== (unsigned int) -1)
2395 if (section_number
== SHN_LORESERVE
)
2396 section_number
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2397 t
->strtab_section
= section_number
++;
2398 _bfd_elf_strtab_addref (elf_shstrtab (abfd
), t
->strtab_hdr
.sh_name
);
2401 _bfd_elf_strtab_finalize (elf_shstrtab (abfd
));
2402 t
->shstrtab_hdr
.sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2404 elf_numsections (abfd
) = section_number
;
2405 elf_elfheader (abfd
)->e_shnum
= section_number
;
2406 if (section_number
> SHN_LORESERVE
)
2407 elf_elfheader (abfd
)->e_shnum
-= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
2409 /* Set up the list of section header pointers, in agreement with the
2411 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2412 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2413 if (i_shdrp
== NULL
)
2416 amt
= sizeof (Elf_Internal_Shdr
);
2417 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2418 if (i_shdrp
[0] == NULL
)
2420 bfd_release (abfd
, i_shdrp
);
2423 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2425 elf_elfsections (abfd
) = i_shdrp
;
2427 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2428 if (bfd_get_symcount (abfd
) > 0)
2430 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2431 if (elf_numsections (abfd
) > SHN_LORESERVE
)
2433 i_shdrp
[t
->symtab_shndx_section
] = &t
->symtab_shndx_hdr
;
2434 t
->symtab_shndx_hdr
.sh_link
= t
->symtab_section
;
2436 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2437 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2439 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2441 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2445 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2446 if (d
->rel_idx
!= 0)
2447 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2448 if (d
->rel_idx2
!= 0)
2449 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2451 /* Fill in the sh_link and sh_info fields while we're at it. */
2453 /* sh_link of a reloc section is the section index of the symbol
2454 table. sh_info is the section index of the section to which
2455 the relocation entries apply. */
2456 if (d
->rel_idx
!= 0)
2458 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2459 d
->rel_hdr
.sh_info
= d
->this_idx
;
2461 if (d
->rel_idx2
!= 0)
2463 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2464 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2467 switch (d
->this_hdr
.sh_type
)
2471 /* A reloc section which we are treating as a normal BFD
2472 section. sh_link is the section index of the symbol
2473 table. sh_info is the section index of the section to
2474 which the relocation entries apply. We assume that an
2475 allocated reloc section uses the dynamic symbol table.
2476 FIXME: How can we be sure? */
2477 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2479 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2481 /* We look up the section the relocs apply to by name. */
2483 if (d
->this_hdr
.sh_type
== SHT_REL
)
2487 s
= bfd_get_section_by_name (abfd
, name
);
2489 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2493 /* We assume that a section named .stab*str is a stabs
2494 string section. We look for a section with the same name
2495 but without the trailing ``str'', and set its sh_link
2496 field to point to this section. */
2497 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2498 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2503 len
= strlen (sec
->name
);
2504 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2507 strncpy (alc
, sec
->name
, len
- 3);
2508 alc
[len
- 3] = '\0';
2509 s
= bfd_get_section_by_name (abfd
, alc
);
2513 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2515 /* This is a .stab section. */
2516 elf_section_data (s
)->this_hdr
.sh_entsize
=
2517 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2524 case SHT_GNU_verneed
:
2525 case SHT_GNU_verdef
:
2526 /* sh_link is the section header index of the string table
2527 used for the dynamic entries, or the symbol table, or the
2529 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2531 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2535 case SHT_GNU_versym
:
2536 /* sh_link is the section header index of the symbol table
2537 this hash table or version table is for. */
2538 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2540 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2544 d
->this_hdr
.sh_link
= t
->symtab_section
;
2548 for (secn
= 1; secn
< section_number
; ++secn
)
2549 if (i_shdrp
[secn
] == NULL
)
2550 i_shdrp
[secn
] = i_shdrp
[0];
2552 i_shdrp
[secn
]->sh_name
= _bfd_elf_strtab_offset (elf_shstrtab (abfd
),
2553 i_shdrp
[secn
]->sh_name
);
2557 /* Map symbol from it's internal number to the external number, moving
2558 all local symbols to be at the head of the list. */
2561 sym_is_global (abfd
, sym
)
2565 /* If the backend has a special mapping, use it. */
2566 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2567 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2570 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2571 || bfd_is_und_section (bfd_get_section (sym
))
2572 || bfd_is_com_section (bfd_get_section (sym
)));
2576 elf_map_symbols (abfd
)
2579 unsigned int symcount
= bfd_get_symcount (abfd
);
2580 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2581 asymbol
**sect_syms
;
2582 unsigned int num_locals
= 0;
2583 unsigned int num_globals
= 0;
2584 unsigned int num_locals2
= 0;
2585 unsigned int num_globals2
= 0;
2593 fprintf (stderr
, "elf_map_symbols\n");
2597 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2599 if (max_index
< asect
->index
)
2600 max_index
= asect
->index
;
2604 amt
= max_index
* sizeof (asymbol
*);
2605 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2606 if (sect_syms
== NULL
)
2608 elf_section_syms (abfd
) = sect_syms
;
2609 elf_num_section_syms (abfd
) = max_index
;
2611 /* Init sect_syms entries for any section symbols we have already
2612 decided to output. */
2613 for (idx
= 0; idx
< symcount
; idx
++)
2615 asymbol
*sym
= syms
[idx
];
2617 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2624 if (sec
->owner
!= NULL
)
2626 if (sec
->owner
!= abfd
)
2628 if (sec
->output_offset
!= 0)
2631 sec
= sec
->output_section
;
2633 /* Empty sections in the input files may have had a
2634 section symbol created for them. (See the comment
2635 near the end of _bfd_generic_link_output_symbols in
2636 linker.c). If the linker script discards such
2637 sections then we will reach this point. Since we know
2638 that we cannot avoid this case, we detect it and skip
2639 the abort and the assignment to the sect_syms array.
2640 To reproduce this particular case try running the
2641 linker testsuite test ld-scripts/weak.exp for an ELF
2642 port that uses the generic linker. */
2643 if (sec
->owner
== NULL
)
2646 BFD_ASSERT (sec
->owner
== abfd
);
2648 sect_syms
[sec
->index
] = syms
[idx
];
2653 /* Classify all of the symbols. */
2654 for (idx
= 0; idx
< symcount
; idx
++)
2656 if (!sym_is_global (abfd
, syms
[idx
]))
2662 /* We will be adding a section symbol for each BFD section. Most normal
2663 sections will already have a section symbol in outsymbols, but
2664 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2665 at least in that case. */
2666 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2668 if (sect_syms
[asect
->index
] == NULL
)
2670 if (!sym_is_global (abfd
, asect
->symbol
))
2677 /* Now sort the symbols so the local symbols are first. */
2678 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2679 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2681 if (new_syms
== NULL
)
2684 for (idx
= 0; idx
< symcount
; idx
++)
2686 asymbol
*sym
= syms
[idx
];
2689 if (!sym_is_global (abfd
, sym
))
2692 i
= num_locals
+ num_globals2
++;
2694 sym
->udata
.i
= i
+ 1;
2696 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2698 if (sect_syms
[asect
->index
] == NULL
)
2700 asymbol
*sym
= asect
->symbol
;
2703 sect_syms
[asect
->index
] = sym
;
2704 if (!sym_is_global (abfd
, sym
))
2707 i
= num_locals
+ num_globals2
++;
2709 sym
->udata
.i
= i
+ 1;
2713 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2715 elf_num_locals (abfd
) = num_locals
;
2716 elf_num_globals (abfd
) = num_globals
;
2720 /* Align to the maximum file alignment that could be required for any
2721 ELF data structure. */
2723 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2724 static INLINE file_ptr
2725 align_file_position (off
, align
)
2729 return (off
+ align
- 1) & ~(align
- 1);
2732 /* Assign a file position to a section, optionally aligning to the
2733 required section alignment. */
2736 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2737 Elf_Internal_Shdr
*i_shdrp
;
2745 al
= i_shdrp
->sh_addralign
;
2747 offset
= BFD_ALIGN (offset
, al
);
2749 i_shdrp
->sh_offset
= offset
;
2750 if (i_shdrp
->bfd_section
!= NULL
)
2751 i_shdrp
->bfd_section
->filepos
= offset
;
2752 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2753 offset
+= i_shdrp
->sh_size
;
2757 /* Compute the file positions we are going to put the sections at, and
2758 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2759 is not NULL, this is being called by the ELF backend linker. */
2762 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2764 struct bfd_link_info
*link_info
;
2766 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2768 struct bfd_strtab_hash
*strtab
;
2769 Elf_Internal_Shdr
*shstrtab_hdr
;
2771 if (abfd
->output_has_begun
)
2774 /* Do any elf backend specific processing first. */
2775 if (bed
->elf_backend_begin_write_processing
)
2776 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2778 if (! prep_headers (abfd
))
2781 /* Post process the headers if necessary. */
2782 if (bed
->elf_backend_post_process_headers
)
2783 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2786 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2790 if (!assign_section_numbers (abfd
))
2793 /* The backend linker builds symbol table information itself. */
2794 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2796 /* Non-zero if doing a relocatable link. */
2797 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2799 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2803 if (link_info
== NULL
|| link_info
->relocateable
)
2805 bfd_map_over_sections (abfd
, set_group_contents
, &failed
);
2810 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2811 /* sh_name was set in prep_headers. */
2812 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2813 shstrtab_hdr
->sh_flags
= 0;
2814 shstrtab_hdr
->sh_addr
= 0;
2815 shstrtab_hdr
->sh_size
= _bfd_elf_strtab_size (elf_shstrtab (abfd
));
2816 shstrtab_hdr
->sh_entsize
= 0;
2817 shstrtab_hdr
->sh_link
= 0;
2818 shstrtab_hdr
->sh_info
= 0;
2819 /* sh_offset is set in assign_file_positions_except_relocs. */
2820 shstrtab_hdr
->sh_addralign
= 1;
2822 if (!assign_file_positions_except_relocs (abfd
))
2825 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2828 Elf_Internal_Shdr
*hdr
;
2830 off
= elf_tdata (abfd
)->next_file_pos
;
2832 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2833 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2835 hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
2836 if (hdr
->sh_size
!= 0)
2837 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2839 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2840 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2842 elf_tdata (abfd
)->next_file_pos
= off
;
2844 /* Now that we know where the .strtab section goes, write it
2846 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2847 || ! _bfd_stringtab_emit (abfd
, strtab
))
2849 _bfd_stringtab_free (strtab
);
2852 abfd
->output_has_begun
= true;
2857 /* Create a mapping from a set of sections to a program segment. */
2859 static INLINE
struct elf_segment_map
*
2860 make_mapping (abfd
, sections
, from
, to
, phdr
)
2862 asection
**sections
;
2867 struct elf_segment_map
*m
;
2872 amt
= sizeof (struct elf_segment_map
);
2873 amt
+= (to
- from
- 1) * sizeof (asection
*);
2874 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2878 m
->p_type
= PT_LOAD
;
2879 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2880 m
->sections
[i
- from
] = *hdrpp
;
2881 m
->count
= to
- from
;
2883 if (from
== 0 && phdr
)
2885 /* Include the headers in the first PT_LOAD segment. */
2886 m
->includes_filehdr
= 1;
2887 m
->includes_phdrs
= 1;
2893 /* Set up a mapping from BFD sections to program segments. */
2896 map_sections_to_segments (abfd
)
2899 asection
**sections
= NULL
;
2903 struct elf_segment_map
*mfirst
;
2904 struct elf_segment_map
**pm
;
2905 struct elf_segment_map
*m
;
2907 unsigned int phdr_index
;
2908 bfd_vma maxpagesize
;
2910 boolean phdr_in_segment
= true;
2912 asection
*dynsec
, *eh_frame_hdr
;
2915 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2918 if (bfd_count_sections (abfd
) == 0)
2921 /* Select the allocated sections, and sort them. */
2923 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
2924 sections
= (asection
**) bfd_malloc (amt
);
2925 if (sections
== NULL
)
2929 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2931 if ((s
->flags
& SEC_ALLOC
) != 0)
2937 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2940 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2942 /* Build the mapping. */
2947 /* If we have a .interp section, then create a PT_PHDR segment for
2948 the program headers and a PT_INTERP segment for the .interp
2950 s
= bfd_get_section_by_name (abfd
, ".interp");
2951 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2953 amt
= sizeof (struct elf_segment_map
);
2954 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2958 m
->p_type
= PT_PHDR
;
2959 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2960 m
->p_flags
= PF_R
| PF_X
;
2961 m
->p_flags_valid
= 1;
2962 m
->includes_phdrs
= 1;
2967 amt
= sizeof (struct elf_segment_map
);
2968 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2972 m
->p_type
= PT_INTERP
;
2980 /* Look through the sections. We put sections in the same program
2981 segment when the start of the second section can be placed within
2982 a few bytes of the end of the first section. */
2985 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2987 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2989 && (dynsec
->flags
& SEC_LOAD
) == 0)
2992 /* Deal with -Ttext or something similar such that the first section
2993 is not adjacent to the program headers. This is an
2994 approximation, since at this point we don't know exactly how many
2995 program headers we will need. */
2998 bfd_size_type phdr_size
;
3000 phdr_size
= elf_tdata (abfd
)->program_header_size
;
3002 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
3003 if ((abfd
->flags
& D_PAGED
) == 0
3004 || sections
[0]->lma
< phdr_size
3005 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
3006 phdr_in_segment
= false;
3009 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
3012 boolean new_segment
;
3016 /* See if this section and the last one will fit in the same
3019 if (last_hdr
== NULL
)
3021 /* If we don't have a segment yet, then we don't need a new
3022 one (we build the last one after this loop). */
3023 new_segment
= false;
3025 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
3027 /* If this section has a different relation between the
3028 virtual address and the load address, then we need a new
3032 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3033 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
3035 /* If putting this section in this segment would force us to
3036 skip a page in the segment, then we need a new segment. */
3039 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
3040 && (hdr
->flags
& SEC_LOAD
) != 0)
3042 /* We don't want to put a loadable section after a
3043 nonloadable section in the same segment. */
3046 else if ((abfd
->flags
& D_PAGED
) == 0)
3048 /* If the file is not demand paged, which means that we
3049 don't require the sections to be correctly aligned in the
3050 file, then there is no other reason for a new segment. */
3051 new_segment
= false;
3054 && (hdr
->flags
& SEC_READONLY
) == 0
3055 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
3058 /* We don't want to put a writable section in a read only
3059 segment, unless they are on the same page in memory
3060 anyhow. We already know that the last section does not
3061 bring us past the current section on the page, so the
3062 only case in which the new section is not on the same
3063 page as the previous section is when the previous section
3064 ends precisely on a page boundary. */
3069 /* Otherwise, we can use the same segment. */
3070 new_segment
= false;
3075 if ((hdr
->flags
& SEC_READONLY
) == 0)
3081 /* We need a new program segment. We must create a new program
3082 header holding all the sections from phdr_index until hdr. */
3084 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3091 if ((hdr
->flags
& SEC_READONLY
) == 0)
3098 phdr_in_segment
= false;
3101 /* Create a final PT_LOAD program segment. */
3102 if (last_hdr
!= NULL
)
3104 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
3112 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3115 amt
= sizeof (struct elf_segment_map
);
3116 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3120 m
->p_type
= PT_DYNAMIC
;
3122 m
->sections
[0] = dynsec
;
3128 /* For each loadable .note section, add a PT_NOTE segment. We don't
3129 use bfd_get_section_by_name, because if we link together
3130 nonloadable .note sections and loadable .note sections, we will
3131 generate two .note sections in the output file. FIXME: Using
3132 names for section types is bogus anyhow. */
3133 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3135 if ((s
->flags
& SEC_LOAD
) != 0
3136 && strncmp (s
->name
, ".note", 5) == 0)
3138 amt
= sizeof (struct elf_segment_map
);
3139 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3143 m
->p_type
= PT_NOTE
;
3152 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3154 eh_frame_hdr
= NULL
;
3155 if (elf_tdata (abfd
)->eh_frame_hdr
)
3156 eh_frame_hdr
= bfd_get_section_by_name (abfd
, ".eh_frame_hdr");
3157 if (eh_frame_hdr
!= NULL
&& (eh_frame_hdr
->flags
& SEC_LOAD
))
3159 amt
= sizeof (struct elf_segment_map
);
3160 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
3164 m
->p_type
= PT_GNU_EH_FRAME
;
3166 m
->sections
[0] = eh_frame_hdr
;
3175 elf_tdata (abfd
)->segment_map
= mfirst
;
3179 if (sections
!= NULL
)
3184 /* Sort sections by address. */
3187 elf_sort_sections (arg1
, arg2
)
3191 const asection
*sec1
= *(const asection
**) arg1
;
3192 const asection
*sec2
= *(const asection
**) arg2
;
3194 /* Sort by LMA first, since this is the address used to
3195 place the section into a segment. */
3196 if (sec1
->lma
< sec2
->lma
)
3198 else if (sec1
->lma
> sec2
->lma
)
3201 /* Then sort by VMA. Normally the LMA and the VMA will be
3202 the same, and this will do nothing. */
3203 if (sec1
->vma
< sec2
->vma
)
3205 else if (sec1
->vma
> sec2
->vma
)
3208 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3210 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3216 /* If the indicies are the same, do not return 0
3217 here, but continue to try the next comparison. */
3218 if (sec1
->target_index
- sec2
->target_index
!= 0)
3219 return sec1
->target_index
- sec2
->target_index
;
3224 else if (TOEND (sec2
))
3229 /* Sort by size, to put zero sized sections
3230 before others at the same address. */
3232 if (sec1
->_raw_size
< sec2
->_raw_size
)
3234 if (sec1
->_raw_size
> sec2
->_raw_size
)
3237 return sec1
->target_index
- sec2
->target_index
;
3240 /* Assign file positions to the sections based on the mapping from
3241 sections to segments. This function also sets up some fields in
3242 the file header, and writes out the program headers. */
3245 assign_file_positions_for_segments (abfd
)
3248 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3250 struct elf_segment_map
*m
;
3252 Elf_Internal_Phdr
*phdrs
;
3254 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3255 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3256 Elf_Internal_Phdr
*p
;
3259 if (elf_tdata (abfd
)->segment_map
== NULL
)
3261 if (! map_sections_to_segments (abfd
))
3265 if (bed
->elf_backend_modify_segment_map
)
3267 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3272 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3275 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3276 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3277 elf_elfheader (abfd
)->e_phnum
= count
;
3282 /* If we already counted the number of program segments, make sure
3283 that we allocated enough space. This happens when SIZEOF_HEADERS
3284 is used in a linker script. */
3285 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3286 if (alloc
!= 0 && count
> alloc
)
3288 ((*_bfd_error_handler
)
3289 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3290 bfd_get_filename (abfd
), alloc
, count
));
3291 bfd_set_error (bfd_error_bad_value
);
3298 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3299 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3303 off
= bed
->s
->sizeof_ehdr
;
3304 off
+= alloc
* bed
->s
->sizeof_phdr
;
3311 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3318 /* If elf_segment_map is not from map_sections_to_segments, the
3319 sections may not be correctly ordered. NOTE: sorting should
3320 not be done to the PT_NOTE section of a corefile, which may
3321 contain several pseudo-sections artificially created by bfd.
3322 Sorting these pseudo-sections breaks things badly. */
3324 && !(elf_elfheader (abfd
)->e_type
== ET_CORE
3325 && m
->p_type
== PT_NOTE
))
3326 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3329 p
->p_type
= m
->p_type
;
3330 p
->p_flags
= m
->p_flags
;
3332 if (p
->p_type
== PT_LOAD
3334 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3336 if ((abfd
->flags
& D_PAGED
) != 0)
3337 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3340 bfd_size_type align
;
3343 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3345 bfd_size_type secalign
;
3347 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3348 if (secalign
> align
)
3352 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3359 p
->p_vaddr
= m
->sections
[0]->vma
;
3361 if (m
->p_paddr_valid
)
3362 p
->p_paddr
= m
->p_paddr
;
3363 else if (m
->count
== 0)
3366 p
->p_paddr
= m
->sections
[0]->lma
;
3368 if (p
->p_type
== PT_LOAD
3369 && (abfd
->flags
& D_PAGED
) != 0)
3370 p
->p_align
= bed
->maxpagesize
;
3371 else if (m
->count
== 0)
3372 p
->p_align
= bed
->s
->file_align
;
3380 if (m
->includes_filehdr
)
3382 if (! m
->p_flags_valid
)
3385 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3386 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3389 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3391 if (p
->p_vaddr
< (bfd_vma
) off
)
3393 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3394 bfd_get_filename (abfd
));
3395 bfd_set_error (bfd_error_bad_value
);
3400 if (! m
->p_paddr_valid
)
3403 if (p
->p_type
== PT_LOAD
)
3405 filehdr_vaddr
= p
->p_vaddr
;
3406 filehdr_paddr
= p
->p_paddr
;
3410 if (m
->includes_phdrs
)
3412 if (! m
->p_flags_valid
)
3415 if (m
->includes_filehdr
)
3417 if (p
->p_type
== PT_LOAD
)
3419 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3420 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3425 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3429 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3430 p
->p_vaddr
-= off
- p
->p_offset
;
3431 if (! m
->p_paddr_valid
)
3432 p
->p_paddr
-= off
- p
->p_offset
;
3435 if (p
->p_type
== PT_LOAD
)
3437 phdrs_vaddr
= p
->p_vaddr
;
3438 phdrs_paddr
= p
->p_paddr
;
3441 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3444 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3445 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3448 if (p
->p_type
== PT_LOAD
3449 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3451 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3457 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3458 p
->p_filesz
+= adjust
;
3459 p
->p_memsz
+= adjust
;
3465 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3469 bfd_size_type align
;
3473 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3475 /* The section may have artificial alignment forced by a
3476 link script. Notice this case by the gap between the
3477 cumulative phdr lma and the section's lma. */
3478 if (p
->p_paddr
+ p
->p_memsz
< sec
->lma
)
3480 bfd_vma adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3482 p
->p_memsz
+= adjust
;
3485 if ((flags
& SEC_LOAD
) != 0)
3486 p
->p_filesz
+= adjust
;
3489 if (p
->p_type
== PT_LOAD
)
3491 bfd_signed_vma adjust
;
3493 if ((flags
& SEC_LOAD
) != 0)
3495 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3499 else if ((flags
& SEC_ALLOC
) != 0)
3501 /* The section VMA must equal the file position
3502 modulo the page size. FIXME: I'm not sure if
3503 this adjustment is really necessary. We used to
3504 not have the SEC_LOAD case just above, and then
3505 this was necessary, but now I'm not sure. */
3506 if ((abfd
->flags
& D_PAGED
) != 0)
3507 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3509 adjust
= (sec
->vma
- voff
) % align
;
3518 (* _bfd_error_handler
) (_("\
3519 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3520 bfd_section_name (abfd
, sec
),
3525 p
->p_memsz
+= adjust
;
3528 if ((flags
& SEC_LOAD
) != 0)
3529 p
->p_filesz
+= adjust
;
3534 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3535 used in a linker script we may have a section with
3536 SEC_LOAD clear but which is supposed to have
3538 if ((flags
& SEC_LOAD
) != 0
3539 || (flags
& SEC_HAS_CONTENTS
) != 0)
3540 off
+= sec
->_raw_size
;
3542 if ((flags
& SEC_ALLOC
) != 0)
3543 voff
+= sec
->_raw_size
;
3546 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3548 /* The actual "note" segment has i == 0.
3549 This is the one that actually contains everything. */
3553 p
->p_filesz
= sec
->_raw_size
;
3554 off
+= sec
->_raw_size
;
3559 /* Fake sections -- don't need to be written. */
3562 flags
= sec
->flags
= 0;
3569 p
->p_memsz
+= sec
->_raw_size
;
3571 if ((flags
& SEC_LOAD
) != 0)
3572 p
->p_filesz
+= sec
->_raw_size
;
3574 if (align
> p
->p_align
3575 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3579 if (! m
->p_flags_valid
)
3582 if ((flags
& SEC_CODE
) != 0)
3584 if ((flags
& SEC_READONLY
) == 0)
3590 /* Now that we have set the section file positions, we can set up
3591 the file positions for the non PT_LOAD segments. */
3592 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3596 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3598 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3599 p
->p_offset
= m
->sections
[0]->filepos
;
3603 if (m
->includes_filehdr
)
3605 p
->p_vaddr
= filehdr_vaddr
;
3606 if (! m
->p_paddr_valid
)
3607 p
->p_paddr
= filehdr_paddr
;
3609 else if (m
->includes_phdrs
)
3611 p
->p_vaddr
= phdrs_vaddr
;
3612 if (! m
->p_paddr_valid
)
3613 p
->p_paddr
= phdrs_paddr
;
3618 /* Clear out any program headers we allocated but did not use. */
3619 for (; count
< alloc
; count
++, p
++)
3621 memset (p
, 0, sizeof *p
);
3622 p
->p_type
= PT_NULL
;
3625 elf_tdata (abfd
)->phdr
= phdrs
;
3627 elf_tdata (abfd
)->next_file_pos
= off
;
3629 /* Write out the program headers. */
3630 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3631 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3637 /* Get the size of the program header.
3639 If this is called by the linker before any of the section VMA's are set, it
3640 can't calculate the correct value for a strange memory layout. This only
3641 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3642 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3643 data segment (exclusive of .interp and .dynamic).
3645 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3646 will be two segments. */
3648 static bfd_size_type
3649 get_program_header_size (abfd
)
3654 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3656 /* We can't return a different result each time we're called. */
3657 if (elf_tdata (abfd
)->program_header_size
!= 0)
3658 return elf_tdata (abfd
)->program_header_size
;
3660 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3662 struct elf_segment_map
*m
;
3665 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3667 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3668 return elf_tdata (abfd
)->program_header_size
;
3671 /* Assume we will need exactly two PT_LOAD segments: one for text
3672 and one for data. */
3675 s
= bfd_get_section_by_name (abfd
, ".interp");
3676 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3678 /* If we have a loadable interpreter section, we need a
3679 PT_INTERP segment. In this case, assume we also need a
3680 PT_PHDR segment, although that may not be true for all
3685 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3687 /* We need a PT_DYNAMIC segment. */
3691 if (elf_tdata (abfd
)->eh_frame_hdr
3692 && bfd_get_section_by_name (abfd
, ".eh_frame_hdr") != NULL
)
3694 /* We need a PT_GNU_EH_FRAME segment. */
3698 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3700 if ((s
->flags
& SEC_LOAD
) != 0
3701 && strncmp (s
->name
, ".note", 5) == 0)
3703 /* We need a PT_NOTE segment. */
3708 /* Let the backend count up any program headers it might need. */
3709 if (bed
->elf_backend_additional_program_headers
)
3713 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3719 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3720 return elf_tdata (abfd
)->program_header_size
;
3723 /* Work out the file positions of all the sections. This is called by
3724 _bfd_elf_compute_section_file_positions. All the section sizes and
3725 VMAs must be known before this is called.
3727 We do not consider reloc sections at this point, unless they form
3728 part of the loadable image. Reloc sections are assigned file
3729 positions in assign_file_positions_for_relocs, which is called by
3730 write_object_contents and final_link.
3732 We also don't set the positions of the .symtab and .strtab here. */
3735 assign_file_positions_except_relocs (abfd
)
3738 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3739 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3740 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3741 unsigned int num_sec
= elf_numsections (abfd
);
3743 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3745 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3746 && bfd_get_format (abfd
) != bfd_core
)
3748 Elf_Internal_Shdr
**hdrpp
;
3751 /* Start after the ELF header. */
3752 off
= i_ehdrp
->e_ehsize
;
3754 /* We are not creating an executable, which means that we are
3755 not creating a program header, and that the actual order of
3756 the sections in the file is unimportant. */
3757 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3759 Elf_Internal_Shdr
*hdr
;
3762 if (hdr
->sh_type
== SHT_REL
3763 || hdr
->sh_type
== SHT_RELA
3764 || i
== tdata
->symtab_section
3765 || i
== tdata
->symtab_shndx_section
3766 || i
== tdata
->strtab_section
)
3768 hdr
->sh_offset
= -1;
3771 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3773 if (i
== SHN_LORESERVE
- 1)
3775 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3776 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3783 Elf_Internal_Shdr
**hdrpp
;
3785 /* Assign file positions for the loaded sections based on the
3786 assignment of sections to segments. */
3787 if (! assign_file_positions_for_segments (abfd
))
3790 /* Assign file positions for the other sections. */
3792 off
= elf_tdata (abfd
)->next_file_pos
;
3793 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< num_sec
; i
++, hdrpp
++)
3795 Elf_Internal_Shdr
*hdr
;
3798 if (hdr
->bfd_section
!= NULL
3799 && hdr
->bfd_section
->filepos
!= 0)
3800 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3801 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3803 ((*_bfd_error_handler
)
3804 (_("%s: warning: allocated section `%s' not in segment"),
3805 bfd_get_filename (abfd
),
3806 (hdr
->bfd_section
== NULL
3808 : hdr
->bfd_section
->name
)));
3809 if ((abfd
->flags
& D_PAGED
) != 0)
3810 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3812 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3813 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3816 else if (hdr
->sh_type
== SHT_REL
3817 || hdr
->sh_type
== SHT_RELA
3818 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3819 || hdr
== i_shdrpp
[tdata
->symtab_shndx_section
]
3820 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3821 hdr
->sh_offset
= -1;
3823 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3825 if (i
== SHN_LORESERVE
- 1)
3827 i
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3828 hdrpp
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
3833 /* Place the section headers. */
3834 off
= align_file_position (off
, bed
->s
->file_align
);
3835 i_ehdrp
->e_shoff
= off
;
3836 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3838 elf_tdata (abfd
)->next_file_pos
= off
;
3847 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3848 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3849 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3851 struct elf_strtab_hash
*shstrtab
;
3852 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3854 i_ehdrp
= elf_elfheader (abfd
);
3855 i_shdrp
= elf_elfsections (abfd
);
3857 shstrtab
= _bfd_elf_strtab_init ();
3858 if (shstrtab
== NULL
)
3861 elf_shstrtab (abfd
) = shstrtab
;
3863 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3864 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3865 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3866 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3868 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3869 i_ehdrp
->e_ident
[EI_DATA
] =
3870 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3871 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3873 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3874 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3876 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3877 i_ehdrp
->e_ident
[count
] = 0;
3879 if ((abfd
->flags
& DYNAMIC
) != 0)
3880 i_ehdrp
->e_type
= ET_DYN
;
3881 else if ((abfd
->flags
& EXEC_P
) != 0)
3882 i_ehdrp
->e_type
= ET_EXEC
;
3883 else if (bfd_get_format (abfd
) == bfd_core
)
3884 i_ehdrp
->e_type
= ET_CORE
;
3886 i_ehdrp
->e_type
= ET_REL
;
3888 switch (bfd_get_arch (abfd
))
3890 case bfd_arch_unknown
:
3891 i_ehdrp
->e_machine
= EM_NONE
;
3894 /* There used to be a long list of cases here, each one setting
3895 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3896 in the corresponding bfd definition. To avoid duplication,
3897 the switch was removed. Machines that need special handling
3898 can generally do it in elf_backend_final_write_processing(),
3899 unless they need the information earlier than the final write.
3900 Such need can generally be supplied by replacing the tests for
3901 e_machine with the conditions used to determine it. */
3903 if (get_elf_backend_data (abfd
) != NULL
)
3904 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
3906 i_ehdrp
->e_machine
= EM_NONE
;
3909 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3910 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3912 /* No program header, for now. */
3913 i_ehdrp
->e_phoff
= 0;
3914 i_ehdrp
->e_phentsize
= 0;
3915 i_ehdrp
->e_phnum
= 0;
3917 /* Each bfd section is section header entry. */
3918 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3919 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3921 /* If we're building an executable, we'll need a program header table. */
3922 if (abfd
->flags
& EXEC_P
)
3924 /* It all happens later. */
3926 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3928 /* elf_build_phdrs() returns a (NULL-terminated) array of
3929 Elf_Internal_Phdrs. */
3930 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3931 i_ehdrp
->e_phoff
= outbase
;
3932 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3937 i_ehdrp
->e_phentsize
= 0;
3939 i_ehdrp
->e_phoff
= 0;
3942 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3943 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".symtab", false);
3944 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3945 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".strtab", false);
3946 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3947 (unsigned int) _bfd_elf_strtab_add (shstrtab
, ".shstrtab", false);
3948 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3949 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3950 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3956 /* Assign file positions for all the reloc sections which are not part
3957 of the loadable file image. */
3960 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3964 unsigned int i
, num_sec
;
3965 Elf_Internal_Shdr
**shdrpp
;
3967 off
= elf_tdata (abfd
)->next_file_pos
;
3969 num_sec
= elf_numsections (abfd
);
3970 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1; i
< num_sec
; i
++, shdrpp
++)
3972 Elf_Internal_Shdr
*shdrp
;
3975 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3976 && shdrp
->sh_offset
== -1)
3977 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3980 elf_tdata (abfd
)->next_file_pos
= off
;
3984 _bfd_elf_write_object_contents (abfd
)
3987 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3988 Elf_Internal_Ehdr
*i_ehdrp
;
3989 Elf_Internal_Shdr
**i_shdrp
;
3991 unsigned int count
, num_sec
;
3993 if (! abfd
->output_has_begun
3994 && ! _bfd_elf_compute_section_file_positions
3995 (abfd
, (struct bfd_link_info
*) NULL
))
3998 i_shdrp
= elf_elfsections (abfd
);
3999 i_ehdrp
= elf_elfheader (abfd
);
4002 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
4006 _bfd_elf_assign_file_positions_for_relocs (abfd
);
4008 /* After writing the headers, we need to write the sections too... */
4009 num_sec
= elf_numsections (abfd
);
4010 for (count
= 1; count
< num_sec
; count
++)
4012 if (bed
->elf_backend_section_processing
)
4013 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
4014 if (i_shdrp
[count
]->contents
)
4016 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
4018 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
4019 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
4022 if (count
== SHN_LORESERVE
- 1)
4023 count
+= SHN_HIRESERVE
+ 1 - SHN_LORESERVE
;
4026 /* Write out the section header names. */
4027 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
4028 || ! _bfd_elf_strtab_emit (abfd
, elf_shstrtab (abfd
)))
4031 if (bed
->elf_backend_final_write_processing
)
4032 (*bed
->elf_backend_final_write_processing
) (abfd
,
4033 elf_tdata (abfd
)->linker
);
4035 return bed
->s
->write_shdrs_and_ehdr (abfd
);
4039 _bfd_elf_write_corefile_contents (abfd
)
4042 /* Hopefully this can be done just like an object file. */
4043 return _bfd_elf_write_object_contents (abfd
);
4046 /* Given a section, search the header to find them. */
4049 _bfd_elf_section_from_bfd_section (abfd
, asect
)
4053 struct elf_backend_data
*bed
;
4056 if (elf_section_data (asect
) != NULL
4057 && elf_section_data (asect
)->this_idx
!= 0)
4058 return elf_section_data (asect
)->this_idx
;
4060 if (bfd_is_abs_section (asect
))
4062 else if (bfd_is_com_section (asect
))
4064 else if (bfd_is_und_section (asect
))
4068 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
4069 int maxindex
= elf_numsections (abfd
);
4071 for (index
= 1; index
< maxindex
; index
++)
4073 Elf_Internal_Shdr
*hdr
= i_shdrp
[index
];
4075 if (hdr
!= NULL
&& hdr
->bfd_section
== asect
)
4081 bed
= get_elf_backend_data (abfd
);
4082 if (bed
->elf_backend_section_from_bfd_section
)
4086 if ((*bed
->elf_backend_section_from_bfd_section
) (abfd
, asect
, &retval
))
4091 bfd_set_error (bfd_error_nonrepresentable_section
);
4096 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4100 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
4102 asymbol
**asym_ptr_ptr
;
4104 asymbol
*asym_ptr
= *asym_ptr_ptr
;
4106 flagword flags
= asym_ptr
->flags
;
4108 /* When gas creates relocations against local labels, it creates its
4109 own symbol for the section, but does put the symbol into the
4110 symbol chain, so udata is 0. When the linker is generating
4111 relocatable output, this section symbol may be for one of the
4112 input sections rather than the output section. */
4113 if (asym_ptr
->udata
.i
== 0
4114 && (flags
& BSF_SECTION_SYM
)
4115 && asym_ptr
->section
)
4119 if (asym_ptr
->section
->output_section
!= NULL
)
4120 indx
= asym_ptr
->section
->output_section
->index
;
4122 indx
= asym_ptr
->section
->index
;
4123 if (indx
< elf_num_section_syms (abfd
)
4124 && elf_section_syms (abfd
)[indx
] != NULL
)
4125 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
4128 idx
= asym_ptr
->udata
.i
;
4132 /* This case can occur when using --strip-symbol on a symbol
4133 which is used in a relocation entry. */
4134 (*_bfd_error_handler
)
4135 (_("%s: symbol `%s' required but not present"),
4136 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
4137 bfd_set_error (bfd_error_no_symbols
);
4144 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
4145 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
4146 elf_symbol_flags (flags
));
4154 /* Copy private BFD data. This copies any program header information. */
4157 copy_private_bfd_data (ibfd
, obfd
)
4161 Elf_Internal_Ehdr
* iehdr
;
4162 struct elf_segment_map
* map
;
4163 struct elf_segment_map
* map_first
;
4164 struct elf_segment_map
** pointer_to_map
;
4165 Elf_Internal_Phdr
* segment
;
4168 unsigned int num_segments
;
4169 boolean phdr_included
= false;
4170 bfd_vma maxpagesize
;
4171 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
4172 unsigned int phdr_adjust_num
= 0;
4174 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4175 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4178 if (elf_tdata (ibfd
)->phdr
== NULL
)
4181 iehdr
= elf_elfheader (ibfd
);
4184 pointer_to_map
= &map_first
;
4186 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
4187 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
4189 /* Returns the end address of the segment + 1. */
4190 #define SEGMENT_END(segment, start) \
4191 (start + (segment->p_memsz > segment->p_filesz \
4192 ? segment->p_memsz : segment->p_filesz))
4194 /* Returns true if the given section is contained within
4195 the given segment. VMA addresses are compared. */
4196 #define IS_CONTAINED_BY_VMA(section, segment) \
4197 (section->vma >= segment->p_vaddr \
4198 && (section->vma + section->_raw_size) \
4199 <= (SEGMENT_END (segment, segment->p_vaddr)))
4201 /* Returns true if the given section is contained within
4202 the given segment. LMA addresses are compared. */
4203 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4204 (section->lma >= base \
4205 && (section->lma + section->_raw_size) \
4206 <= SEGMENT_END (segment, base))
4208 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4209 #define IS_COREFILE_NOTE(p, s) \
4210 (p->p_type == PT_NOTE \
4211 && bfd_get_format (ibfd) == bfd_core \
4212 && s->vma == 0 && s->lma == 0 \
4213 && (bfd_vma) s->filepos >= p->p_offset \
4214 && (bfd_vma) s->filepos + s->_raw_size \
4215 <= p->p_offset + p->p_filesz)
4217 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4218 linker, which generates a PT_INTERP section with p_vaddr and
4219 p_memsz set to 0. */
4220 #define IS_SOLARIS_PT_INTERP(p, s) \
4222 && p->p_filesz > 0 \
4223 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4224 && s->_raw_size > 0 \
4225 && (bfd_vma) s->filepos >= p->p_offset \
4226 && ((bfd_vma) s->filepos + s->_raw_size \
4227 <= p->p_offset + p->p_filesz))
4229 /* Decide if the given section should be included in the given segment.
4230 A section will be included if:
4231 1. It is within the address space of the segment -- we use the LMA
4232 if that is set for the segment and the VMA otherwise,
4233 2. It is an allocated segment,
4234 3. There is an output section associated with it,
4235 4. The section has not already been allocated to a previous segment. */
4236 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4237 (((((segment->p_paddr \
4238 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4239 : IS_CONTAINED_BY_VMA (section, segment)) \
4240 || IS_SOLARIS_PT_INTERP (segment, section)) \
4241 && (section->flags & SEC_ALLOC) != 0) \
4242 || IS_COREFILE_NOTE (segment, section)) \
4243 && section->output_section != NULL \
4244 && section->segment_mark == false)
4246 /* Returns true iff seg1 starts after the end of seg2. */
4247 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4248 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4250 /* Returns true iff seg1 and seg2 overlap. */
4251 #define SEGMENT_OVERLAPS(seg1, seg2) \
4252 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4254 /* Initialise the segment mark field. */
4255 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4256 section
->segment_mark
= false;
4258 /* Scan through the segments specified in the program header
4259 of the input BFD. For this first scan we look for overlaps
4260 in the loadable segments. These can be created by weird
4261 parameters to objcopy. */
4262 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4267 Elf_Internal_Phdr
*segment2
;
4269 if (segment
->p_type
!= PT_LOAD
)
4272 /* Determine if this segment overlaps any previous segments. */
4273 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4275 bfd_signed_vma extra_length
;
4277 if (segment2
->p_type
!= PT_LOAD
4278 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4281 /* Merge the two segments together. */
4282 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4284 /* Extend SEGMENT2 to include SEGMENT and then delete
4287 SEGMENT_END (segment
, segment
->p_vaddr
)
4288 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4290 if (extra_length
> 0)
4292 segment2
->p_memsz
+= extra_length
;
4293 segment2
->p_filesz
+= extra_length
;
4296 segment
->p_type
= PT_NULL
;
4298 /* Since we have deleted P we must restart the outer loop. */
4300 segment
= elf_tdata (ibfd
)->phdr
;
4305 /* Extend SEGMENT to include SEGMENT2 and then delete
4308 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4309 - SEGMENT_END (segment
, segment
->p_vaddr
);
4311 if (extra_length
> 0)
4313 segment
->p_memsz
+= extra_length
;
4314 segment
->p_filesz
+= extra_length
;
4317 segment2
->p_type
= PT_NULL
;
4322 /* The second scan attempts to assign sections to segments. */
4323 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4327 unsigned int section_count
;
4328 asection
** sections
;
4329 asection
* output_section
;
4331 bfd_vma matching_lma
;
4332 bfd_vma suggested_lma
;
4336 if (segment
->p_type
== PT_NULL
)
4339 /* Compute how many sections might be placed into this segment. */
4341 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4342 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4345 /* Allocate a segment map big enough to contain all of the
4346 sections we have selected. */
4347 amt
= sizeof (struct elf_segment_map
);
4348 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4349 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4353 /* Initialise the fields of the segment map. Default to
4354 using the physical address of the segment in the input BFD. */
4356 map
->p_type
= segment
->p_type
;
4357 map
->p_flags
= segment
->p_flags
;
4358 map
->p_flags_valid
= 1;
4359 map
->p_paddr
= segment
->p_paddr
;
4360 map
->p_paddr_valid
= 1;
4362 /* Determine if this segment contains the ELF file header
4363 and if it contains the program headers themselves. */
4364 map
->includes_filehdr
= (segment
->p_offset
== 0
4365 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4367 map
->includes_phdrs
= 0;
4369 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4371 map
->includes_phdrs
=
4372 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4373 && (segment
->p_offset
+ segment
->p_filesz
4374 >= ((bfd_vma
) iehdr
->e_phoff
4375 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4377 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4378 phdr_included
= true;
4381 if (section_count
== 0)
4383 /* Special segments, such as the PT_PHDR segment, may contain
4384 no sections, but ordinary, loadable segments should contain
4386 if (segment
->p_type
== PT_LOAD
)
4388 (_("%s: warning: Empty loadable segment detected\n"),
4389 bfd_archive_filename (ibfd
));
4392 *pointer_to_map
= map
;
4393 pointer_to_map
= &map
->next
;
4398 /* Now scan the sections in the input BFD again and attempt
4399 to add their corresponding output sections to the segment map.
4400 The problem here is how to handle an output section which has
4401 been moved (ie had its LMA changed). There are four possibilities:
4403 1. None of the sections have been moved.
4404 In this case we can continue to use the segment LMA from the
4407 2. All of the sections have been moved by the same amount.
4408 In this case we can change the segment's LMA to match the LMA
4409 of the first section.
4411 3. Some of the sections have been moved, others have not.
4412 In this case those sections which have not been moved can be
4413 placed in the current segment which will have to have its size,
4414 and possibly its LMA changed, and a new segment or segments will
4415 have to be created to contain the other sections.
4417 4. The sections have been moved, but not be the same amount.
4418 In this case we can change the segment's LMA to match the LMA
4419 of the first section and we will have to create a new segment
4420 or segments to contain the other sections.
4422 In order to save time, we allocate an array to hold the section
4423 pointers that we are interested in. As these sections get assigned
4424 to a segment, they are removed from this array. */
4426 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4427 sections
= (asection
**) bfd_malloc (amt
);
4428 if (sections
== NULL
)
4431 /* Step One: Scan for segment vs section LMA conflicts.
4432 Also add the sections to the section array allocated above.
4433 Also add the sections to the current segment. In the common
4434 case, where the sections have not been moved, this means that
4435 we have completely filled the segment, and there is nothing
4441 for (j
= 0, section
= ibfd
->sections
;
4443 section
= section
->next
)
4445 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4447 output_section
= section
->output_section
;
4449 sections
[j
++] = section
;
4451 /* The Solaris native linker always sets p_paddr to 0.
4452 We try to catch that case here, and set it to the
4454 if (segment
->p_paddr
== 0
4455 && segment
->p_vaddr
!= 0
4457 && output_section
->lma
!= 0
4458 && (output_section
->vma
== (segment
->p_vaddr
4459 + (map
->includes_filehdr
4462 + (map
->includes_phdrs
4464 * iehdr
->e_phentsize
)
4466 map
->p_paddr
= segment
->p_vaddr
;
4468 /* Match up the physical address of the segment with the
4469 LMA address of the output section. */
4470 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4471 || IS_COREFILE_NOTE (segment
, section
))
4473 if (matching_lma
== 0)
4474 matching_lma
= output_section
->lma
;
4476 /* We assume that if the section fits within the segment
4477 then it does not overlap any other section within that
4479 map
->sections
[isec
++] = output_section
;
4481 else if (suggested_lma
== 0)
4482 suggested_lma
= output_section
->lma
;
4486 BFD_ASSERT (j
== section_count
);
4488 /* Step Two: Adjust the physical address of the current segment,
4490 if (isec
== section_count
)
4492 /* All of the sections fitted within the segment as currently
4493 specified. This is the default case. Add the segment to
4494 the list of built segments and carry on to process the next
4495 program header in the input BFD. */
4496 map
->count
= section_count
;
4497 *pointer_to_map
= map
;
4498 pointer_to_map
= &map
->next
;
4505 if (matching_lma
!= 0)
4507 /* At least one section fits inside the current segment.
4508 Keep it, but modify its physical address to match the
4509 LMA of the first section that fitted. */
4510 map
->p_paddr
= matching_lma
;
4514 /* None of the sections fitted inside the current segment.
4515 Change the current segment's physical address to match
4516 the LMA of the first section. */
4517 map
->p_paddr
= suggested_lma
;
4520 /* Offset the segment physical address from the lma
4521 to allow for space taken up by elf headers. */
4522 if (map
->includes_filehdr
)
4523 map
->p_paddr
-= iehdr
->e_ehsize
;
4525 if (map
->includes_phdrs
)
4527 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4529 /* iehdr->e_phnum is just an estimate of the number
4530 of program headers that we will need. Make a note
4531 here of the number we used and the segment we chose
4532 to hold these headers, so that we can adjust the
4533 offset when we know the correct value. */
4534 phdr_adjust_num
= iehdr
->e_phnum
;
4535 phdr_adjust_seg
= map
;
4539 /* Step Three: Loop over the sections again, this time assigning
4540 those that fit to the current segment and remvoing them from the
4541 sections array; but making sure not to leave large gaps. Once all
4542 possible sections have been assigned to the current segment it is
4543 added to the list of built segments and if sections still remain
4544 to be assigned, a new segment is constructed before repeating
4552 /* Fill the current segment with sections that fit. */
4553 for (j
= 0; j
< section_count
; j
++)
4555 section
= sections
[j
];
4557 if (section
== NULL
)
4560 output_section
= section
->output_section
;
4562 BFD_ASSERT (output_section
!= NULL
);
4564 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4565 || IS_COREFILE_NOTE (segment
, section
))
4567 if (map
->count
== 0)
4569 /* If the first section in a segment does not start at
4570 the beginning of the segment, then something is
4572 if (output_section
->lma
!=
4574 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4575 + (map
->includes_phdrs
4576 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4582 asection
* prev_sec
;
4584 prev_sec
= map
->sections
[map
->count
- 1];
4586 /* If the gap between the end of the previous section
4587 and the start of this section is more than
4588 maxpagesize then we need to start a new segment. */
4589 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
,
4591 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4592 || ((prev_sec
->lma
+ prev_sec
->_raw_size
)
4593 > output_section
->lma
))
4595 if (suggested_lma
== 0)
4596 suggested_lma
= output_section
->lma
;
4602 map
->sections
[map
->count
++] = output_section
;
4605 section
->segment_mark
= true;
4607 else if (suggested_lma
== 0)
4608 suggested_lma
= output_section
->lma
;
4611 BFD_ASSERT (map
->count
> 0);
4613 /* Add the current segment to the list of built segments. */
4614 *pointer_to_map
= map
;
4615 pointer_to_map
= &map
->next
;
4617 if (isec
< section_count
)
4619 /* We still have not allocated all of the sections to
4620 segments. Create a new segment here, initialise it
4621 and carry on looping. */
4622 amt
= sizeof (struct elf_segment_map
);
4623 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4624 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4628 /* Initialise the fields of the segment map. Set the physical
4629 physical address to the LMA of the first section that has
4630 not yet been assigned. */
4632 map
->p_type
= segment
->p_type
;
4633 map
->p_flags
= segment
->p_flags
;
4634 map
->p_flags_valid
= 1;
4635 map
->p_paddr
= suggested_lma
;
4636 map
->p_paddr_valid
= 1;
4637 map
->includes_filehdr
= 0;
4638 map
->includes_phdrs
= 0;
4641 while (isec
< section_count
);
4646 /* The Solaris linker creates program headers in which all the
4647 p_paddr fields are zero. When we try to objcopy or strip such a
4648 file, we get confused. Check for this case, and if we find it
4649 reset the p_paddr_valid fields. */
4650 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4651 if (map
->p_paddr
!= 0)
4655 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4656 map
->p_paddr_valid
= 0;
4659 elf_tdata (obfd
)->segment_map
= map_first
;
4661 /* If we had to estimate the number of program headers that were
4662 going to be needed, then check our estimate now and adjust
4663 the offset if necessary. */
4664 if (phdr_adjust_seg
!= NULL
)
4668 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4671 if (count
> phdr_adjust_num
)
4672 phdr_adjust_seg
->p_paddr
4673 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4677 /* Final Step: Sort the segments into ascending order of physical
4679 if (map_first
!= NULL
)
4681 struct elf_segment_map
*prev
;
4684 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4686 /* Yes I know - its a bubble sort.... */
4687 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4689 /* Swap map and map->next. */
4690 prev
->next
= map
->next
;
4691 map
->next
= map
->next
->next
;
4692 prev
->next
->next
= map
;
4702 #undef IS_CONTAINED_BY_VMA
4703 #undef IS_CONTAINED_BY_LMA
4704 #undef IS_COREFILE_NOTE
4705 #undef IS_SOLARIS_PT_INTERP
4706 #undef INCLUDE_SECTION_IN_SEGMENT
4707 #undef SEGMENT_AFTER_SEGMENT
4708 #undef SEGMENT_OVERLAPS
4712 /* Copy private section information. This copies over the entsize
4713 field, and sometimes the info field. */
4716 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4722 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4724 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4725 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4728 /* Copy over private BFD data if it has not already been copied.
4729 This must be done here, rather than in the copy_private_bfd_data
4730 entry point, because the latter is called after the section
4731 contents have been set, which means that the program headers have
4732 already been worked out. */
4733 if (elf_tdata (obfd
)->segment_map
== NULL
4734 && elf_tdata (ibfd
)->phdr
!= NULL
)
4738 /* Only set up the segments if there are no more SEC_ALLOC
4739 sections. FIXME: This won't do the right thing if objcopy is
4740 used to remove the last SEC_ALLOC section, since objcopy
4741 won't call this routine in that case. */
4742 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4743 if ((s
->flags
& SEC_ALLOC
) != 0)
4747 if (! copy_private_bfd_data (ibfd
, obfd
))
4752 ihdr
= &elf_section_data (isec
)->this_hdr
;
4753 ohdr
= &elf_section_data (osec
)->this_hdr
;
4755 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4757 if (ihdr
->sh_type
== SHT_SYMTAB
4758 || ihdr
->sh_type
== SHT_DYNSYM
4759 || ihdr
->sh_type
== SHT_GNU_verneed
4760 || ihdr
->sh_type
== SHT_GNU_verdef
)
4761 ohdr
->sh_info
= ihdr
->sh_info
;
4763 elf_section_data (osec
)->use_rela_p
4764 = elf_section_data (isec
)->use_rela_p
;
4769 /* Copy private symbol information. If this symbol is in a section
4770 which we did not map into a BFD section, try to map the section
4771 index correctly. We use special macro definitions for the mapped
4772 section indices; these definitions are interpreted by the
4773 swap_out_syms function. */
4775 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4776 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4777 #define MAP_STRTAB (SHN_HIOS + 3)
4778 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4779 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4782 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4788 elf_symbol_type
*isym
, *osym
;
4790 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4791 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4794 isym
= elf_symbol_from (ibfd
, isymarg
);
4795 osym
= elf_symbol_from (obfd
, osymarg
);
4799 && bfd_is_abs_section (isym
->symbol
.section
))
4803 shndx
= isym
->internal_elf_sym
.st_shndx
;
4804 if (shndx
== elf_onesymtab (ibfd
))
4805 shndx
= MAP_ONESYMTAB
;
4806 else if (shndx
== elf_dynsymtab (ibfd
))
4807 shndx
= MAP_DYNSYMTAB
;
4808 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4810 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4811 shndx
= MAP_SHSTRTAB
;
4812 else if (shndx
== elf_tdata (ibfd
)->symtab_shndx_section
)
4813 shndx
= MAP_SYM_SHNDX
;
4814 osym
->internal_elf_sym
.st_shndx
= shndx
;
4820 /* Swap out the symbols. */
4823 swap_out_syms (abfd
, sttp
, relocatable_p
)
4825 struct bfd_strtab_hash
**sttp
;
4828 struct elf_backend_data
*bed
;
4831 struct bfd_strtab_hash
*stt
;
4832 Elf_Internal_Shdr
*symtab_hdr
;
4833 Elf_Internal_Shdr
*symtab_shndx_hdr
;
4834 Elf_Internal_Shdr
*symstrtab_hdr
;
4835 char *outbound_syms
;
4836 char *outbound_shndx
;
4840 if (!elf_map_symbols (abfd
))
4843 /* Dump out the symtabs. */
4844 stt
= _bfd_elf_stringtab_init ();
4848 bed
= get_elf_backend_data (abfd
);
4849 symcount
= bfd_get_symcount (abfd
);
4850 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4851 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4852 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4853 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4854 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4855 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4857 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4858 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4860 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
4861 outbound_syms
= bfd_alloc (abfd
, amt
);
4862 if (outbound_syms
== NULL
)
4864 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4866 outbound_shndx
= NULL
;
4867 symtab_shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
4868 if (symtab_shndx_hdr
->sh_name
!= 0)
4870 amt
= (bfd_size_type
) (1 + symcount
) * sizeof (Elf_External_Sym_Shndx
);
4871 outbound_shndx
= bfd_alloc (abfd
, amt
);
4872 if (outbound_shndx
== NULL
)
4874 memset (outbound_shndx
, 0, (unsigned long) amt
);
4875 symtab_shndx_hdr
->contents
= outbound_shndx
;
4876 symtab_shndx_hdr
->sh_type
= SHT_SYMTAB_SHNDX
;
4877 symtab_shndx_hdr
->sh_size
= amt
;
4878 symtab_shndx_hdr
->sh_addralign
= sizeof (Elf_External_Sym_Shndx
);
4879 symtab_shndx_hdr
->sh_entsize
= sizeof (Elf_External_Sym_Shndx
);
4882 /* now generate the data (for "contents") */
4884 /* Fill in zeroth symbol and swap it out. */
4885 Elf_Internal_Sym sym
;
4891 sym
.st_shndx
= SHN_UNDEF
;
4892 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
4893 outbound_syms
+= bed
->s
->sizeof_sym
;
4894 if (outbound_shndx
!= NULL
)
4895 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
4898 syms
= bfd_get_outsymbols (abfd
);
4899 for (idx
= 0; idx
< symcount
; idx
++)
4901 Elf_Internal_Sym sym
;
4902 bfd_vma value
= syms
[idx
]->value
;
4903 elf_symbol_type
*type_ptr
;
4904 flagword flags
= syms
[idx
]->flags
;
4907 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4909 /* Local section symbols have no name. */
4914 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4917 if (sym
.st_name
== (unsigned long) -1)
4921 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4923 if ((flags
& BSF_SECTION_SYM
) == 0
4924 && bfd_is_com_section (syms
[idx
]->section
))
4926 /* ELF common symbols put the alignment into the `value' field,
4927 and the size into the `size' field. This is backwards from
4928 how BFD handles it, so reverse it here. */
4929 sym
.st_size
= value
;
4930 if (type_ptr
== NULL
4931 || type_ptr
->internal_elf_sym
.st_value
== 0)
4932 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4934 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4935 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4936 (abfd
, syms
[idx
]->section
);
4940 asection
*sec
= syms
[idx
]->section
;
4943 if (sec
->output_section
)
4945 value
+= sec
->output_offset
;
4946 sec
= sec
->output_section
;
4948 /* Don't add in the section vma for relocatable output. */
4949 if (! relocatable_p
)
4951 sym
.st_value
= value
;
4952 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4954 if (bfd_is_abs_section (sec
)
4956 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4958 /* This symbol is in a real ELF section which we did
4959 not create as a BFD section. Undo the mapping done
4960 by copy_private_symbol_data. */
4961 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4965 shndx
= elf_onesymtab (abfd
);
4968 shndx
= elf_dynsymtab (abfd
);
4971 shndx
= elf_tdata (abfd
)->strtab_section
;
4974 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4977 shndx
= elf_tdata (abfd
)->symtab_shndx_section
;
4985 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4991 /* Writing this would be a hell of a lot easier if
4992 we had some decent documentation on bfd, and
4993 knew what to expect of the library, and what to
4994 demand of applications. For example, it
4995 appears that `objcopy' might not set the
4996 section of a symbol to be a section that is
4997 actually in the output file. */
4998 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4999 BFD_ASSERT (sec2
!= 0);
5000 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
5001 BFD_ASSERT (shndx
!= -1);
5005 sym
.st_shndx
= shndx
;
5008 if ((flags
& BSF_FUNCTION
) != 0)
5010 else if ((flags
& BSF_OBJECT
) != 0)
5015 /* Processor-specific types */
5016 if (type_ptr
!= NULL
5017 && bed
->elf_backend_get_symbol_type
)
5018 type
= ((*bed
->elf_backend_get_symbol_type
)
5019 (&type_ptr
->internal_elf_sym
, type
));
5021 if (flags
& BSF_SECTION_SYM
)
5023 if (flags
& BSF_GLOBAL
)
5024 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
5026 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
5028 else if (bfd_is_com_section (syms
[idx
]->section
))
5029 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
5030 else if (bfd_is_und_section (syms
[idx
]->section
))
5031 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
5035 else if (flags
& BSF_FILE
)
5036 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
5039 int bind
= STB_LOCAL
;
5041 if (flags
& BSF_LOCAL
)
5043 else if (flags
& BSF_WEAK
)
5045 else if (flags
& BSF_GLOBAL
)
5048 sym
.st_info
= ELF_ST_INFO (bind
, type
);
5051 if (type_ptr
!= NULL
)
5052 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
5056 bed
->s
->swap_symbol_out (abfd
, &sym
, outbound_syms
, outbound_shndx
);
5057 outbound_syms
+= bed
->s
->sizeof_sym
;
5058 if (outbound_shndx
!= NULL
)
5059 outbound_shndx
+= sizeof (Elf_External_Sym_Shndx
);
5063 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
5064 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
5066 symstrtab_hdr
->sh_flags
= 0;
5067 symstrtab_hdr
->sh_addr
= 0;
5068 symstrtab_hdr
->sh_entsize
= 0;
5069 symstrtab_hdr
->sh_link
= 0;
5070 symstrtab_hdr
->sh_info
= 0;
5071 symstrtab_hdr
->sh_addralign
= 1;
5076 /* Return the number of bytes required to hold the symtab vector.
5078 Note that we base it on the count plus 1, since we will null terminate
5079 the vector allocated based on this size. However, the ELF symbol table
5080 always has a dummy entry as symbol #0, so it ends up even. */
5083 _bfd_elf_get_symtab_upper_bound (abfd
)
5088 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
5090 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5091 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
5097 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
5102 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
5104 if (elf_dynsymtab (abfd
) == 0)
5106 bfd_set_error (bfd_error_invalid_operation
);
5110 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
5111 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
5117 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
5118 bfd
*abfd ATTRIBUTE_UNUSED
;
5121 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
5124 /* Canonicalize the relocs. */
5127 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
5135 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5137 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
5140 tblptr
= section
->relocation
;
5141 for (i
= 0; i
< section
->reloc_count
; i
++)
5142 *relptr
++ = tblptr
++;
5146 return section
->reloc_count
;
5150 _bfd_elf_get_symtab (abfd
, alocation
)
5152 asymbol
**alocation
;
5154 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5155 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
5158 bfd_get_symcount (abfd
) = symcount
;
5163 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
5165 asymbol
**alocation
;
5167 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5168 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
5171 /* Return the size required for the dynamic reloc entries. Any
5172 section that was actually installed in the BFD, and has type
5173 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5174 considered to be a dynamic reloc section. */
5177 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
5183 if (elf_dynsymtab (abfd
) == 0)
5185 bfd_set_error (bfd_error_invalid_operation
);
5189 ret
= sizeof (arelent
*);
5190 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5191 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5192 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5193 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5194 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
5195 * sizeof (arelent
*));
5200 /* Canonicalize the dynamic relocation entries. Note that we return
5201 the dynamic relocations as a single block, although they are
5202 actually associated with particular sections; the interface, which
5203 was designed for SunOS style shared libraries, expects that there
5204 is only one set of dynamic relocs. Any section that was actually
5205 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5206 the dynamic symbol table, is considered to be a dynamic reloc
5210 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
5215 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
5219 if (elf_dynsymtab (abfd
) == 0)
5221 bfd_set_error (bfd_error_invalid_operation
);
5225 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
5227 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
5229 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
5230 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
5231 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
5236 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
5238 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
5240 for (i
= 0; i
< count
; i
++)
5251 /* Read in the version information. */
5254 _bfd_elf_slurp_version_tables (abfd
)
5257 bfd_byte
*contents
= NULL
;
5260 if (elf_dynverdef (abfd
) != 0)
5262 Elf_Internal_Shdr
*hdr
;
5263 Elf_External_Verdef
*everdef
;
5264 Elf_Internal_Verdef
*iverdef
;
5265 Elf_Internal_Verdef
*iverdefarr
;
5266 Elf_Internal_Verdef iverdefmem
;
5268 unsigned int maxidx
;
5270 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5272 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5273 if (contents
== NULL
)
5275 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5276 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5279 /* We know the number of entries in the section but not the maximum
5280 index. Therefore we have to run through all entries and find
5282 everdef
= (Elf_External_Verdef
*) contents
;
5284 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5286 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5288 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5289 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5291 everdef
= ((Elf_External_Verdef
*)
5292 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5295 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5296 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5297 if (elf_tdata (abfd
)->verdef
== NULL
)
5300 elf_tdata (abfd
)->cverdefs
= maxidx
;
5302 everdef
= (Elf_External_Verdef
*) contents
;
5303 iverdefarr
= elf_tdata (abfd
)->verdef
;
5304 for (i
= 0; i
< hdr
->sh_info
; i
++)
5306 Elf_External_Verdaux
*everdaux
;
5307 Elf_Internal_Verdaux
*iverdaux
;
5310 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5312 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5313 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5315 iverdef
->vd_bfd
= abfd
;
5317 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5318 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5319 if (iverdef
->vd_auxptr
== NULL
)
5322 everdaux
= ((Elf_External_Verdaux
*)
5323 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5324 iverdaux
= iverdef
->vd_auxptr
;
5325 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5327 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5329 iverdaux
->vda_nodename
=
5330 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5331 iverdaux
->vda_name
);
5332 if (iverdaux
->vda_nodename
== NULL
)
5335 if (j
+ 1 < iverdef
->vd_cnt
)
5336 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5338 iverdaux
->vda_nextptr
= NULL
;
5340 everdaux
= ((Elf_External_Verdaux
*)
5341 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5344 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5346 if (i
+ 1 < hdr
->sh_info
)
5347 iverdef
->vd_nextdef
= iverdef
+ 1;
5349 iverdef
->vd_nextdef
= NULL
;
5351 everdef
= ((Elf_External_Verdef
*)
5352 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5359 if (elf_dynverref (abfd
) != 0)
5361 Elf_Internal_Shdr
*hdr
;
5362 Elf_External_Verneed
*everneed
;
5363 Elf_Internal_Verneed
*iverneed
;
5366 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5368 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5369 elf_tdata (abfd
)->verref
=
5370 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5371 if (elf_tdata (abfd
)->verref
== NULL
)
5374 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5376 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5377 if (contents
== NULL
)
5379 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5380 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5383 everneed
= (Elf_External_Verneed
*) contents
;
5384 iverneed
= elf_tdata (abfd
)->verref
;
5385 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5387 Elf_External_Vernaux
*evernaux
;
5388 Elf_Internal_Vernaux
*ivernaux
;
5391 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5393 iverneed
->vn_bfd
= abfd
;
5395 iverneed
->vn_filename
=
5396 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5398 if (iverneed
->vn_filename
== NULL
)
5401 amt
= iverneed
->vn_cnt
;
5402 amt
*= sizeof (Elf_Internal_Vernaux
);
5403 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5405 evernaux
= ((Elf_External_Vernaux
*)
5406 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5407 ivernaux
= iverneed
->vn_auxptr
;
5408 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5410 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5412 ivernaux
->vna_nodename
=
5413 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5414 ivernaux
->vna_name
);
5415 if (ivernaux
->vna_nodename
== NULL
)
5418 if (j
+ 1 < iverneed
->vn_cnt
)
5419 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5421 ivernaux
->vna_nextptr
= NULL
;
5423 evernaux
= ((Elf_External_Vernaux
*)
5424 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5427 if (i
+ 1 < hdr
->sh_info
)
5428 iverneed
->vn_nextref
= iverneed
+ 1;
5430 iverneed
->vn_nextref
= NULL
;
5432 everneed
= ((Elf_External_Verneed
*)
5433 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5443 if (contents
== NULL
)
5449 _bfd_elf_make_empty_symbol (abfd
)
5452 elf_symbol_type
*newsym
;
5453 bfd_size_type amt
= sizeof (elf_symbol_type
);
5455 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5460 newsym
->symbol
.the_bfd
= abfd
;
5461 return &newsym
->symbol
;
5466 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5467 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5471 bfd_symbol_info (symbol
, ret
);
5474 /* Return whether a symbol name implies a local symbol. Most targets
5475 use this function for the is_local_label_name entry point, but some
5479 _bfd_elf_is_local_label_name (abfd
, name
)
5480 bfd
*abfd ATTRIBUTE_UNUSED
;
5483 /* Normal local symbols start with ``.L''. */
5484 if (name
[0] == '.' && name
[1] == 'L')
5487 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5488 DWARF debugging symbols starting with ``..''. */
5489 if (name
[0] == '.' && name
[1] == '.')
5492 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5493 emitting DWARF debugging output. I suspect this is actually a
5494 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5495 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5496 underscore to be emitted on some ELF targets). For ease of use,
5497 we treat such symbols as local. */
5498 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5505 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5506 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5507 asymbol
*symbol ATTRIBUTE_UNUSED
;
5514 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5516 enum bfd_architecture arch
;
5517 unsigned long machine
;
5519 /* If this isn't the right architecture for this backend, and this
5520 isn't the generic backend, fail. */
5521 if (arch
!= get_elf_backend_data (abfd
)->arch
5522 && arch
!= bfd_arch_unknown
5523 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5526 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5529 /* Find the function to a particular section and offset,
5530 for error reporting. */
5533 elf_find_function (abfd
, section
, symbols
, offset
,
5534 filename_ptr
, functionname_ptr
)
5535 bfd
*abfd ATTRIBUTE_UNUSED
;
5539 const char **filename_ptr
;
5540 const char **functionname_ptr
;
5542 const char *filename
;
5551 for (p
= symbols
; *p
!= NULL
; p
++)
5555 q
= (elf_symbol_type
*) *p
;
5557 if (bfd_get_section (&q
->symbol
) != section
)
5560 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5565 filename
= bfd_asymbol_name (&q
->symbol
);
5569 if (q
->symbol
.section
== section
5570 && q
->symbol
.value
>= low_func
5571 && q
->symbol
.value
<= offset
)
5573 func
= (asymbol
*) q
;
5574 low_func
= q
->symbol
.value
;
5584 *filename_ptr
= filename
;
5585 if (functionname_ptr
)
5586 *functionname_ptr
= bfd_asymbol_name (func
);
5591 /* Find the nearest line to a particular section and offset,
5592 for error reporting. */
5595 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5596 filename_ptr
, functionname_ptr
, line_ptr
)
5601 const char **filename_ptr
;
5602 const char **functionname_ptr
;
5603 unsigned int *line_ptr
;
5607 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5608 filename_ptr
, functionname_ptr
,
5611 if (!*functionname_ptr
)
5612 elf_find_function (abfd
, section
, symbols
, offset
,
5613 *filename_ptr
? NULL
: filename_ptr
,
5619 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5620 filename_ptr
, functionname_ptr
,
5622 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5624 if (!*functionname_ptr
)
5625 elf_find_function (abfd
, section
, symbols
, offset
,
5626 *filename_ptr
? NULL
: filename_ptr
,
5632 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5633 &found
, filename_ptr
,
5634 functionname_ptr
, line_ptr
,
5635 &elf_tdata (abfd
)->line_info
))
5640 if (symbols
== NULL
)
5643 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5644 filename_ptr
, functionname_ptr
))
5652 _bfd_elf_sizeof_headers (abfd
, reloc
)
5658 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5660 ret
+= get_program_header_size (abfd
);
5665 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5670 bfd_size_type count
;
5672 Elf_Internal_Shdr
*hdr
;
5675 if (! abfd
->output_has_begun
5676 && ! _bfd_elf_compute_section_file_positions
5677 (abfd
, (struct bfd_link_info
*) NULL
))
5680 hdr
= &elf_section_data (section
)->this_hdr
;
5681 pos
= hdr
->sh_offset
+ offset
;
5682 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5683 || bfd_bwrite (location
, count
, abfd
) != count
)
5690 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5691 bfd
*abfd ATTRIBUTE_UNUSED
;
5692 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5693 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5700 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5703 Elf_Internal_Rel
*dst
;
5709 /* Try to convert a non-ELF reloc into an ELF one. */
5712 _bfd_elf_validate_reloc (abfd
, areloc
)
5716 /* Check whether we really have an ELF howto. */
5718 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5720 bfd_reloc_code_real_type code
;
5721 reloc_howto_type
*howto
;
5723 /* Alien reloc: Try to determine its type to replace it with an
5724 equivalent ELF reloc. */
5726 if (areloc
->howto
->pc_relative
)
5728 switch (areloc
->howto
->bitsize
)
5731 code
= BFD_RELOC_8_PCREL
;
5734 code
= BFD_RELOC_12_PCREL
;
5737 code
= BFD_RELOC_16_PCREL
;
5740 code
= BFD_RELOC_24_PCREL
;
5743 code
= BFD_RELOC_32_PCREL
;
5746 code
= BFD_RELOC_64_PCREL
;
5752 howto
= bfd_reloc_type_lookup (abfd
, code
);
5754 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5756 if (howto
->pcrel_offset
)
5757 areloc
->addend
+= areloc
->address
;
5759 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5764 switch (areloc
->howto
->bitsize
)
5770 code
= BFD_RELOC_14
;
5773 code
= BFD_RELOC_16
;
5776 code
= BFD_RELOC_26
;
5779 code
= BFD_RELOC_32
;
5782 code
= BFD_RELOC_64
;
5788 howto
= bfd_reloc_type_lookup (abfd
, code
);
5792 areloc
->howto
= howto
;
5800 (*_bfd_error_handler
)
5801 (_("%s: unsupported relocation type %s"),
5802 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5803 bfd_set_error (bfd_error_bad_value
);
5808 _bfd_elf_close_and_cleanup (abfd
)
5811 if (bfd_get_format (abfd
) == bfd_object
)
5813 if (elf_shstrtab (abfd
) != NULL
)
5814 _bfd_elf_strtab_free (elf_shstrtab (abfd
));
5817 return _bfd_generic_close_and_cleanup (abfd
);
5820 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5821 in the relocation's offset. Thus we cannot allow any sort of sanity
5822 range-checking to interfere. There is nothing else to do in processing
5825 bfd_reloc_status_type
5826 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5827 bfd
*abfd ATTRIBUTE_UNUSED
;
5828 arelent
*re ATTRIBUTE_UNUSED
;
5829 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5830 PTR data ATTRIBUTE_UNUSED
;
5831 asection
*is ATTRIBUTE_UNUSED
;
5832 bfd
*obfd ATTRIBUTE_UNUSED
;
5833 char **errmsg ATTRIBUTE_UNUSED
;
5835 return bfd_reloc_ok
;
5838 /* Elf core file support. Much of this only works on native
5839 toolchains, since we rely on knowing the
5840 machine-dependent procfs structure in order to pick
5841 out details about the corefile. */
5843 #ifdef HAVE_SYS_PROCFS_H
5844 # include <sys/procfs.h>
5847 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5850 elfcore_make_pid (abfd
)
5853 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5854 + (elf_tdata (abfd
)->core_pid
));
5857 /* If there isn't a section called NAME, make one, using
5858 data from SECT. Note, this function will generate a
5859 reference to NAME, so you shouldn't deallocate or
5863 elfcore_maybe_make_sect (abfd
, name
, sect
)
5870 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5873 sect2
= bfd_make_section (abfd
, name
);
5877 sect2
->_raw_size
= sect
->_raw_size
;
5878 sect2
->filepos
= sect
->filepos
;
5879 sect2
->flags
= sect
->flags
;
5880 sect2
->alignment_power
= sect
->alignment_power
;
5884 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5885 actually creates up to two pseudosections:
5886 - For the single-threaded case, a section named NAME, unless
5887 such a section already exists.
5888 - For the multi-threaded case, a section named "NAME/PID", where
5889 PID is elfcore_make_pid (abfd).
5890 Both pseudosections have identical contents. */
5892 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5899 char *threaded_name
;
5902 /* Build the section name. */
5904 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5905 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5906 if (threaded_name
== NULL
)
5908 strcpy (threaded_name
, buf
);
5910 sect
= bfd_make_section (abfd
, threaded_name
);
5913 sect
->_raw_size
= size
;
5914 sect
->filepos
= filepos
;
5915 sect
->flags
= SEC_HAS_CONTENTS
;
5916 sect
->alignment_power
= 2;
5918 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5921 /* prstatus_t exists on:
5923 linux 2.[01] + glibc
5927 #if defined (HAVE_PRSTATUS_T)
5928 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
5931 elfcore_grok_prstatus (abfd
, note
)
5933 Elf_Internal_Note
*note
;
5938 if (note
->descsz
== sizeof (prstatus_t
))
5942 raw_size
= sizeof (prstat
.pr_reg
);
5943 offset
= offsetof (prstatus_t
, pr_reg
);
5944 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5946 /* Do not overwrite the core signal if it
5947 has already been set by another thread. */
5948 if (elf_tdata (abfd
)->core_signal
== 0)
5949 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5950 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5952 /* pr_who exists on:
5955 pr_who doesn't exist on:
5958 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5959 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5962 #if defined (HAVE_PRSTATUS32_T)
5963 else if (note
->descsz
== sizeof (prstatus32_t
))
5965 /* 64-bit host, 32-bit corefile */
5966 prstatus32_t prstat
;
5968 raw_size
= sizeof (prstat
.pr_reg
);
5969 offset
= offsetof (prstatus32_t
, pr_reg
);
5970 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5972 /* Do not overwrite the core signal if it
5973 has already been set by another thread. */
5974 if (elf_tdata (abfd
)->core_signal
== 0)
5975 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5976 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5978 /* pr_who exists on:
5981 pr_who doesn't exist on:
5984 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5985 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5988 #endif /* HAVE_PRSTATUS32_T */
5991 /* Fail - we don't know how to handle any other
5992 note size (ie. data object type). */
5996 /* Make a ".reg/999" section and a ".reg" section. */
5997 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5998 raw_size
, note
->descpos
+ offset
);
6000 #endif /* defined (HAVE_PRSTATUS_T) */
6002 /* Create a pseudosection containing the exact contents of NOTE. */
6004 elfcore_make_note_pseudosection (abfd
, name
, note
)
6007 Elf_Internal_Note
*note
;
6009 return _bfd_elfcore_make_pseudosection (abfd
, name
,
6010 note
->descsz
, note
->descpos
);
6013 /* There isn't a consistent prfpregset_t across platforms,
6014 but it doesn't matter, because we don't have to pick this
6015 data structure apart. */
6018 elfcore_grok_prfpreg (abfd
, note
)
6020 Elf_Internal_Note
*note
;
6022 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6025 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6026 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6030 elfcore_grok_prxfpreg (abfd
, note
)
6032 Elf_Internal_Note
*note
;
6034 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
6037 #if defined (HAVE_PRPSINFO_T)
6038 typedef prpsinfo_t elfcore_psinfo_t
;
6039 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6040 typedef prpsinfo32_t elfcore_psinfo32_t
;
6044 #if defined (HAVE_PSINFO_T)
6045 typedef psinfo_t elfcore_psinfo_t
;
6046 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6047 typedef psinfo32_t elfcore_psinfo32_t
;
6051 /* return a malloc'ed copy of a string at START which is at
6052 most MAX bytes long, possibly without a terminating '\0'.
6053 the copy will always have a terminating '\0'. */
6056 _bfd_elfcore_strndup (abfd
, start
, max
)
6062 char *end
= memchr (start
, '\0', max
);
6070 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
6074 memcpy (dups
, start
, len
);
6080 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6081 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
6084 elfcore_grok_psinfo (abfd
, note
)
6086 Elf_Internal_Note
*note
;
6088 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
6090 elfcore_psinfo_t psinfo
;
6092 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6094 elf_tdata (abfd
)->core_program
6095 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6096 sizeof (psinfo
.pr_fname
));
6098 elf_tdata (abfd
)->core_command
6099 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6100 sizeof (psinfo
.pr_psargs
));
6102 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6103 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
6105 /* 64-bit host, 32-bit corefile */
6106 elfcore_psinfo32_t psinfo
;
6108 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
6110 elf_tdata (abfd
)->core_program
6111 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
6112 sizeof (psinfo
.pr_fname
));
6114 elf_tdata (abfd
)->core_command
6115 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
6116 sizeof (psinfo
.pr_psargs
));
6122 /* Fail - we don't know how to handle any other
6123 note size (ie. data object type). */
6127 /* Note that for some reason, a spurious space is tacked
6128 onto the end of the args in some (at least one anyway)
6129 implementations, so strip it off if it exists. */
6132 char *command
= elf_tdata (abfd
)->core_command
;
6133 int n
= strlen (command
);
6135 if (0 < n
&& command
[n
- 1] == ' ')
6136 command
[n
- 1] = '\0';
6141 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6143 #if defined (HAVE_PSTATUS_T)
6145 elfcore_grok_pstatus (abfd
, note
)
6147 Elf_Internal_Note
*note
;
6149 if (note
->descsz
== sizeof (pstatus_t
)
6150 #if defined (HAVE_PXSTATUS_T)
6151 || note
->descsz
== sizeof (pxstatus_t
)
6157 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6159 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6161 #if defined (HAVE_PSTATUS32_T)
6162 else if (note
->descsz
== sizeof (pstatus32_t
))
6164 /* 64-bit host, 32-bit corefile */
6167 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
6169 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
6172 /* Could grab some more details from the "representative"
6173 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6174 NT_LWPSTATUS note, presumably. */
6178 #endif /* defined (HAVE_PSTATUS_T) */
6180 #if defined (HAVE_LWPSTATUS_T)
6182 elfcore_grok_lwpstatus (abfd
, note
)
6184 Elf_Internal_Note
*note
;
6186 lwpstatus_t lwpstat
;
6191 if (note
->descsz
!= sizeof (lwpstat
)
6192 #if defined (HAVE_LWPXSTATUS_T)
6193 && note
->descsz
!= sizeof (lwpxstatus_t
)
6198 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
6200 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
6201 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
6203 /* Make a ".reg/999" section. */
6205 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
6206 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6211 sect
= bfd_make_section (abfd
, name
);
6215 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6216 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
6217 sect
->filepos
= note
->descpos
6218 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
6221 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6222 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
6223 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
6226 sect
->flags
= SEC_HAS_CONTENTS
;
6227 sect
->alignment_power
= 2;
6229 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6232 /* Make a ".reg2/999" section */
6234 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
6235 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6240 sect
= bfd_make_section (abfd
, name
);
6244 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6245 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
6246 sect
->filepos
= note
->descpos
6247 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
6250 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6251 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
6252 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
6255 sect
->flags
= SEC_HAS_CONTENTS
;
6256 sect
->alignment_power
= 2;
6258 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
6260 #endif /* defined (HAVE_LWPSTATUS_T) */
6262 #if defined (HAVE_WIN32_PSTATUS_T)
6264 elfcore_grok_win32pstatus (abfd
, note
)
6266 Elf_Internal_Note
*note
;
6271 win32_pstatus_t pstatus
;
6273 if (note
->descsz
< sizeof (pstatus
))
6276 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
6278 switch (pstatus
.data_type
)
6280 case NOTE_INFO_PROCESS
:
6281 /* FIXME: need to add ->core_command. */
6282 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6283 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6286 case NOTE_INFO_THREAD
:
6287 /* Make a ".reg/999" section. */
6288 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6290 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6296 sect
= bfd_make_section (abfd
, name
);
6300 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6301 sect
->filepos
= (note
->descpos
6302 + offsetof (struct win32_pstatus
,
6303 data
.thread_info
.thread_context
));
6304 sect
->flags
= SEC_HAS_CONTENTS
;
6305 sect
->alignment_power
= 2;
6307 if (pstatus
.data
.thread_info
.is_active_thread
)
6308 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6312 case NOTE_INFO_MODULE
:
6313 /* Make a ".module/xxxxxxxx" section. */
6314 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6316 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6322 sect
= bfd_make_section (abfd
, name
);
6327 sect
->_raw_size
= note
->descsz
;
6328 sect
->filepos
= note
->descpos
;
6329 sect
->flags
= SEC_HAS_CONTENTS
;
6330 sect
->alignment_power
= 2;
6339 #endif /* HAVE_WIN32_PSTATUS_T */
6342 elfcore_grok_note (abfd
, note
)
6344 Elf_Internal_Note
*note
;
6346 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6354 if (bed
->elf_backend_grok_prstatus
)
6355 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6357 #if defined (HAVE_PRSTATUS_T)
6358 return elfcore_grok_prstatus (abfd
, note
);
6363 #if defined (HAVE_PSTATUS_T)
6365 return elfcore_grok_pstatus (abfd
, note
);
6368 #if defined (HAVE_LWPSTATUS_T)
6370 return elfcore_grok_lwpstatus (abfd
, note
);
6373 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6374 return elfcore_grok_prfpreg (abfd
, note
);
6376 #if defined (HAVE_WIN32_PSTATUS_T)
6377 case NT_WIN32PSTATUS
:
6378 return elfcore_grok_win32pstatus (abfd
, note
);
6381 case NT_PRXFPREG
: /* Linux SSE extension */
6382 if (note
->namesz
== 5
6383 && ! strcmp (note
->namedata
, "LINUX"))
6384 return elfcore_grok_prxfpreg (abfd
, note
);
6390 if (bed
->elf_backend_grok_psinfo
)
6391 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6393 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6394 return elfcore_grok_psinfo (abfd
, note
);
6402 elfcore_netbsd_get_lwpid (note
, lwpidp
)
6403 Elf_Internal_Note
*note
;
6408 cp
= strchr (note
->namedata
, '@');
6418 elfcore_grok_netbsd_procinfo (abfd
, note
)
6420 Elf_Internal_Note
*note
;
6423 /* Signal number at offset 0x08. */
6424 elf_tdata (abfd
)->core_signal
6425 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x08);
6427 /* Process ID at offset 0x50. */
6428 elf_tdata (abfd
)->core_pid
6429 = bfd_h_get_32 (abfd
, (bfd_byte
*) note
->descdata
+ 0x50);
6431 /* Command name at 0x7c (max 32 bytes, including nul). */
6432 elf_tdata (abfd
)->core_command
6433 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 0x7c, 31);
6439 elfcore_grok_netbsd_note (abfd
, note
)
6441 Elf_Internal_Note
*note
;
6445 if (elfcore_netbsd_get_lwpid (note
, &lwp
))
6446 elf_tdata (abfd
)->core_lwpid
= lwp
;
6448 if (note
->type
== NT_NETBSDCORE_PROCINFO
)
6450 /* NetBSD-specific core "procinfo". Note that we expect to
6451 find this note before any of the others, which is fine,
6452 since the kernel writes this note out first when it
6453 creates a core file. */
6455 return elfcore_grok_netbsd_procinfo (abfd
, note
);
6458 /* As of Jan 2002 there are no other machine-independent notes
6459 defined for NetBSD core files. If the note type is less
6460 than the start of the machine-dependent note types, we don't
6463 if (note
->type
< NT_NETBSDCORE_FIRSTMACH
)
6467 switch (bfd_get_arch (abfd
))
6469 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6470 PT_GETFPREGS == mach+2. */
6472 case bfd_arch_alpha
:
6473 case bfd_arch_sparc
:
6476 case NT_NETBSDCORE_FIRSTMACH
+0:
6477 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6479 case NT_NETBSDCORE_FIRSTMACH
+2:
6480 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6486 /* On all other arch's, PT_GETREGS == mach+1 and
6487 PT_GETFPREGS == mach+3. */
6492 case NT_NETBSDCORE_FIRSTMACH
+1:
6493 return elfcore_make_note_pseudosection (abfd
, ".reg", note
);
6495 case NT_NETBSDCORE_FIRSTMACH
+3:
6496 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
6505 /* Function: elfcore_write_note
6512 size of data for note
6515 End of buffer containing note. */
6518 elfcore_write_note (abfd
, buf
, bufsiz
, name
, type
, input
, size
)
6527 Elf_External_Note
*xnp
;
6528 int namesz
= strlen (name
);
6529 int newspace
= BFD_ALIGN (sizeof (Elf_External_Note
) + size
+ namesz
- 1, 4);
6532 p
= realloc (buf
, *bufsiz
+ newspace
);
6534 *bufsiz
+= newspace
;
6535 xnp
= (Elf_External_Note
*) dest
;
6536 H_PUT_32 (abfd
, namesz
, xnp
->namesz
);
6537 H_PUT_32 (abfd
, size
, xnp
->descsz
);
6538 H_PUT_32 (abfd
, type
, xnp
->type
);
6539 strcpy (xnp
->name
, name
);
6540 memcpy (xnp
->name
+ BFD_ALIGN (namesz
, 4), input
, size
);
6544 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6546 elfcore_write_prpsinfo (abfd
, buf
, bufsiz
, fname
, psargs
)
6554 char *note_name
= "CORE";
6556 #if defined (HAVE_PSINFO_T)
6558 note_type
= NT_PSINFO
;
6561 note_type
= NT_PRPSINFO
;
6564 memset (&data
, 0, sizeof (data
));
6565 strncpy (data
.pr_fname
, fname
, sizeof (data
.pr_fname
));
6566 strncpy (data
.pr_psargs
, psargs
, sizeof (data
.pr_psargs
));
6567 return elfcore_write_note (abfd
, buf
, bufsiz
,
6568 note_name
, note_type
, &data
, sizeof (data
));
6570 #endif /* PSINFO_T or PRPSINFO_T */
6572 #if defined (HAVE_PRSTATUS_T)
6574 elfcore_write_prstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6583 char *note_name
= "CORE";
6585 memset (&prstat
, 0, sizeof (prstat
));
6586 prstat
.pr_pid
= pid
;
6587 prstat
.pr_cursig
= cursig
;
6588 memcpy (prstat
.pr_reg
, gregs
, sizeof (prstat
.pr_reg
));
6589 return elfcore_write_note (abfd
, buf
, bufsiz
,
6590 note_name
, NT_PRSTATUS
, &prstat
, sizeof (prstat
));
6592 #endif /* HAVE_PRSTATUS_T */
6594 #if defined (HAVE_PSTATUS_T)
6596 elfcore_write_pstatus (abfd
, buf
, bufsiz
, pid
, cursig
, gregs
)
6605 char *note_name
= "CORE";
6607 memset (&pstat
, 0, sizeof (prstat
));
6609 memcpy (pstat
.pr_reg
, gregs
, sizeof (pstat
.pr_reg
));
6610 return elfcore_write_note (abfd
, buf
, bufsiz
,
6611 note_name
, NT_PSTATUS
, &pstat
, sizeof (pstat
));
6613 #endif /* HAVE_PSTATUS_T */
6616 elfcore_write_prfpreg (abfd
, buf
, bufsiz
, fpregs
, size
)
6623 char *note_name
= "CORE";
6624 return elfcore_write_note (abfd
, buf
, bufsiz
,
6625 note_name
, NT_FPREGSET
, fpregs
, size
);
6629 elfcore_write_prxfpreg (abfd
, buf
, bufsiz
, xfpregs
, size
)
6636 char *note_name
= "LINUX";
6637 return elfcore_write_note (abfd
, buf
, bufsiz
,
6638 note_name
, NT_PRXFPREG
, xfpregs
, size
);
6642 elfcore_read_notes (abfd
, offset
, size
)
6653 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6656 buf
= bfd_malloc (size
);
6660 if (bfd_bread (buf
, size
, abfd
) != size
)
6668 while (p
< buf
+ size
)
6670 /* FIXME: bad alignment assumption. */
6671 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6672 Elf_Internal_Note in
;
6674 in
.type
= H_GET_32 (abfd
, xnp
->type
);
6676 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
6677 in
.namedata
= xnp
->name
;
6679 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
6680 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
6681 in
.descpos
= offset
+ (in
.descdata
- buf
);
6683 if (strncmp (in
.namedata
, "NetBSD-CORE", 11) == 0)
6685 if (! elfcore_grok_netbsd_note (abfd
, &in
))
6690 if (! elfcore_grok_note (abfd
, &in
))
6694 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
6701 /* Providing external access to the ELF program header table. */
6703 /* Return an upper bound on the number of bytes required to store a
6704 copy of ABFD's program header table entries. Return -1 if an error
6705 occurs; bfd_get_error will return an appropriate code. */
6708 bfd_get_elf_phdr_upper_bound (abfd
)
6711 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6713 bfd_set_error (bfd_error_wrong_format
);
6717 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
6720 /* Copy ABFD's program header table entries to *PHDRS. The entries
6721 will be stored as an array of Elf_Internal_Phdr structures, as
6722 defined in include/elf/internal.h. To find out how large the
6723 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6725 Return the number of program header table entries read, or -1 if an
6726 error occurs; bfd_get_error will return an appropriate code. */
6729 bfd_get_elf_phdrs (abfd
, phdrs
)
6735 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6737 bfd_set_error (bfd_error_wrong_format
);
6741 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
6742 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
6743 num_phdrs
* sizeof (Elf_Internal_Phdr
));
6749 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
6750 bfd
*abfd ATTRIBUTE_UNUSED
;
6755 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6757 i_ehdrp
= elf_elfheader (abfd
);
6758 if (i_ehdrp
== NULL
)
6759 sprintf_vma (buf
, value
);
6762 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6764 #if BFD_HOST_64BIT_LONG
6765 sprintf (buf
, "%016lx", value
);
6767 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
6768 _bfd_int64_low (value
));
6772 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
6775 sprintf_vma (buf
, value
);
6780 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
6781 bfd
*abfd ATTRIBUTE_UNUSED
;
6786 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6788 i_ehdrp
= elf_elfheader (abfd
);
6789 if (i_ehdrp
== NULL
)
6790 fprintf_vma ((FILE *) stream
, value
);
6793 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6795 #if BFD_HOST_64BIT_LONG
6796 fprintf ((FILE *) stream
, "%016lx", value
);
6798 fprintf ((FILE *) stream
, "%08lx%08lx",
6799 _bfd_int64_high (value
), _bfd_int64_low (value
));
6803 fprintf ((FILE *) stream
, "%08lx",
6804 (unsigned long) (value
& 0xffffffff));
6807 fprintf_vma ((FILE *) stream
, value
);
6811 enum elf_reloc_type_class
6812 _bfd_elf_reloc_type_class (rela
)
6813 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
6815 return reloc_class_normal
;
6818 /* For RELA architectures, return what the relocation value for
6819 relocation against a local symbol. */
6822 _bfd_elf_rela_local_sym (abfd
, sym
, sec
, rel
)
6824 Elf_Internal_Sym
*sym
;
6826 Elf_Internal_Rela
*rel
;
6830 relocation
= (sec
->output_section
->vma
6831 + sec
->output_offset
6833 if ((sec
->flags
& SEC_MERGE
)
6834 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
6835 && elf_section_data (sec
)->sec_info_type
== ELF_INFO_TYPE_MERGE
)
6841 _bfd_merged_section_offset (abfd
, &msec
,
6842 elf_section_data (sec
)->sec_info
,
6843 sym
->st_value
+ rel
->r_addend
,
6846 rel
->r_addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
6852 _bfd_elf_rel_local_sym (abfd
, sym
, psec
, addend
)
6854 Elf_Internal_Sym
*sym
;
6858 asection
*sec
= *psec
;
6860 if (elf_section_data (sec
)->sec_info_type
!= ELF_INFO_TYPE_MERGE
)
6861 return sym
->st_value
+ addend
;
6863 return _bfd_merged_section_offset (abfd
, psec
,
6864 elf_section_data (sec
)->sec_info
,
6865 sym
->st_value
+ addend
, (bfd_vma
) 0);
6869 _bfd_elf_section_offset (abfd
, info
, sec
, offset
)
6871 struct bfd_link_info
*info
;
6875 struct bfd_elf_section_data
*sec_data
;
6877 sec_data
= elf_section_data (sec
);
6878 switch (sec_data
->sec_info_type
)
6880 case ELF_INFO_TYPE_STABS
:
6881 return _bfd_stab_section_offset
6882 (abfd
, &elf_hash_table (info
)->merge_info
, sec
, &sec_data
->sec_info
,
6884 case ELF_INFO_TYPE_EH_FRAME
:
6885 return _bfd_elf_eh_frame_section_offset (abfd
, sec
, offset
);