Skip to content

Commit

Permalink
Merge branch 'llvm:main' into check-version
Browse files Browse the repository at this point in the history
  • Loading branch information
ZijunZhaoCCK authored Feb 1, 2024
2 parents d657b7f + 4d89356 commit 6173010
Show file tree
Hide file tree
Showing 176 changed files with 7,318 additions and 1,771 deletions.
51 changes: 29 additions & 22 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 @@ -4141,10 +4145,10 @@ RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,

// Keep track of section header entries attached to the corresponding section.
std::vector<std::pair<BinarySection *, ELFShdrTy>> OutputSections;
auto addSection = [&](const ELFShdrTy &Section, BinarySection *BinSec) {
auto addSection = [&](const ELFShdrTy &Section, BinarySection &BinSec) {
ELFShdrTy NewSection = Section;
NewSection.sh_name = SHStrTab.getOffset(BinSec->getOutputName());
OutputSections.emplace_back(BinSec, std::move(NewSection));
NewSection.sh_name = SHStrTab.getOffset(BinSec.getOutputName());
OutputSections.emplace_back(&BinSec, std::move(NewSection));
};

// Copy over entries for original allocatable sections using modified name.
Expand All @@ -4162,7 +4166,7 @@ RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,
BinarySection *BinSec = BC->getSectionForSectionRef(SecRef);
assert(BinSec && "Matching BinarySection should exist.");

addSection(Section, BinSec);
addSection(Section, *BinSec);
}

for (BinarySection &Section : BC->allocatableSections()) {
Expand All @@ -4189,7 +4193,7 @@ RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,
NewSection.sh_link = 0;
NewSection.sh_info = 0;
NewSection.sh_addralign = Section.getAlignment();
addSection(NewSection, &Section);
addSection(NewSection, Section);
}

// Sort all allocatable sections by their offset.
Expand All @@ -4203,19 +4207,19 @@ RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,
for (auto &SectionKV : OutputSections) {
ELFShdrTy &Section = SectionKV.second;

// TBSS section does not take file or memory space. Ignore it for layout
// purposes.
if (Section.sh_type == ELF::SHT_NOBITS && (Section.sh_flags & ELF::SHF_TLS))
// Ignore TLS sections as they don't take any space in the file.
if (Section.sh_type == ELF::SHT_NOBITS)
continue;

// Note that address continuity is not guaranteed as sections could be
// placed in different loadable segments.
if (PrevSection &&
PrevSection->sh_addr + PrevSection->sh_size > Section.sh_addr) {
if (opts::Verbosity > 1)
PrevSection->sh_offset + PrevSection->sh_size > Section.sh_offset) {
if (opts::Verbosity > 1) {
outs() << "BOLT-INFO: adjusting size for section "
<< PrevBinSec->getOutputName() << '\n';
PrevSection->sh_size = Section.sh_addr > PrevSection->sh_addr
? Section.sh_addr - PrevSection->sh_addr
: 0;
}
PrevSection->sh_size = Section.sh_offset - PrevSection->sh_offset;
}

PrevSection = &Section;
Expand Down Expand Up @@ -4249,7 +4253,7 @@ RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,
if (NewSection.sh_type == ELF::SHT_SYMTAB)
NewSection.sh_info = NumLocalSymbols;

addSection(NewSection, BinSec);
addSection(NewSection, *BinSec);

LastFileOffset = BinSec->getOutputFileOffset();
}
Expand All @@ -4274,7 +4278,7 @@ RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,
NewSection.sh_info = 0;
NewSection.sh_addralign = Section.getAlignment();

addSection(NewSection, &Section);
addSection(NewSection, Section);
}

// Assign indices to sections.
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
52 changes: 52 additions & 0 deletions bolt/test/X86/phdr-out-of-order.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Check that llvm-bolt correctly processes a binary with program headers and
## corresponding sections specified in non-ascending address order.

RUN: split-file %s %t
RUN: yaml2obj %t/yaml -o %t.exe --max-size=0
RUN: llvm-bolt %t.exe -o %t.bolt --allow-stripped
RUN: llvm-readelf -WS %t.bolt | FileCheck %s

CHECK: .a PROGBITS 0000000000400000 [[#%.6x, OFFSET:]] 000001
CHECK-NEXT: .b PROGBITS 0000000000000000 [[#%.6x, OFFSET+1]] 000001
CHECK-NEXT: .c PROGBITS 0000000000600000 [[#%.6x, OFFSET+2]] 000001

#--- yaml
--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
ProgramHeaders:
- Type: PT_LOAD
FirstSec: .a
LastSec: .a
VAddr: 0x400000
- Type: PT_LOAD
FirstSec: .b
LastSec: .b
VAddr: 0x0
- Type: PT_LOAD
FirstSec: .c
LastSec: .c
VAddr: 0x600000
Sections:
- Name: .a
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Content: 00
AddressAlign: 0x1
Address: 0x400000
- Name: .b
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Content: 00
AddressAlign: 0x1
Address: 0x0
- Name: .c
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Content: 00
AddressAlign: 0x1
Address: 0x600000
...
Loading

0 comments on commit 6173010

Please sign in to comment.