Mercurial > emacs
annotate src/unexw32.c @ 13896:d23e1ac7b432
Doc fixes.
(ff-get-file): Use get-file-buffer to avoid false match.
(ff-switch-to-buffer): Rename arg FILE to BUFFER-OR-NAME.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 31 Dec 1995 00:10:18 +0000 |
parents | 8d30151f4acb |
children | 621a575db6f7 |
rev | line source |
---|---|
12245 | 1 /* |
2 unexec for GNU Emacs on Windows NT. | |
3 | |
4 Copyright (C) 1994 Free Software Foundation, Inc. | |
5 | |
6 This file is part of GNU Emacs. | |
7 | |
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. | |
12 | |
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. | |
17 | |
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. | |
21 | |
22 Geoff Voelker (voelker@cs.washington.edu) 8-12-94 | |
23 */ | |
24 | |
25 #include <stdlib.h> /* _fmode */ | |
26 #include <stdio.h> | |
27 #include <fcntl.h> | |
28 #include <windows.h> | |
29 | |
30 extern BOOL ctrl_c_handler (unsigned long type); | |
31 | |
32 #include "ntheap.h" | |
33 | |
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; | |
42 | |
43 /* Basically, our "initialized" flag. */ | |
44 BOOL need_to_recreate_heap = FALSE; | |
45 | |
46 /* So we can find our heap in the file to recreate it. */ | |
47 unsigned long heap_index_in_executable = 0; | |
48 | |
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); | |
52 | |
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); | |
56 | |
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; | |
61 | |
62 /* Cached info about the .bss section in the executable. */ | |
63 PUCHAR bss_start = 0; | |
64 DWORD bss_size = 0; | |
65 | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
66 #ifdef HAVE_NTGUI |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
67 HINSTANCE hinst = NULL; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
68 HINSTANCE hprevinst = NULL; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
69 LPSTR lpCmdLine = ""; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
70 int nCmdShow = 0; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
71 |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
72 int __stdcall |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
73 WinMain (_hinst, _hPrevInst, _lpCmdLine, _nCmdShow) |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
74 HINSTANCE _hinst; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
75 HINSTANCE _hPrevInst; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
76 LPSTR _lpCmdLine; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
77 int _nCmdShow; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
78 { |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
79 /* Need to parse command line */ |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
80 |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
81 hinst = _hinst; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
82 hprevinst = _hPrevInst; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
83 lpCmdLine = _lpCmdLine; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
84 nCmdShow = _nCmdShow; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
85 |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
86 return (main (__argc,__argv,_environ)); |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
87 } |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
88 #endif /* HAVE_NTGUI */ |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
89 |
12245 | 90 /* Startup code for running on NT. When we are running as the dumped |
91 version, we need to bootstrap our heap and .bss section into our | |
92 address space before we can actually hand off control to the startup | |
93 code supplied by NT (primarily because that code relies upon malloc ()). */ | |
94 void | |
95 _start (void) | |
96 { | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
97 #ifdef HAVE_NTGUI |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
98 extern void WinMainCRTStartup (void); |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
99 #else |
12245 | 100 extern void mainCRTStartup (void); |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
101 #endif /* HAVE_NTGUI */ |
12245 | 102 |
103 /* Cache system info, e.g., the NT page size. */ | |
104 cache_system_info (); | |
105 | |
106 /* If we're a dumped version of emacs then we need to recreate | |
107 our heap and play tricks with our .bss section. Do this before | |
108 start up. (WARNING: Do not put any code before this section | |
109 that relies upon malloc () and runs in the dumped version. It | |
110 won't work.) */ | |
111 if (need_to_recreate_heap) | |
112 { | |
113 char executable_path[MAX_PATH]; | |
114 | |
115 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0) | |
116 { | |
117 printf ("Failed to find path for executable.\n"); | |
118 exit (1); | |
119 } | |
120 recreate_heap (executable_path); | |
121 need_to_recreate_heap = FALSE; | |
122 } | |
123 | |
124 /* The default behavior is to treat files as binary and patch up | |
125 text files appropriately, in accordance with the MSDOS code. */ | |
126 _fmode = O_BINARY; | |
127 | |
128 /* This prevents ctrl-c's in shells running while we're suspended from | |
129 having us exit. */ | |
130 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE); | |
131 | |
132 /* Invoke the NT CRT startup routine now that our housecleaning | |
133 is finished. */ | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
134 #ifdef HAVE_NTGUI |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
135 WinMainCRTStartup (); |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
136 #else |
12245 | 137 mainCRTStartup (); |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
138 #endif /* HAVE_NTGUI */ |
12245 | 139 } |
140 | |
141 /* Dump out .data and .bss sections into a new exectubale. */ | |
142 void | |
143 unexec (char *new_name, char *old_name, void *start_data, void *start_bss, | |
144 void *entry_address) | |
145 { | |
146 file_data in_file, out_file; | |
147 char out_filename[MAX_PATH], in_filename[MAX_PATH]; | |
148 unsigned long size; | |
149 char *ptr; | |
150 | |
151 /* Make sure that the input and output filenames have the | |
152 ".exe" extension...patch them up if they don't. */ | |
153 strcpy (in_filename, old_name); | |
154 ptr = in_filename + strlen (in_filename) - 4; | |
155 if (strcmp (ptr, ".exe")) | |
156 strcat (in_filename, ".exe"); | |
157 | |
158 strcpy (out_filename, new_name); | |
159 ptr = out_filename + strlen (out_filename) - 4; | |
160 if (strcmp (ptr, ".exe")) | |
161 strcat (out_filename, ".exe"); | |
162 | |
163 printf ("Dumping from %s\n", in_filename); | |
164 printf (" to %s\n", out_filename); | |
165 | |
166 /* We need to round off our heap to NT's allocation unit (64KB). */ | |
167 round_heap (get_allocation_unit ()); | |
168 | |
169 /* Open the undumped executable file. */ | |
170 open_input_file (&in_file, in_filename); | |
171 | |
172 /* Get the interesting section info, like start and size of .bss... */ | |
173 get_section_info (&in_file); | |
174 | |
175 /* The size of the dumped executable is the size of the original | |
176 executable plus the size of the heap and the size of the .bss section. */ | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
177 heap_index_in_executable = (unsigned long) |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
178 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ()); |
12245 | 179 size = heap_index_in_executable + get_committed_heap_size () + bss_size; |
180 open_output_file (&out_file, out_filename, size); | |
181 | |
182 /* Set the flag (before dumping). */ | |
183 need_to_recreate_heap = TRUE; | |
184 | |
185 copy_executable_and_dump_data_section (&in_file, &out_file); | |
186 dump_bss_and_heap (&in_file, &out_file); | |
187 | |
188 close_file_data (&in_file); | |
189 close_file_data (&out_file); | |
190 } | |
191 | |
192 | |
193 /* File handling. */ | |
194 | |
195 | |
196 void | |
197 open_input_file (file_data *p_file, char *filename) | |
198 { | |
199 HANDLE file; | |
200 HANDLE file_mapping; | |
201 void *file_base; | |
202 unsigned long size, upper_size; | |
203 | |
204 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
205 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
206 if (file == INVALID_HANDLE_VALUE) | |
207 { | |
208 printf ("Failed to open %s (%d)...bailing.\n", | |
209 filename, GetLastError ()); | |
210 exit (1); | |
211 } | |
212 | |
213 size = GetFileSize (file, &upper_size); | |
214 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY, | |
215 0, size, NULL); | |
216 if (!file_mapping) | |
217 { | |
218 printf ("Failed to create file mapping of %s (%d)...bailing.\n", | |
219 filename, GetLastError ()); | |
220 exit (1); | |
221 } | |
222 | |
223 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size); | |
224 if (file_base == 0) | |
225 { | |
226 printf ("Failed to map view of file of %s (%d)...bailing.\n", | |
227 filename, GetLastError ()); | |
228 exit (1); | |
229 } | |
230 | |
231 p_file->name = filename; | |
232 p_file->size = size; | |
233 p_file->file = file; | |
234 p_file->file_mapping = file_mapping; | |
235 p_file->file_base = file_base; | |
236 } | |
237 | |
238 void | |
239 open_output_file (file_data *p_file, char *filename, unsigned long size) | |
240 { | |
241 HANDLE file; | |
242 HANDLE file_mapping; | |
243 void *file_base; | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
244 int i; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
245 |
12245 | 246 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
247 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); | |
248 if (file == INVALID_HANDLE_VALUE) | |
249 { | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
250 i = GetLastError (); |
12245 | 251 printf ("open_output_file: Failed to open %s (%d).\n", |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
252 filename, i); |
12245 | 253 exit (1); |
254 } | |
255 | |
256 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE, | |
257 0, size, NULL); | |
258 if (!file_mapping) | |
259 { | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
260 i = GetLastError (); |
12245 | 261 printf ("open_output_file: Failed to create file mapping of %s (%d).\n", |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
262 filename, i); |
12245 | 263 exit (1); |
264 } | |
265 | |
266 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size); | |
267 if (file_base == 0) | |
268 { | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
269 i = GetLastError (); |
12245 | 270 printf ("open_output_file: Failed to map view of file of %s (%d).\n", |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
271 filename, i); |
12245 | 272 exit (1); |
273 } | |
274 | |
275 p_file->name = filename; | |
276 p_file->size = size; | |
277 p_file->file = file; | |
278 p_file->file_mapping = file_mapping; | |
279 p_file->file_base = file_base; | |
280 } | |
281 | |
282 /* Close the system structures associated with the given file. */ | |
283 static void | |
284 close_file_data (file_data *p_file) | |
285 { | |
286 UnmapViewOfFile (p_file->file_base); | |
287 CloseHandle (p_file->file_mapping); | |
288 CloseHandle (p_file->file); | |
289 } | |
290 | |
291 | |
292 /* Routines to manipulate NT executable file sections. */ | |
293 | |
13830
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
294 static void |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
295 get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start, |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
296 DWORD *p_bss_size) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
297 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
298 int n, start, len; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
299 char map_filename[MAX_PATH]; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
300 char buffer[256]; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
301 FILE *map; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
302 |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
303 /* Overwrite the .exe extension on the executable file name with |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
304 the .map extension. */ |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
305 strcpy (map_filename, p_infile->name); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
306 n = strlen (map_filename) - 3; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
307 strcpy (&map_filename[n], "map"); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
308 |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
309 map = fopen (map_filename, "r"); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
310 if (!map) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
311 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
312 printf ("Failed to open map file %s, error %d...bailing out.\n", |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
313 map_filename, GetLastError ()); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
314 exit (-1); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
315 } |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
316 |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
317 while (fgets (buffer, sizeof (buffer), map)) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
318 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
319 if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA"))) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
320 continue; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
321 n = sscanf (buffer, " %*d:%x %x", &start, &len); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
322 if (n != 2) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
323 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
324 printf ("Failed to scan the .bss section line:\n%s", buffer); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
325 exit (-1); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
326 } |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
327 break; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
328 } |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
329 *p_bss_start = (PUCHAR) start; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
330 *p_bss_size = (DWORD) len; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
331 } |
12245 | 332 |
333 static unsigned long | |
334 get_section_size (PIMAGE_SECTION_HEADER p_section) | |
335 { | |
336 /* The section size is in different locations in the different versions. */ | |
337 switch (get_nt_minor_version ()) | |
338 { | |
339 case 10: | |
340 return p_section->SizeOfRawData; | |
341 default: | |
342 return p_section->Misc.VirtualSize; | |
343 } | |
344 } | |
345 | |
346 /* Flip through the executable and cache the info necessary for dumping. */ | |
347 static void | |
348 get_section_info (file_data *p_infile) | |
349 { | |
350 PIMAGE_DOS_HEADER dos_header; | |
351 PIMAGE_NT_HEADERS nt_header; | |
13830
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
352 PIMAGE_SECTION_HEADER section, data_section; |
12245 | 353 unsigned char *ptr; |
354 int i; | |
355 | |
356 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base; | |
357 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) | |
358 { | |
359 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name); | |
360 exit (1); | |
361 } | |
362 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + | |
363 dos_header->e_lfanew); | |
364 if (nt_header == NULL) | |
365 { | |
366 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n", | |
367 p_infile->name); | |
368 exit (1); | |
369 } | |
370 | |
371 /* Check the NT header signature ... */ | |
372 if (nt_header->Signature != IMAGE_NT_SIGNATURE) | |
373 { | |
374 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n", | |
375 nt_header->Signature, p_infile->name); | |
376 } | |
377 | |
378 /* Flip through the sections for .data and .bss ... */ | |
379 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header); | |
380 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) | |
381 { | |
382 if (!strcmp (section->Name, ".bss")) | |
383 { | |
384 /* The .bss section. */ | |
385 ptr = (char *) nt_header->OptionalHeader.ImageBase + | |
386 section->VirtualAddress; | |
387 bss_start = ptr; | |
388 bss_size = get_section_size (section); | |
389 } | |
390 if (!strcmp (section->Name, ".data")) | |
391 { | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
392 /* From lastfile.c */ |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
393 extern char my_edata[]; |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
394 |
12245 | 395 /* The .data section. */ |
13830
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
396 data_section = section; |
12245 | 397 ptr = (char *) nt_header->OptionalHeader.ImageBase + |
398 section->VirtualAddress; | |
399 data_start_va = ptr; | |
400 data_start_file = section->PointerToRawData; | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
401 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
402 /* We want to only write Emacs data back to the executable, |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
403 not any of the library data (if library data is included, |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
404 then a dumped Emacs won't run on system versions other |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
405 than the one Emacs was dumped on). */ |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
406 data_size = my_edata - data_start_va; |
12245 | 407 } |
408 section++; | |
409 } | |
13830
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
410 |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
411 if (!bss_start && !bss_size) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
412 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
413 /* Starting with MSVC 4.0, the .bss section has been eliminated |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
414 and appended virtually to the end of the .data section. Our |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
415 only hint about where the .bss section starts in the address |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
416 comes from the SizeOfRawData field in the .data section |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
417 header. Unfortunately, this field is only approximate, as it |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
418 is a rounded number and is typically rounded just beyond the |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
419 start of the .bss section. To find the start and size of the |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
420 .bss section exactly, we have to peek into the map file. */ |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
421 get_bss_info_from_map_file (p_infile, &ptr, &bss_size); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
422 bss_start = ptr + nt_header->OptionalHeader.ImageBase |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
423 + data_section->VirtualAddress; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
424 } |
12245 | 425 } |
426 | |
427 | |
428 /* The dump routines. */ | |
429 | |
430 static void | |
431 copy_executable_and_dump_data_section (file_data *p_infile, | |
432 file_data *p_outfile) | |
433 { | |
434 unsigned char *data_file, *data_va; | |
435 unsigned long size, index; | |
436 | |
437 /* Get a pointer to where the raw data should go in the executable file. */ | |
438 data_file = (char *) p_outfile->file_base + data_start_file; | |
439 | |
440 /* Get a pointer to the raw data in our address space. */ | |
441 data_va = data_start_va; | |
442 | |
443 size = (DWORD) data_file - (DWORD) p_outfile->file_base; | |
444 printf ("Copying executable up to data section...\n"); | |
445 printf ("\t0x%08x Offset in input file.\n", 0); | |
446 printf ("\t0x%08x Offset in output file.\n", 0); | |
447 printf ("\t0x%08x Size in bytes.\n", size); | |
448 memcpy (p_outfile->file_base, p_infile->file_base, size); | |
449 | |
450 size = data_size; | |
451 printf ("Dumping .data section...\n"); | |
452 printf ("\t0x%08x Address in process.\n", data_va); | |
453 printf ("\t0x%08x Offset in output file.\n", | |
454 data_file - p_outfile->file_base); | |
455 printf ("\t0x%08x Size in bytes.\n", size); | |
456 memcpy (data_file, data_va, size); | |
457 | |
458 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base; | |
459 size = p_infile->size - index; | |
460 printf ("Copying rest of executable...\n"); | |
461 printf ("\t0x%08x Offset in input file.\n", index); | |
462 printf ("\t0x%08x Offset in output file.\n", index); | |
463 printf ("\t0x%08x Size in bytes.\n", size); | |
464 memcpy ((char *) p_outfile->file_base + index, | |
465 (char *) p_infile->file_base + index, size); | |
466 } | |
467 | |
468 static void | |
469 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile) | |
470 { | |
471 unsigned char *heap_data, *bss_data; | |
472 unsigned long size, index; | |
473 | |
474 printf ("Dumping heap into executable...\n"); | |
475 | |
476 index = heap_index_in_executable; | |
477 size = get_committed_heap_size (); | |
478 heap_data = get_heap_start (); | |
479 | |
480 printf ("\t0x%08x Heap start in process.\n", heap_data); | |
481 printf ("\t0x%08x Heap offset in executable.\n", index); | |
482 printf ("\t0x%08x Heap size in bytes.\n", size); | |
483 | |
484 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size); | |
485 | |
486 printf ("Dumping .bss into executable...\n"); | |
487 | |
488 index += size; | |
489 size = bss_size; | |
490 bss_data = bss_start; | |
491 | |
492 printf ("\t0x%08x BSS start in process.\n", bss_data); | |
493 printf ("\t0x%08x BSS offset in executable.\n", index); | |
494 printf ("\t0x%08x BSS size in bytes.\n", size); | |
495 memcpy ((char *) p_outfile->file_base + index, bss_data, size); | |
496 } | |
497 | |
498 | |
499 /* Reload and remap routines. */ | |
500 | |
501 | |
502 /* Load the dumped .bss section into the .bss area of our address space. */ | |
503 void | |
504 read_in_bss (char *filename) | |
505 { | |
506 HANDLE file; | |
507 unsigned long size, index, n_read, total_read; | |
508 char buffer[512], *bss; | |
509 int i; | |
510 | |
511 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
512 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
513 if (file == INVALID_HANDLE_VALUE) | |
514 { | |
515 i = GetLastError (); | |
516 exit (1); | |
517 } | |
518 | |
519 /* Seek to where the .bss section is tucked away after the heap... */ | |
520 index = heap_index_in_executable + get_committed_heap_size (); | |
521 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF) | |
522 { | |
523 i = GetLastError (); | |
524 exit (1); | |
525 } | |
526 | |
527 | |
528 /* Ok, read in the saved .bss section and initialize all | |
529 uninitialized variables. */ | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
530 if (!ReadFile (file, bss_start, bss_size, &n_read, NULL)) |
12245 | 531 { |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
532 i = GetLastError (); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
533 exit (1); |
12245 | 534 } |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
535 |
12245 | 536 CloseHandle (file); |
537 } | |
538 | |
539 /* Map the heap dumped into the executable file into our address space. */ | |
540 void | |
541 map_in_heap (char *filename) | |
542 { | |
543 HANDLE file; | |
544 HANDLE file_mapping; | |
545 void *file_base; | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
546 unsigned long size, upper_size, n_read; |
12245 | 547 int i; |
548 | |
549 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
550 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
551 if (file == INVALID_HANDLE_VALUE) | |
552 { | |
553 i = GetLastError (); | |
554 exit (1); | |
555 } | |
556 | |
557 size = GetFileSize (file, &upper_size); | |
558 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY, | |
559 0, size, NULL); | |
560 if (!file_mapping) | |
561 { | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
562 i = GetLastError (); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
563 exit (1); |
12245 | 564 } |
565 | |
566 size = get_committed_heap_size (); | |
567 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0, | |
568 heap_index_in_executable, size, | |
569 get_heap_start ()); | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
570 if (file_base != 0) |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
571 { |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
572 return; |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
573 } |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
574 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
575 /* If we don't succeed with the mapping, then copy from the |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
576 data into the heap. */ |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
577 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
578 CloseHandle (file_mapping); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
579 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
580 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (), |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
581 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL) |
12245 | 582 { |
583 i = GetLastError (); | |
584 exit (1); | |
585 } | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
586 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
587 /* Seek to the location of the heap data in the executable. */ |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
588 i = heap_index_in_executable; |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
589 if (SetFilePointer (file, i, NULL, FILE_BEGIN) == 0xFFFFFFFF) |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
590 { |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
591 i = GetLastError (); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
592 exit (1); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
593 } |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
594 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
595 /* Read in the data. */ |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
596 if (!ReadFile (file, get_heap_start (), |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
597 get_committed_heap_size (), &n_read, NULL)) |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
598 { |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
599 i = GetLastError (); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
600 exit (1); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
601 } |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
602 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
603 CloseHandle (file); |
12245 | 604 } |