Skip to content

Commit 142168f

Browse files
[JITLink][AArch32] Multi-stub support for armv7/thumbv7
1 parent 773fc10 commit 142168f

File tree

5 files changed

+274
-59
lines changed

5 files changed

+274
-59
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/aarch32.h

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -318,64 +318,31 @@ inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
318318
llvm_unreachable("Relocation must be of class Data, Arm or Thumb");
319319
}
320320

321-
/// Stubs builder for v7 emits non-position-independent Thumb stubs.
322-
///
323-
/// Right now we only have one default stub kind, but we want to extend this
324-
/// and allow creation of specific kinds in the future (e.g. branch range
325-
/// extension or interworking).
326-
///
327-
/// Let's keep it simple for the moment and not wire this through a GOT.
328-
///
329-
class StubsManager_v7 : public TableManager<StubsManager_v7> {
321+
/// Stubs builder for v7 emits non-position-independent Arm and Thumb stubs.
322+
class StubsManager_v7 {
330323
public:
331324
StubsManager_v7() = default;
332325

333326
/// Name of the object file section that will contain all our stubs.
334327
static StringRef getSectionName() {
335-
return "__llvm_jitlink_aarch32_STUBS_Thumbv7";
328+
return "__llvm_jitlink_aarch32_STUBS_v7";
336329
}
337330

338331
/// Implements link-graph traversal via visitExistingEdges().
339-
bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
340-
if (E.getTarget().isDefined())
341-
return false;
342-
343-
switch (E.getKind()) {
344-
case Thumb_Call:
345-
case Thumb_Jump24: {
346-
DEBUG_WITH_TYPE("jitlink", {
347-
dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
348-
<< B->getFixupAddress(E) << " (" << B->getAddress() << " + "
349-
<< formatv("{0:x}", E.getOffset()) << ")\n";
350-
});
351-
E.setTarget(this->getEntryForTarget(G, E.getTarget()));
352-
return true;
353-
}
354-
}
355-
return false;
356-
}
357-
358-
/// Create a branch range extension stub with Thumb encoding for v7 CPUs.
359-
Symbol &createEntry(LinkGraph &G, Symbol &Target);
332+
bool visitEdge(LinkGraph &G, Block *B, Edge &E);
360333

361334
private:
362-
/// Create a new node in the link-graph for the given stub template.
363-
template <size_t Size>
364-
Block &addStub(LinkGraph &G, const uint8_t (&Code)[Size],
365-
uint64_t Alignment) {
366-
ArrayRef<char> Template(reinterpret_cast<const char *>(Code), Size);
367-
return G.createContentBlock(getStubsSection(G), Template,
368-
orc::ExecutorAddr(), Alignment, 0);
369-
}
370-
371-
/// Get or create the object file section that will contain all our stubs.
372-
Section &getStubsSection(LinkGraph &G) {
373-
if (!StubsSection)
374-
StubsSection = &G.createSection(getSectionName(),
375-
orc::MemProt::Read | orc::MemProt::Exec);
376-
return *StubsSection;
335+
// Two slots per external: Arm and Thumb
336+
using StubMapEntry = std::tuple<Symbol *, Symbol *>;
337+
338+
Symbol *&getStubSymbolSlot(StringRef Name, bool Thumb) {
339+
StubMapEntry &Stubs = StubMap.try_emplace(Name).first->second;
340+
if (Thumb)
341+
return std::get<1>(Stubs);
342+
return std::get<0>(Stubs);
377343
}
378344

345+
DenseMap<StringRef, StubMapEntry> StubMap;
379346
Section *StubsSection = nullptr;
380347
};
381348

llvm/lib/ExecutionEngine/JITLink/aarch32.cpp

Lines changed: 113 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/StringExtras.h"
1616
#include "llvm/BinaryFormat/ELF.h"
1717
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
18+
#include "llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h"
1819
#include "llvm/Object/ELFObjectFile.h"
1920
#include "llvm/Support/Endian.h"
2021
#include "llvm/Support/ManagedStatic.h"
@@ -678,27 +679,127 @@ Error applyFixupThumb(LinkGraph &G, Block &B, const Edge &E,
678679
}
679680
}
680681

682+
const uint8_t Armv7ABS[] = {
683+
0x00, 0xc0, 0x00, 0xe3, // movw r12, #0x0000 ; lower 16-bit
684+
0x00, 0xc0, 0x40, 0xe3, // movt r12, #0x0000 ; upper 16-bit
685+
0x1c, 0xff, 0x2f, 0xe1 // bx r12
686+
};
687+
681688
const uint8_t Thumbv7ABS[] = {
682689
0x40, 0xf2, 0x00, 0x0c, // movw r12, #0x0000 ; lower 16-bit
683690
0xc0, 0xf2, 0x00, 0x0c, // movt r12, #0x0000 ; upper 16-bit
684691
0x60, 0x47 // bx r12
685692
};
686693

687-
Symbol &StubsManager_v7::createEntry(LinkGraph &G, Symbol &Target) {
694+
/// Create a new node in the link-graph for the given stub template.
695+
template <size_t Size>
696+
static Block &allocStub(LinkGraph &G, Section &S, const uint8_t (&Code)[Size]) {
688697
constexpr uint64_t Alignment = 4;
689-
Block &B = addStub(G, Thumbv7ABS, Alignment);
690-
LLVM_DEBUG({
691-
const char *StubPtr = B.getContent().data();
692-
HalfWords Reg12 = encodeRegMovtT1MovwT3(12);
693-
assert(checkRegister<Thumb_MovwAbsNC>(StubPtr, Reg12) &&
694-
checkRegister<Thumb_MovtAbs>(StubPtr + 4, Reg12) &&
695-
"Linker generated stubs may only corrupt register r12 (IP)");
696-
});
698+
ArrayRef<char> Template(reinterpret_cast<const char *>(Code), Size);
699+
return G.createContentBlock(S, Template, orc::ExecutorAddr(), Alignment, 0);
700+
}
701+
702+
static Block &createStubThumbv7(LinkGraph &G, Section &S, Symbol &Target) {
703+
Block &B = allocStub(G, S, Thumbv7ABS);
697704
B.addEdge(Thumb_MovwAbsNC, 0, Target, 0);
698705
B.addEdge(Thumb_MovtAbs, 4, Target, 0);
699-
Symbol &Stub = G.addAnonymousSymbol(B, 0, B.getSize(), true, false);
700-
Stub.setTargetFlags(ThumbSymbol);
701-
return Stub;
706+
707+
[[maybe_unused]] const char *StubPtr = B.getContent().data();
708+
[[maybe_unused]] HalfWords Reg12 = encodeRegMovtT1MovwT3(12);
709+
assert(checkRegister<Thumb_MovwAbsNC>(StubPtr, Reg12) &&
710+
checkRegister<Thumb_MovtAbs>(StubPtr + 4, Reg12) &&
711+
"Linker generated stubs may only corrupt register r12 (IP)");
712+
return B;
713+
}
714+
715+
static Block &createStubArmv7(LinkGraph &G, Section &S, Symbol &Target) {
716+
Block &B = allocStub(G, S, Armv7ABS);
717+
B.addEdge(Arm_MovwAbsNC, 0, Target, 0);
718+
B.addEdge(Arm_MovtAbs, 4, Target, 0);
719+
720+
[[maybe_unused]] const char *StubPtr = B.getContent().data();
721+
[[maybe_unused]] uint32_t Reg12 = encodeRegMovtA1MovwA2(12);
722+
assert(checkRegister<Arm_MovwAbsNC>(StubPtr, Reg12) &&
723+
checkRegister<Arm_MovtAbs>(StubPtr + 4, Reg12) &&
724+
"Linker generated stubs may only corrupt register r12 (IP)");
725+
return B;
726+
}
727+
728+
static bool needsStub(const Edge &E) {
729+
Symbol &Target = E.getTarget();
730+
731+
// Create stubs for external branch targets.
732+
if (!Target.isDefined()) {
733+
switch (E.getKind()) {
734+
case Arm_Call:
735+
case Arm_Jump24:
736+
case Thumb_Call:
737+
case Thumb_Jump24:
738+
return true;
739+
default:
740+
return false;
741+
}
742+
}
743+
744+
// For local targets, create interworking stubs if we switch Arm/Thumb with an
745+
// instruction that cannot switch the instruction set state natively.
746+
bool TargetIsThumb = Target.getTargetFlags() & ThumbSymbol;
747+
switch (E.getKind()) {
748+
case Arm_Jump24:
749+
return TargetIsThumb; // Branch to Thumb needs interworking stub
750+
case Thumb_Jump24:
751+
return !TargetIsThumb; // Branch to Arm needs interworking stub
752+
default:
753+
break;
754+
}
755+
756+
return false;
757+
}
758+
759+
bool StubsManager_v7::visitEdge(LinkGraph &G, Block *B, Edge &E) {
760+
if (!needsStub(E))
761+
return false;
762+
763+
// Stub Arm/Thumb follows instruction set state at relocation site.
764+
// TODO: We may reduce them at relaxation time and reuse freed slots.
765+
bool MakeThumb = (E.getKind() > LastArmRelocation);
766+
LLVM_DEBUG(dbgs() << " Preparing " << (MakeThumb ? "Thumb" : "Arm")
767+
<< " stub for " << G.getEdgeKindName(E.getKind())
768+
<< " edge at " << B->getFixupAddress(E) << " ("
769+
<< B->getAddress() << " + "
770+
<< formatv("{0:x}", E.getOffset()) << ")\n");
771+
772+
Symbol &Target = E.getTarget();
773+
assert(Target.hasName() && "Edge cannot point to anonymous target");
774+
Symbol *&StubSymbol = getStubSymbolSlot(Target.getName(), MakeThumb);
775+
776+
if (!StubSymbol) {
777+
if (!StubsSection)
778+
StubsSection = &G.createSection(getSectionName(),
779+
orc::MemProt::Read | orc::MemProt::Exec);
780+
Block &B = MakeThumb ? createStubThumbv7(G, *StubsSection, Target)
781+
: createStubArmv7(G, *StubsSection, Target);
782+
StubSymbol = &G.addAnonymousSymbol(B, 0, B.getSize(), true, false);
783+
if (MakeThumb)
784+
StubSymbol->setTargetFlags(ThumbSymbol);
785+
786+
LLVM_DEBUG({
787+
dbgs() << " Created " << (MakeThumb ? "Thumb" : "Arm") << " entry for "
788+
<< Target.getName() << " in " << StubsSection->getName() << ": "
789+
<< *StubSymbol << "\n";
790+
});
791+
}
792+
793+
assert(MakeThumb == (StubSymbol->getTargetFlags() & ThumbSymbol) &&
794+
"Instruction set states of stub and relocation site should be equal");
795+
LLVM_DEBUG({
796+
dbgs() << " Using " << (MakeThumb ? "Thumb" : "Arm") << " entry "
797+
<< *StubSymbol << " in "
798+
<< StubSymbol->getBlock().getSection().getName() << "\n";
799+
});
800+
801+
E.setTarget(*StubSymbol);
802+
return true;
702803
}
703804

704805
const char *getEdgeKindName(Edge::Kind K) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# RUN: rm -rf %t && mkdir -p %t
2+
# RUN: llvm-mc -triple=armv7-linux-gnueabi -arm-add-build-attributes \
3+
# RUN: -filetype=obj -o %t/out.o %s
4+
# RUN: llvm-jitlink -noexec -slab-address 0x76ff0000 \
5+
# RUN: -slab-allocate 10Kb -slab-page-size 4096 \
6+
# RUN: -abs ext=0x76bbe880 \
7+
# RUN: -check %s %t/out.o
8+
9+
.text
10+
.syntax unified
11+
12+
# Check that calls/jumps to external functions trigger the generation of
13+
# branch-range extension stubs. These stubs don't follow the default PLT model
14+
# where the branch-target address is loaded from a GOT entry. Instead, they
15+
# hard-code it in the immediate field.
16+
17+
# The external function ext will return to the caller directly.
18+
# jitlink-check: decode_operand(test_arm_jump, 0) = stub_addr(out.o, ext) - (test_arm_jump + 8)
19+
.globl test_arm_jump
20+
.type test_arm_jump,%function
21+
.p2align 2
22+
test_arm_jump:
23+
b ext
24+
.size test_arm_jump, .-test_arm_jump
25+
26+
# The branch-with-link sets the LR register so that the external function ext
27+
# returns to us. We have to save the register (push) and return to main manually
28+
# (pop). This adds the +4 offset for the bl instruction we decode:
29+
# jitlink-check: decode_operand(test_arm_call + 4, 0) = stub_addr(out.o, ext) - (test_arm_call + 8) - 4
30+
.globl test_arm_call
31+
.type test_arm_call,%function
32+
.p2align 2
33+
test_arm_call:
34+
push {lr}
35+
bl ext
36+
pop {pc}
37+
.size test_arm_call, .-test_arm_call
38+
39+
# This test is executable with both, Arm and Thumb `ext` functions. It only has
40+
# to return with `bx lr`. For example:
41+
# > echo "void ext() {}" | clang -target armv7-linux-gnueabihf -o ext-arm.o -c -xc -
42+
# > llvm-jitlink ext-arm.o out.o
43+
#
44+
.globl main
45+
.type main,%function
46+
.p2align 2
47+
main:
48+
push {lr}
49+
bl test_arm_call
50+
bl test_arm_jump
51+
movw r0, #0
52+
pop {pc}
53+
.size main, .-main
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# RUN: rm -rf %t && mkdir -p %t
2+
# RUN: llvm-mc -triple=armv7-linux-gnueabi -arm-add-build-attributes \
3+
# RUN: -filetype=obj -o %t/out.o %s
4+
# RUN: llvm-jitlink -noexec -slab-address 0x76ff0000 \
5+
# RUN: -slab-allocate=10Kb -slab-page-size=4096 \
6+
# RUN: -abs ext=0x76bbe880 -check %s %t/out.o
7+
8+
.text
9+
.syntax unified
10+
11+
# Check that a single external symbol can have multiple stubs. We access them
12+
# with the extra stub-index argument to stub_addr(). Stubs are sorted by
13+
# ascending size (because the default memory manager lays out blocks by size).
14+
15+
# Thumb relocation site emits thumb stub
16+
# jitlink-check: decode_operand(test_stub_thumb, 0) = stub_addr(out.o, ext, thumb) - (test_stub_thumb + 4)
17+
.globl test_stub_thumb
18+
.type test_stub_thumb,%function
19+
.p2align 1
20+
.code 16
21+
.thumb_func
22+
test_stub_thumb:
23+
b ext
24+
.size test_stub_thumb, .-test_stub_thumb
25+
26+
# Arm relocation site emits arm stub
27+
# jitlink-check: decode_operand(test_stub_arm, 0) = stub_addr(out.o, ext, arm) - (test_stub_arm + 8)
28+
.globl test_stub_arm
29+
.type test_stub_arm,%function
30+
.p2align 2
31+
.code 32
32+
test_stub_arm:
33+
b ext
34+
.size test_stub_arm, .-test_stub_arm
35+
36+
# This test is executable with both, Arm and Thumb `ext` functions. It only has
37+
# to return (directly to main) with `bx lr`. For example:
38+
# > echo "void ext() {}" | clang -target armv7-linux-gnueabihf -o ext-arm.o -c -xc -
39+
# > llvm-jitlink ext-arm.o out.o
40+
#
41+
.globl main
42+
.type main,%function
43+
.p2align 2
44+
main:
45+
push {lr}
46+
bl test_stub_arm
47+
bl test_stub_thumb
48+
movw r0, #0
49+
pop {pc}
50+
.size main, .-main

llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,8 +1265,52 @@ Session::findSectionInfo(StringRef FileName, StringRef SectionName) {
12651265
return SecInfoItr->second;
12661266
}
12671267

1268+
class MemoryMatcher {
1269+
public:
1270+
MemoryMatcher(ArrayRef<char> Content)
1271+
: Pos(Content.data()), End(Pos + Content.size()) {}
1272+
1273+
template <typename MaskType> bool matchMask(MaskType Mask) {
1274+
if (Mask == (Mask & *reinterpret_cast<const MaskType *>(Pos))) {
1275+
Pos += sizeof(MaskType);
1276+
return true;
1277+
}
1278+
return false;
1279+
}
1280+
1281+
template <typename ValueType> bool matchEqual(ValueType Value) {
1282+
if (Value == *reinterpret_cast<const ValueType *>(Pos)) {
1283+
Pos += sizeof(ValueType);
1284+
return true;
1285+
}
1286+
return false;
1287+
}
1288+
1289+
bool done() const { return Pos == End; }
1290+
1291+
private:
1292+
const char *Pos;
1293+
const char *End;
1294+
};
1295+
12681296
static StringRef detectStubKind(const Session::MemoryRegionInfo &Stub) {
1269-
// Implement acutal stub kind detection
1297+
constexpr uint32_t Armv7MovWTle = 0xe300c000;
1298+
constexpr uint32_t Armv7BxR12le = 0xe12fff1c;
1299+
constexpr uint32_t Thumbv7MovWTle = 0x0c00f240;
1300+
constexpr uint16_t Thumbv7BxR12le = 0x4760;
1301+
1302+
MemoryMatcher M(Stub.getContent());
1303+
if (M.matchMask(Thumbv7MovWTle)) {
1304+
if (M.matchMask(Thumbv7MovWTle))
1305+
if (M.matchEqual(Thumbv7BxR12le))
1306+
if (M.done())
1307+
return "thumbv7_abs_le";
1308+
} else if (M.matchMask(Armv7MovWTle)) {
1309+
if (M.matchMask(Armv7MovWTle))
1310+
if (M.matchEqual(Armv7BxR12le))
1311+
if (M.done())
1312+
return "armv7_abs_le";
1313+
}
12701314
return "";
12711315
}
12721316

0 commit comments

Comments
 (0)