1 /* ELF support for BFD.
2 Copyright (C) 1991 Free Software Foundation, Inc.
4 Written by Fred Fish @ Cygnus Support, from information published
5 in "UNIX System V Release 4, Programmers Guide: ANSI C and
6 Programming Support Tools".
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /****************************************
29 This is only a partial ELF implementation,
30 incorporating only those parts that are
31 required to get gdb up and running. It is
32 expected that it will be expanded to a full
33 ELF implementation at some future date.
35 Unimplemented stubs call abort() to ensure
36 that they get proper attention if they are
37 ever called. The stubs are here since
38 this version was hacked from the COFF
39 version, and thus they will probably
40 go away or get expanded appropriately in a
45 *****************************************/
48 /* Problems and other issues to resolve.
50 (1) BFD expects there to be some fixed number of "sections" in
51 the object file. I.E. there is a "section_count" variable in the
52 bfd structure which contains the number of sections. However, ELF
53 supports multiple "views" of a file. In particular, with current
54 implementations, executable files typically have two tables, a
55 program header table and a section header table, both of which
56 partition the executable.
58 In ELF-speak, the "linking view" of the file uses the section header
59 table to access "sections" within the file, and the "execution view"
60 uses the program header table to access "segments" within the file.
61 "Segments" typically may contain all the data from one or more
64 Note that the section header table is optional in ELF executables,
65 but it is this information that is most useful to gdb. If the
66 section header table is missing, then gdb should probably try
67 to make do with the program header table. (FIXME)
76 #include "elf-common.h"
77 #include "elf-internal.h"
78 #include "elf-external.h"
80 /* Forward data declarations */
81 extern bfd_target elf_little_vec
, elf_big_vec
;
83 /* Translate an ELF header in external format into an ELF header in internal
87 DEFUN(bfd_swap_ehdr_in
,(abfd
, src
, dst
),
89 Elf_External_Ehdr
*src AND
90 Elf_Internal_Ehdr
*dst
)
92 bcopy (src
-> e_ident
, dst
-> e_ident
, EI_NIDENT
);
93 dst
-> e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_type
);
94 dst
-> e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_machine
);
95 dst
-> e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_version
);
96 dst
-> e_entry
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_entry
);
97 dst
-> e_phoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_phoff
);
98 dst
-> e_shoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_shoff
);
99 dst
-> e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_flags
);
100 dst
-> e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_ehsize
);
101 dst
-> e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phentsize
);
102 dst
-> e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phnum
);
103 dst
-> e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shentsize
);
104 dst
-> e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shnum
);
105 dst
-> e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shstrndx
);
109 /* Translate an ELF section header table entry in external format into an
110 ELF section header table entry in internal format. */
113 DEFUN(bfd_swap_shdr_in
,(abfd
, src
, dst
),
115 Elf_External_Shdr
*src AND
116 Elf_Internal_Shdr
*dst
)
118 dst
-> sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_name
);
119 dst
-> sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_type
);
120 dst
-> sh_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_flags
);
121 dst
-> sh_addr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_addr
);
122 dst
-> sh_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_offset
);
123 dst
-> sh_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_size
);
124 dst
-> sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_link
);
125 dst
-> sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_info
);
126 dst
-> sh_addralign
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_addralign
);
127 dst
-> sh_entsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_entsize
);
131 /* Create a new bfd section from an ELF section header. */
134 DEFUN(bfd_section_from_shdr
, (abfd
, hdr
, shstrtab
),
136 Elf_Internal_Shdr
*hdr AND
142 name
= hdr
-> sh_name
? shstrtab
+ hdr
-> sh_name
: "unnamed";
143 newsect
= bfd_make_section (abfd
, name
);
144 newsect
-> vma
= hdr
-> sh_addr
;
145 newsect
-> size
= hdr
-> sh_size
;
146 if (!(hdr
-> sh_type
== SHT_NOBITS
))
148 newsect
-> filepos
= hdr
-> sh_offset
;
149 newsect
-> flags
|= SEC_HAS_CONTENTS
;
151 if (hdr
-> sh_flags
& SHF_ALLOC
)
153 newsect
-> flags
|= SEC_ALLOC
;
154 if (hdr
-> sh_type
!= SHT_NOBITS
)
156 newsect
-> flags
|= SEC_LOAD
;
159 if (!(hdr
-> sh_flags
& SHF_WRITE
))
161 newsect
-> flags
|= SEC_READONLY
;
163 if (hdr
-> sh_flags
& SHF_EXECINSTR
)
165 newsect
-> flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
167 if (hdr
-> sh_type
== SHT_SYMTAB
)
169 abfd
-> flags
|= HAS_SYMS
;
175 /* Begin processing a given object.
177 First we validate the file by reading in the ELF header and checking
183 DEFUN (elf_object_p
, (abfd
), bfd
*abfd
)
185 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
186 Elf_Internal_Ehdr i_ehdr
; /* Elf file header, internal form */
187 Elf_External_Shdr
*x_shdr
; /* Section header table, external form */
188 Elf_Internal_Shdr
*i_shdr
; /* Section header table, internal form */
190 char *shstrtab
; /* Internal copy of section header stringtab */
191 int shstrtabsize
; /* Size of section header string table */
193 /* Read in the ELF header in external format. */
195 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
197 bfd_error
= system_call_error
;
201 /* Now check to see if we have a valid ELF file, and one that BFD can
202 make use of. The magic number must match, the address size ('class')
203 and byte-swapping must match our XVEC entry, and it must have a
204 section header table (FIXME: See comments re sections at top of this
207 if (x_ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
208 x_ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
209 x_ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
210 x_ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
213 bfd_error
= wrong_format
;
217 /* FIXME, Check EI_VERSION here ! */
219 switch (x_ehdr
.e_ident
[EI_CLASS
]) {
220 case ELFCLASSNONE
: /* address size not specified */
221 goto wrong
; /* No support if can't tell address size */
222 case ELFCLASS32
: /* 32-bit addresses */
224 case ELFCLASS64
: /* 64-bit addresses */
225 goto wrong
; /* FIXME: 64 bits not yet supported */
227 goto wrong
; /* No support if unknown address class */
230 /* Switch xvec to match the specified byte order. */
231 switch (x_ehdr
.e_ident
[EI_DATA
]) {
232 case ELFDATA2MSB
: /* Big-endian */
233 abfd
->xvec
= &elf_big_vec
;
235 case ELFDATA2LSB
: /* Little-endian */
236 abfd
->xvec
= &elf_little_vec
;
237 case ELFDATANONE
: /* No data encoding specified */
238 default: /* Unknown data encoding specified */
242 /* Now that we know the byte order, swap in the rest of the header */
243 bfd_swap_ehdr_in (abfd
, &x_ehdr
, &i_ehdr
);
244 if (x_ehdr
.e_shoff
== 0)
247 if (i_ehdr
.e_type
== ET_EXEC
|| i_ehdr
.e_type
== ET_DYN
)
249 abfd
-> flags
|= EXEC_P
;
252 /* Allocate space for copies of the section header table in external
253 and internal form, seek to the section header table in the file,
254 read it in, and convert it to internal form. As a simple sanity
255 check, verify that the what BFD thinks is the size of each section
256 header table entry actually matches the size recorded in the file. */
258 if (i_ehdr
.e_shentsize
!= sizeof (*x_shdr
))
260 if ((x_shdr
= (Elf_External_Shdr
*)
261 bfd_alloc (abfd
, sizeof (*x_shdr
) * i_ehdr
.e_shnum
)) == NULL
)
263 bfd_error
= no_memory
;
266 if ((i_shdr
= (Elf_Internal_Shdr
*)
267 bfd_alloc (abfd
, sizeof (*i_shdr
) * i_ehdr
.e_shnum
)) == NULL
)
269 bfd_error
= no_memory
;
272 if (bfd_seek (abfd
, i_ehdr
.e_shoff
, SEEK_SET
) == -1)
274 bfd_error
= system_call_error
;
277 for (shindex
= 0; shindex
< i_ehdr
.e_shnum
; shindex
++)
279 if (bfd_read ((PTR
) (x_shdr
+ shindex
), sizeof (*x_shdr
), 1, abfd
)
282 bfd_error
= system_call_error
;
285 bfd_swap_shdr_in (abfd
, x_shdr
+ shindex
, i_shdr
+ shindex
);
288 /* Read in the string table containing the names of the sections. We
289 will need the base pointer to this table later. */
291 shstrtabsize
= i_shdr
[i_ehdr
.e_shstrndx
].sh_size
;
292 if ((shstrtab
= bfd_alloc (abfd
, shstrtabsize
)) == NULL
)
294 bfd_error
= no_memory
;
297 if (bfd_seek (abfd
, i_shdr
[i_ehdr
.e_shstrndx
].sh_offset
, SEEK_SET
) == -1)
299 bfd_error
= system_call_error
;
302 if (bfd_read ((PTR
) shstrtab
, shstrtabsize
, 1, abfd
) != shstrtabsize
)
304 bfd_error
= system_call_error
;
308 /* Once all of the section headers have been read and converted, we
309 can start processing them. */
311 for (shindex
= 0; shindex
< i_ehdr
.e_shnum
; shindex
++)
313 bfd_section_from_shdr (abfd
, i_shdr
+ shindex
, shstrtab
);
320 DEFUN (elf_mkobject
, (abfd
), bfd
*abfd
)
322 fprintf (stderr
, "elf_mkobject unimplemented\n");
329 DEFUN (elf_write_object_contents
, (abfd
), bfd
*abfd
)
331 fprintf (stderr
, "elf_write_object_contents unimplemented\n");
338 DEFUN (elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
345 fprintf (stderr
, "elf_set_section_contents unimplemented\n");
351 #define elf_new_section_hook _bfd_dummy_new_section_hook
352 #define elf_core_file_failing_command _bfd_dummy_core_file_failing_command
353 #define elf_core_file_failing_signal _bfd_dummy_core_file_failing_signal
354 #define elf_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
355 #define elf_slurp_armap bfd_false
356 #define elf_slurp_extended_name_table _bfd_slurp_extended_name_table
357 #define elf_truncate_arname bfd_dont_truncate_arname
358 #define elf_openr_next_archived_file bfd_generic_openr_next_archived_file
359 #define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt
360 #define elf_get_section_contents bfd_generic_get_section_contents
361 #define elf_close_and_cleanup bfd_generic_close_and_cleanup
363 #define elf_bfd_debug_info_start bfd_void
364 #define elf_bfd_debug_info_end bfd_void
365 #define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
367 #define elf_write_armap bfd_false
371 elf_get_symtab_upper_bound(abfd
)
374 fprintf (stderr
, "elf_get_symtab_upper_bound unimplemented\n");
381 elf_get_reloc_upper_bound (abfd
, asect
)
385 fprintf (stderr
, "elf_get_reloc_upper_bound unimplemented\n");
392 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
398 fprintf (stderr
, "elf_canonicalize_reloc unimplemented\n");
405 elf_get_symtab (abfd
, alocation
)
409 fprintf (stderr
, "elf_get_symtab unimplemented\n");
416 elf_make_empty_symbol(abfd
)
419 fprintf (stderr
, "elf_make_empty_symbol unimplemented\n");
426 DEFUN (elf_print_symbol
,(ignore_abfd
, filep
, symbol
, how
),
430 bfd_print_symbol_enum_type how
)
432 fprintf (stderr
, "elf_print_symbol unimplemented\n");
438 DEFUN (elf_get_lineno
,(ignore_abfd
, symbol
),
442 fprintf (stderr
, "elf_get_lineno unimplemented\n");
449 DEFUN (elf_set_arch_mach
,(abfd
, arch
, machine
),
451 enum bfd_architecture arch AND
452 unsigned long machine
)
454 fprintf (stderr
, "elf_set_arch_mach unimplemented\n");
456 /* Allow any architecture to be supported by the elf backend */
457 return bfd_default_set_arch_mach(abfd
, arch
, machine
);
461 DEFUN (elf_find_nearest_line
,(abfd
,
469 asection
*section AND
470 asymbol
**symbols AND
472 CONST
char **filename_ptr AND
473 CONST
char **functionname_ptr AND
474 unsigned int *line_ptr
)
476 fprintf (stderr
, "elf_find_nearest_line unimplemented\n");
483 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
487 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
493 /* This structure contains everything that BFD knows about a target.
494 It includes things like its byte order, name, what routines to call
495 to do various operations, etc. Every BFD points to a target structure
496 with its "xvec" member.
498 There are two such structures here: one for big-endian machines and
499 one for little-endian machines. */
501 bfd_target elf_big_vec
=
503 /* name: identify kind of target */
506 /* flavour: general indication about file */
507 bfd_target_elf_flavour_enum
,
509 /* byteorder_big_p: data is big endian */
512 /* header_byteorder_big_p: header is also big endian */
515 /* object_flags: mask of all file flags */
516 (HAS_RELOC
| EXEC_P
| HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
|
519 /* section_flags: mask of all section flags */
520 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_READONLY
|
523 /* ar_pad_char: pad character for filenames within an archive header
524 FIXME: this really has nothing to do with ELF, this is a characteristic
525 of the archiver and/or os and should be independently tunable */
528 /* ar_max_namelen: maximum number of characters in an archive header
529 FIXME: this really has nothing to do with ELF, this is a characteristic
530 of the archiver and should be independently tunable. This value is
531 a WAG (wild a** guess) */
534 /* align_power_min: minimum alignment restriction for any section
535 FIXME: this value may be target machine dependent */
538 /* Routines to byte-swap various sized integers from the data sections */
539 _do_getb64
, _do_putb64
, _do_getb32
, _do_putb32
, _do_getb16
, _do_putb16
,
541 /* Routines to byte-swap various sized integers from the file headers */
542 _do_getb64
, _do_putb64
, _do_getb32
, _do_putb32
, _do_getb16
, _do_putb16
,
544 /* bfd_check_format: check the format of a file being read */
547 bfd_generic_archive_p
,
551 /* bfd_set_format: set the format of a file being written */
554 _bfd_generic_mkarchive
,
558 /* bfd_write_contents: write cached information into a file being written */
560 elf_write_object_contents
,
561 _bfd_write_archive_contents
,
565 /* Initialize a jump table with the standard macro. All names start
573 bfd_target elf_little_vec
=
575 /* name: identify kind of target */
578 /* flavour: general indication about file */
579 bfd_target_elf_flavour_enum
,
581 /* byteorder_big_p: data is big endian */
582 false, /* Nope -- this one's little endian */
584 /* header_byteorder_big_p: header is also big endian */
585 false, /* Nope -- this one's little endian */
587 /* object_flags: mask of all file flags */
588 (HAS_RELOC
| EXEC_P
| HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
|
591 /* section_flags: mask of all section flags */
592 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_READONLY
|
595 /* ar_pad_char: pad character for filenames within an archive header
596 FIXME: this really has nothing to do with ELF, this is a characteristic
597 of the archiver and/or os and should be independently tunable */
600 /* ar_max_namelen: maximum number of characters in an archive header
601 FIXME: this really has nothing to do with ELF, this is a characteristic
602 of the archiver and should be independently tunable. This value is
603 a WAG (wild a** guess) */
606 /* align_power_min: minimum alignment restriction for any section
607 FIXME: this value may be target machine dependent */
610 /* Routines to byte-swap various sized integers from the data sections */
611 _do_getl64
, _do_putl64
, _do_getl32
, _do_putl32
, _do_getl16
, _do_putl16
,
613 /* Routines to byte-swap various sized integers from the file headers */
614 _do_getl64
, _do_putl64
, _do_getl32
, _do_putl32
, _do_getl16
, _do_putl16
,
616 /* bfd_check_format: check the format of a file being read */
619 bfd_generic_archive_p
,
623 /* bfd_set_format: set the format of a file being written */
626 _bfd_generic_mkarchive
,
630 /* bfd_write_contents: write cached information into a file being written */
632 elf_write_object_contents
,
633 _bfd_write_archive_contents
,
637 /* Initialize a jump table with the standard macro. All names start