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