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