2017-08-17 23:26:39 +02:00
|
|
|
//==- llvm/CodeGen/DwarfAccelTable.h - Dwarf Accelerator Tables --*- C++ -*-==//
|
2011-11-07 10:18:42 +01:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for writing dwarf accelerator tables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-13 18:26:38 +02:00
|
|
|
#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFACCELTABLE_H
|
|
|
|
#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFACCELTABLE_H
|
2011-11-07 10:18:42 +01:00
|
|
|
|
2012-04-13 22:06:17 +02:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-08-17 23:26:39 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-12-04 08:12:27 +01:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2017-08-17 23:26:39 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-07 05:48:56 +02:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
2015-01-05 22:29:41 +01:00
|
|
|
#include "llvm/CodeGen/DIE.h"
|
2017-08-17 23:26:39 +02:00
|
|
|
#include "llvm/CodeGen/DwarfStringPoolEntry.h"
|
2011-11-07 10:18:42 +01:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2017-08-17 23:26:39 +02:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2011-11-07 10:18:42 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
2017-08-17 23:26:39 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2012-12-04 08:12:27 +01:00
|
|
|
#include <vector>
|
2011-11-07 10:18:42 +01:00
|
|
|
|
2011-11-07 10:24:32 +01:00
|
|
|
// The dwarf accelerator tables are an indirect hash table optimized
|
2011-11-07 10:18:42 +01:00
|
|
|
// for null lookup rather than access to known data. They are output into
|
|
|
|
// an on-disk format that looks like this:
|
|
|
|
//
|
|
|
|
// .-------------.
|
|
|
|
// | HEADER |
|
|
|
|
// |-------------|
|
|
|
|
// | BUCKETS |
|
|
|
|
// |-------------|
|
|
|
|
// | HASHES |
|
|
|
|
// |-------------|
|
|
|
|
// | OFFSETS |
|
|
|
|
// |-------------|
|
|
|
|
// | DATA |
|
|
|
|
// `-------------'
|
|
|
|
//
|
|
|
|
// where the header contains a magic number, version, type of hash function,
|
|
|
|
// the number of buckets, total number of hashes, and room for a special
|
|
|
|
// struct of data and the length of that struct.
|
|
|
|
//
|
|
|
|
// The buckets contain an index (e.g. 6) into the hashes array. The hashes
|
|
|
|
// section contains all of the 32-bit hash values in contiguous memory, and
|
|
|
|
// the offsets contain the offset into the data area for the particular
|
|
|
|
// hash.
|
2012-12-20 22:58:40 +01:00
|
|
|
//
|
2011-11-07 10:18:42 +01:00
|
|
|
// For a lookup example, we could hash a function name and take it modulo the
|
|
|
|
// number of buckets giving us our bucket. From there we take the bucket value
|
|
|
|
// as an index into the hashes table and look at each successive hash as long
|
|
|
|
// as the hash value is still the same modulo result (bucket value) as earlier.
|
|
|
|
// If we have a match we look at that same entry in the offsets table and
|
|
|
|
// grab the offset in the data for our final match.
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class AsmPrinter;
|
2014-11-13 00:48:14 +01:00
|
|
|
class DwarfDebug;
|
2012-12-20 22:58:40 +01:00
|
|
|
|
2013-09-11 20:05:11 +02:00
|
|
|
class DwarfAccelTable {
|
2011-11-07 10:18:42 +01:00
|
|
|
// Helper function to compute the number of buckets needed based on
|
|
|
|
// the number of unique hashes.
|
2017-08-17 23:26:39 +02:00
|
|
|
void ComputeBucketCount();
|
2012-12-20 22:58:40 +01:00
|
|
|
|
2011-11-07 10:18:42 +01:00
|
|
|
struct TableHeader {
|
2017-08-17 23:26:39 +02:00
|
|
|
uint32_t magic = MagicHash; // 'HASH' magic value to allow endian detection
|
|
|
|
uint16_t version = 1; // Version number.
|
|
|
|
uint16_t hash_function = dwarf::DW_hash_function_djb;
|
|
|
|
// The hash function enumeration that was used.
|
|
|
|
uint32_t bucket_count = 0; // The number of buckets in this hash table.
|
|
|
|
uint32_t hashes_count = 0; // The total number of unique hash values
|
|
|
|
// and hash data offsets in this table.
|
|
|
|
uint32_t header_data_len; // The bytes to skip to get to the hash
|
|
|
|
// indexes (buckets) for correct alignment.
|
2011-11-07 10:18:42 +01:00
|
|
|
// Also written to disk is the implementation specific header data.
|
|
|
|
|
|
|
|
static const uint32_t MagicHash = 0x48415348;
|
2012-12-20 22:58:40 +01:00
|
|
|
|
2017-08-17 23:26:39 +02:00
|
|
|
TableHeader(uint32_t data_len) : header_data_len(data_len) {}
|
2011-11-07 10:18:42 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2017-08-17 23:26:39 +02:00
|
|
|
void print(raw_ostream &OS) {
|
|
|
|
OS << "Magic: " << format("0x%x", magic) << "\n"
|
|
|
|
<< "Version: " << version << "\n"
|
|
|
|
<< "Hash Function: " << hash_function << "\n"
|
|
|
|
<< "Bucket Count: " << bucket_count << "\n"
|
|
|
|
<< "Header Data Length: " << header_data_len << "\n";
|
2011-11-07 10:18:42 +01:00
|
|
|
}
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2011-11-07 10:18:42 +01:00
|
|
|
void dump() { print(dbgs()); }
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
// The HeaderData describes the form of each set of data. In general this
|
|
|
|
// is as a list of atoms (atom_count) where each atom contains a type
|
|
|
|
// (AtomType type) of data, and an encoding form (form). In the case of
|
|
|
|
// data that is referenced via DW_FORM_ref_* the die_offset_base is
|
|
|
|
// used to describe the offset for all forms in the list of atoms.
|
|
|
|
// This also serves as a public interface of sorts.
|
|
|
|
// When written to disk this will have the form:
|
|
|
|
//
|
|
|
|
// uint32_t die_offset_base
|
|
|
|
// uint32_t atom_count
|
2012-12-20 22:58:40 +01:00
|
|
|
// atom_count Atoms
|
|
|
|
|
2011-11-07 10:18:42 +01:00
|
|
|
// Make these public so that they can be used as a general interface to
|
|
|
|
// the class.
|
|
|
|
struct Atom {
|
2013-09-05 20:20:16 +02:00
|
|
|
uint16_t type; // enum AtomType
|
2011-11-07 10:18:42 +01:00
|
|
|
uint16_t form; // DWARF DW_FORM_ defines
|
|
|
|
|
2016-10-23 21:39:16 +02:00
|
|
|
constexpr Atom(uint16_t type, uint16_t form) : type(type), form(form) {}
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2011-11-07 10:18:42 +01:00
|
|
|
#ifndef NDEBUG
|
2017-08-17 23:26:39 +02:00
|
|
|
void print(raw_ostream &OS) {
|
|
|
|
OS << "Type: " << dwarf::AtomTypeString(type) << "\n"
|
|
|
|
<< "Form: " << dwarf::FormEncodingString(form) << "\n";
|
2011-11-07 10:18:42 +01:00
|
|
|
}
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2013-09-05 18:46:43 +02:00
|
|
|
void dump() { print(dbgs()); }
|
2011-11-07 10:18:42 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2013-09-05 18:46:43 +02:00
|
|
|
private:
|
2011-11-07 10:18:42 +01:00
|
|
|
struct TableHeaderData {
|
|
|
|
uint32_t die_offset_base;
|
2014-08-11 04:18:15 +02:00
|
|
|
SmallVector<Atom, 3> Atoms;
|
2012-04-13 22:06:17 +02:00
|
|
|
|
|
|
|
TableHeaderData(ArrayRef<Atom> AtomList, uint32_t offset = 0)
|
2013-09-05 18:46:43 +02:00
|
|
|
: die_offset_base(offset), Atoms(AtomList.begin(), AtomList.end()) {}
|
2011-11-07 10:18:42 +01:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2017-08-17 23:26:39 +02:00
|
|
|
void print(raw_ostream &OS) {
|
|
|
|
OS << "die_offset_base: " << die_offset_base << "\n";
|
2011-11-07 10:18:42 +01:00
|
|
|
for (size_t i = 0; i < Atoms.size(); i++)
|
2017-08-17 23:26:39 +02:00
|
|
|
Atoms[i].print(OS);
|
2011-11-07 10:18:42 +01:00
|
|
|
}
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2013-09-05 18:46:43 +02:00
|
|
|
void dump() { print(dbgs()); }
|
2011-11-07 10:18:42 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2011-11-07 10:24:32 +01:00
|
|
|
// The data itself consists of a str_offset, a count of the DIEs in the
|
2011-11-07 10:18:42 +01:00
|
|
|
// hash and the offsets to the DIEs themselves.
|
|
|
|
// On disk each data section is ended with a 0 KeyType as the end of the
|
|
|
|
// hash chain.
|
|
|
|
// On output this looks like:
|
|
|
|
// uint32_t str_offset
|
|
|
|
// uint32_t hash_data_count
|
|
|
|
// HashData[hash_data_count]
|
2012-01-06 05:35:23 +01:00
|
|
|
public:
|
|
|
|
struct HashDataContents {
|
2013-11-19 23:51:04 +01:00
|
|
|
const DIE *Die; // Offsets
|
2012-01-06 05:35:23 +01:00
|
|
|
char Flags; // Specific flags to output
|
|
|
|
|
2013-11-19 23:51:04 +01:00
|
|
|
HashDataContents(const DIE *D, char Flags) : Die(D), Flags(Flags) {}
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2013-09-05 18:46:43 +02:00
|
|
|
#ifndef NDEBUG
|
2017-08-17 23:26:39 +02:00
|
|
|
void print(raw_ostream &OS) const {
|
|
|
|
OS << " Offset: " << Die->getOffset() << "\n"
|
|
|
|
<< " Tag: " << dwarf::TagString(Die->getTag()) << "\n"
|
|
|
|
<< " Flags: " << Flags << "\n";
|
2012-01-06 05:35:23 +01:00
|
|
|
}
|
2013-09-05 18:46:43 +02:00
|
|
|
#endif
|
2012-01-06 05:35:23 +01:00
|
|
|
};
|
2013-09-05 18:46:43 +02:00
|
|
|
|
2012-01-06 05:35:23 +01:00
|
|
|
private:
|
2014-04-26 00:21:35 +02:00
|
|
|
// String Data
|
|
|
|
struct DataArray {
|
2015-05-24 18:44:32 +02:00
|
|
|
DwarfStringPoolEntryRef Name;
|
2014-04-26 00:21:35 +02:00
|
|
|
std::vector<HashDataContents *> Values;
|
|
|
|
};
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2014-04-26 00:21:35 +02:00
|
|
|
friend struct HashData;
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2011-11-07 10:18:42 +01:00
|
|
|
struct HashData {
|
|
|
|
StringRef Str;
|
|
|
|
uint32_t HashValue;
|
|
|
|
MCSymbol *Sym;
|
2014-04-26 00:21:35 +02:00
|
|
|
DwarfAccelTable::DataArray &Data; // offsets
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2014-04-26 00:21:35 +02:00
|
|
|
HashData(StringRef S, DwarfAccelTable::DataArray &Data)
|
2013-09-05 18:46:43 +02:00
|
|
|
: Str(S), Data(Data) {
|
2017-09-28 20:10:52 +02:00
|
|
|
HashValue = dwarf::djbHash(S);
|
2011-11-07 10:18:42 +01:00
|
|
|
}
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2013-09-05 18:46:43 +02:00
|
|
|
#ifndef NDEBUG
|
2017-08-17 23:26:39 +02:00
|
|
|
void print(raw_ostream &OS) {
|
|
|
|
OS << "Name: " << Str << "\n";
|
|
|
|
OS << " Hash Value: " << format("0x%x", HashValue) << "\n";
|
|
|
|
OS << " Symbol: ";
|
2013-09-05 18:46:43 +02:00
|
|
|
if (Sym)
|
2017-08-17 23:26:39 +02:00
|
|
|
OS << *Sym;
|
2013-09-05 18:46:43 +02:00
|
|
|
else
|
2017-08-17 23:26:39 +02:00
|
|
|
OS << "<none>";
|
|
|
|
OS << "\n";
|
2014-04-26 00:21:35 +02:00
|
|
|
for (HashDataContents *C : Data.Values) {
|
2017-08-17 23:26:39 +02:00
|
|
|
OS << " Offset: " << C->Die->getOffset() << "\n";
|
|
|
|
OS << " Tag: " << dwarf::TagString(C->Die->getTag()) << "\n";
|
|
|
|
OS << " Flags: " << C->Flags << "\n";
|
2012-01-06 05:35:23 +01:00
|
|
|
}
|
2011-11-07 10:18:42 +01:00
|
|
|
}
|
2017-08-17 23:26:39 +02:00
|
|
|
|
2013-09-05 18:46:43 +02:00
|
|
|
void dump() { print(dbgs()); }
|
|
|
|
#endif
|
2011-11-07 10:18:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Internal Functions
|
|
|
|
void EmitHeader(AsmPrinter *);
|
|
|
|
void EmitBuckets(AsmPrinter *);
|
|
|
|
void EmitHashes(AsmPrinter *);
|
2015-03-10 17:58:10 +01:00
|
|
|
void emitOffsets(AsmPrinter *, const MCSymbol *);
|
|
|
|
void EmitData(AsmPrinter *, DwarfDebug *D);
|
2012-04-13 22:06:17 +02:00
|
|
|
|
|
|
|
// Allocator for HashData and HashDataContents.
|
|
|
|
BumpPtrAllocator Allocator;
|
|
|
|
|
2011-11-07 10:18:42 +01:00
|
|
|
// Output Variables
|
|
|
|
TableHeader Header;
|
|
|
|
TableHeaderData HeaderData;
|
2013-09-05 18:46:43 +02:00
|
|
|
std::vector<HashData *> Data;
|
2011-11-07 10:18:42 +01:00
|
|
|
|
2017-08-17 23:26:39 +02:00
|
|
|
using StringEntries = StringMap<DataArray, BumpPtrAllocator &>;
|
|
|
|
|
2011-11-07 10:18:42 +01:00
|
|
|
StringEntries Entries;
|
|
|
|
|
|
|
|
// Buckets/Hashes/Offsets
|
2017-08-17 23:26:39 +02:00
|
|
|
using HashList = std::vector<HashData *>;
|
|
|
|
using BucketList = std::vector<HashList>;
|
2011-11-07 10:18:42 +01:00
|
|
|
BucketList Buckets;
|
|
|
|
HashList Hashes;
|
2012-12-20 22:58:40 +01:00
|
|
|
|
2011-11-07 10:18:42 +01:00
|
|
|
// Public Implementation
|
2013-09-05 18:46:43 +02:00
|
|
|
public:
|
2012-04-13 22:06:17 +02:00
|
|
|
DwarfAccelTable(ArrayRef<DwarfAccelTable::Atom>);
|
2017-08-17 23:26:39 +02:00
|
|
|
DwarfAccelTable(const DwarfAccelTable &) = delete;
|
|
|
|
DwarfAccelTable &operator=(const DwarfAccelTable &) = delete;
|
|
|
|
|
2015-05-24 18:44:32 +02:00
|
|
|
void AddName(DwarfStringPoolEntryRef Name, const DIE *Die, char Flags = 0);
|
2013-05-11 20:24:28 +02:00
|
|
|
void FinalizeTable(AsmPrinter *, StringRef);
|
2015-03-10 17:58:10 +01:00
|
|
|
void emit(AsmPrinter *, const MCSymbol *, DwarfDebug *);
|
2011-11-07 10:18:42 +01:00
|
|
|
#ifndef NDEBUG
|
2017-08-17 23:26:39 +02:00
|
|
|
void print(raw_ostream &OS);
|
2011-11-07 10:18:42 +01:00
|
|
|
void dump() { print(dbgs()); }
|
|
|
|
#endif
|
|
|
|
};
|
2017-08-17 23:26:39 +02:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFACCELTABLE_H
|