comparison src/unexnt.c @ 442:abe6d1db359e r21-2-36

Import from CVS: tag r21-2-36
author cvs
date Mon, 13 Aug 2007 11:35:02 +0200
parents 8de8e3f6228a
children ed498ef2108b
comparison
equal deleted inserted replaced
441:72a7cfa4a488 442:abe6d1db359e
54 * addition to the emacs data. This is wrong, but we appear to be able to 54 * addition to the emacs data. This is wrong, but we appear to be able to
55 * get away with it. A proper fix might involve the introduction of a static 55 * get away with it. A proper fix might involve the introduction of a static
56 * version of my_ebss in lastfile.c and a new firstfile.c file. jhar */ 56 * version of my_ebss in lastfile.c and a new firstfile.c file. jhar */
57 57
58 #include <config.h> 58 #include <config.h>
59 #include <stdlib.h> /* _fmode */ 59 #include "lisp.h"
60 #include <stdio.h> 60
61 #include <fcntl.h> 61 #include "syswindows.h"
62 #include <windows.h> 62
63 #include "nt.h"
64 #include "ntheap.h"
63 65
64 /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */ 66 /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */
65 /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */ 67 /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */
66 PIMAGE_NT_HEADERS 68 typedef PIMAGE_NT_HEADERS
67 (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress, DWORD FileLength, 69 (__stdcall * pfnCheckSumMappedFile_t) (LPVOID BaseAddress, DWORD FileLength,
68 LPDWORD HeaderSum, LPDWORD CheckSum); 70 LPDWORD HeaderSum, LPDWORD CheckSum);
71
69 72
70 #if 0 73 #if 0
71 extern BOOL ctrl_c_handler (unsigned long type); 74 extern BOOL ctrl_c_handler (unsigned long type);
72 #endif 75 #endif
73 76
74 #include "ntheap.h" 77 /* Sync with FSF Emacs 19.34.6
75 78 note: struct file_data is now defined in nt.h */
76 /* Sync with FSF Emacs 19.34.6 note: struct file_data is now defined in ntheap.h */
77 79
78 enum { 80 enum {
79 HEAP_UNINITIALIZED = 1, 81 HEAP_UNINITIALIZED = 1,
80 HEAP_UNLOADED, 82 HEAP_UNLOADED,
81 HEAP_LOADED 83 HEAP_LOADED
97 DWORD data_size = UNINIT_LONG; 99 DWORD data_size = UNINIT_LONG;
98 100
99 /* Cached info about the .bss section in the executable. */ 101 /* Cached info about the .bss section in the executable. */
100 PUCHAR bss_start = UNINIT_PTR; 102 PUCHAR bss_start = UNINIT_PTR;
101 DWORD bss_size = UNINIT_LONG; 103 DWORD bss_size = UNINIT_LONG;
102
103 #ifdef HAVE_NTGUI
104 HINSTANCE hinst = NULL;
105 HINSTANCE hprevinst = NULL;
106 LPSTR lpCmdLine = "";
107 int nCmdShow = 0;
108 #endif /* HAVE_NTGUI */
109 104
110 /* Startup code for running on NT. When we are running as the dumped 105 /* Startup code for running on NT. When we are running as the dumped
111 version, we need to bootstrap our heap and .bss section into our 106 version, we need to bootstrap our heap and .bss section into our
112 address space before we can actually hand off control to the startup 107 address space before we can actually hand off control to the startup
113 code supplied by NT (primarily because that code relies upon malloc ()). */ 108 code supplied by NT (primarily because that code relies upon malloc ()). */
158 /* #### This is bogus, too. _fmode is set to different values 153 /* #### This is bogus, too. _fmode is set to different values
159 when we run `xemacs' and `temacs run-emacs'. The sooner we 154 when we run `xemacs' and `temacs run-emacs'. The sooner we
160 hit and fix all the weirdities this causes us, the better --kkm */ 155 hit and fix all the weirdities this causes us, the better --kkm */
161 #if 0 156 #if 0
162 /* The default behavior is to treat files as binary and patch up 157 /* The default behavior is to treat files as binary and patch up
163 text files appropriately, in accordance with the MSDOS code. */ 158 text files appropriately. */
164 _fmode = O_BINARY; 159 _fmode = O_BINARY;
165 #endif 160 #endif
166 161
167 #if 0 162 #if 0
168 /* This prevents ctrl-c's in shells running while we're suspended from 163 /* This prevents ctrl-c's in shells running while we're suspended from
169 having us exit. */ 164 having us exit. */
170 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE); 165 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
171 #endif 166 #endif
172 167
173 /* Invoke the NT CRT startup routine now that our housecleaning
174 is finished. */
175 #ifdef HAVE_NTGUI
176 /* determine WinMain args like crt0.c does */
177 hinst = GetModuleHandle(NULL);
178 lpCmdLine = GetCommandLine();
179 nCmdShow = SW_SHOWDEFAULT;
180 #endif
181 mainCRTStartup (); 168 mainCRTStartup ();
182 } 169 }
183 170
184 /* Dump out .data and .bss sections into a new executable. */ 171 /* Dump out .data and .bss sections into a new executable. */
185 void 172 int
186 unexec (char *new_name, char *old_name, void *start_data, void *start_bss, 173 unexec (char *new_name, char *old_name, unsigned int start_data,
187 void *entry_address) 174 unsigned int start_bss, unsigned int entry_address)
188 { 175 {
189 file_data in_file, out_file; 176 file_data in_file, out_file;
190 char out_filename[MAX_PATH], in_filename[MAX_PATH]; 177 char out_filename[MAX_PATH], in_filename[MAX_PATH];
191 unsigned long size; 178 unsigned long size;
192 char *ptr; 179 char *ptr;
193 HANDLE hImagehelp; 180 HINSTANCE hImagehelp;
194 181
195 /* Make sure that the input and output filenames have the 182 /* Make sure that the input and output filenames have the
196 ".exe" extension...patch them up if they don't. */ 183 ".exe" extension...patch them up if they don't. */
197 strcpy (in_filename, old_name); 184 strcpy (in_filename, old_name);
198 ptr = in_filename + strlen (in_filename) - 4; 185 ptr = in_filename + strlen (in_filename) - 4;
243 hImagehelp = LoadLibrary ("imagehlp.dll"); 230 hImagehelp = LoadLibrary ("imagehlp.dll");
244 if (hImagehelp) 231 if (hImagehelp)
245 { 232 {
246 PIMAGE_DOS_HEADER dos_header; 233 PIMAGE_DOS_HEADER dos_header;
247 PIMAGE_NT_HEADERS nt_header; 234 PIMAGE_NT_HEADERS nt_header;
235
248 DWORD headersum; 236 DWORD headersum;
249 DWORD checksum; 237 DWORD checksum;
238 pfnCheckSumMappedFile_t pfnCheckSumMappedFile;
250 239
251 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base; 240 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
252 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew); 241 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
253 242
254 nt_header->OptionalHeader.CheckSum = 0; 243 nt_header->OptionalHeader.CheckSum = 0;
255 // nt_header->FileHeader.TimeDateStamp = time (NULL); 244 #if 0
256 // dos_header->e_cp = size / 512; 245 nt_header->FileHeader.TimeDateStamp = time (NULL);
257 // nt_header->OptionalHeader.SizeOfImage = size; 246 dos_header->e_cp = size / 512;
258 247 nt_header->OptionalHeader.SizeOfImage = size;
259 pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile"); 248 #endif
249
250 pfnCheckSumMappedFile =
251 (pfnCheckSumMappedFile_t) GetProcAddress (hImagehelp,
252 "CheckSumMappedFile");
260 if (pfnCheckSumMappedFile) 253 if (pfnCheckSumMappedFile)
261 { 254 {
262 // nt_header->FileHeader.TimeDateStamp = time (NULL); 255 #if 0
256 nt_header->FileHeader.TimeDateStamp = time (NULL);
257 #endif
263 pfnCheckSumMappedFile (out_file.file_base, 258 pfnCheckSumMappedFile (out_file.file_base,
264 out_file.size, 259 out_file.size,
265 &headersum, 260 &headersum,
266 &checksum); 261 &checksum);
267 nt_header->OptionalHeader.CheckSum = checksum; 262 nt_header->OptionalHeader.CheckSum = checksum;
269 FreeLibrary (hImagehelp); 264 FreeLibrary (hImagehelp);
270 } 265 }
271 266
272 close_file_data (&in_file); 267 close_file_data (&in_file);
273 close_file_data (&out_file); 268 close_file_data (&out_file);
274 } 269
275 270 return 0;
276
277 /* File handling. */
278
279
280 int
281 open_output_file (file_data *p_file, CONST char *filename, unsigned long size)
282 {
283 HANDLE file;
284 HANDLE file_mapping;
285 void *file_base;
286
287 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
288 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
289 if (file == INVALID_HANDLE_VALUE)
290 return FALSE;
291
292 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
293 0, size, NULL);
294 if (!file_mapping)
295 return FALSE;
296
297 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
298 if (file_base == 0)
299 return FALSE;
300
301 p_file->name = filename;
302 p_file->size = size;
303 p_file->file = file;
304 p_file->file_mapping = file_mapping;
305 p_file->file_base = file_base;
306
307 return TRUE;
308 } 271 }
309 272
310 /* Routines to manipulate NT executable file sections. */ 273 /* Routines to manipulate NT executable file sections. */
311 274
312 #ifndef DUMP_SEPARATE_SECTION 275 #ifndef DUMP_SEPARATE_SECTION
484 447
485 size = data_size; 448 size = data_size;
486 DUMP_MSG (("Dumping data section...\n")); 449 DUMP_MSG (("Dumping data section...\n"));
487 DUMP_MSG (("\t0x%08x Address in process.\n", data_va)); 450 DUMP_MSG (("\t0x%08x Address in process.\n", data_va));
488 DUMP_MSG (("\t0x%08x Offset in output file.\n", 451 DUMP_MSG (("\t0x%08x Offset in output file.\n",
489 data_file - p_outfile->file_base)); 452 (char*)data_file - p_outfile->file_base));
490 DUMP_MSG (("\t0x%08x Size in bytes.\n", size)); 453 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
491 memcpy (data_file, data_va, size); 454 memcpy (data_file, data_va, size);
492 455
493 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base; 456 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
494 size = p_infile->size - index; 457 size = p_infile->size - index;