2 unexec for GNU Emacs on Windows NT.
4 Copyright (C) 1994 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 GNU Emacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 You should have received a copy of the GNU General Public License along
19 with GNU Emacs; see the file COPYING. If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 Geoff Voelker (voelker@cs.washington.edu) 8-12-94
25 #include <stdlib.h> /* _fmode */
30 extern BOOL
ctrl_c_handler (unsigned long type
);
34 /* A convenient type for keeping all the info about a mapped file together. */
35 typedef struct file_data
{
40 unsigned char *file_base
;
43 /* Basically, our "initialized" flag. */
44 BOOL need_to_recreate_heap
= FALSE
;
46 /* So we can find our heap in the file to recreate it. */
47 unsigned long heap_index_in_executable
= 0;
49 void open_input_file (file_data
*p_file
, char *name
);
50 void open_output_file (file_data
*p_file
, char *name
, unsigned long size
);
51 void close_file_data (file_data
*p_file
);
53 void get_section_info (file_data
*p_file
);
54 void copy_executable_and_dump_data_section (file_data
*, file_data
*);
55 void dump_bss_and_heap (file_data
*p_infile
, file_data
*p_outfile
);
57 /* Cached info about the .data section in the executable. */
58 PUCHAR data_start_va
= 0;
59 DWORD data_start_file
= 0;
62 /* Cached info about the .bss section in the executable. */
66 /* Startup code for running on NT. When we are running as the dumped
67 version, we need to bootstrap our heap and .bss section into our
68 address space before we can actually hand off control to the startup
69 code supplied by NT (primarily because that code relies upon malloc ()). */
73 extern void mainCRTStartup (void);
75 /* Cache system info, e.g., the NT page size. */
78 /* If we're a dumped version of emacs then we need to recreate
79 our heap and play tricks with our .bss section. Do this before
80 start up. (WARNING: Do not put any code before this section
81 that relies upon malloc () and runs in the dumped version. It
83 if (need_to_recreate_heap
)
85 char executable_path
[MAX_PATH
];
87 if (GetModuleFileName (NULL
, executable_path
, MAX_PATH
) == 0)
89 printf ("Failed to find path for executable.\n");
92 recreate_heap (executable_path
);
93 need_to_recreate_heap
= FALSE
;
96 /* The default behavior is to treat files as binary and patch up
97 text files appropriately, in accordance with the MSDOS code. */
100 /* This prevents ctrl-c's in shells running while we're suspended from
102 SetConsoleCtrlHandler ((PHANDLER_ROUTINE
) ctrl_c_handler
, TRUE
);
104 /* Invoke the NT CRT startup routine now that our housecleaning
109 /* Dump out .data and .bss sections into a new exectubale. */
111 unexec (char *new_name
, char *old_name
, void *start_data
, void *start_bss
,
114 file_data in_file
, out_file
;
115 char out_filename
[MAX_PATH
], in_filename
[MAX_PATH
];
119 /* Make sure that the input and output filenames have the
120 ".exe" extension...patch them up if they don't. */
121 strcpy (in_filename
, old_name
);
122 ptr
= in_filename
+ strlen (in_filename
) - 4;
123 if (strcmp (ptr
, ".exe"))
124 strcat (in_filename
, ".exe");
126 strcpy (out_filename
, new_name
);
127 ptr
= out_filename
+ strlen (out_filename
) - 4;
128 if (strcmp (ptr
, ".exe"))
129 strcat (out_filename
, ".exe");
131 printf ("Dumping from %s\n", in_filename
);
132 printf (" to %s\n", out_filename
);
134 /* We need to round off our heap to NT's allocation unit (64KB). */
135 round_heap (get_allocation_unit ());
137 /* Open the undumped executable file. */
138 open_input_file (&in_file
, in_filename
);
140 /* Get the interesting section info, like start and size of .bss... */
141 get_section_info (&in_file
);
143 /* The size of the dumped executable is the size of the original
144 executable plus the size of the heap and the size of the .bss section. */
145 heap_index_in_executable
= (unsigned long)
146 round_to_next ((unsigned char *) in_file
.size
, get_allocation_unit ());
147 size
= heap_index_in_executable
+ get_committed_heap_size () + bss_size
;
148 open_output_file (&out_file
, out_filename
, size
);
150 /* Set the flag (before dumping). */
151 need_to_recreate_heap
= TRUE
;
153 copy_executable_and_dump_data_section (&in_file
, &out_file
);
154 dump_bss_and_heap (&in_file
, &out_file
);
156 close_file_data (&in_file
);
157 close_file_data (&out_file
);
165 open_input_file (file_data
*p_file
, char *filename
)
170 unsigned long size
, upper_size
;
172 file
= CreateFile (filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
173 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
174 if (file
== INVALID_HANDLE_VALUE
)
176 printf ("Failed to open %s (%d)...bailing.\n",
177 filename
, GetLastError ());
181 size
= GetFileSize (file
, &upper_size
);
182 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_READONLY
,
186 printf ("Failed to create file mapping of %s (%d)...bailing.\n",
187 filename
, GetLastError ());
191 file_base
= MapViewOfFile (file_mapping
, FILE_MAP_READ
, 0, 0, size
);
194 printf ("Failed to map view of file of %s (%d)...bailing.\n",
195 filename
, GetLastError ());
199 p_file
->name
= filename
;
202 p_file
->file_mapping
= file_mapping
;
203 p_file
->file_base
= file_base
;
207 open_output_file (file_data
*p_file
, char *filename
, unsigned long size
)
213 file
= CreateFile (filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
214 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
215 if (file
== INVALID_HANDLE_VALUE
)
217 printf ("open_output_file: Failed to open %s (%d).\n",
218 filename
, GetLastError ());
222 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_READWRITE
,
226 printf ("open_output_file: Failed to create file mapping of %s (%d).\n",
227 filename
, GetLastError ());
231 file_base
= MapViewOfFile (file_mapping
, FILE_MAP_WRITE
, 0, 0, size
);
234 printf ("open_output_file: Failed to map view of file of %s (%d).\n",
235 filename
, GetLastError ());
239 p_file
->name
= filename
;
242 p_file
->file_mapping
= file_mapping
;
243 p_file
->file_base
= file_base
;
246 /* Close the system structures associated with the given file. */
248 close_file_data (file_data
*p_file
)
250 UnmapViewOfFile (p_file
->file_base
);
251 CloseHandle (p_file
->file_mapping
);
252 CloseHandle (p_file
->file
);
256 /* Routines to manipulate NT executable file sections. */
260 get_section_size (PIMAGE_SECTION_HEADER p_section
)
262 /* The section size is in different locations in the different versions. */
263 switch (get_nt_minor_version ())
266 return p_section
->SizeOfRawData
;
268 return p_section
->Misc
.VirtualSize
;
272 /* Flip through the executable and cache the info necessary for dumping. */
274 get_section_info (file_data
*p_infile
)
276 PIMAGE_DOS_HEADER dos_header
;
277 PIMAGE_NT_HEADERS nt_header
;
278 PIMAGE_SECTION_HEADER section
;
282 dos_header
= (PIMAGE_DOS_HEADER
) p_infile
->file_base
;
283 if (dos_header
->e_magic
!= IMAGE_DOS_SIGNATURE
)
285 printf ("Unknown EXE header in %s...bailing.\n", p_infile
->name
);
288 nt_header
= (PIMAGE_NT_HEADERS
) (((unsigned long) dos_header
) +
289 dos_header
->e_lfanew
);
290 if (nt_header
== NULL
)
292 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
297 /* Check the NT header signature ... */
298 if (nt_header
->Signature
!= IMAGE_NT_SIGNATURE
)
300 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
301 nt_header
->Signature
, p_infile
->name
);
304 /* Flip through the sections for .data and .bss ... */
305 section
= (PIMAGE_SECTION_HEADER
) IMAGE_FIRST_SECTION (nt_header
);
306 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
308 if (!strcmp (section
->Name
, ".bss"))
310 /* The .bss section. */
311 ptr
= (char *) nt_header
->OptionalHeader
.ImageBase
+
312 section
->VirtualAddress
;
314 bss_size
= get_section_size (section
);
316 if (!strcmp (section
->Name
, ".data"))
318 /* From lastfile.c */
319 extern char my_edata
[];
321 /* The .data section. */
322 ptr
= (char *) nt_header
->OptionalHeader
.ImageBase
+
323 section
->VirtualAddress
;
325 data_start_file
= section
->PointerToRawData
;
327 /* We want to only write Emacs data back to the executable,
328 not any of the library data (if library data is included,
329 then a dumped Emacs won't run on system versions other
330 than the one Emacs was dumped on). */
331 data_size
= my_edata
- data_start_va
;
338 /* The dump routines. */
341 copy_executable_and_dump_data_section (file_data
*p_infile
,
342 file_data
*p_outfile
)
344 unsigned char *data_file
, *data_va
;
345 unsigned long size
, index
;
347 /* Get a pointer to where the raw data should go in the executable file. */
348 data_file
= (char *) p_outfile
->file_base
+ data_start_file
;
350 /* Get a pointer to the raw data in our address space. */
351 data_va
= data_start_va
;
353 size
= (DWORD
) data_file
- (DWORD
) p_outfile
->file_base
;
354 printf ("Copying executable up to data section...\n");
355 printf ("\t0x%08x Offset in input file.\n", 0);
356 printf ("\t0x%08x Offset in output file.\n", 0);
357 printf ("\t0x%08x Size in bytes.\n", size
);
358 memcpy (p_outfile
->file_base
, p_infile
->file_base
, size
);
361 printf ("Dumping .data section...\n");
362 printf ("\t0x%08x Address in process.\n", data_va
);
363 printf ("\t0x%08x Offset in output file.\n",
364 data_file
- p_outfile
->file_base
);
365 printf ("\t0x%08x Size in bytes.\n", size
);
366 memcpy (data_file
, data_va
, size
);
368 index
= (DWORD
) data_file
+ size
- (DWORD
) p_outfile
->file_base
;
369 size
= p_infile
->size
- index
;
370 printf ("Copying rest of executable...\n");
371 printf ("\t0x%08x Offset in input file.\n", index
);
372 printf ("\t0x%08x Offset in output file.\n", index
);
373 printf ("\t0x%08x Size in bytes.\n", size
);
374 memcpy ((char *) p_outfile
->file_base
+ index
,
375 (char *) p_infile
->file_base
+ index
, size
);
379 dump_bss_and_heap (file_data
*p_infile
, file_data
*p_outfile
)
381 unsigned char *heap_data
, *bss_data
;
382 unsigned long size
, index
;
384 printf ("Dumping heap into executable...\n");
386 index
= heap_index_in_executable
;
387 size
= get_committed_heap_size ();
388 heap_data
= get_heap_start ();
390 printf ("\t0x%08x Heap start in process.\n", heap_data
);
391 printf ("\t0x%08x Heap offset in executable.\n", index
);
392 printf ("\t0x%08x Heap size in bytes.\n", size
);
394 memcpy ((PUCHAR
) p_outfile
->file_base
+ index
, heap_data
, size
);
396 printf ("Dumping .bss into executable...\n");
400 bss_data
= bss_start
;
402 printf ("\t0x%08x BSS start in process.\n", bss_data
);
403 printf ("\t0x%08x BSS offset in executable.\n", index
);
404 printf ("\t0x%08x BSS size in bytes.\n", size
);
405 memcpy ((char *) p_outfile
->file_base
+ index
, bss_data
, size
);
409 /* Reload and remap routines. */
412 /* Load the dumped .bss section into the .bss area of our address space. */
414 read_in_bss (char *filename
)
417 unsigned long size
, index
, n_read
, total_read
;
418 char buffer
[512], *bss
;
421 file
= CreateFile (filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
422 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
423 if (file
== INVALID_HANDLE_VALUE
)
429 /* Seek to where the .bss section is tucked away after the heap... */
430 index
= heap_index_in_executable
+ get_committed_heap_size ();
431 if (SetFilePointer (file
, index
, NULL
, FILE_BEGIN
) == 0xFFFFFFFF)
438 /* Ok, read in the saved .bss section and initialize all
439 uninitialized variables. */
440 if (!ReadFile (file
, bss_start
, bss_size
, &n_read
, NULL
))
449 /* Map the heap dumped into the executable file into our address space. */
451 map_in_heap (char *filename
)
456 unsigned long size
, upper_size
, n_read
;
459 file
= CreateFile (filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
460 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
461 if (file
== INVALID_HANDLE_VALUE
)
467 size
= GetFileSize (file
, &upper_size
);
468 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_WRITECOPY
,
476 size
= get_committed_heap_size ();
477 file_base
= MapViewOfFileEx (file_mapping
, FILE_MAP_COPY
, 0,
478 heap_index_in_executable
, size
,
485 /* If we don't succeed with the mapping, then copy from the
486 data into the heap. */
488 CloseHandle (file_mapping
);
490 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
491 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
) == NULL
)
497 /* Seek to the location of the heap data in the executable. */
498 i
= heap_index_in_executable
;
499 if (SetFilePointer (file
, i
, NULL
, FILE_BEGIN
) == 0xFFFFFFFF)
505 /* Read in the data. */
506 if (!ReadFile (file
, get_heap_start (),
507 get_committed_heap_size (), &n_read
, NULL
))