Daniel Jasper | 0f77869 | 2016-12-08 12:45:29 +0000 | [diff] [blame] | 1 | //===--- unittests/DebugInfo/DWARF/DwarfGenerator.cpp -----------*- C++ -*-===// |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 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 "DwarfGenerator.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 11 | #include "../lib/CodeGen/AsmPrinter/DwarfStringPool.h" |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Triple.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 13 | #include "llvm/BinaryFormat/Dwarf.h" |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/AsmPrinter.h" |
| 15 | #include "llvm/CodeGen/DIE.h" |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" |
| 17 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmBackend.h" |
| 19 | #include "llvm/MC/MCAsmInfo.h" |
| 20 | #include "llvm/MC/MCCodeEmitter.h" |
| 21 | #include "llvm/MC/MCContext.h" |
| 22 | #include "llvm/MC/MCDwarf.h" |
| 23 | #include "llvm/MC/MCInstrInfo.h" |
| 24 | #include "llvm/MC/MCObjectFileInfo.h" |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame^] | 25 | #include "llvm/MC/MCObjectWriter.h" |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCRegisterInfo.h" |
| 27 | #include "llvm/MC/MCStreamer.h" |
| 28 | #include "llvm/MC/MCSubtargetInfo.h" |
David Blaikie | 4333f97 | 2018-04-11 18:49:37 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCTargetOptionsCommandFlags.inc" |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 30 | #include "llvm/PassAnalysisSupport.h" |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TargetRegistry.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include "llvm/Target/TargetOptions.h" |
| 35 | |
| 36 | using namespace llvm; |
| 37 | using namespace dwarf; |
| 38 | |
| 39 | namespace {} // end anonymous namespace |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | /// dwarfgen::DIE implementation. |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | unsigned dwarfgen::DIE::computeSizeAndOffsets(unsigned Offset) { |
| 45 | auto &DG = CU->getGenerator(); |
| 46 | return Die->computeOffsetsAndAbbrevs(DG.getAsmPrinter(), DG.getAbbrevSet(), |
| 47 | Offset); |
| 48 | } |
| 49 | |
| 50 | void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, uint64_t U) { |
| 51 | auto &DG = CU->getGenerator(); |
| 52 | Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, |
| 53 | DIEInteger(U)); |
| 54 | } |
| 55 | |
| 56 | void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, |
| 57 | StringRef String) { |
| 58 | auto &DG = CU->getGenerator(); |
| 59 | if (Form == DW_FORM_string) { |
| 60 | Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, |
Benjamin Kramer | eedc405 | 2016-12-09 13:33:41 +0000 | [diff] [blame] | 61 | new (DG.getAllocator()) |
| 62 | DIEInlineString(String, DG.getAllocator())); |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 63 | } else { |
| 64 | Die->addValue( |
| 65 | DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, |
| 66 | DIEString(DG.getStringPool().getEntry(*DG.getAsmPrinter(), String))); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, |
| 71 | dwarfgen::DIE &RefDie) { |
| 72 | auto &DG = CU->getGenerator(); |
| 73 | Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, |
| 74 | DIEEntry(*RefDie.Die)); |
| 75 | } |
| 76 | |
| 77 | void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, const void *P, |
| 78 | size_t S) { |
| 79 | auto &DG = CU->getGenerator(); |
| 80 | DIEBlock *Block = new (DG.getAllocator()) DIEBlock; |
| 81 | for (size_t I = 0; I < S; ++I) |
Saleem Abdulrasool | ecf13bb | 2016-12-27 18:35:24 +0000 | [diff] [blame] | 82 | Block->addValue( |
| 83 | DG.getAllocator(), (dwarf::Attribute)0, dwarf::DW_FORM_data1, |
| 84 | DIEInteger( |
| 85 | (const_cast<uint8_t *>(static_cast<const uint8_t *>(P)))[I])); |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 86 | |
| 87 | Block->ComputeSize(DG.getAsmPrinter()); |
| 88 | Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, |
| 89 | Block); |
| 90 | } |
| 91 | |
| 92 | void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form) { |
| 93 | auto &DG = CU->getGenerator(); |
| 94 | assert(Form == DW_FORM_flag_present); |
| 95 | Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form, |
| 96 | DIEInteger(1)); |
| 97 | } |
| 98 | |
| 99 | dwarfgen::DIE dwarfgen::DIE::addChild(dwarf::Tag Tag) { |
| 100 | auto &DG = CU->getGenerator(); |
| 101 | return dwarfgen::DIE(CU, |
| 102 | &Die->addChild(llvm::DIE::get(DG.getAllocator(), Tag))); |
| 103 | } |
| 104 | |
| 105 | dwarfgen::DIE dwarfgen::CompileUnit::getUnitDIE() { |
| 106 | return dwarfgen::DIE(this, &DU.getUnitDie()); |
| 107 | } |
| 108 | |
| 109 | //===----------------------------------------------------------------------===// |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 110 | /// dwarfgen::LineTable implementation. |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | DWARFDebugLine::Prologue dwarfgen::LineTable::createBasicPrologue() const { |
| 113 | DWARFDebugLine::Prologue P; |
| 114 | switch (Version) { |
| 115 | case 2: |
| 116 | case 3: |
| 117 | P.TotalLength = 41; |
| 118 | P.PrologueLength = 35; |
| 119 | break; |
| 120 | case 4: |
| 121 | P.TotalLength = 42; |
| 122 | P.PrologueLength = 36; |
| 123 | break; |
| 124 | case 5: |
| 125 | P.TotalLength = 47; |
| 126 | P.PrologueLength = 39; |
| 127 | P.FormParams.AddrSize = AddrSize; |
| 128 | break; |
| 129 | default: |
| 130 | llvm_unreachable("unsupported version"); |
| 131 | } |
| 132 | if (Format == DWARF64) { |
| 133 | P.TotalLength += 4; |
| 134 | P.FormParams.Format = DWARF64; |
| 135 | } |
| 136 | P.FormParams.Version = Version; |
| 137 | P.MinInstLength = 1; |
| 138 | P.MaxOpsPerInst = 1; |
| 139 | P.DefaultIsStmt = 1; |
| 140 | P.LineBase = -5; |
| 141 | P.LineRange = 14; |
| 142 | P.OpcodeBase = 13; |
| 143 | P.StandardOpcodeLengths = {0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1}; |
| 144 | P.IncludeDirectories.push_back(DWARFFormValue(DW_FORM_string)); |
| 145 | P.IncludeDirectories.back().setPValue("a dir"); |
| 146 | P.FileNames.push_back(DWARFDebugLine::FileNameEntry()); |
| 147 | P.FileNames.back().Name.setPValue("a file"); |
| 148 | P.FileNames.back().Name.setForm(DW_FORM_string); |
| 149 | return P; |
| 150 | } |
| 151 | |
| 152 | void dwarfgen::LineTable::setPrologue(DWARFDebugLine::Prologue NewPrologue) { |
| 153 | Prologue = NewPrologue; |
| 154 | CustomPrologue.clear(); |
| 155 | } |
| 156 | |
| 157 | void dwarfgen::LineTable::setCustomPrologue( |
| 158 | ArrayRef<ValueAndLength> NewPrologue) { |
| 159 | Prologue.reset(); |
| 160 | CustomPrologue = NewPrologue; |
| 161 | } |
| 162 | |
| 163 | void dwarfgen::LineTable::addByte(uint8_t Value) { |
| 164 | Contents.push_back({Value, Byte}); |
| 165 | } |
| 166 | |
| 167 | void dwarfgen::LineTable::addStandardOpcode(uint8_t Opcode, |
| 168 | ArrayRef<ValueAndLength> Operands) { |
| 169 | Contents.push_back({Opcode, Byte}); |
| 170 | Contents.insert(Contents.end(), Operands.begin(), Operands.end()); |
| 171 | } |
| 172 | |
| 173 | void dwarfgen::LineTable::addExtendedOpcode(uint64_t Length, uint8_t Opcode, |
| 174 | ArrayRef<ValueAndLength> Operands) { |
| 175 | Contents.push_back({0, Byte}); |
| 176 | Contents.push_back({Length, ULEB}); |
| 177 | Contents.push_back({Opcode, Byte}); |
| 178 | Contents.insert(Contents.end(), Operands.begin(), Operands.end()); |
| 179 | } |
| 180 | |
| 181 | void dwarfgen::LineTable::generate(MCContext &MC, AsmPrinter &Asm) const { |
| 182 | MC.setDwarfVersion(Version); |
| 183 | |
| 184 | MCSymbol *EndSymbol = nullptr; |
| 185 | if (!CustomPrologue.empty()) { |
| 186 | writeData(CustomPrologue, Asm); |
| 187 | } else if (!Prologue) { |
| 188 | EndSymbol = writeDefaultPrologue(Asm); |
| 189 | } else { |
| 190 | writePrologue(Asm); |
| 191 | } |
| 192 | |
| 193 | writeData(Contents, Asm); |
| 194 | if (EndSymbol != nullptr) |
| 195 | Asm.OutStreamer->EmitLabel(EndSymbol); |
| 196 | } |
| 197 | |
| 198 | void dwarfgen::LineTable::writeData(ArrayRef<ValueAndLength> Data, |
| 199 | AsmPrinter &Asm) const { |
| 200 | for (auto Entry : Data) { |
| 201 | switch (Entry.Length) { |
| 202 | case Byte: |
| 203 | case Half: |
| 204 | case Long: |
| 205 | case Quad: |
| 206 | Asm.OutStreamer->EmitIntValue(Entry.Value, Entry.Length); |
Roman Lebedev | e1b0e29 | 2018-05-10 14:16:41 +0000 | [diff] [blame] | 207 | continue; |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 208 | case ULEB: |
| 209 | Asm.EmitULEB128(Entry.Value); |
Roman Lebedev | e1b0e29 | 2018-05-10 14:16:41 +0000 | [diff] [blame] | 210 | continue; |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 211 | case SLEB: |
| 212 | Asm.EmitSLEB128(Entry.Value); |
Roman Lebedev | e1b0e29 | 2018-05-10 14:16:41 +0000 | [diff] [blame] | 213 | continue; |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 214 | } |
Roman Lebedev | e1b0e29 | 2018-05-10 14:16:41 +0000 | [diff] [blame] | 215 | llvm_unreachable("unsupported ValueAndLength Length value"); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | MCSymbol *dwarfgen::LineTable::writeDefaultPrologue(AsmPrinter &Asm) const { |
| 220 | MCSymbol *UnitStart = Asm.createTempSymbol("line_unit_start"); |
| 221 | MCSymbol *UnitEnd = Asm.createTempSymbol("line_unit_end"); |
| 222 | if (Format == DwarfFormat::DWARF64) { |
| 223 | Asm.emitInt32(0xffffffff); |
| 224 | Asm.EmitLabelDifference(UnitEnd, UnitStart, 8); |
| 225 | } else { |
| 226 | Asm.EmitLabelDifference(UnitEnd, UnitStart, 4); |
| 227 | } |
| 228 | Asm.OutStreamer->EmitLabel(UnitStart); |
| 229 | Asm.emitInt16(Version); |
| 230 | if (Version == 5) { |
| 231 | Asm.emitInt8(AddrSize); |
| 232 | Asm.emitInt8(SegSize); |
| 233 | } |
| 234 | |
| 235 | MCSymbol *PrologueStart = Asm.createTempSymbol("line_prologue_start"); |
| 236 | MCSymbol *PrologueEnd = Asm.createTempSymbol("line_prologue_end"); |
| 237 | Asm.EmitLabelDifference(PrologueEnd, PrologueStart, |
| 238 | Format == DwarfFormat::DWARF64 ? 8 : 4); |
| 239 | Asm.OutStreamer->EmitLabel(PrologueStart); |
| 240 | |
| 241 | DWARFDebugLine::Prologue DefaultPrologue = createBasicPrologue(); |
| 242 | writeProloguePayload(DefaultPrologue, Asm); |
| 243 | Asm.OutStreamer->EmitLabel(PrologueEnd); |
| 244 | return UnitEnd; |
| 245 | } |
| 246 | |
| 247 | void dwarfgen::LineTable::writePrologue(AsmPrinter &Asm) const { |
| 248 | if (Format == DwarfFormat::DWARF64) { |
| 249 | Asm.emitInt32(0xffffffff); |
| 250 | Asm.emitInt64(Prologue->TotalLength); |
| 251 | } else { |
| 252 | Asm.emitInt32(Prologue->TotalLength); |
| 253 | } |
| 254 | Asm.emitInt16(Prologue->getVersion()); |
| 255 | if (Version == 5) { |
| 256 | Asm.emitInt8(Prologue->getAddressSize()); |
| 257 | Asm.emitInt8(Prologue->SegSelectorSize); |
| 258 | } |
| 259 | if (Format == DwarfFormat::DWARF64) |
| 260 | Asm.emitInt64(Prologue->PrologueLength); |
| 261 | else |
| 262 | Asm.emitInt32(Prologue->PrologueLength); |
| 263 | |
| 264 | writeProloguePayload(*Prologue, Asm); |
| 265 | } |
| 266 | |
| 267 | static void writeCString(StringRef Str, AsmPrinter &Asm) { |
| 268 | Asm.OutStreamer->EmitBytes(Str); |
| 269 | Asm.emitInt8(0); |
| 270 | } |
| 271 | |
| 272 | static void writeV2IncludeAndFileTable(const DWARFDebugLine::Prologue &Prologue, |
| 273 | AsmPrinter &Asm) { |
| 274 | for (auto Include : Prologue.IncludeDirectories) { |
| 275 | assert(Include.getAsCString() && "expected a string form for include dir"); |
| 276 | writeCString(*Include.getAsCString(), Asm); |
| 277 | } |
| 278 | Asm.emitInt8(0); |
| 279 | |
| 280 | for (auto File : Prologue.FileNames) { |
| 281 | assert(File.Name.getAsCString() && "expected a string form for file name"); |
| 282 | writeCString(*File.Name.getAsCString(), Asm); |
| 283 | Asm.EmitULEB128(File.DirIdx); |
| 284 | Asm.EmitULEB128(File.ModTime); |
| 285 | Asm.EmitULEB128(File.Length); |
| 286 | } |
| 287 | Asm.emitInt8(0); |
| 288 | } |
| 289 | |
| 290 | static void writeV5IncludeAndFileTable(const DWARFDebugLine::Prologue &Prologue, |
| 291 | AsmPrinter &Asm) { |
| 292 | Asm.emitInt8(1); // directory_entry_format_count. |
| 293 | // TODO: Add support for other content descriptions - we currently only |
| 294 | // support a single DW_LNCT_path/DW_FORM_string. |
| 295 | Asm.EmitULEB128(DW_LNCT_path); |
| 296 | Asm.EmitULEB128(DW_FORM_string); |
| 297 | Asm.EmitULEB128(Prologue.IncludeDirectories.size()); |
| 298 | for (auto Include : Prologue.IncludeDirectories) { |
| 299 | assert(Include.getAsCString() && "expected a string form for include dir"); |
| 300 | writeCString(*Include.getAsCString(), Asm); |
| 301 | } |
| 302 | |
| 303 | Asm.emitInt8(1); // file_name_entry_format_count. |
| 304 | Asm.EmitULEB128(DW_LNCT_path); |
| 305 | Asm.EmitULEB128(DW_FORM_string); |
| 306 | Asm.EmitULEB128(Prologue.FileNames.size()); |
| 307 | for (auto File : Prologue.FileNames) { |
| 308 | assert(File.Name.getAsCString() && "expected a string form for file name"); |
| 309 | writeCString(*File.Name.getAsCString(), Asm); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void dwarfgen::LineTable::writeProloguePayload( |
| 314 | const DWARFDebugLine::Prologue &Prologue, AsmPrinter &Asm) const { |
| 315 | Asm.emitInt8(Prologue.MinInstLength); |
| 316 | if (Version >= 4) |
| 317 | Asm.emitInt8(Prologue.MaxOpsPerInst); |
| 318 | Asm.emitInt8(Prologue.DefaultIsStmt); |
| 319 | Asm.emitInt8(Prologue.LineBase); |
| 320 | Asm.emitInt8(Prologue.LineRange); |
| 321 | Asm.emitInt8(Prologue.OpcodeBase); |
| 322 | for (auto Length : Prologue.StandardOpcodeLengths) { |
| 323 | Asm.emitInt8(Length); |
| 324 | } |
| 325 | |
| 326 | if (Version < 5) |
| 327 | writeV2IncludeAndFileTable(Prologue, Asm); |
| 328 | else |
| 329 | writeV5IncludeAndFileTable(Prologue, Asm); |
| 330 | } |
| 331 | |
| 332 | //===----------------------------------------------------------------------===// |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 333 | /// dwarfgen::Generator implementation. |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
Greg Clayton | b903283 | 2016-12-08 16:57:04 +0000 | [diff] [blame] | 336 | dwarfgen::Generator::Generator() |
| 337 | : MAB(nullptr), MCE(nullptr), MS(nullptr), StringPool(nullptr), |
| 338 | Abbreviations(Allocator) {} |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 339 | dwarfgen::Generator::~Generator() = default; |
| 340 | |
| 341 | llvm::Expected<std::unique_ptr<dwarfgen::Generator>> |
| 342 | dwarfgen::Generator::create(Triple TheTriple, uint16_t DwarfVersion) { |
| 343 | std::unique_ptr<dwarfgen::Generator> GenUP(new dwarfgen::Generator()); |
| 344 | llvm::Error error = GenUP->init(TheTriple, DwarfVersion); |
| 345 | if (error) |
| 346 | return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(error)); |
| 347 | return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(GenUP)); |
| 348 | } |
| 349 | |
| 350 | llvm::Error dwarfgen::Generator::init(Triple TheTriple, uint16_t V) { |
| 351 | Version = V; |
| 352 | std::string ErrorStr; |
| 353 | std::string TripleName; |
| 354 | |
| 355 | // Get the target. |
| 356 | const Target *TheTarget = |
| 357 | TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr); |
| 358 | if (!TheTarget) |
| 359 | return make_error<StringError>(ErrorStr, inconvertibleErrorCode()); |
| 360 | |
| 361 | TripleName = TheTriple.getTriple(); |
| 362 | |
| 363 | // Create all the MC Objects. |
| 364 | MRI.reset(TheTarget->createMCRegInfo(TripleName)); |
| 365 | if (!MRI) |
| 366 | return make_error<StringError>(Twine("no register info for target ") + |
| 367 | TripleName, |
| 368 | inconvertibleErrorCode()); |
| 369 | |
| 370 | MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 371 | if (!MAI) |
| 372 | return make_error<StringError>("no asm info for target " + TripleName, |
| 373 | inconvertibleErrorCode()); |
| 374 | |
| 375 | MOFI.reset(new MCObjectFileInfo); |
| 376 | MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get())); |
Rafael Espindola | 9f92995 | 2017-08-02 20:32:26 +0000 | [diff] [blame] | 377 | MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, *MC); |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 378 | |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 379 | MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 380 | if (!MSTI) |
| 381 | return make_error<StringError>("no subtarget info for target " + TripleName, |
| 382 | inconvertibleErrorCode()); |
| 383 | |
David Blaikie | eef5c23 | 2017-11-27 19:55:16 +0000 | [diff] [blame] | 384 | MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 385 | MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions); |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 386 | if (!MAB) |
| 387 | return make_error<StringError>("no asm backend for target " + TripleName, |
| 388 | inconvertibleErrorCode()); |
| 389 | |
| 390 | MII.reset(TheTarget->createMCInstrInfo()); |
| 391 | if (!MII) |
| 392 | return make_error<StringError>("no instr info info for target " + |
| 393 | TripleName, |
| 394 | inconvertibleErrorCode()); |
| 395 | |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 396 | MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC); |
| 397 | if (!MCE) |
| 398 | return make_error<StringError>("no code emitter for target " + TripleName, |
| 399 | inconvertibleErrorCode()); |
| 400 | |
| 401 | Stream = make_unique<raw_svector_ostream>(FileBytes); |
| 402 | |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 403 | MS = TheTarget->createMCObjectStreamer( |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame^] | 404 | TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB), |
| 405 | MAB->createObjectWriter(*Stream), std::unique_ptr<MCCodeEmitter>(MCE), |
| 406 | *MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible, |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 407 | /*DWARFMustBeAtTheEnd*/ false); |
| 408 | if (!MS) |
| 409 | return make_error<StringError>("no object streamer for target " + |
| 410 | TripleName, |
| 411 | inconvertibleErrorCode()); |
| 412 | |
| 413 | // Finally create the AsmPrinter we'll use to emit the DIEs. |
| 414 | TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(), |
| 415 | None)); |
| 416 | if (!TM) |
| 417 | return make_error<StringError>("no target machine for target " + TripleName, |
| 418 | inconvertibleErrorCode()); |
| 419 | |
| 420 | Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS))); |
| 421 | if (!Asm) |
| 422 | return make_error<StringError>("no asm printer for target " + TripleName, |
| 423 | inconvertibleErrorCode()); |
| 424 | |
| 425 | // Set the DWARF version correctly on all classes that we use. |
| 426 | MC->setDwarfVersion(Version); |
| 427 | Asm->setDwarfVersion(Version); |
| 428 | |
Benjamin Kramer | 9fcb7fe | 2016-12-09 13:12:30 +0000 | [diff] [blame] | 429 | StringPool = llvm::make_unique<DwarfStringPool>(Allocator, *Asm, StringRef()); |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 430 | |
| 431 | return Error::success(); |
| 432 | } |
| 433 | |
| 434 | StringRef dwarfgen::Generator::generate() { |
| 435 | // Offset from the first CU in the debug info section is 0 initially. |
| 436 | unsigned SecOffset = 0; |
| 437 | |
| 438 | // Iterate over each compile unit and set the size and offsets for each |
| 439 | // DIE within each compile unit. All offsets are CU relative. |
| 440 | for (auto &CU : CompileUnits) { |
| 441 | // Set the absolute .debug_info offset for this compile unit. |
| 442 | CU->setOffset(SecOffset); |
| 443 | // The DIEs contain compile unit relative offsets. |
| 444 | unsigned CUOffset = 11; |
| 445 | CUOffset = CU->getUnitDIE().computeSizeAndOffsets(CUOffset); |
| 446 | // Update our absolute .debug_info offset. |
| 447 | SecOffset += CUOffset; |
| 448 | CU->setLength(CUOffset - 4); |
| 449 | } |
| 450 | Abbreviations.Emit(Asm.get(), MOFI->getDwarfAbbrevSection()); |
| 451 | StringPool->emit(*Asm, MOFI->getDwarfStrSection()); |
| 452 | MS->SwitchSection(MOFI->getDwarfInfoSection()); |
| 453 | for (auto &CU : CompileUnits) { |
| 454 | uint16_t Version = CU->getVersion(); |
| 455 | auto Length = CU->getLength(); |
| 456 | MC->setDwarfVersion(Version); |
| 457 | assert(Length != -1U); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 458 | Asm->emitInt32(Length); |
| 459 | Asm->emitInt16(Version); |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 460 | if (Version <= 4) { |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 461 | Asm->emitInt32(0); |
| 462 | Asm->emitInt8(CU->getAddressSize()); |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 463 | } else { |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 464 | Asm->emitInt8(dwarf::DW_UT_compile); |
| 465 | Asm->emitInt8(CU->getAddressSize()); |
| 466 | Asm->emitInt32(0); |
Paul Robinson | cddd604 | 2017-02-28 20:24:55 +0000 | [diff] [blame] | 467 | } |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 468 | Asm->emitDwarfDIE(*CU->getUnitDIE().Die); |
| 469 | } |
| 470 | |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 471 | MS->SwitchSection(MOFI->getDwarfLineSection()); |
| 472 | for (auto < : LineTables) |
| 473 | LT->generate(*MC, *Asm); |
| 474 | |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 475 | MS->Finish(); |
| 476 | if (FileBytes.empty()) |
| 477 | return StringRef(); |
| 478 | return StringRef(FileBytes.data(), FileBytes.size()); |
| 479 | } |
| 480 | |
| 481 | bool dwarfgen::Generator::saveFile(StringRef Path) { |
| 482 | if (FileBytes.empty()) |
| 483 | return false; |
| 484 | std::error_code EC; |
| 485 | raw_fd_ostream Strm(Path, EC, sys::fs::F_None); |
| 486 | if (EC) |
| 487 | return false; |
| 488 | Strm.write(FileBytes.data(), FileBytes.size()); |
| 489 | Strm.close(); |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | dwarfgen::CompileUnit &dwarfgen::Generator::addCompileUnit() { |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 494 | CompileUnits.push_back( |
| 495 | make_unique<CompileUnit>(*this, Version, Asm->getPointerSize())); |
Greg Clayton | 3462a42 | 2016-12-08 01:03:48 +0000 | [diff] [blame] | 496 | return *CompileUnits.back(); |
| 497 | } |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 498 | |
| 499 | dwarfgen::LineTable &dwarfgen::Generator::addLineTable(DwarfFormat Format) { |
| 500 | LineTables.push_back( |
James Henderson | 198a87c | 2018-05-10 14:36:24 +0000 | [diff] [blame] | 501 | make_unique<LineTable>(Version, Format, Asm->getPointerSize())); |
James Henderson | a3acf99 | 2018-05-10 10:51:33 +0000 | [diff] [blame] | 502 | return *LineTables.back(); |
| 503 | } |