1 /* ELF executable support for BFD.
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4 Written by Fred Fish @ Cygnus Support, from information published
5 in "UNIX System V Release 4, Programmers Guide: ANSI C and
6 Programming Support Tools". Sufficient support for gdb.
8 Rewritten by Mark Eichin @ Cygnus Support, from information
9 published in "System V Application Binary Interface", chapters 4
10 and 5, as well as the various "Processor Supplement" documents
11 derived from it. Added support for assembler and other object file
14 This file is part of BFD, the Binary File Descriptor library.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /****************************************
35 This is only a partial ELF implementation,
36 incorporating only those parts that are
37 required to get gdb up and running. It is
38 expected that it will be expanded to a full
39 ELF implementation at some future date.
41 Unimplemented stubs call abort() to ensure
42 that they get proper attention if they are
43 ever called. The stubs are here since
44 this version was hacked from the COFF
45 version, and thus they will probably
46 go away or get expanded appropriately in a
51 *****************************************/
54 /* Problems and other issues to resolve.
56 (1) BFD expects there to be some fixed number of "sections" in
57 the object file. I.E. there is a "section_count" variable in the
58 bfd structure which contains the number of sections. However, ELF
59 supports multiple "views" of a file. In particular, with current
60 implementations, executable files typically have two tables, a
61 program header table and a section header table, both of which
62 partition the executable.
64 In ELF-speak, the "linking view" of the file uses the section header
65 table to access "sections" within the file, and the "execution view"
66 uses the program header table to access "segments" within the file.
67 "Segments" typically may contain all the data from one or more
70 Note that the section header table is optional in ELF executables,
71 but it is this information that is most useful to gdb. If the
72 section header table is missing, then gdb should probably try
73 to make do with the program header table. (FIXME)
81 #include "elf/common.h"
82 #include "elf/internal.h"
83 #include "elf/external.h"
85 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
86 #include <sys/procfs.h>
88 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
89 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
90 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
93 /* Forward data declarations */
95 extern bfd_target elf_little_vec, elf_big_vec;
97 /* Currently the elf_symbol_type struct just contains the generic bfd
105 /* Some private data is stashed away for future use using the tdata pointer
106 in the bfd structure. This information is different for ELF core files
107 and other ELF files. */
109 typedef struct elf_core_tdata_struct
111 void *prstatus; /* The raw /proc prstatus structure */
112 void *prpsinfo; /* The raw /proc prpsinfo structure */
115 #define core_prpsinfo(bfd) (((bfd)->tdata.elf_core_data) -> prpsinfo)
116 #define core_prstatus(bfd) (((bfd)->tdata.elf_core_data) -> prstatus)
119 typedef struct elf_obj_tdata_struct
121 Elf_Internal_Ehdr *elf_header;
122 Elf_Internal_Shdr *elf_sect_ptr;
123 struct strtab *strtab_ptr;
127 #define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data)
128 #define elf_elfheader(bfd) (elf_tdata(bfd) -> elf_header)
129 #define elf_elfsections(bfd) (elf_tdata(bfd) -> elf_sect_ptr)
130 #define elf_shstrtab(bfd) (elf_tdata(bfd) -> strtab_ptr)
131 #define elf_onesymtab(bfd) (elf_tdata(bfd) -> symtab_section)
133 /* Translate an ELF symbol in external format into an ELF symbol in internal
137 DEFUN(elf_swap_symbol_in,(abfd, src, dst),
139 Elf_External_Sym *src AND
140 Elf_Internal_Sym *dst)
142 dst -> st_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_name);
143 dst -> st_value = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_value);
144 dst -> st_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_size);
145 dst -> st_info = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_info);
146 dst -> st_other = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_other);
147 dst -> st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> st_shndx);
150 /* Translate an ELF symbol in internal format into an ELF symbol in external
154 DEFUN(elf_swap_symbol_out,(abfd, src, dst),
156 Elf_Internal_Sym *src AND
157 Elf_External_Sym *dst)
159 bfd_h_put_32 (abfd, src->st_name, dst->st_name);
160 bfd_h_put_32 (abfd, src->st_value, dst->st_value);
161 bfd_h_put_32 (abfd, src->st_size, dst->st_size);
162 bfd_h_put_8 (abfd, src->st_info, dst->st_info);
163 bfd_h_put_8 (abfd, src->st_other, dst->st_other);
164 bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
168 /* Translate an ELF file header in external format into an ELF file header in
172 DEFUN(elf_swap_ehdr_in,(abfd, src, dst),
174 Elf_External_Ehdr *src AND
175 Elf_Internal_Ehdr *dst)
177 memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT);
178 dst -> e_type = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_type);
179 dst -> e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_machine);
180 dst -> e_version = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_version);
181 dst -> e_entry = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_entry);
182 dst -> e_phoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_phoff);
183 dst -> e_shoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_shoff);
184 dst -> e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_flags);
185 dst -> e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_ehsize);
186 dst -> e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phentsize);
187 dst -> e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phnum);
188 dst -> e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shentsize);
189 dst -> e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shnum);
190 dst -> e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shstrndx);
193 /* Translate an ELF file header in internal format into an ELF file header in
197 DEFUN(elf_swap_ehdr_out,(abfd, src, dst),
199 Elf_Internal_Ehdr *src AND
200 Elf_External_Ehdr *dst)
202 memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT);
203 /* note that all elements of dst are *arrays of unsigned char* already... */
204 bfd_h_put_16 (abfd, src->e_type, dst->e_type);
205 bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
206 bfd_h_put_32 (abfd, src->e_version, dst->e_version);
207 bfd_h_put_32 (abfd, src->e_entry, dst->e_entry);
208 bfd_h_put_32 (abfd, src->e_phoff, dst->e_phoff);
209 bfd_h_put_32 (abfd, src->e_shoff, dst->e_shoff);
210 bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
211 bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
212 bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
213 bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
214 bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
215 bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
216 bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
220 /* Translate an ELF section header table entry in external format into an
221 ELF section header table entry in internal format. */
224 DEFUN(elf_swap_shdr_in,(abfd, src, dst),
226 Elf_External_Shdr *src AND
227 Elf_Internal_Shdr *dst)
229 dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
230 dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
231 dst->sh_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_flags);
232 dst->sh_addr = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_addr);
233 dst->sh_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_offset);
234 dst->sh_size = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_size);
235 dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
236 dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
237 dst->sh_addralign = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_addralign);
238 dst->sh_entsize = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_entsize);
239 /* we haven't done any processing on it yet, so... */
240 dst->rawdata = (void*)0;
243 /* Translate an ELF section header table entry in internal format into an
244 ELF section header table entry in external format. */
247 DEFUN(elf_swap_shdr_out,(abfd, src, dst),
249 Elf_Internal_Shdr *src AND
250 Elf_External_Shdr *dst)
252 /* note that all elements of dst are *arrays of unsigned char* already... */
253 bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
254 bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
255 bfd_h_put_32 (abfd, src->sh_flags, dst->sh_flags);
256 bfd_h_put_32 (abfd, src->sh_addr, dst->sh_addr);
257 bfd_h_put_32 (abfd, src->sh_offset, dst->sh_offset);
258 bfd_h_put_32 (abfd, src->sh_size, dst->sh_size);
259 bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
260 bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
261 bfd_h_put_32 (abfd, src->sh_addralign, dst->sh_addralign);
262 bfd_h_put_32 (abfd, src->sh_entsize, dst->sh_entsize);
266 /* Translate an ELF program header table entry in external format into an
267 ELF program header table entry in internal format. */
270 DEFUN(elf_swap_phdr_in,(abfd, src, dst),
272 Elf_External_Phdr *src AND
273 Elf_Internal_Phdr *dst)
275 dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
276 dst->p_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->p_offset);
277 dst->p_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) src->p_vaddr);
278 dst->p_paddr = bfd_h_get_32 (abfd, (bfd_byte *) src->p_paddr);
279 dst->p_filesz = bfd_h_get_32 (abfd, (bfd_byte *) src->p_filesz);
280 dst->p_memsz = bfd_h_get_32 (abfd, (bfd_byte *) src->p_memsz);
281 dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
282 dst->p_align = bfd_h_get_32 (abfd, (bfd_byte *) src->p_align);
286 /* Translate an ELF reloc from external format to internal format. */
288 DEFUN(elf_swap_reloc_in,(abfd, src, dst),
290 Elf_External_Rel *src AND
291 Elf_Internal_Rel *dst)
293 dst->r_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->r_offset);
294 dst->r_info = bfd_h_get_32 (abfd, (bfd_byte *) src->r_info);
298 DEFUN(elf_swap_reloca_in,(abfd, src, dst),
300 Elf_External_Rela *src AND
301 Elf_Internal_Rela *dst)
303 dst->r_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->r_offset);
304 dst->r_info = bfd_h_get_32 (abfd, (bfd_byte *) src->r_info);
305 dst->r_addend = bfd_h_get_32 (abfd, (bfd_byte *) src->r_addend);
308 /* Translate an ELF reloc from internal format to external format. */
310 DEFUN(elf_swap_reloc_out,(abfd, src, dst),
312 Elf_Internal_Rel *src AND
313 Elf_External_Rel *dst)
315 bfd_h_put_32 (abfd, src->r_offset, dst->r_offset);
316 bfd_h_put_32 (abfd, src->r_info, dst->r_info);
320 DEFUN(elf_swap_reloca_out,(abfd, src, dst),
322 Elf_Internal_Rela *src AND
323 Elf_External_Rela *dst)
325 bfd_h_put_32 (abfd, src->r_offset, dst->r_offset);
326 bfd_h_put_32 (abfd, src->r_info, dst->r_info);
327 bfd_h_put_32 (abfd, src->r_addend, dst->r_addend);
330 static char *EXFUN(elf_read, (bfd *, long, int));
331 static struct sec * EXFUN(section_from_elf_index, (bfd *, int));
332 static int EXFUN(elf_section_from_bfd_section, (bfd *, struct sec *));
333 static boolean EXFUN(elf_slurp_symbol_table, (bfd *, Elf_Internal_Shdr*));
334 static void EXFUN(elf_info_to_howto, (bfd *, arelent *, Elf_Internal_Rela *));
338 DEFUN(elf_get_str_section, (abfd, shindex),
340 unsigned int shindex)
342 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
343 unsigned int shstrtabsize = i_shdrp[shindex].sh_size;
344 unsigned int offset = i_shdrp[shindex].sh_offset;
346 if ((shstrtab = elf_read (abfd, offset, shstrtabsize)) == NULL)
350 i_shdrp[shindex].rawdata = (void*)shstrtab;
354 DEFUN(elf_string_from_elf_section, (abfd, shindex, strindex),
356 unsigned int shindex AND
357 unsigned int strindex)
359 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
360 Elf_Internal_Shdr *hdr = i_shdrp + shindex;
363 if (elf_get_str_section (abfd, shindex) == NULL)
368 return ((char*)hdr->rawdata)+strindex;
371 #define elf_string_from_elf_strtab(abfd, strindex) \
372 elf_string_from_elf_section (abfd, elf_elfheader(abfd)->e_shstrndx, strindex)
374 /* Create a new bfd section from an ELF section header. */
377 DEFUN(bfd_section_from_shdr, (abfd, shindex),
379 unsigned int shindex)
381 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
382 Elf_Internal_Shdr *hdr = i_shdrp + shindex;
386 name = hdr->sh_name ?
387 elf_string_from_elf_strtab (abfd, hdr->sh_name) : "unnamed";
389 switch(hdr->sh_type) {
391 /* inactive section. Throw it away. */
395 /* Bits that get saved. This one is real. */
398 newsect = bfd_make_section (abfd, name);
399 newsect->vma = hdr->sh_addr;
400 newsect->_raw_size = hdr->sh_size;
401 newsect->filepos = hdr->sh_offset; /* so we can read back the bits */
402 newsect->flags |= SEC_HAS_CONTENTS;
404 if (hdr->sh_flags & SHF_ALLOC)
406 newsect->flags |= SEC_ALLOC;
407 if (hdr->sh_type != SHT_NOBITS)
408 newsect->flags |= SEC_LOAD;
411 if (!(hdr->sh_flags & SHF_WRITE))
412 newsect->flags |= SEC_READONLY;
414 if (hdr->sh_flags & SHF_EXECINSTR)
415 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
417 newsect->flags |= SEC_DATA;
419 hdr->rawdata = (void*)newsect;
424 /* we may be getting called by reference. Bring'em in... */
425 if (! hdr->rawdata) {
426 /* fetch our corresponding string table. */
427 bfd_section_from_shdr (abfd, hdr->sh_link);
429 /* start turning our elf symbols into bfd symbols. */
430 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
431 elf_slurp_symbol_table (abfd, hdr);
432 abfd->flags |= HAS_SYMS;
437 /* we may be getting called by reference. Bring'em in... */
440 /* we don't need to do anything, just make the data available. */
441 if (elf_get_str_section (abfd, shindex) == NULL)
447 /* *these* do a lot of work -- but build no sections! */
448 /* the spec says there can be multiple strtabs, but only one symtab */
449 /* but there can be lots of REL* sections. */
451 asection *target_sect;
454 bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */
455 bfd_section_from_shdr (abfd, hdr->sh_info); /* target */
456 target_sect = section_from_elf_index (abfd, hdr->sh_info);
458 if (!elf_slurp_symbol_table(abfd, i_shdrp + hdr->sh_link))
461 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
462 target_sect->flags |= SEC_RELOC;
463 target_sect->relocation = 0;
464 target_sect->rel_filepos = hdr->sh_offset;
465 fprintf(stderr, "ELF>> section %s reading %d relocs\n",
466 target_sect->name, target_sect->reloc_count);
473 case SHT_DYNSYM: /* could treat this like symtab... */
474 fprintf(stderr, "Dynamic Linking sections not yet supported.\n");
478 fprintf(stderr, "Note Sections not yet supported.\n");
482 fprintf(stderr, "SHLIB Sections not supported (and non conforming.)\n");
501 static struct strtab *
502 DEFUN(bfd_new_strtab, (abfd),
507 ss = (struct strtab *)malloc(sizeof(struct strtab));
509 BFD_ASSERT(ss->tab != 0);
518 DEFUN(bfd_add_to_strtab, (abfd, ss, str),
520 struct strtab *ss AND
523 /* should search first, but for now: */
524 /* include the trailing NUL */
525 int ln = strlen(str)+1;
527 /* should this be using obstacks? */
528 ss->tab = realloc(ss->tab, ss->length + ln);
530 BFD_ASSERT(ss->tab != 0);
531 strcpy(ss->tab + ss->length, str);
535 return ss->length - ln;
539 DEFUN(bfd_add_2_to_strtab, (abfd, ss, str, str2),
541 struct strtab *ss AND
545 /* should search first, but for now: */
546 /* include the trailing NUL */
547 int ln = strlen(str)+strlen(str2)+1;
549 /* should this be using obstacks? */
551 ss->tab = realloc(ss->tab, ss->length + ln);
553 ss->tab = malloc(ln);
555 BFD_ASSERT(ss->tab != 0);
556 strcpy(ss->tab + ss->length, str);
557 strcpy(ss->tab + ss->length + strlen(str), str2);
561 return ss->length - ln;
564 /* Create a new ELF section from a bfd section. */
567 DEFUN(bfd_shdr_from_section, (abfd, hdr, shstrtab, indx),
569 Elf_Internal_Shdr *hdr AND
570 struct strtab *shstrtab AND
576 /* figure out out to write the section name from the bfd section name. MWE */
578 sect = abfd->sections;
579 for (ndx = indx; --ndx; )
583 hdr[indx].sh_name = bfd_add_to_strtab(abfd, shstrtab,
584 bfd_section_name(abfd, sect));
585 hdr[indx].sh_addr = sect->vma;
586 hdr[indx].sh_size = sect->_raw_size;
587 hdr[indx].sh_flags = 0;
588 /* these need to be preserved on */
589 hdr[indx].sh_link = 0;
590 hdr[indx].sh_info = 0;
591 hdr[indx].sh_addralign = 0;
592 hdr[indx].sh_entsize = 0;
594 hdr[indx].sh_type = 0;
595 if (sect->flags & SEC_RELOC) {
596 hdr[indx].sh_type = SHT_RELA; /* FIXME -- sparc specific */
599 if (sect->flags & SEC_HAS_CONTENTS)
601 hdr[indx].sh_offset = sect->filepos;
602 hdr[indx].sh_size = sect->_raw_size;
604 if (sect->flags & SEC_ALLOC)
606 hdr[indx].sh_flags |= SHF_ALLOC;
607 if (sect->flags & SEC_LOAD)
609 /* do something with sh_type ? */
612 if (!(sect->flags & SEC_READONLY))
613 hdr[indx].sh_flags |= SHF_WRITE;
615 if (sect->flags & SEC_CODE)
616 hdr[indx].sh_flags |= SHF_EXECINSTR;
621 /* Create a new bfd section from an ELF program header.
623 Since program segments have no names, we generate a synthetic name
624 of the form segment<NUM>, where NUM is generally the index in the
625 program header table. For segments that are split (see below) we
626 generate the names segment<NUM>a and segment<NUM>b.
628 Note that some program segments may have a file size that is different than
629 (less than) the memory size. All this means is that at execution the
630 system must allocate the amount of memory specified by the memory size,
631 but only initialize it with the first "file size" bytes read from the
632 file. This would occur for example, with program segments consisting
633 of combined data+bss.
635 To handle the above situation, this routine generates TWO bfd sections
636 for the single program segment. The first has the length specified by
637 the file size of the segment, and the second has the length specified
638 by the difference between the two sizes. In effect, the segment is split
639 into it's initialized and uninitialized parts.
644 DEFUN(bfd_section_from_phdr, (abfd, hdr, index),
646 Elf_Internal_Phdr *hdr AND
654 split = ((hdr -> p_memsz > 0) &&
655 (hdr -> p_filesz > 0) &&
656 (hdr -> p_memsz > hdr -> p_filesz));
657 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
658 name = bfd_alloc (abfd, strlen (namebuf) + 1);
659 (void) strcpy (name, namebuf);
660 newsect = bfd_make_section (abfd, name);
661 newsect -> vma = hdr -> p_vaddr;
662 newsect -> _raw_size = hdr -> p_filesz;
663 newsect -> filepos = hdr -> p_offset;
664 newsect -> flags |= SEC_HAS_CONTENTS;
665 if (hdr -> p_type == PT_LOAD)
667 newsect -> flags |= SEC_ALLOC;
668 newsect -> flags |= SEC_LOAD;
669 if (hdr -> p_flags & PF_X)
671 /* FIXME: all we known is that it has execute PERMISSION,
673 newsect -> flags |= SEC_CODE;
676 if (!(hdr -> p_flags & PF_W))
678 newsect -> flags |= SEC_READONLY;
683 sprintf (namebuf, "segment%db", index);
684 name = bfd_alloc (abfd, strlen (namebuf) + 1);
685 (void) strcpy (name, namebuf);
686 newsect = bfd_make_section (abfd, name);
687 newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz;
688 newsect -> _raw_size = hdr -> p_memsz - hdr -> p_filesz;
689 if (hdr -> p_type == PT_LOAD)
691 newsect -> flags |= SEC_ALLOC;
692 if (hdr -> p_flags & PF_X)
693 newsect -> flags |= SEC_CODE;
695 if (!(hdr -> p_flags & PF_W))
696 newsect -> flags |= SEC_READONLY;
705 DEFUN(bfd_prstatus,(abfd, descdata, descsz, filepos),
712 prstatus_t *status = (prstatus_t *)0;
714 if (descsz == sizeof (prstatus_t))
716 newsect = bfd_make_section (abfd, ".reg");
717 newsect -> _raw_size = sizeof (status->pr_reg);
718 newsect -> filepos = filepos + (long) &status->pr_reg;
719 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
720 newsect -> alignment_power = 2;
721 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
723 memcpy (core_prstatus (abfd), descdata, descsz);
728 /* Stash a copy of the prpsinfo structure away for future use. */
731 DEFUN(bfd_prpsinfo,(abfd, descdata, descsz, filepos),
739 if (descsz == sizeof (prpsinfo_t))
741 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
743 bcopy (descdata, core_prpsinfo (abfd), descsz);
749 DEFUN(bfd_fpregset,(abfd, descdata, descsz, filepos),
757 newsect = bfd_make_section (abfd, ".reg2");
758 newsect -> _raw_size = descsz;
759 newsect -> filepos = filepos;
760 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
761 newsect -> alignment_power = 2;
764 #endif /* HAVE_PROCFS */
766 /* Return a pointer to the args (including the command name) that were
767 seen by the program that generated the core dump. Note that for
768 some reason, a spurious space is tacked onto the end of the args
769 in some (at least one anyway) implementations, so strip it off if
773 DEFUN(elf_core_file_failing_command, (abfd),
777 if (core_prpsinfo (abfd))
779 prpsinfo_t *p = core_prpsinfo (abfd);
780 char *scan = p -> pr_psargs;
783 if ((scan > p -> pr_psargs) && (*scan == ' '))
787 return (p -> pr_psargs);
793 /* Return the number of the signal that caused the core dump. Presumably,
794 since we have a core file, we got a signal of some kind, so don't bother
795 checking the other process status fields, just return the signal number.
799 DEFUN(elf_core_file_failing_signal, (abfd),
803 if (core_prstatus (abfd))
805 return (((prstatus_t *)(core_prstatus (abfd))) -> pr_cursig);
811 /* Check to see if the core file could reasonably be expected to have
812 come for the current executable file. Note that by default we return
813 true unless we find something that indicates that there might be a
818 DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
827 /* First, xvecs must match since both are ELF files for the same target. */
829 if (core_bfd->xvec != exec_bfd->xvec)
831 bfd_error = system_call_error;
837 /* If no prpsinfo, just return true. Otherwise, grab the last component
838 of the exec'd pathname from the prpsinfo. */
840 if (core_prpsinfo (core_bfd))
842 corename = (((struct prpsinfo *) core_prpsinfo (core_bfd)) -> pr_fname);
849 /* Find the last component of the executable pathname. */
851 if ((execname = strrchr (exec_bfd -> filename, '/')) != NULL)
857 execname = (char *) exec_bfd -> filename;
860 /* See if they match */
862 return (strcmp (execname, corename) ? false : true);
868 #endif /* HAVE_PROCFS */
871 /* ELF core files contain a segment of type PT_NOTE, that holds much of
872 the information that would normally be available from the /proc interface
873 for the process, at the time the process dumped core. Currently this
874 includes copies of the prstatus, prpsinfo, and fpregset structures.
876 Since these structures are potentially machine dependent in size and
877 ordering, bfd provides two levels of support for them. The first level,
878 available on all machines since it does not require that the host
879 have /proc support or the relevant include files, is to create a bfd
880 section for each of the prstatus, prpsinfo, and fpregset structures,
881 without any interpretation of their contents. With just this support,
882 the bfd client will have to interpret the structures itself. Even with
883 /proc support, it might want these full structures for it's own reasons.
885 In the second level of support, where HAVE_PROCFS is defined, bfd will
886 pick apart the structures to gather some additional information that
887 clients may want, such as the general register set, the name of the
888 exec'ed file and its arguments, the signal (if any) that caused the
894 DEFUN(elf_corefile_note, (abfd, hdr),
896 Elf_Internal_Phdr *hdr)
898 Elf_External_Note *x_note_p; /* Elf note, external form */
899 Elf_Internal_Note i_note; /* Elf note, internal form */
900 char *buf = NULL; /* Entire note segment contents */
901 char *namedata; /* Name portion of the note */
902 char *descdata; /* Descriptor portion of the note */
903 char *sectname; /* Name to use for new section */
904 long filepos; /* File offset to descriptor data */
907 if (hdr -> p_filesz > 0
908 && (buf = (char *) bfd_xmalloc (hdr -> p_filesz)) != NULL
909 && bfd_seek (abfd, hdr -> p_offset, SEEK_SET) != -1L
910 && bfd_read ((PTR) buf, hdr -> p_filesz, 1, abfd) == hdr -> p_filesz)
912 x_note_p = (Elf_External_Note *) buf;
913 while ((char *) x_note_p < (buf + hdr -> p_filesz))
915 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> namesz);
916 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> descsz);
917 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> type);
918 namedata = x_note_p -> name;
919 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
920 filepos = hdr -> p_offset + (descdata - buf);
921 switch (i_note.type) {
923 /* process descdata as prstatus info */
924 bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
925 sectname = ".prstatus";
928 /* process descdata as fpregset info */
929 bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
930 sectname = ".fpregset";
933 /* process descdata as prpsinfo */
934 bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
935 sectname = ".prpsinfo";
938 /* Unknown descriptor, just ignore it. */
942 if (sectname != NULL)
944 newsect = bfd_make_section (abfd, sectname);
945 newsect -> _raw_size = i_note.descsz;
946 newsect -> filepos = filepos;
947 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
948 newsect -> alignment_power = 2;
950 x_note_p = (Elf_External_Note *)
951 (descdata + BFD_ALIGN (i_note.descsz, 4));
963 /* Read a specified number of bytes at a specified offset in an ELF
964 file, into a newly allocated buffer, and return a pointer to the
968 DEFUN(elf_read, (abfd, offset, size),
975 if ((buf = bfd_alloc (abfd, size)) == NULL)
977 bfd_error = no_memory;
980 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
982 bfd_error = system_call_error;
985 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
987 bfd_error = system_call_error;
993 /* Begin processing a given object.
995 First we validate the file by reading in the ELF header and checking
1001 DEFUN (elf_object_p, (abfd), bfd *abfd)
1003 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
1004 Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
1005 Elf_External_Shdr x_shdr; /* Section header table entry, external form */
1006 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1008 char *shstrtab; /* Internal copy of section header stringtab */
1009 Elf_Off offset; /* Temp place to stash file offsets */
1011 /* Read in the ELF header in external format. */
1013 if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
1015 bfd_error = system_call_error;
1019 /* Now check to see if we have a valid ELF file, and one that BFD can
1020 make use of. The magic number must match, the address size ('class')
1021 and byte-swapping must match our XVEC entry, and it must have a
1022 section header table (FIXME: See comments re sections at top of this
1025 if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
1026 x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
1027 x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
1028 x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
1031 bfd_error = wrong_format;
1035 /* FIXME, Check EI_VERSION here ! */
1037 switch (x_ehdr.e_ident[EI_CLASS])
1039 case ELFCLASSNONE: /* address size not specified */
1040 goto wrong; /* No support if can't tell address size */
1041 case ELFCLASS32: /* 32-bit addresses */
1043 case ELFCLASS64: /* 64-bit addresses */
1044 goto wrong; /* FIXME: 64 bits not yet supported */
1046 goto wrong; /* No support if unknown address class */
1049 /* Switch xvec to match the specified byte order. */
1050 switch (x_ehdr.e_ident[EI_DATA])
1052 case ELFDATA2MSB: /* Big-endian */
1053 if (!abfd->xvec->header_byteorder_big_p)
1056 case ELFDATA2LSB: /* Little-endian */
1057 if (abfd->xvec->header_byteorder_big_p)
1060 case ELFDATANONE: /* No data encoding specified */
1061 default: /* Unknown data encoding specified */
1065 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1066 the tdata pointer in the bfd. */
1068 if ((abfd -> tdata.elf_obj_data =
1069 (elf_obj_tdata*) bfd_zalloc (abfd, sizeof (elf_obj_tdata)))
1072 bfd_error = no_memory;
1076 /* Now that we know the byte order, swap in the rest of the header */
1077 elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
1078 /* FIXME: should be alloc'ed */
1079 elf_elfheader (abfd) = &i_ehdr;
1081 /* If there is no section header table, we're hosed. */
1082 if (i_ehdr.e_shoff == 0)
1085 if (i_ehdr.e_type == ET_EXEC || i_ehdr.e_type == ET_DYN)
1086 abfd -> flags |= EXEC_P;
1088 switch (i_ehdr.e_machine)
1091 case EM_M32: /* or should this be bfd_arch_obscure? */
1092 bfd_default_set_arch_mach(abfd, bfd_arch_unknown, 0);
1095 bfd_default_set_arch_mach(abfd, bfd_arch_sparc, 0);
1098 bfd_default_set_arch_mach(abfd, bfd_arch_i386, 0);
1101 bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0);
1104 bfd_default_set_arch_mach(abfd, bfd_arch_m88k, 0);
1107 bfd_default_set_arch_mach(abfd, bfd_arch_i860, 0);
1110 bfd_default_set_arch_mach(abfd, bfd_arch_mips, 0);
1116 /* Allocate space for a copy of the section header table in
1117 internal form, seek to the section header table in the file,
1118 read it in, and convert it to internal form. As a simple sanity
1119 check, verify that the what BFD thinks is the size of each section
1120 header table entry actually matches the size recorded in the file. */
1122 if (i_ehdr.e_shentsize != sizeof (x_shdr))
1124 i_shdrp = (Elf_Internal_Shdr *)
1125 bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdr.e_shnum);
1128 bfd_error = no_memory;
1131 if (bfd_seek (abfd, i_ehdr.e_shoff, SEEK_SET) == -1)
1133 bfd_error = system_call_error;
1136 for (shindex = 0; shindex < i_ehdr.e_shnum; shindex++)
1138 if (bfd_read ((PTR) &x_shdr, sizeof x_shdr, 1, abfd)
1141 bfd_error = system_call_error;
1144 elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
1147 elf_elfsections (abfd) = i_shdrp;
1149 /* Read in the string table containing the names of the sections. We
1150 will need the base pointer to this table later. */
1151 /* We read this inline now, so that we don't have to go through
1152 bfd_section_from_shdr with it (since this particular strtab is
1153 used to find all of the ELF section names.) */
1155 shstrtab = elf_get_str_section (abfd, i_ehdr.e_shstrndx);
1159 /* Once all of the section headers have been read and converted, we
1160 can start processing them. Note that the first section header is
1161 a dummy placeholder entry, so we ignore it.
1163 We also watch for the symbol table section and remember the file
1164 offset and section size for both the symbol table section and the
1165 associated string table section. */
1167 for (shindex = 1; shindex < i_ehdr.e_shnum; shindex++)
1169 bfd_section_from_shdr (abfd, shindex);
1172 /* Remember the entry point specified in the ELF file header. */
1174 bfd_get_start_address (abfd) = i_ehdr.e_entry;
1176 return (abfd->xvec);
1179 /* Core files are simply standard ELF formatted files that partition
1180 the file using the execution view of the file (program header table)
1181 rather than the linking view. In fact, there is no section header
1182 table in a core file.
1184 The process status information (including the contents of the general
1185 register set) and the floating point register set are stored in a
1186 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
1187 that allow standard bfd access to the general registers (.reg) and the
1188 floating point registers (.reg2).
1193 DEFUN (elf_core_file_p, (abfd), bfd *abfd)
1195 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
1196 Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
1197 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
1198 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
1199 unsigned int phindex;
1201 /* Read in the ELF header in external format. */
1203 if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
1205 bfd_error = system_call_error;
1209 /* Now check to see if we have a valid ELF file, and one that BFD can
1210 make use of. The magic number must match, the address size ('class')
1211 and byte-swapping must match our XVEC entry, and it must have a
1212 program header table (FIXME: See comments re segments at top of this
1215 if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
1216 x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
1217 x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
1218 x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
1221 bfd_error = wrong_format;
1225 /* FIXME, Check EI_VERSION here ! */
1227 switch (x_ehdr.e_ident[EI_CLASS])
1229 case ELFCLASSNONE: /* address size not specified */
1230 goto wrong; /* No support if can't tell address size */
1231 case ELFCLASS32: /* 32-bit addresses */
1233 case ELFCLASS64: /* 64-bit addresses */
1234 goto wrong; /* FIXME: 64 bits not yet supported */
1236 goto wrong; /* No support if unknown address class */
1239 /* Switch xvec to match the specified byte order. */
1240 switch (x_ehdr.e_ident[EI_DATA])
1242 case ELFDATA2MSB: /* Big-endian */
1243 abfd->xvec = &elf_big_vec;
1245 case ELFDATA2LSB: /* Little-endian */
1246 abfd->xvec = &elf_little_vec;
1248 case ELFDATANONE: /* No data encoding specified */
1249 default: /* Unknown data encoding specified */
1253 /* Now that we know the byte order, swap in the rest of the header */
1254 elf_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
1256 /* If there is no program header, or the type is not a core file, then
1258 if (i_ehdr.e_phoff == 0 || i_ehdr.e_type != ET_CORE)
1261 /* Allocate an instance of the elf_core_tdata structure and hook it up to
1262 the tdata pointer in the bfd. */
1264 abfd->tdata.elf_core_data =
1265 (elf_core_tdata *) bfd_zalloc (abfd, sizeof (elf_core_tdata));
1266 if (abfd->tdata.elf_core_data == NULL)
1268 bfd_error = no_memory;
1272 /* Allocate space for a copy of the program header table in
1273 internal form, seek to the program header table in the file,
1274 read it in, and convert it to internal form. As a simple sanity
1275 check, verify that the what BFD thinks is the size of each program
1276 header table entry actually matches the size recorded in the file. */
1278 if (i_ehdr.e_phentsize != sizeof (x_phdr))
1280 i_phdrp = (Elf_Internal_Phdr *)
1281 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdr.e_phnum);
1284 bfd_error = no_memory;
1287 if (bfd_seek (abfd, i_ehdr.e_phoff, SEEK_SET) == -1)
1289 bfd_error = system_call_error;
1292 for (phindex = 0; phindex < i_ehdr.e_phnum; phindex++)
1294 if (bfd_read ((PTR) &x_phdr, sizeof (x_phdr), 1, abfd)
1297 bfd_error = system_call_error;
1300 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
1303 /* Once all of the program headers have been read and converted, we
1304 can start processing them. */
1306 for (phindex = 0; phindex < i_ehdr.e_phnum; phindex++)
1308 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
1309 if ((i_phdrp + phindex) -> p_type == PT_NOTE)
1311 elf_corefile_note (abfd, i_phdrp + phindex);
1315 /* Remember the entry point specified in the ELF file header. */
1317 bfd_get_start_address (abfd) = i_ehdr.e_entry;
1319 return (abfd->xvec);
1323 DEFUN (elf_mkobject, (abfd), bfd *abfd)
1325 /* this just does initialization */
1326 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
1327 elf_tdata(abfd) = (elf_obj_tdata *)
1328 bfd_zalloc (abfd, sizeof(elf_obj_tdata));
1329 if (elf_tdata(abfd) == 0) {
1330 bfd_error = no_memory;
1333 /* since everything is done at close time, do we need any
1340 Create ELF output from BFD sections.
1342 Essentially, just create the section header and forget about the program
1347 /* lacking nested functions and nested types, set up for mapping over
1348 BFD sections to produce ELF sections */
1351 Elf_Internal_Ehdr *i_ehdr;
1352 Elf_Internal_Shdr *i_shdrp;
1353 struct strtab *shstrtab;
1360 DEFUN (elf_make_sections, (abfd, asect, obj),
1365 elf_sect_thunk *thunk = (elf_sect_thunk*)obj;
1366 /* most of what is in bfd_shdr_from_section goes in here... */
1367 /* and all of these sections generate at *least* one ELF section. */
1371 /* check if we're making a PROGBITS section... */
1372 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1373 /* this was too strict... what *do* we want to check here? */
1376 Elf_Internal_Shdr *this_hdr;
1377 this_section = elf_section_from_bfd_section (abfd, asect);
1378 this_hdr = &thunk->i_shdrp[this_section];
1380 this_hdr->sh_addr = asect->vma;
1381 this_hdr->sh_size = asect->_raw_size;
1382 /* contents already set by elf_set_section_contents */
1384 if (asect->flags & SEC_RELOC)
1386 /* emit a reloc section, and thus strtab and symtab... */
1387 Elf_Internal_Shdr *rela_hdr;
1388 Elf_Internal_Shdr *symtab_hdr;
1389 Elf_Internal_Shdr *symstrtab_hdr;
1390 Elf_External_Rela *outbound_relocs;
1391 Elf_External_Sym *outbound_syms;
1393 int symstrtab_section;
1395 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1397 if (thunk->symtab_section == this_section + 1)
1398 rela_section = thunk->symtab_section + 2; /* symtab + symstrtab */
1400 rela_section = this_section + 1;
1401 rela_hdr = &thunk->i_shdrp[rela_section];
1402 rela_hdr->sh_type = SHT_RELA;
1403 rela_hdr->sh_link = thunk->symtab_section;
1404 rela_hdr->sh_info = this_section;
1405 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1406 /* orelocation has the data, reloc_count has the count... */
1407 rela_hdr->sh_size = rela_hdr->sh_entsize * asect->reloc_count;
1408 fprintf(stderr,"ELF>> sending out %d relocs to %s\n",
1409 asect->reloc_count, asect->name);
1410 outbound_relocs = (Elf_External_Rela *)
1411 bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela));
1412 for (idx = 0; idx < asect->reloc_count; idx++)
1414 Elf_Internal_Rela dst;
1416 Elf_External_Rela *src;
1418 ptr = asect->orelocation[idx];
1419 src = outbound_relocs + idx;
1420 if (asect->flags & SEC_RELOC)
1421 dst.r_offset = ptr->address - asect->vma;
1423 dst.r_offset = ptr->address;
1425 dst.r_info = ELF_R_INFO(1 /*ptr->sym_ptr_ptr*/, /* needs index into symtab (FIXME) */
1428 dst.r_addend = ptr->addend;
1429 elf_swap_reloca_out(abfd, &dst, src);
1431 rela_hdr->contents = (void*)outbound_relocs;
1437 DEFUN (elf_fake_sections, (abfd, asect, obj),
1442 elf_sect_thunk *thunk = (elf_sect_thunk*)obj;
1443 /* most of what is in bfd_shdr_from_section goes in here... */
1444 /* and all of these sections generate at *least* one ELF section. */
1448 /* check if we're making a PROGBITS section... */
1449 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1450 /* this was too strict... what *do* we want to check here? */
1453 Elf_Internal_Shdr *this_hdr;
1454 this_section = thunk->i_ehdr->e_shnum++;
1455 this_hdr = &thunk->i_shdrp[this_section];
1457 bfd_add_to_strtab (abfd, thunk->shstrtab, asect->name);
1458 /* we need to log the type *now* so that elf_section_from_bfd_section
1459 can find us... have to set rawdata too. */
1460 this_hdr->rawdata = (void*)asect;
1461 if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1462 this_hdr->sh_type = SHT_PROGBITS;
1464 /* what *do* we put here? */
1465 this_hdr->sh_type = SHT_PROGBITS;
1468 if (asect->flags & SEC_RELOC)
1470 /* emit a reloc section, and thus strtab and symtab... */
1471 Elf_Internal_Shdr *rela_hdr;
1472 Elf_Internal_Shdr *symtab_hdr;
1473 Elf_Internal_Shdr *symstrtab_hdr;
1474 Elf_External_Rela *outbound_relocs;
1475 Elf_External_Sym *outbound_syms;
1477 int symstrtab_section;
1479 /* note that only one symtab is used, so just remember it
1481 if (! thunk->symtab_section)
1483 thunk->symtab_section = thunk->i_ehdr->e_shnum++;
1484 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1485 symtab_hdr->sh_name =
1486 bfd_add_to_strtab (abfd, thunk->shstrtab, ".symtab");
1487 symtab_hdr->sh_type = SHT_SYMTAB;
1488 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
1490 symstrtab_section = thunk->i_ehdr->e_shnum++;
1491 BFD_ASSERT(symstrtab_section == thunk->symtab_section+1);
1492 symstrtab_hdr = &thunk->i_shdrp[symstrtab_section];
1493 symtab_hdr->sh_link = symstrtab_section;
1494 symstrtab_hdr->sh_name =
1495 bfd_add_to_strtab (abfd, thunk->shstrtab, ".strtab");
1496 symstrtab_hdr->sh_type = SHT_STRTAB;
1498 symtab_hdr->contents = 0;
1499 symstrtab_hdr->contents = 0;
1500 symstrtab_hdr->sh_size = 0;
1503 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1505 rela_section = thunk->i_ehdr->e_shnum++;
1506 rela_hdr = &thunk->i_shdrp[rela_section];
1508 bfd_add_2_to_strtab (abfd, thunk->shstrtab, ".rela", asect->name);
1509 rela_hdr->sh_type = SHT_RELA;
1510 rela_hdr->sh_link = thunk->symtab_section;
1511 rela_hdr->sh_info = this_section;
1512 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1519 DEFUN (elf_compute_section_file_positions, (abfd), bfd *abfd)
1521 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1522 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1523 struct strtab *shstrtab;
1524 int count, maxsections;
1528 if (! elf_shstrtab (abfd)) {
1529 i_ehdrp = (Elf_Internal_Ehdr *)
1530 bfd_alloc (abfd, sizeof (*i_ehdrp));
1531 shstrtab = bfd_new_strtab(abfd);
1533 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
1534 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
1535 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
1536 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
1538 i_ehdrp->e_ident[EI_CLASS] = ELFCLASS32; /* FIXME: find out from bfd */
1539 i_ehdrp->e_ident[EI_DATA] =
1540 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
1541 i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
1543 for(count = EI_PAD; count < EI_NIDENT; count ++)
1544 i_ehdrp->e_ident[count] = 0;
1546 i_ehdrp->e_type = (abfd->flags & EXEC_P)? ET_EXEC : ET_REL;
1547 switch(bfd_get_arch(abfd))
1549 case bfd_arch_unknown:
1550 i_ehdrp->e_machine = EM_NONE;
1552 case bfd_arch_sparc:
1553 i_ehdrp->e_machine = EM_SPARC;
1556 i_ehdrp->e_machine = EM_386;
1559 i_ehdrp->e_machine = EM_68K;
1562 i_ehdrp->e_machine = EM_88K;
1565 i_ehdrp->e_machine = EM_860;
1567 case bfd_arch_mips: /* MIPS Rxxxx */
1568 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
1570 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1572 i_ehdrp->e_machine = EM_NONE;
1574 i_ehdrp->e_version = EV_CURRENT;
1575 i_ehdrp->e_ehsize = sizeof(Elf_External_Ehdr);
1577 /* no program header, for now. */
1578 i_ehdrp->e_phoff = 0;
1579 i_ehdrp->e_phentsize = 0;
1580 i_ehdrp->e_phnum = 0;
1582 /* each bfd section is section header entry */
1583 i_ehdrp->e_entry = bfd_get_start_address (abfd);
1584 i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
1586 /* can't do this: we'll need many more... */
1587 /* i_ehdr.e_shnum = bfd_count_sections(abfd)+1; /* include 0th, shstrtab */
1588 /* figure at most each section can have a rel, strtab, symtab */
1589 maxsections = 4*bfd_count_sections(abfd)+2;
1591 i_ehdrp->e_shoff = i_ehdrp->e_ehsize;
1593 /* and we'll just have to fix up the offsets later. */
1594 /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */
1596 i_shdrp = (Elf_Internal_Shdr *)
1597 bfd_alloc (abfd, sizeof (*i_shdrp) * maxsections);
1600 bfd_error = no_memory;
1603 for (count=0; count < maxsections; count++)
1605 i_shdrp[count].rawdata = 0;
1606 i_shdrp[count].contents = 0;
1610 i_shdrp[0].sh_name = 0;
1611 i_shdrp[0].sh_type = SHT_NULL;
1612 i_shdrp[0].sh_flags = 0;
1613 i_shdrp[0].sh_addr = 0;
1614 i_shdrp[0].sh_offset = 0;
1615 i_shdrp[0].sh_size = 0;
1616 i_shdrp[0].sh_link = SHN_UNDEF;
1617 i_shdrp[0].sh_info = 0;
1618 i_shdrp[0].sh_addralign = 0;
1619 i_shdrp[0].sh_entsize = 0;
1621 i_ehdrp->e_shnum = 1;
1623 elf_elfheader (abfd) = i_ehdrp;
1624 elf_elfsections (abfd) = i_shdrp;
1625 elf_shstrtab (abfd) = shstrtab;
1627 est.i_ehdr = elf_elfheader(abfd);
1628 est.i_shdrp = elf_elfsections(abfd);
1629 est.shstrtab = elf_shstrtab(abfd);
1630 est.symtab_section = 0; /* elf_fake_sections fils it in */
1632 bfd_map_over_sections(abfd, elf_fake_sections, &est);
1633 elf_onesymtab (abfd) = est.symtab_section;
1638 DEFUN (elf_write_object_contents, (abfd), bfd *abfd)
1640 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
1641 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1642 Elf_External_Phdr *x_phdrp; /* Program header table, external form */
1643 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
1644 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
1645 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1652 struct strtab *shstrtab;
1654 if(abfd->output_has_begun == false)
1655 elf_compute_section_file_positions(abfd);
1657 i_ehdrp = elf_elfheader (abfd);
1658 i_shdrp = elf_elfsections (abfd);
1659 shstrtab = elf_shstrtab (abfd);
1661 est.i_ehdr = i_ehdrp;
1662 est.i_shdrp = i_shdrp;
1663 est.shstrtab = shstrtab;
1664 est.symtab_section = elf_onesymtab (abfd); /* filled in by elf_fake */
1666 bfd_map_over_sections(abfd, elf_make_sections, &est);
1668 /* dump out the one symtab */
1670 int symcount = bfd_get_symcount (abfd);
1671 asymbol ** syms = bfd_get_outsymbols (abfd);
1672 struct strtab * stt = bfd_new_strtab (abfd);
1673 Elf_Internal_Shdr *symtab_hdr;
1674 Elf_Internal_Shdr *symstrtab_hdr;
1675 int symstrtab_section;
1676 Elf_External_Sym *outbound_syms;
1679 symtab_hdr = &i_shdrp[est.symtab_section];
1680 symtab_hdr->sh_type = SHT_SYMTAB;
1681 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
1682 symtab_hdr->sh_size = symtab_hdr->sh_entsize * symcount;
1684 /* see assert in elf_fake_sections that supports this: */
1685 symstrtab_section = est.symtab_section+1;
1686 symstrtab_hdr = &i_shdrp[symstrtab_section];
1687 symtab_hdr->sh_link = symstrtab_section;
1688 symstrtab_hdr->sh_type = SHT_STRTAB;
1690 fprintf(stderr,"ELF>> sending out %d syms\n",symcount);
1691 outbound_syms = (Elf_External_Sym*)
1692 bfd_alloc(abfd, (1+symcount) * sizeof(Elf_External_Sym));
1693 /* now generate the data (for "contents") */
1694 for (idx = 0; idx < symcount; idx++)
1696 Elf_Internal_Sym sym;
1697 sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
1698 sym.st_value = syms[idx]->value;
1699 sym.st_size = 0; /* we should recover this (FIXME) */
1700 if (syms[idx]->flags & BSF_WEAK)
1701 sym.st_info = ELF_ST_INFO(STB_WEAK, STT_OBJECT);
1702 else if (syms[idx]->flags & BSF_LOCAL)
1703 sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_OBJECT);
1704 else if (syms[idx]->flags & BSF_GLOBAL)
1705 sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
1708 if (syms[idx]->section)
1710 elf_section_from_bfd_section(abfd,
1711 syms[idx]->section->output_section);
1713 sym.st_shndx = SHN_UNDEF;
1715 elf_swap_symbol_out (abfd, &sym, outbound_syms+idx+1);
1718 /* fill in 0th symbol */
1719 Elf_Internal_Sym sym;
1725 sym.st_shndx = SHN_UNDEF;
1726 elf_swap_symbol_out (abfd, &sym, outbound_syms);
1728 symtab_hdr->contents = (void*)outbound_syms;
1729 symstrtab_hdr->contents = (void*)stt->tab;
1730 symstrtab_hdr->sh_size = stt->length;
1733 /* put the strtab out too... */
1735 Elf_Internal_Shdr *this_hdr;
1738 this_section = i_ehdrp->e_shnum++;
1739 i_ehdrp->e_shstrndx = this_section;
1740 this_hdr = &i_shdrp[this_section];
1741 this_hdr->sh_name = bfd_add_to_strtab (abfd, shstrtab, ".shstrtab");
1742 this_hdr->sh_size = shstrtab->length;
1743 this_hdr->contents = (void*)shstrtab->tab;
1746 outbase = i_ehdrp->e_ehsize;
1748 /* swap the header before spitting it out... */
1749 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1750 bfd_seek (abfd, 0L, SEEK_SET);
1751 bfd_write ((PTR) &x_ehdr, sizeof(x_ehdr), 1, abfd);
1753 outbase += i_ehdrp->e_shentsize * i_ehdrp->e_shnum;
1755 /* now we fix up the offsets... */
1756 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1758 i_shdrp[count].sh_offset = outbase;
1759 outbase += i_shdrp[count].sh_size;
1762 /* at this point we've concocted all the ELF sections... */
1763 x_shdrp = (Elf_External_Shdr *)
1764 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
1767 bfd_error = no_memory;
1771 fprintf(stderr, "ELF>> total sections: %d\n", i_ehdrp->e_shnum);
1772 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1774 elf_swap_shdr_out (abfd, i_shdrp+count, x_shdrp+count);
1776 bfd_write ((PTR) x_shdrp, sizeof(*x_shdrp), i_ehdrp->e_shnum, abfd);
1777 /* need to dump the string table too... */
1779 /* after writing the headers, we need to write the sections too... */
1780 nsect = abfd->sections;
1781 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1783 if(i_shdrp[count].contents)
1785 fprintf(stderr, "found some userdata: count %d, pos 0x%x\n",
1786 count, i_shdrp[count].sh_offset);
1787 bfd_seek (abfd, i_shdrp[count].sh_offset, SEEK_SET);
1788 bfd_write (i_shdrp[count].contents, i_shdrp[count].sh_size, 1, abfd);
1792 /* sample use of bfd:
1793 * bfd_seek (abfd, 0L, false);
1794 * bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
1795 * if (bfd_seek(abfd, scn_base, SEEK_SET) != 0)
1797 * old = bfd_tell(abfd);
1804 /* Given an index of a section, retrieve a pointer to it. Note
1805 that for our purposes, sections are indexed by {1, 2, ...} with
1806 0 being an illegal index. */
1808 /* In the original, each ELF section went into exactly one BFD
1809 section. This doesn't really make sense, so we need a real mapping.
1810 The mapping has to hide in the Elf_Internal_Shdr since asection
1811 doesn't have anything like a tdata field... */
1814 DEFUN (section_from_elf_index, (abfd, index),
1818 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
1819 Elf_Internal_Shdr *hdr = i_shdrp + index;
1821 switch (hdr->sh_type)
1823 /* ELF sections that map to BFD sections */
1827 bfd_section_from_shdr (abfd, index);
1828 return (struct sec *)hdr->rawdata;
1835 /* given a section, search the header to find them... */
1837 DEFUN (elf_section_from_bfd_section, (abfd, asect),
1841 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
1843 Elf_Internal_Shdr *hdr;
1844 int maxindex = elf_elfheader (abfd)->e_shnum;
1846 for(index = 0; index < maxindex; index++) {
1847 hdr = &i_shdrp[index];
1848 switch (hdr->sh_type)
1850 /* ELF sections that map to BFD sections */
1855 if (((struct sec *)(hdr->rawdata)) == asect)
1867 DEFUN (elf_slurp_symbol_table, (abfd, hdr),
1869 Elf_Internal_Shdr *hdr)
1871 int symcount; /* Number of external ELF symbols */
1872 char *strtab; /* Buffer for raw ELF string table section */
1873 asymbol *sym; /* Pointer to current bfd symbol */
1874 asymbol *symbase; /* Buffer for generated bfd symbols */
1875 asymbol **vec; /* Pointer to current bfd symbol pointer */
1876 Elf_Internal_Sym i_sym;
1877 Elf_External_Sym x_sym;
1878 Elf_External_Sym *x_symp;
1879 unsigned int *table_ptr; /* bfd symbol translation table */
1881 /* this is only valid because there is only one symtab... */
1882 if (bfd_get_outsymbols (abfd) != NULL)
1887 /* Read each raw ELF symbol, converting from external ELF form to
1888 internal ELF form, and then using the information to create a
1889 canonical bfd symbol table entry.
1891 Note that we allocate the initial bfd canonical symbol buffer
1892 based on a one-to-one mapping of the ELF symbols to canonical
1893 symbols. However, it is likely that not all the ELF symbols will
1894 be used, so there will be some space leftover at the end. Once
1895 we know how many symbols we actual generate, we realloc the buffer
1896 to the correct size and then build the pointer vector. */
1898 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
1900 bfd_error = system_call_error;
1904 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1905 sym = symbase = (asymbol *) bfd_zalloc (abfd, symcount * sizeof (asymbol));
1906 x_symp = (Elf_External_Sym *)
1907 bfd_zalloc (abfd, symcount * sizeof (Elf_External_Sym));
1909 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
1910 != symcount * sizeof (Elf_External_Sym))
1912 bfd_error = system_call_error;
1915 while (symcount-- > 0)
1917 elf_swap_symbol_in (abfd, x_symp + symcount, &i_sym);
1918 if (i_sym.st_name > 0)
1920 sym -> the_bfd = abfd;
1921 sym -> name = elf_string_from_elf_section(abfd, hdr->sh_link,
1923 sym -> value = i_sym.st_value;
1924 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
1926 sym -> section = section_from_elf_index (abfd, i_sym.st_shndx);
1928 else if (i_sym.st_shndx == SHN_ABS)
1930 sym -> section = &bfd_abs_section;
1932 else if (i_sym.st_shndx == SHN_COMMON)
1934 sym -> section = &bfd_com_section;
1936 else if (i_sym.st_shndx == SHN_UNDEF)
1938 sym -> section = &bfd_und_section;
1941 switch (ELF_ST_BIND (i_sym.st_info))
1944 sym -> flags |= BSF_LOCAL;
1947 sym -> flags |= (BSF_GLOBAL | BSF_EXPORT);
1950 sym -> flags |= BSF_WEAK;
1957 /* let's try *not* punting unnamed symbols... */
1958 sym -> the_bfd = abfd;
1959 sym -> name = "unnamed"; /* perhaps should include the number? */
1960 sym -> value = i_sym.st_value;
1961 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
1963 sym -> section = section_from_elf_index (abfd, i_sym.st_shndx);
1965 else if (i_sym.st_shndx == SHN_ABS)
1967 sym -> section = &bfd_abs_section;
1969 else if (i_sym.st_shndx == SHN_COMMON)
1971 sym -> section = &bfd_com_section;
1973 else if (i_sym.st_shndx == SHN_UNDEF)
1975 sym -> section = &bfd_und_section;
1978 switch (ELF_ST_BIND (i_sym.st_info))
1981 sym -> flags |= BSF_LOCAL;
1984 sym -> flags |= (BSF_GLOBAL | BSF_EXPORT);
1987 sym -> flags |= BSF_WEAK;
1995 bfd_get_symcount(abfd) = symcount = sym - symbase;
1996 sym = symbase = (asymbol *)
1997 bfd_realloc (abfd, symbase, symcount * sizeof (asymbol));
1998 bfd_get_outsymbols(abfd) = vec = (asymbol **)
1999 bfd_alloc (abfd, symcount * sizeof (asymbol *));
2001 while (symcount-- > 0)
2009 /* Return the number of bytes required to hold the symtab vector.
2011 Note that we base it on the count plus 1, since we will null terminate
2012 the vector allocated based on this size. */
2015 DEFUN (elf_get_symtab_upper_bound, (abfd), bfd *abfd)
2017 unsigned int symtab_size = 0;
2019 /* if (elf_slurp_symbol_table (abfd, ???)) */
2021 symtab_size = (bfd_get_symcount (abfd) + 1) * (sizeof (asymbol));
2023 return (symtab_size);
2027 This function return the number of bytes required to store the
2028 relocation information associated with section <<sect>>
2029 attached to bfd <<abfd>>
2033 elf_get_reloc_upper_bound (abfd, asect)
2037 if (asect->flags & SEC_RELOC)
2039 /* either rel or rela */
2040 return asect->_raw_size;
2046 /* FIXME!!! sparc howto should go into elf-32-sparc.c */
2051 R_SPARC_8, R_SPARC_16, R_SPARC_32,
2052 R_SPARC_DISP8, R_SPARC_DISP16, R_SPARC_DISP32,
2053 R_SPARC_WDISP30, R_SPARC_WDISP22,
2054 R_SPARC_HI22, R_SPARC_22,
2055 R_SPARC_13, R_SPARC_LO10,
2056 R_SPARC_GOT10, R_SPARC_GOT13, R_SPARC_GOT22,
2057 R_SPARC_PC10, R_SPARC_PC22,
2060 R_SPARC_GLOB_DAT, R_SPARC_JMP_SLOT,
2065 #define RELOC_TYPE_NAMES \
2067 "R_SPARC_8", "R_SPARC_16", "R_SPARC_32", \
2068 "R_SPARC_DISP8", "R_SPARC_DISP16", "R_SPARC_DISP32", \
2069 "R_SPARC_WDISP30", "R_SPARC_WDISP22", \
2070 "R_SPARC_HI22", "R_SPARC_22", \
2071 "R_SPARC_13", "R_SPARC_LO10", \
2072 "R_SPARC_GOT10", "R_SPARC_GOT13", "R_SPARC_GOT22", \
2073 "R_SPARC_PC10", "R_SPARC_PC22", \
2076 "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT", \
2077 "R_SPARC_RELATIVE", \
2080 static reloc_howto_type elf_howto_table[] =
2082 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,false,false, 0,"R_SPARC_NONE", false,0,0x00000000,false),
2083 HOWTO(R_SPARC_8, 0,0, 8,false,0,true, true, 0,"R_SPARC_8", false,0,0x000000ff,false),
2084 HOWTO(R_SPARC_16, 0,1,16,false,0,true, true, 0,"R_SPARC_16", false,0,0x0000ffff,false),
2085 HOWTO(R_SPARC_32, 0,2,32,false,0,true, true, 0,"R_SPARC_32", false,0,0xffffffff,false),
2086 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,false, true, 0,"R_SPARC_DISP8", false,0,0x000000ff,false),
2087 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,false, true, 0,"R_SPARC_DISP16", false,0,0x0000ffff,false),
2088 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,false, true, 0,"R_SPARC_DISP32", false,0,0x00ffffff,false),
2089 HOWTO(R_SPARC_WDISP30,2,2,30,true, 0,false, true, 0,"R_SPARC_WDISP30",false,0,0x3fffffff,false),
2090 HOWTO(R_SPARC_WDISP22,2,2,22,true, 0,false, true, 0,"R_SPARC_WDISP22",false,0,0x003fffff,false),
2091 HOWTO(R_SPARC_HI22, 10,2,22,false,0,true, false, 0,"R_SPARC_HI22", false,0,0x003fffff,false),
2092 HOWTO(R_SPARC_22, 0,2,22,false,0,true, true, 0,"R_SPARC_22", false,0,0x003fffff,false),
2093 HOWTO(R_SPARC_13, 0,1,13,false,0,true, true, 0,"R_SPARC_13", false,0,0x00001fff,false),
2094 HOWTO(R_SPARC_LO10, 0,1,10,false,0,true, false, 0,"R_SPARC_LO10", false,0,0x000003ff,false),
2095 HOWTO(R_SPARC_GOT10, 0,1,10,false,0,false, true, 0,"R_SPARC_GOT10", false,0,0x000003ff,false),
2096 HOWTO(R_SPARC_GOT13, 0,1,13,false,0,false, true, 0,"R_SPARC_GOT13", false,0,0x00001fff,false),
2097 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,false, true, 0,"R_SPARC_GOT22", false,0,0x003fffff,false),
2098 HOWTO(R_SPARC_PC10, 0,1,10,false,0,true, true, 0,"R_SPARC_PC10", false,0,0x000003ff,false),
2099 HOWTO(R_SPARC_PC22, 0,2,22,false,0,true, true, 0,"R_SPARC_PC22", false,0,0x003fffff,false),
2100 HOWTO(R_SPARC_WPLT30, 0,0,00,false,0,false,false, 0,"R_SPARC_WPLT30", false,0,0x00000000,false),
2101 HOWTO(R_SPARC_COPY, 0,0,00,false,0,false,false, 0,"R_SPARC_COPY", false,0,0x00000000,false),
2102 HOWTO(R_SPARC_GLOB_DAT,0,0,00,false,0,false,false,0,"R_SPARC_GLOB_DAT",false,0,0x00000000,false),
2103 HOWTO(R_SPARC_JMP_SLOT,0,0,00,false,0,false,false,0,"R_SPARC_JMP_SLOT",false,0,0x00000000,false),
2104 HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,false,false,0,"R_SPARC_RELATIVE",false,0,0x00000000,false),
2105 HOWTO(R_SPARC_UA32, 0,0,00,false,0,false,false,0,"R_SPARC_UA32", false,0,0x00000000,false),
2110 DEFUN(elf_info_to_howto, (abfd, cache_ptr, dst),
2112 arelent *cache_ptr AND
2113 Elf_Internal_Rela *dst)
2115 /* FIXME!!! just doing sparc for now... */
2117 BFD_ASSERT (ELF_R_TYPE(dst->r_info) < 24);
2119 cache_ptr->howto = &elf_howto_table[ELF_R_TYPE(dst->r_info)];
2121 fprintf (stderr, "elf_info_to_howto not implemented\n");
2127 DEFUN(elf_slurp_reloca_table,(abfd, asect, symbols),
2132 Elf_External_Rela *native_relocs;
2133 arelent *reloc_cache;
2138 if (asect->relocation)
2140 if (asect->reloc_count == 0)
2142 if (asect->flags & SEC_CONSTRUCTOR)
2144 /* if (!elf_slurp_symbol_table(abfd))
2145 return false; -- should be done by now */
2147 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
2148 native_relocs = (Elf_External_Rela *)
2149 bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela));
2150 fprintf(stderr, "ELF>> really reading %d relocs for section %s\n",
2151 asect->reloc_count, asect->name);
2152 bfd_read ((PTR) native_relocs,
2153 sizeof(Elf_External_Rela), asect->reloc_count, abfd);
2155 reloc_cache = (arelent *)
2156 bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent)));
2158 if (! reloc_cache) {
2159 bfd_error = no_memory;
2163 for (idx = 0; idx < asect->reloc_count; idx ++)
2165 #ifdef RELOC_PROCESSING
2166 /* sparc, 68k, 88k, 860 use rela only. */
2167 /* 386 and we32000 use rel only... fix it for them later. */
2168 Elf_Internal_Rela dst;
2169 Elf_External_Rela *src;
2171 cache_ptr = reloc_cache + idx;
2172 src = native_relocs + idx;
2173 elf_swap_reloca_in(abfd, src, &dst);
2175 RELOC_PROCESSING(cache_ptr, &dst, symbols, abfd, asect);
2177 Elf_Internal_Rela dst;
2179 Elf_External_Rela *src;
2181 cache_ptr = reloc_cache + idx;
2182 src = native_relocs + idx;
2184 elf_swap_reloca_in(abfd, src, &dst);
2186 if(asect->flags & SEC_RELOC)
2188 /* relocatable, so the offset is off of the section */
2189 cache_ptr->address = dst.r_offset + asect->vma;
2193 /* non-relocatable, so the offset a virtual address */
2194 cache_ptr->address = dst.r_offset;
2196 /* ELF_R_SYM(dst.r_info) is the symbol table offset... */
2197 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM(dst.r_info);
2198 cache_ptr->addend = dst.r_addend;
2199 /* ptr = *(cache_ptr->sym_ptr_ptr); */
2201 /* Fill in the cache_ptr->howto field from dst.r_type */
2202 elf_info_to_howto(abfd, cache_ptr, &dst);
2206 asect->relocation = reloc_cache;
2212 elf_canonicalize_reloc (abfd, section, relptr, symbols)
2218 arelent *tblptr = section->relocation;
2219 unsigned int count = 0;
2221 /* snarfed from coffcode.h */
2222 /* FIXME: this could be reloc... */
2223 elf_slurp_reloca_table(abfd, section, symbols);
2225 tblptr = section->relocation;
2229 for (; count++ < section->reloc_count;)
2230 *relptr++ = tblptr++;
2233 return section->reloc_count;
2237 DEFUN (elf_get_symtab, (abfd, alocation),
2239 asymbol **alocation)
2241 unsigned int symcount;
2244 /* if (!elf_slurp_symbol_table (abfd))
2248 symcount = bfd_get_symcount (abfd);
2249 vec = bfd_get_outsymbols (abfd);
2250 while (symcount-- > 0)
2252 *alocation++ = *vec++;
2254 *alocation++ = NULL;
2255 return (bfd_get_symcount (abfd));
2260 DEFUN (elf_make_empty_symbol, (abfd),
2263 elf_symbol_type *newsym;
2265 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
2268 bfd_error = no_memory;
2273 newsym -> symbol.the_bfd = abfd;
2274 return (&newsym -> symbol);
2279 DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how),
2280 bfd *ignore_abfd AND
2283 bfd_print_symbol_type how)
2285 FILE *file = (FILE *)filep;
2288 case bfd_print_symbol_name:
2289 fprintf(file, "%s", symbol->name);
2291 case bfd_print_symbol_more:
2292 fprintf(file, "elf %lx %lx",
2296 case bfd_print_symbol_nm:
2297 case bfd_print_symbol_all:
2300 section_name = symbol->section? symbol->section->name : "(*none*)";
2301 bfd_print_symbol_vandf((PTR) file, symbol);
2302 fprintf(file, " %-5s %s %s %s",
2313 DEFUN (elf_get_lineno,(ignore_abfd, symbol),
2314 bfd *ignore_abfd AND
2317 fprintf (stderr, "elf_get_lineno unimplemented\n");
2324 DEFUN (elf_set_arch_mach,(abfd, arch, machine),
2326 enum bfd_architecture arch AND
2327 unsigned long machine)
2329 /* Allow any architecture to be supported by the elf backend */
2332 case bfd_arch_unknown: /* EM_NONE */
2333 case bfd_arch_sparc: /* EM_SPARC */
2334 case bfd_arch_i386: /* EM_386 */
2335 case bfd_arch_m68k: /* EM_68K */
2336 case bfd_arch_m88k: /* EM_88K */
2337 case bfd_arch_i860: /* EM_860 */
2338 case bfd_arch_mips: /* EM_MIPS (MIPS R3000) */
2339 return bfd_default_set_arch_mach(abfd, arch, machine);
2346 DEFUN (elf_find_nearest_line,(abfd,
2354 asection *section AND
2355 asymbol **symbols AND
2357 CONST char **filename_ptr AND
2358 CONST char **functionname_ptr AND
2359 unsigned int *line_ptr)
2361 fprintf (stderr, "elf_find_nearest_line unimplemented\n");
2368 DEFUN (elf_sizeof_headers, (abfd, reloc),
2372 fprintf (stderr, "elf_sizeof_headers unimplemented\n");
2379 DEFUN(elf_set_section_contents, (abfd, section, location, offset, count),
2384 bfd_size_type count)
2388 if (abfd->output_has_begun == false) /* set by bfd.c handler? */
2390 /* do setup calculations (FIXME) */
2391 elf_compute_section_file_positions(abfd);
2394 if(bfd_seek (abfd, (file_ptr)section->filepos + offset, SEEK_SET) == -1)
2396 if(bfd_write (location, (bfd_size_type)1, count, abfd) != count)
2399 /* we really just need to save the contents away... */
2400 dest_sect = elf_section_from_bfd_section(abfd, section);
2404 /* FIXME: allocate in set_section_size, then copy in here... */
2405 contents = (void*)bfd_alloc(abfd, count);
2406 BFD_ASSERT(contents);
2407 memcpy(contents, location, count);
2408 elf_elfsections (abfd)[dest_sect].contents = contents;
2414 /* This structure contains everything that BFD knows about a target.
2415 It includes things like its byte order, name, what routines to call
2416 to do various operations, etc. Every BFD points to a target structure
2417 with its "xvec" member.
2419 There are two such structures here: one for big-endian machines and
2420 one for little-endian machines. */
2422 /* Archives are generic or unimplemented. */
2423 #define elf_slurp_armap bfd_false
2424 #define elf_slurp_extended_name_table _bfd_slurp_extended_name_table
2425 #define elf_truncate_arname bfd_dont_truncate_arname
2426 #define elf_openr_next_archived_file bfd_generic_openr_next_archived_file
2427 #define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt
2428 #define elf_write_armap (PROTO (boolean, (*), \
2429 (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count, \
2430 int stridx))) bfd_false
2432 /* Ordinary section reading and writing */
2433 #define elf_new_section_hook _bfd_dummy_new_section_hook
2434 #define elf_get_section_contents bfd_generic_get_section_contents
2435 /* #define elf_set_section_contents bfd_generic_set_section_contents */
2436 #define elf_close_and_cleanup bfd_generic_close_and_cleanup
2438 #define elf_bfd_debug_info_start bfd_void
2439 #define elf_bfd_debug_info_end bfd_void
2440 #define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
2441 #define elf_bfd_get_relocated_section_contents \
2442 bfd_generic_get_relocated_section_contents
2443 #define elf_bfd_relax_section bfd_generic_relax_section
2444 bfd_target elf_big_vec =
2446 /* name: identify kind of target */
2449 /* flavour: general indication about file */
2450 bfd_target_elf_flavour,
2452 /* byteorder_big_p: data is big endian */
2455 /* header_byteorder_big_p: header is also big endian */
2458 /* object_flags: mask of all file flags */
2459 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
2462 /* section_flags: mask of all section flags */
2463 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
2464 SEC_CODE | SEC_DATA),
2466 /* ar_pad_char: pad character for filenames within an archive header
2467 FIXME: this really has nothing to do with ELF, this is a characteristic
2468 of the archiver and/or os and should be independently tunable */
2471 /* ar_max_namelen: maximum number of characters in an archive header
2472 FIXME: this really has nothing to do with ELF, this is a characteristic
2473 of the archiver and should be independently tunable. This value is
2474 a WAG (wild a** guess) */
2477 /* align_power_min: minimum alignment restriction for any section
2478 FIXME: this value may be target machine dependent */
2481 /* Routines to byte-swap various sized integers from the data sections */
2482 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
2484 /* Routines to byte-swap various sized integers from the file headers */
2485 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
2487 /* bfd_check_format: check the format of a file being read */
2488 { _bfd_dummy_target, /* unknown format */
2489 elf_object_p, /* assembler/linker output (object file) */
2490 bfd_generic_archive_p, /* an archive */
2491 elf_core_file_p /* a core file */
2494 /* bfd_set_format: set the format of a file being written */
2497 _bfd_generic_mkarchive,
2501 /* bfd_write_contents: write cached information into a file being written */
2503 elf_write_object_contents,
2504 _bfd_write_archive_contents,
2508 /* Initialize a jump table with the standard macro. All names start
2516 bfd_target elf_little_vec =
2518 /* name: identify kind of target */
2521 /* flavour: general indication about file */
2522 bfd_target_elf_flavour,
2524 /* byteorder_big_p: data is big endian */
2525 false, /* Nope -- this one's little endian */
2527 /* header_byteorder_big_p: header is also big endian */
2528 false, /* Nope -- this one's little endian */
2530 /* object_flags: mask of all file flags */
2531 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
2534 /* section_flags: mask of all section flags */
2535 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
2538 /* ar_pad_char: pad character for filenames within an archive header
2539 FIXME: this really has nothing to do with ELF, this is a characteristic
2540 of the archiver and/or os and should be independently tunable */
2543 /* ar_max_namelen: maximum number of characters in an archive header
2544 FIXME: this really has nothing to do with ELF, this is a characteristic
2545 of the archiver and should be independently tunable. This value is
2546 a WAG (wild a** guess) */
2549 /* align_power_min: minimum alignment restriction for any section
2550 FIXME: this value may be target machine dependent */
2553 /* Routines to byte-swap various sized integers from the data sections */
2554 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
2556 /* Routines to byte-swap various sized integers from the file headers */
2557 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
2559 /* bfd_check_format: check the format of a file being read */
2560 { _bfd_dummy_target, /* unknown format */
2561 elf_object_p, /* assembler/linker output (object file) */
2562 bfd_generic_archive_p, /* an archive */
2563 elf_core_file_p /* a core file */
2566 /* bfd_set_format: set the format of a file being written */
2569 _bfd_generic_mkarchive,
2573 /* bfd_write_contents: write cached information into a file being written */
2575 elf_write_object_contents,
2576 _bfd_write_archive_contents,
2580 /* Initialize a jump table with the standard macro. All names start