[llvm] [BOLT] Detect Linux kernel version if the binary is a Linux kernel (PR #119088)

Maksim Panchenko via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 8 19:01:23 PST 2024


================
@@ -1030,6 +1031,25 @@ void RewriteInstance::discoverFileObjects() {
       continue;
     }
 
+    if (BC->IsLinuxKernel && SymName == "linux_banner") {
+      const StringRef SectionContents =
+          cantFail(Section->getContents(), "can not get section contents");
+      const std::string S =
+          SectionContents
+              .substr(SymbolAddress - Section->getAddress(), SymbolSize)
+              .str();
+
+      const std::regex Re(R"---(Linux version ((\d+)\.(\d+)(\.(\d+))?))---");
+      std::smatch Match;
+      if (std::regex_search(S, Match, Re)) {
+        unsigned Major = std::stoi(Match[2].str());
+        unsigned Minor = std::stoi(Match[3].str());
+        unsigned Rev = Match.size() > 5 ? std::stoi(Match[5].str()) : 0;
----------------
maksfb wrote:

nit: constify.

https://github.com/llvm/llvm-project/pull/119088


More information about the llvm-commits mailing list