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