1a34c753fSRafael Auler //===--- JumpTable.h - Representation of a jump table ---------------------===// 2a34c753fSRafael Auler // 3a34c753fSRafael Auler // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4a34c753fSRafael Auler // See https://llvm.org/LICENSE.txt for license information. 5a34c753fSRafael Auler // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6a34c753fSRafael Auler // 7a34c753fSRafael Auler //===----------------------------------------------------------------------===// 8a34c753fSRafael Auler // 9a34c753fSRafael Auler //===----------------------------------------------------------------------===// 10a34c753fSRafael Auler 11a34c753fSRafael Auler #include "bolt/Core/JumpTable.h" 12a34c753fSRafael Auler #include "bolt/Core/BinaryFunction.h" 13a34c753fSRafael Auler #include "bolt/Core/BinarySection.h" 14a34c753fSRafael Auler #include "llvm/Support/CommandLine.h" 15a34c753fSRafael Auler #include "llvm/Support/Debug.h" 16a34c753fSRafael Auler 17a34c753fSRafael Auler #define DEBUG_TYPE "bolt" 18a34c753fSRafael Auler 19a34c753fSRafael Auler using namespace llvm; 20a34c753fSRafael Auler using namespace bolt; 21a34c753fSRafael Auler 22a34c753fSRafael Auler using JumpTable = bolt::JumpTable; 23a34c753fSRafael Auler 24a34c753fSRafael Auler namespace opts { 25a34c753fSRafael Auler extern cl::opt<JumpTableSupportLevel> JumpTables; 26a34c753fSRafael Auler extern cl::opt<unsigned> Verbosity; 27*40c2e0faSMaksim Panchenko } // namespace opts 28a34c753fSRafael Auler 29a34c753fSRafael Auler bolt::JumpTable::JumpTable(MCSymbol &Symbol, uint64_t Address, size_t EntrySize, 30a34c753fSRafael Auler JumpTableType Type, LabelMapType &&Labels, 31a34c753fSRafael Auler BinaryFunction &BF, BinarySection &Section) 32a34c753fSRafael Auler : BinaryData(Symbol, Address, 0, EntrySize, Section), EntrySize(EntrySize), 33a34c753fSRafael Auler OutputEntrySize(EntrySize), Type(Type), Labels(Labels), Parent(&BF) {} 34a34c753fSRafael Auler 35a34c753fSRafael Auler std::pair<size_t, size_t> 36a34c753fSRafael Auler bolt::JumpTable::getEntriesForAddress(const uint64_t Addr) const { 37a34c753fSRafael Auler // Check if this is not an address, but a cloned JT id 38a34c753fSRafael Auler if ((int64_t)Addr < 0ll) 39a34c753fSRafael Auler return std::make_pair(0, Entries.size()); 40a34c753fSRafael Auler 41a34c753fSRafael Auler const uint64_t InstOffset = Addr - getAddress(); 42a34c753fSRafael Auler size_t StartIndex = 0, EndIndex = 0; 43a34c753fSRafael Auler uint64_t Offset = 0; 44a34c753fSRafael Auler 45a34c753fSRafael Auler for (size_t I = 0; I < Entries.size(); ++I) { 46a34c753fSRafael Auler auto LI = Labels.find(Offset); 47a34c753fSRafael Auler if (LI != Labels.end()) { 48a34c753fSRafael Auler const auto NextLI = std::next(LI); 49a34c753fSRafael Auler const uint64_t NextOffset = 50a34c753fSRafael Auler NextLI == Labels.end() ? getSize() : NextLI->first; 51a34c753fSRafael Auler if (InstOffset >= LI->first && InstOffset < NextOffset) { 52a34c753fSRafael Auler StartIndex = I; 53a34c753fSRafael Auler EndIndex = I; 54a34c753fSRafael Auler while (Offset < NextOffset) { 55a34c753fSRafael Auler ++EndIndex; 56a34c753fSRafael Auler Offset += EntrySize; 57a34c753fSRafael Auler } 58a34c753fSRafael Auler break; 59a34c753fSRafael Auler } 60a34c753fSRafael Auler } 61a34c753fSRafael Auler Offset += EntrySize; 62a34c753fSRafael Auler } 63a34c753fSRafael Auler 64a34c753fSRafael Auler return std::make_pair(StartIndex, EndIndex); 65a34c753fSRafael Auler } 66a34c753fSRafael Auler 67a34c753fSRafael Auler bool bolt::JumpTable::replaceDestination(uint64_t JTAddress, 68a34c753fSRafael Auler const MCSymbol *OldDest, 69a34c753fSRafael Auler MCSymbol *NewDest) { 70a34c753fSRafael Auler bool Patched = false; 71a34c753fSRafael Auler const std::pair<size_t, size_t> Range = getEntriesForAddress(JTAddress); 72a34c753fSRafael Auler for (auto I = &Entries[Range.first], E = &Entries[Range.second]; I != E; 73a34c753fSRafael Auler ++I) { 74a34c753fSRafael Auler MCSymbol *&Entry = *I; 75a34c753fSRafael Auler if (Entry == OldDest) { 76a34c753fSRafael Auler Patched = true; 77a34c753fSRafael Auler Entry = NewDest; 78a34c753fSRafael Auler } 79a34c753fSRafael Auler } 80a34c753fSRafael Auler return Patched; 81a34c753fSRafael Auler } 82a34c753fSRafael Auler 83a34c753fSRafael Auler void bolt::JumpTable::updateOriginal() { 84a34c753fSRafael Auler BinaryContext &BC = getSection().getBinaryContext(); 85a34c753fSRafael Auler const uint64_t BaseOffset = getAddress() - getSection().getAddress(); 86a34c753fSRafael Auler uint64_t EntryOffset = BaseOffset; 87a34c753fSRafael Auler for (MCSymbol *Entry : Entries) { 88a34c753fSRafael Auler const uint64_t RelType = 89a34c753fSRafael Auler Type == JTT_NORMAL ? ELF::R_X86_64_64 : ELF::R_X86_64_PC32; 90a34c753fSRafael Auler const uint64_t RelAddend = 91a34c753fSRafael Auler Type == JTT_NORMAL ? 0 : EntryOffset - BaseOffset; 92a34c753fSRafael Auler // Replace existing relocation with the new one to allow any modifications 93a34c753fSRafael Auler // to the original jump table. 94a34c753fSRafael Auler if (BC.HasRelocations) 95a34c753fSRafael Auler getOutputSection().removeRelocationAt(EntryOffset); 96a34c753fSRafael Auler getOutputSection().addRelocation(EntryOffset, Entry, RelType, RelAddend); 97a34c753fSRafael Auler EntryOffset += EntrySize; 98a34c753fSRafael Auler } 99a34c753fSRafael Auler } 100a34c753fSRafael Auler 101a34c753fSRafael Auler void bolt::JumpTable::print(raw_ostream &OS) const { 102a34c753fSRafael Auler uint64_t Offset = 0; 103a34c753fSRafael Auler if (Type == JTT_PIC) 104a34c753fSRafael Auler OS << "PIC "; 105a34c753fSRafael Auler OS << "Jump table " << getName() << " for function " << *Parent << " at 0x" 106a34c753fSRafael Auler << Twine::utohexstr(getAddress()) << " with a total count of " << Count 107a34c753fSRafael Auler << ":\n"; 108a34c753fSRafael Auler for (const uint64_t EntryOffset : OffsetEntries) { 109a34c753fSRafael Auler OS << " 0x" << Twine::utohexstr(EntryOffset) << '\n'; 110a34c753fSRafael Auler } 111a34c753fSRafael Auler for (const MCSymbol *Entry : Entries) { 112a34c753fSRafael Auler auto LI = Labels.find(Offset); 113a34c753fSRafael Auler if (Offset && LI != Labels.end()) { 114a34c753fSRafael Auler OS << "Jump Table " << LI->second->getName() << " at 0x" 115a34c753fSRafael Auler << Twine::utohexstr(getAddress() + Offset) 116a34c753fSRafael Auler << " (possibly part of larger jump table):\n"; 117a34c753fSRafael Auler } 118a34c753fSRafael Auler OS << format(" 0x%04" PRIx64 " : ", Offset) << Entry->getName(); 119a34c753fSRafael Auler if (!Counts.empty()) { 120*40c2e0faSMaksim Panchenko OS << " : " << Counts[Offset / EntrySize].Mispreds << "/" 121*40c2e0faSMaksim Panchenko << Counts[Offset / EntrySize].Count; 122a34c753fSRafael Auler } 123a34c753fSRafael Auler OS << '\n'; 124a34c753fSRafael Auler Offset += EntrySize; 125a34c753fSRafael Auler } 126a34c753fSRafael Auler OS << "\n\n"; 127a34c753fSRafael Auler } 128