1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
35 /* For sparc64-cross-sparc32. */
44 static INLINE
struct elf_segment_map
*make_mapping
45 PARAMS ((bfd
*, asection
**, unsigned int, unsigned int, boolean
));
46 static boolean map_sections_to_segments
PARAMS ((bfd
*));
47 static int elf_sort_sections
PARAMS ((const PTR
, const PTR
));
48 static boolean assign_file_positions_for_segments
PARAMS ((bfd
*));
49 static boolean assign_file_positions_except_relocs
PARAMS ((bfd
*));
50 static boolean prep_headers
PARAMS ((bfd
*));
51 static boolean swap_out_syms
PARAMS ((bfd
*, struct bfd_strtab_hash
**, int));
52 static boolean copy_private_bfd_data
PARAMS ((bfd
*, bfd
*));
53 static char *elf_read
PARAMS ((bfd
*, long, unsigned int));
54 static void elf_fake_sections
PARAMS ((bfd
*, asection
*, PTR
));
55 static boolean assign_section_numbers
PARAMS ((bfd
*));
56 static INLINE
int sym_is_global
PARAMS ((bfd
*, asymbol
*));
57 static boolean elf_map_symbols
PARAMS ((bfd
*));
58 static bfd_size_type get_program_header_size
PARAMS ((bfd
*));
59 static boolean elfcore_read_notes
PARAMS ((bfd
*, bfd_vma
, bfd_vma
));
60 static boolean elf_find_function
PARAMS ((bfd
*, asection
*,
62 bfd_vma
, const char **,
65 /* Swap version information in and out. The version information is
66 currently size independent. If that ever changes, this code will
67 need to move into elfcode.h. */
69 /* Swap in a Verdef structure. */
72 _bfd_elf_swap_verdef_in (abfd
, src
, dst
)
74 const Elf_External_Verdef
*src
;
75 Elf_Internal_Verdef
*dst
;
77 dst
->vd_version
= bfd_h_get_16 (abfd
, src
->vd_version
);
78 dst
->vd_flags
= bfd_h_get_16 (abfd
, src
->vd_flags
);
79 dst
->vd_ndx
= bfd_h_get_16 (abfd
, src
->vd_ndx
);
80 dst
->vd_cnt
= bfd_h_get_16 (abfd
, src
->vd_cnt
);
81 dst
->vd_hash
= bfd_h_get_32 (abfd
, src
->vd_hash
);
82 dst
->vd_aux
= bfd_h_get_32 (abfd
, src
->vd_aux
);
83 dst
->vd_next
= bfd_h_get_32 (abfd
, src
->vd_next
);
86 /* Swap out a Verdef structure. */
89 _bfd_elf_swap_verdef_out (abfd
, src
, dst
)
91 const Elf_Internal_Verdef
*src
;
92 Elf_External_Verdef
*dst
;
94 bfd_h_put_16 (abfd
, src
->vd_version
, dst
->vd_version
);
95 bfd_h_put_16 (abfd
, src
->vd_flags
, dst
->vd_flags
);
96 bfd_h_put_16 (abfd
, src
->vd_ndx
, dst
->vd_ndx
);
97 bfd_h_put_16 (abfd
, src
->vd_cnt
, dst
->vd_cnt
);
98 bfd_h_put_32 (abfd
, src
->vd_hash
, dst
->vd_hash
);
99 bfd_h_put_32 (abfd
, src
->vd_aux
, dst
->vd_aux
);
100 bfd_h_put_32 (abfd
, src
->vd_next
, dst
->vd_next
);
103 /* Swap in a Verdaux structure. */
106 _bfd_elf_swap_verdaux_in (abfd
, src
, dst
)
108 const Elf_External_Verdaux
*src
;
109 Elf_Internal_Verdaux
*dst
;
111 dst
->vda_name
= bfd_h_get_32 (abfd
, src
->vda_name
);
112 dst
->vda_next
= bfd_h_get_32 (abfd
, src
->vda_next
);
115 /* Swap out a Verdaux structure. */
118 _bfd_elf_swap_verdaux_out (abfd
, src
, dst
)
120 const Elf_Internal_Verdaux
*src
;
121 Elf_External_Verdaux
*dst
;
123 bfd_h_put_32 (abfd
, src
->vda_name
, dst
->vda_name
);
124 bfd_h_put_32 (abfd
, src
->vda_next
, dst
->vda_next
);
127 /* Swap in a Verneed structure. */
130 _bfd_elf_swap_verneed_in (abfd
, src
, dst
)
132 const Elf_External_Verneed
*src
;
133 Elf_Internal_Verneed
*dst
;
135 dst
->vn_version
= bfd_h_get_16 (abfd
, src
->vn_version
);
136 dst
->vn_cnt
= bfd_h_get_16 (abfd
, src
->vn_cnt
);
137 dst
->vn_file
= bfd_h_get_32 (abfd
, src
->vn_file
);
138 dst
->vn_aux
= bfd_h_get_32 (abfd
, src
->vn_aux
);
139 dst
->vn_next
= bfd_h_get_32 (abfd
, src
->vn_next
);
142 /* Swap out a Verneed structure. */
145 _bfd_elf_swap_verneed_out (abfd
, src
, dst
)
147 const Elf_Internal_Verneed
*src
;
148 Elf_External_Verneed
*dst
;
150 bfd_h_put_16 (abfd
, src
->vn_version
, dst
->vn_version
);
151 bfd_h_put_16 (abfd
, src
->vn_cnt
, dst
->vn_cnt
);
152 bfd_h_put_32 (abfd
, src
->vn_file
, dst
->vn_file
);
153 bfd_h_put_32 (abfd
, src
->vn_aux
, dst
->vn_aux
);
154 bfd_h_put_32 (abfd
, src
->vn_next
, dst
->vn_next
);
157 /* Swap in a Vernaux structure. */
160 _bfd_elf_swap_vernaux_in (abfd
, src
, dst
)
162 const Elf_External_Vernaux
*src
;
163 Elf_Internal_Vernaux
*dst
;
165 dst
->vna_hash
= bfd_h_get_32 (abfd
, src
->vna_hash
);
166 dst
->vna_flags
= bfd_h_get_16 (abfd
, src
->vna_flags
);
167 dst
->vna_other
= bfd_h_get_16 (abfd
, src
->vna_other
);
168 dst
->vna_name
= bfd_h_get_32 (abfd
, src
->vna_name
);
169 dst
->vna_next
= bfd_h_get_32 (abfd
, src
->vna_next
);
172 /* Swap out a Vernaux structure. */
175 _bfd_elf_swap_vernaux_out (abfd
, src
, dst
)
177 const Elf_Internal_Vernaux
*src
;
178 Elf_External_Vernaux
*dst
;
180 bfd_h_put_32 (abfd
, src
->vna_hash
, dst
->vna_hash
);
181 bfd_h_put_16 (abfd
, src
->vna_flags
, dst
->vna_flags
);
182 bfd_h_put_16 (abfd
, src
->vna_other
, dst
->vna_other
);
183 bfd_h_put_32 (abfd
, src
->vna_name
, dst
->vna_name
);
184 bfd_h_put_32 (abfd
, src
->vna_next
, dst
->vna_next
);
187 /* Swap in a Versym structure. */
190 _bfd_elf_swap_versym_in (abfd
, src
, dst
)
192 const Elf_External_Versym
*src
;
193 Elf_Internal_Versym
*dst
;
195 dst
->vs_vers
= bfd_h_get_16 (abfd
, src
->vs_vers
);
198 /* Swap out a Versym structure. */
201 _bfd_elf_swap_versym_out (abfd
, src
, dst
)
203 const Elf_Internal_Versym
*src
;
204 Elf_External_Versym
*dst
;
206 bfd_h_put_16 (abfd
, src
->vs_vers
, dst
->vs_vers
);
209 /* Standard ELF hash function. Do not change this function; you will
210 cause invalid hash tables to be generated. */
213 bfd_elf_hash (namearg
)
216 const unsigned char *name
= (const unsigned char *) namearg
;
221 while ((ch
= *name
++) != '\0')
224 if ((g
= (h
& 0xf0000000)) != 0)
227 /* The ELF ABI says `h &= ~g', but this is equivalent in
228 this case and on some machines one insn instead of two. */
235 /* Read a specified number of bytes at a specified offset in an ELF
236 file, into a newly allocated buffer, and return a pointer to the
240 elf_read (abfd
, offset
, size
)
247 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
249 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
251 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
253 if (bfd_get_error () != bfd_error_system_call
)
254 bfd_set_error (bfd_error_file_truncated
);
261 bfd_elf_mkobject (abfd
)
264 /* This just does initialization. */
265 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
266 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
267 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
268 if (elf_tdata (abfd
) == 0)
270 /* Since everything is done at close time, do we need any
277 bfd_elf_mkcorefile (abfd
)
280 /* I think this can be done just like an object file. */
281 return bfd_elf_mkobject (abfd
);
285 bfd_elf_get_str_section (abfd
, shindex
)
287 unsigned int shindex
;
289 Elf_Internal_Shdr
**i_shdrp
;
290 char *shstrtab
= NULL
;
292 unsigned int shstrtabsize
;
294 i_shdrp
= elf_elfsections (abfd
);
295 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
298 shstrtab
= (char *) i_shdrp
[shindex
]->contents
;
299 if (shstrtab
== NULL
)
301 /* No cached one, attempt to read, and cache what we read. */
302 offset
= i_shdrp
[shindex
]->sh_offset
;
303 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
304 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
305 i_shdrp
[shindex
]->contents
= (PTR
) shstrtab
;
311 bfd_elf_string_from_elf_section (abfd
, shindex
, strindex
)
313 unsigned int shindex
;
314 unsigned int strindex
;
316 Elf_Internal_Shdr
*hdr
;
321 hdr
= elf_elfsections (abfd
)[shindex
];
323 if (hdr
->contents
== NULL
324 && bfd_elf_get_str_section (abfd
, shindex
) == NULL
)
327 if (strindex
>= hdr
->sh_size
)
329 (*_bfd_error_handler
)
330 (_("%s: invalid string offset %u >= %lu for section `%s'"),
331 bfd_get_filename (abfd
), strindex
, (unsigned long) hdr
->sh_size
,
332 ((shindex
== elf_elfheader(abfd
)->e_shstrndx
333 && strindex
== hdr
->sh_name
)
335 : elf_string_from_elf_strtab (abfd
, hdr
->sh_name
)));
339 return ((char *) hdr
->contents
) + strindex
;
342 /* Make a BFD section from an ELF section. We store a pointer to the
343 BFD section in the bfd_section field of the header. */
346 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
348 Elf_Internal_Shdr
*hdr
;
353 struct elf_backend_data
*bed
;
355 if (hdr
->bfd_section
!= NULL
)
357 BFD_ASSERT (strcmp (name
,
358 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
362 newsect
= bfd_make_section_anyway (abfd
, name
);
366 newsect
->filepos
= hdr
->sh_offset
;
368 if (! bfd_set_section_vma (abfd
, newsect
, hdr
->sh_addr
)
369 || ! bfd_set_section_size (abfd
, newsect
, hdr
->sh_size
)
370 || ! bfd_set_section_alignment (abfd
, newsect
,
371 bfd_log2 (hdr
->sh_addralign
)))
374 flags
= SEC_NO_FLAGS
;
375 if (hdr
->sh_type
!= SHT_NOBITS
)
376 flags
|= SEC_HAS_CONTENTS
;
377 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
380 if (hdr
->sh_type
!= SHT_NOBITS
)
383 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
384 flags
|= SEC_READONLY
;
385 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
387 else if ((flags
& SEC_LOAD
) != 0)
390 /* The debugging sections appear to be recognized only by name, not
393 static const char *debug_sec_names
[] =
402 for (i
= sizeof (debug_sec_names
) / sizeof (debug_sec_names
[0]); i
--;)
403 if (strncmp (name
, debug_sec_names
[i
], strlen (debug_sec_names
[i
])) == 0)
407 flags
|= SEC_DEBUGGING
;
410 /* As a GNU extension, if the name begins with .gnu.linkonce, we
411 only link a single copy of the section. This is used to support
412 g++. g++ will emit each template expansion in its own section.
413 The symbols will be defined as weak, so that multiple definitions
414 are permitted. The GNU linker extension is to actually discard
415 all but one of the sections. */
416 if (strncmp (name
, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
417 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
419 bed
= get_elf_backend_data (abfd
);
420 if (bed
->elf_backend_section_flags
)
421 if (! bed
->elf_backend_section_flags (&flags
, hdr
))
424 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
427 if ((flags
& SEC_ALLOC
) != 0)
429 Elf_Internal_Phdr
*phdr
;
432 /* Look through the phdrs to see if we need to adjust the lma.
433 If all the p_paddr fields are zero, we ignore them, since
434 some ELF linkers produce such output. */
435 phdr
= elf_tdata (abfd
)->phdr
;
436 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
438 if (phdr
->p_paddr
!= 0)
441 if (i
< elf_elfheader (abfd
)->e_phnum
)
443 phdr
= elf_tdata (abfd
)->phdr
;
444 for (i
= 0; i
< elf_elfheader (abfd
)->e_phnum
; i
++, phdr
++)
446 if (phdr
->p_type
== PT_LOAD
447 && phdr
->p_vaddr
!= phdr
->p_paddr
448 && phdr
->p_vaddr
<= hdr
->sh_addr
449 && (phdr
->p_vaddr
+ phdr
->p_memsz
450 >= hdr
->sh_addr
+ hdr
->sh_size
)
451 && ((flags
& SEC_LOAD
) == 0
452 || (phdr
->p_offset
<= (bfd_vma
) hdr
->sh_offset
453 && (phdr
->p_offset
+ phdr
->p_filesz
454 >= hdr
->sh_offset
+ hdr
->sh_size
))))
456 newsect
->lma
+= phdr
->p_paddr
- phdr
->p_vaddr
;
463 hdr
->bfd_section
= newsect
;
464 elf_section_data (newsect
)->this_hdr
= *hdr
;
474 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
477 Helper functions for GDB to locate the string tables.
478 Since BFD hides string tables from callers, GDB needs to use an
479 internal hook to find them. Sun's .stabstr, in particular,
480 isn't even pointed to by the .stab section, so ordinary
481 mechanisms wouldn't work to find it, even if we had some.
484 struct elf_internal_shdr
*
485 bfd_elf_find_section (abfd
, name
)
489 Elf_Internal_Shdr
**i_shdrp
;
494 i_shdrp
= elf_elfsections (abfd
);
497 shstrtab
= bfd_elf_get_str_section
498 (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
499 if (shstrtab
!= NULL
)
501 max
= elf_elfheader (abfd
)->e_shnum
;
502 for (i
= 1; i
< max
; i
++)
503 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
510 const char *const bfd_elf_section_type_names
[] = {
511 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
512 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
513 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
516 /* ELF relocs are against symbols. If we are producing relocateable
517 output, and the reloc is against an external symbol, and nothing
518 has given us any additional addend, the resulting reloc will also
519 be against the same symbol. In such a case, we don't want to
520 change anything about the way the reloc is handled, since it will
521 all be done at final link time. Rather than put special case code
522 into bfd_perform_relocation, all the reloc types use this howto
523 function. It just short circuits the reloc if producing
524 relocateable output against an external symbol. */
526 bfd_reloc_status_type
527 bfd_elf_generic_reloc (abfd
,
534 bfd
*abfd ATTRIBUTE_UNUSED
;
535 arelent
*reloc_entry
;
537 PTR data ATTRIBUTE_UNUSED
;
538 asection
*input_section
;
540 char **error_message ATTRIBUTE_UNUSED
;
542 if (output_bfd
!= (bfd
*) NULL
543 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
544 && (! reloc_entry
->howto
->partial_inplace
545 || reloc_entry
->addend
== 0))
547 reloc_entry
->address
+= input_section
->output_offset
;
551 return bfd_reloc_continue
;
554 /* Print out the program headers. */
557 _bfd_elf_print_private_bfd_data (abfd
, farg
)
561 FILE *f
= (FILE *) farg
;
562 Elf_Internal_Phdr
*p
;
564 bfd_byte
*dynbuf
= NULL
;
566 p
= elf_tdata (abfd
)->phdr
;
571 fprintf (f
, _("\nProgram Header:\n"));
572 c
= elf_elfheader (abfd
)->e_phnum
;
573 for (i
= 0; i
< c
; i
++, p
++)
580 case PT_NULL
: s
= "NULL"; break;
581 case PT_LOAD
: s
= "LOAD"; break;
582 case PT_DYNAMIC
: s
= "DYNAMIC"; break;
583 case PT_INTERP
: s
= "INTERP"; break;
584 case PT_NOTE
: s
= "NOTE"; break;
585 case PT_SHLIB
: s
= "SHLIB"; break;
586 case PT_PHDR
: s
= "PHDR"; break;
587 default: sprintf (buf
, "0x%lx", p
->p_type
); s
= buf
; break;
589 fprintf (f
, "%8s off 0x", s
);
590 fprintf_vma (f
, p
->p_offset
);
591 fprintf (f
, " vaddr 0x");
592 fprintf_vma (f
, p
->p_vaddr
);
593 fprintf (f
, " paddr 0x");
594 fprintf_vma (f
, p
->p_paddr
);
595 fprintf (f
, " align 2**%u\n", bfd_log2 (p
->p_align
));
596 fprintf (f
, " filesz 0x");
597 fprintf_vma (f
, p
->p_filesz
);
598 fprintf (f
, " memsz 0x");
599 fprintf_vma (f
, p
->p_memsz
);
600 fprintf (f
, " flags %c%c%c",
601 (p
->p_flags
& PF_R
) != 0 ? 'r' : '-',
602 (p
->p_flags
& PF_W
) != 0 ? 'w' : '-',
603 (p
->p_flags
& PF_X
) != 0 ? 'x' : '-');
604 if ((p
->p_flags
&~ (PF_R
| PF_W
| PF_X
)) != 0)
605 fprintf (f
, " %lx", p
->p_flags
&~ (PF_R
| PF_W
| PF_X
));
610 s
= bfd_get_section_by_name (abfd
, ".dynamic");
615 bfd_byte
*extdyn
, *extdynend
;
617 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
619 fprintf (f
, _("\nDynamic Section:\n"));
621 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
624 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
628 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
631 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
633 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
634 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
637 extdynend
= extdyn
+ s
->_raw_size
;
638 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
640 Elf_Internal_Dyn dyn
;
645 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
647 if (dyn
.d_tag
== DT_NULL
)
654 sprintf (ab
, "0x%lx", (unsigned long) dyn
.d_tag
);
658 case DT_NEEDED
: name
= "NEEDED"; stringp
= true; break;
659 case DT_PLTRELSZ
: name
= "PLTRELSZ"; break;
660 case DT_PLTGOT
: name
= "PLTGOT"; break;
661 case DT_HASH
: name
= "HASH"; break;
662 case DT_STRTAB
: name
= "STRTAB"; break;
663 case DT_SYMTAB
: name
= "SYMTAB"; break;
664 case DT_RELA
: name
= "RELA"; break;
665 case DT_RELASZ
: name
= "RELASZ"; break;
666 case DT_RELAENT
: name
= "RELAENT"; break;
667 case DT_STRSZ
: name
= "STRSZ"; break;
668 case DT_SYMENT
: name
= "SYMENT"; break;
669 case DT_INIT
: name
= "INIT"; break;
670 case DT_FINI
: name
= "FINI"; break;
671 case DT_SONAME
: name
= "SONAME"; stringp
= true; break;
672 case DT_RPATH
: name
= "RPATH"; stringp
= true; break;
673 case DT_SYMBOLIC
: name
= "SYMBOLIC"; break;
674 case DT_REL
: name
= "REL"; break;
675 case DT_RELSZ
: name
= "RELSZ"; break;
676 case DT_RELENT
: name
= "RELENT"; break;
677 case DT_PLTREL
: name
= "PLTREL"; break;
678 case DT_DEBUG
: name
= "DEBUG"; break;
679 case DT_TEXTREL
: name
= "TEXTREL"; break;
680 case DT_JMPREL
: name
= "JMPREL"; break;
681 case DT_BIND_NOW
: name
= "BIND_NOW"; break;
682 case DT_INIT_ARRAY
: name
= "INIT_ARRAY"; break;
683 case DT_FINI_ARRAY
: name
= "FINI_ARRAY"; break;
684 case DT_INIT_ARRAYSZ
: name
= "INIT_ARRAYSZ"; break;
685 case DT_FINI_ARRAYSZ
: name
= "FINI_ARRAYSZ"; break;
686 case DT_RUNPATH
: name
= "RUNPATH"; stringp
= true; break;
687 case DT_FLAGS
: name
= "FLAGS"; break;
688 case DT_PREINIT_ARRAY
: name
= "PREINIT_ARRAY"; break;
689 case DT_PREINIT_ARRAYSZ
: name
= "PREINIT_ARRAYSZ"; break;
690 case DT_CHECKSUM
: name
= "CHECKSUM"; break;
691 case DT_PLTPADSZ
: name
= "PLTPADSZ"; break;
692 case DT_MOVEENT
: name
= "MOVEENT"; break;
693 case DT_MOVESZ
: name
= "MOVESZ"; break;
694 case DT_FEATURE
: name
= "FEATURE"; break;
695 case DT_POSFLAG_1
: name
= "POSFLAG_1"; break;
696 case DT_SYMINSZ
: name
= "SYMINSZ"; break;
697 case DT_SYMINENT
: name
= "SYMINENT"; break;
698 case DT_CONFIG
: name
= "CONFIG"; stringp
= true; break;
699 case DT_DEPAUDIT
: name
= "DEPAUDIT"; stringp
= true; break;
700 case DT_AUDIT
: name
= "AUDIT"; stringp
= true; break;
701 case DT_PLTPAD
: name
= "PLTPAD"; break;
702 case DT_MOVETAB
: name
= "MOVETAB"; break;
703 case DT_SYMINFO
: name
= "SYMINFO"; break;
704 case DT_RELACOUNT
: name
= "RELACOUNT"; break;
705 case DT_RELCOUNT
: name
= "RELCOUNT"; break;
706 case DT_FLAGS_1
: name
= "FLAGS_1"; break;
707 case DT_VERSYM
: name
= "VERSYM"; break;
708 case DT_VERDEF
: name
= "VERDEF"; break;
709 case DT_VERDEFNUM
: name
= "VERDEFNUM"; break;
710 case DT_VERNEED
: name
= "VERNEED"; break;
711 case DT_VERNEEDNUM
: name
= "VERNEEDNUM"; break;
712 case DT_AUXILIARY
: name
= "AUXILIARY"; stringp
= true; break;
713 case DT_USED
: name
= "USED"; break;
714 case DT_FILTER
: name
= "FILTER"; stringp
= true; break;
717 fprintf (f
, " %-11s ", name
);
719 fprintf (f
, "0x%lx", (unsigned long) dyn
.d_un
.d_val
);
724 string
= bfd_elf_string_from_elf_section (abfd
, link
,
728 fprintf (f
, "%s", string
);
737 if ((elf_dynverdef (abfd
) != 0 && elf_tdata (abfd
)->verdef
== NULL
)
738 || (elf_dynverref (abfd
) != 0 && elf_tdata (abfd
)->verref
== NULL
))
740 if (! _bfd_elf_slurp_version_tables (abfd
))
744 if (elf_dynverdef (abfd
) != 0)
746 Elf_Internal_Verdef
*t
;
748 fprintf (f
, _("\nVersion definitions:\n"));
749 for (t
= elf_tdata (abfd
)->verdef
; t
!= NULL
; t
= t
->vd_nextdef
)
751 fprintf (f
, "%d 0x%2.2x 0x%8.8lx %s\n", t
->vd_ndx
,
752 t
->vd_flags
, t
->vd_hash
, t
->vd_nodename
);
753 if (t
->vd_auxptr
->vda_nextptr
!= NULL
)
755 Elf_Internal_Verdaux
*a
;
758 for (a
= t
->vd_auxptr
->vda_nextptr
;
761 fprintf (f
, "%s ", a
->vda_nodename
);
767 if (elf_dynverref (abfd
) != 0)
769 Elf_Internal_Verneed
*t
;
771 fprintf (f
, _("\nVersion References:\n"));
772 for (t
= elf_tdata (abfd
)->verref
; t
!= NULL
; t
= t
->vn_nextref
)
774 Elf_Internal_Vernaux
*a
;
776 fprintf (f
, _(" required from %s:\n"), t
->vn_filename
);
777 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
778 fprintf (f
, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a
->vna_hash
,
779 a
->vna_flags
, a
->vna_other
, a
->vna_nodename
);
791 /* Display ELF-specific fields of a symbol. */
794 bfd_elf_print_symbol (abfd
, filep
, symbol
, how
)
798 bfd_print_symbol_type how
;
800 FILE *file
= (FILE *) filep
;
803 case bfd_print_symbol_name
:
804 fprintf (file
, "%s", symbol
->name
);
806 case bfd_print_symbol_more
:
807 fprintf (file
, "elf ");
808 fprintf_vma (file
, symbol
->value
);
809 fprintf (file
, " %lx", (long) symbol
->flags
);
811 case bfd_print_symbol_all
:
813 CONST
char *section_name
;
814 CONST
char *name
= NULL
;
815 struct elf_backend_data
*bed
;
816 unsigned char st_other
;
818 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
820 bed
= get_elf_backend_data (abfd
);
821 if (bed
->elf_backend_print_symbol_all
)
822 name
= (*bed
->elf_backend_print_symbol_all
) (abfd
, filep
, symbol
);
827 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
830 fprintf (file
, " %s\t", section_name
);
831 /* Print the "other" value for a symbol. For common symbols,
832 we've already printed the size; now print the alignment.
833 For other symbols, we have no specified alignment, and
834 we've printed the address; now print the size. */
836 (bfd_is_com_section (symbol
->section
)
837 ? ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_value
838 : ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_size
));
840 /* If we have version information, print it. */
841 if (elf_tdata (abfd
)->dynversym_section
!= 0
842 && (elf_tdata (abfd
)->dynverdef_section
!= 0
843 || elf_tdata (abfd
)->dynverref_section
!= 0))
846 const char *version_string
;
848 vernum
= ((elf_symbol_type
*) symbol
)->version
& VERSYM_VERSION
;
852 else if (vernum
== 1)
853 version_string
= "Base";
854 else if (vernum
<= elf_tdata (abfd
)->cverdefs
)
856 elf_tdata (abfd
)->verdef
[vernum
- 1].vd_nodename
;
859 Elf_Internal_Verneed
*t
;
862 for (t
= elf_tdata (abfd
)->verref
;
866 Elf_Internal_Vernaux
*a
;
868 for (a
= t
->vn_auxptr
; a
!= NULL
; a
= a
->vna_nextptr
)
870 if (a
->vna_other
== vernum
)
872 version_string
= a
->vna_nodename
;
879 if ((((elf_symbol_type
*) symbol
)->version
& VERSYM_HIDDEN
) == 0)
880 fprintf (file
, " %-11s", version_string
);
885 fprintf (file
, " (%s)", version_string
);
886 for (i
= 10 - strlen (version_string
); i
> 0; --i
)
891 /* If the st_other field is not zero, print it. */
892 st_other
= ((elf_symbol_type
*) symbol
)->internal_elf_sym
.st_other
;
897 case STV_INTERNAL
: fprintf (file
, " .internal"); break;
898 case STV_HIDDEN
: fprintf (file
, " .hidden"); break;
899 case STV_PROTECTED
: fprintf (file
, " .protected"); break;
901 /* Some other non-defined flags are also present, so print
903 fprintf (file
, " 0x%02x", (unsigned int) st_other
);
906 fprintf (file
, " %s", name
);
912 /* Create an entry in an ELF linker hash table. */
914 struct bfd_hash_entry
*
915 _bfd_elf_link_hash_newfunc (entry
, table
, string
)
916 struct bfd_hash_entry
*entry
;
917 struct bfd_hash_table
*table
;
920 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
922 /* Allocate the structure if it has not already been allocated by a
924 if (ret
== (struct elf_link_hash_entry
*) NULL
)
925 ret
= ((struct elf_link_hash_entry
*)
926 bfd_hash_allocate (table
, sizeof (struct elf_link_hash_entry
)));
927 if (ret
== (struct elf_link_hash_entry
*) NULL
)
928 return (struct bfd_hash_entry
*) ret
;
930 /* Call the allocation method of the superclass. */
931 ret
= ((struct elf_link_hash_entry
*)
932 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
934 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
936 /* Set local fields. */
940 ret
->dynstr_index
= 0;
942 ret
->got
.offset
= (bfd_vma
) -1;
943 ret
->plt
.offset
= (bfd_vma
) -1;
944 ret
->linker_section_pointer
= (elf_linker_section_pointers_t
*)0;
945 ret
->verinfo
.verdef
= NULL
;
946 ret
->vtable_entries_used
= NULL
;
947 ret
->vtable_entries_size
= 0;
948 ret
->vtable_parent
= NULL
;
949 ret
->type
= STT_NOTYPE
;
951 /* Assume that we have been called by a non-ELF symbol reader.
952 This flag is then reset by the code which reads an ELF input
953 file. This ensures that a symbol created by a non-ELF symbol
954 reader will have the flag set correctly. */
955 ret
->elf_link_hash_flags
= ELF_LINK_NON_ELF
;
958 return (struct bfd_hash_entry
*) ret
;
961 /* Copy data from an indirect symbol to its direct symbol, hiding the
962 old indirect symbol. */
965 _bfd_elf_link_hash_copy_indirect (dir
, ind
)
966 struct elf_link_hash_entry
*dir
, *ind
;
968 /* Copy down any references that we may have already seen to the
969 symbol which just became indirect. */
971 dir
->elf_link_hash_flags
|=
972 (ind
->elf_link_hash_flags
973 & (ELF_LINK_HASH_REF_DYNAMIC
974 | ELF_LINK_HASH_REF_REGULAR
975 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
976 | ELF_LINK_NON_GOT_REF
));
978 /* Copy over the global and procedure linkage table offset entries.
979 These may have been already set up by a check_relocs routine. */
980 if (dir
->got
.offset
== (bfd_vma
) -1)
982 dir
->got
.offset
= ind
->got
.offset
;
983 ind
->got
.offset
= (bfd_vma
) -1;
985 BFD_ASSERT (ind
->got
.offset
== (bfd_vma
) -1);
987 if (dir
->plt
.offset
== (bfd_vma
) -1)
989 dir
->plt
.offset
= ind
->plt
.offset
;
990 ind
->plt
.offset
= (bfd_vma
) -1;
992 BFD_ASSERT (ind
->plt
.offset
== (bfd_vma
) -1);
994 if (dir
->dynindx
== -1)
996 dir
->dynindx
= ind
->dynindx
;
997 dir
->dynstr_index
= ind
->dynstr_index
;
999 ind
->dynstr_index
= 0;
1001 BFD_ASSERT (ind
->dynindx
== -1);
1005 _bfd_elf_link_hash_hide_symbol (info
, h
)
1006 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1007 struct elf_link_hash_entry
*h
;
1009 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1011 h
->plt
.offset
= (bfd_vma
) -1;
1014 /* Initialize an ELF linker hash table. */
1017 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
1018 struct elf_link_hash_table
*table
;
1020 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
1021 struct bfd_hash_table
*,
1024 table
->dynamic_sections_created
= false;
1025 table
->dynobj
= NULL
;
1026 /* The first dynamic symbol is a dummy. */
1027 table
->dynsymcount
= 1;
1028 table
->dynstr
= NULL
;
1029 table
->bucketcount
= 0;
1030 table
->needed
= NULL
;
1031 table
->runpath
= NULL
;
1033 table
->stab_info
= NULL
;
1034 table
->dynlocal
= NULL
;
1035 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
1038 /* Create an ELF linker hash table. */
1040 struct bfd_link_hash_table
*
1041 _bfd_elf_link_hash_table_create (abfd
)
1044 struct elf_link_hash_table
*ret
;
1046 ret
= ((struct elf_link_hash_table
*)
1047 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
1048 if (ret
== (struct elf_link_hash_table
*) NULL
)
1051 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
1053 bfd_release (abfd
, ret
);
1060 /* This is a hook for the ELF emulation code in the generic linker to
1061 tell the backend linker what file name to use for the DT_NEEDED
1062 entry for a dynamic object. The generic linker passes name as an
1063 empty string to indicate that no DT_NEEDED entry should be made. */
1066 bfd_elf_set_dt_needed_name (abfd
, name
)
1070 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1071 && bfd_get_format (abfd
) == bfd_object
)
1072 elf_dt_name (abfd
) = name
;
1076 bfd_elf_set_dt_needed_soname (abfd
, name
)
1080 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1081 && bfd_get_format (abfd
) == bfd_object
)
1082 elf_dt_soname (abfd
) = name
;
1085 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1086 the linker ELF emulation code. */
1088 struct bfd_link_needed_list
*
1089 bfd_elf_get_needed_list (abfd
, info
)
1090 bfd
*abfd ATTRIBUTE_UNUSED
;
1091 struct bfd_link_info
*info
;
1093 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1095 return elf_hash_table (info
)->needed
;
1098 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1099 hook for the linker ELF emulation code. */
1101 struct bfd_link_needed_list
*
1102 bfd_elf_get_runpath_list (abfd
, info
)
1103 bfd
*abfd ATTRIBUTE_UNUSED
;
1104 struct bfd_link_info
*info
;
1106 if (info
->hash
->creator
->flavour
!= bfd_target_elf_flavour
)
1108 return elf_hash_table (info
)->runpath
;
1111 /* Get the name actually used for a dynamic object for a link. This
1112 is the SONAME entry if there is one. Otherwise, it is the string
1113 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1116 bfd_elf_get_dt_soname (abfd
)
1119 if (bfd_get_flavour (abfd
) == bfd_target_elf_flavour
1120 && bfd_get_format (abfd
) == bfd_object
)
1121 return elf_dt_name (abfd
);
1125 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1126 the ELF linker emulation code. */
1129 bfd_elf_get_bfd_needed_list (abfd
, pneeded
)
1131 struct bfd_link_needed_list
**pneeded
;
1134 bfd_byte
*dynbuf
= NULL
;
1137 bfd_byte
*extdyn
, *extdynend
;
1139 void (*swap_dyn_in
) PARAMS ((bfd
*, const PTR
, Elf_Internal_Dyn
*));
1143 if (bfd_get_flavour (abfd
) != bfd_target_elf_flavour
1144 || bfd_get_format (abfd
) != bfd_object
)
1147 s
= bfd_get_section_by_name (abfd
, ".dynamic");
1148 if (s
== NULL
|| s
->_raw_size
== 0)
1151 dynbuf
= (bfd_byte
*) bfd_malloc (s
->_raw_size
);
1155 if (! bfd_get_section_contents (abfd
, s
, (PTR
) dynbuf
, (file_ptr
) 0,
1159 elfsec
= _bfd_elf_section_from_bfd_section (abfd
, s
);
1163 link
= elf_elfsections (abfd
)[elfsec
]->sh_link
;
1165 extdynsize
= get_elf_backend_data (abfd
)->s
->sizeof_dyn
;
1166 swap_dyn_in
= get_elf_backend_data (abfd
)->s
->swap_dyn_in
;
1169 extdynend
= extdyn
+ s
->_raw_size
;
1170 for (; extdyn
< extdynend
; extdyn
+= extdynsize
)
1172 Elf_Internal_Dyn dyn
;
1174 (*swap_dyn_in
) (abfd
, (PTR
) extdyn
, &dyn
);
1176 if (dyn
.d_tag
== DT_NULL
)
1179 if (dyn
.d_tag
== DT_NEEDED
)
1182 struct bfd_link_needed_list
*l
;
1184 string
= bfd_elf_string_from_elf_section (abfd
, link
,
1189 l
= (struct bfd_link_needed_list
*) bfd_alloc (abfd
, sizeof *l
);
1210 /* Allocate an ELF string table--force the first byte to be zero. */
1212 struct bfd_strtab_hash
*
1213 _bfd_elf_stringtab_init ()
1215 struct bfd_strtab_hash
*ret
;
1217 ret
= _bfd_stringtab_init ();
1222 loc
= _bfd_stringtab_add (ret
, "", true, false);
1223 BFD_ASSERT (loc
== 0 || loc
== (bfd_size_type
) -1);
1224 if (loc
== (bfd_size_type
) -1)
1226 _bfd_stringtab_free (ret
);
1233 /* ELF .o/exec file reading */
1235 /* Create a new bfd section from an ELF section header. */
1238 bfd_section_from_shdr (abfd
, shindex
)
1240 unsigned int shindex
;
1242 Elf_Internal_Shdr
*hdr
= elf_elfsections (abfd
)[shindex
];
1243 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (abfd
);
1244 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1247 name
= elf_string_from_elf_strtab (abfd
, hdr
->sh_name
);
1249 switch (hdr
->sh_type
)
1252 /* Inactive section. Throw it away. */
1255 case SHT_PROGBITS
: /* Normal section with contents. */
1256 case SHT_DYNAMIC
: /* Dynamic linking information. */
1257 case SHT_NOBITS
: /* .bss section. */
1258 case SHT_HASH
: /* .hash section. */
1259 case SHT_NOTE
: /* .note section. */
1260 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1262 case SHT_SYMTAB
: /* A symbol table */
1263 if (elf_onesymtab (abfd
) == shindex
)
1266 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1267 BFD_ASSERT (elf_onesymtab (abfd
) == 0);
1268 elf_onesymtab (abfd
) = shindex
;
1269 elf_tdata (abfd
)->symtab_hdr
= *hdr
;
1270 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1271 abfd
->flags
|= HAS_SYMS
;
1273 /* Sometimes a shared object will map in the symbol table. If
1274 SHF_ALLOC is set, and this is a shared object, then we also
1275 treat this section as a BFD section. We can not base the
1276 decision purely on SHF_ALLOC, because that flag is sometimes
1277 set in a relocateable object file, which would confuse the
1279 if ((hdr
->sh_flags
& SHF_ALLOC
) != 0
1280 && (abfd
->flags
& DYNAMIC
) != 0
1281 && ! _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
))
1286 case SHT_DYNSYM
: /* A dynamic symbol table */
1287 if (elf_dynsymtab (abfd
) == shindex
)
1290 BFD_ASSERT (hdr
->sh_entsize
== bed
->s
->sizeof_sym
);
1291 BFD_ASSERT (elf_dynsymtab (abfd
) == 0);
1292 elf_dynsymtab (abfd
) = shindex
;
1293 elf_tdata (abfd
)->dynsymtab_hdr
= *hdr
;
1294 elf_elfsections (abfd
)[shindex
] = hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
1295 abfd
->flags
|= HAS_SYMS
;
1297 /* Besides being a symbol table, we also treat this as a regular
1298 section, so that objcopy can handle it. */
1299 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1301 case SHT_STRTAB
: /* A string table */
1302 if (hdr
->bfd_section
!= NULL
)
1304 if (ehdr
->e_shstrndx
== shindex
)
1306 elf_tdata (abfd
)->shstrtab_hdr
= *hdr
;
1307 elf_elfsections (abfd
)[shindex
] = &elf_tdata (abfd
)->shstrtab_hdr
;
1313 for (i
= 1; i
< ehdr
->e_shnum
; i
++)
1315 Elf_Internal_Shdr
*hdr2
= elf_elfsections (abfd
)[i
];
1316 if (hdr2
->sh_link
== shindex
)
1318 if (! bfd_section_from_shdr (abfd
, i
))
1320 if (elf_onesymtab (abfd
) == i
)
1322 elf_tdata (abfd
)->strtab_hdr
= *hdr
;
1323 elf_elfsections (abfd
)[shindex
] =
1324 &elf_tdata (abfd
)->strtab_hdr
;
1327 if (elf_dynsymtab (abfd
) == i
)
1329 elf_tdata (abfd
)->dynstrtab_hdr
= *hdr
;
1330 elf_elfsections (abfd
)[shindex
] = hdr
=
1331 &elf_tdata (abfd
)->dynstrtab_hdr
;
1332 /* We also treat this as a regular section, so
1333 that objcopy can handle it. */
1336 #if 0 /* Not handling other string tables specially right now. */
1337 hdr2
= elf_elfsections (abfd
)[i
]; /* in case it moved */
1338 /* We have a strtab for some random other section. */
1339 newsect
= (asection
*) hdr2
->bfd_section
;
1342 hdr
->bfd_section
= newsect
;
1343 hdr2
= &elf_section_data (newsect
)->str_hdr
;
1345 elf_elfsections (abfd
)[shindex
] = hdr2
;
1351 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1355 /* *These* do a lot of work -- but build no sections! */
1357 asection
*target_sect
;
1358 Elf_Internal_Shdr
*hdr2
;
1360 /* Check for a bogus link to avoid crashing. */
1361 if (hdr
->sh_link
>= ehdr
->e_shnum
)
1363 ((*_bfd_error_handler
)
1364 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1365 bfd_get_filename (abfd
), hdr
->sh_link
, name
, shindex
));
1366 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1369 /* For some incomprehensible reason Oracle distributes
1370 libraries for Solaris in which some of the objects have
1371 bogus sh_link fields. It would be nice if we could just
1372 reject them, but, unfortunately, some people need to use
1373 them. We scan through the section headers; if we find only
1374 one suitable symbol table, we clobber the sh_link to point
1375 to it. I hope this doesn't break anything. */
1376 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_SYMTAB
1377 && elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
!= SHT_DYNSYM
)
1383 for (scan
= 1; scan
< ehdr
->e_shnum
; scan
++)
1385 if (elf_elfsections (abfd
)[scan
]->sh_type
== SHT_SYMTAB
1386 || elf_elfsections (abfd
)[scan
]->sh_type
== SHT_DYNSYM
)
1397 hdr
->sh_link
= found
;
1400 /* Get the symbol table. */
1401 if (elf_elfsections (abfd
)[hdr
->sh_link
]->sh_type
== SHT_SYMTAB
1402 && ! bfd_section_from_shdr (abfd
, hdr
->sh_link
))
1405 /* If this reloc section does not use the main symbol table we
1406 don't treat it as a reloc section. BFD can't adequately
1407 represent such a section, so at least for now, we don't
1408 try. We just present it as a normal section. We also
1409 can't use it as a reloc section if it points to the null
1411 if (hdr
->sh_link
!= elf_onesymtab (abfd
) || hdr
->sh_info
== SHN_UNDEF
)
1412 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1414 if (! bfd_section_from_shdr (abfd
, hdr
->sh_info
))
1416 target_sect
= bfd_section_from_elf_index (abfd
, hdr
->sh_info
);
1417 if (target_sect
== NULL
)
1420 if ((target_sect
->flags
& SEC_RELOC
) == 0
1421 || target_sect
->reloc_count
== 0)
1422 hdr2
= &elf_section_data (target_sect
)->rel_hdr
;
1425 BFD_ASSERT (elf_section_data (target_sect
)->rel_hdr2
== NULL
);
1426 hdr2
= (Elf_Internal_Shdr
*) bfd_alloc (abfd
, sizeof (*hdr2
));
1427 elf_section_data (target_sect
)->rel_hdr2
= hdr2
;
1430 elf_elfsections (abfd
)[shindex
] = hdr2
;
1431 target_sect
->reloc_count
+= hdr
->sh_size
/ hdr
->sh_entsize
;
1432 target_sect
->flags
|= SEC_RELOC
;
1433 target_sect
->relocation
= NULL
;
1434 target_sect
->rel_filepos
= hdr
->sh_offset
;
1435 /* In the section to which the relocations apply, mark whether
1436 its relocations are of the REL or RELA variety. */
1437 if (hdr
->sh_size
!= 0)
1438 elf_section_data (target_sect
)->use_rela_p
1439 = (hdr
->sh_type
== SHT_RELA
);
1440 abfd
->flags
|= HAS_RELOC
;
1445 case SHT_GNU_verdef
:
1446 elf_dynverdef (abfd
) = shindex
;
1447 elf_tdata (abfd
)->dynverdef_hdr
= *hdr
;
1448 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1451 case SHT_GNU_versym
:
1452 elf_dynversym (abfd
) = shindex
;
1453 elf_tdata (abfd
)->dynversym_hdr
= *hdr
;
1454 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1457 case SHT_GNU_verneed
:
1458 elf_dynverref (abfd
) = shindex
;
1459 elf_tdata (abfd
)->dynverref_hdr
= *hdr
;
1460 return _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
);
1467 /* Check for any processor-specific section types. */
1469 if (bed
->elf_backend_section_from_shdr
)
1470 (*bed
->elf_backend_section_from_shdr
) (abfd
, hdr
, name
);
1478 /* Given an ELF section number, retrieve the corresponding BFD
1482 bfd_section_from_elf_index (abfd
, index
)
1486 BFD_ASSERT (index
> 0 && index
< SHN_LORESERVE
);
1487 if (index
>= elf_elfheader (abfd
)->e_shnum
)
1489 return elf_elfsections (abfd
)[index
]->bfd_section
;
1493 _bfd_elf_new_section_hook (abfd
, sec
)
1497 struct bfd_elf_section_data
*sdata
;
1499 sdata
= (struct bfd_elf_section_data
*) bfd_zalloc (abfd
, sizeof (*sdata
));
1502 sec
->used_by_bfd
= (PTR
) sdata
;
1504 /* Indicate whether or not this section should use RELA relocations. */
1506 = get_elf_backend_data (abfd
)->default_use_rela_p
;
1511 /* Create a new bfd section from an ELF program header.
1513 Since program segments have no names, we generate a synthetic name
1514 of the form segment<NUM>, where NUM is generally the index in the
1515 program header table. For segments that are split (see below) we
1516 generate the names segment<NUM>a and segment<NUM>b.
1518 Note that some program segments may have a file size that is different than
1519 (less than) the memory size. All this means is that at execution the
1520 system must allocate the amount of memory specified by the memory size,
1521 but only initialize it with the first "file size" bytes read from the
1522 file. This would occur for example, with program segments consisting
1523 of combined data+bss.
1525 To handle the above situation, this routine generates TWO bfd sections
1526 for the single program segment. The first has the length specified by
1527 the file size of the segment, and the second has the length specified
1528 by the difference between the two sizes. In effect, the segment is split
1529 into it's initialized and uninitialized parts.
1534 _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, typename
)
1536 Elf_Internal_Phdr
*hdr
;
1538 const char *typename
;
1545 split
= ((hdr
->p_memsz
> 0)
1546 && (hdr
->p_filesz
> 0)
1547 && (hdr
->p_memsz
> hdr
->p_filesz
));
1548 sprintf (namebuf
, "%s%d%s", typename
, index
, split
? "a" : "");
1549 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1552 strcpy (name
, namebuf
);
1553 newsect
= bfd_make_section (abfd
, name
);
1554 if (newsect
== NULL
)
1556 newsect
->vma
= hdr
->p_vaddr
;
1557 newsect
->lma
= hdr
->p_paddr
;
1558 newsect
->_raw_size
= hdr
->p_filesz
;
1559 newsect
->filepos
= hdr
->p_offset
;
1560 newsect
->flags
|= SEC_HAS_CONTENTS
;
1561 if (hdr
->p_type
== PT_LOAD
)
1563 newsect
->flags
|= SEC_ALLOC
;
1564 newsect
->flags
|= SEC_LOAD
;
1565 if (hdr
->p_flags
& PF_X
)
1567 /* FIXME: all we known is that it has execute PERMISSION,
1569 newsect
->flags
|= SEC_CODE
;
1572 if (!(hdr
->p_flags
& PF_W
))
1574 newsect
->flags
|= SEC_READONLY
;
1579 sprintf (namebuf
, "%s%db", typename
, index
);
1580 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
1583 strcpy (name
, namebuf
);
1584 newsect
= bfd_make_section (abfd
, name
);
1585 if (newsect
== NULL
)
1587 newsect
->vma
= hdr
->p_vaddr
+ hdr
->p_filesz
;
1588 newsect
->lma
= hdr
->p_paddr
+ hdr
->p_filesz
;
1589 newsect
->_raw_size
= hdr
->p_memsz
- hdr
->p_filesz
;
1590 if (hdr
->p_type
== PT_LOAD
)
1592 newsect
->flags
|= SEC_ALLOC
;
1593 if (hdr
->p_flags
& PF_X
)
1594 newsect
->flags
|= SEC_CODE
;
1596 if (!(hdr
->p_flags
& PF_W
))
1597 newsect
->flags
|= SEC_READONLY
;
1604 bfd_section_from_phdr (abfd
, hdr
, index
)
1606 Elf_Internal_Phdr
*hdr
;
1609 struct elf_backend_data
*bed
;
1611 switch (hdr
->p_type
)
1614 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "null");
1617 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "load");
1620 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "dynamic");
1623 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "interp");
1626 if (! _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "note"))
1628 if (! elfcore_read_notes (abfd
, hdr
->p_offset
, hdr
->p_filesz
))
1633 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "shlib");
1636 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "phdr");
1639 /* Check for any processor-specific program segment types.
1640 If no handler for them, default to making "segment" sections. */
1641 bed
= get_elf_backend_data (abfd
);
1642 if (bed
->elf_backend_section_from_phdr
)
1643 return (*bed
->elf_backend_section_from_phdr
) (abfd
, hdr
, index
);
1645 return _bfd_elf_make_section_from_phdr (abfd
, hdr
, index
, "segment");
1649 /* Initialize REL_HDR, the section-header for new section, containing
1650 relocations against ASECT. If USE_RELA_P is true, we use RELA
1651 relocations; otherwise, we use REL relocations. */
1654 _bfd_elf_init_reloc_shdr (abfd
, rel_hdr
, asect
, use_rela_p
)
1656 Elf_Internal_Shdr
*rel_hdr
;
1661 struct elf_backend_data
*bed
;
1663 bed
= get_elf_backend_data (abfd
);
1664 name
= bfd_alloc (abfd
, sizeof ".rela" + strlen (asect
->name
));
1667 sprintf (name
, "%s%s", use_rela_p
? ".rela" : ".rel", asect
->name
);
1669 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd
), name
,
1671 if (rel_hdr
->sh_name
== (unsigned int) -1)
1673 rel_hdr
->sh_type
= use_rela_p
? SHT_RELA
: SHT_REL
;
1674 rel_hdr
->sh_entsize
= (use_rela_p
1675 ? bed
->s
->sizeof_rela
1676 : bed
->s
->sizeof_rel
);
1677 rel_hdr
->sh_addralign
= bed
->s
->file_align
;
1678 rel_hdr
->sh_flags
= 0;
1679 rel_hdr
->sh_addr
= 0;
1680 rel_hdr
->sh_size
= 0;
1681 rel_hdr
->sh_offset
= 0;
1686 /* Set up an ELF internal section header for a section. */
1689 elf_fake_sections (abfd
, asect
, failedptrarg
)
1694 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
1695 boolean
*failedptr
= (boolean
*) failedptrarg
;
1696 Elf_Internal_Shdr
*this_hdr
;
1700 /* We already failed; just get out of the bfd_map_over_sections
1705 this_hdr
= &elf_section_data (asect
)->this_hdr
;
1707 this_hdr
->sh_name
= (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd
),
1710 if (this_hdr
->sh_name
== (unsigned long) -1)
1716 this_hdr
->sh_flags
= 0;
1718 if ((asect
->flags
& SEC_ALLOC
) != 0
1719 || asect
->user_set_vma
)
1720 this_hdr
->sh_addr
= asect
->vma
;
1722 this_hdr
->sh_addr
= 0;
1724 this_hdr
->sh_offset
= 0;
1725 this_hdr
->sh_size
= asect
->_raw_size
;
1726 this_hdr
->sh_link
= 0;
1727 this_hdr
->sh_addralign
= 1 << asect
->alignment_power
;
1728 /* The sh_entsize and sh_info fields may have been set already by
1729 copy_private_section_data. */
1731 this_hdr
->bfd_section
= asect
;
1732 this_hdr
->contents
= NULL
;
1734 /* FIXME: This should not be based on section names. */
1735 if (strcmp (asect
->name
, ".dynstr") == 0)
1736 this_hdr
->sh_type
= SHT_STRTAB
;
1737 else if (strcmp (asect
->name
, ".hash") == 0)
1739 this_hdr
->sh_type
= SHT_HASH
;
1740 this_hdr
->sh_entsize
= bed
->s
->sizeof_hash_entry
;
1742 else if (strcmp (asect
->name
, ".dynsym") == 0)
1744 this_hdr
->sh_type
= SHT_DYNSYM
;
1745 this_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
1747 else if (strcmp (asect
->name
, ".dynamic") == 0)
1749 this_hdr
->sh_type
= SHT_DYNAMIC
;
1750 this_hdr
->sh_entsize
= bed
->s
->sizeof_dyn
;
1752 else if (strncmp (asect
->name
, ".rela", 5) == 0
1753 && get_elf_backend_data (abfd
)->may_use_rela_p
)
1755 this_hdr
->sh_type
= SHT_RELA
;
1756 this_hdr
->sh_entsize
= bed
->s
->sizeof_rela
;
1758 else if (strncmp (asect
->name
, ".rel", 4) == 0
1759 && get_elf_backend_data (abfd
)->may_use_rel_p
)
1761 this_hdr
->sh_type
= SHT_REL
;
1762 this_hdr
->sh_entsize
= bed
->s
->sizeof_rel
;
1764 else if (strncmp (asect
->name
, ".note", 5) == 0)
1765 this_hdr
->sh_type
= SHT_NOTE
;
1766 else if (strncmp (asect
->name
, ".stab", 5) == 0
1767 && strcmp (asect
->name
+ strlen (asect
->name
) - 3, "str") == 0)
1768 this_hdr
->sh_type
= SHT_STRTAB
;
1769 else if (strcmp (asect
->name
, ".gnu.version") == 0)
1771 this_hdr
->sh_type
= SHT_GNU_versym
;
1772 this_hdr
->sh_entsize
= sizeof (Elf_External_Versym
);
1774 else if (strcmp (asect
->name
, ".gnu.version_d") == 0)
1776 this_hdr
->sh_type
= SHT_GNU_verdef
;
1777 this_hdr
->sh_entsize
= 0;
1778 /* objcopy or strip will copy over sh_info, but may not set
1779 cverdefs. The linker will set cverdefs, but sh_info will be
1781 if (this_hdr
->sh_info
== 0)
1782 this_hdr
->sh_info
= elf_tdata (abfd
)->cverdefs
;
1784 BFD_ASSERT (elf_tdata (abfd
)->cverdefs
== 0
1785 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverdefs
);
1787 else if (strcmp (asect
->name
, ".gnu.version_r") == 0)
1789 this_hdr
->sh_type
= SHT_GNU_verneed
;
1790 this_hdr
->sh_entsize
= 0;
1791 /* objcopy or strip will copy over sh_info, but may not set
1792 cverrefs. The linker will set cverrefs, but sh_info will be
1794 if (this_hdr
->sh_info
== 0)
1795 this_hdr
->sh_info
= elf_tdata (abfd
)->cverrefs
;
1797 BFD_ASSERT (elf_tdata (abfd
)->cverrefs
== 0
1798 || this_hdr
->sh_info
== elf_tdata (abfd
)->cverrefs
);
1800 else if ((asect
->flags
& SEC_ALLOC
) != 0
1801 && ((asect
->flags
& (SEC_LOAD
| SEC_HAS_CONTENTS
)) == 0))
1802 this_hdr
->sh_type
= SHT_NOBITS
;
1804 this_hdr
->sh_type
= SHT_PROGBITS
;
1806 if ((asect
->flags
& SEC_ALLOC
) != 0)
1807 this_hdr
->sh_flags
|= SHF_ALLOC
;
1808 if ((asect
->flags
& SEC_READONLY
) == 0)
1809 this_hdr
->sh_flags
|= SHF_WRITE
;
1810 if ((asect
->flags
& SEC_CODE
) != 0)
1811 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1813 /* Check for processor-specific section types. */
1814 if (bed
->elf_backend_fake_sections
)
1815 (*bed
->elf_backend_fake_sections
) (abfd
, this_hdr
, asect
);
1817 /* If the section has relocs, set up a section header for the
1818 SHT_REL[A] section. If two relocation sections are required for
1819 this section, it is up to the processor-specific back-end to
1820 create the other. */
1821 if ((asect
->flags
& SEC_RELOC
) != 0
1822 && !_bfd_elf_init_reloc_shdr (abfd
,
1823 &elf_section_data (asect
)->rel_hdr
,
1825 elf_section_data (asect
)->use_rela_p
))
1829 /* Assign all ELF section numbers. The dummy first section is handled here
1830 too. The link/info pointers for the standard section types are filled
1831 in here too, while we're at it. */
1834 assign_section_numbers (abfd
)
1837 struct elf_obj_tdata
*t
= elf_tdata (abfd
);
1839 unsigned int section_number
;
1840 Elf_Internal_Shdr
**i_shdrp
;
1844 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1846 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1848 d
->this_idx
= section_number
++;
1849 if ((sec
->flags
& SEC_RELOC
) == 0)
1852 d
->rel_idx
= section_number
++;
1855 d
->rel_idx2
= section_number
++;
1860 t
->shstrtab_section
= section_number
++;
1861 elf_elfheader (abfd
)->e_shstrndx
= t
->shstrtab_section
;
1862 t
->shstrtab_hdr
.sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
1864 if (bfd_get_symcount (abfd
) > 0)
1866 t
->symtab_section
= section_number
++;
1867 t
->strtab_section
= section_number
++;
1870 elf_elfheader (abfd
)->e_shnum
= section_number
;
1872 /* Set up the list of section header pointers, in agreement with the
1874 i_shdrp
= ((Elf_Internal_Shdr
**)
1875 bfd_alloc (abfd
, section_number
* sizeof (Elf_Internal_Shdr
*)));
1876 if (i_shdrp
== NULL
)
1879 i_shdrp
[0] = ((Elf_Internal_Shdr
*)
1880 bfd_alloc (abfd
, sizeof (Elf_Internal_Shdr
)));
1881 if (i_shdrp
[0] == NULL
)
1883 bfd_release (abfd
, i_shdrp
);
1886 memset (i_shdrp
[0], 0, sizeof (Elf_Internal_Shdr
));
1888 elf_elfsections (abfd
) = i_shdrp
;
1890 i_shdrp
[t
->shstrtab_section
] = &t
->shstrtab_hdr
;
1891 if (bfd_get_symcount (abfd
) > 0)
1893 i_shdrp
[t
->symtab_section
] = &t
->symtab_hdr
;
1894 i_shdrp
[t
->strtab_section
] = &t
->strtab_hdr
;
1895 t
->symtab_hdr
.sh_link
= t
->strtab_section
;
1897 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
1899 struct bfd_elf_section_data
*d
= elf_section_data (sec
);
1903 i_shdrp
[d
->this_idx
] = &d
->this_hdr
;
1904 if (d
->rel_idx
!= 0)
1905 i_shdrp
[d
->rel_idx
] = &d
->rel_hdr
;
1906 if (d
->rel_idx2
!= 0)
1907 i_shdrp
[d
->rel_idx2
] = d
->rel_hdr2
;
1909 /* Fill in the sh_link and sh_info fields while we're at it. */
1911 /* sh_link of a reloc section is the section index of the symbol
1912 table. sh_info is the section index of the section to which
1913 the relocation entries apply. */
1914 if (d
->rel_idx
!= 0)
1916 d
->rel_hdr
.sh_link
= t
->symtab_section
;
1917 d
->rel_hdr
.sh_info
= d
->this_idx
;
1919 if (d
->rel_idx2
!= 0)
1921 d
->rel_hdr2
->sh_link
= t
->symtab_section
;
1922 d
->rel_hdr2
->sh_info
= d
->this_idx
;
1925 switch (d
->this_hdr
.sh_type
)
1929 /* A reloc section which we are treating as a normal BFD
1930 section. sh_link is the section index of the symbol
1931 table. sh_info is the section index of the section to
1932 which the relocation entries apply. We assume that an
1933 allocated reloc section uses the dynamic symbol table.
1934 FIXME: How can we be sure? */
1935 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1937 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1939 /* We look up the section the relocs apply to by name. */
1941 if (d
->this_hdr
.sh_type
== SHT_REL
)
1945 s
= bfd_get_section_by_name (abfd
, name
);
1947 d
->this_hdr
.sh_info
= elf_section_data (s
)->this_idx
;
1951 /* We assume that a section named .stab*str is a stabs
1952 string section. We look for a section with the same name
1953 but without the trailing ``str'', and set its sh_link
1954 field to point to this section. */
1955 if (strncmp (sec
->name
, ".stab", sizeof ".stab" - 1) == 0
1956 && strcmp (sec
->name
+ strlen (sec
->name
) - 3, "str") == 0)
1961 len
= strlen (sec
->name
);
1962 alc
= (char *) bfd_malloc (len
- 2);
1965 strncpy (alc
, sec
->name
, len
- 3);
1966 alc
[len
- 3] = '\0';
1967 s
= bfd_get_section_by_name (abfd
, alc
);
1971 elf_section_data (s
)->this_hdr
.sh_link
= d
->this_idx
;
1973 /* This is a .stab section. */
1974 elf_section_data (s
)->this_hdr
.sh_entsize
=
1975 4 + 2 * bfd_get_arch_size (abfd
) / 8;
1982 case SHT_GNU_verneed
:
1983 case SHT_GNU_verdef
:
1984 /* sh_link is the section header index of the string table
1985 used for the dynamic entries, or the symbol table, or the
1987 s
= bfd_get_section_by_name (abfd
, ".dynstr");
1989 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
1993 case SHT_GNU_versym
:
1994 /* sh_link is the section header index of the symbol table
1995 this hash table or version table is for. */
1996 s
= bfd_get_section_by_name (abfd
, ".dynsym");
1998 d
->this_hdr
.sh_link
= elf_section_data (s
)->this_idx
;
2006 /* Map symbol from it's internal number to the external number, moving
2007 all local symbols to be at the head of the list. */
2010 sym_is_global (abfd
, sym
)
2014 /* If the backend has a special mapping, use it. */
2015 if (get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2016 return ((*get_elf_backend_data (abfd
)->elf_backend_sym_is_global
)
2019 return ((sym
->flags
& (BSF_GLOBAL
| BSF_WEAK
)) != 0
2020 || bfd_is_und_section (bfd_get_section (sym
))
2021 || bfd_is_com_section (bfd_get_section (sym
)));
2025 elf_map_symbols (abfd
)
2028 int symcount
= bfd_get_symcount (abfd
);
2029 asymbol
**syms
= bfd_get_outsymbols (abfd
);
2030 asymbol
**sect_syms
;
2032 int num_globals
= 0;
2033 int num_locals2
= 0;
2034 int num_globals2
= 0;
2036 int num_sections
= 0;
2043 fprintf (stderr
, "elf_map_symbols\n");
2047 /* Add a section symbol for each BFD section. FIXME: Is this really
2049 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2051 if (max_index
< asect
->index
)
2052 max_index
= asect
->index
;
2056 sect_syms
= (asymbol
**) bfd_zalloc (abfd
, max_index
* sizeof (asymbol
*));
2057 if (sect_syms
== NULL
)
2059 elf_section_syms (abfd
) = sect_syms
;
2061 for (idx
= 0; idx
< symcount
; idx
++)
2065 if ((sym
->flags
& BSF_SECTION_SYM
) != 0
2072 if (sec
->owner
!= NULL
)
2074 if (sec
->owner
!= abfd
)
2076 if (sec
->output_offset
!= 0)
2079 sec
= sec
->output_section
;
2081 /* Empty sections in the input files may have had a section
2082 symbol created for them. (See the comment near the end of
2083 _bfd_generic_link_output_symbols in linker.c). If the linker
2084 script discards such sections then we will reach this point.
2085 Since we know that we cannot avoid this case, we detect it
2086 and skip the abort and the assignment to the sect_syms array.
2087 To reproduce this particular case try running the linker
2088 testsuite test ld-scripts/weak.exp for an ELF port that uses
2089 the generic linker. */
2090 if (sec
->owner
== NULL
)
2093 BFD_ASSERT (sec
->owner
== abfd
);
2095 sect_syms
[sec
->index
] = syms
[idx
];
2100 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2102 if (sect_syms
[asect
->index
] != NULL
)
2105 sym
= bfd_make_empty_symbol (abfd
);
2108 sym
->the_bfd
= abfd
;
2109 sym
->name
= asect
->name
;
2111 /* Set the flags to 0 to indicate that this one was newly added. */
2113 sym
->section
= asect
;
2114 sect_syms
[asect
->index
] = sym
;
2118 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2119 asect
->name
, (long) asect
->vma
, asect
->index
, (long) asect
);
2123 /* Classify all of the symbols. */
2124 for (idx
= 0; idx
< symcount
; idx
++)
2126 if (!sym_is_global (abfd
, syms
[idx
]))
2131 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2133 if (sect_syms
[asect
->index
] != NULL
2134 && sect_syms
[asect
->index
]->flags
== 0)
2136 sect_syms
[asect
->index
]->flags
= BSF_SECTION_SYM
;
2137 if (!sym_is_global (abfd
, sect_syms
[asect
->index
]))
2141 sect_syms
[asect
->index
]->flags
= 0;
2145 /* Now sort the symbols so the local symbols are first. */
2146 new_syms
= ((asymbol
**)
2148 (num_locals
+ num_globals
) * sizeof (asymbol
*)));
2149 if (new_syms
== NULL
)
2152 for (idx
= 0; idx
< symcount
; idx
++)
2154 asymbol
*sym
= syms
[idx
];
2157 if (!sym_is_global (abfd
, sym
))
2160 i
= num_locals
+ num_globals2
++;
2162 sym
->udata
.i
= i
+ 1;
2164 for (asect
= abfd
->sections
; asect
; asect
= asect
->next
)
2166 if (sect_syms
[asect
->index
] != NULL
2167 && sect_syms
[asect
->index
]->flags
== 0)
2169 asymbol
*sym
= sect_syms
[asect
->index
];
2172 sym
->flags
= BSF_SECTION_SYM
;
2173 if (!sym_is_global (abfd
, sym
))
2176 i
= num_locals
+ num_globals2
++;
2178 sym
->udata
.i
= i
+ 1;
2182 bfd_set_symtab (abfd
, new_syms
, num_locals
+ num_globals
);
2184 elf_num_locals (abfd
) = num_locals
;
2185 elf_num_globals (abfd
) = num_globals
;
2189 /* Align to the maximum file alignment that could be required for any
2190 ELF data structure. */
2192 static INLINE file_ptr align_file_position
PARAMS ((file_ptr
, int));
2193 static INLINE file_ptr
2194 align_file_position (off
, align
)
2198 return (off
+ align
- 1) & ~(align
- 1);
2201 /* Assign a file position to a section, optionally aligning to the
2202 required section alignment. */
2205 _bfd_elf_assign_file_position_for_section (i_shdrp
, offset
, align
)
2206 Elf_Internal_Shdr
*i_shdrp
;
2214 al
= i_shdrp
->sh_addralign
;
2216 offset
= BFD_ALIGN (offset
, al
);
2218 i_shdrp
->sh_offset
= offset
;
2219 if (i_shdrp
->bfd_section
!= NULL
)
2220 i_shdrp
->bfd_section
->filepos
= offset
;
2221 if (i_shdrp
->sh_type
!= SHT_NOBITS
)
2222 offset
+= i_shdrp
->sh_size
;
2226 /* Compute the file positions we are going to put the sections at, and
2227 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2228 is not NULL, this is being called by the ELF backend linker. */
2231 _bfd_elf_compute_section_file_positions (abfd
, link_info
)
2233 struct bfd_link_info
*link_info
;
2235 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2237 struct bfd_strtab_hash
*strtab
;
2238 Elf_Internal_Shdr
*shstrtab_hdr
;
2240 if (abfd
->output_has_begun
)
2243 /* Do any elf backend specific processing first. */
2244 if (bed
->elf_backend_begin_write_processing
)
2245 (*bed
->elf_backend_begin_write_processing
) (abfd
, link_info
);
2247 if (! prep_headers (abfd
))
2250 /* Post process the headers if necessary. */
2251 if (bed
->elf_backend_post_process_headers
)
2252 (*bed
->elf_backend_post_process_headers
) (abfd
, link_info
);
2255 bfd_map_over_sections (abfd
, elf_fake_sections
, &failed
);
2259 if (!assign_section_numbers (abfd
))
2262 /* The backend linker builds symbol table information itself. */
2263 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2265 /* Non-zero if doing a relocatable link. */
2266 int relocatable_p
= ! (abfd
->flags
& (EXEC_P
| DYNAMIC
));
2268 if (! swap_out_syms (abfd
, &strtab
, relocatable_p
))
2272 shstrtab_hdr
= &elf_tdata (abfd
)->shstrtab_hdr
;
2273 /* sh_name was set in prep_headers. */
2274 shstrtab_hdr
->sh_type
= SHT_STRTAB
;
2275 shstrtab_hdr
->sh_flags
= 0;
2276 shstrtab_hdr
->sh_addr
= 0;
2277 shstrtab_hdr
->sh_size
= _bfd_stringtab_size (elf_shstrtab (abfd
));
2278 shstrtab_hdr
->sh_entsize
= 0;
2279 shstrtab_hdr
->sh_link
= 0;
2280 shstrtab_hdr
->sh_info
= 0;
2281 /* sh_offset is set in assign_file_positions_except_relocs. */
2282 shstrtab_hdr
->sh_addralign
= 1;
2284 if (!assign_file_positions_except_relocs (abfd
))
2287 if (link_info
== NULL
&& bfd_get_symcount (abfd
) > 0)
2290 Elf_Internal_Shdr
*hdr
;
2292 off
= elf_tdata (abfd
)->next_file_pos
;
2294 hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2295 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2297 hdr
= &elf_tdata (abfd
)->strtab_hdr
;
2298 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
2300 elf_tdata (abfd
)->next_file_pos
= off
;
2302 /* Now that we know where the .strtab section goes, write it
2304 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
2305 || ! _bfd_stringtab_emit (abfd
, strtab
))
2307 _bfd_stringtab_free (strtab
);
2310 abfd
->output_has_begun
= true;
2315 /* Create a mapping from a set of sections to a program segment. */
2317 static INLINE
struct elf_segment_map
*
2318 make_mapping (abfd
, sections
, from
, to
, phdr
)
2320 asection
**sections
;
2325 struct elf_segment_map
*m
;
2329 m
= ((struct elf_segment_map
*)
2331 (sizeof (struct elf_segment_map
)
2332 + (to
- from
- 1) * sizeof (asection
*))));
2336 m
->p_type
= PT_LOAD
;
2337 for (i
= from
, hdrpp
= sections
+ from
; i
< to
; i
++, hdrpp
++)
2338 m
->sections
[i
- from
] = *hdrpp
;
2339 m
->count
= to
- from
;
2341 if (from
== 0 && phdr
)
2343 /* Include the headers in the first PT_LOAD segment. */
2344 m
->includes_filehdr
= 1;
2345 m
->includes_phdrs
= 1;
2351 /* Set up a mapping from BFD sections to program segments. */
2354 map_sections_to_segments (abfd
)
2357 asection
**sections
= NULL
;
2361 struct elf_segment_map
*mfirst
;
2362 struct elf_segment_map
**pm
;
2363 struct elf_segment_map
*m
;
2365 unsigned int phdr_index
;
2366 bfd_vma maxpagesize
;
2368 boolean phdr_in_segment
= true;
2372 if (elf_tdata (abfd
)->segment_map
!= NULL
)
2375 if (bfd_count_sections (abfd
) == 0)
2378 /* Select the allocated sections, and sort them. */
2380 sections
= (asection
**) bfd_malloc (bfd_count_sections (abfd
)
2381 * sizeof (asection
*));
2382 if (sections
== NULL
)
2386 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2388 if ((s
->flags
& SEC_ALLOC
) != 0)
2394 BFD_ASSERT (i
<= bfd_count_sections (abfd
));
2397 qsort (sections
, (size_t) count
, sizeof (asection
*), elf_sort_sections
);
2399 /* Build the mapping. */
2404 /* If we have a .interp section, then create a PT_PHDR segment for
2405 the program headers and a PT_INTERP segment for the .interp
2407 s
= bfd_get_section_by_name (abfd
, ".interp");
2408 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
2410 m
= ((struct elf_segment_map
*)
2411 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2415 m
->p_type
= PT_PHDR
;
2416 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2417 m
->p_flags
= PF_R
| PF_X
;
2418 m
->p_flags_valid
= 1;
2419 m
->includes_phdrs
= 1;
2424 m
= ((struct elf_segment_map
*)
2425 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2429 m
->p_type
= PT_INTERP
;
2437 /* Look through the sections. We put sections in the same program
2438 segment when the start of the second section can be placed within
2439 a few bytes of the end of the first section. */
2442 maxpagesize
= get_elf_backend_data (abfd
)->maxpagesize
;
2444 dynsec
= bfd_get_section_by_name (abfd
, ".dynamic");
2446 && (dynsec
->flags
& SEC_LOAD
) == 0)
2449 /* Deal with -Ttext or something similar such that the first section
2450 is not adjacent to the program headers. This is an
2451 approximation, since at this point we don't know exactly how many
2452 program headers we will need. */
2455 bfd_size_type phdr_size
;
2457 phdr_size
= elf_tdata (abfd
)->program_header_size
;
2459 phdr_size
= get_elf_backend_data (abfd
)->s
->sizeof_phdr
;
2460 if ((abfd
->flags
& D_PAGED
) == 0
2461 || sections
[0]->lma
< phdr_size
2462 || sections
[0]->lma
% maxpagesize
< phdr_size
% maxpagesize
)
2463 phdr_in_segment
= false;
2466 for (i
= 0, hdrpp
= sections
; i
< count
; i
++, hdrpp
++)
2469 boolean new_segment
;
2473 /* See if this section and the last one will fit in the same
2476 if (last_hdr
== NULL
)
2478 /* If we don't have a segment yet, then we don't need a new
2479 one (we build the last one after this loop). */
2480 new_segment
= false;
2482 else if (last_hdr
->lma
- last_hdr
->vma
!= hdr
->lma
- hdr
->vma
)
2484 /* If this section has a different relation between the
2485 virtual address and the load address, then we need a new
2489 else if (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2490 < BFD_ALIGN (hdr
->lma
, maxpagesize
))
2492 /* If putting this section in this segment would force us to
2493 skip a page in the segment, then we need a new segment. */
2496 else if ((last_hdr
->flags
& SEC_LOAD
) == 0
2497 && (hdr
->flags
& SEC_LOAD
) != 0)
2499 /* We don't want to put a loadable section after a
2500 nonloadable section in the same segment. */
2503 else if ((abfd
->flags
& D_PAGED
) == 0)
2505 /* If the file is not demand paged, which means that we
2506 don't require the sections to be correctly aligned in the
2507 file, then there is no other reason for a new segment. */
2508 new_segment
= false;
2511 && (hdr
->flags
& SEC_READONLY
) == 0
2512 && (BFD_ALIGN (last_hdr
->lma
+ last_hdr
->_raw_size
, maxpagesize
)
2515 /* We don't want to put a writable section in a read only
2516 segment, unless they are on the same page in memory
2517 anyhow. We already know that the last section does not
2518 bring us past the current section on the page, so the
2519 only case in which the new section is not on the same
2520 page as the previous section is when the previous section
2521 ends precisely on a page boundary. */
2526 /* Otherwise, we can use the same segment. */
2527 new_segment
= false;
2532 if ((hdr
->flags
& SEC_READONLY
) == 0)
2538 /* We need a new program segment. We must create a new program
2539 header holding all the sections from phdr_index until hdr. */
2541 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2548 if ((hdr
->flags
& SEC_READONLY
) == 0)
2555 phdr_in_segment
= false;
2558 /* Create a final PT_LOAD program segment. */
2559 if (last_hdr
!= NULL
)
2561 m
= make_mapping (abfd
, sections
, phdr_index
, i
, phdr_in_segment
);
2569 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2572 m
= ((struct elf_segment_map
*)
2573 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2577 m
->p_type
= PT_DYNAMIC
;
2579 m
->sections
[0] = dynsec
;
2585 /* For each loadable .note section, add a PT_NOTE segment. We don't
2586 use bfd_get_section_by_name, because if we link together
2587 nonloadable .note sections and loadable .note sections, we will
2588 generate two .note sections in the output file. FIXME: Using
2589 names for section types is bogus anyhow. */
2590 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
2592 if ((s
->flags
& SEC_LOAD
) != 0
2593 && strncmp (s
->name
, ".note", 5) == 0)
2595 m
= ((struct elf_segment_map
*)
2596 bfd_zalloc (abfd
, sizeof (struct elf_segment_map
)));
2600 m
->p_type
= PT_NOTE
;
2612 elf_tdata (abfd
)->segment_map
= mfirst
;
2616 if (sections
!= NULL
)
2621 /* Sort sections by address. */
2624 elf_sort_sections (arg1
, arg2
)
2628 const asection
*sec1
= *(const asection
**) arg1
;
2629 const asection
*sec2
= *(const asection
**) arg2
;
2631 /* Sort by LMA first, since this is the address used to
2632 place the section into a segment. */
2633 if (sec1
->lma
< sec2
->lma
)
2635 else if (sec1
->lma
> sec2
->lma
)
2638 /* Then sort by VMA. Normally the LMA and the VMA will be
2639 the same, and this will do nothing. */
2640 if (sec1
->vma
< sec2
->vma
)
2642 else if (sec1
->vma
> sec2
->vma
)
2645 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2647 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2652 return sec1
->target_index
- sec2
->target_index
;
2662 /* Sort by size, to put zero sized sections before others at the
2665 if (sec1
->_raw_size
< sec2
->_raw_size
)
2667 if (sec1
->_raw_size
> sec2
->_raw_size
)
2670 return sec1
->target_index
- sec2
->target_index
;
2673 /* Assign file positions to the sections based on the mapping from
2674 sections to segments. This function also sets up some fields in
2675 the file header, and writes out the program headers. */
2678 assign_file_positions_for_segments (abfd
)
2681 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
2683 struct elf_segment_map
*m
;
2685 Elf_Internal_Phdr
*phdrs
;
2687 bfd_vma filehdr_vaddr
, filehdr_paddr
;
2688 bfd_vma phdrs_vaddr
, phdrs_paddr
;
2689 Elf_Internal_Phdr
*p
;
2691 if (elf_tdata (abfd
)->segment_map
== NULL
)
2693 if (! map_sections_to_segments (abfd
))
2697 if (bed
->elf_backend_modify_segment_map
)
2699 if (! (*bed
->elf_backend_modify_segment_map
) (abfd
))
2704 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
2707 elf_elfheader (abfd
)->e_phoff
= bed
->s
->sizeof_ehdr
;
2708 elf_elfheader (abfd
)->e_phentsize
= bed
->s
->sizeof_phdr
;
2709 elf_elfheader (abfd
)->e_phnum
= count
;
2714 /* If we already counted the number of program segments, make sure
2715 that we allocated enough space. This happens when SIZEOF_HEADERS
2716 is used in a linker script. */
2717 alloc
= elf_tdata (abfd
)->program_header_size
/ bed
->s
->sizeof_phdr
;
2718 if (alloc
!= 0 && count
> alloc
)
2720 ((*_bfd_error_handler
)
2721 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2722 bfd_get_filename (abfd
), alloc
, count
));
2723 bfd_set_error (bfd_error_bad_value
);
2730 phdrs
= ((Elf_Internal_Phdr
*)
2731 bfd_alloc (abfd
, alloc
* sizeof (Elf_Internal_Phdr
)));
2735 off
= bed
->s
->sizeof_ehdr
;
2736 off
+= alloc
* bed
->s
->sizeof_phdr
;
2743 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
2750 /* If elf_segment_map is not from map_sections_to_segments, the
2751 sections may not be correctly ordered. */
2753 qsort (m
->sections
, (size_t) m
->count
, sizeof (asection
*),
2756 p
->p_type
= m
->p_type
;
2757 p
->p_flags
= m
->p_flags
;
2759 if (p
->p_type
== PT_LOAD
2761 && (m
->sections
[0]->flags
& SEC_ALLOC
) != 0)
2763 if ((abfd
->flags
& D_PAGED
) != 0)
2764 off
+= (m
->sections
[0]->vma
- off
) % bed
->maxpagesize
;
2767 bfd_size_type align
;
2770 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2772 bfd_size_type secalign
;
2774 secalign
= bfd_get_section_alignment (abfd
, *secpp
);
2775 if (secalign
> align
)
2779 off
+= (m
->sections
[0]->vma
- off
) % (1 << align
);
2786 p
->p_vaddr
= m
->sections
[0]->vma
;
2788 if (m
->p_paddr_valid
)
2789 p
->p_paddr
= m
->p_paddr
;
2790 else if (m
->count
== 0)
2793 p
->p_paddr
= m
->sections
[0]->lma
;
2795 if (p
->p_type
== PT_LOAD
2796 && (abfd
->flags
& D_PAGED
) != 0)
2797 p
->p_align
= bed
->maxpagesize
;
2798 else if (m
->count
== 0)
2799 p
->p_align
= bed
->s
->file_align
;
2807 if (m
->includes_filehdr
)
2809 if (! m
->p_flags_valid
)
2812 p
->p_filesz
= bed
->s
->sizeof_ehdr
;
2813 p
->p_memsz
= bed
->s
->sizeof_ehdr
;
2816 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2818 if (p
->p_vaddr
< (bfd_vma
) off
)
2820 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2821 bfd_get_filename (abfd
));
2822 bfd_set_error (bfd_error_bad_value
);
2827 if (! m
->p_paddr_valid
)
2830 if (p
->p_type
== PT_LOAD
)
2832 filehdr_vaddr
= p
->p_vaddr
;
2833 filehdr_paddr
= p
->p_paddr
;
2837 if (m
->includes_phdrs
)
2839 if (! m
->p_flags_valid
)
2842 if (m
->includes_filehdr
)
2844 if (p
->p_type
== PT_LOAD
)
2846 phdrs_vaddr
= p
->p_vaddr
+ bed
->s
->sizeof_ehdr
;
2847 phdrs_paddr
= p
->p_paddr
+ bed
->s
->sizeof_ehdr
;
2852 p
->p_offset
= bed
->s
->sizeof_ehdr
;
2856 BFD_ASSERT (p
->p_type
== PT_LOAD
);
2857 p
->p_vaddr
-= off
- p
->p_offset
;
2858 if (! m
->p_paddr_valid
)
2859 p
->p_paddr
-= off
- p
->p_offset
;
2862 if (p
->p_type
== PT_LOAD
)
2864 phdrs_vaddr
= p
->p_vaddr
;
2865 phdrs_paddr
= p
->p_paddr
;
2868 phdrs_vaddr
= bed
->maxpagesize
+ bed
->s
->sizeof_ehdr
;
2871 p
->p_filesz
+= alloc
* bed
->s
->sizeof_phdr
;
2872 p
->p_memsz
+= alloc
* bed
->s
->sizeof_phdr
;
2875 if (p
->p_type
== PT_LOAD
2876 || (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
))
2878 if (! m
->includes_filehdr
&& ! m
->includes_phdrs
)
2884 adjust
= off
- (p
->p_offset
+ p
->p_filesz
);
2885 p
->p_filesz
+= adjust
;
2886 p
->p_memsz
+= adjust
;
2892 for (i
= 0, secpp
= m
->sections
; i
< m
->count
; i
++, secpp
++)
2896 bfd_size_type align
;
2900 align
= 1 << bfd_get_section_alignment (abfd
, sec
);
2902 /* The section may have artificial alignment forced by a
2903 link script. Notice this case by the gap between the
2904 cumulative phdr vma and the section's vma. */
2905 if (p
->p_vaddr
+ p
->p_memsz
< sec
->vma
)
2907 bfd_vma adjust
= sec
->vma
- (p
->p_vaddr
+ p
->p_memsz
);
2909 p
->p_memsz
+= adjust
;
2912 if ((flags
& SEC_LOAD
) != 0)
2913 p
->p_filesz
+= adjust
;
2916 if (p
->p_type
== PT_LOAD
)
2918 bfd_signed_vma adjust
;
2920 if ((flags
& SEC_LOAD
) != 0)
2922 adjust
= sec
->lma
- (p
->p_paddr
+ p
->p_memsz
);
2926 else if ((flags
& SEC_ALLOC
) != 0)
2928 /* The section VMA must equal the file position
2929 modulo the page size. FIXME: I'm not sure if
2930 this adjustment is really necessary. We used to
2931 not have the SEC_LOAD case just above, and then
2932 this was necessary, but now I'm not sure. */
2933 if ((abfd
->flags
& D_PAGED
) != 0)
2934 adjust
= (sec
->vma
- voff
) % bed
->maxpagesize
;
2936 adjust
= (sec
->vma
- voff
) % align
;
2945 (* _bfd_error_handler
)
2946 (_("Error: First section in segment (%s) starts at 0x%x"),
2947 bfd_section_name (abfd
, sec
), sec
->lma
);
2948 (* _bfd_error_handler
)
2949 (_(" whereas segment starts at 0x%x"),
2954 p
->p_memsz
+= adjust
;
2957 if ((flags
& SEC_LOAD
) != 0)
2958 p
->p_filesz
+= adjust
;
2963 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2964 used in a linker script we may have a section with
2965 SEC_LOAD clear but which is supposed to have
2967 if ((flags
& SEC_LOAD
) != 0
2968 || (flags
& SEC_HAS_CONTENTS
) != 0)
2969 off
+= sec
->_raw_size
;
2971 if ((flags
& SEC_ALLOC
) != 0)
2972 voff
+= sec
->_raw_size
;
2975 if (p
->p_type
== PT_NOTE
&& bfd_get_format (abfd
) == bfd_core
)
2977 /* The actual "note" segment has i == 0.
2978 This is the one that actually contains everything. */
2982 p
->p_filesz
= sec
->_raw_size
;
2983 off
+= sec
->_raw_size
;
2988 /* Fake sections -- don't need to be written. */
2991 flags
= sec
->flags
= 0;
2998 p
->p_memsz
+= sec
->_raw_size
;
3000 if ((flags
& SEC_LOAD
) != 0)
3001 p
->p_filesz
+= sec
->_raw_size
;
3003 if (align
> p
->p_align
3004 && (p
->p_type
!= PT_LOAD
|| (abfd
->flags
& D_PAGED
) == 0))
3008 if (! m
->p_flags_valid
)
3011 if ((flags
& SEC_CODE
) != 0)
3013 if ((flags
& SEC_READONLY
) == 0)
3019 /* Now that we have set the section file positions, we can set up
3020 the file positions for the non PT_LOAD segments. */
3021 for (m
= elf_tdata (abfd
)->segment_map
, p
= phdrs
;
3025 if (p
->p_type
!= PT_LOAD
&& m
->count
> 0)
3027 BFD_ASSERT (! m
->includes_filehdr
&& ! m
->includes_phdrs
);
3028 p
->p_offset
= m
->sections
[0]->filepos
;
3032 if (m
->includes_filehdr
)
3034 p
->p_vaddr
= filehdr_vaddr
;
3035 if (! m
->p_paddr_valid
)
3036 p
->p_paddr
= filehdr_paddr
;
3038 else if (m
->includes_phdrs
)
3040 p
->p_vaddr
= phdrs_vaddr
;
3041 if (! m
->p_paddr_valid
)
3042 p
->p_paddr
= phdrs_paddr
;
3047 /* Clear out any program headers we allocated but did not use. */
3048 for (; count
< alloc
; count
++, p
++)
3050 memset (p
, 0, sizeof *p
);
3051 p
->p_type
= PT_NULL
;
3054 elf_tdata (abfd
)->phdr
= phdrs
;
3056 elf_tdata (abfd
)->next_file_pos
= off
;
3058 /* Write out the program headers. */
3059 if (bfd_seek (abfd
, bed
->s
->sizeof_ehdr
, SEEK_SET
) != 0
3060 || bed
->s
->write_out_phdrs (abfd
, phdrs
, alloc
) != 0)
3066 /* Get the size of the program header.
3068 If this is called by the linker before any of the section VMA's are set, it
3069 can't calculate the correct value for a strange memory layout. This only
3070 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3071 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3072 data segment (exclusive of .interp and .dynamic).
3074 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3075 will be two segments. */
3077 static bfd_size_type
3078 get_program_header_size (abfd
)
3083 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3085 /* We can't return a different result each time we're called. */
3086 if (elf_tdata (abfd
)->program_header_size
!= 0)
3087 return elf_tdata (abfd
)->program_header_size
;
3089 if (elf_tdata (abfd
)->segment_map
!= NULL
)
3091 struct elf_segment_map
*m
;
3094 for (m
= elf_tdata (abfd
)->segment_map
; m
!= NULL
; m
= m
->next
)
3096 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3097 return elf_tdata (abfd
)->program_header_size
;
3100 /* Assume we will need exactly two PT_LOAD segments: one for text
3101 and one for data. */
3104 s
= bfd_get_section_by_name (abfd
, ".interp");
3105 if (s
!= NULL
&& (s
->flags
& SEC_LOAD
) != 0)
3107 /* If we have a loadable interpreter section, we need a
3108 PT_INTERP segment. In this case, assume we also need a
3109 PT_PHDR segment, although that may not be true for all
3114 if (bfd_get_section_by_name (abfd
, ".dynamic") != NULL
)
3116 /* We need a PT_DYNAMIC segment. */
3120 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
3122 if ((s
->flags
& SEC_LOAD
) != 0
3123 && strncmp (s
->name
, ".note", 5) == 0)
3125 /* We need a PT_NOTE segment. */
3130 /* Let the backend count up any program headers it might need. */
3131 if (bed
->elf_backend_additional_program_headers
)
3135 a
= (*bed
->elf_backend_additional_program_headers
) (abfd
);
3141 elf_tdata (abfd
)->program_header_size
= segs
* bed
->s
->sizeof_phdr
;
3142 return elf_tdata (abfd
)->program_header_size
;
3145 /* Work out the file positions of all the sections. This is called by
3146 _bfd_elf_compute_section_file_positions. All the section sizes and
3147 VMAs must be known before this is called.
3149 We do not consider reloc sections at this point, unless they form
3150 part of the loadable image. Reloc sections are assigned file
3151 positions in assign_file_positions_for_relocs, which is called by
3152 write_object_contents and final_link.
3154 We also don't set the positions of the .symtab and .strtab here. */
3157 assign_file_positions_except_relocs (abfd
)
3160 struct elf_obj_tdata
* const tdata
= elf_tdata (abfd
);
3161 Elf_Internal_Ehdr
* const i_ehdrp
= elf_elfheader (abfd
);
3162 Elf_Internal_Shdr
** const i_shdrpp
= elf_elfsections (abfd
);
3164 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3166 if ((abfd
->flags
& (EXEC_P
| DYNAMIC
)) == 0
3167 && bfd_get_format (abfd
) != bfd_core
)
3169 Elf_Internal_Shdr
**hdrpp
;
3172 /* Start after the ELF header. */
3173 off
= i_ehdrp
->e_ehsize
;
3175 /* We are not creating an executable, which means that we are
3176 not creating a program header, and that the actual order of
3177 the sections in the file is unimportant. */
3178 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3180 Elf_Internal_Shdr
*hdr
;
3183 if (hdr
->sh_type
== SHT_REL
|| hdr
->sh_type
== SHT_RELA
)
3185 hdr
->sh_offset
= -1;
3188 if (i
== tdata
->symtab_section
3189 || i
== tdata
->strtab_section
)
3191 hdr
->sh_offset
= -1;
3195 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3201 Elf_Internal_Shdr
**hdrpp
;
3203 /* Assign file positions for the loaded sections based on the
3204 assignment of sections to segments. */
3205 if (! assign_file_positions_for_segments (abfd
))
3208 /* Assign file positions for the other sections. */
3210 off
= elf_tdata (abfd
)->next_file_pos
;
3211 for (i
= 1, hdrpp
= i_shdrpp
+ 1; i
< i_ehdrp
->e_shnum
; i
++, hdrpp
++)
3213 Elf_Internal_Shdr
*hdr
;
3216 if (hdr
->bfd_section
!= NULL
3217 && hdr
->bfd_section
->filepos
!= 0)
3218 hdr
->sh_offset
= hdr
->bfd_section
->filepos
;
3219 else if ((hdr
->sh_flags
& SHF_ALLOC
) != 0)
3221 ((*_bfd_error_handler
)
3222 (_("%s: warning: allocated section `%s' not in segment"),
3223 bfd_get_filename (abfd
),
3224 (hdr
->bfd_section
== NULL
3226 : hdr
->bfd_section
->name
)));
3227 if ((abfd
->flags
& D_PAGED
) != 0)
3228 off
+= (hdr
->sh_addr
- off
) % bed
->maxpagesize
;
3230 off
+= (hdr
->sh_addr
- off
) % hdr
->sh_addralign
;
3231 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
,
3234 else if (hdr
->sh_type
== SHT_REL
3235 || hdr
->sh_type
== SHT_RELA
3236 || hdr
== i_shdrpp
[tdata
->symtab_section
]
3237 || hdr
== i_shdrpp
[tdata
->strtab_section
])
3238 hdr
->sh_offset
= -1;
3240 off
= _bfd_elf_assign_file_position_for_section (hdr
, off
, true);
3244 /* Place the section headers. */
3245 off
= align_file_position (off
, bed
->s
->file_align
);
3246 i_ehdrp
->e_shoff
= off
;
3247 off
+= i_ehdrp
->e_shnum
* i_ehdrp
->e_shentsize
;
3249 elf_tdata (abfd
)->next_file_pos
= off
;
3258 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
3259 Elf_Internal_Phdr
*i_phdrp
= 0; /* Program header table, internal form */
3260 Elf_Internal_Shdr
**i_shdrp
; /* Section header table, internal form */
3262 struct bfd_strtab_hash
*shstrtab
;
3263 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3265 i_ehdrp
= elf_elfheader (abfd
);
3266 i_shdrp
= elf_elfsections (abfd
);
3268 shstrtab
= _bfd_elf_stringtab_init ();
3269 if (shstrtab
== NULL
)
3272 elf_shstrtab (abfd
) = shstrtab
;
3274 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
3275 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
3276 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
3277 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
3279 i_ehdrp
->e_ident
[EI_CLASS
] = bed
->s
->elfclass
;
3280 i_ehdrp
->e_ident
[EI_DATA
] =
3281 bfd_big_endian (abfd
) ? ELFDATA2MSB
: ELFDATA2LSB
;
3282 i_ehdrp
->e_ident
[EI_VERSION
] = bed
->s
->ev_current
;
3284 i_ehdrp
->e_ident
[EI_OSABI
] = ELFOSABI_NONE
;
3285 i_ehdrp
->e_ident
[EI_ABIVERSION
] = 0;
3287 for (count
= EI_PAD
; count
< EI_NIDENT
; count
++)
3288 i_ehdrp
->e_ident
[count
] = 0;
3290 if ((abfd
->flags
& DYNAMIC
) != 0)
3291 i_ehdrp
->e_type
= ET_DYN
;
3292 else if ((abfd
->flags
& EXEC_P
) != 0)
3293 i_ehdrp
->e_type
= ET_EXEC
;
3294 else if (bfd_get_format (abfd
) == bfd_core
)
3295 i_ehdrp
->e_type
= ET_CORE
;
3297 i_ehdrp
->e_type
= ET_REL
;
3299 switch (bfd_get_arch (abfd
))
3301 case bfd_arch_unknown
:
3302 i_ehdrp
->e_machine
= EM_NONE
;
3304 case bfd_arch_sparc
:
3305 if (bfd_get_arch_size (abfd
) == 64)
3306 i_ehdrp
->e_machine
= EM_SPARCV9
;
3308 i_ehdrp
->e_machine
= EM_SPARC
;
3311 i_ehdrp
->e_machine
= EM_S370
;
3314 if (bfd_get_arch_size (abfd
) == 64)
3315 i_ehdrp
->e_machine
= EM_X86_64
;
3317 i_ehdrp
->e_machine
= EM_386
;
3320 i_ehdrp
->e_machine
= EM_IA_64
;
3322 case bfd_arch_m68hc11
:
3323 i_ehdrp
->e_machine
= EM_68HC11
;
3325 case bfd_arch_m68hc12
:
3326 i_ehdrp
->e_machine
= EM_68HC12
;
3329 i_ehdrp
->e_machine
= EM_S390
;
3332 i_ehdrp
->e_machine
= EM_68K
;
3335 i_ehdrp
->e_machine
= EM_88K
;
3338 i_ehdrp
->e_machine
= EM_860
;
3341 i_ehdrp
->e_machine
= EM_960
;
3343 case bfd_arch_mips
: /* MIPS Rxxxx */
3344 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
3347 i_ehdrp
->e_machine
= EM_PARISC
;
3349 case bfd_arch_powerpc
:
3350 i_ehdrp
->e_machine
= EM_PPC
;
3352 case bfd_arch_alpha
:
3353 i_ehdrp
->e_machine
= EM_ALPHA
;
3356 i_ehdrp
->e_machine
= EM_SH
;
3359 i_ehdrp
->e_machine
= EM_CYGNUS_D10V
;
3362 i_ehdrp
->e_machine
= EM_CYGNUS_D30V
;
3365 i_ehdrp
->e_machine
= EM_CYGNUS_FR30
;
3367 case bfd_arch_mcore
:
3368 i_ehdrp
->e_machine
= EM_MCORE
;
3371 i_ehdrp
->e_machine
= EM_AVR
;
3374 switch (bfd_get_mach (abfd
))
3377 case 0: i_ehdrp
->e_machine
= EM_CYGNUS_V850
; break;
3381 i_ehdrp
->e_machine
= EM_CYGNUS_ARC
;
3384 i_ehdrp
->e_machine
= EM_ARM
;
3387 i_ehdrp
->e_machine
= EM_CYGNUS_M32R
;
3389 case bfd_arch_mn10200
:
3390 i_ehdrp
->e_machine
= EM_CYGNUS_MN10200
;
3392 case bfd_arch_mn10300
:
3393 i_ehdrp
->e_machine
= EM_CYGNUS_MN10300
;
3396 i_ehdrp
->e_machine
= EM_PJ
;
3399 i_ehdrp
->e_machine
= EM_CRIS
;
3401 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3403 i_ehdrp
->e_machine
= EM_NONE
;
3405 i_ehdrp
->e_version
= bed
->s
->ev_current
;
3406 i_ehdrp
->e_ehsize
= bed
->s
->sizeof_ehdr
;
3408 /* No program header, for now. */
3409 i_ehdrp
->e_phoff
= 0;
3410 i_ehdrp
->e_phentsize
= 0;
3411 i_ehdrp
->e_phnum
= 0;
3413 /* Each bfd section is section header entry. */
3414 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
3415 i_ehdrp
->e_shentsize
= bed
->s
->sizeof_shdr
;
3417 /* If we're building an executable, we'll need a program header table. */
3418 if (abfd
->flags
& EXEC_P
)
3420 /* It all happens later. */
3422 i_ehdrp
->e_phentsize
= sizeof (Elf_External_Phdr
);
3424 /* elf_build_phdrs() returns a (NULL-terminated) array of
3425 Elf_Internal_Phdrs. */
3426 i_phdrp
= elf_build_phdrs (abfd
, i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
3427 i_ehdrp
->e_phoff
= outbase
;
3428 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
3433 i_ehdrp
->e_phentsize
= 0;
3435 i_ehdrp
->e_phoff
= 0;
3438 elf_tdata (abfd
)->symtab_hdr
.sh_name
=
3439 (unsigned int) _bfd_stringtab_add (shstrtab
, ".symtab", true, false);
3440 elf_tdata (abfd
)->strtab_hdr
.sh_name
=
3441 (unsigned int) _bfd_stringtab_add (shstrtab
, ".strtab", true, false);
3442 elf_tdata (abfd
)->shstrtab_hdr
.sh_name
=
3443 (unsigned int) _bfd_stringtab_add (shstrtab
, ".shstrtab", true, false);
3444 if (elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3445 || elf_tdata (abfd
)->symtab_hdr
.sh_name
== (unsigned int) -1
3446 || elf_tdata (abfd
)->shstrtab_hdr
.sh_name
== (unsigned int) -1)
3452 /* Assign file positions for all the reloc sections which are not part
3453 of the loadable file image. */
3456 _bfd_elf_assign_file_positions_for_relocs (abfd
)
3461 Elf_Internal_Shdr
**shdrpp
;
3463 off
= elf_tdata (abfd
)->next_file_pos
;
3465 for (i
= 1, shdrpp
= elf_elfsections (abfd
) + 1;
3466 i
< elf_elfheader (abfd
)->e_shnum
;
3469 Elf_Internal_Shdr
*shdrp
;
3472 if ((shdrp
->sh_type
== SHT_REL
|| shdrp
->sh_type
== SHT_RELA
)
3473 && shdrp
->sh_offset
== -1)
3474 off
= _bfd_elf_assign_file_position_for_section (shdrp
, off
, true);
3477 elf_tdata (abfd
)->next_file_pos
= off
;
3481 _bfd_elf_write_object_contents (abfd
)
3484 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3485 Elf_Internal_Ehdr
*i_ehdrp
;
3486 Elf_Internal_Shdr
**i_shdrp
;
3490 if (! abfd
->output_has_begun
3491 && ! _bfd_elf_compute_section_file_positions
3492 (abfd
, (struct bfd_link_info
*) NULL
))
3495 i_shdrp
= elf_elfsections (abfd
);
3496 i_ehdrp
= elf_elfheader (abfd
);
3499 bfd_map_over_sections (abfd
, bed
->s
->write_relocs
, &failed
);
3503 _bfd_elf_assign_file_positions_for_relocs (abfd
);
3505 /* After writing the headers, we need to write the sections too... */
3506 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
3508 if (bed
->elf_backend_section_processing
)
3509 (*bed
->elf_backend_section_processing
) (abfd
, i_shdrp
[count
]);
3510 if (i_shdrp
[count
]->contents
)
3512 if (bfd_seek (abfd
, i_shdrp
[count
]->sh_offset
, SEEK_SET
) != 0
3513 || (bfd_write (i_shdrp
[count
]->contents
, i_shdrp
[count
]->sh_size
,
3515 != i_shdrp
[count
]->sh_size
))
3520 /* Write out the section header names. */
3521 if (bfd_seek (abfd
, elf_tdata (abfd
)->shstrtab_hdr
.sh_offset
, SEEK_SET
) != 0
3522 || ! _bfd_stringtab_emit (abfd
, elf_shstrtab (abfd
)))
3525 if (bed
->elf_backend_final_write_processing
)
3526 (*bed
->elf_backend_final_write_processing
) (abfd
,
3527 elf_tdata (abfd
)->linker
);
3529 return bed
->s
->write_shdrs_and_ehdr (abfd
);
3533 _bfd_elf_write_corefile_contents (abfd
)
3536 /* Hopefully this can be done just like an object file. */
3537 return _bfd_elf_write_object_contents (abfd
);
3540 /* Given a section, search the header to find them. */
3543 _bfd_elf_section_from_bfd_section (abfd
, asect
)
3547 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3548 Elf_Internal_Shdr
**i_shdrp
= elf_elfsections (abfd
);
3550 Elf_Internal_Shdr
*hdr
;
3551 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
3553 for (index
= 0; index
< maxindex
; index
++)
3555 hdr
= i_shdrp
[index
];
3556 if (hdr
->bfd_section
== asect
)
3560 if (bed
->elf_backend_section_from_bfd_section
)
3562 for (index
= 0; index
< maxindex
; index
++)
3566 hdr
= i_shdrp
[index
];
3568 if ((*bed
->elf_backend_section_from_bfd_section
)
3569 (abfd
, hdr
, asect
, &retval
))
3574 if (bfd_is_abs_section (asect
))
3576 if (bfd_is_com_section (asect
))
3578 if (bfd_is_und_section (asect
))
3581 bfd_set_error (bfd_error_nonrepresentable_section
);
3586 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3590 _bfd_elf_symbol_from_bfd_symbol (abfd
, asym_ptr_ptr
)
3592 asymbol
**asym_ptr_ptr
;
3594 asymbol
*asym_ptr
= *asym_ptr_ptr
;
3596 flagword flags
= asym_ptr
->flags
;
3598 /* When gas creates relocations against local labels, it creates its
3599 own symbol for the section, but does put the symbol into the
3600 symbol chain, so udata is 0. When the linker is generating
3601 relocatable output, this section symbol may be for one of the
3602 input sections rather than the output section. */
3603 if (asym_ptr
->udata
.i
== 0
3604 && (flags
& BSF_SECTION_SYM
)
3605 && asym_ptr
->section
)
3609 if (asym_ptr
->section
->output_section
!= NULL
)
3610 indx
= asym_ptr
->section
->output_section
->index
;
3612 indx
= asym_ptr
->section
->index
;
3613 if (elf_section_syms (abfd
)[indx
])
3614 asym_ptr
->udata
.i
= elf_section_syms (abfd
)[indx
]->udata
.i
;
3617 idx
= asym_ptr
->udata
.i
;
3621 /* This case can occur when using --strip-symbol on a symbol
3622 which is used in a relocation entry. */
3623 (*_bfd_error_handler
)
3624 (_("%s: symbol `%s' required but not present"),
3625 bfd_get_filename (abfd
), bfd_asymbol_name (asym_ptr
));
3626 bfd_set_error (bfd_error_no_symbols
);
3633 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3634 (long) asym_ptr
, asym_ptr
->name
, idx
, flags
,
3635 elf_symbol_flags (flags
));
3643 /* Copy private BFD data. This copies any program header information. */
3646 copy_private_bfd_data (ibfd
, obfd
)
3650 Elf_Internal_Ehdr
* iehdr
;
3651 struct elf_segment_map
* map
;
3652 struct elf_segment_map
* map_first
;
3653 struct elf_segment_map
** pointer_to_map
;
3654 Elf_Internal_Phdr
* segment
;
3657 unsigned int num_segments
;
3658 boolean phdr_included
= false;
3659 bfd_vma maxpagesize
;
3660 struct elf_segment_map
* phdr_adjust_seg
= NULL
;
3661 unsigned int phdr_adjust_num
= 0;
3663 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3664 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3667 if (elf_tdata (ibfd
)->phdr
== NULL
)
3670 iehdr
= elf_elfheader (ibfd
);
3673 pointer_to_map
= &map_first
;
3675 num_segments
= elf_elfheader (ibfd
)->e_phnum
;
3676 maxpagesize
= get_elf_backend_data (obfd
)->maxpagesize
;
3678 /* Returns the end address of the segment + 1. */
3679 #define SEGMENT_END(segment, start) \
3680 (start + (segment->p_memsz > segment->p_filesz \
3681 ? segment->p_memsz : segment->p_filesz))
3683 /* Returns true if the given section is contained within
3684 the given segment. VMA addresses are compared. */
3685 #define IS_CONTAINED_BY_VMA(section, segment) \
3686 (section->vma >= segment->p_vaddr \
3687 && (section->vma + section->_raw_size) \
3688 <= (SEGMENT_END (segment, segment->p_vaddr)))
3690 /* Returns true if the given section is contained within
3691 the given segment. LMA addresses are compared. */
3692 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3693 (section->lma >= base \
3694 && (section->lma + section->_raw_size) \
3695 <= SEGMENT_END (segment, base))
3697 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3698 #define IS_COREFILE_NOTE(p, s) \
3699 (p->p_type == PT_NOTE \
3700 && bfd_get_format (ibfd) == bfd_core \
3701 && s->vma == 0 && s->lma == 0 \
3702 && (bfd_vma) s->filepos >= p->p_offset \
3703 && (bfd_vma) s->filepos + s->_raw_size \
3704 <= p->p_offset + p->p_filesz)
3706 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3707 linker, which generates a PT_INTERP section with p_vaddr and
3708 p_memsz set to 0. */
3709 #define IS_SOLARIS_PT_INTERP(p, s) \
3711 && p->p_filesz > 0 \
3712 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3713 && s->_raw_size > 0 \
3714 && (bfd_vma) s->filepos >= p->p_offset \
3715 && ((bfd_vma) s->filepos + s->_raw_size \
3716 <= p->p_offset + p->p_filesz))
3718 /* Decide if the given section should be included in the given segment.
3719 A section will be included if:
3720 1. It is within the address space of the segment,
3721 2. It is an allocated segment,
3722 3. There is an output section associated with it,
3723 4. The section has not already been allocated to a previous segment. */
3724 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3725 ((((IS_CONTAINED_BY_VMA (section, segment) \
3726 || IS_SOLARIS_PT_INTERP (segment, section)) \
3727 && (section->flags & SEC_ALLOC) != 0) \
3728 || IS_COREFILE_NOTE (segment, section)) \
3729 && section->output_section != NULL \
3730 && section->segment_mark == false)
3732 /* Returns true iff seg1 starts after the end of seg2. */
3733 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3734 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3736 /* Returns true iff seg1 and seg2 overlap. */
3737 #define SEGMENT_OVERLAPS(seg1, seg2) \
3738 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3740 /* Initialise the segment mark field. */
3741 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3742 section
->segment_mark
= false;
3744 /* Scan through the segments specified in the program header
3745 of the input BFD. For this first scan we look for overlaps
3746 in the loadable segments. These can be created by wierd
3747 parameters to objcopy. */
3748 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3753 Elf_Internal_Phdr
*segment2
;
3755 if (segment
->p_type
!= PT_LOAD
)
3758 /* Determine if this segment overlaps any previous segments. */
3759 for (j
= 0, segment2
= elf_tdata (ibfd
)->phdr
; j
< i
; j
++, segment2
++)
3761 bfd_signed_vma extra_length
;
3763 if (segment2
->p_type
!= PT_LOAD
3764 || ! SEGMENT_OVERLAPS (segment
, segment2
))
3767 /* Merge the two segments together. */
3768 if (segment2
->p_vaddr
< segment
->p_vaddr
)
3770 /* Extend SEGMENT2 to include SEGMENT and then delete
3773 SEGMENT_END (segment
, segment
->p_vaddr
)
3774 - SEGMENT_END (segment2
, segment2
->p_vaddr
);
3776 if (extra_length
> 0)
3778 segment2
->p_memsz
+= extra_length
;
3779 segment2
->p_filesz
+= extra_length
;
3782 segment
->p_type
= PT_NULL
;
3784 /* Since we have deleted P we must restart the outer loop. */
3786 segment
= elf_tdata (ibfd
)->phdr
;
3791 /* Extend SEGMENT to include SEGMENT2 and then delete
3794 SEGMENT_END (segment2
, segment2
->p_vaddr
)
3795 - SEGMENT_END (segment
, segment
->p_vaddr
);
3797 if (extra_length
> 0)
3799 segment
->p_memsz
+= extra_length
;
3800 segment
->p_filesz
+= extra_length
;
3803 segment2
->p_type
= PT_NULL
;
3808 /* The second scan attempts to assign sections to segments. */
3809 for (i
= 0, segment
= elf_tdata (ibfd
)->phdr
;
3813 unsigned int section_count
;
3814 asection
** sections
;
3815 asection
* output_section
;
3817 bfd_vma matching_lma
;
3818 bfd_vma suggested_lma
;
3821 if (segment
->p_type
== PT_NULL
)
3824 /* Compute how many sections might be placed into this segment. */
3826 for (section
= ibfd
->sections
; section
!= NULL
; section
= section
->next
)
3827 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3830 /* Allocate a segment map big enough to contain all of the
3831 sections we have selected. */
3832 map
= ((struct elf_segment_map
*)
3834 (sizeof (struct elf_segment_map
)
3835 + ((size_t) section_count
- 1) * sizeof (asection
*))));
3839 /* Initialise the fields of the segment map. Default to
3840 using the physical address of the segment in the input BFD. */
3842 map
->p_type
= segment
->p_type
;
3843 map
->p_flags
= segment
->p_flags
;
3844 map
->p_flags_valid
= 1;
3845 map
->p_paddr
= segment
->p_paddr
;
3846 map
->p_paddr_valid
= 1;
3848 /* Determine if this segment contains the ELF file header
3849 and if it contains the program headers themselves. */
3850 map
->includes_filehdr
= (segment
->p_offset
== 0
3851 && segment
->p_filesz
>= iehdr
->e_ehsize
);
3853 map
->includes_phdrs
= 0;
3855 if (! phdr_included
|| segment
->p_type
!= PT_LOAD
)
3857 map
->includes_phdrs
=
3858 (segment
->p_offset
<= (bfd_vma
) iehdr
->e_phoff
3859 && (segment
->p_offset
+ segment
->p_filesz
3860 >= ((bfd_vma
) iehdr
->e_phoff
3861 + iehdr
->e_phnum
* iehdr
->e_phentsize
)));
3863 if (segment
->p_type
== PT_LOAD
&& map
->includes_phdrs
)
3864 phdr_included
= true;
3867 if (section_count
== 0)
3869 /* Special segments, such as the PT_PHDR segment, may contain
3870 no sections, but ordinary, loadable segments should contain
3872 if (segment
->p_type
== PT_LOAD
)
3874 (_("%s: warning: Empty loadable segment detected\n"),
3875 bfd_get_filename (ibfd
));
3878 *pointer_to_map
= map
;
3879 pointer_to_map
= &map
->next
;
3884 /* Now scan the sections in the input BFD again and attempt
3885 to add their corresponding output sections to the segment map.
3886 The problem here is how to handle an output section which has
3887 been moved (ie had its LMA changed). There are four possibilities:
3889 1. None of the sections have been moved.
3890 In this case we can continue to use the segment LMA from the
3893 2. All of the sections have been moved by the same amount.
3894 In this case we can change the segment's LMA to match the LMA
3895 of the first section.
3897 3. Some of the sections have been moved, others have not.
3898 In this case those sections which have not been moved can be
3899 placed in the current segment which will have to have its size,
3900 and possibly its LMA changed, and a new segment or segments will
3901 have to be created to contain the other sections.
3903 4. The sections have been moved, but not be the same amount.
3904 In this case we can change the segment's LMA to match the LMA
3905 of the first section and we will have to create a new segment
3906 or segments to contain the other sections.
3908 In order to save time, we allocate an array to hold the section
3909 pointers that we are interested in. As these sections get assigned
3910 to a segment, they are removed from this array. */
3912 sections
= (asection
**) bfd_malloc
3913 (sizeof (asection
*) * section_count
);
3914 if (sections
== NULL
)
3917 /* Step One: Scan for segment vs section LMA conflicts.
3918 Also add the sections to the section array allocated above.
3919 Also add the sections to the current segment. In the common
3920 case, where the sections have not been moved, this means that
3921 we have completely filled the segment, and there is nothing
3927 for (j
= 0, section
= ibfd
->sections
;
3929 section
= section
->next
)
3931 if (INCLUDE_SECTION_IN_SEGMENT (section
, segment
))
3933 output_section
= section
->output_section
;
3935 sections
[j
++] = section
;
3937 /* The Solaris native linker always sets p_paddr to 0.
3938 We try to catch that case here, and set it to the
3940 if (segment
->p_paddr
== 0
3941 && segment
->p_vaddr
!= 0
3943 && output_section
->lma
!= 0
3944 && (output_section
->vma
== (segment
->p_vaddr
3945 + (map
->includes_filehdr
3948 + (map
->includes_phdrs
3949 ? iehdr
->e_phnum
* iehdr
->e_phentsize
3951 map
->p_paddr
= segment
->p_vaddr
;
3953 /* Match up the physical address of the segment with the
3954 LMA address of the output section. */
3955 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
3956 || IS_COREFILE_NOTE (segment
, section
))
3958 if (matching_lma
== 0)
3959 matching_lma
= output_section
->lma
;
3961 /* We assume that if the section fits within the segment
3962 then it does not overlap any other section within that
3964 map
->sections
[isec
++] = output_section
;
3966 else if (suggested_lma
== 0)
3967 suggested_lma
= output_section
->lma
;
3971 BFD_ASSERT (j
== section_count
);
3973 /* Step Two: Adjust the physical address of the current segment,
3975 if (isec
== section_count
)
3977 /* All of the sections fitted within the segment as currently
3978 specified. This is the default case. Add the segment to
3979 the list of built segments and carry on to process the next
3980 program header in the input BFD. */
3981 map
->count
= section_count
;
3982 *pointer_to_map
= map
;
3983 pointer_to_map
= &map
->next
;
3990 if (matching_lma
!= 0)
3992 /* At least one section fits inside the current segment.
3993 Keep it, but modify its physical address to match the
3994 LMA of the first section that fitted. */
3995 map
->p_paddr
= matching_lma
;
3999 /* None of the sections fitted inside the current segment.
4000 Change the current segment's physical address to match
4001 the LMA of the first section. */
4002 map
->p_paddr
= suggested_lma
;
4005 /* Offset the segment physical address from the lma
4006 to allow for space taken up by elf headers. */
4007 if (map
->includes_filehdr
)
4008 map
->p_paddr
-= iehdr
->e_ehsize
;
4010 if (map
->includes_phdrs
)
4012 map
->p_paddr
-= iehdr
->e_phnum
* iehdr
->e_phentsize
;
4014 /* iehdr->e_phnum is just an estimate of the number
4015 of program headers that we will need. Make a note
4016 here of the number we used and the segment we chose
4017 to hold these headers, so that we can adjust the
4018 offset when we know the correct value. */
4019 phdr_adjust_num
= iehdr
->e_phnum
;
4020 phdr_adjust_seg
= map
;
4024 /* Step Three: Loop over the sections again, this time assigning
4025 those that fit to the current segment and remvoing them from the
4026 sections array; but making sure not to leave large gaps. Once all
4027 possible sections have been assigned to the current segment it is
4028 added to the list of built segments and if sections still remain
4029 to be assigned, a new segment is constructed before repeating
4037 /* Fill the current segment with sections that fit. */
4038 for (j
= 0; j
< section_count
; j
++)
4040 section
= sections
[j
];
4042 if (section
== NULL
)
4045 output_section
= section
->output_section
;
4047 BFD_ASSERT (output_section
!= NULL
);
4049 if (IS_CONTAINED_BY_LMA (output_section
, segment
, map
->p_paddr
)
4050 || IS_COREFILE_NOTE (segment
, section
))
4052 if (map
->count
== 0)
4054 /* If the first section in a segment does not start at
4055 the beginning of the segment, then something is
4057 if (output_section
->lma
!=
4059 + (map
->includes_filehdr
? iehdr
->e_ehsize
: 0)
4060 + (map
->includes_phdrs
4061 ? iehdr
->e_phnum
* iehdr
->e_phentsize
4067 asection
* prev_sec
;
4069 prev_sec
= map
->sections
[map
->count
- 1];
4071 /* If the gap between the end of the previous section
4072 and the start of this section is more than
4073 maxpagesize then we need to start a new segment. */
4074 if ((BFD_ALIGN (prev_sec
->lma
+ prev_sec
->_raw_size
, maxpagesize
)
4075 < BFD_ALIGN (output_section
->lma
, maxpagesize
))
4076 || ((prev_sec
->lma
+ prev_sec
->_raw_size
) > output_section
->lma
))
4078 if (suggested_lma
== 0)
4079 suggested_lma
= output_section
->lma
;
4085 map
->sections
[map
->count
++] = output_section
;
4088 section
->segment_mark
= true;
4090 else if (suggested_lma
== 0)
4091 suggested_lma
= output_section
->lma
;
4094 BFD_ASSERT (map
->count
> 0);
4096 /* Add the current segment to the list of built segments. */
4097 *pointer_to_map
= map
;
4098 pointer_to_map
= &map
->next
;
4100 if (isec
< section_count
)
4102 /* We still have not allocated all of the sections to
4103 segments. Create a new segment here, initialise it
4104 and carry on looping. */
4105 map
= ((struct elf_segment_map
*)
4107 (sizeof (struct elf_segment_map
)
4108 + ((size_t) section_count
- 1)
4109 * sizeof (asection
*))));
4113 /* Initialise the fields of the segment map. Set the physical
4114 physical address to the LMA of the first section that has
4115 not yet been assigned. */
4117 map
->p_type
= segment
->p_type
;
4118 map
->p_flags
= segment
->p_flags
;
4119 map
->p_flags_valid
= 1;
4120 map
->p_paddr
= suggested_lma
;
4121 map
->p_paddr_valid
= 1;
4122 map
->includes_filehdr
= 0;
4123 map
->includes_phdrs
= 0;
4126 while (isec
< section_count
);
4131 /* The Solaris linker creates program headers in which all the
4132 p_paddr fields are zero. When we try to objcopy or strip such a
4133 file, we get confused. Check for this case, and if we find it
4134 reset the p_paddr_valid fields. */
4135 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4136 if (map
->p_paddr
!= 0)
4140 for (map
= map_first
; map
!= NULL
; map
= map
->next
)
4141 map
->p_paddr_valid
= 0;
4144 elf_tdata (obfd
)->segment_map
= map_first
;
4146 /* If we had to estimate the number of program headers that were
4147 going to be needed, then check our estimate know and adjust
4148 the offset if necessary. */
4149 if (phdr_adjust_seg
!= NULL
)
4153 for (count
= 0, map
= map_first
; map
!= NULL
; map
= map
->next
)
4156 if (count
> phdr_adjust_num
)
4157 phdr_adjust_seg
->p_paddr
4158 -= (count
- phdr_adjust_num
) * iehdr
->e_phentsize
;
4162 /* Final Step: Sort the segments into ascending order of physical
4164 if (map_first
!= NULL
)
4166 struct elf_segment_map
*prev
;
4169 for (map
= map_first
->next
; map
!= NULL
; prev
= map
, map
= map
->next
)
4171 /* Yes I know - its a bubble sort.... */
4172 if (map
->next
!= NULL
&& (map
->next
->p_paddr
< map
->p_paddr
))
4174 /* Swap map and map->next. */
4175 prev
->next
= map
->next
;
4176 map
->next
= map
->next
->next
;
4177 prev
->next
->next
= map
;
4187 #undef IS_CONTAINED_BY_VMA
4188 #undef IS_CONTAINED_BY_LMA
4189 #undef IS_COREFILE_NOTE
4190 #undef IS_SOLARIS_PT_INTERP
4191 #undef INCLUDE_SECTION_IN_SEGMENT
4192 #undef SEGMENT_AFTER_SEGMENT
4193 #undef SEGMENT_OVERLAPS
4197 /* Copy private section information. This copies over the entsize
4198 field, and sometimes the info field. */
4201 _bfd_elf_copy_private_section_data (ibfd
, isec
, obfd
, osec
)
4207 Elf_Internal_Shdr
*ihdr
, *ohdr
;
4209 if (ibfd
->xvec
->flavour
!= bfd_target_elf_flavour
4210 || obfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
4213 /* Copy over private BFD data if it has not already been copied.
4214 This must be done here, rather than in the copy_private_bfd_data
4215 entry point, because the latter is called after the section
4216 contents have been set, which means that the program headers have
4217 already been worked out. */
4218 if (elf_tdata (obfd
)->segment_map
== NULL
4219 && elf_tdata (ibfd
)->phdr
!= NULL
)
4223 /* Only set up the segments if there are no more SEC_ALLOC
4224 sections. FIXME: This won't do the right thing if objcopy is
4225 used to remove the last SEC_ALLOC section, since objcopy
4226 won't call this routine in that case. */
4227 for (s
= isec
->next
; s
!= NULL
; s
= s
->next
)
4228 if ((s
->flags
& SEC_ALLOC
) != 0)
4232 if (! copy_private_bfd_data (ibfd
, obfd
))
4237 ihdr
= &elf_section_data (isec
)->this_hdr
;
4238 ohdr
= &elf_section_data (osec
)->this_hdr
;
4240 ohdr
->sh_entsize
= ihdr
->sh_entsize
;
4242 if (ihdr
->sh_type
== SHT_SYMTAB
4243 || ihdr
->sh_type
== SHT_DYNSYM
4244 || ihdr
->sh_type
== SHT_GNU_verneed
4245 || ihdr
->sh_type
== SHT_GNU_verdef
)
4246 ohdr
->sh_info
= ihdr
->sh_info
;
4248 elf_section_data (osec
)->use_rela_p
4249 = elf_section_data (isec
)->use_rela_p
;
4254 /* Copy private symbol information. If this symbol is in a section
4255 which we did not map into a BFD section, try to map the section
4256 index correctly. We use special macro definitions for the mapped
4257 section indices; these definitions are interpreted by the
4258 swap_out_syms function. */
4260 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4261 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4262 #define MAP_STRTAB (SHN_LORESERVE - 3)
4263 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4266 _bfd_elf_copy_private_symbol_data (ibfd
, isymarg
, obfd
, osymarg
)
4272 elf_symbol_type
*isym
, *osym
;
4274 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
4275 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
4278 isym
= elf_symbol_from (ibfd
, isymarg
);
4279 osym
= elf_symbol_from (obfd
, osymarg
);
4283 && bfd_is_abs_section (isym
->symbol
.section
))
4287 shndx
= isym
->internal_elf_sym
.st_shndx
;
4288 if (shndx
== elf_onesymtab (ibfd
))
4289 shndx
= MAP_ONESYMTAB
;
4290 else if (shndx
== elf_dynsymtab (ibfd
))
4291 shndx
= MAP_DYNSYMTAB
;
4292 else if (shndx
== elf_tdata (ibfd
)->strtab_section
)
4294 else if (shndx
== elf_tdata (ibfd
)->shstrtab_section
)
4295 shndx
= MAP_SHSTRTAB
;
4296 osym
->internal_elf_sym
.st_shndx
= shndx
;
4302 /* Swap out the symbols. */
4305 swap_out_syms (abfd
, sttp
, relocatable_p
)
4307 struct bfd_strtab_hash
**sttp
;
4310 struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
4312 if (!elf_map_symbols (abfd
))
4315 /* Dump out the symtabs. */
4317 int symcount
= bfd_get_symcount (abfd
);
4318 asymbol
**syms
= bfd_get_outsymbols (abfd
);
4319 struct bfd_strtab_hash
*stt
;
4320 Elf_Internal_Shdr
*symtab_hdr
;
4321 Elf_Internal_Shdr
*symstrtab_hdr
;
4322 char *outbound_syms
;
4325 stt
= _bfd_elf_stringtab_init ();
4329 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4330 symtab_hdr
->sh_type
= SHT_SYMTAB
;
4331 symtab_hdr
->sh_entsize
= bed
->s
->sizeof_sym
;
4332 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
4333 symtab_hdr
->sh_info
= elf_num_locals (abfd
) + 1;
4334 symtab_hdr
->sh_addralign
= bed
->s
->file_align
;
4336 symstrtab_hdr
= &elf_tdata (abfd
)->strtab_hdr
;
4337 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4339 outbound_syms
= bfd_alloc (abfd
,
4340 (1 + symcount
) * bed
->s
->sizeof_sym
);
4341 if (outbound_syms
== NULL
)
4343 symtab_hdr
->contents
= (PTR
) outbound_syms
;
4345 /* now generate the data (for "contents") */
4347 /* Fill in zeroth symbol and swap it out. */
4348 Elf_Internal_Sym sym
;
4354 sym
.st_shndx
= SHN_UNDEF
;
4355 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4356 outbound_syms
+= bed
->s
->sizeof_sym
;
4358 for (idx
= 0; idx
< symcount
; idx
++)
4360 Elf_Internal_Sym sym
;
4361 bfd_vma value
= syms
[idx
]->value
;
4362 elf_symbol_type
*type_ptr
;
4363 flagword flags
= syms
[idx
]->flags
;
4366 if ((flags
& BSF_SECTION_SYM
) != 0)
4368 /* Section symbols have no name. */
4373 sym
.st_name
= (unsigned long) _bfd_stringtab_add (stt
,
4376 if (sym
.st_name
== (unsigned long) -1)
4380 type_ptr
= elf_symbol_from (abfd
, syms
[idx
]);
4382 if ((flags
& BSF_SECTION_SYM
) == 0
4383 && bfd_is_com_section (syms
[idx
]->section
))
4385 /* ELF common symbols put the alignment into the `value' field,
4386 and the size into the `size' field. This is backwards from
4387 how BFD handles it, so reverse it here. */
4388 sym
.st_size
= value
;
4389 if (type_ptr
== NULL
4390 || type_ptr
->internal_elf_sym
.st_value
== 0)
4391 sym
.st_value
= value
>= 16 ? 16 : (1 << bfd_log2 (value
));
4393 sym
.st_value
= type_ptr
->internal_elf_sym
.st_value
;
4394 sym
.st_shndx
= _bfd_elf_section_from_bfd_section
4395 (abfd
, syms
[idx
]->section
);
4399 asection
*sec
= syms
[idx
]->section
;
4402 if (sec
->output_section
)
4404 value
+= sec
->output_offset
;
4405 sec
= sec
->output_section
;
4407 /* Don't add in the section vma for relocatable output. */
4408 if (! relocatable_p
)
4410 sym
.st_value
= value
;
4411 sym
.st_size
= type_ptr
? type_ptr
->internal_elf_sym
.st_size
: 0;
4413 if (bfd_is_abs_section (sec
)
4415 && type_ptr
->internal_elf_sym
.st_shndx
!= 0)
4417 /* This symbol is in a real ELF section which we did
4418 not create as a BFD section. Undo the mapping done
4419 by copy_private_symbol_data. */
4420 shndx
= type_ptr
->internal_elf_sym
.st_shndx
;
4424 shndx
= elf_onesymtab (abfd
);
4427 shndx
= elf_dynsymtab (abfd
);
4430 shndx
= elf_tdata (abfd
)->strtab_section
;
4433 shndx
= elf_tdata (abfd
)->shstrtab_section
;
4441 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4447 /* Writing this would be a hell of a lot easier if
4448 we had some decent documentation on bfd, and
4449 knew what to expect of the library, and what to
4450 demand of applications. For example, it
4451 appears that `objcopy' might not set the
4452 section of a symbol to be a section that is
4453 actually in the output file. */
4454 sec2
= bfd_get_section_by_name (abfd
, sec
->name
);
4455 BFD_ASSERT (sec2
!= 0);
4456 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec2
);
4457 BFD_ASSERT (shndx
!= -1);
4461 sym
.st_shndx
= shndx
;
4464 if ((flags
& BSF_FUNCTION
) != 0)
4466 else if ((flags
& BSF_OBJECT
) != 0)
4471 /* Processor-specific types */
4472 if (type_ptr
!= NULL
4473 && bed
->elf_backend_get_symbol_type
)
4474 type
= (*bed
->elf_backend_get_symbol_type
) (&type_ptr
->internal_elf_sym
, type
);
4476 if (flags
& BSF_SECTION_SYM
)
4477 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_SECTION
);
4478 else if (bfd_is_com_section (syms
[idx
]->section
))
4479 sym
.st_info
= ELF_ST_INFO (STB_GLOBAL
, type
);
4480 else if (bfd_is_und_section (syms
[idx
]->section
))
4481 sym
.st_info
= ELF_ST_INFO (((flags
& BSF_WEAK
)
4485 else if (flags
& BSF_FILE
)
4486 sym
.st_info
= ELF_ST_INFO (STB_LOCAL
, STT_FILE
);
4489 int bind
= STB_LOCAL
;
4491 if (flags
& BSF_LOCAL
)
4493 else if (flags
& BSF_WEAK
)
4495 else if (flags
& BSF_GLOBAL
)
4498 sym
.st_info
= ELF_ST_INFO (bind
, type
);
4501 if (type_ptr
!= NULL
)
4502 sym
.st_other
= type_ptr
->internal_elf_sym
.st_other
;
4506 bed
->s
->swap_symbol_out (abfd
, &sym
, (PTR
) outbound_syms
);
4507 outbound_syms
+= bed
->s
->sizeof_sym
;
4511 symstrtab_hdr
->sh_size
= _bfd_stringtab_size (stt
);
4512 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
4514 symstrtab_hdr
->sh_flags
= 0;
4515 symstrtab_hdr
->sh_addr
= 0;
4516 symstrtab_hdr
->sh_entsize
= 0;
4517 symstrtab_hdr
->sh_link
= 0;
4518 symstrtab_hdr
->sh_info
= 0;
4519 symstrtab_hdr
->sh_addralign
= 1;
4525 /* Return the number of bytes required to hold the symtab vector.
4527 Note that we base it on the count plus 1, since we will null terminate
4528 the vector allocated based on this size. However, the ELF symbol table
4529 always has a dummy entry as symbol #0, so it ends up even. */
4532 _bfd_elf_get_symtab_upper_bound (abfd
)
4537 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4539 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4540 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4546 _bfd_elf_get_dynamic_symtab_upper_bound (abfd
)
4551 Elf_Internal_Shdr
*hdr
= &elf_tdata (abfd
)->dynsymtab_hdr
;
4553 if (elf_dynsymtab (abfd
) == 0)
4555 bfd_set_error (bfd_error_invalid_operation
);
4559 symcount
= hdr
->sh_size
/ get_elf_backend_data (abfd
)->s
->sizeof_sym
;
4560 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
*));
4566 _bfd_elf_get_reloc_upper_bound (abfd
, asect
)
4567 bfd
*abfd ATTRIBUTE_UNUSED
;
4570 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
4573 /* Canonicalize the relocs. */
4576 _bfd_elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
4585 if (! get_elf_backend_data (abfd
)->s
->slurp_reloc_table (abfd
,
4591 tblptr
= section
->relocation
;
4592 for (i
= 0; i
< section
->reloc_count
; i
++)
4593 *relptr
++ = tblptr
++;
4597 return section
->reloc_count
;
4601 _bfd_elf_get_symtab (abfd
, alocation
)
4603 asymbol
**alocation
;
4605 long symcount
= get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4606 (abfd
, alocation
, false);
4609 bfd_get_symcount (abfd
) = symcount
;
4614 _bfd_elf_canonicalize_dynamic_symtab (abfd
, alocation
)
4616 asymbol
**alocation
;
4618 return get_elf_backend_data (abfd
)->s
->slurp_symbol_table
4619 (abfd
, alocation
, true);
4622 /* Return the size required for the dynamic reloc entries. Any
4623 section that was actually installed in the BFD, and has type
4624 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4625 considered to be a dynamic reloc section. */
4628 _bfd_elf_get_dynamic_reloc_upper_bound (abfd
)
4634 if (elf_dynsymtab (abfd
) == 0)
4636 bfd_set_error (bfd_error_invalid_operation
);
4640 ret
= sizeof (arelent
*);
4641 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4642 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4643 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4644 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4645 ret
+= ((s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
)
4646 * sizeof (arelent
*));
4651 /* Canonicalize the dynamic relocation entries. Note that we return
4652 the dynamic relocations as a single block, although they are
4653 actually associated with particular sections; the interface, which
4654 was designed for SunOS style shared libraries, expects that there
4655 is only one set of dynamic relocs. Any section that was actually
4656 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4657 the dynamic symbol table, is considered to be a dynamic reloc
4661 _bfd_elf_canonicalize_dynamic_reloc (abfd
, storage
, syms
)
4666 boolean (*slurp_relocs
) PARAMS ((bfd
*, asection
*, asymbol
**, boolean
));
4670 if (elf_dynsymtab (abfd
) == 0)
4672 bfd_set_error (bfd_error_invalid_operation
);
4676 slurp_relocs
= get_elf_backend_data (abfd
)->s
->slurp_reloc_table
;
4678 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
4680 if (elf_section_data (s
)->this_hdr
.sh_link
== elf_dynsymtab (abfd
)
4681 && (elf_section_data (s
)->this_hdr
.sh_type
== SHT_REL
4682 || elf_section_data (s
)->this_hdr
.sh_type
== SHT_RELA
))
4687 if (! (*slurp_relocs
) (abfd
, s
, syms
, true))
4689 count
= s
->_raw_size
/ elf_section_data (s
)->this_hdr
.sh_entsize
;
4691 for (i
= 0; i
< count
; i
++)
4702 /* Read in the version information. */
4705 _bfd_elf_slurp_version_tables (abfd
)
4708 bfd_byte
*contents
= NULL
;
4710 if (elf_dynverdef (abfd
) != 0)
4712 Elf_Internal_Shdr
*hdr
;
4713 Elf_External_Verdef
*everdef
;
4714 Elf_Internal_Verdef
*iverdef
;
4715 Elf_Internal_Verdef
*iverdefarr
;
4716 Elf_Internal_Verdef iverdefmem
;
4720 hdr
= &elf_tdata (abfd
)->dynverdef_hdr
;
4722 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4723 if (contents
== NULL
)
4725 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4726 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4729 /* We know the number of entries in the section but not the maximum
4730 index. Therefore we have to run through all entries and find
4732 everdef
= (Elf_External_Verdef
*) contents
;
4734 for (i
= 0; i
< hdr
->sh_info
; ++i
)
4736 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4738 if ((iverdefmem
.vd_ndx
& VERSYM_VERSION
) > maxidx
)
4739 maxidx
= iverdefmem
.vd_ndx
& VERSYM_VERSION
;
4741 everdef
= ((Elf_External_Verdef
*)
4742 ((bfd_byte
*) everdef
+ iverdefmem
.vd_next
));
4745 elf_tdata (abfd
)->verdef
=
4746 ((Elf_Internal_Verdef
*)
4747 bfd_zalloc (abfd
, maxidx
* sizeof (Elf_Internal_Verdef
)));
4748 if (elf_tdata (abfd
)->verdef
== NULL
)
4751 elf_tdata (abfd
)->cverdefs
= maxidx
;
4753 everdef
= (Elf_External_Verdef
*) contents
;
4754 iverdefarr
= elf_tdata (abfd
)->verdef
;
4755 for (i
= 0; i
< hdr
->sh_info
; i
++)
4757 Elf_External_Verdaux
*everdaux
;
4758 Elf_Internal_Verdaux
*iverdaux
;
4761 _bfd_elf_swap_verdef_in (abfd
, everdef
, &iverdefmem
);
4763 iverdef
= &iverdefarr
[(iverdefmem
.vd_ndx
& VERSYM_VERSION
) - 1];
4764 memcpy (iverdef
, &iverdefmem
, sizeof (Elf_Internal_Verdef
));
4766 iverdef
->vd_bfd
= abfd
;
4768 iverdef
->vd_auxptr
= ((Elf_Internal_Verdaux
*)
4771 * sizeof (Elf_Internal_Verdaux
))));
4772 if (iverdef
->vd_auxptr
== NULL
)
4775 everdaux
= ((Elf_External_Verdaux
*)
4776 ((bfd_byte
*) everdef
+ iverdef
->vd_aux
));
4777 iverdaux
= iverdef
->vd_auxptr
;
4778 for (j
= 0; j
< iverdef
->vd_cnt
; j
++, iverdaux
++)
4780 _bfd_elf_swap_verdaux_in (abfd
, everdaux
, iverdaux
);
4782 iverdaux
->vda_nodename
=
4783 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4784 iverdaux
->vda_name
);
4785 if (iverdaux
->vda_nodename
== NULL
)
4788 if (j
+ 1 < iverdef
->vd_cnt
)
4789 iverdaux
->vda_nextptr
= iverdaux
+ 1;
4791 iverdaux
->vda_nextptr
= NULL
;
4793 everdaux
= ((Elf_External_Verdaux
*)
4794 ((bfd_byte
*) everdaux
+ iverdaux
->vda_next
));
4797 iverdef
->vd_nodename
= iverdef
->vd_auxptr
->vda_nodename
;
4799 if (i
+ 1 < hdr
->sh_info
)
4800 iverdef
->vd_nextdef
= iverdef
+ 1;
4802 iverdef
->vd_nextdef
= NULL
;
4804 everdef
= ((Elf_External_Verdef
*)
4805 ((bfd_byte
*) everdef
+ iverdef
->vd_next
));
4812 if (elf_dynverref (abfd
) != 0)
4814 Elf_Internal_Shdr
*hdr
;
4815 Elf_External_Verneed
*everneed
;
4816 Elf_Internal_Verneed
*iverneed
;
4819 hdr
= &elf_tdata (abfd
)->dynverref_hdr
;
4821 elf_tdata (abfd
)->verref
=
4822 ((Elf_Internal_Verneed
*)
4823 bfd_zalloc (abfd
, hdr
->sh_info
* sizeof (Elf_Internal_Verneed
)));
4824 if (elf_tdata (abfd
)->verref
== NULL
)
4827 elf_tdata (abfd
)->cverrefs
= hdr
->sh_info
;
4829 contents
= (bfd_byte
*) bfd_malloc (hdr
->sh_size
);
4830 if (contents
== NULL
)
4832 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) != 0
4833 || bfd_read ((PTR
) contents
, 1, hdr
->sh_size
, abfd
) != hdr
->sh_size
)
4836 everneed
= (Elf_External_Verneed
*) contents
;
4837 iverneed
= elf_tdata (abfd
)->verref
;
4838 for (i
= 0; i
< hdr
->sh_info
; i
++, iverneed
++)
4840 Elf_External_Vernaux
*evernaux
;
4841 Elf_Internal_Vernaux
*ivernaux
;
4844 _bfd_elf_swap_verneed_in (abfd
, everneed
, iverneed
);
4846 iverneed
->vn_bfd
= abfd
;
4848 iverneed
->vn_filename
=
4849 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4851 if (iverneed
->vn_filename
== NULL
)
4854 iverneed
->vn_auxptr
=
4855 ((Elf_Internal_Vernaux
*)
4857 iverneed
->vn_cnt
* sizeof (Elf_Internal_Vernaux
)));
4859 evernaux
= ((Elf_External_Vernaux
*)
4860 ((bfd_byte
*) everneed
+ iverneed
->vn_aux
));
4861 ivernaux
= iverneed
->vn_auxptr
;
4862 for (j
= 0; j
< iverneed
->vn_cnt
; j
++, ivernaux
++)
4864 _bfd_elf_swap_vernaux_in (abfd
, evernaux
, ivernaux
);
4866 ivernaux
->vna_nodename
=
4867 bfd_elf_string_from_elf_section (abfd
, hdr
->sh_link
,
4868 ivernaux
->vna_name
);
4869 if (ivernaux
->vna_nodename
== NULL
)
4872 if (j
+ 1 < iverneed
->vn_cnt
)
4873 ivernaux
->vna_nextptr
= ivernaux
+ 1;
4875 ivernaux
->vna_nextptr
= NULL
;
4877 evernaux
= ((Elf_External_Vernaux
*)
4878 ((bfd_byte
*) evernaux
+ ivernaux
->vna_next
));
4881 if (i
+ 1 < hdr
->sh_info
)
4882 iverneed
->vn_nextref
= iverneed
+ 1;
4884 iverneed
->vn_nextref
= NULL
;
4886 everneed
= ((Elf_External_Verneed
*)
4887 ((bfd_byte
*) everneed
+ iverneed
->vn_next
));
4897 if (contents
== NULL
)
4903 _bfd_elf_make_empty_symbol (abfd
)
4906 elf_symbol_type
*newsym
;
4908 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
4913 newsym
->symbol
.the_bfd
= abfd
;
4914 return &newsym
->symbol
;
4919 _bfd_elf_get_symbol_info (ignore_abfd
, symbol
, ret
)
4920 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4924 bfd_symbol_info (symbol
, ret
);
4927 /* Return whether a symbol name implies a local symbol. Most targets
4928 use this function for the is_local_label_name entry point, but some
4932 _bfd_elf_is_local_label_name (abfd
, name
)
4933 bfd
*abfd ATTRIBUTE_UNUSED
;
4936 /* Normal local symbols start with ``.L''. */
4937 if (name
[0] == '.' && name
[1] == 'L')
4940 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4941 DWARF debugging symbols starting with ``..''. */
4942 if (name
[0] == '.' && name
[1] == '.')
4945 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4946 emitting DWARF debugging output. I suspect this is actually a
4947 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4948 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4949 underscore to be emitted on some ELF targets). For ease of use,
4950 we treat such symbols as local. */
4951 if (name
[0] == '_' && name
[1] == '.' && name
[2] == 'L' && name
[3] == '_')
4958 _bfd_elf_get_lineno (ignore_abfd
, symbol
)
4959 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
4960 asymbol
*symbol ATTRIBUTE_UNUSED
;
4967 _bfd_elf_set_arch_mach (abfd
, arch
, machine
)
4969 enum bfd_architecture arch
;
4970 unsigned long machine
;
4972 /* If this isn't the right architecture for this backend, and this
4973 isn't the generic backend, fail. */
4974 if (arch
!= get_elf_backend_data (abfd
)->arch
4975 && arch
!= bfd_arch_unknown
4976 && get_elf_backend_data (abfd
)->arch
!= bfd_arch_unknown
)
4979 return bfd_default_set_arch_mach (abfd
, arch
, machine
);
4982 /* Find the function to a particular section and offset,
4983 for error reporting. */
4986 elf_find_function (abfd
, section
, symbols
, offset
,
4987 filename_ptr
, functionname_ptr
)
4988 bfd
*abfd ATTRIBUTE_UNUSED
;
4992 CONST
char **filename_ptr
;
4993 CONST
char **functionname_ptr
;
4995 const char *filename
;
5004 for (p
= symbols
; *p
!= NULL
; p
++)
5008 q
= (elf_symbol_type
*) *p
;
5010 if (bfd_get_section (&q
->symbol
) != section
)
5013 switch (ELF_ST_TYPE (q
->internal_elf_sym
.st_info
))
5018 filename
= bfd_asymbol_name (&q
->symbol
);
5022 if (q
->symbol
.section
== section
5023 && q
->symbol
.value
>= low_func
5024 && q
->symbol
.value
<= offset
)
5026 func
= (asymbol
*) q
;
5027 low_func
= q
->symbol
.value
;
5037 *filename_ptr
= filename
;
5038 if (functionname_ptr
)
5039 *functionname_ptr
= bfd_asymbol_name (func
);
5044 /* Find the nearest line to a particular section and offset,
5045 for error reporting. */
5048 _bfd_elf_find_nearest_line (abfd
, section
, symbols
, offset
,
5049 filename_ptr
, functionname_ptr
, line_ptr
)
5054 CONST
char **filename_ptr
;
5055 CONST
char **functionname_ptr
;
5056 unsigned int *line_ptr
;
5060 if (_bfd_dwarf1_find_nearest_line (abfd
, section
, symbols
, offset
,
5061 filename_ptr
, functionname_ptr
,
5064 if (!*functionname_ptr
)
5065 elf_find_function (abfd
, section
, symbols
, offset
,
5066 *filename_ptr
? NULL
: filename_ptr
,
5072 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
5073 filename_ptr
, functionname_ptr
,
5075 &elf_tdata (abfd
)->dwarf2_find_line_info
))
5077 if (!*functionname_ptr
)
5078 elf_find_function (abfd
, section
, symbols
, offset
,
5079 *filename_ptr
? NULL
: filename_ptr
,
5085 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
5086 &found
, filename_ptr
,
5087 functionname_ptr
, line_ptr
,
5088 &elf_tdata (abfd
)->line_info
))
5093 if (symbols
== NULL
)
5096 if (! elf_find_function (abfd
, section
, symbols
, offset
,
5097 filename_ptr
, functionname_ptr
))
5105 _bfd_elf_sizeof_headers (abfd
, reloc
)
5111 ret
= get_elf_backend_data (abfd
)->s
->sizeof_ehdr
;
5113 ret
+= get_program_header_size (abfd
);
5118 _bfd_elf_set_section_contents (abfd
, section
, location
, offset
, count
)
5123 bfd_size_type count
;
5125 Elf_Internal_Shdr
*hdr
;
5127 if (! abfd
->output_has_begun
5128 && ! _bfd_elf_compute_section_file_positions
5129 (abfd
, (struct bfd_link_info
*) NULL
))
5132 hdr
= &elf_section_data (section
)->this_hdr
;
5134 if (bfd_seek (abfd
, hdr
->sh_offset
+ offset
, SEEK_SET
) == -1)
5136 if (bfd_write (location
, 1, count
, abfd
) != count
)
5143 _bfd_elf_no_info_to_howto (abfd
, cache_ptr
, dst
)
5144 bfd
*abfd ATTRIBUTE_UNUSED
;
5145 arelent
*cache_ptr ATTRIBUTE_UNUSED
;
5146 Elf_Internal_Rela
*dst ATTRIBUTE_UNUSED
;
5153 _bfd_elf_no_info_to_howto_rel (abfd
, cache_ptr
, dst
)
5156 Elf_Internal_Rel
*dst
;
5162 /* Try to convert a non-ELF reloc into an ELF one. */
5165 _bfd_elf_validate_reloc (abfd
, areloc
)
5169 /* Check whether we really have an ELF howto. */
5171 if ((*areloc
->sym_ptr_ptr
)->the_bfd
->xvec
!= abfd
->xvec
)
5173 bfd_reloc_code_real_type code
;
5174 reloc_howto_type
*howto
;
5176 /* Alien reloc: Try to determine its type to replace it with an
5177 equivalent ELF reloc. */
5179 if (areloc
->howto
->pc_relative
)
5181 switch (areloc
->howto
->bitsize
)
5184 code
= BFD_RELOC_8_PCREL
;
5187 code
= BFD_RELOC_12_PCREL
;
5190 code
= BFD_RELOC_16_PCREL
;
5193 code
= BFD_RELOC_24_PCREL
;
5196 code
= BFD_RELOC_32_PCREL
;
5199 code
= BFD_RELOC_64_PCREL
;
5205 howto
= bfd_reloc_type_lookup (abfd
, code
);
5207 if (areloc
->howto
->pcrel_offset
!= howto
->pcrel_offset
)
5209 if (howto
->pcrel_offset
)
5210 areloc
->addend
+= areloc
->address
;
5212 areloc
->addend
-= areloc
->address
; /* addend is unsigned!! */
5217 switch (areloc
->howto
->bitsize
)
5223 code
= BFD_RELOC_14
;
5226 code
= BFD_RELOC_16
;
5229 code
= BFD_RELOC_26
;
5232 code
= BFD_RELOC_32
;
5235 code
= BFD_RELOC_64
;
5241 howto
= bfd_reloc_type_lookup (abfd
, code
);
5245 areloc
->howto
= howto
;
5253 (*_bfd_error_handler
)
5254 (_("%s: unsupported relocation type %s"),
5255 bfd_get_filename (abfd
), areloc
->howto
->name
);
5256 bfd_set_error (bfd_error_bad_value
);
5261 _bfd_elf_close_and_cleanup (abfd
)
5264 if (bfd_get_format (abfd
) == bfd_object
)
5266 if (elf_shstrtab (abfd
) != NULL
)
5267 _bfd_stringtab_free (elf_shstrtab (abfd
));
5270 return _bfd_generic_close_and_cleanup (abfd
);
5273 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5274 in the relocation's offset. Thus we cannot allow any sort of sanity
5275 range-checking to interfere. There is nothing else to do in processing
5278 bfd_reloc_status_type
5279 _bfd_elf_rel_vtable_reloc_fn (abfd
, re
, symbol
, data
, is
, obfd
, errmsg
)
5280 bfd
*abfd ATTRIBUTE_UNUSED
;
5281 arelent
*re ATTRIBUTE_UNUSED
;
5282 struct symbol_cache_entry
*symbol ATTRIBUTE_UNUSED
;
5283 PTR data ATTRIBUTE_UNUSED
;
5284 asection
*is ATTRIBUTE_UNUSED
;
5285 bfd
*obfd ATTRIBUTE_UNUSED
;
5286 char **errmsg ATTRIBUTE_UNUSED
;
5288 return bfd_reloc_ok
;
5291 /* Elf core file support. Much of this only works on native
5292 toolchains, since we rely on knowing the
5293 machine-dependent procfs structure in order to pick
5294 out details about the corefile. */
5296 #ifdef HAVE_SYS_PROCFS_H
5297 # include <sys/procfs.h>
5300 /* Define offsetof for those systems which lack it. */
5303 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5306 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5309 elfcore_make_pid (abfd
)
5312 return ((elf_tdata (abfd
)->core_lwpid
<< 16)
5313 + (elf_tdata (abfd
)->core_pid
));
5316 /* If there isn't a section called NAME, make one, using
5317 data from SECT. Note, this function will generate a
5318 reference to NAME, so you shouldn't deallocate or
5322 elfcore_maybe_make_sect (abfd
, name
, sect
)
5329 if (bfd_get_section_by_name (abfd
, name
) != NULL
)
5332 sect2
= bfd_make_section (abfd
, name
);
5336 sect2
->_raw_size
= sect
->_raw_size
;
5337 sect2
->filepos
= sect
->filepos
;
5338 sect2
->flags
= sect
->flags
;
5339 sect2
->alignment_power
= sect
->alignment_power
;
5343 /* prstatus_t exists on:
5345 linux 2.[01] + glibc
5349 #if defined (HAVE_PRSTATUS_T)
5351 elfcore_grok_prstatus (abfd
, note
)
5353 Elf_Internal_Note
*note
;
5361 if (note
->descsz
== sizeof (prstatus_t
))
5365 raw_size
= sizeof (prstat
.pr_reg
);
5366 offset
= offsetof (prstatus_t
, pr_reg
);
5367 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5369 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5370 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5372 /* pr_who exists on:
5375 pr_who doesn't exist on:
5378 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5379 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5382 #if defined (HAVE_PRSTATUS32_T)
5383 else if (note
->descsz
== sizeof (prstatus32_t
))
5385 /* 64-bit host, 32-bit corefile */
5386 prstatus32_t prstat
;
5388 raw_size
= sizeof (prstat
.pr_reg
);
5389 offset
= offsetof (prstatus32_t
, pr_reg
);
5390 memcpy (&prstat
, note
->descdata
, sizeof (prstat
));
5392 elf_tdata (abfd
)->core_signal
= prstat
.pr_cursig
;
5393 elf_tdata (abfd
)->core_pid
= prstat
.pr_pid
;
5395 /* pr_who exists on:
5398 pr_who doesn't exist on:
5401 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5402 elf_tdata (abfd
)->core_lwpid
= prstat
.pr_who
;
5405 #endif /* HAVE_PRSTATUS32_T */
5408 /* Fail - we don't know how to handle any other
5409 note size (ie. data object type). */
5413 /* Make a ".reg/999" section. */
5415 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5416 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5421 sect
= bfd_make_section (abfd
, name
);
5425 sect
->_raw_size
= raw_size
;
5426 sect
->filepos
= note
->descpos
+ offset
;
5428 sect
->flags
= SEC_HAS_CONTENTS
;
5429 sect
->alignment_power
= 2;
5431 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5436 #endif /* defined (HAVE_PRSTATUS_T) */
5438 /* Create a pseudosection containing the exact contents of NOTE. This
5439 actually creates up to two pseudosections:
5440 - For the single-threaded case, a section named NAME, unless
5441 such a section already exists.
5442 - For the multi-threaded case, a section named "NAME/PID", where
5443 PID is elfcore_make_pid (abfd).
5444 Both pseudosections have identical contents: the contents of NOTE. */
5447 elfcore_make_note_pseudosection (abfd
, name
, note
)
5450 Elf_Internal_Note
*note
;
5453 char *threaded_name
;
5456 /* Build the section name. */
5458 sprintf (buf
, "%s/%d", name
, elfcore_make_pid (abfd
));
5459 threaded_name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5460 if (threaded_name
== NULL
)
5462 strcpy (threaded_name
, buf
);
5464 sect
= bfd_make_section (abfd
, threaded_name
);
5467 sect
->_raw_size
= note
->descsz
;
5468 sect
->filepos
= note
->descpos
;
5469 sect
->flags
= SEC_HAS_CONTENTS
;
5470 sect
->alignment_power
= 2;
5472 if (! elfcore_maybe_make_sect (abfd
, name
, sect
))
5478 /* There isn't a consistent prfpregset_t across platforms,
5479 but it doesn't matter, because we don't have to pick this
5480 data structure apart. */
5483 elfcore_grok_prfpreg (abfd
, note
)
5485 Elf_Internal_Note
*note
;
5487 return elfcore_make_note_pseudosection (abfd
, ".reg2", note
);
5490 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5491 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5495 elfcore_grok_prxfpreg (abfd
, note
)
5497 Elf_Internal_Note
*note
;
5499 return elfcore_make_note_pseudosection (abfd
, ".reg-xfp", note
);
5502 #if defined (HAVE_PRPSINFO_T)
5503 typedef prpsinfo_t elfcore_psinfo_t
;
5504 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5505 typedef prpsinfo32_t elfcore_psinfo32_t
;
5509 #if defined (HAVE_PSINFO_T)
5510 typedef psinfo_t elfcore_psinfo_t
;
5511 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5512 typedef psinfo32_t elfcore_psinfo32_t
;
5516 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5518 /* return a malloc'ed copy of a string at START which is at
5519 most MAX bytes long, possibly without a terminating '\0'.
5520 the copy will always have a terminating '\0'. */
5523 elfcore_strndup (abfd
, start
, max
)
5529 char *end
= memchr (start
, '\0', max
);
5537 dup
= bfd_alloc (abfd
, len
+ 1);
5541 memcpy (dup
, start
, len
);
5548 elfcore_grok_psinfo (abfd
, note
)
5550 Elf_Internal_Note
*note
;
5552 if (note
->descsz
== sizeof (elfcore_psinfo_t
))
5554 elfcore_psinfo_t psinfo
;
5556 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5558 elf_tdata (abfd
)->core_program
5559 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5561 elf_tdata (abfd
)->core_command
5562 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5564 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5565 else if (note
->descsz
== sizeof (elfcore_psinfo32_t
))
5567 /* 64-bit host, 32-bit corefile */
5568 elfcore_psinfo32_t psinfo
;
5570 memcpy (&psinfo
, note
->descdata
, sizeof (psinfo
));
5572 elf_tdata (abfd
)->core_program
5573 = elfcore_strndup (abfd
, psinfo
.pr_fname
, sizeof (psinfo
.pr_fname
));
5575 elf_tdata (abfd
)->core_command
5576 = elfcore_strndup (abfd
, psinfo
.pr_psargs
, sizeof (psinfo
.pr_psargs
));
5582 /* Fail - we don't know how to handle any other
5583 note size (ie. data object type). */
5587 /* Note that for some reason, a spurious space is tacked
5588 onto the end of the args in some (at least one anyway)
5589 implementations, so strip it off if it exists. */
5592 char *command
= elf_tdata (abfd
)->core_command
;
5593 int n
= strlen (command
);
5595 if (0 < n
&& command
[n
- 1] == ' ')
5596 command
[n
- 1] = '\0';
5601 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5603 #if defined (HAVE_PSTATUS_T)
5605 elfcore_grok_pstatus (abfd
, note
)
5607 Elf_Internal_Note
*note
;
5609 if (note
->descsz
== sizeof (pstatus_t
)
5610 #if defined (HAVE_PXSTATUS_T)
5611 || note
->descsz
== sizeof (pxstatus_t
)
5617 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5619 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5621 #if defined (HAVE_PSTATUS32_T)
5622 else if (note
->descsz
== sizeof (pstatus32_t
))
5624 /* 64-bit host, 32-bit corefile */
5627 memcpy (&pstat
, note
->descdata
, sizeof (pstat
));
5629 elf_tdata (abfd
)->core_pid
= pstat
.pr_pid
;
5632 /* Could grab some more details from the "representative"
5633 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5634 NT_LWPSTATUS note, presumably. */
5638 #endif /* defined (HAVE_PSTATUS_T) */
5640 #if defined (HAVE_LWPSTATUS_T)
5642 elfcore_grok_lwpstatus (abfd
, note
)
5644 Elf_Internal_Note
*note
;
5646 lwpstatus_t lwpstat
;
5651 if (note
->descsz
!= sizeof (lwpstat
)
5652 #if defined (HAVE_LWPXSTATUS_T)
5653 && note
->descsz
!= sizeof (lwpxstatus_t
)
5658 memcpy (&lwpstat
, note
->descdata
, sizeof (lwpstat
));
5660 elf_tdata (abfd
)->core_lwpid
= lwpstat
.pr_lwpid
;
5661 elf_tdata (abfd
)->core_signal
= lwpstat
.pr_cursig
;
5663 /* Make a ".reg/999" section. */
5665 sprintf (buf
, ".reg/%d", elfcore_make_pid (abfd
));
5666 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5671 sect
= bfd_make_section (abfd
, name
);
5675 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5676 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.gregs
);
5677 sect
->filepos
= note
->descpos
5678 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.gregs
);
5681 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5682 sect
->_raw_size
= sizeof (lwpstat
.pr_reg
);
5683 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_reg
);
5686 sect
->flags
= SEC_HAS_CONTENTS
;
5687 sect
->alignment_power
= 2;
5689 if (!elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5692 /* Make a ".reg2/999" section */
5694 sprintf (buf
, ".reg2/%d", elfcore_make_pid (abfd
));
5695 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5700 sect
= bfd_make_section (abfd
, name
);
5704 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5705 sect
->_raw_size
= sizeof (lwpstat
.pr_context
.uc_mcontext
.fpregs
);
5706 sect
->filepos
= note
->descpos
5707 + offsetof (lwpstatus_t
, pr_context
.uc_mcontext
.fpregs
);
5710 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5711 sect
->_raw_size
= sizeof (lwpstat
.pr_fpreg
);
5712 sect
->filepos
= note
->descpos
+ offsetof (lwpstatus_t
, pr_fpreg
);
5715 sect
->flags
= SEC_HAS_CONTENTS
;
5716 sect
->alignment_power
= 2;
5718 if (!elfcore_maybe_make_sect (abfd
, ".reg2", sect
))
5723 #endif /* defined (HAVE_LWPSTATUS_T) */
5725 #if defined (HAVE_WIN32_PSTATUS_T)
5727 elfcore_grok_win32pstatus (abfd
, note
)
5729 Elf_Internal_Note
*note
;
5734 win32_pstatus_t pstatus
;
5736 if (note
->descsz
< sizeof (pstatus
))
5739 memcpy (&pstatus
, note
->descdata
, note
->descsz
);
5741 switch (pstatus
.data_type
)
5743 case NOTE_INFO_PROCESS
:
5744 /* FIXME: need to add ->core_command. */
5745 elf_tdata (abfd
)->core_signal
= pstatus
.data
.process_info
.signal
;
5746 elf_tdata (abfd
)->core_pid
= pstatus
.data
.process_info
.pid
;
5749 case NOTE_INFO_THREAD
:
5750 /* Make a ".reg/999" section. */
5751 sprintf (buf
, ".reg/%d", pstatus
.data
.thread_info
.tid
);
5753 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5759 sect
= bfd_make_section (abfd
, name
);
5763 sect
->_raw_size
= sizeof (pstatus
.data
.thread_info
.thread_context
);
5764 sect
->filepos
= note
->descpos
+ offsetof (struct win32_pstatus
,
5765 data
.thread_info
.thread_context
);
5766 sect
->flags
= SEC_HAS_CONTENTS
;
5767 sect
->alignment_power
= 2;
5769 if (pstatus
.data
.thread_info
.is_active_thread
)
5770 if (! elfcore_maybe_make_sect (abfd
, ".reg", sect
))
5774 case NOTE_INFO_MODULE
:
5775 /* Make a ".module/xxxxxxxx" section. */
5776 sprintf (buf
, ".module/%08x", pstatus
.data
.module_info
.base_address
);
5778 name
= bfd_alloc (abfd
, strlen (buf
) + 1);
5784 sect
= bfd_make_section (abfd
, name
);
5789 sect
->_raw_size
= note
->descsz
;
5790 sect
->filepos
= note
->descpos
;
5791 sect
->flags
= SEC_HAS_CONTENTS
;
5792 sect
->alignment_power
= 2;
5801 #endif /* HAVE_WIN32_PSTATUS_T */
5804 elfcore_grok_note (abfd
, note
)
5806 Elf_Internal_Note
*note
;
5813 #if defined (HAVE_PRSTATUS_T)
5815 return elfcore_grok_prstatus (abfd
, note
);
5818 #if defined (HAVE_PSTATUS_T)
5820 return elfcore_grok_pstatus (abfd
, note
);
5823 #if defined (HAVE_LWPSTATUS_T)
5825 return elfcore_grok_lwpstatus (abfd
, note
);
5828 case NT_FPREGSET
: /* FIXME: rename to NT_PRFPREG */
5829 return elfcore_grok_prfpreg (abfd
, note
);
5831 #if defined (HAVE_WIN32_PSTATUS_T)
5832 case NT_WIN32PSTATUS
:
5833 return elfcore_grok_win32pstatus (abfd
, note
);
5836 case NT_PRXFPREG
: /* Linux SSE extension */
5837 if (note
->namesz
== 5
5838 && ! strcmp (note
->namedata
, "LINUX"))
5839 return elfcore_grok_prxfpreg (abfd
, note
);
5843 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5846 return elfcore_grok_psinfo (abfd
, note
);
5852 elfcore_read_notes (abfd
, offset
, size
)
5863 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
5866 buf
= bfd_malloc ((size_t) size
);
5870 if (bfd_read (buf
, size
, 1, abfd
) != size
)
5878 while (p
< buf
+ size
)
5880 /* FIXME: bad alignment assumption. */
5881 Elf_External_Note
*xnp
= (Elf_External_Note
*) p
;
5882 Elf_Internal_Note in
;
5884 in
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->type
);
5886 in
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->namesz
);
5887 in
.namedata
= xnp
->name
;
5889 in
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) xnp
->descsz
);
5890 in
.descdata
= in
.namedata
+ BFD_ALIGN (in
.namesz
, 4);
5891 in
.descpos
= offset
+ (in
.descdata
- buf
);
5893 if (! elfcore_grok_note (abfd
, &in
))
5896 p
= in
.descdata
+ BFD_ALIGN (in
.descsz
, 4);
5903 /* FIXME: This function is now unnecessary. Callers can just call
5904 bfd_section_from_phdr directly. */
5907 _bfd_elfcore_section_from_phdr (abfd
, phdr
, sec_num
)
5909 Elf_Internal_Phdr
* phdr
;
5912 if (! bfd_section_from_phdr (abfd
, phdr
, sec_num
))
5918 /* Providing external access to the ELF program header table. */
5920 /* Return an upper bound on the number of bytes required to store a
5921 copy of ABFD's program header table entries. Return -1 if an error
5922 occurs; bfd_get_error will return an appropriate code. */
5925 bfd_get_elf_phdr_upper_bound (abfd
)
5928 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5930 bfd_set_error (bfd_error_wrong_format
);
5934 return (elf_elfheader (abfd
)->e_phnum
5935 * sizeof (Elf_Internal_Phdr
));
5938 /* Copy ABFD's program header table entries to *PHDRS. The entries
5939 will be stored as an array of Elf_Internal_Phdr structures, as
5940 defined in include/elf/internal.h. To find out how large the
5941 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5943 Return the number of program header table entries read, or -1 if an
5944 error occurs; bfd_get_error will return an appropriate code. */
5947 bfd_get_elf_phdrs (abfd
, phdrs
)
5953 if (abfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
5955 bfd_set_error (bfd_error_wrong_format
);
5959 num_phdrs
= elf_elfheader (abfd
)->e_phnum
;
5960 memcpy (phdrs
, elf_tdata (abfd
)->phdr
,
5961 num_phdrs
* sizeof (Elf_Internal_Phdr
));