Share via


Choosing a Base Address

When Windows loads a module, it checks whether it can use the base address indicated by the module. If using this base address would cause address conflicts, Windows relocates the module to a different address. This relocation consumes extra time during module load.

To avoid this problem, you can give each module in a user-mode program a unique base address, taking into account the size of each module.

There are three ways to control the base address of the binaries that you build:

  • Use the DLLBASE or UMBASE macros.

  • Use the coffbase.txt file, along with the COFFBASE and COFFBASE_TXT_FILE macros.

  • Use a rebase tool after you build your binaries.

If you set the base address through DLLBASE or UMBASE, the coffbase.txt file is ignored.

If none of these methods are used, the linker uses 0x01000000 as the base address.

Note   Windows relocates all kernel-mode components when they are loaded. Therefore, you do not have to choose the base address of any kernel-mode build product.

The UMBASE and DLLBASE Macros

If you are building a user-mode program (where TARGETTYPE equals PROGRAM or PROGLIB), you can control the base address with the UMBASE macro.

If you are building a dynamic link library (where TARGETTYPE equals DYNLINK), you can control the base address with the DLLBASE macro.

For example:

DLLBASE=0x04000000

If UMBASE is not defined and the base address cannot be determined through COFFBASE_TXT_FILE, Windows uses the default base address of 0x01000000.

For kernel-mode code, the analogous macros DRIVERBASE and HALBASE are available. However, because kernel-mode binaries are relocated when they are loaded, these macros are not especially useful.

The Coffbase.txt File

The coffbase.txt file contains information about the base addresses of various build products. In the Windows Driver Kit (WDK), the default location of this file is %DDKROOT%\bin\coffbase.txt.

You can specify a different location or even a different file name by setting the COFFBASE_TXT_FILE macro equal to the full path and file name of the alternate file.

Note   If you are using this method to control your base addresses, it is recommended that you use COFFBASE_TXT_FILE to indicate a location other than the default. If you edit the coffbase.txt file in its default location and then reinstall the WDK, the file will be overwritten.

The linker opens coffbase.txt and searches for the name of the executable file that you are building. Use the COFFBASE macro to specify the name that will be looked up in coffbase.txt.

If you do not use COFFBASE to specify a name, the name is determined as follows:

  • If a DYNLINK is being built, the name is determined by the value of TARGETNAME.

  • If a PROGLIB or PROGRAM is being built, the name "usermode" is used.

The contents of the coffbase.txt file must be arranged in three columns. The first column contains the name of the executable file. The second contains the base address of the image; which is the beginning of the address range where the executable image will be located in memory. The last contains the maximum size of the image. Numbers can be prefixed with 0x (hexadecimal) or not prefixed at all (decimal).

There should also be one entry with the name "usermode."

Here is an example:

usermode      0x60000000    0x20000000
MyDll         0x70000000    0x00010000
AnotherDll2   0x70010000    0x00010000

In this example, if you have set COFFBASE to MyDll, the linker uses a starting address of 0x70000000 and a maximum size of 0x00010000.

Natural Variations

Any specified base address or size is just an educated guess. Different architectures and build environments generate binaries of different sizes--thus, for example, the coffbase.txt file that you use in an x86 free build environment may be inappropriate for an Itanium checked build environment. For best results, make an educated guess when you create the coffbase.txt file, and then after the build is complete, run the rebase.exe tool to pack all your binaries together in an optimal manner.

 

 

Send comments about this topic to Microsoft

Build date: 5/3/2011