/* ** Fix the dumped emacs executable: ** ** - copy .data section data of interest from running executable into ** output .exe file ** ** - convert .bss section into an initialized data section (like ** .data) and copy .bss section data of interest from running ** executable into output .exe file */ static void fixup_executable (int fd) { exe_header_t exe_header_buffer; exe_header_t *exe_header; int i; int ret; int found_data = 0; int found_bss = 0; exe_header = read_exe_header (fd, &exe_header_buffer); assert (exe_header != 0); assert (exe_header->file_header.f_nscns > 0); for (i = 0; i < exe_header->file_header.f_nscns; ++i) { unsigned long start_address = exe_header->section_header[i].s_vaddr + exe_header->file_optional_header.ImageBase; unsigned long end_address = exe_header->section_header[i].s_vaddr + exe_header->file_optional_header.ImageBase + exe_header->section_header[i].s_paddr; if (debug_unexcw) printf ("%8s start 0x%08x end 0x%08x\n", exe_header->section_header[i].s_name, start_address, end_address); if (my_edata >= (char *) start_address && my_edata < (char *) end_address) { /* data section */ ret = lseek (fd, (long) (exe_header->section_header[i].s_scnptr), SEEK_SET); assert (ret != -1); ret = write (fd, (char *) start_address, my_edata - (char *) start_address); assert (ret == my_edata - (char *) start_address); ++found_data; if (debug_unexcw) printf (" .data, mem start 0x%08x mem length %d\n", start_address, my_edata - (char *) start_address); if (debug_unexcw) printf (" .data, file start %d file length %d\n", (int) exe_header->section_header[i].s_scnptr, (int) exe_header->section_header[i].s_paddr); } else if (my_endbss >= (char *) start_address && my_endbss < (char *) end_address) { /* bss section */ ++found_bss; if (exe_header->section_header[i].s_flags & 0x00000080) { /* convert uninitialized data section to initialized data section */ struct stat statbuf; ret = fstat (fd, &statbuf); assert (ret != -1); exe_header->section_header[i].s_flags &= ~0x00000080; exe_header->section_header[i].s_flags |= 0x00000040; exe_header->section_header[i].s_scnptr = (statbuf.st_size + exe_header->file_optional_header.FileAlignment) / exe_header->file_optional_header.FileAlignment * exe_header->file_optional_header.FileAlignment; exe_header->section_header[i].s_size = (exe_header->section_header[i].s_paddr + exe_header->file_optional_header.FileAlignment) / exe_header->file_optional_header.FileAlignment * exe_header->file_optional_header.FileAlignment; ret = lseek (fd, (long) (exe_header->section_header[i].s_scnptr + exe_header->section_header[i].s_size - 1), SEEK_SET); assert (ret != -1); ret = write (fd, "", 1); assert (ret == 1); ret = lseek (fd, (long) ((char *) &exe_header->section_header[i] - (char *) exe_header), SEEK_SET); assert (ret != -1); ret = write (fd, &exe_header->section_header[i], sizeof (exe_header->section_header[i])); assert (ret == sizeof (exe_header->section_header[i])); if (debug_unexcw) printf (" seek to %ld, write %d\n", (long) ((char *) &exe_header->section_header[i] - (char *) exe_header), sizeof (exe_header->section_header[i])); } /* write initialized data section */ ret = lseek (fd, (long) (exe_header->section_header[i].s_scnptr), SEEK_SET); assert (ret != -1); /* force the dumped emacs to reinitialize malloc */ __malloc_initialized = 0; ret = write (fd, (char *) start_address, my_endbss - (char *) start_address); __malloc_initialized = 1; assert (ret == (my_endbss - (char *) start_address)); if (debug_unexcw) printf (" .bss, mem start 0x%08x mem length %d\n", start_address, my_endbss - (char *) start_address); if (debug_unexcw) printf (" .bss, file start %d file length %d\n", (int) exe_header->section_header[i].s_scnptr, (int) exe_header->section_header[i].s_paddr); } } assert (found_bss == 1); assert (found_data == 1); }
/* ** Fix the dumped emacs executable: ** ** - copy .data section data of interest from running executable into ** output .exe file ** ** - convert .bss section into an initialized data section (like ** .data) and copy .bss section data of interest from running ** executable into output .exe file */ static void fixup_executable (int fd) { exe_header_t exe_header_buffer; exe_header_t *exe_header; int i; int ret; int found_data = 0; int found_bss = 0; exe_header = read_exe_header (fd, &exe_header_buffer); assert (exe_header != 0); assert (exe_header->file_header.f_nscns > 0); for (i = 0; i < exe_header->file_header.f_nscns; ++i) { unsigned long start_address = exe_header->section_header[i].s_vaddr + exe_header->file_optional_header.ImageBase; unsigned long end_address = exe_header->section_header[i].s_vaddr + exe_header->file_optional_header.ImageBase + exe_header->section_header[i].s_paddr; if (debug_unexcw) printf ("%8s start %#lx end %#lx\n", exe_header->section_header[i].s_name, start_address, end_address); if (my_edata >= (char *) start_address && my_edata < (char *) end_address) { /* data section */ ret = lseek (fd, (long) (exe_header->section_header[i].s_scnptr), SEEK_SET); assert (ret != -1); ret = write (fd, (char *) start_address, my_edata - (char *) start_address); assert (ret == my_edata - (char *) start_address); ++found_data; if (debug_unexcw) printf (" .data, mem start %#lx mem length %d\n", start_address, my_edata - (char *) start_address); if (debug_unexcw) printf (" .data, file start %d file length %d\n", (int) exe_header->section_header[i].s_scnptr, (int) exe_header->section_header[i].s_paddr); } else if (my_endbss >= (char *) start_address && my_endbss < (char *) end_address) { /* bss section */ ++found_bss; if (exe_header->section_header[i].s_flags & 0x00000080) { /* convert uninitialized data section to initialized data section */ struct stat statbuf; ret = fstat (fd, &statbuf); assert (ret != -1); exe_header->section_header[i].s_flags &= ~0x00000080; exe_header->section_header[i].s_flags |= 0x00000040; exe_header->section_header[i].s_scnptr = (statbuf.st_size + exe_header->file_optional_header.FileAlignment) / exe_header->file_optional_header.FileAlignment * exe_header->file_optional_header.FileAlignment; exe_header->section_header[i].s_size = (exe_header->section_header[i].s_paddr + exe_header->file_optional_header.FileAlignment) / exe_header->file_optional_header.FileAlignment * exe_header->file_optional_header.FileAlignment; /* Make sure the generated bootstrap binary isn't * sparse. NT doesn't use a file cache for sparse * executables, so if we bootstrap Emacs using a sparse * bootstrap-emacs.exe, bootstrap takes about twenty * times longer than it would otherwise. */ ret = posix_fallocate (fd, ( exe_header->section_header[i].s_scnptr + exe_header->section_header[i].s_size ), 1); assert (ret != -1); ret = lseek (fd, (long) (exe_header->section_header[i].s_scnptr + exe_header->section_header[i].s_size - 1), SEEK_SET); assert (ret != -1); ret = write (fd, "", 1); assert (ret == 1); ret = lseek (fd, (long) ((char *) &exe_header->section_header[i] - (char *) exe_header), SEEK_SET); assert (ret != -1); ret = write (fd, &exe_header->section_header[i], sizeof (exe_header->section_header[i])); assert (ret == sizeof (exe_header->section_header[i])); if (debug_unexcw) printf (" seek to %ld, write %d\n", (long) ((char *) &exe_header->section_header[i] - (char *) exe_header), sizeof (exe_header->section_header[i])); } /* write initialized data section */ ret = lseek (fd, (long) (exe_header->section_header[i].s_scnptr), SEEK_SET); assert (ret != -1); /* force the dumped emacs to reinitialize malloc */ __malloc_initialized = 0; ret = write (fd, (char *) start_address, my_endbss - (char *) start_address); __malloc_initialized = 1; assert (ret == (my_endbss - (char *) start_address)); if (debug_unexcw) printf (" .bss, mem start %#lx mem length %d\n", start_address, my_endbss - (char *) start_address); if (debug_unexcw) printf (" .bss, file start %d file length %d\n", (int) exe_header->section_header[i].s_scnptr, (int) exe_header->section_header[i].s_paddr); } } assert (found_bss == 1); assert (found_data == 1); }