1//===-- 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"
15#include <vector>
16
17namespace llvm {
18
19class DWARFContext;
20class raw_ostream;
21
22class DWARFCompileUnit {
23  DWARFContext &Context;
24
25  uint32_t Offset;
26  uint32_t Length;
27  uint16_t Version;
28  const DWARFAbbreviationDeclarationSet *Abbrevs;
29  uint8_t AddrSize;
30  uint64_t BaseAddr;
31  // The compile unit debug information entry item.
32  std::vector<DWARFDebugInfoEntryMinimal> DieArray;
33public:
34  DWARFCompileUnit(DWARFContext &context) : Context(context) {
35    clear();
36  }
37
38  DWARFContext &getContext() const { return Context; }
39  DataExtractor getDebugInfoExtractor() const;
40
41  bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
42  uint32_t extract(uint32_t offset, DataExtractor debug_info_data,
43                   const DWARFAbbreviationDeclarationSet *abbrevs);
44
45  /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
46  /// hasn't already been done.
47  size_t extractDIEsIfNeeded(bool cu_die_only);
48  void clear();
49  void dump(raw_ostream &OS);
50  uint32_t getOffset() const { return Offset; }
51  /// Size in bytes of the compile unit header.
52  uint32_t getSize() const { return 11; }
53  bool containsDIEOffset(uint32_t die_offset) const {
54    return die_offset >= getFirstDIEOffset() &&
55      die_offset < getNextCompileUnitOffset();
56  }
57  uint32_t getFirstDIEOffset() const { return Offset + getSize(); }
58  uint32_t getNextCompileUnitOffset() const { return Offset + Length + 4; }
59  /// Size in bytes of the .debug_info data associated with this compile unit.
60  size_t getDebugInfoSize() const { return Length + 4 - getSize(); }
61  uint32_t getLength() const { return Length; }
62  uint16_t getVersion() const { return Version; }
63  const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
64    return Abbrevs;
65  }
66  uint8_t getAddressByteSize() const { return AddrSize; }
67  uint64_t getBaseAddress() const { return BaseAddr; }
68
69  void setBaseAddress(uint64_t base_addr) {
70    BaseAddr = base_addr;
71  }
72
73  const DWARFDebugInfoEntryMinimal *
74  getCompileUnitDIE(bool extract_cu_die_only = true) {
75    extractDIEsIfNeeded(extract_cu_die_only);
76    if (DieArray.empty())
77      return NULL;
78    return &DieArray[0];
79  }
80
81  /// setDIERelations - We read in all of the DIE entries into our flat list
82  /// of DIE entries and now we need to go back through all of them and set the
83  /// parent, sibling and child pointers for quick DIE navigation.
84  void setDIERelations();
85
86  void addDIE(DWARFDebugInfoEntryMinimal &die) {
87    // The average bytes per DIE entry has been seen to be
88    // around 14-20 so lets pre-reserve the needed memory for
89    // our DIE entries accordingly. Search forward for "Compute
90    // average bytes per DIE" to see #if'ed out code that does
91    // that determination.
92
93    // Only reserve the memory if we are adding children of
94    // the main compile unit DIE. The compile unit DIE is always
95    // the first entry, so if our size is 1, then we are adding
96    // the first compile unit child DIE and should reserve
97    // the memory.
98    if (DieArray.empty())
99      DieArray.reserve(getDebugInfoSize() / 14);
100    DieArray.push_back(die);
101  }
102
103  void clearDIEs(bool keep_compile_unit_die);
104
105  void buildAddressRangeTable(DWARFDebugAranges *debug_aranges,
106                              bool clear_dies_if_already_not_parsed);
107};
108
109}
110
111#endif
112