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 
9 #include "llvm/DWARFLinker/DWARFStreamer.h"
10 #include "llvm/ADT/Triple.h"
11 #include "llvm/CodeGen/NonRelocatableStringpool.h"
12 #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
13 #include "llvm/DebugInfo/DWARF/DWARFContext.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"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/MC/MCTargetOptions.h"
23 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
24 #include "llvm/Support/LEB128.h"
25 #include "llvm/Support/TargetRegistry.h"
26 #include "llvm/Target/TargetOptions.h"
27 
28 namespace llvm {
29 
init(Triple TheTriple)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 
48   MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags();
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) {
74   case OutputFileType::Assembly: {
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   }
83   case OutputFileType::Object: {
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 
finish()115 void DwarfStreamer::finish() { MS->Finish(); }
116 
switchToDebugInfoSection(unsigned DwarfVersion)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.
emitCompileUnitHeader(CompileUnit & Unit)131 void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
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.
emitAbbrevs(const std::vector<std::unique_ptr<DIEAbbrev>> & Abbrevs,unsigned DwarfVersion)157 void DwarfStreamer::emitAbbrevs(
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.
emitDIE(DIE & Die)166 void DwarfStreamer::emitDIE(DIE &Die) {
167   MS->SwitchSection(MOFI->getDwarfInfoSection());
168   Asm->emitDwarfDIE(Die);
169   DebugInfoSectionSize += Die.getSize();
170 }
171 
172 /// Emit contents of section SecName From Obj.
emitSectionContents(StringRef SecData,StringRef SecName)173 void DwarfStreamer::emitSectionContents(StringRef SecData, StringRef SecName) {
174   MCSection *Section =
175       StringSwitch<MCSection *>(SecName)
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.
emitPaperTrailWarningsDie(DIE & Die)193 void DwarfStreamer::emitPaperTrailWarningsDie(DIE &Die) {
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.
emitStrings(const NonRelocatableStringpool & Pool)205 void DwarfStreamer::emitStrings(const NonRelocatableStringpool &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 
emitDebugNames(AccelTable<DWARF5AccelTableStaticData> & Table)216 void DwarfStreamer::emitDebugNames(
217     AccelTable<DWARF5AccelTableStaticData> &Table) {
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());
232   emitDWARF5AccelTable(
233       Asm.get(), Table, CompUnits,
234       [&UniqueIdToCuMap](const DWARF5AccelTableStaticData &Entry) {
235         return UniqueIdToCuMap[Entry.getCUIndex()];
236       });
237 }
238 
emitAppleNamespaces(AccelTable<AppleAccelTableStaticOffsetData> & Table)239 void DwarfStreamer::emitAppleNamespaces(
240     AccelTable<AppleAccelTableStaticOffsetData> &Table) {
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 
emitAppleNames(AccelTable<AppleAccelTableStaticOffsetData> & Table)247 void DwarfStreamer::emitAppleNames(
248     AccelTable<AppleAccelTableStaticOffsetData> &Table) {
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 
emitAppleObjc(AccelTable<AppleAccelTableStaticOffsetData> & Table)255 void DwarfStreamer::emitAppleObjc(
256     AccelTable<AppleAccelTableStaticOffsetData> &Table) {
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 
emitAppleTypes(AccelTable<AppleAccelTableStaticTypeData> & Table)263 void DwarfStreamer::emitAppleTypes(
264     AccelTable<AppleAccelTableStaticTypeData> &Table) {
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.
emitSwiftAST(StringRef Buffer)272 void DwarfStreamer::emitSwiftAST(StringRef Buffer) {
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.
emitRangesEntries(int64_t UnitPcOffset,uint64_t OrigLowPc,const FunctionIntervals::const_iterator & FuncRange,const std::vector<DWARFDebugRangeList::RangeListEntry> & Entries,unsigned AddressSize)283 void DwarfStreamer::emitRangesEntries(
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.
emitUnitRangesEntries(CompileUnit & Unit,bool DoDebugRanges)322 void DwarfStreamer::emitUnitRangesEntries(CompileUnit &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.
emitLocationsForUnit(const CompileUnit & Unit,DWARFContext & Dwarf,std::function<void (StringRef,SmallVectorImpl<uint8_t> &)> ProcessExpr)402 void DwarfStreamer::emitLocationsForUnit(
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)
414                                    ? std::numeric_limits<uint64_t>::max()
415                                    : std::numeric_limits<uint32_t>::max();
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 
424   SmallVector<uint8_t, 32> Buffer;
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 
emitLineTableForUnit(MCDwarfLineTableParams Params,StringRef PrologueBytes,unsigned MinInstLength,std::vector<DWARFDebugLine::Row> & Rows,unsigned PointerSize)466 void DwarfStreamer::emitLineTableForUnit(MCDwarfLineTableParams Params,
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.
490     MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
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       }
590       MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(),
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) {
602     MCDwarfLineAddr::Encode(*MC, Params, std::numeric_limits<int64_t>::max(), 0,
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.
translateLineTable(DataExtractor Data,uint64_t Offset)614 void DwarfStreamer::translateLineTable(DataExtractor Data, uint64_t Offset) {
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.
emitPubSectionForUnit(MCSection * Sec,StringRef SecName,const CompileUnit & Unit,const std::vector<CompileUnit::AccelInfo> & 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.
emitPubNamesForUnit(const CompileUnit & Unit)737 void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &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.
emitPubTypesForUnit(const CompileUnit & Unit)745 void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &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.
emitCIE(StringRef CIEBytes)753 void DwarfStreamer::emitCIE(StringRef CIEBytes) {
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.
emitFDE(uint32_t CIEOffset,uint32_t AddrSize,uint32_t Address,StringRef FDEBytes)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
775