Skip to content

Instantly share code, notes, and snippets.

@sunho
Created May 23, 2022 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunho/1a0a74dc5ce29da04fcf88bfbd47f973 to your computer and use it in GitHub Desktop.
Save sunho/1a0a74dc5ce29da04fcf88bfbd47f973 to your computer and use it in GitHub Desktop.
diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/aarch64.h b/llvm/include/llvm/ExecutionEngine/JITLink/aarch64.h
index 994ce783b058..391b9f198fe7 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/aarch64.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/aarch64.h
@@ -25,6 +25,13 @@ enum EdgeKind_aarch64 : Edge::Kind {
/// Set a CALL immediate field to bits [27:2] of X = Target - Fixup + Addend
R_AARCH64_CALL26 = Edge::FirstRelocation,
+ /// Set an ADRP immediate value to bits [32:12] of the X
+ R_AARCH64_ADR_PREL_PG_HI21,
+
+ /// Set an ADD immediate value to bits [11:0] of X. No overflow check.
+ R_AARCH64_ADD_ABS_LO12_NC,
+
+ R_AARCH64_PREL32,
};
/// Returns a string name for the given aarch64 edge. For debugging purposes
diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h
index dc080cfc79d1..ff8040132c0a 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h
@@ -101,6 +101,11 @@ public:
return *this;
}
+ ExecutorAddr &operator&(const ExecutorAddrDiff Mask) {
+ Addr &= Mask;
+ return *this;
+ }
+
private:
uint64_t Addr = 0;
};
diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp
index dd3eb97c21a0..137fdb79ae53 100644
--- a/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/ELF_aarch64.cpp
@@ -17,6 +17,8 @@
#include "llvm/ExecutionEngine/JITLink/aarch64.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Support/MathExtras.h"
+#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
+#include <iostream>
#define DEBUG_TYPE "jitlink"
@@ -63,6 +65,27 @@ private:
*(little32_t *)FixupPtr = FixedInstr;
break;
}
+ case R_AARCH64_ADR_PREL_PG_HI21: {
+ uint32_t RawInstr = *(little32_t *)FixupPtr;
+ int64_t Value = ((E.getTarget().getAddress() + E.getAddend()) & ~0xFFFLL) - (FixupAddress & ~0xFFFLL);
+ if (!isInt<33>(Value))
+ return makeTargetOutOfRangeError(G, B, E);
+
+ uint32_t Imm32_12 = static_cast<uint32_t>(Value >> 12) & 0x1fffff;
+ uint32_t ImmLo = (Imm32_12 & 0x3) << 29;
+ uint32_t ImmHi = (Imm32_12 >> 2) << 5;
+ uint32_t Mask = ~(((0x1FFFFC >> 2) << 5) | (0x3U << 29));
+
+ RawInstr &= Mask;
+ *(little32_t *)FixupPtr = RawInstr | ImmLo | ImmHi;
+ break;
+ }
+ case R_AARCH64_PREL32: {
+ break;
+ }
+ case R_AARCH64_ADD_ABS_LO12_NC: {
+ break;
+ }
}
return Error::success();
}
@@ -77,6 +100,12 @@ private:
switch (Type) {
case ELF::R_AARCH64_CALL26:
return EdgeKind_aarch64::R_AARCH64_CALL26;
+ case ELF::R_AARCH64_ADR_PREL_PG_HI21:
+ return EdgeKind_aarch64::R_AARCH64_ADR_PREL_PG_HI21;
+ case ELF::R_AARCH64_ADD_ABS_LO12_NC:
+ return EdgeKind_aarch64::R_AARCH64_ADD_ABS_LO12_NC;
+ case ELF::R_AARCH64_PREL32:
+ return EdgeKind_aarch64::R_AARCH64_PREL32;
}
return make_error<JITLinkError>("Unsupported aarch64 relocation:" +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment