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)
82 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
83 #include <sys/procfs.h>
85 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
86 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
87 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
90 /* Forward declarations of static functions */
93 elf_read
PARAMS ((bfd
*, long, int));
96 section_from_elf_index
PARAMS ((bfd
*, int));
99 elf_section_from_bfd_section
PARAMS ((bfd
*, struct sec
*));
102 elf_slurp_symbol_table
PARAMS ((bfd
*, asymbol
**));
105 elf_get_str_section
PARAMS ((bfd
*, unsigned int));
107 /* Forward data declarations */
109 extern bfd_target elf_little_vec
, elf_big_vec
;
111 /* Currently the elf_symbol_type struct just contains the generic bfd
119 /* Some private data is stashed away for future use using the tdata pointer
120 in the bfd structure. */
124 Elf_Internal_Ehdr elf_header
[1]; /* Actual data, but ref like ptr */
125 Elf_Internal_Shdr
*elf_sect_ptr
;
126 struct strtab
*strtab_ptr
;
128 void *prstatus
; /* The raw /proc prstatus structure */
129 void *prpsinfo
; /* The raw /proc prpsinfo structure */
132 #define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data)
133 #define elf_elfheader(bfd) (elf_tdata(bfd) -> elf_header)
134 #define elf_elfsections(bfd) (elf_tdata(bfd) -> elf_sect_ptr)
135 #define elf_shstrtab(bfd) (elf_tdata(bfd) -> strtab_ptr)
136 #define elf_onesymtab(bfd) (elf_tdata(bfd) -> symtab_section)
137 #define core_prpsinfo(bfd) (elf_tdata(bfd) -> prpsinfo)
138 #define core_prstatus(bfd) (elf_tdata(bfd) -> prstatus)
140 /* Translate an ELF symbol in external format into an ELF symbol in internal
144 DEFUN(elf_swap_symbol_in
,(abfd
, src
, dst
),
146 Elf_External_Sym
*src AND
147 Elf_Internal_Sym
*dst
)
149 dst
-> st_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_name
);
150 dst
-> st_value
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_value
);
151 dst
-> st_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> st_size
);
152 dst
-> st_info
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
-> st_info
);
153 dst
-> st_other
= bfd_h_get_8 (abfd
, (bfd_byte
*) src
-> st_other
);
154 dst
-> st_shndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> st_shndx
);
157 /* Translate an ELF symbol in internal format into an ELF symbol in external
161 DEFUN(elf_swap_symbol_out
,(abfd
, src
, dst
),
163 Elf_Internal_Sym
*src AND
164 Elf_External_Sym
*dst
)
166 bfd_h_put_32 (abfd
, src
->st_name
, dst
->st_name
);
167 bfd_h_put_32 (abfd
, src
->st_value
, dst
->st_value
);
168 bfd_h_put_32 (abfd
, src
->st_size
, dst
->st_size
);
169 bfd_h_put_8 (abfd
, src
->st_info
, dst
->st_info
);
170 bfd_h_put_8 (abfd
, src
->st_other
, dst
->st_other
);
171 bfd_h_put_16 (abfd
, src
->st_shndx
, dst
->st_shndx
);
175 /* Translate an ELF file header in external format into an ELF file header in
179 DEFUN(elf_swap_ehdr_in
,(abfd
, src
, dst
),
181 Elf_External_Ehdr
*src AND
182 Elf_Internal_Ehdr
*dst
)
184 memcpy (dst
-> e_ident
, src
-> e_ident
, EI_NIDENT
);
185 dst
-> e_type
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_type
);
186 dst
-> e_machine
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_machine
);
187 dst
-> e_version
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_version
);
188 dst
-> e_entry
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_entry
);
189 dst
-> e_phoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_phoff
);
190 dst
-> e_shoff
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_shoff
);
191 dst
-> e_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
-> e_flags
);
192 dst
-> e_ehsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_ehsize
);
193 dst
-> e_phentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phentsize
);
194 dst
-> e_phnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_phnum
);
195 dst
-> e_shentsize
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shentsize
);
196 dst
-> e_shnum
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shnum
);
197 dst
-> e_shstrndx
= bfd_h_get_16 (abfd
, (bfd_byte
*) src
-> e_shstrndx
);
200 /* Translate an ELF file header in internal format into an ELF file header in
204 DEFUN(elf_swap_ehdr_out
,(abfd
, src
, dst
),
206 Elf_Internal_Ehdr
*src AND
207 Elf_External_Ehdr
*dst
)
209 memcpy (dst
-> e_ident
, src
-> e_ident
, EI_NIDENT
);
210 /* note that all elements of dst are *arrays of unsigned char* already... */
211 bfd_h_put_16 (abfd
, src
->e_type
, dst
->e_type
);
212 bfd_h_put_16 (abfd
, src
->e_machine
, dst
->e_machine
);
213 bfd_h_put_32 (abfd
, src
->e_version
, dst
->e_version
);
214 bfd_h_put_32 (abfd
, src
->e_entry
, dst
->e_entry
);
215 bfd_h_put_32 (abfd
, src
->e_phoff
, dst
->e_phoff
);
216 bfd_h_put_32 (abfd
, src
->e_shoff
, dst
->e_shoff
);
217 bfd_h_put_32 (abfd
, src
->e_flags
, dst
->e_flags
);
218 bfd_h_put_16 (abfd
, src
->e_ehsize
, dst
->e_ehsize
);
219 bfd_h_put_16 (abfd
, src
->e_phentsize
, dst
->e_phentsize
);
220 bfd_h_put_16 (abfd
, src
->e_phnum
, dst
->e_phnum
);
221 bfd_h_put_16 (abfd
, src
->e_shentsize
, dst
->e_shentsize
);
222 bfd_h_put_16 (abfd
, src
->e_shnum
, dst
->e_shnum
);
223 bfd_h_put_16 (abfd
, src
->e_shstrndx
, dst
->e_shstrndx
);
227 /* Translate an ELF section header table entry in external format into an
228 ELF section header table entry in internal format. */
231 DEFUN(elf_swap_shdr_in
,(abfd
, src
, dst
),
233 Elf_External_Shdr
*src AND
234 Elf_Internal_Shdr
*dst
)
236 dst
->sh_name
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_name
);
237 dst
->sh_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_type
);
238 dst
->sh_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_flags
);
239 dst
->sh_addr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_addr
);
240 dst
->sh_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_offset
);
241 dst
->sh_size
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_size
);
242 dst
->sh_link
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_link
);
243 dst
->sh_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_info
);
244 dst
->sh_addralign
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_addralign
);
245 dst
->sh_entsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->sh_entsize
);
246 /* we haven't done any processing on it yet, so... */
247 dst
->rawdata
= (void*)0;
250 /* Translate an ELF section header table entry in internal format into an
251 ELF section header table entry in external format. */
254 DEFUN(elf_swap_shdr_out
,(abfd
, src
, dst
),
256 Elf_Internal_Shdr
*src AND
257 Elf_External_Shdr
*dst
)
259 /* note that all elements of dst are *arrays of unsigned char* already... */
260 bfd_h_put_32 (abfd
, src
->sh_name
, dst
->sh_name
);
261 bfd_h_put_32 (abfd
, src
->sh_type
, dst
->sh_type
);
262 bfd_h_put_32 (abfd
, src
->sh_flags
, dst
->sh_flags
);
263 bfd_h_put_32 (abfd
, src
->sh_addr
, dst
->sh_addr
);
264 bfd_h_put_32 (abfd
, src
->sh_offset
, dst
->sh_offset
);
265 bfd_h_put_32 (abfd
, src
->sh_size
, dst
->sh_size
);
266 bfd_h_put_32 (abfd
, src
->sh_link
, dst
->sh_link
);
267 bfd_h_put_32 (abfd
, src
->sh_info
, dst
->sh_info
);
268 bfd_h_put_32 (abfd
, src
->sh_addralign
, dst
->sh_addralign
);
269 bfd_h_put_32 (abfd
, src
->sh_entsize
, dst
->sh_entsize
);
273 /* Translate an ELF program header table entry in external format into an
274 ELF program header table entry in internal format. */
277 DEFUN(elf_swap_phdr_in
,(abfd
, src
, dst
),
279 Elf_External_Phdr
*src AND
280 Elf_Internal_Phdr
*dst
)
282 dst
->p_type
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_type
);
283 dst
->p_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_offset
);
284 dst
->p_vaddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_vaddr
);
285 dst
->p_paddr
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_paddr
);
286 dst
->p_filesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_filesz
);
287 dst
->p_memsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_memsz
);
288 dst
->p_flags
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_flags
);
289 dst
->p_align
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->p_align
);
293 /* Translate an ELF reloc from external format to internal format. */
295 DEFUN(elf_swap_reloc_in
,(abfd
, src
, dst
),
297 Elf_External_Rel
*src AND
298 Elf_Internal_Rel
*dst
)
300 dst
->r_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_offset
);
301 dst
->r_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_info
);
305 DEFUN(elf_swap_reloca_in
,(abfd
, src
, dst
),
307 Elf_External_Rela
*src AND
308 Elf_Internal_Rela
*dst
)
310 dst
->r_offset
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_offset
);
311 dst
->r_info
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_info
);
312 dst
->r_addend
= bfd_h_get_32 (abfd
, (bfd_byte
*) src
->r_addend
);
315 /* Translate an ELF reloc from internal format to external format. */
317 DEFUN(elf_swap_reloc_out
,(abfd
, src
, dst
),
319 Elf_Internal_Rel
*src AND
320 Elf_External_Rel
*dst
)
322 bfd_h_put_32 (abfd
, src
->r_offset
, dst
->r_offset
);
323 bfd_h_put_32 (abfd
, src
->r_info
, dst
->r_info
);
327 DEFUN(elf_swap_reloca_out
,(abfd
, src
, dst
),
329 Elf_Internal_Rela
*src AND
330 Elf_External_Rela
*dst
)
332 bfd_h_put_32 (abfd
, src
->r_offset
, dst
->r_offset
);
333 bfd_h_put_32 (abfd
, src
->r_info
, dst
->r_info
);
334 bfd_h_put_32 (abfd
, src
->r_addend
, dst
->r_addend
);
342 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
345 Helper functions for GDB to locate the string tables.
346 Since BFD hides string tables from callers, GDB needs to use an
347 internal hook to find them. Sun's .stabstr, in particular,
348 isn't even pointed to by the .stab section, so ordinary
349 mechanisms wouldn't work to find it, even if we had some.
352 struct elf_internal_shdr
*
353 DEFUN(bfd_elf_find_section
, (abfd
, name
),
357 Elf_Internal_Shdr
*i_shdrp
;
358 Elf_Internal_Shdr
*gotit
= NULL
;
363 i_shdrp
= elf_elfsections (abfd
);
366 shstrtab
= elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
367 if (shstrtab
!= NULL
)
369 max
= elf_elfheader (abfd
)->e_shnum
;
370 for (i
= 1; i
< max
; i
++)
372 if (!strcmp (&shstrtab
[i_shdrp
[i
].sh_name
], name
))
382 /* End of GDB support. */
385 DEFUN(elf_get_str_section
, (abfd
, shindex
),
387 unsigned int shindex
)
389 Elf_Internal_Shdr
*i_shdrp
;
390 char *shstrtab
= NULL
;
392 unsigned int shstrtabsize
;
394 i_shdrp
= elf_elfsections (abfd
);
397 shstrtab
= i_shdrp
[shindex
].rawdata
;
398 if (shstrtab
== NULL
)
400 /* No cached one, attempt to read, and cache what we read. */
401 offset
= i_shdrp
[shindex
].sh_offset
;
402 shstrtabsize
= i_shdrp
[shindex
].sh_size
;
403 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
404 i_shdrp
[shindex
].rawdata
= (void*) shstrtab
;
411 DEFUN(elf_string_from_elf_section
, (abfd
, shindex
, strindex
),
413 unsigned int shindex AND
414 unsigned int strindex
)
416 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
417 Elf_Internal_Shdr
*hdr
= i_shdrp
+ shindex
;
421 if (elf_get_str_section (abfd
, shindex
) == NULL
)
426 return ((char*)hdr
->rawdata
)+strindex
;
429 #define elf_string_from_elf_strtab(abfd, strindex) \
430 elf_string_from_elf_section (abfd, elf_elfheader(abfd)->e_shstrndx, strindex)
432 /* Create a new bfd section from an ELF section header. */
435 DEFUN(bfd_section_from_shdr
, (abfd
, shindex
),
437 unsigned int shindex
)
439 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
440 Elf_Internal_Shdr
*hdr
= i_shdrp
+ shindex
;
444 name
= hdr
->sh_name
?
445 elf_string_from_elf_strtab (abfd
, hdr
->sh_name
) : "unnamed";
447 switch(hdr
->sh_type
) {
450 /* inactive section. Throw it away. */
455 /* Bits that get saved. This one is real. */
458 newsect
= bfd_make_section (abfd
, name
);
459 newsect
->vma
= hdr
->sh_addr
;
460 newsect
->_raw_size
= hdr
->sh_size
;
461 newsect
->filepos
= hdr
->sh_offset
; /* so we can read back the bits */
462 newsect
->flags
|= SEC_HAS_CONTENTS
;
464 if (hdr
->sh_flags
& SHF_ALLOC
)
466 newsect
->flags
|= SEC_ALLOC
;
467 if (hdr
->sh_type
!= SHT_NOBITS
)
468 newsect
->flags
|= SEC_LOAD
;
471 if (!(hdr
->sh_flags
& SHF_WRITE
))
472 newsect
->flags
|= SEC_READONLY
;
474 if (hdr
->sh_flags
& SHF_EXECINSTR
)
475 newsect
->flags
|= SEC_CODE
; /* FIXME: may only contain SOME code */
477 newsect
->flags
|= SEC_DATA
;
479 hdr
->rawdata
= (void*)newsect
;
484 case SHT_SYMTAB
: /* A symbol table */
485 BFD_ASSERT (hdr
->sh_entsize
== sizeof (Elf_External_Sym
));
486 elf_onesymtab (abfd
) = shindex
;
487 abfd
->flags
|= HAS_SYMS
;
490 case SHT_STRTAB
: /* A string table */
495 /* *these* do a lot of work -- but build no sections! */
496 /* the spec says there can be multiple strtabs, but only one symtab */
497 /* but there can be lots of REL* sections. */
498 /* FIXME: The above statement is wrong! There are typically at least
499 two symbol tables in a dynamically linked executable, ".dynsym"
500 which is the dynamic linkage symbol table and ".symtab", which is
501 the "traditional" symbol table. -fnf */
504 asection
*target_sect
;
506 bfd_section_from_shdr (abfd
, hdr
->sh_link
); /* symbol table */
507 bfd_section_from_shdr (abfd
, hdr
->sh_info
); /* target */
508 target_sect
= section_from_elf_index (abfd
, hdr
->sh_info
);
509 if (target_sect
== NULL
)
513 /* FIXME: We are only prepared to read one symbol table, so
514 do NOT read the dynamic symbol table since it is only a
515 subset of the full symbol table. Also see comment above. -fnf */
516 if (!elf_slurp_symbol_table(abfd
, i_shdrp
+ hdr
->sh_link
))
520 target_sect
->reloc_count
= hdr
->sh_size
/ hdr
->sh_entsize
;
521 target_sect
->flags
|= SEC_RELOC
;
522 target_sect
->relocation
= 0;
523 target_sect
->rel_filepos
= hdr
->sh_offset
;
530 case SHT_DYNSYM
: /* could treat this like symtab... */
532 fprintf(stderr
, "Dynamic Linking sections not yet supported.\n");
539 fprintf(stderr
, "Note Sections not yet supported.\n");
546 fprintf(stderr
, "SHLIB Sections not supported (and non conforming.)\n");
567 static struct strtab
*
568 DEFUN(bfd_new_strtab
, (abfd
),
573 ss
= (struct strtab
*) bfd_xmalloc(sizeof(struct strtab
));
574 ss
->tab
= bfd_xmalloc(1);
575 BFD_ASSERT(ss
->tab
!= 0);
584 DEFUN(bfd_add_to_strtab
, (abfd
, ss
, str
),
586 struct strtab
*ss AND
589 /* should search first, but for now: */
590 /* include the trailing NUL */
591 int ln
= strlen(str
)+1;
593 /* should this be using obstacks? */
594 ss
->tab
= realloc(ss
->tab
, ss
->length
+ ln
);
596 BFD_ASSERT(ss
->tab
!= 0);
597 strcpy(ss
->tab
+ ss
->length
, str
);
601 return ss
->length
- ln
;
605 DEFUN(bfd_add_2_to_strtab
, (abfd
, ss
, str
, str2
),
607 struct strtab
*ss AND
611 /* should search first, but for now: */
612 /* include the trailing NUL */
613 int ln
= strlen(str
)+strlen(str2
)+1;
615 /* should this be using obstacks? */
617 ss
->tab
= realloc(ss
->tab
, ss
->length
+ ln
);
619 ss
->tab
= bfd_xmalloc(ln
);
621 BFD_ASSERT(ss
->tab
!= 0);
622 strcpy(ss
->tab
+ ss
->length
, str
);
623 strcpy(ss
->tab
+ ss
->length
+ strlen(str
), str2
);
627 return ss
->length
- ln
;
630 /* Create a new ELF section from a bfd section. */
633 DEFUN(bfd_shdr_from_section
, (abfd
, hdr
, shstrtab
, indx
),
635 Elf_Internal_Shdr
*hdr AND
636 struct strtab
*shstrtab AND
642 /* figure out out to write the section name from the bfd section name. MWE */
644 sect
= abfd
->sections
;
645 for (ndx
= indx
; --ndx
; )
649 hdr
[indx
].sh_name
= bfd_add_to_strtab(abfd
, shstrtab
,
650 bfd_section_name(abfd
, sect
));
651 hdr
[indx
].sh_addr
= sect
->vma
;
652 hdr
[indx
].sh_size
= sect
->_raw_size
;
653 hdr
[indx
].sh_flags
= 0;
654 /* these need to be preserved on */
655 hdr
[indx
].sh_link
= 0;
656 hdr
[indx
].sh_info
= 0;
657 hdr
[indx
].sh_addralign
= 0;
658 hdr
[indx
].sh_entsize
= 0;
660 hdr
[indx
].sh_type
= 0;
661 if (sect
->flags
& SEC_RELOC
) {
662 hdr
[indx
].sh_type
= SHT_RELA
; /* FIXME -- sparc specific */
665 if (sect
->flags
& SEC_HAS_CONTENTS
)
667 hdr
[indx
].sh_offset
= sect
->filepos
;
668 hdr
[indx
].sh_size
= sect
->_raw_size
;
670 if (sect
->flags
& SEC_ALLOC
)
672 hdr
[indx
].sh_flags
|= SHF_ALLOC
;
673 if (sect
->flags
& SEC_LOAD
)
675 /* do something with sh_type ? */
678 if (!(sect
->flags
& SEC_READONLY
))
679 hdr
[indx
].sh_flags
|= SHF_WRITE
;
681 if (sect
->flags
& SEC_CODE
)
682 hdr
[indx
].sh_flags
|= SHF_EXECINSTR
;
687 /* Create a new bfd section from an ELF program header.
689 Since program segments have no names, we generate a synthetic name
690 of the form segment<NUM>, where NUM is generally the index in the
691 program header table. For segments that are split (see below) we
692 generate the names segment<NUM>a and segment<NUM>b.
694 Note that some program segments may have a file size that is different than
695 (less than) the memory size. All this means is that at execution the
696 system must allocate the amount of memory specified by the memory size,
697 but only initialize it with the first "file size" bytes read from the
698 file. This would occur for example, with program segments consisting
699 of combined data+bss.
701 To handle the above situation, this routine generates TWO bfd sections
702 for the single program segment. The first has the length specified by
703 the file size of the segment, and the second has the length specified
704 by the difference between the two sizes. In effect, the segment is split
705 into it's initialized and uninitialized parts.
710 DEFUN(bfd_section_from_phdr
, (abfd
, hdr
, index
),
712 Elf_Internal_Phdr
*hdr AND
720 split
= ((hdr
-> p_memsz
> 0) &&
721 (hdr
-> p_filesz
> 0) &&
722 (hdr
-> p_memsz
> hdr
-> p_filesz
));
723 sprintf (namebuf
, split
? "segment%da" : "segment%d", index
);
724 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
725 strcpy (name
, namebuf
);
726 newsect
= bfd_make_section (abfd
, name
);
727 newsect
-> vma
= hdr
-> p_vaddr
;
728 newsect
-> _raw_size
= hdr
-> p_filesz
;
729 newsect
-> filepos
= hdr
-> p_offset
;
730 newsect
-> flags
|= SEC_HAS_CONTENTS
;
731 if (hdr
-> p_type
== PT_LOAD
)
733 newsect
-> flags
|= SEC_ALLOC
;
734 newsect
-> flags
|= SEC_LOAD
;
735 if (hdr
-> p_flags
& PF_X
)
737 /* FIXME: all we known is that it has execute PERMISSION,
739 newsect
-> flags
|= SEC_CODE
;
742 if (!(hdr
-> p_flags
& PF_W
))
744 newsect
-> flags
|= SEC_READONLY
;
749 sprintf (namebuf
, "segment%db", index
);
750 name
= bfd_alloc (abfd
, strlen (namebuf
) + 1);
751 strcpy (name
, namebuf
);
752 newsect
= bfd_make_section (abfd
, name
);
753 newsect
-> vma
= hdr
-> p_vaddr
+ hdr
-> p_filesz
;
754 newsect
-> _raw_size
= hdr
-> p_memsz
- hdr
-> p_filesz
;
755 if (hdr
-> p_type
== PT_LOAD
)
757 newsect
-> flags
|= SEC_ALLOC
;
758 if (hdr
-> p_flags
& PF_X
)
759 newsect
-> flags
|= SEC_CODE
;
761 if (!(hdr
-> p_flags
& PF_W
))
762 newsect
-> flags
|= SEC_READONLY
;
771 DEFUN(bfd_prstatus
,(abfd
, descdata
, descsz
, filepos
),
778 prstatus_t
*status
= (prstatus_t
*)0;
780 if (descsz
== sizeof (prstatus_t
))
782 newsect
= bfd_make_section (abfd
, ".reg");
783 newsect
-> _raw_size
= sizeof (status
->pr_reg
);
784 newsect
-> filepos
= filepos
+ (long) &status
->pr_reg
;
785 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
786 newsect
-> alignment_power
= 2;
787 if ((core_prstatus (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
789 memcpy (core_prstatus (abfd
), descdata
, descsz
);
794 /* Stash a copy of the prpsinfo structure away for future use. */
797 DEFUN(bfd_prpsinfo
,(abfd
, descdata
, descsz
, filepos
),
805 if (descsz
== sizeof (prpsinfo_t
))
807 if ((core_prpsinfo (abfd
) = bfd_alloc (abfd
, descsz
)) != NULL
)
809 memcpy (core_prpsinfo (abfd
), descdata
, descsz
);
815 DEFUN(bfd_fpregset
,(abfd
, descdata
, descsz
, filepos
),
823 newsect
= bfd_make_section (abfd
, ".reg2");
824 newsect
-> _raw_size
= descsz
;
825 newsect
-> filepos
= filepos
;
826 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
827 newsect
-> alignment_power
= 2;
830 #endif /* HAVE_PROCFS */
832 /* Return a pointer to the args (including the command name) that were
833 seen by the program that generated the core dump. Note that for
834 some reason, a spurious space is tacked onto the end of the args
835 in some (at least one anyway) implementations, so strip it off if
839 DEFUN(elf_core_file_failing_command
, (abfd
),
843 if (core_prpsinfo (abfd
))
845 prpsinfo_t
*p
= core_prpsinfo (abfd
);
846 char *scan
= p
-> pr_psargs
;
849 if ((scan
> p
-> pr_psargs
) && (*scan
== ' '))
853 return (p
-> pr_psargs
);
859 /* Return the number of the signal that caused the core dump. Presumably,
860 since we have a core file, we got a signal of some kind, so don't bother
861 checking the other process status fields, just return the signal number.
865 DEFUN(elf_core_file_failing_signal
, (abfd
),
869 if (core_prstatus (abfd
))
871 return (((prstatus_t
*)(core_prstatus (abfd
))) -> pr_cursig
);
877 /* Check to see if the core file could reasonably be expected to have
878 come for the current executable file. Note that by default we return
879 true unless we find something that indicates that there might be a
884 DEFUN(elf_core_file_matches_executable_p
, (core_bfd
, exec_bfd
),
893 /* First, xvecs must match since both are ELF files for the same target. */
895 if (core_bfd
->xvec
!= exec_bfd
->xvec
)
897 bfd_error
= system_call_error
;
903 /* If no prpsinfo, just return true. Otherwise, grab the last component
904 of the exec'd pathname from the prpsinfo. */
906 if (core_prpsinfo (core_bfd
))
908 corename
= (((struct prpsinfo
*) core_prpsinfo (core_bfd
)) -> pr_fname
);
915 /* Find the last component of the executable pathname. */
917 if ((execname
= strrchr (exec_bfd
-> filename
, '/')) != NULL
)
923 execname
= (char *) exec_bfd
-> filename
;
926 /* See if they match */
928 return (strcmp (execname
, corename
) ? false : true);
934 #endif /* HAVE_PROCFS */
937 /* ELF core files contain a segment of type PT_NOTE, that holds much of
938 the information that would normally be available from the /proc interface
939 for the process, at the time the process dumped core. Currently this
940 includes copies of the prstatus, prpsinfo, and fpregset structures.
942 Since these structures are potentially machine dependent in size and
943 ordering, bfd provides two levels of support for them. The first level,
944 available on all machines since it does not require that the host
945 have /proc support or the relevant include files, is to create a bfd
946 section for each of the prstatus, prpsinfo, and fpregset structures,
947 without any interpretation of their contents. With just this support,
948 the bfd client will have to interpret the structures itself. Even with
949 /proc support, it might want these full structures for it's own reasons.
951 In the second level of support, where HAVE_PROCFS is defined, bfd will
952 pick apart the structures to gather some additional information that
953 clients may want, such as the general register set, the name of the
954 exec'ed file and its arguments, the signal (if any) that caused the
960 DEFUN(elf_corefile_note
, (abfd
, hdr
),
962 Elf_Internal_Phdr
*hdr
)
964 Elf_External_Note
*x_note_p
; /* Elf note, external form */
965 Elf_Internal_Note i_note
; /* Elf note, internal form */
966 char *buf
= NULL
; /* Entire note segment contents */
967 char *namedata
; /* Name portion of the note */
968 char *descdata
; /* Descriptor portion of the note */
969 char *sectname
; /* Name to use for new section */
970 long filepos
; /* File offset to descriptor data */
973 if (hdr
-> p_filesz
> 0
974 && (buf
= (char *) bfd_xmalloc (hdr
-> p_filesz
)) != NULL
975 && bfd_seek (abfd
, hdr
-> p_offset
, SEEK_SET
) != -1
976 && bfd_read ((PTR
) buf
, hdr
-> p_filesz
, 1, abfd
) == hdr
-> p_filesz
)
978 x_note_p
= (Elf_External_Note
*) buf
;
979 while ((char *) x_note_p
< (buf
+ hdr
-> p_filesz
))
981 i_note
.namesz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> namesz
);
982 i_note
.descsz
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> descsz
);
983 i_note
.type
= bfd_h_get_32 (abfd
, (bfd_byte
*) x_note_p
-> type
);
984 namedata
= x_note_p
-> name
;
985 descdata
= namedata
+ BFD_ALIGN (i_note
.namesz
, 4);
986 filepos
= hdr
-> p_offset
+ (descdata
- buf
);
987 switch (i_note
.type
) {
989 /* process descdata as prstatus info */
990 bfd_prstatus (abfd
, descdata
, i_note
.descsz
, filepos
);
991 sectname
= ".prstatus";
994 /* process descdata as fpregset info */
995 bfd_fpregset (abfd
, descdata
, i_note
.descsz
, filepos
);
996 sectname
= ".fpregset";
999 /* process descdata as prpsinfo */
1000 bfd_prpsinfo (abfd
, descdata
, i_note
.descsz
, filepos
);
1001 sectname
= ".prpsinfo";
1004 /* Unknown descriptor, just ignore it. */
1008 if (sectname
!= NULL
)
1010 newsect
= bfd_make_section (abfd
, sectname
);
1011 newsect
-> _raw_size
= i_note
.descsz
;
1012 newsect
-> filepos
= filepos
;
1013 newsect
-> flags
= SEC_ALLOC
| SEC_HAS_CONTENTS
;
1014 newsect
-> alignment_power
= 2;
1016 x_note_p
= (Elf_External_Note
*)
1017 (descdata
+ BFD_ALIGN (i_note
.descsz
, 4));
1029 /* Read a specified number of bytes at a specified offset in an ELF
1030 file, into a newly allocated buffer, and return a pointer to the
1034 DEFUN(elf_read
, (abfd
, offset
, size
),
1041 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
1043 bfd_error
= no_memory
;
1046 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
1048 bfd_error
= system_call_error
;
1051 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
1053 bfd_error
= system_call_error
;
1059 /* Begin processing a given object.
1061 First we validate the file by reading in the ELF header and checking
1067 DEFUN (elf_object_p
, (abfd
), bfd
*abfd
)
1069 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1070 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1071 Elf_External_Shdr x_shdr
; /* Section header table entry, external form */
1072 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1074 char *shstrtab
; /* Internal copy of section header stringtab */
1076 /* Read in the ELF header in external format. */
1078 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
1080 bfd_error
= system_call_error
;
1084 /* Now check to see if we have a valid ELF file, and one that BFD can
1085 make use of. The magic number must match, the address size ('class')
1086 and byte-swapping must match our XVEC entry, and it must have a
1087 section header table (FIXME: See comments re sections at top of this
1090 if (x_ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
1091 x_ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
1092 x_ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
1093 x_ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
1096 bfd_error
= wrong_format
;
1100 /* FIXME, Check EI_VERSION here ! */
1102 switch (x_ehdr
.e_ident
[EI_CLASS
])
1104 case ELFCLASSNONE
: /* address size not specified */
1105 goto wrong
; /* No support if can't tell address size */
1106 case ELFCLASS32
: /* 32-bit addresses */
1108 case ELFCLASS64
: /* 64-bit addresses */
1109 goto wrong
; /* FIXME: 64 bits not yet supported */
1111 goto wrong
; /* No support if unknown address class */
1114 /* Switch xvec to match the specified byte order. */
1115 switch (x_ehdr
.e_ident
[EI_DATA
])
1117 case ELFDATA2MSB
: /* Big-endian */
1118 if (!abfd
->xvec
->header_byteorder_big_p
)
1121 case ELFDATA2LSB
: /* Little-endian */
1122 if (abfd
->xvec
->header_byteorder_big_p
)
1125 case ELFDATANONE
: /* No data encoding specified */
1126 default: /* Unknown data encoding specified */
1130 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1131 the tdata pointer in the bfd. */
1133 if (NULL
== (elf_tdata (abfd
) = (struct elf_obj_tdata
*)
1134 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
))))
1136 bfd_error
= no_memory
;
1140 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
1142 /* Now that we know the byte order, swap in the rest of the header */
1143 i_ehdrp
= elf_elfheader (abfd
);
1144 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
1146 /* If there is no section header table, we're hosed. */
1147 if (i_ehdrp
->e_shoff
== 0)
1150 if (i_ehdrp
->e_type
== ET_EXEC
|| i_ehdrp
->e_type
== ET_DYN
)
1151 abfd
-> flags
|= EXEC_P
;
1153 switch (i_ehdrp
->e_machine
)
1156 case EM_M32
: /* or should this be bfd_arch_obscure? */
1157 bfd_default_set_arch_mach(abfd
, bfd_arch_unknown
, 0);
1160 bfd_default_set_arch_mach(abfd
, bfd_arch_sparc
, 0);
1163 bfd_default_set_arch_mach(abfd
, bfd_arch_i386
, 0);
1166 bfd_default_set_arch_mach(abfd
, bfd_arch_m68k
, 0);
1169 bfd_default_set_arch_mach(abfd
, bfd_arch_m88k
, 0);
1172 bfd_default_set_arch_mach(abfd
, bfd_arch_i860
, 0);
1175 bfd_default_set_arch_mach(abfd
, bfd_arch_mips
, 0);
1181 /* Allocate space for a copy of the section header table in
1182 internal form, seek to the section header table in the file,
1183 read it in, and convert it to internal form. As a simple sanity
1184 check, verify that the what BFD thinks is the size of each section
1185 header table entry actually matches the size recorded in the file. */
1187 if (i_ehdrp
->e_shentsize
!= sizeof (x_shdr
))
1189 i_shdrp
= (Elf_Internal_Shdr
*)
1190 bfd_alloc (abfd
, sizeof (*i_shdrp
) * i_ehdrp
->e_shnum
);
1193 bfd_error
= no_memory
;
1196 if (bfd_seek (abfd
, i_ehdrp
->e_shoff
, SEEK_SET
) == -1)
1198 bfd_error
= system_call_error
;
1201 for (shindex
= 0; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1203 if (bfd_read ((PTR
) &x_shdr
, sizeof x_shdr
, 1, abfd
)
1206 bfd_error
= system_call_error
;
1209 elf_swap_shdr_in (abfd
, &x_shdr
, i_shdrp
+ shindex
);
1212 elf_elfsections (abfd
) = i_shdrp
;
1214 /* Read in the string table containing the names of the sections. We
1215 will need the base pointer to this table later. */
1216 /* We read this inline now, so that we don't have to go through
1217 bfd_section_from_shdr with it (since this particular strtab is
1218 used to find all of the ELF section names.) */
1220 shstrtab
= elf_get_str_section (abfd
, i_ehdrp
->e_shstrndx
);
1224 /* Once all of the section headers have been read and converted, we
1225 can start processing them. Note that the first section header is
1226 a dummy placeholder entry, so we ignore it.
1228 We also watch for the symbol table section and remember the file
1229 offset and section size for both the symbol table section and the
1230 associated string table section. */
1232 for (shindex
= 1; shindex
< i_ehdrp
->e_shnum
; shindex
++)
1234 bfd_section_from_shdr (abfd
, shindex
);
1237 /* Remember the entry point specified in the ELF file header. */
1239 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1241 return (abfd
->xvec
);
1244 /* Core files are simply standard ELF formatted files that partition
1245 the file using the execution view of the file (program header table)
1246 rather than the linking view. In fact, there is no section header
1247 table in a core file.
1249 The process status information (including the contents of the general
1250 register set) and the floating point register set are stored in a
1251 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
1252 that allow standard bfd access to the general registers (.reg) and the
1253 floating point registers (.reg2).
1258 DEFUN (elf_core_file_p
, (abfd
), bfd
*abfd
)
1260 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1261 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1262 Elf_External_Phdr x_phdr
; /* Program header table entry, external form */
1263 Elf_Internal_Phdr
*i_phdrp
; /* Program header table, internal form */
1264 unsigned int phindex
;
1266 /* Read in the ELF header in external format. */
1268 if (bfd_read ((PTR
) &x_ehdr
, sizeof (x_ehdr
), 1, abfd
) != sizeof (x_ehdr
))
1270 bfd_error
= system_call_error
;
1274 /* Now check to see if we have a valid ELF file, and one that BFD can
1275 make use of. The magic number must match, the address size ('class')
1276 and byte-swapping must match our XVEC entry, and it must have a
1277 program header table (FIXME: See comments re segments at top of this
1280 if (x_ehdr
.e_ident
[EI_MAG0
] != ELFMAG0
||
1281 x_ehdr
.e_ident
[EI_MAG1
] != ELFMAG1
||
1282 x_ehdr
.e_ident
[EI_MAG2
] != ELFMAG2
||
1283 x_ehdr
.e_ident
[EI_MAG3
] != ELFMAG3
)
1286 bfd_error
= wrong_format
;
1290 /* FIXME, Check EI_VERSION here ! */
1292 switch (x_ehdr
.e_ident
[EI_CLASS
])
1294 case ELFCLASSNONE
: /* address size not specified */
1295 goto wrong
; /* No support if can't tell address size */
1296 case ELFCLASS32
: /* 32-bit addresses */
1298 case ELFCLASS64
: /* 64-bit addresses */
1299 goto wrong
; /* FIXME: 64 bits not yet supported */
1301 goto wrong
; /* No support if unknown address class */
1304 /* Switch xvec to match the specified byte order. */
1305 switch (x_ehdr
.e_ident
[EI_DATA
])
1307 case ELFDATA2MSB
: /* Big-endian */
1308 if (abfd
->xvec
->byteorder_big_p
== false)
1311 case ELFDATA2LSB
: /* Little-endian */
1312 if (abfd
->xvec
->byteorder_big_p
== true)
1315 case ELFDATANONE
: /* No data encoding specified */
1316 default: /* Unknown data encoding specified */
1320 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1321 the tdata pointer in the bfd. */
1324 (struct elf_obj_tdata
*) bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
1325 if (elf_tdata (abfd
) == NULL
)
1327 bfd_error
= no_memory
;
1331 /* FIXME, `wrong' returns from this point onward, leak memory. */
1333 /* Now that we know the byte order, swap in the rest of the header */
1334 i_ehdrp
= elf_elfheader (abfd
);
1335 elf_swap_ehdr_in (abfd
, &x_ehdr
, i_ehdrp
);
1337 /* If there is no program header, or the type is not a core file, then
1339 if (i_ehdrp
->e_phoff
== 0 || i_ehdrp
->e_type
!= ET_CORE
)
1342 /* Allocate space for a copy of the program header table in
1343 internal form, seek to the program header table in the file,
1344 read it in, and convert it to internal form. As a simple sanity
1345 check, verify that the what BFD thinks is the size of each program
1346 header table entry actually matches the size recorded in the file. */
1348 if (i_ehdrp
->e_phentsize
!= sizeof (x_phdr
))
1350 i_phdrp
= (Elf_Internal_Phdr
*)
1351 bfd_alloc (abfd
, sizeof (*i_phdrp
) * i_ehdrp
->e_phnum
);
1354 bfd_error
= no_memory
;
1357 if (bfd_seek (abfd
, i_ehdrp
->e_phoff
, SEEK_SET
) == -1)
1359 bfd_error
= system_call_error
;
1362 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
1364 if (bfd_read ((PTR
) &x_phdr
, sizeof (x_phdr
), 1, abfd
)
1367 bfd_error
= system_call_error
;
1370 elf_swap_phdr_in (abfd
, &x_phdr
, i_phdrp
+ phindex
);
1373 /* Once all of the program headers have been read and converted, we
1374 can start processing them. */
1376 for (phindex
= 0; phindex
< i_ehdrp
->e_phnum
; phindex
++)
1378 bfd_section_from_phdr (abfd
, i_phdrp
+ phindex
, phindex
);
1379 if ((i_phdrp
+ phindex
) -> p_type
== PT_NOTE
)
1381 elf_corefile_note (abfd
, i_phdrp
+ phindex
);
1385 /* Remember the entry point specified in the ELF file header. */
1387 bfd_get_start_address (abfd
) = i_ehdrp
->e_entry
;
1389 return (abfd
->xvec
);
1393 DEFUN (elf_mkobject
, (abfd
), bfd
*abfd
)
1395 /* this just does initialization */
1396 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
1397 elf_tdata(abfd
) = (struct elf_obj_tdata
*)
1398 bfd_zalloc (abfd
, sizeof(struct elf_obj_tdata
));
1399 if (elf_tdata(abfd
) == 0) {
1400 bfd_error
= no_memory
;
1403 /* since everything is done at close time, do we need any
1410 Create ELF output from BFD sections.
1412 Essentially, just create the section header and forget about the program
1417 /* lacking nested functions and nested types, set up for mapping over
1418 BFD sections to produce ELF sections */
1421 Elf_Internal_Ehdr
*i_ehdr
;
1422 Elf_Internal_Shdr
*i_shdrp
;
1423 struct strtab
*shstrtab
;
1430 DEFUN (elf_make_sections
, (abfd
, asect
, obj
),
1435 elf_sect_thunk
*thunk
= (elf_sect_thunk
*)obj
;
1436 /* most of what is in bfd_shdr_from_section goes in here... */
1437 /* and all of these sections generate at *least* one ELF section. */
1441 /* check if we're making a PROGBITS section... */
1442 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1443 /* this was too strict... what *do* we want to check here? */
1446 Elf_Internal_Shdr
*this_hdr
;
1447 this_section
= elf_section_from_bfd_section (abfd
, asect
);
1448 this_hdr
= &thunk
->i_shdrp
[this_section
];
1450 this_hdr
->sh_addr
= asect
->vma
;
1451 this_hdr
->sh_size
= asect
->_raw_size
;
1452 /* contents already set by elf_set_section_contents */
1454 if (asect
->flags
& SEC_RELOC
)
1456 /* emit a reloc section, and thus strtab and symtab... */
1457 Elf_Internal_Shdr
*rela_hdr
;
1458 Elf_Internal_Shdr
*symtab_hdr
;
1459 Elf_External_Rela
*outbound_relocs
;
1462 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1464 if (thunk
->symtab_section
== this_section
+ 1)
1465 rela_section
= thunk
->symtab_section
+ 2; /* symtab + symstrtab */
1467 rela_section
= this_section
+ 1;
1468 rela_hdr
= &thunk
->i_shdrp
[rela_section
];
1469 rela_hdr
->sh_type
= SHT_RELA
;
1470 rela_hdr
->sh_link
= thunk
->symtab_section
;
1471 rela_hdr
->sh_info
= this_section
;
1472 rela_hdr
->sh_entsize
= sizeof (Elf_External_Rela
);
1473 /* orelocation has the data, reloc_count has the count... */
1474 rela_hdr
->sh_size
= rela_hdr
->sh_entsize
* asect
->reloc_count
;
1475 outbound_relocs
= (Elf_External_Rela
*)
1476 bfd_alloc(abfd
, asect
->reloc_count
* sizeof(Elf_External_Rela
));
1477 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
1479 Elf_Internal_Rela dst
;
1481 Elf_External_Rela
*src
;
1483 ptr
= asect
->orelocation
[idx
];
1484 src
= outbound_relocs
+ idx
;
1485 if (asect
->flags
& SEC_RELOC
)
1486 dst
.r_offset
= ptr
->address
- asect
->vma
;
1488 dst
.r_offset
= ptr
->address
;
1490 dst
.r_info
= ELF_R_INFO(1 /*ptr->sym_ptr_ptr*/, /* needs index into symtab (FIXME) */
1493 dst
.r_addend
= ptr
->addend
;
1494 elf_swap_reloca_out(abfd
, &dst
, src
);
1496 rela_hdr
->contents
= (void*)outbound_relocs
;
1502 DEFUN (elf_fake_sections
, (abfd
, asect
, obj
),
1507 elf_sect_thunk
*thunk
= (elf_sect_thunk
*)obj
;
1508 /* most of what is in bfd_shdr_from_section goes in here... */
1509 /* and all of these sections generate at *least* one ELF section. */
1512 /* check if we're making a PROGBITS section... */
1513 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1514 /* this was too strict... what *do* we want to check here? */
1517 Elf_Internal_Shdr
*this_hdr
;
1518 this_section
= thunk
->i_ehdr
->e_shnum
++;
1519 this_hdr
= &thunk
->i_shdrp
[this_section
];
1521 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, asect
->name
);
1522 /* we need to log the type *now* so that elf_section_from_bfd_section
1523 can find us... have to set rawdata too. */
1524 this_hdr
->rawdata
= (void*)asect
;
1525 if ((asect
->flags
& SEC_ALLOC
) && (asect
->flags
& SEC_LOAD
))
1526 this_hdr
->sh_type
= SHT_PROGBITS
;
1528 /* what *do* we put here? */
1529 this_hdr
->sh_type
= SHT_PROGBITS
;
1532 if (asect
->flags
& SEC_RELOC
)
1534 /* emit a reloc section, and thus strtab and symtab... */
1535 Elf_Internal_Shdr
*rela_hdr
;
1536 Elf_Internal_Shdr
*symtab_hdr
;
1537 Elf_Internal_Shdr
*symstrtab_hdr
;
1539 int symstrtab_section
;
1541 /* note that only one symtab is used, so just remember it
1543 if (! thunk
->symtab_section
)
1545 thunk
->symtab_section
= thunk
->i_ehdr
->e_shnum
++;
1546 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1547 symtab_hdr
->sh_name
=
1548 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, ".symtab");
1549 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1550 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1552 symstrtab_section
= thunk
->i_ehdr
->e_shnum
++;
1553 BFD_ASSERT(symstrtab_section
== thunk
->symtab_section
+1);
1554 symstrtab_hdr
= &thunk
->i_shdrp
[symstrtab_section
];
1555 symtab_hdr
->sh_link
= symstrtab_section
;
1556 symstrtab_hdr
->sh_name
=
1557 bfd_add_to_strtab (abfd
, thunk
->shstrtab
, ".strtab");
1558 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1560 symtab_hdr
->contents
= 0;
1561 symstrtab_hdr
->contents
= 0;
1562 symstrtab_hdr
->sh_size
= 0;
1565 symtab_hdr
= &thunk
->i_shdrp
[thunk
->symtab_section
];
1567 rela_section
= thunk
->i_ehdr
->e_shnum
++;
1568 rela_hdr
= &thunk
->i_shdrp
[rela_section
];
1570 bfd_add_2_to_strtab (abfd
, thunk
->shstrtab
, ".rela", asect
->name
);
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
);
1581 DEFUN (elf_compute_section_file_positions
, (abfd
), bfd
*abfd
)
1583 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1584 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1585 struct strtab
*shstrtab
;
1586 int count
, maxsections
;
1589 if (! elf_shstrtab (abfd
)) {
1590 i_ehdrp
= elf_elfheader (abfd
); /* build new header in tdata memory */
1591 shstrtab
= bfd_new_strtab(abfd
);
1593 i_ehdrp
->e_ident
[EI_MAG0
] = ELFMAG0
;
1594 i_ehdrp
->e_ident
[EI_MAG1
] = ELFMAG1
;
1595 i_ehdrp
->e_ident
[EI_MAG2
] = ELFMAG2
;
1596 i_ehdrp
->e_ident
[EI_MAG3
] = ELFMAG3
;
1598 i_ehdrp
->e_ident
[EI_CLASS
] = ELFCLASS32
; /* FIXME: find out from bfd */
1599 i_ehdrp
->e_ident
[EI_DATA
] =
1600 abfd
->xvec
->byteorder_big_p
? ELFDATA2MSB
: ELFDATA2LSB
;
1601 i_ehdrp
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1603 for(count
= EI_PAD
; count
< EI_NIDENT
; count
++)
1604 i_ehdrp
->e_ident
[count
] = 0;
1606 i_ehdrp
->e_type
= (abfd
->flags
& EXEC_P
)? ET_EXEC
: ET_REL
;
1607 switch(bfd_get_arch(abfd
))
1609 case bfd_arch_unknown
:
1610 i_ehdrp
->e_machine
= EM_NONE
;
1612 case bfd_arch_sparc
:
1613 i_ehdrp
->e_machine
= EM_SPARC
;
1616 i_ehdrp
->e_machine
= EM_386
;
1619 i_ehdrp
->e_machine
= EM_68K
;
1622 i_ehdrp
->e_machine
= EM_88K
;
1625 i_ehdrp
->e_machine
= EM_860
;
1627 case bfd_arch_mips
: /* MIPS Rxxxx */
1628 i_ehdrp
->e_machine
= EM_MIPS
; /* only MIPS R3000 */
1630 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1632 i_ehdrp
->e_machine
= EM_NONE
;
1634 i_ehdrp
->e_version
= EV_CURRENT
;
1635 i_ehdrp
->e_ehsize
= sizeof(Elf_External_Ehdr
);
1637 /* no program header, for now. */
1638 i_ehdrp
->e_phoff
= 0;
1639 i_ehdrp
->e_phentsize
= 0;
1640 i_ehdrp
->e_phnum
= 0;
1642 /* each bfd section is section header entry */
1643 i_ehdrp
->e_entry
= bfd_get_start_address (abfd
);
1644 i_ehdrp
->e_shentsize
= sizeof (Elf_External_Shdr
);
1646 /* figure at most each section can have a rel, strtab, symtab */
1647 maxsections
= 4*bfd_count_sections(abfd
)+2;
1649 i_ehdrp
->e_shoff
= i_ehdrp
->e_ehsize
;
1651 /* and we'll just have to fix up the offsets later. */
1652 /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */
1654 i_shdrp
= (Elf_Internal_Shdr
*)
1655 bfd_alloc (abfd
, sizeof (*i_shdrp
) * maxsections
);
1658 bfd_error
= no_memory
;
1661 for (count
=0; count
< maxsections
; count
++)
1663 i_shdrp
[count
].rawdata
= 0;
1664 i_shdrp
[count
].contents
= 0;
1668 i_shdrp
[0].sh_name
= 0;
1669 i_shdrp
[0].sh_type
= SHT_NULL
;
1670 i_shdrp
[0].sh_flags
= 0;
1671 i_shdrp
[0].sh_addr
= 0;
1672 i_shdrp
[0].sh_offset
= 0;
1673 i_shdrp
[0].sh_size
= 0;
1674 i_shdrp
[0].sh_link
= SHN_UNDEF
;
1675 i_shdrp
[0].sh_info
= 0;
1676 i_shdrp
[0].sh_addralign
= 0;
1677 i_shdrp
[0].sh_entsize
= 0;
1679 i_ehdrp
->e_shnum
= 1;
1681 elf_elfsections (abfd
) = i_shdrp
;
1682 elf_shstrtab (abfd
) = shstrtab
;
1684 est
.i_ehdr
= elf_elfheader(abfd
);
1685 est
.i_shdrp
= elf_elfsections(abfd
);
1686 est
.shstrtab
= elf_shstrtab(abfd
);
1687 est
.symtab_section
= 0; /* elf_fake_sections fils it in */
1689 bfd_map_over_sections(abfd
, elf_fake_sections
, &est
);
1690 elf_onesymtab (abfd
) = est
.symtab_section
;
1695 DEFUN (elf_write_object_contents
, (abfd
), bfd
*abfd
)
1697 Elf_External_Ehdr x_ehdr
; /* Elf file header, external form */
1698 Elf_Internal_Ehdr
*i_ehdrp
; /* Elf file header, internal form */
1699 Elf_External_Shdr
*x_shdrp
; /* Section header table, external form */
1700 Elf_Internal_Shdr
*i_shdrp
; /* Section header table, internal form */
1706 struct strtab
*shstrtab
;
1708 if(abfd
->output_has_begun
== false)
1709 elf_compute_section_file_positions(abfd
);
1711 i_ehdrp
= elf_elfheader (abfd
);
1712 i_shdrp
= elf_elfsections (abfd
);
1713 shstrtab
= elf_shstrtab (abfd
);
1715 est
.i_ehdr
= i_ehdrp
;
1716 est
.i_shdrp
= i_shdrp
;
1717 est
.shstrtab
= shstrtab
;
1718 est
.symtab_section
= elf_onesymtab (abfd
); /* filled in by elf_fake */
1720 bfd_map_over_sections(abfd
, elf_make_sections
, &est
);
1722 /* dump out the one symtab */
1724 int symcount
= bfd_get_symcount (abfd
);
1725 asymbol
** syms
= bfd_get_outsymbols (abfd
);
1726 struct strtab
* stt
= bfd_new_strtab (abfd
);
1727 Elf_Internal_Shdr
*symtab_hdr
;
1728 Elf_Internal_Shdr
*symstrtab_hdr
;
1729 int symstrtab_section
;
1730 Elf_External_Sym
*outbound_syms
;
1733 symtab_hdr
= &i_shdrp
[est
.symtab_section
];
1734 symtab_hdr
->sh_type
= SHT_SYMTAB
;
1735 symtab_hdr
->sh_entsize
= sizeof (Elf_External_Sym
);
1736 symtab_hdr
->sh_size
= symtab_hdr
->sh_entsize
* symcount
;
1738 /* see assert in elf_fake_sections that supports this: */
1739 symstrtab_section
= est
.symtab_section
+1;
1740 symstrtab_hdr
= &i_shdrp
[symstrtab_section
];
1741 symtab_hdr
->sh_link
= symstrtab_section
;
1742 symstrtab_hdr
->sh_type
= SHT_STRTAB
;
1744 outbound_syms
= (Elf_External_Sym
*)
1745 bfd_alloc(abfd
, (1+symcount
) * sizeof(Elf_External_Sym
));
1746 /* now generate the data (for "contents") */
1747 for (idx
= 0; idx
< symcount
; idx
++)
1749 Elf_Internal_Sym sym
;
1750 sym
.st_name
= bfd_add_to_strtab (abfd
, stt
, syms
[idx
]->name
);
1751 sym
.st_value
= syms
[idx
]->value
;
1752 sym
.st_size
= 0; /* we should recover this (FIXME) */
1753 if (syms
[idx
]->flags
& BSF_WEAK
)
1754 sym
.st_info
= ELF_ST_INFO(STB_WEAK
, STT_OBJECT
);
1755 else if (syms
[idx
]->flags
& BSF_LOCAL
)
1756 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_OBJECT
);
1757 else if (syms
[idx
]->flags
& BSF_GLOBAL
)
1758 sym
.st_info
= ELF_ST_INFO(STB_GLOBAL
, STT_OBJECT
);
1759 else if (syms
[idx
]->flags
& BSF_SECTION_SYM
)
1760 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_SECTION
);
1761 else if (syms
[idx
]->flags
& BSF_FILE
)
1762 sym
.st_info
= ELF_ST_INFO(STB_LOCAL
, STT_FILE
);
1765 if (syms
[idx
]->section
)
1767 elf_section_from_bfd_section(abfd
,
1768 syms
[idx
]->section
->output_section
);
1770 sym
.st_shndx
= SHN_UNDEF
;
1772 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
+idx
+1);
1775 /* fill in 0th symbol */
1776 Elf_Internal_Sym sym
;
1782 sym
.st_shndx
= SHN_UNDEF
;
1783 elf_swap_symbol_out (abfd
, &sym
, outbound_syms
);
1785 symtab_hdr
->contents
= (void*)outbound_syms
;
1786 symstrtab_hdr
->contents
= (void*)stt
->tab
;
1787 symstrtab_hdr
->sh_size
= stt
->length
;
1790 /* put the strtab out too... */
1792 Elf_Internal_Shdr
*this_hdr
;
1795 this_section
= i_ehdrp
->e_shnum
++;
1796 i_ehdrp
->e_shstrndx
= this_section
;
1797 this_hdr
= &i_shdrp
[this_section
];
1798 this_hdr
->sh_name
= bfd_add_to_strtab (abfd
, shstrtab
, ".shstrtab");
1799 this_hdr
->sh_type
= SHT_STRTAB
;
1800 this_hdr
->sh_size
= shstrtab
->length
;
1801 this_hdr
->contents
= (void*)shstrtab
->tab
;
1804 outbase
= i_ehdrp
->e_ehsize
;
1806 /* swap the header before spitting it out... */
1807 elf_swap_ehdr_out (abfd
, i_ehdrp
, &x_ehdr
);
1808 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1809 bfd_write ((PTR
) &x_ehdr
, sizeof(x_ehdr
), 1, abfd
);
1811 outbase
+= i_ehdrp
->e_shentsize
* i_ehdrp
->e_shnum
;
1813 /* now we fix up the offsets... */
1814 for (count
= 1; count
< i_ehdrp
->e_shnum
; count
++)
1816 i_shdrp
[count
].sh_offset
= outbase
;
1817 outbase
+= i_shdrp
[count
].sh_size
;
1820 /* at this point we've concocted all the ELF sections... */
1821 x_shdrp
= (Elf_External_Shdr
*)
1822 bfd_alloc (abfd
, sizeof (*x_shdrp
) * (i_ehdrp
->e_shnum
));
1825 bfd_error
= no_memory
;
1829 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
1831 elf_swap_shdr_out (abfd
, i_shdrp
+count
, x_shdrp
+count
);
1833 bfd_write ((PTR
) x_shdrp
, sizeof(*x_shdrp
), i_ehdrp
->e_shnum
, abfd
);
1834 /* need to dump the string table too... */
1836 /* after writing the headers, we need to write the sections too... */
1837 nsect
= abfd
->sections
;
1838 for (count
= 0; count
< i_ehdrp
->e_shnum
; count
++)
1840 if(i_shdrp
[count
].contents
)
1842 bfd_seek (abfd
, i_shdrp
[count
].sh_offset
, SEEK_SET
);
1843 bfd_write (i_shdrp
[count
].contents
, i_shdrp
[count
].sh_size
, 1, abfd
);
1847 /* sample use of bfd:
1848 * bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
1849 * bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
1850 * if (bfd_seek(abfd, scn_base, SEEK_SET) != 0)
1852 * old = bfd_tell(abfd);
1859 /* Given an index of a section, retrieve a pointer to it. Note
1860 that for our purposes, sections are indexed by {1, 2, ...} with
1861 0 being an illegal index. */
1863 /* In the original, each ELF section went into exactly one BFD
1864 section. This doesn't really make sense, so we need a real mapping.
1865 The mapping has to hide in the Elf_Internal_Shdr since asection
1866 doesn't have anything like a tdata field... */
1869 DEFUN (section_from_elf_index
, (abfd
, index
),
1873 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
1874 Elf_Internal_Shdr
*hdr
= i_shdrp
+ index
;
1876 switch (hdr
->sh_type
)
1878 /* ELF sections that map to BFD sections */
1882 bfd_section_from_shdr (abfd
, index
);
1883 return (struct sec
*)hdr
->rawdata
;
1886 return (struct sec
*)&bfd_abs_section
;
1890 /* given a section, search the header to find them... */
1892 DEFUN (elf_section_from_bfd_section
, (abfd
, asect
),
1896 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
1898 Elf_Internal_Shdr
*hdr
;
1899 int maxindex
= elf_elfheader (abfd
)->e_shnum
;
1901 for(index
= 0; index
< maxindex
; index
++) {
1902 hdr
= &i_shdrp
[index
];
1903 switch (hdr
->sh_type
)
1905 /* ELF sections that map to BFD sections */
1910 if (((struct sec
*)(hdr
->rawdata
)) == asect
)
1922 DEFUN (elf_slurp_symbol_table
, (abfd
, symptrs
),
1924 asymbol
**symptrs
) /* Buffer for generated bfd symbols */
1926 Elf_Internal_Shdr
*i_shdrp
= elf_elfsections (abfd
);
1927 Elf_Internal_Shdr
*hdr
= i_shdrp
+ elf_onesymtab (abfd
);
1928 int symcount
; /* Number of external ELF symbols */
1930 asymbol
*sym
; /* Pointer to current bfd symbol */
1931 asymbol
*symbase
; /* Buffer for generated bfd symbols */
1932 Elf_Internal_Sym i_sym
;
1933 Elf_External_Sym
*x_symp
;
1935 /* this is only valid because there is only one symtab... */
1936 /* FIXME: This is incorrect, there may also be a dynamic symbol
1937 table which is a subset of the full symbol table. We either need
1938 to be prepared to read both (and merge them) or ensure that we
1939 only read the full symbol table. Currently we only get called to
1940 read the full symbol table. -fnf */
1941 if (bfd_get_outsymbols (abfd
) != NULL
)
1946 /* Read each raw ELF symbol, converting from external ELF form to
1947 internal ELF form, and then using the information to create a
1948 canonical bfd symbol table entry.
1950 Note that we allocate the initial bfd canonical symbol buffer
1951 based on a one-to-one mapping of the ELF symbols to canonical
1952 symbols. We actually use all the ELF symbols, so there will be no
1953 space left over at the end. When we have all the symbols, we
1954 build the caller's pointer vector. */
1956 if (bfd_seek (abfd
, hdr
->sh_offset
, SEEK_SET
) == -1)
1958 bfd_error
= system_call_error
;
1962 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
1963 symbase
= (asymbol
*) bfd_zalloc (abfd
, symcount
* sizeof (asymbol
));
1966 /* Temporarily allocate room for the raw ELF symbols. */
1967 x_symp
= (Elf_External_Sym
*) bfd_xmalloc (symcount
* sizeof (Elf_External_Sym
));
1969 if (bfd_read ((PTR
) x_symp
, sizeof (Elf_External_Sym
), symcount
, abfd
)
1970 != symcount
* sizeof (Elf_External_Sym
))
1973 bfd_error
= system_call_error
;
1976 /* Skip first symbol, which is a null dummy. */
1977 for (i
= 1; i
< symcount
; i
++)
1979 elf_swap_symbol_in (abfd
, x_symp
+ i
, &i_sym
);
1980 sym
-> the_bfd
= abfd
;
1981 if (i_sym
.st_name
> 0)
1982 sym
-> name
= elf_string_from_elf_section(abfd
, hdr
->sh_link
,
1985 sym
-> name
= "unnamed"; /* perhaps should include the number? */
1986 sym
-> value
= i_sym
.st_value
;
1987 /* FIXME -- this is almost certainly bogus. It's from Pace Willisson's
1988 hasty Solaris support, to pass the sizes of object files or functions
1989 down into GDB via the back door, to circumvent some other kludge in
1990 how Sun hacked stabs. -- gnu@cygnus.com */
1991 sym
-> udata
= (PTR
)i_sym
.st_size
;
1992 /* FIXME -- end of bogosity. */
1993 if (i_sym
.st_shndx
> 0 && i_sym
.st_shndx
< SHN_LORESERV
)
1995 sym
-> section
= section_from_elf_index (abfd
, i_sym
.st_shndx
);
1997 else if (i_sym
.st_shndx
== SHN_ABS
)
1999 sym
-> section
= &bfd_abs_section
;
2001 else if (i_sym
.st_shndx
== SHN_COMMON
)
2003 sym
-> section
= &bfd_com_section
;
2005 else if (i_sym
.st_shndx
== SHN_UNDEF
)
2007 sym
-> section
= &bfd_und_section
;
2010 sym
-> section
= &bfd_abs_section
;
2012 switch (ELF_ST_BIND (i_sym
.st_info
))
2015 sym
-> flags
|= BSF_LOCAL
;
2018 sym
-> flags
|= (BSF_GLOBAL
| BSF_EXPORT
);
2021 sym
-> flags
|= BSF_WEAK
;
2025 switch (ELF_ST_TYPE (i_sym
.st_info
))
2028 sym
->flags
|= BSF_SECTION_SYM
| BSF_DEBUGGING
;
2031 sym
->flags
|= BSF_FILE
| BSF_DEBUGGING
;
2037 /* We rely on the zalloc to clear out the final symbol entry. */
2039 /* We're now done with the raw symbols. */
2042 bfd_get_symcount(abfd
) = symcount
= sym
- symbase
;
2044 /* Fill in the user's symbol pointer vector if needed. */
2048 while (symcount
-- > 0)
2052 *symptrs
= 0; /* Final null pointer */
2058 /* Return the number of bytes required to hold the symtab vector.
2060 Note that we base it on the count plus 1, since we will null terminate
2061 the vector allocated based on this size. However, the ELF symbol table
2062 always has a dummy entry as symbol #0, so it ends up even. */
2065 DEFUN (elf_get_symtab_upper_bound
, (abfd
), bfd
*abfd
)
2067 unsigned int symcount
;
2068 unsigned int symtab_size
= 0;
2069 Elf_Internal_Shdr
*i_shdrp
;
2070 Elf_Internal_Shdr
*hdr
;
2072 i_shdrp
= elf_elfsections (abfd
);
2073 if (i_shdrp
!= NULL
)
2075 hdr
= i_shdrp
+ elf_onesymtab (abfd
);
2076 symcount
= hdr
->sh_size
/ sizeof (Elf_External_Sym
);
2077 symtab_size
= (symcount
- 1 + 1) * (sizeof (asymbol
));
2079 return (symtab_size
);
2083 This function return the number of bytes required to store the
2084 relocation information associated with section <<sect>>
2085 attached to bfd <<abfd>>
2089 elf_get_reloc_upper_bound (abfd
, asect
)
2093 if (asect
->flags
& SEC_RELOC
)
2095 /* either rel or rela */
2096 return asect
->_raw_size
;
2103 DEFUN(elf_slurp_reloca_table
,(abfd
, asect
, symbols
),
2108 Elf_External_Rela
*native_relocs
;
2109 arelent
*reloc_cache
;
2114 if (asect
->relocation
)
2116 if (asect
->reloc_count
== 0)
2118 if (asect
->flags
& SEC_CONSTRUCTOR
)
2121 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2122 native_relocs
= (Elf_External_Rela
*)
2123 bfd_alloc(abfd
, asect
->reloc_count
* sizeof(Elf_External_Rela
));
2124 bfd_read ((PTR
) native_relocs
,
2125 sizeof(Elf_External_Rela
), asect
->reloc_count
, abfd
);
2127 reloc_cache
= (arelent
*)
2128 bfd_alloc(abfd
, (size_t) (asect
->reloc_count
* sizeof(arelent
)));
2130 if (! reloc_cache
) {
2131 bfd_error
= no_memory
;
2135 for (idx
= 0; idx
< asect
->reloc_count
; idx
++)
2137 #ifdef RELOC_PROCESSING
2138 /* sparc, 68k, 88k, 860 use rela only. */
2139 /* 386 and we32000 use rel only... fix it for them later. */
2140 Elf_Internal_Rela dst
;
2141 Elf_External_Rela
*src
;
2143 cache_ptr
= reloc_cache
+ idx
;
2144 src
= native_relocs
+ idx
;
2145 elf_swap_reloca_in(abfd
, src
, &dst
);
2147 RELOC_PROCESSING(cache_ptr
, &dst
, symbols
, abfd
, asect
);
2149 Elf_Internal_Rela dst
;
2150 Elf_External_Rela
*src
;
2152 cache_ptr
= reloc_cache
+ idx
;
2153 src
= native_relocs
+ idx
;
2155 elf_swap_reloca_in(abfd
, src
, &dst
);
2157 if(asect
->flags
& SEC_RELOC
)
2159 /* relocatable, so the offset is off of the section */
2160 cache_ptr
->address
= dst
.r_offset
+ asect
->vma
;
2164 /* non-relocatable, so the offset a virtual address */
2165 cache_ptr
->address
= dst
.r_offset
;
2167 /* ELF_R_SYM(dst.r_info) is the symbol table offset... */
2168 cache_ptr
->sym_ptr_ptr
= symbols
+ ELF_R_SYM(dst
.r_info
);
2169 cache_ptr
->addend
= dst
.r_addend
;
2171 /* Fill in the cache_ptr->howto field from dst.r_type */
2173 struct elf_backend_data
*ebd
;
2174 ebd
= (struct elf_backend_data
*) (abfd
->xvec
->backend_data
);
2175 (*ebd
->elf_info_to_howto
)(abfd
, cache_ptr
, &dst
);
2180 asect
->relocation
= reloc_cache
;
2186 elf_canonicalize_reloc (abfd
, section
, relptr
, symbols
)
2192 arelent
*tblptr
= section
->relocation
;
2193 unsigned int count
= 0;
2195 /* snarfed from coffcode.h */
2196 /* FIXME: this could be reloc... */
2197 elf_slurp_reloca_table(abfd
, section
, symbols
);
2199 tblptr
= section
->relocation
;
2203 for (; count
++ < section
->reloc_count
;)
2204 *relptr
++ = tblptr
++;
2207 return section
->reloc_count
;
2211 DEFUN (elf_get_symtab
, (abfd
, alocation
),
2213 asymbol
**alocation
)
2216 if (!elf_slurp_symbol_table (abfd
, alocation
))
2219 return (bfd_get_symcount (abfd
));
2223 DEFUN (elf_make_empty_symbol
, (abfd
),
2226 elf_symbol_type
*newsym
;
2228 newsym
= (elf_symbol_type
*) bfd_zalloc (abfd
, sizeof (elf_symbol_type
));
2231 bfd_error
= no_memory
;
2236 newsym
-> symbol
.the_bfd
= abfd
;
2237 return (&newsym
-> symbol
);
2242 DEFUN (elf_print_symbol
,(ignore_abfd
, filep
, symbol
, how
),
2243 bfd
*ignore_abfd AND
2246 bfd_print_symbol_type how
)
2248 FILE *file
= (FILE *)filep
;
2251 case bfd_print_symbol_name
:
2252 fprintf(file
, "%s", symbol
->name
);
2254 case bfd_print_symbol_more
:
2255 fprintf(file
, "elf %lx %lx",
2259 case bfd_print_symbol_nm
:
2260 case bfd_print_symbol_all
:
2262 CONST
char *section_name
;
2263 section_name
= symbol
->section
? symbol
->section
->name
: "(*none*)";
2264 bfd_print_symbol_vandf((PTR
) file
, symbol
);
2265 fprintf(file
, " %s\t%s",
2275 DEFUN (elf_get_lineno
,(ignore_abfd
, symbol
),
2276 bfd
*ignore_abfd AND
2279 fprintf (stderr
, "elf_get_lineno unimplemented\n");
2286 DEFUN (elf_set_arch_mach
,(abfd
, arch
, machine
),
2288 enum bfd_architecture arch AND
2289 unsigned long machine
)
2291 /* Allow any architecture to be supported by the elf backend */
2294 case bfd_arch_unknown
: /* EM_NONE */
2295 case bfd_arch_sparc
: /* EM_SPARC */
2296 case bfd_arch_i386
: /* EM_386 */
2297 case bfd_arch_m68k
: /* EM_68K */
2298 case bfd_arch_m88k
: /* EM_88K */
2299 case bfd_arch_i860
: /* EM_860 */
2300 case bfd_arch_mips
: /* EM_MIPS (MIPS R3000) */
2301 return bfd_default_set_arch_mach(abfd
, arch
, machine
);
2308 DEFUN (elf_find_nearest_line
,(abfd
,
2316 asection
*section AND
2317 asymbol
**symbols AND
2319 CONST
char **filename_ptr AND
2320 CONST
char **functionname_ptr AND
2321 unsigned int *line_ptr
)
2323 fprintf (stderr
, "elf_find_nearest_line unimplemented\n");
2330 DEFUN (elf_sizeof_headers
, (abfd
, reloc
),
2334 fprintf (stderr
, "elf_sizeof_headers unimplemented\n");
2341 DEFUN(elf_set_section_contents
, (abfd
, section
, location
, offset
, count
),
2346 bfd_size_type count
)
2350 if (abfd
->output_has_begun
== false) /* set by bfd.c handler? */
2352 /* do setup calculations (FIXME) */
2353 elf_compute_section_file_positions(abfd
);
2356 if(bfd_seek (abfd
, (file_ptr
)section
->filepos
+ offset
, SEEK_SET
) == -1)
2358 if(bfd_write (location
, (bfd_size_type
)1, count
, abfd
) != count
)
2361 /* we really just need to save the contents away... */
2362 dest_sect
= elf_section_from_bfd_section(abfd
, section
);
2366 /* FIXME: allocate in set_section_size, then copy in here... */
2367 contents
= (void*)bfd_alloc(abfd
, count
);
2368 BFD_ASSERT(contents
);
2369 memcpy(contents
, location
, count
);
2370 elf_elfsections (abfd
)[dest_sect
].contents
= contents
;
2376 DEFUN (elf_no_info_to_howto
, (abfd
, cache_ptr
, dst
),
2378 arelent
*cache_ptr AND
2379 Elf_Internal_Rela
*dst
)