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/DIContext.h" 13 #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h" 14 #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 15 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 16 #include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h" 17 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 18 #include "llvm/Support/Format.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <algorithm> 21 #include <cinttypes> 22 #include <cstdint> 23 24 using namespace llvm; 25 using object::SectionedAddress; 26 27 namespace llvm { 28 class DWARFObject; 29 } 30 31 namespace { 32 class DWARFLocationInterpreter { 33 std::optional<object::SectionedAddress> Base; 34 std::function<std::optional<object::SectionedAddress>(uint32_t)> LookupAddr; 35 36 public: 37 DWARFLocationInterpreter( 38 std::optional<object::SectionedAddress> Base, 39 std::function<std::optional<object::SectionedAddress>(uint32_t)> 40 LookupAddr) 41 : Base(Base), LookupAddr(std::move(LookupAddr)) {} 42 43 Expected<std::optional<DWARFLocationExpression>> 44 Interpret(const DWARFLocationEntry &E); 45 }; 46 } // namespace 47 48 static Error createResolverError(uint32_t Index, unsigned Kind) { 49 return make_error<ResolverError>(Index, (dwarf::LoclistEntries)Kind); 50 } 51 52 Expected<std::optional<DWARFLocationExpression>> 53 DWARFLocationInterpreter::Interpret(const DWARFLocationEntry &E) { 54 switch (E.Kind) { 55 case dwarf::DW_LLE_end_of_list: 56 return std::nullopt; 57 case dwarf::DW_LLE_base_addressx: { 58 Base = LookupAddr(E.Value0); 59 if (!Base) 60 return createResolverError(E.Value0, E.Kind); 61 return std::nullopt; 62 } 63 case dwarf::DW_LLE_startx_endx: { 64 std::optional<SectionedAddress> LowPC = LookupAddr(E.Value0); 65 if (!LowPC) 66 return createResolverError(E.Value0, E.Kind); 67 std::optional<SectionedAddress> HighPC = LookupAddr(E.Value1); 68 if (!HighPC) 69 return createResolverError(E.Value1, E.Kind); 70 return DWARFLocationExpression{ 71 DWARFAddressRange{LowPC->Address, HighPC->Address, LowPC->SectionIndex}, 72 E.Loc}; 73 } 74 case dwarf::DW_LLE_startx_length: { 75 std::optional<SectionedAddress> LowPC = LookupAddr(E.Value0); 76 if (!LowPC) 77 return createResolverError(E.Value0, E.Kind); 78 return DWARFLocationExpression{DWARFAddressRange{LowPC->Address, 79 LowPC->Address + E.Value1, 80 LowPC->SectionIndex}, 81 E.Loc}; 82 } 83 case dwarf::DW_LLE_offset_pair: { 84 if (!Base) { 85 return createStringError(inconvertibleErrorCode(), 86 "Unable to resolve location list offset pair: " 87 "Base address not defined"); 88 } 89 DWARFAddressRange Range{Base->Address + E.Value0, Base->Address + E.Value1, 90 Base->SectionIndex}; 91 if (Range.SectionIndex == SectionedAddress::UndefSection) 92 Range.SectionIndex = E.SectionIndex; 93 return DWARFLocationExpression{Range, E.Loc}; 94 } 95 case dwarf::DW_LLE_default_location: 96 return DWARFLocationExpression{std::nullopt, E.Loc}; 97 case dwarf::DW_LLE_base_address: 98 Base = SectionedAddress{E.Value0, E.SectionIndex}; 99 return std::nullopt; 100 case dwarf::DW_LLE_start_end: 101 return DWARFLocationExpression{ 102 DWARFAddressRange{E.Value0, E.Value1, E.SectionIndex}, E.Loc}; 103 case dwarf::DW_LLE_start_length: 104 return DWARFLocationExpression{ 105 DWARFAddressRange{E.Value0, E.Value0 + E.Value1, E.SectionIndex}, 106 E.Loc}; 107 default: 108 llvm_unreachable("unreachable locations list kind"); 109 } 110 } 111 112 static void dumpExpression(raw_ostream &OS, DIDumpOptions DumpOpts, 113 ArrayRef<uint8_t> Data, bool IsLittleEndian, 114 unsigned AddressSize, DWARFUnit *U) { 115 DWARFDataExtractor Extractor(Data, IsLittleEndian, AddressSize); 116 std::optional<dwarf::DwarfFormat> Format; 117 if (U) 118 Format = U->getFormat(); 119 DWARFExpression(Extractor, AddressSize, Format).print(OS, DumpOpts, U); 120 } 121 122 bool DWARFLocationTable::dumpLocationList( 123 uint64_t *Offset, raw_ostream &OS, std::optional<SectionedAddress> BaseAddr, 124 const DWARFObject &Obj, DWARFUnit *U, DIDumpOptions DumpOpts, 125 unsigned Indent) const { 126 DWARFLocationInterpreter Interp( 127 BaseAddr, [U](uint32_t Index) -> std::optional<SectionedAddress> { 128 if (U) 129 return U->getAddrOffsetSectionItem(Index); 130 return std::nullopt; 131 }); 132 OS << format("0x%8.8" PRIx64 ": ", *Offset); 133 Error E = visitLocationList(Offset, [&](const DWARFLocationEntry &E) { 134 Expected<std::optional<DWARFLocationExpression>> Loc = Interp.Interpret(E); 135 if (!Loc || DumpOpts.DisplayRawContents) 136 dumpRawEntry(E, OS, Indent, DumpOpts, Obj); 137 if (Loc && *Loc) { 138 OS << "\n"; 139 OS.indent(Indent); 140 if (DumpOpts.DisplayRawContents) 141 OS << " => "; 142 143 DIDumpOptions RangeDumpOpts(DumpOpts); 144 RangeDumpOpts.DisplayRawContents = false; 145 if (Loc.get()->Range) 146 Loc.get()->Range->dump(OS, Data.getAddressSize(), RangeDumpOpts, &Obj); 147 else 148 OS << "<default>"; 149 } 150 if (!Loc) 151 consumeError(Loc.takeError()); 152 153 if (E.Kind != dwarf::DW_LLE_base_address && 154 E.Kind != dwarf::DW_LLE_base_addressx && 155 E.Kind != dwarf::DW_LLE_end_of_list) { 156 OS << ": "; 157 dumpExpression(OS, DumpOpts, E.Loc, Data.isLittleEndian(), 158 Data.getAddressSize(), U); 159 } 160 return true; 161 }); 162 if (E) { 163 DumpOpts.RecoverableErrorHandler(std::move(E)); 164 return false; 165 } 166 return true; 167 } 168 169 Error DWARFLocationTable::visitAbsoluteLocationList( 170 uint64_t Offset, std::optional<SectionedAddress> BaseAddr, 171 std::function<std::optional<SectionedAddress>(uint32_t)> LookupAddr, 172 function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const { 173 DWARFLocationInterpreter Interp(BaseAddr, std::move(LookupAddr)); 174 return visitLocationList(&Offset, [&](const DWARFLocationEntry &E) { 175 Expected<std::optional<DWARFLocationExpression>> Loc = Interp.Interpret(E); 176 if (!Loc) 177 return Callback(Loc.takeError()); 178 if (*Loc) 179 return Callback(**Loc); 180 return true; 181 }); 182 } 183 184 void DWARFDebugLoc::dump(raw_ostream &OS, const DWARFObject &Obj, 185 DIDumpOptions DumpOpts, 186 std::optional<uint64_t> DumpOffset) const { 187 auto BaseAddr = std::nullopt; 188 unsigned Indent = 12; 189 if (DumpOffset) { 190 dumpLocationList(&*DumpOffset, OS, BaseAddr, Obj, nullptr, DumpOpts, 191 Indent); 192 } else { 193 uint64_t Offset = 0; 194 StringRef Separator; 195 bool CanContinue = true; 196 while (CanContinue && Data.isValidOffset(Offset)) { 197 OS << Separator; 198 Separator = "\n"; 199 200 CanContinue = dumpLocationList(&Offset, OS, BaseAddr, Obj, nullptr, 201 DumpOpts, Indent); 202 OS << '\n'; 203 } 204 } 205 } 206 207 Error DWARFDebugLoc::visitLocationList( 208 uint64_t *Offset, 209 function_ref<bool(const DWARFLocationEntry &)> Callback) const { 210 DataExtractor::Cursor C(*Offset); 211 while (true) { 212 uint64_t SectionIndex; 213 uint64_t Value0 = Data.getRelocatedAddress(C); 214 uint64_t Value1 = Data.getRelocatedAddress(C, &SectionIndex); 215 216 DWARFLocationEntry E; 217 218 // The end of any given location list is marked by an end of list entry, 219 // which consists of a 0 for the beginning address offset and a 0 for the 220 // ending address offset. A beginning offset of 0xff...f marks the base 221 // address selection entry. 222 if (Value0 == 0 && Value1 == 0) { 223 E.Kind = dwarf::DW_LLE_end_of_list; 224 } else if (Value0 == (Data.getAddressSize() == 4 ? -1U : -1ULL)) { 225 E.Kind = dwarf::DW_LLE_base_address; 226 E.Value0 = Value1; 227 E.SectionIndex = SectionIndex; 228 } else { 229 E.Kind = dwarf::DW_LLE_offset_pair; 230 E.Value0 = Value0; 231 E.Value1 = Value1; 232 E.SectionIndex = SectionIndex; 233 unsigned Bytes = Data.getU16(C); 234 // A single location description describing the location of the object... 235 Data.getU8(C, E.Loc, Bytes); 236 } 237 238 if (!C) 239 return C.takeError(); 240 if (!Callback(E) || E.Kind == dwarf::DW_LLE_end_of_list) 241 break; 242 } 243 *Offset = C.tell(); 244 return Error::success(); 245 } 246 247 void DWARFDebugLoc::dumpRawEntry(const DWARFLocationEntry &Entry, 248 raw_ostream &OS, unsigned Indent, 249 DIDumpOptions DumpOpts, 250 const DWARFObject &Obj) const { 251 uint64_t Value0, Value1; 252 switch (Entry.Kind) { 253 case dwarf::DW_LLE_base_address: 254 Value0 = Data.getAddressSize() == 4 ? -1U : -1ULL; 255 Value1 = Entry.Value0; 256 break; 257 case dwarf::DW_LLE_offset_pair: 258 Value0 = Entry.Value0; 259 Value1 = Entry.Value1; 260 break; 261 case dwarf::DW_LLE_end_of_list: 262 return; 263 default: 264 llvm_unreachable("Not possible in DWARF4!"); 265 } 266 OS << '\n'; 267 OS.indent(Indent); 268 OS << '(' << format_hex(Value0, 2 + Data.getAddressSize() * 2) << ", " 269 << format_hex(Value1, 2 + Data.getAddressSize() * 2) << ')'; 270 DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex); 271 } 272 273 Error DWARFDebugLoclists::visitLocationList( 274 uint64_t *Offset, function_ref<bool(const DWARFLocationEntry &)> F) const { 275 276 DataExtractor::Cursor C(*Offset); 277 bool Continue = true; 278 while (Continue) { 279 DWARFLocationEntry E; 280 E.Kind = Data.getU8(C); 281 switch (E.Kind) { 282 case dwarf::DW_LLE_end_of_list: 283 break; 284 case dwarf::DW_LLE_base_addressx: 285 E.Value0 = Data.getULEB128(C); 286 break; 287 case dwarf::DW_LLE_startx_endx: 288 E.Value0 = Data.getULEB128(C); 289 E.Value1 = Data.getULEB128(C); 290 break; 291 case dwarf::DW_LLE_startx_length: 292 E.Value0 = Data.getULEB128(C); 293 // Pre-DWARF 5 has different interpretation of the length field. We have 294 // to support both pre- and standartized styles for the compatibility. 295 if (Version < 5) 296 E.Value1 = Data.getU32(C); 297 else 298 E.Value1 = Data.getULEB128(C); 299 break; 300 case dwarf::DW_LLE_offset_pair: 301 E.Value0 = Data.getULEB128(C); 302 E.Value1 = Data.getULEB128(C); 303 E.SectionIndex = SectionedAddress::UndefSection; 304 break; 305 case dwarf::DW_LLE_default_location: 306 break; 307 case dwarf::DW_LLE_base_address: 308 E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex); 309 break; 310 case dwarf::DW_LLE_start_end: 311 E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex); 312 E.Value1 = Data.getRelocatedAddress(C); 313 break; 314 case dwarf::DW_LLE_start_length: 315 E.Value0 = Data.getRelocatedAddress(C, &E.SectionIndex); 316 E.Value1 = Data.getULEB128(C); 317 break; 318 default: 319 cantFail(C.takeError()); 320 return createStringError(errc::illegal_byte_sequence, 321 "LLE of kind %x not supported", (int)E.Kind); 322 } 323 324 if (E.Kind != dwarf::DW_LLE_base_address && 325 E.Kind != dwarf::DW_LLE_base_addressx && 326 E.Kind != dwarf::DW_LLE_end_of_list) { 327 unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C); 328 // A single location description describing the location of the object... 329 Data.getU8(C, E.Loc, Bytes); 330 } 331 332 if (!C) 333 return C.takeError(); 334 Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list; 335 } 336 *Offset = C.tell(); 337 return Error::success(); 338 } 339 340 void DWARFDebugLoclists::dumpRawEntry(const DWARFLocationEntry &Entry, 341 raw_ostream &OS, unsigned Indent, 342 DIDumpOptions DumpOpts, 343 const DWARFObject &Obj) const { 344 size_t MaxEncodingStringLength = 0; 345 #define HANDLE_DW_LLE(ID, NAME) \ 346 MaxEncodingStringLength = std::max(MaxEncodingStringLength, \ 347 dwarf::LocListEncodingString(ID).size()); 348 #include "llvm/BinaryFormat/Dwarf.def" 349 350 OS << "\n"; 351 OS.indent(Indent); 352 StringRef EncodingString = dwarf::LocListEncodingString(Entry.Kind); 353 // Unsupported encodings should have been reported during parsing. 354 assert(!EncodingString.empty() && "Unknown loclist entry encoding"); 355 OS << format("%-*s(", MaxEncodingStringLength, EncodingString.data()); 356 unsigned FieldSize = 2 + 2 * Data.getAddressSize(); 357 switch (Entry.Kind) { 358 case dwarf::DW_LLE_end_of_list: 359 case dwarf::DW_LLE_default_location: 360 break; 361 case dwarf::DW_LLE_startx_endx: 362 case dwarf::DW_LLE_startx_length: 363 case dwarf::DW_LLE_offset_pair: 364 case dwarf::DW_LLE_start_end: 365 case dwarf::DW_LLE_start_length: 366 OS << format_hex(Entry.Value0, FieldSize) << ", " 367 << format_hex(Entry.Value1, FieldSize); 368 break; 369 case dwarf::DW_LLE_base_addressx: 370 case dwarf::DW_LLE_base_address: 371 OS << format_hex(Entry.Value0, FieldSize); 372 break; 373 } 374 OS << ')'; 375 switch (Entry.Kind) { 376 case dwarf::DW_LLE_base_address: 377 case dwarf::DW_LLE_start_end: 378 case dwarf::DW_LLE_start_length: 379 DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, Entry.SectionIndex); 380 break; 381 default: 382 break; 383 } 384 } 385 386 void DWARFDebugLoclists::dumpRange(uint64_t StartOffset, uint64_t Size, 387 raw_ostream &OS, const DWARFObject &Obj, 388 DIDumpOptions DumpOpts) { 389 if (!Data.isValidOffsetForDataOfSize(StartOffset, Size)) { 390 OS << "Invalid dump range\n"; 391 return; 392 } 393 uint64_t Offset = StartOffset; 394 StringRef Separator; 395 bool CanContinue = true; 396 while (CanContinue && Offset < StartOffset + Size) { 397 OS << Separator; 398 Separator = "\n"; 399 400 CanContinue = dumpLocationList(&Offset, OS, /*BaseAddr=*/std::nullopt, Obj, 401 nullptr, DumpOpts, /*Indent=*/12); 402 OS << '\n'; 403 } 404 } 405 406 void llvm::ResolverError::log(raw_ostream &OS) const { 407 OS << format("unable to resolve indirect address %u for: %s", Index, 408 dwarf::LocListEncodingString(Kind).data()); 409 } 410 411 char llvm::ResolverError::ID; 412