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
*, long, unsigned int));
54 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
55 static boolean assign_section_numbers
PARAMS ((bfd
*));
56 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
57 static boolean elf_map_symbols
PARAMS ((bfd
*));
58 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
59 static boolean elfcore_read_notes
PARAMS ((bfd
*, bfd_vma
, bfd_vma
));
60 static boolean elf_find_function
PARAMS ((bfd
*, asection
*,
62 bfd_vma
, const char **,
65 /* Swap version information in and out. The version information is
66 currently size independent. If that ever changes, this code will
67 need to move into elfcode.h. */
69 /* Swap in a Verdef structure. */
72 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
74 const Elf_External_Verdef
*src
;
75 Elf_Internal_Verdef
*dst
;
77 dst
->vd_version
= bfd_h_get_16 (abfd
, src
->vd_version
);
78 dst
->vd_flags
= bfd_h_get_16 (abfd
, src
->vd_flags
);
79 dst
->vd_ndx
= bfd_h_get_16 (abfd
, src
->vd_ndx
);
80 dst
->vd_cnt
= bfd_h_get_16 (abfd
, src
->vd_cnt
);
81 dst
->vd_hash
= bfd_h_get_32 (abfd
, src
->vd_hash
);
82 dst
->vd_aux
= bfd_h_get_32 (abfd
, src
->vd_aux
);
83 dst
->vd_next
= bfd_h_get_32 (abfd
, src
->vd_next
);
86 /* Swap out a Verdef structure. */
89 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
91 const Elf_Internal_Verdef
*src
;
92 Elf_External_Verdef
*dst
;
94 bfd_h_put_16 (abfd
, src
->vd_version
, dst
->vd_version
);
95 bfd_h_put_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
96 bfd_h_put_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
97 bfd_h_put_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
98 bfd_h_put_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
99 bfd_h_put_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
100 bfd_h_put_32 (abfd
, src
->vd_next
, dst
->vd_next
);
103 /* Swap in a Verdaux structure. */
106 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
108 const Elf_External_Verdaux
*src
;
109 Elf_Internal_Verdaux
*dst
;
111 dst
->vda_name
= bfd_h_get_32 (abfd
, src
->vda_name
);
112 dst
->vda_next
= bfd_h_get_32 (abfd
, src
->vda_next
);
115 /* Swap out a Verdaux structure. */
118 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
120 const Elf_Internal_Verdaux
*src
;
121 Elf_External_Verdaux
*dst
;
123 bfd_h_put_32 (abfd
, src
->vda_name
, dst
->vda_name
);
124 bfd_h_put_32 (abfd
, src
->vda_next
, dst
->vda_next
);
127 /* Swap in a Verneed structure. */
130 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
132 const Elf_External_Verneed
*src
;
133 Elf_Internal_Verneed
*dst
;
135 dst
->vn_version
= bfd_h_get_16 (abfd
, src
->vn_version
);
136 dst
->vn_cnt
= bfd_h_get_16 (abfd
, src
->vn_cnt
);
137 dst
->vn_file
= bfd_h_get_32 (abfd
, src
->vn_file
);
138 dst
->vn_aux
= bfd_h_get_32 (abfd
, src
->vn_aux
);
139 dst
->vn_next
= bfd_h_get_32 (abfd
, src
->vn_next
);
142 /* Swap out a Verneed structure. */
145 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
147 const Elf_Internal_Verneed
*src
;
148 Elf_External_Verneed
*dst
;
150 bfd_h_put_16 (abfd
, src
->vn_version
, dst
->vn_version
);
151 bfd_h_put_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
152 bfd_h_put_32 (abfd
, src
->vn_file
, dst
->vn_file
);
153 bfd_h_put_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
154 bfd_h_put_32 (abfd
, src
->vn_next
, dst
->vn_next
);
157 /* Swap in a Vernaux structure. */
160 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
162 const Elf_External_Vernaux
*src
;
163 Elf_Internal_Vernaux
*dst
;
165 dst
->vna_hash
= bfd_h_get_32 (abfd
, src
->vna_hash
);
166 dst
->vna_flags
= bfd_h_get_16 (abfd
, src
->vna_flags
);
167 dst
->vna_other
= bfd_h_get_16 (abfd
, src
->vna_other
);
168 dst
->vna_name
= bfd_h_get_32 (abfd
, src
->vna_name
);
169 dst
->vna_next
= bfd_h_get_32 (abfd
, src
->vna_next
);
172 /* Swap out a Vernaux structure. */
175 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
177 const Elf_Internal_Vernaux
*src
;
178 Elf_External_Vernaux
*dst
;
180 bfd_h_put_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
181 bfd_h_put_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
182 bfd_h_put_16 (abfd
, src
->vna_other
, dst
->vna_other
);
183 bfd_h_put_32 (abfd
, src
->vna_name
, dst
->vna_name
);
184 bfd_h_put_32 (abfd
, src
->vna_next
, dst
->vna_next
);
187 /* Swap in a Versym structure. */
190 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
192 const Elf_External_Versym
*src
;
193 Elf_Internal_Versym
*dst
;
195 dst
->vs_vers
= bfd_h_get_16 (abfd
, src
->vs_vers
);
198 /* Swap out a Versym structure. */
201 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
203 const Elf_Internal_Versym
*src
;
204 Elf_External_Versym
*dst
;
206 bfd_h_put_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
209 /* Standard ELF hash function. Do not change this function; you will
210 cause invalid hash tables to be generated. */
213 bfd_elf_hash (namearg
)
216 const unsigned char *name
= (const unsigned char *) namearg
;
221 while ((ch
= *name
++) != '\0')
224 if ((g
= (h
& 0xf0000000)) != 0)
227 /* The ELF ABI says `h &= ~g', but this is equivalent in
228 this case and on some machines one insn instead of two. */
235 /* Read a specified number of bytes at a specified offset in an ELF
236 file, into a newly allocated buffer, and return a pointer to the
240 elf_read (abfd
, offset
, size
)
247 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
249 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
251 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
253 if (bfd_get_error () != bfd_error_system_call
)
254 bfd_set_error (bfd_error_file_truncated
);
261 bfd_elf_mkobject (abfd
)
264 /* This just does initialization. */
265 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
266 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
267 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
268 if (elf_tdata (abfd
) == 0)
270 /* Since everything is done at close time, do we need any
277 bfd_elf_mkcorefile (abfd
)
280 /* I think this can be done just like an object file. */
281 return bfd_elf_mkobject (abfd
);
285 bfd_elf_get_str_section (abfd
, shindex
)
287 unsigned int shindex
;
289 Elf_Internal_Shdr
**i_shdrp
;
290 char *shstrtab
= NULL
;
292 unsigned int shstrtabsize
;
294 i_shdrp
= elf_elfsections (abfd
);
295 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
298 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
299 if (shstrtab
== NULL
)
301 /* No cached one, attempt to read, and cache what we read. */
302 offset
= i_shdrp
[shindex
]->sh_offset
;
303 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
304 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
305 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
311 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
313 unsigned int shindex
;
314 unsigned int strindex
;
316 Elf_Internal_Shdr
*hdr
;
321 hdr
= elf_elfsections (abfd
)[shindex
];
323 if (hdr
->contents
== NULL
324 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
327 if (strindex
>= hdr
->sh_size
)
329 (*_bfd_error_handler
)
330 (_("%s: invalid string offset %u >= %lu for section `%s'"),
331 bfd_get_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
332 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
333 && strindex
== hdr
->sh_name
)
335 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
339 return ((char *) hdr
->contents
) + strindex
;
342 /* Make a BFD section from an ELF section. We store a pointer to the
343 BFD section in the bfd_section field of the header. */
346 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
348 Elf_Internal_Shdr
*hdr
;
353 struct elf_backend_data
*bed
;
355 if (hdr
->bfd_section
!= NULL
)
357 BFD_ASSERT (strcmp (name
,
358 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
362 newsect
= bfd_make_section_anyway (abfd
, name
);
366 newsect
->filepos
= hdr
->sh_offset
;
368 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
369 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
370 || ! bfd_set_section_alignment (abfd
, newsect
,
371 bfd_log2 (hdr
->sh_addralign
)))
374 flags
= SEC_NO_FLAGS
;
375 if (hdr
->sh_type
!= SHT_NOBITS
)
376 flags
|= SEC_HAS_CONTENTS
;
377 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
380 if (hdr
->sh_type
!= SHT_NOBITS
)
383 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
384 flags
|= SEC_READONLY
;
385 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
387 else if ((flags
& SEC_LOAD
) != 0)
389 if ((hdr
->sh_flags
& SHF_MERGE
) != 0)
392 newsect
->entsize
= hdr
->sh_entsize
;
393 if ((hdr
->sh_flags
& SHF_STRINGS
) != 0)
394 flags
|= SEC_STRINGS
;
397 /* The debugging sections appear to be recognized only by name, not
400 static const char *debug_sec_names
[] =
409 for (i
= sizeof (debug_sec_names
) / sizeof (debug_sec_names
[0]); i
--;)
410 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
414 flags
|= SEC_DEBUGGING
;
417 /* As a GNU extension, if the name begins with .gnu.linkonce, we
418 only link a single copy of the section. This is used to support
419 g++. g++ will emit each template expansion in its own section.
420 The symbols will be defined as weak, so that multiple definitions
421 are permitted. The GNU linker extension is to actually discard
422 all but one of the sections. */
423 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
424 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
426 bed
= get_elf_backend_data (abfd
);
427 if (bed
->elf_backend_section_flags
)
428 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
431 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
434 if ((flags
& SEC_ALLOC
) != 0)
436 Elf_Internal_Phdr
*phdr
;
439 /* Look through the phdrs to see if we need to adjust the lma.
440 If all the p_paddr fields are zero, we ignore them, since
441 some ELF linkers produce such output. */
442 phdr
= elf_tdata (abfd
)->phdr
;
443 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
445 if (phdr
->p_paddr
!= 0)
448 if (i
< elf_elfheader (abfd
)->e_phnum
)
450 phdr
= elf_tdata (abfd
)->phdr
;
451 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
453 if (phdr
->p_type
== PT_LOAD
454 && phdr
->p_vaddr
!= phdr
->p_paddr
455 && phdr
->p_vaddr
<= hdr
->sh_addr
456 && (phdr
->p_vaddr
+ phdr
->p_memsz
457 >= hdr
->sh_addr
+ hdr
->sh_size
)
458 && ((flags
& SEC_LOAD
) == 0
459 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
460 && (phdr
->p_offset
+ phdr
->p_filesz
461 >= hdr
->sh_offset
+ hdr
->sh_size
))))
463 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
470 hdr
->bfd_section
= newsect
;
471 elf_section_data (newsect
)->this_hdr
= *hdr
;
481 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
484 Helper functions for GDB to locate the string tables.
485 Since BFD hides string tables from callers, GDB needs to use an
486 internal hook to find them. Sun's .stabstr, in particular,
487 isn't even pointed to by the .stab section, so ordinary
488 mechanisms wouldn't work to find it, even if we had some.
491 struct elf_internal_shdr
*
492 bfd_elf_find_section (abfd
, name
)
496 Elf_Internal_Shdr
**i_shdrp
;
501 i_shdrp
= elf_elfsections (abfd
);
504 shstrtab
= bfd_elf_get_str_section
505 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
506 if (shstrtab
!= NULL
)
508 max
= elf_elfheader (abfd
)->e_shnum
;
509 for (i
= 1; i
< max
; i
++)
510 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
517 const char *const bfd_elf_section_type_names
[] = {
518 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
519 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
520 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
523 /* ELF relocs are against symbols. If we are producing relocateable
524 output, and the reloc is against an external symbol, and nothing
525 has given us any additional addend, the resulting reloc will also
526 be against the same symbol. In such a case, we don't want to
527 change anything about the way the reloc is handled, since it will
528 all be done at final link time. Rather than put special case code
529 into bfd_perform_relocation, all the reloc types use this howto
530 function. It just short circuits the reloc if producing
531 relocateable output against an external symbol. */
533 bfd_reloc_status_type
534 bfd_elf_generic_reloc (abfd
,
541 bfd
*abfd ATTRIBUTE_UNUSED
;
542 arelent
*reloc_entry
;
544 PTR data ATTRIBUTE_UNUSED
;
545 asection
*input_section
;
547 char **error_message ATTRIBUTE_UNUSED
;
549 if (output_bfd
!= (bfd
*) NULL
550 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
551 && (! reloc_entry
->howto
->partial_inplace
552 || reloc_entry
->addend
== 0))
554 reloc_entry
->address
+= input_section
->output_offset
;
558 return bfd_reloc_continue
;
561 /* Finish SHF_MERGE section merging. */
564 _bfd_elf_merge_sections (abfd
, info
)
566 struct bfd_link_info
*info
;
568 if (elf_hash_table (info
)->merge_info
)
569 _bfd_merge_sections (abfd
, elf_hash_table (info
)->merge_info
);
573 /* Print out the program headers. */
576 _bfd_elf_print_private_bfd_data (abfd
, farg
)
580 FILE *f
= (FILE *) farg
;
581 Elf_Internal_Phdr
*p
;
583 bfd_byte
*dynbuf
= NULL
;
585 p
= elf_tdata (abfd
)->phdr
;
590 fprintf (f
, _("\nProgram Header:\n"));
591 c
= elf_elfheader (abfd
)->e_phnum
;
592 for (i
= 0; i
< c
; i
++, p
++)
599 case PT_NULL
: s
= "NULL"; break;
600 case PT_LOAD
: s
= "LOAD"; break;
601 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
602 case PT_INTERP
: s
= "INTERP"; break;
603 case PT_NOTE
: s
= "NOTE"; break;
604 case PT_SHLIB
: s
= "SHLIB"; break;
605 case PT_PHDR
: s
= "PHDR"; break;
606 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
608 fprintf (f
, "%8s off 0x", s
);
609 fprintf_vma (f
, p
->p_offset
);
610 fprintf (f
, " vaddr 0x");
611 fprintf_vma (f
, p
->p_vaddr
);
612 fprintf (f
, " paddr 0x");
613 fprintf_vma (f
, p
->p_paddr
);
614 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
615 fprintf (f
, " filesz 0x");
616 fprintf_vma (f
, p
->p_filesz
);
617 fprintf (f
, " memsz 0x");
618 fprintf_vma (f
, p
->p_memsz
);
619 fprintf (f
, " flags %c%c%c",
620 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
621 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
622 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
623 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
624 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
629 s
= bfd_get_section_by_name (abfd
, ".dynamic");
634 bfd_byte
*extdyn
, *extdynend
;
636 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
638 fprintf (f
, _("\nDynamic Section:\n"));
640 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
643 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
647 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
650 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
652 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
653 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
656 extdynend
= extdyn
+ s
->_raw_size
;
657 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
659 Elf_Internal_Dyn dyn
;
664 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
666 if (dyn
.d_tag
== DT_NULL
)
673 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
677 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
678 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
679 case DT_PLTGOT
: name
= "PLTGOT"; break;
680 case DT_HASH
: name
= "HASH"; break;
681 case DT_STRTAB
: name
= "STRTAB"; break;
682 case DT_SYMTAB
: name
= "SYMTAB"; break;
683 case DT_RELA
: name
= "RELA"; break;
684 case DT_RELASZ
: name
= "RELASZ"; break;
685 case DT_RELAENT
: name
= "RELAENT"; break;
686 case DT_STRSZ
: name
= "STRSZ"; break;
687 case DT_SYMENT
: name
= "SYMENT"; break;
688 case DT_INIT
: name
= "INIT"; break;
689 case DT_FINI
: name
= "FINI"; break;
690 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
691 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
692 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
693 case DT_REL
: name
= "REL"; break;
694 case DT_RELSZ
: name
= "RELSZ"; break;
695 case DT_RELENT
: name
= "RELENT"; break;
696 case DT_PLTREL
: name
= "PLTREL"; break;
697 case DT_DEBUG
: name
= "DEBUG"; break;
698 case DT_TEXTREL
: name
= "TEXTREL"; break;
699 case DT_JMPREL
: name
= "JMPREL"; break;
700 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
701 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
702 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
703 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
704 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
705 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
706 case DT_FLAGS
: name
= "FLAGS"; break;
707 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
708 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
709 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
710 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
711 case DT_MOVEENT
: name
= "MOVEENT"; break;
712 case DT_MOVESZ
: name
= "MOVESZ"; break;
713 case DT_FEATURE
: name
= "FEATURE"; break;
714 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
715 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
716 case DT_SYMINENT
: name
= "SYMINENT"; break;
717 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
718 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
719 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
720 case DT_PLTPAD
: name
= "PLTPAD"; break;
721 case DT_MOVETAB
: name
= "MOVETAB"; break;
722 case DT_SYMINFO
: name
= "SYMINFO"; break;
723 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
724 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
725 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
726 case DT_VERSYM
: name
= "VERSYM"; break;
727 case DT_VERDEF
: name
= "VERDEF"; break;
728 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
729 case DT_VERNEED
: name
= "VERNEED"; break;
730 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
731 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
732 case DT_USED
: name
= "USED"; break;
733 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
736 fprintf (f
, " %-11s ", name
);
738 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
743 string
= bfd_elf_string_from_elf_section (abfd
, link
,
747 fprintf (f
, "%s", string
);
756 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
757 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
759 if (! _bfd_elf_slurp_version_tables (abfd
))
763 if (elf_dynverdef (abfd
) != 0)
765 Elf_Internal_Verdef
*t
;
767 fprintf (f
, _("\nVersion definitions:\n"));
768 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
770 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
771 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
772 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
774 Elf_Internal_Verdaux
*a
;
777 for (a
= t
->vd_auxptr
->vda_nextptr
;
780 fprintf (f
, "%s ", a
->vda_nodename
);
786 if (elf_dynverref (abfd
) != 0)
788 Elf_Internal_Verneed
*t
;
790 fprintf (f
, _("\nVersion References:\n"));
791 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
793 Elf_Internal_Vernaux
*a
;
795 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
796 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
797 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
798 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
810 /* Display ELF-specific fields of a symbol. */
813 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
817 bfd_print_symbol_type how
;
819 FILE *file
= (FILE *) filep
;
822 case bfd_print_symbol_name
:
823 fprintf (file
, "%s", symbol
->name
);
825 case bfd_print_symbol_more
:
826 fprintf (file
, "elf ");
827 fprintf_vma (file
, symbol
->value
);
828 fprintf (file
, " %lx", (long) symbol
->flags
);
830 case bfd_print_symbol_all
:
832 const char *section_name
;
833 const char *name
= NULL
;
834 struct elf_backend_data
*bed
;
835 unsigned char st_other
;
837 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
839 bed
= get_elf_backend_data (abfd
);
840 if (bed
->elf_backend_print_symbol_all
)
841 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
846 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
849 fprintf (file
, " %s\t", section_name
);
850 /* Print the "other" value for a symbol. For common symbols,
851 we've already printed the size; now print the alignment.
852 For other symbols, we have no specified alignment, and
853 we've printed the address; now print the size. */
855 (bfd_is_com_section (symbol
->section
)
856 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
857 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
859 /* If we have version information, print it. */
860 if (elf_tdata (abfd
)->dynversym_section
!= 0
861 && (elf_tdata (abfd
)->dynverdef_section
!= 0
862 || elf_tdata (abfd
)->dynverref_section
!= 0))
865 const char *version_string
;
867 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
871 else if (vernum
== 1)
872 version_string
= "Base";
873 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
875 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
878 Elf_Internal_Verneed
*t
;
881 for (t
= elf_tdata (abfd
)->verref
;
885 Elf_Internal_Vernaux
*a
;
887 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
889 if (a
->vna_other
== vernum
)
891 version_string
= a
->vna_nodename
;
898 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
899 fprintf (file
, " %-11s", version_string
);
904 fprintf (file
, " (%s)", version_string
);
905 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
910 /* If the st_other field is not zero, print it. */
911 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
916 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
917 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
918 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
920 /* Some other non-defined flags are also present, so print
922 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
925 fprintf (file
, " %s", name
);
931 /* Create an entry in an ELF linker hash table. */
933 struct bfd_hash_entry
*
934 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
935 struct bfd_hash_entry
*entry
;
936 struct bfd_hash_table
*table
;
939 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
941 /* Allocate the structure if it has not already been allocated by a
943 if (ret
== (struct elf_link_hash_entry
*) NULL
)
944 ret
= ((struct elf_link_hash_entry
*)
945 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
946 if (ret
== (struct elf_link_hash_entry
*) NULL
)
947 return (struct bfd_hash_entry
*) ret
;
949 /* Call the allocation method of the superclass. */
950 ret
= ((struct elf_link_hash_entry
*)
951 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
953 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
955 /* Set local fields. */
959 ret
->dynstr_index
= 0;
961 ret
->got
.offset
= (bfd_vma
) -1;
962 ret
->plt
.offset
= (bfd_vma
) -1;
963 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
964 ret
->verinfo
.verdef
= NULL
;
965 ret
->vtable_entries_used
= NULL
;
966 ret
->vtable_entries_size
= 0;
967 ret
->vtable_parent
= NULL
;
968 ret
->type
= STT_NOTYPE
;
970 /* Assume that we have been called by a non-ELF symbol reader.
971 This flag is then reset by the code which reads an ELF input
972 file. This ensures that a symbol created by a non-ELF symbol
973 reader will have the flag set correctly. */
974 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
977 return (struct bfd_hash_entry
*) ret
;
980 /* Copy data from an indirect symbol to its direct symbol, hiding the
981 old indirect symbol. */
984 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
985 struct elf_link_hash_entry
*dir
, *ind
;
987 /* Copy down any references that we may have already seen to the
988 symbol which just became indirect. */
990 dir
->elf_link_hash_flags
|=
991 (ind
->elf_link_hash_flags
992 & (ELF_LINK_HASH_REF_DYNAMIC
993 | ELF_LINK_HASH_REF_REGULAR
994 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
995 | ELF_LINK_NON_GOT_REF
));
997 /* Copy over the global and procedure linkage table offset entries.
998 These may have been already set up by a check_relocs routine. */
999 if (dir
->got
.offset
== (bfd_vma
) -1)
1001 dir
->got
.offset
= ind
->got
.offset
;
1002 ind
->got
.offset
= (bfd_vma
) -1;
1004 BFD_ASSERT (ind
->got
.offset
== (bfd_vma
) -1);
1006 if (dir
->plt
.offset
== (bfd_vma
) -1)
1008 dir
->plt
.offset
= ind
->plt
.offset
;
1009 ind
->plt
.offset
= (bfd_vma
) -1;
1011 BFD_ASSERT (ind
->plt
.offset
== (bfd_vma
) -1);
1013 if (dir
->dynindx
== -1)
1015 dir
->dynindx
= ind
->dynindx
;
1016 dir
->dynstr_index
= ind
->dynstr_index
;
1018 ind
->dynstr_index
= 0;
1020 BFD_ASSERT (ind
->dynindx
== -1);
1024 _bfd_elf_link_hash_hide_symbol (info
, h
)
1025 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1026 struct elf_link_hash_entry
*h
;
1028 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1029 h
->plt
.offset
= (bfd_vma
) -1;
1030 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1034 /* Initialize an ELF linker hash table. */
1037 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1038 struct elf_link_hash_table
*table
;
1040 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1041 struct bfd_hash_table
*,
1044 table
->dynamic_sections_created
= false;
1045 table
->dynobj
= NULL
;
1046 /* The first dynamic symbol is a dummy. */
1047 table
->dynsymcount
= 1;
1048 table
->dynstr
= NULL
;
1049 table
->bucketcount
= 0;
1050 table
->needed
= NULL
;
1051 table
->runpath
= NULL
;
1053 table
->stab_info
= NULL
;
1054 table
->merge_info
= NULL
;
1055 table
->dynlocal
= NULL
;
1056 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1059 /* Create an ELF linker hash table. */
1061 struct bfd_link_hash_table
*
1062 _bfd_elf_link_hash_table_create (abfd
)
1065 struct elf_link_hash_table
*ret
;
1067 ret
= ((struct elf_link_hash_table
*)
1068 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
1069 if (ret
== (struct elf_link_hash_table
*) NULL
)
1072 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1074 bfd_release (abfd
, ret
);
1081 /* This is a hook for the ELF emulation code in the generic linker to
1082 tell the backend linker what file name to use for the DT_NEEDED
1083 entry for a dynamic object. The generic linker passes name as an
1084 empty string to indicate that no DT_NEEDED entry should be made. */
1087 bfd_elf_set_dt_needed_name (abfd
, name
)
1091 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1092 && bfd_get_format (abfd
) == bfd_object
)
1093 elf_dt_name (abfd
) = name
;
1097 bfd_elf_set_dt_needed_soname (abfd
, name
)
1101 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1102 && bfd_get_format (abfd
) == bfd_object
)
1103 elf_dt_soname (abfd
) = name
;
1106 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1107 the linker ELF emulation code. */
1109 struct bfd_link_needed_list
*
1110 bfd_elf_get_needed_list (abfd
, info
)
1111 bfd
*abfd ATTRIBUTE_UNUSED
;
1112 struct bfd_link_info
*info
;
1114 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1116 return elf_hash_table (info
)->needed
;
1119 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1120 hook for the linker ELF emulation code. */
1122 struct bfd_link_needed_list
*
1123 bfd_elf_get_runpath_list (abfd
, info
)
1124 bfd
*abfd ATTRIBUTE_UNUSED
;
1125 struct bfd_link_info
*info
;
1127 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1129 return elf_hash_table (info
)->runpath
;
1132 /* Get the name actually used for a dynamic object for a link. This
1133 is the SONAME entry if there is one. Otherwise, it is the string
1134 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1137 bfd_elf_get_dt_soname (abfd
)
1140 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1141 && bfd_get_format (abfd
) == bfd_object
)
1142 return elf_dt_name (abfd
);
1146 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1147 the ELF linker emulation code. */
1150 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1152 struct bfd_link_needed_list
**pneeded
;
1155 bfd_byte
*dynbuf
= NULL
;
1158 bfd_byte
*extdyn
, *extdynend
;
1160 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1164 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1165 || bfd_get_format (abfd
) != bfd_object
)
1168 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1169 if (s
== NULL
|| s
->_raw_size
== 0)
1172 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1176 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1180 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1184 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1186 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1187 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1190 extdynend
= extdyn
+ s
->_raw_size
;
1191 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1193 Elf_Internal_Dyn dyn
;
1195 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1197 if (dyn
.d_tag
== DT_NULL
)
1200 if (dyn
.d_tag
== DT_NEEDED
)
1203 struct bfd_link_needed_list
*l
;
1205 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1210 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1231 /* Allocate an ELF string table--force the first byte to be zero. */
1233 struct bfd_strtab_hash
*
1234 _bfd_elf_stringtab_init ()
1236 struct bfd_strtab_hash
*ret
;
1238 ret
= _bfd_stringtab_init ();
1243 loc
= _bfd_stringtab_add (ret
, "", true, false);
1244 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1245 if (loc
== (bfd_size_type
) -1)
1247 _bfd_stringtab_free (ret
);
1254 /* ELF .o/exec file reading */
1256 /* Create a new bfd section from an ELF section header. */
1259 bfd_section_from_shdr (abfd
, shindex
)
1261 unsigned int shindex
;
1263 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1264 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1265 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1268 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1270 switch (hdr
->sh_type
)
1273 /* Inactive section. Throw it away. */
1276 case SHT_PROGBITS
: /* Normal section with contents. */
1277 case SHT_DYNAMIC
: /* Dynamic linking information. */
1278 case SHT_NOBITS
: /* .bss section. */
1279 case SHT_HASH
: /* .hash section. */
1280 case SHT_NOTE
: /* .note section. */
1281 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1283 case SHT_SYMTAB
: /* A symbol table */
1284 if (elf_onesymtab (abfd
) == shindex
)
1287 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1288 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1289 elf_onesymtab (abfd
) = shindex
;
1290 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1291 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1292 abfd
->flags
|= HAS_SYMS
;
1294 /* Sometimes a shared object will map in the symbol table. If
1295 SHF_ALLOC is set, and this is a shared object, then we also
1296 treat this section as a BFD section. We can not base the
1297 decision purely on SHF_ALLOC, because that flag is sometimes
1298 set in a relocateable object file, which would confuse the
1300 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1301 && (abfd
->flags
& DYNAMIC
) != 0
1302 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1307 case SHT_DYNSYM
: /* A dynamic symbol table */
1308 if (elf_dynsymtab (abfd
) == shindex
)
1311 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1312 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1313 elf_dynsymtab (abfd
) = shindex
;
1314 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1315 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1316 abfd
->flags
|= HAS_SYMS
;
1318 /* Besides being a symbol table, we also treat this as a regular
1319 section, so that objcopy can handle it. */
1320 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1322 case SHT_STRTAB
: /* A string table */
1323 if (hdr
->bfd_section
!= NULL
)
1325 if (ehdr
->e_shstrndx
== shindex
)
1327 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1328 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1334 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1336 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1337 if (hdr2
->sh_link
== shindex
)
1339 if (! bfd_section_from_shdr (abfd
, i
))
1341 if (elf_onesymtab (abfd
) == i
)
1343 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1344 elf_elfsections (abfd
)[shindex
] =
1345 &elf_tdata (abfd
)->strtab_hdr
;
1348 if (elf_dynsymtab (abfd
) == i
)
1350 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1351 elf_elfsections (abfd
)[shindex
] = hdr
=
1352 &elf_tdata (abfd
)->dynstrtab_hdr
;
1353 /* We also treat this as a regular section, so
1354 that objcopy can handle it. */
1357 #if 0 /* Not handling other string tables specially right now. */
1358 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1359 /* We have a strtab for some random other section. */
1360 newsect
= (asection
*) hdr2
->bfd_section
;
1363 hdr
->bfd_section
= newsect
;
1364 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1366 elf_elfsections (abfd
)[shindex
] = hdr2
;
1372 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1376 /* *These* do a lot of work -- but build no sections! */
1378 asection
*target_sect
;
1379 Elf_Internal_Shdr
*hdr2
;
1381 /* Check for a bogus link to avoid crashing. */
1382 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1384 ((*_bfd_error_handler
)
1385 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1386 bfd_get_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1387 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1390 /* For some incomprehensible reason Oracle distributes
1391 libraries for Solaris in which some of the objects have
1392 bogus sh_link fields. It would be nice if we could just
1393 reject them, but, unfortunately, some people need to use
1394 them. We scan through the section headers; if we find only
1395 one suitable symbol table, we clobber the sh_link to point
1396 to it. I hope this doesn't break anything. */
1397 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1398 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1404 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1406 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1407 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1418 hdr
->sh_link
= found
;
1421 /* Get the symbol table. */
1422 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1423 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1426 /* If this reloc section does not use the main symbol table we
1427 don't treat it as a reloc section. BFD can't adequately
1428 represent such a section, so at least for now, we don't
1429 try. We just present it as a normal section. We also
1430 can't use it as a reloc section if it points to the null
1432 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1433 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1435 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1437 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1438 if (target_sect
== NULL
)
1441 if ((target_sect
->flags
& SEC_RELOC
) == 0
1442 || target_sect
->reloc_count
== 0)
1443 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1446 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1447 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1448 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1451 elf_elfsections (abfd
)[shindex
] = hdr2
;
1452 target_sect
->reloc_count
+= NUM_SHDR_ENTRIES (hdr
);
1453 target_sect
->flags
|= SEC_RELOC
;
1454 target_sect
->relocation
= NULL
;
1455 target_sect
->rel_filepos
= hdr
->sh_offset
;
1456 /* In the section to which the relocations apply, mark whether
1457 its relocations are of the REL or RELA variety. */
1458 if (hdr
->sh_size
!= 0)
1459 elf_section_data (target_sect
)->use_rela_p
1460 = (hdr
->sh_type
== SHT_RELA
);
1461 abfd
->flags
|= HAS_RELOC
;
1466 case SHT_GNU_verdef
:
1467 elf_dynverdef (abfd
) = shindex
;
1468 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1469 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1472 case SHT_GNU_versym
:
1473 elf_dynversym (abfd
) = shindex
;
1474 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1475 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1478 case SHT_GNU_verneed
:
1479 elf_dynverref (abfd
) = shindex
;
1480 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1481 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1488 /* Check for any processor-specific section types. */
1490 if (bed
->elf_backend_section_from_shdr
)
1491 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1499 /* Given an ELF section number, retrieve the corresponding BFD
1503 bfd_section_from_elf_index (abfd
, index
)
1507 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1508 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1510 return elf_elfsections (abfd
)[index
]->bfd_section
;
1514 _bfd_elf_new_section_hook (abfd
, sec
)
1518 struct bfd_elf_section_data
*sdata
;
1520 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, sizeof (*sdata
));
1523 sec
->used_by_bfd
= (PTR
) sdata
;
1525 /* Indicate whether or not this section should use RELA relocations. */
1527 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1532 /* Create a new bfd section from an ELF program header.
1534 Since program segments have no names, we generate a synthetic name
1535 of the form segment<NUM>, where NUM is generally the index in the
1536 program header table. For segments that are split (see below) we
1537 generate the names segment<NUM>a and segment<NUM>b.
1539 Note that some program segments may have a file size that is different than
1540 (less than) the memory size. All this means is that at execution the
1541 system must allocate the amount of memory specified by the memory size,
1542 but only initialize it with the first "file size" bytes read from the
1543 file. This would occur for example, with program segments consisting
1544 of combined data+bss.
1546 To handle the above situation, this routine generates TWO bfd sections
1547 for the single program segment. The first has the length specified by
1548 the file size of the segment, and the second has the length specified
1549 by the difference between the two sizes. In effect, the segment is split
1550 into it's initialized and uninitialized parts.
1555 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1557 Elf_Internal_Phdr
*hdr
;
1559 const char *typename
;
1566 split
= ((hdr
->p_memsz
> 0)
1567 && (hdr
->p_filesz
> 0)
1568 && (hdr
->p_memsz
> hdr
->p_filesz
));
1569 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1570 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1573 strcpy (name
, namebuf
);
1574 newsect
= bfd_make_section (abfd
, name
);
1575 if (newsect
== NULL
)
1577 newsect
->vma
= hdr
->p_vaddr
;
1578 newsect
->lma
= hdr
->p_paddr
;
1579 newsect
->_raw_size
= hdr
->p_filesz
;
1580 newsect
->filepos
= hdr
->p_offset
;
1581 newsect
->flags
|= SEC_HAS_CONTENTS
;
1582 if (hdr
->p_type
== PT_LOAD
)
1584 newsect
->flags
|= SEC_ALLOC
;
1585 newsect
->flags
|= SEC_LOAD
;
1586 if (hdr
->p_flags
& PF_X
)
1588 /* FIXME: all we known is that it has execute PERMISSION,
1590 newsect
->flags
|= SEC_CODE
;
1593 if (!(hdr
->p_flags
& PF_W
))
1595 newsect
->flags
|= SEC_READONLY
;
1600 sprintf (namebuf
, "%s%db", typename
, index
);
1601 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1604 strcpy (name
, namebuf
);
1605 newsect
= bfd_make_section (abfd
, name
);
1606 if (newsect
== NULL
)
1608 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1609 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1610 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1611 if (hdr
->p_type
== PT_LOAD
)
1613 newsect
->flags
|= SEC_ALLOC
;
1614 if (hdr
->p_flags
& PF_X
)
1615 newsect
->flags
|= SEC_CODE
;
1617 if (!(hdr
->p_flags
& PF_W
))
1618 newsect
->flags
|= SEC_READONLY
;
1625 bfd_section_from_phdr (abfd
, hdr
, index
)
1627 Elf_Internal_Phdr
*hdr
;
1630 struct elf_backend_data
*bed
;
1632 switch (hdr
->p_type
)
1635 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1638 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1641 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1644 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1647 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1649 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
1654 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1657 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1660 /* Check for any processor-specific program segment types.
1661 If no handler for them, default to making "segment" sections. */
1662 bed
= get_elf_backend_data (abfd
);
1663 if (bed
->elf_backend_section_from_phdr
)
1664 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1666 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1670 /* Initialize REL_HDR, the section-header for new section, containing
1671 relocations against ASECT. If USE_RELA_P is true, we use RELA
1672 relocations; otherwise, we use REL relocations. */
1675 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1677 Elf_Internal_Shdr
*rel_hdr
;
1682 struct elf_backend_data
*bed
;
1684 bed
= get_elf_backend_data (abfd
);
1685 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1688 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1690 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1692 if (rel_hdr
->sh_name
== (unsigned int) -1)
1694 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1695 rel_hdr
->sh_entsize
= (use_rela_p
1696 ? bed
->s
->sizeof_rela
1697 : bed
->s
->sizeof_rel
);
1698 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1699 rel_hdr
->sh_flags
= 0;
1700 rel_hdr
->sh_addr
= 0;
1701 rel_hdr
->sh_size
= 0;
1702 rel_hdr
->sh_offset
= 0;
1707 /* Set up an ELF internal section header for a section. */
1710 elf_fake_sections (abfd
, asect
, failedptrarg
)
1715 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1716 boolean
*failedptr
= (boolean
*) failedptrarg
;
1717 Elf_Internal_Shdr
*this_hdr
;
1721 /* We already failed; just get out of the bfd_map_over_sections
1726 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1728 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1731 if (this_hdr
->sh_name
== (unsigned long) -1)
1737 this_hdr
->sh_flags
= 0;
1739 if ((asect
->flags
& SEC_ALLOC
) != 0
1740 || asect
->user_set_vma
)
1741 this_hdr
->sh_addr
= asect
->vma
;
1743 this_hdr
->sh_addr
= 0;
1745 this_hdr
->sh_offset
= 0;
1746 this_hdr
->sh_size
= asect
->_raw_size
;
1747 this_hdr
->sh_link
= 0;
1748 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1749 /* The sh_entsize and sh_info fields may have been set already by
1750 copy_private_section_data. */
1752 this_hdr
->bfd_section
= asect
;
1753 this_hdr
->contents
= NULL
;
1755 /* FIXME: This should not be based on section names. */
1756 if (strcmp (asect
->name
, ".dynstr") == 0)
1757 this_hdr
->sh_type
= SHT_STRTAB
;
1758 else if (strcmp (asect
->name
, ".hash") == 0)
1760 this_hdr
->sh_type
= SHT_HASH
;
1761 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1763 else if (strcmp (asect
->name
, ".dynsym") == 0)
1765 this_hdr
->sh_type
= SHT_DYNSYM
;
1766 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1768 else if (strcmp (asect
->name
, ".dynamic") == 0)
1770 this_hdr
->sh_type
= SHT_DYNAMIC
;
1771 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1773 else if (strncmp (asect
->name
, ".rela", 5) == 0
1774 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1776 this_hdr
->sh_type
= SHT_RELA
;
1777 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1779 else if (strncmp (asect
->name
, ".rel", 4) == 0
1780 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1782 this_hdr
->sh_type
= SHT_REL
;
1783 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1785 else if (strncmp (asect
->name
, ".note", 5) == 0)
1786 this_hdr
->sh_type
= SHT_NOTE
;
1787 else if (strncmp (asect
->name
, ".stab", 5) == 0
1788 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1789 this_hdr
->sh_type
= SHT_STRTAB
;
1790 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1792 this_hdr
->sh_type
= SHT_GNU_versym
;
1793 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1795 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1797 this_hdr
->sh_type
= SHT_GNU_verdef
;
1798 this_hdr
->sh_entsize
= 0;
1799 /* objcopy or strip will copy over sh_info, but may not set
1800 cverdefs. The linker will set cverdefs, but sh_info will be
1802 if (this_hdr
->sh_info
== 0)
1803 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1805 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1806 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1808 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1810 this_hdr
->sh_type
= SHT_GNU_verneed
;
1811 this_hdr
->sh_entsize
= 0;
1812 /* objcopy or strip will copy over sh_info, but may not set
1813 cverrefs. The linker will set cverrefs, but sh_info will be
1815 if (this_hdr
->sh_info
== 0)
1816 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1818 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1819 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1821 else if ((asect
->flags
& SEC_ALLOC
) != 0
1822 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
1823 this_hdr
->sh_type
= SHT_NOBITS
;
1825 this_hdr
->sh_type
= SHT_PROGBITS
;
1827 if ((asect
->flags
& SEC_ALLOC
) != 0)
1828 this_hdr
->sh_flags
|= SHF_ALLOC
;
1829 if ((asect
->flags
& SEC_READONLY
) == 0)
1830 this_hdr
->sh_flags
|= SHF_WRITE
;
1831 if ((asect
->flags
& SEC_CODE
) != 0)
1832 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1833 if ((asect
->flags
& SEC_MERGE
) != 0)
1835 this_hdr
->sh_flags
|= SHF_MERGE
;
1836 this_hdr
->sh_entsize
= asect
->entsize
;
1837 if ((asect
->flags
& SEC_STRINGS
) != 0)
1838 this_hdr
->sh_flags
|= SHF_STRINGS
;
1841 /* Check for processor-specific section types. */
1842 if (bed
->elf_backend_fake_sections
)
1843 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1845 /* If the section has relocs, set up a section header for the
1846 SHT_REL[A] section. If two relocation sections are required for
1847 this section, it is up to the processor-specific back-end to
1848 create the other. */
1849 if ((asect
->flags
& SEC_RELOC
) != 0
1850 && !_bfd_elf_init_reloc_shdr (abfd
,
1851 &elf_section_data (asect
)->rel_hdr
,
1853 elf_section_data (asect
)->use_rela_p
))
1857 /* Assign all ELF section numbers. The dummy first section is handled here
1858 too. The link/info pointers for the standard section types are filled
1859 in here too, while we're at it. */
1862 assign_section_numbers (abfd
)
1865 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1867 unsigned int section_number
;
1868 Elf_Internal_Shdr
**i_shdrp
;
1872 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1874 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1876 d
->this_idx
= section_number
++;
1877 if ((sec
->flags
& SEC_RELOC
) == 0)
1880 d
->rel_idx
= section_number
++;
1883 d
->rel_idx2
= section_number
++;
1888 t
->shstrtab_section
= section_number
++;
1889 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1890 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1892 if (bfd_get_symcount (abfd
) > 0)
1894 t
->symtab_section
= section_number
++;
1895 t
->strtab_section
= section_number
++;
1898 elf_elfheader (abfd
)->e_shnum
= section_number
;
1900 /* Set up the list of section header pointers, in agreement with the
1902 i_shdrp
= ((Elf_Internal_Shdr
**)
1903 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1904 if (i_shdrp
== NULL
)
1907 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1908 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1909 if (i_shdrp
[0] == NULL
)
1911 bfd_release (abfd
, i_shdrp
);
1914 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1916 elf_elfsections (abfd
) = i_shdrp
;
1918 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1919 if (bfd_get_symcount (abfd
) > 0)
1921 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1922 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1923 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1925 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1927 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1931 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1932 if (d
->rel_idx
!= 0)
1933 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1934 if (d
->rel_idx2
!= 0)
1935 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1937 /* Fill in the sh_link and sh_info fields while we're at it. */
1939 /* sh_link of a reloc section is the section index of the symbol
1940 table. sh_info is the section index of the section to which
1941 the relocation entries apply. */
1942 if (d
->rel_idx
!= 0)
1944 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1945 d
->rel_hdr
.sh_info
= d
->this_idx
;
1947 if (d
->rel_idx2
!= 0)
1949 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1950 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1953 switch (d
->this_hdr
.sh_type
)
1957 /* A reloc section which we are treating as a normal BFD
1958 section. sh_link is the section index of the symbol
1959 table. sh_info is the section index of the section to
1960 which the relocation entries apply. We assume that an
1961 allocated reloc section uses the dynamic symbol table.
1962 FIXME: How can we be sure? */
1963 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1965 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1967 /* We look up the section the relocs apply to by name. */
1969 if (d
->this_hdr
.sh_type
== SHT_REL
)
1973 s
= bfd_get_section_by_name (abfd
, name
);
1975 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1979 /* We assume that a section named .stab*str is a stabs
1980 string section. We look for a section with the same name
1981 but without the trailing ``str'', and set its sh_link
1982 field to point to this section. */
1983 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1984 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1989 len
= strlen (sec
->name
);
1990 alc
= (char *) bfd_malloc (len
- 2);
1993 strncpy (alc
, sec
->name
, len
- 3);
1994 alc
[len
- 3] = '\0';
1995 s
= bfd_get_section_by_name (abfd
, alc
);
1999 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
2001 /* This is a .stab section. */
2002 elf_section_data (s
)->this_hdr
.sh_entsize
=
2003 4 + 2 * bfd_get_arch_size (abfd
) / 8;
2010 case SHT_GNU_verneed
:
2011 case SHT_GNU_verdef
:
2012 /* sh_link is the section header index of the string table
2013 used for the dynamic entries, or the symbol table, or the
2015 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2017 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2021 case SHT_GNU_versym
:
2022 /* sh_link is the section header index of the symbol table
2023 this hash table or version table is for. */
2024 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2026 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2034 /* Map symbol from it's internal number to the external number, moving
2035 all local symbols to be at the head of the list. */
2038 sym_is_global (abfd
, sym
)
2042 /* If the backend has a special mapping, use it. */
2043 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2044 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2047 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2048 || bfd_is_und_section (bfd_get_section (sym
))
2049 || bfd_is_com_section (bfd_get_section (sym
)));
2053 elf_map_symbols (abfd
)
2056 int symcount
= bfd_get_symcount (abfd
);
2057 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2058 asymbol
**sect_syms
;
2060 int num_globals
= 0;
2061 int num_locals2
= 0;
2062 int num_globals2
= 0;
2064 int num_sections
= 0;
2071 fprintf (stderr
, "elf_map_symbols\n");
2075 /* Add a section symbol for each BFD section. FIXME: Is this really
2077 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2079 if (max_index
< asect
->index
)
2080 max_index
= asect
->index
;
2084 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
2085 if (sect_syms
== NULL
)
2087 elf_section_syms (abfd
) = sect_syms
;
2089 for (idx
= 0; idx
< symcount
; idx
++)
2093 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2100 if (sec
->owner
!= NULL
)
2102 if (sec
->owner
!= abfd
)
2104 if (sec
->output_offset
!= 0)
2107 sec
= sec
->output_section
;
2109 /* Empty sections in the input files may have had a section
2110 symbol created for them. (See the comment near the end of
2111 _bfd_generic_link_output_symbols in linker.c). If the linker
2112 script discards such sections then we will reach this point.
2113 Since we know that we cannot avoid this case, we detect it
2114 and skip the abort and the assignment to the sect_syms array.
2115 To reproduce this particular case try running the linker
2116 testsuite test ld-scripts/weak.exp for an ELF port that uses
2117 the generic linker. */
2118 if (sec
->owner
== NULL
)
2121 BFD_ASSERT (sec
->owner
== abfd
);
2123 sect_syms
[sec
->index
] = syms
[idx
];
2128 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2130 if (sect_syms
[asect
->index
] != NULL
)
2133 sym
= bfd_make_empty_symbol (abfd
);
2136 sym
->the_bfd
= abfd
;
2137 sym
->name
= asect
->name
;
2139 /* Set the flags to 0 to indicate that this one was newly added. */
2141 sym
->section
= asect
;
2142 sect_syms
[asect
->index
] = sym
;
2146 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2147 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2151 /* Classify all of the symbols. */
2152 for (idx
= 0; idx
< symcount
; idx
++)
2154 if (!sym_is_global (abfd
, syms
[idx
]))
2159 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2161 if (sect_syms
[asect
->index
] != NULL
2162 && sect_syms
[asect
->index
]->flags
== 0)
2164 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2165 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2169 sect_syms
[asect
->index
]->flags
= 0;
2173 /* Now sort the symbols so the local symbols are first. */
2174 new_syms
= ((asymbol
**)
2176 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
2177 if (new_syms
== NULL
)
2180 for (idx
= 0; idx
< symcount
; idx
++)
2182 asymbol
*sym
= syms
[idx
];
2185 if (!sym_is_global (abfd
, sym
))
2188 i
= num_locals
+ num_globals2
++;
2190 sym
->udata
.i
= i
+ 1;
2192 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2194 if (sect_syms
[asect
->index
] != NULL
2195 && sect_syms
[asect
->index
]->flags
== 0)
2197 asymbol
*sym
= sect_syms
[asect
->index
];
2200 sym
->flags
= BSF_SECTION_SYM
;
2201 if (!sym_is_global (abfd
, sym
))
2204 i
= num_locals
+ num_globals2
++;
2206 sym
->udata
.i
= i
+ 1;
2210 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2212 elf_num_locals (abfd
) = num_locals
;
2213 elf_num_globals (abfd
) = num_globals
;
2217 /* Align to the maximum file alignment that could be required for any
2218 ELF data structure. */
2220 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2221 static INLINE file_ptr
2222 align_file_position (off
, align
)
2226 return (off
+ align
- 1) & ~(align
- 1);
2229 /* Assign a file position to a section, optionally aligning to the
2230 required section alignment. */
2233 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2234 Elf_Internal_Shdr
*i_shdrp
;
2242 al
= i_shdrp
->sh_addralign
;
2244 offset
= BFD_ALIGN (offset
, al
);
2246 i_shdrp
->sh_offset
= offset
;
2247 if (i_shdrp
->bfd_section
!= NULL
)
2248 i_shdrp
->bfd_section
->filepos
= offset
;
2249 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2250 offset
+= i_shdrp
->sh_size
;
2254 /* Compute the file positions we are going to put the sections at, and
2255 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2256 is not NULL, this is being called by the ELF backend linker. */
2259 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2261 struct bfd_link_info
*link_info
;
2263 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2265 struct bfd_strtab_hash
*strtab
;
2266 Elf_Internal_Shdr
*shstrtab_hdr
;
2268 if (abfd
->output_has_begun
)
2271 /* Do any elf backend specific processing first. */
2272 if (bed
->elf_backend_begin_write_processing
)
2273 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2275 if (! prep_headers (abfd
))
2278 /* Post process the headers if necessary. */
2279 if (bed
->elf_backend_post_process_headers
)
2280 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2283 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2287 if (!assign_section_numbers (abfd
))
2290 /* The backend linker builds symbol table information itself. */
2291 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2293 /* Non-zero if doing a relocatable link. */
2294 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2296 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2300 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2301 /* sh_name was set in prep_headers. */
2302 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2303 shstrtab_hdr
->sh_flags
= 0;
2304 shstrtab_hdr
->sh_addr
= 0;
2305 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2306 shstrtab_hdr
->sh_entsize
= 0;
2307 shstrtab_hdr
->sh_link
= 0;
2308 shstrtab_hdr
->sh_info
= 0;
2309 /* sh_offset is set in assign_file_positions_except_relocs. */
2310 shstrtab_hdr
->sh_addralign
= 1;
2312 if (!assign_file_positions_except_relocs (abfd
))
2315 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2318 Elf_Internal_Shdr
*hdr
;
2320 off
= elf_tdata (abfd
)->next_file_pos
;
2322 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2323 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2325 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2326 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2328 elf_tdata (abfd
)->next_file_pos
= off
;
2330 /* Now that we know where the .strtab section goes, write it
2332 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2333 || ! _bfd_stringtab_emit (abfd
, strtab
))
2335 _bfd_stringtab_free (strtab
);
2338 abfd
->output_has_begun
= true;
2343 /* Create a mapping from a set of sections to a program segment. */
2345 static INLINE
struct elf_segment_map
*
2346 make_mapping (abfd
, sections
, from
, to
, phdr
)
2348 asection
**sections
;
2353 struct elf_segment_map
*m
;
2357 m
= ((struct elf_segment_map
*)
2359 (sizeof (struct elf_segment_map
)
2360 + (to
- from
- 1) * sizeof (asection
*))));
2364 m
->p_type
= PT_LOAD
;
2365 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2366 m
->sections
[i
- from
] = *hdrpp
;
2367 m
->count
= to
- from
;
2369 if (from
== 0 && phdr
)
2371 /* Include the headers in the first PT_LOAD segment. */
2372 m
->includes_filehdr
= 1;
2373 m
->includes_phdrs
= 1;
2379 /* Set up a mapping from BFD sections to program segments. */
2382 map_sections_to_segments (abfd
)
2385 asection
**sections
= NULL
;
2389 struct elf_segment_map
*mfirst
;
2390 struct elf_segment_map
**pm
;
2391 struct elf_segment_map
*m
;
2393 unsigned int phdr_index
;
2394 bfd_vma maxpagesize
;
2396 boolean phdr_in_segment
= true;
2400 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2403 if (bfd_count_sections (abfd
) == 0)
2406 /* Select the allocated sections, and sort them. */
2408 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2409 * sizeof (asection
*));
2410 if (sections
== NULL
)
2414 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2416 if ((s
->flags
& SEC_ALLOC
) != 0)
2422 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2425 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2427 /* Build the mapping. */
2432 /* If we have a .interp section, then create a PT_PHDR segment for
2433 the program headers and a PT_INTERP segment for the .interp
2435 s
= bfd_get_section_by_name (abfd
, ".interp");
2436 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2438 m
= ((struct elf_segment_map
*)
2439 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2443 m
->p_type
= PT_PHDR
;
2444 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2445 m
->p_flags
= PF_R
| PF_X
;
2446 m
->p_flags_valid
= 1;
2447 m
->includes_phdrs
= 1;
2452 m
= ((struct elf_segment_map
*)
2453 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2457 m
->p_type
= PT_INTERP
;
2465 /* Look through the sections. We put sections in the same program
2466 segment when the start of the second section can be placed within
2467 a few bytes of the end of the first section. */
2470 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2472 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2474 && (dynsec
->flags
& SEC_LOAD
) == 0)
2477 /* Deal with -Ttext or something similar such that the first section
2478 is not adjacent to the program headers. This is an
2479 approximation, since at this point we don't know exactly how many
2480 program headers we will need. */
2483 bfd_size_type phdr_size
;
2485 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2487 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2488 if ((abfd
->flags
& D_PAGED
) == 0
2489 || sections
[0]->lma
< phdr_size
2490 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2491 phdr_in_segment
= false;
2494 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2497 boolean new_segment
;
2501 /* See if this section and the last one will fit in the same
2504 if (last_hdr
== NULL
)
2506 /* If we don't have a segment yet, then we don't need a new
2507 one (we build the last one after this loop). */
2508 new_segment
= false;
2510 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2512 /* If this section has a different relation between the
2513 virtual address and the load address, then we need a new
2517 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2518 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2520 /* If putting this section in this segment would force us to
2521 skip a page in the segment, then we need a new segment. */
2524 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2525 && (hdr
->flags
& SEC_LOAD
) != 0)
2527 /* We don't want to put a loadable section after a
2528 nonloadable section in the same segment. */
2531 else if ((abfd
->flags
& D_PAGED
) == 0)
2533 /* If the file is not demand paged, which means that we
2534 don't require the sections to be correctly aligned in the
2535 file, then there is no other reason for a new segment. */
2536 new_segment
= false;
2539 && (hdr
->flags
& SEC_READONLY
) == 0
2540 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2543 /* We don't want to put a writable section in a read only
2544 segment, unless they are on the same page in memory
2545 anyhow. We already know that the last section does not
2546 bring us past the current section on the page, so the
2547 only case in which the new section is not on the same
2548 page as the previous section is when the previous section
2549 ends precisely on a page boundary. */
2554 /* Otherwise, we can use the same segment. */
2555 new_segment
= false;
2560 if ((hdr
->flags
& SEC_READONLY
) == 0)
2566 /* We need a new program segment. We must create a new program
2567 header holding all the sections from phdr_index until hdr. */
2569 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2576 if ((hdr
->flags
& SEC_READONLY
) == 0)
2583 phdr_in_segment
= false;
2586 /* Create a final PT_LOAD program segment. */
2587 if (last_hdr
!= NULL
)
2589 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2597 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2600 m
= ((struct elf_segment_map
*)
2601 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2605 m
->p_type
= PT_DYNAMIC
;
2607 m
->sections
[0] = dynsec
;
2613 /* For each loadable .note section, add a PT_NOTE segment. We don't
2614 use bfd_get_section_by_name, because if we link together
2615 nonloadable .note sections and loadable .note sections, we will
2616 generate two .note sections in the output file. FIXME: Using
2617 names for section types is bogus anyhow. */
2618 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2620 if ((s
->flags
& SEC_LOAD
) != 0
2621 && strncmp (s
->name
, ".note", 5) == 0)
2623 m
= ((struct elf_segment_map
*)
2624 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2628 m
->p_type
= PT_NOTE
;
2640 elf_tdata (abfd
)->segment_map
= mfirst
;
2644 if (sections
!= NULL
)
2649 /* Sort sections by address. */
2652 elf_sort_sections (arg1
, arg2
)
2656 const asection
*sec1
= *(const asection
**) arg1
;
2657 const asection
*sec2
= *(const asection
**) arg2
;
2659 /* Sort by LMA first, since this is the address used to
2660 place the section into a segment. */
2661 if (sec1
->lma
< sec2
->lma
)
2663 else if (sec1
->lma
> sec2
->lma
)
2666 /* Then sort by VMA. Normally the LMA and the VMA will be
2667 the same, and this will do nothing. */
2668 if (sec1
->vma
< sec2
->vma
)
2670 else if (sec1
->vma
> sec2
->vma
)
2673 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2675 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2680 return sec1
->target_index
- sec2
->target_index
;
2690 /* Sort by size, to put zero sized sections before others at the
2693 if (sec1
->_raw_size
< sec2
->_raw_size
)
2695 if (sec1
->_raw_size
> sec2
->_raw_size
)
2698 return sec1
->target_index
- sec2
->target_index
;
2701 /* Assign file positions to the sections based on the mapping from
2702 sections to segments. This function also sets up some fields in
2703 the file header, and writes out the program headers. */
2706 assign_file_positions_for_segments (abfd
)
2709 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2711 struct elf_segment_map
*m
;
2713 Elf_Internal_Phdr
*phdrs
;
2715 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2716 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2717 Elf_Internal_Phdr
*p
;
2719 if (elf_tdata (abfd
)->segment_map
== NULL
)
2721 if (! map_sections_to_segments (abfd
))
2725 if (bed
->elf_backend_modify_segment_map
)
2727 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2732 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2735 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2736 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2737 elf_elfheader (abfd
)->e_phnum
= count
;
2742 /* If we already counted the number of program segments, make sure
2743 that we allocated enough space. This happens when SIZEOF_HEADERS
2744 is used in a linker script. */
2745 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2746 if (alloc
!= 0 && count
> alloc
)
2748 ((*_bfd_error_handler
)
2749 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2750 bfd_get_filename (abfd
), alloc
, count
));
2751 bfd_set_error (bfd_error_bad_value
);
2758 phdrs
= ((Elf_Internal_Phdr
*)
2759 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2763 off
= bed
->s
->sizeof_ehdr
;
2764 off
+= alloc
* bed
->s
->sizeof_phdr
;
2771 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2778 /* If elf_segment_map is not from map_sections_to_segments, the
2779 sections may not be correctly ordered. */
2781 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2784 p
->p_type
= m
->p_type
;
2785 p
->p_flags
= m
->p_flags
;
2787 if (p
->p_type
== PT_LOAD
2789 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2791 if ((abfd
->flags
& D_PAGED
) != 0)
2792 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2795 bfd_size_type align
;
2798 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2800 bfd_size_type secalign
;
2802 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2803 if (secalign
> align
)
2807 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2814 p
->p_vaddr
= m
->sections
[0]->vma
;
2816 if (m
->p_paddr_valid
)
2817 p
->p_paddr
= m
->p_paddr
;
2818 else if (m
->count
== 0)
2821 p
->p_paddr
= m
->sections
[0]->lma
;
2823 if (p
->p_type
== PT_LOAD
2824 && (abfd
->flags
& D_PAGED
) != 0)
2825 p
->p_align
= bed
->maxpagesize
;
2826 else if (m
->count
== 0)
2827 p
->p_align
= bed
->s
->file_align
;
2835 if (m
->includes_filehdr
)
2837 if (! m
->p_flags_valid
)
2840 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2841 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2844 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2846 if (p
->p_vaddr
< (bfd_vma
) off
)
2848 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2849 bfd_get_filename (abfd
));
2850 bfd_set_error (bfd_error_bad_value
);
2855 if (! m
->p_paddr_valid
)
2858 if (p
->p_type
== PT_LOAD
)
2860 filehdr_vaddr
= p
->p_vaddr
;
2861 filehdr_paddr
= p
->p_paddr
;
2865 if (m
->includes_phdrs
)
2867 if (! m
->p_flags_valid
)
2870 if (m
->includes_filehdr
)
2872 if (p
->p_type
== PT_LOAD
)
2874 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2875 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2880 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2884 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2885 p
->p_vaddr
-= off
- p
->p_offset
;
2886 if (! m
->p_paddr_valid
)
2887 p
->p_paddr
-= off
- p
->p_offset
;
2890 if (p
->p_type
== PT_LOAD
)
2892 phdrs_vaddr
= p
->p_vaddr
;
2893 phdrs_paddr
= p
->p_paddr
;
2896 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2899 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2900 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2903 if (p
->p_type
== PT_LOAD
2904 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2906 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2912 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2913 p
->p_filesz
+= adjust
;
2914 p
->p_memsz
+= adjust
;
2920 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2924 bfd_size_type align
;
2928 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2930 /* The section may have artificial alignment forced by a
2931 link script. Notice this case by the gap between the
2932 cumulative phdr vma and the section's vma. */
2933 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2935 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2937 p
->p_memsz
+= adjust
;
2940 if ((flags
& SEC_LOAD
) != 0)
2941 p
->p_filesz
+= adjust
;
2944 if (p
->p_type
== PT_LOAD
)
2946 bfd_signed_vma adjust
;
2948 if ((flags
& SEC_LOAD
) != 0)
2950 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2954 else if ((flags
& SEC_ALLOC
) != 0)
2956 /* The section VMA must equal the file position
2957 modulo the page size. FIXME: I'm not sure if
2958 this adjustment is really necessary. We used to
2959 not have the SEC_LOAD case just above, and then
2960 this was necessary, but now I'm not sure. */
2961 if ((abfd
->flags
& D_PAGED
) != 0)
2962 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2964 adjust
= (sec
->vma
- voff
) % align
;
2973 (* _bfd_error_handler
)
2974 (_("Error: First section in segment (%s) starts at 0x%x"),
2975 bfd_section_name (abfd
, sec
), sec
->lma
);
2976 (* _bfd_error_handler
)
2977 (_(" whereas segment starts at 0x%x"),
2982 p
->p_memsz
+= adjust
;
2985 if ((flags
& SEC_LOAD
) != 0)
2986 p
->p_filesz
+= adjust
;
2991 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2992 used in a linker script we may have a section with
2993 SEC_LOAD clear but which is supposed to have
2995 if ((flags
& SEC_LOAD
) != 0
2996 || (flags
& SEC_HAS_CONTENTS
) != 0)
2997 off
+= sec
->_raw_size
;
2999 if ((flags
& SEC_ALLOC
) != 0)
3000 voff
+= sec
->_raw_size
;
3003 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
3005 /* The actual "note" segment has i == 0.
3006 This is the one that actually contains everything. */
3010 p
->p_filesz
= sec
->_raw_size
;
3011 off
+= sec
->_raw_size
;
3016 /* Fake sections -- don't need to be written. */
3019 flags
= sec
->flags
= 0;
3026 p
->p_memsz
+= sec
->_raw_size
;
3028 if ((flags
& SEC_LOAD
) != 0)
3029 p
->p_filesz
+= sec
->_raw_size
;
3031 if (align
> p
->p_align
3032 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3036 if (! m
->p_flags_valid
)
3039 if ((flags
& SEC_CODE
) != 0)
3041 if ((flags
& SEC_READONLY
) == 0)
3047 /* Now that we have set the section file positions, we can set up
3048 the file positions for the non PT_LOAD segments. */
3049 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3053 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3055 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3056 p
->p_offset
= m
->sections
[0]->filepos
;
3060 if (m
->includes_filehdr
)
3062 p
->p_vaddr
= filehdr_vaddr
;
3063 if (! m
->p_paddr_valid
)
3064 p
->p_paddr
= filehdr_paddr
;
3066 else if (m
->includes_phdrs
)
3068 p
->p_vaddr
= phdrs_vaddr
;
3069 if (! m
->p_paddr_valid
)
3070 p
->p_paddr
= phdrs_paddr
;
3075 /* Clear out any program headers we allocated but did not use. */
3076 for (; count
< alloc
; count
++, p
++)
3078 memset (p
, 0, sizeof *p
);
3079 p
->p_type
= PT_NULL
;
3082 elf_tdata (abfd
)->phdr
= phdrs
;
3084 elf_tdata (abfd
)->next_file_pos
= off
;
3086 /* Write out the program headers. */
3087 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3088 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3094 /* Get the size of the program header.
3096 If this is called by the linker before any of the section VMA's are set, it
3097 can't calculate the correct value for a strange memory layout. This only
3098 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3099 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3100 data segment (exclusive of .interp and .dynamic).
3102 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3103 will be two segments. */
3105 static bfd_size_type
3106 get_program_header_size (abfd
)
3111 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3113 /* We can't return a different result each time we're called. */
3114 if (elf_tdata (abfd
)->program_header_size
!= 0)
3115 return elf_tdata (abfd
)->program_header_size
;
3117 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3119 struct elf_segment_map
*m
;
3122 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3124 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3125 return elf_tdata (abfd
)->program_header_size
;
3128 /* Assume we will need exactly two PT_LOAD segments: one for text
3129 and one for data. */
3132 s
= bfd_get_section_by_name (abfd
, ".interp");
3133 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3135 /* If we have a loadable interpreter section, we need a
3136 PT_INTERP segment. In this case, assume we also need a
3137 PT_PHDR segment, although that may not be true for all
3142 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3144 /* We need a PT_DYNAMIC segment. */
3148 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3150 if ((s
->flags
& SEC_LOAD
) != 0
3151 && strncmp (s
->name
, ".note", 5) == 0)
3153 /* We need a PT_NOTE segment. */
3158 /* Let the backend count up any program headers it might need. */
3159 if (bed
->elf_backend_additional_program_headers
)
3163 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3169 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3170 return elf_tdata (abfd
)->program_header_size
;
3173 /* Work out the file positions of all the sections. This is called by
3174 _bfd_elf_compute_section_file_positions. All the section sizes and
3175 VMAs must be known before this is called.
3177 We do not consider reloc sections at this point, unless they form
3178 part of the loadable image. Reloc sections are assigned file
3179 positions in assign_file_positions_for_relocs, which is called by
3180 write_object_contents and final_link.
3182 We also don't set the positions of the .symtab and .strtab here. */
3185 assign_file_positions_except_relocs (abfd
)
3188 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3189 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3190 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3192 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3194 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3195 && bfd_get_format (abfd
) != bfd_core
)
3197 Elf_Internal_Shdr
**hdrpp
;
3200 /* Start after the ELF header. */
3201 off
= i_ehdrp
->e_ehsize
;
3203 /* We are not creating an executable, which means that we are
3204 not creating a program header, and that the actual order of
3205 the sections in the file is unimportant. */
3206 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3208 Elf_Internal_Shdr
*hdr
;
3211 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3213 hdr
->sh_offset
= -1;
3216 if (i
== tdata
->symtab_section
3217 || i
== tdata
->strtab_section
)
3219 hdr
->sh_offset
= -1;
3223 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3229 Elf_Internal_Shdr
**hdrpp
;
3231 /* Assign file positions for the loaded sections based on the
3232 assignment of sections to segments. */
3233 if (! assign_file_positions_for_segments (abfd
))
3236 /* Assign file positions for the other sections. */
3238 off
= elf_tdata (abfd
)->next_file_pos
;
3239 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3241 Elf_Internal_Shdr
*hdr
;
3244 if (hdr
->bfd_section
!= NULL
3245 && hdr
->bfd_section
->filepos
!= 0)
3246 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3247 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3249 ((*_bfd_error_handler
)
3250 (_("%s: warning: allocated section `%s' not in segment"),
3251 bfd_get_filename (abfd
),
3252 (hdr
->bfd_section
== NULL
3254 : hdr
->bfd_section
->name
)));
3255 if ((abfd
->flags
& D_PAGED
) != 0)
3256 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3258 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3259 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3262 else if (hdr
->sh_type
== SHT_REL
3263 || hdr
->sh_type
== SHT_RELA
3264 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3265 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3266 hdr
->sh_offset
= -1;
3268 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3272 /* Place the section headers. */
3273 off
= align_file_position (off
, bed
->s
->file_align
);
3274 i_ehdrp
->e_shoff
= off
;
3275 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3277 elf_tdata (abfd
)->next_file_pos
= off
;
3286 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3287 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3288 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3290 struct bfd_strtab_hash
*shstrtab
;
3291 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3293 i_ehdrp
= elf_elfheader (abfd
);
3294 i_shdrp
= elf_elfsections (abfd
);
3296 shstrtab
= _bfd_elf_stringtab_init ();
3297 if (shstrtab
== NULL
)
3300 elf_shstrtab (abfd
) = shstrtab
;
3302 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3303 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3304 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3305 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3307 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3308 i_ehdrp
->e_ident
[EI_DATA
] =
3309 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3310 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3312 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3313 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3315 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3316 i_ehdrp
->e_ident
[count
] = 0;
3318 if ((abfd
->flags
& DYNAMIC
) != 0)
3319 i_ehdrp
->e_type
= ET_DYN
;
3320 else if ((abfd
->flags
& EXEC_P
) != 0)
3321 i_ehdrp
->e_type
= ET_EXEC
;
3322 else if (bfd_get_format (abfd
) == bfd_core
)
3323 i_ehdrp
->e_type
= ET_CORE
;
3325 i_ehdrp
->e_type
= ET_REL
;
3327 switch (bfd_get_arch (abfd
))
3329 case bfd_arch_unknown
:
3330 i_ehdrp
->e_machine
= EM_NONE
;
3332 case bfd_arch_sparc
:
3333 if (bfd_get_arch_size (abfd
) == 64)
3334 i_ehdrp
->e_machine
= EM_SPARCV9
;
3336 i_ehdrp
->e_machine
= EM_SPARC
;
3339 i_ehdrp
->e_machine
= EM_S370
;
3342 if (bfd_get_arch_size (abfd
) == 64)
3343 i_ehdrp
->e_machine
= EM_X86_64
;
3345 i_ehdrp
->e_machine
= EM_386
;
3348 i_ehdrp
->e_machine
= EM_IA_64
;
3350 case bfd_arch_m68hc11
:
3351 i_ehdrp
->e_machine
= EM_68HC11
;
3353 case bfd_arch_m68hc12
:
3354 i_ehdrp
->e_machine
= EM_68HC12
;
3357 i_ehdrp
->e_machine
= EM_S390
;
3360 i_ehdrp
->e_machine
= EM_68K
;
3363 i_ehdrp
->e_machine
= EM_88K
;
3366 i_ehdrp
->e_machine
= EM_860
;
3369 i_ehdrp
->e_machine
= EM_960
;
3371 case bfd_arch_mips
: /* MIPS Rxxxx */
3372 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3375 i_ehdrp
->e_machine
= EM_PARISC
;
3377 case bfd_arch_powerpc
:
3378 i_ehdrp
->e_machine
= EM_PPC
;
3380 case bfd_arch_alpha
:
3381 i_ehdrp
->e_machine
= EM_ALPHA
;
3384 i_ehdrp
->e_machine
= EM_SH
;
3387 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3390 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3393 i_ehdrp
->e_machine
= EM_CYGNUS_FR30
;
3395 case bfd_arch_mcore
:
3396 i_ehdrp
->e_machine
= EM_MCORE
;
3399 i_ehdrp
->e_machine
= EM_AVR
;
3402 switch (bfd_get_mach (abfd
))
3405 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3409 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3412 i_ehdrp
->e_machine
= EM_ARM
;
3415 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3417 case bfd_arch_mn10200
:
3418 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3420 case bfd_arch_mn10300
:
3421 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3424 i_ehdrp
->e_machine
= EM_PJ
;
3427 i_ehdrp
->e_machine
= EM_CRIS
;
3429 case bfd_arch_openrisc
:
3430 i_ehdrp
->e_machine
= EM_OPENRISC
;
3432 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3434 i_ehdrp
->e_machine
= EM_NONE
;
3436 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3437 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3439 /* No program header, for now. */
3440 i_ehdrp
->e_phoff
= 0;
3441 i_ehdrp
->e_phentsize
= 0;
3442 i_ehdrp
->e_phnum
= 0;
3444 /* Each bfd section is section header entry. */
3445 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3446 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3448 /* If we're building an executable, we'll need a program header table. */
3449 if (abfd
->flags
& EXEC_P
)
3451 /* It all happens later. */
3453 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3455 /* elf_build_phdrs() returns a (NULL-terminated) array of
3456 Elf_Internal_Phdrs. */
3457 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3458 i_ehdrp
->e_phoff
= outbase
;
3459 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3464 i_ehdrp
->e_phentsize
= 0;
3466 i_ehdrp
->e_phoff
= 0;
3469 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3470 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3471 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3472 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3473 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3474 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3475 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3476 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3477 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3483 /* Assign file positions for all the reloc sections which are not part
3484 of the loadable file image. */
3487 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3492 Elf_Internal_Shdr
**shdrpp
;
3494 off
= elf_tdata (abfd
)->next_file_pos
;
3496 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3497 i
< elf_elfheader (abfd
)->e_shnum
;
3500 Elf_Internal_Shdr
*shdrp
;
3503 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3504 && shdrp
->sh_offset
== -1)
3505 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3508 elf_tdata (abfd
)->next_file_pos
= off
;
3512 _bfd_elf_write_object_contents (abfd
)
3515 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3516 Elf_Internal_Ehdr
*i_ehdrp
;
3517 Elf_Internal_Shdr
**i_shdrp
;
3521 if (! abfd
->output_has_begun
3522 && ! _bfd_elf_compute_section_file_positions
3523 (abfd
, (struct bfd_link_info
*) NULL
))
3526 i_shdrp
= elf_elfsections (abfd
);
3527 i_ehdrp
= elf_elfheader (abfd
);
3530 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3534 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3536 /* After writing the headers, we need to write the sections too... */
3537 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3539 if (bed
->elf_backend_section_processing
)
3540 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3541 if (i_shdrp
[count
]->contents
)
3543 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3544 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3546 != i_shdrp
[count
]->sh_size
))
3551 /* Write out the section header names. */
3552 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3553 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3556 if (bed
->elf_backend_final_write_processing
)
3557 (*bed
->elf_backend_final_write_processing
) (abfd
,
3558 elf_tdata (abfd
)->linker
);
3560 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3564 _bfd_elf_write_corefile_contents (abfd
)
3567 /* Hopefully this can be done just like an object file. */
3568 return _bfd_elf_write_object_contents (abfd
);
3571 /* Given a section, search the header to find them. */
3574 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3578 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3579 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3581 Elf_Internal_Shdr
*hdr
;
3582 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3584 for (index
= 0; index
< maxindex
; index
++)
3586 hdr
= i_shdrp
[index
];
3587 if (hdr
->bfd_section
== asect
)
3591 if (bed
->elf_backend_section_from_bfd_section
)
3593 for (index
= 0; index
< maxindex
; index
++)
3597 hdr
= i_shdrp
[index
];
3599 if ((*bed
->elf_backend_section_from_bfd_section
)
3600 (abfd
, hdr
, asect
, &retval
))
3605 if (bfd_is_abs_section (asect
))
3607 if (bfd_is_com_section (asect
))
3609 if (bfd_is_und_section (asect
))
3612 bfd_set_error (bfd_error_nonrepresentable_section
);
3617 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3621 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3623 asymbol
**asym_ptr_ptr
;
3625 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3627 flagword flags
= asym_ptr
->flags
;
3629 /* When gas creates relocations against local labels, it creates its
3630 own symbol for the section, but does put the symbol into the
3631 symbol chain, so udata is 0. When the linker is generating
3632 relocatable output, this section symbol may be for one of the
3633 input sections rather than the output section. */
3634 if (asym_ptr
->udata
.i
== 0
3635 && (flags
& BSF_SECTION_SYM
)
3636 && asym_ptr
->section
)
3640 if (asym_ptr
->section
->output_section
!= NULL
)
3641 indx
= asym_ptr
->section
->output_section
->index
;
3643 indx
= asym_ptr
->section
->index
;
3644 if (elf_section_syms (abfd
)[indx
])
3645 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3648 idx
= asym_ptr
->udata
.i
;
3652 /* This case can occur when using --strip-symbol on a symbol
3653 which is used in a relocation entry. */
3654 (*_bfd_error_handler
)
3655 (_("%s: symbol `%s' required but not present"),
3656 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3657 bfd_set_error (bfd_error_no_symbols
);
3664 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3665 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3666 elf_symbol_flags (flags
));
3674 /* Copy private BFD data. This copies any program header information. */
3677 copy_private_bfd_data (ibfd
, obfd
)
3681 Elf_Internal_Ehdr
* iehdr
;
3682 struct elf_segment_map
* map
;
3683 struct elf_segment_map
* map_first
;
3684 struct elf_segment_map
** pointer_to_map
;
3685 Elf_Internal_Phdr
* segment
;
3688 unsigned int num_segments
;
3689 boolean phdr_included
= false;
3690 bfd_vma maxpagesize
;
3691 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3692 unsigned int phdr_adjust_num
= 0;
3694 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3695 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3698 if (elf_tdata (ibfd
)->phdr
== NULL
)
3701 iehdr
= elf_elfheader (ibfd
);
3704 pointer_to_map
= &map_first
;
3706 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3707 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3709 /* Returns the end address of the segment + 1. */
3710 #define SEGMENT_END(segment, start) \
3711 (start + (segment->p_memsz > segment->p_filesz \
3712 ? segment->p_memsz : segment->p_filesz))
3714 /* Returns true if the given section is contained within
3715 the given segment. VMA addresses are compared. */
3716 #define IS_CONTAINED_BY_VMA(section, segment) \
3717 (section->vma >= segment->p_vaddr \
3718 && (section->vma + section->_raw_size) \
3719 <= (SEGMENT_END (segment, segment->p_vaddr)))
3721 /* Returns true if the given section is contained within
3722 the given segment. LMA addresses are compared. */
3723 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3724 (section->lma >= base \
3725 && (section->lma + section->_raw_size) \
3726 <= SEGMENT_END (segment, base))
3728 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3729 #define IS_COREFILE_NOTE(p, s) \
3730 (p->p_type == PT_NOTE \
3731 && bfd_get_format (ibfd) == bfd_core \
3732 && s->vma == 0 && s->lma == 0 \
3733 && (bfd_vma) s->filepos >= p->p_offset \
3734 && (bfd_vma) s->filepos + s->_raw_size \
3735 <= p->p_offset + p->p_filesz)
3737 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3738 linker, which generates a PT_INTERP section with p_vaddr and
3739 p_memsz set to 0. */
3740 #define IS_SOLARIS_PT_INTERP(p, s) \
3742 && p->p_filesz > 0 \
3743 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3744 && s->_raw_size > 0 \
3745 && (bfd_vma) s->filepos >= p->p_offset \
3746 && ((bfd_vma) s->filepos + s->_raw_size \
3747 <= p->p_offset + p->p_filesz))
3749 /* Decide if the given section should be included in the given segment.
3750 A section will be included if:
3751 1. It is within the address space of the segment,
3752 2. It is an allocated segment,
3753 3. There is an output section associated with it,
3754 4. The section has not already been allocated to a previous segment. */
3755 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3756 ((((IS_CONTAINED_BY_VMA (section, segment) \
3757 || IS_SOLARIS_PT_INTERP (segment, section)) \
3758 && (section->flags & SEC_ALLOC) != 0) \
3759 || IS_COREFILE_NOTE (segment, section)) \
3760 && section->output_section != NULL \
3761 && section->segment_mark == false)
3763 /* Returns true iff seg1 starts after the end of seg2. */
3764 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3765 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3767 /* Returns true iff seg1 and seg2 overlap. */
3768 #define SEGMENT_OVERLAPS(seg1, seg2) \
3769 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3771 /* Initialise the segment mark field. */
3772 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3773 section
->segment_mark
= false;
3775 /* Scan through the segments specified in the program header
3776 of the input BFD. For this first scan we look for overlaps
3777 in the loadable segments. These can be created by wierd
3778 parameters to objcopy. */
3779 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3784 Elf_Internal_Phdr
*segment2
;
3786 if (segment
->p_type
!= PT_LOAD
)
3789 /* Determine if this segment overlaps any previous segments. */
3790 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
3792 bfd_signed_vma extra_length
;
3794 if (segment2
->p_type
!= PT_LOAD
3795 || ! SEGMENT_OVERLAPS (segment
, segment2
))
3798 /* Merge the two segments together. */
3799 if (segment2
->p_vaddr
< segment
->p_vaddr
)
3801 /* Extend SEGMENT2 to include SEGMENT and then delete
3804 SEGMENT_END (segment
, segment
->p_vaddr
)
3805 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
3807 if (extra_length
> 0)
3809 segment2
->p_memsz
+= extra_length
;
3810 segment2
->p_filesz
+= extra_length
;
3813 segment
->p_type
= PT_NULL
;
3815 /* Since we have deleted P we must restart the outer loop. */
3817 segment
= elf_tdata (ibfd
)->phdr
;
3822 /* Extend SEGMENT to include SEGMENT2 and then delete
3825 SEGMENT_END (segment2
, segment2
->p_vaddr
)
3826 - SEGMENT_END (segment
, segment
->p_vaddr
);
3828 if (extra_length
> 0)
3830 segment
->p_memsz
+= extra_length
;
3831 segment
->p_filesz
+= extra_length
;
3834 segment2
->p_type
= PT_NULL
;
3839 /* The second scan attempts to assign sections to segments. */
3840 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3844 unsigned int section_count
;
3845 asection
** sections
;
3846 asection
* output_section
;
3848 bfd_vma matching_lma
;
3849 bfd_vma suggested_lma
;
3852 if (segment
->p_type
== PT_NULL
)
3855 /* Compute how many sections might be placed into this segment. */
3857 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3858 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3861 /* Allocate a segment map big enough to contain all of the
3862 sections we have selected. */
3863 map
= ((struct elf_segment_map
*)
3865 (sizeof (struct elf_segment_map
)
3866 + ((size_t) section_count
- 1) * sizeof (asection
*))));
3870 /* Initialise the fields of the segment map. Default to
3871 using the physical address of the segment in the input BFD. */
3873 map
->p_type
= segment
->p_type
;
3874 map
->p_flags
= segment
->p_flags
;
3875 map
->p_flags_valid
= 1;
3876 map
->p_paddr
= segment
->p_paddr
;
3877 map
->p_paddr_valid
= 1;
3879 /* Determine if this segment contains the ELF file header
3880 and if it contains the program headers themselves. */
3881 map
->includes_filehdr
= (segment
->p_offset
== 0
3882 && segment
->p_filesz
>= iehdr
->e_ehsize
);
3884 map
->includes_phdrs
= 0;
3886 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
3888 map
->includes_phdrs
=
3889 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3890 && (segment
->p_offset
+ segment
->p_filesz
3891 >= ((bfd_vma
) iehdr
->e_phoff
3892 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3894 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
3895 phdr_included
= true;
3898 if (section_count
== 0)
3900 /* Special segments, such as the PT_PHDR segment, may contain
3901 no sections, but ordinary, loadable segments should contain
3903 if (segment
->p_type
== PT_LOAD
)
3905 (_("%s: warning: Empty loadable segment detected\n"),
3906 bfd_get_filename (ibfd
));
3909 *pointer_to_map
= map
;
3910 pointer_to_map
= &map
->next
;
3915 /* Now scan the sections in the input BFD again and attempt
3916 to add their corresponding output sections to the segment map.
3917 The problem here is how to handle an output section which has
3918 been moved (ie had its LMA changed). There are four possibilities:
3920 1. None of the sections have been moved.
3921 In this case we can continue to use the segment LMA from the
3924 2. All of the sections have been moved by the same amount.
3925 In this case we can change the segment's LMA to match the LMA
3926 of the first section.
3928 3. Some of the sections have been moved, others have not.
3929 In this case those sections which have not been moved can be
3930 placed in the current segment which will have to have its size,
3931 and possibly its LMA changed, and a new segment or segments will
3932 have to be created to contain the other sections.
3934 4. The sections have been moved, but not be the same amount.
3935 In this case we can change the segment's LMA to match the LMA
3936 of the first section and we will have to create a new segment
3937 or segments to contain the other sections.
3939 In order to save time, we allocate an array to hold the section
3940 pointers that we are interested in. As these sections get assigned
3941 to a segment, they are removed from this array. */
3943 sections
= (asection
**) bfd_malloc
3944 (sizeof (asection
*) * section_count
);
3945 if (sections
== NULL
)
3948 /* Step One: Scan for segment vs section LMA conflicts.
3949 Also add the sections to the section array allocated above.
3950 Also add the sections to the current segment. In the common
3951 case, where the sections have not been moved, this means that
3952 we have completely filled the segment, and there is nothing
3958 for (j
= 0, section
= ibfd
->sections
;
3960 section
= section
->next
)
3962 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3964 output_section
= section
->output_section
;
3966 sections
[j
++] = section
;
3968 /* The Solaris native linker always sets p_paddr to 0.
3969 We try to catch that case here, and set it to the
3971 if (segment
->p_paddr
== 0
3972 && segment
->p_vaddr
!= 0
3974 && output_section
->lma
!= 0
3975 && (output_section
->vma
== (segment
->p_vaddr
3976 + (map
->includes_filehdr
3979 + (map
->includes_phdrs
3980 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3982 map
->p_paddr
= segment
->p_vaddr
;
3984 /* Match up the physical address of the segment with the
3985 LMA address of the output section. */
3986 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
3987 || IS_COREFILE_NOTE (segment
, section
))
3989 if (matching_lma
== 0)
3990 matching_lma
= output_section
->lma
;
3992 /* We assume that if the section fits within the segment
3993 then it does not overlap any other section within that
3995 map
->sections
[isec
++] = output_section
;
3997 else if (suggested_lma
== 0)
3998 suggested_lma
= output_section
->lma
;
4002 BFD_ASSERT (j
== section_count
);
4004 /* Step Two: Adjust the physical address of the current segment,
4006 if (isec
== section_count
)
4008 /* All of the sections fitted within the segment as currently
4009 specified. This is the default case. Add the segment to
4010 the list of built segments and carry on to process the next
4011 program header in the input BFD. */
4012 map
->count
= section_count
;
4013 *pointer_to_map
= map
;
4014 pointer_to_map
= &map
->next
;
4021 if (matching_lma
!= 0)
4023 /* At least one section fits inside the current segment.
4024 Keep it, but modify its physical address to match the
4025 LMA of the first section that fitted. */
4026 map
->p_paddr
= matching_lma
;
4030 /* None of the sections fitted inside the current segment.
4031 Change the current segment's physical address to match
4032 the LMA of the first section. */
4033 map
->p_paddr
= suggested_lma
;
4036 /* Offset the segment physical address from the lma
4037 to allow for space taken up by elf headers. */
4038 if (map
->includes_filehdr
)
4039 map
->p_paddr
-= iehdr
->e_ehsize
;
4041 if (map
->includes_phdrs
)
4043 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4045 /* iehdr->e_phnum is just an estimate of the number
4046 of program headers that we will need. Make a note
4047 here of the number we used and the segment we chose
4048 to hold these headers, so that we can adjust the
4049 offset when we know the correct value. */
4050 phdr_adjust_num
= iehdr
->e_phnum
;
4051 phdr_adjust_seg
= map
;
4055 /* Step Three: Loop over the sections again, this time assigning
4056 those that fit to the current segment and remvoing them from the
4057 sections array; but making sure not to leave large gaps. Once all
4058 possible sections have been assigned to the current segment it is
4059 added to the list of built segments and if sections still remain
4060 to be assigned, a new segment is constructed before repeating
4068 /* Fill the current segment with sections that fit. */
4069 for (j
= 0; j
< section_count
; j
++)
4071 section
= sections
[j
];
4073 if (section
== NULL
)
4076 output_section
= section
->output_section
;
4078 BFD_ASSERT (output_section
!= NULL
);
4080 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4081 || IS_COREFILE_NOTE (segment
, section
))
4083 if (map
->count
== 0)
4085 /* If the first section in a segment does not start at
4086 the beginning of the segment, then something is
4088 if (output_section
->lma
!=
4090 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4091 + (map
->includes_phdrs
4092 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4098 asection
* prev_sec
;
4100 prev_sec
= map
->sections
[map
->count
- 1];
4102 /* If the gap between the end of the previous section
4103 and the start of this section is more than
4104 maxpagesize then we need to start a new segment. */
4105 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4106 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4107 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4109 if (suggested_lma
== 0)
4110 suggested_lma
= output_section
->lma
;
4116 map
->sections
[map
->count
++] = output_section
;
4119 section
->segment_mark
= true;
4121 else if (suggested_lma
== 0)
4122 suggested_lma
= output_section
->lma
;
4125 BFD_ASSERT (map
->count
> 0);
4127 /* Add the current segment to the list of built segments. */
4128 *pointer_to_map
= map
;
4129 pointer_to_map
= &map
->next
;
4131 if (isec
< section_count
)
4133 /* We still have not allocated all of the sections to
4134 segments. Create a new segment here, initialise it
4135 and carry on looping. */
4136 map
= ((struct elf_segment_map
*)
4138 (sizeof (struct elf_segment_map
)
4139 + ((size_t) section_count
- 1)
4140 * sizeof (asection
*))));
4144 /* Initialise the fields of the segment map. Set the physical
4145 physical address to the LMA of the first section that has
4146 not yet been assigned. */
4148 map
->p_type
= segment
->p_type
;
4149 map
->p_flags
= segment
->p_flags
;
4150 map
->p_flags_valid
= 1;
4151 map
->p_paddr
= suggested_lma
;
4152 map
->p_paddr_valid
= 1;
4153 map
->includes_filehdr
= 0;
4154 map
->includes_phdrs
= 0;
4157 while (isec
< section_count
);
4162 /* The Solaris linker creates program headers in which all the
4163 p_paddr fields are zero. When we try to objcopy or strip such a
4164 file, we get confused. Check for this case, and if we find it
4165 reset the p_paddr_valid fields. */
4166 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4167 if (map
->p_paddr
!= 0)
4171 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4172 map
->p_paddr_valid
= 0;
4175 elf_tdata (obfd
)->segment_map
= map_first
;
4177 /* If we had to estimate the number of program headers that were
4178 going to be needed, then check our estimate know and adjust
4179 the offset if necessary. */
4180 if (phdr_adjust_seg
!= NULL
)
4184 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4187 if (count
> phdr_adjust_num
)
4188 phdr_adjust_seg
->p_paddr
4189 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4193 /* Final Step: Sort the segments into ascending order of physical
4195 if (map_first
!= NULL
)
4197 struct elf_segment_map
*prev
;
4200 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4202 /* Yes I know - its a bubble sort.... */
4203 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4205 /* Swap map and map->next. */
4206 prev
->next
= map
->next
;
4207 map
->next
= map
->next
->next
;
4208 prev
->next
->next
= map
;
4218 #undef IS_CONTAINED_BY_VMA
4219 #undef IS_CONTAINED_BY_LMA
4220 #undef IS_COREFILE_NOTE
4221 #undef IS_SOLARIS_PT_INTERP
4222 #undef INCLUDE_SECTION_IN_SEGMENT
4223 #undef SEGMENT_AFTER_SEGMENT
4224 #undef SEGMENT_OVERLAPS
4228 /* Copy private section information. This copies over the entsize
4229 field, and sometimes the info field. */
4232 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4238 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4240 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4241 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4244 /* Copy over private BFD data if it has not already been copied.
4245 This must be done here, rather than in the copy_private_bfd_data
4246 entry point, because the latter is called after the section
4247 contents have been set, which means that the program headers have
4248 already been worked out. */
4249 if (elf_tdata (obfd
)->segment_map
== NULL
4250 && elf_tdata (ibfd
)->phdr
!= NULL
)
4254 /* Only set up the segments if there are no more SEC_ALLOC
4255 sections. FIXME: This won't do the right thing if objcopy is
4256 used to remove the last SEC_ALLOC section, since objcopy
4257 won't call this routine in that case. */
4258 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4259 if ((s
->flags
& SEC_ALLOC
) != 0)
4263 if (! copy_private_bfd_data (ibfd
, obfd
))
4268 ihdr
= &elf_section_data (isec
)->this_hdr
;
4269 ohdr
= &elf_section_data (osec
)->this_hdr
;
4271 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4273 if (ihdr
->sh_type
== SHT_SYMTAB
4274 || ihdr
->sh_type
== SHT_DYNSYM
4275 || ihdr
->sh_type
== SHT_GNU_verneed
4276 || ihdr
->sh_type
== SHT_GNU_verdef
)
4277 ohdr
->sh_info
= ihdr
->sh_info
;
4279 elf_section_data (osec
)->use_rela_p
4280 = elf_section_data (isec
)->use_rela_p
;
4285 /* Copy private symbol information. If this symbol is in a section
4286 which we did not map into a BFD section, try to map the section
4287 index correctly. We use special macro definitions for the mapped
4288 section indices; these definitions are interpreted by the
4289 swap_out_syms function. */
4291 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4292 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4293 #define MAP_STRTAB (SHN_LORESERVE - 3)
4294 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4297 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4303 elf_symbol_type
*isym
, *osym
;
4305 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4306 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4309 isym
= elf_symbol_from (ibfd
, isymarg
);
4310 osym
= elf_symbol_from (obfd
, osymarg
);
4314 && bfd_is_abs_section (isym
->symbol
.section
))
4318 shndx
= isym
->internal_elf_sym
.st_shndx
;
4319 if (shndx
== elf_onesymtab (ibfd
))
4320 shndx
= MAP_ONESYMTAB
;
4321 else if (shndx
== elf_dynsymtab (ibfd
))
4322 shndx
= MAP_DYNSYMTAB
;
4323 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4325 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4326 shndx
= MAP_SHSTRTAB
;
4327 osym
->internal_elf_sym
.st_shndx
= shndx
;
4333 /* Swap out the symbols. */
4336 swap_out_syms (abfd
, sttp
, relocatable_p
)
4338 struct bfd_strtab_hash
**sttp
;
4341 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4343 if (!elf_map_symbols (abfd
))
4346 /* Dump out the symtabs. */
4348 int symcount
= bfd_get_symcount (abfd
);
4349 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4350 struct bfd_strtab_hash
*stt
;
4351 Elf_Internal_Shdr
*symtab_hdr
;
4352 Elf_Internal_Shdr
*symstrtab_hdr
;
4353 char *outbound_syms
;
4356 stt
= _bfd_elf_stringtab_init ();
4360 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4361 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4362 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4363 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4364 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4365 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4367 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4368 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4370 outbound_syms
= bfd_alloc (abfd
,
4371 (1 + symcount
) * bed
->s
->sizeof_sym
);
4372 if (outbound_syms
== NULL
)
4374 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4376 /* now generate the data (for "contents") */
4378 /* Fill in zeroth symbol and swap it out. */
4379 Elf_Internal_Sym sym
;
4385 sym
.st_shndx
= SHN_UNDEF
;
4386 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4387 outbound_syms
+= bed
->s
->sizeof_sym
;
4389 for (idx
= 0; idx
< symcount
; idx
++)
4391 Elf_Internal_Sym sym
;
4392 bfd_vma value
= syms
[idx
]->value
;
4393 elf_symbol_type
*type_ptr
;
4394 flagword flags
= syms
[idx
]->flags
;
4397 if ((flags
& (BSF_SECTION_SYM
| BSF_GLOBAL
)) == BSF_SECTION_SYM
)
4399 /* Local section symbols have no name. */
4404 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4407 if (sym
.st_name
== (unsigned long) -1)
4411 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4413 if ((flags
& BSF_SECTION_SYM
) == 0
4414 && bfd_is_com_section (syms
[idx
]->section
))
4416 /* ELF common symbols put the alignment into the `value' field,
4417 and the size into the `size' field. This is backwards from
4418 how BFD handles it, so reverse it here. */
4419 sym
.st_size
= value
;
4420 if (type_ptr
== NULL
4421 || type_ptr
->internal_elf_sym
.st_value
== 0)
4422 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4424 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4425 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4426 (abfd
, syms
[idx
]->section
);
4430 asection
*sec
= syms
[idx
]->section
;
4433 if (sec
->output_section
)
4435 value
+= sec
->output_offset
;
4436 sec
= sec
->output_section
;
4438 /* Don't add in the section vma for relocatable output. */
4439 if (! relocatable_p
)
4441 sym
.st_value
= value
;
4442 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4444 if (bfd_is_abs_section (sec
)
4446 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4448 /* This symbol is in a real ELF section which we did
4449 not create as a BFD section. Undo the mapping done
4450 by copy_private_symbol_data. */
4451 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4455 shndx
= elf_onesymtab (abfd
);
4458 shndx
= elf_dynsymtab (abfd
);
4461 shndx
= elf_tdata (abfd
)->strtab_section
;
4464 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4472 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4478 /* Writing this would be a hell of a lot easier if
4479 we had some decent documentation on bfd, and
4480 knew what to expect of the library, and what to
4481 demand of applications. For example, it
4482 appears that `objcopy' might not set the
4483 section of a symbol to be a section that is
4484 actually in the output file. */
4485 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4486 BFD_ASSERT (sec2
!= 0);
4487 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4488 BFD_ASSERT (shndx
!= -1);
4492 sym
.st_shndx
= shndx
;
4495 if ((flags
& BSF_FUNCTION
) != 0)
4497 else if ((flags
& BSF_OBJECT
) != 0)
4502 /* Processor-specific types */
4503 if (type_ptr
!= NULL
4504 && bed
->elf_backend_get_symbol_type
)
4505 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4507 if (flags
& BSF_SECTION_SYM
)
4509 if (flags
& BSF_GLOBAL
)
4510 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, STT_SECTION
);
4512 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4514 else if (bfd_is_com_section (syms
[idx
]->section
))
4515 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4516 else if (bfd_is_und_section (syms
[idx
]->section
))
4517 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4521 else if (flags
& BSF_FILE
)
4522 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4525 int bind
= STB_LOCAL
;
4527 if (flags
& BSF_LOCAL
)
4529 else if (flags
& BSF_WEAK
)
4531 else if (flags
& BSF_GLOBAL
)
4534 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4537 if (type_ptr
!= NULL
)
4538 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4542 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4543 outbound_syms
+= bed
->s
->sizeof_sym
;
4547 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4548 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4550 symstrtab_hdr
->sh_flags
= 0;
4551 symstrtab_hdr
->sh_addr
= 0;
4552 symstrtab_hdr
->sh_entsize
= 0;
4553 symstrtab_hdr
->sh_link
= 0;
4554 symstrtab_hdr
->sh_info
= 0;
4555 symstrtab_hdr
->sh_addralign
= 1;
4561 /* Return the number of bytes required to hold the symtab vector.
4563 Note that we base it on the count plus 1, since we will null terminate
4564 the vector allocated based on this size. However, the ELF symbol table
4565 always has a dummy entry as symbol #0, so it ends up even. */
4568 _bfd_elf_get_symtab_upper_bound (abfd
)
4573 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4575 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4576 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4582 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4587 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4589 if (elf_dynsymtab (abfd
) == 0)
4591 bfd_set_error (bfd_error_invalid_operation
);
4595 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4596 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4602 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4603 bfd
*abfd ATTRIBUTE_UNUSED
;
4606 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4609 /* Canonicalize the relocs. */
4612 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4621 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4627 tblptr
= section
->relocation
;
4628 for (i
= 0; i
< section
->reloc_count
; i
++)
4629 *relptr
++ = tblptr
++;
4633 return section
->reloc_count
;
4637 _bfd_elf_get_symtab (abfd
, alocation
)
4639 asymbol
**alocation
;
4641 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4642 (abfd
, alocation
, false);
4645 bfd_get_symcount (abfd
) = symcount
;
4650 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4652 asymbol
**alocation
;
4654 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4655 (abfd
, alocation
, true);
4658 /* Return the size required for the dynamic reloc entries. Any
4659 section that was actually installed in the BFD, and has type
4660 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4661 considered to be a dynamic reloc section. */
4664 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4670 if (elf_dynsymtab (abfd
) == 0)
4672 bfd_set_error (bfd_error_invalid_operation
);
4676 ret
= sizeof (arelent
*);
4677 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4678 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4679 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4680 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4681 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4682 * sizeof (arelent
*));
4687 /* Canonicalize the dynamic relocation entries. Note that we return
4688 the dynamic relocations as a single block, although they are
4689 actually associated with particular sections; the interface, which
4690 was designed for SunOS style shared libraries, expects that there
4691 is only one set of dynamic relocs. Any section that was actually
4692 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4693 the dynamic symbol table, is considered to be a dynamic reloc
4697 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4702 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4706 if (elf_dynsymtab (abfd
) == 0)
4708 bfd_set_error (bfd_error_invalid_operation
);
4712 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4714 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4716 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4717 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4718 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4723 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4725 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4727 for (i
= 0; i
< count
; i
++)
4738 /* Read in the version information. */
4741 _bfd_elf_slurp_version_tables (abfd
)
4744 bfd_byte
*contents
= NULL
;
4746 if (elf_dynverdef (abfd
) != 0)
4748 Elf_Internal_Shdr
*hdr
;
4749 Elf_External_Verdef
*everdef
;
4750 Elf_Internal_Verdef
*iverdef
;
4751 Elf_Internal_Verdef
*iverdefarr
;
4752 Elf_Internal_Verdef iverdefmem
;
4754 unsigned int maxidx
;
4756 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4758 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4759 if (contents
== NULL
)
4761 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4762 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4765 /* We know the number of entries in the section but not the maximum
4766 index. Therefore we have to run through all entries and find
4768 everdef
= (Elf_External_Verdef
*) contents
;
4770 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4772 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4774 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
4775 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
4777 everdef
= ((Elf_External_Verdef
*)
4778 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4781 elf_tdata (abfd
)->verdef
=
4782 ((Elf_Internal_Verdef
*)
4783 bfd_zalloc (abfd
, maxidx
* sizeof (Elf_Internal_Verdef
)));
4784 if (elf_tdata (abfd
)->verdef
== NULL
)
4787 elf_tdata (abfd
)->cverdefs
= maxidx
;
4789 everdef
= (Elf_External_Verdef
*) contents
;
4790 iverdefarr
= elf_tdata (abfd
)->verdef
;
4791 for (i
= 0; i
< hdr
->sh_info
; i
++)
4793 Elf_External_Verdaux
*everdaux
;
4794 Elf_Internal_Verdaux
*iverdaux
;
4797 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4799 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4800 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4802 iverdef
->vd_bfd
= abfd
;
4804 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4807 * sizeof (Elf_Internal_Verdaux
))));
4808 if (iverdef
->vd_auxptr
== NULL
)
4811 everdaux
= ((Elf_External_Verdaux
*)
4812 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4813 iverdaux
= iverdef
->vd_auxptr
;
4814 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4816 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4818 iverdaux
->vda_nodename
=
4819 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4820 iverdaux
->vda_name
);
4821 if (iverdaux
->vda_nodename
== NULL
)
4824 if (j
+ 1 < iverdef
->vd_cnt
)
4825 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4827 iverdaux
->vda_nextptr
= NULL
;
4829 everdaux
= ((Elf_External_Verdaux
*)
4830 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4833 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4835 if (i
+ 1 < hdr
->sh_info
)
4836 iverdef
->vd_nextdef
= iverdef
+ 1;
4838 iverdef
->vd_nextdef
= NULL
;
4840 everdef
= ((Elf_External_Verdef
*)
4841 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4848 if (elf_dynverref (abfd
) != 0)
4850 Elf_Internal_Shdr
*hdr
;
4851 Elf_External_Verneed
*everneed
;
4852 Elf_Internal_Verneed
*iverneed
;
4855 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4857 elf_tdata (abfd
)->verref
=
4858 ((Elf_Internal_Verneed
*)
4859 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4860 if (elf_tdata (abfd
)->verref
== NULL
)
4863 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4865 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4866 if (contents
== NULL
)
4868 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4869 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4872 everneed
= (Elf_External_Verneed
*) contents
;
4873 iverneed
= elf_tdata (abfd
)->verref
;
4874 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4876 Elf_External_Vernaux
*evernaux
;
4877 Elf_Internal_Vernaux
*ivernaux
;
4880 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4882 iverneed
->vn_bfd
= abfd
;
4884 iverneed
->vn_filename
=
4885 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4887 if (iverneed
->vn_filename
== NULL
)
4890 iverneed
->vn_auxptr
=
4891 ((Elf_Internal_Vernaux
*)
4893 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4895 evernaux
= ((Elf_External_Vernaux
*)
4896 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4897 ivernaux
= iverneed
->vn_auxptr
;
4898 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4900 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4902 ivernaux
->vna_nodename
=
4903 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4904 ivernaux
->vna_name
);
4905 if (ivernaux
->vna_nodename
== NULL
)
4908 if (j
+ 1 < iverneed
->vn_cnt
)
4909 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4911 ivernaux
->vna_nextptr
= NULL
;
4913 evernaux
= ((Elf_External_Vernaux
*)
4914 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4917 if (i
+ 1 < hdr
->sh_info
)
4918 iverneed
->vn_nextref
= iverneed
+ 1;
4920 iverneed
->vn_nextref
= NULL
;
4922 everneed
= ((Elf_External_Verneed
*)
4923 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4933 if (contents
== NULL
)
4939 _bfd_elf_make_empty_symbol (abfd
)
4942 elf_symbol_type
*newsym
;
4944 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4949 newsym
->symbol
.the_bfd
= abfd
;
4950 return &newsym
->symbol
;
4955 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4956 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4960 bfd_symbol_info (symbol
, ret
);
4963 /* Return whether a symbol name implies a local symbol. Most targets
4964 use this function for the is_local_label_name entry point, but some
4968 _bfd_elf_is_local_label_name (abfd
, name
)
4969 bfd
*abfd ATTRIBUTE_UNUSED
;
4972 /* Normal local symbols start with ``.L''. */
4973 if (name
[0] == '.' && name
[1] == 'L')
4976 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4977 DWARF debugging symbols starting with ``..''. */
4978 if (name
[0] == '.' && name
[1] == '.')
4981 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4982 emitting DWARF debugging output. I suspect this is actually a
4983 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4984 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4985 underscore to be emitted on some ELF targets). For ease of use,
4986 we treat such symbols as local. */
4987 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4994 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4995 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4996 asymbol
*symbol ATTRIBUTE_UNUSED
;
5003 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
5005 enum bfd_architecture arch
;
5006 unsigned long machine
;
5008 /* If this isn't the right architecture for this backend, and this
5009 isn't the generic backend, fail. */
5010 if (arch
!= get_elf_backend_data (abfd
)->arch
5011 && arch
!= bfd_arch_unknown
5012 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
5015 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5018 /* Find the function to a particular section and offset,
5019 for error reporting. */
5022 elf_find_function (abfd
, section
, symbols
, offset
,
5023 filename_ptr
, functionname_ptr
)
5024 bfd
*abfd ATTRIBUTE_UNUSED
;
5028 const char **filename_ptr
;
5029 const char **functionname_ptr
;
5031 const char *filename
;
5040 for (p
= symbols
; *p
!= NULL
; p
++)
5044 q
= (elf_symbol_type
*) *p
;
5046 if (bfd_get_section (&q
->symbol
) != section
)
5049 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5054 filename
= bfd_asymbol_name (&q
->symbol
);
5058 if (q
->symbol
.section
== section
5059 && q
->symbol
.value
>= low_func
5060 && q
->symbol
.value
<= offset
)
5062 func
= (asymbol
*) q
;
5063 low_func
= q
->symbol
.value
;
5073 *filename_ptr
= filename
;
5074 if (functionname_ptr
)
5075 *functionname_ptr
= bfd_asymbol_name (func
);
5080 /* Find the nearest line to a particular section and offset,
5081 for error reporting. */
5084 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5085 filename_ptr
, functionname_ptr
, line_ptr
)
5090 const char **filename_ptr
;
5091 const char **functionname_ptr
;
5092 unsigned int *line_ptr
;
5096 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5097 filename_ptr
, functionname_ptr
,
5100 if (!*functionname_ptr
)
5101 elf_find_function (abfd
, section
, symbols
, offset
,
5102 *filename_ptr
? NULL
: filename_ptr
,
5108 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5109 filename_ptr
, functionname_ptr
,
5111 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5113 if (!*functionname_ptr
)
5114 elf_find_function (abfd
, section
, symbols
, offset
,
5115 *filename_ptr
? NULL
: filename_ptr
,
5121 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5122 &found
, filename_ptr
,
5123 functionname_ptr
, line_ptr
,
5124 &elf_tdata (abfd
)->line_info
))
5129 if (symbols
== NULL
)
5132 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5133 filename_ptr
, functionname_ptr
))
5141 _bfd_elf_sizeof_headers (abfd
, reloc
)
5147 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5149 ret
+= get_program_header_size (abfd
);
5154 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5159 bfd_size_type count
;
5161 Elf_Internal_Shdr
*hdr
;
5163 if (! abfd
->output_has_begun
5164 && ! _bfd_elf_compute_section_file_positions
5165 (abfd
, (struct bfd_link_info
*) NULL
))
5168 hdr
= &elf_section_data (section
)->this_hdr
;
5170 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
5172 if (bfd_write (location
, 1, count
, abfd
) != count
)
5179 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5180 bfd
*abfd ATTRIBUTE_UNUSED
;
5181 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5182 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5189 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5192 Elf_Internal_Rel
*dst
;
5198 /* Try to convert a non-ELF reloc into an ELF one. */
5201 _bfd_elf_validate_reloc (abfd
, areloc
)
5205 /* Check whether we really have an ELF howto. */
5207 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5209 bfd_reloc_code_real_type code
;
5210 reloc_howto_type
*howto
;
5212 /* Alien reloc: Try to determine its type to replace it with an
5213 equivalent ELF reloc. */
5215 if (areloc
->howto
->pc_relative
)
5217 switch (areloc
->howto
->bitsize
)
5220 code
= BFD_RELOC_8_PCREL
;
5223 code
= BFD_RELOC_12_PCREL
;
5226 code
= BFD_RELOC_16_PCREL
;
5229 code
= BFD_RELOC_24_PCREL
;
5232 code
= BFD_RELOC_32_PCREL
;
5235 code
= BFD_RELOC_64_PCREL
;
5241 howto
= bfd_reloc_type_lookup (abfd
, code
);
5243 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5245 if (howto
->pcrel_offset
)
5246 areloc
->addend
+= areloc
->address
;
5248 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5253 switch (areloc
->howto
->bitsize
)
5259 code
= BFD_RELOC_14
;
5262 code
= BFD_RELOC_16
;
5265 code
= BFD_RELOC_26
;
5268 code
= BFD_RELOC_32
;
5271 code
= BFD_RELOC_64
;
5277 howto
= bfd_reloc_type_lookup (abfd
, code
);
5281 areloc
->howto
= howto
;
5289 (*_bfd_error_handler
)
5290 (_("%s: unsupported relocation type %s"),
5291 bfd_get_filename (abfd
), areloc
->howto
->name
);
5292 bfd_set_error (bfd_error_bad_value
);
5297 _bfd_elf_close_and_cleanup (abfd
)
5300 if (bfd_get_format (abfd
) == bfd_object
)
5302 if (elf_shstrtab (abfd
) != NULL
)
5303 _bfd_stringtab_free (elf_shstrtab (abfd
));
5306 return _bfd_generic_close_and_cleanup (abfd
);
5309 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5310 in the relocation's offset. Thus we cannot allow any sort of sanity
5311 range-checking to interfere. There is nothing else to do in processing
5314 bfd_reloc_status_type
5315 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5316 bfd
*abfd ATTRIBUTE_UNUSED
;
5317 arelent
*re ATTRIBUTE_UNUSED
;
5318 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5319 PTR data ATTRIBUTE_UNUSED
;
5320 asection
*is ATTRIBUTE_UNUSED
;
5321 bfd
*obfd ATTRIBUTE_UNUSED
;
5322 char **errmsg ATTRIBUTE_UNUSED
;
5324 return bfd_reloc_ok
;
5327 /* Elf core file support. Much of this only works on native
5328 toolchains, since we rely on knowing the
5329 machine-dependent procfs structure in order to pick
5330 out details about the corefile. */
5332 #ifdef HAVE_SYS_PROCFS_H
5333 # include <sys/procfs.h>
5336 /* Define offsetof for those systems which lack it. */
5339 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5342 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5345 elfcore_make_pid (abfd
)
5348 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5349 + (elf_tdata (abfd
)->core_pid
));
5352 /* If there isn't a section called NAME, make one, using
5353 data from SECT. Note, this function will generate a
5354 reference to NAME, so you shouldn't deallocate or
5358 elfcore_maybe_make_sect (abfd
, name
, sect
)
5365 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5368 sect2
= bfd_make_section (abfd
, name
);
5372 sect2
->_raw_size
= sect
->_raw_size
;
5373 sect2
->filepos
= sect
->filepos
;
5374 sect2
->flags
= sect
->flags
;
5375 sect2
->alignment_power
= sect
->alignment_power
;
5379 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5380 actually creates up to two pseudosections:
5381 - For the single-threaded case, a section named NAME, unless
5382 such a section already exists.
5383 - For the multi-threaded case, a section named "NAME/PID", where
5384 PID is elfcore_make_pid (abfd).
5385 Both pseudosections have identical contents. */
5387 _bfd_elfcore_make_pseudosection (abfd
, name
, size
, filepos
)
5394 char *threaded_name
;
5397 /* Build the section name. */
5399 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5400 threaded_name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5401 if (threaded_name
== NULL
)
5403 strcpy (threaded_name
, buf
);
5405 sect
= bfd_make_section (abfd
, threaded_name
);
5408 sect
->_raw_size
= size
;
5409 sect
->filepos
= filepos
;
5410 sect
->flags
= SEC_HAS_CONTENTS
;
5411 sect
->alignment_power
= 2;
5413 return elfcore_maybe_make_sect (abfd
, name
, sect
);
5416 /* prstatus_t exists on:
5418 linux 2.[01] + glibc
5422 #if defined (HAVE_PRSTATUS_T)
5424 elfcore_grok_prstatus (abfd
, note
)
5426 Elf_Internal_Note
*note
;
5431 if (note
->descsz
== sizeof (prstatus_t
))
5435 raw_size
= sizeof (prstat
.pr_reg
);
5436 offset
= offsetof (prstatus_t
, pr_reg
);
5437 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5439 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5440 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5442 /* pr_who exists on:
5445 pr_who doesn't exist on:
5448 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5449 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5452 #if defined (HAVE_PRSTATUS32_T)
5453 else if (note
->descsz
== sizeof (prstatus32_t
))
5455 /* 64-bit host, 32-bit corefile */
5456 prstatus32_t prstat
;
5458 raw_size
= sizeof (prstat
.pr_reg
);
5459 offset
= offsetof (prstatus32_t
, pr_reg
);
5460 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5462 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5463 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5465 /* pr_who exists on:
5468 pr_who doesn't exist on:
5471 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5472 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5475 #endif /* HAVE_PRSTATUS32_T */
5478 /* Fail - we don't know how to handle any other
5479 note size (ie. data object type). */
5483 /* Make a ".reg/999" section and a ".reg" section. */
5484 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
5485 raw_size
, note
->descpos
+ offset
);
5487 #endif /* defined (HAVE_PRSTATUS_T) */
5489 /* Create a pseudosection containing the exact contents of NOTE. */
5491 elfcore_make_note_pseudosection (abfd
, name
, note
)
5494 Elf_Internal_Note
*note
;
5496 return _bfd_elfcore_make_pseudosection (abfd
, name
,
5497 note
->descsz
, note
->descpos
);
5500 /* There isn't a consistent prfpregset_t across platforms,
5501 but it doesn't matter, because we don't have to pick this
5502 data structure apart. */
5505 elfcore_grok_prfpreg (abfd
, note
)
5507 Elf_Internal_Note
*note
;
5509 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5512 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5513 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5517 elfcore_grok_prxfpreg (abfd
, note
)
5519 Elf_Internal_Note
*note
;
5521 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5524 #if defined (HAVE_PRPSINFO_T)
5525 typedef prpsinfo_t elfcore_psinfo_t
;
5526 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5527 typedef prpsinfo32_t elfcore_psinfo32_t
;
5531 #if defined (HAVE_PSINFO_T)
5532 typedef psinfo_t elfcore_psinfo_t
;
5533 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5534 typedef psinfo32_t elfcore_psinfo32_t
;
5538 /* return a malloc'ed copy of a string at START which is at
5539 most MAX bytes long, possibly without a terminating '\0'.
5540 the copy will always have a terminating '\0'. */
5543 _bfd_elfcore_strndup (abfd
, start
, max
)
5549 char *end
= memchr (start
, '\0', max
);
5557 dup
= bfd_alloc (abfd
, len
+ 1);
5561 memcpy (dup
, start
, len
);
5567 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5570 elfcore_grok_psinfo (abfd
, note
)
5572 Elf_Internal_Note
*note
;
5574 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5576 elfcore_psinfo_t psinfo
;
5578 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5580 elf_tdata (abfd
)->core_program
5581 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5582 sizeof (psinfo
.pr_fname
));
5584 elf_tdata (abfd
)->core_command
5585 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5586 sizeof (psinfo
.pr_psargs
));
5588 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5589 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5591 /* 64-bit host, 32-bit corefile */
5592 elfcore_psinfo32_t psinfo
;
5594 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5596 elf_tdata (abfd
)->core_program
5597 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_fname
,
5598 sizeof (psinfo
.pr_fname
));
5600 elf_tdata (abfd
)->core_command
5601 = _bfd_elfcore_strndup (abfd
, psinfo
.pr_psargs
,
5602 sizeof (psinfo
.pr_psargs
));
5608 /* Fail - we don't know how to handle any other
5609 note size (ie. data object type). */
5613 /* Note that for some reason, a spurious space is tacked
5614 onto the end of the args in some (at least one anyway)
5615 implementations, so strip it off if it exists. */
5618 char *command
= elf_tdata (abfd
)->core_command
;
5619 int n
= strlen (command
);
5621 if (0 < n
&& command
[n
- 1] == ' ')
5622 command
[n
- 1] = '\0';
5627 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5629 #if defined (HAVE_PSTATUS_T)
5631 elfcore_grok_pstatus (abfd
, note
)
5633 Elf_Internal_Note
*note
;
5635 if (note
->descsz
== sizeof (pstatus_t
)
5636 #if defined (HAVE_PXSTATUS_T)
5637 || note
->descsz
== sizeof (pxstatus_t
)
5643 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5645 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5647 #if defined (HAVE_PSTATUS32_T)
5648 else if (note
->descsz
== sizeof (pstatus32_t
))
5650 /* 64-bit host, 32-bit corefile */
5653 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5655 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5658 /* Could grab some more details from the "representative"
5659 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5660 NT_LWPSTATUS note, presumably. */
5664 #endif /* defined (HAVE_PSTATUS_T) */
5666 #if defined (HAVE_LWPSTATUS_T)
5668 elfcore_grok_lwpstatus (abfd
, note
)
5670 Elf_Internal_Note
*note
;
5672 lwpstatus_t lwpstat
;
5677 if (note
->descsz
!= sizeof (lwpstat
)
5678 #if defined (HAVE_LWPXSTATUS_T)
5679 && note
->descsz
!= sizeof (lwpxstatus_t
)
5684 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5686 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5687 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5689 /* Make a ".reg/999" section. */
5691 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5692 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5697 sect
= bfd_make_section (abfd
, name
);
5701 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5702 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5703 sect
->filepos
= note
->descpos
5704 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5707 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5708 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5709 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5712 sect
->flags
= SEC_HAS_CONTENTS
;
5713 sect
->alignment_power
= 2;
5715 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5718 /* Make a ".reg2/999" section */
5720 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5721 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5726 sect
= bfd_make_section (abfd
, name
);
5730 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5731 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5732 sect
->filepos
= note
->descpos
5733 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5736 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5737 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5738 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5741 sect
->flags
= SEC_HAS_CONTENTS
;
5742 sect
->alignment_power
= 2;
5744 return elfcore_maybe_make_sect (abfd
, ".reg2", sect
);
5746 #endif /* defined (HAVE_LWPSTATUS_T) */
5748 #if defined (HAVE_WIN32_PSTATUS_T)
5750 elfcore_grok_win32pstatus (abfd
, note
)
5752 Elf_Internal_Note
*note
;
5757 win32_pstatus_t pstatus
;
5759 if (note
->descsz
< sizeof (pstatus
))
5762 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
5764 switch (pstatus
.data_type
)
5766 case NOTE_INFO_PROCESS
:
5767 /* FIXME: need to add ->core_command. */
5768 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5769 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5772 case NOTE_INFO_THREAD
:
5773 /* Make a ".reg/999" section. */
5774 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5776 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5782 sect
= bfd_make_section (abfd
, name
);
5786 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5787 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5788 data
.thread_info
.thread_context
);
5789 sect
->flags
= SEC_HAS_CONTENTS
;
5790 sect
->alignment_power
= 2;
5792 if (pstatus
.data
.thread_info
.is_active_thread
)
5793 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5797 case NOTE_INFO_MODULE
:
5798 /* Make a ".module/xxxxxxxx" section. */
5799 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
5801 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5807 sect
= bfd_make_section (abfd
, name
);
5812 sect
->_raw_size
= note
->descsz
;
5813 sect
->filepos
= note
->descpos
;
5814 sect
->flags
= SEC_HAS_CONTENTS
;
5815 sect
->alignment_power
= 2;
5824 #endif /* HAVE_WIN32_PSTATUS_T */
5827 elfcore_grok_note (abfd
, note
)
5829 Elf_Internal_Note
*note
;
5831 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
5839 if (bed
->elf_backend_grok_prstatus
)
5840 if ((*bed
->elf_backend_grok_prstatus
) (abfd
, note
))
5842 #if defined (HAVE_PRSTATUS_T)
5843 return elfcore_grok_prstatus (abfd
, note
);
5848 #if defined (HAVE_PSTATUS_T)
5850 return elfcore_grok_pstatus (abfd
, note
);
5853 #if defined (HAVE_LWPSTATUS_T)
5855 return elfcore_grok_lwpstatus (abfd
, note
);
5858 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5859 return elfcore_grok_prfpreg (abfd
, note
);
5861 #if defined (HAVE_WIN32_PSTATUS_T)
5862 case NT_WIN32PSTATUS
:
5863 return elfcore_grok_win32pstatus (abfd
, note
);
5866 case NT_PRXFPREG
: /* Linux SSE extension */
5867 if (note
->namesz
== 5
5868 && ! strcmp (note
->namedata
, "LINUX"))
5869 return elfcore_grok_prxfpreg (abfd
, note
);
5875 if (bed
->elf_backend_grok_psinfo
)
5876 if ((*bed
->elf_backend_grok_psinfo
) (abfd
, note
))
5878 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5879 return elfcore_grok_psinfo (abfd
, note
);
5887 elfcore_read_notes (abfd
, offset
, size
)
5898 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
5901 buf
= bfd_malloc ((size_t) size
);
5905 if (bfd_read (buf
, size
, 1, abfd
) != size
)
5913 while (p
< buf
+ size
)
5915 /* FIXME: bad alignment assumption. */
5916 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
5917 Elf_Internal_Note in
;
5919 in
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->type
);
5921 in
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->namesz
);
5922 in
.namedata
= xnp
->name
;
5924 in
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->descsz
);
5925 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5926 in
.descpos
= offset
+ (in
.descdata
- buf
);
5928 if (! elfcore_grok_note (abfd
, &in
))
5931 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5938 /* Providing external access to the ELF program header table. */
5940 /* Return an upper bound on the number of bytes required to store a
5941 copy of ABFD's program header table entries. Return -1 if an error
5942 occurs; bfd_get_error will return an appropriate code. */
5945 bfd_get_elf_phdr_upper_bound (abfd
)
5948 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5950 bfd_set_error (bfd_error_wrong_format
);
5954 return elf_elfheader (abfd
)->e_phnum
* sizeof (Elf_Internal_Phdr
);
5957 /* Copy ABFD's program header table entries to *PHDRS. The entries
5958 will be stored as an array of Elf_Internal_Phdr structures, as
5959 defined in include/elf/internal.h. To find out how large the
5960 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5962 Return the number of program header table entries read, or -1 if an
5963 error occurs; bfd_get_error will return an appropriate code. */
5966 bfd_get_elf_phdrs (abfd
, phdrs
)
5972 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5974 bfd_set_error (bfd_error_wrong_format
);
5978 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
5979 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
5980 num_phdrs
* sizeof (Elf_Internal_Phdr
));