*** empty log message ***
[bpt/emacs.git] / src / unexw32.c
index 2aa2a52..b183864 100644 (file)
@@ -23,7 +23,6 @@ Boston, MA 02111-1307, USA.
 
 #include <config.h>
 
-#include <stdlib.h>    /* _fmode */
 #include <stdio.h>
 #include <fcntl.h>
 #include <time.h>
@@ -67,17 +66,17 @@ void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
 
 /* Cached info about the .data section in the executable.  */
 PIMAGE_SECTION_HEADER data_section;
-PUCHAR data_start = 0;
+PCHAR  data_start = 0;
 DWORD  data_size = 0;
 
 /* Cached info about the .bss section in the executable.  */
 PIMAGE_SECTION_HEADER bss_section;
-PUCHAR bss_start = 0;
+PCHAR  bss_start = 0;
 DWORD  bss_size = 0;
 DWORD  extra_bss_size = 0;
 /* bss data that is static might be discontiguous from non-static.  */
 PIMAGE_SECTION_HEADER bss_section_static;
-PUCHAR bss_start_static = 0;
+PCHAR  bss_start_static = 0;
 DWORD  bss_size_static = 0;
 DWORD  extra_bss_size_static = 0;
 
@@ -112,10 +111,6 @@ _start (void)
   /* Grab our malloc arena space now, before CRT starts up. */
   init_heap ();
 
-  /* The default behavior is to treat files as binary and patch up
-     text files appropriately, in accordance with the MSDOS code.  */
-  _fmode = O_BINARY;
-
   /* This prevents ctrl-c's in shells running while we're suspended from
      having us exit.  */
   SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
@@ -148,17 +143,17 @@ open_input_file (file_data *p_file, char *filename)
 
   file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
-  if (file == INVALID_HANDLE_VALUE) 
+  if (file == INVALID_HANDLE_VALUE)
     return FALSE;
 
   size = GetFileSize (file, &upper_size);
-  file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY, 
+  file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
                                    0, size, NULL);
-  if (!file_mapping) 
+  if (!file_mapping)
     return FALSE;
 
   file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
-  if (file_base == 0) 
+  if (file_base == 0)
     return FALSE;
 
   p_file->name = filename;
@@ -179,18 +174,18 @@ open_output_file (file_data *p_file, char *filename, unsigned long size)
 
   file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
-  if (file == INVALID_HANDLE_VALUE) 
+  if (file == INVALID_HANDLE_VALUE)
     return FALSE;
 
-  file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE, 
+  file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
                                    0, size, NULL);
-  if (!file_mapping) 
+  if (!file_mapping)
     return FALSE;
-  
+
   file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
-  if (file_base == 0) 
+  if (file_base == 0)
     return FALSE;
-  
+
   p_file->name = filename;
   p_file->size = size;
   p_file->file = file;
@@ -284,7 +279,7 @@ offset_to_section (DWORD offset, IMAGE_NT_HEADERS * nt_header)
 /* Return offset to an object in dst, given offset in src.  We assume
    there is at least one section in both src and dst images, and that
    the some sections may have been added to dst (after sections in src).  */
-static DWORD
+DWORD
 relocate_offset (DWORD offset,
                 IMAGE_NT_HEADERS * src_nt_header,
                 IMAGE_NT_HEADERS * dst_nt_header)
@@ -331,38 +326,38 @@ relocate_offset (DWORD offset,
 #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL))
 
 #define PTR_TO_OFFSET(ptr, pfile_data) \
-          ((char *)(ptr) - (pfile_data)->file_base)
+          ((unsigned char *)(ptr) - (pfile_data)->file_base)
 
 #define OFFSET_TO_PTR(offset, pfile_data) \
           ((pfile_data)->file_base + (DWORD)(offset))
 
 
 /* Flip through the executable and cache the info necessary for dumping.  */
-static void
+void
 get_section_info (file_data *p_infile)
 {
   PIMAGE_DOS_HEADER dos_header;
   PIMAGE_NT_HEADERS nt_header;
   PIMAGE_SECTION_HEADER section;
   int overlap;
-  
+
   dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
-  if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) 
+  if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
     {
       printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
       exit (1);
     }
-  nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + 
+  nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
                                   dos_header->e_lfanew);
-  if (nt_header == NULL) 
+  if (nt_header == NULL)
     {
-      printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n", 
+      printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
             p_infile->name);
       exit (1);
     }
 
   /* Check the NT header signature ...  */
-  if (nt_header->Signature != IMAGE_NT_SIGNATURE) 
+  if (nt_header->Signature != IMAGE_NT_SIGNATURE)
     {
       printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
              nt_header->Signature, p_infile->name);
@@ -447,7 +442,11 @@ get_section_info (file_data *p_infile)
     - bss_section_static->SizeOfRawData;
 
   /* Combine the bss sections into one if they overlap.  */
+#ifdef _ALPHA_
+  overlap = 1;                 /* force all bss data to be dumped */
+#else
   overlap = 0;
+#endif
   if (bss_start < bss_start_static)
     {
       if (bss_start_static < bss_start + bss_size)
@@ -477,8 +476,8 @@ get_section_info (file_data *p_infile)
 
 /* The dump routines.  */
 
-static void
-copy_executable_and_dump_data (file_data *p_infile, 
+void
+copy_executable_and_dump_data (file_data *p_infile,
                               file_data *p_outfile)
 {
   unsigned char *dst, *dst_save;
@@ -533,10 +532,10 @@ copy_executable_and_dump_data (file_data *p_infile,
      Note that dst is updated implicitly by each COPY_CHUNK.  */
 
   dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
-  nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + 
+  nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
                                   dos_header->e_lfanew);
   section = IMAGE_FIRST_SECTION (nt_header);
+
   dst = (unsigned char *) p_outfile->file_base;
 
   COPY_CHUNK ("Copying DOS header...", dos_header,
@@ -613,7 +612,7 @@ copy_executable_and_dump_data (file_data *p_infile,
        }
       if (section == heap_section)
        {
-         DWORD heap_start = get_heap_start ();
+         DWORD heap_start = (DWORD) get_heap_start ();
          DWORD heap_size = get_committed_heap_size ();
 
          /* Dump the used portion of the predump heap, adjusting the
@@ -715,18 +714,28 @@ unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
   file_data in_file, out_file;
   char out_filename[MAX_PATH], in_filename[MAX_PATH];
   unsigned long size;
-  char *ptr;
-  
-  /* Make sure that the input and output filenames have the
-     ".exe" extension...patch them up if they don't.  */
-  strcpy (in_filename, old_name);
-  ptr = in_filename + strlen (in_filename) - 4;
-  if (strcmp (ptr, ".exe"))
-    strcat (in_filename, ".exe");
-
-  strcpy (out_filename, new_name);
-  ptr = out_filename + strlen (out_filename) - 4;
-  if (strcmp (ptr, ".exe"))
+  char *p;
+  char *q;
+
+  /* Ignore old_name, and get our actual location from the OS.  */
+  if (!GetModuleFileName (NULL, in_filename, MAX_PATH))
+    abort ();
+  dostounix_filename (in_filename);
+  strcpy (out_filename, in_filename);
+
+  /* Change the base of the output filename to match the requested name.  */
+  if ((p = strrchr (out_filename, '/')) == NULL)
+    abort ();
+  /* The filenames have already been expanded, and will be in Unix
+     format, so it is safe to expect an absolute name.  */
+  if ((q = strrchr (new_name, '/')) == NULL)
+    abort ();
+  strcpy (p, q);
+
+  /* Make sure that the output filename has the ".exe" extension...patch
+     it up if not.  */
+  p = out_filename + strlen (out_filename) - 4;
+  if (strcmp (p, ".exe"))
     strcat (out_filename, ".exe");
 
   printf ("Dumping from %s\n", in_filename);
@@ -738,7 +747,7 @@ unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
   /* Open the undumped executable file.  */
   if (!open_input_file (&in_file, in_filename))
     {
-      printf ("Failed to open %s (%d)...bailing.\n", 
+      printf ("Failed to open %s (%d)...bailing.\n",
              in_filename, GetLastError ());
       exit (1);
     }
@@ -754,7 +763,7 @@ unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
     extra_bss_size_static;
   if (!open_output_file (&out_file, out_filename, size))
     {
-      printf ("Failed to open %s (%d)...bailing.\n", 
+      printf ("Failed to open %s (%d)...bailing.\n",
              out_filename, GetLastError ());
       exit (1);
     }
@@ -798,3 +807,6 @@ unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
 }
 
 /* eof */
+
+/* arch-tag: fe1d3d1c-ef88-4917-ab22-f12ab16b3254
+   (do not change this comment) */