Skip to content

Commit

Permalink
[Dsymutil][Debuginfo][NFC] rust-lang#3 Refactor dsymutil to separate …
Browse files Browse the repository at this point in the history
…DWARF optimizing part.

Summary:
This is the next portion of patches for dsymutil.

Create DwarfEmitter interface to generate all debug info tables.
Put DwarfEmitter into DwarfLinker library and make tools/dsymutil/DwarfStreamer
to be child of DwarfEmitter.

It passes check-all testing. MD5 checksum for clang .dSYM bundle matches
for the dsymutil with/without that patch.

Reviewers: JDevlieghere, friss, dblaikie, aprantl

Reviewed By: JDevlieghere

Subscribers: merge_guards_bot, hiraditya, thegameg, probinson, llvm-commits

Tags: #llvm, #debug-info

Differential Revision: https://reviews.llvm.org/D72476
  • Loading branch information
avl-llvm committed Jan 13, 2020
1 parent d0aad9f commit f163755
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 54 deletions.
112 changes: 112 additions & 0 deletions llvm/include/llvm/DWARFLinker/DWARFLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#ifndef LLVM_DWARFLINKER_DWARFLINKER_H
#define LLVM_DWARFLINKER_DWARFLINKER_H

#include "llvm/CodeGen/AccelTable.h"
#include "llvm/CodeGen/NonRelocatableStringpool.h"
#include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/MC/MCDwarf.h"
#include <map>

namespace llvm {
Expand Down Expand Up @@ -81,6 +83,116 @@ class AddressesMap {
virtual void clear() = 0;
};

/// DwarfEmitter presents interface to generate all debug info tables.
class DwarfEmitter {
public:
virtual ~DwarfEmitter();

/// Emit DIE containing warnings.
virtual void emitPaperTrailWarningsDie(const Triple &Triple, DIE &Die) = 0;

/// Emit section named SecName with content equals to
/// corresponding section in Obj.
virtual void emitSectionContents(const object::ObjectFile &Obj,
StringRef SecName) = 0;

/// Emit the abbreviation table \p Abbrevs to the debug_abbrev section.
virtual void
emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
unsigned DwarfVersion) = 0;

/// Emit the string table described by \p Pool.
virtual void emitStrings(const NonRelocatableStringpool &Pool) = 0;

/// Emit DWARF debug names.
virtual void
emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) = 0;

/// Emit Apple namespaces accelerator table.
virtual void
emitAppleNamespaces(AccelTable<AppleAccelTableStaticOffsetData> &Table) = 0;

/// Emit Apple names accelerator table.
virtual void
emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> &Table) = 0;

/// Emit Apple Objective-C accelerator table.
virtual void
emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> &Table) = 0;

/// Emit Apple type accelerator table.
virtual void
emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> &Table) = 0;

/// Emit debug_ranges for \p FuncRange by translating the
/// original \p Entries.
virtual void emitRangesEntries(
int64_t UnitPcOffset, uint64_t OrigLowPc,
const FunctionIntervals::const_iterator &FuncRange,
const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
unsigned AddressSize) = 0;

/// Emit debug_aranges entries for \p Unit and if \p DoRangesSection is true,
/// also emit the debug_ranges entries for the DW_TAG_compile_unit's
/// DW_AT_ranges attribute.
virtual void emitUnitRangesEntries(CompileUnit &Unit,
bool DoRangesSection) = 0;

/// Copy the debug_line over to the updated binary while unobfuscating the
/// file names and directories.
virtual void translateLineTable(DataExtractor LineData, uint64_t Offset) = 0;

/// Emit the line table described in \p Rows into the debug_line section.
virtual void emitLineTableForUnit(MCDwarfLineTableParams Params,
StringRef PrologueBytes,
unsigned MinInstLength,
std::vector<DWARFDebugLine::Row> &Rows,
unsigned AdddressSize) = 0;

/// Emit the .debug_pubnames contribution for \p Unit.
virtual void emitPubNamesForUnit(const CompileUnit &Unit) = 0;

/// Emit the .debug_pubtypes contribution for \p Unit.
virtual void emitPubTypesForUnit(const CompileUnit &Unit) = 0;

/// Emit a CIE.
virtual void emitCIE(StringRef CIEBytes) = 0;

/// Emit an FDE with data \p Bytes.
virtual void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address,
StringRef Bytes) = 0;

/// Emit the debug_loc contribution for \p Unit by copying the entries from
/// \p Dwarf and offsetting them. Update the location attributes to point to
/// the new entries.
virtual void emitLocationsForUnit(
const CompileUnit &Unit, DWARFContext &Dwarf,
std::function<void(StringRef, SmallVectorImpl<uint8_t> &)>
ProcessExpr) = 0;

/// Emit the compilation unit header for \p Unit in the
/// debug_info section.
///
/// As a side effect, this also switches the current Dwarf version
/// of the MC layer to the one of U.getOrigUnit().
virtual void emitCompileUnitHeader(CompileUnit &Unit) = 0;

/// Recursively emit the DIE tree rooted at \p Die.
virtual void emitDIE(DIE &Die) = 0;

/// Returns size of generated .debug_line section.
virtual uint64_t getLineSectionSize() const = 0;

/// Returns size of generated .debug_frame section.
virtual uint64_t getFrameSectionSize() const = 0;

/// Returns size of generated .debug_ranges section.
virtual uint64_t getRangesSectionSize() const = 0;

/// Returns size of generated .debug_info section.
virtual uint64_t getDebugInfoSectionSize() const = 0;
};

} // end namespace llvm

#endif // LLVM_DWARFLINKER_DWARFLINKER_H
2 changes: 2 additions & 0 deletions llvm/lib/DWARFLinker/DWARFLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ namespace llvm {

AddressesMap::~AddressesMap() {}

DwarfEmitter::~DwarfEmitter() {}

} // namespace llvm
23 changes: 11 additions & 12 deletions llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2464,11 +2464,12 @@ void DwarfLinkerForBinary::DIECloner::cloneAllCompileUnits(
if (!Linker.Streamer)
return;

uint64_t OutputDebugInfoSize = Linker.Streamer->getDebugInfoSectionSize();
for (auto &CurrentUnit : CompileUnits) {
auto InputDIE = CurrentUnit->getOrigUnit().getUnitDIE();
CurrentUnit->setStartOffset(Linker.OutputDebugInfoSize);
CurrentUnit->setStartOffset(OutputDebugInfoSize);
if (!InputDIE) {
Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
continue;
}
if (CurrentUnit->getInfo(0).Keep) {
Expand All @@ -2480,7 +2481,7 @@ void DwarfLinkerForBinary::DIECloner::cloneAllCompileUnits(
CurrentUnit->getOutputUnitDIE());
}

Linker.OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();
OutputDebugInfoSize = CurrentUnit->computeNextUnitOffset();

if (Linker.Options.NoOutput)
continue;
Expand Down Expand Up @@ -2522,8 +2523,12 @@ void DwarfLinkerForBinary::DIECloner::cloneAllCompileUnits(
if (!CurrentUnit->getOutputUnitDIE())
continue;

assert(Linker.Streamer->getDebugInfoSectionSize() ==
CurrentUnit->getStartOffset());
Linker.Streamer->emitCompileUnitHeader(*CurrentUnit);
Linker.Streamer->emitDIE(*CurrentUnit->getOutputUnitDIE());
assert(Linker.Streamer->getDebugInfoSectionSize() ==
CurrentUnit->computeNextUnitOffset());
}
}

Expand Down Expand Up @@ -2593,13 +2598,7 @@ bool DwarfLinkerForBinary::emitPaperTrailWarnings(
Size += getULEB128Size(Abbrev.getNumber());
}
CUDie->setSize(Size);
auto &Asm = Streamer->getAsmPrinter();
Asm.emitInt32(11 + CUDie->getSize() - 4);
Asm.emitInt16(2);
Asm.emitInt32(0);
Asm.emitInt8(Map.getTriple().isArch64Bit() ? 8 : 4);
Streamer->emitDIE(*CUDie);
OutputDebugInfoSize += 11 /* Header */ + Size;
Streamer->emitPaperTrailWarningsDie(Map.getTriple(), *CUDie);

return true;
}
Expand Down Expand Up @@ -2680,7 +2679,6 @@ bool DwarfLinkerForBinary::link(const DebugMap &Map) {
return false;

// Size of the DIEs (and headers) generated for the linked output.
OutputDebugInfoSize = 0;
// A unique ID that identifies each compile unit.
unsigned UnitID = 0;
DebugMap ModuleMap(Map.getTriple(), Map.getBinaryPath());
Expand Down Expand Up @@ -2819,7 +2817,8 @@ bool DwarfLinkerForBinary::link(const DebugMap &Map) {
// is already emitted, without being affected by canonical die offsets set
// later. This prevents undeterminism when analyze and clone execute
// concurrently, as clone set the canonical DIE offset and analyze reads it.
const uint64_t ModulesEndOffset = OutputDebugInfoSize;
const uint64_t ModulesEndOffset =
Options.NoOutput ? 0 : Streamer->getDebugInfoSectionSize();

// These variables manage the list of processed object files.
// The mutex and condition variable are to ensure that this is thread safe.
Expand Down
1 change: 0 additions & 1 deletion llvm/tools/dsymutil/DwarfLinkerForBinary.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ class DwarfLinkerForBinary {
BinaryHolder &BinHolder;
LinkOptions Options;
std::unique_ptr<DwarfStreamer> Streamer;
uint64_t OutputDebugInfoSize;

unsigned MaxDwarfVersion = 0;
unsigned MinDwarfVersion = std::numeric_limits<unsigned>::max();
Expand Down
61 changes: 46 additions & 15 deletions llvm/tools/dsymutil/DwarfStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ bool DwarfStreamer::init(Triple TheTriple) {
LocSectionSize = 0;
LineSectionSize = 0;
FrameSectionSize = 0;
DebugInfoSectionSize = 0;

return true;
}
Expand Down Expand Up @@ -169,6 +170,7 @@ void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
// start of the section.
Asm->emitInt32(0);
Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
DebugInfoSectionSize += 11;

// Remember this CU.
EmittedUnits.push_back({Unit.getUniqueID(), Unit.getLabelBegin()});
Expand All @@ -188,6 +190,45 @@ void DwarfStreamer::emitAbbrevs(
void DwarfStreamer::emitDIE(DIE &Die) {
MS->SwitchSection(MOFI->getDwarfInfoSection());
Asm->emitDwarfDIE(Die);
DebugInfoSectionSize += Die.getSize();
}

/// Emit contents of section SecName From Obj.
void DwarfStreamer::emitSectionContents(const object::ObjectFile &Obj,
StringRef SecName) {
MCSection *Section =
StringSwitch<MCSection *>(SecName)
.Case("debug_line", MC->getObjectFileInfo()->getDwarfLineSection())
.Case("debug_loc", MC->getObjectFileInfo()->getDwarfLocSection())
.Case("debug_ranges",
MC->getObjectFileInfo()->getDwarfRangesSection())
.Case("debug_frame", MC->getObjectFileInfo()->getDwarfFrameSection())
.Case("debug_aranges",
MC->getObjectFileInfo()->getDwarfARangesSection())
.Default(nullptr);

if (Section) {
MS->SwitchSection(Section);

if (auto Sec = getSectionByName(Obj, SecName)) {
if (Expected<StringRef> E = Sec->getContents())
MS->EmitBytes(*E);
else
consumeError(E.takeError());
}
}
}

/// Emit DIE containing warnings.
void DwarfStreamer::emitPaperTrailWarningsDie(const Triple &Triple, DIE &Die) {
switchToDebugInfoSection(/* Version */ 2);
auto &Asm = getAsmPrinter();
Asm.emitInt32(11 + Die.getSize() - 4);
Asm.emitInt16(2);
Asm.emitInt32(0);
Asm.emitInt8(Triple.isArch64Bit() ? 8 : 4);
DebugInfoSectionSize += 11;
emitDIE(Die);
}

/// Emit the debug_str section stored in \p Pool.
Expand Down Expand Up @@ -680,33 +721,23 @@ void DwarfStreamer::translateLineTable(DataExtractor Data, uint64_t Offset) {
Offset = UnitEnd;
}

static void emitSectionContents(const object::ObjectFile &Obj,
StringRef SecName, MCStreamer *MS) {
if (auto Sec = getSectionByName(Obj, SecName)) {
if (Expected<StringRef> E = Sec->getContents())
MS->EmitBytes(*E);
else
consumeError(E.takeError());
}
}

void DwarfStreamer::copyInvariantDebugSection(const object::ObjectFile &Obj) {
if (!Options.Translator) {
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
emitSectionContents(Obj, "debug_line", MS);
emitSectionContents(Obj, "debug_line");
}

MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
emitSectionContents(Obj, "debug_loc", MS);
emitSectionContents(Obj, "debug_loc");

MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
emitSectionContents(Obj, "debug_ranges", MS);
emitSectionContents(Obj, "debug_ranges");

MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
emitSectionContents(Obj, "debug_frame", MS);
emitSectionContents(Obj, "debug_frame");

MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
emitSectionContents(Obj, "debug_aranges", MS);
emitSectionContents(Obj, "debug_aranges");
}

/// Emit the pubnames or pubtypes section contribution for \p
Expand Down
Loading

0 comments on commit f163755

Please sign in to comment.