LLVM 19.0.0git
DWARFEmitterImpl.cpp
Go to the documentation of this file.
1//===- DWARFEmitterImpl.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DWARFEmitterImpl.h"
19
20using namespace llvm;
21using namespace dwarf_linker;
22using namespace dwarf_linker::parallel;
23
25 StringRef Swift5ReflectionSegmentName) {
26 std::string ErrorStr;
27 std::string TripleName;
28
29 // Get the target.
30 const Target *TheTarget =
31 TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
32 if (!TheTarget)
33 return createStringError(std::errc::invalid_argument, ErrorStr.c_str());
34 TripleName = TheTriple.getTriple();
35
36 // Create all the MC Objects.
37 MRI.reset(TheTarget->createMCRegInfo(TripleName));
38 if (!MRI)
39 return createStringError(std::errc::invalid_argument,
40 "no register info for target %s",
41 TripleName.c_str());
42
44 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
45 if (!MAI)
46 return createStringError(std::errc::invalid_argument,
47 "no asm info for target %s", TripleName.c_str());
48
49 MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
50 if (!MSTI)
51 return createStringError(std::errc::invalid_argument,
52 "no subtarget info for target %s",
53 TripleName.c_str());
54
55 MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get(), nullptr,
56 nullptr, true, Swift5ReflectionSegmentName));
57 MOFI.reset(TheTarget->createMCObjectFileInfo(*MC, /*PIC=*/false, false));
58 MC->setObjectFileInfo(MOFI.get());
59
60 MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
61 if (!MAB)
62 return createStringError(std::errc::invalid_argument,
63 "no asm backend for target %s",
64 TripleName.c_str());
65
66 MII.reset(TheTarget->createMCInstrInfo());
67 if (!MII)
68 return createStringError(std::errc::invalid_argument,
69 "no instr info info for target %s",
70 TripleName.c_str());
71
72 MCE = TheTarget->createMCCodeEmitter(*MII, *MC);
73 if (!MCE)
74 return createStringError(std::errc::invalid_argument,
75 "no code emitter for target %s",
76 TripleName.c_str());
77
78 switch (OutFileType) {
80 MIP = TheTarget->createMCInstPrinter(TheTriple, MAI->getAssemblerDialect(),
81 *MAI, *MII, *MRI);
82 MS = TheTarget->createAsmStreamer(
83 *MC, std::make_unique<formatted_raw_ostream>(OutFile), true, true, MIP,
84 std::unique_ptr<MCCodeEmitter>(MCE), std::unique_ptr<MCAsmBackend>(MAB),
85 true);
86 break;
87 }
89 MS = TheTarget->createMCObjectStreamer(
90 TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB),
91 MAB->createObjectWriter(OutFile), std::unique_ptr<MCCodeEmitter>(MCE),
92 *MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
93 /*DWARFMustBeAtTheEnd*/ false);
94 break;
95 }
96 }
97
98 if (!MS)
99 return createStringError(std::errc::invalid_argument,
100 "no object streamer for target %s",
101 TripleName.c_str());
102
103 // Finally create the AsmPrinter we'll use to emit the DIEs.
104 TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
105 std::nullopt));
106 if (!TM)
107 return createStringError(std::errc::invalid_argument,
108 "no target machine for target %s",
109 TripleName.c_str());
110
111 Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
112 if (!Asm)
113 return createStringError(std::errc::invalid_argument,
114 "no asm printer for target %s",
115 TripleName.c_str());
116 Asm->setDwarfUsesRelocationsAcrossSections(false);
117
118 DebugInfoSectionSize = 0;
119
120 return Error::success();
121}
122
124 const SmallVector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
125 unsigned DwarfVersion) {
126 MS->switchSection(MOFI->getDwarfAbbrevSection());
127 MC->setDwarfVersion(DwarfVersion);
128 Asm->emitDwarfAbbrevs(Abbrevs);
129}
130
132 MS->switchSection(MOFI->getDwarfInfoSection());
133 MC->setDwarfVersion(Unit.getVersion());
134
135 // Emit size of content not including length itself. The size has already
136 // been computed in CompileUnit::computeOffsets(). Subtract 4 to that size to
137 // account for the length field.
138 Asm->emitInt32(Unit.getUnitSize() - 4);
139 Asm->emitInt16(Unit.getVersion());
140
141 if (Unit.getVersion() >= 5) {
142 Asm->emitInt8(dwarf::DW_UT_compile);
143 Asm->emitInt8(Unit.getFormParams().AddrSize);
144 // Proper offset to the abbreviations table will be set later.
145 Asm->emitInt32(0);
146 DebugInfoSectionSize += 12;
147 } else {
148 // Proper offset to the abbreviations table will be set later.
149 Asm->emitInt32(0);
150 Asm->emitInt8(Unit.getFormParams().AddrSize);
151 DebugInfoSectionSize += 11;
152 }
153}
154
156 MS->switchSection(MOFI->getDwarfInfoSection());
157 Asm->emitDwarfDIE(Die);
158 DebugInfoSectionSize += Die.getSize();
159}
160
162 DebugNamesUnitsOffsets &CUOffsets,
163 CompUnitIDToIdx &CUidToIdx) {
164 if (CUOffsets.empty())
165 return;
166
167 Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection());
169 DIEInteger::BestForm(/*IsSigned*/ false, (uint64_t)CUidToIdx.size() - 1);
170 // FIXME: add support for type units + .debug_names. For now the behavior is
171 // unsuported.
173 Asm.get(), Table, CUOffsets,
174 [&](const DWARF5AccelTableData &Entry)
175 -> std::optional<DWARF5AccelTable::UnitIndexAndEncoding> {
176 if (CUidToIdx.size() > 1)
177 return {{CUidToIdx[Entry.getUnitID()],
178 {dwarf::DW_IDX_compile_unit, Form}}};
179 return std::nullopt;
180 });
181}
182
185 Asm->OutStreamer->switchSection(MOFI->getDwarfAccelNamespaceSection());
186 auto *SectionBegin = Asm->createTempSymbol("namespac_begin");
187 Asm->OutStreamer->emitLabel(SectionBegin);
188 emitAppleAccelTable(Asm.get(), Table, "namespac", SectionBegin);
189}
190
193 Asm->OutStreamer->switchSection(MOFI->getDwarfAccelNamesSection());
194 auto *SectionBegin = Asm->createTempSymbol("names_begin");
195 Asm->OutStreamer->emitLabel(SectionBegin);
196 emitAppleAccelTable(Asm.get(), Table, "names", SectionBegin);
197}
198
201 Asm->OutStreamer->switchSection(MOFI->getDwarfAccelObjCSection());
202 auto *SectionBegin = Asm->createTempSymbol("objc_begin");
203 Asm->OutStreamer->emitLabel(SectionBegin);
204 emitAppleAccelTable(Asm.get(), Table, "objc", SectionBegin);
205}
206
209 Asm->OutStreamer->switchSection(MOFI->getDwarfAccelTypesSection());
210 auto *SectionBegin = Asm->createTempSymbol("types_begin");
211 Asm->OutStreamer->emitLabel(SectionBegin);
212 emitAppleAccelTable(Asm.get(), Table, "types", SectionBegin);
213}
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition: AccelTable.h:202
static dwarf::Form BestForm(bool IsSigned, uint64_t Int)
Choose the best form for integer.
Definition: DIE.h:175
A structured debug information entry.
Definition: DIE.h:819
unsigned getSize() const
Definition: DIE.h:862
The Data class implementation for DWARF v5 accelerator table.
Definition: AccelTable.h:276
unsigned size() const
Definition: DenseMap.h:99
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file.
Context object for machine code objects.
Definition: MCContext.h:81
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
MCCodeEmitter * createMCCodeEmitter(const MCInstrInfo &II, MCContext &Ctx) const
createMCCodeEmitter - Create a target specific code emitter.
MCObjectFileInfo * createMCObjectFileInfo(MCContext &Ctx, bool PIC, bool LargeCodeModel=false) const
Create a MCObjectFileInfo implementation for the specified target triple.
MCSubtargetInfo * createMCSubtargetInfo(StringRef TheTriple, StringRef CPU, StringRef Features) const
createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
MCStreamer * createAsmStreamer(MCContext &Ctx, std::unique_ptr< formatted_raw_ostream > OS, bool IsVerboseAsm, bool UseDwarfDirectory, MCInstPrinter *InstPrint, std::unique_ptr< MCCodeEmitter > &&CE, std::unique_ptr< MCAsmBackend > &&TAB, bool ShowInst) const
MCAsmBackend * createMCAsmBackend(const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options) const
createMCAsmBackend - Create a target specific assembly parser.
MCRegisterInfo * createMCRegInfo(StringRef TT) const
createMCRegInfo - Create a MCRegisterInfo implementation.
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM=std::nullopt, CodeGenOptLevel OL=CodeGenOptLevel::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple.
MCStreamer * createMCObjectStreamer(const Triple &T, MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&Emitter, const MCSubtargetInfo &STI, bool, bool IncrementalLinkerCompatible, bool DWARFMustBeAtTheEnd) const
Create a target specific MCStreamer.
MCAsmInfo * createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple, const MCTargetOptions &Options) const
createMCAsmInfo - Create a MCAsmInfo implementation for the specified target triple.
MCInstPrinter * createMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI) const
AsmPrinter * createAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > &&Streamer) const
createAsmPrinter - Create a target specific assembly printer pass.
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
const std::string & getTriple() const
Definition: Triple.h:437
void emitCompileUnitHeader(DwarfUnit &Unit)
Emit compile unit header.
Error init(Triple TheTriple, StringRef Swift5ReflectionSegmentName)
Initialize AsmPrinter data.
void emitAppleNames(AccelTable< AppleAccelTableStaticOffsetData > &Table)
Emits .apple_names section according to the specified Table.
void emitAbbrevs(const SmallVector< std::unique_ptr< DIEAbbrev > > &Abbrevs, unsigned DwarfVersion)
Emit abbreviations.
void emitAppleTypes(AccelTable< AppleAccelTableStaticTypeData > &Table)
Emits .apple_types section according to the specified Table.
void emitDIE(DIE &Die)
Emit DIE recursively.
void emitAppleNamespaces(AccelTable< AppleAccelTableStaticOffsetData > &Table)
Emits .apple_namespaces section according to the specified Table.
void emitDebugNames(DWARF5AccelTable &Table, DebugNamesUnitsOffsets &CUOffsets, CompUnitIDToIdx &UnitIDToIdxMap)
Emits .debug_names section according to the specified Table.
void emitAppleObjc(AccelTable< AppleAccelTableStaticOffsetData > &Table)
Emits .apple_objc section according to the specified Table.
Base class for all Dwarf units(Compile unit/Type table unit).
std::vector< std::variant< MCSymbol *, uint64_t > > DebugNamesUnitsOffsets
MCTargetOptions InitMCTargetOptionsFromFlags()
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1258
void emitAppleAccelTable(AsmPrinter *Asm, AccelTable< DataT > &Contents, StringRef Prefix, const MCSymbol *SecBegin)
Emit an Apple Accelerator Table consisting of entries in the specified AccelTable.
Definition: AccelTable.h:433
void emitDWARF5AccelTable(AsmPrinter *Asm, DWARF5AccelTable &Contents, const DwarfDebug &DD, ArrayRef< std::unique_ptr< DwarfCompileUnit > > CUs)
Definition: AccelTable.cpp:645
static const Target * lookupTarget(StringRef Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.