[dsymutil] Add support for linking line tables.
[oota-llvm.git] / tools / dsymutil / DwarfLinker.cpp
1 //===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "DebugMap.h"
10 #include "BinaryHolder.h"
11 #include "DebugMap.h"
12 #include "dsymutil.h"
13 #include "llvm/ADT/IntervalMap.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/DIE.h"
18 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
20 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCCodeEmitter.h"
25 #include "llvm/MC/MCDwarf.h"
26 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCObjectFileInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/Object/MachO.h"
31 #include "llvm/Support/Dwarf.h"
32 #include "llvm/Support/LEB128.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetOptions.h"
36 #include <string>
37 #include <tuple>
38
39 namespace llvm {
40 namespace dsymutil {
41
42 namespace {
43
44 void warn(const Twine &Warning, const Twine &Context) {
45   errs() << Twine("while processing ") + Context + ":\n";
46   errs() << Twine("warning: ") + Warning + "\n";
47 }
48
49 bool error(const Twine &Error, const Twine &Context) {
50   errs() << Twine("while processing ") + Context + ":\n";
51   errs() << Twine("error: ") + Error + "\n";
52   return false;
53 }
54
55 template <typename KeyT, typename ValT>
56 using HalfOpenIntervalMap =
57     IntervalMap<KeyT, ValT, IntervalMapImpl::NodeSizer<KeyT, ValT>::LeafSize,
58                 IntervalMapHalfOpenInfo<KeyT>>;
59
60 typedef HalfOpenIntervalMap<uint64_t, int64_t> FunctionIntervals;
61
62 /// \brief Stores all information relating to a compile unit, be it in
63 /// its original instance in the object file to its brand new cloned
64 /// and linked DIE tree.
65 class CompileUnit {
66 public:
67   /// \brief Information gathered about a DIE in the object file.
68   struct DIEInfo {
69     int64_t AddrAdjust; ///< Address offset to apply to the described entity.
70     DIE *Clone;         ///< Cloned version of that DIE.
71     uint32_t ParentIdx; ///< The index of this DIE's parent.
72     bool Keep;          ///< Is the DIE part of the linked output?
73     bool InDebugMap;    ///< Was this DIE's entity found in the map?
74   };
75
76   CompileUnit(DWARFUnit &OrigUnit, unsigned ID)
77       : OrigUnit(OrigUnit), ID(ID), LowPc(UINT64_MAX), HighPc(0), RangeAlloc(),
78         Ranges(RangeAlloc), UnitRangeAttribute(nullptr) {
79     Info.resize(OrigUnit.getNumDIEs());
80   }
81
82   CompileUnit(CompileUnit &&RHS)
83       : OrigUnit(RHS.OrigUnit), Info(std::move(RHS.Info)),
84         CUDie(std::move(RHS.CUDie)), StartOffset(RHS.StartOffset),
85         NextUnitOffset(RHS.NextUnitOffset), RangeAlloc(), Ranges(RangeAlloc) {
86     // The CompileUnit container has been 'reserve()'d with the right
87     // size. We cannot move the IntervalMap anyway.
88     llvm_unreachable("CompileUnits should not be moved.");
89   }
90
91   DWARFUnit &getOrigUnit() const { return OrigUnit; }
92
93   unsigned getUniqueID() const { return ID; }
94
95   DIE *getOutputUnitDIE() const { return CUDie.get(); }
96   void setOutputUnitDIE(DIE *Die) { CUDie.reset(Die); }
97
98   DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
99   const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
100
101   uint64_t getStartOffset() const { return StartOffset; }
102   uint64_t getNextUnitOffset() const { return NextUnitOffset; }
103   void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
104
105   uint64_t getLowPc() const { return LowPc; }
106   uint64_t getHighPc() const { return HighPc; }
107
108   DIEInteger *getUnitRangesAttribute() const { return UnitRangeAttribute; }
109   const FunctionIntervals &getFunctionRanges() const { return Ranges; }
110   const std::vector<DIEInteger *> &getRangesAttributes() const {
111     return RangeAttributes;
112   }
113
114   const std::vector<std::pair<DIEInteger *, int64_t>> &
115   getLocationAttributes() const {
116     return LocationAttributes;
117   }
118
119   /// \brief Compute the end offset for this unit. Must be
120   /// called after the CU's DIEs have been cloned.
121   /// \returns the next unit offset (which is also the current
122   /// debug_info section size).
123   uint64_t computeNextUnitOffset();
124
125   /// \brief Keep track of a forward reference to DIE \p Die in \p
126   /// RefUnit by \p Attr. The attribute should be fixed up later to
127   /// point to the absolute offset of \p Die in the debug_info section.
128   void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
129                             DIEInteger *Attr);
130
131   /// \brief Apply all fixups recored by noteForwardReference().
132   void fixupForwardReferences();
133
134   /// \brief Add a function range [\p LowPC, \p HighPC) that is
135   /// relocatad by applying offset \p PCOffset.
136   void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
137
138   /// \brief Keep track of a DW_AT_range attribute that we will need to
139   /// patch up later.
140   void noteRangeAttribute(const DIE &Die, DIEInteger *Attr);
141
142   /// \brief Keep track of a location attribute pointing to a location
143   /// list in the debug_loc section.
144   void noteLocationAttribute(DIEInteger *Attr, int64_t PcOffset);
145
146 private:
147   DWARFUnit &OrigUnit;
148   unsigned ID;
149   std::vector<DIEInfo> Info;  ///< DIE info indexed by DIE index.
150   std::unique_ptr<DIE> CUDie; ///< Root of the linked DIE tree.
151
152   uint64_t StartOffset;
153   uint64_t NextUnitOffset;
154
155   uint64_t LowPc;
156   uint64_t HighPc;
157
158   /// \brief A list of attributes to fixup with the absolute offset of
159   /// a DIE in the debug_info section.
160   ///
161   /// The offsets for the attributes in this array couldn't be set while
162   /// cloning because for cross-cu forward refences the target DIE's
163   /// offset isn't known you emit the reference attribute.
164   std::vector<std::tuple<DIE *, const CompileUnit *, DIEInteger *>>
165       ForwardDIEReferences;
166
167   FunctionIntervals::Allocator RangeAlloc;
168   /// \brief The ranges in that interval map are the PC ranges for
169   /// functions in this unit, associated with the PC offset to apply
170   /// to the addresses to get the linked address.
171   FunctionIntervals Ranges;
172
173   /// \brief DW_AT_ranges attributes to patch after we have gathered
174   /// all the unit's function addresses.
175   /// @{
176   std::vector<DIEInteger *> RangeAttributes;
177   DIEInteger *UnitRangeAttribute;
178   /// @}
179
180   /// \brief Location attributes that need to be transfered from th
181   /// original debug_loc section to the liked one. They are stored
182   /// along with the PC offset that is to be applied to their
183   /// function's address.
184   std::vector<std::pair<DIEInteger *, int64_t>> LocationAttributes;
185 };
186
187 uint64_t CompileUnit::computeNextUnitOffset() {
188   NextUnitOffset = StartOffset + 11 /* Header size */;
189   // The root DIE might be null, meaning that the Unit had nothing to
190   // contribute to the linked output. In that case, we will emit the
191   // unit header without any actual DIE.
192   if (CUDie)
193     NextUnitOffset += CUDie->getSize();
194   return NextUnitOffset;
195 }
196
197 /// \brief Keep track of a forward cross-cu reference from this unit
198 /// to \p Die that lives in \p RefUnit.
199 void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
200                                        DIEInteger *Attr) {
201   ForwardDIEReferences.emplace_back(Die, RefUnit, Attr);
202 }
203
204 /// \brief Apply all fixups recorded by noteForwardReference().
205 void CompileUnit::fixupForwardReferences() {
206   for (const auto &Ref : ForwardDIEReferences) {
207     DIE *RefDie;
208     const CompileUnit *RefUnit;
209     DIEInteger *Attr;
210     std::tie(RefDie, RefUnit, Attr) = Ref;
211     Attr->setValue(RefDie->getOffset() + RefUnit->getStartOffset());
212   }
213 }
214
215 void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
216                                    int64_t PcOffset) {
217   Ranges.insert(FuncLowPc, FuncHighPc, PcOffset);
218   this->LowPc = std::min(LowPc, FuncLowPc + PcOffset);
219   this->HighPc = std::max(HighPc, FuncHighPc + PcOffset);
220 }
221
222 void CompileUnit::noteRangeAttribute(const DIE &Die, DIEInteger *Attr) {
223   if (Die.getTag() != dwarf::DW_TAG_compile_unit)
224     RangeAttributes.push_back(Attr);
225   else
226     UnitRangeAttribute = Attr;
227 }
228
229 void CompileUnit::noteLocationAttribute(DIEInteger *Attr, int64_t PcOffset) {
230   LocationAttributes.emplace_back(Attr, PcOffset);
231 }
232
233 /// \brief A string table that doesn't need relocations.
234 ///
235 /// We are doing a final link, no need for a string table that
236 /// has relocation entries for every reference to it. This class
237 /// provides this ablitity by just associating offsets with
238 /// strings.
239 class NonRelocatableStringpool {
240 public:
241   /// \brief Entries are stored into the StringMap and simply linked
242   /// together through the second element of this pair in order to
243   /// keep track of insertion order.
244   typedef StringMap<std::pair<uint32_t, StringMapEntryBase *>, BumpPtrAllocator>
245       MapTy;
246
247   NonRelocatableStringpool()
248       : CurrentEndOffset(0), Sentinel(0), Last(&Sentinel) {
249     // Legacy dsymutil puts an empty string at the start of the line
250     // table.
251     getStringOffset("");
252   }
253
254   /// \brief Get the offset of string \p S in the string table. This
255   /// can insert a new element or return the offset of a preexisitng
256   /// one.
257   uint32_t getStringOffset(StringRef S);
258
259   /// \brief Get permanent storage for \p S (but do not necessarily
260   /// emit \p S in the output section).
261   /// \returns The StringRef that points to permanent storage to use
262   /// in place of \p S.
263   StringRef internString(StringRef S);
264
265   // \brief Return the first entry of the string table.
266   const MapTy::MapEntryTy *getFirstEntry() const {
267     return getNextEntry(&Sentinel);
268   }
269
270   // \brief Get the entry following \p E in the string table or null
271   // if \p E was the last entry.
272   const MapTy::MapEntryTy *getNextEntry(const MapTy::MapEntryTy *E) const {
273     return static_cast<const MapTy::MapEntryTy *>(E->getValue().second);
274   }
275
276   uint64_t getSize() { return CurrentEndOffset; }
277
278 private:
279   MapTy Strings;
280   uint32_t CurrentEndOffset;
281   MapTy::MapEntryTy Sentinel, *Last;
282 };
283
284 /// \brief Get the offset of string \p S in the string table. This
285 /// can insert a new element or return the offset of a preexisitng
286 /// one.
287 uint32_t NonRelocatableStringpool::getStringOffset(StringRef S) {
288   if (S.empty() && !Strings.empty())
289     return 0;
290
291   std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
292   MapTy::iterator It;
293   bool Inserted;
294
295   // A non-empty string can't be at offset 0, so if we have an entry
296   // with a 0 offset, it must be a previously interned string.
297   std::tie(It, Inserted) = Strings.insert(std::make_pair(S, Entry));
298   if (Inserted || It->getValue().first == 0) {
299     // Set offset and chain at the end of the entries list.
300     It->getValue().first = CurrentEndOffset;
301     CurrentEndOffset += S.size() + 1; // +1 for the '\0'.
302     Last->getValue().second = &*It;
303     Last = &*It;
304   }
305   return It->getValue().first;
306 }
307
308 /// \brief Put \p S into the StringMap so that it gets permanent
309 /// storage, but do not actually link it in the chain of elements
310 /// that go into the output section. A latter call to
311 /// getStringOffset() with the same string will chain it though.
312 StringRef NonRelocatableStringpool::internString(StringRef S) {
313   std::pair<uint32_t, StringMapEntryBase *> Entry(0, nullptr);
314   auto InsertResult = Strings.insert(std::make_pair(S, Entry));
315   return InsertResult.first->getKey();
316 }
317
318 /// \brief The Dwarf streaming logic
319 ///
320 /// All interactions with the MC layer that is used to build the debug
321 /// information binary representation are handled in this class.
322 class DwarfStreamer {
323   /// \defgroup MCObjects MC layer objects constructed by the streamer
324   /// @{
325   std::unique_ptr<MCRegisterInfo> MRI;
326   std::unique_ptr<MCAsmInfo> MAI;
327   std::unique_ptr<MCObjectFileInfo> MOFI;
328   std::unique_ptr<MCContext> MC;
329   MCAsmBackend *MAB; // Owned by MCStreamer
330   std::unique_ptr<MCInstrInfo> MII;
331   std::unique_ptr<MCSubtargetInfo> MSTI;
332   MCCodeEmitter *MCE; // Owned by MCStreamer
333   MCStreamer *MS;     // Owned by AsmPrinter
334   std::unique_ptr<TargetMachine> TM;
335   std::unique_ptr<AsmPrinter> Asm;
336   /// @}
337
338   /// \brief the file we stream the linked Dwarf to.
339   std::unique_ptr<raw_fd_ostream> OutFile;
340
341   uint32_t RangesSectionSize;
342   uint32_t LocSectionSize;
343   uint32_t LineSectionSize;
344
345 public:
346   /// \brief Actually create the streamer and the ouptut file.
347   ///
348   /// This could be done directly in the constructor, but it feels
349   /// more natural to handle errors through return value.
350   bool init(Triple TheTriple, StringRef OutputFilename);
351
352   /// \brief Dump the file to the disk.
353   bool finish();
354
355   AsmPrinter &getAsmPrinter() const { return *Asm; }
356
357   /// \brief Set the current output section to debug_info and change
358   /// the MC Dwarf version to \p DwarfVersion.
359   void switchToDebugInfoSection(unsigned DwarfVersion);
360
361   /// \brief Emit the compilation unit header for \p Unit in the
362   /// debug_info section.
363   ///
364   /// As a side effect, this also switches the current Dwarf version
365   /// of the MC layer to the one of U.getOrigUnit().
366   void emitCompileUnitHeader(CompileUnit &Unit);
367
368   /// \brief Recursively emit the DIE tree rooted at \p Die.
369   void emitDIE(DIE &Die);
370
371   /// \brief Emit the abbreviation table \p Abbrevs to the
372   /// debug_abbrev section.
373   void emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs);
374
375   /// \brief Emit the string table described by \p Pool.
376   void emitStrings(const NonRelocatableStringpool &Pool);
377
378   /// \brief Emit debug_ranges for \p FuncRange by translating the
379   /// original \p Entries.
380   void emitRangesEntries(
381       int64_t UnitPcOffset, uint64_t OrigLowPc,
382       FunctionIntervals::const_iterator FuncRange,
383       const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
384       unsigned AddressSize);
385
386   /// \brief Emit debug_aranges entries for \p Unit and if \p
387   /// DoRangesSection is true, also emit the debug_ranges entries for
388   /// the DW_TAG_compile_unit's DW_AT_ranges attribute.
389   void emitUnitRangesEntries(CompileUnit &Unit, bool DoRangesSection);
390
391   uint32_t getRangesSectionSize() const { return RangesSectionSize; }
392
393   /// \brief Emit the debug_loc contribution for \p Unit by copying
394   /// the entries from \p Dwarf and offseting them. Update the
395   /// location attributes to point to the new entries.
396   void emitLocationsForUnit(const CompileUnit &Unit, DWARFContext &Dwarf);
397
398   /// \brief Emit the line table described in \p Rows into the
399   /// debug_line section.
400   void emitLineTableForUnit(StringRef PrologueBytes, unsigned MinInstLength,
401                             std::vector<DWARFDebugLine::Row> &Rows,
402                             unsigned AdddressSize);
403
404   uint32_t getLineSectionSize() const { return LineSectionSize; }
405 };
406
407 bool DwarfStreamer::init(Triple TheTriple, StringRef OutputFilename) {
408   std::string ErrorStr;
409   std::string TripleName;
410   StringRef Context = "dwarf streamer init";
411
412   // Get the target.
413   const Target *TheTarget =
414       TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
415   if (!TheTarget)
416     return error(ErrorStr, Context);
417   TripleName = TheTriple.getTriple();
418
419   // Create all the MC Objects.
420   MRI.reset(TheTarget->createMCRegInfo(TripleName));
421   if (!MRI)
422     return error(Twine("no register info for target ") + TripleName, Context);
423
424   MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
425   if (!MAI)
426     return error("no asm info for target " + TripleName, Context);
427
428   MOFI.reset(new MCObjectFileInfo);
429   MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
430   MOFI->InitMCObjectFileInfo(TripleName, Reloc::Default, CodeModel::Default,
431                              *MC);
432
433   MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
434   if (!MAB)
435     return error("no asm backend for target " + TripleName, Context);
436
437   MII.reset(TheTarget->createMCInstrInfo());
438   if (!MII)
439     return error("no instr info info for target " + TripleName, Context);
440
441   MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
442   if (!MSTI)
443     return error("no subtarget info for target " + TripleName, Context);
444
445   MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
446   if (!MCE)
447     return error("no code emitter for target " + TripleName, Context);
448
449   // Create the output file.
450   std::error_code EC;
451   OutFile =
452       llvm::make_unique<raw_fd_ostream>(OutputFilename, EC, sys::fs::F_None);
453   if (EC)
454     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
455
456   MS = TheTarget->createMCObjectStreamer(TripleName, *MC, *MAB, *OutFile, MCE,
457                                          *MSTI, false);
458   if (!MS)
459     return error("no object streamer for target " + TripleName, Context);
460
461   // Finally create the AsmPrinter we'll use to emit the DIEs.
462   TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions()));
463   if (!TM)
464     return error("no target machine for target " + TripleName, Context);
465
466   Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
467   if (!Asm)
468     return error("no asm printer for target " + TripleName, Context);
469
470   RangesSectionSize = 0;
471   LocSectionSize = 0;
472   LineSectionSize = 0;
473
474   return true;
475 }
476
477 bool DwarfStreamer::finish() {
478   MS->Finish();
479   return true;
480 }
481
482 /// \brief Set the current output section to debug_info and change
483 /// the MC Dwarf version to \p DwarfVersion.
484 void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) {
485   MS->SwitchSection(MOFI->getDwarfInfoSection());
486   MC->setDwarfVersion(DwarfVersion);
487 }
488
489 /// \brief Emit the compilation unit header for \p Unit in the
490 /// debug_info section.
491 ///
492 /// A Dwarf scetion header is encoded as:
493 ///  uint32_t   Unit length (omiting this field)
494 ///  uint16_t   Version
495 ///  uint32_t   Abbreviation table offset
496 ///  uint8_t    Address size
497 ///
498 /// Leading to a total of 11 bytes.
499 void DwarfStreamer::emitCompileUnitHeader(CompileUnit &Unit) {
500   unsigned Version = Unit.getOrigUnit().getVersion();
501   switchToDebugInfoSection(Version);
502
503   // Emit size of content not including length itself. The size has
504   // already been computed in CompileUnit::computeOffsets(). Substract
505   // 4 to that size to account for the length field.
506   Asm->EmitInt32(Unit.getNextUnitOffset() - Unit.getStartOffset() - 4);
507   Asm->EmitInt16(Version);
508   // We share one abbreviations table across all units so it's always at the
509   // start of the section.
510   Asm->EmitInt32(0);
511   Asm->EmitInt8(Unit.getOrigUnit().getAddressByteSize());
512 }
513
514 /// \brief Emit the \p Abbrevs array as the shared abbreviation table
515 /// for the linked Dwarf file.
516 void DwarfStreamer::emitAbbrevs(const std::vector<DIEAbbrev *> &Abbrevs) {
517   MS->SwitchSection(MOFI->getDwarfAbbrevSection());
518   Asm->emitDwarfAbbrevs(Abbrevs);
519 }
520
521 /// \brief Recursively emit the DIE tree rooted at \p Die.
522 void DwarfStreamer::emitDIE(DIE &Die) {
523   MS->SwitchSection(MOFI->getDwarfInfoSection());
524   Asm->emitDwarfDIE(Die);
525 }
526
527 /// \brief Emit the debug_str section stored in \p Pool.
528 void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) {
529   Asm->OutStreamer.SwitchSection(MOFI->getDwarfStrSection());
530   for (auto *Entry = Pool.getFirstEntry(); Entry;
531        Entry = Pool.getNextEntry(Entry))
532     Asm->OutStreamer.EmitBytes(
533         StringRef(Entry->getKey().data(), Entry->getKey().size() + 1));
534 }
535
536 /// \brief Emit the debug_range section contents for \p FuncRange by
537 /// translating the original \p Entries. The debug_range section
538 /// format is totally trivial, consisting just of pairs of address
539 /// sized addresses describing the ranges.
540 void DwarfStreamer::emitRangesEntries(
541     int64_t UnitPcOffset, uint64_t OrigLowPc,
542     FunctionIntervals::const_iterator FuncRange,
543     const std::vector<DWARFDebugRangeList::RangeListEntry> &Entries,
544     unsigned AddressSize) {
545   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
546
547   // Offset each range by the right amount.
548   int64_t PcOffset = FuncRange.value() + UnitPcOffset;
549   for (const auto &Range : Entries) {
550     if (Range.isBaseAddressSelectionEntry(AddressSize)) {
551       warn("unsupported base address selection operation",
552            "emitting debug_ranges");
553       break;
554     }
555     // Do not emit empty ranges.
556     if (Range.StartAddress == Range.EndAddress)
557       continue;
558
559     // All range entries should lie in the function range.
560     if (!(Range.StartAddress + OrigLowPc >= FuncRange.start() &&
561           Range.EndAddress + OrigLowPc <= FuncRange.stop()))
562       warn("inconsistent range data.", "emitting debug_ranges");
563     MS->EmitIntValue(Range.StartAddress + PcOffset, AddressSize);
564     MS->EmitIntValue(Range.EndAddress + PcOffset, AddressSize);
565     RangesSectionSize += 2 * AddressSize;
566   }
567
568   // Add the terminator entry.
569   MS->EmitIntValue(0, AddressSize);
570   MS->EmitIntValue(0, AddressSize);
571   RangesSectionSize += 2 * AddressSize;
572 }
573
574 /// \brief Emit the debug_aranges contribution of a unit and
575 /// if \p DoDebugRanges is true the debug_range contents for a
576 /// compile_unit level DW_AT_ranges attribute (Which are basically the
577 /// same thing with a different base address).
578 /// Just aggregate all the ranges gathered inside that unit.
579 void DwarfStreamer::emitUnitRangesEntries(CompileUnit &Unit,
580                                           bool DoDebugRanges) {
581   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
582   // Gather the ranges in a vector, so that we can simplify them. The
583   // IntervalMap will have coalesced the non-linked ranges, but here
584   // we want to coalesce the linked addresses.
585   std::vector<std::pair<uint64_t, uint64_t>> Ranges;
586   const auto &FunctionRanges = Unit.getFunctionRanges();
587   for (auto Range = FunctionRanges.begin(), End = FunctionRanges.end();
588        Range != End; ++Range)
589     Ranges.push_back(std::make_pair(Range.start() + Range.value(),
590                                     Range.stop() + Range.value()));
591
592   // The object addresses where sorted, but again, the linked
593   // addresses might end up in a different order.
594   std::sort(Ranges.begin(), Ranges.end());
595
596   if (!Ranges.empty()) {
597     MS->SwitchSection(MC->getObjectFileInfo()->getDwarfARangesSection());
598
599     MCSymbol *BeginLabel = Asm->GetTempSymbol("Barange", Unit.getUniqueID());
600     MCSymbol *EndLabel = Asm->GetTempSymbol("Earange", Unit.getUniqueID());
601
602     unsigned HeaderSize =
603         sizeof(int32_t) + // Size of contents (w/o this field
604         sizeof(int16_t) + // DWARF ARange version number
605         sizeof(int32_t) + // Offset of CU in the .debug_info section
606         sizeof(int8_t) +  // Pointer Size (in bytes)
607         sizeof(int8_t);   // Segment Size (in bytes)
608
609     unsigned TupleSize = AddressSize * 2;
610     unsigned Padding = OffsetToAlignment(HeaderSize, TupleSize);
611
612     Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); // Arange length
613     Asm->OutStreamer.EmitLabel(BeginLabel);
614     Asm->EmitInt16(dwarf::DW_ARANGES_VERSION); // Version number
615     Asm->EmitInt32(Unit.getStartOffset());     // Corresponding unit's offset
616     Asm->EmitInt8(AddressSize);                // Address size
617     Asm->EmitInt8(0);                          // Segment size
618
619     Asm->OutStreamer.EmitFill(Padding, 0x0);
620
621     for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End;
622          ++Range) {
623       uint64_t RangeStart = Range->first;
624       MS->EmitIntValue(RangeStart, AddressSize);
625       while ((Range + 1) != End && Range->second == (Range + 1)->first)
626         ++Range;
627       MS->EmitIntValue(Range->second - RangeStart, AddressSize);
628     }
629
630     // Emit terminator
631     Asm->OutStreamer.EmitIntValue(0, AddressSize);
632     Asm->OutStreamer.EmitIntValue(0, AddressSize);
633     Asm->OutStreamer.EmitLabel(EndLabel);
634   }
635
636   if (!DoDebugRanges)
637     return;
638
639   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfRangesSection());
640   // Offset each range by the right amount.
641   int64_t PcOffset = -Unit.getLowPc();
642   // Emit coalesced ranges.
643   for (auto Range = Ranges.begin(), End = Ranges.end(); Range != End; ++Range) {
644     MS->EmitIntValue(Range->first + PcOffset, AddressSize);
645     while (Range + 1 != End && Range->second == (Range + 1)->first)
646       ++Range;
647     MS->EmitIntValue(Range->second + PcOffset, AddressSize);
648     RangesSectionSize += 2 * AddressSize;
649   }
650
651   // Add the terminator entry.
652   MS->EmitIntValue(0, AddressSize);
653   MS->EmitIntValue(0, AddressSize);
654   RangesSectionSize += 2 * AddressSize;
655 }
656
657 /// \brief Emit location lists for \p Unit and update attribtues to
658 /// point to the new entries.
659 void DwarfStreamer::emitLocationsForUnit(const CompileUnit &Unit,
660                                          DWARFContext &Dwarf) {
661   const std::vector<std::pair<DIEInteger *, int64_t>> &Attributes =
662       Unit.getLocationAttributes();
663
664   if (Attributes.empty())
665     return;
666
667   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLocSection());
668
669   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
670   const DWARFSection &InputSec = Dwarf.getLocSection();
671   DataExtractor Data(InputSec.Data, Dwarf.isLittleEndian(), AddressSize);
672   DWARFUnit &OrigUnit = Unit.getOrigUnit();
673   const auto *OrigUnitDie = OrigUnit.getCompileUnitDIE(false);
674   int64_t UnitPcOffset = 0;
675   uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress(
676       &OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
677   if (OrigLowPc != -1ULL)
678     UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
679
680   for (const auto &Attr : Attributes) {
681     uint32_t Offset = Attr.first->getValue();
682     Attr.first->setValue(LocSectionSize);
683     // This is the quantity to add to the old location address to get
684     // the correct address for the new one.
685     int64_t LocPcOffset = Attr.second + UnitPcOffset;
686     while (Data.isValidOffset(Offset)) {
687       uint64_t Low = Data.getUnsigned(&Offset, AddressSize);
688       uint64_t High = Data.getUnsigned(&Offset, AddressSize);
689       LocSectionSize += 2 * AddressSize;
690       if (Low == 0 && High == 0) {
691         Asm->OutStreamer.EmitIntValue(0, AddressSize);
692         Asm->OutStreamer.EmitIntValue(0, AddressSize);
693         break;
694       }
695       Asm->OutStreamer.EmitIntValue(Low + LocPcOffset, AddressSize);
696       Asm->OutStreamer.EmitIntValue(High + LocPcOffset, AddressSize);
697       uint64_t Length = Data.getU16(&Offset);
698       Asm->OutStreamer.EmitIntValue(Length, 2);
699       // Just copy the bytes over.
700       Asm->OutStreamer.EmitBytes(
701           StringRef(InputSec.Data.substr(Offset, Length)));
702       Offset += Length;
703       LocSectionSize += Length + 2;
704     }
705   }
706 }
707
708 void DwarfStreamer::emitLineTableForUnit(StringRef PrologueBytes,
709                                          unsigned MinInstLength,
710                                          std::vector<DWARFDebugLine::Row> &Rows,
711                                          unsigned PointerSize) {
712   // Switch to the section where the table will be emitted into.
713   MS->SwitchSection(MC->getObjectFileInfo()->getDwarfLineSection());
714   MCSymbol *LineStartSym = MC->CreateTempSymbol();
715   MCSymbol *LineEndSym = MC->CreateTempSymbol();
716
717   // The first 4 bytes is the total length of the information for this
718   // compilation unit (not including these 4 bytes for the length).
719   Asm->EmitLabelDifference(LineEndSym, LineStartSym, 4);
720   Asm->OutStreamer.EmitLabel(LineStartSym);
721   // Copy Prologue.
722   MS->EmitBytes(PrologueBytes);
723   LineSectionSize += PrologueBytes.size() + 4;
724
725   SmallString<16> EncodingBuffer;
726   raw_svector_ostream EncodingOS(EncodingBuffer);
727
728   if (Rows.empty()) {
729     // We only have the dummy entry, dsymutil emits an entry with a 0
730     // address in that case.
731     MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
732     MS->EmitBytes(EncodingOS.str());
733     LineSectionSize += EncodingBuffer.size();
734     EncodingBuffer.resize(0);
735     MS->EmitLabel(LineEndSym);
736     return;
737   }
738
739   // Line table state machine fields
740   unsigned FileNum = 1;
741   unsigned LastLine = 1;
742   unsigned Column = 0;
743   unsigned IsStatement = 1;
744   unsigned Isa = 0;
745   uint64_t Address = -1ULL;
746
747   unsigned RowsSinceLastSequence = 0;
748
749   for (unsigned Idx = 0; Idx < Rows.size(); ++Idx) {
750     auto &Row = Rows[Idx];
751
752     int64_t AddressDelta;
753     if (Address == -1ULL) {
754       MS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
755       MS->EmitULEB128IntValue(PointerSize + 1);
756       MS->EmitIntValue(dwarf::DW_LNE_set_address, 1);
757       MS->EmitIntValue(Row.Address, PointerSize);
758       LineSectionSize += 2 + PointerSize + getULEB128Size(PointerSize + 1);
759       AddressDelta = 0;
760     } else {
761       AddressDelta = (Row.Address - Address) / MinInstLength;
762     }
763
764     // FIXME: code copied and transfromed from
765     // MCDwarf.cpp::EmitDwarfLineTable. We should find a way to share
766     // this code, but the current compatibility requirement with
767     // classic dsymutil makes it hard. Revisit that once this
768     // requirement is dropped.
769
770     if (FileNum != Row.File) {
771       FileNum = Row.File;
772       MS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
773       MS->EmitULEB128IntValue(FileNum);
774       LineSectionSize += 1 + getULEB128Size(FileNum);
775     }
776     if (Column != Row.Column) {
777       Column = Row.Column;
778       MS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
779       MS->EmitULEB128IntValue(Column);
780       LineSectionSize += 1 + getULEB128Size(Column);
781     }
782
783     // FIXME: We should handle the discriminator here, but dsymutil
784     // doesn' consider it, thus ignore it for now.
785
786     if (Isa != Row.Isa) {
787       Isa = Row.Isa;
788       MS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
789       MS->EmitULEB128IntValue(Isa);
790       LineSectionSize += 1 + getULEB128Size(Isa);
791     }
792     if (IsStatement != Row.IsStmt) {
793       IsStatement = Row.IsStmt;
794       MS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
795       LineSectionSize += 1;
796     }
797     if (Row.BasicBlock) {
798       MS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
799       LineSectionSize += 1;
800     }
801
802     if (Row.PrologueEnd) {
803       MS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
804       LineSectionSize += 1;
805     }
806
807     if (Row.EpilogueBegin) {
808       MS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
809       LineSectionSize += 1;
810     }
811
812     int64_t LineDelta = int64_t(Row.Line) - LastLine;
813     if (!Row.EndSequence) {
814       MCDwarfLineAddr::Encode(*MC, LineDelta, AddressDelta, EncodingOS);
815       MS->EmitBytes(EncodingOS.str());
816       LineSectionSize += EncodingBuffer.size();
817       EncodingBuffer.resize(0);
818       Address = Row.Address;
819       LastLine = Row.Line;
820       RowsSinceLastSequence++;
821     } else {
822       if (LineDelta) {
823         MS->EmitIntValue(dwarf::DW_LNS_advance_line, 1);
824         MS->EmitSLEB128IntValue(LineDelta);
825         LineSectionSize += 1 + getSLEB128Size(LineDelta);
826       }
827       if (AddressDelta) {
828         MS->EmitIntValue(dwarf::DW_LNS_advance_pc, 1);
829         MS->EmitULEB128IntValue(AddressDelta);
830         LineSectionSize += 1 + getULEB128Size(AddressDelta);
831       }
832       MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
833       MS->EmitBytes(EncodingOS.str());
834       LineSectionSize += EncodingBuffer.size();
835       EncodingBuffer.resize(0);
836
837       Address = -1ULL;
838       LastLine = FileNum = IsStatement = 1;
839       RowsSinceLastSequence = Column = Isa = 0;
840     }
841   }
842
843   if (RowsSinceLastSequence) {
844     MCDwarfLineAddr::Encode(*MC, INT64_MAX, 0, EncodingOS);
845     MS->EmitBytes(EncodingOS.str());
846     LineSectionSize += EncodingBuffer.size();
847     EncodingBuffer.resize(0);
848   }
849
850   MS->EmitLabel(LineEndSym);
851 }
852
853 /// \brief The core of the Dwarf linking logic.
854 ///
855 /// The link of the dwarf information from the object files will be
856 /// driven by the selection of 'root DIEs', which are DIEs that
857 /// describe variables or functions that are present in the linked
858 /// binary (and thus have entries in the debug map). All the debug
859 /// information that will be linked (the DIEs, but also the line
860 /// tables, ranges, ...) is derived from that set of root DIEs.
861 ///
862 /// The root DIEs are identified because they contain relocations that
863 /// correspond to a debug map entry at specific places (the low_pc for
864 /// a function, the location for a variable). These relocations are
865 /// called ValidRelocs in the DwarfLinker and are gathered as a very
866 /// first step when we start processing a DebugMapObject.
867 class DwarfLinker {
868 public:
869   DwarfLinker(StringRef OutputFilename, const LinkOptions &Options)
870       : OutputFilename(OutputFilename), Options(Options),
871         BinHolder(Options.Verbose) {}
872
873   ~DwarfLinker() {
874     for (auto *Abbrev : Abbreviations)
875       delete Abbrev;
876   }
877
878   /// \brief Link the contents of the DebugMap.
879   bool link(const DebugMap &);
880
881 private:
882   /// \brief Called at the start of a debug object link.
883   void startDebugObject(DWARFContext &, DebugMapObject &);
884
885   /// \brief Called at the end of a debug object link.
886   void endDebugObject();
887
888   /// \defgroup FindValidRelocations Translate debug map into a list
889   /// of relevant relocations
890   ///
891   /// @{
892   struct ValidReloc {
893     uint32_t Offset;
894     uint32_t Size;
895     uint64_t Addend;
896     const DebugMapObject::DebugMapEntry *Mapping;
897
898     ValidReloc(uint32_t Offset, uint32_t Size, uint64_t Addend,
899                const DebugMapObject::DebugMapEntry *Mapping)
900         : Offset(Offset), Size(Size), Addend(Addend), Mapping(Mapping) {}
901
902     bool operator<(const ValidReloc &RHS) const { return Offset < RHS.Offset; }
903   };
904
905   /// \brief The valid relocations for the current DebugMapObject.
906   /// This vector is sorted by relocation offset.
907   std::vector<ValidReloc> ValidRelocs;
908
909   /// \brief Index into ValidRelocs of the next relocation to
910   /// consider. As we walk the DIEs in acsending file offset and as
911   /// ValidRelocs is sorted by file offset, keeping this index
912   /// uptodate is all we have to do to have a cheap lookup during the
913   /// root DIE selection and during DIE cloning.
914   unsigned NextValidReloc;
915
916   bool findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
917                                   const DebugMapObject &DMO);
918
919   bool findValidRelocs(const object::SectionRef &Section,
920                        const object::ObjectFile &Obj,
921                        const DebugMapObject &DMO);
922
923   void findValidRelocsMachO(const object::SectionRef &Section,
924                             const object::MachOObjectFile &Obj,
925                             const DebugMapObject &DMO);
926   /// @}
927
928   /// \defgroup FindRootDIEs Find DIEs corresponding to debug map entries.
929   ///
930   /// @{
931   /// \brief Recursively walk the \p DIE tree and look for DIEs to
932   /// keep. Store that information in \p CU's DIEInfo.
933   void lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
934                          const DebugMapObject &DMO, CompileUnit &CU,
935                          unsigned Flags);
936
937   /// \brief Flags passed to DwarfLinker::lookForDIEsToKeep
938   enum TravesalFlags {
939     TF_Keep = 1 << 0,            ///< Mark the traversed DIEs as kept.
940     TF_InFunctionScope = 1 << 1, ///< Current scope is a fucntion scope.
941     TF_DependencyWalk = 1 << 2,  ///< Walking the dependencies of a kept DIE.
942     TF_ParentWalk = 1 << 3,      ///< Walking up the parents of a kept DIE.
943   };
944
945   /// \brief Mark the passed DIE as well as all the ones it depends on
946   /// as kept.
947   void keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
948                                CompileUnit::DIEInfo &MyInfo,
949                                const DebugMapObject &DMO, CompileUnit &CU,
950                                unsigned Flags);
951
952   unsigned shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
953                          CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo,
954                          unsigned Flags);
955
956   unsigned shouldKeepVariableDIE(const DWARFDebugInfoEntryMinimal &DIE,
957                                  CompileUnit &Unit,
958                                  CompileUnit::DIEInfo &MyInfo, unsigned Flags);
959
960   unsigned shouldKeepSubprogramDIE(const DWARFDebugInfoEntryMinimal &DIE,
961                                    CompileUnit &Unit,
962                                    CompileUnit::DIEInfo &MyInfo,
963                                    unsigned Flags);
964
965   bool hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
966                           CompileUnit::DIEInfo &Info);
967   /// @}
968
969   /// \defgroup Linking Methods used to link the debug information
970   ///
971   /// @{
972   /// \brief Recursively clone \p InputDIE into an tree of DIE objects
973   /// where useless (as decided by lookForDIEsToKeep()) bits have been
974   /// stripped out and addresses have been rewritten according to the
975   /// debug map.
976   ///
977   /// \param OutOffset is the offset the cloned DIE in the output
978   /// compile unit.
979   /// \param PCOffset (while cloning a function scope) is the offset
980   /// applied to the entry point of the function to get the linked address.
981   ///
982   /// \returns the root of the cloned tree.
983   DIE *cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &U,
984                 int64_t PCOffset, uint32_t OutOffset);
985
986   typedef DWARFAbbreviationDeclaration::AttributeSpec AttributeSpec;
987
988   /// \brief Information gathered and exchanged between the various
989   /// clone*Attributes helpers about the attributes of a particular DIE.
990   struct AttributesInfo {
991     uint64_t OrigHighPc; ///< Value of AT_high_pc in the input DIE
992     int64_t PCOffset;    ///< Offset to apply to PC addresses inside a function.
993
994     AttributesInfo() : OrigHighPc(0), PCOffset(0) {}
995   };
996
997   /// \brief Helper for cloneDIE.
998   unsigned cloneAttribute(DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE,
999                           CompileUnit &U, const DWARFFormValue &Val,
1000                           const AttributeSpec AttrSpec, unsigned AttrSize,
1001                           AttributesInfo &AttrInfo);
1002
1003   /// \brief Helper for cloneDIE.
1004   unsigned cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
1005                                 const DWARFFormValue &Val, const DWARFUnit &U);
1006
1007   /// \brief Helper for cloneDIE.
1008   unsigned
1009   cloneDieReferenceAttribute(DIE &Die,
1010                              const DWARFDebugInfoEntryMinimal &InputDIE,
1011                              AttributeSpec AttrSpec, unsigned AttrSize,
1012                              const DWARFFormValue &Val, CompileUnit &Unit);
1013
1014   /// \brief Helper for cloneDIE.
1015   unsigned cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
1016                                const DWARFFormValue &Val, unsigned AttrSize);
1017
1018   /// \brief Helper for cloneDIE.
1019   unsigned cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
1020                                  const DWARFFormValue &Val,
1021                                  const CompileUnit &Unit, AttributesInfo &Info);
1022
1023   /// \brief Helper for cloneDIE.
1024   unsigned cloneScalarAttribute(DIE &Die,
1025                                 const DWARFDebugInfoEntryMinimal &InputDIE,
1026                                 CompileUnit &U, AttributeSpec AttrSpec,
1027                                 const DWARFFormValue &Val, unsigned AttrSize,
1028                                 const AttributesInfo &Info);
1029
1030   /// \brief Helper for cloneDIE.
1031   bool applyValidRelocs(MutableArrayRef<char> Data, uint32_t BaseOffset,
1032                         bool isLittleEndian);
1033
1034   /// \brief Assign an abbreviation number to \p Abbrev
1035   void AssignAbbrev(DIEAbbrev &Abbrev);
1036
1037   /// \brief FoldingSet that uniques the abbreviations.
1038   FoldingSet<DIEAbbrev> AbbreviationsSet;
1039   /// \brief Storage for the unique Abbreviations.
1040   /// This is passed to AsmPrinter::emitDwarfAbbrevs(), thus it cannot
1041   /// be changed to a vecot of unique_ptrs.
1042   std::vector<DIEAbbrev *> Abbreviations;
1043
1044   /// \brief Compute and emit debug_ranges section for \p Unit, and
1045   /// patch the attributes referencing it.
1046   void patchRangesForUnit(const CompileUnit &Unit, DWARFContext &Dwarf) const;
1047
1048   /// \brief Generate and emit the DW_AT_ranges attribute for a
1049   /// compile_unit if it had one.
1050   void generateUnitRanges(CompileUnit &Unit) const;
1051
1052   /// \brief Extract the line tables fromt he original dwarf, extract
1053   /// the relevant parts according to the linked function ranges and
1054   /// emit the result in the debug_line section.
1055   void patchLineTableForUnit(CompileUnit &Unit, DWARFContext &OrigDwarf);
1056
1057   /// \brief DIELoc objects that need to be destructed (but not freed!).
1058   std::vector<DIELoc *> DIELocs;
1059   /// \brief DIEBlock objects that need to be destructed (but not freed!).
1060   std::vector<DIEBlock *> DIEBlocks;
1061   /// \brief Allocator used for all the DIEValue objects.
1062   BumpPtrAllocator DIEAlloc;
1063   /// @}
1064
1065   /// \defgroup Helpers Various helper methods.
1066   ///
1067   /// @{
1068   const DWARFDebugInfoEntryMinimal *
1069   resolveDIEReference(DWARFFormValue &RefValue, const DWARFUnit &Unit,
1070                       const DWARFDebugInfoEntryMinimal &DIE,
1071                       CompileUnit *&ReferencedCU);
1072
1073   CompileUnit *getUnitForOffset(unsigned Offset);
1074
1075   void reportWarning(const Twine &Warning, const DWARFUnit *Unit = nullptr,
1076                      const DWARFDebugInfoEntryMinimal *DIE = nullptr) const;
1077
1078   bool createStreamer(Triple TheTriple, StringRef OutputFilename);
1079   /// @}
1080
1081 private:
1082   std::string OutputFilename;
1083   LinkOptions Options;
1084   BinaryHolder BinHolder;
1085   std::unique_ptr<DwarfStreamer> Streamer;
1086
1087   /// The units of the current debug map object.
1088   std::vector<CompileUnit> Units;
1089
1090   /// The debug map object curently under consideration.
1091   DebugMapObject *CurrentDebugObject;
1092
1093   /// \brief The Dwarf string pool
1094   NonRelocatableStringpool StringPool;
1095
1096   /// \brief This map is keyed by the entry PC of functions in that
1097   /// debug object and the associated value is a pair storing the
1098   /// corresponding end PC and the offset to apply to get the linked
1099   /// address.
1100   ///
1101   /// See startDebugObject() for a more complete description of its use.
1102   std::map<uint64_t, std::pair<uint64_t, int64_t>> Ranges;
1103 };
1104
1105 /// \brief Similar to DWARFUnitSection::getUnitForOffset(), but
1106 /// returning our CompileUnit object instead.
1107 CompileUnit *DwarfLinker::getUnitForOffset(unsigned Offset) {
1108   auto CU =
1109       std::upper_bound(Units.begin(), Units.end(), Offset,
1110                        [](uint32_t LHS, const CompileUnit &RHS) {
1111                          return LHS < RHS.getOrigUnit().getNextUnitOffset();
1112                        });
1113   return CU != Units.end() ? &*CU : nullptr;
1114 }
1115
1116 /// \brief Resolve the DIE attribute reference that has been
1117 /// extracted in \p RefValue. The resulting DIE migh be in another
1118 /// CompileUnit which is stored into \p ReferencedCU.
1119 /// \returns null if resolving fails for any reason.
1120 const DWARFDebugInfoEntryMinimal *DwarfLinker::resolveDIEReference(
1121     DWARFFormValue &RefValue, const DWARFUnit &Unit,
1122     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit *&RefCU) {
1123   assert(RefValue.isFormClass(DWARFFormValue::FC_Reference));
1124   uint64_t RefOffset = *RefValue.getAsReference(&Unit);
1125
1126   if ((RefCU = getUnitForOffset(RefOffset)))
1127     if (const auto *RefDie = RefCU->getOrigUnit().getDIEForOffset(RefOffset))
1128       return RefDie;
1129
1130   reportWarning("could not find referenced DIE", &Unit, &DIE);
1131   return nullptr;
1132 }
1133
1134 /// \brief Report a warning to the user, optionaly including
1135 /// information about a specific \p DIE related to the warning.
1136 void DwarfLinker::reportWarning(const Twine &Warning, const DWARFUnit *Unit,
1137                                 const DWARFDebugInfoEntryMinimal *DIE) const {
1138   StringRef Context = "<debug map>";
1139   if (CurrentDebugObject)
1140     Context = CurrentDebugObject->getObjectFilename();
1141   warn(Warning, Context);
1142
1143   if (!Options.Verbose || !DIE)
1144     return;
1145
1146   errs() << "    in DIE:\n";
1147   DIE->dump(errs(), const_cast<DWARFUnit *>(Unit), 0 /* RecurseDepth */,
1148             6 /* Indent */);
1149 }
1150
1151 bool DwarfLinker::createStreamer(Triple TheTriple, StringRef OutputFilename) {
1152   if (Options.NoOutput)
1153     return true;
1154
1155   Streamer = llvm::make_unique<DwarfStreamer>();
1156   return Streamer->init(TheTriple, OutputFilename);
1157 }
1158
1159 /// \brief Recursive helper to gather the child->parent relationships in the
1160 /// original compile unit.
1161 static void gatherDIEParents(const DWARFDebugInfoEntryMinimal *DIE,
1162                              unsigned ParentIdx, CompileUnit &CU) {
1163   unsigned MyIdx = CU.getOrigUnit().getDIEIndex(DIE);
1164   CU.getInfo(MyIdx).ParentIdx = ParentIdx;
1165
1166   if (DIE->hasChildren())
1167     for (auto *Child = DIE->getFirstChild(); Child && !Child->isNULL();
1168          Child = Child->getSibling())
1169       gatherDIEParents(Child, MyIdx, CU);
1170 }
1171
1172 static bool dieNeedsChildrenToBeMeaningful(uint32_t Tag) {
1173   switch (Tag) {
1174   default:
1175     return false;
1176   case dwarf::DW_TAG_subprogram:
1177   case dwarf::DW_TAG_lexical_block:
1178   case dwarf::DW_TAG_subroutine_type:
1179   case dwarf::DW_TAG_structure_type:
1180   case dwarf::DW_TAG_class_type:
1181   case dwarf::DW_TAG_union_type:
1182     return true;
1183   }
1184   llvm_unreachable("Invalid Tag");
1185 }
1186
1187 void DwarfLinker::startDebugObject(DWARFContext &Dwarf, DebugMapObject &Obj) {
1188   Units.reserve(Dwarf.getNumCompileUnits());
1189   NextValidReloc = 0;
1190   // Iterate over the debug map entries and put all the ones that are
1191   // functions (because they have a size) into the Ranges map. This
1192   // map is very similar to the FunctionRanges that are stored in each
1193   // unit, with 2 notable differences:
1194   //  - obviously this one is global, while the other ones are per-unit.
1195   //  - this one contains not only the functions described in the DIE
1196   // tree, but also the ones that are only in the debug map.
1197   // The latter information is required to reproduce dsymutil's logic
1198   // while linking line tables. The cases where this information
1199   // matters look like bugs that need to be investigated, but for now
1200   // we need to reproduce dsymutil's behavior.
1201   // FIXME: Once we understood exactly if that information is needed,
1202   // maybe totally remove this (or try to use it to do a real
1203   // -gline-tables-only on Darwin.
1204   for (const auto &Entry : Obj.symbols()) {
1205     const auto &Mapping = Entry.getValue();
1206     if (Mapping.Size)
1207       Ranges[Mapping.ObjectAddress] = std::make_pair(
1208           Mapping.ObjectAddress + Mapping.Size,
1209           int64_t(Mapping.BinaryAddress) - Mapping.ObjectAddress);
1210   }
1211 }
1212
1213 void DwarfLinker::endDebugObject() {
1214   Units.clear();
1215   ValidRelocs.clear();
1216   Ranges.clear();
1217
1218   for (auto *Block : DIEBlocks)
1219     Block->~DIEBlock();
1220   for (auto *Loc : DIELocs)
1221     Loc->~DIELoc();
1222
1223   DIEBlocks.clear();
1224   DIELocs.clear();
1225   DIEAlloc.Reset();
1226 }
1227
1228 /// \brief Iterate over the relocations of the given \p Section and
1229 /// store the ones that correspond to debug map entries into the
1230 /// ValidRelocs array.
1231 void DwarfLinker::findValidRelocsMachO(const object::SectionRef &Section,
1232                                        const object::MachOObjectFile &Obj,
1233                                        const DebugMapObject &DMO) {
1234   StringRef Contents;
1235   Section.getContents(Contents);
1236   DataExtractor Data(Contents, Obj.isLittleEndian(), 0);
1237
1238   for (const object::RelocationRef &Reloc : Section.relocations()) {
1239     object::DataRefImpl RelocDataRef = Reloc.getRawDataRefImpl();
1240     MachO::any_relocation_info MachOReloc = Obj.getRelocation(RelocDataRef);
1241     unsigned RelocSize = 1 << Obj.getAnyRelocationLength(MachOReloc);
1242     uint64_t Offset64;
1243     if ((RelocSize != 4 && RelocSize != 8) || Reloc.getOffset(Offset64)) {
1244       reportWarning(" unsupported relocation in debug_info section.");
1245       continue;
1246     }
1247     uint32_t Offset = Offset64;
1248     // Mach-o uses REL relocations, the addend is at the relocation offset.
1249     uint64_t Addend = Data.getUnsigned(&Offset, RelocSize);
1250
1251     auto Sym = Reloc.getSymbol();
1252     if (Sym != Obj.symbol_end()) {
1253       StringRef SymbolName;
1254       if (Sym->getName(SymbolName)) {
1255         reportWarning("error getting relocation symbol name.");
1256         continue;
1257       }
1258       if (const auto *Mapping = DMO.lookupSymbol(SymbolName))
1259         ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
1260     } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
1261       // Do not store the addend. The addend was the address of the
1262       // symbol in the object file, the address in the binary that is
1263       // stored in the debug map doesn't need to be offseted.
1264       ValidRelocs.emplace_back(Offset64, RelocSize, 0, Mapping);
1265     }
1266   }
1267 }
1268
1269 /// \brief Dispatch the valid relocation finding logic to the
1270 /// appropriate handler depending on the object file format.
1271 bool DwarfLinker::findValidRelocs(const object::SectionRef &Section,
1272                                   const object::ObjectFile &Obj,
1273                                   const DebugMapObject &DMO) {
1274   // Dispatch to the right handler depending on the file type.
1275   if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj))
1276     findValidRelocsMachO(Section, *MachOObj, DMO);
1277   else
1278     reportWarning(Twine("unsupported object file type: ") + Obj.getFileName());
1279
1280   if (ValidRelocs.empty())
1281     return false;
1282
1283   // Sort the relocations by offset. We will walk the DIEs linearly in
1284   // the file, this allows us to just keep an index in the relocation
1285   // array that we advance during our walk, rather than resorting to
1286   // some associative container. See DwarfLinker::NextValidReloc.
1287   std::sort(ValidRelocs.begin(), ValidRelocs.end());
1288   return true;
1289 }
1290
1291 /// \brief Look for relocations in the debug_info section that match
1292 /// entries in the debug map. These relocations will drive the Dwarf
1293 /// link by indicating which DIEs refer to symbols present in the
1294 /// linked binary.
1295 /// \returns wether there are any valid relocations in the debug info.
1296 bool DwarfLinker::findValidRelocsInDebugInfo(const object::ObjectFile &Obj,
1297                                              const DebugMapObject &DMO) {
1298   // Find the debug_info section.
1299   for (const object::SectionRef &Section : Obj.sections()) {
1300     StringRef SectionName;
1301     Section.getName(SectionName);
1302     SectionName = SectionName.substr(SectionName.find_first_not_of("._"));
1303     if (SectionName != "debug_info")
1304       continue;
1305     return findValidRelocs(Section, Obj, DMO);
1306   }
1307   return false;
1308 }
1309
1310 /// \brief Checks that there is a relocation against an actual debug
1311 /// map entry between \p StartOffset and \p NextOffset.
1312 ///
1313 /// This function must be called with offsets in strictly ascending
1314 /// order because it never looks back at relocations it already 'went past'.
1315 /// \returns true and sets Info.InDebugMap if it is the case.
1316 bool DwarfLinker::hasValidRelocation(uint32_t StartOffset, uint32_t EndOffset,
1317                                      CompileUnit::DIEInfo &Info) {
1318   assert(NextValidReloc == 0 ||
1319          StartOffset > ValidRelocs[NextValidReloc - 1].Offset);
1320   if (NextValidReloc >= ValidRelocs.size())
1321     return false;
1322
1323   uint64_t RelocOffset = ValidRelocs[NextValidReloc].Offset;
1324
1325   // We might need to skip some relocs that we didn't consider. For
1326   // example the high_pc of a discarded DIE might contain a reloc that
1327   // is in the list because it actually corresponds to the start of a
1328   // function that is in the debug map.
1329   while (RelocOffset < StartOffset && NextValidReloc < ValidRelocs.size() - 1)
1330     RelocOffset = ValidRelocs[++NextValidReloc].Offset;
1331
1332   if (RelocOffset < StartOffset || RelocOffset >= EndOffset)
1333     return false;
1334
1335   const auto &ValidReloc = ValidRelocs[NextValidReloc++];
1336   if (Options.Verbose)
1337     outs() << "Found valid debug map entry: " << ValidReloc.Mapping->getKey()
1338            << " " << format("\t%016" PRIx64 " => %016" PRIx64,
1339                             ValidReloc.Mapping->getValue().ObjectAddress,
1340                             ValidReloc.Mapping->getValue().BinaryAddress);
1341
1342   Info.AddrAdjust = int64_t(ValidReloc.Mapping->getValue().BinaryAddress) +
1343                     ValidReloc.Addend -
1344                     ValidReloc.Mapping->getValue().ObjectAddress;
1345   Info.InDebugMap = true;
1346   return true;
1347 }
1348
1349 /// \brief Get the starting and ending (exclusive) offset for the
1350 /// attribute with index \p Idx descibed by \p Abbrev. \p Offset is
1351 /// supposed to point to the position of the first attribute described
1352 /// by \p Abbrev.
1353 /// \return [StartOffset, EndOffset) as a pair.
1354 static std::pair<uint32_t, uint32_t>
1355 getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx,
1356                     unsigned Offset, const DWARFUnit &Unit) {
1357   DataExtractor Data = Unit.getDebugInfoExtractor();
1358
1359   for (unsigned i = 0; i < Idx; ++i)
1360     DWARFFormValue::skipValue(Abbrev->getFormByIndex(i), Data, &Offset, &Unit);
1361
1362   uint32_t End = Offset;
1363   DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, &Unit);
1364
1365   return std::make_pair(Offset, End);
1366 }
1367
1368 /// \brief Check if a variable describing DIE should be kept.
1369 /// \returns updated TraversalFlags.
1370 unsigned DwarfLinker::shouldKeepVariableDIE(
1371     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
1372     CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
1373   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
1374
1375   // Global variables with constant value can always be kept.
1376   if (!(Flags & TF_InFunctionScope) &&
1377       Abbrev->findAttributeIndex(dwarf::DW_AT_const_value) != -1U) {
1378     MyInfo.InDebugMap = true;
1379     return Flags | TF_Keep;
1380   }
1381
1382   uint32_t LocationIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_location);
1383   if (LocationIdx == -1U)
1384     return Flags;
1385
1386   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
1387   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
1388   uint32_t LocationOffset, LocationEndOffset;
1389   std::tie(LocationOffset, LocationEndOffset) =
1390       getAttributeOffsets(Abbrev, LocationIdx, Offset, OrigUnit);
1391
1392   // See if there is a relocation to a valid debug map entry inside
1393   // this variable's location. The order is important here. We want to
1394   // always check in the variable has a valid relocation, so that the
1395   // DIEInfo is filled. However, we don't want a static variable in a
1396   // function to force us to keep the enclosing function.
1397   if (!hasValidRelocation(LocationOffset, LocationEndOffset, MyInfo) ||
1398       (Flags & TF_InFunctionScope))
1399     return Flags;
1400
1401   if (Options.Verbose)
1402     DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
1403
1404   return Flags | TF_Keep;
1405 }
1406
1407 /// \brief Check if a function describing DIE should be kept.
1408 /// \returns updated TraversalFlags.
1409 unsigned DwarfLinker::shouldKeepSubprogramDIE(
1410     const DWARFDebugInfoEntryMinimal &DIE, CompileUnit &Unit,
1411     CompileUnit::DIEInfo &MyInfo, unsigned Flags) {
1412   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
1413
1414   Flags |= TF_InFunctionScope;
1415
1416   uint32_t LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc);
1417   if (LowPcIdx == -1U)
1418     return Flags;
1419
1420   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
1421   const DWARFUnit &OrigUnit = Unit.getOrigUnit();
1422   uint32_t LowPcOffset, LowPcEndOffset;
1423   std::tie(LowPcOffset, LowPcEndOffset) =
1424       getAttributeOffsets(Abbrev, LowPcIdx, Offset, OrigUnit);
1425
1426   uint64_t LowPc =
1427       DIE.getAttributeValueAsAddress(&OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
1428   assert(LowPc != -1ULL && "low_pc attribute is not an address.");
1429   if (LowPc == -1ULL ||
1430       !hasValidRelocation(LowPcOffset, LowPcEndOffset, MyInfo))
1431     return Flags;
1432
1433   if (Options.Verbose)
1434     DIE.dump(outs(), const_cast<DWARFUnit *>(&OrigUnit), 0, 8 /* Indent */);
1435
1436   Flags |= TF_Keep;
1437
1438   DWARFFormValue HighPcValue;
1439   if (!DIE.getAttributeValue(&OrigUnit, dwarf::DW_AT_high_pc, HighPcValue)) {
1440     reportWarning("Function without high_pc. Range will be discarded.\n",
1441                   &OrigUnit, &DIE);
1442     return Flags;
1443   }
1444
1445   uint64_t HighPc;
1446   if (HighPcValue.isFormClass(DWARFFormValue::FC_Address)) {
1447     HighPc = *HighPcValue.getAsAddress(&OrigUnit);
1448   } else {
1449     assert(HighPcValue.isFormClass(DWARFFormValue::FC_Constant));
1450     HighPc = LowPc + *HighPcValue.getAsUnsignedConstant();
1451   }
1452
1453   // Replace the debug map range with a more accurate one.
1454   Ranges[LowPc] = std::make_pair(HighPc, MyInfo.AddrAdjust);
1455   Unit.addFunctionRange(LowPc, HighPc, MyInfo.AddrAdjust);
1456   return Flags;
1457 }
1458
1459 /// \brief Check if a DIE should be kept.
1460 /// \returns updated TraversalFlags.
1461 unsigned DwarfLinker::shouldKeepDIE(const DWARFDebugInfoEntryMinimal &DIE,
1462                                     CompileUnit &Unit,
1463                                     CompileUnit::DIEInfo &MyInfo,
1464                                     unsigned Flags) {
1465   switch (DIE.getTag()) {
1466   case dwarf::DW_TAG_constant:
1467   case dwarf::DW_TAG_variable:
1468     return shouldKeepVariableDIE(DIE, Unit, MyInfo, Flags);
1469   case dwarf::DW_TAG_subprogram:
1470     return shouldKeepSubprogramDIE(DIE, Unit, MyInfo, Flags);
1471   case dwarf::DW_TAG_module:
1472   case dwarf::DW_TAG_imported_module:
1473   case dwarf::DW_TAG_imported_declaration:
1474   case dwarf::DW_TAG_imported_unit:
1475     // We always want to keep these.
1476     return Flags | TF_Keep;
1477   }
1478
1479   return Flags;
1480 }
1481
1482 /// \brief Mark the passed DIE as well as all the ones it depends on
1483 /// as kept.
1484 ///
1485 /// This function is called by lookForDIEsToKeep on DIEs that are
1486 /// newly discovered to be needed in the link. It recursively calls
1487 /// back to lookForDIEsToKeep while adding TF_DependencyWalk to the
1488 /// TraversalFlags to inform it that it's not doing the primary DIE
1489 /// tree walk.
1490 void DwarfLinker::keepDIEAndDenpendencies(const DWARFDebugInfoEntryMinimal &DIE,
1491                                           CompileUnit::DIEInfo &MyInfo,
1492                                           const DebugMapObject &DMO,
1493                                           CompileUnit &CU, unsigned Flags) {
1494   const DWARFUnit &Unit = CU.getOrigUnit();
1495   MyInfo.Keep = true;
1496
1497   // First mark all the parent chain as kept.
1498   unsigned AncestorIdx = MyInfo.ParentIdx;
1499   while (!CU.getInfo(AncestorIdx).Keep) {
1500     lookForDIEsToKeep(*Unit.getDIEAtIndex(AncestorIdx), DMO, CU,
1501                       TF_ParentWalk | TF_Keep | TF_DependencyWalk);
1502     AncestorIdx = CU.getInfo(AncestorIdx).ParentIdx;
1503   }
1504
1505   // Then we need to mark all the DIEs referenced by this DIE's
1506   // attributes as kept.
1507   DataExtractor Data = Unit.getDebugInfoExtractor();
1508   const auto *Abbrev = DIE.getAbbreviationDeclarationPtr();
1509   uint32_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode());
1510
1511   // Mark all DIEs referenced through atttributes as kept.
1512   for (const auto &AttrSpec : Abbrev->attributes()) {
1513     DWARFFormValue Val(AttrSpec.Form);
1514
1515     if (!Val.isFormClass(DWARFFormValue::FC_Reference)) {
1516       DWARFFormValue::skipValue(AttrSpec.Form, Data, &Offset, &Unit);
1517       continue;
1518     }
1519
1520     Val.extractValue(Data, &Offset, &Unit);
1521     CompileUnit *ReferencedCU;
1522     if (const auto *RefDIE = resolveDIEReference(Val, Unit, DIE, ReferencedCU))
1523       lookForDIEsToKeep(*RefDIE, DMO, *ReferencedCU,
1524                         TF_Keep | TF_DependencyWalk);
1525   }
1526 }
1527
1528 /// \brief Recursively walk the \p DIE tree and look for DIEs to
1529 /// keep. Store that information in \p CU's DIEInfo.
1530 ///
1531 /// This function is the entry point of the DIE selection
1532 /// algorithm. It is expected to walk the DIE tree in file order and
1533 /// (though the mediation of its helper) call hasValidRelocation() on
1534 /// each DIE that might be a 'root DIE' (See DwarfLinker class
1535 /// comment).
1536 /// While walking the dependencies of root DIEs, this function is
1537 /// also called, but during these dependency walks the file order is
1538 /// not respected. The TF_DependencyWalk flag tells us which kind of
1539 /// traversal we are currently doing.
1540 void DwarfLinker::lookForDIEsToKeep(const DWARFDebugInfoEntryMinimal &DIE,
1541                                     const DebugMapObject &DMO, CompileUnit &CU,
1542                                     unsigned Flags) {
1543   unsigned Idx = CU.getOrigUnit().getDIEIndex(&DIE);
1544   CompileUnit::DIEInfo &MyInfo = CU.getInfo(Idx);
1545   bool AlreadyKept = MyInfo.Keep;
1546
1547   // If the Keep flag is set, we are marking a required DIE's
1548   // dependencies. If our target is already marked as kept, we're all
1549   // set.
1550   if ((Flags & TF_DependencyWalk) && AlreadyKept)
1551     return;
1552
1553   // We must not call shouldKeepDIE while called from keepDIEAndDenpendencies,
1554   // because it would screw up the relocation finding logic.
1555   if (!(Flags & TF_DependencyWalk))
1556     Flags = shouldKeepDIE(DIE, CU, MyInfo, Flags);
1557
1558   // If it is a newly kept DIE mark it as well as all its dependencies as kept.
1559   if (!AlreadyKept && (Flags & TF_Keep))
1560     keepDIEAndDenpendencies(DIE, MyInfo, DMO, CU, Flags);
1561
1562   // The TF_ParentWalk flag tells us that we are currently walking up
1563   // the parent chain of a required DIE, and we don't want to mark all
1564   // the children of the parents as kept (consider for example a
1565   // DW_TAG_namespace node in the parent chain). There are however a
1566   // set of DIE types for which we want to ignore that directive and still
1567   // walk their children.
1568   if (dieNeedsChildrenToBeMeaningful(DIE.getTag()))
1569     Flags &= ~TF_ParentWalk;
1570
1571   if (!DIE.hasChildren() || (Flags & TF_ParentWalk))
1572     return;
1573
1574   for (auto *Child = DIE.getFirstChild(); Child && !Child->isNULL();
1575        Child = Child->getSibling())
1576     lookForDIEsToKeep(*Child, DMO, CU, Flags);
1577 }
1578
1579 /// \brief Assign an abbreviation numer to \p Abbrev.
1580 ///
1581 /// Our DIEs get freed after every DebugMapObject has been processed,
1582 /// thus the FoldingSet we use to unique DIEAbbrevs cannot refer to
1583 /// the instances hold by the DIEs. When we encounter an abbreviation
1584 /// that we don't know, we create a permanent copy of it.
1585 void DwarfLinker::AssignAbbrev(DIEAbbrev &Abbrev) {
1586   // Check the set for priors.
1587   FoldingSetNodeID ID;
1588   Abbrev.Profile(ID);
1589   void *InsertToken;
1590   DIEAbbrev *InSet = AbbreviationsSet.FindNodeOrInsertPos(ID, InsertToken);
1591
1592   // If it's newly added.
1593   if (InSet) {
1594     // Assign existing abbreviation number.
1595     Abbrev.setNumber(InSet->getNumber());
1596   } else {
1597     // Add to abbreviation list.
1598     Abbreviations.push_back(
1599         new DIEAbbrev(Abbrev.getTag(), Abbrev.hasChildren()));
1600     for (const auto &Attr : Abbrev.getData())
1601       Abbreviations.back()->AddAttribute(Attr.getAttribute(), Attr.getForm());
1602     AbbreviationsSet.InsertNode(Abbreviations.back(), InsertToken);
1603     // Assign the unique abbreviation number.
1604     Abbrev.setNumber(Abbreviations.size());
1605     Abbreviations.back()->setNumber(Abbreviations.size());
1606   }
1607 }
1608
1609 /// \brief Clone a string attribute described by \p AttrSpec and add
1610 /// it to \p Die.
1611 /// \returns the size of the new attribute.
1612 unsigned DwarfLinker::cloneStringAttribute(DIE &Die, AttributeSpec AttrSpec,
1613                                            const DWARFFormValue &Val,
1614                                            const DWARFUnit &U) {
1615   // Switch everything to out of line strings.
1616   const char *String = *Val.getAsCString(&U);
1617   unsigned Offset = StringPool.getStringOffset(String);
1618   Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_strp,
1619                new (DIEAlloc) DIEInteger(Offset));
1620   return 4;
1621 }
1622
1623 /// \brief Clone an attribute referencing another DIE and add
1624 /// it to \p Die.
1625 /// \returns the size of the new attribute.
1626 unsigned DwarfLinker::cloneDieReferenceAttribute(
1627     DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE,
1628     AttributeSpec AttrSpec, unsigned AttrSize, const DWARFFormValue &Val,
1629     CompileUnit &Unit) {
1630   uint32_t Ref = *Val.getAsReference(&Unit.getOrigUnit());
1631   DIE *NewRefDie = nullptr;
1632   CompileUnit *RefUnit = nullptr;
1633   const DWARFDebugInfoEntryMinimal *RefDie = nullptr;
1634
1635   if (!(RefUnit = getUnitForOffset(Ref)) ||
1636       !(RefDie = RefUnit->getOrigUnit().getDIEForOffset(Ref))) {
1637     const char *AttributeString = dwarf::AttributeString(AttrSpec.Attr);
1638     if (!AttributeString)
1639       AttributeString = "DW_AT_???";
1640     reportWarning(Twine("Missing DIE for ref in attribute ") + AttributeString +
1641                       ". Dropping.",
1642                   &Unit.getOrigUnit(), &InputDIE);
1643     return 0;
1644   }
1645
1646   unsigned Idx = RefUnit->getOrigUnit().getDIEIndex(RefDie);
1647   CompileUnit::DIEInfo &RefInfo = RefUnit->getInfo(Idx);
1648   if (!RefInfo.Clone) {
1649     assert(Ref > InputDIE.getOffset());
1650     // We haven't cloned this DIE yet. Just create an empty one and
1651     // store it. It'll get really cloned when we process it.
1652     RefInfo.Clone = new DIE(dwarf::Tag(RefDie->getTag()));
1653   }
1654   NewRefDie = RefInfo.Clone;
1655
1656   if (AttrSpec.Form == dwarf::DW_FORM_ref_addr) {
1657     // We cannot currently rely on a DIEEntry to emit ref_addr
1658     // references, because the implementation calls back to DwarfDebug
1659     // to find the unit offset. (We don't have a DwarfDebug)
1660     // FIXME: we should be able to design DIEEntry reliance on
1661     // DwarfDebug away.
1662     DIEInteger *Attr;
1663     if (Ref < InputDIE.getOffset()) {
1664       // We must have already cloned that DIE.
1665       uint32_t NewRefOffset =
1666           RefUnit->getStartOffset() + NewRefDie->getOffset();
1667       Attr = new (DIEAlloc) DIEInteger(NewRefOffset);
1668     } else {
1669       // A forward reference. Note and fixup later.
1670       Attr = new (DIEAlloc) DIEInteger(0xBADDEF);
1671       Unit.noteForwardReference(NewRefDie, RefUnit, Attr);
1672     }
1673     Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_ref_addr,
1674                  Attr);
1675     return AttrSize;
1676   }
1677
1678   Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1679                new (DIEAlloc) DIEEntry(*NewRefDie));
1680   return AttrSize;
1681 }
1682
1683 /// \brief Clone an attribute of block form (locations, constants) and add
1684 /// it to \p Die.
1685 /// \returns the size of the new attribute.
1686 unsigned DwarfLinker::cloneBlockAttribute(DIE &Die, AttributeSpec AttrSpec,
1687                                           const DWARFFormValue &Val,
1688                                           unsigned AttrSize) {
1689   DIE *Attr;
1690   DIEValue *Value;
1691   DIELoc *Loc = nullptr;
1692   DIEBlock *Block = nullptr;
1693   // Just copy the block data over.
1694   if (AttrSpec.Form == dwarf::DW_FORM_exprloc) {
1695     Loc = new (DIEAlloc) DIELoc();
1696     DIELocs.push_back(Loc);
1697   } else {
1698     Block = new (DIEAlloc) DIEBlock();
1699     DIEBlocks.push_back(Block);
1700   }
1701   Attr = Loc ? static_cast<DIE *>(Loc) : static_cast<DIE *>(Block);
1702   Value = Loc ? static_cast<DIEValue *>(Loc) : static_cast<DIEValue *>(Block);
1703   ArrayRef<uint8_t> Bytes = *Val.getAsBlock();
1704   for (auto Byte : Bytes)
1705     Attr->addValue(static_cast<dwarf::Attribute>(0), dwarf::DW_FORM_data1,
1706                    new (DIEAlloc) DIEInteger(Byte));
1707   // FIXME: If DIEBlock and DIELoc just reuses the Size field of
1708   // the DIE class, this if could be replaced by
1709   // Attr->setSize(Bytes.size()).
1710   if (Streamer) {
1711     if (Loc)
1712       Loc->ComputeSize(&Streamer->getAsmPrinter());
1713     else
1714       Block->ComputeSize(&Streamer->getAsmPrinter());
1715   }
1716   Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1717                Value);
1718   return AttrSize;
1719 }
1720
1721 /// \brief Clone an address attribute and add it to \p Die.
1722 /// \returns the size of the new attribute.
1723 unsigned DwarfLinker::cloneAddressAttribute(DIE &Die, AttributeSpec AttrSpec,
1724                                             const DWARFFormValue &Val,
1725                                             const CompileUnit &Unit,
1726                                             AttributesInfo &Info) {
1727   uint64_t Addr = *Val.getAsAddress(&Unit.getOrigUnit());
1728   if (AttrSpec.Attr == dwarf::DW_AT_low_pc) {
1729     if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine ||
1730         Die.getTag() == dwarf::DW_TAG_lexical_block)
1731       Addr += Info.PCOffset;
1732     else if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1733       Addr = Unit.getLowPc();
1734       if (Addr == UINT64_MAX)
1735         return 0;
1736     }
1737   } else if (AttrSpec.Attr == dwarf::DW_AT_high_pc) {
1738     if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
1739       if (uint64_t HighPc = Unit.getHighPc())
1740         Addr = HighPc;
1741       else
1742         return 0;
1743     } else
1744       // If we have a high_pc recorded for the input DIE, use
1745       // it. Otherwise (when no relocations where applied) just use the
1746       // one we just decoded.
1747       Addr = (Info.OrigHighPc ? Info.OrigHighPc : Addr) + Info.PCOffset;
1748   }
1749
1750   Die.addValue(static_cast<dwarf::Attribute>(AttrSpec.Attr),
1751                static_cast<dwarf::Form>(AttrSpec.Form),
1752                new (DIEAlloc) DIEInteger(Addr));
1753   return Unit.getOrigUnit().getAddressByteSize();
1754 }
1755
1756 /// \brief Clone a scalar attribute  and add it to \p Die.
1757 /// \returns the size of the new attribute.
1758 unsigned DwarfLinker::cloneScalarAttribute(
1759     DIE &Die, const DWARFDebugInfoEntryMinimal &InputDIE, CompileUnit &Unit,
1760     AttributeSpec AttrSpec, const DWARFFormValue &Val, unsigned AttrSize,
1761     const AttributesInfo &Info) {
1762   uint64_t Value;
1763   if (AttrSpec.Attr == dwarf::DW_AT_high_pc &&
1764       Die.getTag() == dwarf::DW_TAG_compile_unit) {
1765     if (Unit.getLowPc() == -1ULL)
1766       return 0;
1767     // Dwarf >= 4 high_pc is an size, not an address.
1768     Value = Unit.getHighPc() - Unit.getLowPc();
1769   } else if (AttrSpec.Form == dwarf::DW_FORM_sec_offset)
1770     Value = *Val.getAsSectionOffset();
1771   else if (AttrSpec.Form == dwarf::DW_FORM_sdata)
1772     Value = *Val.getAsSignedConstant();
1773   else if (auto OptionalValue = Val.getAsUnsignedConstant())
1774     Value = *OptionalValue;
1775   else {
1776     reportWarning("Unsupported scalar attribute form. Dropping attribute.",
1777                   &Unit.getOrigUnit(), &InputDIE);
1778     return 0;
1779   }
1780   DIEInteger *Attr = new (DIEAlloc) DIEInteger(Value);
1781   if (AttrSpec.Attr == dwarf::DW_AT_ranges)
1782     Unit.noteRangeAttribute(Die, Attr);
1783   // A more generic way to check for location attributes would be
1784   // nice, but it's very unlikely that any other attribute needs a
1785   // location list.
1786   else if (AttrSpec.Attr == dwarf::DW_AT_location ||
1787            AttrSpec.Attr == dwarf::DW_AT_frame_base)
1788     Unit.noteLocationAttribute(Attr, Info.PCOffset);
1789
1790   Die.addValue(dwarf::Attribute(AttrSpec.Attr), dwarf::Form(AttrSpec.Form),
1791                Attr);
1792   return AttrSize;
1793 }
1794
1795 /// \brief Clone \p InputDIE's attribute described by \p AttrSpec with
1796 /// value \p Val, and add it to \p Die.
1797 /// \returns the size of the cloned attribute.
1798 unsigned DwarfLinker::cloneAttribute(DIE &Die,
1799                                      const DWARFDebugInfoEntryMinimal &InputDIE,
1800                                      CompileUnit &Unit,
1801                                      const DWARFFormValue &Val,
1802                                      const AttributeSpec AttrSpec,
1803                                      unsigned AttrSize, AttributesInfo &Info) {
1804   const DWARFUnit &U = Unit.getOrigUnit();
1805
1806   switch (AttrSpec.Form) {
1807   case dwarf::DW_FORM_strp:
1808   case dwarf::DW_FORM_string:
1809     return cloneStringAttribute(Die, AttrSpec, Val, U);
1810   case dwarf::DW_FORM_ref_addr:
1811   case dwarf::DW_FORM_ref1:
1812   case dwarf::DW_FORM_ref2:
1813   case dwarf::DW_FORM_ref4:
1814   case dwarf::DW_FORM_ref8:
1815     return cloneDieReferenceAttribute(Die, InputDIE, AttrSpec, AttrSize, Val,
1816                                       Unit);
1817   case dwarf::DW_FORM_block:
1818   case dwarf::DW_FORM_block1:
1819   case dwarf::DW_FORM_block2:
1820   case dwarf::DW_FORM_block4:
1821   case dwarf::DW_FORM_exprloc:
1822     return cloneBlockAttribute(Die, AttrSpec, Val, AttrSize);
1823   case dwarf::DW_FORM_addr:
1824     return cloneAddressAttribute(Die, AttrSpec, Val, Unit, Info);
1825   case dwarf::DW_FORM_data1:
1826   case dwarf::DW_FORM_data2:
1827   case dwarf::DW_FORM_data4:
1828   case dwarf::DW_FORM_data8:
1829   case dwarf::DW_FORM_udata:
1830   case dwarf::DW_FORM_sdata:
1831   case dwarf::DW_FORM_sec_offset:
1832   case dwarf::DW_FORM_flag:
1833   case dwarf::DW_FORM_flag_present:
1834     return cloneScalarAttribute(Die, InputDIE, Unit, AttrSpec, Val, AttrSize,
1835                                 Info);
1836   default:
1837     reportWarning("Unsupported attribute form in cloneAttribute. Dropping.", &U,
1838                   &InputDIE);
1839   }
1840
1841   return 0;
1842 }
1843
1844 /// \brief Apply the valid relocations found by findValidRelocs() to
1845 /// the buffer \p Data, taking into account that Data is at \p BaseOffset
1846 /// in the debug_info section.
1847 ///
1848 /// Like for findValidRelocs(), this function must be called with
1849 /// monotonic \p BaseOffset values.
1850 ///
1851 /// \returns wether any reloc has been applied.
1852 bool DwarfLinker::applyValidRelocs(MutableArrayRef<char> Data,
1853                                    uint32_t BaseOffset, bool isLittleEndian) {
1854   assert((NextValidReloc == 0 ||
1855           BaseOffset > ValidRelocs[NextValidReloc - 1].Offset) &&
1856          "BaseOffset should only be increasing.");
1857   if (NextValidReloc >= ValidRelocs.size())
1858     return false;
1859
1860   // Skip relocs that haven't been applied.
1861   while (NextValidReloc < ValidRelocs.size() &&
1862          ValidRelocs[NextValidReloc].Offset < BaseOffset)
1863     ++NextValidReloc;
1864
1865   bool Applied = false;
1866   uint64_t EndOffset = BaseOffset + Data.size();
1867   while (NextValidReloc < ValidRelocs.size() &&
1868          ValidRelocs[NextValidReloc].Offset >= BaseOffset &&
1869          ValidRelocs[NextValidReloc].Offset < EndOffset) {
1870     const auto &ValidReloc = ValidRelocs[NextValidReloc++];
1871     assert(ValidReloc.Offset - BaseOffset < Data.size());
1872     assert(ValidReloc.Offset - BaseOffset + ValidReloc.Size <= Data.size());
1873     char Buf[8];
1874     uint64_t Value = ValidReloc.Mapping->getValue().BinaryAddress;
1875     Value += ValidReloc.Addend;
1876     for (unsigned i = 0; i != ValidReloc.Size; ++i) {
1877       unsigned Index = isLittleEndian ? i : (ValidReloc.Size - i - 1);
1878       Buf[i] = uint8_t(Value >> (Index * 8));
1879     }
1880     assert(ValidReloc.Size <= sizeof(Buf));
1881     memcpy(&Data[ValidReloc.Offset - BaseOffset], Buf, ValidReloc.Size);
1882     Applied = true;
1883   }
1884
1885   return Applied;
1886 }
1887
1888 /// \brief Recursively clone \p InputDIE's subtrees that have been
1889 /// selected to appear in the linked output.
1890 ///
1891 /// \param OutOffset is the Offset where the newly created DIE will
1892 /// lie in the linked compile unit.
1893 ///
1894 /// \returns the cloned DIE object or null if nothing was selected.
1895 DIE *DwarfLinker::cloneDIE(const DWARFDebugInfoEntryMinimal &InputDIE,
1896                            CompileUnit &Unit, int64_t PCOffset,
1897                            uint32_t OutOffset) {
1898   DWARFUnit &U = Unit.getOrigUnit();
1899   unsigned Idx = U.getDIEIndex(&InputDIE);
1900   CompileUnit::DIEInfo &Info = Unit.getInfo(Idx);
1901
1902   // Should the DIE appear in the output?
1903   if (!Unit.getInfo(Idx).Keep)
1904     return nullptr;
1905
1906   uint32_t Offset = InputDIE.getOffset();
1907   // The DIE might have been already created by a forward reference
1908   // (see cloneDieReferenceAttribute()).
1909   DIE *Die = Info.Clone;
1910   if (!Die)
1911     Die = Info.Clone = new DIE(dwarf::Tag(InputDIE.getTag()));
1912   assert(Die->getTag() == InputDIE.getTag());
1913   Die->setOffset(OutOffset);
1914
1915   // Extract and clone every attribute.
1916   DataExtractor Data = U.getDebugInfoExtractor();
1917   uint32_t NextOffset = U.getDIEAtIndex(Idx + 1)->getOffset();
1918   AttributesInfo AttrInfo;
1919
1920   // We could copy the data only if we need to aply a relocation to
1921   // it. After testing, it seems there is no performance downside to
1922   // doing the copy unconditionally, and it makes the code simpler.
1923   SmallString<40> DIECopy(Data.getData().substr(Offset, NextOffset - Offset));
1924   Data = DataExtractor(DIECopy, Data.isLittleEndian(), Data.getAddressSize());
1925   // Modify the copy with relocated addresses.
1926   if (applyValidRelocs(DIECopy, Offset, Data.isLittleEndian())) {
1927     // If we applied relocations, we store the value of high_pc that was
1928     // potentially stored in the input DIE. If high_pc is an address
1929     // (Dwarf version == 2), then it might have been relocated to a
1930     // totally unrelated value (because the end address in the object
1931     // file might be start address of another function which got moved
1932     // independantly by the linker). The computation of the actual
1933     // high_pc value is done in cloneAddressAttribute().
1934     AttrInfo.OrigHighPc =
1935         InputDIE.getAttributeValueAsAddress(&U, dwarf::DW_AT_high_pc, 0);
1936   }
1937
1938   // Reset the Offset to 0 as we will be working on the local copy of
1939   // the data.
1940   Offset = 0;
1941
1942   const auto *Abbrev = InputDIE.getAbbreviationDeclarationPtr();
1943   Offset += getULEB128Size(Abbrev->getCode());
1944
1945   // We are entering a subprogram. Get and propagate the PCOffset.
1946   if (Die->getTag() == dwarf::DW_TAG_subprogram)
1947     PCOffset = Info.AddrAdjust;
1948   AttrInfo.PCOffset = PCOffset;
1949
1950   for (const auto &AttrSpec : Abbrev->attributes()) {
1951     DWARFFormValue Val(AttrSpec.Form);
1952     uint32_t AttrSize = Offset;
1953     Val.extractValue(Data, &Offset, &U);
1954     AttrSize = Offset - AttrSize;
1955
1956     OutOffset +=
1957         cloneAttribute(*Die, InputDIE, Unit, Val, AttrSpec, AttrSize, AttrInfo);
1958   }
1959
1960   DIEAbbrev &NewAbbrev = Die->getAbbrev();
1961   // If a scope DIE is kept, we must have kept at least one child. If
1962   // it's not the case, we'll just be emitting one wasteful end of
1963   // children marker, but things won't break.
1964   if (InputDIE.hasChildren())
1965     NewAbbrev.setChildrenFlag(dwarf::DW_CHILDREN_yes);
1966   // Assign a permanent abbrev number
1967   AssignAbbrev(Die->getAbbrev());
1968
1969   // Add the size of the abbreviation number to the output offset.
1970   OutOffset += getULEB128Size(Die->getAbbrevNumber());
1971
1972   if (!Abbrev->hasChildren()) {
1973     // Update our size.
1974     Die->setSize(OutOffset - Die->getOffset());
1975     return Die;
1976   }
1977
1978   // Recursively clone children.
1979   for (auto *Child = InputDIE.getFirstChild(); Child && !Child->isNULL();
1980        Child = Child->getSibling()) {
1981     if (DIE *Clone = cloneDIE(*Child, Unit, PCOffset, OutOffset)) {
1982       Die->addChild(std::unique_ptr<DIE>(Clone));
1983       OutOffset = Clone->getOffset() + Clone->getSize();
1984     }
1985   }
1986
1987   // Account for the end of children marker.
1988   OutOffset += sizeof(int8_t);
1989   // Update our size.
1990   Die->setSize(OutOffset - Die->getOffset());
1991   return Die;
1992 }
1993
1994 /// \brief Patch the input object file relevant debug_ranges entries
1995 /// and emit them in the output file. Update the relevant attributes
1996 /// to point at the new entries.
1997 void DwarfLinker::patchRangesForUnit(const CompileUnit &Unit,
1998                                      DWARFContext &OrigDwarf) const {
1999   DWARFDebugRangeList RangeList;
2000   const auto &FunctionRanges = Unit.getFunctionRanges();
2001   unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize();
2002   DataExtractor RangeExtractor(OrigDwarf.getRangeSection(),
2003                                OrigDwarf.isLittleEndian(), AddressSize);
2004   auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
2005   DWARFUnit &OrigUnit = Unit.getOrigUnit();
2006   const auto *OrigUnitDie = OrigUnit.getCompileUnitDIE(false);
2007   uint64_t OrigLowPc = OrigUnitDie->getAttributeValueAsAddress(
2008       &OrigUnit, dwarf::DW_AT_low_pc, -1ULL);
2009   // Ranges addresses are based on the unit's low_pc. Compute the
2010   // offset we need to apply to adapt to the the new unit's low_pc.
2011   int64_t UnitPcOffset = 0;
2012   if (OrigLowPc != -1ULL)
2013     UnitPcOffset = int64_t(OrigLowPc) - Unit.getLowPc();
2014
2015   for (const auto &RangeAttribute : Unit.getRangesAttributes()) {
2016     uint32_t Offset = RangeAttribute->getValue();
2017     RangeAttribute->setValue(Streamer->getRangesSectionSize());
2018     RangeList.extract(RangeExtractor, &Offset);
2019     const auto &Entries = RangeList.getEntries();
2020     const DWARFDebugRangeList::RangeListEntry &First = Entries.front();
2021
2022     if (CurrRange == InvalidRange || First.StartAddress < CurrRange.start() ||
2023         First.StartAddress >= CurrRange.stop()) {
2024       CurrRange = FunctionRanges.find(First.StartAddress + OrigLowPc);
2025       if (CurrRange == InvalidRange ||
2026           CurrRange.start() > First.StartAddress + OrigLowPc) {
2027         reportWarning("no mapping for range.");
2028         continue;
2029       }
2030     }
2031
2032     Streamer->emitRangesEntries(UnitPcOffset, OrigLowPc, CurrRange, Entries,
2033                                 AddressSize);
2034   }
2035 }
2036
2037 /// \brief Generate the debug_aranges entries for \p Unit and if the
2038 /// unit has a DW_AT_ranges attribute, also emit the debug_ranges
2039 /// contribution for this attribute.
2040 /// FIXME: this could actually be done right in patchRangesForUnit,
2041 /// but for the sake of initial bit-for-bit compatibility with legacy
2042 /// dsymutil, we have to do it in a delayed pass.
2043 void DwarfLinker::generateUnitRanges(CompileUnit &Unit) const {
2044   DIEInteger *Attr = Unit.getUnitRangesAttribute();
2045   if (Attr)
2046     Attr->setValue(Streamer->getRangesSectionSize());
2047   Streamer->emitUnitRangesEntries(Unit, Attr != nullptr);
2048 }
2049
2050 /// \brief Insert the new line info sequence \p Seq into the current
2051 /// set of already linked line info \p Rows.
2052 static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
2053                                std::vector<DWARFDebugLine::Row> &Rows) {
2054   if (Seq.empty())
2055     return;
2056
2057   if (!Rows.empty() && Rows.back().Address < Seq.front().Address) {
2058     Rows.insert(Rows.end(), Seq.begin(), Seq.end());
2059     Seq.clear();
2060     return;
2061   }
2062
2063   auto InsertPoint = std::lower_bound(
2064       Rows.begin(), Rows.end(), Seq.front(),
2065       [](const DWARFDebugLine::Row &LHS, const DWARFDebugLine::Row &RHS) {
2066         return LHS.Address < RHS.Address;
2067       });
2068
2069   // FIXME: this only removes the unneeded end_sequence if the
2070   // sequences have been inserted in order. using a global sort like
2071   // described in patchLineTableForUnit() and delaying the end_sequene
2072   // elimination to emitLineTableForUnit() we can get rid of all of them.
2073   if (InsertPoint != Rows.end() &&
2074       InsertPoint->Address == Seq.front().Address && InsertPoint->EndSequence) {
2075     *InsertPoint = Seq.front();
2076     Rows.insert(InsertPoint + 1, Seq.begin() + 1, Seq.end());
2077   } else {
2078     Rows.insert(InsertPoint, Seq.begin(), Seq.end());
2079   }
2080
2081   Seq.clear();
2082 }
2083
2084 /// \brief Extract the line table for \p Unit from \p OrigDwarf, and
2085 /// recreate a relocated version of these for the address ranges that
2086 /// are present in the binary.
2087 void DwarfLinker::patchLineTableForUnit(CompileUnit &Unit,
2088                                         DWARFContext &OrigDwarf) {
2089   const DWARFDebugInfoEntryMinimal *CUDie =
2090       Unit.getOrigUnit().getCompileUnitDIE();
2091   uint64_t StmtList = CUDie->getAttributeValueAsSectionOffset(
2092       &Unit.getOrigUnit(), dwarf::DW_AT_stmt_list, -1ULL);
2093   if (StmtList == -1ULL)
2094     return;
2095
2096   // Update the cloned DW_AT_stmt_list with the correct debug_line offset.
2097   if (auto *OutputDIE = Unit.getOutputUnitDIE()) {
2098     const auto &Abbrev = OutputDIE->getAbbrev().getData();
2099     auto Stmt = std::find_if(
2100         Abbrev.begin(), Abbrev.end(), [](const DIEAbbrevData &AbbrevData) {
2101           return AbbrevData.getAttribute() == dwarf::DW_AT_stmt_list;
2102         });
2103     assert(Stmt < Abbrev.end() && "Didn't find DW_AT_stmt_list in cloned DIE!");
2104     DIEInteger *StmtAttr =
2105         cast<DIEInteger>(OutputDIE->getValues()[Stmt - Abbrev.begin()]);
2106     StmtAttr->setValue(Streamer->getLineSectionSize());
2107   }
2108
2109   // Parse the original line info for the unit.
2110   DWARFDebugLine::LineTable LineTable;
2111   uint32_t StmtOffset = StmtList;
2112   StringRef LineData = OrigDwarf.getLineSection().Data;
2113   DataExtractor LineExtractor(LineData, OrigDwarf.isLittleEndian(),
2114                               Unit.getOrigUnit().getAddressByteSize());
2115   LineTable.parse(LineExtractor, &OrigDwarf.getLineSection().Relocs,
2116                   &StmtOffset);
2117
2118   // This vector is the output line table.
2119   std::vector<DWARFDebugLine::Row> NewRows;
2120   NewRows.reserve(LineTable.Rows.size());
2121
2122   // Current sequence of rows being extracted, before being inserted
2123   // in NewRows.
2124   std::vector<DWARFDebugLine::Row> Seq;
2125   const auto &FunctionRanges = Unit.getFunctionRanges();
2126   auto InvalidRange = FunctionRanges.end(), CurrRange = InvalidRange;
2127
2128   // FIXME: This logic is meant to generate exactly the same output as
2129   // Darwin's classic dsynutil. There is a nicer way to implement this
2130   // by simply putting all the relocated line info in NewRows and simply
2131   // sorting NewRows before passing it to emitLineTableForUnit. This
2132   // should be correct as sequences for a function should stay
2133   // together in the sorted output. There are a few corner cases that
2134   // look suspicious though, and that required to implement the logic
2135   // this way. Revisit that once initial validation is finished.
2136
2137   // Iterate over the object file line info and extract the sequences
2138   // that correspond to linked functions.
2139   for (auto &Row : LineTable.Rows) {
2140     // Check wether we stepped out of the range. The range is
2141     // half-open, but consider accept the end address of the range if
2142     // it is marked as end_sequence in the input (because in that
2143     // case, the relocation offset is accurate and that entry won't
2144     // serve as the start of another function).
2145     if (CurrRange == InvalidRange || Row.Address < CurrRange.start() ||
2146         Row.Address > CurrRange.stop() ||
2147         (Row.Address == CurrRange.stop() && !Row.EndSequence)) {
2148       // We just stepped out of a known range. Insert a end_sequence
2149       // corresponding to the end of the range.
2150       uint64_t StopAddress = CurrRange != InvalidRange
2151                                  ? CurrRange.stop() + CurrRange.value()
2152                                  : -1ULL;
2153       CurrRange = FunctionRanges.find(Row.Address);
2154       bool CurrRangeValid =
2155           CurrRange != InvalidRange && CurrRange.start() <= Row.Address;
2156       if (!CurrRangeValid) {
2157         CurrRange = InvalidRange;
2158         if (StopAddress != -1ULL) {
2159           // Try harder by looking in the DebugMapObject function
2160           // ranges map. There are corner cases where this finds a
2161           // valid entry. It's unclear if this is right or wrong, but
2162           // for now do as dsymutil.
2163           // FIXME: Understand exactly what cases this addresses and
2164           // potentially remove it along with the Ranges map.
2165           auto Range = Ranges.lower_bound(Row.Address);
2166           if (Range != Ranges.begin() && Range != Ranges.end())
2167             --Range;
2168
2169           if (Range != Ranges.end() && Range->first <= Row.Address &&
2170               Range->second.first >= Row.Address) {
2171             StopAddress = Row.Address + Range->second.second;
2172           }
2173         }
2174       }
2175       if (StopAddress != -1ULL && !Seq.empty()) {
2176         // Insert end sequence row with the computed end address, but
2177         // the same line as the previous one.
2178         Seq.emplace_back(Seq.back());
2179         Seq.back().Address = StopAddress;
2180         Seq.back().EndSequence = 1;
2181         Seq.back().PrologueEnd = 0;
2182         Seq.back().BasicBlock = 0;
2183         Seq.back().EpilogueBegin = 0;
2184         insertLineSequence(Seq, NewRows);
2185       }
2186
2187       if (!CurrRangeValid)
2188         continue;
2189     }
2190
2191     // Ignore empty sequences.
2192     if (Row.EndSequence && Seq.empty())
2193       continue;
2194
2195     // Relocate row address and add it to the current sequence.
2196     Row.Address += CurrRange.value();
2197     Seq.emplace_back(Row);
2198
2199     if (Row.EndSequence)
2200       insertLineSequence(Seq, NewRows);
2201   }
2202
2203   // Finished extracting, now emit the line tables.
2204   uint32_t PrologueEnd = StmtList + 10 + LineTable.Prologue.PrologueLength;
2205   // FIXME: LLVM hardcodes it's prologue values. We just copy the
2206   // prologue over and that works because we act as both producer and
2207   // consumer. It would be nicer to have a real configurable line
2208   // table emitter.
2209   if (LineTable.Prologue.Version != 2 ||
2210       LineTable.Prologue.DefaultIsStmt != DWARF2_LINE_DEFAULT_IS_STMT ||
2211       LineTable.Prologue.LineBase != -5 || LineTable.Prologue.LineRange != 14 ||
2212       LineTable.Prologue.OpcodeBase != 13)
2213     reportWarning("line table paramters mismatch. Cannot emit.");
2214   else
2215     Streamer->emitLineTableForUnit(LineData.slice(StmtList + 4, PrologueEnd),
2216                                    LineTable.Prologue.MinInstLength, NewRows,
2217                                    Unit.getOrigUnit().getAddressByteSize());
2218 }
2219
2220 bool DwarfLinker::link(const DebugMap &Map) {
2221
2222   if (Map.begin() == Map.end()) {
2223     errs() << "Empty debug map.\n";
2224     return false;
2225   }
2226
2227   if (!createStreamer(Map.getTriple(), OutputFilename))
2228     return false;
2229
2230   // Size of the DIEs (and headers) generated for the linked output.
2231   uint64_t OutputDebugInfoSize = 0;
2232   // A unique ID that identifies each compile unit.
2233   unsigned UnitID = 0;
2234   for (const auto &Obj : Map.objects()) {
2235     CurrentDebugObject = Obj.get();
2236
2237     if (Options.Verbose)
2238       outs() << "DEBUG MAP OBJECT: " << Obj->getObjectFilename() << "\n";
2239     auto ErrOrObj = BinHolder.GetObjectFile(Obj->getObjectFilename());
2240     if (std::error_code EC = ErrOrObj.getError()) {
2241       reportWarning(Twine(Obj->getObjectFilename()) + ": " + EC.message());
2242       continue;
2243     }
2244
2245     // Look for relocations that correspond to debug map entries.
2246     if (!findValidRelocsInDebugInfo(*ErrOrObj, *Obj)) {
2247       if (Options.Verbose)
2248         outs() << "No valid relocations found. Skipping.\n";
2249       continue;
2250     }
2251
2252     // Setup access to the debug info.
2253     DWARFContextInMemory DwarfContext(*ErrOrObj);
2254     startDebugObject(DwarfContext, *Obj);
2255
2256     // In a first phase, just read in the debug info and store the DIE
2257     // parent links that we will use during the next phase.
2258     for (const auto &CU : DwarfContext.compile_units()) {
2259       auto *CUDie = CU->getCompileUnitDIE(false);
2260       if (Options.Verbose) {
2261         outs() << "Input compilation unit:";
2262         CUDie->dump(outs(), CU.get(), 0);
2263       }
2264       Units.emplace_back(*CU, UnitID++);
2265       gatherDIEParents(CUDie, 0, Units.back());
2266     }
2267
2268     // Then mark all the DIEs that need to be present in the linked
2269     // output and collect some information about them. Note that this
2270     // loop can not be merged with the previous one becaue cross-cu
2271     // references require the ParentIdx to be setup for every CU in
2272     // the object file before calling this.
2273     for (auto &CurrentUnit : Units)
2274       lookForDIEsToKeep(*CurrentUnit.getOrigUnit().getCompileUnitDIE(), *Obj,
2275                         CurrentUnit, 0);
2276
2277     // The calls to applyValidRelocs inside cloneDIE will walk the
2278     // reloc array again (in the same way findValidRelocsInDebugInfo()
2279     // did). We need to reset the NextValidReloc index to the beginning.
2280     NextValidReloc = 0;
2281
2282     // Construct the output DIE tree by cloning the DIEs we chose to
2283     // keep above. If there are no valid relocs, then there's nothing
2284     // to clone/emit.
2285     if (!ValidRelocs.empty())
2286       for (auto &CurrentUnit : Units) {
2287         const auto *InputDIE = CurrentUnit.getOrigUnit().getCompileUnitDIE();
2288         CurrentUnit.setStartOffset(OutputDebugInfoSize);
2289         DIE *OutputDIE = cloneDIE(*InputDIE, CurrentUnit, 0 /* PCOffset */,
2290                                   11 /* Unit Header size */);
2291         CurrentUnit.setOutputUnitDIE(OutputDIE);
2292         OutputDebugInfoSize = CurrentUnit.computeNextUnitOffset();
2293         if (Options.NoOutput)
2294           continue;
2295         // FIXME: for compatibility with the classic dsymutil, we emit
2296         // an empty line table for the unit, even if the unit doesn't
2297         // actually exist in the DIE tree.
2298         patchLineTableForUnit(CurrentUnit, DwarfContext);
2299         if (!OutputDIE)
2300           continue;
2301         patchRangesForUnit(CurrentUnit, DwarfContext);
2302         Streamer->emitLocationsForUnit(CurrentUnit, DwarfContext);
2303       }
2304
2305     // Emit all the compile unit's debug information.
2306     if (!ValidRelocs.empty() && !Options.NoOutput)
2307       for (auto &CurrentUnit : Units) {
2308         generateUnitRanges(CurrentUnit);
2309         CurrentUnit.fixupForwardReferences();
2310         Streamer->emitCompileUnitHeader(CurrentUnit);
2311         if (!CurrentUnit.getOutputUnitDIE())
2312           continue;
2313         Streamer->emitDIE(*CurrentUnit.getOutputUnitDIE());
2314       }
2315
2316     // Clean-up before starting working on the next object.
2317     endDebugObject();
2318   }
2319
2320   // Emit everything that's global.
2321   if (!Options.NoOutput) {
2322     Streamer->emitAbbrevs(Abbreviations);
2323     Streamer->emitStrings(StringPool);
2324   }
2325
2326   return Options.NoOutput ? true : Streamer->finish();
2327 }
2328 }
2329
2330 bool linkDwarf(StringRef OutputFilename, const DebugMap &DM,
2331                const LinkOptions &Options) {
2332   DwarfLinker Linker(OutputFilename, Options);
2333   return Linker.link(DM);
2334 }
2335 }
2336 }