1 //===- bolt/Rewrite/DWARFRewriter.cpp -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "bolt/Rewrite/DWARFRewriter.h"
10 #include "bolt/Core/BinaryContext.h"
11 #include "bolt/Core/BinaryFunction.h"
12 #include "bolt/Core/DebugData.h"
13 #include "bolt/Core/ParallelUtilities.h"
14 #include "bolt/Rewrite/RewriteInstance.h"
15 #include "bolt/Utils/Utils.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/BinaryFormat/Dwarf.h"
18 #include "llvm/DWP/DWP.h"
19 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
20 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmLayout.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCObjectWriter.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/Endian.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/ThreadPool.h"
33 #include "llvm/Support/ToolOutputFile.h"
34 #include <algorithm>
35 #include <cstdint>
36 #include <string>
37 #include <unordered_map>
38 
39 #undef  DEBUG_TYPE
40 #define DEBUG_TYPE "bolt"
41 
42 LLVM_ATTRIBUTE_UNUSED
43 static void printDie(const DWARFDie &DIE) {
44   DIDumpOptions DumpOpts;
45   DumpOpts.ShowForm = true;
46   DumpOpts.Verbose = true;
47   DumpOpts.ChildRecurseDepth = 0;
48   DumpOpts.ShowChildren = 0;
49   DIE.dump(dbgs(), 0, DumpOpts);
50 }
51 
52 struct AttrInfo {
53   DWARFFormValue V;
54   uint64_t Offset;
55   uint32_t Size; // Size of the attribute.
56 };
57 
58 /// Finds attributes FormValue and Offset.
59 ///
60 /// \param DIE die to look up in.
61 /// \param Index the attribute index to extract.
62 /// \return an optional AttrInfo with DWARFFormValue and Offset.
63 static Optional<AttrInfo>
64 findAttributeInfo(const DWARFDie DIE,
65                   const DWARFAbbreviationDeclaration *AbbrevDecl,
66                   uint32_t Index) {
67   const DWARFUnit &U = *DIE.getDwarfUnit();
68   uint64_t Offset =
69       AbbrevDecl->getAttributeOffsetFromIndex(Index, DIE.getOffset(), U);
70   Optional<DWARFFormValue> Value =
71       AbbrevDecl->getAttributeValueFromOffset(Index, Offset, U);
72   if (!Value)
73     return None;
74   // AttributeSpec
75   const DWARFAbbreviationDeclaration::AttributeSpec *AttrVal =
76       AbbrevDecl->attributes().begin() + Index;
77   uint32_t ValSize = 0;
78   Optional<int64_t> ValSizeOpt = AttrVal->getByteSize(U);
79   if (ValSizeOpt) {
80     ValSize = static_cast<uint32_t>(*ValSizeOpt);
81   } else {
82     DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor();
83     uint64_t NewOffset = Offset;
84     DWARFFormValue::skipValue(Value->getForm(), DebugInfoData, &NewOffset,
85                               U.getFormParams());
86     // This includes entire size of the entry, which might not be just the
87     // encoding part. For example for DW_AT_loc it will include expression
88     // location.
89     ValSize = NewOffset - Offset;
90   }
91 
92   return AttrInfo{*Value, Offset, ValSize};
93 }
94 
95 /// Finds attributes FormValue and Offset.
96 ///
97 /// \param DIE die to look up in.
98 /// \param Attr the attribute to extract.
99 /// \return an optional AttrInfo with DWARFFormValue and Offset.
100 static Optional<AttrInfo> findAttributeInfo(const DWARFDie DIE,
101                                             dwarf::Attribute Attr) {
102   if (!DIE.isValid())
103     return None;
104   const DWARFAbbreviationDeclaration *AbbrevDecl =
105       DIE.getAbbreviationDeclarationPtr();
106   if (!AbbrevDecl)
107     return None;
108   Optional<uint32_t> Index = AbbrevDecl->findAttributeIndex(Attr);
109   if (!Index)
110     return None;
111   return findAttributeInfo(DIE, AbbrevDecl, *Index);
112 }
113 
114 using namespace llvm;
115 using namespace llvm::support::endian;
116 using namespace object;
117 using namespace bolt;
118 
119 namespace opts {
120 
121 extern cl::OptionCategory BoltCategory;
122 extern cl::opt<unsigned> Verbosity;
123 extern cl::opt<std::string> OutputFilename;
124 
125 static cl::opt<bool>
126 KeepARanges("keep-aranges",
127   cl::desc("keep or generate .debug_aranges section if .gdb_index is written"),
128   cl::ZeroOrMore,
129   cl::Hidden,
130   cl::cat(BoltCategory));
131 
132 static cl::opt<bool>
133 DeterministicDebugInfo("deterministic-debuginfo",
134   cl::desc("disables parallel execution of tasks that may produce"
135            "nondeterministic debug info"),
136   cl::init(true),
137   cl::cat(BoltCategory));
138 
139 static cl::opt<std::string> DwarfOutputPath(
140     "dwarf-output-path",
141     cl::desc("Path to where .dwo files or dwp file will be written out to."),
142     cl::init(""), cl::cat(BoltCategory));
143 
144 static cl::opt<bool>
145     WriteDWP("write-dwp",
146              cl::desc("output a single dwarf package file (dwp) instead of "
147                       "multiple non-relocatable dwarf object files (dwo)."),
148              cl::init(false), cl::cat(BoltCategory));
149 
150 static cl::opt<bool>
151     DebugSkeletonCu("debug-skeleton-cu",
152                     cl::desc("prints out offsetrs for abbrev and debu_info of "
153                              "Skeleton CUs that get patched."),
154                     cl::ZeroOrMore, cl::Hidden, cl::init(false),
155                     cl::cat(BoltCategory));
156 } // namespace opts
157 
158 /// Returns DWO Name to be used. Handles case where user specifies output DWO
159 /// directory, and there are duplicate names. Assumes DWO ID is unique.
160 static std::string
161 getDWOName(llvm::DWARFUnit &CU,
162            std::unordered_map<std::string, uint32_t> *NameToIndexMap,
163            std::unordered_map<uint64_t, std::string> &DWOIdToName) {
164   llvm::Optional<uint64_t> DWOId = CU.getDWOId();
165   assert(DWOId && "DWO ID not found.");
166   (void)DWOId;
167   auto NameIter = DWOIdToName.find(*DWOId);
168   if (NameIter != DWOIdToName.end())
169     return NameIter->second;
170 
171   std::string DWOName = dwarf::toString(
172       CU.getUnitDIE().find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
173       "");
174   assert(!DWOName.empty() &&
175          "DW_AT_dwo_name/DW_AT_GNU_dwo_name does not exists.");
176   if (NameToIndexMap && !opts::DwarfOutputPath.empty()) {
177     auto Iter = NameToIndexMap->find(DWOName);
178     if (Iter == NameToIndexMap->end())
179       Iter = NameToIndexMap->insert({DWOName, 0}).first;
180     DWOName.append(std::to_string(Iter->second));
181     ++Iter->second;
182   }
183   DWOName.append(".dwo");
184   DWOIdToName[*DWOId] = DWOName;
185   return DWOName;
186 }
187 
188 static bool isHighPcFormEightBytes(dwarf::Form DwarfForm) {
189   return DwarfForm == dwarf::DW_FORM_addr || DwarfForm == dwarf::DW_FORM_data8;
190 }
191 
192 void DWARFRewriter::updateDebugInfo() {
193   ErrorOr<BinarySection &> DebugInfo = BC.getUniqueSectionByName(".debug_info");
194   if (!DebugInfo)
195     return;
196 
197   auto *DebugInfoPatcher =
198       static_cast<DebugInfoBinaryPatcher *>(DebugInfo->getPatcher());
199 
200   ARangesSectionWriter = std::make_unique<DebugARangesSectionWriter>();
201   RangesSectionWriter = std::make_unique<DebugRangesSectionWriter>();
202   StrWriter = std::make_unique<DebugStrWriter>(&BC);
203   AbbrevWriter = std::make_unique<DebugAbbrevWriter>(*BC.DwCtx);
204 
205   AddrWriter = std::make_unique<DebugAddrWriter>(&BC);
206   DebugLoclistWriter::setAddressWriter(AddrWriter.get());
207 
208   uint64_t NumCUs = BC.DwCtx->getNumCompileUnits();
209   if ((opts::NoThreads || opts::DeterministicDebugInfo) &&
210       BC.getNumDWOCUs() == 0) {
211     // Use single entry for efficiency when running single-threaded
212     NumCUs = 1;
213   }
214 
215   LocListWritersByCU.reserve(NumCUs);
216 
217   for (size_t CUIndex = 0; CUIndex < NumCUs; ++CUIndex)
218     LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>(&BC);
219 
220   // Unordered maps to handle name collision if output DWO directory is
221   // specified.
222   std::unordered_map<std::string, uint32_t> NameToIndexMap;
223   std::unordered_map<uint64_t, std::string> DWOIdToName;
224   std::mutex AccessMutex;
225 
226   auto updateDWONameCompDir = [&](DWARFUnit &Unit) -> void {
227     const DWARFDie &DIE = Unit.getUnitDIE();
228     Optional<AttrInfo> AttrInfoVal =
229         findAttributeInfo(DIE, dwarf::DW_AT_GNU_dwo_name);
230     (void)AttrInfoVal;
231     assert(AttrInfoVal && "Skeleton CU doesn't have dwo_name.");
232 
233     std::string ObjectName = "";
234 
235     {
236       std::lock_guard<std::mutex> Lock(AccessMutex);
237       ObjectName = getDWOName(Unit, &NameToIndexMap, DWOIdToName);
238     }
239 
240     uint32_t NewOffset = StrWriter->addString(ObjectName.c_str());
241     DebugInfoPatcher->addLE32Patch(AttrInfoVal->Offset, NewOffset,
242                                    AttrInfoVal->Size);
243 
244     AttrInfoVal = findAttributeInfo(DIE, dwarf::DW_AT_comp_dir);
245     (void)AttrInfoVal;
246     assert(AttrInfoVal && "DW_AT_comp_dir is not in Skeleton CU.");
247 
248     if (!opts::DwarfOutputPath.empty()) {
249       uint32_t NewOffset = StrWriter->addString(opts::DwarfOutputPath.c_str());
250       DebugInfoPatcher->addLE32Patch(AttrInfoVal->Offset, NewOffset,
251                                      AttrInfoVal->Size);
252     }
253   };
254 
255   auto processUnitDIE = [&](size_t CUIndex, DWARFUnit *Unit) {
256     // Check if the unit is a skeleton and we need special updates for it and
257     // its matching split/DWO CU.
258     Optional<DWARFUnit *> SplitCU;
259     Optional<uint64_t> RangesBase;
260     llvm::Optional<uint64_t> DWOId = Unit->getDWOId();
261     if (DWOId)
262       SplitCU = BC.getDWOCU(*DWOId);
263 
264     DebugLocWriter *DebugLocWriter = nullptr;
265     // Skipping CUs that failed to load.
266     if (SplitCU) {
267       updateDWONameCompDir(*Unit);
268 
269       // Assuming there is unique DWOID per binary. i.e. two or more CUs don't
270       // have same DWO ID.
271       assert(LocListWritersByCU.count(*DWOId) == 0 &&
272              "LocList writer for DWO unit already exists.");
273       {
274         std::lock_guard<std::mutex> Lock(AccessMutex);
275         DebugLocWriter =
276             LocListWritersByCU
277                 .insert(
278                     {*DWOId, std::make_unique<DebugLoclistWriter>(&BC, *DWOId)})
279                 .first->second.get();
280       }
281       DebugInfoBinaryPatcher *DwoDebugInfoPatcher =
282           llvm::cast<DebugInfoBinaryPatcher>(
283               getBinaryDWODebugInfoPatcher(*DWOId));
284       RangesBase = RangesSectionWriter->getSectionOffset();
285       DWARFContext *DWOCtx = BC.getDWOContext();
286       // Setting this CU offset with DWP to normalize DIE offsets to uint32_t
287       if (DWOCtx && !DWOCtx->getCUIndex().getRows().empty())
288         DwoDebugInfoPatcher->setDWPOffset((*SplitCU)->getOffset());
289       DwoDebugInfoPatcher->setRangeBase(*RangesBase);
290       DwoDebugInfoPatcher->addUnitBaseOffsetLabel((*SplitCU)->getOffset());
291       DebugAbbrevWriter *DWOAbbrevWriter =
292           createBinaryDWOAbbrevWriter((*SplitCU)->getContext(), *DWOId);
293       updateUnitDebugInfo(*(*SplitCU), *DwoDebugInfoPatcher, *DWOAbbrevWriter,
294                           *DebugLocWriter);
295       DwoDebugInfoPatcher->clearDestinationLabels();
296       if (!DwoDebugInfoPatcher->getWasRangBasedUsed())
297         RangesBase = None;
298     }
299 
300     {
301       std::lock_guard<std::mutex> Lock(AccessMutex);
302       DebugLocWriter = LocListWritersByCU[CUIndex].get();
303     }
304     DebugInfoPatcher->addUnitBaseOffsetLabel(Unit->getOffset());
305     updateUnitDebugInfo(*Unit, *DebugInfoPatcher, *AbbrevWriter,
306                         *DebugLocWriter, RangesBase);
307   };
308 
309   if (opts::NoThreads || opts::DeterministicDebugInfo) {
310     for (std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units())
311       processUnitDIE(0, CU.get());
312   } else {
313     // Update unit debug info in parallel
314     ThreadPool &ThreadPool = ParallelUtilities::getThreadPool();
315     size_t CUIndex = 0;
316     for (std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units()) {
317       ThreadPool.async(processUnitDIE, CUIndex, CU.get());
318       CUIndex++;
319     }
320     ThreadPool.wait();
321   }
322 
323   DebugInfoPatcher->clearDestinationLabels();
324   CUOffsetMap OffsetMap = finalizeDebugSections(*DebugInfoPatcher);
325 
326   if (opts::WriteDWP)
327     writeDWP(DWOIdToName);
328   else
329     writeDWOFiles(DWOIdToName);
330 
331   updateGdbIndexSection(OffsetMap);
332 }
333 
334 void DWARFRewriter::updateUnitDebugInfo(
335     DWARFUnit &Unit, DebugInfoBinaryPatcher &DebugInfoPatcher,
336     DebugAbbrevWriter &AbbrevWriter, DebugLocWriter &DebugLocWriter,
337     Optional<uint64_t> RangesBase) {
338   // Cache debug ranges so that the offset for identical ranges could be reused.
339   std::map<DebugAddressRangesVector, uint64_t> CachedRanges;
340 
341   uint64_t DIEOffset = Unit.getOffset() + Unit.getHeaderSize();
342   uint64_t NextCUOffset = Unit.getNextUnitOffset();
343   DWARFDebugInfoEntry Die;
344   DWARFDataExtractor DebugInfoData = Unit.getDebugInfoExtractor();
345   uint32_t Depth = 0;
346 
347   while (
348       DIEOffset < NextCUOffset &&
349       Die.extractFast(Unit, &DIEOffset, DebugInfoData, NextCUOffset, Depth)) {
350     if (const DWARFAbbreviationDeclaration *AbbrDecl =
351             Die.getAbbreviationDeclarationPtr()) {
352       if (AbbrDecl->hasChildren())
353         ++Depth;
354     } else {
355       // NULL entry.
356       if (Depth > 0)
357         --Depth;
358       if (Depth == 0)
359         break;
360     }
361 
362     DWARFDie DIE(&Unit, &Die);
363 
364     switch (DIE.getTag()) {
365     case dwarf::DW_TAG_compile_unit: {
366       auto ModuleRangesOrError = DIE.getAddressRanges();
367       if (!ModuleRangesOrError) {
368         consumeError(ModuleRangesOrError.takeError());
369         break;
370       }
371       DWARFAddressRangesVector &ModuleRanges = *ModuleRangesOrError;
372       DebugAddressRangesVector OutputRanges =
373           BC.translateModuleAddressRanges(ModuleRanges);
374       const uint64_t RangesSectionOffset =
375           RangesSectionWriter->addRanges(OutputRanges);
376       if (!Unit.isDWOUnit())
377         ARangesSectionWriter->addCURanges(Unit.getOffset(),
378                                           std::move(OutputRanges));
379       updateDWARFObjectAddressRanges(DIE, RangesSectionOffset, DebugInfoPatcher,
380                                      AbbrevWriter, RangesBase);
381       break;
382     }
383     case dwarf::DW_TAG_subprogram: {
384       // Get function address either from ranges or [LowPC, HighPC) pair.
385       uint64_t Address;
386       uint64_t SectionIndex, HighPC;
387       if (!DIE.getLowAndHighPC(Address, HighPC, SectionIndex)) {
388         Expected<DWARFAddressRangesVector> RangesOrError =
389             DIE.getAddressRanges();
390         if (!RangesOrError) {
391           consumeError(RangesOrError.takeError());
392           break;
393         }
394         DWARFAddressRangesVector Ranges = *RangesOrError;
395         // Not a function definition.
396         if (Ranges.empty())
397           break;
398 
399         Address = Ranges.front().LowPC;
400       }
401 
402       // Clear cached ranges as the new function will have its own set.
403       CachedRanges.clear();
404 
405       DebugAddressRangesVector FunctionRanges;
406       if (const BinaryFunction *Function =
407               BC.getBinaryFunctionAtAddress(Address))
408         FunctionRanges = Function->getOutputAddressRanges();
409 
410       if (FunctionRanges.empty())
411         FunctionRanges.push_back({0, 0});
412 
413       updateDWARFObjectAddressRanges(
414           DIE, RangesSectionWriter->addRanges(FunctionRanges), DebugInfoPatcher,
415           AbbrevWriter);
416 
417       break;
418     }
419     case dwarf::DW_TAG_lexical_block:
420     case dwarf::DW_TAG_inlined_subroutine:
421     case dwarf::DW_TAG_try_block:
422     case dwarf::DW_TAG_catch_block: {
423       uint64_t RangesSectionOffset =
424           RangesSectionWriter->getEmptyRangesOffset();
425       Expected<DWARFAddressRangesVector> RangesOrError = DIE.getAddressRanges();
426       const BinaryFunction *Function =
427           RangesOrError && !RangesOrError->empty()
428               ? BC.getBinaryFunctionContainingAddress(
429                     RangesOrError->front().LowPC)
430               : nullptr;
431       if (Function) {
432         DebugAddressRangesVector OutputRanges =
433             Function->translateInputToOutputRanges(*RangesOrError);
434         LLVM_DEBUG(if (OutputRanges.empty() != RangesOrError->empty()) {
435           dbgs() << "BOLT-DEBUG: problem with DIE at 0x"
436                  << Twine::utohexstr(DIE.getOffset()) << " in CU at 0x"
437                  << Twine::utohexstr(Unit.getOffset()) << '\n';
438         });
439         RangesSectionOffset = RangesSectionWriter->addRanges(
440             std::move(OutputRanges), CachedRanges);
441       } else if (!RangesOrError) {
442         consumeError(RangesOrError.takeError());
443       }
444       updateDWARFObjectAddressRanges(DIE, RangesSectionOffset, DebugInfoPatcher,
445                                      AbbrevWriter);
446       break;
447     }
448     default: {
449       // Handle any tag that can have DW_AT_location attribute.
450       DWARFFormValue Value;
451       uint64_t AttrOffset;
452       if (Optional<AttrInfo> AttrVal =
453               findAttributeInfo(DIE, dwarf::DW_AT_location)) {
454         AttrOffset = AttrVal->Offset;
455         Value = AttrVal->V;
456         if (Value.isFormClass(DWARFFormValue::FC_Constant) ||
457             Value.isFormClass(DWARFFormValue::FC_SectionOffset)) {
458           uint64_t Offset = Value.isFormClass(DWARFFormValue::FC_Constant)
459                                 ? Value.getAsUnsignedConstant().getValue()
460                                 : Value.getAsSectionOffset().getValue();
461           DebugLocationsVector InputLL;
462 
463           Optional<object::SectionedAddress> SectionAddress =
464               Unit.getBaseAddress();
465           uint64_t BaseAddress = 0;
466           if (SectionAddress)
467             BaseAddress = SectionAddress->Address;
468 
469           Error E = Unit.getLocationTable().visitLocationList(
470               &Offset, [&](const DWARFLocationEntry &Entry) {
471                 switch (Entry.Kind) {
472                 default:
473                   llvm_unreachable("Unsupported DWARFLocationEntry Kind.");
474                 case dwarf::DW_LLE_end_of_list:
475                   return false;
476                 case dwarf::DW_LLE_base_address:
477                   assert(Entry.SectionIndex == SectionedAddress::UndefSection &&
478                          "absolute address expected");
479                   BaseAddress = Entry.Value0;
480                   break;
481                 case dwarf::DW_LLE_offset_pair:
482                   assert(
483                       (Entry.SectionIndex == SectionedAddress::UndefSection &&
484                        !Unit.isDWOUnit()) &&
485                       "absolute address expected");
486                   InputLL.emplace_back(DebugLocationEntry{
487                       BaseAddress + Entry.Value0, BaseAddress + Entry.Value1,
488                       Entry.Loc});
489                   break;
490                 case dwarf::DW_LLE_startx_length:
491                   assert(Unit.isDWOUnit() &&
492                          "None DWO Unit with DW_LLE_startx_length encoding.");
493                   Optional<object::SectionedAddress> EntryAddress =
494                       Unit.getAddrOffsetSectionItem(Entry.Value0);
495                   assert(EntryAddress && "Address does not exist.");
496                   InputLL.emplace_back(DebugLocationEntry{
497                       EntryAddress->Address,
498                       EntryAddress->Address + Entry.Value1, Entry.Loc});
499                   break;
500                 }
501                 return true;
502               });
503 
504           if (E || InputLL.empty()) {
505             errs() << "BOLT-WARNING: empty location list detected at 0x"
506                    << Twine::utohexstr(Offset) << " for DIE at 0x"
507                    << Twine::utohexstr(DIE.getOffset()) << " in CU at 0x"
508                    << Twine::utohexstr(Unit.getOffset()) << '\n';
509           } else {
510             const uint64_t Address = InputLL.front().LowPC;
511             if (const BinaryFunction *Function =
512                     BC.getBinaryFunctionContainingAddress(Address)) {
513               DebugLocationsVector OutputLL =
514                   Function->translateInputToOutputLocationList(InputLL);
515               LLVM_DEBUG(if (OutputLL.empty()) {
516                 dbgs() << "BOLT-DEBUG: location list translated to an empty "
517                           "one at 0x"
518                        << Twine::utohexstr(DIE.getOffset()) << " in CU at 0x"
519                        << Twine::utohexstr(Unit.getOffset()) << '\n';
520               });
521               DebugLocWriter.addList(AttrOffset, std::move(OutputLL));
522             }
523           }
524         } else {
525           assert((Value.isFormClass(DWARFFormValue::FC_Exprloc) ||
526                   Value.isFormClass(DWARFFormValue::FC_Block)) &&
527                  "unexpected DW_AT_location form");
528           if (Unit.isDWOUnit()) {
529             ArrayRef<uint8_t> Expr = *Value.getAsBlock();
530             DataExtractor Data(
531                 StringRef((const char *)Expr.data(), Expr.size()),
532                 Unit.getContext().isLittleEndian(), 0);
533             DWARFExpression LocExpr(Data, Unit.getAddressByteSize(),
534                                     Unit.getFormParams().Format);
535             for (auto &Expr : LocExpr) {
536               if (Expr.getCode() != dwarf::DW_OP_GNU_addr_index)
537                 continue;
538               uint64_t Index = Expr.getRawOperand(0);
539               Optional<object::SectionedAddress> EntryAddress =
540                   Unit.getAddrOffsetSectionItem(Index);
541               assert(EntryAddress && "Address is not found.");
542               assert(Index <= std::numeric_limits<uint32_t>::max() &&
543                      "Invalid Operand Index.");
544               AddrWriter->addIndexAddress(EntryAddress->Address,
545                                           static_cast<uint32_t>(Index),
546                                           *Unit.getDWOId());
547             }
548           }
549         }
550       } else if (Optional<AttrInfo> AttrVal =
551                      findAttributeInfo(DIE, dwarf::DW_AT_low_pc)) {
552         AttrOffset = AttrVal->Offset;
553         Value = AttrVal->V;
554         const Optional<uint64_t> Result = Value.getAsAddress();
555         if (Result.hasValue()) {
556           const uint64_t Address = Result.getValue();
557           uint64_t NewAddress = 0;
558           if (const BinaryFunction *Function =
559                   BC.getBinaryFunctionContainingAddress(Address)) {
560             NewAddress = Function->translateInputToOutputAddress(Address);
561             LLVM_DEBUG(dbgs()
562                        << "BOLT-DEBUG: Fixing low_pc 0x"
563                        << Twine::utohexstr(Address) << " for DIE with tag "
564                        << DIE.getTag() << " to 0x"
565                        << Twine::utohexstr(NewAddress) << '\n');
566           }
567 
568           dwarf::Form Form = Value.getForm();
569           assert(Form != dwarf::DW_FORM_LLVM_addrx_offset &&
570                  "DW_FORM_LLVM_addrx_offset is not supported");
571           std::lock_guard<std::mutex> Lock(DebugInfoPatcherMutex);
572           if (Form == dwarf::DW_FORM_GNU_addr_index) {
573             assert(Unit.isDWOUnit() &&
574                    "DW_FORM_GNU_addr_index in Non DWO unit.");
575             uint64_t Index = Value.getRawUValue();
576             // If there is no new address, storing old address.
577             // Re-using Index to make implementation easier.
578             // DW_FORM_GNU_addr_index is variable lenght encoding so we either
579             // have to create indices of same sizes, or use same index.
580             AddrWriter->addIndexAddress(NewAddress ? NewAddress : Address,
581                                         Index, *Unit.getDWOId());
582           } else {
583             DebugInfoPatcher.addLE64Patch(AttrOffset, NewAddress);
584           }
585         } else if (opts::Verbosity >= 1) {
586           errs() << "BOLT-WARNING: unexpected form value for attribute at 0x"
587                  << Twine::utohexstr(AttrOffset);
588         }
589       }
590     }
591     }
592 
593     // Handling references.
594     assert(DIE.isValid() && "Invalid DIE.");
595     const DWARFAbbreviationDeclaration *AbbrevDecl =
596         DIE.getAbbreviationDeclarationPtr();
597     if (!AbbrevDecl)
598       continue;
599     uint32_t Index = 0;
600     for (const DWARFAbbreviationDeclaration::AttributeSpec &Decl :
601          AbbrevDecl->attributes()) {
602       switch (Decl.Form) {
603       default:
604         break;
605       case dwarf::DW_FORM_ref1:
606       case dwarf::DW_FORM_ref2:
607       case dwarf::DW_FORM_ref4:
608       case dwarf::DW_FORM_ref8:
609       case dwarf::DW_FORM_ref_udata:
610       case dwarf::DW_FORM_ref_addr: {
611         Optional<AttrInfo> AttrVal = findAttributeInfo(DIE, AbbrevDecl, Index);
612         uint32_t DestinationAddress =
613             AttrVal->V.getRawUValue() +
614             (Decl.Form == dwarf::DW_FORM_ref_addr ? 0 : Unit.getOffset());
615         DebugInfoPatcher.addReferenceToPatch(
616             AttrVal->Offset, DestinationAddress, AttrVal->Size, Decl.Form);
617         // We can have only one reference, and it can be backward one.
618         DebugInfoPatcher.addDestinationReferenceLabel(DestinationAddress);
619         break;
620       }
621       }
622       ++Index;
623     }
624   }
625   if (DIEOffset > NextCUOffset)
626     errs() << "BOLT-WARNING: corrupt DWARF detected at 0x"
627            << Twine::utohexstr(Unit.getOffset()) << '\n';
628 }
629 
630 void DWARFRewriter::updateDWARFObjectAddressRanges(
631     const DWARFDie DIE, uint64_t DebugRangesOffset,
632     SimpleBinaryPatcher &DebugInfoPatcher, DebugAbbrevWriter &AbbrevWriter,
633     Optional<uint64_t> RangesBase) {
634 
635   // Some objects don't have an associated DIE and cannot be updated (such as
636   // compiler-generated functions).
637   if (!DIE)
638     return;
639 
640   const DWARFAbbreviationDeclaration *AbbreviationDecl =
641       DIE.getAbbreviationDeclarationPtr();
642   if (!AbbreviationDecl) {
643     if (opts::Verbosity >= 1)
644       errs() << "BOLT-WARNING: object's DIE doesn't have an abbreviation: "
645              << "skipping update. DIE at offset 0x"
646              << Twine::utohexstr(DIE.getOffset()) << '\n';
647     return;
648   }
649 
650   if (RangesBase) {
651     // If DW_AT_GNU_ranges_base is present, update it. No further modifications
652     // are needed for ranges base.
653     Optional<AttrInfo> RangesBaseAttrInfo =
654         findAttributeInfo(DIE, dwarf::DW_AT_GNU_ranges_base);
655     if (RangesBaseAttrInfo) {
656       DebugInfoPatcher.addLE32Patch(RangesBaseAttrInfo->Offset,
657                                     static_cast<uint32_t>(*RangesBase),
658                                     RangesBaseAttrInfo->Size);
659       RangesBase = None;
660     }
661   }
662 
663   Optional<AttrInfo> LowPCAttrInfo =
664       findAttributeInfo(DIE, dwarf::DW_AT_low_pc);
665   if (AbbreviationDecl->findAttributeIndex(dwarf::DW_AT_ranges)) {
666     // Case 1: The object was already non-contiguous and had DW_AT_ranges.
667     // In this case we simply need to update the value of DW_AT_ranges
668     // and introduce DW_AT_GNU_ranges_base if required.
669     Optional<AttrInfo> AttrVal = findAttributeInfo(DIE, dwarf::DW_AT_ranges);
670     std::lock_guard<std::mutex> Lock(DebugInfoPatcherMutex);
671     DebugInfoPatcher.addLE32Patch(
672         AttrVal->Offset, DebugRangesOffset - DebugInfoPatcher.getRangeBase(),
673         AttrVal->Size);
674 
675     if (!RangesBase) {
676       if (LowPCAttrInfo &&
677           LowPCAttrInfo->V.getForm() != dwarf::DW_FORM_GNU_addr_index &&
678           LowPCAttrInfo->V.getForm() != dwarf::DW_FORM_addrx)
679         DebugInfoPatcher.addLE64Patch(LowPCAttrInfo->Offset, 0);
680       return;
681     }
682 
683     // Convert DW_AT_low_pc into DW_AT_GNU_ranges_base.
684     if (!LowPCAttrInfo) {
685       errs() << "BOLT-ERROR: skeleton CU at 0x"
686              << Twine::utohexstr(DIE.getOffset())
687              << " does not have DW_AT_GNU_ranges_base or DW_AT_low_pc to"
688                 " convert to update ranges base\n";
689       return;
690     }
691 
692     AbbrevWriter.addAttributePatch(
693         *DIE.getDwarfUnit(), AbbreviationDecl, dwarf::DW_AT_low_pc,
694         dwarf::DW_AT_GNU_ranges_base, dwarf::DW_FORM_indirect);
695     DebugInfoPatcher.addUDataPatch(LowPCAttrInfo->Offset, dwarf::DW_FORM_udata,
696                                    1);
697     DebugInfoPatcher.addUDataPatch(LowPCAttrInfo->Offset + 1, *RangesBase, 7);
698 
699     return;
700   }
701 
702   // Case 2: The object has both DW_AT_low_pc and DW_AT_high_pc emitted back
703   // to back. Replace with new attributes and patch the DIE.
704   Optional<AttrInfo> HighPCAttrInfo =
705       findAttributeInfo(DIE, dwarf::DW_AT_high_pc);
706   if (LowPCAttrInfo && HighPCAttrInfo) {
707     convertToRangesPatchAbbrev(*DIE.getDwarfUnit(), AbbreviationDecl,
708                                AbbrevWriter, RangesBase);
709     convertToRangesPatchDebugInfo(DIE, DebugRangesOffset, DebugInfoPatcher,
710                                   RangesBase);
711   } else {
712     if (opts::Verbosity >= 1)
713       errs() << "BOLT-ERROR: cannot update ranges for DIE at offset 0x"
714              << Twine::utohexstr(DIE.getOffset()) << '\n';
715   }
716 }
717 
718 void DWARFRewriter::updateLineTableOffsets(const MCAsmLayout &Layout) {
719   ErrorOr<BinarySection &> DbgInfoSection =
720       BC.getUniqueSectionByName(".debug_info");
721   ErrorOr<BinarySection &> TypeInfoSection =
722       BC.getUniqueSectionByName(".debug_types");
723   assert(((BC.DwCtx->getNumTypeUnits() > 0 && TypeInfoSection) ||
724           BC.DwCtx->getNumTypeUnits() == 0) &&
725          "Was not able to retrieve Debug Types section.");
726 
727   // We will be re-writing .debug_info so relocation mechanism doesn't work for
728   // Debug Info Patcher.
729   DebugInfoBinaryPatcher *DebugInfoPatcher = nullptr;
730   if (BC.DwCtx->getNumCompileUnits()) {
731     DbgInfoSection->registerPatcher(std::make_unique<DebugInfoBinaryPatcher>());
732     DebugInfoPatcher =
733         static_cast<DebugInfoBinaryPatcher *>(DbgInfoSection->getPatcher());
734   }
735 
736   // There is no direct connection between CU and TU, but same offsets,
737   // encoded in DW_AT_stmt_list, into .debug_line get modified.
738   // We take advantage of that to map original CU line table offsets to new
739   // ones.
740   std::unordered_map<uint64_t, uint64_t> DebugLineOffsetMap;
741 
742   auto GetStatementListValue = [](DWARFUnit *Unit) {
743     Optional<DWARFFormValue> StmtList =
744         Unit->getUnitDIE().find(dwarf::DW_AT_stmt_list);
745     Optional<uint64_t> Offset = dwarf::toSectionOffset(StmtList);
746     assert(Offset && "Was not able to retreive value of DW_AT_stmt_list.");
747     return *Offset;
748   };
749 
750   const uint64_t Reloc32Type = BC.isAArch64()
751                                    ? static_cast<uint64_t>(ELF::R_AARCH64_ABS32)
752                                    : static_cast<uint64_t>(ELF::R_X86_64_32);
753 
754   for (const std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units()) {
755     const unsigned CUID = CU->getOffset();
756     MCSymbol *Label = BC.getDwarfLineTable(CUID).getLabel();
757     if (!Label)
758       continue;
759 
760     Optional<AttrInfo> AttrVal =
761         findAttributeInfo(CU.get()->getUnitDIE(), dwarf::DW_AT_stmt_list);
762     if (!AttrVal)
763       continue;
764 
765     const uint64_t AttributeOffset = AttrVal->Offset;
766     const uint64_t LineTableOffset = Layout.getSymbolOffset(*Label);
767     DebugLineOffsetMap[GetStatementListValue(CU.get())] = LineTableOffset;
768     assert(DbgInfoSection && ".debug_info section must exist");
769     DebugInfoPatcher->addLE32Patch(AttributeOffset, LineTableOffset);
770   }
771 
772   for (const std::unique_ptr<DWARFUnit> &TU : BC.DwCtx->types_section_units()) {
773     DWARFUnit *Unit = TU.get();
774     Optional<AttrInfo> AttrVal =
775         findAttributeInfo(TU.get()->getUnitDIE(), dwarf::DW_AT_stmt_list);
776     if (!AttrVal)
777       continue;
778     const uint64_t AttributeOffset = AttrVal->Offset;
779     auto Iter = DebugLineOffsetMap.find(GetStatementListValue(Unit));
780     assert(Iter != DebugLineOffsetMap.end() &&
781            "Type Unit Updated Line Number Entry does not exist.");
782     TypeInfoSection->addRelocation(AttributeOffset, nullptr, Reloc32Type,
783                                    Iter->second, 0, /*Pending=*/true);
784   }
785 
786   // Set .debug_info as finalized so it won't be skipped over when
787   // we process sections while writing out the new binary. This ensures
788   // that the pending relocations will be processed and not ignored.
789   if (DbgInfoSection)
790     DbgInfoSection->setIsFinalized();
791 
792   if (TypeInfoSection)
793     TypeInfoSection->setIsFinalized();
794 }
795 
796 CUOffsetMap
797 DWARFRewriter::finalizeDebugSections(DebugInfoBinaryPatcher &DebugInfoPatcher) {
798   if (StrWriter->isInitialized()) {
799     RewriteInstance::addToDebugSectionsToOverwrite(".debug_str");
800     std::unique_ptr<DebugStrBufferVector> DebugStrSectionContents =
801         StrWriter->finalize();
802     BC.registerOrUpdateNoteSection(".debug_str",
803                                    copyByteArray(*DebugStrSectionContents),
804                                    DebugStrSectionContents->size());
805   }
806 
807   std::unique_ptr<DebugBufferVector> RangesSectionContents =
808       RangesSectionWriter->finalize();
809   BC.registerOrUpdateNoteSection(".debug_ranges",
810                                  copyByteArray(*RangesSectionContents),
811                                  RangesSectionContents->size());
812 
813   std::unique_ptr<DebugBufferVector> LocationListSectionContents =
814       makeFinalLocListsSection(DebugInfoPatcher);
815   BC.registerOrUpdateNoteSection(".debug_loc",
816                                  copyByteArray(*LocationListSectionContents),
817                                  LocationListSectionContents->size());
818 
819   // AddrWriter should be finalized after debug_loc since more addresses can be
820   // added there.
821   if (AddrWriter->isInitialized()) {
822     AddressSectionBuffer AddressSectionContents = AddrWriter->finalize();
823     BC.registerOrUpdateNoteSection(".debug_addr",
824                                    copyByteArray(AddressSectionContents),
825                                    AddressSectionContents.size());
826     for (auto &CU : BC.DwCtx->compile_units()) {
827       DWARFDie DIE = CU->getUnitDIE();
828       if (Optional<AttrInfo> AttrVal =
829               findAttributeInfo(DIE, dwarf::DW_AT_GNU_addr_base)) {
830         uint64_t Offset = AddrWriter->getOffset(*CU->getDWOId());
831         DebugInfoPatcher.addLE32Patch(
832             AttrVal->Offset, static_cast<int32_t>(Offset), AttrVal->Size);
833       }
834     }
835   }
836 
837   std::unique_ptr<DebugBufferVector> AbbrevSectionContents =
838       AbbrevWriter->finalize();
839   BC.registerOrUpdateNoteSection(".debug_abbrev",
840                                  copyByteArray(*AbbrevSectionContents),
841                                  AbbrevSectionContents->size());
842 
843   // Update abbreviation offsets for CUs/TUs if they were changed.
844   SimpleBinaryPatcher *DebugTypesPatcher = nullptr;
845   for (auto &Unit : BC.DwCtx->normal_units()) {
846     const uint64_t NewAbbrevOffset =
847         AbbrevWriter->getAbbreviationsOffsetForUnit(*Unit);
848     if (Unit->getAbbreviationsOffset() == NewAbbrevOffset)
849       continue;
850 
851     // DWARFv4
852     // unit_length - 4 bytes
853     // version - 2 bytes
854     // So + 6 to patch debug_abbrev_offset
855     constexpr uint64_t AbbrevFieldOffset = 6;
856     if (!Unit->isTypeUnit()) {
857       DebugInfoPatcher.addLE32Patch(Unit->getOffset() + AbbrevFieldOffset,
858                                     static_cast<uint32_t>(NewAbbrevOffset));
859       continue;
860     }
861 
862     if (!DebugTypesPatcher) {
863       ErrorOr<BinarySection &> DebugTypes =
864           BC.getUniqueSectionByName(".debug_types");
865       DebugTypes->registerPatcher(std::make_unique<SimpleBinaryPatcher>());
866       DebugTypesPatcher =
867           static_cast<SimpleBinaryPatcher *>(DebugTypes->getPatcher());
868     }
869     DebugTypesPatcher->addLE32Patch(Unit->getOffset() + AbbrevFieldOffset,
870                                     static_cast<uint32_t>(NewAbbrevOffset));
871   }
872 
873   // No more creating new DebugInfoPatches.
874   CUOffsetMap CUMap =
875       DebugInfoPatcher.computeNewOffsets(*BC.DwCtx.get(), false);
876 
877   // Skip .debug_aranges if we are re-generating .gdb_index.
878   if (opts::KeepARanges || !BC.getGdbIndexSection()) {
879     SmallVector<char, 16> ARangesBuffer;
880     raw_svector_ostream OS(ARangesBuffer);
881 
882     auto MAB = std::unique_ptr<MCAsmBackend>(
883         BC.TheTarget->createMCAsmBackend(*BC.STI, *BC.MRI, MCTargetOptions()));
884 
885     ARangesSectionWriter->writeARangesSection(OS, CUMap);
886     const StringRef &ARangesContents = OS.str();
887 
888     BC.registerOrUpdateNoteSection(".debug_aranges",
889                                    copyByteArray(ARangesContents),
890                                    ARangesContents.size());
891   }
892   return CUMap;
893 }
894 
895 // Creates all the data structures necessary for creating MCStreamer.
896 // They are passed by reference because they need to be kept around.
897 // Also creates known debug sections. These are sections handled by
898 // handleDebugDataPatching.
899 using KnownSectionsEntry = std::pair<MCSection *, DWARFSectionKind>;
900 namespace {
901 
902 std::unique_ptr<BinaryContext>
903 createDwarfOnlyBC(const object::ObjectFile &File) {
904   return BinaryContext::createBinaryContext(
905       &File, false,
906       DWARFContext::create(File, DWARFContext::ProcessDebugRelocations::Ignore,
907                            nullptr, "", WithColor::defaultErrorHandler,
908                            WithColor::defaultWarningHandler));
909 }
910 
911 StringMap<KnownSectionsEntry>
912 createKnownSectionsMap(const MCObjectFileInfo &MCOFI) {
913   StringMap<KnownSectionsEntry> KnownSectionsTemp = {
914       {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
915       {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_EXT_TYPES}},
916       {"debug_str_offsets.dwo",
917        {MCOFI.getDwarfStrOffDWOSection(), DW_SECT_STR_OFFSETS}},
918       {"debug_str.dwo", {MCOFI.getDwarfStrDWOSection(), DW_SECT_EXT_unknown}},
919       {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_EXT_LOC}},
920       {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
921       {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}}};
922   return KnownSectionsTemp;
923 }
924 
925 StringRef getSectionName(const SectionRef &Section) {
926   Expected<StringRef> SectionName = Section.getName();
927   assert(SectionName && "Invalid section name.");
928   StringRef Name = *SectionName;
929   Name = Name.substr(Name.find_first_not_of("._"));
930   return Name;
931 }
932 
933 // Exctracts an appropriate slice if input is DWP.
934 // Applies patches or overwrites the section.
935 Optional<StringRef> updateDebugData(
936     DWARFContext &DWCtx, std::string &Storage, const SectionRef &Section,
937     const StringMap<KnownSectionsEntry> &KnownSections, MCStreamer &Streamer,
938     DWARFRewriter &Writer, const DWARFUnitIndex::Entry *DWOEntry,
939     uint64_t DWOId, std::unique_ptr<DebugBufferVector> &OutputBuffer) {
940   auto applyPatch = [&](DebugInfoBinaryPatcher *Patcher,
941                         StringRef Data) -> StringRef {
942     Patcher->computeNewOffsets(DWCtx, true);
943     Storage = Patcher->patchBinary(Data);
944     return StringRef(Storage.c_str(), Storage.size());
945   };
946 
947   using DWOSectionContribution =
948       const DWARFUnitIndex::Entry::SectionContribution;
949   auto getSliceData = [&](const DWARFUnitIndex::Entry *DWOEntry,
950                           StringRef OutData, DWARFSectionKind Sec,
951                           uint32_t &DWPOffset) -> StringRef {
952     if (DWOEntry) {
953       DWOSectionContribution *DWOContrubution = DWOEntry->getContribution(Sec);
954       DWPOffset = DWOContrubution->Offset;
955       OutData = OutData.substr(DWPOffset, DWOContrubution->Length);
956     }
957     return OutData;
958   };
959 
960   StringRef Name = getSectionName(Section);
961   auto SectionIter = KnownSections.find(Name);
962   if (SectionIter == KnownSections.end())
963     return None;
964   Streamer.SwitchSection(SectionIter->second.first);
965   Expected<StringRef> Contents = Section.getContents();
966   assert(Contents && "Invalid contents.");
967   StringRef OutData = *Contents;
968   uint32_t DWPOffset = 0;
969 
970   switch (SectionIter->second.second) {
971   default: {
972     if (!Name.equals("debug_str.dwo"))
973       errs() << "BOLT-WARNING: Unsupported Debug section: " << Name << "\n";
974     return OutData;
975   }
976   case DWARFSectionKind::DW_SECT_INFO: {
977     OutData = getSliceData(DWOEntry, OutData, DWARFSectionKind::DW_SECT_INFO,
978                            DWPOffset);
979     DebugInfoBinaryPatcher *Patcher = llvm::cast<DebugInfoBinaryPatcher>(
980         Writer.getBinaryDWODebugInfoPatcher(DWOId));
981     return applyPatch(Patcher, OutData);
982   }
983   case DWARFSectionKind::DW_SECT_EXT_TYPES: {
984     return getSliceData(DWOEntry, OutData, DWARFSectionKind::DW_SECT_EXT_TYPES,
985                         DWPOffset);
986   }
987   case DWARFSectionKind::DW_SECT_STR_OFFSETS: {
988     return getSliceData(DWOEntry, OutData,
989                         DWARFSectionKind::DW_SECT_STR_OFFSETS, DWPOffset);
990   }
991   case DWARFSectionKind::DW_SECT_ABBREV: {
992     DebugAbbrevWriter *AbbrevWriter = Writer.getBinaryDWOAbbrevWriter(DWOId);
993     OutputBuffer = AbbrevWriter->finalize();
994     // Creating explicit StringRef here, otherwise
995     // with impicit conversion it will take null byte as end of
996     // string.
997     return StringRef(reinterpret_cast<const char *>(OutputBuffer->data()),
998                      OutputBuffer->size());
999   }
1000   case DWARFSectionKind::DW_SECT_EXT_LOC: {
1001     DebugLocWriter *LocWriter = Writer.getDebugLocWriter(DWOId);
1002     OutputBuffer = LocWriter->getBuffer();
1003     // Creating explicit StringRef here, otherwise
1004     // with impicit conversion it will take null byte as end of
1005     // string.
1006     return StringRef(reinterpret_cast<const char *>(OutputBuffer->data()),
1007                      OutputBuffer->size());
1008   }
1009   case DWARFSectionKind::DW_SECT_LINE: {
1010     return getSliceData(DWOEntry, OutData, DWARFSectionKind::DW_SECT_LINE,
1011                         DWPOffset);
1012   }
1013   }
1014 }
1015 
1016 } // namespace
1017 
1018 void DWARFRewriter::writeDWP(
1019     std::unordered_map<uint64_t, std::string> &DWOIdToName) {
1020   SmallString<0> OutputNameStr;
1021   StringRef OutputName;
1022   if (opts::DwarfOutputPath.empty()) {
1023     OutputName =
1024         Twine(opts::OutputFilename).concat(".dwp").toStringRef(OutputNameStr);
1025   } else {
1026     StringRef ExeFileName = llvm::sys::path::filename(opts::OutputFilename);
1027     OutputName = Twine(opts::DwarfOutputPath)
1028                      .concat("/")
1029                      .concat(ExeFileName)
1030                      .concat(".dwp")
1031                      .toStringRef(OutputNameStr);
1032     errs() << "BOLT-WARNING: dwarf-output-path is in effect and .dwp file will "
1033               "possibly be written to another location that is not the same as "
1034               "the executable\n";
1035   }
1036   std::error_code EC;
1037   std::unique_ptr<ToolOutputFile> Out =
1038       std::make_unique<ToolOutputFile>(OutputName, EC, sys::fs::OF_None);
1039 
1040   const object::ObjectFile *File = BC.DwCtx->getDWARFObj().getFile();
1041   std::unique_ptr<BinaryContext> TmpBC = createDwarfOnlyBC(*File);
1042   std::unique_ptr<MCStreamer> Streamer = TmpBC->createStreamer(Out->os());
1043   const MCObjectFileInfo &MCOFI = *Streamer->getContext().getObjectFileInfo();
1044   StringMap<KnownSectionsEntry> KnownSections = createKnownSectionsMap(MCOFI);
1045   MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
1046   MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
1047 
1048   // Data Structures for DWP book keeping
1049   // Size of array corresponds to the number of sections supported by DWO format
1050   // in DWARF4/5.
1051   uint32_t ContributionOffsets[8] = {};
1052   std::deque<SmallString<32>> UncompressedSections;
1053   DWPStringPool Strings(*Streamer, StrSection);
1054   MapVector<uint64_t, UnitIndexEntry> IndexEntries;
1055   constexpr uint32_t IndexVersion = 2;
1056 
1057   // Setup DWP code once.
1058   DWARFContext *DWOCtx = BC.getDWOContext();
1059   const DWARFUnitIndex *CUIndex = nullptr;
1060   bool IsDWP = false;
1061   if (DWOCtx) {
1062     CUIndex = &DWOCtx->getCUIndex();
1063     IsDWP = !CUIndex->getRows().empty();
1064   }
1065 
1066   for (const std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units()) {
1067     Optional<uint64_t> DWOId = CU->getDWOId();
1068     if (!DWOId)
1069       continue;
1070 
1071     // Skipping CUs that we failed to load.
1072     Optional<DWARFUnit *> DWOCU = BC.getDWOCU(*DWOId);
1073     if (!DWOCU)
1074       continue;
1075 
1076     assert(CU->getVersion() == 4 && "For DWP output only DWARF4 is supported");
1077     UnitIndexEntry CurEntry = {};
1078     CurEntry.DWOName =
1079         dwarf::toString(CU->getUnitDIE().find(
1080                             {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
1081                         "");
1082     const char *Name = CU->getUnitDIE().getShortName();
1083     if (Name)
1084       CurEntry.Name = Name;
1085     StringRef CurStrSection;
1086     StringRef CurStrOffsetSection;
1087 
1088     // This maps each section contained in this file to its length.
1089     // This information is later on used to calculate the contributions,
1090     // i.e. offset and length, of each compile/type unit to a section.
1091     std::vector<std::pair<DWARFSectionKind, uint32_t>> SectionLength;
1092 
1093     const DWARFUnitIndex::Entry *DWOEntry = nullptr;
1094     if (IsDWP)
1095       DWOEntry = CUIndex->getFromHash(*DWOId);
1096 
1097     bool StrSectionWrittenOut = false;
1098     const object::ObjectFile *DWOFile =
1099         (*DWOCU)->getContext().getDWARFObj().getFile();
1100     for (const SectionRef &Section : DWOFile->sections()) {
1101       std::string Storage = "";
1102       std::unique_ptr<DebugBufferVector> OutputData;
1103       Optional<StringRef> TOutData = updateDebugData(
1104           (*DWOCU)->getContext(), Storage, Section, KnownSections, *Streamer,
1105           *this, DWOEntry, *DWOId, OutputData);
1106       if (!TOutData)
1107         continue;
1108 
1109       StringRef OutData = *TOutData;
1110       StringRef Name = getSectionName(Section);
1111       if (Name.equals("debug_str.dwo")) {
1112         CurStrSection = OutData;
1113       } else {
1114         // Since handleDebugDataPatching returned true, we already know this is
1115         // a known section.
1116         auto SectionIter = KnownSections.find(Name);
1117         if (SectionIter->second.second == DWARFSectionKind::DW_SECT_STR_OFFSETS)
1118           CurStrOffsetSection = OutData;
1119         else
1120           Streamer->emitBytes(OutData);
1121         auto Index =
1122             getContributionIndex(SectionIter->second.second, IndexVersion);
1123         CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
1124         CurEntry.Contributions[Index].Length = OutData.size();
1125         ContributionOffsets[Index] += CurEntry.Contributions[Index].Length;
1126       }
1127 
1128       // Strings are combined in to a new string section, and de-duplicated
1129       // based on hash.
1130       if (!StrSectionWrittenOut && !CurStrOffsetSection.empty() &&
1131           !CurStrSection.empty()) {
1132         writeStringsAndOffsets(*Streamer.get(), Strings, StrOffsetSection,
1133                                CurStrSection, CurStrOffsetSection,
1134                                CU->getVersion());
1135         StrSectionWrittenOut = true;
1136       }
1137     }
1138     CompileUnitIdentifiers CUI{*DWOId, CurEntry.Name.c_str(),
1139                                CurEntry.DWOName.c_str()};
1140     auto P = IndexEntries.insert(std::make_pair(CUI.Signature, CurEntry));
1141     if (!P.second) {
1142       Error Err = buildDuplicateError(*P.first, CUI, "");
1143       errs() << "BOLT-ERROR: " << toString(std::move(Err)) << "\n";
1144       return;
1145     }
1146   }
1147 
1148   // Lie about the type contribution for DWARF < 5. In DWARFv5 the type
1149   // section does not exist, so no need to do anything about this.
1150   ContributionOffsets[getContributionIndex(DW_SECT_EXT_TYPES, 2)] = 0;
1151   writeIndex(*Streamer.get(), MCOFI.getDwarfCUIndexSection(),
1152              ContributionOffsets, IndexEntries, IndexVersion);
1153 
1154   Streamer->Finish();
1155   Out->keep();
1156 }
1157 
1158 void DWARFRewriter::writeDWOFiles(
1159     std::unordered_map<uint64_t, std::string> &DWOIdToName) {
1160   // Setup DWP code once.
1161   DWARFContext *DWOCtx = BC.getDWOContext();
1162   const DWARFUnitIndex *CUIndex = nullptr;
1163   bool IsDWP = false;
1164   if (DWOCtx) {
1165     CUIndex = &DWOCtx->getCUIndex();
1166     IsDWP = !CUIndex->getRows().empty();
1167   }
1168 
1169   for (const std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units()) {
1170     Optional<uint64_t> DWOId = CU->getDWOId();
1171     if (!DWOId)
1172       continue;
1173 
1174     // Skipping CUs that we failed to load.
1175     Optional<DWARFUnit *> DWOCU = BC.getDWOCU(*DWOId);
1176     if (!DWOCU)
1177       continue;
1178 
1179     std::string CompDir = opts::DwarfOutputPath.empty()
1180                               ? CU->getCompilationDir()
1181                               : opts::DwarfOutputPath.c_str();
1182     std::string ObjectName = getDWOName(*CU.get(), nullptr, DWOIdToName);
1183     auto FullPath = CompDir.append("/").append(ObjectName);
1184 
1185     std::error_code EC;
1186     std::unique_ptr<ToolOutputFile> TempOut =
1187         std::make_unique<ToolOutputFile>(FullPath, EC, sys::fs::OF_None);
1188 
1189     const DWARFUnitIndex::Entry *DWOEntry = nullptr;
1190     if (IsDWP)
1191       DWOEntry = CUIndex->getFromHash(*DWOId);
1192 
1193     const object::ObjectFile *File =
1194         (*DWOCU)->getContext().getDWARFObj().getFile();
1195     std::unique_ptr<BinaryContext> TmpBC = createDwarfOnlyBC(*File);
1196     std::unique_ptr<MCStreamer> Streamer = TmpBC->createStreamer(TempOut->os());
1197     StringMap<KnownSectionsEntry> KnownSections =
1198         createKnownSectionsMap(*Streamer->getContext().getObjectFileInfo());
1199 
1200     for (const SectionRef &Section : File->sections()) {
1201       std::string Storage = "";
1202       std::unique_ptr<DebugBufferVector> OutputData;
1203       if (Optional<StringRef> OutData = updateDebugData(
1204               (*DWOCU)->getContext(), Storage, Section, KnownSections,
1205               *Streamer, *this, DWOEntry, *DWOId, OutputData))
1206         Streamer->emitBytes(*OutData);
1207     }
1208     Streamer->Finish();
1209     TempOut->keep();
1210   }
1211 }
1212 
1213 void DWARFRewriter::updateGdbIndexSection(CUOffsetMap &CUMap) {
1214   if (!BC.getGdbIndexSection())
1215     return;
1216 
1217   // See https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html
1218   // for .gdb_index section format.
1219 
1220   StringRef GdbIndexContents = BC.getGdbIndexSection()->getContents();
1221 
1222   const char *Data = GdbIndexContents.data();
1223 
1224   // Parse the header.
1225   const uint32_t Version = read32le(Data);
1226   if (Version != 7 && Version != 8) {
1227     errs() << "BOLT-ERROR: can only process .gdb_index versions 7 and 8\n";
1228     exit(1);
1229   }
1230 
1231   // Some .gdb_index generators use file offsets while others use section
1232   // offsets. Hence we can only rely on offsets relative to each other,
1233   // and ignore their absolute values.
1234   const uint32_t CUListOffset = read32le(Data + 4);
1235   const uint32_t CUTypesOffset = read32le(Data + 8);
1236   const uint32_t AddressTableOffset = read32le(Data + 12);
1237   const uint32_t SymbolTableOffset = read32le(Data + 16);
1238   const uint32_t ConstantPoolOffset = read32le(Data + 20);
1239   Data += 24;
1240 
1241   // Map CUs offsets to indices and verify existing index table.
1242   std::map<uint32_t, uint32_t> OffsetToIndexMap;
1243   const uint32_t CUListSize = CUTypesOffset - CUListOffset;
1244   const unsigned NumCUs = BC.DwCtx->getNumCompileUnits();
1245   if (CUListSize != NumCUs * 16) {
1246     errs() << "BOLT-ERROR: .gdb_index: CU count mismatch\n";
1247     exit(1);
1248   }
1249   for (unsigned Index = 0; Index < NumCUs; ++Index, Data += 16) {
1250     const DWARFUnit *CU = BC.DwCtx->getUnitAtIndex(Index);
1251     const uint64_t Offset = read64le(Data);
1252     if (CU->getOffset() != Offset) {
1253       errs() << "BOLT-ERROR: .gdb_index CU offset mismatch\n";
1254       exit(1);
1255     }
1256 
1257     OffsetToIndexMap[Offset] = Index;
1258   }
1259 
1260   // Ignore old address table.
1261   const uint32_t OldAddressTableSize = SymbolTableOffset - AddressTableOffset;
1262   // Move Data to the beginning of symbol table.
1263   Data += SymbolTableOffset - CUTypesOffset;
1264 
1265   // Calculate the size of the new address table.
1266   uint32_t NewAddressTableSize = 0;
1267   for (const auto &CURangesPair : ARangesSectionWriter->getCUAddressRanges()) {
1268     const SmallVector<DebugAddressRange, 2> &Ranges = CURangesPair.second;
1269     NewAddressTableSize += Ranges.size() * 20;
1270   }
1271 
1272   // Difference between old and new table (and section) sizes.
1273   // Could be negative.
1274   int32_t Delta = NewAddressTableSize - OldAddressTableSize;
1275 
1276   size_t NewGdbIndexSize = GdbIndexContents.size() + Delta;
1277 
1278   // Free'd by ExecutableFileMemoryManager.
1279   auto *NewGdbIndexContents = new uint8_t[NewGdbIndexSize];
1280   uint8_t *Buffer = NewGdbIndexContents;
1281 
1282   write32le(Buffer, Version);
1283   write32le(Buffer + 4, CUListOffset);
1284   write32le(Buffer + 8, CUTypesOffset);
1285   write32le(Buffer + 12, AddressTableOffset);
1286   write32le(Buffer + 16, SymbolTableOffset + Delta);
1287   write32le(Buffer + 20, ConstantPoolOffset + Delta);
1288   Buffer += 24;
1289 
1290   // Writing out CU List <Offset, Size>
1291   for (auto &CUInfo : CUMap) {
1292     write64le(Buffer, CUInfo.second.Offset);
1293     // Length encoded in CU doesn't contain first 4 bytes that encode length.
1294     write64le(Buffer + 8, CUInfo.second.Length + 4);
1295     Buffer += 16;
1296   }
1297 
1298   // Copy over types CU list
1299   // Spec says " triplet, the first value is the CU offset, the second value is
1300   // the type offset in the CU, and the third value is the type signature"
1301   // Looking at what is being generated by gdb-add-index. The first entry is TU
1302   // offset, second entry is offset from it, and third entry is the type
1303   // signature.
1304   memcpy(Buffer, GdbIndexContents.data() + CUTypesOffset,
1305          AddressTableOffset - CUTypesOffset);
1306   Buffer += AddressTableOffset - CUTypesOffset;
1307 
1308   // Generate new address table.
1309   for (const std::pair<const uint64_t, DebugAddressRangesVector> &CURangesPair :
1310        ARangesSectionWriter->getCUAddressRanges()) {
1311     const uint32_t CUIndex = OffsetToIndexMap[CURangesPair.first];
1312     const DebugAddressRangesVector &Ranges = CURangesPair.second;
1313     for (const DebugAddressRange &Range : Ranges) {
1314       write64le(Buffer, Range.LowPC);
1315       write64le(Buffer + 8, Range.HighPC);
1316       write32le(Buffer + 16, CUIndex);
1317       Buffer += 20;
1318     }
1319   }
1320 
1321   const size_t TrailingSize =
1322       GdbIndexContents.data() + GdbIndexContents.size() - Data;
1323   assert(Buffer + TrailingSize == NewGdbIndexContents + NewGdbIndexSize &&
1324          "size calculation error");
1325 
1326   // Copy over the rest of the original data.
1327   memcpy(Buffer, Data, TrailingSize);
1328 
1329   // Register the new section.
1330   BC.registerOrUpdateNoteSection(".gdb_index", NewGdbIndexContents,
1331                                  NewGdbIndexSize);
1332 }
1333 
1334 std::unique_ptr<DebugBufferVector>
1335 DWARFRewriter::makeFinalLocListsSection(SimpleBinaryPatcher &DebugInfoPatcher) {
1336   auto LocBuffer = std::make_unique<DebugBufferVector>();
1337   auto LocStream = std::make_unique<raw_svector_ostream>(*LocBuffer);
1338   auto Writer =
1339       std::unique_ptr<MCObjectWriter>(BC.createObjectWriter(*LocStream));
1340 
1341   uint64_t SectionOffset = 0;
1342 
1343   // Add an empty list as the first entry;
1344   const char Zeroes[16] = {0};
1345   *LocStream << StringRef(Zeroes, 16);
1346   SectionOffset += 2 * 8;
1347 
1348   for (std::pair<const uint64_t, std::unique_ptr<DebugLocWriter>> &Loc :
1349        LocListWritersByCU) {
1350     DebugLocWriter *LocWriter = Loc.second.get();
1351     if (auto *LocListWriter = llvm::dyn_cast<DebugLoclistWriter>(LocWriter)) {
1352       SimpleBinaryPatcher *Patcher =
1353           getBinaryDWODebugInfoPatcher(LocListWriter->getDWOID());
1354       LocListWriter->finalize(0, *Patcher);
1355       continue;
1356     }
1357     LocWriter->finalize(SectionOffset, DebugInfoPatcher);
1358     std::unique_ptr<DebugBufferVector> CurrCULocationLists =
1359         LocWriter->getBuffer();
1360     *LocStream << *CurrCULocationLists;
1361     SectionOffset += CurrCULocationLists->size();
1362   }
1363 
1364   return LocBuffer;
1365 }
1366 
1367 namespace {
1368 
1369 void getRangeAttrData(DWARFDie DIE, Optional<AttrInfo> &LowPCVal,
1370                       Optional<AttrInfo> &HighPCVal) {
1371   LowPCVal = findAttributeInfo(DIE, dwarf::DW_AT_low_pc);
1372   HighPCVal = findAttributeInfo(DIE, dwarf::DW_AT_high_pc);
1373   uint64_t LowPCOffset = LowPCVal->Offset;
1374   uint64_t HighPCOffset = HighPCVal->Offset;
1375   dwarf::Form LowPCForm = LowPCVal->V.getForm();
1376   dwarf::Form HighPCForm = HighPCVal->V.getForm();
1377 
1378   if (LowPCForm != dwarf::DW_FORM_addr &&
1379       LowPCForm != dwarf::DW_FORM_GNU_addr_index) {
1380     errs() << "BOLT-WARNING: unexpected low_pc form value. Cannot update DIE "
1381            << "at offset 0x" << Twine::utohexstr(DIE.getOffset()) << "\n";
1382     return;
1383   }
1384   if (HighPCForm != dwarf::DW_FORM_addr && HighPCForm != dwarf::DW_FORM_data8 &&
1385       HighPCForm != dwarf::DW_FORM_data4 &&
1386       HighPCForm != dwarf::DW_FORM_data2 &&
1387       HighPCForm != dwarf::DW_FORM_data1 &&
1388       HighPCForm != dwarf::DW_FORM_udata) {
1389     errs() << "BOLT-WARNING: unexpected high_pc form value. Cannot update DIE "
1390            << "at offset 0x" << Twine::utohexstr(DIE.getOffset()) << "\n";
1391     return;
1392   }
1393   if ((LowPCOffset == -1U || (LowPCOffset + 8 != HighPCOffset)) &&
1394       LowPCForm != dwarf::DW_FORM_GNU_addr_index) {
1395     errs() << "BOLT-WARNING: high_pc expected immediately after low_pc. "
1396            << "Cannot update DIE at offset 0x"
1397            << Twine::utohexstr(DIE.getOffset()) << '\n';
1398     return;
1399   }
1400 }
1401 
1402 } // namespace
1403 
1404 void DWARFRewriter::patchLowHigh(DWARFDie DIE, DebugAddressRange Range,
1405                                  SimpleBinaryPatcher &DebugInfoPatcher,
1406                                  Optional<uint64_t> DWOId) {
1407   Optional<AttrInfo> LowPCVal = None;
1408   Optional<AttrInfo> HighPCVal = None;
1409   getRangeAttrData(DIE, LowPCVal, HighPCVal);
1410   uint64_t LowPCOffset = LowPCVal->Offset;
1411   uint64_t HighPCOffset = HighPCVal->Offset;
1412   auto *TempDebugPatcher = &DebugInfoPatcher;
1413   if (LowPCVal->V.getForm() == dwarf::DW_FORM_GNU_addr_index) {
1414     uint32_t AddressIndex =
1415         AddrWriter->getIndexFromAddress(Range.LowPC, *DWOId);
1416     TempDebugPatcher = getBinaryDWODebugInfoPatcher(*DWOId);
1417     TempDebugPatcher->addUDataPatch(LowPCOffset, AddressIndex, LowPCVal->Size);
1418     // 2.17.2
1419     // If the value of the DW_AT_high_pc is of class address, it is the
1420     // relocated address of the first location past the last instruction
1421     // associated with the entity; if it is of class constant, the value is
1422     // an unsigned integer offset which when added to the low PC gives the
1423     // address of the first location past the last instruction associated
1424     // with the entity.
1425     if (!HighPCVal->V.isFormClass(DWARFFormValue::FC_Constant)) {
1426       AddressIndex = AddrWriter->getIndexFromAddress(Range.HighPC, *DWOId);
1427       TempDebugPatcher->addUDataPatch(HighPCOffset, AddressIndex,
1428                                       HighPCVal->Size);
1429     }
1430   } else {
1431     TempDebugPatcher->addLE64Patch(LowPCOffset, Range.LowPC);
1432   }
1433 
1434   uint64_t HighPC = Range.HighPC;
1435   // The DW_FORM_data* is delta between high and low pc
1436   if (HighPCVal->V.getForm() != dwarf::Form::DW_FORM_addr)
1437     HighPC -= Range.LowPC;
1438 
1439   if (isHighPcFormEightBytes(HighPCVal->V.getForm()))
1440     TempDebugPatcher->addLE64Patch(HighPCOffset, HighPC);
1441   else
1442     TempDebugPatcher->addLE32Patch(HighPCOffset, HighPC);
1443 }
1444 
1445 void DWARFRewriter::convertToRangesPatchAbbrev(
1446     const DWARFUnit &Unit, const DWARFAbbreviationDeclaration *Abbrev,
1447     DebugAbbrevWriter &AbbrevWriter, Optional<uint64_t> RangesBase) {
1448   auto getAttributeForm = [&Abbrev](const dwarf::Attribute Attr) {
1449     Optional<uint32_t> Index = Abbrev->findAttributeIndex(Attr);
1450     assert(Index && "attribute not found");
1451     return Abbrev->getFormByIndex(*Index);
1452   };
1453   dwarf::Form LowPCForm = getAttributeForm(dwarf::DW_AT_low_pc);
1454 
1455   // DW_FORM_GNU_addr_index is already variable encoding so nothing to do
1456   // there.
1457   if (RangesBase) {
1458     assert(LowPCForm != dwarf::DW_FORM_GNU_addr_index);
1459     AbbrevWriter.addAttributePatch(Unit, Abbrev, dwarf::DW_AT_low_pc,
1460                                    dwarf::DW_AT_GNU_ranges_base,
1461                                    dwarf::DW_FORM_sec_offset);
1462   }
1463 
1464   AbbrevWriter.addAttributePatch(Unit, Abbrev, dwarf::DW_AT_high_pc,
1465                                  dwarf::DW_AT_ranges,
1466                                  dwarf::DW_FORM_sec_offset);
1467 }
1468 
1469 void DWARFRewriter::convertToRangesPatchDebugInfo(
1470     DWARFDie DIE, uint64_t RangesSectionOffset,
1471     SimpleBinaryPatcher &DebugInfoPatcher, Optional<uint64_t> RangesBase) {
1472   Optional<AttrInfo> LowPCVal = None;
1473   Optional<AttrInfo> HighPCVal = None;
1474   getRangeAttrData(DIE, LowPCVal, HighPCVal);
1475   uint64_t LowPCOffset = LowPCVal->Offset;
1476   uint64_t HighPCOffset = HighPCVal->Offset;
1477 
1478   std::lock_guard<std::mutex> Lock(DebugInfoPatcherMutex);
1479   uint32_t BaseOffset = 0;
1480   if (LowPCVal->V.getForm() == dwarf::DW_FORM_GNU_addr_index) {
1481     // Use ULEB128 for the value.
1482     DebugInfoPatcher.addUDataPatch(LowPCOffset, 0,
1483                                    std::abs(int(HighPCOffset - LowPCOffset)));
1484     // Ranges are relative to DW_AT_GNU_ranges_base.
1485     BaseOffset = DebugInfoPatcher.getRangeBase();
1486   } else {
1487     // If case DW_AT_low_pc was converted into DW_AT_GNU_ranges_base
1488     if (RangesBase)
1489       DebugInfoPatcher.addLE32Patch(LowPCOffset, *RangesBase, 8);
1490     else
1491       DebugInfoPatcher.addLE64Patch(LowPCOffset, 0);
1492   }
1493   DebugInfoPatcher.addLE32Patch(HighPCOffset, RangesSectionOffset - BaseOffset,
1494                                 HighPCVal->Size);
1495 }
1496