Backport the :end-of-capability fix
[emacs.git] / src / unexcoff.c
blob0cb72d9dcd2a21d4b10ed446aa979d4c7db7b3ec
1 /* Copyright (C) 1985-1988, 1992-1994, 2001-2015 Free Software
2 * Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 * unexcoff.c - Convert a running program into an a.out or COFF file.
23 * ==================================================================
24 * Note: This file is currently used only by the MSDOS (a.k.a. DJGPP)
25 * build of Emacs. If you are not interested in the MSDOS build, you
26 * are looking at the wrong version of unexec!
27 * ==================================================================
29 * Author: Spencer W. Thomas
30 * Computer Science Dept.
31 * University of Utah
32 * Date: Tue Mar 2 1982
33 * Originally under the name unexec.c.
34 * Modified heavily since then.
36 * Synopsis:
37 * unexec (const char *new_name, const char *old_name);
39 * Takes a snapshot of the program and makes an a.out format file in the
40 * file named by the string argument new_name.
41 * If a_name is non-NULL, the symbol table will be taken from the given file.
42 * On some machines, an existing a_name file is required.
44 * If you make improvements I'd like to get them too.
45 * harpo!utah-cs!thomas, thomas@Utah-20
49 /* Modified to support SysVr3 shared libraries by James Van Artsdalen
50 * of Dell Computer Corporation. james@bigtex.cactus.org.
53 #include <config.h>
54 #include "unexec.h"
55 #include "lisp.h"
57 #define PERROR(file) report_error (file, new)
59 #ifndef CANNOT_DUMP /* all rest of file! */
61 #ifdef HAVE_COFF_H
62 #include <coff.h>
63 #ifdef MSDOS
64 #include <fcntl.h> /* for O_RDONLY, O_RDWR */
65 #include <crt0.h> /* for _crt0_startup_flags and its bits */
66 #include <sys/exceptn.h>
67 static int save_djgpp_startup_flags;
68 #include <libc/atexit.h>
69 static struct __atexit *save_atexit_ptr;
70 #define filehdr external_filehdr
71 #define scnhdr external_scnhdr
72 #define syment external_syment
73 #define auxent external_auxent
74 #define n_numaux e_numaux
75 #define n_type e_type
76 struct aouthdr
78 unsigned short magic; /* type of file */
79 unsigned short vstamp; /* version stamp */
80 unsigned long tsize; /* text size in bytes, padded to FW bdry*/
81 unsigned long dsize; /* initialized data " " */
82 unsigned long bsize; /* uninitialized data " " */
83 unsigned long entry; /* entry pt. */
84 unsigned long text_start;/* base of text used for this file */
85 unsigned long data_start;/* base of data used for this file */
87 #endif /* MSDOS */
88 #else /* not HAVE_COFF_H */
89 #include <a.out.h>
90 #endif /* not HAVE_COFF_H */
92 /* Define getpagesize if the system does not.
93 Note that this may depend on symbols defined in a.out.h. */
94 #include "getpagesize.h"
96 #ifndef makedev /* Try to detect types.h already loaded */
97 #include <sys/types.h>
98 #endif /* makedev */
99 #include <stdio.h>
100 #include <sys/stat.h>
101 #include <errno.h>
103 #include <sys/file.h>
105 extern int etext;
107 static long block_copy_start; /* Old executable start point */
108 static struct filehdr f_hdr; /* File header */
109 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
110 long bias; /* Bias to add for growth */
111 long lnnoptr; /* Pointer to line-number info within file */
112 #define SYMS_START block_copy_start
114 static long text_scnptr;
115 static long data_scnptr;
117 static long coff_offset;
119 static int pagemask;
121 /* Correct an int which is the bit pattern of a pointer to a byte
122 into an int which is the number of a byte.
123 This is a no-op on ordinary machines, but not on all. */
125 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
127 #include "lisp.h"
129 static void
130 report_error (const char *file, int fd)
132 int err = errno;
133 if (fd)
134 emacs_close (fd);
135 report_file_errno ("Cannot unexec", build_string (file), err);
138 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
139 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
140 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
142 static void
143 report_error_1 (int fd, const char *msg, int a1, int a2)
145 emacs_close (fd);
146 error (msg, a1, a2);
149 static int make_hdr (int, int, const char *, const char *);
150 static int copy_text_and_data (int, int);
151 static int copy_sym (int, int, const char *, const char *);
152 static void mark_x (const char *);
154 /* ****************************************************************
155 * make_hdr
157 * Make the header in the new a.out from the header in core.
158 * Modify the text and data sizes.
160 static int
161 make_hdr (int new, int a_out,
162 const char *a_name, const char *new_name)
164 auto struct scnhdr f_thdr; /* Text section header */
165 auto struct scnhdr f_dhdr; /* Data section header */
166 auto struct scnhdr f_bhdr; /* Bss section header */
167 auto struct scnhdr scntemp; /* Temporary section header */
168 register int scns;
169 unsigned int bss_start;
170 unsigned int data_start;
172 pagemask = getpagesize () - 1;
174 /* Adjust text/data boundary. */
175 data_start = (int) DATA_START;
176 data_start = ADDR_CORRECT (data_start);
177 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
179 bss_start = ADDR_CORRECT (sbrk (0)) + pagemask;
180 bss_start &= ~ pagemask;
182 if (data_start > bss_start) /* Can't have negative data size. */
184 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
185 data_start, bss_start);
188 coff_offset = 0L; /* stays zero, except in DJGPP */
190 /* Salvage as much info from the existing file as possible */
191 if (a_out >= 0)
193 #ifdef MSDOS
194 /* Support the coff-go32-exe format with a prepended stub, since
195 this is what GCC 2.8.0 and later generates by default in DJGPP. */
196 unsigned short mz_header[3];
198 if (read (a_out, &mz_header, sizeof (mz_header)) != sizeof (mz_header))
200 PERROR (a_name);
202 if (mz_header[0] == 0x5a4d || mz_header[0] == 0x4d5a) /* "MZ" or "ZM" */
204 coff_offset = (long)mz_header[2] * 512L;
205 if (mz_header[1])
206 coff_offset += (long)mz_header[1] - 512L;
207 lseek (a_out, coff_offset, 0);
209 else
210 lseek (a_out, 0L, 0);
211 #endif /* MSDOS */
212 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
214 PERROR (a_name);
216 block_copy_start += sizeof (f_hdr);
217 if (f_hdr.f_opthdr > 0)
219 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
221 PERROR (a_name);
223 block_copy_start += sizeof (f_ohdr);
225 /* Loop through section headers, copying them in */
226 lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0);
227 for (scns = f_hdr.f_nscns; scns > 0; scns--) {
228 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
230 PERROR (a_name);
232 if (scntemp.s_scnptr > 0L)
234 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
235 block_copy_start = scntemp.s_scnptr + scntemp.s_size;
237 if (strcmp (scntemp.s_name, ".text") == 0)
239 f_thdr = scntemp;
241 else if (strcmp (scntemp.s_name, ".data") == 0)
243 f_dhdr = scntemp;
245 else if (strcmp (scntemp.s_name, ".bss") == 0)
247 f_bhdr = scntemp;
251 else
253 ERROR0 ("can't build a COFF file from scratch yet");
256 /* Now we alter the contents of all the f_*hdr variables
257 to correspond to what we want to dump. */
259 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
260 f_ohdr.dsize = bss_start - f_ohdr.data_start;
261 f_ohdr.bsize = 0;
262 f_thdr.s_size = f_ohdr.tsize;
263 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
264 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
265 lnnoptr = f_thdr.s_lnnoptr;
266 text_scnptr = f_thdr.s_scnptr;
267 f_dhdr.s_paddr = f_ohdr.data_start;
268 f_dhdr.s_vaddr = f_ohdr.data_start;
269 f_dhdr.s_size = f_ohdr.dsize;
270 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
271 data_scnptr = f_dhdr.s_scnptr;
272 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
273 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
274 f_bhdr.s_size = f_ohdr.bsize;
275 f_bhdr.s_scnptr = 0L;
276 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
278 if (f_hdr.f_symptr > 0L)
280 f_hdr.f_symptr += bias;
283 if (f_thdr.s_lnnoptr > 0L)
285 f_thdr.s_lnnoptr += bias;
288 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
290 PERROR (new_name);
293 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
295 PERROR (new_name);
298 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
300 PERROR (new_name);
303 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
305 PERROR (new_name);
308 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
310 PERROR (new_name);
313 return (0);
317 void
318 write_segment (int new, const char *ptr, const char *end)
320 register int i, nwrite, ret;
321 /* This is the normal amount to write at once.
322 It is the size of block that NFS uses. */
323 int writesize = 1 << 13;
324 int pagesize = getpagesize ();
325 char zeros[1 << 13];
327 memset (zeros, 0, sizeof (zeros));
329 for (i = 0; ptr < end;)
331 /* Distance to next multiple of writesize. */
332 nwrite = (((int) ptr + writesize) & -writesize) - (int) ptr;
333 /* But not beyond specified end. */
334 if (nwrite > end - ptr) nwrite = end - ptr;
335 ret = write (new, ptr, nwrite);
336 /* If write gets a page fault, it means we reached
337 a gap between the old text segment and the old data segment.
338 This gap has probably been remapped into part of the text segment.
339 So write zeros for it. */
340 if (ret == -1 && errno == EFAULT)
342 /* Write only a page of zeros at once,
343 so that we don't overshoot the start
344 of the valid memory in the old data segment. */
345 if (nwrite > pagesize)
346 nwrite = pagesize;
347 write (new, zeros, nwrite);
349 i += nwrite;
350 ptr += nwrite;
353 /* ****************************************************************
354 * copy_text_and_data
356 * Copy the text and data segments from memory to the new a.out
358 static int
359 copy_text_and_data (int new, int a_out)
361 register char *end;
362 register char *ptr;
364 #ifdef MSDOS
365 /* Dump the original table of exception handlers, not the one
366 where our exception hooks are registered. */
367 __djgpp_exception_toggle ();
369 /* Switch off startup flags that might have been set at runtime
370 and which might change the way that dumped Emacs works. */
371 save_djgpp_startup_flags = _crt0_startup_flags;
372 _crt0_startup_flags &= ~(_CRT0_FLAG_NO_LFN | _CRT0_FLAG_NEARPTR);
374 /* Zero out the 'atexit' chain in the dumped executable, to avoid
375 calling the atexit functions twice. (emacs.c:main installs an
376 atexit function.) */
377 save_atexit_ptr = __atexit_ptr;
378 __atexit_ptr = NULL;
379 #endif
381 lseek (new, (long) text_scnptr, 0);
382 ptr = (char *) f_ohdr.text_start;
383 end = ptr + f_ohdr.tsize;
384 write_segment (new, ptr, end);
386 lseek (new, (long) data_scnptr, 0);
387 ptr = (char *) f_ohdr.data_start;
388 end = ptr + f_ohdr.dsize;
389 write_segment (new, ptr, end);
391 #ifdef MSDOS
392 /* Restore our exception hooks. */
393 __djgpp_exception_toggle ();
395 /* Restore the startup flags. */
396 _crt0_startup_flags = save_djgpp_startup_flags;
398 /* Restore the atexit chain. */
399 __atexit_ptr = save_atexit_ptr;
400 #endif
403 return 0;
406 /* ****************************************************************
407 * copy_sym
409 * Copy the relocation information and symbol table from the a.out to the new
411 static int
412 copy_sym (int new, int a_out, const char *a_name, const char *new_name)
414 char page[1024];
415 int n;
417 if (a_out < 0)
418 return 0;
420 if (SYMS_START == 0L)
421 return 0;
423 if (lnnoptr) /* if there is line number info */
424 lseek (a_out, coff_offset + lnnoptr, 0); /* start copying from there */
425 else
426 lseek (a_out, coff_offset + SYMS_START, 0); /* Position a.out to symtab. */
428 while ((n = read (a_out, page, sizeof page)) > 0)
430 if (write (new, page, n) != n)
432 PERROR (new_name);
435 if (n < 0)
437 PERROR (a_name);
439 return 0;
442 /* ****************************************************************
443 * mark_x
445 * After successfully building the new a.out, mark it executable
447 static void
448 mark_x (const char *name)
450 struct stat sbuf;
451 int um;
452 int new = 0; /* for PERROR */
454 um = umask (777);
455 umask (um);
456 if (stat (name, &sbuf) == -1)
458 PERROR (name);
460 sbuf.st_mode |= 0111 & ~um;
461 if (chmod (name, sbuf.st_mode) == -1)
462 PERROR (name);
467 * If the COFF file contains a symbol table and a line number section,
468 * then any auxiliary entries that have values for x_lnnoptr must
469 * be adjusted by the amount that the line number section has moved
470 * in the file (bias computed in make_hdr). The #@$%&* designers of
471 * the auxiliary entry structures used the absolute file offsets for
472 * the line number entry rather than an offset from the start of the
473 * line number section!
475 * When I figure out how to scan through the symbol table and pick out
476 * the auxiliary entries that need adjustment, this routine will
477 * be fixed. As it is now, all such entries are wrong and sdb
478 * will complain. Fred Fish, UniSoft Systems Inc.
481 /* This function is probably very slow. Instead of reopening the new
482 file for input and output it should copy from the old to the new
483 using the two descriptors already open (WRITEDESC and READDESC).
484 Instead of reading one small structure at a time it should use
485 a reasonable size buffer. But I don't have time to work on such
486 things, so I am installing it as submitted to me. -- RMS. */
489 adjust_lnnoptrs (int writedesc, int readdesc, const char *new_name)
491 register int nsyms;
492 register int new;
493 struct syment symentry;
494 union auxent auxentry;
496 if (!lnnoptr || !f_hdr.f_symptr)
497 return 0;
499 #ifdef MSDOS
500 if ((new = writedesc) < 0)
501 #else
502 if ((new = emacs_open (new_name, O_RDWR, 0)) < 0)
503 #endif
505 PERROR (new_name);
506 return -1;
509 lseek (new, f_hdr.f_symptr, 0);
510 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
512 read (new, &symentry, SYMESZ);
513 if (symentry.n_numaux)
515 read (new, &auxentry, AUXESZ);
516 nsyms++;
517 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400)
519 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
520 lseek (new, -AUXESZ, 1);
521 write (new, &auxentry, AUXESZ);
525 #ifndef MSDOS
526 emacs_close (new);
527 #endif
528 return 0;
531 /* ****************************************************************
532 * unexec
534 * driving logic.
536 void
537 unexec (const char *new_name, const char *a_name)
539 int new = -1, a_out = -1;
541 if (a_name && (a_out = emacs_open (a_name, O_RDONLY, 0)) < 0)
543 PERROR (a_name);
545 if ((new = emacs_open (new_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
547 PERROR (new_name);
550 if (make_hdr (new, a_out, a_name, new_name) < 0
551 || copy_text_and_data (new, a_out) < 0
552 || copy_sym (new, a_out, a_name, new_name) < 0
553 || adjust_lnnoptrs (new, a_out, new_name) < 0
556 emacs_close (new);
557 return;
560 emacs_close (new);
561 if (a_out >= 0)
562 emacs_close (a_out);
563 mark_x (new_name);
566 #endif /* not CANNOT_DUMP */