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