Mercurial > emacs
annotate src/unexw32.c @ 15013:aad88afa6a0c libc-960413 libc-960414 libc-960415 libc-960416 libc-960417 libc-960418 libc-960419 libc-960420 libc-960421 libc-960422 libc-960423 libc-960424 libc-960425 libc-960426 libc-960427 libc-960428 libc-960429 libc-960430 libc-960501 libc-960502 libc-960503 libc-960504 libc-960505 libc-960506 libc-960507 libc-960508 libc-960509 libc-960510 libc-960511 libc-960512 libc-960513 libc-960514 libc-960515 libc-960516 libc-960517 libc-960518 libc-960519 libc-960520
* config.guess: Combine two OSF1 rules.
Also recognize field test versions. From mjr@zk3.dec.com.
* config.guess (dgux): Use /usr/bin/uname rather than uname,
because GNU uname does not support -p. From pmr@pajato.com.
author | Per Bothner <bothner@cygnus.com> |
---|---|
date | Sat, 13 Apr 1996 00:06:54 +0000 |
parents | bdffcf3b695f |
children | be6560506cce |
rev | line source |
---|---|
14186
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
1 /* unexec for GNU Emacs on Windows NT. |
12245 | 2 Copyright (C) 1994 Free Software Foundation, Inc. |
3 | |
14186
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
4 This file is part of GNU Emacs. |
12245 | 5 |
14186
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
6 GNU Emacs is free software; you can redistribute it and/or modify |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
7 it under the terms of the GNU General Public License as published by |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
8 the Free Software Foundation; either version 2, or (at your option) |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
9 any later version. |
12245 | 10 |
14186
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
11 GNU Emacs is distributed in the hope that it will be useful, |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
14 GNU General Public License for more details. |
12245 | 15 |
14186
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
17 along with GNU Emacs; see the file COPYING. If not, write to |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
14036
diff
changeset
|
19 Boston, MA 02111-1307, USA. |
12245 | 20 |
21 Geoff Voelker (voelker@cs.washington.edu) 8-12-94 | |
22 */ | |
23 | |
24 #include <stdlib.h> /* _fmode */ | |
25 #include <stdio.h> | |
26 #include <fcntl.h> | |
27 #include <windows.h> | |
28 | |
29 extern BOOL ctrl_c_handler (unsigned long type); | |
30 | |
31 #include "ntheap.h" | |
32 | |
33 /* A convenient type for keeping all the info about a mapped file together. */ | |
34 typedef struct file_data { | |
35 char *name; | |
36 unsigned long size; | |
37 HANDLE file; | |
38 HANDLE file_mapping; | |
39 unsigned char *file_base; | |
40 } file_data; | |
41 | |
42 /* Basically, our "initialized" flag. */ | |
43 BOOL need_to_recreate_heap = FALSE; | |
44 | |
45 /* So we can find our heap in the file to recreate it. */ | |
46 unsigned long heap_index_in_executable = 0; | |
47 | |
48 void open_input_file (file_data *p_file, char *name); | |
49 void open_output_file (file_data *p_file, char *name, unsigned long size); | |
50 void close_file_data (file_data *p_file); | |
51 | |
52 void get_section_info (file_data *p_file); | |
53 void copy_executable_and_dump_data_section (file_data *, file_data *); | |
54 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile); | |
55 | |
56 /* Cached info about the .data section in the executable. */ | |
57 PUCHAR data_start_va = 0; | |
58 DWORD data_start_file = 0; | |
59 DWORD data_size = 0; | |
60 | |
61 /* Cached info about the .bss section in the executable. */ | |
62 PUCHAR bss_start = 0; | |
63 DWORD bss_size = 0; | |
64 | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
65 #ifdef HAVE_NTGUI |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
66 HINSTANCE hinst = NULL; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
67 HINSTANCE hprevinst = NULL; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
68 LPSTR lpCmdLine = ""; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
69 int nCmdShow = 0; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
70 |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
71 int __stdcall |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
72 WinMain (_hinst, _hPrevInst, _lpCmdLine, _nCmdShow) |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
73 HINSTANCE _hinst; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
74 HINSTANCE _hPrevInst; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
75 LPSTR _lpCmdLine; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
76 int _nCmdShow; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
77 { |
14812
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
78 int i, j, new_argc; |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
79 char **new_argv; |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
80 |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
81 /* Need to parse command line */ |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
82 |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
83 hinst = _hinst; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
84 hprevinst = _hPrevInst; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
85 lpCmdLine = _lpCmdLine; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
86 nCmdShow = _nCmdShow; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
87 |
14812
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
88 new_argc = __argc; |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
89 new_argv = (char **) xmalloc (sizeof (char *) * new_argc); |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
90 if (!new_argv) |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
91 return main (__argc, __argv, _environ); |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
92 |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
93 for (i = j = 0; i < __argc; i++) |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
94 { |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
95 /* Allocate a console window for stdout and stderr if requested. |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
96 We want to allocate as soon as we possibly can to catch |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
97 debugging output. */ |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
98 if (!strcmp ("-output_console", __argv[i])) |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
99 { |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
100 AllocConsole (); |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
101 new_argc--; |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
102 continue; |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
103 } |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
104 new_argv[j++] = __argv[i]; |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
105 } |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
106 |
bdffcf3b695f
(WinMain): Allocate a console for stdout
Geoff Voelker <voelker@cs.washington.edu>
parents:
14186
diff
changeset
|
107 return main (new_argc, new_argv, _environ); |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
108 } |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
109 #endif /* HAVE_NTGUI */ |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
110 |
12245 | 111 /* Startup code for running on NT. When we are running as the dumped |
112 version, we need to bootstrap our heap and .bss section into our | |
113 address space before we can actually hand off control to the startup | |
114 code supplied by NT (primarily because that code relies upon malloc ()). */ | |
115 void | |
116 _start (void) | |
117 { | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
118 #ifdef HAVE_NTGUI |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
119 extern void WinMainCRTStartup (void); |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
120 #else |
12245 | 121 extern void mainCRTStartup (void); |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
122 #endif /* HAVE_NTGUI */ |
12245 | 123 |
124 /* Cache system info, e.g., the NT page size. */ | |
125 cache_system_info (); | |
126 | |
127 /* If we're a dumped version of emacs then we need to recreate | |
128 our heap and play tricks with our .bss section. Do this before | |
129 start up. (WARNING: Do not put any code before this section | |
130 that relies upon malloc () and runs in the dumped version. It | |
131 won't work.) */ | |
132 if (need_to_recreate_heap) | |
133 { | |
134 char executable_path[MAX_PATH]; | |
135 | |
136 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0) | |
137 { | |
138 printf ("Failed to find path for executable.\n"); | |
139 exit (1); | |
140 } | |
141 recreate_heap (executable_path); | |
142 need_to_recreate_heap = FALSE; | |
143 } | |
144 | |
145 /* The default behavior is to treat files as binary and patch up | |
146 text files appropriately, in accordance with the MSDOS code. */ | |
147 _fmode = O_BINARY; | |
148 | |
149 /* This prevents ctrl-c's in shells running while we're suspended from | |
150 having us exit. */ | |
151 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE); | |
152 | |
153 /* Invoke the NT CRT startup routine now that our housecleaning | |
154 is finished. */ | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
155 #ifdef HAVE_NTGUI |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
156 WinMainCRTStartup (); |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
157 #else |
12245 | 158 mainCRTStartup (); |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
159 #endif /* HAVE_NTGUI */ |
12245 | 160 } |
161 | |
14036 | 162 /* Dump out .data and .bss sections into a new executable. */ |
12245 | 163 void |
164 unexec (char *new_name, char *old_name, void *start_data, void *start_bss, | |
165 void *entry_address) | |
166 { | |
167 file_data in_file, out_file; | |
168 char out_filename[MAX_PATH], in_filename[MAX_PATH]; | |
169 unsigned long size; | |
170 char *ptr; | |
171 | |
172 /* Make sure that the input and output filenames have the | |
173 ".exe" extension...patch them up if they don't. */ | |
174 strcpy (in_filename, old_name); | |
175 ptr = in_filename + strlen (in_filename) - 4; | |
176 if (strcmp (ptr, ".exe")) | |
177 strcat (in_filename, ".exe"); | |
178 | |
179 strcpy (out_filename, new_name); | |
180 ptr = out_filename + strlen (out_filename) - 4; | |
181 if (strcmp (ptr, ".exe")) | |
182 strcat (out_filename, ".exe"); | |
183 | |
184 printf ("Dumping from %s\n", in_filename); | |
185 printf (" to %s\n", out_filename); | |
186 | |
187 /* We need to round off our heap to NT's allocation unit (64KB). */ | |
188 round_heap (get_allocation_unit ()); | |
189 | |
190 /* Open the undumped executable file. */ | |
191 open_input_file (&in_file, in_filename); | |
192 | |
193 /* Get the interesting section info, like start and size of .bss... */ | |
194 get_section_info (&in_file); | |
195 | |
196 /* The size of the dumped executable is the size of the original | |
197 executable plus the size of the heap and the size of the .bss section. */ | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
198 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
|
199 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ()); |
12245 | 200 size = heap_index_in_executable + get_committed_heap_size () + bss_size; |
201 open_output_file (&out_file, out_filename, size); | |
202 | |
203 /* Set the flag (before dumping). */ | |
204 need_to_recreate_heap = TRUE; | |
205 | |
206 copy_executable_and_dump_data_section (&in_file, &out_file); | |
207 dump_bss_and_heap (&in_file, &out_file); | |
208 | |
209 close_file_data (&in_file); | |
210 close_file_data (&out_file); | |
211 } | |
212 | |
213 | |
214 /* File handling. */ | |
215 | |
216 | |
217 void | |
218 open_input_file (file_data *p_file, char *filename) | |
219 { | |
220 HANDLE file; | |
221 HANDLE file_mapping; | |
222 void *file_base; | |
223 unsigned long size, upper_size; | |
224 | |
225 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
226 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
227 if (file == INVALID_HANDLE_VALUE) | |
228 { | |
229 printf ("Failed to open %s (%d)...bailing.\n", | |
230 filename, GetLastError ()); | |
231 exit (1); | |
232 } | |
233 | |
234 size = GetFileSize (file, &upper_size); | |
235 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY, | |
236 0, size, NULL); | |
237 if (!file_mapping) | |
238 { | |
239 printf ("Failed to create file mapping of %s (%d)...bailing.\n", | |
240 filename, GetLastError ()); | |
241 exit (1); | |
242 } | |
243 | |
244 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size); | |
245 if (file_base == 0) | |
246 { | |
247 printf ("Failed to map view of file of %s (%d)...bailing.\n", | |
248 filename, GetLastError ()); | |
249 exit (1); | |
250 } | |
251 | |
252 p_file->name = filename; | |
253 p_file->size = size; | |
254 p_file->file = file; | |
255 p_file->file_mapping = file_mapping; | |
256 p_file->file_base = file_base; | |
257 } | |
258 | |
259 void | |
260 open_output_file (file_data *p_file, char *filename, unsigned long size) | |
261 { | |
262 HANDLE file; | |
263 HANDLE file_mapping; | |
264 void *file_base; | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
265 int i; |
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
266 |
12245 | 267 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
268 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); | |
269 if (file == INVALID_HANDLE_VALUE) | |
270 { | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
271 i = GetLastError (); |
12245 | 272 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
|
273 filename, i); |
12245 | 274 exit (1); |
275 } | |
276 | |
277 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE, | |
278 0, size, NULL); | |
279 if (!file_mapping) | |
280 { | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
281 i = GetLastError (); |
12245 | 282 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
|
283 filename, i); |
12245 | 284 exit (1); |
285 } | |
286 | |
287 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size); | |
288 if (file_base == 0) | |
289 { | |
13423
eefa4f720371
[HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents:
12454
diff
changeset
|
290 i = GetLastError (); |
12245 | 291 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
|
292 filename, i); |
12245 | 293 exit (1); |
294 } | |
295 | |
296 p_file->name = filename; | |
297 p_file->size = size; | |
298 p_file->file = file; | |
299 p_file->file_mapping = file_mapping; | |
300 p_file->file_base = file_base; | |
301 } | |
302 | |
303 /* Close the system structures associated with the given file. */ | |
304 static void | |
305 close_file_data (file_data *p_file) | |
306 { | |
307 UnmapViewOfFile (p_file->file_base); | |
308 CloseHandle (p_file->file_mapping); | |
309 CloseHandle (p_file->file); | |
310 } | |
311 | |
312 | |
313 /* Routines to manipulate NT executable file sections. */ | |
314 | |
13830
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
315 static void |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
316 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
|
317 DWORD *p_bss_size) |
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 int n, start, len; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
320 char map_filename[MAX_PATH]; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
321 char buffer[256]; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
322 FILE *map; |
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 /* 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
|
325 the .map extension. */ |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
326 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
|
327 n = strlen (map_filename) - 3; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
328 strcpy (&map_filename[n], "map"); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
329 |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
330 map = fopen (map_filename, "r"); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
331 if (!map) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
332 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
333 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
|
334 map_filename, GetLastError ()); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
335 exit (-1); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
336 } |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
337 |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
338 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
|
339 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
340 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
|
341 continue; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
342 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
|
343 if (n != 2) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
344 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
345 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
|
346 exit (-1); |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
347 } |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
348 break; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
349 } |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
350 *p_bss_start = (PUCHAR) start; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
351 *p_bss_size = (DWORD) len; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
352 } |
12245 | 353 |
354 static unsigned long | |
355 get_section_size (PIMAGE_SECTION_HEADER p_section) | |
356 { | |
357 /* The section size is in different locations in the different versions. */ | |
358 switch (get_nt_minor_version ()) | |
359 { | |
360 case 10: | |
361 return p_section->SizeOfRawData; | |
362 default: | |
363 return p_section->Misc.VirtualSize; | |
364 } | |
365 } | |
366 | |
367 /* Flip through the executable and cache the info necessary for dumping. */ | |
368 static void | |
369 get_section_info (file_data *p_infile) | |
370 { | |
371 PIMAGE_DOS_HEADER dos_header; | |
372 PIMAGE_NT_HEADERS nt_header; | |
13830
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
373 PIMAGE_SECTION_HEADER section, data_section; |
12245 | 374 unsigned char *ptr; |
375 int i; | |
376 | |
377 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base; | |
378 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) | |
379 { | |
380 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name); | |
381 exit (1); | |
382 } | |
383 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + | |
384 dos_header->e_lfanew); | |
385 if (nt_header == NULL) | |
386 { | |
387 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n", | |
388 p_infile->name); | |
389 exit (1); | |
390 } | |
391 | |
392 /* Check the NT header signature ... */ | |
393 if (nt_header->Signature != IMAGE_NT_SIGNATURE) | |
394 { | |
395 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n", | |
396 nt_header->Signature, p_infile->name); | |
397 } | |
398 | |
399 /* Flip through the sections for .data and .bss ... */ | |
400 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header); | |
401 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) | |
402 { | |
403 if (!strcmp (section->Name, ".bss")) | |
404 { | |
405 /* The .bss section. */ | |
406 ptr = (char *) nt_header->OptionalHeader.ImageBase + | |
407 section->VirtualAddress; | |
408 bss_start = ptr; | |
409 bss_size = get_section_size (section); | |
410 } | |
411 if (!strcmp (section->Name, ".data")) | |
412 { | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
413 /* From lastfile.c */ |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
414 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
|
415 |
12245 | 416 /* The .data section. */ |
13830
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
417 data_section = section; |
12245 | 418 ptr = (char *) nt_header->OptionalHeader.ImageBase + |
419 section->VirtualAddress; | |
420 data_start_va = ptr; | |
421 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
|
422 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
423 /* 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
|
424 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
|
425 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
|
426 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
|
427 data_size = my_edata - data_start_va; |
12245 | 428 } |
429 section++; | |
430 } | |
13830
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
431 |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
432 if (!bss_start && !bss_size) |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
433 { |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
434 /* 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
|
435 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
|
436 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
|
437 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
|
438 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
|
439 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
|
440 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
|
441 .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
|
442 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
|
443 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
|
444 + data_section->VirtualAddress; |
8d30151f4acb
(get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents:
13423
diff
changeset
|
445 } |
12245 | 446 } |
447 | |
448 | |
449 /* The dump routines. */ | |
450 | |
451 static void | |
452 copy_executable_and_dump_data_section (file_data *p_infile, | |
453 file_data *p_outfile) | |
454 { | |
455 unsigned char *data_file, *data_va; | |
456 unsigned long size, index; | |
457 | |
458 /* Get a pointer to where the raw data should go in the executable file. */ | |
459 data_file = (char *) p_outfile->file_base + data_start_file; | |
460 | |
461 /* Get a pointer to the raw data in our address space. */ | |
462 data_va = data_start_va; | |
463 | |
464 size = (DWORD) data_file - (DWORD) p_outfile->file_base; | |
465 printf ("Copying executable up to data section...\n"); | |
466 printf ("\t0x%08x Offset in input file.\n", 0); | |
467 printf ("\t0x%08x Offset in output file.\n", 0); | |
468 printf ("\t0x%08x Size in bytes.\n", size); | |
469 memcpy (p_outfile->file_base, p_infile->file_base, size); | |
470 | |
471 size = data_size; | |
472 printf ("Dumping .data section...\n"); | |
473 printf ("\t0x%08x Address in process.\n", data_va); | |
474 printf ("\t0x%08x Offset in output file.\n", | |
475 data_file - p_outfile->file_base); | |
476 printf ("\t0x%08x Size in bytes.\n", size); | |
477 memcpy (data_file, data_va, size); | |
478 | |
479 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base; | |
480 size = p_infile->size - index; | |
481 printf ("Copying rest of executable...\n"); | |
482 printf ("\t0x%08x Offset in input file.\n", index); | |
483 printf ("\t0x%08x Offset in output file.\n", index); | |
484 printf ("\t0x%08x Size in bytes.\n", size); | |
485 memcpy ((char *) p_outfile->file_base + index, | |
486 (char *) p_infile->file_base + index, size); | |
487 } | |
488 | |
489 static void | |
490 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile) | |
491 { | |
492 unsigned char *heap_data, *bss_data; | |
493 unsigned long size, index; | |
494 | |
495 printf ("Dumping heap into executable...\n"); | |
496 | |
497 index = heap_index_in_executable; | |
498 size = get_committed_heap_size (); | |
499 heap_data = get_heap_start (); | |
500 | |
501 printf ("\t0x%08x Heap start in process.\n", heap_data); | |
502 printf ("\t0x%08x Heap offset in executable.\n", index); | |
503 printf ("\t0x%08x Heap size in bytes.\n", size); | |
504 | |
505 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size); | |
506 | |
507 printf ("Dumping .bss into executable...\n"); | |
508 | |
509 index += size; | |
510 size = bss_size; | |
511 bss_data = bss_start; | |
512 | |
513 printf ("\t0x%08x BSS start in process.\n", bss_data); | |
514 printf ("\t0x%08x BSS offset in executable.\n", index); | |
515 printf ("\t0x%08x BSS size in bytes.\n", size); | |
516 memcpy ((char *) p_outfile->file_base + index, bss_data, size); | |
517 } | |
518 | |
519 | |
520 /* Reload and remap routines. */ | |
521 | |
522 | |
523 /* Load the dumped .bss section into the .bss area of our address space. */ | |
524 void | |
525 read_in_bss (char *filename) | |
526 { | |
527 HANDLE file; | |
528 unsigned long size, index, n_read, total_read; | |
529 char buffer[512], *bss; | |
530 int i; | |
531 | |
532 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
533 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
534 if (file == INVALID_HANDLE_VALUE) | |
535 { | |
536 i = GetLastError (); | |
537 exit (1); | |
538 } | |
539 | |
540 /* Seek to where the .bss section is tucked away after the heap... */ | |
541 index = heap_index_in_executable + get_committed_heap_size (); | |
542 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF) | |
543 { | |
544 i = GetLastError (); | |
545 exit (1); | |
546 } | |
547 | |
548 | |
549 /* Ok, read in the saved .bss section and initialize all | |
550 uninitialized variables. */ | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
551 if (!ReadFile (file, bss_start, bss_size, &n_read, NULL)) |
12245 | 552 { |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
553 i = GetLastError (); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
554 exit (1); |
12245 | 555 } |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
556 |
12245 | 557 CloseHandle (file); |
558 } | |
559 | |
560 /* Map the heap dumped into the executable file into our address space. */ | |
561 void | |
562 map_in_heap (char *filename) | |
563 { | |
564 HANDLE file; | |
565 HANDLE file_mapping; | |
566 void *file_base; | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
567 unsigned long size, upper_size, n_read; |
12245 | 568 int i; |
569 | |
570 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, | |
571 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
572 if (file == INVALID_HANDLE_VALUE) | |
573 { | |
574 i = GetLastError (); | |
575 exit (1); | |
576 } | |
577 | |
578 size = GetFileSize (file, &upper_size); | |
579 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY, | |
580 0, size, NULL); | |
581 if (!file_mapping) | |
582 { | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
583 i = GetLastError (); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
584 exit (1); |
12245 | 585 } |
586 | |
587 size = get_committed_heap_size (); | |
588 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0, | |
589 heap_index_in_executable, size, | |
590 get_heap_start ()); | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
591 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
|
592 { |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
593 return; |
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 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
596 /* 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
|
597 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
|
598 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
599 CloseHandle (file_mapping); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
600 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
601 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
|
602 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL) |
12245 | 603 { |
604 i = GetLastError (); | |
605 exit (1); | |
606 } | |
12454
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
607 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
608 /* 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
|
609 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
|
610 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
|
611 { |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
612 i = GetLastError (); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
613 exit (1); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
614 } |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
615 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
616 /* 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
|
617 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
|
618 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
|
619 { |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
620 i = GetLastError (); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
621 exit (1); |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
622 } |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
623 |
3f4da17a7cd8
(get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents:
12245
diff
changeset
|
624 CloseHandle (file); |
12245 | 625 } |