comparison src/unexnt.c @ 412:697ef44129c6 r21-2-14

Import from CVS: tag r21-2-14
author cvs
date Mon, 13 Aug 2007 11:20:41 +0200
parents de805c49cfc1
children 11054d720c21
comparison
equal deleted inserted replaced
411:12e008d41344 412:697ef44129c6
59 #include <stdlib.h> /* _fmode */ 59 #include <stdlib.h> /* _fmode */
60 #include <stdio.h> 60 #include <stdio.h>
61 #include <fcntl.h> 61 #include <fcntl.h>
62 #include <windows.h> 62 #include <windows.h>
63 63
64 #include "nt.h"
65 #include "ntheap.h"
66
67 /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */ 64 /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */
68 /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */ 65 /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */
69 typedef PIMAGE_NT_HEADERS 66 PIMAGE_NT_HEADERS
70 (__stdcall * pfnCheckSumMappedFile_t) (LPVOID BaseAddress, DWORD FileLength, 67 (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress, DWORD FileLength,
71 LPDWORD HeaderSum, LPDWORD CheckSum); 68 LPDWORD HeaderSum, LPDWORD CheckSum);
72
73 69
74 #if 0 70 #if 0
75 extern BOOL ctrl_c_handler (unsigned long type); 71 extern BOOL ctrl_c_handler (unsigned long type);
76 #endif 72 #endif
77 73
78 /* Sync with FSF Emacs 19.34.6 74 #include "ntheap.h"
79 note: struct file_data is now defined in nt.h */ 75
76 /* Sync with FSF Emacs 19.34.6 note: struct file_data is now defined in ntheap.h */
80 77
81 enum { 78 enum {
82 HEAP_UNINITIALIZED = 1, 79 HEAP_UNINITIALIZED = 1,
83 HEAP_UNLOADED, 80 HEAP_UNLOADED,
84 HEAP_LOADED 81 HEAP_LOADED
101 98
102 /* Cached info about the .bss section in the executable. */ 99 /* Cached info about the .bss section in the executable. */
103 PUCHAR bss_start = UNINIT_PTR; 100 PUCHAR bss_start = UNINIT_PTR;
104 DWORD bss_size = UNINIT_LONG; 101 DWORD bss_size = UNINIT_LONG;
105 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
106 /* Startup code for running on NT. When we are running as the dumped 110 /* Startup code for running on NT. When we are running as the dumped
107 version, we need to bootstrap our heap and .bss section into our 111 version, we need to bootstrap our heap and .bss section into our
108 address space before we can actually hand off control to the startup 112 address space before we can actually hand off control to the startup
109 code supplied by NT (primarily because that code relies upon malloc ()). */ 113 code supplied by NT (primarily because that code relies upon malloc ()). */
110
111 /* **********************
112 Hackers please remember, this _start() thingy is *not* called neither
113 when dumping portably, nor when running from temacs! Do not put
114 significant XEmacs initialization here!
115 ********************** */
116
117 void 114 void
118 _start (void) 115 _start (void)
119 { 116 {
117 char * p;
120 extern void mainCRTStartup (void); 118 extern void mainCRTStartup (void);
121 119
122 /* Cache system info, e.g., the NT page size. */ 120 /* Cache system info, e.g., the NT page size. */
123 cache_system_info (); 121 cache_system_info ();
124 122
134 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0) 132 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0)
135 { 133 {
136 exit (1); 134 exit (1);
137 } 135 }
138 136
139 /* #### This is super-bogus. When I rename xemacs.exe, 137 /* To allow profiling, make sure executable_path names the .exe
140 the renamed file still loads its heap from xemacs.exe --kkm */ 138 file, not the file created by the profiler */
141 #if 0 139 p = strrchr (executable_path, '\\');
142 { 140 strcpy (p+1, PATH_PROGNAME ".exe");
143 /* To allow profiling, make sure executable_path names the .exe
144 file, not the file created by the profiler */
145 char *p = strrchr (executable_path, '\\');
146 strcpy (p+1, PATH_PROGNAME ".exe");
147 }
148 #endif
149 141
150 recreate_heap (executable_path); 142 recreate_heap (executable_path);
151 heap_state = HEAP_LOADED; 143 heap_state = HEAP_LOADED;
152 } 144 }
153 145
154 /* #### This is bogus, too. _fmode is set to different values
155 when we run `xemacs' and `temacs run-emacs'. The sooner we
156 hit and fix all the weirdities this causes us, the better --kkm */
157 #if 0
158 /* The default behavior is to treat files as binary and patch up 146 /* The default behavior is to treat files as binary and patch up
159 text files appropriately. */ 147 text files appropriately, in accordance with the MSDOS code. */
160 _fmode = O_BINARY; 148 _fmode = O_BINARY;
161 #endif
162 149
163 #if 0 150 #if 0
164 /* This prevents ctrl-c's in shells running while we're suspended from 151 /* This prevents ctrl-c's in shells running while we're suspended from
165 having us exit. */ 152 having us exit. */
166 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE); 153 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
167 #endif 154 #endif
168 155
156 /* Invoke the NT CRT startup routine now that our housecleaning
157 is finished. */
158 #ifdef HAVE_NTGUI
159 /* determine WinMain args like crt0.c does */
160 hinst = GetModuleHandle(NULL);
161 lpCmdLine = GetCommandLine();
162 nCmdShow = SW_SHOWDEFAULT;
163 #endif
169 mainCRTStartup (); 164 mainCRTStartup ();
170 } 165 }
171 166
172 /* Dump out .data and .bss sections into a new executable. */ 167 /* Dump out .data and .bss sections into a new executable. */
173 void 168 void
176 { 171 {
177 file_data in_file, out_file; 172 file_data in_file, out_file;
178 char out_filename[MAX_PATH], in_filename[MAX_PATH]; 173 char out_filename[MAX_PATH], in_filename[MAX_PATH];
179 unsigned long size; 174 unsigned long size;
180 char *ptr; 175 char *ptr;
181 HINSTANCE hImagehelp; 176 HANDLE hImagehelp;
182 177
183 /* Make sure that the input and output filenames have the 178 /* Make sure that the input and output filenames have the
184 ".exe" extension...patch them up if they don't. */ 179 ".exe" extension...patch them up if they don't. */
185 strcpy (in_filename, old_name); 180 strcpy (in_filename, old_name);
186 ptr = in_filename + strlen (in_filename) - 4; 181 ptr = in_filename + strlen (in_filename) - 4;
231 hImagehelp = LoadLibrary ("imagehlp.dll"); 226 hImagehelp = LoadLibrary ("imagehlp.dll");
232 if (hImagehelp) 227 if (hImagehelp)
233 { 228 {
234 PIMAGE_DOS_HEADER dos_header; 229 PIMAGE_DOS_HEADER dos_header;
235 PIMAGE_NT_HEADERS nt_header; 230 PIMAGE_NT_HEADERS nt_header;
236
237 DWORD headersum; 231 DWORD headersum;
238 DWORD checksum; 232 DWORD checksum;
239 pfnCheckSumMappedFile_t pfnCheckSumMappedFile;
240 233
241 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base; 234 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
242 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew); 235 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
243 236
244 nt_header->OptionalHeader.CheckSum = 0; 237 nt_header->OptionalHeader.CheckSum = 0;
245 #if 0 238 // nt_header->FileHeader.TimeDateStamp = time (NULL);
246 nt_header->FileHeader.TimeDateStamp = time (NULL); 239 // dos_header->e_cp = size / 512;
247 dos_header->e_cp = size / 512; 240 // nt_header->OptionalHeader.SizeOfImage = size;
248 nt_header->OptionalHeader.SizeOfImage = size; 241
249 #endif 242 pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile");
250
251 pfnCheckSumMappedFile =
252 (pfnCheckSumMappedFile_t) GetProcAddress (hImagehelp,
253 "CheckSumMappedFile");
254 if (pfnCheckSumMappedFile) 243 if (pfnCheckSumMappedFile)
255 { 244 {
256 #if 0 245 // nt_header->FileHeader.TimeDateStamp = time (NULL);
257 nt_header->FileHeader.TimeDateStamp = time (NULL);
258 #endif
259 pfnCheckSumMappedFile (out_file.file_base, 246 pfnCheckSumMappedFile (out_file.file_base,
260 out_file.size, 247 out_file.size,
261 &headersum, 248 &headersum,
262 &checksum); 249 &checksum);
263 nt_header->OptionalHeader.CheckSum = checksum; 250 nt_header->OptionalHeader.CheckSum = checksum;
265 FreeLibrary (hImagehelp); 252 FreeLibrary (hImagehelp);
266 } 253 }
267 254
268 close_file_data (&in_file); 255 close_file_data (&in_file);
269 close_file_data (&out_file); 256 close_file_data (&out_file);
257 }
258
259
260 /* File handling. */
261
262
263 int
264 open_output_file (file_data *p_file, char *filename, unsigned long size)
265 {
266 HANDLE file;
267 HANDLE file_mapping;
268 void *file_base;
269
270 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
271 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
272 if (file == INVALID_HANDLE_VALUE)
273 return FALSE;
274
275 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
276 0, size, NULL);
277 if (!file_mapping)
278 return FALSE;
279
280 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
281 if (file_base == 0)
282 return FALSE;
283
284 p_file->name = filename;
285 p_file->size = size;
286 p_file->file = file;
287 p_file->file_mapping = file_mapping;
288 p_file->file_base = file_base;
289
290 return TRUE;
270 } 291 }
271 292
272 /* Routines to manipulate NT executable file sections. */ 293 /* Routines to manipulate NT executable file sections. */
273 294
274 #ifndef DUMP_SEPARATE_SECTION 295 #ifndef DUMP_SEPARATE_SECTION
446 467
447 size = data_size; 468 size = data_size;
448 DUMP_MSG (("Dumping data section...\n")); 469 DUMP_MSG (("Dumping data section...\n"));
449 DUMP_MSG (("\t0x%08x Address in process.\n", data_va)); 470 DUMP_MSG (("\t0x%08x Address in process.\n", data_va));
450 DUMP_MSG (("\t0x%08x Offset in output file.\n", 471 DUMP_MSG (("\t0x%08x Offset in output file.\n",
451 (char*)data_file - p_outfile->file_base)); 472 data_file - p_outfile->file_base));
452 DUMP_MSG (("\t0x%08x Size in bytes.\n", size)); 473 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
453 memcpy (data_file, data_va, size); 474 memcpy (data_file, data_va, size);
454 475
455 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base; 476 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
456 size = p_infile->size - index; 477 size = p_infile->size - index;