From b8f4a93633e714a2b571f69f675046c2302c181c Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Mon, 17 Nov 2025 11:28:49 +1100 Subject: [PATCH] [JITLink][MachO] Endianness fix for relocation bit-field handling. When reading r_pcrel, r_length, r_extern and r_type fields of any_relocation_info we need to take endianness into account. This is an alternative fix the issue described in for https://github.com/llvm/llvm-project/pull/167902. This fix avoids external calls and further dependence on MachOObject file. --- .../JITLink/MachOLinkGraphBuilder.h | 16 ++++++++++++---- llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp | 7 ++++--- .../lib/ExecutionEngine/JITLink/MachO_x86_64.cpp | 5 +++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h index 91021e457532e..e7db1cdc1a3d6 100644 --- a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h +++ b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h @@ -163,6 +163,7 @@ class MachOLinkGraphBuilder { static bool isDebugSection(const NormalizedSection &NSec); static bool isZeroFillSection(const NormalizedSection &NSec); + template MachO::relocation_info getRelocationInfo(const object::relocation_iterator RelItr) { MachO::any_relocation_info ARI = @@ -170,10 +171,17 @@ class MachOLinkGraphBuilder { MachO::relocation_info RI; RI.r_address = ARI.r_word0; RI.r_symbolnum = ARI.r_word1 & 0xffffff; - RI.r_pcrel = (ARI.r_word1 >> 24) & 1; - RI.r_length = (ARI.r_word1 >> 25) & 3; - RI.r_extern = (ARI.r_word1 >> 27) & 1; - RI.r_type = (ARI.r_word1 >> 28); + if (ObjE == endianness::native) { + RI.r_pcrel = (ARI.r_word1 >> 24) & 1; + RI.r_length = (ARI.r_word1 >> 25) & 3; + RI.r_extern = (ARI.r_word1 >> 27) & 1; + RI.r_type = (ARI.r_word1 >> 28); + } else { + RI.r_pcrel = (ARI.r_word1 >> 28) & 0x1; + RI.r_length = (ARI.r_word1 >> 26) & 0x3; + RI.r_extern = (ARI.r_word1 >> 25) & 0x1; + RI.r_type = (ARI.r_word1 >> 24) & 0xf; + } return RI; } diff --git a/llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp b/llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp index f79478038c5cb..52d13d5c18e8e 100644 --- a/llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp @@ -165,7 +165,7 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder { return make_error("arm64 SUBTRACTOR without paired " "UNSIGNED relocation"); - auto UnsignedRI = getRelocationInfo(UnsignedRelItr); + auto UnsignedRI = getRelocationInfo(UnsignedRelItr); if (SubRI.r_address != UnsignedRI.r_address) return make_error("arm64 SUBTRACTOR and paired UNSIGNED " @@ -288,7 +288,8 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder { for (auto RelItr = S.relocation_begin(), RelEnd = S.relocation_end(); RelItr != RelEnd; ++RelItr) { - MachO::relocation_info RI = getRelocationInfo(RelItr); + MachO::relocation_info RI = + getRelocationInfo(RelItr); // Validate the relocation kind. auto MachORelocKind = getRelocationKind(RI); @@ -337,7 +338,7 @@ class MachOLinkGraphBuilder_arm64 : public MachOLinkGraphBuilder { if (RelItr == RelEnd) return make_error("Unpaired Addend reloc at " + formatv("{0:x16}", FixupAddress)); - RI = getRelocationInfo(RelItr); + RI = getRelocationInfo(RelItr); MachORelocKind = getRelocationKind(RI); if (!MachORelocKind) diff --git a/llvm/lib/ExecutionEngine/JITLink/MachO_x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/MachO_x86_64.cpp index 27209a82eb5f4..79aee71528b4c 100644 --- a/llvm/lib/ExecutionEngine/JITLink/MachO_x86_64.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/MachO_x86_64.cpp @@ -140,7 +140,7 @@ class MachOLinkGraphBuilder_x86_64 : public MachOLinkGraphBuilder { return make_error("x86_64 SUBTRACTOR without paired " "UNSIGNED relocation"); - auto UnsignedRI = getRelocationInfo(UnsignedRelItr); + auto UnsignedRI = getRelocationInfo(UnsignedRelItr); if (SubRI.r_address != UnsignedRI.r_address) return make_error("x86_64 SUBTRACTOR and paired UNSIGNED " @@ -264,7 +264,8 @@ class MachOLinkGraphBuilder_x86_64 : public MachOLinkGraphBuilder { for (auto RelItr = S.relocation_begin(), RelEnd = S.relocation_end(); RelItr != RelEnd; ++RelItr) { - MachO::relocation_info RI = getRelocationInfo(RelItr); + MachO::relocation_info RI = + getRelocationInfo(RelItr); // Find the address of the value to fix up. auto FixupAddress = SectionAddress + (uint32_t)RI.r_address;