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