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