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