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