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