1 /* ELF executable support for BFD.
2 Copyright 1991, 1992, 1993 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)
77 #include <string.h> /* For strrchr and friends */
83 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
84 #include <sys/procfs.h>
86 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
87 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
88 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
91 /* Forward declarations of static functions */
94 elf_read
PARAMS ((bfd
*, long, int));
97 section_from_elf_index
PARAMS ((bfd
*, int));
100 elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
103 elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
106 elf_get_str_section
PARAMS ((bfd
*, unsigned int));
108 /* Some private data is stashed away for future use using the tdata pointer
109 in the bfd structure. */
113 Elf_Internal_Ehdr elf_header
[1]; /* Actual data, but ref like ptr */
114 Elf_Internal_Shdr
*elf_sect_ptr
;
115 struct strtab
*strtab_ptr
;
117 void *prstatus
; /* The raw /proc prstatus structure */
118 void *prpsinfo
; /* The raw /proc prpsinfo structure */
119 Elf_External_Sym
*raw_syms
;
120 Elf_Internal_Sym
*internal_syms
;
121 elf_symbol_type
*symbols
;
124 #define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data)
125 #define elf_elfheader(bfd) (elf_tdata(bfd) -> elf_header)
126 #define elf_elfsections(bfd) (elf_tdata(bfd) -> elf_sect_ptr)
127 #define elf_shstrtab(bfd) (elf_tdata(bfd) -> strtab_ptr)
128 #define elf_onesymtab(bfd) (elf_tdata(bfd) -> symtab_section)
129 #define core_prpsinfo(bfd) (elf_tdata(bfd) -> prpsinfo)
130 #define core_prstatus(bfd) (elf_tdata(bfd) -> prstatus)
131 #define obj_symbols(bfd) (elf_tdata(bfd) -> symbols)
132 #define obj_raw_syms(bfd) (elf_tdata(bfd) -> raw_syms)
133 #define obj_internal_syms(bfd) (elf_tdata(bfd) -> internal_syms)
135 /* Translate an ELF symbol in external format into an ELF symbol in internal
139 DEFUN(elf_swap_symbol_in
,(abfd
, src
, dst
),
141 Elf_External_Sym
*src AND
142 Elf_Internal_Sym
*dst
)
144 dst
-> st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_name
);
145 dst
-> st_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_value
);
146 dst
-> st_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_size
);
147 dst
-> st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
-> st_info
);
148 dst
-> st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
-> st_other
);
149 dst
-> st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> st_shndx
);
152 /* Translate an ELF symbol in internal format into an ELF symbol in external
156 DEFUN(elf_swap_symbol_out
,(abfd
, src
, dst
),
158 Elf_Internal_Sym
*src AND
159 Elf_External_Sym
*dst
)
161 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
162 bfd_h_put_32 (abfd
, src
->st_value
, dst
->st_value
);
163 bfd_h_put_32 (abfd
, src
->st_size
, dst
->st_size
);
164 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
165 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
166 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
170 /* Translate an ELF file header in external format into an ELF file header in
174 DEFUN(elf_swap_ehdr_in
,(abfd
, src
, dst
),
176 Elf_External_Ehdr
*src AND
177 Elf_Internal_Ehdr
*dst
)
179 memcpy (dst
-> e_ident
, src
-> e_ident
, EI_NIDENT
);
180 dst
-> e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_type
);
181 dst
-> e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_machine
);
182 dst
-> e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_version
);
183 dst
-> e_entry
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_entry
);
184 dst
-> e_phoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_phoff
);
185 dst
-> e_shoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_shoff
);
186 dst
-> e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_flags
);
187 dst
-> e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_ehsize
);
188 dst
-> e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phentsize
);
189 dst
-> e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phnum
);
190 dst
-> e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shentsize
);
191 dst
-> e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shnum
);
192 dst
-> e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shstrndx
);
195 /* Translate an ELF file header in internal format into an ELF file header in
199 DEFUN(elf_swap_ehdr_out
,(abfd
, src
, dst
),
201 Elf_Internal_Ehdr
*src AND
202 Elf_External_Ehdr
*dst
)
204 memcpy (dst
-> e_ident
, src
-> e_ident
, EI_NIDENT
);
205 /* note that all elements of dst are *arrays of unsigned char* already... */
206 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
207 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
208 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
209 bfd_h_put_32 (abfd
, src
->e_entry
, dst
->e_entry
);
210 bfd_h_put_32 (abfd
, src
->e_phoff
, dst
->e_phoff
);
211 bfd_h_put_32 (abfd
, src
->e_shoff
, dst
->e_shoff
);
212 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
213 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
214 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
215 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
216 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
217 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
218 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
222 /* Translate an ELF section header table entry in external format into an
223 ELF section header table entry in internal format. */
226 DEFUN(elf_swap_shdr_in
,(abfd
, src
, dst
),
228 Elf_External_Shdr
*src AND
229 Elf_Internal_Shdr
*dst
)
231 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
232 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
233 dst
->sh_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_flags
);
234 dst
->sh_addr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_addr
);
235 dst
->sh_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_offset
);
236 dst
->sh_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_size
);
237 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
238 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
239 dst
->sh_addralign
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_addralign
);
240 dst
->sh_entsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_entsize
);
241 /* we haven't done any processing on it yet, so... */
242 dst
->rawdata
= (void*)0;
245 /* Translate an ELF section header table entry in internal format into an
246 ELF section header table entry in external format. */
249 DEFUN(elf_swap_shdr_out
,(abfd
, src
, dst
),
251 Elf_Internal_Shdr
*src AND
252 Elf_External_Shdr
*dst
)
254 /* note that all elements of dst are *arrays of unsigned char* already... */
255 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
256 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
257 bfd_h_put_32 (abfd
, src
->sh_flags
, dst
->sh_flags
);
258 bfd_h_put_32 (abfd
, src
->sh_addr
, dst
->sh_addr
);
259 bfd_h_put_32 (abfd
, src
->sh_offset
, dst
->sh_offset
);
260 bfd_h_put_32 (abfd
, src
->sh_size
, dst
->sh_size
);
261 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
262 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
263 bfd_h_put_32 (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
264 bfd_h_put_32 (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
268 /* Translate an ELF program header table entry in external format into an
269 ELF program header table entry in internal format. */
272 DEFUN(elf_swap_phdr_in
,(abfd
, src
, dst
),
274 Elf_External_Phdr
*src AND
275 Elf_Internal_Phdr
*dst
)
277 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
278 dst
->p_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_offset
);
279 dst
->p_vaddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_vaddr
);
280 dst
->p_paddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_paddr
);
281 dst
->p_filesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_filesz
);
282 dst
->p_memsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_memsz
);
283 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
284 dst
->p_align
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_align
);
290 DEFUN(elf_swap_phdr_out
,(abfd
, src
, dst
),
292 Elf_Internal_Phdr
*src AND
293 Elf_External_Phdr
*dst
)
295 /* note that all elements of dst are *arrays of unsigned char* already... */
296 bfd_h_put_32 (abfd
, src
->p_type
, dst
->p_type
);
297 bfd_h_put_32 (abfd
, src
->p_offset
, dst
->p_offset
);
298 bfd_h_put_32 (abfd
, src
->p_vaddr
, dst
->p_vaddr
);
299 bfd_h_put_32 (abfd
, src
->p_paddr
, dst
->p_paddr
);
300 bfd_h_put_32 (abfd
, src
->p_filesz
, dst
->p_filesz
);
301 bfd_h_put_32 (abfd
, src
->p_memsz
, dst
->p_memsz
);
302 bfd_h_put_32 (abfd
, src
->p_flags
, dst
->p_flags
);
303 bfd_h_put_32 (abfd
, src
->p_align
, dst
->p_align
);
306 /* Translate an ELF reloc from external format to internal format. */
308 DEFUN(elf_swap_reloc_in
,(abfd
, src
, dst
),
310 Elf_External_Rel
*src AND
311 Elf_Internal_Rel
*dst
)
313 dst
->r_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_offset
);
314 dst
->r_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_info
);
318 DEFUN(elf_swap_reloca_in
,(abfd
, src
, dst
),
320 Elf_External_Rela
*src AND
321 Elf_Internal_Rela
*dst
)
323 dst
->r_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_offset
);
324 dst
->r_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_info
);
325 dst
->r_addend
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_addend
);
328 /* Translate an ELF reloc from internal format to external format. */
330 DEFUN(elf_swap_reloc_out
,(abfd
, src
, dst
),
332 Elf_Internal_Rel
*src AND
333 Elf_External_Rel
*dst
)
335 bfd_h_put_32 (abfd
, src
->r_offset
, dst
->r_offset
);
336 bfd_h_put_32 (abfd
, src
->r_info
, dst
->r_info
);
340 DEFUN(elf_swap_reloca_out
,(abfd
, src
, dst
),
342 Elf_Internal_Rela
*src AND
343 Elf_External_Rela
*dst
)
345 bfd_h_put_32 (abfd
, src
->r_offset
, dst
->r_offset
);
346 bfd_h_put_32 (abfd
, src
->r_info
, dst
->r_info
);
347 bfd_h_put_32 (abfd
, src
->r_addend
, dst
->r_addend
);
355 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
358 Helper functions for GDB to locate the string tables.
359 Since BFD hides string tables from callers, GDB needs to use an
360 internal hook to find them. Sun's .stabstr, in particular,
361 isn't even pointed to by the .stab section, so ordinary
362 mechanisms wouldn't work to find it, even if we had some.
365 struct elf_internal_shdr
*
366 DEFUN(bfd_elf_find_section
, (abfd
, name
),
370 Elf_Internal_Shdr
*i_shdrp
;
371 Elf_Internal_Shdr
*gotit
= NULL
;
376 i_shdrp
= elf_elfsections (abfd
);
379 shstrtab
= elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
380 if (shstrtab
!= NULL
)
382 max
= elf_elfheader (abfd
)->e_shnum
;
383 for (i
= 1; i
< max
; i
++)
385 if (!strcmp (&shstrtab
[i_shdrp
[i
].sh_name
], name
))
395 /* End of GDB support. */
398 DEFUN(elf_get_str_section
, (abfd
, shindex
),
400 unsigned int shindex
)
402 Elf_Internal_Shdr
*i_shdrp
;
403 char *shstrtab
= NULL
;
405 unsigned int shstrtabsize
;
407 i_shdrp
= elf_elfsections (abfd
);
410 shstrtab
= i_shdrp
[shindex
].rawdata
;
411 if (shstrtab
== NULL
)
413 /* No cached one, attempt to read, and cache what we read. */
414 offset
= i_shdrp
[shindex
].sh_offset
;
415 shstrtabsize
= i_shdrp
[shindex
].sh_size
;
416 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
417 i_shdrp
[shindex
].rawdata
= (void*) shstrtab
;
424 DEFUN(elf_string_from_elf_section
, (abfd
, shindex
, strindex
),
426 unsigned int shindex AND
427 unsigned int strindex
)
429 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
430 Elf_Internal_Shdr
*hdr
= i_shdrp
+ shindex
;
434 if (elf_get_str_section (abfd
, shindex
) == NULL
)
439 return ((char*)hdr
->rawdata
)+strindex
;
442 #define elf_string_from_elf_strtab(abfd, strindex) \
443 elf_string_from_elf_section (abfd, elf_elfheader(abfd)->e_shstrndx, strindex)
445 /* Create a new bfd section from an ELF section header. */
448 DEFUN(bfd_section_from_shdr
, (abfd
, shindex
),
450 unsigned int shindex
)
452 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
453 Elf_Internal_Shdr
*hdr
= i_shdrp
+ shindex
;
457 name
= hdr
->sh_name
? elf_string_from_elf_strtab (abfd
, hdr
->sh_name
) : "";
459 switch(hdr
->sh_type
) {
462 /* inactive section. Throw it away. */
466 /* Bits that get saved. This one is real. */
469 newsect
= bfd_make_section (abfd
, name
);
472 newsect
->vma
= hdr
->sh_addr
;
473 newsect
->_raw_size
= hdr
->sh_size
;
474 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
475 newsect
->flags
|= SEC_HAS_CONTENTS
;
477 if (hdr
->sh_flags
& SHF_ALLOC
)
479 newsect
->flags
|= SEC_ALLOC
;
480 newsect
->flags
|= SEC_LOAD
;
483 if (!(hdr
->sh_flags
& SHF_WRITE
))
484 newsect
->flags
|= SEC_READONLY
;
486 if (hdr
->sh_flags
& SHF_EXECINSTR
)
487 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
489 newsect
->flags
|= SEC_DATA
;
491 hdr
->rawdata
= (void*)newsect
;
497 /* Bits that get saved. This one is real. */
500 newsect
= bfd_make_section (abfd
, name
);
503 newsect
->vma
= hdr
->sh_addr
;
504 newsect
->_raw_size
= hdr
->sh_size
;
505 newsect
->filepos
= hdr
->sh_offset
; /* fake */
506 if (hdr
->sh_flags
& SHF_ALLOC
)
507 newsect
->flags
|= SEC_ALLOC
;
509 if (!(hdr
->sh_flags
& SHF_WRITE
))
510 newsect
->flags
|= SEC_READONLY
;
512 if (hdr
->sh_flags
& SHF_EXECINSTR
)
513 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
515 newsect
->flags
|= SEC_DATA
;
517 hdr
->rawdata
= (void*)newsect
;
522 case SHT_SYMTAB
: /* A symbol table */
523 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
524 elf_onesymtab (abfd
) = shindex
;
525 abfd
->flags
|= HAS_SYMS
;
528 case SHT_STRTAB
: /* A string table */
533 /* *these* do a lot of work -- but build no sections! */
534 /* the spec says there can be multiple strtabs, but only one symtab */
535 /* but there can be lots of REL* sections. */
536 /* FIXME: The above statement is wrong! There are typically at least
537 two symbol tables in a dynamically linked executable, ".dynsym"
538 which is the dynamic linkage symbol table and ".symtab", which is
539 the "traditional" symbol table. -fnf */
542 asection
*target_sect
;
544 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
545 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
546 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
547 if (target_sect
== NULL
)
551 /* FIXME: We are only prepared to read one symbol table, so
552 do NOT read the dynamic symbol table since it is only a
553 subset of the full symbol table. Also see comment above. -fnf */
554 if (!elf_slurp_symbol_table(abfd
, i_shdrp
+ hdr
->sh_link
))
558 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
559 target_sect
->flags
|= SEC_RELOC
;
560 target_sect
->relocation
= 0;
561 target_sect
->rel_filepos
= hdr
->sh_offset
;
568 case SHT_DYNSYM
: /* could treat this like symtab... */
570 fprintf(stderr
, "Dynamic Linking sections not yet supported.\n");
577 fprintf(stderr
, "Note Sections not yet supported.\n");
584 fprintf(stderr
, "SHLIB Sections not supported (and non conforming.)\n");
605 static struct strtab
*
606 DEFUN(bfd_new_strtab
, (abfd
),
611 ss
= (struct strtab
*) bfd_xmalloc(sizeof(struct strtab
));
612 ss
->tab
= bfd_xmalloc(1);
613 BFD_ASSERT(ss
->tab
!= 0);
622 DEFUN(bfd_add_to_strtab
, (abfd
, ss
, str
),
624 struct strtab
*ss AND
627 /* should search first, but for now: */
628 /* include the trailing NUL */
629 int ln
= strlen(str
)+1;
631 /* should this be using obstacks? */
632 ss
->tab
= realloc(ss
->tab
, ss
->length
+ ln
);
634 BFD_ASSERT(ss
->tab
!= 0);
635 strcpy(ss
->tab
+ ss
->length
, str
);
639 return ss
->length
- ln
;
643 DEFUN(bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
645 struct strtab
*ss AND
649 /* should search first, but for now: */
650 /* include the trailing NUL */
651 int ln
= strlen(str
)+strlen(str2
)+1;
653 /* should this be using obstacks? */
655 ss
->tab
= realloc(ss
->tab
, ss
->length
+ ln
);
657 ss
->tab
= bfd_xmalloc(ln
);
659 BFD_ASSERT(ss
->tab
!= 0);
660 strcpy(ss
->tab
+ ss
->length
, str
);
661 strcpy(ss
->tab
+ ss
->length
+ strlen(str
), str2
);
665 return ss
->length
- ln
;
668 /* Create a new ELF section from a bfd section. */
671 DEFUN(bfd_shdr_from_section
, (abfd
, hdr
, shstrtab
, indx
),
673 Elf_Internal_Shdr
*hdr AND
674 struct strtab
*shstrtab AND
680 /* figure out out to write the section name from the bfd section name. MWE */
682 sect
= abfd
->sections
;
683 for (ndx
= indx
; --ndx
; )
687 hdr
[indx
].sh_name
= bfd_add_to_strtab(abfd
, shstrtab
,
688 bfd_section_name(abfd
, sect
));
689 hdr
[indx
].sh_addr
= sect
->vma
;
690 hdr
[indx
].sh_size
= sect
->_raw_size
;
691 hdr
[indx
].sh_flags
= 0;
692 /* these need to be preserved on */
693 hdr
[indx
].sh_link
= 0;
694 hdr
[indx
].sh_info
= 0;
695 hdr
[indx
].sh_addralign
= 0;
696 hdr
[indx
].sh_entsize
= 0;
698 hdr
[indx
].sh_type
= 0;
699 if (sect
->flags
& SEC_RELOC
) {
700 hdr
[indx
].sh_type
= SHT_RELA
; /* FIXME -- sparc specific */
703 if (sect
->flags
& SEC_HAS_CONTENTS
)
705 hdr
[indx
].sh_offset
= sect
->filepos
;
706 hdr
[indx
].sh_size
= sect
->_raw_size
;
708 if (sect
->flags
& SEC_ALLOC
)
710 hdr
[indx
].sh_flags
|= SHF_ALLOC
;
711 if (sect
->flags
& SEC_LOAD
)
713 /* do something with sh_type ? */
716 if (!(sect
->flags
& SEC_READONLY
))
717 hdr
[indx
].sh_flags
|= SHF_WRITE
;
719 if (sect
->flags
& SEC_CODE
)
720 hdr
[indx
].sh_flags
|= SHF_EXECINSTR
;
725 /* Create a new bfd section from an ELF program header.
727 Since program segments have no names, we generate a synthetic name
728 of the form segment<NUM>, where NUM is generally the index in the
729 program header table. For segments that are split (see below) we
730 generate the names segment<NUM>a and segment<NUM>b.
732 Note that some program segments may have a file size that is different than
733 (less than) the memory size. All this means is that at execution the
734 system must allocate the amount of memory specified by the memory size,
735 but only initialize it with the first "file size" bytes read from the
736 file. This would occur for example, with program segments consisting
737 of combined data+bss.
739 To handle the above situation, this routine generates TWO bfd sections
740 for the single program segment. The first has the length specified by
741 the file size of the segment, and the second has the length specified
742 by the difference between the two sizes. In effect, the segment is split
743 into it's initialized and uninitialized parts.
748 DEFUN(bfd_section_from_phdr
, (abfd
, hdr
, index
),
750 Elf_Internal_Phdr
*hdr AND
758 split
= ((hdr
-> p_memsz
> 0) &&
759 (hdr
-> p_filesz
> 0) &&
760 (hdr
-> p_memsz
> hdr
-> p_filesz
));
761 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
762 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
763 strcpy (name
, namebuf
);
764 newsect
= bfd_make_section (abfd
, name
);
765 newsect
-> vma
= hdr
-> p_vaddr
;
766 newsect
-> _raw_size
= hdr
-> p_filesz
;
767 newsect
-> filepos
= hdr
-> p_offset
;
768 newsect
-> flags
|= SEC_HAS_CONTENTS
;
769 if (hdr
-> p_type
== PT_LOAD
)
771 newsect
-> flags
|= SEC_ALLOC
;
772 newsect
-> flags
|= SEC_LOAD
;
773 if (hdr
-> p_flags
& PF_X
)
775 /* FIXME: all we known is that it has execute PERMISSION,
777 newsect
-> flags
|= SEC_CODE
;
780 if (!(hdr
-> p_flags
& PF_W
))
782 newsect
-> flags
|= SEC_READONLY
;
787 sprintf (namebuf
, "segment%db", index
);
788 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
789 strcpy (name
, namebuf
);
790 newsect
= bfd_make_section (abfd
, name
);
791 newsect
-> vma
= hdr
-> p_vaddr
+ hdr
-> p_filesz
;
792 newsect
-> _raw_size
= hdr
-> p_memsz
- hdr
-> p_filesz
;
793 if (hdr
-> p_type
== PT_LOAD
)
795 newsect
-> flags
|= SEC_ALLOC
;
796 if (hdr
-> p_flags
& PF_X
)
797 newsect
-> flags
|= SEC_CODE
;
799 if (!(hdr
-> p_flags
& PF_W
))
800 newsect
-> flags
|= SEC_READONLY
;
809 DEFUN(bfd_prstatus
,(abfd
, descdata
, descsz
, filepos
),
816 prstatus_t
*status
= (prstatus_t
*)0;
818 if (descsz
== sizeof (prstatus_t
))
820 newsect
= bfd_make_section (abfd
, ".reg");
821 newsect
-> _raw_size
= sizeof (status
->pr_reg
);
822 newsect
-> filepos
= filepos
+ (long) &status
->pr_reg
;
823 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
824 newsect
-> alignment_power
= 2;
825 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
827 memcpy (core_prstatus (abfd
), descdata
, descsz
);
832 /* Stash a copy of the prpsinfo structure away for future use. */
835 DEFUN(bfd_prpsinfo
,(abfd
, descdata
, descsz
, filepos
),
843 if (descsz
== sizeof (prpsinfo_t
))
845 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
847 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
853 DEFUN(bfd_fpregset
,(abfd
, descdata
, descsz
, filepos
),
861 newsect
= bfd_make_section (abfd
, ".reg2");
862 newsect
-> _raw_size
= descsz
;
863 newsect
-> filepos
= filepos
;
864 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
865 newsect
-> alignment_power
= 2;
868 #endif /* HAVE_PROCFS */
870 /* Return a pointer to the args (including the command name) that were
871 seen by the program that generated the core dump. Note that for
872 some reason, a spurious space is tacked onto the end of the args
873 in some (at least one anyway) implementations, so strip it off if
877 DEFUN(elf_core_file_failing_command
, (abfd
),
881 if (core_prpsinfo (abfd
))
883 prpsinfo_t
*p
= core_prpsinfo (abfd
);
884 char *scan
= p
-> pr_psargs
;
887 if ((scan
> p
-> pr_psargs
) && (*scan
== ' '))
891 return (p
-> pr_psargs
);
897 /* Return the number of the signal that caused the core dump. Presumably,
898 since we have a core file, we got a signal of some kind, so don't bother
899 checking the other process status fields, just return the signal number.
903 DEFUN(elf_core_file_failing_signal
, (abfd
),
907 if (core_prstatus (abfd
))
909 return (((prstatus_t
*)(core_prstatus (abfd
))) -> pr_cursig
);
915 /* Check to see if the core file could reasonably be expected to have
916 come for the current executable file. Note that by default we return
917 true unless we find something that indicates that there might be a
922 DEFUN(elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
931 /* First, xvecs must match since both are ELF files for the same target. */
933 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
935 bfd_error
= system_call_error
;
941 /* If no prpsinfo, just return true. Otherwise, grab the last component
942 of the exec'd pathname from the prpsinfo. */
944 if (core_prpsinfo (core_bfd
))
946 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
)) -> pr_fname
);
953 /* Find the last component of the executable pathname. */
955 if ((execname
= strrchr (exec_bfd
-> filename
, '/')) != NULL
)
961 execname
= (char *) exec_bfd
-> filename
;
964 /* See if they match */
966 return (strcmp (execname
, corename
) ? false : true);
972 #endif /* HAVE_PROCFS */
975 /* ELF core files contain a segment of type PT_NOTE, that holds much of
976 the information that would normally be available from the /proc interface
977 for the process, at the time the process dumped core. Currently this
978 includes copies of the prstatus, prpsinfo, and fpregset structures.
980 Since these structures are potentially machine dependent in size and
981 ordering, bfd provides two levels of support for them. The first level,
982 available on all machines since it does not require that the host
983 have /proc support or the relevant include files, is to create a bfd
984 section for each of the prstatus, prpsinfo, and fpregset structures,
985 without any interpretation of their contents. With just this support,
986 the bfd client will have to interpret the structures itself. Even with
987 /proc support, it might want these full structures for it's own reasons.
989 In the second level of support, where HAVE_PROCFS is defined, bfd will
990 pick apart the structures to gather some additional information that
991 clients may want, such as the general register set, the name of the
992 exec'ed file and its arguments, the signal (if any) that caused the
998 DEFUN(elf_corefile_note
, (abfd
, hdr
),
1000 Elf_Internal_Phdr
*hdr
)
1002 Elf_External_Note
*x_note_p
; /* Elf note, external form */
1003 Elf_Internal_Note i_note
; /* Elf note, internal form */
1004 char *buf
= NULL
; /* Entire note segment contents */
1005 char *namedata
; /* Name portion of the note */
1006 char *descdata
; /* Descriptor portion of the note */
1007 char *sectname
; /* Name to use for new section */
1008 long filepos
; /* File offset to descriptor data */
1011 if (hdr
-> p_filesz
> 0
1012 && (buf
= (char *) bfd_xmalloc (hdr
-> p_filesz
)) != NULL
1013 && bfd_seek (abfd
, hdr
-> p_offset
, SEEK_SET
) != -1
1014 && bfd_read ((PTR
) buf
, hdr
-> p_filesz
, 1, abfd
) == hdr
-> p_filesz
)
1016 x_note_p
= (Elf_External_Note
*) buf
;
1017 while ((char *) x_note_p
< (buf
+ hdr
-> p_filesz
))
1019 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> namesz
);
1020 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> descsz
);
1021 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> type
);
1022 namedata
= x_note_p
-> name
;
1023 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
1024 filepos
= hdr
-> p_offset
+ (descdata
- buf
);
1025 switch (i_note
.type
) {
1027 /* process descdata as prstatus info */
1028 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
1029 sectname
= ".prstatus";
1032 /* process descdata as fpregset info */
1033 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
1034 sectname
= ".fpregset";
1037 /* process descdata as prpsinfo */
1038 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
1039 sectname
= ".prpsinfo";
1042 /* Unknown descriptor, just ignore it. */
1046 if (sectname
!= NULL
)
1048 newsect
= bfd_make_section (abfd
, sectname
);
1049 newsect
-> _raw_size
= i_note
.descsz
;
1050 newsect
-> filepos
= filepos
;
1051 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
1052 newsect
-> alignment_power
= 2;
1054 x_note_p
= (Elf_External_Note
*)
1055 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
1067 /* Read a specified number of bytes at a specified offset in an ELF
1068 file, into a newly allocated buffer, and return a pointer to the
1072 DEFUN(elf_read
, (abfd
, offset
, size
),
1079 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
1081 bfd_error
= no_memory
;
1084 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
1086 bfd_error
= system_call_error
;
1089 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
1091 bfd_error
= system_call_error
;
1097 /* Begin processing a given object.
1099 First we validate the file by reading in the ELF header and checking
1105 DEFUN (elf_file_p
, (x_ehdrp
), Elf_External_Ehdr
*x_ehdrp
)
1107 return ((x_ehdrp
->e_ident
[EI_MAG0
] == ELFMAG0
)
1108 && (x_ehdrp
->e_ident
[EI_MAG1
] == ELFMAG1
)
1109 && (x_ehdrp
->e_ident
[EI_MAG2
] == ELFMAG2
)
1110 && (x_ehdrp
->e_ident
[EI_MAG3
] == ELFMAG3
));
1114 DEFUN (elf_object_p
, (abfd
), bfd
*abfd
)
1116 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1117 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1118 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
1119 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1121 char *shstrtab
; /* Internal copy of section header stringtab */
1122 struct elf_backend_data
*ebd
; /* Use to get ELF_ARCH stored in xvec */
1124 /* Read in the ELF header in external format. */
1126 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
1128 bfd_error
= system_call_error
;
1132 /* Now check to see if we have a valid ELF file, and one that BFD can
1133 make use of. The magic number must match, the address size ('class')
1134 and byte-swapping must match our XVEC entry, and it must have a
1135 section header table (FIXME: See comments re sections at top of this
1138 if (elf_file_p (&x_ehdr
) == false)
1141 bfd_error
= wrong_format
;
1145 /* FIXME, Check EI_VERSION here ! */
1147 switch (x_ehdr
.e_ident
[EI_CLASS
])
1149 case ELFCLASSNONE
: /* address size not specified */
1150 goto wrong
; /* No support if can't tell address size */
1151 case ELFCLASS32
: /* 32-bit addresses */
1153 case ELFCLASS64
: /* 64-bit addresses */
1154 goto wrong
; /* FIXME: 64 bits not yet supported */
1156 goto wrong
; /* No support if unknown address class */
1159 /* Switch xvec to match the specified byte order. */
1160 switch (x_ehdr
.e_ident
[EI_DATA
])
1162 case ELFDATA2MSB
: /* Big-endian */
1163 if (!abfd
->xvec
->header_byteorder_big_p
)
1166 case ELFDATA2LSB
: /* Little-endian */
1167 if (abfd
->xvec
->header_byteorder_big_p
)
1170 case ELFDATANONE
: /* No data encoding specified */
1171 default: /* Unknown data encoding specified */
1175 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1176 the tdata pointer in the bfd. */
1178 if (NULL
== (elf_tdata (abfd
) = (struct elf_obj_tdata
*)
1179 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
))))
1181 bfd_error
= no_memory
;
1185 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
1187 /* Now that we know the byte order, swap in the rest of the header */
1188 i_ehdrp
= elf_elfheader (abfd
);
1189 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
1191 /* If there is no section header table, we're hosed. */
1192 if (i_ehdrp
->e_shoff
== 0)
1195 if (i_ehdrp
->e_type
== ET_EXEC
|| i_ehdrp
->e_type
== ET_DYN
)
1196 abfd
-> flags
|= EXEC_P
;
1198 /* Retrieve the architecture information from the xvec and verify
1199 that it matches the machine info stored in the ELF header.
1200 This allows us to resolve ambiguous formats that might not
1201 otherwise be distinguishable. */
1203 ebd
= (struct elf_backend_data
*) (abfd
->xvec
->backend_data
);
1204 switch (i_ehdrp
->e_machine
)
1207 case EM_M32
: /* or should this be bfd_arch_obscure? */
1208 if (ebd
-> arch
!= bfd_arch_unknown
)
1210 bfd_default_set_arch_mach(abfd
, bfd_arch_unknown
, 0);
1213 if (ebd
-> arch
!= bfd_arch_sparc
)
1215 bfd_default_set_arch_mach(abfd
, bfd_arch_sparc
, 0);
1218 if (ebd
-> arch
!= bfd_arch_i386
)
1220 bfd_default_set_arch_mach(abfd
, bfd_arch_i386
, 0);
1223 if (ebd
-> arch
!= bfd_arch_m68k
)
1225 bfd_default_set_arch_mach(abfd
, bfd_arch_m68k
, 0);
1228 if (ebd
-> arch
!= bfd_arch_m88k
)
1230 bfd_default_set_arch_mach(abfd
, bfd_arch_m88k
, 0);
1233 if (ebd
-> arch
!= bfd_arch_i860
)
1235 bfd_default_set_arch_mach(abfd
, bfd_arch_i860
, 0);
1238 if (ebd
-> arch
!= bfd_arch_mips
)
1240 bfd_default_set_arch_mach(abfd
, bfd_arch_mips
, 0);
1243 if (ebd
-> arch
!= bfd_arch_hppa
)
1245 bfd_default_set_arch_mach (abfd
, bfd_arch_hppa
, 0);
1251 /* Allocate space for a copy of the section header table in
1252 internal form, seek to the section header table in the file,
1253 read it in, and convert it to internal form. As a simple sanity
1254 check, verify that the what BFD thinks is the size of each section
1255 header table entry actually matches the size recorded in the file. */
1257 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
1259 i_shdrp
= (Elf_Internal_Shdr
*)
1260 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
1263 bfd_error
= no_memory
;
1266 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
1268 bfd_error
= system_call_error
;
1271 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1273 if (bfd_read ((PTR
) &x_shdr
, sizeof x_shdr
, 1, abfd
)
1276 bfd_error
= system_call_error
;
1279 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
1282 elf_elfsections (abfd
) = i_shdrp
;
1284 /* Read in the string table containing the names of the sections. We
1285 will need the base pointer to this table later. */
1286 /* We read this inline now, so that we don't have to go through
1287 bfd_section_from_shdr with it (since this particular strtab is
1288 used to find all of the ELF section names.) */
1290 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
1294 /* Once all of the section headers have been read and converted, we
1295 can start processing them. Note that the first section header is
1296 a dummy placeholder entry, so we ignore it.
1298 We also watch for the symbol table section and remember the file
1299 offset and section size for both the symbol table section and the
1300 associated string table section. */
1302 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1304 bfd_section_from_shdr (abfd
, shindex
);
1307 /* Remember the entry point specified in the ELF file header. */
1309 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1311 return (abfd
->xvec
);
1315 Takes a bfd and a symbol, returns a pointer to the elf specific area
1316 of the symbol if there is one.
1318 static elf_symbol_type
*
1319 DEFUN(elf_symbol_from
,(ignore_abfd
, symbol
),
1320 bfd
*ignore_abfd AND
1323 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_elf_flavour
)
1324 return (elf_symbol_type
*)NULL
;
1326 if (symbol
->the_bfd
->tdata
.elf_obj_data
== (struct elf_obj_tdata
*)NULL
)
1327 return (elf_symbol_type
*)NULL
;
1329 return (elf_symbol_type
*) symbol
;
1332 /* Core files are simply standard ELF formatted files that partition
1333 the file using the execution view of the file (program header table)
1334 rather than the linking view. In fact, there is no section header
1335 table in a core file.
1337 The process status information (including the contents of the general
1338 register set) and the floating point register set are stored in a
1339 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
1340 that allow standard bfd access to the general registers (.reg) and the
1341 floating point registers (.reg2).
1346 DEFUN (elf_core_file_p
, (abfd
), bfd
*abfd
)
1348 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1349 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1350 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
1351 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
1352 unsigned int phindex
;
1354 /* Read in the ELF header in external format. */
1356 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
1358 bfd_error
= system_call_error
;
1362 /* Now check to see if we have a valid ELF file, and one that BFD can
1363 make use of. The magic number must match, the address size ('class')
1364 and byte-swapping must match our XVEC entry, and it must have a
1365 program header table (FIXME: See comments re segments at top of this
1368 if (elf_file_p (&x_ehdr
) == false)
1371 bfd_error
= wrong_format
;
1375 /* FIXME, Check EI_VERSION here ! */
1377 switch (x_ehdr
.e_ident
[EI_CLASS
])
1379 case ELFCLASSNONE
: /* address size not specified */
1380 goto wrong
; /* No support if can't tell address size */
1381 case ELFCLASS32
: /* 32-bit addresses */
1383 case ELFCLASS64
: /* 64-bit addresses */
1384 goto wrong
; /* FIXME: 64 bits not yet supported */
1386 goto wrong
; /* No support if unknown address class */
1389 /* Switch xvec to match the specified byte order. */
1390 switch (x_ehdr
.e_ident
[EI_DATA
])
1392 case ELFDATA2MSB
: /* Big-endian */
1393 if (abfd
->xvec
->byteorder_big_p
== false)
1396 case ELFDATA2LSB
: /* Little-endian */
1397 if (abfd
->xvec
->byteorder_big_p
== true)
1400 case ELFDATANONE
: /* No data encoding specified */
1401 default: /* Unknown data encoding specified */
1405 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1406 the tdata pointer in the bfd. */
1409 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
1410 if (elf_tdata (abfd
) == NULL
)
1412 bfd_error
= no_memory
;
1416 /* FIXME, `wrong' returns from this point onward, leak memory. */
1418 /* Now that we know the byte order, swap in the rest of the header */
1419 i_ehdrp
= elf_elfheader (abfd
);
1420 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
1422 /* If there is no program header, or the type is not a core file, then
1424 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
1427 /* Allocate space for a copy of the program header table in
1428 internal form, seek to the program header table in the file,
1429 read it in, and convert it to internal form. As a simple sanity
1430 check, verify that the what BFD thinks is the size of each program
1431 header table entry actually matches the size recorded in the file. */
1433 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
1435 i_phdrp
= (Elf_Internal_Phdr
*)
1436 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
1439 bfd_error
= no_memory
;
1442 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
1444 bfd_error
= system_call_error
;
1447 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
1449 if (bfd_read ((PTR
) &x_phdr
, sizeof (x_phdr
), 1, abfd
)
1452 bfd_error
= system_call_error
;
1455 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
1458 /* Once all of the program headers have been read and converted, we
1459 can start processing them. */
1461 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
1463 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
1464 if ((i_phdrp
+ phindex
) -> p_type
== PT_NOTE
)
1466 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
1470 /* Remember the entry point specified in the ELF file header. */
1472 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1474 return (abfd
->xvec
);
1478 DEFUN (elf_mkobject
, (abfd
), bfd
*abfd
)
1480 /* this just does initialization */
1481 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
1482 elf_tdata(abfd
) = (struct elf_obj_tdata
*)
1483 bfd_zalloc (abfd
, sizeof(struct elf_obj_tdata
));
1484 if (elf_tdata(abfd
) == 0) {
1485 bfd_error
= no_memory
;
1488 /* since everything is done at close time, do we need any
1495 Create ELF output from BFD sections.
1497 Essentially, just create the section header and forget about the program
1502 /* lacking nested functions and nested types, set up for mapping over
1503 BFD sections to produce ELF sections */
1506 Elf_Internal_Ehdr
*i_ehdr
;
1507 Elf_Internal_Shdr
*i_shdrp
;
1508 struct strtab
*shstrtab
;
1513 elf_idx_of_sym(abfd
, sym
)
1518 for ( i
= 0; i
< abfd
->symcount
; i
++ )
1520 if ( sym
== (asymbol
*)abfd
->outsymbols
[i
] )
1523 BFD_ASSERT( (strcmp(sym
->name
, abfd
->outsymbols
[i
]->name
) == 0)
1524 || (strlen(sym
->name
) == 0) );
1532 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1537 elf_sect_thunk
*thunk
= (elf_sect_thunk
*)obj
;
1538 /* most of what is in bfd_shdr_from_section goes in here... */
1539 /* and all of these sections generate at *least* one ELF section. */
1543 /* check if we're making a PROGBITS section... */
1544 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1545 /* this was too strict... what *do* we want to check here? */
1548 Elf_Internal_Shdr
*this_hdr
;
1549 this_section
= elf_section_from_bfd_section (abfd
, asect
);
1550 this_hdr
= &thunk
->i_shdrp
[this_section
];
1552 this_hdr
->sh_addr
= asect
->vma
;
1553 this_hdr
->sh_size
= asect
->_raw_size
;
1554 /* contents already set by elf_set_section_contents */
1556 if (asect
->flags
& SEC_RELOC
)
1558 /* emit a reloc section, and thus strtab and symtab... */
1559 Elf_Internal_Shdr
*rela_hdr
;
1560 Elf_Internal_Shdr
*symtab_hdr
;
1561 Elf_External_Rela
*outbound_relocs
;
1564 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1566 if (thunk
->symtab_section
== this_section
+ 1)
1567 rela_section
= thunk
->symtab_section
+ 2; /* symtab + symstrtab */
1569 rela_section
= this_section
+ 1;
1570 rela_hdr
= &thunk
->i_shdrp
[rela_section
];
1571 rela_hdr
->sh_type
= SHT_RELA
;
1572 rela_hdr
->sh_link
= thunk
->symtab_section
;
1573 rela_hdr
->sh_info
= this_section
;
1574 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1575 /* orelocation has the data, reloc_count has the count... */
1576 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* asect
->reloc_count
;
1577 outbound_relocs
= (Elf_External_Rela
*)
1578 bfd_alloc(abfd
, asect
->reloc_count
* sizeof(Elf_External_Rela
));
1579 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
1581 Elf_Internal_Rela dst
;
1583 Elf_External_Rela
*src
;
1585 ptr
= asect
->orelocation
[idx
];
1586 src
= outbound_relocs
+ idx
;
1587 if (asect
->flags
& SEC_RELOC
)
1588 dst
.r_offset
= ptr
->address
- asect
->vma
;
1590 dst
.r_offset
= ptr
->address
;
1592 /* @@ This assumes the symbols were written (or will be
1593 written) in the same order that they appear in
1594 abfd->outsymbols. */
1595 if (ptr
->sym_ptr_ptr
&& ptr
->sym_ptr_ptr
[0])
1596 dst
.r_info
= ELF_R_INFO (elf_idx_of_sym (abfd
,
1597 ptr
->sym_ptr_ptr
[0]),
1600 dst
.r_info
= ELF_R_INFO (STN_UNDEF
, ptr
->howto
->type
);
1602 dst
.r_addend
= ptr
->addend
;
1603 elf_swap_reloca_out(abfd
, &dst
, src
);
1605 rela_hdr
->contents
= (void*)outbound_relocs
;
1607 if (asect
->flags
& SEC_ALLOC
)
1609 this_hdr
->sh_flags
|= SHF_ALLOC
;
1610 if (asect
->flags
& SEC_LOAD
)
1612 /* @@ Do something with sh_type? */
1615 if (!(asect
->flags
& SEC_READONLY
))
1616 this_hdr
->sh_flags
|= SHF_WRITE
;
1618 if (asect
->flags
& SEC_CODE
)
1619 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1624 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1629 elf_sect_thunk
*thunk
= (elf_sect_thunk
*)obj
;
1630 /* most of what is in bfd_shdr_from_section goes in here... */
1631 /* and all of these sections generate at *least* one ELF section. */
1634 /* check if we're making a PROGBITS section... */
1635 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1636 /* this was too strict... what *do* we want to check here? */
1639 Elf_Internal_Shdr
*this_hdr
;
1640 this_section
= thunk
->i_ehdr
->e_shnum
++;
1641 this_hdr
= &thunk
->i_shdrp
[this_section
];
1643 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, asect
->name
);
1644 /* we need to log the type *now* so that elf_section_from_bfd_section
1645 can find us... have to set rawdata too. */
1646 this_hdr
->rawdata
= (void*)asect
;
1647 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1648 this_hdr
->sh_type
= SHT_PROGBITS
;
1649 /* @@ Select conditions correctly! */
1650 else if (!strcmp (asect
->name
, ".bss"))
1651 this_hdr
->sh_type
= SHT_NOBITS
;
1653 /* what *do* we put here? */
1654 this_hdr
->sh_type
= SHT_PROGBITS
;
1658 /* Emit a strtab and symtab, and possibly a reloc section. */
1659 Elf_Internal_Shdr
*rela_hdr
;
1660 Elf_Internal_Shdr
*symtab_hdr
;
1661 Elf_Internal_Shdr
*symstrtab_hdr
;
1663 int symstrtab_section
;
1665 /* Note that only one symtab is used, so just remember it
1667 if (! thunk
->symtab_section
)
1669 thunk
->symtab_section
= thunk
->i_ehdr
->e_shnum
++;
1670 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1671 symtab_hdr
->sh_name
=
1672 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, ".symtab");
1673 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1674 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1676 symstrtab_section
= thunk
->i_ehdr
->e_shnum
++;
1677 BFD_ASSERT(symstrtab_section
== thunk
->symtab_section
+1);
1678 symstrtab_hdr
= &thunk
->i_shdrp
[symstrtab_section
];
1679 symtab_hdr
->sh_link
= symstrtab_section
;
1680 symstrtab_hdr
->sh_name
=
1681 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, ".strtab");
1682 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1684 symtab_hdr
->contents
= 0;
1685 symstrtab_hdr
->contents
= 0;
1686 symstrtab_hdr
->sh_size
= 0;
1689 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1691 if (asect
->flags
& SEC_RELOC
)
1693 rela_section
= thunk
->i_ehdr
->e_shnum
++;
1694 rela_hdr
= &thunk
->i_shdrp
[rela_section
];
1696 bfd_add_2_to_strtab (abfd
, thunk
->shstrtab
, ".rela",
1698 rela_hdr
->sh_type
= SHT_RELA
;
1699 rela_hdr
->sh_link
= thunk
->symtab_section
;
1700 rela_hdr
->sh_info
= this_section
;
1701 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1704 if (asect
->flags
& SEC_ALLOC
)
1706 this_hdr
->sh_flags
|= SHF_ALLOC
;
1707 if (asect
->flags
& SEC_LOAD
)
1709 /* @@ Do something with sh_type? */
1712 if (!(asect
->flags
& SEC_READONLY
))
1713 this_hdr
->sh_flags
|= SHF_WRITE
;
1714 if (asect
->flags
& SEC_CODE
)
1715 this_hdr
->sh_flags
|= SHF_EXECINSTR
;
1721 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
*abfd
)
1723 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1724 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1725 struct strtab
*shstrtab
;
1726 int count
, maxsections
;
1729 if (! elf_shstrtab (abfd
)) {
1730 i_ehdrp
= elf_elfheader (abfd
); /* build new header in tdata memory */
1731 shstrtab
= bfd_new_strtab(abfd
);
1733 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
1734 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
1735 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
1736 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
1738 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS32
; /* FIXME: find out from bfd */
1739 i_ehdrp
->e_ident
[EI_DATA
] =
1740 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
1741 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1743 for(count
= EI_PAD
; count
< EI_NIDENT
; count
++)
1744 i_ehdrp
->e_ident
[count
] = 0;
1746 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
)? ET_EXEC
: ET_REL
;
1747 switch(bfd_get_arch(abfd
))
1749 case bfd_arch_unknown
:
1750 i_ehdrp
->e_machine
= EM_NONE
;
1752 case bfd_arch_sparc
:
1753 i_ehdrp
->e_machine
= EM_SPARC
;
1756 i_ehdrp
->e_machine
= EM_386
;
1759 i_ehdrp
->e_machine
= EM_68K
;
1762 i_ehdrp
->e_machine
= EM_88K
;
1765 i_ehdrp
->e_machine
= EM_860
;
1767 case bfd_arch_mips
: /* MIPS Rxxxx */
1768 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
1771 i_ehdrp
->e_machine
= EM_HPPA
;
1773 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1775 i_ehdrp
->e_machine
= EM_NONE
;
1777 i_ehdrp
->e_version
= EV_CURRENT
;
1778 i_ehdrp
->e_ehsize
= sizeof(Elf_External_Ehdr
);
1780 /* no program header, for now. */
1781 i_ehdrp
->e_phoff
= 0;
1782 i_ehdrp
->e_phentsize
= 0;
1783 i_ehdrp
->e_phnum
= 0;
1785 /* each bfd section is section header entry */
1786 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
1787 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
1789 /* figure at most each section can have a rel, strtab, symtab */
1790 maxsections
= 4*bfd_count_sections(abfd
)+2;
1792 i_ehdrp
->e_shoff
= i_ehdrp
->e_ehsize
;
1794 /* and we'll just have to fix up the offsets later. */
1795 /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */
1797 i_shdrp
= (Elf_Internal_Shdr
*)
1798 bfd_alloc (abfd
, sizeof (*i_shdrp
) * maxsections
);
1801 bfd_error
= no_memory
;
1804 for (count
=0; count
< maxsections
; count
++)
1806 i_shdrp
[count
].rawdata
= 0;
1807 i_shdrp
[count
].contents
= 0;
1811 i_shdrp
[0].sh_name
= 0;
1812 i_shdrp
[0].sh_type
= SHT_NULL
;
1813 i_shdrp
[0].sh_flags
= 0;
1814 i_shdrp
[0].sh_addr
= 0;
1815 i_shdrp
[0].sh_offset
= 0;
1816 i_shdrp
[0].sh_size
= 0;
1817 i_shdrp
[0].sh_link
= SHN_UNDEF
;
1818 i_shdrp
[0].sh_info
= 0;
1819 i_shdrp
[0].sh_addralign
= 0;
1820 i_shdrp
[0].sh_entsize
= 0;
1822 i_ehdrp
->e_shnum
= 1;
1824 elf_elfsections (abfd
) = i_shdrp
;
1825 elf_shstrtab (abfd
) = shstrtab
;
1827 est
.i_ehdr
= elf_elfheader(abfd
);
1828 est
.i_shdrp
= elf_elfsections(abfd
);
1829 est
.shstrtab
= elf_shstrtab(abfd
);
1830 est
.symtab_section
= 0; /* elf_fake_sections fils it in */
1832 bfd_map_over_sections(abfd
, elf_fake_sections
, &est
);
1833 elf_onesymtab (abfd
) = est
.symtab_section
;
1838 DEFUN (elf_write_phdrs
, (abfd
, i_ehdrp
, i_phdrp
, phdr_cnt
),
1840 Elf_Internal_Ehdr
*i_ehdrp AND
1841 Elf_Internal_Phdr
*i_phdrp AND
1844 /* first program header entry goes after the file header */
1845 int outbase
= i_ehdrp
->e_ehsize
;
1847 Elf_External_Phdr x_phdr
;
1849 for ( i
= 0; i
< phdr_cnt
; i
++ ) {
1850 elf_swap_phdr_out(abfd
, i_phdrp
+ i
, &x_phdr
);
1851 bfd_seek(abfd
, outbase
, SEEK_SET
);
1852 bfd_write( (PTR
)&x_phdr
, sizeof(x_phdr
), 1, abfd
);
1853 outbase
+= sizeof(x_phdr
);
1859 static Elf_Internal_Phdr
*
1860 DEFUN (elf_build_phdrs
, (abfd
, i_ehdrp
, i_shdrp
, phdr_cnt
),
1862 Elf_Internal_Ehdr
*i_ehdrp AND
1863 Elf_Internal_Shdr
*i_shdrp AND
1866 Elf_Internal_Phdr
*phdr_buf
;
1870 1. The program header table is *not* loaded as part
1871 of the memory image of the program. If this
1872 changes later, the PT_PHDR entry must come first.
1873 2. there is currently no support for program header
1874 entries of type PT_PHDR, PT_DYNAMIC, PT_INTERP,
1878 /* A. Figure out how many program header table entries are needed */
1879 /* 1. PT_LOAD for the text segment */
1880 /* 2. PT_LOAD for the data segment */
1881 /* Then, reserve space for one more pointer. This will be NULL */
1882 /* to indicate the end of the program header table. */
1884 #ifdef PHDRS_INCLUDED
1887 *phdr_cnt
= 3; /* XXX right now, execve() expects exactly 3 PT entries */
1890 phdr_buf
= (Elf_Internal_Phdr
*)bfd_xmalloc( ((*phdr_cnt
) + 1)
1892 sizeof(Elf_Internal_Phdr
));
1895 #ifdef PHDRS_INCLUDED
1896 /* B. Fill in the PT_PHDR entry. */
1901 /* C. Fill in the PT_LOAD entry for the text segment. */
1903 phdr_buf
[idx
].p_type
= PT_LOAD
;
1905 /* get virtual/physical address from .text section */
1906 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name(abfd
,".text")->vma
;
1907 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
1909 /* Ultimately, we would like the size of the .text load */
1910 /* segment to be the sum of the following sections: */
1911 /* the program header table itself */
1922 /* But, right now, it will be the sum of the following */
1928 static char *CONST ld_sect_names
[] =
1929 { ".text", ".rodata", NULL
};
1933 for ( i
= 0; ld_sect_names
[i
]; i
++ ) {
1934 asection
*asect
= bfd_get_section_by_name(abfd
,
1938 ld_size
+= bfd_section_size(abfd
, asect
);
1940 phdr_buf
[idx
].p_filesz
= ld_size
;
1941 /* XXX: need to fix this */
1942 phdr_buf
[idx
].p_memsz
= ld_size
;
1944 phdr_buf
[idx
].p_flags
= PF_R
+ PF_X
;
1945 phdr_buf
[idx
].p_align
1946 = bfd_get_section_by_name(abfd
,".text")->alignment_power
;
1950 /* D. Fill in the PT_LOAD entry for the data segment. */
1952 phdr_buf
[idx
].p_type
= PT_LOAD
;
1954 /* get virtual/physical address from .data section */
1955 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name(abfd
,".data")->vma
;
1956 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
1958 /* Ultimately, we would like the size of the data load segment */
1959 /* to be the sum of the following sections: */
1960 /* the PT_DYNAMIC program header table entry */
1966 /* But, right now, it will be the sum of the following */
1971 static char *CONST ld_sect_names
[] =
1976 for ( i
= 0; ld_sect_names
[i
]; i
++ ) {
1977 asection
*asect
= bfd_get_section_by_name(abfd
,
1981 ld_size
+= bfd_section_size(abfd
, asect
);
1983 phdr_buf
[idx
].p_filesz
= ld_size
;
1984 /* XXX: need to fix this */
1985 phdr_buf
[idx
].p_memsz
= ld_size
;
1987 phdr_buf
[idx
].p_flags
= PF_R
+ PF_W
+ PF_X
;
1988 phdr_buf
[idx
].p_align
1989 = bfd_get_section_by_name(abfd
,".data")->alignment_power
;
1993 /* E. Fill in the PT_LOAD entry for the bss segment. */
1995 phdr_buf
[idx
].p_type
= PT_LOAD
;
1997 /* get virtual/physical address from .data section */
1998 phdr_buf
[idx
].p_vaddr
= bfd_get_section_by_name(abfd
,".bss")->vma
;
1999 phdr_buf
[idx
].p_paddr
= 0; /* XXX */
2002 static char *CONST ld_sect_names
[] =
2007 for ( i
= 0; ld_sect_names
[i
]; i
++ ) {
2008 asection
*asect
= bfd_get_section_by_name(abfd
,
2012 ld_size
+= bfd_section_size(abfd
, asect
);
2014 phdr_buf
[idx
].p_filesz
= 0;
2015 /* XXX: need to fix this */
2016 phdr_buf
[idx
].p_memsz
= ld_size
;
2018 phdr_buf
[idx
].p_flags
= PF_R
+ PF_W
+ PF_X
;
2019 phdr_buf
[idx
].p_align
2020 = bfd_get_section_by_name(abfd
,".bss")->alignment_power
;
2024 /* F. Set up the "end of program header table" sentinel. */
2026 bzero((char *)(phdr_buf
+idx
),sizeof(Elf_Internal_Phdr
));
2029 BFD_ASSERT(idx
- 1 == *phdr_cnt
);
2035 DEFUN (elf_write_object_contents
, (abfd
), bfd
*abfd
)
2037 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
2038 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
2039 Elf_External_Phdr
*x_phdrp
; /* Program header table, external form */
2040 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
2041 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
2042 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
2049 struct strtab
*shstrtab
;
2051 if(abfd
->output_has_begun
== false)
2053 elf_compute_section_file_positions(abfd
);
2054 abfd
->output_has_begun
= true;
2057 i_ehdrp
= elf_elfheader (abfd
);
2058 i_shdrp
= elf_elfsections (abfd
);
2059 shstrtab
= elf_shstrtab (abfd
);
2061 est
.i_ehdr
= i_ehdrp
;
2062 est
.i_shdrp
= i_shdrp
;
2063 est
.shstrtab
= shstrtab
;
2064 est
.symtab_section
= elf_onesymtab (abfd
); /* filled in by elf_fake */
2066 bfd_map_over_sections(abfd
, elf_make_sections
, &est
);
2068 /* Dump out the symtabs. */
2070 int symcount
= bfd_get_symcount (abfd
);
2071 asymbol
** syms
= bfd_get_outsymbols (abfd
);
2072 struct strtab
* stt
= bfd_new_strtab (abfd
);
2073 Elf_Internal_Shdr
*symtab_hdr
;
2074 Elf_Internal_Shdr
*symstrtab_hdr
;
2075 int symstrtab_section
;
2076 Elf_External_Sym
*outbound_syms
;
2079 symtab_hdr
= &i_shdrp
[est
.symtab_section
];
2080 symtab_hdr
->sh_type
= SHT_SYMTAB
;
2081 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
2082 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* (symcount
+ 1);
2084 /* see assert in elf_fake_sections that supports this: */
2085 symstrtab_section
= est
.symtab_section
+1;
2086 symstrtab_hdr
= &i_shdrp
[symstrtab_section
];
2087 symtab_hdr
->sh_link
= symstrtab_section
;
2088 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2090 outbound_syms
= (Elf_External_Sym
*)
2091 bfd_alloc(abfd
, (1+symcount
) * sizeof(Elf_External_Sym
));
2092 /* now generate the data (for "contents") */
2093 for (idx
= 0; idx
< symcount
; idx
++)
2095 Elf_Internal_Sym sym
;
2096 bfd_vma value
= syms
[idx
]->value
;
2098 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
2100 value
+= syms
[idx
]->section
->output_section
->vma
2101 + syms
[idx
]->section
->output_offset
;
2102 sym
.st_value
= value
;
2104 sym
.st_size
= (Elf_Word
)(elf_symbol_from(abfd
, syms
[idx
]))->internal_elf_sym
.st_size
;
2106 if (syms
[idx
]->flags
& BSF_WEAK
)
2107 sym
.st_info
= ELF_ST_INFO(STB_WEAK
, STT_OBJECT
);
2108 else if (syms
[idx
]->flags
& BSF_LOCAL
) {
2109 if ( syms
[idx
]->flags
& BSF_FUNCTION
)
2110 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_FUNC
);
2112 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_OBJECT
);
2114 else if (syms
[idx
]->flags
& BSF_GLOBAL
) {
2115 if ( syms
[idx
]->flags
& BSF_FUNCTION
)
2116 sym
.st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_FUNC
);
2118 sym
.st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_OBJECT
);
2120 else if (syms
[idx
]->flags
& BSF_EXPORT
) {
2121 if ( syms
[idx
]->flags
& BSF_FUNCTION
)
2122 sym
.st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_FUNC
);
2124 sym
.st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_OBJECT
);
2126 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
2127 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_SECTION
);
2128 else if (syms
[idx
]->flags
& BSF_FILE
)
2129 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_FILE
);
2131 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_OBJECT
);
2134 if (syms
[idx
]->section
)
2136 elf_section_from_bfd_section(abfd
,
2137 syms
[idx
]->section
->output_section
);
2139 sym
.st_shndx
= SHN_UNDEF
;
2141 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
+idx
+1);
2144 /* fill in 0th symbol */
2145 Elf_Internal_Sym sym
;
2151 sym
.st_shndx
= SHN_UNDEF
;
2152 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
2154 symtab_hdr
->contents
= (void*)outbound_syms
;
2155 symstrtab_hdr
->contents
= (void*)stt
->tab
;
2156 symstrtab_hdr
->sh_size
= stt
->length
;
2157 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
2160 /* put the strtab out too... */
2162 Elf_Internal_Shdr
*this_hdr
;
2165 this_section
= i_ehdrp
->e_shnum
++;
2166 i_ehdrp
->e_shstrndx
= this_section
;
2167 this_hdr
= &i_shdrp
[this_section
];
2168 this_hdr
->sh_name
= bfd_add_to_strtab (abfd
, shstrtab
, ".shstrtab");
2169 this_hdr
->sh_type
= SHT_STRTAB
;
2170 this_hdr
->sh_size
= shstrtab
->length
;
2171 this_hdr
->sh_type
= SHT_STRTAB
;
2172 this_hdr
->contents
= (void*)shstrtab
->tab
;
2175 outbase
= i_ehdrp
->e_ehsize
;
2177 /* if we're building an executable, we'll need a program header table */
2178 if (abfd
->flags
& EXEC_P
)
2180 i_ehdrp
->e_phentsize
= sizeof(Elf_External_Phdr
);
2182 /* elf_build_phdrs() returns a (NULL-terminated) array of
2183 Elf_Internal_Phdrs */
2184 i_phdrp
= elf_build_phdrs(abfd
,i_ehdrp
, i_shdrp
, &i_ehdrp
->e_phnum
);
2185 i_ehdrp
->e_phoff
= i_ehdrp
->e_ehsize
;
2186 i_ehdrp
->e_shoff
= i_ehdrp
->e_phoff
+ (i_ehdrp
->e_phentsize
2187 * i_ehdrp
->e_phnum
);
2190 /* swap the header before spitting it out... */
2191 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
2192 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
2193 bfd_write ((PTR
) &x_ehdr
, sizeof(x_ehdr
), 1, abfd
);
2195 outbase
+= i_ehdrp
->e_phentsize
* i_ehdrp
->e_phnum
;
2196 outbase
+= i_ehdrp
->e_shentsize
* i_ehdrp
->e_shnum
;
2198 /* now we fix up the offsets... */
2199 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
2201 i_shdrp
[count
].sh_offset
= outbase
;
2202 outbase
+= i_shdrp
[count
].sh_size
;
2205 /* If we're building an executable, fixup the program header table
2208 @@ For now, assume that the entries are in a fixed order: text,
2211 if ( abfd
->flags
& EXEC_P
)
2213 static char *CONST section_name
[] = { ".text", ".data", ".bss" };
2215 for ( count
= 0; count
< 3; count
++ )
2217 asection
*asect
= bfd_get_section_by_name(abfd
, section_name
[count
]);
2218 int sh_idx
= elf_section_from_bfd_section(abfd
, asect
);
2220 i_phdrp
[count
].p_offset
= i_shdrp
[sh_idx
].sh_offset
;
2223 /* write out the program header table entries */
2224 elf_write_phdrs(abfd
, i_ehdrp
, i_phdrp
, i_ehdrp
->e_phnum
);
2227 /* at this point we've concocted all the ELF sections... */
2228 x_shdrp
= (Elf_External_Shdr
*)
2229 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
2232 bfd_error
= no_memory
;
2236 for (count
= 0, scnt
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2238 elf_swap_shdr_out (abfd
, i_shdrp
+count
, x_shdrp
+scnt
);
2241 bfd_write ((PTR
) x_shdrp
, sizeof(*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
2242 /* need to dump the string table too... */
2244 /* after writing the headers, we need to write the sections too... */
2245 nsect
= abfd
->sections
;
2246 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
2248 if(i_shdrp
[count
].contents
)
2250 bfd_seek (abfd
, i_shdrp
[count
].sh_offset
, SEEK_SET
);
2251 bfd_write (i_shdrp
[count
].contents
, i_shdrp
[count
].sh_size
, 1, abfd
);
2258 /* Given an index of a section, retrieve a pointer to it. Note
2259 that for our purposes, sections are indexed by {1, 2, ...} with
2260 0 being an illegal index. */
2262 /* In the original, each ELF section went into exactly one BFD
2263 section. This doesn't really make sense, so we need a real mapping.
2264 The mapping has to hide in the Elf_Internal_Shdr since asection
2265 doesn't have anything like a tdata field... */
2268 DEFUN (section_from_elf_index
, (abfd
, index
),
2272 /* @@ Is bfd_com_section really correct in all the places it could
2273 be returned from this routine? */
2275 if (index
== SHN_ABS
)
2276 return &bfd_com_section
;
2277 if (index
== SHN_COMMON
)
2278 return &bfd_com_section
;
2281 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
2282 Elf_Internal_Shdr
*hdr
= i_shdrp
+ index
;
2284 switch (hdr
->sh_type
)
2286 /* ELF sections that map to BFD sections */
2290 bfd_section_from_shdr (abfd
, index
);
2291 return (struct sec
*) hdr
->rawdata
;
2294 return (struct sec
*) &bfd_abs_section
;
2299 /* given a section, search the header to find them... */
2301 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
2305 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
2307 Elf_Internal_Shdr
*hdr
;
2308 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
2310 if (asect
== &bfd_abs_section
)
2312 if (asect
== &bfd_com_section
)
2315 for(index
= 0; index
< maxindex
; index
++) {
2316 hdr
= &i_shdrp
[index
];
2317 switch (hdr
->sh_type
)
2319 /* ELF sections that map to BFD sections */
2324 if (((struct sec
*)(hdr
->rawdata
)) == asect
)
2336 DEFUN (elf_slurp_symbol_table
, (abfd
, symptrs
),
2338 asymbol
**symptrs
) /* Buffer for generated bfd symbols */
2340 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
2341 Elf_Internal_Shdr
*hdr
= i_shdrp
+ elf_onesymtab (abfd
);
2342 int symcount
; /* Number of external ELF symbols */
2344 elf_symbol_type
*sym
; /* Pointer to current bfd symbol */
2345 elf_symbol_type
*symbase
; /* Buffer for generated bfd symbols */
2346 Elf_Internal_Sym i_sym
;
2347 Elf_External_Sym
*x_symp
;
2349 /* this is only valid because there is only one symtab... */
2350 /* FIXME: This is incorrect, there may also be a dynamic symbol
2351 table which is a subset of the full symbol table. We either need
2352 to be prepared to read both (and merge them) or ensure that we
2353 only read the full symbol table. Currently we only get called to
2354 read the full symbol table. -fnf */
2355 if (bfd_get_outsymbols (abfd
) != NULL
)
2360 /* Read each raw ELF symbol, converting from external ELF form to
2361 internal ELF form, and then using the information to create a
2362 canonical bfd symbol table entry.
2364 Note that we allocate the initial bfd canonical symbol buffer
2365 based on a one-to-one mapping of the ELF symbols to canonical
2366 symbols. We actually use all the ELF symbols, so there will be no
2367 space left over at the end. When we have all the symbols, we
2368 build the caller's pointer vector. */
2370 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
2372 bfd_error
= system_call_error
;
2376 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2377 symbase
= (elf_symbol_type
*) bfd_zalloc (abfd
, symcount
* sizeof (elf_symbol_type
));
2380 /* Temporarily allocate room for the raw ELF symbols. */
2381 x_symp
= (Elf_External_Sym
*) bfd_xmalloc (symcount
* sizeof (Elf_External_Sym
));
2383 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
2384 != symcount
* sizeof (Elf_External_Sym
))
2387 bfd_error
= system_call_error
;
2390 /* Skip first symbol, which is a null dummy. */
2391 for (i
= 1; i
< symcount
; i
++)
2393 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
2394 memcpy (&sym
->internal_elf_sym
, &i_sym
, sizeof (Elf_Internal_Sym
));
2395 memcpy (&sym
->native_elf_sym
, x_symp
+ i
, sizeof (Elf_External_Sym
));
2396 sym
->symbol
.the_bfd
= abfd
;
2397 if (i_sym
.st_name
> 0)
2398 sym
->symbol
.name
= elf_string_from_elf_section(abfd
, hdr
->sh_link
,
2401 sym
->symbol
.name
= ""; /* perhaps should include the number? */
2403 sym
->symbol
.value
= i_sym
.st_value
;
2404 /* FIXME -- this is almost certainly bogus. It's from Pace
2405 Willisson's hasty Solaris support, to pass the sizes of
2406 object files or functions down into GDB via the back door, to
2407 circumvent some other kludge in how Sun hacked stabs. --
2409 /* XXX size is now stored via a pointer in an elf_symbol_type */
2410 /* sym ->symbol.udata = (PTR)i_sym.st_size; */
2411 /* FIXME -- end of bogosity. */
2412 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
2414 sym
->symbol
.section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
2416 else if (i_sym
.st_shndx
== SHN_ABS
)
2418 sym
->symbol
.section
= &bfd_abs_section
;
2420 else if (i_sym
.st_shndx
== SHN_COMMON
)
2422 sym
->symbol
.section
= &bfd_com_section
;
2424 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2426 sym
->symbol
.section
= &bfd_und_section
;
2429 sym
->symbol
.section
= &bfd_abs_section
;
2431 switch (ELF_ST_BIND (i_sym
.st_info
))
2434 sym
->symbol
.flags
|= BSF_LOCAL
;
2437 sym
->symbol
.flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
2440 sym
->symbol
.flags
|= BSF_WEAK
;
2444 switch (ELF_ST_TYPE (i_sym
.st_info
))
2447 sym
->symbol
.flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2450 sym
->symbol
.flags
|= BSF_FILE
| BSF_DEBUGGING
;
2453 sym
->symbol
.flags
|= BSF_FUNCTION
;
2456 /* Is this a definition of $global$? If so, keep it because it will be
2457 needd if any relocations are performed. */
2458 if (!strcmp (sym
->symbol
.name
, "$global$")
2459 && sym
->symbol
.section
!= &bfd_und_section
)
2461 /* @@ Why is this referring to backend data and not a field of
2463 struct elf_backend_data
*be_data
= (struct elf_backend_data
*) abfd
->xvec
->backend_data
;
2465 be_data
->global_sym
= sym
;
2470 /* We rely on the zalloc to clear out the final symbol entry. */
2472 obj_raw_syms (abfd
) = x_symp
;
2474 bfd_get_symcount(abfd
) = symcount
= sym
- symbase
;
2476 /* Fill in the user's symbol pointer vector if needed. */
2480 while (symcount
-- > 0)
2482 *symptrs
++ = &sym
->symbol
;
2485 *symptrs
= 0; /* Final null pointer */
2491 /* Return the number of bytes required to hold the symtab vector.
2493 Note that we base it on the count plus 1, since we will null terminate
2494 the vector allocated based on this size. However, the ELF symbol table
2495 always has a dummy entry as symbol #0, so it ends up even. */
2498 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
*abfd
)
2500 unsigned int symcount
;
2501 unsigned int symtab_size
= 0;
2502 Elf_Internal_Shdr
*i_shdrp
;
2503 Elf_Internal_Shdr
*hdr
;
2505 i_shdrp
= elf_elfsections (abfd
);
2506 if (i_shdrp
!= NULL
)
2508 hdr
= i_shdrp
+ elf_onesymtab (abfd
);
2509 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2510 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
));
2512 return (symtab_size
);
2516 This function return the number of bytes required to store the
2517 relocation information associated with section <<sect>>
2518 attached to bfd <<abfd>>
2522 elf_get_reloc_upper_bound (abfd
, asect
)
2526 if (asect
->flags
& SEC_RELOC
)
2528 /* either rel or rela */
2529 return asect
->_raw_size
;
2536 DEFUN(elf_slurp_reloca_table
,(abfd
, asect
, symbols
),
2541 Elf_External_Rela
*native_relocs
;
2542 arelent
*reloc_cache
;
2547 if (asect
->relocation
)
2549 if (asect
->reloc_count
== 0)
2551 if (asect
->flags
& SEC_CONSTRUCTOR
)
2554 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2555 native_relocs
= (Elf_External_Rela
*)
2556 bfd_alloc(abfd
, asect
->reloc_count
* sizeof(Elf_External_Rela
));
2557 bfd_read ((PTR
) native_relocs
,
2558 sizeof(Elf_External_Rela
), asect
->reloc_count
, abfd
);
2560 reloc_cache
= (arelent
*)
2561 bfd_alloc(abfd
, (size_t) (asect
->reloc_count
* sizeof(arelent
)));
2563 if (! reloc_cache
) {
2564 bfd_error
= no_memory
;
2568 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2570 #ifdef RELOC_PROCESSING
2571 /* sparc, 68k, 88k, 860 use rela only. */
2572 /* 386 and we32000 use rel only... fix it for them later. */
2573 Elf_Internal_Rela dst
;
2574 Elf_External_Rela
*src
;
2576 cache_ptr
= reloc_cache
+ idx
;
2577 src
= native_relocs
+ idx
;
2578 elf_swap_reloca_in(abfd
, src
, &dst
);
2580 RELOC_PROCESSING(cache_ptr
, &dst
, symbols
, abfd
, asect
);
2582 Elf_Internal_Rela dst
;
2583 Elf_External_Rela
*src
;
2585 cache_ptr
= reloc_cache
+ idx
;
2586 src
= native_relocs
+ idx
;
2588 elf_swap_reloca_in(abfd
, src
, &dst
);
2590 if(asect
->flags
& SEC_RELOC
)
2592 /* relocatable, so the offset is off of the section */
2593 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2597 /* non-relocatable, so the offset a virtual address */
2598 cache_ptr
->address
= dst
.r_offset
;
2600 /* ELF_R_SYM(dst.r_info) is the symbol table offset; subtract 1
2601 because the first entry is NULL. */
2602 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM(dst
.r_info
) - 1;
2603 cache_ptr
->addend
= dst
.r_addend
;
2605 /* Fill in the cache_ptr->howto field from dst.r_type */
2607 struct elf_backend_data
*ebd
;
2608 ebd
= (struct elf_backend_data
*) (abfd
->xvec
->backend_data
);
2609 (*ebd
->elf_info_to_howto
)(abfd
, cache_ptr
, &dst
);
2614 asect
->relocation
= reloc_cache
;
2620 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2626 arelent
*tblptr
= section
->relocation
;
2627 unsigned int count
= 0;
2629 /* snarfed from coffcode.h */
2630 /* FIXME: this could be reloc... */
2631 elf_slurp_reloca_table(abfd
, section
, symbols
);
2633 tblptr
= section
->relocation
;
2637 for (; count
++ < section
->reloc_count
;)
2638 *relptr
++ = tblptr
++;
2641 return section
->reloc_count
;
2645 DEFUN (elf_get_symtab
, (abfd
, alocation
),
2647 asymbol
**alocation
)
2650 if (!elf_slurp_symbol_table (abfd
, alocation
))
2653 return (bfd_get_symcount (abfd
));
2657 DEFUN (elf_make_empty_symbol
, (abfd
),
2660 elf_symbol_type
*newsym
;
2662 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2665 bfd_error
= no_memory
;
2670 newsym
-> symbol
.the_bfd
= abfd
;
2671 return (&newsym
-> symbol
);
2676 DEFUN (elf_print_symbol
,(ignore_abfd
, filep
, symbol
, how
),
2677 bfd
*ignore_abfd AND
2680 bfd_print_symbol_type how
)
2682 FILE *file
= (FILE *)filep
;
2685 case bfd_print_symbol_name
:
2686 fprintf(file
, "%s", symbol
->name
);
2688 case bfd_print_symbol_more
:
2689 fprintf(file
, "elf %lx %lx",
2693 case bfd_print_symbol_nm
:
2694 case bfd_print_symbol_all
:
2696 CONST
char *section_name
;
2697 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2698 bfd_print_symbol_vandf((PTR
) file
, symbol
);
2699 fprintf(file
, " %s\t%s",
2709 DEFUN (elf_get_lineno
,(ignore_abfd
, symbol
),
2710 bfd
*ignore_abfd AND
2713 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2720 DEFUN (elf_set_arch_mach
,(abfd
, arch
, machine
),
2722 enum bfd_architecture arch AND
2723 unsigned long machine
)
2725 /* Allow any architecture to be supported by the elf backend */
2728 case bfd_arch_unknown
: /* EM_NONE */
2729 case bfd_arch_sparc
: /* EM_SPARC */
2730 case bfd_arch_i386
: /* EM_386 */
2731 case bfd_arch_m68k
: /* EM_68K */
2732 case bfd_arch_m88k
: /* EM_88K */
2733 case bfd_arch_i860
: /* EM_860 */
2734 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
2735 case bfd_arch_hppa
: /* EM_HPPA (HP PA_RISC) */
2736 return bfd_default_set_arch_mach(abfd
, arch
, machine
);
2743 DEFUN (elf_find_nearest_line
,(abfd
,
2751 asection
*section AND
2752 asymbol
**symbols AND
2754 CONST
char **filename_ptr AND
2755 CONST
char **functionname_ptr AND
2756 unsigned int *line_ptr
)
2762 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
2766 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
2773 DEFUN(elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
2778 bfd_size_type count
)
2782 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
2784 /* do setup calculations (FIXME) */
2785 elf_compute_section_file_positions(abfd
);
2786 abfd
->output_has_begun
= true;
2789 dest_sect
= elf_section_from_bfd_section(abfd
, section
);
2793 if (bfd_seek (abfd
, elf_elfsections(abfd
)[dest_sect
].sh_offset
+ offset
, SEEK_SET
) == -1)
2795 if (bfd_write (location
, 1, count
, abfd
) != count
)
2801 DEFUN (elf_no_info_to_howto
, (abfd
, cache_ptr
, dst
),
2803 arelent
*cache_ptr AND
2804 Elf_Internal_Rela
*dst
)