Home | History | Annotate | Download | only in DWARF
      1 //===- DWARFDebugRangeList.h ------------------------------------*- C++ -*-===//
      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 #ifndef LLVM_DEBUGINFO_DWARF_DWARFDEBUGRANGELIST_H
     11 #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGRANGELIST_H
     12 
     13 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
     14 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
     15 #include <cassert>
     16 #include <cstdint>
     17 #include <vector>
     18 
     19 namespace llvm {
     20 
     21 struct BaseAddress;
     22 class raw_ostream;
     23 
     24 struct DWARFAddressRange {
     25   uint64_t LowPC;
     26   uint64_t HighPC;
     27   uint64_t SectionIndex;
     28 
     29   DWARFAddressRange() = default;
     30 
     31   /// Used for unit testing.
     32   DWARFAddressRange(uint64_t LowPC, uint64_t HighPC, uint64_t SectionIndex = 0)
     33       : LowPC(LowPC), HighPC(HighPC), SectionIndex(SectionIndex) {}
     34 
     35   /// Returns true if LowPC is smaller or equal to HighPC. This accounts for
     36   /// dead-stripped ranges.
     37   bool valid() const { return LowPC <= HighPC; }
     38 
     39   /// Returns true if [LowPC, HighPC) intersects with [RHS.LowPC, RHS.HighPC).
     40   bool intersects(const DWARFAddressRange &RHS) const {
     41     // Empty ranges can't intersect.
     42     if (LowPC == HighPC || RHS.LowPC == RHS.HighPC)
     43       return false;
     44     return (LowPC < RHS.HighPC) && (HighPC > RHS.LowPC);
     45   }
     46 
     47   /// Returns true if [LowPC, HighPC) fully contains [RHS.LowPC, RHS.HighPC).
     48   bool contains(const DWARFAddressRange &RHS) const {
     49     if (LowPC <= RHS.LowPC && RHS.LowPC <= HighPC)
     50       return LowPC <= RHS.HighPC && RHS.HighPC <= HighPC;
     51     return false;
     52   }
     53 };
     54 
     55 static inline bool operator<(const DWARFAddressRange &LHS,
     56                              const DWARFAddressRange &RHS) {
     57   return std::tie(LHS.LowPC, LHS.HighPC) < std::tie(RHS.LowPC, RHS.HighPC);
     58 }
     59 
     60 raw_ostream &operator<<(raw_ostream &OS, const DWARFAddressRange &R);
     61 
     62 /// DWARFAddressRangesVector - represents a set of absolute address ranges.
     63 using DWARFAddressRangesVector = std::vector<DWARFAddressRange>;
     64 
     65 class DWARFDebugRangeList {
     66 public:
     67   struct RangeListEntry {
     68     /// A beginning address offset. This address offset has the size of an
     69     /// address and is relative to the applicable base address of the
     70     /// compilation unit referencing this range list. It marks the beginning
     71     /// of an address range.
     72     uint64_t StartAddress;
     73     /// An ending address offset. This address offset again has the size of
     74     /// an address and is relative to the applicable base address of the
     75     /// compilation unit referencing this range list. It marks the first
     76     /// address past the end of the address range. The ending address must
     77     /// be greater than or equal to the beginning address.
     78     uint64_t EndAddress;
     79     /// A section index this range belongs to.
     80     uint64_t SectionIndex;
     81 
     82     /// The end of any given range list is marked by an end of list entry,
     83     /// which consists of a 0 for the beginning address offset
     84     /// and a 0 for the ending address offset.
     85     bool isEndOfListEntry() const {
     86       return (StartAddress == 0) && (EndAddress == 0);
     87     }
     88 
     89     /// A base address selection entry consists of:
     90     /// 1. The value of the largest representable address offset
     91     /// (for example, 0xffffffff when the size of an address is 32 bits).
     92     /// 2. An address, which defines the appropriate base address for
     93     /// use in interpreting the beginning and ending address offsets of
     94     /// subsequent entries of the location list.
     95     bool isBaseAddressSelectionEntry(uint8_t AddressSize) const {
     96       assert(AddressSize == 4 || AddressSize == 8);
     97       if (AddressSize == 4)
     98         return StartAddress == -1U;
     99       else
    100         return StartAddress == -1ULL;
    101     }
    102   };
    103 
    104 private:
    105   /// Offset in .debug_ranges section.
    106   uint32_t Offset;
    107   uint8_t AddressSize;
    108   std::vector<RangeListEntry> Entries;
    109 
    110 public:
    111   DWARFDebugRangeList() { clear(); }
    112 
    113   void clear();
    114   void dump(raw_ostream &OS) const;
    115   bool extract(const DWARFDataExtractor &data, uint32_t *offset_ptr);
    116   const std::vector<RangeListEntry> &getEntries() { return Entries; }
    117 
    118   /// getAbsoluteRanges - Returns absolute address ranges defined by this range
    119   /// list. Has to be passed base address of the compile unit referencing this
    120   /// range list.
    121   DWARFAddressRangesVector
    122   getAbsoluteRanges(llvm::Optional<BaseAddress> BaseAddr) const;
    123 };
    124 
    125 } // end namespace llvm
    126 
    127 #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGRANGELIST_H
    128