home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / plbin.zip / pl / src / gnu / unexmips.c < prev   
C/C++ Source or Header  |  1992-06-18  |  14KB  |  390 lines

  1. /* Unexec for MIPS (including IRIS4D).
  2.    Note that the GNU project considers support for MIPS operation
  3.    a peripheral activity which should not be allowed to divert effort
  4.    from development of the GNU system.  Changes in this code will be
  5.    installed when users send them in, but aside from that
  6.    we don't plan to think about it, or about whether other Emacs
  7.    maintenance might break it.
  8.  
  9.    Copyright (C) 1988 Free Software Foundation, Inc.
  10.  
  11.                NO WARRANTY
  12.  
  13.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  14. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  15. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  16. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  17. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  18. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  19. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  20. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  21. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  22. CORRECTION.
  23.  
  24.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  25. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  26. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  27. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  28. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  29. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  30. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  31. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  32. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  33. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  34.  
  35.         GENERAL PUBLIC LICENSE TO COPY
  36.  
  37.   1. You may copy and distribute verbatim copies of this source file
  38. as you receive it, in any medium, provided that you conspicuously and
  39. appropriately publish on each copy a valid copyright notice "Copyright
  40. (C) 1987 Free Software Foundation, Inc."; and include following the
  41. copyright notice a verbatim copy of the above disclaimer of warranty
  42. and of this License.  You may charge a distribution fee for the
  43. physical act of transferring a copy.
  44.  
  45.   2. You may modify your copy or copies of this source file or
  46. any portion of it, and copy and distribute such modifications under
  47. the terms of Paragraph 1 above, provided that you also do the following:
  48.  
  49.     a) cause the modified files to carry prominent notices stating
  50.     that you changed the files and the date of any change; and
  51.  
  52.     b) cause the whole of any work that you distribute or publish,
  53.     that in whole or in part contains or is a derivative of this
  54.     program or any part thereof, to be licensed at no charge to all
  55.     third parties on terms identical to those contained in this
  56.     License Agreement (except that you may choose to grant more extensive
  57.     warranty protection to some or all third parties, at your option).
  58.  
  59.     c) You may charge a distribution fee for the physical act of
  60.     transferring a copy, and you may at your option offer warranty
  61.     protection in exchange for a fee.
  62.  
  63. Mere aggregation of another unrelated program with this program (or its
  64. derivative) on a volume of a storage or distribution medium does not bring
  65. the other program under the scope of these terms.
  66.  
  67.   3. You may copy and distribute this program (or a portion or derivative
  68. of it, under Paragraph 2) in object code or executable form under the terms
  69. of Paragraphs 1 and 2 above provided that you also do one of the following:
  70.  
  71.     a) accompany it with the complete corresponding machine-readable
  72.     source code, which must be distributed under the terms of
  73.     Paragraphs 1 and 2 above; or,
  74.  
  75.     b) accompany it with a written offer, valid for at least three
  76.     years, to give any third party free (except for a nominal
  77.     shipping charge) a complete machine-readable copy of the
  78.     corresponding source code, to be distributed under the terms of
  79.     Paragraphs 1 and 2 above; or,
  80.  
  81.     c) accompany it with the information you received as to where the
  82.     corresponding source code may be obtained.  (This alternative is
  83.     allowed only for noncommercial distribution and only if you
  84.     received the program in object code or executable form alone.)
  85.  
  86. For an executable file, complete source code means all the source code for
  87. all modules it contains; but, as a special exception, it need not include
  88. source code for modules which are standard libraries that accompany the
  89. operating system on which the executable file runs.
  90.  
  91.   4. You may not copy, sublicense, distribute or transfer this program
  92. except as expressly provided under this License Agreement.  Any attempt
  93. otherwise to copy, sublicense, distribute or transfer this program is void and
  94. your rights to use the program under this License agreement shall be
  95. automatically terminated.  However, parties who have received computer
  96. software programs from you with this License Agreement will not have
  97. their licenses terminated so long as such parties remain in full compliance.
  98.  
  99.   5. If you wish to incorporate parts of this program into other free
  100. programs whose distribution conditions are different, write to the Free
  101. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  102. worked out a simple rule that can be stated here, but we will often permit
  103. this.  We will be guided by the two goals of preserving the free status of
  104. all derivatives of our free software and of promoting the sharing and reuse of
  105. software.
  106.  
  107.  
  108. In other words, you are welcome to use, share and improve this program.
  109. You are forbidden to forbid anyone else to use, share and improve
  110. what you give them.   Help stamp out software-hoarding!  */
  111.  
  112.  
  113. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  114. THIS IS A MODIFIED VERSION.
  115.  
  116. Modified by: Jan Wielemaker (jan@swi.psy.uva.nl)
  117. Reason:         Changed error message interface to confirm with SWI-Prolog
  118. Changes:     Various, but only messages
  119. Date:         10/10/90
  120. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  121.  
  122. #ifdef EMACS
  123. #include "config.h"
  124. #endif
  125. #include <sys/types.h>
  126. #include <sys/file.h>
  127. #include <sys/stat.h>
  128. #include <stdio.h>
  129. #ifndef va_dcl
  130. #include <varargs.h>
  131. #endif
  132. #include <filehdr.h>
  133. #include <aouthdr.h>
  134. #include <scnhdr.h>
  135. #include <sym.h>
  136.  
  137. #ifdef IRIS_4D
  138. #include "getpagesize.h"
  139. #include <fcntl.h>
  140. #endif
  141.  
  142. static void fatal_unexec ();
  143.  
  144. #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
  145.     errno = EEOF; \
  146.     if (read(_fd, _buffer, _size) != _size) \
  147.       fatal_unexec(_error_message, _error_arg);
  148.  
  149. #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
  150.     if (write(_fd, _buffer, _size) != _size) \
  151.       fatal_unexec(_error_message, _error_arg);
  152.  
  153. #define SEEK(_fd, _position, _error_message, _error_arg) \
  154.     errno = EEOF; \
  155.     if (lseek(_fd, _position, L_SET) != _position) \
  156.       fatal_unexec(_error_message, _error_arg);
  157.  
  158. extern int errno;
  159. extern int sys_nerr;
  160. extern char *sys_errlist[];
  161. #define EEOF -1
  162.  
  163. static struct scnhdr *text_section;
  164. static struct scnhdr *init_section;
  165. static struct scnhdr *finit_section;
  166. static struct scnhdr *rdata_section;
  167. static struct scnhdr *data_section;
  168. static struct scnhdr *lit8_section;
  169. static struct scnhdr *lit4_section;
  170. static struct scnhdr *sdata_section;
  171. static struct scnhdr *sbss_section;
  172. static struct scnhdr *bss_section;
  173.  
  174. struct headers {
  175.     struct filehdr fhdr;
  176.     struct aouthdr aout;
  177.     struct scnhdr section[10];
  178. };
  179.  
  180. /* Define name of label for entry point for the dumped executable.  */
  181.  
  182. #ifndef DEFAULT_ENTRY_ADDRESS
  183. #define DEFAULT_ENTRY_ADDRESS __start
  184. #endif
  185.  
  186. unexec (new_name, a_name, s_name, data_start, bss_start, entry_address)
  187.      char *new_name, *a_name, *s_name;    /* s_name not implemented yet */
  188.      unsigned data_start, bss_start, entry_address;
  189. {
  190.   int new, old;
  191.   int pagesize, brk;
  192.   int newsyms, symrel;
  193.   int nread;
  194.   struct headers hdr;
  195.   int i;
  196.   int vaddr, scnptr;
  197. #define BUFSIZE 8192
  198.   char buffer[BUFSIZE];
  199.  
  200.   old = open (a_name, O_RDONLY, 0);
  201.   if (old < 0) fatal_unexec ("opening %s", a_name);
  202.  
  203.   new = creat (new_name, 0666);
  204.   if (new < 0) fatal_unexec ("creating %s", new_name);
  205.  
  206.   hdr = *((struct headers *)TEXT_START);
  207.   if (hdr.fhdr.f_magic != MIPSELMAGIC
  208.       && hdr.fhdr.f_magic != MIPSEBMAGIC)
  209.     {
  210.       fatalError("input file magic number is %x, not %x or %x.\n",
  211.          hdr.fhdr.f_magic, MIPSELMAGIC, MIPSEBMAGIC);
  212.     }
  213.   if (hdr.fhdr.f_opthdr != sizeof(hdr.aout))
  214.     {
  215.       fatalError("unexec: input a.out header is %d bytes, not %d.\n",
  216.          hdr.fhdr.f_opthdr, sizeof(hdr.aout));
  217.     }
  218.   if (hdr.aout.magic != ZMAGIC)
  219.     {
  220.       fatalError("unexec: input file a.out magic number is %o, not %o.\n",
  221.          hdr.aout.magic, ZMAGIC);
  222.       exit(1);
  223.     }
  224.  
  225. #define CHECK_SCNHDR(ptr, name, flags) \
  226.   if (strcmp(hdr.section[i].s_name, name) == 0) { \
  227.     if (hdr.section[i].s_flags != flags) { \
  228.       warning("%x flags where %x expected in %s section.\n", \
  229.           hdr.section[i].s_flags, flags, name); \
  230.     } \
  231.     ptr = hdr.section + i; \
  232.     i += 1; \
  233.   } \
  234.   else { \
  235.     ptr = NULL; \
  236.     }
  237.  
  238.   i = 0;
  239.   CHECK_SCNHDR(text_section,  _TEXT,  STYP_TEXT);
  240.   CHECK_SCNHDR(init_section,  _INIT,  STYP_INIT);
  241.   CHECK_SCNHDR(rdata_section, _RDATA, STYP_RDATA);
  242.   CHECK_SCNHDR(data_section,  _DATA,  STYP_DATA);
  243. #ifdef _LIT8
  244.   CHECK_SCNHDR(lit8_section,  _LIT8,  STYP_LIT8);
  245.   CHECK_SCNHDR(lit4_section,  _LIT4,  STYP_LIT4);
  246. #endif /* _LIT8 */
  247.   CHECK_SCNHDR(sdata_section, _SDATA, STYP_SDATA);
  248.   CHECK_SCNHDR(sbss_section,  _SBSS,  STYP_SBSS);
  249.   CHECK_SCNHDR(bss_section,   _BSS,   STYP_BSS);
  250.   if (i != hdr.fhdr.f_nscns)
  251.     warning("%d sections found instead of %d.\n",
  252.         i, hdr.fhdr.f_nscns);
  253.  
  254.   pagesize = getpagesize();
  255.   brk = (sbrk(0) + pagesize - 1) & (-pagesize);
  256.   hdr.aout.dsize = brk - DATA_START;
  257.   hdr.aout.bsize = 0;
  258.   if (entry_address == 0)
  259.     {
  260.       extern DEFAULT_ENTRY_ADDRESS();
  261.       hdr.aout.entry = (unsigned)DEFAULT_ENTRY_ADDRESS;
  262.     }
  263.   else
  264.     hdr.aout.entry = entry_address;
  265.  
  266.   hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize;
  267.   rdata_section->s_size = data_start - DATA_START;
  268.   data_section->s_vaddr = data_start;
  269.   data_section->s_paddr = data_start;
  270.   data_section->s_size = brk - DATA_START;
  271.   data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size;
  272.   vaddr = data_section->s_vaddr + data_section->s_size;
  273.   scnptr = data_section->s_scnptr + data_section->s_size;
  274.   if (lit8_section != NULL)
  275.     {
  276.       lit8_section->s_vaddr = vaddr;
  277.       lit8_section->s_paddr = vaddr;
  278.       lit8_section->s_size = 0;
  279.       lit8_section->s_scnptr = scnptr;
  280.     }
  281.   if (lit4_section != NULL)
  282.     {
  283.       lit4_section->s_vaddr = vaddr;
  284.       lit4_section->s_paddr = vaddr;
  285.       lit4_section->s_size = 0;
  286.       lit4_section->s_scnptr = scnptr;
  287.     }
  288.   if (sdata_section != NULL)
  289.     {
  290.       sdata_section->s_vaddr = vaddr;
  291.       sdata_section->s_paddr = vaddr;
  292.       sdata_section->s_size = 0;
  293.       sdata_section->s_scnptr = scnptr;
  294.     }
  295.   if (sbss_section != NULL)
  296.     {
  297.       sbss_section->s_vaddr = vaddr;
  298.       sbss_section->s_paddr = vaddr;
  299.       sbss_section->s_size = 0;
  300.       sbss_section->s_scnptr = scnptr;
  301.     }
  302.   if (bss_section != NULL)
  303.     {
  304.       bss_section->s_vaddr = vaddr;
  305.       bss_section->s_paddr = vaddr;
  306.       bss_section->s_size = 0;
  307.       bss_section->s_scnptr = scnptr;
  308.     }
  309.  
  310.   Putf("text: %ld ... ", hdr.aout.tsize); pl_flush();
  311.   WRITE(new, TEXT_START, hdr.aout.tsize,
  312.     "writing text section to %s", new_name);
  313.   Putf("data: %ld ... ", hdr.aout.dsize); pl_flush();
  314.   WRITE(new, DATA_START, hdr.aout.dsize,
  315.     "writing text section to %s", new_name);
  316.  
  317.   SEEK(old, hdr.fhdr.f_symptr, "seeking to start of symbols in %s", a_name);
  318.   errno = EEOF;
  319.   nread = read(old, buffer, BUFSIZE);
  320.   if (nread < sizeof(HDRR)) fatal_unexec("reading symbols from %s", a_name);
  321. #define symhdr ((pHDRR)buffer)
  322.   newsyms = hdr.aout.tsize + hdr.aout.dsize;
  323.   symrel = newsyms - hdr.fhdr.f_symptr;
  324.   hdr.fhdr.f_symptr = newsyms;
  325.   symhdr->cbLineOffset += symrel;
  326.   symhdr->cbDnOffset += symrel;
  327.   symhdr->cbPdOffset += symrel;
  328.   symhdr->cbSymOffset += symrel;
  329.   symhdr->cbOptOffset += symrel;
  330.   symhdr->cbAuxOffset += symrel;
  331.   symhdr->cbSsOffset += symrel;
  332.   symhdr->cbSsExtOffset += symrel;
  333.   symhdr->cbFdOffset += symrel;
  334.   symhdr->cbRfdOffset += symrel;
  335.   symhdr->cbExtOffset += symrel;
  336. #undef symhdr
  337.   Putf("symbols ... "); pl_flush();
  338.   do
  339.     {
  340.       if (write(new, buffer, nread) != nread)
  341.     fatal_unexec("writing symbols to %s", new_name);
  342.       nread = read(old, buffer, BUFSIZE);
  343.       if (nread < 0) fatal_unexec("reading symbols from %s", a_name);
  344. #undef BUFSIZE
  345.     } while (nread != 0);
  346.  
  347.   SEEK(new, 0, "seeking to start of header in %s", new_name);
  348.   WRITE(new, &hdr, sizeof(hdr),
  349.     "writing header of %s", new_name);
  350.  
  351.   Putf("ok\n");
  352.   close(old);
  353.   close(new);
  354.   mark_x(new_name);
  355. }
  356.  
  357. /*
  358.  * mark_x
  359.  *
  360.  * After succesfully building the new a.out, mark it executable
  361.  */
  362.  
  363. static
  364. mark_x (name)
  365.      char *name;
  366. {
  367.   struct stat sbuf;
  368.   int um = umask (777);
  369.   umask (um);
  370.   if (stat (name, &sbuf) < 0)
  371.     fatal_unexec ("getting protection on %s", name);
  372.   sbuf.st_mode |= 0111 & ~um;
  373.   if (chmod (name, sbuf.st_mode) < 0)
  374.     fatal_unexec ("setting protection on %s", name);
  375. }
  376.  
  377. static void
  378. fatal_unexec (s, va_alist)
  379.      va_dcl
  380. { char msg[1000];
  381.   va_list ap;
  382.  
  383.   va_start(ap);
  384.   vsprintf(msg, s, ap);
  385.   va_end(ap);
  386.  
  387.   fatalError("save_program/2: failed to create a saved state: %s (%s)",
  388.          OsError(), msg);
  389. }
  390.