From 1f3d4525d87e9cd69c18098449a21ed0199fc459 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Wed, 12 Jan 2022 17:53:13 +1100 Subject: [PATCH] [JITLink][AArch64] Use R-X permissions for the GOT. This consistent with ld64's treatment of the GOT, but the main aim here is a short-term workaround for a bad interaction between stub code sequences and memory layout: Stubs use LDRLiteral19 relocations to reference the GOT, but BasicLayout currently puts RW- segments between R-- and R-X segments -- a large RW- segment (or a large R-- for that matter) can cause the relocation to fail with an out-of-range error. Putting the GOT in R-X fixes this efficiently in practice. A more robust fix will be to use a longer code sequence to materialize the GOT pointer and then rewrite the stub to use a shorter sequence where possible. --- llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp | 2 +- .../MachO_arm64_stub_to_got_distance_stress_test.s | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_stub_to_got_distance_stress_test.s diff --git a/llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp b/llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp index 844e76a..45e70af 100644 --- a/llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp @@ -475,7 +475,7 @@ public: private: Section &getGOTSection() { if (!GOTSection) - GOTSection = &G.createSection("$__GOT", MemProt::Read); + GOTSection = &G.createSection("$__GOT", MemProt::Read | MemProt::Exec); return *GOTSection; } diff --git a/llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_stub_to_got_distance_stress_test.s b/llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_stub_to_got_distance_stress_test.s new file mode 100644 index 0000000..d9a8cae --- /dev/null +++ b/llvm/test/ExecutionEngine/JITLink/AArch64/MachO_arm64_stub_to_got_distance_stress_test.s @@ -0,0 +1,17 @@ +# RUN: llvm-mc -triple=arm64-apple-darwin19 -filetype=obj -o %t.o %s +# RUN: llvm-jitlink -noexec -phony-externals %t.o +# +# Use RW- empty space sufficient to push the R-- and R-X segments more than +# 2^20 bytes apart. This will cause the LDRLiteral19 relocations from the STUB +# section to the GOT to overflow if not handled correctly. + + .section __TEXT,__text,regular,pure_instructions + .ios_version_min 7, 0 sdk_version 16, 0 + .globl _main + .p2align 2 +_main: + b _foo + + .comm _empty_space,2097152,0 + +.subsections_via_symbols -- 2.7.4