1 /* unexec for GNU Emacs on Windows NT.
2 Copyright (C) 1994 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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.
21 Geoff Voelker (voelker@cs.washington.edu) 8-12-94
24 #include <stdlib.h> /* _fmode */
29 extern BOOL
ctrl_c_handler (unsigned long type
);
33 /* A convenient type for keeping all the info about a mapped file together. */
34 typedef struct file_data
{
39 unsigned char *file_base
;
42 /* Basically, our "initialized" flag. */
43 BOOL need_to_recreate_heap
= FALSE
;
45 /* So we can find our heap in the file to recreate it. */
46 unsigned long heap_index_in_executable
= 0;
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
);
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
);
56 /* Cached info about the .data section in the executable. */
57 PUCHAR data_start_va
= 0;
58 DWORD data_start_file
= 0;
61 /* Cached info about the .bss section in the executable. */
66 HINSTANCE hinst
= NULL
;
67 HINSTANCE hprevinst
= NULL
;
72 WinMain (_hinst
, _hPrevInst
, _lpCmdLine
, _nCmdShow
)
81 /* Need to parse command line */
84 hprevinst
= _hPrevInst
;
85 lpCmdLine
= _lpCmdLine
;
89 new_argv
= (char **) xmalloc (sizeof (char *) * new_argc
);
91 return main (__argc
, __argv
, _environ
);
93 for (i
= j
= 0; i
< __argc
; i
++)
95 /* Allocate a console window for stdout and stderr if requested.
96 We want to allocate as soon as we possibly can to catch
98 if (!strcmp ("-output_console", __argv
[i
]))
104 new_argv
[j
++] = __argv
[i
];
107 return main (new_argc
, new_argv
, _environ
);
109 #endif /* HAVE_NTGUI */
111 /* Startup code for running on NT. When we are running as the dumped
112 version, we need to bootstrap our heap and .bss section into our
113 address space before we can actually hand off control to the startup
114 code supplied by NT (primarily because that code relies upon malloc ()). */
119 extern void WinMainCRTStartup (void);
121 extern void mainCRTStartup (void);
122 #endif /* HAVE_NTGUI */
124 /* Cache system info, e.g., the NT page size. */
125 cache_system_info ();
127 /* If we're a dumped version of emacs then we need to recreate
128 our heap and play tricks with our .bss section. Do this before
129 start up. (WARNING: Do not put any code before this section
130 that relies upon malloc () and runs in the dumped version. It
132 if (need_to_recreate_heap
)
134 char executable_path
[MAX_PATH
];
136 if (GetModuleFileName (NULL
, executable_path
, MAX_PATH
) == 0)
138 printf ("Failed to find path for executable.\n");
141 recreate_heap (executable_path
);
142 need_to_recreate_heap
= FALSE
;
145 /* The default behavior is to treat files as binary and patch up
146 text files appropriately, in accordance with the MSDOS code. */
149 /* This prevents ctrl-c's in shells running while we're suspended from
151 SetConsoleCtrlHandler ((PHANDLER_ROUTINE
) ctrl_c_handler
, TRUE
);
153 /* Invoke the NT CRT startup routine now that our housecleaning
156 WinMainCRTStartup ();
159 #endif /* HAVE_NTGUI */
162 /* Dump out .data and .bss sections into a new executable. */
164 unexec (char *new_name
, char *old_name
, void *start_data
, void *start_bss
,
167 file_data in_file
, out_file
;
168 char out_filename
[MAX_PATH
], in_filename
[MAX_PATH
];
172 /* Make sure that the input and output filenames have the
173 ".exe" extension...patch them up if they don't. */
174 strcpy (in_filename
, old_name
);
175 ptr
= in_filename
+ strlen (in_filename
) - 4;
176 if (strcmp (ptr
, ".exe"))
177 strcat (in_filename
, ".exe");
179 strcpy (out_filename
, new_name
);
180 ptr
= out_filename
+ strlen (out_filename
) - 4;
181 if (strcmp (ptr
, ".exe"))
182 strcat (out_filename
, ".exe");
184 printf ("Dumping from %s\n", in_filename
);
185 printf (" to %s\n", out_filename
);
187 /* We need to round off our heap to NT's allocation unit (64KB). */
188 round_heap (get_allocation_unit ());
190 /* Open the undumped executable file. */
191 open_input_file (&in_file
, in_filename
);
193 /* Get the interesting section info, like start and size of .bss... */
194 get_section_info (&in_file
);
196 /* The size of the dumped executable is the size of the original
197 executable plus the size of the heap and the size of the .bss section. */
198 heap_index_in_executable
= (unsigned long)
199 round_to_next ((unsigned char *) in_file
.size
, get_allocation_unit ());
200 size
= heap_index_in_executable
+ get_committed_heap_size () + bss_size
;
201 open_output_file (&out_file
, out_filename
, size
);
203 /* Set the flag (before dumping). */
204 need_to_recreate_heap
= TRUE
;
206 copy_executable_and_dump_data_section (&in_file
, &out_file
);
207 dump_bss_and_heap (&in_file
, &out_file
);
209 close_file_data (&in_file
);
210 close_file_data (&out_file
);
218 open_input_file (file_data
*p_file
, char *filename
)
223 unsigned long size
, upper_size
;
225 file
= CreateFile (filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
226 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
227 if (file
== INVALID_HANDLE_VALUE
)
229 printf ("Failed to open %s (%d)...bailing.\n",
230 filename
, GetLastError ());
234 size
= GetFileSize (file
, &upper_size
);
235 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_READONLY
,
239 printf ("Failed to create file mapping of %s (%d)...bailing.\n",
240 filename
, GetLastError ());
244 file_base
= MapViewOfFile (file_mapping
, FILE_MAP_READ
, 0, 0, size
);
247 printf ("Failed to map view of file of %s (%d)...bailing.\n",
248 filename
, GetLastError ());
252 p_file
->name
= filename
;
255 p_file
->file_mapping
= file_mapping
;
256 p_file
->file_base
= file_base
;
260 open_output_file (file_data
*p_file
, char *filename
, unsigned long size
)
267 file
= CreateFile (filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
268 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
269 if (file
== INVALID_HANDLE_VALUE
)
272 printf ("open_output_file: Failed to open %s (%d).\n",
277 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_READWRITE
,
282 printf ("open_output_file: Failed to create file mapping of %s (%d).\n",
287 file_base
= MapViewOfFile (file_mapping
, FILE_MAP_WRITE
, 0, 0, size
);
291 printf ("open_output_file: Failed to map view of file of %s (%d).\n",
296 p_file
->name
= filename
;
299 p_file
->file_mapping
= file_mapping
;
300 p_file
->file_base
= file_base
;
303 /* Close the system structures associated with the given file. */
305 close_file_data (file_data
*p_file
)
307 UnmapViewOfFile (p_file
->file_base
);
308 CloseHandle (p_file
->file_mapping
);
309 CloseHandle (p_file
->file
);
313 /* Routines to manipulate NT executable file sections. */
316 get_bss_info_from_map_file (file_data
*p_infile
, PUCHAR
*p_bss_start
,
320 char map_filename
[MAX_PATH
];
324 /* Overwrite the .exe extension on the executable file name with
325 the .map extension. */
326 strcpy (map_filename
, p_infile
->name
);
327 n
= strlen (map_filename
) - 3;
328 strcpy (&map_filename
[n
], "map");
330 map
= fopen (map_filename
, "r");
333 printf ("Failed to open map file %s, error %d...bailing out.\n",
334 map_filename
, GetLastError ());
338 while (fgets (buffer
, sizeof (buffer
), map
))
340 if (!(strstr (buffer
, ".bss") && strstr (buffer
, "DATA")))
342 n
= sscanf (buffer
, " %*d:%x %x", &start
, &len
);
345 printf ("Failed to scan the .bss section line:\n%s", buffer
);
350 *p_bss_start
= (PUCHAR
) start
;
351 *p_bss_size
= (DWORD
) len
;
355 get_section_size (PIMAGE_SECTION_HEADER p_section
)
357 /* The section size is in different locations in the different versions. */
358 switch (get_nt_minor_version ())
361 return p_section
->SizeOfRawData
;
363 return p_section
->Misc
.VirtualSize
;
367 /* Flip through the executable and cache the info necessary for dumping. */
369 get_section_info (file_data
*p_infile
)
371 PIMAGE_DOS_HEADER dos_header
;
372 PIMAGE_NT_HEADERS nt_header
;
373 PIMAGE_SECTION_HEADER section
, data_section
;
377 dos_header
= (PIMAGE_DOS_HEADER
) p_infile
->file_base
;
378 if (dos_header
->e_magic
!= IMAGE_DOS_SIGNATURE
)
380 printf ("Unknown EXE header in %s...bailing.\n", p_infile
->name
);
383 nt_header
= (PIMAGE_NT_HEADERS
) (((unsigned long) dos_header
) +
384 dos_header
->e_lfanew
);
385 if (nt_header
== NULL
)
387 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
392 /* Check the NT header signature ... */
393 if (nt_header
->Signature
!= IMAGE_NT_SIGNATURE
)
395 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
396 nt_header
->Signature
, p_infile
->name
);
399 /* Flip through the sections for .data and .bss ... */
400 section
= (PIMAGE_SECTION_HEADER
) IMAGE_FIRST_SECTION (nt_header
);
401 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
403 if (!strcmp (section
->Name
, ".bss"))
405 /* The .bss section. */
406 ptr
= (char *) nt_header
->OptionalHeader
.ImageBase
+
407 section
->VirtualAddress
;
409 bss_size
= get_section_size (section
);
411 if (!strcmp (section
->Name
, ".data"))
413 /* From lastfile.c */
414 extern char my_edata
[];
416 /* The .data section. */
417 data_section
= section
;
418 ptr
= (char *) nt_header
->OptionalHeader
.ImageBase
+
419 section
->VirtualAddress
;
421 data_start_file
= section
->PointerToRawData
;
423 /* We want to only write Emacs data back to the executable,
424 not any of the library data (if library data is included,
425 then a dumped Emacs won't run on system versions other
426 than the one Emacs was dumped on). */
427 data_size
= my_edata
- data_start_va
;
432 if (!bss_start
&& !bss_size
)
434 /* Starting with MSVC 4.0, the .bss section has been eliminated
435 and appended virtually to the end of the .data section. Our
436 only hint about where the .bss section starts in the address
437 comes from the SizeOfRawData field in the .data section
438 header. Unfortunately, this field is only approximate, as it
439 is a rounded number and is typically rounded just beyond the
440 start of the .bss section. To find the start and size of the
441 .bss section exactly, we have to peek into the map file. */
442 get_bss_info_from_map_file (p_infile
, &ptr
, &bss_size
);
443 bss_start
= ptr
+ nt_header
->OptionalHeader
.ImageBase
444 + data_section
->VirtualAddress
;
449 /* The dump routines. */
452 copy_executable_and_dump_data_section (file_data
*p_infile
,
453 file_data
*p_outfile
)
455 unsigned char *data_file
, *data_va
;
456 unsigned long size
, index
;
458 /* Get a pointer to where the raw data should go in the executable file. */
459 data_file
= (char *) p_outfile
->file_base
+ data_start_file
;
461 /* Get a pointer to the raw data in our address space. */
462 data_va
= data_start_va
;
464 size
= (DWORD
) data_file
- (DWORD
) p_outfile
->file_base
;
465 printf ("Copying executable up to data section...\n");
466 printf ("\t0x%08x Offset in input file.\n", 0);
467 printf ("\t0x%08x Offset in output file.\n", 0);
468 printf ("\t0x%08x Size in bytes.\n", size
);
469 memcpy (p_outfile
->file_base
, p_infile
->file_base
, size
);
472 printf ("Dumping .data section...\n");
473 printf ("\t0x%08x Address in process.\n", data_va
);
474 printf ("\t0x%08x Offset in output file.\n",
475 data_file
- p_outfile
->file_base
);
476 printf ("\t0x%08x Size in bytes.\n", size
);
477 memcpy (data_file
, data_va
, size
);
479 index
= (DWORD
) data_file
+ size
- (DWORD
) p_outfile
->file_base
;
480 size
= p_infile
->size
- index
;
481 printf ("Copying rest of executable...\n");
482 printf ("\t0x%08x Offset in input file.\n", index
);
483 printf ("\t0x%08x Offset in output file.\n", index
);
484 printf ("\t0x%08x Size in bytes.\n", size
);
485 memcpy ((char *) p_outfile
->file_base
+ index
,
486 (char *) p_infile
->file_base
+ index
, size
);
490 dump_bss_and_heap (file_data
*p_infile
, file_data
*p_outfile
)
492 unsigned char *heap_data
, *bss_data
;
493 unsigned long size
, index
;
495 printf ("Dumping heap into executable...\n");
497 index
= heap_index_in_executable
;
498 size
= get_committed_heap_size ();
499 heap_data
= get_heap_start ();
501 printf ("\t0x%08x Heap start in process.\n", heap_data
);
502 printf ("\t0x%08x Heap offset in executable.\n", index
);
503 printf ("\t0x%08x Heap size in bytes.\n", size
);
505 memcpy ((PUCHAR
) p_outfile
->file_base
+ index
, heap_data
, size
);
507 printf ("Dumping .bss into executable...\n");
511 bss_data
= bss_start
;
513 printf ("\t0x%08x BSS start in process.\n", bss_data
);
514 printf ("\t0x%08x BSS offset in executable.\n", index
);
515 printf ("\t0x%08x BSS size in bytes.\n", size
);
516 memcpy ((char *) p_outfile
->file_base
+ index
, bss_data
, size
);
520 /* Reload and remap routines. */
523 /* Load the dumped .bss section into the .bss area of our address space. */
525 read_in_bss (char *filename
)
528 unsigned long size
, index
, n_read
, total_read
;
529 char buffer
[512], *bss
;
532 file
= CreateFile (filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
533 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
534 if (file
== INVALID_HANDLE_VALUE
)
540 /* Seek to where the .bss section is tucked away after the heap... */
541 index
= heap_index_in_executable
+ get_committed_heap_size ();
542 if (SetFilePointer (file
, index
, NULL
, FILE_BEGIN
) == 0xFFFFFFFF)
549 /* Ok, read in the saved .bss section and initialize all
550 uninitialized variables. */
551 if (!ReadFile (file
, bss_start
, bss_size
, &n_read
, NULL
))
560 /* Map the heap dumped into the executable file into our address space. */
562 map_in_heap (char *filename
)
567 unsigned long size
, upper_size
, n_read
;
570 file
= CreateFile (filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
571 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
572 if (file
== INVALID_HANDLE_VALUE
)
578 size
= GetFileSize (file
, &upper_size
);
579 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_WRITECOPY
,
587 size
= get_committed_heap_size ();
588 file_base
= MapViewOfFileEx (file_mapping
, FILE_MAP_COPY
, 0,
589 heap_index_in_executable
, size
,
596 /* If we don't succeed with the mapping, then copy from the
597 data into the heap. */
599 CloseHandle (file_mapping
);
601 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
602 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
) == NULL
)
608 /* Seek to the location of the heap data in the executable. */
609 i
= heap_index_in_executable
;
610 if (SetFilePointer (file
, i
, NULL
, FILE_BEGIN
) == 0xFFFFFFFF)
616 /* Read in the data. */
617 if (!ReadFile (file
, get_heap_start (),
618 get_committed_heap_size (), &n_read
, NULL
))