Port to pedantic memcpy
[emacs.git] / src / unexcoff.c
blob3f6549003a5a8a4a6864a37a2b339c1ffc19613e
1 /* Copyright (C) 1985-1988, 1992-1994, 2001-2015 Free Software
2 * 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 (const char *new_name, const char *old_name);
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.
44 * If you make improvements I'd like to get them too.
45 * harpo!utah-cs!thomas, thomas@Utah-20
49 /* Modified to support SysVr3 shared libraries by James Van Artsdalen
50 * of Dell Computer Corporation. james@bigtex.cactus.org.
53 #include <config.h>
54 #include "unexec.h"
55 #include "lisp.h"
57 #define PERROR(file) report_error (file, new)
59 #ifndef CANNOT_DUMP /* all rest of file! */
61 #ifdef HAVE_COFF_H
62 #include <coff.h>
63 #ifdef MSDOS
64 #include <fcntl.h> /* for O_RDONLY, O_RDWR */
65 #include <crt0.h> /* for _crt0_startup_flags and its bits */
66 #include <sys/exceptn.h>
67 static int save_djgpp_startup_flags;
68 #include <libc/atexit.h>
69 static struct __atexit *save_atexit_ptr;
70 #define filehdr external_filehdr
71 #define scnhdr external_scnhdr
72 #define syment external_syment
73 #define auxent external_auxent
74 #define n_numaux e_numaux
75 #define n_type e_type
76 struct aouthdr
78 unsigned short magic; /* type of file */
79 unsigned short vstamp; /* version stamp */
80 unsigned long tsize; /* text size in bytes, padded to FW bdry*/
81 unsigned long dsize; /* initialized data " " */
82 unsigned long bsize; /* uninitialized data " " */
83 unsigned long entry; /* entry pt. */
84 unsigned long text_start;/* base of text used for this file */
85 unsigned long data_start;/* base of data used for this file */
87 #endif /* MSDOS */
88 #else /* not HAVE_COFF_H */
89 #include <a.out.h>
90 #endif /* not HAVE_COFF_H */
92 /* Define getpagesize if the system does not.
93 Note that this may depend on symbols defined in a.out.h. */
94 #include "getpagesize.h"
96 #ifndef makedev /* Try to detect types.h already loaded */
97 #include <sys/types.h>
98 #endif /* makedev */
99 #include <stdio.h>
100 #include <errno.h>
102 #include <sys/file.h>
104 extern int etext;
106 static long block_copy_start; /* Old executable start point */
107 static struct filehdr f_hdr; /* File header */
108 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
109 long bias; /* Bias to add for growth */
110 long lnnoptr; /* Pointer to line-number info within file */
111 #define SYMS_START block_copy_start
113 static long text_scnptr;
114 static long data_scnptr;
116 static long coff_offset;
118 static int pagemask;
120 /* Correct an int which is the bit pattern of a pointer to a byte
121 into an int which is the number of a byte.
122 This is a no-op on ordinary machines, but not on all. */
124 #define ADDR_CORRECT(x) ((char *) (x) - (char *) 0)
126 #include "lisp.h"
128 static void
129 report_error (const char *file, int fd)
131 int err = errno;
132 if (fd)
133 emacs_close (fd);
134 report_file_errno ("Cannot unexec", build_string (file), err);
137 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
138 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
139 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
141 static void
142 report_error_1 (int fd, const char *msg, int a1, int a2)
144 emacs_close (fd);
145 error (msg, a1, a2);
148 static int make_hdr (int, int, const char *, const char *);
149 static int copy_text_and_data (int, int);
150 static int copy_sym (int, int, const char *, const char *);
151 static void mark_x (const char *);
153 /* ****************************************************************
154 * make_hdr
156 * Make the header in the new a.out from the header in core.
157 * Modify the text and data sizes.
159 static int
160 make_hdr (int new, int a_out,
161 const char *a_name, const char *new_name)
163 auto struct scnhdr f_thdr; /* Text section header */
164 auto struct scnhdr f_dhdr; /* Data section header */
165 auto struct scnhdr f_bhdr; /* Bss section header */
166 auto struct scnhdr scntemp; /* Temporary section header */
167 register int scns;
168 unsigned int bss_start;
169 unsigned int data_start;
171 pagemask = getpagesize () - 1;
173 /* Adjust text/data boundary. */
174 data_start = (int) DATA_START;
175 data_start = ADDR_CORRECT (data_start);
176 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
178 bss_start = ADDR_CORRECT (sbrk (0)) + pagemask;
179 bss_start &= ~ pagemask;
181 if (data_start > bss_start) /* Can't have negative data size. */
183 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
184 data_start, bss_start);
187 coff_offset = 0L; /* stays zero, except in DJGPP */
189 /* Salvage as much info from the existing file as possible */
190 if (a_out >= 0)
192 #ifdef MSDOS
193 /* Support the coff-go32-exe format with a prepended stub, since
194 this is what GCC 2.8.0 and later generates by default in DJGPP. */
195 unsigned short mz_header[3];
197 if (read (a_out, &mz_header, sizeof (mz_header)) != sizeof (mz_header))
199 PERROR (a_name);
201 if (mz_header[0] == 0x5a4d || mz_header[0] == 0x4d5a) /* "MZ" or "ZM" */
203 coff_offset = (long)mz_header[2] * 512L;
204 if (mz_header[1])
205 coff_offset += (long)mz_header[1] - 512L;
206 lseek (a_out, coff_offset, 0);
208 else
209 lseek (a_out, 0L, 0);
210 #endif /* MSDOS */
211 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
213 PERROR (a_name);
215 block_copy_start += sizeof (f_hdr);
216 if (f_hdr.f_opthdr > 0)
218 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
220 PERROR (a_name);
222 block_copy_start += sizeof (f_ohdr);
224 /* Loop through section headers, copying them in */
225 lseek (a_out, coff_offset + sizeof (f_hdr) + f_hdr.f_opthdr, 0);
226 for (scns = f_hdr.f_nscns; scns > 0; scns--) {
227 if (read (a_out, &scntemp, sizeof (scntemp)) != sizeof (scntemp))
229 PERROR (a_name);
231 if (scntemp.s_scnptr > 0L)
233 if (block_copy_start < scntemp.s_scnptr + scntemp.s_size)
234 block_copy_start = scntemp.s_scnptr + scntemp.s_size;
236 if (strcmp (scntemp.s_name, ".text") == 0)
238 f_thdr = scntemp;
240 else if (strcmp (scntemp.s_name, ".data") == 0)
242 f_dhdr = scntemp;
244 else if (strcmp (scntemp.s_name, ".bss") == 0)
246 f_bhdr = scntemp;
250 else
252 ERROR0 ("can't build a COFF file from scratch yet");
255 /* Now we alter the contents of all the f_*hdr variables
256 to correspond to what we want to dump. */
258 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
259 f_ohdr.dsize = bss_start - f_ohdr.data_start;
260 f_ohdr.bsize = 0;
261 f_thdr.s_size = f_ohdr.tsize;
262 f_thdr.s_scnptr = sizeof (f_hdr) + sizeof (f_ohdr);
263 f_thdr.s_scnptr += (f_hdr.f_nscns) * (sizeof (f_thdr));
264 lnnoptr = f_thdr.s_lnnoptr;
265 text_scnptr = f_thdr.s_scnptr;
266 f_dhdr.s_paddr = f_ohdr.data_start;
267 f_dhdr.s_vaddr = f_ohdr.data_start;
268 f_dhdr.s_size = f_ohdr.dsize;
269 f_dhdr.s_scnptr = f_thdr.s_scnptr + f_thdr.s_size;
270 data_scnptr = f_dhdr.s_scnptr;
271 f_bhdr.s_paddr = f_ohdr.data_start + f_ohdr.dsize;
272 f_bhdr.s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
273 f_bhdr.s_size = f_ohdr.bsize;
274 f_bhdr.s_scnptr = 0L;
275 bias = f_dhdr.s_scnptr + f_dhdr.s_size - block_copy_start;
277 if (f_hdr.f_symptr > 0L)
279 f_hdr.f_symptr += bias;
282 if (f_thdr.s_lnnoptr > 0L)
284 f_thdr.s_lnnoptr += bias;
287 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
289 PERROR (new_name);
292 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
294 PERROR (new_name);
297 if (write (new, &f_thdr, sizeof (f_thdr)) != sizeof (f_thdr))
299 PERROR (new_name);
302 if (write (new, &f_dhdr, sizeof (f_dhdr)) != sizeof (f_dhdr))
304 PERROR (new_name);
307 if (write (new, &f_bhdr, sizeof (f_bhdr)) != sizeof (f_bhdr))
309 PERROR (new_name);
312 return (0);
316 void
317 write_segment (int new, const char *ptr, const char *end)
319 register int i, nwrite, ret;
320 /* This is the normal amount to write at once.
321 It is the size of block that NFS uses. */
322 int writesize = 1 << 13;
323 int pagesize = getpagesize ();
324 char zeros[1 << 13];
326 memset (zeros, 0, sizeof (zeros));
328 for (i = 0; ptr < end;)
330 /* Distance to next multiple of writesize. */
331 nwrite = (((int) ptr + writesize) & -writesize) - (int) ptr;
332 /* But not beyond specified end. */
333 if (nwrite > end - ptr) nwrite = end - ptr;
334 ret = write (new, ptr, nwrite);
335 /* If write gets a page fault, it means we reached
336 a gap between the old text segment and the old data segment.
337 This gap has probably been remapped into part of the text segment.
338 So write zeros for it. */
339 if (ret == -1 && errno == EFAULT)
341 /* Write only a page of zeros at once,
342 so that we don't overshoot the start
343 of the valid memory in the old data segment. */
344 if (nwrite > pagesize)
345 nwrite = pagesize;
346 write (new, zeros, nwrite);
348 i += nwrite;
349 ptr += nwrite;
352 /* ****************************************************************
353 * copy_text_and_data
355 * Copy the text and data segments from memory to the new a.out
357 static int
358 copy_text_and_data (int new, int a_out)
360 register char *end;
361 register char *ptr;
363 #ifdef MSDOS
364 /* Dump the original table of exception handlers, not the one
365 where our exception hooks are registered. */
366 __djgpp_exception_toggle ();
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);
373 /* Zero out the 'atexit' chain in the dumped executable, to avoid
374 calling the atexit functions twice. (emacs.c:main installs an
375 atexit function.) */
376 save_atexit_ptr = __atexit_ptr;
377 __atexit_ptr = NULL;
378 #endif
380 lseek (new, (long) text_scnptr, 0);
381 ptr = (char *) f_ohdr.text_start;
382 end = ptr + f_ohdr.tsize;
383 write_segment (new, ptr, end);
385 lseek (new, (long) data_scnptr, 0);
386 ptr = (char *) f_ohdr.data_start;
387 end = ptr + f_ohdr.dsize;
388 write_segment (new, ptr, end);
390 #ifdef MSDOS
391 /* Restore our exception hooks. */
392 __djgpp_exception_toggle ();
394 /* Restore the startup flags. */
395 _crt0_startup_flags = save_djgpp_startup_flags;
397 /* Restore the atexit chain. */
398 __atexit_ptr = save_atexit_ptr;
399 #endif
402 return 0;
405 /* ****************************************************************
406 * copy_sym
408 * Copy the relocation information and symbol table from the a.out to the new
410 static int
411 copy_sym (int new, int a_out, const char *a_name, const char *new_name)
413 char page[1024];
414 int n;
416 if (a_out < 0)
417 return 0;
419 if (SYMS_START == 0L)
420 return 0;
422 if (lnnoptr) /* if there is line number info */
423 lseek (a_out, coff_offset + lnnoptr, 0); /* start copying from there */
424 else
425 lseek (a_out, coff_offset + SYMS_START, 0); /* Position a.out to symtab. */
427 while ((n = read (a_out, page, sizeof page)) > 0)
429 if (write (new, page, n) != n)
431 PERROR (new_name);
434 if (n < 0)
436 PERROR (a_name);
438 return 0;
443 * If the COFF file contains a symbol table and a line number section,
444 * then any auxiliary entries that have values for x_lnnoptr must
445 * be adjusted by the amount that the line number section has moved
446 * in the file (bias computed in make_hdr). The #@$%&* designers of
447 * the auxiliary entry structures used the absolute file offsets for
448 * the line number entry rather than an offset from the start of the
449 * line number section!
451 * When I figure out how to scan through the symbol table and pick out
452 * the auxiliary entries that need adjustment, this routine will
453 * be fixed. As it is now, all such entries are wrong and sdb
454 * will complain. Fred Fish, UniSoft Systems Inc.
457 /* This function is probably very slow. Instead of reopening the new
458 file for input and output it should copy from the old to the new
459 using the two descriptors already open (WRITEDESC and READDESC).
460 Instead of reading one small structure at a time it should use
461 a reasonable size buffer. But I don't have time to work on such
462 things, so I am installing it as submitted to me. -- RMS. */
465 adjust_lnnoptrs (int writedesc, int readdesc, const char *new_name)
467 register int nsyms;
468 register int new;
469 struct syment symentry;
470 union auxent auxentry;
472 if (!lnnoptr || !f_hdr.f_symptr)
473 return 0;
475 #ifdef MSDOS
476 if ((new = writedesc) < 0)
477 #else
478 if ((new = emacs_open (new_name, O_RDWR, 0)) < 0)
479 #endif
481 PERROR (new_name);
482 return -1;
485 lseek (new, f_hdr.f_symptr, 0);
486 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
488 read (new, &symentry, SYMESZ);
489 if (symentry.n_numaux)
491 read (new, &auxentry, AUXESZ);
492 nsyms++;
493 if (ISFCN (symentry.n_type) || symentry.n_type == 0x2400)
495 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
496 lseek (new, -AUXESZ, 1);
497 write (new, &auxentry, AUXESZ);
501 #ifndef MSDOS
502 emacs_close (new);
503 #endif
504 return 0;
507 /* ****************************************************************
508 * unexec
510 * driving logic.
512 void
513 unexec (const char *new_name, const char *a_name)
515 int new = -1, a_out = -1;
517 if (a_name && (a_out = emacs_open (a_name, O_RDONLY, 0)) < 0)
519 PERROR (a_name);
521 if ((new = emacs_open (new_name, O_WRONLY | O_CREAT | O_TRUNC, 0777)) < 0)
523 PERROR (new_name);
526 if (make_hdr (new, a_out, a_name, new_name) < 0
527 || copy_text_and_data (new, a_out) < 0
528 || copy_sym (new, a_out, a_name, new_name) < 0
529 || adjust_lnnoptrs (new, a_out, new_name) < 0
532 emacs_close (new);
533 return;
536 emacs_close (new);
537 if (a_out >= 0)
538 emacs_close (a_out);
541 #endif /* not CANNOT_DUMP */