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