1 /* unexec for GNU Emacs on Windows NT.
2 Copyright (C) 1994 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 2, or (at your option)
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; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Geoff Voelker (voelker@cs.washington.edu) 8-12-94
26 #include <stdlib.h> /* _fmode */
32 /* Include relevant definitions from IMAGEHLP.H, which can be found
33 in \\win32sdk\mstools\samples\image\include\imagehlp.h. */
36 (__stdcall
* pfnCheckSumMappedFile
) (LPVOID BaseAddress
,
41 extern BOOL
ctrl_c_handler (unsigned long type
);
43 extern char my_begdata
[];
44 extern char my_edata
[];
45 extern char my_begbss
[];
46 extern char my_endbss
[];
47 extern char *my_begbss_static
;
48 extern char *my_endbss_static
;
54 #define min(x, y) (((x) < (y)) ? (x) : (y))
55 #define max(x, y) (((x) > (y)) ? (x) : (y))
57 /* Basically, our "initialized" flag. */
58 BOOL using_dynamic_heap
= FALSE
;
60 int open_input_file (file_data
*p_file
, char *name
);
61 int open_output_file (file_data
*p_file
, char *name
, unsigned long size
);
62 void close_file_data (file_data
*p_file
);
64 void get_section_info (file_data
*p_file
);
65 void copy_executable_and_dump_data (file_data
*, file_data
*);
66 void dump_bss_and_heap (file_data
*p_infile
, file_data
*p_outfile
);
68 /* Cached info about the .data section in the executable. */
69 PIMAGE_SECTION_HEADER data_section
;
70 PUCHAR data_start
= 0;
73 /* Cached info about the .bss section in the executable. */
74 PIMAGE_SECTION_HEADER bss_section
;
77 DWORD extra_bss_size
= 0;
78 /* bss data that is static might be discontiguous from non-static. */
79 PIMAGE_SECTION_HEADER bss_section_static
;
80 PUCHAR bss_start_static
= 0;
81 DWORD bss_size_static
= 0;
82 DWORD extra_bss_size_static
= 0;
84 PIMAGE_SECTION_HEADER heap_section
;
87 HINSTANCE hinst
= NULL
;
88 HINSTANCE hprevinst
= NULL
;
91 #endif /* HAVE_NTGUI */
93 /* Startup code for running on NT. When we are running as the dumped
94 version, we need to bootstrap our heap and .bss section into our
95 address space before we can actually hand off control to the startup
96 code supplied by NT (primarily because that code relies upon malloc ()). */
100 extern void mainCRTStartup (void);
103 /* Give us a way to debug problems with crashes on startup when
104 running under the MSVC profiler. */
105 if (GetEnvironmentVariable ("EMACS_DEBUG", NULL
, 0) > 0)
109 /* Cache system info, e.g., the NT page size. */
110 cache_system_info ();
112 /* Grab our malloc arena space now, before CRT starts up. */
115 /* The default behavior is to treat files as binary and patch up
116 text files appropriately, in accordance with the MSDOS code. */
119 /* This prevents ctrl-c's in shells running while we're suspended from
121 SetConsoleCtrlHandler ((PHANDLER_ROUTINE
) ctrl_c_handler
, TRUE
);
123 /* Prevent Emacs from being locked up (eg. in batch mode) when
124 accessing devices that aren't mounted (eg. removable media drives). */
125 SetErrorMode (SEM_FAILCRITICALERRORS
);
127 /* Invoke the NT CRT startup routine now that our housecleaning
130 /* determine WinMain args like crt0.c does */
131 hinst
= GetModuleHandle(NULL
);
132 lpCmdLine
= GetCommandLine();
133 nCmdShow
= SW_SHOWDEFAULT
;
142 open_input_file (file_data
*p_file
, char *filename
)
147 unsigned long size
, upper_size
;
149 file
= CreateFile (filename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
150 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0);
151 if (file
== INVALID_HANDLE_VALUE
)
154 size
= GetFileSize (file
, &upper_size
);
155 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_READONLY
,
160 file_base
= MapViewOfFile (file_mapping
, FILE_MAP_READ
, 0, 0, size
);
164 p_file
->name
= filename
;
167 p_file
->file_mapping
= file_mapping
;
168 p_file
->file_base
= file_base
;
174 open_output_file (file_data
*p_file
, char *filename
, unsigned long size
)
180 file
= CreateFile (filename
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
181 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
182 if (file
== INVALID_HANDLE_VALUE
)
185 file_mapping
= CreateFileMapping (file
, NULL
, PAGE_READWRITE
,
190 file_base
= MapViewOfFile (file_mapping
, FILE_MAP_WRITE
, 0, 0, size
);
194 p_file
->name
= filename
;
197 p_file
->file_mapping
= file_mapping
;
198 p_file
->file_base
= file_base
;
203 /* Close the system structures associated with the given file. */
205 close_file_data (file_data
*p_file
)
207 UnmapViewOfFile (p_file
->file_base
);
208 CloseHandle (p_file
->file_mapping
);
209 /* For the case of output files, set final size. */
210 SetFilePointer (p_file
->file
, p_file
->size
, NULL
, FILE_BEGIN
);
211 SetEndOfFile (p_file
->file
);
212 CloseHandle (p_file
->file
);
216 /* Routines to manipulate NT executable file sections. */
218 /* Return pointer to section header for named section. */
219 IMAGE_SECTION_HEADER
*
220 find_section (char * name
, IMAGE_NT_HEADERS
* nt_header
)
222 PIMAGE_SECTION_HEADER section
;
225 section
= IMAGE_FIRST_SECTION (nt_header
);
227 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
229 if (strcmp (section
->Name
, name
) == 0)
236 /* Return pointer to section header for section containing the given
237 relative virtual address. */
238 IMAGE_SECTION_HEADER
*
239 rva_to_section (DWORD rva
, IMAGE_NT_HEADERS
* nt_header
)
241 PIMAGE_SECTION_HEADER section
;
244 section
= IMAGE_FIRST_SECTION (nt_header
);
246 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
248 /* Some linkers (eg. the NT SDK linker I believe) swapped the
249 meaning of these two values - or rather, they ignored
250 VirtualSize entirely and always set it to zero. This affects
251 some very old exes (eg. gzip dated Dec 1993). Since
252 w32_executable_type relies on this function to work reliably,
253 we need to cope with this. */
254 DWORD real_size
= max (section
->SizeOfRawData
,
255 section
->Misc
.VirtualSize
);
256 if (rva
>= section
->VirtualAddress
257 && rva
< section
->VirtualAddress
+ real_size
)
264 /* Return pointer to section header for section containing the given
265 offset in its raw data area. */
266 IMAGE_SECTION_HEADER
*
267 offset_to_section (DWORD offset
, IMAGE_NT_HEADERS
* nt_header
)
269 PIMAGE_SECTION_HEADER section
;
272 section
= IMAGE_FIRST_SECTION (nt_header
);
274 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
276 if (offset
>= section
->PointerToRawData
277 && offset
< section
->PointerToRawData
+ section
->SizeOfRawData
)
284 /* Return offset to an object in dst, given offset in src. We assume
285 there is at least one section in both src and dst images, and that
286 the some sections may have been added to dst (after sections in src). */
288 relocate_offset (DWORD offset
,
289 IMAGE_NT_HEADERS
* src_nt_header
,
290 IMAGE_NT_HEADERS
* dst_nt_header
)
292 PIMAGE_SECTION_HEADER src_section
= IMAGE_FIRST_SECTION (src_nt_header
);
293 PIMAGE_SECTION_HEADER dst_section
= IMAGE_FIRST_SECTION (dst_nt_header
);
296 while (offset
>= src_section
->PointerToRawData
)
298 if (offset
< src_section
->PointerToRawData
+ src_section
->SizeOfRawData
)
301 if (i
== src_nt_header
->FileHeader
.NumberOfSections
)
303 /* Handle offsets after the last section. */
304 dst_section
= IMAGE_FIRST_SECTION (dst_nt_header
);
305 dst_section
+= dst_nt_header
->FileHeader
.NumberOfSections
- 1;
306 while (dst_section
->PointerToRawData
== 0)
308 while (src_section
->PointerToRawData
== 0)
311 + (dst_section
->PointerToRawData
+ dst_section
->SizeOfRawData
)
312 - (src_section
->PointerToRawData
+ src_section
->SizeOfRawData
);
318 (dst_section
->PointerToRawData
- src_section
->PointerToRawData
);
321 #define OFFSET_TO_RVA(offset, section) \
322 (section->VirtualAddress + ((DWORD)(offset) - section->PointerToRawData))
324 #define RVA_TO_OFFSET(rva, section) \
325 (section->PointerToRawData + ((DWORD)(rva) - section->VirtualAddress))
327 #define RVA_TO_SECTION_OFFSET(rva, section) \
328 ((DWORD)(rva) - section->VirtualAddress)
330 /* Convert address in executing image to RVA. */
331 #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL))
333 #define PTR_TO_OFFSET(ptr, pfile_data) \
334 ((char *)(ptr) - (pfile_data)->file_base)
336 #define OFFSET_TO_PTR(offset, pfile_data) \
337 ((pfile_data)->file_base + (DWORD)(offset))
340 /* Flip through the executable and cache the info necessary for dumping. */
342 get_section_info (file_data
*p_infile
)
344 PIMAGE_DOS_HEADER dos_header
;
345 PIMAGE_NT_HEADERS nt_header
;
346 PIMAGE_SECTION_HEADER section
;
349 dos_header
= (PIMAGE_DOS_HEADER
) p_infile
->file_base
;
350 if (dos_header
->e_magic
!= IMAGE_DOS_SIGNATURE
)
352 printf ("Unknown EXE header in %s...bailing.\n", p_infile
->name
);
355 nt_header
= (PIMAGE_NT_HEADERS
) (((unsigned long) dos_header
) +
356 dos_header
->e_lfanew
);
357 if (nt_header
== NULL
)
359 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
364 /* Check the NT header signature ... */
365 if (nt_header
->Signature
!= IMAGE_NT_SIGNATURE
)
367 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
368 nt_header
->Signature
, p_infile
->name
);
372 /* Locate the ".data" and ".bss" sections for Emacs. (Note that the
373 actual section names are probably different from these, and might
374 actually be the same section.)
376 We do this as follows: first we determine the virtual address
377 ranges in this process for the data and bss variables that we wish
378 to preserve. Then we map these VAs to the section entries in the
379 source image. Finally, we determine the new size of the raw data
380 area for the bss section, so we can make the new image the correct
383 data_start
= my_begdata
;
384 data_size
= my_edata
- my_begdata
;
385 data_section
= rva_to_section (PTR_TO_RVA (my_begdata
), nt_header
);
386 if (data_section
!= rva_to_section (PTR_TO_RVA (my_edata
), nt_header
))
388 printf ("Initialized data is not in a single section...bailing\n");
392 /* As noted in lastfile.c, the Alpha (but not the Intel) MSVC linker
393 globally segregates all static and public bss data (ie. across all
394 linked modules, not just per module), so we must take both static
395 and public bss areas into account to determine the true extent of
396 the bss area used by Emacs.
398 To be strictly correct, we dump the static and public bss areas
399 used by Emacs separately if non-overlapping (since otherwise we are
400 dumping bss data belonging to system libraries, eg. the static bss
401 system data on the Alpha). */
403 bss_start
= my_begbss
;
404 bss_size
= my_endbss
- my_begbss
;
405 bss_section
= rva_to_section (PTR_TO_RVA (my_begbss
), nt_header
);
406 if (bss_section
!= rva_to_section (PTR_TO_RVA (my_endbss
), nt_header
))
408 printf ("Uninitialized data is not in a single section...bailing\n");
411 /* Compute how much the .bss section's raw data will grow. */
413 ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss
), bss_section
),
414 nt_header
->OptionalHeader
.FileAlignment
)
415 - bss_section
->SizeOfRawData
;
417 bss_start_static
= my_begbss_static
;
418 bss_size_static
= my_endbss_static
- my_begbss_static
;
419 bss_section_static
= rva_to_section (PTR_TO_RVA (my_begbss_static
), nt_header
);
420 if (bss_section_static
!= rva_to_section (PTR_TO_RVA (my_endbss_static
), nt_header
))
422 printf ("Uninitialized static data is not in a single section...bailing\n");
425 /* Compute how much the static .bss section's raw data will grow. */
426 extra_bss_size_static
=
427 ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss_static
), bss_section_static
),
428 nt_header
->OptionalHeader
.FileAlignment
)
429 - bss_section_static
->SizeOfRawData
;
431 /* Combine the bss sections into one if they overlap. */
433 if (bss_start
< bss_start_static
)
435 if (bss_start_static
< bss_start
+ bss_size
)
440 if (bss_start
< bss_start_static
+ bss_size_static
)
445 if (bss_section
!= bss_section_static
)
447 printf ("BSS data not in a single section...bailing\n");
450 bss_start
= min (bss_start
, bss_start_static
);
451 bss_size
= max (my_endbss
, my_endbss_static
) - bss_start
;
452 bss_section_static
= 0;
453 extra_bss_size_static
= 0;
456 heap_section
= rva_to_section (PTR_TO_RVA (get_heap_start ()), nt_header
);
460 /* The dump routines. */
463 copy_executable_and_dump_data (file_data
*p_infile
,
464 file_data
*p_outfile
)
466 unsigned char *dst
, *dst_save
;
467 PIMAGE_DOS_HEADER dos_header
;
468 PIMAGE_NT_HEADERS nt_header
;
469 PIMAGE_NT_HEADERS dst_nt_header
;
470 PIMAGE_SECTION_HEADER section
;
471 PIMAGE_SECTION_HEADER dst_section
;
475 #define COPY_CHUNK(message, src, size) \
477 unsigned char *s = (void *)(src); \
478 unsigned long count = (size); \
479 printf ("%s\n", (message)); \
480 printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \
481 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
482 printf ("\t0x%08x Size in bytes.\n", count); \
483 memcpy (dst, s, count); \
487 #define COPY_PROC_CHUNK(message, src, size) \
489 unsigned char *s = (void *)(src); \
490 unsigned long count = (size); \
491 printf ("%s\n", (message)); \
492 printf ("\t0x%08x Address in process.\n", s); \
493 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
494 printf ("\t0x%08x Size in bytes.\n", count); \
495 memcpy (dst, s, count); \
499 #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile)
500 #define ROUND_UP_DST(align) \
501 (dst = p_outfile->file_base + ROUND_UP (DST_TO_OFFSET (), (align)))
502 #define ROUND_UP_DST_AND_ZERO(align) \
504 unsigned char *newdst = p_outfile->file_base \
505 + ROUND_UP (DST_TO_OFFSET (), (align)); \
506 /* Zero the alignment slop; it may actually initialize real data. */ \
507 memset (dst, 0, newdst - dst); \
511 /* Copy the source image sequentially, ie. section by section after
512 copying the headers and section table, to simplify the process of
513 dumping the raw data for the bss and heap sections.
515 Note that dst is updated implicitly by each COPY_CHUNK. */
517 dos_header
= (PIMAGE_DOS_HEADER
) p_infile
->file_base
;
518 nt_header
= (PIMAGE_NT_HEADERS
) (((unsigned long) dos_header
) +
519 dos_header
->e_lfanew
);
520 section
= IMAGE_FIRST_SECTION (nt_header
);
522 dst
= (unsigned char *) p_outfile
->file_base
;
524 COPY_CHUNK ("Copying DOS header...", dos_header
,
525 (DWORD
) nt_header
- (DWORD
) dos_header
);
526 dst_nt_header
= (PIMAGE_NT_HEADERS
) dst
;
527 COPY_CHUNK ("Copying NT header...", nt_header
,
528 (DWORD
) section
- (DWORD
) nt_header
);
529 dst_section
= (PIMAGE_SECTION_HEADER
) dst
;
530 COPY_CHUNK ("Copying section table...", section
,
531 nt_header
->FileHeader
.NumberOfSections
* sizeof (*section
));
533 /* Align the first section's raw data area, and set the header size
534 field accordingly. */
535 ROUND_UP_DST_AND_ZERO (dst_nt_header
->OptionalHeader
.FileAlignment
);
536 dst_nt_header
->OptionalHeader
.SizeOfHeaders
= DST_TO_OFFSET ();
538 for (i
= 0; i
< nt_header
->FileHeader
.NumberOfSections
; i
++)
541 sprintf (msg
, "Copying raw data for %s...", section
->Name
);
545 /* Update the file-relative offset for this section's raw data (if
546 it has any) in case things have been relocated; we will update
547 the other offsets below once we know where everything is. */
548 if (dst_section
->PointerToRawData
)
549 dst_section
->PointerToRawData
= DST_TO_OFFSET ();
551 /* Can always copy the original raw data. */
553 (msg
, OFFSET_TO_PTR (section
->PointerToRawData
, p_infile
),
554 section
->SizeOfRawData
);
555 /* Ensure alignment slop is zeroed. */
556 ROUND_UP_DST_AND_ZERO (dst_nt_header
->OptionalHeader
.FileAlignment
);
558 /* Note that various sections below may be aliases. */
559 if (section
== data_section
)
562 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start
), dst_section
);
563 COPY_PROC_CHUNK ("Dumping initialized data...", data_start
, data_size
);
564 dst
= dst_save
+ dst_section
->SizeOfRawData
;
566 if (section
== bss_section
)
568 /* Dump contents of bss variables, adjusting the section's raw
569 data size as necessary. */
571 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start
), dst_section
);
572 COPY_PROC_CHUNK ("Dumping bss data...", bss_start
, bss_size
);
573 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
574 dst_section
->PointerToRawData
= PTR_TO_OFFSET (dst_save
, p_outfile
);
575 /* Determine new size of raw data area. */
576 dst
= max (dst
, dst_save
+ dst_section
->SizeOfRawData
);
577 dst_section
->SizeOfRawData
= dst
- dst_save
;
578 dst_section
->Characteristics
&= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA
;
579 dst_section
->Characteristics
|= IMAGE_SCN_CNT_INITIALIZED_DATA
;
581 if (section
== bss_section_static
)
583 /* Dump contents of static bss variables, adjusting the
584 section's raw data size as necessary. */
586 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static
), dst_section
);
587 COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static
, bss_size_static
);
588 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
589 dst_section
->PointerToRawData
= PTR_TO_OFFSET (dst_save
, p_outfile
);
590 /* Determine new size of raw data area. */
591 dst
= max (dst
, dst_save
+ dst_section
->SizeOfRawData
);
592 dst_section
->SizeOfRawData
= dst
- dst_save
;
593 dst_section
->Characteristics
&= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA
;
594 dst_section
->Characteristics
|= IMAGE_SCN_CNT_INITIALIZED_DATA
;
596 if (section
== heap_section
)
598 DWORD heap_start
= get_heap_start ();
599 DWORD heap_size
= get_committed_heap_size ();
601 /* Dump the used portion of the predump heap, adjusting the
602 section's size to the appropriate size. */
604 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (heap_start
), dst_section
);
605 COPY_PROC_CHUNK ("Dumping heap...", heap_start
, heap_size
);
606 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
607 dst_section
->PointerToRawData
= PTR_TO_OFFSET (dst_save
, p_outfile
);
608 /* Determine new size of raw data area. */
609 dst
= max (dst
, dst_save
+ dst_section
->SizeOfRawData
);
610 dst_section
->SizeOfRawData
= dst
- dst_save
;
611 /* Reduce the size of the heap section to fit (must be last
613 dst_nt_header
->OptionalHeader
.SizeOfImage
-=
614 dst_section
->Misc
.VirtualSize
615 - ROUND_UP (dst_section
->SizeOfRawData
,
616 dst_nt_header
->OptionalHeader
.SectionAlignment
);
617 dst_section
->Misc
.VirtualSize
=
618 ROUND_UP (dst_section
->SizeOfRawData
,
619 dst_nt_header
->OptionalHeader
.SectionAlignment
);
620 dst_section
->Characteristics
&= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA
;
621 dst_section
->Characteristics
|= IMAGE_SCN_CNT_INITIALIZED_DATA
;
624 /* Align the section's raw data area. */
625 ROUND_UP_DST (dst_nt_header
->OptionalHeader
.FileAlignment
);
631 /* Copy remainder of source image. */
634 while (section
->PointerToRawData
== 0);
635 offset
= ROUND_UP (section
->PointerToRawData
+ section
->SizeOfRawData
,
636 nt_header
->OptionalHeader
.FileAlignment
);
638 ("Copying remainder of executable...",
639 OFFSET_TO_PTR (offset
, p_infile
),
640 p_infile
->size
- offset
);
642 /* Final size for new image. */
643 p_outfile
->size
= DST_TO_OFFSET ();
645 /* Now patch up remaining file-relative offsets. */
646 section
= IMAGE_FIRST_SECTION (nt_header
);
647 dst_section
= IMAGE_FIRST_SECTION (dst_nt_header
);
649 #define ADJUST_OFFSET(var) \
652 (var) = relocate_offset ((var), nt_header, dst_nt_header); \
655 dst_nt_header
->OptionalHeader
.SizeOfInitializedData
= 0;
656 dst_nt_header
->OptionalHeader
.SizeOfUninitializedData
= 0;
657 for (i
= 0; i
< dst_nt_header
->FileHeader
.NumberOfSections
; i
++)
659 /* Recompute data sizes for completeness. */
660 if (dst_section
[i
].Characteristics
& IMAGE_SCN_CNT_INITIALIZED_DATA
)
661 dst_nt_header
->OptionalHeader
.SizeOfInitializedData
+=
662 ROUND_UP (dst_section
[i
].Misc
.VirtualSize
, dst_nt_header
->OptionalHeader
.FileAlignment
);
663 else if (dst_section
[i
].Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
664 dst_nt_header
->OptionalHeader
.SizeOfUninitializedData
+=
665 ROUND_UP (dst_section
[i
].Misc
.VirtualSize
, dst_nt_header
->OptionalHeader
.FileAlignment
);
667 ADJUST_OFFSET (dst_section
[i
].PointerToLinenumbers
);
670 ADJUST_OFFSET (dst_nt_header
->FileHeader
.PointerToSymbolTable
);
672 /* Update offsets in debug directory entries. */
674 IMAGE_DATA_DIRECTORY debug_dir
=
675 dst_nt_header
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
];
676 PIMAGE_DEBUG_DIRECTORY debug_entry
;
678 section
= rva_to_section (debug_dir
.VirtualAddress
, dst_nt_header
);
681 debug_entry
= (PIMAGE_DEBUG_DIRECTORY
)
682 (RVA_TO_OFFSET (debug_dir
.VirtualAddress
, section
) + p_outfile
->file_base
);
683 debug_dir
.Size
/= sizeof (IMAGE_DEBUG_DIRECTORY
);
685 for (i
= 0; i
< debug_dir
.Size
; i
++, debug_entry
++)
686 ADJUST_OFFSET (debug_entry
->PointerToRawData
);
692 /* Dump out .data and .bss sections into a new executable. */
694 unexec (char *new_name
, char *old_name
, void *start_data
, void *start_bss
,
697 file_data in_file
, out_file
;
698 char out_filename
[MAX_PATH
], in_filename
[MAX_PATH
];
702 /* Make sure that the input and output filenames have the
703 ".exe" extension...patch them up if they don't. */
704 strcpy (in_filename
, old_name
);
705 ptr
= in_filename
+ strlen (in_filename
) - 4;
706 if (strcmp (ptr
, ".exe"))
707 strcat (in_filename
, ".exe");
709 strcpy (out_filename
, new_name
);
710 ptr
= out_filename
+ strlen (out_filename
) - 4;
711 if (strcmp (ptr
, ".exe"))
712 strcat (out_filename
, ".exe");
714 printf ("Dumping from %s\n", in_filename
);
715 printf (" to %s\n", out_filename
);
717 /* We need to round off our heap to NT's page size. */
718 round_heap (get_page_size ());
720 /* Open the undumped executable file. */
721 if (!open_input_file (&in_file
, in_filename
))
723 printf ("Failed to open %s (%d)...bailing.\n",
724 in_filename
, GetLastError ());
728 /* Get the interesting section info, like start and size of .bss... */
729 get_section_info (&in_file
);
731 /* The size of the dumped executable is the size of the original
732 executable plus the size of the heap and the size of the .bss section. */
733 size
= in_file
.size
+
734 get_committed_heap_size () +
736 extra_bss_size_static
;
737 if (!open_output_file (&out_file
, out_filename
, size
))
739 printf ("Failed to open %s (%d)...bailing.\n",
740 out_filename
, GetLastError ());
744 /* Set the flag (before dumping). */
745 using_dynamic_heap
= TRUE
;
747 copy_executable_and_dump_data (&in_file
, &out_file
);
749 /* Patch up header fields; profiler is picky about this. */
751 PIMAGE_DOS_HEADER dos_header
;
752 PIMAGE_NT_HEADERS nt_header
;
753 HANDLE hImagehelp
= LoadLibrary ("imagehlp.dll");
757 dos_header
= (PIMAGE_DOS_HEADER
) out_file
.file_base
;
758 nt_header
= (PIMAGE_NT_HEADERS
) ((char *) dos_header
+ dos_header
->e_lfanew
);
760 nt_header
->OptionalHeader
.CheckSum
= 0;
761 // nt_header->FileHeader.TimeDateStamp = time (NULL);
762 // dos_header->e_cp = size / 512;
763 // nt_header->OptionalHeader.SizeOfImage = size;
765 pfnCheckSumMappedFile
= (void *) GetProcAddress (hImagehelp
, "CheckSumMappedFile");
766 if (pfnCheckSumMappedFile
)
768 // nt_header->FileHeader.TimeDateStamp = time (NULL);
769 pfnCheckSumMappedFile (out_file
.file_base
,
773 nt_header
->OptionalHeader
.CheckSum
= checksum
;
775 FreeLibrary (hImagehelp
);
778 close_file_data (&in_file
);
779 close_file_data (&out_file
);