1*854c3394SAlexey Lapshin //===- ExtractRanges.cpp ----------------------------------------*- C++ -*-===// 2*854c3394SAlexey Lapshin // 3*854c3394SAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*854c3394SAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information. 5*854c3394SAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*854c3394SAlexey Lapshin // 7*854c3394SAlexey Lapshin //===----------------------------------------------------------------------===// 8*854c3394SAlexey Lapshin 9*854c3394SAlexey Lapshin #include "llvm/DebugInfo/GSYM/ExtractRanges.h" 10*854c3394SAlexey Lapshin #include "llvm/DebugInfo/GSYM/FileWriter.h" 11*854c3394SAlexey Lapshin #include "llvm/Support/DataExtractor.h" 12*854c3394SAlexey Lapshin #include <inttypes.h> 13*854c3394SAlexey Lapshin 14*854c3394SAlexey Lapshin namespace llvm { 15*854c3394SAlexey Lapshin namespace gsym { 16*854c3394SAlexey Lapshin 17*854c3394SAlexey Lapshin void encodeRange(const AddressRange &Range, FileWriter &O, uint64_t BaseAddr) { 18*854c3394SAlexey Lapshin assert(Range.start() >= BaseAddr); 19*854c3394SAlexey Lapshin O.writeULEB(Range.start() - BaseAddr); 20*854c3394SAlexey Lapshin O.writeULEB(Range.size()); 21*854c3394SAlexey Lapshin } 22*854c3394SAlexey Lapshin 23*854c3394SAlexey Lapshin AddressRange decodeRange(DataExtractor &Data, uint64_t BaseAddr, 24*854c3394SAlexey Lapshin uint64_t &Offset) { 25*854c3394SAlexey Lapshin const uint64_t AddrOffset = Data.getULEB128(&Offset); 26*854c3394SAlexey Lapshin const uint64_t Size = Data.getULEB128(&Offset); 27*854c3394SAlexey Lapshin const uint64_t StartAddr = BaseAddr + AddrOffset; 28*854c3394SAlexey Lapshin 29*854c3394SAlexey Lapshin return {StartAddr, StartAddr + Size}; 30*854c3394SAlexey Lapshin } 31*854c3394SAlexey Lapshin 32*854c3394SAlexey Lapshin void encodeRanges(const AddressRanges &Ranges, FileWriter &O, 33*854c3394SAlexey Lapshin uint64_t BaseAddr) { 34*854c3394SAlexey Lapshin O.writeULEB(Ranges.size()); 35*854c3394SAlexey Lapshin if (Ranges.empty()) 36*854c3394SAlexey Lapshin return; 37*854c3394SAlexey Lapshin for (auto Range : Ranges) 38*854c3394SAlexey Lapshin encodeRange(Range, O, BaseAddr); 39*854c3394SAlexey Lapshin } 40*854c3394SAlexey Lapshin 41*854c3394SAlexey Lapshin void decodeRanges(AddressRanges &Ranges, DataExtractor &Data, uint64_t BaseAddr, 42*854c3394SAlexey Lapshin uint64_t &Offset) { 43*854c3394SAlexey Lapshin Ranges.clear(); 44*854c3394SAlexey Lapshin uint64_t NumRanges = Data.getULEB128(&Offset); 45*854c3394SAlexey Lapshin Ranges.reserve(NumRanges); 46*854c3394SAlexey Lapshin for (uint64_t RangeIdx = 0; RangeIdx < NumRanges; RangeIdx++) 47*854c3394SAlexey Lapshin Ranges.insert(decodeRange(Data, BaseAddr, Offset)); 48*854c3394SAlexey Lapshin } 49*854c3394SAlexey Lapshin 50*854c3394SAlexey Lapshin void skipRange(DataExtractor &Data, uint64_t &Offset) { 51*854c3394SAlexey Lapshin Data.getULEB128(&Offset); 52*854c3394SAlexey Lapshin Data.getULEB128(&Offset); 53*854c3394SAlexey Lapshin } 54*854c3394SAlexey Lapshin 55*854c3394SAlexey Lapshin uint64_t skipRanges(DataExtractor &Data, uint64_t &Offset) { 56*854c3394SAlexey Lapshin uint64_t NumRanges = Data.getULEB128(&Offset); 57*854c3394SAlexey Lapshin for (uint64_t I = 0; I < NumRanges; ++I) 58*854c3394SAlexey Lapshin skipRange(Data, Offset); 59*854c3394SAlexey Lapshin return NumRanges; 60*854c3394SAlexey Lapshin } 61*854c3394SAlexey Lapshin 62*854c3394SAlexey Lapshin } // namespace gsym 63*854c3394SAlexey Lapshin 64*854c3394SAlexey Lapshin raw_ostream &operator<<(raw_ostream &OS, const AddressRange &R) { 65*854c3394SAlexey Lapshin return OS << '[' << HEX64(R.start()) << " - " << HEX64(R.end()) << ")"; 66*854c3394SAlexey Lapshin } 67*854c3394SAlexey Lapshin 68*854c3394SAlexey Lapshin raw_ostream &operator<<(raw_ostream &OS, const AddressRanges &AR) { 69*854c3394SAlexey Lapshin size_t Size = AR.size(); 70*854c3394SAlexey Lapshin for (size_t I = 0; I < Size; ++I) { 71*854c3394SAlexey Lapshin if (I) 72*854c3394SAlexey Lapshin OS << ' '; 73*854c3394SAlexey Lapshin OS << AR[I]; 74*854c3394SAlexey Lapshin } 75*854c3394SAlexey Lapshin return OS; 76*854c3394SAlexey Lapshin } 77*854c3394SAlexey Lapshin 78*854c3394SAlexey Lapshin } // namespace llvm 79