Repository navigation
[NativeAOT] Do not resolve an out-of-range arm64 BRANCH26 within a section - #137129
Conversation
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.
|
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. |
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
|
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 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 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
FitsInArm64Rel28accessible 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
We will take all 3 related fixes together for servicing. 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. |
…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 <b8s@hotmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Related to #103073
Problem
ObjectWriter.EmitOrResolveRelocationresolves a PC-relative relocation itself when its target is already defined in the same section, rather than emitting it for the linker. ForIMAGE_REL_BASED_THUMB_BRANCH24it checks that the displacement fits and emits the relocation when it does not. ForIMAGE_REL_BASED_ARM64_BRANCH26there is no such check, andRelocation.PutArm64Rel28only asserts the +/-128 MB range in debug builds. A release ILC therefore masks the displacement to 26 bits and writes aB/BLto 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_BRANCH26as toTHUMB_BRANCH24: when the displacement does not fit, emit the relocation, so the linker either places a range-extension thunk or reports the overflow.PutArm64Rel28keeps its debug assert, like the other encoders inRelocation.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.