annotate src/unexw32.c @ 14667:3de266c52c9a

(read_minibuf): Bind inhibit-read-only to t around clearing the minibuffer. (read_minibuf_unwind): Likewise. Also, clearing the buffer is now the last thing we do.
author Richard M. Stallman <rms@gnu.org>
date Sun, 25 Feb 1996 20:46:59 +0000
parents ee40177f6c68
children bdffcf3b695f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
1 /* unexec for GNU Emacs on Windows NT.
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
2 Copyright (C) 1994 Free Software Foundation, Inc.
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
3
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
4 This file is part of GNU Emacs.
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
5
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
6 GNU Emacs is free software; you can redistribute it and/or modify
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
7 it under the terms of the GNU General Public License as published by
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
9 any later version.
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
10
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
11 GNU Emacs is distributed in the hope that it will be useful,
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
14 GNU General Public License for more details.
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
15
14186
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
16 You should have received a copy of the GNU General Public License
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
17 along with GNU Emacs; see the file COPYING. If not, write to
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ee40177f6c68 Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents: 14036
diff changeset
19 Boston, MA 02111-1307, USA.
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
20
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
21 Geoff Voelker (voelker@cs.washington.edu) 8-12-94
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
22 */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
23
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
24 #include <stdlib.h> /* _fmode */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
25 #include <stdio.h>
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
26 #include <fcntl.h>
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
27 #include <windows.h>
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
28
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
29 extern BOOL ctrl_c_handler (unsigned long type);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
30
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
31 #include "ntheap.h"
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
32
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
33 /* A convenient type for keeping all the info about a mapped file together. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
34 typedef struct file_data {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
35 char *name;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
36 unsigned long size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
37 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
38 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
39 unsigned char *file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
40 } file_data;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
41
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
42 /* Basically, our "initialized" flag. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
43 BOOL need_to_recreate_heap = FALSE;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
44
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
45 /* So we can find our heap in the file to recreate it. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
46 unsigned long heap_index_in_executable = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
47
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
48 void open_input_file (file_data *p_file, char *name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
49 void open_output_file (file_data *p_file, char *name, unsigned long size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
50 void close_file_data (file_data *p_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
51
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
52 void get_section_info (file_data *p_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
53 void copy_executable_and_dump_data_section (file_data *, file_data *);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
54 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
55
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
56 /* Cached info about the .data section in the executable. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
57 PUCHAR data_start_va = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
58 DWORD data_start_file = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
59 DWORD data_size = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
60
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
61 /* Cached info about the .bss section in the executable. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
62 PUCHAR bss_start = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
63 DWORD bss_size = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
64
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
65 #ifdef HAVE_NTGUI
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
66 HINSTANCE hinst = NULL;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
67 HINSTANCE hprevinst = NULL;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
68 LPSTR lpCmdLine = "";
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
69 int nCmdShow = 0;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
70
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
71 int __stdcall
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
72 WinMain (_hinst, _hPrevInst, _lpCmdLine, _nCmdShow)
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
73 HINSTANCE _hinst;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
74 HINSTANCE _hPrevInst;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
75 LPSTR _lpCmdLine;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
76 int _nCmdShow;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
77 {
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
78 /* Need to parse command line */
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
79
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
80 hinst = _hinst;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
81 hprevinst = _hPrevInst;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
82 lpCmdLine = _lpCmdLine;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
83 nCmdShow = _nCmdShow;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
84
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
85 return (main (__argc,__argv,_environ));
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
86 }
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
87 #endif /* HAVE_NTGUI */
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
88
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
89 /* Startup code for running on NT. When we are running as the dumped
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
90 version, we need to bootstrap our heap and .bss section into our
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
91 address space before we can actually hand off control to the startup
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
92 code supplied by NT (primarily because that code relies upon malloc ()). */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
93 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
94 _start (void)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
95 {
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
96 #ifdef HAVE_NTGUI
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
97 extern void WinMainCRTStartup (void);
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
98 #else
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
99 extern void mainCRTStartup (void);
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
100 #endif /* HAVE_NTGUI */
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
101
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
102 /* Cache system info, e.g., the NT page size. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
103 cache_system_info ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
104
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
105 /* If we're a dumped version of emacs then we need to recreate
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
106 our heap and play tricks with our .bss section. Do this before
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
107 start up. (WARNING: Do not put any code before this section
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
108 that relies upon malloc () and runs in the dumped version. It
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
109 won't work.) */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
110 if (need_to_recreate_heap)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
111 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
112 char executable_path[MAX_PATH];
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
113
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
114 if (GetModuleFileName (NULL, executable_path, MAX_PATH) == 0)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
115 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
116 printf ("Failed to find path for executable.\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
117 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
118 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
119 recreate_heap (executable_path);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
120 need_to_recreate_heap = FALSE;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
121 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
122
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
123 /* The default behavior is to treat files as binary and patch up
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
124 text files appropriately, in accordance with the MSDOS code. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
125 _fmode = O_BINARY;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
126
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
127 /* This prevents ctrl-c's in shells running while we're suspended from
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
128 having us exit. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
129 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
130
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
131 /* Invoke the NT CRT startup routine now that our housecleaning
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
132 is finished. */
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
133 #ifdef HAVE_NTGUI
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
134 WinMainCRTStartup ();
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
135 #else
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
136 mainCRTStartup ();
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
137 #endif /* HAVE_NTGUI */
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
138 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
139
14036
621a575db6f7 Comment fixes.
Karl Heuer <kwzh@gnu.org>
parents: 13830
diff changeset
140 /* Dump out .data and .bss sections into a new executable. */
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
141 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
142 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
143 void *entry_address)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
144 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
145 file_data in_file, out_file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
146 char out_filename[MAX_PATH], in_filename[MAX_PATH];
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
147 unsigned long size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
148 char *ptr;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
149
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
150 /* Make sure that the input and output filenames have the
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
151 ".exe" extension...patch them up if they don't. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
152 strcpy (in_filename, old_name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
153 ptr = in_filename + strlen (in_filename) - 4;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
154 if (strcmp (ptr, ".exe"))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
155 strcat (in_filename, ".exe");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
156
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
157 strcpy (out_filename, new_name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
158 ptr = out_filename + strlen (out_filename) - 4;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
159 if (strcmp (ptr, ".exe"))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
160 strcat (out_filename, ".exe");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
161
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
162 printf ("Dumping from %s\n", in_filename);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
163 printf (" to %s\n", out_filename);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
164
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
165 /* We need to round off our heap to NT's allocation unit (64KB). */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
166 round_heap (get_allocation_unit ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
167
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
168 /* Open the undumped executable file. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
169 open_input_file (&in_file, in_filename);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
170
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
171 /* Get the interesting section info, like start and size of .bss... */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
172 get_section_info (&in_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
173
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
174 /* The size of the dumped executable is the size of the original
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
175 executable plus the size of the heap and the size of the .bss section. */
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
176 heap_index_in_executable = (unsigned long)
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
177 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ());
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
178 size = heap_index_in_executable + get_committed_heap_size () + bss_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
179 open_output_file (&out_file, out_filename, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
180
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
181 /* Set the flag (before dumping). */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
182 need_to_recreate_heap = TRUE;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
183
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
184 copy_executable_and_dump_data_section (&in_file, &out_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
185 dump_bss_and_heap (&in_file, &out_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
186
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
187 close_file_data (&in_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
188 close_file_data (&out_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
189 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
190
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
191
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
192 /* File handling. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
193
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
194
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
195 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
196 open_input_file (file_data *p_file, char *filename)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
197 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
198 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
199 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
200 void *file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
201 unsigned long size, upper_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
202
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
203 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
204 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
205 if (file == INVALID_HANDLE_VALUE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
206 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
207 printf ("Failed to open %s (%d)...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
208 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
209 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
210 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
211
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
212 size = GetFileSize (file, &upper_size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
213 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
214 0, size, NULL);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
215 if (!file_mapping)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
216 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
217 printf ("Failed to create file mapping of %s (%d)...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
218 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
219 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
220 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
221
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
222 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
223 if (file_base == 0)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
224 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
225 printf ("Failed to map view of file of %s (%d)...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
226 filename, GetLastError ());
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
227 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
228 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
229
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
230 p_file->name = filename;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
231 p_file->size = size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
232 p_file->file = file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
233 p_file->file_mapping = file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
234 p_file->file_base = file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
235 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
236
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
237 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
238 open_output_file (file_data *p_file, char *filename, unsigned long size)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
239 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
240 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
241 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
242 void *file_base;
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
243 int i;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
244
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
245 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
246 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
247 if (file == INVALID_HANDLE_VALUE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
248 {
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
249 i = GetLastError ();
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
250 printf ("open_output_file: Failed to open %s (%d).\n",
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
251 filename, i);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
252 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
253 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
254
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
255 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
256 0, size, NULL);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
257 if (!file_mapping)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
258 {
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
259 i = GetLastError ();
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
260 printf ("open_output_file: Failed to create file mapping of %s (%d).\n",
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
261 filename, i);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
262 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
263 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
264
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
265 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
266 if (file_base == 0)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
267 {
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
268 i = GetLastError ();
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
269 printf ("open_output_file: Failed to map view of file of %s (%d).\n",
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
270 filename, i);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
271 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
272 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
273
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
274 p_file->name = filename;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
275 p_file->size = size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
276 p_file->file = file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
277 p_file->file_mapping = file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
278 p_file->file_base = file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
279 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
280
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
281 /* Close the system structures associated with the given file. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
282 static void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
283 close_file_data (file_data *p_file)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
284 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
285 UnmapViewOfFile (p_file->file_base);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
286 CloseHandle (p_file->file_mapping);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
287 CloseHandle (p_file->file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
288 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
289
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
290
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
291 /* Routines to manipulate NT executable file sections. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
292
13830
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
293 static void
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
294 get_bss_info_from_map_file (file_data *p_infile, PUCHAR *p_bss_start,
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
295 DWORD *p_bss_size)
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
296 {
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
297 int n, start, len;
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
298 char map_filename[MAX_PATH];
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
299 char buffer[256];
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
300 FILE *map;
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
301
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
302 /* Overwrite the .exe extension on the executable file name with
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
303 the .map extension. */
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
304 strcpy (map_filename, p_infile->name);
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
305 n = strlen (map_filename) - 3;
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
306 strcpy (&map_filename[n], "map");
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
307
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
308 map = fopen (map_filename, "r");
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
309 if (!map)
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
310 {
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
311 printf ("Failed to open map file %s, error %d...bailing out.\n",
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
312 map_filename, GetLastError ());
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
313 exit (-1);
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
314 }
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
315
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
316 while (fgets (buffer, sizeof (buffer), map))
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
317 {
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
318 if (!(strstr (buffer, ".bss") && strstr (buffer, "DATA")))
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
319 continue;
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
320 n = sscanf (buffer, " %*d:%x %x", &start, &len);
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
321 if (n != 2)
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
322 {
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
323 printf ("Failed to scan the .bss section line:\n%s", buffer);
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
324 exit (-1);
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
325 }
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
326 break;
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
327 }
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
328 *p_bss_start = (PUCHAR) start;
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
329 *p_bss_size = (DWORD) len;
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
330 }
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
331
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
332 static unsigned long
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
333 get_section_size (PIMAGE_SECTION_HEADER p_section)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
334 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
335 /* The section size is in different locations in the different versions. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
336 switch (get_nt_minor_version ())
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
337 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
338 case 10:
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
339 return p_section->SizeOfRawData;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
340 default:
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
341 return p_section->Misc.VirtualSize;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
342 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
343 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
344
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
345 /* Flip through the executable and cache the info necessary for dumping. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
346 static void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
347 get_section_info (file_data *p_infile)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
348 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
349 PIMAGE_DOS_HEADER dos_header;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
350 PIMAGE_NT_HEADERS nt_header;
13830
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
351 PIMAGE_SECTION_HEADER section, data_section;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
352 unsigned char *ptr;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
353 int i;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
354
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
355 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
356 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
357 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
358 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
359 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
360 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
361 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
362 dos_header->e_lfanew);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
363 if (nt_header == NULL)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
364 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
365 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
366 p_infile->name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
367 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
368 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
369
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
370 /* Check the NT header signature ... */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
371 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
372 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
373 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
374 nt_header->Signature, p_infile->name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
375 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
376
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
377 /* Flip through the sections for .data and .bss ... */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
378 section = (PIMAGE_SECTION_HEADER) IMAGE_FIRST_SECTION (nt_header);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
379 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
380 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
381 if (!strcmp (section->Name, ".bss"))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
382 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
383 /* The .bss section. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
384 ptr = (char *) nt_header->OptionalHeader.ImageBase +
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
385 section->VirtualAddress;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
386 bss_start = ptr;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
387 bss_size = get_section_size (section);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
388 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
389 if (!strcmp (section->Name, ".data"))
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
390 {
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
391 /* From lastfile.c */
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
392 extern char my_edata[];
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
393
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
394 /* The .data section. */
13830
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
395 data_section = section;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
396 ptr = (char *) nt_header->OptionalHeader.ImageBase +
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
397 section->VirtualAddress;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
398 data_start_va = ptr;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
399 data_start_file = section->PointerToRawData;
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
400
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
401 /* We want to only write Emacs data back to the executable,
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
402 not any of the library data (if library data is included,
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
403 then a dumped Emacs won't run on system versions other
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
404 than the one Emacs was dumped on). */
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
405 data_size = my_edata - data_start_va;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
406 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
407 section++;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
408 }
13830
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
409
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
410 if (!bss_start && !bss_size)
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
411 {
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
412 /* Starting with MSVC 4.0, the .bss section has been eliminated
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
413 and appended virtually to the end of the .data section. Our
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
414 only hint about where the .bss section starts in the address
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
415 comes from the SizeOfRawData field in the .data section
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
416 header. Unfortunately, this field is only approximate, as it
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
417 is a rounded number and is typically rounded just beyond the
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
418 start of the .bss section. To find the start and size of the
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
419 .bss section exactly, we have to peek into the map file. */
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
420 get_bss_info_from_map_file (p_infile, &ptr, &bss_size);
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
421 bss_start = ptr + nt_header->OptionalHeader.ImageBase
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
422 + data_section->VirtualAddress;
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
423 }
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
424 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
425
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
426
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
427 /* The dump routines. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
428
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
429 static void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
430 copy_executable_and_dump_data_section (file_data *p_infile,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
431 file_data *p_outfile)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
432 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
433 unsigned char *data_file, *data_va;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
434 unsigned long size, index;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
435
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
436 /* Get a pointer to where the raw data should go in the executable file. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
437 data_file = (char *) p_outfile->file_base + data_start_file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
438
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
439 /* Get a pointer to the raw data in our address space. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
440 data_va = data_start_va;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
441
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
442 size = (DWORD) data_file - (DWORD) p_outfile->file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
443 printf ("Copying executable up to data section...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
444 printf ("\t0x%08x Offset in input file.\n", 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
445 printf ("\t0x%08x Offset in output file.\n", 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
446 printf ("\t0x%08x Size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
447 memcpy (p_outfile->file_base, p_infile->file_base, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
448
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
449 size = data_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
450 printf ("Dumping .data section...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
451 printf ("\t0x%08x Address in process.\n", data_va);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
452 printf ("\t0x%08x Offset in output file.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
453 data_file - p_outfile->file_base);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
454 printf ("\t0x%08x Size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
455 memcpy (data_file, data_va, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
456
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
457 index = (DWORD) data_file + size - (DWORD) p_outfile->file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
458 size = p_infile->size - index;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
459 printf ("Copying rest of executable...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
460 printf ("\t0x%08x Offset in input file.\n", index);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
461 printf ("\t0x%08x Offset in output file.\n", index);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
462 printf ("\t0x%08x Size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
463 memcpy ((char *) p_outfile->file_base + index,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
464 (char *) p_infile->file_base + index, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
465 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
466
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
467 static void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
468 dump_bss_and_heap (file_data *p_infile, file_data *p_outfile)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
469 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
470 unsigned char *heap_data, *bss_data;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
471 unsigned long size, index;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
472
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
473 printf ("Dumping heap into executable...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
474
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
475 index = heap_index_in_executable;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
476 size = get_committed_heap_size ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
477 heap_data = get_heap_start ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
478
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
479 printf ("\t0x%08x Heap start in process.\n", heap_data);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
480 printf ("\t0x%08x Heap offset in executable.\n", index);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
481 printf ("\t0x%08x Heap size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
482
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
483 memcpy ((PUCHAR) p_outfile->file_base + index, heap_data, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
484
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
485 printf ("Dumping .bss into executable...\n");
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
486
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
487 index += size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
488 size = bss_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
489 bss_data = bss_start;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
490
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
491 printf ("\t0x%08x BSS start in process.\n", bss_data);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
492 printf ("\t0x%08x BSS offset in executable.\n", index);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
493 printf ("\t0x%08x BSS size in bytes.\n", size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
494 memcpy ((char *) p_outfile->file_base + index, bss_data, size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
495 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
496
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
497
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
498 /* Reload and remap routines. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
499
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
500
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
501 /* Load the dumped .bss section into the .bss area of our address space. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
502 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
503 read_in_bss (char *filename)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
504 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
505 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
506 unsigned long size, index, n_read, total_read;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
507 char buffer[512], *bss;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
508 int i;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
509
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
510 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
511 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
512 if (file == INVALID_HANDLE_VALUE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
513 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
514 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
515 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
516 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
517
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
518 /* Seek to where the .bss section is tucked away after the heap... */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
519 index = heap_index_in_executable + get_committed_heap_size ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
520 if (SetFilePointer (file, index, NULL, FILE_BEGIN) == 0xFFFFFFFF)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
521 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
522 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
523 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
524 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
525
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
526
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
527 /* Ok, read in the saved .bss section and initialize all
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
528 uninitialized variables. */
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
529 if (!ReadFile (file, bss_start, bss_size, &n_read, NULL))
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
530 {
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
531 i = GetLastError ();
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
532 exit (1);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
533 }
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
534
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
535 CloseHandle (file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
536 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
537
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
538 /* Map the heap dumped into the executable file into our address space. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
539 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
540 map_in_heap (char *filename)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
541 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
542 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
543 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
544 void *file_base;
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
545 unsigned long size, upper_size, n_read;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
546 int i;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
547
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
548 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
549 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
550 if (file == INVALID_HANDLE_VALUE)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
551 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
552 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
553 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
554 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
555
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
556 size = GetFileSize (file, &upper_size);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
557 file_mapping = CreateFileMapping (file, NULL, PAGE_WRITECOPY,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
558 0, size, NULL);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
559 if (!file_mapping)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
560 {
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
561 i = GetLastError ();
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
562 exit (1);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
563 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
564
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
565 size = get_committed_heap_size ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
566 file_base = MapViewOfFileEx (file_mapping, FILE_MAP_COPY, 0,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
567 heap_index_in_executable, size,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
568 get_heap_start ());
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
569 if (file_base != 0)
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
570 {
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
571 return;
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
572 }
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
573
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
574 /* If we don't succeed with the mapping, then copy from the
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
575 data into the heap. */
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
576
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
577 CloseHandle (file_mapping);
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
578
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
579 if (VirtualAlloc (get_heap_start (), get_committed_heap_size (),
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
580 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) == NULL)
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
581 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
582 i = GetLastError ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
583 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
584 }
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
585
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
586 /* Seek to the location of the heap data in the executable. */
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
587 i = heap_index_in_executable;
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
588 if (SetFilePointer (file, i, NULL, FILE_BEGIN) == 0xFFFFFFFF)
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
589 {
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
590 i = GetLastError ();
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
591 exit (1);
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
592 }
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
593
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
594 /* Read in the data. */
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
595 if (!ReadFile (file, get_heap_start (),
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
596 get_committed_heap_size (), &n_read, NULL))
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
597 {
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
598 i = GetLastError ();
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
599 exit (1);
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
600 }
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
601
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
602 CloseHandle (file);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
603 }