Commit | Line | Data |
---|---|---|
3b7ad313 | 1 | /* unexec for GNU Emacs on Windows NT. |
2147fb50 KH |
2 | Copyright (C) 1994 Free Software Foundation, Inc. |
3 | ||
3b7ad313 | 4 | This file is part of GNU Emacs. |
2147fb50 | 5 | |
3b7ad313 EN |
6 | GNU Emacs is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
2147fb50 | 10 | |
3b7ad313 EN |
11 | GNU Emacs is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
2147fb50 | 15 | |
3b7ad313 EN |
16 | You should have received a copy of the GNU General Public License |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. | |
2147fb50 KH |
20 | |
21 | Geoff Voelker (voelker@cs.washington.edu) 8-12-94 | |
22 | */ | |
23 | ||
43640c4d GV |
24 | #include <config.h> |
25 | ||
2147fb50 KH |
26 | #include <stdio.h> |
27 | #include <fcntl.h> | |
43640c4d | 28 | #include <time.h> |
2147fb50 KH |
29 | #include <windows.h> |
30 | ||
43640c4d GV |
31 | /* Include relevant definitions from IMAGEHLP.H, which can be found |
32 | in \\win32sdk\mstools\samples\image\include\imagehlp.h. */ | |
2147fb50 | 33 | |
43640c4d GV |
34 | PIMAGE_NT_HEADERS |
35 | (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress, | |
36 | DWORD FileLength, | |
37 | LPDWORD HeaderSum, | |
38 | LPDWORD CheckSum); | |
2147fb50 | 39 | |
43640c4d GV |
40 | extern BOOL ctrl_c_handler (unsigned long type); |
41 | ||
42 | extern char my_begdata[]; | |
43 | extern char my_edata[]; | |
44 | extern char my_begbss[]; | |
45 | extern char my_endbss[]; | |
9c8056fe GV |
46 | extern char *my_begbss_static; |
47 | extern char *my_endbss_static; | |
2147fb50 | 48 | |
43640c4d | 49 | #include "w32heap.h" |
e54c8cd1 | 50 | |
03887dd3 KH |
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 | ||
2147fb50 | 56 | /* Basically, our "initialized" flag. */ |
5b79dba5 | 57 | BOOL using_dynamic_heap = FALSE; |
2147fb50 | 58 | |
43640c4d GV |
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); | |
2147fb50 KH |
61 | void close_file_data (file_data *p_file); |
62 | ||
63 | void get_section_info (file_data *p_file); | |
5b79dba5 | 64 | void copy_executable_and_dump_data (file_data *, file_data *); |
2147fb50 KH |
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. */ | |
5b79dba5 | 68 | PIMAGE_SECTION_HEADER data_section; |
49dc9682 | 69 | PCHAR data_start = 0; |
2147fb50 KH |
70 | DWORD data_size = 0; |
71 | ||
72 | /* Cached info about the .bss section in the executable. */ | |
5b79dba5 | 73 | PIMAGE_SECTION_HEADER bss_section; |
49dc9682 | 74 | PCHAR bss_start = 0; |
2147fb50 | 75 | DWORD bss_size = 0; |
5b79dba5 AI |
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; | |
49dc9682 | 79 | PCHAR bss_start_static = 0; |
5b79dba5 AI |
80 | DWORD bss_size_static = 0; |
81 | DWORD extra_bss_size_static = 0; | |
82 | ||
83 | PIMAGE_SECTION_HEADER heap_section; | |
2147fb50 | 84 | |
cd6885f3 GV |
85 | #ifdef HAVE_NTGUI |
86 | HINSTANCE hinst = NULL; | |
87 | HINSTANCE hprevinst = NULL; | |
88 | LPSTR lpCmdLine = ""; | |
89 | int nCmdShow = 0; | |
cd6885f3 GV |
90 | #endif /* HAVE_NTGUI */ |
91 | ||
2147fb50 KH |
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 | ||
7fef47a3 | 101 | #if 1 |
43640c4d GV |
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 | ||
2147fb50 KH |
108 | /* Cache system info, e.g., the NT page size. */ |
109 | cache_system_info (); | |
110 | ||
5b79dba5 AI |
111 | /* Grab our malloc arena space now, before CRT starts up. */ |
112 | init_heap (); | |
2147fb50 | 113 | |
2147fb50 KH |
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 | ||
467af476 AI |
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 | ||
2147fb50 KH |
122 | /* Invoke the NT CRT startup routine now that our housecleaning |
123 | is finished. */ | |
cd6885f3 | 124 | #ifdef HAVE_NTGUI |
c2ccbd43 GV |
125 | /* determine WinMain args like crt0.c does */ |
126 | hinst = GetModuleHandle(NULL); | |
127 | lpCmdLine = GetCommandLine(); | |
128 | nCmdShow = SW_SHOWDEFAULT; | |
129 | #endif | |
2147fb50 KH |
130 | mainCRTStartup (); |
131 | } | |
132 | ||
2147fb50 KH |
133 | |
134 | /* File handling. */ | |
135 | ||
43640c4d | 136 | int |
2147fb50 KH |
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); | |
177c0ea7 | 146 | if (file == INVALID_HANDLE_VALUE) |
43640c4d | 147 | return FALSE; |
2147fb50 KH |
148 | |
149 | size = GetFileSize (file, &upper_size); | |
177c0ea7 | 150 | file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY, |
2147fb50 | 151 | 0, size, NULL); |
177c0ea7 | 152 | if (!file_mapping) |
43640c4d | 153 | return FALSE; |
2147fb50 KH |
154 | |
155 | file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size); | |
177c0ea7 | 156 | if (file_base == 0) |
43640c4d | 157 | return FALSE; |
2147fb50 KH |
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; | |
43640c4d GV |
164 | |
165 | return TRUE; | |
2147fb50 KH |
166 | } |
167 | ||
43640c4d | 168 | int |
2147fb50 KH |
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; | |
cd6885f3 | 174 | |
2147fb50 KH |
175 | file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
176 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); | |
177c0ea7 | 177 | if (file == INVALID_HANDLE_VALUE) |
43640c4d GV |
178 | return FALSE; |
179 | ||
177c0ea7 | 180 | file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE, |
2147fb50 | 181 | 0, size, NULL); |
177c0ea7 | 182 | if (!file_mapping) |
43640c4d | 183 | return FALSE; |
177c0ea7 | 184 | |
2147fb50 | 185 | file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size); |
177c0ea7 | 186 | if (file_base == 0) |
43640c4d | 187 | return FALSE; |
177c0ea7 | 188 | |
2147fb50 KH |
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; | |
43640c4d GV |
194 | |
195 | return TRUE; | |
2147fb50 KH |
196 | } |
197 | ||
198 | /* Close the system structures associated with the given file. */ | |
43640c4d | 199 | void |
2147fb50 KH |
200 | close_file_data (file_data *p_file) |
201 | { | |
5b79dba5 AI |
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); | |
2147fb50 KH |
208 | } |
209 | ||
210 | ||
211 | /* Routines to manipulate NT executable file sections. */ | |
212 | ||
43640c4d GV |
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++) | |
223 | { | |
224 | if (strcmp (section->Name, name) == 0) | |
225 | return section; | |
226 | section++; | |
227 | } | |
228 | return NULL; | |
229 | } | |
230 | ||
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++) | |
2147fb50 | 242 | { |
5b79dba5 AI |
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); | |
43640c4d | 251 | if (rva >= section->VirtualAddress |
5b79dba5 AI |
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) | |
43640c4d GV |
273 | return section; |
274 | section++; | |
2147fb50 | 275 | } |
43640c4d | 276 | return NULL; |
2147fb50 KH |
277 | } |
278 | ||
5b79dba5 AI |
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). */ | |
49dc9682 | 282 | DWORD |
5b79dba5 AI |
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 | ||
328 | #define PTR_TO_OFFSET(ptr, pfile_data) \ | |
49dc9682 | 329 | ((unsigned char *)(ptr) - (pfile_data)->file_base) |
5b79dba5 AI |
330 | |
331 | #define OFFSET_TO_PTR(offset, pfile_data) \ | |
332 | ((pfile_data)->file_base + (DWORD)(offset)) | |
333 | ||
43640c4d | 334 | |
2147fb50 | 335 | /* Flip through the executable and cache the info necessary for dumping. */ |
49dc9682 | 336 | void |
2147fb50 KH |
337 | get_section_info (file_data *p_infile) |
338 | { | |
339 | PIMAGE_DOS_HEADER dos_header; | |
340 | PIMAGE_NT_HEADERS nt_header; | |
5b79dba5 AI |
341 | PIMAGE_SECTION_HEADER section; |
342 | int overlap; | |
177c0ea7 | 343 | |
2147fb50 | 344 | dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base; |
177c0ea7 | 345 | if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) |
2147fb50 KH |
346 | { |
347 | printf ("Unknown EXE header in %s...bailing.\n", p_infile->name); | |
348 | exit (1); | |
349 | } | |
177c0ea7 | 350 | nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + |
2147fb50 | 351 | dos_header->e_lfanew); |
177c0ea7 | 352 | if (nt_header == NULL) |
2147fb50 | 353 | { |
177c0ea7 | 354 | printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n", |
2147fb50 KH |
355 | p_infile->name); |
356 | exit (1); | |
357 | } | |
358 | ||
359 | /* Check the NT header signature ... */ | |
177c0ea7 | 360 | if (nt_header->Signature != IMAGE_NT_SIGNATURE) |
2147fb50 KH |
361 | { |
362 | printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n", | |
363 | nt_header->Signature, p_infile->name); | |
5b79dba5 | 364 | exit (1); |
2147fb50 KH |
365 | } |
366 | ||
5b79dba5 AI |
367 | /* Locate the ".data" and ".bss" sections for Emacs. (Note that the |
368 | actual section names are probably different from these, and might | |
369 | actually be the same section.) | |
370 | ||
371 | We do this as follows: first we determine the virtual address | |
372 | ranges in this process for the data and bss variables that we wish | |
373 | to preserve. Then we map these VAs to the section entries in the | |
374 | source image. Finally, we determine the new size of the raw data | |
375 | area for the bss section, so we can make the new image the correct | |
376 | size. */ | |
377 | ||
e3ddd18c AI |
378 | /* We arrange for the Emacs initialized data to be in a separate |
379 | section if possible, because we cannot rely on my_begdata and | |
380 | my_edata marking out the full extent of the initialized data, at | |
381 | least on the Alpha where the linker freely reorders variables | |
382 | across libraries. If we can arrange for this, all we need to do is | |
383 | find the start and size of the EMDATA section. */ | |
384 | data_section = find_section ("EMDATA", nt_header); | |
385 | if (data_section) | |
2147fb50 | 386 | { |
e3ddd18c AI |
387 | data_start = (char *) nt_header->OptionalHeader.ImageBase + |
388 | data_section->VirtualAddress; | |
389 | data_size = data_section->Misc.VirtualSize; | |
390 | } | |
391 | else | |
392 | { | |
393 | /* Fallback on the old method if compiler doesn't support the | |
394 | data_set #pragma (or its equivalent). */ | |
395 | data_start = my_begdata; | |
396 | data_size = my_edata - my_begdata; | |
397 | data_section = rva_to_section (PTR_TO_RVA (my_begdata), nt_header); | |
398 | if (data_section != rva_to_section (PTR_TO_RVA (my_edata), nt_header)) | |
399 | { | |
400 | printf ("Initialized data is not in a single section...bailing\n"); | |
401 | exit (1); | |
402 | } | |
5b79dba5 AI |
403 | } |
404 | ||
405 | /* As noted in lastfile.c, the Alpha (but not the Intel) MSVC linker | |
406 | globally segregates all static and public bss data (ie. across all | |
407 | linked modules, not just per module), so we must take both static | |
408 | and public bss areas into account to determine the true extent of | |
409 | the bss area used by Emacs. | |
410 | ||
411 | To be strictly correct, we dump the static and public bss areas | |
412 | used by Emacs separately if non-overlapping (since otherwise we are | |
413 | dumping bss data belonging to system libraries, eg. the static bss | |
414 | system data on the Alpha). */ | |
415 | ||
416 | bss_start = my_begbss; | |
417 | bss_size = my_endbss - my_begbss; | |
418 | bss_section = rva_to_section (PTR_TO_RVA (my_begbss), nt_header); | |
419 | if (bss_section != rva_to_section (PTR_TO_RVA (my_endbss), nt_header)) | |
420 | { | |
421 | printf ("Uninitialized data is not in a single section...bailing\n"); | |
422 | exit (1); | |
423 | } | |
424 | /* Compute how much the .bss section's raw data will grow. */ | |
425 | extra_bss_size = | |
426 | ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss), bss_section), | |
427 | nt_header->OptionalHeader.FileAlignment) | |
428 | - bss_section->SizeOfRawData; | |
429 | ||
430 | bss_start_static = my_begbss_static; | |
431 | bss_size_static = my_endbss_static - my_begbss_static; | |
432 | bss_section_static = rva_to_section (PTR_TO_RVA (my_begbss_static), nt_header); | |
433 | if (bss_section_static != rva_to_section (PTR_TO_RVA (my_endbss_static), nt_header)) | |
434 | { | |
435 | printf ("Uninitialized static data is not in a single section...bailing\n"); | |
436 | exit (1); | |
437 | } | |
438 | /* Compute how much the static .bss section's raw data will grow. */ | |
439 | extra_bss_size_static = | |
440 | ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss_static), bss_section_static), | |
441 | nt_header->OptionalHeader.FileAlignment) | |
442 | - bss_section_static->SizeOfRawData; | |
443 | ||
444 | /* Combine the bss sections into one if they overlap. */ | |
972ee7e0 AI |
445 | #ifdef _ALPHA_ |
446 | overlap = 1; /* force all bss data to be dumped */ | |
447 | #else | |
5b79dba5 | 448 | overlap = 0; |
972ee7e0 | 449 | #endif |
5b79dba5 AI |
450 | if (bss_start < bss_start_static) |
451 | { | |
452 | if (bss_start_static < bss_start + bss_size) | |
453 | overlap = 1; | |
454 | } | |
455 | else | |
456 | { | |
457 | if (bss_start < bss_start_static + bss_size_static) | |
458 | overlap = 1; | |
459 | } | |
460 | if (overlap) | |
461 | { | |
462 | if (bss_section != bss_section_static) | |
2147fb50 | 463 | { |
5b79dba5 AI |
464 | printf ("BSS data not in a single section...bailing\n"); |
465 | exit (1); | |
2147fb50 | 466 | } |
5b79dba5 AI |
467 | bss_start = min (bss_start, bss_start_static); |
468 | bss_size = max (my_endbss, my_endbss_static) - bss_start; | |
469 | bss_section_static = 0; | |
470 | extra_bss_size_static = 0; | |
471 | } | |
472 | ||
473 | heap_section = rva_to_section (PTR_TO_RVA (get_heap_start ()), nt_header); | |
474 | } | |
475 | ||
476 | ||
477 | /* The dump routines. */ | |
478 | ||
49dc9682 | 479 | void |
177c0ea7 | 480 | copy_executable_and_dump_data (file_data *p_infile, |
5b79dba5 AI |
481 | file_data *p_outfile) |
482 | { | |
483 | unsigned char *dst, *dst_save; | |
484 | PIMAGE_DOS_HEADER dos_header; | |
485 | PIMAGE_NT_HEADERS nt_header; | |
486 | PIMAGE_NT_HEADERS dst_nt_header; | |
487 | PIMAGE_SECTION_HEADER section; | |
488 | PIMAGE_SECTION_HEADER dst_section; | |
489 | DWORD offset; | |
490 | int i; | |
491 | ||
492 | #define COPY_CHUNK(message, src, size) \ | |
493 | do { \ | |
494 | unsigned char *s = (void *)(src); \ | |
495 | unsigned long count = (size); \ | |
496 | printf ("%s\n", (message)); \ | |
497 | printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \ | |
498 | printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \ | |
499 | printf ("\t0x%08x Size in bytes.\n", count); \ | |
500 | memcpy (dst, s, count); \ | |
501 | dst += count; \ | |
502 | } while (0) | |
503 | ||
504 | #define COPY_PROC_CHUNK(message, src, size) \ | |
505 | do { \ | |
506 | unsigned char *s = (void *)(src); \ | |
507 | unsigned long count = (size); \ | |
508 | printf ("%s\n", (message)); \ | |
509 | printf ("\t0x%08x Address in process.\n", s); \ | |
510 | printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \ | |
511 | printf ("\t0x%08x Size in bytes.\n", count); \ | |
512 | memcpy (dst, s, count); \ | |
513 | dst += count; \ | |
514 | } while (0) | |
515 | ||
516 | #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile) | |
517 | #define ROUND_UP_DST(align) \ | |
518 | (dst = p_outfile->file_base + ROUND_UP (DST_TO_OFFSET (), (align))) | |
7fef47a3 AI |
519 | #define ROUND_UP_DST_AND_ZERO(align) \ |
520 | do { \ | |
521 | unsigned char *newdst = p_outfile->file_base \ | |
522 | + ROUND_UP (DST_TO_OFFSET (), (align)); \ | |
523 | /* Zero the alignment slop; it may actually initialize real data. */ \ | |
524 | memset (dst, 0, newdst - dst); \ | |
525 | dst = newdst; \ | |
526 | } while (0) | |
5b79dba5 AI |
527 | |
528 | /* Copy the source image sequentially, ie. section by section after | |
529 | copying the headers and section table, to simplify the process of | |
530 | dumping the raw data for the bss and heap sections. | |
531 | ||
532 | Note that dst is updated implicitly by each COPY_CHUNK. */ | |
533 | ||
534 | dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base; | |
177c0ea7 | 535 | nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + |
5b79dba5 AI |
536 | dos_header->e_lfanew); |
537 | section = IMAGE_FIRST_SECTION (nt_header); | |
177c0ea7 | 538 | |
5b79dba5 AI |
539 | dst = (unsigned char *) p_outfile->file_base; |
540 | ||
541 | COPY_CHUNK ("Copying DOS header...", dos_header, | |
542 | (DWORD) nt_header - (DWORD) dos_header); | |
543 | dst_nt_header = (PIMAGE_NT_HEADERS) dst; | |
544 | COPY_CHUNK ("Copying NT header...", nt_header, | |
545 | (DWORD) section - (DWORD) nt_header); | |
546 | dst_section = (PIMAGE_SECTION_HEADER) dst; | |
547 | COPY_CHUNK ("Copying section table...", section, | |
548 | nt_header->FileHeader.NumberOfSections * sizeof (*section)); | |
549 | ||
7fef47a3 AI |
550 | /* Align the first section's raw data area, and set the header size |
551 | field accordingly. */ | |
552 | ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment); | |
553 | dst_nt_header->OptionalHeader.SizeOfHeaders = DST_TO_OFFSET (); | |
554 | ||
5b79dba5 AI |
555 | for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) |
556 | { | |
557 | char msg[100]; | |
558 | sprintf (msg, "Copying raw data for %s...", section->Name); | |
559 | ||
5b79dba5 AI |
560 | dst_save = dst; |
561 | ||
562 | /* Update the file-relative offset for this section's raw data (if | |
563 | it has any) in case things have been relocated; we will update | |
564 | the other offsets below once we know where everything is. */ | |
565 | if (dst_section->PointerToRawData) | |
566 | dst_section->PointerToRawData = DST_TO_OFFSET (); | |
567 | ||
568 | /* Can always copy the original raw data. */ | |
569 | COPY_CHUNK | |
570 | (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile), | |
571 | section->SizeOfRawData); | |
7fef47a3 AI |
572 | /* Ensure alignment slop is zeroed. */ |
573 | ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment); | |
5b79dba5 AI |
574 | |
575 | /* Note that various sections below may be aliases. */ | |
576 | if (section == data_section) | |
2147fb50 | 577 | { |
5b79dba5 AI |
578 | dst = dst_save |
579 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start), dst_section); | |
580 | COPY_PROC_CHUNK ("Dumping initialized data...", data_start, data_size); | |
581 | dst = dst_save + dst_section->SizeOfRawData; | |
2147fb50 | 582 | } |
5b79dba5 | 583 | if (section == bss_section) |
43640c4d | 584 | { |
5b79dba5 AI |
585 | /* Dump contents of bss variables, adjusting the section's raw |
586 | data size as necessary. */ | |
587 | dst = dst_save | |
588 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start), dst_section); | |
589 | COPY_PROC_CHUNK ("Dumping bss data...", bss_start, bss_size); | |
590 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); | |
591 | dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile); | |
592 | /* Determine new size of raw data area. */ | |
593 | dst = max (dst, dst_save + dst_section->SizeOfRawData); | |
594 | dst_section->SizeOfRawData = dst - dst_save; | |
595 | dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA; | |
596 | dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA; | |
43640c4d | 597 | } |
5b79dba5 AI |
598 | if (section == bss_section_static) |
599 | { | |
600 | /* Dump contents of static bss variables, adjusting the | |
601 | section's raw data size as necessary. */ | |
602 | dst = dst_save | |
603 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static), dst_section); | |
604 | COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static, bss_size_static); | |
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 == heap_section) | |
614 | { | |
49dc9682 | 615 | DWORD heap_start = (DWORD) get_heap_start (); |
5b79dba5 AI |
616 | DWORD heap_size = get_committed_heap_size (); |
617 | ||
618 | /* Dump the used portion of the predump heap, adjusting the | |
619 | section's size to the appropriate size. */ | |
620 | dst = dst_save | |
621 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (heap_start), dst_section); | |
622 | COPY_PROC_CHUNK ("Dumping heap...", heap_start, heap_size); | |
623 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); | |
624 | dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile); | |
625 | /* Determine new size of raw data area. */ | |
626 | dst = max (dst, dst_save + dst_section->SizeOfRawData); | |
627 | dst_section->SizeOfRawData = dst - dst_save; | |
628 | /* Reduce the size of the heap section to fit (must be last | |
629 | section). */ | |
630 | dst_nt_header->OptionalHeader.SizeOfImage -= | |
631 | dst_section->Misc.VirtualSize | |
632 | - ROUND_UP (dst_section->SizeOfRawData, | |
633 | dst_nt_header->OptionalHeader.SectionAlignment); | |
634 | dst_section->Misc.VirtualSize = | |
635 | ROUND_UP (dst_section->SizeOfRawData, | |
636 | dst_nt_header->OptionalHeader.SectionAlignment); | |
637 | dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA; | |
638 | dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA; | |
639 | } | |
640 | ||
7fef47a3 AI |
641 | /* Align the section's raw data area. */ |
642 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); | |
643 | ||
2147fb50 | 644 | section++; |
5b79dba5 | 645 | dst_section++; |
2147fb50 | 646 | } |
a610993d | 647 | |
5b79dba5 AI |
648 | /* Copy remainder of source image. */ |
649 | do | |
650 | section--; | |
651 | while (section->PointerToRawData == 0); | |
652 | offset = ROUND_UP (section->PointerToRawData + section->SizeOfRawData, | |
653 | nt_header->OptionalHeader.FileAlignment); | |
654 | COPY_CHUNK | |
655 | ("Copying remainder of executable...", | |
656 | OFFSET_TO_PTR (offset, p_infile), | |
657 | p_infile->size - offset); | |
658 | ||
659 | /* Final size for new image. */ | |
660 | p_outfile->size = DST_TO_OFFSET (); | |
661 | ||
662 | /* Now patch up remaining file-relative offsets. */ | |
663 | section = IMAGE_FIRST_SECTION (nt_header); | |
664 | dst_section = IMAGE_FIRST_SECTION (dst_nt_header); | |
665 | ||
666 | #define ADJUST_OFFSET(var) \ | |
667 | do { \ | |
668 | if ((var) != 0) \ | |
669 | (var) = relocate_offset ((var), nt_header, dst_nt_header); \ | |
670 | } while (0) | |
671 | ||
672 | dst_nt_header->OptionalHeader.SizeOfInitializedData = 0; | |
673 | dst_nt_header->OptionalHeader.SizeOfUninitializedData = 0; | |
674 | for (i = 0; i < dst_nt_header->FileHeader.NumberOfSections; i++) | |
a610993d | 675 | { |
5b79dba5 AI |
676 | /* Recompute data sizes for completeness. */ |
677 | if (dst_section[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) | |
678 | dst_nt_header->OptionalHeader.SizeOfInitializedData += | |
679 | ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment); | |
680 | else if (dst_section[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA) | |
681 | dst_nt_header->OptionalHeader.SizeOfUninitializedData += | |
682 | ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment); | |
683 | ||
684 | ADJUST_OFFSET (dst_section[i].PointerToLinenumbers); | |
a610993d | 685 | } |
2147fb50 | 686 | |
5b79dba5 | 687 | ADJUST_OFFSET (dst_nt_header->FileHeader.PointerToSymbolTable); |
2147fb50 | 688 | |
5b79dba5 AI |
689 | /* Update offsets in debug directory entries. */ |
690 | { | |
691 | IMAGE_DATA_DIRECTORY debug_dir = | |
692 | dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG]; | |
693 | PIMAGE_DEBUG_DIRECTORY debug_entry; | |
2147fb50 | 694 | |
5b79dba5 AI |
695 | section = rva_to_section (debug_dir.VirtualAddress, dst_nt_header); |
696 | if (section) | |
697 | { | |
698 | debug_entry = (PIMAGE_DEBUG_DIRECTORY) | |
699 | (RVA_TO_OFFSET (debug_dir.VirtualAddress, section) + p_outfile->file_base); | |
700 | debug_dir.Size /= sizeof (IMAGE_DEBUG_DIRECTORY); | |
2147fb50 | 701 | |
5b79dba5 AI |
702 | for (i = 0; i < debug_dir.Size; i++, debug_entry++) |
703 | ADJUST_OFFSET (debug_entry->PointerToRawData); | |
704 | } | |
705 | } | |
2147fb50 KH |
706 | } |
707 | ||
708 | ||
5b79dba5 | 709 | /* Dump out .data and .bss sections into a new executable. */ |
9c8056fe | 710 | void |
5b79dba5 AI |
711 | unexec (char *new_name, char *old_name, void *start_data, void *start_bss, |
712 | void *entry_address) | |
9c8056fe | 713 | { |
5b79dba5 AI |
714 | file_data in_file, out_file; |
715 | char out_filename[MAX_PATH], in_filename[MAX_PATH]; | |
716 | unsigned long size; | |
49dc9682 AI |
717 | char *p; |
718 | char *q; | |
719 | ||
720 | /* Ignore old_name, and get our actual location from the OS. */ | |
721 | if (!GetModuleFileName (NULL, in_filename, MAX_PATH)) | |
722 | abort (); | |
723 | dostounix_filename (in_filename); | |
724 | strcpy (out_filename, in_filename); | |
725 | ||
726 | /* Change the base of the output filename to match the requested name. */ | |
727 | if ((p = strrchr (out_filename, '/')) == NULL) | |
728 | abort (); | |
729 | /* The filenames have already been expanded, and will be in Unix | |
730 | format, so it is safe to expect an absolute name. */ | |
731 | if ((q = strrchr (new_name, '/')) == NULL) | |
732 | abort (); | |
733 | strcpy (p, q); | |
177c0ea7 | 734 | |
49dc9682 AI |
735 | /* Make sure that the output filename has the ".exe" extension...patch |
736 | it up if not. */ | |
737 | p = out_filename + strlen (out_filename) - 4; | |
738 | if (strcmp (p, ".exe")) | |
5b79dba5 | 739 | strcat (out_filename, ".exe"); |
2147fb50 | 740 | |
5b79dba5 AI |
741 | printf ("Dumping from %s\n", in_filename); |
742 | printf (" to %s\n", out_filename); | |
2147fb50 | 743 | |
5b79dba5 AI |
744 | /* We need to round off our heap to NT's page size. */ |
745 | round_heap (get_page_size ()); | |
198fdd15 | 746 | |
5b79dba5 AI |
747 | /* Open the undumped executable file. */ |
748 | if (!open_input_file (&in_file, in_filename)) | |
749 | { | |
177c0ea7 | 750 | printf ("Failed to open %s (%d)...bailing.\n", |
5b79dba5 AI |
751 | in_filename, GetLastError ()); |
752 | exit (1); | |
753 | } | |
2147fb50 | 754 | |
5b79dba5 AI |
755 | /* Get the interesting section info, like start and size of .bss... */ |
756 | get_section_info (&in_file); | |
2147fb50 | 757 | |
5b79dba5 AI |
758 | /* The size of the dumped executable is the size of the original |
759 | executable plus the size of the heap and the size of the .bss section. */ | |
760 | size = in_file.size + | |
761 | get_committed_heap_size () + | |
762 | extra_bss_size + | |
763 | extra_bss_size_static; | |
764 | if (!open_output_file (&out_file, out_filename, size)) | |
198fdd15 | 765 | { |
177c0ea7 | 766 | printf ("Failed to open %s (%d)...bailing.\n", |
5b79dba5 AI |
767 | out_filename, GetLastError ()); |
768 | exit (1); | |
198fdd15 GV |
769 | } |
770 | ||
5b79dba5 AI |
771 | /* Set the flag (before dumping). */ |
772 | using_dynamic_heap = TRUE; | |
198fdd15 | 773 | |
5b79dba5 | 774 | copy_executable_and_dump_data (&in_file, &out_file); |
198fdd15 | 775 | |
5b79dba5 AI |
776 | /* Patch up header fields; profiler is picky about this. */ |
777 | { | |
778 | PIMAGE_DOS_HEADER dos_header; | |
779 | PIMAGE_NT_HEADERS nt_header; | |
780 | HANDLE hImagehelp = LoadLibrary ("imagehlp.dll"); | |
781 | DWORD headersum; | |
782 | DWORD checksum; | |
198fdd15 | 783 | |
5b79dba5 AI |
784 | dos_header = (PIMAGE_DOS_HEADER) out_file.file_base; |
785 | nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew); | |
198fdd15 | 786 | |
5b79dba5 AI |
787 | nt_header->OptionalHeader.CheckSum = 0; |
788 | // nt_header->FileHeader.TimeDateStamp = time (NULL); | |
789 | // dos_header->e_cp = size / 512; | |
790 | // nt_header->OptionalHeader.SizeOfImage = size; | |
791 | ||
792 | pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile"); | |
793 | if (pfnCheckSumMappedFile) | |
794 | { | |
795 | // nt_header->FileHeader.TimeDateStamp = time (NULL); | |
796 | pfnCheckSumMappedFile (out_file.file_base, | |
797 | out_file.size, | |
798 | &headersum, | |
799 | &checksum); | |
800 | nt_header->OptionalHeader.CheckSum = checksum; | |
801 | } | |
802 | FreeLibrary (hImagehelp); | |
803 | } | |
198fdd15 | 804 | |
5b79dba5 AI |
805 | close_file_data (&in_file); |
806 | close_file_data (&out_file); | |
2147fb50 | 807 | } |
5b79dba5 AI |
808 | |
809 | /* eof */ | |
ab5796a9 MB |
810 | |
811 | /* arch-tag: fe1d3d1c-ef88-4917-ab22-f12ab16b3254 | |
812 | (do not change this comment) */ |