home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v92.tgz / v92.tar / v92 / src / common / save.c < prev    next >
C/C++ Source or Header  |  1996-03-22  |  9KB  |  337 lines

  1. /*
  2.  * save(s) -- for systems that support ExecImages
  3.  */
  4.  
  5. #include "::h:gsupport.h"
  6.  
  7. #ifdef ExecImages
  8.  
  9. /*
  10.  * save(s) -- for the Convex.
  11.  */
  12.  
  13. #ifdef CONVEX
  14. #define TEXT0 0x80001000        /* address of first .text page */
  15. #define DATA0 ((int) &environ & -4096)    /* address of first .data page */
  16. #define START TEXT0            /* start address */
  17.  
  18. #include <convex/filehdr.h>
  19. #include <convex/opthdr.h>
  20. #include <convex/scnhdr.h>
  21.  
  22. extern char environ;
  23.  
  24. wrtexec (ef)
  25. int ef;
  26. {
  27.     struct filehdr filehdr;
  28.     struct opthdr opthdr;
  29.     struct scnhdr texthdr;
  30.     struct scnhdr datahdr;
  31.  
  32.     int foffs = 0;
  33.     int ooffs = foffs + sizeof filehdr;
  34.     int toffs = ooffs + sizeof opthdr;
  35.     int doffs = toffs + sizeof texthdr;
  36.  
  37.     int tsize = DATA0 - TEXT0;
  38.     int dsize = (sbrk (0) - DATA0 + 4095) & -4096;
  39.  
  40.     bzero (&filehdr, sizeof filehdr);
  41.     bzero (&opthdr, sizeof opthdr);
  42.     bzero (&texthdr, sizeof texthdr);
  43.     bzero (&datahdr, sizeof datahdr);
  44.     
  45.     filehdr.h_magic = SOFF_MAGIC;
  46.     filehdr.h_nscns = 2;
  47.     filehdr.h_scnptr = toffs;
  48.     filehdr.h_opthdr = sizeof opthdr;
  49.  
  50.     opthdr.o_entry = START;
  51.     opthdr.o_flags = OF_EXEC | OF_STRIPPED;
  52.  
  53.     texthdr.s_vaddr = TEXT0;
  54.     texthdr.s_size = tsize;
  55.     texthdr.s_scnptr = 0x1000;
  56.     texthdr.s_prot = VM_PG_R | VM_PG_E;
  57.     texthdr.s_flags = S_TEXT;
  58.  
  59.     datahdr.s_vaddr = DATA0;
  60.     datahdr.s_size = dsize;
  61.     datahdr.s_scnptr = 0x1000 + tsize;
  62.     datahdr.s_prot = VM_PG_R | VM_PG_W;
  63.     datahdr.s_flags = S_DATA;
  64.  
  65.     write (ef, &filehdr, sizeof filehdr);
  66.     write (ef, &opthdr, sizeof opthdr);
  67.     write (ef, &texthdr, sizeof texthdr);
  68.     write (ef, &datahdr, sizeof datahdr);
  69.     lseek (ef, 0x1000, 0);
  70.     write (ef, TEXT0, tsize + dsize);
  71.     close (ef);
  72.  
  73.     return dsize;
  74. }
  75. #endif                    /* CONVEX */
  76.  
  77. /*
  78.  * save(s) -- for generic BSD systems.
  79.  */
  80.  
  81. #ifdef GenericBSD
  82. #include <a.out.h>
  83. wrtexec(ef)
  84. int ef;
  85. {
  86.    struct exec hdr;
  87.    extern environ, etext;
  88.    int tsize, dsize;
  89.  
  90.    /*
  91.     * Construct the header.  The text and data region sizes must be multiples
  92.     *  of 1024.
  93.     */
  94.    hdr.a_magic = ZMAGIC;
  95.    tsize = (int)&etext;
  96.    hdr.a_text = (tsize + 1024) & ~(1024-1);
  97.    dsize = sbrk(0) - (int)&environ;
  98.    hdr.a_data = (dsize + 1024) & ~(1024-1);
  99.    hdr.a_bss = 0;
  100.    hdr.a_syms = 0;
  101.    hdr.a_entry = 0;
  102.    hdr.a_trsize = 0;
  103.    hdr.a_drsize = 0;
  104.  
  105.    /*
  106.     * Write the header.
  107.     */
  108.    write(ef, &hdr, sizeof(hdr));
  109.  
  110.    /*
  111.     * Write the text, starting at N_TXTOFF.
  112.     */
  113.    lseek(ef, N_TXTOFF(hdr), 0);
  114.    write(ef, 0, tsize);
  115.    lseek(ef, hdr.a_text - tsize, 1);
  116.  
  117.    /*
  118.     * Write the data.
  119.     */
  120.    write(ef, &environ, dsize);
  121.    lseek(ef, hdr.a_data - dsize, 1);
  122.    close(ef);
  123.    return hdr.a_data;
  124. }
  125. #endif                    /* GenericBSD */
  126.  
  127. /*
  128.  * save(s) -- for the Encore.
  129. */
  130.  
  131. #ifdef MULTIMAX
  132.  
  133. #include <time.h>
  134. #include <a.out.h>
  135. #include <sys/file.h>
  136. #include <sgs.h>
  137.  
  138. #define NUMSECS        2        /* Two sections in the image. */
  139. #define TEXTSTART    0        /* Text starts at address 0. */
  140. #define MODSTART    0x20        /* Depends on crt0.s */
  141. #define MODSIZE        0x10        /* Depends on crt0.s */
  142. #define IMAGEPAGE    4096        /* Page size for images.  Found it */
  143.                                         /* with aoutdump(1) */
  144. #define HDRSIZE        (sizeof(struct filehdr)+sizeof(struct aouthdr)+ \
  145.              NUMSECS*sizeof(struct scnhdr))
  146. #define PAGEROUND(x)    (((x+IMAGEPAGE-1)/IMAGEPAGE)*IMAGEPAGE)
  147.  
  148. extern etext;        /* ld(1) puts this at the end of the text segment. */
  149. extern environ;        /* ld(1) puts this at the start of the data segment. */
  150.  
  151. /*
  152.  * wrtexec() - save image in file.
  153.  */
  154. wrtexec(ExecFile)
  155.      int ExecFile;
  156. {
  157.   int Status;        /* For saving status codes. */
  158.  
  159.   /* Call internal wrtexec2() routine. */
  160.   Status=wrtexec2(ExecFile);
  161.  
  162.   /* Close the file. */
  163.   close(ExecFile);
  164.   
  165.   return Status;
  166. }
  167.  
  168. /*
  169.  * wrtexec2 - Code to write the image file.
  170.  */
  171. static
  172. wrtexec2(ExecFile)
  173.      int ExecFile;
  174. {
  175.   struct filehdr FileHeader;    /* File header record. */
  176.   struct aouthdr SystemHeader;    /* System header record. */
  177.   struct scnhdr SectionHeader;    /* Section header record. */
  178.  
  179.   struct timeval TV;        /* Time value. */
  180.   struct timezone TZ;        /* Time zone. */
  181.  
  182.   unsigned long TextStart;    /* Start of text. */
  183.   unsigned long TextSize;    /* Size of text. */
  184.   unsigned long TextFPtr;    /* Location of text in image file. */
  185.   unsigned long DataStart;    /* Start of data. */
  186.   unsigned long DataSize;    /* Size of data. */
  187.   unsigned long DataFPtr;    /* Location of data in image file. */
  188.  
  189.   /* Figure out a few things we need to know. */
  190.   TextStart = TEXTSTART;
  191.   TextSize = (unsigned long)&etext;
  192.   TextFPtr = PAGEROUND(HDRSIZE);
  193.  
  194.   DataStart = (unsigned long)&environ;
  195.   DataSize = sbrk(0)-DataStart;
  196.   DataFPtr = TextFPtr+PAGEROUND(TextSize);
  197.  
  198.   /* Write a file header. */
  199.   FileHeader.f_magic = NS32GMAGIC;        /* NS 32k executable. */
  200.   FileHeader.f_nscns = NUMSECS;            /* Three standard sections. */
  201.   gettimeofday(&TV,&TZ);
  202.   FileHeader.f_timdat = TV.tv_sec;        /* Time stamp. */
  203.   FileHeader.f_symptr = 0;            /* No symbols. */
  204.   FileHeader.f_nsyms = 0;            /* No symbols. */
  205.   FileHeader.f_opthdr = sizeof(struct aouthdr);    /* Size of system header. */
  206.   FileHeader.f_flags = F_RELFLG|F_EXEC|F_LNNO|F_LSYMS; /* Misc. Flags. */
  207.   if(write(ExecFile,&FileHeader,sizeof FileHeader)==-1)
  208.     return -1;
  209.  
  210.   /* Write a system header. */
  211.   SystemHeader.magic = PAGEMAGIC;        /* Normal executable. */
  212.   SystemHeader.vstamp = 0;            /* Ignore this. */
  213.   SystemHeader.tsize = TextSize;        /* Size of text segment. */
  214.   SystemHeader.dsize = DataSize;        /* Size of data segment. */
  215.   SystemHeader.bsize = 0;            /* No bss */
  216.   SystemHeader.msize = MODSIZE;            /* Magic from aoutdump(1). */
  217.   SystemHeader.mod_start = MODSTART;        /* Magic from aoutdump(1). */
  218.   SystemHeader.entry = 0x2;            /* Magic from aoutdump(1). */
  219.   SystemHeader.text_start = TextStart;        /* Magic from aoutdump(1). */
  220.   SystemHeader.data_start = DataStart;        /* Start of data segment. */
  221.   SystemHeader.entry_mod = 0;            /* Unused. */
  222.   SystemHeader.flags = U_SYS_42|U_AL_4096;    /* UMAX 4.2, 4k align. */
  223.   if(write(ExecFile,&SystemHeader,sizeof SystemHeader)==-1)
  224.     return -1;
  225.  
  226.   /* Write text section header. */
  227.   strcpy(SectionHeader.s_name,_TEXT);        /* Section name. */
  228.   SectionHeader.s_paddr = TextStart;        /* Physical address. */
  229.   SectionHeader.s_vaddr = TextStart;        /* Virtual address. */
  230.   SectionHeader.s_size = TextSize;        /* Section size. */
  231.   SectionHeader.s_scnptr = TextFPtr;        /* File ptr to section. */
  232.   SectionHeader.s_relptr = 0;            /* No relocation data. */
  233.   SectionHeader.s_lnnoptr = 0;            /* No line numbers. */
  234.   SectionHeader.s_nreloc = 0;            /* No relocation data. */
  235.   SectionHeader.s_nlnno = 0;            /* No line numbers. */
  236.   SectionHeader.s_flags = STYP_TEXT;        /* Text section. */
  237.   SectionHeader.s_symptr = 0;            /* No symbol data. */
  238.   SectionHeader.s_modno = 0;            /* Ignore this. */
  239.   SectionHeader.s_pad = 0;            /* Padding. */
  240.   if(write(ExecFile,&SectionHeader,sizeof SectionHeader)==-1)
  241.     return -1;
  242.  
  243.   /* Write data section header. */
  244.   strcpy(SectionHeader.s_name,_DATA);        /* Section name. */
  245.   SectionHeader.s_paddr = DataStart;        /* Physical address. */
  246.   SectionHeader.s_vaddr = DataStart;        /* Virtual address. */
  247.   SectionHeader.s_size = DataSize;        /* Section size. */
  248.   SectionHeader.s_scnptr = DataFPtr;        /* File ptr to section. */
  249.   SectionHeader.s_relptr = 0;            /* No relocation data. */
  250.   SectionHeader.s_lnnoptr = 0;            /* No line numbers. */
  251.   SectionHeader.s_nreloc = 0;            /* No relocation data. */
  252.   SectionHeader.s_nlnno = 0;            /* No line numbers. */
  253.   SectionHeader.s_flags = STYP_DATA;        /* Data section. */
  254.   SectionHeader.s_symptr = 0;            /* No symbol data. */
  255.   SectionHeader.s_modno = 0;            /* Ignore this. */
  256.   SectionHeader.s_pad = 0;            /* Padding. */
  257.   if(write(ExecFile,&SectionHeader,sizeof SectionHeader)==-1)
  258.     return -1;
  259.  
  260.   /* Write the text section. */
  261.   if(lseek(ExecFile,TextFPtr,L_SET)==-1)
  262.     return -1;
  263.   if(write(ExecFile,TextStart,TextSize)==-1)
  264.     return -1;
  265.  
  266.   /* Write the data section. */
  267.   if(lseek(ExecFile,DataFPtr,L_SET)==-1)
  268.      return -1;
  269.   if(write(ExecFile,DataStart,DataSize)==-1)
  270.     return -1;
  271.  
  272.   return DataSize;
  273. }
  274.  
  275. #endif                    /* MULTIMAX */
  276.  
  277. /*
  278.  * save(s) -- for Sun Workstations.
  279.  */
  280.  
  281. #ifdef SUN
  282. #include <a.out.h>
  283. wrtexec(ef)
  284. int ef;
  285. {
  286.    struct exec *hdrp, hdr;
  287.    extern environ, etext;
  288.    int tsize, dsize;
  289.  
  290.    hdrp = (struct exec *)PAGSIZ;
  291.     
  292.    /*
  293.     * This code only handles the ZMAGIC format...
  294.     */
  295.    if (hdrp->a_magic != ZMAGIC)
  296.       syserr("executable is not ZMAGIC format");
  297.    /*
  298.     * Construct the header by copying in the header in core and fixing
  299.     *  up values as necessary.
  300.     */
  301.    hdr = *hdrp;
  302.    tsize = (char *)&etext - (char *)hdrp;
  303.    hdr.a_text = (tsize + PAGSIZ) & ~(PAGSIZ-1);
  304.    dsize = sbrk(0) - (int)&environ;
  305.    hdr.a_data = (dsize + PAGSIZ) & ~(PAGSIZ-1);
  306.    hdr.a_bss = 0;
  307.    hdr.a_syms = 0;
  308.    hdr.a_trsize = 0;
  309.    hdr.a_drsize = 0;
  310.  
  311.    /*
  312.     * Write the text.
  313.     */
  314.    write(ef, hdrp, tsize);
  315.    lseek(ef, hdr.a_text, 0);
  316.  
  317.    /*
  318.     * Write the data.
  319.     */
  320.    write(ef, &environ, dsize);
  321.    lseek(ef, hdr.a_data - dsize, 1);
  322.  
  323.    /*
  324.     * Write the header.
  325.     */
  326.    lseek(ef, 0, 0);
  327.    write(ef, &hdr, sizeof(hdr));
  328.  
  329.    close(ef);
  330.    return hdr.a_data;
  331. }
  332. #endif                    /* SUN */
  333.  
  334. #else                    /* ExecImages */
  335. static char junk;
  336. #endif                    /* ExecImages */
  337.