Skip to content

Commit 6b931b3

Browse files
committed
[BOLT] Detect Linux kernel version if the binary is a Linux kernel
Use struct instead of tuple to represent Linux kernel version
1 parent 69554a8 commit 6b931b3

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,34 @@ struct JournalingStreams {
180180
Error createNonFatalBOLTError(const Twine &S);
181181
Error createFatalBOLTError(const Twine &S);
182182

183+
/// Linux kernel version
184+
struct LKVersion {
185+
LKVersion() {}
186+
LKVersion(unsigned Major, unsigned Minor, unsigned Rev)
187+
: Major(Major), Minor(Minor), Rev(Rev) {}
188+
189+
bool operator<(const LKVersion &Other) const {
190+
return std::make_tuple(Major, Minor, Rev) <
191+
std::make_tuple(Other.Major, Other.Minor, Other.Rev);
192+
}
193+
194+
bool operator>(const LKVersion &Other) const { return Other < *this; }
195+
196+
bool operator<=(const LKVersion &Other) const { return !(*this > Other); }
197+
198+
bool operator>=(const LKVersion &Other) const { return !(*this < Other); }
199+
200+
bool operator==(const LKVersion &Other) const {
201+
return Major == Other.Major && Minor == Other.Minor && Rev == Other.Rev;
202+
}
203+
204+
bool operator!=(const LKVersion &Other) const { return !(*this == Other); }
205+
206+
unsigned Major{0};
207+
unsigned Minor{0};
208+
unsigned Rev{0};
209+
};
210+
183211
class BinaryContext {
184212
BinaryContext() = delete;
185213

@@ -670,8 +698,7 @@ class BinaryContext {
670698
/// Indicates if the binary is Linux kernel.
671699
bool IsLinuxKernel{false};
672700

673-
/// Linux kernel version (major, minor, reversion)
674-
std::tuple<unsigned, unsigned, unsigned> LinuxKernelVersion;
701+
LKVersion LinuxKernelVersion;
675702

676703
/// Indicates if relocations are available for usage.
677704
bool HasRelocations{false};

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ void RewriteInstance::discoverFileObjects() {
10451045
unsigned Major = std::stoi(Match[2].str());
10461046
unsigned Minor = std::stoi(Match[3].str());
10471047
unsigned Rev = Match.size() > 5 ? std::stoi(Match[5].str()) : 0;
1048-
BC->LinuxKernelVersion = std::make_tuple(Major, Minor, Rev);
1048+
BC->LinuxKernelVersion = LKVersion(Major, Minor, Rev);
10491049
BC->outs() << "BOLT-INFO: Linux kernel version is " << Match[1].str();
10501050
}
10511051
}
@@ -1225,7 +1225,7 @@ void RewriteInstance::discoverFileObjects() {
12251225
PreviousFunction = BF;
12261226
}
12271227

1228-
if (BC->IsLinuxKernel && !std::get<0>(BC->LinuxKernelVersion))
1228+
if (BC->IsLinuxKernel && !BC->LinuxKernelVersion.Major)
12291229
BC->errs() << "BOLT-WARNING: Linux kernel version is unknown\n";
12301230

12311231
// Read dynamic relocation first as their presence affects the way we process

bolt/unittests/Core/BinaryContext.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,17 @@ TEST_P(BinaryContextTester, BaseAddressSegmentsSmallerThanAlignment) {
218218
ASSERT_TRUE(BaseAddress.has_value());
219219
ASSERT_EQ(*BaseAddress, 0xaaaaaaaa0000ULL);
220220
}
221+
222+
TEST(BinaryContextTester, LKVersion) {
223+
LKVersion V1;
224+
LKVersion V2(5, 9, 15);
225+
LKVersion V3(6, 12, 1);
226+
227+
V1 = V3;
228+
ASSERT_TRUE(V1 == V3);
229+
ASSERT_TRUE(V2 != V3);
230+
ASSERT_TRUE(V2 < V3);
231+
ASSERT_TRUE(V2 <= V3);
232+
ASSERT_TRUE(V3 > V2);
233+
ASSERT_TRUE(V3 >= V2);
234+
}

0 commit comments

Comments
 (0)