[llvm] [BOLT] Enable re-writing of Linux kernel binary (PR #80228)

Maksim Panchenko via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 31 17:43:32 PST 2024


https://github.com/maksfb created https://github.com/llvm/llvm-project/pull/80228

Write modified Linux kernel binary to disk. The output is not supposed to be functional at the moment, but it will allow for future patches to test the output binary.

>From 799b7d8980367488f6a41ee9c5fe88fcb4e1753d Mon Sep 17 00:00:00 2001
From: Maksim Panchenko <maks at fb.com>
Date: Wed, 31 Jan 2024 17:28:32 -0800
Subject: [PATCH] [BOLT] Enable re-writing of Linux kernel binary

Write modified Linux kernel binary to disk. The output is not supposed
to be functional at the moment, but it will allow for future patches to
test the output binary.
---
 bolt/lib/Rewrite/RewriteInstance.cpp | 21 ++++++++++++++-------
 bolt/test/X86/linux-orc.s            |  2 ++
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index dee1bf125f0a7..9ff5c9d448daa 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -757,12 +757,11 @@ Error RewriteInstance::run() {
   if (opts::Instrument && !BC->IsStaticExecutable)
     updateRtFiniReloc();
 
-  if (BC->IsLinuxKernel) {
-    errs() << "BOLT-WARNING: not writing the output file for Linux Kernel\n";
-    return Error::success();
-  } else if (opts::OutputFilename == "/dev/null") {
+  if (opts::OutputFilename == "/dev/null") {
     outs() << "BOLT-INFO: skipping writing final binary to disk\n";
     return Error::success();
+  } else if (BC->IsLinuxKernel) {
+    errs() << "BOLT-WARNING: Linux kernel support is experimental\n";
   }
 
   // Rewrite allocatable contents and copy non-allocatable parts with mods.
@@ -1860,6 +1859,11 @@ Error RewriteInstance::readSpecialSections() {
   BC->HasRelocations =
       HasTextRelocations && (opts::RelocationMode != cl::BOU_FALSE);
 
+  if (BC->IsLinuxKernel && BC->HasRelocations) {
+    outs() << "BOLT-INFO: disabling relocation mode for Linux kernel\n";
+    BC->HasRelocations = false;
+  }
+
   BC->IsStripped = !HasSymbolTable;
 
   if (BC->IsStripped && !opts::AllowStripped) {
@@ -4369,8 +4373,10 @@ void RewriteInstance::patchELFSectionHeaderTable(ELFObjectFile<ELFT> *File) {
     assert((NewEhdr.e_entry || !Obj.getHeader().e_entry) &&
            "cannot find new address for entry point");
   }
-  NewEhdr.e_phoff = PHDRTableOffset;
-  NewEhdr.e_phnum = Phnum;
+  if (PHDRTableOffset) {
+    NewEhdr.e_phoff = PHDRTableOffset;
+    NewEhdr.e_phnum = Phnum;
+  }
   NewEhdr.e_shoff = SHTOffset;
   NewEhdr.e_shnum = OutputSections.size();
   NewEhdr.e_shstrndx = NewSectionIndex[NewEhdr.e_shstrndx];
@@ -5505,7 +5511,8 @@ void RewriteInstance::rewriteFile() {
     addBATSection();
 
   // Patch program header table.
-  patchELFPHDRTable();
+  if (!BC->IsLinuxKernel)
+    patchELFPHDRTable();
 
   // Finalize memory image of section string table.
   finalizeSectionStringTable();
diff --git a/bolt/test/X86/linux-orc.s b/bolt/test/X86/linux-orc.s
index 3d8a3d77ec2ea..bd652eafa23dd 100644
--- a/bolt/test/X86/linux-orc.s
+++ b/bolt/test/X86/linux-orc.s
@@ -52,6 +52,8 @@ bar:
 .L4:
   .size bar, .-bar
 
+# CHECK: BOLT-WARNING: Linux kernel support is experimental
+
   .section .orc_unwind,"a", at progbits
   .align 4
   .section .orc_unwind_ip,"a", at progbits



More information about the llvm-commits mailing list