public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* rebase addon - Bas and Size listing of dll's without rebasing
@ 2001-12-26 14:01 Ralf Habacker
  2001-12-27 10:22 ` Jason Tishler
  0 siblings, 1 reply; 12+ messages in thread
From: Ralf Habacker @ 2001-12-26 14:01 UTC (permalink / raw)
  To: Cygwin

[-- Attachment #1: Type: text/plain, Size: 4840 bytes --]

Hi,

while porting kde2 the rebase tool solved some problems for me on using libtools libltdl.

In the past I have to use the LTDL_DLOPEN_DEPLIBS in the config.h of libtldl, which enables
loading of all dependency dll's of a lib and because of many dll's used for kde this slow
down application loading significantly. If I haven't used this flag, cygwin tells about a
"... can't remap ..." problem.
After rebasing the dll's from the kdelibs, this error does not happens anymore and I can
reject this config option.

libltdl/config.h
...
/* Define if the OS needs help to load dependent libraries for dlopen(). */
//#define LTDL_DLOPEN_DEPLIBS 1
....

On analysing this problem I added an option "-l" to the rebase tool to list dll's imagebase
and -size without rebasing. It's very interesting to see how MS chooses there Imagebase.

Because I hacked this very quick, I have used simple FILE structure and fopen/fread calls.
Perhaps someone like to change this to iostream functionality.

$ rebase -h
rebase: unknown option -- h
usage: rebase -b BaseAddress [-d] -o Offset <file> ...  rebase <file>
usage: rebase -l <file> ...        list Imagebase and -size of <file>

Example:
$ rebase -l /opt/kde2/bin/*.dll
c:\programme\cygwin\opt\kde2\bin\cygDCOP-1.dll               - ImageBase: 74fb0000 ImageSize:
00026000
c:\programme\cygwin\opt\kde2\bin\cygartsc-0.dll              - ImageBase: 74f80000 ImageSize:
00007000
c:\programme\cygwin\opt\kde2\bin\cygartsflow-0.dll           - ImageBase: 74ee0000 ImageSize:
0007a000
c:\programme\cygwin\opt\kde2\bin\cygartsflow_idl-0.dll       - ImageBase: 74e50000 ImageSize:
0006f000
c:\programme\cygwin\opt\kde2\bin\cygartskde.dll              - ImageBase: 74df0000 ImageSize:
0003f000
c:\programme\cygwin\opt\kde2\bin\cygkICE-0.dll               - ImageBase: 74db0000 ImageSize:
00017000
c:\programme\cygwin\opt\kde2\bin\cygkab-3.dll                - ImageBase: 74d30000 ImageSize:
0005e000
c:\programme\cygwin\opt\kde2\bin\cygkbuildsycoca.dll         - ImageBase: 74cf0000 ImageSize:
0001d000
c:\programme\cygwin\opt\kde2\bin\cygkdecore-3.dll            - ImageBase: 74b30000 ImageSize:
00194000
c:\programme\cygwin\opt\kde2\bin\cygkdefakes-3.dll           - ImageBase: 74b00000 ImageSize:
00007000
c:\programme\cygwin\opt\kde2\bin\cygkdeprint-0.dll           - ImageBase: 74a50000 ImageSize:
00084000
c:\programme\cygwin\opt\kde2\bin\cygkdeprint_management-0.dll - ImageBase: 749b0000
ImageSize: 00075000
c:\programme\cygwin\opt\kde2\bin\cygkdesu-1.dll              - ImageBase: 74960000 ImageSize:
0002d000
c:\programme\cygwin\opt\kde2\bin\cygkdeui-3.dll              - ImageBase: 74750000 ImageSize:
001ee000
c:\programme\cygwin\opt\kde2\bin\cygkfile-3.dll              - ImageBase: 74690000 ImageSize:
0009c000
c:\programme\cygwin\opt\kde2\bin\cygkio-3.dll                - ImageBase: 74590000 ImageSize:
000d2000
c:\programme\cygwin\opt\kde2\bin\cygkjava-1.dll              - ImageBase: 74540000 ImageSize:
00027000
c:\programme\cygwin\opt\kde2\bin\cygkjs-1.dll                - ImageBase: 744d0000 ImageSize:
00044000
c:\programme\cygwin\opt\kde2\bin\cygkmedia2-0.dll            - ImageBase: 74480000 ImageSize:
00025000
c:\programme\cygwin\opt\kde2\bin\cygkmedia2_idl-0.dll        - ImageBase: 74400000 ImageSize:
00054000
c:\programme\cygwin\opt\kde2\bin\cygkmid-0.dll               - ImageBase: 743c0000 ImageSize:
0001b000
c:\programme\cygwin\opt\kde2\bin\cygkonq-3.dll               - ImageBase: 74300000 ImageSize:
0009c000
c:\programme\cygwin\opt\kde2\bin\cygkonqsidebartree_bookmarks-0.dll - ImageBase: 742d0000
ImageSize: 0000e000
c:\programme\cygwin\opt\kde2\bin\cygkparts-1.dll             - ImageBase: 74270000 ImageSize:
00033000
c:\programme\cygwin\opt\kde2\bin\cygkspell-3.dll             - ImageBase: 74220000 ImageSize:
00029000
c:\programme\cygwin\opt\kde2\bin\cygkssl-2.dll               - ImageBase: 741d0000 ImageSize:
0002e000
c:\programme\cygwin\opt\kde2\bin\cygksycoca-3.dll            - ImageBase: 740f0000 ImageSize:
000b9000
c:\programme\cygwin\opt\kde2\bin\cygktexteditor-0.dll        - ImageBase: 740c0000 ImageSize:
0000f000
c:\programme\cygwin\opt\kde2\bin\cygltdl-0.dll               - ImageBase: 74090000 ImageSize:
0000a000
c:\programme\cygwin\opt\kde2\bin\cygmcop-0.dll               - ImageBase: 73f60000 ImageSize:
00101000
c:\programme\cygwin\opt\kde2\bin\cygmcop_mt-0.dll            - ImageBase: 73f30000 ImageSize:
00007000
c:\programme\cygwin\opt\kde2\bin\cygqtmcop-0.dll             - ImageBase: 73ef0000 ImageSize:
0001f000
c:\programme\cygwin\opt\kde2\bin\cygsoundserver_idl-0.dll    - ImageBase: 73e70000 ImageSize:
0005a000
c:\programme\cygwin\opt\kde2\bin\cygxml2-2.dll               - ImageBase: 73dc0000 ImageSize:
00085000
c:\programme\cygwin\opt\kde2\bin\cygxslt-0.dll               - ImageBase: 73d70000 ImageSize:
00021000


Regards
Ralf



[-- Attachment #2: rebase-list-option.dif --]
[-- Type: application/octet-stream, Size: 2399 bytes --]

--- rebase.cc.orig	Mon Dec 24 11:33:46 2001
+++ rebase.cc	Wed Dec 26 19:53:36 2001
@@ -28,6 +28,8 @@
 #include <windows.h>
 #include <imagehlp.h>
 
+#include <stdio.h>
+
 using namespace std;
 
 string PosixToWin32(const string& aPosixPath);
@@ -39,6 +41,7 @@ ULONG theImageBase = 0;
 BOOL theDownFlag = FALSE;
 ULONG theOffset = 0;
 int theArgsIndex = 0;
+int theListFlag = 0; 
 
 void
 main(int argc, char* argv[])
@@ -48,10 +51,20 @@ main(int argc, char* argv[])
 
 	for (int i = theArgsIndex; i < argc; i++)
 	{
+    string aFile = PosixToWin32(argv[i]);
+    if (theListFlag)
+    {
+      long image[60]; 
+      
+      FILE *f = fopen(const_cast<char*>(aFile.c_str()),"r");
+      fread(&image,sizeof(image),1,f);
+      printf("%-60s - ImageBase: %08x ImageSize: %08x\n",aFile.c_str(),image[45],image[52]);
+      fclose(f);
+    }
+    else {
 		if (theDownFlag)
 			aNewImageBase -= theOffset;
 
-		string aFile = PosixToWin32(argv[i]);
 		ULONG anOldImageSize, anOldImageBase, aNewImageSize;
 		ULONG aPrevNewImageBase = aNewImageBase;
 		BOOL aStatus = ReBaseImage(
@@ -82,6 +95,8 @@ main(int argc, char* argv[])
 		if (!theDownFlag)
 			aNewImageBase += theOffset;
 	}
+  }
+  
 
 	exit(0);
 }
@@ -97,7 +112,7 @@ PosixToWin32(const string& aPosixPath)
 void
 ParseArgs(int argc, char* argv[])
 {
-	const char* anOptions = "b:do:";
+  const char* anOptions = "b:do:l:";
 	for (int anOption; (anOption = getopt(argc, argv, anOptions)) != -1;)
 	{
 		switch (anOption)
@@ -111,6 +126,9 @@ ParseArgs(int argc, char* argv[])
 			case 'o':
 				theOffset = StringToUlong(optarg);
 				break;
+      case 'l':
+        theListFlag = 1;
+        break;
 			default:
 				Usage();
 				exit(1);
@@ -118,12 +136,15 @@ ParseArgs(int argc, char* argv[])
 		}
 	}
 
-	if (theImageBase == 0)
+  if (theImageBase == 0 && !theListFlag)
 	{
 		Usage();
 		exit(1);
 	}
 
+  if (theListFlag)
+    theArgsIndex = optind-1;
+  else 
 	theArgsIndex = optind;
 }
 
@@ -144,6 +165,7 @@ StringToUlong(const string& aString)
 void
 Usage()
 {
-	cerr << "usage: rebase -b BaseAddress [-d] -o Offset ImageFileName ..." << endl;
+  cerr << "usage: rebase -b BaseAddress [-d] -o Offset <file> ...  rebase <file>" << endl;
+  cerr << "usage: rebase -l <file> ...        list Imagebase and -size of <file>" << endl;
 }
 

[-- Attachment #3: rebase.cc --]
[-- Type: application/octet-stream, Size: 4179 bytes --]

/*
 * Copyright (c) 2001 Jason Tishler
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: rebase.cc,v 1.5 2001/12/18 20:21:19 jtishler Exp $
 */

#include <iostream>
#include <sstream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/cygwin.h>
#include <windows.h>
#include <imagehlp.h>

#include <stdio.h>

using namespace std;

string PosixToWin32(const string& aPosixPath);
void ParseArgs(int argc, char* argv[]);
unsigned long StringToUlong(const string& aString);
void Usage();

ULONG theImageBase = 0;
BOOL theDownFlag = FALSE;
ULONG theOffset = 0;
int theArgsIndex = 0;
int theListFlag = 0; 

void
main(int argc, char* argv[])
{
  ParseArgs(argc, argv);
  ULONG aNewImageBase = theImageBase;

  for (int i = theArgsIndex; i < argc; i++)
  {
    string aFile = PosixToWin32(argv[i]);
    if (theListFlag)
    {
      long image[60]; 
      
      FILE *f = fopen(const_cast<char*>(aFile.c_str()),"r");
      fread(&image,sizeof(image),1,f);
      printf("%-60s - ImageBase: %08x ImageSize: %08x\n",aFile.c_str(),image[45],image[52]);
      fclose(f);
    }
    else {
    if (theDownFlag)
      aNewImageBase -= theOffset;

    ULONG anOldImageSize, anOldImageBase, aNewImageSize;
    ULONG aPrevNewImageBase = aNewImageBase;
    BOOL aStatus = ReBaseImage(
      const_cast<char*>(aFile.c_str()), // CurrentImageName
      0, // SymbolPath
      TRUE, // fReBase
      FALSE, // fRebaseSysfileOk
      theDownFlag, // fGoingDown
      0, // CheckImageSize
      &anOldImageSize, // OldImageSize
      &anOldImageBase, // OldImageBase
      &aNewImageSize, // NewImageSize
      &aNewImageBase, // NewImageBase
      time(0)); // TimeStamp

    // ReBaseImage seems to never returns false!
    DWORD aStatus2 = GetLastError();
    if (aStatus2 != 0)
    {
      cerr << "ReBaseImage() failed with last error = " <<
        GetLastError() << endl;
      exit(2);
    }
    cout << aFile << hex << ": new base = " <<
      ((theDownFlag) ? aNewImageBase : aPrevNewImageBase) <<
      ", new size = " << aNewImageSize + theOffset << endl;

    if (!theDownFlag)
      aNewImageBase += theOffset;
    }
  }
  

  exit(0);
}

string
PosixToWin32(const string& aPosixPath)
{
  char aWin32Path[MAX_PATH];
  cygwin_conv_to_win32_path(aPosixPath.c_str(), aWin32Path);
  return aWin32Path;
}

void
ParseArgs(int argc, char* argv[])
{
  const char* anOptions = "b:do:l:";
  for (int anOption; (anOption = getopt(argc, argv, anOptions)) != -1;)
  {
    switch (anOption)
    {
      case 'b':
        theImageBase = StringToUlong(optarg);
        break;
      case 'd':
        theDownFlag = TRUE;
        break;
      case 'o':
        theOffset = StringToUlong(optarg);
        break;
      case 'l':
        theListFlag = 1;
        break;
      default:
        Usage();
        exit(1);
        break;
    }
  }

  if (theImageBase == 0 && !theListFlag)
  {
    Usage();
    exit(1);
  }

  if (theListFlag)
    theArgsIndex = optind-1;
  else 
    theArgsIndex = optind;
}

unsigned long
StringToUlong(const string& aString)
{
  stringstream ss;
  unsigned long aUlong;
  string::size_type anIndex = aString.find("0x");
  if (anIndex == 0)
    ss << hex << string(aString, 2, aString.size() - 2);
  else
    ss << aString;
  ss >> aUlong;
  return aUlong;
}

void
Usage()
{
  cerr << "usage: rebase -b BaseAddress [-d] -o Offset <file> ...  rebase <file>" << endl;
  cerr << "usage: rebase -l <file> ...        list Imagebase and -size of <file>" << endl;
}


[-- Attachment #4: Makefile --]
[-- Type: application/octet-stream, Size: 110 bytes --]

rebase: rebase.cc
	g++ -g -o $@ $< -limagehlp
	
install: rebase
	/usr/bin/install rebase.exe /usr/local/bin



[-- Attachment #5: Type: text/plain, Size: 214 bytes --]

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2002-01-04 14:18 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-12-26 14:01 rebase addon - Bas and Size listing of dll's without rebasing Ralf Habacker
2001-12-27 10:22 ` Jason Tishler
2001-12-27 18:34   ` Robert Collins
2002-01-03  5:24     ` Jason Tishler
2002-01-03  5:27       ` Robert Collins
2002-01-04  6:10         ` Jason Tishler
2001-12-28  5:48   ` Ralf Habacker
2001-12-28 10:24     ` Christopher Faylor
2001-12-29  3:22       ` Ralf Habacker
2002-01-03  9:25         ` Ralf Habacker
2002-01-03 13:42           ` Robert Collins
2002-01-04  6:18             ` Jason Tishler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).