Skip to content

Commit

Permalink
[BOLT] Adjust section sizes based on file offsets (#80226)
Browse files Browse the repository at this point in the history
When we adjust section sizes while rewriting a binary, we should be
using section offsets and not addresses to determine if section overlap.
NFC for existing binaries.
  • Loading branch information
maksfb committed Feb 1, 2024
1 parent f883365 commit 116e801
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
30 changes: 15 additions & 15 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4141,10 +4141,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 +4162,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 +4189,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 +4203,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 +4249,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 +4274,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
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
...

0 comments on commit 116e801

Please sign in to comment.