Commit | Line | Data |
---|---|---|
3b7ad313 | 1 | /* unexec for GNU Emacs on Windows NT. |
acaf905b | 2 | Copyright (C) 1994, 2001-2012 Free Software Foundation, Inc. |
2147fb50 | 3 | |
3b7ad313 | 4 | This file is part of GNU Emacs. |
2147fb50 | 5 | |
9ec0b715 | 6 | GNU Emacs is free software: you can redistribute it and/or modify |
3b7ad313 | 7 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
8 | the Free Software Foundation, either version 3 of the License, or |
9 | (at your option) 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 | 16 | You should have received a copy of the GNU General Public License |
9ec0b715 | 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
2147fb50 | 18 | |
9ec0b715 | 19 | /* |
2147fb50 KH |
20 | Geoff Voelker (voelker@cs.washington.edu) 8-12-94 |
21 | */ | |
22 | ||
43640c4d | 23 | #include <config.h> |
ce701a33 | 24 | #include "unexec.h" |
501199a3 | 25 | #include "w32common.h" |
43640c4d | 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; |
62aba0d4 | 71 | DWORD_PTR data_size = 0; |
2147fb50 KH |
72 | |
73 | /* Cached info about the .bss section in the executable. */ | |
5b79dba5 | 74 | PIMAGE_SECTION_HEADER bss_section; |
49dc9682 | 75 | PCHAR bss_start = 0; |
62aba0d4 FP |
76 | DWORD_PTR bss_size = 0; |
77 | DWORD_PTR extra_bss_size = 0; | |
5b79dba5 AI |
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; |
62aba0d4 FP |
81 | DWORD_PTR bss_size_static = 0; |
82 | DWORD_PTR extra_bss_size_static = 0; | |
5b79dba5 AI |
83 | |
84 | PIMAGE_SECTION_HEADER heap_section; | |
2147fb50 | 85 | |
cd6885f3 | 86 | #ifdef HAVE_NTGUI |
0fda9b75 | 87 | extern HINSTANCE hinst; |
cd6885f3 GV |
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 | 126 | /* determine WinMain args like crt0.c does */ |
5e617bc2 JB |
127 | hinst = GetModuleHandle (NULL); |
128 | lpCmdLine = GetCommandLine (); | |
c2ccbd43 GV |
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 * | |
62aba0d4 | 235 | rva_to_section (DWORD_PTR rva, IMAGE_NT_HEADERS * nt_header) |
43640c4d GV |
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. */ | |
62aba0d4 | 250 | DWORD_PTR real_size = max (section->SizeOfRawData, |
5b79dba5 | 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 * | |
62aba0d4 | 263 | offset_to_section (DWORD_PTR offset, IMAGE_NT_HEADERS * nt_header) |
5b79dba5 AI |
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). */ | |
62aba0d4 FP |
283 | DWORD_PTR |
284 | relocate_offset (DWORD_PTR offset, | |
5b79dba5 AI |
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) \ | |
2d7d1608 | 318 | ((section)->VirtualAddress + ((DWORD_PTR)(offset) - (section)->PointerToRawData)) |
5b79dba5 AI |
319 | |
320 | #define RVA_TO_OFFSET(rva, section) \ | |
2d7d1608 | 321 | ((section)->PointerToRawData + ((DWORD_PTR)(rva) - (section)->VirtualAddress)) |
5b79dba5 AI |
322 | |
323 | #define RVA_TO_SECTION_OFFSET(rva, section) \ | |
2d7d1608 | 324 | ((DWORD_PTR)(rva) - (section)->VirtualAddress) |
5b79dba5 AI |
325 | |
326 | /* Convert address in executing image to RVA. */ | |
62aba0d4 | 327 | #define PTR_TO_RVA(ptr) ((DWORD_PTR)(ptr) - (DWORD_PTR) GetModuleHandle (NULL)) |
5b79dba5 | 328 | |
e9bdb9c9 | 329 | #define RVA_TO_PTR(var,section,filedata) \ |
2d7d1608 | 330 | ((unsigned char *)(RVA_TO_OFFSET (var,section) + (filedata).file_base)) |
e9bdb9c9 | 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) \ | |
62aba0d4 | 336 | ((pfile_data)->file_base + (DWORD_PTR)(offset)) |
5b79dba5 | 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 | 345 | int overlap; |
177c0ea7 | 346 | |
2147fb50 | 347 | dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base; |
177c0ea7 | 348 | if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) |
2147fb50 KH |
349 | { |
350 | printf ("Unknown EXE header in %s...bailing.\n", p_infile->name); | |
351 | exit (1); | |
352 | } | |
62aba0d4 | 353 | nt_header = (PIMAGE_NT_HEADERS) (((DWORD_PTR) dos_header) + |
2147fb50 | 354 | dos_header->e_lfanew); |
177c0ea7 | 355 | if (nt_header == NULL) |
2147fb50 | 356 | { |
177c0ea7 | 357 | printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n", |
2147fb50 KH |
358 | p_infile->name); |
359 | exit (1); | |
360 | } | |
361 | ||
362 | /* Check the NT header signature ... */ | |
177c0ea7 | 363 | if (nt_header->Signature != IMAGE_NT_SIGNATURE) |
2147fb50 KH |
364 | { |
365 | printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n", | |
366 | nt_header->Signature, p_infile->name); | |
5b79dba5 | 367 | exit (1); |
2147fb50 KH |
368 | } |
369 | ||
5b79dba5 AI |
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 | ||
e3ddd18c AI |
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) | |
2147fb50 | 389 | { |
e3ddd18c AI |
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 | } | |
5b79dba5 AI |
406 | } |
407 | ||
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)) | |
423 | { | |
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; | |
432 | ||
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; | |
446 | ||
447 | /* Combine the bss sections into one if they overlap. */ | |
972ee7e0 AI |
448 | #ifdef _ALPHA_ |
449 | overlap = 1; /* force all bss data to be dumped */ | |
450 | #else | |
5b79dba5 | 451 | overlap = 0; |
972ee7e0 | 452 | #endif |
5b79dba5 AI |
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) | |
2147fb50 | 466 | { |
5b79dba5 AI |
467 | printf ("BSS data not in a single section...bailing\n"); |
468 | exit (1); | |
2147fb50 | 469 | } |
5b79dba5 AI |
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; | |
474 | } | |
475 | ||
476 | heap_section = rva_to_section (PTR_TO_RVA (get_heap_start ()), nt_header); | |
477 | } | |
478 | ||
479 | ||
480 | /* The dump routines. */ | |
481 | ||
49dc9682 | 482 | void |
177c0ea7 | 483 | copy_executable_and_dump_data (file_data *p_infile, |
5b79dba5 AI |
484 | file_data *p_outfile) |
485 | { | |
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; | |
62aba0d4 | 492 | DWORD_PTR offset; |
5b79dba5 | 493 | int i; |
4162f25f | 494 | int be_verbose = GetEnvironmentVariable ("DEBUG_DUMP", NULL, 0) > 0; |
5b79dba5 | 495 | |
4162f25f | 496 | #define COPY_CHUNK(message, src, size, verbose) \ |
5b79dba5 AI |
497 | do { \ |
498 | unsigned char *s = (void *)(src); \ | |
499 | unsigned long count = (size); \ | |
4162f25f EZ |
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 | } \ | |
5b79dba5 AI |
507 | memcpy (dst, s, count); \ |
508 | dst += count; \ | |
509 | } while (0) | |
510 | ||
4162f25f | 511 | #define COPY_PROC_CHUNK(message, src, size, verbose) \ |
5b79dba5 AI |
512 | do { \ |
513 | unsigned char *s = (void *)(src); \ | |
514 | unsigned long count = (size); \ | |
4162f25f EZ |
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 | } \ | |
5b79dba5 AI |
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))) | |
7fef47a3 AI |
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) | |
5b79dba5 AI |
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; | |
62aba0d4 | 545 | nt_header = (PIMAGE_NT_HEADERS) (((DWORD_PTR) dos_header) + |
5b79dba5 AI |
546 | dos_header->e_lfanew); |
547 | section = IMAGE_FIRST_SECTION (nt_header); | |
177c0ea7 | 548 | |
5b79dba5 AI |
549 | dst = (unsigned char *) p_outfile->file_base; |
550 | ||
551 | COPY_CHUNK ("Copying DOS header...", dos_header, | |
62aba0d4 | 552 | (DWORD_PTR) nt_header - (DWORD_PTR) dos_header, be_verbose); |
5b79dba5 AI |
553 | dst_nt_header = (PIMAGE_NT_HEADERS) dst; |
554 | COPY_CHUNK ("Copying NT header...", nt_header, | |
62aba0d4 | 555 | (DWORD_PTR) section - (DWORD_PTR) nt_header, be_verbose); |
5b79dba5 AI |
556 | dst_section = (PIMAGE_SECTION_HEADER) dst; |
557 | COPY_CHUNK ("Copying section table...", section, | |
4162f25f EZ |
558 | nt_header->FileHeader.NumberOfSections * sizeof (*section), |
559 | be_verbose); | |
5b79dba5 | 560 | |
7fef47a3 AI |
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 | ||
5b79dba5 AI |
566 | for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) |
567 | { | |
568 | char msg[100]; | |
4162f25f EZ |
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); | |
5b79dba5 | 572 | |
5b79dba5 AI |
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), | |
4162f25f | 584 | section->SizeOfRawData, be_verbose); |
7fef47a3 AI |
585 | /* Ensure alignment slop is zeroed. */ |
586 | ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment); | |
5b79dba5 AI |
587 | |
588 | /* Note that various sections below may be aliases. */ | |
589 | if (section == data_section) | |
2147fb50 | 590 | { |
5b79dba5 AI |
591 | dst = dst_save |
592 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start), dst_section); | |
4162f25f EZ |
593 | COPY_PROC_CHUNK ("Dumping initialized data...", |
594 | data_start, data_size, be_verbose); | |
5b79dba5 | 595 | dst = dst_save + dst_section->SizeOfRawData; |
2147fb50 | 596 | } |
5b79dba5 | 597 | if (section == bss_section) |
43640c4d | 598 | { |
5b79dba5 AI |
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); | |
4162f25f EZ |
603 | COPY_PROC_CHUNK ("Dumping bss data...", bss_start, |
604 | bss_size, be_verbose); | |
5b79dba5 AI |
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; | |
43640c4d | 612 | } |
5b79dba5 AI |
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); | |
4162f25f EZ |
619 | COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static, |
620 | bss_size_static, be_verbose); | |
5b79dba5 AI |
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 | { | |
62aba0d4 FP |
631 | DWORD_PTR heap_start = (DWORD_PTR) get_heap_start (); |
632 | DWORD_PTR heap_size = get_committed_heap_size (); | |
5b79dba5 AI |
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); | |
4162f25f EZ |
638 | COPY_PROC_CHUNK ("Dumping heap...", heap_start, heap_size, |
639 | be_verbose); | |
5b79dba5 AI |
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 | } | |
657 | ||
7fef47a3 AI |
658 | /* Align the section's raw data area. */ |
659 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); | |
660 | ||
2147fb50 | 661 | section++; |
5b79dba5 | 662 | dst_section++; |
2147fb50 | 663 | } |
a610993d | 664 | |
5b79dba5 AI |
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), | |
4162f25f | 674 | p_infile->size - offset, be_verbose); |
5b79dba5 AI |
675 | |
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) | |
688 | ||
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++) | |
a610993d | 692 | { |
5b79dba5 AI |
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); | |
700 | ||
701 | ADJUST_OFFSET (dst_section[i].PointerToLinenumbers); | |
a610993d | 702 | } |
2147fb50 | 703 | |
5b79dba5 | 704 | ADJUST_OFFSET (dst_nt_header->FileHeader.PointerToSymbolTable); |
2147fb50 | 705 | |
5b79dba5 AI |
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; | |
2147fb50 | 711 | |
5b79dba5 AI |
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); | |
2147fb50 | 718 | |
5b79dba5 AI |
719 | for (i = 0; i < debug_dir.Size; i++, debug_entry++) |
720 | ADJUST_OFFSET (debug_entry->PointerToRawData); | |
721 | } | |
722 | } | |
2147fb50 KH |
723 | } |
724 | ||
725 | ||
5b79dba5 | 726 | /* Dump out .data and .bss sections into a new executable. */ |
381259ef | 727 | void |
dd5ecd6b | 728 | unexec (const char *new_name, const char *old_name) |
9c8056fe | 729 | { |
5b79dba5 AI |
730 | file_data in_file, out_file; |
731 | char out_filename[MAX_PATH], in_filename[MAX_PATH]; | |
732 | unsigned long size; | |
49dc9682 AI |
733 | char *p; |
734 | char *q; | |
735 | ||
736 | /* Ignore old_name, and get our actual location from the OS. */ | |
737 | if (!GetModuleFileName (NULL, in_filename, MAX_PATH)) | |
738 | abort (); | |
739 | dostounix_filename (in_filename); | |
740 | strcpy (out_filename, in_filename); | |
741 | ||
742 | /* Change the base of the output filename to match the requested name. */ | |
743 | if ((p = strrchr (out_filename, '/')) == NULL) | |
744 | abort (); | |
745 | /* The filenames have already been expanded, and will be in Unix | |
746 | format, so it is safe to expect an absolute name. */ | |
747 | if ((q = strrchr (new_name, '/')) == NULL) | |
748 | abort (); | |
749 | strcpy (p, q); | |
177c0ea7 | 750 | |
49dc9682 AI |
751 | /* Make sure that the output filename has the ".exe" extension...patch |
752 | it up if not. */ | |
753 | p = out_filename + strlen (out_filename) - 4; | |
754 | if (strcmp (p, ".exe")) | |
5b79dba5 | 755 | strcat (out_filename, ".exe"); |
2147fb50 | 756 | |
5b79dba5 AI |
757 | printf ("Dumping from %s\n", in_filename); |
758 | printf (" to %s\n", out_filename); | |
2147fb50 | 759 | |
5b79dba5 AI |
760 | /* We need to round off our heap to NT's page size. */ |
761 | round_heap (get_page_size ()); | |
198fdd15 | 762 | |
5b79dba5 AI |
763 | /* Open the undumped executable file. */ |
764 | if (!open_input_file (&in_file, in_filename)) | |
765 | { | |
177c0ea7 | 766 | printf ("Failed to open %s (%d)...bailing.\n", |
5b79dba5 AI |
767 | in_filename, GetLastError ()); |
768 | exit (1); | |
769 | } | |
2147fb50 | 770 | |
5b79dba5 AI |
771 | /* Get the interesting section info, like start and size of .bss... */ |
772 | get_section_info (&in_file); | |
2147fb50 | 773 | |
5b79dba5 AI |
774 | /* The size of the dumped executable is the size of the original |
775 | executable plus the size of the heap and the size of the .bss section. */ | |
776 | size = in_file.size + | |
777 | get_committed_heap_size () + | |
778 | extra_bss_size + | |
779 | extra_bss_size_static; | |
780 | if (!open_output_file (&out_file, out_filename, size)) | |
198fdd15 | 781 | { |
177c0ea7 | 782 | printf ("Failed to open %s (%d)...bailing.\n", |
5b79dba5 AI |
783 | out_filename, GetLastError ()); |
784 | exit (1); | |
198fdd15 GV |
785 | } |
786 | ||
5b79dba5 AI |
787 | /* Set the flag (before dumping). */ |
788 | using_dynamic_heap = TRUE; | |
198fdd15 | 789 | |
5b79dba5 | 790 | copy_executable_and_dump_data (&in_file, &out_file); |
198fdd15 | 791 | |
5b79dba5 AI |
792 | /* Patch up header fields; profiler is picky about this. */ |
793 | { | |
794 | PIMAGE_DOS_HEADER dos_header; | |
795 | PIMAGE_NT_HEADERS nt_header; | |
796 | HANDLE hImagehelp = LoadLibrary ("imagehlp.dll"); | |
797 | DWORD headersum; | |
798 | DWORD checksum; | |
198fdd15 | 799 | |
5b79dba5 AI |
800 | dos_header = (PIMAGE_DOS_HEADER) out_file.file_base; |
801 | nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew); | |
198fdd15 | 802 | |
5b79dba5 AI |
803 | nt_header->OptionalHeader.CheckSum = 0; |
804 | // nt_header->FileHeader.TimeDateStamp = time (NULL); | |
805 | // dos_header->e_cp = size / 512; | |
806 | // nt_header->OptionalHeader.SizeOfImage = size; | |
807 | ||
808 | pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile"); | |
809 | if (pfnCheckSumMappedFile) | |
810 | { | |
811 | // nt_header->FileHeader.TimeDateStamp = time (NULL); | |
812 | pfnCheckSumMappedFile (out_file.file_base, | |
813 | out_file.size, | |
814 | &headersum, | |
815 | &checksum); | |
816 | nt_header->OptionalHeader.CheckSum = checksum; | |
817 | } | |
818 | FreeLibrary (hImagehelp); | |
819 | } | |
198fdd15 | 820 | |
5b79dba5 AI |
821 | close_file_data (&in_file); |
822 | close_file_data (&out_file); | |
2147fb50 | 823 | } |
5b79dba5 AI |
824 | |
825 | /* eof */ |