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