comparison src/unexw32.c @ 31104:79c890cce460

Change PUCHAR to PCHAR. (PTR_TO_OFFSET): Cast ptr to unsigned char *. (relocate_offset): (get_section_info): (copy_executable_and_dump_data): Remove unnecessary static defs. (copy_executable_and_dump_data): Fix compile warnings. (unexec): Ignore old_name, and use the actual location of the current executable instead. Base new_name on this.
author Andrew Innes <andrewi@gnu.org>
date Tue, 22 Aug 2000 22:44:39 +0000
parents a67730c51617
children 23a1cea22d13
comparison
equal deleted inserted replaced
31103:a805d5d9bc7e 31104:79c890cce460
65 void copy_executable_and_dump_data (file_data *, file_data *); 65 void copy_executable_and_dump_data (file_data *, file_data *);
66 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile); 66 void dump_bss_and_heap (file_data *p_infile, file_data *p_outfile);
67 67
68 /* Cached info about the .data section in the executable. */ 68 /* Cached info about the .data section in the executable. */
69 PIMAGE_SECTION_HEADER data_section; 69 PIMAGE_SECTION_HEADER data_section;
70 PUCHAR data_start = 0; 70 PCHAR data_start = 0;
71 DWORD data_size = 0; 71 DWORD data_size = 0;
72 72
73 /* Cached info about the .bss section in the executable. */ 73 /* Cached info about the .bss section in the executable. */
74 PIMAGE_SECTION_HEADER bss_section; 74 PIMAGE_SECTION_HEADER bss_section;
75 PUCHAR bss_start = 0; 75 PCHAR bss_start = 0;
76 DWORD bss_size = 0; 76 DWORD bss_size = 0;
77 DWORD extra_bss_size = 0; 77 DWORD extra_bss_size = 0;
78 /* bss data that is static might be discontiguous from non-static. */ 78 /* bss data that is static might be discontiguous from non-static. */
79 PIMAGE_SECTION_HEADER bss_section_static; 79 PIMAGE_SECTION_HEADER bss_section_static;
80 PUCHAR bss_start_static = 0; 80 PCHAR bss_start_static = 0;
81 DWORD bss_size_static = 0; 81 DWORD bss_size_static = 0;
82 DWORD extra_bss_size_static = 0; 82 DWORD extra_bss_size_static = 0;
83 83
84 PIMAGE_SECTION_HEADER heap_section; 84 PIMAGE_SECTION_HEADER heap_section;
85 85
282 } 282 }
283 283
284 /* Return offset to an object in dst, given offset in src. We assume 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 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). */ 286 the some sections may have been added to dst (after sections in src). */
287 static DWORD 287 DWORD
288 relocate_offset (DWORD offset, 288 relocate_offset (DWORD offset,
289 IMAGE_NT_HEADERS * src_nt_header, 289 IMAGE_NT_HEADERS * src_nt_header,
290 IMAGE_NT_HEADERS * dst_nt_header) 290 IMAGE_NT_HEADERS * dst_nt_header)
291 { 291 {
292 PIMAGE_SECTION_HEADER src_section = IMAGE_FIRST_SECTION (src_nt_header); 292 PIMAGE_SECTION_HEADER src_section = IMAGE_FIRST_SECTION (src_nt_header);
329 329
330 /* Convert address in executing image to RVA. */ 330 /* Convert address in executing image to RVA. */
331 #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL)) 331 #define PTR_TO_RVA(ptr) ((DWORD)(ptr) - (DWORD) GetModuleHandle (NULL))
332 332
333 #define PTR_TO_OFFSET(ptr, pfile_data) \ 333 #define PTR_TO_OFFSET(ptr, pfile_data) \
334 ((char *)(ptr) - (pfile_data)->file_base) 334 ((unsigned char *)(ptr) - (pfile_data)->file_base)
335 335
336 #define OFFSET_TO_PTR(offset, pfile_data) \ 336 #define OFFSET_TO_PTR(offset, pfile_data) \
337 ((pfile_data)->file_base + (DWORD)(offset)) 337 ((pfile_data)->file_base + (DWORD)(offset))
338 338
339 339
340 /* Flip through the executable and cache the info necessary for dumping. */ 340 /* Flip through the executable and cache the info necessary for dumping. */
341 static void 341 void
342 get_section_info (file_data *p_infile) 342 get_section_info (file_data *p_infile)
343 { 343 {
344 PIMAGE_DOS_HEADER dos_header; 344 PIMAGE_DOS_HEADER dos_header;
345 PIMAGE_NT_HEADERS nt_header; 345 PIMAGE_NT_HEADERS nt_header;
346 PIMAGE_SECTION_HEADER section; 346 PIMAGE_SECTION_HEADER section;
479 } 479 }
480 480
481 481
482 /* The dump routines. */ 482 /* The dump routines. */
483 483
484 static void 484 void
485 copy_executable_and_dump_data (file_data *p_infile, 485 copy_executable_and_dump_data (file_data *p_infile,
486 file_data *p_outfile) 486 file_data *p_outfile)
487 { 487 {
488 unsigned char *dst, *dst_save; 488 unsigned char *dst, *dst_save;
489 PIMAGE_DOS_HEADER dos_header; 489 PIMAGE_DOS_HEADER dos_header;
615 dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA; 615 dst_section->Characteristics &= ~IMAGE_SCN_CNT_UNINITIALIZED_DATA;
616 dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA; 616 dst_section->Characteristics |= IMAGE_SCN_CNT_INITIALIZED_DATA;
617 } 617 }
618 if (section == heap_section) 618 if (section == heap_section)
619 { 619 {
620 DWORD heap_start = get_heap_start (); 620 DWORD heap_start = (DWORD) get_heap_start ();
621 DWORD heap_size = get_committed_heap_size (); 621 DWORD heap_size = get_committed_heap_size ();
622 622
623 /* Dump the used portion of the predump heap, adjusting the 623 /* Dump the used portion of the predump heap, adjusting the
624 section's size to the appropriate size. */ 624 section's size to the appropriate size. */
625 dst = dst_save 625 dst = dst_save
717 void *entry_address) 717 void *entry_address)
718 { 718 {
719 file_data in_file, out_file; 719 file_data in_file, out_file;
720 char out_filename[MAX_PATH], in_filename[MAX_PATH]; 720 char out_filename[MAX_PATH], in_filename[MAX_PATH];
721 unsigned long size; 721 unsigned long size;
722 char *ptr; 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);
723 739
724 /* Make sure that the input and output filenames have the 740 /* Make sure that the output filename has the ".exe" extension...patch
725 ".exe" extension...patch them up if they don't. */ 741 it up if not. */
726 strcpy (in_filename, old_name); 742 p = out_filename + strlen (out_filename) - 4;
727 ptr = in_filename + strlen (in_filename) - 4; 743 if (strcmp (p, ".exe"))
728 if (strcmp (ptr, ".exe"))
729 strcat (in_filename, ".exe");
730
731 strcpy (out_filename, new_name);
732 ptr = out_filename + strlen (out_filename) - 4;
733 if (strcmp (ptr, ".exe"))
734 strcat (out_filename, ".exe"); 744 strcat (out_filename, ".exe");
735 745
736 printf ("Dumping from %s\n", in_filename); 746 printf ("Dumping from %s\n", in_filename);
737 printf (" to %s\n", out_filename); 747 printf (" to %s\n", out_filename);
738 748