Search code examples
c++msbuildlinkerbitcoinaslr

How to disable DYNAMICBASE in msbuild vcxproj


I would like to pass the linker option /DYNAMICBASE:no to the msvc linker via the vcxproj file which msbuild is using to compile the project.

If someone would like to replicate my failures I am using this vcxproj file to compile bitcoind using this command to build the project: msbuild bitcoind/bitcoind.vcxproj /p:Platform=x64 /p:Configuration=Release /t:build While the project builds successfully when run dumpbin \headers against the compiled exe I get output containing:

8160 DLL characteristics
  High Entropy Virtual Addresses
  Dynamic base
  NX compatible
  Terminal Server Aware

I have attempted to modify the vcxproj file according to "Passing /highentropyva- to CSC compiler from MSBuild command-line or project file". Note the changes below also include HighEntropyVA as I wanted to verify that it was just an issue with setting the dynamicbase.

  <PropertyGroup Label="Globals">
    <FileAlignment>
      <HighEntropyVA>False</HighEntropyVA>
    </FileAlignment>
    <Link>
      <DynamicBase>False</DynamicBase>
    </Link>
    <ProjectGuid>{D4513DDF-6013-44DC-ADCC-12EAF6D1F038}</ProjectGuid>
  </PropertyGroup>

I also attempted to get this working using an answer to "How to pass linker options to msbuild via command line?". Whereby I created the following prop file:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemDefinitionGroup>
    <Link>
       <DynamicBase>False</DynamicBase>
       <HighEntropyVA>False</HighEntropyVA>
    </Link>
  </ItemDefinitionGroup>
</Project>

and then added to the build using the command: msbuild bitcoind/bitcoind.vcxproj /p:Platform=x64 /p:Configuration=Release /t:build /p:ForceImportBeforeCppTargets=profile.props


Solution

  • To turn off /DYNAMICBASE in the .vcxproj file insert the following before the <ItemGroup> list.

      <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
        <Link>
          <RandomizedBaseAddress>false</RandomizedBaseAddress>
        </Link>
      </ItemDefinitionGroup>
    

    To have it apply to all builds, instead of just Release x64, remove the Condition= part.

    When /DYNAMICBASE is off, /HIGHENTROPYVA is ineffective, though the characteristic bit may still be set in the header. To turn it off, add <AdditionalOptions>/HIGHENTROPYVA:NO%(AdditionalOptions)</AdditionalOptions> next to the <RandomizedBaseAddress> line.