Now full of documentation. Yum Yum.
[platform/upstream/binutils.git] / bfd / aoutf1.h
1 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
2
3 This file is part of BFD, the Binary File Diddler.
4
5 BFD is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
8 any later version.
9
10 BFD is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with BFD; see the file COPYING.  If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
18
19
20 #include <ansidecl.h>
21 #include <sysdep.h>
22 struct external_exec;
23 #include <a.out.sun4.h>
24 #include "bfd.h"
25 #include "libaout.h"           
26 #include "libbfd.h"
27
28
29
30 #include "aout64.h"
31 #include "stab.gnu.h"
32 #include "ar.h"
33
34
35 /*
36 inheritd two core files and various implimentation files. 
37 The file @code{aoutf1.h} contains the code for BFD's
38 a.out back end. Control over the generated back end is given by these
39 three preprocessor names:
40 @table @code
41 @item ARCH
42 This value should be either 32 or 64, depending upon the size of an
43 int in the target format. It changes the sizes of the structs which
44 perform the memory/disk mapping of structures.
45
46 The 64 bit backend may only be used if the host compiler supports 64
47 ints (eg with gcc), by defining the name @code{HOST_64_BIT}. With this
48 name defined, @emph{all} bfd operations are performed with 64bit
49 arithmetic, not just those to a 64bit target.
50
51 @item TARGETNAME
52 bit long longsIf bfd is being compiled with gcc, (or any other compiler which gives
53 64 bit long longs), 
54 @item
55 It is structured in such a way that @code{#define}ing
56 the size of the architecture into a  @code{#include}ing
57 it with different @code{#define}s present will alter the definitions
58 of various structures in include files and generate correct code for
59 th
60
61 */
62
63 void (*bfd_error_trap)();
64
65 static bfd_target *sunos4_callback ();
66
67 /*SUPPRESS558*/
68 /*SUPPRESS529*/
69
70 bfd_target *
71 DEFUN(NAME(sunos,object_p), (abfd),
72      bfd *abfd)
73 {
74   unsigned char magicbuf[4];    /* Raw bytes of magic number from file */
75   unsigned long magic;          /* Swapped magic number */
76
77   bfd_error = system_call_error;
78
79   if (bfd_read ((PTR)magicbuf, 1 , 4, abfd) !=
80       sizeof (magicbuf))
81     return 0;
82   magic = bfd_h_get_32 (abfd, magicbuf);
83
84   if (N_BADMAG (*((struct internal_exec *) &magic))) return 0;
85
86   return NAME(aout,some_aout_object_p) (abfd, sunos4_callback);
87 }
88
89   /* Determine the size of a relocation entry, based on the architecture */
90 static void
91 DEFUN(choose_reloc_size,(abfd),
92 bfd *abfd)
93   {
94     switch (abfd->obj_arch) {
95     case bfd_arch_sparc:
96     case bfd_arch_a29k:
97       obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
98       break;
99     default:
100       obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
101       break;
102     }
103   }
104
105 /* Set parameters about this a.out file that are machine-dependent.
106    This routine is called from some_aout_object_p just before it returns.  */
107
108 static bfd_target *
109 sunos4_callback (abfd)
110      bfd *abfd;
111 {
112   struct internal_exec *execp = exec_hdr (abfd);
113
114   WORK_OUT_FILE_POSITIONS(abfd, execp);  
115
116   /* Determine the architecture and machine type of the object file.  */
117   switch (N_MACHTYPE (*exec_hdr (abfd))) {
118     
119   case M_UNKNOWN:
120     abfd->obj_arch = bfd_arch_unknown;
121     abfd->obj_machine = 0;
122     break;
123     
124   case M_68010:
125     abfd->obj_arch = bfd_arch_m68k;
126     abfd->obj_machine = 68010;
127     break;
128     
129   case M_68020:
130     abfd->obj_arch = bfd_arch_m68k;
131     abfd->obj_machine = 68020;
132     break;
133     
134   case M_SPARC:
135     abfd->obj_arch = bfd_arch_sparc;
136     abfd->obj_machine = 0;
137     break;
138     
139   case M_386:
140     abfd->obj_arch = bfd_arch_i386;
141     abfd->obj_machine = 0;
142     break;
143     
144   case M_29K:
145     abfd->obj_arch = bfd_arch_a29k;
146     abfd->obj_machine = 0;
147     break;
148     
149   default:
150     abfd->obj_arch = bfd_arch_obscure;
151     abfd->obj_machine = 0;
152     break;
153   }
154   
155   choose_reloc_size(abfd);
156   return abfd->xvec;
157 }
158
159
160 /* Write an object file in SunOS format.
161 Section contents have already been written.  We write the
162 file header, symbols, and relocation.  */
163
164 boolean
165 DEFUN(NAME(aout,sunos4_write_object_contents),(abfd),
166       bfd *abfd)
167
168   {
169     bfd_size_type data_pad = 0;
170     struct external_exec exec_bytes;
171     struct internal_exec *execp = exec_hdr (abfd);
172     
173     
174     
175     execp->a_text = obj_textsec (abfd)->size;
176     
177     /* Magic number, maestro, please!  */
178     switch (bfd_get_architecture(abfd)) {
179     case bfd_arch_m68k:
180       switch (bfd_get_machine(abfd)) {
181       case 68010:
182         N_SET_MACHTYPE(*execp, M_68010);
183         break;
184       default:
185       case 68020:
186         N_SET_MACHTYPE(*execp, M_68020);
187         break;
188       }
189       break;
190     case bfd_arch_sparc:
191       N_SET_MACHTYPE(*execp, M_SPARC);
192       break;
193     case bfd_arch_i386:
194       N_SET_MACHTYPE(*execp, M_386);
195       break;
196     case bfd_arch_a29k:
197       N_SET_MACHTYPE(*execp, M_29K);
198       break;
199     default:
200       N_SET_MACHTYPE(*execp, M_UNKNOWN);
201     }
202     
203     choose_reloc_size(abfd);
204
205     /* FIXME */
206     N_SET_FLAGS (*execp, 0x1);
207     
208     WRITE_HEADERS(abfd, execp);
209
210   return true;
211 }
212 \f
213 /* core files */
214
215 #define CORE_MAGIC 0x080456
216 #define CORE_NAMELEN 16
217
218 /* The core structure is taken from the Sun documentation.
219 Unfortunately, they don't document the FPA structure, or at least I
220 can't find it easily.  Fortunately the core header contains its own
221 length.  So this shouldn't cause problems, except for c_ucode, which
222 so far we don't use but is easy to find with a little arithmetic. */
223
224 /* But the reg structure can be gotten from the SPARC processor handbook.
225 This really should be in a GNU include file though so that gdb can use
226 the same info. */
227 struct regs {
228   int r_psr;
229   int r_pc;
230   int r_npc;
231   int r_y;
232   int r_g1;
233   int r_g2;
234   int r_g3;
235   int r_g4;
236   int r_g5;
237   int r_g6;
238   int r_g7;
239   int r_o0;
240   int r_o1;
241   int r_o2;
242   int r_o3;
243   int r_o4;
244   int r_o5;
245   int r_o6;
246   int r_o7;
247 };
248
249 /* Taken from Sun documentation: */
250
251 /* FIXME:  It's worse than we expect.  This struct contains TWO substructs
252 neither of whose size we know, WITH STUFF IN BETWEEN THEM!  We can't
253 even portably access the stuff in between!  */
254
255 struct core {
256   int c_magic;                  /* Corefile magic number */
257   int c_len;                    /* Sizeof (struct core) */
258   struct regs c_regs;           /* General purpose registers -- MACHDEP SIZE */
259   struct internal_exec c_aouthdr;       /* A.out header */
260   int c_signo;                  /* Killing signal, if any */
261   int c_tsize;                  /* Text size (bytes) */
262   int c_dsize;                  /* Data size (bytes) */
263   int c_ssize;                  /* Stack size (bytes) */
264   char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
265   double fp_stuff[1];               /* external FPU state (size unknown by us) */
266   /* The type "double" is critical here, for alignment.
267     SunOS declares a struct here, but the struct's alignment
268       is double since it contains doubles.  */
269   int c_ucode;                  /* Exception no. from u_code */
270   /* (this member is not accessible by name since we don't
271     portably know the size of fp_stuff.) */
272 };
273
274 /* Supposedly the user stack grows downward from the bottom of kernel memory.
275 Presuming that this remains true, this definition will work. */
276 #define USRSTACK (-(128*1024*1024))
277
278 PROTO (static void, swapcore, (bfd *abfd, struct core *core));
279
280 /* need this cast b/c ptr is really void * */
281 #define core_hdr(bfd) (((struct suncordata *) (bfd->tdata))->hdr)
282 #define core_datasec(bfd) (((struct suncordata *) ((bfd)->tdata))->data_section)
283 #define core_stacksec(bfd) (((struct suncordata*)((bfd)->tdata))->stack_section)
284 #define core_regsec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg_section)
285 #define core_reg2sec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg2_section)
286
287 /* These are stored in the bfd's tdata */
288 struct suncordata {
289 struct core *hdr;             /* core file header */
290 asection *data_section;
291 asection *stack_section;
292 asection *reg_section;
293 asection *reg2_section;
294 };
295
296 static bfd_target *
297 DEFUN(sunos4_core_file_p,(abfd),
298       bfd *abfd)
299 {
300   unsigned char longbuf[4];     /* Raw bytes of various header fields */
301   int core_size;
302   int core_mag;
303   struct core *core;
304   char *rawptr;
305   
306   bfd_error = system_call_error;
307   
308   if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
309       sizeof (longbuf))
310     return 0;
311   core_mag = bfd_h_get_32 (abfd, longbuf);
312
313   if (core_mag != CORE_MAGIC) return 0;
314
315   /* SunOS core headers can vary in length; second word is size; */
316   if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
317       sizeof (longbuf))
318     return 0;
319   core_size = bfd_h_get_32 (abfd, longbuf);
320   /* Sanity check */
321   if (core_size > 20000)
322     return 0;
323
324   if (bfd_seek (abfd, 0L, false) < 0) return 0;
325
326   rawptr = bfd_zalloc (abfd, core_size + sizeof (struct suncordata));
327   if (rawptr == NULL) {
328     bfd_error = no_memory;
329     return 0;
330   }
331
332   core = (struct core *) (rawptr + sizeof (struct suncordata));
333
334   if ((bfd_read ((PTR) core, 1, core_size, abfd)) != core_size) {
335     bfd_error = system_call_error;
336     bfd_release (abfd, rawptr);
337     return 0;
338   }
339
340   swapcore (abfd, core);
341   set_tdata (abfd, ((struct suncordata *) rawptr));
342   core_hdr (abfd) = core;
343
344   /* create the sections.  This is raunchy, but bfd_close wants to reclaim
345      them */
346   core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
347   if (core_stacksec (abfd) == NULL) {
348   loser:
349     bfd_error = no_memory;
350     bfd_release (abfd, rawptr);
351     return 0;
352   }
353   core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
354   if (core_datasec (abfd) == NULL) {
355   loser1:
356     bfd_release (abfd, core_stacksec (abfd));
357     goto loser;
358   }
359   core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
360   if (core_regsec (abfd) == NULL) {
361   loser2:
362     bfd_release (abfd, core_datasec (abfd));
363     goto loser1;
364   }
365   core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
366   if (core_reg2sec (abfd) == NULL) {
367     bfd_release (abfd, core_regsec (abfd));
368     goto loser2;
369   }
370
371   core_stacksec (abfd)->name = ".stack";
372   core_datasec (abfd)->name = ".data";
373   core_regsec (abfd)->name = ".reg";
374   core_reg2sec (abfd)->name = ".reg2";
375
376   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
377   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
378   core_regsec (abfd)->flags = SEC_ALLOC;
379   core_reg2sec (abfd)->flags = SEC_ALLOC;
380
381   core_stacksec (abfd)->size = core->c_ssize;
382   core_datasec (abfd)->size = core->c_dsize;
383   core_regsec (abfd)->size = (sizeof core->c_regs);
384   /* Float regs take up end of struct, except c_ucode.  */
385   core_reg2sec (abfd)->size = core_size - (sizeof core->c_ucode) -
386     (file_ptr)(((struct core *)0)->fp_stuff);
387
388   core_stacksec (abfd)->vma = (USRSTACK - core->c_ssize);
389   core_datasec (abfd)->vma = N_DATADDR(core->c_aouthdr);
390   core_regsec (abfd)->vma = -1;
391   core_reg2sec (abfd)->vma = -1;
392
393   core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
394   core_datasec (abfd)->filepos = core->c_len;
395   /* In file header: */
396   core_regsec (abfd)->filepos = (file_ptr)(&((struct core *)0)->c_regs);
397   core_reg2sec (abfd)->filepos = (file_ptr)(((struct core *)0)->fp_stuff);
398
399   /* Align to word at least */
400   core_stacksec (abfd)->alignment_power = 2;
401   core_datasec (abfd)->alignment_power = 2;
402   core_regsec (abfd)->alignment_power = 2;
403   core_reg2sec (abfd)->alignment_power = 2;
404
405   abfd->sections = core_stacksec (abfd);
406   core_stacksec (abfd)->next = core_datasec (abfd);
407   core_datasec (abfd)->next = core_regsec (abfd);
408   core_regsec (abfd)->next = core_reg2sec (abfd);
409
410   abfd->section_count = 4;
411
412   return abfd->xvec;
413 }
414
415 static char *sunos4_core_file_failing_command (abfd)
416 bfd *abfd;
417   {
418   return core_hdr (abfd)->c_cmdname;
419 }
420
421 static int
422 DEFUN(sunos4_core_file_failing_signal,(abfd),
423       bfd *abfd)
424 {
425   return core_hdr (abfd)->c_signo;
426 }
427
428 static boolean
429 DEFUN(sunos4_core_file_matches_executable_p, (core_bfd, exec_bfd),
430       bfd *core_bfd AND
431       bfd *exec_bfd)
432 {
433   if (core_bfd->xvec != exec_bfd->xvec) {
434     bfd_error = system_call_error;
435     return false;
436   }
437
438   return (bcmp ((char *)&core_hdr (core_bfd), (char*) &exec_hdr (exec_bfd),
439                 sizeof (struct internal_exec)) == 0) ? true : false;
440 }
441
442 /* byte-swap core structure */
443 /* FIXME, this needs more work to swap IN a core struct from raw bytes */
444 static void
445 DEFUN(swapcore,(abfd, core),
446       bfd *abfd AND
447       struct core *core)
448 {
449   struct external_exec exec_bytes;
450   
451   core->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&core->c_magic);
452   core->c_len   = bfd_h_get_32 (abfd, (unsigned char *)&core->c_len  );
453   /* Leave integer registers in target byte order.  */
454   bcopy ((char *)&(core->c_aouthdr), (char *)&exec_bytes, EXEC_BYTES_SIZE);
455   NAME(aout,swap_exec_header_in)(abfd, &exec_bytes, &core->c_aouthdr);
456   core->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&core->c_signo);
457   core->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_tsize);
458   core->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_dsize);
459   core->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_ssize);
460   /* Leave FP registers in target byte order.  */
461   /* Leave "c_ucode" unswapped for now, since we can't find it easily.  */
462 }
463 \f
464 /* We use BFD generic archive files.  */
465 #define aout_32_openr_next_archived_file        bfd_generic_openr_next_archived_file
466 #define aout_32_generic_stat_arch_elt           bfd_generic_stat_arch_elt
467 #define aout_32_slurp_armap                     bfd_slurp_bsd_armap
468 #define aout_32_slurp_extended_name_table       bfd_true
469 #define aout_32_write_armap                     bsd_write_armap
470 #define aout_32_truncate_arname                 bfd_bsd_truncate_arname
471 #define aout_32_machine_type                    sunos_machine_type
472
473 #define aout_32_core_file_failing_command       sunos4_core_file_failing_command
474 #define aout_32_core_file_failing_signal        sunos4_core_file_failing_signal
475 #define aout_32_core_file_matches_executable_p  sunos4_core_file_matches_executable_p
476
477
478 #define aout_64_openr_next_archived_file        bfd_generic_openr_next_archived_file
479 #define aout_64_generic_stat_arch_elt           bfd_generic_stat_arch_elt
480 #define aout_64_slurp_armap                     bfd_slurp_bsd_armap
481 #define aout_64_slurp_extended_name_table       bfd_true
482 #define aout_64_write_armap                     bsd_write_armap
483 #define aout_64_truncate_arname                 bfd_bsd_truncate_arname
484 #define aout_64_machine_type                    sunos_machine_type
485
486 #define aout_64_core_file_failing_command       sunos4_core_file_failing_command
487 #define aout_64_core_file_failing_signal        sunos4_core_file_failing_signal
488 #define aout_64_core_file_matches_executable_p  sunos4_core_file_matches_executable_p
489
490 #define aout_64_bfd_debug_info_start            bfd_void
491 #define aout_64_bfd_debug_info_end              bfd_void
492 #define aout_64_bfd_debug_info_accumulate       bfd_void
493
494 #define aout_32_bfd_debug_info_start            bfd_void
495 #define aout_32_bfd_debug_info_end              bfd_void
496 #define aout_32_bfd_debug_info_accumulate       bfd_void
497
498
499
500 /* We implement these routines ourselves, rather than using the generic
501 a.out versions.  */
502 #define aout_write_object_contents      sunos4_write_object_contents
503
504 bfd_target VECNAME =
505   {
506     TARGETNAME,
507     bfd_target_aout_flavour_enum,
508     true,                       /* target byte order */
509     true,                       /* target headers byte order */
510     (HAS_RELOC | EXEC_P |       /* object flags */
511      HAS_LINENO | HAS_DEBUG |
512      HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
513     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
514     ' ',                                                   /* ar_pad_char */
515     16,                                                    /* ar_max_namelen */
516     3,                                                     /* minimum alignment power */
517     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
518     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
519     
520       {_bfd_dummy_target, NAME(sunos,object_p),
521        bfd_generic_archive_p, sunos4_core_file_p},
522       {bfd_false, NAME(aout,mkobject),
523        _bfd_generic_mkarchive, bfd_false},
524       {bfd_false, NAME(aout,sunos4_write_object_contents), /* bfd_write_contents */
525        _bfd_write_archive_contents, bfd_false},
526     
527     JUMP_TABLE(JNAME(aout))
528     };