| 1 | /* unexec for GNU Emacs on Windows NT. |
| 2 | Copyright (C) 1994, 2001-2013 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 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 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 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. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* |
| 20 | Geoff Voelker (voelker@cs.washington.edu) 8-12-94 |
| 21 | */ |
| 22 | |
| 23 | #include <config.h> |
| 24 | #include "unexec.h" |
| 25 | #include "lisp.h" |
| 26 | #include "w32common.h" |
| 27 | #include "w32.h" |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <time.h> |
| 32 | #include <windows.h> |
| 33 | |
| 34 | /* Include relevant definitions from IMAGEHLP.H, which can be found |
| 35 | in \\win32sdk\mstools\samples\image\include\imagehlp.h. */ |
| 36 | |
| 37 | PIMAGE_NT_HEADERS |
| 38 | (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress, |
| 39 | DWORD FileLength, |
| 40 | LPDWORD HeaderSum, |
| 41 | LPDWORD CheckSum); |
| 42 | |
| 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[]; |
| 49 | extern char *my_begbss_static; |
| 50 | extern char *my_endbss_static; |
| 51 | |
| 52 | #include "w32heap.h" |
| 53 | |
| 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 | |
| 59 | /* Basically, our "initialized" flag. */ |
| 60 | BOOL using_dynamic_heap = FALSE; |
| 61 | |
| 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); |
| 64 | void close_file_data (file_data *p_file); |
| 65 | |
| 66 | void get_section_info (file_data *p_file); |
| 67 | void copy_executable_and_dump_data (file_data *, file_data *); |
| 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. */ |
| 71 | PIMAGE_SECTION_HEADER data_section; |
| 72 | PCHAR data_start = 0; |
| 73 | DWORD_PTR data_size = 0; |
| 74 | |
| 75 | /* Cached info about the .bss section in the executable. */ |
| 76 | PIMAGE_SECTION_HEADER bss_section; |
| 77 | PCHAR bss_start = 0; |
| 78 | DWORD_PTR bss_size = 0; |
| 79 | DWORD_PTR extra_bss_size = 0; |
| 80 | /* bss data that is static might be discontiguous from non-static. */ |
| 81 | PIMAGE_SECTION_HEADER bss_section_static; |
| 82 | PCHAR bss_start_static = 0; |
| 83 | DWORD_PTR bss_size_static = 0; |
| 84 | DWORD_PTR extra_bss_size_static = 0; |
| 85 | |
| 86 | PIMAGE_SECTION_HEADER heap_section; |
| 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 | |
| 97 | #if 1 |
| 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 | |
| 104 | /* Cache system info, e.g., the NT page size. */ |
| 105 | cache_system_info (); |
| 106 | |
| 107 | /* Grab our malloc arena space now, before CRT starts up. */ |
| 108 | init_heap (); |
| 109 | |
| 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 | |
| 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); |
| 117 | mainCRTStartup (); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /* File handling. */ |
| 122 | |
| 123 | int |
| 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); |
| 133 | if (file == INVALID_HANDLE_VALUE) |
| 134 | return FALSE; |
| 135 | |
| 136 | size = GetFileSize (file, &upper_size); |
| 137 | file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY, |
| 138 | 0, size, NULL); |
| 139 | if (!file_mapping) |
| 140 | return FALSE; |
| 141 | |
| 142 | file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size); |
| 143 | if (file_base == 0) |
| 144 | return FALSE; |
| 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; |
| 151 | |
| 152 | return TRUE; |
| 153 | } |
| 154 | |
| 155 | int |
| 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; |
| 161 | |
| 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); |
| 170 | file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 171 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
| 172 | if (file == INVALID_HANDLE_VALUE) |
| 173 | return FALSE; |
| 174 | |
| 175 | file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE, |
| 176 | 0, size, NULL); |
| 177 | if (!file_mapping) |
| 178 | return FALSE; |
| 179 | |
| 180 | file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size); |
| 181 | if (file_base == 0) |
| 182 | return FALSE; |
| 183 | |
| 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; |
| 189 | |
| 190 | return TRUE; |
| 191 | } |
| 192 | |
| 193 | /* Close the system structures associated with the given file. */ |
| 194 | void |
| 195 | close_file_data (file_data *p_file) |
| 196 | { |
| 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); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /* Routines to manipulate NT executable file sections. */ |
| 207 | |
| 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 * |
| 229 | rva_to_section (DWORD_PTR rva, IMAGE_NT_HEADERS * nt_header) |
| 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++) |
| 237 | { |
| 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. */ |
| 244 | DWORD_PTR real_size = max (section->SizeOfRawData, |
| 245 | section->Misc.VirtualSize); |
| 246 | if (rva >= section->VirtualAddress |
| 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 * |
| 257 | offset_to_section (DWORD_PTR offset, IMAGE_NT_HEADERS * nt_header) |
| 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) |
| 268 | return section; |
| 269 | section++; |
| 270 | } |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 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). */ |
| 277 | DWORD_PTR |
| 278 | relocate_offset (DWORD_PTR offset, |
| 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) \ |
| 312 | ((section)->VirtualAddress + ((DWORD_PTR)(offset) - (section)->PointerToRawData)) |
| 313 | |
| 314 | #define RVA_TO_OFFSET(rva, section) \ |
| 315 | ((section)->PointerToRawData + ((DWORD_PTR)(rva) - (section)->VirtualAddress)) |
| 316 | |
| 317 | #define RVA_TO_SECTION_OFFSET(rva, section) \ |
| 318 | ((DWORD_PTR)(rva) - (section)->VirtualAddress) |
| 319 | |
| 320 | /* Convert address in executing image to RVA. */ |
| 321 | #define PTR_TO_RVA(ptr) ((DWORD_PTR)(ptr) - (DWORD_PTR) GetModuleHandle (NULL)) |
| 322 | |
| 323 | #define RVA_TO_PTR(var,section,filedata) \ |
| 324 | ((unsigned char *)(RVA_TO_OFFSET (var,section) + (filedata).file_base)) |
| 325 | |
| 326 | #define PTR_TO_OFFSET(ptr, pfile_data) \ |
| 327 | ((unsigned char *)(ptr) - (pfile_data)->file_base) |
| 328 | |
| 329 | #define OFFSET_TO_PTR(offset, pfile_data) \ |
| 330 | ((pfile_data)->file_base + (DWORD_PTR)(offset)) |
| 331 | |
| 332 | |
| 333 | /* Flip through the executable and cache the info necessary for dumping. */ |
| 334 | void |
| 335 | get_section_info (file_data *p_infile) |
| 336 | { |
| 337 | PIMAGE_DOS_HEADER dos_header; |
| 338 | PIMAGE_NT_HEADERS nt_header; |
| 339 | int overlap; |
| 340 | |
| 341 | dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base; |
| 342 | if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) |
| 343 | { |
| 344 | printf ("Unknown EXE header in %s...bailing.\n", p_infile->name); |
| 345 | exit (1); |
| 346 | } |
| 347 | nt_header = (PIMAGE_NT_HEADERS) (((DWORD_PTR) dos_header) + |
| 348 | dos_header->e_lfanew); |
| 349 | if (nt_header == NULL) |
| 350 | { |
| 351 | printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n", |
| 352 | p_infile->name); |
| 353 | exit (1); |
| 354 | } |
| 355 | |
| 356 | /* Check the NT header signature ... */ |
| 357 | if (nt_header->Signature != IMAGE_NT_SIGNATURE) |
| 358 | { |
| 359 | printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n", |
| 360 | nt_header->Signature, p_infile->name); |
| 361 | exit (1); |
| 362 | } |
| 363 | |
| 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 | |
| 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) |
| 383 | { |
| 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 | } |
| 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. */ |
| 442 | #ifdef _ALPHA_ |
| 443 | overlap = 1; /* force all bss data to be dumped */ |
| 444 | #else |
| 445 | overlap = 0; |
| 446 | #endif |
| 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) |
| 460 | { |
| 461 | printf ("BSS data not in a single section...bailing\n"); |
| 462 | exit (1); |
| 463 | } |
| 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 | |
| 476 | void |
| 477 | copy_executable_and_dump_data (file_data *p_infile, |
| 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; |
| 486 | DWORD_PTR offset; |
| 487 | int i; |
| 488 | int be_verbose = GetEnvironmentVariable ("DEBUG_DUMP", NULL, 0) > 0; |
| 489 | |
| 490 | #define COPY_CHUNK(message, src, size, verbose) \ |
| 491 | do { \ |
| 492 | unsigned char *s = (void *)(src); \ |
| 493 | unsigned long count = (size); \ |
| 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 | } \ |
| 501 | memcpy (dst, s, count); \ |
| 502 | dst += count; \ |
| 503 | } while (0) |
| 504 | |
| 505 | #define COPY_PROC_CHUNK(message, src, size, verbose) \ |
| 506 | do { \ |
| 507 | unsigned char *s = (void *)(src); \ |
| 508 | unsigned long count = (size); \ |
| 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 | } \ |
| 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))) |
| 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) |
| 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; |
| 539 | nt_header = (PIMAGE_NT_HEADERS) (((DWORD_PTR) dos_header) + |
| 540 | dos_header->e_lfanew); |
| 541 | section = IMAGE_FIRST_SECTION (nt_header); |
| 542 | |
| 543 | dst = (unsigned char *) p_outfile->file_base; |
| 544 | |
| 545 | COPY_CHUNK ("Copying DOS header...", dos_header, |
| 546 | (DWORD_PTR) nt_header - (DWORD_PTR) dos_header, be_verbose); |
| 547 | dst_nt_header = (PIMAGE_NT_HEADERS) dst; |
| 548 | COPY_CHUNK ("Copying NT header...", nt_header, |
| 549 | (DWORD_PTR) section - (DWORD_PTR) nt_header, be_verbose); |
| 550 | dst_section = (PIMAGE_SECTION_HEADER) dst; |
| 551 | COPY_CHUNK ("Copying section table...", section, |
| 552 | nt_header->FileHeader.NumberOfSections * sizeof (*section), |
| 553 | be_verbose); |
| 554 | |
| 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 | |
| 560 | for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) |
| 561 | { |
| 562 | char msg[100]; |
| 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); |
| 566 | |
| 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), |
| 578 | section->SizeOfRawData, be_verbose); |
| 579 | /* Ensure alignment slop is zeroed. */ |
| 580 | ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment); |
| 581 | |
| 582 | /* Note that various sections below may be aliases. */ |
| 583 | if (section == data_section) |
| 584 | { |
| 585 | dst = dst_save |
| 586 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start), dst_section); |
| 587 | COPY_PROC_CHUNK ("Dumping initialized data...", |
| 588 | data_start, data_size, be_verbose); |
| 589 | dst = dst_save + dst_section->SizeOfRawData; |
| 590 | } |
| 591 | if (section == bss_section) |
| 592 | { |
| 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); |
| 597 | COPY_PROC_CHUNK ("Dumping bss data...", bss_start, |
| 598 | bss_size, be_verbose); |
| 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; |
| 606 | } |
| 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); |
| 613 | COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static, |
| 614 | bss_size_static, be_verbose); |
| 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 | { |
| 625 | DWORD_PTR heap_start = (DWORD_PTR) get_heap_start (); |
| 626 | DWORD_PTR heap_size = get_committed_heap_size (); |
| 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); |
| 632 | COPY_PROC_CHUNK ("Dumping heap...", heap_start, heap_size, |
| 633 | be_verbose); |
| 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 | |
| 652 | /* Align the section's raw data area. */ |
| 653 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); |
| 654 | |
| 655 | section++; |
| 656 | dst_section++; |
| 657 | } |
| 658 | |
| 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), |
| 668 | p_infile->size - offset, be_verbose); |
| 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++) |
| 686 | { |
| 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); |
| 696 | } |
| 697 | |
| 698 | ADJUST_OFFSET (dst_nt_header->FileHeader.PointerToSymbolTable); |
| 699 | |
| 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; |
| 705 | |
| 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); |
| 712 | |
| 713 | for (i = 0; i < debug_dir.Size; i++, debug_entry++) |
| 714 | ADJUST_OFFSET (debug_entry->PointerToRawData); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | |
| 720 | /* Dump out .data and .bss sections into a new executable. */ |
| 721 | void |
| 722 | unexec (const char *new_name, const char *old_name) |
| 723 | { |
| 724 | file_data in_file, out_file; |
| 725 | char out_filename[MAX_PATH], in_filename[MAX_PATH]; |
| 726 | unsigned long size; |
| 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 (); |
| 733 | dostounix_filename (in_filename, 0); |
| 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); |
| 744 | |
| 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")) |
| 749 | strcat (out_filename, ".exe"); |
| 750 | |
| 751 | printf ("Dumping from %s\n", in_filename); |
| 752 | printf (" to %s\n", out_filename); |
| 753 | |
| 754 | /* We need to round off our heap to NT's page size. */ |
| 755 | round_heap (get_page_size ()); |
| 756 | |
| 757 | /* Open the undumped executable file. */ |
| 758 | if (!open_input_file (&in_file, in_filename)) |
| 759 | { |
| 760 | printf ("Failed to open %s (%d)...bailing.\n", |
| 761 | in_filename, GetLastError ()); |
| 762 | exit (1); |
| 763 | } |
| 764 | |
| 765 | /* Get the interesting section info, like start and size of .bss... */ |
| 766 | get_section_info (&in_file); |
| 767 | |
| 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)) |
| 775 | { |
| 776 | printf ("Failed to open %s (%d)...bailing.\n", |
| 777 | out_filename, GetLastError ()); |
| 778 | exit (1); |
| 779 | } |
| 780 | |
| 781 | /* Set the flag (before dumping). */ |
| 782 | using_dynamic_heap = TRUE; |
| 783 | |
| 784 | copy_executable_and_dump_data (&in_file, &out_file); |
| 785 | |
| 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; |
| 793 | |
| 794 | dos_header = (PIMAGE_DOS_HEADER) out_file.file_base; |
| 795 | nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew); |
| 796 | |
| 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 | } |
| 814 | |
| 815 | close_file_data (&in_file); |
| 816 | close_file_data (&out_file); |
| 817 | } |
| 818 | |
| 819 | /* eof */ |