home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / unexec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-10  |  23.6 KB  |  890 lines

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