1 /* ELF executable support for BFD.
2 Copyright (C) 1991, 1992 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". Sufficient support for gdb.
8 Rewritten by Mark Eichin @ Cygnus Support, from information
9 published in "System V Application Binary Interface", chapters 4
10 and 5, as well as the various "Processor Supplement" documents
11 derived from it. Added support for assembler and other object file
14 This file is part of BFD, the Binary File Descriptor library.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /****************************************
35 This is only a partial ELF implementation,
36 incorporating only those parts that are
37 required to get gdb up and running. It is
38 expected that it will be expanded to a full
39 ELF implementation at some future date.
41 Unimplemented stubs call abort() to ensure
42 that they get proper attention if they are
43 ever called. The stubs are here since
44 this version was hacked from the COFF
45 version, and thus they will probably
46 go away or get expanded appropriately in a
51 *****************************************/
54 /* Problems and other issues to resolve.
56 (1) BFD expects there to be some fixed number of "sections" in
57 the object file. I.E. there is a "section_count" variable in the
58 bfd structure which contains the number of sections. However, ELF
59 supports multiple "views" of a file. In particular, with current
60 implementations, executable files typically have two tables, a
61 program header table and a section header table, both of which
62 partition the executable.
64 In ELF-speak, the "linking view" of the file uses the section header
65 table to access "sections" within the file, and the "execution view"
66 uses the program header table to access "segments" within the file.
67 "Segments" typically may contain all the data from one or more
70 Note that the section header table is optional in ELF executables,
71 but it is this information that is most useful to gdb. If the
72 section header table is missing, then gdb should probably try
73 to make do with the program header table. (FIXME)
81 #include "elf/common.h"
82 #include "elf/internal.h"
83 #include "elf/external.h"
85 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
86 #include <sys/procfs.h>
88 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
89 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
90 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
93 /* Forward data declarations */
95 extern bfd_target elf_little_vec
, elf_big_vec
;
97 /* Currently the elf_symbol_type struct just contains the generic bfd
105 /* Some private data is stashed away for future use using the tdata pointer
106 in the bfd structure. */
110 Elf_Internal_Ehdr elf_header
[1]; /* Actual data, but ref like ptr */
111 Elf_Internal_Shdr
*elf_sect_ptr
;
112 struct strtab
*strtab_ptr
;
114 void *prstatus
; /* The raw /proc prstatus structure */
115 void *prpsinfo
; /* The raw /proc prpsinfo structure */
118 #define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data)
119 #define elf_elfheader(bfd) (elf_tdata(bfd) -> elf_header)
120 #define elf_elfsections(bfd) (elf_tdata(bfd) -> elf_sect_ptr)
121 #define elf_shstrtab(bfd) (elf_tdata(bfd) -> strtab_ptr)
122 #define elf_onesymtab(bfd) (elf_tdata(bfd) -> symtab_section)
123 #define core_prpsinfo(bfd) (elf_tdata(bfd) -> prpsinfo)
124 #define core_prstatus(bfd) (elf_tdata(bfd) -> prstatus)
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
);
143 /* Translate an ELF symbol in internal format into an ELF symbol in external
147 DEFUN(elf_swap_symbol_out
,(abfd
, src
, dst
),
149 Elf_Internal_Sym
*src AND
150 Elf_External_Sym
*dst
)
152 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
153 bfd_h_put_32 (abfd
, src
->st_value
, dst
->st_value
);
154 bfd_h_put_32 (abfd
, src
->st_size
, dst
->st_size
);
155 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
156 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
157 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
161 /* Translate an ELF file header in external format into an ELF file header in
165 DEFUN(elf_swap_ehdr_in
,(abfd
, src
, dst
),
167 Elf_External_Ehdr
*src AND
168 Elf_Internal_Ehdr
*dst
)
170 memcpy (dst
-> e_ident
, src
-> e_ident
, EI_NIDENT
);
171 dst
-> e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_type
);
172 dst
-> e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_machine
);
173 dst
-> e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_version
);
174 dst
-> e_entry
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_entry
);
175 dst
-> e_phoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_phoff
);
176 dst
-> e_shoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_shoff
);
177 dst
-> e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_flags
);
178 dst
-> e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_ehsize
);
179 dst
-> e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phentsize
);
180 dst
-> e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phnum
);
181 dst
-> e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shentsize
);
182 dst
-> e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shnum
);
183 dst
-> e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shstrndx
);
186 /* Translate an ELF file header in internal format into an ELF file header in
190 DEFUN(elf_swap_ehdr_out
,(abfd
, src
, dst
),
192 Elf_Internal_Ehdr
*src AND
193 Elf_External_Ehdr
*dst
)
195 memcpy (dst
-> e_ident
, src
-> e_ident
, EI_NIDENT
);
196 /* note that all elements of dst are *arrays of unsigned char* already... */
197 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
198 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
199 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
200 bfd_h_put_32 (abfd
, src
->e_entry
, dst
->e_entry
);
201 bfd_h_put_32 (abfd
, src
->e_phoff
, dst
->e_phoff
);
202 bfd_h_put_32 (abfd
, src
->e_shoff
, dst
->e_shoff
);
203 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
204 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
205 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
206 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
207 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
208 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
209 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
213 /* Translate an ELF section header table entry in external format into an
214 ELF section header table entry in internal format. */
217 DEFUN(elf_swap_shdr_in
,(abfd
, src
, dst
),
219 Elf_External_Shdr
*src AND
220 Elf_Internal_Shdr
*dst
)
222 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
223 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
224 dst
->sh_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_flags
);
225 dst
->sh_addr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_addr
);
226 dst
->sh_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_offset
);
227 dst
->sh_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_size
);
228 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
229 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
230 dst
->sh_addralign
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_addralign
);
231 dst
->sh_entsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_entsize
);
232 /* we haven't done any processing on it yet, so... */
233 dst
->rawdata
= (void*)0;
236 /* Translate an ELF section header table entry in internal format into an
237 ELF section header table entry in external format. */
240 DEFUN(elf_swap_shdr_out
,(abfd
, src
, dst
),
242 Elf_Internal_Shdr
*src AND
243 Elf_External_Shdr
*dst
)
245 /* note that all elements of dst are *arrays of unsigned char* already... */
246 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
247 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
248 bfd_h_put_32 (abfd
, src
->sh_flags
, dst
->sh_flags
);
249 bfd_h_put_32 (abfd
, src
->sh_addr
, dst
->sh_addr
);
250 bfd_h_put_32 (abfd
, src
->sh_offset
, dst
->sh_offset
);
251 bfd_h_put_32 (abfd
, src
->sh_size
, dst
->sh_size
);
252 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
253 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
254 bfd_h_put_32 (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
255 bfd_h_put_32 (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
259 /* Translate an ELF program header table entry in external format into an
260 ELF program header table entry in internal format. */
263 DEFUN(elf_swap_phdr_in
,(abfd
, src
, dst
),
265 Elf_External_Phdr
*src AND
266 Elf_Internal_Phdr
*dst
)
268 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
269 dst
->p_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_offset
);
270 dst
->p_vaddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_vaddr
);
271 dst
->p_paddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_paddr
);
272 dst
->p_filesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_filesz
);
273 dst
->p_memsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_memsz
);
274 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
275 dst
->p_align
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_align
);
279 /* Translate an ELF reloc from external format to internal format. */
281 DEFUN(elf_swap_reloc_in
,(abfd
, src
, dst
),
283 Elf_External_Rel
*src AND
284 Elf_Internal_Rel
*dst
)
286 dst
->r_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_offset
);
287 dst
->r_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_info
);
291 DEFUN(elf_swap_reloca_in
,(abfd
, src
, dst
),
293 Elf_External_Rela
*src AND
294 Elf_Internal_Rela
*dst
)
296 dst
->r_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_offset
);
297 dst
->r_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_info
);
298 dst
->r_addend
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_addend
);
301 /* Translate an ELF reloc from internal format to external format. */
303 DEFUN(elf_swap_reloc_out
,(abfd
, src
, dst
),
305 Elf_Internal_Rel
*src AND
306 Elf_External_Rel
*dst
)
308 bfd_h_put_32 (abfd
, src
->r_offset
, dst
->r_offset
);
309 bfd_h_put_32 (abfd
, src
->r_info
, dst
->r_info
);
313 DEFUN(elf_swap_reloca_out
,(abfd
, src
, dst
),
315 Elf_Internal_Rela
*src AND
316 Elf_External_Rela
*dst
)
318 bfd_h_put_32 (abfd
, src
->r_offset
, dst
->r_offset
);
319 bfd_h_put_32 (abfd
, src
->r_info
, dst
->r_info
);
320 bfd_h_put_32 (abfd
, src
->r_addend
, dst
->r_addend
);
323 static char *EXFUN(elf_read
, (bfd
*, long, int));
324 static struct sec
* EXFUN(section_from_elf_index
, (bfd
*, int));
325 static int EXFUN(elf_section_from_bfd_section
, (bfd
*, struct sec
*));
326 static boolean
EXFUN(elf_slurp_symbol_table
, (bfd
*, Elf_Internal_Shdr
*));
327 static void EXFUN(elf_info_to_howto
, (bfd
*, arelent
*, Elf_Internal_Rela
*));
328 static char *EXFUN(elf_get_str_section
, (bfd
*, unsigned int));
335 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
338 Helper functions for GDB to locate the string tables.
339 Since BFD hides string tables from callers, GDB needs to use an
340 internal hook to find them. Sun's .stabstr, in particular,
341 isn't even pointed to by the .stab section, so ordinary
342 mechanisms wouldn't work to find it, even if we had some.
345 struct elf_internal_shdr
*
346 DEFUN(bfd_elf_find_section
, (abfd
, name
),
350 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
351 char *shstrtab
= elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
352 unsigned int max
= elf_elfheader (abfd
)->e_shnum
;
355 for (i
= 1; i
< max
; i
++)
356 if (!strcmp (&shstrtab
[i_shdrp
[i
].sh_name
], name
))
361 /* End of GDB support. */
364 DEFUN(elf_get_str_section
, (abfd
, shindex
),
366 unsigned int shindex
)
368 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
369 unsigned int shstrtabsize
= i_shdrp
[shindex
].sh_size
;
370 unsigned int offset
= i_shdrp
[shindex
].sh_offset
;
371 char *shstrtab
= i_shdrp
[shindex
].rawdata
;
376 if ((shstrtab
= elf_read (abfd
, offset
, shstrtabsize
)) == NULL
)
380 i_shdrp
[shindex
].rawdata
= (void*)shstrtab
;
385 DEFUN(elf_string_from_elf_section
, (abfd
, shindex
, strindex
),
387 unsigned int shindex AND
388 unsigned int strindex
)
390 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
391 Elf_Internal_Shdr
*hdr
= i_shdrp
+ shindex
;
395 if (elf_get_str_section (abfd
, shindex
) == NULL
)
400 return ((char*)hdr
->rawdata
)+strindex
;
403 #define elf_string_from_elf_strtab(abfd, strindex) \
404 elf_string_from_elf_section (abfd, elf_elfheader(abfd)->e_shstrndx, strindex)
406 /* Create a new bfd section from an ELF section header. */
409 DEFUN(bfd_section_from_shdr
, (abfd
, shindex
),
411 unsigned int shindex
)
413 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
414 Elf_Internal_Shdr
*hdr
= i_shdrp
+ shindex
;
418 name
= hdr
->sh_name
?
419 elf_string_from_elf_strtab (abfd
, hdr
->sh_name
) : "unnamed";
421 switch(hdr
->sh_type
) {
423 /* inactive section. Throw it away. */
427 /* Bits that get saved. This one is real. */
430 newsect
= bfd_make_section (abfd
, name
);
431 newsect
->vma
= hdr
->sh_addr
;
432 newsect
->_raw_size
= hdr
->sh_size
;
433 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
434 newsect
->flags
|= SEC_HAS_CONTENTS
;
436 if (hdr
->sh_flags
& SHF_ALLOC
)
438 newsect
->flags
|= SEC_ALLOC
;
439 if (hdr
->sh_type
!= SHT_NOBITS
)
440 newsect
->flags
|= SEC_LOAD
;
443 if (!(hdr
->sh_flags
& SHF_WRITE
))
444 newsect
->flags
|= SEC_READONLY
;
446 if (hdr
->sh_flags
& SHF_EXECINSTR
)
447 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
449 newsect
->flags
|= SEC_DATA
;
451 hdr
->rawdata
= (void*)newsect
;
456 /* we may be getting called by reference. Bring'em in... */
457 if (! hdr
->rawdata
) {
458 /* fetch our corresponding string table. */
459 bfd_section_from_shdr (abfd
, hdr
->sh_link
);
461 /* start turning our elf symbols into bfd symbols. */
462 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
463 elf_slurp_symbol_table (abfd
, hdr
);
464 abfd
->flags
|= HAS_SYMS
;
469 /* we may be getting called by reference. Bring'em in... */
472 /* we don't need to do anything, just make the data available. */
473 if (elf_get_str_section (abfd
, shindex
) == NULL
)
479 /* *these* do a lot of work -- but build no sections! */
480 /* the spec says there can be multiple strtabs, but only one symtab */
481 /* but there can be lots of REL* sections. */
482 /* FIXME: The above statement is wrong! There are typically at least
483 two symbol tables in a dynamically linked executable, ".dynsym"
484 which is the dynamic linkage symbol table and ".symtab", which is
485 the "traditional" symbol table. -fnf */
488 asection
*target_sect
;
491 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
492 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
493 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
496 /* FIXME: We are only prepared to read one symbol table, so
497 do NOT read the dynamic symbol table since it is only a
498 subset of the full symbol table. Also see comment above. -fnf */
499 if (!elf_slurp_symbol_table(abfd
, i_shdrp
+ hdr
->sh_link
))
503 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
504 target_sect
->flags
|= SEC_RELOC
;
505 target_sect
->relocation
= 0;
506 target_sect
->rel_filepos
= hdr
->sh_offset
;
508 fprintf(stderr
, "ELF>> section %s reading %d relocs\n",
509 target_sect
->name
, target_sect
->reloc_count
);
517 case SHT_DYNSYM
: /* could treat this like symtab... */
519 fprintf(stderr
, "Dynamic Linking sections not yet supported.\n");
525 fprintf(stderr
, "Note Sections not yet supported.\n");
531 fprintf(stderr
, "SHLIB Sections not supported (and non conforming.)\n");
552 static struct strtab
*
553 DEFUN(bfd_new_strtab
, (abfd
),
558 ss
= (struct strtab
*)malloc(sizeof(struct strtab
));
560 BFD_ASSERT(ss
->tab
!= 0);
569 DEFUN(bfd_add_to_strtab
, (abfd
, ss
, str
),
571 struct strtab
*ss AND
574 /* should search first, but for now: */
575 /* include the trailing NUL */
576 int ln
= strlen(str
)+1;
578 /* should this be using obstacks? */
579 ss
->tab
= realloc(ss
->tab
, ss
->length
+ ln
);
581 BFD_ASSERT(ss
->tab
!= 0);
582 strcpy(ss
->tab
+ ss
->length
, str
);
586 return ss
->length
- ln
;
590 DEFUN(bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
592 struct strtab
*ss AND
596 /* should search first, but for now: */
597 /* include the trailing NUL */
598 int ln
= strlen(str
)+strlen(str2
)+1;
600 /* should this be using obstacks? */
602 ss
->tab
= realloc(ss
->tab
, ss
->length
+ ln
);
604 ss
->tab
= malloc(ln
);
606 BFD_ASSERT(ss
->tab
!= 0);
607 strcpy(ss
->tab
+ ss
->length
, str
);
608 strcpy(ss
->tab
+ ss
->length
+ strlen(str
), str2
);
612 return ss
->length
- ln
;
615 /* Create a new ELF section from a bfd section. */
618 DEFUN(bfd_shdr_from_section
, (abfd
, hdr
, shstrtab
, indx
),
620 Elf_Internal_Shdr
*hdr AND
621 struct strtab
*shstrtab AND
627 /* figure out out to write the section name from the bfd section name. MWE */
629 sect
= abfd
->sections
;
630 for (ndx
= indx
; --ndx
; )
634 hdr
[indx
].sh_name
= bfd_add_to_strtab(abfd
, shstrtab
,
635 bfd_section_name(abfd
, sect
));
636 hdr
[indx
].sh_addr
= sect
->vma
;
637 hdr
[indx
].sh_size
= sect
->_raw_size
;
638 hdr
[indx
].sh_flags
= 0;
639 /* these need to be preserved on */
640 hdr
[indx
].sh_link
= 0;
641 hdr
[indx
].sh_info
= 0;
642 hdr
[indx
].sh_addralign
= 0;
643 hdr
[indx
].sh_entsize
= 0;
645 hdr
[indx
].sh_type
= 0;
646 if (sect
->flags
& SEC_RELOC
) {
647 hdr
[indx
].sh_type
= SHT_RELA
; /* FIXME -- sparc specific */
650 if (sect
->flags
& SEC_HAS_CONTENTS
)
652 hdr
[indx
].sh_offset
= sect
->filepos
;
653 hdr
[indx
].sh_size
= sect
->_raw_size
;
655 if (sect
->flags
& SEC_ALLOC
)
657 hdr
[indx
].sh_flags
|= SHF_ALLOC
;
658 if (sect
->flags
& SEC_LOAD
)
660 /* do something with sh_type ? */
663 if (!(sect
->flags
& SEC_READONLY
))
664 hdr
[indx
].sh_flags
|= SHF_WRITE
;
666 if (sect
->flags
& SEC_CODE
)
667 hdr
[indx
].sh_flags
|= SHF_EXECINSTR
;
672 /* Create a new bfd section from an ELF program header.
674 Since program segments have no names, we generate a synthetic name
675 of the form segment<NUM>, where NUM is generally the index in the
676 program header table. For segments that are split (see below) we
677 generate the names segment<NUM>a and segment<NUM>b.
679 Note that some program segments may have a file size that is different than
680 (less than) the memory size. All this means is that at execution the
681 system must allocate the amount of memory specified by the memory size,
682 but only initialize it with the first "file size" bytes read from the
683 file. This would occur for example, with program segments consisting
684 of combined data+bss.
686 To handle the above situation, this routine generates TWO bfd sections
687 for the single program segment. The first has the length specified by
688 the file size of the segment, and the second has the length specified
689 by the difference between the two sizes. In effect, the segment is split
690 into it's initialized and uninitialized parts.
695 DEFUN(bfd_section_from_phdr
, (abfd
, hdr
, index
),
697 Elf_Internal_Phdr
*hdr AND
705 split
= ((hdr
-> p_memsz
> 0) &&
706 (hdr
-> p_filesz
> 0) &&
707 (hdr
-> p_memsz
> hdr
-> p_filesz
));
708 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
709 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
710 strcpy (name
, namebuf
);
711 newsect
= bfd_make_section (abfd
, name
);
712 newsect
-> vma
= hdr
-> p_vaddr
;
713 newsect
-> _raw_size
= hdr
-> p_filesz
;
714 newsect
-> filepos
= hdr
-> p_offset
;
715 newsect
-> flags
|= SEC_HAS_CONTENTS
;
716 if (hdr
-> p_type
== PT_LOAD
)
718 newsect
-> flags
|= SEC_ALLOC
;
719 newsect
-> flags
|= SEC_LOAD
;
720 if (hdr
-> p_flags
& PF_X
)
722 /* FIXME: all we known is that it has execute PERMISSION,
724 newsect
-> flags
|= SEC_CODE
;
727 if (!(hdr
-> p_flags
& PF_W
))
729 newsect
-> flags
|= SEC_READONLY
;
734 sprintf (namebuf
, "segment%db", index
);
735 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
736 strcpy (name
, namebuf
);
737 newsect
= bfd_make_section (abfd
, name
);
738 newsect
-> vma
= hdr
-> p_vaddr
+ hdr
-> p_filesz
;
739 newsect
-> _raw_size
= hdr
-> p_memsz
- hdr
-> p_filesz
;
740 if (hdr
-> p_type
== PT_LOAD
)
742 newsect
-> flags
|= SEC_ALLOC
;
743 if (hdr
-> p_flags
& PF_X
)
744 newsect
-> flags
|= SEC_CODE
;
746 if (!(hdr
-> p_flags
& PF_W
))
747 newsect
-> flags
|= SEC_READONLY
;
756 DEFUN(bfd_prstatus
,(abfd
, descdata
, descsz
, filepos
),
763 prstatus_t
*status
= (prstatus_t
*)0;
765 if (descsz
== sizeof (prstatus_t
))
767 newsect
= bfd_make_section (abfd
, ".reg");
768 newsect
-> _raw_size
= sizeof (status
->pr_reg
);
769 newsect
-> filepos
= filepos
+ (long) &status
->pr_reg
;
770 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
771 newsect
-> alignment_power
= 2;
772 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
774 memcpy (core_prstatus (abfd
), descdata
, descsz
);
779 /* Stash a copy of the prpsinfo structure away for future use. */
782 DEFUN(bfd_prpsinfo
,(abfd
, descdata
, descsz
, filepos
),
790 if (descsz
== sizeof (prpsinfo_t
))
792 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
794 bcopy (descdata
, core_prpsinfo (abfd
), descsz
);
800 DEFUN(bfd_fpregset
,(abfd
, descdata
, descsz
, filepos
),
808 newsect
= bfd_make_section (abfd
, ".reg2");
809 newsect
-> _raw_size
= descsz
;
810 newsect
-> filepos
= filepos
;
811 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
812 newsect
-> alignment_power
= 2;
815 #endif /* HAVE_PROCFS */
817 /* Return a pointer to the args (including the command name) that were
818 seen by the program that generated the core dump. Note that for
819 some reason, a spurious space is tacked onto the end of the args
820 in some (at least one anyway) implementations, so strip it off if
824 DEFUN(elf_core_file_failing_command
, (abfd
),
828 if (core_prpsinfo (abfd
))
830 prpsinfo_t
*p
= core_prpsinfo (abfd
);
831 char *scan
= p
-> pr_psargs
;
834 if ((scan
> p
-> pr_psargs
) && (*scan
== ' '))
838 return (p
-> pr_psargs
);
844 /* Return the number of the signal that caused the core dump. Presumably,
845 since we have a core file, we got a signal of some kind, so don't bother
846 checking the other process status fields, just return the signal number.
850 DEFUN(elf_core_file_failing_signal
, (abfd
),
854 if (core_prstatus (abfd
))
856 return (((prstatus_t
*)(core_prstatus (abfd
))) -> pr_cursig
);
862 /* Check to see if the core file could reasonably be expected to have
863 come for the current executable file. Note that by default we return
864 true unless we find something that indicates that there might be a
869 DEFUN(elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
878 /* First, xvecs must match since both are ELF files for the same target. */
880 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
882 bfd_error
= system_call_error
;
888 /* If no prpsinfo, just return true. Otherwise, grab the last component
889 of the exec'd pathname from the prpsinfo. */
891 if (core_prpsinfo (core_bfd
))
893 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
)) -> pr_fname
);
900 /* Find the last component of the executable pathname. */
902 if ((execname
= strrchr (exec_bfd
-> filename
, '/')) != NULL
)
908 execname
= (char *) exec_bfd
-> filename
;
911 /* See if they match */
913 return (strcmp (execname
, corename
) ? false : true);
919 #endif /* HAVE_PROCFS */
922 /* ELF core files contain a segment of type PT_NOTE, that holds much of
923 the information that would normally be available from the /proc interface
924 for the process, at the time the process dumped core. Currently this
925 includes copies of the prstatus, prpsinfo, and fpregset structures.
927 Since these structures are potentially machine dependent in size and
928 ordering, bfd provides two levels of support for them. The first level,
929 available on all machines since it does not require that the host
930 have /proc support or the relevant include files, is to create a bfd
931 section for each of the prstatus, prpsinfo, and fpregset structures,
932 without any interpretation of their contents. With just this support,
933 the bfd client will have to interpret the structures itself. Even with
934 /proc support, it might want these full structures for it's own reasons.
936 In the second level of support, where HAVE_PROCFS is defined, bfd will
937 pick apart the structures to gather some additional information that
938 clients may want, such as the general register set, the name of the
939 exec'ed file and its arguments, the signal (if any) that caused the
945 DEFUN(elf_corefile_note
, (abfd
, hdr
),
947 Elf_Internal_Phdr
*hdr
)
949 Elf_External_Note
*x_note_p
; /* Elf note, external form */
950 Elf_Internal_Note i_note
; /* Elf note, internal form */
951 char *buf
= NULL
; /* Entire note segment contents */
952 char *namedata
; /* Name portion of the note */
953 char *descdata
; /* Descriptor portion of the note */
954 char *sectname
; /* Name to use for new section */
955 long filepos
; /* File offset to descriptor data */
958 if (hdr
-> p_filesz
> 0
959 && (buf
= (char *) bfd_xmalloc (hdr
-> p_filesz
)) != NULL
960 && bfd_seek (abfd
, hdr
-> p_offset
, SEEK_SET
) != -1L
961 && bfd_read ((PTR
) buf
, hdr
-> p_filesz
, 1, abfd
) == hdr
-> p_filesz
)
963 x_note_p
= (Elf_External_Note
*) buf
;
964 while ((char *) x_note_p
< (buf
+ hdr
-> p_filesz
))
966 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> namesz
);
967 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> descsz
);
968 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> type
);
969 namedata
= x_note_p
-> name
;
970 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
971 filepos
= hdr
-> p_offset
+ (descdata
- buf
);
972 switch (i_note
.type
) {
974 /* process descdata as prstatus info */
975 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
976 sectname
= ".prstatus";
979 /* process descdata as fpregset info */
980 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
981 sectname
= ".fpregset";
984 /* process descdata as prpsinfo */
985 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
986 sectname
= ".prpsinfo";
989 /* Unknown descriptor, just ignore it. */
993 if (sectname
!= NULL
)
995 newsect
= bfd_make_section (abfd
, sectname
);
996 newsect
-> _raw_size
= i_note
.descsz
;
997 newsect
-> filepos
= filepos
;
998 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
999 newsect
-> alignment_power
= 2;
1001 x_note_p
= (Elf_External_Note
*)
1002 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
1014 /* Read a specified number of bytes at a specified offset in an ELF
1015 file, into a newly allocated buffer, and return a pointer to the
1019 DEFUN(elf_read
, (abfd
, offset
, size
),
1026 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
1028 bfd_error
= no_memory
;
1031 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
1033 bfd_error
= system_call_error
;
1036 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
1038 bfd_error
= system_call_error
;
1044 /* Begin processing a given object.
1046 First we validate the file by reading in the ELF header and checking
1052 DEFUN (elf_object_p
, (abfd
), bfd
*abfd
)
1054 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1055 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1056 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
1057 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1059 char *shstrtab
; /* Internal copy of section header stringtab */
1060 Elf_Off offset
; /* Temp place to stash file offsets */
1062 /* Read in the ELF header in external format. */
1064 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
1066 bfd_error
= system_call_error
;
1070 /* Now check to see if we have a valid ELF file, and one that BFD can
1071 make use of. The magic number must match, the address size ('class')
1072 and byte-swapping must match our XVEC entry, and it must have a
1073 section header table (FIXME: See comments re sections at top of this
1076 if (x_ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
1077 x_ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
1078 x_ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
1079 x_ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
1082 bfd_error
= wrong_format
;
1086 /* FIXME, Check EI_VERSION here ! */
1088 switch (x_ehdr
.e_ident
[EI_CLASS
])
1090 case ELFCLASSNONE
: /* address size not specified */
1091 goto wrong
; /* No support if can't tell address size */
1092 case ELFCLASS32
: /* 32-bit addresses */
1094 case ELFCLASS64
: /* 64-bit addresses */
1095 goto wrong
; /* FIXME: 64 bits not yet supported */
1097 goto wrong
; /* No support if unknown address class */
1100 /* Switch xvec to match the specified byte order. */
1101 switch (x_ehdr
.e_ident
[EI_DATA
])
1103 case ELFDATA2MSB
: /* Big-endian */
1104 if (!abfd
->xvec
->header_byteorder_big_p
)
1107 case ELFDATA2LSB
: /* Little-endian */
1108 if (abfd
->xvec
->header_byteorder_big_p
)
1111 case ELFDATANONE
: /* No data encoding specified */
1112 default: /* Unknown data encoding specified */
1116 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1117 the tdata pointer in the bfd. */
1119 if (NULL
== (elf_tdata (abfd
) = (struct elf_obj_tdata
*)
1120 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
))))
1122 bfd_error
= no_memory
;
1126 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
1128 /* Now that we know the byte order, swap in the rest of the header */
1129 i_ehdrp
= elf_elfheader (abfd
);
1130 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
1132 /* If there is no section header table, we're hosed. */
1133 if (i_ehdrp
->e_shoff
== 0)
1136 if (i_ehdrp
->e_type
== ET_EXEC
|| i_ehdrp
->e_type
== ET_DYN
)
1137 abfd
-> flags
|= EXEC_P
;
1139 switch (i_ehdrp
->e_machine
)
1142 case EM_M32
: /* or should this be bfd_arch_obscure? */
1143 bfd_default_set_arch_mach(abfd
, bfd_arch_unknown
, 0);
1146 bfd_default_set_arch_mach(abfd
, bfd_arch_sparc
, 0);
1149 bfd_default_set_arch_mach(abfd
, bfd_arch_i386
, 0);
1152 bfd_default_set_arch_mach(abfd
, bfd_arch_m68k
, 0);
1155 bfd_default_set_arch_mach(abfd
, bfd_arch_m88k
, 0);
1158 bfd_default_set_arch_mach(abfd
, bfd_arch_i860
, 0);
1161 bfd_default_set_arch_mach(abfd
, bfd_arch_mips
, 0);
1167 /* Allocate space for a copy of the section header table in
1168 internal form, seek to the section header table in the file,
1169 read it in, and convert it to internal form. As a simple sanity
1170 check, verify that the what BFD thinks is the size of each section
1171 header table entry actually matches the size recorded in the file. */
1173 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
1175 i_shdrp
= (Elf_Internal_Shdr
*)
1176 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
1179 bfd_error
= no_memory
;
1182 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
1184 bfd_error
= system_call_error
;
1187 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1189 if (bfd_read ((PTR
) &x_shdr
, sizeof x_shdr
, 1, abfd
)
1192 bfd_error
= system_call_error
;
1195 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
1198 elf_elfsections (abfd
) = i_shdrp
;
1200 /* Read in the string table containing the names of the sections. We
1201 will need the base pointer to this table later. */
1202 /* We read this inline now, so that we don't have to go through
1203 bfd_section_from_shdr with it (since this particular strtab is
1204 used to find all of the ELF section names.) */
1206 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
1210 /* Once all of the section headers have been read and converted, we
1211 can start processing them. Note that the first section header is
1212 a dummy placeholder entry, so we ignore it.
1214 We also watch for the symbol table section and remember the file
1215 offset and section size for both the symbol table section and the
1216 associated string table section. */
1218 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1220 bfd_section_from_shdr (abfd
, shindex
);
1223 /* Remember the entry point specified in the ELF file header. */
1225 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1227 return (abfd
->xvec
);
1230 /* Core files are simply standard ELF formatted files that partition
1231 the file using the execution view of the file (program header table)
1232 rather than the linking view. In fact, there is no section header
1233 table in a core file.
1235 The process status information (including the contents of the general
1236 register set) and the floating point register set are stored in a
1237 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
1238 that allow standard bfd access to the general registers (.reg) and the
1239 floating point registers (.reg2).
1244 DEFUN (elf_core_file_p
, (abfd
), bfd
*abfd
)
1246 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1247 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1248 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
1249 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
1250 unsigned int phindex
;
1252 /* Read in the ELF header in external format. */
1254 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
1256 bfd_error
= system_call_error
;
1260 /* Now check to see if we have a valid ELF file, and one that BFD can
1261 make use of. The magic number must match, the address size ('class')
1262 and byte-swapping must match our XVEC entry, and it must have a
1263 program header table (FIXME: See comments re segments at top of this
1266 if (x_ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
1267 x_ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
1268 x_ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
1269 x_ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
1272 bfd_error
= wrong_format
;
1276 /* FIXME, Check EI_VERSION here ! */
1278 switch (x_ehdr
.e_ident
[EI_CLASS
])
1280 case ELFCLASSNONE
: /* address size not specified */
1281 goto wrong
; /* No support if can't tell address size */
1282 case ELFCLASS32
: /* 32-bit addresses */
1284 case ELFCLASS64
: /* 64-bit addresses */
1285 goto wrong
; /* FIXME: 64 bits not yet supported */
1287 goto wrong
; /* No support if unknown address class */
1290 /* Switch xvec to match the specified byte order. */
1291 switch (x_ehdr
.e_ident
[EI_DATA
])
1293 case ELFDATA2MSB
: /* Big-endian */
1294 abfd
->xvec
= &elf_big_vec
;
1296 case ELFDATA2LSB
: /* Little-endian */
1297 abfd
->xvec
= &elf_little_vec
;
1299 case ELFDATANONE
: /* No data encoding specified */
1300 default: /* Unknown data encoding specified */
1304 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1305 the tdata pointer in the bfd. */
1308 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
1309 if (elf_tdata (abfd
) == NULL
)
1311 bfd_error
= no_memory
;
1315 /* FIXME, `wrong' returns from this point onward, leak memory. */
1317 /* Now that we know the byte order, swap in the rest of the header */
1318 i_ehdrp
= elf_elfheader (abfd
);
1319 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
1321 /* If there is no program header, or the type is not a core file, then
1323 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
1326 /* Allocate space for a copy of the program header table in
1327 internal form, seek to the program header table in the file,
1328 read it in, and convert it to internal form. As a simple sanity
1329 check, verify that the what BFD thinks is the size of each program
1330 header table entry actually matches the size recorded in the file. */
1332 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
1334 i_phdrp
= (Elf_Internal_Phdr
*)
1335 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
1338 bfd_error
= no_memory
;
1341 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
1343 bfd_error
= system_call_error
;
1346 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
1348 if (bfd_read ((PTR
) &x_phdr
, sizeof (x_phdr
), 1, abfd
)
1351 bfd_error
= system_call_error
;
1354 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
1357 /* Once all of the program headers have been read and converted, we
1358 can start processing them. */
1360 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
1362 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
1363 if ((i_phdrp
+ phindex
) -> p_type
== PT_NOTE
)
1365 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
1369 /* Remember the entry point specified in the ELF file header. */
1371 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1373 return (abfd
->xvec
);
1377 DEFUN (elf_mkobject
, (abfd
), bfd
*abfd
)
1379 /* this just does initialization */
1380 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
1381 elf_tdata(abfd
) = (struct elf_obj_tdata
*)
1382 bfd_zalloc (abfd
, sizeof(struct elf_obj_tdata
));
1383 if (elf_tdata(abfd
) == 0) {
1384 bfd_error
= no_memory
;
1387 /* since everything is done at close time, do we need any
1394 Create ELF output from BFD sections.
1396 Essentially, just create the section header and forget about the program
1401 /* lacking nested functions and nested types, set up for mapping over
1402 BFD sections to produce ELF sections */
1405 Elf_Internal_Ehdr
*i_ehdr
;
1406 Elf_Internal_Shdr
*i_shdrp
;
1407 struct strtab
*shstrtab
;
1414 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1419 elf_sect_thunk
*thunk
= (elf_sect_thunk
*)obj
;
1420 /* most of what is in bfd_shdr_from_section goes in here... */
1421 /* and all of these sections generate at *least* one ELF section. */
1425 /* check if we're making a PROGBITS section... */
1426 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1427 /* this was too strict... what *do* we want to check here? */
1430 Elf_Internal_Shdr
*this_hdr
;
1431 this_section
= elf_section_from_bfd_section (abfd
, asect
);
1432 this_hdr
= &thunk
->i_shdrp
[this_section
];
1434 this_hdr
->sh_addr
= asect
->vma
;
1435 this_hdr
->sh_size
= asect
->_raw_size
;
1436 /* contents already set by elf_set_section_contents */
1438 if (asect
->flags
& SEC_RELOC
)
1440 /* emit a reloc section, and thus strtab and symtab... */
1441 Elf_Internal_Shdr
*rela_hdr
;
1442 Elf_Internal_Shdr
*symtab_hdr
;
1443 Elf_Internal_Shdr
*symstrtab_hdr
;
1444 Elf_External_Rela
*outbound_relocs
;
1445 Elf_External_Sym
*outbound_syms
;
1447 int symstrtab_section
;
1449 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1451 if (thunk
->symtab_section
== this_section
+ 1)
1452 rela_section
= thunk
->symtab_section
+ 2; /* symtab + symstrtab */
1454 rela_section
= this_section
+ 1;
1455 rela_hdr
= &thunk
->i_shdrp
[rela_section
];
1456 rela_hdr
->sh_type
= SHT_RELA
;
1457 rela_hdr
->sh_link
= thunk
->symtab_section
;
1458 rela_hdr
->sh_info
= this_section
;
1459 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1460 /* orelocation has the data, reloc_count has the count... */
1461 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* asect
->reloc_count
;
1462 fprintf(stderr
,"ELF>> sending out %d relocs to %s\n",
1463 asect
->reloc_count
, asect
->name
);
1464 outbound_relocs
= (Elf_External_Rela
*)
1465 bfd_alloc(abfd
, asect
->reloc_count
* sizeof(Elf_External_Rela
));
1466 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
1468 Elf_Internal_Rela dst
;
1470 Elf_External_Rela
*src
;
1472 ptr
= asect
->orelocation
[idx
];
1473 src
= outbound_relocs
+ idx
;
1474 if (asect
->flags
& SEC_RELOC
)
1475 dst
.r_offset
= ptr
->address
- asect
->vma
;
1477 dst
.r_offset
= ptr
->address
;
1479 dst
.r_info
= ELF_R_INFO(1 /*ptr->sym_ptr_ptr*/, /* needs index into symtab (FIXME) */
1482 dst
.r_addend
= ptr
->addend
;
1483 elf_swap_reloca_out(abfd
, &dst
, src
);
1485 rela_hdr
->contents
= (void*)outbound_relocs
;
1491 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1496 elf_sect_thunk
*thunk
= (elf_sect_thunk
*)obj
;
1497 /* most of what is in bfd_shdr_from_section goes in here... */
1498 /* and all of these sections generate at *least* one ELF section. */
1502 /* check if we're making a PROGBITS section... */
1503 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1504 /* this was too strict... what *do* we want to check here? */
1507 Elf_Internal_Shdr
*this_hdr
;
1508 this_section
= thunk
->i_ehdr
->e_shnum
++;
1509 this_hdr
= &thunk
->i_shdrp
[this_section
];
1511 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, asect
->name
);
1512 /* we need to log the type *now* so that elf_section_from_bfd_section
1513 can find us... have to set rawdata too. */
1514 this_hdr
->rawdata
= (void*)asect
;
1515 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1516 this_hdr
->sh_type
= SHT_PROGBITS
;
1518 /* what *do* we put here? */
1519 this_hdr
->sh_type
= SHT_PROGBITS
;
1522 if (asect
->flags
& SEC_RELOC
)
1524 /* emit a reloc section, and thus strtab and symtab... */
1525 Elf_Internal_Shdr
*rela_hdr
;
1526 Elf_Internal_Shdr
*symtab_hdr
;
1527 Elf_Internal_Shdr
*symstrtab_hdr
;
1528 Elf_External_Rela
*outbound_relocs
;
1529 Elf_External_Sym
*outbound_syms
;
1531 int symstrtab_section
;
1533 /* note that only one symtab is used, so just remember it
1535 if (! thunk
->symtab_section
)
1537 thunk
->symtab_section
= thunk
->i_ehdr
->e_shnum
++;
1538 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1539 symtab_hdr
->sh_name
=
1540 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, ".symtab");
1541 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1542 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1544 symstrtab_section
= thunk
->i_ehdr
->e_shnum
++;
1545 BFD_ASSERT(symstrtab_section
== thunk
->symtab_section
+1);
1546 symstrtab_hdr
= &thunk
->i_shdrp
[symstrtab_section
];
1547 symtab_hdr
->sh_link
= symstrtab_section
;
1548 symstrtab_hdr
->sh_name
=
1549 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, ".strtab");
1550 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1552 symtab_hdr
->contents
= 0;
1553 symstrtab_hdr
->contents
= 0;
1554 symstrtab_hdr
->sh_size
= 0;
1557 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1559 rela_section
= thunk
->i_ehdr
->e_shnum
++;
1560 rela_hdr
= &thunk
->i_shdrp
[rela_section
];
1562 bfd_add_2_to_strtab (abfd
, thunk
->shstrtab
, ".rela", asect
->name
);
1563 rela_hdr
->sh_type
= SHT_RELA
;
1564 rela_hdr
->sh_link
= thunk
->symtab_section
;
1565 rela_hdr
->sh_info
= this_section
;
1566 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1573 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
*abfd
)
1575 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1576 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1577 struct strtab
*shstrtab
;
1578 int count
, maxsections
;
1582 if (! elf_shstrtab (abfd
)) {
1583 i_ehdrp
= elf_elfheader (abfd
); /* build new header in tdata memory */
1584 shstrtab
= bfd_new_strtab(abfd
);
1586 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
1587 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
1588 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
1589 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
1591 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS32
; /* FIXME: find out from bfd */
1592 i_ehdrp
->e_ident
[EI_DATA
] =
1593 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
1594 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1596 for(count
= EI_PAD
; count
< EI_NIDENT
; count
++)
1597 i_ehdrp
->e_ident
[count
] = 0;
1599 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
)? ET_EXEC
: ET_REL
;
1600 switch(bfd_get_arch(abfd
))
1602 case bfd_arch_unknown
:
1603 i_ehdrp
->e_machine
= EM_NONE
;
1605 case bfd_arch_sparc
:
1606 i_ehdrp
->e_machine
= EM_SPARC
;
1609 i_ehdrp
->e_machine
= EM_386
;
1612 i_ehdrp
->e_machine
= EM_68K
;
1615 i_ehdrp
->e_machine
= EM_88K
;
1618 i_ehdrp
->e_machine
= EM_860
;
1620 case bfd_arch_mips
: /* MIPS Rxxxx */
1621 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
1623 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1625 i_ehdrp
->e_machine
= EM_NONE
;
1627 i_ehdrp
->e_version
= EV_CURRENT
;
1628 i_ehdrp
->e_ehsize
= sizeof(Elf_External_Ehdr
);
1630 /* no program header, for now. */
1631 i_ehdrp
->e_phoff
= 0;
1632 i_ehdrp
->e_phentsize
= 0;
1633 i_ehdrp
->e_phnum
= 0;
1635 /* each bfd section is section header entry */
1636 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
1637 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
1639 /* figure at most each section can have a rel, strtab, symtab */
1640 maxsections
= 4*bfd_count_sections(abfd
)+2;
1642 i_ehdrp
->e_shoff
= i_ehdrp
->e_ehsize
;
1644 /* and we'll just have to fix up the offsets later. */
1645 /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */
1647 i_shdrp
= (Elf_Internal_Shdr
*)
1648 bfd_alloc (abfd
, sizeof (*i_shdrp
) * maxsections
);
1651 bfd_error
= no_memory
;
1654 for (count
=0; count
< maxsections
; count
++)
1656 i_shdrp
[count
].rawdata
= 0;
1657 i_shdrp
[count
].contents
= 0;
1661 i_shdrp
[0].sh_name
= 0;
1662 i_shdrp
[0].sh_type
= SHT_NULL
;
1663 i_shdrp
[0].sh_flags
= 0;
1664 i_shdrp
[0].sh_addr
= 0;
1665 i_shdrp
[0].sh_offset
= 0;
1666 i_shdrp
[0].sh_size
= 0;
1667 i_shdrp
[0].sh_link
= SHN_UNDEF
;
1668 i_shdrp
[0].sh_info
= 0;
1669 i_shdrp
[0].sh_addralign
= 0;
1670 i_shdrp
[0].sh_entsize
= 0;
1672 i_ehdrp
->e_shnum
= 1;
1674 elf_elfsections (abfd
) = i_shdrp
;
1675 elf_shstrtab (abfd
) = shstrtab
;
1677 est
.i_ehdr
= elf_elfheader(abfd
);
1678 est
.i_shdrp
= elf_elfsections(abfd
);
1679 est
.shstrtab
= elf_shstrtab(abfd
);
1680 est
.symtab_section
= 0; /* elf_fake_sections fils it in */
1682 bfd_map_over_sections(abfd
, elf_fake_sections
, &est
);
1683 elf_onesymtab (abfd
) = est
.symtab_section
;
1688 DEFUN (elf_write_object_contents
, (abfd
), bfd
*abfd
)
1690 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1691 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1692 Elf_External_Phdr
*x_phdrp
; /* Program header table, external form */
1693 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
1694 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
1695 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1702 struct strtab
*shstrtab
;
1704 if(abfd
->output_has_begun
== false)
1705 elf_compute_section_file_positions(abfd
);
1707 i_ehdrp
= elf_elfheader (abfd
);
1708 i_shdrp
= elf_elfsections (abfd
);
1709 shstrtab
= elf_shstrtab (abfd
);
1711 est
.i_ehdr
= i_ehdrp
;
1712 est
.i_shdrp
= i_shdrp
;
1713 est
.shstrtab
= shstrtab
;
1714 est
.symtab_section
= elf_onesymtab (abfd
); /* filled in by elf_fake */
1716 bfd_map_over_sections(abfd
, elf_make_sections
, &est
);
1718 /* dump out the one symtab */
1720 int symcount
= bfd_get_symcount (abfd
);
1721 asymbol
** syms
= bfd_get_outsymbols (abfd
);
1722 struct strtab
* stt
= bfd_new_strtab (abfd
);
1723 Elf_Internal_Shdr
*symtab_hdr
;
1724 Elf_Internal_Shdr
*symstrtab_hdr
;
1725 int symstrtab_section
;
1726 Elf_External_Sym
*outbound_syms
;
1729 symtab_hdr
= &i_shdrp
[est
.symtab_section
];
1730 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1731 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1732 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* symcount
;
1734 /* see assert in elf_fake_sections that supports this: */
1735 symstrtab_section
= est
.symtab_section
+1;
1736 symstrtab_hdr
= &i_shdrp
[symstrtab_section
];
1737 symtab_hdr
->sh_link
= symstrtab_section
;
1738 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1740 fprintf(stderr
,"ELF>> sending out %d syms\n",symcount
);
1741 outbound_syms
= (Elf_External_Sym
*)
1742 bfd_alloc(abfd
, (1+symcount
) * sizeof(Elf_External_Sym
));
1743 /* now generate the data (for "contents") */
1744 for (idx
= 0; idx
< symcount
; idx
++)
1746 Elf_Internal_Sym sym
;
1747 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
1748 sym
.st_value
= syms
[idx
]->value
;
1749 sym
.st_size
= 0; /* we should recover this (FIXME) */
1750 if (syms
[idx
]->flags
& BSF_WEAK
)
1751 sym
.st_info
= ELF_ST_INFO(STB_WEAK
, STT_OBJECT
);
1752 else if (syms
[idx
]->flags
& BSF_LOCAL
)
1753 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_OBJECT
);
1754 else if (syms
[idx
]->flags
& BSF_GLOBAL
)
1755 sym
.st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_OBJECT
);
1758 if (syms
[idx
]->section
)
1760 elf_section_from_bfd_section(abfd
,
1761 syms
[idx
]->section
->output_section
);
1763 sym
.st_shndx
= SHN_UNDEF
;
1765 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
+idx
+1);
1768 /* fill in 0th symbol */
1769 Elf_Internal_Sym sym
;
1775 sym
.st_shndx
= SHN_UNDEF
;
1776 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
1778 symtab_hdr
->contents
= (void*)outbound_syms
;
1779 symstrtab_hdr
->contents
= (void*)stt
->tab
;
1780 symstrtab_hdr
->sh_size
= stt
->length
;
1783 /* put the strtab out too... */
1785 Elf_Internal_Shdr
*this_hdr
;
1788 this_section
= i_ehdrp
->e_shnum
++;
1789 i_ehdrp
->e_shstrndx
= this_section
;
1790 this_hdr
= &i_shdrp
[this_section
];
1791 this_hdr
->sh_name
= bfd_add_to_strtab (abfd
, shstrtab
, ".shstrtab");
1792 this_hdr
->sh_size
= shstrtab
->length
;
1793 this_hdr
->contents
= (void*)shstrtab
->tab
;
1796 outbase
= i_ehdrp
->e_ehsize
;
1798 /* swap the header before spitting it out... */
1799 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
1800 bfd_seek (abfd
, 0L, SEEK_SET
);
1801 bfd_write ((PTR
) &x_ehdr
, sizeof(x_ehdr
), 1, abfd
);
1803 outbase
+= i_ehdrp
->e_shentsize
* i_ehdrp
->e_shnum
;
1805 /* now we fix up the offsets... */
1806 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
1808 i_shdrp
[count
].sh_offset
= outbase
;
1809 outbase
+= i_shdrp
[count
].sh_size
;
1812 /* at this point we've concocted all the ELF sections... */
1813 x_shdrp
= (Elf_External_Shdr
*)
1814 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
1817 bfd_error
= no_memory
;
1821 fprintf(stderr
, "ELF>> total sections: %d\n", i_ehdrp
->e_shnum
);
1822 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
1824 elf_swap_shdr_out (abfd
, i_shdrp
+count
, x_shdrp
+count
);
1826 bfd_write ((PTR
) x_shdrp
, sizeof(*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
1827 /* need to dump the string table too... */
1829 /* after writing the headers, we need to write the sections too... */
1830 nsect
= abfd
->sections
;
1831 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
1833 if(i_shdrp
[count
].contents
)
1835 fprintf(stderr
, "found some userdata: count %d, pos 0x%x\n",
1836 count
, i_shdrp
[count
].sh_offset
);
1837 bfd_seek (abfd
, i_shdrp
[count
].sh_offset
, SEEK_SET
);
1838 bfd_write (i_shdrp
[count
].contents
, i_shdrp
[count
].sh_size
, 1, abfd
);
1842 /* sample use of bfd:
1843 * bfd_seek (abfd, 0L, false);
1844 * bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
1845 * if (bfd_seek(abfd, scn_base, SEEK_SET) != 0)
1847 * old = bfd_tell(abfd);
1854 /* Given an index of a section, retrieve a pointer to it. Note
1855 that for our purposes, sections are indexed by {1, 2, ...} with
1856 0 being an illegal index. */
1858 /* In the original, each ELF section went into exactly one BFD
1859 section. This doesn't really make sense, so we need a real mapping.
1860 The mapping has to hide in the Elf_Internal_Shdr since asection
1861 doesn't have anything like a tdata field... */
1864 DEFUN (section_from_elf_index
, (abfd
, index
),
1868 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
1869 Elf_Internal_Shdr
*hdr
= i_shdrp
+ index
;
1871 switch (hdr
->sh_type
)
1873 /* ELF sections that map to BFD sections */
1877 bfd_section_from_shdr (abfd
, index
);
1878 return (struct sec
*)hdr
->rawdata
;
1885 /* given a section, search the header to find them... */
1887 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
1891 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
1893 Elf_Internal_Shdr
*hdr
;
1894 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
1896 for(index
= 0; index
< maxindex
; index
++) {
1897 hdr
= &i_shdrp
[index
];
1898 switch (hdr
->sh_type
)
1900 /* ELF sections that map to BFD sections */
1905 if (((struct sec
*)(hdr
->rawdata
)) == asect
)
1917 DEFUN (elf_slurp_symbol_table
, (abfd
, hdr
),
1919 Elf_Internal_Shdr
*hdr
)
1921 int symcount
; /* Number of external ELF symbols */
1922 char *strtab
; /* Buffer for raw ELF string table section */
1923 asymbol
*sym
; /* Pointer to current bfd symbol */
1924 asymbol
*symbase
; /* Buffer for generated bfd symbols */
1925 asymbol
**vec
; /* Pointer to current bfd symbol pointer */
1926 Elf_Internal_Sym i_sym
;
1927 Elf_External_Sym x_sym
;
1928 Elf_External_Sym
*x_symp
;
1929 unsigned int *table_ptr
; /* bfd symbol translation table */
1931 /* this is only valid because there is only one symtab... */
1932 /* FIXME: This is incorrect, there may also be a dynamic symbol
1933 table which is a subset of the full symbol table. We either need
1934 to be prepared to read both (and merge them) or ensure that we
1935 only read the full symbol table. Currently we only get called to
1936 read the full symbol table. -fnf */
1937 if (bfd_get_outsymbols (abfd
) != NULL
)
1942 /* Read each raw ELF symbol, converting from external ELF form to
1943 internal ELF form, and then using the information to create a
1944 canonical bfd symbol table entry.
1946 Note that we allocate the initial bfd canonical symbol buffer
1947 based on a one-to-one mapping of the ELF symbols to canonical
1948 symbols. However, it is likely that not all the ELF symbols will
1949 be used, so there will be some space leftover at the end. Once
1950 we know how many symbols we actual generate, we realloc the buffer
1951 to the correct size and then build the pointer vector. */
1953 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
1955 bfd_error
= system_call_error
;
1959 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1960 sym
= symbase
= (asymbol
*) bfd_zalloc (abfd
, symcount
* sizeof (asymbol
));
1961 x_symp
= (Elf_External_Sym
*)
1962 bfd_zalloc (abfd
, symcount
* sizeof (Elf_External_Sym
));
1964 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
1965 != symcount
* sizeof (Elf_External_Sym
))
1967 bfd_error
= system_call_error
;
1970 while (symcount
-- > 0)
1972 elf_swap_symbol_in (abfd
, x_symp
+ symcount
, &i_sym
);
1973 if (i_sym
.st_name
> 0)
1975 sym
-> the_bfd
= abfd
;
1976 sym
-> name
= elf_string_from_elf_section(abfd
, hdr
->sh_link
,
1978 sym
-> value
= i_sym
.st_value
;
1979 /* FIXME -- this is almost certainly bogus. It's from Pace Willisson's
1980 hasty Solaris support, to pass the sizes of object files or functions
1981 down into GDB via the back door, to circumvent some other kludge in
1982 how Sun hacked stabs. */
1983 sym
-> udata
= (PTR
)i_sym
.st_size
;
1984 /* FIXME -- end of bogosity. */
1985 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
1987 sym
-> section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
1989 else if (i_sym
.st_shndx
== SHN_ABS
)
1991 sym
-> section
= &bfd_abs_section
;
1993 else if (i_sym
.st_shndx
== SHN_COMMON
)
1995 sym
-> section
= &bfd_com_section
;
1997 else if (i_sym
.st_shndx
== SHN_UNDEF
)
1999 sym
-> section
= &bfd_und_section
;
2002 switch (ELF_ST_BIND (i_sym
.st_info
))
2005 sym
-> flags
|= BSF_LOCAL
;
2008 sym
-> flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
2011 sym
-> flags
|= BSF_WEAK
;
2018 /* let's try *not* punting unnamed symbols... */
2019 sym
-> the_bfd
= abfd
;
2020 sym
-> name
= "unnamed"; /* perhaps should include the number? */
2021 sym
-> value
= i_sym
.st_value
;
2022 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2024 sym
-> section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
2026 else if (i_sym
.st_shndx
== SHN_ABS
)
2028 sym
-> section
= &bfd_abs_section
;
2030 else if (i_sym
.st_shndx
== SHN_COMMON
)
2032 sym
-> section
= &bfd_com_section
;
2034 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2036 sym
-> section
= &bfd_und_section
;
2039 switch (ELF_ST_BIND (i_sym
.st_info
))
2042 sym
-> flags
|= BSF_LOCAL
;
2045 sym
-> flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
2048 sym
-> flags
|= BSF_WEAK
;
2056 bfd_get_symcount(abfd
) = symcount
= sym
- symbase
;
2057 sym
= symbase
= (asymbol
*)
2058 bfd_realloc (abfd
, symbase
, symcount
* sizeof (asymbol
));
2059 bfd_get_outsymbols(abfd
) = vec
= (asymbol
**)
2060 bfd_alloc (abfd
, symcount
* sizeof (asymbol
*));
2062 while (symcount
-- > 0)
2070 /* Return the number of bytes required to hold the symtab vector.
2072 Note that we base it on the count plus 1, since we will null terminate
2073 the vector allocated based on this size. */
2076 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
*abfd
)
2078 unsigned int symtab_size
= 0;
2080 /* if (elf_slurp_symbol_table (abfd, FIXME...)) */
2082 symtab_size
= (bfd_get_symcount (abfd
) + 1) * (sizeof (asymbol
));
2084 return (symtab_size
);
2088 This function return the number of bytes required to store the
2089 relocation information associated with section <<sect>>
2090 attached to bfd <<abfd>>
2094 elf_get_reloc_upper_bound (abfd
, asect
)
2098 if (asect
->flags
& SEC_RELOC
)
2100 /* either rel or rela */
2101 return asect
->_raw_size
;
2107 /* FIXME!!! sparc howto should go into elf-32-sparc.c */
2112 R_SPARC_8
, R_SPARC_16
, R_SPARC_32
,
2113 R_SPARC_DISP8
, R_SPARC_DISP16
, R_SPARC_DISP32
,
2114 R_SPARC_WDISP30
, R_SPARC_WDISP22
,
2115 R_SPARC_HI22
, R_SPARC_22
,
2116 R_SPARC_13
, R_SPARC_LO10
,
2117 R_SPARC_GOT10
, R_SPARC_GOT13
, R_SPARC_GOT22
,
2118 R_SPARC_PC10
, R_SPARC_PC22
,
2121 R_SPARC_GLOB_DAT
, R_SPARC_JMP_SLOT
,
2126 #define RELOC_TYPE_NAMES \
2128 "R_SPARC_8", "R_SPARC_16", "R_SPARC_32", \
2129 "R_SPARC_DISP8", "R_SPARC_DISP16", "R_SPARC_DISP32", \
2130 "R_SPARC_WDISP30", "R_SPARC_WDISP22", \
2131 "R_SPARC_HI22", "R_SPARC_22", \
2132 "R_SPARC_13", "R_SPARC_LO10", \
2133 "R_SPARC_GOT10", "R_SPARC_GOT13", "R_SPARC_GOT22", \
2134 "R_SPARC_PC10", "R_SPARC_PC22", \
2137 "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT", \
2138 "R_SPARC_RELATIVE", \
2141 static reloc_howto_type elf_howto_table
[] =
2143 HOWTO(R_SPARC_NONE
, 0,0, 0,false,0,false,false, 0,"R_SPARC_NONE", false,0,0x00000000,false),
2144 HOWTO(R_SPARC_8
, 0,0, 8,false,0,true, true, 0,"R_SPARC_8", false,0,0x000000ff,false),
2145 HOWTO(R_SPARC_16
, 0,1,16,false,0,true, true, 0,"R_SPARC_16", false,0,0x0000ffff,false),
2146 HOWTO(R_SPARC_32
, 0,2,32,false,0,true, true, 0,"R_SPARC_32", false,0,0xffffffff,false),
2147 HOWTO(R_SPARC_DISP8
, 0,0, 8,true, 0,false, true, 0,"R_SPARC_DISP8", false,0,0x000000ff,false),
2148 HOWTO(R_SPARC_DISP16
, 0,1,16,true, 0,false, true, 0,"R_SPARC_DISP16", false,0,0x0000ffff,false),
2149 HOWTO(R_SPARC_DISP32
, 0,2,32,true, 0,false, true, 0,"R_SPARC_DISP32", false,0,0x00ffffff,false),
2150 HOWTO(R_SPARC_WDISP30
,2,2,30,true, 0,false, true, 0,"R_SPARC_WDISP30",false,0,0x3fffffff,false),
2151 HOWTO(R_SPARC_WDISP22
,2,2,22,true, 0,false, true, 0,"R_SPARC_WDISP22",false,0,0x003fffff,false),
2152 HOWTO(R_SPARC_HI22
, 10,2,22,false,0,true, false, 0,"R_SPARC_HI22", false,0,0x003fffff,false),
2153 HOWTO(R_SPARC_22
, 0,2,22,false,0,true, true, 0,"R_SPARC_22", false,0,0x003fffff,false),
2154 HOWTO(R_SPARC_13
, 0,1,13,false,0,true, true, 0,"R_SPARC_13", false,0,0x00001fff,false),
2155 HOWTO(R_SPARC_LO10
, 0,1,10,false,0,true, false, 0,"R_SPARC_LO10", false,0,0x000003ff,false),
2156 HOWTO(R_SPARC_GOT10
, 0,1,10,false,0,false, true, 0,"R_SPARC_GOT10", false,0,0x000003ff,false),
2157 HOWTO(R_SPARC_GOT13
, 0,1,13,false,0,false, true, 0,"R_SPARC_GOT13", false,0,0x00001fff,false),
2158 HOWTO(R_SPARC_GOT22
, 10,2,22,false,0,false, true, 0,"R_SPARC_GOT22", false,0,0x003fffff,false),
2159 HOWTO(R_SPARC_PC10
, 0,1,10,false,0,true, true, 0,"R_SPARC_PC10", false,0,0x000003ff,false),
2160 HOWTO(R_SPARC_PC22
, 0,2,22,false,0,true, true, 0,"R_SPARC_PC22", false,0,0x003fffff,false),
2161 HOWTO(R_SPARC_WPLT30
, 0,0,00,false,0,false,false, 0,"R_SPARC_WPLT30", false,0,0x00000000,false),
2162 HOWTO(R_SPARC_COPY
, 0,0,00,false,0,false,false, 0,"R_SPARC_COPY", false,0,0x00000000,false),
2163 HOWTO(R_SPARC_GLOB_DAT
,0,0,00,false,0,false,false,0,"R_SPARC_GLOB_DAT",false,0,0x00000000,false),
2164 HOWTO(R_SPARC_JMP_SLOT
,0,0,00,false,0,false,false,0,"R_SPARC_JMP_SLOT",false,0,0x00000000,false),
2165 HOWTO(R_SPARC_RELATIVE
,0,0,00,false,0,false,false,0,"R_SPARC_RELATIVE",false,0,0x00000000,false),
2166 HOWTO(R_SPARC_UA32
, 0,0,00,false,0,false,false,0,"R_SPARC_UA32", false,0,0x00000000,false),
2171 DEFUN(elf_info_to_howto
, (abfd
, cache_ptr
, dst
),
2173 arelent
*cache_ptr AND
2174 Elf_Internal_Rela
*dst
)
2176 /* FIXME!!! just doing sparc for now... */
2178 BFD_ASSERT (ELF_R_TYPE(dst
->r_info
) < 24);
2180 cache_ptr
->howto
= &elf_howto_table
[ELF_R_TYPE(dst
->r_info
)];
2182 fprintf (stderr
, "elf_info_to_howto not implemented\n");
2188 DEFUN(elf_slurp_reloca_table
,(abfd
, asect
, symbols
),
2193 Elf_External_Rela
*native_relocs
;
2194 arelent
*reloc_cache
;
2199 if (asect
->relocation
)
2201 if (asect
->reloc_count
== 0)
2203 if (asect
->flags
& SEC_CONSTRUCTOR
)
2205 /* if (!elf_slurp_symbol_table(abfd))
2206 return false; -- should be done by now */
2208 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2209 native_relocs
= (Elf_External_Rela
*)
2210 bfd_alloc(abfd
, asect
->reloc_count
* sizeof(Elf_External_Rela
));
2211 fprintf(stderr
, "ELF>> really reading %d relocs for section %s\n",
2212 asect
->reloc_count
, asect
->name
);
2213 bfd_read ((PTR
) native_relocs
,
2214 sizeof(Elf_External_Rela
), asect
->reloc_count
, abfd
);
2216 reloc_cache
= (arelent
*)
2217 bfd_alloc(abfd
, (size_t) (asect
->reloc_count
* sizeof(arelent
)));
2219 if (! reloc_cache
) {
2220 bfd_error
= no_memory
;
2224 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2226 #ifdef RELOC_PROCESSING
2227 /* sparc, 68k, 88k, 860 use rela only. */
2228 /* 386 and we32000 use rel only... fix it for them later. */
2229 Elf_Internal_Rela dst
;
2230 Elf_External_Rela
*src
;
2232 cache_ptr
= reloc_cache
+ idx
;
2233 src
= native_relocs
+ idx
;
2234 elf_swap_reloca_in(abfd
, src
, &dst
);
2236 RELOC_PROCESSING(cache_ptr
, &dst
, symbols
, abfd
, asect
);
2238 Elf_Internal_Rela dst
;
2240 Elf_External_Rela
*src
;
2242 cache_ptr
= reloc_cache
+ idx
;
2243 src
= native_relocs
+ idx
;
2245 elf_swap_reloca_in(abfd
, src
, &dst
);
2247 if(asect
->flags
& SEC_RELOC
)
2249 /* relocatable, so the offset is off of the section */
2250 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2254 /* non-relocatable, so the offset a virtual address */
2255 cache_ptr
->address
= dst
.r_offset
;
2257 /* ELF_R_SYM(dst.r_info) is the symbol table offset... */
2258 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM(dst
.r_info
);
2259 cache_ptr
->addend
= dst
.r_addend
;
2260 /* ptr = *(cache_ptr->sym_ptr_ptr); */
2262 /* Fill in the cache_ptr->howto field from dst.r_type */
2263 elf_info_to_howto(abfd
, cache_ptr
, &dst
);
2267 asect
->relocation
= reloc_cache
;
2273 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2279 arelent
*tblptr
= section
->relocation
;
2280 unsigned int count
= 0;
2282 /* snarfed from coffcode.h */
2283 /* FIXME: this could be reloc... */
2284 elf_slurp_reloca_table(abfd
, section
, symbols
);
2286 tblptr
= section
->relocation
;
2290 for (; count
++ < section
->reloc_count
;)
2291 *relptr
++ = tblptr
++;
2294 return section
->reloc_count
;
2298 DEFUN (elf_get_symtab
, (abfd
, alocation
),
2300 asymbol
**alocation
)
2302 unsigned int symcount
;
2305 /* if (!elf_slurp_symbol_table (abfd))
2309 symcount
= bfd_get_symcount (abfd
);
2310 vec
= bfd_get_outsymbols (abfd
);
2311 while (symcount
-- > 0)
2313 *alocation
++ = *vec
++;
2315 *alocation
++ = NULL
;
2316 return (bfd_get_symcount (abfd
));
2321 DEFUN (elf_make_empty_symbol
, (abfd
),
2324 elf_symbol_type
*newsym
;
2326 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2329 bfd_error
= no_memory
;
2334 newsym
-> symbol
.the_bfd
= abfd
;
2335 return (&newsym
-> symbol
);
2340 DEFUN (elf_print_symbol
,(ignore_abfd
, filep
, symbol
, how
),
2341 bfd
*ignore_abfd AND
2344 bfd_print_symbol_type how
)
2346 FILE *file
= (FILE *)filep
;
2349 case bfd_print_symbol_name
:
2350 fprintf(file
, "%s", symbol
->name
);
2352 case bfd_print_symbol_more
:
2353 fprintf(file
, "elf %lx %lx",
2357 case bfd_print_symbol_nm
:
2358 case bfd_print_symbol_all
:
2360 CONST
char *section_name
;
2361 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2362 bfd_print_symbol_vandf((PTR
) file
, symbol
);
2363 fprintf(file
, " %-5s %s %s %s",
2374 DEFUN (elf_get_lineno
,(ignore_abfd
, symbol
),
2375 bfd
*ignore_abfd AND
2378 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2385 DEFUN (elf_set_arch_mach
,(abfd
, arch
, machine
),
2387 enum bfd_architecture arch AND
2388 unsigned long machine
)
2390 /* Allow any architecture to be supported by the elf backend */
2393 case bfd_arch_unknown
: /* EM_NONE */
2394 case bfd_arch_sparc
: /* EM_SPARC */
2395 case bfd_arch_i386
: /* EM_386 */
2396 case bfd_arch_m68k
: /* EM_68K */
2397 case bfd_arch_m88k
: /* EM_88K */
2398 case bfd_arch_i860
: /* EM_860 */
2399 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
2400 return bfd_default_set_arch_mach(abfd
, arch
, machine
);
2407 DEFUN (elf_find_nearest_line
,(abfd
,
2415 asection
*section AND
2416 asymbol
**symbols AND
2418 CONST
char **filename_ptr AND
2419 CONST
char **functionname_ptr AND
2420 unsigned int *line_ptr
)
2422 fprintf (stderr
, "elf_find_nearest_line unimplemented\n");
2429 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
2433 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
2440 DEFUN(elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
2445 bfd_size_type count
)
2449 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
2451 /* do setup calculations (FIXME) */
2452 elf_compute_section_file_positions(abfd
);
2455 if(bfd_seek (abfd
, (file_ptr
)section
->filepos
+ offset
, SEEK_SET
) == -1)
2457 if(bfd_write (location
, (bfd_size_type
)1, count
, abfd
) != count
)
2460 /* we really just need to save the contents away... */
2461 dest_sect
= elf_section_from_bfd_section(abfd
, section
);
2465 /* FIXME: allocate in set_section_size, then copy in here... */
2466 contents
= (void*)bfd_alloc(abfd
, count
);
2467 BFD_ASSERT(contents
);
2468 memcpy(contents
, location
, count
);
2469 elf_elfsections (abfd
)[dest_sect
].contents
= contents
;
2475 /* This structure contains everything that BFD knows about a target.
2476 It includes things like its byte order, name, what routines to call
2477 to do various operations, etc. Every BFD points to a target structure
2478 with its "xvec" member.
2480 There are two such structures here: one for big-endian machines and
2481 one for little-endian machines. */
2483 /* Archives are generic or unimplemented. */
2484 #define elf_slurp_armap bfd_false
2485 #define elf_slurp_extended_name_table _bfd_slurp_extended_name_table
2486 #define elf_truncate_arname bfd_dont_truncate_arname
2487 #define elf_openr_next_archived_file bfd_generic_openr_next_archived_file
2488 #define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt
2489 #define elf_write_armap (PROTO (boolean, (*), \
2490 (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count, \
2491 int stridx))) bfd_false
2493 /* Ordinary section reading and writing */
2494 #define elf_new_section_hook _bfd_dummy_new_section_hook
2495 #define elf_get_section_contents bfd_generic_get_section_contents
2496 /* #define elf_set_section_contents bfd_generic_set_section_contents */
2497 #define elf_close_and_cleanup bfd_generic_close_and_cleanup
2499 #define elf_bfd_debug_info_start bfd_void
2500 #define elf_bfd_debug_info_end bfd_void
2501 #define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
2502 #define elf_bfd_get_relocated_section_contents \
2503 bfd_generic_get_relocated_section_contents
2504 #define elf_bfd_relax_section bfd_generic_relax_section
2505 bfd_target elf_big_vec
=
2507 /* name: identify kind of target */
2510 /* flavour: general indication about file */
2511 bfd_target_elf_flavour
,
2513 /* byteorder_big_p: data is big endian */
2516 /* header_byteorder_big_p: header is also big endian */
2519 /* object_flags: mask of all file flags */
2520 (HAS_RELOC
| EXEC_P
| HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
|
2523 /* section_flags: mask of all section flags */
2524 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_READONLY
|
2525 SEC_CODE
| SEC_DATA
),
2527 /* ar_pad_char: pad character for filenames within an archive header
2528 FIXME: this really has nothing to do with ELF, this is a characteristic
2529 of the archiver and/or os and should be independently tunable */
2532 /* ar_max_namelen: maximum number of characters in an archive header
2533 FIXME: this really has nothing to do with ELF, this is a characteristic
2534 of the archiver and should be independently tunable. This value is
2535 a WAG (wild a** guess) */
2538 /* align_power_min: minimum alignment restriction for any section
2539 FIXME: this value may be target machine dependent */
2542 /* Routines to byte-swap various sized integers from the data sections */
2543 _do_getb64
, _do_putb64
, _do_getb32
, _do_putb32
, _do_getb16
, _do_putb16
,
2545 /* Routines to byte-swap various sized integers from the file headers */
2546 _do_getb64
, _do_putb64
, _do_getb32
, _do_putb32
, _do_getb16
, _do_putb16
,
2548 /* bfd_check_format: check the format of a file being read */
2549 { _bfd_dummy_target
, /* unknown format */
2550 elf_object_p
, /* assembler/linker output (object file) */
2551 bfd_generic_archive_p
, /* an archive */
2552 elf_core_file_p
/* a core file */
2555 /* bfd_set_format: set the format of a file being written */
2558 _bfd_generic_mkarchive
,
2562 /* bfd_write_contents: write cached information into a file being written */
2564 elf_write_object_contents
,
2565 _bfd_write_archive_contents
,
2569 /* Initialize a jump table with the standard macro. All names start
2577 bfd_target elf_little_vec
=
2579 /* name: identify kind of target */
2582 /* flavour: general indication about file */
2583 bfd_target_elf_flavour
,
2585 /* byteorder_big_p: data is big endian */
2586 false, /* Nope -- this one's little endian */
2588 /* header_byteorder_big_p: header is also big endian */
2589 false, /* Nope -- this one's little endian */
2591 /* object_flags: mask of all file flags */
2592 (HAS_RELOC
| EXEC_P
| HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
|
2595 /* section_flags: mask of all section flags */
2596 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
| SEC_READONLY
|
2599 /* ar_pad_char: pad character for filenames within an archive header
2600 FIXME: this really has nothing to do with ELF, this is a characteristic
2601 of the archiver and/or os and should be independently tunable */
2604 /* ar_max_namelen: maximum number of characters in an archive header
2605 FIXME: this really has nothing to do with ELF, this is a characteristic
2606 of the archiver and should be independently tunable. This value is
2607 a WAG (wild a** guess) */
2610 /* align_power_min: minimum alignment restriction for any section
2611 FIXME: this value may be target machine dependent */
2614 /* Routines to byte-swap various sized integers from the data sections */
2615 _do_getl64
, _do_putl64
, _do_getl32
, _do_putl32
, _do_getl16
, _do_putl16
,
2617 /* Routines to byte-swap various sized integers from the file headers */
2618 _do_getl64
, _do_putl64
, _do_getl32
, _do_putl32
, _do_getl16
, _do_putl16
,
2620 /* bfd_check_format: check the format of a file being read */
2621 { _bfd_dummy_target
, /* unknown format */
2622 elf_object_p
, /* assembler/linker output (object file) */
2623 bfd_generic_archive_p
, /* an archive */
2624 elf_core_file_p
/* a core file */
2627 /* bfd_set_format: set the format of a file being written */
2630 _bfd_generic_mkarchive
,
2634 /* bfd_write_contents: write cached information into a file being written */
2636 elf_write_object_contents
,
2637 _bfd_write_archive_contents
,
2641 /* Initialize a jump table with the standard macro. All names start