xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
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.
40 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 
47 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".
56 void AsmPrinter::emitLabelDifferenceAsULEB128(const MCSymbol *Hi,
57                                               const MCSymbol *Lo) const {
58   OutStreamer->emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
59 }
60 
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").
116 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.
129 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 
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 
158 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 
181 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 
192 void AsmPrinter::emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {
193   emitLabelPlusOffset(Label, Offset, getDwarfOffsetByteSize());
194 }
195 
196 void AsmPrinter::emitDwarfLengthOrOffset(uint64_t Value) const {
197   assert(isDwarf64() || Value <= UINT32_MAX);
198   OutStreamer->emitIntValue(Value, getDwarfOffsetByteSize());
199 }
200 
201 void AsmPrinter::emitDwarfUnitLength(uint64_t Length,
202                                      const Twine &Comment) const {
203   OutStreamer->emitDwarfUnitLength(Length, Comment);
204 }
205 
206 MCSymbol *AsmPrinter::emitDwarfUnitLength(const Twine &Prefix,
207                                           const Twine &Comment) const {
208   return OutStreamer->emitDwarfUnitLength(Prefix, Comment);
209 }
210 
211 void AsmPrinter::emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo,
212                                     unsigned Encoding) const {
213   // The least significant 3 bits specify the width of the encoding
214   if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
215     emitLabelDifferenceAsULEB128(Hi, Lo);
216   else
217     emitLabelDifference(Hi, Lo, GetSizeOfEncodedValue(Encoding));
218 }
219 
220 void AsmPrinter::emitCallSiteValue(uint64_t Value, unsigned Encoding) const {
221   // The least significant 3 bits specify the width of the encoding
222   if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
223     emitULEB128(Value);
224   else
225     OutStreamer->emitIntValue(Value, GetSizeOfEncodedValue(Encoding));
226 }
227 
228 //===----------------------------------------------------------------------===//
229 // Dwarf Lowering Routines
230 //===----------------------------------------------------------------------===//
231 
232 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
233   switch (Inst.getOperation()) {
234   default:
235     llvm_unreachable("Unexpected instruction");
236   case MCCFIInstruction::OpDefCfaOffset:
237     OutStreamer->emitCFIDefCfaOffset(Inst.getOffset());
238     break;
239   case MCCFIInstruction::OpAdjustCfaOffset:
240     OutStreamer->emitCFIAdjustCfaOffset(Inst.getOffset());
241     break;
242   case MCCFIInstruction::OpDefCfa:
243     OutStreamer->emitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
244     break;
245   case MCCFIInstruction::OpDefCfaRegister:
246     OutStreamer->emitCFIDefCfaRegister(Inst.getRegister());
247     break;
248   case MCCFIInstruction::OpLLVMDefAspaceCfa:
249     OutStreamer->emitCFILLVMDefAspaceCfa(Inst.getRegister(), Inst.getOffset(),
250                                          Inst.getAddressSpace());
251     break;
252   case MCCFIInstruction::OpOffset:
253     OutStreamer->emitCFIOffset(Inst.getRegister(), Inst.getOffset());
254     break;
255   case MCCFIInstruction::OpRegister:
256     OutStreamer->emitCFIRegister(Inst.getRegister(), Inst.getRegister2());
257     break;
258   case MCCFIInstruction::OpWindowSave:
259     OutStreamer->emitCFIWindowSave();
260     break;
261   case MCCFIInstruction::OpNegateRAState:
262     OutStreamer->emitCFINegateRAState();
263     break;
264   case MCCFIInstruction::OpSameValue:
265     OutStreamer->emitCFISameValue(Inst.getRegister());
266     break;
267   case MCCFIInstruction::OpGnuArgsSize:
268     OutStreamer->emitCFIGnuArgsSize(Inst.getOffset());
269     break;
270   case MCCFIInstruction::OpEscape:
271     OutStreamer->AddComment(Inst.getComment());
272     OutStreamer->emitCFIEscape(Inst.getValues());
273     break;
274   case MCCFIInstruction::OpRestore:
275     OutStreamer->emitCFIRestore(Inst.getRegister());
276     break;
277   case MCCFIInstruction::OpUndefined:
278     OutStreamer->emitCFIUndefined(Inst.getRegister());
279     break;
280   }
281 }
282 
283 void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
284   // Emit the code (index) for the abbreviation.
285   if (isVerbose())
286     OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
287                             Twine::utohexstr(Die.getOffset()) + ":0x" +
288                             Twine::utohexstr(Die.getSize()) + " " +
289                             dwarf::TagString(Die.getTag()));
290   emitULEB128(Die.getAbbrevNumber());
291 
292   // Emit the DIE attribute values.
293   for (const auto &V : Die.values()) {
294     dwarf::Attribute Attr = V.getAttribute();
295     assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
296 
297     if (isVerbose()) {
298       OutStreamer->AddComment(dwarf::AttributeString(Attr));
299       if (Attr == dwarf::DW_AT_accessibility)
300         OutStreamer->AddComment(
301             dwarf::AccessibilityString(V.getDIEInteger().getValue()));
302     }
303 
304     // Emit an attribute using the defined form.
305     V.emitValue(this);
306   }
307 
308   // Emit the DIE children if any.
309   if (Die.hasChildren()) {
310     for (auto &Child : Die.children())
311       emitDwarfDIE(Child);
312 
313     OutStreamer->AddComment("End Of Children Mark");
314     emitInt8(0);
315   }
316 }
317 
318 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
319   // Emit the abbreviations code (base 1 index.)
320   emitULEB128(Abbrev.getNumber(), "Abbreviation Code");
321 
322   // Emit the abbreviations data.
323   Abbrev.Emit(this);
324 }
325