1226584Sdim//===-- DWARFCompileUnit.h --------------------------------------*- C++ -*-===//
2226584Sdim//
3226584Sdim//                     The LLVM Compiler Infrastructure
4226584Sdim//
5226584Sdim// This file is distributed under the University of Illinois Open Source
6226584Sdim// License. See LICENSE.TXT for details.
7226584Sdim//
8226584Sdim//===----------------------------------------------------------------------===//
9226584Sdim
10226584Sdim#ifndef LLVM_DEBUGINFO_DWARFCOMPILEUNIT_H
11226584Sdim#define LLVM_DEBUGINFO_DWARFCOMPILEUNIT_H
12226584Sdim
13226584Sdim#include "DWARFDebugAbbrev.h"
14226584Sdim#include "DWARFDebugInfoEntry.h"
15243830Sdim#include "DWARFDebugRangeList.h"
16249423Sdim#include "DWARFRelocMap.h"
17226584Sdim#include <vector>
18226584Sdim
19226584Sdimnamespace llvm {
20226584Sdim
21249423Sdimclass DWARFDebugAbbrev;
22249423Sdimclass StringRef;
23226584Sdimclass raw_ostream;
24226584Sdim
25226584Sdimclass DWARFCompileUnit {
26249423Sdim  const DWARFDebugAbbrev *Abbrev;
27249423Sdim  StringRef InfoSection;
28249423Sdim  StringRef AbbrevSection;
29249423Sdim  StringRef RangeSection;
30249423Sdim  StringRef StringSection;
31249423Sdim  StringRef StringOffsetSection;
32249423Sdim  StringRef AddrOffsetSection;
33249423Sdim  const RelocAddrMap *RelocMap;
34249423Sdim  bool isLittleEndian;
35226584Sdim
36226584Sdim  uint32_t Offset;
37226584Sdim  uint32_t Length;
38226584Sdim  uint16_t Version;
39226584Sdim  const DWARFAbbreviationDeclarationSet *Abbrevs;
40226584Sdim  uint8_t AddrSize;
41226584Sdim  uint64_t BaseAddr;
42226584Sdim  // The compile unit debug information entry item.
43226584Sdim  std::vector<DWARFDebugInfoEntryMinimal> DieArray;
44226584Sdimpublic:
45249423Sdim
46249423Sdim  DWARFCompileUnit(const DWARFDebugAbbrev *DA, StringRef IS, StringRef AS,
47249423Sdim                   StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
48249423Sdim                   const RelocAddrMap *M, bool LE) :
49249423Sdim    Abbrev(DA), InfoSection(IS), AbbrevSection(AS),
50249423Sdim    RangeSection(RS), StringSection(SS), StringOffsetSection(SOS),
51249423Sdim    AddrOffsetSection(AOS), RelocMap(M), isLittleEndian(LE) {
52226584Sdim    clear();
53226584Sdim  }
54226584Sdim
55249423Sdim  StringRef getStringSection() const { return StringSection; }
56249423Sdim  StringRef getStringOffsetSection() const { return StringOffsetSection; }
57249423Sdim  StringRef getAddrOffsetSection() const { return AddrOffsetSection; }
58249423Sdim  const RelocAddrMap *getRelocMap() const { return RelocMap; }
59226584Sdim  DataExtractor getDebugInfoExtractor() const;
60226584Sdim
61226584Sdim  bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
62226584Sdim  uint32_t extract(uint32_t offset, DataExtractor debug_info_data,
63226584Sdim                   const DWARFAbbreviationDeclarationSet *abbrevs);
64226584Sdim
65226584Sdim  /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
66239462Sdim  /// hasn't already been done. Returns the number of DIEs parsed at this call.
67226584Sdim  size_t extractDIEsIfNeeded(bool cu_die_only);
68243830Sdim  /// extractRangeList - extracts the range list referenced by this compile
69243830Sdim  /// unit from .debug_ranges section. Returns true on success.
70243830Sdim  /// Requires that compile unit is already extracted.
71243830Sdim  bool extractRangeList(uint32_t RangeListOffset,
72243830Sdim                        DWARFDebugRangeList &RangeList) const;
73226584Sdim  void clear();
74226584Sdim  void dump(raw_ostream &OS);
75226584Sdim  uint32_t getOffset() const { return Offset; }
76226584Sdim  /// Size in bytes of the compile unit header.
77226584Sdim  uint32_t getSize() const { return 11; }
78226584Sdim  bool containsDIEOffset(uint32_t die_offset) const {
79226584Sdim    return die_offset >= getFirstDIEOffset() &&
80226584Sdim      die_offset < getNextCompileUnitOffset();
81226584Sdim  }
82226584Sdim  uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
83226584Sdim  uint32_t getNextCompileUnitOffset() const { return Offset + Length + 4; }
84226584Sdim  /// Size in bytes of the .debug_info data associated with this compile unit.
85226584Sdim  size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
86226584Sdim  uint32_t getLength() const { return Length; }
87226584Sdim  uint16_t getVersion() const { return Version; }
88226584Sdim  const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
89226584Sdim    return Abbrevs;
90226584Sdim  }
91226584Sdim  uint8_t getAddressByteSize() const { return AddrSize; }
92226584Sdim  uint64_t getBaseAddress() const { return BaseAddr; }
93226584Sdim
94226584Sdim  void setBaseAddress(uint64_t base_addr) {
95226584Sdim    BaseAddr = base_addr;
96226584Sdim  }
97226584Sdim
98226584Sdim  const DWARFDebugInfoEntryMinimal *
99226584Sdim  getCompileUnitDIE(bool extract_cu_die_only = true) {
100226584Sdim    extractDIEsIfNeeded(extract_cu_die_only);
101226584Sdim    if (DieArray.empty())
102226584Sdim      return NULL;
103226584Sdim    return &DieArray[0];
104226584Sdim  }
105226584Sdim
106239462Sdim  const char *getCompilationDir();
107239462Sdim
108226584Sdim  /// setDIERelations - We read in all of the DIE entries into our flat list
109226584Sdim  /// of DIE entries and now we need to go back through all of them and set the
110226584Sdim  /// parent, sibling and child pointers for quick DIE navigation.
111226584Sdim  void setDIERelations();
112226584Sdim
113226584Sdim  void addDIE(DWARFDebugInfoEntryMinimal &die) {
114226584Sdim    // The average bytes per DIE entry has been seen to be
115226584Sdim    // around 14-20 so lets pre-reserve the needed memory for
116226584Sdim    // our DIE entries accordingly. Search forward for "Compute
117226584Sdim    // average bytes per DIE" to see #if'ed out code that does
118226584Sdim    // that determination.
119226584Sdim
120226584Sdim    // Only reserve the memory if we are adding children of
121226584Sdim    // the main compile unit DIE. The compile unit DIE is always
122226584Sdim    // the first entry, so if our size is 1, then we are adding
123226584Sdim    // the first compile unit child DIE and should reserve
124226584Sdim    // the memory.
125226584Sdim    if (DieArray.empty())
126226584Sdim      DieArray.reserve(getDebugInfoSize() / 14);
127226584Sdim    DieArray.push_back(die);
128226584Sdim  }
129226584Sdim
130226584Sdim  void clearDIEs(bool keep_compile_unit_die);
131226584Sdim
132226584Sdim  void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
133226584Sdim                              bool clear_dies_if_already_not_parsed);
134243830Sdim
135243830Sdim  /// getInlinedChainForAddress - fetches inlined chain for a given address.
136243830Sdim  /// Returns empty chain if there is no subprogram containing address.
137243830Sdim  DWARFDebugInfoEntryMinimal::InlinedChain getInlinedChainForAddress(
138243830Sdim      uint64_t Address);
139226584Sdim};
140226584Sdim
141226584Sdim}
142226584Sdim
143226584Sdim#endif
144