z8kgen: temp file to generate z8k-opc.h from
[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 "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24
25 #include "aout/sun4.h"
26 #include "libaout.h"           /* BFD a.out internal data structures */
27
28 #include "aout/aout64.h"
29 #include "aout/stab_gnu.h"
30 #include "aout/ar.h"
31
32 /*
33 The file @code{aoutf1.h} contains the code for BFD's
34 a.out back end. Control over the generated back end is given by these
35 two preprocessor names:
36 @table @code
37 @item ARCH_SIZE
38 This value should be either 32 or 64, depending upon the size of an
39 int in the target format. It changes the sizes of the structs which
40 perform the memory/disk mapping of structures.
41
42 The 64 bit backend may only be used if the host compiler supports 64
43 ints (eg long long with gcc), by defining the name @code{HOST_64_BIT} in @code{bfd.h}.
44 With this name defined, @emph{all} bfd operations are performed with 64bit
45 arithmetic, not just those to a 64bit target.
46
47 @item TARGETNAME
48 The name put into the target vector.
49 @item
50 @end table
51
52 */
53
54 void (*bfd_error_trap)();
55
56 static bfd_target *sunos4_callback ();
57
58 /*SUPPRESS558*/
59 /*SUPPRESS529*/
60
61 bfd_target *
62 DEFUN(NAME(sunos,object_p), (abfd),
63      bfd *abfd)
64 {
65   struct external_exec exec_bytes;      /* Raw exec header from file */
66   struct internal_exec exec;            /* Cleaned-up exec header */
67
68   if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)
69       != EXEC_BYTES_SIZE) {
70     bfd_error = wrong_format;
71     return 0;
72   }
73
74   exec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info);
75
76   if (N_BADMAG (exec)) return 0;
77
78   NAME(aout,swap_exec_header_in)(abfd, &exec_bytes, &exec);
79
80   return NAME(aout,some_aout_object_p) (abfd, &exec, sunos4_callback);
81 }
82
83   /* Determine the size of a relocation entry, based on the architecture */
84 static void
85 DEFUN(choose_reloc_size,(abfd),
86 bfd *abfd)
87 {
88   switch (bfd_get_arch(abfd)) {
89   case bfd_arch_sparc:
90   case bfd_arch_a29k:
91     obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
92     break;
93   default:
94     obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
95     break;
96   }
97 }
98
99 /* Set parameters about this a.out file that are machine-dependent.
100    This routine is called from some_aout_object_p just before it returns.  */
101
102 static bfd_target *
103 sunos4_callback (abfd)
104      bfd *abfd;
105 {
106   struct internal_exec *execp = exec_hdr (abfd);
107   enum bfd_architecture arch;
108   long machine;
109
110   WORK_OUT_FILE_POSITIONS(abfd, execp);  
111
112   /* Determine the architecture and machine type of the object file.  */
113   switch (N_MACHTYPE (*exec_hdr (abfd))) {
114
115   case M_UNKNOWN:
116       /* Some Sun3s make magic numbers without cpu types in them, so
117          we'll default to the 68020. */
118     arch = bfd_arch_m68k;
119     machine = 68020;
120     break;
121     
122   case M_68010:
123   case M_HP200:
124     arch = bfd_arch_m68k;
125     machine = 68010;
126     break;
127     
128   case M_68020:
129   case M_HP300:
130     arch = bfd_arch_m68k;
131     machine = 68020;
132     break;
133     
134   case M_SPARC:
135     arch = bfd_arch_sparc;
136     machine = 0;
137     break;
138     
139   case M_386:
140     arch = bfd_arch_i386;
141     machine = 0;
142     break;
143     
144   case M_29K:
145     arch = bfd_arch_a29k;
146     machine = 0;
147     break;
148     
149   case M_HPUX:
150     arch = bfd_arch_m68k;
151     machine = 0;
152     break;
153
154   default:
155     arch = bfd_arch_obscure;
156     machine = 0;
157     break;
158   }
159   bfd_set_arch_mach(abfd, arch, machine);  
160   choose_reloc_size(abfd);
161   adata(abfd)->page_size = PAGE_SIZE;
162 #ifdef SEGMENT_SIZE
163   adata(abfd)->segment_size = SEGMENT_SIZE;
164 #else
165   adata(abfd)->segment_size = PAGE_SIZE;
166 #endif
167   adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
168
169   return abfd->xvec;
170 }
171
172
173 /* Write an object file in SunOS format.
174   Section contents have already been written.  We write the
175   file header, symbols, and relocation.  */
176
177 static boolean
178 DEFUN(NAME(aout,sunos4_write_object_contents),
179       (abfd),
180       bfd *abfd)
181 {
182   bfd_size_type data_pad = 0;
183   struct external_exec exec_bytes;
184   struct internal_exec *execp = exec_hdr (abfd);
185     
186   execp->a_text = obj_textsec (abfd)->size;
187     
188   /* Magic number, maestro, please!  */
189   switch (bfd_get_arch(abfd)) {
190   case bfd_arch_m68k:
191     switch (bfd_get_mach(abfd)) {
192     case 68010:
193       N_SET_MACHTYPE(*execp, M_68010);
194       break;
195     default:
196     case 68020:
197       N_SET_MACHTYPE(*execp, M_68020);
198       break;
199     }
200     break;
201   case bfd_arch_sparc:
202     N_SET_MACHTYPE(*execp, M_SPARC);
203     break;
204   case bfd_arch_i386:
205     N_SET_MACHTYPE(*execp, M_386);
206     break;
207   case bfd_arch_a29k:
208     N_SET_MACHTYPE(*execp, M_29K);
209     break;
210   default:
211     N_SET_MACHTYPE(*execp, M_UNKNOWN);
212   }
213     
214   choose_reloc_size(abfd);
215     
216   /* FIXME */
217   N_SET_FLAGS (*execp, 0x1);
218     
219   WRITE_HEADERS(abfd, execp);
220     
221   return true;
222 }
223 \f
224 /* core files */
225
226 #define CORE_MAGIC 0x080456
227 #define CORE_NAMELEN 16
228
229 /* The core structure is taken from the Sun documentation.
230   Unfortunately, they don't document the FPA structure, or at least I
231   can't find it easily.  Fortunately the core header contains its own
232   length.  So this shouldn't cause problems, except for c_ucode, which
233   so far we don't use but is easy to find with a little arithmetic. */
234
235 /* But the reg structure can be gotten from the SPARC processor handbook.
236   This really should be in a GNU include file though so that gdb can use
237   the same info. */
238 struct regs {
239   int r_psr;
240   int r_pc;
241   int r_npc;
242   int r_y;
243   int r_g1;
244   int r_g2;
245   int r_g3;
246   int r_g4;
247   int r_g5;
248   int r_g6;
249   int r_g7;
250   int r_o0;
251   int r_o1;
252   int r_o2;
253   int r_o3;
254   int r_o4;
255   int r_o5;
256   int r_o6;
257   int r_o7;
258 };
259
260 /* Taken from Sun documentation: */
261
262 /* FIXME:  It's worse than we expect.  This struct contains TWO substructs
263   neither of whose size we know, WITH STUFF IN BETWEEN THEM!  We can't
264   even portably access the stuff in between!  */
265
266 struct external_sparc_core {
267   int c_magic;                  /* Corefile magic number */
268   int c_len;                    /* Sizeof (struct core) */
269 #define SPARC_CORE_LEN  432
270   int c_regs[19];               /* General purpose registers -- MACHDEP SIZE */
271   struct external_exec c_aouthdr; /* A.out header */
272   int c_signo;                    /* Killing signal, if any */
273   int c_tsize;                    /* Text size (bytes) */
274   int c_dsize;                    /* Data size (bytes) */
275   int c_ssize;                    /* Stack size (bytes) */
276   char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
277   double fp_stuff[1];               /* external FPU state (size unknown by us) */
278   /* The type "double" is critical here, for alignment.
279     SunOS declares a struct here, but the struct's alignment
280       is double since it contains doubles.  */
281   int c_ucode;                  /* Exception no. from u_code */
282   /* (this member is not accessible by name since we don't
283     portably know the size of fp_stuff.) */
284 };
285
286 struct external_sun3_core {
287   int c_magic;                  /* Corefile magic number */
288   int c_len;                    /* Sizeof (struct core) */
289 #define SUN3_CORE_LEN   826     /* As of SunOS 4.1.1 */
290   int c_regs[18];               /* General purpose registers -- MACHDEP SIZE */
291   struct external_exec c_aouthdr;       /* A.out header */
292   int c_signo;                  /* Killing signal, if any */
293   int c_tsize;                  /* Text size (bytes) */
294   int c_dsize;                  /* Data size (bytes) */
295   int c_ssize;                  /* Stack size (bytes) */
296   char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
297   double fp_stuff[1];               /* external FPU state (size unknown by us) */
298   /* The type "double" is critical here, for alignment.
299     SunOS declares a struct here, but the struct's alignment
300       is double since it contains doubles.  */
301   int c_ucode;                  /* Exception no. from u_code */
302   /* (this member is not accessible by name since we don't
303     portably know the size of fp_stuff.) */
304 };
305
306 struct internal_sunos_core {
307   int c_magic;                  /* Corefile magic number */
308   int c_len;                    /* Sizeof (struct core) */
309   long c_regs_pos;              /* file offset of General purpose registers */
310   int c_regs_size;              /* size of General purpose registers */
311   struct internal_exec c_aouthdr; /* A.out header */
312   int c_signo;                    /* Killing signal, if any */
313   int c_tsize;                    /* Text size (bytes) */
314   int c_dsize;                    /* Data size (bytes) */
315   int c_ssize;                    /* Stack size (bytes) */
316   long c_stacktop;                /* Stack top (address) */
317   char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
318   long fp_stuff_pos;            /* file offset of external FPU state (regs) */
319   int fp_stuff_size;            /* Size of it */
320   int c_ucode;                  /* Exception no. from u_code */
321 };
322
323 /* byte-swap in the Sun-3 core structure */
324 static void
325 DEFUN(swapcore_sun3,(abfd, ext, intcore),
326       bfd *abfd AND
327       char *ext AND
328       struct internal_sunos_core *intcore)
329 {
330   struct external_sun3_core *extcore = (struct external_sun3_core *)ext;
331
332   intcore->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_magic);
333   intcore->c_len   = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_len  );
334   intcore->c_regs_pos  = (long) (((struct external_sun3_core *)0)->c_regs);
335   intcore->c_regs_size = sizeof (extcore->c_regs);
336   NAME(aout,swap_exec_header_in)(abfd, &extcore->c_aouthdr,&intcore->c_aouthdr);
337   intcore->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_signo);
338   intcore->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_tsize);
339   intcore->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_dsize);
340   intcore->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_ssize);
341   bcopy (extcore->c_cmdname, intcore->c_cmdname, sizeof (intcore->c_cmdname));
342   intcore->fp_stuff_pos = (long) (((struct external_sun3_core *)0)->fp_stuff);
343   /* FP stuff takes up whole rest of struct, except c_ucode. */
344   intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
345     (file_ptr)(((struct external_sun3_core *)0)->fp_stuff);
346   /* Ucode is the last thing in the struct -- just before the end */
347   intcore->c_ucode = 
348     bfd_h_get_32 (abfd, 
349                   intcore->c_len - sizeof (extcore->c_ucode) + (unsigned char *)extcore);
350   intcore->c_stacktop = 0x0E000000; /* By experimentation */
351 }
352
353
354 /* byte-swap in the Sparc core structure */
355 static void
356 DEFUN(swapcore_sparc,(abfd, ext, intcore),
357       bfd *abfd AND
358       char *ext AND
359       struct internal_sunos_core *intcore)
360 {
361   struct external_sparc_core *extcore = (struct external_sparc_core *)ext;
362   
363   intcore->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_magic);
364   intcore->c_len   = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_len  );
365   intcore->c_regs_pos  = (long) (((struct external_sparc_core *)0)->c_regs);
366   intcore->c_regs_size = sizeof (extcore->c_regs);
367   NAME(aout,swap_exec_header_in)(abfd, &extcore->c_aouthdr,&intcore->c_aouthdr);
368   intcore->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_signo);
369   intcore->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_tsize);
370   intcore->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_dsize);
371   intcore->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&extcore->c_ssize);
372   bcopy (extcore->c_cmdname, intcore->c_cmdname, sizeof (intcore->c_cmdname));
373   intcore->fp_stuff_pos = (long) (((struct external_sparc_core *)0)->fp_stuff);
374   /* FP stuff takes up whole rest of struct, except c_ucode. */
375   intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
376     (file_ptr)(((struct external_sparc_core *)0)->fp_stuff);
377   /* Ucode is the last thing in the struct -- just before the end */
378   intcore->c_ucode =
379     bfd_h_get_32 (abfd, 
380                   intcore->c_len - sizeof (extcore->c_ucode) + (unsigned char *)extcore);
381   /* Supposedly the user stack grows downward from the bottom of kernel memory.
382      Presuming that this remains true, this definition will work. */
383 #define SPARC_USRSTACK (-(128*1024*1024))
384   intcore->c_stacktop = SPARC_USRSTACK; /* By experimentation */
385 }
386
387 /* need this cast because ptr is really void * */
388 #define core_hdr(bfd) (((struct suncoredata *) (bfd->tdata))->hdr)
389 #define core_datasec(bfd) (((struct suncoredata *) ((bfd)->tdata))->data_section)
390 #define core_stacksec(bfd) (((struct suncoredata*)((bfd)->tdata))->stack_section)
391 #define core_regsec(bfd) (((struct suncoredata *) ((bfd)->tdata))->reg_section)
392 #define core_reg2sec(bfd) (((struct suncoredata *) ((bfd)->tdata))->reg2_section)
393
394 /* These are stored in the bfd's tdata */
395 struct suncoredata {
396   struct internal_sunos_core *hdr;             /* core file header */
397   asection *data_section;
398   asection *stack_section;
399   asection *reg_section;
400   asection *reg2_section;
401 };
402
403 static bfd_target *
404 DEFUN(sunos4_core_file_p,(abfd),
405       bfd *abfd)
406 {
407   unsigned char longbuf[4];     /* Raw bytes of various header fields */
408   int core_size;
409   int core_mag;
410   struct internal_sunos_core *core;
411   char *extcore;
412   struct mergem {
413     struct suncoredata suncoredata;
414     struct internal_sunos_core internal_sunos_core;
415     char external_core[1];
416   } *mergem;
417   
418   bfd_error = system_call_error;
419   
420   if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
421       sizeof (longbuf))
422     return 0;
423   core_mag = bfd_h_get_32 (abfd, longbuf);
424
425   if (core_mag != CORE_MAGIC) return 0;
426
427   /* SunOS core headers can vary in length; second word is size; */
428   if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
429       sizeof (longbuf))
430     return 0;
431   core_size = bfd_h_get_32 (abfd, longbuf);
432   /* Sanity check */
433   if (core_size > 20000)
434     return 0;
435
436   if (bfd_seek (abfd, 0L, false) < 0) return 0;
437
438   mergem = (struct mergem *)bfd_zalloc (abfd, core_size + sizeof (struct mergem));
439   if (mergem == NULL) {
440     bfd_error = no_memory;
441     return 0;
442   }
443
444   extcore = mergem->external_core;
445
446   if ((bfd_read ((PTR) extcore, 1, core_size, abfd)) != core_size) {
447     bfd_error = system_call_error;
448     bfd_release (abfd, (char *)mergem);
449     return 0;
450   }
451
452   /* Validate that it's a core file we know how to handle, due to sun
453      botching the positioning of registers and other fields in a machine
454      dependent way.  */
455   core = &mergem->internal_sunos_core;
456   switch (core_size) {
457   case SPARC_CORE_LEN:
458     swapcore_sparc (abfd, extcore, core);
459     break;
460   case SUN3_CORE_LEN:
461     swapcore_sun3 (abfd, extcore, core);
462     break;
463   default:
464     bfd_error = system_call_error;              /* FIXME */
465     bfd_release (abfd, (char *)mergem);
466     return 0;
467   }
468
469   set_tdata (abfd, &mergem->suncoredata);
470   core_hdr (abfd) = core;
471
472   /* create the sections.  This is raunchy, but bfd_close wants to reclaim
473      them */
474   core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
475   if (core_stacksec (abfd) == NULL) {
476   loser:
477     bfd_error = no_memory;
478     bfd_release (abfd, (char *)mergem);
479     return 0;
480   }
481   core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
482   if (core_datasec (abfd) == NULL) {
483   loser1:
484     bfd_release (abfd, core_stacksec (abfd));
485     goto loser;
486   }
487   core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
488   if (core_regsec (abfd) == NULL) {
489   loser2:
490     bfd_release (abfd, core_datasec (abfd));
491     goto loser1;
492   }
493   core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
494   if (core_reg2sec (abfd) == NULL) {
495     bfd_release (abfd, core_regsec (abfd));
496     goto loser2;
497   }
498
499   core_stacksec (abfd)->name = ".stack";
500   core_datasec (abfd)->name = ".data";
501   core_regsec (abfd)->name = ".reg";
502   core_reg2sec (abfd)->name = ".reg2";
503
504   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
505   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
506   core_regsec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
507   core_reg2sec (abfd)->flags = SEC_ALLOC + SEC_HAS_CONTENTS;
508
509   core_stacksec (abfd)->size = core->c_ssize;
510   core_datasec (abfd)->size = core->c_dsize;
511   core_regsec (abfd)->size = core->c_regs_size;
512   core_reg2sec (abfd)->size = core->fp_stuff_size;
513
514   core_stacksec (abfd)->vma = (core->c_stacktop - core->c_ssize);
515   core_datasec (abfd)->vma = N_DATADDR(core->c_aouthdr);
516   core_regsec (abfd)->vma = 0;
517   core_reg2sec (abfd)->vma = 0;
518
519   core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
520   core_datasec (abfd)->filepos = core->c_len;
521   /* We'll access the regs afresh in the core file, like any section: */
522   core_regsec (abfd)->filepos = (file_ptr)core->c_regs_pos;
523   core_reg2sec (abfd)->filepos = (file_ptr)core->fp_stuff_pos;
524
525   /* Align to word at least */
526   core_stacksec (abfd)->alignment_power = 2;
527   core_datasec (abfd)->alignment_power = 2;
528   core_regsec (abfd)->alignment_power = 2;
529   core_reg2sec (abfd)->alignment_power = 2;
530
531   abfd->sections = core_stacksec (abfd);
532   core_stacksec (abfd)->next = core_datasec (abfd);
533   core_datasec (abfd)->next = core_regsec (abfd);
534   core_regsec (abfd)->next = core_reg2sec (abfd);
535
536   abfd->section_count = 4;
537
538   return abfd->xvec;
539 }
540
541 static char *sunos4_core_file_failing_command (abfd)
542 bfd *abfd;
543   {
544   return core_hdr (abfd)->c_cmdname;
545 }
546
547 static int
548 DEFUN(sunos4_core_file_failing_signal,(abfd),
549       bfd *abfd)
550 {
551   return core_hdr (abfd)->c_signo;
552 }
553
554 static boolean
555 DEFUN(sunos4_core_file_matches_executable_p, (core_bfd, exec_bfd),
556       bfd *core_bfd AND
557       bfd *exec_bfd)
558 {
559   if (core_bfd->xvec != exec_bfd->xvec) {
560     bfd_error = system_call_error;
561     return false;
562   }
563
564   return (bcmp ((char *)&core_hdr (core_bfd)->c_aouthdr, 
565                 (char *) exec_hdr (exec_bfd),
566                 sizeof (struct internal_exec)) == 0) ? true : false;
567 }
568 \f
569 #define MY_core_file_failing_command    sunos4_core_file_failing_command
570 #define MY_core_file_failing_signal     sunos4_core_file_failing_signal
571 #define MY_core_file_matches_executable_p sunos4_core_file_matches_executable_p
572
573 #define MY_bfd_debug_info_start         bfd_void
574 #define MY_bfd_debug_info_end           bfd_void
575 #define MY_bfd_debug_info_accumulate    (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
576 #define MY_object_p NAME(sunos,object_p)
577 #define MY_core_file_p sunos4_core_file_p
578 #define MY_write_object_contents NAME(aout,sunos4_write_object_contents)
579
580 #define TARGET_IS_BIG_ENDIAN_P
581
582 #include "aout-target.h"