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