1 //===- DWARFDebugLoc.cpp --------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" 10 #include "llvm/ADT/StringRef.h" 11 #include "llvm/BinaryFormat/Dwarf.h" 12 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 13 #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 14 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" 15 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 16 #include "llvm/Support/Compiler.h" 17 #include "llvm/Support/Format.h" 18 #include "llvm/Support/WithColor.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <algorithm> 21 #include <cinttypes> 22 #include <cstdint> 23 24 using namespace llvm; 25 26 // When directly dumping the .debug_loc without a compile unit, we have to guess 27 // at the DWARF version. This only affects DW_OP_call_ref, which is a rare 28 // expression that LLVM doesn't produce. Guessing the wrong version means we 29 // won't be able to pretty print expressions in DWARF2 binaries produced by 30 // non-LLVM tools. 31 static void dumpExpression(raw_ostream &OS, ArrayRef<uint8_t> Data, 32 bool IsLittleEndian, unsigned AddressSize, 33 const MCRegisterInfo *MRI, DWARFUnit *U) { 34 DWARFDataExtractor Extractor(toStringRef(Data), IsLittleEndian, AddressSize); 35 DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI, U); 36 } 37 38 void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, uint64_t BaseAddress, 39 bool IsLittleEndian, 40 unsigned AddressSize, 41 const MCRegisterInfo *MRI, DWARFUnit *U, 42 DIDumpOptions DumpOpts, 43 unsigned Indent) const { 44 for (const Entry &E : Entries) { 45 OS << '\n'; 46 OS.indent(Indent); 47 OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2, 48 BaseAddress + E.Begin); 49 OS << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2, 50 BaseAddress + E.End); 51 OS << ": "; 52 53 dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI, U); 54 } 55 } 56 57 DWARFDebugLoc::LocationList const * 58 DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const { 59 auto It = partition_point( 60 Locations, [=](const LocationList &L) { return L.Offset < Offset; }); 61 if (It != Locations.end() && It->Offset == Offset) 62 return &(*It); 63 return nullptr; 64 } 65 66 void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI, DIDumpOptions DumpOpts, 67 Optional<uint64_t> Offset) const { 68 auto DumpLocationList = [&](const LocationList &L) { 69 OS << format("0x%8.8" PRIx64 ": ", L.Offset); 70 L.dump(OS, 0, IsLittleEndian, AddressSize, MRI, nullptr, DumpOpts, 12); 71 OS << "\n"; 72 }; 73 74 if (Offset) { 75 if (auto *L = getLocationListAtOffset(*Offset)) 76 DumpLocationList(*L); 77 return; 78 } 79 80 for (const LocationList &L : Locations) { 81 DumpLocationList(L); 82 if (&L != &Locations.back()) 83 OS << '\n'; 84 } 85 } 86 87 Expected<DWARFDebugLoc::LocationList> 88 DWARFDebugLoc::parseOneLocationList(const DWARFDataExtractor &Data, 89 uint64_t *Offset) { 90 LocationList LL; 91 LL.Offset = *Offset; 92 AddressSize = Data.getAddressSize(); 93 DataExtractor::Cursor C(*Offset); 94 95 // 2.6.2 Location Lists 96 // A location list entry consists of: 97 while (true) { 98 Entry E; 99 100 // 1. A beginning address offset. ... 101 E.Begin = Data.getRelocatedAddress(C); 102 103 // 2. An ending address offset. ... 104 E.End = Data.getRelocatedAddress(C); 105 106 if (Error Err = C.takeError()) 107 return std::move(Err); 108 109 // The end of any given location list is marked by an end of list entry, 110 // which consists of a 0 for the beginning address offset and a 0 for the 111 // ending address offset. 112 if (E.Begin == 0 && E.End == 0) { 113 *Offset = C.tell(); 114 return LL; 115 } 116 117 if (E.Begin != (AddressSize == 4 ? -1U : -1ULL)) { 118 unsigned Bytes = Data.getU16(C); 119 // A single location description describing the location of the object... 120 Data.getU8(C, E.Loc, Bytes); 121 } 122 123 LL.Entries.push_back(std::move(E)); 124 } 125 } 126 127 void DWARFDebugLoc::parse(const DWARFDataExtractor &data) { 128 IsLittleEndian = data.isLittleEndian(); 129 AddressSize = data.getAddressSize(); 130 131 uint64_t Offset = 0; 132 while (Offset < data.getData().size()) { 133 if (auto LL = parseOneLocationList(data, &Offset)) 134 Locations.push_back(std::move(*LL)); 135 else { 136 logAllUnhandledErrors(LL.takeError(), WithColor::error()); 137 break; 138 } 139 } 140 } 141 142 Error DWARFDebugLoclists::visitLocationList( 143 const DWARFDataExtractor &Data, uint64_t *Offset, uint16_t Version, 144 llvm::function_ref<bool(const Entry &)> F) { 145 146 DataExtractor::Cursor C(*Offset); 147 bool Continue = true; 148 while (Continue) { 149 Entry E; 150 E.Offset = C.tell(); 151 E.Kind = Data.getU8(C); 152 switch (E.Kind) { 153 case dwarf::DW_LLE_end_of_list: 154 break; 155 case dwarf::DW_LLE_base_addressx: 156 E.Value0 = Data.getULEB128(C); 157 break; 158 case dwarf::DW_LLE_startx_length: 159 E.Value0 = Data.getULEB128(C); 160 // Pre-DWARF 5 has different interpretation of the length field. We have 161 // to support both pre- and standartized styles for the compatibility. 162 if (Version < 5) 163 E.Value1 = Data.getU32(C); 164 else 165 E.Value1 = Data.getULEB128(C); 166 break; 167 case dwarf::DW_LLE_offset_pair: 168 E.Value0 = Data.getULEB128(C); 169 E.Value1 = Data.getULEB128(C); 170 break; 171 case dwarf::DW_LLE_base_address: 172 E.Value0 = Data.getRelocatedAddress(C); 173 break; 174 case dwarf::DW_LLE_start_length: 175 E.Value0 = Data.getRelocatedAddress(C); 176 E.Value1 = Data.getULEB128(C); 177 break; 178 case dwarf::DW_LLE_startx_endx: 179 case dwarf::DW_LLE_default_location: 180 case dwarf::DW_LLE_start_end: 181 default: 182 cantFail(C.takeError()); 183 return createStringError(errc::illegal_byte_sequence, 184 "LLE of kind %x not supported", (int)E.Kind); 185 } 186 187 if (E.Kind != dwarf::DW_LLE_base_address && 188 E.Kind != dwarf::DW_LLE_base_addressx && 189 E.Kind != dwarf::DW_LLE_end_of_list) { 190 unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C); 191 // A single location description describing the location of the object... 192 Data.getU8(C, E.Loc, Bytes); 193 } 194 195 if (!C) 196 return C.takeError(); 197 Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list; 198 } 199 *Offset = C.tell(); 200 return Error::success(); 201 } 202 203 bool DWARFDebugLoclists::dumpLocationList(const DWARFDataExtractor &Data, 204 uint64_t *Offset, uint16_t Version, 205 raw_ostream &OS, uint64_t BaseAddr, 206 const MCRegisterInfo *MRI, 207 DWARFUnit *U, DIDumpOptions DumpOpts, 208 unsigned Indent) { 209 size_t MaxEncodingStringLength = 0; 210 if (DumpOpts.Verbose) { 211 #define HANDLE_DW_LLE(ID, NAME) \ 212 MaxEncodingStringLength = std::max(MaxEncodingStringLength, \ 213 dwarf::LocListEncodingString(ID).size()); 214 #include "llvm/BinaryFormat/Dwarf.def" 215 } 216 217 OS << format("0x%8.8" PRIx64 ": ", *Offset); 218 Error E = visitLocationList(Data, Offset, Version, [&](const Entry &E) { 219 E.dump(OS, BaseAddr, Data.isLittleEndian(), Data.getAddressSize(), MRI, U, 220 DumpOpts, Indent, MaxEncodingStringLength); 221 return true; 222 }); 223 if (E) { 224 OS << "\n"; 225 OS.indent(Indent); 226 OS << "error: " << toString(std::move(E)); 227 return false; 228 } 229 return true; 230 } 231 232 void DWARFDebugLoclists::Entry::dump(raw_ostream &OS, uint64_t &BaseAddr, 233 bool IsLittleEndian, unsigned AddressSize, 234 const MCRegisterInfo *MRI, DWARFUnit *U, 235 DIDumpOptions DumpOpts, unsigned Indent, 236 size_t MaxEncodingStringLength) const { 237 if (DumpOpts.Verbose) { 238 OS << "\n"; 239 OS.indent(Indent); 240 auto EncodingString = dwarf::LocListEncodingString(Kind); 241 // Unsupported encodings should have been reported during parsing. 242 assert(!EncodingString.empty() && "Unknown loclist entry encoding"); 243 OS << format("%s%*c", EncodingString.data(), 244 MaxEncodingStringLength - EncodingString.size() + 1, '('); 245 switch (Kind) { 246 case dwarf::DW_LLE_startx_length: 247 case dwarf::DW_LLE_start_length: 248 case dwarf::DW_LLE_offset_pair: 249 OS << format("0x%*.*" PRIx64 ", 0x%*.*" PRIx64, AddressSize * 2, 250 AddressSize * 2, Value0, AddressSize * 2, AddressSize * 2, 251 Value1); 252 break; 253 case dwarf::DW_LLE_base_addressx: 254 case dwarf::DW_LLE_base_address: 255 OS << format("0x%*.*" PRIx64, AddressSize * 2, AddressSize * 2, 256 Value0); 257 break; 258 case dwarf::DW_LLE_end_of_list: 259 break; 260 } 261 OS << ')'; 262 } 263 auto PrintPrefix = [&] { 264 OS << "\n"; 265 OS.indent(Indent); 266 if (DumpOpts.Verbose) 267 OS << format("%*s", MaxEncodingStringLength, (const char *)"=> "); 268 }; 269 switch (Kind) { 270 case dwarf::DW_LLE_startx_length: 271 PrintPrefix(); 272 OS << "Addr idx " << Value0 << " (w/ length " << Value1 << "): "; 273 break; 274 case dwarf::DW_LLE_start_length: 275 PrintPrefix(); 276 DWARFAddressRange(Value0, Value0 + Value1) 277 .dump(OS, AddressSize, DumpOpts); 278 OS << ": "; 279 break; 280 case dwarf::DW_LLE_offset_pair: 281 PrintPrefix(); 282 DWARFAddressRange(BaseAddr + Value0, BaseAddr + Value1) 283 .dump(OS, AddressSize, DumpOpts); 284 OS << ": "; 285 break; 286 case dwarf::DW_LLE_base_addressx: 287 if (!DumpOpts.Verbose) 288 return; 289 break; 290 case dwarf::DW_LLE_end_of_list: 291 if (!DumpOpts.Verbose) 292 return; 293 break; 294 case dwarf::DW_LLE_base_address: 295 BaseAddr = Value0; 296 if (!DumpOpts.Verbose) 297 return; 298 break; 299 default: 300 llvm_unreachable("unreachable locations list kind"); 301 } 302 303 dumpExpression(OS, Loc, IsLittleEndian, AddressSize, MRI, U); 304 } 305 306 void DWARFDebugLoclists::dumpRange(const DWARFDataExtractor &Data, 307 uint64_t StartOffset, uint64_t Size, 308 uint16_t Version, raw_ostream &OS, 309 uint64_t BaseAddr, const MCRegisterInfo *MRI, 310 DIDumpOptions DumpOpts) { 311 if (!Data.isValidOffsetForDataOfSize(StartOffset, Size)) { 312 OS << "Invalid dump range\n"; 313 return; 314 } 315 uint64_t Offset = StartOffset; 316 StringRef Separator; 317 bool CanContinue = true; 318 while (CanContinue && Offset < StartOffset + Size) { 319 OS << Separator; 320 Separator = "\n"; 321 322 CanContinue = dumpLocationList(Data, &Offset, Version, OS, BaseAddr, MRI, 323 nullptr, DumpOpts, /*Indent=*/12); 324 OS << '\n'; 325 } 326 } 327