-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[lld] Add thunks for hexagon #111217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[lld] Add thunks for hexagon #111217
Changes from all commits
cced3c5
e3b915a
9b8059e
ea13019
58d735c
8a97197
061758b
b7e019b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2140,17 +2140,44 @@ void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) { | |
}); | ||
} | ||
|
||
static int64_t getPCBias(Ctx &ctx, RelType type) { | ||
if (ctx.arg.emachine != EM_ARM) | ||
return 0; | ||
switch (type) { | ||
case R_ARM_THM_JUMP19: | ||
case R_ARM_THM_JUMP24: | ||
case R_ARM_THM_CALL: | ||
return 4; | ||
default: | ||
return 8; | ||
constexpr uint32_t HEXAGON_MASK_END_PACKET = 3 << 14; | ||
constexpr uint32_t HEXAGON_END_OF_PACKET = 3 << 14; | ||
constexpr uint32_t HEXAGON_END_OF_DUPLEX = 0 << 14; | ||
|
||
// Return the distance between the packet start and the instruction in the | ||
// relocation. | ||
static int getHexagonPacketOffset(const InputSection &isec, | ||
const Relocation &rel) { | ||
const ArrayRef<uint8_t> data = isec.content(); | ||
|
||
// Search back as many as 3 instructions. | ||
for (unsigned i = 0;; i++) { | ||
if (i == 3 || rel.offset < (i + 1) * 4) | ||
return i * 4; | ||
uint32_t instWord = 0; | ||
const ArrayRef<uint8_t> instWordContents = | ||
data.drop_front(rel.offset - (i + 1) * 4); | ||
memcpy(&instWord, instWordContents.data(), sizeof(instWord)); | ||
if (((instWord & HEXAGON_MASK_END_PACKET) == HEXAGON_END_OF_PACKET) || | ||
((instWord & HEXAGON_MASK_END_PACKET) == HEXAGON_END_OF_DUPLEX)) | ||
return i * 4; | ||
} | ||
} | ||
static int64_t getPCBias(Ctx &ctx, const InputSection &isec, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line between functions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was not fixed |
||
const Relocation &rel) { | ||
if (ctx.arg.emachine == EM_ARM) { | ||
switch (rel.type) { | ||
case R_ARM_THM_JUMP19: | ||
case R_ARM_THM_JUMP24: | ||
case R_ARM_THM_CALL: | ||
return 4; | ||
default: | ||
return 8; | ||
} | ||
} | ||
if (ctx.arg.emachine == EM_HEXAGON) | ||
return -getHexagonPacketOffset(isec, rel); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed, thank you. |
||
return 0; | ||
} | ||
|
||
// Find or create a ThunkSection within the InputSectionDescription (ISD) that | ||
|
@@ -2162,7 +2189,7 @@ ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os, | |
const Relocation &rel, | ||
uint64_t src) { | ||
// See the comment in getThunk for -pcBias below. | ||
const int64_t pcBias = getPCBias(ctx, rel.type); | ||
const int64_t pcBias = getPCBias(ctx, *isec, rel); | ||
for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) { | ||
ThunkSection *ts = tp.first; | ||
uint64_t tsBase = os->addr + ts->outSecOff - pcBias; | ||
|
@@ -2323,7 +2350,7 @@ std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec, | |
// out in the relocation addend. We compensate for the PC bias so that | ||
// an Arm and Thumb relocation to the same destination get the same keyAddend, | ||
// which is usually 0. | ||
const int64_t pcBias = getPCBias(ctx, rel.type); | ||
const int64_t pcBias = getPCBias(ctx, *isec, rel); | ||
const int64_t keyAddend = rel.addend + pcBias; | ||
|
||
// We use a ((section, offset), addend) pair to find the thunk position if | ||
|
@@ -2482,7 +2509,7 @@ bool ThunkCreator::createThunks(uint32_t pass, | |
// STT_SECTION + non-zero addend, clear the addend after | ||
// redirection. | ||
if (ctx.arg.emachine != EM_MIPS) | ||
rel.addend = -getPCBias(ctx, rel.type); | ||
rel.addend = -getPCBias(ctx, *isec, rel); | ||
} | ||
|
||
for (auto &p : isd->thunkSections) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -415,6 +415,22 @@ class AVRThunk : public Thunk { | |
void addSymbols(ThunkSection &isec) override; | ||
}; | ||
|
||
// Hexagon CPUs need thunks for R_HEX_B{9,1{3,5},22}_PCREL, | ||
// R_HEX_{,GD_}PLT_B22_PCREL when their destination is out of | ||
// range. | ||
class HexagonThunk : public Thunk { | ||
public: | ||
HexagonThunk(Ctx &ctx, const InputSection &isec, Relocation &rel, | ||
Symbol &dest) | ||
: Thunk(ctx, dest, 0), relOffset(rel.offset) { | ||
alignment = 4; | ||
} | ||
uint32_t relOffset; | ||
uint32_t size() override { return ctx.arg.isPic ? 12 : 8; } | ||
void writeTo(uint8_t *buf) override; | ||
void addSymbols(ThunkSection &isec) override; | ||
}; | ||
|
||
// MIPS LA25 thunk | ||
class MipsThunk final : public Thunk { | ||
public: | ||
|
@@ -1519,6 +1535,39 @@ bool PPC64LongBranchThunk::isCompatibleWith(const InputSection &isec, | |
return rel.type == R_PPC64_REL24 || rel.type == R_PPC64_REL14; | ||
} | ||
|
||
// Hexagon Target Thunks | ||
static uint64_t getHexagonThunkDestVA(Ctx &ctx, const Symbol &s, int64_t a) { | ||
uint64_t v = s.isInPlt(ctx) ? s.getPltVA(ctx) : s.getVA(ctx, a); | ||
return SignExtend64<32>(v); | ||
} | ||
|
||
void HexagonThunk::writeTo(uint8_t *buf) { | ||
uint64_t s = getHexagonThunkDestVA(ctx, destination, addend); | ||
uint64_t p = getThunkTargetSym()->getVA(ctx); | ||
|
||
if (ctx.arg.isPic) { | ||
write32(ctx, buf + 0, 0x00004000); // { immext(#0) | ||
ctx.target->relocateNoSym(buf, R_HEX_B32_PCREL_X, s - p); | ||
write32(ctx, buf + 4, 0x6a49c00e); // r14 = add(pc,##0) } | ||
ctx.target->relocateNoSym(buf + 4, R_HEX_6_PCREL_X, s - p); | ||
|
||
write32(ctx, buf + 8, 0x528ec000); // { jumpr r14 } | ||
} else { | ||
write32(ctx, buf + 0, 0x00004000); // { immext | ||
ctx.target->relocateNoSym(buf, R_HEX_B32_PCREL_X, s - p); | ||
write32(ctx, buf + 4, 0x5800c000); // jump <> } | ||
ctx.target->relocateNoSym(buf + 4, R_HEX_B22_PCREL_X, s - p); | ||
} | ||
} | ||
void HexagonThunk::addSymbols(ThunkSection &isec) { | ||
Symbol *enclosing = isec.getEnclosingSymbol(relOffset); | ||
StringRef src = enclosing ? enclosing->getName() : isec.name; | ||
|
||
addSymbol( | ||
saver().save("__hexagon_thunk_" + destination.getName() + "_from_" + src), | ||
STT_FUNC, 0, isec); | ||
} | ||
|
||
Thunk::Thunk(Ctx &ctx, Symbol &d, int64_t a) | ||
: ctx(ctx), destination(d), addend(a), offset(0) { | ||
destination.thunkAccessed = true; | ||
|
@@ -1692,6 +1741,24 @@ static std::unique_ptr<Thunk> addThunkAVR(Ctx &ctx, RelType type, Symbol &s, | |
} | ||
} | ||
|
||
static std::unique_ptr<Thunk> addThunkHexagon(Ctx &ctx, | ||
const InputSection &isec, | ||
Relocation &rel, Symbol &s) { | ||
switch (rel.type) { | ||
case R_HEX_B9_PCREL: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case R_HEX_B22_PCREL: Here are only 6. Please fix it and don't add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
case R_HEX_B13_PCREL: | ||
case R_HEX_B15_PCREL: | ||
case R_HEX_B22_PCREL: | ||
case R_HEX_PLT_B22_PCREL: | ||
case R_HEX_GD_PLT_B22_PCREL: | ||
return std::make_unique<HexagonThunk>(ctx, isec, rel, s); | ||
default: | ||
Fatal(ctx) << "unrecognized relocation " << rel.type << " to " << &s | ||
<< " for hexagon target"; | ||
llvm_unreachable(""); | ||
} | ||
} | ||
|
||
static std::unique_ptr<Thunk> addThunkMips(Ctx &ctx, RelType type, Symbol &s) { | ||
if ((s.stOther & STO_MIPS_MICROMIPS) && isMipsR6(ctx)) | ||
return std::make_unique<MicroMipsR6Thunk>(ctx, s); | ||
|
@@ -1761,8 +1828,11 @@ std::unique_ptr<Thunk> elf::addThunk(Ctx &ctx, const InputSection &isec, | |
return addThunkPPC32(ctx, isec, rel, s); | ||
case EM_PPC64: | ||
return addThunkPPC64(ctx, rel.type, s, a); | ||
case EM_HEXAGON: | ||
return addThunkHexagon(ctx, isec, rel, s); | ||
default: | ||
llvm_unreachable("add Thunk only supported for ARM, AVR, Mips and PowerPC"); | ||
llvm_unreachable( | ||
"add Thunk only supported for ARM, AVR, Hexagon, Mips and PowerPC"); | ||
} | ||
} | ||
|
||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need tests to test the thunk range. aarch64-thunk-pi.s ppc64-long-branch.s are examples to test the exact range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add the necessary tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
range test cases added