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