LLVM  12.0.0git
DWARFStreamer.cpp
Go to the documentation of this file.
1 //===- DwarfStreamer.cpp --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/Triple.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCCodeEmitter.h"
16 #include "llvm/MC/MCDwarf.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Support/LEB128.h"
27 
28 namespace llvm {
29 
30 bool DwarfStreamer::init(Triple TheTriple) {
31  std::string ErrorStr;
32  std::string TripleName;
33  StringRef Context = "dwarf streamer init";
34 
35  // Get the target.
36  const Target *TheTarget =
37  TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
38  if (!TheTarget)
39  return error(ErrorStr, Context), false;
40  TripleName = TheTriple.getTriple();
41 
42  // Create all the MC Objects.
43  MRI.reset(TheTarget->createMCRegInfo(TripleName));
44  if (!MRI)
45  return error(Twine("no register info for target ") + TripleName, Context),
46  false;
47 
49  MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
50  if (!MAI)
51  return error("no asm info for target " + TripleName, Context), false;
52 
53  MOFI.reset(new MCObjectFileInfo);
54  MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
55  MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, *MC);
56 
57  MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
58  if (!MSTI)
59  return error("no subtarget info for target " + TripleName, Context), false;
60 
61  MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
62  if (!MAB)
63  return error("no asm backend for target " + TripleName, Context), false;
64 
65  MII.reset(TheTarget->createMCInstrInfo());
66  if (!MII)
67  return error("no instr info info for target " + TripleName, Context), false;
68 
69  MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
70  if (!MCE)
71  return error("no code emitter for target " + TripleName, Context), false;
72 
73  switch (OutFileType) {
75  MIP = TheTarget->createMCInstPrinter(TheTriple, MAI->getAssemblerDialect(),
76  *MAI, *MII, *MRI);
77  MS = TheTarget->createAsmStreamer(
78  *MC, std::make_unique<formatted_raw_ostream>(OutFile), true, true, MIP,
79  std::unique_ptr<MCCodeEmitter>(MCE), std::unique_ptr<MCAsmBackend>(MAB),
80  true);
81  break;
82  }
84  MS = TheTarget->createMCObjectStreamer(
85  TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB),
86  MAB->createObjectWriter(OutFile), std::unique_ptr<MCCodeEmitter>(MCE),
87  *MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
88  /*DWARFMustBeAtTheEnd*/ false);
89  break;
90  }
91  }
92 
93  if (!MS)
94  return error("no object streamer for target " + TripleName, Context), false;
95 
96  // Finally create the AsmPrinter we'll use to emit the DIEs.
97  TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
98  None));
99  if (!TM)
100  return error("no target machine for target " + TripleName, Context), false;
101 
102  Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
103  if (!Asm)
104  return error("no asm printer for target " + TripleName, Context), false;
105 
106  RangesSectionSize = 0;
107  LocSectionSize = 0;
108  LineSectionSize = 0;
109  FrameSectionSize = 0;
110  DebugInfoSectionSize = 0;
111 
112  return true;
113 }
114 
115 void DwarfStreamer::finish() { MS->Finish(); }
116 
117 void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
118  MS->SwitchSection(MOFI->getDwarfInfoSection());
119  MC->setDwarfVersion(DwarfVersion);
120 }
121 
122 /// Emit the compilation unit header for \p Unit in the debug_info section.
123 ///
124 /// A Dwarf section header is encoded as:
125 /// uint32_t Unit length (omitting this field)
126 /// uint16_t Version
127 /// uint32_t Abbreviation table offset
128 /// uint8_t Address size
129 ///
130 /// Leading to a total of 11 bytes.
132  unsigned Version = Unit.getOrigUnit().getVersion();
133  switchToDebugInfoSection(Version);
134 
135  /// The start of the unit within its section.
136  Unit.setLabelBegin(Asm->createTempSymbol("cu_begin"));
137  Asm->OutStreamer->emitLabel(Unit.getLabelBegin());
138 
139  // Emit size of content not including length itself. The size has already
140  // been computed in CompileUnit::computeOffsets(). Subtract 4 to that size to
141  // account for the length field.
142  Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
143  Asm->emitInt16(Version);
144 
145  // We share one abbreviations table across all units so it's always at the
146  // start of the section.
147  Asm->emitInt32(0);
148  Asm->emitInt8(Unit.getOrigUnit().getAddressByteSize());
149  DebugInfoSectionSize += 11;
150 
151  // Remember this CU.
152  EmittedUnits.push_back({Unit.getUniqueID(), Unit.getLabelBegin()});
153 }
154 
155 /// Emit the \p Abbrevs array as the shared abbreviation table
156 /// for the linked Dwarf file.
158  const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs,
159  unsigned DwarfVersion) {
160  MS->SwitchSection(MOFI->getDwarfAbbrevSection());
161  MC->setDwarfVersion(DwarfVersion);
162  Asm->emitDwarfAbbrevs(Abbrevs);
163 }
164 
165 /// Recursively emit the DIE tree rooted at \p Die.
167  MS->SwitchSection(MOFI->getDwarfInfoSection());
168  Asm->emitDwarfDIE(Die);
169  DebugInfoSectionSize += Die.getSize();
170 }
171 
172 /// Emit contents of section SecName From Obj.
174  MCSection *Section =
176  .Case("debug_line", MC->getObjectFileInfo()->getDwarfLineSection())
177  .Case("debug_loc", MC->getObjectFileInfo()->getDwarfLocSection())
178  .Case("debug_ranges",
179  MC->getObjectFileInfo()->getDwarfRangesSection())
180  .Case("debug_frame", MC->getObjectFileInfo()->getDwarfFrameSection())
181  .Case("debug_aranges",
182  MC->getObjectFileInfo()->getDwarfARangesSection())
183  .Default(nullptr);
184 
185  if (Section) {
186  MS->SwitchSection(Section);
187 
188  MS->emitBytes(SecData);
189  }
190 }
191 
192 /// Emit DIE containing warnings.
194  switchToDebugInfoSection(/* Version */ 2);
195  auto &Asm = getAsmPrinter();
196  Asm.emitInt32(11 + Die.getSize() - 4);
197  Asm.emitInt16(2);
198  Asm.emitInt32(0);
199  Asm.emitInt8(MOFI->getTargetTriple().isArch64Bit() ? 8 : 4);
200  DebugInfoSectionSize += 11;
201  emitDIE(Die);
202 }
203 
204 /// Emit the debug_str section stored in \p Pool.
206  Asm->OutStreamer->SwitchSection(MOFI->getDwarfStrSection());
207  std::vector<DwarfStringPoolEntryRef> Entries = Pool.getEntriesForEmission();
208  for (auto Entry : Entries) {
209  // Emit the string itself.
210  Asm->OutStreamer->emitBytes(Entry.getString());
211  // Emit a null terminator.
212  Asm->emitInt8(0);
213  }
214 }
215 
218  if (EmittedUnits.empty())
219  return;
220 
221  // Build up data structures needed to emit this section.
222  std::vector<MCSymbol *> CompUnits;
223  DenseMap<unsigned, size_t> UniqueIdToCuMap;
224  unsigned Id = 0;
225  for (auto &CU : EmittedUnits) {
226  CompUnits.push_back(CU.LabelBegin);
227  // We might be omitting CUs, so we need to remap them.
228  UniqueIdToCuMap[CU.ID] = Id++;
229  }
230 
231  Asm->OutStreamer->SwitchSection(MOFI->getDwarfDebugNamesSection());
233  Asm.get(), Table, CompUnits,
234  [&UniqueIdToCuMap](const DWARF5AccelTableStaticData &Entry) {
235  return UniqueIdToCuMap[Entry.getCUIndex()];
236  });
237 }
238 
241  Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelNamespaceSection());
242  auto *SectionBegin = Asm->createTempSymbol("namespac_begin");
243  Asm->OutStreamer->emitLabel(SectionBegin);
244  emitAppleAccelTable(Asm.get(), Table, "namespac", SectionBegin);
245 }
246 
249  Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelNamesSection());
250  auto *SectionBegin = Asm->createTempSymbol("names_begin");
251  Asm->OutStreamer->emitLabel(SectionBegin);
252  emitAppleAccelTable(Asm.get(), Table, "names", SectionBegin);
253 }
254 
257  Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelObjCSection());
258  auto *SectionBegin = Asm->createTempSymbol("objc_begin");
259  Asm->OutStreamer->emitLabel(SectionBegin);
260  emitAppleAccelTable(Asm.get(), Table, "objc", SectionBegin);
261 }
262 
265  Asm->OutStreamer->SwitchSection(MOFI->getDwarfAccelTypesSection());
266  auto *SectionBegin = Asm->createTempSymbol("types_begin");
267  Asm->OutStreamer->emitLabel(SectionBegin);
268  emitAppleAccelTable(Asm.get(), Table, "types", SectionBegin);
269 }
270 
271 /// Emit the swift_ast section stored in \p Buffers.
273  MCSection *SwiftASTSection = MOFI->getDwarfSwiftASTSection();
274  SwiftASTSection->setAlignment(Align(32));
275  MS->SwitchSection(SwiftASTSection);
276  MS->emitBytes(Buffer);
277 }
278 
279 /// Emit the debug_range section contents for \p FuncRange by
280 /// translating the original \p Entries. The debug_range section
281 /// format is totally trivial, consisting just of pairs of address
282 /// sized addresses describing the ranges.
284  int64_t UnitPcOffset, uint64_t OrigLowPc,
285  const FunctionIntervals::const_iterator &FuncRange,
286  const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
287  unsigned AddressSize) {
288  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
289 
290  // Offset each range by the right amount.
291  int64_t PcOffset = Entries.empty() ? 0 : FuncRange.value() + UnitPcOffset;
292  for (const auto &Range : Entries) {
293  if (Range.isBaseAddressSelectionEntry(AddressSize)) {
294  warn("unsupported base address selection operation",
295  "emitting debug_ranges");
296  break;
297  }
298  // Do not emit empty ranges.
299  if (Range.StartAddress == Range.EndAddress)
300  continue;
301 
302  // All range entries should lie in the function range.
303  if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
304  Range.EndAddress + OrigLowPc <= FuncRange.stop()))
305  warn("inconsistent range data.", "emitting debug_ranges");
306  MS->emitIntValue(Range.StartAddress + PcOffset, AddressSize);
307  MS->emitIntValue(Range.EndAddress + PcOffset, AddressSize);
308  RangesSectionSize += 2 * AddressSize;
309  }
310 
311  // Add the terminator entry.
312  MS->emitIntValue(0, AddressSize);
313  MS->emitIntValue(0, AddressSize);
314  RangesSectionSize += 2 * AddressSize;
315 }
316 
317 /// Emit the debug_aranges contribution of a unit and
318 /// if \p DoDebugRanges is true the debug_range contents for a
319 /// compile_unit level DW_AT_ranges attribute (Which are basically the
320 /// same thing with a different base address).
321 /// Just aggregate all the ranges gathered inside that unit.
323  bool DoDebugRanges) {
324  unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
325  // Gather the ranges in a vector, so that we can simplify them. The
326  // IntervalMap will have coalesced the non-linked ranges, but here
327  // we want to coalesce the linked addresses.
328  std::vector<std::pair<uint64_t, uint64_t>> Ranges;
329  const auto &FunctionRanges = Unit.getFunctionRanges();
330  for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
331  Range != End; ++Range)
332  Ranges.push_back(std::make_pair(Range.start() + Range.value(),
333  Range.stop() + Range.value()));
334 
335  // The object addresses where sorted, but again, the linked
336  // addresses might end up in a different order.
337  llvm::sort(Ranges);
338 
339  if (!Ranges.empty()) {
340  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
341 
342  MCSymbol *BeginLabel = Asm->createTempSymbol("Barange");
343  MCSymbol *EndLabel = Asm->createTempSymbol("Earange");
344 
345  unsigned HeaderSize =
346  sizeof(int32_t) + // Size of contents (w/o this field
347  sizeof(int16_t) + // DWARF ARange version number
348  sizeof(int32_t) + // Offset of CU in the .debug_info section
349  sizeof(int8_t) + // Pointer Size (in bytes)
350  sizeof(int8_t); // Segment Size (in bytes)
351 
352  unsigned TupleSize = AddressSize * 2;
353  unsigned Padding = offsetToAlignment(HeaderSize, Align(TupleSize));
354 
355  Asm->emitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
356  Asm->OutStreamer->emitLabel(BeginLabel);
357  Asm->emitInt16(dwarf::DW_ARANGES_VERSION); // Version number
358  Asm->emitInt32(Unit.getStartOffset()); // Corresponding unit's offset
359  Asm->emitInt8(AddressSize); // Address size
360  Asm->emitInt8(0); // Segment size
361 
362  Asm->OutStreamer->emitFill(Padding, 0x0);
363 
364  for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
365  ++Range) {
366  uint64_t RangeStart = Range->first;
367  MS->emitIntValue(RangeStart, AddressSize);
368  while ((Range + 1) != End && Range->second == (Range + 1)->first)
369  ++Range;
370  MS->emitIntValue(Range->second - RangeStart, AddressSize);
371  }
372 
373  // Emit terminator
374  Asm->OutStreamer->emitIntValue(0, AddressSize);
375  Asm->OutStreamer->emitIntValue(0, AddressSize);
376  Asm->OutStreamer->emitLabel(EndLabel);
377  }
378 
379  if (!DoDebugRanges)
380  return;
381 
382  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
383  // Offset each range by the right amount.
384  int64_t PcOffset = -Unit.getLowPc();
385  // Emit coalesced ranges.
386  for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
387  MS->emitIntValue(Range->first + PcOffset, AddressSize);
388  while (Range + 1 != End && Range->second == (Range + 1)->first)
389  ++Range;
390  MS->emitIntValue(Range->second + PcOffset, AddressSize);
391  RangesSectionSize += 2 * AddressSize;
392  }
393 
394  // Add the terminator entry.
395  MS->emitIntValue(0, AddressSize);
396  MS->emitIntValue(0, AddressSize);
397  RangesSectionSize += 2 * AddressSize;
398 }
399 
400 /// Emit location lists for \p Unit and update attributes to point to the new
401 /// entries.
403  const CompileUnit &Unit, DWARFContext &Dwarf,
404  std::function<void(StringRef, SmallVectorImpl<uint8_t> &)> ProcessExpr) {
405  const auto &Attributes = Unit.getLocationAttributes();
406 
407  if (Attributes.empty())
408  return;
409 
410  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
411 
412  unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
413  uint64_t BaseAddressMarker = (AddressSize == 8)
416  const DWARFSection &InputSec = Dwarf.getDWARFObj().getLocSection();
417  DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
418  DWARFUnit &OrigUnit = Unit.getOrigUnit();
419  auto OrigUnitDie = OrigUnit.getUnitDIE(false);
420  int64_t UnitPcOffset = 0;
421  if (auto OrigLowPc = dwarf::toAddress(OrigUnitDie.find(dwarf::DW_AT_low_pc)))
422  UnitPcOffset = int64_t(*OrigLowPc) - Unit.getLowPc();
423 
425  for (const auto &Attr : Attributes) {
426  uint64_t Offset = Attr.first.get();
427  Attr.first.set(LocSectionSize);
428  // This is the quantity to add to the old location address to get
429  // the correct address for the new one.
430  int64_t LocPcOffset = Attr.second + UnitPcOffset;
431  while (Data.isValidOffset(Offset)) {
432  uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
433  uint64_t High = Data.getUnsigned(&Offset, AddressSize);
434  LocSectionSize += 2 * AddressSize;
435  // End of list entry.
436  if (Low == 0 && High == 0) {
437  Asm->OutStreamer->emitIntValue(0, AddressSize);
438  Asm->OutStreamer->emitIntValue(0, AddressSize);
439  break;
440  }
441  // Base address selection entry.
442  if (Low == BaseAddressMarker) {
443  Asm->OutStreamer->emitIntValue(BaseAddressMarker, AddressSize);
444  Asm->OutStreamer->emitIntValue(High + Attr.second, AddressSize);
445  LocPcOffset = 0;
446  continue;
447  }
448  // Location list entry.
449  Asm->OutStreamer->emitIntValue(Low + LocPcOffset, AddressSize);
450  Asm->OutStreamer->emitIntValue(High + LocPcOffset, AddressSize);
451  uint64_t Length = Data.getU16(&Offset);
452  Asm->OutStreamer->emitIntValue(Length, 2);
453  // Copy the bytes into to the buffer, process them, emit them.
454  Buffer.reserve(Length);
455  Buffer.resize(0);
456  StringRef Input = InputSec.Data.substr(Offset, Length);
457  ProcessExpr(Input, Buffer);
458  Asm->OutStreamer->emitBytes(
459  StringRef((const char *)Buffer.data(), Length));
460  Offset += Length;
461  LocSectionSize += Length + 2;
462  }
463  }
464 }
465 
467  StringRef PrologueBytes,
468  unsigned MinInstLength,
469  std::vector<DWARFDebugLine::Row> &Rows,
470  unsigned PointerSize) {
471  // Switch to the section where the table will be emitted into.
472  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
473  MCSymbol *LineStartSym = MC->createTempSymbol();
474  MCSymbol *LineEndSym = MC->createTempSymbol();
475 
476  // The first 4 bytes is the total length of the information for this
477  // compilation unit (not including these 4 bytes for the length).
478  Asm->emitLabelDifference(LineEndSym, LineStartSym, 4);
479  Asm->OutStreamer->emitLabel(LineStartSym);
480  // Copy Prologue.
481  MS->emitBytes(PrologueBytes);
482  LineSectionSize += PrologueBytes.size() + 4;
483 
484  SmallString<128> EncodingBuffer;
485  raw_svector_ostream EncodingOS(EncodingBuffer);
486 
487  if (Rows.empty()) {
488  // We only have the dummy entry, dsymutil emits an entry with a 0
489  // address in that case.
491  EncodingOS);
492  MS->emitBytes(EncodingOS.str());
493  LineSectionSize += EncodingBuffer.size();
494  MS->emitLabel(LineEndSym);
495  return;
496  }
497 
498  // Line table state machine fields
499  unsigned FileNum = 1;
500  unsigned LastLine = 1;
501  unsigned Column = 0;
502  unsigned IsStatement = 1;
503  unsigned Isa = 0;
504  uint64_t Address = -1ULL;
505 
506  unsigned RowsSinceLastSequence = 0;
507 
508  for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
509  auto &Row = Rows[Idx];
510 
511  int64_t AddressDelta;
512  if (Address == -1ULL) {
513  MS->emitIntValue(dwarf::DW_LNS_extended_op, 1);
514  MS->emitULEB128IntValue(PointerSize + 1);
515  MS->emitIntValue(dwarf::DW_LNE_set_address, 1);
516  MS->emitIntValue(Row.Address.Address, PointerSize);
517  LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
518  AddressDelta = 0;
519  } else {
520  AddressDelta = (Row.Address.Address - Address) / MinInstLength;
521  }
522 
523  // FIXME: code copied and transformed from MCDwarf.cpp::EmitDwarfLineTable.
524  // We should find a way to share this code, but the current compatibility
525  // requirement with classic dsymutil makes it hard. Revisit that once this
526  // requirement is dropped.
527 
528  if (FileNum != Row.File) {
529  FileNum = Row.File;
530  MS->emitIntValue(dwarf::DW_LNS_set_file, 1);
531  MS->emitULEB128IntValue(FileNum);
532  LineSectionSize += 1 + getULEB128Size(FileNum);
533  }
534  if (Column != Row.Column) {
535  Column = Row.Column;
536  MS->emitIntValue(dwarf::DW_LNS_set_column, 1);
537  MS->emitULEB128IntValue(Column);
538  LineSectionSize += 1 + getULEB128Size(Column);
539  }
540 
541  // FIXME: We should handle the discriminator here, but dsymutil doesn't
542  // consider it, thus ignore it for now.
543 
544  if (Isa != Row.Isa) {
545  Isa = Row.Isa;
546  MS->emitIntValue(dwarf::DW_LNS_set_isa, 1);
547  MS->emitULEB128IntValue(Isa);
548  LineSectionSize += 1 + getULEB128Size(Isa);
549  }
550  if (IsStatement != Row.IsStmt) {
551  IsStatement = Row.IsStmt;
552  MS->emitIntValue(dwarf::DW_LNS_negate_stmt, 1);
553  LineSectionSize += 1;
554  }
555  if (Row.BasicBlock) {
556  MS->emitIntValue(dwarf::DW_LNS_set_basic_block, 1);
557  LineSectionSize += 1;
558  }
559 
560  if (Row.PrologueEnd) {
561  MS->emitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
562  LineSectionSize += 1;
563  }
564 
565  if (Row.EpilogueBegin) {
566  MS->emitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
567  LineSectionSize += 1;
568  }
569 
570  int64_t LineDelta = int64_t(Row.Line) - LastLine;
571  if (!Row.EndSequence) {
572  MCDwarfLineAddr::Encode(*MC, Params, LineDelta, AddressDelta, EncodingOS);
573  MS->emitBytes(EncodingOS.str());
574  LineSectionSize += EncodingBuffer.size();
575  EncodingBuffer.resize(0);
576  Address = Row.Address.Address;
577  LastLine = Row.Line;
578  RowsSinceLastSequence++;
579  } else {
580  if (LineDelta) {
581  MS->emitIntValue(dwarf::DW_LNS_advance_line, 1);
582  MS->emitSLEB128IntValue(LineDelta);
583  LineSectionSize += 1 + getSLEB128Size(LineDelta);
584  }
585  if (AddressDelta) {
586  MS->emitIntValue(dwarf::DW_LNS_advance_pc, 1);
587  MS->emitULEB128IntValue(AddressDelta);
588  LineSectionSize += 1 + getULEB128Size(AddressDelta);
589  }
591  0, EncodingOS);
592  MS->emitBytes(EncodingOS.str());
593  LineSectionSize += EncodingBuffer.size();
594  EncodingBuffer.resize(0);
595  Address = -1ULL;
596  LastLine = FileNum = IsStatement = 1;
597  RowsSinceLastSequence = Column = Isa = 0;
598  }
599  }
600 
601  if (RowsSinceLastSequence) {
603  EncodingOS);
604  MS->emitBytes(EncodingOS.str());
605  LineSectionSize += EncodingBuffer.size();
606  EncodingBuffer.resize(0);
607  }
608 
609  MS->emitLabel(LineEndSym);
610 }
611 
612 /// Copy the debug_line over to the updated binary while unobfuscating the file
613 /// names and directories.
615  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
616  StringRef Contents = Data.getData();
617 
618  // We have to deconstruct the line table header, because it contains to
619  // length fields that will need to be updated when we change the length of
620  // the files and directories in there.
621  unsigned UnitLength = Data.getU32(&Offset);
622  uint64_t UnitEnd = Offset + UnitLength;
623  MCSymbol *BeginLabel = MC->createTempSymbol();
624  MCSymbol *EndLabel = MC->createTempSymbol();
625  unsigned Version = Data.getU16(&Offset);
626 
627  if (Version > 5) {
628  warn("Unsupported line table version: dropping contents and not "
629  "unobfsucating line table.");
630  return;
631  }
632 
633  Asm->emitLabelDifference(EndLabel, BeginLabel, 4);
634  Asm->OutStreamer->emitLabel(BeginLabel);
635  Asm->emitInt16(Version);
636  LineSectionSize += 6;
637 
638  MCSymbol *HeaderBeginLabel = MC->createTempSymbol();
639  MCSymbol *HeaderEndLabel = MC->createTempSymbol();
640  Asm->emitLabelDifference(HeaderEndLabel, HeaderBeginLabel, 4);
641  Asm->OutStreamer->emitLabel(HeaderBeginLabel);
642  Offset += 4;
643  LineSectionSize += 4;
644 
645  uint64_t AfterHeaderLengthOffset = Offset;
646  // Skip to the directories.
647  Offset += (Version >= 4) ? 5 : 4;
648  unsigned OpcodeBase = Data.getU8(&Offset);
649  Offset += OpcodeBase - 1;
650  Asm->OutStreamer->emitBytes(Contents.slice(AfterHeaderLengthOffset, Offset));
651  LineSectionSize += Offset - AfterHeaderLengthOffset;
652 
653  // Offset points to the first directory.
654  while (const char *Dir = Data.getCStr(&Offset)) {
655  if (Dir[0] == 0)
656  break;
657 
658  StringRef Translated = Translator(Dir);
659  Asm->OutStreamer->emitBytes(Translated);
660  Asm->emitInt8(0);
661  LineSectionSize += Translated.size() + 1;
662  }
663  Asm->emitInt8(0);
664  LineSectionSize += 1;
665 
666  while (const char *File = Data.getCStr(&Offset)) {
667  if (File[0] == 0)
668  break;
669 
670  StringRef Translated = Translator(File);
671  Asm->OutStreamer->emitBytes(Translated);
672  Asm->emitInt8(0);
673  LineSectionSize += Translated.size() + 1;
674 
675  uint64_t OffsetBeforeLEBs = Offset;
676  Asm->emitULEB128(Data.getULEB128(&Offset));
677  Asm->emitULEB128(Data.getULEB128(&Offset));
678  Asm->emitULEB128(Data.getULEB128(&Offset));
679  LineSectionSize += Offset - OffsetBeforeLEBs;
680  }
681  Asm->emitInt8(0);
682  LineSectionSize += 1;
683 
684  Asm->OutStreamer->emitLabel(HeaderEndLabel);
685 
686  // Copy the actual line table program over.
687  Asm->OutStreamer->emitBytes(Contents.slice(Offset, UnitEnd));
688  LineSectionSize += UnitEnd - Offset;
689 
690  Asm->OutStreamer->emitLabel(EndLabel);
691  Offset = UnitEnd;
692 }
693 
694 /// Emit the pubnames or pubtypes section contribution for \p
695 /// Unit into \p Sec. The data is provided in \p Names.
696 void DwarfStreamer::emitPubSectionForUnit(
697  MCSection *Sec, StringRef SecName, const CompileUnit &Unit,
698  const std::vector<CompileUnit::AccelInfo> &Names) {
699  if (Names.empty())
700  return;
701 
702  // Start the dwarf pubnames section.
703  Asm->OutStreamer->SwitchSection(Sec);
704  MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + SecName + "_begin");
705  MCSymbol *EndLabel = Asm->createTempSymbol("pub" + SecName + "_end");
706 
707  bool HeaderEmitted = false;
708  // Emit the pubnames for this compilation unit.
709  for (const auto &Name : Names) {
710  if (Name.SkipPubSection)
711  continue;
712 
713  if (!HeaderEmitted) {
714  // Emit the header.
715  Asm->emitLabelDifference(EndLabel, BeginLabel, 4); // Length
716  Asm->OutStreamer->emitLabel(BeginLabel);
717  Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); // Version
718  Asm->emitInt32(Unit.getStartOffset()); // Unit offset
719  Asm->emitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size
720  HeaderEmitted = true;
721  }
722  Asm->emitInt32(Name.Die->getOffset());
723 
724  // Emit the string itself.
725  Asm->OutStreamer->emitBytes(Name.Name.getString());
726  // Emit a null terminator.
727  Asm->emitInt8(0);
728  }
729 
730  if (!HeaderEmitted)
731  return;
732  Asm->emitInt32(0); // End marker.
733  Asm->OutStreamer->emitLabel(EndLabel);
734 }
735 
736 /// Emit .debug_pubnames for \p Unit.
738  if (Minimize)
739  return;
740  emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubNamesSection(),
741  "names", Unit, Unit.getPubnames());
742 }
743 
744 /// Emit .debug_pubtypes for \p Unit.
746  if (Minimize)
747  return;
748  emitPubSectionForUnit(MC->getObjectFileInfo()->getDwarfPubTypesSection(),
749  "types", Unit, Unit.getPubtypes());
750 }
751 
752 /// Emit a CIE into the debug_frame section.
754  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
755 
756  MS->emitBytes(CIEBytes);
757  FrameSectionSize += CIEBytes.size();
758 }
759 
760 /// Emit a FDE into the debug_frame section. \p FDEBytes
761 /// contains the FDE data without the length, CIE offset and address
762 /// which will be replaced with the parameter values.
763 void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize,
764  uint32_t Address, StringRef FDEBytes) {
765  MS->SwitchSection(MC->getObjectFileInfo()->getDwarfFrameSection());
766 
767  MS->emitIntValue(FDEBytes.size() + 4 + AddrSize, 4);
768  MS->emitIntValue(CIEOffset, 4);
769  MS->emitIntValue(Address, AddrSize);
770  MS->emitBytes(FDEBytes);
771  FrameSectionSize += FDEBytes.size() + 8 + AddrSize;
772 }
773 
774 } // namespace llvm
unsigned getSize() const
Definition: DIE.h:762
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:395
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
MCAsmInfo * createMCAsmInfo(const MCRegisterInfo &MRI, StringRef TheTriple, const MCTargetOptions &Options) const
createMCAsmInfo - Create a MCAsmInfo implementation for the specified target triple.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
MCStreamer * createMCObjectStreamer(const Triple &T, MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&Emitter, const MCSubtargetInfo &STI, bool RelaxAll, bool IncrementalLinkerCompatible, bool DWARFMustBeAtTheEnd) const
Create a target specific MCStreamer.
LLVMContext & Context
void emitAppleAccelTable(AsmPrinter *Asm, AccelTable< DataT > &Contents, StringRef Prefix, const MCSymbol *SecBegin)
Emit an Apple Accelerator Table consisting of entries in the specified AccelTable.
Definition: AccelTable.h:302
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
DEBUG_TYPE to vector
void setLabelBegin(MCSymbol *S)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
uint64_t getLowPc() const
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Stores all information relating to a compile unit, be it in its original instance in the object file ...
DWARF v5 .debug_names.
void emitSwiftAST(StringRef Buffer)
Emit the swift_ast section stored in Buffer.
This class holds an abstract representation of an Accelerator Table, consisting of a sequence of buck...
Definition: AccelTable.h:196
MCAsmBackend * createMCAsmBackend(const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options) const
createMCAsmBackend - Create a target specific assembly parser.
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
void emitLineTableForUnit(MCDwarfLineTableParams Params, StringRef PrologueBytes, unsigned MinInstLength, std::vector< DWARFDebugLine::Row > &Rows, unsigned AdddressSize) override
Emit the line table described in Rows into the debug_line section.
virtual const DWARFSection & getLocSection() const
Definition: DWARFObject.h:40
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:550
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:92
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
void reserve(size_type N)
Definition: SmallVector.h:415
uint64_t getNextUnitOffset() const
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: Alignment.h:206
uint64_t High
Optional< uint64_t > toAddress(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an address.
static void Encode(MCContext &Context, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS)
Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Definition: MCDwarf.cpp:689
void emitPaperTrailWarningsDie(DIE &Die) override
Emit DIE containing warnings.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection) override
Emit debug_aranges entries for Unit and if DoRangesSection is true, also emit the debug_ranges entrie...
MCInstPrinter * createMCInstPrinter(const Triple &T, unsigned SyntaxVariant, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:713
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:612
void emitAppleTypes(AccelTable< AppleAccelTableStaticTypeData > &Table) override
Emit Apple type accelerator table.
virtual void emitBytes(StringRef Data)
Emit the bytes in Data into the output.
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:288
Context object for machine code objects.
Definition: MCContext.h:67
void emitPubNamesForUnit(const CompileUnit &Unit) override
Emit the .debug_pubnames contribution for Unit.
MCTargetOptions InitMCTargetOptionsFromFlags()
void switchToDebugInfoSection(unsigned DwarfVersion)
Set the current output section to debug_info and change the MC Dwarf version to DwarfVersion.
const std::vector< std::pair< PatchLocation, int64_t > > & getLocationAttributes() const
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:160
void emitCompileUnitHeader(CompileUnit &Unit) override
Emit the compilation unit header for Unit in the debug_info section.
void emitDebugNames(AccelTable< DWARF5AccelTableStaticData > &Table) override
Emit DWARF debug names.
void emitRangesEntries(int64_t UnitPcOffset, uint64_t OrigLowPc, const FunctionIntervals::const_iterator &FuncRange, const std::vector< DWARFDebugRangeList::RangeListEntry > &Entries, unsigned AddressSize) override
Emit debug_ranges for FuncRange by translating the original Entries.
MCCodeEmitter * createMCCodeEmitter(const MCInstrInfo &II, const MCRegisterInfo &MRI, MCContext &Ctx) const
createMCCodeEmitter - Create a target specific code emitter.
uint16_t getVersion() const
Definition: DWARFUnit.h:287
void emitAbbrevs(const std::vector< std::unique_ptr< DIEAbbrev >> &Abbrevs, unsigned DwarfVersion) override
Emit the abbreviation table Abbrevs to the debug_abbrev section.
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
unsigned getUniqueID() const
std::vector< DwarfStringPoolEntryRef > getEntriesForEmission() const
Return the list of strings to be emitted.
AsmPrinter * createAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > &&Streamer) const
createAsmPrinter - Create a target specific assembly printer pass.
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:131
Instrumentation for Order File
const char * getCStr(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
void emitAppleObjc(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple Objective-C accelerator table.
A structured debug information entry.
Definition: DIE.h:723
const KeyT & start() const
start - Return the beginning of the current interval.
Definition: IntervalMap.h:1364
void emitSectionContents(StringRef SecData, StringRef SecName) override
Emit contents of section SecName From Obj.
uint64_t getStartOffset() const
void setAlignment(Align Value)
Definition: MCSection.h:132
void emitULEB128IntValue(uint64_t Value, unsigned PadTo=0)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
Definition: MCStreamer.cpp:144
MCRegisterInfo * createMCRegInfo(StringRef TT) const
createMCRegInfo - Create a MCRegisterInfo implementation.
uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
void emitCIE(StringRef CIEBytes) override
Emit a CIE.
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1433
void emitAppleNames(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple names accelerator table.
void Finish()
Finish emission of machine code.
Definition: MCStreamer.cpp:950
void emitStrings(const NonRelocatableStringpool &Pool) override
Emit the string table described by Pool.
void emitDWARF5AccelTable(AsmPrinter *Asm, AccelTable< DWARF5AccelTableData > &Contents, const DwarfDebug &DD, ArrayRef< std::unique_ptr< DwarfCompileUnit >> CUs)
Definition: AccelTable.cpp:551
void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf, std::function< void(StringRef, SmallVectorImpl< uint8_t > &)> ProcessExpr) override
Emit the debug_loc contribution for Unit by copying the entries from Dwarf and offsetting them...
void emitSLEB128IntValue(int64_t Value)
Special case of EmitSLEB128Value that avoids the client having to pass in a MCExpr for constant integ...
Definition: MCStreamer.cpp:153
StringRef str() const
Return a StringRef for the vector contents.
Definition: raw_ostream.h:575
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition: LEB128.cpp:19
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:381
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:350
void emitPubTypesForUnit(const CompileUnit &Unit) override
Emit the .debug_pubtypes contribution for Unit.
const std::vector< AccelInfo > & getPubnames() const
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:53
void finish()
Dump the file to the disk.
std::unique_ptr< MCObjectWriter > createObjectWriter(raw_pwrite_stream &OS) const
Create a new MCObjectWriter instance for use by the assembler backend to emit the final object file...
Target - Wrapper for Target specific information.
const ValT & value() const
value - Return the mapped value at the current interval.
Definition: IntervalMap.h:1370
const std::string & getTriple() const
Definition: Triple.h:371
AsmPrinter & getAsmPrinter() const
Definition: DWARFStreamer.h:56
MCSubtargetInfo * createMCSubtargetInfo(StringRef TheTriple, StringRef CPU, StringRef Features) const
createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
const KeyT & stop() const
stop - Return the end of the current interval.
Definition: IntervalMap.h:1367
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:171
const std::vector< AccelInfo > & getPubtypes() const
void translateLineTable(DataExtractor LineData, uint64_t Offset) override
Copy the debug_line over to the updated binary while unobfuscating the file names and directories...
bool init(Triple TheTriple)
void emitDIE(DIE &Die) override
Recursively emit the DIE tree rooted at Die.
MCStreamer * createAsmStreamer(MCContext &Ctx, std::unique_ptr< formatted_raw_ostream > OS, bool IsVerboseAsm, bool UseDwarfDirectory, MCInstPrinter *InstPrint, std::unique_ptr< MCCodeEmitter > &&CE, std::unique_ptr< MCAsmBackend > &&TAB, bool ShowInst) const
bool isLittleEndian() const
Definition: DWARFContext.h:360
size_t size() const
Definition: SmallVector.h:66
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:130
const FunctionIntervals & getFunctionRanges() const
unsigned getSLEB128Size(int64_t Value)
Utility function to get the size of the SLEB128-encoded value.
Definition: LEB128.cpp:29
A string table that doesn&#39;t need relocations.
print Print MemDeps of function
void emitFDE(uint32_t CIEOffset, uint32_t AddreSize, uint32_t Address, StringRef Bytes) override
Emit an FDE with data Bytes.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
const uint64_t Version
Definition: InstrProf.h:996
void emitAppleNamespaces(AccelTable< AppleAccelTableStaticOffsetData > &Table) override
Emit Apple namespaces accelerator table.
DWARFUnit & getOrigUnit() const
void resize(size_type N)
Definition: SmallVector.h:390