1 /* unexec for GNU Emacs on Windows NT.
2 Copyright (C) 1994, 2001-2016 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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 (at
9 your option) any later version.
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.
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/>. */
20 Geoff Voelker (voelker@cs.washington.edu) 8-12-94
26 #include "w32common.h"
34 /* Include relevant definitions from IMAGEHLP.H, which can be found
35 in \\win32sdk\mstools\samples\image\include\imagehlp.h. */
37 PIMAGE_NT_HEADERS (__stdcall
* pfnCheckSumMappedFile
) (LPVOID BaseAddress
,
42 extern BOOL
ctrl_c_handler (unsigned long type
);
44 extern char my_begdata
[];
45 extern char my_begbss
[];
46 extern char *my_begbss_static
;
50 /* Basically, our "initialized" flag. */
51 BOOL using_dynamic_heap
= FALSE
;
53 void get_section_info (file_data
*p_file
);
54 void copy_executable_and_dump_data (file_data
*, file_data
*);
55 void dump_bss_and_heap (file_data
*p_infile
, file_data
*p_outfile
);
57 /* Cached info about the .data section in the executable. */
58 PIMAGE_SECTION_HEADER data_section
;
60 DWORD_PTR data_size
= 0;
62 /* Cached info about the .bss section in the executable. */
63 PIMAGE_SECTION_HEADER bss_section
;
65 DWORD_PTR bss_size
= 0;
66 DWORD_PTR extra_bss_size
= 0;
67 /* bss data that is static might be discontiguous from non-static. */
68 PIMAGE_SECTION_HEADER bss_section_static
;
69 PCHAR bss_start_static
= 0;
70 DWORD_PTR bss_size_static
= 0;
71 DWORD_PTR extra_bss_size_static
= 0;
73 /* MinGW64 doesn't add a leading underscore to external symbols,
74 whereas configure.ac sets up LD_SWITCH_SYSTEM_TEMACS to force the
75 entry point at __start, with two underscores. */
77 #define _start __start
80 extern void mainCRTStartup (void);
82 /* Startup code for running on NT. When we are running as the dumped
83 version, we need to bootstrap our heap and .bss section into our
84 address space before we can actually hand off control to the startup
85 code supplied by NT (primarily because that code relies upon malloc ()). */
93 /* Give us a way to debug problems with crashes on startup when
94 running under the MSVC profiler. */
95 if (GetEnvironmentVariable ("EMACS_DEBUG", NULL
, 0) > 0)
99 /* Cache system info, e.g., the NT page size. */
100 cache_system_info ();
102 /* Grab our malloc arena space now, before CRT starts up. */
105 /* This prevents ctrl-c's in shells running while we're suspended from
107 SetConsoleCtrlHandler ((PHANDLER_ROUTINE
) ctrl_c_handler
, TRUE
);
109 /* Prevent Emacs from being locked up (eg. in batch mode) when
110 accessing devices that aren't mounted (eg. removable media drives). */
111 SetErrorMode (SEM_FAILCRITICALERRORS
);
118 /* Implementation note: this and the next functions work with ANSI
119 codepage encoded file names! */
121 open_input_file (file_data
*p_file
, char *filename
)
126 unsigned long size
, upper_size
;
128 file
= CreateFileA (filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
129 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
130 if (file
== INVALID_HANDLE_VALUE
)
133 size
= GetFileSize (file
, &upper_size
);
134 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_READONLY
,
139 file_base
= MapViewOfFile (file_mapping
, FILE_MAP_READ
, 0, 0, size
);
143 p_file
->name
= filename
;
146 p_file
->file_mapping
= file_mapping
;
147 p_file
->file_base
= file_base
;
153 open_output_file (file_data
*p_file
, char *filename
, unsigned long size
)
159 /* We delete any existing FILENAME because loadup.el will create a
160 hard link to it under the name emacs-XX.YY.ZZ.nn.exe. Evidently,
161 overwriting a file on Unix breaks any hard links to it, but that
162 doesn't happen on Windows. If we don't delete the file before
163 creating it, all the emacs-XX.YY.ZZ.nn.exe end up being hard
164 links to the same file, which defeats the purpose of these hard
165 links: being able to run previous builds. */
166 DeleteFileA (filename
);
167 file
= CreateFileA (filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
168 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
169 if (file
== INVALID_HANDLE_VALUE
)
172 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_READWRITE
,
177 file_base
= MapViewOfFile (file_mapping
, FILE_MAP_WRITE
, 0, 0, size
);
181 p_file
->name
= filename
;
184 p_file
->file_mapping
= file_mapping
;
185 p_file
->file_base
= file_base
;
190 /* Close the system structures associated with the given file. */
192 close_file_data (file_data
*p_file
)
194 UnmapViewOfFile (p_file
->file_base
);
195 CloseHandle (p_file
->file_mapping
);
196 /* For the case of output files, set final size. */
197 SetFilePointer (p_file
->file
, p_file
->size
, NULL
, FILE_BEGIN
);
198 SetEndOfFile (p_file
->file
);
199 CloseHandle (p_file
->file
);
203 /* Routines to manipulate NT executable file sections. */
205 /* Return pointer to section header for named section. */
206 IMAGE_SECTION_HEADER
*
207 find_section (const char * name
, IMAGE_NT_HEADERS
* nt_header
)
209 PIMAGE_SECTION_HEADER section
;
212 section
= IMAGE_FIRST_SECTION (nt_header
);
214 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
216 if (strcmp ((char *)section
->Name
, name
) == 0)
223 /* Return pointer to section header for section containing the given
224 relative virtual address. */
225 IMAGE_SECTION_HEADER
*
226 rva_to_section (DWORD_PTR rva
, IMAGE_NT_HEADERS
* nt_header
)
228 PIMAGE_SECTION_HEADER section
;
231 section
= IMAGE_FIRST_SECTION (nt_header
);
233 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
235 /* Some linkers (eg. the NT SDK linker I believe) swapped the
236 meaning of these two values - or rather, they ignored
237 VirtualSize entirely and always set it to zero. This affects
238 some very old exes (eg. gzip dated Dec 1993). Since
239 w32_executable_type relies on this function to work reliably,
240 we need to cope with this. */
241 DWORD_PTR real_size
= max (section
->SizeOfRawData
,
242 section
->Misc
.VirtualSize
);
243 if (rva
>= section
->VirtualAddress
244 && rva
< section
->VirtualAddress
+ real_size
)
252 /* Return pointer to section header for section containing the given
253 offset in its raw data area. */
254 static IMAGE_SECTION_HEADER
*
255 offset_to_section (DWORD_PTR offset
, IMAGE_NT_HEADERS
* nt_header
)
257 PIMAGE_SECTION_HEADER section
;
260 section
= IMAGE_FIRST_SECTION (nt_header
);
262 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
264 if (offset
>= section
->PointerToRawData
265 && offset
< section
->PointerToRawData
+ section
->SizeOfRawData
)
273 /* Return offset to an object in dst, given offset in src. We assume
274 there is at least one section in both src and dst images, and that
275 the some sections may have been added to dst (after sections in src). */
277 relocate_offset (DWORD_PTR offset
,
278 IMAGE_NT_HEADERS
* src_nt_header
,
279 IMAGE_NT_HEADERS
* dst_nt_header
)
281 PIMAGE_SECTION_HEADER src_section
= IMAGE_FIRST_SECTION (src_nt_header
);
282 PIMAGE_SECTION_HEADER dst_section
= IMAGE_FIRST_SECTION (dst_nt_header
);
285 while (offset
>= src_section
->PointerToRawData
)
287 if (offset
< src_section
->PointerToRawData
+ src_section
->SizeOfRawData
)
290 if (i
== src_nt_header
->FileHeader
.NumberOfSections
)
292 /* Handle offsets after the last section. */
293 dst_section
= IMAGE_FIRST_SECTION (dst_nt_header
);
294 dst_section
+= dst_nt_header
->FileHeader
.NumberOfSections
- 1;
295 while (dst_section
->PointerToRawData
== 0)
297 while (src_section
->PointerToRawData
== 0)
300 + (dst_section
->PointerToRawData
+ dst_section
->SizeOfRawData
)
301 - (src_section
->PointerToRawData
+ src_section
->SizeOfRawData
);
307 (dst_section
->PointerToRawData
- src_section
->PointerToRawData
);
310 #define RVA_TO_OFFSET(rva, section) \
311 ((section)->PointerToRawData + ((DWORD_PTR)(rva) - (section)->VirtualAddress))
313 #define RVA_TO_SECTION_OFFSET(rva, section) \
314 ((DWORD_PTR)(rva) - (section)->VirtualAddress)
316 /* Convert address in executing image to RVA. */
317 #define PTR_TO_RVA(ptr) ((DWORD_PTR)(ptr) - (DWORD_PTR) GetModuleHandle (NULL))
319 #define PTR_TO_OFFSET(ptr, pfile_data) \
320 ((unsigned char *)(ptr) - (pfile_data)->file_base)
322 #define OFFSET_TO_PTR(offset, pfile_data) \
323 ((pfile_data)->file_base + (DWORD_PTR)(offset))
326 #define OFFSET_TO_RVA(offset, section) \
327 ((section)->VirtualAddress + ((DWORD_PTR)(offset) - (section)->PointerToRawData))
329 #define RVA_TO_PTR(var,section,filedata) \
330 ((unsigned char *)(RVA_TO_OFFSET (var,section) + (filedata).file_base))
334 /* Flip through the executable and cache the info necessary for dumping. */
336 get_section_info (file_data
*p_infile
)
338 PIMAGE_DOS_HEADER dos_header
;
339 PIMAGE_NT_HEADERS nt_header
;
342 dos_header
= (PIMAGE_DOS_HEADER
) p_infile
->file_base
;
343 if (dos_header
->e_magic
!= IMAGE_DOS_SIGNATURE
)
345 printf ("Unknown EXE header in %s...bailing.\n", p_infile
->name
);
348 nt_header
= (PIMAGE_NT_HEADERS
) (((DWORD_PTR
) dos_header
) +
349 dos_header
->e_lfanew
);
350 if (nt_header
== NULL
)
352 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
357 /* Check the NT header signature ... */
358 if (nt_header
->Signature
!= IMAGE_NT_SIGNATURE
)
360 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
361 nt_header
->Signature
, p_infile
->name
);
365 /* Locate the ".data" and ".bss" sections for Emacs. (Note that the
366 actual section names are probably different from these, and might
367 actually be the same section.)
369 We do this as follows: first we determine the virtual address
370 ranges in this process for the data and bss variables that we wish
371 to preserve. Then we map these VAs to the section entries in the
372 source image. Finally, we determine the new size of the raw data
373 area for the bss section, so we can make the new image the correct
376 /* We arrange for the Emacs initialized data to be in a separate
377 section if possible, because we cannot rely on my_begdata and
378 my_edata marking out the full extent of the initialized data, at
379 least on the Alpha where the linker freely reorders variables
380 across libraries. If we can arrange for this, all we need to do is
381 find the start and size of the EMDATA section. */
382 data_section
= find_section ("EMDATA", nt_header
);
385 data_start
= (char *) nt_header
->OptionalHeader
.ImageBase
+
386 data_section
->VirtualAddress
;
387 data_size
= data_section
->Misc
.VirtualSize
;
391 /* Fallback on the old method if compiler doesn't support the
392 data_set #pragma (or its equivalent). */
393 data_start
= my_begdata
;
394 data_size
= my_edata
- my_begdata
;
395 data_section
= rva_to_section (PTR_TO_RVA (my_begdata
), nt_header
);
396 if (data_section
!= rva_to_section (PTR_TO_RVA (my_edata
), nt_header
))
398 printf ("Initialized data is not in a single section...bailing\n");
403 /* As noted in lastfile.c, the Alpha (but not the Intel) MSVC linker
404 globally segregates all static and public bss data (ie. across all
405 linked modules, not just per module), so we must take both static
406 and public bss areas into account to determine the true extent of
407 the bss area used by Emacs.
409 To be strictly correct, we dump the static and public bss areas
410 used by Emacs separately if non-overlapping (since otherwise we are
411 dumping bss data belonging to system libraries, eg. the static bss
412 system data on the Alpha). */
414 bss_start
= my_begbss
;
415 bss_size
= my_endbss
- my_begbss
;
416 bss_section
= rva_to_section (PTR_TO_RVA (my_begbss
), nt_header
);
417 if (bss_section
!= rva_to_section (PTR_TO_RVA (my_endbss
), nt_header
))
419 printf ("Uninitialized data is not in a single section...bailing\n");
422 /* Compute how much the .bss section's raw data will grow. */
424 ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss
), bss_section
),
425 nt_header
->OptionalHeader
.FileAlignment
)
426 - bss_section
->SizeOfRawData
;
428 bss_start_static
= my_begbss_static
;
429 bss_size_static
= my_endbss_static
- my_begbss_static
;
430 bss_section_static
= rva_to_section (PTR_TO_RVA (my_begbss_static
), nt_header
);
431 if (bss_section_static
!= rva_to_section (PTR_TO_RVA (my_endbss_static
), nt_header
))
433 printf ("Uninitialized static data is not in a single section...bailing\n");
436 /* Compute how much the static .bss section's raw data will grow. */
437 extra_bss_size_static
=
438 ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss_static
), bss_section_static
),
439 nt_header
->OptionalHeader
.FileAlignment
)
440 - bss_section_static
->SizeOfRawData
;
442 /* Combine the bss sections into one if they overlap. */
444 overlap
= 1; /* force all bss data to be dumped */
448 if (bss_start
< bss_start_static
)
450 if (bss_start_static
< bss_start
+ bss_size
)
455 if (bss_start
< bss_start_static
+ bss_size_static
)
460 if (bss_section
!= bss_section_static
)
462 printf ("BSS data not in a single section...bailing\n");
465 bss_start
= min (bss_start
, bss_start_static
);
466 bss_size
= max (my_endbss
, my_endbss_static
) - bss_start
;
467 bss_section_static
= 0;
468 extra_bss_size_static
= 0;
473 /* The dump routines. */
476 copy_executable_and_dump_data (file_data
*p_infile
,
477 file_data
*p_outfile
)
479 unsigned char *dst
, *dst_save
;
480 PIMAGE_DOS_HEADER dos_header
;
481 PIMAGE_NT_HEADERS nt_header
;
482 PIMAGE_NT_HEADERS dst_nt_header
;
483 PIMAGE_SECTION_HEADER section
;
484 PIMAGE_SECTION_HEADER dst_section
;
487 int be_verbose
= GetEnvironmentVariable ("DEBUG_DUMP", NULL
, 0) > 0;
489 #define COPY_CHUNK(message, src, size, verbose) \
491 unsigned char *s = (void *)(src); \
492 unsigned long count = (size); \
495 printf ("%s\n", (message)); \
496 printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \
497 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
498 printf ("\t0x%08x Size in bytes.\n", count); \
500 memcpy (dst, s, count); \
504 #define COPY_PROC_CHUNK(message, src, size, verbose) \
506 unsigned char *s = (void *)(src); \
507 unsigned long count = (size); \
510 printf ("%s\n", (message)); \
511 printf ("\t0x%p Address in process.\n", s); \
512 printf ("\t0x%p Base output file.\n", p_outfile->file_base); \
513 printf ("\t0x%p Offset in output file.\n", dst - p_outfile->file_base); \
514 printf ("\t0x%p Address in output file.\n", dst); \
515 printf ("\t0x%p Size in bytes.\n", count); \
517 memcpy (dst, s, count); \
521 #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile)
522 #define ROUND_UP_DST(align) \
523 (dst = p_outfile->file_base + ROUND_UP (DST_TO_OFFSET (), (align)))
524 #define ROUND_UP_DST_AND_ZERO(align) \
526 unsigned char *newdst = p_outfile->file_base \
527 + ROUND_UP (DST_TO_OFFSET (), (align)); \
528 /* Zero the alignment slop; it may actually initialize real data. */ \
529 memset (dst, 0, newdst - dst); \
533 /* Copy the source image sequentially, ie. section by section after
534 copying the headers and section table, to simplify the process of
535 dumping the raw data for the bss and heap sections.
537 Note that dst is updated implicitly by each COPY_CHUNK. */
539 dos_header
= (PIMAGE_DOS_HEADER
) p_infile
->file_base
;
540 nt_header
= (PIMAGE_NT_HEADERS
) (((DWORD_PTR
) dos_header
) +
541 dos_header
->e_lfanew
);
542 section
= IMAGE_FIRST_SECTION (nt_header
);
544 dst
= (unsigned char *) p_outfile
->file_base
;
546 COPY_CHUNK ("Copying DOS header...", dos_header
,
547 (DWORD_PTR
) nt_header
- (DWORD_PTR
) dos_header
, be_verbose
);
548 dst_nt_header
= (PIMAGE_NT_HEADERS
) dst
;
549 COPY_CHUNK ("Copying NT header...", nt_header
,
550 (DWORD_PTR
) section
- (DWORD_PTR
) nt_header
, be_verbose
);
551 dst_section
= (PIMAGE_SECTION_HEADER
) dst
;
552 COPY_CHUNK ("Copying section table...", section
,
553 nt_header
->FileHeader
.NumberOfSections
* sizeof (*section
),
556 /* Align the first section's raw data area, and set the header size
557 field accordingly. */
558 ROUND_UP_DST_AND_ZERO (dst_nt_header
->OptionalHeader
.FileAlignment
);
559 dst_nt_header
->OptionalHeader
.SizeOfHeaders
= DST_TO_OFFSET ();
561 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
564 /* Windows section names are fixed 8-char strings, only
565 zero-terminated if the name is shorter than 8 characters. */
566 sprintf (msg
, "Copying raw data for %.8s...", section
->Name
);
570 /* Update the file-relative offset for this section's raw data (if
571 it has any) in case things have been relocated; we will update
572 the other offsets below once we know where everything is. */
573 if (dst_section
->PointerToRawData
)
574 dst_section
->PointerToRawData
= DST_TO_OFFSET ();
576 /* Can always copy the original raw data. */
578 (msg
, OFFSET_TO_PTR (section
->PointerToRawData
, p_infile
),
579 section
->SizeOfRawData
, be_verbose
);
580 /* Ensure alignment slop is zeroed. */
581 ROUND_UP_DST_AND_ZERO (dst_nt_header
->OptionalHeader
.FileAlignment
);
583 /* Note that various sections below may be aliases. */
584 if (section
== data_section
)
587 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start
), dst_section
);
588 COPY_PROC_CHUNK ("Dumping initialized data...",
589 data_start
, data_size
, be_verbose
);
590 dst
= dst_save
+ dst_section
->SizeOfRawData
;
592 if (section
== bss_section
)
594 /* Dump contents of bss variables, adjusting the section's raw
595 data size as necessary. */
597 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start
), dst_section
);
598 COPY_PROC_CHUNK ("Dumping bss data...", bss_start
,
599 bss_size
, be_verbose
);
600 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
601 dst_section
->PointerToRawData
= PTR_TO_OFFSET (dst_save
, p_outfile
);
602 /* Determine new size of raw data area. */
603 dst
= max (dst
, dst_save
+ dst_section
->SizeOfRawData
);
604 dst_section
->SizeOfRawData
= dst
- dst_save
;
605 dst_section
->Characteristics
&= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA
;
606 dst_section
->Characteristics
|= IMAGE_SCN_CNT_INITIALIZED_DATA
;
608 if (section
== bss_section_static
)
610 /* Dump contents of static bss variables, adjusting the
611 section's raw data size as necessary. */
613 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static
), dst_section
);
614 COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static
,
615 bss_size_static
, be_verbose
);
616 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
617 dst_section
->PointerToRawData
= PTR_TO_OFFSET (dst_save
, p_outfile
);
618 /* Determine new size of raw data area. */
619 dst
= max (dst
, dst_save
+ dst_section
->SizeOfRawData
);
620 dst_section
->SizeOfRawData
= dst
- dst_save
;
621 dst_section
->Characteristics
&= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA
;
622 dst_section
->Characteristics
|= IMAGE_SCN_CNT_INITIALIZED_DATA
;
625 /* Align the section's raw data area. */
626 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
632 /* Copy remainder of source image. */
635 while (section
->PointerToRawData
== 0);
636 offset
= ROUND_UP (section
->PointerToRawData
+ section
->SizeOfRawData
,
637 nt_header
->OptionalHeader
.FileAlignment
);
639 ("Copying remainder of executable...",
640 OFFSET_TO_PTR (offset
, p_infile
),
641 p_infile
->size
- offset
, be_verbose
);
643 /* Final size for new image. */
644 p_outfile
->size
= DST_TO_OFFSET ();
646 /* Now patch up remaining file-relative offsets. */
647 section
= IMAGE_FIRST_SECTION (nt_header
);
648 dst_section
= IMAGE_FIRST_SECTION (dst_nt_header
);
650 #define ADJUST_OFFSET(var) \
653 (var) = relocate_offset ((var), nt_header, dst_nt_header); \
656 dst_nt_header
->OptionalHeader
.SizeOfInitializedData
= 0;
657 dst_nt_header
->OptionalHeader
.SizeOfUninitializedData
= 0;
658 for (i
= 0; i
< dst_nt_header
->FileHeader
.NumberOfSections
; i
++)
660 /* Recompute data sizes for completeness. */
661 if (dst_section
[i
].Characteristics
& IMAGE_SCN_CNT_INITIALIZED_DATA
)
662 dst_nt_header
->OptionalHeader
.SizeOfInitializedData
+=
663 ROUND_UP (dst_section
[i
].Misc
.VirtualSize
, dst_nt_header
->OptionalHeader
.FileAlignment
);
664 else if (dst_section
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
665 dst_nt_header
->OptionalHeader
.SizeOfUninitializedData
+=
666 ROUND_UP (dst_section
[i
].Misc
.VirtualSize
, dst_nt_header
->OptionalHeader
.FileAlignment
);
668 ADJUST_OFFSET (dst_section
[i
].PointerToLinenumbers
);
671 ADJUST_OFFSET (dst_nt_header
->FileHeader
.PointerToSymbolTable
);
673 /* Update offsets in debug directory entries. */
675 IMAGE_DATA_DIRECTORY debug_dir
=
676 dst_nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
];
677 PIMAGE_DEBUG_DIRECTORY debug_entry
;
679 section
= rva_to_section (debug_dir
.VirtualAddress
, dst_nt_header
);
682 debug_entry
= (PIMAGE_DEBUG_DIRECTORY
)
683 (RVA_TO_OFFSET (debug_dir
.VirtualAddress
, section
) + p_outfile
->file_base
);
684 debug_dir
.Size
/= sizeof (IMAGE_DEBUG_DIRECTORY
);
686 for (i
= 0; i
< debug_dir
.Size
; i
++, debug_entry
++)
687 ADJUST_OFFSET (debug_entry
->PointerToRawData
);
693 /* Dump out .data and .bss sections into a new executable. */
695 unexec (const char *new_name
, const char *old_name
)
697 file_data in_file
, out_file
;
698 char out_filename
[MAX_PATH
], in_filename
[MAX_PATH
], new_name_a
[MAX_PATH
];
703 /* Ignore old_name, and get our actual location from the OS. */
704 if (!GetModuleFileNameA (NULL
, in_filename
, MAX_PATH
))
707 /* Can't use dostounix_filename here, since that needs its file name
708 argument encoded in UTF-8. */
709 for (p
= in_filename
; *p
; p
= CharNextA (p
))
713 strcpy (out_filename
, in_filename
);
714 filename_to_ansi (new_name
, new_name_a
);
716 /* Change the base of the output filename to match the requested name. */
717 if ((p
= strrchr (out_filename
, '/')) == NULL
)
719 /* The filenames have already been expanded, and will be in Unix
720 format, so it is safe to expect an absolute name. */
721 if ((q
= strrchr (new_name_a
, '/')) == NULL
)
725 #ifdef ENABLE_CHECKING
726 report_temacs_memory_usage ();
729 /* Make sure that the output filename has the ".exe" extension...patch
731 p
= out_filename
+ strlen (out_filename
) - 4;
732 if (strcmp (p
, ".exe"))
733 strcat (out_filename
, ".exe");
735 printf ("Dumping from %s\n", in_filename
);
736 printf (" to %s\n", out_filename
);
738 /* Open the undumped executable file. */
739 if (!open_input_file (&in_file
, in_filename
))
741 printf ("Failed to open %s (%d)...bailing.\n",
742 in_filename
, GetLastError ());
746 /* Get the interesting section info, like start and size of .bss... */
747 get_section_info (&in_file
);
749 /* The size of the dumped executable is the size of the original
750 executable plus the size of the heap and the size of the .bss section. */
751 size
= in_file
.size
+
753 extra_bss_size_static
;
754 if (!open_output_file (&out_file
, out_filename
, size
))
756 printf ("Failed to open %s (%d)...bailing.\n",
757 out_filename
, GetLastError ());
761 /* Set the flag (before dumping). */
762 using_dynamic_heap
= TRUE
;
764 copy_executable_and_dump_data (&in_file
, &out_file
);
766 /* Unset it because it is plain wrong to keep it after dumping.
767 Malloc can still occur! */
768 using_dynamic_heap
= FALSE
;
770 /* Patch up header fields; profiler is picky about this. */
772 PIMAGE_DOS_HEADER dos_header
;
773 PIMAGE_NT_HEADERS nt_header
;
774 HANDLE hImagehelp
= LoadLibrary ("imagehlp.dll");
778 dos_header
= (PIMAGE_DOS_HEADER
) out_file
.file_base
;
779 nt_header
= (PIMAGE_NT_HEADERS
) ((char *) dos_header
+ dos_header
->e_lfanew
);
781 nt_header
->OptionalHeader
.CheckSum
= 0;
782 // nt_header->FileHeader.TimeDateStamp = time (NULL);
783 // dos_header->e_cp = size / 512;
784 // nt_header->OptionalHeader.SizeOfImage = size;
786 pfnCheckSumMappedFile
= (void *) GetProcAddress (hImagehelp
, "CheckSumMappedFile");
787 if (pfnCheckSumMappedFile
)
789 // nt_header->FileHeader.TimeDateStamp = time (NULL);
790 pfnCheckSumMappedFile (out_file
.file_base
,
794 nt_header
->OptionalHeader
.CheckSum
= checksum
;
796 FreeLibrary (hImagehelp
);
799 close_file_data (&in_file
);
800 close_file_data (&out_file
);