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