Back to index

cell-binutils  2.17cvs20070401
aoutf1.h
Go to the documentation of this file.
00001 /* A.out "format 1" file handling code for BFD.
00002    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
00003    2001, 2002, 2003, 2004, 2005, 2006
00004    Free Software Foundation, Inc.
00005    Written by Cygnus Support.
00006 
00007    This file is part of BFD, the Binary File Descriptor library.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00022 
00023 #include "bfd.h"
00024 #include "sysdep.h"
00025 #include "libbfd.h"
00026 
00027 #include "aout/sun4.h"
00028 #include "libaout.h"        /* BFD a.out internal data structures.  */
00029 
00030 #include "aout/aout64.h"
00031 #include "aout/stab_gnu.h"
00032 #include "aout/ar.h"
00033 
00034 /* This is needed to reject a NewsOS file, e.g. in
00035    gdb/testsuite/gdb.t10/crossload.exp. <kingdon@cygnus.com>
00036    I needed to add M_UNKNOWN to recognize a 68000 object, so this will
00037    probably no longer reject a NewsOS object.  <ian@cygnus.com>.  */
00038 #ifndef MACHTYPE_OK
00039 #define MACHTYPE_OK(mtype) \
00040   (((mtype) == M_SPARC && bfd_lookup_arch (bfd_arch_sparc, 0) != NULL) \
00041    || (((mtype) == M_UNKNOWN || (mtype) == M_68010 || (mtype) == M_68020) \
00042        && bfd_lookup_arch (bfd_arch_m68k, 0) != NULL))
00043 #endif
00044 
00045 /* The file @code{aoutf1.h} contains the code for BFD's
00046    a.out back end.  Control over the generated back end is given by these
00047    two preprocessor names:
00048    @table @code
00049    @item ARCH_SIZE
00050    This value should be either 32 or 64, depending upon the size of an
00051    int in the target format. It changes the sizes of the structs which
00052    perform the memory/disk mapping of structures.
00053 
00054    The 64 bit backend may only be used if the host compiler supports 64
00055    ints (eg long long with gcc), by defining the name @code{BFD_HOST_64_BIT} in @code{bfd.h}.
00056    With this name defined, @emph{all} bfd operations are performed with 64bit
00057    arithmetic, not just those to a 64bit target.
00058 
00059    @item TARGETNAME
00060    The name put into the target vector.
00061    @item
00062    @end table.  */
00063 
00064 #if ARCH_SIZE == 64
00065 #define sunos_set_arch_mach sunos_64_set_arch_mach
00066 #define sunos_write_object_contents aout_64_sunos4_write_object_contents
00067 #else
00068 #define sunos_set_arch_mach sunos_32_set_arch_mach
00069 #define sunos_write_object_contents aout_32_sunos4_write_object_contents
00070 #endif
00071 
00072 /* Merge backend data into the output file.
00073    This is necessary on sparclet-aout where we want the resultant machine
00074    number to be M_SPARCLET if any input file is M_SPARCLET.  */
00075 
00076 #define MY_bfd_merge_private_bfd_data sunos_merge_private_bfd_data
00077 
00078 static bfd_boolean
00079 sunos_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
00080 {
00081   if (bfd_get_flavour (ibfd) != bfd_target_aout_flavour
00082       || bfd_get_flavour (obfd) != bfd_target_aout_flavour)
00083     return TRUE;
00084 
00085   if (bfd_get_arch (obfd) == bfd_arch_sparc)
00086     {
00087       if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
00088        bfd_set_arch_mach (obfd, bfd_arch_sparc, bfd_get_mach (ibfd));
00089     }
00090 
00091   return TRUE;
00092 }
00093 
00094 /* This is either sunos_32_set_arch_mach or sunos_64_set_arch_mach,
00095    depending upon ARCH_SIZE.  */
00096 
00097 static void
00098 sunos_set_arch_mach (bfd *abfd, enum machine_type machtype)
00099 {
00100   /* Determine the architecture and machine type of the object file.  */
00101   enum bfd_architecture arch;
00102   unsigned long machine;
00103 
00104   switch (machtype)
00105     {
00106     case M_UNKNOWN:
00107       /* Some Sun3s make magic numbers without cpu types in them, so
00108         we'll default to the 68000.  */
00109       arch = bfd_arch_m68k;
00110       machine = bfd_mach_m68000;
00111       break;
00112 
00113     case M_68010:
00114     case M_HP200:
00115       arch = bfd_arch_m68k;
00116       machine = bfd_mach_m68010;
00117       break;
00118 
00119     case M_68020:
00120     case M_HP300:
00121       arch = bfd_arch_m68k;
00122       machine = bfd_mach_m68020;
00123       break;
00124 
00125     case M_SPARC:
00126       arch = bfd_arch_sparc;
00127       machine = 0;
00128       break;
00129 
00130     case M_SPARCLET:
00131       arch = bfd_arch_sparc;
00132       machine = bfd_mach_sparc_sparclet;
00133       break;
00134 
00135     case M_SPARCLITE_LE:
00136       arch = bfd_arch_sparc;
00137       machine = bfd_mach_sparc_sparclite_le;
00138       break;
00139 
00140     case M_386:
00141     case M_386_DYNIX:
00142       arch = bfd_arch_i386;
00143       machine = 0;
00144       break;
00145 
00146     case M_HPUX:
00147       arch = bfd_arch_m68k;
00148       machine = 0;
00149       break;
00150 
00151     default:
00152       arch = bfd_arch_obscure;
00153       machine = 0;
00154       break;
00155     }
00156   bfd_set_arch_mach (abfd, arch, machine);
00157 }
00158 
00159 #define SET_ARCH_MACH(ABFD, EXEC) \
00160   NAME(sunos,set_arch_mach) (ABFD, N_MACHTYPE (EXEC)); \
00161   choose_reloc_size(ABFD);
00162 
00163 /* Determine the size of a relocation entry, based on the architecture.  */
00164 
00165 static void
00166 choose_reloc_size (bfd *abfd)
00167 {
00168   switch (bfd_get_arch (abfd))
00169     {
00170     case bfd_arch_sparc:
00171       obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
00172       break;
00173     default:
00174       obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
00175       break;
00176     }
00177 }
00178 
00179 /* Write an object file in SunOS format.  Section contents have
00180    already been written.  We write the file header, symbols, and
00181    relocation.  The real name of this function is either
00182    aout_64_sunos4_write_object_contents or
00183    aout_32_sunos4_write_object_contents, depending upon ARCH_SIZE.  */
00184 
00185 static bfd_boolean
00186 sunos_write_object_contents (bfd *abfd)
00187 {
00188   struct external_exec exec_bytes;
00189   struct internal_exec *execp = exec_hdr (abfd);
00190 
00191   /* Magic number, maestro, please!  */
00192   switch (bfd_get_arch (abfd))
00193     {
00194     case bfd_arch_m68k:
00195       switch (bfd_get_mach (abfd))
00196        {
00197        case bfd_mach_m68000:
00198          N_SET_MACHTYPE (*execp, M_UNKNOWN);
00199          break;
00200        case bfd_mach_m68010:
00201          N_SET_MACHTYPE (*execp, M_68010);
00202          break;
00203        default:
00204        case bfd_mach_m68020:
00205          N_SET_MACHTYPE (*execp, M_68020);
00206          break;
00207        }
00208       break;
00209     case bfd_arch_sparc:
00210       switch (bfd_get_mach (abfd))
00211        {
00212        case bfd_mach_sparc_sparclet:
00213          N_SET_MACHTYPE (*execp, M_SPARCLET);
00214          break;
00215        case bfd_mach_sparc_sparclite_le:
00216          N_SET_MACHTYPE (*execp, M_SPARCLITE_LE);
00217          break;
00218        default:
00219          N_SET_MACHTYPE (*execp, M_SPARC);
00220          break;
00221        }
00222       break;
00223     case bfd_arch_i386:
00224       N_SET_MACHTYPE (*execp, M_386);
00225       break;
00226     default:
00227       N_SET_MACHTYPE (*execp, M_UNKNOWN);
00228     }
00229 
00230   choose_reloc_size (abfd);
00231 
00232   N_SET_FLAGS (*execp, aout_backend_info (abfd)->exec_hdr_flags);
00233 
00234   N_SET_DYNAMIC (*execp, (long)(bfd_get_file_flags (abfd) & DYNAMIC));
00235 
00236   WRITE_HEADERS (abfd, execp);
00237 
00238   return TRUE;
00239 }
00240 
00241 /* Core files.  */
00242 
00243 #define CORE_MAGIC   0x080456
00244 #define CORE_NAMELEN 16
00245 
00246 /* The core structure is taken from the Sun documentation.
00247   Unfortunately, they don't document the FPA structure, or at least I
00248   can't find it easily.  Fortunately the core header contains its own
00249   length.  So this shouldn't cause problems, except for c_ucode, which
00250   so far we don't use but is easy to find with a little arithmetic.  */
00251 
00252 /* But the reg structure can be gotten from the SPARC processor handbook.
00253   This really should be in a GNU include file though so that gdb can use
00254   the same info.  */
00255 struct regs
00256 {
00257   int r_psr;
00258   int r_pc;
00259   int r_npc;
00260   int r_y;
00261   int r_g1;
00262   int r_g2;
00263   int r_g3;
00264   int r_g4;
00265   int r_g5;
00266   int r_g6;
00267   int r_g7;
00268   int r_o0;
00269   int r_o1;
00270   int r_o2;
00271   int r_o3;
00272   int r_o4;
00273   int r_o5;
00274   int r_o6;
00275   int r_o7;
00276 };
00277 
00278 /* Taken from Sun documentation: */
00279 
00280 /* FIXME:  It's worse than we expect.  This struct contains TWO substructs
00281   neither of whose size we know, WITH STUFF IN BETWEEN THEM!  We can't
00282   even portably access the stuff in between!  */
00283 
00284 struct external_sparc_core
00285 {
00286   int c_magic;                            /* Corefile magic number.  */
00287   int c_len;                       /* Sizeof (struct core).  */
00288 #define       SPARC_CORE_LEN       432
00289   struct regs c_regs;                     /* General purpose registers -- MACHDEP SIZE.  */
00290   struct external_exec c_aouthdr;  /* A.out header.  */
00291   int c_signo;                            /* Killing signal, if any.  */
00292   int c_tsize;                            /* Text size (bytes).  */
00293   int c_dsize;                            /* Data size (bytes).  */
00294   int c_ssize;                            /* Stack size (bytes).  */
00295   char c_cmdname[CORE_NAMELEN + 1];       /* Command name.  */
00296   double fp_stuff[1];                     /* External FPU state (size unknown by us).  */
00297   /* The type "double" is critical here, for alignment.
00298      SunOS declares a struct here, but the struct's
00299      alignment is double since it contains doubles.  */
00300   int c_ucode;                            /* Exception no. from u_code.  */
00301   /* This member is not accessible by name since
00302      we don't portably know the size of fp_stuff.  */
00303 };
00304 
00305 /* Core files generated by the BCP (the part of Solaris which allows
00306    it to run SunOS4 a.out files).  */
00307 struct external_solaris_bcp_core
00308 {
00309   int c_magic;                            /* Corefile magic number.  */
00310   int c_len;                       /* Sizeof (struct core).  */
00311 #define       SOLARIS_BCP_CORE_LEN 456
00312   struct regs c_regs;                     /* General purpose registers -- MACHDEP SIZE.  */
00313   int c_exdata_vp;                 /* Exdata structure.  */
00314   int c_exdata_tsize;
00315   int c_exdata_dsize;
00316   int c_exdata_bsize;
00317   int c_exdata_lsize;
00318   int c_exdata_nshlibs;
00319   short c_exdata_mach;
00320   short c_exdata_mag;
00321   int c_exdata_toffset;
00322   int c_exdata_doffset;
00323   int c_exdata_loffset;
00324   int c_exdata_txtorg;
00325   int c_exdata_datorg;
00326   int c_exdata_entloc;
00327   int c_signo;                            /* Killing signal, if any.  */
00328   int c_tsize;                            /* Text size (bytes).  */
00329   int c_dsize;                            /* Data size (bytes).  */
00330   int c_ssize;                            /* Stack size (bytes).  */
00331   char c_cmdname[CORE_NAMELEN + 1];       /* Command name.  */
00332   double fp_stuff[1];                     /* External FPU state (size unknown by us).  */
00333   /* The type "double" is critical here, for alignment.
00334      SunOS declares a struct here, but the struct's
00335      alignment is double since it contains doubles.  */
00336   int c_ucode;                            /* Exception no. from u_code.  */
00337   /* This member is not accessible by name since
00338      we don't portably know the size of fp_stuff.  */
00339 };
00340 
00341 struct external_sun3_core
00342 {
00343   int c_magic;                            /* Corefile magic number.  */
00344   int c_len;                       /* Sizeof (struct core).  */
00345 #define       SUN3_CORE_LEN 826           /* As of SunOS 4.1.1.  */
00346   int c_regs[18];                  /* General purpose registers -- MACHDEP SIZE.  */
00347   struct external_exec c_aouthdr;  /* A.out header.  */
00348   int c_signo;                            /* Killing signal, if any.  */
00349   int c_tsize;                            /* Text size (bytes).  */
00350   int c_dsize;                            /* Data size (bytes).  */
00351   int c_ssize;                            /* Stack size (bytes).  */
00352   char c_cmdname[CORE_NAMELEN + 1];       /* Command name.  */
00353   double fp_stuff[1];                     /* External FPU state (size unknown by us).  */
00354   /* The type "double" is critical here, for alignment.
00355      SunOS declares a struct here, but the struct's
00356      alignment is double since it contains doubles.  */
00357   int c_ucode;                            /* Exception no. from u_code.  */
00358   /* This member is not accessible by name since
00359      we don't portably know the size of fp_stuff.  */
00360 };
00361 
00362 struct internal_sunos_core
00363 {
00364   int c_magic;                            /* Corefile magic number.  */
00365   int c_len;                       /* Sizeof (struct core).  */
00366   long c_regs_pos;                 /* File offset of General purpose registers.  */
00367   int c_regs_size;                 /* Size of General purpose registers.  */
00368   struct internal_exec c_aouthdr;  /* A.out header.  */
00369   int c_signo;                            /* Killing signal, if any.  */
00370   int c_tsize;                            /* Text size (bytes).  */
00371   int c_dsize;                            /* Data size (bytes).  */
00372   bfd_vma c_data_addr;                    /* Data start (address).  */
00373   int c_ssize;                            /* Stack size (bytes).  */
00374   bfd_vma c_stacktop;                     /* Stack top (address).  */
00375   char c_cmdname[CORE_NAMELEN + 1];       /* Command name.  */
00376   long fp_stuff_pos;               /* File offset of external FPU state (regs).  */
00377   int fp_stuff_size;               /* Size of it.  */
00378   int c_ucode;                            /* Exception no. from u_code.  */
00379 };
00380 
00381 /* Byte-swap in the Sun-3 core structure.  */
00382 
00383 static void
00384 swapcore_sun3 (bfd *abfd, char *ext, struct internal_sunos_core *intcore)
00385 {
00386   struct external_sun3_core *extcore = (struct external_sun3_core *) ext;
00387 
00388   intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
00389   intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
00390   intcore->c_regs_pos = offsetof (struct external_sun3_core, c_regs);
00391   intcore->c_regs_size = sizeof (extcore->c_regs);
00392 #if ARCH_SIZE == 64
00393   aout_64_swap_exec_header_in
00394 #else
00395   aout_32_swap_exec_header_in
00396 #endif
00397     (abfd, &extcore->c_aouthdr, &intcore->c_aouthdr);
00398   intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
00399   intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
00400   intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
00401   intcore->c_data_addr = N_DATADDR (intcore->c_aouthdr);
00402   intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
00403   memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
00404   intcore->fp_stuff_pos = offsetof (struct external_sun3_core, fp_stuff);
00405   /* FP stuff takes up whole rest of struct, except c_ucode.  */
00406   intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
00407     offsetof (struct external_sun3_core, fp_stuff);
00408   /* Ucode is the last thing in the struct -- just before the end.  */
00409   intcore->c_ucode = H_GET_32 (abfd,
00410                             (intcore->c_len
00411                             - sizeof (extcore->c_ucode)
00412                             + (unsigned char *) extcore));
00413   intcore->c_stacktop = 0x0E000000;       /* By experimentation.  */
00414 }
00415 
00416 /* Byte-swap in the Sparc core structure.  */
00417 
00418 static void
00419 swapcore_sparc (bfd *abfd, char *ext, struct internal_sunos_core *intcore)
00420 {
00421   struct external_sparc_core *extcore = (struct external_sparc_core *) ext;
00422 
00423   intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
00424   intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
00425   intcore->c_regs_pos = offsetof (struct external_sparc_core, c_regs);
00426   intcore->c_regs_size = sizeof (extcore->c_regs);
00427 #if ARCH_SIZE == 64
00428   aout_64_swap_exec_header_in
00429 #else
00430   aout_32_swap_exec_header_in
00431 #endif
00432     (abfd, &extcore->c_aouthdr, &intcore->c_aouthdr);
00433   intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
00434   intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
00435   intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
00436   intcore->c_data_addr = N_DATADDR (intcore->c_aouthdr);
00437   intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
00438   memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
00439   intcore->fp_stuff_pos = offsetof (struct external_sparc_core, fp_stuff);
00440   /* FP stuff takes up whole rest of struct, except c_ucode.  */
00441   intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
00442     offsetof (struct external_sparc_core, fp_stuff);
00443   /* Ucode is the last thing in the struct -- just before the end.  */
00444   intcore->c_ucode = H_GET_32 (abfd,
00445                             (intcore->c_len
00446                             - sizeof (extcore->c_ucode)
00447                             + (unsigned char *) extcore));
00448 
00449   /* Supposedly the user stack grows downward from the bottom of kernel memory.
00450      Presuming that this remains true, this definition will work.  */
00451   /* Now sun has provided us with another challenge.  The value is different
00452      for sparc2 and sparc10 (both running SunOS 4.1.3).  We pick one or
00453      the other based on the current value of the stack pointer.  This
00454      loses (a) if the stack pointer has been clobbered, or (b) if the stack
00455      is larger than 128 megabytes.
00456 
00457      It's times like these you're glad they're switching to ELF.
00458 
00459      Note that using include files or nlist on /vmunix would be wrong,
00460      because we want the value for this core file, no matter what kind of
00461      machine we were compiled on or are running on.  */
00462 #define SPARC_USRSTACK_SPARC2 ((bfd_vma)0xf8000000)
00463 #define SPARC_USRSTACK_SPARC10 ((bfd_vma)0xf0000000)
00464   {
00465     bfd_vma sp = H_GET_32 (abfd, &extcore->c_regs.r_o6);
00466     if (sp < SPARC_USRSTACK_SPARC10)
00467       intcore->c_stacktop = SPARC_USRSTACK_SPARC10;
00468     else
00469       intcore->c_stacktop = SPARC_USRSTACK_SPARC2;
00470   }
00471 }
00472 
00473 /* Byte-swap in the Solaris BCP core structure.  */
00474 
00475 static void
00476 swapcore_solaris_bcp (bfd *abfd, char *ext, struct internal_sunos_core *intcore)
00477 {
00478   struct external_solaris_bcp_core *extcore =
00479     (struct external_solaris_bcp_core *) ext;
00480 
00481   intcore->c_magic = H_GET_32 (abfd, &extcore->c_magic);
00482   intcore->c_len = H_GET_32 (abfd, &extcore->c_len);
00483   intcore->c_regs_pos = offsetof (struct external_solaris_bcp_core, c_regs);
00484   intcore->c_regs_size = sizeof (extcore->c_regs);
00485 
00486   /* The Solaris BCP exdata structure does not contain an a_syms field,
00487      so we are unable to synthesize an internal exec header.
00488      Luckily we are able to figure out the start address of the data section,
00489      which is the only thing needed from the internal exec header,
00490      from the exdata structure.
00491 
00492      As of Solaris 2.3, BCP core files for statically linked executables
00493      are buggy. The exdata structure is not properly filled in, and
00494      the data section is written from address zero instead of the data
00495      start address.  */
00496   memset ((void *) &intcore->c_aouthdr, 0, sizeof (struct internal_exec));
00497   intcore->c_data_addr = H_GET_32 (abfd, &extcore->c_exdata_datorg);
00498   intcore->c_signo = H_GET_32 (abfd, &extcore->c_signo);
00499   intcore->c_tsize = H_GET_32 (abfd, &extcore->c_tsize);
00500   intcore->c_dsize = H_GET_32 (abfd, &extcore->c_dsize);
00501   intcore->c_ssize = H_GET_32 (abfd, &extcore->c_ssize);
00502   memcpy (intcore->c_cmdname, extcore->c_cmdname, sizeof (intcore->c_cmdname));
00503   intcore->fp_stuff_pos =
00504     offsetof (struct external_solaris_bcp_core, fp_stuff);
00505   /* FP stuff takes up whole rest of struct, except c_ucode.  */
00506   intcore->fp_stuff_size = intcore->c_len - (sizeof extcore->c_ucode) -
00507     offsetof (struct external_solaris_bcp_core, fp_stuff);
00508   /* Ucode is the last thing in the struct -- just before the end */
00509   intcore->c_ucode = H_GET_32 (abfd,
00510                             (intcore->c_len
00511                             - sizeof (extcore->c_ucode)
00512                             + (unsigned char *) extcore));
00513 
00514   /* Supposedly the user stack grows downward from the bottom of kernel memory.
00515      Presuming that this remains true, this definition will work.  */
00516   /* Now sun has provided us with another challenge.  The value is different
00517      for sparc2 and sparc10 (both running SunOS 4.1.3).  We pick one or
00518      the other based on the current value of the stack pointer.  This
00519      loses (a) if the stack pointer has been clobbered, or (b) if the stack
00520      is larger than 128 megabytes.
00521 
00522      It's times like these you're glad they're switching to ELF.
00523 
00524      Note that using include files or nlist on /vmunix would be wrong,
00525      because we want the value for this core file, no matter what kind of
00526      machine we were compiled on or are running on.  */
00527 #define SPARC_USRSTACK_SPARC2 ((bfd_vma)0xf8000000)
00528 #define SPARC_USRSTACK_SPARC10 ((bfd_vma)0xf0000000)
00529   {
00530     bfd_vma sp = H_GET_32 (abfd, &extcore->c_regs.r_o6);
00531     if (sp < SPARC_USRSTACK_SPARC10)
00532       intcore->c_stacktop = SPARC_USRSTACK_SPARC10;
00533     else
00534       intcore->c_stacktop = SPARC_USRSTACK_SPARC2;
00535   }
00536 }
00537 
00538 /* Need this cast because ptr is really void *.  */
00539 #define core_hdr(bfd)      ((bfd)->tdata.sun_core_data)
00540 #define core_datasec(bfd)  (core_hdr (bfd)->data_section)
00541 #define core_stacksec(bfd) (core_hdr (bfd)->stack_section)
00542 #define core_regsec(bfd)   (core_hdr (bfd)->reg_section)
00543 #define core_reg2sec(bfd)  (core_hdr (bfd)->reg2_section)
00544 
00545 /* These are stored in the bfd's tdata.  */
00546 struct sun_core_struct
00547 {
00548   struct internal_sunos_core *hdr; /* Core file header.  */
00549   asection *data_section;
00550   asection *stack_section;
00551   asection *reg_section;
00552   asection *reg2_section;
00553 };
00554 
00555 static const bfd_target *
00556 sunos4_core_file_p (bfd *abfd)
00557 {
00558   unsigned char longbuf[4]; /* Raw bytes of various header fields.  */
00559   bfd_size_type core_size, amt;
00560   unsigned long core_mag;
00561   struct internal_sunos_core *core;
00562   char *extcore;
00563   struct mergem
00564     {
00565       struct sun_core_struct suncoredata;
00566       struct internal_sunos_core internal_sunos_core;
00567       char external_core[1];
00568     } *mergem;
00569   flagword flags;
00570 
00571   if (bfd_bread ((void *) longbuf, (bfd_size_type) sizeof (longbuf), abfd)
00572       != sizeof (longbuf))
00573     return NULL;
00574   core_mag = H_GET_32 (abfd, longbuf);
00575 
00576   if (core_mag != CORE_MAGIC)
00577     return NULL;
00578 
00579   /* SunOS core headers can vary in length; second word is size; */
00580   if (bfd_bread ((void *) longbuf, (bfd_size_type) sizeof (longbuf), abfd)
00581       != sizeof (longbuf))
00582     return NULL;
00583   core_size = H_GET_32 (abfd, longbuf);
00584   /* Sanity check.  */
00585   if (core_size > 20000)
00586     return NULL;
00587 
00588   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
00589     return NULL;
00590 
00591   amt = core_size + sizeof (struct mergem);
00592   mergem = bfd_zalloc (abfd, amt);
00593   if (mergem == NULL)
00594     return NULL;
00595 
00596   extcore = mergem->external_core;
00597 
00598   if ((bfd_bread ((void *) extcore, core_size, abfd)) != core_size)
00599     {
00600     loser:
00601       bfd_release (abfd, (char *) mergem);
00602       abfd->tdata.any = NULL;
00603       bfd_section_list_clear (abfd);
00604       return NULL;
00605     }
00606 
00607   /* Validate that it's a core file we know how to handle, due to sun
00608      botching the positioning of registers and other fields in a machine
00609      dependent way.  */
00610   core = &mergem->internal_sunos_core;
00611   switch (core_size)
00612     {
00613     case SPARC_CORE_LEN:
00614       swapcore_sparc (abfd, extcore, core);
00615       break;
00616     case SUN3_CORE_LEN:
00617       swapcore_sun3 (abfd, extcore, core);
00618       break;
00619     case SOLARIS_BCP_CORE_LEN:
00620       swapcore_solaris_bcp (abfd, extcore, core);
00621       break;
00622     default:
00623       bfd_set_error (bfd_error_system_call);     /* FIXME.  */
00624       goto loser;
00625     }
00626 
00627   abfd->tdata.sun_core_data = &mergem->suncoredata;
00628   abfd->tdata.sun_core_data->hdr = core;
00629 
00630   /* Create the sections.  */
00631   flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
00632   core_stacksec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".stack",
00633                                                       flags);
00634   if (core_stacksec (abfd) == NULL)
00635     /* bfd_release frees everything allocated after it's arg.  */
00636     goto loser;
00637 
00638   flags = SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS;
00639   core_datasec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".data",
00640                                                      flags);
00641   if (core_datasec (abfd) == NULL)
00642     goto loser;
00643 
00644   flags = SEC_HAS_CONTENTS;
00645   core_regsec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg",
00646                                                     flags);
00647   if (core_regsec (abfd) == NULL)
00648     goto loser;
00649 
00650   flags = SEC_HAS_CONTENTS;
00651   core_reg2sec (abfd) = bfd_make_section_anyway_with_flags (abfd, ".reg2",
00652                                                      flags);
00653   if (core_reg2sec (abfd) == NULL)
00654     goto loser;
00655 
00656   core_stacksec (abfd)->size = core->c_ssize;
00657   core_datasec (abfd)->size = core->c_dsize;
00658   core_regsec (abfd)->size = core->c_regs_size;
00659   core_reg2sec (abfd)->size = core->fp_stuff_size;
00660 
00661   core_stacksec (abfd)->vma = (core->c_stacktop - core->c_ssize);
00662   core_datasec (abfd)->vma = core->c_data_addr;
00663   core_regsec (abfd)->vma = 0;
00664   core_reg2sec (abfd)->vma = 0;
00665 
00666   core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
00667   core_datasec (abfd)->filepos = core->c_len;
00668   /* We'll access the regs afresh in the core file, like any section:  */
00669   core_regsec (abfd)->filepos = (file_ptr) core->c_regs_pos;
00670   core_reg2sec (abfd)->filepos = (file_ptr) core->fp_stuff_pos;
00671 
00672   /* Align to word at least.  */
00673   core_stacksec (abfd)->alignment_power = 2;
00674   core_datasec (abfd)->alignment_power = 2;
00675   core_regsec (abfd)->alignment_power = 2;
00676   core_reg2sec (abfd)->alignment_power = 2;
00677 
00678   return abfd->xvec;
00679 }
00680 
00681 static char *
00682 sunos4_core_file_failing_command (bfd *abfd)
00683 {
00684   return core_hdr (abfd)->hdr->c_cmdname;
00685 }
00686 
00687 static int
00688 sunos4_core_file_failing_signal (bfd *abfd)
00689 {
00690   return core_hdr (abfd)->hdr->c_signo;
00691 }
00692 
00693 static bfd_boolean
00694 sunos4_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
00695 {
00696   if (core_bfd->xvec != exec_bfd->xvec)
00697     {
00698       bfd_set_error (bfd_error_system_call);
00699       return FALSE;
00700     }
00701 
00702   /* Solaris core files do not include an aouthdr.  */
00703   if ((core_hdr (core_bfd)->hdr)->c_len == SOLARIS_BCP_CORE_LEN)
00704     return TRUE;
00705 
00706   return memcmp ((char *) &((core_hdr (core_bfd)->hdr)->c_aouthdr),
00707                (char *) exec_hdr (exec_bfd),
00708                sizeof (struct internal_exec)) == 0;
00709 }
00710 
00711 #define MY_set_sizes sunos4_set_sizes
00712 
00713 static bfd_boolean
00714 sunos4_set_sizes (bfd *abfd)
00715 {
00716   switch (bfd_get_arch (abfd))
00717     {
00718     default:
00719       return FALSE;
00720     case bfd_arch_sparc:
00721       adata (abfd).page_size = 0x2000;
00722       adata (abfd).segment_size = 0x2000;
00723       adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
00724       return TRUE;
00725     case bfd_arch_m68k:
00726       adata (abfd).page_size = 0x2000;
00727       adata (abfd).segment_size = 0x20000;
00728       adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
00729       return TRUE;
00730     }
00731 }
00732 
00733 /* We default to setting the toolversion field to 1, as is required by
00734    SunOS.  */
00735 #ifndef MY_exec_hdr_flags
00736 #define MY_exec_hdr_flags 1
00737 #endif
00738 
00739 #ifndef MY_entry_is_text_address
00740 #define MY_entry_is_text_address 0
00741 #endif
00742 #ifndef MY_add_dynamic_symbols
00743 #define MY_add_dynamic_symbols 0
00744 #endif
00745 #ifndef MY_add_one_symbol
00746 #define MY_add_one_symbol 0
00747 #endif
00748 #ifndef MY_link_dynamic_object
00749 #define MY_link_dynamic_object 0
00750 #endif
00751 #ifndef MY_write_dynamic_symbol
00752 #define MY_write_dynamic_symbol 0
00753 #endif
00754 #ifndef MY_check_dynamic_reloc
00755 #define MY_check_dynamic_reloc 0
00756 #endif
00757 #ifndef MY_finish_dynamic_link
00758 #define MY_finish_dynamic_link 0
00759 #endif
00760 
00761 static const struct aout_backend_data sunos4_aout_backend =
00762 {
00763   0,                        /* Zmagic files are not contiguous.  */
00764   1,                        /* Text includes header.  */
00765   MY_entry_is_text_address,
00766   MY_exec_hdr_flags,
00767   0,                        /* Default text vma.  */
00768   sunos4_set_sizes,
00769   0,                        /* Header is counted in zmagic text.  */
00770   MY_add_dynamic_symbols,
00771   MY_add_one_symbol,
00772   MY_link_dynamic_object,
00773   MY_write_dynamic_symbol,
00774   MY_check_dynamic_reloc,
00775   MY_finish_dynamic_link
00776 };
00777 
00778 #define       MY_core_file_failing_command       sunos4_core_file_failing_command
00779 #define       MY_core_file_failing_signal sunos4_core_file_failing_signal
00780 #define       MY_core_file_matches_executable_p sunos4_core_file_matches_executable_p
00781 
00782 #define MY_bfd_debug_info_start           bfd_void
00783 #define MY_bfd_debug_info_end             bfd_void
00784 #define MY_bfd_debug_info_accumulate      (void (*) (bfd *, struct bfd_section *)) bfd_void
00785 #define MY_core_file_p                    sunos4_core_file_p
00786 #define MY_write_object_contents   NAME(aout, sunos4_write_object_contents)
00787 #define MY_backend_data                   & sunos4_aout_backend
00788 
00789 #ifndef TARGET_IS_LITTLE_ENDIAN_P
00790 #define TARGET_IS_BIG_ENDIAN_P
00791 #endif
00792 
00793 #include "aout-target.h"