1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
35 /* For sparc64-cross-sparc32. */
44 static INLINE
struct elf_segment_map
*make_mapping
45 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
46 static boolean map_sections_to_segments
PARAMS ((bfd
*));
47 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
48 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
49 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
50 static boolean prep_headers
PARAMS ((bfd
*));
51 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
52 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
53 static char *elf_read
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
54 static boolean setup_group
PARAMS ((bfd
*, Elf_Internal_Shdr
*, asection
*));
55 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
56 static void set_group_contents
PARAMS ((bfd
*, asection
*, PTR
));
57 static boolean assign_section_numbers
PARAMS ((bfd
*));
58 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
59 static boolean elf_map_symbols
PARAMS ((bfd
*));
60 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
61 static boolean elfcore_read_notes
PARAMS ((bfd
*, file_ptr
, bfd_size_type
));
62 static boolean elf_find_function
PARAMS ((bfd
*, asection
*, asymbol
**,
63 bfd_vma
, const char **,
65 static int elfcore_make_pid
PARAMS ((bfd
*));
66 static boolean elfcore_maybe_make_sect
PARAMS ((bfd
*, char *, asection
*));
67 static boolean elfcore_make_note_pseudosection
PARAMS ((bfd
*, char *,
68 Elf_Internal_Note
*));
69 static boolean elfcore_grok_prfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
70 static boolean elfcore_grok_prxfpreg
PARAMS ((bfd
*, Elf_Internal_Note
*));
71 static boolean elfcore_grok_note
PARAMS ((bfd
*, Elf_Internal_Note
*));
73 /* Swap version information in and out. The version information is
74 currently size independent. If that ever changes, this code will
75 need to move into elfcode.h. */
77 /* Swap in a Verdef structure. */
80 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
82 const Elf_External_Verdef
*src
;
83 Elf_Internal_Verdef
*dst
;
85 dst
->vd_version
= H_GET_16 (abfd
, src
->vd_version
);
86 dst
->vd_flags
= H_GET_16 (abfd
, src
->vd_flags
);
87 dst
->vd_ndx
= H_GET_16 (abfd
, src
->vd_ndx
);
88 dst
->vd_cnt
= H_GET_16 (abfd
, src
->vd_cnt
);
89 dst
->vd_hash
= H_GET_32 (abfd
, src
->vd_hash
);
90 dst
->vd_aux
= H_GET_32 (abfd
, src
->vd_aux
);
91 dst
->vd_next
= H_GET_32 (abfd
, src
->vd_next
);
94 /* Swap out a Verdef structure. */
97 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
99 const Elf_Internal_Verdef
*src
;
100 Elf_External_Verdef
*dst
;
102 H_PUT_16 (abfd
, src
->vd_version
, dst
->vd_version
);
103 H_PUT_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
104 H_PUT_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
105 H_PUT_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
106 H_PUT_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
107 H_PUT_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
108 H_PUT_32 (abfd
, src
->vd_next
, dst
->vd_next
);
111 /* Swap in a Verdaux structure. */
114 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
116 const Elf_External_Verdaux
*src
;
117 Elf_Internal_Verdaux
*dst
;
119 dst
->vda_name
= H_GET_32 (abfd
, src
->vda_name
);
120 dst
->vda_next
= H_GET_32 (abfd
, src
->vda_next
);
123 /* Swap out a Verdaux structure. */
126 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
128 const Elf_Internal_Verdaux
*src
;
129 Elf_External_Verdaux
*dst
;
131 H_PUT_32 (abfd
, src
->vda_name
, dst
->vda_name
);
132 H_PUT_32 (abfd
, src
->vda_next
, dst
->vda_next
);
135 /* Swap in a Verneed structure. */
138 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
140 const Elf_External_Verneed
*src
;
141 Elf_Internal_Verneed
*dst
;
143 dst
->vn_version
= H_GET_16 (abfd
, src
->vn_version
);
144 dst
->vn_cnt
= H_GET_16 (abfd
, src
->vn_cnt
);
145 dst
->vn_file
= H_GET_32 (abfd
, src
->vn_file
);
146 dst
->vn_aux
= H_GET_32 (abfd
, src
->vn_aux
);
147 dst
->vn_next
= H_GET_32 (abfd
, src
->vn_next
);
150 /* Swap out a Verneed structure. */
153 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
155 const Elf_Internal_Verneed
*src
;
156 Elf_External_Verneed
*dst
;
158 H_PUT_16 (abfd
, src
->vn_version
, dst
->vn_version
);
159 H_PUT_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
160 H_PUT_32 (abfd
, src
->vn_file
, dst
->vn_file
);
161 H_PUT_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
162 H_PUT_32 (abfd
, src
->vn_next
, dst
->vn_next
);
165 /* Swap in a Vernaux structure. */
168 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
170 const Elf_External_Vernaux
*src
;
171 Elf_Internal_Vernaux
*dst
;
173 dst
->vna_hash
= H_GET_32 (abfd
, src
->vna_hash
);
174 dst
->vna_flags
= H_GET_16 (abfd
, src
->vna_flags
);
175 dst
->vna_other
= H_GET_16 (abfd
, src
->vna_other
);
176 dst
->vna_name
= H_GET_32 (abfd
, src
->vna_name
);
177 dst
->vna_next
= H_GET_32 (abfd
, src
->vna_next
);
180 /* Swap out a Vernaux structure. */
183 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
185 const Elf_Internal_Vernaux
*src
;
186 Elf_External_Vernaux
*dst
;
188 H_PUT_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
189 H_PUT_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
190 H_PUT_16 (abfd
, src
->vna_other
, dst
->vna_other
);
191 H_PUT_32 (abfd
, src
->vna_name
, dst
->vna_name
);
192 H_PUT_32 (abfd
, src
->vna_next
, dst
->vna_next
);
195 /* Swap in a Versym structure. */
198 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
200 const Elf_External_Versym
*src
;
201 Elf_Internal_Versym
*dst
;
203 dst
->vs_vers
= H_GET_16 (abfd
, src
->vs_vers
);
206 /* Swap out a Versym structure. */
209 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
211 const Elf_Internal_Versym
*src
;
212 Elf_External_Versym
*dst
;
214 H_PUT_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
217 /* Standard ELF hash function. Do not change this function; you will
218 cause invalid hash tables to be generated. */
221 bfd_elf_hash (namearg
)
224 const unsigned char *name
= (const unsigned char *) namearg
;
229 while ((ch
= *name
++) != '\0')
232 if ((g
= (h
& 0xf0000000)) != 0)
235 /* The ELF ABI says `h &= ~g', but this is equivalent in
236 this case and on some machines one insn instead of two. */
243 /* Read a specified number of bytes at a specified offset in an ELF
244 file, into a newly allocated buffer, and return a pointer to the
248 elf_read (abfd
, offset
, size
)
255 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
257 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
259 if (bfd_bread ((PTR
) buf
, size
, abfd
) != size
)
261 if (bfd_get_error () != bfd_error_system_call
)
262 bfd_set_error (bfd_error_file_truncated
);
269 bfd_elf_mkobject (abfd
)
272 /* This just does initialization. */
273 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
274 bfd_size_type amt
= sizeof (struct elf_obj_tdata
);
275 elf_tdata (abfd
) = (struct elf_obj_tdata
*) bfd_zalloc (abfd
, amt
);
276 if (elf_tdata (abfd
) == 0)
278 /* Since everything is done at close time, do we need any
285 bfd_elf_mkcorefile (abfd
)
288 /* I think this can be done just like an object file. */
289 return bfd_elf_mkobject (abfd
);
293 bfd_elf_get_str_section (abfd
, shindex
)
295 unsigned int shindex
;
297 Elf_Internal_Shdr
**i_shdrp
;
298 char *shstrtab
= NULL
;
300 bfd_size_type shstrtabsize
;
302 i_shdrp
= elf_elfsections (abfd
);
303 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
306 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
307 if (shstrtab
== NULL
)
309 /* No cached one, attempt to read, and cache what we read. */
310 offset
= i_shdrp
[shindex
]->sh_offset
;
311 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
312 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
313 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
319 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
321 unsigned int shindex
;
322 unsigned int strindex
;
324 Elf_Internal_Shdr
*hdr
;
329 hdr
= elf_elfsections (abfd
)[shindex
];
331 if (hdr
->contents
== NULL
332 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
335 if (strindex
>= hdr
->sh_size
)
337 (*_bfd_error_handler
)
338 (_("%s: invalid string offset %u >= %lu for section `%s'"),
339 bfd_archive_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
340 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
341 && strindex
== hdr
->sh_name
)
343 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
347 return ((char *) hdr
->contents
) + strindex
;
350 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
351 sections. The first element is the flags, the rest are section
354 typedef union elf_internal_group
{
355 Elf_Internal_Shdr
*shdr
;
357 } Elf_Internal_Group
;
359 /* Set next_in_group list pointer, and group name for NEWSECT. */
362 setup_group (abfd
, hdr
, newsect
)
364 Elf_Internal_Shdr
*hdr
;
367 unsigned int num_group
= elf_tdata (abfd
)->num_group
;
369 /* If num_group is zero, read in all SHT_GROUP sections. The count
370 is set to -1 if there are no SHT_GROUP sections. */
373 unsigned int i
, shnum
;
375 /* First count the number of groups. If we have a SHT_GROUP
376 section with just a flag word (ie. sh_size is 4), ignore it. */
377 shnum
= elf_elfheader (abfd
)->e_shnum
;
379 for (i
= 0; i
< shnum
; i
++)
381 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
382 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
388 elf_tdata (abfd
)->num_group
= num_group
;
392 /* We keep a list of elf section headers for group sections,
393 so we can find them quickly. */
394 bfd_size_type amt
= num_group
* sizeof (Elf_Internal_Shdr
*);
395 elf_tdata (abfd
)->group_sect_ptr
= bfd_alloc (abfd
, amt
);
396 if (elf_tdata (abfd
)->group_sect_ptr
== NULL
)
400 for (i
= 0; i
< shnum
; i
++)
402 Elf_Internal_Shdr
*shdr
= elf_elfsections (abfd
)[i
];
403 if (shdr
->sh_type
== SHT_GROUP
&& shdr
->sh_size
>= 8)
406 Elf_Internal_Group
*dest
;
408 /* Add to list of sections. */
409 elf_tdata (abfd
)->group_sect_ptr
[num_group
] = shdr
;
412 /* Read the raw contents. */
413 BFD_ASSERT (sizeof (*dest
) >= 4);
414 amt
= shdr
->sh_size
* sizeof (*dest
) / 4;
415 shdr
->contents
= bfd_alloc (abfd
, amt
);
416 if (shdr
->contents
== NULL
417 || bfd_seek (abfd
, shdr
->sh_offset
, SEEK_SET
) != 0
418 || (bfd_bread (shdr
->contents
, shdr
->sh_size
, abfd
)
422 /* Translate raw contents, a flag word followed by an
423 array of elf section indices all in target byte order,
424 to the flag word followed by an array of elf section
426 src
= shdr
->contents
+ shdr
->sh_size
;
427 dest
= (Elf_Internal_Group
*) (shdr
->contents
+ amt
);
434 idx
= H_GET_32 (abfd
, src
);
435 if (src
== shdr
->contents
)
442 ((*_bfd_error_handler
)
443 (_("%s: invalid SHT_GROUP entry"),
444 bfd_archive_filename (abfd
)));
447 dest
->shdr
= elf_elfsections (abfd
)[idx
];
454 if (num_group
!= (unsigned) -1)
458 for (i
= 0; i
< num_group
; i
++)
460 Elf_Internal_Shdr
*shdr
= elf_tdata (abfd
)->group_sect_ptr
[i
];
461 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) shdr
->contents
;
462 unsigned int n_elt
= shdr
->sh_size
/ 4;
464 /* Look through this group's sections to see if current
465 section is a member. */
467 if ((++idx
)->shdr
== hdr
)
471 /* We are a member of this group. Go looking through
472 other members to see if any others are linked via
474 idx
= (Elf_Internal_Group
*) shdr
->contents
;
475 n_elt
= shdr
->sh_size
/ 4;
477 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
478 && elf_section_data (s
)->next_in_group
!= NULL
)
485 /* Snarf the group name from other member, and
486 insert current section in circular list. */
487 gname
= elf_section_data (s
)->group
;
488 elf_section_data (newsect
)->group
= gname
;
489 next
= elf_section_data (s
)->next_in_group
;
490 elf_section_data (newsect
)->next_in_group
= next
;
491 elf_section_data (s
)->next_in_group
= newsect
;
495 struct elf_backend_data
*bed
;
497 unsigned char ename
[4];
501 /* Humbug. Get the name from the group signature
502 symbol. Why isn't the signature just a string?
503 Fortunately, the name index is at the same
504 place in the external symbol for both 32 and 64
506 bed
= get_elf_backend_data (abfd
);
507 pos
= elf_tdata (abfd
)->symtab_hdr
.sh_offset
;
508 pos
+= shdr
->sh_info
* bed
->s
->sizeof_sym
;
509 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
510 || bfd_bread (ename
, 4, abfd
) != 4)
512 iname
= H_GET_32 (abfd
, ename
);
513 gname
= elf_string_from_elf_strtab (abfd
, iname
);
514 elf_section_data (newsect
)->group
= gname
;
516 /* Start a circular list with one element. */
517 elf_section_data (newsect
)->next_in_group
= newsect
;
519 if (shdr
->bfd_section
!= NULL
)
520 shdr
->bfd_section
->lineno
= (alent
*) newsect
;
527 if (elf_section_data (newsect
)->group
== NULL
)
529 (*_bfd_error_handler
) (_("%s: no group info for section %s"),
530 bfd_archive_filename (abfd
), newsect
->name
);
535 /* Make a BFD section from an ELF section. We store a pointer to the
536 BFD section in the bfd_section field of the header. */
539 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
541 Elf_Internal_Shdr
*hdr
;
546 struct elf_backend_data
*bed
;
548 if (hdr
->bfd_section
!= NULL
)
550 BFD_ASSERT (strcmp (name
,
551 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
555 newsect
= bfd_make_section_anyway (abfd
, name
);
559 newsect
->filepos
= hdr
->sh_offset
;
561 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
562 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
563 || ! bfd_set_section_alignment (abfd
, newsect
,
564 bfd_log2 ((bfd_vma
) hdr
->sh_addralign
)))
567 flags
= SEC_NO_FLAGS
;
568 if (hdr
->sh_type
!= SHT_NOBITS
)
569 flags
|= SEC_HAS_CONTENTS
;
570 if (hdr
->sh_type
== SHT_GROUP
)
571 flags
|= SEC_GROUP
| SEC_EXCLUDE
;
572 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
575 if (hdr
->sh_type
!= SHT_NOBITS
)
578 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
579 flags
|= SEC_READONLY
;
580 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
582 else if ((flags
& SEC_LOAD
) != 0)
584 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
587 newsect
->entsize
= hdr
->sh_entsize
;
588 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
589 flags
|= SEC_STRINGS
;
591 if (hdr
->sh_flags
& SHF_GROUP
)
592 if (!setup_group (abfd
, hdr
, newsect
))
595 /* The debugging sections appear to be recognized only by name, not
598 static const char *debug_sec_names
[] =
607 for (i
= sizeof (debug_sec_names
) / sizeof (debug_sec_names
[0]); i
--;)
608 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
612 flags
|= SEC_DEBUGGING
;
615 /* As a GNU extension, if the name begins with .gnu.linkonce, we
616 only link a single copy of the section. This is used to support
617 g++. g++ will emit each template expansion in its own section.
618 The symbols will be defined as weak, so that multiple definitions
619 are permitted. The GNU linker extension is to actually discard
620 all but one of the sections. */
621 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
622 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
624 bed
= get_elf_backend_data (abfd
);
625 if (bed
->elf_backend_section_flags
)
626 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
629 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
632 if ((flags
& SEC_ALLOC
) != 0)
634 Elf_Internal_Phdr
*phdr
;
637 /* Look through the phdrs to see if we need to adjust the lma.
638 If all the p_paddr fields are zero, we ignore them, since
639 some ELF linkers produce such output. */
640 phdr
= elf_tdata (abfd
)->phdr
;
641 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
643 if (phdr
->p_paddr
!= 0)
646 if (i
< elf_elfheader (abfd
)->e_phnum
)
648 phdr
= elf_tdata (abfd
)->phdr
;
649 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
651 if (phdr
->p_type
== PT_LOAD
652 && phdr
->p_vaddr
!= phdr
->p_paddr
653 && phdr
->p_vaddr
<= hdr
->sh_addr
654 && (phdr
->p_vaddr
+ phdr
->p_memsz
655 >= hdr
->sh_addr
+ hdr
->sh_size
)
656 && ((flags
& SEC_LOAD
) == 0
657 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
658 && (phdr
->p_offset
+ phdr
->p_filesz
659 >= hdr
->sh_offset
+ hdr
->sh_size
))))
661 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
668 hdr
->bfd_section
= newsect
;
669 elf_section_data (newsect
)->this_hdr
= *hdr
;
679 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
682 Helper functions for GDB to locate the string tables.
683 Since BFD hides string tables from callers, GDB needs to use an
684 internal hook to find them. Sun's .stabstr, in particular,
685 isn't even pointed to by the .stab section, so ordinary
686 mechanisms wouldn't work to find it, even if we had some.
689 struct elf_internal_shdr
*
690 bfd_elf_find_section (abfd
, name
)
694 Elf_Internal_Shdr
**i_shdrp
;
699 i_shdrp
= elf_elfsections (abfd
);
702 shstrtab
= bfd_elf_get_str_section
703 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
704 if (shstrtab
!= NULL
)
706 max
= elf_elfheader (abfd
)->e_shnum
;
707 for (i
= 1; i
< max
; i
++)
708 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
715 const char *const bfd_elf_section_type_names
[] = {
716 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
717 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
718 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
721 /* ELF relocs are against symbols. If we are producing relocateable
722 output, and the reloc is against an external symbol, and nothing
723 has given us any additional addend, the resulting reloc will also
724 be against the same symbol. In such a case, we don't want to
725 change anything about the way the reloc is handled, since it will
726 all be done at final link time. Rather than put special case code
727 into bfd_perform_relocation, all the reloc types use this howto
728 function. It just short circuits the reloc if producing
729 relocateable output against an external symbol. */
731 bfd_reloc_status_type
732 bfd_elf_generic_reloc (abfd
,
739 bfd
*abfd ATTRIBUTE_UNUSED
;
740 arelent
*reloc_entry
;
742 PTR data ATTRIBUTE_UNUSED
;
743 asection
*input_section
;
745 char **error_message ATTRIBUTE_UNUSED
;
747 if (output_bfd
!= (bfd
*) NULL
748 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
749 && (! reloc_entry
->howto
->partial_inplace
750 || reloc_entry
->addend
== 0))
752 reloc_entry
->address
+= input_section
->output_offset
;
756 return bfd_reloc_continue
;
759 /* Finish SHF_MERGE section merging. */
762 _bfd_elf_merge_sections (abfd
, info
)
764 struct bfd_link_info
*info
;
766 if (!is_elf_hash_table (info
))
768 if (elf_hash_table (info
)->merge_info
)
769 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
);
773 /* Print out the program headers. */
776 _bfd_elf_print_private_bfd_data (abfd
, farg
)
780 FILE *f
= (FILE *) farg
;
781 Elf_Internal_Phdr
*p
;
783 bfd_byte
*dynbuf
= NULL
;
785 p
= elf_tdata (abfd
)->phdr
;
790 fprintf (f
, _("\nProgram Header:\n"));
791 c
= elf_elfheader (abfd
)->e_phnum
;
792 for (i
= 0; i
< c
; i
++, p
++)
799 case PT_NULL
: pt
= "NULL"; break;
800 case PT_LOAD
: pt
= "LOAD"; break;
801 case PT_DYNAMIC
: pt
= "DYNAMIC"; break;
802 case PT_INTERP
: pt
= "INTERP"; break;
803 case PT_NOTE
: pt
= "NOTE"; break;
804 case PT_SHLIB
: pt
= "SHLIB"; break;
805 case PT_PHDR
: pt
= "PHDR"; break;
806 default: sprintf (buf
, "0x%lx", p
->p_type
); pt
= buf
; break;
808 fprintf (f
, "%8s off 0x", pt
);
809 bfd_fprintf_vma (abfd
, f
, p
->p_offset
);
810 fprintf (f
, " vaddr 0x");
811 bfd_fprintf_vma (abfd
, f
, p
->p_vaddr
);
812 fprintf (f
, " paddr 0x");
813 bfd_fprintf_vma (abfd
, f
, p
->p_paddr
);
814 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
815 fprintf (f
, " filesz 0x");
816 bfd_fprintf_vma (abfd
, f
, p
->p_filesz
);
817 fprintf (f
, " memsz 0x");
818 bfd_fprintf_vma (abfd
, f
, p
->p_memsz
);
819 fprintf (f
, " flags %c%c%c",
820 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
821 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
822 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
823 if ((p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
)) != 0)
824 fprintf (f
, " %lx", p
->p_flags
&~ (unsigned) (PF_R
| PF_W
| PF_X
));
829 s
= bfd_get_section_by_name (abfd
, ".dynamic");
833 unsigned long shlink
;
834 bfd_byte
*extdyn
, *extdynend
;
836 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
838 fprintf (f
, _("\nDynamic Section:\n"));
840 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
843 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
847 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
850 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
852 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
853 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
856 extdynend
= extdyn
+ s
->_raw_size
;
857 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
859 Elf_Internal_Dyn dyn
;
864 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
866 if (dyn
.d_tag
== DT_NULL
)
873 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
877 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
878 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
879 case DT_PLTGOT
: name
= "PLTGOT"; break;
880 case DT_HASH
: name
= "HASH"; break;
881 case DT_STRTAB
: name
= "STRTAB"; break;
882 case DT_SYMTAB
: name
= "SYMTAB"; break;
883 case DT_RELA
: name
= "RELA"; break;
884 case DT_RELASZ
: name
= "RELASZ"; break;
885 case DT_RELAENT
: name
= "RELAENT"; break;
886 case DT_STRSZ
: name
= "STRSZ"; break;
887 case DT_SYMENT
: name
= "SYMENT"; break;
888 case DT_INIT
: name
= "INIT"; break;
889 case DT_FINI
: name
= "FINI"; break;
890 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
891 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
892 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
893 case DT_REL
: name
= "REL"; break;
894 case DT_RELSZ
: name
= "RELSZ"; break;
895 case DT_RELENT
: name
= "RELENT"; break;
896 case DT_PLTREL
: name
= "PLTREL"; break;
897 case DT_DEBUG
: name
= "DEBUG"; break;
898 case DT_TEXTREL
: name
= "TEXTREL"; break;
899 case DT_JMPREL
: name
= "JMPREL"; break;
900 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
901 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
902 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
903 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
904 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
905 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
906 case DT_FLAGS
: name
= "FLAGS"; break;
907 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
908 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
909 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
910 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
911 case DT_MOVEENT
: name
= "MOVEENT"; break;
912 case DT_MOVESZ
: name
= "MOVESZ"; break;
913 case DT_FEATURE
: name
= "FEATURE"; break;
914 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
915 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
916 case DT_SYMINENT
: name
= "SYMINENT"; break;
917 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
918 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
919 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
920 case DT_PLTPAD
: name
= "PLTPAD"; break;
921 case DT_MOVETAB
: name
= "MOVETAB"; break;
922 case DT_SYMINFO
: name
= "SYMINFO"; break;
923 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
924 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
925 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
926 case DT_VERSYM
: name
= "VERSYM"; break;
927 case DT_VERDEF
: name
= "VERDEF"; break;
928 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
929 case DT_VERNEED
: name
= "VERNEED"; break;
930 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
931 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
932 case DT_USED
: name
= "USED"; break;
933 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
936 fprintf (f
, " %-11s ", name
);
938 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
942 unsigned int tagv
= dyn
.d_un
.d_val
;
944 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
947 fprintf (f
, "%s", string
);
956 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
957 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
959 if (! _bfd_elf_slurp_version_tables (abfd
))
963 if (elf_dynverdef (abfd
) != 0)
965 Elf_Internal_Verdef
*t
;
967 fprintf (f
, _("\nVersion definitions:\n"));
968 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
970 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
971 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
972 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
974 Elf_Internal_Verdaux
*a
;
977 for (a
= t
->vd_auxptr
->vda_nextptr
;
980 fprintf (f
, "%s ", a
->vda_nodename
);
986 if (elf_dynverref (abfd
) != 0)
988 Elf_Internal_Verneed
*t
;
990 fprintf (f
, _("\nVersion References:\n"));
991 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
993 Elf_Internal_Vernaux
*a
;
995 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
996 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
997 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
998 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
1010 /* Display ELF-specific fields of a symbol. */
1013 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
1017 bfd_print_symbol_type how
;
1019 FILE *file
= (FILE *) filep
;
1022 case bfd_print_symbol_name
:
1023 fprintf (file
, "%s", symbol
->name
);
1025 case bfd_print_symbol_more
:
1026 fprintf (file
, "elf ");
1027 bfd_fprintf_vma (abfd
, file
, symbol
->value
);
1028 fprintf (file
, " %lx", (long) symbol
->flags
);
1030 case bfd_print_symbol_all
:
1032 const char *section_name
;
1033 const char *name
= NULL
;
1034 struct elf_backend_data
*bed
;
1035 unsigned char st_other
;
1038 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
1040 bed
= get_elf_backend_data (abfd
);
1041 if (bed
->elf_backend_print_symbol_all
)
1042 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
1046 name
= symbol
->name
;
1047 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
1050 fprintf (file
, " %s\t", section_name
);
1051 /* Print the "other" value for a symbol. For common symbols,
1052 we've already printed the size; now print the alignment.
1053 For other symbols, we have no specified alignment, and
1054 we've printed the address; now print the size. */
1055 if (bfd_is_com_section (symbol
->section
))
1056 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
;
1058 val
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
;
1059 bfd_fprintf_vma (abfd
, file
, val
);
1061 /* If we have version information, print it. */
1062 if (elf_tdata (abfd
)->dynversym_section
!= 0
1063 && (elf_tdata (abfd
)->dynverdef_section
!= 0
1064 || elf_tdata (abfd
)->dynverref_section
!= 0))
1066 unsigned int vernum
;
1067 const char *version_string
;
1069 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
1072 version_string
= "";
1073 else if (vernum
== 1)
1074 version_string
= "Base";
1075 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
1077 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
1080 Elf_Internal_Verneed
*t
;
1082 version_string
= "";
1083 for (t
= elf_tdata (abfd
)->verref
;
1087 Elf_Internal_Vernaux
*a
;
1089 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
1091 if (a
->vna_other
== vernum
)
1093 version_string
= a
->vna_nodename
;
1100 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
1101 fprintf (file
, " %-11s", version_string
);
1106 fprintf (file
, " (%s)", version_string
);
1107 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
1112 /* If the st_other field is not zero, print it. */
1113 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
1118 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
1119 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
1120 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
1122 /* Some other non-defined flags are also present, so print
1124 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
1127 fprintf (file
, " %s", name
);
1133 /* Create an entry in an ELF linker hash table. */
1135 struct bfd_hash_entry
*
1136 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
1137 struct bfd_hash_entry
*entry
;
1138 struct bfd_hash_table
*table
;
1141 /* Allocate the structure if it has not already been allocated by a
1145 entry
= bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
));
1150 /* Call the allocation method of the superclass. */
1151 entry
= _bfd_link_hash_newfunc (entry
, table
, string
);
1154 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
1155 struct elf_link_hash_table
*htab
= (struct elf_link_hash_table
*) table
;
1157 /* Set local fields. */
1161 ret
->dynstr_index
= 0;
1162 ret
->weakdef
= NULL
;
1163 ret
->got
.refcount
= htab
->init_refcount
;
1164 ret
->plt
.refcount
= htab
->init_refcount
;
1165 ret
->linker_section_pointer
= NULL
;
1166 ret
->verinfo
.verdef
= NULL
;
1167 ret
->vtable_entries_used
= NULL
;
1168 ret
->vtable_entries_size
= 0;
1169 ret
->vtable_parent
= NULL
;
1170 ret
->type
= STT_NOTYPE
;
1172 /* Assume that we have been called by a non-ELF symbol reader.
1173 This flag is then reset by the code which reads an ELF input
1174 file. This ensures that a symbol created by a non-ELF symbol
1175 reader will have the flag set correctly. */
1176 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
1182 /* Copy data from an indirect symbol to its direct symbol, hiding the
1183 old indirect symbol. Also used for copying flags to a weakdef. */
1186 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
1187 struct elf_link_hash_entry
*dir
, *ind
;
1191 /* Copy down any references that we may have already seen to the
1192 symbol which just became indirect. */
1194 dir
->elf_link_hash_flags
|=
1195 (ind
->elf_link_hash_flags
1196 & (ELF_LINK_HASH_REF_DYNAMIC
1197 | ELF_LINK_HASH_REF_REGULAR
1198 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1199 | ELF_LINK_NON_GOT_REF
));
1201 if (dir
== ind
->weakdef
)
1204 /* Copy over the global and procedure linkage table refcount entries.
1205 These may have been already set up by a check_relocs routine. */
1206 tmp
= dir
->got
.refcount
;
1209 dir
->got
.refcount
= ind
->got
.refcount
;
1210 ind
->got
.refcount
= tmp
;
1213 BFD_ASSERT (ind
->got
.refcount
<= 0);
1215 tmp
= dir
->plt
.refcount
;
1218 dir
->plt
.refcount
= ind
->plt
.refcount
;
1219 ind
->plt
.refcount
= tmp
;
1222 BFD_ASSERT (ind
->plt
.refcount
<= 0);
1224 if (dir
->dynindx
== -1)
1226 dir
->dynindx
= ind
->dynindx
;
1227 dir
->dynstr_index
= ind
->dynstr_index
;
1229 ind
->dynstr_index
= 0;
1232 BFD_ASSERT (ind
->dynindx
== -1);
1236 _bfd_elf_link_hash_hide_symbol (info
, h
)
1237 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1238 struct elf_link_hash_entry
*h
;
1240 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1241 h
->plt
.offset
= (bfd_vma
) -1;
1242 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1246 /* Initialize an ELF linker hash table. */
1249 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1250 struct elf_link_hash_table
*table
;
1252 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1253 struct bfd_hash_table
*,
1258 table
->dynamic_sections_created
= false;
1259 table
->dynobj
= NULL
;
1260 table
->init_refcount
= get_elf_backend_data (abfd
)->can_refcount
- 1;
1261 /* The first dynamic symbol is a dummy. */
1262 table
->dynsymcount
= 1;
1263 table
->dynstr
= NULL
;
1264 table
->bucketcount
= 0;
1265 table
->needed
= NULL
;
1266 table
->runpath
= NULL
;
1268 table
->stab_info
= NULL
;
1269 table
->merge_info
= NULL
;
1270 table
->dynlocal
= NULL
;
1271 ret
= _bfd_link_hash_table_init (& table
->root
, abfd
, newfunc
);
1272 table
->root
.type
= bfd_link_elf_hash_table
;
1277 /* Create an ELF linker hash table. */
1279 struct bfd_link_hash_table
*
1280 _bfd_elf_link_hash_table_create (abfd
)
1283 struct elf_link_hash_table
*ret
;
1284 bfd_size_type amt
= sizeof (struct elf_link_hash_table
);
1286 ret
= (struct elf_link_hash_table
*) bfd_alloc (abfd
, amt
);
1287 if (ret
== (struct elf_link_hash_table
*) NULL
)
1290 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1292 bfd_release (abfd
, ret
);
1299 /* This is a hook for the ELF emulation code in the generic linker to
1300 tell the backend linker what file name to use for the DT_NEEDED
1301 entry for a dynamic object. The generic linker passes name as an
1302 empty string to indicate that no DT_NEEDED entry should be made. */
1305 bfd_elf_set_dt_needed_name (abfd
, name
)
1309 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1310 && bfd_get_format (abfd
) == bfd_object
)
1311 elf_dt_name (abfd
) = name
;
1315 bfd_elf_set_dt_needed_soname (abfd
, name
)
1319 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1320 && bfd_get_format (abfd
) == bfd_object
)
1321 elf_dt_soname (abfd
) = name
;
1324 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1325 the linker ELF emulation code. */
1327 struct bfd_link_needed_list
*
1328 bfd_elf_get_needed_list (abfd
, info
)
1329 bfd
*abfd ATTRIBUTE_UNUSED
;
1330 struct bfd_link_info
*info
;
1332 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1334 return elf_hash_table (info
)->needed
;
1337 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1338 hook for the linker ELF emulation code. */
1340 struct bfd_link_needed_list
*
1341 bfd_elf_get_runpath_list (abfd
, info
)
1342 bfd
*abfd ATTRIBUTE_UNUSED
;
1343 struct bfd_link_info
*info
;
1345 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1347 return elf_hash_table (info
)->runpath
;
1350 /* Get the name actually used for a dynamic object for a link. This
1351 is the SONAME entry if there is one. Otherwise, it is the string
1352 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1355 bfd_elf_get_dt_soname (abfd
)
1358 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1359 && bfd_get_format (abfd
) == bfd_object
)
1360 return elf_dt_name (abfd
);
1364 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1365 the ELF linker emulation code. */
1368 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1370 struct bfd_link_needed_list
**pneeded
;
1373 bfd_byte
*dynbuf
= NULL
;
1375 unsigned long shlink
;
1376 bfd_byte
*extdyn
, *extdynend
;
1378 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1382 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1383 || bfd_get_format (abfd
) != bfd_object
)
1386 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1387 if (s
== NULL
|| s
->_raw_size
== 0)
1390 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1394 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1398 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1402 shlink
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1404 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1405 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1408 extdynend
= extdyn
+ s
->_raw_size
;
1409 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1411 Elf_Internal_Dyn dyn
;
1413 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1415 if (dyn
.d_tag
== DT_NULL
)
1418 if (dyn
.d_tag
== DT_NEEDED
)
1421 struct bfd_link_needed_list
*l
;
1422 unsigned int tagv
= dyn
.d_un
.d_val
;
1425 string
= bfd_elf_string_from_elf_section (abfd
, shlink
, tagv
);
1430 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, amt
);
1451 /* Allocate an ELF string table--force the first byte to be zero. */
1453 struct bfd_strtab_hash
*
1454 _bfd_elf_stringtab_init ()
1456 struct bfd_strtab_hash
*ret
;
1458 ret
= _bfd_stringtab_init ();
1463 loc
= _bfd_stringtab_add (ret
, "", true, false);
1464 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1465 if (loc
== (bfd_size_type
) -1)
1467 _bfd_stringtab_free (ret
);
1474 /* ELF .o/exec file reading */
1476 /* Create a new bfd section from an ELF section header. */
1479 bfd_section_from_shdr (abfd
, shindex
)
1481 unsigned int shindex
;
1483 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1484 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1485 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1488 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1490 switch (hdr
->sh_type
)
1493 /* Inactive section. Throw it away. */
1496 case SHT_PROGBITS
: /* Normal section with contents. */
1497 case SHT_DYNAMIC
: /* Dynamic linking information. */
1498 case SHT_NOBITS
: /* .bss section. */
1499 case SHT_HASH
: /* .hash section. */
1500 case SHT_NOTE
: /* .note section. */
1501 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1503 case SHT_SYMTAB
: /* A symbol table */
1504 if (elf_onesymtab (abfd
) == shindex
)
1507 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1508 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1509 elf_onesymtab (abfd
) = shindex
;
1510 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1511 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1512 abfd
->flags
|= HAS_SYMS
;
1514 /* Sometimes a shared object will map in the symbol table. If
1515 SHF_ALLOC is set, and this is a shared object, then we also
1516 treat this section as a BFD section. We can not base the
1517 decision purely on SHF_ALLOC, because that flag is sometimes
1518 set in a relocateable object file, which would confuse the
1520 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1521 && (abfd
->flags
& DYNAMIC
) != 0
1522 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1527 case SHT_DYNSYM
: /* A dynamic symbol table */
1528 if (elf_dynsymtab (abfd
) == shindex
)
1531 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1532 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1533 elf_dynsymtab (abfd
) = shindex
;
1534 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1535 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1536 abfd
->flags
|= HAS_SYMS
;
1538 /* Besides being a symbol table, we also treat this as a regular
1539 section, so that objcopy can handle it. */
1540 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1542 case SHT_STRTAB
: /* A string table */
1543 if (hdr
->bfd_section
!= NULL
)
1545 if (ehdr
->e_shstrndx
== shindex
)
1547 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1548 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1554 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1556 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1557 if (hdr2
->sh_link
== shindex
)
1559 if (! bfd_section_from_shdr (abfd
, i
))
1561 if (elf_onesymtab (abfd
) == i
)
1563 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1564 elf_elfsections (abfd
)[shindex
] =
1565 &elf_tdata (abfd
)->strtab_hdr
;
1568 if (elf_dynsymtab (abfd
) == i
)
1570 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1571 elf_elfsections (abfd
)[shindex
] = hdr
=
1572 &elf_tdata (abfd
)->dynstrtab_hdr
;
1573 /* We also treat this as a regular section, so
1574 that objcopy can handle it. */
1577 #if 0 /* Not handling other string tables specially right now. */
1578 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1579 /* We have a strtab for some random other section. */
1580 newsect
= (asection
*) hdr2
->bfd_section
;
1583 hdr
->bfd_section
= newsect
;
1584 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1586 elf_elfsections (abfd
)[shindex
] = hdr2
;
1592 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1596 /* *These* do a lot of work -- but build no sections! */
1598 asection
*target_sect
;
1599 Elf_Internal_Shdr
*hdr2
;
1601 /* Check for a bogus link to avoid crashing. */
1602 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1604 ((*_bfd_error_handler
)
1605 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1606 bfd_archive_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1607 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1610 /* For some incomprehensible reason Oracle distributes
1611 libraries for Solaris in which some of the objects have
1612 bogus sh_link fields. It would be nice if we could just
1613 reject them, but, unfortunately, some people need to use
1614 them. We scan through the section headers; if we find only
1615 one suitable symbol table, we clobber the sh_link to point
1616 to it. I hope this doesn't break anything. */
1617 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1618 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1624 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1626 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1627 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1638 hdr
->sh_link
= found
;
1641 /* Get the symbol table. */
1642 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1643 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1646 /* If this reloc section does not use the main symbol table we
1647 don't treat it as a reloc section. BFD can't adequately
1648 represent such a section, so at least for now, we don't
1649 try. We just present it as a normal section. We also
1650 can't use it as a reloc section if it points to the null
1652 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1653 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1655 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1657 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1658 if (target_sect
== NULL
)
1661 if ((target_sect
->flags
& SEC_RELOC
) == 0
1662 || target_sect
->reloc_count
== 0)
1663 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1667 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1668 amt
= sizeof (*hdr2
);
1669 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
1670 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1673 elf_elfsections (abfd
)[shindex
] = hdr2
;
1674 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1675 target_sect
->flags
|= SEC_RELOC
;
1676 target_sect
->relocation
= NULL
;
1677 target_sect
->rel_filepos
= hdr
->sh_offset
;
1678 /* In the section to which the relocations apply, mark whether
1679 its relocations are of the REL or RELA variety. */
1680 if (hdr
->sh_size
!= 0)
1681 elf_section_data (target_sect
)->use_rela_p
1682 = (hdr
->sh_type
== SHT_RELA
);
1683 abfd
->flags
|= HAS_RELOC
;
1688 case SHT_GNU_verdef
:
1689 elf_dynverdef (abfd
) = shindex
;
1690 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1691 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1694 case SHT_GNU_versym
:
1695 elf_dynversym (abfd
) = shindex
;
1696 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1697 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1700 case SHT_GNU_verneed
:
1701 elf_dynverref (abfd
) = shindex
;
1702 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1703 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1710 /* Make a section for objcopy and relocatable links. */
1711 if (!_bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1713 if (hdr
->contents
!= NULL
)
1715 Elf_Internal_Group
*idx
= (Elf_Internal_Group
*) hdr
->contents
;
1716 unsigned int n_elt
= hdr
->sh_size
/ 4;
1719 while (--n_elt
!= 0)
1720 if ((s
= (++idx
)->shdr
->bfd_section
) != NULL
1721 && elf_section_data (s
)->next_in_group
!= NULL
)
1723 hdr
->bfd_section
->lineno
= (alent
*) s
;
1730 /* Check for any processor-specific section types. */
1732 if (bed
->elf_backend_section_from_shdr
)
1733 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1741 /* Given an ELF section number, retrieve the corresponding BFD
1745 bfd_section_from_elf_index (abfd
, index
)
1749 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1750 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1752 return elf_elfsections (abfd
)[index
]->bfd_section
;
1756 _bfd_elf_new_section_hook (abfd
, sec
)
1760 struct bfd_elf_section_data
*sdata
;
1761 bfd_size_type amt
= sizeof (*sdata
);
1763 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, amt
);
1766 sec
->used_by_bfd
= (PTR
) sdata
;
1768 /* Indicate whether or not this section should use RELA relocations. */
1770 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1775 /* Create a new bfd section from an ELF program header.
1777 Since program segments have no names, we generate a synthetic name
1778 of the form segment<NUM>, where NUM is generally the index in the
1779 program header table. For segments that are split (see below) we
1780 generate the names segment<NUM>a and segment<NUM>b.
1782 Note that some program segments may have a file size that is different than
1783 (less than) the memory size. All this means is that at execution the
1784 system must allocate the amount of memory specified by the memory size,
1785 but only initialize it with the first "file size" bytes read from the
1786 file. This would occur for example, with program segments consisting
1787 of combined data+bss.
1789 To handle the above situation, this routine generates TWO bfd sections
1790 for the single program segment. The first has the length specified by
1791 the file size of the segment, and the second has the length specified
1792 by the difference between the two sizes. In effect, the segment is split
1793 into it's initialized and uninitialized parts.
1798 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1800 Elf_Internal_Phdr
*hdr
;
1802 const char *typename
;
1809 split
= ((hdr
->p_memsz
> 0)
1810 && (hdr
->p_filesz
> 0)
1811 && (hdr
->p_memsz
> hdr
->p_filesz
));
1812 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1813 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1816 strcpy (name
, namebuf
);
1817 newsect
= bfd_make_section (abfd
, name
);
1818 if (newsect
== NULL
)
1820 newsect
->vma
= hdr
->p_vaddr
;
1821 newsect
->lma
= hdr
->p_paddr
;
1822 newsect
->_raw_size
= hdr
->p_filesz
;
1823 newsect
->filepos
= hdr
->p_offset
;
1824 newsect
->flags
|= SEC_HAS_CONTENTS
;
1825 if (hdr
->p_type
== PT_LOAD
)
1827 newsect
->flags
|= SEC_ALLOC
;
1828 newsect
->flags
|= SEC_LOAD
;
1829 if (hdr
->p_flags
& PF_X
)
1831 /* FIXME: all we known is that it has execute PERMISSION,
1833 newsect
->flags
|= SEC_CODE
;
1836 if (!(hdr
->p_flags
& PF_W
))
1838 newsect
->flags
|= SEC_READONLY
;
1843 sprintf (namebuf
, "%s%db", typename
, index
);
1844 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (namebuf
) + 1);
1847 strcpy (name
, namebuf
);
1848 newsect
= bfd_make_section (abfd
, name
);
1849 if (newsect
== NULL
)
1851 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1852 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1853 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1854 if (hdr
->p_type
== PT_LOAD
)
1856 newsect
->flags
|= SEC_ALLOC
;
1857 if (hdr
->p_flags
& PF_X
)
1858 newsect
->flags
|= SEC_CODE
;
1860 if (!(hdr
->p_flags
& PF_W
))
1861 newsect
->flags
|= SEC_READONLY
;
1868 bfd_section_from_phdr (abfd
, hdr
, index
)
1870 Elf_Internal_Phdr
*hdr
;
1873 struct elf_backend_data
*bed
;
1875 switch (hdr
->p_type
)
1878 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1881 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1884 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1887 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1890 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1892 if (! elfcore_read_notes (abfd
, (file_ptr
) hdr
->p_offset
, hdr
->p_filesz
))
1897 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1900 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1903 /* Check for any processor-specific program segment types.
1904 If no handler for them, default to making "segment" sections. */
1905 bed
= get_elf_backend_data (abfd
);
1906 if (bed
->elf_backend_section_from_phdr
)
1907 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1909 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1913 /* Initialize REL_HDR, the section-header for new section, containing
1914 relocations against ASECT. If USE_RELA_P is true, we use RELA
1915 relocations; otherwise, we use REL relocations. */
1918 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1920 Elf_Internal_Shdr
*rel_hdr
;
1925 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1926 bfd_size_type amt
= sizeof ".rela" + strlen (asect
->name
);
1928 name
= bfd_alloc (abfd
, amt
);
1931 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1933 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1935 if (rel_hdr
->sh_name
== (unsigned int) -1)
1937 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1938 rel_hdr
->sh_entsize
= (use_rela_p
1939 ? bed
->s
->sizeof_rela
1940 : bed
->s
->sizeof_rel
);
1941 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1942 rel_hdr
->sh_flags
= 0;
1943 rel_hdr
->sh_addr
= 0;
1944 rel_hdr
->sh_size
= 0;
1945 rel_hdr
->sh_offset
= 0;
1950 /* Set up an ELF internal section header for a section. */
1953 elf_fake_sections (abfd
, asect
, failedptrarg
)
1958 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1959 boolean
*failedptr
= (boolean
*) failedptrarg
;
1960 Elf_Internal_Shdr
*this_hdr
;
1964 /* We already failed; just get out of the bfd_map_over_sections
1969 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1971 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1974 if (this_hdr
->sh_name
== (unsigned long) -1)
1980 this_hdr
->sh_flags
= 0;
1982 if ((asect
->flags
& SEC_ALLOC
) != 0
1983 || asect
->user_set_vma
)
1984 this_hdr
->sh_addr
= asect
->vma
;
1986 this_hdr
->sh_addr
= 0;
1988 this_hdr
->sh_offset
= 0;
1989 this_hdr
->sh_size
= asect
->_raw_size
;
1990 this_hdr
->sh_link
= 0;
1991 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1992 /* The sh_entsize and sh_info fields may have been set already by
1993 copy_private_section_data. */
1995 this_hdr
->bfd_section
= asect
;
1996 this_hdr
->contents
= NULL
;
1998 /* FIXME: This should not be based on section names. */
1999 if (strcmp (asect
->name
, ".dynstr") == 0)
2000 this_hdr
->sh_type
= SHT_STRTAB
;
2001 else if (strcmp (asect
->name
, ".hash") == 0)
2003 this_hdr
->sh_type
= SHT_HASH
;
2004 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
2006 else if (strcmp (asect
->name
, ".dynsym") == 0)
2008 this_hdr
->sh_type
= SHT_DYNSYM
;
2009 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
2011 else if (strcmp (asect
->name
, ".dynamic") == 0)
2013 this_hdr
->sh_type
= SHT_DYNAMIC
;
2014 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
2016 else if (strncmp (asect
->name
, ".rela", 5) == 0
2017 && get_elf_backend_data (abfd
)->may_use_rela_p
)
2019 this_hdr
->sh_type
= SHT_RELA
;
2020 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
2022 else if (strncmp (asect
->name
, ".rel", 4) == 0
2023 && get_elf_backend_data (abfd
)->may_use_rel_p
)
2025 this_hdr
->sh_type
= SHT_REL
;
2026 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
2028 else if (strncmp (asect
->name
, ".note", 5) == 0)
2029 this_hdr
->sh_type
= SHT_NOTE
;
2030 else if (strncmp (asect
->name
, ".stab", 5) == 0
2031 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
2032 this_hdr
->sh_type
= SHT_STRTAB
;
2033 else if (strcmp (asect
->name
, ".gnu.version") == 0)
2035 this_hdr
->sh_type
= SHT_GNU_versym
;
2036 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
2038 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
2040 this_hdr
->sh_type
= SHT_GNU_verdef
;
2041 this_hdr
->sh_entsize
= 0;
2042 /* objcopy or strip will copy over sh_info, but may not set
2043 cverdefs. The linker will set cverdefs, but sh_info will be
2045 if (this_hdr
->sh_info
== 0)
2046 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
2048 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
2049 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
2051 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
2053 this_hdr
->sh_type
= SHT_GNU_verneed
;
2054 this_hdr
->sh_entsize
= 0;
2055 /* objcopy or strip will copy over sh_info, but may not set
2056 cverrefs. The linker will set cverrefs, but sh_info will be
2058 if (this_hdr
->sh_info
== 0)
2059 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
2061 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
2062 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
2064 else if ((asect
->flags
& SEC_GROUP
) != 0)
2066 this_hdr
->sh_type
= SHT_GROUP
;
2067 this_hdr
->sh_entsize
= 4;
2069 else if ((asect
->flags
& SEC_ALLOC
) != 0
2070 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
2071 this_hdr
->sh_type
= SHT_NOBITS
;
2073 this_hdr
->sh_type
= SHT_PROGBITS
;
2075 if ((asect
->flags
& SEC_ALLOC
) != 0)
2076 this_hdr
->sh_flags
|= SHF_ALLOC
;
2077 if ((asect
->flags
& SEC_READONLY
) == 0)
2078 this_hdr
->sh_flags
|= SHF_WRITE
;
2079 if ((asect
->flags
& SEC_CODE
) != 0)
2080 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
2081 if ((asect
->flags
& SEC_MERGE
) != 0)
2083 this_hdr
->sh_flags
|= SHF_MERGE
;
2084 this_hdr
->sh_entsize
= asect
->entsize
;
2085 if ((asect
->flags
& SEC_STRINGS
) != 0)
2086 this_hdr
->sh_flags
|= SHF_STRINGS
;
2088 if (elf_section_data (asect
)->group
!= NULL
)
2089 this_hdr
->sh_flags
|= SHF_GROUP
;
2091 /* Check for processor-specific section types. */
2092 if (bed
->elf_backend_fake_sections
)
2093 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
2095 /* If the section has relocs, set up a section header for the
2096 SHT_REL[A] section. If two relocation sections are required for
2097 this section, it is up to the processor-specific back-end to
2098 create the other. */
2099 if ((asect
->flags
& SEC_RELOC
) != 0
2100 && !_bfd_elf_init_reloc_shdr (abfd
,
2101 &elf_section_data (asect
)->rel_hdr
,
2103 elf_section_data (asect
)->use_rela_p
))
2107 /* Fill in the contents of a SHT_GROUP section. */
2110 set_group_contents (abfd
, sec
, failedptrarg
)
2113 PTR failedptrarg ATTRIBUTE_UNUSED
;
2115 boolean
*failedptr
= (boolean
*) failedptrarg
;
2116 unsigned long symindx
;
2119 struct bfd_link_order
*l
;
2121 if (elf_section_data (sec
)->this_hdr
.sh_type
!= SHT_GROUP
2125 /* If called from the assembler, swap_out_syms will have set up
2126 udata.i; If called for "ld -r", the symbols won't yet be mapped,
2127 so emulate elf_bfd_final_link. */
2128 symindx
= sec
->symbol
->udata
.i
;
2130 symindx
= elf_section_data (sec
)->this_idx
;
2131 elf_section_data (sec
)->this_hdr
.sh_info
= symindx
;
2133 /* Nor will the contents be allocated for "ld -r". */
2134 if (sec
->contents
== NULL
)
2136 sec
->contents
= bfd_alloc (abfd
, sec
->_raw_size
);
2137 if (sec
->contents
== NULL
)
2144 loc
= sec
->contents
+ sec
->_raw_size
;
2146 /* Get the pointer to the first section in the group that we
2147 squirreled away here. */
2148 elt
= (asection
*) sec
->lineno
;
2150 /* First element is a flag word. Rest of section is elf section
2151 indices for all the sections of the group. Write them backwards
2152 just to keep the group in the same order as given in .section
2153 directives, not that it matters. */
2157 H_PUT_32 (abfd
, elf_section_data (elt
)->this_idx
, loc
);
2158 elt
= elf_section_data (elt
)->next_in_group
;
2161 /* If this is a relocatable link, then the above did nothing because
2162 SEC is the output section. Look through the input sections
2164 for (l
= sec
->link_order_head
; l
!= NULL
; l
= l
->next
)
2165 if (l
->type
== bfd_indirect_link_order
2166 && (elt
= (asection
*) l
->u
.indirect
.section
->lineno
) != NULL
)
2171 elf_section_data (elt
->output_section
)->this_idx
, loc
);
2172 elt
= elf_section_data (elt
)->next_in_group
;
2173 /* During a relocatable link, the lists are circular. */
2175 while (elt
!= (asection
*) l
->u
.indirect
.section
->lineno
);
2178 H_PUT_32 (abfd
, 0, loc
);
2180 BFD_ASSERT (loc
== sec
->contents
);
2183 /* Assign all ELF section numbers. The dummy first section is handled here
2184 too. The link/info pointers for the standard section types are filled
2185 in here too, while we're at it. */
2188 assign_section_numbers (abfd
)
2191 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
2193 unsigned int section_number
;
2194 Elf_Internal_Shdr
**i_shdrp
;
2199 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2201 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2203 d
->this_idx
= section_number
++;
2204 if ((sec
->flags
& SEC_RELOC
) == 0)
2207 d
->rel_idx
= section_number
++;
2210 d
->rel_idx2
= section_number
++;
2215 t
->shstrtab_section
= section_number
++;
2216 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
2217 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2219 if (bfd_get_symcount (abfd
) > 0)
2221 t
->symtab_section
= section_number
++;
2222 t
->strtab_section
= section_number
++;
2225 elf_elfheader (abfd
)->e_shnum
= section_number
;
2227 /* Set up the list of section header pointers, in agreement with the
2229 amt
= section_number
* sizeof (Elf_Internal_Shdr
*);
2230 i_shdrp
= (Elf_Internal_Shdr
**) bfd_alloc (abfd
, amt
);
2231 if (i_shdrp
== NULL
)
2234 amt
= sizeof (Elf_Internal_Shdr
);
2235 i_shdrp
[0] = (Elf_Internal_Shdr
*) bfd_alloc (abfd
, amt
);
2236 if (i_shdrp
[0] == NULL
)
2238 bfd_release (abfd
, i_shdrp
);
2241 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
2243 elf_elfsections (abfd
) = i_shdrp
;
2245 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
2246 if (bfd_get_symcount (abfd
) > 0)
2248 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
2249 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
2250 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
2252 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
2254 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
2258 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
2259 if (d
->rel_idx
!= 0)
2260 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
2261 if (d
->rel_idx2
!= 0)
2262 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
2264 /* Fill in the sh_link and sh_info fields while we're at it. */
2266 /* sh_link of a reloc section is the section index of the symbol
2267 table. sh_info is the section index of the section to which
2268 the relocation entries apply. */
2269 if (d
->rel_idx
!= 0)
2271 d
->rel_hdr
.sh_link
= t
->symtab_section
;
2272 d
->rel_hdr
.sh_info
= d
->this_idx
;
2274 if (d
->rel_idx2
!= 0)
2276 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
2277 d
->rel_hdr2
->sh_info
= d
->this_idx
;
2280 switch (d
->this_hdr
.sh_type
)
2284 /* A reloc section which we are treating as a normal BFD
2285 section. sh_link is the section index of the symbol
2286 table. sh_info is the section index of the section to
2287 which the relocation entries apply. We assume that an
2288 allocated reloc section uses the dynamic symbol table.
2289 FIXME: How can we be sure? */
2290 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2292 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2294 /* We look up the section the relocs apply to by name. */
2296 if (d
->this_hdr
.sh_type
== SHT_REL
)
2300 s
= bfd_get_section_by_name (abfd
, name
);
2302 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
2306 /* We assume that a section named .stab*str is a stabs
2307 string section. We look for a section with the same name
2308 but without the trailing ``str'', and set its sh_link
2309 field to point to this section. */
2310 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
2311 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
2316 len
= strlen (sec
->name
);
2317 alc
= (char *) bfd_malloc ((bfd_size_type
) len
- 2);
2320 strncpy (alc
, sec
->name
, len
- 3);
2321 alc
[len
- 3] = '\0';
2322 s
= bfd_get_section_by_name (abfd
, alc
);
2326 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2328 /* This is a .stab section. */
2329 elf_section_data (s
)->this_hdr
.sh_entsize
=
2330 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2337 case SHT_GNU_verneed
:
2338 case SHT_GNU_verdef
:
2339 /* sh_link is the section header index of the string table
2340 used for the dynamic entries, or the symbol table, or the
2342 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2344 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2348 case SHT_GNU_versym
:
2349 /* sh_link is the section header index of the symbol table
2350 this hash table or version table is for. */
2351 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2353 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2357 d
->this_hdr
.sh_link
= t
->symtab_section
;
2364 /* Map symbol from it's internal number to the external number, moving
2365 all local symbols to be at the head of the list. */
2368 sym_is_global (abfd
, sym
)
2372 /* If the backend has a special mapping, use it. */
2373 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2374 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2377 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2378 || bfd_is_und_section (bfd_get_section (sym
))
2379 || bfd_is_com_section (bfd_get_section (sym
)));
2383 elf_map_symbols (abfd
)
2386 unsigned int symcount
= bfd_get_symcount (abfd
);
2387 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2388 asymbol
**sect_syms
;
2389 unsigned int num_locals
= 0;
2390 unsigned int num_globals
= 0;
2391 unsigned int num_locals2
= 0;
2392 unsigned int num_globals2
= 0;
2394 unsigned int num_sections
= 0;
2401 fprintf (stderr
, "elf_map_symbols\n");
2405 /* Add a section symbol for each BFD section. FIXME: Is this really
2407 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2409 if (max_index
< asect
->index
)
2410 max_index
= asect
->index
;
2414 amt
= max_index
* sizeof (asymbol
*);
2415 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, amt
);
2416 if (sect_syms
== NULL
)
2418 elf_section_syms (abfd
) = sect_syms
;
2419 elf_num_section_syms (abfd
) = max_index
;
2421 for (idx
= 0; idx
< symcount
; idx
++)
2423 asymbol
*sym
= syms
[idx
];
2425 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2432 if (sec
->owner
!= NULL
)
2434 if (sec
->owner
!= abfd
)
2436 if (sec
->output_offset
!= 0)
2439 sec
= sec
->output_section
;
2441 /* Empty sections in the input files may have had a section
2442 symbol created for them. (See the comment near the end of
2443 _bfd_generic_link_output_symbols in linker.c). If the linker
2444 script discards such sections then we will reach this point.
2445 Since we know that we cannot avoid this case, we detect it
2446 and skip the abort and the assignment to the sect_syms array.
2447 To reproduce this particular case try running the linker
2448 testsuite test ld-scripts/weak.exp for an ELF port that uses
2449 the generic linker. */
2450 if (sec
->owner
== NULL
)
2453 BFD_ASSERT (sec
->owner
== abfd
);
2455 sect_syms
[sec
->index
] = syms
[idx
];
2460 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2464 if (sect_syms
[asect
->index
] != NULL
)
2467 sym
= bfd_make_empty_symbol (abfd
);
2470 sym
->the_bfd
= abfd
;
2471 sym
->name
= asect
->name
;
2473 /* Set the flags to 0 to indicate that this one was newly added. */
2475 sym
->section
= asect
;
2476 sect_syms
[asect
->index
] = sym
;
2480 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2481 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2485 /* Classify all of the symbols. */
2486 for (idx
= 0; idx
< symcount
; idx
++)
2488 if (!sym_is_global (abfd
, syms
[idx
]))
2493 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2495 if (sect_syms
[asect
->index
] != NULL
2496 && sect_syms
[asect
->index
]->flags
== 0)
2498 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2499 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2503 sect_syms
[asect
->index
]->flags
= 0;
2507 /* Now sort the symbols so the local symbols are first. */
2508 amt
= (num_locals
+ num_globals
) * sizeof (asymbol
*);
2509 new_syms
= (asymbol
**) bfd_alloc (abfd
, amt
);
2511 if (new_syms
== NULL
)
2514 for (idx
= 0; idx
< symcount
; idx
++)
2516 asymbol
*sym
= syms
[idx
];
2519 if (!sym_is_global (abfd
, sym
))
2522 i
= num_locals
+ num_globals2
++;
2524 sym
->udata
.i
= i
+ 1;
2526 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2528 if (sect_syms
[asect
->index
] != NULL
2529 && sect_syms
[asect
->index
]->flags
== 0)
2531 asymbol
*sym
= sect_syms
[asect
->index
];
2534 sym
->flags
= BSF_SECTION_SYM
;
2535 if (!sym_is_global (abfd
, sym
))
2538 i
= num_locals
+ num_globals2
++;
2540 sym
->udata
.i
= i
+ 1;
2544 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2546 elf_num_locals (abfd
) = num_locals
;
2547 elf_num_globals (abfd
) = num_globals
;
2551 /* Align to the maximum file alignment that could be required for any
2552 ELF data structure. */
2554 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2555 static INLINE file_ptr
2556 align_file_position (off
, align
)
2560 return (off
+ align
- 1) & ~(align
- 1);
2563 /* Assign a file position to a section, optionally aligning to the
2564 required section alignment. */
2567 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2568 Elf_Internal_Shdr
*i_shdrp
;
2576 al
= i_shdrp
->sh_addralign
;
2578 offset
= BFD_ALIGN (offset
, al
);
2580 i_shdrp
->sh_offset
= offset
;
2581 if (i_shdrp
->bfd_section
!= NULL
)
2582 i_shdrp
->bfd_section
->filepos
= offset
;
2583 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2584 offset
+= i_shdrp
->sh_size
;
2588 /* Compute the file positions we are going to put the sections at, and
2589 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2590 is not NULL, this is being called by the ELF backend linker. */
2593 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2595 struct bfd_link_info
*link_info
;
2597 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2599 struct bfd_strtab_hash
*strtab
;
2600 Elf_Internal_Shdr
*shstrtab_hdr
;
2602 if (abfd
->output_has_begun
)
2605 /* Do any elf backend specific processing first. */
2606 if (bed
->elf_backend_begin_write_processing
)
2607 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2609 if (! prep_headers (abfd
))
2612 /* Post process the headers if necessary. */
2613 if (bed
->elf_backend_post_process_headers
)
2614 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2617 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2621 if (!assign_section_numbers (abfd
))
2624 /* The backend linker builds symbol table information itself. */
2625 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2627 /* Non-zero if doing a relocatable link. */
2628 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2630 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2634 if (link_info
== NULL
|| link_info
->relocateable
)
2636 bfd_map_over_sections (abfd
, set_group_contents
, &failed
);
2641 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2642 /* sh_name was set in prep_headers. */
2643 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2644 shstrtab_hdr
->sh_flags
= 0;
2645 shstrtab_hdr
->sh_addr
= 0;
2646 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2647 shstrtab_hdr
->sh_entsize
= 0;
2648 shstrtab_hdr
->sh_link
= 0;
2649 shstrtab_hdr
->sh_info
= 0;
2650 /* sh_offset is set in assign_file_positions_except_relocs. */
2651 shstrtab_hdr
->sh_addralign
= 1;
2653 if (!assign_file_positions_except_relocs (abfd
))
2656 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2659 Elf_Internal_Shdr
*hdr
;
2661 off
= elf_tdata (abfd
)->next_file_pos
;
2663 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2664 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2666 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2667 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2669 elf_tdata (abfd
)->next_file_pos
= off
;
2671 /* Now that we know where the .strtab section goes, write it
2673 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2674 || ! _bfd_stringtab_emit (abfd
, strtab
))
2676 _bfd_stringtab_free (strtab
);
2679 abfd
->output_has_begun
= true;
2684 /* Create a mapping from a set of sections to a program segment. */
2686 static INLINE
struct elf_segment_map
*
2687 make_mapping (abfd
, sections
, from
, to
, phdr
)
2689 asection
**sections
;
2694 struct elf_segment_map
*m
;
2699 amt
= sizeof (struct elf_segment_map
);
2700 amt
+= (to
- from
- 1) * sizeof (asection
*);
2701 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2705 m
->p_type
= PT_LOAD
;
2706 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2707 m
->sections
[i
- from
] = *hdrpp
;
2708 m
->count
= to
- from
;
2710 if (from
== 0 && phdr
)
2712 /* Include the headers in the first PT_LOAD segment. */
2713 m
->includes_filehdr
= 1;
2714 m
->includes_phdrs
= 1;
2720 /* Set up a mapping from BFD sections to program segments. */
2723 map_sections_to_segments (abfd
)
2726 asection
**sections
= NULL
;
2730 struct elf_segment_map
*mfirst
;
2731 struct elf_segment_map
**pm
;
2732 struct elf_segment_map
*m
;
2734 unsigned int phdr_index
;
2735 bfd_vma maxpagesize
;
2737 boolean phdr_in_segment
= true;
2742 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2745 if (bfd_count_sections (abfd
) == 0)
2748 /* Select the allocated sections, and sort them. */
2750 amt
= bfd_count_sections (abfd
) * sizeof (asection
*);
2751 sections
= (asection
**) bfd_malloc (amt
);
2752 if (sections
== NULL
)
2756 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2758 if ((s
->flags
& SEC_ALLOC
) != 0)
2764 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2767 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2769 /* Build the mapping. */
2774 /* If we have a .interp section, then create a PT_PHDR segment for
2775 the program headers and a PT_INTERP segment for the .interp
2777 s
= bfd_get_section_by_name (abfd
, ".interp");
2778 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2780 amt
= sizeof (struct elf_segment_map
);
2781 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2785 m
->p_type
= PT_PHDR
;
2786 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2787 m
->p_flags
= PF_R
| PF_X
;
2788 m
->p_flags_valid
= 1;
2789 m
->includes_phdrs
= 1;
2794 amt
= sizeof (struct elf_segment_map
);
2795 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2799 m
->p_type
= PT_INTERP
;
2807 /* Look through the sections. We put sections in the same program
2808 segment when the start of the second section can be placed within
2809 a few bytes of the end of the first section. */
2812 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2814 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2816 && (dynsec
->flags
& SEC_LOAD
) == 0)
2819 /* Deal with -Ttext or something similar such that the first section
2820 is not adjacent to the program headers. This is an
2821 approximation, since at this point we don't know exactly how many
2822 program headers we will need. */
2825 bfd_size_type phdr_size
;
2827 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2829 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2830 if ((abfd
->flags
& D_PAGED
) == 0
2831 || sections
[0]->lma
< phdr_size
2832 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2833 phdr_in_segment
= false;
2836 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2839 boolean new_segment
;
2843 /* See if this section and the last one will fit in the same
2846 if (last_hdr
== NULL
)
2848 /* If we don't have a segment yet, then we don't need a new
2849 one (we build the last one after this loop). */
2850 new_segment
= false;
2852 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2854 /* If this section has a different relation between the
2855 virtual address and the load address, then we need a new
2859 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2860 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2862 /* If putting this section in this segment would force us to
2863 skip a page in the segment, then we need a new segment. */
2866 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2867 && (hdr
->flags
& SEC_LOAD
) != 0)
2869 /* We don't want to put a loadable section after a
2870 nonloadable section in the same segment. */
2873 else if ((abfd
->flags
& D_PAGED
) == 0)
2875 /* If the file is not demand paged, which means that we
2876 don't require the sections to be correctly aligned in the
2877 file, then there is no other reason for a new segment. */
2878 new_segment
= false;
2881 && (hdr
->flags
& SEC_READONLY
) == 0
2882 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2885 /* We don't want to put a writable section in a read only
2886 segment, unless they are on the same page in memory
2887 anyhow. We already know that the last section does not
2888 bring us past the current section on the page, so the
2889 only case in which the new section is not on the same
2890 page as the previous section is when the previous section
2891 ends precisely on a page boundary. */
2896 /* Otherwise, we can use the same segment. */
2897 new_segment
= false;
2902 if ((hdr
->flags
& SEC_READONLY
) == 0)
2908 /* We need a new program segment. We must create a new program
2909 header holding all the sections from phdr_index until hdr. */
2911 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2918 if ((hdr
->flags
& SEC_READONLY
) == 0)
2925 phdr_in_segment
= false;
2928 /* Create a final PT_LOAD program segment. */
2929 if (last_hdr
!= NULL
)
2931 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2939 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2942 amt
= sizeof (struct elf_segment_map
);
2943 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2947 m
->p_type
= PT_DYNAMIC
;
2949 m
->sections
[0] = dynsec
;
2955 /* For each loadable .note section, add a PT_NOTE segment. We don't
2956 use bfd_get_section_by_name, because if we link together
2957 nonloadable .note sections and loadable .note sections, we will
2958 generate two .note sections in the output file. FIXME: Using
2959 names for section types is bogus anyhow. */
2960 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2962 if ((s
->flags
& SEC_LOAD
) != 0
2963 && strncmp (s
->name
, ".note", 5) == 0)
2965 amt
= sizeof (struct elf_segment_map
);
2966 m
= (struct elf_segment_map
*) bfd_zalloc (abfd
, amt
);
2970 m
->p_type
= PT_NOTE
;
2982 elf_tdata (abfd
)->segment_map
= mfirst
;
2986 if (sections
!= NULL
)
2991 /* Sort sections by address. */
2994 elf_sort_sections (arg1
, arg2
)
2998 const asection
*sec1
= *(const asection
**) arg1
;
2999 const asection
*sec2
= *(const asection
**) arg2
;
3001 /* Sort by LMA first, since this is the address used to
3002 place the section into a segment. */
3003 if (sec1
->lma
< sec2
->lma
)
3005 else if (sec1
->lma
> sec2
->lma
)
3008 /* Then sort by VMA. Normally the LMA and the VMA will be
3009 the same, and this will do nothing. */
3010 if (sec1
->vma
< sec2
->vma
)
3012 else if (sec1
->vma
> sec2
->vma
)
3015 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3017 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3023 /* If the indicies are the same, do not return 0
3024 here, but continue to try the next comparison. */
3025 if (sec1
->target_index
- sec2
->target_index
!= 0)
3026 return sec1
->target_index
- sec2
->target_index
;
3031 else if (TOEND (sec2
))
3036 /* Sort by size, to put zero sized sections
3037 before others at the same address. */
3039 if (sec1
->_raw_size
< sec2
->_raw_size
)
3041 if (sec1
->_raw_size
> sec2
->_raw_size
)
3044 return sec1
->target_index
- sec2
->target_index
;
3047 /* Assign file positions to the sections based on the mapping from
3048 sections to segments. This function also sets up some fields in
3049 the file header, and writes out the program headers. */
3052 assign_file_positions_for_segments (abfd
)
3055 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3057 struct elf_segment_map
*m
;
3059 Elf_Internal_Phdr
*phdrs
;
3061 bfd_vma filehdr_vaddr
, filehdr_paddr
;
3062 bfd_vma phdrs_vaddr
, phdrs_paddr
;
3063 Elf_Internal_Phdr
*p
;
3066 if (elf_tdata (abfd
)->segment_map
== NULL
)
3068 if (! map_sections_to_segments (abfd
))
3072 if (bed
->elf_backend_modify_segment_map
)
3074 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
3079 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3082 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
3083 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
3084 elf_elfheader (abfd
)->e_phnum
= count
;
3089 /* If we already counted the number of program segments, make sure
3090 that we allocated enough space. This happens when SIZEOF_HEADERS
3091 is used in a linker script. */
3092 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
3093 if (alloc
!= 0 && count
> alloc
)
3095 ((*_bfd_error_handler
)
3096 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3097 bfd_get_filename (abfd
), alloc
, count
));
3098 bfd_set_error (bfd_error_bad_value
);
3105 amt
= alloc
* sizeof (Elf_Internal_Phdr
);
3106 phdrs
= (Elf_Internal_Phdr
*) bfd_alloc (abfd
, amt
);
3110 off
= bed
->s
->sizeof_ehdr
;
3111 off
+= alloc
* bed
->s
->sizeof_phdr
;
3118 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3125 /* If elf_segment_map is not from map_sections_to_segments, the
3126 sections may not be correctly ordered. */
3128 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
3131 p
->p_type
= m
->p_type
;
3132 p
->p_flags
= m
->p_flags
;
3134 if (p
->p_type
== PT_LOAD
3136 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
3138 if ((abfd
->flags
& D_PAGED
) != 0)
3139 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
3142 bfd_size_type align
;
3145 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3147 bfd_size_type secalign
;
3149 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
3150 if (secalign
> align
)
3154 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
3161 p
->p_vaddr
= m
->sections
[0]->vma
;
3163 if (m
->p_paddr_valid
)
3164 p
->p_paddr
= m
->p_paddr
;
3165 else if (m
->count
== 0)
3168 p
->p_paddr
= m
->sections
[0]->lma
;
3170 if (p
->p_type
== PT_LOAD
3171 && (abfd
->flags
& D_PAGED
) != 0)
3172 p
->p_align
= bed
->maxpagesize
;
3173 else if (m
->count
== 0)
3174 p
->p_align
= bed
->s
->file_align
;
3182 if (m
->includes_filehdr
)
3184 if (! m
->p_flags_valid
)
3187 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
3188 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
3191 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3193 if (p
->p_vaddr
< (bfd_vma
) off
)
3195 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3196 bfd_get_filename (abfd
));
3197 bfd_set_error (bfd_error_bad_value
);
3202 if (! m
->p_paddr_valid
)
3205 if (p
->p_type
== PT_LOAD
)
3207 filehdr_vaddr
= p
->p_vaddr
;
3208 filehdr_paddr
= p
->p_paddr
;
3212 if (m
->includes_phdrs
)
3214 if (! m
->p_flags_valid
)
3217 if (m
->includes_filehdr
)
3219 if (p
->p_type
== PT_LOAD
)
3221 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
3222 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
3227 p
->p_offset
= bed
->s
->sizeof_ehdr
;
3231 BFD_ASSERT (p
->p_type
== PT_LOAD
);
3232 p
->p_vaddr
-= off
- p
->p_offset
;
3233 if (! m
->p_paddr_valid
)
3234 p
->p_paddr
-= off
- p
->p_offset
;
3237 if (p
->p_type
== PT_LOAD
)
3239 phdrs_vaddr
= p
->p_vaddr
;
3240 phdrs_paddr
= p
->p_paddr
;
3243 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
3246 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
3247 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
3250 if (p
->p_type
== PT_LOAD
3251 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
3253 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
3259 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
3260 p
->p_filesz
+= adjust
;
3261 p
->p_memsz
+= adjust
;
3267 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
3271 bfd_size_type align
;
3275 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
3277 /* The section may have artificial alignment forced by a
3278 link script. Notice this case by the gap between the
3279 cumulative phdr vma and the section's vma. */
3280 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
3282 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
3284 p
->p_memsz
+= adjust
;
3287 if ((flags
& SEC_LOAD
) != 0)
3288 p
->p_filesz
+= adjust
;
3291 if (p
->p_type
== PT_LOAD
)
3293 bfd_signed_vma adjust
;
3295 if ((flags
& SEC_LOAD
) != 0)
3297 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
3301 else if ((flags
& SEC_ALLOC
) != 0)
3303 /* The section VMA must equal the file position
3304 modulo the page size. FIXME: I'm not sure if
3305 this adjustment is really necessary. We used to
3306 not have the SEC_LOAD case just above, and then
3307 this was necessary, but now I'm not sure. */
3308 if ((abfd
->flags
& D_PAGED
) != 0)
3309 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
3311 adjust
= (sec
->vma
- voff
) % align
;
3320 (* _bfd_error_handler
)
3321 (_("Error: First section in segment (%s) starts at 0x%x"),
3322 bfd_section_name (abfd
, sec
), sec
->lma
);
3323 (* _bfd_error_handler
)
3324 (_(" whereas segment starts at 0x%x"),
3329 p
->p_memsz
+= adjust
;
3332 if ((flags
& SEC_LOAD
) != 0)
3333 p
->p_filesz
+= adjust
;
3338 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3339 used in a linker script we may have a section with
3340 SEC_LOAD clear but which is supposed to have
3342 if ((flags
& SEC_LOAD
) != 0
3343 || (flags
& SEC_HAS_CONTENTS
) != 0)
3344 off
+= sec
->_raw_size
;
3346 if ((flags
& SEC_ALLOC
) != 0)
3347 voff
+= sec
->_raw_size
;
3350 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3352 /* The actual "note" segment has i == 0.
3353 This is the one that actually contains everything. */
3357 p
->p_filesz
= sec
->_raw_size
;
3358 off
+= sec
->_raw_size
;
3363 /* Fake sections -- don't need to be written. */
3366 flags
= sec
->flags
= 0;
3373 p
->p_memsz
+= sec
->_raw_size
;
3375 if ((flags
& SEC_LOAD
) != 0)
3376 p
->p_filesz
+= sec
->_raw_size
;
3378 if (align
> p
->p_align
3379 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3383 if (! m
->p_flags_valid
)
3386 if ((flags
& SEC_CODE
) != 0)
3388 if ((flags
& SEC_READONLY
) == 0)
3394 /* Now that we have set the section file positions, we can set up
3395 the file positions for the non PT_LOAD segments. */
3396 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3400 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3402 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3403 p
->p_offset
= m
->sections
[0]->filepos
;
3407 if (m
->includes_filehdr
)
3409 p
->p_vaddr
= filehdr_vaddr
;
3410 if (! m
->p_paddr_valid
)
3411 p
->p_paddr
= filehdr_paddr
;
3413 else if (m
->includes_phdrs
)
3415 p
->p_vaddr
= phdrs_vaddr
;
3416 if (! m
->p_paddr_valid
)
3417 p
->p_paddr
= phdrs_paddr
;
3422 /* Clear out any program headers we allocated but did not use. */
3423 for (; count
< alloc
; count
++, p
++)
3425 memset (p
, 0, sizeof *p
);
3426 p
->p_type
= PT_NULL
;
3429 elf_tdata (abfd
)->phdr
= phdrs
;
3431 elf_tdata (abfd
)->next_file_pos
= off
;
3433 /* Write out the program headers. */
3434 if (bfd_seek (abfd
, (bfd_signed_vma
) bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3435 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3441 /* Get the size of the program header.
3443 If this is called by the linker before any of the section VMA's are set, it
3444 can't calculate the correct value for a strange memory layout. This only
3445 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3446 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3447 data segment (exclusive of .interp and .dynamic).
3449 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3450 will be two segments. */
3452 static bfd_size_type
3453 get_program_header_size (abfd
)
3458 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3460 /* We can't return a different result each time we're called. */
3461 if (elf_tdata (abfd
)->program_header_size
!= 0)
3462 return elf_tdata (abfd
)->program_header_size
;
3464 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3466 struct elf_segment_map
*m
;
3469 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3471 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3472 return elf_tdata (abfd
)->program_header_size
;
3475 /* Assume we will need exactly two PT_LOAD segments: one for text
3476 and one for data. */
3479 s
= bfd_get_section_by_name (abfd
, ".interp");
3480 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3482 /* If we have a loadable interpreter section, we need a
3483 PT_INTERP segment. In this case, assume we also need a
3484 PT_PHDR segment, although that may not be true for all
3489 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3491 /* We need a PT_DYNAMIC segment. */
3495 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3497 if ((s
->flags
& SEC_LOAD
) != 0
3498 && strncmp (s
->name
, ".note", 5) == 0)
3500 /* We need a PT_NOTE segment. */
3505 /* Let the backend count up any program headers it might need. */
3506 if (bed
->elf_backend_additional_program_headers
)
3510 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3516 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3517 return elf_tdata (abfd
)->program_header_size
;
3520 /* Work out the file positions of all the sections. This is called by
3521 _bfd_elf_compute_section_file_positions. All the section sizes and
3522 VMAs must be known before this is called.
3524 We do not consider reloc sections at this point, unless they form
3525 part of the loadable image. Reloc sections are assigned file
3526 positions in assign_file_positions_for_relocs, which is called by
3527 write_object_contents and final_link.
3529 We also don't set the positions of the .symtab and .strtab here. */
3532 assign_file_positions_except_relocs (abfd
)
3535 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3536 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3537 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3539 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3541 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3542 && bfd_get_format (abfd
) != bfd_core
)
3544 Elf_Internal_Shdr
**hdrpp
;
3547 /* Start after the ELF header. */
3548 off
= i_ehdrp
->e_ehsize
;
3550 /* We are not creating an executable, which means that we are
3551 not creating a program header, and that the actual order of
3552 the sections in the file is unimportant. */
3553 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3555 Elf_Internal_Shdr
*hdr
;
3558 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3560 hdr
->sh_offset
= -1;
3563 if (i
== tdata
->symtab_section
3564 || i
== tdata
->strtab_section
)
3566 hdr
->sh_offset
= -1;
3570 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3576 Elf_Internal_Shdr
**hdrpp
;
3578 /* Assign file positions for the loaded sections based on the
3579 assignment of sections to segments. */
3580 if (! assign_file_positions_for_segments (abfd
))
3583 /* Assign file positions for the other sections. */
3585 off
= elf_tdata (abfd
)->next_file_pos
;
3586 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3588 Elf_Internal_Shdr
*hdr
;
3591 if (hdr
->bfd_section
!= NULL
3592 && hdr
->bfd_section
->filepos
!= 0)
3593 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3594 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3596 ((*_bfd_error_handler
)
3597 (_("%s: warning: allocated section `%s' not in segment"),
3598 bfd_get_filename (abfd
),
3599 (hdr
->bfd_section
== NULL
3601 : hdr
->bfd_section
->name
)));
3602 if ((abfd
->flags
& D_PAGED
) != 0)
3603 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3605 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3606 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3609 else if (hdr
->sh_type
== SHT_REL
3610 || hdr
->sh_type
== SHT_RELA
3611 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3612 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3613 hdr
->sh_offset
= -1;
3615 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3619 /* Place the section headers. */
3620 off
= align_file_position (off
, bed
->s
->file_align
);
3621 i_ehdrp
->e_shoff
= off
;
3622 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3624 elf_tdata (abfd
)->next_file_pos
= off
;
3633 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3634 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3635 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3637 struct bfd_strtab_hash
*shstrtab
;
3638 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3640 i_ehdrp
= elf_elfheader (abfd
);
3641 i_shdrp
= elf_elfsections (abfd
);
3643 shstrtab
= _bfd_elf_stringtab_init ();
3644 if (shstrtab
== NULL
)
3647 elf_shstrtab (abfd
) = shstrtab
;
3649 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3650 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3651 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3652 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3654 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3655 i_ehdrp
->e_ident
[EI_DATA
] =
3656 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3657 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3659 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3660 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3662 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3663 i_ehdrp
->e_ident
[count
] = 0;
3665 if ((abfd
->flags
& DYNAMIC
) != 0)
3666 i_ehdrp
->e_type
= ET_DYN
;
3667 else if ((abfd
->flags
& EXEC_P
) != 0)
3668 i_ehdrp
->e_type
= ET_EXEC
;
3669 else if (bfd_get_format (abfd
) == bfd_core
)
3670 i_ehdrp
->e_type
= ET_CORE
;
3672 i_ehdrp
->e_type
= ET_REL
;
3674 switch (bfd_get_arch (abfd
))
3676 case bfd_arch_unknown
:
3677 i_ehdrp
->e_machine
= EM_NONE
;
3680 /* There used to be a long list of cases here, each one setting
3681 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3682 in the corresponding bfd definition. To avoid duplication,
3683 the switch was removed. Machines that need special handling
3684 can generally do it in elf_backend_final_write_processing(),
3685 unless they need the information earlier than the final write.
3686 Such need can generally be supplied by replacing the tests for
3687 e_machine with the conditions used to determine it. */
3689 if (get_elf_backend_data (abfd
) != NULL
)
3690 i_ehdrp
->e_machine
= get_elf_backend_data (abfd
)->elf_machine_code
;
3692 i_ehdrp
->e_machine
= EM_NONE
;
3695 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3696 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3698 /* No program header, for now. */
3699 i_ehdrp
->e_phoff
= 0;
3700 i_ehdrp
->e_phentsize
= 0;
3701 i_ehdrp
->e_phnum
= 0;
3703 /* Each bfd section is section header entry. */
3704 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3705 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3707 /* If we're building an executable, we'll need a program header table. */
3708 if (abfd
->flags
& EXEC_P
)
3710 /* It all happens later. */
3712 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3714 /* elf_build_phdrs() returns a (NULL-terminated) array of
3715 Elf_Internal_Phdrs. */
3716 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3717 i_ehdrp
->e_phoff
= outbase
;
3718 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3723 i_ehdrp
->e_phentsize
= 0;
3725 i_ehdrp
->e_phoff
= 0;
3728 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3729 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3730 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3731 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3732 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3733 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3734 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3735 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3736 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3742 /* Assign file positions for all the reloc sections which are not part
3743 of the loadable file image. */
3746 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3751 Elf_Internal_Shdr
**shdrpp
;
3753 off
= elf_tdata (abfd
)->next_file_pos
;
3755 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3756 i
< elf_elfheader (abfd
)->e_shnum
;
3759 Elf_Internal_Shdr
*shdrp
;
3762 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3763 && shdrp
->sh_offset
== -1)
3764 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3767 elf_tdata (abfd
)->next_file_pos
= off
;
3771 _bfd_elf_write_object_contents (abfd
)
3774 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3775 Elf_Internal_Ehdr
*i_ehdrp
;
3776 Elf_Internal_Shdr
**i_shdrp
;
3780 if (! abfd
->output_has_begun
3781 && ! _bfd_elf_compute_section_file_positions
3782 (abfd
, (struct bfd_link_info
*) NULL
))
3785 i_shdrp
= elf_elfsections (abfd
);
3786 i_ehdrp
= elf_elfheader (abfd
);
3789 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3793 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3795 /* After writing the headers, we need to write the sections too... */
3796 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3798 if (bed
->elf_backend_section_processing
)
3799 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3800 if (i_shdrp
[count
]->contents
)
3802 bfd_size_type amt
= i_shdrp
[count
]->sh_size
;
3804 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3805 || bfd_bwrite (i_shdrp
[count
]->contents
, amt
, abfd
) != amt
)
3810 /* Write out the section header names. */
3811 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3812 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3815 if (bed
->elf_backend_final_write_processing
)
3816 (*bed
->elf_backend_final_write_processing
) (abfd
,
3817 elf_tdata (abfd
)->linker
);
3819 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3823 _bfd_elf_write_corefile_contents (abfd
)
3826 /* Hopefully this can be done just like an object file. */
3827 return _bfd_elf_write_object_contents (abfd
);
3830 /* Given a section, search the header to find them. */
3833 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3837 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3838 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3840 Elf_Internal_Shdr
*hdr
;
3841 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3843 for (index
= 0; index
< maxindex
; index
++)
3845 hdr
= i_shdrp
[index
];
3846 if (hdr
->bfd_section
== asect
)
3850 if (bed
->elf_backend_section_from_bfd_section
)
3852 for (index
= 0; index
< maxindex
; index
++)
3856 hdr
= i_shdrp
[index
];
3858 if ((*bed
->elf_backend_section_from_bfd_section
)
3859 (abfd
, hdr
, asect
, &retval
))
3864 if (bfd_is_abs_section (asect
))
3866 if (bfd_is_com_section (asect
))
3868 if (bfd_is_und_section (asect
))
3871 bfd_set_error (bfd_error_nonrepresentable_section
);
3876 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3880 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3882 asymbol
**asym_ptr_ptr
;
3884 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3886 flagword flags
= asym_ptr
->flags
;
3888 /* When gas creates relocations against local labels, it creates its
3889 own symbol for the section, but does put the symbol into the
3890 symbol chain, so udata is 0. When the linker is generating
3891 relocatable output, this section symbol may be for one of the
3892 input sections rather than the output section. */
3893 if (asym_ptr
->udata
.i
== 0
3894 && (flags
& BSF_SECTION_SYM
)
3895 && asym_ptr
->section
)
3899 if (asym_ptr
->section
->output_section
!= NULL
)
3900 indx
= asym_ptr
->section
->output_section
->index
;
3902 indx
= asym_ptr
->section
->index
;
3903 if (indx
< elf_num_section_syms (abfd
)
3904 && elf_section_syms (abfd
)[indx
] != NULL
)
3905 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3908 idx
= asym_ptr
->udata
.i
;
3912 /* This case can occur when using --strip-symbol on a symbol
3913 which is used in a relocation entry. */
3914 (*_bfd_error_handler
)
3915 (_("%s: symbol `%s' required but not present"),
3916 bfd_archive_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3917 bfd_set_error (bfd_error_no_symbols
);
3924 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3925 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3926 elf_symbol_flags (flags
));
3934 /* Copy private BFD data. This copies any program header information. */
3937 copy_private_bfd_data (ibfd
, obfd
)
3941 Elf_Internal_Ehdr
* iehdr
;
3942 struct elf_segment_map
* map
;
3943 struct elf_segment_map
* map_first
;
3944 struct elf_segment_map
** pointer_to_map
;
3945 Elf_Internal_Phdr
* segment
;
3948 unsigned int num_segments
;
3949 boolean phdr_included
= false;
3950 bfd_vma maxpagesize
;
3951 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3952 unsigned int phdr_adjust_num
= 0;
3954 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3955 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3958 if (elf_tdata (ibfd
)->phdr
== NULL
)
3961 iehdr
= elf_elfheader (ibfd
);
3964 pointer_to_map
= &map_first
;
3966 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3967 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3969 /* Returns the end address of the segment + 1. */
3970 #define SEGMENT_END(segment, start) \
3971 (start + (segment->p_memsz > segment->p_filesz \
3972 ? segment->p_memsz : segment->p_filesz))
3974 /* Returns true if the given section is contained within
3975 the given segment. VMA addresses are compared. */
3976 #define IS_CONTAINED_BY_VMA(section, segment) \
3977 (section->vma >= segment->p_vaddr \
3978 && (section->vma + section->_raw_size) \
3979 <= (SEGMENT_END (segment, segment->p_vaddr)))
3981 /* Returns true if the given section is contained within
3982 the given segment. LMA addresses are compared. */
3983 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3984 (section->lma >= base \
3985 && (section->lma + section->_raw_size) \
3986 <= SEGMENT_END (segment, base))
3988 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3989 #define IS_COREFILE_NOTE(p, s) \
3990 (p->p_type == PT_NOTE \
3991 && bfd_get_format (ibfd) == bfd_core \
3992 && s->vma == 0 && s->lma == 0 \
3993 && (bfd_vma) s->filepos >= p->p_offset \
3994 && (bfd_vma) s->filepos + s->_raw_size \
3995 <= p->p_offset + p->p_filesz)
3997 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3998 linker, which generates a PT_INTERP section with p_vaddr and
3999 p_memsz set to 0. */
4000 #define IS_SOLARIS_PT_INTERP(p, s) \
4002 && p->p_filesz > 0 \
4003 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4004 && s->_raw_size > 0 \
4005 && (bfd_vma) s->filepos >= p->p_offset \
4006 && ((bfd_vma) s->filepos + s->_raw_size \
4007 <= p->p_offset + p->p_filesz))
4009 /* Decide if the given section should be included in the given segment.
4010 A section will be included if:
4011 1. It is within the address space of the segment,
4012 2. It is an allocated segment,
4013 3. There is an output section associated with it,
4014 4. The section has not already been allocated to a previous segment. */
4015 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4016 ((((IS_CONTAINED_BY_VMA (section, segment) \
4017 || IS_SOLARIS_PT_INTERP (segment, section)) \
4018 && (section->flags & SEC_ALLOC) != 0) \
4019 || IS_COREFILE_NOTE (segment, section)) \
4020 && section->output_section != NULL \
4021 && section->segment_mark == false)
4023 /* Returns true iff seg1 starts after the end of seg2. */
4024 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4025 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4027 /* Returns true iff seg1 and seg2 overlap. */
4028 #define SEGMENT_OVERLAPS(seg1, seg2) \
4029 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4031 /* Initialise the segment mark field. */
4032 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4033 section
->segment_mark
= false;
4035 /* Scan through the segments specified in the program header
4036 of the input BFD. For this first scan we look for overlaps
4037 in the loadable segments. These can be created by wierd
4038 parameters to objcopy. */
4039 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4044 Elf_Internal_Phdr
*segment2
;
4046 if (segment
->p_type
!= PT_LOAD
)
4049 /* Determine if this segment overlaps any previous segments. */
4050 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
4052 bfd_signed_vma extra_length
;
4054 if (segment2
->p_type
!= PT_LOAD
4055 || ! SEGMENT_OVERLAPS (segment
, segment2
))
4058 /* Merge the two segments together. */
4059 if (segment2
->p_vaddr
< segment
->p_vaddr
)
4061 /* Extend SEGMENT2 to include SEGMENT and then delete
4064 SEGMENT_END (segment
, segment
->p_vaddr
)
4065 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
4067 if (extra_length
> 0)
4069 segment2
->p_memsz
+= extra_length
;
4070 segment2
->p_filesz
+= extra_length
;
4073 segment
->p_type
= PT_NULL
;
4075 /* Since we have deleted P we must restart the outer loop. */
4077 segment
= elf_tdata (ibfd
)->phdr
;
4082 /* Extend SEGMENT to include SEGMENT2 and then delete
4085 SEGMENT_END (segment2
, segment2
->p_vaddr
)
4086 - SEGMENT_END (segment
, segment
->p_vaddr
);
4088 if (extra_length
> 0)
4090 segment
->p_memsz
+= extra_length
;
4091 segment
->p_filesz
+= extra_length
;
4094 segment2
->p_type
= PT_NULL
;
4099 /* The second scan attempts to assign sections to segments. */
4100 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
4104 unsigned int section_count
;
4105 asection
** sections
;
4106 asection
* output_section
;
4108 bfd_vma matching_lma
;
4109 bfd_vma suggested_lma
;
4113 if (segment
->p_type
== PT_NULL
)
4116 /* Compute how many sections might be placed into this segment. */
4118 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
4119 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4122 /* Allocate a segment map big enough to contain all of the
4123 sections we have selected. */
4124 amt
= sizeof (struct elf_segment_map
);
4125 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4126 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4130 /* Initialise the fields of the segment map. Default to
4131 using the physical address of the segment in the input BFD. */
4133 map
->p_type
= segment
->p_type
;
4134 map
->p_flags
= segment
->p_flags
;
4135 map
->p_flags_valid
= 1;
4136 map
->p_paddr
= segment
->p_paddr
;
4137 map
->p_paddr_valid
= 1;
4139 /* Determine if this segment contains the ELF file header
4140 and if it contains the program headers themselves. */
4141 map
->includes_filehdr
= (segment
->p_offset
== 0
4142 && segment
->p_filesz
>= iehdr
->e_ehsize
);
4144 map
->includes_phdrs
= 0;
4146 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
4148 map
->includes_phdrs
=
4149 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
4150 && (segment
->p_offset
+ segment
->p_filesz
4151 >= ((bfd_vma
) iehdr
->e_phoff
4152 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
4154 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
4155 phdr_included
= true;
4158 if (section_count
== 0)
4160 /* Special segments, such as the PT_PHDR segment, may contain
4161 no sections, but ordinary, loadable segments should contain
4163 if (segment
->p_type
== PT_LOAD
)
4165 (_("%s: warning: Empty loadable segment detected\n"),
4166 bfd_archive_filename (ibfd
));
4169 *pointer_to_map
= map
;
4170 pointer_to_map
= &map
->next
;
4175 /* Now scan the sections in the input BFD again and attempt
4176 to add their corresponding output sections to the segment map.
4177 The problem here is how to handle an output section which has
4178 been moved (ie had its LMA changed). There are four possibilities:
4180 1. None of the sections have been moved.
4181 In this case we can continue to use the segment LMA from the
4184 2. All of the sections have been moved by the same amount.
4185 In this case we can change the segment's LMA to match the LMA
4186 of the first section.
4188 3. Some of the sections have been moved, others have not.
4189 In this case those sections which have not been moved can be
4190 placed in the current segment which will have to have its size,
4191 and possibly its LMA changed, and a new segment or segments will
4192 have to be created to contain the other sections.
4194 4. The sections have been moved, but not be the same amount.
4195 In this case we can change the segment's LMA to match the LMA
4196 of the first section and we will have to create a new segment
4197 or segments to contain the other sections.
4199 In order to save time, we allocate an array to hold the section
4200 pointers that we are interested in. As these sections get assigned
4201 to a segment, they are removed from this array. */
4203 amt
= (bfd_size_type
) section_count
* sizeof (asection
*);
4204 sections
= (asection
**) bfd_malloc (amt
);
4205 if (sections
== NULL
)
4208 /* Step One: Scan for segment vs section LMA conflicts.
4209 Also add the sections to the section array allocated above.
4210 Also add the sections to the current segment. In the common
4211 case, where the sections have not been moved, this means that
4212 we have completely filled the segment, and there is nothing
4218 for (j
= 0, section
= ibfd
->sections
;
4220 section
= section
->next
)
4222 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
4224 output_section
= section
->output_section
;
4226 sections
[j
++] = section
;
4228 /* The Solaris native linker always sets p_paddr to 0.
4229 We try to catch that case here, and set it to the
4231 if (segment
->p_paddr
== 0
4232 && segment
->p_vaddr
!= 0
4234 && output_section
->lma
!= 0
4235 && (output_section
->vma
== (segment
->p_vaddr
4236 + (map
->includes_filehdr
4239 + (map
->includes_phdrs
4240 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4242 map
->p_paddr
= segment
->p_vaddr
;
4244 /* Match up the physical address of the segment with the
4245 LMA address of the output section. */
4246 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4247 || IS_COREFILE_NOTE (segment
, section
))
4249 if (matching_lma
== 0)
4250 matching_lma
= output_section
->lma
;
4252 /* We assume that if the section fits within the segment
4253 then it does not overlap any other section within that
4255 map
->sections
[isec
++] = output_section
;
4257 else if (suggested_lma
== 0)
4258 suggested_lma
= output_section
->lma
;
4262 BFD_ASSERT (j
== section_count
);
4264 /* Step Two: Adjust the physical address of the current segment,
4266 if (isec
== section_count
)
4268 /* All of the sections fitted within the segment as currently
4269 specified. This is the default case. Add the segment to
4270 the list of built segments and carry on to process the next
4271 program header in the input BFD. */
4272 map
->count
= section_count
;
4273 *pointer_to_map
= map
;
4274 pointer_to_map
= &map
->next
;
4281 if (matching_lma
!= 0)
4283 /* At least one section fits inside the current segment.
4284 Keep it, but modify its physical address to match the
4285 LMA of the first section that fitted. */
4286 map
->p_paddr
= matching_lma
;
4290 /* None of the sections fitted inside the current segment.
4291 Change the current segment's physical address to match
4292 the LMA of the first section. */
4293 map
->p_paddr
= suggested_lma
;
4296 /* Offset the segment physical address from the lma
4297 to allow for space taken up by elf headers. */
4298 if (map
->includes_filehdr
)
4299 map
->p_paddr
-= iehdr
->e_ehsize
;
4301 if (map
->includes_phdrs
)
4303 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4305 /* iehdr->e_phnum is just an estimate of the number
4306 of program headers that we will need. Make a note
4307 here of the number we used and the segment we chose
4308 to hold these headers, so that we can adjust the
4309 offset when we know the correct value. */
4310 phdr_adjust_num
= iehdr
->e_phnum
;
4311 phdr_adjust_seg
= map
;
4315 /* Step Three: Loop over the sections again, this time assigning
4316 those that fit to the current segment and remvoing them from the
4317 sections array; but making sure not to leave large gaps. Once all
4318 possible sections have been assigned to the current segment it is
4319 added to the list of built segments and if sections still remain
4320 to be assigned, a new segment is constructed before repeating
4328 /* Fill the current segment with sections that fit. */
4329 for (j
= 0; j
< section_count
; j
++)
4331 section
= sections
[j
];
4333 if (section
== NULL
)
4336 output_section
= section
->output_section
;
4338 BFD_ASSERT (output_section
!= NULL
);
4340 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4341 || IS_COREFILE_NOTE (segment
, section
))
4343 if (map
->count
== 0)
4345 /* If the first section in a segment does not start at
4346 the beginning of the segment, then something is
4348 if (output_section
->lma
!=
4350 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4351 + (map
->includes_phdrs
4352 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4358 asection
* prev_sec
;
4360 prev_sec
= map
->sections
[map
->count
- 1];
4362 /* If the gap between the end of the previous section
4363 and the start of this section is more than
4364 maxpagesize then we need to start a new segment. */
4365 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4366 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4367 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4369 if (suggested_lma
== 0)
4370 suggested_lma
= output_section
->lma
;
4376 map
->sections
[map
->count
++] = output_section
;
4379 section
->segment_mark
= true;
4381 else if (suggested_lma
== 0)
4382 suggested_lma
= output_section
->lma
;
4385 BFD_ASSERT (map
->count
> 0);
4387 /* Add the current segment to the list of built segments. */
4388 *pointer_to_map
= map
;
4389 pointer_to_map
= &map
->next
;
4391 if (isec
< section_count
)
4393 /* We still have not allocated all of the sections to
4394 segments. Create a new segment here, initialise it
4395 and carry on looping. */
4396 amt
= sizeof (struct elf_segment_map
);
4397 amt
+= ((bfd_size_type
) section_count
- 1) * sizeof (asection
*);
4398 map
= (struct elf_segment_map
*) bfd_alloc (obfd
, amt
);
4402 /* Initialise the fields of the segment map. Set the physical
4403 physical address to the LMA of the first section that has
4404 not yet been assigned. */
4406 map
->p_type
= segment
->p_type
;
4407 map
->p_flags
= segment
->p_flags
;
4408 map
->p_flags_valid
= 1;
4409 map
->p_paddr
= suggested_lma
;
4410 map
->p_paddr_valid
= 1;
4411 map
->includes_filehdr
= 0;
4412 map
->includes_phdrs
= 0;
4415 while (isec
< section_count
);
4420 /* The Solaris linker creates program headers in which all the
4421 p_paddr fields are zero. When we try to objcopy or strip such a
4422 file, we get confused. Check for this case, and if we find it
4423 reset the p_paddr_valid fields. */
4424 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4425 if (map
->p_paddr
!= 0)
4429 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4430 map
->p_paddr_valid
= 0;
4433 elf_tdata (obfd
)->segment_map
= map_first
;
4435 /* If we had to estimate the number of program headers that were
4436 going to be needed, then check our estimate know and adjust
4437 the offset if necessary. */
4438 if (phdr_adjust_seg
!= NULL
)
4442 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4445 if (count
> phdr_adjust_num
)
4446 phdr_adjust_seg
->p_paddr
4447 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4451 /* Final Step: Sort the segments into ascending order of physical
4453 if (map_first
!= NULL
)
4455 struct elf_segment_map
*prev
;
4458 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4460 /* Yes I know - its a bubble sort.... */
4461 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4463 /* Swap map and map->next. */
4464 prev
->next
= map
->next
;
4465 map
->next
= map
->next
->next
;
4466 prev
->next
->next
= map
;
4476 #undef IS_CONTAINED_BY_VMA
4477 #undef IS_CONTAINED_BY_LMA
4478 #undef IS_COREFILE_NOTE
4479 #undef IS_SOLARIS_PT_INTERP
4480 #undef INCLUDE_SECTION_IN_SEGMENT
4481 #undef SEGMENT_AFTER_SEGMENT
4482 #undef SEGMENT_OVERLAPS
4486 /* Copy private section information. This copies over the entsize
4487 field, and sometimes the info field. */
4490 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4496 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4498 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4499 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4502 /* Copy over private BFD data if it has not already been copied.
4503 This must be done here, rather than in the copy_private_bfd_data
4504 entry point, because the latter is called after the section
4505 contents have been set, which means that the program headers have
4506 already been worked out. */
4507 if (elf_tdata (obfd
)->segment_map
== NULL
4508 && elf_tdata (ibfd
)->phdr
!= NULL
)
4512 /* Only set up the segments if there are no more SEC_ALLOC
4513 sections. FIXME: This won't do the right thing if objcopy is
4514 used to remove the last SEC_ALLOC section, since objcopy
4515 won't call this routine in that case. */
4516 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4517 if ((s
->flags
& SEC_ALLOC
) != 0)
4521 if (! copy_private_bfd_data (ibfd
, obfd
))
4526 ihdr
= &elf_section_data (isec
)->this_hdr
;
4527 ohdr
= &elf_section_data (osec
)->this_hdr
;
4529 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4531 if (ihdr
->sh_type
== SHT_SYMTAB
4532 || ihdr
->sh_type
== SHT_DYNSYM
4533 || ihdr
->sh_type
== SHT_GNU_verneed
4534 || ihdr
->sh_type
== SHT_GNU_verdef
)
4535 ohdr
->sh_info
= ihdr
->sh_info
;
4537 elf_section_data (osec
)->use_rela_p
4538 = elf_section_data (isec
)->use_rela_p
;
4543 /* Copy private symbol information. If this symbol is in a section
4544 which we did not map into a BFD section, try to map the section
4545 index correctly. We use special macro definitions for the mapped
4546 section indices; these definitions are interpreted by the
4547 swap_out_syms function. */
4549 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4550 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4551 #define MAP_STRTAB (SHN_LORESERVE - 3)
4552 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4555 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4561 elf_symbol_type
*isym
, *osym
;
4563 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4564 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4567 isym
= elf_symbol_from (ibfd
, isymarg
);
4568 osym
= elf_symbol_from (obfd
, osymarg
);
4572 && bfd_is_abs_section (isym
->symbol
.section
))
4576 shndx
= isym
->internal_elf_sym
.st_shndx
;
4577 if (shndx
== elf_onesymtab (ibfd
))
4578 shndx
= MAP_ONESYMTAB
;
4579 else if (shndx
== elf_dynsymtab (ibfd
))
4580 shndx
= MAP_DYNSYMTAB
;
4581 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4583 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4584 shndx
= MAP_SHSTRTAB
;
4585 osym
->internal_elf_sym
.st_shndx
= shndx
;
4591 /* Swap out the symbols. */
4594 swap_out_syms (abfd
, sttp
, relocatable_p
)
4596 struct bfd_strtab_hash
**sttp
;
4599 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4601 if (!elf_map_symbols (abfd
))
4604 /* Dump out the symtabs. */
4606 int symcount
= bfd_get_symcount (abfd
);
4607 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4608 struct bfd_strtab_hash
*stt
;
4609 Elf_Internal_Shdr
*symtab_hdr
;
4610 Elf_Internal_Shdr
*symstrtab_hdr
;
4611 char *outbound_syms
;
4615 stt
= _bfd_elf_stringtab_init ();
4619 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4620 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4621 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4622 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4623 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4624 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4626 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4627 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4629 amt
= (bfd_size_type
) (1 + symcount
) * bed
->s
->sizeof_sym
;
4630 outbound_syms
= bfd_alloc (abfd
, amt
);
4631 if (outbound_syms
== NULL
)
4633 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4635 /* now generate the data (for "contents") */
4637 /* Fill in zeroth symbol and swap it out. */
4638 Elf_Internal_Sym sym
;
4644 sym
.st_shndx
= SHN_UNDEF
;
4645 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4646 outbound_syms
+= bed
->s
->sizeof_sym
;
4648 for (idx
= 0; idx
< symcount
; idx
++)
4650 Elf_Internal_Sym sym
;
4651 bfd_vma value
= syms
[idx
]->value
;
4652 elf_symbol_type
*type_ptr
;
4653 flagword flags
= syms
[idx
]->flags
;
4656 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4658 /* Local section symbols have no name. */
4663 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4666 if (sym
.st_name
== (unsigned long) -1)
4670 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4672 if ((flags
& BSF_SECTION_SYM
) == 0
4673 && bfd_is_com_section (syms
[idx
]->section
))
4675 /* ELF common symbols put the alignment into the `value' field,
4676 and the size into the `size' field. This is backwards from
4677 how BFD handles it, so reverse it here. */
4678 sym
.st_size
= value
;
4679 if (type_ptr
== NULL
4680 || type_ptr
->internal_elf_sym
.st_value
== 0)
4681 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4683 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4684 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4685 (abfd
, syms
[idx
]->section
);
4689 asection
*sec
= syms
[idx
]->section
;
4692 if (sec
->output_section
)
4694 value
+= sec
->output_offset
;
4695 sec
= sec
->output_section
;
4697 /* Don't add in the section vma for relocatable output. */
4698 if (! relocatable_p
)
4700 sym
.st_value
= value
;
4701 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4703 if (bfd_is_abs_section (sec
)
4705 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4707 /* This symbol is in a real ELF section which we did
4708 not create as a BFD section. Undo the mapping done
4709 by copy_private_symbol_data. */
4710 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4714 shndx
= elf_onesymtab (abfd
);
4717 shndx
= elf_dynsymtab (abfd
);
4720 shndx
= elf_tdata (abfd
)->strtab_section
;
4723 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4731 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4737 /* Writing this would be a hell of a lot easier if
4738 we had some decent documentation on bfd, and
4739 knew what to expect of the library, and what to
4740 demand of applications. For example, it
4741 appears that `objcopy' might not set the
4742 section of a symbol to be a section that is
4743 actually in the output file. */
4744 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4745 BFD_ASSERT (sec2
!= 0);
4746 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4747 BFD_ASSERT (shndx
!= -1);
4751 sym
.st_shndx
= shndx
;
4754 if ((flags
& BSF_FUNCTION
) != 0)
4756 else if ((flags
& BSF_OBJECT
) != 0)
4761 /* Processor-specific types */
4762 if (type_ptr
!= NULL
4763 && bed
->elf_backend_get_symbol_type
)
4764 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4766 if (flags
& BSF_SECTION_SYM
)
4768 if (flags
& BSF_GLOBAL
)
4769 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
4771 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4773 else if (bfd_is_com_section (syms
[idx
]->section
))
4774 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4775 else if (bfd_is_und_section (syms
[idx
]->section
))
4776 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4780 else if (flags
& BSF_FILE
)
4781 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4784 int bind
= STB_LOCAL
;
4786 if (flags
& BSF_LOCAL
)
4788 else if (flags
& BSF_WEAK
)
4790 else if (flags
& BSF_GLOBAL
)
4793 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4796 if (type_ptr
!= NULL
)
4797 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4801 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4802 outbound_syms
+= bed
->s
->sizeof_sym
;
4806 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4807 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4809 symstrtab_hdr
->sh_flags
= 0;
4810 symstrtab_hdr
->sh_addr
= 0;
4811 symstrtab_hdr
->sh_entsize
= 0;
4812 symstrtab_hdr
->sh_link
= 0;
4813 symstrtab_hdr
->sh_info
= 0;
4814 symstrtab_hdr
->sh_addralign
= 1;
4820 /* Return the number of bytes required to hold the symtab vector.
4822 Note that we base it on the count plus 1, since we will null terminate
4823 the vector allocated based on this size. However, the ELF symbol table
4824 always has a dummy entry as symbol #0, so it ends up even. */
4827 _bfd_elf_get_symtab_upper_bound (abfd
)
4832 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4834 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4835 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4841 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4846 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4848 if (elf_dynsymtab (abfd
) == 0)
4850 bfd_set_error (bfd_error_invalid_operation
);
4854 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4855 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4861 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4862 bfd
*abfd ATTRIBUTE_UNUSED
;
4865 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4868 /* Canonicalize the relocs. */
4871 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4879 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4881 if (! bed
->s
->slurp_reloc_table (abfd
, section
, symbols
, false))
4884 tblptr
= section
->relocation
;
4885 for (i
= 0; i
< section
->reloc_count
; i
++)
4886 *relptr
++ = tblptr
++;
4890 return section
->reloc_count
;
4894 _bfd_elf_get_symtab (abfd
, alocation
)
4896 asymbol
**alocation
;
4898 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4899 long symcount
= bed
->s
->slurp_symbol_table (abfd
, alocation
, false);
4902 bfd_get_symcount (abfd
) = symcount
;
4907 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4909 asymbol
**alocation
;
4911 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4912 return bed
->s
->slurp_symbol_table (abfd
, alocation
, true);
4915 /* Return the size required for the dynamic reloc entries. Any
4916 section that was actually installed in the BFD, and has type
4917 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4918 considered to be a dynamic reloc section. */
4921 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4927 if (elf_dynsymtab (abfd
) == 0)
4929 bfd_set_error (bfd_error_invalid_operation
);
4933 ret
= sizeof (arelent
*);
4934 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4935 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4936 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4937 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4938 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4939 * sizeof (arelent
*));
4944 /* Canonicalize the dynamic relocation entries. Note that we return
4945 the dynamic relocations as a single block, although they are
4946 actually associated with particular sections; the interface, which
4947 was designed for SunOS style shared libraries, expects that there
4948 is only one set of dynamic relocs. Any section that was actually
4949 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4950 the dynamic symbol table, is considered to be a dynamic reloc
4954 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4959 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4963 if (elf_dynsymtab (abfd
) == 0)
4965 bfd_set_error (bfd_error_invalid_operation
);
4969 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4971 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4973 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4974 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4975 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4980 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4982 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4984 for (i
= 0; i
< count
; i
++)
4995 /* Read in the version information. */
4998 _bfd_elf_slurp_version_tables (abfd
)
5001 bfd_byte
*contents
= NULL
;
5004 if (elf_dynverdef (abfd
) != 0)
5006 Elf_Internal_Shdr
*hdr
;
5007 Elf_External_Verdef
*everdef
;
5008 Elf_Internal_Verdef
*iverdef
;
5009 Elf_Internal_Verdef
*iverdefarr
;
5010 Elf_Internal_Verdef iverdefmem
;
5012 unsigned int maxidx
;
5014 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
5016 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5017 if (contents
== NULL
)
5019 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5020 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5023 /* We know the number of entries in the section but not the maximum
5024 index. Therefore we have to run through all entries and find
5026 everdef
= (Elf_External_Verdef
*) contents
;
5028 for (i
= 0; i
< hdr
->sh_info
; ++i
)
5030 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5032 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
5033 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
5035 everdef
= ((Elf_External_Verdef
*)
5036 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
5039 amt
= (bfd_size_type
) maxidx
* sizeof (Elf_Internal_Verdef
);
5040 elf_tdata (abfd
)->verdef
= (Elf_Internal_Verdef
*) bfd_zalloc (abfd
, amt
);
5041 if (elf_tdata (abfd
)->verdef
== NULL
)
5044 elf_tdata (abfd
)->cverdefs
= maxidx
;
5046 everdef
= (Elf_External_Verdef
*) contents
;
5047 iverdefarr
= elf_tdata (abfd
)->verdef
;
5048 for (i
= 0; i
< hdr
->sh_info
; i
++)
5050 Elf_External_Verdaux
*everdaux
;
5051 Elf_Internal_Verdaux
*iverdaux
;
5054 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
5056 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
5057 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
5059 iverdef
->vd_bfd
= abfd
;
5061 amt
= (bfd_size_type
) iverdef
->vd_cnt
* sizeof (Elf_Internal_Verdaux
);
5062 iverdef
->vd_auxptr
= (Elf_Internal_Verdaux
*) bfd_alloc (abfd
, amt
);
5063 if (iverdef
->vd_auxptr
== NULL
)
5066 everdaux
= ((Elf_External_Verdaux
*)
5067 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
5068 iverdaux
= iverdef
->vd_auxptr
;
5069 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
5071 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
5073 iverdaux
->vda_nodename
=
5074 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5075 iverdaux
->vda_name
);
5076 if (iverdaux
->vda_nodename
== NULL
)
5079 if (j
+ 1 < iverdef
->vd_cnt
)
5080 iverdaux
->vda_nextptr
= iverdaux
+ 1;
5082 iverdaux
->vda_nextptr
= NULL
;
5084 everdaux
= ((Elf_External_Verdaux
*)
5085 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
5088 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
5090 if (i
+ 1 < hdr
->sh_info
)
5091 iverdef
->vd_nextdef
= iverdef
+ 1;
5093 iverdef
->vd_nextdef
= NULL
;
5095 everdef
= ((Elf_External_Verdef
*)
5096 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
5103 if (elf_dynverref (abfd
) != 0)
5105 Elf_Internal_Shdr
*hdr
;
5106 Elf_External_Verneed
*everneed
;
5107 Elf_Internal_Verneed
*iverneed
;
5110 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
5112 amt
= (bfd_size_type
) hdr
->sh_info
* sizeof (Elf_Internal_Verneed
);
5113 elf_tdata (abfd
)->verref
=
5114 (Elf_Internal_Verneed
*) bfd_zalloc (abfd
, amt
);
5115 if (elf_tdata (abfd
)->verref
== NULL
)
5118 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
5120 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
5121 if (contents
== NULL
)
5123 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
5124 || bfd_bread ((PTR
) contents
, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
5127 everneed
= (Elf_External_Verneed
*) contents
;
5128 iverneed
= elf_tdata (abfd
)->verref
;
5129 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
5131 Elf_External_Vernaux
*evernaux
;
5132 Elf_Internal_Vernaux
*ivernaux
;
5135 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
5137 iverneed
->vn_bfd
= abfd
;
5139 iverneed
->vn_filename
=
5140 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5142 if (iverneed
->vn_filename
== NULL
)
5145 amt
= iverneed
->vn_cnt
;
5146 amt
*= sizeof (Elf_Internal_Vernaux
);
5147 iverneed
->vn_auxptr
= (Elf_Internal_Vernaux
*) bfd_alloc (abfd
, amt
);
5149 evernaux
= ((Elf_External_Vernaux
*)
5150 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
5151 ivernaux
= iverneed
->vn_auxptr
;
5152 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
5154 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
5156 ivernaux
->vna_nodename
=
5157 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
5158 ivernaux
->vna_name
);
5159 if (ivernaux
->vna_nodename
== NULL
)
5162 if (j
+ 1 < iverneed
->vn_cnt
)
5163 ivernaux
->vna_nextptr
= ivernaux
+ 1;
5165 ivernaux
->vna_nextptr
= NULL
;
5167 evernaux
= ((Elf_External_Vernaux
*)
5168 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
5171 if (i
+ 1 < hdr
->sh_info
)
5172 iverneed
->vn_nextref
= iverneed
+ 1;
5174 iverneed
->vn_nextref
= NULL
;
5176 everneed
= ((Elf_External_Verneed
*)
5177 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
5187 if (contents
== NULL
)
5193 _bfd_elf_make_empty_symbol (abfd
)
5196 elf_symbol_type
*newsym
;
5197 bfd_size_type amt
= sizeof (elf_symbol_type
);
5199 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, amt
);
5204 newsym
->symbol
.the_bfd
= abfd
;
5205 return &newsym
->symbol
;
5210 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
5211 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5215 bfd_symbol_info (symbol
, ret
);
5218 /* Return whether a symbol name implies a local symbol. Most targets
5219 use this function for the is_local_label_name entry point, but some
5223 _bfd_elf_is_local_label_name (abfd
, name
)
5224 bfd
*abfd ATTRIBUTE_UNUSED
;
5227 /* Normal local symbols start with ``.L''. */
5228 if (name
[0] == '.' && name
[1] == 'L')
5231 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5232 DWARF debugging symbols starting with ``..''. */
5233 if (name
[0] == '.' && name
[1] == '.')
5236 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5237 emitting DWARF debugging output. I suspect this is actually a
5238 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5239 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5240 underscore to be emitted on some ELF targets). For ease of use,
5241 we treat such symbols as local. */
5242 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
5249 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
5250 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
5251 asymbol
*symbol ATTRIBUTE_UNUSED
;
5258 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5260 enum bfd_architecture arch
;
5261 unsigned long machine
;
5263 /* If this isn't the right architecture for this backend, and this
5264 isn't the generic backend, fail. */
5265 if (arch
!= get_elf_backend_data (abfd
)->arch
5266 && arch
!= bfd_arch_unknown
5267 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5270 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5273 /* Find the function to a particular section and offset,
5274 for error reporting. */
5277 elf_find_function (abfd
, section
, symbols
, offset
,
5278 filename_ptr
, functionname_ptr
)
5279 bfd
*abfd ATTRIBUTE_UNUSED
;
5283 const char **filename_ptr
;
5284 const char **functionname_ptr
;
5286 const char *filename
;
5295 for (p
= symbols
; *p
!= NULL
; p
++)
5299 q
= (elf_symbol_type
*) *p
;
5301 if (bfd_get_section (&q
->symbol
) != section
)
5304 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5309 filename
= bfd_asymbol_name (&q
->symbol
);
5313 if (q
->symbol
.section
== section
5314 && q
->symbol
.value
>= low_func
5315 && q
->symbol
.value
<= offset
)
5317 func
= (asymbol
*) q
;
5318 low_func
= q
->symbol
.value
;
5328 *filename_ptr
= filename
;
5329 if (functionname_ptr
)
5330 *functionname_ptr
= bfd_asymbol_name (func
);
5335 /* Find the nearest line to a particular section and offset,
5336 for error reporting. */
5339 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5340 filename_ptr
, functionname_ptr
, line_ptr
)
5345 const char **filename_ptr
;
5346 const char **functionname_ptr
;
5347 unsigned int *line_ptr
;
5351 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5352 filename_ptr
, functionname_ptr
,
5355 if (!*functionname_ptr
)
5356 elf_find_function (abfd
, section
, symbols
, offset
,
5357 *filename_ptr
? NULL
: filename_ptr
,
5363 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5364 filename_ptr
, functionname_ptr
,
5366 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5368 if (!*functionname_ptr
)
5369 elf_find_function (abfd
, section
, symbols
, offset
,
5370 *filename_ptr
? NULL
: filename_ptr
,
5376 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5377 &found
, filename_ptr
,
5378 functionname_ptr
, line_ptr
,
5379 &elf_tdata (abfd
)->line_info
))
5384 if (symbols
== NULL
)
5387 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5388 filename_ptr
, functionname_ptr
))
5396 _bfd_elf_sizeof_headers (abfd
, reloc
)
5402 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5404 ret
+= get_program_header_size (abfd
);
5409 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5414 bfd_size_type count
;
5416 Elf_Internal_Shdr
*hdr
;
5419 if (! abfd
->output_has_begun
5420 && ! _bfd_elf_compute_section_file_positions
5421 (abfd
, (struct bfd_link_info
*) NULL
))
5424 hdr
= &elf_section_data (section
)->this_hdr
;
5425 pos
= hdr
->sh_offset
+ offset
;
5426 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
5427 || bfd_bwrite (location
, count
, abfd
) != count
)
5434 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5435 bfd
*abfd ATTRIBUTE_UNUSED
;
5436 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5437 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5444 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5447 Elf_Internal_Rel
*dst
;
5453 /* Try to convert a non-ELF reloc into an ELF one. */
5456 _bfd_elf_validate_reloc (abfd
, areloc
)
5460 /* Check whether we really have an ELF howto. */
5462 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5464 bfd_reloc_code_real_type code
;
5465 reloc_howto_type
*howto
;
5467 /* Alien reloc: Try to determine its type to replace it with an
5468 equivalent ELF reloc. */
5470 if (areloc
->howto
->pc_relative
)
5472 switch (areloc
->howto
->bitsize
)
5475 code
= BFD_RELOC_8_PCREL
;
5478 code
= BFD_RELOC_12_PCREL
;
5481 code
= BFD_RELOC_16_PCREL
;
5484 code
= BFD_RELOC_24_PCREL
;
5487 code
= BFD_RELOC_32_PCREL
;
5490 code
= BFD_RELOC_64_PCREL
;
5496 howto
= bfd_reloc_type_lookup (abfd
, code
);
5498 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5500 if (howto
->pcrel_offset
)
5501 areloc
->addend
+= areloc
->address
;
5503 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5508 switch (areloc
->howto
->bitsize
)
5514 code
= BFD_RELOC_14
;
5517 code
= BFD_RELOC_16
;
5520 code
= BFD_RELOC_26
;
5523 code
= BFD_RELOC_32
;
5526 code
= BFD_RELOC_64
;
5532 howto
= bfd_reloc_type_lookup (abfd
, code
);
5536 areloc
->howto
= howto
;
5544 (*_bfd_error_handler
)
5545 (_("%s: unsupported relocation type %s"),
5546 bfd_archive_filename (abfd
), areloc
->howto
->name
);
5547 bfd_set_error (bfd_error_bad_value
);
5552 _bfd_elf_close_and_cleanup (abfd
)
5555 if (bfd_get_format (abfd
) == bfd_object
)
5557 if (elf_shstrtab (abfd
) != NULL
)
5558 _bfd_stringtab_free (elf_shstrtab (abfd
));
5561 return _bfd_generic_close_and_cleanup (abfd
);
5564 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5565 in the relocation's offset. Thus we cannot allow any sort of sanity
5566 range-checking to interfere. There is nothing else to do in processing
5569 bfd_reloc_status_type
5570 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5571 bfd
*abfd ATTRIBUTE_UNUSED
;
5572 arelent
*re ATTRIBUTE_UNUSED
;
5573 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5574 PTR data ATTRIBUTE_UNUSED
;
5575 asection
*is ATTRIBUTE_UNUSED
;
5576 bfd
*obfd ATTRIBUTE_UNUSED
;
5577 char **errmsg ATTRIBUTE_UNUSED
;
5579 return bfd_reloc_ok
;
5582 /* Elf core file support. Much of this only works on native
5583 toolchains, since we rely on knowing the
5584 machine-dependent procfs structure in order to pick
5585 out details about the corefile. */
5587 #ifdef HAVE_SYS_PROCFS_H
5588 # include <sys/procfs.h>
5591 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5594 elfcore_make_pid (abfd
)
5597 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5598 + (elf_tdata (abfd
)->core_pid
));
5601 /* If there isn't a section called NAME, make one, using
5602 data from SECT. Note, this function will generate a
5603 reference to NAME, so you shouldn't deallocate or
5607 elfcore_maybe_make_sect (abfd
, name
, sect
)
5614 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5617 sect2
= bfd_make_section (abfd
, name
);
5621 sect2
->_raw_size
= sect
->_raw_size
;
5622 sect2
->filepos
= sect
->filepos
;
5623 sect2
->flags
= sect
->flags
;
5624 sect2
->alignment_power
= sect
->alignment_power
;
5628 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5629 actually creates up to two pseudosections:
5630 - For the single-threaded case, a section named NAME, unless
5631 such a section already exists.
5632 - For the multi-threaded case, a section named "NAME/PID", where
5633 PID is elfcore_make_pid (abfd).
5634 Both pseudosections have identical contents. */
5636 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5643 char *threaded_name
;
5646 /* Build the section name. */
5648 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5649 threaded_name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5650 if (threaded_name
== NULL
)
5652 strcpy (threaded_name
, buf
);
5654 sect
= bfd_make_section (abfd
, threaded_name
);
5657 sect
->_raw_size
= size
;
5658 sect
->filepos
= filepos
;
5659 sect
->flags
= SEC_HAS_CONTENTS
;
5660 sect
->alignment_power
= 2;
5662 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5665 /* prstatus_t exists on:
5667 linux 2.[01] + glibc
5671 #if defined (HAVE_PRSTATUS_T)
5672 static boolean elfcore_grok_prstatus
PARAMS ((bfd
*, Elf_Internal_Note
*));
5675 elfcore_grok_prstatus (abfd
, note
)
5677 Elf_Internal_Note
*note
;
5682 if (note
->descsz
== sizeof (prstatus_t
))
5686 raw_size
= sizeof (prstat
.pr_reg
);
5687 offset
= offsetof (prstatus_t
, pr_reg
);
5688 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5690 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5691 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5693 /* pr_who exists on:
5696 pr_who doesn't exist on:
5699 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5700 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5703 #if defined (HAVE_PRSTATUS32_T)
5704 else if (note
->descsz
== sizeof (prstatus32_t
))
5706 /* 64-bit host, 32-bit corefile */
5707 prstatus32_t prstat
;
5709 raw_size
= sizeof (prstat
.pr_reg
);
5710 offset
= offsetof (prstatus32_t
, pr_reg
);
5711 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5713 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5714 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5716 /* pr_who exists on:
5719 pr_who doesn't exist on:
5722 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5723 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5726 #endif /* HAVE_PRSTATUS32_T */
5729 /* Fail - we don't know how to handle any other
5730 note size (ie. data object type). */
5734 /* Make a ".reg/999" section and a ".reg" section. */
5735 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5736 raw_size
, note
->descpos
+ offset
);
5738 #endif /* defined (HAVE_PRSTATUS_T) */
5740 /* Create a pseudosection containing the exact contents of NOTE. */
5742 elfcore_make_note_pseudosection (abfd
, name
, note
)
5745 Elf_Internal_Note
*note
;
5747 return _bfd_elfcore_make_pseudosection (abfd
, name
,
5748 note
->descsz
, note
->descpos
);
5751 /* There isn't a consistent prfpregset_t across platforms,
5752 but it doesn't matter, because we don't have to pick this
5753 data structure apart. */
5756 elfcore_grok_prfpreg (abfd
, note
)
5758 Elf_Internal_Note
*note
;
5760 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5763 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5764 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5768 elfcore_grok_prxfpreg (abfd
, note
)
5770 Elf_Internal_Note
*note
;
5772 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5775 #if defined (HAVE_PRPSINFO_T)
5776 typedef prpsinfo_t elfcore_psinfo_t
;
5777 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5778 typedef prpsinfo32_t elfcore_psinfo32_t
;
5782 #if defined (HAVE_PSINFO_T)
5783 typedef psinfo_t elfcore_psinfo_t
;
5784 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5785 typedef psinfo32_t elfcore_psinfo32_t
;
5789 /* return a malloc'ed copy of a string at START which is at
5790 most MAX bytes long, possibly without a terminating '\0'.
5791 the copy will always have a terminating '\0'. */
5794 _bfd_elfcore_strndup (abfd
, start
, max
)
5800 char *end
= memchr (start
, '\0', max
);
5808 dups
= bfd_alloc (abfd
, (bfd_size_type
) len
+ 1);
5812 memcpy (dups
, start
, len
);
5818 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5819 static boolean elfcore_grok_psinfo
PARAMS ((bfd
*, Elf_Internal_Note
*));
5822 elfcore_grok_psinfo (abfd
, note
)
5824 Elf_Internal_Note
*note
;
5826 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5828 elfcore_psinfo_t psinfo
;
5830 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5832 elf_tdata (abfd
)->core_program
5833 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5834 sizeof (psinfo
.pr_fname
));
5836 elf_tdata (abfd
)->core_command
5837 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5838 sizeof (psinfo
.pr_psargs
));
5840 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5841 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5843 /* 64-bit host, 32-bit corefile */
5844 elfcore_psinfo32_t psinfo
;
5846 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5848 elf_tdata (abfd
)->core_program
5849 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5850 sizeof (psinfo
.pr_fname
));
5852 elf_tdata (abfd
)->core_command
5853 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5854 sizeof (psinfo
.pr_psargs
));
5860 /* Fail - we don't know how to handle any other
5861 note size (ie. data object type). */
5865 /* Note that for some reason, a spurious space is tacked
5866 onto the end of the args in some (at least one anyway)
5867 implementations, so strip it off if it exists. */
5870 char *command
= elf_tdata (abfd
)->core_command
;
5871 int n
= strlen (command
);
5873 if (0 < n
&& command
[n
- 1] == ' ')
5874 command
[n
- 1] = '\0';
5879 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5881 #if defined (HAVE_PSTATUS_T)
5883 elfcore_grok_pstatus (abfd
, note
)
5885 Elf_Internal_Note
*note
;
5887 if (note
->descsz
== sizeof (pstatus_t
)
5888 #if defined (HAVE_PXSTATUS_T)
5889 || note
->descsz
== sizeof (pxstatus_t
)
5895 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5897 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5899 #if defined (HAVE_PSTATUS32_T)
5900 else if (note
->descsz
== sizeof (pstatus32_t
))
5902 /* 64-bit host, 32-bit corefile */
5905 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5907 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5910 /* Could grab some more details from the "representative"
5911 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5912 NT_LWPSTATUS note, presumably. */
5916 #endif /* defined (HAVE_PSTATUS_T) */
5918 #if defined (HAVE_LWPSTATUS_T)
5920 elfcore_grok_lwpstatus (abfd
, note
)
5922 Elf_Internal_Note
*note
;
5924 lwpstatus_t lwpstat
;
5929 if (note
->descsz
!= sizeof (lwpstat
)
5930 #if defined (HAVE_LWPXSTATUS_T)
5931 && note
->descsz
!= sizeof (lwpxstatus_t
)
5936 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5938 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5939 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5941 /* Make a ".reg/999" section. */
5943 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5944 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5949 sect
= bfd_make_section (abfd
, name
);
5953 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5954 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5955 sect
->filepos
= note
->descpos
5956 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5959 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5960 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5961 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5964 sect
->flags
= SEC_HAS_CONTENTS
;
5965 sect
->alignment_power
= 2;
5967 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5970 /* Make a ".reg2/999" section */
5972 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5973 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
5978 sect
= bfd_make_section (abfd
, name
);
5982 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5983 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5984 sect
->filepos
= note
->descpos
5985 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5988 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5989 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5990 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5993 sect
->flags
= SEC_HAS_CONTENTS
;
5994 sect
->alignment_power
= 2;
5996 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
5998 #endif /* defined (HAVE_LWPSTATUS_T) */
6000 #if defined (HAVE_WIN32_PSTATUS_T)
6002 elfcore_grok_win32pstatus (abfd
, note
)
6004 Elf_Internal_Note
*note
;
6009 win32_pstatus_t pstatus
;
6011 if (note
->descsz
< sizeof (pstatus
))
6014 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
6016 switch (pstatus
.data_type
)
6018 case NOTE_INFO_PROCESS
:
6019 /* FIXME: need to add ->core_command. */
6020 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
6021 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
6024 case NOTE_INFO_THREAD
:
6025 /* Make a ".reg/999" section. */
6026 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
6028 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6034 sect
= bfd_make_section (abfd
, name
);
6038 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
6039 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
6040 data
.thread_info
.thread_context
);
6041 sect
->flags
= SEC_HAS_CONTENTS
;
6042 sect
->alignment_power
= 2;
6044 if (pstatus
.data
.thread_info
.is_active_thread
)
6045 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
6049 case NOTE_INFO_MODULE
:
6050 /* Make a ".module/xxxxxxxx" section. */
6051 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
6053 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (buf
) + 1);
6059 sect
= bfd_make_section (abfd
, name
);
6064 sect
->_raw_size
= note
->descsz
;
6065 sect
->filepos
= note
->descpos
;
6066 sect
->flags
= SEC_HAS_CONTENTS
;
6067 sect
->alignment_power
= 2;
6076 #endif /* HAVE_WIN32_PSTATUS_T */
6079 elfcore_grok_note (abfd
, note
)
6081 Elf_Internal_Note
*note
;
6083 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
6091 if (bed
->elf_backend_grok_prstatus
)
6092 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
6094 #if defined (HAVE_PRSTATUS_T)
6095 return elfcore_grok_prstatus (abfd
, note
);
6100 #if defined (HAVE_PSTATUS_T)
6102 return elfcore_grok_pstatus (abfd
, note
);
6105 #if defined (HAVE_LWPSTATUS_T)
6107 return elfcore_grok_lwpstatus (abfd
, note
);
6110 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
6111 return elfcore_grok_prfpreg (abfd
, note
);
6113 #if defined (HAVE_WIN32_PSTATUS_T)
6114 case NT_WIN32PSTATUS
:
6115 return elfcore_grok_win32pstatus (abfd
, note
);
6118 case NT_PRXFPREG
: /* Linux SSE extension */
6119 if (note
->namesz
== 5
6120 && ! strcmp (note
->namedata
, "LINUX"))
6121 return elfcore_grok_prxfpreg (abfd
, note
);
6127 if (bed
->elf_backend_grok_psinfo
)
6128 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
6130 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6131 return elfcore_grok_psinfo (abfd
, note
);
6139 elfcore_read_notes (abfd
, offset
, size
)
6150 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0)
6153 buf
= bfd_malloc (size
);
6157 if (bfd_bread (buf
, size
, abfd
) != size
)
6165 while (p
< buf
+ size
)
6167 /* FIXME: bad alignment assumption. */
6168 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
6169 Elf_Internal_Note in
;
6171 in
.type
= H_GET_32 (abfd
, xnp
->type
);
6173 in
.namesz
= H_GET_32 (abfd
, xnp
->namesz
);
6174 in
.namedata
= xnp
->name
;
6176 in
.descsz
= H_GET_32 (abfd
, xnp
->descsz
);
6177 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
6178 in
.descpos
= offset
+ (in
.descdata
- buf
);
6180 if (! elfcore_grok_note (abfd
, &in
))
6183 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
6190 /* Providing external access to the ELF program header table. */
6192 /* Return an upper bound on the number of bytes required to store a
6193 copy of ABFD's program header table entries. Return -1 if an error
6194 occurs; bfd_get_error will return an appropriate code. */
6197 bfd_get_elf_phdr_upper_bound (abfd
)
6200 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6202 bfd_set_error (bfd_error_wrong_format
);
6206 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
6209 /* Copy ABFD's program header table entries to *PHDRS. The entries
6210 will be stored as an array of Elf_Internal_Phdr structures, as
6211 defined in include/elf/internal.h. To find out how large the
6212 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6214 Return the number of program header table entries read, or -1 if an
6215 error occurs; bfd_get_error will return an appropriate code. */
6218 bfd_get_elf_phdrs (abfd
, phdrs
)
6224 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
6226 bfd_set_error (bfd_error_wrong_format
);
6230 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
6231 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
6232 num_phdrs
* sizeof (Elf_Internal_Phdr
));
6238 _bfd_elf_sprintf_vma (abfd
, buf
, value
)
6239 bfd
*abfd ATTRIBUTE_UNUSED
;
6244 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6246 i_ehdrp
= elf_elfheader (abfd
);
6247 if (i_ehdrp
== NULL
)
6248 sprintf_vma (buf
, value
);
6251 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6253 #if BFD_HOST_64BIT_LONG
6254 sprintf (buf
, "%016lx", value
);
6256 sprintf (buf
, "%08lx%08lx", _bfd_int64_high (value
),
6257 _bfd_int64_low (value
));
6261 sprintf (buf
, "%08lx", (unsigned long) (value
& 0xffffffff));
6264 sprintf_vma (buf
, value
);
6269 _bfd_elf_fprintf_vma (abfd
, stream
, value
)
6270 bfd
*abfd ATTRIBUTE_UNUSED
;
6275 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
6277 i_ehdrp
= elf_elfheader (abfd
);
6278 if (i_ehdrp
== NULL
)
6279 fprintf_vma ((FILE *) stream
, value
);
6282 if (i_ehdrp
->e_ident
[EI_CLASS
] == ELFCLASS64
)
6284 #if BFD_HOST_64BIT_LONG
6285 fprintf ((FILE *) stream
, "%016lx", value
);
6287 fprintf ((FILE *) stream
, "%08lx%08lx",
6288 _bfd_int64_high (value
), _bfd_int64_low (value
));
6292 fprintf ((FILE *) stream
, "%08lx",
6293 (unsigned long) (value
& 0xffffffff));
6296 fprintf_vma ((FILE *) stream
, value
);
6300 enum elf_reloc_type_class
6301 _bfd_elf_reloc_type_class (rela
)
6302 const Elf_Internal_Rela
*rela ATTRIBUTE_UNUSED
;
6304 return reloc_class_normal
;