Split non-target-dependent code out of target_attach routines.
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
37ac3b76
ME
1/* ELF executable support for BFD.
2 Copyright (C) 1991, 1992 Free Software Foundation, Inc.
9ce0058c
SC
3
4 Written by Fred Fish @ Cygnus Support, from information published
5 in "UNIX System V Release 4, Programmers Guide: ANSI C and
3dfa6cfb 6 Programming Support Tools". Sufficient support for gdb.
37ac3b76
ME
7
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
12 utilities.
13
9ce0058c
SC
14This file is part of BFD, the Binary File Descriptor library.
15
16This program is free software; you can redistribute it and/or modify
17it under the terms of the GNU General Public License as published by
18the Free Software Foundation; either version 2 of the License, or
19(at your option) any later version.
20
21This program is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, write to the Free Software
28Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29
30
31 /****************************************
32
33 WARNING
34
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.
40
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
47 future version.
48
49 fnf@cygnus.com
50
51 *****************************************/
52
53
54/* Problems and other issues to resolve.
55
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.
63
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
68 "sections".
69
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)
74
75*/
76
9ce0058c 77#include "bfd.h"
e0796d22 78#include "sysdep.h"
9ce0058c
SC
79#include "libbfd.h"
80#include "obstack.h"
c3eb25fc
SC
81#include "elf/common.h"
82#include "elf/internal.h"
83#include "elf/external.h"
9ce0058c 84
8c4a1ace
JG
85#ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
86#include <sys/procfs.h>
87#else
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 */
91#endif
92
9ce0058c 93/* Forward data declarations */
8c4a1ace 94
9ce0058c
SC
95extern bfd_target elf_little_vec, elf_big_vec;
96
8c4a1ace
JG
97/* Currently the elf_symbol_type struct just contains the generic bfd
98 symbol structure. */
99
100typedef struct
101{
102 asymbol symbol;
103} elf_symbol_type;
104
105/* Some private data is stashed away for future use using the tdata pointer
80bdcb77 106 in the bfd structure. */
8c4a1ace 107
80bdcb77 108struct elf_obj_tdata
8c4a1ace 109{
80bdcb77 110 Elf_Internal_Ehdr elf_header[1]; /* Actual data, but ref like ptr */
37ac3b76
ME
111 Elf_Internal_Shdr *elf_sect_ptr;
112 struct strtab *strtab_ptr;
113 int symtab_section;
80bdcb77
JG
114 void *prstatus; /* The raw /proc prstatus structure */
115 void *prpsinfo; /* The raw /proc prpsinfo structure */
116};
8c4a1ace 117
e98e6ec1 118#define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data)
37ac3b76
ME
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)
80bdcb77
JG
123#define core_prpsinfo(bfd) (elf_tdata(bfd) -> prpsinfo)
124#define core_prstatus(bfd) (elf_tdata(bfd) -> prstatus)
8c4a1ace
JG
125
126/* Translate an ELF symbol in external format into an ELF symbol in internal
127 format. */
128
129static void
130DEFUN(elf_swap_symbol_in,(abfd, src, dst),
131 bfd *abfd AND
132 Elf_External_Sym *src AND
133 Elf_Internal_Sym *dst)
134{
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);
141}
142
37ac3b76
ME
143/* Translate an ELF symbol in internal format into an ELF symbol in external
144 format. */
145
146static void
147DEFUN(elf_swap_symbol_out,(abfd, src, dst),
148 bfd *abfd AND
149 Elf_Internal_Sym *src AND
150 Elf_External_Sym *dst)
151{
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);
158}
159
8c4a1ace 160
e83f3040
FF
161/* Translate an ELF file header in external format into an ELF file header in
162 internal format. */
9ce0058c
SC
163
164static void
8c4a1ace 165DEFUN(elf_swap_ehdr_in,(abfd, src, dst),
9ce0058c
SC
166 bfd *abfd AND
167 Elf_External_Ehdr *src AND
168 Elf_Internal_Ehdr *dst)
169{
e98e6ec1 170 memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT);
9ce0058c
SC
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);
184}
185
37ac3b76
ME
186/* Translate an ELF file header in internal format into an ELF file header in
187 external format. */
188
189static void
190DEFUN(elf_swap_ehdr_out,(abfd, src, dst),
191 bfd *abfd AND
192 Elf_Internal_Ehdr *src AND
193 Elf_External_Ehdr *dst)
194{
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);
210}
211
9ce0058c
SC
212
213/* Translate an ELF section header table entry in external format into an
214 ELF section header table entry in internal format. */
215
216static void
8c4a1ace 217DEFUN(elf_swap_shdr_in,(abfd, src, dst),
9ce0058c
SC
218 bfd *abfd AND
219 Elf_External_Shdr *src AND
220 Elf_Internal_Shdr *dst)
221{
37ac3b76
ME
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;
234}
235
236/* Translate an ELF section header table entry in internal format into an
237 ELF section header table entry in external format. */
238
239static void
240DEFUN(elf_swap_shdr_out,(abfd, src, dst),
241 bfd *abfd AND
242 Elf_Internal_Shdr *src AND
243 Elf_External_Shdr *dst)
244{
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);
9ce0058c
SC
256}
257
258
e0796d22
FF
259/* Translate an ELF program header table entry in external format into an
260 ELF program header table entry in internal format. */
261
262static void
8c4a1ace 263DEFUN(elf_swap_phdr_in,(abfd, src, dst),
e0796d22
FF
264 bfd *abfd AND
265 Elf_External_Phdr *src AND
266 Elf_Internal_Phdr *dst)
267{
37ac3b76
ME
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);
e0796d22
FF
276}
277
278
37ac3b76
ME
279/* Translate an ELF reloc from external format to internal format. */
280static void
281DEFUN(elf_swap_reloc_in,(abfd, src, dst),
282 bfd *abfd AND
283 Elf_External_Rel *src AND
284 Elf_Internal_Rel *dst)
285{
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);
288}
9ce0058c 289
37ac3b76
ME
290static void
291DEFUN(elf_swap_reloca_in,(abfd, src, dst),
292 bfd *abfd AND
293 Elf_External_Rela *src AND
294 Elf_Internal_Rela *dst)
9ce0058c 295{
37ac3b76
ME
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);
299}
9ce0058c 300
37ac3b76
ME
301/* Translate an ELF reloc from internal format to external format. */
302static void
303DEFUN(elf_swap_reloc_out,(abfd, src, dst),
304 bfd *abfd AND
305 Elf_Internal_Rel *src AND
306 Elf_External_Rel *dst)
307{
308 bfd_h_put_32 (abfd, src->r_offset, dst->r_offset);
309 bfd_h_put_32 (abfd, src->r_info, dst->r_info);
310}
311
312static void
313DEFUN(elf_swap_reloca_out,(abfd, src, dst),
314 bfd *abfd AND
315 Elf_Internal_Rela *src AND
316 Elf_External_Rela *dst)
317{
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);
321}
322
323static char *EXFUN(elf_read, (bfd *, long, int));
324static struct sec * EXFUN(section_from_elf_index, (bfd *, int));
325static int EXFUN(elf_section_from_bfd_section, (bfd *, struct sec *));
3dfa6cfb 326static boolean EXFUN(elf_slurp_symbol_table, (bfd *, asymbol **));
37ac3b76 327static void EXFUN(elf_info_to_howto, (bfd *, arelent *, Elf_Internal_Rela *));
80bdcb77 328static char *EXFUN(elf_get_str_section, (bfd *, unsigned int));
37ac3b76 329
796aae47
JG
330/*
331INTERNAL_FUNCTION
332 bfd_elf_find_section
333
334SYNOPSIS
335 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
336
337DESCRIPTION
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.
343*/
80bdcb77 344
796aae47
JG
345struct elf_internal_shdr *
346DEFUN(bfd_elf_find_section, (abfd, name),
80bdcb77
JG
347 bfd *abfd AND
348 char *name)
349{
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;
353 unsigned int i;
354
355 for (i = 1; i < max; i++)
356 if (!strcmp (&shstrtab[i_shdrp[i].sh_name], name))
357 return &i_shdrp[i];
358 return 0;
359}
360
361/* End of GDB support. */
37ac3b76
ME
362
363static char *
364DEFUN(elf_get_str_section, (abfd, shindex),
365 bfd *abfd AND
366 unsigned int shindex)
367{
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;
80bdcb77
JG
371 char *shstrtab = i_shdrp[shindex].rawdata;
372
373 if (shstrtab)
374 return shstrtab;
375
37ac3b76 376 if ((shstrtab = elf_read (abfd, offset, shstrtabsize)) == NULL)
9ce0058c 377 {
37ac3b76 378 return (NULL);
9ce0058c 379 }
37ac3b76 380 i_shdrp[shindex].rawdata = (void*)shstrtab;
80bdcb77 381 return shstrtab;
37ac3b76 382}
80bdcb77 383
37ac3b76
ME
384static char *
385DEFUN(elf_string_from_elf_section, (abfd, shindex, strindex),
386 bfd *abfd AND
387 unsigned int shindex AND
388 unsigned int strindex)
389{
390 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
391 Elf_Internal_Shdr *hdr = i_shdrp + shindex;
80bdcb77 392
37ac3b76 393 if (! hdr->rawdata)
9ce0058c 394 {
37ac3b76 395 if (elf_get_str_section (abfd, shindex) == NULL)
9ce0058c 396 {
37ac3b76 397 return NULL;
9ce0058c
SC
398 }
399 }
37ac3b76
ME
400 return ((char*)hdr->rawdata)+strindex;
401}
402
403#define elf_string_from_elf_strtab(abfd, strindex) \
404 elf_string_from_elf_section (abfd, elf_elfheader(abfd)->e_shstrndx, strindex)
405
406/* Create a new bfd section from an ELF section header. */
407
408static boolean
409DEFUN(bfd_section_from_shdr, (abfd, shindex),
410 bfd *abfd AND
411 unsigned int shindex)
412{
413 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
414 Elf_Internal_Shdr *hdr = i_shdrp + shindex;
415 asection *newsect;
416 char *name;
417
418 name = hdr->sh_name ?
419 elf_string_from_elf_strtab (abfd, hdr->sh_name) : "unnamed";
420
421 switch(hdr->sh_type) {
3dfa6cfb 422
37ac3b76
ME
423 case SHT_NULL:
424 /* inactive section. Throw it away. */
425 return true;
3dfa6cfb 426
37ac3b76
ME
427 case SHT_PROGBITS:
428 case SHT_NOBITS:
429 /* Bits that get saved. This one is real. */
430 if (! hdr->rawdata )
431 {
432 newsect = bfd_make_section (abfd, name);
433 newsect->vma = hdr->sh_addr;
434 newsect->_raw_size = hdr->sh_size;
435 newsect->filepos = hdr->sh_offset; /* so we can read back the bits */
436 newsect->flags |= SEC_HAS_CONTENTS;
437
438 if (hdr->sh_flags & SHF_ALLOC)
439 {
440 newsect->flags |= SEC_ALLOC;
441 if (hdr->sh_type != SHT_NOBITS)
442 newsect->flags |= SEC_LOAD;
443 }
444
445 if (!(hdr->sh_flags & SHF_WRITE))
446 newsect->flags |= SEC_READONLY;
447
448 if (hdr->sh_flags & SHF_EXECINSTR)
449 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
450 else
451 newsect->flags |= SEC_DATA;
452
453 hdr->rawdata = (void*)newsect;
454 }
455 return true;
456 break;
3dfa6cfb
FF
457
458 case SHT_SYMTAB: /* A symbol table */
459 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
460 elf_onesymtab (abfd) = shindex;
461 abfd->flags |= HAS_SYMS;
37ac3b76 462 return true;
3dfa6cfb
FF
463
464 case SHT_STRTAB: /* A string table */
37ac3b76 465 return true;
3dfa6cfb 466
37ac3b76
ME
467 case SHT_REL:
468 case SHT_RELA:
469 /* *these* do a lot of work -- but build no sections! */
470 /* the spec says there can be multiple strtabs, but only one symtab */
471 /* but there can be lots of REL* sections. */
91f781ff
FF
472 /* FIXME: The above statement is wrong! There are typically at least
473 two symbol tables in a dynamically linked executable, ".dynsym"
474 which is the dynamic linkage symbol table and ".symtab", which is
475 the "traditional" symbol table. -fnf */
476
9ce0058c 477 {
37ac3b76
ME
478 asection *target_sect;
479 unsigned int idx;
480
481 bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */
482 bfd_section_from_shdr (abfd, hdr->sh_info); /* target */
483 target_sect = section_from_elf_index (abfd, hdr->sh_info);
3dfa6cfb
FF
484 if (target_sect == NULL)
485 return false;
486
91f781ff
FF
487#if 0
488 /* FIXME: We are only prepared to read one symbol table, so
489 do NOT read the dynamic symbol table since it is only a
490 subset of the full symbol table. Also see comment above. -fnf */
37ac3b76
ME
491 if (!elf_slurp_symbol_table(abfd, i_shdrp + hdr->sh_link))
492 return false;
91f781ff 493#endif
37ac3b76
ME
494
495 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
496 target_sect->flags |= SEC_RELOC;
497 target_sect->relocation = 0;
498 target_sect->rel_filepos = hdr->sh_offset;
37ac3b76 499 return true;
9ce0058c 500 }
37ac3b76 501 break;
3dfa6cfb 502
37ac3b76
ME
503 case SHT_HASH:
504 case SHT_DYNAMIC:
505 case SHT_DYNSYM: /* could treat this like symtab... */
80bdcb77 506#if 0
37ac3b76
ME
507 fprintf(stderr, "Dynamic Linking sections not yet supported.\n");
508 abort ();
80bdcb77 509#endif
37ac3b76 510 break;
3dfa6cfb 511
37ac3b76 512 case SHT_NOTE:
80bdcb77 513#if 0
37ac3b76
ME
514 fprintf(stderr, "Note Sections not yet supported.\n");
515 abort ();
80bdcb77 516#endif
37ac3b76 517 break;
3dfa6cfb 518
37ac3b76 519 case SHT_SHLIB:
80bdcb77 520#if 0
37ac3b76 521 fprintf(stderr, "SHLIB Sections not supported (and non conforming.)\n");
80bdcb77 522#endif
37ac3b76 523 return true;
80bdcb77 524
37ac3b76
ME
525 default:
526 break;
527 }
528
529 return (true);
530}
531
3dfa6cfb 532
37ac3b76
ME
533
534
535struct strtab {
536 char *tab;
537 int nentries;
538 int length;
539};
540
541
542static struct strtab *
543DEFUN(bfd_new_strtab, (abfd),
544 bfd *abfd)
545{
546 struct strtab *ss;
547
548 ss = (struct strtab *)malloc(sizeof(struct strtab));
549 ss->tab = malloc(1);
550 BFD_ASSERT(ss->tab != 0);
551 *ss->tab = 0;
552 ss->nentries = 0;
553 ss->length = 1;
554
555 return ss;
556}
557
558static int
559DEFUN(bfd_add_to_strtab, (abfd, ss, str),
560 bfd *abfd AND
561 struct strtab *ss AND
808dfd5a 562 CONST char *str)
37ac3b76
ME
563{
564 /* should search first, but for now: */
565 /* include the trailing NUL */
566 int ln = strlen(str)+1;
567
568 /* should this be using obstacks? */
569 ss->tab = realloc(ss->tab, ss->length + ln);
570
571 BFD_ASSERT(ss->tab != 0);
572 strcpy(ss->tab + ss->length, str);
573 ss->nentries++;
574 ss->length += ln;
575
576 return ss->length - ln;
577}
578
579static int
580DEFUN(bfd_add_2_to_strtab, (abfd, ss, str, str2),
581 bfd *abfd AND
582 struct strtab *ss AND
583 char *str AND
808dfd5a 584 CONST char *str2)
37ac3b76
ME
585{
586 /* should search first, but for now: */
587 /* include the trailing NUL */
588 int ln = strlen(str)+strlen(str2)+1;
589
590 /* should this be using obstacks? */
591 if (ss->length)
592 ss->tab = realloc(ss->tab, ss->length + ln);
593 else
594 ss->tab = malloc(ln);
595
596 BFD_ASSERT(ss->tab != 0);
597 strcpy(ss->tab + ss->length, str);
598 strcpy(ss->tab + ss->length + strlen(str), str2);
599 ss->nentries++;
600 ss->length += ln;
601
602 return ss->length - ln;
603}
604
605/* Create a new ELF section from a bfd section. */
606
607static boolean
608DEFUN(bfd_shdr_from_section, (abfd, hdr, shstrtab, indx),
609 bfd *abfd AND
610 Elf_Internal_Shdr *hdr AND
611 struct strtab *shstrtab AND
612 int indx)
613{
614 asection *sect;
615 int ndx;
616
617 /* figure out out to write the section name from the bfd section name. MWE */
618
619 sect = abfd->sections;
620 for (ndx = indx; --ndx; )
9ce0058c 621 {
37ac3b76 622 sect = sect->next;
9ce0058c 623 }
37ac3b76
ME
624 hdr[indx].sh_name = bfd_add_to_strtab(abfd, shstrtab,
625 bfd_section_name(abfd, sect));
626 hdr[indx].sh_addr = sect->vma;
627 hdr[indx].sh_size = sect->_raw_size;
628 hdr[indx].sh_flags = 0;
629 /* these need to be preserved on */
630 hdr[indx].sh_link = 0;
631 hdr[indx].sh_info = 0;
632 hdr[indx].sh_addralign = 0;
633 hdr[indx].sh_entsize = 0;
634
635 hdr[indx].sh_type = 0;
636 if (sect->flags & SEC_RELOC) {
637 hdr[indx].sh_type = SHT_RELA; /* FIXME -- sparc specific */
638 }
639
640 if (sect->flags & SEC_HAS_CONTENTS)
e83f3040 641 {
37ac3b76
ME
642 hdr[indx].sh_offset = sect->filepos;
643 hdr[indx].sh_size = sect->_raw_size;
e83f3040 644 }
37ac3b76 645 if (sect->flags & SEC_ALLOC)
9ce0058c 646 {
37ac3b76
ME
647 hdr[indx].sh_flags |= SHF_ALLOC;
648 if (sect->flags & SEC_LOAD)
649 {
650 /* do something with sh_type ? */
651 }
9ce0058c 652 }
37ac3b76
ME
653 if (!(sect->flags & SEC_READONLY))
654 hdr[indx].sh_flags |= SHF_WRITE;
655
656 if (sect->flags & SEC_CODE)
657 hdr[indx].sh_flags |= SHF_EXECINSTR;
9ce0058c
SC
658
659 return (true);
660}
661
e0796d22
FF
662/* Create a new bfd section from an ELF program header.
663
664 Since program segments have no names, we generate a synthetic name
665 of the form segment<NUM>, where NUM is generally the index in the
666 program header table. For segments that are split (see below) we
667 generate the names segment<NUM>a and segment<NUM>b.
668
669 Note that some program segments may have a file size that is different than
670 (less than) the memory size. All this means is that at execution the
671 system must allocate the amount of memory specified by the memory size,
672 but only initialize it with the first "file size" bytes read from the
673 file. This would occur for example, with program segments consisting
674 of combined data+bss.
675
676 To handle the above situation, this routine generates TWO bfd sections
677 for the single program segment. The first has the length specified by
678 the file size of the segment, and the second has the length specified
679 by the difference between the two sizes. In effect, the segment is split
680 into it's initialized and uninitialized parts.
681
682 */
683
684static boolean
685DEFUN(bfd_section_from_phdr, (abfd, hdr, index),
686 bfd *abfd AND
687 Elf_Internal_Phdr *hdr AND
688 int index)
689{
690 asection *newsect;
691 char *name;
692 char namebuf[64];
693 int split;
694
695 split = ((hdr -> p_memsz > 0) &&
696 (hdr -> p_filesz > 0) &&
697 (hdr -> p_memsz > hdr -> p_filesz));
698 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
699 name = bfd_alloc (abfd, strlen (namebuf) + 1);
97225e37 700 strcpy (name, namebuf);
e0796d22
FF
701 newsect = bfd_make_section (abfd, name);
702 newsect -> vma = hdr -> p_vaddr;
e98e6ec1 703 newsect -> _raw_size = hdr -> p_filesz;
e0796d22
FF
704 newsect -> filepos = hdr -> p_offset;
705 newsect -> flags |= SEC_HAS_CONTENTS;
706 if (hdr -> p_type == PT_LOAD)
707 {
708 newsect -> flags |= SEC_ALLOC;
709 newsect -> flags |= SEC_LOAD;
710 if (hdr -> p_flags & PF_X)
711 {
712 /* FIXME: all we known is that it has execute PERMISSION,
713 may be data. */
714 newsect -> flags |= SEC_CODE;
715 }
716 }
717 if (!(hdr -> p_flags & PF_W))
718 {
719 newsect -> flags |= SEC_READONLY;
720 }
721
722 if (split)
723 {
724 sprintf (namebuf, "segment%db", index);
725 name = bfd_alloc (abfd, strlen (namebuf) + 1);
97225e37 726 strcpy (name, namebuf);
e0796d22
FF
727 newsect = bfd_make_section (abfd, name);
728 newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz;
e98e6ec1 729 newsect -> _raw_size = hdr -> p_memsz - hdr -> p_filesz;
e0796d22
FF
730 if (hdr -> p_type == PT_LOAD)
731 {
732 newsect -> flags |= SEC_ALLOC;
733 if (hdr -> p_flags & PF_X)
37ac3b76 734 newsect -> flags |= SEC_CODE;
e0796d22
FF
735 }
736 if (!(hdr -> p_flags & PF_W))
37ac3b76 737 newsect -> flags |= SEC_READONLY;
e0796d22
FF
738 }
739
740 return (true);
741}
742
8c4a1ace
JG
743#ifdef HAVE_PROCFS
744
745static void
746DEFUN(bfd_prstatus,(abfd, descdata, descsz, filepos),
747 bfd *abfd AND
748 char *descdata AND
749 int descsz AND
750 long filepos)
751{
752 asection *newsect;
37ac3b76 753 prstatus_t *status = (prstatus_t *)0;
8c4a1ace
JG
754
755 if (descsz == sizeof (prstatus_t))
756 {
757 newsect = bfd_make_section (abfd, ".reg");
37ac3b76
ME
758 newsect -> _raw_size = sizeof (status->pr_reg);
759 newsect -> filepos = filepos + (long) &status->pr_reg;
8c4a1ace
JG
760 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
761 newsect -> alignment_power = 2;
762 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
763 {
e98e6ec1 764 memcpy (core_prstatus (abfd), descdata, descsz);
8c4a1ace
JG
765 }
766 }
767}
768
769/* Stash a copy of the prpsinfo structure away for future use. */
770
771static void
772DEFUN(bfd_prpsinfo,(abfd, descdata, descsz, filepos),
773 bfd *abfd AND
774 char *descdata AND
775 int descsz AND
776 long filepos)
777{
778 asection *newsect;
779
780 if (descsz == sizeof (prpsinfo_t))
781 {
782 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
783 {
784 bcopy (descdata, core_prpsinfo (abfd), descsz);
785 }
786 }
787}
788
789static void
790DEFUN(bfd_fpregset,(abfd, descdata, descsz, filepos),
791 bfd *abfd AND
792 char *descdata AND
793 int descsz AND
794 long filepos)
795{
796 asection *newsect;
797
37ac3b76
ME
798 newsect = bfd_make_section (abfd, ".reg2");
799 newsect -> _raw_size = descsz;
800 newsect -> filepos = filepos;
801 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
802 newsect -> alignment_power = 2;
8c4a1ace
JG
803}
804
805#endif /* HAVE_PROCFS */
806
807/* Return a pointer to the args (including the command name) that were
808 seen by the program that generated the core dump. Note that for
809 some reason, a spurious space is tacked onto the end of the args
810 in some (at least one anyway) implementations, so strip it off if
811 it exists. */
812
813char *
814DEFUN(elf_core_file_failing_command, (abfd),
815 bfd *abfd)
816{
e98e6ec1 817#ifdef HAVE_PROCFS
8c4a1ace
JG
818 if (core_prpsinfo (abfd))
819 {
820 prpsinfo_t *p = core_prpsinfo (abfd);
821 char *scan = p -> pr_psargs;
822 while (*scan++) {;}
823 scan -= 2;
824 if ((scan > p -> pr_psargs) && (*scan == ' '))
825 {
826 *scan = '\000';
827 }
828 return (p -> pr_psargs);
829 }
830#endif
831 return (NULL);
832}
833
834/* Return the number of the signal that caused the core dump. Presumably,
835 since we have a core file, we got a signal of some kind, so don't bother
836 checking the other process status fields, just return the signal number.
837 */
838
839static int
840DEFUN(elf_core_file_failing_signal, (abfd),
841 bfd *abfd)
842{
e98e6ec1 843#ifdef HAVE_PROCFS
8c4a1ace
JG
844 if (core_prstatus (abfd))
845 {
846 return (((prstatus_t *)(core_prstatus (abfd))) -> pr_cursig);
847 }
848#endif
849 return (-1);
850}
851
852/* Check to see if the core file could reasonably be expected to have
853 come for the current executable file. Note that by default we return
854 true unless we find something that indicates that there might be a
855 problem.
856 */
857
858static boolean
859DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
860 bfd *core_bfd AND
861 bfd *exec_bfd)
862{
e98e6ec1 863#ifdef HAVE_PROCFS
8c4a1ace
JG
864 char *corename;
865 char *execname;
e83f3040 866#endif
8c4a1ace
JG
867
868 /* First, xvecs must match since both are ELF files for the same target. */
869
870 if (core_bfd->xvec != exec_bfd->xvec)
871 {
872 bfd_error = system_call_error;
873 return (false);
874 }
875
e98e6ec1 876#ifdef HAVE_PROCFS
8c4a1ace
JG
877
878 /* If no prpsinfo, just return true. Otherwise, grab the last component
879 of the exec'd pathname from the prpsinfo. */
880
881 if (core_prpsinfo (core_bfd))
882 {
883 corename = (((struct prpsinfo *) core_prpsinfo (core_bfd)) -> pr_fname);
884 }
885 else
886 {
887 return (true);
888 }
889
890 /* Find the last component of the executable pathname. */
891
892 if ((execname = strrchr (exec_bfd -> filename, '/')) != NULL)
893 {
894 execname++;
895 }
896 else
897 {
898 execname = (char *) exec_bfd -> filename;
899 }
900
901 /* See if they match */
902
903 return (strcmp (execname, corename) ? false : true);
904
905#else
906
907 return (true);
908
909#endif /* HAVE_PROCFS */
910}
911
912/* ELF core files contain a segment of type PT_NOTE, that holds much of
913 the information that would normally be available from the /proc interface
914 for the process, at the time the process dumped core. Currently this
915 includes copies of the prstatus, prpsinfo, and fpregset structures.
916
917 Since these structures are potentially machine dependent in size and
918 ordering, bfd provides two levels of support for them. The first level,
919 available on all machines since it does not require that the host
920 have /proc support or the relevant include files, is to create a bfd
921 section for each of the prstatus, prpsinfo, and fpregset structures,
922 without any interpretation of their contents. With just this support,
923 the bfd client will have to interpret the structures itself. Even with
924 /proc support, it might want these full structures for it's own reasons.
925
926 In the second level of support, where HAVE_PROCFS is defined, bfd will
927 pick apart the structures to gather some additional information that
928 clients may want, such as the general register set, the name of the
929 exec'ed file and its arguments, the signal (if any) that caused the
930 core dump, etc.
931
932 */
933
934static boolean
935DEFUN(elf_corefile_note, (abfd, hdr),
936 bfd *abfd AND
937 Elf_Internal_Phdr *hdr)
938{
939 Elf_External_Note *x_note_p; /* Elf note, external form */
940 Elf_Internal_Note i_note; /* Elf note, internal form */
941 char *buf = NULL; /* Entire note segment contents */
942 char *namedata; /* Name portion of the note */
943 char *descdata; /* Descriptor portion of the note */
944 char *sectname; /* Name to use for new section */
945 long filepos; /* File offset to descriptor data */
946 asection *newsect;
947
948 if (hdr -> p_filesz > 0
d4acec2c 949 && (buf = (char *) bfd_xmalloc (hdr -> p_filesz)) != NULL
8c4a1ace
JG
950 && bfd_seek (abfd, hdr -> p_offset, SEEK_SET) != -1L
951 && bfd_read ((PTR) buf, hdr -> p_filesz, 1, abfd) == hdr -> p_filesz)
952 {
953 x_note_p = (Elf_External_Note *) buf;
954 while ((char *) x_note_p < (buf + hdr -> p_filesz))
955 {
956 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> namesz);
957 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> descsz);
958 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> type);
959 namedata = x_note_p -> name;
960 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
961 filepos = hdr -> p_offset + (descdata - buf);
962 switch (i_note.type) {
963 case NT_PRSTATUS:
964 /* process descdata as prstatus info */
965 bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
966 sectname = ".prstatus";
967 break;
968 case NT_FPREGSET:
969 /* process descdata as fpregset info */
970 bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
971 sectname = ".fpregset";
972 break;
973 case NT_PRPSINFO:
974 /* process descdata as prpsinfo */
975 bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
976 sectname = ".prpsinfo";
977 break;
978 default:
979 /* Unknown descriptor, just ignore it. */
980 sectname = NULL;
981 break;
982 }
983 if (sectname != NULL)
984 {
985 newsect = bfd_make_section (abfd, sectname);
e98e6ec1 986 newsect -> _raw_size = i_note.descsz;
8c4a1ace
JG
987 newsect -> filepos = filepos;
988 newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS;
989 newsect -> alignment_power = 2;
990 }
991 x_note_p = (Elf_External_Note *)
992 (descdata + BFD_ALIGN (i_note.descsz, 4));
993 }
994 }
995 if (buf != NULL)
996 {
997 free (buf);
998 }
e83f3040
FF
999 return true;
1000
8c4a1ace
JG
1001}
1002
1003
e83f3040
FF
1004/* Read a specified number of bytes at a specified offset in an ELF
1005 file, into a newly allocated buffer, and return a pointer to the
1006 buffer. */
1007
1008static char *
1009DEFUN(elf_read, (abfd, offset, size),
1010 bfd *abfd AND
1011 long offset AND
1012 int size)
1013{
1014 char *buf;
1015
1016 if ((buf = bfd_alloc (abfd, size)) == NULL)
1017 {
1018 bfd_error = no_memory;
1019 return (NULL);
1020 }
1021 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
1022 {
1023 bfd_error = system_call_error;
1024 return (NULL);
1025 }
1026 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
1027 {
1028 bfd_error = system_call_error;
1029 return (NULL);
1030 }
1031 return (buf);
1032}
1033
9ce0058c
SC
1034/* Begin processing a given object.
1035
1036 First we validate the file by reading in the ELF header and checking
1037 the magic number.
1038
1039 */
1040
1041static bfd_target *
1042DEFUN (elf_object_p, (abfd), bfd *abfd)
1043{
1044 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
80bdcb77 1045 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
d4acec2c
FF
1046 Elf_External_Shdr x_shdr; /* Section header table entry, external form */
1047 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
9ce0058c
SC
1048 int shindex;
1049 char *shstrtab; /* Internal copy of section header stringtab */
e83f3040 1050 Elf_Off offset; /* Temp place to stash file offsets */
9ce0058c
SC
1051
1052 /* Read in the ELF header in external format. */
1053
1054 if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
1055 {
1056 bfd_error = system_call_error;
1057 return (NULL);
1058 }
1059
1060 /* Now check to see if we have a valid ELF file, and one that BFD can
1061 make use of. The magic number must match, the address size ('class')
1062 and byte-swapping must match our XVEC entry, and it must have a
1063 section header table (FIXME: See comments re sections at top of this
1064 file). */
1065
1066 if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
1067 x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
1068 x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
1069 x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
1070 {
1071wrong:
1072 bfd_error = wrong_format;
1073 return (NULL);
1074 }
1075
1076 /* FIXME, Check EI_VERSION here ! */
1077
37ac3b76
ME
1078 switch (x_ehdr.e_ident[EI_CLASS])
1079 {
1080 case ELFCLASSNONE: /* address size not specified */
1081 goto wrong; /* No support if can't tell address size */
1082 case ELFCLASS32: /* 32-bit addresses */
1083 break;
1084 case ELFCLASS64: /* 64-bit addresses */
1085 goto wrong; /* FIXME: 64 bits not yet supported */
1086 default:
1087 goto wrong; /* No support if unknown address class */
1088 }
9ce0058c
SC
1089
1090 /* Switch xvec to match the specified byte order. */
37ac3b76
ME
1091 switch (x_ehdr.e_ident[EI_DATA])
1092 {
1093 case ELFDATA2MSB: /* Big-endian */
1094 if (!abfd->xvec->header_byteorder_big_p)
1095 goto wrong;
1096 break;
1097 case ELFDATA2LSB: /* Little-endian */
1098 if (abfd->xvec->header_byteorder_big_p)
1099 goto wrong;
1100 break;
1101 case ELFDATANONE: /* No data encoding specified */
1102 default: /* Unknown data encoding specified */
d4acec2c 1103 goto wrong;
37ac3b76 1104 }
9ce0058c 1105
8c4a1ace
JG
1106 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
1107 the tdata pointer in the bfd. */
1108
80bdcb77
JG
1109 if (NULL == (elf_tdata (abfd) = (struct elf_obj_tdata *)
1110 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata))))
8c4a1ace
JG
1111 {
1112 bfd_error = no_memory;
1113 return (NULL);
1114 }
1115
80bdcb77
JG
1116 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
1117
9ce0058c 1118 /* Now that we know the byte order, swap in the rest of the header */
80bdcb77
JG
1119 i_ehdrp = elf_elfheader (abfd);
1120 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
37ac3b76 1121
e0796d22 1122 /* If there is no section header table, we're hosed. */
80bdcb77 1123 if (i_ehdrp->e_shoff == 0)
9ce0058c
SC
1124 goto wrong;
1125
80bdcb77 1126 if (i_ehdrp->e_type == ET_EXEC || i_ehdrp->e_type == ET_DYN)
37ac3b76
ME
1127 abfd -> flags |= EXEC_P;
1128
80bdcb77 1129 switch (i_ehdrp->e_machine)
9ce0058c 1130 {
37ac3b76
ME
1131 case EM_NONE:
1132 case EM_M32: /* or should this be bfd_arch_obscure? */
1133 bfd_default_set_arch_mach(abfd, bfd_arch_unknown, 0);
1134 break;
1135 case EM_SPARC:
1136 bfd_default_set_arch_mach(abfd, bfd_arch_sparc, 0);
1137 break;
1138 case EM_386:
1139 bfd_default_set_arch_mach(abfd, bfd_arch_i386, 0);
1140 break;
1141 case EM_68K:
1142 bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0);
1143 break;
1144 case EM_88K:
1145 bfd_default_set_arch_mach(abfd, bfd_arch_m88k, 0);
1146 break;
1147 case EM_860:
1148 bfd_default_set_arch_mach(abfd, bfd_arch_i860, 0);
1149 break;
1150 case EM_MIPS:
1151 bfd_default_set_arch_mach(abfd, bfd_arch_mips, 0);
1152 break;
1153 default:
1154 goto wrong;
9ce0058c 1155 }
37ac3b76
ME
1156
1157 /* Allocate space for a copy of the section header table in
1158 internal form, seek to the section header table in the file,
9ce0058c
SC
1159 read it in, and convert it to internal form. As a simple sanity
1160 check, verify that the what BFD thinks is the size of each section
1161 header table entry actually matches the size recorded in the file. */
1162
80bdcb77 1163 if (i_ehdrp->e_shentsize != sizeof (x_shdr))
9ce0058c 1164 goto wrong;
37ac3b76 1165 i_shdrp = (Elf_Internal_Shdr *)
80bdcb77 1166 bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum);
37ac3b76 1167 if (! i_shdrp)
9ce0058c
SC
1168 {
1169 bfd_error = no_memory;
1170 return (NULL);
1171 }
80bdcb77 1172 if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1)
9ce0058c
SC
1173 {
1174 bfd_error = system_call_error;
1175 return (NULL);
1176 }
80bdcb77 1177 for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
9ce0058c 1178 {
37ac3b76 1179 if (bfd_read ((PTR) &x_shdr, sizeof x_shdr, 1, abfd)
d4acec2c 1180 != sizeof (x_shdr))
9ce0058c
SC
1181 {
1182 bfd_error = system_call_error;
1183 return (NULL);
1184 }
d4acec2c 1185 elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
9ce0058c
SC
1186 }
1187
37ac3b76
ME
1188 elf_elfsections (abfd) = i_shdrp;
1189
9ce0058c
SC
1190 /* Read in the string table containing the names of the sections. We
1191 will need the base pointer to this table later. */
37ac3b76
ME
1192 /* We read this inline now, so that we don't have to go through
1193 bfd_section_from_shdr with it (since this particular strtab is
1194 used to find all of the ELF section names.) */
9ce0058c 1195
80bdcb77 1196 shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
37ac3b76
ME
1197 if (! shstrtab)
1198 return (NULL);
1199
9ce0058c 1200 /* Once all of the section headers have been read and converted, we
a6c1d731 1201 can start processing them. Note that the first section header is
8c4a1ace
JG
1202 a dummy placeholder entry, so we ignore it.
1203
1204 We also watch for the symbol table section and remember the file
1205 offset and section size for both the symbol table section and the
1206 associated string table section. */
9ce0058c 1207
80bdcb77 1208 for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
9ce0058c 1209 {
37ac3b76 1210 bfd_section_from_shdr (abfd, shindex);
9ce0058c
SC
1211 }
1212
e83f3040
FF
1213 /* Remember the entry point specified in the ELF file header. */
1214
80bdcb77 1215 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
e83f3040 1216
9ce0058c
SC
1217 return (abfd->xvec);
1218}
1219
e0796d22
FF
1220/* Core files are simply standard ELF formatted files that partition
1221 the file using the execution view of the file (program header table)
1222 rather than the linking view. In fact, there is no section header
1223 table in a core file.
8c4a1ace
JG
1224
1225 The process status information (including the contents of the general
1226 register set) and the floating point register set are stored in a
1227 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
1228 that allow standard bfd access to the general registers (.reg) and the
1229 floating point registers (.reg2).
1230
e0796d22
FF
1231 */
1232
1233static bfd_target *
1234DEFUN (elf_core_file_p, (abfd), bfd *abfd)
1235{
1236 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
80bdcb77 1237 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
d4acec2c
FF
1238 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
1239 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
37ac3b76 1240 unsigned int phindex;
e0796d22
FF
1241
1242 /* Read in the ELF header in external format. */
1243
1244 if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
1245 {
1246 bfd_error = system_call_error;
1247 return (NULL);
1248 }
1249
1250 /* Now check to see if we have a valid ELF file, and one that BFD can
1251 make use of. The magic number must match, the address size ('class')
1252 and byte-swapping must match our XVEC entry, and it must have a
1253 program header table (FIXME: See comments re segments at top of this
1254 file). */
1255
1256 if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
1257 x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
1258 x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
1259 x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
1260 {
1261wrong:
1262 bfd_error = wrong_format;
1263 return (NULL);
1264 }
1265
1266 /* FIXME, Check EI_VERSION here ! */
1267
37ac3b76
ME
1268 switch (x_ehdr.e_ident[EI_CLASS])
1269 {
1270 case ELFCLASSNONE: /* address size not specified */
1271 goto wrong; /* No support if can't tell address size */
1272 case ELFCLASS32: /* 32-bit addresses */
1273 break;
1274 case ELFCLASS64: /* 64-bit addresses */
1275 goto wrong; /* FIXME: 64 bits not yet supported */
1276 default:
1277 goto wrong; /* No support if unknown address class */
1278 }
e0796d22
FF
1279
1280 /* Switch xvec to match the specified byte order. */
37ac3b76
ME
1281 switch (x_ehdr.e_ident[EI_DATA])
1282 {
1283 case ELFDATA2MSB: /* Big-endian */
1284 abfd->xvec = &elf_big_vec;
1285 break;
1286 case ELFDATA2LSB: /* Little-endian */
1287 abfd->xvec = &elf_little_vec;
1288 break;
1289 case ELFDATANONE: /* No data encoding specified */
1290 default: /* Unknown data encoding specified */
1291 goto wrong;
1292 }
e0796d22 1293
80bdcb77 1294 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
8c4a1ace
JG
1295 the tdata pointer in the bfd. */
1296
80bdcb77
JG
1297 elf_tdata (abfd) =
1298 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
1299 if (elf_tdata (abfd) == NULL)
8c4a1ace
JG
1300 {
1301 bfd_error = no_memory;
1302 return (NULL);
1303 }
1304
80bdcb77
JG
1305 /* FIXME, `wrong' returns from this point onward, leak memory. */
1306
1307 /* Now that we know the byte order, swap in the rest of the header */
1308 i_ehdrp = elf_elfheader (abfd);
1309 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
1310
1311 /* If there is no program header, or the type is not a core file, then
1312 we are hosed. */
1313 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
1314 goto wrong;
1315
37ac3b76 1316 /* Allocate space for a copy of the program header table in
80bdcb77 1317 internal form, seek to the program header table in the file,
e0796d22
FF
1318 read it in, and convert it to internal form. As a simple sanity
1319 check, verify that the what BFD thinks is the size of each program
1320 header table entry actually matches the size recorded in the file. */
1321
80bdcb77 1322 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
e0796d22 1323 goto wrong;
37ac3b76 1324 i_phdrp = (Elf_Internal_Phdr *)
80bdcb77 1325 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
37ac3b76 1326 if (! i_phdrp)
e0796d22
FF
1327 {
1328 bfd_error = no_memory;
1329 return (NULL);
1330 }
80bdcb77 1331 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
e0796d22
FF
1332 {
1333 bfd_error = system_call_error;
1334 return (NULL);
1335 }
80bdcb77 1336 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
e0796d22 1337 {
d4acec2c
FF
1338 if (bfd_read ((PTR) &x_phdr, sizeof (x_phdr), 1, abfd)
1339 != sizeof (x_phdr))
e0796d22
FF
1340 {
1341 bfd_error = system_call_error;
1342 return (NULL);
1343 }
d4acec2c 1344 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
e0796d22
FF
1345 }
1346
1347 /* Once all of the program headers have been read and converted, we
1348 can start processing them. */
1349
80bdcb77 1350 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
e0796d22 1351 {
d4acec2c
FF
1352 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
1353 if ((i_phdrp + phindex) -> p_type == PT_NOTE)
8c4a1ace 1354 {
d4acec2c 1355 elf_corefile_note (abfd, i_phdrp + phindex);
8c4a1ace 1356 }
e0796d22
FF
1357 }
1358
e83f3040
FF
1359 /* Remember the entry point specified in the ELF file header. */
1360
80bdcb77 1361 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
e83f3040 1362
e0796d22
FF
1363 return (abfd->xvec);
1364}
1365
9ce0058c
SC
1366static boolean
1367DEFUN (elf_mkobject, (abfd), bfd *abfd)
1368{
37ac3b76
ME
1369 /* this just does initialization */
1370 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
80bdcb77
JG
1371 elf_tdata(abfd) = (struct elf_obj_tdata *)
1372 bfd_zalloc (abfd, sizeof(struct elf_obj_tdata));
37ac3b76
ME
1373 if (elf_tdata(abfd) == 0) {
1374 bfd_error = no_memory;
1375 return false;
1376 }
1377 /* since everything is done at close time, do we need any
1378 initialization? */
1379
1380 return (true);
1381}
1382
1383/*
1384 Create ELF output from BFD sections.
1385
1386 Essentially, just create the section header and forget about the program
1387 header for now.
1388
1389*/
1390
1391/* lacking nested functions and nested types, set up for mapping over
1392 BFD sections to produce ELF sections */
1393
1394typedef struct {
1395 Elf_Internal_Ehdr *i_ehdr;
1396 Elf_Internal_Shdr *i_shdrp;
1397 struct strtab *shstrtab;
1398 int symtab_section;
1399} elf_sect_thunk;
1400
1401
1402
1403static void
1404DEFUN (elf_make_sections, (abfd, asect, obj),
1405 bfd *abfd AND
1406 asection *asect AND
1407 PTR obj)
1408{
1409 elf_sect_thunk *thunk = (elf_sect_thunk*)obj;
1410 /* most of what is in bfd_shdr_from_section goes in here... */
1411 /* and all of these sections generate at *least* one ELF section. */
1412 int this_section;
1413 int idx;
1414
1415 /* check if we're making a PROGBITS section... */
1416 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1417 /* this was too strict... what *do* we want to check here? */
1418 if(1)
1419 {
1420 Elf_Internal_Shdr *this_hdr;
1421 this_section = elf_section_from_bfd_section (abfd, asect);
1422 this_hdr = &thunk->i_shdrp[this_section];
1423
1424 this_hdr->sh_addr = asect->vma;
1425 this_hdr->sh_size = asect->_raw_size;
1426 /* contents already set by elf_set_section_contents */
1427
1428 if (asect->flags & SEC_RELOC)
1429 {
1430 /* emit a reloc section, and thus strtab and symtab... */
1431 Elf_Internal_Shdr *rela_hdr;
1432 Elf_Internal_Shdr *symtab_hdr;
1433 Elf_Internal_Shdr *symstrtab_hdr;
1434 Elf_External_Rela *outbound_relocs;
1435 Elf_External_Sym *outbound_syms;
1436 int rela_section;
1437 int symstrtab_section;
1438
1439 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1440
1441 if (thunk->symtab_section == this_section + 1)
1442 rela_section = thunk->symtab_section + 2; /* symtab + symstrtab */
1443 else
1444 rela_section = this_section + 1;
1445 rela_hdr = &thunk->i_shdrp[rela_section];
1446 rela_hdr->sh_type = SHT_RELA;
1447 rela_hdr->sh_link = thunk->symtab_section;
1448 rela_hdr->sh_info = this_section;
1449 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1450 /* orelocation has the data, reloc_count has the count... */
1451 rela_hdr->sh_size = rela_hdr->sh_entsize * asect->reloc_count;
37ac3b76
ME
1452 outbound_relocs = (Elf_External_Rela *)
1453 bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela));
1454 for (idx = 0; idx < asect->reloc_count; idx++)
1455 {
1456 Elf_Internal_Rela dst;
1457 arelent *ptr;
1458 Elf_External_Rela *src;
1459
1460 ptr = asect->orelocation[idx];
1461 src = outbound_relocs + idx;
1462 if (asect->flags & SEC_RELOC)
1463 dst.r_offset = ptr->address - asect->vma;
1464 else
1465 dst.r_offset = ptr->address;
1466
1467 dst.r_info = ELF_R_INFO(1 /*ptr->sym_ptr_ptr*/, /* needs index into symtab (FIXME) */
1468 ptr->howto->type);
1469
1470 dst.r_addend = ptr->addend;
1471 elf_swap_reloca_out(abfd, &dst, src);
1472 }
1473 rela_hdr->contents = (void*)outbound_relocs;
1474 }
1475 }
1476}
1477
1478static void
1479DEFUN (elf_fake_sections, (abfd, asect, obj),
1480 bfd *abfd AND
1481 asection *asect AND
1482 PTR obj)
1483{
1484 elf_sect_thunk *thunk = (elf_sect_thunk*)obj;
1485 /* most of what is in bfd_shdr_from_section goes in here... */
1486 /* and all of these sections generate at *least* one ELF section. */
1487 int this_section;
1488 int idx;
1489
1490 /* check if we're making a PROGBITS section... */
1491 /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */
1492 /* this was too strict... what *do* we want to check here? */
1493 if(1)
1494 {
1495 Elf_Internal_Shdr *this_hdr;
1496 this_section = thunk->i_ehdr->e_shnum++;
1497 this_hdr = &thunk->i_shdrp[this_section];
1498 this_hdr->sh_name =
1499 bfd_add_to_strtab (abfd, thunk->shstrtab, asect->name);
1500 /* we need to log the type *now* so that elf_section_from_bfd_section
1501 can find us... have to set rawdata too. */
1502 this_hdr->rawdata = (void*)asect;
1503 if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1504 this_hdr->sh_type = SHT_PROGBITS;
1505 else
1506 /* what *do* we put here? */
1507 this_hdr->sh_type = SHT_PROGBITS;
1508
1509
1510 if (asect->flags & SEC_RELOC)
1511 {
1512 /* emit a reloc section, and thus strtab and symtab... */
1513 Elf_Internal_Shdr *rela_hdr;
1514 Elf_Internal_Shdr *symtab_hdr;
1515 Elf_Internal_Shdr *symstrtab_hdr;
1516 Elf_External_Rela *outbound_relocs;
1517 Elf_External_Sym *outbound_syms;
1518 int rela_section;
1519 int symstrtab_section;
1520
1521 /* note that only one symtab is used, so just remember it
1522 for now */
1523 if (! thunk->symtab_section)
1524 {
1525 thunk->symtab_section = thunk->i_ehdr->e_shnum++;
1526 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1527 symtab_hdr->sh_name =
1528 bfd_add_to_strtab (abfd, thunk->shstrtab, ".symtab");
1529 symtab_hdr->sh_type = SHT_SYMTAB;
1530 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
1531
1532 symstrtab_section = thunk->i_ehdr->e_shnum++;
1533 BFD_ASSERT(symstrtab_section == thunk->symtab_section+1);
1534 symstrtab_hdr = &thunk->i_shdrp[symstrtab_section];
1535 symtab_hdr->sh_link = symstrtab_section;
1536 symstrtab_hdr->sh_name =
1537 bfd_add_to_strtab (abfd, thunk->shstrtab, ".strtab");
1538 symstrtab_hdr->sh_type = SHT_STRTAB;
1539
1540 symtab_hdr->contents = 0;
1541 symstrtab_hdr->contents = 0;
1542 symstrtab_hdr->sh_size = 0;
1543 }
1544 else
1545 symtab_hdr = &thunk->i_shdrp[thunk->symtab_section];
1546
1547 rela_section = thunk->i_ehdr->e_shnum++;
1548 rela_hdr = &thunk->i_shdrp[rela_section];
1549 rela_hdr->sh_name =
1550 bfd_add_2_to_strtab (abfd, thunk->shstrtab, ".rela", asect->name);
1551 rela_hdr->sh_type = SHT_RELA;
1552 rela_hdr->sh_link = thunk->symtab_section;
1553 rela_hdr->sh_info = this_section;
1554 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1555 }
1556 }
1557}
1558
1559
1560static boolean
1561DEFUN (elf_compute_section_file_positions, (abfd), bfd *abfd)
1562{
1563 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1564 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1565 struct strtab *shstrtab;
1566 int count, maxsections;
1567 int outbase;
1568 elf_sect_thunk est;
1569
1570 if (! elf_shstrtab (abfd)) {
80bdcb77
JG
1571 i_ehdrp = elf_elfheader (abfd); /* build new header in tdata memory */
1572 shstrtab = bfd_new_strtab(abfd);
37ac3b76 1573
80bdcb77
JG
1574 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
1575 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
1576 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
1577 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
1578
1579 i_ehdrp->e_ident[EI_CLASS] = ELFCLASS32; /* FIXME: find out from bfd */
1580 i_ehdrp->e_ident[EI_DATA] =
1581 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
1582 i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
1583
1584 for(count = EI_PAD; count < EI_NIDENT; count ++)
1585 i_ehdrp->e_ident[count] = 0;
1586
1587 i_ehdrp->e_type = (abfd->flags & EXEC_P)? ET_EXEC : ET_REL;
1588 switch(bfd_get_arch(abfd))
1589 {
1590 case bfd_arch_unknown:
1591 i_ehdrp->e_machine = EM_NONE;
1592 break;
1593 case bfd_arch_sparc:
1594 i_ehdrp->e_machine = EM_SPARC;
1595 break;
1596 case bfd_arch_i386:
1597 i_ehdrp->e_machine = EM_386;
1598 break;
1599 case bfd_arch_m68k:
1600 i_ehdrp->e_machine = EM_68K;
1601 break;
1602 case bfd_arch_m88k:
1603 i_ehdrp->e_machine = EM_88K;
1604 break;
1605 case bfd_arch_i860:
1606 i_ehdrp->e_machine = EM_860;
1607 break;
1608 case bfd_arch_mips: /* MIPS Rxxxx */
1609 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
1610 break;
1611 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
1612 default:
1613 i_ehdrp->e_machine = EM_NONE;
1614 }
1615 i_ehdrp->e_version = EV_CURRENT;
1616 i_ehdrp->e_ehsize = sizeof(Elf_External_Ehdr);
1617
1618 /* no program header, for now. */
1619 i_ehdrp->e_phoff = 0;
1620 i_ehdrp->e_phentsize = 0;
1621 i_ehdrp->e_phnum = 0;
37ac3b76 1622
80bdcb77
JG
1623 /* each bfd section is section header entry */
1624 i_ehdrp->e_entry = bfd_get_start_address (abfd);
1625 i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
37ac3b76 1626
80bdcb77
JG
1627 /* figure at most each section can have a rel, strtab, symtab */
1628 maxsections = 4*bfd_count_sections(abfd)+2;
37ac3b76 1629
80bdcb77 1630 i_ehdrp->e_shoff = i_ehdrp->e_ehsize;
37ac3b76 1631
80bdcb77
JG
1632 /* and we'll just have to fix up the offsets later. */
1633 /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */
1634
1635 i_shdrp = (Elf_Internal_Shdr *)
1636 bfd_alloc (abfd, sizeof (*i_shdrp) * maxsections);
1637 if (! i_shdrp)
1638 {
1639 bfd_error = no_memory;
1640 return (false);
1641 }
1642 for (count=0; count < maxsections; count++)
1643 {
1644 i_shdrp[count].rawdata = 0;
1645 i_shdrp[count].contents = 0;
1646 }
1647
1648
1649 i_shdrp[0].sh_name = 0;
1650 i_shdrp[0].sh_type = SHT_NULL;
1651 i_shdrp[0].sh_flags = 0;
1652 i_shdrp[0].sh_addr = 0;
1653 i_shdrp[0].sh_offset = 0;
1654 i_shdrp[0].sh_size = 0;
1655 i_shdrp[0].sh_link = SHN_UNDEF;
1656 i_shdrp[0].sh_info = 0;
1657 i_shdrp[0].sh_addralign = 0;
1658 i_shdrp[0].sh_entsize = 0;
1659
1660 i_ehdrp->e_shnum = 1;
1661
1662 elf_elfsections (abfd) = i_shdrp;
1663 elf_shstrtab (abfd) = shstrtab;
37ac3b76
ME
1664 }
1665 est.i_ehdr = elf_elfheader(abfd);
1666 est.i_shdrp = elf_elfsections(abfd);
1667 est.shstrtab = elf_shstrtab(abfd);
1668 est.symtab_section = 0; /* elf_fake_sections fils it in */
1669
1670 bfd_map_over_sections(abfd, elf_fake_sections, &est);
1671 elf_onesymtab (abfd) = est.symtab_section;
808dfd5a 1672 return (true);
9ce0058c
SC
1673}
1674
1675static boolean
1676DEFUN (elf_write_object_contents, (abfd), bfd *abfd)
1677{
37ac3b76
ME
1678 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
1679 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1680 Elf_External_Phdr *x_phdrp; /* Program header table, external form */
1681 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
1682 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
1683 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1684 asection *nsect;
1685 int maxsections;
1686 elf_sect_thunk est;
1687
1688 int outbase = 0;
1689 int count;
1690 struct strtab *shstrtab;
1691
1692 if(abfd->output_has_begun == false)
1693 elf_compute_section_file_positions(abfd);
1694
1695 i_ehdrp = elf_elfheader (abfd);
1696 i_shdrp = elf_elfsections (abfd);
1697 shstrtab = elf_shstrtab (abfd);
1698
1699 est.i_ehdr = i_ehdrp;
1700 est.i_shdrp = i_shdrp;
1701 est.shstrtab = shstrtab;
1702 est.symtab_section = elf_onesymtab (abfd); /* filled in by elf_fake */
1703
1704 bfd_map_over_sections(abfd, elf_make_sections, &est);
1705
1706 /* dump out the one symtab */
1707 {
1708 int symcount = bfd_get_symcount (abfd);
1709 asymbol ** syms = bfd_get_outsymbols (abfd);
1710 struct strtab * stt = bfd_new_strtab (abfd);
1711 Elf_Internal_Shdr *symtab_hdr;
1712 Elf_Internal_Shdr *symstrtab_hdr;
1713 int symstrtab_section;
1714 Elf_External_Sym *outbound_syms;
1715 int idx;
1716
1717 symtab_hdr = &i_shdrp[est.symtab_section];
1718 symtab_hdr->sh_type = SHT_SYMTAB;
1719 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
1720 symtab_hdr->sh_size = symtab_hdr->sh_entsize * symcount;
1721
1722 /* see assert in elf_fake_sections that supports this: */
1723 symstrtab_section = est.symtab_section+1;
1724 symstrtab_hdr = &i_shdrp[symstrtab_section];
1725 symtab_hdr->sh_link = symstrtab_section;
1726 symstrtab_hdr->sh_type = SHT_STRTAB;
1727
37ac3b76
ME
1728 outbound_syms = (Elf_External_Sym*)
1729 bfd_alloc(abfd, (1+symcount) * sizeof(Elf_External_Sym));
1730 /* now generate the data (for "contents") */
1731 for (idx = 0; idx < symcount; idx++)
1732 {
1733 Elf_Internal_Sym sym;
1734 sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
1735 sym.st_value = syms[idx]->value;
1736 sym.st_size = 0; /* we should recover this (FIXME) */
1737 if (syms[idx]->flags & BSF_WEAK)
1738 sym.st_info = ELF_ST_INFO(STB_WEAK, STT_OBJECT);
1739 else if (syms[idx]->flags & BSF_LOCAL)
1740 sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_OBJECT);
1741 else if (syms[idx]->flags & BSF_GLOBAL)
1742 sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
3dfa6cfb
FF
1743 else if (syms[idx]->flags & BSF_SECTION_SYM)
1744 sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
1745 else if (syms[idx]->flags & BSF_FILE)
1746 sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
37ac3b76
ME
1747
1748 sym.st_other = 0;
1749 if (syms[idx]->section)
1750 sym.st_shndx =
1751 elf_section_from_bfd_section(abfd,
1752 syms[idx]->section->output_section);
1753 else
1754 sym.st_shndx = SHN_UNDEF;
1755
1756 elf_swap_symbol_out (abfd, &sym, outbound_syms+idx+1);
1757 }
1758 {
1759 /* fill in 0th symbol */
1760 Elf_Internal_Sym sym;
1761 sym.st_name = 0;
1762 sym.st_value = 0;
1763 sym.st_size = 0;
1764 sym.st_info = 0;
1765 sym.st_other = 0;
1766 sym.st_shndx = SHN_UNDEF;
1767 elf_swap_symbol_out (abfd, &sym, outbound_syms);
1768 }
1769 symtab_hdr->contents = (void*)outbound_syms;
1770 symstrtab_hdr->contents = (void*)stt->tab;
1771 symstrtab_hdr->sh_size = stt->length;
1772 }
1773
1774 /* put the strtab out too... */
1775 {
1776 Elf_Internal_Shdr *this_hdr;
1777 int this_section;
1778
1779 this_section = i_ehdrp->e_shnum++;
1780 i_ehdrp->e_shstrndx = this_section;
1781 this_hdr = &i_shdrp[this_section];
1782 this_hdr->sh_name = bfd_add_to_strtab (abfd, shstrtab, ".shstrtab");
1783 this_hdr->sh_size = shstrtab->length;
1784 this_hdr->contents = (void*)shstrtab->tab;
1785 }
1786
1787 outbase = i_ehdrp->e_ehsize;
1788
1789 /* swap the header before spitting it out... */
1790 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1791 bfd_seek (abfd, 0L, SEEK_SET);
1792 bfd_write ((PTR) &x_ehdr, sizeof(x_ehdr), 1, abfd);
1793
1794 outbase += i_ehdrp->e_shentsize * i_ehdrp->e_shnum;
1795
1796 /* now we fix up the offsets... */
1797 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1798 {
1799 i_shdrp[count].sh_offset = outbase;
1800 outbase += i_shdrp[count].sh_size;
1801 }
1802
1803 /* at this point we've concocted all the ELF sections... */
1804 x_shdrp = (Elf_External_Shdr *)
1805 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
1806 if (! x_shdrp)
1807 {
1808 bfd_error = no_memory;
808dfd5a 1809 return (false);
37ac3b76
ME
1810 }
1811
37ac3b76
ME
1812 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1813 {
1814 elf_swap_shdr_out (abfd, i_shdrp+count, x_shdrp+count);
1815 }
1816 bfd_write ((PTR) x_shdrp, sizeof(*x_shdrp), i_ehdrp->e_shnum, abfd);
1817 /* need to dump the string table too... */
1818
1819 /* after writing the headers, we need to write the sections too... */
1820 nsect = abfd->sections;
1821 for (count = 0; count < i_ehdrp->e_shnum; count ++)
1822 {
1823 if(i_shdrp[count].contents)
1824 {
37ac3b76
ME
1825 bfd_seek (abfd, i_shdrp[count].sh_offset, SEEK_SET);
1826 bfd_write (i_shdrp[count].contents, i_shdrp[count].sh_size, 1, abfd);
1827 }
1828 }
1829
1830 /* sample use of bfd:
1831 * bfd_seek (abfd, 0L, false);
1832 * bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd);
1833 * if (bfd_seek(abfd, scn_base, SEEK_SET) != 0)
1834 * return false;
1835 * old = bfd_tell(abfd);
1836 */
1837
1838 return true;
1839
9ce0058c
SC
1840}
1841
8c4a1ace
JG
1842/* Given an index of a section, retrieve a pointer to it. Note
1843 that for our purposes, sections are indexed by {1, 2, ...} with
1844 0 being an illegal index. */
1845
37ac3b76
ME
1846/* In the original, each ELF section went into exactly one BFD
1847 section. This doesn't really make sense, so we need a real mapping.
1848 The mapping has to hide in the Elf_Internal_Shdr since asection
1849 doesn't have anything like a tdata field... */
1850
8c4a1ace 1851static struct sec *
37ac3b76 1852DEFUN (section_from_elf_index, (abfd, index),
8c4a1ace
JG
1853 bfd *abfd AND
1854 int index)
1855{
37ac3b76
ME
1856 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
1857 Elf_Internal_Shdr *hdr = i_shdrp + index;
1858
1859 switch (hdr->sh_type)
8c4a1ace 1860 {
37ac3b76
ME
1861 /* ELF sections that map to BFD sections */
1862 case SHT_PROGBITS:
1863 case SHT_NOBITS:
1864 if (! hdr->rawdata)
1865 bfd_section_from_shdr (abfd, index);
1866 return (struct sec *)hdr->rawdata;
1867 break;
1868 default:
13ff1343 1869 return (struct sec *)&bfd_abs_section;
8c4a1ace 1870 }
37ac3b76
ME
1871}
1872
1873/* given a section, search the header to find them... */
1874static int
1875DEFUN (elf_section_from_bfd_section, (abfd, asect),
1876 bfd *abfd AND
1877 struct sec *asect)
1878{
1879 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
1880 int index;
1881 Elf_Internal_Shdr *hdr;
1882 int maxindex = elf_elfheader (abfd)->e_shnum;
1883
1884 for(index = 0; index < maxindex; index++) {
1885 hdr = &i_shdrp[index];
1886 switch (hdr->sh_type)
1887 {
1888 /* ELF sections that map to BFD sections */
1889 case SHT_PROGBITS:
1890 case SHT_NOBITS:
1891 if (hdr->rawdata)
1892 {
1893 if (((struct sec *)(hdr->rawdata)) == asect)
1894 return index;
1895 }
1896 break;
1897 default:
1898 break;
1899 }
1900 }
1901 return 0;
8c4a1ace
JG
1902}
1903
1904static boolean
3dfa6cfb 1905DEFUN (elf_slurp_symbol_table, (abfd, symptrs),
37ac3b76 1906 bfd *abfd AND
3dfa6cfb 1907 asymbol **symptrs) /* Buffer for generated bfd symbols */
8c4a1ace 1908{
3dfa6cfb
FF
1909 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
1910 Elf_Internal_Shdr *hdr = i_shdrp + elf_onesymtab (abfd);
8c4a1ace 1911 int symcount; /* Number of external ELF symbols */
3dfa6cfb 1912 int i;
8c4a1ace
JG
1913 char *strtab; /* Buffer for raw ELF string table section */
1914 asymbol *sym; /* Pointer to current bfd symbol */
1915 asymbol *symbase; /* Buffer for generated bfd symbols */
8c4a1ace 1916 Elf_Internal_Sym i_sym;
37ac3b76 1917 Elf_External_Sym *x_symp;
8c4a1ace 1918
37ac3b76 1919 /* this is only valid because there is only one symtab... */
91f781ff
FF
1920 /* FIXME: This is incorrect, there may also be a dynamic symbol
1921 table which is a subset of the full symbol table. We either need
1922 to be prepared to read both (and merge them) or ensure that we
1923 only read the full symbol table. Currently we only get called to
1924 read the full symbol table. -fnf */
8c4a1ace
JG
1925 if (bfd_get_outsymbols (abfd) != NULL)
1926 {
1927 return (true);
1928 }
1929
8c4a1ace
JG
1930 /* Read each raw ELF symbol, converting from external ELF form to
1931 internal ELF form, and then using the information to create a
1932 canonical bfd symbol table entry.
1933
37ac3b76 1934 Note that we allocate the initial bfd canonical symbol buffer
8c4a1ace 1935 based on a one-to-one mapping of the ELF symbols to canonical
3dfa6cfb
FF
1936 symbols. We actually use all the ELF symbols, so there will be no
1937 space left over at the end. When we have all the symbols, we
1938 build the caller's pointer vector. */
8c4a1ace 1939
37ac3b76 1940 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
8c4a1ace
JG
1941 {
1942 bfd_error = system_call_error;
1943 return (false);
1944 }
1945
37ac3b76 1946 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
3dfa6cfb
FF
1947 symbase = (asymbol *) bfd_zalloc (abfd, symcount * sizeof (asymbol));
1948 sym = symbase;
1949
1950 /* Temporarily allocate room for the raw ELF symbols. */
1951 x_symp = (Elf_External_Sym *) malloc (symcount * sizeof (Elf_External_Sym));
8c4a1ace 1952
37ac3b76
ME
1953 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
1954 != symcount * sizeof (Elf_External_Sym))
1955 {
3dfa6cfb 1956 free ((PTR)x_symp);
37ac3b76
ME
1957 bfd_error = system_call_error;
1958 return (false);
1959 }
3dfa6cfb
FF
1960 /* Skip first symbol, which is a null dummy. */
1961 for (i = 1; i < symcount; i++)
8c4a1ace 1962 {
3dfa6cfb
FF
1963 elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
1964 sym -> the_bfd = abfd;
37ac3b76 1965 if (i_sym.st_name > 0)
3dfa6cfb
FF
1966 sym -> name = elf_string_from_elf_section(abfd, hdr->sh_link,
1967 i_sym.st_name);
1968 else
1969 sym -> name = "unnamed"; /* perhaps should include the number? */
1970 sym -> value = i_sym.st_value;
80bdcb77 1971/* FIXME -- this is almost certainly bogus. It's from Pace Willisson's
3dfa6cfb
FF
1972hasty Solaris support, to pass the sizes of object files or functions
1973down into GDB via the back door, to circumvent some other kludge in
1974how Sun hacked stabs. -- gnu@cygnus.com */
1975 sym -> udata = (PTR)i_sym.st_size;
80bdcb77 1976/* FIXME -- end of bogosity. */
3dfa6cfb
FF
1977 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
1978 {
1979 sym -> section = section_from_elf_index (abfd, i_sym.st_shndx);
8c4a1ace 1980 }
3dfa6cfb 1981 else if (i_sym.st_shndx == SHN_ABS)
8c4a1ace 1982 {
3dfa6cfb
FF
1983 sym -> section = &bfd_abs_section;
1984 }
1985 else if (i_sym.st_shndx == SHN_COMMON)
1986 {
1987 sym -> section = &bfd_com_section;
1988 }
1989 else if (i_sym.st_shndx == SHN_UNDEF)
1990 {
1991 sym -> section = &bfd_und_section;
1992 }
13ff1343
BK
1993 else
1994 sym -> section = &bfd_abs_section;
3dfa6cfb
FF
1995
1996 switch (ELF_ST_BIND (i_sym.st_info))
1997 {
1998 case STB_LOCAL:
1999 sym -> flags |= BSF_LOCAL;
2000 break;
2001 case STB_GLOBAL:
2002 sym -> flags |= (BSF_GLOBAL | BSF_EXPORT);
2003 break;
2004 case STB_WEAK:
2005 sym -> flags |= BSF_WEAK;
2006 break;
2007 }
37ac3b76 2008
3dfa6cfb
FF
2009 switch (ELF_ST_TYPE (i_sym.st_info))
2010 {
2011 case STT_SECTION:
2012 sym->flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
2013 break;
2014 case STT_FILE:
2015 sym->flags |= BSF_FILE | BSF_DEBUGGING;
2016 break;
8c4a1ace 2017 }
3dfa6cfb 2018 sym++;
8c4a1ace
JG
2019 }
2020
3dfa6cfb
FF
2021 /* We rely on the zalloc to clear out the final symbol entry. */
2022
2023 /* We're now done with the raw symbols. */
2024 free ((PTR)x_symp);
8c4a1ace 2025
3dfa6cfb
FF
2026 bfd_get_symcount(abfd) = symcount = sym - symbase;
2027
2028 /* Fill in the user's symbol pointer vector if needed. */
2029 if (symptrs)
8c4a1ace 2030 {
3dfa6cfb
FF
2031 sym = symbase;
2032 while (symcount-- > 0)
2033 {
2034 *symptrs++ = sym++;
2035 }
2036 *symptrs = 0; /* Final null pointer */
8c4a1ace
JG
2037 }
2038
2039 return (true);
2040}
2041
2042/* Return the number of bytes required to hold the symtab vector.
2043
2044 Note that we base it on the count plus 1, since we will null terminate
3dfa6cfb
FF
2045 the vector allocated based on this size. However, the ELF symbol table
2046 always has a dummy entry as symbol #0, so it ends up even. */
8c4a1ace 2047
9ce0058c 2048static unsigned int
8c4a1ace 2049DEFUN (elf_get_symtab_upper_bound, (abfd), bfd *abfd)
9ce0058c 2050{
3dfa6cfb
FF
2051 unsigned int symcount;
2052 unsigned int symtab_size;
2053 Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd);
2054 Elf_Internal_Shdr *hdr = i_shdrp + elf_onesymtab (abfd);
8c4a1ace 2055
3dfa6cfb
FF
2056 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2057 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol));
8c4a1ace 2058 return (symtab_size);
9ce0058c
SC
2059}
2060
37ac3b76
ME
2061/*
2062 This function return the number of bytes required to store the
2063 relocation information associated with section <<sect>>
2064 attached to bfd <<abfd>>
2065
2066*/
9ce0058c
SC
2067static unsigned int
2068elf_get_reloc_upper_bound (abfd, asect)
2069bfd *abfd;
2070sec_ptr asect;
2071{
37ac3b76
ME
2072 if (asect->flags & SEC_RELOC)
2073 {
2074 /* either rel or rela */
2075 return asect->_raw_size;
2076 }
2077 else
2078 return (0);
2079}
2080
2081/* FIXME!!! sparc howto should go into elf-32-sparc.c */
2082#ifdef sparc
2083enum reloc_type
2084 {
2085 R_SPARC_NONE = 0,
2086 R_SPARC_8, R_SPARC_16, R_SPARC_32,
2087 R_SPARC_DISP8, R_SPARC_DISP16, R_SPARC_DISP32,
2088 R_SPARC_WDISP30, R_SPARC_WDISP22,
2089 R_SPARC_HI22, R_SPARC_22,
2090 R_SPARC_13, R_SPARC_LO10,
2091 R_SPARC_GOT10, R_SPARC_GOT13, R_SPARC_GOT22,
2092 R_SPARC_PC10, R_SPARC_PC22,
2093 R_SPARC_WPLT30,
2094 R_SPARC_COPY,
2095 R_SPARC_GLOB_DAT, R_SPARC_JMP_SLOT,
2096 R_SPARC_RELATIVE,
2097 R_SPARC_UA32,
2098 };
2099
2100#define RELOC_TYPE_NAMES \
2101 "R_SPARC_NONE", \
2102 "R_SPARC_8", "R_SPARC_16", "R_SPARC_32", \
2103 "R_SPARC_DISP8", "R_SPARC_DISP16", "R_SPARC_DISP32", \
2104 "R_SPARC_WDISP30", "R_SPARC_WDISP22", \
2105 "R_SPARC_HI22", "R_SPARC_22", \
2106 "R_SPARC_13", "R_SPARC_LO10", \
2107 "R_SPARC_GOT10", "R_SPARC_GOT13", "R_SPARC_GOT22", \
2108 "R_SPARC_PC10", "R_SPARC_PC22", \
2109 "R_SPARC_WPLT30", \
2110 "R_SPARC_COPY", \
2111 "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT", \
2112 "R_SPARC_RELATIVE", \
2113 "R_SPARC_UA32"
2114
2115static reloc_howto_type elf_howto_table[] =
2116{
2117 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,false,false, 0,"R_SPARC_NONE", false,0,0x00000000,false),
2118 HOWTO(R_SPARC_8, 0,0, 8,false,0,true, true, 0,"R_SPARC_8", false,0,0x000000ff,false),
2119 HOWTO(R_SPARC_16, 0,1,16,false,0,true, true, 0,"R_SPARC_16", false,0,0x0000ffff,false),
2120 HOWTO(R_SPARC_32, 0,2,32,false,0,true, true, 0,"R_SPARC_32", false,0,0xffffffff,false),
2121 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,false, true, 0,"R_SPARC_DISP8", false,0,0x000000ff,false),
2122 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,false, true, 0,"R_SPARC_DISP16", false,0,0x0000ffff,false),
2123 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,false, true, 0,"R_SPARC_DISP32", false,0,0x00ffffff,false),
2124 HOWTO(R_SPARC_WDISP30,2,2,30,true, 0,false, true, 0,"R_SPARC_WDISP30",false,0,0x3fffffff,false),
2125 HOWTO(R_SPARC_WDISP22,2,2,22,true, 0,false, true, 0,"R_SPARC_WDISP22",false,0,0x003fffff,false),
2126 HOWTO(R_SPARC_HI22, 10,2,22,false,0,true, false, 0,"R_SPARC_HI22", false,0,0x003fffff,false),
2127 HOWTO(R_SPARC_22, 0,2,22,false,0,true, true, 0,"R_SPARC_22", false,0,0x003fffff,false),
2128 HOWTO(R_SPARC_13, 0,1,13,false,0,true, true, 0,"R_SPARC_13", false,0,0x00001fff,false),
2129 HOWTO(R_SPARC_LO10, 0,1,10,false,0,true, false, 0,"R_SPARC_LO10", false,0,0x000003ff,false),
2130 HOWTO(R_SPARC_GOT10, 0,1,10,false,0,false, true, 0,"R_SPARC_GOT10", false,0,0x000003ff,false),
2131 HOWTO(R_SPARC_GOT13, 0,1,13,false,0,false, true, 0,"R_SPARC_GOT13", false,0,0x00001fff,false),
2132 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,false, true, 0,"R_SPARC_GOT22", false,0,0x003fffff,false),
2133 HOWTO(R_SPARC_PC10, 0,1,10,false,0,true, true, 0,"R_SPARC_PC10", false,0,0x000003ff,false),
2134 HOWTO(R_SPARC_PC22, 0,2,22,false,0,true, true, 0,"R_SPARC_PC22", false,0,0x003fffff,false),
2135 HOWTO(R_SPARC_WPLT30, 0,0,00,false,0,false,false, 0,"R_SPARC_WPLT30", false,0,0x00000000,false),
2136 HOWTO(R_SPARC_COPY, 0,0,00,false,0,false,false, 0,"R_SPARC_COPY", false,0,0x00000000,false),
2137 HOWTO(R_SPARC_GLOB_DAT,0,0,00,false,0,false,false,0,"R_SPARC_GLOB_DAT",false,0,0x00000000,false),
2138 HOWTO(R_SPARC_JMP_SLOT,0,0,00,false,0,false,false,0,"R_SPARC_JMP_SLOT",false,0,0x00000000,false),
2139 HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,false,false,0,"R_SPARC_RELATIVE",false,0,0x00000000,false),
2140 HOWTO(R_SPARC_UA32, 0,0,00,false,0,false,false,0,"R_SPARC_UA32", false,0,0x00000000,false),
2141};
2142#endif
2143
2144static void
2145DEFUN(elf_info_to_howto, (abfd, cache_ptr, dst),
2146 bfd *abfd AND
2147 arelent *cache_ptr AND
2148 Elf_Internal_Rela *dst)
2149{
2150 /* FIXME!!! just doing sparc for now... */
2151#ifdef sparc
2152 BFD_ASSERT (ELF_R_TYPE(dst->r_info) < 24);
2153
2154 cache_ptr->howto = &elf_howto_table[ELF_R_TYPE(dst->r_info)];
2155#else
2156 fprintf (stderr, "elf_info_to_howto not implemented\n");
9ce0058c 2157 abort ();
37ac3b76
ME
2158#endif
2159}
2160
2161static boolean
2162DEFUN(elf_slurp_reloca_table,(abfd, asect, symbols),
2163 bfd *abfd AND
2164 sec_ptr asect AND
2165 asymbol **symbols)
2166{
2167 Elf_External_Rela *native_relocs;
2168 arelent *reloc_cache;
2169 arelent *cache_ptr;
2170
2171 unsigned int idx;
2172
2173 if (asect->relocation)
2174 return true;
2175 if (asect->reloc_count == 0)
2176 return true;
2177 if (asect->flags & SEC_CONSTRUCTOR)
2178 return true;
37ac3b76
ME
2179
2180 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
2181 native_relocs = (Elf_External_Rela *)
2182 bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela));
37ac3b76
ME
2183 bfd_read ((PTR) native_relocs,
2184 sizeof(Elf_External_Rela), asect->reloc_count, abfd);
2185
2186 reloc_cache = (arelent *)
2187 bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent)));
2188
2189 if (! reloc_cache) {
2190 bfd_error = no_memory;
2191 return false;
2192 }
2193
2194 for (idx = 0; idx < asect->reloc_count; idx ++)
2195 {
2196#ifdef RELOC_PROCESSING
2197 /* sparc, 68k, 88k, 860 use rela only. */
2198 /* 386 and we32000 use rel only... fix it for them later. */
2199 Elf_Internal_Rela dst;
2200 Elf_External_Rela *src;
2201
2202 cache_ptr = reloc_cache + idx;
2203 src = native_relocs + idx;
2204 elf_swap_reloca_in(abfd, src, &dst);
2205
2206 RELOC_PROCESSING(cache_ptr, &dst, symbols, abfd, asect);
2207#else
2208 Elf_Internal_Rela dst;
2209 asymbol *ptr;
2210 Elf_External_Rela *src;
2211
2212 cache_ptr = reloc_cache + idx;
2213 src = native_relocs + idx;
2214
2215 elf_swap_reloca_in(abfd, src, &dst);
2216
2217 if(asect->flags & SEC_RELOC)
2218 {
2219 /* relocatable, so the offset is off of the section */
2220 cache_ptr->address = dst.r_offset + asect->vma;
2221 }
2222 else
2223 {
2224 /* non-relocatable, so the offset a virtual address */
2225 cache_ptr->address = dst.r_offset;
2226 }
2227 /* ELF_R_SYM(dst.r_info) is the symbol table offset... */
2228 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM(dst.r_info);
2229 cache_ptr->addend = dst.r_addend;
2230 /* ptr = *(cache_ptr->sym_ptr_ptr); */
2231
2232 /* Fill in the cache_ptr->howto field from dst.r_type */
2233 elf_info_to_howto(abfd, cache_ptr, &dst);
2234#endif
2235 }
2236
2237 asect->relocation = reloc_cache;
2238 return true;
9ce0058c
SC
2239}
2240
37ac3b76 2241
9ce0058c
SC
2242static unsigned int
2243elf_canonicalize_reloc (abfd, section, relptr, symbols)
2244bfd *abfd;
2245sec_ptr section;
2246arelent **relptr;
2247asymbol **symbols;
2248{
37ac3b76
ME
2249 arelent *tblptr = section->relocation;
2250 unsigned int count = 0;
2251
2252 /* snarfed from coffcode.h */
2253 /* FIXME: this could be reloc... */
2254 elf_slurp_reloca_table(abfd, section, symbols);
2255
2256 tblptr = section->relocation;
2257 if (!tblptr)
2258 return 0;
2259
2260 for (; count++ < section->reloc_count;)
2261 *relptr++ = tblptr++;
2262
2263 *relptr = 0;
2264 return section->reloc_count;
9ce0058c
SC
2265}
2266
2267static unsigned int
8c4a1ace
JG
2268DEFUN (elf_get_symtab, (abfd, alocation),
2269 bfd *abfd AND
2270 asymbol **alocation)
9ce0058c 2271{
8c4a1ace
JG
2272 unsigned int symcount;
2273 asymbol **vec;
2274
3dfa6cfb 2275 if (!elf_slurp_symbol_table (abfd, alocation))
37ac3b76 2276 return (0);
3dfa6cfb
FF
2277 else
2278 return (bfd_get_symcount (abfd));
9ce0058c
SC
2279}
2280
2281static asymbol *
d01cd8fc
FF
2282DEFUN (elf_make_empty_symbol, (abfd),
2283 bfd *abfd)
9ce0058c 2284{
37ac3b76 2285 elf_symbol_type *newsym;
d01cd8fc 2286
37ac3b76
ME
2287 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
2288 if (! newsym)
d01cd8fc
FF
2289 {
2290 bfd_error = no_memory;
2291 return (NULL);
2292 }
2293 else
2294 {
37ac3b76
ME
2295 newsym -> symbol.the_bfd = abfd;
2296 return (&newsym -> symbol);
d01cd8fc 2297 }
9ce0058c
SC
2298}
2299
2300static void
2301DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how),
2302 bfd *ignore_abfd AND
2303 PTR filep AND
2304 asymbol *symbol AND
e0796d22 2305 bfd_print_symbol_type how)
9ce0058c 2306{
37ac3b76
ME
2307 FILE *file = (FILE *)filep;
2308 switch (how)
2309 {
2310 case bfd_print_symbol_name:
2311 fprintf(file, "%s", symbol->name);
2312 break;
2313 case bfd_print_symbol_more:
2314 fprintf(file, "elf %lx %lx",
2315 symbol->value,
2316 symbol->flags);
2317 break;
2318 case bfd_print_symbol_nm:
2319 case bfd_print_symbol_all:
2320 {
ec6b2951 2321 CONST char *section_name;
37ac3b76
ME
2322 section_name = symbol->section? symbol->section->name : "(*none*)";
2323 bfd_print_symbol_vandf((PTR) file, symbol);
3dfa6cfb 2324 fprintf(file, " %s\t%s",
37ac3b76 2325 section_name,
37ac3b76
ME
2326 symbol->name);
2327 }
2328 break;
2329 }
2330
9ce0058c
SC
2331}
2332
2333static alent *
2334DEFUN (elf_get_lineno,(ignore_abfd, symbol),
2335 bfd *ignore_abfd AND
2336 asymbol *symbol)
2337{
2338 fprintf (stderr, "elf_get_lineno unimplemented\n");
2339 fflush (stderr);
2340 abort ();
2341 return (NULL);
2342}
2343
2344static boolean
2345DEFUN (elf_set_arch_mach,(abfd, arch, machine),
2346 bfd *abfd AND
2347 enum bfd_architecture arch AND
2348 unsigned long machine)
2349{
9ce0058c 2350 /* Allow any architecture to be supported by the elf backend */
37ac3b76
ME
2351 switch(arch)
2352 {
2353 case bfd_arch_unknown: /* EM_NONE */
2354 case bfd_arch_sparc: /* EM_SPARC */
2355 case bfd_arch_i386: /* EM_386 */
2356 case bfd_arch_m68k: /* EM_68K */
2357 case bfd_arch_m88k: /* EM_88K */
2358 case bfd_arch_i860: /* EM_860 */
2359 case bfd_arch_mips: /* EM_MIPS (MIPS R3000) */
2360 return bfd_default_set_arch_mach(abfd, arch, machine);
2361 default:
2362 return false;
2363 }
9ce0058c
SC
2364}
2365
2366static boolean
2367DEFUN (elf_find_nearest_line,(abfd,
2368 section,
2369 symbols,
2370 offset,
2371 filename_ptr,
2372 functionname_ptr,
2373 line_ptr),
2374 bfd *abfd AND
2375 asection *section AND
2376 asymbol **symbols AND
2377 bfd_vma offset AND
2378 CONST char **filename_ptr AND
2379 CONST char **functionname_ptr AND
2380 unsigned int *line_ptr)
2381{
2382 fprintf (stderr, "elf_find_nearest_line unimplemented\n");
2383 fflush (stderr);
2384 abort ();
2385 return (false);
2386}
2387
2388static int
2389DEFUN (elf_sizeof_headers, (abfd, reloc),
2390 bfd *abfd AND
2391 boolean reloc)
2392{
2393 fprintf (stderr, "elf_sizeof_headers unimplemented\n");
2394 fflush (stderr);
2395 abort ();
2396 return (0);
2397}
37ac3b76
ME
2398
2399boolean
2400DEFUN(elf_set_section_contents, (abfd, section, location, offset, count),
2401 bfd *abfd AND
2402 sec_ptr section AND
2403 PTR location AND
2404 file_ptr offset AND
2405 bfd_size_type count)
2406{
2407 int dest_sect;
2408 void *contents;
2409 if (abfd->output_has_begun == false) /* set by bfd.c handler? */
2410 {
2411 /* do setup calculations (FIXME) */
2412 elf_compute_section_file_positions(abfd);
2413 }
2414#if 0
2415 if(bfd_seek (abfd, (file_ptr)section->filepos + offset, SEEK_SET) == -1)
2416 return false;
2417 if(bfd_write (location, (bfd_size_type)1, count, abfd) != count)
2418 return false;
2419#endif
2420 /* we really just need to save the contents away... */
2421 dest_sect = elf_section_from_bfd_section(abfd, section);
2422 if(!dest_sect)
2423 return false;
2424
2425 /* FIXME: allocate in set_section_size, then copy in here... */
2426 contents = (void*)bfd_alloc(abfd, count);
2427 BFD_ASSERT(contents);
2428 memcpy(contents, location, count);
2429 elf_elfsections (abfd)[dest_sect].contents = contents;
2430
2431 return true;
2432}
2433
e0796d22 2434\f
9ce0058c
SC
2435/* This structure contains everything that BFD knows about a target.
2436 It includes things like its byte order, name, what routines to call
2437 to do various operations, etc. Every BFD points to a target structure
2438 with its "xvec" member.
2439
2440 There are two such structures here: one for big-endian machines and
2441 one for little-endian machines. */
2442
e0796d22
FF
2443/* Archives are generic or unimplemented. */
2444#define elf_slurp_armap bfd_false
2445#define elf_slurp_extended_name_table _bfd_slurp_extended_name_table
2446#define elf_truncate_arname bfd_dont_truncate_arname
2447#define elf_openr_next_archived_file bfd_generic_openr_next_archived_file
2448#define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt
2449#define elf_write_armap (PROTO (boolean, (*), \
a6c1d731 2450 (bfd *arch, unsigned int elength, struct orl *map, unsigned int orl_count, \
e0796d22
FF
2451 int stridx))) bfd_false
2452
2453/* Ordinary section reading and writing */
2454#define elf_new_section_hook _bfd_dummy_new_section_hook
37ac3b76
ME
2455#define elf_get_section_contents bfd_generic_get_section_contents
2456/* #define elf_set_section_contents bfd_generic_set_section_contents */
e0796d22
FF
2457#define elf_close_and_cleanup bfd_generic_close_and_cleanup
2458
2459#define elf_bfd_debug_info_start bfd_void
2460#define elf_bfd_debug_info_end bfd_void
2461#define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
e98e6ec1
SC
2462#define elf_bfd_get_relocated_section_contents \
2463 bfd_generic_get_relocated_section_contents
d01cd8fc 2464#define elf_bfd_relax_section bfd_generic_relax_section
9ce0058c
SC
2465bfd_target elf_big_vec =
2466{
2467 /* name: identify kind of target */
2468 "elf-big",
2469
2470 /* flavour: general indication about file */
e0796d22 2471 bfd_target_elf_flavour,
9ce0058c
SC
2472
2473 /* byteorder_big_p: data is big endian */
2474 true,
2475
2476 /* header_byteorder_big_p: header is also big endian */
2477 true,
2478
2479 /* object_flags: mask of all file flags */
2480 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
2481 DYNAMIC | WP_TEXT),
2482
2483 /* section_flags: mask of all section flags */
2484 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
37ac3b76 2485 SEC_CODE | SEC_DATA),
9ce0058c
SC
2486
2487 /* ar_pad_char: pad character for filenames within an archive header
2488 FIXME: this really has nothing to do with ELF, this is a characteristic
2489 of the archiver and/or os and should be independently tunable */
2490 '/',
2491
2492 /* ar_max_namelen: maximum number of characters in an archive header
2493 FIXME: this really has nothing to do with ELF, this is a characteristic
2494 of the archiver and should be independently tunable. This value is
2495 a WAG (wild a** guess) */
2496 15,
2497
2498 /* align_power_min: minimum alignment restriction for any section
2499 FIXME: this value may be target machine dependent */
2500 3,
2501
2502 /* Routines to byte-swap various sized integers from the data sections */
2503 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
2504
2505 /* Routines to byte-swap various sized integers from the file headers */
2506 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
2507
2508 /* bfd_check_format: check the format of a file being read */
e0796d22
FF
2509 { _bfd_dummy_target, /* unknown format */
2510 elf_object_p, /* assembler/linker output (object file) */
2511 bfd_generic_archive_p, /* an archive */
2512 elf_core_file_p /* a core file */
9ce0058c
SC
2513 },
2514
2515 /* bfd_set_format: set the format of a file being written */
2516 { bfd_false,
2517 elf_mkobject,
2518 _bfd_generic_mkarchive,
2519 bfd_false
2520 },
2521
2522 /* bfd_write_contents: write cached information into a file being written */
2523 { bfd_false,
2524 elf_write_object_contents,
2525 _bfd_write_archive_contents,
2526 bfd_false
2527 },
2528
2529 /* Initialize a jump table with the standard macro. All names start
2530 with "elf" */
2531 JUMP_TABLE(elf),
2532
2533 /* SWAP_TABLE */
2534 NULL, NULL, NULL
2535};
2536
2537bfd_target elf_little_vec =
2538{
2539 /* name: identify kind of target */
2540 "elf-little",
2541
2542 /* flavour: general indication about file */
e0796d22 2543 bfd_target_elf_flavour,
9ce0058c
SC
2544
2545 /* byteorder_big_p: data is big endian */
2546 false, /* Nope -- this one's little endian */
2547
2548 /* header_byteorder_big_p: header is also big endian */
2549 false, /* Nope -- this one's little endian */
2550
2551 /* object_flags: mask of all file flags */
2552 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
2553 DYNAMIC | WP_TEXT),
2554
2555 /* section_flags: mask of all section flags */
2556 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
2557 SEC_DATA),
2558
2559 /* ar_pad_char: pad character for filenames within an archive header
2560 FIXME: this really has nothing to do with ELF, this is a characteristic
2561 of the archiver and/or os and should be independently tunable */
2562 '/',
2563
2564 /* ar_max_namelen: maximum number of characters in an archive header
2565 FIXME: this really has nothing to do with ELF, this is a characteristic
2566 of the archiver and should be independently tunable. This value is
2567 a WAG (wild a** guess) */
2568 15,
2569
2570 /* align_power_min: minimum alignment restriction for any section
2571 FIXME: this value may be target machine dependent */
2572 3,
2573
2574 /* Routines to byte-swap various sized integers from the data sections */
2575 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
2576
2577 /* Routines to byte-swap various sized integers from the file headers */
2578 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
2579
2580 /* bfd_check_format: check the format of a file being read */
e0796d22
FF
2581 { _bfd_dummy_target, /* unknown format */
2582 elf_object_p, /* assembler/linker output (object file) */
2583 bfd_generic_archive_p, /* an archive */
2584 elf_core_file_p /* a core file */
9ce0058c
SC
2585 },
2586
2587 /* bfd_set_format: set the format of a file being written */
2588 { bfd_false,
2589 elf_mkobject,
2590 _bfd_generic_mkarchive,
2591 bfd_false
2592 },
2593
2594 /* bfd_write_contents: write cached information into a file being written */
2595 { bfd_false,
2596 elf_write_object_contents,
2597 _bfd_write_archive_contents,
2598 bfd_false
2599 },
2600
2601 /* Initialize a jump table with the standard macro. All names start
2602 with "elf" */
2603 JUMP_TABLE(elf),
2604
2605 /* SWAP_TABLE */
2606 NULL, NULL, NULL
2607};
This page took 0.150652 seconds and 4 git commands to generate.