annotate src/unexw32.c @ 12424:aa25c9b2e043

Bind f1 and help like C-h.
author Richard M. Stallman <rms@gnu.org>
date Thu, 29 Jun 1995 02:50:52 +0000
parents e8a6dfd8d5d2
children 3f4da17a7cd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
1 /*
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
2 unexec for GNU Emacs on Windows NT.
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
3
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
4 Copyright (C) 1994 Free Software Foundation, Inc.
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
5
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
6 This file is part of GNU Emacs.
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
7
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
8 GNU Emacs is free software; you can redistribute it and/or modify it
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
10 Free Software Foundation; either version 2, or (at your option) any later
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
11 version.
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
12
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
13 GNU Emacs is distributed in the hope that it will be useful, but WITHOUT
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
16 more details.
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
17
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
19 with GNU Emacs; see the file COPYING. If not, write to the Free Software
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
21
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
22 Geoff Voelker (voelker@cs.washington.edu) 8-12-94
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
23 */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
24
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
25 #include <stdlib.h> /* _fmode */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
26 #include <stdio.h>
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
27 #include <fcntl.h>
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
28 #include <windows.h>
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
29
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
30 extern BOOL ctrl_c_handler (unsigned long type);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
31
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
32 #include "ntheap.h"
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
33
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
34 /* A convenient type for keeping all the info about a mapped file together. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
35 typedef struct file_data {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
36 char *name;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
37 unsigned long size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
38 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
39 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
40 unsigned char *file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
41 } file_data;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
42
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
43 /* Basically, our "initialized" flag. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
44 BOOL need_to_recreate_heap = FALSE;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
45
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
46 /* So we can find our heap in the file to recreate it. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
47 unsigned long heap_index_in_executable = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
48
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
49 void open_input_file (file_data *p_file, char *name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
50 void open_output_file (file_data *p_file, char *name, unsigned long size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
51 void close_file_data (file_data *p_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
52
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
53 void get_section_info (file_data *p_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
54 void copy_executable_and_dump_data_section (file_data *, file_data *);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
55 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
56
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
57 /* Cached info about the .data section in the executable. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
58 PUCHAR data_start_va = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
59 DWORD data_start_file = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
60 DWORD data_size = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
61
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
62 /* Cached info about the .bss section in the executable. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
63 PUCHAR bss_start = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
64 DWORD bss_size = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
65
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
66 /* Startup code for running on NT. When we are running as the dumped
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
67 version, we need to bootstrap our heap and .bss section into our
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
68 address space before we can actually hand off control to the startup
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
69 code supplied by NT (primarily because that code relies upon malloc ()). */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
70 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
71 _start (void)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
72 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
73 extern void mainCRTStartup (void);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
74
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
75 /* Cache system info, e.g., the NT page size. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
76 cache_system_info ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
77
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
78 /* If we're a dumped version of emacs then we need to recreate
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
79 our heap and play tricks with our .bss section. Do this before
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
80 start up. (WARNING: Do not put any code before this section
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
81 that relies upon malloc () and runs in the dumped version. It
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
82 won't work.) */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
83 if (need_to_recreate_heap)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
84 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
85 char executable_path[MAX_PATH];
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
86
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
87 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
88 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
89 printf ("Failed to find path for executable.\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
90 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
91 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
92 recreate_heap (executable_path);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
93 need_to_recreate_heap = FALSE;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
94 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
95
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
96 /* The default behavior is to treat files as binary and patch up
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
97 text files appropriately, in accordance with the MSDOS code. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
98 _fmode = O_BINARY;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
99
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
100 /* This prevents ctrl-c's in shells running while we're suspended from
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
101 having us exit. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
102 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
103
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
104 /* Invoke the NT CRT startup routine now that our housecleaning
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
105 is finished. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
106 mainCRTStartup ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
107 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
108
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
109 /* Dump out .data and .bss sections into a new exectubale. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
110 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
111 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
112 void *entry_address)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
113 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
114 file_data in_file, out_file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
115 char out_filename[MAX_PATH], in_filename[MAX_PATH];
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
116 unsigned long size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
117 char *ptr;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
118
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
119 /* Make sure that the input and output filenames have the
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
120 ".exe" extension...patch them up if they don't. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
121 strcpy (in_filename, old_name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
122 ptr = in_filename + strlen (in_filename) - 4;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
123 if (strcmp (ptr, ".exe"))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
124 strcat (in_filename, ".exe");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
125
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
126 strcpy (out_filename, new_name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
127 ptr = out_filename + strlen (out_filename) - 4;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
128 if (strcmp (ptr, ".exe"))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
129 strcat (out_filename, ".exe");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
130
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
131 printf ("Dumping from %s\n", in_filename);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
132 printf (" to %s\n", out_filename);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
133
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
134 /* We need to round off our heap to NT's allocation unit (64KB). */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
135 round_heap (get_allocation_unit ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
136
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
137 /* Open the undumped executable file. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
138 open_input_file (&in_file, in_filename);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
139
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
140 /* Get the interesting section info, like start and size of .bss... */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
141 get_section_info (&in_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
142
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
143 /* The size of the dumped executable is the size of the original
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
144 executable plus the size of the heap and the size of the .bss section. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
145 heap_index_in_executable = round_to_next (in_file.size,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
146 get_allocation_unit ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
147 size = heap_index_in_executable + get_committed_heap_size () + bss_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
148 open_output_file (&out_file, out_filename, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
149
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
150 /* Set the flag (before dumping). */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
151 need_to_recreate_heap = TRUE;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
152
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
153 copy_executable_and_dump_data_section (&in_file, &out_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
154 dump_bss_and_heap (&in_file, &out_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
155
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
156 close_file_data (&in_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
157 close_file_data (&out_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
158 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
159
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
160
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
161 /* File handling. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
162
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
163
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
164 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
165 open_input_file (file_data *p_file, char *filename)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
166 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
167 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
168 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
169 void *file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
170 unsigned long size, upper_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
171
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
172 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
173 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
174 if (file == INVALID_HANDLE_VALUE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
175 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
176 printf ("Failed to open %s (%d)...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
177 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
178 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
179 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
180
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
181 size = GetFileSize (file, &upper_size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
182 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
183 0, size, NULL);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
184 if (!file_mapping)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
185 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
186 printf ("Failed to create file mapping of %s (%d)...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
187 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
188 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
189 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
190
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
191 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
192 if (file_base == 0)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
193 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
194 printf ("Failed to map view of file of %s (%d)...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
195 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
196 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
197 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
198
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
199 p_file->name = filename;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
200 p_file->size = size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
201 p_file->file = file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
202 p_file->file_mapping = file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
203 p_file->file_base = file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
204 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
205
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
206 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
207 open_output_file (file_data *p_file, char *filename, unsigned long size)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
208 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
209 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
210 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
211 void *file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
212
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
213 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
214 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
215 if (file == INVALID_HANDLE_VALUE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
216 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
217 printf ("open_output_file: Failed to open %s (%d).\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
218 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
219 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
220 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
221
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
222 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
223 0, size, NULL);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
224 if (!file_mapping)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
225 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
226 printf ("open_output_file: Failed to create file mapping of %s (%d).\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
227 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
228 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
229 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
230
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
231 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
232 if (file_base == 0)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
233 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
234 printf ("open_output_file: Failed to map view of file of %s (%d).\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
235 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
236 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
237 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
238
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
239 p_file->name = filename;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
240 p_file->size = size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
241 p_file->file = file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
242 p_file->file_mapping = file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
243 p_file->file_base = file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
244 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
246 /* Close the system structures associated with the given file. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
247 static void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
248 close_file_data (file_data *p_file)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
249 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
250 UnmapViewOfFile (p_file->file_base);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
251 CloseHandle (p_file->file_mapping);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
252 CloseHandle (p_file->file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
253 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
254
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
255
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
256 /* Routines to manipulate NT executable file sections. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
257
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
258
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
259 static unsigned long
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
260 get_section_size (PIMAGE_SECTION_HEADER p_section)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
261 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
262 /* The section size is in different locations in the different versions. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
263 switch (get_nt_minor_version ())
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
264 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
265 case 10:
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
266 return p_section->SizeOfRawData;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
267 default:
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
268 return p_section->Misc.VirtualSize;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
269 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
270 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
271
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
272 /* Flip through the executable and cache the info necessary for dumping. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
273 static void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
274 get_section_info (file_data *p_infile)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
275 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
276 PIMAGE_DOS_HEADER dos_header;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
277 PIMAGE_NT_HEADERS nt_header;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
278 PIMAGE_SECTION_HEADER section;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
279 unsigned char *ptr;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
280 int i;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
281
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
282 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
283 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
284 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
285 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
286 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
287 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
288 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
289 dos_header->e_lfanew);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
290 if (nt_header == NULL)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
291 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
292 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
293 p_infile->name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
294 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
295 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
296
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
297 /* Check the NT header signature ... */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
298 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
299 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
300 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
301 nt_header->Signature, p_infile->name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
302 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
303
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
304 /* Flip through the sections for .data and .bss ... */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
305 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
306 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
307 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
308 if (!strcmp (section->Name, ".bss"))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
309 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
310 /* The .bss section. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
311 ptr = (char *) nt_header->OptionalHeader.ImageBase +
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
312 section->VirtualAddress;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
313 bss_start = ptr;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
314 bss_size = get_section_size (section);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
315 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
316 if (!strcmp (section->Name, ".data"))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
317 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
318 /* The .data section. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
319 ptr = (char *) nt_header->OptionalHeader.ImageBase +
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
320 section->VirtualAddress;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
321 data_start_va = ptr;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
322 data_start_file = section->PointerToRawData;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
323 data_size = get_section_size (section);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
324 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
325 section++;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
326 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
327 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
328
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
329
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
330 /* The dump routines. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
331
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
332 static void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
333 copy_executable_and_dump_data_section (file_data *p_infile,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
334 file_data *p_outfile)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
335 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
336 unsigned char *data_file, *data_va;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
337 unsigned long size, index;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
338
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
339 /* Get a pointer to where the raw data should go in the executable file. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
340 data_file = (char *) p_outfile->file_base + data_start_file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
341
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
342 /* Get a pointer to the raw data in our address space. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
343 data_va = data_start_va;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
344
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
345 size = (DWORD) data_file - (DWORD) p_outfile->file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
346 printf ("Copying executable up to data section...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
347 printf ("\t0x%08x Offset in input file.\n", 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
348 printf ("\t0x%08x Offset in output file.\n", 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
349 printf ("\t0x%08x Size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
350 memcpy (p_outfile->file_base, p_infile->file_base, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
351
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
352 size = data_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
353 printf ("Dumping .data section...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
354 printf ("\t0x%08x Address in process.\n", data_va);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
355 printf ("\t0x%08x Offset in output file.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
356 data_file - p_outfile->file_base);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
357 printf ("\t0x%08x Size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
358 memcpy (data_file, data_va, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
359
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
360 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
361 size = p_infile->size - index;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
362 printf ("Copying rest of executable...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
363 printf ("\t0x%08x Offset in input file.\n", index);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
364 printf ("\t0x%08x Offset in output file.\n", index);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
365 printf ("\t0x%08x Size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
366 memcpy ((char *) p_outfile->file_base + index,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
367 (char *) p_infile->file_base + index, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
368 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
369
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
370 static void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
371 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
372 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
373 unsigned char *heap_data, *bss_data;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
374 unsigned long size, index;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
375
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
376 printf ("Dumping heap into executable...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
377
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
378 index = heap_index_in_executable;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
379 size = get_committed_heap_size ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
380 heap_data = get_heap_start ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
381
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
382 printf ("\t0x%08x Heap start in process.\n", heap_data);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
383 printf ("\t0x%08x Heap offset in executable.\n", index);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
384 printf ("\t0x%08x Heap size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
385
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
386 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
387
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
388 printf ("Dumping .bss into executable...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
389
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
390 index += size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
391 size = bss_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
392 bss_data = bss_start;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
393
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
394 printf ("\t0x%08x BSS start in process.\n", bss_data);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
395 printf ("\t0x%08x BSS offset in executable.\n", index);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
396 printf ("\t0x%08x BSS size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
397 memcpy ((char *) p_outfile->file_base + index, bss_data, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
398 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
399
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
400
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
401 /* Reload and remap routines. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
402
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
403
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
404 /* Load the dumped .bss section into the .bss area of our address space. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
405 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
406 read_in_bss (char *filename)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
407 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
408 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
409 unsigned long size, index, n_read, total_read;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
410 char buffer[512], *bss;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
411 int i;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
412
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
413 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
414 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
415 if (file == INVALID_HANDLE_VALUE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
416 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
417 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
418 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
419 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
420
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
421 /* Seek to where the .bss section is tucked away after the heap... */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
422 index = heap_index_in_executable + get_committed_heap_size ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
423 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
424 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
425 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
426 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
427 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
428
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
429
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
430 /* Ok, read in the saved .bss section and initialize all
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
431 uninitialized variables. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
432 total_read = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
433 size = bss_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
434 bss = bss_start;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
435 while (ReadFile (file, buffer, 512, &n_read, NULL))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
436 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
437 if (n_read == 0)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
438 break;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
439 memcpy (bss, buffer, n_read);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
440 bss += n_read;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
441 total_read += n_read;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
442 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
443
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
444 CloseHandle (file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
445 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
446
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
447 /* Map the heap dumped into the executable file into our address space. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
448 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
449 map_in_heap (char *filename)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
450 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
451 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
452 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
453 void *file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
454 unsigned long size, upper_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
455 int i;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
456
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
457 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
458 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
459 if (file == INVALID_HANDLE_VALUE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
460 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
461 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
462 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
463 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
464
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
465 size = GetFileSize (file, &upper_size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
466 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
467 0, size, NULL);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
468 if (!file_mapping)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
469 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
470 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
471 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
472 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
473
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
474 size = get_committed_heap_size ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
475 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
476 heap_index_in_executable, size,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
477 get_heap_start ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
478 if (file_base == 0)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
479 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
480 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
481 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
482 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
483 }