DWARFContext.h revision 243830
1//===-- DWARFContext.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_DWARFCONTEXT_H
11#define LLVM_DEBUGINFO_DWARFCONTEXT_H
12
13#include "DWARFCompileUnit.h"
14#include "DWARFDebugAranges.h"
15#include "DWARFDebugLine.h"
16#include "DWARFDebugRangeList.h"
17#include "llvm/DebugInfo/DIContext.h"
18#include "llvm/ADT/OwningPtr.h"
19#include "llvm/ADT/SmallVector.h"
20
21namespace llvm {
22
23/// DWARFContext
24/// This data structure is the top level entity that deals with dwarf debug
25/// information parsing. The actual data is supplied through pure virtual
26/// methods that a concrete implementation provides.
27class DWARFContext : public DIContext {
28  bool IsLittleEndian;
29  const RelocAddrMap &RelocMap;
30
31  SmallVector<DWARFCompileUnit, 1> CUs;
32  OwningPtr<DWARFDebugAbbrev> Abbrev;
33  OwningPtr<DWARFDebugAranges> Aranges;
34  OwningPtr<DWARFDebugLine> Line;
35
36  DWARFContext(DWARFContext &) LLVM_DELETED_FUNCTION;
37  DWARFContext &operator=(DWARFContext &) LLVM_DELETED_FUNCTION;
38
39  /// Read compile units from the debug_info section and store them in CUs.
40  void parseCompileUnits();
41protected:
42  DWARFContext(bool isLittleEndian, const RelocAddrMap &Map) :
43    IsLittleEndian(isLittleEndian), RelocMap(Map) {}
44public:
45  virtual void dump(raw_ostream &OS);
46
47  /// Get the number of compile units in this context.
48  unsigned getNumCompileUnits() {
49    if (CUs.empty())
50      parseCompileUnits();
51    return CUs.size();
52  }
53  /// Get the compile unit at the specified index for this compile unit.
54  DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
55    if (CUs.empty())
56      parseCompileUnits();
57    return &CUs[index];
58  }
59
60  /// Get a pointer to the parsed DebugAbbrev object.
61  const DWARFDebugAbbrev *getDebugAbbrev();
62
63  /// Get a pointer to the parsed DebugAranges object.
64  const DWARFDebugAranges *getDebugAranges();
65
66  /// Get a pointer to a parsed line table corresponding to a compile unit.
67  const DWARFDebugLine::LineTable *
68  getLineTableForCompileUnit(DWARFCompileUnit *cu);
69
70  virtual DILineInfo getLineInfoForAddress(uint64_t Address,
71      DILineInfoSpecifier Specifier = DILineInfoSpecifier());
72  virtual DIInliningInfo getInliningInfoForAddress(uint64_t Address,
73      DILineInfoSpecifier Specifier = DILineInfoSpecifier());
74
75  bool isLittleEndian() const { return IsLittleEndian; }
76  const RelocAddrMap &relocMap() const { return RelocMap; }
77
78  virtual StringRef getInfoSection() = 0;
79  virtual StringRef getAbbrevSection() = 0;
80  virtual StringRef getARangeSection() = 0;
81  virtual StringRef getLineSection() = 0;
82  virtual StringRef getStringSection() = 0;
83  virtual StringRef getRangeSection() = 0;
84
85  static bool isSupportedVersion(unsigned version) {
86    return version == 2 || version == 3;
87  }
88private:
89  /// Return the compile unit that includes an offset (relative to .debug_info).
90  DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
91
92  /// Return the compile unit which contains instruction with provided
93  /// address.
94  DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
95};
96
97/// DWARFContextInMemory is the simplest possible implementation of a
98/// DWARFContext. It assumes all content is available in memory and stores
99/// pointers to it.
100class DWARFContextInMemory : public DWARFContext {
101  virtual void anchor();
102  StringRef InfoSection;
103  StringRef AbbrevSection;
104  StringRef ARangeSection;
105  StringRef LineSection;
106  StringRef StringSection;
107  StringRef RangeSection;
108public:
109  DWARFContextInMemory(bool isLittleEndian,
110                       StringRef infoSection,
111                       StringRef abbrevSection,
112                       StringRef aRangeSection,
113                       StringRef lineSection,
114                       StringRef stringSection,
115                       StringRef rangeSection,
116                       const RelocAddrMap &Map = RelocAddrMap())
117    : DWARFContext(isLittleEndian, Map),
118      InfoSection(infoSection),
119      AbbrevSection(abbrevSection),
120      ARangeSection(aRangeSection),
121      LineSection(lineSection),
122      StringSection(stringSection),
123      RangeSection(rangeSection)
124    {}
125
126  virtual StringRef getInfoSection() { return InfoSection; }
127  virtual StringRef getAbbrevSection() { return AbbrevSection; }
128  virtual StringRef getARangeSection() { return ARangeSection; }
129  virtual StringRef getLineSection() { return LineSection; }
130  virtual StringRef getStringSection() { return StringSection; }
131  virtual StringRef getRangeSection() { return RangeSection; }
132};
133
134}
135
136#endif
137