DWARFDebugLoc.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===-- DWARFDebugLoc.cpp -------------------------------------------------===//
  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/DWARFDebugLoc.h"
  10. #include "llvm/Support/Compiler.h"
  11. #include "llvm/Support/Dwarf.h"
  12. #include "llvm/Support/Format.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. using namespace llvm;
  15. void DWARFDebugLoc::dump(raw_ostream &OS) const {
  16. for (const LocationList &L : Locations) {
  17. OS << format("0x%8.8x: ", L.Offset);
  18. const unsigned Indent = 12;
  19. for (const Entry &E : L.Entries) {
  20. if (&E != L.Entries.begin())
  21. OS.indent(Indent);
  22. OS << "Beginning address offset: " << format("0x%016" PRIx64, E.Begin)
  23. << '\n';
  24. OS.indent(Indent) << " Ending address offset: "
  25. << format("0x%016" PRIx64, E.End) << '\n';
  26. OS.indent(Indent) << " Location description: ";
  27. for (unsigned char Loc : E.Loc) {
  28. OS << format("%2.2x ", Loc);
  29. }
  30. OS << "\n\n";
  31. }
  32. }
  33. }
  34. void DWARFDebugLoc::parse(DataExtractor data, unsigned AddressSize) {
  35. uint32_t Offset = 0;
  36. while (data.isValidOffset(Offset+AddressSize-1)) {
  37. Locations.resize(Locations.size() + 1);
  38. LocationList &Loc = Locations.back();
  39. Loc.Offset = Offset;
  40. // 2.6.2 Location Lists
  41. // A location list entry consists of:
  42. while (true) {
  43. Entry E;
  44. RelocAddrMap::const_iterator AI = RelocMap.find(Offset);
  45. // 1. A beginning address offset. ...
  46. E.Begin = data.getUnsigned(&Offset, AddressSize);
  47. if (AI != RelocMap.end())
  48. E.Begin += AI->second.second;
  49. AI = RelocMap.find(Offset);
  50. // 2. An ending address offset. ...
  51. E.End = data.getUnsigned(&Offset, AddressSize);
  52. if (AI != RelocMap.end())
  53. E.End += AI->second.second;
  54. // The end of any given location list is marked by an end of list entry,
  55. // which consists of a 0 for the beginning address offset and a 0 for the
  56. // ending address offset.
  57. if (E.Begin == 0 && E.End == 0)
  58. break;
  59. unsigned Bytes = data.getU16(&Offset);
  60. // A single location description describing the location of the object...
  61. StringRef str = data.getData().substr(Offset, Bytes);
  62. Offset += Bytes;
  63. E.Loc.reserve(str.size());
  64. std::copy(str.begin(), str.end(), std::back_inserter(E.Loc));
  65. Loc.Entries.push_back(std::move(E));
  66. }
  67. }
  68. if (data.isValidOffset(Offset))
  69. llvm::errs() << "error: failed to consume entire .debug_loc section\n";
  70. }
  71. void DWARFDebugLocDWO::parse(DataExtractor data) {
  72. uint32_t Offset = 0;
  73. while (data.isValidOffset(Offset)) {
  74. Locations.resize(Locations.size() + 1);
  75. LocationList &Loc = Locations.back();
  76. Loc.Offset = Offset;
  77. dwarf::LocationListEntry Kind;
  78. while ((Kind = static_cast<dwarf::LocationListEntry>(
  79. data.getU8(&Offset))) != dwarf::DW_LLE_end_of_list_entry) {
  80. if (Kind != dwarf::DW_LLE_start_length_entry) {
  81. llvm::errs() << "error: dumping support for LLE of kind " << (int)Kind
  82. << " not implemented\n";
  83. return;
  84. }
  85. Entry E;
  86. E.Start = data.getULEB128(&Offset);
  87. E.Length = data.getU32(&Offset);
  88. unsigned Bytes = data.getU16(&Offset);
  89. // A single location description describing the location of the object...
  90. StringRef str = data.getData().substr(Offset, Bytes);
  91. Offset += Bytes;
  92. E.Loc.resize(str.size());
  93. std::copy(str.begin(), str.end(), E.Loc.begin());
  94. Loc.Entries.push_back(std::move(E));
  95. }
  96. }
  97. }
  98. void DWARFDebugLocDWO::dump(raw_ostream &OS) const {
  99. for (const LocationList &L : Locations) {
  100. OS << format("0x%8.8x: ", L.Offset);
  101. const unsigned Indent = 12;
  102. for (const Entry &E : L.Entries) {
  103. if (&E != L.Entries.begin())
  104. OS.indent(Indent);
  105. OS << "Beginning address index: " << E.Start << '\n';
  106. OS.indent(Indent) << " Length: " << E.Length << '\n';
  107. OS.indent(Indent) << " Location description: ";
  108. for (unsigned char Loc : E.Loc)
  109. OS << format("%2.2x ", Loc);
  110. OS << "\n\n";
  111. }
  112. }
  113. }