]> code.delx.au - gnu-emacs/blob - src/unexw32.c
(compute_motion): Pass window to Fget_char_property.
[gnu-emacs] / src / unexw32.c
1 /* unexec for GNU Emacs on Windows NT.
2 Copyright (C) 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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.
10
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.
15
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.
20
21 Geoff Voelker (voelker@cs.washington.edu) 8-12-94
22 */
23
24 #include <stdlib.h> /* _fmode */
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <windows.h>
28
29 extern BOOL ctrl_c_handler (unsigned long type);
30
31 #include "ntheap.h"
32
33 /* A convenient type for keeping all the info about a mapped file together. */
34 typedef struct file_data {
35 char *name;
36 unsigned long size;
37 HANDLE file;
38 HANDLE file_mapping;
39 unsigned char *file_base;
40 } file_data;
41
42 /* Basically, our "initialized" flag. */
43 BOOL need_to_recreate_heap = FALSE;
44
45 /* So we can find our heap in the file to recreate it. */
46 unsigned long heap_index_in_executable = 0;
47
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);
51
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);
55
56 /* Cached info about the .data section in the executable. */
57 PUCHAR data_start_va = 0;
58 DWORD data_start_file = 0;
59 DWORD data_size = 0;
60
61 /* Cached info about the .bss section in the executable. */
62 PUCHAR bss_start = 0;
63 DWORD bss_size = 0;
64
65 #ifdef HAVE_NTGUI
66 HINSTANCE hinst = NULL;
67 HINSTANCE hprevinst = NULL;
68 LPSTR lpCmdLine = "";
69 int nCmdShow = 0;
70
71 int __stdcall
72 WinMain (_hinst, _hPrevInst, _lpCmdLine, _nCmdShow)
73 HINSTANCE _hinst;
74 HINSTANCE _hPrevInst;
75 LPSTR _lpCmdLine;
76 int _nCmdShow;
77 {
78 int i, j, new_argc;
79 char **new_argv;
80
81 /* Need to parse command line */
82
83 hinst = _hinst;
84 hprevinst = _hPrevInst;
85 lpCmdLine = _lpCmdLine;
86 nCmdShow = _nCmdShow;
87
88 new_argc = __argc;
89 new_argv = (char **) xmalloc (sizeof (char *) * new_argc);
90 if (!new_argv)
91 return main (__argc, __argv, _environ);
92
93 for (i = j = 0; i < __argc; i++)
94 {
95 /* Allocate a console window for stdout and stderr if requested.
96 We want to allocate as soon as we possibly can to catch
97 debugging output. */
98 if (!strcmp ("-output_console", __argv[i]))
99 {
100 AllocConsole ();
101 new_argc--;
102 continue;
103 }
104 new_argv[j++] = __argv[i];
105 }
106
107 return main (new_argc, new_argv, _environ);
108 }
109 #endif /* HAVE_NTGUI */
110
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 ()). */
115 void
116 _start (void)
117 {
118 #ifdef HAVE_NTGUI
119 extern void WinMainCRTStartup (void);
120 #else
121 extern void mainCRTStartup (void);
122 #endif /* HAVE_NTGUI */
123
124 /* Cache system info, e.g., the NT page size. */
125 cache_system_info ();
126
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
131 won't work.) */
132 if (need_to_recreate_heap)
133 {
134 char executable_path[MAX_PATH];
135
136 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0)
137 {
138 printf ("Failed to find path for executable.\n");
139 exit (1);
140 }
141 recreate_heap (executable_path);
142 need_to_recreate_heap = FALSE;
143 }
144
145 /* The default behavior is to treat files as binary and patch up
146 text files appropriately, in accordance with the MSDOS code. */
147 _fmode = O_BINARY;
148
149 /* This prevents ctrl-c's in shells running while we're suspended from
150 having us exit. */
151 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
152
153 /* Invoke the NT CRT startup routine now that our housecleaning
154 is finished. */
155 #ifdef HAVE_NTGUI
156 WinMainCRTStartup ();
157 #else
158 mainCRTStartup ();
159 #endif /* HAVE_NTGUI */
160 }
161
162 /* Dump out .data and .bss sections into a new executable. */
163 void
164 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
165 void *entry_address)
166 {
167 file_data in_file, out_file;
168 char out_filename[MAX_PATH], in_filename[MAX_PATH];
169 unsigned long size;
170 char *ptr;
171
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");
178
179 strcpy (out_filename, new_name);
180 ptr = out_filename + strlen (out_filename) - 4;
181 if (strcmp (ptr, ".exe"))
182 strcat (out_filename, ".exe");
183
184 printf ("Dumping from %s\n", in_filename);
185 printf (" to %s\n", out_filename);
186
187 /* We need to round off our heap to NT's allocation unit (64KB). */
188 round_heap (get_allocation_unit ());
189
190 /* Open the undumped executable file. */
191 open_input_file (&in_file, in_filename);
192
193 /* Get the interesting section info, like start and size of .bss... */
194 get_section_info (&in_file);
195
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);
202
203 /* Set the flag (before dumping). */
204 need_to_recreate_heap = TRUE;
205
206 copy_executable_and_dump_data_section (&in_file, &out_file);
207 dump_bss_and_heap (&in_file, &out_file);
208
209 close_file_data (&in_file);
210 close_file_data (&out_file);
211 }
212
213
214 /* File handling. */
215
216
217 void
218 open_input_file (file_data *p_file, char *filename)
219 {
220 HANDLE file;
221 HANDLE file_mapping;
222 void *file_base;
223 unsigned long size, upper_size;
224
225 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
226 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
227 if (file == INVALID_HANDLE_VALUE)
228 {
229 printf ("Failed to open %s (%d)...bailing.\n",
230 filename, GetLastError ());
231 exit (1);
232 }
233
234 size = GetFileSize (file, &upper_size);
235 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
236 0, size, NULL);
237 if (!file_mapping)
238 {
239 printf ("Failed to create file mapping of %s (%d)...bailing.\n",
240 filename, GetLastError ());
241 exit (1);
242 }
243
244 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
245 if (file_base == 0)
246 {
247 printf ("Failed to map view of file of %s (%d)...bailing.\n",
248 filename, GetLastError ());
249 exit (1);
250 }
251
252 p_file->name = filename;
253 p_file->size = size;
254 p_file->file = file;
255 p_file->file_mapping = file_mapping;
256 p_file->file_base = file_base;
257 }
258
259 void
260 open_output_file (file_data *p_file, char *filename, unsigned long size)
261 {
262 HANDLE file;
263 HANDLE file_mapping;
264 void *file_base;
265 int i;
266
267 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
268 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
269 if (file == INVALID_HANDLE_VALUE)
270 {
271 i = GetLastError ();
272 printf ("open_output_file: Failed to open %s (%d).\n",
273 filename, i);
274 exit (1);
275 }
276
277 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
278 0, size, NULL);
279 if (!file_mapping)
280 {
281 i = GetLastError ();
282 printf ("open_output_file: Failed to create file mapping of %s (%d).\n",
283 filename, i);
284 exit (1);
285 }
286
287 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
288 if (file_base == 0)
289 {
290 i = GetLastError ();
291 printf ("open_output_file: Failed to map view of file of %s (%d).\n",
292 filename, i);
293 exit (1);
294 }
295
296 p_file->name = filename;
297 p_file->size = size;
298 p_file->file = file;
299 p_file->file_mapping = file_mapping;
300 p_file->file_base = file_base;
301 }
302
303 /* Close the system structures associated with the given file. */
304 static void
305 close_file_data (file_data *p_file)
306 {
307 UnmapViewOfFile (p_file->file_base);
308 CloseHandle (p_file->file_mapping);
309 CloseHandle (p_file->file);
310 }
311
312
313 /* Routines to manipulate NT executable file sections. */
314
315 static void
316 get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start,
317 DWORD *p_bss_size)
318 {
319 int n, start, len;
320 char map_filename[MAX_PATH];
321 char buffer[256];
322 FILE *map;
323
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");
329
330 map = fopen (map_filename, "r");
331 if (!map)
332 {
333 printf ("Failed to open map file %s, error %d...bailing out.\n",
334 map_filename, GetLastError ());
335 exit (-1);
336 }
337
338 while (fgets (buffer, sizeof (buffer), map))
339 {
340 if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA")))
341 continue;
342 n = sscanf (buffer, " %*d:%x %x", &start, &len);
343 if (n != 2)
344 {
345 printf ("Failed to scan the .bss section line:\n%s", buffer);
346 exit (-1);
347 }
348 break;
349 }
350 *p_bss_start = (PUCHAR) start;
351 *p_bss_size = (DWORD) len;
352 }
353
354 static unsigned long
355 get_section_size (PIMAGE_SECTION_HEADER p_section)
356 {
357 /* The section size is in different locations in the different versions. */
358 switch (get_nt_minor_version ())
359 {
360 case 10:
361 return p_section->SizeOfRawData;
362 default:
363 return p_section->Misc.VirtualSize;
364 }
365 }
366
367 /* Flip through the executable and cache the info necessary for dumping. */
368 static void
369 get_section_info (file_data *p_infile)
370 {
371 PIMAGE_DOS_HEADER dos_header;
372 PIMAGE_NT_HEADERS nt_header;
373 PIMAGE_SECTION_HEADER section, data_section;
374 unsigned char *ptr;
375 int i;
376
377 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
378 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
379 {
380 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
381 exit (1);
382 }
383 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
384 dos_header->e_lfanew);
385 if (nt_header == NULL)
386 {
387 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
388 p_infile->name);
389 exit (1);
390 }
391
392 /* Check the NT header signature ... */
393 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
394 {
395 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
396 nt_header->Signature, p_infile->name);
397 }
398
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++)
402 {
403 if (!strcmp (section->Name, ".bss"))
404 {
405 /* The .bss section. */
406 ptr = (char *) nt_header->OptionalHeader.ImageBase +
407 section->VirtualAddress;
408 bss_start = ptr;
409 bss_size = get_section_size (section);
410 }
411 if (!strcmp (section->Name, ".data"))
412 {
413 /* From lastfile.c */
414 extern char my_edata[];
415
416 /* The .data section. */
417 data_section = section;
418 ptr = (char *) nt_header->OptionalHeader.ImageBase +
419 section->VirtualAddress;
420 data_start_va = ptr;
421 data_start_file = section->PointerToRawData;
422
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;
428 }
429 section++;
430 }
431
432 if (!bss_start && !bss_size)
433 {
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;
445 }
446 }
447
448
449 /* The dump routines. */
450
451 static void
452 copy_executable_and_dump_data_section (file_data *p_infile,
453 file_data *p_outfile)
454 {
455 unsigned char *data_file, *data_va;
456 unsigned long size, index;
457
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;
460
461 /* Get a pointer to the raw data in our address space. */
462 data_va = data_start_va;
463
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);
470
471 size = data_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);
478
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);
487 }
488
489 static void
490 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
491 {
492 unsigned char *heap_data, *bss_data;
493 unsigned long size, index;
494
495 printf ("Dumping heap into executable...\n");
496
497 index = heap_index_in_executable;
498 size = get_committed_heap_size ();
499 heap_data = get_heap_start ();
500
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);
504
505 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
506
507 printf ("Dumping .bss into executable...\n");
508
509 index += size;
510 size = bss_size;
511 bss_data = bss_start;
512
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);
517 }
518
519
520 /* Reload and remap routines. */
521
522
523 /* Load the dumped .bss section into the .bss area of our address space. */
524 void
525 read_in_bss (char *filename)
526 {
527 HANDLE file;
528 unsigned long size, index, n_read, total_read;
529 char buffer[512], *bss;
530 int i;
531
532 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
533 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
534 if (file == INVALID_HANDLE_VALUE)
535 {
536 i = GetLastError ();
537 exit (1);
538 }
539
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)
543 {
544 i = GetLastError ();
545 exit (1);
546 }
547
548
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))
552 {
553 i = GetLastError ();
554 exit (1);
555 }
556
557 CloseHandle (file);
558 }
559
560 /* Map the heap dumped into the executable file into our address space. */
561 void
562 map_in_heap (char *filename)
563 {
564 HANDLE file;
565 HANDLE file_mapping;
566 void *file_base;
567 unsigned long size, upper_size, n_read;
568 int i;
569
570 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
571 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
572 if (file == INVALID_HANDLE_VALUE)
573 {
574 i = GetLastError ();
575 exit (1);
576 }
577
578 size = GetFileSize (file, &upper_size);
579 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY,
580 0, size, NULL);
581 if (!file_mapping)
582 {
583 i = GetLastError ();
584 exit (1);
585 }
586
587 size = get_committed_heap_size ();
588 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
589 heap_index_in_executable, size,
590 get_heap_start ());
591 if (file_base != 0)
592 {
593 return;
594 }
595
596 /* If we don't succeed with the mapping, then copy from the
597 data into the heap. */
598
599 CloseHandle (file_mapping);
600
601 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
602 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL)
603 {
604 i = GetLastError ();
605 exit (1);
606 }
607
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)
611 {
612 i = GetLastError ();
613 exit (1);
614 }
615
616 /* Read in the data. */
617 if (!ReadFile (file, get_heap_start (),
618 get_committed_heap_size (), &n_read, NULL))
619 {
620 i = GetLastError ();
621 exit (1);
622 }
623
624 CloseHandle (file);
625 }