100
|
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 #include <stdlib.h> /* _fmode */
|
|
26 #include <stdio.h>
|
|
27 #include <fcntl.h>
|
|
28 #include <windows.h>
|
|
29
|
|
30 #if 0
|
|
31 extern BOOL ctrl_c_handler (unsigned long type);
|
|
32 #endif
|
|
33
|
|
34 #include "ntheap.h"
|
|
35
|
|
36 /* A convenient type for keeping all the info about a mapped file together. */
|
|
37 typedef struct file_data {
|
|
38 char *name;
|
|
39 unsigned long size;
|
|
40 HANDLE file;
|
|
41 HANDLE file_mapping;
|
|
42 unsigned char *file_base;
|
|
43 } file_data;
|
|
44
|
169
|
45 enum {
|
|
46 HEAP_UNINITIALIZED = 1,
|
|
47 HEAP_UNLOADED,
|
|
48 HEAP_LOADED
|
|
49 };
|
|
50
|
100
|
51 /* Basically, our "initialized" flag. */
|
169
|
52 int heap_state = HEAP_UNINITIALIZED;
|
100
|
53
|
|
54 /* So we can find our heap in the file to recreate it. */
|
169
|
55 unsigned long heap_index_in_executable = UNINIT_LONG;
|
100
|
56
|
|
57 void open_input_file (file_data *p_file, char *name);
|
|
58 void open_output_file (file_data *p_file, char *name, unsigned long size);
|
|
59 void close_file_data (file_data *p_file);
|
|
60
|
|
61 void get_section_info (file_data *p_file);
|
|
62 void copy_executable_and_dump_data_section (file_data *, file_data *);
|
|
63 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
|
|
64
|
|
65 /* Cached info about the .data section in the executable. */
|
169
|
66 PUCHAR data_start_va = UNINIT_PTR;
|
|
67 DWORD data_start_file = UNINIT_LONG;
|
|
68 DWORD data_size = UNINIT_LONG;
|
100
|
69
|
|
70 /* Cached info about the .bss section in the executable. */
|
169
|
71 PUCHAR bss_start = UNINIT_PTR;
|
|
72 DWORD bss_size = UNINIT_LONG;
|
100
|
73
|
|
74 #ifdef HAVE_NTGUI
|
|
75 HINSTANCE hinst = NULL;
|
|
76 HINSTANCE hprevinst = NULL;
|
|
77 LPSTR lpCmdLine = "";
|
|
78 int nCmdShow = 0;
|
|
79 #endif /* HAVE_NTGUI */
|
|
80
|
|
81 /* Startup code for running on NT. When we are running as the dumped
|
|
82 version, we need to bootstrap our heap and .bss section into our
|
|
83 address space before we can actually hand off control to the startup
|
|
84 code supplied by NT (primarily because that code relies upon malloc ()). */
|
|
85 void
|
|
86 _start (void)
|
|
87 {
|
|
88 extern void mainCRTStartup (void);
|
|
89
|
|
90 /* Cache system info, e.g., the NT page size. */
|
|
91 cache_system_info ();
|
|
92
|
|
93 /* If we're a dumped version of emacs then we need to recreate
|
|
94 our heap and play tricks with our .bss section. Do this before
|
|
95 start up. (WARNING: Do not put any code before this section
|
|
96 that relies upon malloc () and runs in the dumped version. It
|
|
97 won't work.) */
|
169
|
98 if (heap_state == HEAP_UNLOADED)
|
100
|
99 {
|
|
100 char executable_path[MAX_PATH];
|
|
101
|
|
102 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0)
|
|
103 {
|
|
104 exit (1);
|
|
105 }
|
|
106 recreate_heap (executable_path);
|
169
|
107 heap_state = HEAP_LOADED;
|
100
|
108 }
|
|
109
|
|
110 /* The default behavior is to treat files as binary and patch up
|
|
111 text files appropriately, in accordance with the MSDOS code. */
|
|
112 _fmode = O_BINARY;
|
|
113
|
|
114 #if 0
|
|
115 /* This prevents ctrl-c's in shells running while we're suspended from
|
|
116 having us exit. */
|
|
117 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
|
|
118 #endif
|
|
119
|
|
120 /* Invoke the NT CRT startup routine now that our housecleaning
|
|
121 is finished. */
|
|
122 #ifdef HAVE_NTGUI
|
|
123 /* determine WinMain args like crt0.c does */
|
|
124 hinst = GetModuleHandle(NULL);
|
|
125 lpCmdLine = GetCommandLine();
|
|
126 nCmdShow = SW_SHOWDEFAULT;
|
|
127 #endif
|
|
128 mainCRTStartup ();
|
|
129 }
|
|
130
|
|
131 /* Dump out .data and .bss sections into a new executable. */
|
|
132 void
|
|
133 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
|
|
134 void *entry_address)
|
|
135 {
|
|
136 file_data in_file, out_file;
|
|
137 char out_filename[MAX_PATH], in_filename[MAX_PATH];
|
|
138 unsigned long size;
|
|
139 char *ptr;
|
|
140
|
|
141 /* Make sure that the input and output filenames have the
|
|
142 ".exe" extension...patch them up if they don't. */
|
|
143 strcpy (in_filename, old_name);
|
|
144 ptr = in_filename + strlen (in_filename) - 4;
|
|
145 if (strcmp (ptr, ".exe"))
|
|
146 strcat (in_filename, ".exe");
|
|
147
|
|
148 strcpy (out_filename, new_name);
|
|
149 ptr = out_filename + strlen (out_filename) - 4;
|
|
150 if (strcmp (ptr, ".exe"))
|
|
151 strcat (out_filename, ".exe");
|
|
152
|
|
153 printf ("Dumping from %s\n", in_filename);
|
|
154 printf (" to %s\n", out_filename);
|
|
155
|
|
156 /* We need to round off our heap to NT's allocation unit (64KB). */
|
|
157 round_heap (get_allocation_unit ());
|
|
158
|
|
159 /* Open the undumped executable file. */
|
|
160 open_input_file (&in_file, in_filename);
|
|
161
|
|
162 /* Get the interesting section info, like start and size of .bss... */
|
|
163 get_section_info (&in_file);
|
|
164
|
|
165 /* The size of the dumped executable is the size of the original
|
|
166 executable plus the size of the heap and the size of the .bss section. */
|
|
167 heap_index_in_executable = (unsigned long)
|
|
168 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ());
|
|
169 size = heap_index_in_executable + get_committed_heap_size () + bss_size;
|
|
170 open_output_file (&out_file, out_filename, size);
|
|
171
|
|
172 /* Set the flag (before dumping). */
|
169
|
173 heap_state = HEAP_UNLOADED;
|
100
|
174
|
|
175 copy_executable_and_dump_data_section (&in_file, &out_file);
|
|
176 dump_bss_and_heap (&in_file, &out_file);
|
|
177
|
|
178 close_file_data (&in_file);
|
|
179 close_file_data (&out_file);
|
|
180 }
|
|
181
|
|
182
|
|
183 /* File handling. */
|
|
184
|
|
185
|
|
186 void
|
|
187 open_input_file (file_data *p_file, char *filename)
|
|
188 {
|
|
189 HANDLE file;
|
|
190 HANDLE file_mapping;
|
|
191 void *file_base;
|
|
192 unsigned long size, upper_size;
|
|
193
|
|
194 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
195 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
|
196 if (file == INVALID_HANDLE_VALUE)
|
|
197 {
|
|
198 printf ("Failed to open %s (%d)...bailing.\n",
|
|
199 filename, GetLastError ());
|
|
200 exit (1);
|
|
201 }
|
|
202
|
|
203 size = GetFileSize (file, &upper_size);
|
|
204 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
|
|
205 0, size, NULL);
|
|
206 if (!file_mapping)
|
|
207 {
|
|
208 printf ("Failed to create file mapping of %s (%d)...bailing.\n",
|
|
209 filename, GetLastError ());
|
|
210 exit (1);
|
|
211 }
|
|
212
|
|
213 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
|
|
214 if (file_base == 0)
|
|
215 {
|
|
216 printf ("Failed to map view of file of %s (%d)...bailing.\n",
|
|
217 filename, GetLastError ());
|
|
218 exit (1);
|
|
219 }
|
|
220
|
|
221 p_file->name = filename;
|
|
222 p_file->size = size;
|
|
223 p_file->file = file;
|
|
224 p_file->file_mapping = file_mapping;
|
|
225 p_file->file_base = file_base;
|
|
226 }
|
|
227
|
|
228 void
|
|
229 open_output_file (file_data *p_file, char *filename, unsigned long size)
|
|
230 {
|
|
231 HANDLE file;
|
|
232 HANDLE file_mapping;
|
|
233 void *file_base;
|
|
234 int i;
|
|
235
|
|
236 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
|
|
237 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
|
238 if (file == INVALID_HANDLE_VALUE)
|
|
239 {
|
|
240 i = GetLastError ();
|
|
241 printf ("open_output_file: Failed to open %s (%d).\n",
|
|
242 filename, i);
|
|
243 exit (1);
|
|
244 }
|
|
245
|
|
246 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
|
|
247 0, size, NULL);
|
|
248 if (!file_mapping)
|
|
249 {
|
|
250 i = GetLastError ();
|
|
251 printf ("open_output_file: Failed to create file mapping of %s (%d).\n",
|
|
252 filename, i);
|
|
253 exit (1);
|
|
254 }
|
|
255
|
|
256 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
|
|
257 if (file_base == 0)
|
|
258 {
|
|
259 i = GetLastError ();
|
|
260 printf ("open_output_file: Failed to map view of file of %s (%d).\n",
|
|
261 filename, i);
|
|
262 exit (1);
|
|
263 }
|
|
264
|
|
265 p_file->name = filename;
|
|
266 p_file->size = size;
|
|
267 p_file->file = file;
|
|
268 p_file->file_mapping = file_mapping;
|
|
269 p_file->file_base = file_base;
|
|
270 }
|
|
271
|
|
272 /* Close the system structures associated with the given file. */
|
|
273 static void
|
|
274 close_file_data (file_data *p_file)
|
|
275 {
|
|
276 UnmapViewOfFile (p_file->file_base);
|
|
277 CloseHandle (p_file->file_mapping);
|
|
278 CloseHandle (p_file->file);
|
|
279 }
|
|
280
|
|
281
|
|
282 /* Routines to manipulate NT executable file sections. */
|
|
283
|
|
284 static void
|
|
285 get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start,
|
|
286 DWORD *p_bss_size)
|
|
287 {
|
|
288 int n, start, len;
|
|
289 char map_filename[MAX_PATH];
|
|
290 char buffer[256];
|
|
291 FILE *map;
|
|
292
|
|
293 /* Overwrite the .exe extension on the executable file name with
|
|
294 the .map extension. */
|
|
295 strcpy (map_filename, p_infile->name);
|
|
296 n = strlen (map_filename) - 3;
|
|
297 strcpy (&map_filename[n], "map");
|
|
298
|
|
299 map = fopen (map_filename, "r");
|
|
300 if (!map)
|
|
301 {
|
|
302 printf ("Failed to open map file %s, error %d...bailing out.\n",
|
|
303 map_filename, GetLastError ());
|
|
304 exit (-1);
|
|
305 }
|
|
306
|
|
307 while (fgets (buffer, sizeof (buffer), map))
|
|
308 {
|
|
309 if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA")))
|
|
310 continue;
|
|
311 n = sscanf (buffer, " %*d:%x %x", &start, &len);
|
|
312 if (n != 2)
|
|
313 {
|
|
314 printf ("Failed to scan the .bss section line:\n%s", buffer);
|
|
315 exit (-1);
|
|
316 }
|
|
317 break;
|
|
318 }
|
|
319 *p_bss_start = (PUCHAR) start;
|
|
320 *p_bss_size = (DWORD) len;
|
|
321 }
|
|
322
|
|
323 static unsigned long
|
|
324 get_section_size (PIMAGE_SECTION_HEADER p_section)
|
|
325 {
|
|
326 /* The section size is in different locations in the different versions. */
|
|
327 switch (get_nt_minor_version ())
|
|
328 {
|
|
329 case 10:
|
|
330 return p_section->SizeOfRawData;
|
|
331 default:
|
|
332 return p_section->Misc.VirtualSize;
|
|
333 }
|
|
334 }
|
|
335
|
|
336 /* Flip through the executable and cache the info necessary for dumping. */
|
|
337 static void
|
|
338 get_section_info (file_data *p_infile)
|
|
339 {
|
|
340 PIMAGE_DOS_HEADER dos_header;
|
|
341 PIMAGE_NT_HEADERS nt_header;
|
|
342 PIMAGE_SECTION_HEADER section, data_section;
|
|
343 unsigned char *ptr;
|
|
344 int i;
|
|
345
|
|
346 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
|
|
347 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
|
348 {
|
|
349 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
|
|
350 exit (1);
|
|
351 }
|
|
352 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
|
|
353 dos_header->e_lfanew);
|
|
354 if (nt_header == NULL)
|
|
355 {
|
|
356 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
|
|
357 p_infile->name);
|
|
358 exit (1);
|
|
359 }
|
|
360
|
|
361 /* Check the NT header signature ... */
|
|
362 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
|
|
363 {
|
|
364 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
|
|
365 nt_header->Signature, p_infile->name);
|
|
366 }
|
|
367
|
|
368 /* Flip through the sections for .data and .bss ... */
|
|
369 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header);
|
|
370 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
|
|
371 {
|
|
372 if (!strcmp (section->Name, ".bss"))
|
|
373 {
|
|
374 /* The .bss section. */
|
|
375 ptr = (char *) nt_header->OptionalHeader.ImageBase +
|
|
376 section->VirtualAddress;
|
|
377 bss_start = ptr;
|
|
378 bss_size = get_section_size (section);
|
|
379 }
|
|
380 if (!strcmp (section->Name, ".data"))
|
|
381 {
|
|
382 /* From lastfile.c */
|
|
383 extern char my_edata[];
|
|
384
|
|
385 /* The .data section. */
|
|
386 data_section = section;
|
|
387 ptr = (char *) nt_header->OptionalHeader.ImageBase +
|
|
388 section->VirtualAddress;
|
|
389 data_start_va = ptr;
|
|
390 data_start_file = section->PointerToRawData;
|
|
391
|
|
392 /* We want to only write Emacs data back to the executable,
|
|
393 not any of the library data (if library data is included,
|
|
394 then a dumped Emacs won't run on system versions other
|
|
395 than the one Emacs was dumped on). */
|
|
396 data_size = my_edata - data_start_va;
|
|
397 }
|
|
398 section++;
|
|
399 }
|
|
400
|
169
|
401 if (bss_start == UNINIT_PTR && bss_size == UNINIT_LONG)
|
100
|
402 {
|
|
403 /* Starting with MSVC 4.0, the .bss section has been eliminated
|
|
404 and appended virtually to the end of the .data section. Our
|
|
405 only hint about where the .bss section starts in the address
|
|
406 comes from the SizeOfRawData field in the .data section
|
|
407 header. Unfortunately, this field is only approximate, as it
|
|
408 is a rounded number and is typically rounded just beyond the
|
|
409 start of the .bss section. To find the start and size of the
|
|
410 .bss section exactly, we have to peek into the map file. */
|
|
411 get_bss_info_from_map_file (p_infile, &ptr, &bss_size);
|
|
412 bss_start = ptr + nt_header->OptionalHeader.ImageBase
|
|
413 + data_section->VirtualAddress;
|
|
414 }
|
|
415 }
|
|
416
|
|
417
|
|
418 /* The dump routines. */
|
|
419
|
|
420 static void
|
|
421 copy_executable_and_dump_data_section (file_data *p_infile,
|
|
422 file_data *p_outfile)
|
|
423 {
|
|
424 unsigned char *data_file, *data_va;
|
|
425 unsigned long size, index;
|
|
426
|
|
427 /* Get a pointer to where the raw data should go in the executable file. */
|
|
428 data_file = (char *) p_outfile->file_base + data_start_file;
|
|
429
|
|
430 /* Get a pointer to the raw data in our address space. */
|
|
431 data_va = data_start_va;
|
|
432
|
|
433 size = (DWORD) data_file - (DWORD) p_outfile->file_base;
|
|
434 printf ("Copying executable up to data section...\n");
|
|
435 printf ("\t0x%08x Offset in input file.\n", 0);
|
|
436 printf ("\t0x%08x Offset in output file.\n", 0);
|
|
437 printf ("\t0x%08x Size in bytes.\n", size);
|
|
438 memcpy (p_outfile->file_base, p_infile->file_base, size);
|
|
439
|
|
440 size = data_size;
|
|
441 printf ("Dumping .data section...\n");
|
|
442 printf ("\t0x%08x Address in process.\n", data_va);
|
|
443 printf ("\t0x%08x Offset in output file.\n",
|
|
444 data_file - p_outfile->file_base);
|
|
445 printf ("\t0x%08x Size in bytes.\n", size);
|
|
446 memcpy (data_file, data_va, size);
|
|
447
|
|
448 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
|
|
449 size = p_infile->size - index;
|
|
450 printf ("Copying rest of executable...\n");
|
|
451 printf ("\t0x%08x Offset in input file.\n", index);
|
|
452 printf ("\t0x%08x Offset in output file.\n", index);
|
|
453 printf ("\t0x%08x Size in bytes.\n", size);
|
|
454 memcpy ((char *) p_outfile->file_base + index,
|
|
455 (char *) p_infile->file_base + index, size);
|
|
456 }
|
|
457
|
|
458 static void
|
|
459 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
|
|
460 {
|
|
461 unsigned char *heap_data, *bss_data;
|
|
462 unsigned long size, index;
|
|
463
|
|
464 printf ("Dumping heap into executable...\n");
|
|
465
|
|
466 index = heap_index_in_executable;
|
|
467 size = get_committed_heap_size ();
|
|
468 heap_data = get_heap_start ();
|
|
469
|
|
470 printf ("\t0x%08x Heap start in process.\n", heap_data);
|
|
471 printf ("\t0x%08x Heap offset in executable.\n", index);
|
|
472 printf ("\t0x%08x Heap size in bytes.\n", size);
|
|
473
|
|
474 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
|
|
475
|
|
476 printf ("Dumping .bss into executable...\n");
|
|
477
|
|
478 index += size;
|
|
479 size = bss_size;
|
|
480 bss_data = bss_start;
|
|
481
|
|
482 printf ("\t0x%08x BSS start in process.\n", bss_data);
|
|
483 printf ("\t0x%08x BSS offset in executable.\n", index);
|
|
484 printf ("\t0x%08x BSS size in bytes.\n", size);
|
|
485 memcpy ((char *) p_outfile->file_base + index, bss_data, size);
|
|
486 }
|
|
487
|
|
488
|
|
489 /* Reload and remap routines. */
|
|
490
|
|
491
|
|
492 /* Load the dumped .bss section into the .bss area of our address space. */
|
|
493 void
|
|
494 read_in_bss (char *filename)
|
|
495 {
|
|
496 HANDLE file;
|
|
497 unsigned long size, index, n_read, total_read;
|
|
498 char buffer[512], *bss;
|
|
499 int i;
|
|
500
|
|
501 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
502 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
|
503 if (file == INVALID_HANDLE_VALUE)
|
|
504 {
|
|
505 i = GetLastError ();
|
|
506 exit (1);
|
|
507 }
|
|
508
|
|
509 /* Seek to where the .bss section is tucked away after the heap... */
|
|
510 index = heap_index_in_executable + get_committed_heap_size ();
|
|
511 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF)
|
|
512 {
|
|
513 i = GetLastError ();
|
|
514 exit (1);
|
|
515 }
|
|
516
|
|
517
|
|
518 /* Ok, read in the saved .bss section and initialize all
|
|
519 uninitialized variables. */
|
|
520 if (!ReadFile (file, bss_start, bss_size, &n_read, NULL))
|
|
521 {
|
|
522 i = GetLastError ();
|
|
523 exit (1);
|
|
524 }
|
|
525
|
|
526 CloseHandle (file);
|
|
527 }
|
|
528
|
|
529 /* Map the heap dumped into the executable file into our address space. */
|
|
530 void
|
|
531 map_in_heap (char *filename)
|
|
532 {
|
|
533 HANDLE file;
|
|
534 HANDLE file_mapping;
|
|
535 void *file_base;
|
|
536 unsigned long size, upper_size, n_read;
|
|
537 int i;
|
|
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 {
|
|
543 i = GetLastError ();
|
|
544 exit (1);
|
|
545 }
|
|
546
|
|
547 size = GetFileSize (file, &upper_size);
|
|
548 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY,
|
|
549 0, size, NULL);
|
|
550 if (!file_mapping)
|
|
551 {
|
|
552 i = GetLastError ();
|
|
553 exit (1);
|
|
554 }
|
|
555
|
|
556 size = get_committed_heap_size ();
|
|
557 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
|
|
558 heap_index_in_executable, size,
|
|
559 get_heap_start ());
|
|
560 if (file_base != 0)
|
|
561 {
|
|
562 return;
|
|
563 }
|
|
564
|
|
565 /* If we don't succeed with the mapping, then copy from the
|
|
566 data into the heap. */
|
|
567
|
|
568 CloseHandle (file_mapping);
|
|
569
|
|
570 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
|
|
571 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL)
|
|
572 {
|
|
573 i = GetLastError ();
|
|
574 exit (1);
|
|
575 }
|
|
576
|
|
577 /* Seek to the location of the heap data in the executable. */
|
|
578 i = heap_index_in_executable;
|
|
579 if (SetFilePointer (file, i, NULL, FILE_BEGIN) == 0xFFFFFFFF)
|
|
580 {
|
|
581 i = GetLastError ();
|
|
582 exit (1);
|
|
583 }
|
|
584
|
|
585 /* Read in the data. */
|
|
586 if (!ReadFile (file, get_heap_start (),
|
|
587 get_committed_heap_size (), &n_read, NULL))
|
|
588 {
|
|
589 i = GetLastError ();
|
|
590 exit (1);
|
|
591 }
|
|
592
|
|
593 CloseHandle (file);
|
|
594 }
|