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/MCRegisterInfo.h"
22 #include "llvm/MC/MCSection.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCSymbol.h"
25 #include "llvm/MC/MachineLocation.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetMachine.h"
31 #define DEBUG_TYPE "asm-printer"
33 //===----------------------------------------------------------------------===//
34 // Dwarf Emission Helper Routines
35 //===----------------------------------------------------------------------===//
37 /// EmitSLEB128 - emit the specified signed leb128 value.
38 void AsmPrinter::EmitSLEB128(int64_t Value
, const char *Desc
) const {
39 if (isVerbose() && Desc
)
40 OutStreamer
->AddComment(Desc
);
42 OutStreamer
->EmitSLEB128IntValue(Value
);
45 void AsmPrinter::EmitULEB128(uint64_t Value
, const char *Desc
) const {
46 if (isVerbose() && Desc
)
47 OutStreamer
->AddComment(Desc
);
49 OutStreamer
->EmitULEB128IntValue(Value
);
52 /// Emit something like ".uleb128 Hi-Lo".
53 void AsmPrinter::EmitLabelDifferenceAsULEB128(const MCSymbol
*Hi
,
54 const MCSymbol
*Lo
) const {
55 OutStreamer
->emitAbsoluteSymbolDiffAsULEB128(Hi
, Lo
);
58 static const char *DecodeDWARFEncoding(unsigned Encoding
) {
60 case dwarf::DW_EH_PE_absptr
:
62 case dwarf::DW_EH_PE_omit
:
64 case dwarf::DW_EH_PE_pcrel
:
66 case dwarf::DW_EH_PE_uleb128
:
68 case dwarf::DW_EH_PE_sleb128
:
70 case dwarf::DW_EH_PE_udata4
:
72 case dwarf::DW_EH_PE_udata8
:
74 case dwarf::DW_EH_PE_sdata4
:
76 case dwarf::DW_EH_PE_sdata8
:
78 case dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_udata4
:
79 return "pcrel udata4";
80 case dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
:
81 return "pcrel sdata4";
82 case dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_udata8
:
83 return "pcrel udata8";
84 case dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata8
:
85 return "pcrel sdata8";
86 case dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_udata4
88 return "indirect pcrel udata4";
89 case dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata4
91 return "indirect pcrel sdata4";
92 case dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_udata8
94 return "indirect pcrel udata8";
95 case dwarf::DW_EH_PE_indirect
| dwarf::DW_EH_PE_pcrel
| dwarf::DW_EH_PE_sdata8
97 return "indirect pcrel sdata8";
100 return "<unknown encoding>";
103 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
104 /// encoding. If verbose assembly output is enabled, we output comments
105 /// describing the encoding. Desc is an optional string saying what the
106 /// encoding is specifying (e.g. "LSDA").
107 void AsmPrinter::EmitEncodingByte(unsigned Val
, const char *Desc
) const {
110 OutStreamer
->AddComment(Twine(Desc
) + " Encoding = " +
111 Twine(DecodeDWARFEncoding(Val
)));
113 OutStreamer
->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val
));
116 OutStreamer
->EmitIntValue(Val
, 1);
119 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
120 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding
) const {
121 if (Encoding
== dwarf::DW_EH_PE_omit
)
124 switch (Encoding
& 0x07) {
126 llvm_unreachable("Invalid encoded value.");
127 case dwarf::DW_EH_PE_absptr
:
128 return MF
->getDataLayout().getPointerSize();
129 case dwarf::DW_EH_PE_udata2
:
131 case dwarf::DW_EH_PE_udata4
:
133 case dwarf::DW_EH_PE_udata8
:
138 void AsmPrinter::EmitTTypeReference(const GlobalValue
*GV
,
139 unsigned Encoding
) const {
141 const TargetLoweringObjectFile
&TLOF
= getObjFileLowering();
144 TLOF
.getTTypeGlobalReference(GV
, Encoding
, TM
, MMI
, *OutStreamer
);
145 OutStreamer
->EmitValue(Exp
, GetSizeOfEncodedValue(Encoding
));
147 OutStreamer
->EmitIntValue(0, GetSizeOfEncodedValue(Encoding
));
150 void AsmPrinter::emitDwarfSymbolReference(const MCSymbol
*Label
,
151 bool ForceOffset
) const {
153 // On COFF targets, we have to emit the special .secrel32 directive.
154 if (MAI
->needsDwarfSectionOffsetDirective()) {
155 OutStreamer
->EmitCOFFSecRel32(Label
, /*Offset=*/0);
159 // If the format uses relocations with dwarf, refer to the symbol directly.
160 if (MAI
->doesDwarfUseRelocationsAcrossSections()) {
161 OutStreamer
->EmitSymbolValue(Label
, 4);
166 // Otherwise, emit it as a label difference from the start of the section.
167 EmitLabelDifference(Label
, Label
->getSection().getBeginSymbol(), 4);
170 void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntry S
) const {
171 if (MAI
->doesDwarfUseRelocationsAcrossSections()) {
172 assert(S
.Symbol
&& "No symbol available");
173 emitDwarfSymbolReference(S
.Symbol
);
177 // Just emit the offset directly; no need for symbol math.
181 void AsmPrinter::EmitDwarfOffset(const MCSymbol
*Label
, uint64_t Offset
) const {
182 EmitLabelPlusOffset(Label
, Offset
, MAI
->getCodePointerSize());
185 //===----------------------------------------------------------------------===//
186 // Dwarf Lowering Routines
187 //===----------------------------------------------------------------------===//
189 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction
&Inst
) const {
190 switch (Inst
.getOperation()) {
192 llvm_unreachable("Unexpected instruction");
193 case MCCFIInstruction::OpDefCfaOffset
:
194 OutStreamer
->EmitCFIDefCfaOffset(Inst
.getOffset());
196 case MCCFIInstruction::OpAdjustCfaOffset
:
197 OutStreamer
->EmitCFIAdjustCfaOffset(Inst
.getOffset());
199 case MCCFIInstruction::OpDefCfa
:
200 OutStreamer
->EmitCFIDefCfa(Inst
.getRegister(), Inst
.getOffset());
202 case MCCFIInstruction::OpDefCfaRegister
:
203 OutStreamer
->EmitCFIDefCfaRegister(Inst
.getRegister());
205 case MCCFIInstruction::OpOffset
:
206 OutStreamer
->EmitCFIOffset(Inst
.getRegister(), Inst
.getOffset());
208 case MCCFIInstruction::OpRegister
:
209 OutStreamer
->EmitCFIRegister(Inst
.getRegister(), Inst
.getRegister2());
211 case MCCFIInstruction::OpWindowSave
:
212 OutStreamer
->EmitCFIWindowSave();
214 case MCCFIInstruction::OpNegateRAState
:
215 OutStreamer
->EmitCFINegateRAState();
217 case MCCFIInstruction::OpSameValue
:
218 OutStreamer
->EmitCFISameValue(Inst
.getRegister());
220 case MCCFIInstruction::OpGnuArgsSize
:
221 OutStreamer
->EmitCFIGnuArgsSize(Inst
.getOffset());
223 case MCCFIInstruction::OpEscape
:
224 OutStreamer
->EmitCFIEscape(Inst
.getValues());
226 case MCCFIInstruction::OpRestore
:
227 OutStreamer
->EmitCFIRestore(Inst
.getRegister());
232 void AsmPrinter::emitDwarfDIE(const DIE
&Die
) const {
233 // Emit the code (index) for the abbreviation.
235 OutStreamer
->AddComment("Abbrev [" + Twine(Die
.getAbbrevNumber()) + "] 0x" +
236 Twine::utohexstr(Die
.getOffset()) + ":0x" +
237 Twine::utohexstr(Die
.getSize()) + " " +
238 dwarf::TagString(Die
.getTag()));
239 EmitULEB128(Die
.getAbbrevNumber());
241 // Emit the DIE attribute values.
242 for (const auto &V
: Die
.values()) {
243 dwarf::Attribute Attr
= V
.getAttribute();
244 assert(V
.getForm() && "Too many attributes for DIE (check abbreviation)");
247 OutStreamer
->AddComment(dwarf::AttributeString(Attr
));
248 if (Attr
== dwarf::DW_AT_accessibility
)
249 OutStreamer
->AddComment(
250 dwarf::AccessibilityString(V
.getDIEInteger().getValue()));
253 // Emit an attribute using the defined form.
257 // Emit the DIE children if any.
258 if (Die
.hasChildren()) {
259 for (auto &Child
: Die
.children())
262 OutStreamer
->AddComment("End Of Children Mark");
267 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev
&Abbrev
) const {
268 // Emit the abbreviations code (base 1 index.)
269 EmitULEB128(Abbrev
.getNumber(), "Abbreviation Code");
271 // Emit the abbreviations data.