1 //===- bolt/Core/DebugData.cpp - Debugging information handling -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements functions and classes for handling debug info.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "bolt/Core/DebugData.h"
14 #include "bolt/Core/BinaryContext.h"
15 #include "bolt/Utils/Utils.h"
16 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCObjectStreamer.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/EndianStream.h"
23 #include "llvm/Support/LEB128.h"
24 #include "llvm/Support/SHA1.h"
25 #include <algorithm>
26 #include <cassert>
27 #include <cstdint>
28 #include <limits>
29 #include <unordered_map>
30 
31 #define DEBUG_TYPE "bolt-debug-info"
32 
33 namespace opts {
34 extern llvm::cl::opt<unsigned> Verbosity;
35 } // namespace opts
36 
37 namespace llvm {
38 class MCSymbol;
39 
40 namespace bolt {
41 
42 /// Finds attributes FormValue and Offset.
43 ///
44 /// \param DIE die to look up in.
45 /// \param Index the attribute index to extract.
46 /// \return an optional AttrInfo with DWARFFormValue and Offset.
47 Optional<AttrInfo>
48 findAttributeInfo(const DWARFDie DIE,
49                   const DWARFAbbreviationDeclaration *AbbrevDecl,
50                   uint32_t Index) {
51   const DWARFUnit &U = *DIE.getDwarfUnit();
52   uint64_t Offset =
53       AbbrevDecl->getAttributeOffsetFromIndex(Index, DIE.getOffset(), U);
54   Optional<DWARFFormValue> Value =
55       AbbrevDecl->getAttributeValueFromOffset(Index, Offset, U);
56   if (!Value)
57     return None;
58   // AttributeSpec
59   const DWARFAbbreviationDeclaration::AttributeSpec *AttrVal =
60       AbbrevDecl->attributes().begin() + Index;
61   uint32_t ValSize = 0;
62   Optional<int64_t> ValSizeOpt = AttrVal->getByteSize(U);
63   if (ValSizeOpt) {
64     ValSize = static_cast<uint32_t>(*ValSizeOpt);
65   } else {
66     DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor();
67     uint64_t NewOffset = Offset;
68     DWARFFormValue::skipValue(Value->getForm(), DebugInfoData, &NewOffset,
69                               U.getFormParams());
70     // This includes entire size of the entry, which might not be just the
71     // encoding part. For example for DW_AT_loc it will include expression
72     // location.
73     ValSize = NewOffset - Offset;
74   }
75 
76   return AttrInfo{*Value, Offset, ValSize};
77 }
78 
79 const DebugLineTableRowRef DebugLineTableRowRef::NULL_ROW{0, 0};
80 
81 namespace {
82 
83 LLVM_ATTRIBUTE_UNUSED
84 static void printLE64(const std::string &S) {
85   for (uint32_t I = 0, Size = S.size(); I < Size; ++I) {
86     errs() << Twine::utohexstr(S[I]);
87     errs() << Twine::utohexstr((int8_t)S[I]);
88   }
89   errs() << "\n";
90 }
91 
92 // Writes address ranges to Writer as pairs of 64-bit (address, size).
93 // If RelativeRange is true, assumes the address range to be written must be of
94 // the form (begin address, range size), otherwise (begin address, end address).
95 // Terminates the list by writing a pair of two zeroes.
96 // Returns the number of written bytes.
97 uint64_t writeAddressRanges(raw_svector_ostream &Stream,
98                             const DebugAddressRangesVector &AddressRanges,
99                             const bool WriteRelativeRanges = false) {
100   for (const DebugAddressRange &Range : AddressRanges) {
101     support::endian::write(Stream, Range.LowPC, support::little);
102     support::endian::write(
103         Stream, WriteRelativeRanges ? Range.HighPC - Range.LowPC : Range.HighPC,
104         support::little);
105   }
106   // Finish with 0 entries.
107   support::endian::write(Stream, 0ULL, support::little);
108   support::endian::write(Stream, 0ULL, support::little);
109   return AddressRanges.size() * 16 + 16;
110 }
111 
112 } // namespace
113 
114 DebugRangesSectionWriter::DebugRangesSectionWriter() {
115   RangesBuffer = std::make_unique<DebugBufferVector>();
116   RangesStream = std::make_unique<raw_svector_ostream>(*RangesBuffer);
117 
118   // Add an empty range as the first entry;
119   SectionOffset +=
120       writeAddressRanges(*RangesStream.get(), DebugAddressRangesVector{});
121 }
122 
123 uint64_t DebugRangesSectionWriter::addRanges(
124     DebugAddressRangesVector &&Ranges,
125     std::map<DebugAddressRangesVector, uint64_t> &CachedRanges) {
126   if (Ranges.empty())
127     return getEmptyRangesOffset();
128 
129   const auto RI = CachedRanges.find(Ranges);
130   if (RI != CachedRanges.end())
131     return RI->second;
132 
133   const uint64_t EntryOffset = addRanges(Ranges);
134   CachedRanges.emplace(std::move(Ranges), EntryOffset);
135 
136   return EntryOffset;
137 }
138 
139 uint64_t
140 DebugRangesSectionWriter::addRanges(const DebugAddressRangesVector &Ranges) {
141   if (Ranges.empty())
142     return getEmptyRangesOffset();
143 
144   // Reading the SectionOffset and updating it should be atomic to guarantee
145   // unique and correct offsets in patches.
146   std::lock_guard<std::mutex> Lock(WriterMutex);
147   const uint32_t EntryOffset = SectionOffset;
148   SectionOffset += writeAddressRanges(*RangesStream.get(), Ranges);
149 
150   return EntryOffset;
151 }
152 
153 uint64_t DebugRangesSectionWriter::getSectionOffset() {
154   std::lock_guard<std::mutex> Lock(WriterMutex);
155   return SectionOffset;
156 }
157 
158 void DebugARangesSectionWriter::addCURanges(uint64_t CUOffset,
159                                             DebugAddressRangesVector &&Ranges) {
160   std::lock_guard<std::mutex> Lock(CUAddressRangesMutex);
161   CUAddressRanges.emplace(CUOffset, std::move(Ranges));
162 }
163 
164 void DebugARangesSectionWriter::writeARangesSection(
165     raw_svector_ostream &RangesStream, const CUOffsetMap &CUMap) const {
166   // For reference on the format of the .debug_aranges section, see the DWARF4
167   // specification, section 6.1.4 Lookup by Address
168   // http://www.dwarfstd.org/doc/DWARF4.pdf
169   for (const auto &CUOffsetAddressRangesPair : CUAddressRanges) {
170     const uint64_t Offset = CUOffsetAddressRangesPair.first;
171     const DebugAddressRangesVector &AddressRanges =
172         CUOffsetAddressRangesPair.second;
173 
174     // Emit header.
175 
176     // Size of this set: 8 (size of the header) + 4 (padding after header)
177     // + 2*sizeof(uint64_t) bytes for each of the ranges, plus an extra
178     // pair of uint64_t's for the terminating, zero-length range.
179     // Does not include size field itself.
180     uint32_t Size = 8 + 4 + 2 * sizeof(uint64_t) * (AddressRanges.size() + 1);
181 
182     // Header field #1: set size.
183     support::endian::write(RangesStream, Size, support::little);
184 
185     // Header field #2: version number, 2 as per the specification.
186     support::endian::write(RangesStream, static_cast<uint16_t>(2),
187                            support::little);
188 
189     assert(CUMap.count(Offset) && "Original CU offset is not found in CU Map");
190     // Header field #3: debug info offset of the correspondent compile unit.
191     support::endian::write(
192         RangesStream, static_cast<uint32_t>(CUMap.find(Offset)->second.Offset),
193         support::little);
194 
195     // Header field #4: address size.
196     // 8 since we only write ELF64 binaries for now.
197     RangesStream << char(8);
198 
199     // Header field #5: segment size of target architecture.
200     RangesStream << char(0);
201 
202     // Padding before address table - 4 bytes in the 64-bit-pointer case.
203     support::endian::write(RangesStream, static_cast<uint32_t>(0),
204                            support::little);
205 
206     writeAddressRanges(RangesStream, AddressRanges, true);
207   }
208 }
209 
210 DebugAddrWriter::DebugAddrWriter(BinaryContext *Bc) { BC = Bc; }
211 
212 void DebugAddrWriter::AddressForDWOCU::dump() {
213   std::vector<IndexAddressPair> SortedMap(indexToAddressBegin(),
214                                           indexToAdddessEnd());
215   // Sorting address in increasing order of indices.
216   std::sort(SortedMap.begin(), SortedMap.end(),
217             [](const IndexAddressPair &A, const IndexAddressPair &B) {
218               return A.first < B.first;
219             });
220   for (auto &Pair : SortedMap)
221     dbgs() << Twine::utohexstr(Pair.second) << "\t" << Pair.first << "\n";
222 }
223 uint32_t DebugAddrWriter::getIndexFromAddress(uint64_t Address,
224                                               uint64_t DWOId) {
225   std::lock_guard<std::mutex> Lock(WriterMutex);
226   if (!AddressMaps.count(DWOId))
227     AddressMaps[DWOId] = AddressForDWOCU();
228 
229   AddressForDWOCU &Map = AddressMaps[DWOId];
230   auto Entry = Map.find(Address);
231   if (Entry == Map.end()) {
232     auto Index = Map.getNextIndex();
233     Entry = Map.insert(Address, Index).first;
234   }
235   return Entry->second;
236 }
237 
238 // Case1) Address is not in map insert in to AddresToIndex and IndexToAddres
239 // Case2) Address is in the map but Index is higher or equal. Need to update
240 // IndexToAddrss. Case3) Address is in the map but Index is lower. Need to
241 // update AddressToIndex and IndexToAddress
242 void DebugAddrWriter::addIndexAddress(uint64_t Address, uint32_t Index,
243                                       uint64_t DWOId) {
244   std::lock_guard<std::mutex> Lock(WriterMutex);
245   AddressForDWOCU &Map = AddressMaps[DWOId];
246   auto Entry = Map.find(Address);
247   if (Entry != Map.end()) {
248     if (Entry->second > Index)
249       Map.updateAddressToIndex(Address, Index);
250     Map.updateIndexToAddrss(Address, Index);
251   } else {
252     Map.insert(Address, Index);
253   }
254 }
255 
256 AddressSectionBuffer DebugAddrWriter::finalize() {
257   // Need to layout all sections within .debug_addr
258   // Within each section sort Address by index.
259   AddressSectionBuffer Buffer;
260   raw_svector_ostream AddressStream(Buffer);
261   for (std::unique_ptr<DWARFUnit> &CU : BC->DwCtx->compile_units()) {
262     Optional<uint64_t> DWOId = CU->getDWOId();
263     // Handling the case wehre debug information is a mix of Debug fission and
264     // monolitic.
265     if (!DWOId)
266       continue;
267     auto AM = AddressMaps.find(*DWOId);
268     // Adding to map even if it did not contribute to .debug_addr.
269     // The Skeleton CU will still have DW_AT_GNU_addr_base.
270     DWOIdToOffsetMap[*DWOId] = Buffer.size();
271     // If does not exist this CUs DWO section didn't contribute to .debug_addr.
272     if (AM == AddressMaps.end())
273       continue;
274     std::vector<IndexAddressPair> SortedMap(AM->second.indexToAddressBegin(),
275                                             AM->second.indexToAdddessEnd());
276     // Sorting address in increasing order of indices.
277     std::sort(SortedMap.begin(), SortedMap.end(),
278               [](const IndexAddressPair &A, const IndexAddressPair &B) {
279                 return A.first < B.first;
280               });
281 
282     uint8_t AddrSize = CU->getAddressByteSize();
283     uint32_t Counter = 0;
284     auto WriteAddress = [&](uint64_t Address) -> void {
285       ++Counter;
286       switch (AddrSize) {
287       default:
288         assert(false && "Address Size is invalid.");
289         break;
290       case 4:
291         support::endian::write(AddressStream, static_cast<uint32_t>(Address),
292                                support::little);
293         break;
294       case 8:
295         support::endian::write(AddressStream, Address, support::little);
296         break;
297       }
298     };
299 
300     for (const IndexAddressPair &Val : SortedMap) {
301       while (Val.first > Counter)
302         WriteAddress(0);
303       WriteAddress(Val.second);
304     }
305   }
306 
307   return Buffer;
308 }
309 
310 uint64_t DebugAddrWriter::getOffset(uint64_t DWOId) {
311   auto Iter = DWOIdToOffsetMap.find(DWOId);
312   assert(Iter != DWOIdToOffsetMap.end() &&
313          "Offset in to.debug_addr was not found for DWO ID.");
314   return Iter->second;
315 }
316 
317 DebugLocWriter::DebugLocWriter(BinaryContext *BC) {
318   LocBuffer = std::make_unique<DebugBufferVector>();
319   LocStream = std::make_unique<raw_svector_ostream>(*LocBuffer);
320 }
321 
322 void DebugLocWriter::addList(uint64_t AttrOffset,
323                              DebugLocationsVector &&LocList) {
324   if (LocList.empty()) {
325     EmptyAttrLists.push_back(AttrOffset);
326     return;
327   }
328   // Since there is a separate DebugLocWriter for each thread,
329   // we don't need a lock to read the SectionOffset and update it.
330   const uint32_t EntryOffset = SectionOffset;
331 
332   for (const DebugLocationEntry &Entry : LocList) {
333     support::endian::write(*LocStream, static_cast<uint64_t>(Entry.LowPC),
334                            support::little);
335     support::endian::write(*LocStream, static_cast<uint64_t>(Entry.HighPC),
336                            support::little);
337     support::endian::write(*LocStream, static_cast<uint16_t>(Entry.Expr.size()),
338                            support::little);
339     *LocStream << StringRef(reinterpret_cast<const char *>(Entry.Expr.data()),
340                             Entry.Expr.size());
341     SectionOffset += 2 * 8 + 2 + Entry.Expr.size();
342   }
343   LocStream->write_zeros(16);
344   SectionOffset += 16;
345   LocListDebugInfoPatches.push_back({AttrOffset, EntryOffset});
346 }
347 
348 void DebugLoclistWriter::addList(uint64_t AttrOffset,
349                                  DebugLocationsVector &&LocList) {
350   Patches.push_back({AttrOffset, std::move(LocList)});
351 }
352 
353 std::unique_ptr<DebugBufferVector> DebugLocWriter::getBuffer() {
354   return std::move(LocBuffer);
355 }
356 
357 // DWARF 4: 2.6.2
358 void DebugLocWriter::finalize(uint64_t SectionOffset,
359                               SimpleBinaryPatcher &DebugInfoPatcher) {
360   for (const auto LocListDebugInfoPatchType : LocListDebugInfoPatches) {
361     uint64_t Offset = SectionOffset + LocListDebugInfoPatchType.LocListOffset;
362     DebugInfoPatcher.addLE32Patch(LocListDebugInfoPatchType.DebugInfoAttrOffset,
363                                   Offset);
364   }
365 
366   for (uint64_t DebugInfoAttrOffset : EmptyAttrLists)
367     DebugInfoPatcher.addLE32Patch(DebugInfoAttrOffset,
368                                   DebugLocWriter::EmptyListOffset);
369 }
370 
371 void DebugLoclistWriter::finalize(uint64_t SectionOffset,
372                                   SimpleBinaryPatcher &DebugInfoPatcher) {
373   for (LocPatch &Patch : Patches) {
374     if (Patch.LocList.empty()) {
375       DebugInfoPatcher.addLE32Patch(Patch.AttrOffset,
376                                     DebugLocWriter::EmptyListOffset);
377       continue;
378     }
379     const uint32_t EntryOffset = LocBuffer->size();
380     for (const DebugLocationEntry &Entry : Patch.LocList) {
381       support::endian::write(*LocStream,
382                              static_cast<uint8_t>(dwarf::DW_LLE_startx_length),
383                              support::little);
384       uint32_t Index = AddrWriter->getIndexFromAddress(Entry.LowPC, DWOId);
385       encodeULEB128(Index, *LocStream);
386 
387       // TODO: Support DWARF5
388       support::endian::write(*LocStream,
389                              static_cast<uint32_t>(Entry.HighPC - Entry.LowPC),
390                              support::little);
391       support::endian::write(*LocStream,
392                              static_cast<uint16_t>(Entry.Expr.size()),
393                              support::little);
394       *LocStream << StringRef(reinterpret_cast<const char *>(Entry.Expr.data()),
395                               Entry.Expr.size());
396     }
397     support::endian::write(*LocStream,
398                            static_cast<uint8_t>(dwarf::DW_LLE_end_of_list),
399                            support::little);
400     DebugInfoPatcher.addLE32Patch(Patch.AttrOffset, EntryOffset);
401     clearList(Patch.LocList);
402   }
403   clearList(Patches);
404 }
405 
406 DebugAddrWriter *DebugLoclistWriter::AddrWriter = nullptr;
407 
408 void DebugInfoBinaryPatcher::addUnitBaseOffsetLabel(uint64_t Offset) {
409   Offset -= DWPUnitOffset;
410   std::lock_guard<std::mutex> Lock(WriterMutex);
411   DebugPatches.emplace_back(new DWARFUnitOffsetBaseLabel(Offset));
412 }
413 
414 void DebugInfoBinaryPatcher::addDestinationReferenceLabel(uint64_t Offset) {
415   Offset -= DWPUnitOffset;
416   std::lock_guard<std::mutex> Lock(WriterMutex);
417   auto RetVal = DestinationLabels.insert(Offset);
418   if (!RetVal.second)
419     return;
420 
421   DebugPatches.emplace_back(new DestinationReferenceLabel(Offset));
422 }
423 
424 static std::string encodeLE(size_t ByteSize, uint64_t NewValue) {
425   std::string LE64(ByteSize, 0);
426   for (size_t I = 0; I < ByteSize; ++I) {
427     LE64[I] = NewValue & 0xff;
428     NewValue >>= 8;
429   }
430   return LE64;
431 }
432 
433 void DebugInfoBinaryPatcher::insertNewEntry(const DWARFDie &DIE,
434                                             uint32_t Value) {
435   std::string StrValue = encodeLE(4, Value);
436   insertNewEntry(DIE, std::move(StrValue));
437 }
438 
439 void DebugInfoBinaryPatcher::insertNewEntry(const DWARFDie &DIE,
440                                             std::string &&Value) {
441   const DWARFAbbreviationDeclaration *AbbrevDecl =
442       DIE.getAbbreviationDeclarationPtr();
443 
444   // In case this DIE has no attributes.
445   uint32_t Offset = DIE.getOffset() + 1;
446   size_t NumOfAttributes = AbbrevDecl->getNumAttributes();
447   if (NumOfAttributes) {
448     Optional<AttrInfo> Val =
449         findAttributeInfo(DIE, AbbrevDecl, NumOfAttributes - 1);
450     assert(Val && "Invalid Value.");
451 
452     Offset = Val->Offset + Val->Size - DWPUnitOffset;
453   }
454   std::lock_guard<std::mutex> Lock(WriterMutex);
455   DebugPatches.emplace_back(new NewDebugEntry(Offset, std::move(Value)));
456 }
457 
458 void DebugInfoBinaryPatcher::addReferenceToPatch(uint64_t Offset,
459                                                  uint32_t DestinationOffset,
460                                                  uint32_t OldValueSize,
461                                                  dwarf::Form Form) {
462   Offset -= DWPUnitOffset;
463   DestinationOffset -= DWPUnitOffset;
464   std::lock_guard<std::mutex> Lock(WriterMutex);
465   DebugPatches.emplace_back(
466       new DebugPatchReference(Offset, OldValueSize, DestinationOffset, Form));
467 }
468 
469 void DebugInfoBinaryPatcher::addUDataPatch(uint64_t Offset, uint64_t NewValue,
470                                            uint32_t OldValueSize) {
471   Offset -= DWPUnitOffset;
472   std::lock_guard<std::mutex> Lock(WriterMutex);
473   DebugPatches.emplace_back(
474       new DebugPatchVariableSize(Offset, OldValueSize, NewValue));
475 }
476 
477 void DebugInfoBinaryPatcher::addLE64Patch(uint64_t Offset, uint64_t NewValue) {
478   Offset -= DWPUnitOffset;
479   std::lock_guard<std::mutex> Lock(WriterMutex);
480   DebugPatches.emplace_back(new DebugPatch64(Offset, NewValue));
481 }
482 
483 void DebugInfoBinaryPatcher::addLE32Patch(uint64_t Offset, uint32_t NewValue,
484                                           uint32_t OldValueSize) {
485   Offset -= DWPUnitOffset;
486   std::lock_guard<std::mutex> Lock(WriterMutex);
487   if (OldValueSize == 4)
488     DebugPatches.emplace_back(new DebugPatch32(Offset, NewValue));
489   else
490     DebugPatches.emplace_back(new DebugPatch64to32(Offset, NewValue));
491 }
492 
493 void SimpleBinaryPatcher::addBinaryPatch(uint64_t Offset,
494                                          std::string &&NewValue,
495                                          uint32_t OldValueSize) {
496   Patches.emplace_back(Offset, std::move(NewValue));
497 }
498 
499 void SimpleBinaryPatcher::addBytePatch(uint64_t Offset, uint8_t Value) {
500   auto Str = std::string(1, Value);
501   Patches.emplace_back(Offset, std::move(Str));
502 }
503 
504 void SimpleBinaryPatcher::addLEPatch(uint64_t Offset, uint64_t NewValue,
505                                      size_t ByteSize) {
506   Patches.emplace_back(Offset, encodeLE(ByteSize, NewValue));
507 }
508 
509 void SimpleBinaryPatcher::addUDataPatch(uint64_t Offset, uint64_t Value,
510                                         uint32_t OldValueSize) {
511   std::string Buff;
512   raw_string_ostream OS(Buff);
513   encodeULEB128(Value, OS, OldValueSize);
514 
515   Patches.emplace_back(Offset, std::move(Buff));
516 }
517 
518 void SimpleBinaryPatcher::addLE64Patch(uint64_t Offset, uint64_t NewValue) {
519   addLEPatch(Offset, NewValue, 8);
520 }
521 
522 void SimpleBinaryPatcher::addLE32Patch(uint64_t Offset, uint32_t NewValue,
523                                        uint32_t OldValueSize) {
524   addLEPatch(Offset, NewValue, 4);
525 }
526 
527 std::string SimpleBinaryPatcher::patchBinary(StringRef BinaryContents) {
528   std::string BinaryContentsStr = std::string(BinaryContents);
529   for (const auto &Patch : Patches) {
530     uint32_t Offset = Patch.first;
531     const std::string &ByteSequence = Patch.second;
532     assert(Offset + ByteSequence.size() <= BinaryContents.size() &&
533            "Applied patch runs over binary size.");
534     for (uint64_t I = 0, Size = ByteSequence.size(); I < Size; ++I) {
535       BinaryContentsStr[Offset + I] = ByteSequence[I];
536     }
537   }
538   return BinaryContentsStr;
539 }
540 
541 CUOffsetMap DebugInfoBinaryPatcher::computeNewOffsets(DWARFContext &DWCtx,
542                                                       bool IsDWOContext) {
543   CUOffsetMap CUMap;
544   std::sort(DebugPatches.begin(), DebugPatches.end(),
545             [](const UniquePatchPtrType &V1, const UniquePatchPtrType &V2) {
546               if (V1.get()->Offset == V2.get()->Offset) {
547                 if (V1->Kind == DebugPatchKind::NewDebugEntry &&
548                     V2->Kind == DebugPatchKind::NewDebugEntry)
549                   return reinterpret_cast<const NewDebugEntry *>(V1.get())
550                              ->CurrentOrder <
551                          reinterpret_cast<const NewDebugEntry *>(V2.get())
552                              ->CurrentOrder;
553 
554                 // This is a case where we are modifying first entry of next
555                 // DIE, and adding a new one.
556                 return V1->Kind == DebugPatchKind::NewDebugEntry;
557               }
558               return V1.get()->Offset < V2.get()->Offset;
559             });
560 
561   DWARFUnitVector::compile_unit_range CompileUnits =
562       IsDWOContext ? DWCtx.dwo_compile_units() : DWCtx.compile_units();
563 
564   for (const std::unique_ptr<DWARFUnit> &CU : CompileUnits)
565     CUMap[CU->getOffset()] = {static_cast<uint32_t>(CU->getOffset()),
566                               static_cast<uint32_t>(CU->getLength())};
567 
568   // Calculating changes in .debug_info size from Patches to build a map of old
569   // to updated reference destination offsets.
570   uint32_t PreviousOffset = 0;
571   int32_t PreviousChangeInSize = 0;
572   for (UniquePatchPtrType &PatchBase : DebugPatches) {
573     Patch *P = PatchBase.get();
574     switch (P->Kind) {
575     default:
576       continue;
577     case DebugPatchKind::PatchValue64to32: {
578       PreviousChangeInSize -= 4;
579       break;
580     }
581     case DebugPatchKind::PatchValueVariable: {
582       DebugPatchVariableSize *DPV =
583           reinterpret_cast<DebugPatchVariableSize *>(P);
584       std::string Temp;
585       raw_string_ostream OS(Temp);
586       encodeULEB128(DPV->Value, OS);
587       PreviousChangeInSize += Temp.size() - DPV->OldValueSize;
588       break;
589     }
590     case DebugPatchKind::DestinationReferenceLabel: {
591       DestinationReferenceLabel *DRL =
592           reinterpret_cast<DestinationReferenceLabel *>(P);
593       OldToNewOffset[DRL->Offset] =
594           DRL->Offset + ChangeInSize + PreviousChangeInSize;
595       break;
596     }
597     case DebugPatchKind::ReferencePatchValue: {
598       // This doesn't look to be a common case, so will always encode as 4 bytes
599       // to reduce algorithmic complexity.
600       DebugPatchReference *RDP = reinterpret_cast<DebugPatchReference *>(P);
601       if (RDP->PatchInfo.IndirectRelative) {
602         PreviousChangeInSize += 4 - RDP->PatchInfo.OldValueSize;
603         assert(RDP->PatchInfo.OldValueSize <= 4 &&
604                "Variable encoding reference greater than 4 bytes.");
605       }
606       break;
607     }
608     case DebugPatchKind::DWARFUnitOffsetBaseLabel: {
609       DWARFUnitOffsetBaseLabel *BaseLabel =
610           reinterpret_cast<DWARFUnitOffsetBaseLabel *>(P);
611       uint32_t CUOffset = BaseLabel->Offset;
612       ChangeInSize += PreviousChangeInSize;
613       uint32_t CUOffsetUpdate = CUOffset + ChangeInSize;
614       CUMap[CUOffset].Offset = CUOffsetUpdate;
615       CUMap[PreviousOffset].Length += PreviousChangeInSize;
616       PreviousChangeInSize = 0;
617       PreviousOffset = CUOffset;
618       break;
619     }
620     case DebugPatchKind::NewDebugEntry: {
621       NewDebugEntry *NDE = reinterpret_cast<NewDebugEntry *>(P);
622       PreviousChangeInSize += NDE->Value.size();
623       break;
624     }
625     }
626   }
627   CUMap[PreviousOffset].Length += PreviousChangeInSize;
628   return CUMap;
629 }
630 uint32_t DebugInfoBinaryPatcher::NewDebugEntry::OrderCounter = 0;
631 
632 std::string DebugInfoBinaryPatcher::patchBinary(StringRef BinaryContents) {
633   std::string NewBinaryContents;
634   NewBinaryContents.reserve(BinaryContents.size() + ChangeInSize);
635   uint32_t StartOffset = 0;
636   uint32_t DwarfUnitBaseOffset = 0;
637   uint32_t OldValueSize = 0;
638   uint32_t Offset = 0;
639   std::string ByteSequence;
640   std::vector<std::pair<uint32_t, uint32_t>> LengthPatches;
641   // Wasting one entry to avoid checks for first.
642   LengthPatches.push_back({0, 0});
643 
644   // Applying all the patches replacing current entry.
645   // This might change the size of .debug_info section.
646   for (const UniquePatchPtrType &PatchBase : DebugPatches) {
647     Patch *P = PatchBase.get();
648     switch (P->Kind) {
649     default:
650       continue;
651     case DebugPatchKind::ReferencePatchValue: {
652       DebugPatchReference *RDP = reinterpret_cast<DebugPatchReference *>(P);
653       uint32_t DestinationOffset = RDP->DestinationOffset;
654       assert(OldToNewOffset.count(DestinationOffset) &&
655              "Destination Offset for reference not updated.");
656       uint32_t UpdatedOffset = OldToNewOffset[DestinationOffset];
657       Offset = RDP->Offset;
658       OldValueSize = RDP->PatchInfo.OldValueSize;
659       if (RDP->PatchInfo.DirectRelative) {
660         UpdatedOffset -= DwarfUnitBaseOffset;
661         ByteSequence = encodeLE(OldValueSize, UpdatedOffset);
662         // In theory reference for DW_FORM_ref{1,2,4,8} can be right on the edge
663         // and overflow if later debug information grows.
664         if (ByteSequence.size() > OldValueSize)
665           errs() << "BOLT-ERROR: Relative reference of size "
666                  << Twine::utohexstr(OldValueSize)
667                  << " overflows with the new encoding.\n";
668       } else if (RDP->PatchInfo.DirectAbsolute) {
669         ByteSequence = encodeLE(OldValueSize, UpdatedOffset);
670       } else if (RDP->PatchInfo.IndirectRelative) {
671         UpdatedOffset -= DwarfUnitBaseOffset;
672         ByteSequence.clear();
673         raw_string_ostream OS(ByteSequence);
674         encodeULEB128(UpdatedOffset, OS, 4);
675       } else {
676         llvm_unreachable("Invalid Reference form.");
677       }
678       break;
679     }
680     case DebugPatchKind::PatchValue32: {
681       DebugPatch32 *P32 = reinterpret_cast<DebugPatch32 *>(P);
682       Offset = P32->Offset;
683       OldValueSize = 4;
684       ByteSequence = encodeLE(4, P32->Value);
685       break;
686     }
687     case DebugPatchKind::PatchValue64to32: {
688       DebugPatch64to32 *P64to32 = reinterpret_cast<DebugPatch64to32 *>(P);
689       Offset = P64to32->Offset;
690       OldValueSize = 8;
691       ByteSequence = encodeLE(4, P64to32->Value);
692       break;
693     }
694     case DebugPatchKind::PatchValueVariable: {
695       DebugPatchVariableSize *PV =
696           reinterpret_cast<DebugPatchVariableSize *>(P);
697       Offset = PV->Offset;
698       OldValueSize = PV->OldValueSize;
699       ByteSequence.clear();
700       raw_string_ostream OS(ByteSequence);
701       encodeULEB128(PV->Value, OS);
702       break;
703     }
704     case DebugPatchKind::PatchValue64: {
705       DebugPatch64 *P64 = reinterpret_cast<DebugPatch64 *>(P);
706       Offset = P64->Offset;
707       OldValueSize = 8;
708       ByteSequence = encodeLE(8, P64->Value);
709       break;
710     }
711     case DebugPatchKind::DWARFUnitOffsetBaseLabel: {
712       DWARFUnitOffsetBaseLabel *BaseLabel =
713           reinterpret_cast<DWARFUnitOffsetBaseLabel *>(P);
714       Offset = BaseLabel->Offset;
715       OldValueSize = 0;
716       ByteSequence.clear();
717       auto &Patch = LengthPatches.back();
718       // Length to copy between last patch entry and next compile unit.
719       uint32_t RemainingLength = Offset - StartOffset;
720       uint32_t NewCUOffset = NewBinaryContents.size() + RemainingLength;
721       DwarfUnitBaseOffset = NewCUOffset;
722       // Length of previous CU = This CU Offset - sizeof(length) - last CU
723       // Offset.
724       Patch.second = NewCUOffset - 4 - Patch.first;
725       LengthPatches.push_back({NewCUOffset, 0});
726       break;
727     }
728     case DebugPatchKind::NewDebugEntry: {
729       NewDebugEntry *NDE = reinterpret_cast<NewDebugEntry *>(P);
730       Offset = NDE->Offset;
731       OldValueSize = 0;
732       ByteSequence = NDE->Value;
733       break;
734     }
735     }
736 
737     assert((P->Kind == DebugPatchKind::NewDebugEntry ||
738             Offset + ByteSequence.size() <= BinaryContents.size()) &&
739            "Applied patch runs over binary size.");
740     uint32_t Length = Offset - StartOffset;
741     NewBinaryContents.append(BinaryContents.substr(StartOffset, Length).data(),
742                              Length);
743     NewBinaryContents.append(ByteSequence.data(), ByteSequence.size());
744     StartOffset = Offset + OldValueSize;
745   }
746   uint32_t Length = BinaryContents.size() - StartOffset;
747   NewBinaryContents.append(BinaryContents.substr(StartOffset, Length).data(),
748                            Length);
749   DebugPatches.clear();
750 
751   // Patching lengths of CUs
752   auto &Patch = LengthPatches.back();
753   Patch.second = NewBinaryContents.size() - 4 - Patch.first;
754   for (uint32_t J = 1, Size = LengthPatches.size(); J < Size; ++J) {
755     const auto &Patch = LengthPatches[J];
756     ByteSequence = encodeLE(4, Patch.second);
757     Offset = Patch.first;
758     for (uint64_t I = 0, Size = ByteSequence.size(); I < Size; ++I)
759       NewBinaryContents[Offset + I] = ByteSequence[I];
760   }
761 
762   return NewBinaryContents;
763 }
764 
765 void DebugStrWriter::create() {
766   StrBuffer = std::make_unique<DebugStrBufferVector>();
767   StrStream = std::make_unique<raw_svector_ostream>(*StrBuffer);
768 }
769 
770 void DebugStrWriter::initialize() {
771   auto StrSection = BC->DwCtx->getDWARFObj().getStrSection();
772   (*StrStream) << StrSection;
773 }
774 
775 uint32_t DebugStrWriter::addString(StringRef Str) {
776   std::lock_guard<std::mutex> Lock(WriterMutex);
777   if (StrBuffer->empty())
778     initialize();
779   auto Offset = StrBuffer->size();
780   (*StrStream) << Str;
781   StrStream->write_zeros(1);
782   return Offset;
783 }
784 
785 void DebugAbbrevWriter::addUnitAbbreviations(DWARFUnit &Unit) {
786   const DWARFAbbreviationDeclarationSet *Abbrevs = Unit.getAbbreviations();
787   if (!Abbrevs)
788     return;
789 
790   const PatchesTy &UnitPatches = Patches[&Unit];
791   const AbbrevEntryTy &AbbrevEntries = NewAbbrevEntries[&Unit];
792 
793   // We are duplicating abbrev sections, to handle the case where for one CU we
794   // modify it, but for another we don't.
795   auto UnitDataPtr = std::make_unique<AbbrevData>();
796   AbbrevData &UnitData = *UnitDataPtr.get();
797   UnitData.Buffer = std::make_unique<DebugBufferVector>();
798   UnitData.Stream = std::make_unique<raw_svector_ostream>(*UnitData.Buffer);
799 
800   raw_svector_ostream &OS = *UnitData.Stream.get();
801 
802   // Returns true if AbbrevData is re-used, false otherwise.
803   auto hashAndAddAbbrev = [&](StringRef AbbrevData) -> bool {
804     llvm::SHA1 Hasher;
805     Hasher.update(AbbrevData);
806     StringRef Key = Hasher.final();
807     auto Iter = AbbrevDataCache.find(Key);
808     if (Iter != AbbrevDataCache.end()) {
809       UnitsAbbrevData[&Unit] = Iter->second.get();
810       return true;
811     }
812     AbbrevDataCache[Key] = std::move(UnitDataPtr);
813     UnitsAbbrevData[&Unit] = &UnitData;
814     return false;
815   };
816   // Take a fast path if there are no patches to apply. Simply copy the original
817   // contents.
818   if (UnitPatches.empty() && AbbrevEntries.empty()) {
819     StringRef AbbrevSectionContents =
820         Unit.isDWOUnit() ? Unit.getContext().getDWARFObj().getAbbrevDWOSection()
821                          : Unit.getContext().getDWARFObj().getAbbrevSection();
822     StringRef AbbrevContents;
823 
824     const DWARFUnitIndex &CUIndex = Unit.getContext().getCUIndex();
825     if (!CUIndex.getRows().empty()) {
826       // Handle DWP section contribution.
827       const DWARFUnitIndex::Entry *DWOEntry =
828           CUIndex.getFromHash(*Unit.getDWOId());
829       if (!DWOEntry)
830         return;
831 
832       const DWARFUnitIndex::Entry::SectionContribution *DWOContrubution =
833           DWOEntry->getContribution(DWARFSectionKind::DW_SECT_ABBREV);
834       AbbrevContents = AbbrevSectionContents.substr(DWOContrubution->Offset,
835                                                     DWOContrubution->Length);
836     } else if (!Unit.isDWOUnit()) {
837       const uint64_t StartOffset = Unit.getAbbreviationsOffset();
838 
839       // We know where the unit's abbreviation set starts, but not where it ends
840       // as such data is not readily available. Hence, we have to build a sorted
841       // list of start addresses and find the next starting address to determine
842       // the set boundaries.
843       //
844       // FIXME: if we had a full access to DWARFDebugAbbrev::AbbrDeclSets
845       // we wouldn't have to build our own sorted list for the quick lookup.
846       if (AbbrevSetOffsets.empty()) {
847         for_each(
848             *Unit.getContext().getDebugAbbrev(),
849             [&](const std::pair<uint64_t, DWARFAbbreviationDeclarationSet> &P) {
850               AbbrevSetOffsets.push_back(P.first);
851             });
852         sort(AbbrevSetOffsets);
853       }
854       auto It = upper_bound(AbbrevSetOffsets, StartOffset);
855       const uint64_t EndOffset =
856           It == AbbrevSetOffsets.end() ? AbbrevSectionContents.size() : *It;
857       AbbrevContents = AbbrevSectionContents.slice(StartOffset, EndOffset);
858     } else {
859       // For DWO unit outside of DWP, we expect the entire section to hold
860       // abbreviations for this unit only.
861       AbbrevContents = AbbrevSectionContents;
862     }
863 
864     if (!hashAndAddAbbrev(AbbrevContents)) {
865       OS.reserveExtraSpace(AbbrevContents.size());
866       OS << AbbrevContents;
867     }
868     return;
869   }
870 
871   for (auto I = Abbrevs->begin(), E = Abbrevs->end(); I != E; ++I) {
872     const DWARFAbbreviationDeclaration &Abbrev = *I;
873     auto Patch = UnitPatches.find(&Abbrev);
874 
875     encodeULEB128(Abbrev.getCode(), OS);
876     encodeULEB128(Abbrev.getTag(), OS);
877     encodeULEB128(Abbrev.hasChildren(), OS);
878     for (const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec :
879          Abbrev.attributes()) {
880       if (Patch != UnitPatches.end()) {
881         bool Patched = false;
882         // Patches added later take a precedence over earlier ones.
883         for (auto I = Patch->second.rbegin(), E = Patch->second.rend(); I != E;
884              ++I) {
885           if (I->OldAttr != AttrSpec.Attr)
886             continue;
887 
888           encodeULEB128(I->NewAttr, OS);
889           encodeULEB128(I->NewAttrForm, OS);
890           Patched = true;
891           break;
892         }
893         if (Patched)
894           continue;
895       }
896 
897       encodeULEB128(AttrSpec.Attr, OS);
898       encodeULEB128(AttrSpec.Form, OS);
899       if (AttrSpec.isImplicitConst())
900         encodeSLEB128(AttrSpec.getImplicitConstValue(), OS);
901     }
902     const auto Entries = AbbrevEntries.find(&Abbrev);
903     // Adding new Abbrevs for inserted entries.
904     if (Entries != AbbrevEntries.end()) {
905       for (const AbbrevEntry &Entry : Entries->second) {
906         encodeULEB128(Entry.Attr, OS);
907         encodeULEB128(Entry.Form, OS);
908       }
909     }
910     encodeULEB128(0, OS);
911     encodeULEB128(0, OS);
912   }
913   encodeULEB128(0, OS);
914 
915   hashAndAddAbbrev(OS.str());
916 }
917 
918 std::unique_ptr<DebugBufferVector> DebugAbbrevWriter::finalize() {
919   // Used to create determinism for writing out abbrevs.
920   std::vector<AbbrevData *> Abbrevs;
921   if (DWOId) {
922     // We expect abbrev_offset to always be zero for DWO units as there
923     // should be one CU per DWO, and TUs should share the same abbreviation
924     // set with the CU.
925     // For DWP AbbreviationsOffset is an Abbrev contribution in the DWP file, so
926     // can be none zero. Thus we are skipping the check for DWP.
927     bool IsDWP = !Context.getCUIndex().getRows().empty();
928     if (!IsDWP) {
929       for (const std::unique_ptr<DWARFUnit> &Unit : Context.dwo_units()) {
930         if (Unit->getAbbreviationsOffset() != 0) {
931           errs() << "BOLT-ERROR: detected DWO unit with non-zero abbr_offset. "
932                     "Unable to update debug info.\n";
933           exit(1);
934         }
935       }
936     }
937 
938     DWARFUnit *Unit = Context.getDWOCompileUnitForHash(*DWOId);
939     // Issue abbreviations for the DWO CU only.
940     addUnitAbbreviations(*Unit);
941     AbbrevData *Abbrev = UnitsAbbrevData[Unit];
942     Abbrevs.push_back(Abbrev);
943   } else {
944     Abbrevs.reserve(Context.getNumCompileUnits() + Context.getNumTypeUnits());
945     std::unordered_set<AbbrevData *> ProcessedAbbrevs;
946     // Add abbreviations from compile and type non-DWO units.
947     for (const std::unique_ptr<DWARFUnit> &Unit : Context.normal_units()) {
948       addUnitAbbreviations(*Unit);
949       AbbrevData *Abbrev = UnitsAbbrevData[Unit.get()];
950       if (!ProcessedAbbrevs.insert(Abbrev).second)
951         continue;
952       Abbrevs.push_back(Abbrev);
953     }
954   }
955 
956   DebugBufferVector ReturnBuffer;
957   // Pre-calculate the total size of abbrev section.
958   uint64_t Size = 0;
959   for (const AbbrevData *UnitData : Abbrevs)
960     Size += UnitData->Buffer->size();
961 
962   ReturnBuffer.reserve(Size);
963 
964   uint64_t Pos = 0;
965   for (AbbrevData *UnitData : Abbrevs) {
966     ReturnBuffer.append(*UnitData->Buffer);
967     UnitData->Offset = Pos;
968     Pos += UnitData->Buffer->size();
969 
970     UnitData->Buffer.reset();
971     UnitData->Stream.reset();
972   }
973 
974   return std::make_unique<DebugBufferVector>(ReturnBuffer);
975 }
976 
977 static void emitDwarfSetLineAddrAbs(MCStreamer &OS,
978                                     MCDwarfLineTableParams Params,
979                                     int64_t LineDelta, uint64_t Address,
980                                     int PointerSize) {
981   // emit the sequence to set the address
982   OS.emitIntValue(dwarf::DW_LNS_extended_op, 1);
983   OS.emitULEB128IntValue(PointerSize + 1);
984   OS.emitIntValue(dwarf::DW_LNE_set_address, 1);
985   OS.emitIntValue(Address, PointerSize);
986 
987   // emit the sequence for the LineDelta (from 1) and a zero address delta.
988   MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
989 }
990 
991 static inline void emitBinaryDwarfLineTable(
992     MCStreamer *MCOS, MCDwarfLineTableParams Params,
993     const DWARFDebugLine::LineTable *Table,
994     const std::vector<DwarfLineTable::RowSequence> &InputSequences) {
995   if (InputSequences.empty())
996     return;
997 
998   constexpr uint64_t InvalidAddress = UINT64_MAX;
999   unsigned FileNum = 1;
1000   unsigned LastLine = 1;
1001   unsigned Column = 0;
1002   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1003   unsigned Isa = 0;
1004   unsigned Discriminator = 0;
1005   uint64_t LastAddress = InvalidAddress;
1006   uint64_t PrevEndOfSequence = InvalidAddress;
1007   const MCAsmInfo *AsmInfo = MCOS->getContext().getAsmInfo();
1008 
1009   auto emitEndOfSequence = [&](uint64_t Address) {
1010     MCDwarfLineAddr::Emit(MCOS, Params, INT64_MAX, Address - LastAddress);
1011     FileNum = 1;
1012     LastLine = 1;
1013     Column = 0;
1014     Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1015     Isa = 0;
1016     Discriminator = 0;
1017     LastAddress = InvalidAddress;
1018   };
1019 
1020   for (const DwarfLineTable::RowSequence &Sequence : InputSequences) {
1021     const uint64_t SequenceStart =
1022         Table->Rows[Sequence.FirstIndex].Address.Address;
1023 
1024     // Check if we need to mark the end of the sequence.
1025     if (PrevEndOfSequence != InvalidAddress && LastAddress != InvalidAddress &&
1026         PrevEndOfSequence != SequenceStart) {
1027       emitEndOfSequence(PrevEndOfSequence);
1028     }
1029 
1030     for (uint32_t RowIndex = Sequence.FirstIndex;
1031          RowIndex <= Sequence.LastIndex; ++RowIndex) {
1032       const DWARFDebugLine::Row &Row = Table->Rows[RowIndex];
1033       int64_t LineDelta = static_cast<int64_t>(Row.Line) - LastLine;
1034       const uint64_t Address = Row.Address.Address;
1035 
1036       if (FileNum != Row.File) {
1037         FileNum = Row.File;
1038         MCOS->emitInt8(dwarf::DW_LNS_set_file);
1039         MCOS->emitULEB128IntValue(FileNum);
1040       }
1041       if (Column != Row.Column) {
1042         Column = Row.Column;
1043         MCOS->emitInt8(dwarf::DW_LNS_set_column);
1044         MCOS->emitULEB128IntValue(Column);
1045       }
1046       if (Discriminator != Row.Discriminator &&
1047           MCOS->getContext().getDwarfVersion() >= 4) {
1048         Discriminator = Row.Discriminator;
1049         unsigned Size = getULEB128Size(Discriminator);
1050         MCOS->emitInt8(dwarf::DW_LNS_extended_op);
1051         MCOS->emitULEB128IntValue(Size + 1);
1052         MCOS->emitInt8(dwarf::DW_LNE_set_discriminator);
1053         MCOS->emitULEB128IntValue(Discriminator);
1054       }
1055       if (Isa != Row.Isa) {
1056         Isa = Row.Isa;
1057         MCOS->emitInt8(dwarf::DW_LNS_set_isa);
1058         MCOS->emitULEB128IntValue(Isa);
1059       }
1060       if (Row.IsStmt != Flags) {
1061         Flags = Row.IsStmt;
1062         MCOS->emitInt8(dwarf::DW_LNS_negate_stmt);
1063       }
1064       if (Row.BasicBlock)
1065         MCOS->emitInt8(dwarf::DW_LNS_set_basic_block);
1066       if (Row.PrologueEnd)
1067         MCOS->emitInt8(dwarf::DW_LNS_set_prologue_end);
1068       if (Row.EpilogueBegin)
1069         MCOS->emitInt8(dwarf::DW_LNS_set_epilogue_begin);
1070 
1071       // The end of the sequence is not normal in the middle of the input
1072       // sequence, but could happen, e.g. for assembly code.
1073       if (Row.EndSequence) {
1074         emitEndOfSequence(Address);
1075       } else {
1076         if (LastAddress == InvalidAddress)
1077           emitDwarfSetLineAddrAbs(*MCOS, Params, LineDelta, Address,
1078                                   AsmInfo->getCodePointerSize());
1079         else
1080           MCDwarfLineAddr::Emit(MCOS, Params, LineDelta, Address - LastAddress);
1081 
1082         LastAddress = Address;
1083         LastLine = Row.Line;
1084       }
1085 
1086       Discriminator = 0;
1087     }
1088     PrevEndOfSequence = Sequence.EndAddress;
1089   }
1090 
1091   // Finish with the end of the sequence.
1092   if (LastAddress != InvalidAddress)
1093     emitEndOfSequence(PrevEndOfSequence);
1094 }
1095 
1096 // This function is similar to the one from MCDwarfLineTable, except it handles
1097 // end-of-sequence entries differently by utilizing line entries with
1098 // DWARF2_FLAG_END_SEQUENCE flag.
1099 static inline void emitDwarfLineTable(
1100     MCStreamer *MCOS, MCSection *Section,
1101     const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
1102   unsigned FileNum = 1;
1103   unsigned LastLine = 1;
1104   unsigned Column = 0;
1105   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1106   unsigned Isa = 0;
1107   unsigned Discriminator = 0;
1108   MCSymbol *LastLabel = nullptr;
1109   const MCAsmInfo *AsmInfo = MCOS->getContext().getAsmInfo();
1110 
1111   // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
1112   for (const MCDwarfLineEntry &LineEntry : LineEntries) {
1113     if (LineEntry.getFlags() & DWARF2_FLAG_END_SEQUENCE) {
1114       MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, LineEntry.getLabel(),
1115                                      AsmInfo->getCodePointerSize());
1116       FileNum = 1;
1117       LastLine = 1;
1118       Column = 0;
1119       Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1120       Isa = 0;
1121       Discriminator = 0;
1122       LastLabel = nullptr;
1123       continue;
1124     }
1125 
1126     int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
1127 
1128     if (FileNum != LineEntry.getFileNum()) {
1129       FileNum = LineEntry.getFileNum();
1130       MCOS->emitInt8(dwarf::DW_LNS_set_file);
1131       MCOS->emitULEB128IntValue(FileNum);
1132     }
1133     if (Column != LineEntry.getColumn()) {
1134       Column = LineEntry.getColumn();
1135       MCOS->emitInt8(dwarf::DW_LNS_set_column);
1136       MCOS->emitULEB128IntValue(Column);
1137     }
1138     if (Discriminator != LineEntry.getDiscriminator() &&
1139         MCOS->getContext().getDwarfVersion() >= 4) {
1140       Discriminator = LineEntry.getDiscriminator();
1141       unsigned Size = getULEB128Size(Discriminator);
1142       MCOS->emitInt8(dwarf::DW_LNS_extended_op);
1143       MCOS->emitULEB128IntValue(Size + 1);
1144       MCOS->emitInt8(dwarf::DW_LNE_set_discriminator);
1145       MCOS->emitULEB128IntValue(Discriminator);
1146     }
1147     if (Isa != LineEntry.getIsa()) {
1148       Isa = LineEntry.getIsa();
1149       MCOS->emitInt8(dwarf::DW_LNS_set_isa);
1150       MCOS->emitULEB128IntValue(Isa);
1151     }
1152     if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
1153       Flags = LineEntry.getFlags();
1154       MCOS->emitInt8(dwarf::DW_LNS_negate_stmt);
1155     }
1156     if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
1157       MCOS->emitInt8(dwarf::DW_LNS_set_basic_block);
1158     if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
1159       MCOS->emitInt8(dwarf::DW_LNS_set_prologue_end);
1160     if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
1161       MCOS->emitInt8(dwarf::DW_LNS_set_epilogue_begin);
1162 
1163     MCSymbol *Label = LineEntry.getLabel();
1164 
1165     // At this point we want to emit/create the sequence to encode the delta
1166     // in line numbers and the increment of the address from the previous
1167     // Label and the current Label.
1168     MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
1169                                    AsmInfo->getCodePointerSize());
1170     Discriminator = 0;
1171     LastLine = LineEntry.getLine();
1172     LastLabel = Label;
1173   }
1174 
1175   assert(LastLabel == nullptr && "end of sequence expected");
1176 }
1177 
1178 void DwarfLineTable::emitCU(MCStreamer *MCOS, MCDwarfLineTableParams Params,
1179                             Optional<MCDwarfLineStr> &LineStr,
1180                             BinaryContext &BC) const {
1181   if (!RawData.empty()) {
1182     assert(MCLineSections.getMCLineEntries().empty() &&
1183            InputSequences.empty() &&
1184            "cannot combine raw data with new line entries");
1185     MCOS->emitLabel(getLabel());
1186     MCOS->emitBytes(RawData);
1187 
1188     // Emit fake relocation for RuntimeDyld to always allocate the section.
1189     //
1190     // FIXME: remove this once RuntimeDyld stops skipping allocatable sections
1191     //        without relocations.
1192     MCOS->emitRelocDirective(
1193         *MCConstantExpr::create(0, *BC.Ctx), "BFD_RELOC_NONE",
1194         MCSymbolRefExpr::create(getLabel(), *BC.Ctx), SMLoc(), *BC.STI);
1195 
1196     return;
1197   }
1198 
1199   MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second;
1200 
1201   // Put out the line tables.
1202   for (const auto &LineSec : MCLineSections.getMCLineEntries())
1203     emitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
1204 
1205   // Emit line tables for the original code.
1206   emitBinaryDwarfLineTable(MCOS, Params, InputTable, InputSequences);
1207 
1208   // This is the end of the section, so set the value of the symbol at the end
1209   // of this section (that was used in a previous expression).
1210   MCOS->emitLabel(LineEndSym);
1211 }
1212 
1213 void DwarfLineTable::emit(BinaryContext &BC, MCStreamer &Streamer) {
1214   MCAssembler &Assembler =
1215       static_cast<MCObjectStreamer *>(&Streamer)->getAssembler();
1216 
1217   MCDwarfLineTableParams Params = Assembler.getDWARFLinetableParams();
1218 
1219   auto &LineTables = BC.getDwarfLineTables();
1220 
1221   // Bail out early so we don't switch to the debug_line section needlessly and
1222   // in doing so create an unnecessary (if empty) section.
1223   if (LineTables.empty())
1224     return;
1225 
1226   // In a v5 non-split line table, put the strings in a separate section.
1227   Optional<MCDwarfLineStr> LineStr(None);
1228   if (BC.Ctx->getDwarfVersion() >= 5)
1229     LineStr = MCDwarfLineStr(*BC.Ctx);
1230 
1231   // Switch to the section where the table will be emitted into.
1232   Streamer.SwitchSection(BC.MOFI->getDwarfLineSection());
1233 
1234   // Handle the rest of the Compile Units.
1235   for (auto &CUIDTablePair : LineTables) {
1236     CUIDTablePair.second.emitCU(&Streamer, Params, LineStr, BC);
1237   }
1238 }
1239 
1240 } // namespace bolt
1241 } // namespace llvm
1242