DWARFDebugFrame.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. //===-- DWARFDebugFrame.h - Parsing of .debug_frame -------------*- 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. #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/Support/Casting.h"
  14. #include "llvm/Support/DataTypes.h"
  15. #include "llvm/Support/Dwarf.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <string>
  20. #include <vector>
  21. using namespace llvm;
  22. using namespace dwarf;
  23. /// \brief Abstract frame entry defining the common interface concrete
  24. /// entries implement.
  25. class llvm::FrameEntry {
  26. public:
  27. enum FrameKind {FK_CIE, FK_FDE};
  28. FrameEntry(FrameKind K, uint64_t Offset, uint64_t Length)
  29. : Kind(K), Offset(Offset), Length(Length) {}
  30. virtual ~FrameEntry() {
  31. }
  32. FrameKind getKind() const { return Kind; }
  33. virtual uint64_t getOffset() const { return Offset; }
  34. /// \brief Parse and store a sequence of CFI instructions from Data,
  35. /// starting at *Offset and ending at EndOffset. If everything
  36. /// goes well, *Offset should be equal to EndOffset when this method
  37. /// returns. Otherwise, an error occurred.
  38. virtual void parseInstructions(DataExtractor Data, uint32_t *Offset,
  39. uint32_t EndOffset);
  40. /// \brief Dump the entry header to the given output stream.
  41. virtual void dumpHeader(raw_ostream &OS) const = 0;
  42. /// \brief Dump the entry's instructions to the given output stream.
  43. virtual void dumpInstructions(raw_ostream &OS) const;
  44. protected:
  45. const FrameKind Kind;
  46. /// \brief Offset of this entry in the section.
  47. uint64_t Offset;
  48. /// \brief Entry length as specified in DWARF.
  49. uint64_t Length;
  50. /// An entry may contain CFI instructions. An instruction consists of an
  51. /// opcode and an optional sequence of operands.
  52. typedef std::vector<uint64_t> Operands;
  53. struct Instruction {
  54. Instruction(uint8_t Opcode)
  55. : Opcode(Opcode)
  56. {}
  57. uint8_t Opcode;
  58. Operands Ops;
  59. };
  60. std::vector<Instruction> Instructions;
  61. /// Convenience methods to add a new instruction with the given opcode and
  62. /// operands to the Instructions vector.
  63. void addInstruction(uint8_t Opcode) {
  64. Instructions.push_back(Instruction(Opcode));
  65. }
  66. void addInstruction(uint8_t Opcode, uint64_t Operand1) {
  67. Instructions.push_back(Instruction(Opcode));
  68. Instructions.back().Ops.push_back(Operand1);
  69. }
  70. void addInstruction(uint8_t Opcode, uint64_t Operand1, uint64_t Operand2) {
  71. Instructions.push_back(Instruction(Opcode));
  72. Instructions.back().Ops.push_back(Operand1);
  73. Instructions.back().Ops.push_back(Operand2);
  74. }
  75. };
  76. // See DWARF standard v3, section 7.23
  77. const uint8_t DWARF_CFI_PRIMARY_OPCODE_MASK = 0xc0;
  78. const uint8_t DWARF_CFI_PRIMARY_OPERAND_MASK = 0x3f;
  79. void FrameEntry::parseInstructions(DataExtractor Data, uint32_t *Offset,
  80. uint32_t EndOffset) {
  81. while (*Offset < EndOffset) {
  82. uint8_t Opcode = Data.getU8(Offset);
  83. // Some instructions have a primary opcode encoded in the top bits.
  84. uint8_t Primary = Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK;
  85. if (Primary) {
  86. // If it's a primary opcode, the first operand is encoded in the bottom
  87. // bits of the opcode itself.
  88. uint64_t Op1 = Opcode & DWARF_CFI_PRIMARY_OPERAND_MASK;
  89. switch (Primary) {
  90. default: llvm_unreachable("Impossible primary CFI opcode");
  91. case DW_CFA_advance_loc:
  92. case DW_CFA_restore:
  93. addInstruction(Primary, Op1);
  94. break;
  95. case DW_CFA_offset:
  96. addInstruction(Primary, Op1, Data.getULEB128(Offset));
  97. break;
  98. }
  99. } else {
  100. // Extended opcode - its value is Opcode itself.
  101. switch (Opcode) {
  102. default: llvm_unreachable("Invalid extended CFI opcode");
  103. case DW_CFA_nop:
  104. case DW_CFA_remember_state:
  105. case DW_CFA_restore_state:
  106. case DW_CFA_GNU_window_save:
  107. // No operands
  108. addInstruction(Opcode);
  109. break;
  110. case DW_CFA_set_loc:
  111. // Operands: Address
  112. addInstruction(Opcode, Data.getAddress(Offset));
  113. break;
  114. case DW_CFA_advance_loc1:
  115. // Operands: 1-byte delta
  116. addInstruction(Opcode, Data.getU8(Offset));
  117. break;
  118. case DW_CFA_advance_loc2:
  119. // Operands: 2-byte delta
  120. addInstruction(Opcode, Data.getU16(Offset));
  121. break;
  122. case DW_CFA_advance_loc4:
  123. // Operands: 4-byte delta
  124. addInstruction(Opcode, Data.getU32(Offset));
  125. break;
  126. case DW_CFA_restore_extended:
  127. case DW_CFA_undefined:
  128. case DW_CFA_same_value:
  129. case DW_CFA_def_cfa_register:
  130. case DW_CFA_def_cfa_offset:
  131. // Operands: ULEB128
  132. addInstruction(Opcode, Data.getULEB128(Offset));
  133. break;
  134. case DW_CFA_def_cfa_offset_sf:
  135. // Operands: SLEB128
  136. addInstruction(Opcode, Data.getSLEB128(Offset));
  137. break;
  138. case DW_CFA_offset_extended:
  139. case DW_CFA_register:
  140. case DW_CFA_def_cfa:
  141. case DW_CFA_val_offset:
  142. // Operands: ULEB128, ULEB128
  143. addInstruction(Opcode, Data.getULEB128(Offset),
  144. Data.getULEB128(Offset));
  145. break;
  146. case DW_CFA_offset_extended_sf:
  147. case DW_CFA_def_cfa_sf:
  148. case DW_CFA_val_offset_sf:
  149. // Operands: ULEB128, SLEB128
  150. addInstruction(Opcode, Data.getULEB128(Offset),
  151. Data.getSLEB128(Offset));
  152. break;
  153. case DW_CFA_def_cfa_expression:
  154. case DW_CFA_expression:
  155. case DW_CFA_val_expression:
  156. // TODO: implement this
  157. report_fatal_error("Values with expressions not implemented yet!");
  158. }
  159. }
  160. }
  161. }
  162. namespace {
  163. /// \brief DWARF Common Information Entry (CIE)
  164. class CIE : public FrameEntry {
  165. public:
  166. // CIEs (and FDEs) are simply container classes, so the only sensible way to
  167. // create them is by providing the full parsed contents in the constructor.
  168. CIE(uint64_t Offset, uint64_t Length, uint8_t Version,
  169. SmallString<8> Augmentation, uint8_t AddressSize,
  170. uint8_t SegmentDescriptorSize, uint64_t CodeAlignmentFactor,
  171. int64_t DataAlignmentFactor, uint64_t ReturnAddressRegister)
  172. : FrameEntry(FK_CIE, Offset, Length), Version(Version),
  173. Augmentation(std::move(Augmentation)),
  174. AddressSize(AddressSize),
  175. SegmentDescriptorSize(SegmentDescriptorSize),
  176. CodeAlignmentFactor(CodeAlignmentFactor),
  177. DataAlignmentFactor(DataAlignmentFactor),
  178. ReturnAddressRegister(ReturnAddressRegister) {}
  179. ~CIE() override {}
  180. uint64_t getCodeAlignmentFactor() const { return CodeAlignmentFactor; }
  181. int64_t getDataAlignmentFactor() const { return DataAlignmentFactor; }
  182. void dumpHeader(raw_ostream &OS) const override {
  183. OS << format("%08x %08x %08x CIE",
  184. (uint32_t)Offset, (uint32_t)Length, DW_CIE_ID)
  185. << "\n";
  186. OS << format(" Version: %d\n", Version);
  187. OS << " Augmentation: \"" << Augmentation << "\"\n";
  188. if (Version >= 4) {
  189. OS << format(" Address size: %u\n",
  190. (uint32_t)AddressSize);
  191. OS << format(" Segment desc size: %u\n",
  192. (uint32_t)SegmentDescriptorSize);
  193. }
  194. OS << format(" Code alignment factor: %u\n",
  195. (uint32_t)CodeAlignmentFactor);
  196. OS << format(" Data alignment factor: %d\n",
  197. (int32_t)DataAlignmentFactor);
  198. OS << format(" Return address column: %d\n",
  199. (int32_t)ReturnAddressRegister);
  200. OS << "\n";
  201. }
  202. static bool classof(const FrameEntry *FE) {
  203. return FE->getKind() == FK_CIE;
  204. }
  205. private:
  206. /// The following fields are defined in section 6.4.1 of the DWARF standard v4
  207. uint8_t Version;
  208. SmallString<8> Augmentation;
  209. uint8_t AddressSize;
  210. uint8_t SegmentDescriptorSize;
  211. uint64_t CodeAlignmentFactor;
  212. int64_t DataAlignmentFactor;
  213. uint64_t ReturnAddressRegister;
  214. };
  215. /// \brief DWARF Frame Description Entry (FDE)
  216. class FDE : public FrameEntry {
  217. public:
  218. // Each FDE has a CIE it's "linked to". Our FDE contains is constructed with
  219. // an offset to the CIE (provided by parsing the FDE header). The CIE itself
  220. // is obtained lazily once it's actually required.
  221. FDE(uint64_t Offset, uint64_t Length, int64_t LinkedCIEOffset,
  222. uint64_t InitialLocation, uint64_t AddressRange,
  223. CIE *Cie)
  224. : FrameEntry(FK_FDE, Offset, Length), LinkedCIEOffset(LinkedCIEOffset),
  225. InitialLocation(InitialLocation), AddressRange(AddressRange),
  226. LinkedCIE(Cie) {}
  227. ~FDE() override {}
  228. CIE *getLinkedCIE() const { return LinkedCIE; }
  229. void dumpHeader(raw_ostream &OS) const override {
  230. OS << format("%08x %08x %08x FDE ",
  231. (uint32_t)Offset, (uint32_t)Length, (int32_t)LinkedCIEOffset);
  232. OS << format("cie=%08x pc=%08x...%08x\n",
  233. (int32_t)LinkedCIEOffset,
  234. (uint32_t)InitialLocation,
  235. (uint32_t)InitialLocation + (uint32_t)AddressRange);
  236. }
  237. static bool classof(const FrameEntry *FE) {
  238. return FE->getKind() == FK_FDE;
  239. }
  240. private:
  241. /// The following fields are defined in section 6.4.1 of the DWARF standard v3
  242. uint64_t LinkedCIEOffset;
  243. uint64_t InitialLocation;
  244. uint64_t AddressRange;
  245. CIE *LinkedCIE;
  246. };
  247. /// \brief Types of operands to CF instructions.
  248. enum OperandType {
  249. OT_Unset,
  250. OT_None,
  251. OT_Address,
  252. OT_Offset,
  253. OT_FactoredCodeOffset,
  254. OT_SignedFactDataOffset,
  255. OT_UnsignedFactDataOffset,
  256. OT_Register,
  257. OT_Expression
  258. };
  259. } // end anonymous namespace
  260. /// \brief Initialize the array describing the types of operands.
  261. static ArrayRef<OperandType[2]> getOperandTypes() {
  262. static OperandType OpTypes[DW_CFA_restore+1][2];
  263. #define DECLARE_OP2(OP, OPTYPE0, OPTYPE1) \
  264. do { \
  265. OpTypes[OP][0] = OPTYPE0; \
  266. OpTypes[OP][1] = OPTYPE1; \
  267. } while (0)
  268. #define DECLARE_OP1(OP, OPTYPE0) DECLARE_OP2(OP, OPTYPE0, OT_None)
  269. #define DECLARE_OP0(OP) DECLARE_OP1(OP, OT_None)
  270. DECLARE_OP1(DW_CFA_set_loc, OT_Address);
  271. DECLARE_OP1(DW_CFA_advance_loc, OT_FactoredCodeOffset);
  272. DECLARE_OP1(DW_CFA_advance_loc1, OT_FactoredCodeOffset);
  273. DECLARE_OP1(DW_CFA_advance_loc2, OT_FactoredCodeOffset);
  274. DECLARE_OP1(DW_CFA_advance_loc4, OT_FactoredCodeOffset);
  275. DECLARE_OP1(DW_CFA_MIPS_advance_loc8, OT_FactoredCodeOffset);
  276. DECLARE_OP2(DW_CFA_def_cfa, OT_Register, OT_Offset);
  277. DECLARE_OP2(DW_CFA_def_cfa_sf, OT_Register, OT_SignedFactDataOffset);
  278. DECLARE_OP1(DW_CFA_def_cfa_register, OT_Register);
  279. DECLARE_OP1(DW_CFA_def_cfa_offset, OT_Offset);
  280. DECLARE_OP1(DW_CFA_def_cfa_offset_sf, OT_SignedFactDataOffset);
  281. DECLARE_OP1(DW_CFA_def_cfa_expression, OT_Expression);
  282. DECLARE_OP1(DW_CFA_undefined, OT_Register);
  283. DECLARE_OP1(DW_CFA_same_value, OT_Register);
  284. DECLARE_OP2(DW_CFA_offset, OT_Register, OT_UnsignedFactDataOffset);
  285. DECLARE_OP2(DW_CFA_offset_extended, OT_Register, OT_UnsignedFactDataOffset);
  286. DECLARE_OP2(DW_CFA_offset_extended_sf, OT_Register, OT_SignedFactDataOffset);
  287. DECLARE_OP2(DW_CFA_val_offset, OT_Register, OT_UnsignedFactDataOffset);
  288. DECLARE_OP2(DW_CFA_val_offset_sf, OT_Register, OT_SignedFactDataOffset);
  289. DECLARE_OP2(DW_CFA_register, OT_Register, OT_Register);
  290. DECLARE_OP2(DW_CFA_expression, OT_Register, OT_Expression);
  291. DECLARE_OP2(DW_CFA_val_expression, OT_Register, OT_Expression);
  292. DECLARE_OP1(DW_CFA_restore, OT_Register);
  293. DECLARE_OP1(DW_CFA_restore_extended, OT_Register);
  294. DECLARE_OP0(DW_CFA_remember_state);
  295. DECLARE_OP0(DW_CFA_restore_state);
  296. DECLARE_OP0(DW_CFA_GNU_window_save);
  297. DECLARE_OP1(DW_CFA_GNU_args_size, OT_Offset);
  298. DECLARE_OP0(DW_CFA_nop);
  299. #undef DECLARE_OP0
  300. #undef DECLARE_OP1
  301. #undef DECLARE_OP2
  302. return ArrayRef<OperandType[2]>(&OpTypes[0], DW_CFA_restore+1);
  303. }
  304. static ArrayRef<OperandType[2]> OpTypes = getOperandTypes();
  305. /// \brief Print \p Opcode's operand number \p OperandIdx which has
  306. /// value \p Operand.
  307. static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx,
  308. uint64_t Operand, uint64_t CodeAlignmentFactor,
  309. int64_t DataAlignmentFactor) {
  310. assert(OperandIdx < 2);
  311. OperandType Type = OpTypes[Opcode][OperandIdx];
  312. switch (Type) {
  313. case OT_Unset:
  314. OS << " Unsupported " << (OperandIdx ? "second" : "first") << " operand to";
  315. if (const char *OpcodeName = CallFrameString(Opcode))
  316. OS << " " << OpcodeName;
  317. else
  318. OS << format(" Opcode %x", Opcode);
  319. break;
  320. case OT_None:
  321. break;
  322. case OT_Address:
  323. OS << format(" %" PRIx64, Operand);
  324. break;
  325. case OT_Offset:
  326. // The offsets are all encoded in a unsigned form, but in practice
  327. // consumers use them signed. It's most certainly legacy due to
  328. // the lack of signed variants in the first Dwarf standards.
  329. OS << format(" %+" PRId64, int64_t(Operand));
  330. break;
  331. case OT_FactoredCodeOffset: // Always Unsigned
  332. if (CodeAlignmentFactor)
  333. OS << format(" %" PRId64, Operand * CodeAlignmentFactor);
  334. else
  335. OS << format(" %" PRId64 "*code_alignment_factor" , Operand);
  336. break;
  337. case OT_SignedFactDataOffset:
  338. if (DataAlignmentFactor)
  339. OS << format(" %" PRId64, int64_t(Operand) * DataAlignmentFactor);
  340. else
  341. OS << format(" %" PRId64 "*data_alignment_factor" , int64_t(Operand));
  342. break;
  343. case OT_UnsignedFactDataOffset:
  344. if (DataAlignmentFactor)
  345. OS << format(" %" PRId64, Operand * DataAlignmentFactor);
  346. else
  347. OS << format(" %" PRId64 "*data_alignment_factor" , Operand);
  348. break;
  349. case OT_Register:
  350. OS << format(" reg%" PRId64, Operand);
  351. break;
  352. case OT_Expression:
  353. OS << " expression";
  354. break;
  355. }
  356. }
  357. void FrameEntry::dumpInstructions(raw_ostream &OS) const {
  358. uint64_t CodeAlignmentFactor = 0;
  359. int64_t DataAlignmentFactor = 0;
  360. const CIE *Cie = dyn_cast<CIE>(this);
  361. if (!Cie)
  362. Cie = cast<FDE>(this)->getLinkedCIE();
  363. if (Cie) {
  364. CodeAlignmentFactor = Cie->getCodeAlignmentFactor();
  365. DataAlignmentFactor = Cie->getDataAlignmentFactor();
  366. }
  367. for (const auto &Instr : Instructions) {
  368. uint8_t Opcode = Instr.Opcode;
  369. if (Opcode & DWARF_CFI_PRIMARY_OPCODE_MASK)
  370. Opcode &= DWARF_CFI_PRIMARY_OPCODE_MASK;
  371. OS << " " << CallFrameString(Opcode) << ":";
  372. for (unsigned i = 0; i < Instr.Ops.size(); ++i)
  373. printOperand(OS, Opcode, i, Instr.Ops[i], CodeAlignmentFactor,
  374. DataAlignmentFactor);
  375. OS << '\n';
  376. }
  377. }
  378. DWARFDebugFrame::DWARFDebugFrame() {
  379. }
  380. DWARFDebugFrame::~DWARFDebugFrame() {
  381. }
  382. static void LLVM_ATTRIBUTE_UNUSED dumpDataAux(DataExtractor Data,
  383. uint32_t Offset, int Length) {
  384. errs() << "DUMP: ";
  385. for (int i = 0; i < Length; ++i) {
  386. uint8_t c = Data.getU8(&Offset);
  387. errs().write_hex(c); errs() << " ";
  388. }
  389. errs() << "\n";
  390. }
  391. void DWARFDebugFrame::parse(DataExtractor Data) {
  392. uint32_t Offset = 0;
  393. DenseMap<uint32_t, CIE *> CIEs;
  394. while (Data.isValidOffset(Offset)) {
  395. uint32_t StartOffset = Offset;
  396. bool IsDWARF64 = false;
  397. uint64_t Length = Data.getU32(&Offset);
  398. uint64_t Id;
  399. if (Length == UINT32_MAX) {
  400. // DWARF-64 is distinguished by the first 32 bits of the initial length
  401. // field being 0xffffffff. Then, the next 64 bits are the actual entry
  402. // length.
  403. IsDWARF64 = true;
  404. Length = Data.getU64(&Offset);
  405. }
  406. // At this point, Offset points to the next field after Length.
  407. // Length is the structure size excluding itself. Compute an offset one
  408. // past the end of the structure (needed to know how many instructions to
  409. // read).
  410. // TODO: For honest DWARF64 support, DataExtractor will have to treat
  411. // offset_ptr as uint64_t*
  412. uint32_t EndStructureOffset = Offset + static_cast<uint32_t>(Length);
  413. // The Id field's size depends on the DWARF format
  414. Id = Data.getUnsigned(&Offset, IsDWARF64 ? 8 : 4);
  415. bool IsCIE = ((IsDWARF64 && Id == DW64_CIE_ID) || Id == DW_CIE_ID);
  416. if (IsCIE) {
  417. uint8_t Version = Data.getU8(&Offset);
  418. const char *Augmentation = Data.getCStr(&Offset);
  419. uint8_t AddressSize = Version < 4 ? Data.getAddressSize() : Data.getU8(&Offset);
  420. Data.setAddressSize(AddressSize);
  421. uint8_t SegmentDescriptorSize = Version < 4 ? 0 : Data.getU8(&Offset);
  422. uint64_t CodeAlignmentFactor = Data.getULEB128(&Offset);
  423. int64_t DataAlignmentFactor = Data.getSLEB128(&Offset);
  424. uint64_t ReturnAddressRegister = Data.getULEB128(&Offset);
  425. auto Cie = make_unique<CIE>(StartOffset, Length, Version,
  426. StringRef(Augmentation), AddressSize,
  427. SegmentDescriptorSize, CodeAlignmentFactor,
  428. DataAlignmentFactor, ReturnAddressRegister);
  429. CIEs[StartOffset] = Cie.get();
  430. Entries.emplace_back(std::move(Cie));
  431. } else {
  432. // FDE
  433. uint64_t CIEPointer = Id;
  434. uint64_t InitialLocation = Data.getAddress(&Offset);
  435. uint64_t AddressRange = Data.getAddress(&Offset);
  436. Entries.emplace_back(new FDE(StartOffset, Length, CIEPointer,
  437. InitialLocation, AddressRange,
  438. CIEs[CIEPointer]));
  439. }
  440. Entries.back()->parseInstructions(Data, &Offset, EndStructureOffset);
  441. if (Offset != EndStructureOffset) {
  442. std::string Str;
  443. raw_string_ostream OS(Str);
  444. OS << format("Parsing entry instructions at %lx failed", StartOffset);
  445. report_fatal_error(Str);
  446. }
  447. }
  448. }
  449. void DWARFDebugFrame::dump(raw_ostream &OS) const {
  450. OS << "\n";
  451. for (const auto &Entry : Entries) {
  452. Entry->dumpHeader(OS);
  453. Entry->dumpInstructions(OS);
  454. OS << "\n";
  455. }
  456. }