Skip to content

[BOLT][NFC] Pass JumpTable to analyzeJumpTable #132110

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

Open
wants to merge 2 commits into
base: users/aaupov/spr/main.boltnfc-pass-jumptable-to-analyzejumptable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions bolt/include/bolt/Core/BinaryContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,14 +584,13 @@ class BinaryContext {
/// If \p NextJTAddress is different from zero, it is used as an upper
/// bound for jump table memory layout.
///
/// Optionally, populate \p Address from jump table entries. The entries
/// could be partially populated if the jump table detection fails.
/// If \p JT is set, populate it with jump table entries. The entries could be
/// partially populated if the jump table detection fails.
bool analyzeJumpTable(const uint64_t Address,
const JumpTable::JumpTableType Type,
const BinaryFunction &BF,
const uint64_t NextJTAddress = 0,
JumpTable::AddressesType *EntriesAsAddress = nullptr,
bool *HasEntryInFragment = nullptr) const;
JumpTable *JT = nullptr) const;

/// After jump table locations are established, this function will populate
/// their EntriesAsAddress based on memory contents.
Expand Down
24 changes: 11 additions & 13 deletions bolt/lib/Core/BinaryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,7 @@ bool BinaryContext::analyzeJumpTable(const uint64_t Address,
const JumpTable::JumpTableType Type,
const BinaryFunction &BF,
const uint64_t NextJTAddress,
JumpTable::AddressesType *EntriesAsAddress,
bool *HasEntryInFragment) const {
JumpTable *JT) const {
// Target address of __builtin_unreachable.
const uint64_t UnreachableAddress = BF.getAddress() + BF.getSize();

Expand All @@ -571,11 +570,11 @@ bool BinaryContext::analyzeJumpTable(const uint64_t Address,
size_t TrimmedSize = 0;

auto addEntryAddress = [&](uint64_t EntryAddress, bool Unreachable = false) {
if (!EntriesAsAddress)
if (!JT)
return;
EntriesAsAddress->emplace_back(EntryAddress);
JT->EntriesAsAddress.emplace_back(EntryAddress);
if (!Unreachable)
TrimmedSize = EntriesAsAddress->size();
TrimmedSize = JT->EntriesAsAddress.size();
};

ErrorOr<const BinarySection &> Section = getSectionForAddress(Address);
Expand Down Expand Up @@ -676,17 +675,17 @@ bool BinaryContext::analyzeJumpTable(const uint64_t Address,
++NumRealEntries;
LLVM_DEBUG(dbgs() << formatv("OK: {0:x} real entry\n", Value));

if (TargetBF != &BF && HasEntryInFragment)
*HasEntryInFragment = true;
if (TargetBF != &BF && JT)
JT->IsSplit = true;
addEntryAddress(Value);
}

// Trim direct/normal jump table to exclude trailing unreachable entries that
// can collide with a function address.
if (Type == JumpTable::JTT_X86_64_ABS && EntriesAsAddress &&
TrimmedSize != EntriesAsAddress->size() &&
if (Type == JumpTable::JTT_X86_64_ABS && JT &&
TrimmedSize != JT->EntriesAsAddress.size() &&
getBinaryFunctionAtAddress(UnreachableAddress))
EntriesAsAddress->resize(TrimmedSize);
JT->EntriesAsAddress.resize(TrimmedSize);

// It's a jump table if the number of real entries is more than 1, or there's
// one real entry and one or more special targets. If there are only multiple
Expand All @@ -710,9 +709,8 @@ void BinaryContext::populateJumpTables() {
if (NextJTI != JTE)
NextJTAddress = NextJTI->second->getAddress();

const bool Success =
analyzeJumpTable(JT->getAddress(), JT->Type, *(JT->Parents[0]),
NextJTAddress, &JT->EntriesAsAddress, &JT->IsSplit);
const bool Success = analyzeJumpTable(
JT->getAddress(), JT->Type, *JT->Parents.front(), NextJTAddress, JT);
if (!Success) {
LLVM_DEBUG({
dbgs() << "failed to analyze ";
Expand Down
Loading