1 //===-- DWARFCompileUnit.h --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_DEBUGINFO_DWARFCOMPILEUNIT_H
11 #define LLVM_DEBUGINFO_DWARFCOMPILEUNIT_H
13 #include "llvm/ADT/OwningPtr.h"
14 #include "DWARFDebugAbbrev.h"
15 #include "DWARFDebugInfoEntry.h"
16 #include "DWARFDebugRangeList.h"
17 #include "DWARFRelocMap.h"
26 class DWARFDebugAbbrev;
30 class DWARFCompileUnit {
31 DWARFCompileUnit(DWARFCompileUnit const &) LLVM_DELETED_FUNCTION;
32 DWARFCompileUnit &operator=(DWARFCompileUnit const &) LLVM_DELETED_FUNCTION;
34 const DWARFDebugAbbrev *Abbrev;
35 StringRef InfoSection;
36 StringRef AbbrevSection;
37 StringRef RangeSection;
38 uint32_t RangeSectionBase;
39 StringRef StringSection;
40 StringRef StringOffsetSection;
41 StringRef AddrOffsetSection;
42 uint32_t AddrOffsetSectionBase;
43 const RelocAddrMap *RelocMap;
49 const DWARFAbbreviationDeclarationSet *Abbrevs;
52 // The compile unit debug information entry items.
53 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
56 OwningPtr<object::ObjectFile> DWOFile;
57 OwningPtr<DWARFContext> DWOContext;
58 DWARFCompileUnit *DWOCU;
60 DWOHolder(object::ObjectFile *DWOFile);
61 DWARFCompileUnit *getCU() const { return DWOCU; }
63 OwningPtr<DWOHolder> DWO;
67 DWARFCompileUnit(const DWARFDebugAbbrev *DA, StringRef IS, StringRef AS,
68 StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
69 const RelocAddrMap *M, bool LE) :
70 Abbrev(DA), InfoSection(IS), AbbrevSection(AS),
71 RangeSection(RS), StringSection(SS), StringOffsetSection(SOS),
72 AddrOffsetSection(AOS), RelocMap(M), isLittleEndian(LE) {
76 StringRef getStringSection() const { return StringSection; }
77 StringRef getStringOffsetSection() const { return StringOffsetSection; }
78 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
79 AddrOffsetSection = AOS;
80 AddrOffsetSectionBase = Base;
82 void setRangesSection(StringRef RS, uint32_t Base) {
84 RangeSectionBase = Base;
87 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
88 // FIXME: Result should be uint64_t in DWARF64.
89 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
91 DataExtractor getDebugInfoExtractor() const {
92 return DataExtractor(InfoSection, isLittleEndian, AddrSize);
94 DataExtractor getStringExtractor() const {
95 return DataExtractor(StringSection, false, 0);
98 const RelocAddrMap *getRelocMap() const { return RelocMap; }
100 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
101 uint32_t extract(uint32_t offset, DataExtractor debug_info_data,
102 const DWARFAbbreviationDeclarationSet *abbrevs);
104 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
105 /// hasn't already been done. Returns the number of DIEs parsed at this call.
106 size_t extractDIEsIfNeeded(bool CUDieOnly);
107 /// extractRangeList - extracts the range list referenced by this compile
108 /// unit from .debug_ranges section. Returns true on success.
109 /// Requires that compile unit is already extracted.
110 bool extractRangeList(uint32_t RangeListOffset,
111 DWARFDebugRangeList &RangeList) const;
113 void dump(raw_ostream &OS);
114 uint32_t getOffset() const { return Offset; }
115 /// Size in bytes of the compile unit header.
116 uint32_t getSize() const { return 11; }
117 bool containsDIEOffset(uint32_t die_offset) const {
118 return die_offset >= getFirstDIEOffset() &&
119 die_offset < getNextCompileUnitOffset();
121 uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
122 uint32_t getNextCompileUnitOffset() const { return Offset + Length + 4; }
123 /// Size in bytes of the .debug_info data associated with this compile unit.
124 size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
125 uint32_t getLength() const { return Length; }
126 uint16_t getVersion() const { return Version; }
127 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
130 uint8_t getAddressByteSize() const { return AddrSize; }
131 uint64_t getBaseAddress() const { return BaseAddr; }
133 void setBaseAddress(uint64_t base_addr) {
134 BaseAddr = base_addr;
137 const DWARFDebugInfoEntryMinimal *
138 getCompileUnitDIE(bool extract_cu_die_only = true) {
139 extractDIEsIfNeeded(extract_cu_die_only);
140 return DieArray.empty() ? NULL : &DieArray[0];
143 const char *getCompilationDir();
146 /// setDIERelations - We read in all of the DIE entries into our flat list
147 /// of DIE entries and now we need to go back through all of them and set the
148 /// parent, sibling and child pointers for quick DIE navigation.
149 void setDIERelations();
151 void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
152 bool clear_dies_if_already_not_parsed,
153 uint32_t CUOffsetInAranges);
155 /// getInlinedChainForAddress - fetches inlined chain for a given address.
156 /// Returns empty chain if there is no subprogram containing address. The
157 /// chain is valid as long as parsed compile unit DIEs are not cleared.
158 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
161 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
162 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
163 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
164 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
165 void clearDIEs(bool KeepCUDie);
167 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
168 /// it was actually constructed.
171 /// getSubprogramForAddress - Returns subprogram DIE with address range
172 /// encompassing the provided address. The pointer is alive as long as parsed
173 /// compile unit DIEs are not cleared.
174 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);