Skip to content

Commit

Permalink
[BOLT] Enable re-writing of Linux kernel binary (llvm#80228)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
maksfb authored and agozillon committed Feb 5, 2024
1 parent 3da5060 commit 9b5fd86
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 14 additions & 7 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1848,6 +1847,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) {
Expand Down Expand Up @@ -4357,8 +4361,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];
Expand Down Expand Up @@ -5493,7 +5499,8 @@ void RewriteInstance::rewriteFile() {
addBATSection();

// Patch program header table.
patchELFPHDRTable();
if (!BC->IsLinuxKernel)
patchELFPHDRTable();

// Finalize memory image of section string table.
finalizeSectionStringTable();
Expand Down
2 changes: 2 additions & 0 deletions bolt/test/X86/linux-orc.s
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ bar:
.L4:
.size bar, .-bar

# CHECK: BOLT-WARNING: Linux kernel support is experimental

.section .orc_unwind,"a",@progbits
.align 4
.section .orc_unwind_ip,"a",@progbits
Expand Down

0 comments on commit 9b5fd86

Please sign in to comment.