1 //===- bolt/Rewrite/LinuxKernelRewriter.cpp -------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Support for updating Linux Kernel metadata. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "bolt/Core/BinaryFunction.h" 14 #include "bolt/Rewrite/MetadataRewriter.h" 15 #include "bolt/Rewrite/MetadataRewriters.h" 16 #include "bolt/Utils/CommandLineOpts.h" 17 #include "llvm/ADT/DenseSet.h" 18 #include "llvm/Support/BinaryStreamWriter.h" 19 #include "llvm/Support/CommandLine.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/Errc.h" 22 23 #define DEBUG_TYPE "bolt-linux" 24 25 using namespace llvm; 26 using namespace bolt; 27 28 namespace opts { 29 30 static cl::opt<bool> 31 AltInstHasPadLen("alt-inst-has-padlen", 32 cl::desc("specify that .altinstructions has padlen field"), 33 cl::init(false), cl::Hidden, cl::cat(BoltCategory)); 34 35 static cl::opt<uint32_t> 36 AltInstFeatureSize("alt-inst-feature-size", 37 cl::desc("size of feature field in .altinstructions"), 38 cl::init(2), cl::Hidden, cl::cat(BoltCategory)); 39 40 static cl::opt<bool> 41 DumpAltInstructions("dump-alt-instructions", 42 cl::desc("dump Linux alternative instructions info"), 43 cl::init(false), cl::Hidden, cl::cat(BoltCategory)); 44 45 static cl::opt<bool> 46 DumpExceptions("dump-linux-exceptions", 47 cl::desc("dump Linux kernel exception table"), 48 cl::init(false), cl::Hidden, cl::cat(BoltCategory)); 49 50 static cl::opt<bool> 51 DumpORC("dump-orc", cl::desc("dump raw ORC unwind information (sorted)"), 52 cl::init(false), cl::Hidden, cl::cat(BoltCategory)); 53 54 static cl::opt<bool> DumpParavirtualPatchSites( 55 "dump-para-sites", cl::desc("dump Linux kernel paravitual patch sites"), 56 cl::init(false), cl::Hidden, cl::cat(BoltCategory)); 57 58 static cl::opt<bool> DumpStaticCalls("dump-static-calls", 59 cl::desc("dump Linux kernel static calls"), 60 cl::init(false), cl::Hidden, 61 cl::cat(BoltCategory)); 62 63 static cl::opt<bool> 64 PrintORC("print-orc", 65 cl::desc("print ORC unwind information for instructions"), 66 cl::init(true), cl::Hidden, cl::cat(BoltCategory)); 67 68 } // namespace opts 69 70 /// Linux Kernel supports stack unwinding using ORC (oops rewind capability). 71 /// ORC state at every IP can be described by the following data structure. 72 struct ORCState { 73 int16_t SPOffset; 74 int16_t BPOffset; 75 int16_t Info; 76 77 bool operator==(const ORCState &Other) const { 78 return SPOffset == Other.SPOffset && BPOffset == Other.BPOffset && 79 Info == Other.Info; 80 } 81 82 bool operator!=(const ORCState &Other) const { return !(*this == Other); } 83 }; 84 85 /// Section terminator ORC entry. 86 static ORCState NullORC = {0, 0, 0}; 87 88 /// Basic printer for ORC entry. It does not provide the same level of 89 /// information as objtool (for now). 90 inline raw_ostream &operator<<(raw_ostream &OS, const ORCState &E) { 91 if (!opts::PrintORC) 92 return OS; 93 if (E != NullORC) 94 OS << format("{sp: %d, bp: %d, info: 0x%x}", E.SPOffset, E.BPOffset, 95 E.Info); 96 else 97 OS << "{terminator}"; 98 99 return OS; 100 } 101 102 namespace { 103 104 class LinuxKernelRewriter final : public MetadataRewriter { 105 /// Linux Kernel special sections point to a specific instruction in many 106 /// cases. Unlike SDTMarkerInfo, these markers can come from different 107 /// sections. 108 struct LKInstructionMarkerInfo { 109 uint64_t SectionOffset; 110 int32_t PCRelativeOffset; 111 bool IsPCRelative; 112 StringRef SectionName; 113 }; 114 115 /// Map linux kernel program locations/instructions to their pointers in 116 /// special linux kernel sections 117 std::unordered_map<uint64_t, std::vector<LKInstructionMarkerInfo>> LKMarkers; 118 119 /// Linux ORC sections. 120 ErrorOr<BinarySection &> ORCUnwindSection = std::errc::bad_address; 121 ErrorOr<BinarySection &> ORCUnwindIPSection = std::errc::bad_address; 122 123 /// Size of entries in ORC sections. 124 static constexpr size_t ORC_UNWIND_ENTRY_SIZE = 6; 125 static constexpr size_t ORC_UNWIND_IP_ENTRY_SIZE = 4; 126 127 struct ORCListEntry { 128 uint64_t IP; /// Instruction address. 129 BinaryFunction *BF; /// Binary function corresponding to the entry. 130 ORCState ORC; /// Stack unwind info in ORC format. 131 132 /// ORC entries are sorted by their IPs. Terminator entries (NullORC) 133 /// should precede other entries with the same address. 134 bool operator<(const ORCListEntry &Other) const { 135 if (IP < Other.IP) 136 return 1; 137 if (IP > Other.IP) 138 return 0; 139 return ORC == NullORC && Other.ORC != NullORC; 140 } 141 }; 142 143 using ORCListType = std::vector<ORCListEntry>; 144 ORCListType ORCEntries; 145 146 /// Number of entries in the input file ORC sections. 147 uint64_t NumORCEntries = 0; 148 149 /// Section containing static call table. 150 ErrorOr<BinarySection &> StaticCallSection = std::errc::bad_address; 151 uint64_t StaticCallTableAddress = 0; 152 static constexpr size_t STATIC_CALL_ENTRY_SIZE = 8; 153 154 struct StaticCallInfo { 155 uint32_t ID; /// Identifier of the entry in the table. 156 BinaryFunction *Function; /// Function containing associated call. 157 MCSymbol *Label; /// Label attached to the call. 158 }; 159 using StaticCallListType = std::vector<StaticCallInfo>; 160 StaticCallListType StaticCallEntries; 161 162 /// Section containing the Linux exception table. 163 ErrorOr<BinarySection &> ExceptionsSection = std::errc::bad_address; 164 static constexpr size_t EXCEPTION_TABLE_ENTRY_SIZE = 12; 165 166 /// Functions with exception handling code. 167 DenseSet<BinaryFunction *> FunctionsWithExceptions; 168 169 /// Section with paravirtual patch sites. 170 ErrorOr<BinarySection &> ParavirtualPatchSection = std::errc::bad_address; 171 172 /// Alignment of paravirtual patch structures. 173 static constexpr size_t PARA_PATCH_ALIGN = 8; 174 175 /// .altinstructions section. 176 ErrorOr<BinarySection &> AltInstrSection = std::errc::bad_address; 177 178 /// Section containing Linux bug table. 179 ErrorOr<BinarySection &> BugTableSection = std::errc::bad_address; 180 181 /// Size of bug_entry struct. 182 static constexpr size_t BUG_TABLE_ENTRY_SIZE = 12; 183 184 /// Insert an LKMarker for a given code pointer \p PC from a non-code section 185 /// \p SectionName. 186 void insertLKMarker(uint64_t PC, uint64_t SectionOffset, 187 int32_t PCRelativeOffset, bool IsPCRelative, 188 StringRef SectionName); 189 190 /// Process linux kernel special sections and their relocations. 191 void processLKSections(); 192 193 /// Process special linux kernel section, .pci_fixup. 194 void processLKPCIFixup(); 195 196 /// Process __ksymtab and __ksymtab_gpl. 197 void processLKKSymtab(bool IsGPL = false); 198 199 /// Process special linux kernel section, .smp_locks. 200 void processLKSMPLocks(); 201 202 /// Update LKMarkers' locations for the output binary. 203 void updateLKMarkers(); 204 205 /// Read ORC unwind information and annotate instructions. 206 Error readORCTables(); 207 208 /// Update ORC for functions once CFG is constructed. 209 Error processORCPostCFG(); 210 211 /// Update ORC data in the binary. 212 Error rewriteORCTables(); 213 214 /// Static call table handling. 215 Error readStaticCalls(); 216 Error rewriteStaticCalls(); 217 218 Error readExceptionTable(); 219 Error rewriteExceptionTable(); 220 221 /// Paravirtual instruction patch sites. 222 Error readParaInstructions(); 223 224 Error readBugTable(); 225 226 /// Read alternative instruction info from .altinstructions. 227 Error readAltInstructions(); 228 229 /// Mark instructions referenced by kernel metadata. 230 Error markInstructions(); 231 232 public: 233 LinuxKernelRewriter(BinaryContext &BC) 234 : MetadataRewriter("linux-kernel-rewriter", BC) {} 235 236 Error preCFGInitializer() override { 237 processLKSections(); 238 if (Error E = markInstructions()) 239 return E; 240 241 if (Error E = readORCTables()) 242 return E; 243 244 if (Error E = readStaticCalls()) 245 return E; 246 247 if (Error E = readExceptionTable()) 248 return E; 249 250 if (Error E = readParaInstructions()) 251 return E; 252 253 if (Error E = readBugTable()) 254 return E; 255 256 if (Error E = readAltInstructions()) 257 return E; 258 259 return Error::success(); 260 } 261 262 Error postCFGInitializer() override { 263 if (Error E = processORCPostCFG()) 264 return E; 265 266 return Error::success(); 267 } 268 269 Error preEmitFinalizer() override { 270 // Since rewriteExceptionTable() can mark functions as non-simple, run it 271 // before other rewriters that depend on simple/emit status. 272 if (Error E = rewriteExceptionTable()) 273 return E; 274 275 if (Error E = rewriteORCTables()) 276 return E; 277 278 if (Error E = rewriteStaticCalls()) 279 return E; 280 281 return Error::success(); 282 } 283 284 Error postEmitFinalizer() override { 285 updateLKMarkers(); 286 287 return Error::success(); 288 } 289 }; 290 291 Error LinuxKernelRewriter::markInstructions() { 292 for (const uint64_t PC : llvm::make_first_range(LKMarkers)) { 293 BinaryFunction *BF = BC.getBinaryFunctionContainingAddress(PC); 294 295 if (!BF || !BC.shouldEmit(*BF)) 296 continue; 297 298 const uint64_t Offset = PC - BF->getAddress(); 299 MCInst *Inst = BF->getInstructionAtOffset(Offset); 300 if (!Inst) 301 return createStringError(errc::executable_format_error, 302 "no instruction matches kernel marker offset"); 303 304 BC.MIB->setOffset(*Inst, static_cast<uint32_t>(Offset)); 305 306 BF->setHasSDTMarker(true); 307 } 308 309 return Error::success(); 310 } 311 312 void LinuxKernelRewriter::insertLKMarker(uint64_t PC, uint64_t SectionOffset, 313 int32_t PCRelativeOffset, 314 bool IsPCRelative, 315 StringRef SectionName) { 316 LKMarkers[PC].emplace_back(LKInstructionMarkerInfo{ 317 SectionOffset, PCRelativeOffset, IsPCRelative, SectionName}); 318 } 319 320 void LinuxKernelRewriter::processLKSections() { 321 processLKPCIFixup(); 322 processLKKSymtab(); 323 processLKKSymtab(true); 324 processLKSMPLocks(); 325 } 326 327 /// Process .pci_fixup section of Linux Kernel. 328 /// This section contains a list of entries for different PCI devices and their 329 /// corresponding hook handler (code pointer where the fixup 330 /// code resides, usually on x86_64 it is an entry PC relative 32 bit offset). 331 /// Documentation is in include/linux/pci.h. 332 void LinuxKernelRewriter::processLKPCIFixup() { 333 ErrorOr<BinarySection &> SectionOrError = 334 BC.getUniqueSectionByName(".pci_fixup"); 335 if (!SectionOrError) 336 return; 337 338 const uint64_t SectionSize = SectionOrError->getSize(); 339 const uint64_t SectionAddress = SectionOrError->getAddress(); 340 assert((SectionSize % 16) == 0 && ".pci_fixup size is not a multiple of 16"); 341 342 for (uint64_t I = 12; I + 4 <= SectionSize; I += 16) { 343 const uint64_t PC = SectionAddress + I; 344 ErrorOr<uint64_t> Offset = BC.getSignedValueAtAddress(PC, 4); 345 assert(Offset && "cannot read value from .pci_fixup"); 346 const int32_t SignedOffset = *Offset; 347 const uint64_t HookupAddress = PC + SignedOffset; 348 BinaryFunction *HookupFunction = 349 BC.getBinaryFunctionAtAddress(HookupAddress); 350 assert(HookupFunction && "expected function for entry in .pci_fixup"); 351 BC.addRelocation(PC, HookupFunction->getSymbol(), Relocation::getPC32(), 0, 352 *Offset); 353 } 354 } 355 356 /// Process __ksymtab[_gpl] sections of Linux Kernel. 357 /// This section lists all the vmlinux symbols that kernel modules can access. 358 /// 359 /// All the entries are 4 bytes each and hence we can read them by one by one 360 /// and ignore the ones that are not pointing to the .text section. All pointers 361 /// are PC relative offsets. Always, points to the beginning of the function. 362 void LinuxKernelRewriter::processLKKSymtab(bool IsGPL) { 363 StringRef SectionName = "__ksymtab"; 364 if (IsGPL) 365 SectionName = "__ksymtab_gpl"; 366 ErrorOr<BinarySection &> SectionOrError = 367 BC.getUniqueSectionByName(SectionName); 368 assert(SectionOrError && 369 "__ksymtab[_gpl] section not found in Linux Kernel binary"); 370 const uint64_t SectionSize = SectionOrError->getSize(); 371 const uint64_t SectionAddress = SectionOrError->getAddress(); 372 assert((SectionSize % 4) == 0 && 373 "The size of the __ksymtab[_gpl] section should be a multiple of 4"); 374 375 for (uint64_t I = 0; I < SectionSize; I += 4) { 376 const uint64_t EntryAddress = SectionAddress + I; 377 ErrorOr<uint64_t> Offset = BC.getSignedValueAtAddress(EntryAddress, 4); 378 assert(Offset && "Reading valid PC-relative offset for a ksymtab entry"); 379 const int32_t SignedOffset = *Offset; 380 const uint64_t RefAddress = EntryAddress + SignedOffset; 381 BinaryFunction *BF = BC.getBinaryFunctionAtAddress(RefAddress); 382 if (!BF) 383 continue; 384 385 BC.addRelocation(EntryAddress, BF->getSymbol(), Relocation::getPC32(), 0, 386 *Offset); 387 } 388 } 389 390 /// .smp_locks section contains PC-relative references to instructions with LOCK 391 /// prefix. The prefix can be converted to NOP at boot time on non-SMP systems. 392 void LinuxKernelRewriter::processLKSMPLocks() { 393 ErrorOr<BinarySection &> SectionOrError = 394 BC.getUniqueSectionByName(".smp_locks"); 395 if (!SectionOrError) 396 return; 397 398 uint64_t SectionSize = SectionOrError->getSize(); 399 const uint64_t SectionAddress = SectionOrError->getAddress(); 400 assert((SectionSize % 4) == 0 && 401 "The size of the .smp_locks section should be a multiple of 4"); 402 403 for (uint64_t I = 0; I < SectionSize; I += 4) { 404 const uint64_t EntryAddress = SectionAddress + I; 405 ErrorOr<uint64_t> Offset = BC.getSignedValueAtAddress(EntryAddress, 4); 406 assert(Offset && "Reading valid PC-relative offset for a .smp_locks entry"); 407 int32_t SignedOffset = *Offset; 408 uint64_t RefAddress = EntryAddress + SignedOffset; 409 410 BinaryFunction *ContainingBF = 411 BC.getBinaryFunctionContainingAddress(RefAddress); 412 if (!ContainingBF) 413 continue; 414 415 insertLKMarker(RefAddress, I, SignedOffset, true, ".smp_locks"); 416 } 417 } 418 419 void LinuxKernelRewriter::updateLKMarkers() { 420 if (LKMarkers.size() == 0) 421 return; 422 423 std::unordered_map<std::string, uint64_t> PatchCounts; 424 for (std::pair<const uint64_t, std::vector<LKInstructionMarkerInfo>> 425 &LKMarkerInfoKV : LKMarkers) { 426 const uint64_t OriginalAddress = LKMarkerInfoKV.first; 427 const BinaryFunction *BF = 428 BC.getBinaryFunctionContainingAddress(OriginalAddress, false, true); 429 if (!BF) 430 continue; 431 432 uint64_t NewAddress = BF->translateInputToOutputAddress(OriginalAddress); 433 if (NewAddress == 0) 434 continue; 435 436 // Apply base address. 437 if (OriginalAddress >= 0xffffffff00000000 && NewAddress < 0xffffffff) 438 NewAddress = NewAddress + 0xffffffff00000000; 439 440 if (OriginalAddress == NewAddress) 441 continue; 442 443 for (LKInstructionMarkerInfo &LKMarkerInfo : LKMarkerInfoKV.second) { 444 StringRef SectionName = LKMarkerInfo.SectionName; 445 SimpleBinaryPatcher *LKPatcher; 446 ErrorOr<BinarySection &> BSec = BC.getUniqueSectionByName(SectionName); 447 assert(BSec && "missing section info for kernel section"); 448 if (!BSec->getPatcher()) 449 BSec->registerPatcher(std::make_unique<SimpleBinaryPatcher>()); 450 LKPatcher = static_cast<SimpleBinaryPatcher *>(BSec->getPatcher()); 451 PatchCounts[std::string(SectionName)]++; 452 if (LKMarkerInfo.IsPCRelative) 453 LKPatcher->addLE32Patch(LKMarkerInfo.SectionOffset, 454 NewAddress - OriginalAddress + 455 LKMarkerInfo.PCRelativeOffset); 456 else 457 LKPatcher->addLE64Patch(LKMarkerInfo.SectionOffset, NewAddress); 458 } 459 } 460 BC.outs() << "BOLT-INFO: patching linux kernel sections. Total patches per " 461 "section are as follows:\n"; 462 for (const std::pair<const std::string, uint64_t> &KV : PatchCounts) 463 BC.outs() << " Section: " << KV.first << ", patch-counts: " << KV.second 464 << '\n'; 465 } 466 467 Error LinuxKernelRewriter::readORCTables() { 468 // NOTE: we should ignore relocations for orc tables as the tables are sorted 469 // post-link time and relocations are not updated. 470 ORCUnwindSection = BC.getUniqueSectionByName(".orc_unwind"); 471 ORCUnwindIPSection = BC.getUniqueSectionByName(".orc_unwind_ip"); 472 473 if (!ORCUnwindSection && !ORCUnwindIPSection) 474 return Error::success(); 475 476 if (!ORCUnwindSection || !ORCUnwindIPSection) 477 return createStringError(errc::executable_format_error, 478 "missing ORC section"); 479 480 NumORCEntries = ORCUnwindIPSection->getSize() / ORC_UNWIND_IP_ENTRY_SIZE; 481 if (ORCUnwindSection->getSize() != NumORCEntries * ORC_UNWIND_ENTRY_SIZE || 482 ORCUnwindIPSection->getSize() != NumORCEntries * ORC_UNWIND_IP_ENTRY_SIZE) 483 return createStringError(errc::executable_format_error, 484 "ORC entries number mismatch detected"); 485 486 const uint64_t IPSectionAddress = ORCUnwindIPSection->getAddress(); 487 DataExtractor OrcDE = DataExtractor(ORCUnwindSection->getContents(), 488 BC.AsmInfo->isLittleEndian(), 489 BC.AsmInfo->getCodePointerSize()); 490 DataExtractor IPDE = DataExtractor(ORCUnwindIPSection->getContents(), 491 BC.AsmInfo->isLittleEndian(), 492 BC.AsmInfo->getCodePointerSize()); 493 DataExtractor::Cursor ORCCursor(0); 494 DataExtractor::Cursor IPCursor(0); 495 uint64_t PrevIP = 0; 496 for (uint32_t Index = 0; Index < NumORCEntries; ++Index) { 497 const uint64_t IP = 498 IPSectionAddress + IPCursor.tell() + (int32_t)IPDE.getU32(IPCursor); 499 500 // Consume the status of the cursor. 501 if (!IPCursor) 502 return createStringError(errc::executable_format_error, 503 "out of bounds while reading ORC IP table"); 504 505 if (IP < PrevIP && opts::Verbosity) 506 BC.errs() << "BOLT-WARNING: out of order IP 0x" << Twine::utohexstr(IP) 507 << " detected while reading ORC\n"; 508 509 PrevIP = IP; 510 511 // Store all entries, includes those we are not going to update as the 512 // tables need to be sorted globally before being written out. 513 ORCEntries.push_back(ORCListEntry()); 514 ORCListEntry &Entry = ORCEntries.back(); 515 516 Entry.IP = IP; 517 Entry.ORC.SPOffset = (int16_t)OrcDE.getU16(ORCCursor); 518 Entry.ORC.BPOffset = (int16_t)OrcDE.getU16(ORCCursor); 519 Entry.ORC.Info = (int16_t)OrcDE.getU16(ORCCursor); 520 Entry.BF = nullptr; 521 522 // Consume the status of the cursor. 523 if (!ORCCursor) 524 return createStringError(errc::executable_format_error, 525 "out of bounds while reading ORC"); 526 527 if (Entry.ORC == NullORC) 528 continue; 529 530 BinaryFunction *&BF = Entry.BF; 531 BF = BC.getBinaryFunctionContainingAddress(IP, /*CheckPastEnd*/ true); 532 533 // If the entry immediately pointing past the end of the function is not 534 // the terminator entry, then it does not belong to this function. 535 if (BF && BF->getAddress() + BF->getSize() == IP) 536 BF = 0; 537 538 if (!BF) { 539 if (opts::Verbosity) 540 BC.errs() << "BOLT-WARNING: no binary function found matching ORC 0x" 541 << Twine::utohexstr(IP) << ": " << Entry.ORC << '\n'; 542 continue; 543 } 544 545 BF->setHasORC(true); 546 547 if (!BF->hasInstructions()) 548 continue; 549 550 MCInst *Inst = BF->getInstructionAtOffset(IP - BF->getAddress()); 551 if (!Inst) 552 return createStringError( 553 errc::executable_format_error, 554 "no instruction at address 0x%" PRIx64 " in .orc_unwind_ip", IP); 555 556 // Some addresses will have two entries associated with them. The first 557 // one being a "weak" section terminator. Since we ignore the terminator, 558 // we should only assign one entry per instruction. 559 if (BC.MIB->hasAnnotation(*Inst, "ORC")) 560 return createStringError( 561 errc::executable_format_error, 562 "duplicate non-terminal ORC IP 0x%" PRIx64 " in .orc_unwind_ip", IP); 563 564 BC.MIB->addAnnotation(*Inst, "ORC", Entry.ORC); 565 } 566 567 BC.outs() << "BOLT-INFO: parsed " << NumORCEntries << " ORC entries\n"; 568 569 if (opts::DumpORC) { 570 BC.outs() << "BOLT-INFO: ORC unwind information:\n"; 571 for (const ORCListEntry &E : ORCEntries) { 572 BC.outs() << "0x" << Twine::utohexstr(E.IP) << ": " << E.ORC; 573 if (E.BF) 574 BC.outs() << ": " << *E.BF; 575 BC.outs() << '\n'; 576 } 577 } 578 579 // Add entries for functions that don't have explicit ORC info at the start. 580 // We'll have the correct info for them even if ORC for the preceding function 581 // changes. 582 ORCListType NewEntries; 583 for (BinaryFunction &BF : llvm::make_second_range(BC.getBinaryFunctions())) { 584 auto It = llvm::partition_point(ORCEntries, [&](const ORCListEntry &E) { 585 return E.IP <= BF.getAddress(); 586 }); 587 if (It != ORCEntries.begin()) 588 --It; 589 590 if (It->BF == &BF) 591 continue; 592 593 if (It->ORC == NullORC && It->IP == BF.getAddress()) { 594 assert(!It->BF); 595 It->BF = &BF; 596 continue; 597 } 598 599 NewEntries.push_back({BF.getAddress(), &BF, It->ORC}); 600 if (It->ORC != NullORC) 601 BF.setHasORC(true); 602 } 603 604 llvm::copy(NewEntries, std::back_inserter(ORCEntries)); 605 llvm::sort(ORCEntries); 606 607 if (opts::DumpORC) { 608 BC.outs() << "BOLT-INFO: amended ORC unwind information:\n"; 609 for (const ORCListEntry &E : ORCEntries) { 610 BC.outs() << "0x" << Twine::utohexstr(E.IP) << ": " << E.ORC; 611 if (E.BF) 612 BC.outs() << ": " << *E.BF; 613 BC.outs() << '\n'; 614 } 615 } 616 617 return Error::success(); 618 } 619 620 Error LinuxKernelRewriter::processORCPostCFG() { 621 if (!NumORCEntries) 622 return Error::success(); 623 624 // Propagate ORC to the rest of the function. We can annotate every 625 // instruction in every function, but to minimize the overhead, we annotate 626 // the first instruction in every basic block to reflect the state at the 627 // entry. This way, the ORC state can be calculated based on annotations 628 // regardless of the basic block layout. Note that if we insert/delete 629 // instructions, we must take care to attach ORC info to the new/deleted ones. 630 for (BinaryFunction &BF : llvm::make_second_range(BC.getBinaryFunctions())) { 631 632 std::optional<ORCState> CurrentState; 633 for (BinaryBasicBlock &BB : BF) { 634 for (MCInst &Inst : BB) { 635 ErrorOr<ORCState> State = 636 BC.MIB->tryGetAnnotationAs<ORCState>(Inst, "ORC"); 637 638 if (State) { 639 CurrentState = *State; 640 continue; 641 } 642 643 // Get state for the start of the function. 644 if (!CurrentState) { 645 // A terminator entry (NullORC) can match the function address. If 646 // there's also a non-terminator entry, it will be placed after the 647 // terminator. Hence, we are looking for the last ORC entry that 648 // matches the address. 649 auto It = 650 llvm::partition_point(ORCEntries, [&](const ORCListEntry &E) { 651 return E.IP <= BF.getAddress(); 652 }); 653 if (It != ORCEntries.begin()) 654 --It; 655 656 assert(It->IP == BF.getAddress() && (!It->BF || It->BF == &BF) && 657 "ORC info at function entry expected."); 658 659 if (It->ORC == NullORC && BF.hasORC()) { 660 BC.errs() << "BOLT-WARNING: ORC unwind info excludes prologue for " 661 << BF << '\n'; 662 } 663 664 It->BF = &BF; 665 666 CurrentState = It->ORC; 667 if (It->ORC != NullORC) 668 BF.setHasORC(true); 669 } 670 671 // While printing ORC, attach info to every instruction for convenience. 672 if (opts::PrintORC || &Inst == &BB.front()) 673 BC.MIB->addAnnotation(Inst, "ORC", *CurrentState); 674 } 675 } 676 } 677 678 return Error::success(); 679 } 680 681 Error LinuxKernelRewriter::rewriteORCTables() { 682 if (!NumORCEntries) 683 return Error::success(); 684 685 // Update ORC sections in-place. As we change the code, the number of ORC 686 // entries may increase for some functions. However, as we remove terminator 687 // redundancy (see below), more space is freed up and we should always be able 688 // to fit new ORC tables in the reserved space. 689 auto createInPlaceWriter = [&](BinarySection &Section) -> BinaryStreamWriter { 690 const size_t Size = Section.getSize(); 691 uint8_t *NewContents = new uint8_t[Size]; 692 Section.updateContents(NewContents, Size); 693 Section.setOutputFileOffset(Section.getInputFileOffset()); 694 return BinaryStreamWriter({NewContents, Size}, BC.AsmInfo->isLittleEndian() 695 ? endianness::little 696 : endianness::big); 697 }; 698 BinaryStreamWriter UnwindWriter = createInPlaceWriter(*ORCUnwindSection); 699 BinaryStreamWriter UnwindIPWriter = createInPlaceWriter(*ORCUnwindIPSection); 700 701 uint64_t NumEmitted = 0; 702 std::optional<ORCState> LastEmittedORC; 703 auto emitORCEntry = [&](const uint64_t IP, const ORCState &ORC, 704 MCSymbol *Label = 0, bool Force = false) -> Error { 705 if (LastEmittedORC && ORC == *LastEmittedORC && !Force) 706 return Error::success(); 707 708 LastEmittedORC = ORC; 709 710 if (++NumEmitted > NumORCEntries) 711 return createStringError(errc::executable_format_error, 712 "exceeded the number of allocated ORC entries"); 713 714 if (Label) 715 ORCUnwindIPSection->addRelocation(UnwindIPWriter.getOffset(), Label, 716 Relocation::getPC32(), /*Addend*/ 0); 717 718 const int32_t IPValue = 719 IP - ORCUnwindIPSection->getAddress() - UnwindIPWriter.getOffset(); 720 if (Error E = UnwindIPWriter.writeInteger(IPValue)) 721 return E; 722 723 if (Error E = UnwindWriter.writeInteger(ORC.SPOffset)) 724 return E; 725 if (Error E = UnwindWriter.writeInteger(ORC.BPOffset)) 726 return E; 727 if (Error E = UnwindWriter.writeInteger(ORC.Info)) 728 return E; 729 730 return Error::success(); 731 }; 732 733 // Emit new ORC entries for the emitted function. 734 auto emitORC = [&](const BinaryFunction &BF) -> Error { 735 assert(!BF.isSplit() && "Split functions not supported by ORC writer yet."); 736 737 ORCState CurrentState = NullORC; 738 for (BinaryBasicBlock *BB : BF.getLayout().blocks()) { 739 for (MCInst &Inst : *BB) { 740 ErrorOr<ORCState> ErrorOrState = 741 BC.MIB->tryGetAnnotationAs<ORCState>(Inst, "ORC"); 742 if (!ErrorOrState || *ErrorOrState == CurrentState) 743 continue; 744 745 // Issue label for the instruction. 746 MCSymbol *Label = 747 BC.MIB->getOrCreateInstLabel(Inst, "__ORC_", BC.Ctx.get()); 748 749 if (Error E = emitORCEntry(0, *ErrorOrState, Label)) 750 return E; 751 752 CurrentState = *ErrorOrState; 753 } 754 } 755 756 return Error::success(); 757 }; 758 759 for (ORCListEntry &Entry : ORCEntries) { 760 // Emit original entries for functions that we haven't modified. 761 if (!Entry.BF || !BC.shouldEmit(*Entry.BF)) { 762 // Emit terminator only if it marks the start of a function. 763 if (Entry.ORC == NullORC && !Entry.BF) 764 continue; 765 if (Error E = emitORCEntry(Entry.IP, Entry.ORC)) 766 return E; 767 continue; 768 } 769 770 // Emit all ORC entries for a function referenced by an entry and skip over 771 // the rest of entries for this function by resetting its ORC attribute. 772 if (Entry.BF->hasORC()) { 773 if (Error E = emitORC(*Entry.BF)) 774 return E; 775 Entry.BF->setHasORC(false); 776 } 777 } 778 779 LLVM_DEBUG(dbgs() << "BOLT-DEBUG: emitted " << NumEmitted 780 << " ORC entries\n"); 781 782 // Replicate terminator entry at the end of sections to match the original 783 // table sizes. 784 const BinaryFunction &LastBF = BC.getBinaryFunctions().rbegin()->second; 785 const uint64_t LastIP = LastBF.getAddress() + LastBF.getMaxSize(); 786 while (UnwindWriter.bytesRemaining()) { 787 if (Error E = emitORCEntry(LastIP, NullORC, nullptr, /*Force*/ true)) 788 return E; 789 } 790 791 return Error::success(); 792 } 793 794 /// The static call site table is created by objtool and contains entries in the 795 /// following format: 796 /// 797 /// struct static_call_site { 798 /// s32 addr; 799 /// s32 key; 800 /// }; 801 /// 802 Error LinuxKernelRewriter::readStaticCalls() { 803 const BinaryData *StaticCallTable = 804 BC.getBinaryDataByName("__start_static_call_sites"); 805 if (!StaticCallTable) 806 return Error::success(); 807 808 StaticCallTableAddress = StaticCallTable->getAddress(); 809 810 const BinaryData *Stop = BC.getBinaryDataByName("__stop_static_call_sites"); 811 if (!Stop) 812 return createStringError(errc::executable_format_error, 813 "missing __stop_static_call_sites symbol"); 814 815 ErrorOr<BinarySection &> ErrorOrSection = 816 BC.getSectionForAddress(StaticCallTableAddress); 817 if (!ErrorOrSection) 818 return createStringError(errc::executable_format_error, 819 "no section matching __start_static_call_sites"); 820 821 StaticCallSection = *ErrorOrSection; 822 if (!StaticCallSection->containsAddress(Stop->getAddress() - 1)) 823 return createStringError(errc::executable_format_error, 824 "__stop_static_call_sites not in the same section " 825 "as __start_static_call_sites"); 826 827 if ((Stop->getAddress() - StaticCallTableAddress) % STATIC_CALL_ENTRY_SIZE) 828 return createStringError(errc::executable_format_error, 829 "static call table size error"); 830 831 const uint64_t SectionAddress = StaticCallSection->getAddress(); 832 DataExtractor DE(StaticCallSection->getContents(), 833 BC.AsmInfo->isLittleEndian(), 834 BC.AsmInfo->getCodePointerSize()); 835 DataExtractor::Cursor Cursor(StaticCallTableAddress - SectionAddress); 836 uint32_t EntryID = 0; 837 while (Cursor && Cursor.tell() < Stop->getAddress() - SectionAddress) { 838 const uint64_t CallAddress = 839 SectionAddress + Cursor.tell() + (int32_t)DE.getU32(Cursor); 840 const uint64_t KeyAddress = 841 SectionAddress + Cursor.tell() + (int32_t)DE.getU32(Cursor); 842 843 // Consume the status of the cursor. 844 if (!Cursor) 845 return createStringError(errc::executable_format_error, 846 "out of bounds while reading static calls"); 847 848 ++EntryID; 849 850 if (opts::DumpStaticCalls) { 851 BC.outs() << "Static Call Site: " << EntryID << '\n'; 852 BC.outs() << "\tCallAddress: 0x" << Twine::utohexstr(CallAddress) 853 << "\n\tKeyAddress: 0x" << Twine::utohexstr(KeyAddress) 854 << '\n'; 855 } 856 857 BinaryFunction *BF = BC.getBinaryFunctionContainingAddress(CallAddress); 858 if (!BF) 859 continue; 860 861 if (!BC.shouldEmit(*BF)) 862 continue; 863 864 if (!BF->hasInstructions()) 865 continue; 866 867 MCInst *Inst = BF->getInstructionAtOffset(CallAddress - BF->getAddress()); 868 if (!Inst) 869 return createStringError(errc::executable_format_error, 870 "no instruction at call site address 0x%" PRIx64, 871 CallAddress); 872 873 // Check for duplicate entries. 874 if (BC.MIB->hasAnnotation(*Inst, "StaticCall")) 875 return createStringError(errc::executable_format_error, 876 "duplicate static call site at 0x%" PRIx64, 877 CallAddress); 878 879 BC.MIB->addAnnotation(*Inst, "StaticCall", EntryID); 880 881 MCSymbol *Label = 882 BC.MIB->getOrCreateInstLabel(*Inst, "__SC_", BC.Ctx.get()); 883 884 StaticCallEntries.push_back({EntryID, BF, Label}); 885 } 886 887 BC.outs() << "BOLT-INFO: parsed " << StaticCallEntries.size() 888 << " static call entries\n"; 889 890 return Error::success(); 891 } 892 893 /// The static call table is sorted during boot time in 894 /// static_call_sort_entries(). This makes it possible to update existing 895 /// entries in-place ignoring their relative order. 896 Error LinuxKernelRewriter::rewriteStaticCalls() { 897 if (!StaticCallTableAddress || !StaticCallSection) 898 return Error::success(); 899 900 for (auto &Entry : StaticCallEntries) { 901 if (!Entry.Function) 902 continue; 903 904 BinaryFunction &BF = *Entry.Function; 905 if (!BC.shouldEmit(BF)) 906 continue; 907 908 // Create a relocation against the label. 909 const uint64_t EntryOffset = StaticCallTableAddress - 910 StaticCallSection->getAddress() + 911 (Entry.ID - 1) * STATIC_CALL_ENTRY_SIZE; 912 StaticCallSection->addRelocation(EntryOffset, Entry.Label, 913 ELF::R_X86_64_PC32, /*Addend*/ 0); 914 } 915 916 return Error::success(); 917 } 918 919 /// Instructions that access user-space memory can cause page faults. These 920 /// faults will be handled by the kernel and execution will resume at the fixup 921 /// code location if the address was invalid. The kernel uses the exception 922 /// table to match the faulting instruction to its fixup. The table consists of 923 /// the following entries: 924 /// 925 /// struct exception_table_entry { 926 /// int insn; 927 /// int fixup; 928 /// int data; 929 /// }; 930 /// 931 /// More info at: 932 /// https://www.kernel.org/doc/Documentation/x86/exception-tables.txt 933 Error LinuxKernelRewriter::readExceptionTable() { 934 ExceptionsSection = BC.getUniqueSectionByName("__ex_table"); 935 if (!ExceptionsSection) 936 return Error::success(); 937 938 if (ExceptionsSection->getSize() % EXCEPTION_TABLE_ENTRY_SIZE) 939 return createStringError(errc::executable_format_error, 940 "exception table size error"); 941 942 const uint64_t SectionAddress = ExceptionsSection->getAddress(); 943 DataExtractor DE(ExceptionsSection->getContents(), 944 BC.AsmInfo->isLittleEndian(), 945 BC.AsmInfo->getCodePointerSize()); 946 DataExtractor::Cursor Cursor(0); 947 uint32_t EntryID = 0; 948 while (Cursor && Cursor.tell() < ExceptionsSection->getSize()) { 949 const uint64_t InstAddress = 950 SectionAddress + Cursor.tell() + (int32_t)DE.getU32(Cursor); 951 const uint64_t FixupAddress = 952 SectionAddress + Cursor.tell() + (int32_t)DE.getU32(Cursor); 953 const uint64_t Data = DE.getU32(Cursor); 954 955 // Consume the status of the cursor. 956 if (!Cursor) 957 return createStringError(errc::executable_format_error, 958 "out of bounds while reading exception table"); 959 960 ++EntryID; 961 962 if (opts::DumpExceptions) { 963 BC.outs() << "Exception Entry: " << EntryID << '\n'; 964 BC.outs() << "\tInsn: 0x" << Twine::utohexstr(InstAddress) << '\n' 965 << "\tFixup: 0x" << Twine::utohexstr(FixupAddress) << '\n' 966 << "\tData: 0x" << Twine::utohexstr(Data) << '\n'; 967 } 968 969 MCInst *Inst = nullptr; 970 MCSymbol *FixupLabel = nullptr; 971 972 BinaryFunction *InstBF = BC.getBinaryFunctionContainingAddress(InstAddress); 973 if (InstBF && BC.shouldEmit(*InstBF)) { 974 Inst = InstBF->getInstructionAtOffset(InstAddress - InstBF->getAddress()); 975 if (!Inst) 976 return createStringError(errc::executable_format_error, 977 "no instruction at address 0x%" PRIx64 978 " in exception table", 979 InstAddress); 980 BC.MIB->addAnnotation(*Inst, "ExceptionEntry", EntryID); 981 FunctionsWithExceptions.insert(InstBF); 982 } 983 984 if (!InstBF && opts::Verbosity) { 985 BC.outs() << "BOLT-INFO: no function matches instruction at 0x" 986 << Twine::utohexstr(InstAddress) 987 << " referenced by Linux exception table\n"; 988 } 989 990 BinaryFunction *FixupBF = 991 BC.getBinaryFunctionContainingAddress(FixupAddress); 992 if (FixupBF && BC.shouldEmit(*FixupBF)) { 993 const uint64_t Offset = FixupAddress - FixupBF->getAddress(); 994 if (!FixupBF->getInstructionAtOffset(Offset)) 995 return createStringError(errc::executable_format_error, 996 "no instruction at fixup address 0x%" PRIx64 997 " in exception table", 998 FixupAddress); 999 FixupLabel = Offset ? FixupBF->addEntryPointAtOffset(Offset) 1000 : FixupBF->getSymbol(); 1001 if (Inst) 1002 BC.MIB->addAnnotation(*Inst, "Fixup", FixupLabel->getName()); 1003 FunctionsWithExceptions.insert(FixupBF); 1004 } 1005 1006 if (!FixupBF && opts::Verbosity) { 1007 BC.outs() << "BOLT-INFO: no function matches fixup code at 0x" 1008 << Twine::utohexstr(FixupAddress) 1009 << " referenced by Linux exception table\n"; 1010 } 1011 } 1012 1013 BC.outs() << "BOLT-INFO: parsed " 1014 << ExceptionsSection->getSize() / EXCEPTION_TABLE_ENTRY_SIZE 1015 << " exception table entries\n"; 1016 1017 return Error::success(); 1018 } 1019 1020 /// Depending on the value of CONFIG_BUILDTIME_TABLE_SORT, the kernel expects 1021 /// the exception table to be sorted. Hence we have to sort it after code 1022 /// reordering. 1023 Error LinuxKernelRewriter::rewriteExceptionTable() { 1024 // Disable output of functions with exceptions before rewrite support is 1025 // added. 1026 for (BinaryFunction *BF : FunctionsWithExceptions) 1027 BF->setSimple(false); 1028 1029 return Error::success(); 1030 } 1031 1032 /// .parainsrtuctions section contains information for patching parvirtual call 1033 /// instructions during runtime. The entries in the section are in the form: 1034 /// 1035 /// struct paravirt_patch_site { 1036 /// u8 *instr; /* original instructions */ 1037 /// u8 type; /* type of this instruction */ 1038 /// u8 len; /* length of original instruction */ 1039 /// }; 1040 /// 1041 /// Note that the structures are aligned at 8-byte boundary. 1042 Error LinuxKernelRewriter::readParaInstructions() { 1043 ParavirtualPatchSection = BC.getUniqueSectionByName(".parainstructions"); 1044 if (!ParavirtualPatchSection) 1045 return Error::success(); 1046 1047 DataExtractor DE = DataExtractor(ParavirtualPatchSection->getContents(), 1048 BC.AsmInfo->isLittleEndian(), 1049 BC.AsmInfo->getCodePointerSize()); 1050 uint32_t EntryID = 0; 1051 DataExtractor::Cursor Cursor(0); 1052 while (Cursor && !DE.eof(Cursor)) { 1053 const uint64_t NextOffset = alignTo(Cursor.tell(), Align(PARA_PATCH_ALIGN)); 1054 if (!DE.isValidOffset(NextOffset)) 1055 break; 1056 1057 Cursor.seek(NextOffset); 1058 1059 const uint64_t InstrLocation = DE.getU64(Cursor); 1060 const uint8_t Type = DE.getU8(Cursor); 1061 const uint8_t Len = DE.getU8(Cursor); 1062 1063 if (!Cursor) 1064 return createStringError(errc::executable_format_error, 1065 "out of bounds while reading .parainstructions"); 1066 1067 ++EntryID; 1068 1069 if (opts::DumpParavirtualPatchSites) { 1070 BC.outs() << "Paravirtual patch site: " << EntryID << '\n'; 1071 BC.outs() << "\tInstr: 0x" << Twine::utohexstr(InstrLocation) 1072 << "\n\tType: 0x" << Twine::utohexstr(Type) << "\n\tLen: 0x" 1073 << Twine::utohexstr(Len) << '\n'; 1074 } 1075 1076 BinaryFunction *BF = BC.getBinaryFunctionContainingAddress(InstrLocation); 1077 if (!BF && opts::Verbosity) { 1078 BC.outs() << "BOLT-INFO: no function matches address 0x" 1079 << Twine::utohexstr(InstrLocation) 1080 << " referenced by paravirutal patch site\n"; 1081 } 1082 1083 if (BF && BC.shouldEmit(*BF)) { 1084 MCInst *Inst = 1085 BF->getInstructionAtOffset(InstrLocation - BF->getAddress()); 1086 if (!Inst) 1087 return createStringError(errc::executable_format_error, 1088 "no instruction at address 0x%" PRIx64 1089 " in paravirtual call site %d", 1090 InstrLocation, EntryID); 1091 BC.MIB->addAnnotation(*Inst, "ParaSite", EntryID); 1092 } 1093 } 1094 1095 BC.outs() << "BOLT-INFO: parsed " << EntryID << " paravirtual patch sites\n"; 1096 1097 return Error::success(); 1098 } 1099 1100 /// Process __bug_table section. 1101 /// This section contains information useful for kernel debugging. 1102 /// Each entry in the section is a struct bug_entry that contains a pointer to 1103 /// the ud2 instruction corresponding to the bug, corresponding file name (both 1104 /// pointers use PC relative offset addressing), line number, and flags. 1105 /// The definition of the struct bug_entry can be found in 1106 /// `include/asm-generic/bug.h` 1107 /// 1108 /// NB: find_bug() uses linear search to match an address to an entry in the bug 1109 /// table. Hence there is no need to sort entries when rewriting the table. 1110 Error LinuxKernelRewriter::readBugTable() { 1111 BugTableSection = BC.getUniqueSectionByName("__bug_table"); 1112 if (!BugTableSection) 1113 return Error::success(); 1114 1115 if (BugTableSection->getSize() % BUG_TABLE_ENTRY_SIZE) 1116 return createStringError(errc::executable_format_error, 1117 "bug table size error"); 1118 1119 const uint64_t SectionAddress = BugTableSection->getAddress(); 1120 DataExtractor DE(BugTableSection->getContents(), BC.AsmInfo->isLittleEndian(), 1121 BC.AsmInfo->getCodePointerSize()); 1122 DataExtractor::Cursor Cursor(0); 1123 uint32_t EntryID = 0; 1124 while (Cursor && Cursor.tell() < BugTableSection->getSize()) { 1125 const uint64_t Pos = Cursor.tell(); 1126 const uint64_t InstAddress = 1127 SectionAddress + Pos + (int32_t)DE.getU32(Cursor); 1128 Cursor.seek(Pos + BUG_TABLE_ENTRY_SIZE); 1129 1130 if (!Cursor) 1131 return createStringError(errc::executable_format_error, 1132 "out of bounds while reading __bug_table"); 1133 1134 ++EntryID; 1135 1136 BinaryFunction *BF = BC.getBinaryFunctionContainingAddress(InstAddress); 1137 if (!BF && opts::Verbosity) { 1138 BC.outs() << "BOLT-INFO: no function matches address 0x" 1139 << Twine::utohexstr(InstAddress) 1140 << " referenced by bug table\n"; 1141 } 1142 1143 if (BF && BC.shouldEmit(*BF)) { 1144 MCInst *Inst = BF->getInstructionAtOffset(InstAddress - BF->getAddress()); 1145 if (!Inst) 1146 return createStringError(errc::executable_format_error, 1147 "no instruction at address 0x%" PRIx64 1148 " referenced by bug table entry %d", 1149 InstAddress, EntryID); 1150 BC.MIB->addAnnotation(*Inst, "BugEntry", EntryID); 1151 } 1152 } 1153 1154 BC.outs() << "BOLT-INFO: parsed " << EntryID << " bug table entries\n"; 1155 1156 return Error::success(); 1157 } 1158 1159 /// The kernel can replace certain instruction sequences depending on hardware 1160 /// it is running on and features specified during boot time. The information 1161 /// about alternative instruction sequences is stored in .altinstructions 1162 /// section. The format of entries in this section is defined in 1163 /// arch/x86/include/asm/alternative.h: 1164 /// 1165 /// struct alt_instr { 1166 /// s32 instr_offset; 1167 /// s32 repl_offset; 1168 /// uXX feature; 1169 /// u8 instrlen; 1170 /// u8 replacementlen; 1171 /// u8 padlen; // present in older kernels 1172 /// } __packed; 1173 /// 1174 /// Note the structures is packed. 1175 Error LinuxKernelRewriter::readAltInstructions() { 1176 AltInstrSection = BC.getUniqueSectionByName(".altinstructions"); 1177 if (!AltInstrSection) 1178 return Error::success(); 1179 1180 const uint64_t Address = AltInstrSection->getAddress(); 1181 DataExtractor DE = DataExtractor(AltInstrSection->getContents(), 1182 BC.AsmInfo->isLittleEndian(), 1183 BC.AsmInfo->getCodePointerSize()); 1184 uint64_t EntryID = 0; 1185 DataExtractor::Cursor Cursor(0); 1186 while (Cursor && !DE.eof(Cursor)) { 1187 const uint64_t OrgInstAddress = 1188 Address + Cursor.tell() + (int32_t)DE.getU32(Cursor); 1189 const uint64_t AltInstAddress = 1190 Address + Cursor.tell() + (int32_t)DE.getU32(Cursor); 1191 const uint64_t Feature = DE.getUnsigned(Cursor, opts::AltInstFeatureSize); 1192 const uint8_t OrgSize = DE.getU8(Cursor); 1193 const uint8_t AltSize = DE.getU8(Cursor); 1194 1195 // Older kernels may have the padlen field. 1196 const uint8_t PadLen = opts::AltInstHasPadLen ? DE.getU8(Cursor) : 0; 1197 1198 if (!Cursor) 1199 return createStringError(errc::executable_format_error, 1200 "out of bounds while reading .altinstructions"); 1201 1202 ++EntryID; 1203 1204 if (opts::DumpAltInstructions) { 1205 BC.outs() << "Alternative instruction entry: " << EntryID 1206 << "\n\tOrg: 0x" << Twine::utohexstr(OrgInstAddress) 1207 << "\n\tAlt: 0x" << Twine::utohexstr(AltInstAddress) 1208 << "\n\tFeature: 0x" << Twine::utohexstr(Feature) 1209 << "\n\tOrgSize: " << (int)OrgSize 1210 << "\n\tAltSize: " << (int)AltSize << '\n'; 1211 if (opts::AltInstHasPadLen) 1212 BC.outs() << "\tPadLen: " << (int)PadLen << '\n'; 1213 } 1214 1215 if (AltSize > OrgSize) 1216 return createStringError(errc::executable_format_error, 1217 "error reading .altinstructions"); 1218 1219 BinaryFunction *BF = BC.getBinaryFunctionContainingAddress(OrgInstAddress); 1220 if (!BF && opts::Verbosity) { 1221 BC.outs() << "BOLT-INFO: no function matches address 0x" 1222 << Twine::utohexstr(OrgInstAddress) 1223 << " of instruction from .altinstructions\n"; 1224 } 1225 1226 BinaryFunction *AltBF = 1227 BC.getBinaryFunctionContainingAddress(AltInstAddress); 1228 if (AltBF && BC.shouldEmit(*AltBF)) { 1229 BC.errs() 1230 << "BOLT-WARNING: alternative instruction sequence found in function " 1231 << *AltBF << '\n'; 1232 AltBF->setIgnored(); 1233 } 1234 1235 if (!BF || !BC.shouldEmit(*BF)) 1236 continue; 1237 1238 if (OrgInstAddress + OrgSize > BF->getAddress() + BF->getSize()) 1239 return createStringError(errc::executable_format_error, 1240 "error reading .altinstructions"); 1241 1242 MCInst *Inst = 1243 BF->getInstructionAtOffset(OrgInstAddress - BF->getAddress()); 1244 if (!Inst) 1245 return createStringError(errc::executable_format_error, 1246 "no instruction at address 0x%" PRIx64 1247 " referenced by .altinstructions entry %d", 1248 OrgInstAddress, EntryID); 1249 1250 // There could be more than one alternative instruction sequences for the 1251 // same original instruction. Annotate each alternative separately. 1252 std::string AnnotationName = "AltInst"; 1253 unsigned N = 2; 1254 while (BC.MIB->hasAnnotation(*Inst, AnnotationName)) 1255 AnnotationName = "AltInst" + std::to_string(N++); 1256 1257 BC.MIB->addAnnotation(*Inst, AnnotationName, EntryID); 1258 1259 // Annotate all instructions from the original sequence. Note that it's not 1260 // the most efficient way to look for instructions in the address range, 1261 // but since alternative instructions are uncommon, it will do for now. 1262 for (uint32_t Offset = 1; Offset < OrgSize; ++Offset) { 1263 Inst = BF->getInstructionAtOffset(OrgInstAddress + Offset - 1264 BF->getAddress()); 1265 if (Inst) 1266 BC.MIB->addAnnotation(*Inst, AnnotationName, EntryID); 1267 } 1268 } 1269 1270 BC.outs() << "BOLT-INFO: parsed " << EntryID 1271 << " alternative instruction entries\n"; 1272 1273 return Error::success(); 1274 } 1275 1276 } // namespace 1277 1278 std::unique_ptr<MetadataRewriter> 1279 llvm::bolt::createLinuxKernelRewriter(BinaryContext &BC) { 1280 return std::make_unique<LinuxKernelRewriter>(BC); 1281 } 1282