Correct initial line typo.
[emacs.git] / src / unexw32.c
blobcf89f031bb956d2d0fb75a27f56831eddac5df13
1 /*
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
11 version.
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
16 more details.
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 */
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <windows.h>
30 extern BOOL ctrl_c_handler (unsigned long type);
32 #include "ntheap.h"
34 /* A convenient type for keeping all the info about a mapped file together. */
35 typedef struct file_data {
36 char *name;
37 unsigned long size;
38 HANDLE file;
39 HANDLE file_mapping;
40 unsigned char *file_base;
41 } file_data;
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;
60 DWORD data_size = 0;
62 /* Cached info about the .bss section in the executable. */
63 PUCHAR bss_start = 0;
64 DWORD bss_size = 0;
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 ()). */
70 void
71 _start (void)
73 extern void mainCRTStartup (void);
75 /* Cache system info, e.g., the NT page size. */
76 cache_system_info ();
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
82 won't work.) */
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");
90 exit (1);
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. */
98 _fmode = O_BINARY;
100 /* This prevents ctrl-c's in shells running while we're suspended from
101 having us exit. */
102 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
104 /* Invoke the NT CRT startup routine now that our housecleaning
105 is finished. */
106 mainCRTStartup ();
109 /* Dump out .data and .bss sections into a new exectubale. */
110 void
111 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
112 void *entry_address)
114 file_data in_file, out_file;
115 char out_filename[MAX_PATH], in_filename[MAX_PATH];
116 unsigned long size;
117 char *ptr;
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);
161 /* File handling. */
164 void
165 open_input_file (file_data *p_file, char *filename)
167 HANDLE file;
168 HANDLE file_mapping;
169 void *file_base;
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 ());
178 exit (1);
181 size = GetFileSize (file, &upper_size);
182 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
183 0, size, NULL);
184 if (!file_mapping)
186 printf ("Failed to create file mapping of %s (%d)...bailing.\n",
187 filename, GetLastError ());
188 exit (1);
191 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
192 if (file_base == 0)
194 printf ("Failed to map view of file of %s (%d)...bailing.\n",
195 filename, GetLastError ());
196 exit (1);
199 p_file->name = filename;
200 p_file->size = size;
201 p_file->file = file;
202 p_file->file_mapping = file_mapping;
203 p_file->file_base = file_base;
206 void
207 open_output_file (file_data *p_file, char *filename, unsigned long size)
209 HANDLE file;
210 HANDLE file_mapping;
211 void *file_base;
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 ());
219 exit (1);
222 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
223 0, size, NULL);
224 if (!file_mapping)
226 printf ("open_output_file: Failed to create file mapping of %s (%d).\n",
227 filename, GetLastError ());
228 exit (1);
231 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
232 if (file_base == 0)
234 printf ("open_output_file: Failed to map view of file of %s (%d).\n",
235 filename, GetLastError ());
236 exit (1);
239 p_file->name = filename;
240 p_file->size = size;
241 p_file->file = file;
242 p_file->file_mapping = file_mapping;
243 p_file->file_base = file_base;
246 /* Close the system structures associated with the given file. */
247 static void
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. */
259 static unsigned long
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 ())
265 case 10:
266 return p_section->SizeOfRawData;
267 default:
268 return p_section->Misc.VirtualSize;
272 /* Flip through the executable and cache the info necessary for dumping. */
273 static void
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;
279 unsigned char *ptr;
280 int i;
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);
286 exit (1);
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",
293 p_infile->name);
294 exit (1);
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;
313 bss_start = ptr;
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;
324 data_start_va = ptr;
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;
333 section++;
338 /* The dump routines. */
340 static void
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);
360 size = data_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);
378 static void
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");
398 index += size;
399 size = bss_size;
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. */
413 void
414 read_in_bss (char *filename)
416 HANDLE file;
417 unsigned long size, index, n_read, total_read;
418 char buffer[512], *bss;
419 int i;
421 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
422 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
423 if (file == INVALID_HANDLE_VALUE)
425 i = GetLastError ();
426 exit (1);
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)
433 i = GetLastError ();
434 exit (1);
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))
442 i = GetLastError ();
443 exit (1);
446 CloseHandle (file);
449 /* Map the heap dumped into the executable file into our address space. */
450 void
451 map_in_heap (char *filename)
453 HANDLE file;
454 HANDLE file_mapping;
455 void *file_base;
456 unsigned long size, upper_size, n_read;
457 int i;
459 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
460 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
461 if (file == INVALID_HANDLE_VALUE)
463 i = GetLastError ();
464 exit (1);
467 size = GetFileSize (file, &upper_size);
468 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY,
469 0, size, NULL);
470 if (!file_mapping)
472 i = GetLastError ();
473 exit (1);
476 size = get_committed_heap_size ();
477 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
478 heap_index_in_executable, size,
479 get_heap_start ());
480 if (file_base != 0)
482 return;
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)
493 i = GetLastError ();
494 exit (1);
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)
501 i = GetLastError ();
502 exit (1);
505 /* Read in the data. */
506 if (!ReadFile (file, get_heap_start (),
507 get_committed_heap_size (), &n_read, NULL))
509 i = GetLastError ();
510 exit (1);
513 CloseHandle (file);