Skip to content

Commit

Permalink
Add support for --image-base and --dynamicbase linker flags.
Browse files Browse the repository at this point in the history
Closes #103 and #104.
  • Loading branch information
alexrp committed Jan 4, 2024
1 parent ed17ad1 commit 88429ec
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<ItemGroup>
<Using Include="System.Diagnostics.CodeAnalysis" />
<Using Include="System.Globalization" />
<Using Include="System.Runtime.CompilerServices" />
<Using Include="System.Runtime.InteropServices" />
</ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions doc/configuration/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ historical reasons.
* `RelocationHardening` (`true`, `false`): Enable/disable marking relocations as
read-only. This has security benefits, especially in combination with
`EagerBinding`. Defaults to `true`.
* `ImageBase`: The location in memory that the binary should be loaded at. Only
takes effect at run time if `DynamicImageBase` is `false`. Unset by default.
* `DynamicImageBase` (`true`, `false`): Enable/disable
[ASLR](https://en.wikipedia.org/wiki/Address_space_layout_randomization), i.e.
randomization of the image base at run time. Only affects Windows binaries.
Defaults to `true`.
* `Sanitizers`: A semicolon-separated list of
[sanitizers](https://github.com/google/sanitizers) to instrument code with.
Currently, only `thread` is supported. Unset by default.
Expand Down
24 changes: 22 additions & 2 deletions src/sdk/ZigCompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,21 @@ public string Configuration
[Required]
public bool DocumentationAnalysis { get; set; }

[Required]
public bool DynamicImageBase { get; set; }

[Required]
public bool EagerBinding { get; set; }

[Required]
public bool FastMath { get; set; }

public int ImageBase
{
get => _imageBase ?? 0;
set => _imageBase = value;
}

[Required]
public ITaskItem[] IncludeDirectories { get; set; } = null!;

Expand Down Expand Up @@ -158,10 +167,14 @@ public string SymbolVisibility
[Required]
public int WarningLevel { get; set; }

private static readonly CultureInfo _culture = CultureInfo.InvariantCulture;

private ZigCompilerMode _compilerMode;

private ZigConfiguration _configuration;

private int? _imageBase;

private ZigOutputType _outputType;

private ZigReleaseMode _releaseMode;
Expand Down Expand Up @@ -556,13 +569,20 @@ void TryAppendWarningSwitch(string name)
if (!RelocationHardening)
builder.AppendSwitch(isZig ? "-z norelro" : "-Wl,-z,norelro");

if (_imageBase is { } ib)
builder.AppendSwitchIfNotNull(
isZig ? "--image-base 0x" : "-Wl,--image-base,0x", ib.ToString("x", _culture));

if (!DynamicImageBase)
builder.AppendSwitch(isZig ? "--no-dynamicbase" : "-Wl,--no-dynamicbase");

builder.AppendSwitch(isZig ? "-z origin" : "-Wl,-z,origin");
builder.AppendSwitchIfNotNull(isZig ? "-rpath " : "-Wl,-rpath,", "$ORIGIN");

// TODO: https://github.com/vezel-dev/zig-sdk/issues/8

builder.AppendFileNamesIfNotNull(Sources, " ");
builder.AppendFileNamesIfNotNull(LibraryReferences, " ");
builder.AppendFileNamesIfNotNull(Sources, delimiter: " ");
builder.AppendFileNamesIfNotNull(LibraryReferences, delimiter: " ");

foreach (var directory in LinkerDirectories)
builder.AppendSwitchIfNotNull("-L ", directory);
Expand Down
2 changes: 2 additions & 0 deletions src/sdk/build/Vezel.Zig.Sdk.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@
Deterministic="$(Deterministic)"
DisableWarnings="$(DisableWarnings)"
DocumentationAnalysis="$(DocumentationAnalysis)"
DynamicImageBase="$(DynamicImageBase)"
EagerBinding="$(EagerBinding)"
EnvironmentVariables="ZIG_GLOBAL_CACHE_DIR=$(CachePath)/global; ZIG_LOCAL_CACHE_DIR=$(CachePath)/local"
FastMath="$(FastMath)"
ImageBase="$(ImageBase)"
IncludeDirectories="@(IncludeDirectory)"
LanguageStandard="$(LanguageStandard)"
LibraryIncludeDirectories="@(LibraryIncludeDirectory)"
Expand Down
1 change: 1 addition & 0 deletions src/sdk/build/Vezel.Zig.Sdk.Defaults.targets
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
</PropertyGroup>

<PropertyGroup>
<DynamicImageBase Condition="'$(DynamicImageBase)' == ''">true</DynamicImageBase>
<EagerBinding Condition="'$(EagerBinding)' == ''">true</EagerBinding>
<FastMath Condition="'$(FastMath)' == ''">false</FastMath>
<!-- TODO: https://github.com/vezel-dev/zig-sdk/issues/33 -->
Expand Down
2 changes: 2 additions & 0 deletions src/sdk/build/Vezel.Zig.Sdk.Test.targets
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
Deterministic="$(Deterministic)"
DisableWarnings="$(DisableWarnings)"
DocumentationAnalysis="$(DocumentationAnalysis)"
DynamicImageBase="$(DynamicImageBase)"
EagerBinding="$(EagerBinding)"
EnvironmentVariables="ZIG_GLOBAL_CACHE_DIR=$(CachePath)/global; ZIG_LOCAL_CACHE_DIR=$(CachePath)/local"
FastMath="$(FastMath)"
ImageBase="$(ImageBase)"
IncludeDirectories="@(IncludeDirectory)"
LanguageStandard="$(LanguageStandard)"
LibraryIncludeDirectories="@(LibraryIncludeDirectory)"
Expand Down

0 comments on commit 88429ec

Please sign in to comment.