Imported Upstream version 5.18.0.205

Former-commit-id: 7f59f7e792705db773f1caecdaa823092f4e2927
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-11-16 08:20:38 +00:00
parent 5cd5df71cc
commit 8e12397d70
28486 changed files with 3867013 additions and 66 deletions

View File

@ -0,0 +1,21 @@
set(LLVM_LINK_COMPONENTS
${LLVM_TARGETS_TO_BUILD}
AsmPrinter
DebugInfoDWARF
MC
Object
ObjectYAML
Support
)
set(DebugInfoSources
DwarfGenerator.cpp
DWARFDebugInfoTest.cpp
DWARFFormValueTest.cpp
)
add_llvm_unittest(DebugInfoDWARFTests
${DebugInfoSources}
)
target_link_libraries(DebugInfoDWARFTests PRIVATE LLVMTestingSupport)

View File

@ -0,0 +1 @@
cb7bf82d86f603bf5f6b0e88a0b0cf586ba4a1c6

View File

@ -0,0 +1,177 @@
//===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/LEB128.h"
#include "gtest/gtest.h"
#include <climits>
using namespace llvm;
using namespace dwarf;
namespace {
TEST(DWARFFormValue, FixedFormSizes) {
Optional<uint8_t> RefSize;
Optional<uint8_t> AddrSize;
// Test 32 bit DWARF version 2 with 4 byte addresses.
DWARFFormParams Params_2_4_32 = {2, 4, DWARF32};
RefSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_2_4_32);
AddrSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_2_4_32);
EXPECT_TRUE(RefSize.hasValue());
EXPECT_TRUE(AddrSize.hasValue());
EXPECT_EQ(*RefSize, *AddrSize);
// Test 32 bit DWARF version 2 with 8 byte addresses.
DWARFFormParams Params_2_8_32 = {2, 8, DWARF32};
RefSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_2_8_32);
AddrSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_2_8_32);
EXPECT_TRUE(RefSize.hasValue());
EXPECT_TRUE(AddrSize.hasValue());
EXPECT_EQ(*RefSize, *AddrSize);
// DW_FORM_ref_addr is 4 bytes in DWARF 32 in DWARF version 3 and beyond.
DWARFFormParams Params_3_4_32 = {3, 4, DWARF32};
RefSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_3_4_32);
EXPECT_TRUE(RefSize.hasValue());
EXPECT_EQ(*RefSize, 4);
DWARFFormParams Params_4_4_32 = {4, 4, DWARF32};
RefSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_4_4_32);
EXPECT_TRUE(RefSize.hasValue());
EXPECT_EQ(*RefSize, 4);
DWARFFormParams Params_5_4_32 = {5, 4, DWARF32};
RefSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_5_4_32);
EXPECT_TRUE(RefSize.hasValue());
EXPECT_EQ(*RefSize, 4);
// DW_FORM_ref_addr is 8 bytes in DWARF 64 in DWARF version 3 and beyond.
DWARFFormParams Params_3_8_64 = {3, 8, DWARF64};
RefSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_3_8_64);
EXPECT_TRUE(RefSize.hasValue());
EXPECT_EQ(*RefSize, 8);
DWARFFormParams Params_4_8_64 = {4, 8, DWARF64};
RefSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_4_8_64);
EXPECT_TRUE(RefSize.hasValue());
EXPECT_EQ(*RefSize, 8);
DWARFFormParams Params_5_8_64 = {5, 8, DWARF64};
RefSize = DWARFFormValue::getFixedByteSize(DW_FORM_ref_addr, Params_5_8_64);
EXPECT_TRUE(RefSize.hasValue());
EXPECT_EQ(*RefSize, 8);
}
bool isFormClass(dwarf::Form Form, DWARFFormValue::FormClass FC) {
return DWARFFormValue(Form).isFormClass(FC);
}
TEST(DWARFFormValue, FormClass) {
EXPECT_TRUE(isFormClass(DW_FORM_addr, DWARFFormValue::FC_Address));
EXPECT_FALSE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Address));
EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Constant));
EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_SectionOffset));
EXPECT_TRUE(
isFormClass(DW_FORM_sec_offset, DWARFFormValue::FC_SectionOffset));
EXPECT_TRUE(isFormClass(DW_FORM_GNU_str_index, DWARFFormValue::FC_String));
EXPECT_TRUE(isFormClass(DW_FORM_GNU_addr_index, DWARFFormValue::FC_Address));
EXPECT_FALSE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Address));
EXPECT_TRUE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Reference));
EXPECT_TRUE(isFormClass(DW_FORM_ref_sig8, DWARFFormValue::FC_Reference));
}
template<typename RawTypeT>
DWARFFormValue createDataXFormValue(dwarf::Form Form, RawTypeT Value) {
char Raw[sizeof(RawTypeT)];
memcpy(Raw, &Value, sizeof(RawTypeT));
uint32_t Offset = 0;
DWARFFormValue Result(Form);
DWARFDataExtractor Data(StringRef(Raw, sizeof(RawTypeT)),
sys::IsLittleEndianHost, sizeof(void *));
Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
return Result;
}
DWARFFormValue createULEBFormValue(uint64_t Value) {
SmallString<10> RawData;
raw_svector_ostream OS(RawData);
encodeULEB128(Value, OS);
uint32_t Offset = 0;
DWARFFormValue Result(DW_FORM_udata);
DWARFDataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void *));
Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
return Result;
}
DWARFFormValue createSLEBFormValue(int64_t Value) {
SmallString<10> RawData;
raw_svector_ostream OS(RawData);
encodeSLEB128(Value, OS);
uint32_t Offset = 0;
DWARFFormValue Result(DW_FORM_sdata);
DWARFDataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void *));
Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
return Result;
}
TEST(DWARFFormValue, SignedConstantForms) {
// Check that we correctly sign extend fixed size forms.
auto Sign1 = createDataXFormValue<uint8_t>(DW_FORM_data1, -123);
auto Sign2 = createDataXFormValue<uint16_t>(DW_FORM_data2, -12345);
auto Sign4 = createDataXFormValue<uint32_t>(DW_FORM_data4, -123456789);
auto Sign8 = createDataXFormValue<uint64_t>(DW_FORM_data8, -1);
EXPECT_EQ(Sign1.getAsSignedConstant().getValue(), -123);
EXPECT_EQ(Sign2.getAsSignedConstant().getValue(), -12345);
EXPECT_EQ(Sign4.getAsSignedConstant().getValue(), -123456789);
EXPECT_EQ(Sign8.getAsSignedConstant().getValue(), -1);
// Check that we can handle big positive values, but that we return
// an error just over the limit.
auto UMax = createULEBFormValue(LLONG_MAX);
auto TooBig = createULEBFormValue(uint64_t(LLONG_MAX) + 1);
EXPECT_EQ(UMax.getAsSignedConstant().getValue(), LLONG_MAX);
EXPECT_EQ(TooBig.getAsSignedConstant().hasValue(), false);
// Sanity check some other forms.
auto Data1 = createDataXFormValue<uint8_t>(DW_FORM_data1, 120);
auto Data2 = createDataXFormValue<uint16_t>(DW_FORM_data2, 32000);
auto Data4 = createDataXFormValue<uint32_t>(DW_FORM_data4, 2000000000);
auto Data8 = createDataXFormValue<uint64_t>(DW_FORM_data8, 0x1234567812345678LL);
auto LEBMin = createSLEBFormValue(LLONG_MIN);
auto LEBMax = createSLEBFormValue(LLONG_MAX);
auto LEB1 = createSLEBFormValue(-42);
auto LEB2 = createSLEBFormValue(42);
EXPECT_EQ(Data1.getAsSignedConstant().getValue(), 120);
EXPECT_EQ(Data2.getAsSignedConstant().getValue(), 32000);
EXPECT_EQ(Data4.getAsSignedConstant().getValue(), 2000000000);
EXPECT_EQ(Data8.getAsSignedConstant().getValue(), 0x1234567812345678LL);
EXPECT_EQ(LEBMin.getAsSignedConstant().getValue(), LLONG_MIN);
EXPECT_EQ(LEBMax.getAsSignedConstant().getValue(), LLONG_MAX);
EXPECT_EQ(LEB1.getAsSignedConstant().getValue(), -42);
EXPECT_EQ(LEB2.getAsSignedConstant().getValue(), 42);
// Data16 is a little tricky.
char Cksum[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
DWARFFormValue Data16(DW_FORM_data16);
DWARFDataExtractor DE16(StringRef(Cksum, 16), sys::IsLittleEndianHost,
sizeof(void *));
uint32_t Offset = 0;
Data16.extractValue(DE16, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
SmallString<32> Str;
raw_svector_ostream Res(Str);
Data16.dump(Res, DIDumpOptions());
EXPECT_EQ(memcmp(Str.data(), "000102030405060708090a0b0c0d0e0f", 32), 0);
}
} // end anonymous namespace

View File

@ -0,0 +1,269 @@
//===--- unittests/DebugInfo/DWARF/DwarfGenerator.cpp -----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "DwarfGenerator.h"
#include "../lib/CodeGen/AsmPrinter/DwarfStringPool.h"
#include "llvm/ADT/Triple.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/DIE.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCTargetOptionsCommandFlags.def"
#include "llvm/PassAnalysisSupport.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
using namespace llvm;
using namespace dwarf;
namespace {} // end anonymous namespace
//===----------------------------------------------------------------------===//
/// dwarfgen::DIE implementation.
//===----------------------------------------------------------------------===//
unsigned dwarfgen::DIE::computeSizeAndOffsets(unsigned Offset) {
auto &DG = CU->getGenerator();
return Die->computeOffsetsAndAbbrevs(DG.getAsmPrinter(), DG.getAbbrevSet(),
Offset);
}
void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, uint64_t U) {
auto &DG = CU->getGenerator();
Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
DIEInteger(U));
}
void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form,
StringRef String) {
auto &DG = CU->getGenerator();
if (Form == DW_FORM_string) {
Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
new (DG.getAllocator())
DIEInlineString(String, DG.getAllocator()));
} else {
Die->addValue(
DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
DIEString(DG.getStringPool().getEntry(*DG.getAsmPrinter(), String)));
}
}
void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form,
dwarfgen::DIE &RefDie) {
auto &DG = CU->getGenerator();
Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
DIEEntry(*RefDie.Die));
}
void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, const void *P,
size_t S) {
auto &DG = CU->getGenerator();
DIEBlock *Block = new (DG.getAllocator()) DIEBlock;
for (size_t I = 0; I < S; ++I)
Block->addValue(
DG.getAllocator(), (dwarf::Attribute)0, dwarf::DW_FORM_data1,
DIEInteger(
(const_cast<uint8_t *>(static_cast<const uint8_t *>(P)))[I]));
Block->ComputeSize(DG.getAsmPrinter());
Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
Block);
}
void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form) {
auto &DG = CU->getGenerator();
assert(Form == DW_FORM_flag_present);
Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
DIEInteger(1));
}
dwarfgen::DIE dwarfgen::DIE::addChild(dwarf::Tag Tag) {
auto &DG = CU->getGenerator();
return dwarfgen::DIE(CU,
&Die->addChild(llvm::DIE::get(DG.getAllocator(), Tag)));
}
dwarfgen::DIE dwarfgen::CompileUnit::getUnitDIE() {
return dwarfgen::DIE(this, &DU.getUnitDie());
}
//===----------------------------------------------------------------------===//
/// dwarfgen::Generator implementation.
//===----------------------------------------------------------------------===//
dwarfgen::Generator::Generator()
: MAB(nullptr), MCE(nullptr), MS(nullptr), StringPool(nullptr),
Abbreviations(Allocator) {}
dwarfgen::Generator::~Generator() = default;
llvm::Expected<std::unique_ptr<dwarfgen::Generator>>
dwarfgen::Generator::create(Triple TheTriple, uint16_t DwarfVersion) {
std::unique_ptr<dwarfgen::Generator> GenUP(new dwarfgen::Generator());
llvm::Error error = GenUP->init(TheTriple, DwarfVersion);
if (error)
return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(error));
return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(GenUP));
}
llvm::Error dwarfgen::Generator::init(Triple TheTriple, uint16_t V) {
Version = V;
std::string ErrorStr;
std::string TripleName;
// Get the target.
const Target *TheTarget =
TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
if (!TheTarget)
return make_error<StringError>(ErrorStr, inconvertibleErrorCode());
TripleName = TheTriple.getTriple();
// Create all the MC Objects.
MRI.reset(TheTarget->createMCRegInfo(TripleName));
if (!MRI)
return make_error<StringError>(Twine("no register info for target ") +
TripleName,
inconvertibleErrorCode());
MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
if (!MAI)
return make_error<StringError>("no asm info for target " + TripleName,
inconvertibleErrorCode());
MOFI.reset(new MCObjectFileInfo);
MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, *MC);
MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
if (!MSTI)
return make_error<StringError>("no subtarget info for target " + TripleName,
inconvertibleErrorCode());
MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
if (!MAB)
return make_error<StringError>("no asm backend for target " + TripleName,
inconvertibleErrorCode());
MII.reset(TheTarget->createMCInstrInfo());
if (!MII)
return make_error<StringError>("no instr info info for target " +
TripleName,
inconvertibleErrorCode());
MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
if (!MCE)
return make_error<StringError>("no code emitter for target " + TripleName,
inconvertibleErrorCode());
Stream = make_unique<raw_svector_ostream>(FileBytes);
MS = TheTarget->createMCObjectStreamer(
TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB), *Stream,
std::unique_ptr<MCCodeEmitter>(MCE), *MSTI, MCOptions.MCRelaxAll,
MCOptions.MCIncrementalLinkerCompatible,
/*DWARFMustBeAtTheEnd*/ false);
if (!MS)
return make_error<StringError>("no object streamer for target " +
TripleName,
inconvertibleErrorCode());
// Finally create the AsmPrinter we'll use to emit the DIEs.
TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
None));
if (!TM)
return make_error<StringError>("no target machine for target " + TripleName,
inconvertibleErrorCode());
Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
if (!Asm)
return make_error<StringError>("no asm printer for target " + TripleName,
inconvertibleErrorCode());
// Set the DWARF version correctly on all classes that we use.
MC->setDwarfVersion(Version);
Asm->setDwarfVersion(Version);
StringPool = llvm::make_unique<DwarfStringPool>(Allocator, *Asm, StringRef());
return Error::success();
}
StringRef dwarfgen::Generator::generate() {
// Offset from the first CU in the debug info section is 0 initially.
unsigned SecOffset = 0;
// Iterate over each compile unit and set the size and offsets for each
// DIE within each compile unit. All offsets are CU relative.
for (auto &CU : CompileUnits) {
// Set the absolute .debug_info offset for this compile unit.
CU->setOffset(SecOffset);
// The DIEs contain compile unit relative offsets.
unsigned CUOffset = 11;
CUOffset = CU->getUnitDIE().computeSizeAndOffsets(CUOffset);
// Update our absolute .debug_info offset.
SecOffset += CUOffset;
CU->setLength(CUOffset - 4);
}
Abbreviations.Emit(Asm.get(), MOFI->getDwarfAbbrevSection());
StringPool->emit(*Asm, MOFI->getDwarfStrSection());
MS->SwitchSection(MOFI->getDwarfInfoSection());
for (auto &CU : CompileUnits) {
uint16_t Version = CU->getVersion();
auto Length = CU->getLength();
MC->setDwarfVersion(Version);
assert(Length != -1U);
Asm->EmitInt32(Length);
Asm->EmitInt16(Version);
if (Version <= 4) {
Asm->EmitInt32(0);
Asm->EmitInt8(CU->getAddressSize());
} else {
Asm->EmitInt8(dwarf::DW_UT_compile);
Asm->EmitInt8(CU->getAddressSize());
Asm->EmitInt32(0);
}
Asm->emitDwarfDIE(*CU->getUnitDIE().Die);
}
MS->Finish();
if (FileBytes.empty())
return StringRef();
return StringRef(FileBytes.data(), FileBytes.size());
}
bool dwarfgen::Generator::saveFile(StringRef Path) {
if (FileBytes.empty())
return false;
std::error_code EC;
raw_fd_ostream Strm(Path, EC, sys::fs::F_None);
if (EC)
return false;
Strm.write(FileBytes.data(), FileBytes.size());
Strm.close();
return true;
}
dwarfgen::CompileUnit &dwarfgen::Generator::addCompileUnit() {
CompileUnits.push_back(std::unique_ptr<CompileUnit>(
new CompileUnit(*this, Version, Asm->getPointerSize())));
return *CompileUnits.back();
}

View File

@ -0,0 +1,232 @@
//===--- unittests/DebugInfo/DWARF/DwarfGenerator.h -------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// A file that can generate DWARF debug info for unit tests.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFGENERATOR_H
#define LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFGENERATOR_H
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/DIE.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
#include "llvm/Support/Error.h"
#include <memory>
#include <string>
#include <tuple>
#include <vector>
namespace llvm {
class AsmPrinter;
class DIE;
class DIEAbbrev;
class DwarfStringPool;
class MCAsmBackend;
class MCAsmInfo;
class MCCodeEmitter;
class MCContext;
struct MCDwarfLineTableParams;
class MCInstrInfo;
class MCObjectFileInfo;
class MCRegisterInfo;
class MCStreamer;
class MCSubtargetInfo;
class raw_fd_ostream;
class TargetMachine;
class Triple;
namespace dwarfgen {
class Generator;
class CompileUnit;
/// A DWARF debug information entry class used to generate DWARF DIEs.
///
/// This class is used to quickly generate DWARF debug information by creating
/// child DIEs or adding attributes to the current DIE. Instances of this class
/// are created from the compile unit (dwarfgen::CompileUnit::getUnitDIE()) or
/// by calling dwarfgen::DIE::addChild(...) and using the returned DIE object.
class DIE {
dwarfgen::CompileUnit *CU;
llvm::DIE *Die;
protected:
friend class Generator;
friend class CompileUnit;
DIE(CompileUnit *U = nullptr, llvm::DIE *D = nullptr) : CU(U), Die(D) {}
/// Called with a compile/type unit relative offset prior to generating the
/// DWARF debug info.
///
/// \param CUOffset the compile/type unit relative offset where the
/// abbreviation code for this DIE will be encoded.
unsigned computeSizeAndOffsets(unsigned CUOffset);
public:
/// Add an attribute value that has no value.
///
/// \param Attr a dwarf::Attribute enumeration value or any uint16_t that
/// represents a user defined DWARF attribute.
/// \param Form the dwarf::Form to use when encoding the attribute. This is
/// only used with the DW_FORM_flag_present form encoding.
void addAttribute(uint16_t Attr, dwarf::Form Form);
/// Add an attribute value to be encoded as a DIEInteger
///
/// \param Attr a dwarf::Attribute enumeration value or any uint16_t that
/// represents a user defined DWARF attribute.
/// \param Form the dwarf::Form to use when encoding the attribute.
/// \param U the unsigned integer to encode.
void addAttribute(uint16_t Attr, dwarf::Form Form, uint64_t U);
/// Add an attribute value to be encoded as a DIEString or DIEInlinedString.
///
/// \param Attr a dwarf::Attribute enumeration value or any uint16_t that
/// represents a user defined DWARF attribute.
/// \param Form the dwarf::Form to use when encoding the attribute. The form
/// must be one of DW_FORM_strp or DW_FORM_string.
/// \param String the string to encode.
void addAttribute(uint16_t Attr, dwarf::Form Form, StringRef String);
/// Add an attribute value to be encoded as a DIEEntry.
///
/// DIEEntry attributes refer to other llvm::DIE objects that have been
/// created.
///
/// \param Attr a dwarf::Attribute enumeration value or any uint16_t that
/// represents a user defined DWARF attribute.
/// \param Form the dwarf::Form to use when encoding the attribute. The form
/// must be one of DW_FORM_strp or DW_FORM_string.
/// \param RefDie the DIE that this attriute refers to.
void addAttribute(uint16_t Attr, dwarf::Form Form, dwarfgen::DIE &RefDie);
/// Add an attribute value to be encoded as a DIEBlock.
///
/// DIEBlock attributes refers to binary data that is stored as the
/// attribute's value.
///
/// \param Attr a dwarf::Attribute enumeration value or any uint16_t that
/// represents a user defined DWARF attribute.
/// \param Form the dwarf::Form to use when encoding the attribute. The form
/// must be one of DW_FORM_strp or DW_FORM_string.
/// \param P a pointer to the data to store as the attribute value.
/// \param S the size in bytes of the data pointed to by P .
void addAttribute(uint16_t Attr, dwarf::Form Form, const void *P, size_t S);
/// Add a new child to this DIE object.
///
/// \param Tag the dwarf::Tag to assing to the llvm::DIE object.
/// \returns the newly created DIE object that is now a child owned by this
/// object.
dwarfgen::DIE addChild(dwarf::Tag Tag);
};
/// A DWARF compile unit used to generate DWARF compile/type units.
///
/// Instances of these classes are created by instances of the Generator
/// class. All information required to generate a DWARF compile unit is
/// contained inside this class.
class CompileUnit {
Generator &DG;
BasicDIEUnit DU;
public:
CompileUnit(Generator &D, uint16_t V, uint8_t A)
: DG(D), DU(V, A, dwarf::DW_TAG_compile_unit) {}
DIE getUnitDIE();
Generator &getGenerator() { return DG; }
uint64_t getOffset() const { return DU.getDebugSectionOffset(); }
uint64_t getLength() const { return DU.getLength(); }
uint16_t getVersion() const { return DU.getDwarfVersion(); }
uint16_t getAddressSize() const { return DU.getAddressSize(); }
void setOffset(uint64_t Offset) { DU.setDebugSectionOffset(Offset); }
void setLength(uint64_t Length) { DU.setLength(Length); }
};
/// A DWARF generator.
///
/// Generate DWARF for unit tests by creating any instance of this class and
/// calling Generator::addCompileUnit(), and then getting the dwarfgen::DIE from
/// the returned compile unit and adding attributes and children to each DIE.
class Generator {
std::unique_ptr<MCRegisterInfo> MRI;
std::unique_ptr<MCAsmInfo> MAI;
std::unique_ptr<MCObjectFileInfo> MOFI;
std::unique_ptr<MCContext> MC;
MCAsmBackend *MAB; // Owned by MCStreamer
std::unique_ptr<MCInstrInfo> MII;
std::unique_ptr<MCSubtargetInfo> MSTI;
MCCodeEmitter *MCE; // Owned by MCStreamer
MCStreamer *MS; // Owned by AsmPrinter
std::unique_ptr<TargetMachine> TM;
std::unique_ptr<AsmPrinter> Asm;
BumpPtrAllocator Allocator;
std::unique_ptr<DwarfStringPool> StringPool; // Entries owned by Allocator.
std::vector<std::unique_ptr<CompileUnit>> CompileUnits;
DIEAbbrevSet Abbreviations;
SmallString<4096> FileBytes;
/// The stream we use to generate the DWARF into as an ELF file.
std::unique_ptr<raw_svector_ostream> Stream;
/// The DWARF version to generate.
uint16_t Version;
/// Private constructor, call Generator::Create(...) to get a DWARF generator
/// expected.
Generator();
/// Create the streamer and setup the output buffer.
llvm::Error init(Triple TheTriple, uint16_t DwarfVersion);
public:
/// Create a DWARF generator or get an appropriate error.
///
/// \param TheTriple the triple to use when creating any required support
/// classes needed to emit the DWARF.
/// \param DwarfVersion the version of DWARF to emit.
///
/// \returns a llvm::Expected that either contains a unique_ptr to a Generator
/// or a llvm::Error.
static llvm::Expected<std::unique_ptr<Generator>>
create(Triple TheTriple, uint16_t DwarfVersion);
~Generator();
/// Generate all DWARF sections and return a memory buffer that
/// contains an ELF file that contains the DWARF.
StringRef generate();
/// Add a compile unit to be generated.
///
/// \returns a dwarfgen::CompileUnit that can be used to retrieve the compile
/// unit dwarfgen::DIE that can be used to add attributes and add child DIE
/// objedts to.
dwarfgen::CompileUnit &addCompileUnit();
BumpPtrAllocator &getAllocator() { return Allocator; }
AsmPrinter *getAsmPrinter() const { return Asm.get(); }
MCContext *getMCContext() const { return MC.get(); }
DIEAbbrevSet &getAbbrevSet() { return Abbreviations; }
DwarfStringPool &getStringPool() { return *StringPool; }
/// Save the generated DWARF file to disk.
///
/// \param Path the path to save the ELF file to.
bool saveFile(StringRef Path);
};
} // end namespace dwarfgen
} // end namespace llvm
#endif // LLVM_UNITTESTS_DEBUG_INFO_DWARF_DWARFGENERATOR_H