Skip to content

[BOLT] Adjust section sizes based on file offsets #80226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4153,10 +4153,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 @@ -4174,7 +4174,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 @@ -4201,7 +4201,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 @@ -4215,19 +4215,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 @@ -4261,7 +4261,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 @@ -4286,7 +4286,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
...