DWARFDebugLoc.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/DWARF/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.append(str.begin(), str.end());
  64. Loc.Entries.push_back(std::move(E));
  65. }
  66. }
  67. if (data.isValidOffset(Offset))
  68. llvm::errs() << "error: failed to consume entire .debug_loc section\n";
  69. }
  70. void DWARFDebugLocDWO::parse(DataExtractor data) {
  71. uint32_t Offset = 0;
  72. while (data.isValidOffset(Offset)) {
  73. Locations.resize(Locations.size() + 1);
  74. LocationList &Loc = Locations.back();
  75. Loc.Offset = Offset;
  76. dwarf::LocationListEntry Kind;
  77. while ((Kind = static_cast<dwarf::LocationListEntry>(
  78. data.getU8(&Offset))) != dwarf::DW_LLE_end_of_list_entry) {
  79. if (Kind != dwarf::DW_LLE_start_length_entry) {
  80. llvm::errs() << "error: dumping support for LLE of kind " << (int)Kind
  81. << " not implemented\n";
  82. return;
  83. }
  84. Entry E;
  85. E.Start = data.getULEB128(&Offset);
  86. E.Length = data.getU32(&Offset);
  87. unsigned Bytes = data.getU16(&Offset);
  88. // A single location description describing the location of the object...
  89. StringRef str = data.getData().substr(Offset, Bytes);
  90. Offset += Bytes;
  91. E.Loc.resize(str.size());
  92. std::copy(str.begin(), str.end(), E.Loc.begin());
  93. Loc.Entries.push_back(std::move(E));
  94. }
  95. }
  96. }
  97. void DWARFDebugLocDWO::dump(raw_ostream &OS) const {
  98. for (const LocationList &L : Locations) {
  99. OS << format("0x%8.8x: ", L.Offset);
  100. const unsigned Indent = 12;
  101. for (const Entry &E : L.Entries) {
  102. if (&E != L.Entries.begin())
  103. OS.indent(Indent);
  104. OS << "Beginning address index: " << E.Start << '\n';
  105. OS.indent(Indent) << " Length: " << E.Length << '\n';
  106. OS.indent(Indent) << " Location description: ";
  107. for (unsigned char Loc : E.Loc)
  108. OS << format("%2.2x ", Loc);
  109. OS << "\n\n";
  110. }
  111. }
  112. }