1 //===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Dwarf emissions parts of AsmPrinter.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ByteStreamer.h"
15 #include "DwarfDebug.h"
16 #include "DwarfExpression.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/BinaryFormat/Dwarf.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCRegisterInfo.h"
26 #include "llvm/MC/MCSection.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/MC/MachineLocation.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetSubtargetInfo.h"
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "asm-printer"
37 
38 //===----------------------------------------------------------------------===//
39 // Dwarf Emission Helper Routines
40 //===----------------------------------------------------------------------===//
41 
42 /// EmitSLEB128 - emit the specified signed leb128 value.
43 void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
44   if (isVerbose() && Desc)
45     OutStreamer->AddComment(Desc);
46 
47   OutStreamer->EmitSLEB128IntValue(Value);
48 }
49 
50 /// EmitULEB128 - emit the specified unsigned leb128 value.
51 void AsmPrinter::EmitPaddedULEB128(uint64_t Value, unsigned PadTo,
52                                    const char *Desc) const {
53   if (isVerbose() && Desc)
54     OutStreamer->AddComment(Desc);
55 
56   OutStreamer->EmitPaddedULEB128IntValue(Value, PadTo);
57 }
58 
59 void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc) const {
60   if (isVerbose() && Desc)
61     OutStreamer->AddComment(Desc);
62 
63   OutStreamer->EmitULEB128IntValue(Value);
64 }
65 
66 static const char *DecodeDWARFEncoding(unsigned Encoding) {
67   switch (Encoding) {
68   case dwarf::DW_EH_PE_absptr:
69     return "absptr";
70   case dwarf::DW_EH_PE_omit:
71     return "omit";
72   case dwarf::DW_EH_PE_pcrel:
73     return "pcrel";
74   case dwarf::DW_EH_PE_udata4:
75     return "udata4";
76   case dwarf::DW_EH_PE_udata8:
77     return "udata8";
78   case dwarf::DW_EH_PE_sdata4:
79     return "sdata4";
80   case dwarf::DW_EH_PE_sdata8:
81     return "sdata8";
82   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
83     return "pcrel udata4";
84   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
85     return "pcrel sdata4";
86   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
87     return "pcrel udata8";
88   case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
89     return "pcrel sdata8";
90   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
91       :
92     return "indirect pcrel udata4";
93   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
94       :
95     return "indirect pcrel sdata4";
96   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
97       :
98     return "indirect pcrel udata8";
99   case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
100       :
101     return "indirect pcrel sdata8";
102   }
103 
104   return "<unknown encoding>";
105 }
106 
107 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
108 /// encoding.  If verbose assembly output is enabled, we output comments
109 /// describing the encoding.  Desc is an optional string saying what the
110 /// encoding is specifying (e.g. "LSDA").
111 void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
112   if (isVerbose()) {
113     if (Desc)
114       OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
115                               Twine(DecodeDWARFEncoding(Val)));
116     else
117       OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
118   }
119 
120   OutStreamer->EmitIntValue(Val, 1);
121 }
122 
123 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
124 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
125   if (Encoding == dwarf::DW_EH_PE_omit)
126     return 0;
127 
128   switch (Encoding & 0x07) {
129   default:
130     llvm_unreachable("Invalid encoded value.");
131   case dwarf::DW_EH_PE_absptr:
132     return MF->getDataLayout().getPointerSize();
133   case dwarf::DW_EH_PE_udata2:
134     return 2;
135   case dwarf::DW_EH_PE_udata4:
136     return 4;
137   case dwarf::DW_EH_PE_udata8:
138     return 8;
139   }
140 }
141 
142 void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
143                                     unsigned Encoding) const {
144   if (GV) {
145     const TargetLoweringObjectFile &TLOF = getObjFileLowering();
146 
147     const MCExpr *Exp =
148         TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
149     OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
150   } else
151     OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
152 }
153 
154 void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
155                                           bool ForceOffset) const {
156   if (!ForceOffset) {
157     // On COFF targets, we have to emit the special .secrel32 directive.
158     if (MAI->needsDwarfSectionOffsetDirective()) {
159       OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
160       return;
161     }
162 
163     // If the format uses relocations with dwarf, refer to the symbol directly.
164     if (MAI->doesDwarfUseRelocationsAcrossSections()) {
165       OutStreamer->EmitSymbolValue(Label, 4);
166       return;
167     }
168   }
169 
170   // Otherwise, emit it as a label difference from the start of the section.
171   EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
172 }
173 
174 void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntryRef S) const {
175   if (MAI->doesDwarfUseRelocationsAcrossSections()) {
176     emitDwarfSymbolReference(S.getSymbol());
177     return;
178   }
179 
180   // Just emit the offset directly; no need for symbol math.
181   EmitInt32(S.getOffset());
182 }
183 
184 //===----------------------------------------------------------------------===//
185 // Dwarf Lowering Routines
186 //===----------------------------------------------------------------------===//
187 
188 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
189   switch (Inst.getOperation()) {
190   default:
191     llvm_unreachable("Unexpected instruction");
192   case MCCFIInstruction::OpDefCfaOffset:
193     OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
194     break;
195   case MCCFIInstruction::OpAdjustCfaOffset:
196     OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
197     break;
198   case MCCFIInstruction::OpDefCfa:
199     OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
200     break;
201   case MCCFIInstruction::OpDefCfaRegister:
202     OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
203     break;
204   case MCCFIInstruction::OpOffset:
205     OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
206     break;
207   case MCCFIInstruction::OpRegister:
208     OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
209     break;
210   case MCCFIInstruction::OpWindowSave:
211     OutStreamer->EmitCFIWindowSave();
212     break;
213   case MCCFIInstruction::OpSameValue:
214     OutStreamer->EmitCFISameValue(Inst.getRegister());
215     break;
216   case MCCFIInstruction::OpGnuArgsSize:
217     OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
218     break;
219   case MCCFIInstruction::OpEscape:
220     OutStreamer->EmitCFIEscape(Inst.getValues());
221     break;
222   case MCCFIInstruction::OpRestore:
223     OutStreamer->EmitCFIRestore(Inst.getRegister());
224     break;
225   }
226 }
227 
228 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
229   // Emit the code (index) for the abbreviation.
230   if (isVerbose())
231     OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
232                             Twine::utohexstr(Die.getOffset()) + ":0x" +
233                             Twine::utohexstr(Die.getSize()) + " " +
234                             dwarf::TagString(Die.getTag()));
235   EmitULEB128(Die.getAbbrevNumber());
236 
237   // Emit the DIE attribute values.
238   for (const auto &V : Die.values()) {
239     dwarf::Attribute Attr = V.getAttribute();
240     assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
241 
242     if (isVerbose()) {
243       OutStreamer->AddComment(dwarf::AttributeString(Attr));
244       if (Attr == dwarf::DW_AT_accessibility)
245         OutStreamer->AddComment(
246             dwarf::AccessibilityString(V.getDIEInteger().getValue()));
247     }
248 
249     // Emit an attribute using the defined form.
250     V.EmitValue(this);
251   }
252 
253   // Emit the DIE children if any.
254   if (Die.hasChildren()) {
255     for (auto &Child : Die.children())
256       emitDwarfDIE(Child);
257 
258     OutStreamer->AddComment("End Of Children Mark");
259     EmitInt8(0);
260   }
261 }
262 
263 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
264   // Emit the abbreviations code (base 1 index.)
265   EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
266 
267   // Emit the abbreviations data.
268   Abbrev.Emit(this);
269 }
270