428
|
1 /* unexec for GNU Emacs on Windows NT.
|
|
2 Copyright (C) 1994 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 Geoff Voelker (voelker@cs.washington.edu) 8-12-94 */
|
|
22
|
|
23 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
|
|
24
|
|
25 /* The linkers that come with MSVC >= 4.0 merge .bss into .data and reorder
|
|
26 * uninitialised data so that the .data section looks like:
|
|
27 *
|
|
28 * crt0 initialised data
|
|
29 * emacs initialised data
|
|
30 * <my_edata>
|
|
31 * library initialised data
|
|
32 * <start of bss part of .data>
|
|
33 * emacs static uninitialised data
|
|
34 * library static uninitialised data
|
|
35 * emacs global uninitialised data
|
|
36 * <my_ebss>
|
|
37 * library global uninitialised data
|
|
38 *
|
|
39 * This means that we can't use the normal my_ebss in lastfile.c trick to
|
|
40 * differentiate between unitialised data that belongs to emacs and
|
|
41 * uninitialised data that belongs to system libraries. This is bad because
|
|
42 * we do want to initialise the emacs data, but we don't want to initialise
|
|
43 * the system library data.
|
|
44 *
|
|
45 * To solve this problem using MSVC >= 5.0 we use a pragma directive to tell
|
|
46 * the compiler to put emacs's data (both initialised and uninitialised) in
|
|
47 * a separate section in the executable, and we only dump that section. This
|
|
48 * means that all files that define initialized data must include config.h
|
|
49 * to pick up the pragma. We don't try to make any part of that section
|
|
50 * read-only.
|
|
51 *
|
|
52 * This pragma directive isn't supported by the MSVC 4.x compiler. Instead,
|
|
53 * we dump crt0 initialised data and library static uninitialised data in
|
|
54 * addition to the emacs data. This is wrong, but we appear to be able to
|
|
55 * get away with it. A proper fix might involve the introduction of a static
|
|
56 * version of my_ebss in lastfile.c and a new firstfile.c file. jhar */
|
|
57
|
|
58 #include <config.h>
|
442
|
59 #include "lisp.h"
|
|
60
|
|
61 #include "syswindows.h"
|
|
62
|
|
63 #include "nt.h"
|
|
64 #include "ntheap.h"
|
428
|
65
|
|
66 /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */
|
|
67 /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */
|
442
|
68 typedef PIMAGE_NT_HEADERS
|
|
69 (__stdcall * pfnCheckSumMappedFile_t) (LPVOID BaseAddress, DWORD FileLength,
|
|
70 LPDWORD HeaderSum, LPDWORD CheckSum);
|
|
71
|
428
|
72
|
|
73 #if 0
|
|
74 extern BOOL ctrl_c_handler (unsigned long type);
|
|
75 #endif
|
|
76
|
442
|
77 /* Sync with FSF Emacs 19.34.6
|
|
78 note: struct file_data is now defined in nt.h */
|
428
|
79
|
|
80 enum {
|
|
81 HEAP_UNINITIALIZED = 1,
|
|
82 HEAP_UNLOADED,
|
|
83 HEAP_LOADED
|
|
84 };
|
|
85
|
|
86 /* Basically, our "initialized" flag. */
|
|
87 int heap_state = HEAP_UNINITIALIZED;
|
|
88
|
|
89 /* So we can find our heap in the file to recreate it. */
|
|
90 unsigned long heap_index_in_executable = UNINIT_LONG;
|
|
91
|
|
92 void get_section_info (file_data *p_file);
|
|
93 void copy_executable_and_dump_data_section (file_data *, file_data *);
|
|
94 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
|
|
95
|
|
96 /* Cached info about the .data section in the executable. */
|
|
97 PUCHAR data_start_va = UNINIT_PTR;
|
|
98 DWORD data_start_file = UNINIT_LONG;
|
|
99 DWORD data_size = UNINIT_LONG;
|
|
100
|
|
101 /* Cached info about the .bss section in the executable. */
|
|
102 PUCHAR bss_start = UNINIT_PTR;
|
|
103 DWORD bss_size = UNINIT_LONG;
|
|
104
|
|
105 /* Startup code for running on NT. When we are running as the dumped
|
|
106 version, we need to bootstrap our heap and .bss section into our
|
|
107 address space before we can actually hand off control to the startup
|
|
108 code supplied by NT (primarily because that code relies upon malloc ()). */
|
440
|
109
|
|
110 /* **********************
|
|
111 Hackers please remember, this _start() thingy is *not* called neither
|
|
112 when dumping portably, nor when running from temacs! Do not put
|
|
113 significant XEmacs initialization here!
|
|
114 ********************** */
|
|
115
|
428
|
116 void
|
|
117 _start (void)
|
|
118 {
|
|
119 extern void mainCRTStartup (void);
|
|
120
|
|
121 /* Cache system info, e.g., the NT page size. */
|
|
122 cache_system_info ();
|
|
123
|
|
124 /* If we're a dumped version of emacs then we need to recreate
|
|
125 our heap and play tricks with our .bss section. Do this before
|
|
126 start up. (WARNING: Do not put any code before this section
|
|
127 that relies upon malloc () and runs in the dumped version. It
|
|
128 won't work.) */
|
|
129 if (heap_state == HEAP_UNLOADED)
|
|
130 {
|
|
131 char executable_path[MAX_PATH];
|
|
132
|
|
133 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0)
|
|
134 {
|
|
135 exit (1);
|
|
136 }
|
|
137
|
440
|
138 /* #### This is super-bogus. When I rename xemacs.exe,
|
|
139 the renamed file still loads its heap from xemacs.exe --kkm */
|
|
140 #if 0
|
|
141 {
|
|
142 /* To allow profiling, make sure executable_path names the .exe
|
|
143 file, not the file created by the profiler */
|
|
144 char *p = strrchr (executable_path, '\\');
|
|
145 strcpy (p+1, PATH_PROGNAME ".exe");
|
|
146 }
|
|
147 #endif
|
428
|
148
|
|
149 recreate_heap (executable_path);
|
|
150 heap_state = HEAP_LOADED;
|
|
151 }
|
|
152
|
440
|
153 /* #### This is bogus, too. _fmode is set to different values
|
|
154 when we run `xemacs' and `temacs run-emacs'. The sooner we
|
|
155 hit and fix all the weirdities this causes us, the better --kkm */
|
|
156 #if 0
|
428
|
157 /* The default behavior is to treat files as binary and patch up
|
442
|
158 text files appropriately. */
|
428
|
159 _fmode = O_BINARY;
|
440
|
160 #endif
|
428
|
161
|
|
162 #if 0
|
|
163 /* This prevents ctrl-c's in shells running while we're suspended from
|
|
164 having us exit. */
|
|
165 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
|
|
166 #endif
|
|
167
|
|
168 mainCRTStartup ();
|
|
169 }
|
|
170
|
|
171 /* Dump out .data and .bss sections into a new executable. */
|
442
|
172 int
|
|
173 unexec (char *new_name, char *old_name, unsigned int start_data,
|
|
174 unsigned int start_bss, unsigned int entry_address)
|
428
|
175 {
|
|
176 file_data in_file, out_file;
|
|
177 char out_filename[MAX_PATH], in_filename[MAX_PATH];
|
|
178 unsigned long size;
|
|
179 char *ptr;
|
442
|
180 HINSTANCE hImagehelp;
|
428
|
181
|
|
182 /* Make sure that the input and output filenames have the
|
|
183 ".exe" extension...patch them up if they don't. */
|
|
184 strcpy (in_filename, old_name);
|
|
185 ptr = in_filename + strlen (in_filename) - 4;
|
|
186 if (strcmp (ptr, ".exe"))
|
|
187 strcat (in_filename, ".exe");
|
|
188
|
|
189 strcpy (out_filename, new_name);
|
|
190 ptr = out_filename + strlen (out_filename) - 4;
|
|
191 if (strcmp (ptr, ".exe"))
|
|
192 strcat (out_filename, ".exe");
|
|
193
|
|
194 printf ("Dumping from %s\n", in_filename);
|
|
195 printf (" to %s\n", out_filename);
|
|
196
|
|
197 /* We need to round off our heap to NT's allocation unit (64KB). */
|
|
198 round_heap (get_allocation_unit ());
|
|
199
|
|
200 /* Open the undumped executable file. */
|
|
201 if (!open_input_file (&in_file, in_filename))
|
|
202 {
|
|
203 printf ("Failed to open %s (%d)...bailing.\n",
|
|
204 in_filename, GetLastError ());
|
|
205 exit (1);
|
|
206 }
|
|
207
|
|
208 /* Get the interesting section info, like start and size of .bss... */
|
|
209 get_section_info (&in_file);
|
|
210
|
|
211 /* The size of the dumped executable is the size of the original
|
|
212 executable plus the size of the heap and the size of the .bss section. */
|
|
213 heap_index_in_executable = (unsigned long)
|
|
214 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ());
|
|
215 size = heap_index_in_executable + get_committed_heap_size () + bss_size;
|
|
216 if (!open_output_file (&out_file, out_filename, size))
|
|
217 {
|
|
218 printf ("Failed to open %s (%d)...bailing.\n",
|
|
219 out_filename, GetLastError ());
|
|
220 exit (1);
|
|
221 }
|
|
222
|
|
223 /* Set the flag (before dumping). */
|
|
224 heap_state = HEAP_UNLOADED;
|
|
225
|
|
226 copy_executable_and_dump_data_section (&in_file, &out_file);
|
|
227 dump_bss_and_heap (&in_file, &out_file);
|
|
228
|
|
229 /* Patch up header fields; profiler is picky about this. */
|
|
230 hImagehelp = LoadLibrary ("imagehlp.dll");
|
|
231 if (hImagehelp)
|
|
232 {
|
|
233 PIMAGE_DOS_HEADER dos_header;
|
|
234 PIMAGE_NT_HEADERS nt_header;
|
442
|
235
|
428
|
236 DWORD headersum;
|
|
237 DWORD checksum;
|
442
|
238 pfnCheckSumMappedFile_t pfnCheckSumMappedFile;
|
428
|
239
|
|
240 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
|
|
241 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
|
|
242
|
|
243 nt_header->OptionalHeader.CheckSum = 0;
|
442
|
244 #if 0
|
|
245 nt_header->FileHeader.TimeDateStamp = time (NULL);
|
|
246 dos_header->e_cp = size / 512;
|
|
247 nt_header->OptionalHeader.SizeOfImage = size;
|
|
248 #endif
|
428
|
249
|
442
|
250 pfnCheckSumMappedFile =
|
|
251 (pfnCheckSumMappedFile_t) GetProcAddress (hImagehelp,
|
|
252 "CheckSumMappedFile");
|
428
|
253 if (pfnCheckSumMappedFile)
|
|
254 {
|
442
|
255 #if 0
|
|
256 nt_header->FileHeader.TimeDateStamp = time (NULL);
|
|
257 #endif
|
428
|
258 pfnCheckSumMappedFile (out_file.file_base,
|
|
259 out_file.size,
|
|
260 &headersum,
|
|
261 &checksum);
|
|
262 nt_header->OptionalHeader.CheckSum = checksum;
|
|
263 }
|
|
264 FreeLibrary (hImagehelp);
|
|
265 }
|
|
266
|
|
267 close_file_data (&in_file);
|
|
268 close_file_data (&out_file);
|
|
269
|
442
|
270 return 0;
|
428
|
271 }
|
|
272
|
|
273 /* Routines to manipulate NT executable file sections. */
|
|
274
|
|
275 #ifndef DUMP_SEPARATE_SECTION
|
|
276 static void
|
|
277 get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start,
|
|
278 DWORD *p_bss_size)
|
|
279 {
|
|
280 int n, start, len;
|
|
281 char map_filename[MAX_PATH];
|
|
282 char buffer[256];
|
|
283 FILE *map;
|
|
284
|
|
285 /* Overwrite the .exe extension on the executable file name with
|
|
286 the .map extension. */
|
|
287 strcpy (map_filename, p_infile->name);
|
|
288 n = strlen (map_filename) - 3;
|
|
289 strcpy (&map_filename[n], "map");
|
|
290
|
|
291 map = fopen (map_filename, "r");
|
|
292 if (!map)
|
|
293 {
|
|
294 printf ("Failed to open map file %s, error %d...bailing out.\n",
|
|
295 map_filename, GetLastError ());
|
|
296 exit (-1);
|
|
297 }
|
|
298
|
|
299 while (fgets (buffer, sizeof (buffer), map))
|
|
300 {
|
|
301 if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA")))
|
|
302 continue;
|
|
303 n = sscanf (buffer, " %*d:%x %x", &start, &len);
|
|
304 if (n != 2)
|
|
305 {
|
|
306 printf ("Failed to scan the .bss section line:\n%s", buffer);
|
|
307 exit (-1);
|
|
308 }
|
|
309 break;
|
|
310 }
|
|
311 *p_bss_start = (PUCHAR) start;
|
|
312 *p_bss_size = (DWORD) len;
|
|
313 }
|
|
314 #endif
|
|
315
|
|
316 /* Flip through the executable and cache the info necessary for dumping. */
|
|
317 static void
|
|
318 get_section_info (file_data *p_infile)
|
|
319 {
|
|
320 PIMAGE_DOS_HEADER dos_header;
|
|
321 PIMAGE_NT_HEADERS nt_header;
|
|
322 PIMAGE_SECTION_HEADER section, data_section;
|
|
323 unsigned char *ptr;
|
|
324 int i;
|
|
325
|
|
326 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
|
|
327 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
|
328 {
|
|
329 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
|
|
330 exit (1);
|
|
331 }
|
|
332 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
|
|
333 dos_header->e_lfanew);
|
|
334 if (nt_header == NULL)
|
|
335 {
|
|
336 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
|
|
337 p_infile->name);
|
|
338 exit (1);
|
|
339 }
|
|
340
|
|
341 /* Check the NT header signature ... */
|
|
342 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
|
|
343 {
|
|
344 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
|
|
345 nt_header->Signature, p_infile->name);
|
|
346 }
|
|
347
|
|
348 /* Flip through the sections for .data and .bss ... */
|
|
349 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header);
|
|
350 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
|
|
351 {
|
|
352 #ifndef DUMP_SEPARATE_SECTION
|
|
353 if (!strcmp (section->Name, ".bss"))
|
|
354 {
|
|
355 extern int my_ebss; /* From lastfile.c */
|
|
356
|
|
357 ptr = (char *) nt_header->OptionalHeader.ImageBase +
|
|
358 section->VirtualAddress;
|
|
359 bss_start = ptr;
|
|
360 bss_size = (char*)&my_ebss - (char*)bss_start;
|
|
361 }
|
|
362
|
|
363 if (!strcmp (section->Name, ".data"))
|
|
364 #else
|
|
365 if (!strcmp (section->Name, "xdata"))
|
|
366 #endif
|
|
367 {
|
|
368 extern char my_edata[]; /* From lastfile.c */
|
|
369
|
|
370 /* The .data section. */
|
|
371 data_section = section;
|
|
372 ptr = (char *) nt_header->OptionalHeader.ImageBase +
|
|
373 section->VirtualAddress;
|
|
374 data_start_va = ptr;
|
|
375 data_start_file = section->PointerToRawData;
|
|
376
|
|
377 #ifndef DUMP_SEPARATE_SECTION
|
|
378 /* Write only the part of the section that contains emacs data. */
|
|
379 data_size = my_edata - data_start_va;
|
|
380 #else
|
|
381 /* Write back the full section. */
|
|
382 data_size = section->SizeOfRawData;
|
|
383
|
|
384 /* This code doesn't know how to grow the raw size of a section. */
|
|
385 if (section->SizeOfRawData < section->Misc.VirtualSize)
|
|
386 {
|
|
387 printf ("The emacs data section is smaller than expected"
|
|
388 "...bailing.\n");
|
|
389 exit (1);
|
|
390 }
|
|
391 #endif
|
|
392 }
|
|
393 section++;
|
|
394 }
|
|
395
|
|
396 #ifndef DUMP_SEPARATE_SECTION
|
|
397 if (bss_start == UNINIT_PTR)
|
|
398 {
|
|
399 /* Starting with MSVC 4.0, the .bss section has been eliminated
|
|
400 and appended virtually to the end of the .data section. Our
|
|
401 only hint about where the .bss section starts in the address
|
|
402 comes from the SizeOfRawData field in the .data section
|
|
403 header. Unfortunately, this field is only approximate, as it
|
|
404 is a rounded number and is typically rounded just beyond the
|
|
405 start of the .bss section. To find the start and size of the
|
|
406 .bss section exactly, we have to peek into the map file. */
|
|
407 extern int my_ebss;
|
|
408
|
|
409 get_bss_info_from_map_file (p_infile, &ptr, &bss_size);
|
|
410 bss_start = ptr + nt_header->OptionalHeader.ImageBase
|
|
411 + data_section->VirtualAddress;
|
|
412 bss_size = (char*)&my_ebss - (char*)bss_start;
|
|
413 }
|
|
414 #else
|
|
415 bss_size = 0;
|
|
416 #endif
|
|
417 }
|
|
418
|
|
419
|
|
420 /* The dump routines. */
|
|
421
|
|
422 #ifdef DEBUG_XEMACS
|
|
423 #define DUMP_MSG(x) printf x
|
|
424 #else
|
|
425 #define DUMP_MSG(x)
|
|
426 #endif
|
|
427
|
|
428 static void
|
|
429 copy_executable_and_dump_data_section (file_data *p_infile,
|
|
430 file_data *p_outfile)
|
|
431 {
|
|
432 unsigned char *data_file, *data_va;
|
|
433 unsigned long size, index;
|
|
434
|
|
435 /* Get a pointer to where the raw data should go in the executable file. */
|
|
436 data_file = (char *) p_outfile->file_base + data_start_file;
|
|
437
|
|
438 /* Get a pointer to the raw data in our address space. */
|
|
439 data_va = data_start_va;
|
|
440
|
|
441 size = (DWORD) data_file - (DWORD) p_outfile->file_base;
|
|
442 DUMP_MSG (("Copying executable up to data section...\n"));
|
|
443 DUMP_MSG (("\t0x%08x Offset in input file.\n", 0));
|
|
444 DUMP_MSG (("\t0x%08x Offset in output file.\n", 0));
|
|
445 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
|
|
446 memcpy (p_outfile->file_base, p_infile->file_base, size);
|
|
447
|
|
448 size = data_size;
|
|
449 DUMP_MSG (("Dumping data section...\n"));
|
|
450 DUMP_MSG (("\t0x%08x Address in process.\n", data_va));
|
|
451 DUMP_MSG (("\t0x%08x Offset in output file.\n",
|
442
|
452 (char*)data_file - p_outfile->file_base));
|
428
|
453 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
|
|
454 memcpy (data_file, data_va, size);
|
|
455
|
|
456 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
|
|
457 size = p_infile->size - index;
|
|
458 DUMP_MSG (("Copying rest of executable...\n"));
|
|
459 DUMP_MSG (("\t0x%08x Offset in input file.\n", index));
|
|
460 DUMP_MSG (("\t0x%08x Offset in output file.\n", index));
|
|
461 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
|
|
462 memcpy ((char *) p_outfile->file_base + index,
|
|
463 (char *) p_infile->file_base + index, size);
|
|
464 }
|
|
465
|
|
466 static void
|
|
467 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
|
|
468 {
|
|
469 unsigned char *heap_data;
|
|
470 unsigned long size, index;
|
|
471
|
|
472 DUMP_MSG (("Dumping heap onto end of executable...\n"));
|
|
473
|
|
474 index = heap_index_in_executable;
|
|
475 size = get_committed_heap_size ();
|
|
476 heap_data = get_heap_start ();
|
|
477
|
|
478 DUMP_MSG (("\t0x%08x Heap start in process.\n", heap_data));
|
|
479 DUMP_MSG (("\t0x%08x Heap offset in executable.\n", index));
|
|
480 DUMP_MSG (("\t0x%08x Heap size in bytes.\n", size));
|
|
481
|
|
482 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
|
|
483
|
|
484 #ifndef DUMP_SEPARATE_SECTION
|
|
485 DUMP_MSG (("Dumping bss onto end of executable...\n"));
|
|
486
|
|
487 index += size;
|
|
488 size = bss_size;
|
|
489
|
|
490 DUMP_MSG (("\t0x%08x BSS start in process.\n", bss_start));
|
|
491 DUMP_MSG (("\t0x%08x BSS offset in executable.\n", index));
|
|
492 DUMP_MSG (("\t0x%08x BSS size in bytes.\n", size));
|
|
493 memcpy ((char *) p_outfile->file_base + index, bss_start, size);
|
|
494 #endif
|
|
495 }
|
|
496
|
|
497 #undef DUMP_MSG
|
|
498
|
|
499 /* Reload and remap routines. */
|
|
500
|
|
501
|
|
502 /* Load the dumped .bss section into the .bss area of our address space. */
|
|
503 /* Already done if the .bss was part of a separate emacs data section */
|
|
504 void
|
|
505 read_in_bss (char *filename)
|
|
506 {
|
|
507 #ifndef DUMP_SEPARATE_SECTION
|
|
508 HANDLE file;
|
|
509 unsigned long index, n_read;
|
|
510
|
|
511 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
512 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
|
513 if (file == INVALID_HANDLE_VALUE)
|
|
514 abort ();
|
|
515
|
|
516 /* Seek to where the .bss section is tucked away after the heap... */
|
|
517 index = heap_index_in_executable + get_committed_heap_size ();
|
|
518 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF)
|
|
519 abort ();
|
|
520
|
|
521 /* Ok, read in the saved .bss section and initialize all
|
|
522 uninitialized variables. */
|
|
523 if (!ReadFile (file, bss_start, bss_size, &n_read, NULL))
|
|
524 abort ();
|
|
525
|
|
526 CloseHandle (file);
|
|
527 #endif
|
|
528 }
|
|
529
|
|
530 /* Map the heap dumped into the executable file into our address space. */
|
|
531 void
|
|
532 map_in_heap (char *filename)
|
|
533 {
|
|
534 HANDLE file;
|
|
535 HANDLE file_mapping;
|
|
536 void *file_base;
|
|
537 unsigned long size, upper_size, n_read;
|
|
538
|
|
539 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
540 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
|
541 if (file == INVALID_HANDLE_VALUE)
|
|
542 abort ();
|
|
543
|
|
544 size = GetFileSize (file, &upper_size);
|
|
545 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY,
|
|
546 0, size, NULL);
|
|
547 if (!file_mapping)
|
|
548 abort ();
|
|
549
|
|
550 size = get_committed_heap_size ();
|
|
551 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
|
|
552 heap_index_in_executable, size,
|
|
553 get_heap_start ());
|
|
554 if (file_base != 0)
|
|
555 {
|
|
556 return;
|
|
557 }
|
|
558
|
|
559 /* If we don't succeed with the mapping, then copy from the
|
|
560 data into the heap. */
|
|
561
|
|
562 CloseHandle (file_mapping);
|
|
563
|
|
564 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
|
|
565 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL)
|
|
566 abort ();
|
|
567
|
|
568 /* Seek to the location of the heap data in the executable. */
|
|
569 if (SetFilePointer (file, heap_index_in_executable,
|
|
570 NULL, FILE_BEGIN) == 0xFFFFFFFF)
|
|
571 abort ();
|
|
572
|
|
573 /* Read in the data. */
|
|
574 if (!ReadFile (file, get_heap_start (),
|
|
575 get_committed_heap_size (), &n_read, NULL))
|
|
576 abort ();
|
|
577
|
|
578 CloseHandle (file);
|
|
579 }
|