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 | ||
43640c4d GV |
24 | #include <config.h> |
25 | ||
2147fb50 KH |
26 | #include <stdlib.h> /* _fmode */ |
27 | #include <stdio.h> | |
28 | #include <fcntl.h> | |
43640c4d | 29 | #include <time.h> |
2147fb50 KH |
30 | #include <windows.h> |
31 | ||
43640c4d GV |
32 | /* Include relevant definitions from IMAGEHLP.H, which can be found |
33 | in \\win32sdk\mstools\samples\image\include\imagehlp.h. */ | |
2147fb50 | 34 | |
43640c4d GV |
35 | PIMAGE_NT_HEADERS |
36 | (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress, | |
37 | DWORD FileLength, | |
38 | LPDWORD HeaderSum, | |
39 | LPDWORD CheckSum); | |
2147fb50 | 40 | |
43640c4d GV |
41 | extern BOOL ctrl_c_handler (unsigned long type); |
42 | ||
43 | extern char my_begdata[]; | |
44 | extern char my_edata[]; | |
45 | extern char my_begbss[]; | |
46 | extern char my_endbss[]; | |
9c8056fe GV |
47 | extern char *my_begbss_static; |
48 | extern char *my_endbss_static; | |
2147fb50 | 49 | |
43640c4d | 50 | #include "w32heap.h" |
e54c8cd1 | 51 | |
03887dd3 KH |
52 | #undef min |
53 | #undef max | |
54 | #define min(x, y) (((x) < (y)) ? (x) : (y)) | |
55 | #define max(x, y) (((x) > (y)) ? (x) : (y)) | |
56 | ||
2147fb50 KH |
57 | /* Basically, our "initialized" flag. */ |
58 | BOOL need_to_recreate_heap = FALSE; | |
59 | ||
60 | /* So we can find our heap in the file to recreate it. */ | |
61 | unsigned long heap_index_in_executable = 0; | |
62 | ||
43640c4d GV |
63 | int open_input_file (file_data *p_file, char *name); |
64 | int open_output_file (file_data *p_file, char *name, unsigned long size); | |
2147fb50 KH |
65 | void close_file_data (file_data *p_file); |
66 | ||
67 | void get_section_info (file_data *p_file); | |
68 | void copy_executable_and_dump_data_section (file_data *, file_data *); | |
69 | void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile); | |
70 | ||
71 | /* Cached info about the .data section in the executable. */ | |
72 | PUCHAR data_start_va = 0; | |
73 | DWORD data_start_file = 0; | |
74 | DWORD data_size = 0; | |
75 | ||
76 | /* Cached info about the .bss section in the executable. */ | |
77 | PUCHAR bss_start = 0; | |
78 | DWORD bss_size = 0; | |
79 | ||
cd6885f3 GV |
80 | #ifdef HAVE_NTGUI |
81 | HINSTANCE hinst = NULL; | |
82 | HINSTANCE hprevinst = NULL; | |
83 | LPSTR lpCmdLine = ""; | |
84 | int nCmdShow = 0; | |
cd6885f3 GV |
85 | #endif /* HAVE_NTGUI */ |
86 | ||
2147fb50 KH |
87 | /* Startup code for running on NT. When we are running as the dumped |
88 | version, we need to bootstrap our heap and .bss section into our | |
89 | address space before we can actually hand off control to the startup | |
90 | code supplied by NT (primarily because that code relies upon malloc ()). */ | |
91 | void | |
92 | _start (void) | |
93 | { | |
94 | extern void mainCRTStartup (void); | |
95 | ||
43640c4d GV |
96 | #if 0 |
97 | /* Give us a way to debug problems with crashes on startup when | |
98 | running under the MSVC profiler. */ | |
99 | if (GetEnvironmentVariable ("EMACS_DEBUG", NULL, 0) > 0) | |
100 | DebugBreak (); | |
101 | #endif | |
102 | ||
2147fb50 KH |
103 | /* Cache system info, e.g., the NT page size. */ |
104 | cache_system_info (); | |
105 | ||
106 | /* If we're a dumped version of emacs then we need to recreate | |
107 | our heap and play tricks with our .bss section. Do this before | |
108 | start up. (WARNING: Do not put any code before this section | |
109 | that relies upon malloc () and runs in the dumped version. It | |
110 | won't work.) */ | |
111 | if (need_to_recreate_heap) | |
112 | { | |
113 | char executable_path[MAX_PATH]; | |
114 | ||
115 | if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0) | |
116 | { | |
117 | printf ("Failed to find path for executable.\n"); | |
118 | exit (1); | |
119 | } | |
43640c4d GV |
120 | |
121 | #if 1 | |
122 | /* To allow profiling, make sure executable_path names the .exe | |
123 | file, not the ._xe file created by the profiler which contains | |
124 | extra code that makes the stored exe offsets incorrect. (This | |
125 | will not be necessary when unexec properly extends the .bss (or | |
126 | .data as appropriate) section to include the dumped bss data, | |
127 | and dumps the heap into a proper section of its own.) */ | |
128 | { | |
129 | char * p = strrchr (executable_path, '.'); | |
130 | if (p && p[1] == '_') | |
131 | p[1] = 'e'; | |
132 | } | |
133 | ||
134 | /* Using HiProf profiler, exe name is different still. */ | |
135 | { | |
136 | char * p = strrchr (executable_path, '\\'); | |
137 | strcpy (p, "\\emacs.exe"); | |
138 | } | |
139 | #endif | |
140 | ||
2147fb50 KH |
141 | recreate_heap (executable_path); |
142 | need_to_recreate_heap = FALSE; | |
143 | } | |
43640c4d GV |
144 | else |
145 | { | |
146 | /* Grab our malloc arena space now, before CRT starts up. */ | |
147 | sbrk (0); | |
148 | } | |
2147fb50 KH |
149 | |
150 | /* The default behavior is to treat files as binary and patch up | |
151 | text files appropriately, in accordance with the MSDOS code. */ | |
152 | _fmode = O_BINARY; | |
153 | ||
154 | /* This prevents ctrl-c's in shells running while we're suspended from | |
155 | having us exit. */ | |
156 | SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE); | |
157 | ||
467af476 AI |
158 | /* Prevent Emacs from being locked up (eg. in batch mode) when |
159 | accessing devices that aren't mounted (eg. removable media drives). */ | |
160 | SetErrorMode (SEM_FAILCRITICALERRORS); | |
161 | ||
2147fb50 KH |
162 | /* Invoke the NT CRT startup routine now that our housecleaning |
163 | is finished. */ | |
cd6885f3 | 164 | #ifdef HAVE_NTGUI |
c2ccbd43 GV |
165 | /* determine WinMain args like crt0.c does */ |
166 | hinst = GetModuleHandle(NULL); | |
167 | lpCmdLine = GetCommandLine(); | |
168 | nCmdShow = SW_SHOWDEFAULT; | |
169 | #endif | |
2147fb50 KH |
170 | mainCRTStartup (); |
171 | } | |
172 | ||
8e6208c5 | 173 | /* Dump out .data and .bss sections into a new executable. */ |
2147fb50 KH |
174 | void |
175 | unexec (char *new_name, char *old_name, void *start_data, void *start_bss, | |
176 | void *entry_address) | |
177 | { | |
178 | file_data in_file, out_file; | |
179 | char out_filename[MAX_PATH], in_filename[MAX_PATH]; | |
180 | unsigned long size; | |
181 | char *ptr; | |
182 | ||
183 | /* Make sure that the input and output filenames have the | |
184 | ".exe" extension...patch them up if they don't. */ | |
185 | strcpy (in_filename, old_name); | |
186 | ptr = in_filename + strlen (in_filename) - 4; | |
187 | if (strcmp (ptr, ".exe")) | |
188 | strcat (in_filename, ".exe"); | |
189 | ||
190 | strcpy (out_filename, new_name); | |
191 | ptr = out_filename + strlen (out_filename) - 4; | |
192 | if (strcmp (ptr, ".exe")) | |
193 | strcat (out_filename, ".exe"); | |
194 | ||
195 | printf ("Dumping from %s\n", in_filename); | |
196 | printf (" to %s\n", out_filename); | |
197 | ||
198 | /* We need to round off our heap to NT's allocation unit (64KB). */ | |
199 | round_heap (get_allocation_unit ()); | |
200 | ||
201 | /* Open the undumped executable file. */ | |
43640c4d GV |
202 | if (!open_input_file (&in_file, in_filename)) |
203 | { | |
204 | printf ("Failed to open %s (%d)...bailing.\n", | |
205 | in_filename, GetLastError ()); | |
206 | exit (1); | |
207 | } | |
2147fb50 KH |
208 | |
209 | /* Get the interesting section info, like start and size of .bss... */ | |
210 | get_section_info (&in_file); | |
211 | ||
212 | /* The size of the dumped executable is the size of the original | |
213 | executable plus the size of the heap and the size of the .bss section. */ | |
198fdd15 GV |
214 | heap_index_in_executable = (unsigned long) |
215 | round_to_next ((unsigned char *) in_file.size, get_allocation_unit ()); | |
2147fb50 | 216 | size = heap_index_in_executable + get_committed_heap_size () + bss_size; |
43640c4d GV |
217 | if (!open_output_file (&out_file, out_filename, size)) |
218 | { | |
219 | printf ("Failed to open %s (%d)...bailing.\n", | |
220 | out_filename, GetLastError ()); | |
221 | exit (1); | |
222 | } | |
2147fb50 KH |
223 | |
224 | /* Set the flag (before dumping). */ | |
225 | need_to_recreate_heap = TRUE; | |
226 | ||
227 | copy_executable_and_dump_data_section (&in_file, &out_file); | |
228 | dump_bss_and_heap (&in_file, &out_file); | |
229 | ||
43640c4d GV |
230 | /* Patch up header fields; profiler is picky about this. */ |
231 | { | |
232 | PIMAGE_DOS_HEADER dos_header; | |
233 | PIMAGE_NT_HEADERS nt_header; | |
234 | HANDLE hImagehelp = LoadLibrary ("imagehlp.dll"); | |
235 | DWORD headersum; | |
236 | DWORD checksum; | |
237 | ||
238 | dos_header = (PIMAGE_DOS_HEADER) out_file.file_base; | |
239 | nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew); | |
240 | ||
241 | nt_header->OptionalHeader.CheckSum = 0; | |
242 | // nt_header->FileHeader.TimeDateStamp = time (NULL); | |
243 | // dos_header->e_cp = size / 512; | |
244 | // nt_header->OptionalHeader.SizeOfImage = size; | |
245 | ||
246 | pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile"); | |
247 | if (pfnCheckSumMappedFile) | |
248 | { | |
249 | // nt_header->FileHeader.TimeDateStamp = time (NULL); | |
250 | pfnCheckSumMappedFile (out_file.file_base, | |
251 | out_file.size, | |
252 | &headersum, | |
253 | &checksum); | |
254 | nt_header->OptionalHeader.CheckSum = checksum; | |
255 | } | |
256 | FreeLibrary (hImagehelp); | |
257 | } | |
258 | ||
2147fb50 KH |
259 | close_file_data (&in_file); |
260 | close_file_data (&out_file); | |
261 | } | |
262 | ||
263 | ||
264 | /* File handling. */ | |
265 | ||
266 | ||
43640c4d | 267 | int |
2147fb50 KH |
268 | open_input_file (file_data *p_file, char *filename) |
269 | { | |
270 | HANDLE file; | |
271 | HANDLE file_mapping; | |
272 | void *file_base; | |
273 | unsigned long size, upper_size; | |
274 | ||
275 | file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
276 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
277 | if (file == INVALID_HANDLE_VALUE) | |
43640c4d | 278 | return FALSE; |
2147fb50 KH |
279 | |
280 | size = GetFileSize (file, &upper_size); | |
281 | file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY, | |
282 | 0, size, NULL); | |
283 | if (!file_mapping) | |
43640c4d | 284 | return FALSE; |
2147fb50 KH |
285 | |
286 | file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size); | |
287 | if (file_base == 0) | |
43640c4d | 288 | return FALSE; |
2147fb50 KH |
289 | |
290 | p_file->name = filename; | |
291 | p_file->size = size; | |
292 | p_file->file = file; | |
293 | p_file->file_mapping = file_mapping; | |
294 | p_file->file_base = file_base; | |
43640c4d GV |
295 | |
296 | return TRUE; | |
2147fb50 KH |
297 | } |
298 | ||
43640c4d | 299 | int |
2147fb50 KH |
300 | open_output_file (file_data *p_file, char *filename, unsigned long size) |
301 | { | |
302 | HANDLE file; | |
303 | HANDLE file_mapping; | |
304 | void *file_base; | |
cd6885f3 | 305 | |
2147fb50 KH |
306 | file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
307 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); | |
308 | if (file == INVALID_HANDLE_VALUE) | |
43640c4d GV |
309 | return FALSE; |
310 | ||
2147fb50 KH |
311 | file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE, |
312 | 0, size, NULL); | |
313 | if (!file_mapping) | |
43640c4d | 314 | return FALSE; |
2147fb50 KH |
315 | |
316 | file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size); | |
317 | if (file_base == 0) | |
43640c4d | 318 | return FALSE; |
2147fb50 KH |
319 | |
320 | p_file->name = filename; | |
321 | p_file->size = size; | |
322 | p_file->file = file; | |
323 | p_file->file_mapping = file_mapping; | |
324 | p_file->file_base = file_base; | |
43640c4d GV |
325 | |
326 | return TRUE; | |
2147fb50 KH |
327 | } |
328 | ||
329 | /* Close the system structures associated with the given file. */ | |
43640c4d | 330 | void |
2147fb50 KH |
331 | close_file_data (file_data *p_file) |
332 | { | |
333 | UnmapViewOfFile (p_file->file_base); | |
334 | CloseHandle (p_file->file_mapping); | |
335 | CloseHandle (p_file->file); | |
336 | } | |
337 | ||
338 | ||
339 | /* Routines to manipulate NT executable file sections. */ | |
340 | ||
9c8056fe | 341 | #ifdef SEPARATE_BSS_SECTION |
a610993d GV |
342 | static void |
343 | get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start, | |
344 | DWORD *p_bss_size) | |
345 | { | |
346 | int n, start, len; | |
347 | char map_filename[MAX_PATH]; | |
348 | char buffer[256]; | |
349 | FILE *map; | |
350 | ||
351 | /* Overwrite the .exe extension on the executable file name with | |
352 | the .map extension. */ | |
353 | strcpy (map_filename, p_infile->name); | |
354 | n = strlen (map_filename) - 3; | |
355 | strcpy (&map_filename[n], "map"); | |
356 | ||
357 | map = fopen (map_filename, "r"); | |
358 | if (!map) | |
359 | { | |
360 | printf ("Failed to open map file %s, error %d...bailing out.\n", | |
361 | map_filename, GetLastError ()); | |
362 | exit (-1); | |
363 | } | |
364 | ||
365 | while (fgets (buffer, sizeof (buffer), map)) | |
366 | { | |
367 | if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA"))) | |
368 | continue; | |
369 | n = sscanf (buffer, " %*d:%x %x", &start, &len); | |
370 | if (n != 2) | |
371 | { | |
372 | printf ("Failed to scan the .bss section line:\n%s", buffer); | |
373 | exit (-1); | |
374 | } | |
375 | break; | |
376 | } | |
377 | *p_bss_start = (PUCHAR) start; | |
378 | *p_bss_size = (DWORD) len; | |
379 | } | |
9c8056fe | 380 | #endif |
2147fb50 | 381 | |
43640c4d | 382 | unsigned long |
2147fb50 KH |
383 | get_section_size (PIMAGE_SECTION_HEADER p_section) |
384 | { | |
43640c4d GV |
385 | /* The true section size, before rounding. Some linkers swap the |
386 | meaning of these two values. */ | |
387 | return min (p_section->SizeOfRawData, | |
388 | p_section->Misc.VirtualSize); | |
389 | } | |
390 | ||
391 | /* Return pointer to section header for named section. */ | |
392 | IMAGE_SECTION_HEADER * | |
393 | find_section (char * name, IMAGE_NT_HEADERS * nt_header) | |
394 | { | |
395 | PIMAGE_SECTION_HEADER section; | |
396 | int i; | |
397 | ||
398 | section = IMAGE_FIRST_SECTION (nt_header); | |
399 | ||
400 | for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) | |
401 | { | |
402 | if (strcmp (section->Name, name) == 0) | |
403 | return section; | |
404 | section++; | |
405 | } | |
406 | return NULL; | |
407 | } | |
408 | ||
409 | /* Return pointer to section header for section containing the given | |
410 | relative virtual address. */ | |
411 | IMAGE_SECTION_HEADER * | |
412 | rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header) | |
413 | { | |
414 | PIMAGE_SECTION_HEADER section; | |
415 | int i; | |
416 | ||
417 | section = IMAGE_FIRST_SECTION (nt_header); | |
418 | ||
419 | for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) | |
2147fb50 | 420 | { |
43640c4d GV |
421 | if (rva >= section->VirtualAddress |
422 | && rva < section->VirtualAddress + section->SizeOfRawData) | |
423 | return section; | |
424 | section++; | |
2147fb50 | 425 | } |
43640c4d | 426 | return NULL; |
2147fb50 KH |
427 | } |
428 | ||
43640c4d | 429 | |
2147fb50 KH |
430 | /* Flip through the executable and cache the info necessary for dumping. */ |
431 | static void | |
432 | get_section_info (file_data *p_infile) | |
433 | { | |
434 | PIMAGE_DOS_HEADER dos_header; | |
435 | PIMAGE_NT_HEADERS nt_header; | |
a610993d | 436 | PIMAGE_SECTION_HEADER section, data_section; |
2147fb50 KH |
437 | unsigned char *ptr; |
438 | int i; | |
439 | ||
440 | dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base; | |
441 | if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) | |
442 | { | |
443 | printf ("Unknown EXE header in %s...bailing.\n", p_infile->name); | |
444 | exit (1); | |
445 | } | |
446 | nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + | |
447 | dos_header->e_lfanew); | |
448 | if (nt_header == NULL) | |
449 | { | |
450 | printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n", | |
451 | p_infile->name); | |
452 | exit (1); | |
453 | } | |
454 | ||
455 | /* Check the NT header signature ... */ | |
456 | if (nt_header->Signature != IMAGE_NT_SIGNATURE) | |
457 | { | |
458 | printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n", | |
459 | nt_header->Signature, p_infile->name); | |
460 | } | |
461 | ||
462 | /* Flip through the sections for .data and .bss ... */ | |
463 | section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header); | |
464 | for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) | |
465 | { | |
43640c4d | 466 | #ifdef SEPARATE_BSS_SECTION |
2147fb50 KH |
467 | if (!strcmp (section->Name, ".bss")) |
468 | { | |
469 | /* The .bss section. */ | |
470 | ptr = (char *) nt_header->OptionalHeader.ImageBase + | |
471 | section->VirtualAddress; | |
472 | bss_start = ptr; | |
473 | bss_size = get_section_size (section); | |
474 | } | |
43640c4d GV |
475 | #endif |
476 | #if 0 | |
2147fb50 KH |
477 | if (!strcmp (section->Name, ".data")) |
478 | { | |
198fdd15 GV |
479 | /* From lastfile.c */ |
480 | extern char my_edata[]; | |
481 | ||
2147fb50 | 482 | /* The .data section. */ |
a610993d | 483 | data_section = section; |
c2ccbd43 | 484 | ptr = (char *) nt_header->OptionalHeader.ImageBase + |
2147fb50 KH |
485 | section->VirtualAddress; |
486 | data_start_va = ptr; | |
487 | data_start_file = section->PointerToRawData; | |
198fdd15 GV |
488 | |
489 | /* We want to only write Emacs data back to the executable, | |
490 | not any of the library data (if library data is included, | |
491 | then a dumped Emacs won't run on system versions other | |
492 | than the one Emacs was dumped on). */ | |
493 | data_size = my_edata - data_start_va; | |
2147fb50 | 494 | } |
43640c4d GV |
495 | #else |
496 | if (!strcmp (section->Name, "EMDATA")) | |
497 | { | |
498 | /* The Emacs initialized data section. */ | |
499 | data_section = section; | |
500 | ptr = (char *) nt_header->OptionalHeader.ImageBase + | |
501 | section->VirtualAddress; | |
502 | data_start_va = ptr; | |
503 | data_start_file = section->PointerToRawData; | |
504 | ||
505 | /* Write back the full section. */ | |
506 | data_size = get_section_size (section); | |
507 | } | |
508 | #endif | |
2147fb50 KH |
509 | section++; |
510 | } | |
a610993d | 511 | |
43640c4d GV |
512 | #ifdef SEPARATE_BSS_SECTION |
513 | if (bss_start == UNINIT_PTR && bss_size == UNINIT_LONG) | |
a610993d GV |
514 | { |
515 | /* Starting with MSVC 4.0, the .bss section has been eliminated | |
516 | and appended virtually to the end of the .data section. Our | |
517 | only hint about where the .bss section starts in the address | |
518 | comes from the SizeOfRawData field in the .data section | |
519 | header. Unfortunately, this field is only approximate, as it | |
520 | is a rounded number and is typically rounded just beyond the | |
521 | start of the .bss section. To find the start and size of the | |
522 | .bss section exactly, we have to peek into the map file. */ | |
523 | get_bss_info_from_map_file (p_infile, &ptr, &bss_size); | |
524 | bss_start = ptr + nt_header->OptionalHeader.ImageBase | |
525 | + data_section->VirtualAddress; | |
526 | } | |
43640c4d | 527 | #else |
9c8056fe GV |
528 | /* As noted in lastfile.c, the Alpha (but not the Intel) MSVC linker |
529 | globally segregates all static and public bss data (ie. across all | |
530 | linked modules, not just per module), so we must take both static and | |
531 | public bss areas into account to determine the true extent of the bss | |
532 | area used by Emacs. | |
533 | ||
534 | To be strictly correct, we should dump the static and public bss | |
535 | areas used by Emacs separately if non-overlapping (since otherwise we | |
536 | are dumping bss data belonging to system libraries, eg. the static | |
537 | bss system data on the Alpha). However, in practice this doesn't | |
538 | seem to matter, since presumably the system libraries always | |
539 | reinitialize their bss variables. */ | |
540 | bss_start = min (my_begbss, my_begbss_static); | |
541 | bss_size = max (my_endbss, my_endbss_static) - bss_start; | |
43640c4d | 542 | #endif |
2147fb50 KH |
543 | } |
544 | ||
545 | ||
546 | /* The dump routines. */ | |
547 | ||
548 | static void | |
549 | copy_executable_and_dump_data_section (file_data *p_infile, | |
550 | file_data *p_outfile) | |
551 | { | |
552 | unsigned char *data_file, *data_va; | |
553 | unsigned long size, index; | |
554 | ||
555 | /* Get a pointer to where the raw data should go in the executable file. */ | |
556 | data_file = (char *) p_outfile->file_base + data_start_file; | |
557 | ||
558 | /* Get a pointer to the raw data in our address space. */ | |
559 | data_va = data_start_va; | |
560 | ||
561 | size = (DWORD) data_file - (DWORD) p_outfile->file_base; | |
562 | printf ("Copying executable up to data section...\n"); | |
563 | printf ("\t0x%08x Offset in input file.\n", 0); | |
564 | printf ("\t0x%08x Offset in output file.\n", 0); | |
565 | printf ("\t0x%08x Size in bytes.\n", size); | |
566 | memcpy (p_outfile->file_base, p_infile->file_base, size); | |
567 | ||
568 | size = data_size; | |
569 | printf ("Dumping .data section...\n"); | |
570 | printf ("\t0x%08x Address in process.\n", data_va); | |
571 | printf ("\t0x%08x Offset in output file.\n", | |
572 | data_file - p_outfile->file_base); | |
573 | printf ("\t0x%08x Size in bytes.\n", size); | |
574 | memcpy (data_file, data_va, size); | |
575 | ||
576 | index = (DWORD) data_file + size - (DWORD) p_outfile->file_base; | |
577 | size = p_infile->size - index; | |
578 | printf ("Copying rest of executable...\n"); | |
579 | printf ("\t0x%08x Offset in input file.\n", index); | |
580 | printf ("\t0x%08x Offset in output file.\n", index); | |
581 | printf ("\t0x%08x Size in bytes.\n", size); | |
582 | memcpy ((char *) p_outfile->file_base + index, | |
583 | (char *) p_infile->file_base + index, size); | |
584 | } | |
585 | ||
586 | static void | |
587 | dump_bss_and_heap (file_data *p_infile, file_data *p_outfile) | |
588 | { | |
589 | unsigned char *heap_data, *bss_data; | |
590 | unsigned long size, index; | |
591 | ||
592 | printf ("Dumping heap into executable...\n"); | |
593 | ||
594 | index = heap_index_in_executable; | |
595 | size = get_committed_heap_size (); | |
596 | heap_data = get_heap_start (); | |
597 | ||
598 | printf ("\t0x%08x Heap start in process.\n", heap_data); | |
599 | printf ("\t0x%08x Heap offset in executable.\n", index); | |
600 | printf ("\t0x%08x Heap size in bytes.\n", size); | |
601 | ||
602 | memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size); | |
603 | ||
604 | printf ("Dumping .bss into executable...\n"); | |
605 | ||
606 | index += size; | |
607 | size = bss_size; | |
608 | bss_data = bss_start; | |
609 | ||
610 | printf ("\t0x%08x BSS start in process.\n", bss_data); | |
611 | printf ("\t0x%08x BSS offset in executable.\n", index); | |
612 | printf ("\t0x%08x BSS size in bytes.\n", size); | |
613 | memcpy ((char *) p_outfile->file_base + index, bss_data, size); | |
614 | } | |
615 | ||
616 | ||
617 | /* Reload and remap routines. */ | |
618 | ||
9c8056fe GV |
619 | void |
620 | w32_fatal_reload_error (char *step) | |
621 | { | |
622 | int error = GetLastError (); | |
623 | char *buffer = alloca (4096); | |
624 | ||
625 | sprintf (buffer, | |
626 | "Emacs failed to load its dumped heap back into its address space.\n" | |
627 | "The error occurred during the following step:\n\n" | |
628 | "%s\n\n" | |
629 | "GetLastError = %d\n\n" | |
630 | "Heap start: 0x%08x\n" | |
631 | "Heap commit: 0x%08x\n" | |
632 | "Heap end: 0x%08x\n\n" | |
633 | "This error typically happens when the system loads a DLL into\n" | |
634 | "the middle of Emacs' address space, preventing Emacs from\n" | |
556a263f GV |
635 | "loading its heap there. If this happens only occasionally, then\n" |
636 | "you can probably ignore it. But if it happens so often that\n" | |
637 | "you cannot get Emacs to start reliably, and you think that Emacs\n" | |
638 | "is installed correctly, then you have a couple of options:\n\n" | |
9c8056fe GV |
639 | "Emacs correctly, then you have two options:\n\n" |
640 | "1) You can dump Emacs yourself. By doing this, you ensure that\n" | |
641 | "Emacs' heap fits around the DLLs in your system. To dump Emacs,\n" | |
642 | "download the emacs-(version)-undump-(arch) distribution file\n" | |
643 | "from the site where you downloaded the executable distribution.\n\n" | |
644 | "2) You can build Emacs from source. This is just another way\n" | |
645 | "to dump Emacs on your system.", | |
646 | step, | |
647 | error, | |
648 | get_heap_start (), | |
649 | get_heap_start () + get_committed_heap_size (), | |
650 | get_heap_end ()); | |
651 | ||
652 | MessageBox (NULL, | |
653 | buffer, | |
654 | "Emacs Abort Dialog", | |
655 | MB_OK | MB_ICONEXCLAMATION | MB_TASKMODAL); | |
656 | ||
657 | exit (-1); | |
658 | } | |
2147fb50 KH |
659 | |
660 | /* Load the dumped .bss section into the .bss area of our address space. */ | |
661 | void | |
662 | read_in_bss (char *filename) | |
663 | { | |
664 | HANDLE file; | |
665 | unsigned long size, index, n_read, total_read; | |
666 | char buffer[512], *bss; | |
667 | int i; | |
668 | ||
669 | file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
670 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
671 | if (file == INVALID_HANDLE_VALUE) | |
9c8056fe | 672 | w32_fatal_reload_error ("Opening Emacs executable file for .bss."); |
2147fb50 KH |
673 | |
674 | /* Seek to where the .bss section is tucked away after the heap... */ | |
675 | index = heap_index_in_executable + get_committed_heap_size (); | |
676 | if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF) | |
9c8056fe | 677 | w32_fatal_reload_error ("Seeking to the saved .bss section."); |
2147fb50 KH |
678 | |
679 | /* Ok, read in the saved .bss section and initialize all | |
680 | uninitialized variables. */ | |
198fdd15 | 681 | if (!ReadFile (file, bss_start, bss_size, &n_read, NULL)) |
9c8056fe | 682 | w32_fatal_reload_error ("Reading the saved .bss section."); |
198fdd15 | 683 | |
2147fb50 KH |
684 | CloseHandle (file); |
685 | } | |
686 | ||
687 | /* Map the heap dumped into the executable file into our address space. */ | |
688 | void | |
689 | map_in_heap (char *filename) | |
690 | { | |
691 | HANDLE file; | |
692 | HANDLE file_mapping; | |
693 | void *file_base; | |
198fdd15 | 694 | unsigned long size, upper_size, n_read; |
2147fb50 KH |
695 | int i; |
696 | ||
697 | file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
698 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
699 | if (file == INVALID_HANDLE_VALUE) | |
9c8056fe | 700 | w32_fatal_reload_error ("Opening Emacs executable file for heap."); |
2147fb50 KH |
701 | |
702 | size = GetFileSize (file, &upper_size); | |
703 | file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY, | |
704 | 0, size, NULL); | |
705 | if (!file_mapping) | |
9c8056fe | 706 | w32_fatal_reload_error ("Creating file mapping to heap in executable."); |
2147fb50 KH |
707 | |
708 | size = get_committed_heap_size (); | |
709 | file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0, | |
710 | heap_index_in_executable, size, | |
711 | get_heap_start ()); | |
198fdd15 GV |
712 | if (file_base != 0) |
713 | { | |
714 | return; | |
715 | } | |
716 | ||
717 | /* If we don't succeed with the mapping, then copy from the | |
718 | data into the heap. */ | |
719 | ||
720 | CloseHandle (file_mapping); | |
721 | ||
722 | if (VirtualAlloc (get_heap_start (), get_committed_heap_size (), | |
723 | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL) | |
9c8056fe | 724 | w32_fatal_reload_error ("Allocating heap address space."); |
198fdd15 GV |
725 | |
726 | /* Seek to the location of the heap data in the executable. */ | |
727 | i = heap_index_in_executable; | |
728 | if (SetFilePointer (file, i, NULL, FILE_BEGIN) == 0xFFFFFFFF) | |
9c8056fe | 729 | w32_fatal_reload_error ("Seeking to saved heap in executable file."); |
198fdd15 GV |
730 | |
731 | /* Read in the data. */ | |
732 | if (!ReadFile (file, get_heap_start (), | |
733 | get_committed_heap_size (), &n_read, NULL)) | |
9c8056fe | 734 | w32_fatal_reload_error ("Reading saved heap from executable file."); |
198fdd15 GV |
735 | |
736 | CloseHandle (file); | |
2147fb50 | 737 | } |