home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / unexconvex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-29  |  21.5 KB  |  810 lines

  1. /* Modified version of unexec for convex machines.
  2.    Note that the GNU project considers support for the peculiarities
  3.    of the Convex operating system a peripheral activity which should
  4.    not be allowed to divert effort from development of the GNU system.
  5.    Changes in this code will be installed when Convex system
  6.    maintainers send them in, but aside from that we don't plan to
  7.    think about it, or about whether other Emacs maintenance might
  8.    break it.
  9.  
  10.    Copyright (C) 1985, 1986, 1988 Free Software Foundation, Inc.
  11.  
  12. This file is part of GNU Emacs.
  13.  
  14. GNU Emacs is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 1, or (at your option)
  17. any later version.
  18.  
  19. GNU Emacs is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with GNU Emacs; see the file COPYING.  If not, write to
  26. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  27.  
  28.  
  29. /* modifyed for C-1 arch by jthomp@convex 871103 */
  30.  
  31. /*
  32.  * unexec.c - Convert a running program into an a.out file.
  33.  *
  34.  * Author:    Spencer W. Thomas
  35.  *         Computer Science Dept.
  36.  *         University of Utah
  37.  * Date:    Tue Mar  2 1982
  38.  * Modified heavily since then.
  39.  *
  40.  * Synopsis:
  41.  *    unexec (new_name, a_name, data_start, bss_start, entry_address)
  42.  *    char *new_name, *a_name;
  43.  *    unsigned data_start, bss_start, entry_address;
  44.  *
  45.  * Takes a snapshot of the program and makes an a.out format file in the
  46.  * file named by the string argument new_name.
  47.  * If a_name is non-NULL, the symbol table will be taken from the given file.
  48.  * On some machines, an existing a_name file is required.
  49.  *
  50.  * The boundaries within the a.out file may be adjusted with the data_start
  51.  * and bss_start arguments.  Either or both may be given as 0 for defaults.
  52.  *
  53.  * Data_start gives the boundary between the text segment and the data
  54.  * segment of the program.  The text segment can contain shared, read-only
  55.  * program code and literal data, while the data segment is always unshared
  56.  * and unprotected.  Data_start gives the lowest unprotected address.
  57.  * The value you specify may be rounded down to a suitable boundary
  58.  * as required by the machine you are using.
  59.  *
  60.  * Specifying zero for data_start means the boundary between text and data
  61.  * should not be the same as when the program was loaded.
  62.  * If NO_REMAP is defined, the argument data_start is ignored and the
  63.  * segment boundaries are never changed.
  64.  *
  65.  * Bss_start indicates how much of the data segment is to be saved in the
  66.  * a.out file and restored when the program is executed.  It gives the lowest
  67.  * unsaved address, and is rounded up to a page boundary.  The default when 0
  68.  * is given assumes that the entire data segment is to be stored, including
  69.  * the previous data and bss as well as any additional storage allocated with
  70.  * break (2).
  71.  *
  72.  * The new file is set up to start at entry_address.
  73.  *
  74.  * If you make improvements I'd like to get them too.
  75.  * harpo!utah-cs!thomas, thomas@Utah-20
  76.  *
  77.  */
  78.  
  79. /* There are several compilation parameters affecting unexec:
  80.  
  81. * COFF
  82.  
  83. Define this if your system uses COFF for executables.
  84. Otherwise we assume you use Berkeley format.
  85.  
  86. * NO_REMAP
  87.  
  88. Define this if you do not want to try to save Emacs's pure data areas
  89. as part of the text segment.
  90.  
  91. Saving them as text is good because it allows users to share more.
  92.  
  93. However, on machines that locate the text area far from the data area,
  94. the boundary cannot feasibly be moved.  Such machines require
  95. NO_REMAP.
  96.  
  97. Also, remapping can cause trouble with the built-in startup routine
  98. /lib/crt0.o, which defines `environ' as an initialized variable.
  99. Dumping `environ' as pure does not work!  So, to use remapping,
  100. you must write a startup routine for your machine in Emacs's crt0.c.
  101. If NO_REMAP is defined, Emacs uses the system's crt0.o.
  102.  
  103. * SECTION_ALIGNMENT
  104.  
  105. Some machines that use COFF executables require that each section
  106. start on a certain boundary *in the COFF file*.  Such machines should
  107. define SECTION_ALIGNMENT to a mask of the low-order bits that must be
  108. zero on such a boundary.  This mask is used to control padding between
  109. segments in the COFF file.
  110.  
  111. If SECTION_ALIGNMENT is not defined, the segments are written
  112. consecutively with no attempt at alignment.  This is right for
  113. unmodified system V.
  114.  
  115. * SEGMENT_MASK
  116.  
  117. Some machines require that the beginnings and ends of segments
  118. *in core* be on certain boundaries.  For most machines, a page
  119. boundary is sufficient.  That is the default.  When a larger
  120. boundary is needed, define SEGMENT_MASK to a mask of
  121. the bits that must be zero on such a boundary.
  122.  
  123. * A_TEXT_OFFSET(HDR)
  124.  
  125. Some machines count the a.out header as part of the size of the text
  126. segment (a_text); they may actually load the header into core as the
  127. first data in the text segment.  Some have additional padding between
  128. the header and the real text of the program that is counted in a_text.
  129.  
  130. For these machines, define A_TEXT_OFFSET(HDR) to examine the header
  131. structure HDR and return the number of bytes to add to `a_text'
  132. before writing it (above and beyond the number of bytes of actual
  133. program text).  HDR's standard fields are already correct, except that
  134. this adjustment to the `a_text' field has not yet been made;
  135. thus, the amount of offset can depend on the data in the file.
  136.   
  137. * A_TEXT_SEEK(HDR)
  138.  
  139. If defined, this macro specifies the number of bytes to seek into the
  140. a.out file before starting to write the text segment.a
  141.  
  142. * EXEC_MAGIC
  143.  
  144. For machines using COFF, this macro, if defined, is a value stored
  145. into the magic number field of the output file.
  146.  
  147. * ADJUST_EXEC_HEADER
  148.  
  149. This macro can be used to generate statements to adjust or
  150. initialize nonstandard fields in the file header
  151.  
  152. * ADDR_CORRECT(ADDR)
  153.  
  154. Macro to correct an int which is the bit pattern of a pointer to a byte
  155. into an int which is the number of a byte.
  156.  
  157. This macro has a default definition which is usually right.
  158. This default definition is a no-op on most machines (where a
  159. pointer looks like an int) but not on all machines.
  160.  
  161. */
  162.  
  163. #ifndef emacs
  164. #define PERROR(arg) perror (arg); return -1
  165. #else
  166. #include "config.h"
  167. #define PERROR(file) report_error (file, new)
  168. #endif
  169.  
  170. #ifndef CANNOT_DUMP  /* all rest of file!  */
  171.  
  172. #ifndef CANNOT_UNEXEC /* most of rest of file */
  173.  
  174. #include <a.out.h>
  175. /* Define getpagesize () if the system does not.
  176.    Note that this may depend on symbols defined in a.out.h
  177.  */
  178. #include "getpagesize.h"
  179.  
  180. #ifndef makedev            /* Try to detect types.h already loaded */
  181. #include <sys/types.h>
  182. #endif
  183. #include <stdio.h>
  184. #include <sys/stat.h>
  185. #include <errno.h>
  186.  
  187. extern char *start_of_text ();        /* Start of text */
  188. extern char *start_of_data ();        /* Start of initialized data */
  189.  
  190. #ifdef COFF
  191. #include <machine/filehdr.h>
  192. #include <machine/opthdr.h>
  193. #include <machine/scnhdr.h>
  194. #include <machine/pte.h>
  195. #include <machine/symtab.h>
  196.  
  197. static long block_copy_start;        /* Old executable start point */
  198. static struct filehdr f_hdr;        /* File header */
  199. static struct opthdr f_ohdr;        /* Optional file header (a.out) */
  200. long bias;            /* Bias to add for growth */
  201. long lnnoptr;            /* Pointer to line-number info within file */
  202. #define SYMS_START block_copy_start
  203.  
  204. static long text_scnptr;
  205. static long data_scnptr;
  206.  
  207. #else /* not COFF */
  208.  
  209. extern char *sbrk ();
  210.  
  211. #define SYMS_START ((long) N_SYMOFF (ohdr))
  212.  
  213. #ifdef HPUX
  214. #ifdef HP9000S200_ID
  215. #define MY_ID HP9000S200_ID
  216. #else
  217. #include <model.h>
  218. #define MY_ID MYSYS
  219. #endif /* no HP9000S200_ID */
  220. static MAGIC OLDMAGIC = {MY_ID, SHARE_MAGIC};
  221. static MAGIC NEWMAGIC = {MY_ID, DEMAND_MAGIC};
  222. #define N_TXTOFF(x) TEXT_OFFSET(x)
  223. #define N_SYMOFF(x) LESYM_OFFSET(x)
  224. static struct exec hdr, ohdr;
  225.  
  226. #else /* not HPUX */
  227.  
  228. #if defined (USG) && !defined (IRIS)
  229. static struct bhdr hdr, ohdr;
  230. #define a_magic fmagic
  231. #define a_text tsize
  232. #define a_data dsize
  233. #define a_bss bsize
  234. #define a_syms ssize
  235. #define a_trsize rtsize
  236. #define a_drsize rdsize
  237. #define a_entry entry
  238. #define    N_BADMAG(x) \
  239.     (((x).fmagic)!=OMAGIC && ((x).fmagic)!=NMAGIC &&\
  240.      ((x).fmagic)!=FMAGIC && ((x).fmagic)!=IMAGIC)
  241. #define NEWMAGIC FMAGIC
  242. #else /* IRIS or not USG */
  243. static struct exec hdr, ohdr;
  244. #define NEWMAGIC ZMAGIC
  245. #endif /* IRIS or not USG */
  246. #endif /* not HPUX */
  247.  
  248. static int unexec_text_start;
  249. static int unexec_data_start;
  250.  
  251. #endif /* not COFF */
  252.  
  253. static int pagemask;
  254.  
  255. /* Correct an int which is the bit pattern of a pointer to a byte
  256.    into an int which is the number of a byte.
  257.    This is a no-op on ordinary machines, but not on all.  */
  258.  
  259. #ifndef ADDR_CORRECT   /* Let m-*.h files override this definition */
  260. #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
  261. #endif
  262.  
  263. #ifdef emacs
  264.  
  265. static
  266. report_error (file, fd)
  267.      char *file;
  268.      int fd;
  269. {
  270.   if (fd)
  271.     close (fd);
  272.   error ("Failure operating on %s", file);
  273. }
  274. #endif /* emacs */
  275.  
  276. #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
  277. #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
  278. #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
  279.  
  280. static
  281. report_error_1 (fd, msg, a1, a2)
  282.      int fd;
  283.      char *msg;
  284.      int a1, a2;
  285. {
  286.   close (fd);
  287. #ifdef emacs
  288.   error (msg, a1, a2);
  289. #else
  290.   fprintf (stderr, msg, a1, a2);
  291.   fprintf (stderr, "\n");
  292. #endif
  293. }
  294.  
  295. /* ****************************************************************
  296.  * unexec
  297.  *
  298.  * driving logic.
  299.  */
  300. unexec (new_name, a_name, data_start, bss_start, entry_address)
  301.      char *new_name, *a_name;
  302.      unsigned data_start, bss_start, entry_address;
  303. {
  304.   int new, a_out = -1;
  305.  
  306.   if (a_name && (a_out = open (a_name, 0)) < 0)
  307.     {
  308.       PERROR (a_name);
  309.     }
  310.   if ((new = creat (new_name, 0666)) < 0)
  311.     {
  312.       PERROR (new_name);
  313.     }
  314.  
  315.   if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0
  316.       || copy_text_and_data (new) < 0
  317.       || copy_sym (new, a_out, a_name, new_name) < 0
  318. #ifdef COFF
  319.       || adjust_lnnoptrs (new, a_out, new_name) < 0
  320. #endif
  321.       )
  322.     {
  323.       close (new);
  324.       /* unlink (new_name);            /* Failed, unlink new a.out */
  325.       return -1;    
  326.     }
  327.  
  328.   close (new);
  329.   if (a_out >= 0)
  330.     close (a_out);
  331.   mark_x (new_name);
  332.   return 0;
  333. }
  334.  
  335. /* ****************************************************************
  336.  * make_hdr
  337.  *
  338.  * Make the header in the new a.out from the header in core.
  339.  * Modify the text and data sizes.
  340.  */
  341. #ifdef COFF
  342.  struct scnhdr f_thdr;        /* Text section header */
  343.  struct scnhdr f_dhdr;        /* Data section header */
  344.  struct scnhdr f_bhdr;        /* Bss section header */
  345.  struct scnhdr scntemp;        /* Temporary section header */
  346. #endif /* COFF */
  347. static int
  348. make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
  349.      int new, a_out;
  350.      unsigned data_start, bss_start, entry_address;
  351.      char *a_name;
  352.      char *new_name;
  353. {
  354.   int tem;
  355.   register int scns;
  356.   unsigned int bss_end;
  357.  
  358.   pagemask = getpagesize () - 1;
  359.  
  360.   /* Adjust text/data boundary. */
  361. #ifdef NO_REMAP
  362.   data_start = (int) start_of_data ();
  363. #else /* not NO_REMAP */
  364.   if (!data_start)
  365.     data_start = (int) start_of_data ();
  366. #endif /* not NO_REMAP */
  367.   data_start = ADDR_CORRECT (data_start);
  368.  
  369. #ifdef SEGMENT_MASK
  370.   data_start = data_start & ~SEGMENT_MASK; /* (Down) to segment boundary. */
  371. #else
  372.   data_start = data_start & ~pagemask; /* (Down) to page boundary. */
  373. #endif
  374.  
  375.   bss_end = (ADDR_CORRECT (sbrk (0)) + pagemask) & ~pagemask;
  376.  
  377.   /* Adjust data/bss boundary. */
  378.   if (bss_start != 0)
  379.     {
  380.       bss_start = (ADDR_CORRECT (bss_start) + pagemask) & ~pagemask;          /* (Up) to page bdry. */
  381.       if (bss_start > bss_end)
  382.     {
  383.       ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
  384.           bss_start);
  385.     }
  386.     }
  387.   else
  388.     bss_start = bss_end;
  389.  
  390.   if (data_start > bss_start)    /* Can't have negative data size. */
  391.     {
  392.       ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
  393.           data_start, bss_start);
  394.     }
  395.  
  396. #ifdef COFF
  397.   /* Salvage as much info from the existing file as possible */
  398.   if (a_out >= 0)
  399.     {
  400.       if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
  401.     {
  402.       PERROR (a_name);
  403.     }
  404.       block_copy_start += sizeof (f_hdr);
  405.       if (f_hdr.h_opthdr > 0)
  406.     {
  407.       if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
  408.         {
  409.           PERROR (a_name);
  410.         }
  411.       block_copy_start += sizeof (f_ohdr);
  412.     }
  413.       /* Loop through section headers, copying them in */
  414.       for (scns = f_hdr.h_nscns; scns > 0; scns--) {
  415.     if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
  416.       {
  417.         PERROR (a_name);
  418.       }
  419.     if (scntemp.s_scnptr > 0L)
  420.       {
  421.             if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
  422.           block_copy_start = scntemp.s_scnptr + scntemp.s_size;
  423.       }
  424.     if (scntemp.s_flags & S_TEXT)
  425.       {
  426.         f_thdr = scntemp;
  427.       }
  428.     else if (scntemp.s_flags & S_DATA)
  429.       {
  430.         f_dhdr = scntemp;
  431.       }
  432.     else if (scntemp.s_flags & S_BSS)
  433.       {
  434.         f_bhdr = scntemp;
  435.       }
  436.       }
  437.     }
  438.   else
  439.     {
  440.       ERROR0 ("can't build a COFF file from scratch yet");
  441.     }
  442.  
  443.   /* Now we alter the contents of all the f_*hdr variables
  444.      to correspond to what we want to dump.  */
  445.  
  446.   f_hdr.h_flags |= (0);      /* this isn't really correct..*/
  447. #ifdef EXEC_MAGIC
  448.   f_ohdr.magic = EXEC_MAGIC;
  449. #endif
  450.   f_thdr.s_vaddr = (long) start_of_text ();
  451.   f_thdr.s_size = data_start - f_thdr.s_vaddr;
  452.   f_thdr.s_scnptr = 4096;
  453.   f_thdr.s_relptr = 0;
  454.   f_thdr.s_nrel = 0;
  455.   f_thdr.s_prot = 0xA;
  456.  
  457.   f_dhdr.s_vaddr = data_start;
  458.   f_dhdr.s_size = bss_start - data_start;
  459.   f_dhdr.s_scnptr = 4096 + (data_start - f_thdr.s_vaddr);
  460.   f_dhdr.s_relptr = 0;
  461.   f_dhdr.s_nrel = 0;
  462.   f_dhdr.s_prot = 0xC;
  463.   
  464.   f_bhdr.s_vaddr = bss_start;
  465.   f_bhdr.s_size = bss_end - bss_start + 4096 /* fudge */;
  466.   f_bhdr.s_scnptr = 0;
  467.   f_bhdr.s_relptr = 0;
  468.   f_bhdr.s_nrel = 0;
  469.   f_bhdr.s_prot = 0xC;
  470. #ifdef SECTION_ALIGNMENT
  471.   /* Some systems require special alignment
  472.      of the sections in the file itself.  */
  473.   f_thdr.s_scnptr
  474.     = (f_thdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT;
  475. #endif /* SECTION_ALIGNMENT */
  476.   text_scnptr = f_thdr.s_scnptr;
  477.   data_scnptr = f_dhdr.s_scnptr;
  478.   bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
  479.  
  480.   if (f_ohdr.o_symptr > 0L)
  481.     {
  482.       f_ohdr.o_symptr += bias;
  483.     }
  484.  
  485.   if (f_hdr.h_strptr > 0)
  486.   {
  487.       f_hdr.h_strptr += bias;
  488.   }
  489.  
  490. #ifdef ADJUST_EXEC_HEADER
  491.   ADJUST_EXEC_HEADER
  492. #endif /* ADJUST_EXEC_HEADER */
  493.  
  494.   if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
  495.     {
  496.       PERROR (new_name);
  497.     }
  498.  
  499.   if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
  500.     {
  501.       PERROR (new_name);
  502.     }
  503.  
  504.   if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
  505.     {
  506.       PERROR (new_name);
  507.     }
  508.  
  509.   if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
  510.     {
  511.       PERROR (new_name);
  512.     }
  513.  
  514.   if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
  515.     {
  516.       PERROR (new_name);
  517.     }
  518.   return (0);
  519.  
  520. #else /* if not COFF */
  521.  
  522.   /* Get symbol table info from header of a.out file if given one. */
  523.   if (a_out >= 0)
  524.     {
  525.       if (read (a_out, &ohdr, sizeof hdr) != sizeof hdr)
  526.     {
  527.       PERROR (a_name);
  528.     }
  529.  
  530.       if N_BADMAG (ohdr)
  531.     {
  532.       ERROR1 ("invalid magic number in %s", a_name);
  533.     }
  534.       hdr = ohdr;
  535.     }
  536.   else
  537.     {
  538.       bzero (hdr, sizeof hdr);
  539.     }
  540.  
  541.   unexec_text_start = (long) start_of_text ();
  542.   unexec_data_start = data_start;
  543.  
  544.   /* Machine-dependent fixup for header, or maybe for unexec_text_start */
  545. #ifdef ADJUST_EXEC_HEADER
  546.   ADJUST_EXEC_HEADER;
  547. #endif /* ADJUST_EXEC_HEADER */
  548.  
  549.   hdr.a_trsize = 0;
  550.   hdr.a_drsize = 0;
  551.   if (entry_address != 0)
  552.     hdr.a_entry = entry_address;
  553.  
  554.   hdr.a_bss = bss_end - bss_start;
  555.   hdr.a_data = bss_start - data_start;
  556. #ifdef NO_REMAP
  557.   hdr.a_text = ohdr.a_text;
  558. #else /* not NO_REMAP */
  559.   hdr.a_text = data_start - unexec_text_start;
  560. #endif /* not NO_REMAP */
  561.  
  562. #ifdef A_TEXT_OFFSET
  563.   hdr.a_text += A_TEXT_OFFSET (ohdr);
  564. #endif
  565.  
  566.   if (write (new, &hdr, sizeof hdr) != sizeof hdr)
  567.     {
  568.       PERROR (new_name);
  569.     }
  570.  
  571. #ifdef A_TEXT_OFFSET
  572.   hdr.a_text -= A_TEXT_OFFSET (ohdr);
  573. #endif
  574.  
  575.   return 0;
  576.  
  577. #endif /* not COFF */
  578. }
  579.  
  580. /* ****************************************************************
  581.  * copy_text_and_data
  582.  *
  583.  * Copy the text and data segments from memory to the new a.out
  584.  */
  585. static int
  586. copy_text_and_data (new)
  587.      int new;
  588. {
  589.   register char *end;
  590.   register char *ptr;
  591.  
  592. #ifdef COFF
  593.   lseek (new, (long) text_scnptr, 0);
  594.   ptr = (char *) f_thdr.s_vaddr;
  595.   end = ptr + f_thdr.s_size;
  596.   write_segment (new, ptr, end);
  597.  
  598.   lseek (new, (long) data_scnptr, 0);
  599.   ptr = (char *) f_dhdr.s_vaddr;
  600.   end = ptr + f_dhdr.s_size;
  601.   write_segment (new, ptr, end);
  602.  
  603. #else /* if not COFF */
  604.  
  605. /* Some machines count the header as part of the text segment.
  606.    That is to say, the header appears in core
  607.    just before the address that start_of_text () returns.
  608.    For them, N_TXTOFF is the place where the header goes.
  609.    We must adjust the seek to the place after the header.
  610.    Note that at this point hdr.a_text does *not* count
  611.    the extra A_TEXT_OFFSET bytes, only the actual bytes of code.  */
  612.  
  613. #ifdef A_TEXT_SEEK
  614.   lseek (new, (long) A_TEXT_SEEK (hdr), 0);
  615. #else
  616.   lseek (new, (long) N_TXTOFF (hdr), 0);
  617. #endif /* no A_TEXT_SEEK */
  618.  
  619.   ptr = (char *) unexec_text_start;
  620.   end = ptr + hdr.a_text;
  621.   write_segment (new, ptr, end);
  622.  
  623.   ptr = (char *) unexec_data_start;
  624.   end = ptr + hdr.a_data;
  625. /*  This lseek is certainly incorrect when A_TEXT_OFFSET
  626.     and I believe it is a no-op otherwise.
  627.     Let's see if its absence ever fails.  */
  628. /*  lseek (new, (long) N_TXTOFF (hdr) + hdr.a_text, 0); */
  629.   write_segment (new, ptr, end);
  630.  
  631. #endif /* not COFF */
  632.  
  633.   return 0;
  634. }
  635.  
  636. write_segment (new, ptr, end)
  637.      int new;
  638.      register char *ptr, *end;
  639. {
  640.   register int i, nwrite, ret;
  641.   char buf[80];
  642.   extern int errno;
  643.   char zeros[128];
  644.  
  645.   bzero (zeros, sizeof zeros);
  646.  
  647.   for (i = 0; ptr < end;)
  648.     {
  649.       /* distance to next multiple of 128.  */
  650.       nwrite = (((int) ptr + 128) & -128) - (int) ptr;
  651.       /* But not beyond specified end.  */
  652.       if (nwrite > end - ptr) nwrite = end - ptr;
  653.       ret = write (new, ptr, nwrite);
  654.       /* If write gets a page fault, it means we reached
  655.      a gap between the old text segment and the old data segment.
  656.      This gap has probably been remapped into part of the text segment.
  657.      So write zeros for it.  */
  658.       if (ret == -1 && errno == EFAULT)
  659.     write (new, zeros, nwrite);
  660.       else if (nwrite != ret)
  661.     {
  662.       sprintf (buf,
  663.            "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d",
  664.            ptr, new, nwrite, ret, errno);
  665.       PERROR (buf);
  666.     }
  667.       i += nwrite;
  668.       ptr += nwrite;
  669.     }
  670. }
  671.  
  672. /* ****************************************************************
  673.  * copy_sym
  674.  *
  675.  * Copy the relocation information and symbol table from the a.out to the new
  676.  */
  677. static int
  678. copy_sym (new, a_out, a_name, new_name)
  679.      int new, a_out;
  680.      char *a_name, *new_name;
  681. {
  682.   char page[1024];
  683.   int n;
  684.  
  685.   if (a_out < 0)
  686.     return 0;
  687.  
  688. #ifdef COFF
  689.   if (SYMS_START == 0L)
  690.     return 0;
  691. #endif  /* COFF */
  692.  
  693. #ifdef COFF
  694.   if (lnnoptr)            /* if there is line number info */
  695.     lseek (a_out, lnnoptr, 0);    /* start copying from there */
  696.   else
  697. #endif /* COFF */
  698.     lseek (a_out, SYMS_START, 0);    /* Position a.out to symtab. */
  699.  
  700.   while ((n = read (a_out, page, sizeof page)) > 0)
  701.     {
  702.       if (write (new, page, n) != n)
  703.     {
  704.       PERROR (new_name);
  705.     }
  706.     }
  707.   if (n < 0)
  708.     {
  709.       PERROR (a_name);
  710.     }
  711.   return 0;
  712. }
  713.  
  714. /* ****************************************************************
  715.  * mark_x
  716.  *
  717.  * After succesfully building the new a.out, mark it executable
  718.  */
  719. static
  720. mark_x (name)
  721.      char *name;
  722. {
  723.   struct stat sbuf;
  724.   int um;
  725.   int new = 0;  /* for PERROR */
  726.  
  727.   um = umask (777);
  728.   umask (um);
  729.   if (stat (name, &sbuf) == -1)
  730.     {
  731.       PERROR (name);
  732.     }
  733.   sbuf.st_mode |= 0111 & ~um;
  734.   if (chmod (name, sbuf.st_mode) == -1)
  735.     PERROR (name);
  736. }
  737.  
  738. /*
  739.  *    If the COFF file contains a symbol table and a line number section,
  740.  *    then any auxiliary entries that have values for x_lnnoptr must
  741.  *    be adjusted by the amount that the line number section has moved
  742.  *    in the file (bias computed in make_hdr).  The #@$%&* designers of
  743.  *    the auxiliary entry structures used the absolute file offsets for
  744.  *    the line number entry rather than an offset from the start of the
  745.  *    line number section!
  746.  *
  747.  *    When I figure out how to scan through the symbol table and pick out
  748.  *    the auxiliary entries that need adjustment, this routine will
  749.  *    be fixed.  As it is now, all such entries are wrong and sdb
  750.  *    will complain.   Fred Fish, UniSoft Systems Inc.
  751.  */
  752.  
  753. #ifdef COFF
  754.  
  755. /* This function is probably very slow.  Instead of reopening the new
  756.    file for input and output it should copy from the old to the new
  757.    using the two descriptors already open (WRITEDESC and READDESC).
  758.    Instead of reading one small structure at a time it should use
  759.    a reasonable size buffer.  But I don't have time to work on such
  760.    things, so I am installing it as submitted to me.  -- RMS.  */
  761.  
  762. adjust_lnnoptrs (writedesc, readdesc, new_name)
  763.      int writedesc;
  764.      int readdesc;
  765.      char *new_name;
  766. {
  767. #if 0
  768.   register int nsyms;
  769.   register int new;
  770. #ifdef amdahl_uts
  771.   SYMENT symentry;
  772.   AUXENT auxentry;
  773. #else
  774.   struct syment symentry;
  775.   struct auxent auxentry;
  776. #endif
  777.  
  778.   if (!lnnoptr || !f_hdr.f_symptr)
  779.     return 0;
  780.  
  781.   if ((new = open (new_name, 2)) < 0)
  782.     {
  783.       PERROR (new_name);
  784.       return -1;
  785.     }
  786.  
  787.   lseek (new, f_hdr.f_symptr, 0);
  788.   for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
  789.     {
  790.       read (new, &symentry, SYMESZ);
  791.       if (symentry.n_numaux)
  792.     {
  793.       read (new, &auxentry, AUXESZ);
  794.       nsyms++;
  795.       if (ISFCN (symentry.n_type)) {
  796.         auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
  797.         lseek (new, -AUXESZ, 1);
  798.         write (new, &auxentry, AUXESZ);
  799.       }
  800.     }
  801.     }
  802.   close (new);
  803. #endif 0
  804. }
  805.  
  806. #endif /* COFF */
  807.  
  808. #endif /* not CANNOT_UNEXEC */
  809. #endif /* not CANNOT_DUMP */
  810.