Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:41

0001 //===- DWARFDebugLoc.h ------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
0010 #define LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H
0011 
0012 #include "llvm/ADT/SmallVector.h"
0013 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
0014 #include "llvm/Support/Errc.h"
0015 #include <cstdint>
0016 
0017 namespace llvm {
0018 class DWARFUnit;
0019 class MCRegisterInfo;
0020 class raw_ostream;
0021 class DWARFObject;
0022 struct DIDumpOptions;
0023 struct DWARFLocationExpression;
0024 namespace object {
0025 struct SectionedAddress;
0026 }
0027 
0028 /// A single location within a location list. Entries are stored in the DWARF5
0029 /// form even if they originally come from a DWARF<=4 location list.
0030 struct DWARFLocationEntry {
0031   /// The entry kind (DW_LLE_***).
0032   uint8_t Kind;
0033 
0034   /// The first value of the location entry (if applicable).
0035   uint64_t Value0;
0036 
0037   /// The second value of the location entry (if applicable).
0038   uint64_t Value1;
0039 
0040   /// The index of the section this entry is relative to (if applicable).
0041   uint64_t SectionIndex;
0042 
0043   /// The location expression itself (if applicable).
0044   SmallVector<uint8_t, 4> Loc;
0045 };
0046 
0047 /// An abstract base class for various kinds of location tables (.debug_loc,
0048 /// .debug_loclists, and their dwo variants).
0049 class DWARFLocationTable {
0050 public:
0051   DWARFLocationTable(DWARFDataExtractor Data) : Data(std::move(Data)) {}
0052   virtual ~DWARFLocationTable() = default;
0053 
0054   /// Call the user-provided callback for each entry (including the end-of-list
0055   /// entry) in the location list starting at \p Offset. The callback can return
0056   /// false to terminate the iteration early. Returns an error if it was unable
0057   /// to parse the entire location list correctly. Upon successful termination
0058   /// \p Offset will be updated point past the end of the list.
0059   virtual Error visitLocationList(
0060       uint64_t *Offset,
0061       function_ref<bool(const DWARFLocationEntry &)> Callback) const = 0;
0062 
0063   /// Dump the location list at the given \p Offset. The function returns true
0064   /// iff it has successfully reched the end of the list. This means that one
0065   /// can attempt to parse another list after the current one (\p Offset will be
0066   /// updated to point past the end of the current list).
0067   bool dumpLocationList(uint64_t *Offset, raw_ostream &OS,
0068                         std::optional<object::SectionedAddress> BaseAddr,
0069                         const DWARFObject &Obj, DWARFUnit *U,
0070                         DIDumpOptions DumpOpts, unsigned Indent) const;
0071 
0072   Error visitAbsoluteLocationList(
0073       uint64_t Offset, std::optional<object::SectionedAddress> BaseAddr,
0074       std::function<std::optional<object::SectionedAddress>(uint32_t)>
0075           LookupAddr,
0076       function_ref<bool(Expected<DWARFLocationExpression>)> Callback) const;
0077 
0078   const DWARFDataExtractor &getData() { return Data; }
0079 
0080 protected:
0081   DWARFDataExtractor Data;
0082 
0083   virtual void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
0084                             unsigned Indent, DIDumpOptions DumpOpts,
0085                             const DWARFObject &Obj) const = 0;
0086 };
0087 
0088 class DWARFDebugLoc final : public DWARFLocationTable {
0089 public:
0090   /// A list of locations that contain one variable.
0091   struct LocationList {
0092     /// The beginning offset where this location list is stored in the debug_loc
0093     /// section.
0094     uint64_t Offset;
0095     /// All the locations in which the variable is stored.
0096     SmallVector<DWARFLocationEntry, 2> Entries;
0097   };
0098 
0099 private:
0100   using LocationLists = SmallVector<LocationList, 4>;
0101 
0102   /// A list of all the variables in the debug_loc section, each one describing
0103   /// the locations in which the variable is stored.
0104   LocationLists Locations;
0105 
0106 public:
0107   DWARFDebugLoc(DWARFDataExtractor Data)
0108       : DWARFLocationTable(std::move(Data)) {}
0109 
0110   /// Print the location lists found within the debug_loc section.
0111   void dump(raw_ostream &OS, const DWARFObject &Obj, DIDumpOptions DumpOpts,
0112             std::optional<uint64_t> Offset) const;
0113 
0114   Error visitLocationList(
0115       uint64_t *Offset,
0116       function_ref<bool(const DWARFLocationEntry &)> Callback) const override;
0117 
0118 protected:
0119   void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
0120                     unsigned Indent, DIDumpOptions DumpOpts,
0121                     const DWARFObject &Obj) const override;
0122 };
0123 
0124 class DWARFDebugLoclists final : public DWARFLocationTable {
0125 public:
0126   DWARFDebugLoclists(DWARFDataExtractor Data, uint16_t Version)
0127       : DWARFLocationTable(std::move(Data)), Version(Version) {}
0128 
0129   Error visitLocationList(
0130       uint64_t *Offset,
0131       function_ref<bool(const DWARFLocationEntry &)> Callback) const override;
0132 
0133   /// Dump all location lists within the given range.
0134   void dumpRange(uint64_t StartOffset, uint64_t Size, raw_ostream &OS,
0135                  const DWARFObject &Obj, DIDumpOptions DumpOpts);
0136 
0137 protected:
0138   void dumpRawEntry(const DWARFLocationEntry &Entry, raw_ostream &OS,
0139                     unsigned Indent, DIDumpOptions DumpOpts,
0140                     const DWARFObject &Obj) const override;
0141 
0142 private:
0143   uint16_t Version;
0144 };
0145 
0146 class ResolverError : public ErrorInfo<ResolverError> {
0147 public:
0148   static char ID;
0149 
0150   ResolverError(uint32_t Index, dwarf::LoclistEntries Kind) : Index(Index), Kind(Kind) {}
0151 
0152   void log(raw_ostream &OS) const override;
0153   std::error_code convertToErrorCode() const override {
0154     return llvm::errc::invalid_argument;
0155   }
0156 
0157 private:
0158   uint32_t Index;
0159   dwarf::LoclistEntries Kind;
0160 };
0161 
0162 } // end namespace llvm
0163 
0164 #endif // LLVM_DEBUGINFO_DWARF_DWARFDEBUGLOC_H