Skip to content

[NativeAOT] Do not resolve an out-of-range arm64 BRANCH26 within a section - #137129

Merged
jkotas merged 2 commits into
dotnet:mainfrom
SHAZAM-Analytics:fix/ilc-arm64-branch26-range
Nov 8, 2026
Merged

jkotas merged 2 commits into
dotnet:mainfrom
SHAZAM-Analytics:fix/ilc-arm64-branch26-range

Conversation

@SHAZAM-Analytics

@SHAZAM-Analytics SHAZAM-Analytics commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Related to #103073

Problem

ObjectWriter.EmitOrResolveRelocation resolves a PC-relative relocation itself when its target is already defined in the same section, rather than emitting it for the linker. For IMAGE_REL_BASED_THUMB_BRANCH24 it checks that the displacement fits and emits the relocation when it does not. For IMAGE_REL_BASED_ARM64_BRANCH26 there is no such check, and Relocation.PutArm64Rel28 only asserts the +/-128 MB range in debug builds. A release ILC therefore masks the displacement to 26 bits and writes a B/BL to the wrong address. The object links, and the program branches into unrelated code at run time.

This is reached when one arm64 code section is larger than 128 MB, which happens for large NativeAOT applications on win-arm64 (all managed code is one COFF section) and on linux-arm64.

Fix

Apply the same range check to ARM64_BRANCH26 as to THUMB_BRANCH24: when the displacement does not fit, emit the relocation, so the linker either places a range-extension thunk or reports the overflow. PutArm64Rel28 keeps its debug assert, like the other encoders in Relocation.cs; the object writer now checks the range before it resolves the branch itself.

Testing

No test added: reaching the out-of-range case needs more than 128 MB of arm64 code in one section, which is too large and slow to compile in CI. We found it building a large NativeAOT application for win-arm64 and carry this change in those builds.

EmitOrResolveRelocation resolved a same-section ARM64_BRANCH26 itself
without checking the +/-128 MB range, and PutArm64Rel28 only asserted the
range in debug builds, so a release ILC wrote a branch to the wrong target.

Emit the relocation for the linker when the displacement does not fit, as
is already done for THUMB_BRANCH24, and throw from PutArm64Rel28 instead of
encoding a truncated displacement.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 7, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@SHAZAM-Analytics

Copy link
Copy Markdown
Contributor Author

This fix is needed in servicing too: shipped .NET 10 and 11 NativeAOT apps hit it on arm64 today. Once it merges, could an area owner backport it to release/10.0 and release/11.0? I opened backport PRs earlier by mistake and they were rightly closed.

The out-of-range decision stays with EmitOrResolveRelocation, which leaves a
same-section BRANCH26 that does not fit to the linker.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The ARM64 branch-range relocation change has no active review issues.

No open issues 🎉

What changed in this PR

This PR prevents NativeAOT from encoding out-of-range ARM64 branches to targets in the same section. It leaves those relocations for the linker to handle instead of silently truncating the displacement.

Changes:

  • Add an ARM64 branch-range check alongside the existing Thumb branch check.
  • Make FitsInArm64Rel28 accessible to the object writer while retaining the encoder’s debug assertion.
File Description
src/​coreclr/​tools/​Common/​Compiler/​ObjectWriter/​ObjectWriter.cs Emit a linker relocation when an ARM64 branch displacement is out of range.
src/​coreclr/​tools/​Common/​Compiler/​DependencyAnalysis/​Relocation.cs Expose the existing ARM64 range-check helper for reuse.

Review effort: Balanced

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@jkotas
jkotas merged commit 614e7da into dotnet:main Nov 8, 2026
100 of 102 checks passed
@jkotas

jkotas commented Nov 8, 2026

Copy link
Copy Markdown
Member

This fix is needed in servicing too

We will take all 3 related fixes together for servicing.

Would .NET 11 only backport be enough for your app?

@SHAZAM-Analytics

Copy link
Copy Markdown
Contributor Author

Would .NET 11 only backport be enough for your app?

Yes, a .NET 11 only backport is enough for our app.

A data point on the linux-arm64 case in the description. On stock 10.0.14, an app with more than 128 MB of managed code links without any error and then crashes at startup. With this change it becomes a link-time "relocation truncated" error instead, because GNU ld and lld place range extension thunks only between input sections. #137130 now covers ELF arm64 as well, so such an app links and runs.

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 9, 2026
jkotas added a commit that referenced this pull request Nov 20, 2026
…37652)

Fixes Issues #103073 and #136687

Main PRs:
- [#137129](#137129) - ARM64
out-of-range branch relocation
- [#137603](#137603) - Split large
ARM64 code sections so linkers can place range-extension thunks
- [#137602](#137602) - Preserve
relocations when folding block-store addresses

## Customer Impact

- [x] Customer reported
- [ ] Found internally

# Description

Large NativeAOT ARM64 binaries can exceed the range of direct branches.
This change splits managed code into sections so linkers can place
range-extension thunks, preserves out-of-range branch relocations, and
handles those thunks during runtime method lookup, thread suspension,
and unwinding. It also avoids folding relocatable constants into
block-store addresses on Arm, LoongArch64, and RISC-V.

# Customer Impact

Without the fix, linking large NativeAOT ARM64 applications can fail
with branch-out-of-range errors, or execution can fail when runtime code
encounters linker-inserted thunks. The included relocation fix also
prevents incorrect addresses and potential faults when copying
preinitialized struct data on affected architectures.

# Regression

No.

# Testing

- Customer validated the fix
- Added smoke tests cover the thunk and relocation scenarios.

# Risk

Moderate. Relatively large change for servicing, it may not fix the
scenario completely.

---------

Co-authored-by: Services <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Copilot App <[email protected]>
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