home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / aoutf1.h < prev    next >
C/C++ Source or Header  |  1996-09-28  |  26KB  |  802 lines

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