annotate src/unexnt.c @ 337:fbbf69b4e8a7 r21-0-66

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