1 //===--- unittests/DebugInfo/DWARF/DwarfGenerator.cpp -----------*- C++ -*-===// 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 #include "../lib/CodeGen/AsmPrinter/DwarfStringPool.h" 11 #include "DwarfGenerator.h" 12 #include "llvm/ADT/Triple.h" 13 #include "llvm/CodeGen/AsmPrinter.h" 14 #include "llvm/CodeGen/DIE.h" 15 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 16 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" 17 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 18 #include "llvm/IR/LegacyPassManagers.h" 19 #include "llvm/MC/MCAsmBackend.h" 20 #include "llvm/MC/MCAsmInfo.h" 21 #include "llvm/MC/MCCodeEmitter.h" 22 #include "llvm/MC/MCContext.h" 23 #include "llvm/MC/MCDwarf.h" 24 #include "llvm/MC/MCInstrInfo.h" 25 #include "llvm/MC/MCObjectFileInfo.h" 26 #include "llvm/MC/MCRegisterInfo.h" 27 #include "llvm/MC/MCStreamer.h" 28 #include "llvm/MC/MCSubtargetInfo.h" 29 #include "llvm/MC/MCTargetOptionsCommandFlags.h" 30 #include "llvm/PassAnalysisSupport.h" 31 #include "llvm/Support/Dwarf.h" 32 #include "llvm/Support/LEB128.h" 33 #include "llvm/Support/TargetRegistry.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include "llvm/Target/TargetMachine.h" 36 #include "llvm/Target/TargetOptions.h" 37 38 using namespace llvm; 39 using namespace dwarf; 40 41 namespace {} // end anonymous namespace 42 43 //===----------------------------------------------------------------------===// 44 /// dwarfgen::DIE implementation. 45 //===----------------------------------------------------------------------===// 46 unsigned dwarfgen::DIE::computeSizeAndOffsets(unsigned Offset) { 47 auto &DG = CU->getGenerator(); 48 return Die->computeOffsetsAndAbbrevs(DG.getAsmPrinter(), DG.getAbbrevSet(), 49 Offset); 50 } 51 52 void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, uint64_t U) { 53 auto &DG = CU->getGenerator(); 54 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, 55 DIEInteger(U)); 56 } 57 58 void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, 59 StringRef String) { 60 auto &DG = CU->getGenerator(); 61 if (Form == DW_FORM_string) { 62 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, 63 new (DG.getAllocator()) 64 DIEInlineString(String, DG.getAllocator())); 65 } else { 66 Die->addValue( 67 DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, 68 DIEString(DG.getStringPool().getEntry(*DG.getAsmPrinter(), String))); 69 } 70 } 71 72 void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, 73 dwarfgen::DIE &RefDie) { 74 auto &DG = CU->getGenerator(); 75 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, 76 DIEEntry(*RefDie.Die)); 77 } 78 79 void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, const void *P, 80 size_t S) { 81 auto &DG = CU->getGenerator(); 82 DIEBlock *Block = new (DG.getAllocator()) DIEBlock; 83 for (size_t I = 0; I < S; ++I) 84 Block->addValue( 85 DG.getAllocator(), (dwarf::Attribute)0, dwarf::DW_FORM_data1, 86 DIEInteger( 87 (const_cast<uint8_t *>(static_cast<const uint8_t *>(P)))[I])); 88 89 Block->ComputeSize(DG.getAsmPrinter()); 90 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, 91 Block); 92 } 93 94 void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form) { 95 auto &DG = CU->getGenerator(); 96 assert(Form == DW_FORM_flag_present); 97 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, 98 DIEInteger(1)); 99 } 100 101 dwarfgen::DIE dwarfgen::DIE::addChild(dwarf::Tag Tag) { 102 auto &DG = CU->getGenerator(); 103 return dwarfgen::DIE(CU, 104 &Die->addChild(llvm::DIE::get(DG.getAllocator(), Tag))); 105 } 106 107 dwarfgen::DIE dwarfgen::CompileUnit::getUnitDIE() { 108 return dwarfgen::DIE(this, &DU.getUnitDie()); 109 } 110 111 void dwarfgen::DIE::setForceChildren() { 112 Die->setForceChildren(true); 113 } 114 115 //===----------------------------------------------------------------------===// 116 /// dwarfgen::Generator implementation. 117 //===----------------------------------------------------------------------===// 118 119 dwarfgen::Generator::Generator() 120 : MAB(nullptr), MCE(nullptr), MS(nullptr), StringPool(nullptr), 121 Abbreviations(Allocator) {} 122 dwarfgen::Generator::~Generator() = default; 123 124 llvm::Expected<std::unique_ptr<dwarfgen::Generator>> 125 dwarfgen::Generator::create(Triple TheTriple, uint16_t DwarfVersion) { 126 std::unique_ptr<dwarfgen::Generator> GenUP(new dwarfgen::Generator()); 127 llvm::Error error = GenUP->init(TheTriple, DwarfVersion); 128 if (error) 129 return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(error)); 130 return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(GenUP)); 131 } 132 133 llvm::Error dwarfgen::Generator::init(Triple TheTriple, uint16_t V) { 134 Version = V; 135 std::string ErrorStr; 136 std::string TripleName; 137 138 // Get the target. 139 const Target *TheTarget = 140 TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr); 141 if (!TheTarget) 142 return make_error<StringError>(ErrorStr, inconvertibleErrorCode()); 143 144 TripleName = TheTriple.getTriple(); 145 146 // Create all the MC Objects. 147 MRI.reset(TheTarget->createMCRegInfo(TripleName)); 148 if (!MRI) 149 return make_error<StringError>(Twine("no register info for target ") + 150 TripleName, 151 inconvertibleErrorCode()); 152 153 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName)); 154 if (!MAI) 155 return make_error<StringError>("no asm info for target " + TripleName, 156 inconvertibleErrorCode()); 157 158 MOFI.reset(new MCObjectFileInfo); 159 MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get())); 160 MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, CodeModel::Default, *MC); 161 162 MCTargetOptions Options; 163 MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "", Options); 164 if (!MAB) 165 return make_error<StringError>("no asm backend for target " + TripleName, 166 inconvertibleErrorCode()); 167 168 MII.reset(TheTarget->createMCInstrInfo()); 169 if (!MII) 170 return make_error<StringError>("no instr info info for target " + 171 TripleName, 172 inconvertibleErrorCode()); 173 174 MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", "")); 175 if (!MSTI) 176 return make_error<StringError>("no subtarget info for target " + TripleName, 177 inconvertibleErrorCode()); 178 179 MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC); 180 if (!MCE) 181 return make_error<StringError>("no code emitter for target " + TripleName, 182 inconvertibleErrorCode()); 183 184 Stream = make_unique<raw_svector_ostream>(FileBytes); 185 186 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); 187 MS = TheTarget->createMCObjectStreamer( 188 TheTriple, *MC, *MAB, *Stream, MCE, *MSTI, MCOptions.MCRelaxAll, 189 MCOptions.MCIncrementalLinkerCompatible, 190 /*DWARFMustBeAtTheEnd*/ false); 191 if (!MS) 192 return make_error<StringError>("no object streamer for target " + 193 TripleName, 194 inconvertibleErrorCode()); 195 196 // Finally create the AsmPrinter we'll use to emit the DIEs. 197 TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(), 198 None)); 199 if (!TM) 200 return make_error<StringError>("no target machine for target " + TripleName, 201 inconvertibleErrorCode()); 202 203 Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS))); 204 if (!Asm) 205 return make_error<StringError>("no asm printer for target " + TripleName, 206 inconvertibleErrorCode()); 207 208 // Set the DWARF version correctly on all classes that we use. 209 MC->setDwarfVersion(Version); 210 Asm->setDwarfVersion(Version); 211 212 StringPool = llvm::make_unique<DwarfStringPool>(Allocator, *Asm, StringRef()); 213 214 return Error::success(); 215 } 216 217 StringRef dwarfgen::Generator::generate() { 218 // Offset from the first CU in the debug info section is 0 initially. 219 unsigned SecOffset = 0; 220 221 // Iterate over each compile unit and set the size and offsets for each 222 // DIE within each compile unit. All offsets are CU relative. 223 for (auto &CU : CompileUnits) { 224 // Set the absolute .debug_info offset for this compile unit. 225 CU->setOffset(SecOffset); 226 // The DIEs contain compile unit relative offsets. 227 unsigned CUOffset = 11; 228 CUOffset = CU->getUnitDIE().computeSizeAndOffsets(CUOffset); 229 // Update our absolute .debug_info offset. 230 SecOffset += CUOffset; 231 CU->setLength(CUOffset - 4); 232 } 233 Abbreviations.Emit(Asm.get(), MOFI->getDwarfAbbrevSection()); 234 StringPool->emit(*Asm, MOFI->getDwarfStrSection()); 235 MS->SwitchSection(MOFI->getDwarfInfoSection()); 236 for (auto &CU : CompileUnits) { 237 uint16_t Version = CU->getVersion(); 238 auto Length = CU->getLength(); 239 MC->setDwarfVersion(Version); 240 assert(Length != -1U); 241 Asm->EmitInt32(Length); 242 Asm->EmitInt16(Version); 243 Asm->EmitInt32(0); 244 Asm->EmitInt8(CU->getAddressSize()); 245 Asm->emitDwarfDIE(*CU->getUnitDIE().Die); 246 } 247 248 MS->Finish(); 249 if (FileBytes.empty()) 250 return StringRef(); 251 return StringRef(FileBytes.data(), FileBytes.size()); 252 } 253 254 bool dwarfgen::Generator::saveFile(StringRef Path) { 255 if (FileBytes.empty()) 256 return false; 257 std::error_code EC; 258 raw_fd_ostream Strm(Path, EC, sys::fs::F_None); 259 if (EC) 260 return false; 261 Strm.write(FileBytes.data(), FileBytes.size()); 262 Strm.close(); 263 return true; 264 } 265 266 dwarfgen::CompileUnit &dwarfgen::Generator::addCompileUnit() { 267 CompileUnits.push_back(std::unique_ptr<CompileUnit>( 268 new CompileUnit(*this, Version, Asm->getPointerSize()))); 269 return *CompileUnits.back(); 270 } 271