/* 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);
}
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++;
}
/* 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);
}
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,
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);
}