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