Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 1 | //===- DWARFDebugLoc.cpp --------------------------------------------------===// |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +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 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 12 | #include "llvm/BinaryFormat/Dwarf.h" |
George Rimar | f8a9642 | 2017-04-21 09:12:18 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/DWARF/DWARFExpression.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFUnit.h" |
| 17 | #include "llvm/Support/Compiler.h" |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 19 | #include "llvm/Support/WithColor.h" |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 28db7e6 | 2017-03-01 01:14:23 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <cinttypes> |
| 23 | #include <cstdint> |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 27 | // When directly dumping the .debug_loc without a compile unit, we have to guess |
| 28 | // at the DWARF version. This only affects DW_OP_call_ref, which is a rare |
| 29 | // expression that LLVM doesn't produce. Guessing the wrong version means we |
| 30 | // won't be able to pretty print expressions in DWARF2 binaries produced by |
| 31 | // non-LLVM tools. |
| 32 | static void dumpExpression(raw_ostream &OS, ArrayRef<char> Data, |
| 33 | bool IsLittleEndian, unsigned AddressSize, |
| 34 | const MCRegisterInfo *MRI) { |
| 35 | DWARFDataExtractor Extractor(StringRef(Data.data(), Data.size()), |
| 36 | IsLittleEndian, AddressSize); |
Pavel Labath | 54ca2d6 | 2018-04-06 08:49:57 +0000 | [diff] [blame] | 37 | DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI); |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, bool IsLittleEndian, |
| 41 | unsigned AddressSize, |
| 42 | const MCRegisterInfo *MRI, |
Jonas Devlieghere | c111382 | 2018-05-21 19:36:54 +0000 | [diff] [blame^] | 43 | uint64_t BaseAddress, |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 44 | unsigned Indent) const { |
| 45 | for (const Entry &E : Entries) { |
| 46 | OS << '\n'; |
| 47 | OS.indent(Indent); |
Jonas Devlieghere | 6f24c87 | 2018-01-16 11:17:57 +0000 | [diff] [blame] | 48 | OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2, |
Jonas Devlieghere | c111382 | 2018-05-21 19:36:54 +0000 | [diff] [blame^] | 49 | BaseAddress + E.Begin); |
| 50 | OS << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2, |
| 51 | BaseAddress + E.End); |
Jonas Devlieghere | 6f24c87 | 2018-01-16 11:17:57 +0000 | [diff] [blame] | 52 | OS << ": "; |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 53 | |
| 54 | dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI); |
| 55 | } |
| 56 | } |
| 57 | |
Jonas Devlieghere | 622c563 | 2017-09-27 09:33:36 +0000 | [diff] [blame] | 58 | DWARFDebugLoc::LocationList const * |
| 59 | DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const { |
| 60 | auto It = std::lower_bound( |
| 61 | Locations.begin(), Locations.end(), Offset, |
| 62 | [](const LocationList &L, uint64_t Offset) { return L.Offset < Offset; }); |
| 63 | if (It != Locations.end() && It->Offset == Offset) |
| 64 | return &(*It); |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI, |
| 69 | Optional<uint64_t> Offset) const { |
| 70 | auto DumpLocationList = [&](const LocationList &L) { |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 71 | OS << format("0x%8.8x: ", L.Offset); |
Jonas Devlieghere | c111382 | 2018-05-21 19:36:54 +0000 | [diff] [blame^] | 72 | L.dump(OS, IsLittleEndian, AddressSize, MRI, 0, 12); |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 73 | OS << "\n\n"; |
Jonas Devlieghere | 622c563 | 2017-09-27 09:33:36 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | if (Offset) { |
| 77 | if (auto *L = getLocationListAtOffset(*Offset)) |
| 78 | DumpLocationList(*L); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | for (const LocationList &L : Locations) { |
| 83 | DumpLocationList(L); |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | Optional<DWARFDebugLoc::LocationList> |
| 88 | DWARFDebugLoc::parseOneLocationList(DWARFDataExtractor Data, unsigned *Offset) { |
| 89 | LocationList LL; |
| 90 | LL.Offset = *Offset; |
| 91 | |
| 92 | // 2.6.2 Location Lists |
| 93 | // A location list entry consists of: |
| 94 | while (true) { |
| 95 | Entry E; |
| 96 | if (!Data.isValidOffsetForDataOfSize(*Offset, 2 * Data.getAddressSize())) { |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 97 | WithColor::error() << "location list overflows the debug_loc section.\n"; |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 98 | return None; |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 99 | } |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 100 | |
| 101 | // 1. A beginning address offset. ... |
| 102 | E.Begin = Data.getRelocatedAddress(Offset); |
| 103 | |
| 104 | // 2. An ending address offset. ... |
| 105 | E.End = Data.getRelocatedAddress(Offset); |
| 106 | |
| 107 | // The end of any given location list is marked by an end of list entry, |
| 108 | // which consists of a 0 for the beginning address offset and a 0 for the |
| 109 | // ending address offset. |
| 110 | if (E.Begin == 0 && E.End == 0) |
| 111 | return LL; |
| 112 | |
| 113 | if (!Data.isValidOffsetForDataOfSize(*Offset, 2)) { |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 114 | WithColor::error() << "location list overflows the debug_loc section.\n"; |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 115 | return None; |
| 116 | } |
| 117 | |
| 118 | unsigned Bytes = Data.getU16(Offset); |
| 119 | if (!Data.isValidOffsetForDataOfSize(*Offset, Bytes)) { |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 120 | WithColor::error() << "location list overflows the debug_loc section.\n"; |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 121 | return None; |
| 122 | } |
| 123 | // A single location description describing the location of the object... |
| 124 | StringRef str = Data.getData().substr(*Offset, Bytes); |
| 125 | *Offset += Bytes; |
| 126 | E.Loc.reserve(str.size()); |
| 127 | std::copy(str.begin(), str.end(), std::back_inserter(E.Loc)); |
| 128 | LL.Entries.push_back(std::move(E)); |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame] | 132 | void DWARFDebugLoc::parse(const DWARFDataExtractor &data) { |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 133 | IsLittleEndian = data.isLittleEndian(); |
| 134 | AddressSize = data.getAddressSize(); |
| 135 | |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 136 | uint32_t Offset = 0; |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 137 | while (data.isValidOffset(Offset + data.getAddressSize() - 1)) { |
| 138 | if (auto LL = parseOneLocationList(data, &Offset)) |
| 139 | Locations.push_back(std::move(*LL)); |
| 140 | else |
| 141 | break; |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 142 | } |
Adrian Prantl | 6f84d31 | 2014-02-11 21:22:53 +0000 | [diff] [blame] | 143 | if (data.isValidOffset(Offset)) |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 144 | WithColor::error() << "failed to consume entire .debug_loc section\n"; |
David Blaikie | 18e7350 | 2013-06-19 21:37:13 +0000 | [diff] [blame] | 145 | } |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 146 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 147 | Optional<DWARFDebugLocDWO::LocationList> |
| 148 | DWARFDebugLocDWO::parseOneLocationList(DataExtractor Data, unsigned *Offset) { |
| 149 | LocationList LL; |
| 150 | LL.Offset = *Offset; |
| 151 | |
| 152 | // dwarf::DW_LLE_end_of_list_entry is 0 and indicates the end of the list. |
| 153 | while (auto Kind = |
| 154 | static_cast<dwarf::LocationListEntry>(Data.getU8(Offset))) { |
| 155 | if (Kind != dwarf::DW_LLE_startx_length) { |
Jonas Devlieghere | 84e9926 | 2018-04-14 22:07:23 +0000 | [diff] [blame] | 156 | WithColor::error() << "dumping support for LLE of kind " << (int)Kind |
| 157 | << " not implemented\n"; |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 158 | return None; |
| 159 | } |
| 160 | |
| 161 | Entry E; |
| 162 | E.Start = Data.getULEB128(Offset); |
| 163 | E.Length = Data.getU32(Offset); |
| 164 | |
| 165 | unsigned Bytes = Data.getU16(Offset); |
| 166 | // A single location description describing the location of the object... |
| 167 | StringRef str = Data.getData().substr(*Offset, Bytes); |
| 168 | *Offset += Bytes; |
| 169 | E.Loc.resize(str.size()); |
| 170 | std::copy(str.begin(), str.end(), E.Loc.begin()); |
| 171 | |
| 172 | LL.Entries.push_back(std::move(E)); |
| 173 | } |
| 174 | return LL; |
| 175 | } |
| 176 | |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 177 | void DWARFDebugLocDWO::parse(DataExtractor data) { |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 178 | IsLittleEndian = data.isLittleEndian(); |
| 179 | AddressSize = data.getAddressSize(); |
| 180 | |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 181 | uint32_t Offset = 0; |
| 182 | while (data.isValidOffset(Offset)) { |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 183 | if (auto LL = parseOneLocationList(data, &Offset)) |
| 184 | Locations.push_back(std::move(*LL)); |
| 185 | else |
| 186 | return; |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Jonas Devlieghere | 622c563 | 2017-09-27 09:33:36 +0000 | [diff] [blame] | 190 | DWARFDebugLocDWO::LocationList const * |
| 191 | DWARFDebugLocDWO::getLocationListAtOffset(uint64_t Offset) const { |
| 192 | auto It = std::lower_bound( |
| 193 | Locations.begin(), Locations.end(), Offset, |
| 194 | [](const LocationList &L, uint64_t Offset) { return L.Offset < Offset; }); |
| 195 | if (It != Locations.end() && It->Offset == Offset) |
| 196 | return &(*It); |
| 197 | return nullptr; |
| 198 | } |
| 199 | |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 200 | void DWARFDebugLocDWO::LocationList::dump(raw_ostream &OS, bool IsLittleEndian, |
| 201 | unsigned AddressSize, |
| 202 | const MCRegisterInfo *MRI, |
| 203 | unsigned Indent) const { |
| 204 | for (const Entry &E : Entries) { |
| 205 | OS << '\n'; |
| 206 | OS.indent(Indent); |
| 207 | OS << "Addr idx " << E.Start << " (w/ length " << E.Length << "): "; |
| 208 | dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI); |
| 209 | } |
| 210 | } |
| 211 | |
Jonas Devlieghere | 622c563 | 2017-09-27 09:33:36 +0000 | [diff] [blame] | 212 | void DWARFDebugLocDWO::dump(raw_ostream &OS, const MCRegisterInfo *MRI, |
| 213 | Optional<uint64_t> Offset) const { |
| 214 | auto DumpLocationList = [&](const LocationList &L) { |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 215 | OS << format("0x%8.8x: ", L.Offset); |
Reid Kleckner | a058736 | 2017-08-29 21:41:21 +0000 | [diff] [blame] | 216 | L.dump(OS, IsLittleEndian, AddressSize, MRI, /*Indent=*/12); |
| 217 | OS << "\n\n"; |
Jonas Devlieghere | 622c563 | 2017-09-27 09:33:36 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | if (Offset) { |
| 221 | if (auto *L = getLocationListAtOffset(*Offset)) |
| 222 | DumpLocationList(*L); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | for (const LocationList &L : Locations) { |
| 227 | DumpLocationList(L); |
David Blaikie | 9c550ac | 2014-03-25 01:44:02 +0000 | [diff] [blame] | 228 | } |
| 229 | } |