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