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