home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / unexenix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-25  |  7.1 KB  |  255 lines

  1. /* Unexec for Xenix.
  2.    Copyright (C) 1988, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.28. */
  21.  
  22.  
  23.  
  24. /*
  25.   On 80386 Xenix, segmentation screws prevent us from modifying the text
  26.   segment at all.  We basically just plug a new value for "data segment
  27.   size" into the countless headers and copy the other records straight
  28.   through.  The data segment is ORG'ed at the xs_rbase value of the data
  29.   segment's xseg record (always @ 0x1880000, thanks to the "sophisticated
  30.   memory management hardware" of the chip) and extends to sbrk(0), exactly.
  31.   This code is afraid to malloc (should it be?), and alloca has to be the
  32.   wimpy, malloc-based version; consequently, data is usually copied in
  33.   smallish chunks.
  34.  
  35.   gb@entity.com
  36. */
  37.  
  38. #include <config.h>
  39. #include <sys/types.h>
  40. #include <fcntl.h>
  41. #include <sys/file.h>
  42. #include <sys/stat.h>
  43. #include <stdio.h>
  44. #include <varargs.h>
  45. #include <a.out.h>
  46.  
  47. static void fatal_unexec ();
  48.  
  49. #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
  50.     errno = EEOF; \
  51.     if (read(_fd, _buffer, _size) != _size) \
  52.       fatal_unexec(_error_message, _error_arg);
  53.  
  54. #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
  55.     if (write(_fd, _buffer, _size) != _size) \
  56.       fatal_unexec(_error_message, _error_arg);
  57.  
  58. #define SEEK(_fd, _position, _error_message, _error_arg) \
  59.     errno = EEOF; \
  60.     if (lseek(_fd, _position, L_SET) != _position) \
  61.       fatal_unexec(_error_message, _error_arg);
  62.  
  63. extern int errno;
  64. extern char *strerror ();
  65. #define EEOF -1
  66.  
  67. #ifndef L_SET
  68. #define L_SET 0
  69. #endif
  70.  
  71. /* Should check the magic number of the old executable;
  72.    not yet written.  */
  73. check_exec (x)
  74.      struct xexec *x;
  75. {
  76. }
  77.  
  78.  
  79. unexec (new_name, a_name, data_start, bss_start, entry_address)
  80.      char *new_name, *a_name;
  81.      unsigned data_start, bss_start, entry_address;
  82. {
  83.   char *sbrk (), *datalim = sbrk (0), *data_org;
  84.   long segpos, textseen, textpos, textlen, datapos, datadiff, datalen;
  85.  
  86.   struct xexec u_xexec,       /*  a.out header */
  87.               *u_xexecp = &u_xexec;
  88.   struct xext  u_xext,        /*  extended header */
  89.               *u_xextp  = &u_xext;
  90.   struct xseg  u_xseg,        /*  segment table entry */
  91.               *u_xsegp  = &u_xseg;
  92.   int i, nsegs, isdata = 0, infd, outfd;
  93.  
  94.   infd = open (a_name, O_RDONLY, 0);
  95.   if (infd < 0) fatal_unexec ("opening %s", a_name);
  96.  
  97.   outfd = creat (new_name, 0666);
  98.   if (outfd < 0) fatal_unexec ("creating %s", new_name);
  99.  
  100.   READ (infd, u_xexecp, sizeof (struct xexec),
  101.     "error reading %s", a_name);
  102.   check_exec (u_xexecp);
  103.   READ (infd, u_xextp, sizeof (struct xext),
  104.     "error reading %s", a_name);
  105.   segpos = u_xextp->xe_segpos;
  106.   nsegs = u_xextp->xe_segsize / sizeof (struct xseg);
  107.   SEEK (infd, segpos, "seek error on %s", a_name);
  108.   for (i = 0; i < nsegs; i ++)
  109.     {
  110.       READ (infd, u_xsegp, sizeof (struct xseg),
  111.         "error reading %s", a_name);
  112.       switch (u_xsegp->xs_type)
  113.     {
  114.     case XS_TTEXT:
  115.       {
  116.         if (i == 0)
  117.           {
  118.         textpos = u_xsegp->xs_filpos;
  119.         textlen = u_xsegp->xs_psize;
  120.         break;
  121.           }
  122.         fatal_unexec ("invalid text segment in %s", a_name);
  123.       }
  124.     case XS_TDATA:
  125.       {
  126.         if (i == 1)
  127.           {
  128.         datapos = u_xsegp->xs_filpos;
  129.         datalen = datalim - (data_org = (char *)(u_xsegp->xs_rbase));
  130.         datadiff = datalen - u_xsegp->xs_psize;
  131.         break;
  132.           }
  133.         fatal_unexec ("invalid data segment in %s", a_name);
  134.       }
  135.     default:
  136.       {
  137.         if (i > 1) break;
  138.         fatal_unexec ("invalid segment record in %s", a_name);
  139.       }
  140.     }
  141.     }
  142.   u_xexecp->x_data = datalen;
  143.   u_xexecp->x_bss = 0;
  144.   WRITE (outfd, u_xexecp, sizeof (struct xexec),
  145.      "error writing %s", new_name);
  146.   WRITE (outfd, u_xextp, sizeof (struct xext),
  147.      "error writing %s", new_name);
  148.   SEEK (infd, segpos, "seek error on %s", a_name);
  149.   SEEK (outfd, segpos, "seek error on %s", new_name);
  150.  
  151.   /* Copy the text segment record verbatim. */
  152.  
  153.   copyrec (infd, outfd, sizeof (struct xseg), a_name, new_name);
  154.  
  155.   /* Read, modify, write the data segment record. */
  156.  
  157.   READ (infd, u_xsegp, sizeof (struct xseg),
  158.     "error reading %s", a_name);
  159.   u_xsegp->xs_psize = u_xsegp->xs_vsize = datalen;
  160.   u_xsegp->xs_attr &= (~XS_AITER & ~XS_ABSS);
  161.   WRITE (outfd, u_xsegp, sizeof (struct xseg),
  162.      "error writing %s", new_name);
  163.  
  164.   /* Now copy any additional segment records, adjusting their
  165.      file position field */
  166.  
  167.   for (i = 2; i < nsegs; i++)
  168.     {
  169.       READ (infd, u_xsegp, sizeof (struct xseg),
  170.         "error reading %s", a_name);
  171.       u_xsegp->xs_filpos += datadiff;
  172.       WRITE (outfd, u_xsegp, sizeof (struct xseg),
  173.          "error writing %s", new_name);
  174.     }
  175.  
  176.   SEEK (infd, textpos, "seek error on %s", a_name);
  177.   SEEK (outfd, textpos, "seek error on %s", new_name);
  178.   copyrec (infd, outfd, textlen, a_name, new_name);
  179.  
  180.   SEEK (outfd, datapos, "seek error on %s", new_name);
  181.   WRITE (outfd, data_org, datalen,
  182.      "write error on %s", new_name);
  183.  
  184.   for (i = 2, segpos += (2 * sizeof (struct xseg)); 
  185.        i < nsegs;
  186.        i++, segpos += sizeof (struct xseg))
  187.     {
  188.       SEEK (infd, segpos, "seek error on %s", a_name);
  189.       READ (infd, u_xsegp, sizeof (struct xseg),
  190.         "read error on %s", a_name);
  191.       SEEK (infd, u_xsegp->xs_filpos, "seek error on %s", a_name);
  192.       /* We should be at eof in the output file here, but we must seek
  193.          because the xs_filpos and xs_psize fields in symbol table
  194.      segments are inconsistent. */
  195.       SEEK (outfd, u_xsegp->xs_filpos + datadiff, "seek error on %s", new_name);
  196.       copyrec (infd, outfd, u_xsegp->xs_psize, a_name, new_name);
  197.     }
  198.   close (infd);
  199.   close (outfd);
  200.   mark_x (new_name);
  201.   return 0;
  202. }
  203.  
  204. copyrec (infd, outfd, len, in_name, out_name)
  205.      int infd, outfd, len;
  206.      char *in_name, *out_name;
  207. {
  208.   char buf[BUFSIZ];
  209.   int chunk;
  210.  
  211.   while (len)
  212.     {
  213.       chunk = BUFSIZ;
  214.       if (chunk > len)
  215.     chunk = len;
  216.       READ (infd, buf, chunk, "error reading %s", in_name);
  217.       WRITE (outfd, buf, chunk, "error writing %s", out_name);
  218.       len -= chunk;
  219.     }
  220. }
  221.  
  222. /*
  223.  * mark_x
  224.  *
  225.  * After successfully building the new a.out, mark it executable
  226.  */
  227. static
  228. mark_x (name)
  229.      char *name;
  230. {
  231.   struct stat sbuf;
  232.   int um = umask (777);
  233.   umask (um);
  234.   if (stat (name, &sbuf) < 0)
  235.     fatal_unexec ("getting protection on %s", name);
  236.   sbuf.st_mode |= 0111 & ~um;
  237.   if (chmod (name, sbuf.st_mode) < 0)
  238.     fatal_unexec ("setting protection on %s", name);
  239. }
  240.  
  241. static void
  242. fatal_unexec (s, va_alist)
  243.     va_dcl
  244. {
  245.   va_list ap;
  246.   if (errno == EEOF)
  247.     fputs ("unexec: unexpected end of file, ", stderr);
  248.   else
  249.     fprintf (stderr, "unexec: %s, ", strerror (errno));
  250.   va_start (ap);
  251.   _doprnt (s, ap, stderr);
  252.   fputs (".\n", stderr);
  253.   exit (1);
  254. }
  255.