annotate src/unexnt.c @ 440:8de8e3f6228a r21-2-28

Import from CVS: tag r21-2-28
author cvs
date Mon, 13 Aug 2007 11:33:38 +0200
parents 3ecd8885ac67
children abe6d1db359e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* unexec for GNU Emacs on Windows NT.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1994 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to the Free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 Geoff Voelker (voelker@cs.washington.edu) 8-12-94 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 /* The linkers that come with MSVC >= 4.0 merge .bss into .data and reorder
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 * uninitialised data so that the .data section looks like:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 * crt0 initialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 * emacs initialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 * <my_edata>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 * library initialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 * <start of bss part of .data>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 * emacs static uninitialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 * library static uninitialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 * emacs global uninitialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 * <my_ebss>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 * library global uninitialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 * This means that we can't use the normal my_ebss in lastfile.c trick to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 * differentiate between unitialised data that belongs to emacs and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 * uninitialised data that belongs to system libraries. This is bad because
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 * we do want to initialise the emacs data, but we don't want to initialise
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 * the system library data.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 * To solve this problem using MSVC >= 5.0 we use a pragma directive to tell
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 * the compiler to put emacs's data (both initialised and uninitialised) in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 * a separate section in the executable, and we only dump that section. This
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 * means that all files that define initialized data must include config.h
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 * to pick up the pragma. We don't try to make any part of that section
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 * read-only.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 * This pragma directive isn't supported by the MSVC 4.x compiler. Instead,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 * we dump crt0 initialised data and library static uninitialised data in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 * addition to the emacs data. This is wrong, but we appear to be able to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 * get away with it. A proper fix might involve the introduction of a static
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 * version of my_ebss in lastfile.c and a new firstfile.c file. jhar */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 #include <stdlib.h> /* _fmode */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 #include <fcntl.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 #include <windows.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 PIMAGE_NT_HEADERS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress, DWORD FileLength,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 LPDWORD HeaderSum, LPDWORD CheckSum);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 extern BOOL ctrl_c_handler (unsigned long type);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 #include "ntheap.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 /* Sync with FSF Emacs 19.34.6 note: struct file_data is now defined in ntheap.h */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 enum {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 HEAP_UNINITIALIZED = 1,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 HEAP_UNLOADED,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 HEAP_LOADED
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 /* Basically, our "initialized" flag. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 int heap_state = HEAP_UNINITIALIZED;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 /* So we can find our heap in the file to recreate it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 unsigned long heap_index_in_executable = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 void get_section_info (file_data *p_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 void copy_executable_and_dump_data_section (file_data *, file_data *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 /* Cached info about the .data section in the executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 PUCHAR data_start_va = UNINIT_PTR;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 DWORD data_start_file = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 DWORD data_size = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 /* Cached info about the .bss section in the executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 PUCHAR bss_start = UNINIT_PTR;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 DWORD bss_size = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 #ifdef HAVE_NTGUI
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 HINSTANCE hinst = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 HINSTANCE hprevinst = NULL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 LPSTR lpCmdLine = "";
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 int nCmdShow = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 #endif /* HAVE_NTGUI */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 /* Startup code for running on NT. When we are running as the dumped
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 version, we need to bootstrap our heap and .bss section into our
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 address space before we can actually hand off control to the startup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 code supplied by NT (primarily because that code relies upon malloc ()). */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
114
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
115 /* **********************
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
116 Hackers please remember, this _start() thingy is *not* called neither
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
117 when dumping portably, nor when running from temacs! Do not put
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
118 significant XEmacs initialization here!
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
119 ********************** */
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
120
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 _start (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 extern void mainCRTStartup (void);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 /* Cache system info, e.g., the NT page size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 cache_system_info ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 /* If we're a dumped version of emacs then we need to recreate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 our heap and play tricks with our .bss section. Do this before
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 start up. (WARNING: Do not put any code before this section
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 that relies upon malloc () and runs in the dumped version. It
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 won't work.) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 if (heap_state == HEAP_UNLOADED)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 char executable_path[MAX_PATH];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
143 /* #### This is super-bogus. When I rename xemacs.exe,
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
144 the renamed file still loads its heap from xemacs.exe --kkm */
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
145 #if 0
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
146 {
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
147 /* To allow profiling, make sure executable_path names the .exe
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
148 file, not the file created by the profiler */
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
149 char *p = strrchr (executable_path, '\\');
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
150 strcpy (p+1, PATH_PROGNAME ".exe");
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
151 }
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
152 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 recreate_heap (executable_path);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 heap_state = HEAP_LOADED;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
158 /* #### This is bogus, too. _fmode is set to different values
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
159 when we run `xemacs' and `temacs run-emacs'. The sooner we
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
160 hit and fix all the weirdities this causes us, the better --kkm */
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
161 #if 0
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 /* The default behavior is to treat files as binary and patch up
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 text files appropriately, in accordance with the MSDOS code. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 _fmode = O_BINARY;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
165 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 /* This prevents ctrl-c's in shells running while we're suspended from
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 having us exit. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 /* Invoke the NT CRT startup routine now that our housecleaning
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 is finished. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 #ifdef HAVE_NTGUI
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 /* determine WinMain args like crt0.c does */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 hinst = GetModuleHandle(NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 lpCmdLine = GetCommandLine();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 nCmdShow = SW_SHOWDEFAULT;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 mainCRTStartup ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 /* Dump out .data and .bss sections into a new executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 void *entry_address)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 file_data in_file, out_file;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 char out_filename[MAX_PATH], in_filename[MAX_PATH];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 unsigned long size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 char *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 HANDLE hImagehelp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 /* Make sure that the input and output filenames have the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 ".exe" extension...patch them up if they don't. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 strcpy (in_filename, old_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 ptr = in_filename + strlen (in_filename) - 4;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 if (strcmp (ptr, ".exe"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 strcat (in_filename, ".exe");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 strcpy (out_filename, new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 ptr = out_filename + strlen (out_filename) - 4;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 if (strcmp (ptr, ".exe"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 strcat (out_filename, ".exe");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207 printf ("Dumping from %s\n", in_filename);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 printf (" to %s\n", out_filename);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 /* We need to round off our heap to NT's allocation unit (64KB). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 round_heap (get_allocation_unit ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 /* Open the undumped executable file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 if (!open_input_file (&in_file, in_filename))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 printf ("Failed to open %s (%d)...bailing.\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 in_filename, GetLastError ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 /* Get the interesting section info, like start and size of .bss... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 get_section_info (&in_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 /* The size of the dumped executable is the size of the original
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 executable plus the size of the heap and the size of the .bss section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 heap_index_in_executable = (unsigned long)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 size = heap_index_in_executable + get_committed_heap_size () + bss_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 if (!open_output_file (&out_file, out_filename, size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 printf ("Failed to open %s (%d)...bailing.\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 out_filename, GetLastError ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 /* Set the flag (before dumping). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 heap_state = HEAP_UNLOADED;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 copy_executable_and_dump_data_section (&in_file, &out_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 dump_bss_and_heap (&in_file, &out_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 /* Patch up header fields; profiler is picky about this. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 hImagehelp = LoadLibrary ("imagehlp.dll");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 if (hImagehelp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 PIMAGE_DOS_HEADER dos_header;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 PIMAGE_NT_HEADERS nt_header;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 DWORD headersum;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 DWORD checksum;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 nt_header->OptionalHeader.CheckSum = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 // nt_header->FileHeader.TimeDateStamp = time (NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 // dos_header->e_cp = size / 512;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 // nt_header->OptionalHeader.SizeOfImage = size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 if (pfnCheckSumMappedFile)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 // nt_header->FileHeader.TimeDateStamp = time (NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 pfnCheckSumMappedFile (out_file.file_base,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 out_file.size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 &headersum,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 &checksum);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 nt_header->OptionalHeader.CheckSum = checksum;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 FreeLibrary (hImagehelp);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 close_file_data (&in_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 close_file_data (&out_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 /* File handling. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 open_output_file (file_data *p_file, CONST char *filename, unsigned long size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 HANDLE file;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 HANDLE file_mapping;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 void *file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 if (file == INVALID_HANDLE_VALUE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 return FALSE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 0, size, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 if (!file_mapping)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 return FALSE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 if (file_base == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 return FALSE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 p_file->name = filename;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 p_file->size = size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 p_file->file = file;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 p_file->file_mapping = file_mapping;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 p_file->file_base = file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 return TRUE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 /* Routines to manipulate NT executable file sections. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 DWORD *p_bss_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 int n, start, len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 char map_filename[MAX_PATH];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 char buffer[256];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 FILE *map;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 /* Overwrite the .exe extension on the executable file name with
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 the .map extension. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 strcpy (map_filename, p_infile->name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 n = strlen (map_filename) - 3;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 strcpy (&map_filename[n], "map");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 map = fopen (map_filename, "r");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 if (!map)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 printf ("Failed to open map file %s, error %d...bailing out.\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 map_filename, GetLastError ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 exit (-1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 while (fgets (buffer, sizeof (buffer), map))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 n = sscanf (buffer, " %*d:%x %x", &start, &len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 if (n != 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 printf ("Failed to scan the .bss section line:\n%s", buffer);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 exit (-1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 *p_bss_start = (PUCHAR) start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 *p_bss_size = (DWORD) len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 /* Flip through the executable and cache the info necessary for dumping. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 get_section_info (file_data *p_infile)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357 PIMAGE_DOS_HEADER dos_header;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 PIMAGE_NT_HEADERS nt_header;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 PIMAGE_SECTION_HEADER section, data_section;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 unsigned char *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 int i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 dos_header->e_lfanew);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 if (nt_header == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 p_infile->name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 /* Check the NT header signature ... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 nt_header->Signature, p_infile->name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385 /* Flip through the sections for .data and .bss ... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 if (!strcmp (section->Name, ".bss"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 extern int my_ebss; /* From lastfile.c */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 ptr = (char *) nt_header->OptionalHeader.ImageBase +
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 section->VirtualAddress;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 bss_start = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 bss_size = (char*)&my_ebss - (char*)bss_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 if (!strcmp (section->Name, ".data"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 if (!strcmp (section->Name, "xdata"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 extern char my_edata[]; /* From lastfile.c */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 /* The .data section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408 data_section = section;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 ptr = (char *) nt_header->OptionalHeader.ImageBase +
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 section->VirtualAddress;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 data_start_va = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 data_start_file = section->PointerToRawData;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 /* Write only the part of the section that contains emacs data. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 data_size = my_edata - data_start_va;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 /* Write back the full section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 data_size = section->SizeOfRawData;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 /* This code doesn't know how to grow the raw size of a section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 if (section->SizeOfRawData < section->Misc.VirtualSize)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 printf ("The emacs data section is smaller than expected"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 "...bailing.\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 section++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 if (bss_start == UNINIT_PTR)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 /* Starting with MSVC 4.0, the .bss section has been eliminated
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 and appended virtually to the end of the .data section. Our
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 only hint about where the .bss section starts in the address
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 comes from the SizeOfRawData field in the .data section
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 header. Unfortunately, this field is only approximate, as it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 is a rounded number and is typically rounded just beyond the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 start of the .bss section. To find the start and size of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 .bss section exactly, we have to peek into the map file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 extern int my_ebss;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 get_bss_info_from_map_file (p_infile, &ptr, &bss_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 bss_start = ptr + nt_header->OptionalHeader.ImageBase
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 + data_section->VirtualAddress;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 bss_size = (char*)&my_ebss - (char*)bss_start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 bss_size = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 /* The dump routines. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 #ifdef DEBUG_XEMACS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 #define DUMP_MSG(x) printf x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 #define DUMP_MSG(x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 copy_executable_and_dump_data_section (file_data *p_infile,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 file_data *p_outfile)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 unsigned char *data_file, *data_va;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 unsigned long size, index;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 /* Get a pointer to where the raw data should go in the executable file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 data_file = (char *) p_outfile->file_base + data_start_file;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 /* Get a pointer to the raw data in our address space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 data_va = data_start_va;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 size = (DWORD) data_file - (DWORD) p_outfile->file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 DUMP_MSG (("Copying executable up to data section...\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 DUMP_MSG (("\t0x%08x Offset in input file.\n", 0));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 DUMP_MSG (("\t0x%08x Offset in output file.\n", 0));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 memcpy (p_outfile->file_base, p_infile->file_base, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 size = data_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 DUMP_MSG (("Dumping data section...\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 DUMP_MSG (("\t0x%08x Address in process.\n", data_va));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 DUMP_MSG (("\t0x%08x Offset in output file.\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 data_file - p_outfile->file_base));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 memcpy (data_file, data_va, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 size = p_infile->size - index;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 DUMP_MSG (("Copying rest of executable...\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 DUMP_MSG (("\t0x%08x Offset in input file.\n", index));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 DUMP_MSG (("\t0x%08x Offset in output file.\n", index));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 memcpy ((char *) p_outfile->file_base + index,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 (char *) p_infile->file_base + index, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 unsigned char *heap_data;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 unsigned long size, index;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 DUMP_MSG (("Dumping heap onto end of executable...\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 index = heap_index_in_executable;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 size = get_committed_heap_size ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 heap_data = get_heap_start ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 DUMP_MSG (("\t0x%08x Heap start in process.\n", heap_data));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 DUMP_MSG (("\t0x%08x Heap offset in executable.\n", index));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 DUMP_MSG (("\t0x%08x Heap size in bytes.\n", size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 DUMP_MSG (("Dumping bss onto end of executable...\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 index += size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 size = bss_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 DUMP_MSG (("\t0x%08x BSS start in process.\n", bss_start));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 DUMP_MSG (("\t0x%08x BSS offset in executable.\n", index));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 DUMP_MSG (("\t0x%08x BSS size in bytes.\n", size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 memcpy ((char *) p_outfile->file_base + index, bss_start, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 #undef DUMP_MSG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 /* Reload and remap routines. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 /* Load the dumped .bss section into the .bss area of our address space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 /* Already done if the .bss was part of a separate emacs data section */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 read_in_bss (char *filename)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 HANDLE file;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 unsigned long index, n_read;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 if (file == INVALID_HANDLE_VALUE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 abort ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 /* Seek to where the .bss section is tucked away after the heap... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 index = heap_index_in_executable + get_committed_heap_size ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
555 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 abort ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 /* Ok, read in the saved .bss section and initialize all
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 uninitialized variables. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 if (!ReadFile (file, bss_start, bss_size, &n_read, NULL))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 abort ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 CloseHandle (file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 /* Map the heap dumped into the executable file into our address space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 map_in_heap (char *filename)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 HANDLE file;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 HANDLE file_mapping;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 void *file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 unsigned long size, upper_size, n_read;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 if (file == INVALID_HANDLE_VALUE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 abort ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 size = GetFileSize (file, &upper_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 0, size, NULL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 if (!file_mapping)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 abort ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 size = get_committed_heap_size ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 heap_index_in_executable, size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 get_heap_start ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591 if (file_base != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 /* If we don't succeed with the mapping, then copy from the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 data into the heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599 CloseHandle (file_mapping);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 abort ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 /* Seek to the location of the heap data in the executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 if (SetFilePointer (file, heap_index_in_executable,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 NULL, FILE_BEGIN) == 0xFFFFFFFF)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 abort ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 /* Read in the data. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 if (!ReadFile (file, get_heap_start (),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 get_committed_heap_size (), &n_read, NULL))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 abort ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615 CloseHandle (file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 }