2017-08-18 05:26:39 +08:00
|
|
|
//===- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework ---------*- C++ -*-===//
|
2014-04-24 02:54:00 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-04-24 02:54:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
|
|
|
|
#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "DwarfStringPool.h"
|
2014-04-24 02:54:00 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2017-08-18 05:26:39 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
Make a DWARF generator so we can unit test DWARF APIs with gtest.
The only tests we have for the DWARF parser are the tests that use llvm-dwarfdump and expect output from textual dumps.
More DWARF parser modification are coming in the next few weeks and I wanted to add tests that can verify that we can encode and decode all form types, as well as test some other basic DWARF APIs where we ask DIE objects for their children and siblings.
DwarfGenerator.cpp was added in the lib/CodeGen directory. This file contains the code necessary to easily create DWARF for tests:
dwarfgen::Generator DG;
Triple Triple("x86_64--");
bool success = DG.init(Triple, Version);
if (!success)
return;
dwarfgen::CompileUnit &CU = DG.addCompileUnit();
dwarfgen::DIE CUDie = CU.getUnitDIE();
CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
// ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
StringRef FileBytes = DG.generate();
MemoryBufferRef FileBuffer(FileBytes, "dwarf");
auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
EXPECT_TRUE((bool)Obj);
DWARFContextInMemory DwarfContext(*Obj.get());
This code is backed by the AsmPrinter code that emits DWARF for the actual compiler.
While adding unit tests it was discovered that DIEValue that used DIEEntry as their values had bugs where DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref8, and DW_FORM_ref_udata forms were not supported. These are all now supported. Added support for DW_FORM_string so we can emit inlined C strings.
Centralized the code to unique abbreviations into a new DIEAbbrevSet class and made both the dwarfgen::Generator and the llvm::DwarfFile classes use the new class.
Fixed comments in the llvm::DIE class so that the Offset is known to be the compile/type unit offset.
DIEInteger now supports more DW_FORM values.
There are also unit tests that cover:
Encoding and decoding all form types and values
Encoding and decoding all reference types (DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8, DW_FORM_ref_udata, DW_FORM_ref_addr) including cross compile unit references with that go forward one compile unit and backward on compile unit.
Differential Revision: https://reviews.llvm.org/D27326
llvm-svn: 289010
2016-12-08 09:03:48 +08:00
|
|
|
#include "llvm/CodeGen/DIE.h"
|
2017-11-04 04:24:19 +08:00
|
|
|
#include "llvm/IR/Metadata.h"
|
2014-04-24 02:54:00 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2018-02-07 06:17:45 +08:00
|
|
|
#include <map>
|
2014-04-24 02:54:00 +08:00
|
|
|
#include <memory>
|
2017-08-18 05:26:39 +08:00
|
|
|
#include <utility>
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2017-08-18 05:26:39 +08:00
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
class AsmPrinter;
|
2018-08-17 23:22:04 +08:00
|
|
|
class DbgEntity;
|
2014-10-23 08:16:05 +08:00
|
|
|
class DbgVariable;
|
2018-08-17 23:22:04 +08:00
|
|
|
class DbgLabel;
|
2016-02-12 03:57:46 +08:00
|
|
|
class DwarfCompileUnit;
|
2014-04-24 02:54:00 +08:00
|
|
|
class DwarfUnit;
|
2014-10-23 08:16:05 +08:00
|
|
|
class LexicalScope;
|
2014-04-24 02:54:00 +08:00
|
|
|
class MCSection;
|
2017-08-18 05:26:39 +08:00
|
|
|
|
2018-10-20 15:36:39 +08:00
|
|
|
// Data structure to hold a range for range lists.
|
|
|
|
class RangeSpan {
|
|
|
|
public:
|
|
|
|
RangeSpan(MCSymbol *S, MCSymbol *E) : Start(S), End(E) {}
|
|
|
|
const MCSymbol *getStart() const { return Start; }
|
|
|
|
const MCSymbol *getEnd() const { return End; }
|
|
|
|
void setEnd(const MCSymbol *E) { End = E; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const MCSymbol *Start, *End;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RangeSpanList {
|
|
|
|
private:
|
|
|
|
// Index for locating within the debug_range section this particular span.
|
|
|
|
MCSymbol *RangeSym;
|
2018-11-08 08:35:54 +08:00
|
|
|
const DwarfCompileUnit *CU;
|
2018-10-20 15:36:39 +08:00
|
|
|
// List of ranges.
|
|
|
|
SmallVector<RangeSpan, 2> Ranges;
|
|
|
|
|
|
|
|
public:
|
2018-11-08 08:35:54 +08:00
|
|
|
RangeSpanList(MCSymbol *Sym, const DwarfCompileUnit &CU,
|
2018-10-20 15:36:39 +08:00
|
|
|
SmallVector<RangeSpan, 2> Ranges)
|
2018-11-08 08:35:54 +08:00
|
|
|
: RangeSym(Sym), CU(&CU), Ranges(std::move(Ranges)) {}
|
2018-10-20 15:36:39 +08:00
|
|
|
MCSymbol *getSym() const { return RangeSym; }
|
2018-11-08 08:35:54 +08:00
|
|
|
const DwarfCompileUnit &getCU() const { return *CU; }
|
2018-10-20 15:36:39 +08:00
|
|
|
const SmallVectorImpl<RangeSpan> &getRanges() const { return Ranges; }
|
|
|
|
void addRange(RangeSpan Range) { Ranges.push_back(Range); }
|
|
|
|
};
|
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
class DwarfFile {
|
|
|
|
// Target of Dwarf emission, used for sizing of abbreviations.
|
|
|
|
AsmPrinter *Asm;
|
|
|
|
|
2015-05-28 06:31:41 +08:00
|
|
|
BumpPtrAllocator AbbrevAllocator;
|
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
// Used to uniquely define abbreviations.
|
Make a DWARF generator so we can unit test DWARF APIs with gtest.
The only tests we have for the DWARF parser are the tests that use llvm-dwarfdump and expect output from textual dumps.
More DWARF parser modification are coming in the next few weeks and I wanted to add tests that can verify that we can encode and decode all form types, as well as test some other basic DWARF APIs where we ask DIE objects for their children and siblings.
DwarfGenerator.cpp was added in the lib/CodeGen directory. This file contains the code necessary to easily create DWARF for tests:
dwarfgen::Generator DG;
Triple Triple("x86_64--");
bool success = DG.init(Triple, Version);
if (!success)
return;
dwarfgen::CompileUnit &CU = DG.addCompileUnit();
dwarfgen::DIE CUDie = CU.getUnitDIE();
CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
// ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
StringRef FileBytes = DG.generate();
MemoryBufferRef FileBuffer(FileBytes, "dwarf");
auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
EXPECT_TRUE((bool)Obj);
DWARFContextInMemory DwarfContext(*Obj.get());
This code is backed by the AsmPrinter code that emits DWARF for the actual compiler.
While adding unit tests it was discovered that DIEValue that used DIEEntry as their values had bugs where DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref8, and DW_FORM_ref_udata forms were not supported. These are all now supported. Added support for DW_FORM_string so we can emit inlined C strings.
Centralized the code to unique abbreviations into a new DIEAbbrevSet class and made both the dwarfgen::Generator and the llvm::DwarfFile classes use the new class.
Fixed comments in the llvm::DIE class so that the Offset is known to be the compile/type unit offset.
DIEInteger now supports more DW_FORM values.
There are also unit tests that cover:
Encoding and decoding all form types and values
Encoding and decoding all reference types (DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8, DW_FORM_ref_udata, DW_FORM_ref_addr) including cross compile unit references with that go forward one compile unit and backward on compile unit.
Differential Revision: https://reviews.llvm.org/D27326
llvm-svn: 289010
2016-12-08 09:03:48 +08:00
|
|
|
DIEAbbrevSet Abbrevs;
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
// A pointer to all units in the section.
|
2016-02-12 03:57:46 +08:00
|
|
|
SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2014-04-26 05:34:35 +08:00
|
|
|
DwarfStringPool StrPool;
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2018-10-20 15:36:39 +08:00
|
|
|
// List of range lists for a given compile unit, separate from the ranges for
|
|
|
|
// the CU itself.
|
|
|
|
SmallVector<RangeSpanList, 1> CURangeLists;
|
|
|
|
|
2018-01-27 02:52:58 +08:00
|
|
|
/// DWARF v5: The symbol that designates the start of the contribution to
|
|
|
|
/// the string offsets table. The contribution is shared by all units.
|
|
|
|
MCSymbol *StringOffsetsStartSym = nullptr;
|
|
|
|
|
2018-07-13 02:18:21 +08:00
|
|
|
/// DWARF v5: The symbol that designates the base of the range list table.
|
|
|
|
/// The table is shared by all units.
|
|
|
|
MCSymbol *RnglistsTableBaseSym = nullptr;
|
|
|
|
|
2018-10-26 19:25:12 +08:00
|
|
|
/// DWARF v5: The symbol that designates the base of the locations list table.
|
|
|
|
/// The table is shared by all units.
|
|
|
|
MCSymbol *LoclistsTableBaseSym = nullptr;
|
|
|
|
|
2018-02-07 06:17:45 +08:00
|
|
|
/// The variables of a lexical scope.
|
|
|
|
struct ScopeVars {
|
|
|
|
/// We need to sort Args by ArgNo and check for duplicates. This could also
|
|
|
|
/// be implemented as a list or vector + std::lower_bound().
|
|
|
|
std::map<unsigned, DbgVariable *> Args;
|
|
|
|
SmallVector<DbgVariable *, 8> Locals;
|
|
|
|
};
|
|
|
|
/// Collection of DbgVariables of each lexical scope.
|
|
|
|
DenseMap<LexicalScope *, ScopeVars> ScopeVariables;
|
2014-10-25 01:57:34 +08:00
|
|
|
|
2018-08-17 23:22:04 +08:00
|
|
|
/// Collection of DbgLabels of each lexical scope.
|
|
|
|
using LabelList = SmallVector<DbgLabel *, 4>;
|
|
|
|
DenseMap<LexicalScope *, LabelList> ScopeLabels;
|
|
|
|
|
2014-11-02 01:21:26 +08:00
|
|
|
// Collection of abstract subprogram DIEs.
|
|
|
|
DenseMap<const MDNode *, DIE *> AbstractSPDies;
|
2018-08-17 23:22:04 +08:00
|
|
|
DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
|
2014-11-02 01:21:26 +08:00
|
|
|
|
2014-11-05 06:12:18 +08:00
|
|
|
/// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
|
|
|
|
/// be shared across CUs, that is why we keep the map here instead
|
|
|
|
/// of in DwarfCompileUnit.
|
2015-04-30 00:38:44 +08:00
|
|
|
DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
|
2014-11-05 06:12:18 +08:00
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
public:
|
2015-03-04 10:30:17 +08:00
|
|
|
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2016-02-12 03:57:46 +08:00
|
|
|
const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
|
|
|
|
return CUs;
|
|
|
|
}
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2018-11-08 08:35:54 +08:00
|
|
|
std::pair<uint32_t, RangeSpanList *> addRange(const DwarfCompileUnit &CU,
|
2018-10-20 15:36:39 +08:00
|
|
|
SmallVector<RangeSpan, 2> R);
|
|
|
|
|
|
|
|
/// getRangeLists - Get the vector of range lists.
|
|
|
|
const SmallVectorImpl<RangeSpanList> &getRangeLists() const {
|
|
|
|
return CURangeLists;
|
|
|
|
}
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Compute the size and offset of a DIE given an incoming Offset.
|
2014-04-24 02:54:00 +08:00
|
|
|
unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Compute the size and offset of all the DIEs.
|
2014-04-24 02:54:00 +08:00
|
|
|
void computeSizeAndOffsets();
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Compute the size and offset of all the DIEs in the given unit.
|
2016-02-12 03:57:46 +08:00
|
|
|
/// \returns The size of the root DIE.
|
|
|
|
unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Add a unit to the list of CUs.
|
2016-02-12 03:57:46 +08:00
|
|
|
void addUnit(std::unique_ptr<DwarfCompileUnit> U);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Emit all of the units to the section listed with the given
|
2014-04-24 02:54:00 +08:00
|
|
|
/// abbreviation section.
|
2015-03-11 00:58:10 +08:00
|
|
|
void emitUnits(bool UseOffsets);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Emit the given unit to its section.
|
2019-03-19 17:17:28 +08:00
|
|
|
void emitUnit(DwarfUnit *U, bool UseOffsets);
|
2016-02-12 03:57:46 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Emit a set of abbreviations to the specific section.
|
2015-05-22 03:20:38 +08:00
|
|
|
void emitAbbrevs(MCSection *);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2018-01-27 02:52:58 +08:00
|
|
|
/// Emit all of the strings to the section given. If OffsetSection is
|
|
|
|
/// non-null, emit a table of string offsets to it. If UseRelativeOffsets
|
|
|
|
/// is false, emit absolute offsets to the strings. Otherwise, emit
|
|
|
|
/// relocatable references to the strings if they are supported by the target.
|
|
|
|
void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr,
|
|
|
|
bool UseRelativeOffsets = false);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// Returns the string pool.
|
2014-04-26 05:34:35 +08:00
|
|
|
DwarfStringPool &getStringPool() { return StrPool; }
|
2014-10-23 08:16:05 +08:00
|
|
|
|
2018-01-27 02:52:58 +08:00
|
|
|
MCSymbol *getStringOffsetsStartSym() const { return StringOffsetsStartSym; }
|
|
|
|
void setStringOffsetsStartSym(MCSymbol *Sym) { StringOffsetsStartSym = Sym; }
|
|
|
|
|
2018-07-13 02:18:21 +08:00
|
|
|
MCSymbol *getRnglistsTableBaseSym() const { return RnglistsTableBaseSym; }
|
|
|
|
void setRnglistsTableBaseSym(MCSymbol *Sym) { RnglistsTableBaseSym = Sym; }
|
|
|
|
|
2018-10-26 19:25:12 +08:00
|
|
|
MCSymbol *getLoclistsTableBaseSym() const { return LoclistsTableBaseSym; }
|
|
|
|
void setLoclistsTableBaseSym(MCSymbol *Sym) { LoclistsTableBaseSym = Sym; }
|
|
|
|
|
2015-02-11 07:18:28 +08:00
|
|
|
/// \returns false if the variable was merged with a previous one.
|
|
|
|
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
|
2014-10-25 01:57:34 +08:00
|
|
|
|
2018-08-17 23:22:04 +08:00
|
|
|
void addScopeLabel(LexicalScope *LS, DbgLabel *Label);
|
|
|
|
|
2018-02-07 06:17:45 +08:00
|
|
|
DenseMap<LexicalScope *, ScopeVars> &getScopeVariables() {
|
2014-10-25 01:57:34 +08:00
|
|
|
return ScopeVariables;
|
|
|
|
}
|
2014-11-02 01:21:26 +08:00
|
|
|
|
2018-10-13 03:41:05 +08:00
|
|
|
DenseMap<LexicalScope *, LabelList> &getScopeLabels() {
|
|
|
|
return ScopeLabels;
|
|
|
|
}
|
2018-08-17 23:22:04 +08:00
|
|
|
|
2014-11-02 01:21:26 +08:00
|
|
|
DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
|
|
|
|
return AbstractSPDies;
|
|
|
|
}
|
2017-08-18 05:26:39 +08:00
|
|
|
|
2018-08-17 23:22:04 +08:00
|
|
|
DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
|
|
|
|
return AbstractEntities;
|
2017-05-12 09:13:45 +08:00
|
|
|
}
|
2014-11-05 06:12:18 +08:00
|
|
|
|
|
|
|
void insertDIE(const MDNode *TypeMD, DIE *Die) {
|
2015-04-30 00:38:44 +08:00
|
|
|
DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
|
2014-11-05 06:12:18 +08:00
|
|
|
}
|
2017-08-18 05:26:39 +08:00
|
|
|
|
2014-11-05 06:12:18 +08:00
|
|
|
DIE *getDIE(const MDNode *TypeMD) {
|
2015-04-30 00:38:44 +08:00
|
|
|
return DITypeNodeToDieMap.lookup(TypeMD);
|
2014-11-05 06:12:18 +08:00
|
|
|
}
|
2014-04-24 02:54:00 +08:00
|
|
|
};
|
2017-08-18 05:26:39 +08:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
|