none
Load C++ DLL At Specific Range Of Address RRS feed

  • Question

  • I want to develop a C++ dynamic library, a DLL. It targets x64. But when the application that uses this DLL runs, I want the DLL to be loaded within range of signed 32 bit integer memory space.

    How to do that?

    Tuesday, December 3, 2019 2:23 PM

Answers

  • You can specify the base address for the DLL using linker options.  You would also need to turn of ASLR and probably prevent the OS from relocating the DLL if your chosen base address is unavailable.

    See linker option  /BASE (Base Address) which says -

    "The /BASE option sets a base address for the program, overriding the default location for an .exe or DLL file. The default base address for an .exe file is 0x400000 for 32-bit images or 0x140000000 for 64-bit images. For a DLL, the default base address is 0x10000000 for 32-bit images or 0x180000000 for 64-bit images. On operating systems that do not support address space layout randomization (ASLR), or when the /DYNAMICBASE:NO option was set, the operating system first attempts to load a program at its specified or default base address. If sufficient space is not available there, the system relocates the program. To prevent relocation, use the /FIXED option."

    Tuesday, December 3, 2019 3:24 PM

All replies

  • Hello,

    a DLL is loaded into the virtual address space of the application. It is normally based at default address 0x10000000. If there are many DLLs, then the DLLs are relocated.

    Maybe modifying the base address of your DLL can solve your need. Have a look at this: https://www.codeproject.com/Articles/35829/Modify-the-Base-Addresses-for-a-DLL-Files-Series

    Regards, Guido

    Tuesday, December 3, 2019 2:57 PM
  • You can specify the base address for the DLL using linker options.  You would also need to turn of ASLR and probably prevent the OS from relocating the DLL if your chosen base address is unavailable.

    See linker option  /BASE (Base Address) which says -

    "The /BASE option sets a base address for the program, overriding the default location for an .exe or DLL file. The default base address for an .exe file is 0x400000 for 32-bit images or 0x140000000 for 64-bit images. For a DLL, the default base address is 0x10000000 for 32-bit images or 0x180000000 for 64-bit images. On operating systems that do not support address space layout randomization (ASLR), or when the /DYNAMICBASE:NO option was set, the operating system first attempts to load a program at its specified or default base address. If sufficient space is not available there, the system relocates the program. To prevent relocation, use the /FIXED option."

    Tuesday, December 3, 2019 3:24 PM
  • Why?  What's the point?  A question like this usually indicates a misunderstanding of how addressing works.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, December 4, 2019 7:52 AM