xref: /llvm-project/bolt/lib/Core/BinaryEmitter.cpp (revision f633f325a1b808d33ca9653ed373353549ddcde6)
1 //===- bolt/Core/BinaryEmitter.cpp - Emit code and data -------------------===//
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 the collection of functions and classes used for
10 // emission of code and data into object/binary file.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "bolt/Core/BinaryEmitter.h"
15 #include "bolt/Core/BinaryContext.h"
16 #include "bolt/Core/BinaryFunction.h"
17 #include "bolt/Core/DebugData.h"
18 #include "bolt/Core/FunctionLayout.h"
19 #include "bolt/Utils/CommandLineOpts.h"
20 #include "bolt/Utils/Utils.h"
21 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
22 #include "llvm/MC/MCSection.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/LEB128.h"
26 #include "llvm/Support/SMLoc.h"
27 
28 #define DEBUG_TYPE "bolt"
29 
30 using namespace llvm;
31 using namespace bolt;
32 
33 namespace opts {
34 
35 extern cl::opt<JumpTableSupportLevel> JumpTables;
36 extern cl::opt<bool> PreserveBlocksAlignment;
37 
38 cl::opt<bool> AlignBlocks("align-blocks", cl::desc("align basic blocks"),
39                           cl::cat(BoltOptCategory));
40 
41 cl::opt<MacroFusionType>
42 AlignMacroOpFusion("align-macro-fusion",
43   cl::desc("fix instruction alignment for macro-fusion (x86 relocation mode)"),
44   cl::init(MFT_HOT),
45   cl::values(clEnumValN(MFT_NONE, "none",
46                "do not insert alignment no-ops for macro-fusion"),
47              clEnumValN(MFT_HOT, "hot",
48                "only insert alignment no-ops on hot execution paths (default)"),
49              clEnumValN(MFT_ALL, "all",
50                "always align instructions to allow macro-fusion")),
51   cl::ZeroOrMore,
52   cl::cat(BoltRelocCategory));
53 
54 static cl::list<std::string>
55 BreakFunctionNames("break-funcs",
56   cl::CommaSeparated,
57   cl::desc("list of functions to core dump on (debugging)"),
58   cl::value_desc("func1,func2,func3,..."),
59   cl::Hidden,
60   cl::cat(BoltCategory));
61 
62 static cl::list<std::string>
63 FunctionPadSpec("pad-funcs",
64   cl::CommaSeparated,
65   cl::desc("list of functions to pad with amount of bytes"),
66   cl::value_desc("func1:pad1,func2:pad2,func3:pad3,..."),
67   cl::Hidden,
68   cl::cat(BoltCategory));
69 
70 static cl::opt<bool> MarkFuncs(
71     "mark-funcs",
72     cl::desc("mark function boundaries with break instruction to make "
73              "sure we accidentally don't cross them"),
74     cl::ReallyHidden, cl::cat(BoltCategory));
75 
76 static cl::opt<bool> PrintJumpTables("print-jump-tables",
77                                      cl::desc("print jump tables"), cl::Hidden,
78                                      cl::cat(BoltCategory));
79 
80 static cl::opt<bool>
81 X86AlignBranchBoundaryHotOnly("x86-align-branch-boundary-hot-only",
82   cl::desc("only apply branch boundary alignment in hot code"),
83   cl::init(true),
84   cl::cat(BoltOptCategory));
85 
86 size_t padFunction(const BinaryFunction &Function) {
87   static std::map<std::string, size_t> FunctionPadding;
88 
89   if (FunctionPadding.empty() && !FunctionPadSpec.empty()) {
90     for (std::string &Spec : FunctionPadSpec) {
91       size_t N = Spec.find(':');
92       if (N == std::string::npos)
93         continue;
94       std::string Name = Spec.substr(0, N);
95       size_t Padding = std::stoull(Spec.substr(N + 1));
96       FunctionPadding[Name] = Padding;
97     }
98   }
99 
100   for (auto &FPI : FunctionPadding) {
101     std::string Name = FPI.first;
102     size_t Padding = FPI.second;
103     if (Function.hasNameRegex(Name))
104       return Padding;
105   }
106 
107   return 0;
108 }
109 
110 } // namespace opts
111 
112 namespace {
113 using JumpTable = bolt::JumpTable;
114 
115 class BinaryEmitter {
116 private:
117   BinaryEmitter(const BinaryEmitter &) = delete;
118   BinaryEmitter &operator=(const BinaryEmitter &) = delete;
119 
120   MCStreamer &Streamer;
121   BinaryContext &BC;
122 
123 public:
124   BinaryEmitter(MCStreamer &Streamer, BinaryContext &BC)
125       : Streamer(Streamer), BC(BC) {}
126 
127   /// Emit all code and data.
128   void emitAll(StringRef OrgSecPrefix);
129 
130   /// Emit function code. The caller is responsible for emitting function
131   /// symbol(s) and setting the section to emit the code to.
132   void emitFunctionBody(BinaryFunction &BF, FunctionFragment &FF,
133                         bool EmitCodeOnly = false);
134 
135 private:
136   /// Emit function code.
137   void emitFunctions();
138 
139   /// Emit a single function.
140   bool emitFunction(BinaryFunction &BF, FunctionFragment &FF);
141 
142   /// Helper for emitFunctionBody to write data inside a function
143   /// (used for AArch64)
144   void emitConstantIslands(BinaryFunction &BF, bool EmitColdPart,
145                            BinaryFunction *OnBehalfOf = nullptr);
146 
147   /// Emit jump tables for the function.
148   void emitJumpTables(const BinaryFunction &BF);
149 
150   /// Emit jump table data. Callee supplies sections for the data.
151   void emitJumpTable(const JumpTable &JT, MCSection *HotSection,
152                      MCSection *ColdSection);
153 
154   void emitCFIInstruction(const MCCFIInstruction &Inst) const;
155 
156   /// Emit exception handling ranges for the function.
157   void emitLSDA(BinaryFunction &BF, const FunctionFragment &FF);
158 
159   /// Emit line number information corresponding to \p NewLoc. \p PrevLoc
160   /// provides a context for de-duplication of line number info.
161   /// \p FirstInstr indicates if \p NewLoc represents the first instruction
162   /// in a sequence, such as a function fragment.
163   ///
164   /// If \p NewLoc location matches \p PrevLoc, no new line number entry will be
165   /// created and the function will return \p PrevLoc while \p InstrLabel will
166   /// be ignored. Otherwise, the caller should use \p InstrLabel to mark the
167   /// corresponding instruction by emitting \p InstrLabel before it.
168   /// If \p InstrLabel is set by the caller, its value will be used with \p
169   /// \p NewLoc. If it was nullptr on entry, it will be populated with a pointer
170   /// to a new temp symbol used with \p NewLoc.
171   ///
172   /// Return new current location which is either \p NewLoc or \p PrevLoc.
173   SMLoc emitLineInfo(const BinaryFunction &BF, SMLoc NewLoc, SMLoc PrevLoc,
174                      bool FirstInstr, MCSymbol *&InstrLabel);
175 
176   /// Use \p FunctionEndSymbol to mark the end of the line info sequence.
177   /// Note that it does not automatically result in the insertion of the EOS
178   /// marker in the line table program, but provides one to the DWARF generator
179   /// when it needs it.
180   void emitLineInfoEnd(const BinaryFunction &BF, MCSymbol *FunctionEndSymbol);
181 
182   /// Emit debug line info for unprocessed functions from CUs that include
183   /// emitted functions.
184   void emitDebugLineInfoForOriginalFunctions();
185 
186   /// Emit debug line for CUs that were not modified.
187   void emitDebugLineInfoForUnprocessedCUs();
188 
189   /// Emit data sections that have code references in them.
190   void emitDataSections(StringRef OrgSecPrefix);
191 };
192 
193 } // anonymous namespace
194 
195 void BinaryEmitter::emitAll(StringRef OrgSecPrefix) {
196   Streamer.initSections(false, *BC.STI);
197 
198   if (opts::UpdateDebugSections && BC.isELF()) {
199     // Force the emission of debug line info into allocatable section to ensure
200     // JITLink will process it.
201     //
202     // NB: on MachO all sections are required for execution, hence no need
203     //     to change flags/attributes.
204     MCSectionELF *ELFDwarfLineSection =
205         static_cast<MCSectionELF *>(BC.MOFI->getDwarfLineSection());
206     ELFDwarfLineSection->setFlags(ELF::SHF_ALLOC);
207     MCSectionELF *ELFDwarfLineStrSection =
208         static_cast<MCSectionELF *>(BC.MOFI->getDwarfLineStrSection());
209     ELFDwarfLineStrSection->setFlags(ELF::SHF_ALLOC);
210   }
211 
212   if (RuntimeLibrary *RtLibrary = BC.getRuntimeLibrary())
213     RtLibrary->emitBinary(BC, Streamer);
214 
215   BC.getTextSection()->setAlignment(Align(opts::AlignText));
216 
217   emitFunctions();
218 
219   if (opts::UpdateDebugSections) {
220     emitDebugLineInfoForOriginalFunctions();
221     DwarfLineTable::emit(BC, Streamer);
222   }
223 
224   emitDataSections(OrgSecPrefix);
225 
226   // TODO Enable for Mach-O once BinaryContext::getDataSection supports it.
227   if (BC.isELF())
228     AddressMap::emit(Streamer, BC);
229 }
230 
231 void BinaryEmitter::emitFunctions() {
232   auto emit = [&](const std::vector<BinaryFunction *> &Functions) {
233     const bool HasProfile = BC.NumProfiledFuncs > 0;
234     const bool OriginalAllowAutoPadding = Streamer.getAllowAutoPadding();
235     for (BinaryFunction *Function : Functions) {
236       if (!BC.shouldEmit(*Function))
237         continue;
238 
239       LLVM_DEBUG(dbgs() << "BOLT: generating code for function \"" << *Function
240                         << "\" : " << Function->getFunctionNumber() << '\n');
241 
242       // Was any part of the function emitted.
243       bool Emitted = false;
244 
245       // Turn off Intel JCC Erratum mitigation for cold code if requested
246       if (HasProfile && opts::X86AlignBranchBoundaryHotOnly &&
247           !Function->hasValidProfile())
248         Streamer.setAllowAutoPadding(false);
249 
250       FunctionLayout &Layout = Function->getLayout();
251       Emitted |= emitFunction(*Function, Layout.getMainFragment());
252 
253       if (Function->isSplit()) {
254         if (opts::X86AlignBranchBoundaryHotOnly)
255           Streamer.setAllowAutoPadding(false);
256 
257         assert((Layout.fragment_size() == 1 || Function->isSimple()) &&
258                "Only simple functions can have fragments");
259         for (FunctionFragment &FF : Layout.getSplitFragments()) {
260           // Skip empty fragments so no symbols and sections for empty fragments
261           // are generated
262           if (FF.empty() && !Function->hasConstantIsland())
263             continue;
264           Emitted |= emitFunction(*Function, FF);
265         }
266       }
267 
268       Streamer.setAllowAutoPadding(OriginalAllowAutoPadding);
269 
270       if (Emitted)
271         Function->setEmitted(/*KeepCFG=*/opts::PrintCacheMetrics);
272     }
273   };
274 
275   // Mark the start of hot text.
276   if (opts::HotText) {
277     Streamer.switchSection(BC.getTextSection());
278     Streamer.emitLabel(BC.getHotTextStartSymbol());
279   }
280 
281   // Emit functions in sorted order.
282   std::vector<BinaryFunction *> SortedFunctions = BC.getSortedFunctions();
283   emit(SortedFunctions);
284 
285   // Emit functions added by BOLT.
286   emit(BC.getInjectedBinaryFunctions());
287 
288   // Mark the end of hot text.
289   if (opts::HotText) {
290     Streamer.switchSection(BC.getTextSection());
291     Streamer.emitLabel(BC.getHotTextEndSymbol());
292   }
293 }
294 
295 bool BinaryEmitter::emitFunction(BinaryFunction &Function,
296                                  FunctionFragment &FF) {
297   if (Function.size() == 0 && !Function.hasIslandsInfo())
298     return false;
299 
300   if (Function.getState() == BinaryFunction::State::Empty)
301     return false;
302 
303   // Avoid emitting function without instructions when overwriting the original
304   // function in-place. Otherwise, emit the empty function to define the symbol.
305   if (!BC.HasRelocations && !Function.hasNonPseudoInstructions())
306     return false;
307 
308   MCSection *Section =
309       BC.getCodeSection(Function.getCodeSectionName(FF.getFragmentNum()));
310   Streamer.switchSection(Section);
311   Section->setHasInstructions(true);
312   BC.Ctx->addGenDwarfSection(Section);
313 
314   if (BC.HasRelocations) {
315     // Set section alignment to at least maximum possible object alignment.
316     // We need this to support LongJmp and other passes that calculates
317     // tentative layout.
318     Section->ensureMinAlignment(Align(opts::AlignFunctions));
319 
320     Streamer.emitCodeAlignment(Function.getMinAlign(), &*BC.STI);
321     uint16_t MaxAlignBytes = FF.isSplitFragment()
322                                  ? Function.getMaxColdAlignmentBytes()
323                                  : Function.getMaxAlignmentBytes();
324     if (MaxAlignBytes > 0)
325       Streamer.emitCodeAlignment(Function.getAlign(), &*BC.STI, MaxAlignBytes);
326   } else {
327     Streamer.emitCodeAlignment(Function.getAlign(), &*BC.STI);
328   }
329 
330   MCContext &Context = Streamer.getContext();
331   const MCAsmInfo *MAI = Context.getAsmInfo();
332 
333   MCSymbol *const StartSymbol = Function.getSymbol(FF.getFragmentNum());
334 
335   // Emit all symbols associated with the main function entry.
336   if (FF.isMainFragment()) {
337     for (MCSymbol *Symbol : Function.getSymbols()) {
338       Streamer.emitSymbolAttribute(Symbol, MCSA_ELF_TypeFunction);
339       Streamer.emitLabel(Symbol);
340     }
341   } else {
342     Streamer.emitSymbolAttribute(StartSymbol, MCSA_ELF_TypeFunction);
343     Streamer.emitLabel(StartSymbol);
344   }
345 
346   // Emit CFI start
347   if (Function.hasCFI()) {
348     Streamer.emitCFIStartProc(/*IsSimple=*/false);
349     if (Function.getPersonalityFunction() != nullptr)
350       Streamer.emitCFIPersonality(Function.getPersonalityFunction(),
351                                   Function.getPersonalityEncoding());
352     MCSymbol *LSDASymbol = Function.getLSDASymbol(FF.getFragmentNum());
353     if (LSDASymbol)
354       Streamer.emitCFILsda(LSDASymbol, BC.LSDAEncoding);
355     else
356       Streamer.emitCFILsda(0, dwarf::DW_EH_PE_omit);
357     // Emit CFI instructions relative to the CIE
358     for (const MCCFIInstruction &CFIInstr : Function.cie()) {
359       // Only write CIE CFI insns that LLVM will not already emit
360       const std::vector<MCCFIInstruction> &FrameInstrs =
361           MAI->getInitialFrameState();
362       if (!llvm::is_contained(FrameInstrs, CFIInstr))
363         emitCFIInstruction(CFIInstr);
364     }
365   }
366 
367   assert((Function.empty() || !(*Function.begin()).isCold()) &&
368          "first basic block should never be cold");
369 
370   // Emit UD2 at the beginning if requested by user.
371   if (!opts::BreakFunctionNames.empty()) {
372     for (std::string &Name : opts::BreakFunctionNames) {
373       if (Function.hasNameRegex(Name)) {
374         Streamer.emitIntValue(0x0B0F, 2); // UD2: 0F 0B
375         break;
376       }
377     }
378   }
379 
380   // Emit code.
381   emitFunctionBody(Function, FF, /*EmitCodeOnly=*/false);
382 
383   // Emit padding if requested.
384   if (size_t Padding = opts::padFunction(Function)) {
385     LLVM_DEBUG(dbgs() << "BOLT-DEBUG: padding function " << Function << " with "
386                       << Padding << " bytes\n");
387     Streamer.emitFill(Padding, MAI->getTextAlignFillValue());
388   }
389 
390   if (opts::MarkFuncs)
391     Streamer.emitBytes(BC.MIB->getTrapFillValue());
392 
393   // Emit CFI end
394   if (Function.hasCFI())
395     Streamer.emitCFIEndProc();
396 
397   MCSymbol *EndSymbol = Function.getFunctionEndLabel(FF.getFragmentNum());
398   Streamer.emitLabel(EndSymbol);
399 
400   if (MAI->hasDotTypeDotSizeDirective()) {
401     const MCExpr *SizeExpr = MCBinaryExpr::createSub(
402         MCSymbolRefExpr::create(EndSymbol, Context),
403         MCSymbolRefExpr::create(StartSymbol, Context), Context);
404     Streamer.emitELFSize(StartSymbol, SizeExpr);
405   }
406 
407   if (opts::UpdateDebugSections && Function.getDWARFUnit())
408     emitLineInfoEnd(Function, EndSymbol);
409 
410   // Exception handling info for the function.
411   emitLSDA(Function, FF);
412 
413   if (FF.isMainFragment() && opts::JumpTables > JTS_NONE)
414     emitJumpTables(Function);
415 
416   return true;
417 }
418 
419 void BinaryEmitter::emitFunctionBody(BinaryFunction &BF, FunctionFragment &FF,
420                                      bool EmitCodeOnly) {
421   if (!EmitCodeOnly && FF.isSplitFragment() && BF.hasConstantIsland()) {
422     assert(BF.getLayout().isHotColdSplit() &&
423            "Constant island support only with hot/cold split");
424     BF.duplicateConstantIslands();
425   }
426 
427   if (!FF.empty() && FF.front()->isLandingPad()) {
428     assert(!FF.front()->isEntryPoint() &&
429            "Landing pad cannot be entry point of function");
430     // If the first block of the fragment is a landing pad, it's offset from the
431     // start of the area that the corresponding LSDA describes is zero. In this
432     // case, the call site entries in that LSDA have 0 as offset to the landing
433     // pad, which the runtime interprets as "no handler". To prevent this,
434     // insert some padding.
435     Streamer.emitBytes(BC.MIB->getTrapFillValue());
436   }
437 
438   // Track the first emitted instruction with debug info.
439   bool FirstInstr = true;
440   for (BinaryBasicBlock *const BB : FF) {
441     if ((opts::AlignBlocks || opts::PreserveBlocksAlignment) &&
442         BB->getAlignment() > 1)
443       Streamer.emitCodeAlignment(BB->getAlign(), &*BC.STI,
444                                  BB->getAlignmentMaxBytes());
445     Streamer.emitLabel(BB->getLabel());
446     if (!EmitCodeOnly) {
447       if (MCSymbol *EntrySymbol = BF.getSecondaryEntryPointSymbol(*BB))
448         Streamer.emitLabel(EntrySymbol);
449     }
450 
451     // Check if special alignment for macro-fusion is needed.
452     bool MayNeedMacroFusionAlignment =
453         (opts::AlignMacroOpFusion == MFT_ALL) ||
454         (opts::AlignMacroOpFusion == MFT_HOT && BB->getKnownExecutionCount());
455     BinaryBasicBlock::const_iterator MacroFusionPair;
456     if (MayNeedMacroFusionAlignment) {
457       MacroFusionPair = BB->getMacroOpFusionPair();
458       if (MacroFusionPair == BB->end())
459         MayNeedMacroFusionAlignment = false;
460     }
461 
462     SMLoc LastLocSeen;
463     // Remember if the last instruction emitted was a prefix.
464     bool LastIsPrefix = false;
465     for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
466       MCInst &Instr = *I;
467 
468       if (EmitCodeOnly && BC.MIB->isPseudo(Instr))
469         continue;
470 
471       // Handle pseudo instructions.
472       if (BC.MIB->isCFI(Instr)) {
473         emitCFIInstruction(*BF.getCFIFor(Instr));
474         continue;
475       }
476 
477       // Handle macro-fusion alignment. If we emitted a prefix as
478       // the last instruction, we should've already emitted the associated
479       // alignment hint, so don't emit it twice.
480       if (MayNeedMacroFusionAlignment && !LastIsPrefix &&
481           I == MacroFusionPair) {
482         // This assumes the second instruction in the macro-op pair will get
483         // assigned to its own MCRelaxableFragment. Since all JCC instructions
484         // are relaxable, we should be safe.
485       }
486 
487       if (!EmitCodeOnly) {
488         // A symbol to be emitted before the instruction to mark its location.
489         MCSymbol *InstrLabel = BC.MIB->getLabel(Instr);
490 
491         if (opts::UpdateDebugSections && BF.getDWARFUnit()) {
492           LastLocSeen = emitLineInfo(BF, Instr.getLoc(), LastLocSeen,
493                                      FirstInstr, InstrLabel);
494           FirstInstr = false;
495         }
496 
497         // Prepare to tag this location with a label if we need to keep track of
498         // the location of calls/returns for BOLT address translation maps
499         if (BF.requiresAddressTranslation() && BC.MIB->getOffset(Instr)) {
500           const uint32_t Offset = *BC.MIB->getOffset(Instr);
501           if (!InstrLabel)
502             InstrLabel = BC.Ctx->createTempSymbol();
503           BB->getLocSyms().emplace_back(Offset, InstrLabel);
504         }
505 
506         if (InstrLabel)
507           Streamer.emitLabel(InstrLabel);
508       }
509 
510       // Emit sized NOPs via MCAsmBackend::writeNopData() interface on x86.
511       // This is a workaround for invalid NOPs handling by asm/disasm layer.
512       if (BC.MIB->isNoop(Instr) && BC.isX86()) {
513         if (std::optional<uint32_t> Size = BC.MIB->getSize(Instr)) {
514           SmallString<15> Code;
515           raw_svector_ostream VecOS(Code);
516           BC.MAB->writeNopData(VecOS, *Size, BC.STI.get());
517           Streamer.emitBytes(Code);
518           continue;
519         }
520       }
521 
522       Streamer.emitInstruction(Instr, *BC.STI);
523       LastIsPrefix = BC.MIB->isPrefix(Instr);
524     }
525   }
526 
527   if (!EmitCodeOnly)
528     emitConstantIslands(BF, FF.isSplitFragment());
529 }
530 
531 void BinaryEmitter::emitConstantIslands(BinaryFunction &BF, bool EmitColdPart,
532                                         BinaryFunction *OnBehalfOf) {
533   if (!BF.hasIslandsInfo())
534     return;
535 
536   BinaryFunction::IslandInfo &Islands = BF.getIslandInfo();
537   if (Islands.DataOffsets.empty() && Islands.Dependency.empty())
538     return;
539 
540   // AArch64 requires CI to be aligned to 8 bytes due to access instructions
541   // restrictions. E.g. the ldr with imm, where imm must be aligned to 8 bytes.
542   const uint16_t Alignment = OnBehalfOf
543                                  ? OnBehalfOf->getConstantIslandAlignment()
544                                  : BF.getConstantIslandAlignment();
545   Streamer.emitCodeAlignment(Align(Alignment), &*BC.STI);
546 
547   if (!OnBehalfOf) {
548     if (!EmitColdPart)
549       Streamer.emitLabel(BF.getFunctionConstantIslandLabel());
550     else
551       Streamer.emitLabel(BF.getFunctionColdConstantIslandLabel());
552   }
553 
554   assert((!OnBehalfOf || Islands.Proxies[OnBehalfOf].size() > 0) &&
555          "spurious OnBehalfOf constant island emission");
556 
557   assert(!BF.isInjected() &&
558          "injected functions should not have constant islands");
559   // Raw contents of the function.
560   StringRef SectionContents = BF.getOriginSection()->getContents();
561 
562   // Raw contents of the function.
563   StringRef FunctionContents = SectionContents.substr(
564       BF.getAddress() - BF.getOriginSection()->getAddress(), BF.getMaxSize());
565 
566   if (opts::Verbosity && !OnBehalfOf)
567     outs() << "BOLT-INFO: emitting constant island for function " << BF << "\n";
568 
569   // We split the island into smaller blocks and output labels between them.
570   auto IS = Islands.Offsets.begin();
571   for (auto DataIter = Islands.DataOffsets.begin();
572        DataIter != Islands.DataOffsets.end(); ++DataIter) {
573     uint64_t FunctionOffset = *DataIter;
574     uint64_t EndOffset = 0ULL;
575 
576     // Determine size of this data chunk
577     auto NextData = std::next(DataIter);
578     auto CodeIter = Islands.CodeOffsets.lower_bound(*DataIter);
579     if (CodeIter == Islands.CodeOffsets.end() &&
580         NextData == Islands.DataOffsets.end())
581       EndOffset = BF.getMaxSize();
582     else if (CodeIter == Islands.CodeOffsets.end())
583       EndOffset = *NextData;
584     else if (NextData == Islands.DataOffsets.end())
585       EndOffset = *CodeIter;
586     else
587       EndOffset = (*CodeIter > *NextData) ? *NextData : *CodeIter;
588 
589     if (FunctionOffset == EndOffset)
590       continue; // Size is zero, nothing to emit
591 
592     auto emitCI = [&](uint64_t &FunctionOffset, uint64_t EndOffset) {
593       if (FunctionOffset >= EndOffset)
594         return;
595 
596       for (auto It = Islands.Relocations.lower_bound(FunctionOffset);
597            It != Islands.Relocations.end(); ++It) {
598         if (It->first >= EndOffset)
599           break;
600 
601         const Relocation &Relocation = It->second;
602         if (FunctionOffset < Relocation.Offset) {
603           Streamer.emitBytes(
604               FunctionContents.slice(FunctionOffset, Relocation.Offset));
605           FunctionOffset = Relocation.Offset;
606         }
607 
608         LLVM_DEBUG(
609             dbgs() << "BOLT-DEBUG: emitting constant island relocation"
610                    << " for " << BF << " at offset 0x"
611                    << Twine::utohexstr(Relocation.Offset) << " with size "
612                    << Relocation::getSizeForType(Relocation.Type) << '\n');
613 
614         FunctionOffset += Relocation.emit(&Streamer);
615       }
616 
617       assert(FunctionOffset <= EndOffset && "overflow error");
618       if (FunctionOffset < EndOffset) {
619         Streamer.emitBytes(FunctionContents.slice(FunctionOffset, EndOffset));
620         FunctionOffset = EndOffset;
621       }
622     };
623 
624     // Emit labels, relocs and data
625     while (IS != Islands.Offsets.end() && IS->first < EndOffset) {
626       auto NextLabelOffset =
627           IS == Islands.Offsets.end() ? EndOffset : IS->first;
628       auto NextStop = std::min(NextLabelOffset, EndOffset);
629       assert(NextStop <= EndOffset && "internal overflow error");
630       emitCI(FunctionOffset, NextStop);
631       if (IS != Islands.Offsets.end() && FunctionOffset == IS->first) {
632         // This is a slightly complex code to decide which label to emit. We
633         // have 4 cases to handle: regular symbol, cold symbol, regular or cold
634         // symbol being emitted on behalf of an external function.
635         if (!OnBehalfOf) {
636           if (!EmitColdPart) {
637             LLVM_DEBUG(dbgs() << "BOLT-DEBUG: emitted label "
638                               << IS->second->getName() << " at offset 0x"
639                               << Twine::utohexstr(IS->first) << '\n');
640             if (IS->second->isUndefined())
641               Streamer.emitLabel(IS->second);
642             else
643               assert(BF.hasName(std::string(IS->second->getName())));
644           } else if (Islands.ColdSymbols.count(IS->second) != 0) {
645             LLVM_DEBUG(dbgs()
646                        << "BOLT-DEBUG: emitted label "
647                        << Islands.ColdSymbols[IS->second]->getName() << '\n');
648             if (Islands.ColdSymbols[IS->second]->isUndefined())
649               Streamer.emitLabel(Islands.ColdSymbols[IS->second]);
650           }
651         } else {
652           if (!EmitColdPart) {
653             if (MCSymbol *Sym = Islands.Proxies[OnBehalfOf][IS->second]) {
654               LLVM_DEBUG(dbgs() << "BOLT-DEBUG: emitted label "
655                                 << Sym->getName() << '\n');
656               Streamer.emitLabel(Sym);
657             }
658           } else if (MCSymbol *Sym =
659                          Islands.ColdProxies[OnBehalfOf][IS->second]) {
660             LLVM_DEBUG(dbgs() << "BOLT-DEBUG: emitted label " << Sym->getName()
661                               << '\n');
662             Streamer.emitLabel(Sym);
663           }
664         }
665         ++IS;
666       }
667     }
668     assert(FunctionOffset <= EndOffset && "overflow error");
669     emitCI(FunctionOffset, EndOffset);
670   }
671   assert(IS == Islands.Offsets.end() && "some symbols were not emitted!");
672 
673   if (OnBehalfOf)
674     return;
675   // Now emit constant islands from other functions that we may have used in
676   // this function.
677   for (BinaryFunction *ExternalFunc : Islands.Dependency)
678     emitConstantIslands(*ExternalFunc, EmitColdPart, &BF);
679 }
680 
681 SMLoc BinaryEmitter::emitLineInfo(const BinaryFunction &BF, SMLoc NewLoc,
682                                   SMLoc PrevLoc, bool FirstInstr,
683                                   MCSymbol *&InstrLabel) {
684   DWARFUnit *FunctionCU = BF.getDWARFUnit();
685   const DWARFDebugLine::LineTable *FunctionLineTable = BF.getDWARFLineTable();
686   assert(FunctionCU && "cannot emit line info for function without CU");
687 
688   DebugLineTableRowRef RowReference = DebugLineTableRowRef::fromSMLoc(NewLoc);
689 
690   // Check if no new line info needs to be emitted.
691   if (RowReference == DebugLineTableRowRef::NULL_ROW ||
692       NewLoc.getPointer() == PrevLoc.getPointer())
693     return PrevLoc;
694 
695   unsigned CurrentFilenum = 0;
696   const DWARFDebugLine::LineTable *CurrentLineTable = FunctionLineTable;
697 
698   // If the CU id from the current instruction location does not
699   // match the CU id from the current function, it means that we
700   // have come across some inlined code.  We must look up the CU
701   // for the instruction's original function and get the line table
702   // from that.
703   const uint64_t FunctionUnitIndex = FunctionCU->getOffset();
704   const uint32_t CurrentUnitIndex = RowReference.DwCompileUnitIndex;
705   if (CurrentUnitIndex != FunctionUnitIndex) {
706     CurrentLineTable = BC.DwCtx->getLineTableForUnit(
707         BC.DwCtx->getCompileUnitForOffset(CurrentUnitIndex));
708     // Add filename from the inlined function to the current CU.
709     CurrentFilenum = BC.addDebugFilenameToUnit(
710         FunctionUnitIndex, CurrentUnitIndex,
711         CurrentLineTable->Rows[RowReference.RowIndex - 1].File);
712   }
713 
714   const DWARFDebugLine::Row &CurrentRow =
715       CurrentLineTable->Rows[RowReference.RowIndex - 1];
716   if (!CurrentFilenum)
717     CurrentFilenum = CurrentRow.File;
718 
719   unsigned Flags = (DWARF2_FLAG_IS_STMT * CurrentRow.IsStmt) |
720                    (DWARF2_FLAG_BASIC_BLOCK * CurrentRow.BasicBlock) |
721                    (DWARF2_FLAG_PROLOGUE_END * CurrentRow.PrologueEnd) |
722                    (DWARF2_FLAG_EPILOGUE_BEGIN * CurrentRow.EpilogueBegin);
723 
724   // Always emit is_stmt at the beginning of function fragment.
725   if (FirstInstr)
726     Flags |= DWARF2_FLAG_IS_STMT;
727 
728   BC.Ctx->setCurrentDwarfLoc(CurrentFilenum, CurrentRow.Line, CurrentRow.Column,
729                              Flags, CurrentRow.Isa, CurrentRow.Discriminator);
730   const MCDwarfLoc &DwarfLoc = BC.Ctx->getCurrentDwarfLoc();
731   BC.Ctx->clearDwarfLocSeen();
732 
733   if (!InstrLabel)
734     InstrLabel = BC.Ctx->createTempSymbol();
735 
736   BC.getDwarfLineTable(FunctionUnitIndex)
737       .getMCLineSections()
738       .addLineEntry(MCDwarfLineEntry(InstrLabel, DwarfLoc),
739                     Streamer.getCurrentSectionOnly());
740 
741   return NewLoc;
742 }
743 
744 void BinaryEmitter::emitLineInfoEnd(const BinaryFunction &BF,
745                                     MCSymbol *FunctionEndLabel) {
746   DWARFUnit *FunctionCU = BF.getDWARFUnit();
747   assert(FunctionCU && "DWARF unit expected");
748   BC.Ctx->setCurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_END_SEQUENCE, 0, 0);
749   const MCDwarfLoc &DwarfLoc = BC.Ctx->getCurrentDwarfLoc();
750   BC.Ctx->clearDwarfLocSeen();
751   BC.getDwarfLineTable(FunctionCU->getOffset())
752       .getMCLineSections()
753       .addLineEntry(MCDwarfLineEntry(FunctionEndLabel, DwarfLoc),
754                     Streamer.getCurrentSectionOnly());
755 }
756 
757 void BinaryEmitter::emitJumpTables(const BinaryFunction &BF) {
758   MCSection *ReadOnlySection = BC.MOFI->getReadOnlySection();
759   MCSection *ReadOnlyColdSection = BC.MOFI->getContext().getELFSection(
760       ".rodata.cold", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
761 
762   if (!BF.hasJumpTables())
763     return;
764 
765   if (opts::PrintJumpTables)
766     outs() << "BOLT-INFO: jump tables for function " << BF << ":\n";
767 
768   for (auto &JTI : BF.jumpTables()) {
769     JumpTable &JT = *JTI.second;
770     // Only emit shared jump tables once, when processing the first parent
771     if (JT.Parents.size() > 1 && JT.Parents[0] != &BF)
772       continue;
773     if (opts::PrintJumpTables)
774       JT.print(outs());
775     if (opts::JumpTables == JTS_BASIC && BC.HasRelocations) {
776       JT.updateOriginal();
777     } else {
778       MCSection *HotSection, *ColdSection;
779       if (opts::JumpTables == JTS_BASIC) {
780         // In non-relocation mode we have to emit jump tables in local sections.
781         // This way we only overwrite them when the corresponding function is
782         // overwritten.
783         std::string Name = ".local." + JT.Labels[0]->getName().str();
784         std::replace(Name.begin(), Name.end(), '/', '.');
785         BinarySection &Section =
786             BC.registerOrUpdateSection(Name, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
787         Section.setAnonymous(true);
788         JT.setOutputSection(Section);
789         HotSection = BC.getDataSection(Name);
790         ColdSection = HotSection;
791       } else {
792         if (BF.isSimple()) {
793           HotSection = ReadOnlySection;
794           ColdSection = ReadOnlyColdSection;
795         } else {
796           HotSection = BF.hasProfile() ? ReadOnlySection : ReadOnlyColdSection;
797           ColdSection = HotSection;
798         }
799       }
800       emitJumpTable(JT, HotSection, ColdSection);
801     }
802   }
803 }
804 
805 void BinaryEmitter::emitJumpTable(const JumpTable &JT, MCSection *HotSection,
806                                   MCSection *ColdSection) {
807   // Pre-process entries for aggressive splitting.
808   // Each label represents a separate switch table and gets its own count
809   // determining its destination.
810   std::map<MCSymbol *, uint64_t> LabelCounts;
811   if (opts::JumpTables > JTS_SPLIT && !JT.Counts.empty()) {
812     MCSymbol *CurrentLabel = JT.Labels.at(0);
813     uint64_t CurrentLabelCount = 0;
814     for (unsigned Index = 0; Index < JT.Entries.size(); ++Index) {
815       auto LI = JT.Labels.find(Index * JT.EntrySize);
816       if (LI != JT.Labels.end()) {
817         LabelCounts[CurrentLabel] = CurrentLabelCount;
818         CurrentLabel = LI->second;
819         CurrentLabelCount = 0;
820       }
821       CurrentLabelCount += JT.Counts[Index].Count;
822     }
823     LabelCounts[CurrentLabel] = CurrentLabelCount;
824   } else {
825     Streamer.switchSection(JT.Count > 0 ? HotSection : ColdSection);
826     Streamer.emitValueToAlignment(Align(JT.EntrySize));
827   }
828   MCSymbol *LastLabel = nullptr;
829   uint64_t Offset = 0;
830   for (MCSymbol *Entry : JT.Entries) {
831     auto LI = JT.Labels.find(Offset);
832     if (LI != JT.Labels.end()) {
833       LLVM_DEBUG({
834         dbgs() << "BOLT-DEBUG: emitting jump table " << LI->second->getName()
835                << " (originally was at address 0x"
836                << Twine::utohexstr(JT.getAddress() + Offset)
837                << (Offset ? ") as part of larger jump table\n" : ")\n");
838       });
839       if (!LabelCounts.empty()) {
840         LLVM_DEBUG(dbgs() << "BOLT-DEBUG: jump table count: "
841                           << LabelCounts[LI->second] << '\n');
842         if (LabelCounts[LI->second] > 0)
843           Streamer.switchSection(HotSection);
844         else
845           Streamer.switchSection(ColdSection);
846         Streamer.emitValueToAlignment(Align(JT.EntrySize));
847       }
848       // Emit all labels registered at the address of this jump table
849       // to sync with our global symbol table.  We may have two labels
850       // registered at this address if one label was created via
851       // getOrCreateGlobalSymbol() (e.g. LEA instructions referencing
852       // this location) and another via getOrCreateJumpTable().  This
853       // creates a race where the symbols created by these two
854       // functions may or may not be the same, but they are both
855       // registered in our symbol table at the same address. By
856       // emitting them all here we make sure there is no ambiguity
857       // that depends on the order that these symbols were created, so
858       // whenever this address is referenced in the binary, it is
859       // certain to point to the jump table identified at this
860       // address.
861       if (BinaryData *BD = BC.getBinaryDataByName(LI->second->getName())) {
862         for (MCSymbol *S : BD->getSymbols())
863           Streamer.emitLabel(S);
864       } else {
865         Streamer.emitLabel(LI->second);
866       }
867       LastLabel = LI->second;
868     }
869     if (JT.Type == JumpTable::JTT_NORMAL) {
870       Streamer.emitSymbolValue(Entry, JT.OutputEntrySize);
871     } else { // JTT_PIC
872       const MCSymbolRefExpr *JTExpr =
873           MCSymbolRefExpr::create(LastLabel, Streamer.getContext());
874       const MCSymbolRefExpr *E =
875           MCSymbolRefExpr::create(Entry, Streamer.getContext());
876       const MCBinaryExpr *Value =
877           MCBinaryExpr::createSub(E, JTExpr, Streamer.getContext());
878       Streamer.emitValue(Value, JT.EntrySize);
879     }
880     Offset += JT.EntrySize;
881   }
882 }
883 
884 void BinaryEmitter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
885   switch (Inst.getOperation()) {
886   default:
887     llvm_unreachable("Unexpected instruction");
888   case MCCFIInstruction::OpDefCfaOffset:
889     Streamer.emitCFIDefCfaOffset(Inst.getOffset());
890     break;
891   case MCCFIInstruction::OpAdjustCfaOffset:
892     Streamer.emitCFIAdjustCfaOffset(Inst.getOffset());
893     break;
894   case MCCFIInstruction::OpDefCfa:
895     Streamer.emitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
896     break;
897   case MCCFIInstruction::OpDefCfaRegister:
898     Streamer.emitCFIDefCfaRegister(Inst.getRegister());
899     break;
900   case MCCFIInstruction::OpOffset:
901     Streamer.emitCFIOffset(Inst.getRegister(), Inst.getOffset());
902     break;
903   case MCCFIInstruction::OpRegister:
904     Streamer.emitCFIRegister(Inst.getRegister(), Inst.getRegister2());
905     break;
906   case MCCFIInstruction::OpWindowSave:
907     Streamer.emitCFIWindowSave();
908     break;
909   case MCCFIInstruction::OpNegateRAState:
910     Streamer.emitCFINegateRAState();
911     break;
912   case MCCFIInstruction::OpSameValue:
913     Streamer.emitCFISameValue(Inst.getRegister());
914     break;
915   case MCCFIInstruction::OpGnuArgsSize:
916     Streamer.emitCFIGnuArgsSize(Inst.getOffset());
917     break;
918   case MCCFIInstruction::OpEscape:
919     Streamer.AddComment(Inst.getComment());
920     Streamer.emitCFIEscape(Inst.getValues());
921     break;
922   case MCCFIInstruction::OpRestore:
923     Streamer.emitCFIRestore(Inst.getRegister());
924     break;
925   case MCCFIInstruction::OpUndefined:
926     Streamer.emitCFIUndefined(Inst.getRegister());
927     break;
928   }
929 }
930 
931 // The code is based on EHStreamer::emitExceptionTable().
932 void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) {
933   const BinaryFunction::CallSitesRange Sites =
934       BF.getCallSites(FF.getFragmentNum());
935   if (Sites.empty())
936     return;
937 
938   // Calculate callsite table size. Size of each callsite entry is:
939   //
940   //  sizeof(start) + sizeof(length) + sizeof(LP) + sizeof(uleb128(action))
941   //
942   // or
943   //
944   //  sizeof(dwarf::DW_EH_PE_data4) * 3 + sizeof(uleb128(action))
945   uint64_t CallSiteTableLength = llvm::size(Sites) * 4 * 3;
946   for (const auto &FragmentCallSite : Sites)
947     CallSiteTableLength += getULEB128Size(FragmentCallSite.second.Action);
948 
949   Streamer.switchSection(BC.MOFI->getLSDASection());
950 
951   const unsigned TTypeEncoding = BF.getLSDATypeEncoding();
952   const unsigned TTypeEncodingSize = BC.getDWARFEncodingSize(TTypeEncoding);
953   const uint16_t TTypeAlignment = 4;
954 
955   // Type tables have to be aligned at 4 bytes.
956   Streamer.emitValueToAlignment(Align(TTypeAlignment));
957 
958   // Emit the LSDA label.
959   MCSymbol *LSDASymbol = BF.getLSDASymbol(FF.getFragmentNum());
960   assert(LSDASymbol && "no LSDA symbol set");
961   Streamer.emitLabel(LSDASymbol);
962 
963   // Corresponding FDE start.
964   const MCSymbol *StartSymbol = BF.getSymbol(FF.getFragmentNum());
965 
966   // Emit the LSDA header.
967 
968   // If LPStart is omitted, then the start of the FDE is used as a base for
969   // landing pad displacements. Then if a cold fragment starts with
970   // a landing pad, this means that the first landing pad offset will be 0.
971   // As a result, the exception handling runtime will ignore this landing pad
972   // because zero offset denotes the absence of a landing pad.
973   // For this reason, when the binary has fixed starting address we emit LPStart
974   // as 0 and output the absolute value of the landing pad in the table.
975   //
976   // If the base address can change, we cannot use absolute addresses for
977   // landing pads (at least not without runtime relocations). Hence, we fall
978   // back to emitting landing pads relative to the FDE start.
979   // As we are emitting label differences, we have to guarantee both labels are
980   // defined in the same section and hence cannot place the landing pad into a
981   // cold fragment when the corresponding call site is in the hot fragment.
982   // Because of this issue and the previously described issue of possible
983   // zero-offset landing pad we have to place landing pads in the same section
984   // as the corresponding invokes for shared objects.
985   std::function<void(const MCSymbol *)> emitLandingPad;
986   if (BC.HasFixedLoadAddress) {
987     Streamer.emitIntValue(dwarf::DW_EH_PE_udata4, 1); // LPStart format
988     Streamer.emitIntValue(0, 4);                      // LPStart
989     emitLandingPad = [&](const MCSymbol *LPSymbol) {
990       if (!LPSymbol)
991         Streamer.emitIntValue(0, 4);
992       else
993         Streamer.emitSymbolValue(LPSymbol, 4);
994     };
995   } else {
996     Streamer.emitIntValue(dwarf::DW_EH_PE_omit, 1); // LPStart format
997     emitLandingPad = [&](const MCSymbol *LPSymbol) {
998       if (!LPSymbol)
999         Streamer.emitIntValue(0, 4);
1000       else
1001         Streamer.emitAbsoluteSymbolDiff(LPSymbol, StartSymbol, 4);
1002     };
1003   }
1004 
1005   Streamer.emitIntValue(TTypeEncoding, 1); // TType format
1006 
1007   // See the comment in EHStreamer::emitExceptionTable() on to use
1008   // uleb128 encoding (which can use variable number of bytes to encode the same
1009   // value) to ensure type info table is properly aligned at 4 bytes without
1010   // iteratively fixing sizes of the tables.
1011   unsigned CallSiteTableLengthSize = getULEB128Size(CallSiteTableLength);
1012   unsigned TTypeBaseOffset =
1013       sizeof(int8_t) +                 // Call site format
1014       CallSiteTableLengthSize +        // Call site table length size
1015       CallSiteTableLength +            // Call site table length
1016       BF.getLSDAActionTable().size() + // Actions table size
1017       BF.getLSDATypeTable().size() * TTypeEncodingSize; // Types table size
1018   unsigned TTypeBaseOffsetSize = getULEB128Size(TTypeBaseOffset);
1019   unsigned TotalSize = sizeof(int8_t) +      // LPStart format
1020                        sizeof(int8_t) +      // TType format
1021                        TTypeBaseOffsetSize + // TType base offset size
1022                        TTypeBaseOffset;      // TType base offset
1023   unsigned SizeAlign = (4 - TotalSize) & 3;
1024 
1025   if (TTypeEncoding != dwarf::DW_EH_PE_omit)
1026     // Account for any extra padding that will be added to the call site table
1027     // length.
1028     Streamer.emitULEB128IntValue(TTypeBaseOffset,
1029                                  /*PadTo=*/TTypeBaseOffsetSize + SizeAlign);
1030 
1031   // Emit the landing pad call site table. We use signed data4 since we can emit
1032   // a landing pad in a different part of the split function that could appear
1033   // earlier in the address space than LPStart.
1034   Streamer.emitIntValue(dwarf::DW_EH_PE_sdata4, 1);
1035   Streamer.emitULEB128IntValue(CallSiteTableLength);
1036 
1037   for (const auto &FragmentCallSite : Sites) {
1038     const BinaryFunction::CallSite &CallSite = FragmentCallSite.second;
1039     const MCSymbol *BeginLabel = CallSite.Start;
1040     const MCSymbol *EndLabel = CallSite.End;
1041 
1042     assert(BeginLabel && "start EH label expected");
1043     assert(EndLabel && "end EH label expected");
1044 
1045     // Start of the range is emitted relative to the start of current
1046     // function split part.
1047     Streamer.emitAbsoluteSymbolDiff(BeginLabel, StartSymbol, 4);
1048     Streamer.emitAbsoluteSymbolDiff(EndLabel, BeginLabel, 4);
1049     emitLandingPad(CallSite.LP);
1050     Streamer.emitULEB128IntValue(CallSite.Action);
1051   }
1052 
1053   // Write out action, type, and type index tables at the end.
1054   //
1055   // For action and type index tables there's no need to change the original
1056   // table format unless we are doing function splitting, in which case we can
1057   // split and optimize the tables.
1058   //
1059   // For type table we (re-)encode the table using TTypeEncoding matching
1060   // the current assembler mode.
1061   for (uint8_t const &Byte : BF.getLSDAActionTable())
1062     Streamer.emitIntValue(Byte, 1);
1063 
1064   const BinaryFunction::LSDATypeTableTy &TypeTable =
1065       (TTypeEncoding & dwarf::DW_EH_PE_indirect) ? BF.getLSDATypeAddressTable()
1066                                                  : BF.getLSDATypeTable();
1067   assert(TypeTable.size() == BF.getLSDATypeTable().size() &&
1068          "indirect type table size mismatch");
1069 
1070   for (int Index = TypeTable.size() - 1; Index >= 0; --Index) {
1071     const uint64_t TypeAddress = TypeTable[Index];
1072     switch (TTypeEncoding & 0x70) {
1073     default:
1074       llvm_unreachable("unsupported TTypeEncoding");
1075     case dwarf::DW_EH_PE_absptr:
1076       Streamer.emitIntValue(TypeAddress, TTypeEncodingSize);
1077       break;
1078     case dwarf::DW_EH_PE_pcrel: {
1079       if (TypeAddress) {
1080         const MCSymbol *TypeSymbol =
1081             BC.getOrCreateGlobalSymbol(TypeAddress, "TI", 0, TTypeAlignment);
1082         MCSymbol *DotSymbol = BC.Ctx->createNamedTempSymbol();
1083         Streamer.emitLabel(DotSymbol);
1084         const MCBinaryExpr *SubDotExpr = MCBinaryExpr::createSub(
1085             MCSymbolRefExpr::create(TypeSymbol, *BC.Ctx),
1086             MCSymbolRefExpr::create(DotSymbol, *BC.Ctx), *BC.Ctx);
1087         Streamer.emitValue(SubDotExpr, TTypeEncodingSize);
1088       } else {
1089         Streamer.emitIntValue(0, TTypeEncodingSize);
1090       }
1091       break;
1092     }
1093     }
1094   }
1095   for (uint8_t const &Byte : BF.getLSDATypeIndexTable())
1096     Streamer.emitIntValue(Byte, 1);
1097 }
1098 
1099 void BinaryEmitter::emitDebugLineInfoForOriginalFunctions() {
1100   // If a function is in a CU containing at least one processed function, we
1101   // have to rewrite the whole line table for that CU. For unprocessed functions
1102   // we use data from the input line table.
1103   for (auto &It : BC.getBinaryFunctions()) {
1104     const BinaryFunction &Function = It.second;
1105 
1106     // If the function was emitted, its line info was emitted with it.
1107     if (Function.isEmitted())
1108       continue;
1109 
1110     const DWARFDebugLine::LineTable *LineTable = Function.getDWARFLineTable();
1111     if (!LineTable)
1112       continue; // nothing to update for this function
1113 
1114     const uint64_t Address = Function.getAddress();
1115     std::vector<uint32_t> Results;
1116     if (!LineTable->lookupAddressRange(
1117             {Address, object::SectionedAddress::UndefSection},
1118             Function.getSize(), Results))
1119       continue;
1120 
1121     if (Results.empty())
1122       continue;
1123 
1124     // The first row returned could be the last row matching the start address.
1125     // Find the first row with the same address that is not the end of the
1126     // sequence.
1127     uint64_t FirstRow = Results.front();
1128     while (FirstRow > 0) {
1129       const DWARFDebugLine::Row &PrevRow = LineTable->Rows[FirstRow - 1];
1130       if (PrevRow.Address.Address != Address || PrevRow.EndSequence)
1131         break;
1132       --FirstRow;
1133     }
1134 
1135     const uint64_t EndOfSequenceAddress =
1136         Function.getAddress() + Function.getMaxSize();
1137     BC.getDwarfLineTable(Function.getDWARFUnit()->getOffset())
1138         .addLineTableSequence(LineTable, FirstRow, Results.back(),
1139                               EndOfSequenceAddress);
1140   }
1141 
1142   // For units that are completely unprocessed, use original debug line contents
1143   // eliminating the need to regenerate line info program.
1144   emitDebugLineInfoForUnprocessedCUs();
1145 }
1146 
1147 void BinaryEmitter::emitDebugLineInfoForUnprocessedCUs() {
1148   // Sorted list of section offsets provides boundaries for section fragments,
1149   // where each fragment is the unit's contribution to debug line section.
1150   std::vector<uint64_t> StmtListOffsets;
1151   StmtListOffsets.reserve(BC.DwCtx->getNumCompileUnits());
1152   for (const std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units()) {
1153     DWARFDie CUDie = CU->getUnitDIE();
1154     auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
1155     if (!StmtList)
1156       continue;
1157 
1158     StmtListOffsets.push_back(*StmtList);
1159   }
1160   llvm::sort(StmtListOffsets);
1161 
1162   // For each CU that was not processed, emit its line info as a binary blob.
1163   for (const std::unique_ptr<DWARFUnit> &CU : BC.DwCtx->compile_units()) {
1164     if (BC.ProcessedCUs.count(CU.get()))
1165       continue;
1166 
1167     DWARFDie CUDie = CU->getUnitDIE();
1168     auto StmtList = dwarf::toSectionOffset(CUDie.find(dwarf::DW_AT_stmt_list));
1169     if (!StmtList)
1170       continue;
1171 
1172     StringRef DebugLineContents = CU->getLineSection().Data;
1173 
1174     const uint64_t Begin = *StmtList;
1175 
1176     // Statement list ends where the next unit contribution begins, or at the
1177     // end of the section.
1178     auto It = llvm::upper_bound(StmtListOffsets, Begin);
1179     const uint64_t End =
1180         It == StmtListOffsets.end() ? DebugLineContents.size() : *It;
1181 
1182     BC.getDwarfLineTable(CU->getOffset())
1183         .addRawContents(DebugLineContents.slice(Begin, End));
1184   }
1185 }
1186 
1187 void BinaryEmitter::emitDataSections(StringRef OrgSecPrefix) {
1188   for (BinarySection &Section : BC.sections()) {
1189     if (!Section.hasRelocations())
1190       continue;
1191 
1192     StringRef Prefix = Section.hasSectionRef() ? OrgSecPrefix : "";
1193     Section.emitAsData(Streamer, Prefix + Section.getName());
1194     Section.clearRelocations();
1195   }
1196 }
1197 
1198 namespace llvm {
1199 namespace bolt {
1200 
1201 void emitBinaryContext(MCStreamer &Streamer, BinaryContext &BC,
1202                        StringRef OrgSecPrefix) {
1203   BinaryEmitter(Streamer, BC).emitAll(OrgSecPrefix);
1204 }
1205 
1206 void emitFunctionBody(MCStreamer &Streamer, BinaryFunction &BF,
1207                       FunctionFragment &FF, bool EmitCodeOnly) {
1208   BinaryEmitter(Streamer, BF.getBinaryContext())
1209       .emitFunctionBody(BF, FF, EmitCodeOnly);
1210 }
1211 
1212 } // namespace bolt
1213 } // namespace llvm
1214