14186
|
1 /* unexec for GNU Emacs on Windows NT.
|
75227
|
2 Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005,
|
106815
|
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
12245
|
4
|
14186
|
5 This file is part of GNU Emacs.
|
12245
|
6
|
94963
|
7 GNU Emacs is free software: you can redistribute it and/or modify
|
14186
|
8 it under the terms of the GNU General Public License as published by
|
94963
|
9 the Free Software Foundation, either version 3 of the License, or
|
|
10 (at your option) any later version.
|
12245
|
11
|
14186
|
12 GNU Emacs is distributed in the hope that it will be useful,
|
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 GNU General Public License for more details.
|
12245
|
16
|
14186
|
17 You should have received a copy of the GNU General Public License
|
94963
|
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
|
12245
|
19
|
94963
|
20 /*
|
12245
|
21 Geoff Voelker (voelker@cs.washington.edu) 8-12-94
|
|
22 */
|
|
23
|
19703
|
24 #include <config.h>
|
|
25
|
12245
|
26 #include <stdio.h>
|
|
27 #include <fcntl.h>
|
19703
|
28 #include <time.h>
|
12245
|
29 #include <windows.h>
|
|
30
|
19703
|
31 /* Include relevant definitions from IMAGEHLP.H, which can be found
|
|
32 in \\win32sdk\mstools\samples\image\include\imagehlp.h. */
|
|
33
|
|
34 PIMAGE_NT_HEADERS
|
|
35 (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress,
|
|
36 DWORD FileLength,
|
|
37 LPDWORD HeaderSum,
|
|
38 LPDWORD CheckSum);
|
|
39
|
12245
|
40 extern BOOL ctrl_c_handler (unsigned long type);
|
|
41
|
19703
|
42 extern char my_begdata[];
|
|
43 extern char my_edata[];
|
|
44 extern char my_begbss[];
|
|
45 extern char my_endbss[];
|
21604
|
46 extern char *my_begbss_static;
|
|
47 extern char *my_endbss_static;
|
12245
|
48
|
19703
|
49 #include "w32heap.h"
|
18506
|
50
|
21456
|
51 #undef min
|
|
52 #undef max
|
|
53 #define min(x, y) (((x) < (y)) ? (x) : (y))
|
|
54 #define max(x, y) (((x) > (y)) ? (x) : (y))
|
|
55
|
12245
|
56 /* Basically, our "initialized" flag. */
|
24102
|
57 BOOL using_dynamic_heap = FALSE;
|
12245
|
58
|
19703
|
59 int open_input_file (file_data *p_file, char *name);
|
|
60 int open_output_file (file_data *p_file, char *name, unsigned long size);
|
12245
|
61 void close_file_data (file_data *p_file);
|
|
62
|
|
63 void get_section_info (file_data *p_file);
|
24102
|
64 void copy_executable_and_dump_data (file_data *, file_data *);
|
12245
|
65 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
|
|
66
|
|
67 /* Cached info about the .data section in the executable. */
|
24102
|
68 PIMAGE_SECTION_HEADER data_section;
|
31104
|
69 PCHAR data_start = 0;
|
12245
|
70 DWORD data_size = 0;
|
|
71
|
|
72 /* Cached info about the .bss section in the executable. */
|
24102
|
73 PIMAGE_SECTION_HEADER bss_section;
|
31104
|
74 PCHAR bss_start = 0;
|
12245
|
75 DWORD bss_size = 0;
|
24102
|
76 DWORD extra_bss_size = 0;
|
|
77 /* bss data that is static might be discontiguous from non-static. */
|
|
78 PIMAGE_SECTION_HEADER bss_section_static;
|
31104
|
79 PCHAR bss_start_static = 0;
|
24102
|
80 DWORD bss_size_static = 0;
|
|
81 DWORD extra_bss_size_static = 0;
|
|
82
|
|
83 PIMAGE_SECTION_HEADER heap_section;
|
12245
|
84
|
13423
|
85 #ifdef HAVE_NTGUI
|
|
86 HINSTANCE hinst = NULL;
|
|
87 HINSTANCE hprevinst = NULL;
|
|
88 LPSTR lpCmdLine = "";
|
|
89 int nCmdShow = 0;
|
|
90 #endif /* HAVE_NTGUI */
|
|
91
|
12245
|
92 /* Startup code for running on NT. When we are running as the dumped
|
|
93 version, we need to bootstrap our heap and .bss section into our
|
|
94 address space before we can actually hand off control to the startup
|
|
95 code supplied by NT (primarily because that code relies upon malloc ()). */
|
|
96 void
|
|
97 _start (void)
|
|
98 {
|
|
99 extern void mainCRTStartup (void);
|
|
100
|
24237
|
101 #if 1
|
19703
|
102 /* Give us a way to debug problems with crashes on startup when
|
|
103 running under the MSVC profiler. */
|
|
104 if (GetEnvironmentVariable ("EMACS_DEBUG", NULL, 0) > 0)
|
|
105 DebugBreak ();
|
|
106 #endif
|
|
107
|
12245
|
108 /* Cache system info, e.g., the NT page size. */
|
|
109 cache_system_info ();
|
|
110
|
24102
|
111 /* Grab our malloc arena space now, before CRT starts up. */
|
|
112 init_heap ();
|
12245
|
113
|
|
114 /* This prevents ctrl-c's in shells running while we're suspended from
|
|
115 having us exit. */
|
|
116 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
|
|
117
|
23948
|
118 /* Prevent Emacs from being locked up (eg. in batch mode) when
|
|
119 accessing devices that aren't mounted (eg. removable media drives). */
|
|
120 SetErrorMode (SEM_FAILCRITICALERRORS);
|
|
121
|
12245
|
122 /* Invoke the NT CRT startup routine now that our housecleaning
|
|
123 is finished. */
|
13423
|
124 #ifdef HAVE_NTGUI
|
15146
|
125 /* determine WinMain args like crt0.c does */
|
|
126 hinst = GetModuleHandle(NULL);
|
|
127 lpCmdLine = GetCommandLine();
|
|
128 nCmdShow = SW_SHOWDEFAULT;
|
|
129 #endif
|
12245
|
130 mainCRTStartup ();
|
|
131 }
|
|
132
|
|
133
|
|
134 /* File handling. */
|
|
135
|
19703
|
136 int
|
12245
|
137 open_input_file (file_data *p_file, char *filename)
|
|
138 {
|
|
139 HANDLE file;
|
|
140 HANDLE file_mapping;
|
|
141 void *file_base;
|
|
142 unsigned long size, upper_size;
|
|
143
|
|
144 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
145 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
49600
|
146 if (file == INVALID_HANDLE_VALUE)
|
19703
|
147 return FALSE;
|
12245
|
148
|
|
149 size = GetFileSize (file, &upper_size);
|
49600
|
150 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
|
12245
|
151 0, size, NULL);
|
49600
|
152 if (!file_mapping)
|
19703
|
153 return FALSE;
|
12245
|
154
|
|
155 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
|
49600
|
156 if (file_base == 0)
|
19703
|
157 return FALSE;
|
12245
|
158
|
|
159 p_file->name = filename;
|
|
160 p_file->size = size;
|
|
161 p_file->file = file;
|
|
162 p_file->file_mapping = file_mapping;
|
|
163 p_file->file_base = file_base;
|
19703
|
164
|
|
165 return TRUE;
|
12245
|
166 }
|
|
167
|
19703
|
168 int
|
12245
|
169 open_output_file (file_data *p_file, char *filename, unsigned long size)
|
|
170 {
|
|
171 HANDLE file;
|
|
172 HANDLE file_mapping;
|
|
173 void *file_base;
|
13423
|
174
|
12245
|
175 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
|
|
176 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
|
49600
|
177 if (file == INVALID_HANDLE_VALUE)
|
19703
|
178 return FALSE;
|
|
179
|
49600
|
180 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
|
12245
|
181 0, size, NULL);
|
49600
|
182 if (!file_mapping)
|
19703
|
183 return FALSE;
|
49600
|
184
|
12245
|
185 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
|
49600
|
186 if (file_base == 0)
|
19703
|
187 return FALSE;
|
49600
|
188
|
12245
|
189 p_file->name = filename;
|
|
190 p_file->size = size;
|
|
191 p_file->file = file;
|
|
192 p_file->file_mapping = file_mapping;
|
|
193 p_file->file_base = file_base;
|
19703
|
194
|
|
195 return TRUE;
|
12245
|
196 }
|
|
197
|
|
198 /* Close the system structures associated with the given file. */
|
19703
|
199 void
|
12245
|
200 close_file_data (file_data *p_file)
|
|
201 {
|
24102
|
202 UnmapViewOfFile (p_file->file_base);
|
|
203 CloseHandle (p_file->file_mapping);
|
|
204 /* For the case of output files, set final size. */
|
|
205 SetFilePointer (p_file->file, p_file->size, NULL, FILE_BEGIN);
|
|
206 SetEndOfFile (p_file->file);
|
|
207 CloseHandle (p_file->file);
|
12245
|
208 }
|
|
209
|
|
210
|
|
211 /* Routines to manipulate NT executable file sections. */
|
|
212
|
19703
|
213 /* Return pointer to section header for named section. */
|
|
214 IMAGE_SECTION_HEADER *
|
|
215 find_section (char * name, IMAGE_NT_HEADERS * nt_header)
|
|
216 {
|
|
217 PIMAGE_SECTION_HEADER section;
|
|
218 int i;
|
|
219
|
|
220 section = IMAGE_FIRST_SECTION (nt_header);
|
|
221
|
|
222 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
|
12245
|
223 {
|
19703
|
224 if (strcmp (section->Name, name) == 0)
|
|
225 return section;
|
|
226 section++;
|
12245
|
227 }
|
19703
|
228 return NULL;
|
12245
|
229 }
|
|
230
|
19703
|
231 /* Return pointer to section header for section containing the given
|
|
232 relative virtual address. */
|
|
233 IMAGE_SECTION_HEADER *
|
|
234 rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header)
|
|
235 {
|
|
236 PIMAGE_SECTION_HEADER section;
|
|
237 int i;
|
|
238
|
|
239 section = IMAGE_FIRST_SECTION (nt_header);
|
|
240
|
|
241 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
|
|
242 {
|
24102
|
243 /* Some linkers (eg. the NT SDK linker I believe) swapped the
|
|
244 meaning of these two values - or rather, they ignored
|
|
245 VirtualSize entirely and always set it to zero. This affects
|
|
246 some very old exes (eg. gzip dated Dec 1993). Since
|
|
247 w32_executable_type relies on this function to work reliably,
|
|
248 we need to cope with this. */
|
|
249 DWORD real_size = max (section->SizeOfRawData,
|
|
250 section->Misc.VirtualSize);
|
19703
|
251 if (rva >= section->VirtualAddress
|
24102
|
252 && rva < section->VirtualAddress + real_size)
|
|
253 return section;
|
|
254 section++;
|
|
255 }
|
|
256 return NULL;
|
|
257 }
|
|
258
|
|
259 /* Return pointer to section header for section containing the given
|
|
260 offset in its raw data area. */
|
|
261 IMAGE_SECTION_HEADER *
|
|
262 offset_to_section (DWORD offset, IMAGE_NT_HEADERS * nt_header)
|
|
263 {
|
|
264 PIMAGE_SECTION_HEADER section;
|
|
265 int i;
|
|
266
|
|
267 section = IMAGE_FIRST_SECTION (nt_header);
|
|
268
|
|
269 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
|
|
270 {
|
|
271 if (offset >= section->PointerToRawData
|
|
272 && offset < section->PointerToRawData + section->SizeOfRawData)
|
19703
|
273 return section;
|
|
274 section++;
|
|
275 }
|
|
276 return NULL;
|
|
277 }
|
|
278
|
24102
|
279 /* Return offset to an object in dst, given offset in src. We assume
|
|
280 there is at least one section in both src and dst images, and that
|
|
281 the some sections may have been added to dst (after sections in src). */
|
31104
|
282 DWORD
|
24102
|
283 relocate_offset (DWORD offset,
|
|
284 IMAGE_NT_HEADERS * src_nt_header,
|
|
285 IMAGE_NT_HEADERS * dst_nt_header)
|
|
286 {
|
|
287 PIMAGE_SECTION_HEADER src_section = IMAGE_FIRST_SECTION (src_nt_header);
|
|
288 PIMAGE_SECTION_HEADER dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
|
|
289 int i = 0;
|
|
290
|
|
291 while (offset >= src_section->PointerToRawData)
|
|
292 {
|
|
293 if (offset < src_section->PointerToRawData + src_section->SizeOfRawData)
|
|
294 break;
|
|
295 i++;
|
|
296 if (i == src_nt_header->FileHeader.NumberOfSections)
|
|
297 {
|
|
298 /* Handle offsets after the last section. */
|
|
299 dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
|
|
300 dst_section += dst_nt_header->FileHeader.NumberOfSections - 1;
|
|
301 while (dst_section->PointerToRawData == 0)
|
|
302 dst_section--;
|
|
303 while (src_section->PointerToRawData == 0)
|
|
304 src_section--;
|
|
305 return offset
|
|
306 + (dst_section->PointerToRawData + dst_section->SizeOfRawData)
|
|
307 - (src_section->PointerToRawData + src_section->SizeOfRawData);
|
|
308 }
|
|
309 src_section++;
|
|
310 dst_section++;
|
|
311 }
|
|
312 return offset +
|
|
313 (dst_section->PointerToRawData - src_section->PointerToRawData);
|
|
314 }
|
|
315
|
|
316 #define OFFSET_TO_RVA(offset, section) \
|
|
317 (section->VirtualAddress + ((DWORD)(offset) - section->PointerToRawData))
|
|
318
|
|
319 #define RVA_TO_OFFSET(rva, section) \
|
|
320 (section->PointerToRawData + ((DWORD)(rva) - section->VirtualAddress))
|
|
321
|
|
322 #define RVA_TO_SECTION_OFFSET(rva, section) \
|
|
323 ((DWORD)(rva) - section->VirtualAddress)
|
|
324
|
|
325 /* Convert address in executing image to RVA. */
|
|
326 #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL))
|
|
327
|
63086
|
328 #define RVA_TO_PTR(var,section,filedata) \
|
|
329 ((void *)(RVA_TO_OFFSET(var,section) + (filedata).file_base))
|
|
330
|
24102
|
331 #define PTR_TO_OFFSET(ptr, pfile_data) \
|
31104
|
332 ((unsigned char *)(ptr) - (pfile_data)->file_base)
|
24102
|
333
|
|
334 #define OFFSET_TO_PTR(offset, pfile_data) \
|
|
335 ((pfile_data)->file_base + (DWORD)(offset))
|
|
336
|
19703
|
337
|
12245
|
338 /* Flip through the executable and cache the info necessary for dumping. */
|
31104
|
339 void
|
12245
|
340 get_section_info (file_data *p_infile)
|
|
341 {
|
|
342 PIMAGE_DOS_HEADER dos_header;
|
|
343 PIMAGE_NT_HEADERS nt_header;
|
24102
|
344 PIMAGE_SECTION_HEADER section;
|
|
345 int overlap;
|
49600
|
346
|
12245
|
347 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
|
49600
|
348 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
12245
|
349 {
|
|
350 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
|
|
351 exit (1);
|
|
352 }
|
49600
|
353 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
|
12245
|
354 dos_header->e_lfanew);
|
49600
|
355 if (nt_header == NULL)
|
12245
|
356 {
|
49600
|
357 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
|
12245
|
358 p_infile->name);
|
|
359 exit (1);
|
|
360 }
|
|
361
|
|
362 /* Check the NT header signature ... */
|
49600
|
363 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
|
12245
|
364 {
|
|
365 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
|
|
366 nt_header->Signature, p_infile->name);
|
24102
|
367 exit (1);
|
|
368 }
|
|
369
|
|
370 /* Locate the ".data" and ".bss" sections for Emacs. (Note that the
|
|
371 actual section names are probably different from these, and might
|
|
372 actually be the same section.)
|
|
373
|
|
374 We do this as follows: first we determine the virtual address
|
|
375 ranges in this process for the data and bss variables that we wish
|
|
376 to preserve. Then we map these VAs to the section entries in the
|
|
377 source image. Finally, we determine the new size of the raw data
|
|
378 area for the bss section, so we can make the new image the correct
|
|
379 size. */
|
|
380
|
24669
|
381 /* We arrange for the Emacs initialized data to be in a separate
|
|
382 section if possible, because we cannot rely on my_begdata and
|
|
383 my_edata marking out the full extent of the initialized data, at
|
|
384 least on the Alpha where the linker freely reorders variables
|
|
385 across libraries. If we can arrange for this, all we need to do is
|
|
386 find the start and size of the EMDATA section. */
|
|
387 data_section = find_section ("EMDATA", nt_header);
|
|
388 if (data_section)
|
24102
|
389 {
|
24669
|
390 data_start = (char *) nt_header->OptionalHeader.ImageBase +
|
|
391 data_section->VirtualAddress;
|
|
392 data_size = data_section->Misc.VirtualSize;
|
|
393 }
|
|
394 else
|
|
395 {
|
|
396 /* Fallback on the old method if compiler doesn't support the
|
|
397 data_set #pragma (or its equivalent). */
|
|
398 data_start = my_begdata;
|
|
399 data_size = my_edata - my_begdata;
|
|
400 data_section = rva_to_section (PTR_TO_RVA (my_begdata), nt_header);
|
|
401 if (data_section != rva_to_section (PTR_TO_RVA (my_edata), nt_header))
|
|
402 {
|
|
403 printf ("Initialized data is not in a single section...bailing\n");
|
|
404 exit (1);
|
|
405 }
|
12245
|
406 }
|
|
407
|
24102
|
408 /* As noted in lastfile.c, the Alpha (but not the Intel) MSVC linker
|
|
409 globally segregates all static and public bss data (ie. across all
|
|
410 linked modules, not just per module), so we must take both static
|
|
411 and public bss areas into account to determine the true extent of
|
|
412 the bss area used by Emacs.
|
|
413
|
|
414 To be strictly correct, we dump the static and public bss areas
|
|
415 used by Emacs separately if non-overlapping (since otherwise we are
|
|
416 dumping bss data belonging to system libraries, eg. the static bss
|
|
417 system data on the Alpha). */
|
|
418
|
|
419 bss_start = my_begbss;
|
|
420 bss_size = my_endbss - my_begbss;
|
|
421 bss_section = rva_to_section (PTR_TO_RVA (my_begbss), nt_header);
|
|
422 if (bss_section != rva_to_section (PTR_TO_RVA (my_endbss), nt_header))
|
12245
|
423 {
|
24102
|
424 printf ("Uninitialized data is not in a single section...bailing\n");
|
|
425 exit (1);
|
|
426 }
|
|
427 /* Compute how much the .bss section's raw data will grow. */
|
|
428 extra_bss_size =
|
|
429 ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss), bss_section),
|
|
430 nt_header->OptionalHeader.FileAlignment)
|
|
431 - bss_section->SizeOfRawData;
|
12454
|
432
|
24102
|
433 bss_start_static = my_begbss_static;
|
|
434 bss_size_static = my_endbss_static - my_begbss_static;
|
|
435 bss_section_static = rva_to_section (PTR_TO_RVA (my_begbss_static), nt_header);
|
|
436 if (bss_section_static != rva_to_section (PTR_TO_RVA (my_endbss_static), nt_header))
|
|
437 {
|
|
438 printf ("Uninitialized static data is not in a single section...bailing\n");
|
|
439 exit (1);
|
|
440 }
|
|
441 /* Compute how much the static .bss section's raw data will grow. */
|
|
442 extra_bss_size_static =
|
|
443 ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss_static), bss_section_static),
|
|
444 nt_header->OptionalHeader.FileAlignment)
|
|
445 - bss_section_static->SizeOfRawData;
|
12454
|
446
|
24102
|
447 /* Combine the bss sections into one if they overlap. */
|
24806
|
448 #ifdef _ALPHA_
|
|
449 overlap = 1; /* force all bss data to be dumped */
|
|
450 #else
|
24102
|
451 overlap = 0;
|
24806
|
452 #endif
|
24102
|
453 if (bss_start < bss_start_static)
|
|
454 {
|
|
455 if (bss_start_static < bss_start + bss_size)
|
|
456 overlap = 1;
|
|
457 }
|
|
458 else
|
|
459 {
|
|
460 if (bss_start < bss_start_static + bss_size_static)
|
|
461 overlap = 1;
|
|
462 }
|
|
463 if (overlap)
|
|
464 {
|
|
465 if (bss_section != bss_section_static)
|
|
466 {
|
|
467 printf ("BSS data not in a single section...bailing\n");
|
|
468 exit (1);
|
12245
|
469 }
|
24102
|
470 bss_start = min (bss_start, bss_start_static);
|
|
471 bss_size = max (my_endbss, my_endbss_static) - bss_start;
|
|
472 bss_section_static = 0;
|
|
473 extra_bss_size_static = 0;
|
12245
|
474 }
|
13830
|
475
|
24102
|
476 heap_section = rva_to_section (PTR_TO_RVA (get_heap_start ()), nt_header);
|
12245
|
477 }
|
|
478
|
|
479
|
|
480 /* The dump routines. */
|
|
481
|
31104
|
482 void
|
49600
|
483 copy_executable_and_dump_data (file_data *p_infile,
|
24102
|
484 file_data *p_outfile)
|
12245
|
485 {
|
24102
|
486 unsigned char *dst, *dst_save;
|
|
487 PIMAGE_DOS_HEADER dos_header;
|
|
488 PIMAGE_NT_HEADERS nt_header;
|
|
489 PIMAGE_NT_HEADERS dst_nt_header;
|
|
490 PIMAGE_SECTION_HEADER section;
|
|
491 PIMAGE_SECTION_HEADER dst_section;
|
|
492 DWORD offset;
|
|
493 int i;
|
63233
|
494 int be_verbose = GetEnvironmentVariable ("DEBUG_DUMP", NULL, 0) > 0;
|
24102
|
495
|
63233
|
496 #define COPY_CHUNK(message, src, size, verbose) \
|
24102
|
497 do { \
|
|
498 unsigned char *s = (void *)(src); \
|
|
499 unsigned long count = (size); \
|
63233
|
500 if (verbose) \
|
|
501 { \
|
|
502 printf ("%s\n", (message)); \
|
|
503 printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \
|
|
504 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
|
|
505 printf ("\t0x%08x Size in bytes.\n", count); \
|
|
506 } \
|
24102
|
507 memcpy (dst, s, count); \
|
|
508 dst += count; \
|
|
509 } while (0)
|
|
510
|
63233
|
511 #define COPY_PROC_CHUNK(message, src, size, verbose) \
|
24102
|
512 do { \
|
|
513 unsigned char *s = (void *)(src); \
|
|
514 unsigned long count = (size); \
|
63233
|
515 if (verbose) \
|
|
516 { \
|
|
517 printf ("%s\n", (message)); \
|
|
518 printf ("\t0x%08x Address in process.\n", s); \
|
|
519 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
|
|
520 printf ("\t0x%08x Size in bytes.\n", count); \
|
|
521 } \
|
24102
|
522 memcpy (dst, s, count); \
|
|
523 dst += count; \
|
|
524 } while (0)
|
|
525
|
|
526 #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile)
|
|
527 #define ROUND_UP_DST(align) \
|
|
528 (dst = p_outfile->file_base + ROUND_UP (DST_TO_OFFSET (), (align)))
|
24237
|
529 #define ROUND_UP_DST_AND_ZERO(align) \
|
|
530 do { \
|
|
531 unsigned char *newdst = p_outfile->file_base \
|
|
532 + ROUND_UP (DST_TO_OFFSET (), (align)); \
|
|
533 /* Zero the alignment slop; it may actually initialize real data. */ \
|
|
534 memset (dst, 0, newdst - dst); \
|
|
535 dst = newdst; \
|
|
536 } while (0)
|
24102
|
537
|
|
538 /* Copy the source image sequentially, ie. section by section after
|
|
539 copying the headers and section table, to simplify the process of
|
|
540 dumping the raw data for the bss and heap sections.
|
|
541
|
|
542 Note that dst is updated implicitly by each COPY_CHUNK. */
|
|
543
|
|
544 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
|
49600
|
545 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
|
24102
|
546 dos_header->e_lfanew);
|
|
547 section = IMAGE_FIRST_SECTION (nt_header);
|
49600
|
548
|
24102
|
549 dst = (unsigned char *) p_outfile->file_base;
|
|
550
|
|
551 COPY_CHUNK ("Copying DOS header...", dos_header,
|
63233
|
552 (DWORD) nt_header - (DWORD) dos_header, be_verbose);
|
24102
|
553 dst_nt_header = (PIMAGE_NT_HEADERS) dst;
|
|
554 COPY_CHUNK ("Copying NT header...", nt_header,
|
63233
|
555 (DWORD) section - (DWORD) nt_header, be_verbose);
|
24102
|
556 dst_section = (PIMAGE_SECTION_HEADER) dst;
|
|
557 COPY_CHUNK ("Copying section table...", section,
|
63233
|
558 nt_header->FileHeader.NumberOfSections * sizeof (*section),
|
|
559 be_verbose);
|
24102
|
560
|
24237
|
561 /* Align the first section's raw data area, and set the header size
|
|
562 field accordingly. */
|
|
563 ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
|
|
564 dst_nt_header->OptionalHeader.SizeOfHeaders = DST_TO_OFFSET ();
|
|
565
|
24102
|
566 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
|
|
567 {
|
|
568 char msg[100];
|
63233
|
569 /* Windows section names are fixed 8-char strings, only
|
|
570 zero-terminated if the name is shorter than 8 characters. */
|
|
571 sprintf (msg, "Copying raw data for %.8s...", section->Name);
|
24102
|
572
|
|
573 dst_save = dst;
|
|
574
|
|
575 /* Update the file-relative offset for this section's raw data (if
|
|
576 it has any) in case things have been relocated; we will update
|
|
577 the other offsets below once we know where everything is. */
|
|
578 if (dst_section->PointerToRawData)
|
|
579 dst_section->PointerToRawData = DST_TO_OFFSET ();
|
|
580
|
|
581 /* Can always copy the original raw data. */
|
|
582 COPY_CHUNK
|
|
583 (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile),
|
63233
|
584 section->SizeOfRawData, be_verbose);
|
24237
|
585 /* Ensure alignment slop is zeroed. */
|
|
586 ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
|
12245
|
587
|
24102
|
588 /* Note that various sections below may be aliases. */
|
|
589 if (section == data_section)
|
|
590 {
|
|
591 dst = dst_save
|
|
592 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start), dst_section);
|
63233
|
593 COPY_PROC_CHUNK ("Dumping initialized data...",
|
|
594 data_start, data_size, be_verbose);
|
24102
|
595 dst = dst_save + dst_section->SizeOfRawData;
|
|
596 }
|
|
597 if (section == bss_section)
|
|
598 {
|
|
599 /* Dump contents of bss variables, adjusting the section's raw
|
|
600 data size as necessary. */
|
|
601 dst = dst_save
|
|
602 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start), dst_section);
|
63233
|
603 COPY_PROC_CHUNK ("Dumping bss data...", bss_start,
|
|
604 bss_size, be_verbose);
|
24102
|
605 ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
|
|
606 dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
|
|
607 /* Determine new size of raw data area. */
|
|
608 dst = max (dst, dst_save + dst_section->SizeOfRawData);
|
|
609 dst_section->SizeOfRawData = dst - dst_save;
|
|
610 dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
|
|
611 dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
612 }
|
|
613 if (section == bss_section_static)
|
|
614 {
|
|
615 /* Dump contents of static bss variables, adjusting the
|
|
616 section's raw data size as necessary. */
|
|
617 dst = dst_save
|
|
618 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static), dst_section);
|
63233
|
619 COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static,
|
|
620 bss_size_static, be_verbose);
|
24102
|
621 ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
|
|
622 dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
|
|
623 /* Determine new size of raw data area. */
|
|
624 dst = max (dst, dst_save + dst_section->SizeOfRawData);
|
|
625 dst_section->SizeOfRawData = dst - dst_save;
|
|
626 dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
|
|
627 dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
628 }
|
|
629 if (section == heap_section)
|
|
630 {
|
31104
|
631 DWORD heap_start = (DWORD) get_heap_start ();
|
24102
|
632 DWORD heap_size = get_committed_heap_size ();
|
|
633
|
|
634 /* Dump the used portion of the predump heap, adjusting the
|
|
635 section's size to the appropriate size. */
|
|
636 dst = dst_save
|
|
637 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (heap_start), dst_section);
|
63233
|
638 COPY_PROC_CHUNK ("Dumping heap...", heap_start, heap_size,
|
|
639 be_verbose);
|
24102
|
640 ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
|
|
641 dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
|
|
642 /* Determine new size of raw data area. */
|
|
643 dst = max (dst, dst_save + dst_section->SizeOfRawData);
|
|
644 dst_section->SizeOfRawData = dst - dst_save;
|
|
645 /* Reduce the size of the heap section to fit (must be last
|
|
646 section). */
|
|
647 dst_nt_header->OptionalHeader.SizeOfImage -=
|
|
648 dst_section->Misc.VirtualSize
|
|
649 - ROUND_UP (dst_section->SizeOfRawData,
|
|
650 dst_nt_header->OptionalHeader.SectionAlignment);
|
|
651 dst_section->Misc.VirtualSize =
|
|
652 ROUND_UP (dst_section->SizeOfRawData,
|
|
653 dst_nt_header->OptionalHeader.SectionAlignment);
|
|
654 dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
|
|
655 dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
|
|
656 }
|
12245
|
657
|
24237
|
658 /* Align the section's raw data area. */
|
|
659 ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
|
|
660
|
24102
|
661 section++;
|
|
662 dst_section++;
|
|
663 }
|
|
664
|
|
665 /* Copy remainder of source image. */
|
|
666 do
|
|
667 section--;
|
|
668 while (section->PointerToRawData == 0);
|
|
669 offset = ROUND_UP (section->PointerToRawData + section->SizeOfRawData,
|
|
670 nt_header->OptionalHeader.FileAlignment);
|
|
671 COPY_CHUNK
|
|
672 ("Copying remainder of executable...",
|
|
673 OFFSET_TO_PTR (offset, p_infile),
|
63233
|
674 p_infile->size - offset, be_verbose);
|
12245
|
675
|
24102
|
676 /* Final size for new image. */
|
|
677 p_outfile->size = DST_TO_OFFSET ();
|
|
678
|
|
679 /* Now patch up remaining file-relative offsets. */
|
|
680 section = IMAGE_FIRST_SECTION (nt_header);
|
|
681 dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
|
|
682
|
|
683 #define ADJUST_OFFSET(var) \
|
|
684 do { \
|
|
685 if ((var) != 0) \
|
|
686 (var) = relocate_offset ((var), nt_header, dst_nt_header); \
|
|
687 } while (0)
|
12245
|
688
|
24102
|
689 dst_nt_header->OptionalHeader.SizeOfInitializedData = 0;
|
|
690 dst_nt_header->OptionalHeader.SizeOfUninitializedData = 0;
|
|
691 for (i = 0; i < dst_nt_header->FileHeader.NumberOfSections; i++)
|
|
692 {
|
|
693 /* Recompute data sizes for completeness. */
|
|
694 if (dst_section[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
|
|
695 dst_nt_header->OptionalHeader.SizeOfInitializedData +=
|
|
696 ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment);
|
|
697 else if (dst_section[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
|
|
698 dst_nt_header->OptionalHeader.SizeOfUninitializedData +=
|
|
699 ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment);
|
12245
|
700
|
24102
|
701 ADJUST_OFFSET (dst_section[i].PointerToLinenumbers);
|
|
702 }
|
|
703
|
|
704 ADJUST_OFFSET (dst_nt_header->FileHeader.PointerToSymbolTable);
|
12245
|
705
|
24102
|
706 /* Update offsets in debug directory entries. */
|
|
707 {
|
|
708 IMAGE_DATA_DIRECTORY debug_dir =
|
|
709 dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
|
|
710 PIMAGE_DEBUG_DIRECTORY debug_entry;
|
|
711
|
|
712 section = rva_to_section (debug_dir.VirtualAddress, dst_nt_header);
|
|
713 if (section)
|
|
714 {
|
|
715 debug_entry = (PIMAGE_DEBUG_DIRECTORY)
|
|
716 (RVA_TO_OFFSET (debug_dir.VirtualAddress, section) + p_outfile->file_base);
|
|
717 debug_dir.Size /= sizeof (IMAGE_DEBUG_DIRECTORY);
|
|
718
|
|
719 for (i = 0; i < debug_dir.Size; i++, debug_entry++)
|
|
720 ADJUST_OFFSET (debug_entry->PointerToRawData);
|
|
721 }
|
|
722 }
|
12245
|
723 }
|
|
724
|
|
725
|
24102
|
726 /* Dump out .data and .bss sections into a new executable. */
|
21604
|
727 void
|
24102
|
728 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
|
|
729 void *entry_address)
|
21604
|
730 {
|
24102
|
731 file_data in_file, out_file;
|
|
732 char out_filename[MAX_PATH], in_filename[MAX_PATH];
|
|
733 unsigned long size;
|
31104
|
734 char *p;
|
|
735 char *q;
|
|
736
|
|
737 /* Ignore old_name, and get our actual location from the OS. */
|
|
738 if (!GetModuleFileName (NULL, in_filename, MAX_PATH))
|
|
739 abort ();
|
|
740 dostounix_filename (in_filename);
|
|
741 strcpy (out_filename, in_filename);
|
|
742
|
|
743 /* Change the base of the output filename to match the requested name. */
|
|
744 if ((p = strrchr (out_filename, '/')) == NULL)
|
|
745 abort ();
|
|
746 /* The filenames have already been expanded, and will be in Unix
|
|
747 format, so it is safe to expect an absolute name. */
|
|
748 if ((q = strrchr (new_name, '/')) == NULL)
|
|
749 abort ();
|
|
750 strcpy (p, q);
|
49600
|
751
|
31104
|
752 /* Make sure that the output filename has the ".exe" extension...patch
|
|
753 it up if not. */
|
|
754 p = out_filename + strlen (out_filename) - 4;
|
|
755 if (strcmp (p, ".exe"))
|
24102
|
756 strcat (out_filename, ".exe");
|
|
757
|
|
758 printf ("Dumping from %s\n", in_filename);
|
|
759 printf (" to %s\n", out_filename);
|
|
760
|
|
761 /* We need to round off our heap to NT's page size. */
|
|
762 round_heap (get_page_size ());
|
|
763
|
|
764 /* Open the undumped executable file. */
|
|
765 if (!open_input_file (&in_file, in_filename))
|
|
766 {
|
49600
|
767 printf ("Failed to open %s (%d)...bailing.\n",
|
24102
|
768 in_filename, GetLastError ());
|
|
769 exit (1);
|
|
770 }
|
|
771
|
|
772 /* Get the interesting section info, like start and size of .bss... */
|
|
773 get_section_info (&in_file);
|
21604
|
774
|
24102
|
775 /* The size of the dumped executable is the size of the original
|
|
776 executable plus the size of the heap and the size of the .bss section. */
|
|
777 size = in_file.size +
|
|
778 get_committed_heap_size () +
|
|
779 extra_bss_size +
|
|
780 extra_bss_size_static;
|
|
781 if (!open_output_file (&out_file, out_filename, size))
|
|
782 {
|
49600
|
783 printf ("Failed to open %s (%d)...bailing.\n",
|
24102
|
784 out_filename, GetLastError ());
|
|
785 exit (1);
|
|
786 }
|
|
787
|
|
788 /* Set the flag (before dumping). */
|
|
789 using_dynamic_heap = TRUE;
|
|
790
|
|
791 copy_executable_and_dump_data (&in_file, &out_file);
|
21604
|
792
|
24102
|
793 /* Patch up header fields; profiler is picky about this. */
|
|
794 {
|
|
795 PIMAGE_DOS_HEADER dos_header;
|
|
796 PIMAGE_NT_HEADERS nt_header;
|
|
797 HANDLE hImagehelp = LoadLibrary ("imagehlp.dll");
|
|
798 DWORD headersum;
|
|
799 DWORD checksum;
|
|
800
|
|
801 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
|
|
802 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
|
|
803
|
|
804 nt_header->OptionalHeader.CheckSum = 0;
|
|
805 // nt_header->FileHeader.TimeDateStamp = time (NULL);
|
|
806 // dos_header->e_cp = size / 512;
|
|
807 // nt_header->OptionalHeader.SizeOfImage = size;
|
21604
|
808
|
24102
|
809 pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile");
|
|
810 if (pfnCheckSumMappedFile)
|
|
811 {
|
|
812 // nt_header->FileHeader.TimeDateStamp = time (NULL);
|
|
813 pfnCheckSumMappedFile (out_file.file_base,
|
|
814 out_file.size,
|
|
815 &headersum,
|
|
816 &checksum);
|
|
817 nt_header->OptionalHeader.CheckSum = checksum;
|
|
818 }
|
|
819 FreeLibrary (hImagehelp);
|
|
820 }
|
|
821
|
|
822 close_file_data (&in_file);
|
|
823 close_file_data (&out_file);
|
21604
|
824 }
|
12245
|
825
|
24102
|
826 /* eof */
|
52401
|
827
|
|
828 /* arch-tag: fe1d3d1c-ef88-4917-ab22-f12ab16b3254
|
|
829 (do not change this comment) */
|