1 /* Copyright (C) 1985-1988, 1992-1994, 2001-2013 Free Software
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.
32 * Date: Tue Mar 2 1982
33 * Originally under the name unexec.c.
34 * Modified heavily since then.
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.
57 #define PERROR(file) report_error (file, new)
59 #ifndef CANNOT_DUMP /* all rest of file! */
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
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 */
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>
101 #include <sys/file.h>
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
;
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)
128 report_error (const char *file
, int 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
140 report_error_1 (int fd
, const char *msg
, int a1
, int 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 /* ****************************************************************
154 * Make the header in the new a.out from the header in core.
155 * Modify the text and data sizes.
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 */
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 */
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
))
199 if (mz_header
[0] == 0x5a4d || mz_header
[0] == 0x4d5a) /* "MZ" or "ZM" */
201 coff_offset
= (long)mz_header
[2] * 512L;
203 coff_offset
+= (long)mz_header
[1] - 512L;
204 lseek (a_out
, coff_offset
, 0);
207 lseek (a_out
, 0L, 0);
209 if (read (a_out
, &f_hdr
, sizeof (f_hdr
)) != sizeof (f_hdr
))
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
))
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
))
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)
238 else if (strcmp (scntemp
.s_name
, ".data") == 0)
242 else if (strcmp (scntemp
.s_name
, ".bss") == 0)
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
;
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
))
290 if (write (new, &f_ohdr
, sizeof (f_ohdr
)) != sizeof (f_ohdr
))
295 if (write (new, &f_thdr
, sizeof (f_thdr
)) != sizeof (f_thdr
))
300 if (write (new, &f_dhdr
, sizeof (f_dhdr
)) != sizeof (f_dhdr
))
305 if (write (new, &f_bhdr
, sizeof (f_bhdr
)) != sizeof (f_bhdr
))
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 ();
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
)
344 write (new, zeros
, nwrite
);
350 /* ****************************************************************
353 * Copy the text and data segments from memory to the new a.out
356 copy_text_and_data (int new, int a_out
)
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
);
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
);
383 /* Restore our exception hooks. */
384 __djgpp_exception_toggle ();
386 /* Restore the startup flags. */
387 _crt0_startup_flags
= save_djgpp_startup_flags
;
394 /* ****************************************************************
397 * Copy the relocation information and symbol table from the a.out to the new
400 copy_sym (int new, int a_out
, const char *a_name
, const char *new_name
)
408 if (SYMS_START
== 0L)
411 if (lnnoptr
) /* if there is line number info */
412 lseek (a_out
, coff_offset
+ lnnoptr
, 0); /* start copying from there */
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
)
430 /* ****************************************************************
433 * After successfully building the new a.out, mark it executable
436 mark_x (const char *name
)
440 int new = 0; /* for PERROR */
444 if (stat (name
, &sbuf
) == -1)
448 sbuf
.st_mode
|= 0111 & ~um
;
449 if (chmod (name
, sbuf
.st_mode
) == -1)
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
)
481 struct syment symentry
;
482 union auxent auxentry
;
484 if (!lnnoptr
|| !f_hdr
.f_symptr
)
488 if ((new = writedesc
) < 0)
490 if ((new = emacs_open (new_name
, O_RDWR
, 0)) < 0)
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
);
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
);
519 /* ****************************************************************
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)
533 if ((new = emacs_open (new_name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666)) < 0)
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
554 #endif /* not CANNOT_DUMP */