-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
base: main
Are you sure you want to change the base?
[lld] Add thunks for hexagon #111217
Changes from all commits
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
#include "Symbols.h" | ||
#include "SyntheticSections.h" | ||
#include "Target.h" | ||
#include "Thunks.h" | ||
#include "lld/Common/ErrorHandler.h" | ||
#include "llvm/BinaryFormat/ELF.h" | ||
#include "llvm/Support/Endian.h" | ||
|
@@ -30,6 +31,10 @@ class Hexagon final : public TargetInfo { | |
const uint8_t *loc) const override; | ||
RelType getDynRel(RelType type) const override; | ||
int64_t getImplicitAddend(const uint8_t *buf, RelType type) const override; | ||
bool needsThunk(RelExpr expr, RelType type, const InputFile *file, | ||
uint64_t branchAddr, const Symbol &s, | ||
int64_t a) const override; | ||
bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override; | ||
void relocate(uint8_t *loc, const Relocation &rel, | ||
uint64_t val) const override; | ||
void writePltHeader(uint8_t *buf) const override; | ||
|
@@ -57,6 +62,8 @@ Hexagon::Hexagon(Ctx &ctx) : TargetInfo(ctx) { | |
tlsGotRel = R_HEX_TPREL_32; | ||
tlsModuleIndexRel = R_HEX_DTPMOD_32; | ||
tlsOffsetRel = R_HEX_DTPREL_32; | ||
|
||
needsThunks = true; | ||
} | ||
|
||
uint32_t Hexagon::calcEFlags() const { | ||
|
@@ -252,6 +259,34 @@ static uint32_t findMaskR16(Ctx &ctx, uint32_t insn) { | |
|
||
static void or32le(uint8_t *p, int32_t v) { write32le(p, read32le(p) | v); } | ||
|
||
bool Hexagon::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { | ||
int64_t offset = dst - src; | ||
switch (type) { | ||
case llvm::ELF::R_HEX_B22_PCREL: | ||
case llvm::ELF::R_HEX_PLT_B22_PCREL: | ||
case llvm::ELF::R_HEX_GD_PLT_B22_PCREL: | ||
case llvm::ELF::R_HEX_LD_PLT_B22_PCREL: | ||
return llvm::isInt<22>(offset >> 2); | ||
case llvm::ELF::R_HEX_B15_PCREL: | ||
return llvm::isInt<15>(offset >> 2); | ||
break; | ||
case llvm::ELF::R_HEX_B13_PCREL: | ||
return llvm::isInt<13>(offset >> 2); | ||
break; | ||
case llvm::ELF::R_HEX_B9_PCREL: | ||
return llvm::isInt<9>(offset >> 2); | ||
default: | ||
return true; | ||
} | ||
llvm_unreachable("unsupported relocation"); | ||
} | ||
|
||
bool Hexagon::needsThunk(RelExpr expr, RelType type, const InputFile *file, | ||
uint64_t branchAddr, const Symbol &s, | ||
int64_t a) const { | ||
return !ctx.target->inBranchRange(type, branchAddr, s.getVA(ctx, a)); | ||
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. if type is unexpected, needsThunk should return false to avoid a unreachable failure in inBranchRange. 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. I will investigate a resolution to this issue. |
||
} | ||
|
||
void Hexagon::relocate(uint8_t *loc, const Relocation &rel, | ||
uint64_t val) const { | ||
switch (rel.type) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2037,17 +2037,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> SectContents = 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 = | ||
SectContents.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, | ||
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 | ||
|
@@ -2059,7 +2086,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; | ||
|
@@ -2220,7 +2247,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 | ||
|
@@ -2379,7 +2406,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) | ||
|
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.