This is the mail archive of the cygwin-apps mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch/rebase] Make rebase 64 bit capable (was Re: [patch/rebase] Make imagehelper 64-bit capable)


On Jul  7 21:23, Corinna Vinschen wrote:
> It was so surprisingly easy to convert imagehelper to 64 bit that I'm
> concerned I forgot something.  However, my tests indicate that it really
> works as advertised.
> [...]
> While I was at it, I also made the code 64 bit clean and fixed a bug 
> in the MultiByteToWideChar code (CP_OEMCP, not CP_OEM).  It builds and
> works fine under 32 bit Cygwin and 64 bit Mingw.

Same goes for rebase itself.  It now works for 32 and 64 bit DLLs
and it builds and runs on 64 bit Mingw, too.

However, since it only know a single base address, it will use that
address for 32 and 64 bit images, mixing them.  That doesn't make
much sense, but for now I was only interested in the basic capability
to rebase both types of DLLs.

I revoke my rebase database patch for now.  My local version works with
32 and 64 bit DLLs, too, but it suffers from the same problem as the
base functionality.

I'll rework my patch to do the right thing(TM) for 32 and 64 bit DLLs.
I'm not sure yet about the command line options.  Maybe we use -b for
the 32 bit base address and -B for the 64 bit base address?

Btw., peflags does not build on 64 bit mingw either, yet.


Corinna


	* rebase.c: Convert all base address variables to ULONG64
	throughout.
	(main): On Cygwin, call GetImageInfos64 to get Cygwin DLLs
	data.
	(print_image_info): Use conditional printf format string.
	(rebase): Call ReBaseImage64.  Use conditional printf format string.
	(parse_args): Call string_to_ulonglong.
	(string_to_ulonglong): Rename from string_to_ulong, call
	strtoull, and return unsigned long long.


Index: rebase.c
===================================================================
RCS file: /sourceware/projects/cygwin-apps-home/cvsfiles/rebase/rebase.c,v
retrieving revision 1.4
diff -u -p -r1.4 rebase.c
--- rebase.c	29 Jun 2011 14:58:55 -0000	1.4
+++ rebase.c	7 Jul 2011 19:41:14 -0000
@@ -29,9 +29,9 @@
 
 BOOL collect_image_info (const char *pathname);
 void print_image_info ();
-BOOL rebase (const char *pathname, ULONG *new_image_base);
+BOOL rebase (const char *pathname, ULONG64 *new_image_base);
 void parse_args (int argc, char *argv[]);
-unsigned long string_to_ulong (const char *string);
+unsigned long long string_to_ulonglong (const char *string);
 void usage ();
 BOOL is_rebaseable (const char *pathname);
 FILE *file_list_fopen (const char *file_list);
@@ -39,7 +39,7 @@ char *file_list_fgets (char *buf, int si
 int file_list_fclose (FILE *file);
 void version ();
 
-ULONG image_base = 0;
+ULONG64 image_base = 0;
 BOOL down_flag = FALSE;
 BOOL image_info_flag = FALSE;
 ULONG offset = 0;
@@ -53,7 +53,7 @@ ULONG ALLOCATION_SLOT;	/* Allocation gra
 typedef struct _img_info
 {
   const char *name;
-  ULONG base;
+  ULONG64 base;
   ULONG size;
 } img_info_t;
 
@@ -62,14 +62,14 @@ unsigned int img_info_size = 0;
 unsigned int img_info_max_size = 0;
 
 #ifdef __CYGWIN__
-ULONG cygwin_dll_image_base = 0;
+ULONG64 cygwin_dll_image_base = 0;
 ULONG cygwin_dll_image_size = 0;
 #endif
 
 int
 main (int argc, char *argv[])
 {
-  ULONG new_image_base = 0;
+  ULONG64 new_image_base = 0;
   int i = 0;
   SYSTEM_INFO si;
   BOOL status;
@@ -83,8 +83,8 @@ main (int argc, char *argv[])
 #ifdef __CYGWIN__
   /* Fetch the Cygwin DLLs data to make sure that DLLs aren't rebased
      into the memory area taken by the Cygwin DLL. */
-  GetImageInfos ("/bin/cygwin1.dll", &cygwin_dll_image_base,
-				     &cygwin_dll_image_size);
+  GetImageInfos64 ("/bin/cygwin1.dll", NULL,
+		   &cygwin_dll_image_base, &cygwin_dll_image_size);
   /* Take the three shared memory areas preceeding the DLL into account. */
   cygwin_dll_image_base -= 3 * ALLOCATION_SLOT;
   /* Add a slack of 8 * 64K at the end of the Cygwin DLL.  This leave a
@@ -156,8 +156,9 @@ collect_image_info (const char *pathname
 	}
     }
 
-  if (GetImageInfos (pathname, &img_info_list[img_info_size].base,
-			       &img_info_list[img_info_size].size))
+  if (GetImageInfos64 (pathname, NULL,
+		       &img_info_list[img_info_size].base,
+		       &img_info_list[img_info_size].size))
     img_info_list[img_info_size++].name = strdup (pathname);
   return TRUE;
 }
@@ -165,8 +166,8 @@ collect_image_info (const char *pathname
 int
 img_info_cmp (const void *a, const void *b)
 {
-  ULONG abase = ((img_info_t *) a)->base;
-  ULONG bbase = ((img_info_t *) b)->base;
+  ULONG64 abase = ((img_info_t *) a)->base;
+  ULONG64 bbase = ((img_info_t *) b)->base;
 
   if (abase < bbase)
     return -1;
@@ -182,16 +183,22 @@ print_image_info ()
 
   qsort (img_info_list, img_info_size, sizeof (img_info_t), img_info_cmp);
   for (i = 0; i < img_info_size; ++i)
-    printf ("%-47s base 0x%08lx size 0x%08lx\n",
+    printf (
+#if defined (__CYGWIN__) || defined (__MSYS__)
+	    "%-47s base 0x%08llx size 0x%08lx\n",
+#else
+	    "%-47s base 0x%08I64x size 0x%08lx\n",
+#endif
 	    img_info_list[i].name,
 	    img_info_list[i].base,
 	    img_info_list[i].size);
 }
 
 BOOL
-rebase (const char *pathname, ULONG *new_image_base)
+rebase (const char *pathname, ULONG64 *new_image_base)
 {
-  ULONG old_image_size, old_image_base, new_image_size, prev_new_image_base;
+  ULONG64 old_image_base, prev_new_image_base;
+  ULONG old_image_size, new_image_size;
   BOOL status, status2;
 
   /* Skip if file does not exist to prevent ReBaseImage() from using it's
@@ -227,17 +234,17 @@ retry:
 
   /* Rebase the image. */
   prev_new_image_base = *new_image_base;
-  status = ReBaseImage ((char*) pathname,	/* CurrentImageName */
-			 "",			/* SymbolPath */
-			 TRUE,			/* fReBase */
-			 FALSE,			/* fRebaseSysfileOk */
-			 down_flag,		/* fGoingDown */
-			 0,			/* CheckImageSize */
-			 &old_image_size,	/* OldImageSize */
-			 &old_image_base,	/* OldImageBase */
-			 &new_image_size,	/* NewImageSize */
-			 new_image_base,	/* NewImageBase */
-			 time (0));		/* TimeStamp */
+  status = ReBaseImage64 ((char*) pathname,	/* CurrentImageName */
+			  "",			/* SymbolPath */
+			  TRUE,			/* fReBase */
+			  FALSE,		/* fRebaseSysfileOk */
+			  down_flag,		/* fGoingDown */
+			  0,			/* CheckImageSize */
+			  &old_image_size,	/* OldImageSize */
+			  &old_image_base,	/* OldImageBase */
+			  &new_image_size,	/* NewImageSize */
+			  new_image_base,	/* NewImageBase */
+			  time (0));		/* TimeStamp */
 
   /* MS's ReBaseImage seems to never return false! */
   status2 = GetLastError ();
@@ -256,17 +263,17 @@ retry:
 	}
 
       /* Retry rebase.*/
-      status = ReBaseImage ((char*) pathname,	/* CurrentImageName */
-			    "",			/* SymbolPath */
-			    TRUE,		/* fReBase */
-			    FALSE,		/* fRebaseSysfileOk */
-			    down_flag,		/* fGoingDown */
-			    0,			/* CheckImageSize */
-			    &old_image_size,	/* OldImageSize */
-			    &old_image_base,	/* OldImageBase */
-			    &new_image_size,	/* NewImageSize */
-			    new_image_base,	/* NewImageBase */
-			    time (0));		/* TimeStamp */
+      status = ReBaseImage64 ((char*) pathname,	/* CurrentImageName */
+			      "",		/* SymbolPath */
+			      TRUE,		/* fReBase */
+			      FALSE,		/* fRebaseSysfileOk */
+			      down_flag,	/* fGoingDown */
+			      0,		/* CheckImageSize */
+			      &old_image_size,	/* OldImageSize */
+			      &old_image_base,	/* OldImageBase */
+			      &new_image_size,	/* NewImageSize */
+			      new_image_base,	/* NewImageBase */
+			      time (0));	/* TimeStamp */
 
       /* MS's ReBaseImage seems to never return false! */
       status2 = GetLastError ();
@@ -296,7 +303,13 @@ retry:
   /* Display rebase results, if verbose. */
   if (verbose)
     {
-      printf ("%s: new base = %lx, new size = %lx\n", pathname,
+      printf (
+#if defined (__CYGWIN__) || defined (__MSYS__)
+	      "%s: new base = %llx, new size = %lx\n",
+#else
+	      "%s: new base = %I64x, new size = %lx\n",
+#endif
+	      pathname,
 	      ((down_flag) ? *new_image_base : prev_new_image_base),
 	      new_image_size + offset);
     }
@@ -319,7 +332,7 @@ parse_args (int argc, char *argv[])
       switch (anOption)
 	{
 	case 'b':
-	  image_base = string_to_ulong (optarg);
+	  image_base = string_to_ulonglong (optarg);
 	  break;
 	case 'd':
 	  down_flag = TRUE;
@@ -328,7 +341,7 @@ parse_args (int argc, char *argv[])
 	  image_info_flag = TRUE;
 	  break;
 	case 'o':
-	  offset = string_to_ulong (optarg);
+	  offset = string_to_ulonglong (optarg);
 	  break;
 	case 'T':
 	  file_list = optarg;
@@ -357,11 +370,11 @@ parse_args (int argc, char *argv[])
   args_index = optind;
 }
 
-unsigned long
-string_to_ulong (const char *string)
+unsigned long long
+string_to_ulonglong (const char *string)
 {
-  unsigned long number = 0;
-  number = strtoul (string, 0, 0);
+  unsigned long long number = 0;
+  number = strtoull (string, 0, 0);
   return number;
 }
 


-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]