Mercurial > hg > xemacs-beta
view src/unexnt.c @ 2367:ecf1ebac70d8
[xemacs-hg @ 2004-11-04 23:05:23 by ben]
commit mega-patch
configure.in: Turn off -Winline and -Wchar-subscripts.
Use the right set of cflags when compiling modules.
Rewrite ldap configuration to separate the inclusion of lber
(needed in recent Cygwin) from the basic checks for the
needed libraries.
add a function for MAKE_JUNK_C; initially code was added to
generate xemacs.def using this, but it will need to be rewritten.
add an rm -f for junk.c to avoid weird Cygwin bug with cp -f onto
an existing file.
Sort list of auto-detected functions and eliminate unused checks for
stpcpy, setlocale and getwd.
Add autodetection of Cygwin scanf problems
BETA: Rewrite section on configure to indicate what flags are important
and what not.
digest-doc.c, make-dump-id.c, profile.c, sorted-doc.c: Add proper decls for main().
make-msgfile.c: Document that this is old junk.
Move proposal to text.c.
make-msgfile.lex: Move proposal to text.c.
make-mswin-unicode.pl: Convert error-generating code so that the entire message will
be seen as a single unrecognized token.
mule/mule-ccl.el: Update docs.
lispref/mule.texi: Update CCL docs.
ldap/eldap.c: Mule-ize.
Use EXTERNAL_LIST_LOOP_2 instead of deleted EXTERNAL_LIST_LOOP.
* XEmacs 21.5.18 "chestnut" is released.
---------------------------------------------------------------
MULE-RELATED WORK:
---------------------------------------------------------------
---------------------------
byte-char conversion
---------------------------
buffer.c, buffer.h, insdel.c, text.c: Port FSF algorithm for byte-char conversion, replacing broken
previous version. Track the char position of the gap. Add
functions to do char-byte conversion downwards as well as upwards.
Move comments about algorithm workings to internals manual.
---------------------------
work on types
---------------------------
alloc.c, console-x-impl.h, dump-data.c, dump-data.h, dumper.c, dialog-msw.c, dired-msw.c, doc.c, editfns.c, esd.c, event-gtk.h, event-msw.c, events.c, file-coding.c, file-coding.h, fns.c, glyphs-eimage.c, glyphs-gtk.c, glyphs-msw.c, glyphs-shared.c, glyphs-x.c, glyphs.c, glyphs.h, gui.c, hpplay.c, imgproc.c, intl-win32.c, lrecord.h, lstream.c, keymap.c, lisp.h, libsst.c, linuxplay.c, miscplay.c, miscplay.h, mule-coding.c, nas.c, nt.c, ntheap.c, ntplay.c, objects-msw.c, objects-tty.c, objects-x.c, print.c, process-nt.c, process.c, redisplay.h, select-common.h, select-gtk.c, select-x.c, sgiplay.c, sound.c, sound.h, sunplay.c, sysfile.h, sysdep.c, syswindows.h, text.c, unexnt.c, win32.c, xgccache.c: Further work on types. This creates a full set of types for all
the basic semantics of `char' that I have so far identified, so that
its semantics can always be identified for the purposes of proper
Mule-safe code, and the raw use of `char' always avoided.
(1) More type renaming, for consistency of naming.
Char_ASCII -> Ascbyte
UChar_ASCII -> UAscbyte
Char_Binary -> CBinbyte
UChar_Binary -> Binbyte
SChar_Binary -> SBinbyte
(2) Introduce Rawbyte, CRawbyte, Boolbyte, Chbyte, UChbyte, and
Bitbyte and use them.
(3) New types Itext, Wexttext and Textcount for separating out
the concepts of bytes and textual units (different under UTF-16
and UTF-32, which are potential internal encodings).
(4) qxestr*_c -> qxestr*_ascii.
lisp.h: New; goes with other qxe() functions. #### Maybe goes in a
different section.
lisp.h: Group generic int-type defs together with EMACS_INT defs.
lisp.h: * lisp.h (WEXTTEXT_IS_WIDE)
New defns.
lisp.h: New type to replace places where int occurs as a boolean.
It's signed because occasionally people may want to use -1 as
an error value, and because unsigned ints are viral -- see comments
in the internals manual against using them.
dynarr.c: int -> Bytecount.
---------------------------
Mule-izing
---------------------------
device-x.c: Partially Mule-ize.
dumper.c, dumper.h: Mule-ize. Use Rawbyte. Use stderr_out not printf. Use wext_*().
sysdep.c, syswindows.h, text.c: New Wexttext API for manipulation of external text that may be
Unicode (e.g. startup code under Windows).
emacs.c: Mule-ize. Properly deal with argv in external encoding.
Use wext_*() and Wexttext. Use Rawbyte.
#if 0 some old junk on SCO that is unlikely to be correct.
Rewrite allocation code in run-temacs.
emacs.c, symsinit.h, win32.c: Rename win32 init function and call it even earlier, to
initialize mswindows_9x_p even earlier, for use in startup code
(XEUNICODE_P).
process.c: Use _wenviron not environ under Windows, to get Unicode environment
variables.
event-Xt.c: Mule-ize drag-n-drop related stuff.
dragdrop.c, dragdrop.h, frame-x.c: Mule-ize.
text.h: Add some more stand-in defines for particular kinds of conversion;
use in Mule-ization work in frame-x.c etc.
---------------------------
Freshening
---------------------------
intl-auto-encap-win32.c, intl-auto-encap-win32.h: Regenerate.
---------------------------
Unicode-work
---------------------------
intl-win32.c, syswindows.h: Factor out common options to MultiByteToWideChar and
WideCharToMultiByte. Add convert_unicode_to_multibyte_malloc()
and convert_unicode_to_multibyte_dynarr() and use. Add stuff for
alloca() conversion of multibyte/unicode.
alloc.c: Use dfc_external_data_len() in case of unicode coding system.
alloc.c, mule-charset.c: Don't zero out and reinit charset Unicode tables. This fucks up
dump-time loading. Anyway, either we load them at dump time or
run time, never both.
unicode.c: Dump the blank tables as well.
---------------------------------------------------------------
DOCUMENTATION, MOSTLY MULE-RELATED:
---------------------------------------------------------------
EmacsFrame.c, emodules.c, event-Xt.c, fileio.c, input-method-xlib.c, mule-wnnfns.c, redisplay-gtk.c, redisplay-tty.c, redisplay-x.c, regex.c, sysdep.c: Add comment about Mule work needed.
text.h: Add more documentation describing why DFC routines were not written
to return their value. Add some other DFC documentation.
console-msw.c, console-msw.h: Add pointer to docs in win32.c.
emacs.c: Add comments on sources of doc info.
text.c, charset.h, unicode.c, intl-win32.c, intl-encap-win32.c, text.h, file-coding.c, mule-coding.c: Collect background comments and related to text matters and
internationalization, and proposals for work to be done, in text.c
or Internals manual, stuff related to specific textual API's in
text.h, and stuff related to internal implementation of Unicode
conversion in unicode.c. Put lots of pointers to the comments to
make them easier to find.
s/mingw32.h, s/win32-common.h, s/win32-native.h, s/windowsnt.h, win32.c: Add bunches of new documentation on the different kinds of
builds and environments under Windows and how they work.
Collect this info in win32.c. Add pointers to these docs in
the relevant s/* files.
emacs.c: Document places with long comments.
Remove comment about exiting, move to internals manual, put
in pointer.
event-stream.c: Move docs about event queues and focus to internals manual, put
in pointer.
events.h: Move docs about event stream callbacks to internals manual, put
in pointer.
profile.c, redisplay.c, signal.c: Move documentation to the Internals manual.
process-nt.c: Add pointer to comment in win32-native.el.
lisp.h: Add comments about some comment conventions.
lisp.h: Add comment about the second argument.
device-msw.c, redisplay-msw.c: @@#### comments are out-of-date.
---------------------------------------------------------------
PDUMP WORK (MOTIVATED BY UNICODE CHANGES)
---------------------------------------------------------------
alloc.c, buffer.c, bytecode.c, console-impl.h, console.c, device.c, dumper.c, lrecord.h, elhash.c, emodules.h, events.c, extents.c, frame.c, glyphs.c, glyphs.h, mule-charset.c, mule-coding.c, objects.c, profile.c, rangetab.c, redisplay.c, specifier.c, specifier.h, window.c, lstream.c, file-coding.h, file-coding.c: PDUMP:
Properly implement dump_add_root_block(), which never worked before,
and is necessary for dumping Unicode tables.
Pdump name changes for accuracy:
XD_STRUCT_PTR -> XD_BLOCK_PTR.
XD_STRUCT_ARRAY -> XD_BLOCK_ARRAY.
XD_C_STRING -> XD_ASCII_STRING.
*_structure_* -> *_block_*.
lrecord.h: some comments added about
dump_add_root_block() vs dump_add_root_block_ptr().
extents.c: remove incorrect comment about pdump problems with gap array.
---------------------------------------------------------------
ALLOCATION
---------------------------------------------------------------
abbrev.c, alloc.c, bytecode.c, casefiddle.c, device-msw.c, device-x.c, dired-msw.c, doc.c, doprnt.c, dragdrop.c, editfns.c, emodules.c, file-coding.c, fileio.c, filelock.c, fns.c, glyphs-eimage.c, glyphs-gtk.c, glyphs-msw.c, glyphs-x.c, gui-msw.c, gui-x.c, imgproc.c, intl-win32.c, lread.c, menubar-gtk.c, menubar.c, nt.c, objects-msw.c, objects-x.c, print.c, process-nt.c, process-unix.c, process.c, realpath.c, redisplay.c, search.c, select-common.c, symbols.c, sysdep.c, syswindows.h, text.c, text.h, ui-byhand.c: New macros {alloca,xnew}_{itext,{i,ext,raw,bin,asc}bytes} for
more convenient allocation of these commonly requested items.
Modify functions to use alloca_ibytes, alloca_array, alloca_extbytes,
xnew_ibytes, etc. also XREALLOC_ARRAY, xnew.
alloc.c: Rewrite the allocation functions to factor out repeated code.
Add assertions for freeing dumped data.
lisp.h: Moved down and consolidated with other allocation stuff.
lisp.h, dynarr.c: New functions for allocation that's very efficient when mostly in
LIFO order.
lisp.h, text.c, text.h: Factor out some stuff for general use by alloca()-conversion funs.
text.h, lisp.h: Fill out convenience routines for allocating various kinds of
bytes and put them in lisp.h. Use them in place of xmalloc(),
ALLOCA().
text.h: Fill out the convenience functions so the _MALLOC() kinds match
the alloca() kinds.
---------------------------------------------------------------
ERROR-CHECKING
---------------------------------------------------------------
text.h: Create ASSERT_ASCTEXT_ASCII() and ASSERT_ASCTEXT_ASCII_LEN()
from similar Eistring checkers and change the Eistring checkers to
use them instead.
---------------------------------------------------------------
MACROS IN LISP.H
---------------------------------------------------------------
lisp.h: Redo GCPRO declarations. Create a "base" set of functions that can
be used to generate any kind of gcpro sets -- regular, ngcpro,
nngcpro, private ones used in GC_EXTERNAL_LIST_LOOP_2.
buffer.c, callint.c, chartab.c, console-msw.c, device-x.c, dialog-msw.c, dired.c, extents.c, ui-gtk.c, rangetab.c, nt.c, mule-coding.c, minibuf.c, menubar-msw.c, menubar.c, menubar-gtk.c, lread.c, lisp.h, gutter.c, glyphs.c, glyphs-widget.c, fns.c, fileio.c, file-coding.c, specifier.c: Eliminate EXTERNAL_LIST_LOOP, which does not check for circularities.
Use EXTERNAL_LIST_LOOP_2 instead or EXTERNAL_LIST_LOOP_3
or EXTERNAL_PROPERTY_LIST_LOOP_3 or GC_EXTERNAL_LIST_LOOP_2
(new macro). Removed/redid comments on EXTERNAL_LIST_LOOP.
---------------------------------------------------------------
SPACING FIXES
---------------------------------------------------------------
callint.c, hftctl.c, number-gmp.c, process-unix.c: Spacing fixes.
---------------------------------------------------------------
FIX FOR GEOMETRY PROBLEM IN FIRST FRAME
---------------------------------------------------------------
unicode.c: Add workaround for newlib bug in sscanf() [should be fixed by
release 1.5.12 of Cygwin].
toolbar.c: bug fix for problem of initial frame being 77 chars wide on Windows.
will be overridden by my other ws.
---------------------------------------------------------------
FIX FOR LEAKING PROCESS HANDLES:
---------------------------------------------------------------
process-nt.c: Fixes for leaking handles. Inspired by work done by Adrian Aichner
<adrian@xemacs.org>.
---------------------------------------------------------------
FIX FOR CYGWIN BUG (Unicode-related):
---------------------------------------------------------------
unicode.c: Add workaround for newlib bug in sscanf() [should be fixed by
release 1.5.12 of Cygwin].
---------------------------------------------------------------
WARNING FIXES:
---------------------------------------------------------------
console-stream.c: `reinit' is unused.
compiler.h, event-msw.c, frame-msw.c, intl-encap-win32.c, text.h: Add stuff to deal with ANSI-aliasing warnings I got.
regex.c: Gather includes together to avoid warning.
---------------------------------------------------------------
CHANGES TO INITIALIZATION ROUTINES:
---------------------------------------------------------------
buffer.c, emacs.c, console.c, debug.c, device-x.c, device.c, dragdrop.c, emodules.c, eval.c, event-Xt.c, event-gtk.c, event-msw.c, event-stream.c, event-tty.c, events.c, extents.c, faces.c, file-coding.c, fileio.c, font-lock.c, frame-msw.c, glyphs-widget.c, glyphs.c, gui-x.c, insdel.c, lread.c, lstream.c, menubar-gtk.c, menubar-x.c, minibuf.c, mule-wnnfns.c, objects-msw.c, objects.c, print.c, scrollbar-x.c, search.c, select-x.c, text.c, undo.c, unicode.c, window.c, symsinit.h: Call reinit_*() functions directly from emacs.c, for clarity.
Factor out some redundant init code. Move disallowed stuff
that had crept into vars_of_glyphs() into complex_vars_of_glyphs().
Call init_eval_semi_early() from eval.c not in the middle of
vars_of_() in emacs.c since there should be no order dependency
in the latter calls.
---------------------------------------------------------------
ARMAGEDDON:
---------------------------------------------------------------
alloc.c, emacs.c, lisp.h, print.c: Rename inhibit_non_essential_printing_operations to
inhibit_non_essential_conversion_operations.
text.c: Assert on !inhibit_non_essential_conversion_operations.
console-msw.c, print.c: Don't do conversion in SetConsoleTitle or FindWindow to avoid
problems during armageddon. Put #errors for NON_ASCII_INTERNAL_FORMAT
in places where problems would arise.
---------------------------------------------------------------
CHANGES TO THE BUILD PROCEDURE:
---------------------------------------------------------------
config.h.in, s/cxux.h, s/usg5-4-2.h, m/powerpc.h: Add comment about correct ordering of this file.
Rearrange everything to follow this -- put all #undefs together
and before the s&m files. Add undefs for HAVE_ALLOCA, C_ALLOCA,
BROKEN_ALLOCA_IN_FUNCTION_CALLS, STACK_DIRECTION. Remove unused
HAVE_STPCPY, HAVE_GETWD, HAVE_SETLOCALE.
m/gec63.h: Deleted; totally broken, not used at all, not in FSF.
m/7300.h, m/acorn.h, m/alliant-2800.h, m/alliant.h, m/altos.h, m/amdahl.h, m/apollo.h, m/att3b.h, m/aviion.h, m/celerity.h, m/clipper.h, m/cnvrgnt.h, m/convex.h, m/cydra5.h, m/delta.h, m/delta88k.h, m/dpx2.h, m/elxsi.h, m/ews4800r.h, m/gould.h, m/hp300bsd.h, m/hp800.h, m/hp9000s300.h, m/i860.h, m/ibmps2-aix.h, m/ibmrs6000.h, m/ibmrt-aix.h, m/ibmrt.h, m/intel386.h, m/iris4d.h, m/iris5d.h, m/iris6d.h, m/irist.h, m/isi-ov.h, m/luna88k.h, m/m68k.h, m/masscomp.h, m/mg1.h, m/mips-nec.h, m/mips-siemens.h, m/mips.h, m/news.h, m/nh3000.h, m/nh4000.h, m/ns32000.h, m/orion105.h, m/pfa50.h, m/plexus.h, m/pmax.h, m/powerpc.h, m/pyrmips.h, m/sequent-ptx.h, m/sequent.h, m/sgi-challenge.h, m/symmetry.h, m/tad68k.h, m/tahoe.h, m/targon31.h, m/tekxd88.h, m/template.h, m/tower32.h, m/tower32v3.h, m/ustation.h, m/vax.h, m/wicat.h, m/xps100.h: Delete C_ALLOCA, HAVE_ALLOCA, STACK_DIRECTION,
BROKEN_ALLOCA_IN_FUNCTION_CALLS. All of this is auto-detected.
When in doubt, I followed recent FSF sources, which also have
these things deleted.
author | ben |
---|---|
date | Thu, 04 Nov 2004 23:08:28 +0000 |
parents | 04bc9d2f42c7 |
children | ab71ad6ff3dd |
line wrap: on
line source
/* unexec for XEmacs on Windows NT. Copyright (C) 1994 Free Software Foundation, Inc. Copyright (C) 2002 Ben Wing. This file is part of XEmacs. XEmacs is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. XEmacs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with XEmacs; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Geoff Voelker (voelker@cs.washington.edu) 8-12-94 */ /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */ /* This file has been Mule-ized, Ben Wing, 4-13-02. */ /* The linkers that come with MSVC >= 4.0 merge .bss into .data and reorder * uninitialised data so that the .data section looks like: * * crt0 initialised data * emacs initialised data * <my_edata> * library initialised data * <start of bss part of .data> * emacs static uninitialised data * library static uninitialised data * emacs global uninitialised data * <my_ebss> * library global uninitialised data * * This means that we can't use the normal my_ebss in lastfile.c trick to * differentiate between unitialised data that belongs to emacs and * uninitialised data that belongs to system libraries. This is bad because * we do want to initialise the emacs data, but we don't want to initialise * the system library data. * * To solve this problem using MSVC >= 5.0 we use a pragma directive to tell * the compiler to put emacs's data (both initialised and uninitialised) in * a separate section in the executable, and we only dump that section. This * means that all files that define initialized data must include config.h * to pick up the pragma. We don't try to make any part of that section * read-only. * * This pragma directive isn't supported by the MSVC 4.x compiler. Instead, * we dump crt0 initialised data and library static uninitialised data in * addition to the emacs data. This is wrong, but we appear to be able to * get away with it. A proper fix might involve the introduction of a static * version of my_ebss in lastfile.c and a new firstfile.c file. jhar */ #include <config.h> #include "lisp.h" #include "sysfile.h" #include "syswindows.h" /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */ /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */ 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 /* Sync with FSF Emacs 19.34.6 note: struct file_data is now defined in nt.h */ enum { HEAP_UNINITIALIZED = 1, HEAP_UNLOADED, HEAP_LOADED }; /* Basically, our "initialized" flag. */ int heap_state = HEAP_UNINITIALIZED; /* So we can find our heap in the file to recreate it. */ unsigned long heap_index_in_executable = UNINIT_LONG; void get_section_info (file_data *p_file); void copy_executable_and_dump_data_section (file_data *, file_data *); void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile); /* Cached info about the .data section in the executable. */ PUCHAR data_start_va = UNINIT_PTR; DWORD data_start_file = UNINIT_LONG; DWORD data_size = UNINIT_LONG; /* Cached info about the .bss section in the executable. */ PUCHAR bss_start = UNINIT_PTR; DWORD bss_size = UNINIT_LONG; /* 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 either when dumping portably, or when running from temacs! Do not put significant XEmacs initialization here! ********************** */ EXTERN_C void mainCRTStartup (void); EXTERN_C int _start (void); int _start (void) { /* Cache system info, e.g., the NT page size. */ cache_system_info (); /* Set OS type, so that tchar stuff below works */ init_win32_very_early (); /* If we're a dumped version of emacs then we need to recreate our heap and play tricks with our .bss section. Do this before start up. (WARNING: Do not put any code before this section that relies upon malloc () and runs in the dumped version. It won't work.) */ if (heap_state == HEAP_UNLOADED) { Extbyte executable_path[MAX_PATH * MAX_XETCHAR_SIZE]; /* Don't use mswindows_get_module_file_name() because it uses xmalloc() */ if (qxeGetModuleFileName (NULL, executable_path, MAX_PATH) == 0) { exit (1); } /* #### This is super-bogus. When I rename xemacs.exe, the renamed file still loads its heap from xemacs.exe --kkm */ #if 0 { Extbyte *p; /* To allow profiling, make sure executable_path names the .exe file, not the file created by the profiler */ p = xetcsrchr (executable_path, '\\'); xetcscpy (p + 1, XETEXT (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. */ _fmode = O_BINARY; #endif #if 0 /* This prevents ctrl-c's in shells running while we're suspended from having us exit. */ SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE); #endif mainCRTStartup (); return 0; /* not reached? */ } /* Dump out .data and .bss sections into a new executable. */ int unexec (Ibyte *new_name, Ibyte *old_name, unsigned int UNUSED (start_data), unsigned int UNUSED (start_bss), unsigned int UNUSED (entry_address)) { file_data in_file, out_file; Ibyte *out_filename = alloca_ibytes (qxestrlen (new_name) + 10); Ibyte *in_filename = alloca_ibytes (qxestrlen (old_name) + 10); unsigned long size; Ibyte *ptr; HINSTANCE hImagehelp; /* Make sure that the input and output filenames have the ".exe" extension...patch them up if they don't. */ qxestrcpy (in_filename, old_name); ptr = in_filename + qxestrlen (in_filename) - 4; if (qxestrcmp_ascii (ptr, ".exe")) qxestrcat_ascii (in_filename, ".exe"); qxestrcpy (out_filename, new_name); ptr = out_filename + qxestrlen (out_filename) - 4; if (qxestrcmp_ascii (ptr, ".exe")) qxestrcat_ascii (out_filename, ".exe"); stdout_out ("Dumping from %s\n", in_filename); stdout_out (" to %s\n", out_filename); /* We need to round off our heap to NT's allocation unit (64KB). */ round_heap (get_allocation_unit ()); /* Open the undumped executable file. */ if (!open_input_file (&in_file, in_filename)) { stdout_out ("Failed to open %s (%d)...bailing.\n", in_filename, GetLastError ()); exit (1); } /* Get the interesting section info, like start and size of .bss... */ get_section_info (&in_file); /* 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 = (unsigned long) round_to_next ((Rawbyte *) in_file.size, get_allocation_unit ()); size = heap_index_in_executable + get_committed_heap_size () + bss_size; if (!open_output_file (&out_file, out_filename, size)) { stdout_out ("Failed to open %s (%d)...bailing.\n", out_filename, GetLastError ()); exit (1); } /* Set the flag (before dumping). */ heap_state = HEAP_UNLOADED; copy_executable_and_dump_data_section (&in_file, &out_file); dump_bss_and_heap (&in_file, &out_file); /* Patch up header fields; profiler is picky about this. */ hImagehelp = LoadLibraryA ("imagehlp.dll"); if (hImagehelp) { 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) ((Rawbyte *) dos_header + dos_header->e_lfanew); nt_header->OptionalHeader.CheckSum = 0; #if 0 nt_header->FileHeader.TimeDateStamp = time (NULL); dos_header->e_cp = size / 512; nt_header->OptionalHeader.SizeOfImage = size; #endif pfnCheckSumMappedFile = (pfnCheckSumMappedFile_t) GetProcAddress (hImagehelp, "CheckSumMappedFile"); if (pfnCheckSumMappedFile) { #if 0 nt_header->FileHeader.TimeDateStamp = time (NULL); #endif pfnCheckSumMappedFile (out_file.file_base, out_file.size, &headersum, &checksum); nt_header->OptionalHeader.CheckSum = checksum; } FreeLibrary (hImagehelp); } close_file_data (&in_file); close_file_data (&out_file); return 0; } /* Routines to manipulate NT executable file sections. */ #ifndef DUMP_SEPARATE_SECTION static void get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start, DWORD *p_bss_size) { int n, start, len; Ibyte *map_filename = alloca_ibytes (qxestrlen (p_infile->name) + 10); Extbyte buffer[256]; FILE *map; /* Overwrite the .exe extension on the executable file name with the .map extension. */ qxestrcpy (map_filename, p_infile->name); n = qxestrlen (map_filename) - 3; qxestrcpy (&map_filename[n], "map"); map = qxe_fopen (map_filename, "r"); if (!map) { stdout_out ("Failed to open map file %s, error %d...bailing out.\n", map_filename, GetLastError ()); exit (-1); } while (fgets (buffer, sizeof (buffer), map)) { if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA"))) continue; n = sscanf (buffer, " %*d:%x %x", &start, &len); if (n != 2) { /* printf with external data, stdout_out with internal */ printf ("Failed to scan the .bss section line:\n%s", buffer); exit (-1); } break; } *p_bss_start = (PUCHAR) start; *p_bss_size = (DWORD) len; } #endif /* Flip through the executable and cache the info necessary for dumping. */ static void get_section_info (file_data *p_infile) { PIMAGE_DOS_HEADER dos_header; PIMAGE_NT_HEADERS nt_header; PIMAGE_SECTION_HEADER section, data_section; Rawbyte *ptr; int i; dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base; if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) { stdout_out ("Unknown EXE header in %s...bailing.\n", p_infile->name); exit (1); } nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) + dos_header->e_lfanew); if (nt_header == NULL) { stdout_out ("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) { stdout_out ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n", nt_header->Signature, p_infile->name); } /* Flip through the sections for .data and .bss ... */ section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header); for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++) { #ifndef DUMP_SEPARATE_SECTION if (!qxestrcmp_ascii (section->Name, ".bss")) { extern int my_ebss; /* From lastfile.c */ ptr = (Rawbyte *) nt_header->OptionalHeader.ImageBase + section->VirtualAddress; bss_start = ptr; bss_size = (Rawbyte*) &my_ebss - (Rawbyte*) bss_start; } if (!qxestrcmp_ascii (section->Name, ".data")) #else if (!qxestrcmp_ascii (section->Name, "xdata")) #endif { extern Rawbyte my_edata[]; /* From lastfile.c */ /* The .data section. */ data_section = section; ptr = (Rawbyte *) nt_header->OptionalHeader.ImageBase + section->VirtualAddress; data_start_va = ptr; data_start_file = section->PointerToRawData; #ifndef DUMP_SEPARATE_SECTION /* Write only the part of the section that contains emacs data. */ data_size = my_edata - data_start_va; #else /* Write back the full section. */ data_size = section->SizeOfRawData; /* This code doesn't know how to grow the raw size of a section. */ if (section->SizeOfRawData < section->Misc.VirtualSize) { stdout_out ("The emacs data section is smaller than expected" "...bailing.\n"); exit (1); } #endif } section++; } #ifndef DUMP_SEPARATE_SECTION if (bss_start == UNINIT_PTR) { /* Starting with MSVC 4.0, the .bss section has been eliminated and appended virtually to the end of the .data section. Our only hint about where the .bss section starts in the address comes from the SizeOfRawData field in the .data section header. Unfortunately, this field is only approximate, as it is a rounded number and is typically rounded just beyond the start of the .bss section. To find the start and size of the .bss section exactly, we have to peek into the map file. */ extern int my_ebss; get_bss_info_from_map_file (p_infile, &ptr, &bss_size); bss_start = ptr + nt_header->OptionalHeader.ImageBase + data_section->VirtualAddress; bss_size = (Rawbyte *) &my_ebss - (Rawbyte *) bss_start; } #else bss_size = 0; #endif } /* The dump routines. */ #ifdef DEBUG_XEMACS /* printf with external data, stdout_out with internal */ #define DUMP_MSG(x) printf x #else #define DUMP_MSG(x) #endif static void copy_executable_and_dump_data_section (file_data *p_infile, file_data *p_outfile) { Rawbyte *data_file, *data_va; unsigned long size, index; /* Get a pointer to where the raw data should go in the executable file. */ data_file = (Rawbyte *) p_outfile->file_base + data_start_file; /* Get a pointer to the raw data in our address space. */ data_va = data_start_va; size = (DWORD) data_file - (DWORD) p_outfile->file_base; DUMP_MSG (("Copying executable up to data section...\n")); DUMP_MSG (("\t0x%08x Offset in input file.\n", 0)); DUMP_MSG (("\t0x%08x Offset in output file.\n", 0)); DUMP_MSG (("\t0x%08x Size in bytes.\n", size)); memcpy (p_outfile->file_base, p_infile->file_base, size); size = data_size; 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", (Rawbyte *) data_file - (Rawbyte *) p_outfile->file_base)); DUMP_MSG (("\t0x%08x Size in bytes.\n", size)); memcpy (data_file, data_va, size); index = (DWORD) data_file + size - (DWORD) p_outfile->file_base; size = p_infile->size - index; DUMP_MSG (("Copying rest of executable...\n")); DUMP_MSG (("\t0x%08x Offset in input file.\n", index)); DUMP_MSG (("\t0x%08x Offset in output file.\n", index)); DUMP_MSG (("\t0x%08x Size in bytes.\n", size)); memcpy ((Rawbyte *) p_outfile->file_base + index, (Rawbyte *) p_infile->file_base + index, size); } static void dump_bss_and_heap (file_data *UNUSED (p_infile), file_data *p_outfile) { Rawbyte *heap_data; unsigned long size, index; DUMP_MSG (("Dumping heap onto end of executable...\n")); index = heap_index_in_executable; size = get_committed_heap_size (); heap_data = get_heap_start (); DUMP_MSG (("\t0x%08x Heap start in process.\n", heap_data)); DUMP_MSG (("\t0x%08x Heap offset in executable.\n", index)); DUMP_MSG (("\t0x%08x Heap size in bytes.\n", size)); memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size); #ifndef DUMP_SEPARATE_SECTION DUMP_MSG (("Dumping bss onto end of executable...\n")); index += size; size = bss_size; 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 ((Rawbyte *) p_outfile->file_base + index, bss_start, size); #endif } #undef DUMP_MSG /* Reload and remap routines. */ /* Load the dumped .bss section into the .bss area of our address space. */ /* Already done if the .bss was part of a separate emacs data section */ void read_in_bss (Extbyte *filename) { #ifndef DUMP_SEPARATE_SECTION HANDLE file; unsigned long index, n_read; file = qxeCreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (file == INVALID_HANDLE_VALUE) abort (); /* Seek to where the .bss section is tucked away after the heap... */ index = heap_index_in_executable + get_committed_heap_size (); if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF) abort (); /* Ok, read in the saved .bss section and initialize all uninitialized variables. */ if (!ReadFile (file, bss_start, bss_size, &n_read, NULL)) abort (); CloseHandle (file); #endif } /* Map the heap dumped into the executable file into our address space. */ void map_in_heap (Extbyte *filename) { HANDLE file; HANDLE file_mapping; void *file_base; unsigned long size, upper_size, n_read; file = qxeCreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (file == INVALID_HANDLE_VALUE) abort (); size = GetFileSize (file, &upper_size); file_mapping = qxeCreateFileMapping (file, NULL, PAGE_WRITECOPY, 0, size, NULL); if (!file_mapping) abort (); 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) { 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) abort (); /* Seek to the location of the heap data in the executable. */ if (SetFilePointer (file, heap_index_in_executable, NULL, FILE_BEGIN) == 0xFFFFFFFF) abort (); /* Read in the data. */ if (!ReadFile (file, get_heap_start (), get_committed_heap_size (), &n_read, NULL)) abort (); CloseHandle (file); }