Minor EBADF fixes.
[emacs.git] / src / unexcoff.c
blob6b2a3336c8a99c8bb16af05e808877f3b90c040d
1 /* Copyright (C) 1985-1988, 1992-1994, 2001-2013 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 #define filehdr external_filehdr
69 #define scnhdr external_scnhdr
70 #define syment external_syment
71 #define auxent external_auxent
72 #define n_numaux e_numaux
73 #define n_type e_type
74 struct aouthdr
76 unsigned short magic; /* type of file */
77 unsigned short vstamp; /* version stamp */
78 unsigned long tsize; /* text size in bytes, padded to FW bdry*/
79 unsigned long dsize; /* initialized data " " */
80 unsigned long bsize; /* uninitialized data " " */
81 unsigned long entry; /* entry pt. */
82 unsigned long text_start;/* base of text used for this file */
83 unsigned long data_start;/* base of data used for this file */
85 #endif /* not MSDOS */
86 #else /* not HAVE_COFF_H */
87 #include <a.out.h>
88 #endif /* not HAVE_COFF_H */
90 /* Define getpagesize if the system does not.
91 Note that this may depend on symbols defined in a.out.h. */
92 #include "getpagesize.h"
94 #ifndef makedev /* Try to detect types.h already loaded */
95 #include <sys/types.h>
96 #endif /* makedev */
97 #include <stdio.h>
98 #include <sys/stat.h>
99 #include <errno.h>
101 #include <sys/file.h>
103 extern int etext;
105 static long block_copy_start; /* Old executable start point */
106 static struct filehdr f_hdr; /* File header */
107 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
108 long bias; /* Bias to add for growth */
109 long lnnoptr; /* Pointer to line-number info within file */
110 #define SYMS_START block_copy_start
112 static long text_scnptr;
113 static long data_scnptr;
115 static long coff_offset;
117 static int pagemask;
119 /* Correct an int which is the bit pattern of a pointer to a byte
120 into an int which is the number of a byte.
121 This is a no-op on ordinary machines, but not on all. */
123 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
125 #include "lisp.h"
127 static void
128 report_error (const char *file, int fd)
130 if (fd)
131 emacs_close (fd);
132 report_file_error ("Cannot unexec", Fcons (build_string (file), Qnil));
135 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
136 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
137 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
139 static void
140 report_error_1 (int fd, const char *msg, int a1, int a2)
142 emacs_close (fd);
143 error (msg, a1, a2);
146 static int make_hdr (int, int, const char *, const char *);
147 static int copy_text_and_data (int, int);
148 static int copy_sym (int, int, const char *, const char *);
149 static void mark_x (const char *);
151 /* ****************************************************************
152 * make_hdr
154 * Make the header in the new a.out from the header in core.
155 * Modify the text and data sizes.
157 static int
158 make_hdr (int new, int a_out,
159 const char *a_name, const char *new_name)
161 auto struct scnhdr f_thdr; /* Text section header */
162 auto struct scnhdr f_dhdr; /* Data section header */
163 auto struct scnhdr f_bhdr; /* Bss section header */
164 auto struct scnhdr scntemp; /* Temporary section header */
165 register int scns;
166 unsigned int bss_start;
167 unsigned int data_start;
169 pagemask = getpagesize () - 1;
171 /* Adjust text/data boundary. */
172 data_start = (int) DATA_START;
173 data_start = ADDR_CORRECT (data_start);
174 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
176 bss_start = ADDR_CORRECT (sbrk (0)) + pagemask;
177 bss_start &= ~ pagemask;
179 if (data_start > bss_start) /* Can't have negative data size. */
181 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
182 data_start, bss_start);
185 coff_offset = 0L; /* stays zero, except in DJGPP */
187 /* Salvage as much info from the existing file as possible */
188 if (a_out >= 0)
190 #ifdef MSDOS
191 /* Support the coff-go32-exe format with a prepended stub, since
192 this is what GCC 2.8.0 and later generates by default in DJGPP. */
193 unsigned short mz_header[3];
195 if (read (a_out, &mz_header, sizeof (mz_header)) != sizeof (mz_header))
197 PERROR (a_name);
199 if (mz_header[0] == 0x5a4d || mz_header[0] == 0x4d5a) /* "MZ" or "ZM" */
201 coff_offset = (long)mz_header[2] * 512L;
202 if (mz_header[1])
203 coff_offset += (long)mz_header[1] - 512L;
204 lseek (a_out, coff_offset, 0);
206 else
207 lseek (a_out, 0L, 0);
208 #endif /* MSDOS */
209 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
211 PERROR (a_name);
213 block_copy_start += sizeof (f_hdr);
214 if (f_hdr.f_opthdr > 0)
216 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
218 PERROR (a_name);
220 block_copy_start += sizeof (f_ohdr);
222 /* Loop through section headers, copying them in */
223 lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0);
224 for (scns = f_hdr.f_nscns; scns > 0; scns--) {
225 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
227 PERROR (a_name);
229 if (scntemp.s_scnptr > 0L)
231 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
232 block_copy_start = scntemp.s_scnptr + scntemp.s_size;
234 if (strcmp (scntemp.s_name, ".text") == 0)
236 f_thdr = scntemp;
238 else if (strcmp (scntemp.s_name, ".data") == 0)
240 f_dhdr = scntemp;
242 else if (strcmp (scntemp.s_name, ".bss") == 0)
244 f_bhdr = scntemp;
248 else
250 ERROR0 ("can't build a COFF file from scratch yet");
253 /* Now we alter the contents of all the f_*hdr variables
254 to correspond to what we want to dump. */
256 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
257 f_ohdr.dsize = bss_start - f_ohdr.data_start;
258 f_ohdr.bsize = 0;
259 f_thdr.s_size = f_ohdr.tsize;
260 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
261 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
262 lnnoptr = f_thdr.s_lnnoptr;
263 text_scnptr = f_thdr.s_scnptr;
264 f_dhdr.s_paddr = f_ohdr.data_start;
265 f_dhdr.s_vaddr = f_ohdr.data_start;
266 f_dhdr.s_size = f_ohdr.dsize;
267 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
268 data_scnptr = f_dhdr.s_scnptr;
269 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
270 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
271 f_bhdr.s_size = f_ohdr.bsize;
272 f_bhdr.s_scnptr = 0L;
273 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
275 if (f_hdr.f_symptr > 0L)
277 f_hdr.f_symptr += bias;
280 if (f_thdr.s_lnnoptr > 0L)
282 f_thdr.s_lnnoptr += bias;
285 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
287 PERROR (new_name);
290 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
292 PERROR (new_name);
295 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
297 PERROR (new_name);
300 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
302 PERROR (new_name);
305 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
307 PERROR (new_name);
310 return (0);
314 void
315 write_segment (int new, const char *ptr, const char *end)
317 register int i, nwrite, ret;
318 /* This is the normal amount to write at once.
319 It is the size of block that NFS uses. */
320 int writesize = 1 << 13;
321 int pagesize = getpagesize ();
322 char zeros[1 << 13];
324 memset (zeros, 0, sizeof (zeros));
326 for (i = 0; ptr < end;)
328 /* Distance to next multiple of writesize. */
329 nwrite = (((int) ptr + writesize) & -writesize) - (int) ptr;
330 /* But not beyond specified end. */
331 if (nwrite > end - ptr) nwrite = end - ptr;
332 ret = write (new, ptr, nwrite);
333 /* If write gets a page fault, it means we reached
334 a gap between the old text segment and the old data segment.
335 This gap has probably been remapped into part of the text segment.
336 So write zeros for it. */
337 if (ret == -1 && errno == EFAULT)
339 /* Write only a page of zeros at once,
340 so that we don't overshoot the start
341 of the valid memory in the old data segment. */
342 if (nwrite > pagesize)
343 nwrite = pagesize;
344 write (new, zeros, nwrite);
346 i += nwrite;
347 ptr += nwrite;
350 /* ****************************************************************
351 * copy_text_and_data
353 * Copy the text and data segments from memory to the new a.out
355 static int
356 copy_text_and_data (int new, int a_out)
358 register char *end;
359 register char *ptr;
361 #ifdef MSDOS
362 /* Dump the original table of exception handlers, not the one
363 where our exception hooks are registered. */
364 __djgpp_exception_toggle ();
366 /* Switch off startup flags that might have been set at runtime
367 and which might change the way that dumped Emacs works. */
368 save_djgpp_startup_flags = _crt0_startup_flags;
369 _crt0_startup_flags &= ~(_CRT0_FLAG_NO_LFN | _CRT0_FLAG_NEARPTR);
370 #endif
372 lseek (new, (long) text_scnptr, 0);
373 ptr = (char *) f_ohdr.text_start;
374 end = ptr + f_ohdr.tsize;
375 write_segment (new, ptr, end);
377 lseek (new, (long) data_scnptr, 0);
378 ptr = (char *) f_ohdr.data_start;
379 end = ptr + f_ohdr.dsize;
380 write_segment (new, ptr, end);
382 #ifdef MSDOS
383 /* Restore our exception hooks. */
384 __djgpp_exception_toggle ();
386 /* Restore the startup flags. */
387 _crt0_startup_flags = save_djgpp_startup_flags;
388 #endif
391 return 0;
394 /* ****************************************************************
395 * copy_sym
397 * Copy the relocation information and symbol table from the a.out to the new
399 static int
400 copy_sym (int new, int a_out, const char *a_name, const char *new_name)
402 char page[1024];
403 int n;
405 if (a_out < 0)
406 return 0;
408 if (SYMS_START == 0L)
409 return 0;
411 if (lnnoptr) /* if there is line number info */
412 lseek (a_out, coff_offset + lnnoptr, 0); /* start copying from there */
413 else
414 lseek (a_out, coff_offset + SYMS_START, 0); /* Position a.out to symtab. */
416 while ((n = read (a_out, page, sizeof page)) > 0)
418 if (write (new, page, n) != n)
420 PERROR (new_name);
423 if (n < 0)
425 PERROR (a_name);
427 return 0;
430 /* ****************************************************************
431 * mark_x
433 * After successfully building the new a.out, mark it executable
435 static void
436 mark_x (const char *name)
438 struct stat sbuf;
439 int um;
440 int new = 0; /* for PERROR */
442 um = umask (777);
443 umask (um);
444 if (stat (name, &sbuf) == -1)
446 PERROR (name);
448 sbuf.st_mode |= 0111 & ~um;
449 if (chmod (name, sbuf.st_mode) == -1)
450 PERROR (name);
455 * If the COFF file contains a symbol table and a line number section,
456 * then any auxiliary entries that have values for x_lnnoptr must
457 * be adjusted by the amount that the line number section has moved
458 * in the file (bias computed in make_hdr). The #@$%&* designers of
459 * the auxiliary entry structures used the absolute file offsets for
460 * the line number entry rather than an offset from the start of the
461 * line number section!
463 * When I figure out how to scan through the symbol table and pick out
464 * the auxiliary entries that need adjustment, this routine will
465 * be fixed. As it is now, all such entries are wrong and sdb
466 * will complain. Fred Fish, UniSoft Systems Inc.
469 /* This function is probably very slow. Instead of reopening the new
470 file for input and output it should copy from the old to the new
471 using the two descriptors already open (WRITEDESC and READDESC).
472 Instead of reading one small structure at a time it should use
473 a reasonable size buffer. But I don't have time to work on such
474 things, so I am installing it as submitted to me. -- RMS. */
477 adjust_lnnoptrs (int writedesc, int readdesc, const char *new_name)
479 register int nsyms;
480 register int new;
481 struct syment symentry;
482 union auxent auxentry;
484 if (!lnnoptr || !f_hdr.f_symptr)
485 return 0;
487 #ifdef MSDOS
488 if ((new = writedesc) < 0)
489 #else
490 if ((new = emacs_open (new_name, O_RDWR, 0)) < 0)
491 #endif
493 PERROR (new_name);
494 return -1;
497 lseek (new, f_hdr.f_symptr, 0);
498 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
500 read (new, &symentry, SYMESZ);
501 if (symentry.n_numaux)
503 read (new, &auxentry, AUXESZ);
504 nsyms++;
505 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400)
507 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
508 lseek (new, -AUXESZ, 1);
509 write (new, &auxentry, AUXESZ);
513 #ifndef MSDOS
514 emacs_close (new);
515 #endif
516 return 0;
519 /* ****************************************************************
520 * unexec
522 * driving logic.
524 void
525 unexec (const char *new_name, const char *a_name)
527 int new = -1, a_out = -1;
529 if (a_name && (a_out = emacs_open (a_name, O_RDONLY, 0)) < 0)
531 PERROR (a_name);
533 if ((new = emacs_open (new_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
535 PERROR (new_name);
538 if (make_hdr (new, a_out, a_name, new_name) < 0
539 || copy_text_and_data (new, a_out) < 0
540 || copy_sym (new, a_out, a_name, new_name) < 0
541 || adjust_lnnoptrs (new, a_out, new_name) < 0
544 emacs_close (new);
545 return;
548 emacs_close (new);
549 if (a_out >= 0)
550 emacs_close (a_out);
551 mark_x (new_name);
554 #endif /* not CANNOT_DUMP */