Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using the Rebase utility in project makefile

0.00/5 (No votes)
5 Oct 2001 1  
Setting the DLL base address in a project makefile using the Rebase utility

Introduction

A Dynamic-link library created in Microsoft Visual C++ has default base address 0x10000000. That means, when the process starts, the loader tries to load the Dll to this address in the process memory. However, if a number of Dlls have the same base address only one of them is loaded to it's default address. For all other Dlls the loader makes the relocation ie.  changes the memory addresses in Dll commands according to the new base address where the Dll is loaded. First, this takes additional time. Second (and more important) - the Dll code kept in process memory is changed. That means, when the Memory Manager needs memory pages for something else, it saves pages with the Dll code to the system paging file. This may reduce the working speed of large program.

Microsoft Visual Studio allows us to change the Dll base address in Project Settings:

Project Settings

But if your system contains a large number of Dlls, managing base addresses is difficult. The Rebase utility which comes both with Microsoft Framework SDK and Microsoft Visual Studio allows you to set optimal base addresses for a number of Dlls starting from some address. The SDK Help Rebase topic recommends to use address 0x60000000. The best place to do this is the  project makefile.

Demo projects

The Demo contains a number of projects. There are two Win32 Dlls Server1 and Server2. Each of them exports one function. The exe project Client calls these two functions. The Output files of these three projects are written to the Bin directory.

The Build project is a makefile which rebuilds Server1, Server2 and Client. This makefile is very simple and the only thing it can do is to make "Rebuild All" for three other projects in the Debug configuration. The interesting point is that it calls the Rebase utility which sets the base addressed for Server1 and Server12 dynamic-link libraries.

Open the Server1 project in Visual Studio and build it. Do the same with Server2 and with the Client projects. Now run the Dependency Walker utility (it comes with Visual Studio and is listed in the Microsoft Visual Studio Tools menu) and open the file bin\Client.exe:

Depends Utility 1

We can see that both Server1.dll and Server2.dll have the same base address 0x10000000. That means, when Client.exe starts, one of these libraries is relocated. If the project runs under Debugger in Windows NT, the Loader shows the appropriate message in the Output window for each relocated Dll.

Now open the Build project and build it (NOTE: Ensure that one of Microsoft SDK or Visual Studio Bin directories contains Rebase.exe is available through system Path). At the end of rebuilding process we see next lines:

REBASE: Total Size of mapping 0x0000000000080000
REBASE: Range 0x0000000060000000 -0x0000000060080000
This is the Rebase utility output. Now open the bin\Client.exe in Dependency Walker:

Depends Utility 2

We can see that Rebase did it's work. The last line of the Build Makefile is:
	-@rebase.exe -b 0x60000000 Server1.dll Server2.dll
The Base addresses in two libraries are updated and now they are not relocated when the Client.exe starts.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here