Merge from vendor branch BINUTILS:
[dragonfly.git] / contrib / binutils / bfd / aoutf1.h
1 /* A.out "format 1" file handling code for BFD.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
3    2001, 2002
4    Free Software Foundation, Inc.
5    Written by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26
27 #include "aout/sun4.h"
28 #include "libaout.h"            /* BFD a.out internal data structures */
29
30 #include "aout/aout64.h"
31 #include "aout/stab_gnu.h"
32 #include "aout/ar.h"
33
34 /* This is needed to reject a NewsOS file, e.g. in
35    gdb/testsuite/gdb.t10/crossload.exp. <kingdon@cygnus.com>
36    I needed to add M_UNKNOWN to recognize a 68000 object, so this will
37    probably no longer reject a NewsOS object.  <ian@cygnus.com>.  */
38 #ifndef MACHTYPE_OK
39 #define MACHTYPE_OK(mtype) \
40   (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
41    || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
42        && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
43 #endif
44
45 /*
46 The file @code{aoutf1.h} contains the code for BFD's
47 a.out back end. Control over the generated back end is given by these
48 two preprocessor names:
49 @table @code
50 @item ARCH_SIZE
51 This value should be either 32 or 64, depending upon the size of an
52 int in the target format. It changes the sizes of the structs which
53 perform the memory/disk mapping of structures.
54
55 The 64 bit backend may only be used if the host compiler supports 64
56 ints (eg long long with gcc), by defining the name @code{BFD_HOST_64_BIT} in @code{bfd.h}.
57 With this name defined, @emph{all} bfd operations are performed with 64bit
58 arithmetic, not just those to a 64bit target.
59
60 @item TARGETNAME
61 The name put into the target vector.
62 @item
63 @end table
64
65 */
66
67 /*SUPPRESS558*/
68 /*SUPPRESS529*/
69
70 #if ARCH_SIZE == 64
71 #define sunos_set_arch_mach sunos_64_set_arch_mach
72 #define sunos_write_object_contents aout_64_sunos4_write_object_contents
73 #else
74 #define sunos_set_arch_mach sunos_32_set_arch_mach
75 #define sunos_write_object_contents aout_32_sunos4_write_object_contents
76 #endif
77
78 static boolean sunos_merge_private_bfd_data PARAMS ((bfd *, bfd *));
79 static void sunos_set_arch_mach PARAMS ((bfd *, enum machine_type));
80 static void choose_reloc_size PARAMS ((bfd *));
81 static boolean sunos_write_object_contents PARAMS ((bfd *));
82 static const bfd_target *sunos4_core_file_p PARAMS ((bfd *));
83 static char *sunos4_core_file_failing_command PARAMS ((bfd *));
84 static int sunos4_core_file_failing_signal PARAMS ((bfd *));
85 static boolean sunos4_core_file_matches_executable_p PARAMS ((bfd *, bfd *));
86 static boolean sunos4_set_sizes PARAMS ((bfd *));
87
88 /* Merge backend data into the output file.
89    This is necessary on sparclet-aout where we want the resultant machine
90    number to be M_SPARCLET if any input file is M_SPARCLET.  */
91
92 #define MY_bfd_merge_private_bfd_data sunos_merge_private_bfd_data
93
94 static boolean
95 sunos_merge_private_bfd_data (ibfd, obfd)
96      bfd *ibfd, *obfd;
97 {
98   if (bfd_get_flavour (ibfd) != bfd_target_aout_flavour
99       || bfd_get_flavour (obfd) != bfd_target_aout_flavour)
100     return true;
101
102   if (bfd_get_arch (obfd) == bfd_arch_sparc)
103     {
104       if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
105         bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
106     }
107
108   return true;
109 }
110
111 /* This is either sunos_32_set_arch_mach or sunos_64_set_arch_mach,
112    depending upon ARCH_SIZE.  */
113
114 static void
115 sunos_set_arch_mach (abfd, machtype)
116      bfd *abfd;
117      enum machine_type machtype;
118 {
119   /* Determine the architecture and machine type of the object file.  */
120   enum bfd_architecture arch;
121   unsigned long machine;
122   switch (machtype)
123     {
124
125     case M_UNKNOWN:
126       /* Some Sun3s make magic numbers without cpu types in them, so
127          we'll default to the 68000.  */
128       arch = bfd_arch_m68k;
129       machine = bfd_mach_m68000;
130       break;
131
132     case M_68010:
133     case M_HP200:
134       arch = bfd_arch_m68k;
135       machine = bfd_mach_m68010;
136       break;
137
138     case M_68020:
139     case M_HP300:
140       arch = bfd_arch_m68k;
141       machine = bfd_mach_m68020;
142       break;
143
144     case M_SPARC:
145       arch = bfd_arch_sparc;
146       machine = 0;
147       break;
148
149     case M_SPARCLET:
150       arch = bfd_arch_sparc;
151       machine = bfd_mach_sparc_sparclet;
152       break;
153
154     case M_SPARCLITE_LE:
155       arch = bfd_arch_sparc;
156       machine = bfd_mach_sparc_sparclite_le;
157       break;
158
159     case M_386:
160     case M_386_DYNIX:
161       arch = bfd_arch_i386;
162       machine = 0;
163       break;
164
165     case M_29K:
166       arch = bfd_arch_a29k;
167       machine = 0;
168       break;
169
170     case M_HPUX:
171       arch = bfd_arch_m68k;
172       machine = 0;
173       break;
174
175     default:
176       arch = bfd_arch_obscure;
177       machine = 0;
178       break;
179     }
180   bfd_set_arch_mach (abfd, arch, machine);
181 }
182
183 #define SET_ARCH_MACH(ABFD, EXEC) \
184   NAME(sunos,set_arch_mach) (ABFD, N_MACHTYPE (EXEC)); \
185   choose_reloc_size(ABFD);
186
187 /* Determine the size of a relocation entry, based on the architecture */
188 static void
189 choose_reloc_size (abfd)
190      bfd *abfd;
191 {
192   switch (bfd_get_arch (abfd))
193     {
194     case bfd_arch_sparc:
195     case bfd_arch_a29k:
196       obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
197       break;
198     default:
199       obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
200       break;
201     }
202 }
203
204 /* Write an object file in SunOS format.  Section contents have
205    already been written.  We write the file header, symbols, and
206    relocation.  The real name of this function is either
207    aout_64_sunos4_write_object_contents or
208    aout_32_sunos4_write_object_contents, depending upon ARCH_SIZE.  */
209
210 static boolean
211 sunos_write_object_contents (abfd)
212      bfd *abfd;
213 {
214   struct external_exec exec_bytes;
215   struct internal_exec *execp = exec_hdr (abfd);
216
217   /* Magic number, maestro, please!  */
218   switch (bfd_get_arch (abfd))
219     {
220     case bfd_arch_m68k:
221       switch (bfd_get_mach (abfd))
222         {
223         case bfd_mach_m68000:
224           N_SET_MACHTYPE (*execp, M_UNKNOWN);
225           break;
226         case bfd_mach_m68010:
227           N_SET_MACHTYPE (*execp, M_68010);
228           break;
229         default:
230         case bfd_mach_m68020:
231           N_SET_MACHTYPE (*execp, M_68020);
232           break;
233         }
234       break;
235     case bfd_arch_sparc:
236       switch (bfd_get_mach (abfd))
237         {
238         case bfd_mach_sparc_sparclet:
239           N_SET_MACHTYPE (*execp, M_SPARCLET);
240           break;
241         case bfd_mach_sparc_sparclite_le:
242           N_SET_MACHTYPE (*execp, M_SPARCLITE_LE);
243           break;
244         default:
245           N_SET_MACHTYPE (*execp, M_SPARC);
246           break;
247         }
248       break;
249     case bfd_arch_i386:
250       N_SET_MACHTYPE (*execp, M_386);
251       break;
252     case bfd_arch_a29k:
253       N_SET_MACHTYPE (*execp, M_29K);
254       break;
255     default:
256       N_SET_MACHTYPE (*execp, M_UNKNOWN);
257     }
258
259   choose_reloc_size (abfd);
260
261   N_SET_FLAGS (*execp, aout_backend_info (abfd)->exec_hdr_flags);
262
263   N_SET_DYNAMIC (*execp, (long)(bfd_get_file_flags (abfd) & DYNAMIC));
264
265   WRITE_HEADERS (abfd, execp);
266
267   return true;
268 }
269 \f
270 /* core files */
271
272 #define CORE_MAGIC 0x080456
273 #define CORE_NAMELEN 16
274
275 /* The core structure is taken from the Sun documentation.
276   Unfortunately, they don't document the FPA structure, or at least I
277   can't find it easily.  Fortunately the core header contains its own
278   length.  So this shouldn't cause problems, except for c_ucode, which
279   so far we don't use but is easy to find with a little arithmetic.  */
280
281 /* But the reg structure can be gotten from the SPARC processor handbook.
282   This really should be in a GNU include file though so that gdb can use
283   the same info.  */
284 struct regs
285 {
286   int r_psr;
287   int r_pc;
288   int r_npc;
289   int r_y;
290   int r_g1;
291   int r_g2;
292   int r_g3;
293   int r_g4;
294   int r_g5;
295   int r_g6;
296   int r_g7;
297   int r_o0;
298   int r_o1;
299   int r_o2;
300   int r_o3;
301   int r_o4;
302   int r_o5;
303   int r_o6;
304   int r_o7;
305 };
306
307 /* Taken from Sun documentation: */
308
309 /* FIXME:  It's worse than we expect.  This struct contains TWO substructs
310   neither of whose size we know, WITH STUFF IN BETWEEN THEM!  We can't
311   even portably access the stuff in between!  */
312
313 struct external_sparc_core
314   {
315     int c_magic;                /* Corefile magic number */
316     int c_len;                  /* Sizeof (struct core) */
317 #define SPARC_CORE_LEN  432
318     int c_regs[19];             /* General purpose registers -- MACHDEP SIZE */
319     struct external_exec c_aouthdr;     /* A.out header */
320     int c_signo;                /* Killing signal, if any */
321     int c_tsize;                /* Text size (bytes) */
322     int c_dsize;                /* Data size (bytes) */
323     int c_ssize;                /* Stack size (bytes) */
324     char c_cmdname[CORE_NAMELEN + 1];   /* Command name */
325     double fp_stuff[1];         /* external FPU state (size unknown by us) */
326     /* The type "double" is critical here, for alignment.
327     SunOS declares a struct here, but the struct's alignment
328       is double since it contains doubles.  */
329     int c_ucode;                /* Exception no. from u_code */
330     /* (this member is not accessible by name since we don't
331     portably know the size of fp_stuff.) */
332   };
333
334 /* Core files generated by the BCP (the part of Solaris which allows
335    it to run SunOS4 a.out files).  */
336 struct external_solaris_bcp_core
337   {
338     int c_magic;                /* Corefile magic number */
339     int c_len;                  /* Sizeof (struct core) */
340 #define SOLARIS_BCP_CORE_LEN    456
341     int c_regs[19];             /* General purpose registers -- MACHDEP SIZE */
342     int c_exdata_vp;            /* exdata structure */
343     int c_exdata_tsize;
344     int c_exdata_dsize;
345     int c_exdata_bsize;
346     int c_exdata_lsize;
347     int c_exdata_nshlibs;
348     short c_exdata_mach;
349     short c_exdata_mag;
350     int c_exdata_toffset;
351     int c_exdata_doffset;
352     int c_exdata_loffset;
353     int c_exdata_txtorg;
354     int c_exdata_datorg;
355     int c_exdata_entloc;
356     int c_signo;                /* Killing signal, if any */
357     int c_tsize;                /* Text size (bytes) */
358     int c_dsize;                /* Data size (bytes) */
359     int c_ssize;                /* Stack size (bytes) */
360     char c_cmdname[CORE_NAMELEN + 1];   /* Command name */
361     double fp_stuff[1];         /* external FPU state (size unknown by us) */
362     /* The type "double" is critical here, for alignment.
363     SunOS declares a struct here, but the struct's alignment
364       is double since it contains doubles.  */
365     int c_ucode;                /* Exception no. from u_code */
366     /* (this member is not accessible by name since we don't
367     portably know the size of fp_stuff.) */
368   };
369
370 struct external_sun3_core
371   {
372     int c_magic;                /* Corefile magic number */
373     int c_len;                  /* Sizeof (struct core) */
374 #define SUN3_CORE_LEN   826     /* As of SunOS 4.1.1 */
375     int c_regs[18];             /* General purpose registers -- MACHDEP SIZE */
376     struct external_exec c_aouthdr;     /* A.out header */
377     int c_signo;                /* Killing signal, if any */
378     int c_tsize;                /* Text size (bytes) */
379     int c_dsize;                /* Data size (bytes) */
380     int c_ssize;                /* Stack size (bytes) */
381     char c_cmdname[CORE_NAMELEN + 1];   /* Command name */
382     double fp_stuff[1];         /* external FPU state (size unknown by us) */
383     /* The type "double" is critical here, for alignment.
384     SunOS declares a struct here, but the struct's alignment
385       is double since it contains doubles.  */
386     int c_ucode;                /* Exception no. from u_code */
387     /* (this member is not accessible by name since we don't
388     portably know the size of fp_stuff.) */
389   };
390
391 struct internal_sunos_core
392   {
393     int c_magic;                /* Corefile magic number */
394     int c_len;                  /* Sizeof (struct core) */
395     long c_regs_pos;            /* file offset of General purpose registers */
396     int c_regs_size;            /* size of General purpose registers */
397     struct internal_exec c_aouthdr;     /* A.out header */
398     int c_signo;                /* Killing signal, if any */
399     int c_tsize;                /* Text size (bytes) */
400     int c_dsize;                /* Data size (bytes) */
401     bfd_vma c_data_addr;        /* Data start (address) */
402     int c_ssize;                /* Stack size (bytes) */
403     bfd_vma c_stacktop;         /* Stack top (address) */
404     char c_cmdname[CORE_NAMELEN + 1];   /* Command name */
405     long fp_stuff_pos;          /* file offset of external FPU state (regs) */
406     int fp_stuff_size;          /* Size of it */
407     int c_ucode;                /* Exception no. from u_code */
408   };
409
410 static void swapcore_sun3
411   PARAMS ((bfd *, char *, struct internal_sunos_core *));
412 static void swapcore_sparc
413   PARAMS ((bfd *, char *, struct internal_sunos_core *));
414 static void swapcore_solaris_bcp
415   PARAMS ((bfd *, char *, struct internal_sunos_core *));
416
417 /* byte-swap in the Sun-3 core structure */
418 static void
419 swapcore_sun3 (abfd, ext, intcore)
420      bfd *abfd;
421      char *ext;
422      struct internal_sunos_core *intcore;
423 {
424   struct external_sun3_core *extcore = (struct external_sun3_core *) ext;
425
426   intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
427   intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
428   intcore->c_regs_pos = (long) (((struct external_sun3_core *) 0)->c_regs);
429   intcore->c_regs_size = sizeof (extcore->c_regs);
430 #if ARCH_SIZE == 64
431   aout_64_swap_exec_header_in
432 #else
433   aout_32_swap_exec_header_in
434 #endif
435     (abfd, &extcore->c_aouthdr, &intcore->c_aouthdr);
436   intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
437   intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
438   intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
439   intcore->c_data_addr = N_DATADDR (intcore->c_aouthdr);
440   intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
441   memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
442   intcore->fp_stuff_pos = (long) (((struct external_sun3_core *) 0)->fp_stuff);
443   /* FP stuff takes up whole rest of struct, except c_ucode.  */
444   intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
445     (file_ptr) (((struct external_sun3_core *) 0)->fp_stuff);
446   /* Ucode is the last thing in the struct -- just before the end */
447   intcore->c_ucode = H_GET_32 (abfd,
448                                (intcore->c_len
449                                 - sizeof (extcore->c_ucode)
450                                 + (unsigned char *) extcore));
451   intcore->c_stacktop = 0x0E000000;     /* By experimentation */
452 }
453
454 /* byte-swap in the Sparc core structure */
455 static void
456 swapcore_sparc (abfd, ext, intcore)
457      bfd *abfd;
458      char *ext;
459      struct internal_sunos_core *intcore;
460 {
461   struct external_sparc_core *extcore = (struct external_sparc_core *) ext;
462
463   intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
464   intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
465   intcore->c_regs_pos = (long) (((struct external_sparc_core *) 0)->c_regs);
466   intcore->c_regs_size = sizeof (extcore->c_regs);
467 #if ARCH_SIZE == 64
468   aout_64_swap_exec_header_in
469 #else
470   aout_32_swap_exec_header_in
471 #endif
472     (abfd, &extcore->c_aouthdr, &intcore->c_aouthdr);
473   intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
474   intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
475   intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
476   intcore->c_data_addr = N_DATADDR (intcore->c_aouthdr);
477   intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
478   memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
479   intcore->fp_stuff_pos = (long) (((struct external_sparc_core *) 0)->fp_stuff);
480   /* FP stuff takes up whole rest of struct, except c_ucode.  */
481   intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
482     (file_ptr) (((struct external_sparc_core *) 0)->fp_stuff);
483   /* Ucode is the last thing in the struct -- just before the end */
484   intcore->c_ucode = H_GET_32 (abfd,
485                                (intcore->c_len
486                                 - sizeof (extcore->c_ucode)
487                                 + (unsigned char *) extcore));
488
489   /* Supposedly the user stack grows downward from the bottom of kernel memory.
490      Presuming that this remains true, this definition will work.  */
491   /* Now sun has provided us with another challenge.  The value is different
492      for sparc2 and sparc10 (both running SunOS 4.1.3).  We pick one or
493      the other based on the current value of the stack pointer.  This
494      loses (a) if the stack pointer has been clobbered, or (b) if the stack
495      is larger than 128 megabytes.
496
497      It's times like these you're glad they're switching to ELF.
498
499      Note that using include files or nlist on /vmunix would be wrong,
500      because we want the value for this core file, no matter what kind of
501      machine we were compiled on or are running on.  */
502 #define SPARC_USRSTACK_SPARC2 ((bfd_vma)0xf8000000)
503 #define SPARC_USRSTACK_SPARC10 ((bfd_vma)0xf0000000)
504   {
505     bfd_vma sp = H_GET_32 (abfd, &((struct regs *) &extcore->c_regs[0])->r_o6);
506     if (sp < SPARC_USRSTACK_SPARC10)
507       intcore->c_stacktop = SPARC_USRSTACK_SPARC10;
508     else
509       intcore->c_stacktop = SPARC_USRSTACK_SPARC2;
510   }
511 }
512
513 /* byte-swap in the Solaris BCP core structure */
514 static void
515 swapcore_solaris_bcp (abfd, ext, intcore)
516      bfd *abfd;
517      char *ext;
518      struct internal_sunos_core *intcore;
519 {
520   struct external_solaris_bcp_core *extcore =
521     (struct external_solaris_bcp_core *) ext;
522
523   intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
524   intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
525   intcore->c_regs_pos = (long) (((struct external_solaris_bcp_core *) 0)->c_regs);
526   intcore->c_regs_size = sizeof (extcore->c_regs);
527
528   /* The Solaris BCP exdata structure does not contain an a_syms field,
529      so we are unable to synthesize an internal exec header.
530      Luckily we are able to figure out the start address of the data section,
531      which is the only thing needed from the internal exec header,
532      from the exdata structure.
533
534      As of Solaris 2.3, BCP core files for statically linked executables
535      are buggy. The exdata structure is not properly filled in, and
536      the data section is written from address zero instead of the data
537      start address.  */
538   memset ((PTR) &intcore->c_aouthdr, 0, sizeof (struct internal_exec));
539   intcore->c_data_addr = H_GET_32 (abfd, &extcore->c_exdata_datorg);
540   intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
541   intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
542   intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
543   intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
544   memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
545   intcore->fp_stuff_pos =
546     (long) (((struct external_solaris_bcp_core *) 0)->fp_stuff);
547   /* FP stuff takes up whole rest of struct, except c_ucode.  */
548   intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
549     (file_ptr) (((struct external_solaris_bcp_core *) 0)->fp_stuff);
550   /* Ucode is the last thing in the struct -- just before the end */
551   intcore->c_ucode = H_GET_32 (abfd,
552                                (intcore->c_len
553                                 - sizeof (extcore->c_ucode)
554                                 + (unsigned char *) extcore));
555
556   /* Supposedly the user stack grows downward from the bottom of kernel memory.
557      Presuming that this remains true, this definition will work.  */
558   /* Now sun has provided us with another challenge.  The value is different
559      for sparc2 and sparc10 (both running SunOS 4.1.3).  We pick one or
560      the other based on the current value of the stack pointer.  This
561      loses (a) if the stack pointer has been clobbered, or (b) if the stack
562      is larger than 128 megabytes.
563
564      It's times like these you're glad they're switching to ELF.
565
566      Note that using include files or nlist on /vmunix would be wrong,
567      because we want the value for this core file, no matter what kind of
568      machine we were compiled on or are running on.  */
569 #define SPARC_USRSTACK_SPARC2 ((bfd_vma)0xf8000000)
570 #define SPARC_USRSTACK_SPARC10 ((bfd_vma)0xf0000000)
571   {
572     bfd_vma sp = H_GET_32 (abfd, &((struct regs *) &extcore->c_regs[0])->r_o6);
573     if (sp < SPARC_USRSTACK_SPARC10)
574       intcore->c_stacktop = SPARC_USRSTACK_SPARC10;
575     else
576       intcore->c_stacktop = SPARC_USRSTACK_SPARC2;
577   }
578 }
579
580 /* need this cast because ptr is really void * */
581 #define core_hdr(bfd) ((bfd)->tdata.sun_core_data)
582 #define core_datasec(bfd) (core_hdr(bfd)->data_section)
583 #define core_stacksec(bfd) (core_hdr(bfd)->stack_section)
584 #define core_regsec(bfd) (core_hdr(bfd)->reg_section)
585 #define core_reg2sec(bfd) (core_hdr(bfd)->reg2_section)
586
587 /* These are stored in the bfd's tdata */
588 struct sun_core_struct
589 {
590   struct internal_sunos_core *hdr;      /* core file header */
591   asection *data_section;
592   asection *stack_section;
593   asection *reg_section;
594   asection *reg2_section;
595 };
596
597 static const bfd_target *
598 sunos4_core_file_p (abfd)
599      bfd *abfd;
600 {
601   unsigned char longbuf[4];     /* Raw bytes of various header fields */
602   bfd_size_type core_size, amt;
603   unsigned long core_mag;
604   struct internal_sunos_core *core;
605   char *extcore;
606   struct mergem
607     {
608       struct sun_core_struct suncoredata;
609       struct internal_sunos_core internal_sunos_core;
610       char external_core[1];
611     } *mergem;
612
613   if (bfd_bread ((PTR) longbuf, (bfd_size_type) sizeof (longbuf), abfd)
614       != sizeof (longbuf))
615     return 0;
616   core_mag = H_GET_32 (abfd, longbuf);
617
618   if (core_mag != CORE_MAGIC)
619     return 0;
620
621   /* SunOS core headers can vary in length; second word is size; */
622   if (bfd_bread ((PTR) longbuf, (bfd_size_type) sizeof (longbuf), abfd)
623       != sizeof (longbuf))
624     return 0;
625   core_size = H_GET_32 (abfd, longbuf);
626   /* Sanity check */
627   if (core_size > 20000)
628     return 0;
629
630   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
631     return 0;
632
633   amt = core_size + sizeof (struct mergem);
634   mergem = (struct mergem *) bfd_zalloc (abfd, amt);
635   if (mergem == NULL)
636     return 0;
637
638   extcore = mergem->external_core;
639
640   if ((bfd_bread ((PTR) extcore, core_size, abfd)) != core_size)
641     {
642     loser:
643       bfd_release (abfd, (char *) mergem);
644       abfd->tdata.any = NULL;
645       bfd_section_list_clear (abfd);
646       return 0;
647     }
648
649   /* Validate that it's a core file we know how to handle, due to sun
650      botching the positioning of registers and other fields in a machine
651      dependent way.  */
652   core = &mergem->internal_sunos_core;
653   switch (core_size)
654     {
655     case SPARC_CORE_LEN:
656       swapcore_sparc (abfd, extcore, core);
657       break;
658     case SUN3_CORE_LEN:
659       swapcore_sun3 (abfd, extcore, core);
660       break;
661     case SOLARIS_BCP_CORE_LEN:
662       swapcore_solaris_bcp (abfd, extcore, core);
663       break;
664     default:
665       bfd_set_error (bfd_error_system_call);    /* FIXME */
666       goto loser;
667     }
668
669   abfd->tdata.sun_core_data = &mergem->suncoredata;
670   abfd->tdata.sun_core_data->hdr = core;
671
672   /* Create the sections.  */
673   core_stacksec (abfd) = bfd_make_section_anyway (abfd, ".stack");
674   if (core_stacksec (abfd) == NULL)
675     /* bfd_release frees everything allocated after it's arg.  */
676     goto loser;
677
678   core_datasec (abfd) = bfd_make_section_anyway (abfd, ".data");
679   if (core_datasec (abfd) == NULL)
680     goto loser;
681
682   core_regsec (abfd) = bfd_make_section_anyway (abfd, ".reg");
683   if (core_regsec (abfd) == NULL)
684     goto loser;
685
686   core_reg2sec (abfd) = bfd_make_section_anyway (abfd, ".reg2");
687   if (core_reg2sec (abfd) == NULL)
688     goto loser;
689
690   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
691   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
692   core_regsec (abfd)->flags = SEC_HAS_CONTENTS;
693   core_reg2sec (abfd)->flags = SEC_HAS_CONTENTS;
694
695   core_stacksec (abfd)->_raw_size = core->c_ssize;
696   core_datasec (abfd)->_raw_size = core->c_dsize;
697   core_regsec (abfd)->_raw_size = core->c_regs_size;
698   core_reg2sec (abfd)->_raw_size = core->fp_stuff_size;
699
700   core_stacksec (abfd)->vma = (core->c_stacktop - core->c_ssize);
701   core_datasec (abfd)->vma = core->c_data_addr;
702   core_regsec (abfd)->vma = 0;
703   core_reg2sec (abfd)->vma = 0;
704
705   core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
706   core_datasec (abfd)->filepos = core->c_len;
707   /* We'll access the regs afresh in the core file, like any section: */
708   core_regsec (abfd)->filepos = (file_ptr) core->c_regs_pos;
709   core_reg2sec (abfd)->filepos = (file_ptr) core->fp_stuff_pos;
710
711   /* Align to word at least */
712   core_stacksec (abfd)->alignment_power = 2;
713   core_datasec (abfd)->alignment_power = 2;
714   core_regsec (abfd)->alignment_power = 2;
715   core_reg2sec (abfd)->alignment_power = 2;
716
717   return abfd->xvec;
718 }
719
720 static char *
721 sunos4_core_file_failing_command (abfd)
722      bfd *abfd;
723 {
724   return core_hdr (abfd)->hdr->c_cmdname;
725 }
726
727 static int
728 sunos4_core_file_failing_signal (abfd)
729      bfd *abfd;
730 {
731   return core_hdr (abfd)->hdr->c_signo;
732 }
733
734 static boolean
735 sunos4_core_file_matches_executable_p (core_bfd, exec_bfd)
736      bfd *core_bfd;
737      bfd *exec_bfd;
738 {
739   if (core_bfd->xvec != exec_bfd->xvec)
740     {
741       bfd_set_error (bfd_error_system_call);
742       return false;
743     }
744
745   /* Solaris core files do not include an aouthdr.  */
746   if ((core_hdr (core_bfd)->hdr)->c_len == SOLARIS_BCP_CORE_LEN)
747     return true;
748
749   return (memcmp ((char *) &((core_hdr (core_bfd)->hdr)->c_aouthdr),
750                   (char *) exec_hdr (exec_bfd),
751                   sizeof (struct internal_exec)) == 0) ? true : false;
752 }
753
754 #define MY_set_sizes sunos4_set_sizes
755 static boolean
756 sunos4_set_sizes (abfd)
757      bfd *abfd;
758 {
759   switch (bfd_get_arch (abfd))
760     {
761     default:
762       return false;
763     case bfd_arch_sparc:
764       adata (abfd).page_size = 0x2000;
765       adata (abfd).segment_size = 0x2000;
766       adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
767       return true;
768     case bfd_arch_m68k:
769       adata (abfd).page_size = 0x2000;
770       adata (abfd).segment_size = 0x20000;
771       adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
772       return true;
773     }
774 }
775
776 /* We default to setting the toolversion field to 1, as is required by
777    SunOS.  */
778 #ifndef MY_exec_hdr_flags
779 #define MY_exec_hdr_flags 1
780 #endif
781
782 #ifndef MY_entry_is_text_address
783 #define MY_entry_is_text_address 0
784 #endif
785 #ifndef MY_add_dynamic_symbols
786 #define MY_add_dynamic_symbols 0
787 #endif
788 #ifndef MY_add_one_symbol
789 #define MY_add_one_symbol 0
790 #endif
791 #ifndef MY_link_dynamic_object
792 #define MY_link_dynamic_object 0
793 #endif
794 #ifndef MY_write_dynamic_symbol
795 #define MY_write_dynamic_symbol 0
796 #endif
797 #ifndef MY_check_dynamic_reloc
798 #define MY_check_dynamic_reloc 0
799 #endif
800 #ifndef MY_finish_dynamic_link
801 #define MY_finish_dynamic_link 0
802 #endif
803
804 static const struct aout_backend_data sunos4_aout_backend =
805 {
806   0,                            /* zmagic files are not contiguous */
807   1,                            /* text includes header */
808   MY_entry_is_text_address,
809   MY_exec_hdr_flags,
810   0,                            /* default text vma */
811   sunos4_set_sizes,
812   0,                            /* header is counted in zmagic text */
813   MY_add_dynamic_symbols,
814   MY_add_one_symbol,
815   MY_link_dynamic_object,
816   MY_write_dynamic_symbol,
817   MY_check_dynamic_reloc,
818   MY_finish_dynamic_link
819 };
820 \f
821 #define MY_core_file_failing_command    sunos4_core_file_failing_command
822 #define MY_core_file_failing_signal     sunos4_core_file_failing_signal
823 #define MY_core_file_matches_executable_p sunos4_core_file_matches_executable_p
824
825 #define MY_bfd_debug_info_start         bfd_void
826 #define MY_bfd_debug_info_end           bfd_void
827 #define MY_bfd_debug_info_accumulate    \
828                         (void (*) PARAMS ((bfd *, struct sec *))) bfd_void
829 #define MY_core_file_p                  sunos4_core_file_p
830 #define MY_write_object_contents        NAME(aout,sunos4_write_object_contents)
831 #define MY_backend_data                 &sunos4_aout_backend
832
833 #ifndef TARGET_IS_LITTLE_ENDIAN_P
834 #define TARGET_IS_BIG_ENDIAN_P
835 #endif
836
837 #include "aout-target.h"