Skip to content

Commit

Permalink
Overwrite contents of .debug_line section.
Browse files Browse the repository at this point in the history
Summary:
Overwrite contents of .debug_line section since we don't reference
the original contents anymore. This saves ~100MB of HHVM binary.

(cherry picked from FBD3314917)
  • Loading branch information
maksfb committed May 17, 2016
1 parent e63984f commit f047b9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
27 changes: 18 additions & 9 deletions bolt/RewriteInstance.cpp
Expand Up @@ -268,6 +268,7 @@ uint8_t *ExecutableFileMemoryManager::allocateSection(intptr_t Size,
return ret;
}

/// Notifier for non-allocatable (note) section.
uint8_t *ExecutableFileMemoryManager::recordNoteSection(
const uint8_t *Data,
uintptr_t Size,
Expand Down Expand Up @@ -758,8 +759,6 @@ void RewriteInstance::readSpecialSections() {
FrameHdrAddress = Section.getAddress();
FrameHdrContents = SectionContents;
FrameHdrAlign = Section.getAlignment();
} else if (SectionName == ".debug_line") {
DebugLineSize = Section.getSize();
} else if (SectionName == ".debug_ranges") {
DebugRangesSize = Section.getSize();
} else if (SectionName == ".debug_loc") {
Expand Down Expand Up @@ -1697,11 +1696,11 @@ void RewriteInstance::rewriteNoteSections() {
ErrorOr<StringRef> SectionName = Obj->getSectionName(&Section);
check_error(SectionName.getError(), "cannot get section name");

// Copy over section contents unless it's .debug_aranges, which shall be
// overwritten if -update-debug-sections is passed.
// New section size.
uint64_t Size = 0;

if (*SectionName != ".debug_aranges" || !opts::UpdateDebugSections) {
// Copy over section contents unless it's one of the sections we ovewrite.
if (!shouldOverwriteSection(*SectionName)) {
Size = Section.sh_size;
std::string Data = InputFile->getData().substr(Section.sh_offset, Size);
auto SectionPatchersIt = SectionPatchers.find(*SectionName);
Expand All @@ -1725,7 +1724,8 @@ void RewriteInstance::rewriteNoteSections() {
// Write section extension.
Address = SI.AllocAddress;
if (Address) {
DEBUG(dbgs() << "BOLT: appending contents to section "
DEBUG(dbgs() << "BOLT: " << (Size ? "appending" : "writing")
<< " contents to section "
<< *SectionName << '\n');
OS.write(reinterpret_cast<const char *>(Address), SI.Size);
Size += SI.Size;
Expand Down Expand Up @@ -2081,7 +2081,6 @@ void RewriteInstance::computeLineTableOffsets() {
continue;

auto Fragment = Label->getFragment();

while (&*CurrentFragment != Fragment) {
switch (CurrentFragment->getKind()) {
case MCFragment::FT_Dwarf:
Expand All @@ -2096,7 +2095,6 @@ void RewriteInstance::computeLineTableOffsets() {
llvm_unreachable(".debug_line section shouldn't contain other types "
"of fragments.");
}

++CurrentFragment;
CurrentOffset = 0;
}
Expand All @@ -2113,7 +2111,7 @@ void RewriteInstance::computeLineTableOffsets() {
<< "in .debug_info\n");
auto &SI = SectionMM->NoteSectionInfo[".debug_info"];
SI.PendingRelocs.emplace_back(
SectionInfo::Reloc{LTOI->second, 4, 0, Offset + DebugLineSize});
SectionInfo::Reloc{LTOI->second, 4, 0, Offset});
}
DEBUG(dbgs() << "BOLT-DEBUG: CU " << CUIDLineTablePair.first
<< " has line table at " << Offset << "\n");
Expand Down Expand Up @@ -2326,3 +2324,14 @@ void RewriteInstance::updateDebugLineInfoForNonSimpleFunctions() {
}
}
}

bool RewriteInstance::shouldOverwriteSection(StringRef SectionName) {
if (opts::UpdateDebugSections) {
for (auto &OverwriteName : DebugSectionsToOverwrite) {
if (SectionName == OverwriteName)
return true;
}
}

return false;
}
19 changes: 13 additions & 6 deletions bolt/RewriteInstance.h
Expand Up @@ -179,9 +179,6 @@ class RewriteInstance {

private:

/// Huge page size used for alignment.
static constexpr unsigned PageAlign = 0x200000;

/// Detect addresses and offsets available in the binary for allocating
/// new sections.
void discoverStorage();
Expand Down Expand Up @@ -247,7 +244,20 @@ class RewriteInstance {
return Address - NewTextSegmentAddress + NewTextSegmentOffset;
}

/// Return true if we should overwrite contents of the section instead
/// of appending contents to it.
bool shouldOverwriteSection(StringRef SectionName);

private:

/// If we are updating debug info, these are the section we need to overwrite.
static constexpr const char *DebugSectionsToOverwrite[] = {
".debug_aranges",
".debug_line"};

/// Huge page size used for alignment.
static constexpr unsigned PageAlign = 0x200000;

/// An instance of the input binary we are processing, externally owned.
llvm::object::ELFObjectFileBase *InputFile;

Expand Down Expand Up @@ -306,9 +316,6 @@ class RewriteInstance {
/// rewriting CFI info for these functions.
std::vector<uint64_t> FailedAddresses;

/// Size of the .debug_line section on input.
uint32_t DebugLineSize{0};

/// Size of the .debug_loc section in input.
uint32_t DebugLocSize{0};

Expand Down

0 comments on commit f047b9d

Please sign in to comment.