* lisp/gnus/gnus-sync.el (gnus-sync): Fix defgroup version.
[emacs.git] / src / unexcoff.c
blob3e9786348c3e7e546cbd61c3357d5b5a97c12605
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.
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 (new_name, a_name, data_start, bss_start, entry_address)
38 * char *new_name, *a_name;
39 * unsigned data_start, bss_start, entry_address;
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.
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.
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.
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.
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).
66 * The new file is set up to start at entry_address.
68 * If you make improvements I'd like to get them too.
69 * harpo!utah-cs!thomas, thomas@Utah-20
73 /* Modified to support SysVr3 shared libraries by James Van Artsdalen
74 * of Dell Computer Corporation. james@bigtex.cactus.org.
77 #include <config.h>
78 #define PERROR(file) report_error (file, new)
80 #ifndef CANNOT_DUMP /* all rest of file! */
82 #ifdef HAVE_COFF_H
83 #include <coff.h>
84 #ifdef MSDOS
85 #include <fcntl.h> /* for O_RDONLY, O_RDWR */
86 #include <crt0.h> /* for _crt0_startup_flags and its bits */
87 static int save_djgpp_startup_flags;
88 #define filehdr external_filehdr
89 #define scnhdr external_scnhdr
90 #define syment external_syment
91 #define auxent external_auxent
92 #define n_numaux e_numaux
93 #define n_type e_type
94 struct aouthdr
96 unsigned short magic; /* type of file */
97 unsigned short vstamp; /* version stamp */
98 unsigned long tsize; /* text size in bytes, padded to FW bdry*/
99 unsigned long dsize; /* initialized data " " */
100 unsigned long bsize; /* uninitialized data " " */
101 unsigned long entry; /* entry pt. */
102 unsigned long text_start;/* base of text used for this file */
103 unsigned long data_start;/* base of data used for this file */
105 #endif /* not MSDOS */
106 #else /* not HAVE_COFF_H */
107 #include <a.out.h>
108 #endif /* not HAVE_COFF_H */
110 /* Define getpagesize if the system does not.
111 Note that this may depend on symbols defined in a.out.h. */
112 #include "getpagesize.h"
114 #ifndef makedev /* Try to detect types.h already loaded */
115 #include <sys/types.h>
116 #endif /* makedev */
117 #include <stdio.h>
118 #include <sys/stat.h>
119 #include <errno.h>
121 #include <sys/file.h>
123 #ifndef O_RDONLY
124 #define O_RDONLY 0
125 #endif
126 #ifndef O_RDWR
127 #define O_RDWR 2
128 #endif
131 extern char *start_of_data (); /* Start of initialized data */
133 static long block_copy_start; /* Old executable start point */
134 static struct filehdr f_hdr; /* File header */
135 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
136 long bias; /* Bias to add for growth */
137 long lnnoptr; /* Pointer to line-number info within file */
138 #define SYMS_START block_copy_start
140 static long text_scnptr;
141 static long data_scnptr;
143 static long coff_offset;
145 static int pagemask;
147 /* Correct an int which is the bit pattern of a pointer to a byte
148 into an int which is the number of a byte.
149 This is a no-op on ordinary machines, but not on all. */
151 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
153 #include <setjmp.h>
154 #include "lisp.h"
156 static
157 report_error (file, fd)
158 char *file;
159 int fd;
161 if (fd)
162 close (fd);
163 report_file_error ("Cannot unexec", Fcons (build_string (file), Qnil));
166 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
167 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
168 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
170 static
171 report_error_1 (fd, msg, a1, a2)
172 int fd;
173 char *msg;
174 int a1, a2;
176 close (fd);
177 error (msg, a1, a2);
180 static int make_hdr ();
181 static int copy_text_and_data ();
182 static int copy_sym ();
183 static void mark_x ();
185 /* ****************************************************************
186 * make_hdr
188 * Make the header in the new a.out from the header in core.
189 * Modify the text and data sizes.
191 static int
192 make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name)
193 int new, a_out;
194 unsigned data_start, bss_start, entry_address;
195 char *a_name;
196 char *new_name;
198 int tem;
199 auto struct scnhdr f_thdr; /* Text section header */
200 auto struct scnhdr f_dhdr; /* Data section header */
201 auto struct scnhdr f_bhdr; /* Bss section header */
202 auto struct scnhdr scntemp; /* Temporary section header */
203 register int scns;
204 unsigned int bss_end;
206 pagemask = getpagesize () - 1;
208 /* Adjust text/data boundary. */
209 data_start = (int) start_of_data ();
210 data_start = ADDR_CORRECT (data_start);
211 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
213 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask;
214 bss_end &= ~ pagemask;
216 /* Adjust data/bss boundary. */
217 if (bss_start != 0)
219 bss_start = (ADDR_CORRECT (bss_start) + pagemask);
220 /* (Up) to page bdry. */
221 bss_start &= ~ pagemask;
222 if (bss_start > bss_end)
224 ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
225 bss_start);
228 else
229 bss_start = bss_end;
231 if (data_start > bss_start) /* Can't have negative data size. */
233 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
234 data_start, bss_start);
237 coff_offset = 0L; /* stays zero, except in DJGPP */
239 /* Salvage as much info from the existing file as possible */
240 if (a_out >= 0)
242 #ifdef MSDOS
243 /* Support the coff-go32-exe format with a prepended stub, since
244 this is what GCC 2.8.0 and later generates by default in DJGPP. */
245 unsigned short mz_header[3];
247 if (read (a_out, &mz_header, sizeof (mz_header)) != sizeof (mz_header))
249 PERROR (a_name);
251 if (mz_header[0] == 0x5a4d || mz_header[0] == 0x4d5a) /* "MZ" or "ZM" */
253 coff_offset = (long)mz_header[2] * 512L;
254 if (mz_header[1])
255 coff_offset += (long)mz_header[1] - 512L;
256 lseek (a_out, coff_offset, 0);
258 else
259 lseek (a_out, 0L, 0);
260 #endif /* MSDOS */
261 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
263 PERROR (a_name);
265 block_copy_start += sizeof (f_hdr);
266 if (f_hdr.f_opthdr > 0)
268 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
270 PERROR (a_name);
272 block_copy_start += sizeof (f_ohdr);
274 /* Loop through section headers, copying them in */
275 lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0);
276 for (scns = f_hdr.f_nscns; scns > 0; scns--) {
277 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
279 PERROR (a_name);
281 if (scntemp.s_scnptr > 0L)
283 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
284 block_copy_start = scntemp.s_scnptr + scntemp.s_size;
286 if (strcmp (scntemp.s_name, ".text") == 0)
288 f_thdr = scntemp;
290 else if (strcmp (scntemp.s_name, ".data") == 0)
292 f_dhdr = scntemp;
294 else if (strcmp (scntemp.s_name, ".bss") == 0)
296 f_bhdr = scntemp;
300 else
302 ERROR0 ("can't build a COFF file from scratch yet");
305 /* Now we alter the contents of all the f_*hdr variables
306 to correspond to what we want to dump. */
308 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
309 f_ohdr.dsize = bss_start - f_ohdr.data_start;
310 f_ohdr.bsize = bss_end - bss_start;
311 f_thdr.s_size = f_ohdr.tsize;
312 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
313 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
314 lnnoptr = f_thdr.s_lnnoptr;
315 text_scnptr = f_thdr.s_scnptr;
316 f_dhdr.s_paddr = f_ohdr.data_start;
317 f_dhdr.s_vaddr = f_ohdr.data_start;
318 f_dhdr.s_size = f_ohdr.dsize;
319 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
320 data_scnptr = f_dhdr.s_scnptr;
321 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
322 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
323 f_bhdr.s_size = f_ohdr.bsize;
324 f_bhdr.s_scnptr = 0L;
325 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
327 if (f_hdr.f_symptr > 0L)
329 f_hdr.f_symptr += bias;
332 if (f_thdr.s_lnnoptr > 0L)
334 f_thdr.s_lnnoptr += bias;
337 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
339 PERROR (new_name);
342 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
344 PERROR (new_name);
347 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
349 PERROR (new_name);
352 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
354 PERROR (new_name);
357 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
359 PERROR (new_name);
362 return (0);
366 write_segment (new, ptr, end)
367 int new;
368 register char *ptr, *end;
370 register int i, nwrite, ret;
371 char buf[80];
372 /* This is the normal amount to write at once.
373 It is the size of block that NFS uses. */
374 int writesize = 1 << 13;
375 int pagesize = getpagesize ();
376 char zeros[1 << 13];
378 memset (zeros, 0, sizeof (zeros));
380 for (i = 0; ptr < end;)
382 /* Distance to next multiple of writesize. */
383 nwrite = (((int) ptr + writesize) & -writesize) - (int) ptr;
384 /* But not beyond specified end. */
385 if (nwrite > end - ptr) nwrite = end - ptr;
386 ret = write (new, ptr, nwrite);
387 /* If write gets a page fault, it means we reached
388 a gap between the old text segment and the old data segment.
389 This gap has probably been remapped into part of the text segment.
390 So write zeros for it. */
391 if (ret == -1
392 #ifdef EFAULT
393 && errno == EFAULT
394 #endif
397 /* Write only a page of zeros at once,
398 so that we don't overshoot the start
399 of the valid memory in the old data segment. */
400 if (nwrite > pagesize)
401 nwrite = pagesize;
402 write (new, zeros, nwrite);
404 i += nwrite;
405 ptr += nwrite;
408 /* ****************************************************************
409 * copy_text_and_data
411 * Copy the text and data segments from memory to the new a.out
413 static int
414 copy_text_and_data (new, a_out)
415 int new, a_out;
417 register char *end;
418 register char *ptr;
420 #ifdef MSDOS
421 /* Dump the original table of exception handlers, not the one
422 where our exception hooks are registered. */
423 __djgpp_exception_toggle ();
425 /* Switch off startup flags that might have been set at runtime
426 and which might change the way that dumped Emacs works. */
427 save_djgpp_startup_flags = _crt0_startup_flags;
428 _crt0_startup_flags &= ~(_CRT0_FLAG_NO_LFN | _CRT0_FLAG_NEARPTR);
429 #endif
431 lseek (new, (long) text_scnptr, 0);
432 ptr = (char *) f_ohdr.text_start;
433 end = ptr + f_ohdr.tsize;
434 write_segment (new, ptr, end);
436 lseek (new, (long) data_scnptr, 0);
437 ptr = (char *) f_ohdr.data_start;
438 end = ptr + f_ohdr.dsize;
439 write_segment (new, ptr, end);
441 #ifdef MSDOS
442 /* Restore our exception hooks. */
443 __djgpp_exception_toggle ();
445 /* Restore the startup flags. */
446 _crt0_startup_flags = save_djgpp_startup_flags;
447 #endif
450 return 0;
453 /* ****************************************************************
454 * copy_sym
456 * Copy the relocation information and symbol table from the a.out to the new
458 static int
459 copy_sym (new, a_out, a_name, new_name)
460 int new, a_out;
461 char *a_name, *new_name;
463 char page[1024];
464 int n;
466 if (a_out < 0)
467 return 0;
469 if (SYMS_START == 0L)
470 return 0;
472 if (lnnoptr) /* if there is line number info */
473 lseek (a_out, coff_offset + lnnoptr, 0); /* start copying from there */
474 else
475 lseek (a_out, coff_offset + SYMS_START, 0); /* Position a.out to symtab. */
477 while ((n = read (a_out, page, sizeof page)) > 0)
479 if (write (new, page, n) != n)
481 PERROR (new_name);
484 if (n < 0)
486 PERROR (a_name);
488 return 0;
491 /* ****************************************************************
492 * mark_x
494 * After successfully building the new a.out, mark it executable
496 static void
497 mark_x (name)
498 char *name;
500 struct stat sbuf;
501 int um;
502 int new = 0; /* for PERROR */
504 um = umask (777);
505 umask (um);
506 if (stat (name, &sbuf) == -1)
508 PERROR (name);
510 sbuf.st_mode |= 0111 & ~um;
511 if (chmod (name, sbuf.st_mode) == -1)
512 PERROR (name);
517 * If the COFF file contains a symbol table and a line number section,
518 * then any auxiliary entries that have values for x_lnnoptr must
519 * be adjusted by the amount that the line number section has moved
520 * in the file (bias computed in make_hdr). The #@$%&* designers of
521 * the auxiliary entry structures used the absolute file offsets for
522 * the line number entry rather than an offset from the start of the
523 * line number section!
525 * When I figure out how to scan through the symbol table and pick out
526 * the auxiliary entries that need adjustment, this routine will
527 * be fixed. As it is now, all such entries are wrong and sdb
528 * will complain. Fred Fish, UniSoft Systems Inc.
531 /* This function is probably very slow. Instead of reopening the new
532 file for input and output it should copy from the old to the new
533 using the two descriptors already open (WRITEDESC and READDESC).
534 Instead of reading one small structure at a time it should use
535 a reasonable size buffer. But I don't have time to work on such
536 things, so I am installing it as submitted to me. -- RMS. */
538 adjust_lnnoptrs (writedesc, readdesc, new_name)
539 int writedesc;
540 int readdesc;
541 char *new_name;
543 register int nsyms;
544 register int new;
545 struct syment symentry;
546 union auxent auxentry;
548 if (!lnnoptr || !f_hdr.f_symptr)
549 return 0;
551 #ifdef MSDOS
552 if ((new = writedesc) < 0)
553 #else
554 if ((new = open (new_name, O_RDWR)) < 0)
555 #endif
557 PERROR (new_name);
558 return -1;
561 lseek (new, f_hdr.f_symptr, 0);
562 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
564 read (new, &symentry, SYMESZ);
565 if (symentry.n_numaux)
567 read (new, &auxentry, AUXESZ);
568 nsyms++;
569 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400)
571 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
572 lseek (new, -AUXESZ, 1);
573 write (new, &auxentry, AUXESZ);
577 #ifndef MSDOS
578 close (new);
579 #endif
580 return 0;
583 /* ****************************************************************
584 * unexec
586 * driving logic.
588 unexec (new_name, a_name, data_start, bss_start, entry_address)
589 char *new_name, *a_name;
590 unsigned data_start, bss_start, entry_address;
592 int new, a_out = -1;
594 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
596 PERROR (a_name);
598 if ((new = creat (new_name, 0666)) < 0)
600 PERROR (new_name);
603 if (make_hdr (new, a_out, data_start, bss_start, entry_address, a_name, new_name) < 0
604 || copy_text_and_data (new, a_out) < 0
605 || copy_sym (new, a_out, a_name, new_name) < 0
606 || adjust_lnnoptrs (new, a_out, new_name) < 0
609 close (new);
610 /* unlink (new_name); /* Failed, unlink new a.out */
611 return -1;
614 close (new);
615 if (a_out >= 0)
616 close (a_out);
617 mark_x (new_name);
618 return 0;
621 #endif /* not CANNOT_DUMP */
623 /* arch-tag: 62409b69-e27a-4a7c-9413-0210d6b54e7f
624 (do not change this comment) */