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 } 102 103 return "<unknown encoding>"; 104 } 105 106 /// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an 107 /// encoding. If verbose assembly output is enabled, we output comments 108 /// describing the encoding. Desc is an optional string saying what the 109 /// encoding is specifying (e.g. "LSDA"). 110 void AsmPrinter::emitEncodingByte(unsigned Val, const char *Desc) const { 111 if (isVerbose()) { 112 if (Desc) 113 OutStreamer->AddComment(Twine(Desc) + " Encoding = " + 114 Twine(DecodeDWARFEncoding(Val))); 115 else 116 OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val)); 117 } 118 119 OutStreamer->emitIntValue(Val, 1); 120 } 121 122 /// GetSizeOfEncodedValue - Return the size of the encoding in bytes. 123 unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const { 124 if (Encoding == dwarf::DW_EH_PE_omit) 125 return 0; 126 127 switch (Encoding & 0x07) { 128 default: 129 llvm_unreachable("Invalid encoded value."); 130 case dwarf::DW_EH_PE_absptr: 131 return MF->getDataLayout().getPointerSize(); 132 case dwarf::DW_EH_PE_udata2: 133 return 2; 134 case dwarf::DW_EH_PE_udata4: 135 return 4; 136 case dwarf::DW_EH_PE_udata8: 137 return 8; 138 } 139 } 140 141 void AsmPrinter::emitTTypeReference(const GlobalValue *GV, 142 unsigned Encoding) const { 143 if (GV) { 144 const TargetLoweringObjectFile &TLOF = getObjFileLowering(); 145 146 const MCExpr *Exp = 147 TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer); 148 OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding)); 149 } else 150 OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding)); 151 } 152 153 void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label, 154 bool ForceOffset) const { 155 if (!ForceOffset) { 156 // On COFF targets, we have to emit the special .secrel32 directive. 157 if (MAI->needsDwarfSectionOffsetDirective()) { 158 assert(!isDwarf64() && 159 "emitting DWARF64 is not implemented for COFF targets"); 160 OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0); 161 return; 162 } 163 164 // If the format uses relocations with dwarf, refer to the symbol directly. 165 if (MAI->doesDwarfUseRelocationsAcrossSections()) { 166 OutStreamer->emitSymbolValue(Label, getDwarfOffsetByteSize()); 167 return; 168 } 169 } 170 171 // Otherwise, emit it as a label difference from the start of the section. 172 emitLabelDifference(Label, Label->getSection().getBeginSymbol(), 173 getDwarfOffsetByteSize()); 174 } 175 176 void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntry S) const { 177 if (MAI->doesDwarfUseRelocationsAcrossSections()) { 178 assert(S.Symbol && "No symbol available"); 179 emitDwarfSymbolReference(S.Symbol); 180 return; 181 } 182 183 // Just emit the offset directly; no need for symbol math. 184 OutStreamer->emitIntValue(S.Offset, getDwarfOffsetByteSize()); 185 } 186 187 void AsmPrinter::emitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const { 188 emitLabelPlusOffset(Label, Offset, getDwarfOffsetByteSize()); 189 } 190 191 void AsmPrinter::emitDwarfLengthOrOffset(uint64_t Value) const { 192 assert(isDwarf64() || Value <= UINT32_MAX); 193 OutStreamer->emitIntValue(Value, getDwarfOffsetByteSize()); 194 } 195 196 void AsmPrinter::maybeEmitDwarf64Mark() const { 197 if (!isDwarf64()) 198 return; 199 OutStreamer->AddComment("DWARF64 Mark"); 200 OutStreamer->emitInt32(dwarf::DW_LENGTH_DWARF64); 201 } 202 203 void AsmPrinter::emitDwarfUnitLength(uint64_t Length, 204 const Twine &Comment) const { 205 assert(isDwarf64() || Length <= dwarf::DW_LENGTH_lo_reserved); 206 maybeEmitDwarf64Mark(); 207 OutStreamer->AddComment(Comment); 208 OutStreamer->emitIntValue(Length, getDwarfOffsetByteSize()); 209 } 210 211 void AsmPrinter::emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo, 212 const Twine &Comment) const { 213 maybeEmitDwarf64Mark(); 214 OutStreamer->AddComment(Comment); 215 OutStreamer->emitAbsoluteSymbolDiff(Hi, Lo, getDwarfOffsetByteSize()); 216 } 217 218 void AsmPrinter::emitCallSiteOffset(const MCSymbol *Hi, const MCSymbol *Lo, 219 unsigned Encoding) const { 220 // The least significant 3 bits specify the width of the encoding 221 if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128) 222 emitLabelDifferenceAsULEB128(Hi, Lo); 223 else 224 emitLabelDifference(Hi, Lo, GetSizeOfEncodedValue(Encoding)); 225 } 226 227 void AsmPrinter::emitCallSiteValue(uint64_t Value, unsigned Encoding) const { 228 // The least significant 3 bits specify the width of the encoding 229 if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128) 230 emitULEB128(Value); 231 else 232 OutStreamer->emitIntValue(Value, GetSizeOfEncodedValue(Encoding)); 233 } 234 235 //===----------------------------------------------------------------------===// 236 // Dwarf Lowering Routines 237 //===----------------------------------------------------------------------===// 238 239 void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const { 240 switch (Inst.getOperation()) { 241 default: 242 llvm_unreachable("Unexpected instruction"); 243 case MCCFIInstruction::OpDefCfaOffset: 244 OutStreamer->emitCFIDefCfaOffset(Inst.getOffset()); 245 break; 246 case MCCFIInstruction::OpAdjustCfaOffset: 247 OutStreamer->emitCFIAdjustCfaOffset(Inst.getOffset()); 248 break; 249 case MCCFIInstruction::OpDefCfa: 250 OutStreamer->emitCFIDefCfa(Inst.getRegister(), Inst.getOffset()); 251 break; 252 case MCCFIInstruction::OpDefCfaRegister: 253 OutStreamer->emitCFIDefCfaRegister(Inst.getRegister()); 254 break; 255 case MCCFIInstruction::OpOffset: 256 OutStreamer->emitCFIOffset(Inst.getRegister(), Inst.getOffset()); 257 break; 258 case MCCFIInstruction::OpRegister: 259 OutStreamer->emitCFIRegister(Inst.getRegister(), Inst.getRegister2()); 260 break; 261 case MCCFIInstruction::OpWindowSave: 262 OutStreamer->emitCFIWindowSave(); 263 break; 264 case MCCFIInstruction::OpNegateRAState: 265 OutStreamer->emitCFINegateRAState(); 266 break; 267 case MCCFIInstruction::OpSameValue: 268 OutStreamer->emitCFISameValue(Inst.getRegister()); 269 break; 270 case MCCFIInstruction::OpGnuArgsSize: 271 OutStreamer->emitCFIGnuArgsSize(Inst.getOffset()); 272 break; 273 case MCCFIInstruction::OpEscape: 274 OutStreamer->AddComment(Inst.getComment()); 275 OutStreamer->emitCFIEscape(Inst.getValues()); 276 break; 277 case MCCFIInstruction::OpRestore: 278 OutStreamer->emitCFIRestore(Inst.getRegister()); 279 break; 280 case MCCFIInstruction::OpUndefined: 281 OutStreamer->emitCFIUndefined(Inst.getRegister()); 282 break; 283 } 284 } 285 286 void AsmPrinter::emitDwarfDIE(const DIE &Die) const { 287 // Emit the code (index) for the abbreviation. 288 if (isVerbose()) 289 OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" + 290 Twine::utohexstr(Die.getOffset()) + ":0x" + 291 Twine::utohexstr(Die.getSize()) + " " + 292 dwarf::TagString(Die.getTag())); 293 emitULEB128(Die.getAbbrevNumber()); 294 295 // Emit the DIE attribute values. 296 for (const auto &V : Die.values()) { 297 dwarf::Attribute Attr = V.getAttribute(); 298 assert(V.getForm() && "Too many attributes for DIE (check abbreviation)"); 299 300 if (isVerbose()) { 301 OutStreamer->AddComment(dwarf::AttributeString(Attr)); 302 if (Attr == dwarf::DW_AT_accessibility) 303 OutStreamer->AddComment( 304 dwarf::AccessibilityString(V.getDIEInteger().getValue())); 305 } 306 307 // Emit an attribute using the defined form. 308 V.emitValue(this); 309 } 310 311 // Emit the DIE children if any. 312 if (Die.hasChildren()) { 313 for (auto &Child : Die.children()) 314 emitDwarfDIE(Child); 315 316 OutStreamer->AddComment("End Of Children Mark"); 317 emitInt8(0); 318 } 319 } 320 321 void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const { 322 // Emit the abbreviations code (base 1 index.) 323 emitULEB128(Abbrev.getNumber(), "Abbreviation Code"); 324 325 // Emit the abbreviations data. 326 Abbrev.Emit(this); 327 } 328