blob: 471da36af1cf01b34c06dc799406043745496015 [file] [log] [blame]
David Blaikie07e22442013-09-23 22:44:40 +00001//===-- DWARFUnit.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_DWARFUNIT_H
11#define LLVM_DEBUGINFO_DWARFUNIT_H
12
David Blaikie07e22442013-09-23 22:44:40 +000013#include "DWARFDebugAbbrev.h"
14#include "DWARFDebugInfoEntry.h"
15#include "DWARFDebugRangeList.h"
16#include "DWARFRelocMap.h"
17#include <vector>
18
19namespace llvm {
20
21namespace object {
22class ObjectFile;
23}
24
25class DWARFDebugAbbrev;
26class StringRef;
27class raw_ostream;
28
29class DWARFUnit {
30 const DWARFDebugAbbrev *Abbrev;
31 StringRef InfoSection;
David Blaikie07e22442013-09-23 22:44:40 +000032 StringRef RangeSection;
33 uint32_t RangeSectionBase;
34 StringRef StringSection;
35 StringRef StringOffsetSection;
36 StringRef AddrOffsetSection;
37 uint32_t AddrOffsetSectionBase;
38 const RelocAddrMap *RelocMap;
39 bool isLittleEndian;
40
41 uint32_t Offset;
42 uint32_t Length;
43 uint16_t Version;
44 const DWARFAbbreviationDeclarationSet *Abbrevs;
45 uint8_t AddrSize;
46 uint64_t BaseAddr;
47 // The compile unit debug information entry items.
48 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
49
50 class DWOHolder {
Ahmed Charles56440fd2014-03-06 05:51:42 +000051 std::unique_ptr<object::ObjectFile> DWOFile;
52 std::unique_ptr<DWARFContext> DWOContext;
David Blaikie07e22442013-09-23 22:44:40 +000053 DWARFUnit *DWOU;
54 public:
55 DWOHolder(object::ObjectFile *DWOFile);
56 DWARFUnit *getUnit() const { return DWOU; }
57 };
Ahmed Charles56440fd2014-03-06 05:51:42 +000058 std::unique_ptr<DWOHolder> DWO;
David Blaikie07e22442013-09-23 22:44:40 +000059
60protected:
61 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
Alexey Samsonov19f76f22014-04-24 23:08:56 +000062 /// Size in bytes of the unit header.
63 virtual uint32_t getHeaderSize() const { return 11; }
David Blaikie07e22442013-09-23 22:44:40 +000064
65public:
Alexey Samsonov7682f812014-04-24 22:51:03 +000066 DWARFUnit(const DWARFDebugAbbrev *DA, StringRef IS, StringRef RS,
67 StringRef SS, StringRef SOS, StringRef AOS, const RelocAddrMap *M,
68 bool LE);
David Blaikie07e22442013-09-23 22:44:40 +000069
70 virtual ~DWARFUnit();
71
72 StringRef getStringSection() const { return StringSection; }
73 StringRef getStringOffsetSection() const { return StringOffsetSection; }
74 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
75 AddrOffsetSection = AOS;
76 AddrOffsetSectionBase = Base;
77 }
78 void setRangesSection(StringRef RS, uint32_t Base) {
79 RangeSection = RS;
80 RangeSectionBase = Base;
81 }
82
83 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
84 // FIXME: Result should be uint64_t in DWARF64.
85 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
86
87 DataExtractor getDebugInfoExtractor() const {
88 return DataExtractor(InfoSection, isLittleEndian, AddrSize);
89 }
90 DataExtractor getStringExtractor() const {
91 return DataExtractor(StringSection, false, 0);
92 }
93
94 const RelocAddrMap *getRelocMap() const { return RelocMap; }
95
96 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
David Blaikie07e22442013-09-23 22:44:40 +000097
David Blaikie07e22442013-09-23 22:44:40 +000098 /// extractRangeList - extracts the range list referenced by this compile
99 /// unit from .debug_ranges section. Returns true on success.
100 /// Requires that compile unit is already extracted.
101 bool extractRangeList(uint32_t RangeListOffset,
102 DWARFDebugRangeList &RangeList) const;
103 void clear();
104 uint32_t getOffset() const { return Offset; }
David Blaikie07e22442013-09-23 22:44:40 +0000105 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
David Blaikie07e22442013-09-23 22:44:40 +0000106 uint32_t getLength() const { return Length; }
107 uint16_t getVersion() const { return Version; }
108 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
109 return Abbrevs;
110 }
111 uint8_t getAddressByteSize() const { return AddrSize; }
112 uint64_t getBaseAddress() const { return BaseAddr; }
113
114 void setBaseAddress(uint64_t base_addr) {
115 BaseAddr = base_addr;
116 }
117
118 const DWARFDebugInfoEntryMinimal *
119 getCompileUnitDIE(bool extract_cu_die_only = true) {
120 extractDIEsIfNeeded(extract_cu_die_only);
Craig Toppere73658d2014-04-28 04:05:08 +0000121 return DieArray.empty() ? nullptr : &DieArray[0];
David Blaikie07e22442013-09-23 22:44:40 +0000122 }
123
124 const char *getCompilationDir();
125 uint64_t getDWOId();
126
Alexey Samsonov762343d2014-04-18 17:25:46 +0000127 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
David Blaikie07e22442013-09-23 22:44:40 +0000128
129 /// getInlinedChainForAddress - fetches inlined chain for a given address.
130 /// Returns empty chain if there is no subprogram containing address. The
131 /// chain is valid as long as parsed compile unit DIEs are not cleared.
132 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
133
134private:
Alexey Samsonov19f76f22014-04-24 23:08:56 +0000135 /// Size in bytes of the .debug_info data associated with this compile unit.
136 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
137
Alexey Samsonov0428d122013-10-31 18:05:02 +0000138 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
139 /// hasn't already been done. Returns the number of DIEs parsed at this call.
140 size_t extractDIEsIfNeeded(bool CUDieOnly);
David Blaikie07e22442013-09-23 22:44:40 +0000141 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
142 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
143 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
Alexey Samsonov0428d122013-10-31 18:05:02 +0000144 /// setDIERelations - We read in all of the DIE entries into our flat list
145 /// of DIE entries and now we need to go back through all of them and set the
146 /// parent, sibling and child pointers for quick DIE navigation.
147 void setDIERelations();
David Blaikie07e22442013-09-23 22:44:40 +0000148 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
149 void clearDIEs(bool KeepCUDie);
150
151 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
152 /// it was actually constructed.
153 bool parseDWO();
154
155 /// getSubprogramForAddress - Returns subprogram DIE with address range
156 /// encompassing the provided address. The pointer is alive as long as parsed
157 /// compile unit DIEs are not cleared.
158 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);
159};
160
161}
162
163#endif