Skip to content

Commit 9ff4be6

Browse files
authored
[DWARFLinker][NFC] Decrease DWARFLinker dependence on DwarfStreamer. (#77932)
This patch is extracted from #74725. The DwarfStreamer interface looks overcomplicated and has unnecessary dependencies. This patch avoids creation of DwarfStreamer by DWARFLinker and simplifies interface.
1 parent 6f37114 commit 9ff4be6

22 files changed

+421
-425
lines changed

llvm/include/llvm/DWARFLinker/Classic/DWARFLinker.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class DwarfEmitter {
4242
virtual ~DwarfEmitter() = default;
4343

4444
/// Emit section named SecName with data SecData.
45-
virtual void emitSectionContents(StringRef SecData, StringRef SecName) = 0;
45+
virtual void emitSectionContents(StringRef SecData,
46+
DebugSectionKind SecKind) = 0;
4647

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

189190
/// Dump the file to the disk.
190191
virtual void finish() = 0;
191-
192-
/// Emit the swift_ast section stored in \p Buffer.
193-
virtual void emitSwiftAST(StringRef Buffer) = 0;
194-
195-
/// Emit the swift reflection section stored in \p Buffer.
196-
virtual void emitSwiftReflectionSection(
197-
llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
198-
StringRef Buffer, uint32_t Alignment, uint32_t Size) = 0;
199-
200-
/// Returns underlying AsmPrinter.
201-
virtual AsmPrinter &getAsmPrinter() const = 0;
202192
};
203193

204194
class DwarfStreamer;
@@ -232,10 +222,10 @@ class DWARFLinker : public DWARFLinkerBase {
232222
StringsTranslator);
233223
}
234224

235-
Error createEmitter(const Triple &TheTriple, OutputFileType FileType,
236-
raw_pwrite_stream &OutFile);
237-
238-
DwarfEmitter *getEmitter();
225+
/// Set output DWARF emitter.
226+
void setOutputDWARFEmitter(DwarfEmitter *Emitter) {
227+
TheDwarfEmitter = Emitter;
228+
}
239229

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

765-
std::unique_ptr<DwarfStreamer> TheDwarfEmitter;
755+
DwarfEmitter *TheDwarfEmitter = nullptr;
766756
std::vector<LinkContext> ObjectContexts;
767757

768758
/// The CIEs that have been emitted in the output section. The actual CIE

llvm/include/llvm/DWARFLinker/Classic/DWARFStreamer.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,23 @@ class DwarfStreamer : public DwarfEmitter {
4545
public:
4646
DwarfStreamer(DWARFLinkerBase::OutputFileType OutFileType,
4747
raw_pwrite_stream &OutFile,
48-
std::function<StringRef(StringRef Input)> Translator,
48+
DWARFLinkerBase::TranslatorFuncTy Translator,
4949
DWARFLinkerBase::MessageHandlerTy Warning)
5050
: OutFile(OutFile), OutFileType(OutFileType), Translator(Translator),
5151
WarningHandler(Warning) {}
5252
virtual ~DwarfStreamer() = default;
5353

54+
static Expected<std::unique_ptr<DwarfStreamer>> createStreamer(
55+
const Triple &TheTriple, DWARFLinkerBase::OutputFileType FileType,
56+
raw_pwrite_stream &OutFile, DWARFLinkerBase::TranslatorFuncTy Translator,
57+
DWARFLinkerBase::MessageHandlerTy Warning);
58+
5459
Error init(Triple TheTriple, StringRef Swift5ReflectionSegmentName);
5560

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

59-
AsmPrinter &getAsmPrinter() const override { return *Asm; }
64+
AsmPrinter &getAsmPrinter() const { return *Asm; }
6065

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

7984
/// Emit contents of section SecName From Obj.
80-
void emitSectionContents(StringRef SecData, StringRef SecName) override;
85+
void emitSectionContents(StringRef SecData,
86+
DebugSectionKind SecKind) override;
8187

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

9399
/// Emit the swift_ast section stored in \p Buffer.
94-
void emitSwiftAST(StringRef Buffer) override;
100+
void emitSwiftAST(StringRef Buffer);
95101

96102
/// Emit the swift reflection section stored in \p Buffer.
97103
void emitSwiftReflectionSection(
98104
llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
99-
StringRef Buffer, uint32_t Alignment, uint32_t Size) override;
105+
StringRef Buffer, uint32_t Alignment, uint32_t Size);
100106

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

224+
MCSection *getMCSection(DebugSectionKind SecKind);
225+
218226
void emitMacroTableImpl(const DWARFDebugMacro *MacroTable,
219227
const Offset2UnitMap &UnitMacroMap,
220228
OffsetsStringPool &StringPool, uint64_t &OutOffset);

llvm/include/llvm/DWARFLinker/DWARFLinkerBase.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,53 @@ class DWARFUnit;
2323

2424
namespace dwarf_linker {
2525

26+
/// List of tracked debug tables.
27+
enum class DebugSectionKind : uint8_t {
28+
DebugInfo = 0,
29+
DebugLine,
30+
DebugFrame,
31+
DebugRange,
32+
DebugRngLists,
33+
DebugLoc,
34+
DebugLocLists,
35+
DebugARanges,
36+
DebugAbbrev,
37+
DebugMacinfo,
38+
DebugMacro,
39+
DebugAddr,
40+
DebugStr,
41+
DebugLineStr,
42+
DebugStrOffsets,
43+
DebugPubNames,
44+
DebugPubTypes,
45+
DebugNames,
46+
AppleNames,
47+
AppleNamespaces,
48+
AppleObjC,
49+
AppleTypes,
50+
NumberOfEnumEntries // must be last
51+
};
52+
53+
static constexpr size_t SectionKindsNum =
54+
static_cast<size_t>(DebugSectionKind::NumberOfEnumEntries);
55+
56+
static constexpr StringLiteral SectionNames[SectionKindsNum] = {
57+
"debug_info", "debug_line", "debug_frame", "debug_ranges",
58+
"debug_rnglists", "debug_loc", "debug_loclists", "debug_aranges",
59+
"debug_abbrev", "debug_macinfo", "debug_macro", "debug_addr",
60+
"debug_str", "debug_line_str", "debug_str_offsets", "debug_pubnames",
61+
"debug_pubtypes", "debug_names", "apple_names", "apple_namespac",
62+
"apple_objc", "apple_types"};
63+
64+
/// Return the name of the section.
65+
static constexpr const StringLiteral &
66+
getSectionName(DebugSectionKind SectionKind) {
67+
return SectionNames[static_cast<uint8_t>(SectionKind)];
68+
}
69+
70+
/// Recognise the table name and match it with the DebugSectionKind.
71+
std::optional<DebugSectionKind> parseDebugTableName(StringRef Name);
72+
2673
/// The base interface for DWARFLinker implementations.
2774
class DWARFLinkerBase {
2875
public:

llvm/include/llvm/DWARFLinker/Parallel/DWARFLinker.h

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,34 @@ namespace llvm {
8989
namespace dwarf_linker {
9090
namespace parallel {
9191

92-
/// ExtraDwarfEmitter allows adding extra data to the DWARFLinker output.
93-
/// The finish() method should be called after all extra data are emitted.
94-
class ExtraDwarfEmitter {
95-
public:
96-
virtual ~ExtraDwarfEmitter() = default;
97-
98-
/// Dump the file to the disk.
99-
virtual void finish() = 0;
100-
101-
/// Emit section named SecName with data SecData.
102-
virtual void emitSectionContents(StringRef SecData, StringRef SecName) = 0;
103-
104-
/// Emit the swift_ast section stored in \p Buffer.
105-
virtual void emitSwiftAST(StringRef Buffer) = 0;
106-
107-
/// Emit the swift reflection section stored in \p Buffer.
108-
virtual void emitSwiftReflectionSection(
109-
llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
110-
StringRef Buffer, uint32_t Alignment, uint32_t Size) = 0;
111-
112-
/// Returns underlying AsmPrinter.
113-
virtual AsmPrinter &getAsmPrinter() const = 0;
92+
/// This structure keeps data of the concrete section.
93+
struct SectionDescriptorBase {
94+
SectionDescriptorBase(DebugSectionKind SectionKind, dwarf::FormParams Format,
95+
llvm::endianness Endianess)
96+
: SectionKind(SectionKind), Format(Format), Endianess(Endianess) {}
97+
virtual ~SectionDescriptorBase() = default;
98+
/// Returns section content.
99+
virtual StringRef getContents() = 0;
100+
/// Returns section kind.
101+
DebugSectionKind getKind() { return SectionKind; }
102+
/// Returns section name.
103+
const StringLiteral &getName() const { return getSectionName(SectionKind); }
104+
/// Returns endianess used by section.
105+
llvm::endianness getEndianess() const { return Endianess; }
106+
/// Returns FormParams used by section.
107+
dwarf::FormParams getFormParams() const { return Format; }
108+
109+
protected:
110+
/// The section kind.
111+
DebugSectionKind SectionKind = DebugSectionKind::NumberOfEnumEntries;
112+
/// Output format.
113+
dwarf::FormParams Format = {4, 4, dwarf::DWARF32};
114+
llvm::endianness Endianess = llvm::endianness::little;
114115
};
115116

117+
using SectionHandlerTy =
118+
std::function<void(std::shared_ptr<SectionDescriptorBase> Section)>;
119+
116120
class DWARFLinker : public DWARFLinkerBase {
117121
public:
118122
virtual ~DWARFLinker() = default;
@@ -122,12 +126,11 @@ class DWARFLinker : public DWARFLinkerBase {
122126
createLinker(MessageHandlerTy ErrorHandler, MessageHandlerTy WarningHandler,
123127
TranslatorFuncTy StringsTranslator = nullptr);
124128

125-
/// Creates emitter for output dwarf.
126-
virtual Error createEmitter(const Triple &TheTriple, OutputFileType FileType,
127-
raw_pwrite_stream &OutFile) = 0;
128-
129-
/// Returns previously created dwarf emitter. May be nullptr.
130-
virtual ExtraDwarfEmitter *getEmitter() = 0;
129+
/// Set output DWARF handler. Result of linking DWARF is set of sections
130+
/// containing final debug info. DWARFLinkerBase::link() pass generated
131+
/// sections using specified \p SectionHandler.
132+
virtual void setOutputDWARFHandler(const Triple &TargetTriple,
133+
SectionHandlerTy SectionHandler) = 0;
131134
};
132135

133136
} // end of namespace parallel

llvm/lib/DWARFLinker/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
add_llvm_component_library(LLVMDWARFLinker
2+
DWARFLinkerBase.cpp
23
Utils.cpp
34

45
ADDITIONAL_HEADER_DIRS

llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,19 +2644,22 @@ uint64_t DWARFLinker::DIECloner::cloneAllCompileUnits(
26442644

26452645
void DWARFLinker::copyInvariantDebugSection(DWARFContext &Dwarf) {
26462646
TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getLocSection().Data,
2647-
"debug_loc");
2647+
DebugSectionKind::DebugLoc);
26482648
TheDwarfEmitter->emitSectionContents(
2649-
Dwarf.getDWARFObj().getRangesSection().Data, "debug_ranges");
2649+
Dwarf.getDWARFObj().getRangesSection().Data,
2650+
DebugSectionKind::DebugRange);
26502651
TheDwarfEmitter->emitSectionContents(
2651-
Dwarf.getDWARFObj().getFrameSection().Data, "debug_frame");
2652+
Dwarf.getDWARFObj().getFrameSection().Data, DebugSectionKind::DebugFrame);
26522653
TheDwarfEmitter->emitSectionContents(Dwarf.getDWARFObj().getArangesSection(),
2653-
"debug_aranges");
2654+
DebugSectionKind::DebugARanges);
26542655
TheDwarfEmitter->emitSectionContents(
2655-
Dwarf.getDWARFObj().getAddrSection().Data, "debug_addr");
2656+
Dwarf.getDWARFObj().getAddrSection().Data, DebugSectionKind::DebugAddr);
26562657
TheDwarfEmitter->emitSectionContents(
2657-
Dwarf.getDWARFObj().getRnglistsSection().Data, "debug_rnglists");
2658+
Dwarf.getDWARFObj().getRnglistsSection().Data,
2659+
DebugSectionKind::DebugRngLists);
26582660
TheDwarfEmitter->emitSectionContents(
2659-
Dwarf.getDWARFObj().getLoclistsSection().Data, "debug_loclists");
2661+
Dwarf.getDWARFObj().getLoclistsSection().Data,
2662+
DebugSectionKind::DebugLocLists);
26602663
}
26612664

26622665
void DWARFLinker::addObjectFile(DWARFFile &File, ObjFileLoaderTy Loader,
@@ -2848,7 +2851,7 @@ Error DWARFLinker::link() {
28482851
SizeByObject[OptContext.File.FileName].Input =
28492852
getDebugInfoSize(*OptContext.File.Dwarf);
28502853
SizeByObject[OptContext.File.FileName].Output =
2851-
DIECloner(*this, TheDwarfEmitter.get(), OptContext.File, DIEAlloc,
2854+
DIECloner(*this, TheDwarfEmitter, OptContext.File, DIEAlloc,
28522855
OptContext.CompileUnits, Options.Update, DebugStrPool,
28532856
DebugLineStrPool, StringOffsetPool)
28542857
.cloneAllCompileUnits(*OptContext.File.Dwarf, OptContext.File,
@@ -3011,7 +3014,7 @@ Error DWARFLinker::cloneModuleUnit(LinkContext &Context, RefModuleUnit &Unit,
30113014
UnitListTy CompileUnits;
30123015
CompileUnits.emplace_back(std::move(Unit.Unit));
30133016
assert(TheDwarfEmitter);
3014-
DIECloner(*this, TheDwarfEmitter.get(), Unit.File, DIEAlloc, CompileUnits,
3017+
DIECloner(*this, TheDwarfEmitter, Unit.File, DIEAlloc, CompileUnits,
30153018
Options.Update, DebugStrPool, DebugLineStrPool, StringOffsetPool)
30163019
.cloneAllCompileUnits(*Unit.File.Dwarf, Unit.File,
30173020
Unit.File.Dwarf->isLittleEndian());
@@ -3030,16 +3033,4 @@ void DWARFLinker::verifyInput(const DWARFFile &File) {
30303033
}
30313034
}
30323035

3033-
Error DWARFLinker::createEmitter(const Triple &TheTriple,
3034-
OutputFileType FileType,
3035-
raw_pwrite_stream &OutFile) {
3036-
3037-
TheDwarfEmitter = std::make_unique<DwarfStreamer>(
3038-
FileType, OutFile, StringsTranslator, WarningHandler);
3039-
3040-
return TheDwarfEmitter->init(TheTriple, "__DWARF");
3041-
}
3042-
3043-
DwarfEmitter *DWARFLinker::getEmitter() { return TheDwarfEmitter.get(); }
3044-
30453036
} // namespace llvm

0 commit comments

Comments
 (0)