annotate nt/preprep.c @ 29330:58805fa6696f

*** empty log message ***
author Dave Love <fx@gnu.org>
date Wed, 31 May 2000 17:41:51 +0000
parents eb5e287ff675
children 24c78d0ab986
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24363
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
1 /* Pro-process emacs.exe for profiling by MSVC.
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
2 Copyright (C) 1999 Free Software Foundation, Inc.
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
3
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
4 This file is part of GNU Emacs.
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
5
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
6 GNU Emacs is free software; you can redistribute it and/or modify
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
9 any later version.
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
10
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
11 GNU Emacs is distributed in the hope that it will be useful,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
14 GNU General Public License for more details.
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
15
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
17 along with GNU Emacs; see the file COPYING. If not, write to
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
19 Boston, MA 02111-1307, USA.
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
20
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
21 Andrew Innes <andrewi@harlequin.co.uk> 16-Jan-1999
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
22 based on code from addsection.c
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
23 */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
24
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
25 #include <stdlib.h>
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
26 #include <stdio.h>
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
27 #include <fcntl.h>
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
28 #include <time.h>
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
29 #include <windows.h>
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
30
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
31 /* Include relevant definitions from IMAGEHLP.H, which can be found
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
32 in \\win32sdk\mstools\samples\image\include\imagehlp.h. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
33
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
34 PIMAGE_NT_HEADERS
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
35 (__stdcall * pfnCheckSumMappedFile) (LPVOID BaseAddress,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
36 DWORD FileLength,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
37 LPDWORD HeaderSum,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
38 LPDWORD CheckSum);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
39
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
40 #undef min
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
41 #undef max
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
42 #define min(x, y) (((x) < (y)) ? (x) : (y))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
43 #define max(x, y) (((x) > (y)) ? (x) : (y))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
44
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
45
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
46 /* File handling. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
47
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
48 typedef struct file_data {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
49 char *name;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
50 unsigned long size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
51 HANDLE file;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
52 HANDLE file_mapping;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
53 unsigned char *file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
54 } file_data;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
55
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
56 int
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
57 open_input_file (file_data *p_file, char *filename)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
58 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
59 HANDLE file;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
60 HANDLE file_mapping;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
61 void *file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
62 unsigned long size, upper_size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
63
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
64 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
65 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
66 if (file == INVALID_HANDLE_VALUE)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
67 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
68
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
69 size = GetFileSize (file, &upper_size);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
70 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
71 0, size, NULL);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
72 if (!file_mapping)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
73 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
74
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
75 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
76 if (file_base == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
77 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
78
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
79 p_file->name = filename;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
80 p_file->size = size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
81 p_file->file = file;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
82 p_file->file_mapping = file_mapping;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
83 p_file->file_base = file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
84
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
85 return TRUE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
86 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
87
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
88 int
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
89 open_output_file (file_data *p_file, char *filename, unsigned long size)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
90 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
91 HANDLE file;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
92 HANDLE file_mapping;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
93 void *file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
94
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
95 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
96 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
97 if (file == INVALID_HANDLE_VALUE)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
98 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
99
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
100 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
101 0, size, NULL);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
102 if (!file_mapping)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
103 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
104
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
105 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
106 if (file_base == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
107 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
108
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
109 p_file->name = filename;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
110 p_file->size = size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
111 p_file->file = file;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
112 p_file->file_mapping = file_mapping;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
113 p_file->file_base = file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
114
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
115 return TRUE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
116 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
117
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
118 int
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
119 open_inout_file (file_data *p_file, char *filename)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
120 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
121 HANDLE file;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
122 HANDLE file_mapping;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
123 void *file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
124 unsigned long size, upper_size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
125
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
126 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
127 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
128 if (file == INVALID_HANDLE_VALUE)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
129 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
130
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
131 size = GetFileSize (file, &upper_size);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
132 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
133 0, size, NULL);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
134 if (!file_mapping)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
135 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
136
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
137 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
138 if (file_base == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
139 return FALSE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
140
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
141 p_file->name = filename;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
142 p_file->size = size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
143 p_file->file = file;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
144 p_file->file_mapping = file_mapping;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
145 p_file->file_base = file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
146
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
147 return TRUE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
148 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
149
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
150 /* Close the system structures associated with the given file. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
151 void
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
152 close_file_data (file_data *p_file)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
153 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
154 UnmapViewOfFile (p_file->file_base);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
155 CloseHandle (p_file->file_mapping);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
156 /* For the case of output files, set final size. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
157 SetFilePointer (p_file->file, p_file->size, NULL, FILE_BEGIN);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
158 SetEndOfFile (p_file->file);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
159 CloseHandle (p_file->file);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
160 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
161
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
162
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
163 /* Routines to manipulate NT executable file sections. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
164
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
165 unsigned long
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
166 get_unrounded_section_size (PIMAGE_SECTION_HEADER p_section)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
167 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
168 /* The true section size, before rounding, for an initialized data or
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
169 code section. (Supposedly some linkers swap the meaning of these
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
170 two values.) */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
171 return min (p_section->SizeOfRawData,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
172 p_section->Misc.VirtualSize);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
173 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
174
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
175 /* Return pointer to section header for named section. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
176 IMAGE_SECTION_HEADER *
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
177 find_section (char * name, IMAGE_NT_HEADERS * nt_header)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
178 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
179 PIMAGE_SECTION_HEADER section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
180 int i;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
181
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
182 section = IMAGE_FIRST_SECTION (nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
183
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
184 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
185 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
186 if (strcmp (section->Name, name) == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
187 return section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
188 section++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
189 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
190 return NULL;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
191 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
192
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
193 /* Return pointer to section header for section containing the given
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
194 relative virtual address. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
195 IMAGE_SECTION_HEADER *
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
196 rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
197 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
198 PIMAGE_SECTION_HEADER section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
199 int i;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
200
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
201 section = IMAGE_FIRST_SECTION (nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
202
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
203 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
204 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
205 /* Some linkers (eg. the NT SDK linker I believe) swapped the
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
206 meaning of these two values - or rather, they ignored
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
207 VirtualSize entirely and always set it to zero. This affects
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
208 some very old exes (eg. gzip dated Dec 1993). Since
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
209 w32_executable_type relies on this function to work reliably,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
210 we need to cope with this. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
211 DWORD real_size = max (section->SizeOfRawData,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
212 section->Misc.VirtualSize);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
213 if (rva >= section->VirtualAddress
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
214 && rva < section->VirtualAddress + real_size)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
215 return section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
216 section++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
217 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
218 return NULL;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
219 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
220
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
221 /* Return pointer to section header for section containing the given
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
222 offset in its raw data area. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
223 IMAGE_SECTION_HEADER *
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
224 offset_to_section (DWORD offset, IMAGE_NT_HEADERS * nt_header)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
225 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
226 PIMAGE_SECTION_HEADER section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
227 int i;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
228
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
229 section = IMAGE_FIRST_SECTION (nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
230
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
231 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
232 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
233 if (offset >= section->PointerToRawData
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
234 && offset < section->PointerToRawData + section->SizeOfRawData)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
235 return section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
236 section++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
237 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
238 return NULL;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
239 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
240
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
241 /* Return offset to an object in dst, given offset in src. We assume
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
242 there is at least one section in both src and dst images, and that
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
243 the some sections may have been added to dst (after sections in src). */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
244 static DWORD
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
245 relocate_offset (DWORD offset,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
246 IMAGE_NT_HEADERS * src_nt_header,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
247 IMAGE_NT_HEADERS * dst_nt_header)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
248 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
249 PIMAGE_SECTION_HEADER src_section = IMAGE_FIRST_SECTION (src_nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
250 PIMAGE_SECTION_HEADER dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
251 int i = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
252
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
253 while (offset >= src_section->PointerToRawData)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
254 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
255 if (offset < src_section->PointerToRawData + src_section->SizeOfRawData)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
256 break;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
257 i++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
258 if (i == src_nt_header->FileHeader.NumberOfSections)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
259 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
260 /* Handle offsets after the last section. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
261 dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
262 dst_section += dst_nt_header->FileHeader.NumberOfSections - 1;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
263 while (dst_section->PointerToRawData == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
264 dst_section--;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
265 while (src_section->PointerToRawData == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
266 src_section--;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
267 return offset
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
268 + (dst_section->PointerToRawData + dst_section->SizeOfRawData)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
269 - (src_section->PointerToRawData + src_section->SizeOfRawData);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
270 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
271 src_section++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
272 dst_section++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
273 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
274 return offset +
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
275 (dst_section->PointerToRawData - src_section->PointerToRawData);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
276 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
277
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
278 #define OFFSET_TO_RVA(offset, section) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
279 (section->VirtualAddress + ((DWORD)(offset) - section->PointerToRawData))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
280
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
281 #define RVA_TO_OFFSET(rva, section) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
282 (section->PointerToRawData + ((DWORD)(rva) - section->VirtualAddress))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
283
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
284 #define RVA_TO_SECTION_OFFSET(rva, section) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
285 ((DWORD)(rva) - section->VirtualAddress)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
286
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
287 #define RVA_TO_PTR(var,section,filedata) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
288 ((void *)(RVA_TO_OFFSET(var,section) + (filedata)->file_base))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
289
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
290 /* Convert address in executing image to RVA. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
291 #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
292
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
293 #define PTR_TO_OFFSET(ptr, pfile_data) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
294 ((char *)(ptr) - (pfile_data)->file_base)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
295
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
296 #define OFFSET_TO_PTR(offset, pfile_data) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
297 ((pfile_data)->file_base + (DWORD)(offset))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
298
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
299 #define ROUND_UP(p, align) (((DWORD)(p) + (align)-1) & ~((align)-1))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
300 #define ROUND_DOWN(p, align) ((DWORD)(p) & ~((align)-1))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
301
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
302
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
303 /* The MSVC prep program generates a ._xe file from .exe, where relevant
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
304 function calls etc have been patched to go through thunks (generated
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
305 by prep) that record timing/call information. Because the thunks
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
306 need to make references to functions imported from profile.dll, the
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
307 import table must be expanded; the end result is that all the
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
308 sections following .rdata are relocated to higher RVAs (add a final
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
309 code section is added holding all the thunks). The .reloc section is
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
310 also expanded, so that the thunks themselves are relocatable.
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
311
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
312 It is this relocation which kills emacs._xe, because the dumped heap
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
313 pointers aren't relocated, because there is no relocation data for
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
314 either the relevant global/static variables or the heap section
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
315 itself, both of which contain pointers into the heap. [Note that
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
316 static variables which aren't initialized during linking may become
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
317 initialized with heap pointers, or even pointers to other static
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
318 variables, because of dumping.]
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
319
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
320 We could potentially generate the relocation data ourselves by making
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
321 two versions of temacs, one with an extra dummmy section before
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
322 EMHEAP to offset it, and then compare the dumped executables from
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
323 both. That is a lot of work though, and it doesn't solve the problem
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
324 of dumped pointers to static variables, which also can be relocated.
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
325
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
326 A better solution is to pre-process emacs.exe so that the .rdata and
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
327 .reloc sections are moved to the end of the section table, and thus
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
328 prep won't relocate anything else. (Of course, we leave "dead"
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
329 copies of these two sections in place, so that the virtual address of
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
330 everything else is unaffected.) Relocating the .reloc data is
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
331 trivial - we just update the IMAGE_BASE_RELOCATION address in the
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
332 header (the data itself doesn't change). Relocating the import table
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
333 is more complicated though, because the calls to imported functions
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
334 must be patched up. That requires us to selectively apply the base
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
335 relocations when we encounter references to imported functions (or
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
336 variables) in other sections, but at least the base relocations are
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
337 easy to parse. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
338
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
339 static void
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
340 copy_executable_and_move_sections (file_data *p_infile,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
341 file_data *p_outfile)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
342 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
343 unsigned char *dst;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
344 PIMAGE_DOS_HEADER dos_header;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
345 PIMAGE_NT_HEADERS nt_header;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
346 PIMAGE_NT_HEADERS dst_nt_header;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
347 PIMAGE_SECTION_HEADER section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
348 PIMAGE_SECTION_HEADER dst_section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
349 PIMAGE_SECTION_HEADER import_section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
350 PIMAGE_SECTION_HEADER reloc_section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
351 PIMAGE_DATA_DIRECTORY import_dir;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
352 PIMAGE_DATA_DIRECTORY reloc_dir;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
353 DWORD import_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
354 DWORD reloc_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
355 DWORD offset;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
356 int i;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
357
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
358 #define COPY_CHUNK(message, src, size) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
359 do { \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
360 unsigned char *s = (void *)(src); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
361 unsigned long count = (size); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
362 printf ("%s\n", (message)); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
363 printf ("\t0x%08x Offset in input file.\n", s - p_infile->file_base); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
364 printf ("\t0x%08x Offset in output file.\n", dst - p_outfile->file_base); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
365 printf ("\t0x%08x Size in bytes.\n", count); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
366 memcpy (dst, s, count); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
367 dst += count; \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
368 } while (0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
369
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
370 #define DST_TO_OFFSET() PTR_TO_OFFSET (dst, p_outfile)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
371 #define ROUND_UP_DST_AND_ZERO(align) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
372 do { \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
373 unsigned char *newdst = p_outfile->file_base \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
374 + ROUND_UP (DST_TO_OFFSET (), (align)); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
375 /* Zero the alignment slop; it may actually initialize real data. */ \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
376 memset (dst, 0, newdst - dst); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
377 dst = newdst; \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
378 } while (0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
379
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
380 /* Copy the source image sequentially, ie. section by section after
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
381 copying the headers and section table, to simplify the process of
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
382 relocating the .rdata and .reloc section table entries (which might
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
383 force the raw section data to be relocated).
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
384
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
385 Note that dst is updated implicitly by each COPY_CHUNK. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
386
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
387 dos_header = (PIMAGE_DOS_HEADER) p_infile->file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
388 nt_header = (PIMAGE_NT_HEADERS) (((unsigned long) dos_header) +
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
389 dos_header->e_lfanew);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
390 section = IMAGE_FIRST_SECTION (nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
391
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
392 import_dir = &nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
393 import_section = rva_to_section (import_dir->VirtualAddress, nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
394
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
395 reloc_dir = &nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
396 reloc_section = rva_to_section (reloc_dir->VirtualAddress, nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
397 if (!reloc_section)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
398 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
399 printf ("No relocation data, cannot prepare for profile prepping.\n");
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
400 exit (1);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
401 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
402
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
403 dst = (unsigned char *) p_outfile->file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
404
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
405 COPY_CHUNK ("Copying DOS header...", dos_header,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
406 (DWORD) nt_header - (DWORD) dos_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
407 dst_nt_header = (PIMAGE_NT_HEADERS) dst;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
408 COPY_CHUNK ("Copying NT header...", nt_header,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
409 (DWORD) section - (DWORD) nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
410 dst_section = (PIMAGE_SECTION_HEADER) dst;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
411 COPY_CHUNK ("Copying section table...", section,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
412 nt_header->FileHeader.NumberOfSections * sizeof (*section));
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
413
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
414 /* Leave room for extra section table entries; filled in below. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
415 dst += 2 * sizeof (*section);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
416
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
417 /* Align the first section's raw data area, and set the header size
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
418 field accordingly. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
419 ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
420 dst_nt_header->OptionalHeader.SizeOfHeaders = DST_TO_OFFSET ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
421
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
422 for (i = 0; i < nt_header->FileHeader.NumberOfSections;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
423 i++, section++, dst_section++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
424 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
425 char msg[100];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
426 sprintf (msg, "Copying raw data for %s...", section->Name);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
427
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
428 /* "Blank out" the two sections being relocated. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
429 if (section == import_section || section == reloc_section)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
430 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
431 dst_section->Name[0] = 'X';
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
432 dst_section->Misc.VirtualSize =
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
433 ROUND_UP (dst_section->Misc.VirtualSize,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
434 dst_nt_header->OptionalHeader.SectionAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
435 dst_section->PointerToRawData = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
436 dst_section->SizeOfRawData = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
437 dst_section->Characteristics &= ~IMAGE_SCN_CNT_INITIALIZED_DATA;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
438 dst_section->Characteristics |= IMAGE_SCN_CNT_UNINITIALIZED_DATA;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
439 dst_section->Characteristics &= ~IMAGE_SCN_MEM_WRITE;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
440 continue;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
441 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
442
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
443 /* Update the file-relative offset for this section's raw data (if
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
444 it has any) in case things have been relocated; we will update
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
445 the other offsets below once we know where everything is. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
446 if (dst_section->PointerToRawData)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
447 dst_section->PointerToRawData = DST_TO_OFFSET ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
448
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
449 /* Copy the original raw data. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
450 COPY_CHUNK
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
451 (msg, OFFSET_TO_PTR (section->PointerToRawData, p_infile),
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
452 section->SizeOfRawData);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
453
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
454 /* Round up the raw data size to the new alignment. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
455 dst_section->SizeOfRawData =
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
456 ROUND_UP (dst_section->SizeOfRawData,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
457 dst_nt_header->OptionalHeader.FileAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
458
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
459 /* Align the next section's raw data area. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
460 ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
461 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
462
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
463 /* Add the extra section entries, copying the raw data we skipped
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
464 earlier. We'll patch up the data itself below. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
465 if (import_section != NULL)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
466 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
467 dst_nt_header->FileHeader.NumberOfSections++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
468 dst_nt_header->OptionalHeader.SizeOfImage +=
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
469 ROUND_UP (import_section->Misc.VirtualSize,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
470 dst_nt_header->OptionalHeader.SectionAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
471 *dst_section = *import_section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
472 dst_section->VirtualAddress =
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
473 dst_section[-1].VirtualAddress
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
474 + ROUND_UP (dst_section[-1].Misc.VirtualSize,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
475 dst_nt_header->OptionalHeader.SectionAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
476 dst_section->PointerToRawData = DST_TO_OFFSET ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
477 /* Remember delta applied to import section. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
478 import_delta_rva = dst_section->VirtualAddress - import_section->VirtualAddress;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
479 COPY_CHUNK
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
480 ("Relocating import directory",
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
481 OFFSET_TO_PTR (import_section->PointerToRawData, p_infile),
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
482 import_section->SizeOfRawData);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
483 ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
484 dst_section++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
485 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
486 if (reloc_section != NULL)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
487 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
488 dst_nt_header->FileHeader.NumberOfSections++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
489 dst_nt_header->OptionalHeader.SizeOfImage +=
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
490 ROUND_UP (reloc_section->Misc.VirtualSize,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
491 dst_nt_header->OptionalHeader.SectionAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
492 *dst_section = *reloc_section;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
493 dst_section->VirtualAddress =
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
494 dst_section[-1].VirtualAddress
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
495 + ROUND_UP (dst_section[-1].Misc.VirtualSize,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
496 dst_nt_header->OptionalHeader.SectionAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
497 dst_section->PointerToRawData = DST_TO_OFFSET ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
498 /* Remember delta applied to reloc section. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
499 reloc_delta_rva = dst_section->VirtualAddress - reloc_section->VirtualAddress;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
500 COPY_CHUNK
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
501 ("Relocating base relocations directory",
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
502 OFFSET_TO_PTR (reloc_section->PointerToRawData, p_infile),
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
503 reloc_section->SizeOfRawData);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
504 ROUND_UP_DST_AND_ZERO (dst_nt_header->OptionalHeader.FileAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
505 reloc_dir = &dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
506 reloc_dir->VirtualAddress += reloc_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
507 dst_section++;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
508 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
509
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
510 /* Copy remainder of source image. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
511 section--;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
512 offset = ROUND_UP (section->PointerToRawData + section->SizeOfRawData,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
513 nt_header->OptionalHeader.FileAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
514 COPY_CHUNK
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
515 ("Copying remainder of executable...",
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
516 OFFSET_TO_PTR (offset, p_infile),
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
517 p_infile->size - offset);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
518
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
519 /* Final size for new image. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
520 p_outfile->size = DST_TO_OFFSET ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
521
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
522 /* Now patch up remaining file-relative offsets. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
523 printf ("Patching up raw data offsets...\n");
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
524
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
525 section = IMAGE_FIRST_SECTION (nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
526 dst_section = IMAGE_FIRST_SECTION (dst_nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
527
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
528 #define ADJUST_OFFSET(var) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
529 do { \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
530 if ((var) != 0) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
531 (var) = relocate_offset ((var), nt_header, dst_nt_header); \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
532 } while (0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
533
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
534 #define ADJUST_IMPORT_RVA(var) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
535 do { \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
536 if ((var) != 0) \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
537 *((DWORD *)&(var)) += import_delta_rva; \
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
538 } while (0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
539
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
540 dst_nt_header->OptionalHeader.SizeOfInitializedData = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
541 dst_nt_header->OptionalHeader.SizeOfUninitializedData = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
542 for (i = 0; i < dst_nt_header->FileHeader.NumberOfSections; i++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
543 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
544 /* Recompute data sizes for completeness. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
545 if (dst_section[i].Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
546 dst_nt_header->OptionalHeader.SizeOfInitializedData +=
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
547 ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
548 else if (dst_section[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
549 dst_nt_header->OptionalHeader.SizeOfUninitializedData +=
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
550 ROUND_UP (dst_section[i].Misc.VirtualSize, dst_nt_header->OptionalHeader.FileAlignment);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
551
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
552 ADJUST_OFFSET (dst_section[i].PointerToLinenumbers);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
553 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
554
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
555 ADJUST_OFFSET (dst_nt_header->FileHeader.PointerToSymbolTable);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
556
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
557 /* Update offsets in debug directory entries. Note that the debug
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
558 directory may be in the same section as the import table, so its
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
559 RVA may need to be adjusted too. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
560 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
561 PIMAGE_DATA_DIRECTORY debug_dir =
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
562 &dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
563 PIMAGE_DEBUG_DIRECTORY debug_entry;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
564
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
565 /* Update debug_dir if part of import_section. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
566 if (rva_to_section (debug_dir->VirtualAddress, nt_header) == import_section)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
567 debug_dir->VirtualAddress += import_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
568
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
569 section = rva_to_section (debug_dir->VirtualAddress, dst_nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
570 if (section)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
571 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
572 int size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
573
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
574 debug_entry = RVA_TO_PTR (debug_dir->VirtualAddress, section, p_outfile);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
575 size = debug_dir->Size / sizeof (IMAGE_DEBUG_DIRECTORY);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
576
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
577 for (i = 0; i < size; i++, debug_entry++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
578 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
579 /* The debug data itself is normally not part of any
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
580 section, but stored after all the raw section data. So
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
581 let relocate_offset do the work. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
582 ADJUST_OFFSET (debug_entry->PointerToRawData);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
583 ADJUST_IMPORT_RVA (debug_entry->AddressOfRawData);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
584 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
585 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
586 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
587
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
588 /* Update RVAs in import directory entries. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
589 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
590 PIMAGE_IMPORT_DESCRIPTOR imports;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
591 PIMAGE_THUNK_DATA import_thunks;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
592
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
593 import_dir = &dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
594 import_dir->VirtualAddress += import_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
595
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
596 section = rva_to_section (import_dir->VirtualAddress, dst_nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
597 imports = RVA_TO_PTR (import_dir->VirtualAddress, section, p_outfile);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
598
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
599 for ( ; imports->Name != 0; imports++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
600 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
601 ADJUST_IMPORT_RVA (imports->OriginalFirstThunk);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
602 ADJUST_IMPORT_RVA (imports->FirstThunk);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
603 ADJUST_IMPORT_RVA (imports->Name);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
604
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
605 for (import_thunks = RVA_TO_PTR (imports->OriginalFirstThunk, section, p_outfile);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
606 import_thunks->u1.Function != 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
607 import_thunks++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
608 if ((import_thunks->u1.Ordinal >> 31) == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
609 ADJUST_IMPORT_RVA (import_thunks->u1.Ordinal);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
610
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
611 for (import_thunks = RVA_TO_PTR (imports->FirstThunk, section, p_outfile);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
612 import_thunks->u1.Function != 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
613 import_thunks++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
614 if ((import_thunks->u1.Ordinal >> 31) == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
615 ADJUST_IMPORT_RVA (import_thunks->u1.Ordinal);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
616 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
617
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
618 import_dir = &dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
619 import_dir->VirtualAddress += import_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
620 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
621
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
622 /* Fix up references to the import section. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
623 printf ("Applying fixups to import references...\n");
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
624
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
625 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
626 IMAGE_BASE_RELOCATION *relocs, *block, *start_block, *end_block;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
627 DWORD import_start = import_section->VirtualAddress + dst_nt_header->OptionalHeader.ImageBase;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
628 DWORD import_end = import_start + import_section->Misc.VirtualSize;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
629 DWORD len_import_relocs;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
630 DWORD len_remaining_relocs;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
631 int seen_high = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
632 WORD * high_word;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
633 void * holder;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
634
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
635 reloc_dir = &dst_nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
636 reloc_section = rva_to_section (reloc_dir->VirtualAddress, dst_nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
637 relocs = RVA_TO_PTR (reloc_dir->VirtualAddress, reloc_section, p_outfile);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
638
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
639 /* Move the base relocations for the import section, if there are
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
640 any; the profiler needs to be able to patch RVAs in the import
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
641 section itself. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
642 for (block = relocs, start_block = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
643 (DWORD) block - (DWORD) relocs < reloc_dir->Size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
644 block = (void *)((DWORD) block + block->SizeOfBlock))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
645 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
646 if (block->VirtualAddress >= import_section->VirtualAddress + import_section->Misc.VirtualSize)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
647 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
648 end_block = block;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
649 break;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
650 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
651 if (block->VirtualAddress >= import_section->VirtualAddress)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
652 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
653 if (start_block == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
654 start_block = block;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
655 block->VirtualAddress += import_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
656 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
657 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
658 if (start_block)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
659 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
660 len_import_relocs = (DWORD) end_block - (DWORD) start_block;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
661 len_remaining_relocs = (DWORD) relocs + reloc_dir->Size - (DWORD) end_block;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
662 holder = malloc (len_import_relocs);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
663 if (holder == 0)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
664 abort ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
665 memcpy (holder, start_block, len_import_relocs);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
666 memcpy (start_block, end_block, len_remaining_relocs);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
667 memcpy ((char *) start_block + len_remaining_relocs, holder, len_import_relocs);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
668 free (holder);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
669 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
670
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
671 /* Walk up the list of base relocations, checking for references
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
672 to the old import section location, and patching them to
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
673 reference the new location. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
674 for (block = relocs;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
675 (DWORD) block - (DWORD) relocs < reloc_dir->Size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
676 block = (void *)((DWORD) block + block->SizeOfBlock))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
677 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
678 DWORD page_rva = block->VirtualAddress;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
679 DWORD page_offset;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
680 union {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
681 WORD word;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
682 DWORD dword;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
683 } * ploc;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
684 WORD *fixup;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
685
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
686 section = rva_to_section (page_rva, dst_nt_header);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
687 /* Don't apply fixups to the blanked sections. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
688 if (section->Name[0] == 'X')
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
689 continue;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
690
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
691 for (fixup = (WORD *) &block[1];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
692 (DWORD) fixup - (DWORD) block < block->SizeOfBlock;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
693 fixup++)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
694 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
695 page_offset = (*fixup) & 0xfff;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
696 ploc = RVA_TO_PTR (page_rva + page_offset, section, p_outfile);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
697
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
698 /* Unless our assumption is wrong, all low word fixups
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
699 should immediately follow a high fixup. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
700 if (seen_high && ((*fixup) >> 12) != IMAGE_REL_BASED_LOW)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
701 abort ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
702
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
703 switch ((*fixup) >> 12)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
704 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
705 case IMAGE_REL_BASED_ABSOLUTE:
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
706 break;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
707 case IMAGE_REL_BASED_HIGH:
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
708 /* We must assume that high and low fixups occur in
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
709 pairs, specifically a low fixup immediately follows a
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
710 high fixup (normally separated by two bytes). We
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
711 have to process the two fixups together, to find out
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
712 the full pointer value and decide whether to apply
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
713 the fixup. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
714 seen_high = 1;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
715 high_word = &ploc->word;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
716 break;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
717 case IMAGE_REL_BASED_LOW:
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
718 offset = (*high_word << 16) + ploc->word;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
719 if (offset >= import_start && offset < import_end)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
720 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
721 (*high_word) += import_delta_rva >> 16;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
722 ploc->dword += import_delta_rva & 0xffff;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
723 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
724 seen_high = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
725 break;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
726 case IMAGE_REL_BASED_HIGHLOW:
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
727 /* Docs imply two words in big-endian order, so perhaps
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
728 this is only used on big-endian platforms, in which
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
729 case the obvious code will work. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
730 if (ploc->dword >= import_start && ploc->dword < import_end)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
731 ploc->dword += import_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
732 break;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
733 case IMAGE_REL_BASED_HIGHADJ:
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
734 /* Docs don't say, but I guess this is the equivalent
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
735 for little-endian platforms. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
736 if (ploc->dword >= import_start && ploc->dword < import_end)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
737 ploc->dword += import_delta_rva;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
738 break;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
739 case IMAGE_REL_BASED_MIPS_JMPADDR:
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
740 /* Don't know how to handle this; MIPS support has been
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
741 dropped from NT4 anyway. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
742 abort ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
743 break;
24805
eb5e287ff675 Fix typo.
Andrew Innes <andrewi@gnu.org>
parents: 24666
diff changeset
744 #ifdef IMAGE_REL_BASED_SECTION
24363
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
745 case IMAGE_REL_BASED_SECTION:
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
746 case IMAGE_REL_BASED_REL32:
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
747 /* Docs don't say what these values mean. */
24666
239ee0e7b828 (copy_executable_and_move_sections): Ifdef out a
Andrew Innes <andrewi@gnu.org>
parents: 24363
diff changeset
748 #endif
239ee0e7b828 (copy_executable_and_move_sections): Ifdef out a
Andrew Innes <andrewi@gnu.org>
parents: 24363
diff changeset
749 default:
24363
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
750 abort ();
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
751 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
752 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
753 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
754 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
755 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
756
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
757
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
758 int
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
759 main (int argc, char **argv)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
760 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
761 PIMAGE_DOS_HEADER dos_header;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
762 PIMAGE_NT_HEADERS nt_header;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
763 file_data in_file, out_file;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
764 char out_filename[MAX_PATH], in_filename[MAX_PATH];
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
765 char *ptr;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
766
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
767 strcpy (in_filename, argv[1]);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
768 strcpy (out_filename, argv[2]);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
769
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
770 printf ("Preparing %s for profile prepping\n", out_filename);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
771
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
772 /* Open the original (dumped) executable file for reference. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
773 if (!open_input_file (&in_file, in_filename))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
774 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
775 printf ("Failed to open %s (%d)...bailing.\n",
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
776 in_filename, GetLastError ());
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
777 exit (1);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
778 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
779
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
780 /* Create a new image that can be prepped; we don't expect the size to
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
781 change because we are only adding two new section table entries,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
782 which should fit in the alignment slop. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
783 if (!open_output_file (&out_file, out_filename, in_file.size))
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
784 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
785 printf ("Failed to open %s (%d)...bailing.\n",
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
786 out_filename, GetLastError ());
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
787 exit (1);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
788 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
789
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
790 copy_executable_and_move_sections (&in_file, &out_file);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
791
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
792 /* Patch up header fields; profiler is picky about this. */
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
793 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
794 HANDLE hImagehelp = LoadLibrary ("imagehlp.dll");
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
795 DWORD headersum;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
796 DWORD checksum;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
797
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
798 dos_header = (PIMAGE_DOS_HEADER) out_file.file_base;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
799 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
800
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
801 nt_header->OptionalHeader.CheckSum = 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
802 // nt_header->FileHeader.TimeDateStamp = time (NULL);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
803 // dos_header->e_cp = size / 512;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
804 // nt_header->OptionalHeader.SizeOfImage = size;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
805
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
806 pfnCheckSumMappedFile = (void *) GetProcAddress (hImagehelp, "CheckSumMappedFile");
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
807 if (pfnCheckSumMappedFile)
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
808 {
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
809 // nt_header->FileHeader.TimeDateStamp = time (NULL);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
810 pfnCheckSumMappedFile (out_file.file_base,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
811 out_file.size,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
812 &headersum,
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
813 &checksum);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
814 nt_header->OptionalHeader.CheckSum = checksum;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
815 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
816 FreeLibrary (hImagehelp);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
817 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
818
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
819 close_file_data (&out_file);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
820 close_file_data (&in_file);
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
821
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
822 return 0;
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
823 }
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
824
b7ccfe3bfb94 Initial revision
Andrew Innes <andrewi@gnu.org>
parents:
diff changeset
825 /* eof */