Skip to content

[BOLT][Linux] Fix linux_banner lookup #144962

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
Jun 20, 2025
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
42 changes: 25 additions & 17 deletions bolt/lib/Rewrite/LinuxKernelRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,25 +432,33 @@ class LinuxKernelRewriter final : public MetadataRewriter {
};

Error LinuxKernelRewriter::detectLinuxKernelVersion() {
if (BinaryData *BD = BC.getBinaryDataByName("linux_banner")) {
const BinarySection &Section = BD->getSection();
const std::string S =
Section.getContents().substr(BD->getOffset(), BD->getSize()).str();

const std::regex Re(R"---(Linux version ((\d+)\.(\d+)(\.(\d+))?))---");
std::smatch Match;
if (std::regex_search(S, Match, Re)) {
const unsigned Major = std::stoi(Match[2].str());
const unsigned Minor = std::stoi(Match[3].str());
const unsigned Rev = Match[5].matched ? std::stoi(Match[5].str()) : 0;
LinuxKernelVersion = LKVersion(Major, Minor, Rev);
BC.outs() << "BOLT-INFO: Linux kernel version is " << Match[1].str()
<< "\n";
return Error::success();
}
// Check for global and local linux_banner symbol.
BinaryData *BD = BC.getBinaryDataByName("linux_banner");
if (!BD)
BD = BC.getBinaryDataByName("linux_banner/1");

if (!BD)
return createStringError(errc::executable_format_error,
"unable to locate linux_banner");

const BinarySection &Section = BD->getSection();
const std::string S =
Section.getContents().substr(BD->getOffset(), BD->getSize()).str();

const std::regex Re(R"---(Linux version ((\d+)\.(\d+)(\.(\d+))?))---");
std::smatch Match;
if (std::regex_search(S, Match, Re)) {
const unsigned Major = std::stoi(Match[2].str());
const unsigned Minor = std::stoi(Match[3].str());
const unsigned Rev = Match[5].matched ? std::stoi(Match[5].str()) : 0;
LinuxKernelVersion = LKVersion(Major, Minor, Rev);
BC.outs() << "BOLT-INFO: Linux kernel version is " << Match[1].str()
<< "\n";
return Error::success();
}

return createStringError(errc::executable_format_error,
"Linux kernel version is unknown");
"Linux kernel version is unknown: " + S);
}

void LinuxKernelRewriter::processLKSections() {
Expand Down
11 changes: 11 additions & 0 deletions bolt/test/X86/linux-version.S
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
# RUN: -Wl,--image-base=0xffffffff80000000,--no-dynamic-linker,--no-eh-frame-hdr
# RUN: llvm-bolt %t.exe -o %t.out 2>&1 | FileCheck --check-prefix=CHECK-C %s

# RUN: %clang -DD -target x86_64-unknown-unknown \
# RUN: %cflags -nostdlib %s -o %t.exe \
# RUN: -Wl,--image-base=0xffffffff80000000,--no-dynamic-linker,--no-eh-frame-hdr
# RUN: llvm-bolt %t.exe -o %t.out 2>&1 | FileCheck --check-prefix=CHECK-D %s

.text
.globl foo
.type foo, %function
Expand Down Expand Up @@ -46,6 +51,12 @@ linux_banner:
#endif
# CHECK-C: BOLT-INFO: Linux kernel version is 6.6

#ifdef D
.hidden linux_banner
.string "Linux version 6.6.15.2-2-xxx\n"
#endif
# CHECK-D: BOLT-INFO: Linux kernel version is 6.6

.size linux_banner, . - linux_banner

## Fake Linux Kernel sections.
Expand Down
Loading