This commit was generated by cvs2svn to compensate for changes in r1705,
[chise/xemacs-chise.git.1] / src / unexnt.c
index ac4b78e..3a6b0b1 100644 (file)
@@ -56,24 +56,26 @@ Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  * version of my_ebss in lastfile.c and a new firstfile.c file.  jhar */
 
 #include <config.h>
-#include <stdlib.h>    /* _fmode */
-#include <stdio.h>
-#include <fcntl.h>
-#include <windows.h>
+#include "lisp.h"
+
+#include "syswindows.h"
+
+#include "nt.h"
+#include "ntheap.h"
 
 /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */
 /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */
-PIMAGE_NT_HEADERS
-(__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress, DWORD FileLength,
-                                    LPDWORD HeaderSum, LPDWORD CheckSum);
+typedef PIMAGE_NT_HEADERS
+(__stdcall * pfnCheckSumMappedFile_t) (LPVOID BaseAddress, DWORD FileLength,
+                                      LPDWORD HeaderSum, LPDWORD CheckSum);
+
 
 #if 0
 extern BOOL ctrl_c_handler (unsigned long type);
 #endif
 
-#include "ntheap.h"
-
-/* Sync with FSF Emacs 19.34.6 note: struct file_data is now defined in ntheap.h */
+/* Sync with FSF Emacs 19.34.6
+   note: struct file_data is now defined in nt.h */
 
 enum {
   HEAP_UNINITIALIZED = 1,
@@ -100,21 +102,20 @@ DWORD  data_size = UNINIT_LONG;
 PUCHAR bss_start = UNINIT_PTR;
 DWORD  bss_size = UNINIT_LONG;
 
-#ifdef HAVE_NTGUI
-HINSTANCE hinst = NULL;
-HINSTANCE hprevinst = NULL;
-LPSTR lpCmdLine = "";
-int nCmdShow = 0;
-#endif /* HAVE_NTGUI */
-
 /* Startup code for running on NT.  When we are running as the dumped
    version, we need to bootstrap our heap and .bss section into our
    address space before we can actually hand off control to the startup
    code supplied by NT (primarily because that code relies upon malloc ()).  */
+
+/* **********************
+   Hackers please remember, this _start() thingy is *not* called neither
+   when dumping portably, nor when running from temacs! Do not put
+   significant XEmacs initialization here!
+   ********************** */
+
 void
 _start (void)
 {
-  char * p;
   extern void mainCRTStartup (void);
 
   /* Cache system info, e.g., the NT page size.  */
@@ -134,18 +135,29 @@ _start (void)
          exit (1);
        }
 
-      /* To allow profiling, make sure executable_path names the .exe
-        file, not the file created by the profiler */
-      p = strrchr (executable_path, '\\');
-      strcpy (p+1, PATH_PROGNAME ".exe");
+      /* #### This is super-bogus. When I rename xemacs.exe,
+        the renamed file still loads its heap from xemacs.exe --kkm */
+#if 0
+      {
+       /* To allow profiling, make sure executable_path names the .exe
+          file, not the file created by the profiler */
+       char *p = strrchr (executable_path, '\\');
+       strcpy (p+1, PATH_PROGNAME ".exe");
+      }
+#endif
 
       recreate_heap (executable_path);
       heap_state = HEAP_LOADED;
     }
 
+  /* #### This is bogus, too. _fmode is set to different values
+     when we run `xemacs' and `temacs run-emacs'. The sooner we
+     hit and fix all the weirdities this causes us, the better --kkm */
+#if 0
   /* The default behavior is to treat files as binary and patch up
-     text files appropriately, in accordance with the MSDOS code.  */
+     text files appropriately.  */
   _fmode = O_BINARY;
+#endif
 
 #if 0
   /* This prevents ctrl-c's in shells running while we're suspended from
@@ -153,27 +165,19 @@ _start (void)
   SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
 #endif
 
-  /* Invoke the NT CRT startup routine now that our housecleaning
-     is finished.  */
-#ifdef HAVE_NTGUI
-  /* determine WinMain args like crt0.c does */
-  hinst = GetModuleHandle(NULL);
-  lpCmdLine = GetCommandLine();
-  nCmdShow = SW_SHOWDEFAULT;
-#endif
   mainCRTStartup ();
 }
 
 /* Dump out .data and .bss sections into a new executable.  */
-void
-unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
-       void *entry_address)
+int
+unexec (char *new_name, char *old_name, unsigned int start_data,
+       unsigned int start_bss, unsigned int entry_address)
 {
   file_data in_file, out_file;
   char out_filename[MAX_PATH], in_filename[MAX_PATH];
   unsigned long size;
   char *ptr;
-  HANDLE hImagehelp;
+  HINSTANCE hImagehelp;
   
   /* Make sure that the input and output filenames have the
      ".exe" extension...patch them up if they don't.  */
@@ -228,21 +232,29 @@ unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
   {
     PIMAGE_DOS_HEADER dos_header;
     PIMAGE_NT_HEADERS nt_header;
+
     DWORD  headersum;
     DWORD  checksum;
+    pfnCheckSumMappedFile_t pfnCheckSumMappedFile;
 
     dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
     nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
 
     nt_header->OptionalHeader.CheckSum = 0;
-//    nt_header->FileHeader.TimeDateStamp = time (NULL);
-//    dos_header->e_cp = size / 512;
-//    nt_header->OptionalHeader.SizeOfImage = size;
+#if 0
+    nt_header->FileHeader.TimeDateStamp = time (NULL);
+    dos_header->e_cp = size / 512;
+    nt_header->OptionalHeader.SizeOfImage = size;
+#endif
 
-    pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile");
+    pfnCheckSumMappedFile =
+      (pfnCheckSumMappedFile_t) GetProcAddress (hImagehelp,
+                                               "CheckSumMappedFile");
     if (pfnCheckSumMappedFile)
       {
-//     nt_header->FileHeader.TimeDateStamp = time (NULL);
+#if 0
+       nt_header->FileHeader.TimeDateStamp = time (NULL);
+#endif
        pfnCheckSumMappedFile (out_file.file_base,
                               out_file.size,
                               &headersum,
@@ -254,84 +266,10 @@ unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
 
   close_file_data (&in_file);
   close_file_data (&out_file);
-}
-
-
-/* File handling.  */
-
-
-int
-open_input_file (file_data *p_file, char *filename)
-{
-  HANDLE file;
-  HANDLE file_mapping;
-  void  *file_base;
-  unsigned long size, upper_size;
-
-  file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
-                    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
-  if (file == INVALID_HANDLE_VALUE) 
-    return FALSE;
 
-  size = GetFileSize (file, &upper_size);
-  file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY, 
-                                   0, size, NULL);
-  if (!file_mapping) 
-    return FALSE;
-
-  file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
-  if (file_base == 0) 
-    return FALSE;
-
-  p_file->name = filename;
-  p_file->size = size;
-  p_file->file = file;
-  p_file->file_mapping = file_mapping;
-  p_file->file_base = file_base;
-
-  return TRUE;
-}
-
-int
-open_output_file (file_data *p_file, char *filename, unsigned long size)
-{
-  HANDLE file;
-  HANDLE file_mapping;
-  void  *file_base;
-
-  file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
-                    CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
-  if (file == INVALID_HANDLE_VALUE) 
-    return FALSE;
-
-  file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE, 
-                                   0, size, NULL);
-  if (!file_mapping) 
-    return FALSE;
-  
-  file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
-  if (file_base == 0) 
-    return FALSE;
-  
-  p_file->name = filename;
-  p_file->size = size;
-  p_file->file = file;
-  p_file->file_mapping = file_mapping;
-  p_file->file_base = file_base;
-
-  return TRUE;
+  return 0;
 }
 
-/* Close the system structures associated with the given file.  */
-void
-close_file_data (file_data *p_file)
-{
-    UnmapViewOfFile (p_file->file_base);
-    CloseHandle (p_file->file_mapping);
-    CloseHandle (p_file->file);
-}
-
-
 /* Routines to manipulate NT executable file sections.  */
 
 #ifndef DUMP_SEPARATE_SECTION
@@ -375,27 +313,6 @@ get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start,
 }
 #endif
 
-/* Return pointer to section header for section containing the given
-   relative virtual address. */
-IMAGE_SECTION_HEADER *
-rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header)
-{
-  PIMAGE_SECTION_HEADER section;
-  int i;
-
-  section = IMAGE_FIRST_SECTION (nt_header);
-
-  for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
-    {
-      if (rva >= section->VirtualAddress
-         && rva < section->VirtualAddress + section->SizeOfRawData)
-       return section;
-      section++;
-    }
-  return NULL;
-}
-
-
 /* Flip through the executable and cache the info necessary for dumping.  */
 static void
 get_section_info (file_data *p_infile)
@@ -532,7 +449,7 @@ copy_executable_and_dump_data_section (file_data *p_infile,
   DUMP_MSG (("Dumping data section...\n"));
   DUMP_MSG (("\t0x%08x Address in process.\n", data_va));
   DUMP_MSG (("\t0x%08x Offset in output file.\n", 
-            data_file - p_outfile->file_base));
+            (char*)data_file - p_outfile->file_base));
   DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
   memcpy (data_file, data_va, size);
 
@@ -549,7 +466,7 @@ copy_executable_and_dump_data_section (file_data *p_infile,
 static void
 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
 {
-    unsigned char *heap_data, *bss_data;
+    unsigned char *heap_data;
     unsigned long size, index;
 
     DUMP_MSG (("Dumping heap onto end of executable...\n"));
@@ -565,16 +482,15 @@ dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
     memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
 
 #ifndef DUMP_SEPARATE_SECTION
-    printf ("Dumping bss onto end of executable...\n");
+    DUMP_MSG (("Dumping bss onto end of executable...\n"));
     
     index += size;
     size = bss_size;
-    bss_data = bss_start;
 
-    DUMP_MSG (("\t0x%08x BSS start in process.\n", bss_data));
+    DUMP_MSG (("\t0x%08x BSS start in process.\n", bss_start));
     DUMP_MSG (("\t0x%08x BSS offset in executable.\n", index));
     DUMP_MSG (("\t0x%08x BSS size in bytes.\n", size));
-    memcpy ((char *) p_outfile->file_base + index, bss_data, size);
+    memcpy ((char *) p_outfile->file_base + index, bss_start, size);
 #endif
 }