blob: ba638dff212d899afd9648281282a9f7f3e35a05 [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;
31 const RelocAddrMap *RelocMap;
32 bool isLittleEndian;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000033
34 uint32_t Offset;
35 uint32_t Length;
36 uint16_t Version;
37 const DWARFAbbreviationDeclarationSet *Abbrevs;
38 uint8_t AddrSize;
39 uint64_t BaseAddr;
40 // The compile unit debug information entry item.
41 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
42public:
Eric Christopher82de10a2013-01-02 23:52:13 +000043
44 DWARFCompileUnit(const DWARFDebugAbbrev *DA, StringRef IS, StringRef AS,
45 StringRef RS, StringRef SS, const RelocAddrMap *M, bool LE) :
46 Abbrev(DA), InfoSection(IS), AbbrevSection(AS),
47 RangeSection(RS), StringSection(SS), RelocMap(M), isLittleEndian(LE) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000048 clear();
49 }
50
Eric Christopher82de10a2013-01-02 23:52:13 +000051 StringRef getStringSection() const { return StringSection; }
52 const RelocAddrMap *getRelocMap() const { return RelocMap; }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000053 DataExtractor getDebugInfoExtractor() const;
54
55 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
56 uint32_t extract(uint32_t offset, DataExtractor debug_info_data,
57 const DWARFAbbreviationDeclarationSet *abbrevs);
58
59 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
Alexey Samsonova9543aa2012-07-04 09:42:54 +000060 /// hasn't already been done. Returns the number of DIEs parsed at this call.
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000061 size_t extractDIEsIfNeeded(bool cu_die_only);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +000062 /// extractRangeList - extracts the range list referenced by this compile
63 /// unit from .debug_ranges section. Returns true on success.
64 /// Requires that compile unit is already extracted.
65 bool extractRangeList(uint32_t RangeListOffset,
66 DWARFDebugRangeList &RangeList) const;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000067 void clear();
68 void dump(raw_ostream &OS);
69 uint32_t getOffset() const { return Offset; }
70 /// Size in bytes of the compile unit header.
71 uint32_t getSize() const { return 11; }
72 bool containsDIEOffset(uint32_t die_offset) const {
73 return die_offset >= getFirstDIEOffset() &&
74 die_offset < getNextCompileUnitOffset();
75 }
76 uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
77 uint32_t getNextCompileUnitOffset() const { return Offset + Length + 4; }
78 /// Size in bytes of the .debug_info data associated with this compile unit.
79 size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
80 uint32_t getLength() const { return Length; }
81 uint16_t getVersion() const { return Version; }
82 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
83 return Abbrevs;
84 }
85 uint8_t getAddressByteSize() const { return AddrSize; }
86 uint64_t getBaseAddress() const { return BaseAddr; }
87
88 void setBaseAddress(uint64_t base_addr) {
89 BaseAddr = base_addr;
90 }
91
Benjamin Kramerfe80f1d2011-09-15 18:02:20 +000092 const DWARFDebugInfoEntryMinimal *
93 getCompileUnitDIE(bool extract_cu_die_only = true) {
94 extractDIEsIfNeeded(extract_cu_die_only);
95 if (DieArray.empty())
96 return NULL;
97 return &DieArray[0];
98 }
99
Alexey Samsonov71d94f82012-07-19 07:03:58 +0000100 const char *getCompilationDir();
101
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000102 /// setDIERelations - We read in all of the DIE entries into our flat list
103 /// of DIE entries and now we need to go back through all of them and set the
104 /// parent, sibling and child pointers for quick DIE navigation.
105 void setDIERelations();
106
107 void addDIE(DWARFDebugInfoEntryMinimal &die) {
108 // The average bytes per DIE entry has been seen to be
109 // around 14-20 so lets pre-reserve the needed memory for
110 // our DIE entries accordingly. Search forward for "Compute
111 // average bytes per DIE" to see #if'ed out code that does
112 // that determination.
113
114 // Only reserve the memory if we are adding children of
115 // the main compile unit DIE. The compile unit DIE is always
116 // the first entry, so if our size is 1, then we are adding
117 // the first compile unit child DIE and should reserve
118 // the memory.
119 if (DieArray.empty())
120 DieArray.reserve(getDebugInfoSize() / 14);
121 DieArray.push_back(die);
122 }
Benjamin Kramer10df8062011-09-14 20:52:27 +0000123
124 void clearDIEs(bool keep_compile_unit_die);
125
126 void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
127 bool clear_dies_if_already_not_parsed);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000128
129 /// getInlinedChainForAddress - fetches inlined chain for a given address.
130 /// Returns empty chain if there is no subprogram containing address.
131 DWARFDebugInfoEntryMinimal::InlinedChain getInlinedChainForAddress(
132 uint64_t Address);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000133};
134
135}
136
137#endif