Skip to content

Commit a693ae5

Browse files
authored
[BOLT] Enable re-writing of Linux kernel binary (#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.
1 parent 116e801 commit a693ae5

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -757,12 +757,11 @@ Error RewriteInstance::run() {
757757
if (opts::Instrument && !BC->IsStaticExecutable)
758758
updateRtFiniReloc();
759759

760-
if (BC->IsLinuxKernel) {
761-
errs() << "BOLT-WARNING: not writing the output file for Linux Kernel\n";
762-
return Error::success();
763-
} else if (opts::OutputFilename == "/dev/null") {
760+
if (opts::OutputFilename == "/dev/null") {
764761
outs() << "BOLT-INFO: skipping writing final binary to disk\n";
765762
return Error::success();
763+
} else if (BC->IsLinuxKernel) {
764+
errs() << "BOLT-WARNING: Linux kernel support is experimental\n";
766765
}
767766

768767
// Rewrite allocatable contents and copy non-allocatable parts with mods.
@@ -1848,6 +1847,11 @@ Error RewriteInstance::readSpecialSections() {
18481847
BC->HasRelocations =
18491848
HasTextRelocations && (opts::RelocationMode != cl::BOU_FALSE);
18501849

1850+
if (BC->IsLinuxKernel && BC->HasRelocations) {
1851+
outs() << "BOLT-INFO: disabling relocation mode for Linux kernel\n";
1852+
BC->HasRelocations = false;
1853+
}
1854+
18511855
BC->IsStripped = !HasSymbolTable;
18521856

18531857
if (BC->IsStripped && !opts::AllowStripped) {
@@ -4357,8 +4361,10 @@ void RewriteInstance::patchELFSectionHeaderTable(ELFObjectFile<ELFT> *File) {
43574361
assert((NewEhdr.e_entry || !Obj.getHeader().e_entry) &&
43584362
"cannot find new address for entry point");
43594363
}
4360-
NewEhdr.e_phoff = PHDRTableOffset;
4361-
NewEhdr.e_phnum = Phnum;
4364+
if (PHDRTableOffset) {
4365+
NewEhdr.e_phoff = PHDRTableOffset;
4366+
NewEhdr.e_phnum = Phnum;
4367+
}
43624368
NewEhdr.e_shoff = SHTOffset;
43634369
NewEhdr.e_shnum = OutputSections.size();
43644370
NewEhdr.e_shstrndx = NewSectionIndex[NewEhdr.e_shstrndx];
@@ -5493,7 +5499,8 @@ void RewriteInstance::rewriteFile() {
54935499
addBATSection();
54945500

54955501
// Patch program header table.
5496-
patchELFPHDRTable();
5502+
if (!BC->IsLinuxKernel)
5503+
patchELFPHDRTable();
54975504

54985505
// Finalize memory image of section string table.
54995506
finalizeSectionStringTable();

bolt/test/X86/linux-orc.s

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ bar:
5252
.L4:
5353
.size bar, .-bar
5454

55+
# CHECK: BOLT-WARNING: Linux kernel support is experimental
56+
5557
.section .orc_unwind,"a",@progbits
5658
.align 4
5759
.section .orc_unwind_ip,"a",@progbits

0 commit comments

Comments
 (0)