diff options
Diffstat (limited to 'src/unexw32.c')
| -rw-r--r-- | src/unexw32.c | 59 |
1 files changed, 38 insertions, 21 deletions
diff --git a/src/unexw32.c b/src/unexw32.c index b1838644965..ebeb7355673 100644 --- a/src/unexw32.c +++ b/src/unexw32.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* unexec for GNU Emacs on Windows NT. | 1 | /* unexec for GNU Emacs on Windows NT. |
| 2 | Copyright (C) 1994 Free Software Foundation, Inc. | 2 | Copyright (C) 1994, 2005 Free Software Foundation, Inc. |
| 3 | 3 | ||
| 4 | This file is part of GNU Emacs. | 4 | This file is part of GNU Emacs. |
| 5 | 5 | ||
| @@ -325,6 +325,9 @@ relocate_offset (DWORD offset, | |||
| 325 | /* Convert address in executing image to RVA. */ | 325 | /* Convert address in executing image to RVA. */ |
| 326 | #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL)) | 326 | #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL)) |
| 327 | 327 | ||
| 328 | #define RVA_TO_PTR(var,section,filedata) \ | ||
| 329 | ((void *)(RVA_TO_OFFSET(var,section) + (filedata).file_base)) | ||
| 330 | |||
| 328 | #define PTR_TO_OFFSET(ptr, pfile_data) \ | 331 | #define PTR_TO_OFFSET(ptr, pfile_data) \ |
| 329 | ((unsigned char *)(ptr) - (pfile_data)->file_base) | 332 | ((unsigned char *)(ptr) - (pfile_data)->file_base) |
| 330 | 333 | ||
| @@ -488,27 +491,34 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 488 | PIMAGE_SECTION_HEADER dst_section; | 491 | PIMAGE_SECTION_HEADER dst_section; |
| 489 | DWORD offset; | 492 | DWORD offset; |
| 490 | int i; | 493 | int i; |
| 494 | int be_verbose = GetEnvironmentVariable ("DEBUG_DUMP", NULL, 0) > 0; | ||
| 491 | 495 | ||
| 492 | #define COPY_CHUNK(message, src, size) \ | 496 | #define COPY_CHUNK(message, src, size, verbose) \ |
| 493 | do { \ | 497 | do { \ |
| 494 | unsigned char *s = (void *)(src); \ | 498 | unsigned char *s = (void *)(src); \ |
| 495 | unsigned long count = (size); \ | 499 | unsigned long count = (size); \ |
| 496 | printf ("%s\n", (message)); \ | 500 | if (verbose) \ |
| 497 | printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \ | 501 | { \ |
| 498 | printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \ | 502 | printf ("%s\n", (message)); \ |
| 499 | printf ("\t0x%08x Size in bytes.\n", count); \ | 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 | } \ | ||
| 500 | memcpy (dst, s, count); \ | 507 | memcpy (dst, s, count); \ |
| 501 | dst += count; \ | 508 | dst += count; \ |
| 502 | } while (0) | 509 | } while (0) |
| 503 | 510 | ||
| 504 | #define COPY_PROC_CHUNK(message, src, size) \ | 511 | #define COPY_PROC_CHUNK(message, src, size, verbose) \ |
| 505 | do { \ | 512 | do { \ |
| 506 | unsigned char *s = (void *)(src); \ | 513 | unsigned char *s = (void *)(src); \ |
| 507 | unsigned long count = (size); \ | 514 | unsigned long count = (size); \ |
| 508 | printf ("%s\n", (message)); \ | 515 | if (verbose) \ |
| 509 | printf ("\t0x%08x Address in process.\n", s); \ | 516 | { \ |
| 510 | printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \ | 517 | printf ("%s\n", (message)); \ |
| 511 | printf ("\t0x%08x Size in bytes.\n", count); \ | 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 | } \ | ||
| 512 | memcpy (dst, s, count); \ | 522 | memcpy (dst, s, count); \ |
| 513 | dst += count; \ | 523 | dst += count; \ |
| 514 | } while (0) | 524 | } while (0) |
| @@ -539,13 +549,14 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 539 | dst = (unsigned char *) p_outfile->file_base; | 549 | dst = (unsigned char *) p_outfile->file_base; |
| 540 | 550 | ||
| 541 | COPY_CHUNK ("Copying DOS header...", dos_header, | 551 | COPY_CHUNK ("Copying DOS header...", dos_header, |
| 542 | (DWORD) nt_header - (DWORD) dos_header); | 552 | (DWORD) nt_header - (DWORD) dos_header, be_verbose); |
| 543 | dst_nt_header = (PIMAGE_NT_HEADERS) dst; | 553 | dst_nt_header = (PIMAGE_NT_HEADERS) dst; |
| 544 | COPY_CHUNK ("Copying NT header...", nt_header, | 554 | COPY_CHUNK ("Copying NT header...", nt_header, |
| 545 | (DWORD) section - (DWORD) nt_header); | 555 | (DWORD) section - (DWORD) nt_header, be_verbose); |
| 546 | dst_section = (PIMAGE_SECTION_HEADER) dst; | 556 | dst_section = (PIMAGE_SECTION_HEADER) dst; |
| 547 | COPY_CHUNK ("Copying section table...", section, | 557 | COPY_CHUNK ("Copying section table...", section, |
| 548 | nt_header->FileHeader.NumberOfSections * sizeof (*section)); | 558 | nt_header->FileHeader.NumberOfSections * sizeof (*section), |
| 559 | be_verbose); | ||
| 549 | 560 | ||
| 550 | /* Align the first section's raw data area, and set the header size | 561 | /* Align the first section's raw data area, and set the header size |
| 551 | field accordingly. */ | 562 | field accordingly. */ |
| @@ -555,7 +566,9 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 555 | for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) | 566 | for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) |
| 556 | { | 567 | { |
| 557 | char msg[100]; | 568 | char msg[100]; |
| 558 | sprintf (msg, "Copying raw data for %s...", section->Name); | 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); | ||
| 559 | 572 | ||
| 560 | dst_save = dst; | 573 | dst_save = dst; |
| 561 | 574 | ||
| @@ -568,7 +581,7 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 568 | /* Can always copy the original raw data. */ | 581 | /* Can always copy the original raw data. */ |
| 569 | COPY_CHUNK | 582 | COPY_CHUNK |
| 570 | (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile), | 583 | (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile), |
| 571 | section->SizeOfRawData); | 584 | section->SizeOfRawData, be_verbose); |
| 572 | /* Ensure alignment slop is zeroed. */ | 585 | /* Ensure alignment slop is zeroed. */ |
| 573 | ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment); | 586 | ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment); |
| 574 | 587 | ||
| @@ -577,7 +590,8 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 577 | { | 590 | { |
| 578 | dst = dst_save | 591 | dst = dst_save |
| 579 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start), dst_section); | 592 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start), dst_section); |
| 580 | COPY_PROC_CHUNK ("Dumping initialized data...", data_start, data_size); | 593 | COPY_PROC_CHUNK ("Dumping initialized data...", |
| 594 | data_start, data_size, be_verbose); | ||
| 581 | dst = dst_save + dst_section->SizeOfRawData; | 595 | dst = dst_save + dst_section->SizeOfRawData; |
| 582 | } | 596 | } |
| 583 | if (section == bss_section) | 597 | if (section == bss_section) |
| @@ -586,7 +600,8 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 586 | data size as necessary. */ | 600 | data size as necessary. */ |
| 587 | dst = dst_save | 601 | dst = dst_save |
| 588 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start), dst_section); | 602 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start), dst_section); |
| 589 | COPY_PROC_CHUNK ("Dumping bss data...", bss_start, bss_size); | 603 | COPY_PROC_CHUNK ("Dumping bss data...", bss_start, |
| 604 | bss_size, be_verbose); | ||
| 590 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); | 605 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); |
| 591 | dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile); | 606 | dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile); |
| 592 | /* Determine new size of raw data area. */ | 607 | /* Determine new size of raw data area. */ |
| @@ -601,7 +616,8 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 601 | section's raw data size as necessary. */ | 616 | section's raw data size as necessary. */ |
| 602 | dst = dst_save | 617 | dst = dst_save |
| 603 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static), dst_section); | 618 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static), dst_section); |
| 604 | COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static, bss_size_static); | 619 | COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static, |
| 620 | bss_size_static, be_verbose); | ||
| 605 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); | 621 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); |
| 606 | dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile); | 622 | dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile); |
| 607 | /* Determine new size of raw data area. */ | 623 | /* Determine new size of raw data area. */ |
| @@ -619,7 +635,8 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 619 | section's size to the appropriate size. */ | 635 | section's size to the appropriate size. */ |
| 620 | dst = dst_save | 636 | dst = dst_save |
| 621 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (heap_start), dst_section); | 637 | + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (heap_start), dst_section); |
| 622 | COPY_PROC_CHUNK ("Dumping heap...", heap_start, heap_size); | 638 | COPY_PROC_CHUNK ("Dumping heap...", heap_start, heap_size, |
| 639 | be_verbose); | ||
| 623 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); | 640 | ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment); |
| 624 | dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile); | 641 | dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile); |
| 625 | /* Determine new size of raw data area. */ | 642 | /* Determine new size of raw data area. */ |
| @@ -654,7 +671,7 @@ copy_executable_and_dump_data (file_data *p_infile, | |||
| 654 | COPY_CHUNK | 671 | COPY_CHUNK |
| 655 | ("Copying remainder of executable...", | 672 | ("Copying remainder of executable...", |
| 656 | OFFSET_TO_PTR (offset, p_infile), | 673 | OFFSET_TO_PTR (offset, p_infile), |
| 657 | p_infile->size - offset); | 674 | p_infile->size - offset, be_verbose); |
| 658 | 675 | ||
| 659 | /* Final size for new image. */ | 676 | /* Final size for new image. */ |
| 660 | p_outfile->size = DST_TO_OFFSET (); | 677 | p_outfile->size = DST_TO_OFFSET (); |