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
34 /* For sparc64-cross-sparc32. */
43 static INLINE
struct elf_segment_map
*make_mapping
44 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
45 static boolean map_sections_to_segments
PARAMS ((bfd
*));
46 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
47 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
48 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
49 static boolean prep_headers
PARAMS ((bfd
*));
50 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
51 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
52 static char *elf_read
PARAMS ((bfd
*, long, unsigned int));
53 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
54 static boolean assign_section_numbers
PARAMS ((bfd
*));
55 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
56 static boolean elf_map_symbols
PARAMS ((bfd
*));
57 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
58 static boolean elfcore_read_notes
PARAMS ((bfd
*, bfd_vma
, bfd_vma
));
60 /* Swap version information in and out. The version information is
61 currently size independent. If that ever changes, this code will
62 need to move into elfcode.h. */
64 /* Swap in a Verdef structure. */
67 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
69 const Elf_External_Verdef
*src
;
70 Elf_Internal_Verdef
*dst
;
72 dst
->vd_version
= bfd_h_get_16 (abfd
, src
->vd_version
);
73 dst
->vd_flags
= bfd_h_get_16 (abfd
, src
->vd_flags
);
74 dst
->vd_ndx
= bfd_h_get_16 (abfd
, src
->vd_ndx
);
75 dst
->vd_cnt
= bfd_h_get_16 (abfd
, src
->vd_cnt
);
76 dst
->vd_hash
= bfd_h_get_32 (abfd
, src
->vd_hash
);
77 dst
->vd_aux
= bfd_h_get_32 (abfd
, src
->vd_aux
);
78 dst
->vd_next
= bfd_h_get_32 (abfd
, src
->vd_next
);
81 /* Swap out a Verdef structure. */
84 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
86 const Elf_Internal_Verdef
*src
;
87 Elf_External_Verdef
*dst
;
89 bfd_h_put_16 (abfd
, src
->vd_version
, dst
->vd_version
);
90 bfd_h_put_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
91 bfd_h_put_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
92 bfd_h_put_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
93 bfd_h_put_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
94 bfd_h_put_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
95 bfd_h_put_32 (abfd
, src
->vd_next
, dst
->vd_next
);
98 /* Swap in a Verdaux structure. */
101 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
103 const Elf_External_Verdaux
*src
;
104 Elf_Internal_Verdaux
*dst
;
106 dst
->vda_name
= bfd_h_get_32 (abfd
, src
->vda_name
);
107 dst
->vda_next
= bfd_h_get_32 (abfd
, src
->vda_next
);
110 /* Swap out a Verdaux structure. */
113 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
115 const Elf_Internal_Verdaux
*src
;
116 Elf_External_Verdaux
*dst
;
118 bfd_h_put_32 (abfd
, src
->vda_name
, dst
->vda_name
);
119 bfd_h_put_32 (abfd
, src
->vda_next
, dst
->vda_next
);
122 /* Swap in a Verneed structure. */
125 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
127 const Elf_External_Verneed
*src
;
128 Elf_Internal_Verneed
*dst
;
130 dst
->vn_version
= bfd_h_get_16 (abfd
, src
->vn_version
);
131 dst
->vn_cnt
= bfd_h_get_16 (abfd
, src
->vn_cnt
);
132 dst
->vn_file
= bfd_h_get_32 (abfd
, src
->vn_file
);
133 dst
->vn_aux
= bfd_h_get_32 (abfd
, src
->vn_aux
);
134 dst
->vn_next
= bfd_h_get_32 (abfd
, src
->vn_next
);
137 /* Swap out a Verneed structure. */
140 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
142 const Elf_Internal_Verneed
*src
;
143 Elf_External_Verneed
*dst
;
145 bfd_h_put_16 (abfd
, src
->vn_version
, dst
->vn_version
);
146 bfd_h_put_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
147 bfd_h_put_32 (abfd
, src
->vn_file
, dst
->vn_file
);
148 bfd_h_put_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
149 bfd_h_put_32 (abfd
, src
->vn_next
, dst
->vn_next
);
152 /* Swap in a Vernaux structure. */
155 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
157 const Elf_External_Vernaux
*src
;
158 Elf_Internal_Vernaux
*dst
;
160 dst
->vna_hash
= bfd_h_get_32 (abfd
, src
->vna_hash
);
161 dst
->vna_flags
= bfd_h_get_16 (abfd
, src
->vna_flags
);
162 dst
->vna_other
= bfd_h_get_16 (abfd
, src
->vna_other
);
163 dst
->vna_name
= bfd_h_get_32 (abfd
, src
->vna_name
);
164 dst
->vna_next
= bfd_h_get_32 (abfd
, src
->vna_next
);
167 /* Swap out a Vernaux structure. */
170 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
172 const Elf_Internal_Vernaux
*src
;
173 Elf_External_Vernaux
*dst
;
175 bfd_h_put_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
176 bfd_h_put_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
177 bfd_h_put_16 (abfd
, src
->vna_other
, dst
->vna_other
);
178 bfd_h_put_32 (abfd
, src
->vna_name
, dst
->vna_name
);
179 bfd_h_put_32 (abfd
, src
->vna_next
, dst
->vna_next
);
182 /* Swap in a Versym structure. */
185 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
187 const Elf_External_Versym
*src
;
188 Elf_Internal_Versym
*dst
;
190 dst
->vs_vers
= bfd_h_get_16 (abfd
, src
->vs_vers
);
193 /* Swap out a Versym structure. */
196 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
198 const Elf_Internal_Versym
*src
;
199 Elf_External_Versym
*dst
;
201 bfd_h_put_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
204 /* Standard ELF hash function. Do not change this function; you will
205 cause invalid hash tables to be generated. */
208 bfd_elf_hash (namearg
)
211 const unsigned char *name
= (const unsigned char *) namearg
;
216 while ((ch
= *name
++) != '\0')
219 if ((g
= (h
& 0xf0000000)) != 0)
222 /* The ELF ABI says `h &= ~g', but this is equivalent in
223 this case and on some machines one insn instead of two. */
230 /* Read a specified number of bytes at a specified offset in an ELF
231 file, into a newly allocated buffer, and return a pointer to the
235 elf_read (abfd
, offset
, size
)
242 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
244 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
246 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
248 if (bfd_get_error () != bfd_error_system_call
)
249 bfd_set_error (bfd_error_file_truncated
);
256 bfd_elf_mkobject (abfd
)
259 /* This just does initialization. */
260 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
261 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
262 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
263 if (elf_tdata (abfd
) == 0)
265 /* Since everything is done at close time, do we need any
272 bfd_elf_mkcorefile (abfd
)
275 /* I think this can be done just like an object file. */
276 return bfd_elf_mkobject (abfd
);
280 bfd_elf_get_str_section (abfd
, shindex
)
282 unsigned int shindex
;
284 Elf_Internal_Shdr
**i_shdrp
;
285 char *shstrtab
= NULL
;
287 unsigned int shstrtabsize
;
289 i_shdrp
= elf_elfsections (abfd
);
290 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
293 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
294 if (shstrtab
== NULL
)
296 /* No cached one, attempt to read, and cache what we read. */
297 offset
= i_shdrp
[shindex
]->sh_offset
;
298 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
299 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
300 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
306 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
308 unsigned int shindex
;
309 unsigned int strindex
;
311 Elf_Internal_Shdr
*hdr
;
316 hdr
= elf_elfsections (abfd
)[shindex
];
318 if (hdr
->contents
== NULL
319 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
322 if (strindex
>= hdr
->sh_size
)
324 (*_bfd_error_handler
)
325 (_("%s: invalid string offset %u >= %lu for section `%s'"),
326 bfd_get_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
327 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
328 && strindex
== hdr
->sh_name
)
330 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
334 return ((char *) hdr
->contents
) + strindex
;
337 /* Make a BFD section from an ELF section. We store a pointer to the
338 BFD section in the bfd_section field of the header. */
341 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
343 Elf_Internal_Shdr
*hdr
;
348 struct elf_backend_data
*bed
;
350 if (hdr
->bfd_section
!= NULL
)
352 BFD_ASSERT (strcmp (name
,
353 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
357 newsect
= bfd_make_section_anyway (abfd
, name
);
361 newsect
->filepos
= hdr
->sh_offset
;
363 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
364 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
365 || ! bfd_set_section_alignment (abfd
, newsect
,
366 bfd_log2 (hdr
->sh_addralign
)))
369 flags
= SEC_NO_FLAGS
;
370 if (hdr
->sh_type
!= SHT_NOBITS
)
371 flags
|= SEC_HAS_CONTENTS
;
372 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
375 if (hdr
->sh_type
!= SHT_NOBITS
)
378 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
379 flags
|= SEC_READONLY
;
380 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
382 else if ((flags
& SEC_LOAD
) != 0)
385 /* The debugging sections appear to be recognized only by name, not
388 static const char *debug_sec_names
[] =
397 for (i
= sizeof (debug_sec_names
) / sizeof (debug_sec_names
[0]); i
--;)
398 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
402 flags
|= SEC_DEBUGGING
;
405 /* As a GNU extension, if the name begins with .gnu.linkonce, we
406 only link a single copy of the section. This is used to support
407 g++. g++ will emit each template expansion in its own section.
408 The symbols will be defined as weak, so that multiple definitions
409 are permitted. The GNU linker extension is to actually discard
410 all but one of the sections. */
411 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
412 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
414 bed
= get_elf_backend_data (abfd
);
415 if (bed
->elf_backend_section_flags
)
416 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
419 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
422 if ((flags
& SEC_ALLOC
) != 0)
424 Elf_Internal_Phdr
*phdr
;
427 /* Look through the phdrs to see if we need to adjust the lma.
428 If all the p_paddr fields are zero, we ignore them, since
429 some ELF linkers produce such output. */
430 phdr
= elf_tdata (abfd
)->phdr
;
431 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
433 if (phdr
->p_paddr
!= 0)
436 if (i
< elf_elfheader (abfd
)->e_phnum
)
438 phdr
= elf_tdata (abfd
)->phdr
;
439 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
441 if (phdr
->p_type
== PT_LOAD
442 && phdr
->p_vaddr
!= phdr
->p_paddr
443 && phdr
->p_vaddr
<= hdr
->sh_addr
444 && (phdr
->p_vaddr
+ phdr
->p_memsz
445 >= hdr
->sh_addr
+ hdr
->sh_size
)
446 && ((flags
& SEC_LOAD
) == 0
447 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
448 && (phdr
->p_offset
+ phdr
->p_filesz
449 >= hdr
->sh_offset
+ hdr
->sh_size
))))
451 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
458 hdr
->bfd_section
= newsect
;
459 elf_section_data (newsect
)->this_hdr
= *hdr
;
469 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
472 Helper functions for GDB to locate the string tables.
473 Since BFD hides string tables from callers, GDB needs to use an
474 internal hook to find them. Sun's .stabstr, in particular,
475 isn't even pointed to by the .stab section, so ordinary
476 mechanisms wouldn't work to find it, even if we had some.
479 struct elf_internal_shdr
*
480 bfd_elf_find_section (abfd
, name
)
484 Elf_Internal_Shdr
**i_shdrp
;
489 i_shdrp
= elf_elfsections (abfd
);
492 shstrtab
= bfd_elf_get_str_section
493 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
494 if (shstrtab
!= NULL
)
496 max
= elf_elfheader (abfd
)->e_shnum
;
497 for (i
= 1; i
< max
; i
++)
498 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
505 const char *const bfd_elf_section_type_names
[] = {
506 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
507 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
508 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
511 /* ELF relocs are against symbols. If we are producing relocateable
512 output, and the reloc is against an external symbol, and nothing
513 has given us any additional addend, the resulting reloc will also
514 be against the same symbol. In such a case, we don't want to
515 change anything about the way the reloc is handled, since it will
516 all be done at final link time. Rather than put special case code
517 into bfd_perform_relocation, all the reloc types use this howto
518 function. It just short circuits the reloc if producing
519 relocateable output against an external symbol. */
521 bfd_reloc_status_type
522 bfd_elf_generic_reloc (abfd
,
529 bfd
*abfd ATTRIBUTE_UNUSED
;
530 arelent
*reloc_entry
;
532 PTR data ATTRIBUTE_UNUSED
;
533 asection
*input_section
;
535 char **error_message ATTRIBUTE_UNUSED
;
537 if (output_bfd
!= (bfd
*) NULL
538 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
539 && (! reloc_entry
->howto
->partial_inplace
540 || reloc_entry
->addend
== 0))
542 reloc_entry
->address
+= input_section
->output_offset
;
546 return bfd_reloc_continue
;
549 /* Print out the program headers. */
552 _bfd_elf_print_private_bfd_data (abfd
, farg
)
556 FILE *f
= (FILE *) farg
;
557 Elf_Internal_Phdr
*p
;
559 bfd_byte
*dynbuf
= NULL
;
561 p
= elf_tdata (abfd
)->phdr
;
566 fprintf (f
, _("\nProgram Header:\n"));
567 c
= elf_elfheader (abfd
)->e_phnum
;
568 for (i
= 0; i
< c
; i
++, p
++)
575 case PT_NULL
: s
= "NULL"; break;
576 case PT_LOAD
: s
= "LOAD"; break;
577 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
578 case PT_INTERP
: s
= "INTERP"; break;
579 case PT_NOTE
: s
= "NOTE"; break;
580 case PT_SHLIB
: s
= "SHLIB"; break;
581 case PT_PHDR
: s
= "PHDR"; break;
582 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
584 fprintf (f
, "%8s off 0x", s
);
585 fprintf_vma (f
, p
->p_offset
);
586 fprintf (f
, " vaddr 0x");
587 fprintf_vma (f
, p
->p_vaddr
);
588 fprintf (f
, " paddr 0x");
589 fprintf_vma (f
, p
->p_paddr
);
590 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
591 fprintf (f
, " filesz 0x");
592 fprintf_vma (f
, p
->p_filesz
);
593 fprintf (f
, " memsz 0x");
594 fprintf_vma (f
, p
->p_memsz
);
595 fprintf (f
, " flags %c%c%c",
596 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
597 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
598 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
599 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
600 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
605 s
= bfd_get_section_by_name (abfd
, ".dynamic");
610 bfd_byte
*extdyn
, *extdynend
;
612 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
614 fprintf (f
, _("\nDynamic Section:\n"));
616 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
619 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
623 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
626 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
628 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
629 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
632 extdynend
= extdyn
+ s
->_raw_size
;
633 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
635 Elf_Internal_Dyn dyn
;
640 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
642 if (dyn
.d_tag
== DT_NULL
)
649 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
653 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
654 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
655 case DT_PLTGOT
: name
= "PLTGOT"; break;
656 case DT_HASH
: name
= "HASH"; break;
657 case DT_STRTAB
: name
= "STRTAB"; break;
658 case DT_SYMTAB
: name
= "SYMTAB"; break;
659 case DT_RELA
: name
= "RELA"; break;
660 case DT_RELASZ
: name
= "RELASZ"; break;
661 case DT_RELAENT
: name
= "RELAENT"; break;
662 case DT_STRSZ
: name
= "STRSZ"; break;
663 case DT_SYMENT
: name
= "SYMENT"; break;
664 case DT_INIT
: name
= "INIT"; break;
665 case DT_FINI
: name
= "FINI"; break;
666 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
667 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
668 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
669 case DT_REL
: name
= "REL"; break;
670 case DT_RELSZ
: name
= "RELSZ"; break;
671 case DT_RELENT
: name
= "RELENT"; break;
672 case DT_PLTREL
: name
= "PLTREL"; break;
673 case DT_DEBUG
: name
= "DEBUG"; break;
674 case DT_TEXTREL
: name
= "TEXTREL"; break;
675 case DT_JMPREL
: name
= "JMPREL"; break;
676 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
677 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
678 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
679 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
680 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
681 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
682 case DT_FLAGS
: name
= "FLAGS"; break;
683 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
684 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
685 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
686 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
687 case DT_MOVEENT
: name
= "MOVEENT"; break;
688 case DT_MOVESZ
: name
= "MOVESZ"; break;
689 case DT_FEATURE
: name
= "FEATURE"; break;
690 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
691 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
692 case DT_SYMINENT
: name
= "SYMINENT"; break;
693 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
694 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
695 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
696 case DT_PLTPAD
: name
= "PLTPAD"; break;
697 case DT_MOVETAB
: name
= "MOVETAB"; break;
698 case DT_SYMINFO
: name
= "SYMINFO"; break;
699 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
700 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
701 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
702 case DT_VERSYM
: name
= "VERSYM"; break;
703 case DT_VERDEF
: name
= "VERDEF"; break;
704 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
705 case DT_VERNEED
: name
= "VERNEED"; break;
706 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
707 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
708 case DT_USED
: name
= "USED"; break;
709 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
712 fprintf (f
, " %-11s ", name
);
714 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
719 string
= bfd_elf_string_from_elf_section (abfd
, link
,
723 fprintf (f
, "%s", string
);
732 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
733 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
735 if (! _bfd_elf_slurp_version_tables (abfd
))
739 if (elf_dynverdef (abfd
) != 0)
741 Elf_Internal_Verdef
*t
;
743 fprintf (f
, _("\nVersion definitions:\n"));
744 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
746 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
747 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
748 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
750 Elf_Internal_Verdaux
*a
;
753 for (a
= t
->vd_auxptr
->vda_nextptr
;
756 fprintf (f
, "%s ", a
->vda_nodename
);
762 if (elf_dynverref (abfd
) != 0)
764 Elf_Internal_Verneed
*t
;
766 fprintf (f
, _("\nVersion References:\n"));
767 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
769 Elf_Internal_Vernaux
*a
;
771 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
772 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
773 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
774 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
786 /* Display ELF-specific fields of a symbol. */
789 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
793 bfd_print_symbol_type how
;
795 FILE *file
= (FILE *) filep
;
798 case bfd_print_symbol_name
:
799 fprintf (file
, "%s", symbol
->name
);
801 case bfd_print_symbol_more
:
802 fprintf (file
, "elf ");
803 fprintf_vma (file
, symbol
->value
);
804 fprintf (file
, " %lx", (long) symbol
->flags
);
806 case bfd_print_symbol_all
:
808 CONST
char *section_name
;
809 CONST
char *name
= NULL
;
810 struct elf_backend_data
*bed
;
811 unsigned char st_other
;
813 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
815 bed
= get_elf_backend_data (abfd
);
816 if (bed
->elf_backend_print_symbol_all
)
817 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
822 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
825 fprintf (file
, " %s\t", section_name
);
826 /* Print the "other" value for a symbol. For common symbols,
827 we've already printed the size; now print the alignment.
828 For other symbols, we have no specified alignment, and
829 we've printed the address; now print the size. */
831 (bfd_is_com_section (symbol
->section
)
832 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
833 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
835 /* If we have version information, print it. */
836 if (elf_tdata (abfd
)->dynversym_section
!= 0
837 && (elf_tdata (abfd
)->dynverdef_section
!= 0
838 || elf_tdata (abfd
)->dynverref_section
!= 0))
841 const char *version_string
;
843 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
847 else if (vernum
== 1)
848 version_string
= "Base";
849 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
851 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
854 Elf_Internal_Verneed
*t
;
857 for (t
= elf_tdata (abfd
)->verref
;
861 Elf_Internal_Vernaux
*a
;
863 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
865 if (a
->vna_other
== vernum
)
867 version_string
= a
->vna_nodename
;
874 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
875 fprintf (file
, " %-11s", version_string
);
880 fprintf (file
, " (%s)", version_string
);
881 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
886 /* If the st_other field is not zero, print it. */
887 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
892 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
893 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
894 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
896 /* Some other non-defined flags are also present, so print
898 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
901 fprintf (file
, " %s", name
);
907 /* Create an entry in an ELF linker hash table. */
909 struct bfd_hash_entry
*
910 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
911 struct bfd_hash_entry
*entry
;
912 struct bfd_hash_table
*table
;
915 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
917 /* Allocate the structure if it has not already been allocated by a
919 if (ret
== (struct elf_link_hash_entry
*) NULL
)
920 ret
= ((struct elf_link_hash_entry
*)
921 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
922 if (ret
== (struct elf_link_hash_entry
*) NULL
)
923 return (struct bfd_hash_entry
*) ret
;
925 /* Call the allocation method of the superclass. */
926 ret
= ((struct elf_link_hash_entry
*)
927 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
929 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
931 /* Set local fields. */
935 ret
->dynstr_index
= 0;
937 ret
->got
.offset
= (bfd_vma
) -1;
938 ret
->plt
.offset
= (bfd_vma
) -1;
939 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
940 ret
->verinfo
.verdef
= NULL
;
941 ret
->vtable_entries_used
= NULL
;
942 ret
->vtable_entries_size
= 0;
943 ret
->vtable_parent
= NULL
;
944 ret
->type
= STT_NOTYPE
;
946 /* Assume that we have been called by a non-ELF symbol reader.
947 This flag is then reset by the code which reads an ELF input
948 file. This ensures that a symbol created by a non-ELF symbol
949 reader will have the flag set correctly. */
950 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
953 return (struct bfd_hash_entry
*) ret
;
956 /* Copy data from an indirect symbol to its direct symbol, hiding the
957 old indirect symbol. */
960 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
961 struct elf_link_hash_entry
*dir
, *ind
;
963 /* Copy down any references that we may have already seen to the
964 symbol which just became indirect. */
966 dir
->elf_link_hash_flags
|=
967 (ind
->elf_link_hash_flags
968 & (ELF_LINK_HASH_REF_DYNAMIC
969 | ELF_LINK_HASH_REF_REGULAR
970 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
971 | ELF_LINK_NON_GOT_REF
));
973 /* Copy over the global and procedure linkage table offset entries.
974 These may have been already set up by a check_relocs routine. */
975 if (dir
->got
.offset
== (bfd_vma
) -1)
977 dir
->got
.offset
= ind
->got
.offset
;
978 ind
->got
.offset
= (bfd_vma
) -1;
980 BFD_ASSERT (ind
->got
.offset
== (bfd_vma
) -1);
982 if (dir
->plt
.offset
== (bfd_vma
) -1)
984 dir
->plt
.offset
= ind
->plt
.offset
;
985 ind
->plt
.offset
= (bfd_vma
) -1;
987 BFD_ASSERT (ind
->plt
.offset
== (bfd_vma
) -1);
989 if (dir
->dynindx
== -1)
991 dir
->dynindx
= ind
->dynindx
;
992 dir
->dynstr_index
= ind
->dynstr_index
;
994 ind
->dynstr_index
= 0;
996 BFD_ASSERT (ind
->dynindx
== -1);
1000 _bfd_elf_link_hash_hide_symbol (info
, h
)
1001 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1002 struct elf_link_hash_entry
*h
;
1004 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1006 h
->plt
.offset
= (bfd_vma
) -1;
1009 /* Initialize an ELF linker hash table. */
1012 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1013 struct elf_link_hash_table
*table
;
1015 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1016 struct bfd_hash_table
*,
1019 table
->dynamic_sections_created
= false;
1020 table
->dynobj
= NULL
;
1021 /* The first dynamic symbol is a dummy. */
1022 table
->dynsymcount
= 1;
1023 table
->dynstr
= NULL
;
1024 table
->bucketcount
= 0;
1025 table
->needed
= NULL
;
1026 table
->runpath
= NULL
;
1028 table
->stab_info
= NULL
;
1029 table
->dynlocal
= NULL
;
1030 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1033 /* Create an ELF linker hash table. */
1035 struct bfd_link_hash_table
*
1036 _bfd_elf_link_hash_table_create (abfd
)
1039 struct elf_link_hash_table
*ret
;
1041 ret
= ((struct elf_link_hash_table
*)
1042 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
1043 if (ret
== (struct elf_link_hash_table
*) NULL
)
1046 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1048 bfd_release (abfd
, ret
);
1055 /* This is a hook for the ELF emulation code in the generic linker to
1056 tell the backend linker what file name to use for the DT_NEEDED
1057 entry for a dynamic object. The generic linker passes name as an
1058 empty string to indicate that no DT_NEEDED entry should be made. */
1061 bfd_elf_set_dt_needed_name (abfd
, name
)
1065 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1066 && bfd_get_format (abfd
) == bfd_object
)
1067 elf_dt_name (abfd
) = name
;
1071 bfd_elf_set_dt_needed_soname (abfd
, name
)
1075 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1076 && bfd_get_format (abfd
) == bfd_object
)
1077 elf_dt_soname (abfd
) = name
;
1080 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1081 the linker ELF emulation code. */
1083 struct bfd_link_needed_list
*
1084 bfd_elf_get_needed_list (abfd
, info
)
1085 bfd
*abfd ATTRIBUTE_UNUSED
;
1086 struct bfd_link_info
*info
;
1088 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1090 return elf_hash_table (info
)->needed
;
1093 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1094 hook for the linker ELF emulation code. */
1096 struct bfd_link_needed_list
*
1097 bfd_elf_get_runpath_list (abfd
, info
)
1098 bfd
*abfd ATTRIBUTE_UNUSED
;
1099 struct bfd_link_info
*info
;
1101 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1103 return elf_hash_table (info
)->runpath
;
1106 /* Get the name actually used for a dynamic object for a link. This
1107 is the SONAME entry if there is one. Otherwise, it is the string
1108 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1111 bfd_elf_get_dt_soname (abfd
)
1114 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1115 && bfd_get_format (abfd
) == bfd_object
)
1116 return elf_dt_name (abfd
);
1120 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1121 the ELF linker emulation code. */
1124 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1126 struct bfd_link_needed_list
**pneeded
;
1129 bfd_byte
*dynbuf
= NULL
;
1132 bfd_byte
*extdyn
, *extdynend
;
1134 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1138 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1139 || bfd_get_format (abfd
) != bfd_object
)
1142 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1143 if (s
== NULL
|| s
->_raw_size
== 0)
1146 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1150 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1154 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1158 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1160 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1161 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1164 extdynend
= extdyn
+ s
->_raw_size
;
1165 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1167 Elf_Internal_Dyn dyn
;
1169 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1171 if (dyn
.d_tag
== DT_NULL
)
1174 if (dyn
.d_tag
== DT_NEEDED
)
1177 struct bfd_link_needed_list
*l
;
1179 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1184 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1205 /* Allocate an ELF string table--force the first byte to be zero. */
1207 struct bfd_strtab_hash
*
1208 _bfd_elf_stringtab_init ()
1210 struct bfd_strtab_hash
*ret
;
1212 ret
= _bfd_stringtab_init ();
1217 loc
= _bfd_stringtab_add (ret
, "", true, false);
1218 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1219 if (loc
== (bfd_size_type
) -1)
1221 _bfd_stringtab_free (ret
);
1228 /* ELF .o/exec file reading */
1230 /* Create a new bfd section from an ELF section header. */
1233 bfd_section_from_shdr (abfd
, shindex
)
1235 unsigned int shindex
;
1237 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1238 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1239 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1242 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1244 switch (hdr
->sh_type
)
1247 /* Inactive section. Throw it away. */
1250 case SHT_PROGBITS
: /* Normal section with contents. */
1251 case SHT_DYNAMIC
: /* Dynamic linking information. */
1252 case SHT_NOBITS
: /* .bss section. */
1253 case SHT_HASH
: /* .hash section. */
1254 case SHT_NOTE
: /* .note section. */
1255 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1257 case SHT_SYMTAB
: /* A symbol table */
1258 if (elf_onesymtab (abfd
) == shindex
)
1261 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1262 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1263 elf_onesymtab (abfd
) = shindex
;
1264 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1265 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1266 abfd
->flags
|= HAS_SYMS
;
1268 /* Sometimes a shared object will map in the symbol table. If
1269 SHF_ALLOC is set, and this is a shared object, then we also
1270 treat this section as a BFD section. We can not base the
1271 decision purely on SHF_ALLOC, because that flag is sometimes
1272 set in a relocateable object file, which would confuse the
1274 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1275 && (abfd
->flags
& DYNAMIC
) != 0
1276 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1281 case SHT_DYNSYM
: /* A dynamic symbol table */
1282 if (elf_dynsymtab (abfd
) == shindex
)
1285 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1286 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1287 elf_dynsymtab (abfd
) = shindex
;
1288 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1289 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1290 abfd
->flags
|= HAS_SYMS
;
1292 /* Besides being a symbol table, we also treat this as a regular
1293 section, so that objcopy can handle it. */
1294 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1296 case SHT_STRTAB
: /* A string table */
1297 if (hdr
->bfd_section
!= NULL
)
1299 if (ehdr
->e_shstrndx
== shindex
)
1301 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1302 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1308 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1310 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1311 if (hdr2
->sh_link
== shindex
)
1313 if (! bfd_section_from_shdr (abfd
, i
))
1315 if (elf_onesymtab (abfd
) == i
)
1317 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1318 elf_elfsections (abfd
)[shindex
] =
1319 &elf_tdata (abfd
)->strtab_hdr
;
1322 if (elf_dynsymtab (abfd
) == i
)
1324 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1325 elf_elfsections (abfd
)[shindex
] = hdr
=
1326 &elf_tdata (abfd
)->dynstrtab_hdr
;
1327 /* We also treat this as a regular section, so
1328 that objcopy can handle it. */
1331 #if 0 /* Not handling other string tables specially right now. */
1332 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1333 /* We have a strtab for some random other section. */
1334 newsect
= (asection
*) hdr2
->bfd_section
;
1337 hdr
->bfd_section
= newsect
;
1338 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1340 elf_elfsections (abfd
)[shindex
] = hdr2
;
1346 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1350 /* *These* do a lot of work -- but build no sections! */
1352 asection
*target_sect
;
1353 Elf_Internal_Shdr
*hdr2
;
1355 /* Check for a bogus link to avoid crashing. */
1356 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1358 ((*_bfd_error_handler
)
1359 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1360 bfd_get_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1361 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1364 /* For some incomprehensible reason Oracle distributes
1365 libraries for Solaris in which some of the objects have
1366 bogus sh_link fields. It would be nice if we could just
1367 reject them, but, unfortunately, some people need to use
1368 them. We scan through the section headers; if we find only
1369 one suitable symbol table, we clobber the sh_link to point
1370 to it. I hope this doesn't break anything. */
1371 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1372 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1378 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1380 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1381 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1392 hdr
->sh_link
= found
;
1395 /* Get the symbol table. */
1396 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1397 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1400 /* If this reloc section does not use the main symbol table we
1401 don't treat it as a reloc section. BFD can't adequately
1402 represent such a section, so at least for now, we don't
1403 try. We just present it as a normal section. We also
1404 can't use it as a reloc section if it points to the null
1406 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1407 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1409 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1411 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1412 if (target_sect
== NULL
)
1415 if ((target_sect
->flags
& SEC_RELOC
) == 0
1416 || target_sect
->reloc_count
== 0)
1417 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1420 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1421 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1422 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1425 elf_elfsections (abfd
)[shindex
] = hdr2
;
1426 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
1427 target_sect
->flags
|= SEC_RELOC
;
1428 target_sect
->relocation
= NULL
;
1429 target_sect
->rel_filepos
= hdr
->sh_offset
;
1430 /* In the section to which the relocations apply, mark whether
1431 its relocations are of the REL or RELA variety. */
1432 if (hdr
->sh_size
!= 0)
1433 elf_section_data (target_sect
)->use_rela_p
1434 = (hdr
->sh_type
== SHT_RELA
);
1435 abfd
->flags
|= HAS_RELOC
;
1440 case SHT_GNU_verdef
:
1441 elf_dynverdef (abfd
) = shindex
;
1442 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1443 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1446 case SHT_GNU_versym
:
1447 elf_dynversym (abfd
) = shindex
;
1448 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1449 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1452 case SHT_GNU_verneed
:
1453 elf_dynverref (abfd
) = shindex
;
1454 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1455 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1462 /* Check for any processor-specific section types. */
1464 if (bed
->elf_backend_section_from_shdr
)
1465 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1473 /* Given an ELF section number, retrieve the corresponding BFD
1477 bfd_section_from_elf_index (abfd
, index
)
1481 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1482 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1484 return elf_elfsections (abfd
)[index
]->bfd_section
;
1488 _bfd_elf_new_section_hook (abfd
, sec
)
1492 struct bfd_elf_section_data
*sdata
;
1494 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, sizeof (*sdata
));
1497 sec
->used_by_bfd
= (PTR
) sdata
;
1499 /* Indicate whether or not this section should use RELA relocations. */
1501 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1506 /* Create a new bfd section from an ELF program header.
1508 Since program segments have no names, we generate a synthetic name
1509 of the form segment<NUM>, where NUM is generally the index in the
1510 program header table. For segments that are split (see below) we
1511 generate the names segment<NUM>a and segment<NUM>b.
1513 Note that some program segments may have a file size that is different than
1514 (less than) the memory size. All this means is that at execution the
1515 system must allocate the amount of memory specified by the memory size,
1516 but only initialize it with the first "file size" bytes read from the
1517 file. This would occur for example, with program segments consisting
1518 of combined data+bss.
1520 To handle the above situation, this routine generates TWO bfd sections
1521 for the single program segment. The first has the length specified by
1522 the file size of the segment, and the second has the length specified
1523 by the difference between the two sizes. In effect, the segment is split
1524 into it's initialized and uninitialized parts.
1529 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1531 Elf_Internal_Phdr
*hdr
;
1533 const char *typename
;
1540 split
= ((hdr
->p_memsz
> 0)
1541 && (hdr
->p_filesz
> 0)
1542 && (hdr
->p_memsz
> hdr
->p_filesz
));
1543 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1544 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1547 strcpy (name
, namebuf
);
1548 newsect
= bfd_make_section (abfd
, name
);
1549 if (newsect
== NULL
)
1551 newsect
->vma
= hdr
->p_vaddr
;
1552 newsect
->lma
= hdr
->p_paddr
;
1553 newsect
->_raw_size
= hdr
->p_filesz
;
1554 newsect
->filepos
= hdr
->p_offset
;
1555 newsect
->flags
|= SEC_HAS_CONTENTS
;
1556 if (hdr
->p_type
== PT_LOAD
)
1558 newsect
->flags
|= SEC_ALLOC
;
1559 newsect
->flags
|= SEC_LOAD
;
1560 if (hdr
->p_flags
& PF_X
)
1562 /* FIXME: all we known is that it has execute PERMISSION,
1564 newsect
->flags
|= SEC_CODE
;
1567 if (!(hdr
->p_flags
& PF_W
))
1569 newsect
->flags
|= SEC_READONLY
;
1574 sprintf (namebuf
, "%s%db", typename
, index
);
1575 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1578 strcpy (name
, namebuf
);
1579 newsect
= bfd_make_section (abfd
, name
);
1580 if (newsect
== NULL
)
1582 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1583 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1584 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1585 if (hdr
->p_type
== PT_LOAD
)
1587 newsect
->flags
|= SEC_ALLOC
;
1588 if (hdr
->p_flags
& PF_X
)
1589 newsect
->flags
|= SEC_CODE
;
1591 if (!(hdr
->p_flags
& PF_W
))
1592 newsect
->flags
|= SEC_READONLY
;
1599 bfd_section_from_phdr (abfd
, hdr
, index
)
1601 Elf_Internal_Phdr
*hdr
;
1604 struct elf_backend_data
*bed
;
1606 switch (hdr
->p_type
)
1609 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1612 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1615 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1618 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1621 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1623 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
1628 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1631 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1634 /* Check for any processor-specific program segment types.
1635 If no handler for them, default to making "segment" sections. */
1636 bed
= get_elf_backend_data (abfd
);
1637 if (bed
->elf_backend_section_from_phdr
)
1638 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1640 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1644 /* Initialize REL_HDR, the section-header for new section, containing
1645 relocations against ASECT. If USE_RELA_P is true, we use RELA
1646 relocations; otherwise, we use REL relocations. */
1649 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1651 Elf_Internal_Shdr
*rel_hdr
;
1656 struct elf_backend_data
*bed
;
1658 bed
= get_elf_backend_data (abfd
);
1659 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1662 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1664 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1666 if (rel_hdr
->sh_name
== (unsigned int) -1)
1668 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1669 rel_hdr
->sh_entsize
= (use_rela_p
1670 ? bed
->s
->sizeof_rela
1671 : bed
->s
->sizeof_rel
);
1672 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1673 rel_hdr
->sh_flags
= 0;
1674 rel_hdr
->sh_addr
= 0;
1675 rel_hdr
->sh_size
= 0;
1676 rel_hdr
->sh_offset
= 0;
1681 /* Set up an ELF internal section header for a section. */
1684 elf_fake_sections (abfd
, asect
, failedptrarg
)
1689 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1690 boolean
*failedptr
= (boolean
*) failedptrarg
;
1691 Elf_Internal_Shdr
*this_hdr
;
1695 /* We already failed; just get out of the bfd_map_over_sections
1700 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1702 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1705 if (this_hdr
->sh_name
== (unsigned long) -1)
1711 this_hdr
->sh_flags
= 0;
1713 if ((asect
->flags
& SEC_ALLOC
) != 0
1714 || asect
->user_set_vma
)
1715 this_hdr
->sh_addr
= asect
->vma
;
1717 this_hdr
->sh_addr
= 0;
1719 this_hdr
->sh_offset
= 0;
1720 this_hdr
->sh_size
= asect
->_raw_size
;
1721 this_hdr
->sh_link
= 0;
1722 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1723 /* The sh_entsize and sh_info fields may have been set already by
1724 copy_private_section_data. */
1726 this_hdr
->bfd_section
= asect
;
1727 this_hdr
->contents
= NULL
;
1729 /* FIXME: This should not be based on section names. */
1730 if (strcmp (asect
->name
, ".dynstr") == 0)
1731 this_hdr
->sh_type
= SHT_STRTAB
;
1732 else if (strcmp (asect
->name
, ".hash") == 0)
1734 this_hdr
->sh_type
= SHT_HASH
;
1735 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1737 else if (strcmp (asect
->name
, ".dynsym") == 0)
1739 this_hdr
->sh_type
= SHT_DYNSYM
;
1740 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1742 else if (strcmp (asect
->name
, ".dynamic") == 0)
1744 this_hdr
->sh_type
= SHT_DYNAMIC
;
1745 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1747 else if (strncmp (asect
->name
, ".rela", 5) == 0
1748 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1750 this_hdr
->sh_type
= SHT_RELA
;
1751 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1753 else if (strncmp (asect
->name
, ".rel", 4) == 0
1754 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1756 this_hdr
->sh_type
= SHT_REL
;
1757 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1759 else if (strncmp (asect
->name
, ".note", 5) == 0)
1760 this_hdr
->sh_type
= SHT_NOTE
;
1761 else if (strncmp (asect
->name
, ".stab", 5) == 0
1762 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1763 this_hdr
->sh_type
= SHT_STRTAB
;
1764 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1766 this_hdr
->sh_type
= SHT_GNU_versym
;
1767 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1769 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1771 this_hdr
->sh_type
= SHT_GNU_verdef
;
1772 this_hdr
->sh_entsize
= 0;
1773 /* objcopy or strip will copy over sh_info, but may not set
1774 cverdefs. The linker will set cverdefs, but sh_info will be
1776 if (this_hdr
->sh_info
== 0)
1777 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1779 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1780 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1782 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1784 this_hdr
->sh_type
= SHT_GNU_verneed
;
1785 this_hdr
->sh_entsize
= 0;
1786 /* objcopy or strip will copy over sh_info, but may not set
1787 cverrefs. The linker will set cverrefs, but sh_info will be
1789 if (this_hdr
->sh_info
== 0)
1790 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1792 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1793 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1795 else if ((asect
->flags
& SEC_ALLOC
) != 0
1796 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
1797 this_hdr
->sh_type
= SHT_NOBITS
;
1799 this_hdr
->sh_type
= SHT_PROGBITS
;
1801 if ((asect
->flags
& SEC_ALLOC
) != 0)
1802 this_hdr
->sh_flags
|= SHF_ALLOC
;
1803 if ((asect
->flags
& SEC_READONLY
) == 0)
1804 this_hdr
->sh_flags
|= SHF_WRITE
;
1805 if ((asect
->flags
& SEC_CODE
) != 0)
1806 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1808 /* Check for processor-specific section types. */
1809 if (bed
->elf_backend_fake_sections
)
1810 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1812 /* If the section has relocs, set up a section header for the
1813 SHT_REL[A] section. If two relocation sections are required for
1814 this section, it is up to the processor-specific back-end to
1815 create the other. */
1816 if ((asect
->flags
& SEC_RELOC
) != 0
1817 && !_bfd_elf_init_reloc_shdr (abfd
,
1818 &elf_section_data (asect
)->rel_hdr
,
1820 elf_section_data (asect
)->use_rela_p
))
1824 /* Assign all ELF section numbers. The dummy first section is handled here
1825 too. The link/info pointers for the standard section types are filled
1826 in here too, while we're at it. */
1829 assign_section_numbers (abfd
)
1832 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1834 unsigned int section_number
;
1835 Elf_Internal_Shdr
**i_shdrp
;
1839 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1841 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1843 d
->this_idx
= section_number
++;
1844 if ((sec
->flags
& SEC_RELOC
) == 0)
1847 d
->rel_idx
= section_number
++;
1850 d
->rel_idx2
= section_number
++;
1855 t
->shstrtab_section
= section_number
++;
1856 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1857 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1859 if (bfd_get_symcount (abfd
) > 0)
1861 t
->symtab_section
= section_number
++;
1862 t
->strtab_section
= section_number
++;
1865 elf_elfheader (abfd
)->e_shnum
= section_number
;
1867 /* Set up the list of section header pointers, in agreement with the
1869 i_shdrp
= ((Elf_Internal_Shdr
**)
1870 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1871 if (i_shdrp
== NULL
)
1874 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1875 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1876 if (i_shdrp
[0] == NULL
)
1878 bfd_release (abfd
, i_shdrp
);
1881 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1883 elf_elfsections (abfd
) = i_shdrp
;
1885 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1886 if (bfd_get_symcount (abfd
) > 0)
1888 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1889 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1890 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1892 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1894 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1898 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1899 if (d
->rel_idx
!= 0)
1900 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1901 if (d
->rel_idx2
!= 0)
1902 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1904 /* Fill in the sh_link and sh_info fields while we're at it. */
1906 /* sh_link of a reloc section is the section index of the symbol
1907 table. sh_info is the section index of the section to which
1908 the relocation entries apply. */
1909 if (d
->rel_idx
!= 0)
1911 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1912 d
->rel_hdr
.sh_info
= d
->this_idx
;
1914 if (d
->rel_idx2
!= 0)
1916 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1917 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1920 switch (d
->this_hdr
.sh_type
)
1924 /* A reloc section which we are treating as a normal BFD
1925 section. sh_link is the section index of the symbol
1926 table. sh_info is the section index of the section to
1927 which the relocation entries apply. We assume that an
1928 allocated reloc section uses the dynamic symbol table.
1929 FIXME: How can we be sure? */
1930 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1932 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1934 /* We look up the section the relocs apply to by name. */
1936 if (d
->this_hdr
.sh_type
== SHT_REL
)
1940 s
= bfd_get_section_by_name (abfd
, name
);
1942 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1946 /* We assume that a section named .stab*str is a stabs
1947 string section. We look for a section with the same name
1948 but without the trailing ``str'', and set its sh_link
1949 field to point to this section. */
1950 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1951 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1956 len
= strlen (sec
->name
);
1957 alc
= (char *) bfd_malloc (len
- 2);
1960 strncpy (alc
, sec
->name
, len
- 3);
1961 alc
[len
- 3] = '\0';
1962 s
= bfd_get_section_by_name (abfd
, alc
);
1966 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1968 /* This is a .stab section. */
1969 elf_section_data (s
)->this_hdr
.sh_entsize
=
1970 4 + 2 * bfd_get_arch_size (abfd
) / 8;
1977 case SHT_GNU_verneed
:
1978 case SHT_GNU_verdef
:
1979 /* sh_link is the section header index of the string table
1980 used for the dynamic entries, or the symbol table, or the
1982 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1984 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1988 case SHT_GNU_versym
:
1989 /* sh_link is the section header index of the symbol table
1990 this hash table or version table is for. */
1991 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1993 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2001 /* Map symbol from it's internal number to the external number, moving
2002 all local symbols to be at the head of the list. */
2005 sym_is_global (abfd
, sym
)
2009 /* If the backend has a special mapping, use it. */
2010 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2011 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2014 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2015 || bfd_is_und_section (bfd_get_section (sym
))
2016 || bfd_is_com_section (bfd_get_section (sym
)));
2020 elf_map_symbols (abfd
)
2023 int symcount
= bfd_get_symcount (abfd
);
2024 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2025 asymbol
**sect_syms
;
2027 int num_globals
= 0;
2028 int num_locals2
= 0;
2029 int num_globals2
= 0;
2031 int num_sections
= 0;
2038 fprintf (stderr
, "elf_map_symbols\n");
2042 /* Add a section symbol for each BFD section. FIXME: Is this really
2044 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2046 if (max_index
< asect
->index
)
2047 max_index
= asect
->index
;
2051 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
2052 if (sect_syms
== NULL
)
2054 elf_section_syms (abfd
) = sect_syms
;
2056 for (idx
= 0; idx
< symcount
; idx
++)
2060 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2067 if (sec
->owner
!= NULL
)
2069 if (sec
->owner
!= abfd
)
2071 if (sec
->output_offset
!= 0)
2074 sec
= sec
->output_section
;
2076 /* Empty sections in the input files may have had a section
2077 symbol created for them. (See the comment near the end of
2078 _bfd_generic_link_output_symbols in linker.c). If the linker
2079 script discards such sections then we will reach this point.
2080 Since we know that we cannot avoid this case, we detect it
2081 and skip the abort and the assignment to the sect_syms array.
2082 To reproduce this particular case try running the linker
2083 testsuite test ld-scripts/weak.exp for an ELF port that uses
2084 the generic linker. */
2085 if (sec
->owner
== NULL
)
2088 BFD_ASSERT (sec
->owner
== abfd
);
2090 sect_syms
[sec
->index
] = syms
[idx
];
2095 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2097 if (sect_syms
[asect
->index
] != NULL
)
2100 sym
= bfd_make_empty_symbol (abfd
);
2103 sym
->the_bfd
= abfd
;
2104 sym
->name
= asect
->name
;
2106 /* Set the flags to 0 to indicate that this one was newly added. */
2108 sym
->section
= asect
;
2109 sect_syms
[asect
->index
] = sym
;
2113 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2114 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2118 /* Classify all of the symbols. */
2119 for (idx
= 0; idx
< symcount
; idx
++)
2121 if (!sym_is_global (abfd
, syms
[idx
]))
2126 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2128 if (sect_syms
[asect
->index
] != NULL
2129 && sect_syms
[asect
->index
]->flags
== 0)
2131 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2132 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2136 sect_syms
[asect
->index
]->flags
= 0;
2140 /* Now sort the symbols so the local symbols are first. */
2141 new_syms
= ((asymbol
**)
2143 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
2144 if (new_syms
== NULL
)
2147 for (idx
= 0; idx
< symcount
; idx
++)
2149 asymbol
*sym
= syms
[idx
];
2152 if (!sym_is_global (abfd
, sym
))
2155 i
= num_locals
+ num_globals2
++;
2157 sym
->udata
.i
= i
+ 1;
2159 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2161 if (sect_syms
[asect
->index
] != NULL
2162 && sect_syms
[asect
->index
]->flags
== 0)
2164 asymbol
*sym
= sect_syms
[asect
->index
];
2167 sym
->flags
= BSF_SECTION_SYM
;
2168 if (!sym_is_global (abfd
, sym
))
2171 i
= num_locals
+ num_globals2
++;
2173 sym
->udata
.i
= i
+ 1;
2177 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2179 elf_num_locals (abfd
) = num_locals
;
2180 elf_num_globals (abfd
) = num_globals
;
2184 /* Align to the maximum file alignment that could be required for any
2185 ELF data structure. */
2187 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2188 static INLINE file_ptr
2189 align_file_position (off
, align
)
2193 return (off
+ align
- 1) & ~(align
- 1);
2196 /* Assign a file position to a section, optionally aligning to the
2197 required section alignment. */
2200 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2201 Elf_Internal_Shdr
*i_shdrp
;
2209 al
= i_shdrp
->sh_addralign
;
2211 offset
= BFD_ALIGN (offset
, al
);
2213 i_shdrp
->sh_offset
= offset
;
2214 if (i_shdrp
->bfd_section
!= NULL
)
2215 i_shdrp
->bfd_section
->filepos
= offset
;
2216 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2217 offset
+= i_shdrp
->sh_size
;
2221 /* Compute the file positions we are going to put the sections at, and
2222 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2223 is not NULL, this is being called by the ELF backend linker. */
2226 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2228 struct bfd_link_info
*link_info
;
2230 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2232 struct bfd_strtab_hash
*strtab
;
2233 Elf_Internal_Shdr
*shstrtab_hdr
;
2235 if (abfd
->output_has_begun
)
2238 /* Do any elf backend specific processing first. */
2239 if (bed
->elf_backend_begin_write_processing
)
2240 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2242 if (! prep_headers (abfd
))
2245 /* Post process the headers if necessary. */
2246 if (bed
->elf_backend_post_process_headers
)
2247 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2250 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2254 if (!assign_section_numbers (abfd
))
2257 /* The backend linker builds symbol table information itself. */
2258 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2260 /* Non-zero if doing a relocatable link. */
2261 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2263 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2267 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2268 /* sh_name was set in prep_headers. */
2269 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2270 shstrtab_hdr
->sh_flags
= 0;
2271 shstrtab_hdr
->sh_addr
= 0;
2272 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2273 shstrtab_hdr
->sh_entsize
= 0;
2274 shstrtab_hdr
->sh_link
= 0;
2275 shstrtab_hdr
->sh_info
= 0;
2276 /* sh_offset is set in assign_file_positions_except_relocs. */
2277 shstrtab_hdr
->sh_addralign
= 1;
2279 if (!assign_file_positions_except_relocs (abfd
))
2282 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2285 Elf_Internal_Shdr
*hdr
;
2287 off
= elf_tdata (abfd
)->next_file_pos
;
2289 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2290 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2292 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2293 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2295 elf_tdata (abfd
)->next_file_pos
= off
;
2297 /* Now that we know where the .strtab section goes, write it
2299 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2300 || ! _bfd_stringtab_emit (abfd
, strtab
))
2302 _bfd_stringtab_free (strtab
);
2305 abfd
->output_has_begun
= true;
2310 /* Create a mapping from a set of sections to a program segment. */
2312 static INLINE
struct elf_segment_map
*
2313 make_mapping (abfd
, sections
, from
, to
, phdr
)
2315 asection
**sections
;
2320 struct elf_segment_map
*m
;
2324 m
= ((struct elf_segment_map
*)
2326 (sizeof (struct elf_segment_map
)
2327 + (to
- from
- 1) * sizeof (asection
*))));
2331 m
->p_type
= PT_LOAD
;
2332 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2333 m
->sections
[i
- from
] = *hdrpp
;
2334 m
->count
= to
- from
;
2336 if (from
== 0 && phdr
)
2338 /* Include the headers in the first PT_LOAD segment. */
2339 m
->includes_filehdr
= 1;
2340 m
->includes_phdrs
= 1;
2346 /* Set up a mapping from BFD sections to program segments. */
2349 map_sections_to_segments (abfd
)
2352 asection
**sections
= NULL
;
2356 struct elf_segment_map
*mfirst
;
2357 struct elf_segment_map
**pm
;
2358 struct elf_segment_map
*m
;
2360 unsigned int phdr_index
;
2361 bfd_vma maxpagesize
;
2363 boolean phdr_in_segment
= true;
2367 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2370 if (bfd_count_sections (abfd
) == 0)
2373 /* Select the allocated sections, and sort them. */
2375 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2376 * sizeof (asection
*));
2377 if (sections
== NULL
)
2381 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2383 if ((s
->flags
& SEC_ALLOC
) != 0)
2389 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2392 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2394 /* Build the mapping. */
2399 /* If we have a .interp section, then create a PT_PHDR segment for
2400 the program headers and a PT_INTERP segment for the .interp
2402 s
= bfd_get_section_by_name (abfd
, ".interp");
2403 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2405 m
= ((struct elf_segment_map
*)
2406 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2410 m
->p_type
= PT_PHDR
;
2411 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2412 m
->p_flags
= PF_R
| PF_X
;
2413 m
->p_flags_valid
= 1;
2414 m
->includes_phdrs
= 1;
2419 m
= ((struct elf_segment_map
*)
2420 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2424 m
->p_type
= PT_INTERP
;
2432 /* Look through the sections. We put sections in the same program
2433 segment when the start of the second section can be placed within
2434 a few bytes of the end of the first section. */
2437 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2439 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2441 && (dynsec
->flags
& SEC_LOAD
) == 0)
2444 /* Deal with -Ttext or something similar such that the first section
2445 is not adjacent to the program headers. This is an
2446 approximation, since at this point we don't know exactly how many
2447 program headers we will need. */
2450 bfd_size_type phdr_size
;
2452 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2454 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2455 if ((abfd
->flags
& D_PAGED
) == 0
2456 || sections
[0]->lma
< phdr_size
2457 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2458 phdr_in_segment
= false;
2461 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2464 boolean new_segment
;
2468 /* See if this section and the last one will fit in the same
2471 if (last_hdr
== NULL
)
2473 /* If we don't have a segment yet, then we don't need a new
2474 one (we build the last one after this loop). */
2475 new_segment
= false;
2477 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2479 /* If this section has a different relation between the
2480 virtual address and the load address, then we need a new
2484 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2485 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2487 /* If putting this section in this segment would force us to
2488 skip a page in the segment, then we need a new segment. */
2491 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2492 && (hdr
->flags
& SEC_LOAD
) != 0)
2494 /* We don't want to put a loadable section after a
2495 nonloadable section in the same segment. */
2498 else if ((abfd
->flags
& D_PAGED
) == 0)
2500 /* If the file is not demand paged, which means that we
2501 don't require the sections to be correctly aligned in the
2502 file, then there is no other reason for a new segment. */
2503 new_segment
= false;
2506 && (hdr
->flags
& SEC_READONLY
) == 0
2507 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2510 /* We don't want to put a writable section in a read only
2511 segment, unless they are on the same page in memory
2512 anyhow. We already know that the last section does not
2513 bring us past the current section on the page, so the
2514 only case in which the new section is not on the same
2515 page as the previous section is when the previous section
2516 ends precisely on a page boundary. */
2521 /* Otherwise, we can use the same segment. */
2522 new_segment
= false;
2527 if ((hdr
->flags
& SEC_READONLY
) == 0)
2533 /* We need a new program segment. We must create a new program
2534 header holding all the sections from phdr_index until hdr. */
2536 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2543 if ((hdr
->flags
& SEC_READONLY
) == 0)
2550 phdr_in_segment
= false;
2553 /* Create a final PT_LOAD program segment. */
2554 if (last_hdr
!= NULL
)
2556 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2564 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2567 m
= ((struct elf_segment_map
*)
2568 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2572 m
->p_type
= PT_DYNAMIC
;
2574 m
->sections
[0] = dynsec
;
2580 /* For each loadable .note section, add a PT_NOTE segment. We don't
2581 use bfd_get_section_by_name, because if we link together
2582 nonloadable .note sections and loadable .note sections, we will
2583 generate two .note sections in the output file. FIXME: Using
2584 names for section types is bogus anyhow. */
2585 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2587 if ((s
->flags
& SEC_LOAD
) != 0
2588 && strncmp (s
->name
, ".note", 5) == 0)
2590 m
= ((struct elf_segment_map
*)
2591 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2595 m
->p_type
= PT_NOTE
;
2607 elf_tdata (abfd
)->segment_map
= mfirst
;
2611 if (sections
!= NULL
)
2616 /* Sort sections by address. */
2619 elf_sort_sections (arg1
, arg2
)
2623 const asection
*sec1
= *(const asection
**) arg1
;
2624 const asection
*sec2
= *(const asection
**) arg2
;
2626 /* Sort by LMA first, since this is the address used to
2627 place the section into a segment. */
2628 if (sec1
->lma
< sec2
->lma
)
2630 else if (sec1
->lma
> sec2
->lma
)
2633 /* Then sort by VMA. Normally the LMA and the VMA will be
2634 the same, and this will do nothing. */
2635 if (sec1
->vma
< sec2
->vma
)
2637 else if (sec1
->vma
> sec2
->vma
)
2640 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2642 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2647 return sec1
->target_index
- sec2
->target_index
;
2657 /* Sort by size, to put zero sized sections before others at the
2660 if (sec1
->_raw_size
< sec2
->_raw_size
)
2662 if (sec1
->_raw_size
> sec2
->_raw_size
)
2665 return sec1
->target_index
- sec2
->target_index
;
2668 /* Assign file positions to the sections based on the mapping from
2669 sections to segments. This function also sets up some fields in
2670 the file header, and writes out the program headers. */
2673 assign_file_positions_for_segments (abfd
)
2676 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2678 struct elf_segment_map
*m
;
2680 Elf_Internal_Phdr
*phdrs
;
2682 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2683 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2684 Elf_Internal_Phdr
*p
;
2686 if (elf_tdata (abfd
)->segment_map
== NULL
)
2688 if (! map_sections_to_segments (abfd
))
2692 if (bed
->elf_backend_modify_segment_map
)
2694 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2699 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2702 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2703 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2704 elf_elfheader (abfd
)->e_phnum
= count
;
2709 /* If we already counted the number of program segments, make sure
2710 that we allocated enough space. This happens when SIZEOF_HEADERS
2711 is used in a linker script. */
2712 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2713 if (alloc
!= 0 && count
> alloc
)
2715 ((*_bfd_error_handler
)
2716 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2717 bfd_get_filename (abfd
), alloc
, count
));
2718 bfd_set_error (bfd_error_bad_value
);
2725 phdrs
= ((Elf_Internal_Phdr
*)
2726 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2730 off
= bed
->s
->sizeof_ehdr
;
2731 off
+= alloc
* bed
->s
->sizeof_phdr
;
2738 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2745 /* If elf_segment_map is not from map_sections_to_segments, the
2746 sections may not be correctly ordered. */
2748 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2751 p
->p_type
= m
->p_type
;
2752 p
->p_flags
= m
->p_flags
;
2754 if (p
->p_type
== PT_LOAD
2756 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2758 if ((abfd
->flags
& D_PAGED
) != 0)
2759 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2762 bfd_size_type align
;
2765 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2767 bfd_size_type secalign
;
2769 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2770 if (secalign
> align
)
2774 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2781 p
->p_vaddr
= m
->sections
[0]->vma
;
2783 if (m
->p_paddr_valid
)
2784 p
->p_paddr
= m
->p_paddr
;
2785 else if (m
->count
== 0)
2788 p
->p_paddr
= m
->sections
[0]->lma
;
2790 if (p
->p_type
== PT_LOAD
2791 && (abfd
->flags
& D_PAGED
) != 0)
2792 p
->p_align
= bed
->maxpagesize
;
2793 else if (m
->count
== 0)
2794 p
->p_align
= bed
->s
->file_align
;
2802 if (m
->includes_filehdr
)
2804 if (! m
->p_flags_valid
)
2807 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2808 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2811 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2813 if (p
->p_vaddr
< (bfd_vma
) off
)
2815 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2816 bfd_get_filename (abfd
));
2817 bfd_set_error (bfd_error_bad_value
);
2822 if (! m
->p_paddr_valid
)
2825 if (p
->p_type
== PT_LOAD
)
2827 filehdr_vaddr
= p
->p_vaddr
;
2828 filehdr_paddr
= p
->p_paddr
;
2832 if (m
->includes_phdrs
)
2834 if (! m
->p_flags_valid
)
2837 if (m
->includes_filehdr
)
2839 if (p
->p_type
== PT_LOAD
)
2841 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2842 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2847 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2851 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2852 p
->p_vaddr
-= off
- p
->p_offset
;
2853 if (! m
->p_paddr_valid
)
2854 p
->p_paddr
-= off
- p
->p_offset
;
2857 if (p
->p_type
== PT_LOAD
)
2859 phdrs_vaddr
= p
->p_vaddr
;
2860 phdrs_paddr
= p
->p_paddr
;
2863 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2866 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2867 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2870 if (p
->p_type
== PT_LOAD
2871 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2873 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2879 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2880 p
->p_filesz
+= adjust
;
2881 p
->p_memsz
+= adjust
;
2887 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2891 bfd_size_type align
;
2895 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2897 /* The section may have artificial alignment forced by a
2898 link script. Notice this case by the gap between the
2899 cumulative phdr vma and the section's vma. */
2900 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2902 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2904 p
->p_memsz
+= adjust
;
2907 if ((flags
& SEC_LOAD
) != 0)
2908 p
->p_filesz
+= adjust
;
2911 if (p
->p_type
== PT_LOAD
)
2913 bfd_signed_vma adjust
;
2915 if ((flags
& SEC_LOAD
) != 0)
2917 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2921 else if ((flags
& SEC_ALLOC
) != 0)
2923 /* The section VMA must equal the file position
2924 modulo the page size. FIXME: I'm not sure if
2925 this adjustment is really necessary. We used to
2926 not have the SEC_LOAD case just above, and then
2927 this was necessary, but now I'm not sure. */
2928 if ((abfd
->flags
& D_PAGED
) != 0)
2929 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2931 adjust
= (sec
->vma
- voff
) % align
;
2940 (* _bfd_error_handler
)
2941 (_("Error: First section in segment (%s) starts at 0x%x"),
2942 bfd_section_name (abfd
, sec
), sec
->lma
);
2943 (* _bfd_error_handler
)
2944 (_(" whereas segment starts at 0x%x"),
2949 p
->p_memsz
+= adjust
;
2952 if ((flags
& SEC_LOAD
) != 0)
2953 p
->p_filesz
+= adjust
;
2958 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2959 used in a linker script we may have a section with
2960 SEC_LOAD clear but which is supposed to have
2962 if ((flags
& SEC_LOAD
) != 0
2963 || (flags
& SEC_HAS_CONTENTS
) != 0)
2964 off
+= sec
->_raw_size
;
2966 if ((flags
& SEC_ALLOC
) != 0)
2967 voff
+= sec
->_raw_size
;
2970 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
2972 /* The actual "note" segment has i == 0.
2973 This is the one that actually contains everything. */
2977 p
->p_filesz
= sec
->_raw_size
;
2978 off
+= sec
->_raw_size
;
2983 /* Fake sections -- don't need to be written. */
2986 flags
= sec
->flags
= 0;
2993 p
->p_memsz
+= sec
->_raw_size
;
2995 if ((flags
& SEC_LOAD
) != 0)
2996 p
->p_filesz
+= sec
->_raw_size
;
2998 if (align
> p
->p_align
2999 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3003 if (! m
->p_flags_valid
)
3006 if ((flags
& SEC_CODE
) != 0)
3008 if ((flags
& SEC_READONLY
) == 0)
3014 /* Now that we have set the section file positions, we can set up
3015 the file positions for the non PT_LOAD segments. */
3016 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3020 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3022 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3023 p
->p_offset
= m
->sections
[0]->filepos
;
3027 if (m
->includes_filehdr
)
3029 p
->p_vaddr
= filehdr_vaddr
;
3030 if (! m
->p_paddr_valid
)
3031 p
->p_paddr
= filehdr_paddr
;
3033 else if (m
->includes_phdrs
)
3035 p
->p_vaddr
= phdrs_vaddr
;
3036 if (! m
->p_paddr_valid
)
3037 p
->p_paddr
= phdrs_paddr
;
3042 /* Clear out any program headers we allocated but did not use. */
3043 for (; count
< alloc
; count
++, p
++)
3045 memset (p
, 0, sizeof *p
);
3046 p
->p_type
= PT_NULL
;
3049 elf_tdata (abfd
)->phdr
= phdrs
;
3051 elf_tdata (abfd
)->next_file_pos
= off
;
3053 /* Write out the program headers. */
3054 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3055 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3061 /* Get the size of the program header.
3063 If this is called by the linker before any of the section VMA's are set, it
3064 can't calculate the correct value for a strange memory layout. This only
3065 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3066 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3067 data segment (exclusive of .interp and .dynamic).
3069 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3070 will be two segments. */
3072 static bfd_size_type
3073 get_program_header_size (abfd
)
3078 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3080 /* We can't return a different result each time we're called. */
3081 if (elf_tdata (abfd
)->program_header_size
!= 0)
3082 return elf_tdata (abfd
)->program_header_size
;
3084 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3086 struct elf_segment_map
*m
;
3089 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3091 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3092 return elf_tdata (abfd
)->program_header_size
;
3095 /* Assume we will need exactly two PT_LOAD segments: one for text
3096 and one for data. */
3099 s
= bfd_get_section_by_name (abfd
, ".interp");
3100 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3102 /* If we have a loadable interpreter section, we need a
3103 PT_INTERP segment. In this case, assume we also need a
3104 PT_PHDR segment, although that may not be true for all
3109 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3111 /* We need a PT_DYNAMIC segment. */
3115 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3117 if ((s
->flags
& SEC_LOAD
) != 0
3118 && strncmp (s
->name
, ".note", 5) == 0)
3120 /* We need a PT_NOTE segment. */
3125 /* Let the backend count up any program headers it might need. */
3126 if (bed
->elf_backend_additional_program_headers
)
3130 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3136 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3137 return elf_tdata (abfd
)->program_header_size
;
3140 /* Work out the file positions of all the sections. This is called by
3141 _bfd_elf_compute_section_file_positions. All the section sizes and
3142 VMAs must be known before this is called.
3144 We do not consider reloc sections at this point, unless they form
3145 part of the loadable image. Reloc sections are assigned file
3146 positions in assign_file_positions_for_relocs, which is called by
3147 write_object_contents and final_link.
3149 We also don't set the positions of the .symtab and .strtab here. */
3152 assign_file_positions_except_relocs (abfd
)
3155 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3156 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3157 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3159 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3161 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3162 && bfd_get_format (abfd
) != bfd_core
)
3164 Elf_Internal_Shdr
**hdrpp
;
3167 /* Start after the ELF header. */
3168 off
= i_ehdrp
->e_ehsize
;
3170 /* We are not creating an executable, which means that we are
3171 not creating a program header, and that the actual order of
3172 the sections in the file is unimportant. */
3173 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3175 Elf_Internal_Shdr
*hdr
;
3178 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3180 hdr
->sh_offset
= -1;
3183 if (i
== tdata
->symtab_section
3184 || i
== tdata
->strtab_section
)
3186 hdr
->sh_offset
= -1;
3190 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3196 Elf_Internal_Shdr
**hdrpp
;
3198 /* Assign file positions for the loaded sections based on the
3199 assignment of sections to segments. */
3200 if (! assign_file_positions_for_segments (abfd
))
3203 /* Assign file positions for the other sections. */
3205 off
= elf_tdata (abfd
)->next_file_pos
;
3206 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3208 Elf_Internal_Shdr
*hdr
;
3211 if (hdr
->bfd_section
!= NULL
3212 && hdr
->bfd_section
->filepos
!= 0)
3213 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3214 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3216 ((*_bfd_error_handler
)
3217 (_("%s: warning: allocated section `%s' not in segment"),
3218 bfd_get_filename (abfd
),
3219 (hdr
->bfd_section
== NULL
3221 : hdr
->bfd_section
->name
)));
3222 if ((abfd
->flags
& D_PAGED
) != 0)
3223 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3225 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3226 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3229 else if (hdr
->sh_type
== SHT_REL
3230 || hdr
->sh_type
== SHT_RELA
3231 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3232 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3233 hdr
->sh_offset
= -1;
3235 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3239 /* Place the section headers. */
3240 off
= align_file_position (off
, bed
->s
->file_align
);
3241 i_ehdrp
->e_shoff
= off
;
3242 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3244 elf_tdata (abfd
)->next_file_pos
= off
;
3253 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3254 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3255 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3257 struct bfd_strtab_hash
*shstrtab
;
3258 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3260 i_ehdrp
= elf_elfheader (abfd
);
3261 i_shdrp
= elf_elfsections (abfd
);
3263 shstrtab
= _bfd_elf_stringtab_init ();
3264 if (shstrtab
== NULL
)
3267 elf_shstrtab (abfd
) = shstrtab
;
3269 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3270 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3271 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3272 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3274 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3275 i_ehdrp
->e_ident
[EI_DATA
] =
3276 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3277 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3279 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3280 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3282 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3283 i_ehdrp
->e_ident
[count
] = 0;
3285 if ((abfd
->flags
& DYNAMIC
) != 0)
3286 i_ehdrp
->e_type
= ET_DYN
;
3287 else if ((abfd
->flags
& EXEC_P
) != 0)
3288 i_ehdrp
->e_type
= ET_EXEC
;
3289 else if (bfd_get_format (abfd
) == bfd_core
)
3290 i_ehdrp
->e_type
= ET_CORE
;
3292 i_ehdrp
->e_type
= ET_REL
;
3294 switch (bfd_get_arch (abfd
))
3296 case bfd_arch_unknown
:
3297 i_ehdrp
->e_machine
= EM_NONE
;
3299 case bfd_arch_sparc
:
3300 if (bfd_get_arch_size (abfd
) == 64)
3301 i_ehdrp
->e_machine
= EM_SPARCV9
;
3303 i_ehdrp
->e_machine
= EM_SPARC
;
3306 i_ehdrp
->e_machine
= EM_S370
;
3309 if (bfd_get_arch_size (abfd
) == 64)
3310 i_ehdrp
->e_machine
= EM_X86_64
;
3312 i_ehdrp
->e_machine
= EM_386
;
3315 i_ehdrp
->e_machine
= EM_IA_64
;
3317 case bfd_arch_m68hc11
:
3318 i_ehdrp
->e_machine
= EM_68HC11
;
3320 case bfd_arch_m68hc12
:
3321 i_ehdrp
->e_machine
= EM_68HC12
;
3324 i_ehdrp
->e_machine
= EM_68K
;
3327 i_ehdrp
->e_machine
= EM_88K
;
3330 i_ehdrp
->e_machine
= EM_860
;
3333 i_ehdrp
->e_machine
= EM_960
;
3335 case bfd_arch_mips
: /* MIPS Rxxxx */
3336 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3339 i_ehdrp
->e_machine
= EM_PARISC
;
3341 case bfd_arch_powerpc
:
3342 i_ehdrp
->e_machine
= EM_PPC
;
3344 case bfd_arch_alpha
:
3345 i_ehdrp
->e_machine
= EM_ALPHA
;
3348 i_ehdrp
->e_machine
= EM_SH
;
3351 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3354 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3357 i_ehdrp
->e_machine
= EM_CYGNUS_FR30
;
3359 case bfd_arch_mcore
:
3360 i_ehdrp
->e_machine
= EM_MCORE
;
3363 i_ehdrp
->e_machine
= EM_AVR
;
3366 switch (bfd_get_mach (abfd
))
3369 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3373 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3376 i_ehdrp
->e_machine
= EM_ARM
;
3379 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3381 case bfd_arch_mn10200
:
3382 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3384 case bfd_arch_mn10300
:
3385 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3388 i_ehdrp
->e_machine
= EM_PJ
;
3391 i_ehdrp
->e_machine
= EM_CRIS
;
3393 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3395 i_ehdrp
->e_machine
= EM_NONE
;
3397 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3398 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3400 /* No program header, for now. */
3401 i_ehdrp
->e_phoff
= 0;
3402 i_ehdrp
->e_phentsize
= 0;
3403 i_ehdrp
->e_phnum
= 0;
3405 /* Each bfd section is section header entry. */
3406 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3407 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3409 /* If we're building an executable, we'll need a program header table. */
3410 if (abfd
->flags
& EXEC_P
)
3412 /* It all happens later. */
3414 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3416 /* elf_build_phdrs() returns a (NULL-terminated) array of
3417 Elf_Internal_Phdrs. */
3418 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3419 i_ehdrp
->e_phoff
= outbase
;
3420 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3425 i_ehdrp
->e_phentsize
= 0;
3427 i_ehdrp
->e_phoff
= 0;
3430 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3431 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3432 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3433 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3434 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3435 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3436 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3437 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3438 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3444 /* Assign file positions for all the reloc sections which are not part
3445 of the loadable file image. */
3448 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3453 Elf_Internal_Shdr
**shdrpp
;
3455 off
= elf_tdata (abfd
)->next_file_pos
;
3457 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3458 i
< elf_elfheader (abfd
)->e_shnum
;
3461 Elf_Internal_Shdr
*shdrp
;
3464 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3465 && shdrp
->sh_offset
== -1)
3466 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3469 elf_tdata (abfd
)->next_file_pos
= off
;
3473 _bfd_elf_write_object_contents (abfd
)
3476 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3477 Elf_Internal_Ehdr
*i_ehdrp
;
3478 Elf_Internal_Shdr
**i_shdrp
;
3482 if (! abfd
->output_has_begun
3483 && ! _bfd_elf_compute_section_file_positions
3484 (abfd
, (struct bfd_link_info
*) NULL
))
3487 i_shdrp
= elf_elfsections (abfd
);
3488 i_ehdrp
= elf_elfheader (abfd
);
3491 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3495 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3497 /* After writing the headers, we need to write the sections too... */
3498 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3500 if (bed
->elf_backend_section_processing
)
3501 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3502 if (i_shdrp
[count
]->contents
)
3504 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3505 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3507 != i_shdrp
[count
]->sh_size
))
3512 /* Write out the section header names. */
3513 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3514 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3517 if (bed
->elf_backend_final_write_processing
)
3518 (*bed
->elf_backend_final_write_processing
) (abfd
,
3519 elf_tdata (abfd
)->linker
);
3521 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3525 _bfd_elf_write_corefile_contents (abfd
)
3528 /* Hopefully this can be done just like an object file. */
3529 return _bfd_elf_write_object_contents (abfd
);
3532 /* Given a section, search the header to find them. */
3535 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3539 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3540 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3542 Elf_Internal_Shdr
*hdr
;
3543 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3545 for (index
= 0; index
< maxindex
; index
++)
3547 hdr
= i_shdrp
[index
];
3548 if (hdr
->bfd_section
== asect
)
3552 if (bed
->elf_backend_section_from_bfd_section
)
3554 for (index
= 0; index
< maxindex
; index
++)
3558 hdr
= i_shdrp
[index
];
3560 if ((*bed
->elf_backend_section_from_bfd_section
)
3561 (abfd
, hdr
, asect
, &retval
))
3566 if (bfd_is_abs_section (asect
))
3568 if (bfd_is_com_section (asect
))
3570 if (bfd_is_und_section (asect
))
3573 bfd_set_error (bfd_error_nonrepresentable_section
);
3578 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3582 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3584 asymbol
**asym_ptr_ptr
;
3586 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3588 flagword flags
= asym_ptr
->flags
;
3590 /* When gas creates relocations against local labels, it creates its
3591 own symbol for the section, but does put the symbol into the
3592 symbol chain, so udata is 0. When the linker is generating
3593 relocatable output, this section symbol may be for one of the
3594 input sections rather than the output section. */
3595 if (asym_ptr
->udata
.i
== 0
3596 && (flags
& BSF_SECTION_SYM
)
3597 && asym_ptr
->section
)
3601 if (asym_ptr
->section
->output_section
!= NULL
)
3602 indx
= asym_ptr
->section
->output_section
->index
;
3604 indx
= asym_ptr
->section
->index
;
3605 if (elf_section_syms (abfd
)[indx
])
3606 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3609 idx
= asym_ptr
->udata
.i
;
3613 /* This case can occur when using --strip-symbol on a symbol
3614 which is used in a relocation entry. */
3615 (*_bfd_error_handler
)
3616 (_("%s: symbol `%s' required but not present"),
3617 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3618 bfd_set_error (bfd_error_no_symbols
);
3625 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3626 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3627 elf_symbol_flags (flags
));
3635 /* Copy private BFD data. This copies any program header information. */
3638 copy_private_bfd_data (ibfd
, obfd
)
3642 Elf_Internal_Ehdr
* iehdr
;
3643 struct elf_segment_map
* map
;
3644 struct elf_segment_map
* map_first
;
3645 struct elf_segment_map
** pointer_to_map
;
3646 Elf_Internal_Phdr
* segment
;
3649 unsigned int num_segments
;
3650 boolean phdr_included
= false;
3651 bfd_vma maxpagesize
;
3652 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3653 unsigned int phdr_adjust_num
= 0;
3655 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3656 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3659 if (elf_tdata (ibfd
)->phdr
== NULL
)
3662 iehdr
= elf_elfheader (ibfd
);
3665 pointer_to_map
= &map_first
;
3667 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3668 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3670 /* Returns the end address of the segment + 1. */
3671 #define SEGMENT_END(segment, start) \
3672 (start + (segment->p_memsz > segment->p_filesz \
3673 ? segment->p_memsz : segment->p_filesz))
3675 /* Returns true if the given section is contained within
3676 the given segment. VMA addresses are compared. */
3677 #define IS_CONTAINED_BY_VMA(section, segment) \
3678 (section->vma >= segment->p_vaddr \
3679 && (section->vma + section->_raw_size) \
3680 <= (SEGMENT_END (segment, segment->p_vaddr)))
3682 /* Returns true if the given section is contained within
3683 the given segment. LMA addresses are compared. */
3684 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3685 (section->lma >= base \
3686 && (section->lma + section->_raw_size) \
3687 <= SEGMENT_END (segment, base))
3689 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3690 #define IS_COREFILE_NOTE(p, s) \
3691 (p->p_type == PT_NOTE \
3692 && bfd_get_format (ibfd) == bfd_core \
3693 && s->vma == 0 && s->lma == 0 \
3694 && (bfd_vma) s->filepos >= p->p_offset \
3695 && (bfd_vma) s->filepos + s->_raw_size \
3696 <= p->p_offset + p->p_filesz)
3698 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3699 linker, which generates a PT_INTERP section with p_vaddr and
3700 p_memsz set to 0. */
3701 #define IS_SOLARIS_PT_INTERP(p, s) \
3703 && p->p_filesz > 0 \
3704 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3705 && s->_raw_size > 0 \
3706 && (bfd_vma) s->filepos >= p->p_offset \
3707 && ((bfd_vma) s->filepos + s->_raw_size \
3708 <= p->p_offset + p->p_filesz))
3710 /* Decide if the given section should be included in the given segment.
3711 A section will be included if:
3712 1. It is within the address space of the segment,
3713 2. It is an allocated segment,
3714 3. There is an output section associated with it,
3715 4. The section has not already been allocated to a previous segment. */
3716 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3717 ((((IS_CONTAINED_BY_VMA (section, segment) \
3718 || IS_SOLARIS_PT_INTERP (segment, section)) \
3719 && (section->flags & SEC_ALLOC) != 0) \
3720 || IS_COREFILE_NOTE (segment, section)) \
3721 && section->output_section != NULL \
3722 && section->segment_mark == false)
3724 /* Returns true iff seg1 starts after the end of seg2. */
3725 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3726 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3728 /* Returns true iff seg1 and seg2 overlap. */
3729 #define SEGMENT_OVERLAPS(seg1, seg2) \
3730 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3732 /* Initialise the segment mark field. */
3733 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3734 section
->segment_mark
= false;
3736 /* Scan through the segments specified in the program header
3737 of the input BFD. For this first scan we look for overlaps
3738 in the loadable segments. These can be created by wierd
3739 parameters to objcopy. */
3740 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3745 Elf_Internal_Phdr
*segment2
;
3747 if (segment
->p_type
!= PT_LOAD
)
3750 /* Determine if this segment overlaps any previous segments. */
3751 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
3753 bfd_signed_vma extra_length
;
3755 if (segment2
->p_type
!= PT_LOAD
3756 || ! SEGMENT_OVERLAPS (segment
, segment2
))
3759 /* Merge the two segments together. */
3760 if (segment2
->p_vaddr
< segment
->p_vaddr
)
3762 /* Extend SEGMENT2 to include SEGMENT and then delete
3765 SEGMENT_END (segment
, segment
->p_vaddr
)
3766 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
3768 if (extra_length
> 0)
3770 segment2
->p_memsz
+= extra_length
;
3771 segment2
->p_filesz
+= extra_length
;
3774 segment
->p_type
= PT_NULL
;
3776 /* Since we have deleted P we must restart the outer loop. */
3778 segment
= elf_tdata (ibfd
)->phdr
;
3783 /* Extend SEGMENT to include SEGMENT2 and then delete
3786 SEGMENT_END (segment2
, segment2
->p_vaddr
)
3787 - SEGMENT_END (segment
, segment
->p_vaddr
);
3789 if (extra_length
> 0)
3791 segment
->p_memsz
+= extra_length
;
3792 segment
->p_filesz
+= extra_length
;
3795 segment2
->p_type
= PT_NULL
;
3800 /* The second scan attempts to assign sections to segments. */
3801 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3805 unsigned int section_count
;
3806 asection
** sections
;
3807 asection
* output_section
;
3809 bfd_vma matching_lma
;
3810 bfd_vma suggested_lma
;
3813 if (segment
->p_type
== PT_NULL
)
3816 /* Compute how many sections might be placed into this segment. */
3818 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3819 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3822 /* Allocate a segment map big enough to contain all of the
3823 sections we have selected. */
3824 map
= ((struct elf_segment_map
*)
3826 (sizeof (struct elf_segment_map
)
3827 + ((size_t) section_count
- 1) * sizeof (asection
*))));
3831 /* Initialise the fields of the segment map. Default to
3832 using the physical address of the segment in the input BFD. */
3834 map
->p_type
= segment
->p_type
;
3835 map
->p_flags
= segment
->p_flags
;
3836 map
->p_flags_valid
= 1;
3837 map
->p_paddr
= segment
->p_paddr
;
3838 map
->p_paddr_valid
= 1;
3840 /* Determine if this segment contains the ELF file header
3841 and if it contains the program headers themselves. */
3842 map
->includes_filehdr
= (segment
->p_offset
== 0
3843 && segment
->p_filesz
>= iehdr
->e_ehsize
);
3845 map
->includes_phdrs
= 0;
3847 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
3849 map
->includes_phdrs
=
3850 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3851 && (segment
->p_offset
+ segment
->p_filesz
3852 >= ((bfd_vma
) iehdr
->e_phoff
3853 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3855 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
3856 phdr_included
= true;
3859 if (section_count
== 0)
3861 /* Special segments, such as the PT_PHDR segment, may contain
3862 no sections, but ordinary, loadable segments should contain
3864 if (segment
->p_type
== PT_LOAD
)
3866 (_("%s: warning: Empty loadable segment detected\n"),
3867 bfd_get_filename (ibfd
));
3870 *pointer_to_map
= map
;
3871 pointer_to_map
= &map
->next
;
3876 /* Now scan the sections in the input BFD again and attempt
3877 to add their corresponding output sections to the segment map.
3878 The problem here is how to handle an output section which has
3879 been moved (ie had its LMA changed). There are four possibilities:
3881 1. None of the sections have been moved.
3882 In this case we can continue to use the segment LMA from the
3885 2. All of the sections have been moved by the same amount.
3886 In this case we can change the segment's LMA to match the LMA
3887 of the first section.
3889 3. Some of the sections have been moved, others have not.
3890 In this case those sections which have not been moved can be
3891 placed in the current segment which will have to have its size,
3892 and possibly its LMA changed, and a new segment or segments will
3893 have to be created to contain the other sections.
3895 4. The sections have been moved, but not be the same amount.
3896 In this case we can change the segment's LMA to match the LMA
3897 of the first section and we will have to create a new segment
3898 or segments to contain the other sections.
3900 In order to save time, we allocate an array to hold the section
3901 pointers that we are interested in. As these sections get assigned
3902 to a segment, they are removed from this array. */
3904 sections
= (asection
**) bfd_malloc
3905 (sizeof (asection
*) * section_count
);
3906 if (sections
== NULL
)
3909 /* Step One: Scan for segment vs section LMA conflicts.
3910 Also add the sections to the section array allocated above.
3911 Also add the sections to the current segment. In the common
3912 case, where the sections have not been moved, this means that
3913 we have completely filled the segment, and there is nothing
3919 for (j
= 0, section
= ibfd
->sections
;
3921 section
= section
->next
)
3923 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3925 output_section
= section
->output_section
;
3927 sections
[j
++] = section
;
3929 /* The Solaris native linker always sets p_paddr to 0.
3930 We try to catch that case here, and set it to the
3932 if (segment
->p_paddr
== 0
3933 && segment
->p_vaddr
!= 0
3935 && output_section
->lma
!= 0
3936 && (output_section
->vma
== (segment
->p_vaddr
3937 + (map
->includes_filehdr
3940 + (map
->includes_phdrs
3941 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3943 map
->p_paddr
= segment
->p_vaddr
;
3945 /* Match up the physical address of the segment with the
3946 LMA address of the output section. */
3947 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
3948 || IS_COREFILE_NOTE (segment
, section
))
3950 if (matching_lma
== 0)
3951 matching_lma
= output_section
->lma
;
3953 /* We assume that if the section fits within the segment
3954 then it does not overlap any other section within that
3956 map
->sections
[isec
++] = output_section
;
3958 else if (suggested_lma
== 0)
3959 suggested_lma
= output_section
->lma
;
3963 BFD_ASSERT (j
== section_count
);
3965 /* Step Two: Adjust the physical address of the current segment,
3967 if (isec
== section_count
)
3969 /* All of the sections fitted within the segment as currently
3970 specified. This is the default case. Add the segment to
3971 the list of built segments and carry on to process the next
3972 program header in the input BFD. */
3973 map
->count
= section_count
;
3974 *pointer_to_map
= map
;
3975 pointer_to_map
= &map
->next
;
3982 if (matching_lma
!= 0)
3984 /* At least one section fits inside the current segment.
3985 Keep it, but modify its physical address to match the
3986 LMA of the first section that fitted. */
3987 map
->p_paddr
= matching_lma
;
3991 /* None of the sections fitted inside the current segment.
3992 Change the current segment's physical address to match
3993 the LMA of the first section. */
3994 map
->p_paddr
= suggested_lma
;
3997 /* Offset the segment physical address from the lma
3998 to allow for space taken up by elf headers. */
3999 if (map
->includes_filehdr
)
4000 map
->p_paddr
-= iehdr
->e_ehsize
;
4002 if (map
->includes_phdrs
)
4004 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4006 /* iehdr->e_phnum is just an estimate of the number
4007 of program headers that we will need. Make a note
4008 here of the number we used and the segment we chose
4009 to hold these headers, so that we can adjust the
4010 offset when we know the correct value. */
4011 phdr_adjust_num
= iehdr
->e_phnum
;
4012 phdr_adjust_seg
= map
;
4016 /* Step Three: Loop over the sections again, this time assigning
4017 those that fit to the current segment and remvoing them from the
4018 sections array; but making sure not to leave large gaps. Once all
4019 possible sections have been assigned to the current segment it is
4020 added to the list of built segments and if sections still remain
4021 to be assigned, a new segment is constructed before repeating
4029 /* Fill the current segment with sections that fit. */
4030 for (j
= 0; j
< section_count
; j
++)
4032 section
= sections
[j
];
4034 if (section
== NULL
)
4037 output_section
= section
->output_section
;
4039 BFD_ASSERT (output_section
!= NULL
);
4041 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4042 || IS_COREFILE_NOTE (segment
, section
))
4044 if (map
->count
== 0)
4046 /* If the first section in a segment does not start at
4047 the beginning of the segment, then something is
4049 if (output_section
->lma
!=
4051 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4052 + (map
->includes_phdrs
4053 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4059 asection
* prev_sec
;
4061 prev_sec
= map
->sections
[map
->count
- 1];
4063 /* If the gap between the end of the previous section
4064 and the start of this section is more than
4065 maxpagesize then we need to start a new segment. */
4066 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4067 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4068 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4070 if (suggested_lma
== 0)
4071 suggested_lma
= output_section
->lma
;
4077 map
->sections
[map
->count
++] = output_section
;
4080 section
->segment_mark
= true;
4082 else if (suggested_lma
== 0)
4083 suggested_lma
= output_section
->lma
;
4086 BFD_ASSERT (map
->count
> 0);
4088 /* Add the current segment to the list of built segments. */
4089 *pointer_to_map
= map
;
4090 pointer_to_map
= &map
->next
;
4092 if (isec
< section_count
)
4094 /* We still have not allocated all of the sections to
4095 segments. Create a new segment here, initialise it
4096 and carry on looping. */
4097 map
= ((struct elf_segment_map
*)
4099 (sizeof (struct elf_segment_map
)
4100 + ((size_t) section_count
- 1)
4101 * sizeof (asection
*))));
4105 /* Initialise the fields of the segment map. Set the physical
4106 physical address to the LMA of the first section that has
4107 not yet been assigned. */
4109 map
->p_type
= segment
->p_type
;
4110 map
->p_flags
= segment
->p_flags
;
4111 map
->p_flags_valid
= 1;
4112 map
->p_paddr
= suggested_lma
;
4113 map
->p_paddr_valid
= 1;
4114 map
->includes_filehdr
= 0;
4115 map
->includes_phdrs
= 0;
4118 while (isec
< section_count
);
4123 /* The Solaris linker creates program headers in which all the
4124 p_paddr fields are zero. When we try to objcopy or strip such a
4125 file, we get confused. Check for this case, and if we find it
4126 reset the p_paddr_valid fields. */
4127 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4128 if (map
->p_paddr
!= 0)
4132 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4133 map
->p_paddr_valid
= 0;
4136 elf_tdata (obfd
)->segment_map
= map_first
;
4138 /* If we had to estimate the number of program headers that were
4139 going to be needed, then check our estimate know and adjust
4140 the offset if necessary. */
4141 if (phdr_adjust_seg
!= NULL
)
4145 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4148 if (count
> phdr_adjust_num
)
4149 phdr_adjust_seg
->p_paddr
4150 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4154 /* Final Step: Sort the segments into ascending order of physical
4156 if (map_first
!= NULL
)
4158 struct elf_segment_map
*prev
;
4161 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4163 /* Yes I know - its a bubble sort.... */
4164 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4166 /* Swap map and map->next. */
4167 prev
->next
= map
->next
;
4168 map
->next
= map
->next
->next
;
4169 prev
->next
->next
= map
;
4179 #undef IS_CONTAINED_BY_VMA
4180 #undef IS_CONTAINED_BY_LMA
4181 #undef IS_COREFILE_NOTE
4182 #undef IS_SOLARIS_PT_INTERP
4183 #undef INCLUDE_SECTION_IN_SEGMENT
4184 #undef SEGMENT_AFTER_SEGMENT
4185 #undef SEGMENT_OVERLAPS
4189 /* Copy private section information. This copies over the entsize
4190 field, and sometimes the info field. */
4193 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4199 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4201 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4202 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4205 /* Copy over private BFD data if it has not already been copied.
4206 This must be done here, rather than in the copy_private_bfd_data
4207 entry point, because the latter is called after the section
4208 contents have been set, which means that the program headers have
4209 already been worked out. */
4210 if (elf_tdata (obfd
)->segment_map
== NULL
4211 && elf_tdata (ibfd
)->phdr
!= NULL
)
4215 /* Only set up the segments if there are no more SEC_ALLOC
4216 sections. FIXME: This won't do the right thing if objcopy is
4217 used to remove the last SEC_ALLOC section, since objcopy
4218 won't call this routine in that case. */
4219 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4220 if ((s
->flags
& SEC_ALLOC
) != 0)
4224 if (! copy_private_bfd_data (ibfd
, obfd
))
4229 ihdr
= &elf_section_data (isec
)->this_hdr
;
4230 ohdr
= &elf_section_data (osec
)->this_hdr
;
4232 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4234 if (ihdr
->sh_type
== SHT_SYMTAB
4235 || ihdr
->sh_type
== SHT_DYNSYM
4236 || ihdr
->sh_type
== SHT_GNU_verneed
4237 || ihdr
->sh_type
== SHT_GNU_verdef
)
4238 ohdr
->sh_info
= ihdr
->sh_info
;
4240 elf_section_data (osec
)->use_rela_p
4241 = elf_section_data (isec
)->use_rela_p
;
4246 /* Copy private symbol information. If this symbol is in a section
4247 which we did not map into a BFD section, try to map the section
4248 index correctly. We use special macro definitions for the mapped
4249 section indices; these definitions are interpreted by the
4250 swap_out_syms function. */
4252 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4253 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4254 #define MAP_STRTAB (SHN_LORESERVE - 3)
4255 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4258 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4264 elf_symbol_type
*isym
, *osym
;
4266 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4267 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4270 isym
= elf_symbol_from (ibfd
, isymarg
);
4271 osym
= elf_symbol_from (obfd
, osymarg
);
4275 && bfd_is_abs_section (isym
->symbol
.section
))
4279 shndx
= isym
->internal_elf_sym
.st_shndx
;
4280 if (shndx
== elf_onesymtab (ibfd
))
4281 shndx
= MAP_ONESYMTAB
;
4282 else if (shndx
== elf_dynsymtab (ibfd
))
4283 shndx
= MAP_DYNSYMTAB
;
4284 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4286 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4287 shndx
= MAP_SHSTRTAB
;
4288 osym
->internal_elf_sym
.st_shndx
= shndx
;
4294 /* Swap out the symbols. */
4297 swap_out_syms (abfd
, sttp
, relocatable_p
)
4299 struct bfd_strtab_hash
**sttp
;
4302 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4304 if (!elf_map_symbols (abfd
))
4307 /* Dump out the symtabs. */
4309 int symcount
= bfd_get_symcount (abfd
);
4310 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4311 struct bfd_strtab_hash
*stt
;
4312 Elf_Internal_Shdr
*symtab_hdr
;
4313 Elf_Internal_Shdr
*symstrtab_hdr
;
4314 char *outbound_syms
;
4317 stt
= _bfd_elf_stringtab_init ();
4321 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4322 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4323 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4324 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4325 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4326 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4328 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4329 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4331 outbound_syms
= bfd_alloc (abfd
,
4332 (1 + symcount
) * bed
->s
->sizeof_sym
);
4333 if (outbound_syms
== NULL
)
4335 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4337 /* now generate the data (for "contents") */
4339 /* Fill in zeroth symbol and swap it out. */
4340 Elf_Internal_Sym sym
;
4346 sym
.st_shndx
= SHN_UNDEF
;
4347 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4348 outbound_syms
+= bed
->s
->sizeof_sym
;
4350 for (idx
= 0; idx
< symcount
; idx
++)
4352 Elf_Internal_Sym sym
;
4353 bfd_vma value
= syms
[idx
]->value
;
4354 elf_symbol_type
*type_ptr
;
4355 flagword flags
= syms
[idx
]->flags
;
4358 if ((flags
& BSF_SECTION_SYM
) != 0)
4360 /* Section symbols have no name. */
4365 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4368 if (sym
.st_name
== (unsigned long) -1)
4372 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4374 if ((flags
& BSF_SECTION_SYM
) == 0
4375 && bfd_is_com_section (syms
[idx
]->section
))
4377 /* ELF common symbols put the alignment into the `value' field,
4378 and the size into the `size' field. This is backwards from
4379 how BFD handles it, so reverse it here. */
4380 sym
.st_size
= value
;
4381 if (type_ptr
== NULL
4382 || type_ptr
->internal_elf_sym
.st_value
== 0)
4383 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4385 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4386 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4387 (abfd
, syms
[idx
]->section
);
4391 asection
*sec
= syms
[idx
]->section
;
4394 if (sec
->output_section
)
4396 value
+= sec
->output_offset
;
4397 sec
= sec
->output_section
;
4399 /* Don't add in the section vma for relocatable output. */
4400 if (! relocatable_p
)
4402 sym
.st_value
= value
;
4403 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4405 if (bfd_is_abs_section (sec
)
4407 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4409 /* This symbol is in a real ELF section which we did
4410 not create as a BFD section. Undo the mapping done
4411 by copy_private_symbol_data. */
4412 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4416 shndx
= elf_onesymtab (abfd
);
4419 shndx
= elf_dynsymtab (abfd
);
4422 shndx
= elf_tdata (abfd
)->strtab_section
;
4425 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4433 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4439 /* Writing this would be a hell of a lot easier if
4440 we had some decent documentation on bfd, and
4441 knew what to expect of the library, and what to
4442 demand of applications. For example, it
4443 appears that `objcopy' might not set the
4444 section of a symbol to be a section that is
4445 actually in the output file. */
4446 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4447 BFD_ASSERT (sec2
!= 0);
4448 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4449 BFD_ASSERT (shndx
!= -1);
4453 sym
.st_shndx
= shndx
;
4456 if ((flags
& BSF_FUNCTION
) != 0)
4458 else if ((flags
& BSF_OBJECT
) != 0)
4463 /* Processor-specific types */
4464 if (type_ptr
!= NULL
4465 && bed
->elf_backend_get_symbol_type
)
4466 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4468 if (flags
& BSF_SECTION_SYM
)
4469 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4470 else if (bfd_is_com_section (syms
[idx
]->section
))
4471 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4472 else if (bfd_is_und_section (syms
[idx
]->section
))
4473 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4477 else if (flags
& BSF_FILE
)
4478 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4481 int bind
= STB_LOCAL
;
4483 if (flags
& BSF_LOCAL
)
4485 else if (flags
& BSF_WEAK
)
4487 else if (flags
& BSF_GLOBAL
)
4490 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4493 if (type_ptr
!= NULL
)
4494 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4498 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4499 outbound_syms
+= bed
->s
->sizeof_sym
;
4503 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4504 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4506 symstrtab_hdr
->sh_flags
= 0;
4507 symstrtab_hdr
->sh_addr
= 0;
4508 symstrtab_hdr
->sh_entsize
= 0;
4509 symstrtab_hdr
->sh_link
= 0;
4510 symstrtab_hdr
->sh_info
= 0;
4511 symstrtab_hdr
->sh_addralign
= 1;
4517 /* Return the number of bytes required to hold the symtab vector.
4519 Note that we base it on the count plus 1, since we will null terminate
4520 the vector allocated based on this size. However, the ELF symbol table
4521 always has a dummy entry as symbol #0, so it ends up even. */
4524 _bfd_elf_get_symtab_upper_bound (abfd
)
4529 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4531 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4532 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4538 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4543 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4545 if (elf_dynsymtab (abfd
) == 0)
4547 bfd_set_error (bfd_error_invalid_operation
);
4551 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4552 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4558 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4559 bfd
*abfd ATTRIBUTE_UNUSED
;
4562 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4565 /* Canonicalize the relocs. */
4568 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4577 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4583 tblptr
= section
->relocation
;
4584 for (i
= 0; i
< section
->reloc_count
; i
++)
4585 *relptr
++ = tblptr
++;
4589 return section
->reloc_count
;
4593 _bfd_elf_get_symtab (abfd
, alocation
)
4595 asymbol
**alocation
;
4597 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4598 (abfd
, alocation
, false);
4601 bfd_get_symcount (abfd
) = symcount
;
4606 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4608 asymbol
**alocation
;
4610 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4611 (abfd
, alocation
, true);
4614 /* Return the size required for the dynamic reloc entries. Any
4615 section that was actually installed in the BFD, and has type
4616 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4617 considered to be a dynamic reloc section. */
4620 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4626 if (elf_dynsymtab (abfd
) == 0)
4628 bfd_set_error (bfd_error_invalid_operation
);
4632 ret
= sizeof (arelent
*);
4633 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4634 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4635 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4636 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4637 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4638 * sizeof (arelent
*));
4643 /* Canonicalize the dynamic relocation entries. Note that we return
4644 the dynamic relocations as a single block, although they are
4645 actually associated with particular sections; the interface, which
4646 was designed for SunOS style shared libraries, expects that there
4647 is only one set of dynamic relocs. Any section that was actually
4648 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4649 the dynamic symbol table, is considered to be a dynamic reloc
4653 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4658 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4662 if (elf_dynsymtab (abfd
) == 0)
4664 bfd_set_error (bfd_error_invalid_operation
);
4668 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4670 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4672 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4673 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4674 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4679 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4681 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4683 for (i
= 0; i
< count
; i
++)
4694 /* Read in the version information. */
4697 _bfd_elf_slurp_version_tables (abfd
)
4700 bfd_byte
*contents
= NULL
;
4702 if (elf_dynverdef (abfd
) != 0)
4704 Elf_Internal_Shdr
*hdr
;
4705 Elf_External_Verdef
*everdef
;
4706 Elf_Internal_Verdef
*iverdef
;
4707 Elf_Internal_Verdef
*iverdefarr
;
4708 Elf_Internal_Verdef iverdefmem
;
4710 unsigned int maxidx
;
4712 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4714 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4715 if (contents
== NULL
)
4717 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4718 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4721 /* We know the number of entries in the section but not the maximum
4722 index. Therefore we have to run through all entries and find
4724 everdef
= (Elf_External_Verdef
*) contents
;
4726 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4728 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4730 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) > maxidx
)
4731 maxidx
= iverdefmem
.vd_ndx
& VERSYM_VERSION
;
4733 everdef
= ((Elf_External_Verdef
*)
4734 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4737 elf_tdata (abfd
)->verdef
=
4738 ((Elf_Internal_Verdef
*)
4739 bfd_zalloc (abfd
, maxidx
* sizeof (Elf_Internal_Verdef
)));
4740 if (elf_tdata (abfd
)->verdef
== NULL
)
4743 elf_tdata (abfd
)->cverdefs
= maxidx
;
4745 everdef
= (Elf_External_Verdef
*) contents
;
4746 iverdefarr
= elf_tdata (abfd
)->verdef
;
4747 for (i
= 0; i
< hdr
->sh_info
; i
++)
4749 Elf_External_Verdaux
*everdaux
;
4750 Elf_Internal_Verdaux
*iverdaux
;
4753 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4755 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4756 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4758 iverdef
->vd_bfd
= abfd
;
4760 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4763 * sizeof (Elf_Internal_Verdaux
))));
4764 if (iverdef
->vd_auxptr
== NULL
)
4767 everdaux
= ((Elf_External_Verdaux
*)
4768 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4769 iverdaux
= iverdef
->vd_auxptr
;
4770 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4772 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4774 iverdaux
->vda_nodename
=
4775 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4776 iverdaux
->vda_name
);
4777 if (iverdaux
->vda_nodename
== NULL
)
4780 if (j
+ 1 < iverdef
->vd_cnt
)
4781 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4783 iverdaux
->vda_nextptr
= NULL
;
4785 everdaux
= ((Elf_External_Verdaux
*)
4786 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4789 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4791 if (i
+ 1 < hdr
->sh_info
)
4792 iverdef
->vd_nextdef
= iverdef
+ 1;
4794 iverdef
->vd_nextdef
= NULL
;
4796 everdef
= ((Elf_External_Verdef
*)
4797 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4804 if (elf_dynverref (abfd
) != 0)
4806 Elf_Internal_Shdr
*hdr
;
4807 Elf_External_Verneed
*everneed
;
4808 Elf_Internal_Verneed
*iverneed
;
4811 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4813 elf_tdata (abfd
)->verref
=
4814 ((Elf_Internal_Verneed
*)
4815 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4816 if (elf_tdata (abfd
)->verref
== NULL
)
4819 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4821 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4822 if (contents
== NULL
)
4824 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4825 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4828 everneed
= (Elf_External_Verneed
*) contents
;
4829 iverneed
= elf_tdata (abfd
)->verref
;
4830 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4832 Elf_External_Vernaux
*evernaux
;
4833 Elf_Internal_Vernaux
*ivernaux
;
4836 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4838 iverneed
->vn_bfd
= abfd
;
4840 iverneed
->vn_filename
=
4841 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4843 if (iverneed
->vn_filename
== NULL
)
4846 iverneed
->vn_auxptr
=
4847 ((Elf_Internal_Vernaux
*)
4849 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4851 evernaux
= ((Elf_External_Vernaux
*)
4852 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4853 ivernaux
= iverneed
->vn_auxptr
;
4854 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4856 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4858 ivernaux
->vna_nodename
=
4859 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4860 ivernaux
->vna_name
);
4861 if (ivernaux
->vna_nodename
== NULL
)
4864 if (j
+ 1 < iverneed
->vn_cnt
)
4865 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4867 ivernaux
->vna_nextptr
= NULL
;
4869 evernaux
= ((Elf_External_Vernaux
*)
4870 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4873 if (i
+ 1 < hdr
->sh_info
)
4874 iverneed
->vn_nextref
= iverneed
+ 1;
4876 iverneed
->vn_nextref
= NULL
;
4878 everneed
= ((Elf_External_Verneed
*)
4879 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4889 if (contents
== NULL
)
4895 _bfd_elf_make_empty_symbol (abfd
)
4898 elf_symbol_type
*newsym
;
4900 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4905 newsym
->symbol
.the_bfd
= abfd
;
4906 return &newsym
->symbol
;
4911 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4912 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4916 bfd_symbol_info (symbol
, ret
);
4919 /* Return whether a symbol name implies a local symbol. Most targets
4920 use this function for the is_local_label_name entry point, but some
4924 _bfd_elf_is_local_label_name (abfd
, name
)
4925 bfd
*abfd ATTRIBUTE_UNUSED
;
4928 /* Normal local symbols start with ``.L''. */
4929 if (name
[0] == '.' && name
[1] == 'L')
4932 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4933 DWARF debugging symbols starting with ``..''. */
4934 if (name
[0] == '.' && name
[1] == '.')
4937 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4938 emitting DWARF debugging output. I suspect this is actually a
4939 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4940 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4941 underscore to be emitted on some ELF targets). For ease of use,
4942 we treat such symbols as local. */
4943 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4950 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4951 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4952 asymbol
*symbol ATTRIBUTE_UNUSED
;
4959 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4961 enum bfd_architecture arch
;
4962 unsigned long machine
;
4964 /* If this isn't the right architecture for this backend, and this
4965 isn't the generic backend, fail. */
4966 if (arch
!= get_elf_backend_data (abfd
)->arch
4967 && arch
!= bfd_arch_unknown
4968 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4971 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4974 /* Find the nearest line to a particular section and offset, for error
4978 _bfd_elf_find_nearest_line (abfd
,
4989 CONST
char **filename_ptr
;
4990 CONST
char **functionname_ptr
;
4991 unsigned int *line_ptr
;
4994 const char *filename
;
4999 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5000 filename_ptr
, functionname_ptr
,
5004 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5005 filename_ptr
, functionname_ptr
,
5007 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5010 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5011 &found
, filename_ptr
,
5012 functionname_ptr
, line_ptr
,
5013 &elf_tdata (abfd
)->line_info
))
5018 if (symbols
== NULL
)
5025 for (p
= symbols
; *p
!= NULL
; p
++)
5029 q
= (elf_symbol_type
*) *p
;
5031 if (bfd_get_section (&q
->symbol
) != section
)
5034 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5039 filename
= bfd_asymbol_name (&q
->symbol
);
5043 if (q
->symbol
.section
== section
5044 && q
->symbol
.value
>= low_func
5045 && q
->symbol
.value
<= offset
)
5047 func
= (asymbol
*) q
;
5048 low_func
= q
->symbol
.value
;
5057 *filename_ptr
= filename
;
5058 *functionname_ptr
= bfd_asymbol_name (func
);
5064 _bfd_elf_sizeof_headers (abfd
, reloc
)
5070 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5072 ret
+= get_program_header_size (abfd
);
5077 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5082 bfd_size_type count
;
5084 Elf_Internal_Shdr
*hdr
;
5086 if (! abfd
->output_has_begun
5087 && ! _bfd_elf_compute_section_file_positions
5088 (abfd
, (struct bfd_link_info
*) NULL
))
5091 hdr
= &elf_section_data (section
)->this_hdr
;
5093 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
5095 if (bfd_write (location
, 1, count
, abfd
) != count
)
5102 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5103 bfd
*abfd ATTRIBUTE_UNUSED
;
5104 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5105 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5112 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5115 Elf_Internal_Rel
*dst
;
5121 /* Try to convert a non-ELF reloc into an ELF one. */
5124 _bfd_elf_validate_reloc (abfd
, areloc
)
5128 /* Check whether we really have an ELF howto. */
5130 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5132 bfd_reloc_code_real_type code
;
5133 reloc_howto_type
*howto
;
5135 /* Alien reloc: Try to determine its type to replace it with an
5136 equivalent ELF reloc. */
5138 if (areloc
->howto
->pc_relative
)
5140 switch (areloc
->howto
->bitsize
)
5143 code
= BFD_RELOC_8_PCREL
;
5146 code
= BFD_RELOC_12_PCREL
;
5149 code
= BFD_RELOC_16_PCREL
;
5152 code
= BFD_RELOC_24_PCREL
;
5155 code
= BFD_RELOC_32_PCREL
;
5158 code
= BFD_RELOC_64_PCREL
;
5164 howto
= bfd_reloc_type_lookup (abfd
, code
);
5166 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5168 if (howto
->pcrel_offset
)
5169 areloc
->addend
+= areloc
->address
;
5171 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5176 switch (areloc
->howto
->bitsize
)
5182 code
= BFD_RELOC_14
;
5185 code
= BFD_RELOC_16
;
5188 code
= BFD_RELOC_26
;
5191 code
= BFD_RELOC_32
;
5194 code
= BFD_RELOC_64
;
5200 howto
= bfd_reloc_type_lookup (abfd
, code
);
5204 areloc
->howto
= howto
;
5212 (*_bfd_error_handler
)
5213 (_("%s: unsupported relocation type %s"),
5214 bfd_get_filename (abfd
), areloc
->howto
->name
);
5215 bfd_set_error (bfd_error_bad_value
);
5220 _bfd_elf_close_and_cleanup (abfd
)
5223 if (bfd_get_format (abfd
) == bfd_object
)
5225 if (elf_shstrtab (abfd
) != NULL
)
5226 _bfd_stringtab_free (elf_shstrtab (abfd
));
5229 return _bfd_generic_close_and_cleanup (abfd
);
5232 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5233 in the relocation's offset. Thus we cannot allow any sort of sanity
5234 range-checking to interfere. There is nothing else to do in processing
5237 bfd_reloc_status_type
5238 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5239 bfd
*abfd ATTRIBUTE_UNUSED
;
5240 arelent
*re ATTRIBUTE_UNUSED
;
5241 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5242 PTR data ATTRIBUTE_UNUSED
;
5243 asection
*is ATTRIBUTE_UNUSED
;
5244 bfd
*obfd ATTRIBUTE_UNUSED
;
5245 char **errmsg ATTRIBUTE_UNUSED
;
5247 return bfd_reloc_ok
;
5250 /* Elf core file support. Much of this only works on native
5251 toolchains, since we rely on knowing the
5252 machine-dependent procfs structure in order to pick
5253 out details about the corefile. */
5255 #ifdef HAVE_SYS_PROCFS_H
5256 # include <sys/procfs.h>
5259 /* Define offsetof for those systems which lack it. */
5262 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5265 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5268 elfcore_make_pid (abfd
)
5271 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5272 + (elf_tdata (abfd
)->core_pid
));
5275 /* If there isn't a section called NAME, make one, using
5276 data from SECT. Note, this function will generate a
5277 reference to NAME, so you shouldn't deallocate or
5281 elfcore_maybe_make_sect (abfd
, name
, sect
)
5288 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5291 sect2
= bfd_make_section (abfd
, name
);
5295 sect2
->_raw_size
= sect
->_raw_size
;
5296 sect2
->filepos
= sect
->filepos
;
5297 sect2
->flags
= sect
->flags
;
5298 sect2
->alignment_power
= sect
->alignment_power
;
5302 /* prstatus_t exists on:
5304 linux 2.[01] + glibc
5308 #if defined (HAVE_PRSTATUS_T)
5310 elfcore_grok_prstatus (abfd
, note
)
5312 Elf_Internal_Note
*note
;
5320 if (note
->descsz
== sizeof (prstatus_t
))
5324 raw_size
= sizeof (prstat
.pr_reg
);
5325 offset
= offsetof (prstatus_t
, pr_reg
);
5326 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5328 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5329 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5331 /* pr_who exists on:
5334 pr_who doesn't exist on:
5337 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5338 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5341 #if defined (HAVE_PRSTATUS32_T)
5342 else if (note
->descsz
== sizeof (prstatus32_t
))
5344 /* 64-bit host, 32-bit corefile */
5345 prstatus32_t prstat
;
5347 raw_size
= sizeof (prstat
.pr_reg
);
5348 offset
= offsetof (prstatus32_t
, pr_reg
);
5349 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5351 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5352 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5354 /* pr_who exists on:
5357 pr_who doesn't exist on:
5360 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5361 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5364 #endif /* HAVE_PRSTATUS32_T */
5367 /* Fail - we don't know how to handle any other
5368 note size (ie. data object type). */
5372 /* Make a ".reg/999" section. */
5374 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5375 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5380 sect
= bfd_make_section (abfd
, name
);
5384 sect
->_raw_size
= raw_size
;
5385 sect
->filepos
= note
->descpos
+ offset
;
5387 sect
->flags
= SEC_HAS_CONTENTS
;
5388 sect
->alignment_power
= 2;
5390 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5395 #endif /* defined (HAVE_PRSTATUS_T) */
5397 /* Create a pseudosection containing the exact contents of NOTE. This
5398 actually creates up to two pseudosections:
5399 - For the single-threaded case, a section named NAME, unless
5400 such a section already exists.
5401 - For the multi-threaded case, a section named "NAME/PID", where
5402 PID is elfcore_make_pid (abfd).
5403 Both pseudosections have identical contents: the contents of NOTE. */
5406 elfcore_make_note_pseudosection (abfd
, name
, note
)
5409 Elf_Internal_Note
*note
;
5412 char *threaded_name
;
5415 /* Build the section name. */
5417 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5418 threaded_name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5419 if (threaded_name
== NULL
)
5421 strcpy (threaded_name
, buf
);
5423 sect
= bfd_make_section (abfd
, threaded_name
);
5426 sect
->_raw_size
= note
->descsz
;
5427 sect
->filepos
= note
->descpos
;
5428 sect
->flags
= SEC_HAS_CONTENTS
;
5429 sect
->alignment_power
= 2;
5431 if (! elfcore_maybe_make_sect (abfd
, name
, sect
))
5437 /* There isn't a consistent prfpregset_t across platforms,
5438 but it doesn't matter, because we don't have to pick this
5439 data structure apart. */
5442 elfcore_grok_prfpreg (abfd
, note
)
5444 Elf_Internal_Note
*note
;
5446 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5449 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5450 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5454 elfcore_grok_prxfpreg (abfd
, note
)
5456 Elf_Internal_Note
*note
;
5458 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5461 #if defined (HAVE_PRPSINFO_T)
5462 typedef prpsinfo_t elfcore_psinfo_t
;
5463 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5464 typedef prpsinfo32_t elfcore_psinfo32_t
;
5468 #if defined (HAVE_PSINFO_T)
5469 typedef psinfo_t elfcore_psinfo_t
;
5470 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5471 typedef psinfo32_t elfcore_psinfo32_t
;
5475 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5477 /* return a malloc'ed copy of a string at START which is at
5478 most MAX bytes long, possibly without a terminating '\0'.
5479 the copy will always have a terminating '\0'. */
5482 elfcore_strndup (abfd
, start
, max
)
5488 char *end
= memchr (start
, '\0', max
);
5496 dup
= bfd_alloc (abfd
, len
+ 1);
5500 memcpy (dup
, start
, len
);
5507 elfcore_grok_psinfo (abfd
, note
)
5509 Elf_Internal_Note
*note
;
5511 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5513 elfcore_psinfo_t psinfo
;
5515 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5517 elf_tdata (abfd
)->core_program
5518 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5520 elf_tdata (abfd
)->core_command
5521 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5523 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5524 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5526 /* 64-bit host, 32-bit corefile */
5527 elfcore_psinfo32_t psinfo
;
5529 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5531 elf_tdata (abfd
)->core_program
5532 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5534 elf_tdata (abfd
)->core_command
5535 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5541 /* Fail - we don't know how to handle any other
5542 note size (ie. data object type). */
5546 /* Note that for some reason, a spurious space is tacked
5547 onto the end of the args in some (at least one anyway)
5548 implementations, so strip it off if it exists. */
5551 char *command
= elf_tdata (abfd
)->core_command
;
5552 int n
= strlen (command
);
5554 if (0 < n
&& command
[n
- 1] == ' ')
5555 command
[n
- 1] = '\0';
5560 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5562 #if defined (HAVE_PSTATUS_T)
5564 elfcore_grok_pstatus (abfd
, note
)
5566 Elf_Internal_Note
*note
;
5568 if (note
->descsz
== sizeof (pstatus_t
)
5569 #if defined (HAVE_PXSTATUS_T)
5570 || note
->descsz
== sizeof (pxstatus_t
)
5576 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5578 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5580 #if defined (HAVE_PSTATUS32_T)
5581 else if (note
->descsz
== sizeof (pstatus32_t
))
5583 /* 64-bit host, 32-bit corefile */
5586 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5588 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5591 /* Could grab some more details from the "representative"
5592 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5593 NT_LWPSTATUS note, presumably. */
5597 #endif /* defined (HAVE_PSTATUS_T) */
5599 #if defined (HAVE_LWPSTATUS_T)
5601 elfcore_grok_lwpstatus (abfd
, note
)
5603 Elf_Internal_Note
*note
;
5605 lwpstatus_t lwpstat
;
5610 if (note
->descsz
!= sizeof (lwpstat
)
5611 #if defined (HAVE_LWPXSTATUS_T)
5612 && note
->descsz
!= sizeof (lwpxstatus_t
)
5617 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5619 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5620 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5622 /* Make a ".reg/999" section. */
5624 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5625 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5630 sect
= bfd_make_section (abfd
, name
);
5634 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5635 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5636 sect
->filepos
= note
->descpos
5637 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5640 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5641 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5642 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5645 sect
->flags
= SEC_HAS_CONTENTS
;
5646 sect
->alignment_power
= 2;
5648 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5651 /* Make a ".reg2/999" section */
5653 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5654 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5659 sect
= bfd_make_section (abfd
, name
);
5663 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5664 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5665 sect
->filepos
= note
->descpos
5666 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5669 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5670 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5671 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5674 sect
->flags
= SEC_HAS_CONTENTS
;
5675 sect
->alignment_power
= 2;
5677 if (!elfcore_maybe_make_sect (abfd
, ".reg2", sect
))
5682 #endif /* defined (HAVE_LWPSTATUS_T) */
5684 #if defined (HAVE_WIN32_PSTATUS_T)
5686 elfcore_grok_win32pstatus (abfd
, note
)
5688 Elf_Internal_Note
*note
;
5693 win32_pstatus_t pstatus
;
5695 if (note
->descsz
< sizeof (pstatus
))
5698 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
5700 switch (pstatus
.data_type
)
5702 case NOTE_INFO_PROCESS
:
5703 /* FIXME: need to add ->core_command. */
5704 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5705 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5708 case NOTE_INFO_THREAD
:
5709 /* Make a ".reg/999" section. */
5710 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5712 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5718 sect
= bfd_make_section (abfd
, name
);
5722 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5723 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5724 data
.thread_info
.thread_context
);
5725 sect
->flags
= SEC_HAS_CONTENTS
;
5726 sect
->alignment_power
= 2;
5728 if (pstatus
.data
.thread_info
.is_active_thread
)
5729 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5733 case NOTE_INFO_MODULE
:
5734 /* Make a ".module/xxxxxxxx" section. */
5735 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
5737 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5743 sect
= bfd_make_section (abfd
, name
);
5748 sect
->_raw_size
= note
->descsz
;
5749 sect
->filepos
= note
->descpos
;
5750 sect
->flags
= SEC_HAS_CONTENTS
;
5751 sect
->alignment_power
= 2;
5760 #endif /* HAVE_WIN32_PSTATUS_T */
5763 elfcore_grok_note (abfd
, note
)
5765 Elf_Internal_Note
*note
;
5772 #if defined (HAVE_PRSTATUS_T)
5774 return elfcore_grok_prstatus (abfd
, note
);
5777 #if defined (HAVE_PSTATUS_T)
5779 return elfcore_grok_pstatus (abfd
, note
);
5782 #if defined (HAVE_LWPSTATUS_T)
5784 return elfcore_grok_lwpstatus (abfd
, note
);
5787 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5788 return elfcore_grok_prfpreg (abfd
, note
);
5790 #if defined (HAVE_WIN32_PSTATUS_T)
5791 case NT_WIN32PSTATUS
:
5792 return elfcore_grok_win32pstatus (abfd
, note
);
5795 case NT_PRXFPREG
: /* Linux SSE extension */
5796 if (note
->namesz
== 5
5797 && ! strcmp (note
->namedata
, "LINUX"))
5798 return elfcore_grok_prxfpreg (abfd
, note
);
5802 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5805 return elfcore_grok_psinfo (abfd
, note
);
5811 elfcore_read_notes (abfd
, offset
, size
)
5822 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
5825 buf
= bfd_malloc ((size_t) size
);
5829 if (bfd_read (buf
, size
, 1, abfd
) != size
)
5837 while (p
< buf
+ size
)
5839 /* FIXME: bad alignment assumption. */
5840 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
5841 Elf_Internal_Note in
;
5843 in
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->type
);
5845 in
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->namesz
);
5846 in
.namedata
= xnp
->name
;
5848 in
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->descsz
);
5849 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5850 in
.descpos
= offset
+ (in
.descdata
- buf
);
5852 if (! elfcore_grok_note (abfd
, &in
))
5855 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5862 /* FIXME: This function is now unnecessary. Callers can just call
5863 bfd_section_from_phdr directly. */
5866 _bfd_elfcore_section_from_phdr (abfd
, phdr
, sec_num
)
5868 Elf_Internal_Phdr
* phdr
;
5871 if (! bfd_section_from_phdr (abfd
, phdr
, sec_num
))
5877 /* Providing external access to the ELF program header table. */
5879 /* Return an upper bound on the number of bytes required to store a
5880 copy of ABFD's program header table entries. Return -1 if an error
5881 occurs; bfd_get_error will return an appropriate code. */
5884 bfd_get_elf_phdr_upper_bound (abfd
)
5887 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5889 bfd_set_error (bfd_error_wrong_format
);
5893 return (elf_elfheader (abfd
)->e_phnum
5894 * sizeof (Elf_Internal_Phdr
));
5897 /* Copy ABFD's program header table entries to *PHDRS. The entries
5898 will be stored as an array of Elf_Internal_Phdr structures, as
5899 defined in include/elf/internal.h. To find out how large the
5900 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5902 Return the number of program header table entries read, or -1 if an
5903 error occurs; bfd_get_error will return an appropriate code. */
5906 bfd_get_elf_phdrs (abfd
, phdrs
)
5912 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5914 bfd_set_error (bfd_error_wrong_format
);
5918 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
5919 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
5920 num_phdrs
* sizeof (Elf_Internal_Phdr
));