1 //===- DWARFDebugRangesList.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 10 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" 11 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 12 #include "llvm/Support/Format.h" 13 #include "llvm/Support/raw_ostream.h" 14 #include <cinttypes> 15 #include <cstdint> 16 #include <utility> 17 18 using namespace llvm; 19 20 void DWARFDebugRangeList::clear() { 21 Offset = -1U; 22 AddressSize = 0; 23 Entries.clear(); 24 } 25 26 bool DWARFDebugRangeList::extract(DataExtractor data, uint32_t *offset_ptr, 27 const RelocAddrMap &Relocs) { 28 clear(); 29 if (!data.isValidOffset(*offset_ptr)) 30 return false; 31 AddressSize = data.getAddressSize(); 32 if (AddressSize != 4 && AddressSize != 8) 33 return false; 34 Offset = *offset_ptr; 35 while (true) { 36 RangeListEntry entry; 37 uint32_t prev_offset = *offset_ptr; 38 entry.StartAddress = getRelocatedValue(data, AddressSize, offset_ptr, 39 &Relocs, &entry.SectionIndex); 40 entry.EndAddress = 41 getRelocatedValue(data, AddressSize, offset_ptr, &Relocs); 42 43 // Check that both values were extracted correctly. 44 if (*offset_ptr != prev_offset + 2 * AddressSize) { 45 clear(); 46 return false; 47 } 48 if (entry.isEndOfListEntry()) 49 break; 50 Entries.push_back(entry); 51 } 52 return true; 53 } 54 55 void DWARFDebugRangeList::dump(raw_ostream &OS) const { 56 for (const RangeListEntry &RLE : Entries) { 57 const char *format_str = (AddressSize == 4 58 ? "%08x %08" PRIx64 " %08" PRIx64 "\n" 59 : "%08x %016" PRIx64 " %016" PRIx64 "\n"); 60 OS << format(format_str, Offset, RLE.StartAddress, RLE.EndAddress); 61 } 62 OS << format("%08x <End of list>\n", Offset); 63 } 64 65 DWARFAddressRangesVector 66 DWARFDebugRangeList::getAbsoluteRanges(uint64_t BaseAddress) const { 67 DWARFAddressRangesVector Res; 68 for (const RangeListEntry &RLE : Entries) { 69 if (RLE.isBaseAddressSelectionEntry(AddressSize)) { 70 BaseAddress = RLE.EndAddress; 71 } else { 72 Res.push_back({BaseAddress + RLE.StartAddress, 73 BaseAddress + RLE.EndAddress, RLE.SectionIndex}); 74 } 75 } 76 return Res; 77 } 78