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. */
105 void *prstatus
; /* The raw /proc prstatus structure */
106 void *prpsinfo
; /* The raw /proc prpsinfo structure */
109 #define core_prpsinfo(bfd) (((elf_core_tdata *)((bfd)->tdata))->prpsinfo)
110 #define core_prstatus(bfd) (((elf_core_tdata *)((bfd)->tdata))->prstatus)
114 file_ptr symtab_filepos
; /* Offset to start of ELF symtab section */
115 long symtab_filesz
; /* Size of ELF symtab section */
116 file_ptr strtab_filepos
; /* Offset to start of ELF string tbl section */
117 long strtab_filesz
; /* Size of ELF string tbl section */
120 #define elf_tdata(bfd) ((elf_obj_tdata *) ((bfd) -> tdata))
121 #define elf_symtab_filepos(bfd) (elf_tdata(bfd) -> symtab_filepos)
122 #define elf_symtab_filesz(bfd) (elf_tdata(bfd) -> symtab_filesz)
123 #define elf_strtab_filepos(bfd) (elf_tdata(bfd) -> strtab_filepos)
124 #define elf_strtab_filesz(bfd) (elf_tdata(bfd) -> strtab_filesz)
126 /* Translate an ELF symbol in external format into an ELF symbol in internal
130 DEFUN(elf_swap_symbol_in
,(abfd
, src
, dst
),
132 Elf_External_Sym
*src AND
133 Elf_Internal_Sym
*dst
)
135 dst
-> st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_name
);
136 dst
-> st_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_value
);
137 dst
-> st_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_size
);
138 dst
-> st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
-> st_info
);
139 dst
-> st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
-> st_other
);
140 dst
-> st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> st_shndx
);
144 /* Translate an ELF file header in external format into an ELF file header in
148 DEFUN(elf_swap_ehdr_in
,(abfd
, src
, dst
),
150 Elf_External_Ehdr
*src AND
151 Elf_Internal_Ehdr
*dst
)
153 bcopy (src
-> e_ident
, dst
-> e_ident
, EI_NIDENT
);
154 dst
-> e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_type
);
155 dst
-> e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_machine
);
156 dst
-> e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_version
);
157 dst
-> e_entry
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_entry
);
158 dst
-> e_phoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_phoff
);
159 dst
-> e_shoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_shoff
);
160 dst
-> e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_flags
);
161 dst
-> e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_ehsize
);
162 dst
-> e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phentsize
);
163 dst
-> e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phnum
);
164 dst
-> e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shentsize
);
165 dst
-> e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shnum
);
166 dst
-> e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shstrndx
);
170 /* Translate an ELF section header table entry in external format into an
171 ELF section header table entry in internal format. */
174 DEFUN(elf_swap_shdr_in
,(abfd
, src
, dst
),
176 Elf_External_Shdr
*src AND
177 Elf_Internal_Shdr
*dst
)
179 dst
-> sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_name
);
180 dst
-> sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_type
);
181 dst
-> sh_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_flags
);
182 dst
-> sh_addr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_addr
);
183 dst
-> sh_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_offset
);
184 dst
-> sh_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_size
);
185 dst
-> sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_link
);
186 dst
-> sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_info
);
187 dst
-> sh_addralign
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_addralign
);
188 dst
-> sh_entsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> sh_entsize
);
192 /* Translate an ELF program header table entry in external format into an
193 ELF program header table entry in internal format. */
196 DEFUN(elf_swap_phdr_in
,(abfd
, src
, dst
),
198 Elf_External_Phdr
*src AND
199 Elf_Internal_Phdr
*dst
)
201 dst
-> p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_type
);
202 dst
-> p_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_offset
);
203 dst
-> p_vaddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_vaddr
);
204 dst
-> p_paddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_paddr
);
205 dst
-> p_filesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_filesz
);
206 dst
-> p_memsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_memsz
);
207 dst
-> p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_flags
);
208 dst
-> p_align
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> p_align
);
212 /* Create a new bfd section from an ELF section header. */
215 DEFUN(bfd_section_from_shdr
, (abfd
, hdr
, shstrtab
),
217 Elf_Internal_Shdr
*hdr AND
223 name
= hdr
-> sh_name
? shstrtab
+ hdr
-> sh_name
: "unnamed";
224 newsect
= bfd_make_section (abfd
, name
);
225 newsect
-> vma
= hdr
-> sh_addr
;
226 newsect
-> size
= hdr
-> sh_size
;
227 if (!(hdr
-> sh_type
== SHT_NOBITS
))
229 newsect
-> filepos
= hdr
-> sh_offset
;
230 newsect
-> flags
|= SEC_HAS_CONTENTS
;
232 if (hdr
-> sh_flags
& SHF_ALLOC
)
234 newsect
-> flags
|= SEC_ALLOC
;
235 if (hdr
-> sh_type
!= SHT_NOBITS
)
237 newsect
-> flags
|= SEC_LOAD
;
240 if (!(hdr
-> sh_flags
& SHF_WRITE
))
242 newsect
-> flags
|= SEC_READONLY
;
244 if (hdr
-> sh_flags
& SHF_EXECINSTR
)
246 newsect
-> flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
250 newsect
-> flags
|= SEC_DATA
;
252 if (hdr
-> sh_type
== SHT_SYMTAB
)
254 abfd
-> flags
|= HAS_SYMS
;
260 /* Create a new bfd section from an ELF program header.
262 Since program segments have no names, we generate a synthetic name
263 of the form segment<NUM>, where NUM is generally the index in the
264 program header table. For segments that are split (see below) we
265 generate the names segment<NUM>a and segment<NUM>b.
267 Note that some program segments may have a file size that is different than
268 (less than) the memory size. All this means is that at execution the
269 system must allocate the amount of memory specified by the memory size,
270 but only initialize it with the first "file size" bytes read from the
271 file. This would occur for example, with program segments consisting
272 of combined data+bss.
274 To handle the above situation, this routine generates TWO bfd sections
275 for the single program segment. The first has the length specified by
276 the file size of the segment, and the second has the length specified
277 by the difference between the two sizes. In effect, the segment is split
278 into it's initialized and uninitialized parts.
283 DEFUN(bfd_section_from_phdr
, (abfd
, hdr
, index
),
285 Elf_Internal_Phdr
*hdr AND
293 split
= ((hdr
-> p_memsz
> 0) &&
294 (hdr
-> p_filesz
> 0) &&
295 (hdr
-> p_memsz
> hdr
-> p_filesz
));
296 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
297 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
298 (void) strcpy (name
, namebuf
);
299 newsect
= bfd_make_section (abfd
, name
);
300 newsect
-> vma
= hdr
-> p_vaddr
;
301 newsect
-> size
= hdr
-> p_filesz
;
302 newsect
-> filepos
= hdr
-> p_offset
;
303 newsect
-> flags
|= SEC_HAS_CONTENTS
;
304 if (hdr
-> p_type
== PT_LOAD
)
306 newsect
-> flags
|= SEC_ALLOC
;
307 newsect
-> flags
|= SEC_LOAD
;
308 if (hdr
-> p_flags
& PF_X
)
310 /* FIXME: all we known is that it has execute PERMISSION,
312 newsect
-> flags
|= SEC_CODE
;
315 if (!(hdr
-> p_flags
& PF_W
))
317 newsect
-> flags
|= SEC_READONLY
;
322 sprintf (namebuf
, "segment%db", index
);
323 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
324 (void) strcpy (name
, namebuf
);
325 newsect
= bfd_make_section (abfd
, name
);
326 newsect
-> vma
= hdr
-> p_vaddr
+ hdr
-> p_filesz
;
327 newsect
-> size
= hdr
-> p_memsz
- hdr
-> p_filesz
;
328 if (hdr
-> p_type
== PT_LOAD
)
330 newsect
-> flags
|= SEC_ALLOC
;
331 if (hdr
-> p_flags
& PF_X
)
333 newsect
-> flags
|= SEC_CODE
;
336 if (!(hdr
-> p_flags
& PF_W
))
338 newsect
-> flags
|= SEC_READONLY
;
348 DEFUN(bfd_prstatus
,(abfd
, descdata
, descsz
, filepos
),
356 if (descsz
== sizeof (prstatus_t
))
358 newsect
= bfd_make_section (abfd
, ".reg");
359 newsect
-> size
= sizeof (gregset_t
);
360 newsect
-> filepos
= filepos
+ (long) (((prstatus_t
*)0) -> pr_reg
);
361 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
362 newsect
-> alignment_power
= 2;
363 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
365 bcopy (descdata
, core_prstatus (abfd
), descsz
);
370 /* Stash a copy of the prpsinfo structure away for future use. */
373 DEFUN(bfd_prpsinfo
,(abfd
, descdata
, descsz
, filepos
),
381 if (descsz
== sizeof (prpsinfo_t
))
383 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
385 bcopy (descdata
, core_prpsinfo (abfd
), descsz
);
391 DEFUN(bfd_fpregset
,(abfd
, descdata
, descsz
, filepos
),
399 if (descsz
== sizeof (fpregset_t
))
401 newsect
= bfd_make_section (abfd
, ".reg2");
402 newsect
-> size
= sizeof (fpregset_t
);
403 newsect
-> filepos
= filepos
;
404 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
405 newsect
-> alignment_power
= 2;
409 #endif /* HAVE_PROCFS */
411 /* Return a pointer to the args (including the command name) that were
412 seen by the program that generated the core dump. Note that for
413 some reason, a spurious space is tacked onto the end of the args
414 in some (at least one anyway) implementations, so strip it off if
418 DEFUN(elf_core_file_failing_command
, (abfd
),
422 if (core_prpsinfo (abfd
))
424 prpsinfo_t
*p
= core_prpsinfo (abfd
);
425 char *scan
= p
-> pr_psargs
;
428 if ((scan
> p
-> pr_psargs
) && (*scan
== ' '))
432 return (p
-> pr_psargs
);
438 /* Return the number of the signal that caused the core dump. Presumably,
439 since we have a core file, we got a signal of some kind, so don't bother
440 checking the other process status fields, just return the signal number.
444 DEFUN(elf_core_file_failing_signal
, (abfd
),
448 if (core_prstatus (abfd
))
450 return (((prstatus_t
*)(core_prstatus (abfd
))) -> pr_cursig
);
456 /* Check to see if the core file could reasonably be expected to have
457 come for the current executable file. Note that by default we return
458 true unless we find something that indicates that there might be a
463 DEFUN(elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
472 /* First, xvecs must match since both are ELF files for the same target. */
474 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
476 bfd_error
= system_call_error
;
482 /* If no prpsinfo, just return true. Otherwise, grab the last component
483 of the exec'd pathname from the prpsinfo. */
485 if (core_prpsinfo (core_bfd
))
487 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
)) -> pr_fname
);
494 /* Find the last component of the executable pathname. */
496 if ((execname
= strrchr (exec_bfd
-> filename
, '/')) != NULL
)
502 execname
= (char *) exec_bfd
-> filename
;
505 /* See if they match */
507 return (strcmp (execname
, corename
) ? false : true);
513 #endif /* HAVE_PROCFS */
516 /* ELF core files contain a segment of type PT_NOTE, that holds much of
517 the information that would normally be available from the /proc interface
518 for the process, at the time the process dumped core. Currently this
519 includes copies of the prstatus, prpsinfo, and fpregset structures.
521 Since these structures are potentially machine dependent in size and
522 ordering, bfd provides two levels of support for them. The first level,
523 available on all machines since it does not require that the host
524 have /proc support or the relevant include files, is to create a bfd
525 section for each of the prstatus, prpsinfo, and fpregset structures,
526 without any interpretation of their contents. With just this support,
527 the bfd client will have to interpret the structures itself. Even with
528 /proc support, it might want these full structures for it's own reasons.
530 In the second level of support, where HAVE_PROCFS is defined, bfd will
531 pick apart the structures to gather some additional information that
532 clients may want, such as the general register set, the name of the
533 exec'ed file and its arguments, the signal (if any) that caused the
539 DEFUN(elf_corefile_note
, (abfd
, hdr
),
541 Elf_Internal_Phdr
*hdr
)
543 Elf_External_Note
*x_note_p
; /* Elf note, external form */
544 Elf_Internal_Note i_note
; /* Elf note, internal form */
545 char *buf
= NULL
; /* Entire note segment contents */
546 char *namedata
; /* Name portion of the note */
547 char *descdata
; /* Descriptor portion of the note */
548 char *sectname
; /* Name to use for new section */
549 long filepos
; /* File offset to descriptor data */
552 if (hdr
-> p_filesz
> 0
553 && (buf
= (char *)malloc(hdr
-> p_filesz
)) != NULL
554 && bfd_seek (abfd
, hdr
-> p_offset
, SEEK_SET
) != -1L
555 && bfd_read ((PTR
) buf
, hdr
-> p_filesz
, 1, abfd
) == hdr
-> p_filesz
)
557 x_note_p
= (Elf_External_Note
*) buf
;
558 while ((char *) x_note_p
< (buf
+ hdr
-> p_filesz
))
560 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> namesz
);
561 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> descsz
);
562 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> type
);
563 namedata
= x_note_p
-> name
;
564 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
565 filepos
= hdr
-> p_offset
+ (descdata
- buf
);
566 switch (i_note
.type
) {
568 /* process descdata as prstatus info */
569 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
570 sectname
= ".prstatus";
573 /* process descdata as fpregset info */
574 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
575 sectname
= ".fpregset";
578 /* process descdata as prpsinfo */
579 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
580 sectname
= ".prpsinfo";
583 /* Unknown descriptor, just ignore it. */
587 if (sectname
!= NULL
)
589 newsect
= bfd_make_section (abfd
, sectname
);
590 newsect
-> size
= i_note
.descsz
;
591 newsect
-> filepos
= filepos
;
592 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
593 newsect
-> alignment_power
= 2;
595 x_note_p
= (Elf_External_Note
*)
596 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
608 /* Read a specified number of bytes at a specified offset in an ELF
609 file, into a newly allocated buffer, and return a pointer to the
613 DEFUN(elf_read
, (abfd
, offset
, size
),
620 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
622 bfd_error
= no_memory
;
625 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
627 bfd_error
= system_call_error
;
630 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
632 bfd_error
= system_call_error
;
638 /* Begin processing a given object.
640 First we validate the file by reading in the ELF header and checking
646 DEFUN (elf_object_p
, (abfd
), bfd
*abfd
)
648 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
649 Elf_Internal_Ehdr i_ehdr
; /* Elf file header, internal form */
650 Elf_External_Shdr
*x_shdr
; /* Section header table, external form */
651 Elf_Internal_Shdr
*i_shdr
; /* Section header table, internal form */
653 char *shstrtab
; /* Internal copy of section header stringtab */
654 int shstrtabsize
; /* Size of section header string table */
655 Elf_Off offset
; /* Temp place to stash file offsets */
657 /* Read in the ELF header in external format. */
659 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
661 bfd_error
= system_call_error
;
665 /* Now check to see if we have a valid ELF file, and one that BFD can
666 make use of. The magic number must match, the address size ('class')
667 and byte-swapping must match our XVEC entry, and it must have a
668 section header table (FIXME: See comments re sections at top of this
671 if (x_ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
672 x_ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
673 x_ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
674 x_ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
677 bfd_error
= wrong_format
;
681 /* FIXME, Check EI_VERSION here ! */
683 switch (x_ehdr
.e_ident
[EI_CLASS
]) {
684 case ELFCLASSNONE
: /* address size not specified */
685 goto wrong
; /* No support if can't tell address size */
686 case ELFCLASS32
: /* 32-bit addresses */
688 case ELFCLASS64
: /* 64-bit addresses */
689 goto wrong
; /* FIXME: 64 bits not yet supported */
691 goto wrong
; /* No support if unknown address class */
694 /* Switch xvec to match the specified byte order. */
695 switch (x_ehdr
.e_ident
[EI_DATA
]) {
696 case ELFDATA2MSB
: /* Big-endian */
697 abfd
->xvec
= &elf_big_vec
;
699 case ELFDATA2LSB
: /* Little-endian */
700 abfd
->xvec
= &elf_little_vec
;
702 case ELFDATANONE
: /* No data encoding specified */
703 default: /* Unknown data encoding specified */
707 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
708 the tdata pointer in the bfd. */
710 if ((abfd
-> tdata
= bfd_zalloc (abfd
, sizeof (elf_obj_tdata
))) == NULL
)
712 bfd_error
= no_memory
;
716 /* Now that we know the byte order, swap in the rest of the header */
717 elf_swap_ehdr_in (abfd
, &x_ehdr
, &i_ehdr
);
719 /* If there is no section header table, we're hosed. */
720 if (i_ehdr
.e_shoff
== 0)
723 if (i_ehdr
.e_type
== ET_EXEC
|| i_ehdr
.e_type
== ET_DYN
)
725 abfd
-> flags
|= EXEC_P
;
728 /* Allocate space for copies of the section header table in external
729 and internal form, seek to the section header table in the file,
730 read it in, and convert it to internal form. As a simple sanity
731 check, verify that the what BFD thinks is the size of each section
732 header table entry actually matches the size recorded in the file. */
734 if (i_ehdr
.e_shentsize
!= sizeof (*x_shdr
))
736 if ((x_shdr
= (Elf_External_Shdr
*)
737 bfd_alloc (abfd
, sizeof (*x_shdr
) * i_ehdr
.e_shnum
)) == NULL
)
739 bfd_error
= no_memory
;
742 if ((i_shdr
= (Elf_Internal_Shdr
*)
743 bfd_alloc (abfd
, sizeof (*i_shdr
) * 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
+ shindex
), sizeof (*x_shdr
), 1, abfd
)
758 bfd_error
= system_call_error
;
761 elf_swap_shdr_in (abfd
, x_shdr
+ shindex
, i_shdr
+ 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_shdr
[i_ehdr
.e_shstrndx
].sh_size
;
768 offset
= i_shdr
[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_shdr
+ 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_shdr
+ hdr
-> sh_link
) -> sh_offset
;
791 elf_strtab_filesz(abfd
) = (i_shdr
+ 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, external form */
821 Elf_Internal_Phdr
*i_phdr
; /* 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
= bfd_zalloc (abfd
, sizeof (elf_core_tdata
))) == NULL
)
887 bfd_error
= no_memory
;
891 /* Allocate space for copies of the program header table in external
892 and internal form, seek to the program header table in the file,
893 read it in, and convert it to internal form. As a simple sanity
894 check, verify that the what BFD thinks is the size of each program
895 header table entry actually matches the size recorded in the file. */
897 if (i_ehdr
.e_phentsize
!= sizeof (*x_phdr
))
899 if ((x_phdr
= (Elf_External_Phdr
*)
900 bfd_alloc (abfd
, sizeof (*x_phdr
) * i_ehdr
.e_phnum
)) == NULL
)
902 bfd_error
= no_memory
;
905 if ((i_phdr
= (Elf_Internal_Phdr
*)
906 bfd_alloc (abfd
, sizeof (*i_phdr
) * i_ehdr
.e_phnum
)) == NULL
)
908 bfd_error
= no_memory
;
911 if (bfd_seek (abfd
, i_ehdr
.e_phoff
, SEEK_SET
) == -1)
913 bfd_error
= system_call_error
;
916 for (phindex
= 0; phindex
< i_ehdr
.e_phnum
; phindex
++)
918 if (bfd_read ((PTR
) (x_phdr
+ phindex
), sizeof (*x_phdr
), 1, abfd
)
921 bfd_error
= system_call_error
;
924 elf_swap_phdr_in (abfd
, x_phdr
+ phindex
, i_phdr
+ phindex
);
927 /* Once all of the program headers have been read and converted, we
928 can start processing them. */
930 for (phindex
= 0; phindex
< i_ehdr
.e_phnum
; phindex
++)
932 bfd_section_from_phdr (abfd
, i_phdr
+ phindex
, phindex
);
933 if ((i_phdr
+ phindex
) -> p_type
== PT_NOTE
)
935 elf_corefile_note (abfd
, i_phdr
+ phindex
);
939 /* Remember the entry point specified in the ELF file header. */
941 bfd_get_start_address (abfd
) = i_ehdr
.e_entry
;
947 DEFUN (elf_mkobject
, (abfd
), bfd
*abfd
)
949 fprintf (stderr
, "elf_mkobject unimplemented\n");
956 DEFUN (elf_write_object_contents
, (abfd
), bfd
*abfd
)
958 fprintf (stderr
, "elf_write_object_contents unimplemented\n");
964 /* Given an index of a section, retrieve a pointer to it. Note
965 that for our purposes, sections are indexed by {1, 2, ...} with
966 0 being an illegal index. */
969 DEFUN (section_from_bfd_index
, (abfd
, index
),
975 struct sec
*answer
= abfd
-> sections
;
978 answer
= answer
-> next
;
986 DEFUN (elf_slurp_symbol_table
, (abfd
), bfd
*abfd
)
988 int symcount
; /* Number of external ELF symbols */
989 char *strtab
; /* Buffer for raw ELF string table section */
990 asymbol
*sym
; /* Pointer to current bfd symbol */
991 asymbol
*symbase
; /* Buffer for generated bfd symbols */
992 asymbol
**vec
; /* Pointer to current bfd symbol pointer */
993 Elf_Internal_Sym i_sym
;
994 Elf_External_Sym x_sym
;
996 if (bfd_get_outsymbols (abfd
) != NULL
)
1001 /* Slurp in the string table. We will keep it around permanently, as
1002 long as the bfd is in use, since we will end up setting up pointers
1003 into it for the names of all the symbols. */
1005 strtab
= elf_read (abfd
, elf_strtab_filepos(abfd
), elf_strtab_filesz(abfd
));
1011 /* Read each raw ELF symbol, converting from external ELF form to
1012 internal ELF form, and then using the information to create a
1013 canonical bfd symbol table entry.
1015 Note that be allocate the initial bfd canonical symbol buffer
1016 based on a one-to-one mapping of the ELF symbols to canonical
1017 symbols. However, it is likely that not all the ELF symbols will
1018 be used, so there will be some space leftover at the end. Once
1019 we know how many symbols we actual generate, we realloc the buffer
1020 to the correct size and then build the pointer vector. */
1022 if (bfd_seek (abfd
, elf_symtab_filepos (abfd
), SEEK_SET
) == -1)
1024 bfd_error
= system_call_error
;
1028 symcount
= elf_symtab_filesz(abfd
) / sizeof (Elf_External_Sym
);
1029 sym
= symbase
= (asymbol
*) bfd_zalloc (abfd
, symcount
* sizeof (asymbol
));
1031 while (symcount
-- > 0)
1033 if (bfd_read ((PTR
) &x_sym
, sizeof (x_sym
), 1, abfd
) != sizeof (x_sym
))
1035 bfd_error
= system_call_error
;
1038 elf_swap_symbol_in (abfd
, &x_sym
, &i_sym
);
1039 if (i_sym
.st_name
> 0)
1041 sym
-> the_bfd
= abfd
;
1042 sym
-> name
= strtab
+ i_sym
.st_name
;
1043 sym
-> value
= i_sym
.st_value
;
1044 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
1046 /* Note: This code depends upon there being an ordered
1047 one-for-one mapping of ELF sections to bfd sections. */
1048 sym
-> section
= section_from_bfd_index (abfd
, i_sym
.st_shndx
);
1050 else if (i_sym
.st_shndx
== SHN_ABS
)
1052 sym
-> flags
|= BSF_ABSOLUTE
;
1054 else if (i_sym
.st_shndx
== SHN_COMMON
)
1056 sym
-> flags
|= BSF_FORT_COMM
;
1058 switch (ELF_ST_BIND (i_sym
.st_info
))
1061 sym
-> flags
|= BSF_LOCAL
;
1064 sym
-> flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
1067 sym
-> flags
|= BSF_WEAK
;
1074 bfd_get_symcount(abfd
) = symcount
= sym
- symbase
;
1075 sym
= symbase
= (asymbol
*)
1076 bfd_realloc (abfd
, symbase
, symcount
* sizeof (asymbol
));
1077 bfd_get_outsymbols(abfd
) = vec
= (asymbol
**)
1078 bfd_alloc (abfd
, symcount
* sizeof (asymbol
*));
1080 while (symcount
-- > 0)
1088 /* Return the number of bytes required to hold the symtab vector.
1090 Note that we base it on the count plus 1, since we will null terminate
1091 the vector allocated based on this size. */
1094 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
*abfd
)
1096 unsigned int symtab_size
= 0;
1098 if (elf_slurp_symbol_table (abfd
))
1100 symtab_size
= (bfd_get_symcount (abfd
) + 1) * (sizeof (asymbol
));
1102 return (symtab_size
);
1106 elf_get_reloc_upper_bound (abfd
, asect
)
1110 fprintf (stderr
, "elf_get_reloc_upper_bound unimplemented\n");
1117 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
1123 fprintf (stderr
, "elf_canonicalize_reloc unimplemented\n");
1130 DEFUN (elf_get_symtab
, (abfd
, alocation
),
1132 asymbol
**alocation
)
1134 unsigned int symcount
;
1137 if (!elf_slurp_symbol_table (abfd
))
1143 symcount
= bfd_get_symcount (abfd
);
1144 vec
= bfd_get_outsymbols (abfd
);
1145 while (symcount
-- > 0)
1147 *alocation
++ = *vec
++;
1149 *alocation
++ = NULL
;
1150 return (bfd_get_symcount (abfd
));
1155 elf_make_empty_symbol(abfd
)
1158 fprintf (stderr
, "elf_make_empty_symbol unimplemented\n");
1165 DEFUN (elf_print_symbol
,(ignore_abfd
, filep
, symbol
, how
),
1166 bfd
*ignore_abfd AND
1169 bfd_print_symbol_type how
)
1171 fprintf (stderr
, "elf_print_symbol unimplemented\n");
1177 DEFUN (elf_get_lineno
,(ignore_abfd
, symbol
),
1178 bfd
*ignore_abfd AND
1181 fprintf (stderr
, "elf_get_lineno unimplemented\n");
1188 DEFUN (elf_set_arch_mach
,(abfd
, arch
, machine
),
1190 enum bfd_architecture arch AND
1191 unsigned long machine
)
1193 fprintf (stderr
, "elf_set_arch_mach unimplemented\n");
1195 /* Allow any architecture to be supported by the elf backend */
1196 return bfd_default_set_arch_mach(abfd
, arch
, machine
);
1200 DEFUN (elf_find_nearest_line
,(abfd
,
1208 asection
*section AND
1209 asymbol
**symbols AND
1211 CONST
char **filename_ptr AND
1212 CONST
char **functionname_ptr AND
1213 unsigned int *line_ptr
)
1215 fprintf (stderr
, "elf_find_nearest_line unimplemented\n");
1222 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
1226 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
1232 /* This structure contains everything that BFD knows about a target.
1233 It includes things like its byte order, name, what routines to call
1234 to do various operations, etc. Every BFD points to a target structure
1235 with its "xvec" member.
1237 There are two such structures here: one for big-endian machines and
1238 one for little-endian machines. */
1240 /* Archives are generic or unimplemented. */
1241 #define elf_slurp_armap bfd_false
1242 #define elf_slurp_extended_name_table _bfd_slurp_extended_name_table
1243 #define elf_truncate_arname bfd_dont_truncate_arname
1244 #define elf_openr_next_archived_file bfd_generic_openr_next_archived_file
1245 #define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt
1246 #define elf_write_armap (PROTO (boolean, (*), \
1247 (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count, \
1248 int stridx))) bfd_false
1250 /* Ordinary section reading and writing */
1251 #define elf_new_section_hook _bfd_dummy_new_section_hook
1252 #define elf_get_section_contents bfd_generic_get_section_contents
1253 #define elf_set_section_contents bfd_generic_set_section_contents
1254 #define elf_close_and_cleanup bfd_generic_close_and_cleanup
1256 #define elf_bfd_debug_info_start bfd_void
1257 #define elf_bfd_debug_info_end bfd_void
1258 #define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
1260 bfd_target elf_big_vec
=
1262 /* name: identify kind of target */
1265 /* flavour: general indication about file */
1266 bfd_target_elf_flavour
,
1268 /* byteorder_big_p: data is big endian */
1271 /* header_byteorder_big_p: header is also big endian */
1274 /* object_flags: mask of all file flags */
1275 (HAS_RELOC
| EXEC_P
| HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
|
1278 /* section_flags: mask of all section flags */
1279 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_READONLY
|
1282 /* ar_pad_char: pad character for filenames within an archive header
1283 FIXME: this really has nothing to do with ELF, this is a characteristic
1284 of the archiver and/or os and should be independently tunable */
1287 /* ar_max_namelen: maximum number of characters in an archive header
1288 FIXME: this really has nothing to do with ELF, this is a characteristic
1289 of the archiver and should be independently tunable. This value is
1290 a WAG (wild a** guess) */
1293 /* align_power_min: minimum alignment restriction for any section
1294 FIXME: this value may be target machine dependent */
1297 /* Routines to byte-swap various sized integers from the data sections */
1298 _do_getb64
, _do_putb64
, _do_getb32
, _do_putb32
, _do_getb16
, _do_putb16
,
1300 /* Routines to byte-swap various sized integers from the file headers */
1301 _do_getb64
, _do_putb64
, _do_getb32
, _do_putb32
, _do_getb16
, _do_putb16
,
1303 /* bfd_check_format: check the format of a file being read */
1304 { _bfd_dummy_target
, /* unknown format */
1305 elf_object_p
, /* assembler/linker output (object file) */
1306 bfd_generic_archive_p
, /* an archive */
1307 elf_core_file_p
/* a core file */
1310 /* bfd_set_format: set the format of a file being written */
1313 _bfd_generic_mkarchive
,
1317 /* bfd_write_contents: write cached information into a file being written */
1319 elf_write_object_contents
,
1320 _bfd_write_archive_contents
,
1324 /* Initialize a jump table with the standard macro. All names start
1332 bfd_target elf_little_vec
=
1334 /* name: identify kind of target */
1337 /* flavour: general indication about file */
1338 bfd_target_elf_flavour
,
1340 /* byteorder_big_p: data is big endian */
1341 false, /* Nope -- this one's little endian */
1343 /* header_byteorder_big_p: header is also big endian */
1344 false, /* Nope -- this one's little endian */
1346 /* object_flags: mask of all file flags */
1347 (HAS_RELOC
| EXEC_P
| HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
|
1350 /* section_flags: mask of all section flags */
1351 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_READONLY
|
1354 /* ar_pad_char: pad character for filenames within an archive header
1355 FIXME: this really has nothing to do with ELF, this is a characteristic
1356 of the archiver and/or os and should be independently tunable */
1359 /* ar_max_namelen: maximum number of characters in an archive header
1360 FIXME: this really has nothing to do with ELF, this is a characteristic
1361 of the archiver and should be independently tunable. This value is
1362 a WAG (wild a** guess) */
1365 /* align_power_min: minimum alignment restriction for any section
1366 FIXME: this value may be target machine dependent */
1369 /* Routines to byte-swap various sized integers from the data sections */
1370 _do_getl64
, _do_putl64
, _do_getl32
, _do_putl32
, _do_getl16
, _do_putl16
,
1372 /* Routines to byte-swap various sized integers from the file headers */
1373 _do_getl64
, _do_putl64
, _do_getl32
, _do_putl32
, _do_getl16
, _do_putl16
,
1375 /* bfd_check_format: check the format of a file being read */
1376 { _bfd_dummy_target
, /* unknown format */
1377 elf_object_p
, /* assembler/linker output (object file) */
1378 bfd_generic_archive_p
, /* an archive */
1379 elf_core_file_p
/* a core file */
1382 /* bfd_set_format: set the format of a file being written */
1385 _bfd_generic_mkarchive
,
1389 /* bfd_write_contents: write cached information into a file being written */
1391 elf_write_object_contents
,
1392 _bfd_write_archive_contents
,
1396 /* Initialize a jump table with the standard macro. All names start