comparison src/unexnt.c @ 209:41ff10fd062f r20-4b3

Import from CVS: tag r20-4b3
author cvs
date Mon, 13 Aug 2007 10:04:58 +0200
parents 15872534500d
children c42ec1d1cded
comparison
equal deleted inserted replaced
208:f427b8ec4379 209:41ff10fd062f
31 extern BOOL ctrl_c_handler (unsigned long type); 31 extern BOOL ctrl_c_handler (unsigned long type);
32 #endif 32 #endif
33 33
34 #include "ntheap.h" 34 #include "ntheap.h"
35 35
36 /* A convenient type for keeping all the info about a mapped file together. */ 36 /* Sync with FSF Emacs 19.34.6 note: struct file_data is now defined in ntheap.h */
37 typedef struct file_data {
38 char *name;
39 unsigned long size;
40 HANDLE file;
41 HANDLE file_mapping;
42 unsigned char *file_base;
43 } file_data;
44 37
45 enum { 38 enum {
46 HEAP_UNINITIALIZED = 1, 39 HEAP_UNINITIALIZED = 1,
47 HEAP_UNLOADED, 40 HEAP_UNLOADED,
48 HEAP_LOADED 41 HEAP_LOADED
51 /* Basically, our "initialized" flag. */ 44 /* Basically, our "initialized" flag. */
52 int heap_state = HEAP_UNINITIALIZED; 45 int heap_state = HEAP_UNINITIALIZED;
53 46
54 /* So we can find our heap in the file to recreate it. */ 47 /* So we can find our heap in the file to recreate it. */
55 unsigned long heap_index_in_executable = UNINIT_LONG; 48 unsigned long heap_index_in_executable = UNINIT_LONG;
56
57 void open_input_file (file_data *p_file, char *name);
58 void open_output_file (file_data *p_file, char *name, unsigned long size);
59 void close_file_data (file_data *p_file);
60 49
61 void get_section_info (file_data *p_file); 50 void get_section_info (file_data *p_file);
62 void copy_executable_and_dump_data_section (file_data *, file_data *); 51 void copy_executable_and_dump_data_section (file_data *, file_data *);
63 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile); 52 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
64 53
155 144
156 /* We need to round off our heap to NT's allocation unit (64KB). */ 145 /* We need to round off our heap to NT's allocation unit (64KB). */
157 round_heap (get_allocation_unit ()); 146 round_heap (get_allocation_unit ());
158 147
159 /* Open the undumped executable file. */ 148 /* Open the undumped executable file. */
160 open_input_file (&in_file, in_filename); 149 if (!open_input_file (&in_file, in_filename))
150 {
151 printf ("Failed to open %s (%d)...bailing.\n",
152 in_filename, GetLastError ());
153 exit (1);
154 }
161 155
162 /* Get the interesting section info, like start and size of .bss... */ 156 /* Get the interesting section info, like start and size of .bss... */
163 get_section_info (&in_file); 157 get_section_info (&in_file);
164 158
165 /* The size of the dumped executable is the size of the original 159 /* The size of the dumped executable is the size of the original
166 executable plus the size of the heap and the size of the .bss section. */ 160 executable plus the size of the heap and the size of the .bss section. */
167 heap_index_in_executable = (unsigned long) 161 heap_index_in_executable = (unsigned long)
168 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ()); 162 round_to_next ((unsigned char *) in_file.size, get_allocation_unit ());
169 size = heap_index_in_executable + get_committed_heap_size () + bss_size; 163 size = heap_index_in_executable + get_committed_heap_size () + bss_size;
170 open_output_file (&out_file, out_filename, size); 164 if (!open_output_file (&out_file, out_filename, size))
165 {
166 printf ("Failed to open %s (%d)...bailing.\n",
167 out_filename, GetLastError ());
168 exit (1);
169 }
171 170
172 /* Set the flag (before dumping). */ 171 /* Set the flag (before dumping). */
173 heap_state = HEAP_UNLOADED; 172 heap_state = HEAP_UNLOADED;
174 173
175 copy_executable_and_dump_data_section (&in_file, &out_file); 174 copy_executable_and_dump_data_section (&in_file, &out_file);
181 180
182 181
183 /* File handling. */ 182 /* File handling. */
184 183
185 184
186 void 185 int
187 open_input_file (file_data *p_file, char *filename) 186 open_input_file (file_data *p_file, char *filename)
188 { 187 {
189 HANDLE file; 188 HANDLE file;
190 HANDLE file_mapping; 189 HANDLE file_mapping;
191 void *file_base; 190 void *file_base;
192 unsigned long size, upper_size; 191 unsigned long size, upper_size;
193 192
194 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL, 193 file = CreateFile (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
195 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); 194 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
196 if (file == INVALID_HANDLE_VALUE) 195 if (file == INVALID_HANDLE_VALUE)
197 { 196 return FALSE;
198 printf ("Failed to open %s (%d)...bailing.\n",
199 filename, GetLastError ());
200 exit (1);
201 }
202 197
203 size = GetFileSize (file, &upper_size); 198 size = GetFileSize (file, &upper_size);
204 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY, 199 file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
205 0, size, NULL); 200 0, size, NULL);
206 if (!file_mapping) 201 if (!file_mapping)
207 { 202 return FALSE;
208 printf ("Failed to create file mapping of %s (%d)...bailing.\n",
209 filename, GetLastError ());
210 exit (1);
211 }
212 203
213 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size); 204 file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
214 if (file_base == 0) 205 if (file_base == 0)
215 { 206 return FALSE;
216 printf ("Failed to map view of file of %s (%d)...bailing.\n",
217 filename, GetLastError ());
218 exit (1);
219 }
220 207
221 p_file->name = filename; 208 p_file->name = filename;
222 p_file->size = size; 209 p_file->size = size;
223 p_file->file = file; 210 p_file->file = file;
224 p_file->file_mapping = file_mapping; 211 p_file->file_mapping = file_mapping;
225 p_file->file_base = file_base; 212 p_file->file_base = file_base;
226 } 213
227 214 return TRUE;
228 void 215 }
216
217 int
229 open_output_file (file_data *p_file, char *filename, unsigned long size) 218 open_output_file (file_data *p_file, char *filename, unsigned long size)
230 { 219 {
231 HANDLE file; 220 HANDLE file;
232 HANDLE file_mapping; 221 HANDLE file_mapping;
233 void *file_base; 222 void *file_base;
234 int i;
235 223
236 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, 224 file = CreateFile (filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
237 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); 225 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
238 if (file == INVALID_HANDLE_VALUE) 226 if (file == INVALID_HANDLE_VALUE)
239 { 227 return FALSE;
240 i = GetLastError ();
241 printf ("open_output_file: Failed to open %s (%d).\n",
242 filename, i);
243 exit (1);
244 }
245 228
246 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE, 229 file_mapping = CreateFileMapping (file, NULL, PAGE_READWRITE,
247 0, size, NULL); 230 0, size, NULL);
248 if (!file_mapping) 231 if (!file_mapping)
249 { 232 return FALSE;
250 i = GetLastError ();
251 printf ("open_output_file: Failed to create file mapping of %s (%d).\n",
252 filename, i);
253 exit (1);
254 }
255 233
256 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size); 234 file_base = MapViewOfFile (file_mapping, FILE_MAP_WRITE, 0, 0, size);
257 if (file_base == 0) 235 if (file_base == 0)
258 { 236 return FALSE;
259 i = GetLastError ();
260 printf ("open_output_file: Failed to map view of file of %s (%d).\n",
261 filename, i);
262 exit (1);
263 }
264 237
265 p_file->name = filename; 238 p_file->name = filename;
266 p_file->size = size; 239 p_file->size = size;
267 p_file->file = file; 240 p_file->file = file;
268 p_file->file_mapping = file_mapping; 241 p_file->file_mapping = file_mapping;
269 p_file->file_base = file_base; 242 p_file->file_base = file_base;
243
244 return TRUE;
270 } 245 }
271 246
272 /* Close the system structures associated with the given file. */ 247 /* Close the system structures associated with the given file. */
273 static void 248 void
274 close_file_data (file_data *p_file) 249 close_file_data (file_data *p_file)
275 { 250 {
276 UnmapViewOfFile (p_file->file_base); 251 UnmapViewOfFile (p_file->file_base);
277 CloseHandle (p_file->file_mapping); 252 CloseHandle (p_file->file_mapping);
278 CloseHandle (p_file->file); 253 CloseHandle (p_file->file);
316 } 291 }
317 break; 292 break;
318 } 293 }
319 *p_bss_start = (PUCHAR) start; 294 *p_bss_start = (PUCHAR) start;
320 *p_bss_size = (DWORD) len; 295 *p_bss_size = (DWORD) len;
296 }
297
298 /* Return pointer to section header for named section. */
299 IMAGE_SECTION_HEADER *
300 find_section (char * name, IMAGE_NT_HEADERS * nt_header)
301 {
302 PIMAGE_SECTION_HEADER section;
303 int i;
304
305 section = IMAGE_FIRST_SECTION (nt_header);
306
307 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
308 {
309 if (strcmp (section->Name, name) == 0)
310 return section;
311 section++;
312 }
313 return NULL;
314 }
315
316 /* Return pointer to section header for section containing the given
317 relative virtual address. */
318 IMAGE_SECTION_HEADER *
319 rva_to_section (DWORD rva, IMAGE_NT_HEADERS * nt_header)
320 {
321 PIMAGE_SECTION_HEADER section;
322 int i;
323
324 section = IMAGE_FIRST_SECTION (nt_header);
325
326 for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
327 {
328 if (rva >= section->VirtualAddress &&
329 rva < section->VirtualAddress + section->SizeOfRawData)
330 return section;
331 section++;
332 }
333 return NULL;
321 } 334 }
322 335
323 static unsigned long 336 static unsigned long
324 get_section_size (PIMAGE_SECTION_HEADER p_section) 337 get_section_size (PIMAGE_SECTION_HEADER p_section)
325 { 338 {