ChangeLog fix
[bpt/emacs.git] / src / unexcoff.c
CommitLineData
ba318903 1/* Copyright (C) 1985-1988, 1992-1994, 2001-2014 Free Software
ab422c4d 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 54#include "unexec.h"
406af475 55#include "lisp.h"
ce701a33 56
7dd63af1 57#define PERROR(file) report_error (file, new)
7dd63af1
RS
58
59#ifndef CANNOT_DUMP /* all rest of file! */
60
f914a6bf 61#ifdef HAVE_COFF_H
2a4487ac 62#include <coff.h>
3680bdc6 63#ifdef MSDOS
8eb2807f 64#include <fcntl.h> /* for O_RDONLY, O_RDWR */
c17a2102 65#include <crt0.h> /* for _crt0_startup_flags and its bits */
5f2f0bc1 66#include <sys/exceptn.h>
c17a2102 67static int save_djgpp_startup_flags;
3680bdc6
RS
68#define filehdr external_filehdr
69#define scnhdr external_scnhdr
70#define syment external_syment
71#define auxent external_auxent
72#define n_numaux e_numaux
73#define n_type e_type
74struct aouthdr
75{
234d3183
RS
76 unsigned short magic; /* type of file */
77 unsigned short vstamp; /* version stamp */
78 unsigned long tsize; /* text size in bytes, padded to FW bdry*/
79 unsigned long dsize; /* initialized data " " */
80 unsigned long bsize; /* uninitialized data " " */
81 unsigned long entry; /* entry pt. */
82 unsigned long text_start;/* base of text used for this file */
83 unsigned long data_start;/* base of data used for this file */
3680bdc6 84};
3680bdc6 85#endif /* not MSDOS */
f914a6bf 86#else /* not HAVE_COFF_H */
8d228cb0 87#include <a.out.h>
f914a6bf 88#endif /* not HAVE_COFF_H */
265a9e55 89
f34e2e18
RS
90/* Define getpagesize if the system does not.
91 Note that this may depend on symbols defined in a.out.h. */
7dd63af1
RS
92#include "getpagesize.h"
93
94#ifndef makedev /* Try to detect types.h already loaded */
95#include <sys/types.h>
265a9e55 96#endif /* makedev */
7dd63af1
RS
97#include <stdio.h>
98#include <sys/stat.h>
99#include <errno.h>
100
f914a6bf 101#include <sys/file.h>
2d30a233 102
1ddc2bd6 103extern int etext;
7dd63af1 104
7dd63af1
RS
105static long block_copy_start; /* Old executable start point */
106static struct filehdr f_hdr; /* File header */
107static struct aouthdr f_ohdr; /* Optional file header (a.out) */
108long bias; /* Bias to add for growth */
109long lnnoptr; /* Pointer to line-number info within file */
110#define SYMS_START block_copy_start
111
112static long text_scnptr;
113static long data_scnptr;
114
c8b14b5f
RS
115static long coff_offset;
116
7dd63af1
RS
117static int pagemask;
118
119/* Correct an int which is the bit pattern of a pointer to a byte
120 into an int which is the number of a byte.
121 This is a no-op on ordinary machines, but not on all. */
122
7dd63af1 123#define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
7dd63af1 124
2d30a233
RM
125#include "lisp.h"
126
5f2f0bc1
EZ
127static void
128report_error (const char *file, int fd)
7dd63af1 129{
a773ed9a 130 int err = errno;
7dd63af1 131 if (fd)
bacba3c2 132 emacs_close (fd);
a9757f6a 133 report_file_errno ("Cannot unexec", build_string (file), err);
7dd63af1 134}
7dd63af1
RS
135
136#define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
137#define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
138#define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
139
5f2f0bc1
EZ
140static void
141report_error_1 (int fd, const char *msg, int a1, int a2)
7dd63af1 142{
bacba3c2 143 emacs_close (fd);
7dd63af1 144 error (msg, a1, a2);
7dd63af1
RS
145}
146\f
c3911ead 147static int make_hdr (int, int, const char *, const char *);
5f2f0bc1
EZ
148static int copy_text_and_data (int, int);
149static int copy_sym (int, int, const char *, const char *);
150static void mark_x (const char *);
7dd63af1 151
7dd63af1
RS
152/* ****************************************************************
153 * make_hdr
154 *
155 * Make the header in the new a.out from the header in core.
156 * Modify the text and data sizes.
157 */
158static int
dd5ecd6b
DN
159make_hdr (int new, int a_out,
160 const char *a_name, const char *new_name)
7dd63af1 161{
7dd63af1
RS
162 auto struct scnhdr f_thdr; /* Text section header */
163 auto struct scnhdr f_dhdr; /* Data section header */
164 auto struct scnhdr f_bhdr; /* Bss section header */
165 auto struct scnhdr scntemp; /* Temporary section header */
166 register int scns;
dd5ecd6b
DN
167 unsigned int bss_start;
168 unsigned int data_start;
7dd63af1
RS
169
170 pagemask = getpagesize () - 1;
171
172 /* Adjust text/data boundary. */
1ddc2bd6 173 data_start = (int) DATA_START;
7dd63af1 174 data_start = ADDR_CORRECT (data_start);
7dd63af1 175 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
7dd63af1 176
dd5ecd6b
DN
177 bss_start = ADDR_CORRECT (sbrk (0)) + pagemask;
178 bss_start &= ~ pagemask;
7dd63af1
RS
179
180 if (data_start > bss_start) /* Can't have negative data size. */
181 {
182 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
183 data_start, bss_start);
184 }
185
c8b14b5f
RS
186 coff_offset = 0L; /* stays zero, except in DJGPP */
187
7dd63af1
RS
188 /* Salvage as much info from the existing file as possible */
189 if (a_out >= 0)
190 {
c8b14b5f 191#ifdef MSDOS
c8b14b5f
RS
192 /* Support the coff-go32-exe format with a prepended stub, since
193 this is what GCC 2.8.0 and later generates by default in DJGPP. */
194 unsigned short mz_header[3];
195
196 if (read (a_out, &mz_header, sizeof (mz_header)) != sizeof (mz_header))
197 {
198 PERROR (a_name);
199 }
200 if (mz_header[0] == 0x5a4d || mz_header[0] == 0x4d5a) /* "MZ" or "ZM" */
201 {
202 coff_offset = (long)mz_header[2] * 512L;
203 if (mz_header[1])
204 coff_offset += (long)mz_header[1] - 512L;
205 lseek (a_out, coff_offset, 0);
206 }
207 else
208 lseek (a_out, 0L, 0);
c8b14b5f 209#endif /* MSDOS */
7dd63af1
RS
210 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
211 {
212 PERROR (a_name);
213 }
214 block_copy_start += sizeof (f_hdr);
215 if (f_hdr.f_opthdr > 0)
216 {
217 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
218 {
219 PERROR (a_name);
220 }
221 block_copy_start += sizeof (f_ohdr);
222 }
223 /* Loop through section headers, copying them in */
c8b14b5f 224 lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0);
7dd63af1
RS
225 for (scns = f_hdr.f_nscns; scns > 0; scns--) {
226 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
227 {
228 PERROR (a_name);
229 }
230 if (scntemp.s_scnptr > 0L)
231 {
232 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
233 block_copy_start = scntemp.s_scnptr + scntemp.s_size;
234 }
235 if (strcmp (scntemp.s_name, ".text") == 0)
236 {
237 f_thdr = scntemp;
238 }
239 else if (strcmp (scntemp.s_name, ".data") == 0)
240 {
241 f_dhdr = scntemp;
242 }
243 else if (strcmp (scntemp.s_name, ".bss") == 0)
244 {
245 f_bhdr = scntemp;
246 }
247 }
248 }
249 else
250 {
251 ERROR0 ("can't build a COFF file from scratch yet");
252 }
253
254 /* Now we alter the contents of all the f_*hdr variables
255 to correspond to what we want to dump. */
256
7dd63af1 257 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
7dd63af1 258 f_ohdr.dsize = bss_start - f_ohdr.data_start;
dd5ecd6b 259 f_ohdr.bsize = 0;
7dd63af1
RS
260 f_thdr.s_size = f_ohdr.tsize;
261 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
262 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
7dd63af1 263 lnnoptr = f_thdr.s_lnnoptr;
7dd63af1 264 text_scnptr = f_thdr.s_scnptr;
7dd63af1 265 f_dhdr.s_paddr = f_ohdr.data_start;
7dd63af1
RS
266 f_dhdr.s_vaddr = f_ohdr.data_start;
267 f_dhdr.s_size = f_ohdr.dsize;
268 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
7dd63af1 269 data_scnptr = f_dhdr.s_scnptr;
7dd63af1 270 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
7dd63af1
RS
271 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
272 f_bhdr.s_size = f_ohdr.bsize;
273 f_bhdr.s_scnptr = 0L;
7dd63af1 274 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
7dd63af1
RS
275
276 if (f_hdr.f_symptr > 0L)
277 {
278 f_hdr.f_symptr += bias;
279 }
280
281 if (f_thdr.s_lnnoptr > 0L)
282 {
283 f_thdr.s_lnnoptr += bias;
284 }
285
7dd63af1
RS
286 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
287 {
288 PERROR (new_name);
289 }
290
291 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
292 {
293 PERROR (new_name);
294 }
295
7dd63af1
RS
296 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
297 {
298 PERROR (new_name);
299 }
300
301 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
302 {
303 PERROR (new_name);
304 }
305
306 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
307 {
308 PERROR (new_name);
309 }
310
7dd63af1
RS
311 return (0);
312
7dd63af1
RS
313}
314\f
5f2f0bc1
EZ
315void
316write_segment (int new, const char *ptr, const char *end)
730f4d72
EZ
317{
318 register int i, nwrite, ret;
730f4d72
EZ
319 /* This is the normal amount to write at once.
320 It is the size of block that NFS uses. */
321 int writesize = 1 << 13;
322 int pagesize = getpagesize ();
323 char zeros[1 << 13];
324
72af86bd 325 memset (zeros, 0, sizeof (zeros));
730f4d72
EZ
326
327 for (i = 0; ptr < end;)
328 {
329 /* Distance to next multiple of writesize. */
330 nwrite = (((int) ptr + writesize) & -writesize) - (int) ptr;
331 /* But not beyond specified end. */
332 if (nwrite > end - ptr) nwrite = end - ptr;
333 ret = write (new, ptr, nwrite);
334 /* If write gets a page fault, it means we reached
335 a gap between the old text segment and the old data segment.
336 This gap has probably been remapped into part of the text segment.
337 So write zeros for it. */
22626a85 338 if (ret == -1 && errno == EFAULT)
730f4d72
EZ
339 {
340 /* Write only a page of zeros at once,
2b34df4e 341 so that we don't overshoot the start
730f4d72
EZ
342 of the valid memory in the old data segment. */
343 if (nwrite > pagesize)
344 nwrite = pagesize;
345 write (new, zeros, nwrite);
346 }
730f4d72
EZ
347 i += nwrite;
348 ptr += nwrite;
349 }
350}
7dd63af1
RS
351/* ****************************************************************
352 * copy_text_and_data
353 *
354 * Copy the text and data segments from memory to the new a.out
355 */
356static int
5f2f0bc1 357copy_text_and_data (int new, int a_out)
7dd63af1
RS
358{
359 register char *end;
360 register char *ptr;
361
8eb2807f 362#ifdef MSDOS
8eb2807f
RS
363 /* Dump the original table of exception handlers, not the one
364 where our exception hooks are registered. */
365 __djgpp_exception_toggle ();
c17a2102
KH
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);
8eb2807f
RS
371#endif
372
7dd63af1
RS
373 lseek (new, (long) text_scnptr, 0);
374 ptr = (char *) f_ohdr.text_start;
7dd63af1
RS
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
8eb2807f 383#ifdef MSDOS
8eb2807f
RS
384 /* Restore our exception hooks. */
385 __djgpp_exception_toggle ();
c17a2102
KH
386
387 /* Restore the startup flags. */
388 _crt0_startup_flags = save_djgpp_startup_flags;
8eb2807f 389#endif
8eb2807f 390
7dd63af1
RS
391
392 return 0;
393}
7dd63af1
RS
394\f
395/* ****************************************************************
396 * copy_sym
397 *
398 * Copy the relocation information and symbol table from the a.out to the new
399 */
400static int
5f2f0bc1 401copy_sym (int new, int a_out, const char *a_name, const char *new_name)
7dd63af1
RS
402{
403 char page[1024];
404 int n;
405
406 if (a_out < 0)
407 return 0;
408
7dd63af1
RS
409 if (SYMS_START == 0L)
410 return 0;
7dd63af1 411
7dd63af1 412 if (lnnoptr) /* if there is line number info */
c8b14b5f 413 lseek (a_out, coff_offset + lnnoptr, 0); /* start copying from there */
7dd63af1 414 else
c8b14b5f 415 lseek (a_out, coff_offset + SYMS_START, 0); /* Position a.out to symtab. */
7dd63af1
RS
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 *
eb8c3be9 434 * After successfully building the new a.out, mark it executable
7dd63af1
RS
435 */
436static void
5f2f0bc1 437mark_x (const char *name)
7dd63af1
RS
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
7dd63af1
RS
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
5f2f0bc1
EZ
477int
478adjust_lnnoptrs (int writedesc, int readdesc, const char *new_name)
7dd63af1
RS
479{
480 register int nsyms;
481 register int new;
7dd63af1
RS
482 struct syment symentry;
483 union auxent auxentry;
7dd63af1
RS
484
485 if (!lnnoptr || !f_hdr.f_symptr)
486 return 0;
487
3680bdc6
RS
488#ifdef MSDOS
489 if ((new = writedesc) < 0)
490#else
406af475 491 if ((new = emacs_open (new_name, O_RDWR, 0)) < 0)
3680bdc6 492#endif
7dd63af1
RS
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++;
1ba3de00
RS
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 }
7dd63af1
RS
512 }
513 }
3680bdc6 514#ifndef MSDOS
bacba3c2 515 emacs_close (new);
3680bdc6
RS
516#endif
517 return 0;
7dd63af1
RS
518}
519
730f4d72
EZ
520/* ****************************************************************
521 * unexec
522 *
523 * driving logic.
524 */
381259ef 525void
dd5ecd6b 526unexec (const char *new_name, const char *a_name)
730f4d72 527{
5f2f0bc1 528 int new = -1, a_out = -1;
730f4d72 529
406af475 530 if (a_name && (a_out = emacs_open (a_name, O_RDONLY, 0)) < 0)
730f4d72
EZ
531 {
532 PERROR (a_name);
533 }
406af475 534 if ((new = emacs_open (new_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
730f4d72
EZ
535 {
536 PERROR (new_name);
537 }
538
dd5ecd6b 539 if (make_hdr (new, a_out, a_name, new_name) < 0
730f4d72
EZ
540 || copy_text_and_data (new, a_out) < 0
541 || copy_sym (new, a_out, a_name, new_name) < 0
730f4d72 542 || adjust_lnnoptrs (new, a_out, new_name) < 0
730f4d72
EZ
543 )
544 {
bacba3c2 545 emacs_close (new);
fffe2e14 546 return;
730f4d72
EZ
547 }
548
bacba3c2 549 emacs_close (new);
730f4d72 550 if (a_out >= 0)
bacba3c2 551 emacs_close (a_out);
730f4d72 552 mark_x (new_name);
730f4d72
EZ
553}
554
7dd63af1 555#endif /* not CANNOT_DUMP */