annotate src/unexw32.c @ 51853:fd71e1aaa838

(fix_overlays_before): Fix typo in last commit.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Wed, 09 Jul 2003 19:05:41 +0000
parents ec1e9e347981
children 695cf19ef79e
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
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
24 #include <config.h>
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
25
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
26 #include <stdio.h>
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
27 #include <fcntl.h>
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
28 #include <time.h>
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
29 #include <windows.h>
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
30
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
31 /* Include relevant definitions from IMAGEHLP.H, which can be found
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
32 in \\win32sdk\mstools\samples\image\include\imagehlp.h. */
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
33
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
34 PIMAGE_NT_HEADERS
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
35 (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress,
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
36 DWORD FileLength,
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
37 LPDWORD HeaderSum,
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
38 LPDWORD CheckSum);
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
39
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
40 extern BOOL ctrl_c_handler (unsigned long type);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
41
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
42 extern char my_begdata[];
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
43 extern char my_edata[];
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
44 extern char my_begbss[];
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
45 extern char my_endbss[];
21604
104277bc1444 (my_begbss_static, my_endbss_static): Declare.
Geoff Voelker <voelker@cs.washington.edu>
parents: 21456
diff changeset
46 extern char *my_begbss_static;
104277bc1444 (my_begbss_static, my_endbss_static): Declare.
Geoff Voelker <voelker@cs.washington.edu>
parents: 21456
diff changeset
47 extern char *my_endbss_static;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
48
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
49 #include "w32heap.h"
18506
bcc706aedcea Add pragma to force zero initialized
Geoff Voelker <voelker@cs.washington.edu>
parents: 16593
diff changeset
50
21456
c0496e62b737 (min, max): Define as macros.
Karl Heuer <kwzh@gnu.org>
parents: 19703
diff changeset
51 #undef min
c0496e62b737 (min, max): Define as macros.
Karl Heuer <kwzh@gnu.org>
parents: 19703
diff changeset
52 #undef max
c0496e62b737 (min, max): Define as macros.
Karl Heuer <kwzh@gnu.org>
parents: 19703
diff changeset
53 #define min(x, y) (((x) < (y)) ? (x) : (y))
c0496e62b737 (min, max): Define as macros.
Karl Heuer <kwzh@gnu.org>
parents: 19703
diff changeset
54 #define max(x, y) (((x) > (y)) ? (x) : (y))
c0496e62b737 (min, max): Define as macros.
Karl Heuer <kwzh@gnu.org>
parents: 19703
diff changeset
55
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
56 /* Basically, our "initialized" flag. */
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
57 BOOL using_dynamic_heap = FALSE;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
58
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
59 int open_input_file (file_data *p_file, char *name);
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
60 int open_output_file (file_data *p_file, char *name, unsigned long size);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
61 void close_file_data (file_data *p_file);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
62
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
63 void get_section_info (file_data *p_file);
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
64 void copy_executable_and_dump_data (file_data *, file_data *);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
65 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
66
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
67 /* Cached info about the .data section in the executable. */
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
68 PIMAGE_SECTION_HEADER data_section;
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
69 PCHAR data_start = 0;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
70 DWORD data_size = 0;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
71
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
72 /* Cached info about the .bss section in the executable. */
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
73 PIMAGE_SECTION_HEADER bss_section;
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
74 PCHAR bss_start = 0;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
75 DWORD bss_size = 0;
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
76 DWORD extra_bss_size = 0;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
77 /* bss data that is static might be discontiguous from non-static. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
78 PIMAGE_SECTION_HEADER bss_section_static;
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
79 PCHAR bss_start_static = 0;
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
80 DWORD bss_size_static = 0;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
81 DWORD extra_bss_size_static = 0;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
82
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
83 PIMAGE_SECTION_HEADER heap_section;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
84
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
85 #ifdef HAVE_NTGUI
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
86 HINSTANCE hinst = NULL;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
87 HINSTANCE hprevinst = NULL;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
88 LPSTR lpCmdLine = "";
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
89 int nCmdShow = 0;
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
90 #endif /* HAVE_NTGUI */
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
91
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
92 /* Startup code for running on NT. When we are running as the dumped
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
93 version, we need to bootstrap our heap and .bss section into our
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
94 address space before we can actually hand off control to the startup
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
95 code supplied by NT (primarily because that code relies upon malloc ()). */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
96 void
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
97 _start (void)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
98 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
99 extern void mainCRTStartup (void);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
100
24237
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
101 #if 1
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
102 /* Give us a way to debug problems with crashes on startup when
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
103 running under the MSVC profiler. */
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
104 if (GetEnvironmentVariable ("EMACS_DEBUG", NULL, 0) > 0)
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
105 DebugBreak ();
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
106 #endif
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
107
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
108 /* Cache system info, e.g., the NT page size. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
109 cache_system_info ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
110
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
111 /* Grab our malloc arena space now, before CRT starts up. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
112 init_heap ();
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
113
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
114 /* This prevents ctrl-c's in shells running while we're suspended from
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
115 having us exit. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
116 SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
117
23948
5d7a43e8b133 (_start): Force system calls accessing unmounted devices to fail
Andrew Innes <andrewi@gnu.org>
parents: 22300
diff changeset
118 /* Prevent Emacs from being locked up (eg. in batch mode) when
5d7a43e8b133 (_start): Force system calls accessing unmounted devices to fail
Andrew Innes <andrewi@gnu.org>
parents: 22300
diff changeset
119 accessing devices that aren't mounted (eg. removable media drives). */
5d7a43e8b133 (_start): Force system calls accessing unmounted devices to fail
Andrew Innes <andrewi@gnu.org>
parents: 22300
diff changeset
120 SetErrorMode (SEM_FAILCRITICALERRORS);
5d7a43e8b133 (_start): Force system calls accessing unmounted devices to fail
Andrew Innes <andrewi@gnu.org>
parents: 22300
diff changeset
121
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
122 /* Invoke the NT CRT startup routine now that our housecleaning
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
123 is finished. */
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
124 #ifdef HAVE_NTGUI
15146
be6560506cce (WinMain): Function removed.
Geoff Voelker <voelker@cs.washington.edu>
parents: 14812
diff changeset
125 /* determine WinMain args like crt0.c does */
be6560506cce (WinMain): Function removed.
Geoff Voelker <voelker@cs.washington.edu>
parents: 14812
diff changeset
126 hinst = GetModuleHandle(NULL);
be6560506cce (WinMain): Function removed.
Geoff Voelker <voelker@cs.washington.edu>
parents: 14812
diff changeset
127 lpCmdLine = GetCommandLine();
be6560506cce (WinMain): Function removed.
Geoff Voelker <voelker@cs.washington.edu>
parents: 14812
diff changeset
128 nCmdShow = SW_SHOWDEFAULT;
be6560506cce (WinMain): Function removed.
Geoff Voelker <voelker@cs.washington.edu>
parents: 14812
diff changeset
129 #endif
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
130 mainCRTStartup ();
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
131 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
132
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
133
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
134 /* File handling. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
135
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
136 int
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
137 open_input_file (file_data *p_file, char *filename)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
138 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
139 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
140 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
141 void *file_base;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
142 unsigned long size, upper_size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
143
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
144 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
145 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
146 if (file == INVALID_HANDLE_VALUE)
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
147 return FALSE;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
148
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
149 size = GetFileSize (file, &upper_size);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
150 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
151 0, size, NULL);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
152 if (!file_mapping)
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
153 return FALSE;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
154
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
155 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
156 if (file_base == 0)
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
157 return FALSE;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
158
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
159 p_file->name = filename;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
160 p_file->size = size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
161 p_file->file = file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
162 p_file->file_mapping = file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
163 p_file->file_base = file_base;
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
164
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
165 return TRUE;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
166 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
167
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
168 int
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
169 open_output_file (file_data *p_file, char *filename, unsigned long size)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
170 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
171 HANDLE file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
172 HANDLE file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
173 void *file_base;
13423
eefa4f720371 [HAVE_NTGUI] (WinMain): New procedure.
Geoff Voelker <voelker@cs.washington.edu>
parents: 12454
diff changeset
174
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
175 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
176 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
177 if (file == INVALID_HANDLE_VALUE)
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
178 return FALSE;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
179
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
180 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
181 0, size, NULL);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
182 if (!file_mapping)
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
183 return FALSE;
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
184
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
185 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
186 if (file_base == 0)
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
187 return FALSE;
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
188
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
189 p_file->name = filename;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
190 p_file->size = size;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
191 p_file->file = file;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
192 p_file->file_mapping = file_mapping;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
193 p_file->file_base = file_base;
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
194
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
195 return TRUE;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
196 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
197
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
198 /* Close the system structures associated with the given file. */
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
199 void
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
200 close_file_data (file_data *p_file)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
201 {
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
202 UnmapViewOfFile (p_file->file_base);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
203 CloseHandle (p_file->file_mapping);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
204 /* For the case of output files, set final size. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
205 SetFilePointer (p_file->file, p_file->size, NULL, FILE_BEGIN);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
206 SetEndOfFile (p_file->file);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
207 CloseHandle (p_file->file);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
208 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
209
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
210
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
211 /* Routines to manipulate NT executable file sections. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
212
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
213 /* Return pointer to section header for named section. */
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
214 IMAGE_SECTION_HEADER *
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
215 find_section (char * name, IMAGE_NT_HEADERS * nt_header)
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
216 {
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
217 PIMAGE_SECTION_HEADER section;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
218 int i;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
219
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
220 section = IMAGE_FIRST_SECTION (nt_header);
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
221
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
222 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
223 {
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
224 if (strcmp (section->Name, name) == 0)
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
225 return section;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
226 section++;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
227 }
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
228 return NULL;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
229 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
230
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
231 /* Return pointer to section header for section containing the given
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
232 relative virtual address. */
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
233 IMAGE_SECTION_HEADER *
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
234 rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header)
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
235 {
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
236 PIMAGE_SECTION_HEADER section;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
237 int i;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
238
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
239 section = IMAGE_FIRST_SECTION (nt_header);
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
240
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
241 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
242 {
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
243 /* Some linkers (eg. the NT SDK linker I believe) swapped the
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
244 meaning of these two values - or rather, they ignored
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
245 VirtualSize entirely and always set it to zero. This affects
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
246 some very old exes (eg. gzip dated Dec 1993). Since
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
247 w32_executable_type relies on this function to work reliably,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
248 we need to cope with this. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
249 DWORD real_size = max (section->SizeOfRawData,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
250 section->Misc.VirtualSize);
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
251 if (rva >= section->VirtualAddress
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
252 && rva < section->VirtualAddress + real_size)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
253 return section;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
254 section++;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
255 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
256 return NULL;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
257 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
258
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
259 /* Return pointer to section header for section containing the given
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
260 offset in its raw data area. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
261 IMAGE_SECTION_HEADER *
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
262 offset_to_section (DWORD offset, IMAGE_NT_HEADERS * nt_header)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
263 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
264 PIMAGE_SECTION_HEADER section;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
265 int i;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
266
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
267 section = IMAGE_FIRST_SECTION (nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
268
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
269 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
270 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
271 if (offset >= section->PointerToRawData
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
272 && offset < section->PointerToRawData + section->SizeOfRawData)
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
273 return section;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
274 section++;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
275 }
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
276 return NULL;
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
277 }
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
278
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
279 /* Return offset to an object in dst, given offset in src. We assume
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
280 there is at least one section in both src and dst images, and that
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
281 the some sections may have been added to dst (after sections in src). */
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
282 DWORD
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
283 relocate_offset (DWORD offset,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
284 IMAGE_NT_HEADERS * src_nt_header,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
285 IMAGE_NT_HEADERS * dst_nt_header)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
286 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
287 PIMAGE_SECTION_HEADER src_section = IMAGE_FIRST_SECTION (src_nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
288 PIMAGE_SECTION_HEADER dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
289 int i = 0;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
290
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
291 while (offset >= src_section->PointerToRawData)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
292 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
293 if (offset < src_section->PointerToRawData + src_section->SizeOfRawData)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
294 break;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
295 i++;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
296 if (i == src_nt_header->FileHeader.NumberOfSections)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
297 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
298 /* Handle offsets after the last section. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
299 dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
300 dst_section += dst_nt_header->FileHeader.NumberOfSections - 1;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
301 while (dst_section->PointerToRawData == 0)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
302 dst_section--;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
303 while (src_section->PointerToRawData == 0)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
304 src_section--;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
305 return offset
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
306 + (dst_section->PointerToRawData + dst_section->SizeOfRawData)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
307 - (src_section->PointerToRawData + src_section->SizeOfRawData);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
308 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
309 src_section++;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
310 dst_section++;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
311 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
312 return offset +
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
313 (dst_section->PointerToRawData - src_section->PointerToRawData);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
314 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
315
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
316 #define OFFSET_TO_RVA(offset, section) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
317 (section->VirtualAddress + ((DWORD)(offset) - section->PointerToRawData))
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
318
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
319 #define RVA_TO_OFFSET(rva, section) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
320 (section->PointerToRawData + ((DWORD)(rva) - section->VirtualAddress))
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
321
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
322 #define RVA_TO_SECTION_OFFSET(rva, section) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
323 ((DWORD)(rva) - section->VirtualAddress)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
324
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
325 /* Convert address in executing image to RVA. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
326 #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL))
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
327
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
328 #define PTR_TO_OFFSET(ptr, pfile_data) \
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
329 ((unsigned char *)(ptr) - (pfile_data)->file_base)
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
330
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
331 #define OFFSET_TO_PTR(offset, pfile_data) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
332 ((pfile_data)->file_base + (DWORD)(offset))
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
333
19703
9fa57c73882d Include config.h and time.h.
Geoff Voelker <voelker@cs.washington.edu>
parents: 18506
diff changeset
334
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
335 /* Flip through the executable and cache the info necessary for dumping. */
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
336 void
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
337 get_section_info (file_data *p_infile)
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
338 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
339 PIMAGE_DOS_HEADER dos_header;
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
340 PIMAGE_NT_HEADERS nt_header;
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
341 PIMAGE_SECTION_HEADER section;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
342 int overlap;
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
343
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
344 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
345 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
346 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
347 printf ("Unknown EXE header in %s...bailing.\n", p_infile->name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
348 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
349 }
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
350 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
351 dos_header->e_lfanew);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
352 if (nt_header == NULL)
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
353 {
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
354 printf ("Failed to find IMAGE_NT_HEADER in %s...bailing.\n",
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
355 p_infile->name);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
356 exit (1);
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
357 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
358
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
359 /* Check the NT header signature ... */
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
360 if (nt_header->Signature != IMAGE_NT_SIGNATURE)
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
361 {
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
362 printf ("Invalid IMAGE_NT_SIGNATURE 0x%x in %s...bailing.\n",
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
363 nt_header->Signature, p_infile->name);
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
364 exit (1);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
365 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
366
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
367 /* Locate the ".data" and ".bss" sections for Emacs. (Note that the
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
368 actual section names are probably different from these, and might
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
369 actually be the same section.)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
370
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
371 We do this as follows: first we determine the virtual address
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
372 ranges in this process for the data and bss variables that we wish
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
373 to preserve. Then we map these VAs to the section entries in the
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
374 source image. Finally, we determine the new size of the raw data
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
375 area for the bss section, so we can make the new image the correct
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
376 size. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
377
24669
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
378 /* We arrange for the Emacs initialized data to be in a separate
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
379 section if possible, because we cannot rely on my_begdata and
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
380 my_edata marking out the full extent of the initialized data, at
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
381 least on the Alpha where the linker freely reorders variables
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
382 across libraries. If we can arrange for this, all we need to do is
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
383 find the start and size of the EMDATA section. */
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
384 data_section = find_section ("EMDATA", nt_header);
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
385 if (data_section)
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
386 {
24669
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
387 data_start = (char *) nt_header->OptionalHeader.ImageBase +
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
388 data_section->VirtualAddress;
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
389 data_size = data_section->Misc.VirtualSize;
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
390 }
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
391 else
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
392 {
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
393 /* Fallback on the old method if compiler doesn't support the
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
394 data_set #pragma (or its equivalent). */
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
395 data_start = my_begdata;
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
396 data_size = my_edata - my_begdata;
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
397 data_section = rva_to_section (PTR_TO_RVA (my_begdata), nt_header);
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
398 if (data_section != rva_to_section (PTR_TO_RVA (my_edata), nt_header))
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
399 {
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
400 printf ("Initialized data is not in a single section...bailing\n");
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
401 exit (1);
66df82aa955e (get_section_info): Dump back the entire EMDATA
Andrew Innes <andrewi@gnu.org>
parents: 24237
diff changeset
402 }
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
403 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
404
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
405 /* As noted in lastfile.c, the Alpha (but not the Intel) MSVC linker
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
406 globally segregates all static and public bss data (ie. across all
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
407 linked modules, not just per module), so we must take both static
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
408 and public bss areas into account to determine the true extent of
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
409 the bss area used by Emacs.
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
410
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
411 To be strictly correct, we dump the static and public bss areas
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
412 used by Emacs separately if non-overlapping (since otherwise we are
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
413 dumping bss data belonging to system libraries, eg. the static bss
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
414 system data on the Alpha). */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
415
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
416 bss_start = my_begbss;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
417 bss_size = my_endbss - my_begbss;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
418 bss_section = rva_to_section (PTR_TO_RVA (my_begbss), nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
419 if (bss_section != rva_to_section (PTR_TO_RVA (my_endbss), nt_header))
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
420 {
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
421 printf ("Uninitialized data is not in a single section...bailing\n");
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
422 exit (1);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
423 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
424 /* Compute how much the .bss section's raw data will grow. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
425 extra_bss_size =
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
426 ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss), bss_section),
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
427 nt_header->OptionalHeader.FileAlignment)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
428 - bss_section->SizeOfRawData;
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
429
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
430 bss_start_static = my_begbss_static;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
431 bss_size_static = my_endbss_static - my_begbss_static;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
432 bss_section_static = rva_to_section (PTR_TO_RVA (my_begbss_static), nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
433 if (bss_section_static != rva_to_section (PTR_TO_RVA (my_endbss_static), nt_header))
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
434 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
435 printf ("Uninitialized static data is not in a single section...bailing\n");
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
436 exit (1);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
437 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
438 /* Compute how much the static .bss section's raw data will grow. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
439 extra_bss_size_static =
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
440 ROUND_UP (RVA_TO_SECTION_OFFSET (PTR_TO_RVA (my_endbss_static), bss_section_static),
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
441 nt_header->OptionalHeader.FileAlignment)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
442 - bss_section_static->SizeOfRawData;
12454
3f4da17a7cd8 (get_section_info): Set the end of the data region
Geoff Voelker <voelker@cs.washington.edu>
parents: 12245
diff changeset
443
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
444 /* Combine the bss sections into one if they overlap. */
24806
a67730c51617 (get_section_info) [_ALPHA_]: Force as much bss data
Andrew Innes <andrewi@gnu.org>
parents: 24669
diff changeset
445 #ifdef _ALPHA_
a67730c51617 (get_section_info) [_ALPHA_]: Force as much bss data
Andrew Innes <andrewi@gnu.org>
parents: 24669
diff changeset
446 overlap = 1; /* force all bss data to be dumped */
a67730c51617 (get_section_info) [_ALPHA_]: Force as much bss data
Andrew Innes <andrewi@gnu.org>
parents: 24669
diff changeset
447 #else
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
448 overlap = 0;
24806
a67730c51617 (get_section_info) [_ALPHA_]: Force as much bss data
Andrew Innes <andrewi@gnu.org>
parents: 24669
diff changeset
449 #endif
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
450 if (bss_start < bss_start_static)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
451 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
452 if (bss_start_static < bss_start + bss_size)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
453 overlap = 1;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
454 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
455 else
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
456 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
457 if (bss_start < bss_start_static + bss_size_static)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
458 overlap = 1;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
459 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
460 if (overlap)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
461 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
462 if (bss_section != bss_section_static)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
463 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
464 printf ("BSS data not in a single section...bailing\n");
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
465 exit (1);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
466 }
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
467 bss_start = min (bss_start, bss_start_static);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
468 bss_size = max (my_endbss, my_endbss_static) - bss_start;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
469 bss_section_static = 0;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
470 extra_bss_size_static = 0;
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
471 }
13830
8d30151f4acb (get_bss_info_from_map_file): New function.
Geoff Voelker <voelker@cs.washington.edu>
parents: 13423
diff changeset
472
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
473 heap_section = rva_to_section (PTR_TO_RVA (get_heap_start ()), nt_header);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
474 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
475
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
476
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
477 /* The dump routines. */
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
478
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
479 void
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
480 copy_executable_and_dump_data (file_data *p_infile,
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
481 file_data *p_outfile)
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
482 {
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
483 unsigned char *dst, *dst_save;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
484 PIMAGE_DOS_HEADER dos_header;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
485 PIMAGE_NT_HEADERS nt_header;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
486 PIMAGE_NT_HEADERS dst_nt_header;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
487 PIMAGE_SECTION_HEADER section;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
488 PIMAGE_SECTION_HEADER dst_section;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
489 DWORD offset;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
490 int i;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
491
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
492 #define COPY_CHUNK(message, src, size) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
493 do { \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
494 unsigned char *s = (void *)(src); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
495 unsigned long count = (size); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
496 printf ("%s\n", (message)); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
497 printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
498 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
499 printf ("\t0x%08x Size in bytes.\n", count); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
500 memcpy (dst, s, count); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
501 dst += count; \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
502 } while (0)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
503
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
504 #define COPY_PROC_CHUNK(message, src, size) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
505 do { \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
506 unsigned char *s = (void *)(src); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
507 unsigned long count = (size); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
508 printf ("%s\n", (message)); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
509 printf ("\t0x%08x Address in process.\n", s); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
510 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
511 printf ("\t0x%08x Size in bytes.\n", count); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
512 memcpy (dst, s, count); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
513 dst += count; \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
514 } while (0)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
515
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
516 #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
517 #define ROUND_UP_DST(align) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
518 (dst = p_outfile->file_base + ROUND_UP (DST_TO_OFFSET (), (align)))
24237
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
519 #define ROUND_UP_DST_AND_ZERO(align) \
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
520 do { \
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
521 unsigned char *newdst = p_outfile->file_base \
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
522 + ROUND_UP (DST_TO_OFFSET (), (align)); \
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
523 /* Zero the alignment slop; it may actually initialize real data. */ \
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
524 memset (dst, 0, newdst - dst); \
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
525 dst = newdst; \
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
526 } while (0)
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
527
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
528 /* Copy the source image sequentially, ie. section by section after
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
529 copying the headers and section table, to simplify the process of
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
530 dumping the raw data for the bss and heap sections.
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
531
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
532 Note that dst is updated implicitly by each COPY_CHUNK. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
533
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
534 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
535 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
536 dos_header->e_lfanew);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
537 section = IMAGE_FIRST_SECTION (nt_header);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
538
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
539 dst = (unsigned char *) p_outfile->file_base;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
540
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
541 COPY_CHUNK ("Copying DOS header...", dos_header,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
542 (DWORD) nt_header - (DWORD) dos_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
543 dst_nt_header = (PIMAGE_NT_HEADERS) dst;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
544 COPY_CHUNK ("Copying NT header...", nt_header,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
545 (DWORD) section - (DWORD) nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
546 dst_section = (PIMAGE_SECTION_HEADER) dst;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
547 COPY_CHUNK ("Copying section table...", section,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
548 nt_header->FileHeader.NumberOfSections * sizeof (*section));
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
549
24237
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
550 /* Align the first section's raw data area, and set the header size
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
551 field accordingly. */
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
552 ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
553 dst_nt_header->OptionalHeader.SizeOfHeaders = DST_TO_OFFSET ();
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
554
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
555 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
556 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
557 char msg[100];
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
558 sprintf (msg, "Copying raw data for %s...", section->Name);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
559
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
560 dst_save = dst;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
561
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
562 /* Update the file-relative offset for this section's raw data (if
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
563 it has any) in case things have been relocated; we will update
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
564 the other offsets below once we know where everything is. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
565 if (dst_section->PointerToRawData)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
566 dst_section->PointerToRawData = DST_TO_OFFSET ();
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
567
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
568 /* Can always copy the original raw data. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
569 COPY_CHUNK
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
570 (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile),
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
571 section->SizeOfRawData);
24237
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
572 /* Ensure alignment slop is zeroed. */
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
573 ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
574
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
575 /* Note that various sections below may be aliases. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
576 if (section == data_section)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
577 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
578 dst = dst_save
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
579 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (data_start), dst_section);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
580 COPY_PROC_CHUNK ("Dumping initialized data...", data_start, data_size);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
581 dst = dst_save + dst_section->SizeOfRawData;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
582 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
583 if (section == bss_section)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
584 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
585 /* Dump contents of bss variables, adjusting the section's raw
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
586 data size as necessary. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
587 dst = dst_save
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
588 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start), dst_section);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
589 COPY_PROC_CHUNK ("Dumping bss data...", bss_start, bss_size);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
590 ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
591 dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
592 /* Determine new size of raw data area. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
593 dst = max (dst, dst_save + dst_section->SizeOfRawData);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
594 dst_section->SizeOfRawData = dst - dst_save;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
595 dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
596 dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
597 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
598 if (section == bss_section_static)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
599 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
600 /* Dump contents of static bss variables, adjusting the
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
601 section's raw data size as necessary. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
602 dst = dst_save
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
603 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (bss_start_static), dst_section);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
604 COPY_PROC_CHUNK ("Dumping static bss data...", bss_start_static, bss_size_static);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
605 ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
606 dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
607 /* Determine new size of raw data area. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
608 dst = max (dst, dst_save + dst_section->SizeOfRawData);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
609 dst_section->SizeOfRawData = dst - dst_save;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
610 dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
611 dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
612 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
613 if (section == heap_section)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
614 {
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
615 DWORD heap_start = (DWORD) get_heap_start ();
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
616 DWORD heap_size = get_committed_heap_size ();
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
617
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
618 /* Dump the used portion of the predump heap, adjusting the
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
619 section's size to the appropriate size. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
620 dst = dst_save
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
621 + RVA_TO_SECTION_OFFSET (PTR_TO_RVA (heap_start), dst_section);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
622 COPY_PROC_CHUNK ("Dumping heap...", heap_start, heap_size);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
623 ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
624 dst_section->PointerToRawData = PTR_TO_OFFSET (dst_save, p_outfile);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
625 /* Determine new size of raw data area. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
626 dst = max (dst, dst_save + dst_section->SizeOfRawData);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
627 dst_section->SizeOfRawData = dst - dst_save;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
628 /* Reduce the size of the heap section to fit (must be last
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
629 section). */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
630 dst_nt_header->OptionalHeader.SizeOfImage -=
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
631 dst_section->Misc.VirtualSize
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
632 - ROUND_UP (dst_section->SizeOfRawData,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
633 dst_nt_header->OptionalHeader.SectionAlignment);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
634 dst_section->Misc.VirtualSize =
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
635 ROUND_UP (dst_section->SizeOfRawData,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
636 dst_nt_header->OptionalHeader.SectionAlignment);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
637 dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
638 dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
639 }
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
640
24237
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
641 /* Align the section's raw data area. */
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
642 ROUND_UP_DST (dst_nt_header->OptionalHeader.FileAlignment);
16e5582ed876 (ROUND_UP_DST_AND_ZERO): New macro.
Andrew Innes <andrewi@gnu.org>
parents: 24102
diff changeset
643
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
644 section++;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
645 dst_section++;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
646 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
647
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
648 /* Copy remainder of source image. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
649 do
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
650 section--;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
651 while (section->PointerToRawData == 0);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
652 offset = ROUND_UP (section->PointerToRawData + section->SizeOfRawData,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
653 nt_header->OptionalHeader.FileAlignment);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
654 COPY_CHUNK
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
655 ("Copying remainder of executable...",
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
656 OFFSET_TO_PTR (offset, p_infile),
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
657 p_infile->size - offset);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
658
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
659 /* Final size for new image. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
660 p_outfile->size = DST_TO_OFFSET ();
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
661
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
662 /* Now patch up remaining file-relative offsets. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
663 section = IMAGE_FIRST_SECTION (nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
664 dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
665
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
666 #define ADJUST_OFFSET(var) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
667 do { \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
668 if ((var) != 0) \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
669 (var) = relocate_offset ((var), nt_header, dst_nt_header); \
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
670 } while (0)
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
671
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
672 dst_nt_header->OptionalHeader.SizeOfInitializedData = 0;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
673 dst_nt_header->OptionalHeader.SizeOfUninitializedData = 0;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
674 for (i = 0; i < dst_nt_header->FileHeader.NumberOfSections; i++)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
675 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
676 /* Recompute data sizes for completeness. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
677 if (dst_section[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
678 dst_nt_header->OptionalHeader.SizeOfInitializedData +=
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
679 ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
680 else if (dst_section[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
681 dst_nt_header->OptionalHeader.SizeOfUninitializedData +=
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
682 ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
683
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
684 ADJUST_OFFSET (dst_section[i].PointerToLinenumbers);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
685 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
686
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
687 ADJUST_OFFSET (dst_nt_header->FileHeader.PointerToSymbolTable);
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
688
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
689 /* Update offsets in debug directory entries. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
690 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
691 IMAGE_DATA_DIRECTORY debug_dir =
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
692 dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
693 PIMAGE_DEBUG_DIRECTORY debug_entry;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
694
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
695 section = rva_to_section (debug_dir.VirtualAddress, dst_nt_header);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
696 if (section)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
697 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
698 debug_entry = (PIMAGE_DEBUG_DIRECTORY)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
699 (RVA_TO_OFFSET (debug_dir.VirtualAddress, section) + p_outfile->file_base);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
700 debug_dir.Size /= sizeof (IMAGE_DEBUG_DIRECTORY);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
701
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
702 for (i = 0; i < debug_dir.Size; i++, debug_entry++)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
703 ADJUST_OFFSET (debug_entry->PointerToRawData);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
704 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
705 }
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
706 }
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
707
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
708
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
709 /* Dump out .data and .bss sections into a new executable. */
21604
104277bc1444 (my_begbss_static, my_endbss_static): Declare.
Geoff Voelker <voelker@cs.washington.edu>
parents: 21456
diff changeset
710 void
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
711 unexec (char *new_name, char *old_name, void *start_data, void *start_bss,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
712 void *entry_address)
21604
104277bc1444 (my_begbss_static, my_endbss_static): Declare.
Geoff Voelker <voelker@cs.washington.edu>
parents: 21456
diff changeset
713 {
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
714 file_data in_file, out_file;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
715 char out_filename[MAX_PATH], in_filename[MAX_PATH];
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
716 unsigned long size;
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
717 char *p;
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
718 char *q;
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
719
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
720 /* Ignore old_name, and get our actual location from the OS. */
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
721 if (!GetModuleFileName (NULL, in_filename, MAX_PATH))
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
722 abort ();
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
723 dostounix_filename (in_filename);
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
724 strcpy (out_filename, in_filename);
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
725
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
726 /* Change the base of the output filename to match the requested name. */
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
727 if ((p = strrchr (out_filename, '/')) == NULL)
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
728 abort ();
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
729 /* The filenames have already been expanded, and will be in Unix
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
730 format, so it is safe to expect an absolute name. */
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
731 if ((q = strrchr (new_name, '/')) == NULL)
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
732 abort ();
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
733 strcpy (p, q);
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
734
31104
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
735 /* Make sure that the output filename has the ".exe" extension...patch
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
736 it up if not. */
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
737 p = out_filename + strlen (out_filename) - 4;
79c890cce460 Change PUCHAR to PCHAR.
Andrew Innes <andrewi@gnu.org>
parents: 24806
diff changeset
738 if (strcmp (p, ".exe"))
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
739 strcat (out_filename, ".exe");
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
740
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
741 printf ("Dumping from %s\n", in_filename);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
742 printf (" to %s\n", out_filename);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
743
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
744 /* We need to round off our heap to NT's page size. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
745 round_heap (get_page_size ());
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
746
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
747 /* Open the undumped executable file. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
748 if (!open_input_file (&in_file, in_filename))
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
749 {
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
750 printf ("Failed to open %s (%d)...bailing.\n",
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
751 in_filename, GetLastError ());
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
752 exit (1);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
753 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
754
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
755 /* Get the interesting section info, like start and size of .bss... */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
756 get_section_info (&in_file);
21604
104277bc1444 (my_begbss_static, my_endbss_static): Declare.
Geoff Voelker <voelker@cs.washington.edu>
parents: 21456
diff changeset
757
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
758 /* The size of the dumped executable is the size of the original
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
759 executable plus the size of the heap and the size of the .bss section. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
760 size = in_file.size +
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
761 get_committed_heap_size () +
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
762 extra_bss_size +
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
763 extra_bss_size_static;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
764 if (!open_output_file (&out_file, out_filename, size))
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
765 {
49600
23a1cea22d13 Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 31104
diff changeset
766 printf ("Failed to open %s (%d)...bailing.\n",
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
767 out_filename, GetLastError ());
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
768 exit (1);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
769 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
770
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
771 /* Set the flag (before dumping). */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
772 using_dynamic_heap = TRUE;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
773
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
774 copy_executable_and_dump_data (&in_file, &out_file);
21604
104277bc1444 (my_begbss_static, my_endbss_static): Declare.
Geoff Voelker <voelker@cs.washington.edu>
parents: 21456
diff changeset
775
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
776 /* Patch up header fields; profiler is picky about this. */
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
777 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
778 PIMAGE_DOS_HEADER dos_header;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
779 PIMAGE_NT_HEADERS nt_header;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
780 HANDLE hImagehelp = LoadLibrary ("imagehlp.dll");
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
781 DWORD headersum;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
782 DWORD checksum;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
783
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
784 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
785 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
786
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
787 nt_header->OptionalHeader.CheckSum = 0;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
788 // nt_header->FileHeader.TimeDateStamp = time (NULL);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
789 // dos_header->e_cp = size / 512;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
790 // nt_header->OptionalHeader.SizeOfImage = size;
21604
104277bc1444 (my_begbss_static, my_endbss_static): Declare.
Geoff Voelker <voelker@cs.washington.edu>
parents: 21456
diff changeset
791
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
792 pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile");
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
793 if (pfnCheckSumMappedFile)
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
794 {
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
795 // nt_header->FileHeader.TimeDateStamp = time (NULL);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
796 pfnCheckSumMappedFile (out_file.file_base,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
797 out_file.size,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
798 &headersum,
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
799 &checksum);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
800 nt_header->OptionalHeader.CheckSum = checksum;
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
801 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
802 FreeLibrary (hImagehelp);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
803 }
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
804
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
805 close_file_data (&in_file);
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
806 close_file_data (&out_file);
21604
104277bc1444 (my_begbss_static, my_endbss_static): Declare.
Geoff Voelker <voelker@cs.washington.edu>
parents: 21456
diff changeset
807 }
12245
e8a6dfd8d5d2 Initial revision
Karl Heuer <kwzh@gnu.org>
parents:
diff changeset
808
24102
1086aa5db591 Major rewrite to support cleaner method of dumping; a
Andrew Innes <andrewi@gnu.org>
parents: 23948
diff changeset
809 /* eof */