]> code.delx.au - gnu-emacs/blob - src/unexcoff.c
Fix missing prototypes for HAVE_NS (caused crash) and vrious warnings.
[gnu-emacs] / src / unexcoff.c
1 /* Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 2001, 2002, 2003,
2 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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/>. */
18
19
20 /*
21 * unexcoff.c - Convert a running program into an a.out or COFF file.
22 *
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 * ==================================================================
28 *
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.
35 *
36 * Synopsis:
37 * unexec (new_name, a_name, data_start, bss_start, entry_address)
38 * char *new_name, *a_name;
39 * unsigned data_start, bss_start, entry_address;
40 *
41 * Takes a snapshot of the program and makes an a.out format file in the
42 * file named by the string argument new_name.
43 * If a_name is non-NULL, the symbol table will be taken from the given file.
44 * On some machines, an existing a_name file is required.
45 *
46 * The boundaries within the a.out file may be adjusted with the data_start
47 * and bss_start arguments. Either or both may be given as 0 for defaults.
48 *
49 * Data_start gives the boundary between the text segment and the data
50 * segment of the program. The text segment can contain shared, read-only
51 * program code and literal data, while the data segment is always unshared
52 * and unprotected. Data_start gives the lowest unprotected address.
53 * The value you specify may be rounded down to a suitable boundary
54 * as required by the machine you are using.
55 *
56 * Specifying zero for data_start means the boundary between text and data
57 * should not be the same as when the program was loaded.
58 *
59 * Bss_start indicates how much of the data segment is to be saved in the
60 * a.out file and restored when the program is executed. It gives the lowest
61 * unsaved address, and is rounded up to a page boundary. The default when 0
62 * is given assumes that the entire data segment is to be stored, including
63 * the previous data and bss as well as any additional storage allocated with
64 * break (2).
65 *
66 * The new file is set up to start at entry_address.
67 *
68 * If you make improvements I'd like to get them too.
69 * harpo!utah-cs!thomas, thomas@Utah-20
70 *
71 */
72
73 /* Modified to support SysVr3 shared libraries by James Van Artsdalen
74 * of Dell Computer Corporation. james@bigtex.cactus.org.
75 */
76
77 #ifndef emacs
78 #define PERROR(arg) perror (arg); return -1
79 #else
80 #include <config.h>
81 #define PERROR(file) report_error (file, new)
82 #endif
83
84 #ifndef CANNOT_DUMP /* all rest of file! */
85
86 #ifdef HAVE_COFF_H
87 #include <coff.h>
88 #ifdef MSDOS
89 #include <fcntl.h> /* for O_RDONLY, O_RDWR */
90 #include <crt0.h> /* for _crt0_startup_flags and its bits */
91 static int save_djgpp_startup_flags;
92 #define filehdr external_filehdr
93 #define scnhdr external_scnhdr
94 #define syment external_syment
95 #define auxent external_auxent
96 #define n_numaux e_numaux
97 #define n_type e_type
98 struct aouthdr
99 {
100 unsigned short magic; /* type of file */
101 unsigned short vstamp; /* version stamp */
102 unsigned long tsize; /* text size in bytes, padded to FW bdry*/
103 unsigned long dsize; /* initialized data " " */
104 unsigned long bsize; /* uninitialized data " " */
105 unsigned long entry; /* entry pt. */
106 unsigned long text_start;/* base of text used for this file */
107 unsigned long data_start;/* base of data used for this file */
108 };
109 #endif /* not MSDOS */
110 #else /* not HAVE_COFF_H */
111 #include <a.out.h>
112 #endif /* not HAVE_COFF_H */
113
114 /* Define getpagesize if the system does not.
115 Note that this may depend on symbols defined in a.out.h. */
116 #include "getpagesize.h"
117
118 #ifndef makedev /* Try to detect types.h already loaded */
119 #include <sys/types.h>
120 #endif /* makedev */
121 #include <stdio.h>
122 #include <sys/stat.h>
123 #include <errno.h>
124
125 #include <sys/file.h>
126
127 #ifndef O_RDONLY
128 #define O_RDONLY 0
129 #endif
130 #ifndef O_RDWR
131 #define O_RDWR 2
132 #endif
133
134
135 extern char *start_of_text (); /* Start of text */
136 extern char *start_of_data (); /* Start of initialized data */
137
138 static long block_copy_start; /* Old executable start point */
139 static struct filehdr f_hdr; /* File header */
140 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
141 long bias; /* Bias to add for growth */
142 long lnnoptr; /* Pointer to line-number info within file */
143 #define SYMS_START block_copy_start
144
145 static long text_scnptr;
146 static long data_scnptr;
147
148 static long coff_offset;
149
150 static int pagemask;
151
152 /* Correct an int which is the bit pattern of a pointer to a byte
153 into an int which is the number of a byte.
154 This is a no-op on ordinary machines, but not on all. */
155
156 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
157
158 #ifdef emacs
159
160 #include <setjmp.h>
161 #include "lisp.h"
162
163 static
164 report_error (file, fd)
165 char *file;
166 int fd;
167 {
168 if (fd)
169 close (fd);
170 report_file_error ("Cannot unexec", Fcons (build_string (file), Qnil));
171 }
172 #endif /* emacs */
173
174 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
175 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
176 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
177
178 static
179 report_error_1 (fd, msg, a1, a2)
180 int fd;
181 char *msg;
182 int a1, a2;
183 {
184 close (fd);
185 #ifdef emacs
186 error (msg, a1, a2);
187 #else
188 fprintf (stderr, msg, a1, a2);
189 fprintf (stderr, "\n");
190 #endif
191 }
192 \f
193 static int make_hdr ();
194 static int copy_text_and_data ();
195 static int copy_sym ();
196 static void mark_x ();
197
198 /* ****************************************************************
199 * make_hdr
200 *
201 * Make the header in the new a.out from the header in core.
202 * Modify the text and data sizes.
203 */
204 static int
205 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
206 int new, a_out;
207 unsigned data_start, bss_start, entry_address;
208 char *a_name;
209 char *new_name;
210 {
211 int tem;
212 auto struct scnhdr f_thdr; /* Text section header */
213 auto struct scnhdr f_dhdr; /* Data section header */
214 auto struct scnhdr f_bhdr; /* Bss section header */
215 auto struct scnhdr scntemp; /* Temporary section header */
216 register int scns;
217 unsigned int bss_end;
218
219 pagemask = getpagesize () - 1;
220
221 /* Adjust text/data boundary. */
222 data_start = (int) start_of_data ();
223 data_start = ADDR_CORRECT (data_start);
224 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
225
226 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask;
227 bss_end &= ~ pagemask;
228
229 /* Adjust data/bss boundary. */
230 if (bss_start != 0)
231 {
232 bss_start = (ADDR_CORRECT (bss_start) + pagemask);
233 /* (Up) to page bdry. */
234 bss_start &= ~ pagemask;
235 if (bss_start > bss_end)
236 {
237 ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
238 bss_start);
239 }
240 }
241 else
242 bss_start = bss_end;
243
244 if (data_start > bss_start) /* Can't have negative data size. */
245 {
246 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
247 data_start, bss_start);
248 }
249
250 coff_offset = 0L; /* stays zero, except in DJGPP */
251
252 /* Salvage as much info from the existing file as possible */
253 if (a_out >= 0)
254 {
255 #ifdef MSDOS
256 /* Support the coff-go32-exe format with a prepended stub, since
257 this is what GCC 2.8.0 and later generates by default in DJGPP. */
258 unsigned short mz_header[3];
259
260 if (read (a_out, &mz_header, sizeof (mz_header)) != sizeof (mz_header))
261 {
262 PERROR (a_name);
263 }
264 if (mz_header[0] == 0x5a4d || mz_header[0] == 0x4d5a) /* "MZ" or "ZM" */
265 {
266 coff_offset = (long)mz_header[2] * 512L;
267 if (mz_header[1])
268 coff_offset += (long)mz_header[1] - 512L;
269 lseek (a_out, coff_offset, 0);
270 }
271 else
272 lseek (a_out, 0L, 0);
273 #endif /* MSDOS */
274 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
275 {
276 PERROR (a_name);
277 }
278 block_copy_start += sizeof (f_hdr);
279 if (f_hdr.f_opthdr > 0)
280 {
281 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
282 {
283 PERROR (a_name);
284 }
285 block_copy_start += sizeof (f_ohdr);
286 }
287 /* Loop through section headers, copying them in */
288 lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0);
289 for (scns = f_hdr.f_nscns; scns > 0; scns--) {
290 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
291 {
292 PERROR (a_name);
293 }
294 if (scntemp.s_scnptr > 0L)
295 {
296 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
297 block_copy_start = scntemp.s_scnptr + scntemp.s_size;
298 }
299 if (strcmp (scntemp.s_name, ".text") == 0)
300 {
301 f_thdr = scntemp;
302 }
303 else if (strcmp (scntemp.s_name, ".data") == 0)
304 {
305 f_dhdr = scntemp;
306 }
307 else if (strcmp (scntemp.s_name, ".bss") == 0)
308 {
309 f_bhdr = scntemp;
310 }
311 }
312 }
313 else
314 {
315 ERROR0 ("can't build a COFF file from scratch yet");
316 }
317
318 /* Now we alter the contents of all the f_*hdr variables
319 to correspond to what we want to dump. */
320
321 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
322 f_ohdr.text_start = (long) start_of_text ();
323 f_ohdr.tsize = data_start - f_ohdr.text_start;
324 f_ohdr.data_start = data_start;
325 f_ohdr.dsize = bss_start - f_ohdr.data_start;
326 f_ohdr.bsize = bss_end - bss_start;
327 f_thdr.s_size = f_ohdr.tsize;
328 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
329 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
330 lnnoptr = f_thdr.s_lnnoptr;
331 text_scnptr = f_thdr.s_scnptr;
332 f_dhdr.s_paddr = f_ohdr.data_start;
333 f_dhdr.s_vaddr = f_ohdr.data_start;
334 f_dhdr.s_size = f_ohdr.dsize;
335 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
336 data_scnptr = f_dhdr.s_scnptr;
337 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
338 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
339 f_bhdr.s_size = f_ohdr.bsize;
340 f_bhdr.s_scnptr = 0L;
341 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
342
343 if (f_hdr.f_symptr > 0L)
344 {
345 f_hdr.f_symptr += bias;
346 }
347
348 if (f_thdr.s_lnnoptr > 0L)
349 {
350 f_thdr.s_lnnoptr += bias;
351 }
352
353 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
354 {
355 PERROR (new_name);
356 }
357
358 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
359 {
360 PERROR (new_name);
361 }
362
363 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
364 {
365 PERROR (new_name);
366 }
367
368 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
369 {
370 PERROR (new_name);
371 }
372
373 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
374 {
375 PERROR (new_name);
376 }
377
378 return (0);
379
380 }
381 \f
382 write_segment (new, ptr, end)
383 int new;
384 register char *ptr, *end;
385 {
386 register int i, nwrite, ret;
387 char buf[80];
388 /* This is the normal amount to write at once.
389 It is the size of block that NFS uses. */
390 int writesize = 1 << 13;
391 int pagesize = getpagesize ();
392 char zeros[1 << 13];
393
394 memset (zeros, 0, sizeof (zeros));
395
396 for (i = 0; ptr < end;)
397 {
398 /* Distance to next multiple of writesize. */
399 nwrite = (((int) ptr + writesize) & -writesize) - (int) ptr;
400 /* But not beyond specified end. */
401 if (nwrite > end - ptr) nwrite = end - ptr;
402 ret = write (new, ptr, nwrite);
403 /* If write gets a page fault, it means we reached
404 a gap between the old text segment and the old data segment.
405 This gap has probably been remapped into part of the text segment.
406 So write zeros for it. */
407 if (ret == -1
408 #ifdef EFAULT
409 && errno == EFAULT
410 #endif
411 )
412 {
413 /* Write only a page of zeros at once,
414 so that we don't overshoot the start
415 of the valid memory in the old data segment. */
416 if (nwrite > pagesize)
417 nwrite = pagesize;
418 write (new, zeros, nwrite);
419 }
420 #if 0 /* Now that we have can ask `write' to write more than a page,
421 it is legit for write do less than the whole amount specified. */
422 else if (nwrite != ret)
423 {
424 sprintf (buf,
425 "unexec write failure: addr 0x%x, fileno %d, size 0x%x, wrote 0x%x, errno %d",
426 ptr, new, nwrite, ret, errno);
427 PERROR (buf);
428 }
429 #endif
430 i += nwrite;
431 ptr += nwrite;
432 }
433 }
434 /* ****************************************************************
435 * copy_text_and_data
436 *
437 * Copy the text and data segments from memory to the new a.out
438 */
439 static int
440 copy_text_and_data (new, a_out)
441 int new, a_out;
442 {
443 register char *end;
444 register char *ptr;
445
446 #ifdef MSDOS
447 /* Dump the original table of exception handlers, not the one
448 where our exception hooks are registered. */
449 __djgpp_exception_toggle ();
450
451 /* Switch off startup flags that might have been set at runtime
452 and which might change the way that dumped Emacs works. */
453 save_djgpp_startup_flags = _crt0_startup_flags;
454 _crt0_startup_flags &= ~(_CRT0_FLAG_NO_LFN | _CRT0_FLAG_NEARPTR);
455 #endif
456
457 lseek (new, (long) text_scnptr, 0);
458 ptr = (char *) f_ohdr.text_start;
459 end = ptr + f_ohdr.tsize;
460 write_segment (new, ptr, end);
461
462 lseek (new, (long) data_scnptr, 0);
463 ptr = (char *) f_ohdr.data_start;
464 end = ptr + f_ohdr.dsize;
465 write_segment (new, ptr, end);
466
467 #ifdef MSDOS
468 /* Restore our exception hooks. */
469 __djgpp_exception_toggle ();
470
471 /* Restore the startup flags. */
472 _crt0_startup_flags = save_djgpp_startup_flags;
473 #endif
474
475
476 return 0;
477 }
478 \f
479 /* ****************************************************************
480 * copy_sym
481 *
482 * Copy the relocation information and symbol table from the a.out to the new
483 */
484 static int
485 copy_sym (new, a_out, a_name, new_name)
486 int new, a_out;
487 char *a_name, *new_name;
488 {
489 char page[1024];
490 int n;
491
492 if (a_out < 0)
493 return 0;
494
495 if (SYMS_START == 0L)
496 return 0;
497
498 if (lnnoptr) /* if there is line number info */
499 lseek (a_out, coff_offset + lnnoptr, 0); /* start copying from there */
500 else
501 lseek (a_out, coff_offset + SYMS_START, 0); /* Position a.out to symtab. */
502
503 while ((n = read (a_out, page, sizeof page)) > 0)
504 {
505 if (write (new, page, n) != n)
506 {
507 PERROR (new_name);
508 }
509 }
510 if (n < 0)
511 {
512 PERROR (a_name);
513 }
514 return 0;
515 }
516 \f
517 /* ****************************************************************
518 * mark_x
519 *
520 * After successfully building the new a.out, mark it executable
521 */
522 static void
523 mark_x (name)
524 char *name;
525 {
526 struct stat sbuf;
527 int um;
528 int new = 0; /* for PERROR */
529
530 um = umask (777);
531 umask (um);
532 if (stat (name, &sbuf) == -1)
533 {
534 PERROR (name);
535 }
536 sbuf.st_mode |= 0111 & ~um;
537 if (chmod (name, sbuf.st_mode) == -1)
538 PERROR (name);
539 }
540 \f
541
542 /*
543 * If the COFF file contains a symbol table and a line number section,
544 * then any auxiliary entries that have values for x_lnnoptr must
545 * be adjusted by the amount that the line number section has moved
546 * in the file (bias computed in make_hdr). The #@$%&* designers of
547 * the auxiliary entry structures used the absolute file offsets for
548 * the line number entry rather than an offset from the start of the
549 * line number section!
550 *
551 * When I figure out how to scan through the symbol table and pick out
552 * the auxiliary entries that need adjustment, this routine will
553 * be fixed. As it is now, all such entries are wrong and sdb
554 * will complain. Fred Fish, UniSoft Systems Inc.
555 */
556
557 /* This function is probably very slow. Instead of reopening the new
558 file for input and output it should copy from the old to the new
559 using the two descriptors already open (WRITEDESC and READDESC).
560 Instead of reading one small structure at a time it should use
561 a reasonable size buffer. But I don't have time to work on such
562 things, so I am installing it as submitted to me. -- RMS. */
563
564 adjust_lnnoptrs (writedesc, readdesc, new_name)
565 int writedesc;
566 int readdesc;
567 char *new_name;
568 {
569 register int nsyms;
570 register int new;
571 struct syment symentry;
572 union auxent auxentry;
573
574 if (!lnnoptr || !f_hdr.f_symptr)
575 return 0;
576
577 #ifdef MSDOS
578 if ((new = writedesc) < 0)
579 #else
580 if ((new = open (new_name, O_RDWR)) < 0)
581 #endif
582 {
583 PERROR (new_name);
584 return -1;
585 }
586
587 lseek (new, f_hdr.f_symptr, 0);
588 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
589 {
590 read (new, &symentry, SYMESZ);
591 if (symentry.n_numaux)
592 {
593 read (new, &auxentry, AUXESZ);
594 nsyms++;
595 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400)
596 {
597 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
598 lseek (new, -AUXESZ, 1);
599 write (new, &auxentry, AUXESZ);
600 }
601 }
602 }
603 #ifndef MSDOS
604 close (new);
605 #endif
606 return 0;
607 }
608
609 extern unsigned start __asm__ ("start");
610
611 /*
612 * Return the address of the start of the text segment prior to
613 * doing an unexec. After unexec the return value is undefined.
614 * See crt0.c for further explanation and _start.
615 *
616 */
617
618 char *
619 start_of_text (void)
620 {
621 return ((char *) &start);
622 }
623
624 /* ****************************************************************
625 * unexec
626 *
627 * driving logic.
628 */
629 unexec (new_name, a_name, data_start, bss_start, entry_address)
630 char *new_name, *a_name;
631 unsigned data_start, bss_start, entry_address;
632 {
633 int new, a_out = -1;
634
635 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
636 {
637 PERROR (a_name);
638 }
639 if ((new = creat (new_name, 0666)) < 0)
640 {
641 PERROR (new_name);
642 }
643
644 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0
645 || copy_text_and_data (new, a_out) < 0
646 || copy_sym (new, a_out, a_name, new_name) < 0
647 || adjust_lnnoptrs (new, a_out, new_name) < 0
648 )
649 {
650 close (new);
651 /* unlink (new_name); /* Failed, unlink new a.out */
652 return -1;
653 }
654
655 close (new);
656 if (a_out >= 0)
657 close (a_out);
658 mark_x (new_name);
659 return 0;
660 }
661
662 #endif /* not CANNOT_DUMP */
663
664 /* arch-tag: 62409b69-e27a-4a7c-9413-0210d6b54e7f
665 (do not change this comment) */