home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / plbin.zip / pl / src / gnu / unexec.c < prev    next >
Text File  |  1992-05-26  |  29KB  |  972 lines

  1. /* Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
  2.  
  3.                NO WARRANTY
  4.  
  5.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  6. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  7. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  8. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  9. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  10. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  11. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  12. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  13. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  14. CORRECTION.
  15.  
  16.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  17. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  18. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  19. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  20. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  21. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  22. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  23. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  24. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  25. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  26.  
  27.         GENERAL PUBLIC LICENSE TO COPY
  28.  
  29.   1. You may copy and distribute verbatim copies of this source file
  30. as you receive it, in any medium, provided that you conspicuously and
  31. appropriately publish on each copy a valid copyright notice "Copyright
  32. (C) 1987 Free Software Foundation, Inc."; and include following the
  33. copyright notice a verbatim copy of the above disclaimer of warranty
  34. and of this License.  You may charge a distribution fee for the
  35. physical act of transferring a copy.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be licensed at no charge to all
  47.     third parties on terms identical to those contained in this
  48.     License Agreement (except that you may choose to grant more extensive
  49.     warranty protection to some or all third parties, at your option).
  50.  
  51.     c) You may charge a distribution fee for the physical act of
  52.     transferring a copy, and you may at your option offer warranty
  53.     protection in exchange for a fee.
  54.  
  55. Mere aggregation of another unrelated program with this program (or its
  56. derivative) on a volume of a storage or distribution medium does not bring
  57. the other program under the scope of these terms.
  58.  
  59.   3. You may copy and distribute this program (or a portion or derivative
  60. of it, under Paragraph 2) in object code or executable form under the terms
  61. of Paragraphs 1 and 2 above provided that you also do one of the following:
  62.  
  63.     a) accompany it with the complete corresponding machine-readable
  64.     source code, which must be distributed under the terms of
  65.     Paragraphs 1 and 2 above; or,
  66.  
  67.     b) accompany it with a written offer, valid for at least three
  68.     years, to give any third party free (except for a nominal
  69.     shipping charge) a complete machine-readable copy of the
  70.     corresponding source code, to be distributed under the terms of
  71.     Paragraphs 1 and 2 above; or,
  72.  
  73.     c) accompany it with the information you received as to where the
  74.     corresponding source code may be obtained.  (This alternative is
  75.     allowed only for noncommercial distribution and only if you
  76.     received the program in object code or executable form alone.)
  77.  
  78. For an executable file, complete source code means all the source code for
  79. all modules it contains; but, as a special exception, it need not include
  80. source code for modules which are standard libraries that accompany the
  81. operating system on which the executable file runs.
  82.  
  83.   4. You may not copy, sublicense, distribute or transfer this program
  84. except as expressly provided under this License Agreement.  Any attempt
  85. otherwise to copy, sublicense, distribute or transfer this program is void and
  86. your rights to use the program under this License agreement shall be
  87. automatically terminated.  However, parties who have received computer
  88. software programs from you with this License Agreement will not have
  89. their licenses terminated so long as such parties remain in full compliance.
  90.  
  91.   5. If you wish to incorporate parts of this program into other free
  92. programs whose distribution conditions are different, write to the Free
  93. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  94. worked out a simple rule that can be stated here, but we will often permit
  95. this.  We will be guided by the two goals of preserving the free status of
  96. all derivatives of our free software and of promoting the sharing and reuse of
  97. software.
  98.  
  99.  
  100. In other words, you are welcome to use, share and improve this program.
  101. You are forbidden to forbid anyone else to use, share and improve
  102. what you give them.   Help stamp out software-hoarding!  */
  103.  
  104.  
  105. /*
  106.  * unexec.c - Convert a running program into an a.out file.
  107.  *
  108.  * Author:    Spencer W. Thomas
  109.  *         Computer Science Dept.
  110.  *         University of Utah
  111.  * Date:    Tue Mar  2 1982
  112.  * Modified heavily since then.
  113.  *
  114.  * Synopsis:
  115.  *    unexec (new_name, a_name, data_start, bss_start, entry_address)
  116.  *    char *new_name, *a_name;
  117.  *    unsigned data_start, bss_start, entry_address;
  118.  *
  119.  * Takes a snapshot of the program and makes an a.out format file in the
  120.  * file named by the string argument new_name.
  121.  * If a_name is non-NULL, the symbol table will be taken from the given file.
  122.  * On some machines, an existing a_name file is required.
  123.  *
  124.  * The boundaries within the a.out file may be adjusted with the data_start
  125.  * and bss_start arguments.  Either or both may be given as 0 for defaults.
  126.  *
  127.  * Data_start gives the boundary between the text segment and the data
  128.  * segment of the program.  The text segment can contain shared, read-only
  129.  * program code and literal data, while the data segment is always unshared
  130.  * and unprotected.  Data_start gives the lowest unprotected address.
  131.  * The value you specify may be rounded down to a suitable boundary
  132.  * as required by the machine you are using.
  133.  *
  134.  * Specifying zero for data_start means the boundary between text and data
  135.  * should not be the same as when the program was loaded.
  136.  * If NO_REMAP is defined, the argument data_start is ignored and the
  137.  * segment boundaries are never changed.
  138.  *
  139.  * Bss_start indicates how much of the data segment is to be saved in the
  140.  * a.out file and restored when the program is executed.  It gives the lowest
  141.  * unsaved address, and is rounded up to a page boundary.  The default when 0
  142.  * is given assumes that the entire data segment is to be stored, including
  143.  * the previous data and bss as well as any additional storage allocated with
  144.  * break (2).
  145.  *
  146.  * The new file is set up to start at entry_address.
  147.  *
  148.  * If you make improvements I'd like to get them too.
  149.  * harpo!utah-cs!thomas, thomas@Utah-20
  150.  *
  151.  */
  152.  
  153. /* There are several compilation parameters affecting unexec:
  154.  
  155. * COFF
  156.  
  157. Define this if your system uses COFF for executables.
  158. Otherwise we assume you use Berkeley format.
  159.  
  160. * NO_REMAP
  161.  
  162. Define this if you do not want to try to save Emacs's pure data areas
  163. as part of the text segment.
  164.  
  165. Saving them as text is good because it allows users to share more.
  166.  
  167. However, on machines that locate the text area far from the data area,
  168. the boundary cannot feasibly be moved.  Such machines require
  169. NO_REMAP.
  170.  
  171. Also, remapping can cause trouble with the built-in startup routine
  172. /lib/crt0.o, which defines `environ' as an initialized variable.
  173. Dumping `environ' as pure does not work!  So, to use remapping,
  174. you must write a startup routine for your machine in Emacs's crt0.c.
  175. If NO_REMAP is defined, Emacs uses the system's crt0.o.
  176.  
  177. * SECTION_ALIGNMENT
  178.  
  179. Some machines that use COFF executables require that each section
  180. start on a certain boundary *in the COFF file*.  Such machines should
  181. define SECTION_ALIGNMENT to a mask of the low-order bits that must be
  182. zero on such a boundary.  This mask is used to control padding between
  183. segments in the COFF file.
  184.  
  185. If SECTION_ALIGNMENT is not defined, the segments are written
  186. consecutively with no attempt at alignment.  This is right for
  187. unmodified system V.
  188.  
  189. * SEGMENT_MASK
  190.  
  191. Some machines require that the beginnings and ends of segments
  192. *in core* be on certain boundaries.  For most machines, a page
  193. boundary is sufficient.  That is the default.  When a larger
  194. boundary is needed, define SEGMENT_MASK to a mask of
  195. the bits that must be zero on such a boundary.
  196.  
  197. * A_TEXT_OFFSET(HDR)
  198.  
  199. Some machines count the a.out header as part of the size of the text
  200. segment (a_text); they may actually load the header into core as the
  201. first data in the text segment.  Some have additional padding between
  202. the header and the real text of the program that is counted in a_text.
  203.  
  204. For these machines, define A_TEXT_OFFSET(HDR) to examine the header
  205. structure HDR and return the number of bytes to add to `a_text'
  206. before writing it (above and beyond the number of bytes of actual
  207. program text).  HDR's standard fields are already correct, except that
  208. this adjustment to the `a_text' field has not yet been made;
  209. thus, the amount of offset can depend on the data in the file.
  210.   
  211. * A_TEXT_SEEK(HDR)
  212.  
  213. If defined, this macro specifies the number of bytes to seek into the
  214. a.out file before starting to write the text segment.a
  215.  
  216. * EXEC_MAGIC
  217.  
  218. For machines using COFF, this macro, if defined, is a value stored
  219. into the magic number field of the output file.
  220.  
  221. * ADJUST_EXEC_HEADER
  222.  
  223. This macro can be used to generate statements to adjust or
  224. initialize nonstandard fields in the file header
  225.  
  226. * ADDR_CORRECT(ADDR)
  227.  
  228. Macro to correct an int which is the bit pattern of a pointer to a byte
  229. into an int which is the number of a byte.
  230.  
  231. This macro has a default definition which is usually right.
  232. This default definition is a no-op on most machines (where a
  233. pointer looks like an int) but not on all machines.
  234.  
  235. */
  236.  
  237. /* This is a MODIFIED version of GNU-emacs unexec.c.  Modifications include:
  238.    - Some real modifications to facilate the interface to SWI-Prolog, tagged
  239.      with (JW)
  240.    - Added some declarations to make GCC -Wall quiet
  241. */
  242.  
  243. #ifndef emacs
  244. #ifndef PERROR                /* added this condition (JW) */
  245. #define PERROR(arg) perror (arg); return -1
  246. #endif
  247. #else
  248. #include "config.h"
  249. #define PERROR(file) report_error (file, new)
  250. #endif
  251.  
  252. #ifndef CANNOT_DUMP  /* all rest of file!  */
  253.  
  254. #ifndef CANNOT_UNEXEC /* most of rest of file */
  255.  
  256. /* #include <a.out.h>            Deleted; already done (JW) */
  257. /* Define getpagesize () if the system does not.
  258.    Note that this may depend on symbols defined in a.out.h
  259.  */
  260. #include "getpagesize.h"
  261.  
  262. #ifndef makedev            /* Try to detect types.h already loaded */
  263. #include <sys/types.h>
  264. #endif
  265. #include <stdio.h>
  266. #include <sys/stat.h>
  267. #include <errno.h>
  268. #if !gould
  269. #include <unistd.h>
  270. #endif
  271.  
  272. #if !ANSI
  273. extern int creat();            /* Unix functions (added by (JW)) */
  274. extern int unlink();
  275. extern int stat();
  276. extern int chmod();
  277. #endif
  278.  
  279. static int make_hdr();
  280. static int copy_text_and_data();
  281. static int copy_sym();
  282. static int mark_x();
  283. static int write_segment();
  284.  
  285. #ifdef COFF
  286. #ifndef USG
  287. #ifndef STRIDE
  288. #ifndef UMAX
  289. #ifndef sun386
  290. #ifndef gould
  291. /* I have a suspicion that these are turned off on all systems
  292.    and can be deleted.  Try it in version 19.  */
  293. #include <filehdr.h>
  294. #include <aouthdr.h>
  295. #include <scnhdr.h>
  296. #include <syms.h>
  297. #endif /* not gould */
  298. #endif /* not sun386 */
  299. #endif /* not UMAX */
  300. #endif /* Not STRIDE */
  301. #endif /* not USG */
  302. static long block_copy_start;        /* Old executable start point */
  303. static struct filehdr f_hdr;        /* File header */
  304. static struct aouthdr f_ohdr;        /* Optional file header (a.out) */
  305. long bias;            /* Bias to add for growth */
  306. long lnnoptr;            /* Pointer to line-number info within file */
  307. #define SYMS_START block_copy_start
  308.  
  309. static long text_scnptr;
  310. static long data_scnptr;
  311.  
  312. #else /* not COFF */
  313.  
  314. extern char *sbrk ();
  315.  
  316. #define SYMS_START ((long) N_SYMOFF (shdr))
  317.  
  318. /* Some machines override the structure name for an a.out header.  */
  319. #ifndef EXEC_HDR_TYPE
  320. #define EXEC_HDR_TYPE struct exec
  321. #endif
  322.  
  323. #ifdef HPUX
  324. #ifdef HP9000S200_ID
  325. #define MY_ID HP9000S200_ID
  326. #else
  327. #include <model.h>
  328. #define MY_ID MYSYS
  329. #endif /* no HP9000S200_ID */
  330. static MAGIC OLDMAGIC = {MY_ID, SHARE_MAGIC};
  331. static MAGIC NEWMAGIC = {MY_ID, DEMAND_MAGIC};
  332. #define N_TXTOFF(x) TEXT_OFFSET(x)
  333. #define N_SYMOFF(x) LESYM_OFFSET(x)
  334. static EXEC_HDR_TYPE hdr, ohdr, shdr;
  335.  
  336. #else /* not HPUX */
  337.  
  338. #if defined (USG) && !defined (IBMRTAIX) && !defined (IRIS)
  339. static struct bhdr hdr, ohdr, shdr;
  340. #define a_magic fmagic
  341. #define a_text tsize
  342. #define a_data dsize
  343. #define a_bss bsize
  344. #define a_syms ssize
  345. #define a_trsize rtsize
  346. #define a_drsize rdsize
  347. #define a_entry entry
  348. #define    N_BADMAG(x) \
  349.     (((x).fmagic)!=OMAGIC && ((x).fmagic)!=NMAGIC &&\
  350.      ((x).fmagic)!=FMAGIC && ((x).fmagic)!=IMAGIC)
  351. #define NEWMAGIC FMAGIC
  352. #else /* IRIS or IBMRTAIX or not USG */
  353. static EXEC_HDR_TYPE hdr, ohdr, shdr;
  354. #define NEWMAGIC ZMAGIC
  355. #endif /* IRIS or IBMRTAIX not USG */
  356. #endif /* not HPUX */
  357.  
  358. static int unexec_text_start;
  359. static int unexec_data_start;
  360.  
  361. #endif /* not COFF */
  362.  
  363. static int pagemask;
  364.  
  365. /* Correct an int which is the bit pattern of a pointer to a byte
  366.    into an int which is the number of a byte.
  367.    This is a no-op on ordinary machines, but not on all.  */
  368.  
  369. #ifndef ADDR_CORRECT   /* Let m-*.h files override this definition */
  370. #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
  371. #endif
  372.  
  373. #ifdef emacs
  374.  
  375. static
  376. report_error (file, fd)
  377.      char *file;
  378.      int fd;
  379. {
  380.   if (fd)
  381.     close (fd);
  382.   error ("Failure operating on %s", file);
  383. }
  384. #endif /* emacs */
  385.  
  386. #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
  387. #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
  388. #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
  389.  
  390. static void
  391. report_error_1 (fd, msg, a1, a2)
  392.      int fd;
  393.      char *msg;
  394.      int a1, a2;
  395. {
  396.   close (fd);
  397. #ifdef emacs
  398.   error (msg, a1, a2);
  399. #else
  400.   fprintf (stderr, msg, a1, a2);
  401.   fprintf (stderr, "\n");
  402. #endif
  403. }
  404.  
  405. /* ****************************************************************
  406.  * unexec
  407.  *
  408.  * driving logic.
  409.  */
  410. int
  411. unexec (new_name, a_name, s_name, data_start, bss_start, entry_address)
  412.      char *new_name, *a_name, *s_name;
  413.      unsigned data_start, bss_start, entry_address;
  414. {
  415.   int new, s_out = -1, a_out = -1;
  416.  
  417.   if (a_name && (a_out = open (a_name, 0)) < 0)
  418.     {
  419.       PERROR (a_name);
  420.     }
  421.   if (s_name && (s_out = open (s_name, 0)) < 0 )
  422.     {
  423.       PERROR (s_name);
  424.     }
  425.   if ((new = creat (new_name, 0666)) < 0)
  426.     {
  427.       PERROR (new_name);
  428.     }
  429.  
  430.   if (make_hdr (new, a_out, s_out, data_start, bss_start, entry_address,
  431.         a_name, s_name, new_name) < 0
  432.       || copy_text_and_data (new) < 0
  433.       || copy_sym (new, s_out >= 0 ? s_out : a_out, a_name, new_name) < 0
  434. #ifdef COFF
  435.       || adjust_lnnoptrs (new, a_out, new_name) < 0
  436. #endif
  437.       )
  438.     {
  439.       close (new);
  440.       unlink (new_name);            /* Failed, unlink new a.out */
  441.       return -1;    
  442.     }
  443.  
  444.   close (new);
  445.   if (a_out >= 0)
  446.     close (a_out);
  447.   if (s_out >= 0)
  448.     close (s_out);
  449.   mark_x (new_name);
  450.   return 0;
  451. }
  452.  
  453. /* ****************************************************************
  454.  * make_hdr
  455.  *
  456.  * Make the header in the new a.out from the header in core.
  457.  * Modify the text and data sizes.
  458.  */
  459. static int
  460. make_hdr (new, a_out, s_out, data_start, bss_start, entry_address, a_name, s_name, new_name)
  461.      int new, a_out, s_out;
  462.      unsigned data_start, bss_start, entry_address;
  463.      char *a_name, *s_name;
  464.      char *new_name;
  465. {
  466. #ifdef COFF
  467.   auto struct scnhdr f_thdr;        /* Text section header */
  468.   auto struct scnhdr f_dhdr;        /* Data section header */
  469.   auto struct scnhdr f_bhdr;        /* Bss section header */
  470.   auto struct scnhdr scntemp;        /* Temporary section header */
  471.   register int scns;
  472. #endif /* COFF */
  473.   unsigned int bss_end;
  474.  
  475.   pagemask = getpagesize () - 1;
  476.  
  477.   /* Adjust text/data boundary. */
  478. #ifdef NO_REMAP
  479.   data_start = (int) start_of_data ();
  480. #else /* not NO_REMAP */
  481.   if (!data_start)
  482.     data_start = (int) start_of_data ();
  483. #endif /* not NO_REMAP */
  484.   data_start = ADDR_CORRECT (data_start);
  485.  
  486. #ifdef SEGMENT_MASK
  487.   data_start = data_start & ~SEGMENT_MASK; /* (Down) to segment boundary. */
  488. #else
  489.   data_start = data_start & ~pagemask; /* (Down) to page boundary. */
  490. #endif
  491.  
  492.   bss_end = ADDR_CORRECT (sbrk (0)) + pagemask;
  493.   bss_end &= ~ pagemask;
  494.  
  495.   /* Adjust data/bss boundary. */
  496.   if (bss_start != 0)
  497.     {
  498.       bss_start = (ADDR_CORRECT (bss_start) + pagemask);
  499.       /* (Up) to page bdry. */
  500.       bss_start &= ~ pagemask;
  501.       if (bss_start > bss_end)
  502.     {
  503.       ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
  504.           bss_start);
  505.     }
  506.     }
  507.   else
  508.     bss_start = bss_end;
  509.  
  510.   if (data_start > bss_start)    /* Can't have negative data size. */
  511.     {
  512.       ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
  513.           data_start, bss_start);
  514.     }
  515.  
  516. #ifdef COFF
  517.   /* Salvage as much info from the existing file as possible */
  518.   if (a_out >= 0)
  519.     {
  520.       if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
  521.     {
  522.       PERROR (a_name);
  523.     }
  524.       block_copy_start += sizeof (f_hdr);
  525.       if (f_hdr.f_opthdr > 0)
  526.     {
  527.       if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
  528.         {
  529.           PERROR (a_name);
  530.         }
  531.       block_copy_start += sizeof (f_ohdr);
  532.     }
  533.       /* Loop through section headers, copying them in */
  534.       for (scns = f_hdr.f_nscns; scns > 0; scns--) {
  535.     if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
  536.       {
  537.         PERROR (a_name);
  538.       }
  539.     if (scntemp.s_scnptr > 0L)
  540.       {
  541.             if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
  542.           block_copy_start = scntemp.s_scnptr + scntemp.s_size;
  543.       }
  544.     if (strcmp (scntemp.s_name, ".text") == 0)
  545.       {
  546.         f_thdr = scntemp;
  547.       }
  548.     else if (strcmp (scntemp.s_name, ".data") == 0)
  549.       {
  550.         f_dhdr = scntemp;
  551.       }
  552.     else if (strcmp (scntemp.s_name, ".bss") == 0)
  553.       {
  554.         f_bhdr = scntemp;
  555.       }
  556.       }
  557.     }
  558.   else
  559.     {
  560.       ERROR0 ("can't build a COFF file from scratch yet");
  561.     }
  562.  
  563.   /* Now we alter the contents of all the f_*hdr variables
  564.      to correspond to what we want to dump.  */
  565.  
  566.   f_hdr.f_flags |= (F_RELFLG | F_EXEC);
  567. #ifdef EXEC_MAGIC
  568.   f_ohdr.magic = EXEC_MAGIC;
  569. #endif
  570. #ifndef NO_REMAP
  571.   f_ohdr.text_start = (long) start_of_text ();
  572.   f_ohdr.tsize = data_start - f_ohdr.text_start;
  573.   f_ohdr.data_start = data_start;
  574. #endif /* NO_REMAP */
  575.   f_ohdr.dsize = bss_start - f_ohdr.data_start;
  576.   f_ohdr.bsize = bss_end - bss_start;
  577.   f_thdr.s_size = f_ohdr.tsize;
  578.   f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
  579.   f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
  580.   lnnoptr = f_thdr.s_lnnoptr;
  581. #ifdef SECTION_ALIGNMENT
  582.   /* Some systems require special alignment
  583.      of the sections in the file itself.  */
  584.   f_thdr.s_scnptr
  585.     = (f_thdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT;
  586. #endif /* SECTION_ALIGNMENT */
  587.   text_scnptr = f_thdr.s_scnptr;
  588.   f_dhdr.s_paddr = f_ohdr.data_start;
  589.   f_dhdr.s_vaddr = f_ohdr.data_start;
  590.   f_dhdr.s_size = f_ohdr.dsize;
  591.   f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
  592. #ifdef SECTION_ALIGNMENT
  593.   /* Some systems require special alignment
  594.      of the sections in the file itself.  */
  595.   f_dhdr.s_scnptr
  596.     = (f_dhdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT;
  597. #endif /* SECTION_ALIGNMENT */
  598. #ifdef DATA_SECTION_ALIGNMENT
  599.   /* Some systems require special alignment
  600.      of the data section only.  */
  601.   f_dhdr.s_scnptr
  602.     = (f_dhdr.s_scnptr + DATA_SECTION_ALIGNMENT) & ~DATA_SECTION_ALIGNMENT;
  603. #endif /* DATA_SECTION_ALIGNMENT */
  604.   data_scnptr = f_dhdr.s_scnptr;
  605.   f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
  606.   f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
  607.   f_bhdr.s_size = f_ohdr.bsize;
  608.   f_bhdr.s_scnptr = 0L;
  609.   bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
  610.  
  611.   if (f_hdr.f_symptr > 0L)
  612.     {
  613.       f_hdr.f_symptr += bias;
  614.     }
  615.  
  616.   if (f_thdr.s_lnnoptr > 0L)
  617.     {
  618.       f_thdr.s_lnnoptr += bias;
  619.     }
  620.  
  621. #ifdef ADJUST_EXEC_HEADER
  622.   ADJUST_EXEC_HEADER
  623. #endif /* ADJUST_EXEC_HEADER */
  624.  
  625.   if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
  626.     {
  627.       PERROR (new_name);
  628.     }
  629.  
  630.   if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
  631.     {
  632.       PERROR (new_name);
  633.     }
  634.  
  635.   if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
  636.     {
  637.       PERROR (new_name);
  638.     }
  639.  
  640.   if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
  641.     {
  642.       PERROR (new_name);
  643.     }
  644.  
  645.   if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
  646.     {
  647.       PERROR (new_name);
  648.     }
  649.   return (0);
  650.  
  651. #else /* if not COFF */
  652.  
  653.   /* Get symbol table info from header of a.out file if given one. */
  654.   if (a_out >= 0)
  655.     {
  656.       if (read (a_out, &ohdr, sizeof hdr) != sizeof hdr)
  657.     {
  658.       PERROR (a_name);
  659.     }
  660.  
  661.       if (N_BADMAG (ohdr))
  662.     {
  663.       ERROR1 ("invalid magic number in %s", a_name);
  664.     }
  665.       hdr = ohdr;
  666.     }
  667.   else
  668.     {
  669.       bzero (hdr, sizeof hdr);
  670.     }
  671.  
  672.   if ( s_out >= 0 )
  673.     { if ( read(s_out, &shdr, sizeof(hdr)) != sizeof(hdr) )
  674.     {
  675.       PERROR(s_name);
  676.     }
  677.       hdr.a_syms = shdr.a_syms;        /* copy symbol table sizes */      
  678.     }
  679.   else
  680.     {
  681.       shdr = ohdr;
  682.     }
  683.  
  684.   unexec_text_start = (long) start_of_text (&ohdr); /* Added &ohdr (JW) */
  685.   unexec_data_start = data_start;
  686.  
  687.   /* Machine-dependent fixup for header, or maybe for unexec_text_start */
  688. #ifdef ADJUST_EXEC_HEADER
  689.   ADJUST_EXEC_HEADER;
  690. #endif /* ADJUST_EXEC_HEADER */
  691.  
  692.   hdr.a_trsize = 0;
  693.   hdr.a_drsize = 0;
  694.   if (entry_address != 0)
  695.     hdr.a_entry = entry_address;
  696.  
  697.   hdr.a_bss = bss_end - bss_start;
  698.   hdr.a_data = bss_start - data_start;
  699. #ifdef NO_REMAP
  700.   hdr.a_text = ohdr.a_text;
  701. #else /* not NO_REMAP */
  702.   hdr.a_text = data_start - unexec_text_start;
  703.  
  704. #ifdef A_TEXT_OFFSET
  705.   hdr.a_text += A_TEXT_OFFSET (ohdr);
  706. #endif
  707.  
  708. #endif /* not NO_REMAP */
  709.  
  710.   if (write (new, &hdr, sizeof hdr) != sizeof hdr)
  711.     {
  712.       PERROR (new_name);
  713.     }
  714.  
  715. #ifdef A_TEXT_OFFSET
  716.   hdr.a_text -= A_TEXT_OFFSET (ohdr);
  717. #endif
  718.  
  719.   return 0;
  720.  
  721. #endif /* not COFF */
  722. }
  723.  
  724. /* ****************************************************************
  725.  * copy_text_and_data
  726.  *
  727.  * Copy the text and data segments from memory to the new a.out
  728.  */
  729. static int
  730. copy_text_and_data (new)
  731.      int new;
  732. {
  733.   register char *end;
  734.   register char *ptr;
  735.  
  736. #ifdef COFF
  737.   lseek (new, (long) text_scnptr, 0);
  738.   ptr = (char *) f_ohdr.text_start;
  739.   end = ptr + f_ohdr.tsize;
  740.   write_segment (new, ptr, end);
  741.  
  742.   lseek (new, (long) data_scnptr, 0);
  743.   ptr = (char *) f_ohdr.data_start;
  744.   end = ptr + f_ohdr.dsize;
  745.   write_segment (new, ptr, end);
  746.  
  747. #else /* if not COFF */
  748.  
  749. /* Some machines count the header as part of the text segment.
  750.    That is to say, the header appears in core
  751.    just before the address that start_of_text () returns.
  752.    For them, N_TXTOFF is the place where the header goes.
  753.    We must adjust the seek to the place after the header.
  754.    Note that at this point hdr.a_text does *not* count
  755.    the extra A_TEXT_OFFSET bytes, only the actual bytes of code.  */
  756.  
  757. #ifdef A_TEXT_SEEK
  758.   lseek (new, (long) A_TEXT_SEEK (hdr), 0);
  759. #else
  760. #ifdef A_TEXT_OFFSET
  761.   /* Note that on the Sequent machine A_TEXT_OFFSET != sizeof (hdr)
  762.      and sizeof (hdr) is the correct amount to add here.  */
  763.   /* In version 19, eliminate this case and use A_TEXT_SEEK whenever
  764.      N_TXTOFF is not right.  */
  765.   lseek (new, (long) N_TXTOFF (hdr) + sizeof (hdr), 0);
  766. #else
  767.   lseek (new, (long) N_TXTOFF (hdr), 0);
  768. #endif /* no A_TEXT_OFFSET */
  769. #endif /* no A_TEXT_SEEK */
  770.  
  771.   ptr = (char *) unexec_text_start;
  772.   end = ptr + hdr.a_text;
  773.   Putf("text: %ld ... ", end - ptr); pl_flush();
  774.   write_segment (new, ptr, end);
  775.  
  776.   ptr = (char *) unexec_data_start;
  777.   end = ptr + hdr.a_data;
  778. /*  This lseek is certainly incorrect when A_TEXT_OFFSET
  779.     and I believe it is a no-op otherwise.
  780.     Let's see if its absence ever fails.  */
  781. /*  lseek (new, (long) N_TXTOFF (hdr) + hdr.a_text, 0); */
  782.   Putf("data: %ld ... ", end - ptr); pl_flush();
  783. #ifdef HPUX
  784.   lseek(new, DATA_OFFSET(hdr), 0);    /* Added (JW) */
  785. #endif
  786.   write_segment (new, ptr, end);
  787.  
  788. #endif /* not COFF */
  789.  
  790.   return 0;
  791. }
  792.  
  793. static int
  794. write_segment (new, ptr, end)
  795.      int new;
  796.      register char *ptr, *end;
  797. {
  798.   register int i, nwrite, ret;
  799.   char buf[80];
  800.   extern int errno;
  801.   char zeros[128];
  802.  
  803.   bzero (zeros, sizeof zeros);
  804.  
  805.   for (i = 0; ptr < end;)
  806.     {
  807.       /* distance to next multiple of 128.  */
  808.       nwrite = (((int) ptr + 128) & -128) - (int) ptr;
  809.       /* But not beyond specified end.  */
  810.       if (nwrite > end - ptr) nwrite = end - ptr;
  811.       ret = write (new, ptr, nwrite);
  812.       /* If write gets a page fault, it means we reached
  813.      a gap between the old text segment and the old data segment.
  814.      This gap has probably been remapped into part of the text segment.
  815.      So write zeros for it.  */
  816.       if (ret == -1 && errno == EFAULT)
  817.     write (new, zeros, nwrite);
  818.       else if (nwrite != ret)
  819.     {
  820.       sprintf (buf,
  821.            "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d",
  822.            (unsigned) ptr, new, nwrite, ret, errno);
  823.       PERROR (buf);
  824.     }
  825.       i += nwrite;
  826.       ptr += nwrite;
  827.     }
  828.  
  829.   return 0;
  830. }
  831.  
  832. /* ****************************************************************
  833.  * copy_sym
  834.  *
  835.  * Copy the relocation information and symbol table from the a.out to the new
  836.  */
  837. static int
  838. copy_sym (new, s_out, a_name, new_name)
  839.      int new, s_out;
  840.      char *a_name, *new_name;
  841. {
  842.   char page[1024];
  843.   int n;
  844.  
  845.   if (s_out < 0)
  846.     return 0;
  847.  
  848. #ifdef COFF
  849.   if (SYMS_START == 0L)
  850.     return 0;
  851. #endif  /* COFF */
  852.  
  853. #ifdef COFF
  854.   if (lnnoptr)            /* if there is line number info */
  855.     lseek (s_out, lnnoptr, 0);    /* start copying from there */
  856.   else
  857. #endif /* COFF */
  858.     lseek (s_out, SYMS_START, 0);    /* Position a.out to symtab. */
  859.  
  860.   Putf("symbols ... "); pl_flush();
  861.   while ((n = read (s_out, page, sizeof page)) > 0)
  862.     {
  863.       if (write (new, page, n) != n)
  864.     {
  865.       PERROR (new_name);
  866.     }
  867.     }
  868.   if (n < 0)
  869.     {
  870.       PERROR (a_name);
  871.     }
  872.   return 0;
  873. }
  874.  
  875. /* ****************************************************************
  876.  * mark_x
  877.  *
  878.  * After succesfully building the new a.out, mark it executable
  879.  */
  880. static int
  881. mark_x (name)
  882.      char *name;
  883. {
  884.   struct stat sbuf;
  885.   int um;
  886. #ifdef emacs                /* (JW) */
  887.   int new = 0;  /* for PERROR */
  888. #endif
  889.  
  890.   um = umask (777);
  891.   umask (um);
  892.   if (stat (name, &sbuf) == -1)
  893.     {
  894.       PERROR (name);
  895.     }
  896.   sbuf.st_mode |= 0111 & ~um;
  897.   if (chmod (name, sbuf.st_mode) == -1)
  898.     PERROR (name);
  899. }
  900.  
  901. /*
  902.  *    If the COFF file contains a symbol table and a line number section,
  903.  *    then any auxiliary entries that have values for x_lnnoptr must
  904.  *    be adjusted by the amount that the line number section has moved
  905.  *    in the file (bias computed in make_hdr).  The #@$%&* designers of
  906.  *    the auxiliary entry structures used the absolute file offsets for
  907.  *    the line number entry rather than an offset from the start of the
  908.  *    line number section!
  909.  *
  910.  *    When I figure out how to scan through the symbol table and pick out
  911.  *    the auxiliary entries that need adjustment, this routine will
  912.  *    be fixed.  As it is now, all such entries are wrong and sdb
  913.  *    will complain.   Fred Fish, UniSoft Systems Inc.
  914.  */
  915.  
  916. #ifdef COFF
  917.  
  918. /* This function is probably very slow.  Instead of reopening the new
  919.    file for input and output it should copy from the old to the new
  920.    using the two descriptors already open (WRITEDESC and READDESC).
  921.    Instead of reading one small structure at a time it should use
  922.    a reasonable size buffer.  But I don't have time to work on such
  923.    things, so I am installing it as submitted to me.  -- RMS.  */
  924.  
  925. adjust_lnnoptrs (writedesc, readdesc, new_name)
  926.      int writedesc;
  927.      int readdesc;
  928.      char *new_name;
  929. {
  930.   register int nsyms;
  931.   register int new;
  932. #ifdef amdahl_uts
  933.   SYMENT symentry;
  934.   AUXENT auxentry;
  935. #else
  936.   struct syment symentry;
  937.   union auxent auxentry;
  938. #endif
  939.  
  940.   if (!lnnoptr || !f_hdr.f_symptr)
  941.     return 0;
  942.  
  943.   if ((new = open (new_name, 2)) < 0)
  944.     {
  945.       PERROR (new_name);
  946.       return -1;
  947.     }
  948.  
  949.   lseek (new, f_hdr.f_symptr, 0);
  950.   for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
  951.     {
  952.       read (new, &symentry, SYMESZ);
  953.       if (symentry.n_numaux)
  954.     {
  955.       read (new, &auxentry, AUXESZ);
  956.       nsyms++;
  957.       if (ISFCN (symentry.n_type)) {
  958.         auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
  959.         lseek (new, -AUXESZ, 1);
  960.         write (new, &auxentry, AUXESZ);
  961.       }
  962.     }
  963.     }
  964.   close (new);
  965. }
  966.  
  967. #endif /* COFF */
  968.  
  969. #endif /* not CANNOT_UNEXEC */
  970.  
  971. #endif /* not CANNOT_DUMP */
  972.