251
|
1 /* unexec for GNU Emacs on Cygwin32.
|
|
2 Copyright (C) 1994, 1998 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to the Free
|
|
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
19 02111-1307, USA.
|
|
20
|
|
21 */
|
|
22
|
|
23 /* This is a complete rewrite, some code snarfed from unexnt.c and
|
398
|
24 unexec.c, Andy Piper (andy@xemacs.org) 13-1-98 */
|
251
|
25
|
|
26 #include <stdio.h>
|
|
27 #include <stdlib.h>
|
|
28 #include <unistd.h>
|
|
29 #include <fcntl.h>
|
|
30 #include <config.h>
|
|
31 #include <string.h>
|
|
32 #include "sysfile.h"
|
|
33 #define PERROR(arg) perror(arg);exit(-1)
|
|
34
|
|
35 #ifndef HAVE_A_OUT_H
|
|
36 unexec (char *, char *, void *, void *, void *)
|
|
37 {
|
|
38 PERROR("cannot unexec() a.out.h not installed");
|
|
39 }
|
|
40 #else
|
|
41
|
398
|
42 #ifndef MAX_PATH
|
|
43 #define MAX_PATH 260
|
|
44 #endif
|
251
|
45 #include <a.out.h>
|
|
46
|
|
47 #define ALLOC_UNIT 0xFFFF
|
|
48 #define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT))
|
|
49 #define ALIGN_ALLOC(addr) \
|
|
50 ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK)
|
|
51
|
|
52 /* To prevent zero-initialized variables from being placed into the bss
|
|
53 section, use non-zero values to represent an uninitialized state. */
|
|
54 #define UNINIT_PTR ((void *) 0xF0A0F0A0)
|
|
55 #define UNINIT_LONG (0xF0A0F0A0L)
|
|
56
|
|
57 static void get_section_info (int a_out, char* a_name);
|
|
58 static void copy_executable_and_dump_data_section (int a_out, int a_new);
|
|
59 static void dup_file_area(int a_out, int a_new, long size);
|
|
60 #if 0
|
|
61 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval);
|
|
62 #endif
|
|
63
|
|
64 /* Cached info about the .data section in the executable. */
|
|
65 void* data_start_va = UNINIT_PTR;
|
|
66 unsigned long data_size = UNINIT_LONG;
|
|
67
|
|
68 /* Cached info about the .bss section in the executable. */
|
|
69 void* bss_start = UNINIT_PTR;
|
|
70 unsigned long bss_size = UNINIT_LONG;
|
278
|
71 int sections_reversed = 0;
|
251
|
72 FILHDR f_hdr;
|
|
73 PEAOUTHDR f_ohdr;
|
269
|
74 SCNHDR f_data, f_bss, f_text, f_nextdata;
|
251
|
75
|
|
76 #define PERROR(arg) perror(arg);exit(-1)
|
|
77 #define CHECK_AOUT_POS(a) \
|
|
78 if (lseek(a_out, 0, SEEK_CUR) != a) \
|
|
79 { \
|
|
80 printf("we are at %lx, should be at %lx\n", \
|
|
81 lseek(a_out, 0, SEEK_CUR), a); \
|
|
82 exit(-1); \
|
|
83 }
|
|
84
|
410
|
85 void
|
|
86 unexec (char *out_name, char *in_name, void *start_data,
|
|
87 void * d1, void * d2);
|
251
|
88 /* Dump out .data and .bss sections into a new executable. */
|
|
89 void unexec (char *out_name, char *in_name, void *start_data,
|
|
90 void * d1, void * d2)
|
|
91 {
|
|
92 /* ugly nt hack - should be in lisp */
|
|
93 int a_new, a_out = -1;
|
|
94 char new_name[MAX_PATH], a_name[MAX_PATH];
|
|
95 char *ptr;
|
|
96
|
|
97 /* Make sure that the input and output filenames have the
|
|
98 ".exe" extension...patch them up if they don't. */
|
|
99 strcpy (a_name, in_name);
|
|
100 ptr = a_name + strlen (a_name) - 4;
|
|
101 if (strcmp (ptr, ".exe"))
|
|
102 strcat (a_name, ".exe");
|
|
103
|
|
104 strcpy (new_name, out_name);
|
|
105 ptr = new_name + strlen (new_name) - 4;
|
|
106 if (strcmp (ptr, ".exe"))
|
|
107 strcat (new_name, ".exe");
|
|
108
|
|
109 /* We need to round off our heap to NT's allocation unit (64KB). */
|
|
110 /* round_heap (get_allocation_unit ()); */
|
|
111
|
|
112 if (a_name && (a_out = open (a_name, O_RDONLY | OPEN_BINARY)) < 0)
|
|
113 {
|
|
114 PERROR (a_name);
|
|
115 }
|
|
116
|
|
117 if ((a_new = open (new_name, O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY,
|
|
118 CREAT_MODE)) < 0)
|
|
119 {
|
|
120 PERROR (new_name);
|
|
121 }
|
|
122
|
|
123 /* Get the interesting section info, like start and size of .bss... */
|
|
124 get_section_info (a_out, a_name);
|
|
125
|
|
126 copy_executable_and_dump_data_section (a_out, a_new);
|
|
127
|
|
128 close(a_out);
|
|
129 close(a_new);
|
|
130 }
|
|
131
|
|
132 /* Flip through the executable and cache the info necessary for dumping. */
|
|
133 static void get_section_info (int a_out, char* a_name)
|
|
134 {
|
|
135 extern int my_ebss;
|
|
136 /* From lastfile.c */
|
|
137 extern char my_edata[];
|
|
138
|
|
139 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
|
|
140 {
|
|
141 PERROR (a_name);
|
|
142 }
|
|
143
|
|
144 if (f_hdr.e_magic != DOSMAGIC)
|
|
145 {
|
|
146 PERROR("unknown exe header");
|
|
147 }
|
|
148
|
|
149 /* Check the NT header signature ... */
|
|
150 if (f_hdr.nt_signature != NT_SIGNATURE)
|
|
151 {
|
|
152 PERROR("invalid nt header");
|
|
153 }
|
|
154
|
|
155 /* Flip through the sections for .data and .bss ... */
|
|
156 if (f_hdr.f_opthdr > 0)
|
|
157 {
|
|
158 if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ)
|
|
159 {
|
|
160 PERROR (a_name);
|
|
161 }
|
|
162 }
|
278
|
163 /* Loop through .data & .bss section headers, copying them in.
|
|
164 With newer lds these are reversed so we have to cope with both */
|
251
|
165 lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
|
|
166
|
|
167 if (read (a_out, &f_text, sizeof (f_text)) != sizeof (f_text)
|
278
|
168 ||
|
251
|
169 strcmp (f_text.s_name, ".text"))
|
|
170 {
|
|
171 PERROR ("no .text section");
|
|
172 }
|
|
173
|
|
174 /* The .bss section. */
|
|
175 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
|
278
|
176 ||
|
|
177 (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data")))
|
251
|
178 {
|
278
|
179 PERROR ("no .bss / .data section");
|
|
180 }
|
|
181
|
|
182 /* check for reversed .bss and .data */
|
|
183 if (!strcmp(f_bss.s_name, ".data"))
|
|
184 {
|
|
185 printf(".data and .bss reversed\n");
|
|
186 sections_reversed = 1;
|
|
187 memcpy(&f_data, &f_bss, sizeof(f_bss));
|
251
|
188 }
|
|
189
|
278
|
190 /* The .data section. */
|
|
191 if (!sections_reversed)
|
|
192 {
|
|
193 if (read (a_out, &f_data, sizeof (f_data)) != sizeof (f_data)
|
|
194 ||
|
|
195 strcmp (f_data.s_name, ".data"))
|
|
196 {
|
|
197 PERROR ("no .data section");
|
|
198 }
|
|
199 }
|
|
200 else
|
|
201 {
|
|
202 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
|
|
203 ||
|
|
204 strcmp (f_bss.s_name, ".bss"))
|
|
205 {
|
|
206 PERROR ("no .bss section");
|
|
207 }
|
|
208 }
|
|
209
|
251
|
210 bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr);
|
|
211 bss_size = (unsigned long)((char*)&my_ebss-(char*)bss_start);
|
|
212
|
|
213 /* must keep bss data that we want to be blank as blank */
|
|
214 printf("found bss - keeping %lx of %lx bytes\n", bss_size, f_ohdr.bsize);
|
|
215
|
|
216 /* The .data section. */
|
|
217 data_start_va = (void *) ((char*)f_ohdr.ImageBase + f_data.s_vaddr);
|
|
218
|
|
219 /* We want to only write Emacs data back to the executable,
|
|
220 not any of the library data (if library data is included,
|
|
221 then a dumped Emacs won't run on system versions other
|
|
222 than the one Emacs was dumped on). */
|
|
223 data_size = (unsigned long)my_edata - (unsigned long)data_start_va;
|
278
|
224 printf("found data - keeping %lx of %lx bytes\n", data_size, f_ohdr.dsize);
|
251
|
225
|
278
|
226 /* The following data section - often .idata */
|
269
|
227 if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata)
|
251
|
228 &&
|
269
|
229 strcmp (&f_nextdata.s_name[2], "data"))
|
251
|
230 {
|
269
|
231 PERROR ("no other data section");
|
251
|
232 }
|
|
233 }
|
|
234
|
|
235 /* The dump routines. */
|
|
236
|
|
237 static void
|
|
238 copy_executable_and_dump_data_section (int a_out, int a_new)
|
|
239 {
|
|
240 long size=0;
|
278
|
241 unsigned long new_data_size, new_bss_size,
|
|
242 bss_padding, file_sz_change, data_padding=0,
|
|
243 f_data_s_vaddr = f_data.s_vaddr,
|
|
244 f_data_s_scnptr = f_data.s_scnptr,
|
|
245 f_bss_s_vaddr = f_bss.s_vaddr,
|
|
246 f_nextdata_s_scnptr = f_nextdata.s_scnptr;
|
|
247
|
251
|
248 int i;
|
|
249 void* empty_space;
|
|
250 extern int static_heap_dumped;
|
|
251 SCNHDR section;
|
380
|
252 /* calculate new sizes f_ohdr.dsize is the total initialized data
|
251
|
253 size on disk which is f_data.s_size + f_idata.s_size.
|
|
254 f_ohdr.data_start is the base addres of all data and so should
|
|
255 not be changed. *.s_vaddr is the virtual address of the start
|
|
256 of the section normalzed from f_ohdr.ImageBase. *.s_paddr
|
|
257 appears to be the number of bytes in the section actually used
|
|
258 (whereas *.s_size is aligned).
|
|
259
|
|
260 bsize is now 0 since subsumed into .data
|
|
261 dsize is dsize + (f_data.s_vaddr - f_bss.s_vaddr)
|
|
262 f_data.s_vaddr is f_bss.s_vaddr
|
|
263 f_data.s_size is new dsize maybe.
|
|
264 what about s_paddr & s_scnptr? */
|
278
|
265
|
251
|
266 /* this is the amount the file increases in size */
|
278
|
267 if (!sections_reversed)
|
|
268 {
|
|
269 new_bss_size = f_data.s_vaddr - f_bss.s_vaddr;
|
|
270 data_padding = 0;
|
|
271 }
|
|
272 else
|
|
273 {
|
|
274 new_bss_size = f_nextdata.s_vaddr - f_bss.s_vaddr;
|
|
275 data_padding = (f_bss.s_vaddr - f_data.s_vaddr) - f_data.s_size;
|
|
276 }
|
|
277
|
|
278 file_sz_change=new_bss_size + data_padding;
|
|
279 new_data_size=f_ohdr.dsize + file_sz_change;
|
|
280
|
|
281 if (!sections_reversed)
|
|
282 {
|
|
283 f_data.s_vaddr = f_bss.s_vaddr;
|
|
284 }
|
|
285 f_data.s_paddr += file_sz_change;
|
263
|
286 #if 0
|
269
|
287 if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize)
|
251
|
288 {
|
|
289 printf("section size doesn't tally with dsize %lx != %lx\n",
|
269
|
290 f_data.s_size + f_nextdata.s_size, f_ohdr.dsize);
|
251
|
291 }
|
263
|
292 #endif
|
278
|
293 f_data.s_size += file_sz_change;
|
251
|
294 lseek (a_new, 0, SEEK_SET);
|
|
295 /* write file header */
|
|
296 f_hdr.f_symptr += file_sz_change;
|
|
297 f_hdr.f_nscns--;
|
|
298 printf("writing file header\n");
|
|
299 if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr))
|
|
300 {
|
|
301 PERROR("failed to write file header");
|
|
302 }
|
|
303 /* write optional header fixing dsize & bsize*/
|
|
304 printf("writing optional header\n");
|
|
305 printf("new data size is %lx, >= %lx\n", new_data_size,
|
|
306 f_ohdr.dsize + f_ohdr.bsize);
|
|
307 if (new_data_size < f_ohdr.dsize + f_ohdr.bsize )
|
|
308 {
|
|
309 PERROR("new data size is < approx");
|
|
310 }
|
|
311 f_ohdr.dsize=new_data_size;
|
|
312 f_ohdr.bsize=0;
|
|
313 if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr))
|
|
314 {
|
|
315 PERROR("failed to write optional header");
|
|
316 }
|
|
317 /* write text as is */
|
|
318 printf("writing text header (unchanged)\n");
|
|
319
|
|
320 if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text))
|
|
321 {
|
|
322 PERROR("failed to write text header");
|
|
323 }
|
|
324
|
|
325 /* write new data header */
|
|
326 printf("writing .data header\n");
|
|
327
|
|
328 if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data))
|
|
329 {
|
|
330 PERROR("failed to write data header");
|
|
331 }
|
263
|
332
|
269
|
333 printf("writing following data header\n");
|
|
334 f_nextdata.s_scnptr += file_sz_change;
|
|
335 if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change;
|
|
336 if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change;
|
|
337 if (write(a_new, &f_nextdata, sizeof(f_nextdata)) != sizeof(f_nextdata))
|
251
|
338 {
|
269
|
339 PERROR("failed to write nextdata header");
|
251
|
340 }
|
263
|
341
|
251
|
342 /* copy other section headers adjusting the file offset */
|
|
343 for (i=0; i<(f_hdr.f_nscns-3); i++)
|
|
344 {
|
|
345 if (read (a_out, §ion, sizeof (section)) != sizeof (section))
|
|
346 {
|
|
347 PERROR ("no .data section");
|
|
348 }
|
|
349
|
|
350 section.s_scnptr += file_sz_change;
|
|
351 if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change;
|
|
352 if (section.s_relptr != 0) section.s_relptr += file_sz_change;
|
|
353
|
|
354 if (write(a_new, §ion, sizeof(section)) != sizeof(section))
|
|
355 {
|
|
356 PERROR("failed to write data header");
|
|
357 }
|
|
358 }
|
|
359
|
|
360 /* dump bss to maintain offsets */
|
|
361 memset(&f_bss, 0, sizeof(f_bss));
|
|
362 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
|
|
363 {
|
|
364 PERROR("failed to write bss header");
|
|
365 }
|
|
366
|
|
367 size=lseek(a_new, 0, SEEK_CUR);
|
|
368 CHECK_AOUT_POS(size);
|
|
369
|
|
370 /* copy eveything else until start of data */
|
|
371 size = f_data_s_scnptr - lseek (a_out, 0, SEEK_CUR);
|
|
372
|
|
373 printf ("copying executable up to data section ... %lx bytes\n",
|
|
374 size);
|
|
375 dup_file_area(a_out, a_new, size);
|
|
376
|
|
377 CHECK_AOUT_POS(f_data_s_scnptr);
|
|
378
|
278
|
379 if (!sections_reversed)
|
251
|
380 {
|
278
|
381 /* dump bss + padding between sections */
|
|
382 printf ("dumping .bss into executable... %lx bytes\n", bss_size);
|
|
383 if (write(a_new, bss_start, bss_size) != (int)bss_size)
|
|
384 {
|
|
385 PERROR("failed to write bss section");
|
|
386 }
|
|
387
|
|
388 /* pad, needs to be zero */
|
|
389 bss_padding = new_bss_size - bss_size;
|
|
390 printf ("padding .bss ... %lx bytes\n", bss_padding);
|
|
391 empty_space = malloc(bss_padding);
|
|
392 memset(empty_space, 0, bss_padding);
|
|
393 if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
|
|
394 {
|
|
395 PERROR("failed to write bss section");
|
|
396 }
|
|
397 free(empty_space);
|
251
|
398 }
|
|
399
|
|
400 /* tell dumped version not to free pure heap */
|
|
401 static_heap_dumped = 1;
|
|
402 /* Get a pointer to the raw data in our address space. */
|
|
403 printf ("dumping .data section... %lx bytes\n", data_size);
|
|
404 if (write(a_new, data_start_va, data_size) != (int)data_size)
|
|
405 {
|
|
406 PERROR("failed to write data section");
|
|
407 }
|
|
408 /* were going to use free again ... */
|
|
409 static_heap_dumped = 0;
|
|
410
|
|
411 size = lseek(a_out, f_data_s_scnptr + data_size, SEEK_SET);
|
278
|
412
|
|
413 if (!sections_reversed)
|
|
414 {
|
|
415 size = f_nextdata_s_scnptr - size;
|
|
416 dup_file_area(a_out, a_new, size);
|
|
417 }
|
|
418 else
|
|
419 {
|
|
420 /* need to bad to bss with data in file */
|
|
421 printf ("padding .data ... %lx bytes\n", data_padding);
|
|
422 size = (f_bss_s_vaddr - f_data_s_vaddr) - data_size;
|
|
423 dup_file_area(a_out, a_new, size);
|
251
|
424
|
278
|
425 /* dump bss + padding between sections */
|
|
426 printf ("dumping .bss into executable... %lx bytes\n", bss_size);
|
|
427 if (write(a_new, bss_start, bss_size) != (int)bss_size)
|
|
428 {
|
|
429 PERROR("failed to write bss section");
|
|
430 }
|
|
431
|
|
432 /* pad, needs to be zero */
|
|
433 bss_padding = new_bss_size - bss_size;
|
|
434 printf ("padding .bss ... %lx bytes\n", bss_padding);
|
|
435 empty_space = malloc(bss_padding);
|
|
436 memset(empty_space, 0, bss_padding);
|
|
437 if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
|
|
438 {
|
|
439 PERROR("failed to write bss section");
|
|
440 }
|
|
441 free(empty_space);
|
|
442 if (lseek(a_new, 0, SEEK_CUR) != f_nextdata.s_scnptr)
|
|
443 {
|
|
444 printf("at %lx should be at %lx\n",
|
|
445 lseek(a_new, 0, SEEK_CUR),
|
|
446 f_nextdata.s_scnptr);
|
|
447 PERROR("file positioning error\n");
|
|
448 }
|
|
449 lseek(a_out, f_nextdata_s_scnptr, SEEK_SET);
|
|
450 }
|
|
451
|
|
452 CHECK_AOUT_POS(f_nextdata_s_scnptr);
|
|
453
|
269
|
454 /* now dump - nextdata don't need to do this cygwin ds is in .data! */
|
|
455 printf ("dumping following data section... %lx bytes\n", f_nextdata.s_size);
|
251
|
456
|
269
|
457 dup_file_area(a_out,a_new,f_nextdata.s_size);
|
251
|
458
|
|
459 /* write rest of file */
|
|
460 printf ("writing rest of file\n");
|
|
461 size = lseek(a_out, 0, SEEK_END);
|
278
|
462 size = size - (f_nextdata_s_scnptr + f_nextdata.s_size); /* length remaining in a_out */
|
|
463 lseek(a_out, f_nextdata_s_scnptr + f_nextdata.s_size, SEEK_SET);
|
251
|
464
|
|
465 dup_file_area(a_out, a_new, size);
|
|
466 }
|
|
467
|
|
468 /*
|
|
469 * copy from aout to anew
|
|
470 */
|
|
471 static void dup_file_area(int a_out, int a_new, long size)
|
|
472 {
|
|
473 char page[BUFSIZ];
|
|
474 long n;
|
|
475 for (; size > 0; size -= sizeof (page))
|
|
476 {
|
|
477 n = size > sizeof (page) ? sizeof (page) : size;
|
|
478 if (read (a_out, page, n) != n || write (a_new, page, n) != n)
|
|
479 {
|
|
480 PERROR ("dump_out()");
|
|
481 }
|
|
482 }
|
|
483 }
|
|
484
|
|
485 #if 0
|
|
486 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval)
|
|
487 {
|
|
488 int cpos;
|
|
489
|
|
490 cpos = lseek(a_new, 0, SEEK_CUR);
|
|
491 if (va < bss_start || va > bss_start + f_data.s_size)
|
|
492 {
|
|
493 PERROR("address not in data space\n");
|
|
494 }
|
|
495 lseek(a_new, f_data.s_scnptr + ((unsigned long)va -
|
|
496 (unsigned long)bss_start), SEEK_SET);
|
|
497 if (write(a_new, newval, sizeof(int)) != (int)sizeof(int))
|
|
498 {
|
|
499 PERROR("failed to write int value");
|
|
500 }
|
|
501 lseek(a_new, cpos, SEEK_SET);
|
|
502 }
|
|
503 #endif
|
|
504
|
|
505 #endif /* HAVE_A_OUT_H */
|