2020-04-06 23:42:40 +02:00
|
|
|
//===- DwarfStreamer.cpp --------------------------------------------------===//
|
2018-06-27 18:13:40 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01: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
|
2018-06-27 18:13:40 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-06 23:42:40 +02:00
|
|
|
#include "llvm/DWARFLinker/DWARFStreamer.h"
|
2018-06-27 18:13:40 +02:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2020-04-06 23:42:40 +02:00
|
|
|
#include "llvm/CodeGen/NonRelocatableStringpool.h"
|
2019-12-20 17:23:31 +01:00
|
|
|
#include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
|
2018-06-27 18:13:40 +02:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
2020-04-06 23:42:40 +02:00
|
|
|
#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/MCSymbol.h"
|
2018-06-27 18:13:40 +02:00
|
|
|
#include "llvm/MC/MCTargetOptions.h"
|
2020-03-04 00:47:43 +01:00
|
|
|
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
|
2018-06-27 18:13:40 +02:00
|
|
|
#include "llvm/Support/LEB128.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
2020-03-04 00:47:43 +01:00
|
|
|
|
2018-06-27 18:13:40 +02:00
|
|
|
bool DwarfStreamer::init(Triple TheTriple) {
|
|
|
|
std::string ErrorStr;
|
|
|
|
std::string TripleName;
|
|
|
|
StringRef Context = "dwarf streamer init";
|
|
|
|
|
|
|
|
// Get the target.
|
|
|
|
const Target *TheTarget =
|
|
|
|
TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
|
|
|
|
if (!TheTarget)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error(ErrorStr, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
TripleName = TheTriple.getTriple();
|
|
|
|
|
|
|
|
// Create all the MC Objects.
|
|
|
|
MRI.reset(TheTarget->createMCRegInfo(TripleName));
|
|
|
|
if (!MRI)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error(Twine("no register info for target ") + TripleName, Context),
|
|
|
|
false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
2020-03-04 00:47:43 +01:00
|
|
|
MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
|
2019-10-23 12:24:35 +02:00
|
|
|
MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
|
2018-06-27 18:13:40 +02:00
|
|
|
if (!MAI)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error("no asm info for target " + TripleName, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
|
|
|
|
if (!MSTI)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error("no subtarget info for target " + TripleName, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
2021-05-23 23:15:23 +02:00
|
|
|
MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get()));
|
|
|
|
MOFI.reset(TheTarget->createMCObjectFileInfo(*MC, /*PIC=*/false));
|
|
|
|
MC->setObjectFileInfo(MOFI.get());
|
2021-05-05 19:03:02 +02:00
|
|
|
|
2018-07-09 18:58:48 +02:00
|
|
|
MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
|
2018-06-27 18:13:40 +02:00
|
|
|
if (!MAB)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error("no asm backend for target " + TripleName, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
MII.reset(TheTarget->createMCInstrInfo());
|
|
|
|
if (!MII)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error("no instr info info for target " + TripleName, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
|
|
|
|
if (!MCE)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error("no code emitter for target " + TripleName, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
2020-04-06 23:42:40 +02:00
|
|
|
switch (OutFileType) {
|
2018-07-09 18:58:48 +02:00
|
|
|
case OutputFileType::Assembly: {
|
|
|
|
MIP = TheTarget->createMCInstPrinter(TheTriple, MAI->getAssemblerDialect(),
|
|
|
|
*MAI, *MII, *MRI);
|
|
|
|
MS = TheTarget->createAsmStreamer(
|
2019-08-15 17:54:37 +02:00
|
|
|
*MC, std::make_unique<formatted_raw_ostream>(OutFile), true, true, MIP,
|
2018-07-09 18:58:48 +02:00
|
|
|
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,
|
|
|
|
/*DWARFMustBeAtTheEnd*/ false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 18:13:40 +02:00
|
|
|
if (!MS)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error("no object streamer for target " + TripleName, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
// Finally create the AsmPrinter we'll use to emit the DIEs.
|
|
|
|
TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
|
|
|
|
None));
|
|
|
|
if (!TM)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error("no target machine for target " + TripleName, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
|
|
|
|
if (!Asm)
|
2020-04-06 23:42:40 +02:00
|
|
|
return error("no asm printer for target " + TripleName, Context), false;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
RangesSectionSize = 0;
|
|
|
|
LocSectionSize = 0;
|
|
|
|
LineSectionSize = 0;
|
|
|
|
FrameSectionSize = 0;
|
2020-01-09 14:02:50 +01:00
|
|
|
DebugInfoSectionSize = 0;
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-06 23:42:40 +02:00
|
|
|
void DwarfStreamer::finish() { MS->Finish(); }
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
|
|
|
|
MS->SwitchSection(MOFI->getDwarfInfoSection());
|
|
|
|
MC->setDwarfVersion(DwarfVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the compilation unit header for \p Unit in the debug_info section.
|
|
|
|
///
|
2021-01-13 06:55:41 +01:00
|
|
|
/// A Dwarf 4 section header is encoded as:
|
2018-06-27 18:13:40 +02:00
|
|
|
/// uint32_t Unit length (omitting this field)
|
|
|
|
/// uint16_t Version
|
|
|
|
/// uint32_t Abbreviation table offset
|
|
|
|
/// uint8_t Address size
|
|
|
|
/// Leading to a total of 11 bytes.
|
2021-01-13 06:55:41 +01:00
|
|
|
///
|
|
|
|
/// A Dwarf 5 section header is encoded as:
|
|
|
|
/// uint32_t Unit length (omitting this field)
|
|
|
|
/// uint16_t Version
|
|
|
|
/// uint8_t Unit type
|
|
|
|
/// uint8_t Address size
|
|
|
|
/// uint32_t Abbreviation table offset
|
|
|
|
/// Leading to a total of 12 bytes.
|
|
|
|
void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit,
|
|
|
|
unsigned DwarfVersion) {
|
|
|
|
switchToDebugInfoSection(DwarfVersion);
|
2018-06-27 18:13:40 +02:00
|
|
|
|
2018-07-06 14:49:54 +02:00
|
|
|
/// The start of the unit within its section.
|
|
|
|
Unit.setLabelBegin(Asm->createTempSymbol("cu_begin"));
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(Unit.getLabelBegin());
|
2018-07-06 14:49:54 +02:00
|
|
|
|
2018-06-27 18:13:40 +02:00
|
|
|
// Emit size of content not including length itself. The size has already
|
|
|
|
// been computed in CompileUnit::computeOffsets(). Subtract 4 to that size to
|
|
|
|
// account for the length field.
|
|
|
|
Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
|
2021-01-13 06:55:41 +01:00
|
|
|
Asm->emitInt16(DwarfVersion);
|
|
|
|
|
|
|
|
if (DwarfVersion >= 5) {
|
|
|
|
Asm->emitInt8(dwarf::DW_UT_compile);
|
|
|
|
Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
|
|
|
|
// We share one abbreviations table across all units so it's always at the
|
|
|
|
// start of the section.
|
|
|
|
Asm->emitInt32(0);
|
|
|
|
DebugInfoSectionSize += 12;
|
|
|
|
} else {
|
|
|
|
// We share one abbreviations table across all units so it's always at the
|
|
|
|
// start of the section.
|
|
|
|
Asm->emitInt32(0);
|
|
|
|
Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
|
|
|
|
DebugInfoSectionSize += 11;
|
|
|
|
}
|
2018-07-26 01:01:38 +02:00
|
|
|
|
|
|
|
// Remember this CU.
|
|
|
|
EmittedUnits.push_back({Unit.getUniqueID(), Unit.getLabelBegin()});
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the \p Abbrevs array as the shared abbreviation table
|
|
|
|
/// for the linked Dwarf file.
|
|
|
|
void DwarfStreamer::emitAbbrevs(
|
|
|
|
const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
|
|
|
|
unsigned DwarfVersion) {
|
|
|
|
MS->SwitchSection(MOFI->getDwarfAbbrevSection());
|
|
|
|
MC->setDwarfVersion(DwarfVersion);
|
|
|
|
Asm->emitDwarfAbbrevs(Abbrevs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recursively emit the DIE tree rooted at \p Die.
|
|
|
|
void DwarfStreamer::emitDIE(DIE &Die) {
|
|
|
|
MS->SwitchSection(MOFI->getDwarfInfoSection());
|
|
|
|
Asm->emitDwarfDIE(Die);
|
2020-01-09 14:02:50 +01:00
|
|
|
DebugInfoSectionSize += Die.getSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit contents of section SecName From Obj.
|
2020-02-22 12:33:58 +01:00
|
|
|
void DwarfStreamer::emitSectionContents(StringRef SecData, StringRef SecName) {
|
2020-01-09 14:02:50 +01:00
|
|
|
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);
|
|
|
|
|
2020-02-22 12:33:58 +01:00
|
|
|
MS->emitBytes(SecData);
|
2020-01-09 14:02:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit DIE containing warnings.
|
2020-02-06 23:48:28 +01:00
|
|
|
void DwarfStreamer::emitPaperTrailWarningsDie(DIE &Die) {
|
2020-01-09 14:02:50 +01:00
|
|
|
switchToDebugInfoSection(/* Version */ 2);
|
|
|
|
auto &Asm = getAsmPrinter();
|
|
|
|
Asm.emitInt32(11 + Die.getSize() - 4);
|
|
|
|
Asm.emitInt16(2);
|
|
|
|
Asm.emitInt32(0);
|
2021-05-05 19:03:02 +02:00
|
|
|
Asm.emitInt8(MC->getTargetTriple().isArch64Bit() ? 8 : 4);
|
2020-01-09 14:02:50 +01:00
|
|
|
DebugInfoSectionSize += 11;
|
|
|
|
emitDIE(Die);
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the debug_str section stored in \p Pool.
|
|
|
|
void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
|
|
|
|
Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrSection());
|
[DebugInfo] Reduce debug_str_offsets section size
Summary:
The accelerator tables use the debug_str section to store their strings.
However, they do not support the indirect method of access that is
available for the debug_info section (DW_FORM_strx et al.).
Currently our code is assuming that all strings can/will be referenced
indirectly, and puts all of them into the debug_str_offsets section.
This is generally true for regular (unsplit) dwarf, but in the DWO case,
most of the strings in the debug_str section will only be used from the
accelerator tables. Therefore the contents of the debug_str_offsets
section will be largely unused and bloating the main executable.
This patch rectifies this by teaching the DwarfStringPool to
differentiate between strings accessed directly and indirectly. When a
user inserts a string into the pool it has to declare whether that
string will be referenced directly or not. If at least one user requsts
indirect access, that string will be assigned an index ID and put into
debug_str_offsets table. Otherwise, the offset table is skipped.
This approach reduces the overall binary size (when compiled with
-gdwarf-5 -gsplit-dwarf) in my tests by about 2% (debug_str_offsets is
shrunk by 99%).
Reviewers: probinson, dblaikie, JDevlieghere
Subscribers: aprantl, mgrang, llvm-commits
Differential Revision: https://reviews.llvm.org/D49493
llvm-svn: 339122
2018-08-07 11:54:52 +02:00
|
|
|
std::vector<DwarfStringPoolEntryRef> Entries = Pool.getEntriesForEmission();
|
2018-06-27 18:13:40 +02:00
|
|
|
for (auto Entry : Entries) {
|
|
|
|
// Emit the string itself.
|
2020-02-15 03:16:24 +01:00
|
|
|
Asm->OutStreamer->emitBytes(Entry.getString());
|
2018-06-27 18:13:40 +02:00
|
|
|
// Emit a null terminator.
|
|
|
|
Asm->emitInt8(0);
|
|
|
|
}
|
2021-01-13 06:55:41 +01:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (DwarfVersion >= 5) {
|
|
|
|
// Emit an empty string offset section.
|
|
|
|
Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrOffSection());
|
|
|
|
Asm->emitDwarfUnitLength(4, "Length of String Offsets Set");
|
|
|
|
Asm->emitInt16(DwarfVersion);
|
|
|
|
Asm->emitInt16(0);
|
|
|
|
}
|
|
|
|
#endif
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 01:01:38 +02:00
|
|
|
void DwarfStreamer::emitDebugNames(
|
|
|
|
AccelTable<DWARF5AccelTableStaticData> &Table) {
|
|
|
|
if (EmittedUnits.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Build up data structures needed to emit this section.
|
|
|
|
std::vector<MCSymbol *> CompUnits;
|
|
|
|
DenseMap<unsigned, size_t> UniqueIdToCuMap;
|
|
|
|
unsigned Id = 0;
|
|
|
|
for (auto &CU : EmittedUnits) {
|
|
|
|
CompUnits.push_back(CU.LabelBegin);
|
|
|
|
// We might be omitting CUs, so we need to remap them.
|
|
|
|
UniqueIdToCuMap[CU.ID] = Id++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Asm->OutStreamer->SwitchSection(MOFI->getDwarfDebugNamesSection());
|
|
|
|
emitDWARF5AccelTable(
|
|
|
|
Asm.get(), Table, CompUnits,
|
|
|
|
[&UniqueIdToCuMap](const DWARF5AccelTableStaticData &Entry) {
|
|
|
|
return UniqueIdToCuMap[Entry.getCUIndex()];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-27 18:13:40 +02:00
|
|
|
void DwarfStreamer::emitAppleNamespaces(
|
|
|
|
AccelTable<AppleAccelTableStaticOffsetData> &Table) {
|
|
|
|
Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelNamespaceSection());
|
|
|
|
auto *SectionBegin = Asm->createTempSymbol("namespac_begin");
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(SectionBegin);
|
2018-06-27 18:13:40 +02:00
|
|
|
emitAppleAccelTable(Asm.get(), Table, "namespac", SectionBegin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DwarfStreamer::emitAppleNames(
|
|
|
|
AccelTable<AppleAccelTableStaticOffsetData> &Table) {
|
|
|
|
Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelNamesSection());
|
|
|
|
auto *SectionBegin = Asm->createTempSymbol("names_begin");
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(SectionBegin);
|
2018-06-27 18:13:40 +02:00
|
|
|
emitAppleAccelTable(Asm.get(), Table, "names", SectionBegin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DwarfStreamer::emitAppleObjc(
|
|
|
|
AccelTable<AppleAccelTableStaticOffsetData> &Table) {
|
|
|
|
Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelObjCSection());
|
|
|
|
auto *SectionBegin = Asm->createTempSymbol("objc_begin");
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(SectionBegin);
|
2018-06-27 18:13:40 +02:00
|
|
|
emitAppleAccelTable(Asm.get(), Table, "objc", SectionBegin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DwarfStreamer::emitAppleTypes(
|
|
|
|
AccelTable<AppleAccelTableStaticTypeData> &Table) {
|
|
|
|
Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelTypesSection());
|
|
|
|
auto *SectionBegin = Asm->createTempSymbol("types_begin");
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(SectionBegin);
|
2018-06-27 18:13:40 +02:00
|
|
|
emitAppleAccelTable(Asm.get(), Table, "types", SectionBegin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the swift_ast section stored in \p Buffers.
|
|
|
|
void DwarfStreamer::emitSwiftAST(StringRef Buffer) {
|
|
|
|
MCSection *SwiftASTSection = MOFI->getDwarfSwiftASTSection();
|
2019-09-27 14:54:21 +02:00
|
|
|
SwiftASTSection->setAlignment(Align(32));
|
2018-06-27 18:13:40 +02:00
|
|
|
MS->SwitchSection(SwiftASTSection);
|
2020-02-15 03:16:24 +01:00
|
|
|
MS->emitBytes(Buffer);
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the debug_range section contents for \p FuncRange by
|
|
|
|
/// translating the original \p Entries. The debug_range section
|
|
|
|
/// format is totally trivial, consisting just of pairs of address
|
|
|
|
/// sized addresses describing the ranges.
|
|
|
|
void DwarfStreamer::emitRangesEntries(
|
|
|
|
int64_t UnitPcOffset, uint64_t OrigLowPc,
|
|
|
|
const FunctionIntervals::const_iterator &FuncRange,
|
2018-10-31 02:12:58 +01:00
|
|
|
const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
|
|
|
|
unsigned AddressSize) {
|
2018-06-27 18:13:40 +02:00
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
|
|
|
|
|
|
|
|
// Offset each range by the right amount.
|
2018-10-31 02:12:58 +01:00
|
|
|
int64_t PcOffset = Entries.empty() ? 0 : FuncRange.value() + UnitPcOffset;
|
|
|
|
for (const auto &Range : Entries) {
|
|
|
|
if (Range.isBaseAddressSelectionEntry(AddressSize)) {
|
2018-06-27 18:13:40 +02:00
|
|
|
warn("unsupported base address selection operation",
|
|
|
|
"emitting debug_ranges");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Do not emit empty ranges.
|
2018-10-31 02:12:58 +01:00
|
|
|
if (Range.StartAddress == Range.EndAddress)
|
2018-06-27 18:13:40 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// All range entries should lie in the function range.
|
2018-10-31 02:12:58 +01:00
|
|
|
if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
|
|
|
|
Range.EndAddress + OrigLowPc <= FuncRange.stop()))
|
2018-06-27 18:13:40 +02:00
|
|
|
warn("inconsistent range data.", "emitting debug_ranges");
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(Range.StartAddress + PcOffset, AddressSize);
|
|
|
|
MS->emitIntValue(Range.EndAddress + PcOffset, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
RangesSectionSize += 2 * AddressSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the terminator entry.
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(0, AddressSize);
|
|
|
|
MS->emitIntValue(0, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
RangesSectionSize += 2 * AddressSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the debug_aranges contribution of a unit and
|
|
|
|
/// if \p DoDebugRanges is true the debug_range contents for a
|
|
|
|
/// compile_unit level DW_AT_ranges attribute (Which are basically the
|
|
|
|
/// same thing with a different base address).
|
|
|
|
/// Just aggregate all the ranges gathered inside that unit.
|
|
|
|
void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
|
|
|
|
bool DoDebugRanges) {
|
|
|
|
unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
|
|
|
|
// Gather the ranges in a vector, so that we can simplify them. The
|
|
|
|
// IntervalMap will have coalesced the non-linked ranges, but here
|
|
|
|
// we want to coalesce the linked addresses.
|
|
|
|
std::vector<std::pair<uint64_t, uint64_t>> Ranges;
|
|
|
|
const auto &FunctionRanges = Unit.getFunctionRanges();
|
|
|
|
for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
|
|
|
|
Range != End; ++Range)
|
|
|
|
Ranges.push_back(std::make_pair(Range.start() + Range.value(),
|
|
|
|
Range.stop() + Range.value()));
|
|
|
|
|
|
|
|
// The object addresses where sorted, but again, the linked
|
|
|
|
// addresses might end up in a different order.
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 04:13:45 +02:00
|
|
|
llvm::sort(Ranges);
|
2018-06-27 18:13:40 +02:00
|
|
|
|
|
|
|
if (!Ranges.empty()) {
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
|
|
|
|
|
|
|
|
MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
|
|
|
|
MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
|
|
|
|
|
|
|
|
unsigned HeaderSize =
|
|
|
|
sizeof(int32_t) + // Size of contents (w/o this field
|
|
|
|
sizeof(int16_t) + // DWARF ARange version number
|
|
|
|
sizeof(int32_t) + // Offset of CU in the .debug_info section
|
|
|
|
sizeof(int8_t) + // Pointer Size (in bytes)
|
|
|
|
sizeof(int8_t); // Segment Size (in bytes)
|
|
|
|
|
|
|
|
unsigned TupleSize = AddressSize * 2;
|
2019-09-27 14:54:21 +02:00
|
|
|
unsigned Padding = offsetToAlignment(HeaderSize, Align(TupleSize));
|
2018-06-27 18:13:40 +02:00
|
|
|
|
2020-02-13 22:26:21 +01:00
|
|
|
Asm->emitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(BeginLabel);
|
2018-06-27 18:13:40 +02:00
|
|
|
Asm->emitInt16(dwarf::DW_ARANGES_VERSION); // Version number
|
|
|
|
Asm->emitInt32(Unit.getStartOffset()); // Corresponding unit's offset
|
|
|
|
Asm->emitInt8(AddressSize); // Address size
|
|
|
|
Asm->emitInt8(0); // Segment size
|
|
|
|
|
|
|
|
Asm->OutStreamer->emitFill(Padding, 0x0);
|
|
|
|
|
|
|
|
for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
|
|
|
|
++Range) {
|
|
|
|
uint64_t RangeStart = Range->first;
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(RangeStart, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
while ((Range + 1) != End && Range->second == (Range + 1)->first)
|
|
|
|
++Range;
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(Range->second - RangeStart, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit terminator
|
2020-02-15 07:40:47 +01:00
|
|
|
Asm->OutStreamer->emitIntValue(0, AddressSize);
|
|
|
|
Asm->OutStreamer->emitIntValue(0, AddressSize);
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(EndLabel);
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoDebugRanges)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
|
|
|
|
// Offset each range by the right amount.
|
|
|
|
int64_t PcOffset = -Unit.getLowPc();
|
|
|
|
// Emit coalesced ranges.
|
|
|
|
for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(Range->first + PcOffset, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
while (Range + 1 != End && Range->second == (Range + 1)->first)
|
|
|
|
++Range;
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(Range->second + PcOffset, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
RangesSectionSize += 2 * AddressSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the terminator entry.
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(0, AddressSize);
|
|
|
|
MS->emitIntValue(0, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
RangesSectionSize += 2 * AddressSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit location lists for \p Unit and update attributes to point to the new
|
|
|
|
/// entries.
|
2019-02-28 23:12:32 +01:00
|
|
|
void DwarfStreamer::emitLocationsForUnit(
|
|
|
|
const CompileUnit &Unit, DWARFContext &Dwarf,
|
|
|
|
std::function<void(StringRef, SmallVectorImpl<uint8_t> &)> ProcessExpr) {
|
2018-06-27 18:13:40 +02:00
|
|
|
const auto &Attributes = Unit.getLocationAttributes();
|
|
|
|
|
|
|
|
if (Attributes.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
|
|
|
|
|
|
|
|
unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
|
2019-10-16 01:43:37 +02:00
|
|
|
uint64_t BaseAddressMarker = (AddressSize == 8)
|
|
|
|
? std::numeric_limits<uint64_t>::max()
|
|
|
|
: std::numeric_limits<uint32_t>::max();
|
2018-06-27 18:13:40 +02:00
|
|
|
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();
|
|
|
|
|
2019-02-28 23:12:32 +01:00
|
|
|
SmallVector<uint8_t, 32> Buffer;
|
2018-06-27 18:13:40 +02:00
|
|
|
for (const auto &Attr : Attributes) {
|
2019-08-06 12:49:40 +02:00
|
|
|
uint64_t Offset = Attr.first.get();
|
2018-06-27 18:13:40 +02:00
|
|
|
Attr.first.set(LocSectionSize);
|
|
|
|
// This is the quantity to add to the old location address to get
|
|
|
|
// the correct address for the new one.
|
|
|
|
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;
|
2019-10-16 01:43:37 +02:00
|
|
|
// End of list entry.
|
2018-06-27 18:13:40 +02:00
|
|
|
if (Low == 0 && High == 0) {
|
2020-02-15 07:40:47 +01:00
|
|
|
Asm->OutStreamer->emitIntValue(0, AddressSize);
|
|
|
|
Asm->OutStreamer->emitIntValue(0, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-10-16 01:43:37 +02:00
|
|
|
// Base address selection entry.
|
|
|
|
if (Low == BaseAddressMarker) {
|
2020-02-15 07:40:47 +01:00
|
|
|
Asm->OutStreamer->emitIntValue(BaseAddressMarker, AddressSize);
|
|
|
|
Asm->OutStreamer->emitIntValue(High + Attr.second, AddressSize);
|
2019-10-16 01:43:37 +02:00
|
|
|
LocPcOffset = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Location list entry.
|
2020-02-15 07:40:47 +01:00
|
|
|
Asm->OutStreamer->emitIntValue(Low + LocPcOffset, AddressSize);
|
|
|
|
Asm->OutStreamer->emitIntValue(High + LocPcOffset, AddressSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
uint64_t Length = Data.getU16(&Offset);
|
2020-02-15 07:40:47 +01:00
|
|
|
Asm->OutStreamer->emitIntValue(Length, 2);
|
2019-02-28 23:12:32 +01:00
|
|
|
// Copy the bytes into to the buffer, process them, emit them.
|
|
|
|
Buffer.reserve(Length);
|
|
|
|
Buffer.resize(0);
|
|
|
|
StringRef Input = InputSec.Data.substr(Offset, Length);
|
|
|
|
ProcessExpr(Input, Buffer);
|
2020-02-15 03:16:24 +01:00
|
|
|
Asm->OutStreamer->emitBytes(
|
2019-02-28 23:12:32 +01:00
|
|
|
StringRef((const char *)Buffer.data(), Length));
|
2018-06-27 18:13:40 +02:00
|
|
|
Offset += Length;
|
|
|
|
LocSectionSize += Length + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DwarfStreamer::emitLineTableForUnit(MCDwarfLineTableParams Params,
|
|
|
|
StringRef PrologueBytes,
|
|
|
|
unsigned MinInstLength,
|
|
|
|
std::vector<DWARFDebugLine::Row> &Rows,
|
|
|
|
unsigned PointerSize) {
|
|
|
|
// Switch to the section where the table will be emitted into.
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
|
|
|
|
MCSymbol *LineStartSym = MC->createTempSymbol();
|
|
|
|
MCSymbol *LineEndSym = MC->createTempSymbol();
|
|
|
|
|
|
|
|
// The first 4 bytes is the total length of the information for this
|
|
|
|
// compilation unit (not including these 4 bytes for the length).
|
2020-02-13 22:26:21 +01:00
|
|
|
Asm->emitLabelDifference(LineEndSym, LineStartSym, 4);
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(LineStartSym);
|
2018-06-27 18:13:40 +02:00
|
|
|
// Copy Prologue.
|
2020-02-15 03:16:24 +01:00
|
|
|
MS->emitBytes(PrologueBytes);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += PrologueBytes.size() + 4;
|
|
|
|
|
|
|
|
SmallString<128> EncodingBuffer;
|
|
|
|
raw_svector_ostream EncodingOS(EncodingBuffer);
|
|
|
|
|
|
|
|
if (Rows.empty()) {
|
|
|
|
// We only have the dummy entry, dsymutil emits an entry with a 0
|
|
|
|
// address in that case.
|
|
|
|
MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
|
|
|
|
EncodingOS);
|
2020-02-15 03:16:24 +01:00
|
|
|
MS->emitBytes(EncodingOS.str());
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += EncodingBuffer.size();
|
2020-02-15 04:21:58 +01:00
|
|
|
MS->emitLabel(LineEndSym);
|
2018-06-27 18:13:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Line table state machine fields
|
|
|
|
unsigned FileNum = 1;
|
|
|
|
unsigned LastLine = 1;
|
|
|
|
unsigned Column = 0;
|
|
|
|
unsigned IsStatement = 1;
|
|
|
|
unsigned Isa = 0;
|
|
|
|
uint64_t Address = -1ULL;
|
|
|
|
|
|
|
|
unsigned RowsSinceLastSequence = 0;
|
|
|
|
|
|
|
|
for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
|
|
|
|
auto &Row = Rows[Idx];
|
|
|
|
|
|
|
|
int64_t AddressDelta;
|
|
|
|
if (Address == -1ULL) {
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_extended_op, 1);
|
2020-02-13 22:26:21 +01:00
|
|
|
MS->emitULEB128IntValue(PointerSize + 1);
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNE_set_address, 1);
|
|
|
|
MS->emitIntValue(Row.Address.Address, PointerSize);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
|
|
|
|
AddressDelta = 0;
|
|
|
|
} else {
|
2019-02-27 14:17:36 +01:00
|
|
|
AddressDelta = (Row.Address.Address - Address) / MinInstLength;
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: code copied and transformed from MCDwarf.cpp::EmitDwarfLineTable.
|
|
|
|
// We should find a way to share this code, but the current compatibility
|
|
|
|
// requirement with classic dsymutil makes it hard. Revisit that once this
|
|
|
|
// requirement is dropped.
|
|
|
|
|
|
|
|
if (FileNum != Row.File) {
|
|
|
|
FileNum = Row.File;
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_set_file, 1);
|
2020-02-13 22:26:21 +01:00
|
|
|
MS->emitULEB128IntValue(FileNum);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1 + getULEB128Size(FileNum);
|
|
|
|
}
|
|
|
|
if (Column != Row.Column) {
|
|
|
|
Column = Row.Column;
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_set_column, 1);
|
2020-02-13 22:26:21 +01:00
|
|
|
MS->emitULEB128IntValue(Column);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1 + getULEB128Size(Column);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: We should handle the discriminator here, but dsymutil doesn't
|
|
|
|
// consider it, thus ignore it for now.
|
|
|
|
|
|
|
|
if (Isa != Row.Isa) {
|
|
|
|
Isa = Row.Isa;
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_set_isa, 1);
|
2020-02-13 22:26:21 +01:00
|
|
|
MS->emitULEB128IntValue(Isa);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1 + getULEB128Size(Isa);
|
|
|
|
}
|
|
|
|
if (IsStatement != Row.IsStmt) {
|
|
|
|
IsStatement = Row.IsStmt;
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_negate_stmt, 1);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1;
|
|
|
|
}
|
|
|
|
if (Row.BasicBlock) {
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_set_basic_block, 1);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Row.PrologueEnd) {
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Row.EpilogueBegin) {
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t LineDelta = int64_t(Row.Line) - LastLine;
|
|
|
|
if (!Row.EndSequence) {
|
|
|
|
MCDwarfLineAddr::Encode(*MC, Params, LineDelta, AddressDelta, EncodingOS);
|
2020-02-15 03:16:24 +01:00
|
|
|
MS->emitBytes(EncodingOS.str());
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += EncodingBuffer.size();
|
|
|
|
EncodingBuffer.resize(0);
|
2019-02-27 14:17:36 +01:00
|
|
|
Address = Row.Address.Address;
|
2018-06-27 18:13:40 +02:00
|
|
|
LastLine = Row.Line;
|
|
|
|
RowsSinceLastSequence++;
|
|
|
|
} else {
|
|
|
|
if (LineDelta) {
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_advance_line, 1);
|
2020-02-13 22:26:21 +01:00
|
|
|
MS->emitSLEB128IntValue(LineDelta);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1 + getSLEB128Size(LineDelta);
|
|
|
|
}
|
|
|
|
if (AddressDelta) {
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(dwarf::DW_LNS_advance_pc, 1);
|
2020-02-13 22:26:21 +01:00
|
|
|
MS->emitULEB128IntValue(AddressDelta);
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += 1 + getULEB128Size(AddressDelta);
|
|
|
|
}
|
|
|
|
MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(),
|
|
|
|
0, EncodingOS);
|
2020-02-15 03:16:24 +01:00
|
|
|
MS->emitBytes(EncodingOS.str());
|
2018-06-27 18:13:40 +02:00
|
|
|
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);
|
2020-02-15 03:16:24 +01:00
|
|
|
MS->emitBytes(EncodingOS.str());
|
2018-06-27 18:13:40 +02:00
|
|
|
LineSectionSize += EncodingBuffer.size();
|
|
|
|
EncodingBuffer.resize(0);
|
|
|
|
}
|
|
|
|
|
2020-02-15 04:21:58 +01:00
|
|
|
MS->emitLabel(LineEndSym);
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 00:27:25 +01:00
|
|
|
/// Copy the debug_line over to the updated binary while unobfuscating the file
|
|
|
|
/// names and directories.
|
2019-08-06 12:49:40 +02:00
|
|
|
void DwarfStreamer::translateLineTable(DataExtractor Data, uint64_t Offset) {
|
2019-01-08 00:27:25 +01:00
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
|
|
|
|
StringRef Contents = Data.getData();
|
|
|
|
|
|
|
|
// We have to deconstruct the line table header, because it contains to
|
|
|
|
// length fields that will need to be updated when we change the length of
|
|
|
|
// the files and directories in there.
|
|
|
|
unsigned UnitLength = Data.getU32(&Offset);
|
2019-08-06 12:49:40 +02:00
|
|
|
uint64_t UnitEnd = Offset + UnitLength;
|
2019-01-08 00:27:25 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:26:21 +01:00
|
|
|
Asm->emitLabelDifference(EndLabel, BeginLabel, 4);
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(BeginLabel);
|
2019-01-08 00:27:25 +01:00
|
|
|
Asm->emitInt16(Version);
|
|
|
|
LineSectionSize += 6;
|
|
|
|
|
|
|
|
MCSymbol *HeaderBeginLabel = MC->createTempSymbol();
|
|
|
|
MCSymbol *HeaderEndLabel = MC->createTempSymbol();
|
2020-02-13 22:26:21 +01:00
|
|
|
Asm->emitLabelDifference(HeaderEndLabel, HeaderBeginLabel, 4);
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(HeaderBeginLabel);
|
2019-01-08 00:27:25 +01:00
|
|
|
Offset += 4;
|
|
|
|
LineSectionSize += 4;
|
|
|
|
|
2019-08-06 12:49:40 +02:00
|
|
|
uint64_t AfterHeaderLengthOffset = Offset;
|
2019-01-08 00:27:25 +01:00
|
|
|
// Skip to the directories.
|
|
|
|
Offset += (Version >= 4) ? 5 : 4;
|
|
|
|
unsigned OpcodeBase = Data.getU8(&Offset);
|
|
|
|
Offset += OpcodeBase - 1;
|
2020-02-15 03:16:24 +01:00
|
|
|
Asm->OutStreamer->emitBytes(Contents.slice(AfterHeaderLengthOffset, Offset));
|
2019-01-08 00:27:25 +01:00
|
|
|
LineSectionSize += Offset - AfterHeaderLengthOffset;
|
|
|
|
|
|
|
|
// Offset points to the first directory.
|
|
|
|
while (const char *Dir = Data.getCStr(&Offset)) {
|
|
|
|
if (Dir[0] == 0)
|
|
|
|
break;
|
|
|
|
|
2020-04-06 23:42:40 +02:00
|
|
|
StringRef Translated = Translator(Dir);
|
2020-02-15 03:16:24 +01:00
|
|
|
Asm->OutStreamer->emitBytes(Translated);
|
2019-01-08 00:27:25 +01:00
|
|
|
Asm->emitInt8(0);
|
|
|
|
LineSectionSize += Translated.size() + 1;
|
|
|
|
}
|
|
|
|
Asm->emitInt8(0);
|
|
|
|
LineSectionSize += 1;
|
|
|
|
|
|
|
|
while (const char *File = Data.getCStr(&Offset)) {
|
|
|
|
if (File[0] == 0)
|
|
|
|
break;
|
|
|
|
|
2020-04-06 23:42:40 +02:00
|
|
|
StringRef Translated = Translator(File);
|
2020-02-15 03:16:24 +01:00
|
|
|
Asm->OutStreamer->emitBytes(Translated);
|
2019-01-08 00:27:25 +01:00
|
|
|
Asm->emitInt8(0);
|
|
|
|
LineSectionSize += Translated.size() + 1;
|
|
|
|
|
2019-08-06 12:49:40 +02:00
|
|
|
uint64_t OffsetBeforeLEBs = Offset;
|
2020-02-13 22:26:21 +01:00
|
|
|
Asm->emitULEB128(Data.getULEB128(&Offset));
|
|
|
|
Asm->emitULEB128(Data.getULEB128(&Offset));
|
|
|
|
Asm->emitULEB128(Data.getULEB128(&Offset));
|
2019-01-08 00:27:25 +01:00
|
|
|
LineSectionSize += Offset - OffsetBeforeLEBs;
|
|
|
|
}
|
|
|
|
Asm->emitInt8(0);
|
|
|
|
LineSectionSize += 1;
|
|
|
|
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(HeaderEndLabel);
|
2019-01-08 00:27:25 +01:00
|
|
|
|
|
|
|
// Copy the actual line table program over.
|
2020-02-15 03:16:24 +01:00
|
|
|
Asm->OutStreamer->emitBytes(Contents.slice(Offset, UnitEnd));
|
2019-01-08 00:27:25 +01:00
|
|
|
LineSectionSize += UnitEnd - Offset;
|
|
|
|
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(EndLabel);
|
2019-01-08 00:27:25 +01:00
|
|
|
Offset = UnitEnd;
|
|
|
|
}
|
|
|
|
|
2018-06-27 18:13:40 +02:00
|
|
|
/// Emit the pubnames or pubtypes section contribution for \p
|
|
|
|
/// Unit into \p Sec. The data is provided in \p Names.
|
|
|
|
void DwarfStreamer::emitPubSectionForUnit(
|
|
|
|
MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
|
|
|
|
const std::vector<CompileUnit::AccelInfo> &Names) {
|
|
|
|
if (Names.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Start the dwarf pubnames section.
|
|
|
|
Asm->OutStreamer->SwitchSection(Sec);
|
|
|
|
MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
|
|
|
|
MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
|
|
|
|
|
|
|
|
bool HeaderEmitted = false;
|
|
|
|
// Emit the pubnames for this compilation unit.
|
|
|
|
for (const auto &Name : Names) {
|
|
|
|
if (Name.SkipPubSection)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!HeaderEmitted) {
|
|
|
|
// Emit the header.
|
2020-02-13 22:26:21 +01:00
|
|
|
Asm->emitLabelDifference(EndLabel, BeginLabel, 4); // Length
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(BeginLabel);
|
2018-06-27 18:13:40 +02:00
|
|
|
Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); // Version
|
|
|
|
Asm->emitInt32(Unit.getStartOffset()); // Unit offset
|
|
|
|
Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size
|
|
|
|
HeaderEmitted = true;
|
|
|
|
}
|
|
|
|
Asm->emitInt32(Name.Die->getOffset());
|
|
|
|
|
|
|
|
// Emit the string itself.
|
2020-02-15 03:16:24 +01:00
|
|
|
Asm->OutStreamer->emitBytes(Name.Name.getString());
|
2018-06-27 18:13:40 +02:00
|
|
|
// Emit a null terminator.
|
|
|
|
Asm->emitInt8(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HeaderEmitted)
|
|
|
|
return;
|
|
|
|
Asm->emitInt32(0); // End marker.
|
2020-02-15 04:21:58 +01:00
|
|
|
Asm->OutStreamer->emitLabel(EndLabel);
|
2018-06-27 18:13:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit .debug_pubnames for \p Unit.
|
|
|
|
void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) {
|
|
|
|
emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
|
|
|
|
"names", Unit, Unit.getPubnames());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit .debug_pubtypes for \p Unit.
|
|
|
|
void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) {
|
|
|
|
emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
|
|
|
|
"types", Unit, Unit.getPubtypes());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a CIE into the debug_frame section.
|
|
|
|
void DwarfStreamer::emitCIE(StringRef CIEBytes) {
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
|
|
|
|
|
2020-02-15 03:16:24 +01:00
|
|
|
MS->emitBytes(CIEBytes);
|
2018-06-27 18:13:40 +02:00
|
|
|
FrameSectionSize += CIEBytes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a FDE into the debug_frame section. \p FDEBytes
|
|
|
|
/// contains the FDE data without the length, CIE offset and address
|
|
|
|
/// which will be replaced with the parameter values.
|
|
|
|
void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize,
|
|
|
|
uint32_t Address, StringRef FDEBytes) {
|
|
|
|
MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
|
|
|
|
|
2020-02-15 07:40:47 +01:00
|
|
|
MS->emitIntValue(FDEBytes.size() + 4 + AddrSize, 4);
|
|
|
|
MS->emitIntValue(CIEOffset, 4);
|
|
|
|
MS->emitIntValue(Address, AddrSize);
|
2020-02-15 03:16:24 +01:00
|
|
|
MS->emitBytes(FDEBytes);
|
2018-06-27 18:13:40 +02:00
|
|
|
FrameSectionSize += FDEBytes.size() + 8 + AddrSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|