Comment change.
[bpt/emacs.git] / src / unexec.c
CommitLineData
3a22ee35 1/* Copyright (C) 1985,86,87,88,92,93,94 Free Software Foundation, Inc.
7dd63af1
RS
2
3This file is part of GNU Emacs.
4
5GNU Emacs is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
f31fe472 7the Free Software Foundation; either version 2, or (at your option)
7dd63af1
RS
8any later version.
9
10GNU Emacs is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GNU Emacs; see the file COPYING. If not, write to
17the 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/* Modified to support SysVr3 shared libraries by James Van Artsdalen
69 * of Dell Computer Corporation. james@bigtex.cactus.org.
70 */
71
72/* There are several compilation parameters affecting unexec:
73
74* COFF
75
76Define this if your system uses COFF for executables.
265a9e55
JB
77
78* COFF_ENCAPSULATE
79
80Define this if you are using the GNU coff encapsulated a.out format.
81This is closer to a.out than COFF. You should *not* define COFF if
82you define COFF_ENCAPSULATE
83
7dd63af1
RS
84Otherwise we assume you use Berkeley format.
85
86* NO_REMAP
87
88Define this if you do not want to try to save Emacs's pure data areas
89as part of the text segment.
90
91Saving them as text is good because it allows users to share more.
92
93However, on machines that locate the text area far from the data area,
94the boundary cannot feasibly be moved. Such machines require
95NO_REMAP.
96
97Also, remapping can cause trouble with the built-in startup routine
98/lib/crt0.o, which defines `environ' as an initialized variable.
99Dumping `environ' as pure does not work! So, to use remapping,
100you must write a startup routine for your machine in Emacs's crt0.c.
101If NO_REMAP is defined, Emacs uses the system's crt0.o.
102
103* SECTION_ALIGNMENT
104
105Some machines that use COFF executables require that each section
106start on a certain boundary *in the COFF file*. Such machines should
107define SECTION_ALIGNMENT to a mask of the low-order bits that must be
108zero on such a boundary. This mask is used to control padding between
109segments in the COFF file.
110
111If SECTION_ALIGNMENT is not defined, the segments are written
112consecutively with no attempt at alignment. This is right for
113unmodified system V.
114
115* SEGMENT_MASK
116
117Some machines require that the beginnings and ends of segments
118*in core* be on certain boundaries. For most machines, a page
119boundary is sufficient. That is the default. When a larger
120boundary is needed, define SEGMENT_MASK to a mask of
121the bits that must be zero on such a boundary.
122
123* A_TEXT_OFFSET(HDR)
124
125Some machines count the a.out header as part of the size of the text
126segment (a_text); they may actually load the header into core as the
127first data in the text segment. Some have additional padding between
128the header and the real text of the program that is counted in a_text.
129
130For these machines, define A_TEXT_OFFSET(HDR) to examine the header
131structure HDR and return the number of bytes to add to `a_text'
132before writing it (above and beyond the number of bytes of actual
133program text). HDR's standard fields are already correct, except that
134this adjustment to the `a_text' field has not yet been made;
135thus, the amount of offset can depend on the data in the file.
136
137* A_TEXT_SEEK(HDR)
138
139If defined, this macro specifies the number of bytes to seek into the
2d30a233 140a.out file before starting to write the text segment.
7dd63af1
RS
141
142* EXEC_MAGIC
143
144For machines using COFF, this macro, if defined, is a value stored
145into the magic number field of the output file.
146
147* ADJUST_EXEC_HEADER
148
149This macro can be used to generate statements to adjust or
150initialize nonstandard fields in the file header
151
152* ADDR_CORRECT(ADDR)
153
154Macro to correct an int which is the bit pattern of a pointer to a byte
155into an int which is the number of a byte.
156
157This macro has a default definition which is usually right.
158This default definition is a no-op on most machines (where a
159pointer looks like an int) but not on all machines.
160
161*/
162
163#ifndef emacs
164#define PERROR(arg) perror (arg); return -1
165#else
166#define IN_UNEXEC
18160b98 167#include <config.h>
7dd63af1
RS
168#define PERROR(file) report_error (file, new)
169#endif
170
171#ifndef CANNOT_DUMP /* all rest of file! */
172
173#ifndef CANNOT_UNEXEC /* most of rest of file */
174
265a9e55
JB
175#ifdef COFF_ENCAPSULATE
176int need_coff_header = 1;
177#include <coff-encap/a.out.encap.h> /* The location might be a poor assumption */
178#else
3680bdc6 179#ifdef MSDOS
234d3183 180#include <coff.h>
3680bdc6
RS
181#define filehdr external_filehdr
182#define scnhdr external_scnhdr
183#define syment external_syment
184#define auxent external_auxent
185#define n_numaux e_numaux
186#define n_type e_type
187struct aouthdr
188{
234d3183
RS
189 unsigned short magic; /* type of file */
190 unsigned short vstamp; /* version stamp */
191 unsigned long tsize; /* text size in bytes, padded to FW bdry*/
192 unsigned long dsize; /* initialized data " " */
193 unsigned long bsize; /* uninitialized data " " */
194 unsigned long entry; /* entry pt. */
195 unsigned long text_start;/* base of text used for this file */
196 unsigned long data_start;/* base of data used for this file */
3680bdc6
RS
197};
198
199
200#else /* not MSDOS */
7dd63af1 201#include <a.out.h>
3680bdc6 202#endif /* not MSDOS */
265a9e55
JB
203#endif
204
7dd63af1
RS
205/* Define getpagesize () if the system does not.
206 Note that this may depend on symbols defined in a.out.h
207 */
208#include "getpagesize.h"
209
210#ifndef makedev /* Try to detect types.h already loaded */
211#include <sys/types.h>
265a9e55 212#endif /* makedev */
7dd63af1
RS
213#include <stdio.h>
214#include <sys/stat.h>
215#include <errno.h>
216
2d30a233
RM
217#include <sys/file.h> /* Must be after sys/types.h for USG and BSD4_1*/
218
219#ifdef USG5
220#include <fcntl.h>
221#endif
222
223#ifndef O_RDONLY
224#define O_RDONLY 0
225#endif
226#ifndef O_RDWR
227#define O_RDWR 2
228#endif
229
230
7dd63af1
RS
231extern char *start_of_text (); /* Start of text */
232extern char *start_of_data (); /* Start of initialized data */
233
234#ifdef COFF
235static long block_copy_start; /* Old executable start point */
236static struct filehdr f_hdr; /* File header */
237static struct aouthdr f_ohdr; /* Optional file header (a.out) */
238long bias; /* Bias to add for growth */
239long lnnoptr; /* Pointer to line-number info within file */
240#define SYMS_START block_copy_start
241
242static long text_scnptr;
243static long data_scnptr;
244
245#else /* not COFF */
246
83cb209c
JB
247#ifdef HPUX
248extern void *sbrk ();
249#else
f31fe472
RM
250#if 0
251/* Some systems with __STDC__ compilers still declare this `char *' in some
252 header file, and our declaration conflicts. The return value is always
253 cast, so it should be harmless to leave it undefined. Hopefully
254 machines with different size pointers and ints declare sbrk in a header
255 file. */
d4327fec
JB
256#ifdef __STDC__
257extern void *sbrk ();
258#else
7dd63af1 259extern char *sbrk ();
83cb209c 260#endif /* __STDC__ */
f31fe472 261#endif
83cb209c 262#endif /* HPUX */
7dd63af1
RS
263
264#define SYMS_START ((long) N_SYMOFF (ohdr))
265
266/* Some machines override the structure name for an a.out header. */
267#ifndef EXEC_HDR_TYPE
268#define EXEC_HDR_TYPE struct exec
269#endif
270
271#ifdef HPUX
272#ifdef HP9000S200_ID
273#define MY_ID HP9000S200_ID
274#else
275#include <model.h>
276#define MY_ID MYSYS
277#endif /* no HP9000S200_ID */
278static MAGIC OLDMAGIC = {MY_ID, SHARE_MAGIC};
279static MAGIC NEWMAGIC = {MY_ID, DEMAND_MAGIC};
280#define N_TXTOFF(x) TEXT_OFFSET(x)
281#define N_SYMOFF(x) LESYM_OFFSET(x)
282static EXEC_HDR_TYPE hdr, ohdr;
283
284#else /* not HPUX */
285
161aa2f8 286#if defined (USG) && !defined (IBMAIX) && !defined (IRIS) && !defined (COFF_ENCAPSULATE) && !defined (LINUX)
7dd63af1
RS
287static struct bhdr hdr, ohdr;
288#define a_magic fmagic
289#define a_text tsize
290#define a_data dsize
291#define a_bss bsize
292#define a_syms ssize
293#define a_trsize rtsize
294#define a_drsize rdsize
295#define a_entry entry
296#define N_BADMAG(x) \
297 (((x).fmagic)!=OMAGIC && ((x).fmagic)!=NMAGIC &&\
298 ((x).fmagic)!=FMAGIC && ((x).fmagic)!=IMAGIC)
299#define NEWMAGIC FMAGIC
300#else /* IRIS or IBMAIX or not USG */
301static EXEC_HDR_TYPE hdr, ohdr;
302#define NEWMAGIC ZMAGIC
303#endif /* IRIS or IBMAIX not USG */
304#endif /* not HPUX */
305
306static int unexec_text_start;
307static int unexec_data_start;
308
265a9e55
JB
309#ifdef COFF_ENCAPSULATE
310/* coffheader is defined in the GNU a.out.encap.h file. */
311struct coffheader coffheader;
312#endif
313
7dd63af1
RS
314#endif /* not COFF */
315
316static int pagemask;
317
318/* Correct an int which is the bit pattern of a pointer to a byte
319 into an int which is the number of a byte.
320 This is a no-op on ordinary machines, but not on all. */
321
322#ifndef ADDR_CORRECT /* Let m-*.h files override this definition */
323#define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
324#endif
325
326#ifdef emacs
327
2d30a233
RM
328#include "lisp.h"
329
7dd63af1
RS
330static
331report_error (file, fd)
332 char *file;
333 int fd;
334{
335 if (fd)
336 close (fd);
2d30a233 337 report_file_error ("Cannot unexec", Fcons (build_string (file), Qnil));
7dd63af1
RS
338}
339#endif /* emacs */
340
341#define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
342#define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
343#define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
344
345static
346report_error_1 (fd, msg, a1, a2)
347 int fd;
348 char *msg;
349 int a1, a2;
350{
351 close (fd);
352#ifdef emacs
353 error (msg, a1, a2);
354#else
355 fprintf (stderr, msg, a1, a2);
356 fprintf (stderr, "\n");
357#endif
358}
359\f
360static int make_hdr ();
361static int copy_text_and_data ();
362static int copy_sym ();
363static void mark_x ();
364
365/* ****************************************************************
366 * unexec
367 *
368 * driving logic.
369 */
370unexec (new_name, a_name, data_start, bss_start, entry_address)
371 char *new_name, *a_name;
372 unsigned data_start, bss_start, entry_address;
373{
374 int new, a_out = -1;
375
2d30a233 376 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
7dd63af1
RS
377 {
378 PERROR (a_name);
379 }
380 if ((new = creat (new_name, 0666)) < 0)
381 {
382 PERROR (new_name);
383 }
384
385 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0
386 || copy_text_and_data (new, a_out) < 0
387 || copy_sym (new, a_out, a_name, new_name) < 0
388#ifdef COFF
389#ifndef COFF_BSD_SYMBOLS
390 || adjust_lnnoptrs (new, a_out, new_name) < 0
391#endif
392#endif
393 )
394 {
395 close (new);
396 /* unlink (new_name); /* Failed, unlink new a.out */
397 return -1;
398 }
399
400 close (new);
401 if (a_out >= 0)
402 close (a_out);
403 mark_x (new_name);
404 return 0;
405}
406
407/* ****************************************************************
408 * make_hdr
409 *
410 * Make the header in the new a.out from the header in core.
411 * Modify the text and data sizes.
412 */
413static int
414make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
415 int new, a_out;
416 unsigned data_start, bss_start, entry_address;
417 char *a_name;
418 char *new_name;
419{
420 int tem;
421#ifdef COFF
422 auto struct scnhdr f_thdr; /* Text section header */
423 auto struct scnhdr f_dhdr; /* Data section header */
424 auto struct scnhdr f_bhdr; /* Bss section header */
425 auto struct scnhdr scntemp; /* Temporary section header */
426 register int scns;
427#endif /* COFF */
428#ifdef USG_SHARED_LIBRARIES
429 extern unsigned int bss_end;
430#else
431 unsigned int bss_end;
432#endif
433
434 pagemask = getpagesize () - 1;
435
436 /* Adjust text/data boundary. */
437#ifdef NO_REMAP
438 data_start = (int) start_of_data ();
439#else /* not NO_REMAP */
440 if (!data_start)
441 data_start = (int) start_of_data ();
442#endif /* not NO_REMAP */
443 data_start = ADDR_CORRECT (data_start);
444
445#ifdef SEGMENT_MASK
446 data_start = data_start & ~SEGMENT_MASK; /* (Down) to segment boundary. */
447#else
448 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
449#endif
450
451 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask;
452 bss_end &= ~ pagemask;
453
454 /* Adjust data/bss boundary. */
455 if (bss_start != 0)
456 {
457 bss_start = (ADDR_CORRECT (bss_start) + pagemask);
458 /* (Up) to page bdry. */
459 bss_start &= ~ pagemask;
460 if (bss_start > bss_end)
461 {
462 ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
463 bss_start);
464 }
465 }
466 else
467 bss_start = bss_end;
468
469 if (data_start > bss_start) /* Can't have negative data size. */
470 {
471 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
472 data_start, bss_start);
473 }
474
475#ifdef COFF
476 /* Salvage as much info from the existing file as possible */
477 if (a_out >= 0)
478 {
479 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
480 {
481 PERROR (a_name);
482 }
483 block_copy_start += sizeof (f_hdr);
484 if (f_hdr.f_opthdr > 0)
485 {
486 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
487 {
488 PERROR (a_name);
489 }
490 block_copy_start += sizeof (f_ohdr);
491 }
492 /* Loop through section headers, copying them in */
493 for (scns = f_hdr.f_nscns; scns > 0; scns--) {
494 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
495 {
496 PERROR (a_name);
497 }
498 if (scntemp.s_scnptr > 0L)
499 {
500 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
501 block_copy_start = scntemp.s_scnptr + scntemp.s_size;
502 }
503 if (strcmp (scntemp.s_name, ".text") == 0)
504 {
505 f_thdr = scntemp;
506 }
507 else if (strcmp (scntemp.s_name, ".data") == 0)
508 {
509 f_dhdr = scntemp;
510 }
511 else if (strcmp (scntemp.s_name, ".bss") == 0)
512 {
513 f_bhdr = scntemp;
514 }
515 }
516 }
517 else
518 {
519 ERROR0 ("can't build a COFF file from scratch yet");
520 }
521
522 /* Now we alter the contents of all the f_*hdr variables
523 to correspond to what we want to dump. */
524
525#ifdef USG_SHARED_LIBRARIES
526
527 /* The amount of data we're adding to the file is distance from the
528 * end of the original .data space to the current end of the .data
529 * space.
530 */
531
1ba3de00 532 bias = bss_start - (f_ohdr.data_start + f_dhdr.s_size);
7dd63af1
RS
533
534#endif
535
536 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
537#ifdef TPIX
538 f_hdr.f_nscns = 3;
539#endif
540#ifdef EXEC_MAGIC
541 f_ohdr.magic = EXEC_MAGIC;
542#endif
543#ifndef NO_REMAP
544 f_ohdr.text_start = (long) start_of_text ();
545 f_ohdr.tsize = data_start - f_ohdr.text_start;
546 f_ohdr.data_start = data_start;
547#endif /* NO_REMAP */
548 f_ohdr.dsize = bss_start - f_ohdr.data_start;
549 f_ohdr.bsize = bss_end - bss_start;
550#ifndef KEEP_OLD_TEXT_SCNPTR
551 /* On some machines, the old values are right.
552 ??? Maybe on all machines with NO_REMAP. */
553 f_thdr.s_size = f_ohdr.tsize;
554 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
555 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
556#endif /* KEEP_OLD_TEXT_SCNPTR */
557#ifdef ADJUST_TEXT_SCNHDR_SIZE
558 /* On some machines, `text size' includes all headers. */
559 f_thdr.s_size -= f_thdr.s_scnptr;
560#endif /* ADJUST_TEST_SCNHDR_SIZE */
561 lnnoptr = f_thdr.s_lnnoptr;
562#ifdef SECTION_ALIGNMENT
563 /* Some systems require special alignment
564 of the sections in the file itself. */
565 f_thdr.s_scnptr
566 = (f_thdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT;
567#endif /* SECTION_ALIGNMENT */
568#ifdef TPIX
569 f_thdr.s_scnptr = 0xd0;
570#endif
571 text_scnptr = f_thdr.s_scnptr;
572#ifdef ADJUST_TEXTBASE
573 text_scnptr = sizeof (f_hdr) + sizeof (f_ohdr) + (f_hdr.f_nscns) * (sizeof (f_thdr));
574#endif
575#ifndef KEEP_OLD_PADDR
576 f_dhdr.s_paddr = f_ohdr.data_start;
577#endif /* KEEP_OLD_PADDR */
578 f_dhdr.s_vaddr = f_ohdr.data_start;
579 f_dhdr.s_size = f_ohdr.dsize;
580 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
581#ifdef SECTION_ALIGNMENT
582 /* Some systems require special alignment
583 of the sections in the file itself. */
584 f_dhdr.s_scnptr
585 = (f_dhdr.s_scnptr + SECTION_ALIGNMENT) & ~SECTION_ALIGNMENT;
586#endif /* SECTION_ALIGNMENT */
587#ifdef DATA_SECTION_ALIGNMENT
588 /* Some systems require special alignment
589 of the data section only. */
590 f_dhdr.s_scnptr
591 = (f_dhdr.s_scnptr + DATA_SECTION_ALIGNMENT) & ~DATA_SECTION_ALIGNMENT;
592#endif /* DATA_SECTION_ALIGNMENT */
593 data_scnptr = f_dhdr.s_scnptr;
594#ifndef KEEP_OLD_PADDR
595 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
596#endif /* KEEP_OLD_PADDR */
597 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
598 f_bhdr.s_size = f_ohdr.bsize;
599 f_bhdr.s_scnptr = 0L;
600#ifndef USG_SHARED_LIBRARIES
601 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
602#endif
603
604 if (f_hdr.f_symptr > 0L)
605 {
606 f_hdr.f_symptr += bias;
607 }
608
609 if (f_thdr.s_lnnoptr > 0L)
610 {
611 f_thdr.s_lnnoptr += bias;
612 }
613
614#ifdef ADJUST_EXEC_HEADER
615 ADJUST_EXEC_HEADER;
616#endif /* ADJUST_EXEC_HEADER */
617
618 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
619 {
620 PERROR (new_name);
621 }
622
623 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
624 {
625 PERROR (new_name);
626 }
627
628#ifndef USG_SHARED_LIBRARIES
629
630 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
631 {
632 PERROR (new_name);
633 }
634
635 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
636 {
637 PERROR (new_name);
638 }
639
640 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
641 {
642 PERROR (new_name);
643 }
644
645#else /* USG_SHARED_LIBRARIES */
646
647 /* The purpose of this code is to write out the new file's section
648 * header table.
649 *
650 * Scan through the original file's sections. If the encountered
651 * section is one we know (.text, .data or .bss), write out the
652 * correct header. If it is a section we do not know (such as
653 * .lib), adjust the address of where the section data is in the
654 * file, and write out the header.
655 *
eb8c3be9 656 * If any section precedes .text or .data in the file, this code
7dd63af1
RS
657 * will not adjust the file pointer for that section correctly.
658 */
659
660 lseek (a_out, sizeof (f_hdr) + sizeof (f_ohdr), 0);
661
662 for (scns = f_hdr.f_nscns; scns > 0; scns--)
663 {
664 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
665 PERROR (a_name);
666
667 if (!strcmp (scntemp.s_name, f_thdr.s_name)) /* .text */
668 {
669 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
670 PERROR (new_name);
671 }
672 else if (!strcmp (scntemp.s_name, f_dhdr.s_name)) /* .data */
673 {
674 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
675 PERROR (new_name);
676 }
677 else if (!strcmp (scntemp.s_name, f_bhdr.s_name)) /* .bss */
678 {
679 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
680 PERROR (new_name);
681 }
682 else
683 {
684 if (scntemp.s_scnptr)
685 scntemp.s_scnptr += bias;
686 if (write (new, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
687 PERROR (new_name);
688 }
689 }
690#endif /* USG_SHARED_LIBRARIES */
691
692 return (0);
693
694#else /* if not COFF */
695
696 /* Get symbol table info from header of a.out file if given one. */
697 if (a_out >= 0)
698 {
265a9e55
JB
699#ifdef COFF_ENCAPSULATE
700 if (read (a_out, &coffheader, sizeof coffheader) != sizeof coffheader)
701 {
702 PERROR(a_name);
703 }
704 if (coffheader.f_magic != COFF_MAGIC)
705 {
706 ERROR1("%s doesn't have legal coff magic number\n", a_name);
707 }
708#endif
7dd63af1
RS
709 if (read (a_out, &ohdr, sizeof hdr) != sizeof hdr)
710 {
711 PERROR (a_name);
712 }
713
714 if (N_BADMAG (ohdr))
715 {
716 ERROR1 ("invalid magic number in %s", a_name);
717 }
718 hdr = ohdr;
719 }
720 else
721 {
265a9e55
JB
722#ifdef COFF_ENCAPSULATE
723 /* We probably could without too much trouble. The code is in gld
724 * but I don't have that much time or incentive.
725 */
726 ERROR0 ("can't build a COFF file from scratch yet");
3680bdc6
RS
727#else
728#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
729 bzero ((void *)&hdr, sizeof hdr);
265a9e55 730#else
a5fd213f 731 bzero (&hdr, sizeof hdr);
3680bdc6 732#endif
265a9e55 733#endif
7dd63af1
RS
734 }
735
736 unexec_text_start = (long) start_of_text ();
737 unexec_data_start = data_start;
738
739 /* Machine-dependent fixup for header, or maybe for unexec_text_start */
740#ifdef ADJUST_EXEC_HEADER
741 ADJUST_EXEC_HEADER;
742#endif /* ADJUST_EXEC_HEADER */
743
744 hdr.a_trsize = 0;
745 hdr.a_drsize = 0;
746 if (entry_address != 0)
747 hdr.a_entry = entry_address;
748
749 hdr.a_bss = bss_end - bss_start;
750 hdr.a_data = bss_start - data_start;
751#ifdef NO_REMAP
752 hdr.a_text = ohdr.a_text;
753#else /* not NO_REMAP */
754 hdr.a_text = data_start - unexec_text_start;
755
756#ifdef A_TEXT_OFFSET
757 hdr.a_text += A_TEXT_OFFSET (ohdr);
758#endif
759
760#endif /* not NO_REMAP */
761
265a9e55
JB
762#ifdef COFF_ENCAPSULATE
763 /* We are encapsulating BSD format within COFF format. */
764 {
765 struct coffscn *tp, *dp, *bp;
766 tp = &coffheader.scns[0];
767 dp = &coffheader.scns[1];
768 bp = &coffheader.scns[2];
769 tp->s_size = hdr.a_text + sizeof(struct exec);
770 dp->s_paddr = data_start;
771 dp->s_vaddr = data_start;
772 dp->s_size = hdr.a_data;
773 bp->s_paddr = dp->s_vaddr + dp->s_size;
774 bp->s_vaddr = bp->s_paddr;
775 bp->s_size = hdr.a_bss;
776 coffheader.tsize = tp->s_size;
777 coffheader.dsize = dp->s_size;
778 coffheader.bsize = bp->s_size;
779 coffheader.text_start = tp->s_vaddr;
780 coffheader.data_start = dp->s_vaddr;
781 }
782 if (write (new, &coffheader, sizeof coffheader) != sizeof coffheader)
783 {
784 PERROR(new_name);
785 }
786#endif /* COFF_ENCAPSULATE */
787
7dd63af1
RS
788 if (write (new, &hdr, sizeof hdr) != sizeof hdr)
789 {
790 PERROR (new_name);
791 }
792
2d30a233
RM
793 /* This adjustment was done above only #ifndef NO_REMAP,
794 so only undo it now #ifndef NO_REMAP. */
795#ifndef NO_REMAP
7dd63af1
RS
796#ifdef A_TEXT_OFFSET
797 hdr.a_text -= A_TEXT_OFFSET (ohdr);
2d30a233 798#endif
7dd63af1
RS
799#endif
800
801 return 0;
802
803#endif /* not COFF */
804}
805\f
806/* ****************************************************************
807 * copy_text_and_data
808 *
809 * Copy the text and data segments from memory to the new a.out
810 */
811static int
812copy_text_and_data (new, a_out)
813 int new, a_out;
814{
815 register char *end;
816 register char *ptr;
817
818#ifdef COFF
819
820#ifdef USG_SHARED_LIBRARIES
821
822 int scns;
823 struct scnhdr scntemp; /* Temporary section header */
824
825 /* The purpose of this code is to write out the new file's section
826 * contents.
827 *
828 * Step through the section table. If we know the section (.text,
829 * .data) do the appropriate thing. Otherwise, if the section has
830 * no allocated space in the file (.bss), do nothing. Otherwise,
831 * the section has space allocated in the file, and is not a section
832 * we know. So just copy it.
833 */
834
835 lseek (a_out, sizeof (struct filehdr) + sizeof (struct aouthdr), 0);
836
837 for (scns = f_hdr.f_nscns; scns > 0; scns--)
838 {
839 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
840 PERROR ("temacs");
841
842 if (!strcmp (scntemp.s_name, ".text"))
843 {
844 lseek (new, (long) text_scnptr, 0);
845 ptr = (char *) f_ohdr.text_start;
846 end = ptr + f_ohdr.tsize;
847 write_segment (new, ptr, end);
848 }
849 else if (!strcmp (scntemp.s_name, ".data"))
850 {
851 lseek (new, (long) data_scnptr, 0);
852 ptr = (char *) f_ohdr.data_start;
853 end = ptr + f_ohdr.dsize;
854 write_segment (new, ptr, end);
855 }
856 else if (!scntemp.s_scnptr)
857 ; /* do nothing - no data for this section */
858 else
859 {
860 char page[BUFSIZ];
861 int size, n;
862 long old_a_out_ptr = lseek (a_out, 0, 1);
863
864 lseek (a_out, scntemp.s_scnptr, 0);
865 for (size = scntemp.s_size; size > 0; size -= sizeof (page))
866 {
867 n = size > sizeof (page) ? sizeof (page) : size;
868 if (read (a_out, page, n) != n || write (new, page, n) != n)
8116bbb0 869 PERROR ("emacs");
7dd63af1
RS
870 }
871 lseek (a_out, old_a_out_ptr, 0);
872 }
873 }
874
875#else /* COFF, but not USG_SHARED_LIBRARIES */
876
877 lseek (new, (long) text_scnptr, 0);
878 ptr = (char *) f_ohdr.text_start;
879#ifdef HEADER_INCL_IN_TEXT
880 /* For Gould UTX/32, text starts after headers */
881 ptr = (char *) (ptr + text_scnptr);
882#endif /* HEADER_INCL_IN_TEXT */
883 end = ptr + f_ohdr.tsize;
884 write_segment (new, ptr, end);
885
886 lseek (new, (long) data_scnptr, 0);
887 ptr = (char *) f_ohdr.data_start;
888 end = ptr + f_ohdr.dsize;
889 write_segment (new, ptr, end);
890
891#endif /* USG_SHARED_LIBRARIES */
892
893#else /* if not COFF */
894
895/* Some machines count the header as part of the text segment.
896 That is to say, the header appears in core
897 just before the address that start_of_text () returns.
898 For them, N_TXTOFF is the place where the header goes.
899 We must adjust the seek to the place after the header.
900 Note that at this point hdr.a_text does *not* count
901 the extra A_TEXT_OFFSET bytes, only the actual bytes of code. */
902
903#ifdef A_TEXT_SEEK
904 lseek (new, (long) A_TEXT_SEEK (hdr), 0);
905#else
906 lseek (new, (long) N_TXTOFF (hdr), 0);
907#endif /* no A_TEXT_SEEK */
908
909 ptr = (char *) unexec_text_start;
910 end = ptr + hdr.a_text;
911 write_segment (new, ptr, end);
912
913 ptr = (char *) unexec_data_start;
914 end = ptr + hdr.a_data;
915/* This lseek is certainly incorrect when A_TEXT_OFFSET
916 and I believe it is a no-op otherwise.
917 Let's see if its absence ever fails. */
918/* lseek (new, (long) N_TXTOFF (hdr) + hdr.a_text, 0); */
919 write_segment (new, ptr, end);
920
921#endif /* not COFF */
922
923 return 0;
924}
925
926write_segment (new, ptr, end)
927 int new;
928 register char *ptr, *end;
929{
930 register int i, nwrite, ret;
931 char buf[80];
932 extern int errno;
933 char zeros[128];
934
935 bzero (zeros, sizeof zeros);
936
937 for (i = 0; ptr < end;)
938 {
939 /* distance to next multiple of 128. */
940 nwrite = (((int) ptr + 128) & -128) - (int) ptr;
941 /* But not beyond specified end. */
942 if (nwrite > end - ptr) nwrite = end - ptr;
943 ret = write (new, ptr, nwrite);
944 /* If write gets a page fault, it means we reached
945 a gap between the old text segment and the old data segment.
946 This gap has probably been remapped into part of the text segment.
947 So write zeros for it. */
3680bdc6
RS
948 if (ret == -1
949#ifdef EFAULT
950 && errno == EFAULT
951#endif
952 )
7dd63af1
RS
953 write (new, zeros, nwrite);
954 else if (nwrite != ret)
955 {
956 sprintf (buf,
957 "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d",
958 ptr, new, nwrite, ret, errno);
959 PERROR (buf);
960 }
961 i += nwrite;
962 ptr += nwrite;
963 }
964}
965\f
966/* ****************************************************************
967 * copy_sym
968 *
969 * Copy the relocation information and symbol table from the a.out to the new
970 */
971static int
972copy_sym (new, a_out, a_name, new_name)
973 int new, a_out;
974 char *a_name, *new_name;
975{
976 char page[1024];
977 int n;
978
979 if (a_out < 0)
980 return 0;
981
982#ifdef COFF
983 if (SYMS_START == 0L)
984 return 0;
985#endif /* COFF */
986
987#ifdef COFF
988 if (lnnoptr) /* if there is line number info */
989 lseek (a_out, lnnoptr, 0); /* start copying from there */
990 else
991#endif /* COFF */
992 lseek (a_out, SYMS_START, 0); /* Position a.out to symtab. */
993
994 while ((n = read (a_out, page, sizeof page)) > 0)
995 {
996 if (write (new, page, n) != n)
997 {
998 PERROR (new_name);
999 }
1000 }
1001 if (n < 0)
1002 {
1003 PERROR (a_name);
1004 }
1005 return 0;
1006}
1007\f
1008/* ****************************************************************
1009 * mark_x
1010 *
eb8c3be9 1011 * After successfully building the new a.out, mark it executable
7dd63af1
RS
1012 */
1013static void
1014mark_x (name)
1015 char *name;
1016{
1017 struct stat sbuf;
1018 int um;
1019 int new = 0; /* for PERROR */
1020
1021 um = umask (777);
1022 umask (um);
1023 if (stat (name, &sbuf) == -1)
1024 {
1025 PERROR (name);
1026 }
1027 sbuf.st_mode |= 0111 & ~um;
1028 if (chmod (name, sbuf.st_mode) == -1)
1029 PERROR (name);
1030}
1031\f
1032#ifdef COFF
1033#ifndef COFF_BSD_SYMBOLS
1034
1035/*
1036 * If the COFF file contains a symbol table and a line number section,
1037 * then any auxiliary entries that have values for x_lnnoptr must
1038 * be adjusted by the amount that the line number section has moved
1039 * in the file (bias computed in make_hdr). The #@$%&* designers of
1040 * the auxiliary entry structures used the absolute file offsets for
1041 * the line number entry rather than an offset from the start of the
1042 * line number section!
1043 *
1044 * When I figure out how to scan through the symbol table and pick out
1045 * the auxiliary entries that need adjustment, this routine will
1046 * be fixed. As it is now, all such entries are wrong and sdb
1047 * will complain. Fred Fish, UniSoft Systems Inc.
1048 */
1049
1050/* This function is probably very slow. Instead of reopening the new
1051 file for input and output it should copy from the old to the new
1052 using the two descriptors already open (WRITEDESC and READDESC).
1053 Instead of reading one small structure at a time it should use
1054 a reasonable size buffer. But I don't have time to work on such
1055 things, so I am installing it as submitted to me. -- RMS. */
1056
1057adjust_lnnoptrs (writedesc, readdesc, new_name)
1058 int writedesc;
1059 int readdesc;
1060 char *new_name;
1061{
1062 register int nsyms;
1063 register int new;
91bac16a 1064#if defined (amdahl_uts) || defined (pfa)
7dd63af1
RS
1065 SYMENT symentry;
1066 AUXENT auxentry;
1067#else
1068 struct syment symentry;
1069 union auxent auxentry;
1070#endif
1071
1072 if (!lnnoptr || !f_hdr.f_symptr)
1073 return 0;
1074
3680bdc6
RS
1075#ifdef MSDOS
1076 if ((new = writedesc) < 0)
1077#else
2d30a233 1078 if ((new = open (new_name, O_RDWR)) < 0)
3680bdc6 1079#endif
7dd63af1
RS
1080 {
1081 PERROR (new_name);
1082 return -1;
1083 }
1084
1085 lseek (new, f_hdr.f_symptr, 0);
1086 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
1087 {
1088 read (new, &symentry, SYMESZ);
1089 if (symentry.n_numaux)
1090 {
1091 read (new, &auxentry, AUXESZ);
1092 nsyms++;
1ba3de00
RS
1093 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400)
1094 {
1095 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
1096 lseek (new, -AUXESZ, 1);
1097 write (new, &auxentry, AUXESZ);
1098 }
7dd63af1
RS
1099 }
1100 }
3680bdc6 1101#ifndef MSDOS
7dd63af1 1102 close (new);
3680bdc6
RS
1103#endif
1104 return 0;
7dd63af1
RS
1105}
1106
1107#endif /* COFF_BSD_SYMBOLS */
1108
1109#endif /* COFF */
1110
1111#endif /* not CANNOT_UNEXEC */
1112
1113#endif /* not CANNOT_DUMP */