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
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:
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 contains a number of projects. There are two Win32 Dlls
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
Build project is a makefile which rebuilds
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
utility which sets the base addressed for
Server1 project in Visual Studio and build it. Do the same
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:
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
utility output. Now open the bin\Client.exe in Dependency Walker:
We can see that
did it's work. The last line of the Build Makefile is:
-@rebase.exe -b 0x60000000 Server1.dll Server2.dll
Base addresses in two libraries are updated and now they are not relocated when