Skip to content

Commit

Permalink
[DWARFLinker][NFC] Decrease DWARFLinker dependence on DwarfStreamer. (l…
Browse files Browse the repository at this point in the history
…lvm#77932)

This patch is extracted from llvm#74725.

The DwarfStreamer interface looks overcomplicated and has unnecessary
dependencies. This patch avoids creation of DwarfStreamer by DWARFLinker and
simplifies interface.
  • Loading branch information
avl-llvm authored Jan 19, 2024
1 parent 6f37114 commit 9ff4be6
Show file tree
Hide file tree
Showing 22 changed files with 421 additions and 425 deletions.
24 changes: 7 additions & 17 deletions llvm/include/llvm/DWARFLinker/Classic/DWARFLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class DwarfEmitter {
virtual ~DwarfEmitter() = default;

/// Emit section named SecName with data SecData.
virtual void emitSectionContents(StringRef SecData, StringRef SecName) = 0;
virtual void emitSectionContents(StringRef SecData,
DebugSectionKind SecKind) = 0;

/// Emit the abbreviation table \p Abbrevs to the .debug_abbrev section.
virtual void
Expand Down Expand Up @@ -188,17 +189,6 @@ class DwarfEmitter {

/// Dump the file to the disk.
virtual void finish() = 0;

/// Emit the swift_ast section stored in \p Buffer.
virtual void emitSwiftAST(StringRef Buffer) = 0;

/// Emit the swift reflection section stored in \p Buffer.
virtual void emitSwiftReflectionSection(
llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
StringRef Buffer, uint32_t Alignment, uint32_t Size) = 0;

/// Returns underlying AsmPrinter.
virtual AsmPrinter &getAsmPrinter() const = 0;
};

class DwarfStreamer;
Expand Down Expand Up @@ -232,10 +222,10 @@ class DWARFLinker : public DWARFLinkerBase {
StringsTranslator);
}

Error createEmitter(const Triple &TheTriple, OutputFileType FileType,
raw_pwrite_stream &OutFile);

DwarfEmitter *getEmitter();
/// Set output DWARF emitter.
void setOutputDWARFEmitter(DwarfEmitter *Emitter) {
TheDwarfEmitter = Emitter;
}

/// Add object file to be linked. Pre-load compile unit die. Call
/// \p OnCUDieLoaded for each compile unit die. If specified \p File
Expand Down Expand Up @@ -762,7 +752,7 @@ class DWARFLinker : public DWARFLinkerBase {
BumpPtrAllocator DIEAlloc;
/// @}

std::unique_ptr<DwarfStreamer> TheDwarfEmitter;
DwarfEmitter *TheDwarfEmitter = nullptr;
std::vector<LinkContext> ObjectContexts;

/// The CIEs that have been emitted in the output section. The actual CIE
Expand Down
18 changes: 13 additions & 5 deletions llvm/include/llvm/DWARFLinker/Classic/DWARFStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,23 @@ class DwarfStreamer : public DwarfEmitter {
public:
DwarfStreamer(DWARFLinkerBase::OutputFileType OutFileType,
raw_pwrite_stream &OutFile,
std::function<StringRef(StringRef Input)> Translator,
DWARFLinkerBase::TranslatorFuncTy Translator,
DWARFLinkerBase::MessageHandlerTy Warning)
: OutFile(OutFile), OutFileType(OutFileType), Translator(Translator),
WarningHandler(Warning) {}
virtual ~DwarfStreamer() = default;

static Expected<std::unique_ptr<DwarfStreamer>> createStreamer(
const Triple &TheTriple, DWARFLinkerBase::OutputFileType FileType,
raw_pwrite_stream &OutFile, DWARFLinkerBase::TranslatorFuncTy Translator,
DWARFLinkerBase::MessageHandlerTy Warning);

Error init(Triple TheTriple, StringRef Swift5ReflectionSegmentName);

/// Dump the file to the disk.
void finish() override;

AsmPrinter &getAsmPrinter() const override { return *Asm; }
AsmPrinter &getAsmPrinter() const { return *Asm; }

/// Set the current output section to debug_info and change
/// the MC Dwarf version to \p DwarfVersion.
Expand All @@ -77,7 +82,8 @@ class DwarfStreamer : public DwarfEmitter {
unsigned DwarfVersion) override;

/// Emit contents of section SecName From Obj.
void emitSectionContents(StringRef SecData, StringRef SecName) override;
void emitSectionContents(StringRef SecData,
DebugSectionKind SecKind) override;

/// Emit the string table described by \p Pool into .debug_str table.
void emitStrings(const NonRelocatableStringpool &Pool) override;
Expand All @@ -91,12 +97,12 @@ class DwarfStreamer : public DwarfEmitter {
void emitLineStrings(const NonRelocatableStringpool &Pool) override;

/// Emit the swift_ast section stored in \p Buffer.
void emitSwiftAST(StringRef Buffer) override;
void emitSwiftAST(StringRef Buffer);

/// Emit the swift reflection section stored in \p Buffer.
void emitSwiftReflectionSection(
llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
StringRef Buffer, uint32_t Alignment, uint32_t Size) override;
StringRef Buffer, uint32_t Alignment, uint32_t Size);

/// Emit debug ranges(.debug_ranges, .debug_rnglists) header.
MCSymbol *emitDwarfDebugRangeListHeader(const CompileUnit &Unit) override;
Expand Down Expand Up @@ -215,6 +221,8 @@ class DwarfStreamer : public DwarfEmitter {
WarningHandler(Warning, Context, nullptr);
}

MCSection *getMCSection(DebugSectionKind SecKind);

void emitMacroTableImpl(const DWARFDebugMacro *MacroTable,
const Offset2UnitMap &UnitMacroMap,
OffsetsStringPool &StringPool, uint64_t &OutOffset);
Expand Down
47 changes: 47 additions & 0 deletions llvm/include/llvm/DWARFLinker/DWARFLinkerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,53 @@ class DWARFUnit;

namespace dwarf_linker {

/// List of tracked debug tables.
enum class DebugSectionKind : uint8_t {
DebugInfo = 0,
DebugLine,
DebugFrame,
DebugRange,
DebugRngLists,
DebugLoc,
DebugLocLists,
DebugARanges,
DebugAbbrev,
DebugMacinfo,
DebugMacro,
DebugAddr,
DebugStr,
DebugLineStr,
DebugStrOffsets,
DebugPubNames,
DebugPubTypes,
DebugNames,
AppleNames,
AppleNamespaces,
AppleObjC,
AppleTypes,
NumberOfEnumEntries // must be last
};

static constexpr size_t SectionKindsNum =
static_cast<size_t>(DebugSectionKind::NumberOfEnumEntries);

static constexpr StringLiteral SectionNames[SectionKindsNum] = {
"debug_info", "debug_line", "debug_frame", "debug_ranges",
"debug_rnglists", "debug_loc", "debug_loclists", "debug_aranges",
"debug_abbrev", "debug_macinfo", "debug_macro", "debug_addr",
"debug_str", "debug_line_str", "debug_str_offsets", "debug_pubnames",
"debug_pubtypes", "debug_names", "apple_names", "apple_namespac",
"apple_objc", "apple_types"};

/// Return the name of the section.
static constexpr const StringLiteral &
getSectionName(DebugSectionKind SectionKind) {
return SectionNames[static_cast<uint8_t>(SectionKind)];
}

/// Recognise the table name and match it with the DebugSectionKind.
std::optional<DebugSectionKind> parseDebugTableName(StringRef Name);

/// The base interface for DWARFLinker implementations.
class DWARFLinkerBase {
public:
Expand Down
59 changes: 31 additions & 28 deletions llvm/include/llvm/DWARFLinker/Parallel/DWARFLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,30 +89,34 @@ namespace llvm {
namespace dwarf_linker {
namespace parallel {

/// ExtraDwarfEmitter allows adding extra data to the DWARFLinker output.
/// The finish() method should be called after all extra data are emitted.
class ExtraDwarfEmitter {
public:
virtual ~ExtraDwarfEmitter() = default;

/// Dump the file to the disk.
virtual void finish() = 0;

/// Emit section named SecName with data SecData.
virtual void emitSectionContents(StringRef SecData, StringRef SecName) = 0;

/// Emit the swift_ast section stored in \p Buffer.
virtual void emitSwiftAST(StringRef Buffer) = 0;

/// Emit the swift reflection section stored in \p Buffer.
virtual void emitSwiftReflectionSection(
llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
StringRef Buffer, uint32_t Alignment, uint32_t Size) = 0;

/// Returns underlying AsmPrinter.
virtual AsmPrinter &getAsmPrinter() const = 0;
/// This structure keeps data of the concrete section.
struct SectionDescriptorBase {
SectionDescriptorBase(DebugSectionKind SectionKind, dwarf::FormParams Format,
llvm::endianness Endianess)
: SectionKind(SectionKind), Format(Format), Endianess(Endianess) {}
virtual ~SectionDescriptorBase() = default;
/// Returns section content.
virtual StringRef getContents() = 0;
/// Returns section kind.
DebugSectionKind getKind() { return SectionKind; }
/// Returns section name.
const StringLiteral &getName() const { return getSectionName(SectionKind); }
/// Returns endianess used by section.
llvm::endianness getEndianess() const { return Endianess; }
/// Returns FormParams used by section.
dwarf::FormParams getFormParams() const { return Format; }

protected:
/// The section kind.
DebugSectionKind SectionKind = DebugSectionKind::NumberOfEnumEntries;
/// Output format.
dwarf::FormParams Format = {4, 4, dwarf::DWARF32};
llvm::endianness Endianess = llvm::endianness::little;
};

using SectionHandlerTy =
std::function<void(std::shared_ptr<SectionDescriptorBase> Section)>;

class DWARFLinker : public DWARFLinkerBase {
public:
virtual ~DWARFLinker() = default;
Expand All @@ -122,12 +126,11 @@ class DWARFLinker : public DWARFLinkerBase {
createLinker(MessageHandlerTy ErrorHandler, MessageHandlerTy WarningHandler,
TranslatorFuncTy StringsTranslator = nullptr);

/// Creates emitter for output dwarf.
virtual Error createEmitter(const Triple &TheTriple, OutputFileType FileType,
raw_pwrite_stream &OutFile) = 0;

/// Returns previously created dwarf emitter. May be nullptr.
virtual ExtraDwarfEmitter *getEmitter() = 0;
/// Set output DWARF handler. Result of linking DWARF is set of sections
/// containing final debug info. DWARFLinkerBase::link() pass generated
/// sections using specified \p SectionHandler.
virtual void setOutputDWARFHandler(const Triple &TargetTriple,
SectionHandlerTy SectionHandler) = 0;
};

} // end of namespace parallel
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/DWARFLinker/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_llvm_component_library(LLVMDWARFLinker
DWARFLinkerBase.cpp
Utils.cpp

ADDITIONAL_HEADER_DIRS
Expand Down
33 changes: 12 additions & 21 deletions llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2644,19 +2644,22 @@ uint64_t DWARFLinker::DIECloner::cloneAllCompileUnits(

void DWARFLinker::copyInvariantDebugSection(DWARFContext &Dwarf) {
TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getLocSection().Data,
"debug_loc");
DebugSectionKind::DebugLoc);
TheDwarfEmitter->emitSectionContents(
Dwarf.getDWARFObj().getRangesSection().Data, "debug_ranges");
Dwarf.getDWARFObj().getRangesSection().Data,
DebugSectionKind::DebugRange);
TheDwarfEmitter->emitSectionContents(
Dwarf.getDWARFObj().getFrameSection().Data, "debug_frame");
Dwarf.getDWARFObj().getFrameSection().Data, DebugSectionKind::DebugFrame);
TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getArangesSection(),
"debug_aranges");
DebugSectionKind::DebugARanges);
TheDwarfEmitter->emitSectionContents(
Dwarf.getDWARFObj().getAddrSection().Data, "debug_addr");
Dwarf.getDWARFObj().getAddrSection().Data, DebugSectionKind::DebugAddr);
TheDwarfEmitter->emitSectionContents(
Dwarf.getDWARFObj().getRnglistsSection().Data, "debug_rnglists");
Dwarf.getDWARFObj().getRnglistsSection().Data,
DebugSectionKind::DebugRngLists);
TheDwarfEmitter->emitSectionContents(
Dwarf.getDWARFObj().getLoclistsSection().Data, "debug_loclists");
Dwarf.getDWARFObj().getLoclistsSection().Data,
DebugSectionKind::DebugLocLists);
}

void DWARFLinker::addObjectFile(DWARFFile &File, ObjFileLoaderTy Loader,
Expand Down Expand Up @@ -2848,7 +2851,7 @@ Error DWARFLinker::link() {
SizeByObject[OptContext.File.FileName].Input =
getDebugInfoSize(*OptContext.File.Dwarf);
SizeByObject[OptContext.File.FileName].Output =
DIECloner(*this, TheDwarfEmitter.get(), OptContext.File, DIEAlloc,
DIECloner(*this, TheDwarfEmitter, OptContext.File, DIEAlloc,
OptContext.CompileUnits, Options.Update, DebugStrPool,
DebugLineStrPool, StringOffsetPool)
.cloneAllCompileUnits(*OptContext.File.Dwarf, OptContext.File,
Expand Down Expand Up @@ -3011,7 +3014,7 @@ Error DWARFLinker::cloneModuleUnit(LinkContext &Context, RefModuleUnit &Unit,
UnitListTy CompileUnits;
CompileUnits.emplace_back(std::move(Unit.Unit));
assert(TheDwarfEmitter);
DIECloner(*this, TheDwarfEmitter.get(), Unit.File, DIEAlloc, CompileUnits,
DIECloner(*this, TheDwarfEmitter, Unit.File, DIEAlloc, CompileUnits,
Options.Update, DebugStrPool, DebugLineStrPool, StringOffsetPool)
.cloneAllCompileUnits(*Unit.File.Dwarf, Unit.File,
Unit.File.Dwarf->isLittleEndian());
Expand All @@ -3030,16 +3033,4 @@ void DWARFLinker::verifyInput(const DWARFFile &File) {
}
}

Error DWARFLinker::createEmitter(const Triple &TheTriple,
OutputFileType FileType,
raw_pwrite_stream &OutFile) {

TheDwarfEmitter = std::make_unique<DwarfStreamer>(
FileType, OutFile, StringsTranslator, WarningHandler);

return TheDwarfEmitter->init(TheTriple, "__DWARF");
}

DwarfEmitter *DWARFLinker::getEmitter() { return TheDwarfEmitter.get(); }

} // namespace llvm
Loading

0 comments on commit 9ff4be6

Please sign in to comment.