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