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