comparison src/unexcw.c @ 251:677f6a0ee643 r20-5b24

Import from CVS: tag r20-5b24
author cvs
date Mon, 13 Aug 2007 10:19:59 +0200
parents
children 727739f917cb
comparison
equal deleted inserted replaced
250:f385a461c9aa 251:677f6a0ee643
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 &&
202 strcmp (f_idata.s_name, ".idata"))
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;
241
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 }
247 f_data.s_size += new_bss_size;
248 lseek (a_new, 0, SEEK_SET);
249 /* write file header */
250 f_hdr.f_symptr += file_sz_change;
251 f_hdr.f_nscns--;
252 printf("writing file header\n");
253 if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr))
254 {
255 PERROR("failed to write file header");
256 }
257 /* write optional header fixing dsize & bsize*/
258 printf("writing optional header\n");
259 printf("new data size is %lx, >= %lx\n", new_data_size,
260 f_ohdr.dsize + f_ohdr.bsize);
261 if (new_data_size < f_ohdr.dsize + f_ohdr.bsize )
262 {
263 PERROR("new data size is < approx");
264 }
265 f_ohdr.dsize=new_data_size;
266 f_ohdr.bsize=0;
267 if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr))
268 {
269 PERROR("failed to write optional header");
270 }
271 /* write text as is */
272 printf("writing text header (unchanged)\n");
273
274 if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text))
275 {
276 PERROR("failed to write text header");
277 }
278
279 /* write new data header */
280 printf("writing .data header\n");
281
282 if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data))
283 {
284 PERROR("failed to write data header");
285 }
286
287 printf("writing .idata header\n");
288 f_idata.s_scnptr += file_sz_change;
289 if (f_idata.s_lnnoptr != 0) f_idata.s_lnnoptr += file_sz_change;
290 if (f_idata.s_relptr != 0) f_idata.s_relptr += file_sz_change;
291 if (write(a_new, &f_idata, sizeof(f_idata)) != sizeof(f_idata))
292 {
293 PERROR("failed to write idata header");
294 }
295
296 /* copy other section headers adjusting the file offset */
297 for (i=0; i<(f_hdr.f_nscns-3); i++)
298 {
299 if (read (a_out, &section, sizeof (section)) != sizeof (section))
300 {
301 PERROR ("no .data section");
302 }
303
304 section.s_scnptr += file_sz_change;
305 if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change;
306 if (section.s_relptr != 0) section.s_relptr += file_sz_change;
307
308 if (write(a_new, &section, sizeof(section)) != sizeof(section))
309 {
310 PERROR("failed to write data header");
311 }
312 }
313
314 /* dump bss to maintain offsets */
315 memset(&f_bss, 0, sizeof(f_bss));
316 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
317 {
318 PERROR("failed to write bss header");
319 }
320
321 size=lseek(a_new, 0, SEEK_CUR);
322 CHECK_AOUT_POS(size);
323
324 /* copy eveything else until start of data */
325 size = f_data_s_scnptr - lseek (a_out, 0, SEEK_CUR);
326
327 printf ("copying executable up to data section ... %lx bytes\n",
328 size);
329 dup_file_area(a_out, a_new, size);
330
331 CHECK_AOUT_POS(f_data_s_scnptr);
332
333 /* dump bss + padding between sections */
334 printf ("dumping .bss into executable... %lx bytes\n", bss_size);
335 if (write(a_new, bss_start, bss_size) != (int)bss_size)
336 {
337 PERROR("failed to write bss section");
338 }
339
340 /* pad, needs to be zero */
341 bss_padding = new_bss_size - bss_size;
342 printf ("padding .bss ... %lx bytes\n", bss_padding);
343 empty_space = malloc(bss_padding);
344 memset(empty_space, 0, bss_padding);
345 if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
346 {
347 PERROR("failed to write bss section");
348 }
349 free(empty_space);
350
351 /* tell dumped version not to free pure heap */
352 static_heap_dumped = 1;
353 /* Get a pointer to the raw data in our address space. */
354 printf ("dumping .data section... %lx bytes\n", data_size);
355 if (write(a_new, data_start_va, data_size) != (int)data_size)
356 {
357 PERROR("failed to write data section");
358 }
359 /* were going to use free again ... */
360 static_heap_dumped = 0;
361
362 size = lseek(a_out, f_data_s_scnptr + data_size, SEEK_SET);
363 size = f_idata.s_scnptr - size;
364 dup_file_area(a_out, a_new, size);
365
366 // lseek(a_out, f_idata.s_scnptr, SEEK_CUR);
367 CHECK_AOUT_POS(f_idata.s_scnptr);
368 /* now dump - idata don't need to do this cygwin ds is in .data! */
369 printf ("dumping .idata section... %lx bytes\n", f_idata.s_size);
370
371 dup_file_area(a_out,a_new,f_idata.s_size);
372
373 /* write rest of file */
374 printf ("writing rest of file\n");
375 size = lseek(a_out, 0, SEEK_END);
376 size = size - (f_idata.s_scnptr + f_idata.s_size); /* length remaining in a_out */
377 lseek(a_out, f_idata.s_scnptr + f_idata.s_size, SEEK_SET);
378
379 dup_file_area(a_out, a_new, size);
380 }
381
382 /*
383 * copy from aout to anew
384 */
385 static void dup_file_area(int a_out, int a_new, long size)
386 {
387 char page[BUFSIZ];
388 long n;
389 for (; size > 0; size -= sizeof (page))
390 {
391 n = size > sizeof (page) ? sizeof (page) : size;
392 if (read (a_out, page, n) != n || write (a_new, page, n) != n)
393 {
394 PERROR ("dump_out()");
395 }
396 }
397 }
398
399 #if 0
400 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval)
401 {
402 int cpos;
403
404 cpos = lseek(a_new, 0, SEEK_CUR);
405 if (va < bss_start || va > bss_start + f_data.s_size)
406 {
407 PERROR("address not in data space\n");
408 }
409 lseek(a_new, f_data.s_scnptr + ((unsigned long)va -
410 (unsigned long)bss_start), SEEK_SET);
411 if (write(a_new, newval, sizeof(int)) != (int)sizeof(int))
412 {
413 PERROR("failed to write int value");
414 }
415 lseek(a_new, cpos, SEEK_SET);
416 }
417 #endif
418
419 #endif /* HAVE_A_OUT_H */