annotate src/unexnt.c @ 2500:3d8143fc88e1

[xemacs-hg @ 2005-01-24 23:33:30 by ben] get working with VC7 config.inc.samp: Declare OPTIONAL_LIBRARY_DIR as root of library directories. Redo all graphics library defaults to mirror the versions and directories in the current binary aux distribution on xemacs web site. Enable TIFF and COMPFACE by default since you can now compile with them and binary libs are provided. xemacs.mak: Put our own directories first in case of conflict (e.g. config.h in compface). xemacs.mak: Use MSVCRT to avoid link problems. s/windowsnt.h: bytecode.c, print.c: Add casts to avoid warning. compiler.h: Add MSC_VERSION and include definitions of DOESNT_RETURN and friends here, like for GCC. Need different definitions for VC7 and VC6. s/windowsnt.h: Remove stuff moved to compiler.h. Disable warning 4646 ("function declared with __declspec(noreturn) has non-void return type") on VC7 since lots of Lisp primitives trigger this and there is no easy way to kludge around the warning. glyphs-eimage.c: Some really nasty hacks to allow TIFF and JPEG to both be compiled. #### The better solution is to move the TIFF and JPEG code to different files. glyphs-msw.c: Define __STDC__ to avoid problems with compface.h. intl-auto-encap-win32.c, intl-auto-encap-win32.h, intl-encap-win32.c, syswindows.h: Those wankers at Microsoft cannot leave well enough alone. Various functions change parameter types semi-randomly between VC6 and VC7, so we need to include our own versions that can handle both kinds with appropriate casting. EmacsFrame.c, EmacsShell-sub.c, EmacsShell.c, alloc.c, alloca.c, buffer.c, bytecode.c, charset.h, chartab.c, cm.c, console-stream.c, console.c, data.c, debug.h, device-msw.c, device-tty.c, device-x.c, doprnt.c, dumper.c, dynarr.c, elhash.c, emacs.c, eval.c, event-Xt.c, event-gtk.c, event-msw.c, event-stream.c, events.c, extents.c, faces.c, file-coding.c, fileio.c, fns.c, font-lock.c, frame-gtk.c, frame-x.c, frame.c, free-hook.c, gccache-gtk.c, glyphs-eimage.c, glyphs-gtk.c, glyphs-msw.c, glyphs-x.c, glyphs.c, gtk-glue.c, gutter.c, input-method-xlib.c, insdel.c, intl-win32.c, keymap.c, lisp.h, lread.c, lstream.c, macros.c, malloc.c, menubar-gtk.c, menubar-msw.c, menubar-x.c, mule-coding.c, native-gtk-toolbar.c, number.c, objects-msw.c, objects.c, print.c, process-nt.c, process-unix.c, process.c, ralloc.c, rangetab.c, redisplay-gtk.c, redisplay-msw.c, redisplay-output.c, redisplay-tty.c, redisplay-x.c, redisplay.c, regex.c, scrollbar-gtk.c, scrollbar-x.c, search.c, select-x.c, signal.c, specifier.c, specifier.h, strftime.c, sunplay.c, symbols.c, sysdep.c, sysproc.h, text.c, text.h, toolbar-common.c, toolbar-msw.c, toolbar.c, ui-gtk.c, unexnt.c, unicode.c, win32.c, window.c, xgccache.c, s/windowsnt.h: abort() -> ABORT(). Eliminate preprocessor games with abort() since it creates huge problems in VC7, solvable only by including massive amounts of files in every compile (and not worth it).
author ben
date Mon, 24 Jan 2005 23:34:34 +0000
parents ab71ad6ff3dd
children 95c4ced5c07c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
613
023b83f4e54b [xemacs-hg @ 2001-06-10 10:42:16 by ben]
ben
parents: 558
diff changeset
1 /* unexec for XEmacs on Windows NT.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1994 Free Software Foundation, Inc.
1204
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
3 Copyright (C) 2002 Ben Wing.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to the Free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 Geoff Voelker (voelker@cs.washington.edu) 8-12-94 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
26 /* This file has been Mule-ized, Ben Wing, 4-13-02. */
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
27
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 /* The linkers that come with MSVC >= 4.0 merge .bss into .data and reorder
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 * uninitialised data so that the .data section looks like:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 * crt0 initialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 * emacs initialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 * <my_edata>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 * library initialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 * <start of bss part of .data>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 * emacs static uninitialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 * library static uninitialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 * emacs global uninitialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 * <my_ebss>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 * library global uninitialised data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 * This means that we can't use the normal my_ebss in lastfile.c trick to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 * differentiate between unitialised data that belongs to emacs and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44 * uninitialised data that belongs to system libraries. This is bad because
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 * we do want to initialise the emacs data, but we don't want to initialise
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 * the system library data.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 * To solve this problem using MSVC >= 5.0 we use a pragma directive to tell
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 * the compiler to put emacs's data (both initialised and uninitialised) in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 * a separate section in the executable, and we only dump that section. This
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 * means that all files that define initialized data must include config.h
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 * to pick up the pragma. We don't try to make any part of that section
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 * read-only.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 *
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 * This pragma directive isn't supported by the MSVC 4.x compiler. Instead,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 * we dump crt0 initialised data and library static uninitialised data in
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 * addition to the emacs data. This is wrong, but we appear to be able to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 * get away with it. A proper fix might involve the introduction of a static
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 * version of my_ebss in lastfile.c and a new firstfile.c file. jhar */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 #include <config.h>
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
62 #include "lisp.h"
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
63
558
ed498ef2108b [xemacs-hg @ 2001-05-23 09:59:33 by ben]
ben
parents: 442
diff changeset
64 #include "sysfile.h"
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
65 #include "syswindows.h"
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
66
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 /* From IMAGEHLP.H which is not installed by default by MSVC < 5 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 /* The IMAGEHLP.DLL library is not distributed by default with Windows95 */
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
69 typedef PIMAGE_NT_HEADERS
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
70 (__stdcall * pfnCheckSumMappedFile_t) (LPVOID BaseAddress, DWORD FileLength,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
71 LPDWORD HeaderSum, LPDWORD CheckSum);
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
72
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 extern BOOL ctrl_c_handler (unsigned long type);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
78 /* Sync with FSF Emacs 19.34.6
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
79 note: struct file_data is now defined in nt.h */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 enum {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 HEAP_UNINITIALIZED = 1,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 HEAP_UNLOADED,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 HEAP_LOADED
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 /* Basically, our "initialized" flag. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 int heap_state = HEAP_UNINITIALIZED;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 /* So we can find our heap in the file to recreate it. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 unsigned long heap_index_in_executable = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 void get_section_info (file_data *p_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 void copy_executable_and_dump_data_section (file_data *, file_data *);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 /* Cached info about the .data section in the executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 PUCHAR data_start_va = UNINIT_PTR;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 DWORD data_start_file = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 DWORD data_size = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 /* Cached info about the .bss section in the executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 PUCHAR bss_start = UNINIT_PTR;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 DWORD bss_size = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 /* Startup code for running on NT. When we are running as the dumped
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 version, we need to bootstrap our heap and .bss section into our
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 address space before we can actually hand off control to the startup
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 code supplied by NT (primarily because that code relies upon malloc ()). */
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
110
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
111 /* **********************
1204
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
112 Hackers please remember, this _start() thingy is *not* called either
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
113 when dumping portably, or when running from temacs! Do not put
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
114 significant XEmacs initialization here!
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
115 ********************** */
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
116
1204
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
117 EXTERN_C void mainCRTStartup (void);
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
118
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
119 EXTERN_C int _start (void);
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
120
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
121 int
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 _start (void)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 /* Cache system info, e.g., the NT page size. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 cache_system_info ();
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
126 /* Set OS type, so that tchar stuff below works */
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
127 init_win32_very_early ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 /* If we're a dumped version of emacs then we need to recreate
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 our heap and play tricks with our .bss section. Do this before
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 start up. (WARNING: Do not put any code before this section
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 that relies upon malloc () and runs in the dumped version. It
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 won't work.) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 if (heap_state == HEAP_UNLOADED)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 {
2421
ab71ad6ff3dd [xemacs-hg @ 2004-12-06 03:50:53 by ben]
ben
parents: 2367
diff changeset
136 Extbyte executable_path[PATH_MAX_EXTERNAL];
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
138 /* Don't use mswindows_get_module_file_name() because it uses
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
139 xmalloc() */
2421
ab71ad6ff3dd [xemacs-hg @ 2004-12-06 03:50:53 by ben]
ben
parents: 2367
diff changeset
140 if (qxeGetModuleFileName (NULL, executable_path, _MAX_PATH) == 0)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
145 /* #### This is super-bogus. When I rename xemacs.exe,
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
146 the renamed file still loads its heap from xemacs.exe --kkm */
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
147 #if 0
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
148 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
149 Extbyte *p;
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
150
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
151 /* To allow profiling, make sure executable_path names the .exe
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
152 file, not the file created by the profiler */
2421
ab71ad6ff3dd [xemacs-hg @ 2004-12-06 03:50:53 by ben]
ben
parents: 2367
diff changeset
153 p = qxetcsrchr (executable_path, '\\');
ab71ad6ff3dd [xemacs-hg @ 2004-12-06 03:50:53 by ben]
ben
parents: 2367
diff changeset
154 qxetcscpy (p + 1, XETEXT (PATH_PROGNAME ".exe"));
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
155 }
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
156 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 recreate_heap (executable_path);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 heap_state = HEAP_LOADED;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
162 /* #### This is bogus, too. _fmode is set to different values
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
163 when we run `xemacs' and `temacs run-emacs'. The sooner we
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
164 hit and fix all the weirdities this causes us, the better --kkm */
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
165 #if 0
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 /* The default behavior is to treat files as binary and patch up
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
167 text files appropriately. */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 _fmode = O_BINARY;
440
8de8e3f6228a Import from CVS: tag r21-2-28
cvs
parents: 428
diff changeset
169 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 /* This prevents ctrl-c's in shells running while we're suspended from
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 having us exit. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 mainCRTStartup ();
1204
e22b0213b713 [xemacs-hg @ 2003-01-12 11:07:58 by michaels]
michaels
parents: 867
diff changeset
178 return 0; /* not reached? */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 /* Dump out .data and .bss sections into a new executable. */
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
182 int
2286
04bc9d2f42c7 [xemacs-hg @ 2004-09-20 19:18:55 by james]
james
parents: 1204
diff changeset
183 unexec (Ibyte *new_name, Ibyte *old_name, unsigned int UNUSED (start_data),
04bc9d2f42c7 [xemacs-hg @ 2004-09-20 19:18:55 by james]
james
parents: 1204
diff changeset
184 unsigned int UNUSED (start_bss), unsigned int UNUSED (entry_address))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 file_data in_file, out_file;
867
804517e16990 [xemacs-hg @ 2002-06-05 09:54:39 by ben]
ben
parents: 814
diff changeset
187 Ibyte *out_filename = alloca_ibytes (qxestrlen (new_name) + 10);
804517e16990 [xemacs-hg @ 2002-06-05 09:54:39 by ben]
ben
parents: 814
diff changeset
188 Ibyte *in_filename = alloca_ibytes (qxestrlen (old_name) + 10);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 unsigned long size;
867
804517e16990 [xemacs-hg @ 2002-06-05 09:54:39 by ben]
ben
parents: 814
diff changeset
190 Ibyte *ptr;
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
191 HINSTANCE hImagehelp;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 /* Make sure that the input and output filenames have the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 ".exe" extension...patch them up if they don't. */
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
195 qxestrcpy (in_filename, old_name);
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
196 ptr = in_filename + qxestrlen (in_filename) - 4;
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
197 if (qxestrcmp_ascii (ptr, ".exe"))
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
198 qxestrcat_ascii (in_filename, ".exe");
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
200 qxestrcpy (out_filename, new_name);
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
201 ptr = out_filename + qxestrlen (out_filename) - 4;
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
202 if (qxestrcmp_ascii (ptr, ".exe"))
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
203 qxestrcat_ascii (out_filename, ".exe");
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
205 stdout_out ("Dumping from %s\n", in_filename);
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
206 stdout_out (" to %s\n", out_filename);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 /* We need to round off our heap to NT's allocation unit (64KB). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 round_heap (get_allocation_unit ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 /* Open the undumped executable file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 if (!open_input_file (&in_file, in_filename))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
214 stdout_out ("Failed to open %s (%d)...bailing.\n",
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
215 in_filename, GetLastError ());
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 /* Get the interesting section info, like start and size of .bss... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 get_section_info (&in_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 /* The size of the dumped executable is the size of the original
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 executable plus the size of the heap and the size of the .bss section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 heap_index_in_executable = (unsigned long)
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
225 round_to_next ((Rawbyte *) in_file.size, get_allocation_unit ());
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 size = heap_index_in_executable + get_committed_heap_size () + bss_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 if (!open_output_file (&out_file, out_filename, size))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
229 stdout_out ("Failed to open %s (%d)...bailing.\n",
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
230 out_filename, GetLastError ());
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234 /* Set the flag (before dumping). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 heap_state = HEAP_UNLOADED;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 copy_executable_and_dump_data_section (&in_file, &out_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 dump_bss_and_heap (&in_file, &out_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 /* Patch up header fields; profiler is picky about this. */
812
f846c2ef930d [xemacs-hg @ 2002-04-13 20:31:27 by jhar]
jhar
parents: 771
diff changeset
241 hImagehelp = LoadLibraryA ("imagehlp.dll");
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 if (hImagehelp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 PIMAGE_DOS_HEADER dos_header;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 PIMAGE_NT_HEADERS nt_header;
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
246
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 DWORD headersum;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 DWORD checksum;
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
249 pfnCheckSumMappedFile_t pfnCheckSumMappedFile;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
252 nt_header = (PIMAGE_NT_HEADERS) ((Rawbyte *) dos_header +
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
253 dos_header->e_lfanew);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 nt_header->OptionalHeader.CheckSum = 0;
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
256 #if 0
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
257 nt_header->FileHeader.TimeDateStamp = time (NULL);
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
258 dos_header->e_cp = size / 512;
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
259 nt_header->OptionalHeader.SizeOfImage = size;
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
260 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
262 pfnCheckSumMappedFile =
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
263 (pfnCheckSumMappedFile_t) GetProcAddress (hImagehelp,
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
264 "CheckSumMappedFile");
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 if (pfnCheckSumMappedFile)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 {
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
267 #if 0
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
268 nt_header->FileHeader.TimeDateStamp = time (NULL);
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
269 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 pfnCheckSumMappedFile (out_file.file_base,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 out_file.size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 &headersum,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 &checksum);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 nt_header->OptionalHeader.CheckSum = checksum;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 FreeLibrary (hImagehelp);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 close_file_data (&in_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 close_file_data (&out_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
282 return 0;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 /* Routines to manipulate NT executable file sections. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 DWORD *p_bss_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 int n, start, len;
867
804517e16990 [xemacs-hg @ 2002-06-05 09:54:39 by ben]
ben
parents: 814
diff changeset
293 Ibyte *map_filename = alloca_ibytes (qxestrlen (p_infile->name) + 10);
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
294 Extbyte buffer[256];
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 FILE *map;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 /* Overwrite the .exe extension on the executable file name with
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 the .map extension. */
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
299 qxestrcpy (map_filename, p_infile->name);
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
300 n = qxestrlen (map_filename) - 3;
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
301 qxestrcpy (&map_filename[n], "map");
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
303 map = qxe_fopen (map_filename, "r");
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 if (!map)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
306 stdout_out ("Failed to open map file %s, error %d...bailing out.\n",
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
307 map_filename, GetLastError ());
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 exit (-1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 while (fgets (buffer, sizeof (buffer), map))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 continue;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 n = sscanf (buffer, " %*d:%x %x", &start, &len);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 if (n != 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
318 /* printf with external data, stdout_out with internal */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 printf ("Failed to scan the .bss section line:\n%s", buffer);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 exit (-1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 *p_bss_start = (PUCHAR) start;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 *p_bss_size = (DWORD) len;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 /* Flip through the executable and cache the info necessary for dumping. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 get_section_info (file_data *p_infile)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 PIMAGE_DOS_HEADER dos_header;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 PIMAGE_NT_HEADERS nt_header;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 PIMAGE_SECTION_HEADER section, data_section;
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
336 Rawbyte *ptr;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 int i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
342 stdout_out ("Unknown EXE header in %s...bailing.\n", p_infile->name);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 dos_header->e_lfanew);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 if (nt_header == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
349 stdout_out ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
350 p_infile->name);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 /* Check the NT header signature ... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
357 stdout_out ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
358 nt_header->Signature, p_infile->name);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 /* Flip through the sections for .data and .bss ... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 #ifndef DUMP_SEPARATE_SECTION
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
366 if (!qxestrcmp_ascii (section->Name, ".bss"))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 extern int my_ebss; /* From lastfile.c */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
370 ptr = (Rawbyte *) nt_header->OptionalHeader.ImageBase +
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 section->VirtualAddress;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 bss_start = ptr;
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
373 bss_size = (Rawbyte*) &my_ebss - (Rawbyte*) bss_start;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
376 if (!qxestrcmp_ascii (section->Name, ".data"))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 #else
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
378 if (!qxestrcmp_ascii (section->Name, "xdata"))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 {
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
381 extern Rawbyte my_edata[]; /* From lastfile.c */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 /* The .data section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 data_section = section;
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
385 ptr = (Rawbyte *) nt_header->OptionalHeader.ImageBase +
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 section->VirtualAddress;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 data_start_va = ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 data_start_file = section->PointerToRawData;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 /* Write only the part of the section that contains emacs data. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 data_size = my_edata - data_start_va;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 /* Write back the full section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 data_size = section->SizeOfRawData;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 /* This code doesn't know how to grow the raw size of a section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 if (section->SizeOfRawData < section->Misc.VirtualSize)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 {
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
400 stdout_out ("The emacs data section is smaller than expected"
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
401 "...bailing.\n");
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 section++;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 if (bss_start == UNINIT_PTR)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 /* Starting with MSVC 4.0, the .bss section has been eliminated
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 and appended virtually to the end of the .data section. Our
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 only hint about where the .bss section starts in the address
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 comes from the SizeOfRawData field in the .data section
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 header. Unfortunately, this field is only approximate, as it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 is a rounded number and is typically rounded just beyond the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 start of the .bss section. To find the start and size of the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 .bss section exactly, we have to peek into the map file. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 extern int my_ebss;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 get_bss_info_from_map_file (p_infile, &ptr, &bss_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 bss_start = ptr + nt_header->OptionalHeader.ImageBase
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 + data_section->VirtualAddress;
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
425 bss_size = (Rawbyte *) &my_ebss - (Rawbyte *) bss_start;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 bss_size = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 /* The dump routines. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 #ifdef DEBUG_XEMACS
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
436 /* printf with external data, stdout_out with internal */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 #define DUMP_MSG(x) printf x
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 #define DUMP_MSG(x)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 copy_executable_and_dump_data_section (file_data *p_infile,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 file_data *p_outfile)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 {
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
446 Rawbyte *data_file, *data_va;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 unsigned long size, index;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 /* Get a pointer to where the raw data should go in the executable file. */
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
450 data_file = (Rawbyte *) p_outfile->file_base + data_start_file;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 /* Get a pointer to the raw data in our address space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 data_va = data_start_va;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 size = (DWORD) data_file - (DWORD) p_outfile->file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 DUMP_MSG (("Copying executable up to data section...\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 DUMP_MSG (("\t0x%08x Offset in input file.\n", 0));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 DUMP_MSG (("\t0x%08x Offset in output file.\n", 0));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 memcpy (p_outfile->file_base, p_infile->file_base, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462 size = data_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 DUMP_MSG (("Dumping data section...\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 DUMP_MSG (("\t0x%08x Address in process.\n", data_va));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 DUMP_MSG (("\t0x%08x Offset in output file.\n",
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
466 (Rawbyte *) data_file -
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
467 (Rawbyte *) p_outfile->file_base));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 memcpy (data_file, data_va, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 size = p_infile->size - index;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 DUMP_MSG (("Copying rest of executable...\n"));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 DUMP_MSG (("\t0x%08x Offset in input file.\n", index));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 DUMP_MSG (("\t0x%08x Offset in output file.\n", index));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 DUMP_MSG (("\t0x%08x Size in bytes.\n", size));
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
477 memcpy ((Rawbyte *) p_outfile->file_base + index,
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
478 (Rawbyte *) p_infile->file_base + index, size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 static void
2286
04bc9d2f42c7 [xemacs-hg @ 2004-09-20 19:18:55 by james]
james
parents: 1204
diff changeset
482 dump_bss_and_heap (file_data *UNUSED (p_infile), file_data *p_outfile)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 {
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
484 Rawbyte *heap_data;
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
485 unsigned long size, index;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
487 DUMP_MSG (("Dumping heap onto end of executable...\n"));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
489 index = heap_index_in_executable;
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
490 size = get_committed_heap_size ();
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
491 heap_data = get_heap_start ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
493 DUMP_MSG (("\t0x%08x Heap start in process.\n", heap_data));
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
494 DUMP_MSG (("\t0x%08x Heap offset in executable.\n", index));
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
495 DUMP_MSG (("\t0x%08x Heap size in bytes.\n", size));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
497 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 #ifndef DUMP_SEPARATE_SECTION
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
500 DUMP_MSG (("Dumping bss onto end of executable...\n"));
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
502 index += size;
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
503 size = bss_size;
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
505 DUMP_MSG (("\t0x%08x BSS start in process.\n", bss_start));
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
506 DUMP_MSG (("\t0x%08x BSS offset in executable.\n", index));
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
507 DUMP_MSG (("\t0x%08x BSS size in bytes.\n", size));
2367
ecf1ebac70d8 [xemacs-hg @ 2004-11-04 23:05:23 by ben]
ben
parents: 2286
diff changeset
508 memcpy ((Rawbyte *) p_outfile->file_base + index, bss_start, size);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 #undef DUMP_MSG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514 /* Reload and remap routines. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 /* Load the dumped .bss section into the .bss area of our address space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 /* Already done if the .bss was part of a separate emacs data section */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
519 void
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
520 read_in_bss (Extbyte *filename)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 #ifndef DUMP_SEPARATE_SECTION
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 HANDLE file;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 unsigned long index, n_read;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
526 file = qxeCreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
527 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 if (file == INVALID_HANDLE_VALUE)
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2421
diff changeset
529 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 /* Seek to where the .bss section is tucked away after the heap... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 index = heap_index_in_executable + get_committed_heap_size ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF)
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2421
diff changeset
534 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 /* Ok, read in the saved .bss section and initialize all
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 uninitialized variables. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 if (!ReadFile (file, bss_start, bss_size, &n_read, NULL))
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2421
diff changeset
539 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 CloseHandle (file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545 /* Map the heap dumped into the executable file into our address space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 void
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
547 map_in_heap (Extbyte *filename)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 HANDLE file;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 HANDLE file_mapping;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 void *file_base;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
552 unsigned long size, upper_size, n_read;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
554 file = qxeCreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
555 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 if (file == INVALID_HANDLE_VALUE)
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2421
diff changeset
557 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 size = GetFileSize (file, &upper_size);
814
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
560 file_mapping = qxeCreateFileMapping (file, NULL, PAGE_WRITECOPY,
a634e3b7acc8 [xemacs-hg @ 2002-04-14 12:41:59 by ben]
ben
parents: 812
diff changeset
561 0, size, NULL);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 if (!file_mapping)
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2421
diff changeset
563 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 size = get_committed_heap_size ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567 heap_index_in_executable, size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 get_heap_start ());
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 if (file_base != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 /* If we don't succeed with the mapping, then copy from the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 data into the heap. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577 CloseHandle (file_mapping);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL)
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2421
diff changeset
581 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583 /* Seek to the location of the heap data in the executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 if (SetFilePointer (file, heap_index_in_executable,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585 NULL, FILE_BEGIN) == 0xFFFFFFFF)
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2421
diff changeset
586 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 /* Read in the data. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 if (!ReadFile (file, get_heap_start (),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 get_committed_heap_size (), &n_read, NULL))
2500
3d8143fc88e1 [xemacs-hg @ 2005-01-24 23:33:30 by ben]
ben
parents: 2421
diff changeset
591 ABORT ();
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 CloseHandle (file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594 }