annotate src/unexnt.c @ 261:405dd6d1825b r20-5b29

Import from CVS: tag r20-5b29
author cvs
date Mon, 13 Aug 2007 10:23:51 +0200
parents 41ff10fd062f
children c42ec1d1cded
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
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
25 #include <stdlib.h> /* _fmode */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
26 #include <stdio.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
27 #include <fcntl.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
28 #include <windows.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
29
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
30 #if 0
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
31 extern BOOL ctrl_c_handler (unsigned long type);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
32 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
33
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
34 #include "ntheap.h"
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
35
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
36 /* 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
37
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
38 enum {
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
39 HEAP_UNINITIALIZED = 1,
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
40 HEAP_UNLOADED,
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
41 HEAP_LOADED
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
42 };
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
43
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
44 /* Basically, our "initialized" flag. */
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
45 int heap_state = HEAP_UNINITIALIZED;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
46
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
47 /* 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
48 unsigned long heap_index_in_executable = UNINIT_LONG;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
49
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
50 void get_section_info (file_data *p_file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
51 void copy_executable_and_dump_data_section (file_data *, file_data *);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
52 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
53
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
54 /* Cached info about the .data section in the executable. */
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
55 PUCHAR data_start_va = UNINIT_PTR;
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
56 DWORD data_start_file = UNINIT_LONG;
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
57 DWORD data_size = UNINIT_LONG;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
58
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
59 /* Cached info about the .bss section in the executable. */
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
60 PUCHAR bss_start = UNINIT_PTR;
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
61 DWORD bss_size = UNINIT_LONG;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
62
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
63 #ifdef HAVE_NTGUI
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
64 HINSTANCE hinst = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
65 HINSTANCE hprevinst = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
66 LPSTR lpCmdLine = "";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
67 int nCmdShow = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
68 #endif /* HAVE_NTGUI */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
69
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
70 /* Startup code for running on NT. When we are running as the dumped
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
71 version, we need to bootstrap our heap and .bss section into our
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
72 address space before we can actually hand off control to the startup
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
73 code supplied by NT (primarily because that code relies upon malloc ()). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
74 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
75 _start (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
76 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
77 extern void mainCRTStartup (void);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
78
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
79 /* Cache system info, e.g., the NT page size. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
80 cache_system_info ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
81
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
82 /* If we're a dumped version of emacs then we need to recreate
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
83 our heap and play tricks with our .bss section. Do this before
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
84 start up. (WARNING: Do not put any code before this section
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
85 that relies upon malloc () and runs in the dumped version. It
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
86 won't work.) */
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
87 if (heap_state == HEAP_UNLOADED)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
88 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
89 char executable_path[MAX_PATH];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
90
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
91 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
92 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
93 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
94 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
95 recreate_heap (executable_path);
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
96 heap_state = HEAP_LOADED;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
97 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
98
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
99 /* The default behavior is to treat files as binary and patch up
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
100 text files appropriately, in accordance with the MSDOS code. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
101 _fmode = O_BINARY;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
102
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
103 #if 0
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
104 /* This prevents ctrl-c's in shells running while we're suspended from
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
105 having us exit. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
106 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
107 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
108
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
109 /* Invoke the NT CRT startup routine now that our housecleaning
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
110 is finished. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
111 #ifdef HAVE_NTGUI
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
112 /* determine WinMain args like crt0.c does */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
113 hinst = GetModuleHandle(NULL);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
114 lpCmdLine = GetCommandLine();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
115 nCmdShow = SW_SHOWDEFAULT;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
116 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
117 mainCRTStartup ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
118 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
119
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
120 /* Dump out .data and .bss sections into a new executable. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
121 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
122 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
123 void *entry_address)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
124 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
125 file_data in_file, out_file;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
126 char out_filename[MAX_PATH], in_filename[MAX_PATH];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
127 unsigned long size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
128 char *ptr;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
129
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
130 /* Make sure that the input and output filenames have the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
131 ".exe" extension...patch them up if they don't. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
132 strcpy (in_filename, old_name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
133 ptr = in_filename + strlen (in_filename) - 4;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
134 if (strcmp (ptr, ".exe"))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
135 strcat (in_filename, ".exe");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
136
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
137 strcpy (out_filename, new_name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
138 ptr = out_filename + strlen (out_filename) - 4;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
139 if (strcmp (ptr, ".exe"))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
140 strcat (out_filename, ".exe");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
141
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
142 printf ("Dumping from %s\n", in_filename);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
143 printf (" to %s\n", out_filename);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
144
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
145 /* We need to round off our heap to NT's allocation unit (64KB). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
146 round_heap (get_allocation_unit ());
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
147
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
148 /* Open the undumped executable file. */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
149 if (!open_input_file (&in_file, in_filename))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
150 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
151 printf ("Failed to open %s (%d)...bailing.\n",
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
152 in_filename, GetLastError ());
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
153 exit (1);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
154 }
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
155
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
156 /* Get the interesting section info, like start and size of .bss... */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
157 get_section_info (&in_file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
158
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
159 /* The size of the dumped executable is the size of the original
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
160 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
161 heap_index_in_executable = (unsigned long)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
162 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ());
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
163 size = heap_index_in_executable + get_committed_heap_size () + bss_size;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
164 if (!open_output_file (&out_file, out_filename, size))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
165 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
166 printf ("Failed to open %s (%d)...bailing.\n",
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
167 out_filename, GetLastError ());
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
168 exit (1);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
169 }
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
170
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
171 /* Set the flag (before dumping). */
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
172 heap_state = HEAP_UNLOADED;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
173
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
174 copy_executable_and_dump_data_section (&in_file, &out_file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
175 dump_bss_and_heap (&in_file, &out_file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
176
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
177 close_file_data (&in_file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
178 close_file_data (&out_file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
179 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
180
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
181
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
182 /* File handling. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
183
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
184
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
185 int
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
186 open_input_file (file_data *p_file, char *filename)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
187 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
188 HANDLE file;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
189 HANDLE file_mapping;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
190 void *file_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
191 unsigned long size, upper_size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
192
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
193 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
194 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
195 if (file == INVALID_HANDLE_VALUE)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
196 return FALSE;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
197
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
198 size = GetFileSize (file, &upper_size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
199 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
200 0, size, NULL);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
201 if (!file_mapping)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
202 return FALSE;
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 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
205 if (file_base == 0)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
206 return FALSE;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
207
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
208 p_file->name = filename;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
209 p_file->size = size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
210 p_file->file = file;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
211 p_file->file_mapping = file_mapping;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
212 p_file->file_base = file_base;
209
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 return TRUE;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
215 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
216
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
217 int
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
218 open_output_file (file_data *p_file, char *filename, unsigned long size)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
219 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
220 HANDLE file;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
221 HANDLE file_mapping;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
222 void *file_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
223
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
224 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
225 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
226 if (file == INVALID_HANDLE_VALUE)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
227 return FALSE;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
228
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
229 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
230 0, size, NULL);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
231 if (!file_mapping)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
232 return FALSE;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
233
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
234 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
235 if (file_base == 0)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
236 return FALSE;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
237
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
238 p_file->name = filename;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
239 p_file->size = size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
240 p_file->file = file;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
241 p_file->file_mapping = file_mapping;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
242 p_file->file_base = file_base;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
243
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
244 return TRUE;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
245 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
246
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
247 /* Close the system structures associated with the given file. */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
248 void
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
249 close_file_data (file_data *p_file)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
250 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
251 UnmapViewOfFile (p_file->file_base);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
252 CloseHandle (p_file->file_mapping);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
253 CloseHandle (p_file->file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
254 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
255
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
256
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
257 /* Routines to manipulate NT executable file sections. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
258
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
259 static void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
260 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
261 DWORD *p_bss_size)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
262 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
263 int n, start, len;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
264 char map_filename[MAX_PATH];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
265 char buffer[256];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
266 FILE *map;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
267
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
268 /* Overwrite the .exe extension on the executable file name with
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
269 the .map extension. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
270 strcpy (map_filename, p_infile->name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
271 n = strlen (map_filename) - 3;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
272 strcpy (&map_filename[n], "map");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
273
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
274 map = fopen (map_filename, "r");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
275 if (!map)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
276 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
277 printf ("Failed to open map file %s, error %d...bailing out.\n",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
278 map_filename, GetLastError ());
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
279 exit (-1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
280 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
281
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
282 while (fgets (buffer, sizeof (buffer), map))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
283 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
284 if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA")))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
285 continue;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
286 n = sscanf (buffer, " %*d:%x %x", &start, &len);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
287 if (n != 2)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
288 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
289 printf ("Failed to scan the .bss section line:\n%s", buffer);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
290 exit (-1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
291 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
292 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
293 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
294 *p_bss_start = (PUCHAR) start;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
295 *p_bss_size = (DWORD) len;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
296 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
297
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
298 /* Return pointer to section header for named section. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
299 IMAGE_SECTION_HEADER *
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
300 find_section (char * name, IMAGE_NT_HEADERS * nt_header)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
301 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
302 PIMAGE_SECTION_HEADER section;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
303 int i;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
304
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
305 section = IMAGE_FIRST_SECTION (nt_header);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
306
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
307 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
308 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
309 if (strcmp (section->Name, name) == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
310 return section;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
311 section++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
312 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
313 return NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
314 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
315
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
316 /* Return pointer to section header for section containing the given
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
317 relative virtual address. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
318 IMAGE_SECTION_HEADER *
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
319 rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
320 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
321 PIMAGE_SECTION_HEADER section;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
322 int i;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
323
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
324 section = IMAGE_FIRST_SECTION (nt_header);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
325
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
326 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
327 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
328 if (rva >= section->VirtualAddress &&
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
329 rva < section->VirtualAddress + section->SizeOfRawData)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
330 return section;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
331 section++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
332 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
333 return NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
334 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 169
diff changeset
335
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
336 static unsigned long
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
337 get_section_size (PIMAGE_SECTION_HEADER p_section)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
338 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
339 /* The section size is in different locations in the different versions. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
340 switch (get_nt_minor_version ())
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
341 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
342 case 10:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
343 return p_section->SizeOfRawData;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
344 default:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
345 return p_section->Misc.VirtualSize;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
346 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
347 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
348
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
349 /* Flip through the executable and cache the info necessary for dumping. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
350 static void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
351 get_section_info (file_data *p_infile)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
352 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
353 PIMAGE_DOS_HEADER dos_header;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
354 PIMAGE_NT_HEADERS nt_header;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
355 PIMAGE_SECTION_HEADER section, data_section;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
356 unsigned char *ptr;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
357 int i;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
358
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
359 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
360 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
361 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
362 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
363 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
364 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
365 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
366 dos_header->e_lfanew);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
367 if (nt_header == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
368 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
369 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
370 p_infile->name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
371 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
372 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
373
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
374 /* Check the NT header signature ... */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
375 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
376 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
377 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
378 nt_header->Signature, p_infile->name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
379 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
380
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
381 /* Flip through the sections for .data and .bss ... */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
382 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
383 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
384 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
385 if (!strcmp (section->Name, ".bss"))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
386 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
387 /* The .bss section. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
388 ptr = (char *) nt_header->OptionalHeader.ImageBase +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
389 section->VirtualAddress;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
390 bss_start = ptr;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
391 bss_size = get_section_size (section);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
392 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
393 if (!strcmp (section->Name, ".data"))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
394 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
395 /* From lastfile.c */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
396 extern char my_edata[];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
397
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
398 /* The .data section. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
399 data_section = section;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
400 ptr = (char *) nt_header->OptionalHeader.ImageBase +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
401 section->VirtualAddress;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
402 data_start_va = ptr;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
403 data_start_file = section->PointerToRawData;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
404
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
405 /* We want to only write Emacs data back to the executable,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
406 not any of the library data (if library data is included,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
407 then a dumped Emacs won't run on system versions other
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
408 than the one Emacs was dumped on). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
409 data_size = my_edata - data_start_va;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
410 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
411 section++;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
412 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
413
169
15872534500d Import from CVS: tag r20-3b11
cvs
parents: 100
diff changeset
414 if (bss_start == UNINIT_PTR && bss_size == UNINIT_LONG)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
415 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
416 /* Starting with MSVC 4.0, the .bss section has been eliminated
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
417 and appended virtually to the end of the .data section. Our
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
418 only hint about where the .bss section starts in the address
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
419 comes from the SizeOfRawData field in the .data section
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
420 header. Unfortunately, this field is only approximate, as it
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
421 is a rounded number and is typically rounded just beyond the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
422 start of the .bss section. To find the start and size of the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
423 .bss section exactly, we have to peek into the map file. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
424 get_bss_info_from_map_file (p_infile, &ptr, &bss_size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
425 bss_start = ptr + nt_header->OptionalHeader.ImageBase
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
426 + data_section->VirtualAddress;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
427 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
428 }
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 /* The dump routines. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
432
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
433 static void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
434 copy_executable_and_dump_data_section (file_data *p_infile,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
435 file_data *p_outfile)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
436 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
437 unsigned char *data_file, *data_va;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
438 unsigned long size, index;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
439
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
440 /* 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
441 data_file = (char *) p_outfile->file_base + data_start_file;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
442
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
443 /* Get a pointer to the raw data in our address space. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
444 data_va = data_start_va;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
445
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
446 size = (DWORD) data_file - (DWORD) p_outfile->file_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
447 printf ("Copying executable up to data section...\n");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
448 printf ("\t0x%08x Offset in input file.\n", 0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
449 printf ("\t0x%08x Offset in output file.\n", 0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
450 printf ("\t0x%08x Size in bytes.\n", size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
451 memcpy (p_outfile->file_base, p_infile->file_base, size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
452
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
453 size = data_size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
454 printf ("Dumping .data section...\n");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
455 printf ("\t0x%08x Address in process.\n", data_va);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
456 printf ("\t0x%08x Offset in output file.\n",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
457 data_file - p_outfile->file_base);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
458 printf ("\t0x%08x Size in bytes.\n", size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
459 memcpy (data_file, data_va, size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
460
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
461 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
462 size = p_infile->size - index;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
463 printf ("Copying rest of executable...\n");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
464 printf ("\t0x%08x Offset in input file.\n", index);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
465 printf ("\t0x%08x Offset in output file.\n", index);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
466 printf ("\t0x%08x Size in bytes.\n", size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
467 memcpy ((char *) p_outfile->file_base + index,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
468 (char *) p_infile->file_base + index, size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
469 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
470
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
471 static void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
472 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
473 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
474 unsigned char *heap_data, *bss_data;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
475 unsigned long size, index;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
476
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
477 printf ("Dumping heap into executable...\n");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
478
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
479 index = heap_index_in_executable;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
480 size = get_committed_heap_size ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
481 heap_data = get_heap_start ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
482
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
483 printf ("\t0x%08x Heap start in process.\n", heap_data);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
484 printf ("\t0x%08x Heap offset in executable.\n", index);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
485 printf ("\t0x%08x Heap size in bytes.\n", size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
486
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
487 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
488
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
489 printf ("Dumping .bss into executable...\n");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
490
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
491 index += size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
492 size = bss_size;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
493 bss_data = bss_start;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
494
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
495 printf ("\t0x%08x BSS start in process.\n", bss_data);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
496 printf ("\t0x%08x BSS offset in executable.\n", index);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
497 printf ("\t0x%08x BSS size in bytes.\n", size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
498 memcpy ((char *) p_outfile->file_base + index, bss_data, size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
499 }
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 /* Reload and remap routines. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
503
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
504
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
505 /* Load the dumped .bss section into the .bss area of our address space. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
506 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
507 read_in_bss (char *filename)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
508 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
509 HANDLE file;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
510 unsigned long size, index, n_read, total_read;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
511 char buffer[512], *bss;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
512 int i;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
513
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
514 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
515 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
516 if (file == INVALID_HANDLE_VALUE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
517 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
518 i = GetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
519 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
520 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
521
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
522 /* Seek to where the .bss section is tucked away after the heap... */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
523 index = heap_index_in_executable + get_committed_heap_size ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
524 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
525 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
526 i = GetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
527 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
528 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
529
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
530
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
531 /* Ok, read in the saved .bss section and initialize all
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
532 uninitialized variables. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
533 if (!ReadFile (file, bss_start, bss_size, &n_read, NULL))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
534 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
535 i = GetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
536 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
537 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
538
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
539 CloseHandle (file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
540 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
541
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
542 /* Map the heap dumped into the executable file into our address space. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
543 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
544 map_in_heap (char *filename)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
545 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
546 HANDLE file;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
547 HANDLE file_mapping;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
548 void *file_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
549 unsigned long size, upper_size, n_read;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
550 int i;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
551
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
552 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
553 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
554 if (file == INVALID_HANDLE_VALUE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
555 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
556 i = GetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
557 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
558 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
559
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
560 size = GetFileSize (file, &upper_size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
561 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
562 0, size, NULL);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
563 if (!file_mapping)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
564 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
565 i = GetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
566 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
567 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
568
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
569 size = get_committed_heap_size ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
570 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
571 heap_index_in_executable, size,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
572 get_heap_start ());
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
573 if (file_base != 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
574 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
575 return;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
576 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
577
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
578 /* If we don't succeed with the mapping, then copy from the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
579 data into the heap. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
580
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
581 CloseHandle (file_mapping);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
582
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
583 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
584 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
585 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
586 i = GetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
587 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
588 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
589
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
590 /* Seek to the location of the heap data in the executable. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
591 i = heap_index_in_executable;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
592 if (SetFilePointer (file, i, NULL, FILE_BEGIN) == 0xFFFFFFFF)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
593 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
594 i = GetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
595 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
596 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
597
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
598 /* Read in the data. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
599 if (!ReadFile (file, get_heap_start (),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
600 get_committed_heap_size (), &n_read, NULL))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
601 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
602 i = GetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
603 exit (1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
604 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
605
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
606 CloseHandle (file);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
607 }