#include "llvm/DWARFLinker/DWARFStreamer.h"
#include "llvm/ADT/Triple.h"
#include "llvm/CodeGen/NonRelocatableStringpool.h"
#include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCTargetOptions.h"
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Target/TargetOptions.h"
namespace llvm {
bool DwarfStreamer::init(Triple TheTriple,
StringRef Swift5ReflectionSegmentName) {
std::string ErrorStr;
std::string TripleName;
StringRef Context = "dwarf streamer init";
const Target *TheTarget =
TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
if (!TheTarget)
return error(ErrorStr, Context), false;
TripleName = TheTriple.getTriple();
MRI.reset(TheTarget->createMCRegInfo(TripleName));
if (!MRI)
return error(Twine("no register info for target ") + TripleName, Context),
false;
MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
if (!MAI)
return error("no asm info for target " + TripleName, Context), false;
MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
if (!MSTI)
return error("no subtarget info for target " + TripleName, Context), false;
MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get(), nullptr,
nullptr, true, Swift5ReflectionSegmentName));
MOFI.reset(TheTarget->createMCObjectFileInfo(*MC, false, false));
MC->setObjectFileInfo(MOFI.get());
MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
if (!MAB)
return error("no asm backend for target " + TripleName, Context), false;
MII.reset(TheTarget->createMCInstrInfo());
if (!MII)
return error("no instr info info for target " + TripleName, Context), false;
MCE = TheTarget->createMCCodeEmitter(*MII, *MC);
if (!MCE)
return error("no code emitter for target " + TripleName, Context), false;
switch (OutFileType) {
case OutputFileType::Assembly: {
MIP = TheTarget->createMCInstPrinter(TheTriple, MAI->getAssemblerDialect(),
*MAI, *MII, *MRI);
MS = TheTarget->createAsmStreamer(
*MC, std::make_unique<formatted_raw_ostream>(OutFile), true, true, MIP,
std::unique_ptr<MCCodeEmitter>(MCE), std::unique_ptr<MCAsmBackend>(MAB),
true);
break;
}
case OutputFileType::Object: {
MS = TheTarget->createMCObjectStreamer(
TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB),
MAB->createObjectWriter(OutFile), std::unique_ptr<MCCodeEmitter>(MCE),
*MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
false);
break;
}
}
if (!MS)
return error("no object streamer for target " + TripleName, Context), false;
TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
None));
if (!TM)
return error("no target machine for target " + TripleName, Context), false;
Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
if (!Asm)
return error("no asm printer for target " + TripleName, Context), false;
RangesSectionSize = 0;
LocSectionSize = 0;
LineSectionSize = 0;
FrameSectionSize = 0;
DebugInfoSectionSize = 0;
return true;
}
void DwarfStreamer::finish() { MS->finish(); }
void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
MS->switchSection(MOFI->getDwarfInfoSection());
MC->setDwarfVersion(DwarfVersion);
}
void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit,
unsigned DwarfVersion) {
switchToDebugInfoSection(DwarfVersion);
Unit.setLabelBegin(Asm->createTempSymbol("cu_begin"));
Asm->OutStreamer->emitLabel(Unit.getLabelBegin());
Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
Asm->emitInt16(DwarfVersion);
if (DwarfVersion >= 5) {
Asm->emitInt8(dwarf::DW_UT_compile);
Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
Asm->emitInt32(0);
DebugInfoSectionSize += 12;
} else {
Asm->emitInt32(0);
Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
DebugInfoSectionSize += 11;
}
EmittedUnits.push_back({Unit.getUniqueID(), Unit.getLabelBegin()});
}
void DwarfStreamer::emitAbbrevs(
const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
unsigned DwarfVersion) {
MS->switchSection(MOFI->getDwarfAbbrevSection());
MC->setDwarfVersion(DwarfVersion);
Asm->emitDwarfAbbrevs(Abbrevs);
}
void DwarfStreamer::emitDIE(DIE &Die) {
MS->switchSection(MOFI->getDwarfInfoSection());
Asm->emitDwarfDIE(Die);
DebugInfoSectionSize += Die.getSize();
}
void DwarfStreamer::emitSectionContents(StringRef SecData, StringRef SecName) {
MCSection *Section =
StringSwitch<MCSection *>(SecName)
.Case("debug_line", MC->getObjectFileInfo()->getDwarfLineSection())
.Case("debug_loc", MC->getObjectFileInfo()->getDwarfLocSection())
.Case("debug_ranges",
MC->getObjectFileInfo()->getDwarfRangesSection())
.Case("debug_frame", MC->getObjectFileInfo()->getDwarfFrameSection())
.Case("debug_aranges",
MC->getObjectFileInfo()->getDwarfARangesSection())
.Default(nullptr);
if (Section) {
MS->switchSection(Section);
MS->emitBytes(SecData);
}
}
void DwarfStreamer::emitPaperTrailWarningsDie(DIE &Die) {
switchToDebugInfoSection( 2);
auto &Asm = getAsmPrinter();
Asm.emitInt32(11 + Die.getSize() - 4);
Asm.emitInt16(2);
Asm.emitInt32(0);
Asm.emitInt8(MC->getTargetTriple().isArch64Bit() ? 8 : 4);
DebugInfoSectionSize += 11;
emitDIE(Die);
}
void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
Asm->OutStreamer->switchSection(MOFI->getDwarfStrSection());
std::vector<DwarfStringPoolEntryRef> Entries = Pool.getEntriesForEmission();
for (auto Entry : Entries) {
Asm->OutStreamer->emitBytes(Entry.getString());
Asm->emitInt8(0);
}
#if 0#endif
}
void DwarfStreamer::emitDebugNames(
AccelTable<DWARF5AccelTableStaticData> &Table) {
if (EmittedUnits.empty())
return;
std::vector<MCSymbol *> CompUnits;
DenseMap<unsigned, size_t> UniqueIdToCuMap;
unsigned Id = 0;
for (auto &CU : EmittedUnits) {
CompUnits.push_back(CU.LabelBegin);
UniqueIdToCuMap[CU.ID] = Id++;
}
Asm->OutStreamer->switchSection(MOFI->getDwarfDebugNamesSection());
emitDWARF5AccelTable(
Asm.get(), Table, CompUnits,
[&UniqueIdToCuMap](const DWARF5AccelTableStaticData &Entry) {
return UniqueIdToCuMap[Entry.getCUIndex()];
});
}
void DwarfStreamer::emitAppleNamespaces(
AccelTable<AppleAccelTableStaticOffsetData> &Table) {
Asm->OutStreamer->switchSection(MOFI->getDwarfAccelNamespaceSection());
auto *SectionBegin = Asm->createTempSymbol("namespac_begin");
Asm->OutStreamer->emitLabel(SectionBegin);
emitAppleAccelTable(Asm.get(), Table, "namespac", SectionBegin);
}
void DwarfStreamer::emitAppleNames(
AccelTable<AppleAccelTableStaticOffsetData> &Table) {
Asm->OutStreamer->switchSection(MOFI->getDwarfAccelNamesSection());
auto *SectionBegin = Asm->createTempSymbol("names_begin");
Asm->OutStreamer->emitLabel(SectionBegin);
emitAppleAccelTable(Asm.get(), Table, "names", SectionBegin);
}
void DwarfStreamer::emitAppleObjc(
AccelTable<AppleAccelTableStaticOffsetData> &Table) {
Asm->OutStreamer->switchSection(MOFI->getDwarfAccelObjCSection());
auto *SectionBegin = Asm->createTempSymbol("objc_begin");
Asm->OutStreamer->emitLabel(SectionBegin);
emitAppleAccelTable(Asm.get(), Table, "objc", SectionBegin);
}
void DwarfStreamer::emitAppleTypes(
AccelTable<AppleAccelTableStaticTypeData> &Table) {
Asm->OutStreamer->switchSection(MOFI->getDwarfAccelTypesSection());
auto *SectionBegin = Asm->createTempSymbol("types_begin");
Asm->OutStreamer->emitLabel(SectionBegin);
emitAppleAccelTable(Asm.get(), Table, "types", SectionBegin);
}
void DwarfStreamer::emitSwiftAST(StringRef Buffer) {
MCSection *SwiftASTSection = MOFI->getDwarfSwiftASTSection();
SwiftASTSection->setAlignment(Align(32));
MS->switchSection(SwiftASTSection);
MS->emitBytes(Buffer);
}
void DwarfStreamer::emitSwiftReflectionSection(
llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
StringRef Buffer, uint32_t Alignment, uint32_t Size) {
MCSection *ReflectionSection =
MOFI->getSwift5ReflectionSection(ReflSectionKind);
if (ReflectionSection == nullptr)
return;
ReflectionSection->setAlignment(Align(Alignment));
MS->switchSection(ReflectionSection);
MS->emitBytes(Buffer);
}
void DwarfStreamer::emitRangesEntries(
int64_t UnitPcOffset, uint64_t OrigLowPc,
Optional<std::pair<AddressRange, int64_t>> FuncRange,
const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
unsigned AddressSize) {
MS->switchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
int64_t PcOffset =
(Entries.empty() || !FuncRange) ? 0 : FuncRange->second + UnitPcOffset;
for (const auto &Range : Entries) {
if (Range.isBaseAddressSelectionEntry(AddressSize)) {
warn("unsupported base address selection operation",
"emitting debug_ranges");
break;
}
if (Range.StartAddress == Range.EndAddress)
continue;
if (!FuncRange->first.contains(Range.StartAddress + OrigLowPc))
warn("inconsistent range data.", "emitting debug_ranges");
MS->emitIntValue(Range.StartAddress + PcOffset, AddressSize);
MS->emitIntValue(Range.EndAddress + PcOffset, AddressSize);
RangesSectionSize += 2 * AddressSize;
}
MS->emitIntValue(0, AddressSize);
MS->emitIntValue(0, AddressSize);
RangesSectionSize += 2 * AddressSize;
}
void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
bool DoDebugRanges) {
unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
std::vector<std::pair<uint64_t, uint64_t>> Ranges;
const RangesTy &FunctionRanges = Unit.getFunctionRanges();
for (size_t Idx = 0; Idx < FunctionRanges.size(); Idx++) {
std::pair<AddressRange, int64_t> CurRange = FunctionRanges[Idx];
Ranges.push_back(std::make_pair(CurRange.first.start() + CurRange.second,
CurRange.first.end() + CurRange.second));
}
llvm::sort(Ranges);
if (!Ranges.empty()) {
MS->switchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
unsigned HeaderSize =
sizeof(int32_t) + sizeof(int16_t) + sizeof(int32_t) + sizeof(int8_t) + sizeof(int8_t);
unsigned TupleSize = AddressSize * 2;
unsigned Padding = offsetToAlignment(HeaderSize, Align(TupleSize));
Asm->emitLabelDifference(EndLabel, BeginLabel, 4); Asm->OutStreamer->emitLabel(BeginLabel);
Asm->emitInt16(dwarf::DW_ARANGES_VERSION); Asm->emitInt32(Unit.getStartOffset()); Asm->emitInt8(AddressSize); Asm->emitInt8(0);
Asm->OutStreamer->emitFill(Padding, 0x0);
for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
++Range) {
uint64_t RangeStart = Range->first;
MS->emitIntValue(RangeStart, AddressSize);
while ((Range + 1) != End && Range->second == (Range + 1)->first)
++Range;
MS->emitIntValue(Range->second - RangeStart, AddressSize);
}
Asm->OutStreamer->emitIntValue(0, AddressSize);
Asm->OutStreamer->emitIntValue(0, AddressSize);
Asm->OutStreamer->emitLabel(EndLabel);
}
if (!DoDebugRanges)
return;
MS->switchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
int64_t PcOffset = -Unit.getLowPc();
for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
MS->emitIntValue(Range->first + PcOffset, AddressSize);
while (Range + 1 != End && Range->second == (Range + 1)->first)
++Range;
MS->emitIntValue(Range->second + PcOffset, AddressSize);
RangesSectionSize += 2 * AddressSize;
}
MS->emitIntValue(0, AddressSize);
MS->emitIntValue(0, AddressSize);
RangesSectionSize += 2 * AddressSize;
}
void DwarfStreamer::emitLocationsForUnit(
const CompileUnit &Unit, DWARFContext &Dwarf,
std::function<void(StringRef, SmallVectorImpl<uint8_t> &)> ProcessExpr) {
const auto &Attributes = Unit.getLocationAttributes();
if (Attributes.empty())
return;
MS->switchSection(MC->getObjectFileInfo()->getDwarfLocSection());
unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
uint64_t BaseAddressMarker = (AddressSize == 8)
? std::numeric_limits<uint64_t>::max()
: std::numeric_limits<uint32_t>::max();
const DWARFSection &InputSec = Dwarf.getDWARFObj().getLocSection();
DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
DWARFUnit &OrigUnit = Unit.getOrigUnit();
auto OrigUnitDie = OrigUnit.getUnitDIE(false);
int64_t UnitPcOffset = 0;
if (auto OrigLowPc = dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc)))
UnitPcOffset = int64_t(*OrigLowPc) - Unit.getLowPc();
SmallVector<uint8_t, 32> Buffer;
for (const auto &Attr : Attributes) {
uint64_t Offset = Attr.first.get();
Attr.first.set(LocSectionSize);
int64_t LocPcOffset = Attr.second + UnitPcOffset;
while (Data.isValidOffset(Offset)) {
uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
uint64_t High = Data.getUnsigned(&Offset, AddressSize);
LocSectionSize += 2 * AddressSize;
if (Low == 0 && High == 0) {
Asm->OutStreamer->emitIntValue(0, AddressSize);
Asm->OutStreamer->emitIntValue(0, AddressSize);
break;
}
if (Low == BaseAddressMarker) {
Asm->OutStreamer->emitIntValue(BaseAddressMarker, AddressSize);
Asm->OutStreamer->emitIntValue(High + Attr.second, AddressSize);
LocPcOffset = 0;
continue;
}
Asm->OutStreamer->emitIntValue(Low + LocPcOffset, AddressSize);
Asm->OutStreamer->emitIntValue(High + LocPcOffset, AddressSize);
uint64_t Length = Data.getU16(&Offset);
Asm->OutStreamer->emitIntValue(Length, 2);
Buffer.reserve(Length);
Buffer.resize(0);
StringRef Input = InputSec.Data.substr(Offset, Length);
ProcessExpr(Input, Buffer);
Asm->OutStreamer->emitBytes(
StringRef((const char *)Buffer.data(), Length));
Offset += Length;
LocSectionSize += Length + 2;
}
}
}
void DwarfStreamer::emitLineTableForUnit(MCDwarfLineTableParams Params,
StringRef PrologueBytes,
unsigned MinInstLength,
std::vector<DWARFDebugLine::Row> &Rows,
unsigned PointerSize) {
MS->switchSection(MC->getObjectFileInfo()->getDwarfLineSection());
MCSymbol *LineStartSym = MC->createTempSymbol();
MCSymbol *LineEndSym = MC->createTempSymbol();
Asm->emitLabelDifference(LineEndSym, LineStartSym, 4);
Asm->OutStreamer->emitLabel(LineStartSym);
MS->emitBytes(PrologueBytes);
LineSectionSize += PrologueBytes.size() + 4;
SmallString<128> EncodingBuffer;
raw_svector_ostream EncodingOS(EncodingBuffer);
if (Rows.empty()) {
MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
EncodingOS);
MS->emitBytes(EncodingOS.str());
LineSectionSize += EncodingBuffer.size();
MS->emitLabel(LineEndSym);
return;
}
unsigned FileNum = 1;
unsigned LastLine = 1;
unsigned Column = 0;
unsigned IsStatement = 1;
unsigned Isa = 0;
uint64_t Address = -1ULL;
unsigned RowsSinceLastSequence = 0;
for (DWARFDebugLine::Row &Row : Rows) {
int64_t AddressDelta;
if (Address == -1ULL) {
MS->emitIntValue(dwarf::DW_LNS_extended_op, 1);
MS->emitULEB128IntValue(PointerSize + 1);
MS->emitIntValue(dwarf::DW_LNE_set_address, 1);
MS->emitIntValue(Row.Address.Address, PointerSize);
LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
AddressDelta = 0;
} else {
AddressDelta = (Row.Address.Address - Address) / MinInstLength;
}
if (FileNum != Row.File) {
FileNum = Row.File;
MS->emitIntValue(dwarf::DW_LNS_set_file, 1);
MS->emitULEB128IntValue(FileNum);
LineSectionSize += 1 + getULEB128Size(FileNum);
}
if (Column != Row.Column) {
Column = Row.Column;
MS->emitIntValue(dwarf::DW_LNS_set_column, 1);
MS->emitULEB128IntValue(Column);
LineSectionSize += 1 + getULEB128Size(Column);
}
if (Isa != Row.Isa) {
Isa = Row.Isa;
MS->emitIntValue(dwarf::DW_LNS_set_isa, 1);
MS->emitULEB128IntValue(Isa);
LineSectionSize += 1 + getULEB128Size(Isa);
}
if (IsStatement != Row.IsStmt) {
IsStatement = Row.IsStmt;
MS->emitIntValue(dwarf::DW_LNS_negate_stmt, 1);
LineSectionSize += 1;
}
if (Row.BasicBlock) {
MS->emitIntValue(dwarf::DW_LNS_set_basic_block, 1);
LineSectionSize += 1;
}
if (Row.PrologueEnd) {
MS->emitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
LineSectionSize += 1;
}
if (Row.EpilogueBegin) {
MS->emitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
LineSectionSize += 1;
}
int64_t LineDelta = int64_t(Row.Line) - LastLine;
if (!Row.EndSequence) {
MCDwarfLineAddr::Encode(*MC, Params, LineDelta, AddressDelta, EncodingOS);
MS->emitBytes(EncodingOS.str());
LineSectionSize += EncodingBuffer.size();
EncodingBuffer.resize(0);
Address = Row.Address.Address;
LastLine = Row.Line;
RowsSinceLastSequence++;
} else {
if (LineDelta) {
MS->emitIntValue(dwarf::DW_LNS_advance_line, 1);
MS->emitSLEB128IntValue(LineDelta);
LineSectionSize += 1 + getSLEB128Size(LineDelta);
}
if (AddressDelta) {
MS->emitIntValue(dwarf::DW_LNS_advance_pc, 1);
MS->emitULEB128IntValue(AddressDelta);
LineSectionSize += 1 + getULEB128Size(AddressDelta);
}
MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(),
0, EncodingOS);
MS->emitBytes(EncodingOS.str());
LineSectionSize += EncodingBuffer.size();
EncodingBuffer.resize(0);
Address = -1ULL;
LastLine = FileNum = IsStatement = 1;
RowsSinceLastSequence = Column = Isa = 0;
}
}
if (RowsSinceLastSequence) {
MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
EncodingOS);
MS->emitBytes(EncodingOS.str());
LineSectionSize += EncodingBuffer.size();
EncodingBuffer.resize(0);
}
MS->emitLabel(LineEndSym);
}
void DwarfStreamer::translateLineTable(DataExtractor Data, uint64_t Offset) {
MS->switchSection(MC->getObjectFileInfo()->getDwarfLineSection());
StringRef Contents = Data.getData();
unsigned UnitLength = Data.getU32(&Offset);
uint64_t UnitEnd = Offset + UnitLength;
MCSymbol *BeginLabel = MC->createTempSymbol();
MCSymbol *EndLabel = MC->createTempSymbol();
unsigned Version = Data.getU16(&Offset);
if (Version > 5) {
warn("Unsupported line table version: dropping contents and not "
"unobfsucating line table.");
return;
}
Asm->emitLabelDifference(EndLabel, BeginLabel, 4);
Asm->OutStreamer->emitLabel(BeginLabel);
Asm->emitInt16(Version);
LineSectionSize += 6;
MCSymbol *HeaderBeginLabel = MC->createTempSymbol();
MCSymbol *HeaderEndLabel = MC->createTempSymbol();
Asm->emitLabelDifference(HeaderEndLabel, HeaderBeginLabel, 4);
Asm->OutStreamer->emitLabel(HeaderBeginLabel);
Offset += 4;
LineSectionSize += 4;
uint64_t AfterHeaderLengthOffset = Offset;
Offset += (Version >= 4) ? 5 : 4;
unsigned OpcodeBase = Data.getU8(&Offset);
Offset += OpcodeBase - 1;
Asm->OutStreamer->emitBytes(Contents.slice(AfterHeaderLengthOffset, Offset));
LineSectionSize += Offset - AfterHeaderLengthOffset;
while (const char *Dir = Data.getCStr(&Offset)) {
if (Dir[0] == 0)
break;
StringRef Translated = Translator(Dir);
Asm->OutStreamer->emitBytes(Translated);
Asm->emitInt8(0);
LineSectionSize += Translated.size() + 1;
}
Asm->emitInt8(0);
LineSectionSize += 1;
while (const char *File = Data.getCStr(&Offset)) {
if (File[0] == 0)
break;
StringRef Translated = Translator(File);
Asm->OutStreamer->emitBytes(Translated);
Asm->emitInt8(0);
LineSectionSize += Translated.size() + 1;
uint64_t OffsetBeforeLEBs = Offset;
Asm->emitULEB128(Data.getULEB128(&Offset));
Asm->emitULEB128(Data.getULEB128(&Offset));
Asm->emitULEB128(Data.getULEB128(&Offset));
LineSectionSize += Offset - OffsetBeforeLEBs;
}
Asm->emitInt8(0);
LineSectionSize += 1;
Asm->OutStreamer->emitLabel(HeaderEndLabel);
Asm->OutStreamer->emitBytes(Contents.slice(Offset, UnitEnd));
LineSectionSize += UnitEnd - Offset;
Asm->OutStreamer->emitLabel(EndLabel);
Offset = UnitEnd;
}
void DwarfStreamer::emitPubSectionForUnit(
MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
const std::vector<CompileUnit::AccelInfo> &Names) {
if (Names.empty())
return;
Asm->OutStreamer->switchSection(Sec);
MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
bool HeaderEmitted = false;
for (const auto &Name : Names) {
if (Name.SkipPubSection)
continue;
if (!HeaderEmitted) {
Asm->emitLabelDifference(EndLabel, BeginLabel, 4); Asm->OutStreamer->emitLabel(BeginLabel);
Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); Asm->emitInt32(Unit.getStartOffset()); Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); HeaderEmitted = true;
}
Asm->emitInt32(Name.Die->getOffset());
Asm->OutStreamer->emitBytes(Name.Name.getString());
Asm->emitInt8(0);
}
if (!HeaderEmitted)
return;
Asm->emitInt32(0); Asm->OutStreamer->emitLabel(EndLabel);
}
void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) {
emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
"names", Unit, Unit.getPubnames());
}
void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) {
emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
"types", Unit, Unit.getPubtypes());
}
void DwarfStreamer::emitCIE(StringRef CIEBytes) {
MS->switchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
MS->emitBytes(CIEBytes);
FrameSectionSize += CIEBytes.size();
}
void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize,
uint32_t Address, StringRef FDEBytes) {
MS->switchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
MS->emitIntValue(FDEBytes.size() + 4 + AddrSize, 4);
MS->emitIntValue(CIEOffset, 4);
MS->emitIntValue(Address, AddrSize);
MS->emitBytes(FDEBytes);
FrameSectionSize += FDEBytes.size() + 8 + AddrSize;
}
}