Merge from trunk
[bpt/emacs.git] / src / unexcoff.c
CommitLineData
429ab54e 1/* Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 2001, 2002, 2003,
114f9c96 2 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
7dd63af1
RS
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
7dd63af1 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
7dd63af1
RS
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
7dd63af1
RS
18
19
20/*
29cf3e20
EZ
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 * ==================================================================
7dd63af1
RS
28 *
29 * Author: Spencer W. Thomas
30 * Computer Science Dept.
31 * University of Utah
32 * Date: Tue Mar 2 1982
29cf3e20 33 * Originally under the name unexec.c.
7dd63af1
RS
34 * Modified heavily since then.
35 *
36 * Synopsis:
dd5ecd6b 37 * unexec (const char *new_name, const char *old_name);
7dd63af1
RS
38 *
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.
43 *
7dd63af1
RS
44 * If you make improvements I'd like to get them too.
45 * harpo!utah-cs!thomas, thomas@Utah-20
46 *
47 */
48
49/* Modified to support SysVr3 shared libraries by James Van Artsdalen
50 * of Dell Computer Corporation. james@bigtex.cactus.org.
51 */
52
18160b98 53#include <config.h>
7dd63af1 54#define PERROR(file) report_error (file, new)
7dd63af1
RS
55
56#ifndef CANNOT_DUMP /* all rest of file! */
57
f914a6bf 58#ifdef HAVE_COFF_H
2a4487ac 59#include <coff.h>
3680bdc6 60#ifdef MSDOS
8eb2807f 61#include <fcntl.h> /* for O_RDONLY, O_RDWR */
c17a2102 62#include <crt0.h> /* for _crt0_startup_flags and its bits */
5f2f0bc1 63#include <sys/exceptn.h>
c17a2102 64static int save_djgpp_startup_flags;
3680bdc6
RS
65#define filehdr external_filehdr
66#define scnhdr external_scnhdr
67#define syment external_syment
68#define auxent external_auxent
69#define n_numaux e_numaux
70#define n_type e_type
71struct aouthdr
72{
234d3183
RS
73 unsigned short magic; /* type of file */
74 unsigned short vstamp; /* version stamp */
75 unsigned long tsize; /* text size in bytes, padded to FW bdry*/
76 unsigned long dsize; /* initialized data " " */
77 unsigned long bsize; /* uninitialized data " " */
78 unsigned long entry; /* entry pt. */
79 unsigned long text_start;/* base of text used for this file */
80 unsigned long data_start;/* base of data used for this file */
3680bdc6 81};
3680bdc6 82#endif /* not MSDOS */
f914a6bf 83#else /* not HAVE_COFF_H */
8d228cb0 84#include <a.out.h>
f914a6bf 85#endif /* not HAVE_COFF_H */
265a9e55 86
f34e2e18
RS
87/* Define getpagesize if the system does not.
88 Note that this may depend on symbols defined in a.out.h. */
7dd63af1
RS
89#include "getpagesize.h"
90
91#ifndef makedev /* Try to detect types.h already loaded */
92#include <sys/types.h>
265a9e55 93#endif /* makedev */
7dd63af1
RS
94#include <stdio.h>
95#include <sys/stat.h>
96#include <errno.h>
97
f914a6bf 98#include <sys/file.h>
2d30a233 99
5f2f0bc1 100extern char *start_of_data (void); /* Start of initialized data */
7dd63af1 101
7dd63af1
RS
102static long block_copy_start; /* Old executable start point */
103static struct filehdr f_hdr; /* File header */
104static struct aouthdr f_ohdr; /* Optional file header (a.out) */
105long bias; /* Bias to add for growth */
106long lnnoptr; /* Pointer to line-number info within file */
107#define SYMS_START block_copy_start
108
109static long text_scnptr;
110static long data_scnptr;
111
c8b14b5f
RS
112static long coff_offset;
113
7dd63af1
RS
114static int pagemask;
115
116/* Correct an int which is the bit pattern of a pointer to a byte
117 into an int which is the number of a byte.
118 This is a no-op on ordinary machines, but not on all. */
119
7dd63af1 120#define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
7dd63af1 121
d7306fe6 122#include <setjmp.h>
2d30a233
RM
123#include "lisp.h"
124
5f2f0bc1
EZ
125static void
126report_error (const char *file, int fd)
7dd63af1
RS
127{
128 if (fd)
129 close (fd);
2d30a233 130 report_file_error ("Cannot unexec", Fcons (build_string (file), Qnil));
7dd63af1 131}
7dd63af1
RS
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
5f2f0bc1
EZ
137static void
138report_error_1 (int fd, const char *msg, int a1, int a2)
7dd63af1
RS
139{
140 close (fd);
7dd63af1 141 error (msg, a1, a2);
7dd63af1
RS
142}
143\f
c3911ead 144static int make_hdr (int, int, const char *, const char *);
5f2f0bc1
EZ
145static int copy_text_and_data (int, int);
146static int copy_sym (int, int, const char *, const char *);
147static void mark_x (const char *);
7dd63af1 148
7dd63af1
RS
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 */
155static int
dd5ecd6b
DN
156make_hdr (int new, int a_out,
157 const char *a_name, const char *new_name)
7dd63af1 158{
7dd63af1
RS
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;
dd5ecd6b
DN
164 unsigned int bss_start;
165 unsigned int data_start;
7dd63af1
RS
166
167 pagemask = getpagesize () - 1;
168
169 /* Adjust text/data boundary. */
7dd63af1 170 data_start = (int) start_of_data ();
7dd63af1 171 data_start = ADDR_CORRECT (data_start);
7dd63af1 172 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
7dd63af1 173
dd5ecd6b
DN
174 bss_start = ADDR_CORRECT (sbrk (0)) + pagemask;
175 bss_start &= ~ pagemask;
7dd63af1
RS
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
c8b14b5f
RS
183 coff_offset = 0L; /* stays zero, except in DJGPP */
184
7dd63af1
RS
185 /* Salvage as much info from the existing file as possible */
186 if (a_out >= 0)
187 {
c8b14b5f 188#ifdef MSDOS
c8b14b5f
RS
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);
c8b14b5f 206#endif /* MSDOS */
7dd63af1
RS
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 */
c8b14b5f 221 lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0);
7dd63af1
RS
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
7dd63af1 254 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
7dd63af1 255 f_ohdr.dsize = bss_start - f_ohdr.data_start;
dd5ecd6b 256 f_ohdr.bsize = 0;
7dd63af1
RS
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));
7dd63af1 260 lnnoptr = f_thdr.s_lnnoptr;
7dd63af1 261 text_scnptr = f_thdr.s_scnptr;
7dd63af1 262 f_dhdr.s_paddr = f_ohdr.data_start;
7dd63af1
RS
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;
7dd63af1 266 data_scnptr = f_dhdr.s_scnptr;
7dd63af1 267 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
7dd63af1
RS
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;
7dd63af1 271 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
7dd63af1
RS
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
7dd63af1
RS
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
7dd63af1
RS
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
7dd63af1
RS
308 return (0);
309
7dd63af1
RS
310}
311\f
5f2f0bc1
EZ
312void
313write_segment (int new, const char *ptr, const char *end)
730f4d72
EZ
314{
315 register int i, nwrite, ret;
730f4d72
EZ
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
72af86bd 322 memset (zeros, 0, sizeof (zeros));
730f4d72
EZ
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
336#ifdef EFAULT
337 && errno == EFAULT
338#endif
339 )
340 {
341 /* Write only a page of zeros at once,
2b34df4e 342 so that we don't overshoot the start
730f4d72
EZ
343 of the valid memory in the old data segment. */
344 if (nwrite > pagesize)
345 nwrite = pagesize;
346 write (new, zeros, nwrite);
347 }
730f4d72
EZ
348 i += nwrite;
349 ptr += nwrite;
350 }
351}
7dd63af1
RS
352/* ****************************************************************
353 * copy_text_and_data
354 *
355 * Copy the text and data segments from memory to the new a.out
356 */
357static int
5f2f0bc1 358copy_text_and_data (int new, int a_out)
7dd63af1
RS
359{
360 register char *end;
361 register char *ptr;
362
8eb2807f 363#ifdef MSDOS
8eb2807f
RS
364 /* Dump the original table of exception handlers, not the one
365 where our exception hooks are registered. */
366 __djgpp_exception_toggle ();
c17a2102
KH
367
368 /* Switch off startup flags that might have been set at runtime
369 and which might change the way that dumped Emacs works. */
370 save_djgpp_startup_flags = _crt0_startup_flags;
371 _crt0_startup_flags &= ~(_CRT0_FLAG_NO_LFN | _CRT0_FLAG_NEARPTR);
8eb2807f
RS
372#endif
373
7dd63af1
RS
374 lseek (new, (long) text_scnptr, 0);
375 ptr = (char *) f_ohdr.text_start;
7dd63af1
RS
376 end = ptr + f_ohdr.tsize;
377 write_segment (new, ptr, end);
378
379 lseek (new, (long) data_scnptr, 0);
380 ptr = (char *) f_ohdr.data_start;
381 end = ptr + f_ohdr.dsize;
382 write_segment (new, ptr, end);
383
8eb2807f 384#ifdef MSDOS
8eb2807f
RS
385 /* Restore our exception hooks. */
386 __djgpp_exception_toggle ();
c17a2102
KH
387
388 /* Restore the startup flags. */
389 _crt0_startup_flags = save_djgpp_startup_flags;
8eb2807f 390#endif
8eb2807f 391
7dd63af1
RS
392
393 return 0;
394}
7dd63af1
RS
395\f
396/* ****************************************************************
397 * copy_sym
398 *
399 * Copy the relocation information and symbol table from the a.out to the new
400 */
401static int
5f2f0bc1 402copy_sym (int new, int a_out, const char *a_name, const char *new_name)
7dd63af1
RS
403{
404 char page[1024];
405 int n;
406
407 if (a_out < 0)
408 return 0;
409
7dd63af1
RS
410 if (SYMS_START == 0L)
411 return 0;
7dd63af1 412
7dd63af1 413 if (lnnoptr) /* if there is line number info */
c8b14b5f 414 lseek (a_out, coff_offset + lnnoptr, 0); /* start copying from there */
7dd63af1 415 else
c8b14b5f 416 lseek (a_out, coff_offset + SYMS_START, 0); /* Position a.out to symtab. */
7dd63af1
RS
417
418 while ((n = read (a_out, page, sizeof page)) > 0)
419 {
420 if (write (new, page, n) != n)
421 {
422 PERROR (new_name);
423 }
424 }
425 if (n < 0)
426 {
427 PERROR (a_name);
428 }
429 return 0;
430}
431\f
432/* ****************************************************************
433 * mark_x
434 *
eb8c3be9 435 * After successfully building the new a.out, mark it executable
7dd63af1
RS
436 */
437static void
5f2f0bc1 438mark_x (const char *name)
7dd63af1
RS
439{
440 struct stat sbuf;
441 int um;
442 int new = 0; /* for PERROR */
443
444 um = umask (777);
445 umask (um);
446 if (stat (name, &sbuf) == -1)
447 {
448 PERROR (name);
449 }
450 sbuf.st_mode |= 0111 & ~um;
451 if (chmod (name, sbuf.st_mode) == -1)
452 PERROR (name);
453}
454\f
7dd63af1
RS
455
456/*
457 * If the COFF file contains a symbol table and a line number section,
458 * then any auxiliary entries that have values for x_lnnoptr must
459 * be adjusted by the amount that the line number section has moved
460 * in the file (bias computed in make_hdr). The #@$%&* designers of
461 * the auxiliary entry structures used the absolute file offsets for
462 * the line number entry rather than an offset from the start of the
463 * line number section!
464 *
465 * When I figure out how to scan through the symbol table and pick out
466 * the auxiliary entries that need adjustment, this routine will
467 * be fixed. As it is now, all such entries are wrong and sdb
468 * will complain. Fred Fish, UniSoft Systems Inc.
469 */
470
471/* This function is probably very slow. Instead of reopening the new
472 file for input and output it should copy from the old to the new
473 using the two descriptors already open (WRITEDESC and READDESC).
474 Instead of reading one small structure at a time it should use
475 a reasonable size buffer. But I don't have time to work on such
476 things, so I am installing it as submitted to me. -- RMS. */
477
5f2f0bc1
EZ
478int
479adjust_lnnoptrs (int writedesc, int readdesc, const char *new_name)
7dd63af1
RS
480{
481 register int nsyms;
482 register int new;
7dd63af1
RS
483 struct syment symentry;
484 union auxent auxentry;
7dd63af1
RS
485
486 if (!lnnoptr || !f_hdr.f_symptr)
487 return 0;
488
3680bdc6
RS
489#ifdef MSDOS
490 if ((new = writedesc) < 0)
491#else
2d30a233 492 if ((new = open (new_name, O_RDWR)) < 0)
3680bdc6 493#endif
7dd63af1
RS
494 {
495 PERROR (new_name);
496 return -1;
497 }
498
499 lseek (new, f_hdr.f_symptr, 0);
500 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
501 {
502 read (new, &symentry, SYMESZ);
503 if (symentry.n_numaux)
504 {
505 read (new, &auxentry, AUXESZ);
506 nsyms++;
1ba3de00
RS
507 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400)
508 {
509 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
510 lseek (new, -AUXESZ, 1);
511 write (new, &auxentry, AUXESZ);
512 }
7dd63af1
RS
513 }
514 }
3680bdc6 515#ifndef MSDOS
7dd63af1 516 close (new);
3680bdc6
RS
517#endif
518 return 0;
7dd63af1
RS
519}
520
730f4d72
EZ
521/* ****************************************************************
522 * unexec
523 *
524 * driving logic.
525 */
5f2f0bc1 526int
dd5ecd6b 527unexec (const char *new_name, const char *a_name)
730f4d72 528{
5f2f0bc1 529 int new = -1, a_out = -1;
730f4d72
EZ
530
531 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
532 {
533 PERROR (a_name);
534 }
535 if ((new = creat (new_name, 0666)) < 0)
536 {
537 PERROR (new_name);
538 }
539
dd5ecd6b 540 if (make_hdr (new, a_out, a_name, new_name) < 0
730f4d72
EZ
541 || copy_text_and_data (new, a_out) < 0
542 || copy_sym (new, a_out, a_name, new_name) < 0
730f4d72 543 || adjust_lnnoptrs (new, a_out, new_name) < 0
730f4d72
EZ
544 )
545 {
546 close (new);
730f4d72
EZ
547 return -1;
548 }
549
550 close (new);
551 if (a_out >= 0)
552 close (a_out);
553 mark_x (new_name);
554 return 0;
555}
556
7dd63af1 557#endif /* not CANNOT_DUMP */
ab5796a9
MB
558
559/* arch-tag: 62409b69-e27a-4a7c-9413-0210d6b54e7f
560 (do not change this comment) */