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