1 /* ELF executable support for BFD.
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
4 Written by Fred Fish @ Cygnus Support, from information published
5 in "UNIX System V Release 4, Programmers Guide: ANSI C and
6 Programming Support Tools". Sufficient support for gdb.
8 Rewritten by Mark Eichin @ Cygnus Support, from information
9 published in "System V Application Binary Interface", chapters 4
10 and 5, as well as the various "Processor Supplement" documents
11 derived from it. Added support for assembler and other object file
14 This file is part of BFD, the Binary File Descriptor library.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /****************************************
35 This is only a partial ELF implementation,
36 incorporating only those parts that are
37 required to get gdb up and running. It is
38 expected that it will be expanded to a full
39 ELF implementation at some future date.
41 Unimplemented stubs call abort() to ensure
42 that they get proper attention if they are
43 ever called. The stubs are here since
44 this version was hacked from the COFF
45 version, and thus they will probably
46 go away or get expanded appropriately in a
51 *****************************************/
54 /* Problems and other issues to resolve.
56 (1) BFD expects there to be some fixed number of "sections" in
57 the object file. I.E. there is a "section_count" variable in the
58 bfd structure which contains the number of sections. However, ELF
59 supports multiple "views" of a file. In particular, with current
60 implementations, executable files typically have two tables, a
61 program header table and a section header table, both of which
62 partition the executable.
64 In ELF-speak, the "linking view" of the file uses the section header
65 table to access "sections" within the file, and the "execution view"
66 uses the program header table to access "segments" within the file.
67 "Segments" typically may contain all the data from one or more
70 Note that the section header table is optional in ELF executables,
71 but it is this information that is most useful to gdb. If the
72 section header table is missing, then gdb should probably try
73 to make do with the program header table. (FIXME)
81 #include "elf/common.h"
82 #include "elf/internal.h"
83 #include "elf/external.h"
85 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
86 #include <sys/procfs.h>
88 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
89 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
90 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
93 /* Forward data declarations */
95 extern bfd_target elf_little_vec, elf_big_vec;
97 /* Currently the elf_symbol_type struct just contains the generic bfd
105 /* Some private data is stashed away for future use using the tdata pointer
106 in the bfd structure. */
110 Elf_Internal_Ehdr elf_header[1]; /* Actual data, but ref like ptr */
111 Elf_Internal_Shdr *elf_sect_ptr;
112 struct strtab *strtab_ptr;
114 void *prstatus; /* The raw /proc prstatus structure */
115 void *prpsinfo; /* The raw /proc prpsinfo structure */
118 #define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data)
119 #define elf_elfheader(bfd) (elf_tdata(bfd) -> elf_header)
120 #define elf_elfsections(bfd) (elf_tdata(bfd) -> elf_sect_ptr)
121 #define elf_shstrtab(bfd) (elf_tdata(bfd) -> strtab_ptr)
122 #define elf_onesymtab(bfd) (elf_tdata(bfd) -> symtab_section)
123 #define core_prpsinfo(bfd) (elf_tdata(bfd) -> prpsinfo)
124 #define core_prstatus(bfd) (elf_tdata(bfd) -> prstatus)
126 /* Translate an ELF symbol in external format into an ELF symbol in internal
130 DEFUN(elf_swap_symbol_in,(abfd, src, dst),
132 Elf_External_Sym *src AND
133 Elf_Internal_Sym *dst)
135 dst -> st_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_name);
136 dst -> st_value = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_value);
137 dst -> st_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_size);
138 dst -> st_info = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_info);
139 dst -> st_other = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_other);
140 dst -> st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> st_shndx);
143 /* Translate an ELF symbol in internal format into an ELF symbol in external
147 DEFUN(elf_swap_symbol_out,(abfd, src, dst),
149 Elf_Internal_Sym *src AND
150 Elf_External_Sym *dst)
152 bfd_h_put_32 (abfd, src->st_name, dst->st_name);
153 bfd_h_put_32 (abfd, src->st_value, dst->st_value);
154 bfd_h_put_32 (abfd, src->st_size, dst->st_size);
155 bfd_h_put_8 (abfd, src->st_info, dst->st_info);
156 bfd_h_put_8 (abfd, src->st_other, dst->st_other);
157 bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
161 /* Translate an ELF file header in external format into an ELF file header in
165 DEFUN(elf_swap_ehdr_in,(abfd, src, dst),
167 Elf_External_Ehdr *src AND
168 Elf_Internal_Ehdr *dst)
170 memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT);
171 dst -> e_type = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_type);
172 dst -> e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_machine);
173 dst -> e_version = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_version);
174 dst -> e_entry = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_entry);
175 dst -> e_phoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_phoff);
176 dst -> e_shoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_shoff);
177 dst -> e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_flags);
178 dst -> e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_ehsize);
179 dst -> e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phentsize);
180 dst -> e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phnum);
181 dst -> e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shentsize);
182 dst -> e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shnum);
183 dst -> e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shstrndx);
186 /* Translate an ELF file header in internal format into an ELF file header in
190 DEFUN(elf_swap_ehdr_out,(abfd, src, dst),
192 Elf_Internal_Ehdr *src AND
193 Elf_External_Ehdr *dst)
195 memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT);
196 /* note that all elements of dst are *arrays of unsigned char* already... */
197 bfd_h_put_16 (abfd, src->e_type, dst->e_type);
198 bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
199 bfd_h_put_32 (abfd, src->e_version, dst->e_version);
200 bfd_h_put_32 (abfd, src->e_entry, dst->e_entry);
201 bfd_h_put_32 (abfd, src->e_phoff, dst->e_phoff);
202 bfd_h_put_32 (abfd, src->e_shoff, dst->e_shoff);
203 bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
204 bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
205 bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
206 bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
207 bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
208 bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
209 bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
213 /* Translate an ELF section header table entry in external format into an
214 ELF section header table entry in internal format. */
217 DEFUN(elf_swap_shdr_in,(abfd, src, dst),
219 Elf_External_Shdr *src AND
220 Elf_Internal_Shdr *dst)
222 dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
223 dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
224 dst->sh_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_flags);
225 dst->sh_addr = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_addr);
226 dst->sh_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_offset);
227 dst->sh_size = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_size);
228 dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
229 dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
230 dst->sh_addralign = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_addralign);
231 dst->sh_entsize = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_entsize);
232 /* we haven't done any processing on it yet, so... */
233 dst->rawdata = (void*)0;
236 /* Translate an ELF section header table entry in internal format into an
237 ELF section header table entry in external format. */
240 DEFUN(elf_swap_shdr_out,(abfd, src, dst),
242 Elf_Internal_Shdr *src AND
243 Elf_External_Shdr *dst)
245 /* note that all elements of dst are *arrays of unsigned char* already... */
246 bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
247 bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
248 bfd_h_put_32 (abfd, src->sh_flags, dst->sh_flags);
249 bfd_h_put_32 (abfd, src->sh_addr, dst->sh_addr);
250 bfd_h_put_32 (abfd, src->sh_offset, dst->sh_offset);
251 bfd_h_put_32 (abfd, src->sh_size, dst->sh_size);
252 bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
253 bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
254 bfd_h_put_32 (abfd, src->sh_addralign, dst->sh_addralign);
255 bfd_h_put_32 (abfd, src->sh_entsize, dst->sh_entsize);
259 /* Translate an ELF program header table entry in external format into an
260 ELF program header table entry in internal format. */
263 DEFUN(elf_swap_phdr_in,(abfd, src, dst),
265 Elf_External_Phdr *src AND
266 Elf_Internal_Phdr *dst)
268 dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
269 dst->p_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->p_offset);
270 dst->p_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) src->p_vaddr);
271 dst->p_paddr = bfd_h_get_32 (abfd, (bfd_byte *) src->p_paddr);
272 dst->p_filesz = bfd_h_get_32 (abfd, (bfd_byte *) src->p_filesz);
273 dst->p_memsz = bfd_h_get_32 (abfd, (bfd_byte *) src->p_memsz);
274 dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
275 dst->p_align = bfd_h_get_32 (abfd, (bfd_byte *) src->p_align);
279 /* Translate an ELF reloc from external format to internal format. */
281 DEFUN(elf_swap_reloc_in,(abfd, src, dst),
283 Elf_External_Rel *src AND
284 Elf_Internal_Rel *dst)
286 dst->r_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->r_offset);
287 dst->r_info = bfd_h_get_32 (abfd, (bfd_byte *) src->r_info);
291 DEFUN(elf_swap_reloca_in,(abfd, src, dst),
293 Elf_External_Rela *src AND
294 Elf_Internal_Rela *dst)
296 dst->r_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->r_offset);
297 dst->r_info = bfd_h_get_32 (abfd, (bfd_byte *) src->r_info);
298 dst->r_addend = bfd_h_get_32 (abfd, (bfd_byte *) src->r_addend);
301 /* Translate an ELF reloc from internal format to external format. */
303 DEFUN(elf_swap_reloc_out,(abfd, src, dst),
305 Elf_Internal_Rel *src AND
306 Elf_External_Rel *dst)
308 bfd_h_put_32 (abfd, src->r_offset, dst->r_offset);
309 bfd_h_put_32 (abfd, src->r_info, dst->r_info);
313 DEFUN(elf_swap_reloca_out,(abfd, src, dst),
315 Elf_Internal_Rela *src AND
316 Elf_External_Rela *dst)
318 bfd_h_put_32 (abfd, src->r_offset, dst->r_offset);
319 bfd_h_put_32 (abfd, src->r_info, dst->r_info);
320 bfd_h_put_32 (abfd, src->r_addend, dst->r_addend);
323 static char *EXFUN(elf_read, (bfd *, long, int));
324 static struct sec * EXFUN(section_from_elf_index, (bfd *, int));
325 static int EXFUN(elf_section_from_bfd_section, (bfd *, struct sec *));
326 static boolean EXFUN(elf_slurp_symbol_table, (bfd *, asymbol **));
327 static void EXFUN(elf_info_to_howto, (bfd *, arelent *, Elf_Internal_Rela *));
328 static char *EXFUN(elf_get_str_section, (bfd *, unsigned int));
335 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
338 Helper functions for GDB to locate the string tables.
339 Since BFD hides string tables from callers, GDB needs to use an
340 internal hook to find them. Sun's .stabstr, in particular,
341 isn't even pointed to by the .stab section, so ordinary
342 mechanisms wouldn't work to find it, even if we had some.
345 struct elf_internal_shdr *
346 DEFUN(bfd_elf_find_section, (abfd, name),
350 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
351 char *shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
352 unsigned int max = elf_elfheader (abfd)->e_shnum;
355 for (i = 1; i < max; i++)
356 if (!strcmp (&shstrtab[i_shdrp[i].sh_name], name))
361 /* End of GDB support. */
364 DEFUN(elf_get_str_section, (abfd, shindex),
366 unsigned int shindex)
368 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
369 unsigned int shstrtabsize = i_shdrp[shindex].sh_size;
370 unsigned int offset = i_shdrp[shindex].sh_offset;
371 char *shstrtab = i_shdrp[shindex].rawdata;
376 if ((shstrtab = elf_read (abfd, offset, shstrtabsize)) == NULL)
380 i_shdrp[shindex].rawdata = (void*)shstrtab;
385 DEFUN(elf_string_from_elf_section, (abfd, shindex, strindex),
387 unsigned int shindex AND
388 unsigned int strindex)
390 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
391 Elf_Internal_Shdr *hdr = i_shdrp + shindex;
395 if (elf_get_str_section (abfd, shindex) == NULL)
400 return ((char*)hdr->rawdata)+strindex;
403 #define elf_string_from_elf_strtab(abfd, strindex) \
404 elf_string_from_elf_section (abfd, elf_elfheader(abfd)->e_shstrndx, strindex)
406 /* Create a new bfd section from an ELF section header. */
409 DEFUN(bfd_section_from_shdr, (abfd, shindex),
411 unsigned int shindex)
413 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
414 Elf_Internal_Shdr *hdr = i_shdrp + shindex;
418 name = hdr->sh_name ?
419 elf_string_from_elf_strtab (abfd, hdr->sh_name) : "unnamed";
421 switch(hdr->sh_type) {
424 /* inactive section. Throw it away. */
430 /* Bits that get saved. This one is real. */
433 newsect = bfd_make_section (abfd, name);
434 newsect->vma = hdr->sh_addr;
435 newsect->_raw_size = hdr->sh_size;
436 newsect->filepos = hdr->sh_offset; /* so we can read back the bits */
437 newsect->flags |= SEC_HAS_CONTENTS;
439 if (hdr->sh_flags & SHF_ALLOC)
441 newsect->flags |= SEC_ALLOC;
442 if (hdr->sh_type != SHT_NOBITS)
443 newsect->flags |= SEC_LOAD;
446 if (!(hdr->sh_flags & SHF_WRITE))
447 newsect->flags |= SEC_READONLY;
449 if (hdr->sh_flags & SHF_EXECINSTR)
450 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
452 newsect->flags |= SEC_DATA;
454 hdr->rawdata = (void*)newsect;
459 case SHT_SYMTAB: /* A symbol table */
460 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
461 elf_onesymtab (abfd) = shindex;
462 abfd->flags |= HAS_SYMS;
465 case SHT_STRTAB: /* A string table */
470 /* *these* do a lot of work -- but build no sections! */
471 /* the spec says there can be multiple strtabs, but only one symtab */
472 /* but there can be lots of REL* sections. */
473 /* FIXME: The above statement is wrong! There are typically at least
474 two symbol tables in a dynamically linked executable, ".dynsym"
475 which is the dynamic linkage symbol table and ".symtab", which is
476 the "traditional" symbol table. -fnf */
479 asection *target_sect;
482 bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */
483 bfd_section_from_shdr (abfd, hdr->sh_info); /* target */
484 target_sect = section_from_elf_index (abfd, hdr->sh_info);
485 if (target_sect == NULL)
489 /* FIXME: We are only prepared to read one symbol table, so
490 do NOT read the dynamic symbol table since it is only a
491 subset of the full symbol table. Also see comment above. -fnf */
492 if (!elf_slurp_symbol_table(abfd, i_shdrp + hdr->sh_link))
496 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
497 target_sect->flags |= SEC_RELOC;
498 target_sect->relocation = 0;
499 target_sect->rel_filepos = hdr->sh_offset;
506 case SHT_DYNSYM: /* could treat this like symtab... */
508 fprintf(stderr, "Dynamic Linking sections not yet supported.\n");
515 fprintf(stderr, "Note Sections not yet supported.\n");
522 fprintf(stderr, "SHLIB Sections not supported (and non conforming.)\n");
543 static struct strtab *
544 DEFUN(bfd_new_strtab, (abfd),
549 ss = (struct strtab *)malloc(sizeof(struct strtab));
551 BFD_ASSERT(ss->tab != 0);
560 DEFUN(bfd_add_to_strtab, (abfd, ss, str),
562 struct strtab *ss AND
565 /* should search first, but for now: */
566 /* include the trailing NUL */
567 int ln = strlen(str)+1;
569 /* should this be using obstacks? */
570 ss->tab = realloc(ss->tab, ss->length + ln);
572 BFD_ASSERT(ss->tab != 0);
573 strcpy(ss->tab + ss->length, str);
577 return ss->length - ln;
581 DEFUN(bfd_add_2_to_strtab, (abfd, ss, str, str2),
583 struct strtab *ss AND
587 /* should search first, but for now: */
588 /* include the trailing NUL */
589 int ln = strlen(str)+strlen(str2)+1;
591 /* should this be using obstacks? */
593 ss->tab = realloc(ss->tab, ss->length + ln);
595 ss->tab = malloc(ln);
597 BFD_ASSERT(ss->tab != 0);
598 strcpy(ss->tab + ss->length, str);
599 strcpy(ss->tab + ss->length + strlen(str), str2);
603 return ss->length - ln;
606 /* Create a new ELF section from a bfd section. */
609 DEFUN(bfd_shdr_from_section, (abfd, hdr, shstrtab, indx),
611 Elf_Internal_Shdr *hdr AND
612 struct strtab *shstrtab AND
618 /* figure out out to write the section name from the bfd section name. MWE */
620 sect = abfd->sections;
621 for (ndx = indx; --ndx; )
625 hdr[indx].sh_name = bfd_add_to_strtab(abfd, shstrtab,
626 bfd_section_name(abfd, sect));
627 hdr[indx].sh_addr = sect->vma;
628 hdr[indx].sh_size = sect->_raw_size;
629 hdr[indx].sh_flags = 0;
630 /* these need to be preserved on */
631 hdr[indx].sh_link = 0;
632 hdr[indx].sh_info = 0;
633 hdr[indx].sh_addralign = 0;
634 hdr[indx].sh_entsize = 0;
636 hdr[indx].sh_type = 0;
637 if (sect->flags & SEC_RELOC) {
638 hdr[indx].sh_type = SHT_RELA; /* FIXME -- sparc specific */
641 if (sect->flags & SEC_HAS_CONTENTS)
643 hdr[indx].sh_offset = sect->filepos;
644 hdr[indx].sh_size = sect->_raw_size;
646 if (sect->flags & SEC_ALLOC)
648 hdr[indx].sh_flags |= SHF_ALLOC;
649 if (sect->flags & SEC_LOAD)
651 /* do something with sh_type ? */
654 if (!(sect->flags & SEC_READONLY))
655 hdr[indx].sh_flags |= SHF_WRITE;
657 if (sect->flags & SEC_CODE)
658 hdr[indx].sh_flags |= SHF_EXECINSTR;
663 /* Create a new bfd section from an ELF program header.
665 Since program segments have no names, we generate a synthetic name
666 of the form segment<NUM>, where NUM is generally the index in the
667 program header table. For segments that are split (see below) we
668 generate the names segment<NUM>a and segment<NUM>b.
670 Note that some program segments may have a file size that is different than
671 (less than) the memory size. All this means is that at execution the
672 system must allocate the amount of memory specified by the memory size,
673 but only initialize it with the first "file size" bytes read from the
674 file. This would occur for example, with program segments consisting
675 of combined data+bss.
677 To handle the above situation, this routine generates TWO bfd sections
678 for the single program segment. The first has the length specified by
679 the file size of the segment, and the second has the length specified
680 by the difference between the two sizes. In effect, the segment is split
681 into it's initialized and uninitialized parts.
686 DEFUN(bfd_section_from_phdr, (abfd, hdr, index),
688 Elf_Internal_Phdr *hdr AND
696 split = ((hdr -> p_memsz > 0) &&
697 (hdr -> p_filesz > 0) &&
698 (hdr -> p_memsz > hdr -> p_filesz));
699 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
700 name = bfd_alloc (abfd, strlen (namebuf) + 1);
701 strcpy (name, namebuf);
702 newsect = bfd_make_section (abfd, name);
703 newsect -> vma = hdr -> p_vaddr;
704 newsect -> _raw_size = hdr -> p_filesz;
705 newsect -> filepos = hdr -> p_offset;
706 newsect -> flags |= SEC_HAS_CONTENTS;
707 if (hdr -> p_type == PT_LOAD)
709 newsect -> flags |= SEC_ALLOC;
710 newsect -> flags |= SEC_LOAD;
711 if (hdr -> p_flags & PF_X)
713 /* FIXME: all we known is that it has execute PERMISSION,
715 newsect -> flags |= SEC_CODE;
718 if (!(hdr -> p_flags & PF_W))
720 newsect -> flags |= SEC_READONLY;
725 sprintf (namebuf, "segment%db", index);
726 name = bfd_alloc (abfd, strlen (namebuf) + 1);
727 strcpy (name, namebuf);
728 newsect = bfd_make_section (abfd, name);
729 newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz;
730 newsect -> _raw_size = hdr -> p_memsz - hdr -> p_filesz;
731 if (hdr -> p_type == PT_LOAD)
733 newsect -> flags |= SEC_ALLOC;
734 if (hdr -> p_flags & PF_X)
735 newsect -> flags |= SEC_CODE;
737 if (!(hdr -> p_flags & PF_W))
738 newsect -> flags |= SEC_READONLY;
747 DEFUN(bfd_prstatus,(abfd, descdata, descsz, filepos),
754 prstatus_t *status = (prstatus_t *)0;
756 if (descsz == sizeof (prstatus_t))
758 newsect = bfd_make_section (abfd, ".reg");
759 newsect -> _raw_size = sizeof (status->pr_reg);
760 newsect -> filepos = filepos + (long) &status->pr_reg;
761 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
762 newsect -> alignment_power = 2;
763 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
765 memcpy (core_prstatus (abfd), descdata, descsz);
770 /* Stash a copy of the prpsinfo structure away for future use. */
773 DEFUN(bfd_prpsinfo,(abfd, descdata, descsz, filepos),
781 if (descsz == sizeof (prpsinfo_t))
783 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
785 bcopy (descdata, core_prpsinfo (abfd), descsz);
791 DEFUN(bfd_fpregset,(abfd, descdata, descsz, filepos),
799 newsect = bfd_make_section (abfd, ".reg2");
800 newsect -> _raw_size = descsz;
801 newsect -> filepos = filepos;
802 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
803 newsect -> alignment_power = 2;
806 #endif /* HAVE_PROCFS */
808 /* Return a pointer to the args (including the command name) that were
809 seen by the program that generated the core dump. Note that for
810 some reason, a spurious space is tacked onto the end of the args
811 in some (at least one anyway) implementations, so strip it off if
815 DEFUN(elf_core_file_failing_command, (abfd),
819 if (core_prpsinfo (abfd))
821 prpsinfo_t *p = core_prpsinfo (abfd);
822 char *scan = p -> pr_psargs;
825 if ((scan > p -> pr_psargs) && (*scan == ' '))
829 return (p -> pr_psargs);
835 /* Return the number of the signal that caused the core dump. Presumably,
836 since we have a core file, we got a signal of some kind, so don't bother
837 checking the other process status fields, just return the signal number.
841 DEFUN(elf_core_file_failing_signal, (abfd),
845 if (core_prstatus (abfd))
847 return (((prstatus_t *)(core_prstatus (abfd))) -> pr_cursig);
853 /* Check to see if the core file could reasonably be expected to have
854 come for the current executable file. Note that by default we return
855 true unless we find something that indicates that there might be a
860 DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
869 /* First, xvecs must match since both are ELF files for the same target. */
871 if (core_bfd->xvec != exec_bfd->xvec)
873 bfd_error = system_call_error;
879 /* If no prpsinfo, just return true. Otherwise, grab the last component
880 of the exec'd pathname from the prpsinfo. */
882 if (core_prpsinfo (core_bfd))
884 corename = (((struct prpsinfo *) core_prpsinfo (core_bfd)) -> pr_fname);
891 /* Find the last component of the executable pathname. */
893 if ((execname = strrchr (exec_bfd -> filename, '/')) != NULL)
899 execname = (char *) exec_bfd -> filename;
902 /* See if they match */
904 return (strcmp (execname, corename) ? false : true);
910 #endif /* HAVE_PROCFS */
913 /* ELF core files contain a segment of type PT_NOTE, that holds much of
914 the information that would normally be available from the /proc interface
915 for the process, at the time the process dumped core. Currently this
916 includes copies of the prstatus, prpsinfo, and fpregset structures.
918 Since these structures are potentially machine dependent in size and
919 ordering, bfd provides two levels of support for them. The first level,
920 available on all machines since it does not require that the host
921 have /proc support or the relevant include files, is to create a bfd
922 section for each of the prstatus, prpsinfo, and fpregset structures,
923 without any interpretation of their contents. With just this support,
924 the bfd client will have to interpret the structures itself. Even with
925 /proc support, it might want these full structures for it's own reasons.
927 In the second level of support, where HAVE_PROCFS is defined, bfd will
928 pick apart the structures to gather some additional information that
929 clients may want, such as the general register set, the name of the
930 exec'ed file and its arguments, the signal (if any) that caused the
936 DEFUN(elf_corefile_note, (abfd, hdr),
938 Elf_Internal_Phdr *hdr)
940 Elf_External_Note *x_note_p; /* Elf note, external form */
941 Elf_Internal_Note i_note; /* Elf note, internal form */
942 char *buf = NULL; /* Entire note segment contents */
943 char *namedata; /* Name portion of the note */
944 char *descdata; /* Descriptor portion of the note */
945 char *sectname; /* Name to use for new section */
946 long filepos; /* File offset to descriptor data */
949 if (hdr -> p_filesz > 0
950 && (buf = (char *) bfd_xmalloc (hdr -> p_filesz)) != NULL
951 && bfd_seek (abfd, hdr -> p_offset, SEEK_SET) != -1L
952 && bfd_read ((PTR) buf, hdr -> p_filesz, 1, abfd) == hdr -> p_filesz)
954 x_note_p = (Elf_External_Note *) buf;
955 while ((char *) x_note_p < (buf + hdr -> p_filesz))
957 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> namesz);
958 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> descsz);
959 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> type);
960 namedata = x_note_p -> name;
961 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
962 filepos = hdr -> p_offset + (descdata - buf);
963 switch (i_note.type) {
965 /* process descdata as prstatus info */
966 bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
967 sectname = ".prstatus";
970 /* process descdata as fpregset info */
971 bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
972 sectname = ".fpregset";
975 /* process descdata as prpsinfo */
976 bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
977 sectname = ".prpsinfo";
980 /* Unknown descriptor, just ignore it. */
984 if (sectname != NULL)
986 newsect = bfd_make_section (abfd, sectname);
987 newsect -> _raw_size = i_note.descsz;
988 newsect -> filepos = filepos;
989 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
990 newsect -> alignment_power = 2;
992 x_note_p = (Elf_External_Note *)
993 (descdata + BFD_ALIGN (i_note.descsz, 4));
1005 /* Read a specified number of bytes at a specified offset in an ELF
1006 file, into a newly allocated buffer, and return a pointer to the
1010 DEFUN(elf_read, (abfd, offset, size),
1017 if ((buf = bfd_alloc (abfd, size)) == NULL)
1019 bfd_error = no_memory;
1022 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
1024 bfd_error = system_call_error;
1027 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
1029 bfd_error = system_call_error;
1035 /* Begin processing a given object.
1037 First we validate the file by reading in the ELF header and checking
1043 DEFUN (elf_object_p, (abfd), bfd *abfd)
1045 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
1046 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1047 Elf_External_Shdr x_shdr; /* Section header table entry, external form */
1048 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1050 char *shstrtab; /* Internal copy of section header stringtab */
1051 Elf_Off offset; /* Temp place to stash file offsets */
1053 /* Read in the ELF header in external format. */
1055 if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
1057 bfd_error = system_call_error;
1061 /* Now check to see if we have a valid ELF file, and one that BFD can
1062 make use of. The magic number must match, the address size ('class')
1063 and byte-swapping must match our XVEC entry, and it must have a
1064 section header table (FIXME: See comments re sections at top of this
1067 if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
1068 x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
1069 x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
1070 x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
1073 bfd_error = wrong_format;
1077 /* FIXME, Check EI_VERSION here ! */
1079 switch (x_ehdr.e_ident[EI_CLASS])
1081 case ELFCLASSNONE: /* address size not specified */
1082 goto wrong; /* No support if can't tell address size */
1083 case ELFCLASS32: /* 32-bit addresses */
1085 case ELFCLASS64: /* 64-bit addresses */
1086 goto wrong; /* FIXME: 64 bits not yet supported */
1088 goto wrong; /* No support if unknown address class */
1091 /* Switch xvec to match the specified byte order. */
1092 switch (x_ehdr.e_ident[EI_DATA])
1094 case ELFDATA2MSB: /* Big-endian */
1095 if (!abfd->xvec->header_byteorder_big_p)
1098 case ELFDATA2LSB: /* Little-endian */
1099 if (abfd->xvec->header_byteorder_big_p)
1102 case ELFDATANONE: /* No data encoding specified */
1103 default: /* Unknown data encoding specified */
1107 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1108 the tdata pointer in the bfd. */
1110 if (NULL == (elf_tdata (abfd) = (struct elf_obj_tdata *)
1111 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata))))
1113 bfd_error = no_memory;
1117 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
1119 /* Now that we know the byte order, swap in the rest of the header */
1120 i_ehdrp = elf_elfheader (abfd);
1121 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
1123 /* If there is no section header table, we're hosed. */
1124 if (i_ehdrp->e_shoff == 0)
1127 if (i_ehdrp->e_type == ET_EXEC || i_ehdrp->e_type == ET_DYN)
1128 abfd -> flags |= EXEC_P;
1130 switch (i_ehdrp->e_machine)
1133 case EM_M32: /* or should this be bfd_arch_obscure? */
1134 bfd_default_set_arch_mach(abfd, bfd_arch_unknown, 0);
1137 bfd_default_set_arch_mach(abfd, bfd_arch_sparc, 0);
1140 bfd_default_set_arch_mach(abfd, bfd_arch_i386, 0);
1143 bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0);
1146 bfd_default_set_arch_mach(abfd, bfd_arch_m88k, 0);
1149 bfd_default_set_arch_mach(abfd, bfd_arch_i860, 0);
1152 bfd_default_set_arch_mach(abfd, bfd_arch_mips, 0);
1158 /* Allocate space for a copy of the section header table in
1159 internal form, seek to the section header table in the file,
1160 read it in, and convert it to internal form. As a simple sanity
1161 check, verify that the what BFD thinks is the size of each section
1162 header table entry actually matches the size recorded in the file. */
1164 if (i_ehdrp->e_shentsize != sizeof (x_shdr))
1166 i_shdrp = (Elf_Internal_Shdr *)
1167 bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum);
1170 bfd_error = no_memory;
1173 if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1)
1175 bfd_error = system_call_error;
1178 for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
1180 if (bfd_read ((PTR) &x_shdr, sizeof x_shdr, 1, abfd)
1183 bfd_error = system_call_error;
1186 elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
1189 elf_elfsections (abfd) = i_shdrp;
1191 /* Read in the string table containing the names of the sections. We
1192 will need the base pointer to this table later. */
1193 /* We read this inline now, so that we don't have to go through
1194 bfd_section_from_shdr with it (since this particular strtab is
1195 used to find all of the ELF section names.) */
1197 shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
1201 /* Once all of the section headers have been read and converted, we
1202 can start processing them. Note that the first section header is
1203 a dummy placeholder entry, so we ignore it.
1205 We also watch for the symbol table section and remember the file
1206 offset and section size for both the symbol table section and the
1207 associated string table section. */
1209 for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
1211 bfd_section_from_shdr (abfd, shindex);
1214 /* Remember the entry point specified in the ELF file header. */
1216 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
1218 return (abfd->xvec);
1221 /* Core files are simply standard ELF formatted files that partition
1222 the file using the execution view of the file (program header table)
1223 rather than the linking view. In fact, there is no section header
1224 table in a core file.
1226 The process status information (including the contents of the general
1227 register set) and the floating point register set are stored in a
1228 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
1229 that allow standard bfd access to the general registers (.reg) and the
1230 floating point registers (.reg2).
1235 DEFUN (elf_core_file_p, (abfd), bfd *abfd)
1237 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
1238 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1239 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
1240 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
1241 unsigned int phindex;
1243 /* Read in the ELF header in external format. */
1245 if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
1247 bfd_error = system_call_error;
1251 /* Now check to see if we have a valid ELF file, and one that BFD can
1252 make use of. The magic number must match, the address size ('class')
1253 and byte-swapping must match our XVEC entry, and it must have a
1254 program header table (FIXME: See comments re segments at top of this
1257 if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
1258 x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
1259 x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
1260 x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
1263 bfd_error = wrong_format;
1267 /* FIXME, Check EI_VERSION here ! */
1269 switch (x_ehdr.e_ident[EI_CLASS])
1271 case ELFCLASSNONE: /* address size not specified */
1272 goto wrong; /* No support if can't tell address size */
1273 case ELFCLASS32: /* 32-bit addresses */
1275 case ELFCLASS64: /* 64-bit addresses */
1276 goto wrong; /* FIXME: 64 bits not yet supported */
1278 goto wrong; /* No support if unknown address class */
1281 /* Switch xvec to match the specified byte order. */
1282 switch (x_ehdr.e_ident[EI_DATA])
1284 case ELFDATA2MSB: /* Big-endian */
1285 abfd->xvec = &elf_big_vec;
1287 case ELFDATA2LSB: /* Little-endian */
1288 abfd->xvec = &elf_little_vec;
1290 case ELFDATANONE: /* No data encoding specified */
1291 default: /* Unknown data encoding specified */
1295 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1296 the tdata pointer in the bfd. */
1299 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
1300 if (elf_tdata (abfd) == NULL)
1302 bfd_error = no_memory;
1306 /* FIXME, `wrong' returns from this point onward, leak memory. */
1308 /* Now that we know the byte order, swap in the rest of the header */
1309 i_ehdrp = elf_elfheader (abfd);
1310 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
1312 /* If there is no program header, or the type is not a core file, then
1314 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
1317 /* Allocate space for a copy of the program header table in
1318 internal form, seek to the program header table in the file,
1319 read it in, and convert it to internal form. As a simple sanity
1320 check, verify that the what BFD thinks is the size of each program
1321 header table entry actually matches the size recorded in the file. */
1323 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
1325 i_phdrp = (Elf_Internal_Phdr *)
1326 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
1329 bfd_error = no_memory;
1332 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
1334 bfd_error = system_call_error;
1337 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
1339 if (bfd_read ((PTR) &x_phdr, sizeof (x_phdr), 1, abfd)
1342 bfd_error = system_call_error;
1345 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
1348 /* Once all of the program headers have been read and converted, we
1349 can start processing them. */
1351 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
1353 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
1354 if ((i_phdrp + phindex) -> p_type == PT_NOTE)
1356 elf_corefile_note (abfd, i_phdrp + phindex);
1360 /* Remember the entry point specified in the ELF file header. */
1362 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
1364 return (abfd->xvec);
1368 DEFUN (elf_mkobject, (abfd), bfd *abfd)
1370 /* this just does initialization */
1371 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
1372 elf_tdata(abfd) = (struct elf_obj_tdata *)
1373 bfd_zalloc (abfd, sizeof(struct elf_obj_tdata));
1374 if (elf_tdata(abfd) == 0) {
1375 bfd_error = no_memory;
1378 /* since everything is done at close time, do we need any
1385 Create ELF output from BFD sections.
1387 Essentially, just create the section header and forget about the program
1392 /* lacking nested functions and nested types, set up for mapping over
1393 BFD sections to produce ELF sections */
1396 Elf_Internal_Ehdr *i_ehdr;
1397 Elf_Internal_Shdr *i_shdrp;
1398 struct strtab *shstrtab;
1405 DEFUN (elf_make_sections, (abfd, asect, obj),
1410 elf_sect_thunk *thunk = (elf_sect_thunk*)obj;
1411 /* most of what is in bfd_shdr_from_section goes in here... */
1412 /* and all of these sections generate at *least* one ELF section. */
1416 /* check if we're making a PROGBITS section... */
1417 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1418 /* this was too strict... what *do* we want to check here? */
1421 Elf_Internal_Shdr *this_hdr;
1422 this_section = elf_section_from_bfd_section (abfd, asect);
1423 this_hdr = &thunk->i_shdrp[this_section];
1425 this_hdr->sh_addr = asect->vma;
1426 this_hdr->sh_size = asect->_raw_size;
1427 /* contents already set by elf_set_section_contents */
1429 if (asect->flags & SEC_RELOC)
1431 /* emit a reloc section, and thus strtab and symtab... */
1432 Elf_Internal_Shdr *rela_hdr;
1433 Elf_Internal_Shdr *symtab_hdr;
1434 Elf_Internal_Shdr *symstrtab_hdr;
1435 Elf_External_Rela *outbound_relocs;
1436 Elf_External_Sym *outbound_syms;
1438 int symstrtab_section;
1440 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1442 if (thunk->symtab_section == this_section + 1)
1443 rela_section = thunk->symtab_section + 2; /* symtab + symstrtab */
1445 rela_section = this_section + 1;
1446 rela_hdr = &thunk->i_shdrp[rela_section];
1447 rela_hdr->sh_type = SHT_RELA;
1448 rela_hdr->sh_link = thunk->symtab_section;
1449 rela_hdr->sh_info = this_section;
1450 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1451 /* orelocation has the data, reloc_count has the count... */
1452 rela_hdr->sh_size = rela_hdr->sh_entsize * asect->reloc_count;
1453 outbound_relocs = (Elf_External_Rela *)
1454 bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela));
1455 for (idx = 0; idx < asect->reloc_count; idx++)
1457 Elf_Internal_Rela dst;
1459 Elf_External_Rela *src;
1461 ptr = asect->orelocation[idx];
1462 src = outbound_relocs + idx;
1463 if (asect->flags & SEC_RELOC)
1464 dst.r_offset = ptr->address - asect->vma;
1466 dst.r_offset = ptr->address;
1468 dst.r_info = ELF_R_INFO(1 /*ptr->sym_ptr_ptr*/, /* needs index into symtab (FIXME) */
1471 dst.r_addend = ptr->addend;
1472 elf_swap_reloca_out(abfd, &dst, src);
1474 rela_hdr->contents = (void*)outbound_relocs;
1480 DEFUN (elf_fake_sections, (abfd, asect, obj),
1485 elf_sect_thunk *thunk = (elf_sect_thunk*)obj;
1486 /* most of what is in bfd_shdr_from_section goes in here... */
1487 /* and all of these sections generate at *least* one ELF section. */
1491 /* check if we're making a PROGBITS section... */
1492 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1493 /* this was too strict... what *do* we want to check here? */
1496 Elf_Internal_Shdr *this_hdr;
1497 this_section = thunk->i_ehdr->e_shnum++;
1498 this_hdr = &thunk->i_shdrp[this_section];
1500 bfd_add_to_strtab (abfd, thunk->shstrtab, asect->name);
1501 /* we need to log the type *now* so that elf_section_from_bfd_section
1502 can find us... have to set rawdata too. */
1503 this_hdr->rawdata = (void*)asect;
1504 if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1505 this_hdr->sh_type = SHT_PROGBITS;
1507 /* what *do* we put here? */
1508 this_hdr->sh_type = SHT_PROGBITS;
1511 if (asect->flags & SEC_RELOC)
1513 /* emit a reloc section, and thus strtab and symtab... */
1514 Elf_Internal_Shdr *rela_hdr;
1515 Elf_Internal_Shdr *symtab_hdr;
1516 Elf_Internal_Shdr *symstrtab_hdr;
1517 Elf_External_Rela *outbound_relocs;
1518 Elf_External_Sym *outbound_syms;
1520 int symstrtab_section;
1522 /* note that only one symtab is used, so just remember it
1524 if (! thunk->symtab_section)
1526 thunk->symtab_section = thunk->i_ehdr->e_shnum++;
1527 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1528 symtab_hdr->sh_name =
1529 bfd_add_to_strtab (abfd, thunk->shstrtab, ".symtab");
1530 symtab_hdr->sh_type = SHT_SYMTAB;
1531 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
1533 symstrtab_section = thunk->i_ehdr->e_shnum++;
1534 BFD_ASSERT(symstrtab_section == thunk->symtab_section+1);
1535 symstrtab_hdr = &thunk->i_shdrp[symstrtab_section];
1536 symtab_hdr->sh_link = symstrtab_section;
1537 symstrtab_hdr->sh_name =
1538 bfd_add_to_strtab (abfd, thunk->shstrtab, ".strtab");
1539 symstrtab_hdr->sh_type = SHT_STRTAB;
1541 symtab_hdr->contents = 0;
1542 symstrtab_hdr->contents = 0;
1543 symstrtab_hdr->sh_size = 0;
1546 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1548 rela_section = thunk->i_ehdr->e_shnum++;
1549 rela_hdr = &thunk->i_shdrp[rela_section];
1551 bfd_add_2_to_strtab (abfd, thunk->shstrtab, ".rela", asect->name);
1552 rela_hdr->sh_type = SHT_RELA;
1553 rela_hdr->sh_link = thunk->symtab_section;
1554 rela_hdr->sh_info = this_section;
1555 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1562 DEFUN (elf_compute_section_file_positions, (abfd), bfd *abfd)
1564 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1565 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1566 struct strtab *shstrtab;
1567 int count, maxsections;
1571 if (! elf_shstrtab (abfd)) {
1572 i_ehdrp = elf_elfheader (abfd); /* build new header in tdata memory */
1573 shstrtab = bfd_new_strtab(abfd);
1575 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
1576 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
1577 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
1578 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
1580 i_ehdrp->e_ident[EI_CLASS] = ELFCLASS32; /* FIXME: find out from bfd */
1581 i_ehdrp->e_ident[EI_DATA] =
1582 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
1583 i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
1585 for(count = EI_PAD; count < EI_NIDENT; count ++)
1586 i_ehdrp->e_ident[count] = 0;
1588 i_ehdrp->e_type = (abfd->flags & EXEC_P)? ET_EXEC : ET_REL;
1589 switch(bfd_get_arch(abfd))
1591 case bfd_arch_unknown:
1592 i_ehdrp->e_machine = EM_NONE;
1594 case bfd_arch_sparc:
1595 i_ehdrp->e_machine = EM_SPARC;
1598 i_ehdrp->e_machine = EM_386;
1601 i_ehdrp->e_machine = EM_68K;
1604 i_ehdrp->e_machine = EM_88K;
1607 i_ehdrp->e_machine = EM_860;
1609 case bfd_arch_mips: /* MIPS Rxxxx */
1610 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
1612 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1614 i_ehdrp->e_machine = EM_NONE;
1616 i_ehdrp->e_version = EV_CURRENT;
1617 i_ehdrp->e_ehsize = sizeof(Elf_External_Ehdr);
1619 /* no program header, for now. */
1620 i_ehdrp->e_phoff = 0;
1621 i_ehdrp->e_phentsize = 0;
1622 i_ehdrp->e_phnum = 0;
1624 /* each bfd section is section header entry */
1625 i_ehdrp->e_entry = bfd_get_start_address (abfd);
1626 i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
1628 /* figure at most each section can have a rel, strtab, symtab */
1629 maxsections = 4*bfd_count_sections(abfd)+2;
1631 i_ehdrp->e_shoff = i_ehdrp->e_ehsize;
1633 /* and we'll just have to fix up the offsets later. */
1634 /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */
1636 i_shdrp = (Elf_Internal_Shdr *)
1637 bfd_alloc (abfd, sizeof (*i_shdrp) * maxsections);
1640 bfd_error = no_memory;
1643 for (count=0; count < maxsections; count++)
1645 i_shdrp[count].rawdata = 0;
1646 i_shdrp[count].contents = 0;
1650 i_shdrp[0].sh_name = 0;
1651 i_shdrp[0].sh_type = SHT_NULL;
1652 i_shdrp[0].sh_flags = 0;
1653 i_shdrp[0].sh_addr = 0;
1654 i_shdrp[0].sh_offset = 0;
1655 i_shdrp[0].sh_size = 0;
1656 i_shdrp[0].sh_link = SHN_UNDEF;
1657 i_shdrp[0].sh_info = 0;
1658 i_shdrp[0].sh_addralign = 0;
1659 i_shdrp[0].sh_entsize = 0;
1661 i_ehdrp->e_shnum = 1;
1663 elf_elfsections (abfd) = i_shdrp;
1664 elf_shstrtab (abfd) = shstrtab;
1666 est.i_ehdr = elf_elfheader(abfd);
1667 est.i_shdrp = elf_elfsections(abfd);
1668 est.shstrtab = elf_shstrtab(abfd);
1669 est.symtab_section = 0; /* elf_fake_sections fils it in */
1671 bfd_map_over_sections(abfd, elf_fake_sections, &est);
1672 elf_onesymtab (abfd) = est.symtab_section;
1677 DEFUN (elf_write_object_contents, (abfd), bfd *abfd)
1679 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
1680 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1681 Elf_External_Phdr *x_phdrp; /* Program header table, external form */
1682 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
1683 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
1684 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1691 struct strtab *shstrtab;
1693 if(abfd->output_has_begun == false)
1694 elf_compute_section_file_positions(abfd);
1696 i_ehdrp = elf_elfheader (abfd);
1697 i_shdrp = elf_elfsections (abfd);
1698 shstrtab = elf_shstrtab (abfd);
1700 est.i_ehdr = i_ehdrp;
1701 est.i_shdrp = i_shdrp;
1702 est.shstrtab = shstrtab;
1703 est.symtab_section = elf_onesymtab (abfd); /* filled in by elf_fake */
1705 bfd_map_over_sections(abfd, elf_make_sections, &est);
1707 /* dump out the one symtab */
1709 int symcount = bfd_get_symcount (abfd);
1710 asymbol ** syms = bfd_get_outsymbols (abfd);
1711 struct strtab * stt = bfd_new_strtab (abfd);
1712 Elf_Internal_Shdr *symtab_hdr;
1713 Elf_Internal_Shdr *symstrtab_hdr;
1714 int symstrtab_section;
1715 Elf_External_Sym *outbound_syms;
1718 symtab_hdr = &i_shdrp[est.symtab_section];
1719 symtab_hdr->sh_type = SHT_SYMTAB;
1720 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
1721 symtab_hdr->sh_size = symtab_hdr->sh_entsize * symcount;
1723 /* see assert in elf_fake_sections that supports this: */
1724 symstrtab_section = est.symtab_section+1;
1725 symstrtab_hdr = &i_shdrp[symstrtab_section];
1726 symtab_hdr->sh_link = symstrtab_section;
1727 symstrtab_hdr->sh_type = SHT_STRTAB;
1729 outbound_syms = (Elf_External_Sym*)
1730 bfd_alloc(abfd, (1+symcount) * sizeof(Elf_External_Sym));
1731 /* now generate the data (for "contents") */
1732 for (idx = 0; idx < symcount; idx++)
1734 Elf_Internal_Sym sym;
1735 sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
1736 sym.st_value = syms[idx]->value;
1737 sym.st_size = 0; /* we should recover this (FIXME) */
1738 if (syms[idx]->flags & BSF_WEAK)
1739 sym.st_info = ELF_ST_INFO(STB_WEAK, STT_OBJECT);
1740 else if (syms[idx]->flags & BSF_LOCAL)
1741 sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_OBJECT);
1742 else if (syms[idx]->flags & BSF_GLOBAL)
1743 sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
1744 else if (syms[idx]->flags & BSF_SECTION_SYM)
1745 sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
1746 else if (syms[idx]->flags & BSF_FILE)
1747 sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
1750 if (syms[idx]->section)
1752 elf_section_from_bfd_section(abfd,
1753 syms[idx]->section->output_section);
1755 sym.st_shndx = SHN_UNDEF;
1757 elf_swap_symbol_out (abfd, &sym, outbound_syms+idx+1);
1760 /* fill in 0th symbol */
1761 Elf_Internal_Sym sym;
1767 sym.st_shndx = SHN_UNDEF;
1768 elf_swap_symbol_out (abfd, &sym, outbound_syms);
1770 symtab_hdr->contents = (void*)outbound_syms;
1771 symstrtab_hdr->contents = (void*)stt->tab;
1772 symstrtab_hdr->sh_size = stt->length;
1775 /* put the strtab out too... */
1777 Elf_Internal_Shdr *this_hdr;
1780 this_section = i_ehdrp->e_shnum++;
1781 i_ehdrp->e_shstrndx = this_section;
1782 this_hdr = &i_shdrp[this_section];
1783 this_hdr->sh_name = bfd_add_to_strtab (abfd, shstrtab, ".shstrtab");
1784 this_hdr->sh_size = shstrtab->length;
1785 this_hdr->contents = (void*)shstrtab->tab;
1788 outbase = i_ehdrp->e_ehsize;
1790 /* swap the header before spitting it out... */
1791 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1792 bfd_seek (abfd, 0L, SEEK_SET);
1793 bfd_write ((PTR) &x_ehdr, sizeof(x_ehdr), 1, abfd);
1795 outbase += i_ehdrp->e_shentsize * i_ehdrp->e_shnum;
1797 /* now we fix up the offsets... */
1798 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1800 i_shdrp[count].sh_offset = outbase;
1801 outbase += i_shdrp[count].sh_size;
1804 /* at this point we've concocted all the ELF sections... */
1805 x_shdrp = (Elf_External_Shdr *)
1806 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
1809 bfd_error = no_memory;
1813 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1815 elf_swap_shdr_out (abfd, i_shdrp+count, x_shdrp+count);
1817 bfd_write ((PTR) x_shdrp, sizeof(*x_shdrp), i_ehdrp->e_shnum, abfd);
1818 /* need to dump the string table too... */
1820 /* after writing the headers, we need to write the sections too... */
1821 nsect = abfd->sections;
1822 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1824 if(i_shdrp[count].contents)
1826 bfd_seek (abfd, i_shdrp[count].sh_offset, SEEK_SET);
1827 bfd_write (i_shdrp[count].contents, i_shdrp[count].sh_size, 1, abfd);
1831 /* sample use of bfd:
1832 * bfd_seek (abfd, 0L, false);
1833 * bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
1834 * if (bfd_seek(abfd, scn_base, SEEK_SET) != 0)
1836 * old = bfd_tell(abfd);
1843 /* Given an index of a section, retrieve a pointer to it. Note
1844 that for our purposes, sections are indexed by {1, 2, ...} with
1845 0 being an illegal index. */
1847 /* In the original, each ELF section went into exactly one BFD
1848 section. This doesn't really make sense, so we need a real mapping.
1849 The mapping has to hide in the Elf_Internal_Shdr since asection
1850 doesn't have anything like a tdata field... */
1853 DEFUN (section_from_elf_index, (abfd, index),
1857 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
1858 Elf_Internal_Shdr *hdr = i_shdrp + index;
1860 switch (hdr->sh_type)
1862 /* ELF sections that map to BFD sections */
1864 case SHT_BEPROGBITS:
1867 bfd_section_from_shdr (abfd, index);
1868 return (struct sec *)hdr->rawdata;
1875 /* given a section, search the header to find them... */
1877 DEFUN (elf_section_from_bfd_section, (abfd, asect),
1881 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
1883 Elf_Internal_Shdr *hdr;
1884 int maxindex = elf_elfheader (abfd)->e_shnum;
1886 for(index = 0; index < maxindex; index++) {
1887 hdr = &i_shdrp[index];
1888 switch (hdr->sh_type)
1890 /* ELF sections that map to BFD sections */
1892 case SHT_BEPROGBITS:
1896 if (((struct sec *)(hdr->rawdata)) == asect)
1908 DEFUN (elf_slurp_symbol_table, (abfd, symptrs),
1910 asymbol **symptrs) /* Buffer for generated bfd symbols */
1912 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
1913 Elf_Internal_Shdr *hdr = i_shdrp + elf_onesymtab (abfd);
1914 int symcount; /* Number of external ELF symbols */
1916 char *strtab; /* Buffer for raw ELF string table section */
1917 asymbol *sym; /* Pointer to current bfd symbol */
1918 asymbol *symbase; /* Buffer for generated bfd symbols */
1919 Elf_Internal_Sym i_sym;
1920 Elf_External_Sym *x_symp;
1922 /* this is only valid because there is only one symtab... */
1923 /* FIXME: This is incorrect, there may also be a dynamic symbol
1924 table which is a subset of the full symbol table. We either need
1925 to be prepared to read both (and merge them) or ensure that we
1926 only read the full symbol table. Currently we only get called to
1927 read the full symbol table. -fnf */
1928 if (bfd_get_outsymbols (abfd) != NULL)
1933 /* Read each raw ELF symbol, converting from external ELF form to
1934 internal ELF form, and then using the information to create a
1935 canonical bfd symbol table entry.
1937 Note that we allocate the initial bfd canonical symbol buffer
1938 based on a one-to-one mapping of the ELF symbols to canonical
1939 symbols. We actually use all the ELF symbols, so there will be no
1940 space left over at the end. When we have all the symbols, we
1941 build the caller's pointer vector. */
1943 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
1945 bfd_error = system_call_error;
1949 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1950 symbase = (asymbol *) bfd_zalloc (abfd, symcount * sizeof (asymbol));
1953 /* Temporarily allocate room for the raw ELF symbols. */
1954 x_symp = (Elf_External_Sym *) malloc (symcount * sizeof (Elf_External_Sym));
1956 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
1957 != symcount * sizeof (Elf_External_Sym))
1960 bfd_error = system_call_error;
1963 /* Skip first symbol, which is a null dummy. */
1964 for (i = 1; i < symcount; i++)
1966 elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
1967 sym -> the_bfd = abfd;
1968 if (i_sym.st_name > 0)
1969 sym -> name = elf_string_from_elf_section(abfd, hdr->sh_link,
1972 sym -> name = "unnamed"; /* perhaps should include the number? */
1973 sym -> value = i_sym.st_value;
1974 /* FIXME -- this is almost certainly bogus. It's from Pace Willisson's
1975 hasty Solaris support, to pass the sizes of object files or functions
1976 down into GDB via the back door, to circumvent some other kludge in
1977 how Sun hacked stabs. -- gnu@cygnus.com */
1978 sym -> udata = (PTR)i_sym.st_size;
1979 /* FIXME -- end of bogosity. */
1980 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
1982 sym -> section = section_from_elf_index (abfd, i_sym.st_shndx);
1984 else if (i_sym.st_shndx == SHN_ABS)
1986 sym -> section = &bfd_abs_section;
1988 else if (i_sym.st_shndx == SHN_COMMON)
1990 sym -> section = &bfd_com_section;
1992 else if (i_sym.st_shndx == SHN_UNDEF)
1994 sym -> section = &bfd_und_section;
1997 switch (ELF_ST_BIND (i_sym.st_info))
2000 sym -> flags |= BSF_LOCAL;
2003 sym -> flags |= (BSF_GLOBAL | BSF_EXPORT);
2006 sym -> flags |= BSF_WEAK;
2010 switch (ELF_ST_TYPE (i_sym.st_info))
2013 sym->flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
2016 sym->flags |= BSF_FILE | BSF_DEBUGGING;
2022 /* We rely on the zalloc to clear out the final symbol entry. */
2024 /* We're now done with the raw symbols. */
2027 bfd_get_symcount(abfd) = symcount = sym - symbase;
2029 /* Fill in the user's symbol pointer vector if needed. */
2033 while (symcount-- > 0)
2037 *symptrs = 0; /* Final null pointer */
2043 /* Return the number of bytes required to hold the symtab vector.
2045 Note that we base it on the count plus 1, since we will null terminate
2046 the vector allocated based on this size. However, the ELF symbol table
2047 always has a dummy entry as symbol #0, so it ends up even. */
2050 DEFUN (elf_get_symtab_upper_bound, (abfd), bfd *abfd)
2052 unsigned int symcount;
2053 unsigned int symtab_size;
2054 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
2055 Elf_Internal_Shdr *hdr = i_shdrp + elf_onesymtab (abfd);
2057 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2058 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol));
2059 return (symtab_size);
2063 This function return the number of bytes required to store the
2064 relocation information associated with section <<sect>>
2065 attached to bfd <<abfd>>
2069 elf_get_reloc_upper_bound (abfd, asect)
2073 if (asect->flags & SEC_RELOC)
2075 /* either rel or rela */
2076 return asect->_raw_size;
2082 /* FIXME!!! sparc howto should go into elf-32-sparc.c */
2087 R_SPARC_8, R_SPARC_16, R_SPARC_32,
2088 R_SPARC_DISP8, R_SPARC_DISP16, R_SPARC_DISP32,
2089 R_SPARC_WDISP30, R_SPARC_WDISP22,
2090 R_SPARC_HI22, R_SPARC_22,
2091 R_SPARC_13, R_SPARC_LO10,
2092 R_SPARC_GOT10, R_SPARC_GOT13, R_SPARC_GOT22,
2093 R_SPARC_PC10, R_SPARC_PC22,
2096 R_SPARC_GLOB_DAT, R_SPARC_JMP_SLOT,
2101 #define RELOC_TYPE_NAMES \
2103 "R_SPARC_8", "R_SPARC_16", "R_SPARC_32", \
2104 "R_SPARC_DISP8", "R_SPARC_DISP16", "R_SPARC_DISP32", \
2105 "R_SPARC_WDISP30", "R_SPARC_WDISP22", \
2106 "R_SPARC_HI22", "R_SPARC_22", \
2107 "R_SPARC_13", "R_SPARC_LO10", \
2108 "R_SPARC_GOT10", "R_SPARC_GOT13", "R_SPARC_GOT22", \
2109 "R_SPARC_PC10", "R_SPARC_PC22", \
2112 "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT", \
2113 "R_SPARC_RELATIVE", \
2116 static reloc_howto_type elf_howto_table[] =
2118 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,false,false, 0,"R_SPARC_NONE", false,0,0x00000000,false),
2119 HOWTO(R_SPARC_8, 0,0, 8,false,0,true, true, 0,"R_SPARC_8", false,0,0x000000ff,false),
2120 HOWTO(R_SPARC_16, 0,1,16,false,0,true, true, 0,"R_SPARC_16", false,0,0x0000ffff,false),
2121 HOWTO(R_SPARC_32, 0,2,32,false,0,true, true, 0,"R_SPARC_32", false,0,0xffffffff,false),
2122 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,false, true, 0,"R_SPARC_DISP8", false,0,0x000000ff,false),
2123 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,false, true, 0,"R_SPARC_DISP16", false,0,0x0000ffff,false),
2124 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,false, true, 0,"R_SPARC_DISP32", false,0,0x00ffffff,false),
2125 HOWTO(R_SPARC_WDISP30,2,2,30,true, 0,false, true, 0,"R_SPARC_WDISP30",false,0,0x3fffffff,false),
2126 HOWTO(R_SPARC_WDISP22,2,2,22,true, 0,false, true, 0,"R_SPARC_WDISP22",false,0,0x003fffff,false),
2127 HOWTO(R_SPARC_HI22, 10,2,22,false,0,true, false, 0,"R_SPARC_HI22", false,0,0x003fffff,false),
2128 HOWTO(R_SPARC_22, 0,2,22,false,0,true, true, 0,"R_SPARC_22", false,0,0x003fffff,false),
2129 HOWTO(R_SPARC_13, 0,1,13,false,0,true, true, 0,"R_SPARC_13", false,0,0x00001fff,false),
2130 HOWTO(R_SPARC_LO10, 0,1,10,false,0,true, false, 0,"R_SPARC_LO10", false,0,0x000003ff,false),
2131 HOWTO(R_SPARC_GOT10, 0,1,10,false,0,false, true, 0,"R_SPARC_GOT10", false,0,0x000003ff,false),
2132 HOWTO(R_SPARC_GOT13, 0,1,13,false,0,false, true, 0,"R_SPARC_GOT13", false,0,0x00001fff,false),
2133 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,false, true, 0,"R_SPARC_GOT22", false,0,0x003fffff,false),
2134 HOWTO(R_SPARC_PC10, 0,1,10,false,0,true, true, 0,"R_SPARC_PC10", false,0,0x000003ff,false),
2135 HOWTO(R_SPARC_PC22, 0,2,22,false,0,true, true, 0,"R_SPARC_PC22", false,0,0x003fffff,false),
2136 HOWTO(R_SPARC_WPLT30, 0,0,00,false,0,false,false, 0,"R_SPARC_WPLT30", false,0,0x00000000,false),
2137 HOWTO(R_SPARC_COPY, 0,0,00,false,0,false,false, 0,"R_SPARC_COPY", false,0,0x00000000,false),
2138 HOWTO(R_SPARC_GLOB_DAT,0,0,00,false,0,false,false,0,"R_SPARC_GLOB_DAT",false,0,0x00000000,false),
2139 HOWTO(R_SPARC_JMP_SLOT,0,0,00,false,0,false,false,0,"R_SPARC_JMP_SLOT",false,0,0x00000000,false),
2140 HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,false,false,0,"R_SPARC_RELATIVE",false,0,0x00000000,false),
2141 HOWTO(R_SPARC_UA32, 0,0,00,false,0,false,false,0,"R_SPARC_UA32", false,0,0x00000000,false),
2146 DEFUN(elf_info_to_howto, (abfd, cache_ptr, dst),
2148 arelent *cache_ptr AND
2149 Elf_Internal_Rela *dst)
2151 /* FIXME!!! just doing sparc for now... */
2153 BFD_ASSERT (ELF_R_TYPE(dst->r_info) < 24);
2155 cache_ptr->howto = &elf_howto_table[ELF_R_TYPE(dst->r_info)];
2157 fprintf (stderr, "elf_info_to_howto not implemented\n");
2163 DEFUN(elf_slurp_reloca_table,(abfd, asect, symbols),
2168 Elf_External_Rela *native_relocs;
2169 arelent *reloc_cache;
2174 if (asect->relocation)
2176 if (asect->reloc_count == 0)
2178 if (asect->flags & SEC_CONSTRUCTOR)
2181 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
2182 native_relocs = (Elf_External_Rela *)
2183 bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela));
2184 bfd_read ((PTR) native_relocs,
2185 sizeof(Elf_External_Rela), asect->reloc_count, abfd);
2187 reloc_cache = (arelent *)
2188 bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent)));
2190 if (! reloc_cache) {
2191 bfd_error = no_memory;
2195 for (idx = 0; idx < asect->reloc_count; idx ++)
2197 #ifdef RELOC_PROCESSING
2198 /* sparc, 68k, 88k, 860 use rela only. */
2199 /* 386 and we32000 use rel only... fix it for them later. */
2200 Elf_Internal_Rela dst;
2201 Elf_External_Rela *src;
2203 cache_ptr = reloc_cache + idx;
2204 src = native_relocs + idx;
2205 elf_swap_reloca_in(abfd, src, &dst);
2207 RELOC_PROCESSING(cache_ptr, &dst, symbols, abfd, asect);
2209 Elf_Internal_Rela dst;
2211 Elf_External_Rela *src;
2213 cache_ptr = reloc_cache + idx;
2214 src = native_relocs + idx;
2216 elf_swap_reloca_in(abfd, src, &dst);
2218 if(asect->flags & SEC_RELOC)
2220 /* relocatable, so the offset is off of the section */
2221 cache_ptr->address = dst.r_offset + asect->vma;
2225 /* non-relocatable, so the offset a virtual address */
2226 cache_ptr->address = dst.r_offset;
2228 /* ELF_R_SYM(dst.r_info) is the symbol table offset... */
2229 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM(dst.r_info);
2230 cache_ptr->addend = dst.r_addend;
2231 /* ptr = *(cache_ptr->sym_ptr_ptr); */
2233 /* Fill in the cache_ptr->howto field from dst.r_type */
2234 elf_info_to_howto(abfd, cache_ptr, &dst);
2238 asect->relocation = reloc_cache;
2244 elf_canonicalize_reloc (abfd, section, relptr, symbols)
2250 arelent *tblptr = section->relocation;
2251 unsigned int count = 0;
2253 /* snarfed from coffcode.h */
2254 /* FIXME: this could be reloc... */
2255 elf_slurp_reloca_table(abfd, section, symbols);
2257 tblptr = section->relocation;
2261 for (; count++ < section->reloc_count;)
2262 *relptr++ = tblptr++;
2265 return section->reloc_count;
2269 DEFUN (elf_get_symtab, (abfd, alocation),
2271 asymbol **alocation)
2273 unsigned int symcount;
2276 if (!elf_slurp_symbol_table (abfd, alocation))
2279 return (bfd_get_symcount (abfd));
2283 DEFUN (elf_make_empty_symbol, (abfd),
2286 elf_symbol_type *newsym;
2288 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
2291 bfd_error = no_memory;
2296 newsym -> symbol.the_bfd = abfd;
2297 return (&newsym -> symbol);
2302 DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how),
2303 bfd *ignore_abfd AND
2306 bfd_print_symbol_type how)
2308 FILE *file = (FILE *)filep;
2311 case bfd_print_symbol_name:
2312 fprintf(file, "%s", symbol->name);
2314 case bfd_print_symbol_more:
2315 fprintf(file, "elf %lx %lx",
2319 case bfd_print_symbol_nm:
2320 case bfd_print_symbol_all:
2322 CONST char *section_name;
2323 section_name = symbol->section? symbol->section->name : "(*none*)";
2324 bfd_print_symbol_vandf((PTR) file, symbol);
2325 fprintf(file, " %s\t%s",
2335 DEFUN (elf_get_lineno,(ignore_abfd, symbol),
2336 bfd *ignore_abfd AND
2339 fprintf (stderr, "elf_get_lineno unimplemented\n");
2346 DEFUN (elf_set_arch_mach,(abfd, arch, machine),
2348 enum bfd_architecture arch AND
2349 unsigned long machine)
2351 /* Allow any architecture to be supported by the elf backend */
2354 case bfd_arch_unknown: /* EM_NONE */
2355 case bfd_arch_sparc: /* EM_SPARC */
2356 case bfd_arch_i386: /* EM_386 */
2357 case bfd_arch_m68k: /* EM_68K */
2358 case bfd_arch_m88k: /* EM_88K */
2359 case bfd_arch_i860: /* EM_860 */
2360 case bfd_arch_mips: /* EM_MIPS (MIPS R3000) */
2361 return bfd_default_set_arch_mach(abfd, arch, machine);
2368 DEFUN (elf_find_nearest_line,(abfd,
2376 asection *section AND
2377 asymbol **symbols AND
2379 CONST char **filename_ptr AND
2380 CONST char **functionname_ptr AND
2381 unsigned int *line_ptr)
2383 fprintf (stderr, "elf_find_nearest_line unimplemented\n");
2390 DEFUN (elf_sizeof_headers, (abfd, reloc),
2394 fprintf (stderr, "elf_sizeof_headers unimplemented\n");
2401 DEFUN(elf_set_section_contents, (abfd, section, location, offset, count),
2406 bfd_size_type count)
2410 if (abfd->output_has_begun == false) /* set by bfd.c handler? */
2412 /* do setup calculations (FIXME) */
2413 elf_compute_section_file_positions(abfd);
2416 if(bfd_seek (abfd, (file_ptr)section->filepos + offset, SEEK_SET) == -1)
2418 if(bfd_write (location, (bfd_size_type)1, count, abfd) != count)
2421 /* we really just need to save the contents away... */
2422 dest_sect = elf_section_from_bfd_section(abfd, section);
2426 /* FIXME: allocate in set_section_size, then copy in here... */
2427 contents = (void*)bfd_alloc(abfd, count);
2428 BFD_ASSERT(contents);
2429 memcpy(contents, location, count);
2430 elf_elfsections (abfd)[dest_sect].contents = contents;
2436 /* This structure contains everything that BFD knows about a target.
2437 It includes things like its byte order, name, what routines to call
2438 to do various operations, etc. Every BFD points to a target structure
2439 with its "xvec" member.
2441 There are two such structures here: one for big-endian machines and
2442 one for little-endian machines. */
2444 /* Archives are generic or unimplemented. */
2445 #define elf_slurp_armap bfd_false
2446 #define elf_slurp_extended_name_table _bfd_slurp_extended_name_table
2447 #define elf_truncate_arname bfd_dont_truncate_arname
2448 #define elf_openr_next_archived_file bfd_generic_openr_next_archived_file
2449 #define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt
2450 #define elf_write_armap (PROTO (boolean, (*), \
2451 (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count, \
2452 int stridx))) bfd_false
2454 /* Ordinary section reading and writing */
2455 #define elf_new_section_hook _bfd_dummy_new_section_hook
2456 #define elf_get_section_contents bfd_generic_get_section_contents
2457 /* #define elf_set_section_contents bfd_generic_set_section_contents */
2458 #define elf_close_and_cleanup bfd_generic_close_and_cleanup
2460 #define elf_bfd_debug_info_start bfd_void
2461 #define elf_bfd_debug_info_end bfd_void
2462 #define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
2463 #define elf_bfd_get_relocated_section_contents \
2464 bfd_generic_get_relocated_section_contents
2465 #define elf_bfd_relax_section bfd_generic_relax_section
2466 bfd_target elf_big_vec =
2468 /* name: identify kind of target */
2471 /* flavour: general indication about file */
2472 bfd_target_elf_flavour,
2474 /* byteorder_big_p: data is big endian */
2477 /* header_byteorder_big_p: header is also big endian */
2480 /* object_flags: mask of all file flags */
2481 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
2484 /* section_flags: mask of all section flags */
2485 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
2486 SEC_CODE | SEC_DATA),
2488 /* ar_pad_char: pad character for filenames within an archive header
2489 FIXME: this really has nothing to do with ELF, this is a characteristic
2490 of the archiver and/or os and should be independently tunable */
2493 /* ar_max_namelen: maximum number of characters in an archive header
2494 FIXME: this really has nothing to do with ELF, this is a characteristic
2495 of the archiver and should be independently tunable. This value is
2496 a WAG (wild a** guess) */
2499 /* align_power_min: minimum alignment restriction for any section
2500 FIXME: this value may be target machine dependent */
2503 /* Routines to byte-swap various sized integers from the data sections */
2504 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
2506 /* Routines to byte-swap various sized integers from the file headers */
2507 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
2509 /* bfd_check_format: check the format of a file being read */
2510 { _bfd_dummy_target, /* unknown format */
2511 elf_object_p, /* assembler/linker output (object file) */
2512 bfd_generic_archive_p, /* an archive */
2513 elf_core_file_p /* a core file */
2516 /* bfd_set_format: set the format of a file being written */
2519 _bfd_generic_mkarchive,
2523 /* bfd_write_contents: write cached information into a file being written */
2525 elf_write_object_contents,
2526 _bfd_write_archive_contents,
2530 /* Initialize a jump table with the standard macro. All names start
2538 bfd_target elf_little_vec =
2540 /* name: identify kind of target */
2543 /* flavour: general indication about file */
2544 bfd_target_elf_flavour,
2546 /* byteorder_big_p: data is big endian */
2547 false, /* Nope -- this one's little endian */
2549 /* header_byteorder_big_p: header is also big endian */
2550 false, /* Nope -- this one's little endian */
2552 /* object_flags: mask of all file flags */
2553 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
2556 /* section_flags: mask of all section flags */
2557 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
2560 /* ar_pad_char: pad character for filenames within an archive header
2561 FIXME: this really has nothing to do with ELF, this is a characteristic
2562 of the archiver and/or os and should be independently tunable */
2565 /* ar_max_namelen: maximum number of characters in an archive header
2566 FIXME: this really has nothing to do with ELF, this is a characteristic
2567 of the archiver and should be independently tunable. This value is
2568 a WAG (wild a** guess) */
2571 /* align_power_min: minimum alignment restriction for any section
2572 FIXME: this value may be target machine dependent */
2575 /* Routines to byte-swap various sized integers from the data sections */
2576 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
2578 /* Routines to byte-swap various sized integers from the file headers */
2579 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
2581 /* bfd_check_format: check the format of a file being read */
2582 { _bfd_dummy_target, /* unknown format */
2583 elf_object_p, /* assembler/linker output (object file) */
2584 bfd_generic_archive_p, /* an archive */
2585 elf_core_file_p /* a core file */
2588 /* bfd_set_format: set the format of a file being written */
2591 _bfd_generic_mkarchive,
2595 /* bfd_write_contents: write cached information into a file being written */
2597 elf_write_object_contents,
2598 _bfd_write_archive_contents,
2602 /* Initialize a jump table with the standard macro. All names start