1 /* unexec for GNU Emacs on Windows NT.
2 Copyright (C) 1994, 2001-2017 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 <https://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%lx 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
= max (extra_bss_size
, extra_bss_size_static
);
469 extra_bss_size_static
= 0;
473 /* Format to print a DWORD_PTR value. */
474 #if defined MINGW_W64 && defined _WIN64
475 # define pDWP "16llx"
480 /* The dump routines. */
483 copy_executable_and_dump_data (file_data
*p_infile
,
484 file_data
*p_outfile
)
486 unsigned char *dst
, *dst_save
;
487 PIMAGE_DOS_HEADER dos_header
;
488 PIMAGE_NT_HEADERS nt_header
;
489 PIMAGE_NT_HEADERS dst_nt_header
;
490 PIMAGE_SECTION_HEADER section
;
491 PIMAGE_SECTION_HEADER dst_section
;
494 int be_verbose
= GetEnvironmentVariable ("DEBUG_DUMP", NULL
, 0) > 0;
496 #define COPY_CHUNK(message, src, size, verbose) \
498 unsigned char *s = (void *)(src); \
499 DWORD_PTR count = (size); \
502 printf ("%s\n", (message)); \
503 printf ("\t0x%"pDWP" Offset in input file.\n", (DWORD_PTR)(s - p_infile->file_base)); \
504 printf ("\t0x%"pDWP" Offset in output file.\n", (DWORD_PTR)(dst - p_outfile->file_base)); \
505 printf ("\t0x%"pDWP" Size in bytes.\n", count); \
507 memcpy (dst, s, count); \
511 #define COPY_PROC_CHUNK(message, src, size, verbose) \
513 unsigned char *s = (void *)(src); \
514 DWORD_PTR count = (size); \
517 printf ("%s\n", (message)); \
518 printf ("\t0x%p Address in process.\n", s); \
519 printf ("\t0x%p Base output file.\n", p_outfile->file_base); \
520 printf ("\t0x%"pDWP" Offset in output file.\n", (DWORD_PTR)(dst - p_outfile->file_base)); \
521 printf ("\t0x%p Address in output file.\n", dst); \
522 printf ("\t0x%"pDWP" Size in bytes.\n", count); \
524 memcpy (dst, s, count); \
528 #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile)
529 #define ROUND_UP_DST(align) \
530 (dst = p_outfile->file_base + ROUND_UP (DST_TO_OFFSET (), (align)))
531 #define ROUND_UP_DST_AND_ZERO(align) \
533 unsigned char *newdst = p_outfile->file_base \
534 + ROUND_UP (DST_TO_OFFSET (), (align)); \
535 /* Zero the alignment slop; it may actually initialize real data. */ \
536 memset (dst, 0, newdst - dst); \
540 /* Copy the source image sequentially, ie. section by section after
541 copying the headers and section table, to simplify the process of
542 dumping the raw data for the bss and heap sections.
544 Note that dst is updated implicitly by each COPY_CHUNK. */
546 dos_header
= (PIMAGE_DOS_HEADER
) p_infile
->file_base
;
547 nt_header
= (PIMAGE_NT_HEADERS
) (((DWORD_PTR
) dos_header
) +
548 dos_header
->e_lfanew
);
549 section
= IMAGE_FIRST_SECTION (nt_header
);
551 dst
= (unsigned char *) p_outfile
->file_base
;
553 COPY_CHUNK ("Copying DOS header...", dos_header
,
554 (DWORD_PTR
) nt_header
- (DWORD_PTR
) dos_header
, be_verbose
);
555 dst_nt_header
= (PIMAGE_NT_HEADERS
) dst
;
556 COPY_CHUNK ("Copying NT header...", nt_header
,
557 (DWORD_PTR
) section
- (DWORD_PTR
) nt_header
, be_verbose
);
558 dst_section
= (PIMAGE_SECTION_HEADER
) dst
;
559 COPY_CHUNK ("Copying section table...", section
,
560 nt_header
->FileHeader
.NumberOfSections
* sizeof (*section
),
563 /* Align the first section's raw data area, and set the header size
564 field accordingly. */
565 ROUND_UP_DST_AND_ZERO (dst_nt_header
->OptionalHeader
.FileAlignment
);
566 dst_nt_header
->OptionalHeader
.SizeOfHeaders
= DST_TO_OFFSET ();
568 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
571 /* Windows section names are fixed 8-char strings, only
572 zero-terminated if the name is shorter than 8 characters. */
573 sprintf (msg
, "Copying raw data for %.8s...", section
->Name
);
577 /* Update the file-relative offset for this section's raw data (if
578 it has any) in case things have been relocated; we will update
579 the other offsets below once we know where everything is. */
580 if (dst_section
->PointerToRawData
)
581 dst_section
->PointerToRawData
= DST_TO_OFFSET ();
583 /* Can always copy the original raw data. */
585 (msg
, OFFSET_TO_PTR (section
->PointerToRawData
, p_infile
),
586 section
->SizeOfRawData
, be_verbose
);
587 /* Ensure alignment slop is zeroed. */
588 ROUND_UP_DST_AND_ZERO (dst_nt_header
->OptionalHeader
.FileAlignment
);
590 /* Note that various sections below may be aliases. */
591 if (section
== data_section
)
594 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start
), dst_section
);
595 COPY_PROC_CHUNK ("Dumping initialized data...",
596 data_start
, data_size
, be_verbose
);
597 dst
= dst_save
+ dst_section
->SizeOfRawData
;
599 if (section
== bss_section
)
601 /* Dump contents of bss variables, adjusting the section's raw
602 data size as necessary. */
604 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start
), dst_section
);
605 COPY_PROC_CHUNK ("Dumping bss data...", bss_start
,
606 bss_size
, be_verbose
);
607 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
608 dst_section
->PointerToRawData
= PTR_TO_OFFSET (dst_save
, p_outfile
);
609 /* Determine new size of raw data area. */
610 dst
= max (dst
, dst_save
+ dst_section
->SizeOfRawData
);
611 dst_section
->SizeOfRawData
= dst
- dst_save
;
612 dst_section
->Characteristics
&= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA
;
613 dst_section
->Characteristics
|= IMAGE_SCN_CNT_INITIALIZED_DATA
;
615 if (section
== bss_section_static
)
617 /* Dump contents of static bss variables, adjusting the
618 section's raw data size as necessary. */
620 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static
), dst_section
);
621 COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static
,
622 bss_size_static
, be_verbose
);
623 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
624 dst_section
->PointerToRawData
= PTR_TO_OFFSET (dst_save
, p_outfile
);
625 /* Determine new size of raw data area. */
626 dst
= max (dst
, dst_save
+ dst_section
->SizeOfRawData
);
627 dst_section
->SizeOfRawData
= dst
- dst_save
;
628 dst_section
->Characteristics
&= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA
;
629 dst_section
->Characteristics
|= IMAGE_SCN_CNT_INITIALIZED_DATA
;
632 /* Align the section's raw data area. */
633 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
639 /* Copy remainder of source image. */
642 while (section
->PointerToRawData
== 0);
643 offset
= ROUND_UP (section
->PointerToRawData
+ section
->SizeOfRawData
,
644 nt_header
->OptionalHeader
.FileAlignment
);
646 ("Copying remainder of executable...",
647 OFFSET_TO_PTR (offset
, p_infile
),
648 p_infile
->size
- offset
, be_verbose
);
650 /* Final size for new image. */
651 p_outfile
->size
= DST_TO_OFFSET ();
653 /* Now patch up remaining file-relative offsets. */
654 section
= IMAGE_FIRST_SECTION (nt_header
);
655 dst_section
= IMAGE_FIRST_SECTION (dst_nt_header
);
657 #define ADJUST_OFFSET(var) \
660 (var) = relocate_offset ((var), nt_header, dst_nt_header); \
663 dst_nt_header
->OptionalHeader
.SizeOfInitializedData
= 0;
664 dst_nt_header
->OptionalHeader
.SizeOfUninitializedData
= 0;
665 for (i
= 0; i
< dst_nt_header
->FileHeader
.NumberOfSections
; i
++)
667 /* Recompute data sizes for completeness. */
668 if (dst_section
[i
].Characteristics
& IMAGE_SCN_CNT_INITIALIZED_DATA
)
669 dst_nt_header
->OptionalHeader
.SizeOfInitializedData
+=
670 ROUND_UP (dst_section
[i
].Misc
.VirtualSize
, dst_nt_header
->OptionalHeader
.FileAlignment
);
671 else if (dst_section
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
672 dst_nt_header
->OptionalHeader
.SizeOfUninitializedData
+=
673 ROUND_UP (dst_section
[i
].Misc
.VirtualSize
, dst_nt_header
->OptionalHeader
.FileAlignment
);
675 ADJUST_OFFSET (dst_section
[i
].PointerToLinenumbers
);
678 ADJUST_OFFSET (dst_nt_header
->FileHeader
.PointerToSymbolTable
);
680 /* Update offsets in debug directory entries. */
682 IMAGE_DATA_DIRECTORY debug_dir
=
683 dst_nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
];
684 PIMAGE_DEBUG_DIRECTORY debug_entry
;
686 section
= rva_to_section (debug_dir
.VirtualAddress
, dst_nt_header
);
689 debug_entry
= (PIMAGE_DEBUG_DIRECTORY
)
690 (RVA_TO_OFFSET (debug_dir
.VirtualAddress
, section
) + p_outfile
->file_base
);
691 debug_dir
.Size
/= sizeof (IMAGE_DEBUG_DIRECTORY
);
693 for (i
= 0; i
< debug_dir
.Size
; i
++, debug_entry
++)
694 ADJUST_OFFSET (debug_entry
->PointerToRawData
);
700 /* Dump out .data and .bss sections into a new executable. */
702 unexec (const char *new_name
, const char *old_name
)
704 file_data in_file
, out_file
;
705 char out_filename
[MAX_PATH
], in_filename
[MAX_PATH
], new_name_a
[MAX_PATH
];
710 /* Ignore old_name, and get our actual location from the OS. */
711 if (!GetModuleFileNameA (NULL
, in_filename
, MAX_PATH
))
714 /* Can't use dostounix_filename here, since that needs its file name
715 argument encoded in UTF-8. */
716 for (p
= in_filename
; *p
; p
= CharNextA (p
))
720 strcpy (out_filename
, in_filename
);
721 filename_to_ansi (new_name
, new_name_a
);
723 /* Change the base of the output filename to match the requested name. */
724 if ((p
= strrchr (out_filename
, '/')) == NULL
)
726 /* The filenames have already been expanded, and will be in Unix
727 format, so it is safe to expect an absolute name. */
728 if ((q
= strrchr (new_name_a
, '/')) == NULL
)
732 #ifdef ENABLE_CHECKING
733 report_temacs_memory_usage ();
736 /* Make sure that the output filename has the ".exe" extension...patch
738 p
= out_filename
+ strlen (out_filename
) - 4;
739 if (strcmp (p
, ".exe"))
740 strcat (out_filename
, ".exe");
742 printf ("Dumping from %s\n", in_filename
);
743 printf (" to %s\n", out_filename
);
745 /* Open the undumped executable file. */
746 if (!open_input_file (&in_file
, in_filename
))
748 printf ("Failed to open %s (%lu)...bailing.\n",
749 in_filename
, GetLastError ());
753 /* Get the interesting section info, like start and size of .bss... */
754 get_section_info (&in_file
);
756 /* The size of the dumped executable is the size of the original
757 executable plus the size of the heap and the size of the .bss section. */
758 size
= in_file
.size
+
760 extra_bss_size_static
;
761 if (!open_output_file (&out_file
, out_filename
, size
))
763 printf ("Failed to open %s (%lu)...bailing.\n",
764 out_filename
, GetLastError ());
768 /* Set the flag (before dumping). */
769 using_dynamic_heap
= TRUE
;
771 copy_executable_and_dump_data (&in_file
, &out_file
);
773 /* Unset it because it is plain wrong to keep it after dumping.
774 Malloc can still occur! */
775 using_dynamic_heap
= FALSE
;
777 /* Patch up header fields; profiler is picky about this. */
779 PIMAGE_DOS_HEADER dos_header
;
780 PIMAGE_NT_HEADERS nt_header
;
781 HANDLE hImagehelp
= LoadLibrary ("imagehlp.dll");
785 dos_header
= (PIMAGE_DOS_HEADER
) out_file
.file_base
;
786 nt_header
= (PIMAGE_NT_HEADERS
) ((char *) dos_header
+ dos_header
->e_lfanew
);
788 nt_header
->OptionalHeader
.CheckSum
= 0;
789 // nt_header->FileHeader.TimeDateStamp = time (NULL);
790 // dos_header->e_cp = size / 512;
791 // nt_header->OptionalHeader.SizeOfImage = size;
793 pfnCheckSumMappedFile
= (void *) GetProcAddress (hImagehelp
, "CheckSumMappedFile");
794 if (pfnCheckSumMappedFile
)
796 // nt_header->FileHeader.TimeDateStamp = time (NULL);
797 pfnCheckSumMappedFile (out_file
.file_base
,
801 nt_header
->OptionalHeader
.CheckSum
= checksum
;
803 FreeLibrary (hImagehelp
);
806 close_file_data (&in_file
);
807 close_file_data (&out_file
);