blob: c58664f22342153f57e746493f3bc978c34d3c2c [file] [log] [blame]
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +00001//===-- DWARFCompileUnit.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_DWARFCOMPILEUNIT_H
11#define LLVM_DEBUGINFO_DWARFCOMPILEUNIT_H
12
13#include "DWARFDebugAbbrev.h"
14#include "DWARFDebugInfoEntry.h"
Alexey Samsonov5eae90d2012-09-04 08:12:33 +000015#include "DWARFDebugRangeList.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000016#include <vector>
17
18namespace llvm {
19
Eric Christopher82de10a2013-01-02 23:52:13 +000020class DWARFDebugAbbrev;
21class StringRef;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000022class raw_ostream;
Eric Christopher82de10a2013-01-02 23:52:13 +000023typedef DenseMap<uint64_t, std::pair<uint8_t, int64_t> > RelocAddrMap;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000024
25class DWARFCompileUnit {
Eric Christopher82de10a2013-01-02 23:52:13 +000026 const DWARFDebugAbbrev *Abbrev;
27 StringRef InfoSection;
28 StringRef AbbrevSection;
29 StringRef RangeSection;
30 StringRef StringSection;
Eric Christopherdd8e9f32013-01-07 19:32:41 +000031 StringRef StringOffsetSection;
Eric Christopher82de10a2013-01-02 23:52:13 +000032 const RelocAddrMap *RelocMap;
33 bool isLittleEndian;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000034
35 uint32_t Offset;
36 uint32_t Length;
37 uint16_t Version;
38 const DWARFAbbreviationDeclarationSet *Abbrevs;
39 uint8_t AddrSize;
40 uint64_t BaseAddr;
41 // The compile unit debug information entry item.
42 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
43public:
Eric Christopher82de10a2013-01-02 23:52:13 +000044
45 DWARFCompileUnit(const DWARFDebugAbbrev *DA, StringRef IS, StringRef AS,
Eric Christopherdd8e9f32013-01-07 19:32:41 +000046 StringRef RS, StringRef SS, StringRef SOS,
47 const RelocAddrMap *M, bool LE) :
Eric Christopher82de10a2013-01-02 23:52:13 +000048 Abbrev(DA), InfoSection(IS), AbbrevSection(AS),
Eric Christopherdd8e9f32013-01-07 19:32:41 +000049 RangeSection(RS), StringSection(SS), StringOffsetSection(SOS),
50 RelocMap(M), isLittleEndian(LE) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000051 clear();
52 }
53
Eric Christopher82de10a2013-01-02 23:52:13 +000054 StringRef getStringSection() const { return StringSection; }
Eric Christopherdd8e9f32013-01-07 19:32:41 +000055 StringRef getStringOffsetSection() const { return StringOffsetSection; }
Eric Christopher82de10a2013-01-02 23:52:13 +000056 const RelocAddrMap *getRelocMap() const { return RelocMap; }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000057 DataExtractor getDebugInfoExtractor() const;
58
59 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
60 uint32_t extract(uint32_t offset, DataExtractor debug_info_data,
61 const DWARFAbbreviationDeclarationSet *abbrevs);
62
63 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
Alexey Samsonova9543aa2012-07-04 09:42:54 +000064 /// hasn't already been done. Returns the number of DIEs parsed at this call.
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000065 size_t extractDIEsIfNeeded(bool cu_die_only);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +000066 /// extractRangeList - extracts the range list referenced by this compile
67 /// unit from .debug_ranges section. Returns true on success.
68 /// Requires that compile unit is already extracted.
69 bool extractRangeList(uint32_t RangeListOffset,
70 DWARFDebugRangeList &RangeList) const;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000071 void clear();
72 void dump(raw_ostream &OS);
73 uint32_t getOffset() const { return Offset; }
74 /// Size in bytes of the compile unit header.
75 uint32_t getSize() const { return 11; }
76 bool containsDIEOffset(uint32_t die_offset) const {
77 return die_offset >= getFirstDIEOffset() &&
78 die_offset < getNextCompileUnitOffset();
79 }
80 uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
81 uint32_t getNextCompileUnitOffset() const { return Offset + Length + 4; }
82 /// Size in bytes of the .debug_info data associated with this compile unit.
83 size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
84 uint32_t getLength() const { return Length; }
85 uint16_t getVersion() const { return Version; }
86 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
87 return Abbrevs;
88 }
89 uint8_t getAddressByteSize() const { return AddrSize; }
90 uint64_t getBaseAddress() const { return BaseAddr; }
91
92 void setBaseAddress(uint64_t base_addr) {
93 BaseAddr = base_addr;
94 }
95
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000096 const DWARFDebugInfoEntryMinimal *
97 getCompileUnitDIE(bool extract_cu_die_only = true) {
98 extractDIEsIfNeeded(extract_cu_die_only);
99 if (DieArray.empty())
100 return NULL;
101 return &DieArray[0];
102 }
103
Alexey Samsonov71d94f82012-07-19 07:03:58 +0000104 const char *getCompilationDir();
105
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000106 /// setDIERelations - We read in all of the DIE entries into our flat list
107 /// of DIE entries and now we need to go back through all of them and set the
108 /// parent, sibling and child pointers for quick DIE navigation.
109 void setDIERelations();
110
111 void addDIE(DWARFDebugInfoEntryMinimal &die) {
112 // The average bytes per DIE entry has been seen to be
113 // around 14-20 so lets pre-reserve the needed memory for
114 // our DIE entries accordingly. Search forward for "Compute
115 // average bytes per DIE" to see #if'ed out code that does
116 // that determination.
117
118 // Only reserve the memory if we are adding children of
119 // the main compile unit DIE. The compile unit DIE is always
120 // the first entry, so if our size is 1, then we are adding
121 // the first compile unit child DIE and should reserve
122 // the memory.
123 if (DieArray.empty())
124 DieArray.reserve(getDebugInfoSize() / 14);
125 DieArray.push_back(die);
126 }
Benjamin Kramer10df8062011-09-14 20:52:27 +0000127
128 void clearDIEs(bool keep_compile_unit_die);
129
130 void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
131 bool clear_dies_if_already_not_parsed);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000132
133 /// getInlinedChainForAddress - fetches inlined chain for a given address.
134 /// Returns empty chain if there is no subprogram containing address.
135 DWARFDebugInfoEntryMinimal::InlinedChain getInlinedChainForAddress(
136 uint64_t Address);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000137};
138
139}
140
141#endif