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