26 : TheTriple(TheTriple), U(U) {}
34 if (
Error Err = init(TheTriple))
46 emitLineTablePrologue(LineTable.
Prologue, OutSection);
49 emitLineTableRows(LineTable, OutSection, OrigRowIndices,
50 RowIndexToSeqStartOffset);
54 assert(OffsetAfterUnitLength -
56 OffsetAfterUnitLength);
57 OutSection.
apply(OffsetAfterUnitLength -
59 dwarf::DW_FORM_sec_offset,
60 OffsetAfterEnd - OffsetAfterUnitLength);
68 std::string TripleName;
81 "no register info for target %s",
87 "no asm info for target %s", TripleName.c_str());
92 "no subtarget info for target %s",
96 new MCContext(TheTriple, *MAI, *
MRI, *MSTI,
nullptr,
true,
"__DWARF"));
102 SectionDescriptor &Section) {
104 Section.emitIntVal(
P.getVersion(), 2);
105 if (
P.getVersion() == 5) {
107 Section.emitIntVal(
P.getAddressSize(), 1);
110 Section.emitIntVal(
P.SegSelectorSize, 1);
114 Section.emitOffset(0xBADDEF);
116 uint64_t OffsetAfterPrologueLength = Section.OS.tell();
117 emitLineTableProloguePayload(
P, Section);
118 uint64_t OffsetAfterPrologueEnd = Section.OS.tell();
121 Section.apply(OffsetAfterPrologueLength -
122 Section.getFormParams().getDwarfOffsetByteSize(),
123 dwarf::DW_FORM_sec_offset,
124 OffsetAfterPrologueEnd - OffsetAfterPrologueLength);
128 emitLineTablePrologueV2IncludeAndFileTable(
const DWARFDebugLine::Prologue &
P,
129 SectionDescriptor &Section) {
131 for (
const DWARFFormValue &
Include :
P.IncludeDirectories) {
134 U.warn(
"cann't read string from line table.");
144 for (
const DWARFDebugLine::FileNameEntry &File :
P.FileNames) {
147 U.warn(
"cann't read string from line table.");
153 Section.emitString(
File.Name.getForm(), *FileNameStr);
170 emitLineTablePrologueV5IncludeAndFileTable(
const DWARFDebugLine::Prologue &
P,
171 SectionDescriptor &Section) {
172 if (
P.IncludeDirectories.empty()) {
187 for (
auto Include :
P.IncludeDirectories) {
190 U.warn(
"cann't read string from line table.");
197 bool HasChecksums =
P.ContentTypes.HasMD5;
198 bool HasInlineSources =
P.ContentTypes.HasSource;
201 dwarf::Form LLVMSourceForm = dwarf::DW_FORM_string;
203 if (
P.FileNames.empty()) {
207 FileNameForm =
P.FileNames[0].Name.getForm();
208 LLVMSourceForm =
P.FileNames[0].Source.getForm();
212 2 + (HasChecksums ? 1 : 0) + (HasInlineSources ? 1 : 0), 1);
226 if (HasInlineSources) {
236 for (
auto File :
P.FileNames) {
239 U.warn(
"cann't read string from line table.");
245 Section.emitString(FileNameForm, *FileNameStr);
250 "checksum size is not equal to 16 bytes.");
252 StringRef(
reinterpret_cast<const char *
>(
File.Checksum.data()),
253 File.Checksum.size()));
256 if (HasInlineSources) {
257 std::optional<const char *> FileSourceStr =
259 if (!FileSourceStr) {
260 U.warn(
"cann't read string from line table.");
264 Section.emitString(LLVMSourceForm, *FileSourceStr);
269 void emitLineTableProloguePayload(
const DWARFDebugLine::Prologue &
P,
270 SectionDescriptor &Section) {
272 Section.emitIntVal(
P.MinInstLength, 1);
273 if (
P.FormParams.Version >= 4) {
275 Section.emitIntVal(
P.MaxOpsPerInst, 1);
278 Section.emitIntVal(
P.DefaultIsStmt, 1);
284 Section.emitIntVal(
P.OpcodeBase, 1);
287 for (
auto Length :
P.StandardOpcodeLengths)
290 if (
P.FormParams.Version < 5)
291 emitLineTablePrologueV2IncludeAndFileTable(
P, Section);
293 emitLineTablePrologueV5IncludeAndFileTable(
P, Section);
296 void emitLineTableRows(
297 const DWARFDebugLine::LineTable &LineTable, SectionDescriptor &Section,
298 ArrayRef<uint64_t> OrigRowIndices = {},
299 DenseMap<uint64_t, uint64_t> *RowIndexToSeqStartOffset =
nullptr) {
301 MCDwarfLineTableParams Params;
302 Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
303 Params.DWARF2LineBase = LineTable.Prologue.LineBase;
304 Params.DWARF2LineRange = LineTable.Prologue.LineRange;
306 SmallString<128> EncodingBuffer;
308 if (LineTable.Rows.empty()) {
313 Section.OS.write(EncodingBuffer.c_str(), EncodingBuffer.size());
318 unsigned FileNum = 1;
319 unsigned LastLine = 1;
322 unsigned IsStatement = 1;
326 unsigned RowsSinceLastSequence = 0;
332 constexpr uint64_t InvalidRowIndex = std::numeric_limits<uint64_t>::max();
334 (!RowIndexToSeqStartOffset ||
335 OrigRowIndices.size() == LineTable.Rows.size()) &&
336 "OrigRowIndices must be supplied alongside RowIndexToSeqStartOffset");
341 CurrentSeqStartOffset =
Section.OS.tell();
342 Section.emitIntVal(dwarf::DW_LNS_extended_op, 1);
344 Section.emitIntVal(dwarf::DW_LNE_set_address, 1);
345 Section.emitIntVal(Row.Address.Address,
346 Section.getFormParams().AddrSize);
350 (Row.Address.Address -
Address) / LineTable.Prologue.MinInstLength;
352 if (RowIndexToSeqStartOffset) {
354 if (InputRowIdx != InvalidRowIndex)
355 (*RowIndexToSeqStartOffset)[InputRowIdx] = CurrentSeqStartOffset;
363 if (FileNum != Row.File) {
365 Section.emitIntVal(dwarf::DW_LNS_set_file, 1);
368 if (Column != Row.Column) {
370 Section.emitIntVal(dwarf::DW_LNS_set_column, 1);
373 if (Discriminator != Row.Discriminator && MC->getDwarfVersion() >= 4) {
376 Section.emitIntVal(dwarf::DW_LNS_extended_op, 1);
378 Section.emitIntVal(dwarf::DW_LNE_set_discriminator, 1);
383 if (Isa != Row.Isa) {
385 Section.emitIntVal(dwarf::DW_LNS_set_isa, 1);
388 if (IsStatement != Row.IsStmt) {
389 IsStatement = Row.IsStmt;
390 Section.emitIntVal(dwarf::DW_LNS_negate_stmt, 1);
393 Section.emitIntVal(dwarf::DW_LNS_set_basic_block, 1);
396 Section.emitIntVal(dwarf::DW_LNS_set_prologue_end, 1);
398 if (Row.EpilogueBegin)
399 Section.emitIntVal(dwarf::DW_LNS_set_epilogue_begin, 1);
401 int64_t LineDelta = int64_t(Row.Line) - LastLine;
402 if (!Row.EndSequence) {
405 Section.OS.write(EncodingBuffer.c_str(), EncodingBuffer.size());
406 EncodingBuffer.resize(0);
409 RowsSinceLastSequence++;
412 Section.emitIntVal(dwarf::DW_LNS_advance_line, 1);
416 Section.emitIntVal(dwarf::DW_LNS_advance_pc, 1);
420 std::numeric_limits<int64_t>::max(), 0,
422 Section.OS.write(EncodingBuffer.c_str(), EncodingBuffer.size());
423 EncodingBuffer.resize(0);
425 LastLine = FileNum = IsStatement = 1;
430 if (RowsSinceLastSequence) {
433 Section.OS.write(EncodingBuffer.c_str(), EncodingBuffer.size());
434 EncodingBuffer.resize(0);
441 MCTargetOptions MCOptions;
442 std::unique_ptr<MCRegisterInfo> MRI;
443 std::unique_ptr<MCAsmInfo> MAI;
444 std::unique_ptr<MCContext> MC;
445 std::unique_ptr<MCSubtargetInfo> MSTI;