Skip to content

Commit 7c6fefe

Browse files
committed
[𝘀𝗽𝗿] changes to main this commit is based on
Created using spr 1.3.4 [skip ci]
1 parent b910beb commit 7c6fefe

File tree

5 files changed

+58
-23
lines changed

5 files changed

+58
-23
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,7 @@ class BinaryContext {
12171217

12181218
/// Return a signed value of \p Size stored at \p Address. The address has
12191219
/// to be a valid statically allocated address for the binary.
1220-
ErrorOr<uint64_t> getSignedValueAtAddress(uint64_t Address,
1221-
size_t Size) const;
1220+
ErrorOr<int64_t> getSignedValueAtAddress(uint64_t Address, size_t Size) const;
12221221

12231222
/// Special case of getUnsignedValueAtAddress() that uses a pointer size.
12241223
ErrorOr<uint64_t> getPointerAtAddress(uint64_t Address) const {

bolt/lib/Core/BinaryContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,7 @@ ErrorOr<uint64_t> BinaryContext::getUnsignedValueAtAddress(uint64_t Address,
22122212
return DE.getUnsigned(&ValueOffset, Size);
22132213
}
22142214

2215-
ErrorOr<uint64_t> BinaryContext::getSignedValueAtAddress(uint64_t Address,
2215+
ErrorOr<int64_t> BinaryContext::getSignedValueAtAddress(uint64_t Address,
22162216
size_t Size) const {
22172217
const ErrorOr<const BinarySection &> Section = getSectionForAddress(Address);
22182218
if (!Section)

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -851,15 +851,19 @@ BinaryFunction::processIndirectBranch(MCInst &Instruction, unsigned Size,
851851
return IndirectBranchType::UNKNOWN;
852852
}
853853

854-
// RIP-relative addressing should be converted to symbol form by now
855-
// in processed instructions (but not in jump).
856-
if (DispExpr) {
854+
auto getExprValue = [&](const MCExpr *Expr) {
857855
const MCSymbol *TargetSym;
858856
uint64_t TargetOffset;
859-
std::tie(TargetSym, TargetOffset) = BC.MIB->getTargetSymbolInfo(DispExpr);
857+
std::tie(TargetSym, TargetOffset) = BC.MIB->getTargetSymbolInfo(Expr);
860858
ErrorOr<uint64_t> SymValueOrError = BC.getSymbolValue(*TargetSym);
861-
assert(SymValueOrError && "global symbol needs a value");
862-
ArrayStart = *SymValueOrError + TargetOffset;
859+
assert(SymValueOrError && "Global symbol needs a value");
860+
return *SymValueOrError + TargetOffset;
861+
};
862+
863+
// RIP-relative addressing should be converted to symbol form by now
864+
// in processed instructions (but not in jump).
865+
if (DispExpr) {
866+
ArrayStart = getExprValue(DispExpr);
863867
BaseRegNum = BC.MIB->getNoRegister();
864868
if (BC.isAArch64()) {
865869
ArrayStart &= ~0xFFFULL;
@@ -1693,6 +1697,26 @@ void BinaryFunction::postProcessEntryPoints() {
16931697
}
16941698

16951699
void BinaryFunction::postProcessJumpTables() {
1700+
// Set of JTs accessed from this function.
1701+
std::unordered_set<uint64_t> LiveJTs;
1702+
for (auto &JTSite : JTSites)
1703+
LiveJTs.emplace(JTSite.second);
1704+
1705+
// Remove dead jump tables (reference removed as a result of
1706+
// POSSIBLE_PIC_FIXED_BRANCH optimization).
1707+
for (auto JTI = JumpTables.begin(), JTE = JumpTables.end(); JTI != JTE; ) {
1708+
const uint64_t Address = JTI->first;
1709+
JumpTable *JT = JTI->second;
1710+
bool HasOneParent = JT->Parents.size() == 1;
1711+
if (LiveJTs.count(Address) == 0 && HasOneParent) {
1712+
BC.deregisterJumpTable(Address);
1713+
delete JT;
1714+
JTI = JumpTables.erase(JTI);
1715+
continue;
1716+
}
1717+
++JTI;
1718+
}
1719+
16961720
// Create labels for all entries.
16971721
for (auto &JTI : JumpTables) {
16981722
JumpTable &JT = *JTI.second;

bolt/lib/Rewrite/LinuxKernelRewriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ void LinuxKernelRewriter::processLKKSymtab(bool IsGPL) {
393393

394394
for (uint64_t I = 0; I < SectionSize; I += 4) {
395395
const uint64_t EntryAddress = SectionAddress + I;
396-
ErrorOr<uint64_t> Offset = BC.getSignedValueAtAddress(EntryAddress, 4);
396+
ErrorOr<int64_t> Offset = BC.getSignedValueAtAddress(EntryAddress, 4);
397397
assert(Offset && "Reading valid PC-relative offset for a ksymtab entry");
398398
const int32_t SignedOffset = *Offset;
399399
const uint64_t RefAddress = EntryAddress + SignedOffset;

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,6 +1932,19 @@ class X86MCPlusBuilder : public MCPlusBuilder {
19321932
// = R_X86_64_PC32(Ln) + En - JT
19331933
// = R_X86_64_PC32(Ln + offsetof(En))
19341934
//
1935+
auto isRIPRel = [&](X86MemOperand &MO) {
1936+
// NB: DispExpr should be set
1937+
return MO.DispExpr != nullptr &&
1938+
MO.BaseRegNum == RegInfo->getProgramCounter() &&
1939+
MO.IndexRegNum == X86::NoRegister &&
1940+
MO.SegRegNum == X86::NoRegister;
1941+
};
1942+
auto isIndexed = [](X86MemOperand &MO, MCPhysReg R) {
1943+
// NB: IndexRegNum should be set.
1944+
return MO.IndexRegNum != X86::NoRegister && MO.BaseRegNum == R &&
1945+
MO.ScaleImm == 4 && MO.DispImm == 0 &&
1946+
MO.SegRegNum == X86::NoRegister;
1947+
};
19351948
LLVM_DEBUG(dbgs() << "Checking for PIC jump table\n");
19361949
MCInst *MemLocInstr = nullptr;
19371950
const MCInst *MovInstr = nullptr;
@@ -1965,9 +1978,8 @@ class X86MCPlusBuilder : public MCPlusBuilder {
19651978
std::optional<X86MemOperand> MO = evaluateX86MemoryOperand(Instr);
19661979
if (!MO)
19671980
break;
1968-
if (MO->BaseRegNum != R1 || MO->ScaleImm != 4 ||
1969-
MO->IndexRegNum == X86::NoRegister || MO->DispImm != 0 ||
1970-
MO->SegRegNum != X86::NoRegister)
1981+
if (!isIndexed(*MO, R1))
1982+
// POSSIBLE_PIC_JUMP_TABLE
19711983
break;
19721984
MovInstr = &Instr;
19731985
} else {
@@ -1986,9 +1998,7 @@ class X86MCPlusBuilder : public MCPlusBuilder {
19861998
std::optional<X86MemOperand> MO = evaluateX86MemoryOperand(Instr);
19871999
if (!MO)
19882000
break;
1989-
if (MO->BaseRegNum != RegInfo->getProgramCounter() ||
1990-
MO->IndexRegNum != X86::NoRegister ||
1991-
MO->SegRegNum != X86::NoRegister || MO->DispExpr == nullptr)
2001+
if (!isRIPRel(*MO))
19922002
break;
19932003
MemLocInstr = &Instr;
19942004
break;
@@ -2105,13 +2115,15 @@ class X86MCPlusBuilder : public MCPlusBuilder {
21052115
return IndirectBranchType::POSSIBLE_FIXED_BRANCH;
21062116
}
21072117

2108-
if (Type == IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE &&
2109-
(MO->ScaleImm != 1 || MO->BaseRegNum != RIPRegister))
2110-
return IndirectBranchType::UNKNOWN;
2111-
2112-
if (Type != IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE &&
2113-
MO->ScaleImm != PtrSize)
2114-
return IndirectBranchType::UNKNOWN;
2118+
switch (Type) {
2119+
case IndirectBranchType::POSSIBLE_PIC_JUMP_TABLE:
2120+
if (MO->ScaleImm != 1 || MO->BaseRegNum != RIPRegister)
2121+
return IndirectBranchType::UNKNOWN;
2122+
break;
2123+
default:
2124+
if (MO->ScaleImm != PtrSize)
2125+
return IndirectBranchType::UNKNOWN;
2126+
}
21152127

21162128
MemLocInstrOut = MemLocInstr;
21172129

0 commit comments

Comments
 (0)