OSDN Git Service

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