(get_section_info): Set the end of the data region
authorGeoff Voelker <voelker@cs.washington.edu>
Fri, 30 Jun 1995 21:15:19 +0000 (21:15 +0000)
committerGeoff Voelker <voelker@cs.washington.edu>
Fri, 30 Jun 1995 21:15:19 +0000 (21:15 +0000)
to be just before the start of the shared library data.
(read_in_bss): Read directly into memory.
(map_in_heap): Read directly into memory if unable to map.

src/unexw32.c

index a29393a..cf89f03 100644 (file)
@@ -142,8 +142,8 @@ unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
 
   /* The size of the dumped executable is the size of the original
      executable plus the size of the heap and the size of the .bss section.  */
-  heap_index_in_executable = round_to_next (in_file.size, 
-                                           get_allocation_unit ());
+  heap_index_in_executable = (unsigned long)
+    round_to_next ((unsigned char *) in_file.size, get_allocation_unit ());
   size = heap_index_in_executable + get_committed_heap_size () + bss_size;
   open_output_file (&out_file, out_filename, size);
 
@@ -315,12 +315,20 @@ get_section_info (file_data *p_infile)
        }
       if (!strcmp (section->Name, ".data")) 
        {
+         /* From lastfile.c  */
+         extern char my_edata[];
+
          /* The .data section.  */
          ptr  = (char *) nt_header->OptionalHeader.ImageBase +
            section->VirtualAddress;
          data_start_va = ptr;
          data_start_file = section->PointerToRawData;
-         data_size = get_section_size (section);
+
+         /* We want to only write Emacs data back to the executable,
+            not any of the library data (if library data is included,
+            then a dumped Emacs won't run on system versions other
+            than the one Emacs was dumped on).  */
+         data_size = my_edata - data_start_va;
        }
       section++;
     }
@@ -429,18 +437,12 @@ read_in_bss (char *filename)
   
   /* Ok, read in the saved .bss section and initialize all 
      uninitialized variables.  */
-  total_read = 0;
-  size = bss_size;
-  bss = bss_start;
-  while (ReadFile (file, buffer, 512, &n_read, NULL)) 
+  if (!ReadFile (file, bss_start, bss_size, &n_read, NULL))
     {
-      if (n_read == 0)
-       break;
-      memcpy (bss, buffer, n_read);
-      bss += n_read;
-      total_read += n_read;
+      i = GetLastError ();
+      exit (1);
     }
-    
+
   CloseHandle (file);
 }
 
@@ -451,7 +453,7 @@ map_in_heap (char *filename)
   HANDLE file;
   HANDLE file_mapping;
   void  *file_base;
-  unsigned long size, upper_size;
+  unsigned long size, upper_size, n_read;
   int    i;
 
   file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
@@ -467,17 +469,46 @@ map_in_heap (char *filename)
                                    0, size, NULL);
   if (!file_mapping) 
     {
-       i = GetLastError ();
-       exit (1);
+      i = GetLastError ();
+      exit (1);
     }
     
   size = get_committed_heap_size ();
   file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0, 
                               heap_index_in_executable, size,
                               get_heap_start ());
-  if (file_base == 0) 
+  if (file_base != 0) 
+    {
+      return;
+    }
+
+  /* If we don't succeed with the mapping, then copy from the 
+     data into the heap.  */
+
+  CloseHandle (file_mapping);
+
+  if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
+                   MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL)
+    {
+      i = GetLastError ();
+      exit (1);
+    }
+
+  /* Seek to the location of the heap data in the executable.  */
+  i = heap_index_in_executable;
+  if (SetFilePointer (file, i, NULL, FILE_BEGIN) == 0xFFFFFFFF)
     {
       i = GetLastError ();
       exit (1);
     }
+
+  /* Read in the data.  */
+  if (!ReadFile (file, get_heap_start (), 
+                get_committed_heap_size (), &n_read, NULL))
+    {
+      i = GetLastError ();
+      exit (1);
+    }
+
+  CloseHandle (file);
 }