1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
35 #define _SYSCALL32 /* For Sparc64-cross-32 */
45 static INLINE
struct elf_segment_map
*make_mapping
46 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
47 static boolean map_sections_to_segments
PARAMS ((bfd
*));
48 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
49 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
50 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
51 static boolean prep_headers
PARAMS ((bfd
*));
52 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
53 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
54 static char *elf_read
PARAMS ((bfd
*, long, unsigned int));
55 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
56 static boolean assign_section_numbers
PARAMS ((bfd
*));
57 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
58 static boolean elf_map_symbols
PARAMS ((bfd
*));
59 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
60 static boolean elfcore_read_notes
PARAMS ((bfd
*, bfd_vma
, bfd_vma
));
62 /* Swap version information in and out. The version information is
63 currently size independent. If that ever changes, this code will
64 need to move into elfcode.h. */
66 /* Swap in a Verdef structure. */
69 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
71 const Elf_External_Verdef
*src
;
72 Elf_Internal_Verdef
*dst
;
74 dst
->vd_version
= bfd_h_get_16 (abfd
, src
->vd_version
);
75 dst
->vd_flags
= bfd_h_get_16 (abfd
, src
->vd_flags
);
76 dst
->vd_ndx
= bfd_h_get_16 (abfd
, src
->vd_ndx
);
77 dst
->vd_cnt
= bfd_h_get_16 (abfd
, src
->vd_cnt
);
78 dst
->vd_hash
= bfd_h_get_32 (abfd
, src
->vd_hash
);
79 dst
->vd_aux
= bfd_h_get_32 (abfd
, src
->vd_aux
);
80 dst
->vd_next
= bfd_h_get_32 (abfd
, src
->vd_next
);
83 /* Swap out a Verdef structure. */
86 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
88 const Elf_Internal_Verdef
*src
;
89 Elf_External_Verdef
*dst
;
91 bfd_h_put_16 (abfd
, src
->vd_version
, dst
->vd_version
);
92 bfd_h_put_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
93 bfd_h_put_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
94 bfd_h_put_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
95 bfd_h_put_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
96 bfd_h_put_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
97 bfd_h_put_32 (abfd
, src
->vd_next
, dst
->vd_next
);
100 /* Swap in a Verdaux structure. */
103 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
105 const Elf_External_Verdaux
*src
;
106 Elf_Internal_Verdaux
*dst
;
108 dst
->vda_name
= bfd_h_get_32 (abfd
, src
->vda_name
);
109 dst
->vda_next
= bfd_h_get_32 (abfd
, src
->vda_next
);
112 /* Swap out a Verdaux structure. */
115 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
117 const Elf_Internal_Verdaux
*src
;
118 Elf_External_Verdaux
*dst
;
120 bfd_h_put_32 (abfd
, src
->vda_name
, dst
->vda_name
);
121 bfd_h_put_32 (abfd
, src
->vda_next
, dst
->vda_next
);
124 /* Swap in a Verneed structure. */
127 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
129 const Elf_External_Verneed
*src
;
130 Elf_Internal_Verneed
*dst
;
132 dst
->vn_version
= bfd_h_get_16 (abfd
, src
->vn_version
);
133 dst
->vn_cnt
= bfd_h_get_16 (abfd
, src
->vn_cnt
);
134 dst
->vn_file
= bfd_h_get_32 (abfd
, src
->vn_file
);
135 dst
->vn_aux
= bfd_h_get_32 (abfd
, src
->vn_aux
);
136 dst
->vn_next
= bfd_h_get_32 (abfd
, src
->vn_next
);
139 /* Swap out a Verneed structure. */
142 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
144 const Elf_Internal_Verneed
*src
;
145 Elf_External_Verneed
*dst
;
147 bfd_h_put_16 (abfd
, src
->vn_version
, dst
->vn_version
);
148 bfd_h_put_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
149 bfd_h_put_32 (abfd
, src
->vn_file
, dst
->vn_file
);
150 bfd_h_put_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
151 bfd_h_put_32 (abfd
, src
->vn_next
, dst
->vn_next
);
154 /* Swap in a Vernaux structure. */
157 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
159 const Elf_External_Vernaux
*src
;
160 Elf_Internal_Vernaux
*dst
;
162 dst
->vna_hash
= bfd_h_get_32 (abfd
, src
->vna_hash
);
163 dst
->vna_flags
= bfd_h_get_16 (abfd
, src
->vna_flags
);
164 dst
->vna_other
= bfd_h_get_16 (abfd
, src
->vna_other
);
165 dst
->vna_name
= bfd_h_get_32 (abfd
, src
->vna_name
);
166 dst
->vna_next
= bfd_h_get_32 (abfd
, src
->vna_next
);
169 /* Swap out a Vernaux structure. */
172 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
174 const Elf_Internal_Vernaux
*src
;
175 Elf_External_Vernaux
*dst
;
177 bfd_h_put_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
178 bfd_h_put_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
179 bfd_h_put_16 (abfd
, src
->vna_other
, dst
->vna_other
);
180 bfd_h_put_32 (abfd
, src
->vna_name
, dst
->vna_name
);
181 bfd_h_put_32 (abfd
, src
->vna_next
, dst
->vna_next
);
184 /* Swap in a Versym structure. */
187 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
189 const Elf_External_Versym
*src
;
190 Elf_Internal_Versym
*dst
;
192 dst
->vs_vers
= bfd_h_get_16 (abfd
, src
->vs_vers
);
195 /* Swap out a Versym structure. */
198 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
200 const Elf_Internal_Versym
*src
;
201 Elf_External_Versym
*dst
;
203 bfd_h_put_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
206 /* Standard ELF hash function. Do not change this function; you will
207 cause invalid hash tables to be generated. */
210 bfd_elf_hash (namearg
)
213 const unsigned char *name
= (const unsigned char *) namearg
;
218 while ((ch
= *name
++) != '\0')
221 if ((g
= (h
& 0xf0000000)) != 0)
224 /* The ELF ABI says `h &= ~g', but this is equivalent in
225 this case and on some machines one insn instead of two. */
232 /* Read a specified number of bytes at a specified offset in an ELF
233 file, into a newly allocated buffer, and return a pointer to the
237 elf_read (abfd
, offset
, size
)
244 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
246 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
248 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
250 if (bfd_get_error () != bfd_error_system_call
)
251 bfd_set_error (bfd_error_file_truncated
);
258 bfd_elf_mkobject (abfd
)
261 /* this just does initialization */
262 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
263 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
264 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
265 if (elf_tdata (abfd
) == 0)
267 /* since everything is done at close time, do we need any
274 bfd_elf_mkcorefile (abfd
)
277 /* I think this can be done just like an object file. */
278 return bfd_elf_mkobject (abfd
);
282 bfd_elf_get_str_section (abfd
, shindex
)
284 unsigned int shindex
;
286 Elf_Internal_Shdr
**i_shdrp
;
287 char *shstrtab
= NULL
;
289 unsigned int shstrtabsize
;
291 i_shdrp
= elf_elfsections (abfd
);
292 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
295 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
296 if (shstrtab
== NULL
)
298 /* No cached one, attempt to read, and cache what we read. */
299 offset
= i_shdrp
[shindex
]->sh_offset
;
300 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
301 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
302 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
308 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
310 unsigned int shindex
;
311 unsigned int strindex
;
313 Elf_Internal_Shdr
*hdr
;
318 hdr
= elf_elfsections (abfd
)[shindex
];
320 if (hdr
->contents
== NULL
321 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
324 if (strindex
>= hdr
->sh_size
)
326 (*_bfd_error_handler
)
327 (_("%s: invalid string offset %u >= %lu for section `%s'"),
328 bfd_get_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
329 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
330 && strindex
== hdr
->sh_name
)
332 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
336 return ((char *) hdr
->contents
) + strindex
;
339 /* Make a BFD section from an ELF section. We store a pointer to the
340 BFD section in the bfd_section field of the header. */
343 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
345 Elf_Internal_Shdr
*hdr
;
351 if (hdr
->bfd_section
!= NULL
)
353 BFD_ASSERT (strcmp (name
,
354 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
358 newsect
= bfd_make_section_anyway (abfd
, name
);
362 newsect
->filepos
= hdr
->sh_offset
;
364 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
365 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
366 || ! bfd_set_section_alignment (abfd
, newsect
,
367 bfd_log2 (hdr
->sh_addralign
)))
370 flags
= SEC_NO_FLAGS
;
371 if (hdr
->sh_type
!= SHT_NOBITS
)
372 flags
|= SEC_HAS_CONTENTS
;
373 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
376 if (hdr
->sh_type
!= SHT_NOBITS
)
379 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
380 flags
|= SEC_READONLY
;
381 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
383 else if ((flags
& SEC_LOAD
) != 0)
386 /* The debugging sections appear to be recognized only by name, not
388 if (strncmp (name
, ".debug", sizeof ".debug" - 1) == 0
389 || strncmp (name
, ".line", sizeof ".line" - 1) == 0
390 || strncmp (name
, ".stab", sizeof ".stab" - 1) == 0)
391 flags
|= SEC_DEBUGGING
;
393 /* As a GNU extension, if the name begins with .gnu.linkonce, we
394 only link a single copy of the section. This is used to support
395 g++. g++ will emit each template expansion in its own section.
396 The symbols will be defined as weak, so that multiple definitions
397 are permitted. The GNU linker extension is to actually discard
398 all but one of the sections. */
399 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
400 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
402 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
405 if ((flags
& SEC_ALLOC
) != 0)
407 Elf_Internal_Phdr
*phdr
;
410 /* Look through the phdrs to see if we need to adjust the lma.
411 If all the p_paddr fields are zero, we ignore them, since
412 some ELF linkers produce such output. */
413 phdr
= elf_tdata (abfd
)->phdr
;
414 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
416 if (phdr
->p_paddr
!= 0)
419 if (i
< elf_elfheader (abfd
)->e_phnum
)
421 phdr
= elf_tdata (abfd
)->phdr
;
422 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
424 if (phdr
->p_type
== PT_LOAD
425 && phdr
->p_vaddr
!= phdr
->p_paddr
426 && phdr
->p_vaddr
<= hdr
->sh_addr
427 && (phdr
->p_vaddr
+ phdr
->p_memsz
428 >= hdr
->sh_addr
+ hdr
->sh_size
)
429 && ((flags
& SEC_LOAD
) == 0
430 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
431 && (phdr
->p_offset
+ phdr
->p_filesz
432 >= hdr
->sh_offset
+ hdr
->sh_size
))))
434 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
441 hdr
->bfd_section
= newsect
;
442 elf_section_data (newsect
)->this_hdr
= *hdr
;
452 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
455 Helper functions for GDB to locate the string tables.
456 Since BFD hides string tables from callers, GDB needs to use an
457 internal hook to find them. Sun's .stabstr, in particular,
458 isn't even pointed to by the .stab section, so ordinary
459 mechanisms wouldn't work to find it, even if we had some.
462 struct elf_internal_shdr
*
463 bfd_elf_find_section (abfd
, name
)
467 Elf_Internal_Shdr
**i_shdrp
;
472 i_shdrp
= elf_elfsections (abfd
);
475 shstrtab
= bfd_elf_get_str_section
476 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
477 if (shstrtab
!= NULL
)
479 max
= elf_elfheader (abfd
)->e_shnum
;
480 for (i
= 1; i
< max
; i
++)
481 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
488 const char *const bfd_elf_section_type_names
[] = {
489 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
490 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
491 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
494 /* ELF relocs are against symbols. If we are producing relocateable
495 output, and the reloc is against an external symbol, and nothing
496 has given us any additional addend, the resulting reloc will also
497 be against the same symbol. In such a case, we don't want to
498 change anything about the way the reloc is handled, since it will
499 all be done at final link time. Rather than put special case code
500 into bfd_perform_relocation, all the reloc types use this howto
501 function. It just short circuits the reloc if producing
502 relocateable output against an external symbol. */
505 bfd_reloc_status_type
506 bfd_elf_generic_reloc (abfd
,
513 bfd
*abfd ATTRIBUTE_UNUSED
;
514 arelent
*reloc_entry
;
516 PTR data ATTRIBUTE_UNUSED
;
517 asection
*input_section
;
519 char **error_message ATTRIBUTE_UNUSED
;
521 if (output_bfd
!= (bfd
*) NULL
522 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
523 && (! reloc_entry
->howto
->partial_inplace
524 || reloc_entry
->addend
== 0))
526 reloc_entry
->address
+= input_section
->output_offset
;
530 return bfd_reloc_continue
;
533 /* Print out the program headers. */
536 _bfd_elf_print_private_bfd_data (abfd
, farg
)
540 FILE *f
= (FILE *) farg
;
541 Elf_Internal_Phdr
*p
;
543 bfd_byte
*dynbuf
= NULL
;
545 p
= elf_tdata (abfd
)->phdr
;
550 fprintf (f
, _("\nProgram Header:\n"));
551 c
= elf_elfheader (abfd
)->e_phnum
;
552 for (i
= 0; i
< c
; i
++, p
++)
559 case PT_NULL
: s
= "NULL"; break;
560 case PT_LOAD
: s
= "LOAD"; break;
561 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
562 case PT_INTERP
: s
= "INTERP"; break;
563 case PT_NOTE
: s
= "NOTE"; break;
564 case PT_SHLIB
: s
= "SHLIB"; break;
565 case PT_PHDR
: s
= "PHDR"; break;
566 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
568 fprintf (f
, "%8s off 0x", s
);
569 fprintf_vma (f
, p
->p_offset
);
570 fprintf (f
, " vaddr 0x");
571 fprintf_vma (f
, p
->p_vaddr
);
572 fprintf (f
, " paddr 0x");
573 fprintf_vma (f
, p
->p_paddr
);
574 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
575 fprintf (f
, " filesz 0x");
576 fprintf_vma (f
, p
->p_filesz
);
577 fprintf (f
, " memsz 0x");
578 fprintf_vma (f
, p
->p_memsz
);
579 fprintf (f
, " flags %c%c%c",
580 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
581 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
582 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
583 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
584 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
589 s
= bfd_get_section_by_name (abfd
, ".dynamic");
594 bfd_byte
*extdyn
, *extdynend
;
596 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
598 fprintf (f
, _("\nDynamic Section:\n"));
600 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
603 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
607 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
610 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
612 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
613 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
616 extdynend
= extdyn
+ s
->_raw_size
;
617 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
619 Elf_Internal_Dyn dyn
;
624 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
626 if (dyn
.d_tag
== DT_NULL
)
633 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
637 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
638 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
639 case DT_PLTGOT
: name
= "PLTGOT"; break;
640 case DT_HASH
: name
= "HASH"; break;
641 case DT_STRTAB
: name
= "STRTAB"; break;
642 case DT_SYMTAB
: name
= "SYMTAB"; break;
643 case DT_RELA
: name
= "RELA"; break;
644 case DT_RELASZ
: name
= "RELASZ"; break;
645 case DT_RELAENT
: name
= "RELAENT"; break;
646 case DT_STRSZ
: name
= "STRSZ"; break;
647 case DT_SYMENT
: name
= "SYMENT"; break;
648 case DT_INIT
: name
= "INIT"; break;
649 case DT_FINI
: name
= "FINI"; break;
650 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
651 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
652 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
653 case DT_REL
: name
= "REL"; break;
654 case DT_RELSZ
: name
= "RELSZ"; break;
655 case DT_RELENT
: name
= "RELENT"; break;
656 case DT_PLTREL
: name
= "PLTREL"; break;
657 case DT_DEBUG
: name
= "DEBUG"; break;
658 case DT_TEXTREL
: name
= "TEXTREL"; break;
659 case DT_JMPREL
: name
= "JMPREL"; break;
660 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
661 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
662 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
663 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
664 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
665 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
666 case DT_FLAGS
: name
= "FLAGS"; break;
667 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
668 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
669 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
670 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
671 case DT_MOVEENT
: name
= "MOVEENT"; break;
672 case DT_MOVESZ
: name
= "MOVESZ"; break;
673 case DT_FEATURE
: name
= "FEATURE"; break;
674 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
675 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
676 case DT_SYMINENT
: name
= "SYMINENT"; break;
677 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
678 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
679 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
680 case DT_PLTPAD
: name
= "PLTPAD"; break;
681 case DT_MOVETAB
: name
= "MOVETAB"; break;
682 case DT_SYMINFO
: name
= "SYMINFO"; break;
683 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
684 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
685 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
686 case DT_VERSYM
: name
= "VERSYM"; break;
687 case DT_VERDEF
: name
= "VERDEF"; break;
688 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
689 case DT_VERNEED
: name
= "VERNEED"; break;
690 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
691 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
692 case DT_USED
: name
= "USED"; break;
693 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
696 fprintf (f
, " %-11s ", name
);
698 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
703 string
= bfd_elf_string_from_elf_section (abfd
, link
,
707 fprintf (f
, "%s", string
);
716 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
717 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
719 if (! _bfd_elf_slurp_version_tables (abfd
))
723 if (elf_dynverdef (abfd
) != 0)
725 Elf_Internal_Verdef
*t
;
727 fprintf (f
, _("\nVersion definitions:\n"));
728 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
730 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
731 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
732 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
734 Elf_Internal_Verdaux
*a
;
737 for (a
= t
->vd_auxptr
->vda_nextptr
;
740 fprintf (f
, "%s ", a
->vda_nodename
);
746 if (elf_dynverref (abfd
) != 0)
748 Elf_Internal_Verneed
*t
;
750 fprintf (f
, _("\nVersion References:\n"));
751 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
753 Elf_Internal_Vernaux
*a
;
755 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
756 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
757 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
758 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
770 /* Display ELF-specific fields of a symbol. */
773 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
777 bfd_print_symbol_type how
;
779 FILE *file
= (FILE *) filep
;
782 case bfd_print_symbol_name
:
783 fprintf (file
, "%s", symbol
->name
);
785 case bfd_print_symbol_more
:
786 fprintf (file
, "elf ");
787 fprintf_vma (file
, symbol
->value
);
788 fprintf (file
, " %lx", (long) symbol
->flags
);
790 case bfd_print_symbol_all
:
792 CONST
char *section_name
;
793 CONST
char *name
= NULL
;
794 struct elf_backend_data
*bed
;
795 unsigned char st_other
;
797 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
799 bed
= get_elf_backend_data (abfd
);
800 if (bed
->elf_backend_print_symbol_all
)
801 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
806 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
809 fprintf (file
, " %s\t", section_name
);
810 /* Print the "other" value for a symbol. For common symbols,
811 we've already printed the size; now print the alignment.
812 For other symbols, we have no specified alignment, and
813 we've printed the address; now print the size. */
815 (bfd_is_com_section (symbol
->section
)
816 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
817 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
819 /* If we have version information, print it. */
820 if (elf_tdata (abfd
)->dynversym_section
!= 0
821 && (elf_tdata (abfd
)->dynverdef_section
!= 0
822 || elf_tdata (abfd
)->dynverref_section
!= 0))
825 const char *version_string
;
827 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
831 else if (vernum
== 1)
832 version_string
= "Base";
833 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
835 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
838 Elf_Internal_Verneed
*t
;
841 for (t
= elf_tdata (abfd
)->verref
;
845 Elf_Internal_Vernaux
*a
;
847 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
849 if (a
->vna_other
== vernum
)
851 version_string
= a
->vna_nodename
;
858 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
859 fprintf (file
, " %-11s", version_string
);
864 fprintf (file
, " (%s)", version_string
);
865 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
870 /* If the st_other field is not zero, print it. */
871 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
876 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
877 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
878 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
880 /* Some other non-defined flags are also present, so print
882 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
885 fprintf (file
, " %s", name
);
891 /* Create an entry in an ELF linker hash table. */
893 struct bfd_hash_entry
*
894 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
895 struct bfd_hash_entry
*entry
;
896 struct bfd_hash_table
*table
;
899 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
901 /* Allocate the structure if it has not already been allocated by a
903 if (ret
== (struct elf_link_hash_entry
*) NULL
)
904 ret
= ((struct elf_link_hash_entry
*)
905 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
906 if (ret
== (struct elf_link_hash_entry
*) NULL
)
907 return (struct bfd_hash_entry
*) ret
;
909 /* Call the allocation method of the superclass. */
910 ret
= ((struct elf_link_hash_entry
*)
911 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
913 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
915 /* Set local fields. */
919 ret
->dynstr_index
= 0;
921 ret
->got
.offset
= (bfd_vma
) -1;
922 ret
->plt
.offset
= (bfd_vma
) -1;
923 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
924 ret
->verinfo
.verdef
= NULL
;
925 ret
->vtable_entries_used
= NULL
;
926 ret
->vtable_entries_size
= 0;
927 ret
->vtable_parent
= NULL
;
928 ret
->type
= STT_NOTYPE
;
930 /* Assume that we have been called by a non-ELF symbol reader.
931 This flag is then reset by the code which reads an ELF input
932 file. This ensures that a symbol created by a non-ELF symbol
933 reader will have the flag set correctly. */
934 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
937 return (struct bfd_hash_entry
*) ret
;
940 /* Copy data from an indirect symbol to its direct symbol, hiding the
941 old indirect symbol. */
944 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
945 struct elf_link_hash_entry
*dir
, *ind
;
947 /* Copy down any references that we may have already seen to the
948 symbol which just became indirect. */
950 dir
->elf_link_hash_flags
|=
951 (ind
->elf_link_hash_flags
952 & (ELF_LINK_HASH_REF_DYNAMIC
953 | ELF_LINK_HASH_REF_REGULAR
954 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
955 | ELF_LINK_NON_GOT_REF
));
957 /* Copy over the global and procedure linkage table offset entries.
958 These may have been already set up by a check_relocs routine. */
959 if (dir
->got
.offset
== (bfd_vma
) -1)
961 dir
->got
.offset
= ind
->got
.offset
;
962 ind
->got
.offset
= (bfd_vma
) -1;
964 BFD_ASSERT (ind
->got
.offset
== (bfd_vma
) -1);
966 if (dir
->plt
.offset
== (bfd_vma
) -1)
968 dir
->plt
.offset
= ind
->plt
.offset
;
969 ind
->plt
.offset
= (bfd_vma
) -1;
971 BFD_ASSERT (ind
->plt
.offset
== (bfd_vma
) -1);
973 if (dir
->dynindx
== -1)
975 dir
->dynindx
= ind
->dynindx
;
976 dir
->dynstr_index
= ind
->dynstr_index
;
978 ind
->dynstr_index
= 0;
980 BFD_ASSERT (ind
->dynindx
== -1);
984 _bfd_elf_link_hash_hide_symbol (info
, h
)
985 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
986 struct elf_link_hash_entry
*h
;
988 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
990 h
->plt
.offset
= (bfd_vma
) -1;
993 /* Initialize an ELF linker hash table. */
996 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
997 struct elf_link_hash_table
*table
;
999 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1000 struct bfd_hash_table
*,
1003 table
->dynamic_sections_created
= false;
1004 table
->dynobj
= NULL
;
1005 /* The first dynamic symbol is a dummy. */
1006 table
->dynsymcount
= 1;
1007 table
->dynstr
= NULL
;
1008 table
->bucketcount
= 0;
1009 table
->needed
= NULL
;
1011 table
->stab_info
= NULL
;
1012 table
->dynlocal
= NULL
;
1013 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1016 /* Create an ELF linker hash table. */
1018 struct bfd_link_hash_table
*
1019 _bfd_elf_link_hash_table_create (abfd
)
1022 struct elf_link_hash_table
*ret
;
1024 ret
= ((struct elf_link_hash_table
*)
1025 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
1026 if (ret
== (struct elf_link_hash_table
*) NULL
)
1029 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1031 bfd_release (abfd
, ret
);
1038 /* This is a hook for the ELF emulation code in the generic linker to
1039 tell the backend linker what file name to use for the DT_NEEDED
1040 entry for a dynamic object. The generic linker passes name as an
1041 empty string to indicate that no DT_NEEDED entry should be made. */
1044 bfd_elf_set_dt_needed_name (abfd
, name
)
1048 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1049 && bfd_get_format (abfd
) == bfd_object
)
1050 elf_dt_name (abfd
) = name
;
1054 bfd_elf_set_dt_needed_soname (abfd
, name
)
1058 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1059 && bfd_get_format (abfd
) == bfd_object
)
1060 elf_dt_soname (abfd
) = name
;
1063 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1064 the linker ELF emulation code. */
1066 struct bfd_link_needed_list
*
1067 bfd_elf_get_needed_list (abfd
, info
)
1068 bfd
*abfd ATTRIBUTE_UNUSED
;
1069 struct bfd_link_info
*info
;
1071 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1073 return elf_hash_table (info
)->needed
;
1076 /* Get the name actually used for a dynamic object for a link. This
1077 is the SONAME entry if there is one. Otherwise, it is the string
1078 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1081 bfd_elf_get_dt_soname (abfd
)
1084 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1085 && bfd_get_format (abfd
) == bfd_object
)
1086 return elf_dt_name (abfd
);
1090 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1091 the ELF linker emulation code. */
1094 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1096 struct bfd_link_needed_list
**pneeded
;
1099 bfd_byte
*dynbuf
= NULL
;
1102 bfd_byte
*extdyn
, *extdynend
;
1104 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1108 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1109 || bfd_get_format (abfd
) != bfd_object
)
1112 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1113 if (s
== NULL
|| s
->_raw_size
== 0)
1116 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1120 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1124 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1128 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1130 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1131 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1134 extdynend
= extdyn
+ s
->_raw_size
;
1135 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1137 Elf_Internal_Dyn dyn
;
1139 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1141 if (dyn
.d_tag
== DT_NULL
)
1144 if (dyn
.d_tag
== DT_NEEDED
)
1147 struct bfd_link_needed_list
*l
;
1149 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1154 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1175 /* Allocate an ELF string table--force the first byte to be zero. */
1177 struct bfd_strtab_hash
*
1178 _bfd_elf_stringtab_init ()
1180 struct bfd_strtab_hash
*ret
;
1182 ret
= _bfd_stringtab_init ();
1187 loc
= _bfd_stringtab_add (ret
, "", true, false);
1188 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1189 if (loc
== (bfd_size_type
) -1)
1191 _bfd_stringtab_free (ret
);
1198 /* ELF .o/exec file reading */
1200 /* Create a new bfd section from an ELF section header. */
1203 bfd_section_from_shdr (abfd
, shindex
)
1205 unsigned int shindex
;
1207 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1208 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1209 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1212 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1214 switch (hdr
->sh_type
)
1217 /* Inactive section. Throw it away. */
1220 case SHT_PROGBITS
: /* Normal section with contents. */
1221 case SHT_DYNAMIC
: /* Dynamic linking information. */
1222 case SHT_NOBITS
: /* .bss section. */
1223 case SHT_HASH
: /* .hash section. */
1224 case SHT_NOTE
: /* .note section. */
1225 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1227 case SHT_SYMTAB
: /* A symbol table */
1228 if (elf_onesymtab (abfd
) == shindex
)
1231 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1232 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1233 elf_onesymtab (abfd
) = shindex
;
1234 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1235 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1236 abfd
->flags
|= HAS_SYMS
;
1238 /* Sometimes a shared object will map in the symbol table. If
1239 SHF_ALLOC is set, and this is a shared object, then we also
1240 treat this section as a BFD section. We can not base the
1241 decision purely on SHF_ALLOC, because that flag is sometimes
1242 set in a relocateable object file, which would confuse the
1244 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1245 && (abfd
->flags
& DYNAMIC
) != 0
1246 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1251 case SHT_DYNSYM
: /* A dynamic symbol table */
1252 if (elf_dynsymtab (abfd
) == shindex
)
1255 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1256 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1257 elf_dynsymtab (abfd
) = shindex
;
1258 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1259 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1260 abfd
->flags
|= HAS_SYMS
;
1262 /* Besides being a symbol table, we also treat this as a regular
1263 section, so that objcopy can handle it. */
1264 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1266 case SHT_STRTAB
: /* A string table */
1267 if (hdr
->bfd_section
!= NULL
)
1269 if (ehdr
->e_shstrndx
== shindex
)
1271 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1272 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1278 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1280 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1281 if (hdr2
->sh_link
== shindex
)
1283 if (! bfd_section_from_shdr (abfd
, i
))
1285 if (elf_onesymtab (abfd
) == i
)
1287 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1288 elf_elfsections (abfd
)[shindex
] =
1289 &elf_tdata (abfd
)->strtab_hdr
;
1292 if (elf_dynsymtab (abfd
) == i
)
1294 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1295 elf_elfsections (abfd
)[shindex
] = hdr
=
1296 &elf_tdata (abfd
)->dynstrtab_hdr
;
1297 /* We also treat this as a regular section, so
1298 that objcopy can handle it. */
1301 #if 0 /* Not handling other string tables specially right now. */
1302 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1303 /* We have a strtab for some random other section. */
1304 newsect
= (asection
*) hdr2
->bfd_section
;
1307 hdr
->bfd_section
= newsect
;
1308 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1310 elf_elfsections (abfd
)[shindex
] = hdr2
;
1316 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1320 /* *These* do a lot of work -- but build no sections! */
1322 asection
*target_sect
;
1323 Elf_Internal_Shdr
*hdr2
;
1325 /* Check for a bogus link to avoid crashing. */
1326 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1328 ((*_bfd_error_handler
)
1329 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1330 bfd_get_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1331 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1334 /* For some incomprehensible reason Oracle distributes
1335 libraries for Solaris in which some of the objects have
1336 bogus sh_link fields. It would be nice if we could just
1337 reject them, but, unfortunately, some people need to use
1338 them. We scan through the section headers; if we find only
1339 one suitable symbol table, we clobber the sh_link to point
1340 to it. I hope this doesn't break anything. */
1341 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1342 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1348 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1350 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1351 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1362 hdr
->sh_link
= found
;
1365 /* Get the symbol table. */
1366 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1367 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1370 /* If this reloc section does not use the main symbol table we
1371 don't treat it as a reloc section. BFD can't adequately
1372 represent such a section, so at least for now, we don't
1373 try. We just present it as a normal section. We also
1374 can't use it as a reloc section if it points to the null
1376 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1377 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1379 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1381 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1382 if (target_sect
== NULL
)
1385 if ((target_sect
->flags
& SEC_RELOC
) == 0
1386 || target_sect
->reloc_count
== 0)
1387 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1390 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1391 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1392 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1395 elf_elfsections (abfd
)[shindex
] = hdr2
;
1396 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
1397 target_sect
->flags
|= SEC_RELOC
;
1398 target_sect
->relocation
= NULL
;
1399 target_sect
->rel_filepos
= hdr
->sh_offset
;
1400 /* In the section to which the relocations apply, mark whether
1401 its relocations are of the REL or RELA variety. */
1402 if (hdr
->sh_size
!= 0)
1403 elf_section_data (target_sect
)->use_rela_p
1404 = (hdr
->sh_type
== SHT_RELA
);
1405 abfd
->flags
|= HAS_RELOC
;
1410 case SHT_GNU_verdef
:
1411 elf_dynverdef (abfd
) = shindex
;
1412 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1413 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1416 case SHT_GNU_versym
:
1417 elf_dynversym (abfd
) = shindex
;
1418 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1419 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1422 case SHT_GNU_verneed
:
1423 elf_dynverref (abfd
) = shindex
;
1424 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1425 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1432 /* Check for any processor-specific section types. */
1434 if (bed
->elf_backend_section_from_shdr
)
1435 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1443 /* Given an ELF section number, retrieve the corresponding BFD
1447 bfd_section_from_elf_index (abfd
, index
)
1451 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1452 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1454 return elf_elfsections (abfd
)[index
]->bfd_section
;
1458 _bfd_elf_new_section_hook (abfd
, sec
)
1462 struct bfd_elf_section_data
*sdata
;
1464 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, sizeof (*sdata
));
1467 sec
->used_by_bfd
= (PTR
) sdata
;
1469 /* Indicate whether or not this section should use RELA relocations. */
1471 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1476 /* Create a new bfd section from an ELF program header.
1478 Since program segments have no names, we generate a synthetic name
1479 of the form segment<NUM>, where NUM is generally the index in the
1480 program header table. For segments that are split (see below) we
1481 generate the names segment<NUM>a and segment<NUM>b.
1483 Note that some program segments may have a file size that is different than
1484 (less than) the memory size. All this means is that at execution the
1485 system must allocate the amount of memory specified by the memory size,
1486 but only initialize it with the first "file size" bytes read from the
1487 file. This would occur for example, with program segments consisting
1488 of combined data+bss.
1490 To handle the above situation, this routine generates TWO bfd sections
1491 for the single program segment. The first has the length specified by
1492 the file size of the segment, and the second has the length specified
1493 by the difference between the two sizes. In effect, the segment is split
1494 into it's initialized and uninitialized parts.
1499 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1501 Elf_Internal_Phdr
*hdr
;
1503 const char *typename
;
1510 split
= ((hdr
->p_memsz
> 0)
1511 && (hdr
->p_filesz
> 0)
1512 && (hdr
->p_memsz
> hdr
->p_filesz
));
1513 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1514 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1517 strcpy (name
, namebuf
);
1518 newsect
= bfd_make_section (abfd
, name
);
1519 if (newsect
== NULL
)
1521 newsect
->vma
= hdr
->p_vaddr
;
1522 newsect
->lma
= hdr
->p_paddr
;
1523 newsect
->_raw_size
= hdr
->p_filesz
;
1524 newsect
->filepos
= hdr
->p_offset
;
1525 newsect
->flags
|= SEC_HAS_CONTENTS
;
1526 if (hdr
->p_type
== PT_LOAD
)
1528 newsect
->flags
|= SEC_ALLOC
;
1529 newsect
->flags
|= SEC_LOAD
;
1530 if (hdr
->p_flags
& PF_X
)
1532 /* FIXME: all we known is that it has execute PERMISSION,
1534 newsect
->flags
|= SEC_CODE
;
1537 if (!(hdr
->p_flags
& PF_W
))
1539 newsect
->flags
|= SEC_READONLY
;
1544 sprintf (namebuf
, "%s%db", typename
, index
);
1545 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1548 strcpy (name
, namebuf
);
1549 newsect
= bfd_make_section (abfd
, name
);
1550 if (newsect
== NULL
)
1552 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1553 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1554 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1555 if (hdr
->p_type
== PT_LOAD
)
1557 newsect
->flags
|= SEC_ALLOC
;
1558 if (hdr
->p_flags
& PF_X
)
1559 newsect
->flags
|= SEC_CODE
;
1561 if (!(hdr
->p_flags
& PF_W
))
1562 newsect
->flags
|= SEC_READONLY
;
1569 bfd_section_from_phdr (abfd
, hdr
, index
)
1571 Elf_Internal_Phdr
*hdr
;
1574 struct elf_backend_data
*bed
;
1576 switch (hdr
->p_type
)
1579 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1582 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1585 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1588 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1591 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1593 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
1598 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1601 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1604 /* Check for any processor-specific program segment types.
1605 If no handler for them, default to making "segment" sections. */
1606 bed
= get_elf_backend_data (abfd
);
1607 if (bed
->elf_backend_section_from_phdr
)
1608 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1610 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1614 /* Initialize REL_HDR, the section-header for new section, containing
1615 relocations against ASECT. If USE_RELA_P is true, we use RELA
1616 relocations; otherwise, we use REL relocations. */
1619 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1621 Elf_Internal_Shdr
*rel_hdr
;
1626 struct elf_backend_data
*bed
;
1628 bed
= get_elf_backend_data (abfd
);
1629 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1632 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1634 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1636 if (rel_hdr
->sh_name
== (unsigned int) -1)
1638 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1639 rel_hdr
->sh_entsize
= (use_rela_p
1640 ? bed
->s
->sizeof_rela
1641 : bed
->s
->sizeof_rel
);
1642 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1643 rel_hdr
->sh_flags
= 0;
1644 rel_hdr
->sh_addr
= 0;
1645 rel_hdr
->sh_size
= 0;
1646 rel_hdr
->sh_offset
= 0;
1651 /* Set up an ELF internal section header for a section. */
1655 elf_fake_sections (abfd
, asect
, failedptrarg
)
1660 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1661 boolean
*failedptr
= (boolean
*) failedptrarg
;
1662 Elf_Internal_Shdr
*this_hdr
;
1666 /* We already failed; just get out of the bfd_map_over_sections
1671 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1673 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1676 if (this_hdr
->sh_name
== (unsigned long) -1)
1682 this_hdr
->sh_flags
= 0;
1684 if ((asect
->flags
& SEC_ALLOC
) != 0
1685 || asect
->user_set_vma
)
1686 this_hdr
->sh_addr
= asect
->vma
;
1688 this_hdr
->sh_addr
= 0;
1690 this_hdr
->sh_offset
= 0;
1691 this_hdr
->sh_size
= asect
->_raw_size
;
1692 this_hdr
->sh_link
= 0;
1693 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1694 /* The sh_entsize and sh_info fields may have been set already by
1695 copy_private_section_data. */
1697 this_hdr
->bfd_section
= asect
;
1698 this_hdr
->contents
= NULL
;
1700 /* FIXME: This should not be based on section names. */
1701 if (strcmp (asect
->name
, ".dynstr") == 0)
1702 this_hdr
->sh_type
= SHT_STRTAB
;
1703 else if (strcmp (asect
->name
, ".hash") == 0)
1705 this_hdr
->sh_type
= SHT_HASH
;
1706 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1708 else if (strcmp (asect
->name
, ".dynsym") == 0)
1710 this_hdr
->sh_type
= SHT_DYNSYM
;
1711 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1713 else if (strcmp (asect
->name
, ".dynamic") == 0)
1715 this_hdr
->sh_type
= SHT_DYNAMIC
;
1716 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1718 else if (strncmp (asect
->name
, ".rela", 5) == 0
1719 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1721 this_hdr
->sh_type
= SHT_RELA
;
1722 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1724 else if (strncmp (asect
->name
, ".rel", 4) == 0
1725 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1727 this_hdr
->sh_type
= SHT_REL
;
1728 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1730 else if (strncmp (asect
->name
, ".note", 5) == 0)
1731 this_hdr
->sh_type
= SHT_NOTE
;
1732 else if (strncmp (asect
->name
, ".stab", 5) == 0
1733 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1734 this_hdr
->sh_type
= SHT_STRTAB
;
1735 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1737 this_hdr
->sh_type
= SHT_GNU_versym
;
1738 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1740 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1742 this_hdr
->sh_type
= SHT_GNU_verdef
;
1743 this_hdr
->sh_entsize
= 0;
1744 /* objcopy or strip will copy over sh_info, but may not set
1745 cverdefs. The linker will set cverdefs, but sh_info will be
1747 if (this_hdr
->sh_info
== 0)
1748 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1750 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1751 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1753 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1755 this_hdr
->sh_type
= SHT_GNU_verneed
;
1756 this_hdr
->sh_entsize
= 0;
1757 /* objcopy or strip will copy over sh_info, but may not set
1758 cverrefs. The linker will set cverrefs, but sh_info will be
1760 if (this_hdr
->sh_info
== 0)
1761 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1763 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1764 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1766 else if ((asect
->flags
& SEC_ALLOC
) != 0
1767 && (asect
->flags
& SEC_LOAD
) != 0)
1768 this_hdr
->sh_type
= SHT_PROGBITS
;
1769 else if ((asect
->flags
& SEC_ALLOC
) != 0
1770 && ((asect
->flags
& SEC_LOAD
) == 0))
1771 this_hdr
->sh_type
= SHT_NOBITS
;
1775 this_hdr
->sh_type
= SHT_PROGBITS
;
1778 if ((asect
->flags
& SEC_ALLOC
) != 0)
1779 this_hdr
->sh_flags
|= SHF_ALLOC
;
1780 if ((asect
->flags
& SEC_READONLY
) == 0)
1781 this_hdr
->sh_flags
|= SHF_WRITE
;
1782 if ((asect
->flags
& SEC_CODE
) != 0)
1783 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1785 /* Check for processor-specific section types. */
1786 if (bed
->elf_backend_fake_sections
)
1787 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1789 /* If the section has relocs, set up a section header for the
1790 SHT_REL[A] section. If two relocation sections are required for
1791 this section, it is up to the processor-specific back-end to
1792 create the other. */
1793 if ((asect
->flags
& SEC_RELOC
) != 0
1794 && !_bfd_elf_init_reloc_shdr (abfd
,
1795 &elf_section_data (asect
)->rel_hdr
,
1797 elf_section_data (asect
)->use_rela_p
))
1801 /* Assign all ELF section numbers. The dummy first section is handled here
1802 too. The link/info pointers for the standard section types are filled
1803 in here too, while we're at it. */
1806 assign_section_numbers (abfd
)
1809 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1811 unsigned int section_number
;
1812 Elf_Internal_Shdr
**i_shdrp
;
1816 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1818 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1820 d
->this_idx
= section_number
++;
1821 if ((sec
->flags
& SEC_RELOC
) == 0)
1824 d
->rel_idx
= section_number
++;
1827 d
->rel_idx2
= section_number
++;
1832 t
->shstrtab_section
= section_number
++;
1833 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1834 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1836 if (bfd_get_symcount (abfd
) > 0)
1838 t
->symtab_section
= section_number
++;
1839 t
->strtab_section
= section_number
++;
1842 elf_elfheader (abfd
)->e_shnum
= section_number
;
1844 /* Set up the list of section header pointers, in agreement with the
1846 i_shdrp
= ((Elf_Internal_Shdr
**)
1847 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1848 if (i_shdrp
== NULL
)
1851 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1852 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1853 if (i_shdrp
[0] == NULL
)
1855 bfd_release (abfd
, i_shdrp
);
1858 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1860 elf_elfsections (abfd
) = i_shdrp
;
1862 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1863 if (bfd_get_symcount (abfd
) > 0)
1865 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1866 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1867 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1869 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1871 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1875 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1876 if (d
->rel_idx
!= 0)
1877 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1878 if (d
->rel_idx2
!= 0)
1879 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1881 /* Fill in the sh_link and sh_info fields while we're at it. */
1883 /* sh_link of a reloc section is the section index of the symbol
1884 table. sh_info is the section index of the section to which
1885 the relocation entries apply. */
1886 if (d
->rel_idx
!= 0)
1888 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1889 d
->rel_hdr
.sh_info
= d
->this_idx
;
1891 if (d
->rel_idx2
!= 0)
1893 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1894 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1897 switch (d
->this_hdr
.sh_type
)
1901 /* A reloc section which we are treating as a normal BFD
1902 section. sh_link is the section index of the symbol
1903 table. sh_info is the section index of the section to
1904 which the relocation entries apply. We assume that an
1905 allocated reloc section uses the dynamic symbol table.
1906 FIXME: How can we be sure? */
1907 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1909 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1911 /* We look up the section the relocs apply to by name. */
1913 if (d
->this_hdr
.sh_type
== SHT_REL
)
1917 s
= bfd_get_section_by_name (abfd
, name
);
1919 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1923 /* We assume that a section named .stab*str is a stabs
1924 string section. We look for a section with the same name
1925 but without the trailing ``str'', and set its sh_link
1926 field to point to this section. */
1927 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1928 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1933 len
= strlen (sec
->name
);
1934 alc
= (char *) bfd_malloc (len
- 2);
1937 strncpy (alc
, sec
->name
, len
- 3);
1938 alc
[len
- 3] = '\0';
1939 s
= bfd_get_section_by_name (abfd
, alc
);
1943 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1945 /* This is a .stab section. */
1946 elf_section_data (s
)->this_hdr
.sh_entsize
=
1947 4 + 2 * bfd_get_arch_size (abfd
) / 8;
1954 case SHT_GNU_verneed
:
1955 case SHT_GNU_verdef
:
1956 /* sh_link is the section header index of the string table
1957 used for the dynamic entries, or the symbol table, or the
1959 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1961 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1965 case SHT_GNU_versym
:
1966 /* sh_link is the section header index of the symbol table
1967 this hash table or version table is for. */
1968 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1970 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1978 /* Map symbol from it's internal number to the external number, moving
1979 all local symbols to be at the head of the list. */
1982 sym_is_global (abfd
, sym
)
1986 /* If the backend has a special mapping, use it. */
1987 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1988 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
1991 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
1992 || bfd_is_und_section (bfd_get_section (sym
))
1993 || bfd_is_com_section (bfd_get_section (sym
)));
1997 elf_map_symbols (abfd
)
2000 int symcount
= bfd_get_symcount (abfd
);
2001 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2002 asymbol
**sect_syms
;
2004 int num_globals
= 0;
2005 int num_locals2
= 0;
2006 int num_globals2
= 0;
2008 int num_sections
= 0;
2015 fprintf (stderr
, "elf_map_symbols\n");
2019 /* Add a section symbol for each BFD section. FIXME: Is this really
2021 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2023 if (max_index
< asect
->index
)
2024 max_index
= asect
->index
;
2028 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
2029 if (sect_syms
== NULL
)
2031 elf_section_syms (abfd
) = sect_syms
;
2033 for (idx
= 0; idx
< symcount
; idx
++)
2037 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2044 if (sec
->owner
!= NULL
)
2046 if (sec
->owner
!= abfd
)
2048 if (sec
->output_offset
!= 0)
2051 sec
= sec
->output_section
;
2053 /* Empty sections in the input files may have had a section
2054 symbol created for them. (See the comment near the end of
2055 _bfd_generic_link_output_symbols in linker.c). If the linker
2056 script discards such sections then we will reach this point.
2057 Since we know that we cannot avoid this case, we detect it
2058 and skip the abort and the assignment to the sect_syms array.
2059 To reproduce this particular case try running the linker
2060 testsuite test ld-scripts/weak.exp for an ELF port that uses
2061 the generic linker. */
2062 if (sec
->owner
== NULL
)
2065 BFD_ASSERT (sec
->owner
== abfd
);
2067 sect_syms
[sec
->index
] = syms
[idx
];
2072 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2074 if (sect_syms
[asect
->index
] != NULL
)
2077 sym
= bfd_make_empty_symbol (abfd
);
2080 sym
->the_bfd
= abfd
;
2081 sym
->name
= asect
->name
;
2083 /* Set the flags to 0 to indicate that this one was newly added. */
2085 sym
->section
= asect
;
2086 sect_syms
[asect
->index
] = sym
;
2090 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2091 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2095 /* Classify all of the symbols. */
2096 for (idx
= 0; idx
< symcount
; idx
++)
2098 if (!sym_is_global (abfd
, syms
[idx
]))
2103 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2105 if (sect_syms
[asect
->index
] != NULL
2106 && sect_syms
[asect
->index
]->flags
== 0)
2108 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2109 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2113 sect_syms
[asect
->index
]->flags
= 0;
2117 /* Now sort the symbols so the local symbols are first. */
2118 new_syms
= ((asymbol
**)
2120 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
2121 if (new_syms
== NULL
)
2124 for (idx
= 0; idx
< symcount
; idx
++)
2126 asymbol
*sym
= syms
[idx
];
2129 if (!sym_is_global (abfd
, sym
))
2132 i
= num_locals
+ num_globals2
++;
2134 sym
->udata
.i
= i
+ 1;
2136 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2138 if (sect_syms
[asect
->index
] != NULL
2139 && sect_syms
[asect
->index
]->flags
== 0)
2141 asymbol
*sym
= sect_syms
[asect
->index
];
2144 sym
->flags
= BSF_SECTION_SYM
;
2145 if (!sym_is_global (abfd
, sym
))
2148 i
= num_locals
+ num_globals2
++;
2150 sym
->udata
.i
= i
+ 1;
2154 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2156 elf_num_locals (abfd
) = num_locals
;
2157 elf_num_globals (abfd
) = num_globals
;
2161 /* Align to the maximum file alignment that could be required for any
2162 ELF data structure. */
2164 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2165 static INLINE file_ptr
2166 align_file_position (off
, align
)
2170 return (off
+ align
- 1) & ~(align
- 1);
2173 /* Assign a file position to a section, optionally aligning to the
2174 required section alignment. */
2177 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2178 Elf_Internal_Shdr
*i_shdrp
;
2186 al
= i_shdrp
->sh_addralign
;
2188 offset
= BFD_ALIGN (offset
, al
);
2190 i_shdrp
->sh_offset
= offset
;
2191 if (i_shdrp
->bfd_section
!= NULL
)
2192 i_shdrp
->bfd_section
->filepos
= offset
;
2193 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2194 offset
+= i_shdrp
->sh_size
;
2198 /* Compute the file positions we are going to put the sections at, and
2199 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2200 is not NULL, this is being called by the ELF backend linker. */
2203 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2205 struct bfd_link_info
*link_info
;
2207 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2209 struct bfd_strtab_hash
*strtab
;
2210 Elf_Internal_Shdr
*shstrtab_hdr
;
2212 if (abfd
->output_has_begun
)
2215 /* Do any elf backend specific processing first. */
2216 if (bed
->elf_backend_begin_write_processing
)
2217 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2219 if (! prep_headers (abfd
))
2222 /* Post process the headers if necessary. */
2223 if (bed
->elf_backend_post_process_headers
)
2224 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2227 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2231 if (!assign_section_numbers (abfd
))
2234 /* The backend linker builds symbol table information itself. */
2235 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2237 /* Non-zero if doing a relocatable link. */
2238 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2240 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2244 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2245 /* sh_name was set in prep_headers. */
2246 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2247 shstrtab_hdr
->sh_flags
= 0;
2248 shstrtab_hdr
->sh_addr
= 0;
2249 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2250 shstrtab_hdr
->sh_entsize
= 0;
2251 shstrtab_hdr
->sh_link
= 0;
2252 shstrtab_hdr
->sh_info
= 0;
2253 /* sh_offset is set in assign_file_positions_except_relocs. */
2254 shstrtab_hdr
->sh_addralign
= 1;
2256 if (!assign_file_positions_except_relocs (abfd
))
2259 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2262 Elf_Internal_Shdr
*hdr
;
2264 off
= elf_tdata (abfd
)->next_file_pos
;
2266 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2267 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2269 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2270 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2272 elf_tdata (abfd
)->next_file_pos
= off
;
2274 /* Now that we know where the .strtab section goes, write it
2276 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2277 || ! _bfd_stringtab_emit (abfd
, strtab
))
2279 _bfd_stringtab_free (strtab
);
2282 abfd
->output_has_begun
= true;
2287 /* Create a mapping from a set of sections to a program segment. */
2289 static INLINE
struct elf_segment_map
*
2290 make_mapping (abfd
, sections
, from
, to
, phdr
)
2292 asection
**sections
;
2297 struct elf_segment_map
*m
;
2301 m
= ((struct elf_segment_map
*)
2303 (sizeof (struct elf_segment_map
)
2304 + (to
- from
- 1) * sizeof (asection
*))));
2308 m
->p_type
= PT_LOAD
;
2309 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2310 m
->sections
[i
- from
] = *hdrpp
;
2311 m
->count
= to
- from
;
2313 if (from
== 0 && phdr
)
2315 /* Include the headers in the first PT_LOAD segment. */
2316 m
->includes_filehdr
= 1;
2317 m
->includes_phdrs
= 1;
2323 /* Set up a mapping from BFD sections to program segments. */
2326 map_sections_to_segments (abfd
)
2329 asection
**sections
= NULL
;
2333 struct elf_segment_map
*mfirst
;
2334 struct elf_segment_map
**pm
;
2335 struct elf_segment_map
*m
;
2337 unsigned int phdr_index
;
2338 bfd_vma maxpagesize
;
2340 boolean phdr_in_segment
= true;
2344 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2347 if (bfd_count_sections (abfd
) == 0)
2350 /* Select the allocated sections, and sort them. */
2352 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2353 * sizeof (asection
*));
2354 if (sections
== NULL
)
2358 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2360 if ((s
->flags
& SEC_ALLOC
) != 0)
2366 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2369 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2371 /* Build the mapping. */
2376 /* If we have a .interp section, then create a PT_PHDR segment for
2377 the program headers and a PT_INTERP segment for the .interp
2379 s
= bfd_get_section_by_name (abfd
, ".interp");
2380 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2382 m
= ((struct elf_segment_map
*)
2383 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2387 m
->p_type
= PT_PHDR
;
2388 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2389 m
->p_flags
= PF_R
| PF_X
;
2390 m
->p_flags_valid
= 1;
2391 m
->includes_phdrs
= 1;
2396 m
= ((struct elf_segment_map
*)
2397 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2401 m
->p_type
= PT_INTERP
;
2409 /* Look through the sections. We put sections in the same program
2410 segment when the start of the second section can be placed within
2411 a few bytes of the end of the first section. */
2414 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2416 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2418 && (dynsec
->flags
& SEC_LOAD
) == 0)
2421 /* Deal with -Ttext or something similar such that the first section
2422 is not adjacent to the program headers. This is an
2423 approximation, since at this point we don't know exactly how many
2424 program headers we will need. */
2427 bfd_size_type phdr_size
;
2429 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2431 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2432 if ((abfd
->flags
& D_PAGED
) == 0
2433 || sections
[0]->lma
< phdr_size
2434 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2435 phdr_in_segment
= false;
2438 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2441 boolean new_segment
;
2445 /* See if this section and the last one will fit in the same
2448 if (last_hdr
== NULL
)
2450 /* If we don't have a segment yet, then we don't need a new
2451 one (we build the last one after this loop). */
2452 new_segment
= false;
2454 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2456 /* If this section has a different relation between the
2457 virtual address and the load address, then we need a new
2461 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2462 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2464 /* If putting this section in this segment would force us to
2465 skip a page in the segment, then we need a new segment. */
2468 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2469 && (hdr
->flags
& SEC_LOAD
) != 0)
2471 /* We don't want to put a loadable section after a
2472 nonloadable section in the same segment. */
2475 else if ((abfd
->flags
& D_PAGED
) == 0)
2477 /* If the file is not demand paged, which means that we
2478 don't require the sections to be correctly aligned in the
2479 file, then there is no other reason for a new segment. */
2480 new_segment
= false;
2483 && (hdr
->flags
& SEC_READONLY
) == 0
2484 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2487 /* We don't want to put a writable section in a read only
2488 segment, unless they are on the same page in memory
2489 anyhow. We already know that the last section does not
2490 bring us past the current section on the page, so the
2491 only case in which the new section is not on the same
2492 page as the previous section is when the previous section
2493 ends precisely on a page boundary. */
2498 /* Otherwise, we can use the same segment. */
2499 new_segment
= false;
2504 if ((hdr
->flags
& SEC_READONLY
) == 0)
2510 /* We need a new program segment. We must create a new program
2511 header holding all the sections from phdr_index until hdr. */
2513 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2520 if ((hdr
->flags
& SEC_READONLY
) == 0)
2527 phdr_in_segment
= false;
2530 /* Create a final PT_LOAD program segment. */
2531 if (last_hdr
!= NULL
)
2533 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2541 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2544 m
= ((struct elf_segment_map
*)
2545 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2549 m
->p_type
= PT_DYNAMIC
;
2551 m
->sections
[0] = dynsec
;
2557 /* For each loadable .note section, add a PT_NOTE segment. We don't
2558 use bfd_get_section_by_name, because if we link together
2559 nonloadable .note sections and loadable .note sections, we will
2560 generate two .note sections in the output file. FIXME: Using
2561 names for section types is bogus anyhow. */
2562 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2564 if ((s
->flags
& SEC_LOAD
) != 0
2565 && strncmp (s
->name
, ".note", 5) == 0)
2567 m
= ((struct elf_segment_map
*)
2568 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2572 m
->p_type
= PT_NOTE
;
2584 elf_tdata (abfd
)->segment_map
= mfirst
;
2588 if (sections
!= NULL
)
2593 /* Sort sections by address. */
2596 elf_sort_sections (arg1
, arg2
)
2600 const asection
*sec1
= *(const asection
**) arg1
;
2601 const asection
*sec2
= *(const asection
**) arg2
;
2603 /* Sort by LMA first, since this is the address used to
2604 place the section into a segment. */
2605 if (sec1
->lma
< sec2
->lma
)
2607 else if (sec1
->lma
> sec2
->lma
)
2610 /* Then sort by VMA. Normally the LMA and the VMA will be
2611 the same, and this will do nothing. */
2612 if (sec1
->vma
< sec2
->vma
)
2614 else if (sec1
->vma
> sec2
->vma
)
2617 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2619 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2624 return sec1
->target_index
- sec2
->target_index
;
2634 /* Sort by size, to put zero sized sections before others at the
2637 if (sec1
->_raw_size
< sec2
->_raw_size
)
2639 if (sec1
->_raw_size
> sec2
->_raw_size
)
2642 return sec1
->target_index
- sec2
->target_index
;
2645 /* Assign file positions to the sections based on the mapping from
2646 sections to segments. This function also sets up some fields in
2647 the file header, and writes out the program headers. */
2650 assign_file_positions_for_segments (abfd
)
2653 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2655 struct elf_segment_map
*m
;
2657 Elf_Internal_Phdr
*phdrs
;
2659 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2660 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2661 Elf_Internal_Phdr
*p
;
2663 if (elf_tdata (abfd
)->segment_map
== NULL
)
2665 if (! map_sections_to_segments (abfd
))
2669 if (bed
->elf_backend_modify_segment_map
)
2671 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2676 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2679 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2680 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2681 elf_elfheader (abfd
)->e_phnum
= count
;
2686 /* If we already counted the number of program segments, make sure
2687 that we allocated enough space. This happens when SIZEOF_HEADERS
2688 is used in a linker script. */
2689 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2690 if (alloc
!= 0 && count
> alloc
)
2692 ((*_bfd_error_handler
)
2693 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2694 bfd_get_filename (abfd
), alloc
, count
));
2695 bfd_set_error (bfd_error_bad_value
);
2702 phdrs
= ((Elf_Internal_Phdr
*)
2703 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2707 off
= bed
->s
->sizeof_ehdr
;
2708 off
+= alloc
* bed
->s
->sizeof_phdr
;
2715 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2722 /* If elf_segment_map is not from map_sections_to_segments, the
2723 sections may not be correctly ordered. */
2725 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2728 p
->p_type
= m
->p_type
;
2729 p
->p_flags
= m
->p_flags
;
2731 if (p
->p_type
== PT_LOAD
2733 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2735 if ((abfd
->flags
& D_PAGED
) != 0)
2736 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2739 bfd_size_type align
;
2742 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2744 bfd_size_type secalign
;
2746 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2747 if (secalign
> align
)
2751 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2758 p
->p_vaddr
= m
->sections
[0]->vma
;
2760 if (m
->p_paddr_valid
)
2761 p
->p_paddr
= m
->p_paddr
;
2762 else if (m
->count
== 0)
2765 p
->p_paddr
= m
->sections
[0]->lma
;
2767 if (p
->p_type
== PT_LOAD
2768 && (abfd
->flags
& D_PAGED
) != 0)
2769 p
->p_align
= bed
->maxpagesize
;
2770 else if (m
->count
== 0)
2771 p
->p_align
= bed
->s
->file_align
;
2779 if (m
->includes_filehdr
)
2781 if (! m
->p_flags_valid
)
2784 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2785 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2788 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2790 if (p
->p_vaddr
< (bfd_vma
) off
)
2792 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2793 bfd_get_filename (abfd
));
2794 bfd_set_error (bfd_error_bad_value
);
2799 if (! m
->p_paddr_valid
)
2802 if (p
->p_type
== PT_LOAD
)
2804 filehdr_vaddr
= p
->p_vaddr
;
2805 filehdr_paddr
= p
->p_paddr
;
2809 if (m
->includes_phdrs
)
2811 if (! m
->p_flags_valid
)
2814 if (m
->includes_filehdr
)
2816 if (p
->p_type
== PT_LOAD
)
2818 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2819 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2824 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2828 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2829 p
->p_vaddr
-= off
- p
->p_offset
;
2830 if (! m
->p_paddr_valid
)
2831 p
->p_paddr
-= off
- p
->p_offset
;
2834 if (p
->p_type
== PT_LOAD
)
2836 phdrs_vaddr
= p
->p_vaddr
;
2837 phdrs_paddr
= p
->p_paddr
;
2840 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2843 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2844 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2847 if (p
->p_type
== PT_LOAD
2848 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2850 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2856 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2857 p
->p_filesz
+= adjust
;
2858 p
->p_memsz
+= adjust
;
2864 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2868 bfd_size_type align
;
2872 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2874 /* The section may have artificial alignment forced by a
2875 link script. Notice this case by the gap between the
2876 cumulative phdr vma and the section's vma. */
2877 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2879 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2881 p
->p_memsz
+= adjust
;
2884 if ((flags
& SEC_LOAD
) != 0)
2885 p
->p_filesz
+= adjust
;
2888 if (p
->p_type
== PT_LOAD
)
2890 bfd_signed_vma adjust
;
2892 if ((flags
& SEC_LOAD
) != 0)
2894 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2898 else if ((flags
& SEC_ALLOC
) != 0)
2900 /* The section VMA must equal the file position
2901 modulo the page size. FIXME: I'm not sure if
2902 this adjustment is really necessary. We used to
2903 not have the SEC_LOAD case just above, and then
2904 this was necessary, but now I'm not sure. */
2905 if ((abfd
->flags
& D_PAGED
) != 0)
2906 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2908 adjust
= (sec
->vma
- voff
) % align
;
2917 (* _bfd_error_handler
)
2918 (_("Error: First section in segment (%s) starts at 0x%x"),
2919 bfd_section_name (abfd
, sec
), sec
->lma
);
2920 (* _bfd_error_handler
)
2921 (_(" whereas segment starts at 0x%x"),
2926 p
->p_memsz
+= adjust
;
2929 if ((flags
& SEC_LOAD
) != 0)
2930 p
->p_filesz
+= adjust
;
2935 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2936 used in a linker script we may have a section with
2937 SEC_LOAD clear but which is supposed to have
2939 if ((flags
& SEC_LOAD
) != 0
2940 || (flags
& SEC_HAS_CONTENTS
) != 0)
2941 off
+= sec
->_raw_size
;
2943 if ((flags
& SEC_ALLOC
) != 0)
2944 voff
+= sec
->_raw_size
;
2947 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
2949 /* The actual "note" segment has i == 0.
2950 This is the one that actually contains everything. */
2954 p
->p_filesz
= sec
->_raw_size
;
2955 off
+= sec
->_raw_size
;
2960 /* Fake sections -- don't need to be written. */
2963 flags
= sec
->flags
= 0;
2970 p
->p_memsz
+= sec
->_raw_size
;
2972 if ((flags
& SEC_LOAD
) != 0)
2973 p
->p_filesz
+= sec
->_raw_size
;
2975 if (align
> p
->p_align
2976 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
2980 if (! m
->p_flags_valid
)
2983 if ((flags
& SEC_CODE
) != 0)
2985 if ((flags
& SEC_READONLY
) == 0)
2991 /* Now that we have set the section file positions, we can set up
2992 the file positions for the non PT_LOAD segments. */
2993 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2997 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
2999 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3000 p
->p_offset
= m
->sections
[0]->filepos
;
3004 if (m
->includes_filehdr
)
3006 p
->p_vaddr
= filehdr_vaddr
;
3007 if (! m
->p_paddr_valid
)
3008 p
->p_paddr
= filehdr_paddr
;
3010 else if (m
->includes_phdrs
)
3012 p
->p_vaddr
= phdrs_vaddr
;
3013 if (! m
->p_paddr_valid
)
3014 p
->p_paddr
= phdrs_paddr
;
3019 /* Clear out any program headers we allocated but did not use. */
3020 for (; count
< alloc
; count
++, p
++)
3022 memset (p
, 0, sizeof *p
);
3023 p
->p_type
= PT_NULL
;
3026 elf_tdata (abfd
)->phdr
= phdrs
;
3028 elf_tdata (abfd
)->next_file_pos
= off
;
3030 /* Write out the program headers. */
3031 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3032 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3038 /* Get the size of the program header.
3040 If this is called by the linker before any of the section VMA's are set, it
3041 can't calculate the correct value for a strange memory layout. This only
3042 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3043 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3044 data segment (exclusive of .interp and .dynamic).
3046 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3047 will be two segments. */
3049 static bfd_size_type
3050 get_program_header_size (abfd
)
3055 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3057 /* We can't return a different result each time we're called. */
3058 if (elf_tdata (abfd
)->program_header_size
!= 0)
3059 return elf_tdata (abfd
)->program_header_size
;
3061 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3063 struct elf_segment_map
*m
;
3066 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3068 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3069 return elf_tdata (abfd
)->program_header_size
;
3072 /* Assume we will need exactly two PT_LOAD segments: one for text
3073 and one for data. */
3076 s
= bfd_get_section_by_name (abfd
, ".interp");
3077 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3079 /* If we have a loadable interpreter section, we need a
3080 PT_INTERP segment. In this case, assume we also need a
3081 PT_PHDR segment, although that may not be true for all
3086 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3088 /* We need a PT_DYNAMIC segment. */
3092 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3094 if ((s
->flags
& SEC_LOAD
) != 0
3095 && strncmp (s
->name
, ".note", 5) == 0)
3097 /* We need a PT_NOTE segment. */
3102 /* Let the backend count up any program headers it might need. */
3103 if (bed
->elf_backend_additional_program_headers
)
3107 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3113 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3114 return elf_tdata (abfd
)->program_header_size
;
3117 /* Work out the file positions of all the sections. This is called by
3118 _bfd_elf_compute_section_file_positions. All the section sizes and
3119 VMAs must be known before this is called.
3121 We do not consider reloc sections at this point, unless they form
3122 part of the loadable image. Reloc sections are assigned file
3123 positions in assign_file_positions_for_relocs, which is called by
3124 write_object_contents and final_link.
3126 We also don't set the positions of the .symtab and .strtab here. */
3129 assign_file_positions_except_relocs (abfd
)
3132 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3133 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3134 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3136 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3138 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3139 && bfd_get_format (abfd
) != bfd_core
)
3141 Elf_Internal_Shdr
**hdrpp
;
3144 /* Start after the ELF header. */
3145 off
= i_ehdrp
->e_ehsize
;
3147 /* We are not creating an executable, which means that we are
3148 not creating a program header, and that the actual order of
3149 the sections in the file is unimportant. */
3150 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3152 Elf_Internal_Shdr
*hdr
;
3155 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3157 hdr
->sh_offset
= -1;
3160 if (i
== tdata
->symtab_section
3161 || i
== tdata
->strtab_section
)
3163 hdr
->sh_offset
= -1;
3167 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3173 Elf_Internal_Shdr
**hdrpp
;
3175 /* Assign file positions for the loaded sections based on the
3176 assignment of sections to segments. */
3177 if (! assign_file_positions_for_segments (abfd
))
3180 /* Assign file positions for the other sections. */
3182 off
= elf_tdata (abfd
)->next_file_pos
;
3183 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3185 Elf_Internal_Shdr
*hdr
;
3188 if (hdr
->bfd_section
!= NULL
3189 && hdr
->bfd_section
->filepos
!= 0)
3190 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3191 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3193 ((*_bfd_error_handler
)
3194 (_("%s: warning: allocated section `%s' not in segment"),
3195 bfd_get_filename (abfd
),
3196 (hdr
->bfd_section
== NULL
3198 : hdr
->bfd_section
->name
)));
3199 if ((abfd
->flags
& D_PAGED
) != 0)
3200 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3202 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3203 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3206 else if (hdr
->sh_type
== SHT_REL
3207 || hdr
->sh_type
== SHT_RELA
3208 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3209 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3210 hdr
->sh_offset
= -1;
3212 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3216 /* Place the section headers. */
3217 off
= align_file_position (off
, bed
->s
->file_align
);
3218 i_ehdrp
->e_shoff
= off
;
3219 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3221 elf_tdata (abfd
)->next_file_pos
= off
;
3230 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3231 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3232 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3234 struct bfd_strtab_hash
*shstrtab
;
3235 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3237 i_ehdrp
= elf_elfheader (abfd
);
3238 i_shdrp
= elf_elfsections (abfd
);
3240 shstrtab
= _bfd_elf_stringtab_init ();
3241 if (shstrtab
== NULL
)
3244 elf_shstrtab (abfd
) = shstrtab
;
3246 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3247 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3248 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3249 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3251 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3252 i_ehdrp
->e_ident
[EI_DATA
] =
3253 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3254 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3256 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3257 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3259 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3260 i_ehdrp
->e_ident
[count
] = 0;
3262 if ((abfd
->flags
& DYNAMIC
) != 0)
3263 i_ehdrp
->e_type
= ET_DYN
;
3264 else if ((abfd
->flags
& EXEC_P
) != 0)
3265 i_ehdrp
->e_type
= ET_EXEC
;
3266 else if (bfd_get_format (abfd
) == bfd_core
)
3267 i_ehdrp
->e_type
= ET_CORE
;
3269 i_ehdrp
->e_type
= ET_REL
;
3271 switch (bfd_get_arch (abfd
))
3273 case bfd_arch_unknown
:
3274 i_ehdrp
->e_machine
= EM_NONE
;
3276 case bfd_arch_sparc
:
3277 if (bfd_get_arch_size (abfd
) == 64)
3278 i_ehdrp
->e_machine
= EM_SPARCV9
;
3280 i_ehdrp
->e_machine
= EM_SPARC
;
3283 i_ehdrp
->e_machine
= EM_S370
;
3286 i_ehdrp
->e_machine
= EM_386
;
3289 i_ehdrp
->e_machine
= EM_IA_64
;
3291 case bfd_arch_m68hc11
:
3292 i_ehdrp
->e_machine
= EM_68HC11
;
3294 case bfd_arch_m68hc12
:
3295 i_ehdrp
->e_machine
= EM_68HC12
;
3298 i_ehdrp
->e_machine
= EM_68K
;
3301 i_ehdrp
->e_machine
= EM_88K
;
3304 i_ehdrp
->e_machine
= EM_860
;
3307 i_ehdrp
->e_machine
= EM_960
;
3309 case bfd_arch_mips
: /* MIPS Rxxxx */
3310 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3313 i_ehdrp
->e_machine
= EM_PARISC
;
3315 case bfd_arch_powerpc
:
3316 i_ehdrp
->e_machine
= EM_PPC
;
3318 case bfd_arch_alpha
:
3319 i_ehdrp
->e_machine
= EM_ALPHA
;
3322 i_ehdrp
->e_machine
= EM_SH
;
3325 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3328 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3331 i_ehdrp
->e_machine
= EM_CYGNUS_FR30
;
3333 case bfd_arch_mcore
:
3334 i_ehdrp
->e_machine
= EM_MCORE
;
3337 i_ehdrp
->e_machine
= EM_AVR
;
3340 switch (bfd_get_mach (abfd
))
3343 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3347 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3350 i_ehdrp
->e_machine
= EM_ARM
;
3353 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3355 case bfd_arch_mn10200
:
3356 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3358 case bfd_arch_mn10300
:
3359 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3362 i_ehdrp
->e_machine
= EM_PJ
;
3365 i_ehdrp
->e_machine
= EM_CRIS
;
3367 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3369 i_ehdrp
->e_machine
= EM_NONE
;
3371 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3372 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3374 /* no program header, for now. */
3375 i_ehdrp
->e_phoff
= 0;
3376 i_ehdrp
->e_phentsize
= 0;
3377 i_ehdrp
->e_phnum
= 0;
3379 /* each bfd section is section header entry */
3380 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3381 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3383 /* if we're building an executable, we'll need a program header table */
3384 if (abfd
->flags
& EXEC_P
)
3386 /* it all happens later */
3388 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3390 /* elf_build_phdrs() returns a (NULL-terminated) array of
3391 Elf_Internal_Phdrs */
3392 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3393 i_ehdrp
->e_phoff
= outbase
;
3394 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3399 i_ehdrp
->e_phentsize
= 0;
3401 i_ehdrp
->e_phoff
= 0;
3404 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3405 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3406 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3407 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3408 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3409 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3410 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3411 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3412 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3418 /* Assign file positions for all the reloc sections which are not part
3419 of the loadable file image. */
3422 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3427 Elf_Internal_Shdr
**shdrpp
;
3429 off
= elf_tdata (abfd
)->next_file_pos
;
3431 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3432 i
< elf_elfheader (abfd
)->e_shnum
;
3435 Elf_Internal_Shdr
*shdrp
;
3438 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3439 && shdrp
->sh_offset
== -1)
3440 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3443 elf_tdata (abfd
)->next_file_pos
= off
;
3447 _bfd_elf_write_object_contents (abfd
)
3450 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3451 Elf_Internal_Ehdr
*i_ehdrp
;
3452 Elf_Internal_Shdr
**i_shdrp
;
3456 if (! abfd
->output_has_begun
3457 && ! _bfd_elf_compute_section_file_positions
3458 (abfd
, (struct bfd_link_info
*) NULL
))
3461 i_shdrp
= elf_elfsections (abfd
);
3462 i_ehdrp
= elf_elfheader (abfd
);
3465 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3469 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3471 /* After writing the headers, we need to write the sections too... */
3472 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3474 if (bed
->elf_backend_section_processing
)
3475 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3476 if (i_shdrp
[count
]->contents
)
3478 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3479 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3481 != i_shdrp
[count
]->sh_size
))
3486 /* Write out the section header names. */
3487 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3488 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3491 if (bed
->elf_backend_final_write_processing
)
3492 (*bed
->elf_backend_final_write_processing
) (abfd
,
3493 elf_tdata (abfd
)->linker
);
3495 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3499 _bfd_elf_write_corefile_contents (abfd
)
3502 /* Hopefully this can be done just like an object file. */
3503 return _bfd_elf_write_object_contents (abfd
);
3505 /* given a section, search the header to find them... */
3507 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3511 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3512 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3514 Elf_Internal_Shdr
*hdr
;
3515 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3517 for (index
= 0; index
< maxindex
; index
++)
3519 hdr
= i_shdrp
[index
];
3520 if (hdr
->bfd_section
== asect
)
3524 if (bed
->elf_backend_section_from_bfd_section
)
3526 for (index
= 0; index
< maxindex
; index
++)
3530 hdr
= i_shdrp
[index
];
3532 if ((*bed
->elf_backend_section_from_bfd_section
)
3533 (abfd
, hdr
, asect
, &retval
))
3538 if (bfd_is_abs_section (asect
))
3540 if (bfd_is_com_section (asect
))
3542 if (bfd_is_und_section (asect
))
3545 bfd_set_error (bfd_error_nonrepresentable_section
);
3550 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3554 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3556 asymbol
**asym_ptr_ptr
;
3558 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3560 flagword flags
= asym_ptr
->flags
;
3562 /* When gas creates relocations against local labels, it creates its
3563 own symbol for the section, but does put the symbol into the
3564 symbol chain, so udata is 0. When the linker is generating
3565 relocatable output, this section symbol may be for one of the
3566 input sections rather than the output section. */
3567 if (asym_ptr
->udata
.i
== 0
3568 && (flags
& BSF_SECTION_SYM
)
3569 && asym_ptr
->section
)
3573 if (asym_ptr
->section
->output_section
!= NULL
)
3574 indx
= asym_ptr
->section
->output_section
->index
;
3576 indx
= asym_ptr
->section
->index
;
3577 if (elf_section_syms (abfd
)[indx
])
3578 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3581 idx
= asym_ptr
->udata
.i
;
3585 /* This case can occur when using --strip-symbol on a symbol
3586 which is used in a relocation entry. */
3587 (*_bfd_error_handler
)
3588 (_("%s: symbol `%s' required but not present"),
3589 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3590 bfd_set_error (bfd_error_no_symbols
);
3597 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3598 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3599 elf_symbol_flags (flags
));
3607 /* Copy private BFD data. This copies any program header information. */
3610 copy_private_bfd_data (ibfd
, obfd
)
3614 Elf_Internal_Ehdr
*iehdr
;
3615 struct elf_segment_map
*mfirst
;
3616 struct elf_segment_map
**pm
;
3617 struct elf_segment_map
*m
;
3618 Elf_Internal_Phdr
*p
;
3620 unsigned int num_segments
;
3621 boolean phdr_included
= false;
3623 bfd_vma maxpagesize
;
3624 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3625 unsigned int phdr_adjust_num
= 0;
3627 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3628 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3631 if (elf_tdata (ibfd
)->phdr
== NULL
)
3634 iehdr
= elf_elfheader (ibfd
);
3639 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3640 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3642 /* Returns the end address of the segment + 1. */
3643 #define SEGMENT_END(segment, start) \
3644 (start + (segment->p_memsz > segment->p_filesz \
3645 ? segment->p_memsz : segment->p_filesz))
3647 /* Returns true if the given section is contained within
3648 the given segment. VMA addresses are compared. */
3649 #define IS_CONTAINED_BY_VMA(section, segment) \
3650 (section->vma >= segment->p_vaddr \
3651 && (section->vma + section->_raw_size) \
3652 <= (SEGMENT_END (segment, segment->p_vaddr)))
3654 /* Returns true if the given section is contained within
3655 the given segment. LMA addresses are compared. */
3656 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3657 (section->lma >= base \
3658 && (section->lma + section->_raw_size) \
3659 <= SEGMENT_END (segment, base))
3661 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3662 #define IS_COREFILE_NOTE(p, s) \
3663 (p->p_type == PT_NOTE \
3664 && bfd_get_format (ibfd) == bfd_core \
3665 && s->vma == 0 && s->lma == 0 \
3666 && (bfd_vma) s->filepos >= p->p_offset \
3667 && (bfd_vma) s->filepos + s->_raw_size \
3668 <= p->p_offset + p->p_filesz)
3670 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3671 linker, which generates a PT_INTERP section with p_vaddr and
3672 p_memsz set to 0. */
3673 #define IS_SOLARIS_PT_INTERP(p, s) \
3675 && p->p_filesz > 0 \
3676 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3677 && s->_raw_size > 0 \
3678 && (bfd_vma) s->filepos >= p->p_offset \
3679 && ((bfd_vma) s->filepos + s->_raw_size \
3680 <= p->p_offset + p->p_filesz))
3682 /* Decide if the given section should be included in the given segment.
3683 A section will be included if:
3684 1. It is within the address space of the segment,
3685 2. It is an allocated segment,
3686 3. There is an output section associated with it,
3687 4. The section has not already been allocated to a previous segment. */
3688 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3689 ((((IS_CONTAINED_BY_VMA (section, segment) \
3690 || IS_SOLARIS_PT_INTERP (segment, section)) \
3691 && (section->flags & SEC_ALLOC) != 0) \
3692 || IS_COREFILE_NOTE (segment, section)) \
3693 && section->output_section != NULL \
3694 && section->segment_mark == false)
3696 /* Returns true iff seg1 starts after the end of seg2. */
3697 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3698 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3700 /* Returns true iff seg1 and seg2 overlap. */
3701 #define SEGMENT_OVERLAPS(seg1, seg2) \
3702 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) && SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3705 /* Initialise the segment mark field. */
3706 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3707 s
->segment_mark
= false;
3709 /* Scan through the segments specified in the program header
3710 of the input BFD. For this first scan we look for overlaps.
3711 These can be created by wierd parameters to objcopy. */
3712 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< num_segments
; i
++, p
++)
3715 Elf_Internal_Phdr
* pp
;
3717 if (p
->p_type
== PT_NULL
)
3720 /* Determine if this segment overlaps any previous segments.
3721 This can happen when objcopy is used to adjust section LMAs. */
3722 for (j
= 0, pp
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, pp
++)
3724 bfd_signed_vma extra_length
;
3727 if (pp
->p_type
== PT_NULL
|| ! SEGMENT_OVERLAPS (p
, pp
))
3730 /* Merge the two segments together. */
3731 if (pp
->p_vaddr
< p
->p_vaddr
)
3733 /* Extend PP to include P and then delete P. */
3735 SEGMENT_END (p
, p
->p_vaddr
) - SEGMENT_END (pp
, pp
->p_vaddr
);
3737 if (extra_length
> 0)
3739 pp
->p_memsz
+= extra_length
;
3740 pp
->p_filesz
+= extra_length
;
3743 p
->p_type
= PT_NULL
;
3745 /* Since we have deleted P we must restart the outer loop. */
3747 p
= elf_tdata (ibfd
)->phdr
;
3752 /* Extend P to include PP and then delete PP. */
3754 SEGMENT_END (pp
, pp
->p_vaddr
) - SEGMENT_END (p
, p
->p_vaddr
);
3756 if (extra_length
> 0)
3758 p
->p_memsz
+= extra_length
;
3759 p
->p_filesz
+= extra_length
;
3762 pp
->p_type
= PT_NULL
;
3767 /* The second scan attempts to assign sections to segments. */
3768 for (i
= 0, p
= elf_tdata (ibfd
)->phdr
; i
< num_segments
; i
++, p
++)
3771 asection
**sections
;
3774 bfd_vma matching_lma
;
3775 bfd_vma suggested_lma
;
3778 if (p
->p_type
== PT_NULL
)
3781 /* Compute how many sections might be placed into this segment. */
3783 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3784 if (INCLUDE_SECTION_IN_SEGMENT (s
, p
))
3787 /* Allocate a segment map big enough to contain all of the
3788 sections we have selected. */
3789 m
= ((struct elf_segment_map
*)
3791 (sizeof (struct elf_segment_map
)
3792 + ((size_t) csecs
- 1) * sizeof (asection
*))));
3796 /* Initialise the fields of the segment map. Default to
3797 using the physical address of the segment in the input BFD. */
3799 m
->p_type
= p
->p_type
;
3800 m
->p_flags
= p
->p_flags
;
3801 m
->p_flags_valid
= 1;
3802 m
->p_paddr
= p
->p_paddr
;
3803 m
->p_paddr_valid
= 1;
3805 /* Determine if this segment contains the ELF file header
3806 and if it contains the program headers themselves. */
3807 m
->includes_filehdr
= (p
->p_offset
== 0
3808 && p
->p_filesz
>= iehdr
->e_ehsize
);
3810 m
->includes_phdrs
= 0;
3812 if (! phdr_included
|| p
->p_type
!= PT_LOAD
)
3815 (p
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3816 && (p
->p_offset
+ p
->p_filesz
3817 >= ((bfd_vma
) iehdr
->e_phoff
3818 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3819 if (p
->p_type
== PT_LOAD
&& m
->includes_phdrs
)
3820 phdr_included
= true;
3825 /* Special segments, such as the PT_PHDR segment, may contain
3826 no sections, but ordinary, loadable segments should contain
3828 if (p
->p_type
== PT_LOAD
)
3830 (_("%s: warning: Empty loadable segment detected\n"),
3831 bfd_get_filename (ibfd
));
3840 /* Now scan the sections in the input BFD again and attempt
3841 to add their corresponding output sections to the segment map.
3842 The problem here is how to handle an output section which has
3843 been moved (ie had its LMA changed). There are four possibilities:
3845 1. None of the sections have been moved.
3846 In this case we can continue to use the segment LMA from the
3849 2. All of the sections have been moved by the same amount.
3850 In this case we can change the segment's LMA to match the LMA
3851 of the first section.
3853 3. Some of the sections have been moved, others have not.
3854 In this case those sections which have not been moved can be
3855 placed in the current segment which will have to have its size,
3856 and possibly its LMA changed, and a new segment or segments will
3857 have to be created to contain the other sections.
3859 4. The sections have been moved, but not be the same amount.
3860 In this case we can change the segment's LMA to match the LMA
3861 of the first section and we will have to create a new segment
3862 or segments to contain the other sections.
3864 In order to save time, we allocate an array to hold the section
3865 pointers that we are interested in. As these sections get assigned
3866 to a segment, they are removed from this array. */
3868 sections
= (asection
**) bfd_malloc (sizeof (asection
*) * csecs
);
3869 if (sections
== NULL
)
3872 /* Step One: Scan for segment vs section LMA conflicts.
3873 Also add the sections to the section array allocated above.
3874 Also add the sections to the current segment. In the common
3875 case, where the sections have not been moved, this means that
3876 we have completely filled the segment, and there is nothing
3882 for (j
= 0, s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
3884 if (INCLUDE_SECTION_IN_SEGMENT (s
, p
))
3886 os
= s
->output_section
;
3890 /* The Solaris native linker always sets p_paddr to 0.
3891 We try to catch that case here, and set it to the
3897 && (os
->vma
== (p
->p_vaddr
3898 + (m
->includes_filehdr
3901 + (m
->includes_phdrs
3902 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3904 m
->p_paddr
= p
->p_vaddr
;
3906 /* Match up the physical address of the segment with the
3907 LMA address of the output section. */
3908 if (IS_CONTAINED_BY_LMA (os
, p
, m
->p_paddr
)
3909 || IS_COREFILE_NOTE (p
, s
))
3911 if (matching_lma
== 0)
3912 matching_lma
= os
->lma
;
3914 /* We assume that if the section fits within the segment
3915 then it does not overlap any other section within that
3917 m
->sections
[isec
++] = os
;
3919 else if (suggested_lma
== 0)
3920 suggested_lma
= os
->lma
;
3924 BFD_ASSERT (j
== csecs
);
3926 /* Step Two: Adjust the physical address of the current segment,
3930 /* All of the sections fitted within the segment as currently
3931 specified. This is the default case. Add the segment to
3932 the list of built segments and carry on to process the next
3933 program header in the input BFD. */
3943 if (matching_lma
!= 0)
3945 /* At least one section fits inside the current segment.
3946 Keep it, but modify its physical address to match the
3947 LMA of the first section that fitted. */
3948 m
->p_paddr
= matching_lma
;
3952 /* None of the sections fitted inside the current segment.
3953 Change the current segment's physical address to match
3954 the LMA of the first section. */
3955 m
->p_paddr
= suggested_lma
;
3958 /* Offset the segment physical address from the lma
3959 to allow for space taken up by elf headers. */
3960 if (m
->includes_filehdr
)
3961 m
->p_paddr
-= iehdr
->e_ehsize
;
3963 if (m
->includes_phdrs
)
3965 m
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
3967 /* iehdr->e_phnum is just an estimate of the number
3968 of program headers that we will need. Make a note
3969 here of the number we used and the segment we chose
3970 to hold these headers, so that we can adjust the
3971 offset when we know the correct value. */
3972 phdr_adjust_num
= iehdr
->e_phnum
;
3973 phdr_adjust_seg
= m
;
3977 /* Step Three: Loop over the sections again, this time assigning
3978 those that fit to the current segment and remvoing them from the
3979 sections array; but making sure not to leave large gaps. Once all
3980 possible sections have been assigned to the current segment it is
3981 added to the list of built segments and if sections still remain
3982 to be assigned, a new segment is constructed before repeating
3990 /* Fill the current segment with sections that fit. */
3991 for (j
= 0; j
< csecs
; j
++)
3998 os
= s
->output_section
;
4000 BFD_ASSERT (os
!= NULL
);
4002 if (IS_CONTAINED_BY_LMA (os
, p
, m
->p_paddr
)
4003 || IS_COREFILE_NOTE (p
, s
))
4007 /* If the first section in a segment does not start at
4008 the beginning of the segment, then something is wrong. */
4009 if (os
->lma
!= (m
->p_paddr
4010 + (m
->includes_filehdr
4011 ? iehdr
->e_ehsize
: 0)
4012 + (m
->includes_phdrs
4013 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4019 asection
* prev_sec
;
4021 prev_sec
= m
->sections
[m
->count
- 1];
4023 /* If the gap between the end of the previous section
4024 and the start of this section is more than maxpagesize
4025 then we need to start a new segment. */
4026 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4027 < BFD_ALIGN (os
->lma
, maxpagesize
))
4028 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > os
->lma
))
4030 if (suggested_lma
== 0)
4031 suggested_lma
= os
->lma
;
4037 m
->sections
[m
->count
++] = os
;
4040 s
->segment_mark
= true;
4042 else if (suggested_lma
== 0)
4043 suggested_lma
= os
->lma
;
4046 BFD_ASSERT (m
->count
> 0);
4048 /* Add the current segment to the list of built segments. */
4054 /* We still have not allocated all of the sections to
4055 segments. Create a new segment here, initialise it
4056 and carry on looping. */
4057 m
= ((struct elf_segment_map
*)
4059 (sizeof (struct elf_segment_map
)
4060 + ((size_t) csecs
- 1) * sizeof (asection
*))));
4064 /* Initialise the fields of the segment map. Set the physical
4065 physical address to the LMA of the first section that has
4066 not yet been assigned. */
4068 m
->p_type
= p
->p_type
;
4069 m
->p_flags
= p
->p_flags
;
4070 m
->p_flags_valid
= 1;
4071 m
->p_paddr
= suggested_lma
;
4072 m
->p_paddr_valid
= 1;
4073 m
->includes_filehdr
= 0;
4074 m
->includes_phdrs
= 0;
4077 while (isec
< csecs
);
4082 /* The Solaris linker creates program headers in which all the
4083 p_paddr fields are zero. When we try to objcopy or strip such a
4084 file, we get confused. Check for this case, and if we find it
4085 reset the p_paddr_valid fields. */
4086 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4087 if (m
->p_paddr
!= 0)
4091 for (m
= mfirst
; m
!= NULL
; m
= m
->next
)
4092 m
->p_paddr_valid
= 0;
4095 elf_tdata (obfd
)->segment_map
= mfirst
;
4097 /* If we had to estimate the number of program headers that were
4098 going to be needed, then check our estimate know and adjust
4099 the offset if necessary. */
4100 if (phdr_adjust_seg
!= NULL
)
4104 for (count
= 0, m
= mfirst
; m
!= NULL
; m
= m
->next
)
4107 if (count
> phdr_adjust_num
)
4108 phdr_adjust_seg
->p_paddr
-= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4112 /* Final Step: Sort the segments into ascending order of physical address. */
4115 struct elf_segment_map
* prev
;
4118 for (m
= mfirst
->next
; m
!= NULL
; prev
= m
, m
= m
->next
)
4120 /* Yes I know - its a bubble sort.... */
4121 if (m
->next
!= NULL
&& (m
->next
->p_paddr
< m
->p_paddr
))
4123 /* swap m and m->next. */
4124 prev
->next
= m
->next
;
4125 m
->next
= m
->next
->next
;
4126 prev
->next
->next
= m
;
4135 #undef IS_CONTAINED_BY
4136 #undef IS_SOLARIS_PT_INTERP
4137 #undef IS_COREFILE_NOTE
4141 /* Copy private section information. This copies over the entsize
4142 field, and sometimes the info field. */
4145 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4151 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4153 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4154 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4157 /* Copy over private BFD data if it has not already been copied.
4158 This must be done here, rather than in the copy_private_bfd_data
4159 entry point, because the latter is called after the section
4160 contents have been set, which means that the program headers have
4161 already been worked out. */
4162 if (elf_tdata (obfd
)->segment_map
== NULL
4163 && elf_tdata (ibfd
)->phdr
!= NULL
)
4167 /* Only set up the segments if there are no more SEC_ALLOC
4168 sections. FIXME: This won't do the right thing if objcopy is
4169 used to remove the last SEC_ALLOC section, since objcopy
4170 won't call this routine in that case. */
4171 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4172 if ((s
->flags
& SEC_ALLOC
) != 0)
4176 if (! copy_private_bfd_data (ibfd
, obfd
))
4181 ihdr
= &elf_section_data (isec
)->this_hdr
;
4182 ohdr
= &elf_section_data (osec
)->this_hdr
;
4184 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4186 if (ihdr
->sh_type
== SHT_SYMTAB
4187 || ihdr
->sh_type
== SHT_DYNSYM
4188 || ihdr
->sh_type
== SHT_GNU_verneed
4189 || ihdr
->sh_type
== SHT_GNU_verdef
)
4190 ohdr
->sh_info
= ihdr
->sh_info
;
4192 elf_section_data (osec
)->use_rela_p
4193 = elf_section_data (isec
)->use_rela_p
;
4198 /* Copy private symbol information. If this symbol is in a section
4199 which we did not map into a BFD section, try to map the section
4200 index correctly. We use special macro definitions for the mapped
4201 section indices; these definitions are interpreted by the
4202 swap_out_syms function. */
4204 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4205 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4206 #define MAP_STRTAB (SHN_LORESERVE - 3)
4207 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4210 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4216 elf_symbol_type
*isym
, *osym
;
4218 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4219 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4222 isym
= elf_symbol_from (ibfd
, isymarg
);
4223 osym
= elf_symbol_from (obfd
, osymarg
);
4227 && bfd_is_abs_section (isym
->symbol
.section
))
4231 shndx
= isym
->internal_elf_sym
.st_shndx
;
4232 if (shndx
== elf_onesymtab (ibfd
))
4233 shndx
= MAP_ONESYMTAB
;
4234 else if (shndx
== elf_dynsymtab (ibfd
))
4235 shndx
= MAP_DYNSYMTAB
;
4236 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4238 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4239 shndx
= MAP_SHSTRTAB
;
4240 osym
->internal_elf_sym
.st_shndx
= shndx
;
4246 /* Swap out the symbols. */
4249 swap_out_syms (abfd
, sttp
, relocatable_p
)
4251 struct bfd_strtab_hash
**sttp
;
4254 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4256 if (!elf_map_symbols (abfd
))
4259 /* Dump out the symtabs. */
4261 int symcount
= bfd_get_symcount (abfd
);
4262 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4263 struct bfd_strtab_hash
*stt
;
4264 Elf_Internal_Shdr
*symtab_hdr
;
4265 Elf_Internal_Shdr
*symstrtab_hdr
;
4266 char *outbound_syms
;
4269 stt
= _bfd_elf_stringtab_init ();
4273 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4274 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4275 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4276 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4277 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4278 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4280 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4281 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4283 outbound_syms
= bfd_alloc (abfd
,
4284 (1 + symcount
) * bed
->s
->sizeof_sym
);
4285 if (outbound_syms
== NULL
)
4287 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4289 /* now generate the data (for "contents") */
4291 /* Fill in zeroth symbol and swap it out. */
4292 Elf_Internal_Sym sym
;
4298 sym
.st_shndx
= SHN_UNDEF
;
4299 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4300 outbound_syms
+= bed
->s
->sizeof_sym
;
4302 for (idx
= 0; idx
< symcount
; idx
++)
4304 Elf_Internal_Sym sym
;
4305 bfd_vma value
= syms
[idx
]->value
;
4306 elf_symbol_type
*type_ptr
;
4307 flagword flags
= syms
[idx
]->flags
;
4310 if (flags
& BSF_SECTION_SYM
)
4311 /* Section symbols have no names. */
4315 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4318 if (sym
.st_name
== (unsigned long) -1)
4322 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4324 if ((flags
& BSF_SECTION_SYM
) == 0
4325 && bfd_is_com_section (syms
[idx
]->section
))
4327 /* ELF common symbols put the alignment into the `value' field,
4328 and the size into the `size' field. This is backwards from
4329 how BFD handles it, so reverse it here. */
4330 sym
.st_size
= value
;
4331 if (type_ptr
== NULL
4332 || type_ptr
->internal_elf_sym
.st_value
== 0)
4333 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4335 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4336 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4337 (abfd
, syms
[idx
]->section
);
4341 asection
*sec
= syms
[idx
]->section
;
4344 if (sec
->output_section
)
4346 value
+= sec
->output_offset
;
4347 sec
= sec
->output_section
;
4349 /* Don't add in the section vma for relocatable output. */
4350 if (! relocatable_p
)
4352 sym
.st_value
= value
;
4353 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4355 if (bfd_is_abs_section (sec
)
4357 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4359 /* This symbol is in a real ELF section which we did
4360 not create as a BFD section. Undo the mapping done
4361 by copy_private_symbol_data. */
4362 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4366 shndx
= elf_onesymtab (abfd
);
4369 shndx
= elf_dynsymtab (abfd
);
4372 shndx
= elf_tdata (abfd
)->strtab_section
;
4375 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4383 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4389 /* Writing this would be a hell of a lot easier if
4390 we had some decent documentation on bfd, and
4391 knew what to expect of the library, and what to
4392 demand of applications. For example, it
4393 appears that `objcopy' might not set the
4394 section of a symbol to be a section that is
4395 actually in the output file. */
4396 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4397 BFD_ASSERT (sec2
!= 0);
4398 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4399 BFD_ASSERT (shndx
!= -1);
4403 sym
.st_shndx
= shndx
;
4406 if ((flags
& BSF_FUNCTION
) != 0)
4408 else if ((flags
& BSF_OBJECT
) != 0)
4413 /* Processor-specific types */
4414 if (type_ptr
!= NULL
4415 && bed
->elf_backend_get_symbol_type
)
4416 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4418 if (flags
& BSF_SECTION_SYM
)
4419 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4420 else if (bfd_is_com_section (syms
[idx
]->section
))
4421 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4422 else if (bfd_is_und_section (syms
[idx
]->section
))
4423 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4427 else if (flags
& BSF_FILE
)
4428 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4431 int bind
= STB_LOCAL
;
4433 if (flags
& BSF_LOCAL
)
4435 else if (flags
& BSF_WEAK
)
4437 else if (flags
& BSF_GLOBAL
)
4440 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4443 if (type_ptr
!= NULL
)
4444 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4448 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4449 outbound_syms
+= bed
->s
->sizeof_sym
;
4453 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4454 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4456 symstrtab_hdr
->sh_flags
= 0;
4457 symstrtab_hdr
->sh_addr
= 0;
4458 symstrtab_hdr
->sh_entsize
= 0;
4459 symstrtab_hdr
->sh_link
= 0;
4460 symstrtab_hdr
->sh_info
= 0;
4461 symstrtab_hdr
->sh_addralign
= 1;
4467 /* Return the number of bytes required to hold the symtab vector.
4469 Note that we base it on the count plus 1, since we will null terminate
4470 the vector allocated based on this size. However, the ELF symbol table
4471 always has a dummy entry as symbol #0, so it ends up even. */
4474 _bfd_elf_get_symtab_upper_bound (abfd
)
4479 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4481 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4482 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4488 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4493 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4495 if (elf_dynsymtab (abfd
) == 0)
4497 bfd_set_error (bfd_error_invalid_operation
);
4501 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4502 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4508 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4509 bfd
*abfd ATTRIBUTE_UNUSED
;
4512 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4515 /* Canonicalize the relocs. */
4518 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4527 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4533 tblptr
= section
->relocation
;
4534 for (i
= 0; i
< section
->reloc_count
; i
++)
4535 *relptr
++ = tblptr
++;
4539 return section
->reloc_count
;
4543 _bfd_elf_get_symtab (abfd
, alocation
)
4545 asymbol
**alocation
;
4547 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4548 (abfd
, alocation
, false);
4551 bfd_get_symcount (abfd
) = symcount
;
4556 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4558 asymbol
**alocation
;
4560 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4561 (abfd
, alocation
, true);
4564 /* Return the size required for the dynamic reloc entries. Any
4565 section that was actually installed in the BFD, and has type
4566 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4567 considered to be a dynamic reloc section. */
4570 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4576 if (elf_dynsymtab (abfd
) == 0)
4578 bfd_set_error (bfd_error_invalid_operation
);
4582 ret
= sizeof (arelent
*);
4583 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4584 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4585 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4586 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4587 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4588 * sizeof (arelent
*));
4593 /* Canonicalize the dynamic relocation entries. Note that we return
4594 the dynamic relocations as a single block, although they are
4595 actually associated with particular sections; the interface, which
4596 was designed for SunOS style shared libraries, expects that there
4597 is only one set of dynamic relocs. Any section that was actually
4598 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4599 the dynamic symbol table, is considered to be a dynamic reloc
4603 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4608 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4612 if (elf_dynsymtab (abfd
) == 0)
4614 bfd_set_error (bfd_error_invalid_operation
);
4618 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4620 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4622 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4623 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4624 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4629 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4631 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4633 for (i
= 0; i
< count
; i
++)
4644 /* Read in the version information. */
4647 _bfd_elf_slurp_version_tables (abfd
)
4650 bfd_byte
*contents
= NULL
;
4652 if (elf_dynverdef (abfd
) != 0)
4654 Elf_Internal_Shdr
*hdr
;
4655 Elf_External_Verdef
*everdef
;
4656 Elf_Internal_Verdef
*iverdef
;
4657 Elf_Internal_Verdef
*iverdefarr
;
4658 Elf_Internal_Verdef iverdefmem
;
4660 unsigned int maxidx
;
4662 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4664 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4665 if (contents
== NULL
)
4667 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4668 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4671 /* We know the number of entries in the section but not the maximum
4672 index. Therefore we have to run through all entries and find
4674 everdef
= (Elf_External_Verdef
*) contents
;
4676 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4678 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4680 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) > maxidx
)
4681 maxidx
= iverdefmem
.vd_ndx
& VERSYM_VERSION
;
4683 everdef
= ((Elf_External_Verdef
*)
4684 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4687 elf_tdata (abfd
)->verdef
=
4688 ((Elf_Internal_Verdef
*)
4689 bfd_zalloc (abfd
, maxidx
* sizeof (Elf_Internal_Verdef
)));
4690 if (elf_tdata (abfd
)->verdef
== NULL
)
4693 elf_tdata (abfd
)->cverdefs
= maxidx
;
4695 everdef
= (Elf_External_Verdef
*) contents
;
4696 iverdefarr
= elf_tdata (abfd
)->verdef
;
4697 for (i
= 0; i
< hdr
->sh_info
; i
++)
4699 Elf_External_Verdaux
*everdaux
;
4700 Elf_Internal_Verdaux
*iverdaux
;
4703 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4705 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4706 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4708 iverdef
->vd_bfd
= abfd
;
4710 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4713 * sizeof (Elf_Internal_Verdaux
))));
4714 if (iverdef
->vd_auxptr
== NULL
)
4717 everdaux
= ((Elf_External_Verdaux
*)
4718 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4719 iverdaux
= iverdef
->vd_auxptr
;
4720 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4722 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4724 iverdaux
->vda_nodename
=
4725 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4726 iverdaux
->vda_name
);
4727 if (iverdaux
->vda_nodename
== NULL
)
4730 if (j
+ 1 < iverdef
->vd_cnt
)
4731 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4733 iverdaux
->vda_nextptr
= NULL
;
4735 everdaux
= ((Elf_External_Verdaux
*)
4736 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4739 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4741 if (i
+ 1 < hdr
->sh_info
)
4742 iverdef
->vd_nextdef
= iverdef
+ 1;
4744 iverdef
->vd_nextdef
= NULL
;
4746 everdef
= ((Elf_External_Verdef
*)
4747 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4754 if (elf_dynverref (abfd
) != 0)
4756 Elf_Internal_Shdr
*hdr
;
4757 Elf_External_Verneed
*everneed
;
4758 Elf_Internal_Verneed
*iverneed
;
4761 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4763 elf_tdata (abfd
)->verref
=
4764 ((Elf_Internal_Verneed
*)
4765 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4766 if (elf_tdata (abfd
)->verref
== NULL
)
4769 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4771 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4772 if (contents
== NULL
)
4774 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4775 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4778 everneed
= (Elf_External_Verneed
*) contents
;
4779 iverneed
= elf_tdata (abfd
)->verref
;
4780 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4782 Elf_External_Vernaux
*evernaux
;
4783 Elf_Internal_Vernaux
*ivernaux
;
4786 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4788 iverneed
->vn_bfd
= abfd
;
4790 iverneed
->vn_filename
=
4791 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4793 if (iverneed
->vn_filename
== NULL
)
4796 iverneed
->vn_auxptr
=
4797 ((Elf_Internal_Vernaux
*)
4799 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4801 evernaux
= ((Elf_External_Vernaux
*)
4802 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4803 ivernaux
= iverneed
->vn_auxptr
;
4804 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4806 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4808 ivernaux
->vna_nodename
=
4809 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4810 ivernaux
->vna_name
);
4811 if (ivernaux
->vna_nodename
== NULL
)
4814 if (j
+ 1 < iverneed
->vn_cnt
)
4815 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4817 ivernaux
->vna_nextptr
= NULL
;
4819 evernaux
= ((Elf_External_Vernaux
*)
4820 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4823 if (i
+ 1 < hdr
->sh_info
)
4824 iverneed
->vn_nextref
= iverneed
+ 1;
4826 iverneed
->vn_nextref
= NULL
;
4828 everneed
= ((Elf_External_Verneed
*)
4829 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4839 if (contents
== NULL
)
4845 _bfd_elf_make_empty_symbol (abfd
)
4848 elf_symbol_type
*newsym
;
4850 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4855 newsym
->symbol
.the_bfd
= abfd
;
4856 return &newsym
->symbol
;
4861 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4862 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4866 bfd_symbol_info (symbol
, ret
);
4869 /* Return whether a symbol name implies a local symbol. Most targets
4870 use this function for the is_local_label_name entry point, but some
4874 _bfd_elf_is_local_label_name (abfd
, name
)
4875 bfd
*abfd ATTRIBUTE_UNUSED
;
4878 /* Normal local symbols start with ``.L''. */
4879 if (name
[0] == '.' && name
[1] == 'L')
4882 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4883 DWARF debugging symbols starting with ``..''. */
4884 if (name
[0] == '.' && name
[1] == '.')
4887 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4888 emitting DWARF debugging output. I suspect this is actually a
4889 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4890 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4891 underscore to be emitted on some ELF targets). For ease of use,
4892 we treat such symbols as local. */
4893 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4900 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4901 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4902 asymbol
*symbol ATTRIBUTE_UNUSED
;
4909 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4911 enum bfd_architecture arch
;
4912 unsigned long machine
;
4914 /* If this isn't the right architecture for this backend, and this
4915 isn't the generic backend, fail. */
4916 if (arch
!= get_elf_backend_data (abfd
)->arch
4917 && arch
!= bfd_arch_unknown
4918 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4921 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4924 /* Find the nearest line to a particular section and offset, for error
4928 _bfd_elf_find_nearest_line (abfd
,
4939 CONST
char **filename_ptr
;
4940 CONST
char **functionname_ptr
;
4941 unsigned int *line_ptr
;
4944 const char *filename
;
4949 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
4950 filename_ptr
, functionname_ptr
,
4954 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
4955 filename_ptr
, functionname_ptr
,
4959 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
4960 &found
, filename_ptr
,
4961 functionname_ptr
, line_ptr
,
4962 &elf_tdata (abfd
)->line_info
))
4967 if (symbols
== NULL
)
4974 for (p
= symbols
; *p
!= NULL
; p
++)
4978 q
= (elf_symbol_type
*) *p
;
4980 if (bfd_get_section (&q
->symbol
) != section
)
4983 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
4988 filename
= bfd_asymbol_name (&q
->symbol
);
4992 if (q
->symbol
.section
== section
4993 && q
->symbol
.value
>= low_func
4994 && q
->symbol
.value
<= offset
)
4996 func
= (asymbol
*) q
;
4997 low_func
= q
->symbol
.value
;
5006 *filename_ptr
= filename
;
5007 *functionname_ptr
= bfd_asymbol_name (func
);
5013 _bfd_elf_sizeof_headers (abfd
, reloc
)
5019 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5021 ret
+= get_program_header_size (abfd
);
5026 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5031 bfd_size_type count
;
5033 Elf_Internal_Shdr
*hdr
;
5035 if (! abfd
->output_has_begun
5036 && ! _bfd_elf_compute_section_file_positions
5037 (abfd
, (struct bfd_link_info
*) NULL
))
5040 hdr
= &elf_section_data (section
)->this_hdr
;
5042 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
5044 if (bfd_write (location
, 1, count
, abfd
) != count
)
5051 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5052 bfd
*abfd ATTRIBUTE_UNUSED
;
5053 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5054 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5061 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5064 Elf_Internal_Rel
*dst
;
5070 /* Try to convert a non-ELF reloc into an ELF one. */
5073 _bfd_elf_validate_reloc (abfd
, areloc
)
5077 /* Check whether we really have an ELF howto. */
5079 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5081 bfd_reloc_code_real_type code
;
5082 reloc_howto_type
*howto
;
5084 /* Alien reloc: Try to determine its type to replace it with an
5085 equivalent ELF reloc. */
5087 if (areloc
->howto
->pc_relative
)
5089 switch (areloc
->howto
->bitsize
)
5092 code
= BFD_RELOC_8_PCREL
;
5095 code
= BFD_RELOC_12_PCREL
;
5098 code
= BFD_RELOC_16_PCREL
;
5101 code
= BFD_RELOC_24_PCREL
;
5104 code
= BFD_RELOC_32_PCREL
;
5107 code
= BFD_RELOC_64_PCREL
;
5113 howto
= bfd_reloc_type_lookup (abfd
, code
);
5115 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5117 if (howto
->pcrel_offset
)
5118 areloc
->addend
+= areloc
->address
;
5120 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5125 switch (areloc
->howto
->bitsize
)
5131 code
= BFD_RELOC_14
;
5134 code
= BFD_RELOC_16
;
5137 code
= BFD_RELOC_26
;
5140 code
= BFD_RELOC_32
;
5143 code
= BFD_RELOC_64
;
5149 howto
= bfd_reloc_type_lookup (abfd
, code
);
5153 areloc
->howto
= howto
;
5161 (*_bfd_error_handler
)
5162 (_("%s: unsupported relocation type %s"),
5163 bfd_get_filename (abfd
), areloc
->howto
->name
);
5164 bfd_set_error (bfd_error_bad_value
);
5169 _bfd_elf_close_and_cleanup (abfd
)
5172 if (bfd_get_format (abfd
) == bfd_object
)
5174 if (elf_shstrtab (abfd
) != NULL
)
5175 _bfd_stringtab_free (elf_shstrtab (abfd
));
5178 return _bfd_generic_close_and_cleanup (abfd
);
5181 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5182 in the relocation's offset. Thus we cannot allow any sort of sanity
5183 range-checking to interfere. There is nothing else to do in processing
5186 bfd_reloc_status_type
5187 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5188 bfd
*abfd ATTRIBUTE_UNUSED
;
5189 arelent
*re ATTRIBUTE_UNUSED
;
5190 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5191 PTR data ATTRIBUTE_UNUSED
;
5192 asection
*is ATTRIBUTE_UNUSED
;
5193 bfd
*obfd ATTRIBUTE_UNUSED
;
5194 char **errmsg ATTRIBUTE_UNUSED
;
5196 return bfd_reloc_ok
;
5200 /* Elf core file support. Much of this only works on native
5201 toolchains, since we rely on knowing the
5202 machine-dependent procfs structure in order to pick
5203 out details about the corefile. */
5205 #ifdef HAVE_SYS_PROCFS_H
5206 # include <sys/procfs.h>
5210 /* Define offsetof for those systems which lack it. */
5213 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5217 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5220 elfcore_make_pid (abfd
)
5223 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5224 + (elf_tdata (abfd
)->core_pid
));
5228 /* If there isn't a section called NAME, make one, using
5229 data from SECT. Note, this function will generate a
5230 reference to NAME, so you shouldn't deallocate or
5234 elfcore_maybe_make_sect (abfd
, name
, sect
)
5241 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5244 sect2
= bfd_make_section (abfd
, name
);
5248 sect2
->_raw_size
= sect
->_raw_size
;
5249 sect2
->filepos
= sect
->filepos
;
5250 sect2
->flags
= sect
->flags
;
5251 sect2
->alignment_power
= sect
->alignment_power
;
5256 /* prstatus_t exists on:
5258 linux 2.[01] + glibc
5262 #if defined (HAVE_PRSTATUS_T)
5264 elfcore_grok_prstatus (abfd
, note
)
5266 Elf_Internal_Note
* note
;
5273 if (note
->descsz
== sizeof (prstatus_t
))
5277 raw_size
= sizeof (prstat
.pr_reg
);
5278 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5280 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5281 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5283 /* pr_who exists on:
5286 pr_who doesn't exist on:
5289 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5290 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5293 #if defined (__sparcv9)
5294 else if (note
->descsz
== sizeof (prstatus32_t
))
5296 /* 64-bit host, 32-bit corefile */
5297 prstatus32_t prstat
;
5299 raw_size
= sizeof (prstat
.pr_reg
);
5300 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5302 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5303 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5305 /* pr_who exists on:
5308 pr_who doesn't exist on:
5311 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5312 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5315 #endif /* __sparcv9 */
5318 /* Fail - we don't know how to handle any other
5319 note size (ie. data object type). */
5323 /* Make a ".reg/999" section. */
5325 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5326 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5331 sect
= bfd_make_section (abfd
, name
);
5335 if (note
->descsz
== sizeof (prstatus_t
))
5337 sect
->_raw_size
= raw_size
;
5338 sect
->filepos
= note
->descpos
+ offsetof (prstatus_t
, pr_reg
);
5340 #if defined (__sparcv9)
5341 else if (note
->descsz
== sizeof (prstatus32_t
))
5343 sect
->_raw_size
= raw_size
;
5344 sect
->filepos
= note
->descpos
+ offsetof (prstatus32_t
, pr_reg
);
5348 sect
->flags
= SEC_HAS_CONTENTS
;
5349 sect
->alignment_power
= 2;
5351 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5356 #endif /* defined (HAVE_PRSTATUS_T) */
5359 /* Create a pseudosection containing the exact contents of NOTE. This
5360 actually creates up to two pseudosections:
5361 - For the single-threaded case, a section named NAME, unless
5362 such a section already exists.
5363 - For the multi-threaded case, a section named "NAME/PID", where
5364 PID is elfcore_make_pid (abfd).
5365 Both pseudosections have identical contents: the contents of NOTE. */
5368 elfcore_make_note_pseudosection (abfd
, name
, note
)
5371 Elf_Internal_Note
* note
;
5374 char *threaded_name
;
5377 /* Build the section name. */
5379 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5380 threaded_name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5381 if (threaded_name
== NULL
)
5383 strcpy (threaded_name
, buf
);
5385 sect
= bfd_make_section (abfd
, threaded_name
);
5388 sect
->_raw_size
= note
->descsz
;
5389 sect
->filepos
= note
->descpos
;
5390 sect
->flags
= SEC_HAS_CONTENTS
;
5391 sect
->alignment_power
= 2;
5393 if (! elfcore_maybe_make_sect (abfd
, name
, sect
))
5400 /* There isn't a consistent prfpregset_t across platforms,
5401 but it doesn't matter, because we don't have to pick this
5402 data structure apart. */
5404 elfcore_grok_prfpreg (abfd
, note
)
5406 Elf_Internal_Note
* note
;
5408 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5412 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5413 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5416 elfcore_grok_prxfpreg (abfd
, note
)
5418 Elf_Internal_Note
* note
;
5420 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5424 #if defined (HAVE_PRPSINFO_T)
5425 typedef prpsinfo_t elfcore_psinfo_t
;
5426 #if defined (__sparcv9) /* Sparc64 cross Sparc32 */
5427 typedef prpsinfo32_t elfcore_psinfo32_t
;
5431 #if defined (HAVE_PSINFO_T)
5432 typedef psinfo_t elfcore_psinfo_t
;
5433 #if defined (__sparcv9) /* Sparc64 cross Sparc32 */
5434 typedef psinfo32_t elfcore_psinfo32_t
;
5439 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5441 /* return a malloc'ed copy of a string at START which is at
5442 most MAX bytes long, possibly without a terminating '\0'.
5443 the copy will always have a terminating '\0'. */
5446 elfcore_strndup (abfd
, start
, max
)
5452 char* end
= memchr (start
, '\0', max
);
5460 dup
= bfd_alloc (abfd
, len
+ 1);
5464 memcpy (dup
, start
, len
);
5471 elfcore_grok_psinfo (abfd
, note
)
5473 Elf_Internal_Note
* note
;
5475 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5477 elfcore_psinfo_t psinfo
;
5479 memcpy (&psinfo
, note
->descdata
, note
->descsz
);
5481 elf_tdata (abfd
)->core_program
5482 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5484 elf_tdata (abfd
)->core_command
5485 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5487 #if defined (__sparcv9)
5488 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5490 /* 64-bit host, 32-bit corefile */
5491 elfcore_psinfo32_t psinfo
;
5493 memcpy (&psinfo
, note
->descdata
, note
->descsz
);
5495 elf_tdata (abfd
)->core_program
5496 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5498 elf_tdata (abfd
)->core_command
5499 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5505 /* Fail - we don't know how to handle any other
5506 note size (ie. data object type). */
5510 /* Note that for some reason, a spurious space is tacked
5511 onto the end of the args in some (at least one anyway)
5512 implementations, so strip it off if it exists. */
5515 char* command
= elf_tdata (abfd
)->core_command
;
5516 int n
= strlen (command
);
5518 if (0 < n
&& command
[n
- 1] == ' ')
5519 command
[n
- 1] = '\0';
5524 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5527 #if defined (HAVE_PSTATUS_T)
5529 elfcore_grok_pstatus (abfd
, note
)
5531 Elf_Internal_Note
* note
;
5533 if (note
->descsz
== sizeof (pstatus_t
))
5537 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5539 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5541 #if defined (__sparcv9)
5542 else if (note
->descsz
== sizeof (pstatus32_t
))
5544 /* 64-bit host, 32-bit corefile */
5547 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5549 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5552 /* Could grab some more details from the "representative"
5553 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5554 NT_LWPSTATUS note, presumably. */
5558 #endif /* defined (HAVE_PSTATUS_T) */
5561 #if defined (HAVE_LWPSTATUS_T)
5563 elfcore_grok_lwpstatus (abfd
, note
)
5565 Elf_Internal_Note
* note
;
5567 lwpstatus_t lwpstat
;
5572 if (note
->descsz
!= sizeof (lwpstat
))
5575 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5577 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5578 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5580 /* Make a ".reg/999" section. */
5582 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5583 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5588 sect
= bfd_make_section (abfd
, name
);
5592 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5593 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5594 sect
->filepos
= note
->descpos
5595 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5598 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5599 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5600 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5603 sect
->flags
= SEC_HAS_CONTENTS
;
5604 sect
->alignment_power
= 2;
5606 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5609 /* Make a ".reg2/999" section */
5611 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5612 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5617 sect
= bfd_make_section (abfd
, name
);
5621 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5622 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5623 sect
->filepos
= note
->descpos
5624 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5627 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5628 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5629 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5632 sect
->flags
= SEC_HAS_CONTENTS
;
5633 sect
->alignment_power
= 2;
5635 if (!elfcore_maybe_make_sect (abfd
, ".reg2", sect
))
5640 #endif /* defined (HAVE_LWPSTATUS_T) */
5642 #if defined (HAVE_WIN32_PSTATUS_T)
5644 elfcore_grok_win32pstatus (abfd
, note
)
5646 Elf_Internal_Note
* note
;
5651 win32_pstatus_t pstatus
;
5653 if (note
->descsz
< sizeof (pstatus
))
5656 memcpy (& pstatus
, note
->descdata
, note
->descsz
);
5658 switch (pstatus
.data_type
)
5660 case NOTE_INFO_PROCESS
:
5661 /* FIXME: need to add ->core_command. */
5662 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5663 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5666 case NOTE_INFO_THREAD
:
5667 /* Make a ".reg/999" section. */
5668 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5670 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5676 sect
= bfd_make_section (abfd
, name
);
5680 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5681 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5682 data
.thread_info
.thread_context
);
5683 sect
->flags
= SEC_HAS_CONTENTS
;
5684 sect
->alignment_power
= 2;
5686 if (pstatus
.data
.thread_info
.is_active_thread
)
5687 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5691 case NOTE_INFO_MODULE
:
5692 /* Make a ".module/xxxxxxxx" section. */
5693 sprintf (buf
, ".module/%08x" , pstatus
.data
.module_info
.base_address
);
5695 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5701 sect
= bfd_make_section (abfd
, name
);
5706 sect
->_raw_size
= note
->descsz
;
5707 sect
->filepos
= note
->descpos
;
5708 sect
->flags
= SEC_HAS_CONTENTS
;
5709 sect
->alignment_power
= 2;
5718 #endif /* HAVE_WIN32_PSTATUS_T */
5721 elfcore_grok_note (abfd
, note
)
5723 Elf_Internal_Note
* note
;
5730 #if defined (HAVE_PRSTATUS_T)
5732 return elfcore_grok_prstatus (abfd
, note
);
5735 #if defined (HAVE_PSTATUS_T)
5737 return elfcore_grok_pstatus (abfd
, note
);
5740 #if defined (HAVE_LWPSTATUS_T)
5742 return elfcore_grok_lwpstatus (abfd
, note
);
5745 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5746 return elfcore_grok_prfpreg (abfd
, note
);
5748 #if defined (HAVE_WIN32_PSTATUS_T)
5749 case NT_WIN32PSTATUS
:
5750 return elfcore_grok_win32pstatus (abfd
, note
);
5753 case NT_PRXFPREG
: /* Linux SSE extension */
5754 if (note
->namesz
== 5
5755 && ! strcmp (note
->namedata
, "LINUX"))
5756 return elfcore_grok_prxfpreg (abfd
, note
);
5760 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5763 return elfcore_grok_psinfo (abfd
, note
);
5770 elfcore_read_notes (abfd
, offset
, size
)
5781 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
5784 buf
= bfd_malloc ((size_t) size
);
5788 if (bfd_read (buf
, size
, 1, abfd
) != size
)
5796 while (p
< buf
+ size
)
5798 /* FIXME: bad alignment assumption. */
5799 Elf_External_Note
* xnp
= (Elf_External_Note
*) p
;
5800 Elf_Internal_Note in
;
5802 in
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->type
);
5804 in
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->namesz
);
5805 in
.namedata
= xnp
->name
;
5807 in
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->descsz
);
5808 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5809 in
.descpos
= offset
+ (in
.descdata
- buf
);
5811 if (! elfcore_grok_note (abfd
, &in
))
5814 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5822 /* FIXME: This function is now unnecessary. Callers can just call
5823 bfd_section_from_phdr directly. */
5826 _bfd_elfcore_section_from_phdr (abfd
, phdr
, sec_num
)
5828 Elf_Internal_Phdr
* phdr
;
5831 if (! bfd_section_from_phdr (abfd
, phdr
, sec_num
))
5839 /* Providing external access to the ELF program header table. */
5841 /* Return an upper bound on the number of bytes required to store a
5842 copy of ABFD's program header table entries. Return -1 if an error
5843 occurs; bfd_get_error will return an appropriate code. */
5845 bfd_get_elf_phdr_upper_bound (abfd
)
5848 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5850 bfd_set_error (bfd_error_wrong_format
);
5854 return (elf_elfheader (abfd
)->e_phnum
5855 * sizeof (Elf_Internal_Phdr
));
5859 /* Copy ABFD's program header table entries to *PHDRS. The entries
5860 will be stored as an array of Elf_Internal_Phdr structures, as
5861 defined in include/elf/internal.h. To find out how large the
5862 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5864 Return the number of program header table entries read, or -1 if an
5865 error occurs; bfd_get_error will return an appropriate code. */
5867 bfd_get_elf_phdrs (abfd
, phdrs
)
5873 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5875 bfd_set_error (bfd_error_wrong_format
);
5879 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
5880 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
5881 num_phdrs
* sizeof (Elf_Internal_Phdr
));