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