-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
aaupov
wants to merge
1
commit into
users/aaupov/spr/main.boltaarch64-add-jump-table-support-using-llvm_jump_table_info
Choose a base branch
from
users/aaupov/spr/boltaarch64-add-jump-table-support-using-llvm_jump_table_info
base: users/aaupov/spr/main.boltaarch64-add-jump-table-support-using-llvm_jump_table_info
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
You are viewing a condensed version of this merge commit. You can view the full changes here.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
here can be a gap between JTs, it's better to check $d symbol or next JT address
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.
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).