Skip to content

[BOLT][AArch64] Add jump table support using .llvm_jump_table_info #132114

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

Draft
wants to merge 1 commit into
base: users/aaupov/spr/main.boltaarch64-add-jump-table-support-using-llvm_jump_table_info
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions bolt/include/bolt/Core/JumpTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BinaryFunction;
/// a different label at a different offset in this jump table.
class JumpTable : public BinaryData {
friend class BinaryContext;
friend class JumpTableInfoReader;

JumpTable() = delete;
JumpTable(const JumpTable &) = delete;
Expand Down Expand Up @@ -116,6 +117,16 @@ class JumpTable : public BinaryData {
/// BinaryFunction this jump tables belongs to.
SmallVector<BinaryFunction *, 1> Parents;

///
/// AArch64-specific fields
///

/// Entries are offsets relative to an arbitrary function location.
uint64_t BaseAddress{0};

/// Address of the instruction referencing the jump table (MemLocInstr).
uint64_t MemLocInstrAddress{0};

private:
/// Constructor should only be called by a BinaryContext.
JumpTable(MCSymbol &Symbol, uint64_t Address, size_t EntrySize,
Expand Down
2 changes: 2 additions & 0 deletions bolt/include/bolt/Rewrite/MetadataRewriters.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ std::unique_ptr<MetadataRewriter> createPseudoProbeRewriter(BinaryContext &);

std::unique_ptr<MetadataRewriter> createSDTRewriter(BinaryContext &);

std::unique_ptr<MetadataRewriter> createJumpTableInfoReader(BinaryContext &);

} // namespace bolt
} // namespace llvm

Expand Down
14 changes: 13 additions & 1 deletion bolt/lib/Core/BinaryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,15 @@ bool BinaryContext::analyzeJumpTable(const uint64_t Address,
case JumpTable::JTT_X86_64_ABS:
Value = *getPointerAtAddress(EntryAddress);
break;
case JumpTable::JTT_AARCH64_REL1:
case JumpTable::JTT_AARCH64_REL2:
case JumpTable::JTT_AARCH64_REL4:
unsigned ShiftAmt = Type == JumpTable::JTT_AARCH64_REL4 ? 0 : 2;
assert(JT &&
"jump table must be non-null for AArch64 in analyzeJumpTable");
Value = JT->BaseAddress +
(*getUnsignedValueAtAddress(EntryAddress, EntrySize) << ShiftAmt);
break;
}

// __builtin_unreachable() case.
Expand Down Expand Up @@ -704,7 +713,10 @@ void BinaryContext::populateJumpTables() {

uint64_t NextJTAddress = 0;
auto NextJTI = std::next(JTI);
if (NextJTI != JTE)
if (isAArch64()) {
Copy link
Contributor

@yavtuk yavtuk Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here can be a gap between JTs, it's better to check $d symbol or next JT address

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for a suggestion, this part is a bit nuanced and needs a comment.
First, there's an implicit assumption that on ARM jump tables are only created in JumpTableInfoReader, not through instruction sequence/memory analysis as on X86.
Second, since jump table size is known in advance, NextJTAddress is just used as end address. It's not actually used to find the next jump table (neither on X86).

NextJTAddress = JT->getAddress() + JT->getSize();
JT->Entries.clear();
} else if (NextJTI != JTE)
NextJTAddress = NextJTI->second->getAddress();

const bool Success = analyzeJumpTable(
Expand Down
1 change: 1 addition & 0 deletions bolt/lib/Rewrite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_llvm_library(LLVMBOLTRewrite
DWARFRewriter.cpp
ExecutableFileMemoryManager.cpp
JITLinkLinker.cpp
JumpTableInfoReader.cpp
LinuxKernelRewriter.cpp
MachORewriteInstance.cpp
MetadataManager.cpp
Expand Down
93 changes: 93 additions & 0 deletions bolt/lib/Rewrite/JumpTableInfoReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//===- bolt/Rewrite/JumpTableInfoReader.cpp -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Read .llvm_jump_table_info section and register jump tables.
//
//===----------------------------------------------------------------------===//

#include "bolt/Core/JumpTable.h"
#include "bolt/Rewrite/MetadataRewriter.h"
#include "bolt/Rewrite/MetadataRewriters.h"
#include "llvm/Support/DataExtractor.h"

using namespace llvm;
using namespace bolt;

namespace {
class JumpTableInfoReader final : public MetadataRewriter {

public:
JumpTableInfoReader(StringRef Name, BinaryContext &BC)
: MetadataRewriter(Name, BC) {}
Error preDisasmInitializer() override;
};

Error JumpTableInfoReader::preDisasmInitializer() {
if (!BC.isAArch64())
return Error::success();

ErrorOr<BinarySection &> ErrorOrJTInfoSection =
BC.getUniqueSectionByName(".llvm_jump_table_info");
if (std::error_code E = ErrorOrJTInfoSection.getError())
return Error::success();
BinarySection &JTInfoSection = *ErrorOrJTInfoSection;
StringRef Buf = JTInfoSection.getContents();
DataExtractor DE = DataExtractor(Buf, BC.AsmInfo->isLittleEndian(),
BC.AsmInfo->getCodePointerSize());
DataExtractor::Cursor Cursor(0);
while (Cursor && !DE.eof(Cursor)) {
const uint8_t Format = DE.getU8(Cursor);
const uint64_t JTAddr = DE.getAddress(Cursor);
const uint64_t JTBase = DE.getAddress(Cursor);
const uint64_t JTLoad = DE.getAddress(Cursor);
const uint64_t Branch = DE.getAddress(Cursor);
const uint64_t NumEntries = DE.getULEB128(Cursor);

JumpTable::JumpTableType Type = JumpTable::JTT_AARCH64_LAST;
switch (Format) {
case 2:
Type = JumpTable::JTT_AARCH64_REL1;
break;
case 3:
Type = JumpTable::JTT_AARCH64_REL2;
break;
case 4:
Type = JumpTable::JTT_AARCH64_REL4;
break;
}

if (Type == JumpTable::JTT_AARCH64_LAST) {
errs() << "BOLT-WARNING: unknown jump table info type " << Format
<< " for jump table " << Twine::utohexstr(JTAddr) << '\n';
continue;
}

BinaryFunction *BF = BC.getBinaryFunctionContainingAddress(Branch);
if (!BF) {
BC.errs() << "BOLT-WARNING: binary function not found for jump table "
"with address "
<< Twine::utohexstr(JTAddr) << " and branch "
<< Twine::utohexstr(Branch) << '\n';
continue;
}
const MCSymbol *JTSym = BC.getOrCreateJumpTable(*BF, JTAddr, Type);
assert(JTSym && "failed to create a jump table");
JumpTable *JT = BC.getJumpTableContainingAddress(JTAddr);
assert(JT && "internal error creating jump table");
JT->BaseAddress = JTBase;
JT->MemLocInstrAddress = JTLoad;
JT->Entries.resize(NumEntries);
}
return Cursor.takeError();
}
} // namespace

std::unique_ptr<MetadataRewriter>
llvm::bolt::createJumpTableInfoReader(BinaryContext &BC) {
return std::make_unique<JumpTableInfoReader>("jump-table-info-reader", BC);
}
2 changes: 2 additions & 0 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3227,6 +3227,8 @@ void RewriteInstance::initializeMetadataManager() {
MetadataManager.registerRewriter(createPseudoProbeRewriter(*BC));

MetadataManager.registerRewriter(createSDTRewriter(*BC));

MetadataManager.registerRewriter(createJumpTableInfoReader(*BC));
}

void RewriteInstance::processSectionMetadata() {
Expand Down
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.