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)
75 #include "elf/common.h"
76 #include "elf/internal.h"
77 #include "elf/external.h"
79 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
80 #include <sys/procfs.h>
82 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
83 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
84 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
87 /* Forward data declarations */
89 extern bfd_target elf_little_vec
, elf_big_vec
;
91 /* Currently the elf_symbol_type struct just contains the generic bfd
99 /* Some private data is stashed away for future use using the tdata pointer
100 in the bfd structure. This information is different for ELF core files
101 and other ELF files. */
103 typedef struct elf_core_tdata_struct
105 void *prstatus
; /* The raw /proc prstatus structure */
106 void *prpsinfo
; /* The raw /proc prpsinfo structure */
109 #define core_prpsinfo(bfd) (((bfd)->tdata.elf_core_data) -> prpsinfo)
110 #define core_prstatus(bfd) (((bfd)->tdata.elf_core_data) -> prstatus)
113 typedef struct elf_obj_tdata_struct
115 file_ptr symtab_filepos
; /* Offset to start of ELF symtab section */
116 long symtab_filesz
; /* Size of ELF symtab section */
117 file_ptr strtab_filepos
; /* Offset to start of ELF string tbl section */
118 long strtab_filesz
; /* Size of ELF string tbl section */
121 #define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data)
122 #define elf_symtab_filepos(bfd) (elf_tdata(bfd) -> symtab_filepos)
123 #define elf_symtab_filesz(bfd) (elf_tdata(bfd) -> symtab_filesz)
124 #define elf_strtab_filepos(bfd) (elf_tdata(bfd) -> strtab_filepos)
125 #define elf_strtab_filesz(bfd) (elf_tdata(bfd) -> strtab_filesz)
127 /* Translate an ELF symbol in external format into an ELF symbol in internal
131 DEFUN(elf_swap_symbol_in
,(abfd
, src
, dst
),
133 Elf_External_Sym
*src AND
134 Elf_Internal_Sym
*dst
)
136 dst
-> st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_name
);
137 dst
-> st_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_value
);
138 dst
-> st_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_size
);
139 dst
-> st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
-> st_info
);
140 dst
-> st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
-> st_other
);
141 dst
-> st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> st_shndx
);
145 /* Translate an ELF file header in external format into an ELF file header in
149 DEFUN(elf_swap_ehdr_in
,(abfd
, src
, dst
),
151 Elf_External_Ehdr
*src AND
152 Elf_Internal_Ehdr
*dst
)
154 memcpy (dst
-> e_ident
, src
-> e_ident
, EI_NIDENT
);
155 dst
-> e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_type
);
156 dst
-> e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_machine
);
157 dst
-> e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_version
);
158 dst
-> e_entry
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_entry
);
159 dst
-> e_phoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_phoff
);
160 dst
-> e_shoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_shoff
);
161 dst
-> e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_flags
);
162 dst
-> e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_ehsize
);
163 dst
-> e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phentsize
);
164 dst
-> e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phnum
);
165 dst
-> e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shentsize
);
166 dst
-> e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shnum
);
167 dst
-> e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shstrndx
);
171 /* Translate an ELF section header table entry in external format into an
172 ELF section header table entry in internal format. */
175 DEFUN(elf_swap_shdr_in
,(abfd
, src
, dst
),
177 Elf_External_Shdr
*src AND
178 Elf_Internal_Shdr
*dst
)
180 dst
-> sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_name
);
181 dst
-> sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_type
);
182 dst
-> sh_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_flags
);
183 dst
-> sh_addr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_addr
);
184 dst
-> sh_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_offset
);
185 dst
-> sh_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_size
);
186 dst
-> sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_link
);
187 dst
-> sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_info
);
188 dst
-> sh_addralign
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_addralign
);
189 dst
-> sh_entsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_entsize
);
193 /* Translate an ELF program header table entry in external format into an
194 ELF program header table entry in internal format. */
197 DEFUN(elf_swap_phdr_in
,(abfd
, src
, dst
),
199 Elf_External_Phdr
*src AND
200 Elf_Internal_Phdr
*dst
)
202 dst
-> p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_type
);
203 dst
-> p_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_offset
);
204 dst
-> p_vaddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_vaddr
);
205 dst
-> p_paddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_paddr
);
206 dst
-> p_filesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_filesz
);
207 dst
-> p_memsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_memsz
);
208 dst
-> p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_flags
);
209 dst
-> p_align
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_align
);
213 /* Create a new bfd section from an ELF section header. */
216 DEFUN(bfd_section_from_shdr
, (abfd
, hdr
, shstrtab
),
218 Elf_Internal_Shdr
*hdr AND
224 name
= hdr
-> sh_name
? shstrtab
+ hdr
-> sh_name
: "unnamed";
225 newsect
= bfd_make_section (abfd
, name
);
226 if (!newsect
) return false;
227 newsect
-> vma
= hdr
-> sh_addr
;
228 newsect
-> _raw_size
= hdr
-> sh_size
;
229 if (!(hdr
-> sh_type
== SHT_NOBITS
))
231 newsect
-> filepos
= hdr
-> sh_offset
;
232 newsect
-> flags
|= SEC_HAS_CONTENTS
;
234 if (hdr
-> sh_flags
& SHF_ALLOC
)
236 newsect
-> flags
|= SEC_ALLOC
;
237 if (hdr
-> sh_type
!= SHT_NOBITS
)
239 newsect
-> flags
|= SEC_LOAD
;
242 if (!(hdr
-> sh_flags
& SHF_WRITE
))
244 newsect
-> flags
|= SEC_READONLY
;
246 if (hdr
-> sh_flags
& SHF_EXECINSTR
)
248 newsect
-> flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
252 newsect
-> flags
|= SEC_DATA
;
254 if (hdr
-> sh_type
== SHT_SYMTAB
)
256 abfd
-> flags
|= HAS_SYMS
;
262 /* Create a new bfd section from an ELF program header.
264 Since program segments have no names, we generate a synthetic name
265 of the form segment<NUM>, where NUM is generally the index in the
266 program header table. For segments that are split (see below) we
267 generate the names segment<NUM>a and segment<NUM>b.
269 Note that some program segments may have a file size that is different than
270 (less than) the memory size. All this means is that at execution the
271 system must allocate the amount of memory specified by the memory size,
272 but only initialize it with the first "file size" bytes read from the
273 file. This would occur for example, with program segments consisting
274 of combined data+bss.
276 To handle the above situation, this routine generates TWO bfd sections
277 for the single program segment. The first has the length specified by
278 the file size of the segment, and the second has the length specified
279 by the difference between the two sizes. In effect, the segment is split
280 into it's initialized and uninitialized parts.
285 DEFUN(bfd_section_from_phdr
, (abfd
, hdr
, index
),
287 Elf_Internal_Phdr
*hdr AND
295 split
= ((hdr
-> p_memsz
> 0) &&
296 (hdr
-> p_filesz
> 0) &&
297 (hdr
-> p_memsz
> hdr
-> p_filesz
));
298 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
299 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
300 (void) strcpy (name
, namebuf
);
301 newsect
= bfd_make_section (abfd
, name
);
302 newsect
-> vma
= hdr
-> p_vaddr
;
303 newsect
-> _raw_size
= hdr
-> p_filesz
;
304 newsect
-> filepos
= hdr
-> p_offset
;
305 newsect
-> flags
|= SEC_HAS_CONTENTS
;
306 if (hdr
-> p_type
== PT_LOAD
)
308 newsect
-> flags
|= SEC_ALLOC
;
309 newsect
-> flags
|= SEC_LOAD
;
310 if (hdr
-> p_flags
& PF_X
)
312 /* FIXME: all we known is that it has execute PERMISSION,
314 newsect
-> flags
|= SEC_CODE
;
317 if (!(hdr
-> p_flags
& PF_W
))
319 newsect
-> flags
|= SEC_READONLY
;
324 sprintf (namebuf
, "segment%db", index
);
325 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
326 (void) strcpy (name
, namebuf
);
327 newsect
= bfd_make_section (abfd
, name
);
328 newsect
-> vma
= hdr
-> p_vaddr
+ hdr
-> p_filesz
;
329 newsect
-> _raw_size
= hdr
-> p_memsz
- hdr
-> p_filesz
;
330 if (hdr
-> p_type
== PT_LOAD
)
332 newsect
-> flags
|= SEC_ALLOC
;
333 if (hdr
-> p_flags
& PF_X
)
335 newsect
-> flags
|= SEC_CODE
;
338 if (!(hdr
-> p_flags
& PF_W
))
340 newsect
-> flags
|= SEC_READONLY
;
350 DEFUN(bfd_prstatus
,(abfd
, descdata
, descsz
, filepos
),
358 if (descsz
== sizeof (prstatus_t
))
360 newsect
= bfd_make_section (abfd
, ".reg");
361 newsect
-> _raw_size
= sizeof (gregset_t
);
362 newsect
-> filepos
= filepos
+ (long) (((prstatus_t
*)0) -> pr_reg
);
363 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
364 newsect
-> alignment_power
= 2;
365 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
367 memcpy (core_prstatus (abfd
), descdata
, descsz
);
372 /* Stash a copy of the prpsinfo structure away for future use. */
375 DEFUN(bfd_prpsinfo
,(abfd
, descdata
, descsz
, filepos
),
383 if (descsz
== sizeof (prpsinfo_t
))
385 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
387 bcopy (descdata
, core_prpsinfo (abfd
), descsz
);
393 DEFUN(bfd_fpregset
,(abfd
, descdata
, descsz
, filepos
),
401 if (descsz
== sizeof (fpregset_t
))
403 newsect
= bfd_make_section (abfd
, ".reg2");
404 newsect
-> _raw_size
= sizeof (fpregset_t
);
405 newsect
-> filepos
= filepos
;
406 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
407 newsect
-> alignment_power
= 2;
411 #endif /* HAVE_PROCFS */
413 /* Return a pointer to the args (including the command name) that were
414 seen by the program that generated the core dump. Note that for
415 some reason, a spurious space is tacked onto the end of the args
416 in some (at least one anyway) implementations, so strip it off if
420 DEFUN(elf_core_file_failing_command
, (abfd
),
424 if (core_prpsinfo (abfd
))
426 prpsinfo_t
*p
= core_prpsinfo (abfd
);
427 char *scan
= p
-> pr_psargs
;
430 if ((scan
> p
-> pr_psargs
) && (*scan
== ' '))
434 return (p
-> pr_psargs
);
440 /* Return the number of the signal that caused the core dump. Presumably,
441 since we have a core file, we got a signal of some kind, so don't bother
442 checking the other process status fields, just return the signal number.
446 DEFUN(elf_core_file_failing_signal
, (abfd
),
450 if (core_prstatus (abfd
))
452 return (((prstatus_t
*)(core_prstatus (abfd
))) -> pr_cursig
);
458 /* Check to see if the core file could reasonably be expected to have
459 come for the current executable file. Note that by default we return
460 true unless we find something that indicates that there might be a
465 DEFUN(elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
474 /* First, xvecs must match since both are ELF files for the same target. */
476 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
478 bfd_error
= system_call_error
;
484 /* If no prpsinfo, just return true. Otherwise, grab the last component
485 of the exec'd pathname from the prpsinfo. */
487 if (core_prpsinfo (core_bfd
))
489 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
)) -> pr_fname
);
496 /* Find the last component of the executable pathname. */
498 if ((execname
= strrchr (exec_bfd
-> filename
, '/')) != NULL
)
504 execname
= (char *) exec_bfd
-> filename
;
507 /* See if they match */
509 return (strcmp (execname
, corename
) ? false : true);
515 #endif /* HAVE_PROCFS */
518 /* ELF core files contain a segment of type PT_NOTE, that holds much of
519 the information that would normally be available from the /proc interface
520 for the process, at the time the process dumped core. Currently this
521 includes copies of the prstatus, prpsinfo, and fpregset structures.
523 Since these structures are potentially machine dependent in size and
524 ordering, bfd provides two levels of support for them. The first level,
525 available on all machines since it does not require that the host
526 have /proc support or the relevant include files, is to create a bfd
527 section for each of the prstatus, prpsinfo, and fpregset structures,
528 without any interpretation of their contents. With just this support,
529 the bfd client will have to interpret the structures itself. Even with
530 /proc support, it might want these full structures for it's own reasons.
532 In the second level of support, where HAVE_PROCFS is defined, bfd will
533 pick apart the structures to gather some additional information that
534 clients may want, such as the general register set, the name of the
535 exec'ed file and its arguments, the signal (if any) that caused the
541 DEFUN(elf_corefile_note
, (abfd
, hdr
),
543 Elf_Internal_Phdr
*hdr
)
545 Elf_External_Note
*x_note_p
; /* Elf note, external form */
546 Elf_Internal_Note i_note
; /* Elf note, internal form */
547 char *buf
= NULL
; /* Entire note segment contents */
548 char *namedata
; /* Name portion of the note */
549 char *descdata
; /* Descriptor portion of the note */
550 char *sectname
; /* Name to use for new section */
551 long filepos
; /* File offset to descriptor data */
554 if (hdr
-> p_filesz
> 0
555 && (buf
= (char *) bfd_xmalloc (hdr
-> p_filesz
)) != NULL
556 && bfd_seek (abfd
, hdr
-> p_offset
, SEEK_SET
) != -1L
557 && bfd_read ((PTR
) buf
, hdr
-> p_filesz
, 1, abfd
) == hdr
-> p_filesz
)
559 x_note_p
= (Elf_External_Note
*) buf
;
560 while ((char *) x_note_p
< (buf
+ hdr
-> p_filesz
))
562 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> namesz
);
563 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> descsz
);
564 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> type
);
565 namedata
= x_note_p
-> name
;
566 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
567 filepos
= hdr
-> p_offset
+ (descdata
- buf
);
568 switch (i_note
.type
) {
570 /* process descdata as prstatus info */
571 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
572 sectname
= ".prstatus";
575 /* process descdata as fpregset info */
576 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
577 sectname
= ".fpregset";
580 /* process descdata as prpsinfo */
581 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
582 sectname
= ".prpsinfo";
585 /* Unknown descriptor, just ignore it. */
589 if (sectname
!= NULL
)
591 newsect
= bfd_make_section (abfd
, sectname
);
592 newsect
-> _raw_size
= i_note
.descsz
;
593 newsect
-> filepos
= filepos
;
594 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
595 newsect
-> alignment_power
= 2;
597 x_note_p
= (Elf_External_Note
*)
598 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
610 /* Read a specified number of bytes at a specified offset in an ELF
611 file, into a newly allocated buffer, and return a pointer to the
615 DEFUN(elf_read
, (abfd
, offset
, size
),
622 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
624 bfd_error
= no_memory
;
627 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
629 bfd_error
= system_call_error
;
632 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
634 bfd_error
= system_call_error
;
640 /* Begin processing a given object.
642 First we validate the file by reading in the ELF header and checking
648 DEFUN (elf_object_p
, (abfd
), bfd
*abfd
)
650 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
651 Elf_Internal_Ehdr i_ehdr
; /* Elf file header, internal form */
652 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
653 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
655 char *shstrtab
; /* Internal copy of section header stringtab */
656 int shstrtabsize
; /* Size of section header string table */
657 Elf_Off offset
; /* Temp place to stash file offsets */
659 /* Read in the ELF header in external format. */
661 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
663 bfd_error
= system_call_error
;
667 /* Now check to see if we have a valid ELF file, and one that BFD can
668 make use of. The magic number must match, the address size ('class')
669 and byte-swapping must match our XVEC entry, and it must have a
670 section header table (FIXME: See comments re sections at top of this
673 if (x_ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
674 x_ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
675 x_ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
676 x_ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
679 bfd_error
= wrong_format
;
683 /* FIXME, Check EI_VERSION here ! */
685 switch (x_ehdr
.e_ident
[EI_CLASS
]) {
686 case ELFCLASSNONE
: /* address size not specified */
687 goto wrong
; /* No support if can't tell address size */
688 case ELFCLASS32
: /* 32-bit addresses */
690 case ELFCLASS64
: /* 64-bit addresses */
691 goto wrong
; /* FIXME: 64 bits not yet supported */
693 goto wrong
; /* No support if unknown address class */
696 /* Switch xvec to match the specified byte order. */
697 switch (x_ehdr
.e_ident
[EI_DATA
]) {
698 case ELFDATA2MSB
: /* Big-endian */
699 if (!abfd
->xvec
->header_byteorder_big_p
)
702 case ELFDATA2LSB
: /* Little-endian */
703 if (abfd
->xvec
->header_byteorder_big_p
)
706 case ELFDATANONE
: /* No data encoding specified */
707 default: /* Unknown data encoding specified */
711 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
712 the tdata pointer in the bfd. */
714 if ((abfd
-> tdata
.elf_obj_data
=
715 (elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (elf_obj_tdata
)))
718 bfd_error
= no_memory
;
722 /* Now that we know the byte order, swap in the rest of the header */
723 elf_swap_ehdr_in (abfd
, &x_ehdr
, &i_ehdr
);
725 /* If there is no section header table, we're hosed. */
726 if (i_ehdr
.e_shoff
== 0)
729 if (i_ehdr
.e_type
== ET_EXEC
|| i_ehdr
.e_type
== ET_DYN
)
731 abfd
-> flags
|= EXEC_P
;
734 /* Allocate space for copies of the section header table in external
735 and internal form, seek to the section header table in the file,
736 read it in, and convert it to internal form. As a simple sanity
737 check, verify that the what BFD thinks is the size of each section
738 header table entry actually matches the size recorded in the file. */
740 if (i_ehdr
.e_shentsize
!= sizeof (x_shdr
))
742 if ((i_shdrp
= (Elf_Internal_Shdr
*)
743 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdr
.e_shnum
)) == NULL
)
745 bfd_error
= no_memory
;
748 if (bfd_seek (abfd
, i_ehdr
.e_shoff
, SEEK_SET
) == -1)
750 bfd_error
= system_call_error
;
753 for (shindex
= 0; shindex
< i_ehdr
.e_shnum
; shindex
++)
755 if (bfd_read ((PTR
) &x_shdr
, sizeof (x_shdr
), 1, abfd
)
758 bfd_error
= system_call_error
;
761 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
764 /* Read in the string table containing the names of the sections. We
765 will need the base pointer to this table later. */
767 shstrtabsize
= i_shdrp
[i_ehdr
.e_shstrndx
].sh_size
;
768 offset
= i_shdrp
[i_ehdr
.e_shstrndx
].sh_offset
;
769 if ((shstrtab
= elf_read (abfd
, offset
, shstrtabsize
)) == NULL
)
774 /* Once all of the section headers have been read and converted, we
775 can start processing them. Note that the first section header is
776 a dummy placeholder entry, so we ignore it.
778 We also watch for the symbol table section and remember the file
779 offset and section size for both the symbol table section and the
780 associated string table section. */
782 for (shindex
= 1; shindex
< i_ehdr
.e_shnum
; shindex
++)
784 Elf_Internal_Shdr
*hdr
= i_shdrp
+ shindex
;
785 bfd_section_from_shdr (abfd
, hdr
, shstrtab
);
786 if (hdr
-> sh_type
== SHT_SYMTAB
)
788 elf_symtab_filepos (abfd
) = hdr
-> sh_offset
;
789 elf_symtab_filesz (abfd
) = hdr
-> sh_size
;
790 elf_strtab_filepos (abfd
) = (i_shdrp
+ hdr
-> sh_link
) -> sh_offset
;
791 elf_strtab_filesz (abfd
) = (i_shdrp
+ hdr
-> sh_link
) -> sh_size
;
795 /* Remember the entry point specified in the ELF file header. */
797 bfd_get_start_address (abfd
) = i_ehdr
.e_entry
;
802 /* Core files are simply standard ELF formatted files that partition
803 the file using the execution view of the file (program header table)
804 rather than the linking view. In fact, there is no section header
805 table in a core file.
807 The process status information (including the contents of the general
808 register set) and the floating point register set are stored in a
809 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
810 that allow standard bfd access to the general registers (.reg) and the
811 floating point registers (.reg2).
816 DEFUN (elf_core_file_p
, (abfd
), bfd
*abfd
)
818 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
819 Elf_Internal_Ehdr i_ehdr
; /* Elf file header, internal form */
820 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
821 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
824 /* Read in the ELF header in external format. */
826 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
828 bfd_error
= system_call_error
;
832 /* Now check to see if we have a valid ELF file, and one that BFD can
833 make use of. The magic number must match, the address size ('class')
834 and byte-swapping must match our XVEC entry, and it must have a
835 program header table (FIXME: See comments re segments at top of this
838 if (x_ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
839 x_ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
840 x_ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
841 x_ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
844 bfd_error
= wrong_format
;
848 /* FIXME, Check EI_VERSION here ! */
850 switch (x_ehdr
.e_ident
[EI_CLASS
]) {
851 case ELFCLASSNONE
: /* address size not specified */
852 goto wrong
; /* No support if can't tell address size */
853 case ELFCLASS32
: /* 32-bit addresses */
855 case ELFCLASS64
: /* 64-bit addresses */
856 goto wrong
; /* FIXME: 64 bits not yet supported */
858 goto wrong
; /* No support if unknown address class */
861 /* Switch xvec to match the specified byte order. */
862 switch (x_ehdr
.e_ident
[EI_DATA
]) {
863 case ELFDATA2MSB
: /* Big-endian */
864 abfd
->xvec
= &elf_big_vec
;
866 case ELFDATA2LSB
: /* Little-endian */
867 abfd
->xvec
= &elf_little_vec
;
869 case ELFDATANONE
: /* No data encoding specified */
870 default: /* Unknown data encoding specified */
874 /* Now that we know the byte order, swap in the rest of the header */
875 elf_swap_ehdr_in (abfd
, &x_ehdr
, &i_ehdr
);
877 /* If there is no program header, or the type is not a core file, then
879 if (i_ehdr
.e_phoff
== 0 || i_ehdr
.e_type
!= ET_CORE
)
882 /* Allocate an instance of the elf_core_tdata structure and hook it up to
883 the tdata pointer in the bfd. */
885 if ((abfd
-> tdata
.elf_core_data
=
886 (elf_core_tdata
*) bfd_zalloc (abfd
, sizeof (elf_core_tdata
)))
889 bfd_error
= no_memory
;
893 /* Allocate space for copies of the program header table in external
894 and internal form, seek to the program header table in the file,
895 read it in, and convert it to internal form. As a simple sanity
896 check, verify that the what BFD thinks is the size of each program
897 header table entry actually matches the size recorded in the file. */
899 if (i_ehdr
.e_phentsize
!= sizeof (x_phdr
))
901 if ((i_phdrp
= (Elf_Internal_Phdr
*)
902 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdr
.e_phnum
)) == NULL
)
904 bfd_error
= no_memory
;
907 if (bfd_seek (abfd
, i_ehdr
.e_phoff
, SEEK_SET
) == -1)
909 bfd_error
= system_call_error
;
912 for (phindex
= 0; phindex
< i_ehdr
.e_phnum
; phindex
++)
914 if (bfd_read ((PTR
) &x_phdr
, sizeof (x_phdr
), 1, abfd
)
917 bfd_error
= system_call_error
;
920 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
923 /* Once all of the program headers have been read and converted, we
924 can start processing them. */
926 for (phindex
= 0; phindex
< i_ehdr
.e_phnum
; phindex
++)
928 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
929 if ((i_phdrp
+ phindex
) -> p_type
== PT_NOTE
)
931 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
935 /* Remember the entry point specified in the ELF file header. */
937 bfd_get_start_address (abfd
) = i_ehdr
.e_entry
;
943 DEFUN (elf_mkobject
, (abfd
), bfd
*abfd
)
945 fprintf (stderr
, "elf_mkobject unimplemented\n");
952 DEFUN (elf_write_object_contents
, (abfd
), bfd
*abfd
)
954 fprintf (stderr
, "elf_write_object_contents unimplemented\n");
960 /* Given an index of a section, retrieve a pointer to it. Note
961 that for our purposes, sections are indexed by {1, 2, ...} with
962 0 being an illegal index. */
965 DEFUN (section_from_bfd_index
, (abfd
, index
),
971 struct sec
*answer
= abfd
-> sections
;
974 answer
= answer
-> next
;
982 DEFUN (elf_slurp_symbol_table
, (abfd
), bfd
*abfd
)
984 int symcount
; /* Number of external ELF symbols */
985 char *strtab
; /* Buffer for raw ELF string table section */
986 asymbol
*sym
; /* Pointer to current bfd symbol */
987 asymbol
*symbase
; /* Buffer for generated bfd symbols */
988 asymbol
**vec
; /* Pointer to current bfd symbol pointer */
989 Elf_Internal_Sym i_sym
;
990 Elf_External_Sym x_sym
;
992 if (bfd_get_outsymbols (abfd
) != NULL
)
997 /* Slurp in the string table. We will keep it around permanently, as
998 long as the bfd is in use, since we will end up setting up pointers
999 into it for the names of all the symbols. */
1001 strtab
= elf_read (abfd
, elf_strtab_filepos(abfd
), elf_strtab_filesz(abfd
));
1007 /* Read each raw ELF symbol, converting from external ELF form to
1008 internal ELF form, and then using the information to create a
1009 canonical bfd symbol table entry.
1011 Note that be allocate the initial bfd canonical symbol buffer
1012 based on a one-to-one mapping of the ELF symbols to canonical
1013 symbols. However, it is likely that not all the ELF symbols will
1014 be used, so there will be some space leftover at the end. Once
1015 we know how many symbols we actual generate, we realloc the buffer
1016 to the correct size and then build the pointer vector. */
1018 if (bfd_seek (abfd
, elf_symtab_filepos (abfd
), SEEK_SET
) == -1)
1020 bfd_error
= system_call_error
;
1024 symcount
= elf_symtab_filesz(abfd
) / sizeof (Elf_External_Sym
);
1025 sym
= symbase
= (asymbol
*) bfd_zalloc (abfd
, symcount
* sizeof (asymbol
));
1027 while (symcount
-- > 0)
1029 if (bfd_read ((PTR
) &x_sym
, sizeof (x_sym
), 1, abfd
) != sizeof (x_sym
))
1031 bfd_error
= system_call_error
;
1034 elf_swap_symbol_in (abfd
, &x_sym
, &i_sym
);
1035 if (i_sym
.st_name
> 0)
1037 sym
-> the_bfd
= abfd
;
1038 sym
-> name
= strtab
+ i_sym
.st_name
;
1039 sym
-> value
= i_sym
.st_value
;
1040 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
1042 /* Note: This code depends upon there being an ordered
1043 one-for-one mapping of ELF sections to bfd sections. */
1044 sym
-> section
= section_from_bfd_index (abfd
, i_sym
.st_shndx
);
1046 else if (i_sym
.st_shndx
== SHN_ABS
)
1048 sym
-> section
= &bfd_abs_section
;
1050 else if (i_sym
.st_shndx
== SHN_COMMON
)
1052 sym
-> section
= &bfd_com_section
;
1054 switch (ELF_ST_BIND (i_sym
.st_info
))
1057 sym
-> flags
|= BSF_LOCAL
;
1060 sym
-> flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
1063 sym
-> flags
|= BSF_WEAK
;
1070 bfd_get_symcount(abfd
) = symcount
= sym
- symbase
;
1071 sym
= symbase
= (asymbol
*)
1072 bfd_realloc (abfd
, symbase
, symcount
* sizeof (asymbol
));
1073 bfd_get_outsymbols(abfd
) = vec
= (asymbol
**)
1074 bfd_alloc (abfd
, symcount
* sizeof (asymbol
*));
1076 while (symcount
-- > 0)
1084 /* Return the number of bytes required to hold the symtab vector.
1086 Note that we base it on the count plus 1, since we will null terminate
1087 the vector allocated based on this size. */
1090 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
*abfd
)
1092 unsigned int symtab_size
= 0;
1094 if (elf_slurp_symbol_table (abfd
))
1096 symtab_size
= (bfd_get_symcount (abfd
) + 1) * (sizeof (asymbol
));
1098 return (symtab_size
);
1102 elf_get_reloc_upper_bound (abfd
, asect
)
1106 fprintf (stderr
, "elf_get_reloc_upper_bound unimplemented\n");
1113 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
1119 fprintf (stderr
, "elf_canonicalize_reloc unimplemented\n");
1126 DEFUN (elf_get_symtab
, (abfd
, alocation
),
1128 asymbol
**alocation
)
1130 unsigned int symcount
;
1133 if (!elf_slurp_symbol_table (abfd
))
1139 symcount
= bfd_get_symcount (abfd
);
1140 vec
= bfd_get_outsymbols (abfd
);
1141 while (symcount
-- > 0)
1143 *alocation
++ = *vec
++;
1145 *alocation
++ = NULL
;
1146 return (bfd_get_symcount (abfd
));
1151 DEFUN (elf_make_empty_symbol
, (abfd
),
1154 elf_symbol_type
*new;
1156 new = (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
1159 bfd_error
= no_memory
;
1164 new -> symbol
.the_bfd
= abfd
;
1165 return (&new -> symbol
);
1170 DEFUN (elf_print_symbol
,(ignore_abfd
, filep
, symbol
, how
),
1171 bfd
*ignore_abfd AND
1174 bfd_print_symbol_type how
)
1176 fprintf (stderr
, "elf_print_symbol unimplemented\n");
1182 DEFUN (elf_get_lineno
,(ignore_abfd
, symbol
),
1183 bfd
*ignore_abfd AND
1186 fprintf (stderr
, "elf_get_lineno unimplemented\n");
1193 DEFUN (elf_set_arch_mach
,(abfd
, arch
, machine
),
1195 enum bfd_architecture arch AND
1196 unsigned long machine
)
1198 fprintf (stderr
, "elf_set_arch_mach unimplemented\n");
1200 /* Allow any architecture to be supported by the elf backend */
1201 return bfd_default_set_arch_mach(abfd
, arch
, machine
);
1205 DEFUN (elf_find_nearest_line
,(abfd
,
1213 asection
*section AND
1214 asymbol
**symbols AND
1216 CONST
char **filename_ptr AND
1217 CONST
char **functionname_ptr AND
1218 unsigned int *line_ptr
)
1220 fprintf (stderr
, "elf_find_nearest_line unimplemented\n");
1227 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
1231 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
1237 /* This structure contains everything that BFD knows about a target.
1238 It includes things like its byte order, name, what routines to call
1239 to do various operations, etc. Every BFD points to a target structure
1240 with its "xvec" member.
1242 There are two such structures here: one for big-endian machines and
1243 one for little-endian machines. */
1245 /* Archives are generic or unimplemented. */
1246 #define elf_slurp_armap bfd_false
1247 #define elf_slurp_extended_name_table _bfd_slurp_extended_name_table
1248 #define elf_truncate_arname bfd_dont_truncate_arname
1249 #define elf_openr_next_archived_file bfd_generic_openr_next_archived_file
1250 #define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt
1251 #define elf_write_armap (PROTO (boolean, (*), \
1252 (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count, \
1253 int stridx))) bfd_false
1255 /* Ordinary section reading and writing */
1256 #define elf_new_section_hook _bfd_dummy_new_section_hook
1257 #define elf_get_section_contents bfd_generic_get_section_contents
1258 #define elf_set_section_contents bfd_generic_set_section_contents
1259 #define elf_close_and_cleanup bfd_generic_close_and_cleanup
1261 #define elf_bfd_debug_info_start bfd_void
1262 #define elf_bfd_debug_info_end bfd_void
1263 #define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
1264 #define elf_bfd_get_relocated_section_contents \
1265 bfd_generic_get_relocated_section_contents
1266 #define elf_bfd_relax_section bfd_generic_relax_section
1267 bfd_target elf_big_vec
=
1269 /* name: identify kind of target */
1272 /* flavour: general indication about file */
1273 bfd_target_elf_flavour
,
1275 /* byteorder_big_p: data is big endian */
1278 /* header_byteorder_big_p: header is also big endian */
1281 /* object_flags: mask of all file flags */
1282 (HAS_RELOC
| EXEC_P
| HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
|
1285 /* section_flags: mask of all section flags */
1286 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_READONLY
|
1289 /* ar_pad_char: pad character for filenames within an archive header
1290 FIXME: this really has nothing to do with ELF, this is a characteristic
1291 of the archiver and/or os and should be independently tunable */
1294 /* ar_max_namelen: maximum number of characters in an archive header
1295 FIXME: this really has nothing to do with ELF, this is a characteristic
1296 of the archiver and should be independently tunable. This value is
1297 a WAG (wild a** guess) */
1300 /* align_power_min: minimum alignment restriction for any section
1301 FIXME: this value may be target machine dependent */
1304 /* Routines to byte-swap various sized integers from the data sections */
1305 _do_getb64
, _do_putb64
, _do_getb32
, _do_putb32
, _do_getb16
, _do_putb16
,
1307 /* Routines to byte-swap various sized integers from the file headers */
1308 _do_getb64
, _do_putb64
, _do_getb32
, _do_putb32
, _do_getb16
, _do_putb16
,
1310 /* bfd_check_format: check the format of a file being read */
1311 { _bfd_dummy_target
, /* unknown format */
1312 elf_object_p
, /* assembler/linker output (object file) */
1313 bfd_generic_archive_p
, /* an archive */
1314 elf_core_file_p
/* a core file */
1317 /* bfd_set_format: set the format of a file being written */
1320 _bfd_generic_mkarchive
,
1324 /* bfd_write_contents: write cached information into a file being written */
1326 elf_write_object_contents
,
1327 _bfd_write_archive_contents
,
1331 /* Initialize a jump table with the standard macro. All names start
1339 bfd_target elf_little_vec
=
1341 /* name: identify kind of target */
1344 /* flavour: general indication about file */
1345 bfd_target_elf_flavour
,
1347 /* byteorder_big_p: data is big endian */
1348 false, /* Nope -- this one's little endian */
1350 /* header_byteorder_big_p: header is also big endian */
1351 false, /* Nope -- this one's little endian */
1353 /* object_flags: mask of all file flags */
1354 (HAS_RELOC
| EXEC_P
| HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
|
1357 /* section_flags: mask of all section flags */
1358 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_READONLY
|
1361 /* ar_pad_char: pad character for filenames within an archive header
1362 FIXME: this really has nothing to do with ELF, this is a characteristic
1363 of the archiver and/or os and should be independently tunable */
1366 /* ar_max_namelen: maximum number of characters in an archive header
1367 FIXME: this really has nothing to do with ELF, this is a characteristic
1368 of the archiver and should be independently tunable. This value is
1369 a WAG (wild a** guess) */
1372 /* align_power_min: minimum alignment restriction for any section
1373 FIXME: this value may be target machine dependent */
1376 /* Routines to byte-swap various sized integers from the data sections */
1377 _do_getl64
, _do_putl64
, _do_getl32
, _do_putl32
, _do_getl16
, _do_putl16
,
1379 /* Routines to byte-swap various sized integers from the file headers */
1380 _do_getl64
, _do_putl64
, _do_getl32
, _do_putl32
, _do_getl16
, _do_putl16
,
1382 /* bfd_check_format: check the format of a file being read */
1383 { _bfd_dummy_target
, /* unknown format */
1384 elf_object_p
, /* assembler/linker output (object file) */
1385 bfd_generic_archive_p
, /* an archive */
1386 elf_core_file_p
/* a core file */
1389 /* bfd_set_format: set the format of a file being written */
1392 _bfd_generic_mkarchive
,
1396 /* bfd_write_contents: write cached information into a file being written */
1398 elf_write_object_contents
,
1399 _bfd_write_archive_contents
,
1403 /* Initialize a jump table with the standard macro. All names start