Skip to content

Fix conditions mismatch for gc-sections with lld #115964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

illnyang
Copy link
Contributor

Judging by the changes made in 410aa0a, gc-sections optimization should be enabled:

  • for Unix targets
  • when using lld newer than 12

Both conditions are checked when adding linker arguments, but only the latter condition is checked when writing the linker script. This PR adds the missing conditions.

Relevant code

<ItemGroup Condition="'$(_targetOS)' != 'win' and '$(_IsApplePlatform)' != 'true'">
<CustomLinkerArg Include="-Wl,--discard-all" />
<CustomLinkerArg Include="-Wl,--gc-sections" Condition="'$(LinkerFlavor)' == '' or '$(LinkerFlavor)' == 'bfd' or '$(LinkerFlavor)' == 'lld'" />
<CustomLinkerArg Include="-Wl,-T,&quot;$(NativeIntermediateOutputPath)sections.ld&quot;" Condition="'$(LinkerFlavor)' == 'lld' and '$(_LinkerVersion)' &gt; '12'" />
</ItemGroup>

<!-- write linker script for lld (13+) to retain the __modules section -->
<WriteLinesToFile File="$(NativeIntermediateOutputPath)sections.ld" Lines="OVERWRITE_SECTIONS { __modules : { KEEP(*(__modules)) } }" Overwrite="true" Condition="'$(LinkerFlavor)' == 'lld' and '$(_LinkerVersion)' &gt; '12'" />

Reproduction steps

This discrepancy is not observable in standard use cases. However, overriding the relevant properties will result in a vague error message unrelated to the actual issue.

> dotnet publish -r win-x64 -p:PublishAot=true -p:LinkerFlavor=lld project.csproj

...\Microsoft.NETCore.Native.targets(364,156): error MSB4086: 
A numeric comparison was attempted on "$(_LinkerVersion)" that evaluates to "" instead of a number,
in condition "'$(LinkerFlavor)' == 'lld' and '$(_LinkerVersion)' > '12'".

Predictably, setting the _LinkerVersion property next will cause sections.ld file to be written but left unused:

> dotnet publish -r win-x64 -p:PublishAot=true -p:LinkerFlavor=lld -p:_LinkerVersion=13 project.csproj

Build succeeded (...)

> cat obj\Release\net8.0\win-x64\native\sections.ld

OVERWRITE_SECTIONS { __modules : { KEEP(*(__modules)) } }

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 24, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 24, 2025
@jkotas jkotas added area-NativeAOT-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels May 24, 2025
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@MichalStrehovsky
Copy link
Member

/ba-g known android timeouts

@MichalStrehovsky MichalStrehovsky merged commit aa902b7 into dotnet:main May 27, 2025
95 of 97 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants