1 //==-- llvm/CodeGen/DwarfAccelTable.h - Dwarf Accelerator Tables -*- 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 // This file contains support for writing dwarf accelerator tables.
12 //===----------------------------------------------------------------------===//
14 #ifndef CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__
15 #define CODEGEN_ASMPRINTER_DWARFACCELTABLE_H__
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/FormattedStream.h"
28 // The apple dwarf accelerator tables are an indirect hash table optimized
29 // for null lookup rather than access to known data. They are output into
30 // an on-disk format that looks like this:
44 // where the header contains a magic number, version, type of hash function,
45 // the number of buckets, total number of hashes, and room for a special
46 // struct of data and the length of that struct.
48 // The buckets contain an index (e.g. 6) into the hashes array. The hashes
49 // section contains all of the 32-bit hash values in contiguous memory, and
50 // the offsets contain the offset into the data area for the particular
53 // For a lookup example, we could hash a function name and take it modulo the
54 // number of buckets giving us our bucket. From there we take the bucket value
55 // as an index into the hashes table and look at each successive hash as long
56 // as the hash value is still the same modulo result (bucket value) as earlier.
57 // If we have a match we look at that same entry in the offsets table and
58 // grab the offset in the data for our final match.
66 class DwarfAccelTable {
68 enum HashFunctionType {
72 static uint32_t HashDJB (const char *s) {
74 for (unsigned char c = *s; c; c = *++s)
75 h = ((h << 5) + h) + c;
79 // Helper function to compute the number of buckets needed based on
80 // the number of unique hashes.
81 void ComputeBucketCount (void);
84 uint32_t magic; // 'HASH' magic value to allow endian detection
85 uint16_t version; // Version number.
86 uint16_t hash_function; // The hash function enumeration that was used.
87 uint32_t bucket_count; // The number of buckets in this hash table.
88 uint32_t hashes_count; // The total number of unique hash values
89 // and hash data offsets in this table.
90 uint32_t header_data_len; // The bytes to skip to get to the hash
91 // indexes (buckets) for correct alignment.
92 // Also written to disk is the implementation specific header data.
94 static const uint32_t MagicHash = 0x48415348;
96 TableHeader (uint32_t data_len) :
97 magic (MagicHash), version (1), hash_function (eHashFunctionDJB),
98 bucket_count (0), hashes_count (0), header_data_len (data_len)
102 void print(raw_ostream &O) {
103 O << "Magic: " << format("0x%x", magic) << "\n"
104 << "Version: " << version << "\n"
105 << "Hash Function: " << hash_function << "\n"
106 << "Bucket Count: " << bucket_count << "\n"
107 << "Header Data Length: " << header_data_len << "\n";
109 void dump() { print(dbgs()); }
114 // The HeaderData describes the form of each set of data. In general this
115 // is as a list of atoms (atom_count) where each atom contains a type
116 // (AtomType type) of data, and an encoding form (form). In the case of
117 // data that is referenced via DW_FORM_ref_* the die_offset_base is
118 // used to describe the offset for all forms in the list of atoms.
119 // This also serves as a public interface of sorts.
120 // When written to disk this will have the form:
122 // uint32_t die_offset_base
123 // uint32_t atom_count
127 eAtomTypeDIEOffset = 1u, // DIE offset, check form for encoding
128 eAtomTypeCUOffset = 2u, // DIE offset of the compiler unit header that
129 // contains the item in question
130 eAtomTypeTag = 3u, // DW_TAG_xxx value, should be encoded as
131 // DW_FORM_data1 (if no tags exceed 255) or
133 eAtomTypeNameFlags = 4u, // Flags from enum NameFlags
134 eAtomTypeTypeFlags = 5u // Flags from enum TypeFlags
137 // Make these public so that they can be used as a general interface to
140 AtomType type; // enum AtomType
141 uint16_t form; // DWARF DW_FORM_ defines
143 Atom(AtomType type, uint16_t form) : type(type), form(form) {};
144 static const char * AtomTypeString(enum AtomType);
146 void print(raw_ostream &O) {
147 O << "Type: " << dwarf::TagString(type) << "\n"
148 << "Form: " << dwarf::FormEncodingString(form) << "\n";
157 struct TableHeaderData {
159 uint32_t die_offset_base;
160 std::vector<Atom> Atoms;
162 TableHeaderData(DwarfAccelTable::Atom Atom, uint32_t offset = 0)
163 : die_offset_base(offset) {
164 Atoms.push_back(Atom);
168 void print (raw_ostream &O) {
169 O << "die_offset_base: " << die_offset_base << "\n";
170 for (size_t i = 0; i < Atoms.size(); i++)
179 // The data itself consists of a str_offset (to deal with collisions in
180 // some magical way? this looks like the KeyType from the spec, which
181 // should mean an integer compare on read), a count of the DIEs in the
182 // hash and the offsets to the DIEs themselves.
183 // On disk each data section is ended with a 0 KeyType as the end of the
185 // On output this looks like:
186 // uint32_t str_offset
187 // uint32_t hash_data_count
188 // HashData[hash_data_count]
193 std::vector<uint32_t> DIEOffsets; // offsets
194 HashData(StringRef S) : Str(S) {
195 HashValue = DwarfAccelTable::HashDJB(S.str().c_str());
197 void addOffset(uint32_t off) { DIEOffsets.push_back(off); }
199 void print(raw_ostream &O) {
200 O << "Name: " << Str << "\n";
201 O << " Hash Value: " << format("0x%x", HashValue) << "\n";
203 if (Sym) Sym->print(O);
206 for (size_t i = 0; i < DIEOffsets.size(); i++)
207 O << " Offset: " << DIEOffsets[i] << "\n";
215 DwarfAccelTable(const DwarfAccelTable&); // DO NOT IMPLEMENT
216 void operator=(const DwarfAccelTable&); // DO NOT IMPLEMENT
218 // Internal Functions
219 void EmitHeader(AsmPrinter *);
220 void EmitBuckets(AsmPrinter *);
221 void EmitHashes(AsmPrinter *);
222 void EmitOffsets(AsmPrinter *, MCSymbol *);
223 void EmitData(AsmPrinter *, DwarfDebug *D);
227 TableHeaderData HeaderData;
228 std::vector<HashData*> Data;
231 typedef std::vector<DIE*> DIEArray;
232 typedef StringMap<DIEArray> StringEntries;
233 StringEntries Entries;
235 // Buckets/Hashes/Offsets
236 typedef std::vector<HashData*> HashList;
237 typedef std::vector<HashList> BucketList;
241 // Public Implementation
243 DwarfAccelTable(DwarfAccelTable::Atom Atom);
244 void AddName(StringRef, DIE*);
245 void FinalizeTable(AsmPrinter *, const char *);
246 void Emit(AsmPrinter *, MCSymbol *, DwarfDebug *);
248 void print(raw_ostream &O);
249 void dump() { print(dbgs()); }