-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Implement a workaround for ld-prime hitting an assert on large addends #124721
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
Open
filipnavara
wants to merge
13
commits into
dotnet:main
Choose a base branch
from
filipnavara:ld-prime-addend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+73
−4
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0b9c22d
Implement a workaround for ld-prime hitting an assert on large addends
filipnavara b99ee70
Fix missing temporaryLabelIndex variable assignment
filipnavara 2fa03fa
Prevent dead-striping of dehydrated data by the linker
filipnavara c157abb
ltmp is special
filipnavara 4a18e9e
Name the local label differently
filipnavara be48d72
Add N_NO_DEAD_STRIP to make Xcode 16 -ld_classic happy
filipnavara 3f8dd48
Be more conservative in the largest allowed addend
filipnavara 610e0f1
Test: Use Xcode 26.2 for build
filipnavara 63cc8fd
Revert "Test: Use Xcode 26.2 for build"
filipnavara 3472934
Add comment
filipnavara 67e24b5
Address review comments: fix overflow check and add assertions
filipnavara 6f6d5c0
Apply suggestions from code review
filipnavara 84fc31e
Apply suggestions from code review
filipnavara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,8 @@ internal sealed partial class MachObjectWriter : UnixObjectWriter | |
| private readonly uint _cpuType; | ||
| private readonly uint _cpuSubType; | ||
| private readonly List<MachSection> _sections = new(); | ||
| private readonly List<SymbolDefinition> _temporaryLabels = new(); | ||
| private uint _temporaryLabelsBaseIndex; | ||
|
|
||
| // Symbol table | ||
| private readonly Dictionary<Utf8String, uint> _symbolNameToIndex = new(); | ||
|
|
@@ -482,10 +484,35 @@ protected internal override unsafe void EmitRelocation( | |
| // subtraction and the PC offset is baked into the addend. | ||
| // On x64, ld64 requires X86_64_RELOC_SUBTRACTOR + X86_64_RELOC_UNSIGNED | ||
| // for DWARF .eh_frame section. | ||
|
|
||
| // Apple ld-prime linker needs a quirk where we restrict the addend to | ||
| // signed 20-bit integer. We generate a temporary label every time the | ||
| // addend would overflow and adjust the addend to be relative to that label. | ||
| const int signedAddendBitWidth = 20; | ||
| const int signedAddendShift = (sizeof(long) * 8) - signedAddendBitWidth; | ||
| var temporaryLabelOffset = _sections[sectionIndex].TemporaryLabelOffset; | ||
| var temporaryLabelIndex = _sections[sectionIndex].TemporaryLabelIndex; | ||
| addend -= offset - temporaryLabelOffset; | ||
| if (addend != ((addend << signedAddendShift) >> signedAddendShift)) | ||
| { | ||
| addend += offset - temporaryLabelOffset; | ||
| _temporaryLabels.Add(new SymbolDefinition(sectionIndex, offset, 0, false)); | ||
| _sections[sectionIndex].TemporaryLabelOffset = temporaryLabelOffset = offset; | ||
| _sections[sectionIndex].TemporaryLabelIndex = temporaryLabelIndex = (uint)_temporaryLabels.Count; | ||
| } | ||
|
|
||
| Debug.Assert(addend >= int.MinValue && addend <= int.MaxValue); | ||
| BinaryPrimitives.WriteInt32LittleEndian( | ||
| data, | ||
| BinaryPrimitives.ReadInt32LittleEndian(data) + | ||
| (int)(addend - offset)); | ||
| (int)addend); | ||
|
filipnavara marked this conversation as resolved.
|
||
|
|
||
| // The SymbolicRelocation.Addend field is repurposed to carry the | ||
| // 1-based temporary label index (0 means no temporary label). This | ||
| // avoids introducing a side table and is consumed by | ||
| // EmitRelocationsX64/EmitRelocationsArm64 when building the | ||
| // subtractor relocation pair. | ||
| addend = temporaryLabelIndex; | ||
|
Comment on lines
+510
to
+515
Comment on lines
+510
to
+515
|
||
| } | ||
| else | ||
| { | ||
|
|
@@ -497,8 +524,8 @@ protected internal override unsafe void EmitRelocation( | |
| BinaryPrimitives.ReadInt32LittleEndian(data) + | ||
| (int)addend); | ||
| } | ||
| addend = 0; | ||
| } | ||
| addend = 0; | ||
| break; | ||
|
|
||
| default: | ||
|
|
@@ -524,6 +551,23 @@ private protected override void EmitSymbolTable( | |
| // We already emitted symbols for all non-debug sections in EmitSectionsAndLayout, | ||
| // these symbols are local and we need to account for them. | ||
| uint symbolIndex = (uint)_symbolTable.Count; | ||
| _temporaryLabelsBaseIndex = symbolIndex; | ||
| Utf8StringBuilder temporaryLabelNameBuilder = new Utf8StringBuilder(); | ||
| foreach (SymbolDefinition definition in _temporaryLabels) | ||
| { | ||
| temporaryLabelNameBuilder.Clear(); | ||
| temporaryLabelNameBuilder.Append("ltemp"u8).Append((int)(symbolIndex - _temporaryLabelsBaseIndex)); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| MachSection section = _sections[definition.SectionIndex]; | ||
| _symbolTable.Add(new MachSymbol | ||
| { | ||
| Name = temporaryLabelNameBuilder.ToUtf8String(), | ||
| Section = section, | ||
| Value = section.VirtualAddress + (ulong)definition.Value, | ||
| Descriptor = N_NO_DEAD_STRIP, | ||
| Type = N_SECT, | ||
| }); | ||
| symbolIndex++; | ||
| } | ||
| _dySymbolTable.LocalSymbolsIndex = 0; | ||
| _dySymbolTable.LocalSymbolsCount = symbolIndex; | ||
|
|
||
|
|
@@ -658,11 +702,20 @@ private void EmitRelocationsX64(int sectionIndex, List<SymbolicRelocation> reloc | |
| } | ||
| else if (symbolicRelocation.Type == IMAGE_REL_BASED_RELPTR32 && IsEhFrameSection(sectionIndex)) | ||
| { | ||
| // Addend is used to encode the temporary label index for ld-prime quirk. See | ||
| // EmitRelocation for details. | ||
| Debug.Assert(symbolicRelocation.Addend == 0 || symbolicRelocation.Addend <= _temporaryLabels.Count); | ||
| uint baseSymbolIndex = | ||
| symbolicRelocation.Addend != 0 ? | ||
| (uint)(_temporaryLabelsBaseIndex + symbolicRelocation.Addend - 1) : | ||
| (uint)sectionIndex; | ||
| Debug.Assert(baseSymbolIndex < (uint)_symbolTable.Count); | ||
|
|
||
| sectionRelocations.Add( | ||
| new MachRelocation | ||
| { | ||
| Address = (int)symbolicRelocation.Offset, | ||
| SymbolOrSectionIndex = (uint)sectionIndex, | ||
| SymbolOrSectionIndex = baseSymbolIndex, | ||
|
filipnavara marked this conversation as resolved.
|
||
| Length = 4, | ||
| RelocationType = X86_64_RELOC_SUBTRACTOR, | ||
| IsExternal = true, | ||
|
|
@@ -821,12 +874,21 @@ private void EmitRelocationsArm64(int sectionIndex, List<SymbolicRelocation> rel | |
| } | ||
| else if (symbolicRelocation.Type == IMAGE_REL_BASED_RELPTR32) | ||
| { | ||
| // Addend is used to encode the temporary label index for ld-prime quirk. See | ||
| // EmitRelocation for details. | ||
| Debug.Assert(symbolicRelocation.Addend == 0 || symbolicRelocation.Addend <= _temporaryLabels.Count); | ||
| uint baseSymbolIndex = | ||
| symbolicRelocation.Addend != 0 ? | ||
| (uint)(_temporaryLabelsBaseIndex + symbolicRelocation.Addend - 1) : | ||
| (uint)sectionIndex; | ||
| Debug.Assert(baseSymbolIndex < _symbolTable.Count); | ||
|
|
||
|
|
||
| // This one is tough... needs to be represented by ARM64_RELOC_SUBTRACTOR + ARM64_RELOC_UNSIGNED. | ||
| sectionRelocations.Add( | ||
| new MachRelocation | ||
| { | ||
| Address = (int)symbolicRelocation.Offset, | ||
| SymbolOrSectionIndex = (uint)sectionIndex, | ||
| SymbolOrSectionIndex = baseSymbolIndex, | ||
| Length = 4, | ||
| RelocationType = ARM64_RELOC_SUBTRACTOR, | ||
| IsExternal = true, | ||
|
|
@@ -975,6 +1037,13 @@ private sealed class MachSection | |
| public Stream Stream => dataStream; | ||
| public byte SectionIndex { get; set; } | ||
|
|
||
| // Variables used for tracking a temporary label created within the section | ||
| // for the purpose of creating relocations with smaller addends. The emitted | ||
| // addends are relative to the last temporary label instead of the section | ||
| // start. | ||
| public long TemporaryLabelOffset { get; set; } | ||
| public uint TemporaryLabelIndex { get; set; } | ||
|
|
||
| public static int HeaderSize => 80; // 64-bit section | ||
|
|
||
| public MachSection(string segmentName, string sectionName, Stream stream) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.