Skip to content

Commit 992ffbd

Browse files
[lld] Add thunks for hexagon
Co-authored-by: Alexey Karyakin <[email protected]>
1 parent ff97b28 commit 992ffbd

File tree

6 files changed

+308
-46
lines changed

6 files changed

+308
-46
lines changed

lld/ELF/Arch/Hexagon.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Symbols.h"
1111
#include "SyntheticSections.h"
1212
#include "Target.h"
13+
#include "Thunks.h"
1314
#include "lld/Common/ErrorHandler.h"
1415
#include "llvm/BinaryFormat/ELF.h"
1516
#include "llvm/Support/Endian.h"
@@ -30,6 +31,10 @@ class Hexagon final : public TargetInfo {
3031
const uint8_t *loc) const override;
3132
RelType getDynRel(RelType type) const override;
3233
int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override;
34+
bool needsThunk(RelExpr expr, RelType type, const InputFile *file,
35+
uint64_t branchAddr, const Symbol &s,
36+
int64_t a) const override;
37+
bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
3338
void relocate(uint8_t *loc, const Relocation &rel,
3439
uint64_t val) const override;
3540
void writePltHeader(uint8_t *buf) const override;
@@ -57,6 +62,8 @@ Hexagon::Hexagon(Ctx &ctx) : TargetInfo(ctx) {
5762
tlsGotRel = R_HEX_TPREL_32;
5863
tlsModuleIndexRel = R_HEX_DTPMOD_32;
5964
tlsOffsetRel = R_HEX_DTPREL_32;
65+
66+
needsThunks = true;
6067
}
6168

6269
uint32_t Hexagon::calcEFlags() const {
@@ -252,6 +259,34 @@ static uint32_t findMaskR16(Ctx &ctx, uint32_t insn) {
252259

253260
static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); }
254261

262+
bool Hexagon::inBranchRange(RelType type, uint64_t src, uint64_t dst) const {
263+
int64_t offset = dst - src;
264+
switch (type) {
265+
case llvm::ELF::R_HEX_B22_PCREL:
266+
case llvm::ELF::R_HEX_PLT_B22_PCREL:
267+
case llvm::ELF::R_HEX_GD_PLT_B22_PCREL:
268+
case llvm::ELF::R_HEX_LD_PLT_B22_PCREL:
269+
return llvm::isInt<22>(offset >> 2);
270+
case llvm::ELF::R_HEX_B15_PCREL:
271+
return llvm::isInt<15>(offset >> 2);
272+
break;
273+
case llvm::ELF::R_HEX_B13_PCREL:
274+
return llvm::isInt<13>(offset >> 2);
275+
break;
276+
case llvm::ELF::R_HEX_B9_PCREL:
277+
return llvm::isInt<9>(offset >> 2);
278+
default:
279+
return true;
280+
}
281+
llvm_unreachable("unsupported relocation");
282+
}
283+
284+
bool Hexagon::needsThunk(RelExpr expr, RelType type, const InputFile *file,
285+
uint64_t branchAddr, const Symbol &s,
286+
int64_t a) const {
287+
return !ctx.target->inBranchRange(type, branchAddr, s.getVA(ctx, a));
288+
}
289+
255290
void Hexagon::relocate(uint8_t *loc, const Relocation &rel,
256291
uint64_t val) const {
257292
switch (rel.type) {

lld/ELF/Relocations.cpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,17 +2031,44 @@ void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) {
20312031
});
20322032
}
20332033

2034-
static int64_t getPCBias(Ctx &ctx, RelType type) {
2035-
if (ctx.arg.emachine != EM_ARM)
2036-
return 0;
2037-
switch (type) {
2038-
case R_ARM_THM_JUMP19:
2039-
case R_ARM_THM_JUMP24:
2040-
case R_ARM_THM_CALL:
2041-
return 4;
2042-
default:
2043-
return 8;
2034+
static const uint32_t HEXAGON_MASK_END_PACKET = 3 << 14;
2035+
static const uint32_t HEXAGON_END_OF_PACKET = 3 << 14;
2036+
static const uint32_t HEXAGON_END_OF_DUPLEX = 0 << 14;
2037+
2038+
// Return the distance between the packet start and the instruction in the
2039+
// relocation.
2040+
static int getHexagonPacketOffset(const InputSection &isec,
2041+
const Relocation &rel) {
2042+
const ArrayRef<uint8_t> SectContents = isec.content();
2043+
2044+
// Search back as many as 3 instructions.
2045+
for (unsigned i = 0;; i++) {
2046+
if (i == 3 || rel.offset < (i + 1) * 4)
2047+
return i * 4;
2048+
uint32_t instWord = 0;
2049+
const ArrayRef<uint8_t> InstWordContents =
2050+
SectContents.drop_front(rel.offset - (i + 1) * 4);
2051+
::memcpy(&instWord, InstWordContents.data(), sizeof(instWord));
2052+
if (((instWord & HEXAGON_MASK_END_PACKET) == HEXAGON_END_OF_PACKET) ||
2053+
((instWord & HEXAGON_MASK_END_PACKET) == HEXAGON_END_OF_DUPLEX))
2054+
return i * 4;
2055+
}
2056+
}
2057+
static int64_t getPCBias(Ctx &ctx, const InputSection &isec,
2058+
const Relocation &rel) {
2059+
if (ctx.arg.emachine == EM_ARM) {
2060+
switch (rel.type) {
2061+
case R_ARM_THM_JUMP19:
2062+
case R_ARM_THM_JUMP24:
2063+
case R_ARM_THM_CALL:
2064+
return 4;
2065+
default:
2066+
return 8;
2067+
}
20442068
}
2069+
if (ctx.arg.emachine == EM_HEXAGON)
2070+
return -getHexagonPacketOffset(isec, rel);
2071+
return 0;
20452072
}
20462073

20472074
// Find or create a ThunkSection within the InputSectionDescription (ISD) that
@@ -2053,7 +2080,7 @@ ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os,
20532080
const Relocation &rel,
20542081
uint64_t src) {
20552082
// See the comment in getThunk for -pcBias below.
2056-
const int64_t pcBias = getPCBias(ctx, rel.type);
2083+
const int64_t pcBias = getPCBias(ctx, *isec, rel);
20572084
for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) {
20582085
ThunkSection *ts = tp.first;
20592086
uint64_t tsBase = os->addr + ts->outSecOff - pcBias;
@@ -2214,7 +2241,7 @@ std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec,
22142241
// out in the relocation addend. We compensate for the PC bias so that
22152242
// an Arm and Thumb relocation to the same destination get the same keyAddend,
22162243
// which is usually 0.
2217-
const int64_t pcBias = getPCBias(ctx, rel.type);
2244+
const int64_t pcBias = getPCBias(ctx, *isec, rel);
22182245
const int64_t keyAddend = rel.addend + pcBias;
22192246

22202247
// We use a ((section, offset), addend) pair to find the thunk position if
@@ -2373,7 +2400,7 @@ bool ThunkCreator::createThunks(uint32_t pass,
23732400
// STT_SECTION + non-zero addend, clear the addend after
23742401
// redirection.
23752402
if (ctx.arg.emachine != EM_MIPS)
2376-
rel.addend = -getPCBias(ctx, rel.type);
2403+
rel.addend = -getPCBias(ctx, *isec, rel);
23772404
}
23782405

23792406
for (auto &p : isd->thunkSections)

lld/ELF/Thunks.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,21 @@ class AVRThunk : public Thunk {
402402
void addSymbols(ThunkSection &isec) override;
403403
};
404404

405+
// Hexagon CPUs need thunks for <<FIXME TBD>>
406+
// when their destination is out of range [0, 0x_?].
407+
class HexagonThunk : public Thunk {
408+
public:
409+
HexagonThunk(Ctx &ctx, const InputSection &isec, Relocation &rel,
410+
Symbol &dest)
411+
: Thunk(ctx, dest, 0), relOffset(rel.offset) {
412+
alignment = 4;
413+
}
414+
uint32_t relOffset;
415+
uint32_t size() override { return ctx.arg.isPic ? 12 : 8; }
416+
void writeTo(uint8_t *buf) override;
417+
void addSymbols(ThunkSection &isec) override;
418+
};
419+
405420
// MIPS LA25 thunk
406421
class MipsThunk final : public Thunk {
407422
public:
@@ -1468,6 +1483,39 @@ bool PPC64LongBranchThunk::isCompatibleWith(const InputSection &isec,
14681483
return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14;
14691484
}
14701485

1486+
// Hexagon Target Thunks
1487+
static uint64_t getHexagonThunkDestVA(Ctx &ctx, const Symbol &s, int64_t a) {
1488+
uint64_t v = s.isInPlt(ctx) ? s.getPltVA(ctx) : s.getVA(ctx, a);
1489+
return SignExtend64<32>(v); // FIXME: sign extend to 64-bit?
1490+
}
1491+
1492+
void HexagonThunk::writeTo(uint8_t *buf) {
1493+
uint64_t s = getHexagonThunkDestVA(ctx, destination, addend);
1494+
uint64_t p = getThunkTargetSym()->getVA(ctx);
1495+
1496+
if (ctx.arg.isPic) {
1497+
write32(ctx, buf + 0, 0x00004000); // { immext(#0)
1498+
ctx.target->relocateNoSym(buf, R_HEX_B32_PCREL_X, s - p);
1499+
write32(ctx, buf + 4, 0x6a49c00e); // r14 = add(pc,##0) }
1500+
ctx.target->relocateNoSym(buf + 4, R_HEX_6_PCREL_X, s - p);
1501+
1502+
write32(ctx, buf + 8, 0x528ec000); // { jumpr r14 }
1503+
} else {
1504+
write32(ctx, buf + 0, 0x00004000); // { immext
1505+
ctx.target->relocateNoSym(buf, R_HEX_B32_PCREL_X, s - p);
1506+
write32(ctx, buf + 4, 0x5800c000); // jump <> }
1507+
ctx.target->relocateNoSym(buf + 4, R_HEX_B22_PCREL_X, s - p);
1508+
}
1509+
}
1510+
void HexagonThunk::addSymbols(ThunkSection &isec) {
1511+
Symbol *enclosing = isec.getEnclosingSymbol(relOffset);
1512+
StringRef src = enclosing ? enclosing->getName() : isec.name;
1513+
1514+
addSymbol(saver().save("__trampoline_for_" + destination.getName() +
1515+
"_from_" + src),
1516+
STT_FUNC, 0, isec);
1517+
}
1518+
14711519
Thunk::Thunk(Ctx &ctx, Symbol &d, int64_t a)
14721520
: ctx(ctx), destination(d), addend(a), offset(0) {
14731521
destination.thunkAccessed = true;
@@ -1637,6 +1685,24 @@ static std::unique_ptr<Thunk> addThunkAVR(Ctx &ctx, RelType type, Symbol &s,
16371685
}
16381686
}
16391687

1688+
static std::unique_ptr<Thunk> addThunkHexagon(Ctx &ctx,
1689+
const InputSection &isec,
1690+
Relocation &rel, Symbol &s) {
1691+
switch (rel.type) {
1692+
case R_HEX_B9_PCREL:
1693+
case R_HEX_B13_PCREL:
1694+
case R_HEX_B15_PCREL:
1695+
case R_HEX_B22_PCREL:
1696+
case R_HEX_PLT_B22_PCREL:
1697+
case R_HEX_GD_PLT_B22_PCREL:
1698+
return std::make_unique<HexagonThunk>(ctx, isec, rel, s);
1699+
default:
1700+
Fatal(ctx) << "unrecognized relocation " << rel.type << " to " << &s
1701+
<< " for hexagon target";
1702+
llvm_unreachable("");
1703+
}
1704+
}
1705+
16401706
static std::unique_ptr<Thunk> addThunkMips(Ctx &ctx, RelType type, Symbol &s) {
16411707
if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6(ctx))
16421708
return std::make_unique<MicroMipsR6Thunk>(ctx, s);
@@ -1706,8 +1772,11 @@ std::unique_ptr<Thunk> elf::addThunk(Ctx &ctx, const InputSection &isec,
17061772
return addThunkPPC32(ctx, isec, rel, s);
17071773
case EM_PPC64:
17081774
return addThunkPPC64(ctx, rel.type, s, a);
1775+
case EM_HEXAGON:
1776+
return addThunkHexagon(ctx, isec, rel, s);
17091777
default:
1710-
llvm_unreachable("add Thunk only supported for ARM, AVR, Mips and PowerPC");
1778+
llvm_unreachable(
1779+
"add Thunk only supported for ARM, AVR, Hexagon, Mips and PowerPC");
17111780
}
17121781
}
17131782

lld/test/ELF/hexagon-jump-error.s

Lines changed: 0 additions & 32 deletions
This file was deleted.

lld/test/ELF/hexagon-thunks-packets.s

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# REQUIRES: hexagon
2+
# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-linux-musl %s -o %t.o
3+
# RUN: ld.lld %t.o -o %t
4+
# RUN: llvm-objdump -d %t 2>&1 | FileCheck --check-prefix=CHECK-NONPIC %s
5+
# RUN: llvm-mc -filetype=obj --position-independent \
6+
# RUN: -triple=hexagon-unknown-linux-musl %s -o %t.o
7+
# RUN: ld.lld --pie %t.o -o %t
8+
# RUN: llvm-objdump -d %t 2>&1 | FileCheck --check-prefix=CHECK-PIC %s
9+
10+
# Packets with pc-relative relocations are more interesting because
11+
# the offset must be relative to the start of the source, destination
12+
# packets and not necessarily the instruction word containing the jump/call.
13+
14+
# CHECK: Disassembly of section .text:
15+
16+
# CHECK-NONPIC: 000200b4 <__trampoline_for_myfn_a_from_.text.thunk>:
17+
# CHECK-NONPIC: { immext(#0x1000040)
18+
# CHECK-NONPIC: jump 0x1020110 }
19+
20+
# CHECK-PIC: 00010150 <__trampoline_for_myfn_a_from_.text.thunk>:
21+
# CHECK-PIC: { immext(#0x1000040)
22+
# CHECK-PIC: r14 = add(pc,##0x1000060) }
23+
# CHECK-PIC: { jumpr r14 }
24+
25+
# CHECK-NONPIC: 000200bc <myfn_b>:
26+
# CHECK-NONPIC: { jumpr r31 }
27+
# CHECK-PIC: 0001015c <myfn_b>:
28+
# CHECK-PIC: { jumpr r31 }
29+
.globl myfn_b
30+
.type myfn_b, @function
31+
myfn_b:
32+
jumpr r31
33+
.size myfn_b, .-myfn_b
34+
35+
# CHECK-PIC: 00010160 <main>:
36+
.globl main
37+
.type main, @function
38+
main:
39+
{ r0 = #0
40+
call myfn_a }
41+
# CHECK-PIC: { call 0x10150
42+
# CHECK-NONPIC: { call 0x200b4
43+
# CHECK: r0 = #0x0 }
44+
call myfn_a
45+
# CHECK-PIC: call 0x10150
46+
# CHECK-NONPIC: call 0x200b4
47+
call myfn_b
48+
# CHECK-PIC: call 0x1015c
49+
# CHECK-NONPIC: call 0x200bc
50+
51+
{ r2 = add(r0, r1)
52+
if (p0) call #myfn_b
53+
if (!p0) call #myfn_a }
54+
# CHECK-PIC: { if (p0) call 0x1015c
55+
# CHECK-PIC: if (!p0) call 0x10150
56+
# CHECK-NONPIC: { if (p0) call 0x200bc
57+
# CHECK-NONPIC: if (!p0) call 0x200b4
58+
# CHECK: r2 = add(r0,r1) }
59+
60+
{ r2 = add(r0, r1)
61+
if (p0) call #myfn_a
62+
if (!p0) call #myfn_a }
63+
# CHECK-PIC: { if (p0) call 0x10150
64+
# CHECK-PIC: if (!p0) call 0x10150
65+
# CHECK-NONPIC: { if (p0) call 0x200b4
66+
# CHECK-NONPIC: if (!p0) call 0x200b4
67+
# CHECK: r2 = add(r0,r1) }
68+
69+
{ r2 = add(r0, r1)
70+
r1 = r4
71+
r4 = r5
72+
if (r0 == #0) jump:t #myfn_a }
73+
# CHECK-PIC: { if (r0==#0) jump:t 0x10150
74+
# CHECK-NONPIC: { if (r0==#0) jump:t 0x200b4
75+
# CHECK: r2 = add(r0,r1)
76+
# CHECK: r1 = r4; r4 = r5 }
77+
78+
{ r2 = add(r0, r1)
79+
r4 = r5
80+
if (r0 <= #0) jump:t #myfn_a
81+
p1 = cmp.eq(r0, #0); if (p1.new) jump:nt #myfn_a }
82+
# CHECK-NONPIC: { if (r0<=#0) jump:t 0x200b4
83+
# CHECK-NONPIC: p1 = cmp.eq(r0,#0x0); if (p1.new) jump:nt 0x200b4
84+
# CHECK-PIC: { if (r0<=#0) jump:t 0x10150
85+
# CHECK-PIC: p1 = cmp.eq(r0,#0x0); if (p1.new) jump:nt 0x10150
86+
# CHECK: r2 = add(r0,r1)
87+
# CHECK: r4 = r5 }
88+
89+
{r0 = #0; jump #myfn_a}
90+
# CHECK-PIC: { r0 = #0x0 ; jump 0x10150 }
91+
# CHECK-NONPIC: { r0 = #0x0 ; jump 0x200b4 }
92+
{r0 = #0; jump #myfn_b}
93+
# CHECK-PIC: { r0 = #0x0 ; jump 0x1015c }
94+
# CHECK-NONPIC: { r0 = #0x0 ; jump 0x200bc }
95+
jumpr r31
96+
.size main, .-main
97+
98+
.section .text.foo
99+
.skip 0x1000000
100+
101+
.globl myfn_a
102+
.type myfn_a, @function
103+
myfn_a:
104+
{r0 = #0; jump #myfn_b}
105+
jumpr r31
106+
.size myfn_a, .-myfn_a
107+
108+
# CHECK-NONPIC: 01020110 <myfn_a>:
109+
# CHECK-NONPIC: { r0 = #0x0 ; jump 0x1020118 }
110+
# CHECK-NONPIC: { jumpr r31 }
111+
112+
# CHECK-NONPIC: 01020118 <__trampoline_for_myfn_b_from_.text.thunk>:
113+
# CHECK-NONPIC: { immext(#0xfeffff80)
114+
# CHECK-NONPIC: jump 0x200bc }
115+
116+
# CHECK-PIC: 010101b8 <__trampoline_for_myfn_b_from_.text.thunk>:
117+
# CHECK-PIC: { immext(#0xfeffff80)
118+
# CHECK-PIC: r14 = add(pc,##0xfeffffa4) }
119+
# CHECK-PIC: { jumpr r14 }

0 commit comments

Comments
 (0)