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