1 //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the Dwarf emissions parts of AsmPrinter.
11 //===----------------------------------------------------------------------===//
13 #include "ByteStreamer.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/BinaryFormat/Dwarf.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/DIE.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSection.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MachineLocation.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Target/TargetLoweringObjectFile.h"
29 #include "llvm/Target/TargetMachine.h"
32 #define DEBUG_TYPE "asm-printer"
34 //===----------------------------------------------------------------------===//
35 // Dwarf Emission Helper Routines
36 //===----------------------------------------------------------------------===//
38 /// EmitSLEB128 - emit the specified signed leb128 value.
39 void AsmPrinter::emitSLEB128(int64_t Value, const char *Desc) const {
40 if (isVerbose() && Desc)
41 OutStreamer->AddComment(Desc);
43 OutStreamer->emitSLEB128IntValue(Value);
46 void AsmPrinter::emitULEB128(uint64_t Value, const char *Desc,
47 unsigned PadTo) const {
48 if (isVerbose() && Desc)
49 OutStreamer->AddComment(Desc);
51 OutStreamer->emitULEB128IntValue(Value, PadTo);
54 /// Emit something like ".uleb128 Hi-Lo".
55 void AsmPrinter::emitLabelDifferenceAsULEB128(const MCSymbol *Hi,
56 const MCSymbol *Lo) const {
57 OutStreamer->emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
60 static const char *DecodeDWARFEncoding(unsigned Encoding) {
62 case dwarf::DW_EH_PE_absptr:
64 case dwarf::DW_EH_PE_omit:
66 case dwarf::DW_EH_PE_pcrel:
68 case dwarf::DW_EH_PE_uleb128:
70 case dwarf::DW_EH_PE_sleb128:
72 case dwarf::DW_EH_PE_udata4:
74 case dwarf::DW_EH_PE_udata8:
76 case dwarf::DW_EH_PE_sdata4:
78 case dwarf::DW_EH_PE_sdata8:
80 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
81 return "pcrel udata4";
82 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
83 return "pcrel sdata4";
84 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
85 return "pcrel udata8";
86 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
87 return "pcrel sdata8";
88 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
90 return "indirect pcrel udata4";
91 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
93 return "indirect pcrel sdata4";
94 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
96 return "indirect pcrel udata8";
97 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
99 return "indirect pcrel sdata8";
102 return "<unknown encoding>";
105 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
106 /// encoding. If verbose assembly output is enabled, we output comments
107 /// describing the encoding. Desc is an optional string saying what the
108 /// encoding is specifying (e.g. "LSDA").
109 void AsmPrinter::emitEncodingByte(unsigned Val, const char *Desc) const {
112 OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
113 Twine(DecodeDWARFEncoding(Val)));
115 OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
118 OutStreamer->emitIntValue(Val, 1);
121 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
122 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
123 if (Encoding == dwarf::DW_EH_PE_omit)
126 switch (Encoding & 0x07) {
128 llvm_unreachable("Invalid encoded value.");
129 case dwarf::DW_EH_PE_absptr:
130 return MF->getDataLayout().getPointerSize();
131 case dwarf::DW_EH_PE_udata2:
133 case dwarf::DW_EH_PE_udata4:
135 case dwarf::DW_EH_PE_udata8:
140 void AsmPrinter::emitTTypeReference(const GlobalValue *GV,
141 unsigned Encoding) const {
143 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
146 TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
147 OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding));
149 OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding));
152 void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
153 bool ForceOffset) const {
155 // On COFF targets, we have to emit the special .secrel32 directive.
156 if (MAI->needsDwarfSectionOffsetDirective()) {
157 OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
161 // If the format uses relocations with dwarf, refer to the symbol directly.
162 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
163 OutStreamer->emitSymbolValue(Label, 4);
168 // Otherwise, emit it as a label difference from the start of the section.
169 emitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
172 void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntry S) const {
173 if (MAI->doesDwarfUseRelocationsAcrossSections()) {
174 assert(S.Symbol && "No symbol available");
175 emitDwarfSymbolReference(S.Symbol);
179 // Just emit the offset directly; no need for symbol math.
183 void AsmPrinter::emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {
184 // TODO: Support DWARF64
185 emitLabelPlusOffset(Label, Offset, 4);
188 void AsmPrinter::emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,
189 unsigned Encoding) const {
190 // The least significant 3 bits specify the width of the encoding
191 if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
192 emitLabelDifferenceAsULEB128(Hi, Lo);
194 emitLabelDifference(Hi, Lo, GetSizeOfEncodedValue(Encoding));
197 void AsmPrinter::emitCallSiteValue(uint64_t Value, unsigned Encoding) const {
198 // The least significant 3 bits specify the width of the encoding
199 if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
202 OutStreamer->emitIntValue(Value, GetSizeOfEncodedValue(Encoding));
205 //===----------------------------------------------------------------------===//
206 // Dwarf Lowering Routines
207 //===----------------------------------------------------------------------===//
209 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
210 switch (Inst.getOperation()) {
212 llvm_unreachable("Unexpected instruction");
213 case MCCFIInstruction::OpDefCfaOffset:
214 OutStreamer->emitCFIDefCfaOffset(Inst.getOffset());
216 case MCCFIInstruction::OpAdjustCfaOffset:
217 OutStreamer->emitCFIAdjustCfaOffset(Inst.getOffset());
219 case MCCFIInstruction::OpDefCfa:
220 OutStreamer->emitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
222 case MCCFIInstruction::OpDefCfaRegister:
223 OutStreamer->emitCFIDefCfaRegister(Inst.getRegister());
225 case MCCFIInstruction::OpOffset:
226 OutStreamer->emitCFIOffset(Inst.getRegister(), Inst.getOffset());
228 case MCCFIInstruction::OpRegister:
229 OutStreamer->emitCFIRegister(Inst.getRegister(), Inst.getRegister2());
231 case MCCFIInstruction::OpWindowSave:
232 OutStreamer->emitCFIWindowSave();
234 case MCCFIInstruction::OpNegateRAState:
235 OutStreamer->emitCFINegateRAState();
237 case MCCFIInstruction::OpSameValue:
238 OutStreamer->emitCFISameValue(Inst.getRegister());
240 case MCCFIInstruction::OpGnuArgsSize:
241 OutStreamer->emitCFIGnuArgsSize(Inst.getOffset());
243 case MCCFIInstruction::OpEscape:
244 OutStreamer->AddComment(Inst.getComment());
245 OutStreamer->emitCFIEscape(Inst.getValues());
247 case MCCFIInstruction::OpRestore:
248 OutStreamer->emitCFIRestore(Inst.getRegister());
250 case MCCFIInstruction::OpUndefined:
251 OutStreamer->emitCFIUndefined(Inst.getRegister());
256 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
257 // Emit the code (index) for the abbreviation.
259 OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
260 Twine::utohexstr(Die.getOffset()) + ":0x" +
261 Twine::utohexstr(Die.getSize()) + " " +
262 dwarf::TagString(Die.getTag()));
263 emitULEB128(Die.getAbbrevNumber());
265 // Emit the DIE attribute values.
266 for (const auto &V : Die.values()) {
267 dwarf::Attribute Attr = V.getAttribute();
268 assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
271 OutStreamer->AddComment(dwarf::AttributeString(Attr));
272 if (Attr == dwarf::DW_AT_accessibility)
273 OutStreamer->AddComment(
274 dwarf::AccessibilityString(V.getDIEInteger().getValue()));
277 // Emit an attribute using the defined form.
281 // Emit the DIE children if any.
282 if (Die.hasChildren()) {
283 for (auto &Child : Die.children())
286 OutStreamer->AddComment("End Of Children Mark");
291 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
292 // Emit the abbreviations code (base 1 index.)
293 emitULEB128(Abbrev.getNumber(), "Abbreviation Code");
295 // Emit the abbreviations data.