• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
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 // This file implements the Dwarf emissions parts of AsmPrinter.
10 //
11 //===----------------------------------------------------------------------===//
12 
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"
30 #include <cstdint>
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "asm-printer"
34 
35 //===----------------------------------------------------------------------===//
36 // Dwarf Emission Helper Routines
37 //===----------------------------------------------------------------------===//
38 
39 /// EmitSLEB128 - emit the specified signed leb128 value.
emitSLEB128(int64_t Value,const char * Desc) const40 void AsmPrinter::emitSLEB128(int64_t Value, const char *Desc) const {
41   if (isVerbose() && Desc)
42     OutStreamer->AddComment(Desc);
43 
44   OutStreamer->emitSLEB128IntValue(Value);
45 }
46 
emitULEB128(uint64_t Value,const char * Desc,unsigned PadTo) const47 void AsmPrinter::emitULEB128(uint64_t Value, const char *Desc,
48                              unsigned PadTo) const {
49   if (isVerbose() && Desc)
50     OutStreamer->AddComment(Desc);
51 
52   OutStreamer->emitULEB128IntValue(Value, PadTo);
53 }
54 
55 /// Emit something like ".uleb128 Hi-Lo".
emitLabelDifferenceAsULEB128(const MCSymbol * Hi,const MCSymbol * Lo) const56 void AsmPrinter::emitLabelDifferenceAsULEB128(const MCSymbol *Hi,
57                                               const MCSymbol *Lo) const {
58   OutStreamer->emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
59 }
60 
DecodeDWARFEncoding(unsigned Encoding)61 static const char *DecodeDWARFEncoding(unsigned Encoding) {
62   switch (Encoding) {
63   case dwarf::DW_EH_PE_absptr:
64     return "absptr";
65   case dwarf::DW_EH_PE_omit:
66     return "omit";
67   case dwarf::DW_EH_PE_pcrel:
68     return "pcrel";
69   case dwarf::DW_EH_PE_uleb128:
70     return "uleb128";
71   case dwarf::DW_EH_PE_sleb128:
72     return "sleb128";
73   case dwarf::DW_EH_PE_udata4:
74     return "udata4";
75   case dwarf::DW_EH_PE_udata8:
76     return "udata8";
77   case dwarf::DW_EH_PE_sdata4:
78     return "sdata4";
79   case dwarf::DW_EH_PE_sdata8:
80     return "sdata8";
81   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
82     return "pcrel udata4";
83   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
84     return "pcrel sdata4";
85   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
86     return "pcrel udata8";
87   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
88     return "pcrel sdata8";
89   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
90       :
91     return "indirect pcrel udata4";
92   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
93       :
94     return "indirect pcrel sdata4";
95   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
96       :
97     return "indirect pcrel udata8";
98   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
99       :
100     return "indirect pcrel sdata8";
101   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel |
102       dwarf::DW_EH_PE_sdata4:
103     return "indirect datarel sdata4";
104   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_datarel |
105       dwarf::DW_EH_PE_sdata8:
106     return "indirect datarel sdata8";
107   }
108 
109   return "<unknown encoding>";
110 }
111 
112 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
113 /// encoding.  If verbose assembly output is enabled, we output comments
114 /// describing the encoding.  Desc is an optional string saying what the
115 /// encoding is specifying (e.g. "LSDA").
emitEncodingByte(unsigned Val,const char * Desc) const116 void AsmPrinter::emitEncodingByte(unsigned Val, const char *Desc) const {
117   if (isVerbose()) {
118     if (Desc)
119       OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
120                               Twine(DecodeDWARFEncoding(Val)));
121     else
122       OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
123   }
124 
125   OutStreamer->emitIntValue(Val, 1);
126 }
127 
128 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
GetSizeOfEncodedValue(unsigned Encoding) const129 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
130   if (Encoding == dwarf::DW_EH_PE_omit)
131     return 0;
132 
133   switch (Encoding & 0x07) {
134   default:
135     llvm_unreachable("Invalid encoded value.");
136   case dwarf::DW_EH_PE_absptr:
137     return MF->getDataLayout().getPointerSize();
138   case dwarf::DW_EH_PE_udata2:
139     return 2;
140   case dwarf::DW_EH_PE_udata4:
141     return 4;
142   case dwarf::DW_EH_PE_udata8:
143     return 8;
144   }
145 }
146 
emitTTypeReference(const GlobalValue * GV,unsigned Encoding)147 void AsmPrinter::emitTTypeReference(const GlobalValue *GV, unsigned Encoding) {
148   if (GV) {
149     const TargetLoweringObjectFile &TLOF = getObjFileLowering();
150 
151     const MCExpr *Exp =
152         TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
153     OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding));
154   } else
155     OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding));
156 }
157 
emitDwarfSymbolReference(const MCSymbol * Label,bool ForceOffset) const158 void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
159                                           bool ForceOffset) const {
160   if (!ForceOffset) {
161     // On COFF targets, we have to emit the special .secrel32 directive.
162     if (MAI->needsDwarfSectionOffsetDirective()) {
163       assert(!isDwarf64() &&
164              "emitting DWARF64 is not implemented for COFF targets");
165       OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
166       return;
167     }
168 
169     // If the format uses relocations with dwarf, refer to the symbol directly.
170     if (MAI->doesDwarfUseRelocationsAcrossSections()) {
171       OutStreamer->emitSymbolValue(Label, getDwarfOffsetByteSize());
172       return;
173     }
174   }
175 
176   // Otherwise, emit it as a label difference from the start of the section.
177   emitLabelDifference(Label, Label->getSection().getBeginSymbol(),
178                       getDwarfOffsetByteSize());
179 }
180 
emitDwarfStringOffset(DwarfStringPoolEntry S) const181 void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntry S) const {
182   if (MAI->doesDwarfUseRelocationsAcrossSections()) {
183     assert(S.Symbol && "No symbol available");
184     emitDwarfSymbolReference(S.Symbol);
185     return;
186   }
187 
188   // Just emit the offset directly; no need for symbol math.
189   OutStreamer->emitIntValue(S.Offset, getDwarfOffsetByteSize());
190 }
191 
emitDwarfOffset(const MCSymbol * Label,uint64_t Offset) const192 void AsmPrinter::emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {
193   emitLabelPlusOffset(Label, Offset, getDwarfOffsetByteSize());
194 }
195 
emitDwarfLengthOrOffset(uint64_t Value) const196 void AsmPrinter::emitDwarfLengthOrOffset(uint64_t Value) const {
197   assert(isDwarf64() || Value <= UINT32_MAX);
198   OutStreamer->emitIntValue(Value, getDwarfOffsetByteSize());
199 }
200 
maybeEmitDwarf64Mark() const201 void AsmPrinter::maybeEmitDwarf64Mark() const {
202   if (!isDwarf64())
203     return;
204   OutStreamer->AddComment("DWARF64 Mark");
205   OutStreamer->emitInt32(dwarf::DW_LENGTH_DWARF64);
206 }
207 
emitDwarfUnitLength(uint64_t Length,const Twine & Comment) const208 void AsmPrinter::emitDwarfUnitLength(uint64_t Length,
209                                      const Twine &Comment) const {
210   assert(isDwarf64() || Length <= dwarf::DW_LENGTH_lo_reserved);
211   maybeEmitDwarf64Mark();
212   OutStreamer->AddComment(Comment);
213   OutStreamer->emitIntValue(Length, getDwarfOffsetByteSize());
214 }
215 
emitDwarfUnitLength(const MCSymbol * Hi,const MCSymbol * Lo,const Twine & Comment) const216 void AsmPrinter::emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo,
217                                      const Twine &Comment) const {
218   maybeEmitDwarf64Mark();
219   OutStreamer->AddComment(Comment);
220   OutStreamer->emitAbsoluteSymbolDiff(Hi, Lo, getDwarfOffsetByteSize());
221 }
222 
emitCallSiteOffset(const MCSymbol * Hi,const MCSymbol * Lo,unsigned Encoding) const223 void AsmPrinter::emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,
224                                     unsigned Encoding) const {
225   // The least significant 3 bits specify the width of the encoding
226   if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
227     emitLabelDifferenceAsULEB128(Hi, Lo);
228   else
229     emitLabelDifference(Hi, Lo, GetSizeOfEncodedValue(Encoding));
230 }
231 
emitCallSiteValue(uint64_t Value,unsigned Encoding) const232 void AsmPrinter::emitCallSiteValue(uint64_t Value, unsigned Encoding) const {
233   // The least significant 3 bits specify the width of the encoding
234   if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
235     emitULEB128(Value);
236   else
237     OutStreamer->emitIntValue(Value, GetSizeOfEncodedValue(Encoding));
238 }
239 
240 //===----------------------------------------------------------------------===//
241 // Dwarf Lowering Routines
242 //===----------------------------------------------------------------------===//
243 
emitCFIInstruction(const MCCFIInstruction & Inst) const244 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
245   switch (Inst.getOperation()) {
246   default:
247     llvm_unreachable("Unexpected instruction");
248   case MCCFIInstruction::OpDefCfaOffset:
249     OutStreamer->emitCFIDefCfaOffset(Inst.getOffset());
250     break;
251   case MCCFIInstruction::OpAdjustCfaOffset:
252     OutStreamer->emitCFIAdjustCfaOffset(Inst.getOffset());
253     break;
254   case MCCFIInstruction::OpDefCfa:
255     OutStreamer->emitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
256     break;
257   case MCCFIInstruction::OpDefCfaRegister:
258     OutStreamer->emitCFIDefCfaRegister(Inst.getRegister());
259     break;
260   case MCCFIInstruction::OpOffset:
261     OutStreamer->emitCFIOffset(Inst.getRegister(), Inst.getOffset());
262     break;
263   case MCCFIInstruction::OpRegister:
264     OutStreamer->emitCFIRegister(Inst.getRegister(), Inst.getRegister2());
265     break;
266   case MCCFIInstruction::OpWindowSave:
267     OutStreamer->emitCFIWindowSave();
268     break;
269   case MCCFIInstruction::OpNegateRAState:
270     OutStreamer->emitCFINegateRAState();
271     break;
272   case MCCFIInstruction::OpSameValue:
273     OutStreamer->emitCFISameValue(Inst.getRegister());
274     break;
275   case MCCFIInstruction::OpGnuArgsSize:
276     OutStreamer->emitCFIGnuArgsSize(Inst.getOffset());
277     break;
278   case MCCFIInstruction::OpEscape:
279     OutStreamer->AddComment(Inst.getComment());
280     OutStreamer->emitCFIEscape(Inst.getValues());
281     break;
282   case MCCFIInstruction::OpRestore:
283     OutStreamer->emitCFIRestore(Inst.getRegister());
284     break;
285   case MCCFIInstruction::OpUndefined:
286     OutStreamer->emitCFIUndefined(Inst.getRegister());
287     break;
288   }
289 }
290 
emitDwarfDIE(const DIE & Die) const291 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
292   // Emit the code (index) for the abbreviation.
293   if (isVerbose())
294     OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
295                             Twine::utohexstr(Die.getOffset()) + ":0x" +
296                             Twine::utohexstr(Die.getSize()) + " " +
297                             dwarf::TagString(Die.getTag()));
298   emitULEB128(Die.getAbbrevNumber());
299 
300   // Emit the DIE attribute values.
301   for (const auto &V : Die.values()) {
302     dwarf::Attribute Attr = V.getAttribute();
303     assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
304 
305     if (isVerbose()) {
306       OutStreamer->AddComment(dwarf::AttributeString(Attr));
307       if (Attr == dwarf::DW_AT_accessibility)
308         OutStreamer->AddComment(
309             dwarf::AccessibilityString(V.getDIEInteger().getValue()));
310     }
311 
312     // Emit an attribute using the defined form.
313     V.emitValue(this);
314   }
315 
316   // Emit the DIE children if any.
317   if (Die.hasChildren()) {
318     for (auto &Child : Die.children())
319       emitDwarfDIE(Child);
320 
321     OutStreamer->AddComment("End Of Children Mark");
322     emitInt8(0);
323   }
324 }
325 
emitDwarfAbbrev(const DIEAbbrev & Abbrev) const326 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
327   // Emit the abbreviations code (base 1 index.)
328   emitULEB128(Abbrev.getNumber(), "Abbreviation Code");
329 
330   // Emit the abbreviations data.
331   Abbrev.Emit(this);
332 }
333