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 /* Print out the program headers. */
564 _bfd_elf_print_private_bfd_data (abfd
, farg
)
568 FILE *f
= (FILE *) farg
;
569 Elf_Internal_Phdr
*p
;
571 bfd_byte
*dynbuf
= NULL
;
573 p
= elf_tdata (abfd
)->phdr
;
578 fprintf (f
, _("\nProgram Header:\n"));
579 c
= elf_elfheader (abfd
)->e_phnum
;
580 for (i
= 0; i
< c
; i
++, p
++)
587 case PT_NULL
: s
= "NULL"; break;
588 case PT_LOAD
: s
= "LOAD"; break;
589 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
590 case PT_INTERP
: s
= "INTERP"; break;
591 case PT_NOTE
: s
= "NOTE"; break;
592 case PT_SHLIB
: s
= "SHLIB"; break;
593 case PT_PHDR
: s
= "PHDR"; break;
594 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
596 fprintf (f
, "%8s off 0x", s
);
597 fprintf_vma (f
, p
->p_offset
);
598 fprintf (f
, " vaddr 0x");
599 fprintf_vma (f
, p
->p_vaddr
);
600 fprintf (f
, " paddr 0x");
601 fprintf_vma (f
, p
->p_paddr
);
602 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
603 fprintf (f
, " filesz 0x");
604 fprintf_vma (f
, p
->p_filesz
);
605 fprintf (f
, " memsz 0x");
606 fprintf_vma (f
, p
->p_memsz
);
607 fprintf (f
, " flags %c%c%c",
608 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
609 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
610 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
611 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
612 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
617 s
= bfd_get_section_by_name (abfd
, ".dynamic");
622 bfd_byte
*extdyn
, *extdynend
;
624 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
626 fprintf (f
, _("\nDynamic Section:\n"));
628 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
631 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
635 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
638 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
640 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
641 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
644 extdynend
= extdyn
+ s
->_raw_size
;
645 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
647 Elf_Internal_Dyn dyn
;
652 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
654 if (dyn
.d_tag
== DT_NULL
)
661 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
665 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
666 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
667 case DT_PLTGOT
: name
= "PLTGOT"; break;
668 case DT_HASH
: name
= "HASH"; break;
669 case DT_STRTAB
: name
= "STRTAB"; break;
670 case DT_SYMTAB
: name
= "SYMTAB"; break;
671 case DT_RELA
: name
= "RELA"; break;
672 case DT_RELASZ
: name
= "RELASZ"; break;
673 case DT_RELAENT
: name
= "RELAENT"; break;
674 case DT_STRSZ
: name
= "STRSZ"; break;
675 case DT_SYMENT
: name
= "SYMENT"; break;
676 case DT_INIT
: name
= "INIT"; break;
677 case DT_FINI
: name
= "FINI"; break;
678 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
679 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
680 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
681 case DT_REL
: name
= "REL"; break;
682 case DT_RELSZ
: name
= "RELSZ"; break;
683 case DT_RELENT
: name
= "RELENT"; break;
684 case DT_PLTREL
: name
= "PLTREL"; break;
685 case DT_DEBUG
: name
= "DEBUG"; break;
686 case DT_TEXTREL
: name
= "TEXTREL"; break;
687 case DT_JMPREL
: name
= "JMPREL"; break;
688 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
689 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
690 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
691 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
692 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
693 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
694 case DT_FLAGS
: name
= "FLAGS"; break;
695 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
696 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
697 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
698 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
699 case DT_MOVEENT
: name
= "MOVEENT"; break;
700 case DT_MOVESZ
: name
= "MOVESZ"; break;
701 case DT_FEATURE
: name
= "FEATURE"; break;
702 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
703 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
704 case DT_SYMINENT
: name
= "SYMINENT"; break;
705 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
706 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
707 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
708 case DT_PLTPAD
: name
= "PLTPAD"; break;
709 case DT_MOVETAB
: name
= "MOVETAB"; break;
710 case DT_SYMINFO
: name
= "SYMINFO"; break;
711 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
712 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
713 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
714 case DT_VERSYM
: name
= "VERSYM"; break;
715 case DT_VERDEF
: name
= "VERDEF"; break;
716 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
717 case DT_VERNEED
: name
= "VERNEED"; break;
718 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
719 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
720 case DT_USED
: name
= "USED"; break;
721 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
724 fprintf (f
, " %-11s ", name
);
726 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
731 string
= bfd_elf_string_from_elf_section (abfd
, link
,
735 fprintf (f
, "%s", string
);
744 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
745 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
747 if (! _bfd_elf_slurp_version_tables (abfd
))
751 if (elf_dynverdef (abfd
) != 0)
753 Elf_Internal_Verdef
*t
;
755 fprintf (f
, _("\nVersion definitions:\n"));
756 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
758 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
759 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
760 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
762 Elf_Internal_Verdaux
*a
;
765 for (a
= t
->vd_auxptr
->vda_nextptr
;
768 fprintf (f
, "%s ", a
->vda_nodename
);
774 if (elf_dynverref (abfd
) != 0)
776 Elf_Internal_Verneed
*t
;
778 fprintf (f
, _("\nVersion References:\n"));
779 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
781 Elf_Internal_Vernaux
*a
;
783 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
784 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
785 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
786 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
798 /* Display ELF-specific fields of a symbol. */
801 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
805 bfd_print_symbol_type how
;
807 FILE *file
= (FILE *) filep
;
810 case bfd_print_symbol_name
:
811 fprintf (file
, "%s", symbol
->name
);
813 case bfd_print_symbol_more
:
814 fprintf (file
, "elf ");
815 fprintf_vma (file
, symbol
->value
);
816 fprintf (file
, " %lx", (long) symbol
->flags
);
818 case bfd_print_symbol_all
:
820 CONST
char *section_name
;
821 CONST
char *name
= NULL
;
822 struct elf_backend_data
*bed
;
823 unsigned char st_other
;
825 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
827 bed
= get_elf_backend_data (abfd
);
828 if (bed
->elf_backend_print_symbol_all
)
829 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
834 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
837 fprintf (file
, " %s\t", section_name
);
838 /* Print the "other" value for a symbol. For common symbols,
839 we've already printed the size; now print the alignment.
840 For other symbols, we have no specified alignment, and
841 we've printed the address; now print the size. */
843 (bfd_is_com_section (symbol
->section
)
844 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
845 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
847 /* If we have version information, print it. */
848 if (elf_tdata (abfd
)->dynversym_section
!= 0
849 && (elf_tdata (abfd
)->dynverdef_section
!= 0
850 || elf_tdata (abfd
)->dynverref_section
!= 0))
853 const char *version_string
;
855 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
859 else if (vernum
== 1)
860 version_string
= "Base";
861 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
863 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
866 Elf_Internal_Verneed
*t
;
869 for (t
= elf_tdata (abfd
)->verref
;
873 Elf_Internal_Vernaux
*a
;
875 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
877 if (a
->vna_other
== vernum
)
879 version_string
= a
->vna_nodename
;
886 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
887 fprintf (file
, " %-11s", version_string
);
892 fprintf (file
, " (%s)", version_string
);
893 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
898 /* If the st_other field is not zero, print it. */
899 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
904 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
905 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
906 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
908 /* Some other non-defined flags are also present, so print
910 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
913 fprintf (file
, " %s", name
);
919 /* Create an entry in an ELF linker hash table. */
921 struct bfd_hash_entry
*
922 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
923 struct bfd_hash_entry
*entry
;
924 struct bfd_hash_table
*table
;
927 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
929 /* Allocate the structure if it has not already been allocated by a
931 if (ret
== (struct elf_link_hash_entry
*) NULL
)
932 ret
= ((struct elf_link_hash_entry
*)
933 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
934 if (ret
== (struct elf_link_hash_entry
*) NULL
)
935 return (struct bfd_hash_entry
*) ret
;
937 /* Call the allocation method of the superclass. */
938 ret
= ((struct elf_link_hash_entry
*)
939 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
941 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
943 /* Set local fields. */
947 ret
->dynstr_index
= 0;
949 ret
->got
.offset
= (bfd_vma
) -1;
950 ret
->plt
.offset
= (bfd_vma
) -1;
951 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
952 ret
->verinfo
.verdef
= NULL
;
953 ret
->vtable_entries_used
= NULL
;
954 ret
->vtable_entries_size
= 0;
955 ret
->vtable_parent
= NULL
;
956 ret
->type
= STT_NOTYPE
;
958 /* Assume that we have been called by a non-ELF symbol reader.
959 This flag is then reset by the code which reads an ELF input
960 file. This ensures that a symbol created by a non-ELF symbol
961 reader will have the flag set correctly. */
962 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
965 return (struct bfd_hash_entry
*) ret
;
968 /* Copy data from an indirect symbol to its direct symbol, hiding the
969 old indirect symbol. */
972 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
973 struct elf_link_hash_entry
*dir
, *ind
;
975 /* Copy down any references that we may have already seen to the
976 symbol which just became indirect. */
978 dir
->elf_link_hash_flags
|=
979 (ind
->elf_link_hash_flags
980 & (ELF_LINK_HASH_REF_DYNAMIC
981 | ELF_LINK_HASH_REF_REGULAR
982 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
983 | ELF_LINK_NON_GOT_REF
));
985 /* Copy over the global and procedure linkage table offset entries.
986 These may have been already set up by a check_relocs routine. */
987 if (dir
->got
.offset
== (bfd_vma
) -1)
989 dir
->got
.offset
= ind
->got
.offset
;
990 ind
->got
.offset
= (bfd_vma
) -1;
992 BFD_ASSERT (ind
->got
.offset
== (bfd_vma
) -1);
994 if (dir
->plt
.offset
== (bfd_vma
) -1)
996 dir
->plt
.offset
= ind
->plt
.offset
;
997 ind
->plt
.offset
= (bfd_vma
) -1;
999 BFD_ASSERT (ind
->plt
.offset
== (bfd_vma
) -1);
1001 if (dir
->dynindx
== -1)
1003 dir
->dynindx
= ind
->dynindx
;
1004 dir
->dynstr_index
= ind
->dynstr_index
;
1006 ind
->dynstr_index
= 0;
1008 BFD_ASSERT (ind
->dynindx
== -1);
1012 _bfd_elf_link_hash_hide_symbol (info
, h
)
1013 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1014 struct elf_link_hash_entry
*h
;
1016 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1017 h
->plt
.offset
= (bfd_vma
) -1;
1018 if ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0)
1022 /* Initialize an ELF linker hash table. */
1025 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1026 struct elf_link_hash_table
*table
;
1028 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1029 struct bfd_hash_table
*,
1032 table
->dynamic_sections_created
= false;
1033 table
->dynobj
= NULL
;
1034 /* The first dynamic symbol is a dummy. */
1035 table
->dynsymcount
= 1;
1036 table
->dynstr
= NULL
;
1037 table
->bucketcount
= 0;
1038 table
->needed
= NULL
;
1039 table
->runpath
= NULL
;
1041 table
->stab_info
= NULL
;
1042 table
->merge_info
= NULL
;
1043 table
->dynlocal
= NULL
;
1044 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1047 /* Create an ELF linker hash table. */
1049 struct bfd_link_hash_table
*
1050 _bfd_elf_link_hash_table_create (abfd
)
1053 struct elf_link_hash_table
*ret
;
1055 ret
= ((struct elf_link_hash_table
*)
1056 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
1057 if (ret
== (struct elf_link_hash_table
*) NULL
)
1060 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1062 bfd_release (abfd
, ret
);
1069 /* This is a hook for the ELF emulation code in the generic linker to
1070 tell the backend linker what file name to use for the DT_NEEDED
1071 entry for a dynamic object. The generic linker passes name as an
1072 empty string to indicate that no DT_NEEDED entry should be made. */
1075 bfd_elf_set_dt_needed_name (abfd
, name
)
1079 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1080 && bfd_get_format (abfd
) == bfd_object
)
1081 elf_dt_name (abfd
) = name
;
1085 bfd_elf_set_dt_needed_soname (abfd
, name
)
1089 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1090 && bfd_get_format (abfd
) == bfd_object
)
1091 elf_dt_soname (abfd
) = name
;
1094 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1095 the linker ELF emulation code. */
1097 struct bfd_link_needed_list
*
1098 bfd_elf_get_needed_list (abfd
, info
)
1099 bfd
*abfd ATTRIBUTE_UNUSED
;
1100 struct bfd_link_info
*info
;
1102 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1104 return elf_hash_table (info
)->needed
;
1107 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1108 hook for the linker ELF emulation code. */
1110 struct bfd_link_needed_list
*
1111 bfd_elf_get_runpath_list (abfd
, info
)
1112 bfd
*abfd ATTRIBUTE_UNUSED
;
1113 struct bfd_link_info
*info
;
1115 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1117 return elf_hash_table (info
)->runpath
;
1120 /* Get the name actually used for a dynamic object for a link. This
1121 is the SONAME entry if there is one. Otherwise, it is the string
1122 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1125 bfd_elf_get_dt_soname (abfd
)
1128 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1129 && bfd_get_format (abfd
) == bfd_object
)
1130 return elf_dt_name (abfd
);
1134 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1135 the ELF linker emulation code. */
1138 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1140 struct bfd_link_needed_list
**pneeded
;
1143 bfd_byte
*dynbuf
= NULL
;
1146 bfd_byte
*extdyn
, *extdynend
;
1148 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1152 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1153 || bfd_get_format (abfd
) != bfd_object
)
1156 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1157 if (s
== NULL
|| s
->_raw_size
== 0)
1160 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1164 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1168 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1172 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1174 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1175 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1178 extdynend
= extdyn
+ s
->_raw_size
;
1179 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1181 Elf_Internal_Dyn dyn
;
1183 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1185 if (dyn
.d_tag
== DT_NULL
)
1188 if (dyn
.d_tag
== DT_NEEDED
)
1191 struct bfd_link_needed_list
*l
;
1193 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1198 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1219 /* Allocate an ELF string table--force the first byte to be zero. */
1221 struct bfd_strtab_hash
*
1222 _bfd_elf_stringtab_init ()
1224 struct bfd_strtab_hash
*ret
;
1226 ret
= _bfd_stringtab_init ();
1231 loc
= _bfd_stringtab_add (ret
, "", true, false);
1232 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1233 if (loc
== (bfd_size_type
) -1)
1235 _bfd_stringtab_free (ret
);
1242 /* ELF .o/exec file reading */
1244 /* Create a new bfd section from an ELF section header. */
1247 bfd_section_from_shdr (abfd
, shindex
)
1249 unsigned int shindex
;
1251 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1252 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1253 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1256 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1258 switch (hdr
->sh_type
)
1261 /* Inactive section. Throw it away. */
1264 case SHT_PROGBITS
: /* Normal section with contents. */
1265 case SHT_DYNAMIC
: /* Dynamic linking information. */
1266 case SHT_NOBITS
: /* .bss section. */
1267 case SHT_HASH
: /* .hash section. */
1268 case SHT_NOTE
: /* .note section. */
1269 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1271 case SHT_SYMTAB
: /* A symbol table */
1272 if (elf_onesymtab (abfd
) == shindex
)
1275 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1276 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1277 elf_onesymtab (abfd
) = shindex
;
1278 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1279 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1280 abfd
->flags
|= HAS_SYMS
;
1282 /* Sometimes a shared object will map in the symbol table. If
1283 SHF_ALLOC is set, and this is a shared object, then we also
1284 treat this section as a BFD section. We can not base the
1285 decision purely on SHF_ALLOC, because that flag is sometimes
1286 set in a relocateable object file, which would confuse the
1288 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1289 && (abfd
->flags
& DYNAMIC
) != 0
1290 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1295 case SHT_DYNSYM
: /* A dynamic symbol table */
1296 if (elf_dynsymtab (abfd
) == shindex
)
1299 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1300 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1301 elf_dynsymtab (abfd
) = shindex
;
1302 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1303 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1304 abfd
->flags
|= HAS_SYMS
;
1306 /* Besides being a symbol table, we also treat this as a regular
1307 section, so that objcopy can handle it. */
1308 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1310 case SHT_STRTAB
: /* A string table */
1311 if (hdr
->bfd_section
!= NULL
)
1313 if (ehdr
->e_shstrndx
== shindex
)
1315 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1316 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1322 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1324 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1325 if (hdr2
->sh_link
== shindex
)
1327 if (! bfd_section_from_shdr (abfd
, i
))
1329 if (elf_onesymtab (abfd
) == i
)
1331 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1332 elf_elfsections (abfd
)[shindex
] =
1333 &elf_tdata (abfd
)->strtab_hdr
;
1336 if (elf_dynsymtab (abfd
) == i
)
1338 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1339 elf_elfsections (abfd
)[shindex
] = hdr
=
1340 &elf_tdata (abfd
)->dynstrtab_hdr
;
1341 /* We also treat this as a regular section, so
1342 that objcopy can handle it. */
1345 #if 0 /* Not handling other string tables specially right now. */
1346 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1347 /* We have a strtab for some random other section. */
1348 newsect
= (asection
*) hdr2
->bfd_section
;
1351 hdr
->bfd_section
= newsect
;
1352 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1354 elf_elfsections (abfd
)[shindex
] = hdr2
;
1360 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1364 /* *These* do a lot of work -- but build no sections! */
1366 asection
*target_sect
;
1367 Elf_Internal_Shdr
*hdr2
;
1369 /* Check for a bogus link to avoid crashing. */
1370 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1372 ((*_bfd_error_handler
)
1373 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1374 bfd_get_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1375 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1378 /* For some incomprehensible reason Oracle distributes
1379 libraries for Solaris in which some of the objects have
1380 bogus sh_link fields. It would be nice if we could just
1381 reject them, but, unfortunately, some people need to use
1382 them. We scan through the section headers; if we find only
1383 one suitable symbol table, we clobber the sh_link to point
1384 to it. I hope this doesn't break anything. */
1385 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1386 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1392 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1394 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1395 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1406 hdr
->sh_link
= found
;
1409 /* Get the symbol table. */
1410 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1411 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1414 /* If this reloc section does not use the main symbol table we
1415 don't treat it as a reloc section. BFD can't adequately
1416 represent such a section, so at least for now, we don't
1417 try. We just present it as a normal section. We also
1418 can't use it as a reloc section if it points to the null
1420 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1421 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1423 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1425 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1426 if (target_sect
== NULL
)
1429 if ((target_sect
->flags
& SEC_RELOC
) == 0
1430 || target_sect
->reloc_count
== 0)
1431 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1434 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1435 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1436 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1439 elf_elfsections (abfd
)[shindex
] = hdr2
;
1440 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
1441 target_sect
->flags
|= SEC_RELOC
;
1442 target_sect
->relocation
= NULL
;
1443 target_sect
->rel_filepos
= hdr
->sh_offset
;
1444 /* In the section to which the relocations apply, mark whether
1445 its relocations are of the REL or RELA variety. */
1446 if (hdr
->sh_size
!= 0)
1447 elf_section_data (target_sect
)->use_rela_p
1448 = (hdr
->sh_type
== SHT_RELA
);
1449 abfd
->flags
|= HAS_RELOC
;
1454 case SHT_GNU_verdef
:
1455 elf_dynverdef (abfd
) = shindex
;
1456 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1457 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1460 case SHT_GNU_versym
:
1461 elf_dynversym (abfd
) = shindex
;
1462 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1463 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1466 case SHT_GNU_verneed
:
1467 elf_dynverref (abfd
) = shindex
;
1468 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1469 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1476 /* Check for any processor-specific section types. */
1478 if (bed
->elf_backend_section_from_shdr
)
1479 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1487 /* Given an ELF section number, retrieve the corresponding BFD
1491 bfd_section_from_elf_index (abfd
, index
)
1495 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1496 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1498 return elf_elfsections (abfd
)[index
]->bfd_section
;
1502 _bfd_elf_new_section_hook (abfd
, sec
)
1506 struct bfd_elf_section_data
*sdata
;
1508 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, sizeof (*sdata
));
1511 sec
->used_by_bfd
= (PTR
) sdata
;
1513 /* Indicate whether or not this section should use RELA relocations. */
1515 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1520 /* Create a new bfd section from an ELF program header.
1522 Since program segments have no names, we generate a synthetic name
1523 of the form segment<NUM>, where NUM is generally the index in the
1524 program header table. For segments that are split (see below) we
1525 generate the names segment<NUM>a and segment<NUM>b.
1527 Note that some program segments may have a file size that is different than
1528 (less than) the memory size. All this means is that at execution the
1529 system must allocate the amount of memory specified by the memory size,
1530 but only initialize it with the first "file size" bytes read from the
1531 file. This would occur for example, with program segments consisting
1532 of combined data+bss.
1534 To handle the above situation, this routine generates TWO bfd sections
1535 for the single program segment. The first has the length specified by
1536 the file size of the segment, and the second has the length specified
1537 by the difference between the two sizes. In effect, the segment is split
1538 into it's initialized and uninitialized parts.
1543 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1545 Elf_Internal_Phdr
*hdr
;
1547 const char *typename
;
1554 split
= ((hdr
->p_memsz
> 0)
1555 && (hdr
->p_filesz
> 0)
1556 && (hdr
->p_memsz
> hdr
->p_filesz
));
1557 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1558 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1561 strcpy (name
, namebuf
);
1562 newsect
= bfd_make_section (abfd
, name
);
1563 if (newsect
== NULL
)
1565 newsect
->vma
= hdr
->p_vaddr
;
1566 newsect
->lma
= hdr
->p_paddr
;
1567 newsect
->_raw_size
= hdr
->p_filesz
;
1568 newsect
->filepos
= hdr
->p_offset
;
1569 newsect
->flags
|= SEC_HAS_CONTENTS
;
1570 if (hdr
->p_type
== PT_LOAD
)
1572 newsect
->flags
|= SEC_ALLOC
;
1573 newsect
->flags
|= SEC_LOAD
;
1574 if (hdr
->p_flags
& PF_X
)
1576 /* FIXME: all we known is that it has execute PERMISSION,
1578 newsect
->flags
|= SEC_CODE
;
1581 if (!(hdr
->p_flags
& PF_W
))
1583 newsect
->flags
|= SEC_READONLY
;
1588 sprintf (namebuf
, "%s%db", typename
, index
);
1589 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1592 strcpy (name
, namebuf
);
1593 newsect
= bfd_make_section (abfd
, name
);
1594 if (newsect
== NULL
)
1596 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1597 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1598 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1599 if (hdr
->p_type
== PT_LOAD
)
1601 newsect
->flags
|= SEC_ALLOC
;
1602 if (hdr
->p_flags
& PF_X
)
1603 newsect
->flags
|= SEC_CODE
;
1605 if (!(hdr
->p_flags
& PF_W
))
1606 newsect
->flags
|= SEC_READONLY
;
1613 bfd_section_from_phdr (abfd
, hdr
, index
)
1615 Elf_Internal_Phdr
*hdr
;
1618 struct elf_backend_data
*bed
;
1620 switch (hdr
->p_type
)
1623 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1626 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1629 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1632 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1635 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1637 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
1642 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1645 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1648 /* Check for any processor-specific program segment types.
1649 If no handler for them, default to making "segment" sections. */
1650 bed
= get_elf_backend_data (abfd
);
1651 if (bed
->elf_backend_section_from_phdr
)
1652 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1654 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1658 /* Initialize REL_HDR, the section-header for new section, containing
1659 relocations against ASECT. If USE_RELA_P is true, we use RELA
1660 relocations; otherwise, we use REL relocations. */
1663 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1665 Elf_Internal_Shdr
*rel_hdr
;
1670 struct elf_backend_data
*bed
;
1672 bed
= get_elf_backend_data (abfd
);
1673 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1676 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1678 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1680 if (rel_hdr
->sh_name
== (unsigned int) -1)
1682 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1683 rel_hdr
->sh_entsize
= (use_rela_p
1684 ? bed
->s
->sizeof_rela
1685 : bed
->s
->sizeof_rel
);
1686 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1687 rel_hdr
->sh_flags
= 0;
1688 rel_hdr
->sh_addr
= 0;
1689 rel_hdr
->sh_size
= 0;
1690 rel_hdr
->sh_offset
= 0;
1695 /* Set up an ELF internal section header for a section. */
1698 elf_fake_sections (abfd
, asect
, failedptrarg
)
1703 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1704 boolean
*failedptr
= (boolean
*) failedptrarg
;
1705 Elf_Internal_Shdr
*this_hdr
;
1709 /* We already failed; just get out of the bfd_map_over_sections
1714 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1716 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1719 if (this_hdr
->sh_name
== (unsigned long) -1)
1725 this_hdr
->sh_flags
= 0;
1727 if ((asect
->flags
& SEC_ALLOC
) != 0
1728 || asect
->user_set_vma
)
1729 this_hdr
->sh_addr
= asect
->vma
;
1731 this_hdr
->sh_addr
= 0;
1733 this_hdr
->sh_offset
= 0;
1734 this_hdr
->sh_size
= asect
->_raw_size
;
1735 this_hdr
->sh_link
= 0;
1736 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1737 /* The sh_entsize and sh_info fields may have been set already by
1738 copy_private_section_data. */
1740 this_hdr
->bfd_section
= asect
;
1741 this_hdr
->contents
= NULL
;
1743 /* FIXME: This should not be based on section names. */
1744 if (strcmp (asect
->name
, ".dynstr") == 0)
1745 this_hdr
->sh_type
= SHT_STRTAB
;
1746 else if (strcmp (asect
->name
, ".hash") == 0)
1748 this_hdr
->sh_type
= SHT_HASH
;
1749 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1751 else if (strcmp (asect
->name
, ".dynsym") == 0)
1753 this_hdr
->sh_type
= SHT_DYNSYM
;
1754 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1756 else if (strcmp (asect
->name
, ".dynamic") == 0)
1758 this_hdr
->sh_type
= SHT_DYNAMIC
;
1759 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1761 else if (strncmp (asect
->name
, ".rela", 5) == 0
1762 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1764 this_hdr
->sh_type
= SHT_RELA
;
1765 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1767 else if (strncmp (asect
->name
, ".rel", 4) == 0
1768 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1770 this_hdr
->sh_type
= SHT_REL
;
1771 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1773 else if (strncmp (asect
->name
, ".note", 5) == 0)
1774 this_hdr
->sh_type
= SHT_NOTE
;
1775 else if (strncmp (asect
->name
, ".stab", 5) == 0
1776 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1777 this_hdr
->sh_type
= SHT_STRTAB
;
1778 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1780 this_hdr
->sh_type
= SHT_GNU_versym
;
1781 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1783 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1785 this_hdr
->sh_type
= SHT_GNU_verdef
;
1786 this_hdr
->sh_entsize
= 0;
1787 /* objcopy or strip will copy over sh_info, but may not set
1788 cverdefs. The linker will set cverdefs, but sh_info will be
1790 if (this_hdr
->sh_info
== 0)
1791 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1793 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1794 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1796 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1798 this_hdr
->sh_type
= SHT_GNU_verneed
;
1799 this_hdr
->sh_entsize
= 0;
1800 /* objcopy or strip will copy over sh_info, but may not set
1801 cverrefs. The linker will set cverrefs, but sh_info will be
1803 if (this_hdr
->sh_info
== 0)
1804 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1806 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1807 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1809 else if ((asect
->flags
& SEC_ALLOC
) != 0
1810 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
1811 this_hdr
->sh_type
= SHT_NOBITS
;
1813 this_hdr
->sh_type
= SHT_PROGBITS
;
1815 if ((asect
->flags
& SEC_ALLOC
) != 0)
1816 this_hdr
->sh_flags
|= SHF_ALLOC
;
1817 if ((asect
->flags
& SEC_READONLY
) == 0)
1818 this_hdr
->sh_flags
|= SHF_WRITE
;
1819 if ((asect
->flags
& SEC_CODE
) != 0)
1820 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1821 if ((asect
->flags
& SEC_MERGE
) != 0)
1823 this_hdr
->sh_flags
|= SHF_MERGE
;
1824 this_hdr
->sh_entsize
= asect
->entsize
;
1825 if ((asect
->flags
& SEC_STRINGS
) != 0)
1826 this_hdr
->sh_flags
|= SHF_STRINGS
;
1829 /* Check for processor-specific section types. */
1830 if (bed
->elf_backend_fake_sections
)
1831 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1833 /* If the section has relocs, set up a section header for the
1834 SHT_REL[A] section. If two relocation sections are required for
1835 this section, it is up to the processor-specific back-end to
1836 create the other. */
1837 if ((asect
->flags
& SEC_RELOC
) != 0
1838 && !_bfd_elf_init_reloc_shdr (abfd
,
1839 &elf_section_data (asect
)->rel_hdr
,
1841 elf_section_data (asect
)->use_rela_p
))
1845 /* Assign all ELF section numbers. The dummy first section is handled here
1846 too. The link/info pointers for the standard section types are filled
1847 in here too, while we're at it. */
1850 assign_section_numbers (abfd
)
1853 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1855 unsigned int section_number
;
1856 Elf_Internal_Shdr
**i_shdrp
;
1860 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1862 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1864 d
->this_idx
= section_number
++;
1865 if ((sec
->flags
& SEC_RELOC
) == 0)
1868 d
->rel_idx
= section_number
++;
1871 d
->rel_idx2
= section_number
++;
1876 t
->shstrtab_section
= section_number
++;
1877 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1878 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1880 if (bfd_get_symcount (abfd
) > 0)
1882 t
->symtab_section
= section_number
++;
1883 t
->strtab_section
= section_number
++;
1886 elf_elfheader (abfd
)->e_shnum
= section_number
;
1888 /* Set up the list of section header pointers, in agreement with the
1890 i_shdrp
= ((Elf_Internal_Shdr
**)
1891 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1892 if (i_shdrp
== NULL
)
1895 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1896 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1897 if (i_shdrp
[0] == NULL
)
1899 bfd_release (abfd
, i_shdrp
);
1902 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1904 elf_elfsections (abfd
) = i_shdrp
;
1906 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1907 if (bfd_get_symcount (abfd
) > 0)
1909 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1910 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1911 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1913 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1915 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1919 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1920 if (d
->rel_idx
!= 0)
1921 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1922 if (d
->rel_idx2
!= 0)
1923 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1925 /* Fill in the sh_link and sh_info fields while we're at it. */
1927 /* sh_link of a reloc section is the section index of the symbol
1928 table. sh_info is the section index of the section to which
1929 the relocation entries apply. */
1930 if (d
->rel_idx
!= 0)
1932 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1933 d
->rel_hdr
.sh_info
= d
->this_idx
;
1935 if (d
->rel_idx2
!= 0)
1937 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1938 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1941 switch (d
->this_hdr
.sh_type
)
1945 /* A reloc section which we are treating as a normal BFD
1946 section. sh_link is the section index of the symbol
1947 table. sh_info is the section index of the section to
1948 which the relocation entries apply. We assume that an
1949 allocated reloc section uses the dynamic symbol table.
1950 FIXME: How can we be sure? */
1951 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1953 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1955 /* We look up the section the relocs apply to by name. */
1957 if (d
->this_hdr
.sh_type
== SHT_REL
)
1961 s
= bfd_get_section_by_name (abfd
, name
);
1963 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1967 /* We assume that a section named .stab*str is a stabs
1968 string section. We look for a section with the same name
1969 but without the trailing ``str'', and set its sh_link
1970 field to point to this section. */
1971 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1972 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1977 len
= strlen (sec
->name
);
1978 alc
= (char *) bfd_malloc (len
- 2);
1981 strncpy (alc
, sec
->name
, len
- 3);
1982 alc
[len
- 3] = '\0';
1983 s
= bfd_get_section_by_name (abfd
, alc
);
1987 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1989 /* This is a .stab section. */
1990 elf_section_data (s
)->this_hdr
.sh_entsize
=
1991 4 + 2 * bfd_get_arch_size (abfd
) / 8;
1998 case SHT_GNU_verneed
:
1999 case SHT_GNU_verdef
:
2000 /* sh_link is the section header index of the string table
2001 used for the dynamic entries, or the symbol table, or the
2003 s
= bfd_get_section_by_name (abfd
, ".dynstr");
2005 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2009 case SHT_GNU_versym
:
2010 /* sh_link is the section header index of the symbol table
2011 this hash table or version table is for. */
2012 s
= bfd_get_section_by_name (abfd
, ".dynsym");
2014 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2022 /* Map symbol from it's internal number to the external number, moving
2023 all local symbols to be at the head of the list. */
2026 sym_is_global (abfd
, sym
)
2030 /* If the backend has a special mapping, use it. */
2031 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2032 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2035 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2036 || bfd_is_und_section (bfd_get_section (sym
))
2037 || bfd_is_com_section (bfd_get_section (sym
)));
2041 elf_map_symbols (abfd
)
2044 int symcount
= bfd_get_symcount (abfd
);
2045 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2046 asymbol
**sect_syms
;
2048 int num_globals
= 0;
2049 int num_locals2
= 0;
2050 int num_globals2
= 0;
2052 int num_sections
= 0;
2059 fprintf (stderr
, "elf_map_symbols\n");
2063 /* Add a section symbol for each BFD section. FIXME: Is this really
2065 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2067 if (max_index
< asect
->index
)
2068 max_index
= asect
->index
;
2072 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
2073 if (sect_syms
== NULL
)
2075 elf_section_syms (abfd
) = sect_syms
;
2077 for (idx
= 0; idx
< symcount
; idx
++)
2081 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2088 if (sec
->owner
!= NULL
)
2090 if (sec
->owner
!= abfd
)
2092 if (sec
->output_offset
!= 0)
2095 sec
= sec
->output_section
;
2097 /* Empty sections in the input files may have had a section
2098 symbol created for them. (See the comment near the end of
2099 _bfd_generic_link_output_symbols in linker.c). If the linker
2100 script discards such sections then we will reach this point.
2101 Since we know that we cannot avoid this case, we detect it
2102 and skip the abort and the assignment to the sect_syms array.
2103 To reproduce this particular case try running the linker
2104 testsuite test ld-scripts/weak.exp for an ELF port that uses
2105 the generic linker. */
2106 if (sec
->owner
== NULL
)
2109 BFD_ASSERT (sec
->owner
== abfd
);
2111 sect_syms
[sec
->index
] = syms
[idx
];
2116 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2118 if (sect_syms
[asect
->index
] != NULL
)
2121 sym
= bfd_make_empty_symbol (abfd
);
2124 sym
->the_bfd
= abfd
;
2125 sym
->name
= asect
->name
;
2127 /* Set the flags to 0 to indicate that this one was newly added. */
2129 sym
->section
= asect
;
2130 sect_syms
[asect
->index
] = sym
;
2134 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2135 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2139 /* Classify all of the symbols. */
2140 for (idx
= 0; idx
< symcount
; idx
++)
2142 if (!sym_is_global (abfd
, syms
[idx
]))
2147 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2149 if (sect_syms
[asect
->index
] != NULL
2150 && sect_syms
[asect
->index
]->flags
== 0)
2152 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2153 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2157 sect_syms
[asect
->index
]->flags
= 0;
2161 /* Now sort the symbols so the local symbols are first. */
2162 new_syms
= ((asymbol
**)
2164 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
2165 if (new_syms
== NULL
)
2168 for (idx
= 0; idx
< symcount
; idx
++)
2170 asymbol
*sym
= syms
[idx
];
2173 if (!sym_is_global (abfd
, sym
))
2176 i
= num_locals
+ num_globals2
++;
2178 sym
->udata
.i
= i
+ 1;
2180 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2182 if (sect_syms
[asect
->index
] != NULL
2183 && sect_syms
[asect
->index
]->flags
== 0)
2185 asymbol
*sym
= sect_syms
[asect
->index
];
2188 sym
->flags
= BSF_SECTION_SYM
;
2189 if (!sym_is_global (abfd
, sym
))
2192 i
= num_locals
+ num_globals2
++;
2194 sym
->udata
.i
= i
+ 1;
2198 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2200 elf_num_locals (abfd
) = num_locals
;
2201 elf_num_globals (abfd
) = num_globals
;
2205 /* Align to the maximum file alignment that could be required for any
2206 ELF data structure. */
2208 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2209 static INLINE file_ptr
2210 align_file_position (off
, align
)
2214 return (off
+ align
- 1) & ~(align
- 1);
2217 /* Assign a file position to a section, optionally aligning to the
2218 required section alignment. */
2221 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2222 Elf_Internal_Shdr
*i_shdrp
;
2230 al
= i_shdrp
->sh_addralign
;
2232 offset
= BFD_ALIGN (offset
, al
);
2234 i_shdrp
->sh_offset
= offset
;
2235 if (i_shdrp
->bfd_section
!= NULL
)
2236 i_shdrp
->bfd_section
->filepos
= offset
;
2237 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2238 offset
+= i_shdrp
->sh_size
;
2242 /* Compute the file positions we are going to put the sections at, and
2243 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2244 is not NULL, this is being called by the ELF backend linker. */
2247 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2249 struct bfd_link_info
*link_info
;
2251 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2253 struct bfd_strtab_hash
*strtab
;
2254 Elf_Internal_Shdr
*shstrtab_hdr
;
2256 if (abfd
->output_has_begun
)
2259 /* Do any elf backend specific processing first. */
2260 if (bed
->elf_backend_begin_write_processing
)
2261 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2263 if (! prep_headers (abfd
))
2266 /* Post process the headers if necessary. */
2267 if (bed
->elf_backend_post_process_headers
)
2268 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2271 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2275 if (!assign_section_numbers (abfd
))
2278 /* The backend linker builds symbol table information itself. */
2279 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2281 /* Non-zero if doing a relocatable link. */
2282 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2284 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2288 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2289 /* sh_name was set in prep_headers. */
2290 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2291 shstrtab_hdr
->sh_flags
= 0;
2292 shstrtab_hdr
->sh_addr
= 0;
2293 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2294 shstrtab_hdr
->sh_entsize
= 0;
2295 shstrtab_hdr
->sh_link
= 0;
2296 shstrtab_hdr
->sh_info
= 0;
2297 /* sh_offset is set in assign_file_positions_except_relocs. */
2298 shstrtab_hdr
->sh_addralign
= 1;
2300 if (!assign_file_positions_except_relocs (abfd
))
2303 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2306 Elf_Internal_Shdr
*hdr
;
2308 off
= elf_tdata (abfd
)->next_file_pos
;
2310 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2311 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2313 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2314 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2316 elf_tdata (abfd
)->next_file_pos
= off
;
2318 /* Now that we know where the .strtab section goes, write it
2320 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2321 || ! _bfd_stringtab_emit (abfd
, strtab
))
2323 _bfd_stringtab_free (strtab
);
2326 abfd
->output_has_begun
= true;
2331 /* Create a mapping from a set of sections to a program segment. */
2333 static INLINE
struct elf_segment_map
*
2334 make_mapping (abfd
, sections
, from
, to
, phdr
)
2336 asection
**sections
;
2341 struct elf_segment_map
*m
;
2345 m
= ((struct elf_segment_map
*)
2347 (sizeof (struct elf_segment_map
)
2348 + (to
- from
- 1) * sizeof (asection
*))));
2352 m
->p_type
= PT_LOAD
;
2353 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2354 m
->sections
[i
- from
] = *hdrpp
;
2355 m
->count
= to
- from
;
2357 if (from
== 0 && phdr
)
2359 /* Include the headers in the first PT_LOAD segment. */
2360 m
->includes_filehdr
= 1;
2361 m
->includes_phdrs
= 1;
2367 /* Set up a mapping from BFD sections to program segments. */
2370 map_sections_to_segments (abfd
)
2373 asection
**sections
= NULL
;
2377 struct elf_segment_map
*mfirst
;
2378 struct elf_segment_map
**pm
;
2379 struct elf_segment_map
*m
;
2381 unsigned int phdr_index
;
2382 bfd_vma maxpagesize
;
2384 boolean phdr_in_segment
= true;
2388 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2391 if (bfd_count_sections (abfd
) == 0)
2394 /* Select the allocated sections, and sort them. */
2396 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2397 * sizeof (asection
*));
2398 if (sections
== NULL
)
2402 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2404 if ((s
->flags
& SEC_ALLOC
) != 0)
2410 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2413 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2415 /* Build the mapping. */
2420 /* If we have a .interp section, then create a PT_PHDR segment for
2421 the program headers and a PT_INTERP segment for the .interp
2423 s
= bfd_get_section_by_name (abfd
, ".interp");
2424 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2426 m
= ((struct elf_segment_map
*)
2427 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2431 m
->p_type
= PT_PHDR
;
2432 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2433 m
->p_flags
= PF_R
| PF_X
;
2434 m
->p_flags_valid
= 1;
2435 m
->includes_phdrs
= 1;
2440 m
= ((struct elf_segment_map
*)
2441 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2445 m
->p_type
= PT_INTERP
;
2453 /* Look through the sections. We put sections in the same program
2454 segment when the start of the second section can be placed within
2455 a few bytes of the end of the first section. */
2458 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2460 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2462 && (dynsec
->flags
& SEC_LOAD
) == 0)
2465 /* Deal with -Ttext or something similar such that the first section
2466 is not adjacent to the program headers. This is an
2467 approximation, since at this point we don't know exactly how many
2468 program headers we will need. */
2471 bfd_size_type phdr_size
;
2473 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2475 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2476 if ((abfd
->flags
& D_PAGED
) == 0
2477 || sections
[0]->lma
< phdr_size
2478 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2479 phdr_in_segment
= false;
2482 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2485 boolean new_segment
;
2489 /* See if this section and the last one will fit in the same
2492 if (last_hdr
== NULL
)
2494 /* If we don't have a segment yet, then we don't need a new
2495 one (we build the last one after this loop). */
2496 new_segment
= false;
2498 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2500 /* If this section has a different relation between the
2501 virtual address and the load address, then we need a new
2505 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2506 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2508 /* If putting this section in this segment would force us to
2509 skip a page in the segment, then we need a new segment. */
2512 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2513 && (hdr
->flags
& SEC_LOAD
) != 0)
2515 /* We don't want to put a loadable section after a
2516 nonloadable section in the same segment. */
2519 else if ((abfd
->flags
& D_PAGED
) == 0)
2521 /* If the file is not demand paged, which means that we
2522 don't require the sections to be correctly aligned in the
2523 file, then there is no other reason for a new segment. */
2524 new_segment
= false;
2527 && (hdr
->flags
& SEC_READONLY
) == 0
2528 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2531 /* We don't want to put a writable section in a read only
2532 segment, unless they are on the same page in memory
2533 anyhow. We already know that the last section does not
2534 bring us past the current section on the page, so the
2535 only case in which the new section is not on the same
2536 page as the previous section is when the previous section
2537 ends precisely on a page boundary. */
2542 /* Otherwise, we can use the same segment. */
2543 new_segment
= false;
2548 if ((hdr
->flags
& SEC_READONLY
) == 0)
2554 /* We need a new program segment. We must create a new program
2555 header holding all the sections from phdr_index until hdr. */
2557 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2564 if ((hdr
->flags
& SEC_READONLY
) == 0)
2571 phdr_in_segment
= false;
2574 /* Create a final PT_LOAD program segment. */
2575 if (last_hdr
!= NULL
)
2577 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2585 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2588 m
= ((struct elf_segment_map
*)
2589 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2593 m
->p_type
= PT_DYNAMIC
;
2595 m
->sections
[0] = dynsec
;
2601 /* For each loadable .note section, add a PT_NOTE segment. We don't
2602 use bfd_get_section_by_name, because if we link together
2603 nonloadable .note sections and loadable .note sections, we will
2604 generate two .note sections in the output file. FIXME: Using
2605 names for section types is bogus anyhow. */
2606 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2608 if ((s
->flags
& SEC_LOAD
) != 0
2609 && strncmp (s
->name
, ".note", 5) == 0)
2611 m
= ((struct elf_segment_map
*)
2612 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2616 m
->p_type
= PT_NOTE
;
2628 elf_tdata (abfd
)->segment_map
= mfirst
;
2632 if (sections
!= NULL
)
2637 /* Sort sections by address. */
2640 elf_sort_sections (arg1
, arg2
)
2644 const asection
*sec1
= *(const asection
**) arg1
;
2645 const asection
*sec2
= *(const asection
**) arg2
;
2647 /* Sort by LMA first, since this is the address used to
2648 place the section into a segment. */
2649 if (sec1
->lma
< sec2
->lma
)
2651 else if (sec1
->lma
> sec2
->lma
)
2654 /* Then sort by VMA. Normally the LMA and the VMA will be
2655 the same, and this will do nothing. */
2656 if (sec1
->vma
< sec2
->vma
)
2658 else if (sec1
->vma
> sec2
->vma
)
2661 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2663 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2668 return sec1
->target_index
- sec2
->target_index
;
2678 /* Sort by size, to put zero sized sections before others at the
2681 if (sec1
->_raw_size
< sec2
->_raw_size
)
2683 if (sec1
->_raw_size
> sec2
->_raw_size
)
2686 return sec1
->target_index
- sec2
->target_index
;
2689 /* Assign file positions to the sections based on the mapping from
2690 sections to segments. This function also sets up some fields in
2691 the file header, and writes out the program headers. */
2694 assign_file_positions_for_segments (abfd
)
2697 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2699 struct elf_segment_map
*m
;
2701 Elf_Internal_Phdr
*phdrs
;
2703 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2704 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2705 Elf_Internal_Phdr
*p
;
2707 if (elf_tdata (abfd
)->segment_map
== NULL
)
2709 if (! map_sections_to_segments (abfd
))
2713 if (bed
->elf_backend_modify_segment_map
)
2715 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2720 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2723 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2724 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2725 elf_elfheader (abfd
)->e_phnum
= count
;
2730 /* If we already counted the number of program segments, make sure
2731 that we allocated enough space. This happens when SIZEOF_HEADERS
2732 is used in a linker script. */
2733 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2734 if (alloc
!= 0 && count
> alloc
)
2736 ((*_bfd_error_handler
)
2737 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2738 bfd_get_filename (abfd
), alloc
, count
));
2739 bfd_set_error (bfd_error_bad_value
);
2746 phdrs
= ((Elf_Internal_Phdr
*)
2747 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2751 off
= bed
->s
->sizeof_ehdr
;
2752 off
+= alloc
* bed
->s
->sizeof_phdr
;
2759 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2766 /* If elf_segment_map is not from map_sections_to_segments, the
2767 sections may not be correctly ordered. */
2769 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2772 p
->p_type
= m
->p_type
;
2773 p
->p_flags
= m
->p_flags
;
2775 if (p
->p_type
== PT_LOAD
2777 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2779 if ((abfd
->flags
& D_PAGED
) != 0)
2780 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2783 bfd_size_type align
;
2786 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2788 bfd_size_type secalign
;
2790 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2791 if (secalign
> align
)
2795 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2802 p
->p_vaddr
= m
->sections
[0]->vma
;
2804 if (m
->p_paddr_valid
)
2805 p
->p_paddr
= m
->p_paddr
;
2806 else if (m
->count
== 0)
2809 p
->p_paddr
= m
->sections
[0]->lma
;
2811 if (p
->p_type
== PT_LOAD
2812 && (abfd
->flags
& D_PAGED
) != 0)
2813 p
->p_align
= bed
->maxpagesize
;
2814 else if (m
->count
== 0)
2815 p
->p_align
= bed
->s
->file_align
;
2823 if (m
->includes_filehdr
)
2825 if (! m
->p_flags_valid
)
2828 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2829 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2832 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2834 if (p
->p_vaddr
< (bfd_vma
) off
)
2836 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2837 bfd_get_filename (abfd
));
2838 bfd_set_error (bfd_error_bad_value
);
2843 if (! m
->p_paddr_valid
)
2846 if (p
->p_type
== PT_LOAD
)
2848 filehdr_vaddr
= p
->p_vaddr
;
2849 filehdr_paddr
= p
->p_paddr
;
2853 if (m
->includes_phdrs
)
2855 if (! m
->p_flags_valid
)
2858 if (m
->includes_filehdr
)
2860 if (p
->p_type
== PT_LOAD
)
2862 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2863 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2868 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2872 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2873 p
->p_vaddr
-= off
- p
->p_offset
;
2874 if (! m
->p_paddr_valid
)
2875 p
->p_paddr
-= off
- p
->p_offset
;
2878 if (p
->p_type
== PT_LOAD
)
2880 phdrs_vaddr
= p
->p_vaddr
;
2881 phdrs_paddr
= p
->p_paddr
;
2884 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2887 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2888 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2891 if (p
->p_type
== PT_LOAD
2892 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2894 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2900 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2901 p
->p_filesz
+= adjust
;
2902 p
->p_memsz
+= adjust
;
2908 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2912 bfd_size_type align
;
2916 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2918 /* The section may have artificial alignment forced by a
2919 link script. Notice this case by the gap between the
2920 cumulative phdr vma and the section's vma. */
2921 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2923 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2925 p
->p_memsz
+= adjust
;
2928 if ((flags
& SEC_LOAD
) != 0)
2929 p
->p_filesz
+= adjust
;
2932 if (p
->p_type
== PT_LOAD
)
2934 bfd_signed_vma adjust
;
2936 if ((flags
& SEC_LOAD
) != 0)
2938 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2942 else if ((flags
& SEC_ALLOC
) != 0)
2944 /* The section VMA must equal the file position
2945 modulo the page size. FIXME: I'm not sure if
2946 this adjustment is really necessary. We used to
2947 not have the SEC_LOAD case just above, and then
2948 this was necessary, but now I'm not sure. */
2949 if ((abfd
->flags
& D_PAGED
) != 0)
2950 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2952 adjust
= (sec
->vma
- voff
) % align
;
2961 (* _bfd_error_handler
)
2962 (_("Error: First section in segment (%s) starts at 0x%x"),
2963 bfd_section_name (abfd
, sec
), sec
->lma
);
2964 (* _bfd_error_handler
)
2965 (_(" whereas segment starts at 0x%x"),
2970 p
->p_memsz
+= adjust
;
2973 if ((flags
& SEC_LOAD
) != 0)
2974 p
->p_filesz
+= adjust
;
2979 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2980 used in a linker script we may have a section with
2981 SEC_LOAD clear but which is supposed to have
2983 if ((flags
& SEC_LOAD
) != 0
2984 || (flags
& SEC_HAS_CONTENTS
) != 0)
2985 off
+= sec
->_raw_size
;
2987 if ((flags
& SEC_ALLOC
) != 0)
2988 voff
+= sec
->_raw_size
;
2991 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
2993 /* The actual "note" segment has i == 0.
2994 This is the one that actually contains everything. */
2998 p
->p_filesz
= sec
->_raw_size
;
2999 off
+= sec
->_raw_size
;
3004 /* Fake sections -- don't need to be written. */
3007 flags
= sec
->flags
= 0;
3014 p
->p_memsz
+= sec
->_raw_size
;
3016 if ((flags
& SEC_LOAD
) != 0)
3017 p
->p_filesz
+= sec
->_raw_size
;
3019 if (align
> p
->p_align
3020 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3024 if (! m
->p_flags_valid
)
3027 if ((flags
& SEC_CODE
) != 0)
3029 if ((flags
& SEC_READONLY
) == 0)
3035 /* Now that we have set the section file positions, we can set up
3036 the file positions for the non PT_LOAD segments. */
3037 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3041 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3043 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3044 p
->p_offset
= m
->sections
[0]->filepos
;
3048 if (m
->includes_filehdr
)
3050 p
->p_vaddr
= filehdr_vaddr
;
3051 if (! m
->p_paddr_valid
)
3052 p
->p_paddr
= filehdr_paddr
;
3054 else if (m
->includes_phdrs
)
3056 p
->p_vaddr
= phdrs_vaddr
;
3057 if (! m
->p_paddr_valid
)
3058 p
->p_paddr
= phdrs_paddr
;
3063 /* Clear out any program headers we allocated but did not use. */
3064 for (; count
< alloc
; count
++, p
++)
3066 memset (p
, 0, sizeof *p
);
3067 p
->p_type
= PT_NULL
;
3070 elf_tdata (abfd
)->phdr
= phdrs
;
3072 elf_tdata (abfd
)->next_file_pos
= off
;
3074 /* Write out the program headers. */
3075 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3076 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3082 /* Get the size of the program header.
3084 If this is called by the linker before any of the section VMA's are set, it
3085 can't calculate the correct value for a strange memory layout. This only
3086 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3087 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3088 data segment (exclusive of .interp and .dynamic).
3090 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3091 will be two segments. */
3093 static bfd_size_type
3094 get_program_header_size (abfd
)
3099 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3101 /* We can't return a different result each time we're called. */
3102 if (elf_tdata (abfd
)->program_header_size
!= 0)
3103 return elf_tdata (abfd
)->program_header_size
;
3105 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3107 struct elf_segment_map
*m
;
3110 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3112 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3113 return elf_tdata (abfd
)->program_header_size
;
3116 /* Assume we will need exactly two PT_LOAD segments: one for text
3117 and one for data. */
3120 s
= bfd_get_section_by_name (abfd
, ".interp");
3121 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3123 /* If we have a loadable interpreter section, we need a
3124 PT_INTERP segment. In this case, assume we also need a
3125 PT_PHDR segment, although that may not be true for all
3130 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3132 /* We need a PT_DYNAMIC segment. */
3136 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3138 if ((s
->flags
& SEC_LOAD
) != 0
3139 && strncmp (s
->name
, ".note", 5) == 0)
3141 /* We need a PT_NOTE segment. */
3146 /* Let the backend count up any program headers it might need. */
3147 if (bed
->elf_backend_additional_program_headers
)
3151 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3157 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3158 return elf_tdata (abfd
)->program_header_size
;
3161 /* Work out the file positions of all the sections. This is called by
3162 _bfd_elf_compute_section_file_positions. All the section sizes and
3163 VMAs must be known before this is called.
3165 We do not consider reloc sections at this point, unless they form
3166 part of the loadable image. Reloc sections are assigned file
3167 positions in assign_file_positions_for_relocs, which is called by
3168 write_object_contents and final_link.
3170 We also don't set the positions of the .symtab and .strtab here. */
3173 assign_file_positions_except_relocs (abfd
)
3176 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3177 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3178 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3180 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3182 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3183 && bfd_get_format (abfd
) != bfd_core
)
3185 Elf_Internal_Shdr
**hdrpp
;
3188 /* Start after the ELF header. */
3189 off
= i_ehdrp
->e_ehsize
;
3191 /* We are not creating an executable, which means that we are
3192 not creating a program header, and that the actual order of
3193 the sections in the file is unimportant. */
3194 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3196 Elf_Internal_Shdr
*hdr
;
3199 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3201 hdr
->sh_offset
= -1;
3204 if (i
== tdata
->symtab_section
3205 || i
== tdata
->strtab_section
)
3207 hdr
->sh_offset
= -1;
3211 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3217 Elf_Internal_Shdr
**hdrpp
;
3219 /* Assign file positions for the loaded sections based on the
3220 assignment of sections to segments. */
3221 if (! assign_file_positions_for_segments (abfd
))
3224 /* Assign file positions for the other sections. */
3226 off
= elf_tdata (abfd
)->next_file_pos
;
3227 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3229 Elf_Internal_Shdr
*hdr
;
3232 if (hdr
->bfd_section
!= NULL
3233 && hdr
->bfd_section
->filepos
!= 0)
3234 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3235 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3237 ((*_bfd_error_handler
)
3238 (_("%s: warning: allocated section `%s' not in segment"),
3239 bfd_get_filename (abfd
),
3240 (hdr
->bfd_section
== NULL
3242 : hdr
->bfd_section
->name
)));
3243 if ((abfd
->flags
& D_PAGED
) != 0)
3244 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3246 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3247 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3250 else if (hdr
->sh_type
== SHT_REL
3251 || hdr
->sh_type
== SHT_RELA
3252 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3253 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3254 hdr
->sh_offset
= -1;
3256 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3260 /* Place the section headers. */
3261 off
= align_file_position (off
, bed
->s
->file_align
);
3262 i_ehdrp
->e_shoff
= off
;
3263 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3265 elf_tdata (abfd
)->next_file_pos
= off
;
3274 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3275 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3276 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3278 struct bfd_strtab_hash
*shstrtab
;
3279 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3281 i_ehdrp
= elf_elfheader (abfd
);
3282 i_shdrp
= elf_elfsections (abfd
);
3284 shstrtab
= _bfd_elf_stringtab_init ();
3285 if (shstrtab
== NULL
)
3288 elf_shstrtab (abfd
) = shstrtab
;
3290 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3291 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3292 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3293 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3295 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3296 i_ehdrp
->e_ident
[EI_DATA
] =
3297 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3298 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3300 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3301 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3303 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3304 i_ehdrp
->e_ident
[count
] = 0;
3306 if ((abfd
->flags
& DYNAMIC
) != 0)
3307 i_ehdrp
->e_type
= ET_DYN
;
3308 else if ((abfd
->flags
& EXEC_P
) != 0)
3309 i_ehdrp
->e_type
= ET_EXEC
;
3310 else if (bfd_get_format (abfd
) == bfd_core
)
3311 i_ehdrp
->e_type
= ET_CORE
;
3313 i_ehdrp
->e_type
= ET_REL
;
3315 switch (bfd_get_arch (abfd
))
3317 case bfd_arch_unknown
:
3318 i_ehdrp
->e_machine
= EM_NONE
;
3320 case bfd_arch_sparc
:
3321 if (bfd_get_arch_size (abfd
) == 64)
3322 i_ehdrp
->e_machine
= EM_SPARCV9
;
3324 i_ehdrp
->e_machine
= EM_SPARC
;
3327 i_ehdrp
->e_machine
= EM_S370
;
3330 if (bfd_get_arch_size (abfd
) == 64)
3331 i_ehdrp
->e_machine
= EM_X86_64
;
3333 i_ehdrp
->e_machine
= EM_386
;
3336 i_ehdrp
->e_machine
= EM_IA_64
;
3338 case bfd_arch_m68hc11
:
3339 i_ehdrp
->e_machine
= EM_68HC11
;
3341 case bfd_arch_m68hc12
:
3342 i_ehdrp
->e_machine
= EM_68HC12
;
3345 i_ehdrp
->e_machine
= EM_S390
;
3348 i_ehdrp
->e_machine
= EM_68K
;
3351 i_ehdrp
->e_machine
= EM_88K
;
3354 i_ehdrp
->e_machine
= EM_860
;
3357 i_ehdrp
->e_machine
= EM_960
;
3359 case bfd_arch_mips
: /* MIPS Rxxxx */
3360 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3363 i_ehdrp
->e_machine
= EM_PARISC
;
3365 case bfd_arch_powerpc
:
3366 i_ehdrp
->e_machine
= EM_PPC
;
3368 case bfd_arch_alpha
:
3369 i_ehdrp
->e_machine
= EM_ALPHA
;
3372 i_ehdrp
->e_machine
= EM_SH
;
3375 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3378 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3381 i_ehdrp
->e_machine
= EM_CYGNUS_FR30
;
3383 case bfd_arch_mcore
:
3384 i_ehdrp
->e_machine
= EM_MCORE
;
3387 i_ehdrp
->e_machine
= EM_AVR
;
3390 switch (bfd_get_mach (abfd
))
3393 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3397 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3400 i_ehdrp
->e_machine
= EM_ARM
;
3403 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3405 case bfd_arch_mn10200
:
3406 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3408 case bfd_arch_mn10300
:
3409 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3412 i_ehdrp
->e_machine
= EM_PJ
;
3415 i_ehdrp
->e_machine
= EM_CRIS
;
3417 case bfd_arch_openrisc
:
3418 i_ehdrp
->e_machine
= EM_OPENRISC
;
3420 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3422 i_ehdrp
->e_machine
= EM_NONE
;
3424 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3425 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3427 /* No program header, for now. */
3428 i_ehdrp
->e_phoff
= 0;
3429 i_ehdrp
->e_phentsize
= 0;
3430 i_ehdrp
->e_phnum
= 0;
3432 /* Each bfd section is section header entry. */
3433 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3434 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3436 /* If we're building an executable, we'll need a program header table. */
3437 if (abfd
->flags
& EXEC_P
)
3439 /* It all happens later. */
3441 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3443 /* elf_build_phdrs() returns a (NULL-terminated) array of
3444 Elf_Internal_Phdrs. */
3445 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3446 i_ehdrp
->e_phoff
= outbase
;
3447 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3452 i_ehdrp
->e_phentsize
= 0;
3454 i_ehdrp
->e_phoff
= 0;
3457 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3458 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3459 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3460 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3461 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3462 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3463 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3464 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3465 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3471 /* Assign file positions for all the reloc sections which are not part
3472 of the loadable file image. */
3475 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3480 Elf_Internal_Shdr
**shdrpp
;
3482 off
= elf_tdata (abfd
)->next_file_pos
;
3484 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3485 i
< elf_elfheader (abfd
)->e_shnum
;
3488 Elf_Internal_Shdr
*shdrp
;
3491 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3492 && shdrp
->sh_offset
== -1)
3493 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3496 elf_tdata (abfd
)->next_file_pos
= off
;
3500 _bfd_elf_write_object_contents (abfd
)
3503 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3504 Elf_Internal_Ehdr
*i_ehdrp
;
3505 Elf_Internal_Shdr
**i_shdrp
;
3509 if (! abfd
->output_has_begun
3510 && ! _bfd_elf_compute_section_file_positions
3511 (abfd
, (struct bfd_link_info
*) NULL
))
3514 i_shdrp
= elf_elfsections (abfd
);
3515 i_ehdrp
= elf_elfheader (abfd
);
3518 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3522 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3524 /* After writing the headers, we need to write the sections too... */
3525 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3527 if (bed
->elf_backend_section_processing
)
3528 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3529 if (i_shdrp
[count
]->contents
)
3531 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3532 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3534 != i_shdrp
[count
]->sh_size
))
3539 /* Write out the section header names. */
3540 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3541 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3544 if (bed
->elf_backend_final_write_processing
)
3545 (*bed
->elf_backend_final_write_processing
) (abfd
,
3546 elf_tdata (abfd
)->linker
);
3548 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3552 _bfd_elf_write_corefile_contents (abfd
)
3555 /* Hopefully this can be done just like an object file. */
3556 return _bfd_elf_write_object_contents (abfd
);
3559 /* Given a section, search the header to find them. */
3562 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3566 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3567 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3569 Elf_Internal_Shdr
*hdr
;
3570 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3572 for (index
= 0; index
< maxindex
; index
++)
3574 hdr
= i_shdrp
[index
];
3575 if (hdr
->bfd_section
== asect
)
3579 if (bed
->elf_backend_section_from_bfd_section
)
3581 for (index
= 0; index
< maxindex
; index
++)
3585 hdr
= i_shdrp
[index
];
3587 if ((*bed
->elf_backend_section_from_bfd_section
)
3588 (abfd
, hdr
, asect
, &retval
))
3593 if (bfd_is_abs_section (asect
))
3595 if (bfd_is_com_section (asect
))
3597 if (bfd_is_und_section (asect
))
3600 bfd_set_error (bfd_error_nonrepresentable_section
);
3605 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3609 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3611 asymbol
**asym_ptr_ptr
;
3613 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3615 flagword flags
= asym_ptr
->flags
;
3617 /* When gas creates relocations against local labels, it creates its
3618 own symbol for the section, but does put the symbol into the
3619 symbol chain, so udata is 0. When the linker is generating
3620 relocatable output, this section symbol may be for one of the
3621 input sections rather than the output section. */
3622 if (asym_ptr
->udata
.i
== 0
3623 && (flags
& BSF_SECTION_SYM
)
3624 && asym_ptr
->section
)
3628 if (asym_ptr
->section
->output_section
!= NULL
)
3629 indx
= asym_ptr
->section
->output_section
->index
;
3631 indx
= asym_ptr
->section
->index
;
3632 if (elf_section_syms (abfd
)[indx
])
3633 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3636 idx
= asym_ptr
->udata
.i
;
3640 /* This case can occur when using --strip-symbol on a symbol
3641 which is used in a relocation entry. */
3642 (*_bfd_error_handler
)
3643 (_("%s: symbol `%s' required but not present"),
3644 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3645 bfd_set_error (bfd_error_no_symbols
);
3652 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3653 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3654 elf_symbol_flags (flags
));
3662 /* Copy private BFD data. This copies any program header information. */
3665 copy_private_bfd_data (ibfd
, obfd
)
3669 Elf_Internal_Ehdr
* iehdr
;
3670 struct elf_segment_map
* map
;
3671 struct elf_segment_map
* map_first
;
3672 struct elf_segment_map
** pointer_to_map
;
3673 Elf_Internal_Phdr
* segment
;
3676 unsigned int num_segments
;
3677 boolean phdr_included
= false;
3678 bfd_vma maxpagesize
;
3679 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3680 unsigned int phdr_adjust_num
= 0;
3682 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3683 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3686 if (elf_tdata (ibfd
)->phdr
== NULL
)
3689 iehdr
= elf_elfheader (ibfd
);
3692 pointer_to_map
= &map_first
;
3694 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3695 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3697 /* Returns the end address of the segment + 1. */
3698 #define SEGMENT_END(segment, start) \
3699 (start + (segment->p_memsz > segment->p_filesz \
3700 ? segment->p_memsz : segment->p_filesz))
3702 /* Returns true if the given section is contained within
3703 the given segment. VMA addresses are compared. */
3704 #define IS_CONTAINED_BY_VMA(section, segment) \
3705 (section->vma >= segment->p_vaddr \
3706 && (section->vma + section->_raw_size) \
3707 <= (SEGMENT_END (segment, segment->p_vaddr)))
3709 /* Returns true if the given section is contained within
3710 the given segment. LMA addresses are compared. */
3711 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3712 (section->lma >= base \
3713 && (section->lma + section->_raw_size) \
3714 <= SEGMENT_END (segment, base))
3716 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3717 #define IS_COREFILE_NOTE(p, s) \
3718 (p->p_type == PT_NOTE \
3719 && bfd_get_format (ibfd) == bfd_core \
3720 && s->vma == 0 && s->lma == 0 \
3721 && (bfd_vma) s->filepos >= p->p_offset \
3722 && (bfd_vma) s->filepos + s->_raw_size \
3723 <= p->p_offset + p->p_filesz)
3725 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3726 linker, which generates a PT_INTERP section with p_vaddr and
3727 p_memsz set to 0. */
3728 #define IS_SOLARIS_PT_INTERP(p, s) \
3730 && p->p_filesz > 0 \
3731 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3732 && s->_raw_size > 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 /* Decide if the given section should be included in the given segment.
3738 A section will be included if:
3739 1. It is within the address space of the segment,
3740 2. It is an allocated segment,
3741 3. There is an output section associated with it,
3742 4. The section has not already been allocated to a previous segment. */
3743 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3744 ((((IS_CONTAINED_BY_VMA (section, segment) \
3745 || IS_SOLARIS_PT_INTERP (segment, section)) \
3746 && (section->flags & SEC_ALLOC) != 0) \
3747 || IS_COREFILE_NOTE (segment, section)) \
3748 && section->output_section != NULL \
3749 && section->segment_mark == false)
3751 /* Returns true iff seg1 starts after the end of seg2. */
3752 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3753 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3755 /* Returns true iff seg1 and seg2 overlap. */
3756 #define SEGMENT_OVERLAPS(seg1, seg2) \
3757 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3759 /* Initialise the segment mark field. */
3760 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3761 section
->segment_mark
= false;
3763 /* Scan through the segments specified in the program header
3764 of the input BFD. For this first scan we look for overlaps
3765 in the loadable segments. These can be created by wierd
3766 parameters to objcopy. */
3767 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3772 Elf_Internal_Phdr
*segment2
;
3774 if (segment
->p_type
!= PT_LOAD
)
3777 /* Determine if this segment overlaps any previous segments. */
3778 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
3780 bfd_signed_vma extra_length
;
3782 if (segment2
->p_type
!= PT_LOAD
3783 || ! SEGMENT_OVERLAPS (segment
, segment2
))
3786 /* Merge the two segments together. */
3787 if (segment2
->p_vaddr
< segment
->p_vaddr
)
3789 /* Extend SEGMENT2 to include SEGMENT and then delete
3792 SEGMENT_END (segment
, segment
->p_vaddr
)
3793 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
3795 if (extra_length
> 0)
3797 segment2
->p_memsz
+= extra_length
;
3798 segment2
->p_filesz
+= extra_length
;
3801 segment
->p_type
= PT_NULL
;
3803 /* Since we have deleted P we must restart the outer loop. */
3805 segment
= elf_tdata (ibfd
)->phdr
;
3810 /* Extend SEGMENT to include SEGMENT2 and then delete
3813 SEGMENT_END (segment2
, segment2
->p_vaddr
)
3814 - SEGMENT_END (segment
, segment
->p_vaddr
);
3816 if (extra_length
> 0)
3818 segment
->p_memsz
+= extra_length
;
3819 segment
->p_filesz
+= extra_length
;
3822 segment2
->p_type
= PT_NULL
;
3827 /* The second scan attempts to assign sections to segments. */
3828 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3832 unsigned int section_count
;
3833 asection
** sections
;
3834 asection
* output_section
;
3836 bfd_vma matching_lma
;
3837 bfd_vma suggested_lma
;
3840 if (segment
->p_type
== PT_NULL
)
3843 /* Compute how many sections might be placed into this segment. */
3845 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3846 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3849 /* Allocate a segment map big enough to contain all of the
3850 sections we have selected. */
3851 map
= ((struct elf_segment_map
*)
3853 (sizeof (struct elf_segment_map
)
3854 + ((size_t) section_count
- 1) * sizeof (asection
*))));
3858 /* Initialise the fields of the segment map. Default to
3859 using the physical address of the segment in the input BFD. */
3861 map
->p_type
= segment
->p_type
;
3862 map
->p_flags
= segment
->p_flags
;
3863 map
->p_flags_valid
= 1;
3864 map
->p_paddr
= segment
->p_paddr
;
3865 map
->p_paddr_valid
= 1;
3867 /* Determine if this segment contains the ELF file header
3868 and if it contains the program headers themselves. */
3869 map
->includes_filehdr
= (segment
->p_offset
== 0
3870 && segment
->p_filesz
>= iehdr
->e_ehsize
);
3872 map
->includes_phdrs
= 0;
3874 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
3876 map
->includes_phdrs
=
3877 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3878 && (segment
->p_offset
+ segment
->p_filesz
3879 >= ((bfd_vma
) iehdr
->e_phoff
3880 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3882 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
3883 phdr_included
= true;
3886 if (section_count
== 0)
3888 /* Special segments, such as the PT_PHDR segment, may contain
3889 no sections, but ordinary, loadable segments should contain
3891 if (segment
->p_type
== PT_LOAD
)
3893 (_("%s: warning: Empty loadable segment detected\n"),
3894 bfd_get_filename (ibfd
));
3897 *pointer_to_map
= map
;
3898 pointer_to_map
= &map
->next
;
3903 /* Now scan the sections in the input BFD again and attempt
3904 to add their corresponding output sections to the segment map.
3905 The problem here is how to handle an output section which has
3906 been moved (ie had its LMA changed). There are four possibilities:
3908 1. None of the sections have been moved.
3909 In this case we can continue to use the segment LMA from the
3912 2. All of the sections have been moved by the same amount.
3913 In this case we can change the segment's LMA to match the LMA
3914 of the first section.
3916 3. Some of the sections have been moved, others have not.
3917 In this case those sections which have not been moved can be
3918 placed in the current segment which will have to have its size,
3919 and possibly its LMA changed, and a new segment or segments will
3920 have to be created to contain the other sections.
3922 4. The sections have been moved, but not be the same amount.
3923 In this case we can change the segment's LMA to match the LMA
3924 of the first section and we will have to create a new segment
3925 or segments to contain the other sections.
3927 In order to save time, we allocate an array to hold the section
3928 pointers that we are interested in. As these sections get assigned
3929 to a segment, they are removed from this array. */
3931 sections
= (asection
**) bfd_malloc
3932 (sizeof (asection
*) * section_count
);
3933 if (sections
== NULL
)
3936 /* Step One: Scan for segment vs section LMA conflicts.
3937 Also add the sections to the section array allocated above.
3938 Also add the sections to the current segment. In the common
3939 case, where the sections have not been moved, this means that
3940 we have completely filled the segment, and there is nothing
3946 for (j
= 0, section
= ibfd
->sections
;
3948 section
= section
->next
)
3950 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3952 output_section
= section
->output_section
;
3954 sections
[j
++] = section
;
3956 /* The Solaris native linker always sets p_paddr to 0.
3957 We try to catch that case here, and set it to the
3959 if (segment
->p_paddr
== 0
3960 && segment
->p_vaddr
!= 0
3962 && output_section
->lma
!= 0
3963 && (output_section
->vma
== (segment
->p_vaddr
3964 + (map
->includes_filehdr
3967 + (map
->includes_phdrs
3968 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3970 map
->p_paddr
= segment
->p_vaddr
;
3972 /* Match up the physical address of the segment with the
3973 LMA address of the output section. */
3974 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
3975 || IS_COREFILE_NOTE (segment
, section
))
3977 if (matching_lma
== 0)
3978 matching_lma
= output_section
->lma
;
3980 /* We assume that if the section fits within the segment
3981 then it does not overlap any other section within that
3983 map
->sections
[isec
++] = output_section
;
3985 else if (suggested_lma
== 0)
3986 suggested_lma
= output_section
->lma
;
3990 BFD_ASSERT (j
== section_count
);
3992 /* Step Two: Adjust the physical address of the current segment,
3994 if (isec
== section_count
)
3996 /* All of the sections fitted within the segment as currently
3997 specified. This is the default case. Add the segment to
3998 the list of built segments and carry on to process the next
3999 program header in the input BFD. */
4000 map
->count
= section_count
;
4001 *pointer_to_map
= map
;
4002 pointer_to_map
= &map
->next
;
4009 if (matching_lma
!= 0)
4011 /* At least one section fits inside the current segment.
4012 Keep it, but modify its physical address to match the
4013 LMA of the first section that fitted. */
4014 map
->p_paddr
= matching_lma
;
4018 /* None of the sections fitted inside the current segment.
4019 Change the current segment's physical address to match
4020 the LMA of the first section. */
4021 map
->p_paddr
= suggested_lma
;
4024 /* Offset the segment physical address from the lma
4025 to allow for space taken up by elf headers. */
4026 if (map
->includes_filehdr
)
4027 map
->p_paddr
-= iehdr
->e_ehsize
;
4029 if (map
->includes_phdrs
)
4031 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4033 /* iehdr->e_phnum is just an estimate of the number
4034 of program headers that we will need. Make a note
4035 here of the number we used and the segment we chose
4036 to hold these headers, so that we can adjust the
4037 offset when we know the correct value. */
4038 phdr_adjust_num
= iehdr
->e_phnum
;
4039 phdr_adjust_seg
= map
;
4043 /* Step Three: Loop over the sections again, this time assigning
4044 those that fit to the current segment and remvoing them from the
4045 sections array; but making sure not to leave large gaps. Once all
4046 possible sections have been assigned to the current segment it is
4047 added to the list of built segments and if sections still remain
4048 to be assigned, a new segment is constructed before repeating
4056 /* Fill the current segment with sections that fit. */
4057 for (j
= 0; j
< section_count
; j
++)
4059 section
= sections
[j
];
4061 if (section
== NULL
)
4064 output_section
= section
->output_section
;
4066 BFD_ASSERT (output_section
!= NULL
);
4068 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4069 || IS_COREFILE_NOTE (segment
, section
))
4071 if (map
->count
== 0)
4073 /* If the first section in a segment does not start at
4074 the beginning of the segment, then something is
4076 if (output_section
->lma
!=
4078 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4079 + (map
->includes_phdrs
4080 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4086 asection
* prev_sec
;
4088 prev_sec
= map
->sections
[map
->count
- 1];
4090 /* If the gap between the end of the previous section
4091 and the start of this section is more than
4092 maxpagesize then we need to start a new segment. */
4093 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4094 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4095 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4097 if (suggested_lma
== 0)
4098 suggested_lma
= output_section
->lma
;
4104 map
->sections
[map
->count
++] = output_section
;
4107 section
->segment_mark
= true;
4109 else if (suggested_lma
== 0)
4110 suggested_lma
= output_section
->lma
;
4113 BFD_ASSERT (map
->count
> 0);
4115 /* Add the current segment to the list of built segments. */
4116 *pointer_to_map
= map
;
4117 pointer_to_map
= &map
->next
;
4119 if (isec
< section_count
)
4121 /* We still have not allocated all of the sections to
4122 segments. Create a new segment here, initialise it
4123 and carry on looping. */
4124 map
= ((struct elf_segment_map
*)
4126 (sizeof (struct elf_segment_map
)
4127 + ((size_t) section_count
- 1)
4128 * sizeof (asection
*))));
4132 /* Initialise the fields of the segment map. Set the physical
4133 physical address to the LMA of the first section that has
4134 not yet been assigned. */
4136 map
->p_type
= segment
->p_type
;
4137 map
->p_flags
= segment
->p_flags
;
4138 map
->p_flags_valid
= 1;
4139 map
->p_paddr
= suggested_lma
;
4140 map
->p_paddr_valid
= 1;
4141 map
->includes_filehdr
= 0;
4142 map
->includes_phdrs
= 0;
4145 while (isec
< section_count
);
4150 /* The Solaris linker creates program headers in which all the
4151 p_paddr fields are zero. When we try to objcopy or strip such a
4152 file, we get confused. Check for this case, and if we find it
4153 reset the p_paddr_valid fields. */
4154 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4155 if (map
->p_paddr
!= 0)
4159 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4160 map
->p_paddr_valid
= 0;
4163 elf_tdata (obfd
)->segment_map
= map_first
;
4165 /* If we had to estimate the number of program headers that were
4166 going to be needed, then check our estimate know and adjust
4167 the offset if necessary. */
4168 if (phdr_adjust_seg
!= NULL
)
4172 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4175 if (count
> phdr_adjust_num
)
4176 phdr_adjust_seg
->p_paddr
4177 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4181 /* Final Step: Sort the segments into ascending order of physical
4183 if (map_first
!= NULL
)
4185 struct elf_segment_map
*prev
;
4188 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4190 /* Yes I know - its a bubble sort.... */
4191 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4193 /* Swap map and map->next. */
4194 prev
->next
= map
->next
;
4195 map
->next
= map
->next
->next
;
4196 prev
->next
->next
= map
;
4206 #undef IS_CONTAINED_BY_VMA
4207 #undef IS_CONTAINED_BY_LMA
4208 #undef IS_COREFILE_NOTE
4209 #undef IS_SOLARIS_PT_INTERP
4210 #undef INCLUDE_SECTION_IN_SEGMENT
4211 #undef SEGMENT_AFTER_SEGMENT
4212 #undef SEGMENT_OVERLAPS
4216 /* Copy private section information. This copies over the entsize
4217 field, and sometimes the info field. */
4220 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4226 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4228 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4229 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4232 /* Copy over private BFD data if it has not already been copied.
4233 This must be done here, rather than in the copy_private_bfd_data
4234 entry point, because the latter is called after the section
4235 contents have been set, which means that the program headers have
4236 already been worked out. */
4237 if (elf_tdata (obfd
)->segment_map
== NULL
4238 && elf_tdata (ibfd
)->phdr
!= NULL
)
4242 /* Only set up the segments if there are no more SEC_ALLOC
4243 sections. FIXME: This won't do the right thing if objcopy is
4244 used to remove the last SEC_ALLOC section, since objcopy
4245 won't call this routine in that case. */
4246 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4247 if ((s
->flags
& SEC_ALLOC
) != 0)
4251 if (! copy_private_bfd_data (ibfd
, obfd
))
4256 ihdr
= &elf_section_data (isec
)->this_hdr
;
4257 ohdr
= &elf_section_data (osec
)->this_hdr
;
4259 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4261 if (ihdr
->sh_type
== SHT_SYMTAB
4262 || ihdr
->sh_type
== SHT_DYNSYM
4263 || ihdr
->sh_type
== SHT_GNU_verneed
4264 || ihdr
->sh_type
== SHT_GNU_verdef
)
4265 ohdr
->sh_info
= ihdr
->sh_info
;
4267 elf_section_data (osec
)->use_rela_p
4268 = elf_section_data (isec
)->use_rela_p
;
4273 /* Copy private symbol information. If this symbol is in a section
4274 which we did not map into a BFD section, try to map the section
4275 index correctly. We use special macro definitions for the mapped
4276 section indices; these definitions are interpreted by the
4277 swap_out_syms function. */
4279 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4280 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4281 #define MAP_STRTAB (SHN_LORESERVE - 3)
4282 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4285 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4291 elf_symbol_type
*isym
, *osym
;
4293 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4294 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4297 isym
= elf_symbol_from (ibfd
, isymarg
);
4298 osym
= elf_symbol_from (obfd
, osymarg
);
4302 && bfd_is_abs_section (isym
->symbol
.section
))
4306 shndx
= isym
->internal_elf_sym
.st_shndx
;
4307 if (shndx
== elf_onesymtab (ibfd
))
4308 shndx
= MAP_ONESYMTAB
;
4309 else if (shndx
== elf_dynsymtab (ibfd
))
4310 shndx
= MAP_DYNSYMTAB
;
4311 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4313 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4314 shndx
= MAP_SHSTRTAB
;
4315 osym
->internal_elf_sym
.st_shndx
= shndx
;
4321 /* Swap out the symbols. */
4324 swap_out_syms (abfd
, sttp
, relocatable_p
)
4326 struct bfd_strtab_hash
**sttp
;
4329 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4331 if (!elf_map_symbols (abfd
))
4334 /* Dump out the symtabs. */
4336 int symcount
= bfd_get_symcount (abfd
);
4337 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4338 struct bfd_strtab_hash
*stt
;
4339 Elf_Internal_Shdr
*symtab_hdr
;
4340 Elf_Internal_Shdr
*symstrtab_hdr
;
4341 char *outbound_syms
;
4344 stt
= _bfd_elf_stringtab_init ();
4348 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4349 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4350 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4351 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4352 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4353 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4355 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4356 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4358 outbound_syms
= bfd_alloc (abfd
,
4359 (1 + symcount
) * bed
->s
->sizeof_sym
);
4360 if (outbound_syms
== NULL
)
4362 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4364 /* now generate the data (for "contents") */
4366 /* Fill in zeroth symbol and swap it out. */
4367 Elf_Internal_Sym sym
;
4373 sym
.st_shndx
= SHN_UNDEF
;
4374 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4375 outbound_syms
+= bed
->s
->sizeof_sym
;
4377 for (idx
= 0; idx
< symcount
; idx
++)
4379 Elf_Internal_Sym sym
;
4380 bfd_vma value
= syms
[idx
]->value
;
4381 elf_symbol_type
*type_ptr
;
4382 flagword flags
= syms
[idx
]->flags
;
4385 if ((flags
& BSF_SECTION_SYM
) != 0)
4387 /* Section symbols have no name. */
4392 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4395 if (sym
.st_name
== (unsigned long) -1)
4399 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4401 if ((flags
& BSF_SECTION_SYM
) == 0
4402 && bfd_is_com_section (syms
[idx
]->section
))
4404 /* ELF common symbols put the alignment into the `value' field,
4405 and the size into the `size' field. This is backwards from
4406 how BFD handles it, so reverse it here. */
4407 sym
.st_size
= value
;
4408 if (type_ptr
== NULL
4409 || type_ptr
->internal_elf_sym
.st_value
== 0)
4410 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4412 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4413 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4414 (abfd
, syms
[idx
]->section
);
4418 asection
*sec
= syms
[idx
]->section
;
4421 if (sec
->output_section
)
4423 value
+= sec
->output_offset
;
4424 sec
= sec
->output_section
;
4426 /* Don't add in the section vma for relocatable output. */
4427 if (! relocatable_p
)
4429 sym
.st_value
= value
;
4430 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4432 if (bfd_is_abs_section (sec
)
4434 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4436 /* This symbol is in a real ELF section which we did
4437 not create as a BFD section. Undo the mapping done
4438 by copy_private_symbol_data. */
4439 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4443 shndx
= elf_onesymtab (abfd
);
4446 shndx
= elf_dynsymtab (abfd
);
4449 shndx
= elf_tdata (abfd
)->strtab_section
;
4452 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4460 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4466 /* Writing this would be a hell of a lot easier if
4467 we had some decent documentation on bfd, and
4468 knew what to expect of the library, and what to
4469 demand of applications. For example, it
4470 appears that `objcopy' might not set the
4471 section of a symbol to be a section that is
4472 actually in the output file. */
4473 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4474 BFD_ASSERT (sec2
!= 0);
4475 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4476 BFD_ASSERT (shndx
!= -1);
4480 sym
.st_shndx
= shndx
;
4483 if ((flags
& BSF_FUNCTION
) != 0)
4485 else if ((flags
& BSF_OBJECT
) != 0)
4490 /* Processor-specific types */
4491 if (type_ptr
!= NULL
4492 && bed
->elf_backend_get_symbol_type
)
4493 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4495 if (flags
& BSF_SECTION_SYM
)
4496 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4497 else if (bfd_is_com_section (syms
[idx
]->section
))
4498 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4499 else if (bfd_is_und_section (syms
[idx
]->section
))
4500 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4504 else if (flags
& BSF_FILE
)
4505 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4508 int bind
= STB_LOCAL
;
4510 if (flags
& BSF_LOCAL
)
4512 else if (flags
& BSF_WEAK
)
4514 else if (flags
& BSF_GLOBAL
)
4517 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4520 if (type_ptr
!= NULL
)
4521 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4525 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4526 outbound_syms
+= bed
->s
->sizeof_sym
;
4530 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4531 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4533 symstrtab_hdr
->sh_flags
= 0;
4534 symstrtab_hdr
->sh_addr
= 0;
4535 symstrtab_hdr
->sh_entsize
= 0;
4536 symstrtab_hdr
->sh_link
= 0;
4537 symstrtab_hdr
->sh_info
= 0;
4538 symstrtab_hdr
->sh_addralign
= 1;
4544 /* Return the number of bytes required to hold the symtab vector.
4546 Note that we base it on the count plus 1, since we will null terminate
4547 the vector allocated based on this size. However, the ELF symbol table
4548 always has a dummy entry as symbol #0, so it ends up even. */
4551 _bfd_elf_get_symtab_upper_bound (abfd
)
4556 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4558 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4559 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4565 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4570 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4572 if (elf_dynsymtab (abfd
) == 0)
4574 bfd_set_error (bfd_error_invalid_operation
);
4578 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4579 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4585 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4586 bfd
*abfd ATTRIBUTE_UNUSED
;
4589 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4592 /* Canonicalize the relocs. */
4595 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4604 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4610 tblptr
= section
->relocation
;
4611 for (i
= 0; i
< section
->reloc_count
; i
++)
4612 *relptr
++ = tblptr
++;
4616 return section
->reloc_count
;
4620 _bfd_elf_get_symtab (abfd
, alocation
)
4622 asymbol
**alocation
;
4624 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4625 (abfd
, alocation
, false);
4628 bfd_get_symcount (abfd
) = symcount
;
4633 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4635 asymbol
**alocation
;
4637 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4638 (abfd
, alocation
, true);
4641 /* Return the size required for the dynamic reloc entries. Any
4642 section that was actually installed in the BFD, and has type
4643 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4644 considered to be a dynamic reloc section. */
4647 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4653 if (elf_dynsymtab (abfd
) == 0)
4655 bfd_set_error (bfd_error_invalid_operation
);
4659 ret
= sizeof (arelent
*);
4660 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4661 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4662 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4663 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4664 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4665 * sizeof (arelent
*));
4670 /* Canonicalize the dynamic relocation entries. Note that we return
4671 the dynamic relocations as a single block, although they are
4672 actually associated with particular sections; the interface, which
4673 was designed for SunOS style shared libraries, expects that there
4674 is only one set of dynamic relocs. Any section that was actually
4675 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4676 the dynamic symbol table, is considered to be a dynamic reloc
4680 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4685 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4689 if (elf_dynsymtab (abfd
) == 0)
4691 bfd_set_error (bfd_error_invalid_operation
);
4695 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4697 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4699 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4700 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4701 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4706 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4708 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4710 for (i
= 0; i
< count
; i
++)
4721 /* Read in the version information. */
4724 _bfd_elf_slurp_version_tables (abfd
)
4727 bfd_byte
*contents
= NULL
;
4729 if (elf_dynverdef (abfd
) != 0)
4731 Elf_Internal_Shdr
*hdr
;
4732 Elf_External_Verdef
*everdef
;
4733 Elf_Internal_Verdef
*iverdef
;
4734 Elf_Internal_Verdef
*iverdefarr
;
4735 Elf_Internal_Verdef iverdefmem
;
4737 unsigned int maxidx
;
4739 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4741 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4742 if (contents
== NULL
)
4744 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4745 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4748 /* We know the number of entries in the section but not the maximum
4749 index. Therefore we have to run through all entries and find
4751 everdef
= (Elf_External_Verdef
*) contents
;
4753 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4755 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4757 if ((iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
)) > maxidx
)
4758 maxidx
= iverdefmem
.vd_ndx
& ((unsigned) VERSYM_VERSION
);
4760 everdef
= ((Elf_External_Verdef
*)
4761 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4764 elf_tdata (abfd
)->verdef
=
4765 ((Elf_Internal_Verdef
*)
4766 bfd_zalloc (abfd
, maxidx
* sizeof (Elf_Internal_Verdef
)));
4767 if (elf_tdata (abfd
)->verdef
== NULL
)
4770 elf_tdata (abfd
)->cverdefs
= maxidx
;
4772 everdef
= (Elf_External_Verdef
*) contents
;
4773 iverdefarr
= elf_tdata (abfd
)->verdef
;
4774 for (i
= 0; i
< hdr
->sh_info
; i
++)
4776 Elf_External_Verdaux
*everdaux
;
4777 Elf_Internal_Verdaux
*iverdaux
;
4780 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4782 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4783 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4785 iverdef
->vd_bfd
= abfd
;
4787 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4790 * sizeof (Elf_Internal_Verdaux
))));
4791 if (iverdef
->vd_auxptr
== NULL
)
4794 everdaux
= ((Elf_External_Verdaux
*)
4795 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4796 iverdaux
= iverdef
->vd_auxptr
;
4797 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4799 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4801 iverdaux
->vda_nodename
=
4802 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4803 iverdaux
->vda_name
);
4804 if (iverdaux
->vda_nodename
== NULL
)
4807 if (j
+ 1 < iverdef
->vd_cnt
)
4808 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4810 iverdaux
->vda_nextptr
= NULL
;
4812 everdaux
= ((Elf_External_Verdaux
*)
4813 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4816 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4818 if (i
+ 1 < hdr
->sh_info
)
4819 iverdef
->vd_nextdef
= iverdef
+ 1;
4821 iverdef
->vd_nextdef
= NULL
;
4823 everdef
= ((Elf_External_Verdef
*)
4824 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4831 if (elf_dynverref (abfd
) != 0)
4833 Elf_Internal_Shdr
*hdr
;
4834 Elf_External_Verneed
*everneed
;
4835 Elf_Internal_Verneed
*iverneed
;
4838 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4840 elf_tdata (abfd
)->verref
=
4841 ((Elf_Internal_Verneed
*)
4842 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4843 if (elf_tdata (abfd
)->verref
== NULL
)
4846 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4848 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4849 if (contents
== NULL
)
4851 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4852 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4855 everneed
= (Elf_External_Verneed
*) contents
;
4856 iverneed
= elf_tdata (abfd
)->verref
;
4857 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4859 Elf_External_Vernaux
*evernaux
;
4860 Elf_Internal_Vernaux
*ivernaux
;
4863 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4865 iverneed
->vn_bfd
= abfd
;
4867 iverneed
->vn_filename
=
4868 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4870 if (iverneed
->vn_filename
== NULL
)
4873 iverneed
->vn_auxptr
=
4874 ((Elf_Internal_Vernaux
*)
4876 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4878 evernaux
= ((Elf_External_Vernaux
*)
4879 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4880 ivernaux
= iverneed
->vn_auxptr
;
4881 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4883 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4885 ivernaux
->vna_nodename
=
4886 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4887 ivernaux
->vna_name
);
4888 if (ivernaux
->vna_nodename
== NULL
)
4891 if (j
+ 1 < iverneed
->vn_cnt
)
4892 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4894 ivernaux
->vna_nextptr
= NULL
;
4896 evernaux
= ((Elf_External_Vernaux
*)
4897 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4900 if (i
+ 1 < hdr
->sh_info
)
4901 iverneed
->vn_nextref
= iverneed
+ 1;
4903 iverneed
->vn_nextref
= NULL
;
4905 everneed
= ((Elf_External_Verneed
*)
4906 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4916 if (contents
== NULL
)
4922 _bfd_elf_make_empty_symbol (abfd
)
4925 elf_symbol_type
*newsym
;
4927 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4932 newsym
->symbol
.the_bfd
= abfd
;
4933 return &newsym
->symbol
;
4938 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4939 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4943 bfd_symbol_info (symbol
, ret
);
4946 /* Return whether a symbol name implies a local symbol. Most targets
4947 use this function for the is_local_label_name entry point, but some
4951 _bfd_elf_is_local_label_name (abfd
, name
)
4952 bfd
*abfd ATTRIBUTE_UNUSED
;
4955 /* Normal local symbols start with ``.L''. */
4956 if (name
[0] == '.' && name
[1] == 'L')
4959 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4960 DWARF debugging symbols starting with ``..''. */
4961 if (name
[0] == '.' && name
[1] == '.')
4964 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4965 emitting DWARF debugging output. I suspect this is actually a
4966 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4967 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4968 underscore to be emitted on some ELF targets). For ease of use,
4969 we treat such symbols as local. */
4970 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4977 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4978 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4979 asymbol
*symbol ATTRIBUTE_UNUSED
;
4986 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4988 enum bfd_architecture arch
;
4989 unsigned long machine
;
4991 /* If this isn't the right architecture for this backend, and this
4992 isn't the generic backend, fail. */
4993 if (arch
!= get_elf_backend_data (abfd
)->arch
4994 && arch
!= bfd_arch_unknown
4995 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4998 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
5001 /* Find the function to a particular section and offset,
5002 for error reporting. */
5005 elf_find_function (abfd
, section
, symbols
, offset
,
5006 filename_ptr
, functionname_ptr
)
5007 bfd
*abfd ATTRIBUTE_UNUSED
;
5011 CONST
char **filename_ptr
;
5012 CONST
char **functionname_ptr
;
5014 const char *filename
;
5023 for (p
= symbols
; *p
!= NULL
; p
++)
5027 q
= (elf_symbol_type
*) *p
;
5029 if (bfd_get_section (&q
->symbol
) != section
)
5032 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5037 filename
= bfd_asymbol_name (&q
->symbol
);
5041 if (q
->symbol
.section
== section
5042 && q
->symbol
.value
>= low_func
5043 && q
->symbol
.value
<= offset
)
5045 func
= (asymbol
*) q
;
5046 low_func
= q
->symbol
.value
;
5056 *filename_ptr
= filename
;
5057 if (functionname_ptr
)
5058 *functionname_ptr
= bfd_asymbol_name (func
);
5063 /* Find the nearest line to a particular section and offset,
5064 for error reporting. */
5067 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5068 filename_ptr
, functionname_ptr
, line_ptr
)
5073 CONST
char **filename_ptr
;
5074 CONST
char **functionname_ptr
;
5075 unsigned int *line_ptr
;
5079 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5080 filename_ptr
, functionname_ptr
,
5083 if (!*functionname_ptr
)
5084 elf_find_function (abfd
, section
, symbols
, offset
,
5085 *filename_ptr
? NULL
: filename_ptr
,
5091 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5092 filename_ptr
, functionname_ptr
,
5094 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5096 if (!*functionname_ptr
)
5097 elf_find_function (abfd
, section
, symbols
, offset
,
5098 *filename_ptr
? NULL
: filename_ptr
,
5104 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5105 &found
, filename_ptr
,
5106 functionname_ptr
, line_ptr
,
5107 &elf_tdata (abfd
)->line_info
))
5112 if (symbols
== NULL
)
5115 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5116 filename_ptr
, functionname_ptr
))
5124 _bfd_elf_sizeof_headers (abfd
, reloc
)
5130 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5132 ret
+= get_program_header_size (abfd
);
5137 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5142 bfd_size_type count
;
5144 Elf_Internal_Shdr
*hdr
;
5146 if (! abfd
->output_has_begun
5147 && ! _bfd_elf_compute_section_file_positions
5148 (abfd
, (struct bfd_link_info
*) NULL
))
5151 hdr
= &elf_section_data (section
)->this_hdr
;
5153 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
5155 if (bfd_write (location
, 1, count
, abfd
) != count
)
5162 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5163 bfd
*abfd ATTRIBUTE_UNUSED
;
5164 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5165 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5172 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5175 Elf_Internal_Rel
*dst
;
5181 /* Try to convert a non-ELF reloc into an ELF one. */
5184 _bfd_elf_validate_reloc (abfd
, areloc
)
5188 /* Check whether we really have an ELF howto. */
5190 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5192 bfd_reloc_code_real_type code
;
5193 reloc_howto_type
*howto
;
5195 /* Alien reloc: Try to determine its type to replace it with an
5196 equivalent ELF reloc. */
5198 if (areloc
->howto
->pc_relative
)
5200 switch (areloc
->howto
->bitsize
)
5203 code
= BFD_RELOC_8_PCREL
;
5206 code
= BFD_RELOC_12_PCREL
;
5209 code
= BFD_RELOC_16_PCREL
;
5212 code
= BFD_RELOC_24_PCREL
;
5215 code
= BFD_RELOC_32_PCREL
;
5218 code
= BFD_RELOC_64_PCREL
;
5224 howto
= bfd_reloc_type_lookup (abfd
, code
);
5226 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5228 if (howto
->pcrel_offset
)
5229 areloc
->addend
+= areloc
->address
;
5231 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5236 switch (areloc
->howto
->bitsize
)
5242 code
= BFD_RELOC_14
;
5245 code
= BFD_RELOC_16
;
5248 code
= BFD_RELOC_26
;
5251 code
= BFD_RELOC_32
;
5254 code
= BFD_RELOC_64
;
5260 howto
= bfd_reloc_type_lookup (abfd
, code
);
5264 areloc
->howto
= howto
;
5272 (*_bfd_error_handler
)
5273 (_("%s: unsupported relocation type %s"),
5274 bfd_get_filename (abfd
), areloc
->howto
->name
);
5275 bfd_set_error (bfd_error_bad_value
);
5280 _bfd_elf_close_and_cleanup (abfd
)
5283 if (bfd_get_format (abfd
) == bfd_object
)
5285 if (elf_shstrtab (abfd
) != NULL
)
5286 _bfd_stringtab_free (elf_shstrtab (abfd
));
5289 return _bfd_generic_close_and_cleanup (abfd
);
5292 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5293 in the relocation's offset. Thus we cannot allow any sort of sanity
5294 range-checking to interfere. There is nothing else to do in processing
5297 bfd_reloc_status_type
5298 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5299 bfd
*abfd ATTRIBUTE_UNUSED
;
5300 arelent
*re ATTRIBUTE_UNUSED
;
5301 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5302 PTR data ATTRIBUTE_UNUSED
;
5303 asection
*is ATTRIBUTE_UNUSED
;
5304 bfd
*obfd ATTRIBUTE_UNUSED
;
5305 char **errmsg ATTRIBUTE_UNUSED
;
5307 return bfd_reloc_ok
;
5310 /* Elf core file support. Much of this only works on native
5311 toolchains, since we rely on knowing the
5312 machine-dependent procfs structure in order to pick
5313 out details about the corefile. */
5315 #ifdef HAVE_SYS_PROCFS_H
5316 # include <sys/procfs.h>
5319 /* Define offsetof for those systems which lack it. */
5322 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5325 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5328 elfcore_make_pid (abfd
)
5331 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5332 + (elf_tdata (abfd
)->core_pid
));
5335 /* If there isn't a section called NAME, make one, using
5336 data from SECT. Note, this function will generate a
5337 reference to NAME, so you shouldn't deallocate or
5341 elfcore_maybe_make_sect (abfd
, name
, sect
)
5348 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5351 sect2
= bfd_make_section (abfd
, name
);
5355 sect2
->_raw_size
= sect
->_raw_size
;
5356 sect2
->filepos
= sect
->filepos
;
5357 sect2
->flags
= sect
->flags
;
5358 sect2
->alignment_power
= sect
->alignment_power
;
5362 /* prstatus_t exists on:
5364 linux 2.[01] + glibc
5368 #if defined (HAVE_PRSTATUS_T)
5370 elfcore_grok_prstatus (abfd
, note
)
5372 Elf_Internal_Note
*note
;
5380 if (note
->descsz
== sizeof (prstatus_t
))
5384 raw_size
= sizeof (prstat
.pr_reg
);
5385 offset
= offsetof (prstatus_t
, pr_reg
);
5386 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5388 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5389 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5391 /* pr_who exists on:
5394 pr_who doesn't exist on:
5397 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5398 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5401 #if defined (HAVE_PRSTATUS32_T)
5402 else if (note
->descsz
== sizeof (prstatus32_t
))
5404 /* 64-bit host, 32-bit corefile */
5405 prstatus32_t prstat
;
5407 raw_size
= sizeof (prstat
.pr_reg
);
5408 offset
= offsetof (prstatus32_t
, pr_reg
);
5409 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5411 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5412 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5414 /* pr_who exists on:
5417 pr_who doesn't exist on:
5420 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5421 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5424 #endif /* HAVE_PRSTATUS32_T */
5427 /* Fail - we don't know how to handle any other
5428 note size (ie. data object type). */
5432 /* Make a ".reg/999" section. */
5434 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5435 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5440 sect
= bfd_make_section (abfd
, name
);
5444 sect
->_raw_size
= raw_size
;
5445 sect
->filepos
= note
->descpos
+ offset
;
5447 sect
->flags
= SEC_HAS_CONTENTS
;
5448 sect
->alignment_power
= 2;
5450 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5455 #endif /* defined (HAVE_PRSTATUS_T) */
5457 /* Create a pseudosection containing the exact contents of NOTE. This
5458 actually creates up to two pseudosections:
5459 - For the single-threaded case, a section named NAME, unless
5460 such a section already exists.
5461 - For the multi-threaded case, a section named "NAME/PID", where
5462 PID is elfcore_make_pid (abfd).
5463 Both pseudosections have identical contents: the contents of NOTE. */
5466 elfcore_make_note_pseudosection (abfd
, name
, note
)
5469 Elf_Internal_Note
*note
;
5472 char *threaded_name
;
5475 /* Build the section name. */
5477 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5478 threaded_name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5479 if (threaded_name
== NULL
)
5481 strcpy (threaded_name
, buf
);
5483 sect
= bfd_make_section (abfd
, threaded_name
);
5486 sect
->_raw_size
= note
->descsz
;
5487 sect
->filepos
= note
->descpos
;
5488 sect
->flags
= SEC_HAS_CONTENTS
;
5489 sect
->alignment_power
= 2;
5491 if (! elfcore_maybe_make_sect (abfd
, name
, sect
))
5497 /* There isn't a consistent prfpregset_t across platforms,
5498 but it doesn't matter, because we don't have to pick this
5499 data structure apart. */
5502 elfcore_grok_prfpreg (abfd
, note
)
5504 Elf_Internal_Note
*note
;
5506 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5509 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5510 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5514 elfcore_grok_prxfpreg (abfd
, note
)
5516 Elf_Internal_Note
*note
;
5518 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5521 #if defined (HAVE_PRPSINFO_T)
5522 typedef prpsinfo_t elfcore_psinfo_t
;
5523 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5524 typedef prpsinfo32_t elfcore_psinfo32_t
;
5528 #if defined (HAVE_PSINFO_T)
5529 typedef psinfo_t elfcore_psinfo_t
;
5530 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5531 typedef psinfo32_t elfcore_psinfo32_t
;
5535 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5537 /* return a malloc'ed copy of a string at START which is at
5538 most MAX bytes long, possibly without a terminating '\0'.
5539 the copy will always have a terminating '\0'. */
5542 elfcore_strndup (abfd
, start
, max
)
5548 char *end
= memchr (start
, '\0', max
);
5556 dup
= bfd_alloc (abfd
, len
+ 1);
5560 memcpy (dup
, start
, len
);
5567 elfcore_grok_psinfo (abfd
, note
)
5569 Elf_Internal_Note
*note
;
5571 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5573 elfcore_psinfo_t psinfo
;
5575 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5577 elf_tdata (abfd
)->core_program
5578 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5580 elf_tdata (abfd
)->core_command
5581 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5583 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5584 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5586 /* 64-bit host, 32-bit corefile */
5587 elfcore_psinfo32_t psinfo
;
5589 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5591 elf_tdata (abfd
)->core_program
5592 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5594 elf_tdata (abfd
)->core_command
5595 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5601 /* Fail - we don't know how to handle any other
5602 note size (ie. data object type). */
5606 /* Note that for some reason, a spurious space is tacked
5607 onto the end of the args in some (at least one anyway)
5608 implementations, so strip it off if it exists. */
5611 char *command
= elf_tdata (abfd
)->core_command
;
5612 int n
= strlen (command
);
5614 if (0 < n
&& command
[n
- 1] == ' ')
5615 command
[n
- 1] = '\0';
5620 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5622 #if defined (HAVE_PSTATUS_T)
5624 elfcore_grok_pstatus (abfd
, note
)
5626 Elf_Internal_Note
*note
;
5628 if (note
->descsz
== sizeof (pstatus_t
)
5629 #if defined (HAVE_PXSTATUS_T)
5630 || note
->descsz
== sizeof (pxstatus_t
)
5636 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5638 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5640 #if defined (HAVE_PSTATUS32_T)
5641 else if (note
->descsz
== sizeof (pstatus32_t
))
5643 /* 64-bit host, 32-bit corefile */
5646 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5648 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5651 /* Could grab some more details from the "representative"
5652 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5653 NT_LWPSTATUS note, presumably. */
5657 #endif /* defined (HAVE_PSTATUS_T) */
5659 #if defined (HAVE_LWPSTATUS_T)
5661 elfcore_grok_lwpstatus (abfd
, note
)
5663 Elf_Internal_Note
*note
;
5665 lwpstatus_t lwpstat
;
5670 if (note
->descsz
!= sizeof (lwpstat
)
5671 #if defined (HAVE_LWPXSTATUS_T)
5672 && note
->descsz
!= sizeof (lwpxstatus_t
)
5677 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5679 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5680 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5682 /* Make a ".reg/999" section. */
5684 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5685 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5690 sect
= bfd_make_section (abfd
, name
);
5694 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5695 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5696 sect
->filepos
= note
->descpos
5697 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5700 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5701 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5702 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5705 sect
->flags
= SEC_HAS_CONTENTS
;
5706 sect
->alignment_power
= 2;
5708 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5711 /* Make a ".reg2/999" section */
5713 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5714 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5719 sect
= bfd_make_section (abfd
, name
);
5723 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5724 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5725 sect
->filepos
= note
->descpos
5726 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5729 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5730 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5731 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5734 sect
->flags
= SEC_HAS_CONTENTS
;
5735 sect
->alignment_power
= 2;
5737 if (!elfcore_maybe_make_sect (abfd
, ".reg2", sect
))
5742 #endif /* defined (HAVE_LWPSTATUS_T) */
5744 #if defined (HAVE_WIN32_PSTATUS_T)
5746 elfcore_grok_win32pstatus (abfd
, note
)
5748 Elf_Internal_Note
*note
;
5753 win32_pstatus_t pstatus
;
5755 if (note
->descsz
< sizeof (pstatus
))
5758 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
5760 switch (pstatus
.data_type
)
5762 case NOTE_INFO_PROCESS
:
5763 /* FIXME: need to add ->core_command. */
5764 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5765 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5768 case NOTE_INFO_THREAD
:
5769 /* Make a ".reg/999" section. */
5770 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5772 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5778 sect
= bfd_make_section (abfd
, name
);
5782 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5783 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5784 data
.thread_info
.thread_context
);
5785 sect
->flags
= SEC_HAS_CONTENTS
;
5786 sect
->alignment_power
= 2;
5788 if (pstatus
.data
.thread_info
.is_active_thread
)
5789 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5793 case NOTE_INFO_MODULE
:
5794 /* Make a ".module/xxxxxxxx" section. */
5795 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
5797 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5803 sect
= bfd_make_section (abfd
, name
);
5808 sect
->_raw_size
= note
->descsz
;
5809 sect
->filepos
= note
->descpos
;
5810 sect
->flags
= SEC_HAS_CONTENTS
;
5811 sect
->alignment_power
= 2;
5820 #endif /* HAVE_WIN32_PSTATUS_T */
5823 elfcore_grok_note (abfd
, note
)
5825 Elf_Internal_Note
*note
;
5832 #if defined (HAVE_PRSTATUS_T)
5834 return elfcore_grok_prstatus (abfd
, note
);
5837 #if defined (HAVE_PSTATUS_T)
5839 return elfcore_grok_pstatus (abfd
, note
);
5842 #if defined (HAVE_LWPSTATUS_T)
5844 return elfcore_grok_lwpstatus (abfd
, note
);
5847 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5848 return elfcore_grok_prfpreg (abfd
, note
);
5850 #if defined (HAVE_WIN32_PSTATUS_T)
5851 case NT_WIN32PSTATUS
:
5852 return elfcore_grok_win32pstatus (abfd
, note
);
5855 case NT_PRXFPREG
: /* Linux SSE extension */
5856 if (note
->namesz
== 5
5857 && ! strcmp (note
->namedata
, "LINUX"))
5858 return elfcore_grok_prxfpreg (abfd
, note
);
5862 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5865 return elfcore_grok_psinfo (abfd
, note
);
5871 elfcore_read_notes (abfd
, offset
, size
)
5882 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
5885 buf
= bfd_malloc ((size_t) size
);
5889 if (bfd_read (buf
, size
, 1, abfd
) != size
)
5897 while (p
< buf
+ size
)
5899 /* FIXME: bad alignment assumption. */
5900 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
5901 Elf_Internal_Note in
;
5903 in
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->type
);
5905 in
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->namesz
);
5906 in
.namedata
= xnp
->name
;
5908 in
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->descsz
);
5909 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5910 in
.descpos
= offset
+ (in
.descdata
- buf
);
5912 if (! elfcore_grok_note (abfd
, &in
))
5915 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5922 /* FIXME: This function is now unnecessary. Callers can just call
5923 bfd_section_from_phdr directly. */
5926 _bfd_elfcore_section_from_phdr (abfd
, phdr
, sec_num
)
5928 Elf_Internal_Phdr
* phdr
;
5931 if (! bfd_section_from_phdr (abfd
, phdr
, sec_num
))
5937 /* Providing external access to the ELF program header table. */
5939 /* Return an upper bound on the number of bytes required to store a
5940 copy of ABFD's program header table entries. Return -1 if an error
5941 occurs; bfd_get_error will return an appropriate code. */
5944 bfd_get_elf_phdr_upper_bound (abfd
)
5947 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5949 bfd_set_error (bfd_error_wrong_format
);
5953 return (elf_elfheader (abfd
)->e_phnum
5954 * 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
));