Skip to content

[BOLT] support AArch64 JUMP26 createRelocation #83531

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

Merged
merged 1 commit into from
Mar 4, 2024
Merged
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
8 changes: 8 additions & 0 deletions bolt/lib/Core/Relocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ static uint64_t encodeValueAArch64(uint64_t Type, uint64_t Value, uint64_t PC) {
// OP 1001_01 goes in bits 31:26 of BL.
Value = ((Value >> 2) & 0x3ffffff) | 0x94000000ULL;
break;
case ELF::R_AARCH64_JUMP26:
Value -= PC;
assert(isInt<28>(Value) &&
"only PC +/- 128MB is allowed for direct branch");
// Immediate goes in bits 25:0 of B.
// OP 0001_01 goes in bits 31:26 of B.
Value = ((Value >> 2) & 0x3ffffff) | 0x14000000ULL;
break;
}
return Value;
}
Expand Down
3 changes: 3 additions & 0 deletions bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,9 @@ class AArch64MCPlusBuilder : public MCPlusBuilder {
uint64_t RelType;
if (Fixup.getKind() == MCFixupKind(AArch64::fixup_aarch64_pcrel_call26))
RelType = ELF::R_AARCH64_CALL26;
else if (Fixup.getKind() ==
MCFixupKind(AArch64::fixup_aarch64_pcrel_branch26))
RelType = ELF::R_AARCH64_JUMP26;
else if (FKI.Flags & MCFixupKindInfo::FKF_IsPCRel) {
switch (FKI.TargetSize) {
default:
Expand Down
43 changes: 43 additions & 0 deletions bolt/unittests/Core/BinaryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,49 @@ TEST_P(BinaryContextTester, FlushPendingRelocCALL26) {
EXPECT_FALSE(memcmp(Func2Call, &Vect[12], 4)) << "Wrong forward call value\n";
}

TEST_P(BinaryContextTester, FlushPendingRelocJUMP26) {
if (GetParam() != Triple::aarch64)
GTEST_SKIP();

// This test checks that encodeValueAArch64 used by flushPendingRelocations
// returns correctly encoded values for R_AARCH64_JUMP26 relocation for both
// backward and forward branches.
//
// The offsets layout is:
// 4: func1
// 8: b func1
// 12: b func2
// 16: func2

const uint64_t Size = 20;
char *Data = new char[Size];
BinarySection &BS = BC->registerOrUpdateSection(
".text", ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
(uint8_t *)Data, Size, 4);
MCSymbol *RelSymbol1 = BC->getOrCreateGlobalSymbol(4, "Func1");
ASSERT_TRUE(RelSymbol1);
BS.addRelocation(8, RelSymbol1, ELF::R_AARCH64_JUMP26, 0, 0, true);
MCSymbol *RelSymbol2 = BC->getOrCreateGlobalSymbol(16, "Func2");
ASSERT_TRUE(RelSymbol2);
BS.addRelocation(12, RelSymbol2, ELF::R_AARCH64_JUMP26, 0, 0, true);

std::error_code EC;
SmallVector<char> Vect(Size);
raw_svector_ostream OS(Vect);

BS.flushPendingRelocations(OS, [&](const MCSymbol *S) {
return S == RelSymbol1 ? 4 : S == RelSymbol2 ? 16 : 0;
});

const uint8_t Func1Call[4] = {255, 255, 255, 23};
const uint8_t Func2Call[4] = {1, 0, 0, 20};

EXPECT_FALSE(memcmp(Func1Call, &Vect[8], 4))
<< "Wrong backward branch value\n";
EXPECT_FALSE(memcmp(Func2Call, &Vect[12], 4))
<< "Wrong forward branch value\n";
}

#endif

TEST_P(BinaryContextTester, BaseAddress) {
Expand Down